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)
92 #if defined(CONFIG_CDP_VERSION)
93 #include <timestamp.h>
95 #if defined(CONFIG_CMD_DNS)
99 #if defined(CONFIG_CMD_NET)
101 DECLARE_GLOBAL_DATA_PTR
;
103 #ifndef CONFIG_ARP_TIMEOUT
104 # define ARP_TIMEOUT 5000UL /* Milliseconds before trying ARP again */
106 # define ARP_TIMEOUT CONFIG_ARP_TIMEOUT
110 #ifndef CONFIG_NET_RETRY_COUNT
111 # define ARP_TIMEOUT_COUNT 5 /* # of timeouts before giving up */
113 # define ARP_TIMEOUT_COUNT CONFIG_NET_RETRY_COUNT
116 /** BOOTP EXTENTIONS **/
118 IPaddr_t NetOurSubnetMask
=0; /* Our subnet mask (0=unknown) */
119 IPaddr_t NetOurGatewayIP
=0; /* Our gateways IP address */
120 IPaddr_t NetOurDNSIP
=0; /* Our DNS IP address */
121 #if defined(CONFIG_BOOTP_DNS2)
122 IPaddr_t NetOurDNS2IP
=0; /* Our 2nd DNS IP address */
124 char NetOurNISDomain
[32]={0,}; /* Our NIS domain */
125 char NetOurHostName
[32]={0,}; /* Our hostname */
126 char NetOurRootPath
[64]={0,}; /* Our bootpath */
127 ushort NetBootFileSize
=0; /* Our bootfile size in blocks */
129 #ifdef CONFIG_MCAST_TFTP /* Multicast TFTP */
133 /** END OF BOOTP EXTENTIONS **/
135 ulong NetBootFileXferSize
; /* The actual transferred size of the bootfile (in bytes) */
136 uchar NetOurEther
[6]; /* Our ethernet address */
137 uchar NetServerEther
[6] = /* Boot server enet address */
138 { 0, 0, 0, 0, 0, 0 };
139 IPaddr_t NetOurIP
; /* Our IP addr (0 = unknown) */
140 IPaddr_t NetServerIP
; /* Server IP addr (0 = unknown) */
141 volatile uchar
*NetRxPacket
; /* Current receive packet */
142 int NetRxPacketLen
; /* Current rx packet length */
143 unsigned NetIPID
; /* IP packet ID */
144 uchar NetBcastAddr
[6] = /* Ethernet bcast address */
145 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
146 uchar NetEtherNullAddr
[6] =
147 { 0, 0, 0, 0, 0, 0 };
149 void (*push_packet
)(volatile void *, int len
) = 0;
151 #if defined(CONFIG_CMD_CDP)
152 uchar NetCDPAddr
[6] = /* Ethernet bcast address */
153 { 0x01, 0x00, 0x0c, 0xcc, 0xcc, 0xcc };
155 int NetState
; /* Network loop state */
156 #ifdef CONFIG_NET_MULTI
157 int NetRestartWrap
= 0; /* Tried all network devices */
158 static int NetRestarted
= 0; /* Network loop restarted */
159 static int NetDevExists
= 0; /* At least one device configured */
162 /* XXX in both little & big endian machines 0xFFFF == ntohs(-1) */
163 ushort NetOurVLAN
= 0xFFFF; /* default is without VLAN */
164 ushort NetOurNativeVLAN
= 0xFFFF; /* ditto */
166 char BootFile
[128]; /* Boot File name */
168 #if defined(CONFIG_CMD_PING)
169 IPaddr_t NetPingIP
; /* the ip address to ping */
171 static void PingStart(void);
174 #if defined(CONFIG_CMD_CDP)
175 static void CDPStart(void);
178 #if defined(CONFIG_CMD_SNTP)
179 IPaddr_t NetNtpServerIP
; /* NTP server IP address */
180 int NetTimeOffset
=0; /* offset time from UTC */
183 #ifdef CONFIG_NETCONSOLE
185 int nc_input_packet(uchar
*pkt
, unsigned dest
, unsigned src
, unsigned len
);
188 volatile uchar PktBuf
[(PKTBUFSRX
+1) * PKTSIZE_ALIGN
+ PKTALIGN
];
190 volatile uchar
*NetRxPackets
[PKTBUFSRX
]; /* Receive packets */
192 static rxhand_f
*packetHandler
; /* Current RX packet handler */
193 static thand_f
*timeHandler
; /* Current timeout handler */
194 static ulong timeStart
; /* Time base value */
195 static ulong timeDelta
; /* Current timeout value */
196 volatile uchar
*NetTxPacket
= 0; /* THE transmit packet */
198 static int net_check_prereq (proto_t protocol
);
200 /**********************************************************************/
202 IPaddr_t NetArpWaitPacketIP
;
203 IPaddr_t NetArpWaitReplyIP
;
204 uchar
*NetArpWaitPacketMAC
; /* MAC address of waiting packet's destination */
205 uchar
*NetArpWaitTxPacket
; /* THE transmit packet */
206 int NetArpWaitTxPacketSize
;
207 uchar NetArpWaitPacketBuf
[PKTSIZE_ALIGN
+ PKTALIGN
];
208 ulong NetArpWaitTimerStart
;
211 void ArpRequest (void)
217 debug("ARP broadcast %d\n", NetArpWaitTry
);
221 pkt
+= NetSetEther (pkt
, NetBcastAddr
, PROT_ARP
);
225 arp
->ar_hrd
= htons (ARP_ETHER
);
226 arp
->ar_pro
= htons (PROT_IP
);
229 arp
->ar_op
= htons (ARPOP_REQUEST
);
231 memcpy (&arp
->ar_data
[0], NetOurEther
, 6); /* source ET addr */
232 NetWriteIP ((uchar
*) & arp
->ar_data
[6], NetOurIP
); /* source IP addr */
233 for (i
= 10; i
< 16; ++i
) {
234 arp
->ar_data
[i
] = 0; /* dest ET addr = 0 */
237 if ((NetArpWaitPacketIP
& NetOurSubnetMask
) !=
238 (NetOurIP
& NetOurSubnetMask
)) {
239 if (NetOurGatewayIP
== 0) {
240 puts ("## Warning: gatewayip needed but not set\n");
241 NetArpWaitReplyIP
= NetArpWaitPacketIP
;
243 NetArpWaitReplyIP
= NetOurGatewayIP
;
246 NetArpWaitReplyIP
= NetArpWaitPacketIP
;
249 NetWriteIP ((uchar
*) & arp
->ar_data
[16], NetArpWaitReplyIP
);
250 (void) eth_send (NetTxPacket
, (pkt
- NetTxPacket
) + ARP_HDR_SIZE
);
253 void ArpTimeoutCheck(void)
257 if (!NetArpWaitPacketIP
)
262 /* check for arp timeout */
263 if ((t
- NetArpWaitTimerStart
) > ARP_TIMEOUT
) {
266 if (NetArpWaitTry
>= ARP_TIMEOUT_COUNT
) {
267 puts ("\nARP Retry count exceeded; starting again\n");
271 NetArpWaitTimerStart
= t
;
278 NetInitLoop(proto_t protocol
)
280 static int env_changed_id
= 0;
282 int env_id
= get_env_id ();
284 /* update only when the environment has changed */
285 if (env_changed_id
!= env_id
) {
286 NetCopyIP(&NetOurIP
, &bd
->bi_ip_addr
);
287 NetOurGatewayIP
= getenv_IPaddr ("gatewayip");
288 NetOurSubnetMask
= getenv_IPaddr ("netmask");
289 NetServerIP
= getenv_IPaddr ("serverip");
290 NetOurNativeVLAN
= getenv_VLAN("nvlan");
291 NetOurVLAN
= getenv_VLAN("vlan");
292 #if defined(CONFIG_CMD_DNS)
293 NetOurDNSIP
= getenv_IPaddr("dnsip");
295 env_changed_id
= env_id
;
301 /**********************************************************************/
303 * Main network processing loop.
307 NetLoop(proto_t protocol
)
311 #ifdef CONFIG_NET_MULTI
316 /* XXX problem with bss workaround */
317 NetArpWaitPacketMAC
= NULL
;
318 NetArpWaitTxPacket
= NULL
;
319 NetArpWaitPacketIP
= 0;
320 NetArpWaitReplyIP
= 0;
321 NetArpWaitTxPacket
= NULL
;
327 * Setup packet buffers, aligned correctly.
329 NetTxPacket
= &PktBuf
[0] + (PKTALIGN
- 1);
330 NetTxPacket
-= (ulong
)NetTxPacket
% PKTALIGN
;
331 for (i
= 0; i
< PKTBUFSRX
; i
++) {
332 NetRxPackets
[i
] = NetTxPacket
+ (i
+1)*PKTSIZE_ALIGN
;
336 if (!NetArpWaitTxPacket
) {
337 NetArpWaitTxPacket
= &NetArpWaitPacketBuf
[0] + (PKTALIGN
- 1);
338 NetArpWaitTxPacket
-= (ulong
)NetArpWaitTxPacket
% PKTALIGN
;
339 NetArpWaitTxPacketSize
= 0;
343 #ifdef CONFIG_NET_MULTI
346 if (eth_init(bd
) < 0) {
352 #ifdef CONFIG_NET_MULTI
353 memcpy (NetOurEther
, eth_get_dev()->enetaddr
, 6);
355 eth_getenv_enetaddr("ethaddr", NetOurEther
);
358 NetState
= NETLOOP_CONTINUE
;
361 * Start the ball rolling with the given start function. From
362 * here on, this code is a state machine driven by received
363 * packets and timer events.
365 NetInitLoop(protocol
);
367 switch (net_check_prereq (protocol
)) {
369 /* network not configured */
373 #ifdef CONFIG_NET_MULTI
375 /* network device not configured */
377 #endif /* CONFIG_NET_MULTI */
380 #ifdef CONFIG_NET_MULTI
385 /* always use ARP to get server ethernet address */
389 #if defined(CONFIG_CMD_DHCP)
393 DhcpRequest(); /* Basically same as BOOTP */
408 #if defined(CONFIG_CMD_PING)
413 #if defined(CONFIG_CMD_NFS)
418 #if defined(CONFIG_CMD_CDP)
423 #ifdef CONFIG_NETCONSOLE
428 #if defined(CONFIG_CMD_SNTP)
433 #if defined(CONFIG_CMD_DNS)
442 NetBootFileXferSize
= 0;
446 #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
447 #if defined(CONFIG_SYS_FAULT_ECHO_LINK_DOWN) && defined(CONFIG_STATUS_LED) && defined(STATUS_LED_RED)
449 * Echo the inverted link state to the fault LED.
451 if(miiphy_link(eth_get_dev()->name
, CONFIG_SYS_FAULT_MII_ADDR
)) {
452 status_led_set (STATUS_LED_RED
, STATUS_LED_OFF
);
454 status_led_set (STATUS_LED_RED
, STATUS_LED_ON
);
456 #endif /* CONFIG_SYS_FAULT_ECHO_LINK_DOWN, ... */
457 #endif /* CONFIG_MII, ... */
460 * Main packet reception loop. Loop receiving packets until
461 * someone sets `NetState' to a state that terminates.
465 #ifdef CONFIG_SHOW_ACTIVITY
467 extern void show_activity(int arg
);
472 * Check the ethernet for a new packet. The ethernet
473 * receive routine will process it.
478 * Abort if ctrl-c was pressed.
489 * Check for a timeout, and run the timeout handler
492 if (timeHandler
&& ((get_timer(0) - timeStart
) > timeDelta
)) {
495 #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
496 # if defined(CONFIG_SYS_FAULT_ECHO_LINK_DOWN) && \
497 defined(CONFIG_STATUS_LED) && \
498 defined(STATUS_LED_RED)
500 * Echo the inverted link state to the fault LED.
502 if(miiphy_link(eth_get_dev()->name
, CONFIG_SYS_FAULT_MII_ADDR
)) {
503 status_led_set (STATUS_LED_RED
, STATUS_LED_OFF
);
505 status_led_set (STATUS_LED_RED
, STATUS_LED_ON
);
507 # endif /* CONFIG_SYS_FAULT_ECHO_LINK_DOWN, ... */
508 #endif /* CONFIG_MII, ... */
510 timeHandler
= (thand_f
*)0;
517 case NETLOOP_RESTART
:
518 #ifdef CONFIG_NET_MULTI
523 case NETLOOP_SUCCESS
:
524 if (NetBootFileXferSize
> 0) {
526 printf("Bytes transferred = %ld (%lx hex)\n",
528 NetBootFileXferSize
);
529 sprintf(buf
, "%lX", NetBootFileXferSize
);
530 setenv("filesize", buf
);
532 sprintf(buf
, "%lX", (unsigned long)load_addr
);
533 setenv("fileaddr", buf
);
536 return NetBootFileXferSize
;
544 /**********************************************************************/
547 startAgainTimeout(void)
549 NetState
= NETLOOP_RESTART
;
553 startAgainHandler(uchar
* pkt
, unsigned dest
, unsigned src
, unsigned len
)
555 /* Totally ignore the packet */
558 void NetStartAgain (void)
561 int noretry
= 0, once
= 0;
563 if ((nretry
= getenv ("netretry")) != NULL
) {
564 noretry
= (strcmp (nretry
, "no") == 0);
565 once
= (strcmp (nretry
, "once") == 0);
569 NetState
= NETLOOP_FAIL
;
572 #ifndef CONFIG_NET_MULTI
573 NetSetTimeout (10000UL, startAgainTimeout
);
574 NetSetHandler (startAgainHandler
);
575 #else /* !CONFIG_NET_MULTI*/
577 #if !defined(CONFIG_NET_DO_NOT_TRY_ANOTHER)
578 eth_try_another (!NetRestarted
);
581 if (NetRestartWrap
) {
583 if (NetDevExists
&& !once
) {
584 NetSetTimeout (10000UL, startAgainTimeout
);
585 NetSetHandler (startAgainHandler
);
587 NetState
= NETLOOP_FAIL
;
590 NetState
= NETLOOP_RESTART
;
592 #endif /* CONFIG_NET_MULTI */
595 /**********************************************************************/
601 NetSetHandler(rxhand_f
* f
)
608 NetSetTimeout(ulong iv
, thand_f
* f
)
611 timeHandler
= (thand_f
*)0;
614 timeStart
= get_timer(0);
621 NetSendPacket(volatile uchar
* pkt
, int len
)
623 (void) eth_send(pkt
, len
);
627 NetSendUDPPacket(uchar
*ether
, IPaddr_t dest
, int dport
, int sport
, int len
)
631 /* convert to new style broadcast */
635 /* if broadcast, make the ether address a broadcast and don't do ARP */
636 if (dest
== 0xFFFFFFFF)
637 ether
= NetBcastAddr
;
639 /* if MAC address was not discovered yet, save the packet and do an ARP request */
640 if (memcmp(ether
, NetEtherNullAddr
, 6) == 0) {
642 debug("sending ARP for %08lx\n", dest
);
644 NetArpWaitPacketIP
= dest
;
645 NetArpWaitPacketMAC
= ether
;
647 pkt
= NetArpWaitTxPacket
;
648 pkt
+= NetSetEther (pkt
, NetArpWaitPacketMAC
, PROT_IP
);
650 NetSetIP (pkt
, dest
, dport
, sport
, len
);
651 memcpy(pkt
+ IP_HDR_SIZE
, (uchar
*)NetTxPacket
+ (pkt
- (uchar
*)NetArpWaitTxPacket
) + IP_HDR_SIZE
, len
);
653 /* size of the waiting packet */
654 NetArpWaitTxPacketSize
= (pkt
- NetArpWaitTxPacket
) + IP_HDR_SIZE
+ len
;
656 /* and do the ARP request */
658 NetArpWaitTimerStart
= get_timer(0);
660 return 1; /* waiting */
663 debug("sending UDP to %08lx/%pM\n", dest
, ether
);
665 pkt
= (uchar
*)NetTxPacket
;
666 pkt
+= NetSetEther (pkt
, ether
, PROT_IP
);
667 NetSetIP (pkt
, dest
, dport
, sport
, len
);
668 (void) eth_send(NetTxPacket
, (pkt
- NetTxPacket
) + IP_HDR_SIZE
+ len
);
670 return 0; /* transmitted */
673 #if defined(CONFIG_CMD_PING)
674 static ushort PingSeqNo
;
683 /* XXX always send arp request */
685 memcpy(mac
, NetEtherNullAddr
, 6);
687 debug("sending ARP for %08lx\n", NetPingIP
);
689 NetArpWaitPacketIP
= NetPingIP
;
690 NetArpWaitPacketMAC
= mac
;
692 pkt
= NetArpWaitTxPacket
;
693 pkt
+= NetSetEther(pkt
, mac
, PROT_IP
);
695 ip
= (volatile IP_t
*)pkt
;
698 * Construct an IP and ICMP header. (need to set no fragment bit - XXX)
700 ip
->ip_hl_v
= 0x45; /* IP_HDR_SIZE / 4 (not including UDP) */
702 ip
->ip_len
= htons(IP_HDR_SIZE_NO_UDP
+ 8);
703 ip
->ip_id
= htons(NetIPID
++);
704 ip
->ip_off
= htons(IP_FLAGS_DFRAG
); /* Don't fragment */
706 ip
->ip_p
= 0x01; /* ICMP */
708 NetCopyIP((void*)&ip
->ip_src
, &NetOurIP
); /* already in network byte order */
709 NetCopyIP((void*)&ip
->ip_dst
, &NetPingIP
); /* - "" - */
710 ip
->ip_sum
= ~NetCksum((uchar
*)ip
, IP_HDR_SIZE_NO_UDP
/ 2);
712 s
= &ip
->udp_src
; /* XXX ICMP starts here */
713 s
[0] = htons(0x0800); /* echo-request, code */
714 s
[1] = 0; /* checksum */
715 s
[2] = 0; /* identifier */
716 s
[3] = htons(PingSeqNo
++); /* sequence number */
717 s
[1] = ~NetCksum((uchar
*)s
, 8/2);
719 /* size of the waiting packet */
720 NetArpWaitTxPacketSize
= (pkt
- NetArpWaitTxPacket
) + IP_HDR_SIZE_NO_UDP
+ 8;
722 /* and do the ARP request */
724 NetArpWaitTimerStart
= get_timer(0);
726 return 1; /* waiting */
733 NetState
= NETLOOP_FAIL
; /* we did not get the reply */
737 PingHandler (uchar
* pkt
, unsigned dest
, unsigned src
, unsigned len
)
740 volatile IP_t
*ip
= (volatile IP_t
*)pkt
;
742 tmp
= NetReadIP((void *)&ip
->ip_src
);
743 if (tmp
!= NetPingIP
)
746 NetState
= NETLOOP_SUCCESS
;
749 static void PingStart(void)
751 #if defined(CONFIG_NET_MULTI)
752 printf ("Using %s device\n", eth_get_name());
753 #endif /* CONFIG_NET_MULTI */
754 NetSetTimeout (10000UL, PingTimeout
);
755 NetSetHandler (PingHandler
);
761 #if defined(CONFIG_CMD_CDP)
763 #define CDP_DEVICE_ID_TLV 0x0001
764 #define CDP_ADDRESS_TLV 0x0002
765 #define CDP_PORT_ID_TLV 0x0003
766 #define CDP_CAPABILITIES_TLV 0x0004
767 #define CDP_VERSION_TLV 0x0005
768 #define CDP_PLATFORM_TLV 0x0006
769 #define CDP_NATIVE_VLAN_TLV 0x000a
770 #define CDP_APPLIANCE_VLAN_TLV 0x000e
771 #define CDP_TRIGGER_TLV 0x000f
772 #define CDP_POWER_CONSUMPTION_TLV 0x0010
773 #define CDP_SYSNAME_TLV 0x0014
774 #define CDP_SYSOBJECT_TLV 0x0015
775 #define CDP_MANAGEMENT_ADDRESS_TLV 0x0016
777 #define CDP_TIMEOUT 250UL /* one packet every 250ms */
782 ushort CDPNativeVLAN
;
783 ushort CDPApplianceVLAN
;
785 static const uchar CDP_SNAP_hdr
[8] = { 0xAA, 0xAA, 0x03, 0x00, 0x00, 0x0C, 0x20, 0x00 };
787 static ushort
CDP_compute_csum(const uchar
*buff
, ushort len
)
796 odd
= 1 & (ulong
)buff
;
806 if (result
& 0x80000000)
807 result
= (result
& 0xFFFF) + (result
>> 16);
811 leftover
= (signed short)(*(const signed char *)buff
);
812 /* CISCO SUCKS big time! (and blows too):
813 * CDP uses the IP checksum algorithm with a twist;
814 * for the last byte it *sign* extends and sums.
816 result
= (result
& 0xffff0000) | ((result
+ leftover
) & 0x0000ffff);
819 result
= (result
& 0xFFFF) + (result
>> 16);
822 result
= ((result
>> 8) & 0xff) | ((result
& 0xff) << 8);
825 /* add up 16-bit and 17-bit words for 17+c bits */
826 result
= (result
& 0xffff) + (result
>> 16);
827 /* add up 16-bit and 2-bit for 16+c bit */
828 result
= (result
& 0xffff) + (result
>> 16);
830 result
= (result
& 0xffff) + (result
>> 16);
833 csum
= ~(ushort
)result
;
835 /* run time endian detection */
836 if (csum
!= htons(csum
)) /* little endian */
842 int CDPSendTrigger(void)
850 #if defined(CONFIG_CDP_DEVICE_ID) || defined(CONFIG_CDP_PORT_ID) || \
851 defined(CONFIG_CDP_VERSION) || defined(CONFIG_CDP_PLATFORM)
856 et
= (Ethernet_t
*)pkt
;
858 /* NOTE: trigger sent not on any VLAN */
860 /* form ethernet header */
861 memcpy(et
->et_dest
, NetCDPAddr
, 6);
862 memcpy(et
->et_src
, NetOurEther
, 6);
864 pkt
+= ETHER_HDR_SIZE
;
867 memcpy((uchar
*)pkt
, CDP_SNAP_hdr
, sizeof(CDP_SNAP_hdr
));
868 pkt
+= sizeof(CDP_SNAP_hdr
);
871 *pkt
++ = 0x02; /* CDP version 2 */
872 *pkt
++ = 180; /* TTL */
873 s
= (volatile ushort
*)pkt
;
875 *s
++ = htons(0); /* checksum (0 for later calculation) */
878 #ifdef CONFIG_CDP_DEVICE_ID
879 *s
++ = htons(CDP_DEVICE_ID_TLV
);
880 *s
++ = htons(CONFIG_CDP_DEVICE_ID
);
881 sprintf(buf
, CONFIG_CDP_DEVICE_ID_PREFIX
"%pm", NetOurEther
);
882 memcpy((uchar
*)s
, buf
, 16);
886 #ifdef CONFIG_CDP_PORT_ID
887 *s
++ = htons(CDP_PORT_ID_TLV
);
888 memset(buf
, 0, sizeof(buf
));
889 sprintf(buf
, CONFIG_CDP_PORT_ID
, eth_get_dev_index());
891 if (len
& 1) /* make it even */
893 *s
++ = htons(len
+ 4);
894 memcpy((uchar
*)s
, buf
, len
);
898 #ifdef CONFIG_CDP_CAPABILITIES
899 *s
++ = htons(CDP_CAPABILITIES_TLV
);
901 *(ulong
*)s
= htonl(CONFIG_CDP_CAPABILITIES
);
905 #ifdef CONFIG_CDP_VERSION
906 *s
++ = htons(CDP_VERSION_TLV
);
907 memset(buf
, 0, sizeof(buf
));
908 strcpy(buf
, CONFIG_CDP_VERSION
);
910 if (len
& 1) /* make it even */
912 *s
++ = htons(len
+ 4);
913 memcpy((uchar
*)s
, buf
, len
);
917 #ifdef CONFIG_CDP_PLATFORM
918 *s
++ = htons(CDP_PLATFORM_TLV
);
919 memset(buf
, 0, sizeof(buf
));
920 strcpy(buf
, CONFIG_CDP_PLATFORM
);
922 if (len
& 1) /* make it even */
924 *s
++ = htons(len
+ 4);
925 memcpy((uchar
*)s
, buf
, len
);
929 #ifdef CONFIG_CDP_TRIGGER
930 *s
++ = htons(CDP_TRIGGER_TLV
);
932 *(ulong
*)s
= htonl(CONFIG_CDP_TRIGGER
);
936 #ifdef CONFIG_CDP_POWER_CONSUMPTION
937 *s
++ = htons(CDP_POWER_CONSUMPTION_TLV
);
939 *s
++ = htons(CONFIG_CDP_POWER_CONSUMPTION
);
942 /* length of ethernet packet */
943 len
= (uchar
*)s
- ((uchar
*)NetTxPacket
+ ETHER_HDR_SIZE
);
944 et
->et_protlen
= htons(len
);
946 len
= ETHER_HDR_SIZE
+ sizeof(CDP_SNAP_hdr
);
947 chksum
= CDP_compute_csum((uchar
*)NetTxPacket
+ len
, (uchar
*)s
- (NetTxPacket
+ len
));
952 (void) eth_send(NetTxPacket
, (uchar
*)s
- NetTxPacket
);
962 NetSetTimeout (CDP_TIMEOUT
, CDPTimeout
);
967 /* if not OK try again */
971 NetState
= NETLOOP_SUCCESS
;
975 CDPDummyHandler (uchar
* pkt
, unsigned dest
, unsigned src
, unsigned len
)
981 CDPHandler(const uchar
* pkt
, unsigned len
)
990 if (len
< sizeof(CDP_SNAP_hdr
) + 4)
993 /* check for valid CDP SNAP header */
994 if (memcmp(pkt
, CDP_SNAP_hdr
, sizeof(CDP_SNAP_hdr
)) != 0)
997 pkt
+= sizeof(CDP_SNAP_hdr
);
998 len
-= sizeof(CDP_SNAP_hdr
);
1000 /* Version of CDP protocol must be >= 2 and TTL != 0 */
1001 if (pkt
[0] < 0x02 || pkt
[1] == 0)
1004 /* if version is greater than 0x02 maybe we'll have a problem; output a warning */
1006 printf("** WARNING: CDP packet received with a protocol version %d > 2\n",
1009 if (CDP_compute_csum(pkt
, len
) != 0)
1021 ss
= (const ushort
*)pkt
;
1022 type
= ntohs(ss
[0]);
1023 tlen
= ntohs(ss
[1]);
1031 ss
+= 2; /* point ss to the data of the TLV */
1035 case CDP_DEVICE_ID_TLV
:
1037 case CDP_ADDRESS_TLV
:
1039 case CDP_PORT_ID_TLV
:
1041 case CDP_CAPABILITIES_TLV
:
1043 case CDP_VERSION_TLV
:
1045 case CDP_PLATFORM_TLV
:
1047 case CDP_NATIVE_VLAN_TLV
:
1050 case CDP_APPLIANCE_VLAN_TLV
:
1051 t
= (const uchar
*)ss
;
1057 ss
= (const ushort
*)(t
+ 1);
1059 #ifdef CONFIG_CDP_APPLIANCE_VLAN_TYPE
1060 if (applid
== CONFIG_CDP_APPLIANCE_VLAN_TYPE
)
1063 vlan
= ntohs(*ss
); /* XXX will this work; dunno */
1068 case CDP_TRIGGER_TLV
:
1070 case CDP_POWER_CONSUMPTION_TLV
:
1072 case CDP_SYSNAME_TLV
:
1074 case CDP_SYSOBJECT_TLV
:
1076 case CDP_MANAGEMENT_ADDRESS_TLV
:
1081 CDPApplianceVLAN
= vlan
;
1082 CDPNativeVLAN
= nvlan
;
1088 printf("** CDP packet is too short\n");
1092 static void CDPStart(void)
1094 #if defined(CONFIG_NET_MULTI)
1095 printf ("Using %s device\n", eth_get_name());
1100 CDPNativeVLAN
= htons(-1);
1101 CDPApplianceVLAN
= htons(-1);
1103 NetSetTimeout (CDP_TIMEOUT
, CDPTimeout
);
1104 NetSetHandler (CDPDummyHandler
);
1110 #ifdef CONFIG_IP_DEFRAG
1112 * This function collects fragments in a single packet, according
1113 * to the algorithm in RFC815. It returns NULL or the pointer to
1114 * a complete packet, in static storage
1116 #ifndef CONFIG_NET_MAXDEFRAG
1117 #define CONFIG_NET_MAXDEFRAG 16384
1120 * MAXDEFRAG, above, is chosen in the config file and is real data
1121 * so we need to add the NFS overhead, which is more than TFTP.
1122 * To use sizeof in the internal unnamed structures, we need a real
1123 * instance (can't do "sizeof(struct rpc_t.u.reply))", unfortunately).
1124 * The compiler doesn't complain nor allocates the actual structure
1126 static struct rpc_t rpc_specimen
;
1127 #define IP_PKTSIZE (CONFIG_NET_MAXDEFRAG + sizeof(rpc_specimen.u.reply))
1129 #define IP_MAXUDP (IP_PKTSIZE - IP_HDR_SIZE_NO_UDP)
1132 * this is the packet being assembled, either data or frag control.
1133 * Fragments go by 8 bytes, so this union must be 8 bytes long
1136 /* first_byte is address of this structure */
1137 u16 last_byte
; /* last byte in this hole + 1 (begin of next hole) */
1138 u16 next_hole
; /* index of next (in 8-b blocks), 0 == none */
1139 u16 prev_hole
; /* index of prev, 0 == none */
1143 static IP_t
*__NetDefragment(IP_t
*ip
, int *lenp
)
1145 static uchar pkt_buff
[IP_PKTSIZE
] __attribute__((aligned(PKTALIGN
)));
1146 static u16 first_hole
, total_len
;
1147 struct hole
*payload
, *thisfrag
, *h
, *newh
;
1148 IP_t
*localip
= (IP_t
*)pkt_buff
;
1149 uchar
*indata
= (uchar
*)ip
;
1150 int offset8
, start
, len
, done
= 0;
1151 u16 ip_off
= ntohs(ip
->ip_off
);
1153 /* payload starts after IP header, this fragment is in there */
1154 payload
= (struct hole
*)(pkt_buff
+ IP_HDR_SIZE_NO_UDP
);
1155 offset8
= (ip_off
& IP_OFFS
);
1156 thisfrag
= payload
+ offset8
;
1157 start
= offset8
* 8;
1158 len
= ntohs(ip
->ip_len
) - IP_HDR_SIZE_NO_UDP
;
1160 if (start
+ len
> IP_MAXUDP
) /* fragment extends too far */
1163 if (!total_len
|| localip
->ip_id
!= ip
->ip_id
) {
1164 /* new (or different) packet, reset structs */
1166 payload
[0].last_byte
= ~0;
1167 payload
[0].next_hole
= 0;
1168 payload
[0].prev_hole
= 0;
1170 /* any IP header will work, copy the first we received */
1171 memcpy(localip
, ip
, IP_HDR_SIZE_NO_UDP
);
1175 * What follows is the reassembly algorithm. We use the payload
1176 * array as a linked list of hole descriptors, as each hole starts
1177 * at a multiple of 8 bytes. However, last byte can be whatever value,
1178 * so it is represented as byte count, not as 8-byte blocks.
1181 h
= payload
+ first_hole
;
1182 while (h
->last_byte
< start
) {
1183 if (!h
->next_hole
) {
1184 /* no hole that far away */
1187 h
= payload
+ h
->next_hole
;
1190 if (offset8
+ (len
/ 8) <= h
- payload
) {
1191 /* no overlap with holes (dup fragment?) */
1195 if (!(ip_off
& IP_FLAGS_MFRAG
)) {
1196 /* no more fragmentss: truncate this (last) hole */
1197 total_len
= start
+ len
;
1198 h
->last_byte
= start
+ len
;
1202 * There is some overlap: fix the hole list. This code doesn't
1203 * deal with a fragment that overlaps with two different holes
1204 * (thus being a superset of a previously-received fragment).
1207 if ( (h
>= thisfrag
) && (h
->last_byte
<= start
+ len
) ) {
1208 /* complete overlap with hole: remove hole */
1209 if (!h
->prev_hole
&& !h
->next_hole
) {
1210 /* last remaining hole */
1212 } else if (!h
->prev_hole
) {
1214 first_hole
= h
->next_hole
;
1215 payload
[h
->next_hole
].prev_hole
= 0;
1216 } else if (!h
->next_hole
) {
1218 payload
[h
->prev_hole
].next_hole
= 0;
1220 /* in the middle of the list */
1221 payload
[h
->next_hole
].prev_hole
= h
->prev_hole
;
1222 payload
[h
->prev_hole
].next_hole
= h
->next_hole
;
1225 } else if (h
->last_byte
<= start
+ len
) {
1226 /* overlaps with final part of the hole: shorten this hole */
1227 h
->last_byte
= start
;
1229 } else if (h
>= thisfrag
) {
1230 /* overlaps with initial part of the hole: move this hole */
1231 newh
= thisfrag
+ (len
/ 8);
1235 payload
[h
->next_hole
].prev_hole
= (h
- payload
);
1237 payload
[h
->prev_hole
].next_hole
= (h
- payload
);
1239 first_hole
= (h
- payload
);
1242 /* fragment sits in the middle: split the hole */
1243 newh
= thisfrag
+ (len
/ 8);
1245 h
->last_byte
= start
;
1246 h
->next_hole
= (newh
- payload
);
1247 newh
->prev_hole
= (h
- payload
);
1248 if (newh
->next_hole
)
1249 payload
[newh
->next_hole
].prev_hole
= (newh
- payload
);
1252 /* finally copy this fragment and possibly return whole packet */
1253 memcpy((uchar
*)thisfrag
, indata
+ IP_HDR_SIZE_NO_UDP
, len
);
1257 localip
->ip_len
= htons(total_len
);
1258 *lenp
= total_len
+ IP_HDR_SIZE_NO_UDP
;
1262 static inline IP_t
*NetDefragment(IP_t
*ip
, int *lenp
)
1264 u16 ip_off
= ntohs(ip
->ip_off
);
1265 if (!(ip_off
& (IP_OFFS
| IP_FLAGS_MFRAG
)))
1266 return ip
; /* not a fragment */
1267 return __NetDefragment(ip
, lenp
);
1270 #else /* !CONFIG_IP_DEFRAG */
1272 static inline IP_t
*NetDefragment(IP_t
*ip
, int *lenp
)
1274 u16 ip_off
= ntohs(ip
->ip_off
);
1275 if (!(ip_off
& (IP_OFFS
| IP_FLAGS_MFRAG
)))
1276 return ip
; /* not a fragment */
1282 NetReceive(volatile uchar
* inpkt
, int len
)
1290 #if defined(CONFIG_CMD_CDP)
1293 ushort cti
= 0, vlanid
= VLAN_NONE
, myvlanid
, mynvlanid
;
1295 debug("packet received\n");
1297 NetRxPacket
= inpkt
;
1298 NetRxPacketLen
= len
;
1299 et
= (Ethernet_t
*)inpkt
;
1301 /* too small packet? */
1302 if (len
< ETHER_HDR_SIZE
)
1307 (*push_packet
)(inpkt
, len
);
1312 #if defined(CONFIG_CMD_CDP)
1313 /* keep track if packet is CDP */
1314 iscdp
= memcmp(et
->et_dest
, NetCDPAddr
, 6) == 0;
1317 myvlanid
= ntohs(NetOurVLAN
);
1318 if (myvlanid
== (ushort
)-1)
1319 myvlanid
= VLAN_NONE
;
1320 mynvlanid
= ntohs(NetOurNativeVLAN
);
1321 if (mynvlanid
== (ushort
)-1)
1322 mynvlanid
= VLAN_NONE
;
1324 x
= ntohs(et
->et_protlen
);
1326 debug("packet received\n");
1330 * Got a 802 packet. Check the other protocol field.
1332 x
= ntohs(et
->et_prot
);
1334 ip
= (IP_t
*)(inpkt
+ E802_HDR_SIZE
);
1335 len
-= E802_HDR_SIZE
;
1337 } else if (x
!= PROT_VLAN
) { /* normal packet */
1338 ip
= (IP_t
*)(inpkt
+ ETHER_HDR_SIZE
);
1339 len
-= ETHER_HDR_SIZE
;
1341 } else { /* VLAN packet */
1342 VLAN_Ethernet_t
*vet
= (VLAN_Ethernet_t
*)et
;
1344 debug("VLAN packet received\n");
1346 /* too small packet? */
1347 if (len
< VLAN_ETHER_HDR_SIZE
)
1350 /* if no VLAN active */
1351 if ((ntohs(NetOurVLAN
) & VLAN_IDMASK
) == VLAN_NONE
1352 #if defined(CONFIG_CMD_CDP)
1358 cti
= ntohs(vet
->vet_tag
);
1359 vlanid
= cti
& VLAN_IDMASK
;
1360 x
= ntohs(vet
->vet_type
);
1362 ip
= (IP_t
*)(inpkt
+ VLAN_ETHER_HDR_SIZE
);
1363 len
-= VLAN_ETHER_HDR_SIZE
;
1366 debug("Receive from protocol 0x%x\n", x
);
1368 #if defined(CONFIG_CMD_CDP)
1370 CDPHandler((uchar
*)ip
, len
);
1375 if ((myvlanid
& VLAN_IDMASK
) != VLAN_NONE
) {
1376 if (vlanid
== VLAN_NONE
)
1377 vlanid
= (mynvlanid
& VLAN_IDMASK
);
1379 if (vlanid
!= (myvlanid
& VLAN_IDMASK
))
1387 * We have to deal with two types of ARP packets:
1388 * - REQUEST packets will be answered by sending our
1389 * IP address - if we know it.
1390 * - REPLY packates are expected only after we asked
1391 * for the TFTP server's or the gateway's ethernet
1392 * address; so if we receive such a packet, we set
1393 * the server ethernet address
1398 if (len
< ARP_HDR_SIZE
) {
1399 printf("bad length %d < %d\n", len
, ARP_HDR_SIZE
);
1402 if (ntohs(arp
->ar_hrd
) != ARP_ETHER
) {
1405 if (ntohs(arp
->ar_pro
) != PROT_IP
) {
1408 if (arp
->ar_hln
!= 6) {
1411 if (arp
->ar_pln
!= 4) {
1415 if (NetOurIP
== 0) {
1419 if (NetReadIP(&arp
->ar_data
[16]) != NetOurIP
) {
1423 switch (ntohs(arp
->ar_op
)) {
1424 case ARPOP_REQUEST
: /* reply with our IP address */
1425 debug("Got ARP REQUEST, return our IP\n");
1427 pkt
+= NetSetEther(pkt
, et
->et_src
, PROT_ARP
);
1428 arp
->ar_op
= htons(ARPOP_REPLY
);
1429 memcpy (&arp
->ar_data
[10], &arp
->ar_data
[0], 6);
1430 NetCopyIP(&arp
->ar_data
[16], &arp
->ar_data
[6]);
1431 memcpy (&arp
->ar_data
[ 0], NetOurEther
, 6);
1432 NetCopyIP(&arp
->ar_data
[ 6], &NetOurIP
);
1433 (void) eth_send((uchar
*)et
, (pkt
- (uchar
*)et
) + ARP_HDR_SIZE
);
1436 case ARPOP_REPLY
: /* arp reply */
1437 /* are we waiting for a reply */
1438 if (!NetArpWaitPacketIP
|| !NetArpWaitPacketMAC
)
1441 #ifdef CONFIG_KEEP_SERVERADDR
1442 if (NetServerIP
== NetArpWaitPacketIP
) {
1444 sprintf(buf
, "%pM", arp
->ar_data
);
1445 setenv("serveraddr", buf
);
1449 debug("Got ARP REPLY, set server/gtwy eth addr (%pM)\n",
1452 tmp
= NetReadIP(&arp
->ar_data
[6]);
1454 /* matched waiting packet's address */
1455 if (tmp
== NetArpWaitReplyIP
) {
1457 /* save address for later use */
1458 memcpy(NetArpWaitPacketMAC
, &arp
->ar_data
[0], 6);
1460 #ifdef CONFIG_NETCONSOLE
1461 (*packetHandler
)(0,0,0,0);
1463 /* modify header, and transmit it */
1464 memcpy(((Ethernet_t
*)NetArpWaitTxPacket
)->et_dest
, NetArpWaitPacketMAC
, 6);
1465 (void) eth_send(NetArpWaitTxPacket
, NetArpWaitTxPacketSize
);
1467 /* no arp request pending now */
1468 NetArpWaitPacketIP
= 0;
1469 NetArpWaitTxPacketSize
= 0;
1470 NetArpWaitPacketMAC
= NULL
;
1475 debug("Unexpected ARP opcode 0x%x\n", ntohs(arp
->ar_op
));
1481 debug("Got RARP\n");
1483 if (len
< ARP_HDR_SIZE
) {
1484 printf("bad length %d < %d\n", len
, ARP_HDR_SIZE
);
1488 if ((ntohs(arp
->ar_op
) != RARPOP_REPLY
) ||
1489 (ntohs(arp
->ar_hrd
) != ARP_ETHER
) ||
1490 (ntohs(arp
->ar_pro
) != PROT_IP
) ||
1491 (arp
->ar_hln
!= 6) || (arp
->ar_pln
!= 4)) {
1493 puts ("invalid RARP header\n");
1495 NetCopyIP(&NetOurIP
, &arp
->ar_data
[16]);
1496 if (NetServerIP
== 0)
1497 NetCopyIP(&NetServerIP
, &arp
->ar_data
[ 6]);
1498 memcpy (NetServerEther
, &arp
->ar_data
[ 0], 6);
1500 (*packetHandler
)(0,0,0,0);
1506 /* Before we start poking the header, make sure it is there */
1507 if (len
< IP_HDR_SIZE
) {
1508 debug("len bad %d < %lu\n", len
, (ulong
)IP_HDR_SIZE
);
1511 /* Check the packet length */
1512 if (len
< ntohs(ip
->ip_len
)) {
1513 printf("len bad %d < %d\n", len
, ntohs(ip
->ip_len
));
1516 len
= ntohs(ip
->ip_len
);
1517 debug("len=%d, v=%02x\n", len
, ip
->ip_hl_v
& 0xff);
1519 /* Can't deal with anything except IPv4 */
1520 if ((ip
->ip_hl_v
& 0xf0) != 0x40) {
1523 /* Can't deal with IP options (headers != 20 bytes) */
1524 if ((ip
->ip_hl_v
& 0x0f) > 0x05) {
1527 /* Check the Checksum of the header */
1528 if (!NetCksumOk((uchar
*)ip
, IP_HDR_SIZE_NO_UDP
/ 2)) {
1529 puts ("checksum bad\n");
1532 /* If it is not for us, ignore it */
1533 tmp
= NetReadIP(&ip
->ip_dst
);
1534 if (NetOurIP
&& tmp
!= NetOurIP
&& tmp
!= 0xFFFFFFFF) {
1535 #ifdef CONFIG_MCAST_TFTP
1536 if (Mcast_addr
!= tmp
)
1541 * The function returns the unchanged packet if it's not
1542 * a fragment, and either the complete packet or NULL if
1543 * it is a fragment (if !CONFIG_IP_DEFRAG, it returns NULL)
1545 if (!(ip
= NetDefragment(ip
, &len
)))
1548 * watch for ICMP host redirects
1550 * There is no real handler code (yet). We just watch
1551 * for ICMP host redirect messages. In case anybody
1552 * sees these messages: please contact me
1553 * (wd@denx.de), or - even better - send me the
1554 * necessary fixes :-)
1556 * Note: in all cases where I have seen this so far
1557 * it was a problem with the router configuration,
1558 * for instance when a router was configured in the
1559 * BOOTP reply, but the TFTP server was on the same
1560 * subnet. So this is probably a warning that your
1561 * configuration might be wrong. But I'm not really
1562 * sure if there aren't any other situations.
1564 if (ip
->ip_p
== IPPROTO_ICMP
) {
1565 ICMP_t
*icmph
= (ICMP_t
*)&(ip
->udp_src
);
1567 switch (icmph
->type
) {
1569 if (icmph
->code
!= ICMP_REDIR_HOST
)
1571 printf (" ICMP Host Redirect to %pI4 ", &icmph
->un
.gateway
);
1573 #if defined(CONFIG_CMD_PING)
1574 case ICMP_ECHO_REPLY
:
1576 * IP header OK. Pass the packet to the current handler.
1578 /* XXX point to ip packet */
1579 (*packetHandler
)((uchar
*)ip
, 0, 0, 0);
1581 case ICMP_ECHO_REQUEST
:
1582 debug("Got ICMP ECHO REQUEST, return %d bytes \n",
1583 ETHER_HDR_SIZE
+ len
);
1585 memcpy (&et
->et_dest
[0], &et
->et_src
[0], 6);
1586 memcpy (&et
->et_src
[ 0], NetOurEther
, 6);
1590 NetCopyIP((void*)&ip
->ip_dst
, &ip
->ip_src
);
1591 NetCopyIP((void*)&ip
->ip_src
, &NetOurIP
);
1592 ip
->ip_sum
= ~NetCksum((uchar
*)ip
, IP_HDR_SIZE_NO_UDP
>> 1);
1594 icmph
->type
= ICMP_ECHO_REPLY
;
1595 icmph
->checksum
= 0;
1596 icmph
->checksum
= ~NetCksum((uchar
*)icmph
,
1597 (len
- IP_HDR_SIZE_NO_UDP
) >> 1);
1598 (void) eth_send((uchar
*)et
, ETHER_HDR_SIZE
+ len
);
1604 } else if (ip
->ip_p
!= IPPROTO_UDP
) { /* Only UDP packets */
1608 #ifdef CONFIG_UDP_CHECKSUM
1609 if (ip
->udp_xsum
!= 0) {
1615 xsum
+= (ntohs(ip
->udp_len
));
1616 xsum
+= (ntohl(ip
->ip_src
) >> 16) & 0x0000ffff;
1617 xsum
+= (ntohl(ip
->ip_src
) >> 0) & 0x0000ffff;
1618 xsum
+= (ntohl(ip
->ip_dst
) >> 16) & 0x0000ffff;
1619 xsum
+= (ntohl(ip
->ip_dst
) >> 0) & 0x0000ffff;
1621 sumlen
= ntohs(ip
->udp_len
);
1622 sumptr
= (ushort
*) &(ip
->udp_src
);
1624 while (sumlen
> 1) {
1627 sumdata
= *sumptr
++;
1628 xsum
+= ntohs(sumdata
);
1634 sumdata
= *(unsigned char *) sumptr
;
1635 sumdata
= (sumdata
<< 8) & 0xff00;
1638 while ((xsum
>> 16) != 0) {
1639 xsum
= (xsum
& 0x0000ffff) + ((xsum
>> 16) & 0x0000ffff);
1641 if ((xsum
!= 0x00000000) && (xsum
!= 0x0000ffff)) {
1642 printf(" UDP wrong checksum %08lx %08x\n",
1643 xsum
, ntohs(ip
->udp_xsum
));
1650 #ifdef CONFIG_NETCONSOLE
1651 nc_input_packet((uchar
*)ip
+IP_HDR_SIZE
,
1654 ntohs(ip
->udp_len
) - 8);
1657 * IP header OK. Pass the packet to the current handler.
1659 (*packetHandler
)((uchar
*)ip
+IP_HDR_SIZE
,
1662 ntohs(ip
->udp_len
) - 8);
1668 /**********************************************************************/
1670 static int net_check_prereq (proto_t protocol
)
1674 #if defined(CONFIG_CMD_PING)
1676 if (NetPingIP
== 0) {
1677 puts ("*** ERROR: ping address not given\n");
1682 #if defined(CONFIG_CMD_SNTP)
1684 if (NetNtpServerIP
== 0) {
1685 puts ("*** ERROR: NTP server address not given\n");
1690 #if defined(CONFIG_CMD_DNS)
1692 if (NetOurDNSIP
== 0) {
1693 puts("*** ERROR: DNS server address not given\n");
1698 #if defined(CONFIG_CMD_NFS)
1703 if (NetServerIP
== 0) {
1704 puts ("*** ERROR: `serverip' not set\n");
1707 #if defined(CONFIG_CMD_PING) || defined(CONFIG_CMD_SNTP)
1711 if (NetOurIP
== 0) {
1712 puts ("*** ERROR: `ipaddr' not set\n");
1721 if (memcmp (NetOurEther
, "\0\0\0\0\0\0", 6) == 0) {
1722 #ifdef CONFIG_NET_MULTI
1723 extern int eth_get_dev_index (void);
1724 int num
= eth_get_dev_index ();
1728 puts ("*** ERROR: No ethernet found.\n");
1731 puts ("*** ERROR: `ethaddr' not set\n");
1734 printf ("*** ERROR: `eth%daddr' not set\n",
1742 puts ("*** ERROR: `ethaddr' not set\n");
1750 return (0); /* OK */
1752 /**********************************************************************/
1755 NetCksumOk(uchar
* ptr
, int len
)
1757 return !((NetCksum(ptr
, len
) + 1) & 0xfffe);
1762 NetCksum(uchar
* ptr
, int len
)
1765 ushort
*p
= (ushort
*)ptr
;
1770 xsum
= (xsum
& 0xffff) + (xsum
>> 16);
1771 xsum
= (xsum
& 0xffff) + (xsum
>> 16);
1772 return (xsum
& 0xffff);
1780 myvlanid
= ntohs(NetOurVLAN
);
1781 if (myvlanid
== (ushort
)-1)
1782 myvlanid
= VLAN_NONE
;
1784 return ((myvlanid
& VLAN_IDMASK
) == VLAN_NONE
) ? ETHER_HDR_SIZE
: VLAN_ETHER_HDR_SIZE
;
1788 NetSetEther(volatile uchar
* xet
, uchar
* addr
, uint prot
)
1790 Ethernet_t
*et
= (Ethernet_t
*)xet
;
1793 myvlanid
= ntohs(NetOurVLAN
);
1794 if (myvlanid
== (ushort
)-1)
1795 myvlanid
= VLAN_NONE
;
1797 memcpy (et
->et_dest
, addr
, 6);
1798 memcpy (et
->et_src
, NetOurEther
, 6);
1799 if ((myvlanid
& VLAN_IDMASK
) == VLAN_NONE
) {
1800 et
->et_protlen
= htons(prot
);
1801 return ETHER_HDR_SIZE
;
1803 VLAN_Ethernet_t
*vet
= (VLAN_Ethernet_t
*)xet
;
1805 vet
->vet_vlan_type
= htons(PROT_VLAN
);
1806 vet
->vet_tag
= htons((0 << 5) | (myvlanid
& VLAN_IDMASK
));
1807 vet
->vet_type
= htons(prot
);
1808 return VLAN_ETHER_HDR_SIZE
;
1813 NetSetIP(volatile uchar
* xip
, IPaddr_t dest
, int dport
, int sport
, int len
)
1815 IP_t
*ip
= (IP_t
*)xip
;
1818 * If the data is an odd number of bytes, zero the
1819 * byte after the last byte so that the checksum
1823 xip
[IP_HDR_SIZE
+ len
] = 0;
1826 * Construct an IP and UDP header.
1827 * (need to set no fragment bit - XXX)
1829 ip
->ip_hl_v
= 0x45; /* IP_HDR_SIZE / 4 (not including UDP) */
1831 ip
->ip_len
= htons(IP_HDR_SIZE
+ len
);
1832 ip
->ip_id
= htons(NetIPID
++);
1833 ip
->ip_off
= htons(IP_FLAGS_DFRAG
); /* Don't fragment */
1835 ip
->ip_p
= 17; /* UDP */
1837 NetCopyIP((void*)&ip
->ip_src
, &NetOurIP
); /* already in network byte order */
1838 NetCopyIP((void*)&ip
->ip_dst
, &dest
); /* - "" - */
1839 ip
->udp_src
= htons(sport
);
1840 ip
->udp_dst
= htons(dport
);
1841 ip
->udp_len
= htons(8 + len
);
1843 ip
->ip_sum
= ~NetCksum((uchar
*)ip
, IP_HDR_SIZE_NO_UDP
/ 2);
1846 void copy_filename (char *dst
, char *src
, int size
)
1848 if (*src
&& (*src
== '"')) {
1853 while ((--size
> 0) && *src
&& (*src
!= '"')) {
1861 #if defined(CONFIG_CMD_NFS) || defined(CONFIG_CMD_SNTP) || defined(CONFIG_CMD_DNS)
1863 * make port a little random, but use something trivial to compute
1865 unsigned int random_port(void)
1867 return 1024 + (get_timer(0) % 0x8000);;
1871 void ip_to_string (IPaddr_t x
, char *s
)
1874 sprintf (s
, "%d.%d.%d.%d",
1875 (int) ((x
>> 24) & 0xff),
1876 (int) ((x
>> 16) & 0xff),
1877 (int) ((x
>> 8) & 0xff), (int) ((x
>> 0) & 0xff)
1881 IPaddr_t
string_to_ip(char *s
)
1890 for (addr
=0, i
=0; i
<4; ++i
) {
1891 ulong val
= s
? simple_strtoul(s
, &e
, 10) : 0;
1893 addr
|= (val
& 0xFF);
1899 return (htonl(addr
));
1902 void VLAN_to_string(ushort x
, char *s
)
1906 if (x
== (ushort
)-1)
1912 sprintf(s
, "%d", x
& VLAN_IDMASK
);
1915 ushort
string_to_VLAN(char *s
)
1920 return htons(VLAN_NONE
);
1922 if (*s
< '0' || *s
> '9')
1925 id
= (ushort
)simple_strtoul(s
, NULL
, 10);
1930 IPaddr_t
getenv_IPaddr (char *var
)
1932 return (string_to_ip(getenv(var
)));
1935 ushort
getenv_VLAN(char *var
)
1937 return (string_to_VLAN(getenv(var
)));