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
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 #ifdef CONFIG_NETCONSOLE
153 int nc_input_packet(uchar *pkt, unsigned dest, unsigned src, unsigned len);
156 volatile uchar PktBuf[(PKTBUFSRX+1) * PKTSIZE_ALIGN + PKTALIGN];
158 volatile uchar *NetRxPackets[PKTBUFSRX]; /* Receive packets */
160 static rxhand_f *packetHandler; /* Current RX packet handler */
161 static thand_f *timeHandler; /* Current timeout handler */
162 static ulong timeStart; /* Time base value */
163 static ulong timeDelta; /* Current timeout value */
164 volatile uchar *NetTxPacket = 0; /* THE transmit packet */
166 static int net_check_prereq (proto_t protocol);
168 /**********************************************************************/
170 IPaddr_t NetArpWaitPacketIP;
171 IPaddr_t NetArpWaitReplyIP;
172 uchar *NetArpWaitPacketMAC; /* MAC address of waiting packet's destination */
173 uchar *NetArpWaitTxPacket; /* THE transmit packet */
174 int NetArpWaitTxPacketSize;
175 uchar NetArpWaitPacketBuf[PKTSIZE_ALIGN + PKTALIGN];
176 ulong NetArpWaitTimerStart;
179 void ArpRequest (void)
186 printf ("ARP broadcast %d\n", NetArpWaitTry);
190 pkt += NetSetEther (pkt, NetBcastAddr, PROT_ARP);
194 arp->ar_hrd = htons (ARP_ETHER);
195 arp->ar_pro = htons (PROT_IP);
198 arp->ar_op = htons (ARPOP_REQUEST);
200 memcpy (&arp->ar_data[0], NetOurEther, 6); /* source ET addr */
201 NetWriteIP ((uchar *) & arp->ar_data[6], NetOurIP); /* source IP addr */
202 for (i = 10; i < 16; ++i) {
203 arp->ar_data[i] = 0; /* dest ET addr = 0 */
206 if ((NetArpWaitPacketIP & NetOurSubnetMask) !=
207 (NetOurIP & NetOurSubnetMask)) {
208 if (NetOurGatewayIP == 0) {
209 puts ("## Warning: gatewayip needed but not set\n");
211 NetArpWaitReplyIP = NetOurGatewayIP;
213 NetArpWaitReplyIP = NetArpWaitPacketIP;
216 NetWriteIP ((uchar *) & arp->ar_data[16], NetArpWaitReplyIP);
217 (void) eth_send (NetTxPacket, (pkt - NetTxPacket) + ARP_HDR_SIZE);
220 void ArpTimeoutCheck(void)
224 if (!NetArpWaitPacketIP)
229 /* check for arp timeout */
230 if ((t - NetArpWaitTimerStart) > ARP_TIMEOUT * CFG_HZ) {
233 if (NetArpWaitTry >= ARP_TIMEOUT_COUNT) {
234 puts ("\nARP Retry count exceeded; starting again\n");
238 NetArpWaitTimerStart = t;
244 /**********************************************************************/
246 * Main network processing loop.
250 NetLoop(proto_t protocol)
252 DECLARE_GLOBAL_DATA_PTR;
256 #ifdef CONFIG_NET_MULTI
261 /* XXX problem with bss workaround */
262 NetArpWaitPacketMAC = NULL;
263 NetArpWaitTxPacket = NULL;
264 NetArpWaitPacketIP = 0;
265 NetArpWaitReplyIP = 0;
266 NetArpWaitTxPacket = NULL;
272 * Setup packet buffers, aligned correctly.
274 NetTxPacket = &PktBuf[0] + (PKTALIGN - 1);
275 NetTxPacket -= (ulong)NetTxPacket % PKTALIGN;
276 for (i = 0; i < PKTBUFSRX; i++) {
277 NetRxPackets[i] = NetTxPacket + (i+1)*PKTSIZE_ALIGN;
281 if (!NetArpWaitTxPacket) {
282 NetArpWaitTxPacket = &NetArpWaitPacketBuf[0] + (PKTALIGN - 1);
283 NetArpWaitTxPacket -= (ulong)NetArpWaitTxPacket % PKTALIGN;
284 NetArpWaitTxPacketSize = 0;
288 #ifdef CONFIG_NET_MULTI
291 if (eth_init(bd) < 0)
295 #ifdef CONFIG_NET_MULTI
296 memcpy (NetOurEther, eth_get_dev()->enetaddr, 6);
298 memcpy (NetOurEther, bd->bi_enetaddr, 6);
301 NetState = NETLOOP_CONTINUE;
304 * Start the ball rolling with the given start function. From
305 * here on, this code is a state machine driven by received
306 * packets and timer events.
310 #if (CONFIG_COMMANDS & CFG_CMD_NFS)
313 #if (CONFIG_COMMANDS & CFG_CMD_PING)
318 NetCopyIP(&NetOurIP, &bd->bi_ip_addr);
319 NetOurGatewayIP = getenv_IPaddr ("gatewayip");
320 NetOurSubnetMask= getenv_IPaddr ("netmask");
321 NetOurVLAN = getenv_VLAN("vlan");
322 NetOurNativeVLAN = getenv_VLAN("nvlan");
325 #if (CONFIG_COMMANDS & CFG_CMD_NFS)
330 NetServerIP = getenv_IPaddr ("serverip");
332 #if (CONFIG_COMMANDS & CFG_CMD_PING)
345 * initialize our IP addr to 0 in order to accept ANY
346 * IP addr assigned to us by the BOOTP / RARP server
349 NetServerIP = getenv_IPaddr ("serverip");
350 NetOurVLAN = getenv_VLAN("vlan"); /* VLANs must be read */
351 NetOurNativeVLAN = getenv_VLAN("nvlan");
353 NetOurVLAN = getenv_VLAN("vlan"); /* VLANs must be read */
354 NetOurNativeVLAN = getenv_VLAN("nvlan");
360 switch (net_check_prereq (protocol)) {
362 /* network not configured */
365 #ifdef CONFIG_NET_MULTI
367 /* network device not configured */
369 #endif /* CONFIG_NET_MULTI */
372 #ifdef CONFIG_NET_MULTI
377 /* always use ARP to get server ethernet address */
381 #if (CONFIG_COMMANDS & CFG_CMD_DHCP)
383 /* Start with a clean slate... */
386 NetServerIP = getenv_IPaddr ("serverip");
387 DhcpRequest(); /* Basically same as BOOTP */
389 #endif /* CFG_CMD_DHCP */
400 #if (CONFIG_COMMANDS & CFG_CMD_PING)
405 #if (CONFIG_COMMANDS & CFG_CMD_NFS)
410 #if (CONFIG_COMMANDS & CFG_CMD_CDP)
415 #ifdef CONFIG_NETCONSOLE
424 NetBootFileXferSize = 0;
428 #if defined(CONFIG_MII) || (CONFIG_COMMANDS & CFG_CMD_MII)
429 #if defined(CFG_FAULT_ECHO_LINK_DOWN) && defined(CONFIG_STATUS_LED) && defined(STATUS_LED_RED)
431 * Echo the inverted link state to the fault LED.
433 if(miiphy_link(CFG_FAULT_MII_ADDR)) {
434 status_led_set (STATUS_LED_RED, STATUS_LED_OFF);
436 status_led_set (STATUS_LED_RED, STATUS_LED_ON);
438 #endif /* CFG_FAULT_ECHO_LINK_DOWN, ... */
439 #endif /* CONFIG_MII, ... */
442 * Main packet reception loop. Loop receiving packets until
443 * someone sets `NetQuit'.
447 #ifdef CONFIG_SHOW_ACTIVITY
449 extern void show_activity(int arg);
454 * Check the ethernet for a new packet. The ethernet
455 * receive routine will process it.
460 * Abort if ctrl-c was pressed.
471 * Check for a timeout, and run the timeout handler
474 if (timeHandler && ((get_timer(0) - timeStart) > timeDelta)) {
477 #if defined(CONFIG_MII) || (CONFIG_COMMANDS & CFG_CMD_MII)
478 #if defined(CFG_FAULT_ECHO_LINK_DOWN) && defined(CONFIG_STATUS_LED) && defined(STATUS_LED_RED)
480 * Echo the inverted link state to the fault LED.
482 if(miiphy_link(CFG_FAULT_MII_ADDR)) {
483 status_led_set (STATUS_LED_RED, STATUS_LED_OFF);
485 status_led_set (STATUS_LED_RED, STATUS_LED_ON);
487 #endif /* CFG_FAULT_ECHO_LINK_DOWN, ... */
488 #endif /* CONFIG_MII, ... */
490 timeHandler = (thand_f *)0;
497 case NETLOOP_RESTART:
498 #ifdef CONFIG_NET_MULTI
503 case NETLOOP_SUCCESS:
504 if (NetBootFileXferSize > 0) {
506 printf("Bytes transferred = %ld (%lx hex)\n",
508 NetBootFileXferSize);
509 sprintf(buf, "%lx", NetBootFileXferSize);
510 setenv("filesize", buf);
512 sprintf(buf, "%lX", (unsigned long)load_addr);
513 setenv("fileaddr", buf);
516 return NetBootFileXferSize;
524 /**********************************************************************/
527 startAgainTimeout(void)
529 NetState = NETLOOP_RESTART;
533 startAgainHandler(uchar * pkt, unsigned dest, unsigned src, unsigned len)
535 /* Totally ignore the packet */
538 void NetStartAgain (void)
540 #ifdef CONFIG_NET_MULTI
541 DECLARE_GLOBAL_DATA_PTR;
544 int noretry = 0, once = 0;
546 if ((nretry = getenv ("netretry")) != NULL) {
547 noretry = (strcmp (nretry, "no") == 0);
548 once = (strcmp (nretry, "once") == 0);
552 NetState = NETLOOP_FAIL;
555 #ifndef CONFIG_NET_MULTI
556 NetSetTimeout (10 * CFG_HZ, startAgainTimeout);
557 NetSetHandler (startAgainHandler);
558 #else /* !CONFIG_NET_MULTI*/
560 eth_try_another (!NetRestarted);
562 if (NetRestartWrap) {
564 if (NetDevExists && !once) {
565 NetSetTimeout (10 * CFG_HZ, startAgainTimeout);
566 NetSetHandler (startAgainHandler);
568 NetState = NETLOOP_FAIL;
571 NetState = NETLOOP_RESTART;
573 #endif /* CONFIG_NET_MULTI */
576 /**********************************************************************/
582 NetSetHandler(rxhand_f * f)
589 NetSetTimeout(ulong iv, thand_f * f)
592 timeHandler = (thand_f *)0;
595 timeStart = get_timer(0);
602 NetSendPacket(volatile uchar * pkt, int len)
604 (void) eth_send(pkt, len);
608 NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport, int sport, int len)
612 /* convert to new style broadcast */
616 /* if broadcast, make the ether address a broadcast and don't do ARP */
617 if (dest == 0xFFFFFFFF)
618 ether = NetBcastAddr;
620 /* if MAC address was not discovered yet, save the packet and do an ARP request */
621 if (memcmp(ether, NetEtherNullAddr, 6) == 0) {
624 printf("sending ARP for %08lx\n", dest);
626 NetArpWaitPacketIP = dest;
627 NetArpWaitPacketMAC = ether;
629 pkt = NetArpWaitTxPacket;
630 pkt += NetSetEther (pkt, NetArpWaitPacketMAC, PROT_IP);
632 NetSetIP (pkt, dest, dport, sport, len);
633 memcpy(pkt + IP_HDR_SIZE, (uchar *)NetTxPacket + (pkt - (uchar *)NetArpWaitTxPacket) + IP_HDR_SIZE, len);
635 /* size of the waiting packet */
636 NetArpWaitTxPacketSize = (pkt - NetArpWaitTxPacket) + IP_HDR_SIZE + len;
638 /* and do the ARP request */
640 NetArpWaitTimerStart = get_timer(0);
642 return 1; /* waiting */
646 printf("sending UDP to %08lx/%02x:%02x:%02x:%02x:%02x:%02x\n",
647 dest, ether[0], ether[1], ether[2], ether[3], ether[4], ether[5]);
650 pkt = (uchar *)NetTxPacket;
651 pkt += NetSetEther (pkt, ether, PROT_IP);
652 NetSetIP (pkt, dest, dport, sport, len);
653 (void) eth_send(NetTxPacket, (pkt - NetTxPacket) + IP_HDR_SIZE + len);
655 return 0; /* transmitted */
658 #if (CONFIG_COMMANDS & CFG_CMD_PING)
659 static ushort PingSeqNo;
668 /* XXX always send arp request */
670 memcpy(mac, NetEtherNullAddr, 6);
673 printf("sending ARP for %08lx\n", NetPingIP);
676 NetArpWaitPacketIP = NetPingIP;
677 NetArpWaitPacketMAC = mac;
679 pkt = NetArpWaitTxPacket;
680 pkt += NetSetEther(pkt, mac, PROT_IP);
682 ip = (volatile IP_t *)pkt;
685 * Construct an IP and ICMP header. (need to set no fragment bit - XXX)
687 ip->ip_hl_v = 0x45; /* IP_HDR_SIZE / 4 (not including UDP) */
689 ip->ip_len = htons(IP_HDR_SIZE_NO_UDP + 8);
690 ip->ip_id = htons(NetIPID++);
691 ip->ip_off = htons(0x4000); /* No fragmentation */
693 ip->ip_p = 0x01; /* ICMP */
695 NetCopyIP((void*)&ip->ip_src, &NetOurIP); /* already in network byte order */
696 NetCopyIP((void*)&ip->ip_dst, &NetPingIP); /* - "" - */
697 ip->ip_sum = ~NetCksum((uchar *)ip, IP_HDR_SIZE_NO_UDP / 2);
699 s = &ip->udp_src; /* XXX ICMP starts here */
700 s[0] = htons(0x0800); /* echo-request, code */
701 s[1] = 0; /* checksum */
702 s[2] = 0; /* identifier */
703 s[3] = htons(PingSeqNo++); /* sequence number */
704 s[1] = ~NetCksum((uchar *)s, 8/2);
706 /* size of the waiting packet */
707 NetArpWaitTxPacketSize = (pkt - NetArpWaitTxPacket) + IP_HDR_SIZE_NO_UDP + 8;
709 /* and do the ARP request */
711 NetArpWaitTimerStart = get_timer(0);
713 return 1; /* waiting */
720 NetState = NETLOOP_FAIL; /* we did not get the reply */
724 PingHandler (uchar * pkt, unsigned dest, unsigned src, unsigned len)
727 volatile IP_t *ip = (volatile IP_t *)pkt;
729 tmp = NetReadIP((void *)&ip->ip_src);
730 if (tmp != NetPingIP)
733 NetState = NETLOOP_SUCCESS;
736 static void PingStart(void)
738 #if defined(CONFIG_NET_MULTI)
739 printf ("Using %s device\n", eth_get_name());
740 #endif /* CONFIG_NET_MULTI */
741 NetSetTimeout (10 * CFG_HZ, PingTimeout);
742 NetSetHandler (PingHandler);
746 #endif /* CFG_CMD_PING */
748 #if (CONFIG_COMMANDS & CFG_CMD_CDP)
750 #define CDP_DEVICE_ID_TLV 0x0001
751 #define CDP_ADDRESS_TLV 0x0002
752 #define CDP_PORT_ID_TLV 0x0003
753 #define CDP_CAPABILITIES_TLV 0x0004
754 #define CDP_VERSION_TLV 0x0005
755 #define CDP_PLATFORM_TLV 0x0006
756 #define CDP_NATIVE_VLAN_TLV 0x000a
757 #define CDP_APPLIANCE_VLAN_TLV 0x000e
758 #define CDP_TRIGGER_TLV 0x000f
759 #define CDP_POWER_CONSUMPTION_TLV 0x0010
760 #define CDP_SYSNAME_TLV 0x0014
761 #define CDP_SYSOBJECT_TLV 0x0015
762 #define CDP_MANAGEMENT_ADDRESS_TLV 0x0016
764 #define CDP_TIMEOUT (CFG_HZ/4) /* one packet every 250ms */
769 ushort CDPNativeVLAN;
770 ushort CDPApplianceVLAN;
772 static const uchar CDP_SNAP_hdr[8] = { 0xAA, 0xAA, 0x03, 0x00, 0x00, 0x0C, 0x20, 0x00 };
774 static ushort CDP_compute_csum(const uchar *buff, ushort len)
782 odd = 1 & (ulong)buff;
789 result += *((const ushort *)buff)++;
790 if (result & 0x80000000)
791 result = (result & 0xFFFF) + (result >> 16);
795 leftover = (signed short)(*(const signed char *)buff);
796 /* * XXX CISCO SUCKS big time! (and blows too) */
797 result = (result & 0xffff0000) | ((result + leftover) & 0x0000ffff);
800 result = (result & 0xFFFF) + (result >> 16);
803 result = ((result >> 8) & 0xff) | ((result & 0xff) << 8);
806 /* add up 16-bit and 17-bit words for 17+c bits */
807 result = (result & 0xffff) + (result >> 16);
808 /* add up 16-bit and 2-bit for 16+c bit */
809 result = (result & 0xffff) + (result >> 16);
811 result = (result & 0xffff) + (result >> 16);
814 csum = ~(ushort)result;
816 /* run time endian detection */
817 if (csum != htons(csum)) /* little endian */
823 int CDPSendTrigger(void)
831 #if defined(CONFIG_CDP_DEVICE_ID) || defined(CONFIG_CDP_PORT_ID) || \
832 defined(CONFIG_CDP_VERSION) || defined(CONFIG_CDP_PLATFORM)
837 et = (Ethernet_t *)pkt;
839 /* NOTE: trigger sent not on any VLAN */
841 /* form ethernet header */
842 memcpy(et->et_dest, NetCDPAddr, 6);
843 memcpy(et->et_src, NetOurEther, 6);
845 pkt += ETHER_HDR_SIZE;
848 memcpy((uchar *)pkt, CDP_SNAP_hdr, sizeof(CDP_SNAP_hdr));
849 pkt += sizeof(CDP_SNAP_hdr);
852 *pkt++ = 0x02; /* CDP version 2 */
853 *pkt++ = 180; /* TTL */
854 s = (volatile ushort *)pkt;
856 *s++ = htons(0); /* checksum (0 for later calculation) */
859 #ifdef CONFIG_CDP_DEVICE_ID
860 *s++ = htons(CDP_DEVICE_ID_TLV);
861 *s++ = htons(CONFIG_CDP_DEVICE_ID);
862 memset(buf, 0, sizeof(buf));
863 sprintf(buf, CONFIG_CDP_DEVICE_ID_PREFIX "%02X%02X%02X%02X%02X%02X",
864 NetOurEther[0] & 0xff, NetOurEther[1] & 0xff,
865 NetOurEther[2] & 0xff, NetOurEther[3] & 0xff,
866 NetOurEther[4] & 0xff, NetOurEther[5] & 0xff);
867 memcpy((uchar *)s, buf, 16);
871 #ifdef CONFIG_CDP_PORT_ID
872 *s++ = htons(CDP_PORT_ID_TLV);
873 memset(buf, 0, sizeof(buf));
874 sprintf(buf, CONFIG_CDP_PORT_ID, eth_get_dev_index());
876 if (len & 1) /* make it even */
878 *s++ = htons(len + 4);
879 memcpy((uchar *)s, buf, len);
883 #ifdef CONFIG_CDP_CAPABILITIES
884 *s++ = htons(CDP_CAPABILITIES_TLV);
886 *(ulong *)s = htonl(CONFIG_CDP_CAPABILITIES);
890 #ifdef CONFIG_CDP_VERSION
891 *s++ = htons(CDP_VERSION_TLV);
892 memset(buf, 0, sizeof(buf));
893 strcpy(buf, CONFIG_CDP_VERSION);
895 if (len & 1) /* make it even */
897 *s++ = htons(len + 4);
898 memcpy((uchar *)s, buf, len);
902 #ifdef CONFIG_CDP_PLATFORM
903 *s++ = htons(CDP_PLATFORM_TLV);
904 memset(buf, 0, sizeof(buf));
905 strcpy(buf, CONFIG_CDP_PLATFORM);
907 if (len & 1) /* make it even */
909 *s++ = htons(len + 4);
910 memcpy((uchar *)s, buf, len);
914 #ifdef CONFIG_CDP_TRIGGER
915 *s++ = htons(CDP_TRIGGER_TLV);
917 *(ulong *)s = htonl(CONFIG_CDP_TRIGGER);
921 #ifdef CONFIG_CDP_POWER_CONSUMPTION
922 *s++ = htons(CDP_POWER_CONSUMPTION_TLV);
924 *s++ = htons(CONFIG_CDP_POWER_CONSUMPTION);
927 /* length of ethernet packet */
928 len = (uchar *)s - ((uchar *)NetTxPacket + ETHER_HDR_SIZE);
929 et->et_protlen = htons(len);
931 len = ETHER_HDR_SIZE + sizeof(CDP_SNAP_hdr);
932 chksum = CDP_compute_csum((uchar *)NetTxPacket + len, (uchar *)s - (NetTxPacket + len));
937 (void) eth_send(NetTxPacket, (uchar *)s - NetTxPacket);
947 NetSetTimeout (CDP_TIMEOUT, CDPTimeout);
952 /* if not OK try again */
956 NetState = NETLOOP_SUCCESS;
960 CDPDummyHandler (uchar * pkt, unsigned dest, unsigned src, unsigned len)
966 CDPHandler(const uchar * pkt, unsigned len)
975 if (len < sizeof(CDP_SNAP_hdr) + 4)
978 /* check for valid CDP SNAP header */
979 if (memcmp(pkt, CDP_SNAP_hdr, sizeof(CDP_SNAP_hdr)) != 0)
982 pkt += sizeof(CDP_SNAP_hdr);
983 len -= sizeof(CDP_SNAP_hdr);
985 /* Version of CDP protocol must be >= 2 and TTL != 0 */
986 if (pkt[0] < 0x02 || pkt[1] == 0)
989 /* if version is greater than 0x02 maybe we'll have a problem; output a warning */
991 printf("** WARNING: CDP packet received with a protocol version %d > 2\n",
994 if (CDP_compute_csum(pkt, len) != 0)
1006 ss = (const ushort *)pkt;
1007 type = ntohs(ss[0]);
1008 tlen = ntohs(ss[1]);
1016 ss += 2; /* point ss to the data of the TLV */
1020 case CDP_DEVICE_ID_TLV:
1022 case CDP_ADDRESS_TLV:
1024 case CDP_PORT_ID_TLV:
1026 case CDP_CAPABILITIES_TLV:
1028 case CDP_VERSION_TLV:
1030 case CDP_PLATFORM_TLV:
1032 case CDP_NATIVE_VLAN_TLV:
1035 case CDP_APPLIANCE_VLAN_TLV:
1036 t = (const uchar *)ss;
1042 ss = (const ushort *)(t + 1);
1044 #ifdef CONFIG_CDP_APPLIANCE_VLAN_TYPE
1045 if (applid == CONFIG_CDP_APPLIANCE_VLAN_TYPE)
1048 vlan = ntohs(*ss); /* XXX will this work; dunno */
1053 case CDP_TRIGGER_TLV:
1055 case CDP_POWER_CONSUMPTION_TLV:
1057 case CDP_SYSNAME_TLV:
1059 case CDP_SYSOBJECT_TLV:
1061 case CDP_MANAGEMENT_ADDRESS_TLV:
1066 CDPApplianceVLAN = vlan;
1067 CDPNativeVLAN = nvlan;
1073 printf("** CDP packet is too short\n");
1077 static void CDPStart(void)
1079 #if defined(CONFIG_NET_MULTI)
1080 printf ("Using %s device\n", eth_get_name());
1085 CDPNativeVLAN = htons(-1);
1086 CDPApplianceVLAN = htons(-1);
1088 NetSetTimeout (CDP_TIMEOUT, CDPTimeout);
1089 NetSetHandler (CDPDummyHandler);
1093 #endif /* CFG_CMD_CDP */
1097 NetReceive(volatile uchar * inpkt, int len)
1105 #if (CONFIG_COMMANDS & CFG_CMD_CDP)
1108 ushort cti = 0, vlanid = VLAN_NONE, myvlanid, mynvlanid;
1111 printf("packet received\n");
1116 et = (Ethernet_t *)inpkt;
1118 /* too small packet? */
1119 if (len < ETHER_HDR_SIZE)
1122 #if (CONFIG_COMMANDS & CFG_CMD_CDP)
1123 /* keep track if packet is CDP */
1124 iscdp = memcmp(et->et_dest, NetCDPAddr, 6) == 0;
1127 myvlanid = ntohs(NetOurVLAN);
1128 if (myvlanid == (ushort)-1)
1129 myvlanid = VLAN_NONE;
1130 mynvlanid = ntohs(NetOurNativeVLAN);
1131 if (mynvlanid == (ushort)-1)
1132 mynvlanid = VLAN_NONE;
1134 x = ntohs(et->et_protlen);
1137 printf("packet received\n");
1142 * Got a 802 packet. Check the other protocol field.
1144 x = ntohs(et->et_prot);
1146 ip = (IP_t *)(inpkt + E802_HDR_SIZE);
1147 len -= E802_HDR_SIZE;
1149 } else if (x != PROT_VLAN) { /* normal packet */
1150 ip = (IP_t *)(inpkt + ETHER_HDR_SIZE);
1151 len -= ETHER_HDR_SIZE;
1153 } else { /* VLAN packet */
1154 VLAN_Ethernet_t *vet = (VLAN_Ethernet_t *)et;
1157 printf("VLAN packet received\n");
1159 /* too small packet? */
1160 if (len < VLAN_ETHER_HDR_SIZE)
1163 /* if no VLAN active */
1164 if ((ntohs(NetOurVLAN) & VLAN_IDMASK) == VLAN_NONE
1165 #if (CONFIG_COMMANDS & CFG_CMD_CDP)
1171 cti = ntohs(vet->vet_tag);
1172 vlanid = cti & VLAN_IDMASK;
1173 x = ntohs(vet->vet_type);
1175 ip = (IP_t *)(inpkt + VLAN_ETHER_HDR_SIZE);
1176 len -= VLAN_ETHER_HDR_SIZE;
1180 printf("Receive from protocol 0x%x\n", x);
1183 #if (CONFIG_COMMANDS & CFG_CMD_CDP)
1185 CDPHandler((uchar *)ip, len);
1190 if ((myvlanid & VLAN_IDMASK) != VLAN_NONE) {
1191 if (vlanid == VLAN_NONE)
1192 vlanid = (mynvlanid & VLAN_IDMASK);
1194 if (vlanid != (myvlanid & VLAN_IDMASK))
1202 * We have to deal with two types of ARP packets:
1203 * - REQUEST packets will be answered by sending our
1204 * IP address - if we know it.
1205 * - REPLY packates are expected only after we asked
1206 * for the TFTP server's or the gateway's ethernet
1207 * address; so if we receive such a packet, we set
1208 * the server ethernet address
1214 if (len < ARP_HDR_SIZE) {
1215 printf("bad length %d < %d\n", len, ARP_HDR_SIZE);
1218 if (ntohs(arp->ar_hrd) != ARP_ETHER) {
1221 if (ntohs(arp->ar_pro) != PROT_IP) {
1224 if (arp->ar_hln != 6) {
1227 if (arp->ar_pln != 4) {
1231 if (NetOurIP == 0) {
1235 if (NetReadIP(&arp->ar_data[16]) != NetOurIP) {
1239 switch (ntohs(arp->ar_op)) {
1240 case ARPOP_REQUEST: /* reply with our IP address */
1242 puts ("Got ARP REQUEST, return our IP\n");
1245 pkt += NetSetEther(pkt, et->et_src, PROT_ARP);
1246 arp->ar_op = htons(ARPOP_REPLY);
1247 memcpy (&arp->ar_data[10], &arp->ar_data[0], 6);
1248 NetCopyIP(&arp->ar_data[16], &arp->ar_data[6]);
1249 memcpy (&arp->ar_data[ 0], NetOurEther, 6);
1250 NetCopyIP(&arp->ar_data[ 6], &NetOurIP);
1251 (void) eth_send((uchar *)et, (pkt - (uchar *)et) + ARP_HDR_SIZE);
1254 case ARPOP_REPLY: /* arp reply */
1255 /* are we waiting for a reply */
1256 if (!NetArpWaitPacketIP || !NetArpWaitPacketMAC)
1259 printf("Got ARP REPLY, set server/gtwy eth addr (%02x:%02x:%02x:%02x:%02x:%02x)\n",
1260 arp->ar_data[0], arp->ar_data[1],
1261 arp->ar_data[2], arp->ar_data[3],
1262 arp->ar_data[4], arp->ar_data[5]);
1265 tmp = NetReadIP(&arp->ar_data[6]);
1267 /* matched waiting packet's address */
1268 if (tmp == NetArpWaitReplyIP) {
1272 /* save address for later use */
1273 memcpy(NetArpWaitPacketMAC, &arp->ar_data[0], 6);
1275 #ifdef CONFIG_NETCONSOLE
1276 (*packetHandler)(0,0,0,0);
1278 /* modify header, and transmit it */
1279 memcpy(((Ethernet_t *)NetArpWaitTxPacket)->et_dest, NetArpWaitPacketMAC, 6);
1280 (void) eth_send(NetArpWaitTxPacket, NetArpWaitTxPacketSize);
1282 /* no arp request pending now */
1283 NetArpWaitPacketIP = 0;
1284 NetArpWaitTxPacketSize = 0;
1285 NetArpWaitPacketMAC = NULL;
1291 printf("Unexpected ARP opcode 0x%x\n", ntohs(arp->ar_op));
1298 puts ("Got RARP\n");
1301 if (len < ARP_HDR_SIZE) {
1302 printf("bad length %d < %d\n", len, ARP_HDR_SIZE);
1306 if ((ntohs(arp->ar_op) != RARPOP_REPLY) ||
1307 (ntohs(arp->ar_hrd) != ARP_ETHER) ||
1308 (ntohs(arp->ar_pro) != PROT_IP) ||
1309 (arp->ar_hln != 6) || (arp->ar_pln != 4)) {
1311 puts ("invalid RARP header\n");
1313 NetCopyIP(&NetOurIP, &arp->ar_data[16]);
1314 if (NetServerIP == 0)
1315 NetCopyIP(&NetServerIP, &arp->ar_data[ 6]);
1316 memcpy (NetServerEther, &arp->ar_data[ 0], 6);
1318 (*packetHandler)(0,0,0,0);
1326 if (len < IP_HDR_SIZE) {
1327 debug ("len bad %d < %d\n", len, IP_HDR_SIZE);
1330 if (len < ntohs(ip->ip_len)) {
1331 printf("len bad %d < %d\n", len, ntohs(ip->ip_len));
1334 len = ntohs(ip->ip_len);
1336 printf("len=%d, v=%02x\n", len, ip->ip_hl_v & 0xff);
1338 if ((ip->ip_hl_v & 0xf0) != 0x40) {
1341 if (ip->ip_off & htons(0x1fff)) { /* Can't deal w/ fragments */
1344 if (!NetCksumOk((uchar *)ip, IP_HDR_SIZE_NO_UDP / 2)) {
1345 puts ("checksum bad\n");
1348 tmp = NetReadIP(&ip->ip_dst);
1349 if (NetOurIP && tmp != NetOurIP && tmp != 0xFFFFFFFF) {
1353 * watch for ICMP host redirects
1355 * There is no real handler code (yet). We just watch
1356 * for ICMP host redirect messages. In case anybody
1357 * sees these messages: please contact me
1358 * (wd@denx.de), or - even better - send me the
1359 * necessary fixes :-)
1361 * Note: in all cases where I have seen this so far
1362 * it was a problem with the router configuration,
1363 * for instance when a router was configured in the
1364 * BOOTP reply, but the TFTP server was on the same
1365 * subnet. So this is probably a warning that your
1366 * configuration might be wrong. But I'm not really
1367 * sure if there aren't any other situations.
1369 if (ip->ip_p == IPPROTO_ICMP) {
1370 ICMP_t *icmph = (ICMP_t *)&(ip->udp_src);
1372 switch (icmph->type) {
1374 if (icmph->code != ICMP_REDIR_HOST)
1376 puts (" ICMP Host Redirect to ");
1377 print_IPaddr(icmph->un.gateway);
1380 #if (CONFIG_COMMANDS & CFG_CMD_PING)
1381 case ICMP_ECHO_REPLY:
1383 * IP header OK. Pass the packet to the current handler.
1385 /* XXX point to ip packet */
1386 (*packetHandler)((uchar *)ip, 0, 0, 0);
1392 } else if (ip->ip_p != IPPROTO_UDP) { /* Only UDP packets */
1396 #ifdef CONFIG_NETCONSOLE
1397 nc_input_packet((uchar *)ip +IP_HDR_SIZE,
1400 ntohs(ip->udp_len) - 8);
1403 * IP header OK. Pass the packet to the current handler.
1405 (*packetHandler)((uchar *)ip +IP_HDR_SIZE,
1408 ntohs(ip->udp_len) - 8);
1414 /**********************************************************************/
1416 static int net_check_prereq (proto_t protocol)
1420 #if (CONFIG_COMMANDS & CFG_CMD_PING)
1422 if (NetPingIP == 0) {
1423 puts ("*** ERROR: ping address not given\n");
1428 #if (CONFIG_COMMANDS & CFG_CMD_NFS)
1433 if (NetServerIP == 0) {
1434 puts ("*** ERROR: `serverip' not set\n");
1437 #if (CONFIG_COMMANDS & CFG_CMD_PING)
1441 if (NetOurIP == 0) {
1442 puts ("*** ERROR: `ipaddr' not set\n");
1451 if (memcmp (NetOurEther, "\0\0\0\0\0\0", 6) == 0) {
1452 #ifdef CONFIG_NET_MULTI
1453 extern int eth_get_dev_index (void);
1454 int num = eth_get_dev_index ();
1458 puts ("*** ERROR: No ethernet found.\n");
1461 puts ("*** ERROR: `ethaddr' not set\n");
1464 printf ("*** ERROR: `eth%daddr' not set\n",
1472 puts ("*** ERROR: `ethaddr' not set\n");
1480 return (0); /* OK */
1482 /**********************************************************************/
1485 NetCksumOk(uchar * ptr, int len)
1487 return !((NetCksum(ptr, len) + 1) & 0xfffe);
1492 NetCksum(uchar * ptr, int len)
1498 xsum += *((ushort *)ptr)++;
1499 xsum = (xsum & 0xffff) + (xsum >> 16);
1500 xsum = (xsum & 0xffff) + (xsum >> 16);
1501 return (xsum & 0xffff);
1509 myvlanid = ntohs(NetOurVLAN);
1510 if (myvlanid == (ushort)-1)
1511 myvlanid = VLAN_NONE;
1513 return ((myvlanid & VLAN_IDMASK) == VLAN_NONE) ? ETHER_HDR_SIZE : VLAN_ETHER_HDR_SIZE;
1517 NetSetEther(volatile uchar * xet, uchar * addr, uint prot)
1519 Ethernet_t *et = (Ethernet_t *)xet;
1522 myvlanid = ntohs(NetOurVLAN);
1523 if (myvlanid == (ushort)-1)
1524 myvlanid = VLAN_NONE;
1526 memcpy (et->et_dest, addr, 6);
1527 memcpy (et->et_src, NetOurEther, 6);
1528 if ((myvlanid & VLAN_IDMASK) == VLAN_NONE) {
1529 et->et_protlen = htons(prot);
1530 return ETHER_HDR_SIZE;
1532 VLAN_Ethernet_t *vet = (VLAN_Ethernet_t *)xet;
1534 vet->vet_vlan_type = htons(PROT_VLAN);
1535 vet->vet_tag = htons((0 << 5) | (myvlanid & VLAN_IDMASK));
1536 vet->vet_type = htons(prot);
1537 return VLAN_ETHER_HDR_SIZE;
1542 NetSetIP(volatile uchar * xip, IPaddr_t dest, int dport, int sport, int len)
1544 volatile IP_t *ip = (IP_t *)xip;
1547 * If the data is an odd number of bytes, zero the
1548 * byte after the last byte so that the checksum
1552 xip[IP_HDR_SIZE + len] = 0;
1555 * Construct an IP and UDP header.
1556 * (need to set no fragment bit - XXX)
1558 ip->ip_hl_v = 0x45; /* IP_HDR_SIZE / 4 (not including UDP) */
1560 ip->ip_len = htons(IP_HDR_SIZE + len);
1561 ip->ip_id = htons(NetIPID++);
1562 ip->ip_off = htons(0x4000); /* No fragmentation */
1564 ip->ip_p = 17; /* UDP */
1566 NetCopyIP((void*)&ip->ip_src, &NetOurIP); /* already in network byte order */
1567 NetCopyIP((void*)&ip->ip_dst, &dest); /* - "" - */
1568 ip->udp_src = htons(sport);
1569 ip->udp_dst = htons(dport);
1570 ip->udp_len = htons(8 + len);
1572 ip->ip_sum = ~NetCksum((uchar *)ip, IP_HDR_SIZE_NO_UDP / 2);
1575 void copy_filename (uchar *dst, uchar *src, int size)
1577 if (*src && (*src == '"')) {
1582 while ((--size > 0) && *src && (*src != '"')) {
1588 #endif /* CFG_CMD_NET */
1590 void ip_to_string (IPaddr_t x, char *s)
1593 sprintf (s, "%d.%d.%d.%d",
1594 (int) ((x >> 24) & 0xff),
1595 (int) ((x >> 16) & 0xff),
1596 (int) ((x >> 8) & 0xff), (int) ((x >> 0) & 0xff)
1600 IPaddr_t string_to_ip(char *s)
1609 for (addr=0, i=0; i<4; ++i) {
1610 ulong val = s ? simple_strtoul(s, &e, 10) : 0;
1612 addr |= (val & 0xFF);
1618 return (htonl(addr));
1621 void VLAN_to_string(ushort x, char *s)
1625 if (x == (ushort)-1)
1631 sprintf(s, "%d", x & VLAN_IDMASK);
1634 ushort string_to_VLAN(char *s)
1639 return htons(VLAN_NONE);
1641 if (*s < '0' || *s > '9')
1644 id = (ushort)simple_strtoul(s, NULL, 10);
1649 void print_IPaddr (IPaddr_t x)
1653 ip_to_string (x, tmp);
1658 IPaddr_t getenv_IPaddr (char *var)
1660 return (string_to_ip(getenv(var)));
1663 ushort getenv_VLAN(char *var)
1665 return (string_to_VLAN(getenv(var)));