blob: 261f7c187dd95b3ea97a1c9afa21eb2b0965336c [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 *
43 * Prerequisites: - own ethernet address
44 * We want: - IP, Netmask, ServerIP, Gateway IP
45 * - bootfilename, lease time
46 * Next step: - TFTP
47 *
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
wdenk2d966952002-10-31 22:12:35 +000067 */
68
69
70#include <common.h>
71#include <watchdog.h>
72#include <command.h>
73#include <net.h>
74#include "bootp.h"
75#include "tftp.h"
76#include "rarp.h"
wdenkcbd8a352004-02-24 02:00:03 +000077#include "nfs.h"
wdenkfc3e2162003-10-08 22:33:00 +000078#ifdef CONFIG_STATUS_LED
79#include <status_led.h>
80#include <miiphy.h>
81#endif
wdenk2d966952002-10-31 22:12:35 +000082
83#if (CONFIG_COMMANDS & CFG_CMD_NET)
84
wdenk73a8b272003-06-05 19:27:42 +000085#define ARP_TIMEOUT 5 /* Seconds before trying ARP again */
86#ifndef CONFIG_NET_RETRY_COUNT
87# define ARP_TIMEOUT_COUNT 5 /* # of timeouts before giving up */
88#else
89# define ARP_TIMEOUT_COUNT (CONFIG_NET_RETRY_COUNT)
90#endif
91
wdenk2d966952002-10-31 22:12:35 +000092#if 0
93#define ET_DEBUG
94#endif
95
96/** BOOTP EXTENTIONS **/
97
98IPaddr_t NetOurSubnetMask=0; /* Our subnet mask (0=unknown) */
99IPaddr_t NetOurGatewayIP=0; /* Our gateways IP address */
100IPaddr_t NetOurDNSIP=0; /* Our DNS IP address */
stroesefe389a82003-08-28 14:17:32 +0000101#if (CONFIG_BOOTP_MASK & CONFIG_BOOTP_DNS2)
102IPaddr_t NetOurDNS2IP=0; /* Our 2nd DNS IP address */
103#endif
wdenk2d966952002-10-31 22:12:35 +0000104char NetOurNISDomain[32]={0,}; /* Our NIS domain */
105char NetOurHostName[32]={0,}; /* Our hostname */
106char NetOurRootPath[64]={0,}; /* Our bootpath */
107ushort NetBootFileSize=0; /* Our bootfile size in blocks */
108
109/** END OF BOOTP EXTENTIONS **/
110
111ulong NetBootFileXferSize; /* The actual transferred size of the bootfile (in bytes) */
112uchar NetOurEther[6]; /* Our ethernet address */
113uchar NetServerEther[6] = /* Boot server enet address */
wdenk73a8b272003-06-05 19:27:42 +0000114 { 0, 0, 0, 0, 0, 0 };
wdenk2d966952002-10-31 22:12:35 +0000115IPaddr_t NetOurIP; /* Our IP addr (0 = unknown) */
116IPaddr_t NetServerIP; /* Our IP addr (0 = unknown) */
117volatile uchar *NetRxPkt; /* Current receive packet */
118int NetRxPktLen; /* Current rx packet length */
119unsigned NetIPID; /* IP packet ID */
120uchar NetBcastAddr[6] = /* Ethernet bcast address */
121 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
wdenk73a8b272003-06-05 19:27:42 +0000122uchar NetEtherNullAddr[6] =
123 { 0, 0, 0, 0, 0, 0 };
wdenka3d991b2004-04-15 21:48:45 +0000124#if (CONFIG_COMMANDS & CFG_CMD_CDP)
wdenk6e592382004-04-18 17:39:38 +0000125uchar NetCDPAddr[6] = /* Ethernet bcast address */
wdenka3d991b2004-04-15 21:48:45 +0000126 { 0x01, 0x00, 0x0c, 0xcc, 0xcc, 0xcc };
127#endif
wdenk2d966952002-10-31 22:12:35 +0000128int NetState; /* Network loop state */
129#ifdef CONFIG_NET_MULTI
130int NetRestartWrap = 0; /* Tried all network devices */
131static int NetRestarted = 0; /* Network loop restarted */
132static int NetDevExists = 0; /* At least one device configured */
133#endif
134
wdenk6e592382004-04-18 17:39:38 +0000135/* XXX in both little & big endian machines 0xFFFF == ntohs(-1) */
136ushort NetOurVLAN = 0xFFFF; /* default is without VLAN */
137ushort NetOurNativeVLAN = 0xFFFF; /* ditto */
wdenka3d991b2004-04-15 21:48:45 +0000138
wdenk2d966952002-10-31 22:12:35 +0000139char BootFile[128]; /* Boot File name */
140
wdenk73a8b272003-06-05 19:27:42 +0000141#if (CONFIG_COMMANDS & CFG_CMD_PING)
142IPaddr_t NetPingIP; /* the ip address to ping */
143
144static void PingStart(void);
145#endif
146
wdenka3d991b2004-04-15 21:48:45 +0000147#if (CONFIG_COMMANDS & CFG_CMD_CDP)
148static void CDPStart(void);
149#endif
150
wdenk68ceb292004-08-02 21:11:11 +0000151#ifdef CONFIG_NETCONSOLE
152void NcStart(void);
153int nc_input_packet(uchar *pkt, unsigned dest, unsigned src, unsigned len);
154#endif
155
wdenk2d966952002-10-31 22:12:35 +0000156volatile uchar PktBuf[(PKTBUFSRX+1) * PKTSIZE_ALIGN + PKTALIGN];
157
158volatile uchar *NetRxPackets[PKTBUFSRX]; /* Receive packets */
159
160static rxhand_f *packetHandler; /* Current RX packet handler */
161static thand_f *timeHandler; /* Current timeout handler */
wdenke0ac62d2003-08-17 18:55:18 +0000162static ulong timeStart; /* Time base value */
163static ulong timeDelta; /* Current timeout value */
wdenk2d966952002-10-31 22:12:35 +0000164volatile uchar *NetTxPacket = 0; /* THE transmit packet */
165
166static int net_check_prereq (proto_t protocol);
167
168/**********************************************************************/
wdenk73a8b272003-06-05 19:27:42 +0000169
170IPaddr_t NetArpWaitPacketIP;
171IPaddr_t NetArpWaitReplyIP;
172uchar *NetArpWaitPacketMAC; /* MAC address of waiting packet's destination */
173uchar *NetArpWaitTxPacket; /* THE transmit packet */
174int NetArpWaitTxPacketSize;
175uchar NetArpWaitPacketBuf[PKTSIZE_ALIGN + PKTALIGN];
176ulong NetArpWaitTimerStart;
177int NetArpWaitTry;
178
wdenk6e592382004-04-18 17:39:38 +0000179void ArpRequest (void)
wdenk73a8b272003-06-05 19:27:42 +0000180{
181 int i;
182 volatile uchar *pkt;
wdenk6e592382004-04-18 17:39:38 +0000183 ARP_t *arp;
wdenk73a8b272003-06-05 19:27:42 +0000184
185#ifdef ET_DEBUG
wdenk6e592382004-04-18 17:39:38 +0000186 printf ("ARP broadcast %d\n", NetArpWaitTry);
wdenk73a8b272003-06-05 19:27:42 +0000187#endif
188 pkt = NetTxPacket;
189
wdenk6e592382004-04-18 17:39:38 +0000190 pkt += NetSetEther (pkt, NetBcastAddr, PROT_ARP);
wdenk73a8b272003-06-05 19:27:42 +0000191
wdenk6e592382004-04-18 17:39:38 +0000192 arp = (ARP_t *) pkt;
wdenk73a8b272003-06-05 19:27:42 +0000193
wdenk6e592382004-04-18 17:39:38 +0000194 arp->ar_hrd = htons (ARP_ETHER);
195 arp->ar_pro = htons (PROT_IP);
wdenk73a8b272003-06-05 19:27:42 +0000196 arp->ar_hln = 6;
197 arp->ar_pln = 4;
wdenk6e592382004-04-18 17:39:38 +0000198 arp->ar_op = htons (ARPOP_REQUEST);
wdenk73a8b272003-06-05 19:27:42 +0000199
wdenk6e592382004-04-18 17:39:38 +0000200 memcpy (&arp->ar_data[0], NetOurEther, 6); /* source ET addr */
201 NetWriteIP ((uchar *) & arp->ar_data[6], NetOurIP); /* source IP addr */
202 for (i = 10; i < 16; ++i) {
203 arp->ar_data[i] = 0; /* dest ET addr = 0 */
wdenk73a8b272003-06-05 19:27:42 +0000204 }
205
wdenk6e592382004-04-18 17:39:38 +0000206 if ((NetArpWaitPacketIP & NetOurSubnetMask) !=
207 (NetOurIP & NetOurSubnetMask)) {
208 if (NetOurGatewayIP == 0) {
209 puts ("## Warning: gatewayip needed but not set\n");
210 }
211 NetArpWaitReplyIP = NetOurGatewayIP;
212 } else {
213 NetArpWaitReplyIP = NetArpWaitPacketIP;
214 }
wdenk73a8b272003-06-05 19:27:42 +0000215
wdenk6e592382004-04-18 17:39:38 +0000216 NetWriteIP ((uchar *) & arp->ar_data[16], NetArpWaitReplyIP);
217 (void) eth_send (NetTxPacket, (pkt - NetTxPacket) + ARP_HDR_SIZE);
wdenk73a8b272003-06-05 19:27:42 +0000218}
219
220void ArpTimeoutCheck(void)
221{
222 ulong t;
223
224 if (!NetArpWaitPacketIP)
225 return;
226
227 t = get_timer(0);
228
229 /* check for arp timeout */
230 if ((t - NetArpWaitTimerStart) > ARP_TIMEOUT * CFG_HZ) {
231 NetArpWaitTry++;
232
233 if (NetArpWaitTry >= ARP_TIMEOUT_COUNT) {
234 puts ("\nARP Retry count exceeded; starting again\n");
235 NetArpWaitTry = 0;
236 NetStartAgain();
237 } else {
238 NetArpWaitTimerStart = t;
239 ArpRequest();
240 }
241 }
242}
243
244/**********************************************************************/
wdenk2d966952002-10-31 22:12:35 +0000245/*
246 * Main network processing loop.
247 */
248
249int
250NetLoop(proto_t protocol)
251{
252 DECLARE_GLOBAL_DATA_PTR;
253
254 bd_t *bd = gd->bd;
255
256#ifdef CONFIG_NET_MULTI
257 NetRestarted = 0;
258 NetDevExists = 0;
259#endif
260
wdenk73a8b272003-06-05 19:27:42 +0000261 /* XXX problem with bss workaround */
262 NetArpWaitPacketMAC = NULL;
263 NetArpWaitTxPacket = NULL;
264 NetArpWaitPacketIP = 0;
265 NetArpWaitReplyIP = 0;
266 NetArpWaitTxPacket = NULL;
267 NetTxPacket = NULL;
268
wdenk2d966952002-10-31 22:12:35 +0000269 if (!NetTxPacket) {
270 int i;
wdenk2d966952002-10-31 22:12:35 +0000271 /*
272 * Setup packet buffers, aligned correctly.
273 */
274 NetTxPacket = &PktBuf[0] + (PKTALIGN - 1);
275 NetTxPacket -= (ulong)NetTxPacket % PKTALIGN;
276 for (i = 0; i < PKTBUFSRX; i++) {
277 NetRxPackets[i] = NetTxPacket + (i+1)*PKTSIZE_ALIGN;
278 }
wdenk73a8b272003-06-05 19:27:42 +0000279 }
280
281 if (!NetArpWaitTxPacket) {
282 NetArpWaitTxPacket = &NetArpWaitPacketBuf[0] + (PKTALIGN - 1);
283 NetArpWaitTxPacket -= (ulong)NetArpWaitTxPacket % PKTALIGN;
284 NetArpWaitTxPacketSize = 0;
wdenk2d966952002-10-31 22:12:35 +0000285 }
286
287 eth_halt();
wdenk6e592382004-04-18 17:39:38 +0000288#ifdef CONFIG_NET_MULTI
wdenka3d991b2004-04-15 21:48:45 +0000289 eth_set_current();
wdenk6e592382004-04-18 17:39:38 +0000290#endif
291 if (eth_init(bd) < 0)
292 return(-1);
wdenk2d966952002-10-31 22:12:35 +0000293
294restart:
295#ifdef CONFIG_NET_MULTI
296 memcpy (NetOurEther, eth_get_dev()->enetaddr, 6);
297#else
298 memcpy (NetOurEther, bd->bi_enetaddr, 6);
299#endif
300
301 NetState = NETLOOP_CONTINUE;
302
303 /*
304 * Start the ball rolling with the given start function. From
305 * here on, this code is a state machine driven by received
306 * packets and timer events.
307 */
308
309 switch (protocol) {
wdenkcbd8a352004-02-24 02:00:03 +0000310#if (CONFIG_COMMANDS & CFG_CMD_NFS)
311 case NFS:
312#endif
wdenk73a8b272003-06-05 19:27:42 +0000313#if (CONFIG_COMMANDS & CFG_CMD_PING)
314 case PING:
315#endif
wdenk68ceb292004-08-02 21:11:11 +0000316 case NETCONS:
wdenk2d966952002-10-31 22:12:35 +0000317 case TFTP:
318 NetCopyIP(&NetOurIP, &bd->bi_ip_addr);
wdenk2d966952002-10-31 22:12:35 +0000319 NetOurGatewayIP = getenv_IPaddr ("gatewayip");
320 NetOurSubnetMask= getenv_IPaddr ("netmask");
wdenka3d991b2004-04-15 21:48:45 +0000321 NetOurVLAN = getenv_VLAN("vlan");
322 NetOurNativeVLAN = getenv_VLAN("nvlan");
wdenk73a8b272003-06-05 19:27:42 +0000323
324 switch (protocol) {
wdenkcbd8a352004-02-24 02:00:03 +0000325#if (CONFIG_COMMANDS & CFG_CMD_NFS)
326 case NFS:
327#endif
wdenk68ceb292004-08-02 21:11:11 +0000328 case NETCONS:
wdenk73a8b272003-06-05 19:27:42 +0000329 case TFTP:
330 NetServerIP = getenv_IPaddr ("serverip");
331 break;
332#if (CONFIG_COMMANDS & CFG_CMD_PING)
333 case PING:
334 /* nothing */
335 break;
336#endif
337 default:
338 break;
339 }
340
wdenk2d966952002-10-31 22:12:35 +0000341 break;
342 case BOOTP:
343 case RARP:
344 /*
wdenk8bde7f72003-06-27 21:31:46 +0000345 * initialize our IP addr to 0 in order to accept ANY
346 * IP addr assigned to us by the BOOTP / RARP server
wdenk2d966952002-10-31 22:12:35 +0000347 */
348 NetOurIP = 0;
wdenk3d3befa2004-03-14 15:06:13 +0000349 NetServerIP = getenv_IPaddr ("serverip");
wdenka3d991b2004-04-15 21:48:45 +0000350 NetOurVLAN = getenv_VLAN("vlan"); /* VLANs must be read */
351 NetOurNativeVLAN = getenv_VLAN("nvlan");
352 case CDP:
353 NetOurVLAN = getenv_VLAN("vlan"); /* VLANs must be read */
354 NetOurNativeVLAN = getenv_VLAN("nvlan");
wdenk2d966952002-10-31 22:12:35 +0000355 break;
356 default:
357 break;
358 }
359
360 switch (net_check_prereq (protocol)) {
361 case 1:
362 /* network not configured */
wdenk1d0350e2002-11-11 21:14:20 +0000363 return (-1);
wdenk2d966952002-10-31 22:12:35 +0000364
365#ifdef CONFIG_NET_MULTI
366 case 2:
367 /* network device not configured */
368 break;
369#endif /* CONFIG_NET_MULTI */
370
371 case 0:
372#ifdef CONFIG_NET_MULTI
373 NetDevExists = 1;
374#endif
375 switch (protocol) {
376 case TFTP:
377 /* always use ARP to get server ethernet address */
wdenk73a8b272003-06-05 19:27:42 +0000378 TftpStart();
wdenk2d966952002-10-31 22:12:35 +0000379 break;
380
381#if (CONFIG_COMMANDS & CFG_CMD_DHCP)
382 case DHCP:
383 /* Start with a clean slate... */
384 NetOurIP = 0;
wdenk3d3befa2004-03-14 15:06:13 +0000385 NetServerIP = getenv_IPaddr ("serverip");
wdenk2d966952002-10-31 22:12:35 +0000386 DhcpRequest(); /* Basically same as BOOTP */
387 break;
388#endif /* CFG_CMD_DHCP */
389
390 case BOOTP:
391 BootpTry = 0;
392 BootpRequest ();
393 break;
394
395 case RARP:
396 RarpTry = 0;
397 RarpRequest ();
398 break;
wdenk73a8b272003-06-05 19:27:42 +0000399#if (CONFIG_COMMANDS & CFG_CMD_PING)
400 case PING:
401 PingStart();
402 break;
403#endif
wdenkcbd8a352004-02-24 02:00:03 +0000404#if (CONFIG_COMMANDS & CFG_CMD_NFS)
405 case NFS:
406 NfsStart();
407 break;
408#endif
wdenka3d991b2004-04-15 21:48:45 +0000409#if (CONFIG_COMMANDS & CFG_CMD_CDP)
410 case CDP:
411 CDPStart();
412 break;
413#endif
wdenk68ceb292004-08-02 21:11:11 +0000414#ifdef CONFIG_NETCONSOLE
415 case NETCONS:
416 NcStart();
417 break;
418#endif
wdenk2d966952002-10-31 22:12:35 +0000419 default:
420 break;
421 }
422
423 NetBootFileXferSize = 0;
424 break;
425 }
426
wdenkfc3e2162003-10-08 22:33:00 +0000427#if defined(CONFIG_MII) || (CONFIG_COMMANDS & CFG_CMD_MII)
428#if defined(CFG_FAULT_ECHO_LINK_DOWN) && defined(CONFIG_STATUS_LED) && defined(STATUS_LED_RED)
429 /*
wdenk42d1f032003-10-15 23:53:47 +0000430 * Echo the inverted link state to the fault LED.
wdenkfc3e2162003-10-08 22:33:00 +0000431 */
432 if(miiphy_link(CFG_FAULT_MII_ADDR)) {
433 status_led_set (STATUS_LED_RED, STATUS_LED_OFF);
434 } else {
435 status_led_set (STATUS_LED_RED, STATUS_LED_ON);
436 }
437#endif /* CFG_FAULT_ECHO_LINK_DOWN, ... */
438#endif /* CONFIG_MII, ... */
wdenk2d966952002-10-31 22:12:35 +0000439
440 /*
441 * Main packet reception loop. Loop receiving packets until
442 * someone sets `NetQuit'.
443 */
444 for (;;) {
445 WATCHDOG_RESET();
446#ifdef CONFIG_SHOW_ACTIVITY
447 {
448 extern void show_activity(int arg);
449 show_activity(1);
450 }
451#endif
452 /*
453 * Check the ethernet for a new packet. The ethernet
454 * receive routine will process it.
455 */
456 eth_rx();
457
458 /*
459 * Abort if ctrl-c was pressed.
460 */
461 if (ctrlc()) {
wdenk8bde7f72003-06-27 21:31:46 +0000462 eth_halt();
wdenk4b9206e2004-03-23 22:14:11 +0000463 puts ("\nAbort\n");
wdenk1d0350e2002-11-11 21:14:20 +0000464 return (-1);
wdenk2d966952002-10-31 22:12:35 +0000465 }
466
wdenk73a8b272003-06-05 19:27:42 +0000467 ArpTimeoutCheck();
wdenk2d966952002-10-31 22:12:35 +0000468
469 /*
470 * Check for a timeout, and run the timeout handler
471 * if we have one.
472 */
wdenke0ac62d2003-08-17 18:55:18 +0000473 if (timeHandler && ((get_timer(0) - timeStart) > timeDelta)) {
wdenk2d966952002-10-31 22:12:35 +0000474 thand_f *x;
475
wdenkfc3e2162003-10-08 22:33:00 +0000476#if defined(CONFIG_MII) || (CONFIG_COMMANDS & CFG_CMD_MII)
477#if defined(CFG_FAULT_ECHO_LINK_DOWN) && defined(CONFIG_STATUS_LED) && defined(STATUS_LED_RED)
478 /*
wdenk42d1f032003-10-15 23:53:47 +0000479 * Echo the inverted link state to the fault LED.
wdenkfc3e2162003-10-08 22:33:00 +0000480 */
481 if(miiphy_link(CFG_FAULT_MII_ADDR)) {
482 status_led_set (STATUS_LED_RED, STATUS_LED_OFF);
483 } else {
484 status_led_set (STATUS_LED_RED, STATUS_LED_ON);
485 }
486#endif /* CFG_FAULT_ECHO_LINK_DOWN, ... */
487#endif /* CONFIG_MII, ... */
wdenk2d966952002-10-31 22:12:35 +0000488 x = timeHandler;
489 timeHandler = (thand_f *)0;
490 (*x)();
491 }
492
493
494 switch (NetState) {
495
496 case NETLOOP_RESTART:
497#ifdef CONFIG_NET_MULTI
498 NetRestarted = 1;
499#endif
500 goto restart;
501
502 case NETLOOP_SUCCESS:
503 if (NetBootFileXferSize > 0) {
504 char buf[10];
505 printf("Bytes transferred = %ld (%lx hex)\n",
506 NetBootFileXferSize,
507 NetBootFileXferSize);
508 sprintf(buf, "%lx", NetBootFileXferSize);
509 setenv("filesize", buf);
wdenka3d991b2004-04-15 21:48:45 +0000510
511 sprintf(buf, "%lX", (unsigned long)load_addr);
512 setenv("fileaddr", buf);
wdenk2d966952002-10-31 22:12:35 +0000513 }
514 eth_halt();
515 return NetBootFileXferSize;
516
517 case NETLOOP_FAIL:
wdenk1d0350e2002-11-11 21:14:20 +0000518 return (-1);
wdenk2d966952002-10-31 22:12:35 +0000519 }
520 }
521}
522
523/**********************************************************************/
524
525static void
526startAgainTimeout(void)
527{
528 NetState = NETLOOP_RESTART;
529}
530
531static void
532startAgainHandler(uchar * pkt, unsigned dest, unsigned src, unsigned len)
533{
534 /* Totally ignore the packet */
535}
536
wdenk6e592382004-04-18 17:39:38 +0000537void NetStartAgain (void)
wdenk2d966952002-10-31 22:12:35 +0000538{
wdenk6e592382004-04-18 17:39:38 +0000539#ifdef CONFIG_NET_MULTI
wdenka3d991b2004-04-15 21:48:45 +0000540 DECLARE_GLOBAL_DATA_PTR;
wdenk6e592382004-04-18 17:39:38 +0000541#endif
542 char *nretry;
543 int noretry = 0, once = 0;
wdenka3d991b2004-04-15 21:48:45 +0000544
wdenk6e592382004-04-18 17:39:38 +0000545 if ((nretry = getenv ("netretry")) != NULL) {
546 noretry = (strcmp (nretry, "no") == 0);
547 once = (strcmp (nretry, "once") == 0);
548 }
549 if (noretry) {
550 eth_halt ();
wdenka3d991b2004-04-15 21:48:45 +0000551 NetState = NETLOOP_FAIL;
552 return;
553 }
wdenk2d966952002-10-31 22:12:35 +0000554#ifndef CONFIG_NET_MULTI
wdenk6e592382004-04-18 17:39:38 +0000555 NetSetTimeout (10 * CFG_HZ, startAgainTimeout);
556 NetSetHandler (startAgainHandler);
557#else /* !CONFIG_NET_MULTI*/
558 eth_halt ();
559 eth_try_another (!NetRestarted);
560 eth_init (gd->bd);
561 if (NetRestartWrap) {
wdenk2d966952002-10-31 22:12:35 +0000562 NetRestartWrap = 0;
wdenk6e592382004-04-18 17:39:38 +0000563 if (NetDevExists && !once) {
564 NetSetTimeout (10 * CFG_HZ, startAgainTimeout);
565 NetSetHandler (startAgainHandler);
566 } else {
wdenk2d966952002-10-31 22:12:35 +0000567 NetState = NETLOOP_FAIL;
568 }
wdenk6e592382004-04-18 17:39:38 +0000569 } else {
wdenk2d966952002-10-31 22:12:35 +0000570 NetState = NETLOOP_RESTART;
571 }
wdenk6e592382004-04-18 17:39:38 +0000572#endif /* CONFIG_NET_MULTI */
wdenk2d966952002-10-31 22:12:35 +0000573}
574
575/**********************************************************************/
576/*
577 * Miscelaneous bits.
578 */
579
580void
581NetSetHandler(rxhand_f * f)
582{
583 packetHandler = f;
584}
585
586
587void
588NetSetTimeout(int iv, thand_f * f)
589{
590 if (iv == 0) {
591 timeHandler = (thand_f *)0;
592 } else {
593 timeHandler = f;
wdenke0ac62d2003-08-17 18:55:18 +0000594 timeStart = get_timer(0);
595 timeDelta = iv;
wdenk2d966952002-10-31 22:12:35 +0000596 }
597}
598
599
600void
601NetSendPacket(volatile uchar * pkt, int len)
602{
603 (void) eth_send(pkt, len);
604}
605
wdenk73a8b272003-06-05 19:27:42 +0000606int
607NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport, int sport, int len)
608{
wdenka3d991b2004-04-15 21:48:45 +0000609 uchar *pkt;
610
wdenk73a8b272003-06-05 19:27:42 +0000611 /* convert to new style broadcast */
612 if (dest == 0)
613 dest = 0xFFFFFFFF;
wdenk2d966952002-10-31 22:12:35 +0000614
wdenk73a8b272003-06-05 19:27:42 +0000615 /* if broadcast, make the ether address a broadcast and don't do ARP */
616 if (dest == 0xFFFFFFFF)
617 ether = NetBcastAddr;
618
619 /* if MAC address was not discovered yet, save the packet and do an ARP request */
620 if (memcmp(ether, NetEtherNullAddr, 6) == 0) {
621
622#ifdef ET_DEBUG
623 printf("sending ARP for %08lx\n", dest);
624#endif
wdenk73a8b272003-06-05 19:27:42 +0000625 NetArpWaitPacketIP = dest;
626 NetArpWaitPacketMAC = ether;
wdenka3d991b2004-04-15 21:48:45 +0000627
628 pkt = NetArpWaitTxPacket;
629 pkt += NetSetEther (pkt, NetArpWaitPacketMAC, PROT_IP);
630
631 NetSetIP (pkt, dest, dport, sport, len);
632 memcpy(pkt + IP_HDR_SIZE, (uchar *)NetTxPacket + (pkt - (uchar *)NetArpWaitTxPacket) + IP_HDR_SIZE, len);
wdenk73a8b272003-06-05 19:27:42 +0000633
634 /* size of the waiting packet */
wdenka3d991b2004-04-15 21:48:45 +0000635 NetArpWaitTxPacketSize = (pkt - NetArpWaitTxPacket) + IP_HDR_SIZE + len;
wdenk73a8b272003-06-05 19:27:42 +0000636
637 /* and do the ARP request */
638 NetArpWaitTry = 1;
639 NetArpWaitTimerStart = get_timer(0);
640 ArpRequest();
641 return 1; /* waiting */
642 }
643
644#ifdef ET_DEBUG
645 printf("sending UDP to %08lx/%02x:%02x:%02x:%02x:%02x:%02x\n",
wdenk6e592382004-04-18 17:39:38 +0000646 dest, ether[0], ether[1], ether[2], ether[3], ether[4], ether[5]);
wdenk73a8b272003-06-05 19:27:42 +0000647#endif
648
wdenka3d991b2004-04-15 21:48:45 +0000649 pkt = (uchar *)NetTxPacket;
650 pkt += NetSetEther (pkt, ether, PROT_IP);
651 NetSetIP (pkt, dest, dport, sport, len);
652 (void) eth_send(NetTxPacket, (pkt - NetTxPacket) + IP_HDR_SIZE + len);
wdenk73a8b272003-06-05 19:27:42 +0000653
wdenk6e592382004-04-18 17:39:38 +0000654 return 0; /* transmitted */
wdenk73a8b272003-06-05 19:27:42 +0000655}
656
657#if (CONFIG_COMMANDS & CFG_CMD_PING)
658static ushort PingSeqNo;
659
660int PingSend(void)
661{
662 static uchar mac[6];
663 volatile IP_t *ip;
664 volatile ushort *s;
wdenka3d991b2004-04-15 21:48:45 +0000665 uchar *pkt;
wdenk73a8b272003-06-05 19:27:42 +0000666
667 /* XXX always send arp request */
668
669 memcpy(mac, NetEtherNullAddr, 6);
670
671#ifdef ET_DEBUG
672 printf("sending ARP for %08lx\n", NetPingIP);
673#endif
674
675 NetArpWaitPacketIP = NetPingIP;
676 NetArpWaitPacketMAC = mac;
677
wdenka3d991b2004-04-15 21:48:45 +0000678 pkt = NetArpWaitTxPacket;
679 pkt += NetSetEther(pkt, mac, PROT_IP);
wdenk73a8b272003-06-05 19:27:42 +0000680
wdenka3d991b2004-04-15 21:48:45 +0000681 ip = (volatile IP_t *)pkt;
wdenk73a8b272003-06-05 19:27:42 +0000682
683 /*
684 * Construct an IP and ICMP header. (need to set no fragment bit - XXX)
685 */
686 ip->ip_hl_v = 0x45; /* IP_HDR_SIZE / 4 (not including UDP) */
687 ip->ip_tos = 0;
688 ip->ip_len = htons(IP_HDR_SIZE_NO_UDP + 8);
689 ip->ip_id = htons(NetIPID++);
690 ip->ip_off = htons(0x4000); /* No fragmentation */
691 ip->ip_ttl = 255;
692 ip->ip_p = 0x01; /* ICMP */
693 ip->ip_sum = 0;
694 NetCopyIP((void*)&ip->ip_src, &NetOurIP); /* already in network byte order */
695 NetCopyIP((void*)&ip->ip_dst, &NetPingIP); /* - "" - */
696 ip->ip_sum = ~NetCksum((uchar *)ip, IP_HDR_SIZE_NO_UDP / 2);
697
698 s = &ip->udp_src; /* XXX ICMP starts here */
699 s[0] = htons(0x0800); /* echo-request, code */
700 s[1] = 0; /* checksum */
701 s[2] = 0; /* identifier */
702 s[3] = htons(PingSeqNo++); /* sequence number */
703 s[1] = ~NetCksum((uchar *)s, 8/2);
704
705 /* size of the waiting packet */
wdenka3d991b2004-04-15 21:48:45 +0000706 NetArpWaitTxPacketSize = (pkt - NetArpWaitTxPacket) + IP_HDR_SIZE_NO_UDP + 8;
wdenk73a8b272003-06-05 19:27:42 +0000707
708 /* and do the ARP request */
709 NetArpWaitTry = 1;
710 NetArpWaitTimerStart = get_timer(0);
711 ArpRequest();
712 return 1; /* waiting */
713}
714
715static void
716PingTimeout (void)
717{
718 eth_halt();
719 NetState = NETLOOP_FAIL; /* we did not get the reply */
720}
721
722static void
723PingHandler (uchar * pkt, unsigned dest, unsigned src, unsigned len)
724{
725 IPaddr_t tmp;
726 volatile IP_t *ip = (volatile IP_t *)pkt;
727
728 tmp = NetReadIP((void *)&ip->ip_src);
729 if (tmp != NetPingIP)
730 return;
731
732 NetState = NETLOOP_SUCCESS;
733}
734
735static void PingStart(void)
736{
wdenka3d991b2004-04-15 21:48:45 +0000737#if defined(CONFIG_NET_MULTI)
738 printf ("Using %s device\n", eth_get_name());
wdenk6e592382004-04-18 17:39:38 +0000739#endif /* CONFIG_NET_MULTI */
wdenk73a8b272003-06-05 19:27:42 +0000740 NetSetTimeout (10 * CFG_HZ, PingTimeout);
741 NetSetHandler (PingHandler);
742
743 PingSend();
744}
wdenk6e592382004-04-18 17:39:38 +0000745#endif /* CFG_CMD_PING */
wdenk2d966952002-10-31 22:12:35 +0000746
wdenka3d991b2004-04-15 21:48:45 +0000747#if (CONFIG_COMMANDS & CFG_CMD_CDP)
748
749#define CDP_DEVICE_ID_TLV 0x0001
750#define CDP_ADDRESS_TLV 0x0002
751#define CDP_PORT_ID_TLV 0x0003
752#define CDP_CAPABILITIES_TLV 0x0004
753#define CDP_VERSION_TLV 0x0005
754#define CDP_PLATFORM_TLV 0x0006
755#define CDP_NATIVE_VLAN_TLV 0x000a
756#define CDP_APPLIANCE_VLAN_TLV 0x000e
757#define CDP_TRIGGER_TLV 0x000f
758#define CDP_POWER_CONSUMPTION_TLV 0x0010
759#define CDP_SYSNAME_TLV 0x0014
760#define CDP_SYSOBJECT_TLV 0x0015
761#define CDP_MANAGEMENT_ADDRESS_TLV 0x0016
762
763#define CDP_TIMEOUT (CFG_HZ/4) /* one packet every 250ms */
764
765static int CDPSeq;
766static int CDPOK;
767
768ushort CDPNativeVLAN;
769ushort CDPApplianceVLAN;
770
771static const uchar CDP_SNAP_hdr[8] = { 0xAA, 0xAA, 0x03, 0x00, 0x00, 0x0C, 0x20, 0x00 };
772
773static ushort CDP_compute_csum(const uchar *buff, ushort len)
774{
775 ushort csum;
776 int odd;
777 ulong result = 0;
778 ushort leftover;
779
780 if (len > 0) {
781 odd = 1 & (ulong)buff;
782 if (odd) {
783 result = *buff << 8;
784 len--;
785 buff++;
786 }
787 while (len > 1) {
788 result += *((const ushort *)buff)++;
789 if (result & 0x80000000)
790 result = (result & 0xFFFF) + (result >> 16);
791 len -= 2;
792 }
793 if (len) {
794 leftover = (signed short)(*(const signed char *)buff);
795 /* * XXX CISCO SUCKS big time! (and blows too) */
796 result = (result & 0xffff0000) | ((result + leftover) & 0x0000ffff);
797 }
798 while (result >> 16)
799 result = (result & 0xFFFF) + (result >> 16);
800
801 if (odd)
802 result = ((result >> 8) & 0xff) | ((result & 0xff) << 8);
803 }
804
805 /* add up 16-bit and 17-bit words for 17+c bits */
806 result = (result & 0xffff) + (result >> 16);
807 /* add up 16-bit and 2-bit for 16+c bit */
808 result = (result & 0xffff) + (result >> 16);
809 /* add up carry.. */
810 result = (result & 0xffff) + (result >> 16);
811
812 /* negate */
813 csum = ~(ushort)result;
814
815 /* run time endian detection */
816 if (csum != htons(csum)) /* little endian */
817 csum = htons(csum);
818
819 return csum;
820}
821
822int CDPSendTrigger(void)
823{
824 volatile uchar *pkt;
825 volatile ushort *s;
826 volatile ushort *cp;
827 Ethernet_t *et;
wdenka3d991b2004-04-15 21:48:45 +0000828 int len;
829 ushort chksum;
wdenk6e592382004-04-18 17:39:38 +0000830#if defined(CONFIG_CDP_DEVICE_ID) || defined(CONFIG_CDP_PORT_ID) || \
831 defined(CONFIG_CDP_VERSION) || defined(CONFIG_CDP_PLATFORM)
832 char buf[32];
833#endif
wdenka3d991b2004-04-15 21:48:45 +0000834
835 pkt = NetTxPacket;
836 et = (Ethernet_t *)pkt;
837
838 /* NOTE: trigger sent not on any VLAN */
839
840 /* form ethernet header */
841 memcpy(et->et_dest, NetCDPAddr, 6);
842 memcpy(et->et_src, NetOurEther, 6);
843
844 pkt += ETHER_HDR_SIZE;
845
846 /* SNAP header */
847 memcpy((uchar *)pkt, CDP_SNAP_hdr, sizeof(CDP_SNAP_hdr));
848 pkt += sizeof(CDP_SNAP_hdr);
849
850 /* CDP header */
851 *pkt++ = 0x02; /* CDP version 2 */
852 *pkt++ = 180; /* TTL */
853 s = (volatile ushort *)pkt;
854 cp = s;
855 *s++ = htons(0); /* checksum (0 for later calculation) */
856
857 /* CDP fields */
858#ifdef CONFIG_CDP_DEVICE_ID
859 *s++ = htons(CDP_DEVICE_ID_TLV);
860 *s++ = htons(CONFIG_CDP_DEVICE_ID);
861 memset(buf, 0, sizeof(buf));
862 sprintf(buf, CONFIG_CDP_DEVICE_ID_PREFIX "%02X%02X%02X%02X%02X%02X",
863 NetOurEther[0] & 0xff, NetOurEther[1] & 0xff,
864 NetOurEther[2] & 0xff, NetOurEther[3] & 0xff,
865 NetOurEther[4] & 0xff, NetOurEther[5] & 0xff);
866 memcpy((uchar *)s, buf, 16);
867 s += 16 / 2;
868#endif
869
870#ifdef CONFIG_CDP_PORT_ID
871 *s++ = htons(CDP_PORT_ID_TLV);
872 memset(buf, 0, sizeof(buf));
873 sprintf(buf, CONFIG_CDP_PORT_ID, eth_get_dev_index());
874 len = strlen(buf);
875 if (len & 1) /* make it even */
876 len++;
877 *s++ = htons(len + 4);
878 memcpy((uchar *)s, buf, len);
879 s += len / 2;
880#endif
881
882#ifdef CONFIG_CDP_CAPABILITIES
883 *s++ = htons(CDP_CAPABILITIES_TLV);
884 *s++ = htons(8);
885 *(ulong *)s = htonl(CONFIG_CDP_CAPABILITIES);
886 s += 2;
887#endif
888
889#ifdef CONFIG_CDP_VERSION
890 *s++ = htons(CDP_VERSION_TLV);
891 memset(buf, 0, sizeof(buf));
892 strcpy(buf, CONFIG_CDP_VERSION);
893 len = strlen(buf);
894 if (len & 1) /* make it even */
895 len++;
896 *s++ = htons(len + 4);
897 memcpy((uchar *)s, buf, len);
898 s += len / 2;
899#endif
900
901#ifdef CONFIG_CDP_PLATFORM
902 *s++ = htons(CDP_PLATFORM_TLV);
903 memset(buf, 0, sizeof(buf));
904 strcpy(buf, CONFIG_CDP_PLATFORM);
905 len = strlen(buf);
906 if (len & 1) /* make it even */
907 len++;
908 *s++ = htons(len + 4);
909 memcpy((uchar *)s, buf, len);
910 s += len / 2;
911#endif
912
913#ifdef CONFIG_CDP_TRIGGER
914 *s++ = htons(CDP_TRIGGER_TLV);
915 *s++ = htons(8);
916 *(ulong *)s = htonl(CONFIG_CDP_TRIGGER);
917 s += 2;
918#endif
919
920#ifdef CONFIG_CDP_POWER_CONSUMPTION
921 *s++ = htons(CDP_POWER_CONSUMPTION_TLV);
922 *s++ = htons(6);
923 *s++ = htons(CONFIG_CDP_POWER_CONSUMPTION);
924#endif
925
926 /* length of ethernet packet */
927 len = (uchar *)s - ((uchar *)NetTxPacket + ETHER_HDR_SIZE);
928 et->et_protlen = htons(len);
929
930 len = ETHER_HDR_SIZE + sizeof(CDP_SNAP_hdr);
931 chksum = CDP_compute_csum((uchar *)NetTxPacket + len, (uchar *)s - (NetTxPacket + len));
932 if (chksum == 0)
933 chksum = 0xFFFF;
934 *cp = htons(chksum);
935
936 (void) eth_send(NetTxPacket, (uchar *)s - NetTxPacket);
937 return 0;
938}
939
940static void
941CDPTimeout (void)
942{
943 CDPSeq++;
944
945 if (CDPSeq < 3) {
946 NetSetTimeout (CDP_TIMEOUT, CDPTimeout);
947 CDPSendTrigger();
948 return;
949 }
950
951 /* if not OK try again */
952 if (!CDPOK)
953 NetStartAgain();
954 else
955 NetState = NETLOOP_SUCCESS;
956}
957
958static void
959CDPDummyHandler (uchar * pkt, unsigned dest, unsigned src, unsigned len)
960{
961 /* nothing */
962}
963
964static void
965CDPHandler(const uchar * pkt, unsigned len)
966{
967 const uchar *t;
968 const ushort *ss;
969 ushort type, tlen;
970 uchar applid;
971 ushort vlan, nvlan;
972
973 /* minimum size? */
974 if (len < sizeof(CDP_SNAP_hdr) + 4)
975 goto pkt_short;
976
977 /* check for valid CDP SNAP header */
978 if (memcmp(pkt, CDP_SNAP_hdr, sizeof(CDP_SNAP_hdr)) != 0)
979 return;
980
981 pkt += sizeof(CDP_SNAP_hdr);
982 len -= sizeof(CDP_SNAP_hdr);
983
984 /* Version of CDP protocol must be >= 2 and TTL != 0 */
985 if (pkt[0] < 0x02 || pkt[1] == 0)
986 return;
987
988 /* if version is greater than 0x02 maybe we'll have a problem; output a warning */
989 if (pkt[0] != 0x02)
990 printf("** WARNING: CDP packet received with a protocol version %d > 2\n",
991 pkt[0] & 0xff);
992
993 if (CDP_compute_csum(pkt, len) != 0)
994 return;
995
996 pkt += 4;
997 len -= 4;
998
999 vlan = htons(-1);
1000 nvlan = htons(-1);
1001 while (len > 0) {
1002 if (len < 4)
1003 goto pkt_short;
1004
1005 ss = (const ushort *)pkt;
1006 type = ntohs(ss[0]);
1007 tlen = ntohs(ss[1]);
1008 if (tlen > len) {
1009 goto pkt_short;
1010 }
1011
1012 pkt += tlen;
1013 len -= tlen;
1014
1015 ss += 2; /* point ss to the data of the TLV */
1016 tlen -= 4;
1017
1018 switch (type) {
1019 case CDP_DEVICE_ID_TLV:
1020 break;
1021 case CDP_ADDRESS_TLV:
1022 break;
1023 case CDP_PORT_ID_TLV:
1024 break;
1025 case CDP_CAPABILITIES_TLV:
1026 break;
1027 case CDP_VERSION_TLV:
1028 break;
1029 case CDP_PLATFORM_TLV:
1030 break;
1031 case CDP_NATIVE_VLAN_TLV:
1032 nvlan = *ss;
1033 break;
1034 case CDP_APPLIANCE_VLAN_TLV:
1035 t = (const uchar *)ss;
1036 while (tlen > 0) {
1037 if (tlen < 3)
1038 goto pkt_short;
1039
1040 applid = t[0];
1041 ss = (const ushort *)(t + 1);
1042
1043#ifdef CONFIG_CDP_APPLIANCE_VLAN_TYPE
1044 if (applid == CONFIG_CDP_APPLIANCE_VLAN_TYPE)
1045 vlan = *ss;
1046#else
1047 vlan = ntohs(*ss); /* XXX will this work; dunno */
1048#endif
1049 t += 3; tlen -= 3;
1050 }
1051 break;
1052 case CDP_TRIGGER_TLV:
1053 break;
1054 case CDP_POWER_CONSUMPTION_TLV:
1055 break;
1056 case CDP_SYSNAME_TLV:
1057 break;
1058 case CDP_SYSOBJECT_TLV:
1059 break;
1060 case CDP_MANAGEMENT_ADDRESS_TLV:
1061 break;
1062 }
1063 }
1064
1065 CDPApplianceVLAN = vlan;
1066 CDPNativeVLAN = nvlan;
1067
1068 CDPOK = 1;
1069 return;
1070
1071 pkt_short:
1072 printf("** CDP packet is too short\n");
1073 return;
1074}
1075
1076static void CDPStart(void)
1077{
1078#if defined(CONFIG_NET_MULTI)
1079 printf ("Using %s device\n", eth_get_name());
1080#endif
1081 CDPSeq = 0;
1082 CDPOK = 0;
1083
1084 CDPNativeVLAN = htons(-1);
1085 CDPApplianceVLAN = htons(-1);
1086
1087 NetSetTimeout (CDP_TIMEOUT, CDPTimeout);
1088 NetSetHandler (CDPDummyHandler);
1089
1090 CDPSendTrigger();
1091}
wdenk6e592382004-04-18 17:39:38 +00001092#endif /* CFG_CMD_CDP */
wdenka3d991b2004-04-15 21:48:45 +00001093
1094
wdenk2d966952002-10-31 22:12:35 +00001095void
wdenka3d991b2004-04-15 21:48:45 +00001096NetReceive(volatile uchar * inpkt, int len)
wdenk2d966952002-10-31 22:12:35 +00001097{
1098 Ethernet_t *et;
1099 IP_t *ip;
1100 ARP_t *arp;
1101 IPaddr_t tmp;
1102 int x;
wdenka3d991b2004-04-15 21:48:45 +00001103 uchar *pkt;
1104#if (CONFIG_COMMANDS & CFG_CMD_CDP)
1105 int iscdp;
1106#endif
1107 ushort cti = 0, vlanid = VLAN_NONE, myvlanid, mynvlanid;
wdenk2d966952002-10-31 22:12:35 +00001108
wdenka3d991b2004-04-15 21:48:45 +00001109#ifdef ET_DEBUG
1110 printf("packet received\n");
1111#endif
1112
1113 NetRxPkt = inpkt;
wdenk2d966952002-10-31 22:12:35 +00001114 NetRxPktLen = len;
wdenka3d991b2004-04-15 21:48:45 +00001115 et = (Ethernet_t *)inpkt;
1116
1117 /* too small packet? */
1118 if (len < ETHER_HDR_SIZE)
1119 return;
1120
1121#if (CONFIG_COMMANDS & CFG_CMD_CDP)
1122 /* keep track if packet is CDP */
1123 iscdp = memcmp(et->et_dest, NetCDPAddr, 6) == 0;
1124#endif
1125
1126 myvlanid = ntohs(NetOurVLAN);
1127 if (myvlanid == (ushort)-1)
1128 myvlanid = VLAN_NONE;
1129 mynvlanid = ntohs(NetOurNativeVLAN);
1130 if (mynvlanid == (ushort)-1)
1131 mynvlanid = VLAN_NONE;
wdenk2d966952002-10-31 22:12:35 +00001132
1133 x = ntohs(et->et_protlen);
1134
wdenka3d991b2004-04-15 21:48:45 +00001135#ifdef ET_DEBUG
1136 printf("packet received\n");
1137#endif
1138
wdenk2d966952002-10-31 22:12:35 +00001139 if (x < 1514) {
1140 /*
1141 * Got a 802 packet. Check the other protocol field.
1142 */
1143 x = ntohs(et->et_prot);
wdenka3d991b2004-04-15 21:48:45 +00001144
1145 ip = (IP_t *)(inpkt + E802_HDR_SIZE);
wdenk2d966952002-10-31 22:12:35 +00001146 len -= E802_HDR_SIZE;
wdenka3d991b2004-04-15 21:48:45 +00001147
1148 } else if (x != PROT_VLAN) { /* normal packet */
1149 ip = (IP_t *)(inpkt + ETHER_HDR_SIZE);
wdenk2d966952002-10-31 22:12:35 +00001150 len -= ETHER_HDR_SIZE;
wdenka3d991b2004-04-15 21:48:45 +00001151
1152 } else { /* VLAN packet */
1153 VLAN_Ethernet_t *vet = (VLAN_Ethernet_t *)et;
1154
1155#ifdef ET_DEBUG
1156 printf("VLAN packet received\n");
1157#endif
1158 /* too small packet? */
1159 if (len < VLAN_ETHER_HDR_SIZE)
1160 return;
1161
1162 /* if no VLAN active */
1163 if ((ntohs(NetOurVLAN) & VLAN_IDMASK) == VLAN_NONE
1164#if (CONFIG_COMMANDS & CFG_CMD_CDP)
1165 && iscdp == 0
1166#endif
1167 )
1168 return;
1169
1170 cti = ntohs(vet->vet_tag);
1171 vlanid = cti & VLAN_IDMASK;
1172 x = ntohs(vet->vet_type);
1173
1174 ip = (IP_t *)(inpkt + VLAN_ETHER_HDR_SIZE);
1175 len -= VLAN_ETHER_HDR_SIZE;
wdenk2d966952002-10-31 22:12:35 +00001176 }
1177
1178#ifdef ET_DEBUG
1179 printf("Receive from protocol 0x%x\n", x);
1180#endif
1181
wdenka3d991b2004-04-15 21:48:45 +00001182#if (CONFIG_COMMANDS & CFG_CMD_CDP)
1183 if (iscdp) {
1184 CDPHandler((uchar *)ip, len);
1185 return;
1186 }
1187#endif
1188
1189 if ((myvlanid & VLAN_IDMASK) != VLAN_NONE) {
1190 if (vlanid == VLAN_NONE)
1191 vlanid = (mynvlanid & VLAN_IDMASK);
1192 /* not matched? */
1193 if (vlanid != (myvlanid & VLAN_IDMASK))
1194 return;
1195 }
1196
wdenk2d966952002-10-31 22:12:35 +00001197 switch (x) {
1198
1199 case PROT_ARP:
1200 /*
1201 * We have to deal with two types of ARP packets:
wdenk8bde7f72003-06-27 21:31:46 +00001202 * - REQUEST packets will be answered by sending our
1203 * IP address - if we know it.
1204 * - REPLY packates are expected only after we asked
1205 * for the TFTP server's or the gateway's ethernet
1206 * address; so if we receive such a packet, we set
1207 * the server ethernet address
wdenk2d966952002-10-31 22:12:35 +00001208 */
1209#ifdef ET_DEBUG
wdenk4b9206e2004-03-23 22:14:11 +00001210 puts ("Got ARP\n");
wdenk2d966952002-10-31 22:12:35 +00001211#endif
1212 arp = (ARP_t *)ip;
1213 if (len < ARP_HDR_SIZE) {
1214 printf("bad length %d < %d\n", len, ARP_HDR_SIZE);
1215 return;
1216 }
1217 if (ntohs(arp->ar_hrd) != ARP_ETHER) {
1218 return;
1219 }
1220 if (ntohs(arp->ar_pro) != PROT_IP) {
1221 return;
1222 }
1223 if (arp->ar_hln != 6) {
1224 return;
1225 }
1226 if (arp->ar_pln != 4) {
1227 return;
1228 }
1229
1230 if (NetOurIP == 0) {
1231 return;
1232 }
1233
1234 if (NetReadIP(&arp->ar_data[16]) != NetOurIP) {
1235 return;
1236 }
1237
1238 switch (ntohs(arp->ar_op)) {
1239 case ARPOP_REQUEST: /* reply with our IP address */
1240#ifdef ET_DEBUG
wdenk4b9206e2004-03-23 22:14:11 +00001241 puts ("Got ARP REQUEST, return our IP\n");
wdenk2d966952002-10-31 22:12:35 +00001242#endif
wdenka3d991b2004-04-15 21:48:45 +00001243 pkt = (uchar *)et;
1244 pkt += NetSetEther(pkt, et->et_src, PROT_ARP);
wdenk2d966952002-10-31 22:12:35 +00001245 arp->ar_op = htons(ARPOP_REPLY);
1246 memcpy (&arp->ar_data[10], &arp->ar_data[0], 6);
1247 NetCopyIP(&arp->ar_data[16], &arp->ar_data[6]);
1248 memcpy (&arp->ar_data[ 0], NetOurEther, 6);
1249 NetCopyIP(&arp->ar_data[ 6], &NetOurIP);
wdenka3d991b2004-04-15 21:48:45 +00001250 (void) eth_send((uchar *)et, (pkt - (uchar *)et) + ARP_HDR_SIZE);
wdenk2d966952002-10-31 22:12:35 +00001251 return;
wdenk73a8b272003-06-05 19:27:42 +00001252
1253 case ARPOP_REPLY: /* arp reply */
1254 /* are we waiting for a reply */
1255 if (!NetArpWaitPacketIP || !NetArpWaitPacketMAC)
1256 break;
wdenk2d966952002-10-31 22:12:35 +00001257#ifdef ET_DEBUG
wdenk73a8b272003-06-05 19:27:42 +00001258 printf("Got ARP REPLY, set server/gtwy eth addr (%02x:%02x:%02x:%02x:%02x:%02x)\n",
1259 arp->ar_data[0], arp->ar_data[1],
1260 arp->ar_data[2], arp->ar_data[3],
1261 arp->ar_data[4], arp->ar_data[5]);
wdenk2d966952002-10-31 22:12:35 +00001262#endif
wdenk73a8b272003-06-05 19:27:42 +00001263
1264 tmp = NetReadIP(&arp->ar_data[6]);
1265
1266 /* matched waiting packet's address */
1267 if (tmp == NetArpWaitReplyIP) {
1268#ifdef ET_DEBUG
wdenk4b9206e2004-03-23 22:14:11 +00001269 puts ("Got it\n");
wdenk73a8b272003-06-05 19:27:42 +00001270#endif
1271 /* save address for later use */
1272 memcpy(NetArpWaitPacketMAC, &arp->ar_data[0], 6);
1273
wdenk68ceb292004-08-02 21:11:11 +00001274#ifdef CONFIG_NETCONSOLE
1275 (*packetHandler)(0,0,0,0);
1276#endif
wdenk73a8b272003-06-05 19:27:42 +00001277 /* modify header, and transmit it */
1278 memcpy(((Ethernet_t *)NetArpWaitTxPacket)->et_dest, NetArpWaitPacketMAC, 6);
1279 (void) eth_send(NetArpWaitTxPacket, NetArpWaitTxPacketSize);
1280
1281 /* no arp request pending now */
1282 NetArpWaitPacketIP = 0;
1283 NetArpWaitTxPacketSize = 0;
1284 NetArpWaitPacketMAC = NULL;
1285
1286 }
wdenk2d966952002-10-31 22:12:35 +00001287 return;
1288 default:
1289#ifdef ET_DEBUG
1290 printf("Unexpected ARP opcode 0x%x\n", ntohs(arp->ar_op));
1291#endif
1292 return;
1293 }
1294
1295 case PROT_RARP:
1296#ifdef ET_DEBUG
wdenk4b9206e2004-03-23 22:14:11 +00001297 puts ("Got RARP\n");
wdenk2d966952002-10-31 22:12:35 +00001298#endif
1299 arp = (ARP_t *)ip;
1300 if (len < ARP_HDR_SIZE) {
1301 printf("bad length %d < %d\n", len, ARP_HDR_SIZE);
1302 return;
1303 }
1304
1305 if ((ntohs(arp->ar_op) != RARPOP_REPLY) ||
1306 (ntohs(arp->ar_hrd) != ARP_ETHER) ||
1307 (ntohs(arp->ar_pro) != PROT_IP) ||
1308 (arp->ar_hln != 6) || (arp->ar_pln != 4)) {
1309
wdenk4b9206e2004-03-23 22:14:11 +00001310 puts ("invalid RARP header\n");
wdenk2d966952002-10-31 22:12:35 +00001311 } else {
1312 NetCopyIP(&NetOurIP, &arp->ar_data[16]);
wdenk3d3befa2004-03-14 15:06:13 +00001313 if (NetServerIP == 0)
1314 NetCopyIP(&NetServerIP, &arp->ar_data[ 6]);
wdenk2d966952002-10-31 22:12:35 +00001315 memcpy (NetServerEther, &arp->ar_data[ 0], 6);
1316
1317 (*packetHandler)(0,0,0,0);
1318 }
1319 break;
1320
1321 case PROT_IP:
1322#ifdef ET_DEBUG
wdenk4b9206e2004-03-23 22:14:11 +00001323 puts ("Got IP\n");
wdenk2d966952002-10-31 22:12:35 +00001324#endif
1325 if (len < IP_HDR_SIZE) {
1326 debug ("len bad %d < %d\n", len, IP_HDR_SIZE);
1327 return;
1328 }
1329 if (len < ntohs(ip->ip_len)) {
1330 printf("len bad %d < %d\n", len, ntohs(ip->ip_len));
1331 return;
1332 }
1333 len = ntohs(ip->ip_len);
1334#ifdef ET_DEBUG
1335 printf("len=%d, v=%02x\n", len, ip->ip_hl_v & 0xff);
1336#endif
1337 if ((ip->ip_hl_v & 0xf0) != 0x40) {
1338 return;
1339 }
1340 if (ip->ip_off & htons(0x1fff)) { /* Can't deal w/ fragments */
1341 return;
1342 }
1343 if (!NetCksumOk((uchar *)ip, IP_HDR_SIZE_NO_UDP / 2)) {
wdenk4b9206e2004-03-23 22:14:11 +00001344 puts ("checksum bad\n");
wdenk2d966952002-10-31 22:12:35 +00001345 return;
1346 }
1347 tmp = NetReadIP(&ip->ip_dst);
1348 if (NetOurIP && tmp != NetOurIP && tmp != 0xFFFFFFFF) {
1349 return;
1350 }
1351 /*
1352 * watch for ICMP host redirects
1353 *
wdenk8bde7f72003-06-27 21:31:46 +00001354 * There is no real handler code (yet). We just watch
1355 * for ICMP host redirect messages. In case anybody
1356 * sees these messages: please contact me
1357 * (wd@denx.de), or - even better - send me the
1358 * necessary fixes :-)
wdenk2d966952002-10-31 22:12:35 +00001359 *
wdenk8bde7f72003-06-27 21:31:46 +00001360 * Note: in all cases where I have seen this so far
1361 * it was a problem with the router configuration,
1362 * for instance when a router was configured in the
1363 * BOOTP reply, but the TFTP server was on the same
1364 * subnet. So this is probably a warning that your
1365 * configuration might be wrong. But I'm not really
1366 * sure if there aren't any other situations.
wdenk2d966952002-10-31 22:12:35 +00001367 */
1368 if (ip->ip_p == IPPROTO_ICMP) {
1369 ICMP_t *icmph = (ICMP_t *)&(ip->udp_src);
1370
wdenk73a8b272003-06-05 19:27:42 +00001371 switch (icmph->type) {
1372 case ICMP_REDIRECT:
wdenk2d966952002-10-31 22:12:35 +00001373 if (icmph->code != ICMP_REDIR_HOST)
1374 return;
1375 puts (" ICMP Host Redirect to ");
1376 print_IPaddr(icmph->un.gateway);
1377 putc(' ');
wdenk73a8b272003-06-05 19:27:42 +00001378 break;
1379#if (CONFIG_COMMANDS & CFG_CMD_PING)
1380 case ICMP_ECHO_REPLY:
1381 /*
1382 * IP header OK. Pass the packet to the current handler.
1383 */
1384 /* XXX point to ip packet */
1385 (*packetHandler)((uchar *)ip, 0, 0, 0);
1386 break;
1387#endif
1388 default:
1389 return;
1390 }
wdenk2d966952002-10-31 22:12:35 +00001391 } else if (ip->ip_p != IPPROTO_UDP) { /* Only UDP packets */
1392 return;
1393 }
1394
wdenk68ceb292004-08-02 21:11:11 +00001395#ifdef CONFIG_NETCONSOLE
1396 nc_input_packet((uchar *)ip +IP_HDR_SIZE,
1397 ntohs(ip->udp_dst),
1398 ntohs(ip->udp_src),
1399 ntohs(ip->udp_len) - 8);
1400#endif
wdenk2d966952002-10-31 22:12:35 +00001401 /*
1402 * IP header OK. Pass the packet to the current handler.
1403 */
1404 (*packetHandler)((uchar *)ip +IP_HDR_SIZE,
1405 ntohs(ip->udp_dst),
1406 ntohs(ip->udp_src),
1407 ntohs(ip->udp_len) - 8);
wdenk2d966952002-10-31 22:12:35 +00001408 break;
1409 }
1410}
1411
1412
1413/**********************************************************************/
1414
1415static int net_check_prereq (proto_t protocol)
1416{
1417 switch (protocol) {
wdenk6e592382004-04-18 17:39:38 +00001418 /* Fall through */
wdenk73a8b272003-06-05 19:27:42 +00001419#if (CONFIG_COMMANDS & CFG_CMD_PING)
1420 case PING:
wdenk6e592382004-04-18 17:39:38 +00001421 if (NetPingIP == 0) {
1422 puts ("*** ERROR: ping address not given\n");
1423 return (1);
1424 }
1425 goto common;
wdenk73a8b272003-06-05 19:27:42 +00001426#endif
wdenkcbd8a352004-02-24 02:00:03 +00001427#if (CONFIG_COMMANDS & CFG_CMD_NFS)
1428 case NFS:
1429#endif
wdenk68ceb292004-08-02 21:11:11 +00001430 case NETCONS:
wdenk2d966952002-10-31 22:12:35 +00001431 case TFTP:
wdenk6e592382004-04-18 17:39:38 +00001432 if (NetServerIP == 0) {
1433 puts ("*** ERROR: `serverip' not set\n");
1434 return (1);
1435 }
wdenk73a8b272003-06-05 19:27:42 +00001436#if (CONFIG_COMMANDS & CFG_CMD_PING)
wdenk6e592382004-04-18 17:39:38 +00001437 common:
wdenk73a8b272003-06-05 19:27:42 +00001438#endif
1439
wdenk6e592382004-04-18 17:39:38 +00001440 if (NetOurIP == 0) {
1441 puts ("*** ERROR: `ipaddr' not set\n");
1442 return (1);
1443 }
1444 /* Fall through */
wdenk2d966952002-10-31 22:12:35 +00001445
1446 case DHCP:
1447 case RARP:
1448 case BOOTP:
wdenka3d991b2004-04-15 21:48:45 +00001449 case CDP:
wdenk6e592382004-04-18 17:39:38 +00001450 if (memcmp (NetOurEther, "\0\0\0\0\0\0", 6) == 0) {
wdenk2d966952002-10-31 22:12:35 +00001451#ifdef CONFIG_NET_MULTI
wdenk6e592382004-04-18 17:39:38 +00001452 extern int eth_get_dev_index (void);
1453 int num = eth_get_dev_index ();
wdenk2d966952002-10-31 22:12:35 +00001454
wdenk6e592382004-04-18 17:39:38 +00001455 switch (num) {
1456 case -1:
wdenk2d966952002-10-31 22:12:35 +00001457 puts ("*** ERROR: No ethernet found.\n");
1458 return (1);
wdenk6e592382004-04-18 17:39:38 +00001459 case 0:
wdenk2d966952002-10-31 22:12:35 +00001460 puts ("*** ERROR: `ethaddr' not set\n");
1461 break;
wdenk6e592382004-04-18 17:39:38 +00001462 default:
wdenk8bde7f72003-06-27 21:31:46 +00001463 printf ("*** ERROR: `eth%daddr' not set\n",
wdenk2d966952002-10-31 22:12:35 +00001464 num);
1465 break;
wdenk2d966952002-10-31 22:12:35 +00001466 }
wdenk6e592382004-04-18 17:39:38 +00001467
1468 NetStartAgain ();
1469 return (2);
1470#else
1471 puts ("*** ERROR: `ethaddr' not set\n");
1472 return (1);
1473#endif
1474 }
1475 /* Fall through */
1476 default:
1477 return (0);
wdenk2d966952002-10-31 22:12:35 +00001478 }
wdenk6e592382004-04-18 17:39:38 +00001479 return (0); /* OK */
wdenk2d966952002-10-31 22:12:35 +00001480}
1481/**********************************************************************/
1482
1483int
1484NetCksumOk(uchar * ptr, int len)
1485{
1486 return !((NetCksum(ptr, len) + 1) & 0xfffe);
1487}
1488
1489
1490unsigned
1491NetCksum(uchar * ptr, int len)
1492{
1493 ulong xsum;
1494
1495 xsum = 0;
1496 while (len-- > 0)
1497 xsum += *((ushort *)ptr)++;
1498 xsum = (xsum & 0xffff) + (xsum >> 16);
1499 xsum = (xsum & 0xffff) + (xsum >> 16);
1500 return (xsum & 0xffff);
1501}
1502
wdenka3d991b2004-04-15 21:48:45 +00001503int
1504NetEthHdrSize(void)
1505{
1506 ushort myvlanid;
wdenk2d966952002-10-31 22:12:35 +00001507
wdenka3d991b2004-04-15 21:48:45 +00001508 myvlanid = ntohs(NetOurVLAN);
1509 if (myvlanid == (ushort)-1)
1510 myvlanid = VLAN_NONE;
1511
1512 return ((myvlanid & VLAN_IDMASK) == VLAN_NONE) ? ETHER_HDR_SIZE : VLAN_ETHER_HDR_SIZE;
1513}
1514
1515int
wdenk2d966952002-10-31 22:12:35 +00001516NetSetEther(volatile uchar * xet, uchar * addr, uint prot)
1517{
1518 Ethernet_t *et = (Ethernet_t *)xet;
wdenka3d991b2004-04-15 21:48:45 +00001519 ushort myvlanid;
1520
1521 myvlanid = ntohs(NetOurVLAN);
1522 if (myvlanid == (ushort)-1)
1523 myvlanid = VLAN_NONE;
wdenk2d966952002-10-31 22:12:35 +00001524
1525 memcpy (et->et_dest, addr, 6);
1526 memcpy (et->et_src, NetOurEther, 6);
wdenka3d991b2004-04-15 21:48:45 +00001527 if ((myvlanid & VLAN_IDMASK) == VLAN_NONE) {
wdenk2d966952002-10-31 22:12:35 +00001528 et->et_protlen = htons(prot);
wdenka3d991b2004-04-15 21:48:45 +00001529 return ETHER_HDR_SIZE;
1530 } else {
1531 VLAN_Ethernet_t *vet = (VLAN_Ethernet_t *)xet;
wdenk2d966952002-10-31 22:12:35 +00001532
wdenka3d991b2004-04-15 21:48:45 +00001533 vet->vet_vlan_type = htons(PROT_VLAN);
1534 vet->vet_tag = htons((0 << 5) | (myvlanid & VLAN_IDMASK));
1535 vet->vet_type = htons(prot);
1536 return VLAN_ETHER_HDR_SIZE;
1537 }
1538}
wdenk2d966952002-10-31 22:12:35 +00001539
1540void
1541NetSetIP(volatile uchar * xip, IPaddr_t dest, int dport, int sport, int len)
1542{
1543 volatile IP_t *ip = (IP_t *)xip;
1544
1545 /*
1546 * If the data is an odd number of bytes, zero the
1547 * byte after the last byte so that the checksum
1548 * will work.
1549 */
1550 if (len & 1)
1551 xip[IP_HDR_SIZE + len] = 0;
1552
1553 /*
1554 * Construct an IP and UDP header.
wdenk6e592382004-04-18 17:39:38 +00001555 * (need to set no fragment bit - XXX)
wdenk2d966952002-10-31 22:12:35 +00001556 */
1557 ip->ip_hl_v = 0x45; /* IP_HDR_SIZE / 4 (not including UDP) */
1558 ip->ip_tos = 0;
1559 ip->ip_len = htons(IP_HDR_SIZE + len);
1560 ip->ip_id = htons(NetIPID++);
1561 ip->ip_off = htons(0x4000); /* No fragmentation */
1562 ip->ip_ttl = 255;
1563 ip->ip_p = 17; /* UDP */
1564 ip->ip_sum = 0;
1565 NetCopyIP((void*)&ip->ip_src, &NetOurIP); /* already in network byte order */
1566 NetCopyIP((void*)&ip->ip_dst, &dest); /* - "" - */
1567 ip->udp_src = htons(sport);
1568 ip->udp_dst = htons(dport);
1569 ip->udp_len = htons(8 + len);
1570 ip->udp_xsum = 0;
1571 ip->ip_sum = ~NetCksum((uchar *)ip, IP_HDR_SIZE_NO_UDP / 2);
1572}
1573
1574void copy_filename (uchar *dst, uchar *src, int size)
1575{
1576 if (*src && (*src == '"')) {
1577 ++src;
1578 --size;
1579 }
1580
1581 while ((--size > 0) && *src && (*src != '"')) {
1582 *dst++ = *src++;
1583 }
1584 *dst = '\0';
1585}
1586
1587#endif /* CFG_CMD_NET */
1588
1589void ip_to_string (IPaddr_t x, char *s)
1590{
wdenka3d991b2004-04-15 21:48:45 +00001591 x = ntohl (x);
1592 sprintf (s, "%d.%d.%d.%d",
1593 (int) ((x >> 24) & 0xff),
1594 (int) ((x >> 16) & 0xff),
1595 (int) ((x >> 8) & 0xff), (int) ((x >> 0) & 0xff)
wdenk6e592382004-04-18 17:39:38 +00001596 );
wdenk2d966952002-10-31 22:12:35 +00001597}
1598
wdenk73a8b272003-06-05 19:27:42 +00001599IPaddr_t string_to_ip(char *s)
wdenk2d966952002-10-31 22:12:35 +00001600{
1601 IPaddr_t addr;
wdenk73a8b272003-06-05 19:27:42 +00001602 char *e;
wdenk2d966952002-10-31 22:12:35 +00001603 int i;
1604
wdenk73a8b272003-06-05 19:27:42 +00001605 if (s == NULL)
1606 return(0);
wdenk2d966952002-10-31 22:12:35 +00001607
1608 for (addr=0, i=0; i<4; ++i) {
1609 ulong val = s ? simple_strtoul(s, &e, 10) : 0;
1610 addr <<= 8;
1611 addr |= (val & 0xFF);
1612 if (s) {
1613 s = (*e) ? e+1 : e;
1614 }
1615 }
1616
1617 return (htonl(addr));
1618}
wdenk73a8b272003-06-05 19:27:42 +00001619
wdenka3d991b2004-04-15 21:48:45 +00001620void VLAN_to_string(ushort x, char *s)
1621{
1622 x = ntohs(x);
1623
1624 if (x == (ushort)-1)
1625 x = VLAN_NONE;
1626
1627 if (x == VLAN_NONE)
1628 strcpy(s, "none");
1629 else
1630 sprintf(s, "%d", x & VLAN_IDMASK);
1631}
1632
1633ushort string_to_VLAN(char *s)
1634{
1635 ushort id;
1636
1637 if (s == NULL)
wdenkb9711de2004-04-25 13:18:40 +00001638 return htons(VLAN_NONE);
wdenka3d991b2004-04-15 21:48:45 +00001639
1640 if (*s < '0' || *s > '9')
1641 id = VLAN_NONE;
1642 else
1643 id = (ushort)simple_strtoul(s, NULL, 10);
1644
wdenkb9711de2004-04-25 13:18:40 +00001645 return htons(id);
wdenka3d991b2004-04-15 21:48:45 +00001646}
1647
wdenk73a8b272003-06-05 19:27:42 +00001648void print_IPaddr (IPaddr_t x)
1649{
wdenka3d991b2004-04-15 21:48:45 +00001650 char tmp[16];
wdenk73a8b272003-06-05 19:27:42 +00001651
wdenka3d991b2004-04-15 21:48:45 +00001652 ip_to_string (x, tmp);
wdenk73a8b272003-06-05 19:27:42 +00001653
wdenka3d991b2004-04-15 21:48:45 +00001654 puts (tmp);
wdenk73a8b272003-06-05 19:27:42 +00001655}
1656
1657IPaddr_t getenv_IPaddr (char *var)
1658{
1659 return (string_to_ip(getenv(var)));
1660}
wdenka3d991b2004-04-15 21:48:45 +00001661
1662ushort getenv_VLAN(char *var)
1663{
1664 return (string_to_VLAN(getenv(var)));
1665}