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)
93 #if defined(CONFIG_CMD_NET)
95 DECLARE_GLOBAL_DATA_PTR;
97 #ifndef CONFIG_ARP_TIMEOUT
98 # define ARP_TIMEOUT 5000UL /* Milliseconds before trying ARP again */
100 # define ARP_TIMEOUT CONFIG_ARP_TIMEOUT
104 #ifndef CONFIG_NET_RETRY_COUNT
105 # define ARP_TIMEOUT_COUNT 5 /* # of timeouts before giving up */
107 # define ARP_TIMEOUT_COUNT CONFIG_NET_RETRY_COUNT
114 /** BOOTP EXTENTIONS **/
116 IPaddr_t NetOurSubnetMask=0; /* Our subnet mask (0=unknown) */
117 IPaddr_t NetOurGatewayIP=0; /* Our gateways IP address */
118 IPaddr_t NetOurDNSIP=0; /* Our DNS IP address */
119 #if defined(CONFIG_BOOTP_DNS2)
120 IPaddr_t NetOurDNS2IP=0; /* Our 2nd DNS IP address */
122 char NetOurNISDomain[32]={0,}; /* Our NIS domain */
123 char NetOurHostName[32]={0,}; /* Our hostname */
124 char NetOurRootPath[64]={0,}; /* Our bootpath */
125 ushort NetBootFileSize=0; /* Our bootfile size in blocks */
127 #ifdef CONFIG_MCAST_TFTP /* Multicast TFTP */
131 /** END OF BOOTP EXTENTIONS **/
133 ulong NetBootFileXferSize; /* The actual transferred size of the bootfile (in bytes) */
134 uchar NetOurEther[6]; /* Our ethernet address */
135 uchar NetServerEther[6] = /* Boot server enet address */
136 { 0, 0, 0, 0, 0, 0 };
137 IPaddr_t NetOurIP; /* Our IP addr (0 = unknown) */
138 IPaddr_t NetServerIP; /* Server IP addr (0 = unknown) */
139 volatile uchar *NetRxPkt; /* Current receive packet */
140 int NetRxPktLen; /* Current rx packet length */
141 unsigned NetIPID; /* IP packet ID */
142 uchar NetBcastAddr[6] = /* Ethernet bcast address */
143 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
144 uchar NetEtherNullAddr[6] =
145 { 0, 0, 0, 0, 0, 0 };
147 void (*push_packet)(volatile void *, int len) = 0;
149 #if defined(CONFIG_CMD_CDP)
150 uchar NetCDPAddr[6] = /* Ethernet bcast address */
151 { 0x01, 0x00, 0x0c, 0xcc, 0xcc, 0xcc };
153 int NetState; /* Network loop state */
154 #ifdef CONFIG_NET_MULTI
155 int NetRestartWrap = 0; /* Tried all network devices */
156 static int NetRestarted = 0; /* Network loop restarted */
157 static int NetDevExists = 0; /* At least one device configured */
160 /* XXX in both little & big endian machines 0xFFFF == ntohs(-1) */
161 ushort NetOurVLAN = 0xFFFF; /* default is without VLAN */
162 ushort NetOurNativeVLAN = 0xFFFF; /* ditto */
164 char BootFile[128]; /* Boot File name */
166 #if defined(CONFIG_CMD_PING)
167 IPaddr_t NetPingIP; /* the ip address to ping */
169 static void PingStart(void);
172 #if defined(CONFIG_CMD_CDP)
173 static void CDPStart(void);
176 #if defined(CONFIG_CMD_SNTP)
177 IPaddr_t NetNtpServerIP; /* NTP server IP address */
178 int NetTimeOffset=0; /* offset time from UTC */
181 #ifdef CONFIG_NETCONSOLE
183 int nc_input_packet(uchar *pkt, unsigned dest, unsigned src, unsigned len);
186 volatile uchar PktBuf[(PKTBUFSRX+1) * PKTSIZE_ALIGN + PKTALIGN];
188 volatile uchar *NetRxPackets[PKTBUFSRX]; /* Receive packets */
190 static rxhand_f *packetHandler; /* Current RX packet handler */
191 static thand_f *timeHandler; /* Current timeout handler */
192 static ulong timeStart; /* Time base value */
193 static ulong timeDelta; /* Current timeout value */
194 volatile uchar *NetTxPacket = 0; /* THE transmit packet */
196 static int net_check_prereq (proto_t protocol);
198 /**********************************************************************/
200 IPaddr_t NetArpWaitPacketIP;
201 IPaddr_t NetArpWaitReplyIP;
202 uchar *NetArpWaitPacketMAC; /* MAC address of waiting packet's destination */
203 uchar *NetArpWaitTxPacket; /* THE transmit packet */
204 int NetArpWaitTxPacketSize;
205 uchar NetArpWaitPacketBuf[PKTSIZE_ALIGN + PKTALIGN];
206 ulong NetArpWaitTimerStart;
209 void ArpRequest (void)
216 printf ("ARP broadcast %d\n", NetArpWaitTry);
220 pkt += NetSetEther (pkt, NetBcastAddr, PROT_ARP);
224 arp->ar_hrd = htons (ARP_ETHER);
225 arp->ar_pro = htons (PROT_IP);
228 arp->ar_op = htons (ARPOP_REQUEST);
230 memcpy (&arp->ar_data[0], NetOurEther, 6); /* source ET addr */
231 NetWriteIP ((uchar *) & arp->ar_data[6], NetOurIP); /* source IP addr */
232 for (i = 10; i < 16; ++i) {
233 arp->ar_data[i] = 0; /* dest ET addr = 0 */
236 if ((NetArpWaitPacketIP & NetOurSubnetMask) !=
237 (NetOurIP & NetOurSubnetMask)) {
238 if (NetOurGatewayIP == 0) {
239 puts ("## Warning: gatewayip needed but not set\n");
240 NetArpWaitReplyIP = NetArpWaitPacketIP;
242 NetArpWaitReplyIP = NetOurGatewayIP;
245 NetArpWaitReplyIP = NetArpWaitPacketIP;
248 NetWriteIP ((uchar *) & arp->ar_data[16], NetArpWaitReplyIP);
249 (void) eth_send (NetTxPacket, (pkt - NetTxPacket) + ARP_HDR_SIZE);
252 void ArpTimeoutCheck(void)
256 if (!NetArpWaitPacketIP)
261 /* check for arp timeout */
262 if ((t - NetArpWaitTimerStart) > ARP_TIMEOUT) {
265 if (NetArpWaitTry >= ARP_TIMEOUT_COUNT) {
266 puts ("\nARP Retry count exceeded; starting again\n");
270 NetArpWaitTimerStart = t;
276 /**********************************************************************/
278 * Main network processing loop.
282 NetLoop(proto_t protocol)
286 #ifdef CONFIG_NET_MULTI
291 /* XXX problem with bss workaround */
292 NetArpWaitPacketMAC = NULL;
293 NetArpWaitTxPacket = NULL;
294 NetArpWaitPacketIP = 0;
295 NetArpWaitReplyIP = 0;
296 NetArpWaitTxPacket = NULL;
302 * Setup packet buffers, aligned correctly.
304 NetTxPacket = &PktBuf[0] + (PKTALIGN - 1);
305 NetTxPacket -= (ulong)NetTxPacket % PKTALIGN;
306 for (i = 0; i < PKTBUFSRX; i++) {
307 NetRxPackets[i] = NetTxPacket + (i+1)*PKTSIZE_ALIGN;
311 if (!NetArpWaitTxPacket) {
312 NetArpWaitTxPacket = &NetArpWaitPacketBuf[0] + (PKTALIGN - 1);
313 NetArpWaitTxPacket -= (ulong)NetArpWaitTxPacket % PKTALIGN;
314 NetArpWaitTxPacketSize = 0;
318 #ifdef CONFIG_NET_MULTI
321 if (eth_init(bd) < 0) {
327 #ifdef CONFIG_NET_MULTI
328 memcpy (NetOurEther, eth_get_dev()->enetaddr, 6);
330 memcpy (NetOurEther, bd->bi_enetaddr, 6);
333 NetState = NETLOOP_CONTINUE;
336 * Start the ball rolling with the given start function. From
337 * here on, this code is a state machine driven by received
338 * packets and timer events.
342 #if defined(CONFIG_CMD_NFS)
345 #if defined(CONFIG_CMD_PING)
348 #if defined(CONFIG_CMD_SNTP)
353 NetCopyIP(&NetOurIP, &bd->bi_ip_addr);
354 NetOurGatewayIP = getenv_IPaddr ("gatewayip");
355 NetOurSubnetMask= getenv_IPaddr ("netmask");
356 NetOurVLAN = getenv_VLAN("vlan");
357 NetOurNativeVLAN = getenv_VLAN("nvlan");
360 #if defined(CONFIG_CMD_NFS)
365 NetServerIP = getenv_IPaddr ("serverip");
367 #if defined(CONFIG_CMD_PING)
372 #if defined(CONFIG_CMD_SNTP)
385 * initialize our IP addr to 0 in order to accept ANY
386 * IP addr assigned to us by the BOOTP / RARP server
389 NetServerIP = getenv_IPaddr ("serverip");
390 NetOurVLAN = getenv_VLAN("vlan"); /* VLANs must be read */
391 NetOurNativeVLAN = getenv_VLAN("nvlan");
393 NetOurVLAN = getenv_VLAN("vlan"); /* VLANs must be read */
394 NetOurNativeVLAN = getenv_VLAN("nvlan");
400 switch (net_check_prereq (protocol)) {
402 /* network not configured */
406 #ifdef CONFIG_NET_MULTI
408 /* network device not configured */
410 #endif /* CONFIG_NET_MULTI */
413 #ifdef CONFIG_NET_MULTI
418 /* always use ARP to get server ethernet address */
422 #if defined(CONFIG_CMD_DHCP)
424 /* Start with a clean slate... */
427 NetServerIP = getenv_IPaddr ("serverip");
428 DhcpRequest(); /* Basically same as BOOTP */
441 #if defined(CONFIG_CMD_PING)
446 #if defined(CONFIG_CMD_NFS)
451 #if defined(CONFIG_CMD_CDP)
456 #ifdef CONFIG_NETCONSOLE
461 #if defined(CONFIG_CMD_SNTP)
470 NetBootFileXferSize = 0;
474 #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
475 #if defined(CFG_FAULT_ECHO_LINK_DOWN) && defined(CONFIG_STATUS_LED) && defined(STATUS_LED_RED)
477 * Echo the inverted link state to the fault LED.
479 if(miiphy_link(eth_get_dev()->name, CFG_FAULT_MII_ADDR)) {
480 status_led_set (STATUS_LED_RED, STATUS_LED_OFF);
482 status_led_set (STATUS_LED_RED, STATUS_LED_ON);
484 #endif /* CFG_FAULT_ECHO_LINK_DOWN, ... */
485 #endif /* CONFIG_MII, ... */
488 * Main packet reception loop. Loop receiving packets until
489 * someone sets `NetState' to a state that terminates.
493 #ifdef CONFIG_SHOW_ACTIVITY
495 extern void show_activity(int arg);
500 * Check the ethernet for a new packet. The ethernet
501 * receive routine will process it.
506 * Abort if ctrl-c was pressed.
517 * Check for a timeout, and run the timeout handler
520 if (timeHandler && ((get_timer(0) - timeStart) > timeDelta)) {
523 #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
524 # if defined(CFG_FAULT_ECHO_LINK_DOWN) && \
525 defined(CONFIG_STATUS_LED) && \
526 defined(STATUS_LED_RED)
528 * Echo the inverted link state to the fault LED.
530 if(miiphy_link(eth_get_dev()->name, CFG_FAULT_MII_ADDR)) {
531 status_led_set (STATUS_LED_RED, STATUS_LED_OFF);
533 status_led_set (STATUS_LED_RED, STATUS_LED_ON);
535 # endif /* CFG_FAULT_ECHO_LINK_DOWN, ... */
536 #endif /* CONFIG_MII, ... */
538 timeHandler = (thand_f *)0;
545 case NETLOOP_RESTART:
546 #ifdef CONFIG_NET_MULTI
551 case NETLOOP_SUCCESS:
552 if (NetBootFileXferSize > 0) {
554 printf("Bytes transferred = %ld (%lx hex)\n",
556 NetBootFileXferSize);
557 sprintf(buf, "%lX", NetBootFileXferSize);
558 setenv("filesize", buf);
560 sprintf(buf, "%lX", (unsigned long)load_addr);
561 setenv("fileaddr", buf);
564 return NetBootFileXferSize;
572 /**********************************************************************/
575 startAgainTimeout(void)
577 NetState = NETLOOP_RESTART;
581 startAgainHandler(uchar * pkt, unsigned dest, unsigned src, unsigned len)
583 /* Totally ignore the packet */
586 void NetStartAgain (void)
589 int noretry = 0, once = 0;
591 if ((nretry = getenv ("netretry")) != NULL) {
592 noretry = (strcmp (nretry, "no") == 0);
593 once = (strcmp (nretry, "once") == 0);
597 NetState = NETLOOP_FAIL;
600 #ifndef CONFIG_NET_MULTI
601 NetSetTimeout (10000UL, startAgainTimeout);
602 NetSetHandler (startAgainHandler);
603 #else /* !CONFIG_NET_MULTI*/
605 #if !defined(CONFIG_NET_DO_NOT_TRY_ANOTHER)
606 eth_try_another (!NetRestarted);
609 if (NetRestartWrap) {
611 if (NetDevExists && !once) {
612 NetSetTimeout (10000UL, startAgainTimeout);
613 NetSetHandler (startAgainHandler);
615 NetState = NETLOOP_FAIL;
618 NetState = NETLOOP_RESTART;
620 #endif /* CONFIG_NET_MULTI */
623 /**********************************************************************/
629 NetSetHandler(rxhand_f * f)
636 NetSetTimeout(ulong iv, thand_f * f)
639 timeHandler = (thand_f *)0;
642 timeStart = get_timer(0);
649 NetSendPacket(volatile uchar * pkt, int len)
651 (void) eth_send(pkt, len);
655 NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport, int sport, int len)
659 /* convert to new style broadcast */
663 /* if broadcast, make the ether address a broadcast and don't do ARP */
664 if (dest == 0xFFFFFFFF)
665 ether = NetBcastAddr;
667 /* if MAC address was not discovered yet, save the packet and do an ARP request */
668 if (memcmp(ether, NetEtherNullAddr, 6) == 0) {
671 printf("sending ARP for %08lx\n", dest);
673 NetArpWaitPacketIP = dest;
674 NetArpWaitPacketMAC = ether;
676 pkt = NetArpWaitTxPacket;
677 pkt += NetSetEther (pkt, NetArpWaitPacketMAC, PROT_IP);
679 NetSetIP (pkt, dest, dport, sport, len);
680 memcpy(pkt + IP_HDR_SIZE, (uchar *)NetTxPacket + (pkt - (uchar *)NetArpWaitTxPacket) + IP_HDR_SIZE, len);
682 /* size of the waiting packet */
683 NetArpWaitTxPacketSize = (pkt - NetArpWaitTxPacket) + IP_HDR_SIZE + len;
685 /* and do the ARP request */
687 NetArpWaitTimerStart = get_timer(0);
689 return 1; /* waiting */
693 printf("sending UDP to %08lx/%02x:%02x:%02x:%02x:%02x:%02x\n",
694 dest, ether[0], ether[1], ether[2], ether[3], ether[4], ether[5]);
697 pkt = (uchar *)NetTxPacket;
698 pkt += NetSetEther (pkt, ether, PROT_IP);
699 NetSetIP (pkt, dest, dport, sport, len);
700 (void) eth_send(NetTxPacket, (pkt - NetTxPacket) + IP_HDR_SIZE + len);
702 return 0; /* transmitted */
705 #if defined(CONFIG_CMD_PING)
706 static ushort PingSeqNo;
715 /* XXX always send arp request */
717 memcpy(mac, NetEtherNullAddr, 6);
720 printf("sending ARP for %08lx\n", NetPingIP);
723 NetArpWaitPacketIP = NetPingIP;
724 NetArpWaitPacketMAC = mac;
726 pkt = NetArpWaitTxPacket;
727 pkt += NetSetEther(pkt, mac, PROT_IP);
729 ip = (volatile IP_t *)pkt;
732 * Construct an IP and ICMP header. (need to set no fragment bit - XXX)
734 ip->ip_hl_v = 0x45; /* IP_HDR_SIZE / 4 (not including UDP) */
736 ip->ip_len = htons(IP_HDR_SIZE_NO_UDP + 8);
737 ip->ip_id = htons(NetIPID++);
738 ip->ip_off = htons(0x4000); /* No fragmentation */
740 ip->ip_p = 0x01; /* ICMP */
742 NetCopyIP((void*)&ip->ip_src, &NetOurIP); /* already in network byte order */
743 NetCopyIP((void*)&ip->ip_dst, &NetPingIP); /* - "" - */
744 ip->ip_sum = ~NetCksum((uchar *)ip, IP_HDR_SIZE_NO_UDP / 2);
746 s = &ip->udp_src; /* XXX ICMP starts here */
747 s[0] = htons(0x0800); /* echo-request, code */
748 s[1] = 0; /* checksum */
749 s[2] = 0; /* identifier */
750 s[3] = htons(PingSeqNo++); /* sequence number */
751 s[1] = ~NetCksum((uchar *)s, 8/2);
753 /* size of the waiting packet */
754 NetArpWaitTxPacketSize = (pkt - NetArpWaitTxPacket) + IP_HDR_SIZE_NO_UDP + 8;
756 /* and do the ARP request */
758 NetArpWaitTimerStart = get_timer(0);
760 return 1; /* waiting */
767 NetState = NETLOOP_FAIL; /* we did not get the reply */
771 PingHandler (uchar * pkt, unsigned dest, unsigned src, unsigned len)
774 volatile IP_t *ip = (volatile IP_t *)pkt;
776 tmp = NetReadIP((void *)&ip->ip_src);
777 if (tmp != NetPingIP)
780 NetState = NETLOOP_SUCCESS;
783 static void PingStart(void)
785 #if defined(CONFIG_NET_MULTI)
786 printf ("Using %s device\n", eth_get_name());
787 #endif /* CONFIG_NET_MULTI */
788 NetSetTimeout (10000UL, PingTimeout);
789 NetSetHandler (PingHandler);
795 #if defined(CONFIG_CMD_CDP)
797 #define CDP_DEVICE_ID_TLV 0x0001
798 #define CDP_ADDRESS_TLV 0x0002
799 #define CDP_PORT_ID_TLV 0x0003
800 #define CDP_CAPABILITIES_TLV 0x0004
801 #define CDP_VERSION_TLV 0x0005
802 #define CDP_PLATFORM_TLV 0x0006
803 #define CDP_NATIVE_VLAN_TLV 0x000a
804 #define CDP_APPLIANCE_VLAN_TLV 0x000e
805 #define CDP_TRIGGER_TLV 0x000f
806 #define CDP_POWER_CONSUMPTION_TLV 0x0010
807 #define CDP_SYSNAME_TLV 0x0014
808 #define CDP_SYSOBJECT_TLV 0x0015
809 #define CDP_MANAGEMENT_ADDRESS_TLV 0x0016
811 #define CDP_TIMEOUT 250UL /* one packet every 250ms */
816 ushort CDPNativeVLAN;
817 ushort CDPApplianceVLAN;
819 static const uchar CDP_SNAP_hdr[8] = { 0xAA, 0xAA, 0x03, 0x00, 0x00, 0x0C, 0x20, 0x00 };
821 static ushort CDP_compute_csum(const uchar *buff, ushort len)
830 odd = 1 & (ulong)buff;
840 if (result & 0x80000000)
841 result = (result & 0xFFFF) + (result >> 16);
845 leftover = (signed short)(*(const signed char *)buff);
846 /* CISCO SUCKS big time! (and blows too):
847 * CDP uses the IP checksum algorithm with a twist;
848 * for the last byte it *sign* extends and sums.
850 result = (result & 0xffff0000) | ((result + leftover) & 0x0000ffff);
853 result = (result & 0xFFFF) + (result >> 16);
856 result = ((result >> 8) & 0xff) | ((result & 0xff) << 8);
859 /* add up 16-bit and 17-bit words for 17+c bits */
860 result = (result & 0xffff) + (result >> 16);
861 /* add up 16-bit and 2-bit for 16+c bit */
862 result = (result & 0xffff) + (result >> 16);
864 result = (result & 0xffff) + (result >> 16);
867 csum = ~(ushort)result;
869 /* run time endian detection */
870 if (csum != htons(csum)) /* little endian */
876 int CDPSendTrigger(void)
884 #if defined(CONFIG_CDP_DEVICE_ID) || defined(CONFIG_CDP_PORT_ID) || \
885 defined(CONFIG_CDP_VERSION) || defined(CONFIG_CDP_PLATFORM)
890 et = (Ethernet_t *)pkt;
892 /* NOTE: trigger sent not on any VLAN */
894 /* form ethernet header */
895 memcpy(et->et_dest, NetCDPAddr, 6);
896 memcpy(et->et_src, NetOurEther, 6);
898 pkt += ETHER_HDR_SIZE;
901 memcpy((uchar *)pkt, CDP_SNAP_hdr, sizeof(CDP_SNAP_hdr));
902 pkt += sizeof(CDP_SNAP_hdr);
905 *pkt++ = 0x02; /* CDP version 2 */
906 *pkt++ = 180; /* TTL */
907 s = (volatile ushort *)pkt;
909 *s++ = htons(0); /* checksum (0 for later calculation) */
912 #ifdef CONFIG_CDP_DEVICE_ID
913 *s++ = htons(CDP_DEVICE_ID_TLV);
914 *s++ = htons(CONFIG_CDP_DEVICE_ID);
915 memset(buf, 0, sizeof(buf));
916 sprintf(buf, CONFIG_CDP_DEVICE_ID_PREFIX "%02X%02X%02X%02X%02X%02X",
917 NetOurEther[0] & 0xff, NetOurEther[1] & 0xff,
918 NetOurEther[2] & 0xff, NetOurEther[3] & 0xff,
919 NetOurEther[4] & 0xff, NetOurEther[5] & 0xff);
920 memcpy((uchar *)s, buf, 16);
924 #ifdef CONFIG_CDP_PORT_ID
925 *s++ = htons(CDP_PORT_ID_TLV);
926 memset(buf, 0, sizeof(buf));
927 sprintf(buf, CONFIG_CDP_PORT_ID, eth_get_dev_index());
929 if (len & 1) /* make it even */
931 *s++ = htons(len + 4);
932 memcpy((uchar *)s, buf, len);
936 #ifdef CONFIG_CDP_CAPABILITIES
937 *s++ = htons(CDP_CAPABILITIES_TLV);
939 *(ulong *)s = htonl(CONFIG_CDP_CAPABILITIES);
943 #ifdef CONFIG_CDP_VERSION
944 *s++ = htons(CDP_VERSION_TLV);
945 memset(buf, 0, sizeof(buf));
946 strcpy(buf, CONFIG_CDP_VERSION);
948 if (len & 1) /* make it even */
950 *s++ = htons(len + 4);
951 memcpy((uchar *)s, buf, len);
955 #ifdef CONFIG_CDP_PLATFORM
956 *s++ = htons(CDP_PLATFORM_TLV);
957 memset(buf, 0, sizeof(buf));
958 strcpy(buf, CONFIG_CDP_PLATFORM);
960 if (len & 1) /* make it even */
962 *s++ = htons(len + 4);
963 memcpy((uchar *)s, buf, len);
967 #ifdef CONFIG_CDP_TRIGGER
968 *s++ = htons(CDP_TRIGGER_TLV);
970 *(ulong *)s = htonl(CONFIG_CDP_TRIGGER);
974 #ifdef CONFIG_CDP_POWER_CONSUMPTION
975 *s++ = htons(CDP_POWER_CONSUMPTION_TLV);
977 *s++ = htons(CONFIG_CDP_POWER_CONSUMPTION);
980 /* length of ethernet packet */
981 len = (uchar *)s - ((uchar *)NetTxPacket + ETHER_HDR_SIZE);
982 et->et_protlen = htons(len);
984 len = ETHER_HDR_SIZE + sizeof(CDP_SNAP_hdr);
985 chksum = CDP_compute_csum((uchar *)NetTxPacket + len, (uchar *)s - (NetTxPacket + len));
990 (void) eth_send(NetTxPacket, (uchar *)s - NetTxPacket);
1000 NetSetTimeout (CDP_TIMEOUT, CDPTimeout);
1005 /* if not OK try again */
1009 NetState = NETLOOP_SUCCESS;
1013 CDPDummyHandler (uchar * pkt, unsigned dest, unsigned src, unsigned len)
1019 CDPHandler(const uchar * pkt, unsigned len)
1028 if (len < sizeof(CDP_SNAP_hdr) + 4)
1031 /* check for valid CDP SNAP header */
1032 if (memcmp(pkt, CDP_SNAP_hdr, sizeof(CDP_SNAP_hdr)) != 0)
1035 pkt += sizeof(CDP_SNAP_hdr);
1036 len -= sizeof(CDP_SNAP_hdr);
1038 /* Version of CDP protocol must be >= 2 and TTL != 0 */
1039 if (pkt[0] < 0x02 || pkt[1] == 0)
1042 /* if version is greater than 0x02 maybe we'll have a problem; output a warning */
1044 printf("** WARNING: CDP packet received with a protocol version %d > 2\n",
1047 if (CDP_compute_csum(pkt, len) != 0)
1059 ss = (const ushort *)pkt;
1060 type = ntohs(ss[0]);
1061 tlen = ntohs(ss[1]);
1069 ss += 2; /* point ss to the data of the TLV */
1073 case CDP_DEVICE_ID_TLV:
1075 case CDP_ADDRESS_TLV:
1077 case CDP_PORT_ID_TLV:
1079 case CDP_CAPABILITIES_TLV:
1081 case CDP_VERSION_TLV:
1083 case CDP_PLATFORM_TLV:
1085 case CDP_NATIVE_VLAN_TLV:
1088 case CDP_APPLIANCE_VLAN_TLV:
1089 t = (const uchar *)ss;
1095 ss = (const ushort *)(t + 1);
1097 #ifdef CONFIG_CDP_APPLIANCE_VLAN_TYPE
1098 if (applid == CONFIG_CDP_APPLIANCE_VLAN_TYPE)
1101 vlan = ntohs(*ss); /* XXX will this work; dunno */
1106 case CDP_TRIGGER_TLV:
1108 case CDP_POWER_CONSUMPTION_TLV:
1110 case CDP_SYSNAME_TLV:
1112 case CDP_SYSOBJECT_TLV:
1114 case CDP_MANAGEMENT_ADDRESS_TLV:
1119 CDPApplianceVLAN = vlan;
1120 CDPNativeVLAN = nvlan;
1126 printf("** CDP packet is too short\n");
1130 static void CDPStart(void)
1132 #if defined(CONFIG_NET_MULTI)
1133 printf ("Using %s device\n", eth_get_name());
1138 CDPNativeVLAN = htons(-1);
1139 CDPApplianceVLAN = htons(-1);
1141 NetSetTimeout (CDP_TIMEOUT, CDPTimeout);
1142 NetSetHandler (CDPDummyHandler);
1150 NetReceive(volatile uchar * inpkt, int len)
1158 #if defined(CONFIG_CMD_CDP)
1161 ushort cti = 0, vlanid = VLAN_NONE, myvlanid, mynvlanid;
1164 printf("packet received\n");
1169 et = (Ethernet_t *)inpkt;
1171 /* too small packet? */
1172 if (len < ETHER_HDR_SIZE)
1177 (*push_packet)(inpkt, len);
1182 #if defined(CONFIG_CMD_CDP)
1183 /* keep track if packet is CDP */
1184 iscdp = memcmp(et->et_dest, NetCDPAddr, 6) == 0;
1187 myvlanid = ntohs(NetOurVLAN);
1188 if (myvlanid == (ushort)-1)
1189 myvlanid = VLAN_NONE;
1190 mynvlanid = ntohs(NetOurNativeVLAN);
1191 if (mynvlanid == (ushort)-1)
1192 mynvlanid = VLAN_NONE;
1194 x = ntohs(et->et_protlen);
1197 printf("packet received\n");
1202 * Got a 802 packet. Check the other protocol field.
1204 x = ntohs(et->et_prot);
1206 ip = (IP_t *)(inpkt + E802_HDR_SIZE);
1207 len -= E802_HDR_SIZE;
1209 } else if (x != PROT_VLAN) { /* normal packet */
1210 ip = (IP_t *)(inpkt + ETHER_HDR_SIZE);
1211 len -= ETHER_HDR_SIZE;
1213 } else { /* VLAN packet */
1214 VLAN_Ethernet_t *vet = (VLAN_Ethernet_t *)et;
1217 printf("VLAN packet received\n");
1219 /* too small packet? */
1220 if (len < VLAN_ETHER_HDR_SIZE)
1223 /* if no VLAN active */
1224 if ((ntohs(NetOurVLAN) & VLAN_IDMASK) == VLAN_NONE
1225 #if defined(CONFIG_CMD_CDP)
1231 cti = ntohs(vet->vet_tag);
1232 vlanid = cti & VLAN_IDMASK;
1233 x = ntohs(vet->vet_type);
1235 ip = (IP_t *)(inpkt + VLAN_ETHER_HDR_SIZE);
1236 len -= VLAN_ETHER_HDR_SIZE;
1240 printf("Receive from protocol 0x%x\n", x);
1243 #if defined(CONFIG_CMD_CDP)
1245 CDPHandler((uchar *)ip, len);
1250 if ((myvlanid & VLAN_IDMASK) != VLAN_NONE) {
1251 if (vlanid == VLAN_NONE)
1252 vlanid = (mynvlanid & VLAN_IDMASK);
1254 if (vlanid != (myvlanid & VLAN_IDMASK))
1262 * We have to deal with two types of ARP packets:
1263 * - REQUEST packets will be answered by sending our
1264 * IP address - if we know it.
1265 * - REPLY packates are expected only after we asked
1266 * for the TFTP server's or the gateway's ethernet
1267 * address; so if we receive such a packet, we set
1268 * the server ethernet address
1274 if (len < ARP_HDR_SIZE) {
1275 printf("bad length %d < %d\n", len, ARP_HDR_SIZE);
1278 if (ntohs(arp->ar_hrd) != ARP_ETHER) {
1281 if (ntohs(arp->ar_pro) != PROT_IP) {
1284 if (arp->ar_hln != 6) {
1287 if (arp->ar_pln != 4) {
1291 if (NetOurIP == 0) {
1295 if (NetReadIP(&arp->ar_data[16]) != NetOurIP) {
1299 switch (ntohs(arp->ar_op)) {
1300 case ARPOP_REQUEST: /* reply with our IP address */
1302 puts ("Got ARP REQUEST, return our IP\n");
1305 pkt += NetSetEther(pkt, et->et_src, PROT_ARP);
1306 arp->ar_op = htons(ARPOP_REPLY);
1307 memcpy (&arp->ar_data[10], &arp->ar_data[0], 6);
1308 NetCopyIP(&arp->ar_data[16], &arp->ar_data[6]);
1309 memcpy (&arp->ar_data[ 0], NetOurEther, 6);
1310 NetCopyIP(&arp->ar_data[ 6], &NetOurIP);
1311 (void) eth_send((uchar *)et, (pkt - (uchar *)et) + ARP_HDR_SIZE);
1314 case ARPOP_REPLY: /* arp reply */
1315 /* are we waiting for a reply */
1316 if (!NetArpWaitPacketIP || !NetArpWaitPacketMAC)
1319 printf("Got ARP REPLY, set server/gtwy eth addr (%02x:%02x:%02x:%02x:%02x:%02x)\n",
1320 arp->ar_data[0], arp->ar_data[1],
1321 arp->ar_data[2], arp->ar_data[3],
1322 arp->ar_data[4], arp->ar_data[5]);
1325 tmp = NetReadIP(&arp->ar_data[6]);
1327 /* matched waiting packet's address */
1328 if (tmp == NetArpWaitReplyIP) {
1332 /* save address for later use */
1333 memcpy(NetArpWaitPacketMAC, &arp->ar_data[0], 6);
1335 #ifdef CONFIG_NETCONSOLE
1336 (*packetHandler)(0,0,0,0);
1338 /* modify header, and transmit it */
1339 memcpy(((Ethernet_t *)NetArpWaitTxPacket)->et_dest, NetArpWaitPacketMAC, 6);
1340 (void) eth_send(NetArpWaitTxPacket, NetArpWaitTxPacketSize);
1342 /* no arp request pending now */
1343 NetArpWaitPacketIP = 0;
1344 NetArpWaitTxPacketSize = 0;
1345 NetArpWaitPacketMAC = NULL;
1351 printf("Unexpected ARP opcode 0x%x\n", ntohs(arp->ar_op));
1359 puts ("Got RARP\n");
1362 if (len < ARP_HDR_SIZE) {
1363 printf("bad length %d < %d\n", len, ARP_HDR_SIZE);
1367 if ((ntohs(arp->ar_op) != RARPOP_REPLY) ||
1368 (ntohs(arp->ar_hrd) != ARP_ETHER) ||
1369 (ntohs(arp->ar_pro) != PROT_IP) ||
1370 (arp->ar_hln != 6) || (arp->ar_pln != 4)) {
1372 puts ("invalid RARP header\n");
1374 NetCopyIP(&NetOurIP, &arp->ar_data[16]);
1375 if (NetServerIP == 0)
1376 NetCopyIP(&NetServerIP, &arp->ar_data[ 6]);
1377 memcpy (NetServerEther, &arp->ar_data[ 0], 6);
1379 (*packetHandler)(0,0,0,0);
1387 if (len < IP_HDR_SIZE) {
1388 debug ("len bad %d < %lu\n", len, (ulong)IP_HDR_SIZE);
1391 if (len < ntohs(ip->ip_len)) {
1392 printf("len bad %d < %d\n", len, ntohs(ip->ip_len));
1395 len = ntohs(ip->ip_len);
1397 printf("len=%d, v=%02x\n", len, ip->ip_hl_v & 0xff);
1399 if ((ip->ip_hl_v & 0xf0) != 0x40) {
1402 if (ip->ip_off & htons(0x1fff)) { /* Can't deal w/ fragments */
1405 /* can't deal with headers > 20 bytes */
1406 if ((ip->ip_hl_v & 0x0f) > 0x05) {
1409 if (!NetCksumOk((uchar *)ip, IP_HDR_SIZE_NO_UDP / 2)) {
1410 puts ("checksum bad\n");
1413 tmp = NetReadIP(&ip->ip_dst);
1414 if (NetOurIP && tmp != NetOurIP && tmp != 0xFFFFFFFF) {
1415 #ifdef CONFIG_MCAST_TFTP
1416 if (Mcast_addr != tmp)
1421 * watch for ICMP host redirects
1423 * There is no real handler code (yet). We just watch
1424 * for ICMP host redirect messages. In case anybody
1425 * sees these messages: please contact me
1426 * (wd@denx.de), or - even better - send me the
1427 * necessary fixes :-)
1429 * Note: in all cases where I have seen this so far
1430 * it was a problem with the router configuration,
1431 * for instance when a router was configured in the
1432 * BOOTP reply, but the TFTP server was on the same
1433 * subnet. So this is probably a warning that your
1434 * configuration might be wrong. But I'm not really
1435 * sure if there aren't any other situations.
1437 if (ip->ip_p == IPPROTO_ICMP) {
1438 ICMP_t *icmph = (ICMP_t *)&(ip->udp_src);
1440 switch (icmph->type) {
1442 if (icmph->code != ICMP_REDIR_HOST)
1444 puts (" ICMP Host Redirect to ");
1445 print_IPaddr(icmph->un.gateway);
1448 #if defined(CONFIG_CMD_PING)
1449 case ICMP_ECHO_REPLY:
1451 * IP header OK. Pass the packet to the current handler.
1453 /* XXX point to ip packet */
1454 (*packetHandler)((uchar *)ip, 0, 0, 0);
1456 case ICMP_ECHO_REQUEST:
1458 printf ("Got ICMP ECHO REQUEST, return %d bytes \n",
1459 ETHER_HDR_SIZE + len);
1461 memcpy (&et->et_dest[0], &et->et_src[0], 6);
1462 memcpy (&et->et_src[ 0], NetOurEther, 6);
1466 NetCopyIP((void*)&ip->ip_dst, &ip->ip_src);
1467 NetCopyIP((void*)&ip->ip_src, &NetOurIP);
1468 ip->ip_sum = ~NetCksum((uchar *)ip, IP_HDR_SIZE_NO_UDP >> 1);
1470 icmph->type = ICMP_ECHO_REPLY;
1471 icmph->checksum = 0;
1472 icmph->checksum = ~NetCksum((uchar *)icmph,
1473 (len - IP_HDR_SIZE_NO_UDP) >> 1);
1474 (void) eth_send((uchar *)et, ETHER_HDR_SIZE + len);
1480 } else if (ip->ip_p != IPPROTO_UDP) { /* Only UDP packets */
1484 #ifdef CONFIG_UDP_CHECKSUM
1485 if (ip->udp_xsum != 0) {
1491 xsum += (ntohs(ip->udp_len));
1492 xsum += (ntohl(ip->ip_src) >> 16) & 0x0000ffff;
1493 xsum += (ntohl(ip->ip_src) >> 0) & 0x0000ffff;
1494 xsum += (ntohl(ip->ip_dst) >> 16) & 0x0000ffff;
1495 xsum += (ntohl(ip->ip_dst) >> 0) & 0x0000ffff;
1497 sumlen = ntohs(ip->udp_len);
1498 sumptr = (ushort *) &(ip->udp_src);
1500 while (sumlen > 1) {
1503 sumdata = *sumptr++;
1504 xsum += ntohs(sumdata);
1510 sumdata = *(unsigned char *) sumptr;
1511 sumdata = (sumdata << 8) & 0xff00;
1514 while ((xsum >> 16) != 0) {
1515 xsum = (xsum & 0x0000ffff) + ((xsum >> 16) & 0x0000ffff);
1517 if ((xsum != 0x00000000) && (xsum != 0x0000ffff)) {
1518 printf(" UDP wrong checksum %08lx %08x\n",
1519 xsum, ntohs(ip->udp_xsum));
1526 #ifdef CONFIG_NETCONSOLE
1527 nc_input_packet((uchar *)ip +IP_HDR_SIZE,
1530 ntohs(ip->udp_len) - 8);
1533 * IP header OK. Pass the packet to the current handler.
1535 (*packetHandler)((uchar *)ip +IP_HDR_SIZE,
1538 ntohs(ip->udp_len) - 8);
1544 /**********************************************************************/
1546 static int net_check_prereq (proto_t protocol)
1550 #if defined(CONFIG_CMD_PING)
1552 if (NetPingIP == 0) {
1553 puts ("*** ERROR: ping address not given\n");
1558 #if defined(CONFIG_CMD_SNTP)
1560 if (NetNtpServerIP == 0) {
1561 puts ("*** ERROR: NTP server address not given\n");
1566 #if defined(CONFIG_CMD_NFS)
1571 if (NetServerIP == 0) {
1572 puts ("*** ERROR: `serverip' not set\n");
1575 #if defined(CONFIG_CMD_PING) || defined(CONFIG_CMD_SNTP)
1579 if (NetOurIP == 0) {
1580 puts ("*** ERROR: `ipaddr' not set\n");
1589 if (memcmp (NetOurEther, "\0\0\0\0\0\0", 6) == 0) {
1590 #ifdef CONFIG_NET_MULTI
1591 extern int eth_get_dev_index (void);
1592 int num = eth_get_dev_index ();
1596 puts ("*** ERROR: No ethernet found.\n");
1599 puts ("*** ERROR: `ethaddr' not set\n");
1602 printf ("*** ERROR: `eth%daddr' not set\n",
1610 puts ("*** ERROR: `ethaddr' not set\n");
1618 return (0); /* OK */
1620 /**********************************************************************/
1623 NetCksumOk(uchar * ptr, int len)
1625 return !((NetCksum(ptr, len) + 1) & 0xfffe);
1630 NetCksum(uchar * ptr, int len)
1633 ushort *p = (ushort *)ptr;
1638 xsum = (xsum & 0xffff) + (xsum >> 16);
1639 xsum = (xsum & 0xffff) + (xsum >> 16);
1640 return (xsum & 0xffff);
1648 myvlanid = ntohs(NetOurVLAN);
1649 if (myvlanid == (ushort)-1)
1650 myvlanid = VLAN_NONE;
1652 return ((myvlanid & VLAN_IDMASK) == VLAN_NONE) ? ETHER_HDR_SIZE : VLAN_ETHER_HDR_SIZE;
1656 NetSetEther(volatile uchar * xet, uchar * addr, uint prot)
1658 Ethernet_t *et = (Ethernet_t *)xet;
1661 myvlanid = ntohs(NetOurVLAN);
1662 if (myvlanid == (ushort)-1)
1663 myvlanid = VLAN_NONE;
1665 memcpy (et->et_dest, addr, 6);
1666 memcpy (et->et_src, NetOurEther, 6);
1667 if ((myvlanid & VLAN_IDMASK) == VLAN_NONE) {
1668 et->et_protlen = htons(prot);
1669 return ETHER_HDR_SIZE;
1671 VLAN_Ethernet_t *vet = (VLAN_Ethernet_t *)xet;
1673 vet->vet_vlan_type = htons(PROT_VLAN);
1674 vet->vet_tag = htons((0 << 5) | (myvlanid & VLAN_IDMASK));
1675 vet->vet_type = htons(prot);
1676 return VLAN_ETHER_HDR_SIZE;
1681 NetSetIP(volatile uchar * xip, IPaddr_t dest, int dport, int sport, int len)
1683 volatile IP_t *ip = (IP_t *)xip;
1686 * If the data is an odd number of bytes, zero the
1687 * byte after the last byte so that the checksum
1691 xip[IP_HDR_SIZE + len] = 0;
1694 * Construct an IP and UDP header.
1695 * (need to set no fragment bit - XXX)
1697 ip->ip_hl_v = 0x45; /* IP_HDR_SIZE / 4 (not including UDP) */
1699 ip->ip_len = htons(IP_HDR_SIZE + len);
1700 ip->ip_id = htons(NetIPID++);
1701 ip->ip_off = htons(0x4000); /* No fragmentation */
1703 ip->ip_p = 17; /* UDP */
1705 NetCopyIP((void*)&ip->ip_src, &NetOurIP); /* already in network byte order */
1706 NetCopyIP((void*)&ip->ip_dst, &dest); /* - "" - */
1707 ip->udp_src = htons(sport);
1708 ip->udp_dst = htons(dport);
1709 ip->udp_len = htons(8 + len);
1711 ip->ip_sum = ~NetCksum((uchar *)ip, IP_HDR_SIZE_NO_UDP / 2);
1714 void copy_filename (char *dst, char *src, int size)
1716 if (*src && (*src == '"')) {
1721 while ((--size > 0) && *src && (*src != '"')) {
1729 void ip_to_string (IPaddr_t x, char *s)
1732 sprintf (s, "%d.%d.%d.%d",
1733 (int) ((x >> 24) & 0xff),
1734 (int) ((x >> 16) & 0xff),
1735 (int) ((x >> 8) & 0xff), (int) ((x >> 0) & 0xff)
1739 IPaddr_t string_to_ip(char *s)
1748 for (addr=0, i=0; i<4; ++i) {
1749 ulong val = s ? simple_strtoul(s, &e, 10) : 0;
1751 addr |= (val & 0xFF);
1757 return (htonl(addr));
1760 void VLAN_to_string(ushort x, char *s)
1764 if (x == (ushort)-1)
1770 sprintf(s, "%d", x & VLAN_IDMASK);
1773 ushort string_to_VLAN(char *s)
1778 return htons(VLAN_NONE);
1780 if (*s < '0' || *s > '9')
1783 id = (ushort)simple_strtoul(s, NULL, 10);
1788 void print_IPaddr (IPaddr_t x)
1792 ip_to_string (x, tmp);
1797 IPaddr_t getenv_IPaddr (char *var)
1799 return (string_to_ip(getenv(var)));
1802 ushort getenv_VLAN(char *var)
1804 return (string_to_VLAN(getenv(var)));