]> git.ipfire.org Git - people/ms/u-boot.git/blob - net/net.c
* Patch by Masami Komiya, 30 Mar 2005:
[people/ms/u-boot.git] / net / net.c
1 /*
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
58 *
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
67 *
68 * SNTP:
69 *
70 * Prerequisites: - own ethernet address
71 * - own IP address
72 * We want: - network time
73 * Next step: none
74 */
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"
84 #include "nfs.h"
85 #ifdef CONFIG_STATUS_LED
86 #include <status_led.h>
87 #include <miiphy.h>
88 #endif
89 #if (CONFIG_COMMANDS & CFG_CMD_SNTP)
90 #include "sntp.h"
91 #endif
92
93 #if (CONFIG_COMMANDS & CFG_CMD_NET)
94
95 #define ARP_TIMEOUT 5 /* Seconds before trying ARP again */
96 #ifndef CONFIG_NET_RETRY_COUNT
97 # define ARP_TIMEOUT_COUNT 5 /* # of timeouts before giving up */
98 #else
99 # define ARP_TIMEOUT_COUNT (CONFIG_NET_RETRY_COUNT)
100 #endif
101
102 #if 0
103 #define ET_DEBUG
104 #endif
105
106 /** BOOTP EXTENTIONS **/
107
108 IPaddr_t NetOurSubnetMask=0; /* Our subnet mask (0=unknown) */
109 IPaddr_t NetOurGatewayIP=0; /* Our gateways IP address */
110 IPaddr_t NetOurDNSIP=0; /* Our DNS IP address */
111 #if (CONFIG_BOOTP_MASK & CONFIG_BOOTP_DNS2)
112 IPaddr_t NetOurDNS2IP=0; /* Our 2nd DNS IP address */
113 #endif
114 char NetOurNISDomain[32]={0,}; /* Our NIS domain */
115 char NetOurHostName[32]={0,}; /* Our hostname */
116 char NetOurRootPath[64]={0,}; /* Our bootpath */
117 ushort NetBootFileSize=0; /* Our bootfile size in blocks */
118
119 /** END OF BOOTP EXTENTIONS **/
120
121 ulong NetBootFileXferSize; /* The actual transferred size of the bootfile (in bytes) */
122 uchar NetOurEther[6]; /* Our ethernet address */
123 uchar NetServerEther[6] = /* Boot server enet address */
124 { 0, 0, 0, 0, 0, 0 };
125 IPaddr_t NetOurIP; /* Our IP addr (0 = unknown) */
126 IPaddr_t NetServerIP; /* Our IP addr (0 = unknown) */
127 volatile uchar *NetRxPkt; /* Current receive packet */
128 int NetRxPktLen; /* Current rx packet length */
129 unsigned NetIPID; /* IP packet ID */
130 uchar NetBcastAddr[6] = /* Ethernet bcast address */
131 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
132 uchar NetEtherNullAddr[6] =
133 { 0, 0, 0, 0, 0, 0 };
134 #if (CONFIG_COMMANDS & CFG_CMD_CDP)
135 uchar NetCDPAddr[6] = /* Ethernet bcast address */
136 { 0x01, 0x00, 0x0c, 0xcc, 0xcc, 0xcc };
137 #endif
138 int NetState; /* Network loop state */
139 #ifdef CONFIG_NET_MULTI
140 int NetRestartWrap = 0; /* Tried all network devices */
141 static int NetRestarted = 0; /* Network loop restarted */
142 static int NetDevExists = 0; /* At least one device configured */
143 #endif
144
145 /* XXX in both little & big endian machines 0xFFFF == ntohs(-1) */
146 ushort NetOurVLAN = 0xFFFF; /* default is without VLAN */
147 ushort NetOurNativeVLAN = 0xFFFF; /* ditto */
148
149 char BootFile[128]; /* Boot File name */
150
151 #if (CONFIG_COMMANDS & CFG_CMD_PING)
152 IPaddr_t NetPingIP; /* the ip address to ping */
153
154 static void PingStart(void);
155 #endif
156
157 #if (CONFIG_COMMANDS & CFG_CMD_CDP)
158 static void CDPStart(void);
159 #endif
160
161 #if (CONFIG_COMMANDS & CFG_CMD_SNTP)
162 IPaddr_t NetNtpServerIP; /* NTP server IP address */
163 int NetTimeOffset=0; /* offset time from UTC */
164 #endif
165
166 #ifdef CONFIG_NETCONSOLE
167 void NcStart(void);
168 int nc_input_packet(uchar *pkt, unsigned dest, unsigned src, unsigned len);
169 #endif
170
171 volatile uchar PktBuf[(PKTBUFSRX+1) * PKTSIZE_ALIGN + PKTALIGN];
172
173 volatile uchar *NetRxPackets[PKTBUFSRX]; /* Receive packets */
174
175 static rxhand_f *packetHandler; /* Current RX packet handler */
176 static thand_f *timeHandler; /* Current timeout handler */
177 static ulong timeStart; /* Time base value */
178 static ulong timeDelta; /* Current timeout value */
179 volatile uchar *NetTxPacket = 0; /* THE transmit packet */
180
181 static int net_check_prereq (proto_t protocol);
182
183 /**********************************************************************/
184
185 IPaddr_t NetArpWaitPacketIP;
186 IPaddr_t NetArpWaitReplyIP;
187 uchar *NetArpWaitPacketMAC; /* MAC address of waiting packet's destination */
188 uchar *NetArpWaitTxPacket; /* THE transmit packet */
189 int NetArpWaitTxPacketSize;
190 uchar NetArpWaitPacketBuf[PKTSIZE_ALIGN + PKTALIGN];
191 ulong NetArpWaitTimerStart;
192 int NetArpWaitTry;
193
194 void ArpRequest (void)
195 {
196 int i;
197 volatile uchar *pkt;
198 ARP_t *arp;
199
200 #ifdef ET_DEBUG
201 printf ("ARP broadcast %d\n", NetArpWaitTry);
202 #endif
203 pkt = NetTxPacket;
204
205 pkt += NetSetEther (pkt, NetBcastAddr, PROT_ARP);
206
207 arp = (ARP_t *) pkt;
208
209 arp->ar_hrd = htons (ARP_ETHER);
210 arp->ar_pro = htons (PROT_IP);
211 arp->ar_hln = 6;
212 arp->ar_pln = 4;
213 arp->ar_op = htons (ARPOP_REQUEST);
214
215 memcpy (&arp->ar_data[0], NetOurEther, 6); /* source ET addr */
216 NetWriteIP ((uchar *) & arp->ar_data[6], NetOurIP); /* source IP addr */
217 for (i = 10; i < 16; ++i) {
218 arp->ar_data[i] = 0; /* dest ET addr = 0 */
219 }
220
221 if ((NetArpWaitPacketIP & NetOurSubnetMask) !=
222 (NetOurIP & NetOurSubnetMask)) {
223 if (NetOurGatewayIP == 0) {
224 puts ("## Warning: gatewayip needed but not set\n");
225 }
226 NetArpWaitReplyIP = NetOurGatewayIP;
227 } else {
228 NetArpWaitReplyIP = NetArpWaitPacketIP;
229 }
230
231 NetWriteIP ((uchar *) & arp->ar_data[16], NetArpWaitReplyIP);
232 (void) eth_send (NetTxPacket, (pkt - NetTxPacket) + ARP_HDR_SIZE);
233 }
234
235 void ArpTimeoutCheck(void)
236 {
237 ulong t;
238
239 if (!NetArpWaitPacketIP)
240 return;
241
242 t = get_timer(0);
243
244 /* check for arp timeout */
245 if ((t - NetArpWaitTimerStart) > ARP_TIMEOUT * CFG_HZ) {
246 NetArpWaitTry++;
247
248 if (NetArpWaitTry >= ARP_TIMEOUT_COUNT) {
249 puts ("\nARP Retry count exceeded; starting again\n");
250 NetArpWaitTry = 0;
251 NetStartAgain();
252 } else {
253 NetArpWaitTimerStart = t;
254 ArpRequest();
255 }
256 }
257 }
258
259 /**********************************************************************/
260 /*
261 * Main network processing loop.
262 */
263
264 int
265 NetLoop(proto_t protocol)
266 {
267 DECLARE_GLOBAL_DATA_PTR;
268
269 bd_t *bd = gd->bd;
270
271 #ifdef CONFIG_NET_MULTI
272 NetRestarted = 0;
273 NetDevExists = 0;
274 #endif
275
276 /* XXX problem with bss workaround */
277 NetArpWaitPacketMAC = NULL;
278 NetArpWaitTxPacket = NULL;
279 NetArpWaitPacketIP = 0;
280 NetArpWaitReplyIP = 0;
281 NetArpWaitTxPacket = NULL;
282 NetTxPacket = NULL;
283
284 if (!NetTxPacket) {
285 int i;
286 /*
287 * Setup packet buffers, aligned correctly.
288 */
289 NetTxPacket = &PktBuf[0] + (PKTALIGN - 1);
290 NetTxPacket -= (ulong)NetTxPacket % PKTALIGN;
291 for (i = 0; i < PKTBUFSRX; i++) {
292 NetRxPackets[i] = NetTxPacket + (i+1)*PKTSIZE_ALIGN;
293 }
294 }
295
296 if (!NetArpWaitTxPacket) {
297 NetArpWaitTxPacket = &NetArpWaitPacketBuf[0] + (PKTALIGN - 1);
298 NetArpWaitTxPacket -= (ulong)NetArpWaitTxPacket % PKTALIGN;
299 NetArpWaitTxPacketSize = 0;
300 }
301
302 eth_halt();
303 #ifdef CONFIG_NET_MULTI
304 eth_set_current();
305 #endif
306 if (eth_init(bd) < 0)
307 return(-1);
308
309 restart:
310 #ifdef CONFIG_NET_MULTI
311 memcpy (NetOurEther, eth_get_dev()->enetaddr, 6);
312 #else
313 memcpy (NetOurEther, bd->bi_enetaddr, 6);
314 #endif
315
316 NetState = NETLOOP_CONTINUE;
317
318 /*
319 * Start the ball rolling with the given start function. From
320 * here on, this code is a state machine driven by received
321 * packets and timer events.
322 */
323
324 switch (protocol) {
325 #if (CONFIG_COMMANDS & CFG_CMD_NFS)
326 case NFS:
327 #endif
328 #if (CONFIG_COMMANDS & CFG_CMD_PING)
329 case PING:
330 #endif
331 #if (CONFIG_COMMANDS & CFG_CMD_SNTP)
332 case SNTP:
333 #endif
334 case NETCONS:
335 case TFTP:
336 NetCopyIP(&NetOurIP, &bd->bi_ip_addr);
337 NetOurGatewayIP = getenv_IPaddr ("gatewayip");
338 NetOurSubnetMask= getenv_IPaddr ("netmask");
339 NetOurVLAN = getenv_VLAN("vlan");
340 NetOurNativeVLAN = getenv_VLAN("nvlan");
341
342 switch (protocol) {
343 #if (CONFIG_COMMANDS & CFG_CMD_NFS)
344 case NFS:
345 #endif
346 case NETCONS:
347 case TFTP:
348 NetServerIP = getenv_IPaddr ("serverip");
349 break;
350 #if (CONFIG_COMMANDS & CFG_CMD_PING)
351 case PING:
352 /* nothing */
353 break;
354 #endif
355 #if (CONFIG_COMMANDS & CFG_CMD_SNTP)
356 case SNTP:
357 /* nothing */
358 break;
359 #endif
360 default:
361 break;
362 }
363
364 break;
365 case BOOTP:
366 case RARP:
367 /*
368 * initialize our IP addr to 0 in order to accept ANY
369 * IP addr assigned to us by the BOOTP / RARP server
370 */
371 NetOurIP = 0;
372 NetServerIP = getenv_IPaddr ("serverip");
373 NetOurVLAN = getenv_VLAN("vlan"); /* VLANs must be read */
374 NetOurNativeVLAN = getenv_VLAN("nvlan");
375 case CDP:
376 NetOurVLAN = getenv_VLAN("vlan"); /* VLANs must be read */
377 NetOurNativeVLAN = getenv_VLAN("nvlan");
378 break;
379 default:
380 break;
381 }
382
383 switch (net_check_prereq (protocol)) {
384 case 1:
385 /* network not configured */
386 return (-1);
387
388 #ifdef CONFIG_NET_MULTI
389 case 2:
390 /* network device not configured */
391 break;
392 #endif /* CONFIG_NET_MULTI */
393
394 case 0:
395 #ifdef CONFIG_NET_MULTI
396 NetDevExists = 1;
397 #endif
398 switch (protocol) {
399 case TFTP:
400 /* always use ARP to get server ethernet address */
401 TftpStart();
402 break;
403
404 #if (CONFIG_COMMANDS & CFG_CMD_DHCP)
405 case DHCP:
406 /* Start with a clean slate... */
407 BootpTry = 0;
408 NetOurIP = 0;
409 NetServerIP = getenv_IPaddr ("serverip");
410 DhcpRequest(); /* Basically same as BOOTP */
411 break;
412 #endif /* CFG_CMD_DHCP */
413
414 case BOOTP:
415 BootpTry = 0;
416 BootpRequest ();
417 break;
418
419 case RARP:
420 RarpTry = 0;
421 RarpRequest ();
422 break;
423 #if (CONFIG_COMMANDS & CFG_CMD_PING)
424 case PING:
425 PingStart();
426 break;
427 #endif
428 #if (CONFIG_COMMANDS & CFG_CMD_NFS)
429 case NFS:
430 NfsStart();
431 break;
432 #endif
433 #if (CONFIG_COMMANDS & CFG_CMD_CDP)
434 case CDP:
435 CDPStart();
436 break;
437 #endif
438 #ifdef CONFIG_NETCONSOLE
439 case NETCONS:
440 NcStart();
441 break;
442 #endif
443 #if (CONFIG_COMMANDS & CFG_CMD_SNTP)
444 case SNTP:
445 SntpStart();
446 break;
447 #endif
448 default:
449 break;
450 }
451
452 NetBootFileXferSize = 0;
453 break;
454 }
455
456 #if defined(CONFIG_MII) || (CONFIG_COMMANDS & CFG_CMD_MII)
457 #if defined(CFG_FAULT_ECHO_LINK_DOWN) && defined(CONFIG_STATUS_LED) && defined(STATUS_LED_RED)
458 /*
459 * Echo the inverted link state to the fault LED.
460 */
461 if(miiphy_link(CFG_FAULT_MII_ADDR)) {
462 status_led_set (STATUS_LED_RED, STATUS_LED_OFF);
463 } else {
464 status_led_set (STATUS_LED_RED, STATUS_LED_ON);
465 }
466 #endif /* CFG_FAULT_ECHO_LINK_DOWN, ... */
467 #endif /* CONFIG_MII, ... */
468
469 /*
470 * Main packet reception loop. Loop receiving packets until
471 * someone sets `NetQuit'.
472 */
473 for (;;) {
474 WATCHDOG_RESET();
475 #ifdef CONFIG_SHOW_ACTIVITY
476 {
477 extern void show_activity(int arg);
478 show_activity(1);
479 }
480 #endif
481 /*
482 * Check the ethernet for a new packet. The ethernet
483 * receive routine will process it.
484 */
485 eth_rx();
486
487 /*
488 * Abort if ctrl-c was pressed.
489 */
490 if (ctrlc()) {
491 eth_halt();
492 puts ("\nAbort\n");
493 return (-1);
494 }
495
496 ArpTimeoutCheck();
497
498 /*
499 * Check for a timeout, and run the timeout handler
500 * if we have one.
501 */
502 if (timeHandler && ((get_timer(0) - timeStart) > timeDelta)) {
503 thand_f *x;
504
505 #if defined(CONFIG_MII) || (CONFIG_COMMANDS & CFG_CMD_MII)
506 #if defined(CFG_FAULT_ECHO_LINK_DOWN) && defined(CONFIG_STATUS_LED) && defined(STATUS_LED_RED)
507 /*
508 * Echo the inverted link state to the fault LED.
509 */
510 if(miiphy_link(CFG_FAULT_MII_ADDR)) {
511 status_led_set (STATUS_LED_RED, STATUS_LED_OFF);
512 } else {
513 status_led_set (STATUS_LED_RED, STATUS_LED_ON);
514 }
515 #endif /* CFG_FAULT_ECHO_LINK_DOWN, ... */
516 #endif /* CONFIG_MII, ... */
517 x = timeHandler;
518 timeHandler = (thand_f *)0;
519 (*x)();
520 }
521
522
523 switch (NetState) {
524
525 case NETLOOP_RESTART:
526 #ifdef CONFIG_NET_MULTI
527 NetRestarted = 1;
528 #endif
529 goto restart;
530
531 case NETLOOP_SUCCESS:
532 if (NetBootFileXferSize > 0) {
533 char buf[10];
534 printf("Bytes transferred = %ld (%lx hex)\n",
535 NetBootFileXferSize,
536 NetBootFileXferSize);
537 sprintf(buf, "%lx", NetBootFileXferSize);
538 setenv("filesize", buf);
539
540 sprintf(buf, "%lX", (unsigned long)load_addr);
541 setenv("fileaddr", buf);
542 }
543 eth_halt();
544 return NetBootFileXferSize;
545
546 case NETLOOP_FAIL:
547 return (-1);
548 }
549 }
550 }
551
552 /**********************************************************************/
553
554 static void
555 startAgainTimeout(void)
556 {
557 NetState = NETLOOP_RESTART;
558 }
559
560 static void
561 startAgainHandler(uchar * pkt, unsigned dest, unsigned src, unsigned len)
562 {
563 /* Totally ignore the packet */
564 }
565
566 void NetStartAgain (void)
567 {
568 #ifdef CONFIG_NET_MULTI
569 DECLARE_GLOBAL_DATA_PTR;
570 #endif
571 char *nretry;
572 int noretry = 0, once = 0;
573
574 if ((nretry = getenv ("netretry")) != NULL) {
575 noretry = (strcmp (nretry, "no") == 0);
576 once = (strcmp (nretry, "once") == 0);
577 }
578 if (noretry) {
579 eth_halt ();
580 NetState = NETLOOP_FAIL;
581 return;
582 }
583 #ifndef CONFIG_NET_MULTI
584 NetSetTimeout (10 * CFG_HZ, startAgainTimeout);
585 NetSetHandler (startAgainHandler);
586 #else /* !CONFIG_NET_MULTI*/
587 eth_halt ();
588 eth_try_another (!NetRestarted);
589 eth_init (gd->bd);
590 if (NetRestartWrap) {
591 NetRestartWrap = 0;
592 if (NetDevExists && !once) {
593 NetSetTimeout (10 * CFG_HZ, startAgainTimeout);
594 NetSetHandler (startAgainHandler);
595 } else {
596 NetState = NETLOOP_FAIL;
597 }
598 } else {
599 NetState = NETLOOP_RESTART;
600 }
601 #endif /* CONFIG_NET_MULTI */
602 }
603
604 /**********************************************************************/
605 /*
606 * Miscelaneous bits.
607 */
608
609 void
610 NetSetHandler(rxhand_f * f)
611 {
612 packetHandler = f;
613 }
614
615
616 void
617 NetSetTimeout(ulong iv, thand_f * f)
618 {
619 if (iv == 0) {
620 timeHandler = (thand_f *)0;
621 } else {
622 timeHandler = f;
623 timeStart = get_timer(0);
624 timeDelta = iv;
625 }
626 }
627
628
629 void
630 NetSendPacket(volatile uchar * pkt, int len)
631 {
632 (void) eth_send(pkt, len);
633 }
634
635 int
636 NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport, int sport, int len)
637 {
638 uchar *pkt;
639
640 /* convert to new style broadcast */
641 if (dest == 0)
642 dest = 0xFFFFFFFF;
643
644 /* if broadcast, make the ether address a broadcast and don't do ARP */
645 if (dest == 0xFFFFFFFF)
646 ether = NetBcastAddr;
647
648 /* if MAC address was not discovered yet, save the packet and do an ARP request */
649 if (memcmp(ether, NetEtherNullAddr, 6) == 0) {
650
651 #ifdef ET_DEBUG
652 printf("sending ARP for %08lx\n", dest);
653 #endif
654 NetArpWaitPacketIP = dest;
655 NetArpWaitPacketMAC = ether;
656
657 pkt = NetArpWaitTxPacket;
658 pkt += NetSetEther (pkt, NetArpWaitPacketMAC, PROT_IP);
659
660 NetSetIP (pkt, dest, dport, sport, len);
661 memcpy(pkt + IP_HDR_SIZE, (uchar *)NetTxPacket + (pkt - (uchar *)NetArpWaitTxPacket) + IP_HDR_SIZE, len);
662
663 /* size of the waiting packet */
664 NetArpWaitTxPacketSize = (pkt - NetArpWaitTxPacket) + IP_HDR_SIZE + len;
665
666 /* and do the ARP request */
667 NetArpWaitTry = 1;
668 NetArpWaitTimerStart = get_timer(0);
669 ArpRequest();
670 return 1; /* waiting */
671 }
672
673 #ifdef ET_DEBUG
674 printf("sending UDP to %08lx/%02x:%02x:%02x:%02x:%02x:%02x\n",
675 dest, ether[0], ether[1], ether[2], ether[3], ether[4], ether[5]);
676 #endif
677
678 pkt = (uchar *)NetTxPacket;
679 pkt += NetSetEther (pkt, ether, PROT_IP);
680 NetSetIP (pkt, dest, dport, sport, len);
681 (void) eth_send(NetTxPacket, (pkt - NetTxPacket) + IP_HDR_SIZE + len);
682
683 return 0; /* transmitted */
684 }
685
686 #if (CONFIG_COMMANDS & CFG_CMD_PING)
687 static ushort PingSeqNo;
688
689 int PingSend(void)
690 {
691 static uchar mac[6];
692 volatile IP_t *ip;
693 volatile ushort *s;
694 uchar *pkt;
695
696 /* XXX always send arp request */
697
698 memcpy(mac, NetEtherNullAddr, 6);
699
700 #ifdef ET_DEBUG
701 printf("sending ARP for %08lx\n", NetPingIP);
702 #endif
703
704 NetArpWaitPacketIP = NetPingIP;
705 NetArpWaitPacketMAC = mac;
706
707 pkt = NetArpWaitTxPacket;
708 pkt += NetSetEther(pkt, mac, PROT_IP);
709
710 ip = (volatile IP_t *)pkt;
711
712 /*
713 * Construct an IP and ICMP header. (need to set no fragment bit - XXX)
714 */
715 ip->ip_hl_v = 0x45; /* IP_HDR_SIZE / 4 (not including UDP) */
716 ip->ip_tos = 0;
717 ip->ip_len = htons(IP_HDR_SIZE_NO_UDP + 8);
718 ip->ip_id = htons(NetIPID++);
719 ip->ip_off = htons(0x4000); /* No fragmentation */
720 ip->ip_ttl = 255;
721 ip->ip_p = 0x01; /* ICMP */
722 ip->ip_sum = 0;
723 NetCopyIP((void*)&ip->ip_src, &NetOurIP); /* already in network byte order */
724 NetCopyIP((void*)&ip->ip_dst, &NetPingIP); /* - "" - */
725 ip->ip_sum = ~NetCksum((uchar *)ip, IP_HDR_SIZE_NO_UDP / 2);
726
727 s = &ip->udp_src; /* XXX ICMP starts here */
728 s[0] = htons(0x0800); /* echo-request, code */
729 s[1] = 0; /* checksum */
730 s[2] = 0; /* identifier */
731 s[3] = htons(PingSeqNo++); /* sequence number */
732 s[1] = ~NetCksum((uchar *)s, 8/2);
733
734 /* size of the waiting packet */
735 NetArpWaitTxPacketSize = (pkt - NetArpWaitTxPacket) + IP_HDR_SIZE_NO_UDP + 8;
736
737 /* and do the ARP request */
738 NetArpWaitTry = 1;
739 NetArpWaitTimerStart = get_timer(0);
740 ArpRequest();
741 return 1; /* waiting */
742 }
743
744 static void
745 PingTimeout (void)
746 {
747 eth_halt();
748 NetState = NETLOOP_FAIL; /* we did not get the reply */
749 }
750
751 static void
752 PingHandler (uchar * pkt, unsigned dest, unsigned src, unsigned len)
753 {
754 IPaddr_t tmp;
755 volatile IP_t *ip = (volatile IP_t *)pkt;
756
757 tmp = NetReadIP((void *)&ip->ip_src);
758 if (tmp != NetPingIP)
759 return;
760
761 NetState = NETLOOP_SUCCESS;
762 }
763
764 static void PingStart(void)
765 {
766 #if defined(CONFIG_NET_MULTI)
767 printf ("Using %s device\n", eth_get_name());
768 #endif /* CONFIG_NET_MULTI */
769 NetSetTimeout (10 * CFG_HZ, PingTimeout);
770 NetSetHandler (PingHandler);
771
772 PingSend();
773 }
774 #endif /* CFG_CMD_PING */
775
776 #if (CONFIG_COMMANDS & CFG_CMD_CDP)
777
778 #define CDP_DEVICE_ID_TLV 0x0001
779 #define CDP_ADDRESS_TLV 0x0002
780 #define CDP_PORT_ID_TLV 0x0003
781 #define CDP_CAPABILITIES_TLV 0x0004
782 #define CDP_VERSION_TLV 0x0005
783 #define CDP_PLATFORM_TLV 0x0006
784 #define CDP_NATIVE_VLAN_TLV 0x000a
785 #define CDP_APPLIANCE_VLAN_TLV 0x000e
786 #define CDP_TRIGGER_TLV 0x000f
787 #define CDP_POWER_CONSUMPTION_TLV 0x0010
788 #define CDP_SYSNAME_TLV 0x0014
789 #define CDP_SYSOBJECT_TLV 0x0015
790 #define CDP_MANAGEMENT_ADDRESS_TLV 0x0016
791
792 #define CDP_TIMEOUT (CFG_HZ/4) /* one packet every 250ms */
793
794 static int CDPSeq;
795 static int CDPOK;
796
797 ushort CDPNativeVLAN;
798 ushort CDPApplianceVLAN;
799
800 static const uchar CDP_SNAP_hdr[8] = { 0xAA, 0xAA, 0x03, 0x00, 0x00, 0x0C, 0x20, 0x00 };
801
802 static ushort CDP_compute_csum(const uchar *buff, ushort len)
803 {
804 ushort csum;
805 int odd;
806 ulong result = 0;
807 ushort leftover;
808
809 if (len > 0) {
810 odd = 1 & (ulong)buff;
811 if (odd) {
812 result = *buff << 8;
813 len--;
814 buff++;
815 }
816 while (len > 1) {
817 result += *((const ushort *)buff)++;
818 if (result & 0x80000000)
819 result = (result & 0xFFFF) + (result >> 16);
820 len -= 2;
821 }
822 if (len) {
823 leftover = (signed short)(*(const signed char *)buff);
824 /* * XXX CISCO SUCKS big time! (and blows too) */
825 result = (result & 0xffff0000) | ((result + leftover) & 0x0000ffff);
826 }
827 while (result >> 16)
828 result = (result & 0xFFFF) + (result >> 16);
829
830 if (odd)
831 result = ((result >> 8) & 0xff) | ((result & 0xff) << 8);
832 }
833
834 /* add up 16-bit and 17-bit words for 17+c bits */
835 result = (result & 0xffff) + (result >> 16);
836 /* add up 16-bit and 2-bit for 16+c bit */
837 result = (result & 0xffff) + (result >> 16);
838 /* add up carry.. */
839 result = (result & 0xffff) + (result >> 16);
840
841 /* negate */
842 csum = ~(ushort)result;
843
844 /* run time endian detection */
845 if (csum != htons(csum)) /* little endian */
846 csum = htons(csum);
847
848 return csum;
849 }
850
851 int CDPSendTrigger(void)
852 {
853 volatile uchar *pkt;
854 volatile ushort *s;
855 volatile ushort *cp;
856 Ethernet_t *et;
857 int len;
858 ushort chksum;
859 #if defined(CONFIG_CDP_DEVICE_ID) || defined(CONFIG_CDP_PORT_ID) || \
860 defined(CONFIG_CDP_VERSION) || defined(CONFIG_CDP_PLATFORM)
861 char buf[32];
862 #endif
863
864 pkt = NetTxPacket;
865 et = (Ethernet_t *)pkt;
866
867 /* NOTE: trigger sent not on any VLAN */
868
869 /* form ethernet header */
870 memcpy(et->et_dest, NetCDPAddr, 6);
871 memcpy(et->et_src, NetOurEther, 6);
872
873 pkt += ETHER_HDR_SIZE;
874
875 /* SNAP header */
876 memcpy((uchar *)pkt, CDP_SNAP_hdr, sizeof(CDP_SNAP_hdr));
877 pkt += sizeof(CDP_SNAP_hdr);
878
879 /* CDP header */
880 *pkt++ = 0x02; /* CDP version 2 */
881 *pkt++ = 180; /* TTL */
882 s = (volatile ushort *)pkt;
883 cp = s;
884 *s++ = htons(0); /* checksum (0 for later calculation) */
885
886 /* CDP fields */
887 #ifdef CONFIG_CDP_DEVICE_ID
888 *s++ = htons(CDP_DEVICE_ID_TLV);
889 *s++ = htons(CONFIG_CDP_DEVICE_ID);
890 memset(buf, 0, sizeof(buf));
891 sprintf(buf, CONFIG_CDP_DEVICE_ID_PREFIX "%02X%02X%02X%02X%02X%02X",
892 NetOurEther[0] & 0xff, NetOurEther[1] & 0xff,
893 NetOurEther[2] & 0xff, NetOurEther[3] & 0xff,
894 NetOurEther[4] & 0xff, NetOurEther[5] & 0xff);
895 memcpy((uchar *)s, buf, 16);
896 s += 16 / 2;
897 #endif
898
899 #ifdef CONFIG_CDP_PORT_ID
900 *s++ = htons(CDP_PORT_ID_TLV);
901 memset(buf, 0, sizeof(buf));
902 sprintf(buf, CONFIG_CDP_PORT_ID, eth_get_dev_index());
903 len = strlen(buf);
904 if (len & 1) /* make it even */
905 len++;
906 *s++ = htons(len + 4);
907 memcpy((uchar *)s, buf, len);
908 s += len / 2;
909 #endif
910
911 #ifdef CONFIG_CDP_CAPABILITIES
912 *s++ = htons(CDP_CAPABILITIES_TLV);
913 *s++ = htons(8);
914 *(ulong *)s = htonl(CONFIG_CDP_CAPABILITIES);
915 s += 2;
916 #endif
917
918 #ifdef CONFIG_CDP_VERSION
919 *s++ = htons(CDP_VERSION_TLV);
920 memset(buf, 0, sizeof(buf));
921 strcpy(buf, CONFIG_CDP_VERSION);
922 len = strlen(buf);
923 if (len & 1) /* make it even */
924 len++;
925 *s++ = htons(len + 4);
926 memcpy((uchar *)s, buf, len);
927 s += len / 2;
928 #endif
929
930 #ifdef CONFIG_CDP_PLATFORM
931 *s++ = htons(CDP_PLATFORM_TLV);
932 memset(buf, 0, sizeof(buf));
933 strcpy(buf, CONFIG_CDP_PLATFORM);
934 len = strlen(buf);
935 if (len & 1) /* make it even */
936 len++;
937 *s++ = htons(len + 4);
938 memcpy((uchar *)s, buf, len);
939 s += len / 2;
940 #endif
941
942 #ifdef CONFIG_CDP_TRIGGER
943 *s++ = htons(CDP_TRIGGER_TLV);
944 *s++ = htons(8);
945 *(ulong *)s = htonl(CONFIG_CDP_TRIGGER);
946 s += 2;
947 #endif
948
949 #ifdef CONFIG_CDP_POWER_CONSUMPTION
950 *s++ = htons(CDP_POWER_CONSUMPTION_TLV);
951 *s++ = htons(6);
952 *s++ = htons(CONFIG_CDP_POWER_CONSUMPTION);
953 #endif
954
955 /* length of ethernet packet */
956 len = (uchar *)s - ((uchar *)NetTxPacket + ETHER_HDR_SIZE);
957 et->et_protlen = htons(len);
958
959 len = ETHER_HDR_SIZE + sizeof(CDP_SNAP_hdr);
960 chksum = CDP_compute_csum((uchar *)NetTxPacket + len, (uchar *)s - (NetTxPacket + len));
961 if (chksum == 0)
962 chksum = 0xFFFF;
963 *cp = htons(chksum);
964
965 (void) eth_send(NetTxPacket, (uchar *)s - NetTxPacket);
966 return 0;
967 }
968
969 static void
970 CDPTimeout (void)
971 {
972 CDPSeq++;
973
974 if (CDPSeq < 3) {
975 NetSetTimeout (CDP_TIMEOUT, CDPTimeout);
976 CDPSendTrigger();
977 return;
978 }
979
980 /* if not OK try again */
981 if (!CDPOK)
982 NetStartAgain();
983 else
984 NetState = NETLOOP_SUCCESS;
985 }
986
987 static void
988 CDPDummyHandler (uchar * pkt, unsigned dest, unsigned src, unsigned len)
989 {
990 /* nothing */
991 }
992
993 static void
994 CDPHandler(const uchar * pkt, unsigned len)
995 {
996 const uchar *t;
997 const ushort *ss;
998 ushort type, tlen;
999 uchar applid;
1000 ushort vlan, nvlan;
1001
1002 /* minimum size? */
1003 if (len < sizeof(CDP_SNAP_hdr) + 4)
1004 goto pkt_short;
1005
1006 /* check for valid CDP SNAP header */
1007 if (memcmp(pkt, CDP_SNAP_hdr, sizeof(CDP_SNAP_hdr)) != 0)
1008 return;
1009
1010 pkt += sizeof(CDP_SNAP_hdr);
1011 len -= sizeof(CDP_SNAP_hdr);
1012
1013 /* Version of CDP protocol must be >= 2 and TTL != 0 */
1014 if (pkt[0] < 0x02 || pkt[1] == 0)
1015 return;
1016
1017 /* if version is greater than 0x02 maybe we'll have a problem; output a warning */
1018 if (pkt[0] != 0x02)
1019 printf("** WARNING: CDP packet received with a protocol version %d > 2\n",
1020 pkt[0] & 0xff);
1021
1022 if (CDP_compute_csum(pkt, len) != 0)
1023 return;
1024
1025 pkt += 4;
1026 len -= 4;
1027
1028 vlan = htons(-1);
1029 nvlan = htons(-1);
1030 while (len > 0) {
1031 if (len < 4)
1032 goto pkt_short;
1033
1034 ss = (const ushort *)pkt;
1035 type = ntohs(ss[0]);
1036 tlen = ntohs(ss[1]);
1037 if (tlen > len) {
1038 goto pkt_short;
1039 }
1040
1041 pkt += tlen;
1042 len -= tlen;
1043
1044 ss += 2; /* point ss to the data of the TLV */
1045 tlen -= 4;
1046
1047 switch (type) {
1048 case CDP_DEVICE_ID_TLV:
1049 break;
1050 case CDP_ADDRESS_TLV:
1051 break;
1052 case CDP_PORT_ID_TLV:
1053 break;
1054 case CDP_CAPABILITIES_TLV:
1055 break;
1056 case CDP_VERSION_TLV:
1057 break;
1058 case CDP_PLATFORM_TLV:
1059 break;
1060 case CDP_NATIVE_VLAN_TLV:
1061 nvlan = *ss;
1062 break;
1063 case CDP_APPLIANCE_VLAN_TLV:
1064 t = (const uchar *)ss;
1065 while (tlen > 0) {
1066 if (tlen < 3)
1067 goto pkt_short;
1068
1069 applid = t[0];
1070 ss = (const ushort *)(t + 1);
1071
1072 #ifdef CONFIG_CDP_APPLIANCE_VLAN_TYPE
1073 if (applid == CONFIG_CDP_APPLIANCE_VLAN_TYPE)
1074 vlan = *ss;
1075 #else
1076 vlan = ntohs(*ss); /* XXX will this work; dunno */
1077 #endif
1078 t += 3; tlen -= 3;
1079 }
1080 break;
1081 case CDP_TRIGGER_TLV:
1082 break;
1083 case CDP_POWER_CONSUMPTION_TLV:
1084 break;
1085 case CDP_SYSNAME_TLV:
1086 break;
1087 case CDP_SYSOBJECT_TLV:
1088 break;
1089 case CDP_MANAGEMENT_ADDRESS_TLV:
1090 break;
1091 }
1092 }
1093
1094 CDPApplianceVLAN = vlan;
1095 CDPNativeVLAN = nvlan;
1096
1097 CDPOK = 1;
1098 return;
1099
1100 pkt_short:
1101 printf("** CDP packet is too short\n");
1102 return;
1103 }
1104
1105 static void CDPStart(void)
1106 {
1107 #if defined(CONFIG_NET_MULTI)
1108 printf ("Using %s device\n", eth_get_name());
1109 #endif
1110 CDPSeq = 0;
1111 CDPOK = 0;
1112
1113 CDPNativeVLAN = htons(-1);
1114 CDPApplianceVLAN = htons(-1);
1115
1116 NetSetTimeout (CDP_TIMEOUT, CDPTimeout);
1117 NetSetHandler (CDPDummyHandler);
1118
1119 CDPSendTrigger();
1120 }
1121 #endif /* CFG_CMD_CDP */
1122
1123
1124 void
1125 NetReceive(volatile uchar * inpkt, int len)
1126 {
1127 Ethernet_t *et;
1128 IP_t *ip;
1129 ARP_t *arp;
1130 IPaddr_t tmp;
1131 int x;
1132 uchar *pkt;
1133 #if (CONFIG_COMMANDS & CFG_CMD_CDP)
1134 int iscdp;
1135 #endif
1136 ushort cti = 0, vlanid = VLAN_NONE, myvlanid, mynvlanid;
1137
1138 #ifdef ET_DEBUG
1139 printf("packet received\n");
1140 #endif
1141
1142 NetRxPkt = inpkt;
1143 NetRxPktLen = len;
1144 et = (Ethernet_t *)inpkt;
1145
1146 /* too small packet? */
1147 if (len < ETHER_HDR_SIZE)
1148 return;
1149
1150 #if (CONFIG_COMMANDS & CFG_CMD_CDP)
1151 /* keep track if packet is CDP */
1152 iscdp = memcmp(et->et_dest, NetCDPAddr, 6) == 0;
1153 #endif
1154
1155 myvlanid = ntohs(NetOurVLAN);
1156 if (myvlanid == (ushort)-1)
1157 myvlanid = VLAN_NONE;
1158 mynvlanid = ntohs(NetOurNativeVLAN);
1159 if (mynvlanid == (ushort)-1)
1160 mynvlanid = VLAN_NONE;
1161
1162 x = ntohs(et->et_protlen);
1163
1164 #ifdef ET_DEBUG
1165 printf("packet received\n");
1166 #endif
1167
1168 if (x < 1514) {
1169 /*
1170 * Got a 802 packet. Check the other protocol field.
1171 */
1172 x = ntohs(et->et_prot);
1173
1174 ip = (IP_t *)(inpkt + E802_HDR_SIZE);
1175 len -= E802_HDR_SIZE;
1176
1177 } else if (x != PROT_VLAN) { /* normal packet */
1178 ip = (IP_t *)(inpkt + ETHER_HDR_SIZE);
1179 len -= ETHER_HDR_SIZE;
1180
1181 } else { /* VLAN packet */
1182 VLAN_Ethernet_t *vet = (VLAN_Ethernet_t *)et;
1183
1184 #ifdef ET_DEBUG
1185 printf("VLAN packet received\n");
1186 #endif
1187 /* too small packet? */
1188 if (len < VLAN_ETHER_HDR_SIZE)
1189 return;
1190
1191 /* if no VLAN active */
1192 if ((ntohs(NetOurVLAN) & VLAN_IDMASK) == VLAN_NONE
1193 #if (CONFIG_COMMANDS & CFG_CMD_CDP)
1194 && iscdp == 0
1195 #endif
1196 )
1197 return;
1198
1199 cti = ntohs(vet->vet_tag);
1200 vlanid = cti & VLAN_IDMASK;
1201 x = ntohs(vet->vet_type);
1202
1203 ip = (IP_t *)(inpkt + VLAN_ETHER_HDR_SIZE);
1204 len -= VLAN_ETHER_HDR_SIZE;
1205 }
1206
1207 #ifdef ET_DEBUG
1208 printf("Receive from protocol 0x%x\n", x);
1209 #endif
1210
1211 #if (CONFIG_COMMANDS & CFG_CMD_CDP)
1212 if (iscdp) {
1213 CDPHandler((uchar *)ip, len);
1214 return;
1215 }
1216 #endif
1217
1218 if ((myvlanid & VLAN_IDMASK) != VLAN_NONE) {
1219 if (vlanid == VLAN_NONE)
1220 vlanid = (mynvlanid & VLAN_IDMASK);
1221 /* not matched? */
1222 if (vlanid != (myvlanid & VLAN_IDMASK))
1223 return;
1224 }
1225
1226 switch (x) {
1227
1228 case PROT_ARP:
1229 /*
1230 * We have to deal with two types of ARP packets:
1231 * - REQUEST packets will be answered by sending our
1232 * IP address - if we know it.
1233 * - REPLY packates are expected only after we asked
1234 * for the TFTP server's or the gateway's ethernet
1235 * address; so if we receive such a packet, we set
1236 * the server ethernet address
1237 */
1238 #ifdef ET_DEBUG
1239 puts ("Got ARP\n");
1240 #endif
1241 arp = (ARP_t *)ip;
1242 if (len < ARP_HDR_SIZE) {
1243 printf("bad length %d < %d\n", len, ARP_HDR_SIZE);
1244 return;
1245 }
1246 if (ntohs(arp->ar_hrd) != ARP_ETHER) {
1247 return;
1248 }
1249 if (ntohs(arp->ar_pro) != PROT_IP) {
1250 return;
1251 }
1252 if (arp->ar_hln != 6) {
1253 return;
1254 }
1255 if (arp->ar_pln != 4) {
1256 return;
1257 }
1258
1259 if (NetOurIP == 0) {
1260 return;
1261 }
1262
1263 if (NetReadIP(&arp->ar_data[16]) != NetOurIP) {
1264 return;
1265 }
1266
1267 switch (ntohs(arp->ar_op)) {
1268 case ARPOP_REQUEST: /* reply with our IP address */
1269 #ifdef ET_DEBUG
1270 puts ("Got ARP REQUEST, return our IP\n");
1271 #endif
1272 pkt = (uchar *)et;
1273 pkt += NetSetEther(pkt, et->et_src, PROT_ARP);
1274 arp->ar_op = htons(ARPOP_REPLY);
1275 memcpy (&arp->ar_data[10], &arp->ar_data[0], 6);
1276 NetCopyIP(&arp->ar_data[16], &arp->ar_data[6]);
1277 memcpy (&arp->ar_data[ 0], NetOurEther, 6);
1278 NetCopyIP(&arp->ar_data[ 6], &NetOurIP);
1279 (void) eth_send((uchar *)et, (pkt - (uchar *)et) + ARP_HDR_SIZE);
1280 return;
1281
1282 case ARPOP_REPLY: /* arp reply */
1283 /* are we waiting for a reply */
1284 if (!NetArpWaitPacketIP || !NetArpWaitPacketMAC)
1285 break;
1286 #ifdef ET_DEBUG
1287 printf("Got ARP REPLY, set server/gtwy eth addr (%02x:%02x:%02x:%02x:%02x:%02x)\n",
1288 arp->ar_data[0], arp->ar_data[1],
1289 arp->ar_data[2], arp->ar_data[3],
1290 arp->ar_data[4], arp->ar_data[5]);
1291 #endif
1292
1293 tmp = NetReadIP(&arp->ar_data[6]);
1294
1295 /* matched waiting packet's address */
1296 if (tmp == NetArpWaitReplyIP) {
1297 #ifdef ET_DEBUG
1298 puts ("Got it\n");
1299 #endif
1300 /* save address for later use */
1301 memcpy(NetArpWaitPacketMAC, &arp->ar_data[0], 6);
1302
1303 #ifdef CONFIG_NETCONSOLE
1304 (*packetHandler)(0,0,0,0);
1305 #endif
1306 /* modify header, and transmit it */
1307 memcpy(((Ethernet_t *)NetArpWaitTxPacket)->et_dest, NetArpWaitPacketMAC, 6);
1308 (void) eth_send(NetArpWaitTxPacket, NetArpWaitTxPacketSize);
1309
1310 /* no arp request pending now */
1311 NetArpWaitPacketIP = 0;
1312 NetArpWaitTxPacketSize = 0;
1313 NetArpWaitPacketMAC = NULL;
1314
1315 }
1316 return;
1317 default:
1318 #ifdef ET_DEBUG
1319 printf("Unexpected ARP opcode 0x%x\n", ntohs(arp->ar_op));
1320 #endif
1321 return;
1322 }
1323 break;
1324
1325 case PROT_RARP:
1326 #ifdef ET_DEBUG
1327 puts ("Got RARP\n");
1328 #endif
1329 arp = (ARP_t *)ip;
1330 if (len < ARP_HDR_SIZE) {
1331 printf("bad length %d < %d\n", len, ARP_HDR_SIZE);
1332 return;
1333 }
1334
1335 if ((ntohs(arp->ar_op) != RARPOP_REPLY) ||
1336 (ntohs(arp->ar_hrd) != ARP_ETHER) ||
1337 (ntohs(arp->ar_pro) != PROT_IP) ||
1338 (arp->ar_hln != 6) || (arp->ar_pln != 4)) {
1339
1340 puts ("invalid RARP header\n");
1341 } else {
1342 NetCopyIP(&NetOurIP, &arp->ar_data[16]);
1343 if (NetServerIP == 0)
1344 NetCopyIP(&NetServerIP, &arp->ar_data[ 6]);
1345 memcpy (NetServerEther, &arp->ar_data[ 0], 6);
1346
1347 (*packetHandler)(0,0,0,0);
1348 }
1349 break;
1350
1351 case PROT_IP:
1352 #ifdef ET_DEBUG
1353 puts ("Got IP\n");
1354 #endif
1355 if (len < IP_HDR_SIZE) {
1356 debug ("len bad %d < %d\n", len, IP_HDR_SIZE);
1357 return;
1358 }
1359 if (len < ntohs(ip->ip_len)) {
1360 printf("len bad %d < %d\n", len, ntohs(ip->ip_len));
1361 return;
1362 }
1363 len = ntohs(ip->ip_len);
1364 #ifdef ET_DEBUG
1365 printf("len=%d, v=%02x\n", len, ip->ip_hl_v & 0xff);
1366 #endif
1367 if ((ip->ip_hl_v & 0xf0) != 0x40) {
1368 return;
1369 }
1370 if (ip->ip_off & htons(0x1fff)) { /* Can't deal w/ fragments */
1371 return;
1372 }
1373 if (!NetCksumOk((uchar *)ip, IP_HDR_SIZE_NO_UDP / 2)) {
1374 puts ("checksum bad\n");
1375 return;
1376 }
1377 tmp = NetReadIP(&ip->ip_dst);
1378 if (NetOurIP && tmp != NetOurIP && tmp != 0xFFFFFFFF) {
1379 return;
1380 }
1381 /*
1382 * watch for ICMP host redirects
1383 *
1384 * There is no real handler code (yet). We just watch
1385 * for ICMP host redirect messages. In case anybody
1386 * sees these messages: please contact me
1387 * (wd@denx.de), or - even better - send me the
1388 * necessary fixes :-)
1389 *
1390 * Note: in all cases where I have seen this so far
1391 * it was a problem with the router configuration,
1392 * for instance when a router was configured in the
1393 * BOOTP reply, but the TFTP server was on the same
1394 * subnet. So this is probably a warning that your
1395 * configuration might be wrong. But I'm not really
1396 * sure if there aren't any other situations.
1397 */
1398 if (ip->ip_p == IPPROTO_ICMP) {
1399 ICMP_t *icmph = (ICMP_t *)&(ip->udp_src);
1400
1401 switch (icmph->type) {
1402 case ICMP_REDIRECT:
1403 if (icmph->code != ICMP_REDIR_HOST)
1404 return;
1405 puts (" ICMP Host Redirect to ");
1406 print_IPaddr(icmph->un.gateway);
1407 putc(' ');
1408 break;
1409 #if (CONFIG_COMMANDS & CFG_CMD_PING)
1410 case ICMP_ECHO_REPLY:
1411 /*
1412 * IP header OK. Pass the packet to the current handler.
1413 */
1414 /* XXX point to ip packet */
1415 (*packetHandler)((uchar *)ip, 0, 0, 0);
1416 break;
1417 #endif
1418 default:
1419 return;
1420 }
1421 } else if (ip->ip_p != IPPROTO_UDP) { /* Only UDP packets */
1422 return;
1423 }
1424
1425 #ifdef CONFIG_NETCONSOLE
1426 nc_input_packet((uchar *)ip +IP_HDR_SIZE,
1427 ntohs(ip->udp_dst),
1428 ntohs(ip->udp_src),
1429 ntohs(ip->udp_len) - 8);
1430 #endif
1431 /*
1432 * IP header OK. Pass the packet to the current handler.
1433 */
1434 (*packetHandler)((uchar *)ip +IP_HDR_SIZE,
1435 ntohs(ip->udp_dst),
1436 ntohs(ip->udp_src),
1437 ntohs(ip->udp_len) - 8);
1438 break;
1439 }
1440 }
1441
1442
1443 /**********************************************************************/
1444
1445 static int net_check_prereq (proto_t protocol)
1446 {
1447 switch (protocol) {
1448 /* Fall through */
1449 #if (CONFIG_COMMANDS & CFG_CMD_PING)
1450 case PING:
1451 if (NetPingIP == 0) {
1452 puts ("*** ERROR: ping address not given\n");
1453 return (1);
1454 }
1455 goto common;
1456 #endif
1457 #if (CONFIG_COMMANDS & CFG_CMD_SNTP)
1458 case SNTP:
1459 if (NetNtpServerIP == 0) {
1460 puts ("*** ERROR: NTP server address not given\n");
1461 return (1);
1462 }
1463 goto common;
1464 #endif
1465 #if (CONFIG_COMMANDS & CFG_CMD_NFS)
1466 case NFS:
1467 #endif
1468 case NETCONS:
1469 case TFTP:
1470 if (NetServerIP == 0) {
1471 puts ("*** ERROR: `serverip' not set\n");
1472 return (1);
1473 }
1474 #if (CONFIG_COMMANDS & (CFG_CMD_PING | CFG_CMD_SNTP))
1475 common:
1476 #endif
1477
1478 if (NetOurIP == 0) {
1479 puts ("*** ERROR: `ipaddr' not set\n");
1480 return (1);
1481 }
1482 /* Fall through */
1483
1484 case DHCP:
1485 case RARP:
1486 case BOOTP:
1487 case CDP:
1488 if (memcmp (NetOurEther, "\0\0\0\0\0\0", 6) == 0) {
1489 #ifdef CONFIG_NET_MULTI
1490 extern int eth_get_dev_index (void);
1491 int num = eth_get_dev_index ();
1492
1493 switch (num) {
1494 case -1:
1495 puts ("*** ERROR: No ethernet found.\n");
1496 return (1);
1497 case 0:
1498 puts ("*** ERROR: `ethaddr' not set\n");
1499 break;
1500 default:
1501 printf ("*** ERROR: `eth%daddr' not set\n",
1502 num);
1503 break;
1504 }
1505
1506 NetStartAgain ();
1507 return (2);
1508 #else
1509 puts ("*** ERROR: `ethaddr' not set\n");
1510 return (1);
1511 #endif
1512 }
1513 /* Fall through */
1514 default:
1515 return (0);
1516 }
1517 return (0); /* OK */
1518 }
1519 /**********************************************************************/
1520
1521 int
1522 NetCksumOk(uchar * ptr, int len)
1523 {
1524 return !((NetCksum(ptr, len) + 1) & 0xfffe);
1525 }
1526
1527
1528 unsigned
1529 NetCksum(uchar * ptr, int len)
1530 {
1531 ulong xsum;
1532
1533 xsum = 0;
1534 while (len-- > 0)
1535 xsum += *((ushort *)ptr)++;
1536 xsum = (xsum & 0xffff) + (xsum >> 16);
1537 xsum = (xsum & 0xffff) + (xsum >> 16);
1538 return (xsum & 0xffff);
1539 }
1540
1541 int
1542 NetEthHdrSize(void)
1543 {
1544 ushort myvlanid;
1545
1546 myvlanid = ntohs(NetOurVLAN);
1547 if (myvlanid == (ushort)-1)
1548 myvlanid = VLAN_NONE;
1549
1550 return ((myvlanid & VLAN_IDMASK) == VLAN_NONE) ? ETHER_HDR_SIZE : VLAN_ETHER_HDR_SIZE;
1551 }
1552
1553 int
1554 NetSetEther(volatile uchar * xet, uchar * addr, uint prot)
1555 {
1556 Ethernet_t *et = (Ethernet_t *)xet;
1557 ushort myvlanid;
1558
1559 myvlanid = ntohs(NetOurVLAN);
1560 if (myvlanid == (ushort)-1)
1561 myvlanid = VLAN_NONE;
1562
1563 memcpy (et->et_dest, addr, 6);
1564 memcpy (et->et_src, NetOurEther, 6);
1565 if ((myvlanid & VLAN_IDMASK) == VLAN_NONE) {
1566 et->et_protlen = htons(prot);
1567 return ETHER_HDR_SIZE;
1568 } else {
1569 VLAN_Ethernet_t *vet = (VLAN_Ethernet_t *)xet;
1570
1571 vet->vet_vlan_type = htons(PROT_VLAN);
1572 vet->vet_tag = htons((0 << 5) | (myvlanid & VLAN_IDMASK));
1573 vet->vet_type = htons(prot);
1574 return VLAN_ETHER_HDR_SIZE;
1575 }
1576 }
1577
1578 void
1579 NetSetIP(volatile uchar * xip, IPaddr_t dest, int dport, int sport, int len)
1580 {
1581 volatile IP_t *ip = (IP_t *)xip;
1582
1583 /*
1584 * If the data is an odd number of bytes, zero the
1585 * byte after the last byte so that the checksum
1586 * will work.
1587 */
1588 if (len & 1)
1589 xip[IP_HDR_SIZE + len] = 0;
1590
1591 /*
1592 * Construct an IP and UDP header.
1593 * (need to set no fragment bit - XXX)
1594 */
1595 ip->ip_hl_v = 0x45; /* IP_HDR_SIZE / 4 (not including UDP) */
1596 ip->ip_tos = 0;
1597 ip->ip_len = htons(IP_HDR_SIZE + len);
1598 ip->ip_id = htons(NetIPID++);
1599 ip->ip_off = htons(0x4000); /* No fragmentation */
1600 ip->ip_ttl = 255;
1601 ip->ip_p = 17; /* UDP */
1602 ip->ip_sum = 0;
1603 NetCopyIP((void*)&ip->ip_src, &NetOurIP); /* already in network byte order */
1604 NetCopyIP((void*)&ip->ip_dst, &dest); /* - "" - */
1605 ip->udp_src = htons(sport);
1606 ip->udp_dst = htons(dport);
1607 ip->udp_len = htons(8 + len);
1608 ip->udp_xsum = 0;
1609 ip->ip_sum = ~NetCksum((uchar *)ip, IP_HDR_SIZE_NO_UDP / 2);
1610 }
1611
1612 void copy_filename (uchar *dst, uchar *src, int size)
1613 {
1614 if (*src && (*src == '"')) {
1615 ++src;
1616 --size;
1617 }
1618
1619 while ((--size > 0) && *src && (*src != '"')) {
1620 *dst++ = *src++;
1621 }
1622 *dst = '\0';
1623 }
1624
1625 #endif /* CFG_CMD_NET */
1626
1627 void ip_to_string (IPaddr_t x, char *s)
1628 {
1629 x = ntohl (x);
1630 sprintf (s, "%d.%d.%d.%d",
1631 (int) ((x >> 24) & 0xff),
1632 (int) ((x >> 16) & 0xff),
1633 (int) ((x >> 8) & 0xff), (int) ((x >> 0) & 0xff)
1634 );
1635 }
1636
1637 IPaddr_t string_to_ip(char *s)
1638 {
1639 IPaddr_t addr;
1640 char *e;
1641 int i;
1642
1643 if (s == NULL)
1644 return(0);
1645
1646 for (addr=0, i=0; i<4; ++i) {
1647 ulong val = s ? simple_strtoul(s, &e, 10) : 0;
1648 addr <<= 8;
1649 addr |= (val & 0xFF);
1650 if (s) {
1651 s = (*e) ? e+1 : e;
1652 }
1653 }
1654
1655 return (htonl(addr));
1656 }
1657
1658 void VLAN_to_string(ushort x, char *s)
1659 {
1660 x = ntohs(x);
1661
1662 if (x == (ushort)-1)
1663 x = VLAN_NONE;
1664
1665 if (x == VLAN_NONE)
1666 strcpy(s, "none");
1667 else
1668 sprintf(s, "%d", x & VLAN_IDMASK);
1669 }
1670
1671 ushort string_to_VLAN(char *s)
1672 {
1673 ushort id;
1674
1675 if (s == NULL)
1676 return htons(VLAN_NONE);
1677
1678 if (*s < '0' || *s > '9')
1679 id = VLAN_NONE;
1680 else
1681 id = (ushort)simple_strtoul(s, NULL, 10);
1682
1683 return htons(id);
1684 }
1685
1686 void print_IPaddr (IPaddr_t x)
1687 {
1688 char tmp[16];
1689
1690 ip_to_string (x, tmp);
1691
1692 puts (tmp);
1693 }
1694
1695 IPaddr_t getenv_IPaddr (char *var)
1696 {
1697 return (string_to_ip(getenv(var)));
1698 }
1699
1700 ushort getenv_VLAN(char *var)
1701 {
1702 return (string_to_VLAN(getenv(var)));
1703 }