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 5UL /* 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 #ifdef CONFIG_MCAST_TFTP /* Multicast TFTP */
125 /** END OF BOOTP EXTENTIONS **/
127 ulong NetBootFileXferSize; /* The actual transferred size of the bootfile (in bytes) */
128 uchar NetOurEther[6]; /* Our ethernet address */
129 uchar NetServerEther[6] = /* Boot server enet address */
130 { 0, 0, 0, 0, 0, 0 };
131 IPaddr_t NetOurIP; /* Our IP addr (0 = unknown) */
132 IPaddr_t NetServerIP; /* Our IP addr (0 = unknown) */
133 volatile uchar *NetRxPkt; /* Current receive packet */
134 int NetRxPktLen; /* Current rx packet length */
135 unsigned NetIPID; /* IP packet ID */
136 uchar NetBcastAddr[6] = /* Ethernet bcast address */
137 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
138 uchar NetEtherNullAddr[6] =
139 { 0, 0, 0, 0, 0, 0 };
141 void (*push_packet)(volatile void *, int len) = 0;
143 #if defined(CONFIG_CMD_CDP)
144 uchar NetCDPAddr[6] = /* Ethernet bcast address */
145 { 0x01, 0x00, 0x0c, 0xcc, 0xcc, 0xcc };
147 int NetState; /* Network loop state */
148 #ifdef CONFIG_NET_MULTI
149 int NetRestartWrap = 0; /* Tried all network devices */
150 static int NetRestarted = 0; /* Network loop restarted */
151 static int NetDevExists = 0; /* At least one device configured */
154 /* XXX in both little & big endian machines 0xFFFF == ntohs(-1) */
155 ushort NetOurVLAN = 0xFFFF; /* default is without VLAN */
156 ushort NetOurNativeVLAN = 0xFFFF; /* ditto */
158 char BootFile[128]; /* Boot File name */
160 #if defined(CONFIG_CMD_PING)
161 IPaddr_t NetPingIP; /* the ip address to ping */
163 static void PingStart(void);
166 #if defined(CONFIG_CMD_CDP)
167 static void CDPStart(void);
170 #if defined(CONFIG_CMD_SNTP)
171 IPaddr_t NetNtpServerIP; /* NTP server IP address */
172 int NetTimeOffset=0; /* offset time from UTC */
175 #ifdef CONFIG_NETCONSOLE
177 int nc_input_packet(uchar *pkt, unsigned dest, unsigned src, unsigned len);
180 volatile uchar PktBuf[(PKTBUFSRX+1) * PKTSIZE_ALIGN + PKTALIGN];
182 volatile uchar *NetRxPackets[PKTBUFSRX]; /* Receive packets */
184 static rxhand_f *packetHandler; /* Current RX packet handler */
185 static thand_f *timeHandler; /* Current timeout handler */
186 static ulong timeStart; /* Time base value */
187 static ulong timeDelta; /* Current timeout value */
188 volatile uchar *NetTxPacket = 0; /* THE transmit packet */
190 static int net_check_prereq (proto_t protocol);
192 /**********************************************************************/
194 IPaddr_t NetArpWaitPacketIP;
195 IPaddr_t NetArpWaitReplyIP;
196 uchar *NetArpWaitPacketMAC; /* MAC address of waiting packet's destination */
197 uchar *NetArpWaitTxPacket; /* THE transmit packet */
198 int NetArpWaitTxPacketSize;
199 uchar NetArpWaitPacketBuf[PKTSIZE_ALIGN + PKTALIGN];
200 ulong NetArpWaitTimerStart;
203 void ArpRequest (void)
210 printf ("ARP broadcast %d\n", NetArpWaitTry);
214 pkt += NetSetEther (pkt, NetBcastAddr, PROT_ARP);
218 arp->ar_hrd = htons (ARP_ETHER);
219 arp->ar_pro = htons (PROT_IP);
222 arp->ar_op = htons (ARPOP_REQUEST);
224 memcpy (&arp->ar_data[0], NetOurEther, 6); /* source ET addr */
225 NetWriteIP ((uchar *) & arp->ar_data[6], NetOurIP); /* source IP addr */
226 for (i = 10; i < 16; ++i) {
227 arp->ar_data[i] = 0; /* dest ET addr = 0 */
230 if ((NetArpWaitPacketIP & NetOurSubnetMask) !=
231 (NetOurIP & NetOurSubnetMask)) {
232 if (NetOurGatewayIP == 0) {
233 puts ("## Warning: gatewayip needed but not set\n");
234 NetArpWaitReplyIP = NetArpWaitPacketIP;
236 NetArpWaitReplyIP = NetOurGatewayIP;
239 NetArpWaitReplyIP = NetArpWaitPacketIP;
242 NetWriteIP ((uchar *) & arp->ar_data[16], NetArpWaitReplyIP);
243 (void) eth_send (NetTxPacket, (pkt - NetTxPacket) + ARP_HDR_SIZE);
246 void ArpTimeoutCheck(void)
250 if (!NetArpWaitPacketIP)
255 /* check for arp timeout */
256 if ((t - NetArpWaitTimerStart) > ARP_TIMEOUT * CFG_HZ) {
259 if (NetArpWaitTry >= ARP_TIMEOUT_COUNT) {
260 puts ("\nARP Retry count exceeded; starting again\n");
264 NetArpWaitTimerStart = t;
270 /**********************************************************************/
272 * Main network processing loop.
276 NetLoop(proto_t protocol)
280 #ifdef CONFIG_NET_MULTI
285 /* XXX problem with bss workaround */
286 NetArpWaitPacketMAC = NULL;
287 NetArpWaitTxPacket = NULL;
288 NetArpWaitPacketIP = 0;
289 NetArpWaitReplyIP = 0;
290 NetArpWaitTxPacket = NULL;
296 * Setup packet buffers, aligned correctly.
298 NetTxPacket = &PktBuf[0] + (PKTALIGN - 1);
299 NetTxPacket -= (ulong)NetTxPacket % PKTALIGN;
300 for (i = 0; i < PKTBUFSRX; i++) {
301 NetRxPackets[i] = NetTxPacket + (i+1)*PKTSIZE_ALIGN;
305 if (!NetArpWaitTxPacket) {
306 NetArpWaitTxPacket = &NetArpWaitPacketBuf[0] + (PKTALIGN - 1);
307 NetArpWaitTxPacket -= (ulong)NetArpWaitTxPacket % PKTALIGN;
308 NetArpWaitTxPacketSize = 0;
312 #ifdef CONFIG_NET_MULTI
315 if (eth_init(bd) < 0) {
321 #ifdef CONFIG_NET_MULTI
322 memcpy (NetOurEther, eth_get_dev()->enetaddr, 6);
324 memcpy (NetOurEther, bd->bi_enetaddr, 6);
327 NetState = NETLOOP_CONTINUE;
330 * Start the ball rolling with the given start function. From
331 * here on, this code is a state machine driven by received
332 * packets and timer events.
336 #if defined(CONFIG_CMD_NFS)
339 #if defined(CONFIG_CMD_PING)
342 #if defined(CONFIG_CMD_SNTP)
347 NetCopyIP(&NetOurIP, &bd->bi_ip_addr);
348 NetOurGatewayIP = getenv_IPaddr ("gatewayip");
349 NetOurSubnetMask= getenv_IPaddr ("netmask");
350 NetOurVLAN = getenv_VLAN("vlan");
351 NetOurNativeVLAN = getenv_VLAN("nvlan");
354 #if defined(CONFIG_CMD_NFS)
359 NetServerIP = getenv_IPaddr ("serverip");
361 #if defined(CONFIG_CMD_PING)
366 #if defined(CONFIG_CMD_SNTP)
379 * initialize our IP addr to 0 in order to accept ANY
380 * IP addr assigned to us by the BOOTP / RARP server
383 NetServerIP = getenv_IPaddr ("serverip");
384 NetOurVLAN = getenv_VLAN("vlan"); /* VLANs must be read */
385 NetOurNativeVLAN = getenv_VLAN("nvlan");
387 NetOurVLAN = getenv_VLAN("vlan"); /* VLANs must be read */
388 NetOurNativeVLAN = getenv_VLAN("nvlan");
394 switch (net_check_prereq (protocol)) {
396 /* network not configured */
400 #ifdef CONFIG_NET_MULTI
402 /* network device not configured */
404 #endif /* CONFIG_NET_MULTI */
407 #ifdef CONFIG_NET_MULTI
412 /* always use ARP to get server ethernet address */
416 #if defined(CONFIG_CMD_DHCP)
418 /* Start with a clean slate... */
421 NetServerIP = getenv_IPaddr ("serverip");
422 DhcpRequest(); /* Basically same as BOOTP */
435 #if defined(CONFIG_CMD_PING)
440 #if defined(CONFIG_CMD_NFS)
445 #if defined(CONFIG_CMD_CDP)
450 #ifdef CONFIG_NETCONSOLE
455 #if defined(CONFIG_CMD_SNTP)
464 NetBootFileXferSize = 0;
468 #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
469 #if defined(CFG_FAULT_ECHO_LINK_DOWN) && defined(CONFIG_STATUS_LED) && defined(STATUS_LED_RED)
471 * Echo the inverted link state to the fault LED.
473 if(miiphy_link(eth_get_dev()->name, CFG_FAULT_MII_ADDR)) {
474 status_led_set (STATUS_LED_RED, STATUS_LED_OFF);
476 status_led_set (STATUS_LED_RED, STATUS_LED_ON);
478 #endif /* CFG_FAULT_ECHO_LINK_DOWN, ... */
479 #endif /* CONFIG_MII, ... */
482 * Main packet reception loop. Loop receiving packets until
483 * someone sets `NetState' to a state that terminates.
487 #ifdef CONFIG_SHOW_ACTIVITY
489 extern void show_activity(int arg);
494 * Check the ethernet for a new packet. The ethernet
495 * receive routine will process it.
500 * Abort if ctrl-c was pressed.
511 * Check for a timeout, and run the timeout handler
514 if (timeHandler && ((get_timer(0) - timeStart) > timeDelta)) {
517 #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
518 # if defined(CFG_FAULT_ECHO_LINK_DOWN) && \
519 defined(CONFIG_STATUS_LED) && \
520 defined(STATUS_LED_RED)
522 * Echo the inverted link state to the fault LED.
524 if(miiphy_link(eth_get_dev()->name, CFG_FAULT_MII_ADDR)) {
525 status_led_set (STATUS_LED_RED, STATUS_LED_OFF);
527 status_led_set (STATUS_LED_RED, STATUS_LED_ON);
529 # endif /* CFG_FAULT_ECHO_LINK_DOWN, ... */
530 #endif /* CONFIG_MII, ... */
532 timeHandler = (thand_f *)0;
539 case NETLOOP_RESTART:
540 #ifdef CONFIG_NET_MULTI
545 case NETLOOP_SUCCESS:
546 if (NetBootFileXferSize > 0) {
548 printf("Bytes transferred = %ld (%lx hex)\n",
550 NetBootFileXferSize);
551 sprintf(buf, "%lX", NetBootFileXferSize);
552 setenv("filesize", buf);
554 sprintf(buf, "%lX", (unsigned long)load_addr);
555 setenv("fileaddr", buf);
558 return NetBootFileXferSize;
566 /**********************************************************************/
569 startAgainTimeout(void)
571 NetState = NETLOOP_RESTART;
575 startAgainHandler(uchar * pkt, unsigned dest, unsigned src, unsigned len)
577 /* Totally ignore the packet */
580 void NetStartAgain (void)
583 int noretry = 0, once = 0;
585 if ((nretry = getenv ("netretry")) != NULL) {
586 noretry = (strcmp (nretry, "no") == 0);
587 once = (strcmp (nretry, "once") == 0);
591 NetState = NETLOOP_FAIL;
594 #ifndef CONFIG_NET_MULTI
595 NetSetTimeout (10UL * CFG_HZ, startAgainTimeout);
596 NetSetHandler (startAgainHandler);
597 #else /* !CONFIG_NET_MULTI*/
599 #if !defined(CONFIG_NET_DO_NOT_TRY_ANOTHER)
600 eth_try_another (!NetRestarted);
603 if (NetRestartWrap) {
605 if (NetDevExists && !once) {
606 NetSetTimeout (10UL * CFG_HZ, startAgainTimeout);
607 NetSetHandler (startAgainHandler);
609 NetState = NETLOOP_FAIL;
612 NetState = NETLOOP_RESTART;
614 #endif /* CONFIG_NET_MULTI */
617 /**********************************************************************/
623 NetSetHandler(rxhand_f * f)
630 NetSetTimeout(ulong iv, thand_f * f)
633 timeHandler = (thand_f *)0;
636 timeStart = get_timer(0);
643 NetSendPacket(volatile uchar * pkt, int len)
645 (void) eth_send(pkt, len);
649 NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport, int sport, int len)
653 /* convert to new style broadcast */
657 /* if broadcast, make the ether address a broadcast and don't do ARP */
658 if (dest == 0xFFFFFFFF)
659 ether = NetBcastAddr;
661 /* if MAC address was not discovered yet, save the packet and do an ARP request */
662 if (memcmp(ether, NetEtherNullAddr, 6) == 0) {
665 printf("sending ARP for %08lx\n", dest);
667 NetArpWaitPacketIP = dest;
668 NetArpWaitPacketMAC = ether;
670 pkt = NetArpWaitTxPacket;
671 pkt += NetSetEther (pkt, NetArpWaitPacketMAC, PROT_IP);
673 NetSetIP (pkt, dest, dport, sport, len);
674 memcpy(pkt + IP_HDR_SIZE, (uchar *)NetTxPacket + (pkt - (uchar *)NetArpWaitTxPacket) + IP_HDR_SIZE, len);
676 /* size of the waiting packet */
677 NetArpWaitTxPacketSize = (pkt - NetArpWaitTxPacket) + IP_HDR_SIZE + len;
679 /* and do the ARP request */
681 NetArpWaitTimerStart = get_timer(0);
683 return 1; /* waiting */
687 printf("sending UDP to %08lx/%02x:%02x:%02x:%02x:%02x:%02x\n",
688 dest, ether[0], ether[1], ether[2], ether[3], ether[4], ether[5]);
691 pkt = (uchar *)NetTxPacket;
692 pkt += NetSetEther (pkt, ether, PROT_IP);
693 NetSetIP (pkt, dest, dport, sport, len);
694 (void) eth_send(NetTxPacket, (pkt - NetTxPacket) + IP_HDR_SIZE + len);
696 return 0; /* transmitted */
699 #if defined(CONFIG_CMD_PING)
700 static ushort PingSeqNo;
709 /* XXX always send arp request */
711 memcpy(mac, NetEtherNullAddr, 6);
714 printf("sending ARP for %08lx\n", NetPingIP);
717 NetArpWaitPacketIP = NetPingIP;
718 NetArpWaitPacketMAC = mac;
720 pkt = NetArpWaitTxPacket;
721 pkt += NetSetEther(pkt, mac, PROT_IP);
723 ip = (volatile IP_t *)pkt;
726 * Construct an IP and ICMP header. (need to set no fragment bit - XXX)
728 ip->ip_hl_v = 0x45; /* IP_HDR_SIZE / 4 (not including UDP) */
730 ip->ip_len = htons(IP_HDR_SIZE_NO_UDP + 8);
731 ip->ip_id = htons(NetIPID++);
732 ip->ip_off = htons(0x4000); /* No fragmentation */
734 ip->ip_p = 0x01; /* ICMP */
736 NetCopyIP((void*)&ip->ip_src, &NetOurIP); /* already in network byte order */
737 NetCopyIP((void*)&ip->ip_dst, &NetPingIP); /* - "" - */
738 ip->ip_sum = ~NetCksum((uchar *)ip, IP_HDR_SIZE_NO_UDP / 2);
740 s = &ip->udp_src; /* XXX ICMP starts here */
741 s[0] = htons(0x0800); /* echo-request, code */
742 s[1] = 0; /* checksum */
743 s[2] = 0; /* identifier */
744 s[3] = htons(PingSeqNo++); /* sequence number */
745 s[1] = ~NetCksum((uchar *)s, 8/2);
747 /* size of the waiting packet */
748 NetArpWaitTxPacketSize = (pkt - NetArpWaitTxPacket) + IP_HDR_SIZE_NO_UDP + 8;
750 /* and do the ARP request */
752 NetArpWaitTimerStart = get_timer(0);
754 return 1; /* waiting */
761 NetState = NETLOOP_FAIL; /* we did not get the reply */
765 PingHandler (uchar * pkt, unsigned dest, unsigned src, unsigned len)
768 volatile IP_t *ip = (volatile IP_t *)pkt;
770 tmp = NetReadIP((void *)&ip->ip_src);
771 if (tmp != NetPingIP)
774 NetState = NETLOOP_SUCCESS;
777 static void PingStart(void)
779 #if defined(CONFIG_NET_MULTI)
780 printf ("Using %s device\n", eth_get_name());
781 #endif /* CONFIG_NET_MULTI */
782 NetSetTimeout (10UL * CFG_HZ, PingTimeout);
783 NetSetHandler (PingHandler);
789 #if defined(CONFIG_CMD_CDP)
791 #define CDP_DEVICE_ID_TLV 0x0001
792 #define CDP_ADDRESS_TLV 0x0002
793 #define CDP_PORT_ID_TLV 0x0003
794 #define CDP_CAPABILITIES_TLV 0x0004
795 #define CDP_VERSION_TLV 0x0005
796 #define CDP_PLATFORM_TLV 0x0006
797 #define CDP_NATIVE_VLAN_TLV 0x000a
798 #define CDP_APPLIANCE_VLAN_TLV 0x000e
799 #define CDP_TRIGGER_TLV 0x000f
800 #define CDP_POWER_CONSUMPTION_TLV 0x0010
801 #define CDP_SYSNAME_TLV 0x0014
802 #define CDP_SYSOBJECT_TLV 0x0015
803 #define CDP_MANAGEMENT_ADDRESS_TLV 0x0016
805 #define CDP_TIMEOUT (CFG_HZ/4) /* one packet every 250ms */
810 ushort CDPNativeVLAN;
811 ushort CDPApplianceVLAN;
813 static const uchar CDP_SNAP_hdr[8] = { 0xAA, 0xAA, 0x03, 0x00, 0x00, 0x0C, 0x20, 0x00 };
815 static ushort CDP_compute_csum(const uchar *buff, ushort len)
824 odd = 1 & (ulong)buff;
834 if (result & 0x80000000)
835 result = (result & 0xFFFF) + (result >> 16);
839 leftover = (signed short)(*(const signed char *)buff);
840 /* CISCO SUCKS big time! (and blows too):
841 * CDP uses the IP checksum algorithm with a twist;
842 * for the last byte it *sign* extends and sums.
844 result = (result & 0xffff0000) | ((result + leftover) & 0x0000ffff);
847 result = (result & 0xFFFF) + (result >> 16);
850 result = ((result >> 8) & 0xff) | ((result & 0xff) << 8);
853 /* add up 16-bit and 17-bit words for 17+c bits */
854 result = (result & 0xffff) + (result >> 16);
855 /* add up 16-bit and 2-bit for 16+c bit */
856 result = (result & 0xffff) + (result >> 16);
858 result = (result & 0xffff) + (result >> 16);
861 csum = ~(ushort)result;
863 /* run time endian detection */
864 if (csum != htons(csum)) /* little endian */
870 int CDPSendTrigger(void)
878 #if defined(CONFIG_CDP_DEVICE_ID) || defined(CONFIG_CDP_PORT_ID) || \
879 defined(CONFIG_CDP_VERSION) || defined(CONFIG_CDP_PLATFORM)
884 et = (Ethernet_t *)pkt;
886 /* NOTE: trigger sent not on any VLAN */
888 /* form ethernet header */
889 memcpy(et->et_dest, NetCDPAddr, 6);
890 memcpy(et->et_src, NetOurEther, 6);
892 pkt += ETHER_HDR_SIZE;
895 memcpy((uchar *)pkt, CDP_SNAP_hdr, sizeof(CDP_SNAP_hdr));
896 pkt += sizeof(CDP_SNAP_hdr);
899 *pkt++ = 0x02; /* CDP version 2 */
900 *pkt++ = 180; /* TTL */
901 s = (volatile ushort *)pkt;
903 *s++ = htons(0); /* checksum (0 for later calculation) */
906 #ifdef CONFIG_CDP_DEVICE_ID
907 *s++ = htons(CDP_DEVICE_ID_TLV);
908 *s++ = htons(CONFIG_CDP_DEVICE_ID);
909 memset(buf, 0, sizeof(buf));
910 sprintf(buf, CONFIG_CDP_DEVICE_ID_PREFIX "%02X%02X%02X%02X%02X%02X",
911 NetOurEther[0] & 0xff, NetOurEther[1] & 0xff,
912 NetOurEther[2] & 0xff, NetOurEther[3] & 0xff,
913 NetOurEther[4] & 0xff, NetOurEther[5] & 0xff);
914 memcpy((uchar *)s, buf, 16);
918 #ifdef CONFIG_CDP_PORT_ID
919 *s++ = htons(CDP_PORT_ID_TLV);
920 memset(buf, 0, sizeof(buf));
921 sprintf(buf, CONFIG_CDP_PORT_ID, eth_get_dev_index());
923 if (len & 1) /* make it even */
925 *s++ = htons(len + 4);
926 memcpy((uchar *)s, buf, len);
930 #ifdef CONFIG_CDP_CAPABILITIES
931 *s++ = htons(CDP_CAPABILITIES_TLV);
933 *(ulong *)s = htonl(CONFIG_CDP_CAPABILITIES);
937 #ifdef CONFIG_CDP_VERSION
938 *s++ = htons(CDP_VERSION_TLV);
939 memset(buf, 0, sizeof(buf));
940 strcpy(buf, CONFIG_CDP_VERSION);
942 if (len & 1) /* make it even */
944 *s++ = htons(len + 4);
945 memcpy((uchar *)s, buf, len);
949 #ifdef CONFIG_CDP_PLATFORM
950 *s++ = htons(CDP_PLATFORM_TLV);
951 memset(buf, 0, sizeof(buf));
952 strcpy(buf, CONFIG_CDP_PLATFORM);
954 if (len & 1) /* make it even */
956 *s++ = htons(len + 4);
957 memcpy((uchar *)s, buf, len);
961 #ifdef CONFIG_CDP_TRIGGER
962 *s++ = htons(CDP_TRIGGER_TLV);
964 *(ulong *)s = htonl(CONFIG_CDP_TRIGGER);
968 #ifdef CONFIG_CDP_POWER_CONSUMPTION
969 *s++ = htons(CDP_POWER_CONSUMPTION_TLV);
971 *s++ = htons(CONFIG_CDP_POWER_CONSUMPTION);
974 /* length of ethernet packet */
975 len = (uchar *)s - ((uchar *)NetTxPacket + ETHER_HDR_SIZE);
976 et->et_protlen = htons(len);
978 len = ETHER_HDR_SIZE + sizeof(CDP_SNAP_hdr);
979 chksum = CDP_compute_csum((uchar *)NetTxPacket + len, (uchar *)s - (NetTxPacket + len));
984 (void) eth_send(NetTxPacket, (uchar *)s - NetTxPacket);
994 NetSetTimeout (CDP_TIMEOUT, CDPTimeout);
999 /* if not OK try again */
1003 NetState = NETLOOP_SUCCESS;
1007 CDPDummyHandler (uchar * pkt, unsigned dest, unsigned src, unsigned len)
1013 CDPHandler(const uchar * pkt, unsigned len)
1022 if (len < sizeof(CDP_SNAP_hdr) + 4)
1025 /* check for valid CDP SNAP header */
1026 if (memcmp(pkt, CDP_SNAP_hdr, sizeof(CDP_SNAP_hdr)) != 0)
1029 pkt += sizeof(CDP_SNAP_hdr);
1030 len -= sizeof(CDP_SNAP_hdr);
1032 /* Version of CDP protocol must be >= 2 and TTL != 0 */
1033 if (pkt[0] < 0x02 || pkt[1] == 0)
1036 /* if version is greater than 0x02 maybe we'll have a problem; output a warning */
1038 printf("** WARNING: CDP packet received with a protocol version %d > 2\n",
1041 if (CDP_compute_csum(pkt, len) != 0)
1053 ss = (const ushort *)pkt;
1054 type = ntohs(ss[0]);
1055 tlen = ntohs(ss[1]);
1063 ss += 2; /* point ss to the data of the TLV */
1067 case CDP_DEVICE_ID_TLV:
1069 case CDP_ADDRESS_TLV:
1071 case CDP_PORT_ID_TLV:
1073 case CDP_CAPABILITIES_TLV:
1075 case CDP_VERSION_TLV:
1077 case CDP_PLATFORM_TLV:
1079 case CDP_NATIVE_VLAN_TLV:
1082 case CDP_APPLIANCE_VLAN_TLV:
1083 t = (const uchar *)ss;
1089 ss = (const ushort *)(t + 1);
1091 #ifdef CONFIG_CDP_APPLIANCE_VLAN_TYPE
1092 if (applid == CONFIG_CDP_APPLIANCE_VLAN_TYPE)
1095 vlan = ntohs(*ss); /* XXX will this work; dunno */
1100 case CDP_TRIGGER_TLV:
1102 case CDP_POWER_CONSUMPTION_TLV:
1104 case CDP_SYSNAME_TLV:
1106 case CDP_SYSOBJECT_TLV:
1108 case CDP_MANAGEMENT_ADDRESS_TLV:
1113 CDPApplianceVLAN = vlan;
1114 CDPNativeVLAN = nvlan;
1120 printf("** CDP packet is too short\n");
1124 static void CDPStart(void)
1126 #if defined(CONFIG_NET_MULTI)
1127 printf ("Using %s device\n", eth_get_name());
1132 CDPNativeVLAN = htons(-1);
1133 CDPApplianceVLAN = htons(-1);
1135 NetSetTimeout (CDP_TIMEOUT, CDPTimeout);
1136 NetSetHandler (CDPDummyHandler);
1144 NetReceive(volatile uchar * inpkt, int len)
1152 #if defined(CONFIG_CMD_CDP)
1155 ushort cti = 0, vlanid = VLAN_NONE, myvlanid, mynvlanid;
1158 printf("packet received\n");
1163 et = (Ethernet_t *)inpkt;
1165 /* too small packet? */
1166 if (len < ETHER_HDR_SIZE)
1171 (*push_packet)(inpkt, len);
1176 #if defined(CONFIG_CMD_CDP)
1177 /* keep track if packet is CDP */
1178 iscdp = memcmp(et->et_dest, NetCDPAddr, 6) == 0;
1181 myvlanid = ntohs(NetOurVLAN);
1182 if (myvlanid == (ushort)-1)
1183 myvlanid = VLAN_NONE;
1184 mynvlanid = ntohs(NetOurNativeVLAN);
1185 if (mynvlanid == (ushort)-1)
1186 mynvlanid = VLAN_NONE;
1188 x = ntohs(et->et_protlen);
1191 printf("packet received\n");
1196 * Got a 802 packet. Check the other protocol field.
1198 x = ntohs(et->et_prot);
1200 ip = (IP_t *)(inpkt + E802_HDR_SIZE);
1201 len -= E802_HDR_SIZE;
1203 } else if (x != PROT_VLAN) { /* normal packet */
1204 ip = (IP_t *)(inpkt + ETHER_HDR_SIZE);
1205 len -= ETHER_HDR_SIZE;
1207 } else { /* VLAN packet */
1208 VLAN_Ethernet_t *vet = (VLAN_Ethernet_t *)et;
1211 printf("VLAN packet received\n");
1213 /* too small packet? */
1214 if (len < VLAN_ETHER_HDR_SIZE)
1217 /* if no VLAN active */
1218 if ((ntohs(NetOurVLAN) & VLAN_IDMASK) == VLAN_NONE
1219 #if defined(CONFIG_CMD_CDP)
1225 cti = ntohs(vet->vet_tag);
1226 vlanid = cti & VLAN_IDMASK;
1227 x = ntohs(vet->vet_type);
1229 ip = (IP_t *)(inpkt + VLAN_ETHER_HDR_SIZE);
1230 len -= VLAN_ETHER_HDR_SIZE;
1234 printf("Receive from protocol 0x%x\n", x);
1237 #if defined(CONFIG_CMD_CDP)
1239 CDPHandler((uchar *)ip, len);
1244 if ((myvlanid & VLAN_IDMASK) != VLAN_NONE) {
1245 if (vlanid == VLAN_NONE)
1246 vlanid = (mynvlanid & VLAN_IDMASK);
1248 if (vlanid != (myvlanid & VLAN_IDMASK))
1256 * We have to deal with two types of ARP packets:
1257 * - REQUEST packets will be answered by sending our
1258 * IP address - if we know it.
1259 * - REPLY packates are expected only after we asked
1260 * for the TFTP server's or the gateway's ethernet
1261 * address; so if we receive such a packet, we set
1262 * the server ethernet address
1268 if (len < ARP_HDR_SIZE) {
1269 printf("bad length %d < %d\n", len, ARP_HDR_SIZE);
1272 if (ntohs(arp->ar_hrd) != ARP_ETHER) {
1275 if (ntohs(arp->ar_pro) != PROT_IP) {
1278 if (arp->ar_hln != 6) {
1281 if (arp->ar_pln != 4) {
1285 if (NetOurIP == 0) {
1289 if (NetReadIP(&arp->ar_data[16]) != NetOurIP) {
1293 switch (ntohs(arp->ar_op)) {
1294 case ARPOP_REQUEST: /* reply with our IP address */
1296 puts ("Got ARP REQUEST, return our IP\n");
1299 pkt += NetSetEther(pkt, et->et_src, PROT_ARP);
1300 arp->ar_op = htons(ARPOP_REPLY);
1301 memcpy (&arp->ar_data[10], &arp->ar_data[0], 6);
1302 NetCopyIP(&arp->ar_data[16], &arp->ar_data[6]);
1303 memcpy (&arp->ar_data[ 0], NetOurEther, 6);
1304 NetCopyIP(&arp->ar_data[ 6], &NetOurIP);
1305 (void) eth_send((uchar *)et, (pkt - (uchar *)et) + ARP_HDR_SIZE);
1308 case ARPOP_REPLY: /* arp reply */
1309 /* are we waiting for a reply */
1310 if (!NetArpWaitPacketIP || !NetArpWaitPacketMAC)
1313 printf("Got ARP REPLY, set server/gtwy eth addr (%02x:%02x:%02x:%02x:%02x:%02x)\n",
1314 arp->ar_data[0], arp->ar_data[1],
1315 arp->ar_data[2], arp->ar_data[3],
1316 arp->ar_data[4], arp->ar_data[5]);
1319 tmp = NetReadIP(&arp->ar_data[6]);
1321 /* matched waiting packet's address */
1322 if (tmp == NetArpWaitReplyIP) {
1326 /* save address for later use */
1327 memcpy(NetArpWaitPacketMAC, &arp->ar_data[0], 6);
1329 #ifdef CONFIG_NETCONSOLE
1330 (*packetHandler)(0,0,0,0);
1332 /* modify header, and transmit it */
1333 memcpy(((Ethernet_t *)NetArpWaitTxPacket)->et_dest, NetArpWaitPacketMAC, 6);
1334 (void) eth_send(NetArpWaitTxPacket, NetArpWaitTxPacketSize);
1336 /* no arp request pending now */
1337 NetArpWaitPacketIP = 0;
1338 NetArpWaitTxPacketSize = 0;
1339 NetArpWaitPacketMAC = NULL;
1345 printf("Unexpected ARP opcode 0x%x\n", ntohs(arp->ar_op));
1353 puts ("Got RARP\n");
1356 if (len < ARP_HDR_SIZE) {
1357 printf("bad length %d < %d\n", len, ARP_HDR_SIZE);
1361 if ((ntohs(arp->ar_op) != RARPOP_REPLY) ||
1362 (ntohs(arp->ar_hrd) != ARP_ETHER) ||
1363 (ntohs(arp->ar_pro) != PROT_IP) ||
1364 (arp->ar_hln != 6) || (arp->ar_pln != 4)) {
1366 puts ("invalid RARP header\n");
1368 NetCopyIP(&NetOurIP, &arp->ar_data[16]);
1369 if (NetServerIP == 0)
1370 NetCopyIP(&NetServerIP, &arp->ar_data[ 6]);
1371 memcpy (NetServerEther, &arp->ar_data[ 0], 6);
1373 (*packetHandler)(0,0,0,0);
1381 if (len < IP_HDR_SIZE) {
1382 debug ("len bad %d < %d\n", len, IP_HDR_SIZE);
1385 if (len < ntohs(ip->ip_len)) {
1386 printf("len bad %d < %d\n", len, ntohs(ip->ip_len));
1389 len = ntohs(ip->ip_len);
1391 printf("len=%d, v=%02x\n", len, ip->ip_hl_v & 0xff);
1393 if ((ip->ip_hl_v & 0xf0) != 0x40) {
1396 if (ip->ip_off & htons(0x1fff)) { /* Can't deal w/ fragments */
1399 if (!NetCksumOk((uchar *)ip, IP_HDR_SIZE_NO_UDP / 2)) {
1400 puts ("checksum bad\n");
1403 tmp = NetReadIP(&ip->ip_dst);
1404 if (NetOurIP && tmp != NetOurIP && tmp != 0xFFFFFFFF) {
1405 #ifdef CONFIG_MCAST_TFTP
1406 if (Mcast_addr != tmp)
1411 * watch for ICMP host redirects
1413 * There is no real handler code (yet). We just watch
1414 * for ICMP host redirect messages. In case anybody
1415 * sees these messages: please contact me
1416 * (wd@denx.de), or - even better - send me the
1417 * necessary fixes :-)
1419 * Note: in all cases where I have seen this so far
1420 * it was a problem with the router configuration,
1421 * for instance when a router was configured in the
1422 * BOOTP reply, but the TFTP server was on the same
1423 * subnet. So this is probably a warning that your
1424 * configuration might be wrong. But I'm not really
1425 * sure if there aren't any other situations.
1427 if (ip->ip_p == IPPROTO_ICMP) {
1428 ICMP_t *icmph = (ICMP_t *)&(ip->udp_src);
1430 switch (icmph->type) {
1432 if (icmph->code != ICMP_REDIR_HOST)
1434 puts (" ICMP Host Redirect to ");
1435 print_IPaddr(icmph->un.gateway);
1438 #if defined(CONFIG_CMD_PING)
1439 case ICMP_ECHO_REPLY:
1441 * IP header OK. Pass the packet to the current handler.
1443 /* XXX point to ip packet */
1444 (*packetHandler)((uchar *)ip, 0, 0, 0);
1446 case ICMP_ECHO_REQUEST:
1448 printf ("Got ICMP ECHO REQUEST, return %d bytes \n",
1449 ETHER_HDR_SIZE + len);
1451 memcpy (&et->et_dest[0], &et->et_src[0], 6);
1452 memcpy (&et->et_src[ 0], NetOurEther, 6);
1456 NetCopyIP((void*)&ip->ip_dst, &ip->ip_src);
1457 NetCopyIP((void*)&ip->ip_src, &NetOurIP);
1458 ip->ip_sum = ~NetCksum((uchar *)ip, IP_HDR_SIZE_NO_UDP >> 1);
1460 icmph->type = ICMP_ECHO_REPLY;
1461 icmph->checksum = 0;
1462 icmph->checksum = ~NetCksum((uchar *)icmph,
1463 (len - IP_HDR_SIZE_NO_UDP) >> 1);
1464 (void) eth_send((uchar *)et, ETHER_HDR_SIZE + len);
1470 } else if (ip->ip_p != IPPROTO_UDP) { /* Only UDP packets */
1474 #ifdef CONFIG_UDP_CHECKSUM
1475 if (ip->udp_xsum != 0) {
1481 xsum += (ntohs(ip->udp_len));
1482 xsum += (ntohl(ip->ip_src) >> 16) & 0x0000ffff;
1483 xsum += (ntohl(ip->ip_src) >> 0) & 0x0000ffff;
1484 xsum += (ntohl(ip->ip_dst) >> 16) & 0x0000ffff;
1485 xsum += (ntohl(ip->ip_dst) >> 0) & 0x0000ffff;
1487 sumlen = ntohs(ip->udp_len);
1488 sumptr = (ushort *) &(ip->udp_src);
1490 while (sumlen > 1) {
1493 sumdata = *sumptr++;
1494 xsum += ntohs(sumdata);
1500 sumdata = *(unsigned char *) sumptr;
1501 sumdata = (sumdata << 8) & 0xff00;
1504 while ((xsum >> 16) != 0) {
1505 xsum = (xsum & 0x0000ffff) + ((xsum >> 16) & 0x0000ffff);
1507 if ((xsum != 0x00000000) && (xsum != 0x0000ffff)) {
1508 printf(" UDP wrong checksum %08x %08x\n", xsum, ntohs(ip->udp_xsum));
1515 #ifdef CONFIG_NETCONSOLE
1516 nc_input_packet((uchar *)ip +IP_HDR_SIZE,
1519 ntohs(ip->udp_len) - 8);
1522 * IP header OK. Pass the packet to the current handler.
1524 (*packetHandler)((uchar *)ip +IP_HDR_SIZE,
1527 ntohs(ip->udp_len) - 8);
1533 /**********************************************************************/
1535 static int net_check_prereq (proto_t protocol)
1539 #if defined(CONFIG_CMD_PING)
1541 if (NetPingIP == 0) {
1542 puts ("*** ERROR: ping address not given\n");
1547 #if defined(CONFIG_CMD_SNTP)
1549 if (NetNtpServerIP == 0) {
1550 puts ("*** ERROR: NTP server address not given\n");
1555 #if defined(CONFIG_CMD_NFS)
1560 if (NetServerIP == 0) {
1561 puts ("*** ERROR: `serverip' not set\n");
1564 #if defined(CONFIG_CMD_PING) || defined(CONFIG_CMD_SNTP)
1568 if (NetOurIP == 0) {
1569 puts ("*** ERROR: `ipaddr' not set\n");
1578 if (memcmp (NetOurEther, "\0\0\0\0\0\0", 6) == 0) {
1579 #ifdef CONFIG_NET_MULTI
1580 extern int eth_get_dev_index (void);
1581 int num = eth_get_dev_index ();
1585 puts ("*** ERROR: No ethernet found.\n");
1588 puts ("*** ERROR: `ethaddr' not set\n");
1591 printf ("*** ERROR: `eth%daddr' not set\n",
1599 puts ("*** ERROR: `ethaddr' not set\n");
1607 return (0); /* OK */
1609 /**********************************************************************/
1612 NetCksumOk(uchar * ptr, int len)
1614 return !((NetCksum(ptr, len) + 1) & 0xfffe);
1619 NetCksum(uchar * ptr, int len)
1622 ushort *p = (ushort *)ptr;
1627 xsum = (xsum & 0xffff) + (xsum >> 16);
1628 xsum = (xsum & 0xffff) + (xsum >> 16);
1629 return (xsum & 0xffff);
1637 myvlanid = ntohs(NetOurVLAN);
1638 if (myvlanid == (ushort)-1)
1639 myvlanid = VLAN_NONE;
1641 return ((myvlanid & VLAN_IDMASK) == VLAN_NONE) ? ETHER_HDR_SIZE : VLAN_ETHER_HDR_SIZE;
1645 NetSetEther(volatile uchar * xet, uchar * addr, uint prot)
1647 Ethernet_t *et = (Ethernet_t *)xet;
1650 myvlanid = ntohs(NetOurVLAN);
1651 if (myvlanid == (ushort)-1)
1652 myvlanid = VLAN_NONE;
1654 memcpy (et->et_dest, addr, 6);
1655 memcpy (et->et_src, NetOurEther, 6);
1656 if ((myvlanid & VLAN_IDMASK) == VLAN_NONE) {
1657 et->et_protlen = htons(prot);
1658 return ETHER_HDR_SIZE;
1660 VLAN_Ethernet_t *vet = (VLAN_Ethernet_t *)xet;
1662 vet->vet_vlan_type = htons(PROT_VLAN);
1663 vet->vet_tag = htons((0 << 5) | (myvlanid & VLAN_IDMASK));
1664 vet->vet_type = htons(prot);
1665 return VLAN_ETHER_HDR_SIZE;
1670 NetSetIP(volatile uchar * xip, IPaddr_t dest, int dport, int sport, int len)
1672 volatile IP_t *ip = (IP_t *)xip;
1675 * If the data is an odd number of bytes, zero the
1676 * byte after the last byte so that the checksum
1680 xip[IP_HDR_SIZE + len] = 0;
1683 * Construct an IP and UDP header.
1684 * (need to set no fragment bit - XXX)
1686 ip->ip_hl_v = 0x45; /* IP_HDR_SIZE / 4 (not including UDP) */
1688 ip->ip_len = htons(IP_HDR_SIZE + len);
1689 ip->ip_id = htons(NetIPID++);
1690 ip->ip_off = htons(0x4000); /* No fragmentation */
1692 ip->ip_p = 17; /* UDP */
1694 NetCopyIP((void*)&ip->ip_src, &NetOurIP); /* already in network byte order */
1695 NetCopyIP((void*)&ip->ip_dst, &dest); /* - "" - */
1696 ip->udp_src = htons(sport);
1697 ip->udp_dst = htons(dport);
1698 ip->udp_len = htons(8 + len);
1700 ip->ip_sum = ~NetCksum((uchar *)ip, IP_HDR_SIZE_NO_UDP / 2);
1703 void copy_filename (char *dst, char *src, int size)
1705 if (*src && (*src == '"')) {
1710 while ((--size > 0) && *src && (*src != '"')) {
1718 void ip_to_string (IPaddr_t x, char *s)
1721 sprintf (s, "%d.%d.%d.%d",
1722 (int) ((x >> 24) & 0xff),
1723 (int) ((x >> 16) & 0xff),
1724 (int) ((x >> 8) & 0xff), (int) ((x >> 0) & 0xff)
1728 IPaddr_t string_to_ip(char *s)
1737 for (addr=0, i=0; i<4; ++i) {
1738 ulong val = s ? simple_strtoul(s, &e, 10) : 0;
1740 addr |= (val & 0xFF);
1746 return (htonl(addr));
1749 void VLAN_to_string(ushort x, char *s)
1753 if (x == (ushort)-1)
1759 sprintf(s, "%d", x & VLAN_IDMASK);
1762 ushort string_to_VLAN(char *s)
1767 return htons(VLAN_NONE);
1769 if (*s < '0' || *s > '9')
1772 id = (ushort)simple_strtoul(s, NULL, 10);
1777 void print_IPaddr (IPaddr_t x)
1781 ip_to_string (x, tmp);
1786 IPaddr_t getenv_IPaddr (char *var)
1788 return (string_to_ip(getenv(var)));
1791 ushort getenv_VLAN(char *var)
1793 return (string_to_VLAN(getenv(var)));