2 * Copied from Linux Monitor (LiMon) - Networking.
4 * Copyright 1994 - 2000 Neil Russell.
6 * Copyright 2000 Roland Borde
7 * Copyright 2000 Paolo Scaffardi
8 * Copyright 2000-2002 Wolfgang Denk, wd@denx.de
14 * The user interface supports commands for BOOTP, RARP, and TFTP.
15 * Also, we support ARP internally. Depending on available data,
16 * these interact as follows:
20 * Prerequisites: - own ethernet address
21 * We want: - own IP address
22 * - TFTP server IP address
28 * Prerequisites: - own ethernet address
29 * We want: - own IP address
30 * - TFTP server IP address
35 * Prerequisites: - own ethernet address
37 * - TFTP server IP address
38 * We want: - TFTP server ethernet address
43 * Prerequisites: - own ethernet address
44 * We want: - IP, Netmask, ServerIP, Gateway IP
45 * - bootfilename, lease time
50 * Prerequisites: - own ethernet address
52 * - TFTP server IP address
53 * - TFTP server ethernet address
54 * - name of bootfile (if unknown, we use a default name
55 * derived from our own IP address)
56 * We want: - load the boot file
61 * Prerequisites: - own ethernet address
63 * - name of bootfile (if unknown, we use a default name
64 * derived from our own IP address)
65 * We want: - load the boot file
70 * Prerequisites: - own ethernet address
72 * We want: - network time
85 #ifdef CONFIG_STATUS_LED
86 #include <status_led.h>
89 #if defined(CONFIG_CMD_SNTP)
93 #if defined(CONFIG_CMD_NET)
95 DECLARE_GLOBAL_DATA_PTR;
97 #define ARP_TIMEOUT 5 /* Seconds before trying ARP again */
98 #ifndef CONFIG_NET_RETRY_COUNT
99 # define ARP_TIMEOUT_COUNT 5 /* # of timeouts before giving up */
101 # define ARP_TIMEOUT_COUNT (CONFIG_NET_RETRY_COUNT)
108 /** BOOTP EXTENTIONS **/
110 IPaddr_t NetOurSubnetMask=0; /* Our subnet mask (0=unknown) */
111 IPaddr_t NetOurGatewayIP=0; /* Our gateways IP address */
112 IPaddr_t NetOurDNSIP=0; /* Our DNS IP address */
113 #if defined(CONFIG_BOOTP_DNS2)
114 IPaddr_t NetOurDNS2IP=0; /* Our 2nd DNS IP address */
116 char NetOurNISDomain[32]={0,}; /* Our NIS domain */
117 char NetOurHostName[32]={0,}; /* Our hostname */
118 char NetOurRootPath[64]={0,}; /* Our bootpath */
119 ushort NetBootFileSize=0; /* Our bootfile size in blocks */
121 /** END OF BOOTP EXTENTIONS **/
123 ulong NetBootFileXferSize; /* The actual transferred size of the bootfile (in bytes) */
124 uchar NetOurEther[6]; /* Our ethernet address */
125 uchar NetServerEther[6] = /* Boot server enet address */
126 { 0, 0, 0, 0, 0, 0 };
127 IPaddr_t NetOurIP; /* Our IP addr (0 = unknown) */
128 IPaddr_t NetServerIP; /* Our IP addr (0 = unknown) */
129 volatile uchar *NetRxPkt; /* Current receive packet */
130 int NetRxPktLen; /* Current rx packet length */
131 unsigned NetIPID; /* IP packet ID */
132 uchar NetBcastAddr[6] = /* Ethernet bcast address */
133 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
134 uchar NetEtherNullAddr[6] =
135 { 0, 0, 0, 0, 0, 0 };
136 #if defined(CONFIG_CMD_CDP)
137 uchar NetCDPAddr[6] = /* Ethernet bcast address */
138 { 0x01, 0x00, 0x0c, 0xcc, 0xcc, 0xcc };
140 int NetState; /* Network loop state */
141 #ifdef CONFIG_NET_MULTI
142 int NetRestartWrap = 0; /* Tried all network devices */
143 static int NetRestarted = 0; /* Network loop restarted */
144 static int NetDevExists = 0; /* At least one device configured */
147 /* XXX in both little & big endian machines 0xFFFF == ntohs(-1) */
148 ushort NetOurVLAN = 0xFFFF; /* default is without VLAN */
149 ushort NetOurNativeVLAN = 0xFFFF; /* ditto */
151 char BootFile[128]; /* Boot File name */
153 #if defined(CONFIG_CMD_PING)
154 IPaddr_t NetPingIP; /* the ip address to ping */
156 static void PingStart(void);
159 #if defined(CONFIG_CMD_CDP)
160 static void CDPStart(void);
163 #if defined(CONFIG_CMD_SNTP)
164 IPaddr_t NetNtpServerIP; /* NTP server IP address */
165 int NetTimeOffset=0; /* offset time from UTC */
168 #ifdef CONFIG_NETCONSOLE
170 int nc_input_packet(uchar *pkt, unsigned dest, unsigned src, unsigned len);
173 volatile uchar PktBuf[(PKTBUFSRX+1) * PKTSIZE_ALIGN + PKTALIGN];
175 volatile uchar *NetRxPackets[PKTBUFSRX]; /* Receive packets */
177 static rxhand_f *packetHandler; /* Current RX packet handler */
178 static thand_f *timeHandler; /* Current timeout handler */
179 static ulong timeStart; /* Time base value */
180 static ulong timeDelta; /* Current timeout value */
181 volatile uchar *NetTxPacket = 0; /* THE transmit packet */
183 static int net_check_prereq (proto_t protocol);
185 /**********************************************************************/
187 IPaddr_t NetArpWaitPacketIP;
188 IPaddr_t NetArpWaitReplyIP;
189 uchar *NetArpWaitPacketMAC; /* MAC address of waiting packet's destination */
190 uchar *NetArpWaitTxPacket; /* THE transmit packet */
191 int NetArpWaitTxPacketSize;
192 uchar NetArpWaitPacketBuf[PKTSIZE_ALIGN + PKTALIGN];
193 ulong NetArpWaitTimerStart;
196 void ArpRequest (void)
203 printf ("ARP broadcast %d\n", NetArpWaitTry);
207 pkt += NetSetEther (pkt, NetBcastAddr, PROT_ARP);
211 arp->ar_hrd = htons (ARP_ETHER);
212 arp->ar_pro = htons (PROT_IP);
215 arp->ar_op = htons (ARPOP_REQUEST);
217 memcpy (&arp->ar_data[0], NetOurEther, 6); /* source ET addr */
218 NetWriteIP ((uchar *) & arp->ar_data[6], NetOurIP); /* source IP addr */
219 for (i = 10; i < 16; ++i) {
220 arp->ar_data[i] = 0; /* dest ET addr = 0 */
223 if ((NetArpWaitPacketIP & NetOurSubnetMask) !=
224 (NetOurIP & NetOurSubnetMask)) {
225 if (NetOurGatewayIP == 0) {
226 puts ("## Warning: gatewayip needed but not set\n");
227 NetArpWaitReplyIP = NetArpWaitPacketIP;
229 NetArpWaitReplyIP = NetOurGatewayIP;
232 NetArpWaitReplyIP = NetArpWaitPacketIP;
235 NetWriteIP ((uchar *) & arp->ar_data[16], NetArpWaitReplyIP);
236 (void) eth_send (NetTxPacket, (pkt - NetTxPacket) + ARP_HDR_SIZE);
239 void ArpTimeoutCheck(void)
243 if (!NetArpWaitPacketIP)
248 /* check for arp timeout */
249 if ((t - NetArpWaitTimerStart) > ARP_TIMEOUT * CFG_HZ) {
252 if (NetArpWaitTry >= ARP_TIMEOUT_COUNT) {
253 puts ("\nARP Retry count exceeded; starting again\n");
257 NetArpWaitTimerStart = t;
263 /**********************************************************************/
265 * Main network processing loop.
269 NetLoop(proto_t protocol)
273 #ifdef CONFIG_NET_MULTI
278 /* XXX problem with bss workaround */
279 NetArpWaitPacketMAC = NULL;
280 NetArpWaitTxPacket = NULL;
281 NetArpWaitPacketIP = 0;
282 NetArpWaitReplyIP = 0;
283 NetArpWaitTxPacket = NULL;
289 * Setup packet buffers, aligned correctly.
291 NetTxPacket = &PktBuf[0] + (PKTALIGN - 1);
292 NetTxPacket -= (ulong)NetTxPacket % PKTALIGN;
293 for (i = 0; i < PKTBUFSRX; i++) {
294 NetRxPackets[i] = NetTxPacket + (i+1)*PKTSIZE_ALIGN;
298 if (!NetArpWaitTxPacket) {
299 NetArpWaitTxPacket = &NetArpWaitPacketBuf[0] + (PKTALIGN - 1);
300 NetArpWaitTxPacket -= (ulong)NetArpWaitTxPacket % PKTALIGN;
301 NetArpWaitTxPacketSize = 0;
305 #ifdef CONFIG_NET_MULTI
308 if (eth_init(bd) < 0) {
314 #ifdef CONFIG_NET_MULTI
315 memcpy (NetOurEther, eth_get_dev()->enetaddr, 6);
317 memcpy (NetOurEther, bd->bi_enetaddr, 6);
320 NetState = NETLOOP_CONTINUE;
323 * Start the ball rolling with the given start function. From
324 * here on, this code is a state machine driven by received
325 * packets and timer events.
329 #if defined(CONFIG_CMD_NFS)
332 #if defined(CONFIG_CMD_PING)
335 #if defined(CONFIG_CMD_SNTP)
340 NetCopyIP(&NetOurIP, &bd->bi_ip_addr);
341 NetOurGatewayIP = getenv_IPaddr ("gatewayip");
342 NetOurSubnetMask= getenv_IPaddr ("netmask");
343 NetOurVLAN = getenv_VLAN("vlan");
344 NetOurNativeVLAN = getenv_VLAN("nvlan");
347 #if defined(CONFIG_CMD_NFS)
352 NetServerIP = getenv_IPaddr ("serverip");
354 #if defined(CONFIG_CMD_PING)
359 #if defined(CONFIG_CMD_SNTP)
372 * initialize our IP addr to 0 in order to accept ANY
373 * IP addr assigned to us by the BOOTP / RARP server
376 NetServerIP = getenv_IPaddr ("serverip");
377 NetOurVLAN = getenv_VLAN("vlan"); /* VLANs must be read */
378 NetOurNativeVLAN = getenv_VLAN("nvlan");
380 NetOurVLAN = getenv_VLAN("vlan"); /* VLANs must be read */
381 NetOurNativeVLAN = getenv_VLAN("nvlan");
387 switch (net_check_prereq (protocol)) {
389 /* network not configured */
393 #ifdef CONFIG_NET_MULTI
395 /* network device not configured */
397 #endif /* CONFIG_NET_MULTI */
400 #ifdef CONFIG_NET_MULTI
405 /* always use ARP to get server ethernet address */
409 #if defined(CONFIG_CMD_DHCP)
411 /* Start with a clean slate... */
414 NetServerIP = getenv_IPaddr ("serverip");
415 DhcpRequest(); /* Basically same as BOOTP */
428 #if defined(CONFIG_CMD_PING)
433 #if defined(CONFIG_CMD_NFS)
438 #if defined(CONFIG_CMD_CDP)
443 #ifdef CONFIG_NETCONSOLE
448 #if defined(CONFIG_CMD_SNTP)
457 NetBootFileXferSize = 0;
461 #if defined(CONFIG_MII) || defined(CONFIG_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(eth_get_dev()->name, 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, ... */
475 * Main packet reception loop. Loop receiving packets until
476 * someone sets `NetState' to a state that terminates.
480 #ifdef CONFIG_SHOW_ACTIVITY
482 extern void show_activity(int arg);
487 * Check the ethernet for a new packet. The ethernet
488 * receive routine will process it.
493 * Abort if ctrl-c was pressed.
504 * Check for a timeout, and run the timeout handler
507 if (timeHandler && ((get_timer(0) - timeStart) > timeDelta)) {
510 #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
511 # if defined(CFG_FAULT_ECHO_LINK_DOWN) && \
512 defined(CONFIG_STATUS_LED) && \
513 defined(STATUS_LED_RED)
515 * Echo the inverted link state to the fault LED.
517 if(miiphy_link(eth_get_dev()->name, CFG_FAULT_MII_ADDR)) {
518 status_led_set (STATUS_LED_RED, STATUS_LED_OFF);
520 status_led_set (STATUS_LED_RED, STATUS_LED_ON);
522 # endif /* CFG_FAULT_ECHO_LINK_DOWN, ... */
523 #endif /* CONFIG_MII, ... */
525 timeHandler = (thand_f *)0;
532 case NETLOOP_RESTART:
533 #ifdef CONFIG_NET_MULTI
538 case NETLOOP_SUCCESS:
539 if (NetBootFileXferSize > 0) {
541 printf("Bytes transferred = %ld (%lx hex)\n",
543 NetBootFileXferSize);
544 sprintf(buf, "%lx", NetBootFileXferSize);
545 setenv("filesize", buf);
547 sprintf(buf, "%lX", (unsigned long)load_addr);
548 setenv("fileaddr", buf);
551 return NetBootFileXferSize;
559 /**********************************************************************/
562 startAgainTimeout(void)
564 NetState = NETLOOP_RESTART;
568 startAgainHandler(uchar * pkt, unsigned dest, unsigned src, unsigned len)
570 /* Totally ignore the packet */
573 void NetStartAgain (void)
576 int noretry = 0, once = 0;
578 if ((nretry = getenv ("netretry")) != NULL) {
579 noretry = (strcmp (nretry, "no") == 0);
580 once = (strcmp (nretry, "once") == 0);
584 NetState = NETLOOP_FAIL;
587 #ifndef CONFIG_NET_MULTI
588 NetSetTimeout (10 * CFG_HZ, startAgainTimeout);
589 NetSetHandler (startAgainHandler);
590 #else /* !CONFIG_NET_MULTI*/
592 eth_try_another (!NetRestarted);
594 if (NetRestartWrap) {
596 if (NetDevExists && !once) {
597 NetSetTimeout (10 * CFG_HZ, startAgainTimeout);
598 NetSetHandler (startAgainHandler);
600 NetState = NETLOOP_FAIL;
603 NetState = NETLOOP_RESTART;
605 #endif /* CONFIG_NET_MULTI */
608 /**********************************************************************/
614 NetSetHandler(rxhand_f * f)
621 NetSetTimeout(ulong iv, thand_f * f)
624 timeHandler = (thand_f *)0;
627 timeStart = get_timer(0);
634 NetSendPacket(volatile uchar * pkt, int len)
636 (void) eth_send(pkt, len);
640 NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport, int sport, int len)
644 /* convert to new style broadcast */
648 /* if broadcast, make the ether address a broadcast and don't do ARP */
649 if (dest == 0xFFFFFFFF)
650 ether = NetBcastAddr;
652 /* if MAC address was not discovered yet, save the packet and do an ARP request */
653 if (memcmp(ether, NetEtherNullAddr, 6) == 0) {
656 printf("sending ARP for %08lx\n", dest);
658 NetArpWaitPacketIP = dest;
659 NetArpWaitPacketMAC = ether;
661 pkt = NetArpWaitTxPacket;
662 pkt += NetSetEther (pkt, NetArpWaitPacketMAC, PROT_IP);
664 NetSetIP (pkt, dest, dport, sport, len);
665 memcpy(pkt + IP_HDR_SIZE, (uchar *)NetTxPacket + (pkt - (uchar *)NetArpWaitTxPacket) + IP_HDR_SIZE, len);
667 /* size of the waiting packet */
668 NetArpWaitTxPacketSize = (pkt - NetArpWaitTxPacket) + IP_HDR_SIZE + len;
670 /* and do the ARP request */
672 NetArpWaitTimerStart = get_timer(0);
674 return 1; /* waiting */
678 printf("sending UDP to %08lx/%02x:%02x:%02x:%02x:%02x:%02x\n",
679 dest, ether[0], ether[1], ether[2], ether[3], ether[4], ether[5]);
682 pkt = (uchar *)NetTxPacket;
683 pkt += NetSetEther (pkt, ether, PROT_IP);
684 NetSetIP (pkt, dest, dport, sport, len);
685 (void) eth_send(NetTxPacket, (pkt - NetTxPacket) + IP_HDR_SIZE + len);
687 return 0; /* transmitted */
690 #if defined(CONFIG_CMD_PING)
691 static ushort PingSeqNo;
700 /* XXX always send arp request */
702 memcpy(mac, NetEtherNullAddr, 6);
705 printf("sending ARP for %08lx\n", NetPingIP);
708 NetArpWaitPacketIP = NetPingIP;
709 NetArpWaitPacketMAC = mac;
711 pkt = NetArpWaitTxPacket;
712 pkt += NetSetEther(pkt, mac, PROT_IP);
714 ip = (volatile IP_t *)pkt;
717 * Construct an IP and ICMP header. (need to set no fragment bit - XXX)
719 ip->ip_hl_v = 0x45; /* IP_HDR_SIZE / 4 (not including UDP) */
721 ip->ip_len = htons(IP_HDR_SIZE_NO_UDP + 8);
722 ip->ip_id = htons(NetIPID++);
723 ip->ip_off = htons(0x4000); /* No fragmentation */
725 ip->ip_p = 0x01; /* ICMP */
727 NetCopyIP((void*)&ip->ip_src, &NetOurIP); /* already in network byte order */
728 NetCopyIP((void*)&ip->ip_dst, &NetPingIP); /* - "" - */
729 ip->ip_sum = ~NetCksum((uchar *)ip, IP_HDR_SIZE_NO_UDP / 2);
731 s = &ip->udp_src; /* XXX ICMP starts here */
732 s[0] = htons(0x0800); /* echo-request, code */
733 s[1] = 0; /* checksum */
734 s[2] = 0; /* identifier */
735 s[3] = htons(PingSeqNo++); /* sequence number */
736 s[1] = ~NetCksum((uchar *)s, 8/2);
738 /* size of the waiting packet */
739 NetArpWaitTxPacketSize = (pkt - NetArpWaitTxPacket) + IP_HDR_SIZE_NO_UDP + 8;
741 /* and do the ARP request */
743 NetArpWaitTimerStart = get_timer(0);
745 return 1; /* waiting */
752 NetState = NETLOOP_FAIL; /* we did not get the reply */
756 PingHandler (uchar * pkt, unsigned dest, unsigned src, unsigned len)
759 volatile IP_t *ip = (volatile IP_t *)pkt;
761 tmp = NetReadIP((void *)&ip->ip_src);
762 if (tmp != NetPingIP)
765 NetState = NETLOOP_SUCCESS;
768 static void PingStart(void)
770 #if defined(CONFIG_NET_MULTI)
771 printf ("Using %s device\n", eth_get_name());
772 #endif /* CONFIG_NET_MULTI */
773 NetSetTimeout (10 * CFG_HZ, PingTimeout);
774 NetSetHandler (PingHandler);
780 #if defined(CONFIG_CMD_CDP)
782 #define CDP_DEVICE_ID_TLV 0x0001
783 #define CDP_ADDRESS_TLV 0x0002
784 #define CDP_PORT_ID_TLV 0x0003
785 #define CDP_CAPABILITIES_TLV 0x0004
786 #define CDP_VERSION_TLV 0x0005
787 #define CDP_PLATFORM_TLV 0x0006
788 #define CDP_NATIVE_VLAN_TLV 0x000a
789 #define CDP_APPLIANCE_VLAN_TLV 0x000e
790 #define CDP_TRIGGER_TLV 0x000f
791 #define CDP_POWER_CONSUMPTION_TLV 0x0010
792 #define CDP_SYSNAME_TLV 0x0014
793 #define CDP_SYSOBJECT_TLV 0x0015
794 #define CDP_MANAGEMENT_ADDRESS_TLV 0x0016
796 #define CDP_TIMEOUT (CFG_HZ/4) /* one packet every 250ms */
801 ushort CDPNativeVLAN;
802 ushort CDPApplianceVLAN;
804 static const uchar CDP_SNAP_hdr[8] = { 0xAA, 0xAA, 0x03, 0x00, 0x00, 0x0C, 0x20, 0x00 };
806 static ushort CDP_compute_csum(const uchar *buff, ushort len)
815 odd = 1 & (ulong)buff;
825 if (result & 0x80000000)
826 result = (result & 0xFFFF) + (result >> 16);
830 leftover = (signed short)(*(const signed char *)buff);
831 /* CISCO SUCKS big time! (and blows too):
832 * CDP uses the IP checksum algorithm with a twist;
833 * for the last byte it *sign* extends and sums.
835 result = (result & 0xffff0000) | ((result + leftover) & 0x0000ffff);
838 result = (result & 0xFFFF) + (result >> 16);
841 result = ((result >> 8) & 0xff) | ((result & 0xff) << 8);
844 /* add up 16-bit and 17-bit words for 17+c bits */
845 result = (result & 0xffff) + (result >> 16);
846 /* add up 16-bit and 2-bit for 16+c bit */
847 result = (result & 0xffff) + (result >> 16);
849 result = (result & 0xffff) + (result >> 16);
852 csum = ~(ushort)result;
854 /* run time endian detection */
855 if (csum != htons(csum)) /* little endian */
861 int CDPSendTrigger(void)
869 #if defined(CONFIG_CDP_DEVICE_ID) || defined(CONFIG_CDP_PORT_ID) || \
870 defined(CONFIG_CDP_VERSION) || defined(CONFIG_CDP_PLATFORM)
875 et = (Ethernet_t *)pkt;
877 /* NOTE: trigger sent not on any VLAN */
879 /* form ethernet header */
880 memcpy(et->et_dest, NetCDPAddr, 6);
881 memcpy(et->et_src, NetOurEther, 6);
883 pkt += ETHER_HDR_SIZE;
886 memcpy((uchar *)pkt, CDP_SNAP_hdr, sizeof(CDP_SNAP_hdr));
887 pkt += sizeof(CDP_SNAP_hdr);
890 *pkt++ = 0x02; /* CDP version 2 */
891 *pkt++ = 180; /* TTL */
892 s = (volatile ushort *)pkt;
894 *s++ = htons(0); /* checksum (0 for later calculation) */
897 #ifdef CONFIG_CDP_DEVICE_ID
898 *s++ = htons(CDP_DEVICE_ID_TLV);
899 *s++ = htons(CONFIG_CDP_DEVICE_ID);
900 memset(buf, 0, sizeof(buf));
901 sprintf(buf, CONFIG_CDP_DEVICE_ID_PREFIX "%02X%02X%02X%02X%02X%02X",
902 NetOurEther[0] & 0xff, NetOurEther[1] & 0xff,
903 NetOurEther[2] & 0xff, NetOurEther[3] & 0xff,
904 NetOurEther[4] & 0xff, NetOurEther[5] & 0xff);
905 memcpy((uchar *)s, buf, 16);
909 #ifdef CONFIG_CDP_PORT_ID
910 *s++ = htons(CDP_PORT_ID_TLV);
911 memset(buf, 0, sizeof(buf));
912 sprintf(buf, CONFIG_CDP_PORT_ID, eth_get_dev_index());
914 if (len & 1) /* make it even */
916 *s++ = htons(len + 4);
917 memcpy((uchar *)s, buf, len);
921 #ifdef CONFIG_CDP_CAPABILITIES
922 *s++ = htons(CDP_CAPABILITIES_TLV);
924 *(ulong *)s = htonl(CONFIG_CDP_CAPABILITIES);
928 #ifdef CONFIG_CDP_VERSION
929 *s++ = htons(CDP_VERSION_TLV);
930 memset(buf, 0, sizeof(buf));
931 strcpy(buf, CONFIG_CDP_VERSION);
933 if (len & 1) /* make it even */
935 *s++ = htons(len + 4);
936 memcpy((uchar *)s, buf, len);
940 #ifdef CONFIG_CDP_PLATFORM
941 *s++ = htons(CDP_PLATFORM_TLV);
942 memset(buf, 0, sizeof(buf));
943 strcpy(buf, CONFIG_CDP_PLATFORM);
945 if (len & 1) /* make it even */
947 *s++ = htons(len + 4);
948 memcpy((uchar *)s, buf, len);
952 #ifdef CONFIG_CDP_TRIGGER
953 *s++ = htons(CDP_TRIGGER_TLV);
955 *(ulong *)s = htonl(CONFIG_CDP_TRIGGER);
959 #ifdef CONFIG_CDP_POWER_CONSUMPTION
960 *s++ = htons(CDP_POWER_CONSUMPTION_TLV);
962 *s++ = htons(CONFIG_CDP_POWER_CONSUMPTION);
965 /* length of ethernet packet */
966 len = (uchar *)s - ((uchar *)NetTxPacket + ETHER_HDR_SIZE);
967 et->et_protlen = htons(len);
969 len = ETHER_HDR_SIZE + sizeof(CDP_SNAP_hdr);
970 chksum = CDP_compute_csum((uchar *)NetTxPacket + len, (uchar *)s - (NetTxPacket + len));
975 (void) eth_send(NetTxPacket, (uchar *)s - NetTxPacket);
985 NetSetTimeout (CDP_TIMEOUT, CDPTimeout);
990 /* if not OK try again */
994 NetState = NETLOOP_SUCCESS;
998 CDPDummyHandler (uchar * pkt, unsigned dest, unsigned src, unsigned len)
1004 CDPHandler(const uchar * pkt, unsigned len)
1013 if (len < sizeof(CDP_SNAP_hdr) + 4)
1016 /* check for valid CDP SNAP header */
1017 if (memcmp(pkt, CDP_SNAP_hdr, sizeof(CDP_SNAP_hdr)) != 0)
1020 pkt += sizeof(CDP_SNAP_hdr);
1021 len -= sizeof(CDP_SNAP_hdr);
1023 /* Version of CDP protocol must be >= 2 and TTL != 0 */
1024 if (pkt[0] < 0x02 || pkt[1] == 0)
1027 /* if version is greater than 0x02 maybe we'll have a problem; output a warning */
1029 printf("** WARNING: CDP packet received with a protocol version %d > 2\n",
1032 if (CDP_compute_csum(pkt, len) != 0)
1044 ss = (const ushort *)pkt;
1045 type = ntohs(ss[0]);
1046 tlen = ntohs(ss[1]);
1054 ss += 2; /* point ss to the data of the TLV */
1058 case CDP_DEVICE_ID_TLV:
1060 case CDP_ADDRESS_TLV:
1062 case CDP_PORT_ID_TLV:
1064 case CDP_CAPABILITIES_TLV:
1066 case CDP_VERSION_TLV:
1068 case CDP_PLATFORM_TLV:
1070 case CDP_NATIVE_VLAN_TLV:
1073 case CDP_APPLIANCE_VLAN_TLV:
1074 t = (const uchar *)ss;
1080 ss = (const ushort *)(t + 1);
1082 #ifdef CONFIG_CDP_APPLIANCE_VLAN_TYPE
1083 if (applid == CONFIG_CDP_APPLIANCE_VLAN_TYPE)
1086 vlan = ntohs(*ss); /* XXX will this work; dunno */
1091 case CDP_TRIGGER_TLV:
1093 case CDP_POWER_CONSUMPTION_TLV:
1095 case CDP_SYSNAME_TLV:
1097 case CDP_SYSOBJECT_TLV:
1099 case CDP_MANAGEMENT_ADDRESS_TLV:
1104 CDPApplianceVLAN = vlan;
1105 CDPNativeVLAN = nvlan;
1111 printf("** CDP packet is too short\n");
1115 static void CDPStart(void)
1117 #if defined(CONFIG_NET_MULTI)
1118 printf ("Using %s device\n", eth_get_name());
1123 CDPNativeVLAN = htons(-1);
1124 CDPApplianceVLAN = htons(-1);
1126 NetSetTimeout (CDP_TIMEOUT, CDPTimeout);
1127 NetSetHandler (CDPDummyHandler);
1135 NetReceive(volatile uchar * inpkt, int len)
1143 #if defined(CONFIG_CMD_CDP)
1146 ushort cti = 0, vlanid = VLAN_NONE, myvlanid, mynvlanid;
1149 printf("packet received\n");
1154 et = (Ethernet_t *)inpkt;
1156 /* too small packet? */
1157 if (len < ETHER_HDR_SIZE)
1160 #if defined(CONFIG_CMD_CDP)
1161 /* keep track if packet is CDP */
1162 iscdp = memcmp(et->et_dest, NetCDPAddr, 6) == 0;
1165 myvlanid = ntohs(NetOurVLAN);
1166 if (myvlanid == (ushort)-1)
1167 myvlanid = VLAN_NONE;
1168 mynvlanid = ntohs(NetOurNativeVLAN);
1169 if (mynvlanid == (ushort)-1)
1170 mynvlanid = VLAN_NONE;
1172 x = ntohs(et->et_protlen);
1175 printf("packet received\n");
1180 * Got a 802 packet. Check the other protocol field.
1182 x = ntohs(et->et_prot);
1184 ip = (IP_t *)(inpkt + E802_HDR_SIZE);
1185 len -= E802_HDR_SIZE;
1187 } else if (x != PROT_VLAN) { /* normal packet */
1188 ip = (IP_t *)(inpkt + ETHER_HDR_SIZE);
1189 len -= ETHER_HDR_SIZE;
1191 } else { /* VLAN packet */
1192 VLAN_Ethernet_t *vet = (VLAN_Ethernet_t *)et;
1195 printf("VLAN packet received\n");
1197 /* too small packet? */
1198 if (len < VLAN_ETHER_HDR_SIZE)
1201 /* if no VLAN active */
1202 if ((ntohs(NetOurVLAN) & VLAN_IDMASK) == VLAN_NONE
1203 #if defined(CONFIG_CMD_CDP)
1209 cti = ntohs(vet->vet_tag);
1210 vlanid = cti & VLAN_IDMASK;
1211 x = ntohs(vet->vet_type);
1213 ip = (IP_t *)(inpkt + VLAN_ETHER_HDR_SIZE);
1214 len -= VLAN_ETHER_HDR_SIZE;
1218 printf("Receive from protocol 0x%x\n", x);
1221 #if defined(CONFIG_CMD_CDP)
1223 CDPHandler((uchar *)ip, len);
1228 if ((myvlanid & VLAN_IDMASK) != VLAN_NONE) {
1229 if (vlanid == VLAN_NONE)
1230 vlanid = (mynvlanid & VLAN_IDMASK);
1232 if (vlanid != (myvlanid & VLAN_IDMASK))
1240 * We have to deal with two types of ARP packets:
1241 * - REQUEST packets will be answered by sending our
1242 * IP address - if we know it.
1243 * - REPLY packates are expected only after we asked
1244 * for the TFTP server's or the gateway's ethernet
1245 * address; so if we receive such a packet, we set
1246 * the server ethernet address
1252 if (len < ARP_HDR_SIZE) {
1253 printf("bad length %d < %d\n", len, ARP_HDR_SIZE);
1256 if (ntohs(arp->ar_hrd) != ARP_ETHER) {
1259 if (ntohs(arp->ar_pro) != PROT_IP) {
1262 if (arp->ar_hln != 6) {
1265 if (arp->ar_pln != 4) {
1269 if (NetOurIP == 0) {
1273 if (NetReadIP(&arp->ar_data[16]) != NetOurIP) {
1277 switch (ntohs(arp->ar_op)) {
1278 case ARPOP_REQUEST: /* reply with our IP address */
1280 puts ("Got ARP REQUEST, return our IP\n");
1283 pkt += NetSetEther(pkt, et->et_src, PROT_ARP);
1284 arp->ar_op = htons(ARPOP_REPLY);
1285 memcpy (&arp->ar_data[10], &arp->ar_data[0], 6);
1286 NetCopyIP(&arp->ar_data[16], &arp->ar_data[6]);
1287 memcpy (&arp->ar_data[ 0], NetOurEther, 6);
1288 NetCopyIP(&arp->ar_data[ 6], &NetOurIP);
1289 (void) eth_send((uchar *)et, (pkt - (uchar *)et) + ARP_HDR_SIZE);
1292 case ARPOP_REPLY: /* arp reply */
1293 /* are we waiting for a reply */
1294 if (!NetArpWaitPacketIP || !NetArpWaitPacketMAC)
1297 printf("Got ARP REPLY, set server/gtwy eth addr (%02x:%02x:%02x:%02x:%02x:%02x)\n",
1298 arp->ar_data[0], arp->ar_data[1],
1299 arp->ar_data[2], arp->ar_data[3],
1300 arp->ar_data[4], arp->ar_data[5]);
1303 tmp = NetReadIP(&arp->ar_data[6]);
1305 /* matched waiting packet's address */
1306 if (tmp == NetArpWaitReplyIP) {
1310 /* save address for later use */
1311 memcpy(NetArpWaitPacketMAC, &arp->ar_data[0], 6);
1313 #ifdef CONFIG_NETCONSOLE
1314 (*packetHandler)(0,0,0,0);
1316 /* modify header, and transmit it */
1317 memcpy(((Ethernet_t *)NetArpWaitTxPacket)->et_dest, NetArpWaitPacketMAC, 6);
1318 (void) eth_send(NetArpWaitTxPacket, NetArpWaitTxPacketSize);
1320 /* no arp request pending now */
1321 NetArpWaitPacketIP = 0;
1322 NetArpWaitTxPacketSize = 0;
1323 NetArpWaitPacketMAC = NULL;
1329 printf("Unexpected ARP opcode 0x%x\n", ntohs(arp->ar_op));
1337 puts ("Got RARP\n");
1340 if (len < ARP_HDR_SIZE) {
1341 printf("bad length %d < %d\n", len, ARP_HDR_SIZE);
1345 if ((ntohs(arp->ar_op) != RARPOP_REPLY) ||
1346 (ntohs(arp->ar_hrd) != ARP_ETHER) ||
1347 (ntohs(arp->ar_pro) != PROT_IP) ||
1348 (arp->ar_hln != 6) || (arp->ar_pln != 4)) {
1350 puts ("invalid RARP header\n");
1352 NetCopyIP(&NetOurIP, &arp->ar_data[16]);
1353 if (NetServerIP == 0)
1354 NetCopyIP(&NetServerIP, &arp->ar_data[ 6]);
1355 memcpy (NetServerEther, &arp->ar_data[ 0], 6);
1357 (*packetHandler)(0,0,0,0);
1365 if (len < IP_HDR_SIZE) {
1366 debug ("len bad %d < %d\n", len, IP_HDR_SIZE);
1369 if (len < ntohs(ip->ip_len)) {
1370 printf("len bad %d < %d\n", len, ntohs(ip->ip_len));
1373 len = ntohs(ip->ip_len);
1375 printf("len=%d, v=%02x\n", len, ip->ip_hl_v & 0xff);
1377 if ((ip->ip_hl_v & 0xf0) != 0x40) {
1380 if (ip->ip_off & htons(0x1fff)) { /* Can't deal w/ fragments */
1383 if (!NetCksumOk((uchar *)ip, IP_HDR_SIZE_NO_UDP / 2)) {
1384 puts ("checksum bad\n");
1387 tmp = NetReadIP(&ip->ip_dst);
1388 if (NetOurIP && tmp != NetOurIP && tmp != 0xFFFFFFFF) {
1392 * watch for ICMP host redirects
1394 * There is no real handler code (yet). We just watch
1395 * for ICMP host redirect messages. In case anybody
1396 * sees these messages: please contact me
1397 * (wd@denx.de), or - even better - send me the
1398 * necessary fixes :-)
1400 * Note: in all cases where I have seen this so far
1401 * it was a problem with the router configuration,
1402 * for instance when a router was configured in the
1403 * BOOTP reply, but the TFTP server was on the same
1404 * subnet. So this is probably a warning that your
1405 * configuration might be wrong. But I'm not really
1406 * sure if there aren't any other situations.
1408 if (ip->ip_p == IPPROTO_ICMP) {
1409 ICMP_t *icmph = (ICMP_t *)&(ip->udp_src);
1411 switch (icmph->type) {
1413 if (icmph->code != ICMP_REDIR_HOST)
1415 puts (" ICMP Host Redirect to ");
1416 print_IPaddr(icmph->un.gateway);
1419 #if defined(CONFIG_CMD_PING)
1420 case ICMP_ECHO_REPLY:
1422 * IP header OK. Pass the packet to the current handler.
1424 /* XXX point to ip packet */
1425 (*packetHandler)((uchar *)ip, 0, 0, 0);
1427 case ICMP_ECHO_REQUEST:
1429 printf ("Got ICMP ECHO REQUEST, return %d bytes \n",
1430 ETHER_HDR_SIZE + len);
1432 memcpy (&et->et_dest[0], &et->et_src[0], 6);
1433 memcpy (&et->et_src[ 0], NetOurEther, 6);
1437 NetCopyIP((void*)&ip->ip_dst, &ip->ip_src);
1438 NetCopyIP((void*)&ip->ip_src, &NetOurIP);
1439 ip->ip_sum = ~NetCksum((uchar *)ip, IP_HDR_SIZE_NO_UDP >> 1);
1441 icmph->type = ICMP_ECHO_REPLY;
1442 icmph->checksum = 0;
1443 icmph->checksum = ~NetCksum((uchar *)icmph,
1444 (len - IP_HDR_SIZE_NO_UDP) >> 1);
1445 (void) eth_send((uchar *)et, ETHER_HDR_SIZE + len);
1451 } else if (ip->ip_p != IPPROTO_UDP) { /* Only UDP packets */
1455 #ifdef CONFIG_UDP_CHECKSUM
1456 if (ip->udp_xsum != 0) {
1462 xsum += (ntohs(ip->udp_len));
1463 xsum += (ntohl(ip->ip_src) >> 16) & 0x0000ffff;
1464 xsum += (ntohl(ip->ip_src) >> 0) & 0x0000ffff;
1465 xsum += (ntohl(ip->ip_dst) >> 16) & 0x0000ffff;
1466 xsum += (ntohl(ip->ip_dst) >> 0) & 0x0000ffff;
1468 sumlen = ntohs(ip->udp_len);
1469 sumptr = (ushort *) &(ip->udp_src);
1471 while (sumlen > 1) {
1474 sumdata = *sumptr++;
1475 xsum += ntohs(sumdata);
1481 sumdata = *(unsigned char *) sumptr;
1482 sumdata = (sumdata << 8) & 0xff00;
1485 while ((xsum >> 16) != 0) {
1486 xsum = (xsum & 0x0000ffff) + ((xsum >> 16) & 0x0000ffff);
1488 if ((xsum != 0x00000000) && (xsum != 0x0000ffff)) {
1489 printf(" UDP wrong checksum %08x %08x\n", xsum, ntohs(ip->udp_xsum));
1495 #ifdef CONFIG_NETCONSOLE
1496 nc_input_packet((uchar *)ip +IP_HDR_SIZE,
1499 ntohs(ip->udp_len) - 8);
1502 * IP header OK. Pass the packet to the current handler.
1504 (*packetHandler)((uchar *)ip +IP_HDR_SIZE,
1507 ntohs(ip->udp_len) - 8);
1513 /**********************************************************************/
1515 static int net_check_prereq (proto_t protocol)
1519 #if defined(CONFIG_CMD_PING)
1521 if (NetPingIP == 0) {
1522 puts ("*** ERROR: ping address not given\n");
1527 #if defined(CONFIG_CMD_SNTP)
1529 if (NetNtpServerIP == 0) {
1530 puts ("*** ERROR: NTP server address not given\n");
1535 #if defined(CONFIG_CMD_NFS)
1540 if (NetServerIP == 0) {
1541 puts ("*** ERROR: `serverip' not set\n");
1544 #if defined(CONFIG_CMD_PING) || defined(CONFIG_CMD_SNTP)
1548 if (NetOurIP == 0) {
1549 puts ("*** ERROR: `ipaddr' not set\n");
1558 if (memcmp (NetOurEther, "\0\0\0\0\0\0", 6) == 0) {
1559 #ifdef CONFIG_NET_MULTI
1560 extern int eth_get_dev_index (void);
1561 int num = eth_get_dev_index ();
1565 puts ("*** ERROR: No ethernet found.\n");
1568 puts ("*** ERROR: `ethaddr' not set\n");
1571 printf ("*** ERROR: `eth%daddr' not set\n",
1579 puts ("*** ERROR: `ethaddr' not set\n");
1587 return (0); /* OK */
1589 /**********************************************************************/
1592 NetCksumOk(uchar * ptr, int len)
1594 return !((NetCksum(ptr, len) + 1) & 0xfffe);
1599 NetCksum(uchar * ptr, int len)
1602 ushort *p = (ushort *)ptr;
1607 xsum = (xsum & 0xffff) + (xsum >> 16);
1608 xsum = (xsum & 0xffff) + (xsum >> 16);
1609 return (xsum & 0xffff);
1617 myvlanid = ntohs(NetOurVLAN);
1618 if (myvlanid == (ushort)-1)
1619 myvlanid = VLAN_NONE;
1621 return ((myvlanid & VLAN_IDMASK) == VLAN_NONE) ? ETHER_HDR_SIZE : VLAN_ETHER_HDR_SIZE;
1625 NetSetEther(volatile uchar * xet, uchar * addr, uint prot)
1627 Ethernet_t *et = (Ethernet_t *)xet;
1630 myvlanid = ntohs(NetOurVLAN);
1631 if (myvlanid == (ushort)-1)
1632 myvlanid = VLAN_NONE;
1634 memcpy (et->et_dest, addr, 6);
1635 memcpy (et->et_src, NetOurEther, 6);
1636 if ((myvlanid & VLAN_IDMASK) == VLAN_NONE) {
1637 et->et_protlen = htons(prot);
1638 return ETHER_HDR_SIZE;
1640 VLAN_Ethernet_t *vet = (VLAN_Ethernet_t *)xet;
1642 vet->vet_vlan_type = htons(PROT_VLAN);
1643 vet->vet_tag = htons((0 << 5) | (myvlanid & VLAN_IDMASK));
1644 vet->vet_type = htons(prot);
1645 return VLAN_ETHER_HDR_SIZE;
1650 NetSetIP(volatile uchar * xip, IPaddr_t dest, int dport, int sport, int len)
1652 volatile IP_t *ip = (IP_t *)xip;
1655 * If the data is an odd number of bytes, zero the
1656 * byte after the last byte so that the checksum
1660 xip[IP_HDR_SIZE + len] = 0;
1663 * Construct an IP and UDP header.
1664 * (need to set no fragment bit - XXX)
1666 ip->ip_hl_v = 0x45; /* IP_HDR_SIZE / 4 (not including UDP) */
1668 ip->ip_len = htons(IP_HDR_SIZE + len);
1669 ip->ip_id = htons(NetIPID++);
1670 ip->ip_off = htons(0x4000); /* No fragmentation */
1672 ip->ip_p = 17; /* UDP */
1674 NetCopyIP((void*)&ip->ip_src, &NetOurIP); /* already in network byte order */
1675 NetCopyIP((void*)&ip->ip_dst, &dest); /* - "" - */
1676 ip->udp_src = htons(sport);
1677 ip->udp_dst = htons(dport);
1678 ip->udp_len = htons(8 + len);
1680 ip->ip_sum = ~NetCksum((uchar *)ip, IP_HDR_SIZE_NO_UDP / 2);
1683 void copy_filename (char *dst, char *src, int size)
1685 if (*src && (*src == '"')) {
1690 while ((--size > 0) && *src && (*src != '"')) {
1698 void ip_to_string (IPaddr_t x, char *s)
1701 sprintf (s, "%d.%d.%d.%d",
1702 (int) ((x >> 24) & 0xff),
1703 (int) ((x >> 16) & 0xff),
1704 (int) ((x >> 8) & 0xff), (int) ((x >> 0) & 0xff)
1708 IPaddr_t string_to_ip(char *s)
1717 for (addr=0, i=0; i<4; ++i) {
1718 ulong val = s ? simple_strtoul(s, &e, 10) : 0;
1720 addr |= (val & 0xFF);
1726 return (htonl(addr));
1729 void VLAN_to_string(ushort x, char *s)
1733 if (x == (ushort)-1)
1739 sprintf(s, "%d", x & VLAN_IDMASK);
1742 ushort string_to_VLAN(char *s)
1747 return htons(VLAN_NONE);
1749 if (*s < '0' || *s > '9')
1752 id = (ushort)simple_strtoul(s, NULL, 10);
1757 void print_IPaddr (IPaddr_t x)
1761 ip_to_string (x, tmp);
1766 IPaddr_t getenv_IPaddr (char *var)
1768 return (string_to_ip(getenv(var)));
1771 ushort getenv_VLAN(char *var)
1773 return (string_to_VLAN(getenv(var)));