2 * Copied from Linux Monitor (LiMon) - Networking.
4 * Copyright 1994 - 2000 Neil Russell.
6 * Copyright 2000 Roland Borde
7 * Copyright 2000 Paolo Scaffardi
8 * Copyright 2000-2002 Wolfgang Denk, wd@denx.de
14 * The user interface supports commands for BOOTP, RARP, and TFTP.
15 * Also, we support ARP internally. Depending on available data,
16 * these interact as follows:
20 * Prerequisites: - own ethernet address
21 * We want: - own IP address
22 * - TFTP server IP address
28 * Prerequisites: - own ethernet address
29 * We want: - own IP address
30 * - TFTP server IP address
35 * Prerequisites: - own ethernet address
37 * - TFTP server IP address
38 * We want: - TFTP server ethernet address
43 * Prerequisites: - own ethernet address
44 * We want: - IP, Netmask, ServerIP, Gateway IP
45 * - bootfilename, lease time
50 * Prerequisites: - own ethernet address
52 * - TFTP server IP address
53 * - TFTP server ethernet address
54 * - name of bootfile (if unknown, we use a default name
55 * derived from our own IP address)
56 * We want: - load the boot file
61 * Prerequisites: - own ethernet address
63 * - name of bootfile (if unknown, we use a default name
64 * derived from our own IP address)
65 * We want: - load the boot file
70 * Prerequisites: - own ethernet address
72 * We want: - network time
85 #ifdef CONFIG_STATUS_LED
86 #include <status_led.h>
89 #if defined(CONFIG_CMD_SNTP)
93 #if defined(CONFIG_CMD_NET)
95 DECLARE_GLOBAL_DATA_PTR
;
97 #ifndef CONFIG_ARP_TIMEOUT
98 # define ARP_TIMEOUT 50UL /* Deciseconds before trying ARP again */
99 #elif (CONFIG_ARP_TIMEOUT < 100)
100 # error "Due to possible overflow CONFIG_ARP_TIMEOUT must be greater than 100ms"
102 # if (CONFIG_ARP_TIMEOUT % 100)
103 # warning "Supported ARP_TIMEOUT precision is 100ms"
105 # define ARP_TIMEOUT (CONFIG_ARP_TIMEOUT / 100)
109 #ifndef CONFIG_NET_RETRY_COUNT
110 # define ARP_TIMEOUT_COUNT 5 /* # of timeouts before giving up */
112 # define ARP_TIMEOUT_COUNT CONFIG_NET_RETRY_COUNT
119 /** BOOTP EXTENTIONS **/
121 IPaddr_t NetOurSubnetMask
=0; /* Our subnet mask (0=unknown) */
122 IPaddr_t NetOurGatewayIP
=0; /* Our gateways IP address */
123 IPaddr_t NetOurDNSIP
=0; /* Our DNS IP address */
124 #if defined(CONFIG_BOOTP_DNS2)
125 IPaddr_t NetOurDNS2IP
=0; /* Our 2nd DNS IP address */
127 char NetOurNISDomain
[32]={0,}; /* Our NIS domain */
128 char NetOurHostName
[32]={0,}; /* Our hostname */
129 char NetOurRootPath
[64]={0,}; /* Our bootpath */
130 ushort NetBootFileSize
=0; /* Our bootfile size in blocks */
132 #ifdef CONFIG_MCAST_TFTP /* Multicast TFTP */
136 /** END OF BOOTP EXTENTIONS **/
138 ulong NetBootFileXferSize
; /* The actual transferred size of the bootfile (in bytes) */
139 uchar NetOurEther
[6]; /* Our ethernet address */
140 uchar NetServerEther
[6] = /* Boot server enet address */
141 { 0, 0, 0, 0, 0, 0 };
142 IPaddr_t NetOurIP
; /* Our IP addr (0 = unknown) */
143 IPaddr_t NetServerIP
; /* Server IP addr (0 = unknown) */
144 volatile uchar
*NetRxPkt
; /* Current receive packet */
145 int NetRxPktLen
; /* Current rx packet length */
146 unsigned NetIPID
; /* IP packet ID */
147 uchar NetBcastAddr
[6] = /* Ethernet bcast address */
148 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
149 uchar NetEtherNullAddr
[6] =
150 { 0, 0, 0, 0, 0, 0 };
152 void (*push_packet
)(volatile void *, int len
) = 0;
154 #if defined(CONFIG_CMD_CDP)
155 uchar NetCDPAddr
[6] = /* Ethernet bcast address */
156 { 0x01, 0x00, 0x0c, 0xcc, 0xcc, 0xcc };
158 int NetState
; /* Network loop state */
159 #ifdef CONFIG_NET_MULTI
160 int NetRestartWrap
= 0; /* Tried all network devices */
161 static int NetRestarted
= 0; /* Network loop restarted */
162 static int NetDevExists
= 0; /* At least one device configured */
165 /* XXX in both little & big endian machines 0xFFFF == ntohs(-1) */
166 ushort NetOurVLAN
= 0xFFFF; /* default is without VLAN */
167 ushort NetOurNativeVLAN
= 0xFFFF; /* ditto */
169 char BootFile
[128]; /* Boot File name */
171 #if defined(CONFIG_CMD_PING)
172 IPaddr_t NetPingIP
; /* the ip address to ping */
174 static void PingStart(void);
177 #if defined(CONFIG_CMD_CDP)
178 static void CDPStart(void);
181 #if defined(CONFIG_CMD_SNTP)
182 IPaddr_t NetNtpServerIP
; /* NTP server IP address */
183 int NetTimeOffset
=0; /* offset time from UTC */
186 #ifdef CONFIG_NETCONSOLE
188 int nc_input_packet(uchar
*pkt
, unsigned dest
, unsigned src
, unsigned len
);
191 volatile uchar PktBuf
[(PKTBUFSRX
+1) * PKTSIZE_ALIGN
+ PKTALIGN
];
193 volatile uchar
*NetRxPackets
[PKTBUFSRX
]; /* Receive packets */
195 static rxhand_f
*packetHandler
; /* Current RX packet handler */
196 static thand_f
*timeHandler
; /* Current timeout handler */
197 static ulong timeStart
; /* Time base value */
198 static ulong timeDelta
; /* Current timeout value */
199 volatile uchar
*NetTxPacket
= 0; /* THE transmit packet */
201 static int net_check_prereq (proto_t protocol
);
203 /**********************************************************************/
205 IPaddr_t NetArpWaitPacketIP
;
206 IPaddr_t NetArpWaitReplyIP
;
207 uchar
*NetArpWaitPacketMAC
; /* MAC address of waiting packet's destination */
208 uchar
*NetArpWaitTxPacket
; /* THE transmit packet */
209 int NetArpWaitTxPacketSize
;
210 uchar NetArpWaitPacketBuf
[PKTSIZE_ALIGN
+ PKTALIGN
];
211 ulong NetArpWaitTimerStart
;
214 void ArpRequest (void)
221 printf ("ARP broadcast %d\n", NetArpWaitTry
);
225 pkt
+= NetSetEther (pkt
, NetBcastAddr
, PROT_ARP
);
229 arp
->ar_hrd
= htons (ARP_ETHER
);
230 arp
->ar_pro
= htons (PROT_IP
);
233 arp
->ar_op
= htons (ARPOP_REQUEST
);
235 memcpy (&arp
->ar_data
[0], NetOurEther
, 6); /* source ET addr */
236 NetWriteIP ((uchar
*) & arp
->ar_data
[6], NetOurIP
); /* source IP addr */
237 for (i
= 10; i
< 16; ++i
) {
238 arp
->ar_data
[i
] = 0; /* dest ET addr = 0 */
241 if ((NetArpWaitPacketIP
& NetOurSubnetMask
) !=
242 (NetOurIP
& NetOurSubnetMask
)) {
243 if (NetOurGatewayIP
== 0) {
244 puts ("## Warning: gatewayip needed but not set\n");
245 NetArpWaitReplyIP
= NetArpWaitPacketIP
;
247 NetArpWaitReplyIP
= NetOurGatewayIP
;
250 NetArpWaitReplyIP
= NetArpWaitPacketIP
;
253 NetWriteIP ((uchar
*) & arp
->ar_data
[16], NetArpWaitReplyIP
);
254 (void) eth_send (NetTxPacket
, (pkt
- NetTxPacket
) + ARP_HDR_SIZE
);
257 void ArpTimeoutCheck(void)
261 if (!NetArpWaitPacketIP
)
266 /* check for arp timeout */
267 if ((t
- NetArpWaitTimerStart
) > ARP_TIMEOUT
* CFG_HZ
/ 10) {
270 if (NetArpWaitTry
>= ARP_TIMEOUT_COUNT
) {
271 puts ("\nARP Retry count exceeded; starting again\n");
275 NetArpWaitTimerStart
= t
;
281 /**********************************************************************/
283 * Main network processing loop.
287 NetLoop(proto_t protocol
)
291 #ifdef CONFIG_NET_MULTI
296 /* XXX problem with bss workaround */
297 NetArpWaitPacketMAC
= NULL
;
298 NetArpWaitTxPacket
= NULL
;
299 NetArpWaitPacketIP
= 0;
300 NetArpWaitReplyIP
= 0;
301 NetArpWaitTxPacket
= NULL
;
307 * Setup packet buffers, aligned correctly.
309 NetTxPacket
= &PktBuf
[0] + (PKTALIGN
- 1);
310 NetTxPacket
-= (ulong
)NetTxPacket
% PKTALIGN
;
311 for (i
= 0; i
< PKTBUFSRX
; i
++) {
312 NetRxPackets
[i
] = NetTxPacket
+ (i
+1)*PKTSIZE_ALIGN
;
316 if (!NetArpWaitTxPacket
) {
317 NetArpWaitTxPacket
= &NetArpWaitPacketBuf
[0] + (PKTALIGN
- 1);
318 NetArpWaitTxPacket
-= (ulong
)NetArpWaitTxPacket
% PKTALIGN
;
319 NetArpWaitTxPacketSize
= 0;
323 #ifdef CONFIG_NET_MULTI
326 if (eth_init(bd
) < 0) {
332 #ifdef CONFIG_NET_MULTI
333 memcpy (NetOurEther
, eth_get_dev()->enetaddr
, 6);
335 memcpy (NetOurEther
, bd
->bi_enetaddr
, 6);
338 NetState
= NETLOOP_CONTINUE
;
341 * Start the ball rolling with the given start function. From
342 * here on, this code is a state machine driven by received
343 * packets and timer events.
347 #if defined(CONFIG_CMD_NFS)
350 #if defined(CONFIG_CMD_PING)
353 #if defined(CONFIG_CMD_SNTP)
358 NetCopyIP(&NetOurIP
, &bd
->bi_ip_addr
);
359 NetOurGatewayIP
= getenv_IPaddr ("gatewayip");
360 NetOurSubnetMask
= getenv_IPaddr ("netmask");
361 NetOurVLAN
= getenv_VLAN("vlan");
362 NetOurNativeVLAN
= getenv_VLAN("nvlan");
365 #if defined(CONFIG_CMD_NFS)
370 NetServerIP
= getenv_IPaddr ("serverip");
372 #if defined(CONFIG_CMD_PING)
377 #if defined(CONFIG_CMD_SNTP)
390 * initialize our IP addr to 0 in order to accept ANY
391 * IP addr assigned to us by the BOOTP / RARP server
394 NetServerIP
= getenv_IPaddr ("serverip");
395 NetOurVLAN
= getenv_VLAN("vlan"); /* VLANs must be read */
396 NetOurNativeVLAN
= getenv_VLAN("nvlan");
398 NetOurVLAN
= getenv_VLAN("vlan"); /* VLANs must be read */
399 NetOurNativeVLAN
= getenv_VLAN("nvlan");
405 switch (net_check_prereq (protocol
)) {
407 /* network not configured */
411 #ifdef CONFIG_NET_MULTI
413 /* network device not configured */
415 #endif /* CONFIG_NET_MULTI */
418 #ifdef CONFIG_NET_MULTI
423 /* always use ARP to get server ethernet address */
427 #if defined(CONFIG_CMD_DHCP)
429 /* Start with a clean slate... */
432 NetServerIP
= getenv_IPaddr ("serverip");
433 DhcpRequest(); /* Basically same as BOOTP */
446 #if defined(CONFIG_CMD_PING)
451 #if defined(CONFIG_CMD_NFS)
456 #if defined(CONFIG_CMD_CDP)
461 #ifdef CONFIG_NETCONSOLE
466 #if defined(CONFIG_CMD_SNTP)
475 NetBootFileXferSize
= 0;
479 #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
480 #if defined(CFG_FAULT_ECHO_LINK_DOWN) && defined(CONFIG_STATUS_LED) && defined(STATUS_LED_RED)
482 * Echo the inverted link state to the fault LED.
484 if(miiphy_link(eth_get_dev()->name
, CFG_FAULT_MII_ADDR
)) {
485 status_led_set (STATUS_LED_RED
, STATUS_LED_OFF
);
487 status_led_set (STATUS_LED_RED
, STATUS_LED_ON
);
489 #endif /* CFG_FAULT_ECHO_LINK_DOWN, ... */
490 #endif /* CONFIG_MII, ... */
493 * Main packet reception loop. Loop receiving packets until
494 * someone sets `NetState' to a state that terminates.
498 #ifdef CONFIG_SHOW_ACTIVITY
500 extern void show_activity(int arg
);
505 * Check the ethernet for a new packet. The ethernet
506 * receive routine will process it.
511 * Abort if ctrl-c was pressed.
522 * Check for a timeout, and run the timeout handler
525 if (timeHandler
&& ((get_timer(0) - timeStart
) > timeDelta
)) {
528 #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
529 # if defined(CFG_FAULT_ECHO_LINK_DOWN) && \
530 defined(CONFIG_STATUS_LED) && \
531 defined(STATUS_LED_RED)
533 * Echo the inverted link state to the fault LED.
535 if(miiphy_link(eth_get_dev()->name
, CFG_FAULT_MII_ADDR
)) {
536 status_led_set (STATUS_LED_RED
, STATUS_LED_OFF
);
538 status_led_set (STATUS_LED_RED
, STATUS_LED_ON
);
540 # endif /* CFG_FAULT_ECHO_LINK_DOWN, ... */
541 #endif /* CONFIG_MII, ... */
543 timeHandler
= (thand_f
*)0;
550 case NETLOOP_RESTART
:
551 #ifdef CONFIG_NET_MULTI
556 case NETLOOP_SUCCESS
:
557 if (NetBootFileXferSize
> 0) {
559 printf("Bytes transferred = %ld (%lx hex)\n",
561 NetBootFileXferSize
);
562 sprintf(buf
, "%lX", NetBootFileXferSize
);
563 setenv("filesize", buf
);
565 sprintf(buf
, "%lX", (unsigned long)load_addr
);
566 setenv("fileaddr", buf
);
569 return NetBootFileXferSize
;
577 /**********************************************************************/
580 startAgainTimeout(void)
582 NetState
= NETLOOP_RESTART
;
586 startAgainHandler(uchar
* pkt
, unsigned dest
, unsigned src
, unsigned len
)
588 /* Totally ignore the packet */
591 void NetStartAgain (void)
594 int noretry
= 0, once
= 0;
596 if ((nretry
= getenv ("netretry")) != NULL
) {
597 noretry
= (strcmp (nretry
, "no") == 0);
598 once
= (strcmp (nretry
, "once") == 0);
602 NetState
= NETLOOP_FAIL
;
605 #ifndef CONFIG_NET_MULTI
606 NetSetTimeout (10UL * CFG_HZ
, startAgainTimeout
);
607 NetSetHandler (startAgainHandler
);
608 #else /* !CONFIG_NET_MULTI*/
610 #if !defined(CONFIG_NET_DO_NOT_TRY_ANOTHER)
611 eth_try_another (!NetRestarted
);
614 if (NetRestartWrap
) {
616 if (NetDevExists
&& !once
) {
617 NetSetTimeout (10UL * CFG_HZ
, startAgainTimeout
);
618 NetSetHandler (startAgainHandler
);
620 NetState
= NETLOOP_FAIL
;
623 NetState
= NETLOOP_RESTART
;
625 #endif /* CONFIG_NET_MULTI */
628 /**********************************************************************/
634 NetSetHandler(rxhand_f
* f
)
641 NetSetTimeout(ulong iv
, thand_f
* f
)
644 timeHandler
= (thand_f
*)0;
647 timeStart
= get_timer(0);
654 NetSendPacket(volatile uchar
* pkt
, int len
)
656 (void) eth_send(pkt
, len
);
660 NetSendUDPPacket(uchar
*ether
, IPaddr_t dest
, int dport
, int sport
, int len
)
664 /* convert to new style broadcast */
668 /* if broadcast, make the ether address a broadcast and don't do ARP */
669 if (dest
== 0xFFFFFFFF)
670 ether
= NetBcastAddr
;
672 /* if MAC address was not discovered yet, save the packet and do an ARP request */
673 if (memcmp(ether
, NetEtherNullAddr
, 6) == 0) {
676 printf("sending ARP for %08lx\n", dest
);
678 NetArpWaitPacketIP
= dest
;
679 NetArpWaitPacketMAC
= ether
;
681 pkt
= NetArpWaitTxPacket
;
682 pkt
+= NetSetEther (pkt
, NetArpWaitPacketMAC
, PROT_IP
);
684 NetSetIP (pkt
, dest
, dport
, sport
, len
);
685 memcpy(pkt
+ IP_HDR_SIZE
, (uchar
*)NetTxPacket
+ (pkt
- (uchar
*)NetArpWaitTxPacket
) + IP_HDR_SIZE
, len
);
687 /* size of the waiting packet */
688 NetArpWaitTxPacketSize
= (pkt
- NetArpWaitTxPacket
) + IP_HDR_SIZE
+ len
;
690 /* and do the ARP request */
692 NetArpWaitTimerStart
= get_timer(0);
694 return 1; /* waiting */
698 printf("sending UDP to %08lx/%02x:%02x:%02x:%02x:%02x:%02x\n",
699 dest
, ether
[0], ether
[1], ether
[2], ether
[3], ether
[4], ether
[5]);
702 pkt
= (uchar
*)NetTxPacket
;
703 pkt
+= NetSetEther (pkt
, ether
, PROT_IP
);
704 NetSetIP (pkt
, dest
, dport
, sport
, len
);
705 (void) eth_send(NetTxPacket
, (pkt
- NetTxPacket
) + IP_HDR_SIZE
+ len
);
707 return 0; /* transmitted */
710 #if defined(CONFIG_CMD_PING)
711 static ushort PingSeqNo
;
720 /* XXX always send arp request */
722 memcpy(mac
, NetEtherNullAddr
, 6);
725 printf("sending ARP for %08lx\n", NetPingIP
);
728 NetArpWaitPacketIP
= NetPingIP
;
729 NetArpWaitPacketMAC
= mac
;
731 pkt
= NetArpWaitTxPacket
;
732 pkt
+= NetSetEther(pkt
, mac
, PROT_IP
);
734 ip
= (volatile IP_t
*)pkt
;
737 * Construct an IP and ICMP header. (need to set no fragment bit - XXX)
739 ip
->ip_hl_v
= 0x45; /* IP_HDR_SIZE / 4 (not including UDP) */
741 ip
->ip_len
= htons(IP_HDR_SIZE_NO_UDP
+ 8);
742 ip
->ip_id
= htons(NetIPID
++);
743 ip
->ip_off
= htons(0x4000); /* No fragmentation */
745 ip
->ip_p
= 0x01; /* ICMP */
747 NetCopyIP((void*)&ip
->ip_src
, &NetOurIP
); /* already in network byte order */
748 NetCopyIP((void*)&ip
->ip_dst
, &NetPingIP
); /* - "" - */
749 ip
->ip_sum
= ~NetCksum((uchar
*)ip
, IP_HDR_SIZE_NO_UDP
/ 2);
751 s
= &ip
->udp_src
; /* XXX ICMP starts here */
752 s
[0] = htons(0x0800); /* echo-request, code */
753 s
[1] = 0; /* checksum */
754 s
[2] = 0; /* identifier */
755 s
[3] = htons(PingSeqNo
++); /* sequence number */
756 s
[1] = ~NetCksum((uchar
*)s
, 8/2);
758 /* size of the waiting packet */
759 NetArpWaitTxPacketSize
= (pkt
- NetArpWaitTxPacket
) + IP_HDR_SIZE_NO_UDP
+ 8;
761 /* and do the ARP request */
763 NetArpWaitTimerStart
= get_timer(0);
765 return 1; /* waiting */
772 NetState
= NETLOOP_FAIL
; /* we did not get the reply */
776 PingHandler (uchar
* pkt
, unsigned dest
, unsigned src
, unsigned len
)
779 volatile IP_t
*ip
= (volatile IP_t
*)pkt
;
781 tmp
= NetReadIP((void *)&ip
->ip_src
);
782 if (tmp
!= NetPingIP
)
785 NetState
= NETLOOP_SUCCESS
;
788 static void PingStart(void)
790 #if defined(CONFIG_NET_MULTI)
791 printf ("Using %s device\n", eth_get_name());
792 #endif /* CONFIG_NET_MULTI */
793 NetSetTimeout (10UL * CFG_HZ
, PingTimeout
);
794 NetSetHandler (PingHandler
);
800 #if defined(CONFIG_CMD_CDP)
802 #define CDP_DEVICE_ID_TLV 0x0001
803 #define CDP_ADDRESS_TLV 0x0002
804 #define CDP_PORT_ID_TLV 0x0003
805 #define CDP_CAPABILITIES_TLV 0x0004
806 #define CDP_VERSION_TLV 0x0005
807 #define CDP_PLATFORM_TLV 0x0006
808 #define CDP_NATIVE_VLAN_TLV 0x000a
809 #define CDP_APPLIANCE_VLAN_TLV 0x000e
810 #define CDP_TRIGGER_TLV 0x000f
811 #define CDP_POWER_CONSUMPTION_TLV 0x0010
812 #define CDP_SYSNAME_TLV 0x0014
813 #define CDP_SYSOBJECT_TLV 0x0015
814 #define CDP_MANAGEMENT_ADDRESS_TLV 0x0016
816 #define CDP_TIMEOUT (CFG_HZ/4) /* one packet every 250ms */
821 ushort CDPNativeVLAN
;
822 ushort CDPApplianceVLAN
;
824 static const uchar CDP_SNAP_hdr
[8] = { 0xAA, 0xAA, 0x03, 0x00, 0x00, 0x0C, 0x20, 0x00 };
826 static ushort
CDP_compute_csum(const uchar
*buff
, ushort len
)
835 odd
= 1 & (ulong
)buff
;
845 if (result
& 0x80000000)
846 result
= (result
& 0xFFFF) + (result
>> 16);
850 leftover
= (signed short)(*(const signed char *)buff
);
851 /* CISCO SUCKS big time! (and blows too):
852 * CDP uses the IP checksum algorithm with a twist;
853 * for the last byte it *sign* extends and sums.
855 result
= (result
& 0xffff0000) | ((result
+ leftover
) & 0x0000ffff);
858 result
= (result
& 0xFFFF) + (result
>> 16);
861 result
= ((result
>> 8) & 0xff) | ((result
& 0xff) << 8);
864 /* add up 16-bit and 17-bit words for 17+c bits */
865 result
= (result
& 0xffff) + (result
>> 16);
866 /* add up 16-bit and 2-bit for 16+c bit */
867 result
= (result
& 0xffff) + (result
>> 16);
869 result
= (result
& 0xffff) + (result
>> 16);
872 csum
= ~(ushort
)result
;
874 /* run time endian detection */
875 if (csum
!= htons(csum
)) /* little endian */
881 int CDPSendTrigger(void)
889 #if defined(CONFIG_CDP_DEVICE_ID) || defined(CONFIG_CDP_PORT_ID) || \
890 defined(CONFIG_CDP_VERSION) || defined(CONFIG_CDP_PLATFORM)
895 et
= (Ethernet_t
*)pkt
;
897 /* NOTE: trigger sent not on any VLAN */
899 /* form ethernet header */
900 memcpy(et
->et_dest
, NetCDPAddr
, 6);
901 memcpy(et
->et_src
, NetOurEther
, 6);
903 pkt
+= ETHER_HDR_SIZE
;
906 memcpy((uchar
*)pkt
, CDP_SNAP_hdr
, sizeof(CDP_SNAP_hdr
));
907 pkt
+= sizeof(CDP_SNAP_hdr
);
910 *pkt
++ = 0x02; /* CDP version 2 */
911 *pkt
++ = 180; /* TTL */
912 s
= (volatile ushort
*)pkt
;
914 *s
++ = htons(0); /* checksum (0 for later calculation) */
917 #ifdef CONFIG_CDP_DEVICE_ID
918 *s
++ = htons(CDP_DEVICE_ID_TLV
);
919 *s
++ = htons(CONFIG_CDP_DEVICE_ID
);
920 memset(buf
, 0, sizeof(buf
));
921 sprintf(buf
, CONFIG_CDP_DEVICE_ID_PREFIX
"%02X%02X%02X%02X%02X%02X",
922 NetOurEther
[0] & 0xff, NetOurEther
[1] & 0xff,
923 NetOurEther
[2] & 0xff, NetOurEther
[3] & 0xff,
924 NetOurEther
[4] & 0xff, NetOurEther
[5] & 0xff);
925 memcpy((uchar
*)s
, buf
, 16);
929 #ifdef CONFIG_CDP_PORT_ID
930 *s
++ = htons(CDP_PORT_ID_TLV
);
931 memset(buf
, 0, sizeof(buf
));
932 sprintf(buf
, CONFIG_CDP_PORT_ID
, eth_get_dev_index());
934 if (len
& 1) /* make it even */
936 *s
++ = htons(len
+ 4);
937 memcpy((uchar
*)s
, buf
, len
);
941 #ifdef CONFIG_CDP_CAPABILITIES
942 *s
++ = htons(CDP_CAPABILITIES_TLV
);
944 *(ulong
*)s
= htonl(CONFIG_CDP_CAPABILITIES
);
948 #ifdef CONFIG_CDP_VERSION
949 *s
++ = htons(CDP_VERSION_TLV
);
950 memset(buf
, 0, sizeof(buf
));
951 strcpy(buf
, CONFIG_CDP_VERSION
);
953 if (len
& 1) /* make it even */
955 *s
++ = htons(len
+ 4);
956 memcpy((uchar
*)s
, buf
, len
);
960 #ifdef CONFIG_CDP_PLATFORM
961 *s
++ = htons(CDP_PLATFORM_TLV
);
962 memset(buf
, 0, sizeof(buf
));
963 strcpy(buf
, CONFIG_CDP_PLATFORM
);
965 if (len
& 1) /* make it even */
967 *s
++ = htons(len
+ 4);
968 memcpy((uchar
*)s
, buf
, len
);
972 #ifdef CONFIG_CDP_TRIGGER
973 *s
++ = htons(CDP_TRIGGER_TLV
);
975 *(ulong
*)s
= htonl(CONFIG_CDP_TRIGGER
);
979 #ifdef CONFIG_CDP_POWER_CONSUMPTION
980 *s
++ = htons(CDP_POWER_CONSUMPTION_TLV
);
982 *s
++ = htons(CONFIG_CDP_POWER_CONSUMPTION
);
985 /* length of ethernet packet */
986 len
= (uchar
*)s
- ((uchar
*)NetTxPacket
+ ETHER_HDR_SIZE
);
987 et
->et_protlen
= htons(len
);
989 len
= ETHER_HDR_SIZE
+ sizeof(CDP_SNAP_hdr
);
990 chksum
= CDP_compute_csum((uchar
*)NetTxPacket
+ len
, (uchar
*)s
- (NetTxPacket
+ len
));
995 (void) eth_send(NetTxPacket
, (uchar
*)s
- NetTxPacket
);
1005 NetSetTimeout (CDP_TIMEOUT
, CDPTimeout
);
1010 /* if not OK try again */
1014 NetState
= NETLOOP_SUCCESS
;
1018 CDPDummyHandler (uchar
* pkt
, unsigned dest
, unsigned src
, unsigned len
)
1024 CDPHandler(const uchar
* pkt
, unsigned len
)
1033 if (len
< sizeof(CDP_SNAP_hdr
) + 4)
1036 /* check for valid CDP SNAP header */
1037 if (memcmp(pkt
, CDP_SNAP_hdr
, sizeof(CDP_SNAP_hdr
)) != 0)
1040 pkt
+= sizeof(CDP_SNAP_hdr
);
1041 len
-= sizeof(CDP_SNAP_hdr
);
1043 /* Version of CDP protocol must be >= 2 and TTL != 0 */
1044 if (pkt
[0] < 0x02 || pkt
[1] == 0)
1047 /* if version is greater than 0x02 maybe we'll have a problem; output a warning */
1049 printf("** WARNING: CDP packet received with a protocol version %d > 2\n",
1052 if (CDP_compute_csum(pkt
, len
) != 0)
1064 ss
= (const ushort
*)pkt
;
1065 type
= ntohs(ss
[0]);
1066 tlen
= ntohs(ss
[1]);
1074 ss
+= 2; /* point ss to the data of the TLV */
1078 case CDP_DEVICE_ID_TLV
:
1080 case CDP_ADDRESS_TLV
:
1082 case CDP_PORT_ID_TLV
:
1084 case CDP_CAPABILITIES_TLV
:
1086 case CDP_VERSION_TLV
:
1088 case CDP_PLATFORM_TLV
:
1090 case CDP_NATIVE_VLAN_TLV
:
1093 case CDP_APPLIANCE_VLAN_TLV
:
1094 t
= (const uchar
*)ss
;
1100 ss
= (const ushort
*)(t
+ 1);
1102 #ifdef CONFIG_CDP_APPLIANCE_VLAN_TYPE
1103 if (applid
== CONFIG_CDP_APPLIANCE_VLAN_TYPE
)
1106 vlan
= ntohs(*ss
); /* XXX will this work; dunno */
1111 case CDP_TRIGGER_TLV
:
1113 case CDP_POWER_CONSUMPTION_TLV
:
1115 case CDP_SYSNAME_TLV
:
1117 case CDP_SYSOBJECT_TLV
:
1119 case CDP_MANAGEMENT_ADDRESS_TLV
:
1124 CDPApplianceVLAN
= vlan
;
1125 CDPNativeVLAN
= nvlan
;
1131 printf("** CDP packet is too short\n");
1135 static void CDPStart(void)
1137 #if defined(CONFIG_NET_MULTI)
1138 printf ("Using %s device\n", eth_get_name());
1143 CDPNativeVLAN
= htons(-1);
1144 CDPApplianceVLAN
= htons(-1);
1146 NetSetTimeout (CDP_TIMEOUT
, CDPTimeout
);
1147 NetSetHandler (CDPDummyHandler
);
1155 NetReceive(volatile uchar
* inpkt
, int len
)
1163 #if defined(CONFIG_CMD_CDP)
1166 ushort cti
= 0, vlanid
= VLAN_NONE
, myvlanid
, mynvlanid
;
1169 printf("packet received\n");
1174 et
= (Ethernet_t
*)inpkt
;
1176 /* too small packet? */
1177 if (len
< ETHER_HDR_SIZE
)
1182 (*push_packet
)(inpkt
, len
);
1187 #if defined(CONFIG_CMD_CDP)
1188 /* keep track if packet is CDP */
1189 iscdp
= memcmp(et
->et_dest
, NetCDPAddr
, 6) == 0;
1192 myvlanid
= ntohs(NetOurVLAN
);
1193 if (myvlanid
== (ushort
)-1)
1194 myvlanid
= VLAN_NONE
;
1195 mynvlanid
= ntohs(NetOurNativeVLAN
);
1196 if (mynvlanid
== (ushort
)-1)
1197 mynvlanid
= VLAN_NONE
;
1199 x
= ntohs(et
->et_protlen
);
1202 printf("packet received\n");
1207 * Got a 802 packet. Check the other protocol field.
1209 x
= ntohs(et
->et_prot
);
1211 ip
= (IP_t
*)(inpkt
+ E802_HDR_SIZE
);
1212 len
-= E802_HDR_SIZE
;
1214 } else if (x
!= PROT_VLAN
) { /* normal packet */
1215 ip
= (IP_t
*)(inpkt
+ ETHER_HDR_SIZE
);
1216 len
-= ETHER_HDR_SIZE
;
1218 } else { /* VLAN packet */
1219 VLAN_Ethernet_t
*vet
= (VLAN_Ethernet_t
*)et
;
1222 printf("VLAN packet received\n");
1224 /* too small packet? */
1225 if (len
< VLAN_ETHER_HDR_SIZE
)
1228 /* if no VLAN active */
1229 if ((ntohs(NetOurVLAN
) & VLAN_IDMASK
) == VLAN_NONE
1230 #if defined(CONFIG_CMD_CDP)
1236 cti
= ntohs(vet
->vet_tag
);
1237 vlanid
= cti
& VLAN_IDMASK
;
1238 x
= ntohs(vet
->vet_type
);
1240 ip
= (IP_t
*)(inpkt
+ VLAN_ETHER_HDR_SIZE
);
1241 len
-= VLAN_ETHER_HDR_SIZE
;
1245 printf("Receive from protocol 0x%x\n", x
);
1248 #if defined(CONFIG_CMD_CDP)
1250 CDPHandler((uchar
*)ip
, len
);
1255 if ((myvlanid
& VLAN_IDMASK
) != VLAN_NONE
) {
1256 if (vlanid
== VLAN_NONE
)
1257 vlanid
= (mynvlanid
& VLAN_IDMASK
);
1259 if (vlanid
!= (myvlanid
& VLAN_IDMASK
))
1267 * We have to deal with two types of ARP packets:
1268 * - REQUEST packets will be answered by sending our
1269 * IP address - if we know it.
1270 * - REPLY packates are expected only after we asked
1271 * for the TFTP server's or the gateway's ethernet
1272 * address; so if we receive such a packet, we set
1273 * the server ethernet address
1279 if (len
< ARP_HDR_SIZE
) {
1280 printf("bad length %d < %d\n", len
, ARP_HDR_SIZE
);
1283 if (ntohs(arp
->ar_hrd
) != ARP_ETHER
) {
1286 if (ntohs(arp
->ar_pro
) != PROT_IP
) {
1289 if (arp
->ar_hln
!= 6) {
1292 if (arp
->ar_pln
!= 4) {
1296 if (NetOurIP
== 0) {
1300 if (NetReadIP(&arp
->ar_data
[16]) != NetOurIP
) {
1304 switch (ntohs(arp
->ar_op
)) {
1305 case ARPOP_REQUEST
: /* reply with our IP address */
1307 puts ("Got ARP REQUEST, return our IP\n");
1310 pkt
+= NetSetEther(pkt
, et
->et_src
, PROT_ARP
);
1311 arp
->ar_op
= htons(ARPOP_REPLY
);
1312 memcpy (&arp
->ar_data
[10], &arp
->ar_data
[0], 6);
1313 NetCopyIP(&arp
->ar_data
[16], &arp
->ar_data
[6]);
1314 memcpy (&arp
->ar_data
[ 0], NetOurEther
, 6);
1315 NetCopyIP(&arp
->ar_data
[ 6], &NetOurIP
);
1316 (void) eth_send((uchar
*)et
, (pkt
- (uchar
*)et
) + ARP_HDR_SIZE
);
1319 case ARPOP_REPLY
: /* arp reply */
1320 /* are we waiting for a reply */
1321 if (!NetArpWaitPacketIP
|| !NetArpWaitPacketMAC
)
1324 printf("Got ARP REPLY, set server/gtwy eth addr (%02x:%02x:%02x:%02x:%02x:%02x)\n",
1325 arp
->ar_data
[0], arp
->ar_data
[1],
1326 arp
->ar_data
[2], arp
->ar_data
[3],
1327 arp
->ar_data
[4], arp
->ar_data
[5]);
1330 tmp
= NetReadIP(&arp
->ar_data
[6]);
1332 /* matched waiting packet's address */
1333 if (tmp
== NetArpWaitReplyIP
) {
1337 /* save address for later use */
1338 memcpy(NetArpWaitPacketMAC
, &arp
->ar_data
[0], 6);
1340 #ifdef CONFIG_NETCONSOLE
1341 (*packetHandler
)(0,0,0,0);
1343 /* modify header, and transmit it */
1344 memcpy(((Ethernet_t
*)NetArpWaitTxPacket
)->et_dest
, NetArpWaitPacketMAC
, 6);
1345 (void) eth_send(NetArpWaitTxPacket
, NetArpWaitTxPacketSize
);
1347 /* no arp request pending now */
1348 NetArpWaitPacketIP
= 0;
1349 NetArpWaitTxPacketSize
= 0;
1350 NetArpWaitPacketMAC
= NULL
;
1356 printf("Unexpected ARP opcode 0x%x\n", ntohs(arp
->ar_op
));
1364 puts ("Got RARP\n");
1367 if (len
< ARP_HDR_SIZE
) {
1368 printf("bad length %d < %d\n", len
, ARP_HDR_SIZE
);
1372 if ((ntohs(arp
->ar_op
) != RARPOP_REPLY
) ||
1373 (ntohs(arp
->ar_hrd
) != ARP_ETHER
) ||
1374 (ntohs(arp
->ar_pro
) != PROT_IP
) ||
1375 (arp
->ar_hln
!= 6) || (arp
->ar_pln
!= 4)) {
1377 puts ("invalid RARP header\n");
1379 NetCopyIP(&NetOurIP
, &arp
->ar_data
[16]);
1380 if (NetServerIP
== 0)
1381 NetCopyIP(&NetServerIP
, &arp
->ar_data
[ 6]);
1382 memcpy (NetServerEther
, &arp
->ar_data
[ 0], 6);
1384 (*packetHandler
)(0,0,0,0);
1392 if (len
< IP_HDR_SIZE
) {
1393 debug ("len bad %d < %d\n", len
, IP_HDR_SIZE
);
1396 if (len
< ntohs(ip
->ip_len
)) {
1397 printf("len bad %d < %d\n", len
, ntohs(ip
->ip_len
));
1400 len
= ntohs(ip
->ip_len
);
1402 printf("len=%d, v=%02x\n", len
, ip
->ip_hl_v
& 0xff);
1404 if ((ip
->ip_hl_v
& 0xf0) != 0x40) {
1407 if (ip
->ip_off
& htons(0x1fff)) { /* Can't deal w/ fragments */
1410 if (!NetCksumOk((uchar
*)ip
, IP_HDR_SIZE_NO_UDP
/ 2)) {
1411 puts ("checksum bad\n");
1414 tmp
= NetReadIP(&ip
->ip_dst
);
1415 if (NetOurIP
&& tmp
!= NetOurIP
&& tmp
!= 0xFFFFFFFF) {
1416 #ifdef CONFIG_MCAST_TFTP
1417 if (Mcast_addr
!= tmp
)
1422 * watch for ICMP host redirects
1424 * There is no real handler code (yet). We just watch
1425 * for ICMP host redirect messages. In case anybody
1426 * sees these messages: please contact me
1427 * (wd@denx.de), or - even better - send me the
1428 * necessary fixes :-)
1430 * Note: in all cases where I have seen this so far
1431 * it was a problem with the router configuration,
1432 * for instance when a router was configured in the
1433 * BOOTP reply, but the TFTP server was on the same
1434 * subnet. So this is probably a warning that your
1435 * configuration might be wrong. But I'm not really
1436 * sure if there aren't any other situations.
1438 if (ip
->ip_p
== IPPROTO_ICMP
) {
1439 ICMP_t
*icmph
= (ICMP_t
*)&(ip
->udp_src
);
1441 switch (icmph
->type
) {
1443 if (icmph
->code
!= ICMP_REDIR_HOST
)
1445 puts (" ICMP Host Redirect to ");
1446 print_IPaddr(icmph
->un
.gateway
);
1449 #if defined(CONFIG_CMD_PING)
1450 case ICMP_ECHO_REPLY
:
1452 * IP header OK. Pass the packet to the current handler.
1454 /* XXX point to ip packet */
1455 (*packetHandler
)((uchar
*)ip
, 0, 0, 0);
1457 case ICMP_ECHO_REQUEST
:
1459 printf ("Got ICMP ECHO REQUEST, return %d bytes \n",
1460 ETHER_HDR_SIZE
+ len
);
1462 memcpy (&et
->et_dest
[0], &et
->et_src
[0], 6);
1463 memcpy (&et
->et_src
[ 0], NetOurEther
, 6);
1467 NetCopyIP((void*)&ip
->ip_dst
, &ip
->ip_src
);
1468 NetCopyIP((void*)&ip
->ip_src
, &NetOurIP
);
1469 ip
->ip_sum
= ~NetCksum((uchar
*)ip
, IP_HDR_SIZE_NO_UDP
>> 1);
1471 icmph
->type
= ICMP_ECHO_REPLY
;
1472 icmph
->checksum
= 0;
1473 icmph
->checksum
= ~NetCksum((uchar
*)icmph
,
1474 (len
- IP_HDR_SIZE_NO_UDP
) >> 1);
1475 (void) eth_send((uchar
*)et
, ETHER_HDR_SIZE
+ len
);
1481 } else if (ip
->ip_p
!= IPPROTO_UDP
) { /* Only UDP packets */
1485 #ifdef CONFIG_UDP_CHECKSUM
1486 if (ip
->udp_xsum
!= 0) {
1492 xsum
+= (ntohs(ip
->udp_len
));
1493 xsum
+= (ntohl(ip
->ip_src
) >> 16) & 0x0000ffff;
1494 xsum
+= (ntohl(ip
->ip_src
) >> 0) & 0x0000ffff;
1495 xsum
+= (ntohl(ip
->ip_dst
) >> 16) & 0x0000ffff;
1496 xsum
+= (ntohl(ip
->ip_dst
) >> 0) & 0x0000ffff;
1498 sumlen
= ntohs(ip
->udp_len
);
1499 sumptr
= (ushort
*) &(ip
->udp_src
);
1501 while (sumlen
> 1) {
1504 sumdata
= *sumptr
++;
1505 xsum
+= ntohs(sumdata
);
1511 sumdata
= *(unsigned char *) sumptr
;
1512 sumdata
= (sumdata
<< 8) & 0xff00;
1515 while ((xsum
>> 16) != 0) {
1516 xsum
= (xsum
& 0x0000ffff) + ((xsum
>> 16) & 0x0000ffff);
1518 if ((xsum
!= 0x00000000) && (xsum
!= 0x0000ffff)) {
1519 printf(" UDP wrong checksum %08x %08x\n", xsum
, ntohs(ip
->udp_xsum
));
1526 #ifdef CONFIG_NETCONSOLE
1527 nc_input_packet((uchar
*)ip
+IP_HDR_SIZE
,
1530 ntohs(ip
->udp_len
) - 8);
1533 * IP header OK. Pass the packet to the current handler.
1535 (*packetHandler
)((uchar
*)ip
+IP_HDR_SIZE
,
1538 ntohs(ip
->udp_len
) - 8);
1544 /**********************************************************************/
1546 static int net_check_prereq (proto_t protocol
)
1550 #if defined(CONFIG_CMD_PING)
1552 if (NetPingIP
== 0) {
1553 puts ("*** ERROR: ping address not given\n");
1558 #if defined(CONFIG_CMD_SNTP)
1560 if (NetNtpServerIP
== 0) {
1561 puts ("*** ERROR: NTP server address not given\n");
1566 #if defined(CONFIG_CMD_NFS)
1571 if (NetServerIP
== 0) {
1572 puts ("*** ERROR: `serverip' not set\n");
1575 #if defined(CONFIG_CMD_PING) || defined(CONFIG_CMD_SNTP)
1579 if (NetOurIP
== 0) {
1580 puts ("*** ERROR: `ipaddr' not set\n");
1589 if (memcmp (NetOurEther
, "\0\0\0\0\0\0", 6) == 0) {
1590 #ifdef CONFIG_NET_MULTI
1591 extern int eth_get_dev_index (void);
1592 int num
= eth_get_dev_index ();
1596 puts ("*** ERROR: No ethernet found.\n");
1599 puts ("*** ERROR: `ethaddr' not set\n");
1602 printf ("*** ERROR: `eth%daddr' not set\n",
1610 puts ("*** ERROR: `ethaddr' not set\n");
1618 return (0); /* OK */
1620 /**********************************************************************/
1623 NetCksumOk(uchar
* ptr
, int len
)
1625 return !((NetCksum(ptr
, len
) + 1) & 0xfffe);
1630 NetCksum(uchar
* ptr
, int len
)
1633 ushort
*p
= (ushort
*)ptr
;
1638 xsum
= (xsum
& 0xffff) + (xsum
>> 16);
1639 xsum
= (xsum
& 0xffff) + (xsum
>> 16);
1640 return (xsum
& 0xffff);
1648 myvlanid
= ntohs(NetOurVLAN
);
1649 if (myvlanid
== (ushort
)-1)
1650 myvlanid
= VLAN_NONE
;
1652 return ((myvlanid
& VLAN_IDMASK
) == VLAN_NONE
) ? ETHER_HDR_SIZE
: VLAN_ETHER_HDR_SIZE
;
1656 NetSetEther(volatile uchar
* xet
, uchar
* addr
, uint prot
)
1658 Ethernet_t
*et
= (Ethernet_t
*)xet
;
1661 myvlanid
= ntohs(NetOurVLAN
);
1662 if (myvlanid
== (ushort
)-1)
1663 myvlanid
= VLAN_NONE
;
1665 memcpy (et
->et_dest
, addr
, 6);
1666 memcpy (et
->et_src
, NetOurEther
, 6);
1667 if ((myvlanid
& VLAN_IDMASK
) == VLAN_NONE
) {
1668 et
->et_protlen
= htons(prot
);
1669 return ETHER_HDR_SIZE
;
1671 VLAN_Ethernet_t
*vet
= (VLAN_Ethernet_t
*)xet
;
1673 vet
->vet_vlan_type
= htons(PROT_VLAN
);
1674 vet
->vet_tag
= htons((0 << 5) | (myvlanid
& VLAN_IDMASK
));
1675 vet
->vet_type
= htons(prot
);
1676 return VLAN_ETHER_HDR_SIZE
;
1681 NetSetIP(volatile uchar
* xip
, IPaddr_t dest
, int dport
, int sport
, int len
)
1683 volatile IP_t
*ip
= (IP_t
*)xip
;
1686 * If the data is an odd number of bytes, zero the
1687 * byte after the last byte so that the checksum
1691 xip
[IP_HDR_SIZE
+ len
] = 0;
1694 * Construct an IP and UDP header.
1695 * (need to set no fragment bit - XXX)
1697 ip
->ip_hl_v
= 0x45; /* IP_HDR_SIZE / 4 (not including UDP) */
1699 ip
->ip_len
= htons(IP_HDR_SIZE
+ len
);
1700 ip
->ip_id
= htons(NetIPID
++);
1701 ip
->ip_off
= htons(0x4000); /* No fragmentation */
1703 ip
->ip_p
= 17; /* UDP */
1705 NetCopyIP((void*)&ip
->ip_src
, &NetOurIP
); /* already in network byte order */
1706 NetCopyIP((void*)&ip
->ip_dst
, &dest
); /* - "" - */
1707 ip
->udp_src
= htons(sport
);
1708 ip
->udp_dst
= htons(dport
);
1709 ip
->udp_len
= htons(8 + len
);
1711 ip
->ip_sum
= ~NetCksum((uchar
*)ip
, IP_HDR_SIZE_NO_UDP
/ 2);
1714 void copy_filename (char *dst
, char *src
, int size
)
1716 if (*src
&& (*src
== '"')) {
1721 while ((--size
> 0) && *src
&& (*src
!= '"')) {
1729 void ip_to_string (IPaddr_t x
, char *s
)
1732 sprintf (s
, "%d.%d.%d.%d",
1733 (int) ((x
>> 24) & 0xff),
1734 (int) ((x
>> 16) & 0xff),
1735 (int) ((x
>> 8) & 0xff), (int) ((x
>> 0) & 0xff)
1739 IPaddr_t
string_to_ip(char *s
)
1748 for (addr
=0, i
=0; i
<4; ++i
) {
1749 ulong val
= s
? simple_strtoul(s
, &e
, 10) : 0;
1751 addr
|= (val
& 0xFF);
1757 return (htonl(addr
));
1760 void VLAN_to_string(ushort x
, char *s
)
1764 if (x
== (ushort
)-1)
1770 sprintf(s
, "%d", x
& VLAN_IDMASK
);
1773 ushort
string_to_VLAN(char *s
)
1778 return htons(VLAN_NONE
);
1780 if (*s
< '0' || *s
> '9')
1783 id
= (ushort
)simple_strtoul(s
, NULL
, 10);
1788 void print_IPaddr (IPaddr_t x
)
1792 ip_to_string (x
, tmp
);
1797 IPaddr_t
getenv_IPaddr (char *var
)
1799 return (string_to_ip(getenv(var
)));
1802 ushort
getenv_VLAN(char *var
)
1804 return (string_to_VLAN(getenv(var
)));