2 * Copied from Linux Monitor (LiMon) - Networking.
4 * Copyright 1994 - 2000 Neil Russell.
6 * Copyright 2000 Roland Borde
7 * Copyright 2000 Paolo Scaffardi
14 * The user interface supports commands for BOOTP, RARP, and TFTP.
15 * Also, we support ARP internally. Depending on available data,
16 * these interact as follows:
20 * Prerequisites: - own ethernet address
21 * We want: - own IP address
22 * - TFTP server IP address
28 * Prerequisites: - own ethernet address
29 * We want: - own IP address
30 * - TFTP server IP address
35 * Prerequisites: - own ethernet address
37 * - TFTP server IP address
38 * We want: - TFTP server ethernet address
43 * Prerequisites: - own ethernet address
44 * We want: - IP, Netmask, ServerIP, Gateway IP
45 * - bootfilename, lease time
50 * Prerequisites: - own ethernet address
52 * - TFTP server IP address
53 * - TFTP server ethernet address
54 * - name of bootfile (if unknown, we use a default name
55 * derived from our own IP address)
56 * We want: - load the boot file
61 * Prerequisites: - own ethernet address
63 * - name of bootfile (if unknown, we use a default name
64 * derived from our own IP address)
65 * We want: - load the boot file
78 #ifdef CONFIG_STATUS_LED
79 #include <status_led.h>
83 #if (CONFIG_COMMANDS & CFG_CMD_NET)
85 #define ARP_TIMEOUT 5 /* Seconds before trying ARP again */
86 #ifndef CONFIG_NET_RETRY_COUNT
87 # define ARP_TIMEOUT_COUNT 5 /* # of timeouts before giving up */
89 # define ARP_TIMEOUT_COUNT (CONFIG_NET_RETRY_COUNT)
96 /** BOOTP EXTENTIONS **/
98 IPaddr_t NetOurSubnetMask=0; /* Our subnet mask (0=unknown) */
99 IPaddr_t NetOurGatewayIP=0; /* Our gateways IP address */
100 IPaddr_t NetOurDNSIP=0; /* Our DNS IP address */
101 #if (CONFIG_BOOTP_MASK & CONFIG_BOOTP_DNS2)
102 IPaddr_t NetOurDNS2IP=0; /* Our 2nd DNS IP address */
104 char NetOurNISDomain[32]={0,}; /* Our NIS domain */
105 char NetOurHostName[32]={0,}; /* Our hostname */
106 char NetOurRootPath[64]={0,}; /* Our bootpath */
107 ushort NetBootFileSize=0; /* Our bootfile size in blocks */
109 /** END OF BOOTP EXTENTIONS **/
111 ulong NetBootFileXferSize; /* The actual transferred size of the bootfile (in bytes) */
112 uchar NetOurEther[6]; /* Our ethernet address */
113 uchar NetServerEther[6] = /* Boot server enet address */
114 { 0, 0, 0, 0, 0, 0 };
115 IPaddr_t NetOurIP; /* Our IP addr (0 = unknown) */
116 IPaddr_t NetServerIP; /* Our IP addr (0 = unknown) */
117 volatile uchar *NetRxPkt; /* Current receive packet */
118 int NetRxPktLen; /* Current rx packet length */
119 unsigned NetIPID; /* IP packet ID */
120 uchar NetBcastAddr[6] = /* Ethernet bcast address */
121 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
122 uchar NetEtherNullAddr[6] =
123 { 0, 0, 0, 0, 0, 0 };
124 #if (CONFIG_COMMANDS & CFG_CMD_CDP)
125 uchar NetCDPAddr[6] = /* Ethernet bcast address */
126 { 0x01, 0x00, 0x0c, 0xcc, 0xcc, 0xcc };
128 int NetState; /* Network loop state */
129 #ifdef CONFIG_NET_MULTI
130 int NetRestartWrap = 0; /* Tried all network devices */
131 static int NetRestarted = 0; /* Network loop restarted */
132 static int NetDevExists = 0; /* At least one device configured */
135 /* XXX in both little & big endian machines 0xFFFF == ntohs(-1) */
136 ushort NetOurVLAN = 0xFFFF; /* default is without VLAN */
137 ushort NetOurNativeVLAN = 0xFFFF; /* ditto */
139 char BootFile[128]; /* Boot File name */
141 #if (CONFIG_COMMANDS & CFG_CMD_PING)
142 IPaddr_t NetPingIP; /* the ip address to ping */
144 static void PingStart(void);
147 #if (CONFIG_COMMANDS & CFG_CMD_CDP)
148 static void CDPStart(void);
151 volatile uchar PktBuf[(PKTBUFSRX+1) * PKTSIZE_ALIGN + PKTALIGN];
153 volatile uchar *NetRxPackets[PKTBUFSRX]; /* Receive packets */
155 static rxhand_f *packetHandler; /* Current RX packet handler */
156 static thand_f *timeHandler; /* Current timeout handler */
157 static ulong timeStart; /* Time base value */
158 static ulong timeDelta; /* Current timeout value */
159 volatile uchar *NetTxPacket = 0; /* THE transmit packet */
161 static int net_check_prereq (proto_t protocol);
163 /**********************************************************************/
165 IPaddr_t NetArpWaitPacketIP;
166 IPaddr_t NetArpWaitReplyIP;
167 uchar *NetArpWaitPacketMAC; /* MAC address of waiting packet's destination */
168 uchar *NetArpWaitTxPacket; /* THE transmit packet */
169 int NetArpWaitTxPacketSize;
170 uchar NetArpWaitPacketBuf[PKTSIZE_ALIGN + PKTALIGN];
171 ulong NetArpWaitTimerStart;
174 void ArpRequest (void)
181 printf ("ARP broadcast %d\n", NetArpWaitTry);
185 pkt += NetSetEther (pkt, NetBcastAddr, PROT_ARP);
189 arp->ar_hrd = htons (ARP_ETHER);
190 arp->ar_pro = htons (PROT_IP);
193 arp->ar_op = htons (ARPOP_REQUEST);
195 memcpy (&arp->ar_data[0], NetOurEther, 6); /* source ET addr */
196 NetWriteIP ((uchar *) & arp->ar_data[6], NetOurIP); /* source IP addr */
197 for (i = 10; i < 16; ++i) {
198 arp->ar_data[i] = 0; /* dest ET addr = 0 */
201 if ((NetArpWaitPacketIP & NetOurSubnetMask) !=
202 (NetOurIP & NetOurSubnetMask)) {
203 if (NetOurGatewayIP == 0) {
204 puts ("## Warning: gatewayip needed but not set\n");
206 NetArpWaitReplyIP = NetOurGatewayIP;
208 NetArpWaitReplyIP = NetArpWaitPacketIP;
211 NetWriteIP ((uchar *) & arp->ar_data[16], NetArpWaitReplyIP);
212 (void) eth_send (NetTxPacket, (pkt - NetTxPacket) + ARP_HDR_SIZE);
215 void ArpTimeoutCheck(void)
219 if (!NetArpWaitPacketIP)
224 /* check for arp timeout */
225 if ((t - NetArpWaitTimerStart) > ARP_TIMEOUT * CFG_HZ) {
228 if (NetArpWaitTry >= ARP_TIMEOUT_COUNT) {
229 puts ("\nARP Retry count exceeded; starting again\n");
233 NetArpWaitTimerStart = t;
239 /**********************************************************************/
241 * Main network processing loop.
245 NetLoop(proto_t protocol)
247 DECLARE_GLOBAL_DATA_PTR;
251 #ifdef CONFIG_NET_MULTI
256 /* XXX problem with bss workaround */
257 NetArpWaitPacketMAC = NULL;
258 NetArpWaitTxPacket = NULL;
259 NetArpWaitPacketIP = 0;
260 NetArpWaitReplyIP = 0;
261 NetArpWaitTxPacket = NULL;
267 * Setup packet buffers, aligned correctly.
269 NetTxPacket = &PktBuf[0] + (PKTALIGN - 1);
270 NetTxPacket -= (ulong)NetTxPacket % PKTALIGN;
271 for (i = 0; i < PKTBUFSRX; i++) {
272 NetRxPackets[i] = NetTxPacket + (i+1)*PKTSIZE_ALIGN;
276 if (!NetArpWaitTxPacket) {
277 NetArpWaitTxPacket = &NetArpWaitPacketBuf[0] + (PKTALIGN - 1);
278 NetArpWaitTxPacket -= (ulong)NetArpWaitTxPacket % PKTALIGN;
279 NetArpWaitTxPacketSize = 0;
283 #ifdef CONFIG_NET_MULTI
286 if (eth_init(bd) < 0)
290 #ifdef CONFIG_NET_MULTI
291 memcpy (NetOurEther, eth_get_dev()->enetaddr, 6);
293 memcpy (NetOurEther, bd->bi_enetaddr, 6);
296 NetState = NETLOOP_CONTINUE;
299 * Start the ball rolling with the given start function. From
300 * here on, this code is a state machine driven by received
301 * packets and timer events.
305 #if (CONFIG_COMMANDS & CFG_CMD_NFS)
308 #if (CONFIG_COMMANDS & CFG_CMD_PING)
312 NetCopyIP(&NetOurIP, &bd->bi_ip_addr);
313 NetOurGatewayIP = getenv_IPaddr ("gatewayip");
314 NetOurSubnetMask= getenv_IPaddr ("netmask");
315 NetOurVLAN = getenv_VLAN("vlan");
316 NetOurNativeVLAN = getenv_VLAN("nvlan");
319 #if (CONFIG_COMMANDS & CFG_CMD_NFS)
323 NetServerIP = getenv_IPaddr ("serverip");
325 #if (CONFIG_COMMANDS & CFG_CMD_PING)
338 * initialize our IP addr to 0 in order to accept ANY
339 * IP addr assigned to us by the BOOTP / RARP server
342 NetServerIP = getenv_IPaddr ("serverip");
343 NetOurVLAN = getenv_VLAN("vlan"); /* VLANs must be read */
344 NetOurNativeVLAN = getenv_VLAN("nvlan");
346 NetOurVLAN = getenv_VLAN("vlan"); /* VLANs must be read */
347 NetOurNativeVLAN = getenv_VLAN("nvlan");
353 switch (net_check_prereq (protocol)) {
355 /* network not configured */
358 #ifdef CONFIG_NET_MULTI
360 /* network device not configured */
362 #endif /* CONFIG_NET_MULTI */
365 #ifdef CONFIG_NET_MULTI
370 /* always use ARP to get server ethernet address */
374 #if (CONFIG_COMMANDS & CFG_CMD_DHCP)
376 /* Start with a clean slate... */
378 NetServerIP = getenv_IPaddr ("serverip");
379 DhcpRequest(); /* Basically same as BOOTP */
381 #endif /* CFG_CMD_DHCP */
392 #if (CONFIG_COMMANDS & CFG_CMD_PING)
397 #if (CONFIG_COMMANDS & CFG_CMD_NFS)
402 #if (CONFIG_COMMANDS & CFG_CMD_CDP)
411 NetBootFileXferSize = 0;
415 #if defined(CONFIG_MII) || (CONFIG_COMMANDS & CFG_CMD_MII)
416 #if defined(CFG_FAULT_ECHO_LINK_DOWN) && defined(CONFIG_STATUS_LED) && defined(STATUS_LED_RED)
418 * Echo the inverted link state to the fault LED.
420 if(miiphy_link(CFG_FAULT_MII_ADDR)) {
421 status_led_set (STATUS_LED_RED, STATUS_LED_OFF);
423 status_led_set (STATUS_LED_RED, STATUS_LED_ON);
425 #endif /* CFG_FAULT_ECHO_LINK_DOWN, ... */
426 #endif /* CONFIG_MII, ... */
429 * Main packet reception loop. Loop receiving packets until
430 * someone sets `NetQuit'.
434 #ifdef CONFIG_SHOW_ACTIVITY
436 extern void show_activity(int arg);
441 * Check the ethernet for a new packet. The ethernet
442 * receive routine will process it.
447 * Abort if ctrl-c was pressed.
458 * Check for a timeout, and run the timeout handler
461 if (timeHandler && ((get_timer(0) - timeStart) > timeDelta)) {
464 #if defined(CONFIG_MII) || (CONFIG_COMMANDS & CFG_CMD_MII)
465 #if defined(CFG_FAULT_ECHO_LINK_DOWN) && defined(CONFIG_STATUS_LED) && defined(STATUS_LED_RED)
467 * Echo the inverted link state to the fault LED.
469 if(miiphy_link(CFG_FAULT_MII_ADDR)) {
470 status_led_set (STATUS_LED_RED, STATUS_LED_OFF);
472 status_led_set (STATUS_LED_RED, STATUS_LED_ON);
474 #endif /* CFG_FAULT_ECHO_LINK_DOWN, ... */
475 #endif /* CONFIG_MII, ... */
477 timeHandler = (thand_f *)0;
484 case NETLOOP_RESTART:
485 #ifdef CONFIG_NET_MULTI
490 case NETLOOP_SUCCESS:
491 if (NetBootFileXferSize > 0) {
493 printf("Bytes transferred = %ld (%lx hex)\n",
495 NetBootFileXferSize);
496 sprintf(buf, "%lx", NetBootFileXferSize);
497 setenv("filesize", buf);
499 sprintf(buf, "%lX", (unsigned long)load_addr);
500 setenv("fileaddr", buf);
503 return NetBootFileXferSize;
511 /**********************************************************************/
514 startAgainTimeout(void)
516 NetState = NETLOOP_RESTART;
520 startAgainHandler(uchar * pkt, unsigned dest, unsigned src, unsigned len)
522 /* Totally ignore the packet */
525 void NetStartAgain (void)
527 #ifdef CONFIG_NET_MULTI
528 DECLARE_GLOBAL_DATA_PTR;
531 int noretry = 0, once = 0;
533 if ((nretry = getenv ("netretry")) != NULL) {
534 noretry = (strcmp (nretry, "no") == 0);
535 once = (strcmp (nretry, "once") == 0);
539 NetState = NETLOOP_FAIL;
542 #ifndef CONFIG_NET_MULTI
543 NetSetTimeout (10 * CFG_HZ, startAgainTimeout);
544 NetSetHandler (startAgainHandler);
545 #else /* !CONFIG_NET_MULTI*/
547 eth_try_another (!NetRestarted);
549 if (NetRestartWrap) {
551 if (NetDevExists && !once) {
552 NetSetTimeout (10 * CFG_HZ, startAgainTimeout);
553 NetSetHandler (startAgainHandler);
555 NetState = NETLOOP_FAIL;
558 NetState = NETLOOP_RESTART;
560 #endif /* CONFIG_NET_MULTI */
563 /**********************************************************************/
569 NetSetHandler(rxhand_f * f)
576 NetSetTimeout(int iv, thand_f * f)
579 timeHandler = (thand_f *)0;
582 timeStart = get_timer(0);
589 NetSendPacket(volatile uchar * pkt, int len)
591 (void) eth_send(pkt, len);
595 NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport, int sport, int len)
599 /* convert to new style broadcast */
603 /* if broadcast, make the ether address a broadcast and don't do ARP */
604 if (dest == 0xFFFFFFFF)
605 ether = NetBcastAddr;
607 /* if MAC address was not discovered yet, save the packet and do an ARP request */
608 if (memcmp(ether, NetEtherNullAddr, 6) == 0) {
611 printf("sending ARP for %08lx\n", dest);
613 NetArpWaitPacketIP = dest;
614 NetArpWaitPacketMAC = ether;
616 pkt = NetArpWaitTxPacket;
617 pkt += NetSetEther (pkt, NetArpWaitPacketMAC, PROT_IP);
619 NetSetIP (pkt, dest, dport, sport, len);
620 memcpy(pkt + IP_HDR_SIZE, (uchar *)NetTxPacket + (pkt - (uchar *)NetArpWaitTxPacket) + IP_HDR_SIZE, len);
622 /* size of the waiting packet */
623 NetArpWaitTxPacketSize = (pkt - NetArpWaitTxPacket) + IP_HDR_SIZE + len;
625 /* and do the ARP request */
627 NetArpWaitTimerStart = get_timer(0);
629 return 1; /* waiting */
633 printf("sending UDP to %08lx/%02x:%02x:%02x:%02x:%02x:%02x\n",
634 dest, ether[0], ether[1], ether[2], ether[3], ether[4], ether[5]);
637 pkt = (uchar *)NetTxPacket;
638 pkt += NetSetEther (pkt, ether, PROT_IP);
639 NetSetIP (pkt, dest, dport, sport, len);
640 (void) eth_send(NetTxPacket, (pkt - NetTxPacket) + IP_HDR_SIZE + len);
642 return 0; /* transmitted */
645 #if (CONFIG_COMMANDS & CFG_CMD_PING)
646 static ushort PingSeqNo;
655 /* XXX always send arp request */
657 memcpy(mac, NetEtherNullAddr, 6);
660 printf("sending ARP for %08lx\n", NetPingIP);
663 NetArpWaitPacketIP = NetPingIP;
664 NetArpWaitPacketMAC = mac;
666 pkt = NetArpWaitTxPacket;
667 pkt += NetSetEther(pkt, mac, PROT_IP);
669 ip = (volatile IP_t *)pkt;
672 * Construct an IP and ICMP header. (need to set no fragment bit - XXX)
674 ip->ip_hl_v = 0x45; /* IP_HDR_SIZE / 4 (not including UDP) */
676 ip->ip_len = htons(IP_HDR_SIZE_NO_UDP + 8);
677 ip->ip_id = htons(NetIPID++);
678 ip->ip_off = htons(0x4000); /* No fragmentation */
680 ip->ip_p = 0x01; /* ICMP */
682 NetCopyIP((void*)&ip->ip_src, &NetOurIP); /* already in network byte order */
683 NetCopyIP((void*)&ip->ip_dst, &NetPingIP); /* - "" - */
684 ip->ip_sum = ~NetCksum((uchar *)ip, IP_HDR_SIZE_NO_UDP / 2);
686 s = &ip->udp_src; /* XXX ICMP starts here */
687 s[0] = htons(0x0800); /* echo-request, code */
688 s[1] = 0; /* checksum */
689 s[2] = 0; /* identifier */
690 s[3] = htons(PingSeqNo++); /* sequence number */
691 s[1] = ~NetCksum((uchar *)s, 8/2);
693 /* size of the waiting packet */
694 NetArpWaitTxPacketSize = (pkt - NetArpWaitTxPacket) + IP_HDR_SIZE_NO_UDP + 8;
696 /* and do the ARP request */
698 NetArpWaitTimerStart = get_timer(0);
700 return 1; /* waiting */
707 NetState = NETLOOP_FAIL; /* we did not get the reply */
711 PingHandler (uchar * pkt, unsigned dest, unsigned src, unsigned len)
714 volatile IP_t *ip = (volatile IP_t *)pkt;
716 tmp = NetReadIP((void *)&ip->ip_src);
717 if (tmp != NetPingIP)
720 NetState = NETLOOP_SUCCESS;
723 static void PingStart(void)
725 #if defined(CONFIG_NET_MULTI)
726 printf ("Using %s device\n", eth_get_name());
727 #endif /* CONFIG_NET_MULTI */
728 NetSetTimeout (10 * CFG_HZ, PingTimeout);
729 NetSetHandler (PingHandler);
733 #endif /* CFG_CMD_PING */
735 #if (CONFIG_COMMANDS & CFG_CMD_CDP)
737 #define CDP_DEVICE_ID_TLV 0x0001
738 #define CDP_ADDRESS_TLV 0x0002
739 #define CDP_PORT_ID_TLV 0x0003
740 #define CDP_CAPABILITIES_TLV 0x0004
741 #define CDP_VERSION_TLV 0x0005
742 #define CDP_PLATFORM_TLV 0x0006
743 #define CDP_NATIVE_VLAN_TLV 0x000a
744 #define CDP_APPLIANCE_VLAN_TLV 0x000e
745 #define CDP_TRIGGER_TLV 0x000f
746 #define CDP_POWER_CONSUMPTION_TLV 0x0010
747 #define CDP_SYSNAME_TLV 0x0014
748 #define CDP_SYSOBJECT_TLV 0x0015
749 #define CDP_MANAGEMENT_ADDRESS_TLV 0x0016
751 #define CDP_TIMEOUT (CFG_HZ/4) /* one packet every 250ms */
756 ushort CDPNativeVLAN;
757 ushort CDPApplianceVLAN;
759 static const uchar CDP_SNAP_hdr[8] = { 0xAA, 0xAA, 0x03, 0x00, 0x00, 0x0C, 0x20, 0x00 };
761 static ushort CDP_compute_csum(const uchar *buff, ushort len)
769 odd = 1 & (ulong)buff;
776 result += *((const ushort *)buff)++;
777 if (result & 0x80000000)
778 result = (result & 0xFFFF) + (result >> 16);
782 leftover = (signed short)(*(const signed char *)buff);
783 /* * XXX CISCO SUCKS big time! (and blows too) */
784 result = (result & 0xffff0000) | ((result + leftover) & 0x0000ffff);
787 result = (result & 0xFFFF) + (result >> 16);
790 result = ((result >> 8) & 0xff) | ((result & 0xff) << 8);
793 /* add up 16-bit and 17-bit words for 17+c bits */
794 result = (result & 0xffff) + (result >> 16);
795 /* add up 16-bit and 2-bit for 16+c bit */
796 result = (result & 0xffff) + (result >> 16);
798 result = (result & 0xffff) + (result >> 16);
801 csum = ~(ushort)result;
803 /* run time endian detection */
804 if (csum != htons(csum)) /* little endian */
810 int CDPSendTrigger(void)
818 #if defined(CONFIG_CDP_DEVICE_ID) || defined(CONFIG_CDP_PORT_ID) || \
819 defined(CONFIG_CDP_VERSION) || defined(CONFIG_CDP_PLATFORM)
824 et = (Ethernet_t *)pkt;
826 /* NOTE: trigger sent not on any VLAN */
828 /* form ethernet header */
829 memcpy(et->et_dest, NetCDPAddr, 6);
830 memcpy(et->et_src, NetOurEther, 6);
832 pkt += ETHER_HDR_SIZE;
835 memcpy((uchar *)pkt, CDP_SNAP_hdr, sizeof(CDP_SNAP_hdr));
836 pkt += sizeof(CDP_SNAP_hdr);
839 *pkt++ = 0x02; /* CDP version 2 */
840 *pkt++ = 180; /* TTL */
841 s = (volatile ushort *)pkt;
843 *s++ = htons(0); /* checksum (0 for later calculation) */
846 #ifdef CONFIG_CDP_DEVICE_ID
847 *s++ = htons(CDP_DEVICE_ID_TLV);
848 *s++ = htons(CONFIG_CDP_DEVICE_ID);
849 memset(buf, 0, sizeof(buf));
850 sprintf(buf, CONFIG_CDP_DEVICE_ID_PREFIX "%02X%02X%02X%02X%02X%02X",
851 NetOurEther[0] & 0xff, NetOurEther[1] & 0xff,
852 NetOurEther[2] & 0xff, NetOurEther[3] & 0xff,
853 NetOurEther[4] & 0xff, NetOurEther[5] & 0xff);
854 memcpy((uchar *)s, buf, 16);
858 #ifdef CONFIG_CDP_PORT_ID
859 *s++ = htons(CDP_PORT_ID_TLV);
860 memset(buf, 0, sizeof(buf));
861 sprintf(buf, CONFIG_CDP_PORT_ID, eth_get_dev_index());
863 if (len & 1) /* make it even */
865 *s++ = htons(len + 4);
866 memcpy((uchar *)s, buf, len);
870 #ifdef CONFIG_CDP_CAPABILITIES
871 *s++ = htons(CDP_CAPABILITIES_TLV);
873 *(ulong *)s = htonl(CONFIG_CDP_CAPABILITIES);
877 #ifdef CONFIG_CDP_VERSION
878 *s++ = htons(CDP_VERSION_TLV);
879 memset(buf, 0, sizeof(buf));
880 strcpy(buf, CONFIG_CDP_VERSION);
882 if (len & 1) /* make it even */
884 *s++ = htons(len + 4);
885 memcpy((uchar *)s, buf, len);
889 #ifdef CONFIG_CDP_PLATFORM
890 *s++ = htons(CDP_PLATFORM_TLV);
891 memset(buf, 0, sizeof(buf));
892 strcpy(buf, CONFIG_CDP_PLATFORM);
894 if (len & 1) /* make it even */
896 *s++ = htons(len + 4);
897 memcpy((uchar *)s, buf, len);
901 #ifdef CONFIG_CDP_TRIGGER
902 *s++ = htons(CDP_TRIGGER_TLV);
904 *(ulong *)s = htonl(CONFIG_CDP_TRIGGER);
908 #ifdef CONFIG_CDP_POWER_CONSUMPTION
909 *s++ = htons(CDP_POWER_CONSUMPTION_TLV);
911 *s++ = htons(CONFIG_CDP_POWER_CONSUMPTION);
914 /* length of ethernet packet */
915 len = (uchar *)s - ((uchar *)NetTxPacket + ETHER_HDR_SIZE);
916 et->et_protlen = htons(len);
918 len = ETHER_HDR_SIZE + sizeof(CDP_SNAP_hdr);
919 chksum = CDP_compute_csum((uchar *)NetTxPacket + len, (uchar *)s - (NetTxPacket + len));
924 (void) eth_send(NetTxPacket, (uchar *)s - NetTxPacket);
934 NetSetTimeout (CDP_TIMEOUT, CDPTimeout);
939 /* if not OK try again */
943 NetState = NETLOOP_SUCCESS;
947 CDPDummyHandler (uchar * pkt, unsigned dest, unsigned src, unsigned len)
953 CDPHandler(const uchar * pkt, unsigned len)
962 if (len < sizeof(CDP_SNAP_hdr) + 4)
965 /* check for valid CDP SNAP header */
966 if (memcmp(pkt, CDP_SNAP_hdr, sizeof(CDP_SNAP_hdr)) != 0)
969 pkt += sizeof(CDP_SNAP_hdr);
970 len -= sizeof(CDP_SNAP_hdr);
972 /* Version of CDP protocol must be >= 2 and TTL != 0 */
973 if (pkt[0] < 0x02 || pkt[1] == 0)
976 /* if version is greater than 0x02 maybe we'll have a problem; output a warning */
978 printf("** WARNING: CDP packet received with a protocol version %d > 2\n",
981 if (CDP_compute_csum(pkt, len) != 0)
993 ss = (const ushort *)pkt;
1003 ss += 2; /* point ss to the data of the TLV */
1007 case CDP_DEVICE_ID_TLV:
1009 case CDP_ADDRESS_TLV:
1011 case CDP_PORT_ID_TLV:
1013 case CDP_CAPABILITIES_TLV:
1015 case CDP_VERSION_TLV:
1017 case CDP_PLATFORM_TLV:
1019 case CDP_NATIVE_VLAN_TLV:
1022 case CDP_APPLIANCE_VLAN_TLV:
1023 t = (const uchar *)ss;
1029 ss = (const ushort *)(t + 1);
1031 #ifdef CONFIG_CDP_APPLIANCE_VLAN_TYPE
1032 if (applid == CONFIG_CDP_APPLIANCE_VLAN_TYPE)
1035 vlan = ntohs(*ss); /* XXX will this work; dunno */
1040 case CDP_TRIGGER_TLV:
1042 case CDP_POWER_CONSUMPTION_TLV:
1044 case CDP_SYSNAME_TLV:
1046 case CDP_SYSOBJECT_TLV:
1048 case CDP_MANAGEMENT_ADDRESS_TLV:
1053 CDPApplianceVLAN = vlan;
1054 CDPNativeVLAN = nvlan;
1060 printf("** CDP packet is too short\n");
1064 static void CDPStart(void)
1066 #if defined(CONFIG_NET_MULTI)
1067 printf ("Using %s device\n", eth_get_name());
1072 CDPNativeVLAN = htons(-1);
1073 CDPApplianceVLAN = htons(-1);
1075 NetSetTimeout (CDP_TIMEOUT, CDPTimeout);
1076 NetSetHandler (CDPDummyHandler);
1080 #endif /* CFG_CMD_CDP */
1084 NetReceive(volatile uchar * inpkt, int len)
1092 #if (CONFIG_COMMANDS & CFG_CMD_CDP)
1095 ushort cti = 0, vlanid = VLAN_NONE, myvlanid, mynvlanid;
1098 printf("packet received\n");
1103 et = (Ethernet_t *)inpkt;
1105 /* too small packet? */
1106 if (len < ETHER_HDR_SIZE)
1109 #if (CONFIG_COMMANDS & CFG_CMD_CDP)
1110 /* keep track if packet is CDP */
1111 iscdp = memcmp(et->et_dest, NetCDPAddr, 6) == 0;
1114 myvlanid = ntohs(NetOurVLAN);
1115 if (myvlanid == (ushort)-1)
1116 myvlanid = VLAN_NONE;
1117 mynvlanid = ntohs(NetOurNativeVLAN);
1118 if (mynvlanid == (ushort)-1)
1119 mynvlanid = VLAN_NONE;
1121 x = ntohs(et->et_protlen);
1124 printf("packet received\n");
1129 * Got a 802 packet. Check the other protocol field.
1131 x = ntohs(et->et_prot);
1133 ip = (IP_t *)(inpkt + E802_HDR_SIZE);
1134 len -= E802_HDR_SIZE;
1136 } else if (x != PROT_VLAN) { /* normal packet */
1137 ip = (IP_t *)(inpkt + ETHER_HDR_SIZE);
1138 len -= ETHER_HDR_SIZE;
1140 } else { /* VLAN packet */
1141 VLAN_Ethernet_t *vet = (VLAN_Ethernet_t *)et;
1144 printf("VLAN packet received\n");
1146 /* too small packet? */
1147 if (len < VLAN_ETHER_HDR_SIZE)
1150 /* if no VLAN active */
1151 if ((ntohs(NetOurVLAN) & VLAN_IDMASK) == VLAN_NONE
1152 #if (CONFIG_COMMANDS & CFG_CMD_CDP)
1158 cti = ntohs(vet->vet_tag);
1159 vlanid = cti & VLAN_IDMASK;
1160 x = ntohs(vet->vet_type);
1162 ip = (IP_t *)(inpkt + VLAN_ETHER_HDR_SIZE);
1163 len -= VLAN_ETHER_HDR_SIZE;
1167 printf("Receive from protocol 0x%x\n", x);
1170 #if (CONFIG_COMMANDS & CFG_CMD_CDP)
1172 CDPHandler((uchar *)ip, len);
1177 if ((myvlanid & VLAN_IDMASK) != VLAN_NONE) {
1178 if (vlanid == VLAN_NONE)
1179 vlanid = (mynvlanid & VLAN_IDMASK);
1181 if (vlanid != (myvlanid & VLAN_IDMASK))
1189 * We have to deal with two types of ARP packets:
1190 * - REQUEST packets will be answered by sending our
1191 * IP address - if we know it.
1192 * - REPLY packates are expected only after we asked
1193 * for the TFTP server's or the gateway's ethernet
1194 * address; so if we receive such a packet, we set
1195 * the server ethernet address
1201 if (len < ARP_HDR_SIZE) {
1202 printf("bad length %d < %d\n", len, ARP_HDR_SIZE);
1205 if (ntohs(arp->ar_hrd) != ARP_ETHER) {
1208 if (ntohs(arp->ar_pro) != PROT_IP) {
1211 if (arp->ar_hln != 6) {
1214 if (arp->ar_pln != 4) {
1218 if (NetOurIP == 0) {
1222 if (NetReadIP(&arp->ar_data[16]) != NetOurIP) {
1226 switch (ntohs(arp->ar_op)) {
1227 case ARPOP_REQUEST: /* reply with our IP address */
1229 puts ("Got ARP REQUEST, return our IP\n");
1232 pkt += NetSetEther(pkt, et->et_src, PROT_ARP);
1233 arp->ar_op = htons(ARPOP_REPLY);
1234 memcpy (&arp->ar_data[10], &arp->ar_data[0], 6);
1235 NetCopyIP(&arp->ar_data[16], &arp->ar_data[6]);
1236 memcpy (&arp->ar_data[ 0], NetOurEther, 6);
1237 NetCopyIP(&arp->ar_data[ 6], &NetOurIP);
1238 (void) eth_send((uchar *)et, (pkt - (uchar *)et) + ARP_HDR_SIZE);
1241 case ARPOP_REPLY: /* arp reply */
1242 /* are we waiting for a reply */
1243 if (!NetArpWaitPacketIP || !NetArpWaitPacketMAC)
1246 printf("Got ARP REPLY, set server/gtwy eth addr (%02x:%02x:%02x:%02x:%02x:%02x)\n",
1247 arp->ar_data[0], arp->ar_data[1],
1248 arp->ar_data[2], arp->ar_data[3],
1249 arp->ar_data[4], arp->ar_data[5]);
1252 tmp = NetReadIP(&arp->ar_data[6]);
1254 /* matched waiting packet's address */
1255 if (tmp == NetArpWaitReplyIP) {
1259 /* save address for later use */
1260 memcpy(NetArpWaitPacketMAC, &arp->ar_data[0], 6);
1262 /* modify header, and transmit it */
1263 memcpy(((Ethernet_t *)NetArpWaitTxPacket)->et_dest, NetArpWaitPacketMAC, 6);
1264 (void) eth_send(NetArpWaitTxPacket, NetArpWaitTxPacketSize);
1266 /* no arp request pending now */
1267 NetArpWaitPacketIP = 0;
1268 NetArpWaitTxPacketSize = 0;
1269 NetArpWaitPacketMAC = NULL;
1275 printf("Unexpected ARP opcode 0x%x\n", ntohs(arp->ar_op));
1282 puts ("Got RARP\n");
1285 if (len < ARP_HDR_SIZE) {
1286 printf("bad length %d < %d\n", len, ARP_HDR_SIZE);
1290 if ((ntohs(arp->ar_op) != RARPOP_REPLY) ||
1291 (ntohs(arp->ar_hrd) != ARP_ETHER) ||
1292 (ntohs(arp->ar_pro) != PROT_IP) ||
1293 (arp->ar_hln != 6) || (arp->ar_pln != 4)) {
1295 puts ("invalid RARP header\n");
1297 NetCopyIP(&NetOurIP, &arp->ar_data[16]);
1298 if (NetServerIP == 0)
1299 NetCopyIP(&NetServerIP, &arp->ar_data[ 6]);
1300 memcpy (NetServerEther, &arp->ar_data[ 0], 6);
1302 (*packetHandler)(0,0,0,0);
1310 if (len < IP_HDR_SIZE) {
1311 debug ("len bad %d < %d\n", len, IP_HDR_SIZE);
1314 if (len < ntohs(ip->ip_len)) {
1315 printf("len bad %d < %d\n", len, ntohs(ip->ip_len));
1318 len = ntohs(ip->ip_len);
1320 printf("len=%d, v=%02x\n", len, ip->ip_hl_v & 0xff);
1322 if ((ip->ip_hl_v & 0xf0) != 0x40) {
1325 if (ip->ip_off & htons(0x1fff)) { /* Can't deal w/ fragments */
1328 if (!NetCksumOk((uchar *)ip, IP_HDR_SIZE_NO_UDP / 2)) {
1329 puts ("checksum bad\n");
1332 tmp = NetReadIP(&ip->ip_dst);
1333 if (NetOurIP && tmp != NetOurIP && tmp != 0xFFFFFFFF) {
1337 * watch for ICMP host redirects
1339 * There is no real handler code (yet). We just watch
1340 * for ICMP host redirect messages. In case anybody
1341 * sees these messages: please contact me
1343 * necessary fixes :-)
1345 * Note: in all cases where I have seen this so far
1346 * it was a problem with the router configuration,
1347 * for instance when a router was configured in the
1348 * BOOTP reply, but the TFTP server was on the same
1349 * subnet. So this is probably a warning that your
1350 * configuration might be wrong. But I'm not really
1351 * sure if there aren't any other situations.
1353 if (ip->ip_p == IPPROTO_ICMP) {
1354 ICMP_t *icmph = (ICMP_t *)&(ip->udp_src);
1356 switch (icmph->type) {
1358 if (icmph->code != ICMP_REDIR_HOST)
1360 puts (" ICMP Host Redirect to ");
1361 print_IPaddr(icmph->un.gateway);
1364 #if (CONFIG_COMMANDS & CFG_CMD_PING)
1365 case ICMP_ECHO_REPLY:
1367 * IP header OK. Pass the packet to the current handler.
1369 /* XXX point to ip packet */
1370 (*packetHandler)((uchar *)ip, 0, 0, 0);
1376 } else if (ip->ip_p != IPPROTO_UDP) { /* Only UDP packets */
1381 * IP header OK. Pass the packet to the current handler.
1383 (*packetHandler)((uchar *)ip +IP_HDR_SIZE,
1386 ntohs(ip->udp_len) - 8);
1392 /**********************************************************************/
1394 static int net_check_prereq (proto_t protocol)
1398 #if (CONFIG_COMMANDS & CFG_CMD_PING)
1400 if (NetPingIP == 0) {
1401 puts ("*** ERROR: ping address not given\n");
1406 #if (CONFIG_COMMANDS & CFG_CMD_NFS)
1410 if (NetServerIP == 0) {
1411 puts ("*** ERROR: `serverip' not set\n");
1414 #if (CONFIG_COMMANDS & CFG_CMD_PING)
1418 if (NetOurIP == 0) {
1419 puts ("*** ERROR: `ipaddr' not set\n");
1428 if (memcmp (NetOurEther, "\0\0\0\0\0\0", 6) == 0) {
1429 #ifdef CONFIG_NET_MULTI
1430 extern int eth_get_dev_index (void);
1431 int num = eth_get_dev_index ();
1435 puts ("*** ERROR: No ethernet found.\n");
1438 puts ("*** ERROR: `ethaddr' not set\n");
1441 printf ("*** ERROR: `eth%daddr' not set\n",
1449 puts ("*** ERROR: `ethaddr' not set\n");
1457 return (0); /* OK */
1459 /**********************************************************************/
1462 NetCksumOk(uchar * ptr, int len)
1464 return !((NetCksum(ptr, len) + 1) & 0xfffe);
1469 NetCksum(uchar * ptr, int len)
1475 xsum += *((ushort *)ptr)++;
1476 xsum = (xsum & 0xffff) + (xsum >> 16);
1477 xsum = (xsum & 0xffff) + (xsum >> 16);
1478 return (xsum & 0xffff);
1486 myvlanid = ntohs(NetOurVLAN);
1487 if (myvlanid == (ushort)-1)
1488 myvlanid = VLAN_NONE;
1490 return ((myvlanid & VLAN_IDMASK) == VLAN_NONE) ? ETHER_HDR_SIZE : VLAN_ETHER_HDR_SIZE;
1494 NetSetEther(volatile uchar * xet, uchar * addr, uint prot)
1496 Ethernet_t *et = (Ethernet_t *)xet;
1499 myvlanid = ntohs(NetOurVLAN);
1500 if (myvlanid == (ushort)-1)
1501 myvlanid = VLAN_NONE;
1503 memcpy (et->et_dest, addr, 6);
1504 memcpy (et->et_src, NetOurEther, 6);
1505 if ((myvlanid & VLAN_IDMASK) == VLAN_NONE) {
1506 et->et_protlen = htons(prot);
1507 return ETHER_HDR_SIZE;
1509 VLAN_Ethernet_t *vet = (VLAN_Ethernet_t *)xet;
1511 vet->vet_vlan_type = htons(PROT_VLAN);
1512 vet->vet_tag = htons((0 << 5) | (myvlanid & VLAN_IDMASK));
1513 vet->vet_type = htons(prot);
1514 return VLAN_ETHER_HDR_SIZE;
1519 NetSetIP(volatile uchar * xip, IPaddr_t dest, int dport, int sport, int len)
1521 volatile IP_t *ip = (IP_t *)xip;
1524 * If the data is an odd number of bytes, zero the
1525 * byte after the last byte so that the checksum
1529 xip[IP_HDR_SIZE + len] = 0;
1532 * Construct an IP and UDP header.
1533 * (need to set no fragment bit - XXX)
1535 ip->ip_hl_v = 0x45; /* IP_HDR_SIZE / 4 (not including UDP) */
1537 ip->ip_len = htons(IP_HDR_SIZE + len);
1538 ip->ip_id = htons(NetIPID++);
1539 ip->ip_off = htons(0x4000); /* No fragmentation */
1541 ip->ip_p = 17; /* UDP */
1543 NetCopyIP((void*)&ip->ip_src, &NetOurIP); /* already in network byte order */
1544 NetCopyIP((void*)&ip->ip_dst, &dest); /* - "" - */
1545 ip->udp_src = htons(sport);
1546 ip->udp_dst = htons(dport);
1547 ip->udp_len = htons(8 + len);
1549 ip->ip_sum = ~NetCksum((uchar *)ip, IP_HDR_SIZE_NO_UDP / 2);
1552 void copy_filename (uchar *dst, uchar *src, int size)
1554 if (*src && (*src == '"')) {
1559 while ((--size > 0) && *src && (*src != '"')) {
1565 #endif /* CFG_CMD_NET */
1567 void ip_to_string (IPaddr_t x, char *s)
1570 sprintf (s, "%d.%d.%d.%d",
1571 (int) ((x >> 24) & 0xff),
1572 (int) ((x >> 16) & 0xff),
1573 (int) ((x >> 8) & 0xff), (int) ((x >> 0) & 0xff)
1577 IPaddr_t string_to_ip(char *s)
1586 for (addr=0, i=0; i<4; ++i) {
1587 ulong val = s ? simple_strtoul(s, &e, 10) : 0;
1589 addr |= (val & 0xFF);
1595 return (htonl(addr));
1598 void VLAN_to_string(ushort x, char *s)
1602 if (x == (ushort)-1)
1608 sprintf(s, "%d", x & VLAN_IDMASK);
1611 ushort string_to_VLAN(char *s)
1618 if (*s < '0' || *s > '9')
1621 id = (ushort)simple_strtoul(s, NULL, 10);
1626 void print_IPaddr (IPaddr_t x)
1630 ip_to_string (x, tmp);
1635 IPaddr_t getenv_IPaddr (char *var)
1637 return (string_to_ip(getenv(var)));
1640 ushort getenv_VLAN(char *var)
1642 return (string_to_VLAN(getenv(var)));