blob: d1cc9b2e9b0592e0100084d2ec2b6ac02a853d5f [file] [log] [blame]
wdenk2d966952002-10-31 22:12:35 +00001/*
2 * Copied from Linux Monitor (LiMon) - Networking.
3 *
4 * Copyright 1994 - 2000 Neil Russell.
5 * (See License)
6 * Copyright 2000 Roland Borde
7 * Copyright 2000 Paolo Scaffardi
8 * Copyright 2000-2002 Wolfgang Denk, wd@denx.de
9 */
10
11/*
12 * General Desription:
13 *
14 * The user interface supports commands for BOOTP, RARP, and TFTP.
15 * Also, we support ARP internally. Depending on available data,
16 * these interact as follows:
17 *
18 * BOOTP:
19 *
20 * Prerequisites: - own ethernet address
21 * We want: - own IP address
22 * - TFTP server IP address
23 * - name of bootfile
24 * Next step: ARP
25 *
26 * RARP:
27 *
28 * Prerequisites: - own ethernet address
29 * We want: - own IP address
30 * - TFTP server IP address
31 * Next step: ARP
32 *
33 * ARP:
34 *
35 * Prerequisites: - own ethernet address
36 * - own IP address
37 * - TFTP server IP address
38 * We want: - TFTP server ethernet address
39 * Next step: TFTP
40 *
41 * DHCP:
42 *
Wolfgang Denkb2f50802005-08-12 23:43:12 +020043 * Prerequisites: - own ethernet address
44 * We want: - IP, Netmask, ServerIP, Gateway IP
45 * - bootfilename, lease time
46 * Next step: - TFTP
wdenk2d966952002-10-31 22:12:35 +000047 *
48 * TFTP:
49 *
50 * Prerequisites: - own ethernet address
51 * - own IP address
52 * - TFTP server IP address
53 * - TFTP server ethernet address
54 * - name of bootfile (if unknown, we use a default name
55 * derived from our own IP address)
56 * We want: - load the boot file
57 * Next step: none
wdenkcbd8a352004-02-24 02:00:03 +000058 *
59 * NFS:
60 *
61 * Prerequisites: - own ethernet address
62 * - own IP address
63 * - name of bootfile (if unknown, we use a default name
64 * derived from our own IP address)
65 * We want: - load the boot file
66 * Next step: none
wdenkea287de2005-04-01 00:25:43 +000067 *
68 * SNTP:
69 *
Wolfgang Denkb2f50802005-08-12 23:43:12 +020070 * Prerequisites: - own ethernet address
wdenkea287de2005-04-01 00:25:43 +000071 * - own IP address
72 * We want: - network time
73 * Next step: none
wdenk2d966952002-10-31 22:12:35 +000074 */
75
76
77#include <common.h>
78#include <watchdog.h>
79#include <command.h>
80#include <net.h>
81#include "bootp.h"
82#include "tftp.h"
83#include "rarp.h"
wdenkcbd8a352004-02-24 02:00:03 +000084#include "nfs.h"
wdenkfc3e2162003-10-08 22:33:00 +000085#ifdef CONFIG_STATUS_LED
86#include <status_led.h>
87#include <miiphy.h>
88#endif
Jon Loeliger643d1ab2007-07-09 17:45:14 -050089#if defined(CONFIG_CMD_SNTP)
wdenkea287de2005-04-01 00:25:43 +000090#include "sntp.h"
91#endif
Peter Tyser561858e2008-11-03 09:30:59 -060092#if defined(CONFIG_CDP_VERSION)
93#include <timestamp.h>
94#endif
Robin Getz1a32bf42009-07-20 14:53:54 -040095#if defined(CONFIG_CMD_DNS)
96#include "dns.h"
97#endif
wdenk2d966952002-10-31 22:12:35 +000098
Jon Loeliger643d1ab2007-07-09 17:45:14 -050099#if defined(CONFIG_CMD_NET)
wdenk2d966952002-10-31 22:12:35 +0000100
Wolfgang Denkd87080b2006-03-31 18:32:53 +0200101DECLARE_GLOBAL_DATA_PTR;
102
Guennadi Liakhovetski40cb90e2008-04-03 17:04:19 +0200103#ifndef CONFIG_ARP_TIMEOUT
Bartlomiej Sieka49f3bdb2008-10-01 15:26:28 +0200104# define ARP_TIMEOUT 5000UL /* Milliseconds before trying ARP again */
wdenk73a8b272003-06-05 19:27:42 +0000105#else
Bartlomiej Sieka49f3bdb2008-10-01 15:26:28 +0200106# define ARP_TIMEOUT CONFIG_ARP_TIMEOUT
Guennadi Liakhovetski40cb90e2008-04-03 17:04:19 +0200107#endif
108
109
110#ifndef CONFIG_NET_RETRY_COUNT
111# define ARP_TIMEOUT_COUNT 5 /* # of timeouts before giving up */
112#else
113# define ARP_TIMEOUT_COUNT CONFIG_NET_RETRY_COUNT
wdenk73a8b272003-06-05 19:27:42 +0000114#endif
115
wdenk2d966952002-10-31 22:12:35 +0000116/** BOOTP EXTENTIONS **/
117
118IPaddr_t NetOurSubnetMask=0; /* Our subnet mask (0=unknown) */
119IPaddr_t NetOurGatewayIP=0; /* Our gateways IP address */
120IPaddr_t NetOurDNSIP=0; /* Our DNS IP address */
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500121#if defined(CONFIG_BOOTP_DNS2)
stroesefe389a82003-08-28 14:17:32 +0000122IPaddr_t NetOurDNS2IP=0; /* Our 2nd DNS IP address */
123#endif
wdenk2d966952002-10-31 22:12:35 +0000124char NetOurNISDomain[32]={0,}; /* Our NIS domain */
125char NetOurHostName[32]={0,}; /* Our hostname */
126char NetOurRootPath[64]={0,}; /* Our bootpath */
127ushort NetBootFileSize=0; /* Our bootfile size in blocks */
128
David Updegraff53a5c422007-06-11 10:41:07 -0500129#ifdef CONFIG_MCAST_TFTP /* Multicast TFTP */
130IPaddr_t Mcast_addr;
131#endif
132
wdenk2d966952002-10-31 22:12:35 +0000133/** END OF BOOTP EXTENTIONS **/
134
135ulong NetBootFileXferSize; /* The actual transferred size of the bootfile (in bytes) */
136uchar NetOurEther[6]; /* Our ethernet address */
137uchar NetServerEther[6] = /* Boot server enet address */
wdenk73a8b272003-06-05 19:27:42 +0000138 { 0, 0, 0, 0, 0, 0 };
wdenk2d966952002-10-31 22:12:35 +0000139IPaddr_t NetOurIP; /* Our IP addr (0 = unknown) */
Guennadi Liakhovetski40cb90e2008-04-03 17:04:19 +0200140IPaddr_t NetServerIP; /* Server IP addr (0 = unknown) */
Mike Frysingerd9bec9f2009-07-18 21:04:08 -0400141volatile uchar *NetRxPacket; /* Current receive packet */
142int NetRxPacketLen; /* Current rx packet length */
wdenk2d966952002-10-31 22:12:35 +0000143unsigned NetIPID; /* IP packet ID */
144uchar NetBcastAddr[6] = /* Ethernet bcast address */
145 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
wdenk73a8b272003-06-05 19:27:42 +0000146uchar NetEtherNullAddr[6] =
147 { 0, 0, 0, 0, 0, 0 };
Rafal Jaworowskif85b6072007-12-27 18:19:02 +0100148#ifdef CONFIG_API
149void (*push_packet)(volatile void *, int len) = 0;
150#endif
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500151#if defined(CONFIG_CMD_CDP)
wdenk6e592382004-04-18 17:39:38 +0000152uchar NetCDPAddr[6] = /* Ethernet bcast address */
wdenka3d991b2004-04-15 21:48:45 +0000153 { 0x01, 0x00, 0x0c, 0xcc, 0xcc, 0xcc };
154#endif
wdenk2d966952002-10-31 22:12:35 +0000155int NetState; /* Network loop state */
156#ifdef CONFIG_NET_MULTI
157int NetRestartWrap = 0; /* Tried all network devices */
158static int NetRestarted = 0; /* Network loop restarted */
159static int NetDevExists = 0; /* At least one device configured */
160#endif
161
wdenk6e592382004-04-18 17:39:38 +0000162/* XXX in both little & big endian machines 0xFFFF == ntohs(-1) */
163ushort NetOurVLAN = 0xFFFF; /* default is without VLAN */
164ushort NetOurNativeVLAN = 0xFFFF; /* ditto */
wdenka3d991b2004-04-15 21:48:45 +0000165
wdenk2d966952002-10-31 22:12:35 +0000166char BootFile[128]; /* Boot File name */
167
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500168#if defined(CONFIG_CMD_PING)
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200169IPaddr_t NetPingIP; /* the ip address to ping */
wdenk73a8b272003-06-05 19:27:42 +0000170
171static void PingStart(void);
172#endif
173
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500174#if defined(CONFIG_CMD_CDP)
wdenka3d991b2004-04-15 21:48:45 +0000175static void CDPStart(void);
176#endif
177
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500178#if defined(CONFIG_CMD_SNTP)
wdenkea287de2005-04-01 00:25:43 +0000179IPaddr_t NetNtpServerIP; /* NTP server IP address */
180int NetTimeOffset=0; /* offset time from UTC */
181#endif
182
wdenk68ceb292004-08-02 21:11:11 +0000183#ifdef CONFIG_NETCONSOLE
184void NcStart(void);
185int nc_input_packet(uchar *pkt, unsigned dest, unsigned src, unsigned len);
186#endif
187
wdenk2d966952002-10-31 22:12:35 +0000188volatile uchar PktBuf[(PKTBUFSRX+1) * PKTSIZE_ALIGN + PKTALIGN];
189
190volatile uchar *NetRxPackets[PKTBUFSRX]; /* Receive packets */
191
192static rxhand_f *packetHandler; /* Current RX packet handler */
193static thand_f *timeHandler; /* Current timeout handler */
wdenke0ac62d2003-08-17 18:55:18 +0000194static ulong timeStart; /* Time base value */
195static ulong timeDelta; /* Current timeout value */
wdenk2d966952002-10-31 22:12:35 +0000196volatile uchar *NetTxPacket = 0; /* THE transmit packet */
197
198static int net_check_prereq (proto_t protocol);
199
200/**********************************************************************/
wdenk73a8b272003-06-05 19:27:42 +0000201
202IPaddr_t NetArpWaitPacketIP;
203IPaddr_t NetArpWaitReplyIP;
204uchar *NetArpWaitPacketMAC; /* MAC address of waiting packet's destination */
Wolfgang Denkb2f50802005-08-12 23:43:12 +0200205uchar *NetArpWaitTxPacket; /* THE transmit packet */
wdenk73a8b272003-06-05 19:27:42 +0000206int NetArpWaitTxPacketSize;
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200207uchar NetArpWaitPacketBuf[PKTSIZE_ALIGN + PKTALIGN];
wdenk73a8b272003-06-05 19:27:42 +0000208ulong NetArpWaitTimerStart;
209int NetArpWaitTry;
210
wdenk6e592382004-04-18 17:39:38 +0000211void ArpRequest (void)
wdenk73a8b272003-06-05 19:27:42 +0000212{
213 int i;
214 volatile uchar *pkt;
wdenk6e592382004-04-18 17:39:38 +0000215 ARP_t *arp;
wdenk73a8b272003-06-05 19:27:42 +0000216
Robin Getz0ebf04c2009-07-23 03:01:03 -0400217 debug("ARP broadcast %d\n", NetArpWaitTry);
218
wdenk73a8b272003-06-05 19:27:42 +0000219 pkt = NetTxPacket;
220
wdenk6e592382004-04-18 17:39:38 +0000221 pkt += NetSetEther (pkt, NetBcastAddr, PROT_ARP);
wdenk73a8b272003-06-05 19:27:42 +0000222
wdenk6e592382004-04-18 17:39:38 +0000223 arp = (ARP_t *) pkt;
wdenk73a8b272003-06-05 19:27:42 +0000224
wdenk6e592382004-04-18 17:39:38 +0000225 arp->ar_hrd = htons (ARP_ETHER);
226 arp->ar_pro = htons (PROT_IP);
wdenk73a8b272003-06-05 19:27:42 +0000227 arp->ar_hln = 6;
228 arp->ar_pln = 4;
wdenk6e592382004-04-18 17:39:38 +0000229 arp->ar_op = htons (ARPOP_REQUEST);
wdenk73a8b272003-06-05 19:27:42 +0000230
Wolfgang Denkb2f50802005-08-12 23:43:12 +0200231 memcpy (&arp->ar_data[0], NetOurEther, 6); /* source ET addr */
232 NetWriteIP ((uchar *) & arp->ar_data[6], NetOurIP); /* source IP addr */
wdenk6e592382004-04-18 17:39:38 +0000233 for (i = 10; i < 16; ++i) {
234 arp->ar_data[i] = 0; /* dest ET addr = 0 */
wdenk73a8b272003-06-05 19:27:42 +0000235 }
236
wdenk6e592382004-04-18 17:39:38 +0000237 if ((NetArpWaitPacketIP & NetOurSubnetMask) !=
238 (NetOurIP & NetOurSubnetMask)) {
239 if (NetOurGatewayIP == 0) {
240 puts ("## Warning: gatewayip needed but not set\n");
Wolfgang Denkd509b812006-03-12 01:13:30 +0100241 NetArpWaitReplyIP = NetArpWaitPacketIP;
242 } else {
243 NetArpWaitReplyIP = NetOurGatewayIP;
wdenk6e592382004-04-18 17:39:38 +0000244 }
wdenk6e592382004-04-18 17:39:38 +0000245 } else {
246 NetArpWaitReplyIP = NetArpWaitPacketIP;
247 }
wdenk73a8b272003-06-05 19:27:42 +0000248
wdenk6e592382004-04-18 17:39:38 +0000249 NetWriteIP ((uchar *) & arp->ar_data[16], NetArpWaitReplyIP);
250 (void) eth_send (NetTxPacket, (pkt - NetTxPacket) + ARP_HDR_SIZE);
wdenk73a8b272003-06-05 19:27:42 +0000251}
252
253void ArpTimeoutCheck(void)
254{
255 ulong t;
256
257 if (!NetArpWaitPacketIP)
258 return;
259
260 t = get_timer(0);
261
262 /* check for arp timeout */
Bartlomiej Sieka49f3bdb2008-10-01 15:26:28 +0200263 if ((t - NetArpWaitTimerStart) > ARP_TIMEOUT) {
wdenk73a8b272003-06-05 19:27:42 +0000264 NetArpWaitTry++;
265
266 if (NetArpWaitTry >= ARP_TIMEOUT_COUNT) {
267 puts ("\nARP Retry count exceeded; starting again\n");
268 NetArpWaitTry = 0;
269 NetStartAgain();
270 } else {
271 NetArpWaitTimerStart = t;
272 ArpRequest();
273 }
274 }
275}
276
Heiko Schocherda954272009-04-28 08:36:11 +0200277static void
Heiko Schocher2f70c492009-02-10 09:38:52 +0100278NetInitLoop(proto_t protocol)
279{
Heiko Schocherda954272009-04-28 08:36:11 +0200280 static int env_changed_id = 0;
Heiko Schocher2f70c492009-02-10 09:38:52 +0100281 bd_t *bd = gd->bd;
282 int env_id = get_env_id ();
283
284 /* update only when the environment has changed */
Michael Zaidman3c172c42009-04-04 01:43:00 +0300285 if (env_changed_id != env_id) {
Heiko Schocher2f70c492009-02-10 09:38:52 +0100286 NetCopyIP(&NetOurIP, &bd->bi_ip_addr);
287 NetOurGatewayIP = getenv_IPaddr ("gatewayip");
288 NetOurSubnetMask= getenv_IPaddr ("netmask");
Heiko Schocher2f70c492009-02-10 09:38:52 +0100289 NetServerIP = getenv_IPaddr ("serverip");
Heiko Schocher2f70c492009-02-10 09:38:52 +0100290 NetOurNativeVLAN = getenv_VLAN("nvlan");
Michael Zaidman3c172c42009-04-04 01:43:00 +0300291 NetOurVLAN = getenv_VLAN("vlan");
Robin Getz1a32bf42009-07-20 14:53:54 -0400292#if defined(CONFIG_CMD_DNS)
293 NetOurDNSIP = getenv_IPaddr("dnsip");
294#endif
Michael Zaidman3c172c42009-04-04 01:43:00 +0300295 env_changed_id = env_id;
Heiko Schocher2f70c492009-02-10 09:38:52 +0100296 }
Michael Zaidman3c172c42009-04-04 01:43:00 +0300297
Heiko Schocherda954272009-04-28 08:36:11 +0200298 return;
Heiko Schocher2f70c492009-02-10 09:38:52 +0100299}
300
wdenk73a8b272003-06-05 19:27:42 +0000301/**********************************************************************/
wdenk2d966952002-10-31 22:12:35 +0000302/*
303 * Main network processing loop.
304 */
305
306int
307NetLoop(proto_t protocol)
308{
wdenk2d966952002-10-31 22:12:35 +0000309 bd_t *bd = gd->bd;
310
311#ifdef CONFIG_NET_MULTI
312 NetRestarted = 0;
313 NetDevExists = 0;
314#endif
315
wdenk73a8b272003-06-05 19:27:42 +0000316 /* XXX problem with bss workaround */
317 NetArpWaitPacketMAC = NULL;
318 NetArpWaitTxPacket = NULL;
319 NetArpWaitPacketIP = 0;
320 NetArpWaitReplyIP = 0;
321 NetArpWaitTxPacket = NULL;
322 NetTxPacket = NULL;
323
wdenk2d966952002-10-31 22:12:35 +0000324 if (!NetTxPacket) {
325 int i;
wdenk2d966952002-10-31 22:12:35 +0000326 /*
327 * Setup packet buffers, aligned correctly.
328 */
329 NetTxPacket = &PktBuf[0] + (PKTALIGN - 1);
330 NetTxPacket -= (ulong)NetTxPacket % PKTALIGN;
331 for (i = 0; i < PKTBUFSRX; i++) {
332 NetRxPackets[i] = NetTxPacket + (i+1)*PKTSIZE_ALIGN;
333 }
wdenk73a8b272003-06-05 19:27:42 +0000334 }
335
336 if (!NetArpWaitTxPacket) {
337 NetArpWaitTxPacket = &NetArpWaitPacketBuf[0] + (PKTALIGN - 1);
338 NetArpWaitTxPacket -= (ulong)NetArpWaitTxPacket % PKTALIGN;
339 NetArpWaitTxPacketSize = 0;
wdenk2d966952002-10-31 22:12:35 +0000340 }
341
342 eth_halt();
wdenk6e592382004-04-18 17:39:38 +0000343#ifdef CONFIG_NET_MULTI
wdenka3d991b2004-04-15 21:48:45 +0000344 eth_set_current();
wdenk6e592382004-04-18 17:39:38 +0000345#endif
wdenkb1bf6f22005-04-03 14:52:59 +0000346 if (eth_init(bd) < 0) {
347 eth_halt();
wdenk6e592382004-04-18 17:39:38 +0000348 return(-1);
wdenkb1bf6f22005-04-03 14:52:59 +0000349 }
wdenk2d966952002-10-31 22:12:35 +0000350
351restart:
352#ifdef CONFIG_NET_MULTI
353 memcpy (NetOurEther, eth_get_dev()->enetaddr, 6);
354#else
Mike Frysinger95823ca2009-02-11 18:23:48 -0500355 eth_getenv_enetaddr("ethaddr", NetOurEther);
wdenk2d966952002-10-31 22:12:35 +0000356#endif
357
358 NetState = NETLOOP_CONTINUE;
359
360 /*
361 * Start the ball rolling with the given start function. From
362 * here on, this code is a state machine driven by received
363 * packets and timer events.
364 */
Heiko Schocher2f70c492009-02-10 09:38:52 +0100365 NetInitLoop(protocol);
wdenk2d966952002-10-31 22:12:35 +0000366
367 switch (net_check_prereq (protocol)) {
368 case 1:
369 /* network not configured */
wdenkb1bf6f22005-04-03 14:52:59 +0000370 eth_halt();
wdenk1d0350e2002-11-11 21:14:20 +0000371 return (-1);
wdenk2d966952002-10-31 22:12:35 +0000372
373#ifdef CONFIG_NET_MULTI
374 case 2:
375 /* network device not configured */
376 break;
377#endif /* CONFIG_NET_MULTI */
378
379 case 0:
380#ifdef CONFIG_NET_MULTI
381 NetDevExists = 1;
382#endif
383 switch (protocol) {
384 case TFTP:
385 /* always use ARP to get server ethernet address */
wdenk73a8b272003-06-05 19:27:42 +0000386 TftpStart();
wdenk2d966952002-10-31 22:12:35 +0000387 break;
388
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500389#if defined(CONFIG_CMD_DHCP)
wdenk2d966952002-10-31 22:12:35 +0000390 case DHCP:
wdenkd407bf52004-10-11 22:51:13 +0000391 BootpTry = 0;
Michael Zaidman09133f82009-07-14 23:37:12 +0300392 NetOurIP = 0;
wdenk2d966952002-10-31 22:12:35 +0000393 DhcpRequest(); /* Basically same as BOOTP */
394 break;
Jon Loeliger610f2e92007-07-10 11:05:02 -0500395#endif
wdenk2d966952002-10-31 22:12:35 +0000396
397 case BOOTP:
398 BootpTry = 0;
Michael Zaidman09133f82009-07-14 23:37:12 +0300399 NetOurIP = 0;
wdenk2d966952002-10-31 22:12:35 +0000400 BootpRequest ();
401 break;
402
403 case RARP:
404 RarpTry = 0;
Michael Zaidman09133f82009-07-14 23:37:12 +0300405 NetOurIP = 0;
wdenk2d966952002-10-31 22:12:35 +0000406 RarpRequest ();
407 break;
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500408#if defined(CONFIG_CMD_PING)
wdenk73a8b272003-06-05 19:27:42 +0000409 case PING:
410 PingStart();
411 break;
412#endif
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500413#if defined(CONFIG_CMD_NFS)
wdenkcbd8a352004-02-24 02:00:03 +0000414 case NFS:
415 NfsStart();
416 break;
417#endif
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500418#if defined(CONFIG_CMD_CDP)
wdenka3d991b2004-04-15 21:48:45 +0000419 case CDP:
420 CDPStart();
421 break;
422#endif
wdenk68ceb292004-08-02 21:11:11 +0000423#ifdef CONFIG_NETCONSOLE
424 case NETCONS:
425 NcStart();
426 break;
427#endif
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500428#if defined(CONFIG_CMD_SNTP)
wdenkea287de2005-04-01 00:25:43 +0000429 case SNTP:
430 SntpStart();
431 break;
432#endif
Robin Getz1a32bf42009-07-20 14:53:54 -0400433#if defined(CONFIG_CMD_DNS)
434 case DNS:
435 DnsStart();
436 break;
437#endif
wdenk2d966952002-10-31 22:12:35 +0000438 default:
439 break;
440 }
441
442 NetBootFileXferSize = 0;
443 break;
444 }
445
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500446#if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200447#if defined(CONFIG_SYS_FAULT_ECHO_LINK_DOWN) && defined(CONFIG_STATUS_LED) && defined(STATUS_LED_RED)
wdenkfc3e2162003-10-08 22:33:00 +0000448 /*
wdenk42d1f032003-10-15 23:53:47 +0000449 * Echo the inverted link state to the fault LED.
wdenkfc3e2162003-10-08 22:33:00 +0000450 */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200451 if(miiphy_link(eth_get_dev()->name, CONFIG_SYS_FAULT_MII_ADDR)) {
wdenkfc3e2162003-10-08 22:33:00 +0000452 status_led_set (STATUS_LED_RED, STATUS_LED_OFF);
453 } else {
454 status_led_set (STATUS_LED_RED, STATUS_LED_ON);
455 }
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200456#endif /* CONFIG_SYS_FAULT_ECHO_LINK_DOWN, ... */
wdenkfc3e2162003-10-08 22:33:00 +0000457#endif /* CONFIG_MII, ... */
wdenk2d966952002-10-31 22:12:35 +0000458
459 /*
460 * Main packet reception loop. Loop receiving packets until
wdenk59acc292005-04-03 14:18:51 +0000461 * someone sets `NetState' to a state that terminates.
wdenk2d966952002-10-31 22:12:35 +0000462 */
463 for (;;) {
464 WATCHDOG_RESET();
465#ifdef CONFIG_SHOW_ACTIVITY
466 {
467 extern void show_activity(int arg);
468 show_activity(1);
469 }
470#endif
471 /*
472 * Check the ethernet for a new packet. The ethernet
473 * receive routine will process it.
474 */
Guennadi Liakhovetski40cb90e2008-04-03 17:04:19 +0200475 eth_rx();
wdenk2d966952002-10-31 22:12:35 +0000476
477 /*
478 * Abort if ctrl-c was pressed.
479 */
480 if (ctrlc()) {
wdenk8bde7f72003-06-27 21:31:46 +0000481 eth_halt();
wdenk4b9206e2004-03-23 22:14:11 +0000482 puts ("\nAbort\n");
wdenk1d0350e2002-11-11 21:14:20 +0000483 return (-1);
wdenk2d966952002-10-31 22:12:35 +0000484 }
485
wdenk73a8b272003-06-05 19:27:42 +0000486 ArpTimeoutCheck();
wdenk2d966952002-10-31 22:12:35 +0000487
488 /*
489 * Check for a timeout, and run the timeout handler
490 * if we have one.
491 */
wdenke0ac62d2003-08-17 18:55:18 +0000492 if (timeHandler && ((get_timer(0) - timeStart) > timeDelta)) {
wdenk2d966952002-10-31 22:12:35 +0000493 thand_f *x;
494
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500495#if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200496# if defined(CONFIG_SYS_FAULT_ECHO_LINK_DOWN) && \
wdenk63153492005-04-03 20:55:38 +0000497 defined(CONFIG_STATUS_LED) && \
wdenk59acc292005-04-03 14:18:51 +0000498 defined(STATUS_LED_RED)
wdenkfc3e2162003-10-08 22:33:00 +0000499 /*
wdenk42d1f032003-10-15 23:53:47 +0000500 * Echo the inverted link state to the fault LED.
wdenkfc3e2162003-10-08 22:33:00 +0000501 */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200502 if(miiphy_link(eth_get_dev()->name, CONFIG_SYS_FAULT_MII_ADDR)) {
wdenkfc3e2162003-10-08 22:33:00 +0000503 status_led_set (STATUS_LED_RED, STATUS_LED_OFF);
504 } else {
505 status_led_set (STATUS_LED_RED, STATUS_LED_ON);
506 }
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200507# endif /* CONFIG_SYS_FAULT_ECHO_LINK_DOWN, ... */
wdenkfc3e2162003-10-08 22:33:00 +0000508#endif /* CONFIG_MII, ... */
wdenk2d966952002-10-31 22:12:35 +0000509 x = timeHandler;
510 timeHandler = (thand_f *)0;
511 (*x)();
512 }
513
514
515 switch (NetState) {
516
517 case NETLOOP_RESTART:
518#ifdef CONFIG_NET_MULTI
519 NetRestarted = 1;
520#endif
521 goto restart;
522
523 case NETLOOP_SUCCESS:
524 if (NetBootFileXferSize > 0) {
Wolfgang Denkf34024d2007-09-12 00:48:57 +0200525 char buf[20];
wdenk2d966952002-10-31 22:12:35 +0000526 printf("Bytes transferred = %ld (%lx hex)\n",
527 NetBootFileXferSize,
528 NetBootFileXferSize);
Wolfgang Denkf34024d2007-09-12 00:48:57 +0200529 sprintf(buf, "%lX", NetBootFileXferSize);
wdenk2d966952002-10-31 22:12:35 +0000530 setenv("filesize", buf);
wdenka3d991b2004-04-15 21:48:45 +0000531
532 sprintf(buf, "%lX", (unsigned long)load_addr);
533 setenv("fileaddr", buf);
wdenk2d966952002-10-31 22:12:35 +0000534 }
535 eth_halt();
536 return NetBootFileXferSize;
537
538 case NETLOOP_FAIL:
wdenk1d0350e2002-11-11 21:14:20 +0000539 return (-1);
wdenk2d966952002-10-31 22:12:35 +0000540 }
541 }
542}
543
544/**********************************************************************/
545
546static void
547startAgainTimeout(void)
548{
549 NetState = NETLOOP_RESTART;
550}
551
552static void
553startAgainHandler(uchar * pkt, unsigned dest, unsigned src, unsigned len)
554{
555 /* Totally ignore the packet */
556}
557
wdenk6e592382004-04-18 17:39:38 +0000558void NetStartAgain (void)
wdenk2d966952002-10-31 22:12:35 +0000559{
wdenk6e592382004-04-18 17:39:38 +0000560 char *nretry;
561 int noretry = 0, once = 0;
wdenka3d991b2004-04-15 21:48:45 +0000562
wdenk6e592382004-04-18 17:39:38 +0000563 if ((nretry = getenv ("netretry")) != NULL) {
564 noretry = (strcmp (nretry, "no") == 0);
565 once = (strcmp (nretry, "once") == 0);
566 }
567 if (noretry) {
568 eth_halt ();
wdenka3d991b2004-04-15 21:48:45 +0000569 NetState = NETLOOP_FAIL;
570 return;
571 }
wdenk2d966952002-10-31 22:12:35 +0000572#ifndef CONFIG_NET_MULTI
Bartlomiej Sieka49f3bdb2008-10-01 15:26:28 +0200573 NetSetTimeout (10000UL, startAgainTimeout);
wdenk6e592382004-04-18 17:39:38 +0000574 NetSetHandler (startAgainHandler);
575#else /* !CONFIG_NET_MULTI*/
576 eth_halt ();
Matthias Fuchs8b0c5c12007-12-27 16:58:41 +0100577#if !defined(CONFIG_NET_DO_NOT_TRY_ANOTHER)
wdenk6e592382004-04-18 17:39:38 +0000578 eth_try_another (!NetRestarted);
Matthias Fuchs8b0c5c12007-12-27 16:58:41 +0100579#endif
wdenk6e592382004-04-18 17:39:38 +0000580 eth_init (gd->bd);
581 if (NetRestartWrap) {
wdenk2d966952002-10-31 22:12:35 +0000582 NetRestartWrap = 0;
wdenk6e592382004-04-18 17:39:38 +0000583 if (NetDevExists && !once) {
Bartlomiej Sieka49f3bdb2008-10-01 15:26:28 +0200584 NetSetTimeout (10000UL, startAgainTimeout);
wdenk6e592382004-04-18 17:39:38 +0000585 NetSetHandler (startAgainHandler);
586 } else {
wdenk2d966952002-10-31 22:12:35 +0000587 NetState = NETLOOP_FAIL;
588 }
wdenk6e592382004-04-18 17:39:38 +0000589 } else {
wdenk2d966952002-10-31 22:12:35 +0000590 NetState = NETLOOP_RESTART;
591 }
wdenk6e592382004-04-18 17:39:38 +0000592#endif /* CONFIG_NET_MULTI */
wdenk2d966952002-10-31 22:12:35 +0000593}
594
595/**********************************************************************/
596/*
597 * Miscelaneous bits.
598 */
599
600void
601NetSetHandler(rxhand_f * f)
602{
603 packetHandler = f;
604}
605
606
607void
wdenk3e01d752004-10-09 21:56:21 +0000608NetSetTimeout(ulong iv, thand_f * f)
wdenk2d966952002-10-31 22:12:35 +0000609{
610 if (iv == 0) {
611 timeHandler = (thand_f *)0;
612 } else {
613 timeHandler = f;
wdenke0ac62d2003-08-17 18:55:18 +0000614 timeStart = get_timer(0);
615 timeDelta = iv;
wdenk2d966952002-10-31 22:12:35 +0000616 }
617}
618
619
620void
621NetSendPacket(volatile uchar * pkt, int len)
622{
623 (void) eth_send(pkt, len);
624}
625
wdenk73a8b272003-06-05 19:27:42 +0000626int
627NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport, int sport, int len)
628{
wdenka3d991b2004-04-15 21:48:45 +0000629 uchar *pkt;
630
wdenk73a8b272003-06-05 19:27:42 +0000631 /* convert to new style broadcast */
632 if (dest == 0)
633 dest = 0xFFFFFFFF;
wdenk2d966952002-10-31 22:12:35 +0000634
wdenk73a8b272003-06-05 19:27:42 +0000635 /* if broadcast, make the ether address a broadcast and don't do ARP */
636 if (dest == 0xFFFFFFFF)
637 ether = NetBcastAddr;
638
639 /* if MAC address was not discovered yet, save the packet and do an ARP request */
640 if (memcmp(ether, NetEtherNullAddr, 6) == 0) {
641
Robin Getz0ebf04c2009-07-23 03:01:03 -0400642 debug("sending ARP for %08lx\n", dest);
643
wdenk73a8b272003-06-05 19:27:42 +0000644 NetArpWaitPacketIP = dest;
645 NetArpWaitPacketMAC = ether;
wdenka3d991b2004-04-15 21:48:45 +0000646
647 pkt = NetArpWaitTxPacket;
648 pkt += NetSetEther (pkt, NetArpWaitPacketMAC, PROT_IP);
649
650 NetSetIP (pkt, dest, dport, sport, len);
651 memcpy(pkt + IP_HDR_SIZE, (uchar *)NetTxPacket + (pkt - (uchar *)NetArpWaitTxPacket) + IP_HDR_SIZE, len);
wdenk73a8b272003-06-05 19:27:42 +0000652
653 /* size of the waiting packet */
wdenka3d991b2004-04-15 21:48:45 +0000654 NetArpWaitTxPacketSize = (pkt - NetArpWaitTxPacket) + IP_HDR_SIZE + len;
wdenk73a8b272003-06-05 19:27:42 +0000655
656 /* and do the ARP request */
657 NetArpWaitTry = 1;
658 NetArpWaitTimerStart = get_timer(0);
659 ArpRequest();
660 return 1; /* waiting */
661 }
662
Robin Getz0ebf04c2009-07-23 03:01:03 -0400663 debug("sending UDP to %08lx/%pM\n", dest, ether);
wdenk73a8b272003-06-05 19:27:42 +0000664
wdenka3d991b2004-04-15 21:48:45 +0000665 pkt = (uchar *)NetTxPacket;
666 pkt += NetSetEther (pkt, ether, PROT_IP);
667 NetSetIP (pkt, dest, dport, sport, len);
668 (void) eth_send(NetTxPacket, (pkt - NetTxPacket) + IP_HDR_SIZE + len);
wdenk73a8b272003-06-05 19:27:42 +0000669
wdenk6e592382004-04-18 17:39:38 +0000670 return 0; /* transmitted */
wdenk73a8b272003-06-05 19:27:42 +0000671}
672
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500673#if defined(CONFIG_CMD_PING)
wdenk73a8b272003-06-05 19:27:42 +0000674static ushort PingSeqNo;
675
676int PingSend(void)
677{
678 static uchar mac[6];
679 volatile IP_t *ip;
680 volatile ushort *s;
wdenka3d991b2004-04-15 21:48:45 +0000681 uchar *pkt;
wdenk73a8b272003-06-05 19:27:42 +0000682
683 /* XXX always send arp request */
684
685 memcpy(mac, NetEtherNullAddr, 6);
686
Robin Getz0ebf04c2009-07-23 03:01:03 -0400687 debug("sending ARP for %08lx\n", NetPingIP);
wdenk73a8b272003-06-05 19:27:42 +0000688
689 NetArpWaitPacketIP = NetPingIP;
690 NetArpWaitPacketMAC = mac;
691
wdenka3d991b2004-04-15 21:48:45 +0000692 pkt = NetArpWaitTxPacket;
693 pkt += NetSetEther(pkt, mac, PROT_IP);
wdenk73a8b272003-06-05 19:27:42 +0000694
wdenka3d991b2004-04-15 21:48:45 +0000695 ip = (volatile IP_t *)pkt;
wdenk73a8b272003-06-05 19:27:42 +0000696
697 /*
698 * Construct an IP and ICMP header. (need to set no fragment bit - XXX)
699 */
700 ip->ip_hl_v = 0x45; /* IP_HDR_SIZE / 4 (not including UDP) */
701 ip->ip_tos = 0;
702 ip->ip_len = htons(IP_HDR_SIZE_NO_UDP + 8);
703 ip->ip_id = htons(NetIPID++);
Peter Tysere0c07b82008-12-01 16:26:20 -0600704 ip->ip_off = htons(IP_FLAGS_DFRAG); /* Don't fragment */
wdenk73a8b272003-06-05 19:27:42 +0000705 ip->ip_ttl = 255;
706 ip->ip_p = 0x01; /* ICMP */
707 ip->ip_sum = 0;
708 NetCopyIP((void*)&ip->ip_src, &NetOurIP); /* already in network byte order */
709 NetCopyIP((void*)&ip->ip_dst, &NetPingIP); /* - "" - */
710 ip->ip_sum = ~NetCksum((uchar *)ip, IP_HDR_SIZE_NO_UDP / 2);
711
712 s = &ip->udp_src; /* XXX ICMP starts here */
713 s[0] = htons(0x0800); /* echo-request, code */
714 s[1] = 0; /* checksum */
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200715 s[2] = 0; /* identifier */
wdenk73a8b272003-06-05 19:27:42 +0000716 s[3] = htons(PingSeqNo++); /* sequence number */
717 s[1] = ~NetCksum((uchar *)s, 8/2);
718
719 /* size of the waiting packet */
wdenka3d991b2004-04-15 21:48:45 +0000720 NetArpWaitTxPacketSize = (pkt - NetArpWaitTxPacket) + IP_HDR_SIZE_NO_UDP + 8;
wdenk73a8b272003-06-05 19:27:42 +0000721
722 /* and do the ARP request */
723 NetArpWaitTry = 1;
724 NetArpWaitTimerStart = get_timer(0);
725 ArpRequest();
726 return 1; /* waiting */
727}
728
729static void
730PingTimeout (void)
731{
732 eth_halt();
733 NetState = NETLOOP_FAIL; /* we did not get the reply */
734}
735
736static void
737PingHandler (uchar * pkt, unsigned dest, unsigned src, unsigned len)
738{
739 IPaddr_t tmp;
740 volatile IP_t *ip = (volatile IP_t *)pkt;
741
742 tmp = NetReadIP((void *)&ip->ip_src);
743 if (tmp != NetPingIP)
744 return;
745
746 NetState = NETLOOP_SUCCESS;
747}
748
749static void PingStart(void)
750{
wdenka3d991b2004-04-15 21:48:45 +0000751#if defined(CONFIG_NET_MULTI)
752 printf ("Using %s device\n", eth_get_name());
wdenk6e592382004-04-18 17:39:38 +0000753#endif /* CONFIG_NET_MULTI */
Bartlomiej Sieka49f3bdb2008-10-01 15:26:28 +0200754 NetSetTimeout (10000UL, PingTimeout);
wdenk73a8b272003-06-05 19:27:42 +0000755 NetSetHandler (PingHandler);
756
757 PingSend();
758}
Jon Loeliger610f2e92007-07-10 11:05:02 -0500759#endif
wdenk2d966952002-10-31 22:12:35 +0000760
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500761#if defined(CONFIG_CMD_CDP)
wdenka3d991b2004-04-15 21:48:45 +0000762
763#define CDP_DEVICE_ID_TLV 0x0001
764#define CDP_ADDRESS_TLV 0x0002
765#define CDP_PORT_ID_TLV 0x0003
766#define CDP_CAPABILITIES_TLV 0x0004
767#define CDP_VERSION_TLV 0x0005
768#define CDP_PLATFORM_TLV 0x0006
769#define CDP_NATIVE_VLAN_TLV 0x000a
770#define CDP_APPLIANCE_VLAN_TLV 0x000e
771#define CDP_TRIGGER_TLV 0x000f
772#define CDP_POWER_CONSUMPTION_TLV 0x0010
773#define CDP_SYSNAME_TLV 0x0014
774#define CDP_SYSOBJECT_TLV 0x0015
775#define CDP_MANAGEMENT_ADDRESS_TLV 0x0016
776
Bartlomiej Sieka49f3bdb2008-10-01 15:26:28 +0200777#define CDP_TIMEOUT 250UL /* one packet every 250ms */
wdenka3d991b2004-04-15 21:48:45 +0000778
779static int CDPSeq;
780static int CDPOK;
781
782ushort CDPNativeVLAN;
783ushort CDPApplianceVLAN;
784
785static const uchar CDP_SNAP_hdr[8] = { 0xAA, 0xAA, 0x03, 0x00, 0x00, 0x0C, 0x20, 0x00 };
786
787static ushort CDP_compute_csum(const uchar *buff, ushort len)
788{
789 ushort csum;
790 int odd;
791 ulong result = 0;
792 ushort leftover;
Wolfgang Denk77ddac92005-10-13 16:45:02 +0200793 ushort *p;
wdenka3d991b2004-04-15 21:48:45 +0000794
795 if (len > 0) {
796 odd = 1 & (ulong)buff;
797 if (odd) {
798 result = *buff << 8;
799 len--;
800 buff++;
801 }
802 while (len > 1) {
Wolfgang Denk77ddac92005-10-13 16:45:02 +0200803 p = (ushort *)buff;
804 result += *p++;
805 buff = (uchar *)p;
wdenka3d991b2004-04-15 21:48:45 +0000806 if (result & 0x80000000)
807 result = (result & 0xFFFF) + (result >> 16);
808 len -= 2;
809 }
810 if (len) {
811 leftover = (signed short)(*(const signed char *)buff);
Wolfgang Denk3ada8342005-11-10 20:59:46 +0100812 /* CISCO SUCKS big time! (and blows too):
813 * CDP uses the IP checksum algorithm with a twist;
814 * for the last byte it *sign* extends and sums.
815 */
wdenka3d991b2004-04-15 21:48:45 +0000816 result = (result & 0xffff0000) | ((result + leftover) & 0x0000ffff);
817 }
818 while (result >> 16)
819 result = (result & 0xFFFF) + (result >> 16);
820
821 if (odd)
822 result = ((result >> 8) & 0xff) | ((result & 0xff) << 8);
823 }
824
825 /* add up 16-bit and 17-bit words for 17+c bits */
826 result = (result & 0xffff) + (result >> 16);
827 /* add up 16-bit and 2-bit for 16+c bit */
828 result = (result & 0xffff) + (result >> 16);
829 /* add up carry.. */
830 result = (result & 0xffff) + (result >> 16);
831
832 /* negate */
833 csum = ~(ushort)result;
834
835 /* run time endian detection */
836 if (csum != htons(csum)) /* little endian */
837 csum = htons(csum);
838
839 return csum;
840}
841
842int CDPSendTrigger(void)
843{
844 volatile uchar *pkt;
845 volatile ushort *s;
846 volatile ushort *cp;
847 Ethernet_t *et;
wdenka3d991b2004-04-15 21:48:45 +0000848 int len;
849 ushort chksum;
wdenk6e592382004-04-18 17:39:38 +0000850#if defined(CONFIG_CDP_DEVICE_ID) || defined(CONFIG_CDP_PORT_ID) || \
851 defined(CONFIG_CDP_VERSION) || defined(CONFIG_CDP_PLATFORM)
852 char buf[32];
853#endif
wdenka3d991b2004-04-15 21:48:45 +0000854
855 pkt = NetTxPacket;
856 et = (Ethernet_t *)pkt;
857
858 /* NOTE: trigger sent not on any VLAN */
859
860 /* form ethernet header */
861 memcpy(et->et_dest, NetCDPAddr, 6);
862 memcpy(et->et_src, NetOurEther, 6);
863
864 pkt += ETHER_HDR_SIZE;
865
866 /* SNAP header */
867 memcpy((uchar *)pkt, CDP_SNAP_hdr, sizeof(CDP_SNAP_hdr));
868 pkt += sizeof(CDP_SNAP_hdr);
869
870 /* CDP header */
871 *pkt++ = 0x02; /* CDP version 2 */
872 *pkt++ = 180; /* TTL */
873 s = (volatile ushort *)pkt;
874 cp = s;
875 *s++ = htons(0); /* checksum (0 for later calculation) */
876
877 /* CDP fields */
878#ifdef CONFIG_CDP_DEVICE_ID
879 *s++ = htons(CDP_DEVICE_ID_TLV);
880 *s++ = htons(CONFIG_CDP_DEVICE_ID);
Mike Frysinger95823ca2009-02-11 18:23:48 -0500881 sprintf(buf, CONFIG_CDP_DEVICE_ID_PREFIX "%pm", NetOurEther);
wdenka3d991b2004-04-15 21:48:45 +0000882 memcpy((uchar *)s, buf, 16);
883 s += 16 / 2;
884#endif
885
886#ifdef CONFIG_CDP_PORT_ID
887 *s++ = htons(CDP_PORT_ID_TLV);
888 memset(buf, 0, sizeof(buf));
889 sprintf(buf, CONFIG_CDP_PORT_ID, eth_get_dev_index());
890 len = strlen(buf);
891 if (len & 1) /* make it even */
892 len++;
893 *s++ = htons(len + 4);
894 memcpy((uchar *)s, buf, len);
895 s += len / 2;
896#endif
897
898#ifdef CONFIG_CDP_CAPABILITIES
899 *s++ = htons(CDP_CAPABILITIES_TLV);
900 *s++ = htons(8);
901 *(ulong *)s = htonl(CONFIG_CDP_CAPABILITIES);
902 s += 2;
903#endif
904
905#ifdef CONFIG_CDP_VERSION
906 *s++ = htons(CDP_VERSION_TLV);
907 memset(buf, 0, sizeof(buf));
908 strcpy(buf, CONFIG_CDP_VERSION);
909 len = strlen(buf);
910 if (len & 1) /* make it even */
911 len++;
912 *s++ = htons(len + 4);
913 memcpy((uchar *)s, buf, len);
914 s += len / 2;
915#endif
916
917#ifdef CONFIG_CDP_PLATFORM
918 *s++ = htons(CDP_PLATFORM_TLV);
919 memset(buf, 0, sizeof(buf));
920 strcpy(buf, CONFIG_CDP_PLATFORM);
921 len = strlen(buf);
922 if (len & 1) /* make it even */
923 len++;
924 *s++ = htons(len + 4);
925 memcpy((uchar *)s, buf, len);
926 s += len / 2;
927#endif
928
929#ifdef CONFIG_CDP_TRIGGER
930 *s++ = htons(CDP_TRIGGER_TLV);
931 *s++ = htons(8);
932 *(ulong *)s = htonl(CONFIG_CDP_TRIGGER);
933 s += 2;
934#endif
935
936#ifdef CONFIG_CDP_POWER_CONSUMPTION
937 *s++ = htons(CDP_POWER_CONSUMPTION_TLV);
938 *s++ = htons(6);
939 *s++ = htons(CONFIG_CDP_POWER_CONSUMPTION);
940#endif
941
942 /* length of ethernet packet */
943 len = (uchar *)s - ((uchar *)NetTxPacket + ETHER_HDR_SIZE);
944 et->et_protlen = htons(len);
945
946 len = ETHER_HDR_SIZE + sizeof(CDP_SNAP_hdr);
947 chksum = CDP_compute_csum((uchar *)NetTxPacket + len, (uchar *)s - (NetTxPacket + len));
948 if (chksum == 0)
949 chksum = 0xFFFF;
950 *cp = htons(chksum);
951
952 (void) eth_send(NetTxPacket, (uchar *)s - NetTxPacket);
953 return 0;
954}
955
956static void
957CDPTimeout (void)
958{
959 CDPSeq++;
960
961 if (CDPSeq < 3) {
962 NetSetTimeout (CDP_TIMEOUT, CDPTimeout);
963 CDPSendTrigger();
964 return;
965 }
966
967 /* if not OK try again */
968 if (!CDPOK)
969 NetStartAgain();
970 else
971 NetState = NETLOOP_SUCCESS;
972}
973
974static void
975CDPDummyHandler (uchar * pkt, unsigned dest, unsigned src, unsigned len)
976{
977 /* nothing */
978}
979
980static void
981CDPHandler(const uchar * pkt, unsigned len)
982{
983 const uchar *t;
984 const ushort *ss;
985 ushort type, tlen;
986 uchar applid;
987 ushort vlan, nvlan;
988
989 /* minimum size? */
990 if (len < sizeof(CDP_SNAP_hdr) + 4)
991 goto pkt_short;
992
993 /* check for valid CDP SNAP header */
994 if (memcmp(pkt, CDP_SNAP_hdr, sizeof(CDP_SNAP_hdr)) != 0)
995 return;
996
997 pkt += sizeof(CDP_SNAP_hdr);
998 len -= sizeof(CDP_SNAP_hdr);
999
1000 /* Version of CDP protocol must be >= 2 and TTL != 0 */
1001 if (pkt[0] < 0x02 || pkt[1] == 0)
1002 return;
1003
1004 /* if version is greater than 0x02 maybe we'll have a problem; output a warning */
1005 if (pkt[0] != 0x02)
1006 printf("** WARNING: CDP packet received with a protocol version %d > 2\n",
1007 pkt[0] & 0xff);
1008
1009 if (CDP_compute_csum(pkt, len) != 0)
1010 return;
1011
1012 pkt += 4;
1013 len -= 4;
1014
1015 vlan = htons(-1);
1016 nvlan = htons(-1);
1017 while (len > 0) {
1018 if (len < 4)
1019 goto pkt_short;
1020
1021 ss = (const ushort *)pkt;
1022 type = ntohs(ss[0]);
1023 tlen = ntohs(ss[1]);
1024 if (tlen > len) {
1025 goto pkt_short;
1026 }
1027
1028 pkt += tlen;
1029 len -= tlen;
1030
1031 ss += 2; /* point ss to the data of the TLV */
1032 tlen -= 4;
1033
1034 switch (type) {
1035 case CDP_DEVICE_ID_TLV:
1036 break;
1037 case CDP_ADDRESS_TLV:
1038 break;
1039 case CDP_PORT_ID_TLV:
1040 break;
1041 case CDP_CAPABILITIES_TLV:
1042 break;
1043 case CDP_VERSION_TLV:
1044 break;
1045 case CDP_PLATFORM_TLV:
1046 break;
1047 case CDP_NATIVE_VLAN_TLV:
1048 nvlan = *ss;
1049 break;
1050 case CDP_APPLIANCE_VLAN_TLV:
1051 t = (const uchar *)ss;
1052 while (tlen > 0) {
1053 if (tlen < 3)
1054 goto pkt_short;
1055
1056 applid = t[0];
1057 ss = (const ushort *)(t + 1);
1058
1059#ifdef CONFIG_CDP_APPLIANCE_VLAN_TYPE
1060 if (applid == CONFIG_CDP_APPLIANCE_VLAN_TYPE)
1061 vlan = *ss;
1062#else
1063 vlan = ntohs(*ss); /* XXX will this work; dunno */
1064#endif
1065 t += 3; tlen -= 3;
1066 }
1067 break;
1068 case CDP_TRIGGER_TLV:
1069 break;
1070 case CDP_POWER_CONSUMPTION_TLV:
1071 break;
1072 case CDP_SYSNAME_TLV:
1073 break;
1074 case CDP_SYSOBJECT_TLV:
1075 break;
1076 case CDP_MANAGEMENT_ADDRESS_TLV:
1077 break;
1078 }
1079 }
1080
1081 CDPApplianceVLAN = vlan;
1082 CDPNativeVLAN = nvlan;
1083
1084 CDPOK = 1;
1085 return;
1086
1087 pkt_short:
1088 printf("** CDP packet is too short\n");
1089 return;
1090}
1091
1092static void CDPStart(void)
1093{
1094#if defined(CONFIG_NET_MULTI)
1095 printf ("Using %s device\n", eth_get_name());
1096#endif
1097 CDPSeq = 0;
1098 CDPOK = 0;
1099
1100 CDPNativeVLAN = htons(-1);
1101 CDPApplianceVLAN = htons(-1);
1102
1103 NetSetTimeout (CDP_TIMEOUT, CDPTimeout);
1104 NetSetHandler (CDPDummyHandler);
1105
1106 CDPSendTrigger();
1107}
Jon Loeliger610f2e92007-07-10 11:05:02 -05001108#endif
wdenka3d991b2004-04-15 21:48:45 +00001109
1110
wdenk2d966952002-10-31 22:12:35 +00001111void
wdenka3d991b2004-04-15 21:48:45 +00001112NetReceive(volatile uchar * inpkt, int len)
wdenk2d966952002-10-31 22:12:35 +00001113{
1114 Ethernet_t *et;
1115 IP_t *ip;
1116 ARP_t *arp;
1117 IPaddr_t tmp;
1118 int x;
wdenka3d991b2004-04-15 21:48:45 +00001119 uchar *pkt;
Jon Loeliger643d1ab2007-07-09 17:45:14 -05001120#if defined(CONFIG_CMD_CDP)
wdenka3d991b2004-04-15 21:48:45 +00001121 int iscdp;
1122#endif
1123 ushort cti = 0, vlanid = VLAN_NONE, myvlanid, mynvlanid;
wdenk2d966952002-10-31 22:12:35 +00001124
Robin Getz0ebf04c2009-07-23 03:01:03 -04001125 debug("packet received\n");
wdenka3d991b2004-04-15 21:48:45 +00001126
Mike Frysingerd9bec9f2009-07-18 21:04:08 -04001127 NetRxPacket = inpkt;
1128 NetRxPacketLen = len;
wdenka3d991b2004-04-15 21:48:45 +00001129 et = (Ethernet_t *)inpkt;
1130
1131 /* too small packet? */
1132 if (len < ETHER_HDR_SIZE)
1133 return;
1134
Rafal Jaworowskif85b6072007-12-27 18:19:02 +01001135#ifdef CONFIG_API
1136 if (push_packet) {
1137 (*push_packet)(inpkt, len);
1138 return;
1139 }
1140#endif
1141
Jon Loeliger643d1ab2007-07-09 17:45:14 -05001142#if defined(CONFIG_CMD_CDP)
wdenka3d991b2004-04-15 21:48:45 +00001143 /* keep track if packet is CDP */
1144 iscdp = memcmp(et->et_dest, NetCDPAddr, 6) == 0;
1145#endif
1146
1147 myvlanid = ntohs(NetOurVLAN);
1148 if (myvlanid == (ushort)-1)
1149 myvlanid = VLAN_NONE;
1150 mynvlanid = ntohs(NetOurNativeVLAN);
1151 if (mynvlanid == (ushort)-1)
1152 mynvlanid = VLAN_NONE;
wdenk2d966952002-10-31 22:12:35 +00001153
1154 x = ntohs(et->et_protlen);
1155
Robin Getz0ebf04c2009-07-23 03:01:03 -04001156 debug("packet received\n");
wdenka3d991b2004-04-15 21:48:45 +00001157
wdenk2d966952002-10-31 22:12:35 +00001158 if (x < 1514) {
1159 /*
1160 * Got a 802 packet. Check the other protocol field.
1161 */
1162 x = ntohs(et->et_prot);
wdenka3d991b2004-04-15 21:48:45 +00001163
1164 ip = (IP_t *)(inpkt + E802_HDR_SIZE);
wdenk2d966952002-10-31 22:12:35 +00001165 len -= E802_HDR_SIZE;
wdenka3d991b2004-04-15 21:48:45 +00001166
1167 } else if (x != PROT_VLAN) { /* normal packet */
1168 ip = (IP_t *)(inpkt + ETHER_HDR_SIZE);
wdenk2d966952002-10-31 22:12:35 +00001169 len -= ETHER_HDR_SIZE;
wdenka3d991b2004-04-15 21:48:45 +00001170
1171 } else { /* VLAN packet */
1172 VLAN_Ethernet_t *vet = (VLAN_Ethernet_t *)et;
1173
Robin Getz0ebf04c2009-07-23 03:01:03 -04001174 debug("VLAN packet received\n");
1175
wdenka3d991b2004-04-15 21:48:45 +00001176 /* too small packet? */
1177 if (len < VLAN_ETHER_HDR_SIZE)
1178 return;
1179
1180 /* if no VLAN active */
1181 if ((ntohs(NetOurVLAN) & VLAN_IDMASK) == VLAN_NONE
Jon Loeliger643d1ab2007-07-09 17:45:14 -05001182#if defined(CONFIG_CMD_CDP)
wdenka3d991b2004-04-15 21:48:45 +00001183 && iscdp == 0
1184#endif
1185 )
1186 return;
1187
1188 cti = ntohs(vet->vet_tag);
1189 vlanid = cti & VLAN_IDMASK;
1190 x = ntohs(vet->vet_type);
1191
1192 ip = (IP_t *)(inpkt + VLAN_ETHER_HDR_SIZE);
1193 len -= VLAN_ETHER_HDR_SIZE;
wdenk2d966952002-10-31 22:12:35 +00001194 }
1195
Robin Getz0ebf04c2009-07-23 03:01:03 -04001196 debug("Receive from protocol 0x%x\n", x);
wdenk2d966952002-10-31 22:12:35 +00001197
Jon Loeliger643d1ab2007-07-09 17:45:14 -05001198#if defined(CONFIG_CMD_CDP)
wdenka3d991b2004-04-15 21:48:45 +00001199 if (iscdp) {
1200 CDPHandler((uchar *)ip, len);
1201 return;
1202 }
1203#endif
1204
1205 if ((myvlanid & VLAN_IDMASK) != VLAN_NONE) {
1206 if (vlanid == VLAN_NONE)
1207 vlanid = (mynvlanid & VLAN_IDMASK);
1208 /* not matched? */
1209 if (vlanid != (myvlanid & VLAN_IDMASK))
1210 return;
1211 }
1212
wdenk2d966952002-10-31 22:12:35 +00001213 switch (x) {
1214
1215 case PROT_ARP:
1216 /*
1217 * We have to deal with two types of ARP packets:
wdenk8bde7f72003-06-27 21:31:46 +00001218 * - REQUEST packets will be answered by sending our
1219 * IP address - if we know it.
1220 * - REPLY packates are expected only after we asked
1221 * for the TFTP server's or the gateway's ethernet
1222 * address; so if we receive such a packet, we set
1223 * the server ethernet address
wdenk2d966952002-10-31 22:12:35 +00001224 */
Robin Getz0ebf04c2009-07-23 03:01:03 -04001225 debug("Got ARP\n");
1226
wdenk2d966952002-10-31 22:12:35 +00001227 arp = (ARP_t *)ip;
1228 if (len < ARP_HDR_SIZE) {
1229 printf("bad length %d < %d\n", len, ARP_HDR_SIZE);
1230 return;
1231 }
1232 if (ntohs(arp->ar_hrd) != ARP_ETHER) {
1233 return;
1234 }
1235 if (ntohs(arp->ar_pro) != PROT_IP) {
1236 return;
1237 }
1238 if (arp->ar_hln != 6) {
1239 return;
1240 }
1241 if (arp->ar_pln != 4) {
1242 return;
1243 }
1244
1245 if (NetOurIP == 0) {
1246 return;
1247 }
1248
1249 if (NetReadIP(&arp->ar_data[16]) != NetOurIP) {
1250 return;
1251 }
1252
1253 switch (ntohs(arp->ar_op)) {
1254 case ARPOP_REQUEST: /* reply with our IP address */
Robin Getz0ebf04c2009-07-23 03:01:03 -04001255 debug("Got ARP REQUEST, return our IP\n");
wdenka3d991b2004-04-15 21:48:45 +00001256 pkt = (uchar *)et;
1257 pkt += NetSetEther(pkt, et->et_src, PROT_ARP);
wdenk2d966952002-10-31 22:12:35 +00001258 arp->ar_op = htons(ARPOP_REPLY);
1259 memcpy (&arp->ar_data[10], &arp->ar_data[0], 6);
1260 NetCopyIP(&arp->ar_data[16], &arp->ar_data[6]);
1261 memcpy (&arp->ar_data[ 0], NetOurEther, 6);
1262 NetCopyIP(&arp->ar_data[ 6], &NetOurIP);
wdenka3d991b2004-04-15 21:48:45 +00001263 (void) eth_send((uchar *)et, (pkt - (uchar *)et) + ARP_HDR_SIZE);
wdenk2d966952002-10-31 22:12:35 +00001264 return;
wdenk73a8b272003-06-05 19:27:42 +00001265
1266 case ARPOP_REPLY: /* arp reply */
1267 /* are we waiting for a reply */
1268 if (!NetArpWaitPacketIP || !NetArpWaitPacketMAC)
1269 break;
Robin Getz97cfe862009-07-21 12:15:28 -04001270
1271#ifdef CONFIG_KEEP_SERVERADDR
1272 if (NetServerIP == NetArpWaitPacketIP) {
1273 char buf[20];
1274 sprintf(buf, "%pM", arp->ar_data);
1275 setenv("serveraddr", buf);
1276 }
1277#endif
1278
Robin Getz0ebf04c2009-07-23 03:01:03 -04001279 debug("Got ARP REPLY, set server/gtwy eth addr (%pM)\n",
Mike Frysinger95823ca2009-02-11 18:23:48 -05001280 arp->ar_data);
wdenk73a8b272003-06-05 19:27:42 +00001281
1282 tmp = NetReadIP(&arp->ar_data[6]);
1283
1284 /* matched waiting packet's address */
1285 if (tmp == NetArpWaitReplyIP) {
Robin Getz0ebf04c2009-07-23 03:01:03 -04001286 debug("Got it\n");
wdenk73a8b272003-06-05 19:27:42 +00001287 /* save address for later use */
1288 memcpy(NetArpWaitPacketMAC, &arp->ar_data[0], 6);
1289
wdenk68ceb292004-08-02 21:11:11 +00001290#ifdef CONFIG_NETCONSOLE
1291 (*packetHandler)(0,0,0,0);
1292#endif
wdenk73a8b272003-06-05 19:27:42 +00001293 /* modify header, and transmit it */
1294 memcpy(((Ethernet_t *)NetArpWaitTxPacket)->et_dest, NetArpWaitPacketMAC, 6);
1295 (void) eth_send(NetArpWaitTxPacket, NetArpWaitTxPacketSize);
1296
1297 /* no arp request pending now */
1298 NetArpWaitPacketIP = 0;
1299 NetArpWaitTxPacketSize = 0;
1300 NetArpWaitPacketMAC = NULL;
1301
1302 }
wdenk2d966952002-10-31 22:12:35 +00001303 return;
1304 default:
Robin Getz0ebf04c2009-07-23 03:01:03 -04001305 debug("Unexpected ARP opcode 0x%x\n", ntohs(arp->ar_op));
wdenk2d966952002-10-31 22:12:35 +00001306 return;
1307 }
wdenk289f9322005-01-12 00:15:14 +00001308 break;
wdenk2d966952002-10-31 22:12:35 +00001309
1310 case PROT_RARP:
Robin Getz0ebf04c2009-07-23 03:01:03 -04001311 debug("Got RARP\n");
wdenk2d966952002-10-31 22:12:35 +00001312 arp = (ARP_t *)ip;
1313 if (len < ARP_HDR_SIZE) {
1314 printf("bad length %d < %d\n", len, ARP_HDR_SIZE);
1315 return;
1316 }
1317
1318 if ((ntohs(arp->ar_op) != RARPOP_REPLY) ||
1319 (ntohs(arp->ar_hrd) != ARP_ETHER) ||
1320 (ntohs(arp->ar_pro) != PROT_IP) ||
1321 (arp->ar_hln != 6) || (arp->ar_pln != 4)) {
1322
wdenk4b9206e2004-03-23 22:14:11 +00001323 puts ("invalid RARP header\n");
wdenk2d966952002-10-31 22:12:35 +00001324 } else {
1325 NetCopyIP(&NetOurIP, &arp->ar_data[16]);
wdenk3d3befa2004-03-14 15:06:13 +00001326 if (NetServerIP == 0)
1327 NetCopyIP(&NetServerIP, &arp->ar_data[ 6]);
wdenk2d966952002-10-31 22:12:35 +00001328 memcpy (NetServerEther, &arp->ar_data[ 0], 6);
1329
1330 (*packetHandler)(0,0,0,0);
1331 }
1332 break;
1333
1334 case PROT_IP:
Robin Getz0ebf04c2009-07-23 03:01:03 -04001335 debug("Got IP\n");
wdenk2d966952002-10-31 22:12:35 +00001336 if (len < IP_HDR_SIZE) {
Robin Getz0ebf04c2009-07-23 03:01:03 -04001337 debug("len bad %d < %lu\n", len, (ulong)IP_HDR_SIZE);
wdenk2d966952002-10-31 22:12:35 +00001338 return;
1339 }
1340 if (len < ntohs(ip->ip_len)) {
1341 printf("len bad %d < %d\n", len, ntohs(ip->ip_len));
1342 return;
1343 }
1344 len = ntohs(ip->ip_len);
Robin Getz0ebf04c2009-07-23 03:01:03 -04001345 debug("len=%d, v=%02x\n", len, ip->ip_hl_v & 0xff);
1346
wdenk2d966952002-10-31 22:12:35 +00001347 if ((ip->ip_hl_v & 0xf0) != 0x40) {
1348 return;
1349 }
Peter Tyserd32c5be2008-12-01 16:26:21 -06001350 /* Can't deal with fragments */
1351 if (ip->ip_off & htons(IP_OFFS | IP_FLAGS_MFRAG)) {
wdenk2d966952002-10-31 22:12:35 +00001352 return;
1353 }
Remy Bohmer6b52cfe2008-06-03 15:48:17 +02001354 /* can't deal with headers > 20 bytes */
1355 if ((ip->ip_hl_v & 0x0f) > 0x05) {
1356 return;
1357 }
wdenk2d966952002-10-31 22:12:35 +00001358 if (!NetCksumOk((uchar *)ip, IP_HDR_SIZE_NO_UDP / 2)) {
wdenk4b9206e2004-03-23 22:14:11 +00001359 puts ("checksum bad\n");
wdenk2d966952002-10-31 22:12:35 +00001360 return;
1361 }
1362 tmp = NetReadIP(&ip->ip_dst);
1363 if (NetOurIP && tmp != NetOurIP && tmp != 0xFFFFFFFF) {
David Updegraff53a5c422007-06-11 10:41:07 -05001364#ifdef CONFIG_MCAST_TFTP
Wolfgang Denk85eb5ca2007-08-14 09:47:27 +02001365 if (Mcast_addr != tmp)
David Updegraff53a5c422007-06-11 10:41:07 -05001366#endif
wdenk2d966952002-10-31 22:12:35 +00001367 return;
1368 }
1369 /*
1370 * watch for ICMP host redirects
1371 *
wdenk8bde7f72003-06-27 21:31:46 +00001372 * There is no real handler code (yet). We just watch
1373 * for ICMP host redirect messages. In case anybody
1374 * sees these messages: please contact me
1375 * (wd@denx.de), or - even better - send me the
1376 * necessary fixes :-)
wdenk2d966952002-10-31 22:12:35 +00001377 *
wdenk8bde7f72003-06-27 21:31:46 +00001378 * Note: in all cases where I have seen this so far
1379 * it was a problem with the router configuration,
1380 * for instance when a router was configured in the
1381 * BOOTP reply, but the TFTP server was on the same
1382 * subnet. So this is probably a warning that your
1383 * configuration might be wrong. But I'm not really
1384 * sure if there aren't any other situations.
wdenk2d966952002-10-31 22:12:35 +00001385 */
1386 if (ip->ip_p == IPPROTO_ICMP) {
1387 ICMP_t *icmph = (ICMP_t *)&(ip->udp_src);
1388
wdenk73a8b272003-06-05 19:27:42 +00001389 switch (icmph->type) {
1390 case ICMP_REDIRECT:
wdenk90dc6702005-05-03 14:12:25 +00001391 if (icmph->code != ICMP_REDIR_HOST)
1392 return;
Mike Frysingerb6446b62009-02-17 00:00:53 -05001393 printf (" ICMP Host Redirect to %pI4 ", &icmph->un.gateway);
Stefan Roese8534bf92005-08-12 20:06:52 +02001394 return;
Jon Loeliger643d1ab2007-07-09 17:45:14 -05001395#if defined(CONFIG_CMD_PING)
wdenk73a8b272003-06-05 19:27:42 +00001396 case ICMP_ECHO_REPLY:
1397 /*
1398 * IP header OK. Pass the packet to the current handler.
1399 */
1400 /* XXX point to ip packet */
1401 (*packetHandler)((uchar *)ip, 0, 0, 0);
Stefan Roese8534bf92005-08-12 20:06:52 +02001402 return;
Ed Swarthout83853172007-03-07 12:14:50 -06001403 case ICMP_ECHO_REQUEST:
Robin Getz0ebf04c2009-07-23 03:01:03 -04001404 debug("Got ICMP ECHO REQUEST, return %d bytes \n",
Ed Swarthout83853172007-03-07 12:14:50 -06001405 ETHER_HDR_SIZE + len);
Robin Getz0ebf04c2009-07-23 03:01:03 -04001406
Ed Swarthout83853172007-03-07 12:14:50 -06001407 memcpy (&et->et_dest[0], &et->et_src[0], 6);
1408 memcpy (&et->et_src[ 0], NetOurEther, 6);
1409
1410 ip->ip_sum = 0;
1411 ip->ip_off = 0;
1412 NetCopyIP((void*)&ip->ip_dst, &ip->ip_src);
1413 NetCopyIP((void*)&ip->ip_src, &NetOurIP);
1414 ip->ip_sum = ~NetCksum((uchar *)ip, IP_HDR_SIZE_NO_UDP >> 1);
1415
1416 icmph->type = ICMP_ECHO_REPLY;
1417 icmph->checksum = 0;
1418 icmph->checksum = ~NetCksum((uchar *)icmph,
1419 (len - IP_HDR_SIZE_NO_UDP) >> 1);
1420 (void) eth_send((uchar *)et, ETHER_HDR_SIZE + len);
1421 return;
wdenk73a8b272003-06-05 19:27:42 +00001422#endif
1423 default:
1424 return;
1425 }
wdenk2d966952002-10-31 22:12:35 +00001426 } else if (ip->ip_p != IPPROTO_UDP) { /* Only UDP packets */
1427 return;
1428 }
1429
Stefan Roese8534bf92005-08-12 20:06:52 +02001430#ifdef CONFIG_UDP_CHECKSUM
1431 if (ip->udp_xsum != 0) {
Wolfgang Denkb2f50802005-08-12 23:43:12 +02001432 ulong xsum;
Stefan Roese8534bf92005-08-12 20:06:52 +02001433 ushort *sumptr;
1434 ushort sumlen;
1435
1436 xsum = ip->ip_p;
1437 xsum += (ntohs(ip->udp_len));
1438 xsum += (ntohl(ip->ip_src) >> 16) & 0x0000ffff;
1439 xsum += (ntohl(ip->ip_src) >> 0) & 0x0000ffff;
1440 xsum += (ntohl(ip->ip_dst) >> 16) & 0x0000ffff;
1441 xsum += (ntohl(ip->ip_dst) >> 0) & 0x0000ffff;
1442
1443 sumlen = ntohs(ip->udp_len);
1444 sumptr = (ushort *) &(ip->udp_src);
1445
1446 while (sumlen > 1) {
Wolfgang Denkb2f50802005-08-12 23:43:12 +02001447 ushort sumdata;
Stefan Roese8534bf92005-08-12 20:06:52 +02001448
1449 sumdata = *sumptr++;
1450 xsum += ntohs(sumdata);
1451 sumlen -= 2;
1452 }
1453 if (sumlen > 0) {
Wolfgang Denkb2f50802005-08-12 23:43:12 +02001454 ushort sumdata;
Stefan Roese8534bf92005-08-12 20:06:52 +02001455
1456 sumdata = *(unsigned char *) sumptr;
Wolfgang Denkb2f50802005-08-12 23:43:12 +02001457 sumdata = (sumdata << 8) & 0xff00;
Stefan Roese8534bf92005-08-12 20:06:52 +02001458 xsum += sumdata;
1459 }
1460 while ((xsum >> 16) != 0) {
Wolfgang Denkb2f50802005-08-12 23:43:12 +02001461 xsum = (xsum & 0x0000ffff) + ((xsum >> 16) & 0x0000ffff);
Stefan Roese8534bf92005-08-12 20:06:52 +02001462 }
1463 if ((xsum != 0x00000000) && (xsum != 0x0000ffff)) {
Wolfgang Denk9b55a252008-07-11 01:16:00 +02001464 printf(" UDP wrong checksum %08lx %08x\n",
1465 xsum, ntohs(ip->udp_xsum));
Stefan Roese8534bf92005-08-12 20:06:52 +02001466 return;
1467 }
1468 }
1469#endif
1470
David Updegraff53a5c422007-06-11 10:41:07 -05001471
wdenk68ceb292004-08-02 21:11:11 +00001472#ifdef CONFIG_NETCONSOLE
1473 nc_input_packet((uchar *)ip +IP_HDR_SIZE,
1474 ntohs(ip->udp_dst),
1475 ntohs(ip->udp_src),
1476 ntohs(ip->udp_len) - 8);
1477#endif
wdenk2d966952002-10-31 22:12:35 +00001478 /*
1479 * IP header OK. Pass the packet to the current handler.
1480 */
1481 (*packetHandler)((uchar *)ip +IP_HDR_SIZE,
1482 ntohs(ip->udp_dst),
1483 ntohs(ip->udp_src),
1484 ntohs(ip->udp_len) - 8);
wdenk2d966952002-10-31 22:12:35 +00001485 break;
1486 }
1487}
1488
1489
1490/**********************************************************************/
1491
1492static int net_check_prereq (proto_t protocol)
1493{
1494 switch (protocol) {
wdenk6e592382004-04-18 17:39:38 +00001495 /* Fall through */
Jon Loeliger643d1ab2007-07-09 17:45:14 -05001496#if defined(CONFIG_CMD_PING)
wdenk73a8b272003-06-05 19:27:42 +00001497 case PING:
wdenk6e592382004-04-18 17:39:38 +00001498 if (NetPingIP == 0) {
1499 puts ("*** ERROR: ping address not given\n");
1500 return (1);
1501 }
1502 goto common;
wdenk73a8b272003-06-05 19:27:42 +00001503#endif
Jon Loeliger643d1ab2007-07-09 17:45:14 -05001504#if defined(CONFIG_CMD_SNTP)
wdenkea287de2005-04-01 00:25:43 +00001505 case SNTP:
1506 if (NetNtpServerIP == 0) {
1507 puts ("*** ERROR: NTP server address not given\n");
1508 return (1);
1509 }
1510 goto common;
1511#endif
Robin Getz1a32bf42009-07-20 14:53:54 -04001512#if defined(CONFIG_CMD_DNS)
1513 case DNS:
1514 if (NetOurDNSIP == 0) {
1515 puts("*** ERROR: DNS server address not given\n");
1516 return 1;
1517 }
1518 goto common;
1519#endif
Jon Loeliger643d1ab2007-07-09 17:45:14 -05001520#if defined(CONFIG_CMD_NFS)
wdenkcbd8a352004-02-24 02:00:03 +00001521 case NFS:
1522#endif
wdenk68ceb292004-08-02 21:11:11 +00001523 case NETCONS:
wdenk2d966952002-10-31 22:12:35 +00001524 case TFTP:
wdenk6e592382004-04-18 17:39:38 +00001525 if (NetServerIP == 0) {
1526 puts ("*** ERROR: `serverip' not set\n");
1527 return (1);
1528 }
Jon Loeliger643d1ab2007-07-09 17:45:14 -05001529#if defined(CONFIG_CMD_PING) || defined(CONFIG_CMD_SNTP)
Wolfgang Denkb2f50802005-08-12 23:43:12 +02001530 common:
wdenk73a8b272003-06-05 19:27:42 +00001531#endif
1532
wdenk6e592382004-04-18 17:39:38 +00001533 if (NetOurIP == 0) {
1534 puts ("*** ERROR: `ipaddr' not set\n");
1535 return (1);
1536 }
1537 /* Fall through */
wdenk2d966952002-10-31 22:12:35 +00001538
1539 case DHCP:
1540 case RARP:
1541 case BOOTP:
wdenka3d991b2004-04-15 21:48:45 +00001542 case CDP:
wdenk6e592382004-04-18 17:39:38 +00001543 if (memcmp (NetOurEther, "\0\0\0\0\0\0", 6) == 0) {
wdenk2d966952002-10-31 22:12:35 +00001544#ifdef CONFIG_NET_MULTI
wdenk6e592382004-04-18 17:39:38 +00001545 extern int eth_get_dev_index (void);
1546 int num = eth_get_dev_index ();
wdenk2d966952002-10-31 22:12:35 +00001547
wdenk6e592382004-04-18 17:39:38 +00001548 switch (num) {
1549 case -1:
wdenk2d966952002-10-31 22:12:35 +00001550 puts ("*** ERROR: No ethernet found.\n");
1551 return (1);
wdenk6e592382004-04-18 17:39:38 +00001552 case 0:
wdenk2d966952002-10-31 22:12:35 +00001553 puts ("*** ERROR: `ethaddr' not set\n");
1554 break;
wdenk6e592382004-04-18 17:39:38 +00001555 default:
wdenk8bde7f72003-06-27 21:31:46 +00001556 printf ("*** ERROR: `eth%daddr' not set\n",
wdenk2d966952002-10-31 22:12:35 +00001557 num);
1558 break;
wdenk2d966952002-10-31 22:12:35 +00001559 }
wdenk6e592382004-04-18 17:39:38 +00001560
1561 NetStartAgain ();
1562 return (2);
1563#else
1564 puts ("*** ERROR: `ethaddr' not set\n");
1565 return (1);
1566#endif
1567 }
1568 /* Fall through */
1569 default:
1570 return (0);
wdenk2d966952002-10-31 22:12:35 +00001571 }
wdenk6e592382004-04-18 17:39:38 +00001572 return (0); /* OK */
wdenk2d966952002-10-31 22:12:35 +00001573}
1574/**********************************************************************/
1575
1576int
1577NetCksumOk(uchar * ptr, int len)
1578{
1579 return !((NetCksum(ptr, len) + 1) & 0xfffe);
1580}
1581
1582
1583unsigned
1584NetCksum(uchar * ptr, int len)
1585{
1586 ulong xsum;
Stefan Roese9d2a8732005-08-31 12:55:50 +02001587 ushort *p = (ushort *)ptr;
wdenk2d966952002-10-31 22:12:35 +00001588
1589 xsum = 0;
1590 while (len-- > 0)
Wolfgang Denk7bc5ee02005-08-26 01:36:03 +02001591 xsum += *p++;
wdenk2d966952002-10-31 22:12:35 +00001592 xsum = (xsum & 0xffff) + (xsum >> 16);
1593 xsum = (xsum & 0xffff) + (xsum >> 16);
1594 return (xsum & 0xffff);
1595}
1596
wdenka3d991b2004-04-15 21:48:45 +00001597int
1598NetEthHdrSize(void)
1599{
1600 ushort myvlanid;
wdenk2d966952002-10-31 22:12:35 +00001601
wdenka3d991b2004-04-15 21:48:45 +00001602 myvlanid = ntohs(NetOurVLAN);
1603 if (myvlanid == (ushort)-1)
1604 myvlanid = VLAN_NONE;
1605
1606 return ((myvlanid & VLAN_IDMASK) == VLAN_NONE) ? ETHER_HDR_SIZE : VLAN_ETHER_HDR_SIZE;
1607}
1608
1609int
wdenk2d966952002-10-31 22:12:35 +00001610NetSetEther(volatile uchar * xet, uchar * addr, uint prot)
1611{
1612 Ethernet_t *et = (Ethernet_t *)xet;
wdenka3d991b2004-04-15 21:48:45 +00001613 ushort myvlanid;
1614
1615 myvlanid = ntohs(NetOurVLAN);
1616 if (myvlanid == (ushort)-1)
1617 myvlanid = VLAN_NONE;
wdenk2d966952002-10-31 22:12:35 +00001618
1619 memcpy (et->et_dest, addr, 6);
1620 memcpy (et->et_src, NetOurEther, 6);
wdenka3d991b2004-04-15 21:48:45 +00001621 if ((myvlanid & VLAN_IDMASK) == VLAN_NONE) {
wdenk2d966952002-10-31 22:12:35 +00001622 et->et_protlen = htons(prot);
wdenka3d991b2004-04-15 21:48:45 +00001623 return ETHER_HDR_SIZE;
1624 } else {
1625 VLAN_Ethernet_t *vet = (VLAN_Ethernet_t *)xet;
wdenk2d966952002-10-31 22:12:35 +00001626
wdenka3d991b2004-04-15 21:48:45 +00001627 vet->vet_vlan_type = htons(PROT_VLAN);
1628 vet->vet_tag = htons((0 << 5) | (myvlanid & VLAN_IDMASK));
1629 vet->vet_type = htons(prot);
1630 return VLAN_ETHER_HDR_SIZE;
1631 }
1632}
wdenk2d966952002-10-31 22:12:35 +00001633
1634void
1635NetSetIP(volatile uchar * xip, IPaddr_t dest, int dport, int sport, int len)
1636{
Olav Morkenaf8626e2009-01-23 12:56:26 +01001637 IP_t *ip = (IP_t *)xip;
wdenk2d966952002-10-31 22:12:35 +00001638
1639 /*
1640 * If the data is an odd number of bytes, zero the
1641 * byte after the last byte so that the checksum
1642 * will work.
1643 */
1644 if (len & 1)
1645 xip[IP_HDR_SIZE + len] = 0;
1646
1647 /*
1648 * Construct an IP and UDP header.
wdenk6e592382004-04-18 17:39:38 +00001649 * (need to set no fragment bit - XXX)
wdenk2d966952002-10-31 22:12:35 +00001650 */
1651 ip->ip_hl_v = 0x45; /* IP_HDR_SIZE / 4 (not including UDP) */
1652 ip->ip_tos = 0;
1653 ip->ip_len = htons(IP_HDR_SIZE + len);
1654 ip->ip_id = htons(NetIPID++);
Peter Tysere0c07b82008-12-01 16:26:20 -06001655 ip->ip_off = htons(IP_FLAGS_DFRAG); /* Don't fragment */
wdenk2d966952002-10-31 22:12:35 +00001656 ip->ip_ttl = 255;
1657 ip->ip_p = 17; /* UDP */
1658 ip->ip_sum = 0;
1659 NetCopyIP((void*)&ip->ip_src, &NetOurIP); /* already in network byte order */
1660 NetCopyIP((void*)&ip->ip_dst, &dest); /* - "" - */
1661 ip->udp_src = htons(sport);
1662 ip->udp_dst = htons(dport);
1663 ip->udp_len = htons(8 + len);
1664 ip->udp_xsum = 0;
1665 ip->ip_sum = ~NetCksum((uchar *)ip, IP_HDR_SIZE_NO_UDP / 2);
1666}
1667
Wolfgang Denk77ddac92005-10-13 16:45:02 +02001668void copy_filename (char *dst, char *src, int size)
wdenk2d966952002-10-31 22:12:35 +00001669{
1670 if (*src && (*src == '"')) {
1671 ++src;
1672 --size;
1673 }
1674
1675 while ((--size > 0) && *src && (*src != '"')) {
1676 *dst++ = *src++;
1677 }
1678 *dst = '\0';
1679}
1680
Jon Loeliger610f2e92007-07-10 11:05:02 -05001681#endif
wdenk2d966952002-10-31 22:12:35 +00001682
Robin Getz1a32bf42009-07-20 14:53:54 -04001683#if defined(CONFIG_CMD_NFS) || defined(CONFIG_CMD_SNTP) || defined(CONFIG_CMD_DNS)
1684/*
1685 * make port a little random, but use something trivial to compute
1686 */
1687unsigned int random_port(void)
1688{
1689 return 1024 + (get_timer(0) % 0x8000);;
1690}
1691#endif
1692
wdenk2d966952002-10-31 22:12:35 +00001693void ip_to_string (IPaddr_t x, char *s)
1694{
wdenka3d991b2004-04-15 21:48:45 +00001695 x = ntohl (x);
1696 sprintf (s, "%d.%d.%d.%d",
1697 (int) ((x >> 24) & 0xff),
1698 (int) ((x >> 16) & 0xff),
1699 (int) ((x >> 8) & 0xff), (int) ((x >> 0) & 0xff)
wdenk6e592382004-04-18 17:39:38 +00001700 );
wdenk2d966952002-10-31 22:12:35 +00001701}
1702
wdenk73a8b272003-06-05 19:27:42 +00001703IPaddr_t string_to_ip(char *s)
wdenk2d966952002-10-31 22:12:35 +00001704{
1705 IPaddr_t addr;
wdenk73a8b272003-06-05 19:27:42 +00001706 char *e;
wdenk2d966952002-10-31 22:12:35 +00001707 int i;
1708
wdenk73a8b272003-06-05 19:27:42 +00001709 if (s == NULL)
1710 return(0);
wdenk2d966952002-10-31 22:12:35 +00001711
1712 for (addr=0, i=0; i<4; ++i) {
1713 ulong val = s ? simple_strtoul(s, &e, 10) : 0;
1714 addr <<= 8;
1715 addr |= (val & 0xFF);
1716 if (s) {
1717 s = (*e) ? e+1 : e;
1718 }
1719 }
1720
1721 return (htonl(addr));
1722}
wdenk73a8b272003-06-05 19:27:42 +00001723
wdenka3d991b2004-04-15 21:48:45 +00001724void VLAN_to_string(ushort x, char *s)
1725{
1726 x = ntohs(x);
1727
1728 if (x == (ushort)-1)
1729 x = VLAN_NONE;
1730
1731 if (x == VLAN_NONE)
1732 strcpy(s, "none");
1733 else
1734 sprintf(s, "%d", x & VLAN_IDMASK);
1735}
1736
1737ushort string_to_VLAN(char *s)
1738{
1739 ushort id;
1740
1741 if (s == NULL)
wdenkb9711de2004-04-25 13:18:40 +00001742 return htons(VLAN_NONE);
wdenka3d991b2004-04-15 21:48:45 +00001743
1744 if (*s < '0' || *s > '9')
1745 id = VLAN_NONE;
1746 else
1747 id = (ushort)simple_strtoul(s, NULL, 10);
1748
wdenkb9711de2004-04-25 13:18:40 +00001749 return htons(id);
wdenka3d991b2004-04-15 21:48:45 +00001750}
1751
wdenk73a8b272003-06-05 19:27:42 +00001752IPaddr_t getenv_IPaddr (char *var)
1753{
1754 return (string_to_ip(getenv(var)));
1755}
wdenka3d991b2004-04-15 21:48:45 +00001756
1757ushort getenv_VLAN(char *var)
1758{
1759 return (string_to_VLAN(getenv(var)));
1760}