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 void ArpRequest (void)
219 printf ("ARP broadcast %d\n", NetArpWaitTry);
223 pkt += NetSetEther (pkt, NetBcastAddr, PROT_ARP);
227 arp->ar_hrd = htons (ARP_ETHER);
228 arp->ar_pro = htons (PROT_IP);
231 arp->ar_op = htons (ARPOP_REQUEST);
233 memcpy (&arp->ar_data[0], NetOurEther, 6); /* source ET addr */
234 NetWriteIP ((uchar *) & arp->ar_data[6], NetOurIP); /* source IP addr */
235 for (i = 10; i < 16; ++i) {
236 arp->ar_data[i] = 0; /* dest ET addr = 0 */
239 if ((NetArpWaitPacketIP & NetOurSubnetMask) !=
240 (NetOurIP & NetOurSubnetMask)) {
241 if (NetOurGatewayIP == 0) {
242 puts ("## Warning: gatewayip needed but not set\n");
243 NetArpWaitReplyIP = NetArpWaitPacketIP;
245 NetArpWaitReplyIP = NetOurGatewayIP;
248 NetArpWaitReplyIP = NetArpWaitPacketIP;
251 NetWriteIP ((uchar *) & arp->ar_data[16], NetArpWaitReplyIP);
252 (void) eth_send (NetTxPacket, (pkt - NetTxPacket) + ARP_HDR_SIZE);
255 void ArpTimeoutCheck(void)
259 if (!NetArpWaitPacketIP)
264 /* check for arp timeout */
265 if ((t - NetArpWaitTimerStart) > ARP_TIMEOUT) {
268 if (NetArpWaitTry >= ARP_TIMEOUT_COUNT) {
269 puts ("\nARP Retry count exceeded; starting again\n");
273 NetArpWaitTimerStart = t;
279 /**********************************************************************/
281 * Main network processing loop.
285 NetLoop(proto_t protocol)
289 #ifdef CONFIG_NET_MULTI
294 /* XXX problem with bss workaround */
295 NetArpWaitPacketMAC = NULL;
296 NetArpWaitTxPacket = NULL;
297 NetArpWaitPacketIP = 0;
298 NetArpWaitReplyIP = 0;
299 NetArpWaitTxPacket = NULL;
305 * Setup packet buffers, aligned correctly.
307 NetTxPacket = &PktBuf[0] + (PKTALIGN - 1);
308 NetTxPacket -= (ulong)NetTxPacket % PKTALIGN;
309 for (i = 0; i < PKTBUFSRX; i++) {
310 NetRxPackets[i] = NetTxPacket + (i+1)*PKTSIZE_ALIGN;
314 if (!NetArpWaitTxPacket) {
315 NetArpWaitTxPacket = &NetArpWaitPacketBuf[0] + (PKTALIGN - 1);
316 NetArpWaitTxPacket -= (ulong)NetArpWaitTxPacket % PKTALIGN;
317 NetArpWaitTxPacketSize = 0;
321 #ifdef CONFIG_NET_MULTI
324 if (eth_init(bd) < 0) {
330 #ifdef CONFIG_NET_MULTI
331 memcpy (NetOurEther, eth_get_dev()->enetaddr, 6);
333 memcpy (NetOurEther, bd->bi_enetaddr, 6);
336 NetState = NETLOOP_CONTINUE;
339 * Start the ball rolling with the given start function. From
340 * here on, this code is a state machine driven by received
341 * packets and timer events.
345 #if defined(CONFIG_CMD_NFS)
348 #if defined(CONFIG_CMD_PING)
351 #if defined(CONFIG_CMD_SNTP)
356 NetCopyIP(&NetOurIP, &bd->bi_ip_addr);
357 NetOurGatewayIP = getenv_IPaddr ("gatewayip");
358 NetOurSubnetMask= getenv_IPaddr ("netmask");
359 NetOurVLAN = getenv_VLAN("vlan");
360 NetOurNativeVLAN = getenv_VLAN("nvlan");
363 #if defined(CONFIG_CMD_NFS)
368 NetServerIP = getenv_IPaddr ("serverip");
370 #if defined(CONFIG_CMD_PING)
375 #if defined(CONFIG_CMD_SNTP)
388 * initialize our IP addr to 0 in order to accept ANY
389 * IP addr assigned to us by the BOOTP / RARP server
392 NetServerIP = getenv_IPaddr ("serverip");
393 NetOurVLAN = getenv_VLAN("vlan"); /* VLANs must be read */
394 NetOurNativeVLAN = getenv_VLAN("nvlan");
396 NetOurVLAN = getenv_VLAN("vlan"); /* VLANs must be read */
397 NetOurNativeVLAN = getenv_VLAN("nvlan");
403 switch (net_check_prereq (protocol)) {
405 /* network not configured */
409 #ifdef CONFIG_NET_MULTI
411 /* network device not configured */
413 #endif /* CONFIG_NET_MULTI */
416 #ifdef CONFIG_NET_MULTI
421 /* always use ARP to get server ethernet address */
425 #if defined(CONFIG_CMD_DHCP)
427 /* Start with a clean slate... */
430 NetServerIP = getenv_IPaddr ("serverip");
431 DhcpRequest(); /* Basically same as BOOTP */
444 #if defined(CONFIG_CMD_PING)
449 #if defined(CONFIG_CMD_NFS)
454 #if defined(CONFIG_CMD_CDP)
459 #ifdef CONFIG_NETCONSOLE
464 #if defined(CONFIG_CMD_SNTP)
473 NetBootFileXferSize = 0;
477 #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
478 #if defined(CONFIG_SYS_FAULT_ECHO_LINK_DOWN) && defined(CONFIG_STATUS_LED) && defined(STATUS_LED_RED)
480 * Echo the inverted link state to the fault LED.
482 if(miiphy_link(eth_get_dev()->name, CONFIG_SYS_FAULT_MII_ADDR)) {
483 status_led_set (STATUS_LED_RED, STATUS_LED_OFF);
485 status_led_set (STATUS_LED_RED, STATUS_LED_ON);
487 #endif /* CONFIG_SYS_FAULT_ECHO_LINK_DOWN, ... */
488 #endif /* CONFIG_MII, ... */
491 * Main packet reception loop. Loop receiving packets until
492 * someone sets `NetState' to a state that terminates.
496 #ifdef CONFIG_SHOW_ACTIVITY
498 extern void show_activity(int arg);
503 * Check the ethernet for a new packet. The ethernet
504 * receive routine will process it.
509 * Abort if ctrl-c was pressed.
520 * Check for a timeout, and run the timeout handler
523 if (timeHandler && ((get_timer(0) - timeStart) > timeDelta)) {
526 #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
527 # if defined(CONFIG_SYS_FAULT_ECHO_LINK_DOWN) && \
528 defined(CONFIG_STATUS_LED) && \
529 defined(STATUS_LED_RED)
531 * Echo the inverted link state to the fault LED.
533 if(miiphy_link(eth_get_dev()->name, CONFIG_SYS_FAULT_MII_ADDR)) {
534 status_led_set (STATUS_LED_RED, STATUS_LED_OFF);
536 status_led_set (STATUS_LED_RED, STATUS_LED_ON);
538 # endif /* CONFIG_SYS_FAULT_ECHO_LINK_DOWN, ... */
539 #endif /* CONFIG_MII, ... */
541 timeHandler = (thand_f *)0;
548 case NETLOOP_RESTART:
549 #ifdef CONFIG_NET_MULTI
554 case NETLOOP_SUCCESS:
555 if (NetBootFileXferSize > 0) {
557 printf("Bytes transferred = %ld (%lx hex)\n",
559 NetBootFileXferSize);
560 sprintf(buf, "%lX", NetBootFileXferSize);
561 setenv("filesize", buf);
563 sprintf(buf, "%lX", (unsigned long)load_addr);
564 setenv("fileaddr", buf);
567 return NetBootFileXferSize;
575 /**********************************************************************/
578 startAgainTimeout(void)
580 NetState = NETLOOP_RESTART;
584 startAgainHandler(uchar * pkt, unsigned dest, unsigned src, unsigned len)
586 /* Totally ignore the packet */
589 void NetStartAgain (void)
592 int noretry = 0, once = 0;
594 if ((nretry = getenv ("netretry")) != NULL) {
595 noretry = (strcmp (nretry, "no") == 0);
596 once = (strcmp (nretry, "once") == 0);
600 NetState = NETLOOP_FAIL;
603 #ifndef CONFIG_NET_MULTI
604 NetSetTimeout (10000UL, startAgainTimeout);
605 NetSetHandler (startAgainHandler);
606 #else /* !CONFIG_NET_MULTI*/
608 #if !defined(CONFIG_NET_DO_NOT_TRY_ANOTHER)
609 eth_try_another (!NetRestarted);
612 if (NetRestartWrap) {
614 if (NetDevExists && !once) {
615 NetSetTimeout (10000UL, startAgainTimeout);
616 NetSetHandler (startAgainHandler);
618 NetState = NETLOOP_FAIL;
621 NetState = NETLOOP_RESTART;
623 #endif /* CONFIG_NET_MULTI */
626 /**********************************************************************/
632 NetSetHandler(rxhand_f * f)
639 NetSetTimeout(ulong iv, thand_f * f)
642 timeHandler = (thand_f *)0;
645 timeStart = get_timer(0);
652 NetSendPacket(volatile uchar * pkt, int len)
654 (void) eth_send(pkt, len);
658 NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport, int sport, int len)
662 /* convert to new style broadcast */
666 /* if broadcast, make the ether address a broadcast and don't do ARP */
667 if (dest == 0xFFFFFFFF)
668 ether = NetBcastAddr;
670 /* if MAC address was not discovered yet, save the packet and do an ARP request */
671 if (memcmp(ether, NetEtherNullAddr, 6) == 0) {
674 printf("sending ARP for %08lx\n", dest);
676 NetArpWaitPacketIP = dest;
677 NetArpWaitPacketMAC = ether;
679 pkt = NetArpWaitTxPacket;
680 pkt += NetSetEther (pkt, NetArpWaitPacketMAC, PROT_IP);
682 NetSetIP (pkt, dest, dport, sport, len);
683 memcpy(pkt + IP_HDR_SIZE, (uchar *)NetTxPacket + (pkt - (uchar *)NetArpWaitTxPacket) + IP_HDR_SIZE, len);
685 /* size of the waiting packet */
686 NetArpWaitTxPacketSize = (pkt - NetArpWaitTxPacket) + IP_HDR_SIZE + len;
688 /* and do the ARP request */
690 NetArpWaitTimerStart = get_timer(0);
692 return 1; /* waiting */
696 printf("sending UDP to %08lx/%02x:%02x:%02x:%02x:%02x:%02x\n",
697 dest, ether[0], ether[1], ether[2], ether[3], ether[4], ether[5]);
700 pkt = (uchar *)NetTxPacket;
701 pkt += NetSetEther (pkt, ether, PROT_IP);
702 NetSetIP (pkt, dest, dport, sport, len);
703 (void) eth_send(NetTxPacket, (pkt - NetTxPacket) + IP_HDR_SIZE + len);
705 return 0; /* transmitted */
708 #if defined(CONFIG_CMD_PING)
709 static ushort PingSeqNo;
718 /* XXX always send arp request */
720 memcpy(mac, NetEtherNullAddr, 6);
723 printf("sending ARP for %08lx\n", NetPingIP);
726 NetArpWaitPacketIP = NetPingIP;
727 NetArpWaitPacketMAC = mac;
729 pkt = NetArpWaitTxPacket;
730 pkt += NetSetEther(pkt, mac, PROT_IP);
732 ip = (volatile IP_t *)pkt;
735 * Construct an IP and ICMP header. (need to set no fragment bit - XXX)
737 ip->ip_hl_v = 0x45; /* IP_HDR_SIZE / 4 (not including UDP) */
739 ip->ip_len = htons(IP_HDR_SIZE_NO_UDP + 8);
740 ip->ip_id = htons(NetIPID++);
741 ip->ip_off = htons(IP_FLAGS_DFRAG); /* Don't fragment */
743 ip->ip_p = 0x01; /* ICMP */
745 NetCopyIP((void*)&ip->ip_src, &NetOurIP); /* already in network byte order */
746 NetCopyIP((void*)&ip->ip_dst, &NetPingIP); /* - "" - */
747 ip->ip_sum = ~NetCksum((uchar *)ip, IP_HDR_SIZE_NO_UDP / 2);
749 s = &ip->udp_src; /* XXX ICMP starts here */
750 s[0] = htons(0x0800); /* echo-request, code */
751 s[1] = 0; /* checksum */
752 s[2] = 0; /* identifier */
753 s[3] = htons(PingSeqNo++); /* sequence number */
754 s[1] = ~NetCksum((uchar *)s, 8/2);
756 /* size of the waiting packet */
757 NetArpWaitTxPacketSize = (pkt - NetArpWaitTxPacket) + IP_HDR_SIZE_NO_UDP + 8;
759 /* and do the ARP request */
761 NetArpWaitTimerStart = get_timer(0);
763 return 1; /* waiting */
770 NetState = NETLOOP_FAIL; /* we did not get the reply */
774 PingHandler (uchar * pkt, unsigned dest, unsigned src, unsigned len)
777 volatile IP_t *ip = (volatile IP_t *)pkt;
779 tmp = NetReadIP((void *)&ip->ip_src);
780 if (tmp != NetPingIP)
783 NetState = NETLOOP_SUCCESS;
786 static void PingStart(void)
788 #if defined(CONFIG_NET_MULTI)
789 printf ("Using %s device\n", eth_get_name());
790 #endif /* CONFIG_NET_MULTI */
791 NetSetTimeout (10000UL, PingTimeout);
792 NetSetHandler (PingHandler);
798 #if defined(CONFIG_CMD_CDP)
800 #define CDP_DEVICE_ID_TLV 0x0001
801 #define CDP_ADDRESS_TLV 0x0002
802 #define CDP_PORT_ID_TLV 0x0003
803 #define CDP_CAPABILITIES_TLV 0x0004
804 #define CDP_VERSION_TLV 0x0005
805 #define CDP_PLATFORM_TLV 0x0006
806 #define CDP_NATIVE_VLAN_TLV 0x000a
807 #define CDP_APPLIANCE_VLAN_TLV 0x000e
808 #define CDP_TRIGGER_TLV 0x000f
809 #define CDP_POWER_CONSUMPTION_TLV 0x0010
810 #define CDP_SYSNAME_TLV 0x0014
811 #define CDP_SYSOBJECT_TLV 0x0015
812 #define CDP_MANAGEMENT_ADDRESS_TLV 0x0016
814 #define CDP_TIMEOUT 250UL /* one packet every 250ms */
819 ushort CDPNativeVLAN;
820 ushort CDPApplianceVLAN;
822 static const uchar CDP_SNAP_hdr[8] = { 0xAA, 0xAA, 0x03, 0x00, 0x00, 0x0C, 0x20, 0x00 };
824 static ushort CDP_compute_csum(const uchar *buff, ushort len)
833 odd = 1 & (ulong)buff;
843 if (result & 0x80000000)
844 result = (result & 0xFFFF) + (result >> 16);
848 leftover = (signed short)(*(const signed char *)buff);
849 /* CISCO SUCKS big time! (and blows too):
850 * CDP uses the IP checksum algorithm with a twist;
851 * for the last byte it *sign* extends and sums.
853 result = (result & 0xffff0000) | ((result + leftover) & 0x0000ffff);
856 result = (result & 0xFFFF) + (result >> 16);
859 result = ((result >> 8) & 0xff) | ((result & 0xff) << 8);
862 /* add up 16-bit and 17-bit words for 17+c bits */
863 result = (result & 0xffff) + (result >> 16);
864 /* add up 16-bit and 2-bit for 16+c bit */
865 result = (result & 0xffff) + (result >> 16);
867 result = (result & 0xffff) + (result >> 16);
870 csum = ~(ushort)result;
872 /* run time endian detection */
873 if (csum != htons(csum)) /* little endian */
879 int CDPSendTrigger(void)
887 #if defined(CONFIG_CDP_DEVICE_ID) || defined(CONFIG_CDP_PORT_ID) || \
888 defined(CONFIG_CDP_VERSION) || defined(CONFIG_CDP_PLATFORM)
893 et = (Ethernet_t *)pkt;
895 /* NOTE: trigger sent not on any VLAN */
897 /* form ethernet header */
898 memcpy(et->et_dest, NetCDPAddr, 6);
899 memcpy(et->et_src, NetOurEther, 6);
901 pkt += ETHER_HDR_SIZE;
904 memcpy((uchar *)pkt, CDP_SNAP_hdr, sizeof(CDP_SNAP_hdr));
905 pkt += sizeof(CDP_SNAP_hdr);
908 *pkt++ = 0x02; /* CDP version 2 */
909 *pkt++ = 180; /* TTL */
910 s = (volatile ushort *)pkt;
912 *s++ = htons(0); /* checksum (0 for later calculation) */
915 #ifdef CONFIG_CDP_DEVICE_ID
916 *s++ = htons(CDP_DEVICE_ID_TLV);
917 *s++ = htons(CONFIG_CDP_DEVICE_ID);
918 memset(buf, 0, sizeof(buf));
919 sprintf(buf, CONFIG_CDP_DEVICE_ID_PREFIX "%02X%02X%02X%02X%02X%02X",
920 NetOurEther[0] & 0xff, NetOurEther[1] & 0xff,
921 NetOurEther[2] & 0xff, NetOurEther[3] & 0xff,
922 NetOurEther[4] & 0xff, NetOurEther[5] & 0xff);
923 memcpy((uchar *)s, buf, 16);
927 #ifdef CONFIG_CDP_PORT_ID
928 *s++ = htons(CDP_PORT_ID_TLV);
929 memset(buf, 0, sizeof(buf));
930 sprintf(buf, CONFIG_CDP_PORT_ID, eth_get_dev_index());
932 if (len & 1) /* make it even */
934 *s++ = htons(len + 4);
935 memcpy((uchar *)s, buf, len);
939 #ifdef CONFIG_CDP_CAPABILITIES
940 *s++ = htons(CDP_CAPABILITIES_TLV);
942 *(ulong *)s = htonl(CONFIG_CDP_CAPABILITIES);
946 #ifdef CONFIG_CDP_VERSION
947 *s++ = htons(CDP_VERSION_TLV);
948 memset(buf, 0, sizeof(buf));
949 strcpy(buf, CONFIG_CDP_VERSION);
951 if (len & 1) /* make it even */
953 *s++ = htons(len + 4);
954 memcpy((uchar *)s, buf, len);
958 #ifdef CONFIG_CDP_PLATFORM
959 *s++ = htons(CDP_PLATFORM_TLV);
960 memset(buf, 0, sizeof(buf));
961 strcpy(buf, CONFIG_CDP_PLATFORM);
963 if (len & 1) /* make it even */
965 *s++ = htons(len + 4);
966 memcpy((uchar *)s, buf, len);
970 #ifdef CONFIG_CDP_TRIGGER
971 *s++ = htons(CDP_TRIGGER_TLV);
973 *(ulong *)s = htonl(CONFIG_CDP_TRIGGER);
977 #ifdef CONFIG_CDP_POWER_CONSUMPTION
978 *s++ = htons(CDP_POWER_CONSUMPTION_TLV);
980 *s++ = htons(CONFIG_CDP_POWER_CONSUMPTION);
983 /* length of ethernet packet */
984 len = (uchar *)s - ((uchar *)NetTxPacket + ETHER_HDR_SIZE);
985 et->et_protlen = htons(len);
987 len = ETHER_HDR_SIZE + sizeof(CDP_SNAP_hdr);
988 chksum = CDP_compute_csum((uchar *)NetTxPacket + len, (uchar *)s - (NetTxPacket + len));
993 (void) eth_send(NetTxPacket, (uchar *)s - NetTxPacket);
1003 NetSetTimeout (CDP_TIMEOUT, CDPTimeout);
1008 /* if not OK try again */
1012 NetState = NETLOOP_SUCCESS;
1016 CDPDummyHandler (uchar * pkt, unsigned dest, unsigned src, unsigned len)
1022 CDPHandler(const uchar * pkt, unsigned len)
1031 if (len < sizeof(CDP_SNAP_hdr) + 4)
1034 /* check for valid CDP SNAP header */
1035 if (memcmp(pkt, CDP_SNAP_hdr, sizeof(CDP_SNAP_hdr)) != 0)
1038 pkt += sizeof(CDP_SNAP_hdr);
1039 len -= sizeof(CDP_SNAP_hdr);
1041 /* Version of CDP protocol must be >= 2 and TTL != 0 */
1042 if (pkt[0] < 0x02 || pkt[1] == 0)
1045 /* if version is greater than 0x02 maybe we'll have a problem; output a warning */
1047 printf("** WARNING: CDP packet received with a protocol version %d > 2\n",
1050 if (CDP_compute_csum(pkt, len) != 0)
1062 ss = (const ushort *)pkt;
1063 type = ntohs(ss[0]);
1064 tlen = ntohs(ss[1]);
1072 ss += 2; /* point ss to the data of the TLV */
1076 case CDP_DEVICE_ID_TLV:
1078 case CDP_ADDRESS_TLV:
1080 case CDP_PORT_ID_TLV:
1082 case CDP_CAPABILITIES_TLV:
1084 case CDP_VERSION_TLV:
1086 case CDP_PLATFORM_TLV:
1088 case CDP_NATIVE_VLAN_TLV:
1091 case CDP_APPLIANCE_VLAN_TLV:
1092 t = (const uchar *)ss;
1098 ss = (const ushort *)(t + 1);
1100 #ifdef CONFIG_CDP_APPLIANCE_VLAN_TYPE
1101 if (applid == CONFIG_CDP_APPLIANCE_VLAN_TYPE)
1104 vlan = ntohs(*ss); /* XXX will this work; dunno */
1109 case CDP_TRIGGER_TLV:
1111 case CDP_POWER_CONSUMPTION_TLV:
1113 case CDP_SYSNAME_TLV:
1115 case CDP_SYSOBJECT_TLV:
1117 case CDP_MANAGEMENT_ADDRESS_TLV:
1122 CDPApplianceVLAN = vlan;
1123 CDPNativeVLAN = nvlan;
1129 printf("** CDP packet is too short\n");
1133 static void CDPStart(void)
1135 #if defined(CONFIG_NET_MULTI)
1136 printf ("Using %s device\n", eth_get_name());
1141 CDPNativeVLAN = htons(-1);
1142 CDPApplianceVLAN = htons(-1);
1144 NetSetTimeout (CDP_TIMEOUT, CDPTimeout);
1145 NetSetHandler (CDPDummyHandler);
1153 NetReceive(volatile uchar * inpkt, int len)
1161 #if defined(CONFIG_CMD_CDP)
1164 ushort cti = 0, vlanid = VLAN_NONE, myvlanid, mynvlanid;
1167 printf("packet received\n");
1172 et = (Ethernet_t *)inpkt;
1174 /* too small packet? */
1175 if (len < ETHER_HDR_SIZE)
1180 (*push_packet)(inpkt, len);
1185 #if defined(CONFIG_CMD_CDP)
1186 /* keep track if packet is CDP */
1187 iscdp = memcmp(et->et_dest, NetCDPAddr, 6) == 0;
1190 myvlanid = ntohs(NetOurVLAN);
1191 if (myvlanid == (ushort)-1)
1192 myvlanid = VLAN_NONE;
1193 mynvlanid = ntohs(NetOurNativeVLAN);
1194 if (mynvlanid == (ushort)-1)
1195 mynvlanid = VLAN_NONE;
1197 x = ntohs(et->et_protlen);
1200 printf("packet received\n");
1205 * Got a 802 packet. Check the other protocol field.
1207 x = ntohs(et->et_prot);
1209 ip = (IP_t *)(inpkt + E802_HDR_SIZE);
1210 len -= E802_HDR_SIZE;
1212 } else if (x != PROT_VLAN) { /* normal packet */
1213 ip = (IP_t *)(inpkt + ETHER_HDR_SIZE);
1214 len -= ETHER_HDR_SIZE;
1216 } else { /* VLAN packet */
1217 VLAN_Ethernet_t *vet = (VLAN_Ethernet_t *)et;
1220 printf("VLAN packet received\n");
1222 /* too small packet? */
1223 if (len < VLAN_ETHER_HDR_SIZE)
1226 /* if no VLAN active */
1227 if ((ntohs(NetOurVLAN) & VLAN_IDMASK) == VLAN_NONE
1228 #if defined(CONFIG_CMD_CDP)
1234 cti = ntohs(vet->vet_tag);
1235 vlanid = cti & VLAN_IDMASK;
1236 x = ntohs(vet->vet_type);
1238 ip = (IP_t *)(inpkt + VLAN_ETHER_HDR_SIZE);
1239 len -= VLAN_ETHER_HDR_SIZE;
1243 printf("Receive from protocol 0x%x\n", x);
1246 #if defined(CONFIG_CMD_CDP)
1248 CDPHandler((uchar *)ip, len);
1253 if ((myvlanid & VLAN_IDMASK) != VLAN_NONE) {
1254 if (vlanid == VLAN_NONE)
1255 vlanid = (mynvlanid & VLAN_IDMASK);
1257 if (vlanid != (myvlanid & VLAN_IDMASK))
1265 * We have to deal with two types of ARP packets:
1266 * - REQUEST packets will be answered by sending our
1267 * IP address - if we know it.
1268 * - REPLY packates are expected only after we asked
1269 * for the TFTP server's or the gateway's ethernet
1270 * address; so if we receive such a packet, we set
1271 * the server ethernet address
1277 if (len < ARP_HDR_SIZE) {
1278 printf("bad length %d < %d\n", len, ARP_HDR_SIZE);
1281 if (ntohs(arp->ar_hrd) != ARP_ETHER) {
1284 if (ntohs(arp->ar_pro) != PROT_IP) {
1287 if (arp->ar_hln != 6) {
1290 if (arp->ar_pln != 4) {
1294 if (NetOurIP == 0) {
1298 if (NetReadIP(&arp->ar_data[16]) != NetOurIP) {
1302 switch (ntohs(arp->ar_op)) {
1303 case ARPOP_REQUEST: /* reply with our IP address */
1305 puts ("Got ARP REQUEST, return our IP\n");
1308 pkt += NetSetEther(pkt, et->et_src, PROT_ARP);
1309 arp->ar_op = htons(ARPOP_REPLY);
1310 memcpy (&arp->ar_data[10], &arp->ar_data[0], 6);
1311 NetCopyIP(&arp->ar_data[16], &arp->ar_data[6]);
1312 memcpy (&arp->ar_data[ 0], NetOurEther, 6);
1313 NetCopyIP(&arp->ar_data[ 6], &NetOurIP);
1314 (void) eth_send((uchar *)et, (pkt - (uchar *)et) + ARP_HDR_SIZE);
1317 case ARPOP_REPLY: /* arp reply */
1318 /* are we waiting for a reply */
1319 if (!NetArpWaitPacketIP || !NetArpWaitPacketMAC)
1322 printf("Got ARP REPLY, set server/gtwy eth addr (%02x:%02x:%02x:%02x:%02x:%02x)\n",
1323 arp->ar_data[0], arp->ar_data[1],
1324 arp->ar_data[2], arp->ar_data[3],
1325 arp->ar_data[4], arp->ar_data[5]);
1328 tmp = NetReadIP(&arp->ar_data[6]);
1330 /* matched waiting packet's address */
1331 if (tmp == NetArpWaitReplyIP) {
1335 /* save address for later use */
1336 memcpy(NetArpWaitPacketMAC, &arp->ar_data[0], 6);
1338 #ifdef CONFIG_NETCONSOLE
1339 (*packetHandler)(0,0,0,0);
1341 /* modify header, and transmit it */
1342 memcpy(((Ethernet_t *)NetArpWaitTxPacket)->et_dest, NetArpWaitPacketMAC, 6);
1343 (void) eth_send(NetArpWaitTxPacket, NetArpWaitTxPacketSize);
1345 /* no arp request pending now */
1346 NetArpWaitPacketIP = 0;
1347 NetArpWaitTxPacketSize = 0;
1348 NetArpWaitPacketMAC = NULL;
1354 printf("Unexpected ARP opcode 0x%x\n", ntohs(arp->ar_op));
1362 puts ("Got RARP\n");
1365 if (len < ARP_HDR_SIZE) {
1366 printf("bad length %d < %d\n", len, ARP_HDR_SIZE);
1370 if ((ntohs(arp->ar_op) != RARPOP_REPLY) ||
1371 (ntohs(arp->ar_hrd) != ARP_ETHER) ||
1372 (ntohs(arp->ar_pro) != PROT_IP) ||
1373 (arp->ar_hln != 6) || (arp->ar_pln != 4)) {
1375 puts ("invalid RARP header\n");
1377 NetCopyIP(&NetOurIP, &arp->ar_data[16]);
1378 if (NetServerIP == 0)
1379 NetCopyIP(&NetServerIP, &arp->ar_data[ 6]);
1380 memcpy (NetServerEther, &arp->ar_data[ 0], 6);
1382 (*packetHandler)(0,0,0,0);
1390 if (len < IP_HDR_SIZE) {
1391 debug ("len bad %d < %lu\n", len, (ulong)IP_HDR_SIZE);
1394 if (len < ntohs(ip->ip_len)) {
1395 printf("len bad %d < %d\n", len, ntohs(ip->ip_len));
1398 len = ntohs(ip->ip_len);
1400 printf("len=%d, v=%02x\n", len, ip->ip_hl_v & 0xff);
1402 if ((ip->ip_hl_v & 0xf0) != 0x40) {
1405 /* Can't deal with fragments */
1406 if (ip->ip_off & htons(IP_OFFS | IP_FLAGS_MFRAG)) {
1409 /* can't deal with headers > 20 bytes */
1410 if ((ip->ip_hl_v & 0x0f) > 0x05) {
1413 if (!NetCksumOk((uchar *)ip, IP_HDR_SIZE_NO_UDP / 2)) {
1414 puts ("checksum bad\n");
1417 tmp = NetReadIP(&ip->ip_dst);
1418 if (NetOurIP && tmp != NetOurIP && tmp != 0xFFFFFFFF) {
1419 #ifdef CONFIG_MCAST_TFTP
1420 if (Mcast_addr != tmp)
1425 * watch for ICMP host redirects
1427 * There is no real handler code (yet). We just watch
1428 * for ICMP host redirect messages. In case anybody
1429 * sees these messages: please contact me
1430 * (wd@denx.de), or - even better - send me the
1431 * necessary fixes :-)
1433 * Note: in all cases where I have seen this so far
1434 * it was a problem with the router configuration,
1435 * for instance when a router was configured in the
1436 * BOOTP reply, but the TFTP server was on the same
1437 * subnet. So this is probably a warning that your
1438 * configuration might be wrong. But I'm not really
1439 * sure if there aren't any other situations.
1441 if (ip->ip_p == IPPROTO_ICMP) {
1442 ICMP_t *icmph = (ICMP_t *)&(ip->udp_src);
1444 switch (icmph->type) {
1446 if (icmph->code != ICMP_REDIR_HOST)
1448 puts (" ICMP Host Redirect to ");
1449 print_IPaddr(icmph->un.gateway);
1452 #if defined(CONFIG_CMD_PING)
1453 case ICMP_ECHO_REPLY:
1455 * IP header OK. Pass the packet to the current handler.
1457 /* XXX point to ip packet */
1458 (*packetHandler)((uchar *)ip, 0, 0, 0);
1460 case ICMP_ECHO_REQUEST:
1462 printf ("Got ICMP ECHO REQUEST, return %d bytes \n",
1463 ETHER_HDR_SIZE + len);
1465 memcpy (&et->et_dest[0], &et->et_src[0], 6);
1466 memcpy (&et->et_src[ 0], NetOurEther, 6);
1470 NetCopyIP((void*)&ip->ip_dst, &ip->ip_src);
1471 NetCopyIP((void*)&ip->ip_src, &NetOurIP);
1472 ip->ip_sum = ~NetCksum((uchar *)ip, IP_HDR_SIZE_NO_UDP >> 1);
1474 icmph->type = ICMP_ECHO_REPLY;
1475 icmph->checksum = 0;
1476 icmph->checksum = ~NetCksum((uchar *)icmph,
1477 (len - IP_HDR_SIZE_NO_UDP) >> 1);
1478 (void) eth_send((uchar *)et, ETHER_HDR_SIZE + len);
1484 } else if (ip->ip_p != IPPROTO_UDP) { /* Only UDP packets */
1488 #ifdef CONFIG_UDP_CHECKSUM
1489 if (ip->udp_xsum != 0) {
1495 xsum += (ntohs(ip->udp_len));
1496 xsum += (ntohl(ip->ip_src) >> 16) & 0x0000ffff;
1497 xsum += (ntohl(ip->ip_src) >> 0) & 0x0000ffff;
1498 xsum += (ntohl(ip->ip_dst) >> 16) & 0x0000ffff;
1499 xsum += (ntohl(ip->ip_dst) >> 0) & 0x0000ffff;
1501 sumlen = ntohs(ip->udp_len);
1502 sumptr = (ushort *) &(ip->udp_src);
1504 while (sumlen > 1) {
1507 sumdata = *sumptr++;
1508 xsum += ntohs(sumdata);
1514 sumdata = *(unsigned char *) sumptr;
1515 sumdata = (sumdata << 8) & 0xff00;
1518 while ((xsum >> 16) != 0) {
1519 xsum = (xsum & 0x0000ffff) + ((xsum >> 16) & 0x0000ffff);
1521 if ((xsum != 0x00000000) && (xsum != 0x0000ffff)) {
1522 printf(" UDP wrong checksum %08lx %08x\n",
1523 xsum, ntohs(ip->udp_xsum));
1530 #ifdef CONFIG_NETCONSOLE
1531 nc_input_packet((uchar *)ip +IP_HDR_SIZE,
1534 ntohs(ip->udp_len) - 8);
1537 * IP header OK. Pass the packet to the current handler.
1539 (*packetHandler)((uchar *)ip +IP_HDR_SIZE,
1542 ntohs(ip->udp_len) - 8);
1548 /**********************************************************************/
1550 static int net_check_prereq (proto_t protocol)
1554 #if defined(CONFIG_CMD_PING)
1556 if (NetPingIP == 0) {
1557 puts ("*** ERROR: ping address not given\n");
1562 #if defined(CONFIG_CMD_SNTP)
1564 if (NetNtpServerIP == 0) {
1565 puts ("*** ERROR: NTP server address not given\n");
1570 #if defined(CONFIG_CMD_NFS)
1575 if (NetServerIP == 0) {
1576 puts ("*** ERROR: `serverip' not set\n");
1579 #if defined(CONFIG_CMD_PING) || defined(CONFIG_CMD_SNTP)
1583 if (NetOurIP == 0) {
1584 puts ("*** ERROR: `ipaddr' not set\n");
1593 if (memcmp (NetOurEther, "\0\0\0\0\0\0", 6) == 0) {
1594 #ifdef CONFIG_NET_MULTI
1595 extern int eth_get_dev_index (void);
1596 int num = eth_get_dev_index ();
1600 puts ("*** ERROR: No ethernet found.\n");
1603 puts ("*** ERROR: `ethaddr' not set\n");
1606 printf ("*** ERROR: `eth%daddr' not set\n",
1614 puts ("*** ERROR: `ethaddr' not set\n");
1622 return (0); /* OK */
1624 /**********************************************************************/
1627 NetCksumOk(uchar * ptr, int len)
1629 return !((NetCksum(ptr, len) + 1) & 0xfffe);
1634 NetCksum(uchar * ptr, int len)
1637 ushort *p = (ushort *)ptr;
1642 xsum = (xsum & 0xffff) + (xsum >> 16);
1643 xsum = (xsum & 0xffff) + (xsum >> 16);
1644 return (xsum & 0xffff);
1652 myvlanid = ntohs(NetOurVLAN);
1653 if (myvlanid == (ushort)-1)
1654 myvlanid = VLAN_NONE;
1656 return ((myvlanid & VLAN_IDMASK) == VLAN_NONE) ? ETHER_HDR_SIZE : VLAN_ETHER_HDR_SIZE;
1660 NetSetEther(volatile uchar * xet, uchar * addr, uint prot)
1662 Ethernet_t *et = (Ethernet_t *)xet;
1665 myvlanid = ntohs(NetOurVLAN);
1666 if (myvlanid == (ushort)-1)
1667 myvlanid = VLAN_NONE;
1669 memcpy (et->et_dest, addr, 6);
1670 memcpy (et->et_src, NetOurEther, 6);
1671 if ((myvlanid & VLAN_IDMASK) == VLAN_NONE) {
1672 et->et_protlen = htons(prot);
1673 return ETHER_HDR_SIZE;
1675 VLAN_Ethernet_t *vet = (VLAN_Ethernet_t *)xet;
1677 vet->vet_vlan_type = htons(PROT_VLAN);
1678 vet->vet_tag = htons((0 << 5) | (myvlanid & VLAN_IDMASK));
1679 vet->vet_type = htons(prot);
1680 return VLAN_ETHER_HDR_SIZE;
1685 NetSetIP(volatile uchar * xip, IPaddr_t dest, int dport, int sport, int len)
1687 IP_t *ip = (IP_t *)xip;
1690 * If the data is an odd number of bytes, zero the
1691 * byte after the last byte so that the checksum
1695 xip[IP_HDR_SIZE + len] = 0;
1698 * Construct an IP and UDP header.
1699 * (need to set no fragment bit - XXX)
1701 ip->ip_hl_v = 0x45; /* IP_HDR_SIZE / 4 (not including UDP) */
1703 ip->ip_len = htons(IP_HDR_SIZE + len);
1704 ip->ip_id = htons(NetIPID++);
1705 ip->ip_off = htons(IP_FLAGS_DFRAG); /* Don't fragment */
1707 ip->ip_p = 17; /* UDP */
1709 NetCopyIP((void*)&ip->ip_src, &NetOurIP); /* already in network byte order */
1710 NetCopyIP((void*)&ip->ip_dst, &dest); /* - "" - */
1711 ip->udp_src = htons(sport);
1712 ip->udp_dst = htons(dport);
1713 ip->udp_len = htons(8 + len);
1715 ip->ip_sum = ~NetCksum((uchar *)ip, IP_HDR_SIZE_NO_UDP / 2);
1718 void copy_filename (char *dst, char *src, int size)
1720 if (*src && (*src == '"')) {
1725 while ((--size > 0) && *src && (*src != '"')) {
1733 void ip_to_string (IPaddr_t x, char *s)
1736 sprintf (s, "%d.%d.%d.%d",
1737 (int) ((x >> 24) & 0xff),
1738 (int) ((x >> 16) & 0xff),
1739 (int) ((x >> 8) & 0xff), (int) ((x >> 0) & 0xff)
1743 IPaddr_t string_to_ip(char *s)
1752 for (addr=0, i=0; i<4; ++i) {
1753 ulong val = s ? simple_strtoul(s, &e, 10) : 0;
1755 addr |= (val & 0xFF);
1761 return (htonl(addr));
1764 void VLAN_to_string(ushort x, char *s)
1768 if (x == (ushort)-1)
1774 sprintf(s, "%d", x & VLAN_IDMASK);
1777 ushort string_to_VLAN(char *s)
1782 return htons(VLAN_NONE);
1784 if (*s < '0' || *s > '9')
1787 id = (ushort)simple_strtoul(s, NULL, 10);
1792 void print_IPaddr (IPaddr_t x)
1796 ip_to_string (x, tmp);
1801 IPaddr_t getenv_IPaddr (char *var)
1803 return (string_to_ip(getenv(var)));
1806 ushort getenv_VLAN(char *var)
1808 return (string_to_VLAN(getenv(var)));