2 * Copied from Linux Monitor (LiMon) - Networking.
4 * Copyright 1994 - 2000 Neil Russell.
6 * Copyright 2000 Roland Borde
7 * Copyright 2000 Paolo Scaffardi
8 * Copyright 2000-2002 Wolfgang Denk, wd@denx.de
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:
20 * Prerequisites: - own ethernet address
21 * We want: - own IP address
22 * - TFTP server IP address
28 * Prerequisites: - own ethernet address
29 * We want: - own IP address
30 * - TFTP server IP address
35 * Prerequisites: - own ethernet address
37 * - TFTP server IP address
38 * We want: - TFTP server ethernet address
43 * Prerequisites: - own ethernet address
44 * We want: - IP, Netmask, ServerIP, Gateway IP
45 * - bootfilename, lease time
50 * Prerequisites: - own ethernet 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
61 * Prerequisites: - own ethernet 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
70 * Prerequisites: - own ethernet address
72 * We want: - network time
85 #ifdef CONFIG_STATUS_LED
86 #include <status_led.h>
89 #if defined(CONFIG_CMD_SNTP)
92 #if defined(CONFIG_CDP_VERSION)
93 #include <timestamp.h>
96 #if defined(CONFIG_CMD_NET)
98 DECLARE_GLOBAL_DATA_PTR;
100 #ifndef CONFIG_ARP_TIMEOUT
101 # define ARP_TIMEOUT 5000UL /* Milliseconds before trying ARP again */
103 # define ARP_TIMEOUT CONFIG_ARP_TIMEOUT
107 #ifndef CONFIG_NET_RETRY_COUNT
108 # define ARP_TIMEOUT_COUNT 5 /* # of timeouts before giving up */
110 # define ARP_TIMEOUT_COUNT CONFIG_NET_RETRY_COUNT
117 /** BOOTP EXTENTIONS **/
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 */
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 */
130 #ifdef CONFIG_MCAST_TFTP /* Multicast TFTP */
134 /** END OF BOOTP EXTENTIONS **/
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 };
150 void (*push_packet)(volatile void *, int len) = 0;
152 #if defined(CONFIG_CMD_CDP)
153 uchar NetCDPAddr[6] = /* Ethernet bcast address */
154 { 0x01, 0x00, 0x0c, 0xcc, 0xcc, 0xcc };
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 */
163 /* XXX in both little & big endian machines 0xFFFF == ntohs(-1) */
164 ushort NetOurVLAN = 0xFFFF; /* default is without VLAN */
165 ushort NetOurNativeVLAN = 0xFFFF; /* ditto */
167 char BootFile[128]; /* Boot File name */
169 #if defined(CONFIG_CMD_PING)
170 IPaddr_t NetPingIP; /* the ip address to ping */
172 static void PingStart(void);
175 #if defined(CONFIG_CMD_CDP)
176 static void CDPStart(void);
179 #if defined(CONFIG_CMD_SNTP)
180 IPaddr_t NetNtpServerIP; /* NTP server IP address */
181 int NetTimeOffset=0; /* offset time from UTC */
184 #ifdef CONFIG_NETCONSOLE
186 int nc_input_packet(uchar *pkt, unsigned dest, unsigned src, unsigned len);
189 volatile uchar PktBuf[(PKTBUFSRX+1) * PKTSIZE_ALIGN + PKTALIGN];
191 volatile uchar *NetRxPackets[PKTBUFSRX]; /* Receive packets */
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 */
199 static int net_check_prereq (proto_t protocol);
201 /**********************************************************************/
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;
212 int env_changed_id = 0;
214 void ArpRequest (void)
221 printf ("ARP broadcast %d\n", NetArpWaitTry);
225 pkt += NetSetEther (pkt, NetBcastAddr, PROT_ARP);
229 arp->ar_hrd = htons (ARP_ETHER);
230 arp->ar_pro = htons (PROT_IP);
233 arp->ar_op = htons (ARPOP_REQUEST);
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 */
241 if ((NetArpWaitPacketIP & NetOurSubnetMask) !=
242 (NetOurIP & NetOurSubnetMask)) {
243 if (NetOurGatewayIP == 0) {
244 puts ("## Warning: gatewayip needed but not set\n");
245 NetArpWaitReplyIP = NetArpWaitPacketIP;
247 NetArpWaitReplyIP = NetOurGatewayIP;
250 NetArpWaitReplyIP = NetArpWaitPacketIP;
253 NetWriteIP ((uchar *) & arp->ar_data[16], NetArpWaitReplyIP);
254 (void) eth_send (NetTxPacket, (pkt - NetTxPacket) + ARP_HDR_SIZE);
257 void ArpTimeoutCheck(void)
261 if (!NetArpWaitPacketIP)
266 /* check for arp timeout */
267 if ((t - NetArpWaitTimerStart) > ARP_TIMEOUT) {
270 if (NetArpWaitTry >= ARP_TIMEOUT_COUNT) {
271 puts ("\nARP Retry count exceeded; starting again\n");
275 NetArpWaitTimerStart = t;
282 NetInitLoop(proto_t protocol)
285 int env_id = get_env_id ();
287 /* update only when the environment has changed */
288 if (env_changed_id != env_id) {
289 NetCopyIP(&NetOurIP, &bd->bi_ip_addr);
290 NetOurGatewayIP = getenv_IPaddr ("gatewayip");
291 NetOurSubnetMask= getenv_IPaddr ("netmask");
292 NetServerIP = getenv_IPaddr ("serverip");
293 NetOurNativeVLAN = getenv_VLAN("nvlan");
294 NetOurVLAN = getenv_VLAN("vlan");
295 env_changed_id = env_id;
301 /**********************************************************************/
303 * Main network processing loop.
307 NetLoop(proto_t protocol)
311 #ifdef CONFIG_NET_MULTI
316 /* XXX problem with bss workaround */
317 NetArpWaitPacketMAC = NULL;
318 NetArpWaitTxPacket = NULL;
319 NetArpWaitPacketIP = 0;
320 NetArpWaitReplyIP = 0;
321 NetArpWaitTxPacket = NULL;
327 * Setup packet buffers, aligned correctly.
329 NetTxPacket = &PktBuf[0] + (PKTALIGN - 1);
330 NetTxPacket -= (ulong)NetTxPacket % PKTALIGN;
331 for (i = 0; i < PKTBUFSRX; i++) {
332 NetRxPackets[i] = NetTxPacket + (i+1)*PKTSIZE_ALIGN;
336 if (!NetArpWaitTxPacket) {
337 NetArpWaitTxPacket = &NetArpWaitPacketBuf[0] + (PKTALIGN - 1);
338 NetArpWaitTxPacket -= (ulong)NetArpWaitTxPacket % PKTALIGN;
339 NetArpWaitTxPacketSize = 0;
343 #ifdef CONFIG_NET_MULTI
346 if (eth_init(bd) < 0) {
352 #ifdef CONFIG_NET_MULTI
353 memcpy (NetOurEther, eth_get_dev()->enetaddr, 6);
355 eth_getenv_enetaddr("ethaddr", NetOurEther);
358 NetState = NETLOOP_CONTINUE;
361 * Start the ball rolling with the given start function. From
362 * here on, this code is a state machine driven by received
363 * packets and timer events.
365 NetInitLoop(protocol);
367 switch (net_check_prereq (protocol)) {
369 /* network not configured */
373 #ifdef CONFIG_NET_MULTI
375 /* network device not configured */
377 #endif /* CONFIG_NET_MULTI */
380 #ifdef CONFIG_NET_MULTI
385 /* always use ARP to get server ethernet address */
389 #if defined(CONFIG_CMD_DHCP)
392 DhcpRequest(); /* Basically same as BOOTP */
405 #if defined(CONFIG_CMD_PING)
410 #if defined(CONFIG_CMD_NFS)
415 #if defined(CONFIG_CMD_CDP)
420 #ifdef CONFIG_NETCONSOLE
425 #if defined(CONFIG_CMD_SNTP)
434 NetBootFileXferSize = 0;
438 #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
439 #if defined(CONFIG_SYS_FAULT_ECHO_LINK_DOWN) && defined(CONFIG_STATUS_LED) && defined(STATUS_LED_RED)
441 * Echo the inverted link state to the fault LED.
443 if(miiphy_link(eth_get_dev()->name, CONFIG_SYS_FAULT_MII_ADDR)) {
444 status_led_set (STATUS_LED_RED, STATUS_LED_OFF);
446 status_led_set (STATUS_LED_RED, STATUS_LED_ON);
448 #endif /* CONFIG_SYS_FAULT_ECHO_LINK_DOWN, ... */
449 #endif /* CONFIG_MII, ... */
452 * Main packet reception loop. Loop receiving packets until
453 * someone sets `NetState' to a state that terminates.
457 #ifdef CONFIG_SHOW_ACTIVITY
459 extern void show_activity(int arg);
464 * Check the ethernet for a new packet. The ethernet
465 * receive routine will process it.
470 * Abort if ctrl-c was pressed.
481 * Check for a timeout, and run the timeout handler
484 if (timeHandler && ((get_timer(0) - timeStart) > timeDelta)) {
487 #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
488 # if defined(CONFIG_SYS_FAULT_ECHO_LINK_DOWN) && \
489 defined(CONFIG_STATUS_LED) && \
490 defined(STATUS_LED_RED)
492 * Echo the inverted link state to the fault LED.
494 if(miiphy_link(eth_get_dev()->name, CONFIG_SYS_FAULT_MII_ADDR)) {
495 status_led_set (STATUS_LED_RED, STATUS_LED_OFF);
497 status_led_set (STATUS_LED_RED, STATUS_LED_ON);
499 # endif /* CONFIG_SYS_FAULT_ECHO_LINK_DOWN, ... */
500 #endif /* CONFIG_MII, ... */
502 timeHandler = (thand_f *)0;
509 case NETLOOP_RESTART:
510 #ifdef CONFIG_NET_MULTI
515 case NETLOOP_SUCCESS:
516 if (NetBootFileXferSize > 0) {
518 printf("Bytes transferred = %ld (%lx hex)\n",
520 NetBootFileXferSize);
521 sprintf(buf, "%lX", NetBootFileXferSize);
522 setenv("filesize", buf);
524 sprintf(buf, "%lX", (unsigned long)load_addr);
525 setenv("fileaddr", buf);
528 return NetBootFileXferSize;
536 /**********************************************************************/
539 startAgainTimeout(void)
541 NetState = NETLOOP_RESTART;
545 startAgainHandler(uchar * pkt, unsigned dest, unsigned src, unsigned len)
547 /* Totally ignore the packet */
550 void NetStartAgain (void)
553 int noretry = 0, once = 0;
555 if ((nretry = getenv ("netretry")) != NULL) {
556 noretry = (strcmp (nretry, "no") == 0);
557 once = (strcmp (nretry, "once") == 0);
561 NetState = NETLOOP_FAIL;
564 #ifndef CONFIG_NET_MULTI
565 NetSetTimeout (10000UL, startAgainTimeout);
566 NetSetHandler (startAgainHandler);
567 #else /* !CONFIG_NET_MULTI*/
569 #if !defined(CONFIG_NET_DO_NOT_TRY_ANOTHER)
570 eth_try_another (!NetRestarted);
573 if (NetRestartWrap) {
575 if (NetDevExists && !once) {
576 NetSetTimeout (10000UL, startAgainTimeout);
577 NetSetHandler (startAgainHandler);
579 NetState = NETLOOP_FAIL;
582 NetState = NETLOOP_RESTART;
584 #endif /* CONFIG_NET_MULTI */
587 /**********************************************************************/
593 NetSetHandler(rxhand_f * f)
600 NetSetTimeout(ulong iv, thand_f * f)
603 timeHandler = (thand_f *)0;
606 timeStart = get_timer(0);
613 NetSendPacket(volatile uchar * pkt, int len)
615 (void) eth_send(pkt, len);
619 NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport, int sport, int len)
623 /* convert to new style broadcast */
627 /* if broadcast, make the ether address a broadcast and don't do ARP */
628 if (dest == 0xFFFFFFFF)
629 ether = NetBcastAddr;
631 /* if MAC address was not discovered yet, save the packet and do an ARP request */
632 if (memcmp(ether, NetEtherNullAddr, 6) == 0) {
635 printf("sending ARP for %08lx\n", dest);
637 NetArpWaitPacketIP = dest;
638 NetArpWaitPacketMAC = ether;
640 pkt = NetArpWaitTxPacket;
641 pkt += NetSetEther (pkt, NetArpWaitPacketMAC, PROT_IP);
643 NetSetIP (pkt, dest, dport, sport, len);
644 memcpy(pkt + IP_HDR_SIZE, (uchar *)NetTxPacket + (pkt - (uchar *)NetArpWaitTxPacket) + IP_HDR_SIZE, len);
646 /* size of the waiting packet */
647 NetArpWaitTxPacketSize = (pkt - NetArpWaitTxPacket) + IP_HDR_SIZE + len;
649 /* and do the ARP request */
651 NetArpWaitTimerStart = get_timer(0);
653 return 1; /* waiting */
657 printf("sending UDP to %08lx/%pM\n", dest, ether);
660 pkt = (uchar *)NetTxPacket;
661 pkt += NetSetEther (pkt, ether, PROT_IP);
662 NetSetIP (pkt, dest, dport, sport, len);
663 (void) eth_send(NetTxPacket, (pkt - NetTxPacket) + IP_HDR_SIZE + len);
665 return 0; /* transmitted */
668 #if defined(CONFIG_CMD_PING)
669 static ushort PingSeqNo;
678 /* XXX always send arp request */
680 memcpy(mac, NetEtherNullAddr, 6);
683 printf("sending ARP for %08lx\n", NetPingIP);
686 NetArpWaitPacketIP = NetPingIP;
687 NetArpWaitPacketMAC = mac;
689 pkt = NetArpWaitTxPacket;
690 pkt += NetSetEther(pkt, mac, PROT_IP);
692 ip = (volatile IP_t *)pkt;
695 * Construct an IP and ICMP header. (need to set no fragment bit - XXX)
697 ip->ip_hl_v = 0x45; /* IP_HDR_SIZE / 4 (not including UDP) */
699 ip->ip_len = htons(IP_HDR_SIZE_NO_UDP + 8);
700 ip->ip_id = htons(NetIPID++);
701 ip->ip_off = htons(IP_FLAGS_DFRAG); /* Don't fragment */
703 ip->ip_p = 0x01; /* ICMP */
705 NetCopyIP((void*)&ip->ip_src, &NetOurIP); /* already in network byte order */
706 NetCopyIP((void*)&ip->ip_dst, &NetPingIP); /* - "" - */
707 ip->ip_sum = ~NetCksum((uchar *)ip, IP_HDR_SIZE_NO_UDP / 2);
709 s = &ip->udp_src; /* XXX ICMP starts here */
710 s[0] = htons(0x0800); /* echo-request, code */
711 s[1] = 0; /* checksum */
712 s[2] = 0; /* identifier */
713 s[3] = htons(PingSeqNo++); /* sequence number */
714 s[1] = ~NetCksum((uchar *)s, 8/2);
716 /* size of the waiting packet */
717 NetArpWaitTxPacketSize = (pkt - NetArpWaitTxPacket) + IP_HDR_SIZE_NO_UDP + 8;
719 /* and do the ARP request */
721 NetArpWaitTimerStart = get_timer(0);
723 return 1; /* waiting */
730 NetState = NETLOOP_FAIL; /* we did not get the reply */
734 PingHandler (uchar * pkt, unsigned dest, unsigned src, unsigned len)
737 volatile IP_t *ip = (volatile IP_t *)pkt;
739 tmp = NetReadIP((void *)&ip->ip_src);
740 if (tmp != NetPingIP)
743 NetState = NETLOOP_SUCCESS;
746 static void PingStart(void)
748 #if defined(CONFIG_NET_MULTI)
749 printf ("Using %s device\n", eth_get_name());
750 #endif /* CONFIG_NET_MULTI */
751 NetSetTimeout (10000UL, PingTimeout);
752 NetSetHandler (PingHandler);
758 #if defined(CONFIG_CMD_CDP)
760 #define CDP_DEVICE_ID_TLV 0x0001
761 #define CDP_ADDRESS_TLV 0x0002
762 #define CDP_PORT_ID_TLV 0x0003
763 #define CDP_CAPABILITIES_TLV 0x0004
764 #define CDP_VERSION_TLV 0x0005
765 #define CDP_PLATFORM_TLV 0x0006
766 #define CDP_NATIVE_VLAN_TLV 0x000a
767 #define CDP_APPLIANCE_VLAN_TLV 0x000e
768 #define CDP_TRIGGER_TLV 0x000f
769 #define CDP_POWER_CONSUMPTION_TLV 0x0010
770 #define CDP_SYSNAME_TLV 0x0014
771 #define CDP_SYSOBJECT_TLV 0x0015
772 #define CDP_MANAGEMENT_ADDRESS_TLV 0x0016
774 #define CDP_TIMEOUT 250UL /* one packet every 250ms */
779 ushort CDPNativeVLAN;
780 ushort CDPApplianceVLAN;
782 static const uchar CDP_SNAP_hdr[8] = { 0xAA, 0xAA, 0x03, 0x00, 0x00, 0x0C, 0x20, 0x00 };
784 static ushort CDP_compute_csum(const uchar *buff, ushort len)
793 odd = 1 & (ulong)buff;
803 if (result & 0x80000000)
804 result = (result & 0xFFFF) + (result >> 16);
808 leftover = (signed short)(*(const signed char *)buff);
809 /* CISCO SUCKS big time! (and blows too):
810 * CDP uses the IP checksum algorithm with a twist;
811 * for the last byte it *sign* extends and sums.
813 result = (result & 0xffff0000) | ((result + leftover) & 0x0000ffff);
816 result = (result & 0xFFFF) + (result >> 16);
819 result = ((result >> 8) & 0xff) | ((result & 0xff) << 8);
822 /* add up 16-bit and 17-bit words for 17+c bits */
823 result = (result & 0xffff) + (result >> 16);
824 /* add up 16-bit and 2-bit for 16+c bit */
825 result = (result & 0xffff) + (result >> 16);
827 result = (result & 0xffff) + (result >> 16);
830 csum = ~(ushort)result;
832 /* run time endian detection */
833 if (csum != htons(csum)) /* little endian */
839 int CDPSendTrigger(void)
847 #if defined(CONFIG_CDP_DEVICE_ID) || defined(CONFIG_CDP_PORT_ID) || \
848 defined(CONFIG_CDP_VERSION) || defined(CONFIG_CDP_PLATFORM)
853 et = (Ethernet_t *)pkt;
855 /* NOTE: trigger sent not on any VLAN */
857 /* form ethernet header */
858 memcpy(et->et_dest, NetCDPAddr, 6);
859 memcpy(et->et_src, NetOurEther, 6);
861 pkt += ETHER_HDR_SIZE;
864 memcpy((uchar *)pkt, CDP_SNAP_hdr, sizeof(CDP_SNAP_hdr));
865 pkt += sizeof(CDP_SNAP_hdr);
868 *pkt++ = 0x02; /* CDP version 2 */
869 *pkt++ = 180; /* TTL */
870 s = (volatile ushort *)pkt;
872 *s++ = htons(0); /* checksum (0 for later calculation) */
875 #ifdef CONFIG_CDP_DEVICE_ID
876 *s++ = htons(CDP_DEVICE_ID_TLV);
877 *s++ = htons(CONFIG_CDP_DEVICE_ID);
878 sprintf(buf, CONFIG_CDP_DEVICE_ID_PREFIX "%pm", NetOurEther);
879 memcpy((uchar *)s, buf, 16);
883 #ifdef CONFIG_CDP_PORT_ID
884 *s++ = htons(CDP_PORT_ID_TLV);
885 memset(buf, 0, sizeof(buf));
886 sprintf(buf, CONFIG_CDP_PORT_ID, eth_get_dev_index());
888 if (len & 1) /* make it even */
890 *s++ = htons(len + 4);
891 memcpy((uchar *)s, buf, len);
895 #ifdef CONFIG_CDP_CAPABILITIES
896 *s++ = htons(CDP_CAPABILITIES_TLV);
898 *(ulong *)s = htonl(CONFIG_CDP_CAPABILITIES);
902 #ifdef CONFIG_CDP_VERSION
903 *s++ = htons(CDP_VERSION_TLV);
904 memset(buf, 0, sizeof(buf));
905 strcpy(buf, CONFIG_CDP_VERSION);
907 if (len & 1) /* make it even */
909 *s++ = htons(len + 4);
910 memcpy((uchar *)s, buf, len);
914 #ifdef CONFIG_CDP_PLATFORM
915 *s++ = htons(CDP_PLATFORM_TLV);
916 memset(buf, 0, sizeof(buf));
917 strcpy(buf, CONFIG_CDP_PLATFORM);
919 if (len & 1) /* make it even */
921 *s++ = htons(len + 4);
922 memcpy((uchar *)s, buf, len);
926 #ifdef CONFIG_CDP_TRIGGER
927 *s++ = htons(CDP_TRIGGER_TLV);
929 *(ulong *)s = htonl(CONFIG_CDP_TRIGGER);
933 #ifdef CONFIG_CDP_POWER_CONSUMPTION
934 *s++ = htons(CDP_POWER_CONSUMPTION_TLV);
936 *s++ = htons(CONFIG_CDP_POWER_CONSUMPTION);
939 /* length of ethernet packet */
940 len = (uchar *)s - ((uchar *)NetTxPacket + ETHER_HDR_SIZE);
941 et->et_protlen = htons(len);
943 len = ETHER_HDR_SIZE + sizeof(CDP_SNAP_hdr);
944 chksum = CDP_compute_csum((uchar *)NetTxPacket + len, (uchar *)s - (NetTxPacket + len));
949 (void) eth_send(NetTxPacket, (uchar *)s - NetTxPacket);
959 NetSetTimeout (CDP_TIMEOUT, CDPTimeout);
964 /* if not OK try again */
968 NetState = NETLOOP_SUCCESS;
972 CDPDummyHandler (uchar * pkt, unsigned dest, unsigned src, unsigned len)
978 CDPHandler(const uchar * pkt, unsigned len)
987 if (len < sizeof(CDP_SNAP_hdr) + 4)
990 /* check for valid CDP SNAP header */
991 if (memcmp(pkt, CDP_SNAP_hdr, sizeof(CDP_SNAP_hdr)) != 0)
994 pkt += sizeof(CDP_SNAP_hdr);
995 len -= sizeof(CDP_SNAP_hdr);
997 /* Version of CDP protocol must be >= 2 and TTL != 0 */
998 if (pkt[0] < 0x02 || pkt[1] == 0)
1001 /* if version is greater than 0x02 maybe we'll have a problem; output a warning */
1003 printf("** WARNING: CDP packet received with a protocol version %d > 2\n",
1006 if (CDP_compute_csum(pkt, len) != 0)
1018 ss = (const ushort *)pkt;
1019 type = ntohs(ss[0]);
1020 tlen = ntohs(ss[1]);
1028 ss += 2; /* point ss to the data of the TLV */
1032 case CDP_DEVICE_ID_TLV:
1034 case CDP_ADDRESS_TLV:
1036 case CDP_PORT_ID_TLV:
1038 case CDP_CAPABILITIES_TLV:
1040 case CDP_VERSION_TLV:
1042 case CDP_PLATFORM_TLV:
1044 case CDP_NATIVE_VLAN_TLV:
1047 case CDP_APPLIANCE_VLAN_TLV:
1048 t = (const uchar *)ss;
1054 ss = (const ushort *)(t + 1);
1056 #ifdef CONFIG_CDP_APPLIANCE_VLAN_TYPE
1057 if (applid == CONFIG_CDP_APPLIANCE_VLAN_TYPE)
1060 vlan = ntohs(*ss); /* XXX will this work; dunno */
1065 case CDP_TRIGGER_TLV:
1067 case CDP_POWER_CONSUMPTION_TLV:
1069 case CDP_SYSNAME_TLV:
1071 case CDP_SYSOBJECT_TLV:
1073 case CDP_MANAGEMENT_ADDRESS_TLV:
1078 CDPApplianceVLAN = vlan;
1079 CDPNativeVLAN = nvlan;
1085 printf("** CDP packet is too short\n");
1089 static void CDPStart(void)
1091 #if defined(CONFIG_NET_MULTI)
1092 printf ("Using %s device\n", eth_get_name());
1097 CDPNativeVLAN = htons(-1);
1098 CDPApplianceVLAN = htons(-1);
1100 NetSetTimeout (CDP_TIMEOUT, CDPTimeout);
1101 NetSetHandler (CDPDummyHandler);
1109 NetReceive(volatile uchar * inpkt, int len)
1117 #if defined(CONFIG_CMD_CDP)
1120 ushort cti = 0, vlanid = VLAN_NONE, myvlanid, mynvlanid;
1123 printf("packet received\n");
1128 et = (Ethernet_t *)inpkt;
1130 /* too small packet? */
1131 if (len < ETHER_HDR_SIZE)
1136 (*push_packet)(inpkt, len);
1141 #if defined(CONFIG_CMD_CDP)
1142 /* keep track if packet is CDP */
1143 iscdp = memcmp(et->et_dest, NetCDPAddr, 6) == 0;
1146 myvlanid = ntohs(NetOurVLAN);
1147 if (myvlanid == (ushort)-1)
1148 myvlanid = VLAN_NONE;
1149 mynvlanid = ntohs(NetOurNativeVLAN);
1150 if (mynvlanid == (ushort)-1)
1151 mynvlanid = VLAN_NONE;
1153 x = ntohs(et->et_protlen);
1156 printf("packet received\n");
1161 * Got a 802 packet. Check the other protocol field.
1163 x = ntohs(et->et_prot);
1165 ip = (IP_t *)(inpkt + E802_HDR_SIZE);
1166 len -= E802_HDR_SIZE;
1168 } else if (x != PROT_VLAN) { /* normal packet */
1169 ip = (IP_t *)(inpkt + ETHER_HDR_SIZE);
1170 len -= ETHER_HDR_SIZE;
1172 } else { /* VLAN packet */
1173 VLAN_Ethernet_t *vet = (VLAN_Ethernet_t *)et;
1176 printf("VLAN packet received\n");
1178 /* too small packet? */
1179 if (len < VLAN_ETHER_HDR_SIZE)
1182 /* if no VLAN active */
1183 if ((ntohs(NetOurVLAN) & VLAN_IDMASK) == VLAN_NONE
1184 #if defined(CONFIG_CMD_CDP)
1190 cti = ntohs(vet->vet_tag);
1191 vlanid = cti & VLAN_IDMASK;
1192 x = ntohs(vet->vet_type);
1194 ip = (IP_t *)(inpkt + VLAN_ETHER_HDR_SIZE);
1195 len -= VLAN_ETHER_HDR_SIZE;
1199 printf("Receive from protocol 0x%x\n", x);
1202 #if defined(CONFIG_CMD_CDP)
1204 CDPHandler((uchar *)ip, len);
1209 if ((myvlanid & VLAN_IDMASK) != VLAN_NONE) {
1210 if (vlanid == VLAN_NONE)
1211 vlanid = (mynvlanid & VLAN_IDMASK);
1213 if (vlanid != (myvlanid & VLAN_IDMASK))
1221 * We have to deal with two types of ARP packets:
1222 * - REQUEST packets will be answered by sending our
1223 * IP address - if we know it.
1224 * - REPLY packates are expected only after we asked
1225 * for the TFTP server's or the gateway's ethernet
1226 * address; so if we receive such a packet, we set
1227 * the server ethernet address
1233 if (len < ARP_HDR_SIZE) {
1234 printf("bad length %d < %d\n", len, ARP_HDR_SIZE);
1237 if (ntohs(arp->ar_hrd) != ARP_ETHER) {
1240 if (ntohs(arp->ar_pro) != PROT_IP) {
1243 if (arp->ar_hln != 6) {
1246 if (arp->ar_pln != 4) {
1250 if (NetOurIP == 0) {
1254 if (NetReadIP(&arp->ar_data[16]) != NetOurIP) {
1258 switch (ntohs(arp->ar_op)) {
1259 case ARPOP_REQUEST: /* reply with our IP address */
1261 puts ("Got ARP REQUEST, return our IP\n");
1264 pkt += NetSetEther(pkt, et->et_src, PROT_ARP);
1265 arp->ar_op = htons(ARPOP_REPLY);
1266 memcpy (&arp->ar_data[10], &arp->ar_data[0], 6);
1267 NetCopyIP(&arp->ar_data[16], &arp->ar_data[6]);
1268 memcpy (&arp->ar_data[ 0], NetOurEther, 6);
1269 NetCopyIP(&arp->ar_data[ 6], &NetOurIP);
1270 (void) eth_send((uchar *)et, (pkt - (uchar *)et) + ARP_HDR_SIZE);
1273 case ARPOP_REPLY: /* arp reply */
1274 /* are we waiting for a reply */
1275 if (!NetArpWaitPacketIP || !NetArpWaitPacketMAC)
1278 printf("Got ARP REPLY, set server/gtwy eth addr (%pM)\n",
1282 tmp = NetReadIP(&arp->ar_data[6]);
1284 /* matched waiting packet's address */
1285 if (tmp == NetArpWaitReplyIP) {
1289 /* save address for later use */
1290 memcpy(NetArpWaitPacketMAC, &arp->ar_data[0], 6);
1292 #ifdef CONFIG_NETCONSOLE
1293 (*packetHandler)(0,0,0,0);
1295 /* modify header, and transmit it */
1296 memcpy(((Ethernet_t *)NetArpWaitTxPacket)->et_dest, NetArpWaitPacketMAC, 6);
1297 (void) eth_send(NetArpWaitTxPacket, NetArpWaitTxPacketSize);
1299 /* no arp request pending now */
1300 NetArpWaitPacketIP = 0;
1301 NetArpWaitTxPacketSize = 0;
1302 NetArpWaitPacketMAC = NULL;
1308 printf("Unexpected ARP opcode 0x%x\n", ntohs(arp->ar_op));
1316 puts ("Got RARP\n");
1319 if (len < ARP_HDR_SIZE) {
1320 printf("bad length %d < %d\n", len, ARP_HDR_SIZE);
1324 if ((ntohs(arp->ar_op) != RARPOP_REPLY) ||
1325 (ntohs(arp->ar_hrd) != ARP_ETHER) ||
1326 (ntohs(arp->ar_pro) != PROT_IP) ||
1327 (arp->ar_hln != 6) || (arp->ar_pln != 4)) {
1329 puts ("invalid RARP header\n");
1331 NetCopyIP(&NetOurIP, &arp->ar_data[16]);
1332 if (NetServerIP == 0)
1333 NetCopyIP(&NetServerIP, &arp->ar_data[ 6]);
1334 memcpy (NetServerEther, &arp->ar_data[ 0], 6);
1336 (*packetHandler)(0,0,0,0);
1344 if (len < IP_HDR_SIZE) {
1345 debug ("len bad %d < %lu\n", len, (ulong)IP_HDR_SIZE);
1348 if (len < ntohs(ip->ip_len)) {
1349 printf("len bad %d < %d\n", len, ntohs(ip->ip_len));
1352 len = ntohs(ip->ip_len);
1354 printf("len=%d, v=%02x\n", len, ip->ip_hl_v & 0xff);
1356 if ((ip->ip_hl_v & 0xf0) != 0x40) {
1359 /* Can't deal with fragments */
1360 if (ip->ip_off & htons(IP_OFFS | IP_FLAGS_MFRAG)) {
1363 /* can't deal with headers > 20 bytes */
1364 if ((ip->ip_hl_v & 0x0f) > 0x05) {
1367 if (!NetCksumOk((uchar *)ip, IP_HDR_SIZE_NO_UDP / 2)) {
1368 puts ("checksum bad\n");
1371 tmp = NetReadIP(&ip->ip_dst);
1372 if (NetOurIP && tmp != NetOurIP && tmp != 0xFFFFFFFF) {
1373 #ifdef CONFIG_MCAST_TFTP
1374 if (Mcast_addr != tmp)
1379 * watch for ICMP host redirects
1381 * There is no real handler code (yet). We just watch
1382 * for ICMP host redirect messages. In case anybody
1383 * sees these messages: please contact me
1384 * (wd@denx.de), or - even better - send me the
1385 * necessary fixes :-)
1387 * Note: in all cases where I have seen this so far
1388 * it was a problem with the router configuration,
1389 * for instance when a router was configured in the
1390 * BOOTP reply, but the TFTP server was on the same
1391 * subnet. So this is probably a warning that your
1392 * configuration might be wrong. But I'm not really
1393 * sure if there aren't any other situations.
1395 if (ip->ip_p == IPPROTO_ICMP) {
1396 ICMP_t *icmph = (ICMP_t *)&(ip->udp_src);
1398 switch (icmph->type) {
1400 if (icmph->code != ICMP_REDIR_HOST)
1402 printf (" ICMP Host Redirect to %pI4 ", &icmph->un.gateway);
1404 #if defined(CONFIG_CMD_PING)
1405 case ICMP_ECHO_REPLY:
1407 * IP header OK. Pass the packet to the current handler.
1409 /* XXX point to ip packet */
1410 (*packetHandler)((uchar *)ip, 0, 0, 0);
1412 case ICMP_ECHO_REQUEST:
1414 printf ("Got ICMP ECHO REQUEST, return %d bytes \n",
1415 ETHER_HDR_SIZE + len);
1417 memcpy (&et->et_dest[0], &et->et_src[0], 6);
1418 memcpy (&et->et_src[ 0], NetOurEther, 6);
1422 NetCopyIP((void*)&ip->ip_dst, &ip->ip_src);
1423 NetCopyIP((void*)&ip->ip_src, &NetOurIP);
1424 ip->ip_sum = ~NetCksum((uchar *)ip, IP_HDR_SIZE_NO_UDP >> 1);
1426 icmph->type = ICMP_ECHO_REPLY;
1427 icmph->checksum = 0;
1428 icmph->checksum = ~NetCksum((uchar *)icmph,
1429 (len - IP_HDR_SIZE_NO_UDP) >> 1);
1430 (void) eth_send((uchar *)et, ETHER_HDR_SIZE + len);
1436 } else if (ip->ip_p != IPPROTO_UDP) { /* Only UDP packets */
1440 #ifdef CONFIG_UDP_CHECKSUM
1441 if (ip->udp_xsum != 0) {
1447 xsum += (ntohs(ip->udp_len));
1448 xsum += (ntohl(ip->ip_src) >> 16) & 0x0000ffff;
1449 xsum += (ntohl(ip->ip_src) >> 0) & 0x0000ffff;
1450 xsum += (ntohl(ip->ip_dst) >> 16) & 0x0000ffff;
1451 xsum += (ntohl(ip->ip_dst) >> 0) & 0x0000ffff;
1453 sumlen = ntohs(ip->udp_len);
1454 sumptr = (ushort *) &(ip->udp_src);
1456 while (sumlen > 1) {
1459 sumdata = *sumptr++;
1460 xsum += ntohs(sumdata);
1466 sumdata = *(unsigned char *) sumptr;
1467 sumdata = (sumdata << 8) & 0xff00;
1470 while ((xsum >> 16) != 0) {
1471 xsum = (xsum & 0x0000ffff) + ((xsum >> 16) & 0x0000ffff);
1473 if ((xsum != 0x00000000) && (xsum != 0x0000ffff)) {
1474 printf(" UDP wrong checksum %08lx %08x\n",
1475 xsum, ntohs(ip->udp_xsum));
1482 #ifdef CONFIG_NETCONSOLE
1483 nc_input_packet((uchar *)ip +IP_HDR_SIZE,
1486 ntohs(ip->udp_len) - 8);
1489 * IP header OK. Pass the packet to the current handler.
1491 (*packetHandler)((uchar *)ip +IP_HDR_SIZE,
1494 ntohs(ip->udp_len) - 8);
1500 /**********************************************************************/
1502 static int net_check_prereq (proto_t protocol)
1506 #if defined(CONFIG_CMD_PING)
1508 if (NetPingIP == 0) {
1509 puts ("*** ERROR: ping address not given\n");
1514 #if defined(CONFIG_CMD_SNTP)
1516 if (NetNtpServerIP == 0) {
1517 puts ("*** ERROR: NTP server address not given\n");
1522 #if defined(CONFIG_CMD_NFS)
1527 if (NetServerIP == 0) {
1528 puts ("*** ERROR: `serverip' not set\n");
1531 #if defined(CONFIG_CMD_PING) || defined(CONFIG_CMD_SNTP)
1535 if (NetOurIP == 0) {
1536 puts ("*** ERROR: `ipaddr' not set\n");
1545 if (memcmp (NetOurEther, "\0\0\0\0\0\0", 6) == 0) {
1546 #ifdef CONFIG_NET_MULTI
1547 extern int eth_get_dev_index (void);
1548 int num = eth_get_dev_index ();
1552 puts ("*** ERROR: No ethernet found.\n");
1555 puts ("*** ERROR: `ethaddr' not set\n");
1558 printf ("*** ERROR: `eth%daddr' not set\n",
1566 puts ("*** ERROR: `ethaddr' not set\n");
1574 return (0); /* OK */
1576 /**********************************************************************/
1579 NetCksumOk(uchar * ptr, int len)
1581 return !((NetCksum(ptr, len) + 1) & 0xfffe);
1586 NetCksum(uchar * ptr, int len)
1589 ushort *p = (ushort *)ptr;
1594 xsum = (xsum & 0xffff) + (xsum >> 16);
1595 xsum = (xsum & 0xffff) + (xsum >> 16);
1596 return (xsum & 0xffff);
1604 myvlanid = ntohs(NetOurVLAN);
1605 if (myvlanid == (ushort)-1)
1606 myvlanid = VLAN_NONE;
1608 return ((myvlanid & VLAN_IDMASK) == VLAN_NONE) ? ETHER_HDR_SIZE : VLAN_ETHER_HDR_SIZE;
1612 NetSetEther(volatile uchar * xet, uchar * addr, uint prot)
1614 Ethernet_t *et = (Ethernet_t *)xet;
1617 myvlanid = ntohs(NetOurVLAN);
1618 if (myvlanid == (ushort)-1)
1619 myvlanid = VLAN_NONE;
1621 memcpy (et->et_dest, addr, 6);
1622 memcpy (et->et_src, NetOurEther, 6);
1623 if ((myvlanid & VLAN_IDMASK) == VLAN_NONE) {
1624 et->et_protlen = htons(prot);
1625 return ETHER_HDR_SIZE;
1627 VLAN_Ethernet_t *vet = (VLAN_Ethernet_t *)xet;
1629 vet->vet_vlan_type = htons(PROT_VLAN);
1630 vet->vet_tag = htons((0 << 5) | (myvlanid & VLAN_IDMASK));
1631 vet->vet_type = htons(prot);
1632 return VLAN_ETHER_HDR_SIZE;
1637 NetSetIP(volatile uchar * xip, IPaddr_t dest, int dport, int sport, int len)
1639 IP_t *ip = (IP_t *)xip;
1642 * If the data is an odd number of bytes, zero the
1643 * byte after the last byte so that the checksum
1647 xip[IP_HDR_SIZE + len] = 0;
1650 * Construct an IP and UDP header.
1651 * (need to set no fragment bit - XXX)
1653 ip->ip_hl_v = 0x45; /* IP_HDR_SIZE / 4 (not including UDP) */
1655 ip->ip_len = htons(IP_HDR_SIZE + len);
1656 ip->ip_id = htons(NetIPID++);
1657 ip->ip_off = htons(IP_FLAGS_DFRAG); /* Don't fragment */
1659 ip->ip_p = 17; /* UDP */
1661 NetCopyIP((void*)&ip->ip_src, &NetOurIP); /* already in network byte order */
1662 NetCopyIP((void*)&ip->ip_dst, &dest); /* - "" - */
1663 ip->udp_src = htons(sport);
1664 ip->udp_dst = htons(dport);
1665 ip->udp_len = htons(8 + len);
1667 ip->ip_sum = ~NetCksum((uchar *)ip, IP_HDR_SIZE_NO_UDP / 2);
1670 void copy_filename (char *dst, char *src, int size)
1672 if (*src && (*src == '"')) {
1677 while ((--size > 0) && *src && (*src != '"')) {
1685 void ip_to_string (IPaddr_t x, char *s)
1688 sprintf (s, "%d.%d.%d.%d",
1689 (int) ((x >> 24) & 0xff),
1690 (int) ((x >> 16) & 0xff),
1691 (int) ((x >> 8) & 0xff), (int) ((x >> 0) & 0xff)
1695 IPaddr_t string_to_ip(char *s)
1704 for (addr=0, i=0; i<4; ++i) {
1705 ulong val = s ? simple_strtoul(s, &e, 10) : 0;
1707 addr |= (val & 0xFF);
1713 return (htonl(addr));
1716 void VLAN_to_string(ushort x, char *s)
1720 if (x == (ushort)-1)
1726 sprintf(s, "%d", x & VLAN_IDMASK);
1729 ushort string_to_VLAN(char *s)
1734 return htons(VLAN_NONE);
1736 if (*s < '0' || *s > '9')
1739 id = (ushort)simple_strtoul(s, NULL, 10);
1744 IPaddr_t getenv_IPaddr (char *var)
1746 return (string_to_ip(getenv(var)));
1749 ushort getenv_VLAN(char *var)
1751 return (string_to_VLAN(getenv(var)));