2 * Copied from Linux Monitor (LiMon) - Networking.
4 * Copyright 1994 - 2000 Neil Russell.
6 * Copyright 2000 Roland Borde
7 * Copyright 2000 Paolo Scaffardi
8 * Copyright 2000-2002 Wolfgang Denk, wd@denx.de
14 * The user interface supports commands for BOOTP, RARP, and TFTP.
15 * Also, we support ARP internally. Depending on available data,
16 * these interact as follows:
20 * Prerequisites: - own ethernet address
21 * We want: - own IP address
22 * - TFTP server IP address
28 * Prerequisites: - own ethernet address
29 * We want: - own IP address
30 * - TFTP server IP address
35 * Prerequisites: - own ethernet address
37 * - TFTP server IP address
38 * We want: - TFTP server ethernet address
43 * Prerequisites: - own ethernet address
44 * We want: - IP, Netmask, ServerIP, Gateway IP
45 * - bootfilename, lease time
50 * Prerequisites: - own ethernet address
52 * - TFTP server IP address
53 * - TFTP server ethernet address
54 * - name of bootfile (if unknown, we use a default name
55 * derived from our own IP address)
56 * We want: - load the boot file
61 * Prerequisites: - own ethernet address
63 * - name of bootfile (if unknown, we use a default name
64 * derived from our own IP address)
65 * We want: - load the boot file
70 * Prerequisites: - own ethernet address
72 * We want: - network time
85 #ifdef CONFIG_STATUS_LED
86 #include <status_led.h>
89 #if defined(CONFIG_CMD_SNTP)
92 #if defined(CONFIG_CDP_VERSION)
93 #include <timestamp.h>
96 #if defined(CONFIG_CMD_NET)
98 DECLARE_GLOBAL_DATA_PTR;
100 #ifndef CONFIG_ARP_TIMEOUT
101 # define ARP_TIMEOUT 5000UL /* Milliseconds before trying ARP again */
103 # define ARP_TIMEOUT CONFIG_ARP_TIMEOUT
107 #ifndef CONFIG_NET_RETRY_COUNT
108 # define ARP_TIMEOUT_COUNT 5 /* # of timeouts before giving up */
110 # define ARP_TIMEOUT_COUNT CONFIG_NET_RETRY_COUNT
117 /** BOOTP EXTENTIONS **/
119 IPaddr_t NetOurSubnetMask=0; /* Our subnet mask (0=unknown) */
120 IPaddr_t NetOurGatewayIP=0; /* Our gateways IP address */
121 IPaddr_t NetOurDNSIP=0; /* Our DNS IP address */
122 #if defined(CONFIG_BOOTP_DNS2)
123 IPaddr_t NetOurDNS2IP=0; /* Our 2nd DNS IP address */
125 char NetOurNISDomain[32]={0,}; /* Our NIS domain */
126 char NetOurHostName[32]={0,}; /* Our hostname */
127 char NetOurRootPath[64]={0,}; /* Our bootpath */
128 ushort NetBootFileSize=0; /* Our bootfile size in blocks */
130 #ifdef CONFIG_MCAST_TFTP /* Multicast TFTP */
134 /** END OF BOOTP EXTENTIONS **/
136 ulong NetBootFileXferSize; /* The actual transferred size of the bootfile (in bytes) */
137 uchar NetOurEther[6]; /* Our ethernet address */
138 uchar NetServerEther[6] = /* Boot server enet address */
139 { 0, 0, 0, 0, 0, 0 };
140 IPaddr_t NetOurIP; /* Our IP addr (0 = unknown) */
141 IPaddr_t NetServerIP; /* Server IP addr (0 = unknown) */
142 volatile uchar *NetRxPkt; /* Current receive packet */
143 int NetRxPktLen; /* Current rx packet length */
144 unsigned NetIPID; /* IP packet ID */
145 uchar NetBcastAddr[6] = /* Ethernet bcast address */
146 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
147 uchar NetEtherNullAddr[6] =
148 { 0, 0, 0, 0, 0, 0 };
150 void (*push_packet)(volatile void *, int len) = 0;
152 #if defined(CONFIG_CMD_CDP)
153 uchar NetCDPAddr[6] = /* Ethernet bcast address */
154 { 0x01, 0x00, 0x0c, 0xcc, 0xcc, 0xcc };
156 int NetState; /* Network loop state */
157 #ifdef CONFIG_NET_MULTI
158 int NetRestartWrap = 0; /* Tried all network devices */
159 static int NetRestarted = 0; /* Network loop restarted */
160 static int NetDevExists = 0; /* At least one device configured */
163 /* XXX in both little & big endian machines 0xFFFF == ntohs(-1) */
164 ushort NetOurVLAN = 0xFFFF; /* default is without VLAN */
165 ushort NetOurNativeVLAN = 0xFFFF; /* ditto */
167 char BootFile[128]; /* Boot File name */
169 #if defined(CONFIG_CMD_PING)
170 IPaddr_t NetPingIP; /* the ip address to ping */
172 static void PingStart(void);
175 #if defined(CONFIG_CMD_CDP)
176 static void CDPStart(void);
179 #if defined(CONFIG_CMD_SNTP)
180 IPaddr_t NetNtpServerIP; /* NTP server IP address */
181 int NetTimeOffset=0; /* offset time from UTC */
184 #ifdef CONFIG_NETCONSOLE
186 int nc_input_packet(uchar *pkt, unsigned dest, unsigned src, unsigned len);
189 volatile uchar PktBuf[(PKTBUFSRX+1) * PKTSIZE_ALIGN + PKTALIGN];
191 volatile uchar *NetRxPackets[PKTBUFSRX]; /* Receive packets */
193 static rxhand_f *packetHandler; /* Current RX packet handler */
194 static thand_f *timeHandler; /* Current timeout handler */
195 static ulong timeStart; /* Time base value */
196 static ulong timeDelta; /* Current timeout value */
197 volatile uchar *NetTxPacket = 0; /* THE transmit packet */
199 static int net_check_prereq (proto_t protocol);
201 /**********************************************************************/
203 IPaddr_t NetArpWaitPacketIP;
204 IPaddr_t NetArpWaitReplyIP;
205 uchar *NetArpWaitPacketMAC; /* MAC address of waiting packet's destination */
206 uchar *NetArpWaitTxPacket; /* THE transmit packet */
207 int NetArpWaitTxPacketSize;
208 uchar NetArpWaitPacketBuf[PKTSIZE_ALIGN + PKTALIGN];
209 ulong NetArpWaitTimerStart;
212 int env_changed_id = 0;
214 void ArpRequest (void)
221 printf ("ARP broadcast %d\n", NetArpWaitTry);
225 pkt += NetSetEther (pkt, NetBcastAddr, PROT_ARP);
229 arp->ar_hrd = htons (ARP_ETHER);
230 arp->ar_pro = htons (PROT_IP);
233 arp->ar_op = htons (ARPOP_REQUEST);
235 memcpy (&arp->ar_data[0], NetOurEther, 6); /* source ET addr */
236 NetWriteIP ((uchar *) & arp->ar_data[6], NetOurIP); /* source IP addr */
237 for (i = 10; i < 16; ++i) {
238 arp->ar_data[i] = 0; /* dest ET addr = 0 */
241 if ((NetArpWaitPacketIP & NetOurSubnetMask) !=
242 (NetOurIP & NetOurSubnetMask)) {
243 if (NetOurGatewayIP == 0) {
244 puts ("## Warning: gatewayip needed but not set\n");
245 NetArpWaitReplyIP = NetArpWaitPacketIP;
247 NetArpWaitReplyIP = NetOurGatewayIP;
250 NetArpWaitReplyIP = NetArpWaitPacketIP;
253 NetWriteIP ((uchar *) & arp->ar_data[16], NetArpWaitReplyIP);
254 (void) eth_send (NetTxPacket, (pkt - NetTxPacket) + ARP_HDR_SIZE);
257 void ArpTimeoutCheck(void)
261 if (!NetArpWaitPacketIP)
266 /* check for arp timeout */
267 if ((t - NetArpWaitTimerStart) > ARP_TIMEOUT) {
270 if (NetArpWaitTry >= ARP_TIMEOUT_COUNT) {
271 puts ("\nARP Retry count exceeded; starting again\n");
275 NetArpWaitTimerStart = t;
282 NetInitLoop(proto_t protocol)
285 int env_id = get_env_id ();
287 /* update only when the environment has changed */
288 if (env_changed_id == env_id)
292 #if defined(CONFIG_CMD_NFS)
295 #if defined(CONFIG_CMD_PING)
298 #if defined(CONFIG_CMD_SNTP)
303 NetCopyIP(&NetOurIP, &bd->bi_ip_addr);
304 NetOurGatewayIP = getenv_IPaddr ("gatewayip");
305 NetOurSubnetMask= getenv_IPaddr ("netmask");
306 NetOurVLAN = getenv_VLAN("vlan");
307 NetOurNativeVLAN = getenv_VLAN("nvlan");
310 #if defined(CONFIG_CMD_NFS)
315 NetServerIP = getenv_IPaddr ("serverip");
317 #if defined(CONFIG_CMD_PING)
322 #if defined(CONFIG_CMD_SNTP)
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");
349 env_changed_id = env_id;
353 /**********************************************************************/
355 * Main network processing loop.
359 NetLoop(proto_t protocol)
363 #ifdef CONFIG_NET_MULTI
368 /* XXX problem with bss workaround */
369 NetArpWaitPacketMAC = NULL;
370 NetArpWaitTxPacket = NULL;
371 NetArpWaitPacketIP = 0;
372 NetArpWaitReplyIP = 0;
373 NetArpWaitTxPacket = NULL;
379 * Setup packet buffers, aligned correctly.
381 NetTxPacket = &PktBuf[0] + (PKTALIGN - 1);
382 NetTxPacket -= (ulong)NetTxPacket % PKTALIGN;
383 for (i = 0; i < PKTBUFSRX; i++) {
384 NetRxPackets[i] = NetTxPacket + (i+1)*PKTSIZE_ALIGN;
388 if (!NetArpWaitTxPacket) {
389 NetArpWaitTxPacket = &NetArpWaitPacketBuf[0] + (PKTALIGN - 1);
390 NetArpWaitTxPacket -= (ulong)NetArpWaitTxPacket % PKTALIGN;
391 NetArpWaitTxPacketSize = 0;
395 #ifdef CONFIG_NET_MULTI
398 if (eth_init(bd) < 0) {
404 #ifdef CONFIG_NET_MULTI
405 memcpy (NetOurEther, eth_get_dev()->enetaddr, 6);
407 memcpy (NetOurEther, bd->bi_enetaddr, 6);
410 NetState = NETLOOP_CONTINUE;
413 * Start the ball rolling with the given start function. From
414 * here on, this code is a state machine driven by received
415 * packets and timer events.
417 NetInitLoop(protocol);
419 switch (net_check_prereq (protocol)) {
421 /* network not configured */
425 #ifdef CONFIG_NET_MULTI
427 /* network device not configured */
429 #endif /* CONFIG_NET_MULTI */
432 #ifdef CONFIG_NET_MULTI
437 /* always use ARP to get server ethernet address */
441 #if defined(CONFIG_CMD_DHCP)
443 /* Start with a clean slate... */
446 NetServerIP = getenv_IPaddr ("serverip");
447 DhcpRequest(); /* Basically same as BOOTP */
460 #if defined(CONFIG_CMD_PING)
465 #if defined(CONFIG_CMD_NFS)
470 #if defined(CONFIG_CMD_CDP)
475 #ifdef CONFIG_NETCONSOLE
480 #if defined(CONFIG_CMD_SNTP)
489 NetBootFileXferSize = 0;
493 #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
494 #if defined(CONFIG_SYS_FAULT_ECHO_LINK_DOWN) && defined(CONFIG_STATUS_LED) && defined(STATUS_LED_RED)
496 * Echo the inverted link state to the fault LED.
498 if(miiphy_link(eth_get_dev()->name, CONFIG_SYS_FAULT_MII_ADDR)) {
499 status_led_set (STATUS_LED_RED, STATUS_LED_OFF);
501 status_led_set (STATUS_LED_RED, STATUS_LED_ON);
503 #endif /* CONFIG_SYS_FAULT_ECHO_LINK_DOWN, ... */
504 #endif /* CONFIG_MII, ... */
507 * Main packet reception loop. Loop receiving packets until
508 * someone sets `NetState' to a state that terminates.
512 #ifdef CONFIG_SHOW_ACTIVITY
514 extern void show_activity(int arg);
519 * Check the ethernet for a new packet. The ethernet
520 * receive routine will process it.
525 * Abort if ctrl-c was pressed.
536 * Check for a timeout, and run the timeout handler
539 if (timeHandler && ((get_timer(0) - timeStart) > timeDelta)) {
542 #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
543 # if defined(CONFIG_SYS_FAULT_ECHO_LINK_DOWN) && \
544 defined(CONFIG_STATUS_LED) && \
545 defined(STATUS_LED_RED)
547 * Echo the inverted link state to the fault LED.
549 if(miiphy_link(eth_get_dev()->name, CONFIG_SYS_FAULT_MII_ADDR)) {
550 status_led_set (STATUS_LED_RED, STATUS_LED_OFF);
552 status_led_set (STATUS_LED_RED, STATUS_LED_ON);
554 # endif /* CONFIG_SYS_FAULT_ECHO_LINK_DOWN, ... */
555 #endif /* CONFIG_MII, ... */
557 timeHandler = (thand_f *)0;
564 case NETLOOP_RESTART:
565 #ifdef CONFIG_NET_MULTI
570 case NETLOOP_SUCCESS:
571 if (NetBootFileXferSize > 0) {
573 printf("Bytes transferred = %ld (%lx hex)\n",
575 NetBootFileXferSize);
576 sprintf(buf, "%lX", NetBootFileXferSize);
577 setenv("filesize", buf);
579 sprintf(buf, "%lX", (unsigned long)load_addr);
580 setenv("fileaddr", buf);
583 return NetBootFileXferSize;
591 /**********************************************************************/
594 startAgainTimeout(void)
596 NetState = NETLOOP_RESTART;
600 startAgainHandler(uchar * pkt, unsigned dest, unsigned src, unsigned len)
602 /* Totally ignore the packet */
605 void NetStartAgain (void)
608 int noretry = 0, once = 0;
610 if ((nretry = getenv ("netretry")) != NULL) {
611 noretry = (strcmp (nretry, "no") == 0);
612 once = (strcmp (nretry, "once") == 0);
616 NetState = NETLOOP_FAIL;
619 #ifndef CONFIG_NET_MULTI
620 NetSetTimeout (10000UL, startAgainTimeout);
621 NetSetHandler (startAgainHandler);
622 #else /* !CONFIG_NET_MULTI*/
624 #if !defined(CONFIG_NET_DO_NOT_TRY_ANOTHER)
625 eth_try_another (!NetRestarted);
628 if (NetRestartWrap) {
630 if (NetDevExists && !once) {
631 NetSetTimeout (10000UL, startAgainTimeout);
632 NetSetHandler (startAgainHandler);
634 NetState = NETLOOP_FAIL;
637 NetState = NETLOOP_RESTART;
639 #endif /* CONFIG_NET_MULTI */
642 /**********************************************************************/
648 NetSetHandler(rxhand_f * f)
655 NetSetTimeout(ulong iv, thand_f * f)
658 timeHandler = (thand_f *)0;
661 timeStart = get_timer(0);
668 NetSendPacket(volatile uchar * pkt, int len)
670 (void) eth_send(pkt, len);
674 NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport, int sport, int len)
678 /* convert to new style broadcast */
682 /* if broadcast, make the ether address a broadcast and don't do ARP */
683 if (dest == 0xFFFFFFFF)
684 ether = NetBcastAddr;
686 /* if MAC address was not discovered yet, save the packet and do an ARP request */
687 if (memcmp(ether, NetEtherNullAddr, 6) == 0) {
690 printf("sending ARP for %08lx\n", dest);
692 NetArpWaitPacketIP = dest;
693 NetArpWaitPacketMAC = ether;
695 pkt = NetArpWaitTxPacket;
696 pkt += NetSetEther (pkt, NetArpWaitPacketMAC, PROT_IP);
698 NetSetIP (pkt, dest, dport, sport, len);
699 memcpy(pkt + IP_HDR_SIZE, (uchar *)NetTxPacket + (pkt - (uchar *)NetArpWaitTxPacket) + IP_HDR_SIZE, len);
701 /* size of the waiting packet */
702 NetArpWaitTxPacketSize = (pkt - NetArpWaitTxPacket) + IP_HDR_SIZE + len;
704 /* and do the ARP request */
706 NetArpWaitTimerStart = get_timer(0);
708 return 1; /* waiting */
712 printf("sending UDP to %08lx/%02x:%02x:%02x:%02x:%02x:%02x\n",
713 dest, ether[0], ether[1], ether[2], ether[3], ether[4], ether[5]);
716 pkt = (uchar *)NetTxPacket;
717 pkt += NetSetEther (pkt, ether, PROT_IP);
718 NetSetIP (pkt, dest, dport, sport, len);
719 (void) eth_send(NetTxPacket, (pkt - NetTxPacket) + IP_HDR_SIZE + len);
721 return 0; /* transmitted */
724 #if defined(CONFIG_CMD_PING)
725 static ushort PingSeqNo;
734 /* XXX always send arp request */
736 memcpy(mac, NetEtherNullAddr, 6);
739 printf("sending ARP for %08lx\n", NetPingIP);
742 NetArpWaitPacketIP = NetPingIP;
743 NetArpWaitPacketMAC = mac;
745 pkt = NetArpWaitTxPacket;
746 pkt += NetSetEther(pkt, mac, PROT_IP);
748 ip = (volatile IP_t *)pkt;
751 * Construct an IP and ICMP header. (need to set no fragment bit - XXX)
753 ip->ip_hl_v = 0x45; /* IP_HDR_SIZE / 4 (not including UDP) */
755 ip->ip_len = htons(IP_HDR_SIZE_NO_UDP + 8);
756 ip->ip_id = htons(NetIPID++);
757 ip->ip_off = htons(IP_FLAGS_DFRAG); /* Don't fragment */
759 ip->ip_p = 0x01; /* ICMP */
761 NetCopyIP((void*)&ip->ip_src, &NetOurIP); /* already in network byte order */
762 NetCopyIP((void*)&ip->ip_dst, &NetPingIP); /* - "" - */
763 ip->ip_sum = ~NetCksum((uchar *)ip, IP_HDR_SIZE_NO_UDP / 2);
765 s = &ip->udp_src; /* XXX ICMP starts here */
766 s[0] = htons(0x0800); /* echo-request, code */
767 s[1] = 0; /* checksum */
768 s[2] = 0; /* identifier */
769 s[3] = htons(PingSeqNo++); /* sequence number */
770 s[1] = ~NetCksum((uchar *)s, 8/2);
772 /* size of the waiting packet */
773 NetArpWaitTxPacketSize = (pkt - NetArpWaitTxPacket) + IP_HDR_SIZE_NO_UDP + 8;
775 /* and do the ARP request */
777 NetArpWaitTimerStart = get_timer(0);
779 return 1; /* waiting */
786 NetState = NETLOOP_FAIL; /* we did not get the reply */
790 PingHandler (uchar * pkt, unsigned dest, unsigned src, unsigned len)
793 volatile IP_t *ip = (volatile IP_t *)pkt;
795 tmp = NetReadIP((void *)&ip->ip_src);
796 if (tmp != NetPingIP)
799 NetState = NETLOOP_SUCCESS;
802 static void PingStart(void)
804 #if defined(CONFIG_NET_MULTI)
805 printf ("Using %s device\n", eth_get_name());
806 #endif /* CONFIG_NET_MULTI */
807 NetSetTimeout (10000UL, PingTimeout);
808 NetSetHandler (PingHandler);
814 #if defined(CONFIG_CMD_CDP)
816 #define CDP_DEVICE_ID_TLV 0x0001
817 #define CDP_ADDRESS_TLV 0x0002
818 #define CDP_PORT_ID_TLV 0x0003
819 #define CDP_CAPABILITIES_TLV 0x0004
820 #define CDP_VERSION_TLV 0x0005
821 #define CDP_PLATFORM_TLV 0x0006
822 #define CDP_NATIVE_VLAN_TLV 0x000a
823 #define CDP_APPLIANCE_VLAN_TLV 0x000e
824 #define CDP_TRIGGER_TLV 0x000f
825 #define CDP_POWER_CONSUMPTION_TLV 0x0010
826 #define CDP_SYSNAME_TLV 0x0014
827 #define CDP_SYSOBJECT_TLV 0x0015
828 #define CDP_MANAGEMENT_ADDRESS_TLV 0x0016
830 #define CDP_TIMEOUT 250UL /* one packet every 250ms */
835 ushort CDPNativeVLAN;
836 ushort CDPApplianceVLAN;
838 static const uchar CDP_SNAP_hdr[8] = { 0xAA, 0xAA, 0x03, 0x00, 0x00, 0x0C, 0x20, 0x00 };
840 static ushort CDP_compute_csum(const uchar *buff, ushort len)
849 odd = 1 & (ulong)buff;
859 if (result & 0x80000000)
860 result = (result & 0xFFFF) + (result >> 16);
864 leftover = (signed short)(*(const signed char *)buff);
865 /* CISCO SUCKS big time! (and blows too):
866 * CDP uses the IP checksum algorithm with a twist;
867 * for the last byte it *sign* extends and sums.
869 result = (result & 0xffff0000) | ((result + leftover) & 0x0000ffff);
872 result = (result & 0xFFFF) + (result >> 16);
875 result = ((result >> 8) & 0xff) | ((result & 0xff) << 8);
878 /* add up 16-bit and 17-bit words for 17+c bits */
879 result = (result & 0xffff) + (result >> 16);
880 /* add up 16-bit and 2-bit for 16+c bit */
881 result = (result & 0xffff) + (result >> 16);
883 result = (result & 0xffff) + (result >> 16);
886 csum = ~(ushort)result;
888 /* run time endian detection */
889 if (csum != htons(csum)) /* little endian */
895 int CDPSendTrigger(void)
903 #if defined(CONFIG_CDP_DEVICE_ID) || defined(CONFIG_CDP_PORT_ID) || \
904 defined(CONFIG_CDP_VERSION) || defined(CONFIG_CDP_PLATFORM)
909 et = (Ethernet_t *)pkt;
911 /* NOTE: trigger sent not on any VLAN */
913 /* form ethernet header */
914 memcpy(et->et_dest, NetCDPAddr, 6);
915 memcpy(et->et_src, NetOurEther, 6);
917 pkt += ETHER_HDR_SIZE;
920 memcpy((uchar *)pkt, CDP_SNAP_hdr, sizeof(CDP_SNAP_hdr));
921 pkt += sizeof(CDP_SNAP_hdr);
924 *pkt++ = 0x02; /* CDP version 2 */
925 *pkt++ = 180; /* TTL */
926 s = (volatile ushort *)pkt;
928 *s++ = htons(0); /* checksum (0 for later calculation) */
931 #ifdef CONFIG_CDP_DEVICE_ID
932 *s++ = htons(CDP_DEVICE_ID_TLV);
933 *s++ = htons(CONFIG_CDP_DEVICE_ID);
934 memset(buf, 0, sizeof(buf));
935 sprintf(buf, CONFIG_CDP_DEVICE_ID_PREFIX "%02X%02X%02X%02X%02X%02X",
936 NetOurEther[0] & 0xff, NetOurEther[1] & 0xff,
937 NetOurEther[2] & 0xff, NetOurEther[3] & 0xff,
938 NetOurEther[4] & 0xff, NetOurEther[5] & 0xff);
939 memcpy((uchar *)s, buf, 16);
943 #ifdef CONFIG_CDP_PORT_ID
944 *s++ = htons(CDP_PORT_ID_TLV);
945 memset(buf, 0, sizeof(buf));
946 sprintf(buf, CONFIG_CDP_PORT_ID, eth_get_dev_index());
948 if (len & 1) /* make it even */
950 *s++ = htons(len + 4);
951 memcpy((uchar *)s, buf, len);
955 #ifdef CONFIG_CDP_CAPABILITIES
956 *s++ = htons(CDP_CAPABILITIES_TLV);
958 *(ulong *)s = htonl(CONFIG_CDP_CAPABILITIES);
962 #ifdef CONFIG_CDP_VERSION
963 *s++ = htons(CDP_VERSION_TLV);
964 memset(buf, 0, sizeof(buf));
965 strcpy(buf, CONFIG_CDP_VERSION);
967 if (len & 1) /* make it even */
969 *s++ = htons(len + 4);
970 memcpy((uchar *)s, buf, len);
974 #ifdef CONFIG_CDP_PLATFORM
975 *s++ = htons(CDP_PLATFORM_TLV);
976 memset(buf, 0, sizeof(buf));
977 strcpy(buf, CONFIG_CDP_PLATFORM);
979 if (len & 1) /* make it even */
981 *s++ = htons(len + 4);
982 memcpy((uchar *)s, buf, len);
986 #ifdef CONFIG_CDP_TRIGGER
987 *s++ = htons(CDP_TRIGGER_TLV);
989 *(ulong *)s = htonl(CONFIG_CDP_TRIGGER);
993 #ifdef CONFIG_CDP_POWER_CONSUMPTION
994 *s++ = htons(CDP_POWER_CONSUMPTION_TLV);
996 *s++ = htons(CONFIG_CDP_POWER_CONSUMPTION);
999 /* length of ethernet packet */
1000 len = (uchar *)s - ((uchar *)NetTxPacket + ETHER_HDR_SIZE);
1001 et->et_protlen = htons(len);
1003 len = ETHER_HDR_SIZE + sizeof(CDP_SNAP_hdr);
1004 chksum = CDP_compute_csum((uchar *)NetTxPacket + len, (uchar *)s - (NetTxPacket + len));
1007 *cp = htons(chksum);
1009 (void) eth_send(NetTxPacket, (uchar *)s - NetTxPacket);
1019 NetSetTimeout (CDP_TIMEOUT, CDPTimeout);
1024 /* if not OK try again */
1028 NetState = NETLOOP_SUCCESS;
1032 CDPDummyHandler (uchar * pkt, unsigned dest, unsigned src, unsigned len)
1038 CDPHandler(const uchar * pkt, unsigned len)
1047 if (len < sizeof(CDP_SNAP_hdr) + 4)
1050 /* check for valid CDP SNAP header */
1051 if (memcmp(pkt, CDP_SNAP_hdr, sizeof(CDP_SNAP_hdr)) != 0)
1054 pkt += sizeof(CDP_SNAP_hdr);
1055 len -= sizeof(CDP_SNAP_hdr);
1057 /* Version of CDP protocol must be >= 2 and TTL != 0 */
1058 if (pkt[0] < 0x02 || pkt[1] == 0)
1061 /* if version is greater than 0x02 maybe we'll have a problem; output a warning */
1063 printf("** WARNING: CDP packet received with a protocol version %d > 2\n",
1066 if (CDP_compute_csum(pkt, len) != 0)
1078 ss = (const ushort *)pkt;
1079 type = ntohs(ss[0]);
1080 tlen = ntohs(ss[1]);
1088 ss += 2; /* point ss to the data of the TLV */
1092 case CDP_DEVICE_ID_TLV:
1094 case CDP_ADDRESS_TLV:
1096 case CDP_PORT_ID_TLV:
1098 case CDP_CAPABILITIES_TLV:
1100 case CDP_VERSION_TLV:
1102 case CDP_PLATFORM_TLV:
1104 case CDP_NATIVE_VLAN_TLV:
1107 case CDP_APPLIANCE_VLAN_TLV:
1108 t = (const uchar *)ss;
1114 ss = (const ushort *)(t + 1);
1116 #ifdef CONFIG_CDP_APPLIANCE_VLAN_TYPE
1117 if (applid == CONFIG_CDP_APPLIANCE_VLAN_TYPE)
1120 vlan = ntohs(*ss); /* XXX will this work; dunno */
1125 case CDP_TRIGGER_TLV:
1127 case CDP_POWER_CONSUMPTION_TLV:
1129 case CDP_SYSNAME_TLV:
1131 case CDP_SYSOBJECT_TLV:
1133 case CDP_MANAGEMENT_ADDRESS_TLV:
1138 CDPApplianceVLAN = vlan;
1139 CDPNativeVLAN = nvlan;
1145 printf("** CDP packet is too short\n");
1149 static void CDPStart(void)
1151 #if defined(CONFIG_NET_MULTI)
1152 printf ("Using %s device\n", eth_get_name());
1157 CDPNativeVLAN = htons(-1);
1158 CDPApplianceVLAN = htons(-1);
1160 NetSetTimeout (CDP_TIMEOUT, CDPTimeout);
1161 NetSetHandler (CDPDummyHandler);
1169 NetReceive(volatile uchar * inpkt, int len)
1177 #if defined(CONFIG_CMD_CDP)
1180 ushort cti = 0, vlanid = VLAN_NONE, myvlanid, mynvlanid;
1183 printf("packet received\n");
1188 et = (Ethernet_t *)inpkt;
1190 /* too small packet? */
1191 if (len < ETHER_HDR_SIZE)
1196 (*push_packet)(inpkt, len);
1201 #if defined(CONFIG_CMD_CDP)
1202 /* keep track if packet is CDP */
1203 iscdp = memcmp(et->et_dest, NetCDPAddr, 6) == 0;
1206 myvlanid = ntohs(NetOurVLAN);
1207 if (myvlanid == (ushort)-1)
1208 myvlanid = VLAN_NONE;
1209 mynvlanid = ntohs(NetOurNativeVLAN);
1210 if (mynvlanid == (ushort)-1)
1211 mynvlanid = VLAN_NONE;
1213 x = ntohs(et->et_protlen);
1216 printf("packet received\n");
1221 * Got a 802 packet. Check the other protocol field.
1223 x = ntohs(et->et_prot);
1225 ip = (IP_t *)(inpkt + E802_HDR_SIZE);
1226 len -= E802_HDR_SIZE;
1228 } else if (x != PROT_VLAN) { /* normal packet */
1229 ip = (IP_t *)(inpkt + ETHER_HDR_SIZE);
1230 len -= ETHER_HDR_SIZE;
1232 } else { /* VLAN packet */
1233 VLAN_Ethernet_t *vet = (VLAN_Ethernet_t *)et;
1236 printf("VLAN packet received\n");
1238 /* too small packet? */
1239 if (len < VLAN_ETHER_HDR_SIZE)
1242 /* if no VLAN active */
1243 if ((ntohs(NetOurVLAN) & VLAN_IDMASK) == VLAN_NONE
1244 #if defined(CONFIG_CMD_CDP)
1250 cti = ntohs(vet->vet_tag);
1251 vlanid = cti & VLAN_IDMASK;
1252 x = ntohs(vet->vet_type);
1254 ip = (IP_t *)(inpkt + VLAN_ETHER_HDR_SIZE);
1255 len -= VLAN_ETHER_HDR_SIZE;
1259 printf("Receive from protocol 0x%x\n", x);
1262 #if defined(CONFIG_CMD_CDP)
1264 CDPHandler((uchar *)ip, len);
1269 if ((myvlanid & VLAN_IDMASK) != VLAN_NONE) {
1270 if (vlanid == VLAN_NONE)
1271 vlanid = (mynvlanid & VLAN_IDMASK);
1273 if (vlanid != (myvlanid & VLAN_IDMASK))
1281 * We have to deal with two types of ARP packets:
1282 * - REQUEST packets will be answered by sending our
1283 * IP address - if we know it.
1284 * - REPLY packates are expected only after we asked
1285 * for the TFTP server's or the gateway's ethernet
1286 * address; so if we receive such a packet, we set
1287 * the server ethernet address
1293 if (len < ARP_HDR_SIZE) {
1294 printf("bad length %d < %d\n", len, ARP_HDR_SIZE);
1297 if (ntohs(arp->ar_hrd) != ARP_ETHER) {
1300 if (ntohs(arp->ar_pro) != PROT_IP) {
1303 if (arp->ar_hln != 6) {
1306 if (arp->ar_pln != 4) {
1310 if (NetOurIP == 0) {
1314 if (NetReadIP(&arp->ar_data[16]) != NetOurIP) {
1318 switch (ntohs(arp->ar_op)) {
1319 case ARPOP_REQUEST: /* reply with our IP address */
1321 puts ("Got ARP REQUEST, return our IP\n");
1324 pkt += NetSetEther(pkt, et->et_src, PROT_ARP);
1325 arp->ar_op = htons(ARPOP_REPLY);
1326 memcpy (&arp->ar_data[10], &arp->ar_data[0], 6);
1327 NetCopyIP(&arp->ar_data[16], &arp->ar_data[6]);
1328 memcpy (&arp->ar_data[ 0], NetOurEther, 6);
1329 NetCopyIP(&arp->ar_data[ 6], &NetOurIP);
1330 (void) eth_send((uchar *)et, (pkt - (uchar *)et) + ARP_HDR_SIZE);
1333 case ARPOP_REPLY: /* arp reply */
1334 /* are we waiting for a reply */
1335 if (!NetArpWaitPacketIP || !NetArpWaitPacketMAC)
1338 printf("Got ARP REPLY, set server/gtwy eth addr (%02x:%02x:%02x:%02x:%02x:%02x)\n",
1339 arp->ar_data[0], arp->ar_data[1],
1340 arp->ar_data[2], arp->ar_data[3],
1341 arp->ar_data[4], arp->ar_data[5]);
1344 tmp = NetReadIP(&arp->ar_data[6]);
1346 /* matched waiting packet's address */
1347 if (tmp == NetArpWaitReplyIP) {
1351 /* save address for later use */
1352 memcpy(NetArpWaitPacketMAC, &arp->ar_data[0], 6);
1354 #ifdef CONFIG_NETCONSOLE
1355 (*packetHandler)(0,0,0,0);
1357 /* modify header, and transmit it */
1358 memcpy(((Ethernet_t *)NetArpWaitTxPacket)->et_dest, NetArpWaitPacketMAC, 6);
1359 (void) eth_send(NetArpWaitTxPacket, NetArpWaitTxPacketSize);
1361 /* no arp request pending now */
1362 NetArpWaitPacketIP = 0;
1363 NetArpWaitTxPacketSize = 0;
1364 NetArpWaitPacketMAC = NULL;
1370 printf("Unexpected ARP opcode 0x%x\n", ntohs(arp->ar_op));
1378 puts ("Got RARP\n");
1381 if (len < ARP_HDR_SIZE) {
1382 printf("bad length %d < %d\n", len, ARP_HDR_SIZE);
1386 if ((ntohs(arp->ar_op) != RARPOP_REPLY) ||
1387 (ntohs(arp->ar_hrd) != ARP_ETHER) ||
1388 (ntohs(arp->ar_pro) != PROT_IP) ||
1389 (arp->ar_hln != 6) || (arp->ar_pln != 4)) {
1391 puts ("invalid RARP header\n");
1393 NetCopyIP(&NetOurIP, &arp->ar_data[16]);
1394 if (NetServerIP == 0)
1395 NetCopyIP(&NetServerIP, &arp->ar_data[ 6]);
1396 memcpy (NetServerEther, &arp->ar_data[ 0], 6);
1398 (*packetHandler)(0,0,0,0);
1406 if (len < IP_HDR_SIZE) {
1407 debug ("len bad %d < %lu\n", len, (ulong)IP_HDR_SIZE);
1410 if (len < ntohs(ip->ip_len)) {
1411 printf("len bad %d < %d\n", len, ntohs(ip->ip_len));
1414 len = ntohs(ip->ip_len);
1416 printf("len=%d, v=%02x\n", len, ip->ip_hl_v & 0xff);
1418 if ((ip->ip_hl_v & 0xf0) != 0x40) {
1421 /* Can't deal with fragments */
1422 if (ip->ip_off & htons(IP_OFFS | IP_FLAGS_MFRAG)) {
1425 /* can't deal with headers > 20 bytes */
1426 if ((ip->ip_hl_v & 0x0f) > 0x05) {
1429 if (!NetCksumOk((uchar *)ip, IP_HDR_SIZE_NO_UDP / 2)) {
1430 puts ("checksum bad\n");
1433 tmp = NetReadIP(&ip->ip_dst);
1434 if (NetOurIP && tmp != NetOurIP && tmp != 0xFFFFFFFF) {
1435 #ifdef CONFIG_MCAST_TFTP
1436 if (Mcast_addr != tmp)
1441 * watch for ICMP host redirects
1443 * There is no real handler code (yet). We just watch
1444 * for ICMP host redirect messages. In case anybody
1445 * sees these messages: please contact me
1446 * (wd@denx.de), or - even better - send me the
1447 * necessary fixes :-)
1449 * Note: in all cases where I have seen this so far
1450 * it was a problem with the router configuration,
1451 * for instance when a router was configured in the
1452 * BOOTP reply, but the TFTP server was on the same
1453 * subnet. So this is probably a warning that your
1454 * configuration might be wrong. But I'm not really
1455 * sure if there aren't any other situations.
1457 if (ip->ip_p == IPPROTO_ICMP) {
1458 ICMP_t *icmph = (ICMP_t *)&(ip->udp_src);
1460 switch (icmph->type) {
1462 if (icmph->code != ICMP_REDIR_HOST)
1464 puts (" ICMP Host Redirect to ");
1465 print_IPaddr(icmph->un.gateway);
1468 #if defined(CONFIG_CMD_PING)
1469 case ICMP_ECHO_REPLY:
1471 * IP header OK. Pass the packet to the current handler.
1473 /* XXX point to ip packet */
1474 (*packetHandler)((uchar *)ip, 0, 0, 0);
1476 case ICMP_ECHO_REQUEST:
1478 printf ("Got ICMP ECHO REQUEST, return %d bytes \n",
1479 ETHER_HDR_SIZE + len);
1481 memcpy (&et->et_dest[0], &et->et_src[0], 6);
1482 memcpy (&et->et_src[ 0], NetOurEther, 6);
1486 NetCopyIP((void*)&ip->ip_dst, &ip->ip_src);
1487 NetCopyIP((void*)&ip->ip_src, &NetOurIP);
1488 ip->ip_sum = ~NetCksum((uchar *)ip, IP_HDR_SIZE_NO_UDP >> 1);
1490 icmph->type = ICMP_ECHO_REPLY;
1491 icmph->checksum = 0;
1492 icmph->checksum = ~NetCksum((uchar *)icmph,
1493 (len - IP_HDR_SIZE_NO_UDP) >> 1);
1494 (void) eth_send((uchar *)et, ETHER_HDR_SIZE + len);
1500 } else if (ip->ip_p != IPPROTO_UDP) { /* Only UDP packets */
1504 #ifdef CONFIG_UDP_CHECKSUM
1505 if (ip->udp_xsum != 0) {
1511 xsum += (ntohs(ip->udp_len));
1512 xsum += (ntohl(ip->ip_src) >> 16) & 0x0000ffff;
1513 xsum += (ntohl(ip->ip_src) >> 0) & 0x0000ffff;
1514 xsum += (ntohl(ip->ip_dst) >> 16) & 0x0000ffff;
1515 xsum += (ntohl(ip->ip_dst) >> 0) & 0x0000ffff;
1517 sumlen = ntohs(ip->udp_len);
1518 sumptr = (ushort *) &(ip->udp_src);
1520 while (sumlen > 1) {
1523 sumdata = *sumptr++;
1524 xsum += ntohs(sumdata);
1530 sumdata = *(unsigned char *) sumptr;
1531 sumdata = (sumdata << 8) & 0xff00;
1534 while ((xsum >> 16) != 0) {
1535 xsum = (xsum & 0x0000ffff) + ((xsum >> 16) & 0x0000ffff);
1537 if ((xsum != 0x00000000) && (xsum != 0x0000ffff)) {
1538 printf(" UDP wrong checksum %08lx %08x\n",
1539 xsum, ntohs(ip->udp_xsum));
1546 #ifdef CONFIG_NETCONSOLE
1547 nc_input_packet((uchar *)ip +IP_HDR_SIZE,
1550 ntohs(ip->udp_len) - 8);
1553 * IP header OK. Pass the packet to the current handler.
1555 (*packetHandler)((uchar *)ip +IP_HDR_SIZE,
1558 ntohs(ip->udp_len) - 8);
1564 /**********************************************************************/
1566 static int net_check_prereq (proto_t protocol)
1570 #if defined(CONFIG_CMD_PING)
1572 if (NetPingIP == 0) {
1573 puts ("*** ERROR: ping address not given\n");
1578 #if defined(CONFIG_CMD_SNTP)
1580 if (NetNtpServerIP == 0) {
1581 puts ("*** ERROR: NTP server address not given\n");
1586 #if defined(CONFIG_CMD_NFS)
1591 if (NetServerIP == 0) {
1592 puts ("*** ERROR: `serverip' not set\n");
1595 #if defined(CONFIG_CMD_PING) || defined(CONFIG_CMD_SNTP)
1599 if (NetOurIP == 0) {
1600 puts ("*** ERROR: `ipaddr' not set\n");
1609 if (memcmp (NetOurEther, "\0\0\0\0\0\0", 6) == 0) {
1610 #ifdef CONFIG_NET_MULTI
1611 extern int eth_get_dev_index (void);
1612 int num = eth_get_dev_index ();
1616 puts ("*** ERROR: No ethernet found.\n");
1619 puts ("*** ERROR: `ethaddr' not set\n");
1622 printf ("*** ERROR: `eth%daddr' not set\n",
1630 puts ("*** ERROR: `ethaddr' not set\n");
1638 return (0); /* OK */
1640 /**********************************************************************/
1643 NetCksumOk(uchar * ptr, int len)
1645 return !((NetCksum(ptr, len) + 1) & 0xfffe);
1650 NetCksum(uchar * ptr, int len)
1653 ushort *p = (ushort *)ptr;
1658 xsum = (xsum & 0xffff) + (xsum >> 16);
1659 xsum = (xsum & 0xffff) + (xsum >> 16);
1660 return (xsum & 0xffff);
1668 myvlanid = ntohs(NetOurVLAN);
1669 if (myvlanid == (ushort)-1)
1670 myvlanid = VLAN_NONE;
1672 return ((myvlanid & VLAN_IDMASK) == VLAN_NONE) ? ETHER_HDR_SIZE : VLAN_ETHER_HDR_SIZE;
1676 NetSetEther(volatile uchar * xet, uchar * addr, uint prot)
1678 Ethernet_t *et = (Ethernet_t *)xet;
1681 myvlanid = ntohs(NetOurVLAN);
1682 if (myvlanid == (ushort)-1)
1683 myvlanid = VLAN_NONE;
1685 memcpy (et->et_dest, addr, 6);
1686 memcpy (et->et_src, NetOurEther, 6);
1687 if ((myvlanid & VLAN_IDMASK) == VLAN_NONE) {
1688 et->et_protlen = htons(prot);
1689 return ETHER_HDR_SIZE;
1691 VLAN_Ethernet_t *vet = (VLAN_Ethernet_t *)xet;
1693 vet->vet_vlan_type = htons(PROT_VLAN);
1694 vet->vet_tag = htons((0 << 5) | (myvlanid & VLAN_IDMASK));
1695 vet->vet_type = htons(prot);
1696 return VLAN_ETHER_HDR_SIZE;
1701 NetSetIP(volatile uchar * xip, IPaddr_t dest, int dport, int sport, int len)
1703 IP_t *ip = (IP_t *)xip;
1706 * If the data is an odd number of bytes, zero the
1707 * byte after the last byte so that the checksum
1711 xip[IP_HDR_SIZE + len] = 0;
1714 * Construct an IP and UDP header.
1715 * (need to set no fragment bit - XXX)
1717 ip->ip_hl_v = 0x45; /* IP_HDR_SIZE / 4 (not including UDP) */
1719 ip->ip_len = htons(IP_HDR_SIZE + len);
1720 ip->ip_id = htons(NetIPID++);
1721 ip->ip_off = htons(IP_FLAGS_DFRAG); /* Don't fragment */
1723 ip->ip_p = 17; /* UDP */
1725 NetCopyIP((void*)&ip->ip_src, &NetOurIP); /* already in network byte order */
1726 NetCopyIP((void*)&ip->ip_dst, &dest); /* - "" - */
1727 ip->udp_src = htons(sport);
1728 ip->udp_dst = htons(dport);
1729 ip->udp_len = htons(8 + len);
1731 ip->ip_sum = ~NetCksum((uchar *)ip, IP_HDR_SIZE_NO_UDP / 2);
1734 void copy_filename (char *dst, char *src, int size)
1736 if (*src && (*src == '"')) {
1741 while ((--size > 0) && *src && (*src != '"')) {
1749 void ip_to_string (IPaddr_t x, char *s)
1752 sprintf (s, "%d.%d.%d.%d",
1753 (int) ((x >> 24) & 0xff),
1754 (int) ((x >> 16) & 0xff),
1755 (int) ((x >> 8) & 0xff), (int) ((x >> 0) & 0xff)
1759 IPaddr_t string_to_ip(char *s)
1768 for (addr=0, i=0; i<4; ++i) {
1769 ulong val = s ? simple_strtoul(s, &e, 10) : 0;
1771 addr |= (val & 0xFF);
1777 return (htonl(addr));
1780 void VLAN_to_string(ushort x, char *s)
1784 if (x == (ushort)-1)
1790 sprintf(s, "%d", x & VLAN_IDMASK);
1793 ushort string_to_VLAN(char *s)
1798 return htons(VLAN_NONE);
1800 if (*s < '0' || *s > '9')
1803 id = (ushort)simple_strtoul(s, NULL, 10);
1808 void print_IPaddr (IPaddr_t x)
1812 ip_to_string (x, tmp);
1817 IPaddr_t getenv_IPaddr (char *var)
1819 return (string_to_ip(getenv(var)));
1822 ushort getenv_VLAN(char *var)
1824 return (string_to_VLAN(getenv(var)));