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