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
78 #ifdef CONFIG_STATUS_LED
79 #include <status_led.h>
83 #if (CONFIG_COMMANDS & CFG_CMD_NET)
85 #define ARP_TIMEOUT 5 /* Seconds before trying ARP again */
86 #ifndef CONFIG_NET_RETRY_COUNT
87 # define ARP_TIMEOUT_COUNT 5 /* # of timeouts before giving up */
89 # define ARP_TIMEOUT_COUNT (CONFIG_NET_RETRY_COUNT)
96 /** BOOTP EXTENTIONS **/
98 IPaddr_t NetOurSubnetMask=0; /* Our subnet mask (0=unknown) */
99 IPaddr_t NetOurGatewayIP=0; /* Our gateways IP address */
100 IPaddr_t NetOurDNSIP=0; /* Our DNS IP address */
101 #if (CONFIG_BOOTP_MASK & CONFIG_BOOTP_DNS2)
102 IPaddr_t NetOurDNS2IP=0; /* Our 2nd DNS IP address */
104 char NetOurNISDomain[32]={0,}; /* Our NIS domain */
105 char NetOurHostName[32]={0,}; /* Our hostname */
106 char NetOurRootPath[64]={0,}; /* Our bootpath */
107 ushort NetBootFileSize=0; /* Our bootfile size in blocks */
109 /** END OF BOOTP EXTENTIONS **/
111 ulong NetBootFileXferSize; /* The actual transferred size of the bootfile (in bytes) */
112 uchar NetOurEther[6]; /* Our ethernet address */
113 uchar NetServerEther[6] = /* Boot server enet address */
114 { 0, 0, 0, 0, 0, 0 };
115 IPaddr_t NetOurIP; /* Our IP addr (0 = unknown) */
116 IPaddr_t NetServerIP; /* Our IP addr (0 = unknown) */
117 volatile uchar *NetRxPkt; /* Current receive packet */
118 int NetRxPktLen; /* Current rx packet length */
119 unsigned NetIPID; /* IP packet ID */
120 uchar NetBcastAddr[6] = /* Ethernet bcast address */
121 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
122 uchar NetEtherNullAddr[6] =
123 { 0, 0, 0, 0, 0, 0 };
124 #if (CONFIG_COMMANDS & CFG_CMD_CDP)
125 uchar NetCDPAddr[6] = /* Ethernet bcast address */
126 { 0x01, 0x00, 0x0c, 0xcc, 0xcc, 0xcc };
128 int NetState; /* Network loop state */
129 #ifdef CONFIG_NET_MULTI
130 int NetRestartWrap = 0; /* Tried all network devices */
131 static int NetRestarted = 0; /* Network loop restarted */
132 static int NetDevExists = 0; /* At least one device configured */
135 ushort NetOurVLAN = ntohs(-1); /* default is without VLAN */
136 ushort NetOurNativeVLAN = htons(-1); /* dido */
138 char BootFile[128]; /* Boot File name */
140 #if (CONFIG_COMMANDS & CFG_CMD_PING)
141 IPaddr_t NetPingIP; /* the ip address to ping */
143 static void PingStart(void);
146 #if (CONFIG_COMMANDS & CFG_CMD_CDP)
147 static void CDPStart(void);
150 volatile uchar PktBuf[(PKTBUFSRX+1) * PKTSIZE_ALIGN + PKTALIGN];
152 volatile uchar *NetRxPackets[PKTBUFSRX]; /* Receive packets */
154 static rxhand_f *packetHandler; /* Current RX packet handler */
155 static thand_f *timeHandler; /* Current timeout handler */
156 static ulong timeStart; /* Time base value */
157 static ulong timeDelta; /* Current timeout value */
158 volatile uchar *NetTxPacket = 0; /* THE transmit packet */
160 static int net_check_prereq (proto_t protocol);
162 /**********************************************************************/
164 IPaddr_t NetArpWaitPacketIP;
165 IPaddr_t NetArpWaitReplyIP;
166 uchar *NetArpWaitPacketMAC; /* MAC address of waiting packet's destination */
167 uchar *NetArpWaitTxPacket; /* THE transmit packet */
168 int NetArpWaitTxPacketSize;
169 uchar NetArpWaitPacketBuf[PKTSIZE_ALIGN + PKTALIGN];
170 ulong NetArpWaitTimerStart;
173 void ArpRequest(void)
180 printf("ARP broadcast %d\n", NetArpWaitTry);
184 pkt += NetSetEther(pkt, NetBcastAddr, PROT_ARP);
188 arp->ar_hrd = htons(ARP_ETHER);
189 arp->ar_pro = htons(PROT_IP);
192 arp->ar_op = htons(ARPOP_REQUEST);
194 memcpy (&arp->ar_data[0], NetOurEther, 6); /* source ET addr */
195 NetWriteIP((uchar*)&arp->ar_data[6], NetOurIP); /* source IP addr */
196 for (i=10; i<16; ++i) {
197 arp->ar_data[i] = 0; /* dest ET addr = 0 */
200 if((NetArpWaitPacketIP & NetOurSubnetMask) != (NetOurIP & NetOurSubnetMask)) {
201 if (NetOurGatewayIP == 0) {
202 puts ("## Warning: gatewayip needed but not set\n");
204 NetArpWaitReplyIP = NetOurGatewayIP;
206 NetArpWaitReplyIP = NetArpWaitPacketIP;
208 NetWriteIP((uchar*)&arp->ar_data[16], NetArpWaitReplyIP);
209 (void) eth_send(NetTxPacket, (pkt - NetTxPacket) + ARP_HDR_SIZE);
212 void ArpTimeoutCheck(void)
216 if (!NetArpWaitPacketIP)
221 /* check for arp timeout */
222 if ((t - NetArpWaitTimerStart) > ARP_TIMEOUT * CFG_HZ) {
225 if (NetArpWaitTry >= ARP_TIMEOUT_COUNT) {
226 puts ("\nARP Retry count exceeded; starting again\n");
230 NetArpWaitTimerStart = t;
236 /**********************************************************************/
238 * Main network processing loop.
242 NetLoop(proto_t protocol)
244 DECLARE_GLOBAL_DATA_PTR;
248 #ifdef CONFIG_NET_MULTI
253 /* XXX problem with bss workaround */
254 NetArpWaitPacketMAC = NULL;
255 NetArpWaitTxPacket = NULL;
256 NetArpWaitPacketIP = 0;
257 NetArpWaitReplyIP = 0;
258 NetArpWaitTxPacket = NULL;
265 * Setup packet buffers, aligned correctly.
267 NetTxPacket = &PktBuf[0] + (PKTALIGN - 1);
268 NetTxPacket -= (ulong)NetTxPacket % PKTALIGN;
269 for (i = 0; i < PKTBUFSRX; i++) {
270 NetRxPackets[i] = NetTxPacket + (i+1)*PKTSIZE_ALIGN;
275 if (!NetArpWaitTxPacket) {
276 NetArpWaitTxPacket = &NetArpWaitPacketBuf[0] + (PKTALIGN - 1);
277 NetArpWaitTxPacket -= (ulong)NetArpWaitTxPacket % PKTALIGN;
278 NetArpWaitTxPacketSize = 0;
287 #ifdef CONFIG_NET_MULTI
288 memcpy (NetOurEther, eth_get_dev()->enetaddr, 6);
290 memcpy (NetOurEther, bd->bi_enetaddr, 6);
293 NetState = NETLOOP_CONTINUE;
296 * Start the ball rolling with the given start function. From
297 * here on, this code is a state machine driven by received
298 * packets and timer events.
302 #if (CONFIG_COMMANDS & CFG_CMD_NFS)
305 #if (CONFIG_COMMANDS & CFG_CMD_PING)
309 NetCopyIP(&NetOurIP, &bd->bi_ip_addr);
310 NetOurGatewayIP = getenv_IPaddr ("gatewayip");
311 NetOurSubnetMask= getenv_IPaddr ("netmask");
312 NetOurVLAN = getenv_VLAN("vlan");
313 NetOurNativeVLAN = getenv_VLAN("nvlan");
316 #if (CONFIG_COMMANDS & CFG_CMD_NFS)
320 NetServerIP = getenv_IPaddr ("serverip");
322 #if (CONFIG_COMMANDS & CFG_CMD_PING)
335 * initialize our IP addr to 0 in order to accept ANY
336 * IP addr assigned to us by the BOOTP / RARP server
339 NetServerIP = getenv_IPaddr ("serverip");
340 NetOurVLAN = getenv_VLAN("vlan"); /* VLANs must be read */
341 NetOurNativeVLAN = getenv_VLAN("nvlan");
343 NetOurVLAN = getenv_VLAN("vlan"); /* VLANs must be read */
344 NetOurNativeVLAN = getenv_VLAN("nvlan");
350 switch (net_check_prereq (protocol)) {
352 /* network not configured */
355 #ifdef CONFIG_NET_MULTI
357 /* network device not configured */
359 #endif /* CONFIG_NET_MULTI */
362 #ifdef CONFIG_NET_MULTI
367 /* always use ARP to get server ethernet address */
371 #if (CONFIG_COMMANDS & CFG_CMD_DHCP)
373 /* Start with a clean slate... */
375 NetServerIP = getenv_IPaddr ("serverip");
376 DhcpRequest(); /* Basically same as BOOTP */
378 #endif /* CFG_CMD_DHCP */
389 #if (CONFIG_COMMANDS & CFG_CMD_PING)
394 #if (CONFIG_COMMANDS & CFG_CMD_NFS)
399 #if (CONFIG_COMMANDS & CFG_CMD_CDP)
408 NetBootFileXferSize = 0;
412 #if defined(CONFIG_MII) || (CONFIG_COMMANDS & CFG_CMD_MII)
413 #if defined(CFG_FAULT_ECHO_LINK_DOWN) && defined(CONFIG_STATUS_LED) && defined(STATUS_LED_RED)
415 * Echo the inverted link state to the fault LED.
417 if(miiphy_link(CFG_FAULT_MII_ADDR)) {
418 status_led_set (STATUS_LED_RED, STATUS_LED_OFF);
420 status_led_set (STATUS_LED_RED, STATUS_LED_ON);
422 #endif /* CFG_FAULT_ECHO_LINK_DOWN, ... */
423 #endif /* CONFIG_MII, ... */
426 * Main packet reception loop. Loop receiving packets until
427 * someone sets `NetQuit'.
431 #ifdef CONFIG_SHOW_ACTIVITY
433 extern void show_activity(int arg);
438 * Check the ethernet for a new packet. The ethernet
439 * receive routine will process it.
444 * Abort if ctrl-c was pressed.
455 * Check for a timeout, and run the timeout handler
458 if (timeHandler && ((get_timer(0) - timeStart) > timeDelta)) {
461 #if defined(CONFIG_MII) || (CONFIG_COMMANDS & CFG_CMD_MII)
462 #if defined(CFG_FAULT_ECHO_LINK_DOWN) && defined(CONFIG_STATUS_LED) && defined(STATUS_LED_RED)
464 * Echo the inverted link state to the fault LED.
466 if(miiphy_link(CFG_FAULT_MII_ADDR)) {
467 status_led_set (STATUS_LED_RED, STATUS_LED_OFF);
469 status_led_set (STATUS_LED_RED, STATUS_LED_ON);
471 #endif /* CFG_FAULT_ECHO_LINK_DOWN, ... */
472 #endif /* CONFIG_MII, ... */
474 timeHandler = (thand_f *)0;
481 case NETLOOP_RESTART:
482 #ifdef CONFIG_NET_MULTI
487 case NETLOOP_SUCCESS:
488 if (NetBootFileXferSize > 0) {
490 printf("Bytes transferred = %ld (%lx hex)\n",
492 NetBootFileXferSize);
493 sprintf(buf, "%lx", NetBootFileXferSize);
494 setenv("filesize", buf);
496 sprintf(buf, "%lX", (unsigned long)load_addr);
497 setenv("fileaddr", buf);
500 return NetBootFileXferSize;
508 /**********************************************************************/
511 startAgainTimeout(void)
513 NetState = NETLOOP_RESTART;
517 startAgainHandler(uchar * pkt, unsigned dest, unsigned src, unsigned len)
519 /* Totally ignore the packet */
525 DECLARE_GLOBAL_DATA_PTR;
528 if ((s = getenv("netretry")) != NULL && *s == 'n') {
530 NetState = NETLOOP_FAIL;
534 #ifndef CONFIG_NET_MULTI
535 NetSetTimeout(10 * CFG_HZ, startAgainTimeout);
536 NetSetHandler(startAgainHandler);
539 eth_try_another(!NetRestarted);
546 NetSetTimeout(10 * CFG_HZ, startAgainTimeout);
547 NetSetHandler(startAgainHandler);
551 NetState = NETLOOP_FAIL;
556 NetState = NETLOOP_RESTART;
561 /**********************************************************************/
567 NetSetHandler(rxhand_f * f)
574 NetSetTimeout(int iv, thand_f * f)
577 timeHandler = (thand_f *)0;
580 timeStart = get_timer(0);
587 NetSendPacket(volatile uchar * pkt, int len)
589 (void) eth_send(pkt, len);
593 NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport, int sport, int len)
597 /* convert to new style broadcast */
601 /* if broadcast, make the ether address a broadcast and don't do ARP */
602 if (dest == 0xFFFFFFFF)
603 ether = NetBcastAddr;
605 /* if MAC address was not discovered yet, save the packet and do an ARP request */
606 if (memcmp(ether, NetEtherNullAddr, 6) == 0) {
609 printf("sending ARP for %08lx\n", dest);
611 NetArpWaitPacketIP = dest;
612 NetArpWaitPacketMAC = ether;
614 pkt = NetArpWaitTxPacket;
615 pkt += NetSetEther (pkt, NetArpWaitPacketMAC, PROT_IP);
617 NetSetIP (pkt, dest, dport, sport, len);
618 memcpy(pkt + IP_HDR_SIZE, (uchar *)NetTxPacket + (pkt - (uchar *)NetArpWaitTxPacket) + IP_HDR_SIZE, len);
620 /* size of the waiting packet */
621 NetArpWaitTxPacketSize = (pkt - NetArpWaitTxPacket) + IP_HDR_SIZE + len;
623 /* and do the ARP request */
625 NetArpWaitTimerStart = get_timer(0);
627 return 1; /* waiting */
631 printf("sending UDP to %08lx/%02x:%02x:%02x:%02x:%02x:%02x\n",
632 dest, ether[0], ether[1], ether[2], ether[3], ether[4], ether[5]);
635 pkt = (uchar *)NetTxPacket;
636 pkt += NetSetEther (pkt, ether, PROT_IP);
637 NetSetIP (pkt, dest, dport, sport, len);
638 (void) eth_send(NetTxPacket, (pkt - NetTxPacket) + IP_HDR_SIZE + len);
640 return 0; /* transmited */
643 #if (CONFIG_COMMANDS & CFG_CMD_PING)
644 static ushort PingSeqNo;
653 /* XXX always send arp request */
655 memcpy(mac, NetEtherNullAddr, 6);
658 printf("sending ARP for %08lx\n", NetPingIP);
661 NetArpWaitPacketIP = NetPingIP;
662 NetArpWaitPacketMAC = mac;
664 pkt = NetArpWaitTxPacket;
665 pkt += NetSetEther(pkt, mac, PROT_IP);
667 ip = (volatile IP_t *)pkt;
670 * Construct an IP and ICMP header. (need to set no fragment bit - XXX)
672 ip->ip_hl_v = 0x45; /* IP_HDR_SIZE / 4 (not including UDP) */
674 ip->ip_len = htons(IP_HDR_SIZE_NO_UDP + 8);
675 ip->ip_id = htons(NetIPID++);
676 ip->ip_off = htons(0x4000); /* No fragmentation */
678 ip->ip_p = 0x01; /* ICMP */
680 NetCopyIP((void*)&ip->ip_src, &NetOurIP); /* already in network byte order */
681 NetCopyIP((void*)&ip->ip_dst, &NetPingIP); /* - "" - */
682 ip->ip_sum = ~NetCksum((uchar *)ip, IP_HDR_SIZE_NO_UDP / 2);
684 s = &ip->udp_src; /* XXX ICMP starts here */
685 s[0] = htons(0x0800); /* echo-request, code */
686 s[1] = 0; /* checksum */
687 s[2] = 0; /* identifier */
688 s[3] = htons(PingSeqNo++); /* sequence number */
689 s[1] = ~NetCksum((uchar *)s, 8/2);
691 /* size of the waiting packet */
692 NetArpWaitTxPacketSize = (pkt - NetArpWaitTxPacket) + IP_HDR_SIZE_NO_UDP + 8;
694 /* and do the ARP request */
696 NetArpWaitTimerStart = get_timer(0);
698 return 1; /* waiting */
705 NetState = NETLOOP_FAIL; /* we did not get the reply */
709 PingHandler (uchar * pkt, unsigned dest, unsigned src, unsigned len)
712 volatile IP_t *ip = (volatile IP_t *)pkt;
714 tmp = NetReadIP((void *)&ip->ip_src);
715 if (tmp != NetPingIP)
718 NetState = NETLOOP_SUCCESS;
721 static void PingStart(void)
723 #if defined(CONFIG_NET_MULTI)
724 printf ("Using %s device\n", eth_get_name());
726 NetSetTimeout (10 * CFG_HZ, PingTimeout);
727 NetSetHandler (PingHandler);
734 #if (CONFIG_COMMANDS & CFG_CMD_CDP)
736 #define CDP_DEVICE_ID_TLV 0x0001
737 #define CDP_ADDRESS_TLV 0x0002
738 #define CDP_PORT_ID_TLV 0x0003
739 #define CDP_CAPABILITIES_TLV 0x0004
740 #define CDP_VERSION_TLV 0x0005
741 #define CDP_PLATFORM_TLV 0x0006
742 #define CDP_NATIVE_VLAN_TLV 0x000a
743 #define CDP_APPLIANCE_VLAN_TLV 0x000e
744 #define CDP_TRIGGER_TLV 0x000f
745 #define CDP_POWER_CONSUMPTION_TLV 0x0010
746 #define CDP_SYSNAME_TLV 0x0014
747 #define CDP_SYSOBJECT_TLV 0x0015
748 #define CDP_MANAGEMENT_ADDRESS_TLV 0x0016
750 #define CDP_TIMEOUT (CFG_HZ/4) /* one packet every 250ms */
755 ushort CDPNativeVLAN;
756 ushort CDPApplianceVLAN;
758 static const uchar CDP_SNAP_hdr[8] = { 0xAA, 0xAA, 0x03, 0x00, 0x00, 0x0C, 0x20, 0x00 };
760 static ushort CDP_compute_csum(const uchar *buff, ushort len)
768 odd = 1 & (ulong)buff;
775 result += *((const ushort *)buff)++;
776 if (result & 0x80000000)
777 result = (result & 0xFFFF) + (result >> 16);
781 leftover = (signed short)(*(const signed char *)buff);
782 /* * XXX CISCO SUCKS big time! (and blows too) */
783 result = (result & 0xffff0000) | ((result + leftover) & 0x0000ffff);
786 result = (result & 0xFFFF) + (result >> 16);
789 result = ((result >> 8) & 0xff) | ((result & 0xff) << 8);
792 /* add up 16-bit and 17-bit words for 17+c bits */
793 result = (result & 0xffff) + (result >> 16);
794 /* add up 16-bit and 2-bit for 16+c bit */
795 result = (result & 0xffff) + (result >> 16);
797 result = (result & 0xffff) + (result >> 16);
800 csum = ~(ushort)result;
802 /* run time endian detection */
803 if (csum != htons(csum)) /* little endian */
809 int CDPSendTrigger(void)
820 et = (Ethernet_t *)pkt;
822 /* NOTE: trigger sent not on any VLAN */
824 /* form ethernet header */
825 memcpy(et->et_dest, NetCDPAddr, 6);
826 memcpy(et->et_src, NetOurEther, 6);
828 pkt += ETHER_HDR_SIZE;
831 memcpy((uchar *)pkt, CDP_SNAP_hdr, sizeof(CDP_SNAP_hdr));
832 pkt += sizeof(CDP_SNAP_hdr);
835 *pkt++ = 0x02; /* CDP version 2 */
836 *pkt++ = 180; /* TTL */
837 s = (volatile ushort *)pkt;
839 *s++ = htons(0); /* checksum (0 for later calculation) */
842 #ifdef CONFIG_CDP_DEVICE_ID
843 *s++ = htons(CDP_DEVICE_ID_TLV);
844 *s++ = htons(CONFIG_CDP_DEVICE_ID);
845 memset(buf, 0, sizeof(buf));
846 sprintf(buf, CONFIG_CDP_DEVICE_ID_PREFIX "%02X%02X%02X%02X%02X%02X",
847 NetOurEther[0] & 0xff, NetOurEther[1] & 0xff,
848 NetOurEther[2] & 0xff, NetOurEther[3] & 0xff,
849 NetOurEther[4] & 0xff, NetOurEther[5] & 0xff);
850 memcpy((uchar *)s, buf, 16);
854 #ifdef CONFIG_CDP_PORT_ID
855 *s++ = htons(CDP_PORT_ID_TLV);
856 memset(buf, 0, sizeof(buf));
857 sprintf(buf, CONFIG_CDP_PORT_ID, eth_get_dev_index());
859 if (len & 1) /* make it even */
861 *s++ = htons(len + 4);
862 memcpy((uchar *)s, buf, len);
866 #ifdef CONFIG_CDP_CAPABILITIES
867 *s++ = htons(CDP_CAPABILITIES_TLV);
869 *(ulong *)s = htonl(CONFIG_CDP_CAPABILITIES);
873 #ifdef CONFIG_CDP_VERSION
874 *s++ = htons(CDP_VERSION_TLV);
875 memset(buf, 0, sizeof(buf));
876 strcpy(buf, CONFIG_CDP_VERSION);
878 if (len & 1) /* make it even */
880 *s++ = htons(len + 4);
881 memcpy((uchar *)s, buf, len);
885 #ifdef CONFIG_CDP_PLATFORM
886 *s++ = htons(CDP_PLATFORM_TLV);
887 memset(buf, 0, sizeof(buf));
888 strcpy(buf, CONFIG_CDP_PLATFORM);
890 if (len & 1) /* make it even */
892 *s++ = htons(len + 4);
893 memcpy((uchar *)s, buf, len);
897 #ifdef CONFIG_CDP_TRIGGER
898 *s++ = htons(CDP_TRIGGER_TLV);
900 *(ulong *)s = htonl(CONFIG_CDP_TRIGGER);
904 #ifdef CONFIG_CDP_POWER_CONSUMPTION
905 *s++ = htons(CDP_POWER_CONSUMPTION_TLV);
907 *s++ = htons(CONFIG_CDP_POWER_CONSUMPTION);
910 /* length of ethernet packet */
911 len = (uchar *)s - ((uchar *)NetTxPacket + ETHER_HDR_SIZE);
912 et->et_protlen = htons(len);
914 len = ETHER_HDR_SIZE + sizeof(CDP_SNAP_hdr);
915 chksum = CDP_compute_csum((uchar *)NetTxPacket + len, (uchar *)s - (NetTxPacket + len));
920 (void) eth_send(NetTxPacket, (uchar *)s - NetTxPacket);
930 NetSetTimeout (CDP_TIMEOUT, CDPTimeout);
935 /* if not OK try again */
939 NetState = NETLOOP_SUCCESS;
943 CDPDummyHandler (uchar * pkt, unsigned dest, unsigned src, unsigned len)
949 CDPHandler(const uchar * pkt, unsigned len)
958 if (len < sizeof(CDP_SNAP_hdr) + 4)
961 /* check for valid CDP SNAP header */
962 if (memcmp(pkt, CDP_SNAP_hdr, sizeof(CDP_SNAP_hdr)) != 0)
965 pkt += sizeof(CDP_SNAP_hdr);
966 len -= sizeof(CDP_SNAP_hdr);
968 /* Version of CDP protocol must be >= 2 and TTL != 0 */
969 if (pkt[0] < 0x02 || pkt[1] == 0)
972 /* if version is greater than 0x02 maybe we'll have a problem; output a warning */
974 printf("** WARNING: CDP packet received with a protocol version %d > 2\n",
977 if (CDP_compute_csum(pkt, len) != 0)
989 ss = (const ushort *)pkt;
999 ss += 2; /* point ss to the data of the TLV */
1003 case CDP_DEVICE_ID_TLV:
1005 case CDP_ADDRESS_TLV:
1007 case CDP_PORT_ID_TLV:
1009 case CDP_CAPABILITIES_TLV:
1011 case CDP_VERSION_TLV:
1013 case CDP_PLATFORM_TLV:
1015 case CDP_NATIVE_VLAN_TLV:
1018 case CDP_APPLIANCE_VLAN_TLV:
1019 t = (const uchar *)ss;
1025 ss = (const ushort *)(t + 1);
1027 #ifdef CONFIG_CDP_APPLIANCE_VLAN_TYPE
1028 if (applid == CONFIG_CDP_APPLIANCE_VLAN_TYPE)
1031 vlan = ntohs(*ss); /* XXX will this work; dunno */
1036 case CDP_TRIGGER_TLV:
1038 case CDP_POWER_CONSUMPTION_TLV:
1040 case CDP_SYSNAME_TLV:
1042 case CDP_SYSOBJECT_TLV:
1044 case CDP_MANAGEMENT_ADDRESS_TLV:
1049 CDPApplianceVLAN = vlan;
1050 CDPNativeVLAN = nvlan;
1056 printf("** CDP packet is too short\n");
1060 static void CDPStart(void)
1062 #if defined(CONFIG_NET_MULTI)
1063 printf ("Using %s device\n", eth_get_name());
1068 CDPNativeVLAN = htons(-1);
1069 CDPApplianceVLAN = htons(-1);
1071 NetSetTimeout (CDP_TIMEOUT, CDPTimeout);
1072 NetSetHandler (CDPDummyHandler);
1081 NetReceive(volatile uchar * inpkt, int len)
1089 #if (CONFIG_COMMANDS & CFG_CMD_CDP)
1092 ushort cti = 0, vlanid = VLAN_NONE, myvlanid, mynvlanid;
1095 printf("packet received\n");
1100 et = (Ethernet_t *)inpkt;
1102 /* too small packet? */
1103 if (len < ETHER_HDR_SIZE)
1106 #if (CONFIG_COMMANDS & CFG_CMD_CDP)
1107 /* keep track if packet is CDP */
1108 iscdp = memcmp(et->et_dest, NetCDPAddr, 6) == 0;
1111 myvlanid = ntohs(NetOurVLAN);
1112 if (myvlanid == (ushort)-1)
1113 myvlanid = VLAN_NONE;
1114 mynvlanid = ntohs(NetOurNativeVLAN);
1115 if (mynvlanid == (ushort)-1)
1116 mynvlanid = VLAN_NONE;
1118 x = ntohs(et->et_protlen);
1121 printf("packet received\n");
1126 * Got a 802 packet. Check the other protocol field.
1128 x = ntohs(et->et_prot);
1130 ip = (IP_t *)(inpkt + E802_HDR_SIZE);
1131 len -= E802_HDR_SIZE;
1133 } else if (x != PROT_VLAN) { /* normal packet */
1134 ip = (IP_t *)(inpkt + ETHER_HDR_SIZE);
1135 len -= ETHER_HDR_SIZE;
1137 } else { /* VLAN packet */
1138 VLAN_Ethernet_t *vet = (VLAN_Ethernet_t *)et;
1141 printf("VLAN packet received\n");
1143 /* too small packet? */
1144 if (len < VLAN_ETHER_HDR_SIZE)
1147 /* if no VLAN active */
1148 if ((ntohs(NetOurVLAN) & VLAN_IDMASK) == VLAN_NONE
1149 #if (CONFIG_COMMANDS & CFG_CMD_CDP)
1155 cti = ntohs(vet->vet_tag);
1156 vlanid = cti & VLAN_IDMASK;
1157 x = ntohs(vet->vet_type);
1159 ip = (IP_t *)(inpkt + VLAN_ETHER_HDR_SIZE);
1160 len -= VLAN_ETHER_HDR_SIZE;
1164 printf("Receive from protocol 0x%x\n", x);
1167 #if (CONFIG_COMMANDS & CFG_CMD_CDP)
1169 CDPHandler((uchar *)ip, len);
1174 if ((myvlanid & VLAN_IDMASK) != VLAN_NONE) {
1175 if (vlanid == VLAN_NONE)
1176 vlanid = (mynvlanid & VLAN_IDMASK);
1178 if (vlanid != (myvlanid & VLAN_IDMASK))
1186 * We have to deal with two types of ARP packets:
1187 * - REQUEST packets will be answered by sending our
1188 * IP address - if we know it.
1189 * - REPLY packates are expected only after we asked
1190 * for the TFTP server's or the gateway's ethernet
1191 * address; so if we receive such a packet, we set
1192 * the server ethernet address
1198 if (len < ARP_HDR_SIZE) {
1199 printf("bad length %d < %d\n", len, ARP_HDR_SIZE);
1202 if (ntohs(arp->ar_hrd) != ARP_ETHER) {
1205 if (ntohs(arp->ar_pro) != PROT_IP) {
1208 if (arp->ar_hln != 6) {
1211 if (arp->ar_pln != 4) {
1215 if (NetOurIP == 0) {
1219 if (NetReadIP(&arp->ar_data[16]) != NetOurIP) {
1223 switch (ntohs(arp->ar_op)) {
1224 case ARPOP_REQUEST: /* reply with our IP address */
1226 puts ("Got ARP REQUEST, return our IP\n");
1229 pkt += NetSetEther(pkt, et->et_src, PROT_ARP);
1230 arp->ar_op = htons(ARPOP_REPLY);
1231 memcpy (&arp->ar_data[10], &arp->ar_data[0], 6);
1232 NetCopyIP(&arp->ar_data[16], &arp->ar_data[6]);
1233 memcpy (&arp->ar_data[ 0], NetOurEther, 6);
1234 NetCopyIP(&arp->ar_data[ 6], &NetOurIP);
1235 (void) eth_send((uchar *)et, (pkt - (uchar *)et) + ARP_HDR_SIZE);
1238 case ARPOP_REPLY: /* arp reply */
1239 /* are we waiting for a reply */
1240 if (!NetArpWaitPacketIP || !NetArpWaitPacketMAC)
1243 printf("Got ARP REPLY, set server/gtwy eth addr (%02x:%02x:%02x:%02x:%02x:%02x)\n",
1244 arp->ar_data[0], arp->ar_data[1],
1245 arp->ar_data[2], arp->ar_data[3],
1246 arp->ar_data[4], arp->ar_data[5]);
1249 tmp = NetReadIP(&arp->ar_data[6]);
1251 /* matched waiting packet's address */
1252 if (tmp == NetArpWaitReplyIP) {
1256 /* save address for later use */
1257 memcpy(NetArpWaitPacketMAC, &arp->ar_data[0], 6);
1259 /* modify header, and transmit it */
1260 memcpy(((Ethernet_t *)NetArpWaitTxPacket)->et_dest, NetArpWaitPacketMAC, 6);
1261 (void) eth_send(NetArpWaitTxPacket, NetArpWaitTxPacketSize);
1263 /* no arp request pending now */
1264 NetArpWaitPacketIP = 0;
1265 NetArpWaitTxPacketSize = 0;
1266 NetArpWaitPacketMAC = NULL;
1272 printf("Unexpected ARP opcode 0x%x\n", ntohs(arp->ar_op));
1279 puts ("Got RARP\n");
1282 if (len < ARP_HDR_SIZE) {
1283 printf("bad length %d < %d\n", len, ARP_HDR_SIZE);
1287 if ((ntohs(arp->ar_op) != RARPOP_REPLY) ||
1288 (ntohs(arp->ar_hrd) != ARP_ETHER) ||
1289 (ntohs(arp->ar_pro) != PROT_IP) ||
1290 (arp->ar_hln != 6) || (arp->ar_pln != 4)) {
1292 puts ("invalid RARP header\n");
1294 NetCopyIP(&NetOurIP, &arp->ar_data[16]);
1295 if (NetServerIP == 0)
1296 NetCopyIP(&NetServerIP, &arp->ar_data[ 6]);
1297 memcpy (NetServerEther, &arp->ar_data[ 0], 6);
1299 (*packetHandler)(0,0,0,0);
1307 if (len < IP_HDR_SIZE) {
1308 debug ("len bad %d < %d\n", len, IP_HDR_SIZE);
1311 if (len < ntohs(ip->ip_len)) {
1312 printf("len bad %d < %d\n", len, ntohs(ip->ip_len));
1315 len = ntohs(ip->ip_len);
1317 printf("len=%d, v=%02x\n", len, ip->ip_hl_v & 0xff);
1319 if ((ip->ip_hl_v & 0xf0) != 0x40) {
1322 if (ip->ip_off & htons(0x1fff)) { /* Can't deal w/ fragments */
1325 if (!NetCksumOk((uchar *)ip, IP_HDR_SIZE_NO_UDP / 2)) {
1326 puts ("checksum bad\n");
1329 tmp = NetReadIP(&ip->ip_dst);
1330 if (NetOurIP && tmp != NetOurIP && tmp != 0xFFFFFFFF) {
1334 * watch for ICMP host redirects
1336 * There is no real handler code (yet). We just watch
1337 * for ICMP host redirect messages. In case anybody
1338 * sees these messages: please contact me
1339 * (wd@denx.de), or - even better - send me the
1340 * necessary fixes :-)
1342 * Note: in all cases where I have seen this so far
1343 * it was a problem with the router configuration,
1344 * for instance when a router was configured in the
1345 * BOOTP reply, but the TFTP server was on the same
1346 * subnet. So this is probably a warning that your
1347 * configuration might be wrong. But I'm not really
1348 * sure if there aren't any other situations.
1350 if (ip->ip_p == IPPROTO_ICMP) {
1351 ICMP_t *icmph = (ICMP_t *)&(ip->udp_src);
1353 switch (icmph->type) {
1355 if (icmph->code != ICMP_REDIR_HOST)
1357 puts (" ICMP Host Redirect to ");
1358 print_IPaddr(icmph->un.gateway);
1361 #if (CONFIG_COMMANDS & CFG_CMD_PING)
1362 case ICMP_ECHO_REPLY:
1364 * IP header OK. Pass the packet to the current handler.
1366 /* XXX point to ip packet */
1367 (*packetHandler)((uchar *)ip, 0, 0, 0);
1373 } else if (ip->ip_p != IPPROTO_UDP) { /* Only UDP packets */
1378 * IP header OK. Pass the packet to the current handler.
1380 (*packetHandler)((uchar *)ip +IP_HDR_SIZE,
1383 ntohs(ip->udp_len) - 8);
1390 /**********************************************************************/
1392 static int net_check_prereq (proto_t protocol)
1396 #if (CONFIG_COMMANDS & CFG_CMD_PING)
1398 if (NetPingIP == 0) {
1399 puts ("*** ERROR: ping address not given\n");
1404 #if (CONFIG_COMMANDS & CFG_CMD_NFS)
1408 if (NetServerIP == 0) {
1409 puts ("*** ERROR: `serverip' not set\n");
1413 #if (CONFIG_COMMANDS & CFG_CMD_PING)
1417 if (NetOurIP == 0) {
1418 puts ("*** ERROR: `ipaddr' not set\n");
1427 if (memcmp(NetOurEther, "\0\0\0\0\0\0", 6) == 0) {
1428 #ifdef CONFIG_NET_MULTI
1429 extern int eth_get_dev_index (void);
1430 int num = eth_get_dev_index();
1434 puts ("*** ERROR: No ethernet found.\n");
1437 puts ("*** ERROR: `ethaddr' not set\n");
1440 printf ("*** ERROR: `eth%daddr' not set\n",
1448 puts ("*** ERROR: `ethaddr' not set\n");
1456 return (0); /* OK */
1458 /**********************************************************************/
1461 NetCksumOk(uchar * ptr, int len)
1463 return !((NetCksum(ptr, len) + 1) & 0xfffe);
1468 NetCksum(uchar * ptr, int len)
1474 xsum += *((ushort *)ptr)++;
1475 xsum = (xsum & 0xffff) + (xsum >> 16);
1476 xsum = (xsum & 0xffff) + (xsum >> 16);
1477 return (xsum & 0xffff);
1485 myvlanid = ntohs(NetOurVLAN);
1486 if (myvlanid == (ushort)-1)
1487 myvlanid = VLAN_NONE;
1489 return ((myvlanid & VLAN_IDMASK) == VLAN_NONE) ? ETHER_HDR_SIZE : VLAN_ETHER_HDR_SIZE;
1493 NetSetEther(volatile uchar * xet, uchar * addr, uint prot)
1495 Ethernet_t *et = (Ethernet_t *)xet;
1498 myvlanid = ntohs(NetOurVLAN);
1499 if (myvlanid == (ushort)-1)
1500 myvlanid = VLAN_NONE;
1502 memcpy (et->et_dest, addr, 6);
1503 memcpy (et->et_src, NetOurEther, 6);
1504 if ((myvlanid & VLAN_IDMASK) == VLAN_NONE) {
1505 et->et_protlen = htons(prot);
1506 return ETHER_HDR_SIZE;
1508 VLAN_Ethernet_t *vet = (VLAN_Ethernet_t *)xet;
1510 vet->vet_vlan_type = htons(PROT_VLAN);
1511 vet->vet_tag = htons((0 << 5) | (myvlanid & VLAN_IDMASK));
1512 vet->vet_type = htons(prot);
1513 return VLAN_ETHER_HDR_SIZE;
1518 NetSetIP(volatile uchar * xip, IPaddr_t dest, int dport, int sport, int len)
1520 volatile IP_t *ip = (IP_t *)xip;
1523 * If the data is an odd number of bytes, zero the
1524 * byte after the last byte so that the checksum
1528 xip[IP_HDR_SIZE + len] = 0;
1531 * Construct an IP and UDP header.
1532 (need to set no fragment bit - XXX)
1534 ip->ip_hl_v = 0x45; /* IP_HDR_SIZE / 4 (not including UDP) */
1536 ip->ip_len = htons(IP_HDR_SIZE + len);
1537 ip->ip_id = htons(NetIPID++);
1538 ip->ip_off = htons(0x4000); /* No fragmentation */
1540 ip->ip_p = 17; /* UDP */
1542 NetCopyIP((void*)&ip->ip_src, &NetOurIP); /* already in network byte order */
1543 NetCopyIP((void*)&ip->ip_dst, &dest); /* - "" - */
1544 ip->udp_src = htons(sport);
1545 ip->udp_dst = htons(dport);
1546 ip->udp_len = htons(8 + len);
1548 ip->ip_sum = ~NetCksum((uchar *)ip, IP_HDR_SIZE_NO_UDP / 2);
1551 void copy_filename (uchar *dst, uchar *src, int size)
1553 if (*src && (*src == '"')) {
1558 while ((--size > 0) && *src && (*src != '"')) {
1564 #endif /* CFG_CMD_NET */
1566 void ip_to_string (IPaddr_t x, char *s)
1569 sprintf (s, "%d.%d.%d.%d",
1570 (int) ((x >> 24) & 0xff),
1571 (int) ((x >> 16) & 0xff),
1572 (int) ((x >> 8) & 0xff), (int) ((x >> 0) & 0xff)
1576 IPaddr_t string_to_ip(char *s)
1585 for (addr=0, i=0; i<4; ++i) {
1586 ulong val = s ? simple_strtoul(s, &e, 10) : 0;
1588 addr |= (val & 0xFF);
1594 return (htonl(addr));
1597 void VLAN_to_string(ushort x, char *s)
1601 if (x == (ushort)-1)
1607 sprintf(s, "%d", x & VLAN_IDMASK);
1610 ushort string_to_VLAN(char *s)
1617 if (*s < '0' || *s > '9')
1620 id = (ushort)simple_strtoul(s, NULL, 10);
1625 void print_IPaddr (IPaddr_t x)
1629 ip_to_string (x, tmp);
1634 IPaddr_t getenv_IPaddr (char *var)
1636 return (string_to_ip(getenv(var)));
1639 ushort getenv_VLAN(char *var)
1641 return (string_to_VLAN(getenv(var)));