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... */
385 NetServerIP = getenv_IPaddr ("serverip");
386 DhcpRequest(); /* Basically same as BOOTP */
388 #endif /* CFG_CMD_DHCP */
399 #if (CONFIG_COMMANDS & CFG_CMD_PING)
404 #if (CONFIG_COMMANDS & CFG_CMD_NFS)
409 #if (CONFIG_COMMANDS & CFG_CMD_CDP)
414 #ifdef CONFIG_NETCONSOLE
423 NetBootFileXferSize = 0;
427 #if defined(CONFIG_MII) || (CONFIG_COMMANDS & CFG_CMD_MII)
428 #if defined(CFG_FAULT_ECHO_LINK_DOWN) && defined(CONFIG_STATUS_LED) && defined(STATUS_LED_RED)
430 * Echo the inverted link state to the fault LED.
432 if(miiphy_link(CFG_FAULT_MII_ADDR)) {
433 status_led_set (STATUS_LED_RED, STATUS_LED_OFF);
435 status_led_set (STATUS_LED_RED, STATUS_LED_ON);
437 #endif /* CFG_FAULT_ECHO_LINK_DOWN, ... */
438 #endif /* CONFIG_MII, ... */
441 * Main packet reception loop. Loop receiving packets until
442 * someone sets `NetQuit'.
446 #ifdef CONFIG_SHOW_ACTIVITY
448 extern void show_activity(int arg);
453 * Check the ethernet for a new packet. The ethernet
454 * receive routine will process it.
459 * Abort if ctrl-c was pressed.
470 * Check for a timeout, and run the timeout handler
473 if (timeHandler && ((get_timer(0) - timeStart) > timeDelta)) {
476 #if defined(CONFIG_MII) || (CONFIG_COMMANDS & CFG_CMD_MII)
477 #if defined(CFG_FAULT_ECHO_LINK_DOWN) && defined(CONFIG_STATUS_LED) && defined(STATUS_LED_RED)
479 * Echo the inverted link state to the fault LED.
481 if(miiphy_link(CFG_FAULT_MII_ADDR)) {
482 status_led_set (STATUS_LED_RED, STATUS_LED_OFF);
484 status_led_set (STATUS_LED_RED, STATUS_LED_ON);
486 #endif /* CFG_FAULT_ECHO_LINK_DOWN, ... */
487 #endif /* CONFIG_MII, ... */
489 timeHandler = (thand_f *)0;
496 case NETLOOP_RESTART:
497 #ifdef CONFIG_NET_MULTI
502 case NETLOOP_SUCCESS:
503 if (NetBootFileXferSize > 0) {
505 printf("Bytes transferred = %ld (%lx hex)\n",
507 NetBootFileXferSize);
508 sprintf(buf, "%lx", NetBootFileXferSize);
509 setenv("filesize", buf);
511 sprintf(buf, "%lX", (unsigned long)load_addr);
512 setenv("fileaddr", buf);
515 return NetBootFileXferSize;
523 /**********************************************************************/
526 startAgainTimeout(void)
528 NetState = NETLOOP_RESTART;
532 startAgainHandler(uchar * pkt, unsigned dest, unsigned src, unsigned len)
534 /* Totally ignore the packet */
537 void NetStartAgain (void)
539 #ifdef CONFIG_NET_MULTI
540 DECLARE_GLOBAL_DATA_PTR;
543 int noretry = 0, once = 0;
545 if ((nretry = getenv ("netretry")) != NULL) {
546 noretry = (strcmp (nretry, "no") == 0);
547 once = (strcmp (nretry, "once") == 0);
551 NetState = NETLOOP_FAIL;
554 #ifndef CONFIG_NET_MULTI
555 NetSetTimeout (10 * CFG_HZ, startAgainTimeout);
556 NetSetHandler (startAgainHandler);
557 #else /* !CONFIG_NET_MULTI*/
559 eth_try_another (!NetRestarted);
561 if (NetRestartWrap) {
563 if (NetDevExists && !once) {
564 NetSetTimeout (10 * CFG_HZ, startAgainTimeout);
565 NetSetHandler (startAgainHandler);
567 NetState = NETLOOP_FAIL;
570 NetState = NETLOOP_RESTART;
572 #endif /* CONFIG_NET_MULTI */
575 /**********************************************************************/
581 NetSetHandler(rxhand_f * f)
588 NetSetTimeout(int iv, thand_f * f)
591 timeHandler = (thand_f *)0;
594 timeStart = get_timer(0);
601 NetSendPacket(volatile uchar * pkt, int len)
603 (void) eth_send(pkt, len);
607 NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport, int sport, int len)
611 /* convert to new style broadcast */
615 /* if broadcast, make the ether address a broadcast and don't do ARP */
616 if (dest == 0xFFFFFFFF)
617 ether = NetBcastAddr;
619 /* if MAC address was not discovered yet, save the packet and do an ARP request */
620 if (memcmp(ether, NetEtherNullAddr, 6) == 0) {
623 printf("sending ARP for %08lx\n", dest);
625 NetArpWaitPacketIP = dest;
626 NetArpWaitPacketMAC = ether;
628 pkt = NetArpWaitTxPacket;
629 pkt += NetSetEther (pkt, NetArpWaitPacketMAC, PROT_IP);
631 NetSetIP (pkt, dest, dport, sport, len);
632 memcpy(pkt + IP_HDR_SIZE, (uchar *)NetTxPacket + (pkt - (uchar *)NetArpWaitTxPacket) + IP_HDR_SIZE, len);
634 /* size of the waiting packet */
635 NetArpWaitTxPacketSize = (pkt - NetArpWaitTxPacket) + IP_HDR_SIZE + len;
637 /* and do the ARP request */
639 NetArpWaitTimerStart = get_timer(0);
641 return 1; /* waiting */
645 printf("sending UDP to %08lx/%02x:%02x:%02x:%02x:%02x:%02x\n",
646 dest, ether[0], ether[1], ether[2], ether[3], ether[4], ether[5]);
649 pkt = (uchar *)NetTxPacket;
650 pkt += NetSetEther (pkt, ether, PROT_IP);
651 NetSetIP (pkt, dest, dport, sport, len);
652 (void) eth_send(NetTxPacket, (pkt - NetTxPacket) + IP_HDR_SIZE + len);
654 return 0; /* transmitted */
657 #if (CONFIG_COMMANDS & CFG_CMD_PING)
658 static ushort PingSeqNo;
667 /* XXX always send arp request */
669 memcpy(mac, NetEtherNullAddr, 6);
672 printf("sending ARP for %08lx\n", NetPingIP);
675 NetArpWaitPacketIP = NetPingIP;
676 NetArpWaitPacketMAC = mac;
678 pkt = NetArpWaitTxPacket;
679 pkt += NetSetEther(pkt, mac, PROT_IP);
681 ip = (volatile IP_t *)pkt;
684 * Construct an IP and ICMP header. (need to set no fragment bit - XXX)
686 ip->ip_hl_v = 0x45; /* IP_HDR_SIZE / 4 (not including UDP) */
688 ip->ip_len = htons(IP_HDR_SIZE_NO_UDP + 8);
689 ip->ip_id = htons(NetIPID++);
690 ip->ip_off = htons(0x4000); /* No fragmentation */
692 ip->ip_p = 0x01; /* ICMP */
694 NetCopyIP((void*)&ip->ip_src, &NetOurIP); /* already in network byte order */
695 NetCopyIP((void*)&ip->ip_dst, &NetPingIP); /* - "" - */
696 ip->ip_sum = ~NetCksum((uchar *)ip, IP_HDR_SIZE_NO_UDP / 2);
698 s = &ip->udp_src; /* XXX ICMP starts here */
699 s[0] = htons(0x0800); /* echo-request, code */
700 s[1] = 0; /* checksum */
701 s[2] = 0; /* identifier */
702 s[3] = htons(PingSeqNo++); /* sequence number */
703 s[1] = ~NetCksum((uchar *)s, 8/2);
705 /* size of the waiting packet */
706 NetArpWaitTxPacketSize = (pkt - NetArpWaitTxPacket) + IP_HDR_SIZE_NO_UDP + 8;
708 /* and do the ARP request */
710 NetArpWaitTimerStart = get_timer(0);
712 return 1; /* waiting */
719 NetState = NETLOOP_FAIL; /* we did not get the reply */
723 PingHandler (uchar * pkt, unsigned dest, unsigned src, unsigned len)
726 volatile IP_t *ip = (volatile IP_t *)pkt;
728 tmp = NetReadIP((void *)&ip->ip_src);
729 if (tmp != NetPingIP)
732 NetState = NETLOOP_SUCCESS;
735 static void PingStart(void)
737 #if defined(CONFIG_NET_MULTI)
738 printf ("Using %s device\n", eth_get_name());
739 #endif /* CONFIG_NET_MULTI */
740 NetSetTimeout (10 * CFG_HZ, PingTimeout);
741 NetSetHandler (PingHandler);
745 #endif /* CFG_CMD_PING */
747 #if (CONFIG_COMMANDS & CFG_CMD_CDP)
749 #define CDP_DEVICE_ID_TLV 0x0001
750 #define CDP_ADDRESS_TLV 0x0002
751 #define CDP_PORT_ID_TLV 0x0003
752 #define CDP_CAPABILITIES_TLV 0x0004
753 #define CDP_VERSION_TLV 0x0005
754 #define CDP_PLATFORM_TLV 0x0006
755 #define CDP_NATIVE_VLAN_TLV 0x000a
756 #define CDP_APPLIANCE_VLAN_TLV 0x000e
757 #define CDP_TRIGGER_TLV 0x000f
758 #define CDP_POWER_CONSUMPTION_TLV 0x0010
759 #define CDP_SYSNAME_TLV 0x0014
760 #define CDP_SYSOBJECT_TLV 0x0015
761 #define CDP_MANAGEMENT_ADDRESS_TLV 0x0016
763 #define CDP_TIMEOUT (CFG_HZ/4) /* one packet every 250ms */
768 ushort CDPNativeVLAN;
769 ushort CDPApplianceVLAN;
771 static const uchar CDP_SNAP_hdr[8] = { 0xAA, 0xAA, 0x03, 0x00, 0x00, 0x0C, 0x20, 0x00 };
773 static ushort CDP_compute_csum(const uchar *buff, ushort len)
781 odd = 1 & (ulong)buff;
788 result += *((const ushort *)buff)++;
789 if (result & 0x80000000)
790 result = (result & 0xFFFF) + (result >> 16);
794 leftover = (signed short)(*(const signed char *)buff);
795 /* * XXX CISCO SUCKS big time! (and blows too) */
796 result = (result & 0xffff0000) | ((result + leftover) & 0x0000ffff);
799 result = (result & 0xFFFF) + (result >> 16);
802 result = ((result >> 8) & 0xff) | ((result & 0xff) << 8);
805 /* add up 16-bit and 17-bit words for 17+c bits */
806 result = (result & 0xffff) + (result >> 16);
807 /* add up 16-bit and 2-bit for 16+c bit */
808 result = (result & 0xffff) + (result >> 16);
810 result = (result & 0xffff) + (result >> 16);
813 csum = ~(ushort)result;
815 /* run time endian detection */
816 if (csum != htons(csum)) /* little endian */
822 int CDPSendTrigger(void)
830 #if defined(CONFIG_CDP_DEVICE_ID) || defined(CONFIG_CDP_PORT_ID) || \
831 defined(CONFIG_CDP_VERSION) || defined(CONFIG_CDP_PLATFORM)
836 et = (Ethernet_t *)pkt;
838 /* NOTE: trigger sent not on any VLAN */
840 /* form ethernet header */
841 memcpy(et->et_dest, NetCDPAddr, 6);
842 memcpy(et->et_src, NetOurEther, 6);
844 pkt += ETHER_HDR_SIZE;
847 memcpy((uchar *)pkt, CDP_SNAP_hdr, sizeof(CDP_SNAP_hdr));
848 pkt += sizeof(CDP_SNAP_hdr);
851 *pkt++ = 0x02; /* CDP version 2 */
852 *pkt++ = 180; /* TTL */
853 s = (volatile ushort *)pkt;
855 *s++ = htons(0); /* checksum (0 for later calculation) */
858 #ifdef CONFIG_CDP_DEVICE_ID
859 *s++ = htons(CDP_DEVICE_ID_TLV);
860 *s++ = htons(CONFIG_CDP_DEVICE_ID);
861 memset(buf, 0, sizeof(buf));
862 sprintf(buf, CONFIG_CDP_DEVICE_ID_PREFIX "%02X%02X%02X%02X%02X%02X",
863 NetOurEther[0] & 0xff, NetOurEther[1] & 0xff,
864 NetOurEther[2] & 0xff, NetOurEther[3] & 0xff,
865 NetOurEther[4] & 0xff, NetOurEther[5] & 0xff);
866 memcpy((uchar *)s, buf, 16);
870 #ifdef CONFIG_CDP_PORT_ID
871 *s++ = htons(CDP_PORT_ID_TLV);
872 memset(buf, 0, sizeof(buf));
873 sprintf(buf, CONFIG_CDP_PORT_ID, eth_get_dev_index());
875 if (len & 1) /* make it even */
877 *s++ = htons(len + 4);
878 memcpy((uchar *)s, buf, len);
882 #ifdef CONFIG_CDP_CAPABILITIES
883 *s++ = htons(CDP_CAPABILITIES_TLV);
885 *(ulong *)s = htonl(CONFIG_CDP_CAPABILITIES);
889 #ifdef CONFIG_CDP_VERSION
890 *s++ = htons(CDP_VERSION_TLV);
891 memset(buf, 0, sizeof(buf));
892 strcpy(buf, CONFIG_CDP_VERSION);
894 if (len & 1) /* make it even */
896 *s++ = htons(len + 4);
897 memcpy((uchar *)s, buf, len);
901 #ifdef CONFIG_CDP_PLATFORM
902 *s++ = htons(CDP_PLATFORM_TLV);
903 memset(buf, 0, sizeof(buf));
904 strcpy(buf, CONFIG_CDP_PLATFORM);
906 if (len & 1) /* make it even */
908 *s++ = htons(len + 4);
909 memcpy((uchar *)s, buf, len);
913 #ifdef CONFIG_CDP_TRIGGER
914 *s++ = htons(CDP_TRIGGER_TLV);
916 *(ulong *)s = htonl(CONFIG_CDP_TRIGGER);
920 #ifdef CONFIG_CDP_POWER_CONSUMPTION
921 *s++ = htons(CDP_POWER_CONSUMPTION_TLV);
923 *s++ = htons(CONFIG_CDP_POWER_CONSUMPTION);
926 /* length of ethernet packet */
927 len = (uchar *)s - ((uchar *)NetTxPacket + ETHER_HDR_SIZE);
928 et->et_protlen = htons(len);
930 len = ETHER_HDR_SIZE + sizeof(CDP_SNAP_hdr);
931 chksum = CDP_compute_csum((uchar *)NetTxPacket + len, (uchar *)s - (NetTxPacket + len));
936 (void) eth_send(NetTxPacket, (uchar *)s - NetTxPacket);
946 NetSetTimeout (CDP_TIMEOUT, CDPTimeout);
951 /* if not OK try again */
955 NetState = NETLOOP_SUCCESS;
959 CDPDummyHandler (uchar * pkt, unsigned dest, unsigned src, unsigned len)
965 CDPHandler(const uchar * pkt, unsigned len)
974 if (len < sizeof(CDP_SNAP_hdr) + 4)
977 /* check for valid CDP SNAP header */
978 if (memcmp(pkt, CDP_SNAP_hdr, sizeof(CDP_SNAP_hdr)) != 0)
981 pkt += sizeof(CDP_SNAP_hdr);
982 len -= sizeof(CDP_SNAP_hdr);
984 /* Version of CDP protocol must be >= 2 and TTL != 0 */
985 if (pkt[0] < 0x02 || pkt[1] == 0)
988 /* if version is greater than 0x02 maybe we'll have a problem; output a warning */
990 printf("** WARNING: CDP packet received with a protocol version %d > 2\n",
993 if (CDP_compute_csum(pkt, len) != 0)
1005 ss = (const ushort *)pkt;
1006 type = ntohs(ss[0]);
1007 tlen = ntohs(ss[1]);
1015 ss += 2; /* point ss to the data of the TLV */
1019 case CDP_DEVICE_ID_TLV:
1021 case CDP_ADDRESS_TLV:
1023 case CDP_PORT_ID_TLV:
1025 case CDP_CAPABILITIES_TLV:
1027 case CDP_VERSION_TLV:
1029 case CDP_PLATFORM_TLV:
1031 case CDP_NATIVE_VLAN_TLV:
1034 case CDP_APPLIANCE_VLAN_TLV:
1035 t = (const uchar *)ss;
1041 ss = (const ushort *)(t + 1);
1043 #ifdef CONFIG_CDP_APPLIANCE_VLAN_TYPE
1044 if (applid == CONFIG_CDP_APPLIANCE_VLAN_TYPE)
1047 vlan = ntohs(*ss); /* XXX will this work; dunno */
1052 case CDP_TRIGGER_TLV:
1054 case CDP_POWER_CONSUMPTION_TLV:
1056 case CDP_SYSNAME_TLV:
1058 case CDP_SYSOBJECT_TLV:
1060 case CDP_MANAGEMENT_ADDRESS_TLV:
1065 CDPApplianceVLAN = vlan;
1066 CDPNativeVLAN = nvlan;
1072 printf("** CDP packet is too short\n");
1076 static void CDPStart(void)
1078 #if defined(CONFIG_NET_MULTI)
1079 printf ("Using %s device\n", eth_get_name());
1084 CDPNativeVLAN = htons(-1);
1085 CDPApplianceVLAN = htons(-1);
1087 NetSetTimeout (CDP_TIMEOUT, CDPTimeout);
1088 NetSetHandler (CDPDummyHandler);
1092 #endif /* CFG_CMD_CDP */
1096 NetReceive(volatile uchar * inpkt, int len)
1104 #if (CONFIG_COMMANDS & CFG_CMD_CDP)
1107 ushort cti = 0, vlanid = VLAN_NONE, myvlanid, mynvlanid;
1110 printf("packet received\n");
1115 et = (Ethernet_t *)inpkt;
1117 /* too small packet? */
1118 if (len < ETHER_HDR_SIZE)
1121 #if (CONFIG_COMMANDS & CFG_CMD_CDP)
1122 /* keep track if packet is CDP */
1123 iscdp = memcmp(et->et_dest, NetCDPAddr, 6) == 0;
1126 myvlanid = ntohs(NetOurVLAN);
1127 if (myvlanid == (ushort)-1)
1128 myvlanid = VLAN_NONE;
1129 mynvlanid = ntohs(NetOurNativeVLAN);
1130 if (mynvlanid == (ushort)-1)
1131 mynvlanid = VLAN_NONE;
1133 x = ntohs(et->et_protlen);
1136 printf("packet received\n");
1141 * Got a 802 packet. Check the other protocol field.
1143 x = ntohs(et->et_prot);
1145 ip = (IP_t *)(inpkt + E802_HDR_SIZE);
1146 len -= E802_HDR_SIZE;
1148 } else if (x != PROT_VLAN) { /* normal packet */
1149 ip = (IP_t *)(inpkt + ETHER_HDR_SIZE);
1150 len -= ETHER_HDR_SIZE;
1152 } else { /* VLAN packet */
1153 VLAN_Ethernet_t *vet = (VLAN_Ethernet_t *)et;
1156 printf("VLAN packet received\n");
1158 /* too small packet? */
1159 if (len < VLAN_ETHER_HDR_SIZE)
1162 /* if no VLAN active */
1163 if ((ntohs(NetOurVLAN) & VLAN_IDMASK) == VLAN_NONE
1164 #if (CONFIG_COMMANDS & CFG_CMD_CDP)
1170 cti = ntohs(vet->vet_tag);
1171 vlanid = cti & VLAN_IDMASK;
1172 x = ntohs(vet->vet_type);
1174 ip = (IP_t *)(inpkt + VLAN_ETHER_HDR_SIZE);
1175 len -= VLAN_ETHER_HDR_SIZE;
1179 printf("Receive from protocol 0x%x\n", x);
1182 #if (CONFIG_COMMANDS & CFG_CMD_CDP)
1184 CDPHandler((uchar *)ip, len);
1189 if ((myvlanid & VLAN_IDMASK) != VLAN_NONE) {
1190 if (vlanid == VLAN_NONE)
1191 vlanid = (mynvlanid & VLAN_IDMASK);
1193 if (vlanid != (myvlanid & VLAN_IDMASK))
1201 * We have to deal with two types of ARP packets:
1202 * - REQUEST packets will be answered by sending our
1203 * IP address - if we know it.
1204 * - REPLY packates are expected only after we asked
1205 * for the TFTP server's or the gateway's ethernet
1206 * address; so if we receive such a packet, we set
1207 * the server ethernet address
1213 if (len < ARP_HDR_SIZE) {
1214 printf("bad length %d < %d\n", len, ARP_HDR_SIZE);
1217 if (ntohs(arp->ar_hrd) != ARP_ETHER) {
1220 if (ntohs(arp->ar_pro) != PROT_IP) {
1223 if (arp->ar_hln != 6) {
1226 if (arp->ar_pln != 4) {
1230 if (NetOurIP == 0) {
1234 if (NetReadIP(&arp->ar_data[16]) != NetOurIP) {
1238 switch (ntohs(arp->ar_op)) {
1239 case ARPOP_REQUEST: /* reply with our IP address */
1241 puts ("Got ARP REQUEST, return our IP\n");
1244 pkt += NetSetEther(pkt, et->et_src, PROT_ARP);
1245 arp->ar_op = htons(ARPOP_REPLY);
1246 memcpy (&arp->ar_data[10], &arp->ar_data[0], 6);
1247 NetCopyIP(&arp->ar_data[16], &arp->ar_data[6]);
1248 memcpy (&arp->ar_data[ 0], NetOurEther, 6);
1249 NetCopyIP(&arp->ar_data[ 6], &NetOurIP);
1250 (void) eth_send((uchar *)et, (pkt - (uchar *)et) + ARP_HDR_SIZE);
1253 case ARPOP_REPLY: /* arp reply */
1254 /* are we waiting for a reply */
1255 if (!NetArpWaitPacketIP || !NetArpWaitPacketMAC)
1258 printf("Got ARP REPLY, set server/gtwy eth addr (%02x:%02x:%02x:%02x:%02x:%02x)\n",
1259 arp->ar_data[0], arp->ar_data[1],
1260 arp->ar_data[2], arp->ar_data[3],
1261 arp->ar_data[4], arp->ar_data[5]);
1264 tmp = NetReadIP(&arp->ar_data[6]);
1266 /* matched waiting packet's address */
1267 if (tmp == NetArpWaitReplyIP) {
1271 /* save address for later use */
1272 memcpy(NetArpWaitPacketMAC, &arp->ar_data[0], 6);
1274 #ifdef CONFIG_NETCONSOLE
1275 (*packetHandler)(0,0,0,0);
1277 /* modify header, and transmit it */
1278 memcpy(((Ethernet_t *)NetArpWaitTxPacket)->et_dest, NetArpWaitPacketMAC, 6);
1279 (void) eth_send(NetArpWaitTxPacket, NetArpWaitTxPacketSize);
1281 /* no arp request pending now */
1282 NetArpWaitPacketIP = 0;
1283 NetArpWaitTxPacketSize = 0;
1284 NetArpWaitPacketMAC = NULL;
1290 printf("Unexpected ARP opcode 0x%x\n", ntohs(arp->ar_op));
1297 puts ("Got RARP\n");
1300 if (len < ARP_HDR_SIZE) {
1301 printf("bad length %d < %d\n", len, ARP_HDR_SIZE);
1305 if ((ntohs(arp->ar_op) != RARPOP_REPLY) ||
1306 (ntohs(arp->ar_hrd) != ARP_ETHER) ||
1307 (ntohs(arp->ar_pro) != PROT_IP) ||
1308 (arp->ar_hln != 6) || (arp->ar_pln != 4)) {
1310 puts ("invalid RARP header\n");
1312 NetCopyIP(&NetOurIP, &arp->ar_data[16]);
1313 if (NetServerIP == 0)
1314 NetCopyIP(&NetServerIP, &arp->ar_data[ 6]);
1315 memcpy (NetServerEther, &arp->ar_data[ 0], 6);
1317 (*packetHandler)(0,0,0,0);
1325 if (len < IP_HDR_SIZE) {
1326 debug ("len bad %d < %d\n", len, IP_HDR_SIZE);
1329 if (len < ntohs(ip->ip_len)) {
1330 printf("len bad %d < %d\n", len, ntohs(ip->ip_len));
1333 len = ntohs(ip->ip_len);
1335 printf("len=%d, v=%02x\n", len, ip->ip_hl_v & 0xff);
1337 if ((ip->ip_hl_v & 0xf0) != 0x40) {
1340 if (ip->ip_off & htons(0x1fff)) { /* Can't deal w/ fragments */
1343 if (!NetCksumOk((uchar *)ip, IP_HDR_SIZE_NO_UDP / 2)) {
1344 puts ("checksum bad\n");
1347 tmp = NetReadIP(&ip->ip_dst);
1348 if (NetOurIP && tmp != NetOurIP && tmp != 0xFFFFFFFF) {
1352 * watch for ICMP host redirects
1354 * There is no real handler code (yet). We just watch
1355 * for ICMP host redirect messages. In case anybody
1356 * sees these messages: please contact me
1357 * (wd@denx.de), or - even better - send me the
1358 * necessary fixes :-)
1360 * Note: in all cases where I have seen this so far
1361 * it was a problem with the router configuration,
1362 * for instance when a router was configured in the
1363 * BOOTP reply, but the TFTP server was on the same
1364 * subnet. So this is probably a warning that your
1365 * configuration might be wrong. But I'm not really
1366 * sure if there aren't any other situations.
1368 if (ip->ip_p == IPPROTO_ICMP) {
1369 ICMP_t *icmph = (ICMP_t *)&(ip->udp_src);
1371 switch (icmph->type) {
1373 if (icmph->code != ICMP_REDIR_HOST)
1375 puts (" ICMP Host Redirect to ");
1376 print_IPaddr(icmph->un.gateway);
1379 #if (CONFIG_COMMANDS & CFG_CMD_PING)
1380 case ICMP_ECHO_REPLY:
1382 * IP header OK. Pass the packet to the current handler.
1384 /* XXX point to ip packet */
1385 (*packetHandler)((uchar *)ip, 0, 0, 0);
1391 } else if (ip->ip_p != IPPROTO_UDP) { /* Only UDP packets */
1395 #ifdef CONFIG_NETCONSOLE
1396 nc_input_packet((uchar *)ip +IP_HDR_SIZE,
1399 ntohs(ip->udp_len) - 8);
1402 * IP header OK. Pass the packet to the current handler.
1404 (*packetHandler)((uchar *)ip +IP_HDR_SIZE,
1407 ntohs(ip->udp_len) - 8);
1413 /**********************************************************************/
1415 static int net_check_prereq (proto_t protocol)
1419 #if (CONFIG_COMMANDS & CFG_CMD_PING)
1421 if (NetPingIP == 0) {
1422 puts ("*** ERROR: ping address not given\n");
1427 #if (CONFIG_COMMANDS & CFG_CMD_NFS)
1432 if (NetServerIP == 0) {
1433 puts ("*** ERROR: `serverip' not set\n");
1436 #if (CONFIG_COMMANDS & CFG_CMD_PING)
1440 if (NetOurIP == 0) {
1441 puts ("*** ERROR: `ipaddr' not set\n");
1450 if (memcmp (NetOurEther, "\0\0\0\0\0\0", 6) == 0) {
1451 #ifdef CONFIG_NET_MULTI
1452 extern int eth_get_dev_index (void);
1453 int num = eth_get_dev_index ();
1457 puts ("*** ERROR: No ethernet found.\n");
1460 puts ("*** ERROR: `ethaddr' not set\n");
1463 printf ("*** ERROR: `eth%daddr' not set\n",
1471 puts ("*** ERROR: `ethaddr' not set\n");
1479 return (0); /* OK */
1481 /**********************************************************************/
1484 NetCksumOk(uchar * ptr, int len)
1486 return !((NetCksum(ptr, len) + 1) & 0xfffe);
1491 NetCksum(uchar * ptr, int len)
1497 xsum += *((ushort *)ptr)++;
1498 xsum = (xsum & 0xffff) + (xsum >> 16);
1499 xsum = (xsum & 0xffff) + (xsum >> 16);
1500 return (xsum & 0xffff);
1508 myvlanid = ntohs(NetOurVLAN);
1509 if (myvlanid == (ushort)-1)
1510 myvlanid = VLAN_NONE;
1512 return ((myvlanid & VLAN_IDMASK) == VLAN_NONE) ? ETHER_HDR_SIZE : VLAN_ETHER_HDR_SIZE;
1516 NetSetEther(volatile uchar * xet, uchar * addr, uint prot)
1518 Ethernet_t *et = (Ethernet_t *)xet;
1521 myvlanid = ntohs(NetOurVLAN);
1522 if (myvlanid == (ushort)-1)
1523 myvlanid = VLAN_NONE;
1525 memcpy (et->et_dest, addr, 6);
1526 memcpy (et->et_src, NetOurEther, 6);
1527 if ((myvlanid & VLAN_IDMASK) == VLAN_NONE) {
1528 et->et_protlen = htons(prot);
1529 return ETHER_HDR_SIZE;
1531 VLAN_Ethernet_t *vet = (VLAN_Ethernet_t *)xet;
1533 vet->vet_vlan_type = htons(PROT_VLAN);
1534 vet->vet_tag = htons((0 << 5) | (myvlanid & VLAN_IDMASK));
1535 vet->vet_type = htons(prot);
1536 return VLAN_ETHER_HDR_SIZE;
1541 NetSetIP(volatile uchar * xip, IPaddr_t dest, int dport, int sport, int len)
1543 volatile IP_t *ip = (IP_t *)xip;
1546 * If the data is an odd number of bytes, zero the
1547 * byte after the last byte so that the checksum
1551 xip[IP_HDR_SIZE + len] = 0;
1554 * Construct an IP and UDP header.
1555 * (need to set no fragment bit - XXX)
1557 ip->ip_hl_v = 0x45; /* IP_HDR_SIZE / 4 (not including UDP) */
1559 ip->ip_len = htons(IP_HDR_SIZE + len);
1560 ip->ip_id = htons(NetIPID++);
1561 ip->ip_off = htons(0x4000); /* No fragmentation */
1563 ip->ip_p = 17; /* UDP */
1565 NetCopyIP((void*)&ip->ip_src, &NetOurIP); /* already in network byte order */
1566 NetCopyIP((void*)&ip->ip_dst, &dest); /* - "" - */
1567 ip->udp_src = htons(sport);
1568 ip->udp_dst = htons(dport);
1569 ip->udp_len = htons(8 + len);
1571 ip->ip_sum = ~NetCksum((uchar *)ip, IP_HDR_SIZE_NO_UDP / 2);
1574 void copy_filename (uchar *dst, uchar *src, int size)
1576 if (*src && (*src == '"')) {
1581 while ((--size > 0) && *src && (*src != '"')) {
1587 #endif /* CFG_CMD_NET */
1589 void ip_to_string (IPaddr_t x, char *s)
1592 sprintf (s, "%d.%d.%d.%d",
1593 (int) ((x >> 24) & 0xff),
1594 (int) ((x >> 16) & 0xff),
1595 (int) ((x >> 8) & 0xff), (int) ((x >> 0) & 0xff)
1599 IPaddr_t string_to_ip(char *s)
1608 for (addr=0, i=0; i<4; ++i) {
1609 ulong val = s ? simple_strtoul(s, &e, 10) : 0;
1611 addr |= (val & 0xFF);
1617 return (htonl(addr));
1620 void VLAN_to_string(ushort x, char *s)
1624 if (x == (ushort)-1)
1630 sprintf(s, "%d", x & VLAN_IDMASK);
1633 ushort string_to_VLAN(char *s)
1638 return htons(VLAN_NONE);
1640 if (*s < '0' || *s > '9')
1643 id = (ushort)simple_strtoul(s, NULL, 10);
1648 void print_IPaddr (IPaddr_t x)
1652 ip_to_string (x, tmp);
1657 IPaddr_t getenv_IPaddr (char *var)
1659 return (string_to_ip(getenv(var)));
1662 ushort getenv_VLAN(char *var)
1664 return (string_to_VLAN(getenv(var)));