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>
95 #if defined(CONFIG_CMD_DNS)
99 #if defined(CONFIG_CMD_NET)
101 DECLARE_GLOBAL_DATA_PTR;
103 #ifndef CONFIG_ARP_TIMEOUT
104 # define ARP_TIMEOUT 5000UL /* Milliseconds before trying ARP again */
106 # define ARP_TIMEOUT CONFIG_ARP_TIMEOUT
110 #ifndef CONFIG_NET_RETRY_COUNT
111 # define ARP_TIMEOUT_COUNT 5 /* # of timeouts before giving up */
113 # define ARP_TIMEOUT_COUNT CONFIG_NET_RETRY_COUNT
120 /** BOOTP EXTENTIONS **/
122 IPaddr_t NetOurSubnetMask=0; /* Our subnet mask (0=unknown) */
123 IPaddr_t NetOurGatewayIP=0; /* Our gateways IP address */
124 IPaddr_t NetOurDNSIP=0; /* Our DNS IP address */
125 #if defined(CONFIG_BOOTP_DNS2)
126 IPaddr_t NetOurDNS2IP=0; /* Our 2nd DNS IP address */
128 char NetOurNISDomain[32]={0,}; /* Our NIS domain */
129 char NetOurHostName[32]={0,}; /* Our hostname */
130 char NetOurRootPath[64]={0,}; /* Our bootpath */
131 ushort NetBootFileSize=0; /* Our bootfile size in blocks */
133 #ifdef CONFIG_MCAST_TFTP /* Multicast TFTP */
137 /** END OF BOOTP EXTENTIONS **/
139 ulong NetBootFileXferSize; /* The actual transferred size of the bootfile (in bytes) */
140 uchar NetOurEther[6]; /* Our ethernet address */
141 uchar NetServerEther[6] = /* Boot server enet address */
142 { 0, 0, 0, 0, 0, 0 };
143 IPaddr_t NetOurIP; /* Our IP addr (0 = unknown) */
144 IPaddr_t NetServerIP; /* Server IP addr (0 = unknown) */
145 volatile uchar *NetRxPacket; /* Current receive packet */
146 int NetRxPacketLen; /* Current rx packet length */
147 unsigned NetIPID; /* IP packet ID */
148 uchar NetBcastAddr[6] = /* Ethernet bcast address */
149 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
150 uchar NetEtherNullAddr[6] =
151 { 0, 0, 0, 0, 0, 0 };
153 void (*push_packet)(volatile void *, int len) = 0;
155 #if defined(CONFIG_CMD_CDP)
156 uchar NetCDPAddr[6] = /* Ethernet bcast address */
157 { 0x01, 0x00, 0x0c, 0xcc, 0xcc, 0xcc };
159 int NetState; /* Network loop state */
160 #ifdef CONFIG_NET_MULTI
161 int NetRestartWrap = 0; /* Tried all network devices */
162 static int NetRestarted = 0; /* Network loop restarted */
163 static int NetDevExists = 0; /* At least one device configured */
166 /* XXX in both little & big endian machines 0xFFFF == ntohs(-1) */
167 ushort NetOurVLAN = 0xFFFF; /* default is without VLAN */
168 ushort NetOurNativeVLAN = 0xFFFF; /* ditto */
170 char BootFile[128]; /* Boot File name */
172 #if defined(CONFIG_CMD_PING)
173 IPaddr_t NetPingIP; /* the ip address to ping */
175 static void PingStart(void);
178 #if defined(CONFIG_CMD_CDP)
179 static void CDPStart(void);
182 #if defined(CONFIG_CMD_SNTP)
183 IPaddr_t NetNtpServerIP; /* NTP server IP address */
184 int NetTimeOffset=0; /* offset time from UTC */
187 #ifdef CONFIG_NETCONSOLE
189 int nc_input_packet(uchar *pkt, unsigned dest, unsigned src, unsigned len);
192 volatile uchar PktBuf[(PKTBUFSRX+1) * PKTSIZE_ALIGN + PKTALIGN];
194 volatile uchar *NetRxPackets[PKTBUFSRX]; /* Receive packets */
196 static rxhand_f *packetHandler; /* Current RX packet handler */
197 static thand_f *timeHandler; /* Current timeout handler */
198 static ulong timeStart; /* Time base value */
199 static ulong timeDelta; /* Current timeout value */
200 volatile uchar *NetTxPacket = 0; /* THE transmit packet */
202 static int net_check_prereq (proto_t protocol);
204 /**********************************************************************/
206 IPaddr_t NetArpWaitPacketIP;
207 IPaddr_t NetArpWaitReplyIP;
208 uchar *NetArpWaitPacketMAC; /* MAC address of waiting packet's destination */
209 uchar *NetArpWaitTxPacket; /* THE transmit packet */
210 int NetArpWaitTxPacketSize;
211 uchar NetArpWaitPacketBuf[PKTSIZE_ALIGN + PKTALIGN];
212 ulong NetArpWaitTimerStart;
215 void ArpRequest (void)
222 printf ("ARP broadcast %d\n", NetArpWaitTry);
226 pkt += NetSetEther (pkt, NetBcastAddr, PROT_ARP);
230 arp->ar_hrd = htons (ARP_ETHER);
231 arp->ar_pro = htons (PROT_IP);
234 arp->ar_op = htons (ARPOP_REQUEST);
236 memcpy (&arp->ar_data[0], NetOurEther, 6); /* source ET addr */
237 NetWriteIP ((uchar *) & arp->ar_data[6], NetOurIP); /* source IP addr */
238 for (i = 10; i < 16; ++i) {
239 arp->ar_data[i] = 0; /* dest ET addr = 0 */
242 if ((NetArpWaitPacketIP & NetOurSubnetMask) !=
243 (NetOurIP & NetOurSubnetMask)) {
244 if (NetOurGatewayIP == 0) {
245 puts ("## Warning: gatewayip needed but not set\n");
246 NetArpWaitReplyIP = NetArpWaitPacketIP;
248 NetArpWaitReplyIP = NetOurGatewayIP;
251 NetArpWaitReplyIP = NetArpWaitPacketIP;
254 NetWriteIP ((uchar *) & arp->ar_data[16], NetArpWaitReplyIP);
255 (void) eth_send (NetTxPacket, (pkt - NetTxPacket) + ARP_HDR_SIZE);
258 void ArpTimeoutCheck(void)
262 if (!NetArpWaitPacketIP)
267 /* check for arp timeout */
268 if ((t - NetArpWaitTimerStart) > ARP_TIMEOUT) {
271 if (NetArpWaitTry >= ARP_TIMEOUT_COUNT) {
272 puts ("\nARP Retry count exceeded; starting again\n");
276 NetArpWaitTimerStart = t;
283 NetInitLoop(proto_t protocol)
285 static int env_changed_id = 0;
287 int env_id = get_env_id ();
289 /* update only when the environment has changed */
290 if (env_changed_id != env_id) {
291 NetCopyIP(&NetOurIP, &bd->bi_ip_addr);
292 NetOurGatewayIP = getenv_IPaddr ("gatewayip");
293 NetOurSubnetMask= getenv_IPaddr ("netmask");
294 NetServerIP = getenv_IPaddr ("serverip");
295 NetOurNativeVLAN = getenv_VLAN("nvlan");
296 NetOurVLAN = getenv_VLAN("vlan");
297 #if defined(CONFIG_CMD_DNS)
298 NetOurDNSIP = getenv_IPaddr("dnsip");
300 env_changed_id = env_id;
306 /**********************************************************************/
308 * Main network processing loop.
312 NetLoop(proto_t protocol)
316 #ifdef CONFIG_NET_MULTI
321 /* XXX problem with bss workaround */
322 NetArpWaitPacketMAC = NULL;
323 NetArpWaitTxPacket = NULL;
324 NetArpWaitPacketIP = 0;
325 NetArpWaitReplyIP = 0;
326 NetArpWaitTxPacket = NULL;
332 * Setup packet buffers, aligned correctly.
334 NetTxPacket = &PktBuf[0] + (PKTALIGN - 1);
335 NetTxPacket -= (ulong)NetTxPacket % PKTALIGN;
336 for (i = 0; i < PKTBUFSRX; i++) {
337 NetRxPackets[i] = NetTxPacket + (i+1)*PKTSIZE_ALIGN;
341 if (!NetArpWaitTxPacket) {
342 NetArpWaitTxPacket = &NetArpWaitPacketBuf[0] + (PKTALIGN - 1);
343 NetArpWaitTxPacket -= (ulong)NetArpWaitTxPacket % PKTALIGN;
344 NetArpWaitTxPacketSize = 0;
348 #ifdef CONFIG_NET_MULTI
351 if (eth_init(bd) < 0) {
357 #ifdef CONFIG_NET_MULTI
358 memcpy (NetOurEther, eth_get_dev()->enetaddr, 6);
360 eth_getenv_enetaddr("ethaddr", NetOurEther);
363 NetState = NETLOOP_CONTINUE;
366 * Start the ball rolling with the given start function. From
367 * here on, this code is a state machine driven by received
368 * packets and timer events.
370 NetInitLoop(protocol);
372 switch (net_check_prereq (protocol)) {
374 /* network not configured */
378 #ifdef CONFIG_NET_MULTI
380 /* network device not configured */
382 #endif /* CONFIG_NET_MULTI */
385 #ifdef CONFIG_NET_MULTI
390 /* always use ARP to get server ethernet address */
394 #if defined(CONFIG_CMD_DHCP)
398 DhcpRequest(); /* Basically same as BOOTP */
413 #if defined(CONFIG_CMD_PING)
418 #if defined(CONFIG_CMD_NFS)
423 #if defined(CONFIG_CMD_CDP)
428 #ifdef CONFIG_NETCONSOLE
433 #if defined(CONFIG_CMD_SNTP)
438 #if defined(CONFIG_CMD_DNS)
447 NetBootFileXferSize = 0;
451 #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
452 #if defined(CONFIG_SYS_FAULT_ECHO_LINK_DOWN) && defined(CONFIG_STATUS_LED) && defined(STATUS_LED_RED)
454 * Echo the inverted link state to the fault LED.
456 if(miiphy_link(eth_get_dev()->name, CONFIG_SYS_FAULT_MII_ADDR)) {
457 status_led_set (STATUS_LED_RED, STATUS_LED_OFF);
459 status_led_set (STATUS_LED_RED, STATUS_LED_ON);
461 #endif /* CONFIG_SYS_FAULT_ECHO_LINK_DOWN, ... */
462 #endif /* CONFIG_MII, ... */
465 * Main packet reception loop. Loop receiving packets until
466 * someone sets `NetState' to a state that terminates.
470 #ifdef CONFIG_SHOW_ACTIVITY
472 extern void show_activity(int arg);
477 * Check the ethernet for a new packet. The ethernet
478 * receive routine will process it.
483 * Abort if ctrl-c was pressed.
494 * Check for a timeout, and run the timeout handler
497 if (timeHandler && ((get_timer(0) - timeStart) > timeDelta)) {
500 #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
501 # if defined(CONFIG_SYS_FAULT_ECHO_LINK_DOWN) && \
502 defined(CONFIG_STATUS_LED) && \
503 defined(STATUS_LED_RED)
505 * Echo the inverted link state to the fault LED.
507 if(miiphy_link(eth_get_dev()->name, CONFIG_SYS_FAULT_MII_ADDR)) {
508 status_led_set (STATUS_LED_RED, STATUS_LED_OFF);
510 status_led_set (STATUS_LED_RED, STATUS_LED_ON);
512 # endif /* CONFIG_SYS_FAULT_ECHO_LINK_DOWN, ... */
513 #endif /* CONFIG_MII, ... */
515 timeHandler = (thand_f *)0;
522 case NETLOOP_RESTART:
523 #ifdef CONFIG_NET_MULTI
528 case NETLOOP_SUCCESS:
529 if (NetBootFileXferSize > 0) {
531 printf("Bytes transferred = %ld (%lx hex)\n",
533 NetBootFileXferSize);
534 sprintf(buf, "%lX", NetBootFileXferSize);
535 setenv("filesize", buf);
537 sprintf(buf, "%lX", (unsigned long)load_addr);
538 setenv("fileaddr", buf);
541 return NetBootFileXferSize;
549 /**********************************************************************/
552 startAgainTimeout(void)
554 NetState = NETLOOP_RESTART;
558 startAgainHandler(uchar * pkt, unsigned dest, unsigned src, unsigned len)
560 /* Totally ignore the packet */
563 void NetStartAgain (void)
566 int noretry = 0, once = 0;
568 if ((nretry = getenv ("netretry")) != NULL) {
569 noretry = (strcmp (nretry, "no") == 0);
570 once = (strcmp (nretry, "once") == 0);
574 NetState = NETLOOP_FAIL;
577 #ifndef CONFIG_NET_MULTI
578 NetSetTimeout (10000UL, startAgainTimeout);
579 NetSetHandler (startAgainHandler);
580 #else /* !CONFIG_NET_MULTI*/
582 #if !defined(CONFIG_NET_DO_NOT_TRY_ANOTHER)
583 eth_try_another (!NetRestarted);
586 if (NetRestartWrap) {
588 if (NetDevExists && !once) {
589 NetSetTimeout (10000UL, startAgainTimeout);
590 NetSetHandler (startAgainHandler);
592 NetState = NETLOOP_FAIL;
595 NetState = NETLOOP_RESTART;
597 #endif /* CONFIG_NET_MULTI */
600 /**********************************************************************/
606 NetSetHandler(rxhand_f * f)
613 NetSetTimeout(ulong iv, thand_f * f)
616 timeHandler = (thand_f *)0;
619 timeStart = get_timer(0);
626 NetSendPacket(volatile uchar * pkt, int len)
628 (void) eth_send(pkt, len);
632 NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport, int sport, int len)
636 /* convert to new style broadcast */
640 /* if broadcast, make the ether address a broadcast and don't do ARP */
641 if (dest == 0xFFFFFFFF)
642 ether = NetBcastAddr;
644 /* if MAC address was not discovered yet, save the packet and do an ARP request */
645 if (memcmp(ether, NetEtherNullAddr, 6) == 0) {
648 printf("sending ARP for %08lx\n", dest);
650 NetArpWaitPacketIP = dest;
651 NetArpWaitPacketMAC = ether;
653 pkt = NetArpWaitTxPacket;
654 pkt += NetSetEther (pkt, NetArpWaitPacketMAC, PROT_IP);
656 NetSetIP (pkt, dest, dport, sport, len);
657 memcpy(pkt + IP_HDR_SIZE, (uchar *)NetTxPacket + (pkt - (uchar *)NetArpWaitTxPacket) + IP_HDR_SIZE, len);
659 /* size of the waiting packet */
660 NetArpWaitTxPacketSize = (pkt - NetArpWaitTxPacket) + IP_HDR_SIZE + len;
662 /* and do the ARP request */
664 NetArpWaitTimerStart = get_timer(0);
666 return 1; /* waiting */
670 printf("sending UDP to %08lx/%pM\n", dest, ether);
673 pkt = (uchar *)NetTxPacket;
674 pkt += NetSetEther (pkt, ether, PROT_IP);
675 NetSetIP (pkt, dest, dport, sport, len);
676 (void) eth_send(NetTxPacket, (pkt - NetTxPacket) + IP_HDR_SIZE + len);
678 return 0; /* transmitted */
681 #if defined(CONFIG_CMD_PING)
682 static ushort PingSeqNo;
691 /* XXX always send arp request */
693 memcpy(mac, NetEtherNullAddr, 6);
696 printf("sending ARP for %08lx\n", NetPingIP);
699 NetArpWaitPacketIP = NetPingIP;
700 NetArpWaitPacketMAC = mac;
702 pkt = NetArpWaitTxPacket;
703 pkt += NetSetEther(pkt, mac, PROT_IP);
705 ip = (volatile IP_t *)pkt;
708 * Construct an IP and ICMP header. (need to set no fragment bit - XXX)
710 ip->ip_hl_v = 0x45; /* IP_HDR_SIZE / 4 (not including UDP) */
712 ip->ip_len = htons(IP_HDR_SIZE_NO_UDP + 8);
713 ip->ip_id = htons(NetIPID++);
714 ip->ip_off = htons(IP_FLAGS_DFRAG); /* Don't fragment */
716 ip->ip_p = 0x01; /* ICMP */
718 NetCopyIP((void*)&ip->ip_src, &NetOurIP); /* already in network byte order */
719 NetCopyIP((void*)&ip->ip_dst, &NetPingIP); /* - "" - */
720 ip->ip_sum = ~NetCksum((uchar *)ip, IP_HDR_SIZE_NO_UDP / 2);
722 s = &ip->udp_src; /* XXX ICMP starts here */
723 s[0] = htons(0x0800); /* echo-request, code */
724 s[1] = 0; /* checksum */
725 s[2] = 0; /* identifier */
726 s[3] = htons(PingSeqNo++); /* sequence number */
727 s[1] = ~NetCksum((uchar *)s, 8/2);
729 /* size of the waiting packet */
730 NetArpWaitTxPacketSize = (pkt - NetArpWaitTxPacket) + IP_HDR_SIZE_NO_UDP + 8;
732 /* and do the ARP request */
734 NetArpWaitTimerStart = get_timer(0);
736 return 1; /* waiting */
743 NetState = NETLOOP_FAIL; /* we did not get the reply */
747 PingHandler (uchar * pkt, unsigned dest, unsigned src, unsigned len)
750 volatile IP_t *ip = (volatile IP_t *)pkt;
752 tmp = NetReadIP((void *)&ip->ip_src);
753 if (tmp != NetPingIP)
756 NetState = NETLOOP_SUCCESS;
759 static void PingStart(void)
761 #if defined(CONFIG_NET_MULTI)
762 printf ("Using %s device\n", eth_get_name());
763 #endif /* CONFIG_NET_MULTI */
764 NetSetTimeout (10000UL, PingTimeout);
765 NetSetHandler (PingHandler);
771 #if defined(CONFIG_CMD_CDP)
773 #define CDP_DEVICE_ID_TLV 0x0001
774 #define CDP_ADDRESS_TLV 0x0002
775 #define CDP_PORT_ID_TLV 0x0003
776 #define CDP_CAPABILITIES_TLV 0x0004
777 #define CDP_VERSION_TLV 0x0005
778 #define CDP_PLATFORM_TLV 0x0006
779 #define CDP_NATIVE_VLAN_TLV 0x000a
780 #define CDP_APPLIANCE_VLAN_TLV 0x000e
781 #define CDP_TRIGGER_TLV 0x000f
782 #define CDP_POWER_CONSUMPTION_TLV 0x0010
783 #define CDP_SYSNAME_TLV 0x0014
784 #define CDP_SYSOBJECT_TLV 0x0015
785 #define CDP_MANAGEMENT_ADDRESS_TLV 0x0016
787 #define CDP_TIMEOUT 250UL /* one packet every 250ms */
792 ushort CDPNativeVLAN;
793 ushort CDPApplianceVLAN;
795 static const uchar CDP_SNAP_hdr[8] = { 0xAA, 0xAA, 0x03, 0x00, 0x00, 0x0C, 0x20, 0x00 };
797 static ushort CDP_compute_csum(const uchar *buff, ushort len)
806 odd = 1 & (ulong)buff;
816 if (result & 0x80000000)
817 result = (result & 0xFFFF) + (result >> 16);
821 leftover = (signed short)(*(const signed char *)buff);
822 /* CISCO SUCKS big time! (and blows too):
823 * CDP uses the IP checksum algorithm with a twist;
824 * for the last byte it *sign* extends and sums.
826 result = (result & 0xffff0000) | ((result + leftover) & 0x0000ffff);
829 result = (result & 0xFFFF) + (result >> 16);
832 result = ((result >> 8) & 0xff) | ((result & 0xff) << 8);
835 /* add up 16-bit and 17-bit words for 17+c bits */
836 result = (result & 0xffff) + (result >> 16);
837 /* add up 16-bit and 2-bit for 16+c bit */
838 result = (result & 0xffff) + (result >> 16);
840 result = (result & 0xffff) + (result >> 16);
843 csum = ~(ushort)result;
845 /* run time endian detection */
846 if (csum != htons(csum)) /* little endian */
852 int CDPSendTrigger(void)
860 #if defined(CONFIG_CDP_DEVICE_ID) || defined(CONFIG_CDP_PORT_ID) || \
861 defined(CONFIG_CDP_VERSION) || defined(CONFIG_CDP_PLATFORM)
866 et = (Ethernet_t *)pkt;
868 /* NOTE: trigger sent not on any VLAN */
870 /* form ethernet header */
871 memcpy(et->et_dest, NetCDPAddr, 6);
872 memcpy(et->et_src, NetOurEther, 6);
874 pkt += ETHER_HDR_SIZE;
877 memcpy((uchar *)pkt, CDP_SNAP_hdr, sizeof(CDP_SNAP_hdr));
878 pkt += sizeof(CDP_SNAP_hdr);
881 *pkt++ = 0x02; /* CDP version 2 */
882 *pkt++ = 180; /* TTL */
883 s = (volatile ushort *)pkt;
885 *s++ = htons(0); /* checksum (0 for later calculation) */
888 #ifdef CONFIG_CDP_DEVICE_ID
889 *s++ = htons(CDP_DEVICE_ID_TLV);
890 *s++ = htons(CONFIG_CDP_DEVICE_ID);
891 sprintf(buf, CONFIG_CDP_DEVICE_ID_PREFIX "%pm", NetOurEther);
892 memcpy((uchar *)s, buf, 16);
896 #ifdef CONFIG_CDP_PORT_ID
897 *s++ = htons(CDP_PORT_ID_TLV);
898 memset(buf, 0, sizeof(buf));
899 sprintf(buf, CONFIG_CDP_PORT_ID, eth_get_dev_index());
901 if (len & 1) /* make it even */
903 *s++ = htons(len + 4);
904 memcpy((uchar *)s, buf, len);
908 #ifdef CONFIG_CDP_CAPABILITIES
909 *s++ = htons(CDP_CAPABILITIES_TLV);
911 *(ulong *)s = htonl(CONFIG_CDP_CAPABILITIES);
915 #ifdef CONFIG_CDP_VERSION
916 *s++ = htons(CDP_VERSION_TLV);
917 memset(buf, 0, sizeof(buf));
918 strcpy(buf, CONFIG_CDP_VERSION);
920 if (len & 1) /* make it even */
922 *s++ = htons(len + 4);
923 memcpy((uchar *)s, buf, len);
927 #ifdef CONFIG_CDP_PLATFORM
928 *s++ = htons(CDP_PLATFORM_TLV);
929 memset(buf, 0, sizeof(buf));
930 strcpy(buf, CONFIG_CDP_PLATFORM);
932 if (len & 1) /* make it even */
934 *s++ = htons(len + 4);
935 memcpy((uchar *)s, buf, len);
939 #ifdef CONFIG_CDP_TRIGGER
940 *s++ = htons(CDP_TRIGGER_TLV);
942 *(ulong *)s = htonl(CONFIG_CDP_TRIGGER);
946 #ifdef CONFIG_CDP_POWER_CONSUMPTION
947 *s++ = htons(CDP_POWER_CONSUMPTION_TLV);
949 *s++ = htons(CONFIG_CDP_POWER_CONSUMPTION);
952 /* length of ethernet packet */
953 len = (uchar *)s - ((uchar *)NetTxPacket + ETHER_HDR_SIZE);
954 et->et_protlen = htons(len);
956 len = ETHER_HDR_SIZE + sizeof(CDP_SNAP_hdr);
957 chksum = CDP_compute_csum((uchar *)NetTxPacket + len, (uchar *)s - (NetTxPacket + len));
962 (void) eth_send(NetTxPacket, (uchar *)s - NetTxPacket);
972 NetSetTimeout (CDP_TIMEOUT, CDPTimeout);
977 /* if not OK try again */
981 NetState = NETLOOP_SUCCESS;
985 CDPDummyHandler (uchar * pkt, unsigned dest, unsigned src, unsigned len)
991 CDPHandler(const uchar * pkt, unsigned len)
1000 if (len < sizeof(CDP_SNAP_hdr) + 4)
1003 /* check for valid CDP SNAP header */
1004 if (memcmp(pkt, CDP_SNAP_hdr, sizeof(CDP_SNAP_hdr)) != 0)
1007 pkt += sizeof(CDP_SNAP_hdr);
1008 len -= sizeof(CDP_SNAP_hdr);
1010 /* Version of CDP protocol must be >= 2 and TTL != 0 */
1011 if (pkt[0] < 0x02 || pkt[1] == 0)
1014 /* if version is greater than 0x02 maybe we'll have a problem; output a warning */
1016 printf("** WARNING: CDP packet received with a protocol version %d > 2\n",
1019 if (CDP_compute_csum(pkt, len) != 0)
1031 ss = (const ushort *)pkt;
1032 type = ntohs(ss[0]);
1033 tlen = ntohs(ss[1]);
1041 ss += 2; /* point ss to the data of the TLV */
1045 case CDP_DEVICE_ID_TLV:
1047 case CDP_ADDRESS_TLV:
1049 case CDP_PORT_ID_TLV:
1051 case CDP_CAPABILITIES_TLV:
1053 case CDP_VERSION_TLV:
1055 case CDP_PLATFORM_TLV:
1057 case CDP_NATIVE_VLAN_TLV:
1060 case CDP_APPLIANCE_VLAN_TLV:
1061 t = (const uchar *)ss;
1067 ss = (const ushort *)(t + 1);
1069 #ifdef CONFIG_CDP_APPLIANCE_VLAN_TYPE
1070 if (applid == CONFIG_CDP_APPLIANCE_VLAN_TYPE)
1073 vlan = ntohs(*ss); /* XXX will this work; dunno */
1078 case CDP_TRIGGER_TLV:
1080 case CDP_POWER_CONSUMPTION_TLV:
1082 case CDP_SYSNAME_TLV:
1084 case CDP_SYSOBJECT_TLV:
1086 case CDP_MANAGEMENT_ADDRESS_TLV:
1091 CDPApplianceVLAN = vlan;
1092 CDPNativeVLAN = nvlan;
1098 printf("** CDP packet is too short\n");
1102 static void CDPStart(void)
1104 #if defined(CONFIG_NET_MULTI)
1105 printf ("Using %s device\n", eth_get_name());
1110 CDPNativeVLAN = htons(-1);
1111 CDPApplianceVLAN = htons(-1);
1113 NetSetTimeout (CDP_TIMEOUT, CDPTimeout);
1114 NetSetHandler (CDPDummyHandler);
1122 NetReceive(volatile uchar * inpkt, int len)
1130 #if defined(CONFIG_CMD_CDP)
1133 ushort cti = 0, vlanid = VLAN_NONE, myvlanid, mynvlanid;
1136 printf("packet received\n");
1139 NetRxPacket = inpkt;
1140 NetRxPacketLen = len;
1141 et = (Ethernet_t *)inpkt;
1143 /* too small packet? */
1144 if (len < ETHER_HDR_SIZE)
1149 (*push_packet)(inpkt, len);
1154 #if defined(CONFIG_CMD_CDP)
1155 /* keep track if packet is CDP */
1156 iscdp = memcmp(et->et_dest, NetCDPAddr, 6) == 0;
1159 myvlanid = ntohs(NetOurVLAN);
1160 if (myvlanid == (ushort)-1)
1161 myvlanid = VLAN_NONE;
1162 mynvlanid = ntohs(NetOurNativeVLAN);
1163 if (mynvlanid == (ushort)-1)
1164 mynvlanid = VLAN_NONE;
1166 x = ntohs(et->et_protlen);
1169 printf("packet received\n");
1174 * Got a 802 packet. Check the other protocol field.
1176 x = ntohs(et->et_prot);
1178 ip = (IP_t *)(inpkt + E802_HDR_SIZE);
1179 len -= E802_HDR_SIZE;
1181 } else if (x != PROT_VLAN) { /* normal packet */
1182 ip = (IP_t *)(inpkt + ETHER_HDR_SIZE);
1183 len -= ETHER_HDR_SIZE;
1185 } else { /* VLAN packet */
1186 VLAN_Ethernet_t *vet = (VLAN_Ethernet_t *)et;
1189 printf("VLAN packet received\n");
1191 /* too small packet? */
1192 if (len < VLAN_ETHER_HDR_SIZE)
1195 /* if no VLAN active */
1196 if ((ntohs(NetOurVLAN) & VLAN_IDMASK) == VLAN_NONE
1197 #if defined(CONFIG_CMD_CDP)
1203 cti = ntohs(vet->vet_tag);
1204 vlanid = cti & VLAN_IDMASK;
1205 x = ntohs(vet->vet_type);
1207 ip = (IP_t *)(inpkt + VLAN_ETHER_HDR_SIZE);
1208 len -= VLAN_ETHER_HDR_SIZE;
1212 printf("Receive from protocol 0x%x\n", x);
1215 #if defined(CONFIG_CMD_CDP)
1217 CDPHandler((uchar *)ip, len);
1222 if ((myvlanid & VLAN_IDMASK) != VLAN_NONE) {
1223 if (vlanid == VLAN_NONE)
1224 vlanid = (mynvlanid & VLAN_IDMASK);
1226 if (vlanid != (myvlanid & VLAN_IDMASK))
1234 * We have to deal with two types of ARP packets:
1235 * - REQUEST packets will be answered by sending our
1236 * IP address - if we know it.
1237 * - REPLY packates are expected only after we asked
1238 * for the TFTP server's or the gateway's ethernet
1239 * address; so if we receive such a packet, we set
1240 * the server ethernet address
1246 if (len < ARP_HDR_SIZE) {
1247 printf("bad length %d < %d\n", len, ARP_HDR_SIZE);
1250 if (ntohs(arp->ar_hrd) != ARP_ETHER) {
1253 if (ntohs(arp->ar_pro) != PROT_IP) {
1256 if (arp->ar_hln != 6) {
1259 if (arp->ar_pln != 4) {
1263 if (NetOurIP == 0) {
1267 if (NetReadIP(&arp->ar_data[16]) != NetOurIP) {
1271 switch (ntohs(arp->ar_op)) {
1272 case ARPOP_REQUEST: /* reply with our IP address */
1274 puts ("Got ARP REQUEST, return our IP\n");
1277 pkt += NetSetEther(pkt, et->et_src, PROT_ARP);
1278 arp->ar_op = htons(ARPOP_REPLY);
1279 memcpy (&arp->ar_data[10], &arp->ar_data[0], 6);
1280 NetCopyIP(&arp->ar_data[16], &arp->ar_data[6]);
1281 memcpy (&arp->ar_data[ 0], NetOurEther, 6);
1282 NetCopyIP(&arp->ar_data[ 6], &NetOurIP);
1283 (void) eth_send((uchar *)et, (pkt - (uchar *)et) + ARP_HDR_SIZE);
1286 case ARPOP_REPLY: /* arp reply */
1287 /* are we waiting for a reply */
1288 if (!NetArpWaitPacketIP || !NetArpWaitPacketMAC)
1291 #ifdef CONFIG_KEEP_SERVERADDR
1292 if (NetServerIP == NetArpWaitPacketIP) {
1294 sprintf(buf, "%pM", arp->ar_data);
1295 setenv("serveraddr", buf);
1300 printf("Got ARP REPLY, set server/gtwy eth addr (%pM)\n",
1304 tmp = NetReadIP(&arp->ar_data[6]);
1306 /* matched waiting packet's address */
1307 if (tmp == NetArpWaitReplyIP) {
1311 /* save address for later use */
1312 memcpy(NetArpWaitPacketMAC, &arp->ar_data[0], 6);
1314 #ifdef CONFIG_NETCONSOLE
1315 (*packetHandler)(0,0,0,0);
1317 /* modify header, and transmit it */
1318 memcpy(((Ethernet_t *)NetArpWaitTxPacket)->et_dest, NetArpWaitPacketMAC, 6);
1319 (void) eth_send(NetArpWaitTxPacket, NetArpWaitTxPacketSize);
1321 /* no arp request pending now */
1322 NetArpWaitPacketIP = 0;
1323 NetArpWaitTxPacketSize = 0;
1324 NetArpWaitPacketMAC = NULL;
1330 printf("Unexpected ARP opcode 0x%x\n", ntohs(arp->ar_op));
1338 puts ("Got RARP\n");
1341 if (len < ARP_HDR_SIZE) {
1342 printf("bad length %d < %d\n", len, ARP_HDR_SIZE);
1346 if ((ntohs(arp->ar_op) != RARPOP_REPLY) ||
1347 (ntohs(arp->ar_hrd) != ARP_ETHER) ||
1348 (ntohs(arp->ar_pro) != PROT_IP) ||
1349 (arp->ar_hln != 6) || (arp->ar_pln != 4)) {
1351 puts ("invalid RARP header\n");
1353 NetCopyIP(&NetOurIP, &arp->ar_data[16]);
1354 if (NetServerIP == 0)
1355 NetCopyIP(&NetServerIP, &arp->ar_data[ 6]);
1356 memcpy (NetServerEther, &arp->ar_data[ 0], 6);
1358 (*packetHandler)(0,0,0,0);
1366 if (len < IP_HDR_SIZE) {
1367 debug ("len bad %d < %lu\n", len, (ulong)IP_HDR_SIZE);
1370 if (len < ntohs(ip->ip_len)) {
1371 printf("len bad %d < %d\n", len, ntohs(ip->ip_len));
1374 len = ntohs(ip->ip_len);
1376 printf("len=%d, v=%02x\n", len, ip->ip_hl_v & 0xff);
1378 if ((ip->ip_hl_v & 0xf0) != 0x40) {
1381 /* Can't deal with fragments */
1382 if (ip->ip_off & htons(IP_OFFS | IP_FLAGS_MFRAG)) {
1385 /* can't deal with headers > 20 bytes */
1386 if ((ip->ip_hl_v & 0x0f) > 0x05) {
1389 if (!NetCksumOk((uchar *)ip, IP_HDR_SIZE_NO_UDP / 2)) {
1390 puts ("checksum bad\n");
1393 tmp = NetReadIP(&ip->ip_dst);
1394 if (NetOurIP && tmp != NetOurIP && tmp != 0xFFFFFFFF) {
1395 #ifdef CONFIG_MCAST_TFTP
1396 if (Mcast_addr != tmp)
1401 * watch for ICMP host redirects
1403 * There is no real handler code (yet). We just watch
1404 * for ICMP host redirect messages. In case anybody
1405 * sees these messages: please contact me
1406 * (wd@denx.de), or - even better - send me the
1407 * necessary fixes :-)
1409 * Note: in all cases where I have seen this so far
1410 * it was a problem with the router configuration,
1411 * for instance when a router was configured in the
1412 * BOOTP reply, but the TFTP server was on the same
1413 * subnet. So this is probably a warning that your
1414 * configuration might be wrong. But I'm not really
1415 * sure if there aren't any other situations.
1417 if (ip->ip_p == IPPROTO_ICMP) {
1418 ICMP_t *icmph = (ICMP_t *)&(ip->udp_src);
1420 switch (icmph->type) {
1422 if (icmph->code != ICMP_REDIR_HOST)
1424 printf (" ICMP Host Redirect to %pI4 ", &icmph->un.gateway);
1426 #if defined(CONFIG_CMD_PING)
1427 case ICMP_ECHO_REPLY:
1429 * IP header OK. Pass the packet to the current handler.
1431 /* XXX point to ip packet */
1432 (*packetHandler)((uchar *)ip, 0, 0, 0);
1434 case ICMP_ECHO_REQUEST:
1436 printf ("Got ICMP ECHO REQUEST, return %d bytes \n",
1437 ETHER_HDR_SIZE + len);
1439 memcpy (&et->et_dest[0], &et->et_src[0], 6);
1440 memcpy (&et->et_src[ 0], NetOurEther, 6);
1444 NetCopyIP((void*)&ip->ip_dst, &ip->ip_src);
1445 NetCopyIP((void*)&ip->ip_src, &NetOurIP);
1446 ip->ip_sum = ~NetCksum((uchar *)ip, IP_HDR_SIZE_NO_UDP >> 1);
1448 icmph->type = ICMP_ECHO_REPLY;
1449 icmph->checksum = 0;
1450 icmph->checksum = ~NetCksum((uchar *)icmph,
1451 (len - IP_HDR_SIZE_NO_UDP) >> 1);
1452 (void) eth_send((uchar *)et, ETHER_HDR_SIZE + len);
1458 } else if (ip->ip_p != IPPROTO_UDP) { /* Only UDP packets */
1462 #ifdef CONFIG_UDP_CHECKSUM
1463 if (ip->udp_xsum != 0) {
1469 xsum += (ntohs(ip->udp_len));
1470 xsum += (ntohl(ip->ip_src) >> 16) & 0x0000ffff;
1471 xsum += (ntohl(ip->ip_src) >> 0) & 0x0000ffff;
1472 xsum += (ntohl(ip->ip_dst) >> 16) & 0x0000ffff;
1473 xsum += (ntohl(ip->ip_dst) >> 0) & 0x0000ffff;
1475 sumlen = ntohs(ip->udp_len);
1476 sumptr = (ushort *) &(ip->udp_src);
1478 while (sumlen > 1) {
1481 sumdata = *sumptr++;
1482 xsum += ntohs(sumdata);
1488 sumdata = *(unsigned char *) sumptr;
1489 sumdata = (sumdata << 8) & 0xff00;
1492 while ((xsum >> 16) != 0) {
1493 xsum = (xsum & 0x0000ffff) + ((xsum >> 16) & 0x0000ffff);
1495 if ((xsum != 0x00000000) && (xsum != 0x0000ffff)) {
1496 printf(" UDP wrong checksum %08lx %08x\n",
1497 xsum, ntohs(ip->udp_xsum));
1504 #ifdef CONFIG_NETCONSOLE
1505 nc_input_packet((uchar *)ip +IP_HDR_SIZE,
1508 ntohs(ip->udp_len) - 8);
1511 * IP header OK. Pass the packet to the current handler.
1513 (*packetHandler)((uchar *)ip +IP_HDR_SIZE,
1516 ntohs(ip->udp_len) - 8);
1522 /**********************************************************************/
1524 static int net_check_prereq (proto_t protocol)
1528 #if defined(CONFIG_CMD_PING)
1530 if (NetPingIP == 0) {
1531 puts ("*** ERROR: ping address not given\n");
1536 #if defined(CONFIG_CMD_SNTP)
1538 if (NetNtpServerIP == 0) {
1539 puts ("*** ERROR: NTP server address not given\n");
1544 #if defined(CONFIG_CMD_DNS)
1546 if (NetOurDNSIP == 0) {
1547 puts("*** ERROR: DNS server address not given\n");
1552 #if defined(CONFIG_CMD_NFS)
1557 if (NetServerIP == 0) {
1558 puts ("*** ERROR: `serverip' not set\n");
1561 #if defined(CONFIG_CMD_PING) || defined(CONFIG_CMD_SNTP)
1565 if (NetOurIP == 0) {
1566 puts ("*** ERROR: `ipaddr' not set\n");
1575 if (memcmp (NetOurEther, "\0\0\0\0\0\0", 6) == 0) {
1576 #ifdef CONFIG_NET_MULTI
1577 extern int eth_get_dev_index (void);
1578 int num = eth_get_dev_index ();
1582 puts ("*** ERROR: No ethernet found.\n");
1585 puts ("*** ERROR: `ethaddr' not set\n");
1588 printf ("*** ERROR: `eth%daddr' not set\n",
1596 puts ("*** ERROR: `ethaddr' not set\n");
1604 return (0); /* OK */
1606 /**********************************************************************/
1609 NetCksumOk(uchar * ptr, int len)
1611 return !((NetCksum(ptr, len) + 1) & 0xfffe);
1616 NetCksum(uchar * ptr, int len)
1619 ushort *p = (ushort *)ptr;
1624 xsum = (xsum & 0xffff) + (xsum >> 16);
1625 xsum = (xsum & 0xffff) + (xsum >> 16);
1626 return (xsum & 0xffff);
1634 myvlanid = ntohs(NetOurVLAN);
1635 if (myvlanid == (ushort)-1)
1636 myvlanid = VLAN_NONE;
1638 return ((myvlanid & VLAN_IDMASK) == VLAN_NONE) ? ETHER_HDR_SIZE : VLAN_ETHER_HDR_SIZE;
1642 NetSetEther(volatile uchar * xet, uchar * addr, uint prot)
1644 Ethernet_t *et = (Ethernet_t *)xet;
1647 myvlanid = ntohs(NetOurVLAN);
1648 if (myvlanid == (ushort)-1)
1649 myvlanid = VLAN_NONE;
1651 memcpy (et->et_dest, addr, 6);
1652 memcpy (et->et_src, NetOurEther, 6);
1653 if ((myvlanid & VLAN_IDMASK) == VLAN_NONE) {
1654 et->et_protlen = htons(prot);
1655 return ETHER_HDR_SIZE;
1657 VLAN_Ethernet_t *vet = (VLAN_Ethernet_t *)xet;
1659 vet->vet_vlan_type = htons(PROT_VLAN);
1660 vet->vet_tag = htons((0 << 5) | (myvlanid & VLAN_IDMASK));
1661 vet->vet_type = htons(prot);
1662 return VLAN_ETHER_HDR_SIZE;
1667 NetSetIP(volatile uchar * xip, IPaddr_t dest, int dport, int sport, int len)
1669 IP_t *ip = (IP_t *)xip;
1672 * If the data is an odd number of bytes, zero the
1673 * byte after the last byte so that the checksum
1677 xip[IP_HDR_SIZE + len] = 0;
1680 * Construct an IP and UDP header.
1681 * (need to set no fragment bit - XXX)
1683 ip->ip_hl_v = 0x45; /* IP_HDR_SIZE / 4 (not including UDP) */
1685 ip->ip_len = htons(IP_HDR_SIZE + len);
1686 ip->ip_id = htons(NetIPID++);
1687 ip->ip_off = htons(IP_FLAGS_DFRAG); /* Don't fragment */
1689 ip->ip_p = 17; /* UDP */
1691 NetCopyIP((void*)&ip->ip_src, &NetOurIP); /* already in network byte order */
1692 NetCopyIP((void*)&ip->ip_dst, &dest); /* - "" - */
1693 ip->udp_src = htons(sport);
1694 ip->udp_dst = htons(dport);
1695 ip->udp_len = htons(8 + len);
1697 ip->ip_sum = ~NetCksum((uchar *)ip, IP_HDR_SIZE_NO_UDP / 2);
1700 void copy_filename (char *dst, char *src, int size)
1702 if (*src && (*src == '"')) {
1707 while ((--size > 0) && *src && (*src != '"')) {
1715 #if defined(CONFIG_CMD_NFS) || defined(CONFIG_CMD_SNTP) || defined(CONFIG_CMD_DNS)
1717 * make port a little random, but use something trivial to compute
1719 unsigned int random_port(void)
1721 return 1024 + (get_timer(0) % 0x8000);;
1725 void ip_to_string (IPaddr_t x, char *s)
1728 sprintf (s, "%d.%d.%d.%d",
1729 (int) ((x >> 24) & 0xff),
1730 (int) ((x >> 16) & 0xff),
1731 (int) ((x >> 8) & 0xff), (int) ((x >> 0) & 0xff)
1735 IPaddr_t string_to_ip(char *s)
1744 for (addr=0, i=0; i<4; ++i) {
1745 ulong val = s ? simple_strtoul(s, &e, 10) : 0;
1747 addr |= (val & 0xFF);
1753 return (htonl(addr));
1756 void VLAN_to_string(ushort x, char *s)
1760 if (x == (ushort)-1)
1766 sprintf(s, "%d", x & VLAN_IDMASK);
1769 ushort string_to_VLAN(char *s)
1774 return htons(VLAN_NONE);
1776 if (*s < '0' || *s > '9')
1779 id = (ushort)simple_strtoul(s, NULL, 10);
1784 IPaddr_t getenv_IPaddr (char *var)
1786 return (string_to_ip(getenv(var)));
1789 ushort getenv_VLAN(char *var)
1791 return (string_to_VLAN(getenv(var)));