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
83 #ifdef CONFIG_CMD_RARP
87 #ifdef CONFIG_STATUS_LED
88 #include <status_led.h>
91 #if defined(CONFIG_CMD_SNTP)
94 #if defined(CONFIG_CDP_VERSION)
95 #include <timestamp.h>
97 #if defined(CONFIG_CMD_DNS)
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 static int NetTryCount
;
202 /**********************************************************************/
204 IPaddr_t NetArpWaitPacketIP
;
205 IPaddr_t NetArpWaitReplyIP
;
206 uchar
*NetArpWaitPacketMAC
; /* MAC address of waiting packet's destination */
207 uchar
*NetArpWaitTxPacket
; /* THE transmit packet */
208 int NetArpWaitTxPacketSize
;
209 uchar NetArpWaitPacketBuf
[PKTSIZE_ALIGN
+ PKTALIGN
];
210 ulong NetArpWaitTimerStart
;
213 void ArpRequest (void)
219 debug("ARP broadcast %d\n", NetArpWaitTry
);
223 pkt
+= NetSetEther (pkt
, NetBcastAddr
, PROT_ARP
);
227 arp
->ar_hrd
= htons (ARP_ETHER
);
228 arp
->ar_pro
= htons (PROT_IP
);
231 arp
->ar_op
= htons (ARPOP_REQUEST
);
233 memcpy (&arp
->ar_data
[0], NetOurEther
, 6); /* source ET addr */
234 NetWriteIP ((uchar
*) & arp
->ar_data
[6], NetOurIP
); /* source IP addr */
235 for (i
= 10; i
< 16; ++i
) {
236 arp
->ar_data
[i
] = 0; /* dest ET addr = 0 */
239 if ((NetArpWaitPacketIP
& NetOurSubnetMask
) !=
240 (NetOurIP
& NetOurSubnetMask
)) {
241 if (NetOurGatewayIP
== 0) {
242 puts ("## Warning: gatewayip needed but not set\n");
243 NetArpWaitReplyIP
= NetArpWaitPacketIP
;
245 NetArpWaitReplyIP
= NetOurGatewayIP
;
248 NetArpWaitReplyIP
= NetArpWaitPacketIP
;
251 NetWriteIP ((uchar
*) & arp
->ar_data
[16], NetArpWaitReplyIP
);
252 (void) eth_send (NetTxPacket
, (pkt
- NetTxPacket
) + ARP_HDR_SIZE
);
255 void ArpTimeoutCheck(void)
259 if (!NetArpWaitPacketIP
)
264 /* check for arp timeout */
265 if ((t
- NetArpWaitTimerStart
) > ARP_TIMEOUT
) {
268 if (NetArpWaitTry
>= ARP_TIMEOUT_COUNT
) {
269 puts ("\nARP Retry count exceeded; starting again\n");
273 NetArpWaitTimerStart
= t
;
280 NetInitLoop(proto_t protocol
)
282 static int env_changed_id
= 0;
284 int env_id
= get_env_id ();
286 /* update only when the environment has changed */
287 if (env_changed_id
!= env_id
) {
288 NetCopyIP(&NetOurIP
, &bd
->bi_ip_addr
);
289 NetOurGatewayIP
= getenv_IPaddr ("gatewayip");
290 NetOurSubnetMask
= getenv_IPaddr ("netmask");
291 NetServerIP
= getenv_IPaddr ("serverip");
292 NetOurNativeVLAN
= getenv_VLAN("nvlan");
293 NetOurVLAN
= getenv_VLAN("vlan");
294 #if defined(CONFIG_CMD_DNS)
295 NetOurDNSIP
= getenv_IPaddr("dnsip");
297 env_changed_id
= env_id
;
303 /**********************************************************************/
305 * Main network processing loop.
309 NetLoop(proto_t protocol
)
313 #ifdef CONFIG_NET_MULTI
318 /* XXX problem with bss workaround */
319 NetArpWaitPacketMAC
= NULL
;
320 NetArpWaitTxPacket
= NULL
;
321 NetArpWaitPacketIP
= 0;
322 NetArpWaitReplyIP
= 0;
323 NetArpWaitTxPacket
= NULL
;
330 * Setup packet buffers, aligned correctly.
332 NetTxPacket
= &PktBuf
[0] + (PKTALIGN
- 1);
333 NetTxPacket
-= (ulong
)NetTxPacket
% PKTALIGN
;
334 for (i
= 0; i
< PKTBUFSRX
; i
++) {
335 NetRxPackets
[i
] = NetTxPacket
+ (i
+1)*PKTSIZE_ALIGN
;
339 if (!NetArpWaitTxPacket
) {
340 NetArpWaitTxPacket
= &NetArpWaitPacketBuf
[0] + (PKTALIGN
- 1);
341 NetArpWaitTxPacket
-= (ulong
)NetArpWaitTxPacket
% PKTALIGN
;
342 NetArpWaitTxPacketSize
= 0;
346 #ifdef CONFIG_NET_MULTI
349 if (eth_init(bd
) < 0) {
355 #ifdef CONFIG_NET_MULTI
356 memcpy (NetOurEther
, eth_get_dev()->enetaddr
, 6);
358 eth_getenv_enetaddr("ethaddr", NetOurEther
);
361 NetState
= NETLOOP_CONTINUE
;
364 * Start the ball rolling with the given start function. From
365 * here on, this code is a state machine driven by received
366 * packets and timer events.
368 NetInitLoop(protocol
);
370 switch (net_check_prereq (protocol
)) {
372 /* network not configured */
376 #ifdef CONFIG_NET_MULTI
378 /* network device not configured */
380 #endif /* CONFIG_NET_MULTI */
383 #ifdef CONFIG_NET_MULTI
388 /* always use ARP to get server ethernet address */
392 #if defined(CONFIG_CMD_DHCP)
396 DhcpRequest(); /* Basically same as BOOTP */
406 #if defined(CONFIG_CMD_RARP)
413 #if defined(CONFIG_CMD_PING)
418 #if defined(CONFIG_CMD_NFS)
423 #if defined(CONFIG_CMD_CDP)
428 #ifdef CONFIG_NETCONSOLE
433 #if defined(CONFIG_CMD_SNTP)
438 #if defined(CONFIG_CMD_DNS)
447 NetBootFileXferSize
= 0;
451 #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
452 #if defined(CONFIG_SYS_FAULT_ECHO_LINK_DOWN) && defined(CONFIG_STATUS_LED) && defined(STATUS_LED_RED)
454 * Echo the inverted link state to the fault LED.
456 if(miiphy_link(eth_get_dev()->name
, CONFIG_SYS_FAULT_MII_ADDR
)) {
457 status_led_set (STATUS_LED_RED
, STATUS_LED_OFF
);
459 status_led_set (STATUS_LED_RED
, STATUS_LED_ON
);
461 #endif /* CONFIG_SYS_FAULT_ECHO_LINK_DOWN, ... */
462 #endif /* CONFIG_MII, ... */
465 * Main packet reception loop. Loop receiving packets until
466 * someone sets `NetState' to a state that terminates.
470 #ifdef CONFIG_SHOW_ACTIVITY
472 extern void show_activity(int arg
);
477 * Check the ethernet for a new packet. The ethernet
478 * receive routine will process it.
483 * Abort if ctrl-c was pressed.
494 * Check for a timeout, and run the timeout handler
497 if (timeHandler
&& ((get_timer(0) - timeStart
) > timeDelta
)) {
500 #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
501 # if defined(CONFIG_SYS_FAULT_ECHO_LINK_DOWN) && \
502 defined(CONFIG_STATUS_LED) && \
503 defined(STATUS_LED_RED)
505 * Echo the inverted link state to the fault LED.
507 if(miiphy_link(eth_get_dev()->name
, CONFIG_SYS_FAULT_MII_ADDR
)) {
508 status_led_set (STATUS_LED_RED
, STATUS_LED_OFF
);
510 status_led_set (STATUS_LED_RED
, STATUS_LED_ON
);
512 # endif /* CONFIG_SYS_FAULT_ECHO_LINK_DOWN, ... */
513 #endif /* CONFIG_MII, ... */
515 timeHandler
= (thand_f
*)0;
522 case NETLOOP_RESTART
:
523 #ifdef CONFIG_NET_MULTI
528 case NETLOOP_SUCCESS
:
529 if (NetBootFileXferSize
> 0) {
531 printf("Bytes transferred = %ld (%lx hex)\n",
533 NetBootFileXferSize
);
534 sprintf(buf
, "%lX", NetBootFileXferSize
);
535 setenv("filesize", buf
);
537 sprintf(buf
, "%lX", (unsigned long)load_addr
);
538 setenv("fileaddr", buf
);
541 return NetBootFileXferSize
;
549 /**********************************************************************/
552 startAgainTimeout(void)
554 NetState
= NETLOOP_RESTART
;
558 startAgainHandler(uchar
* pkt
, unsigned dest
, unsigned src
, unsigned len
)
560 /* Totally ignore the packet */
563 void NetStartAgain (void)
566 int retry_forever
= 0;
567 unsigned long retrycnt
= 0;
569 nretry
= getenv("netretry");
571 if (!strcmp(nretry
, "yes"))
573 else if (!strcmp(nretry
, "no"))
575 else if (!strcmp(nretry
, "once"))
578 retrycnt
= simple_strtoul(nretry
, NULL
, 0);
582 if ((!retry_forever
) && (NetTryCount
>= retrycnt
)) {
584 NetState
= NETLOOP_FAIL
;
590 #ifndef CONFIG_NET_MULTI
591 NetSetTimeout (10000UL, startAgainTimeout
);
592 NetSetHandler (startAgainHandler
);
593 #else /* !CONFIG_NET_MULTI*/
595 #if !defined(CONFIG_NET_DO_NOT_TRY_ANOTHER)
596 eth_try_another (!NetRestarted
);
599 if (NetRestartWrap
) {
602 NetSetTimeout (10000UL, startAgainTimeout
);
603 NetSetHandler (startAgainHandler
);
605 NetState
= NETLOOP_FAIL
;
608 NetState
= NETLOOP_RESTART
;
610 #endif /* CONFIG_NET_MULTI */
613 /**********************************************************************/
619 NetSetHandler(rxhand_f
* f
)
626 NetSetTimeout(ulong iv
, thand_f
* f
)
629 timeHandler
= (thand_f
*)0;
632 timeStart
= get_timer(0);
639 NetSendPacket(volatile uchar
* pkt
, int len
)
641 (void) eth_send(pkt
, len
);
645 NetSendUDPPacket(uchar
*ether
, IPaddr_t dest
, int dport
, int sport
, int len
)
649 /* convert to new style broadcast */
653 /* if broadcast, make the ether address a broadcast and don't do ARP */
654 if (dest
== 0xFFFFFFFF)
655 ether
= NetBcastAddr
;
657 /* if MAC address was not discovered yet, save the packet and do an ARP request */
658 if (memcmp(ether
, NetEtherNullAddr
, 6) == 0) {
660 debug("sending ARP for %08lx\n", dest
);
662 NetArpWaitPacketIP
= dest
;
663 NetArpWaitPacketMAC
= ether
;
665 pkt
= NetArpWaitTxPacket
;
666 pkt
+= NetSetEther (pkt
, NetArpWaitPacketMAC
, PROT_IP
);
668 NetSetIP (pkt
, dest
, dport
, sport
, len
);
669 memcpy(pkt
+ IP_HDR_SIZE
, (uchar
*)NetTxPacket
+ (pkt
- (uchar
*)NetArpWaitTxPacket
) + IP_HDR_SIZE
, len
);
671 /* size of the waiting packet */
672 NetArpWaitTxPacketSize
= (pkt
- NetArpWaitTxPacket
) + IP_HDR_SIZE
+ len
;
674 /* and do the ARP request */
676 NetArpWaitTimerStart
= get_timer(0);
678 return 1; /* waiting */
681 debug("sending UDP to %08lx/%pM\n", dest
, ether
);
683 pkt
= (uchar
*)NetTxPacket
;
684 pkt
+= NetSetEther (pkt
, ether
, PROT_IP
);
685 NetSetIP (pkt
, dest
, dport
, sport
, len
);
686 (void) eth_send(NetTxPacket
, (pkt
- NetTxPacket
) + IP_HDR_SIZE
+ len
);
688 return 0; /* transmitted */
691 #if defined(CONFIG_CMD_PING)
692 static ushort PingSeqNo
;
701 /* XXX always send arp request */
703 memcpy(mac
, NetEtherNullAddr
, 6);
705 debug("sending ARP for %08lx\n", NetPingIP
);
707 NetArpWaitPacketIP
= NetPingIP
;
708 NetArpWaitPacketMAC
= mac
;
710 pkt
= NetArpWaitTxPacket
;
711 pkt
+= NetSetEther(pkt
, mac
, PROT_IP
);
713 ip
= (volatile IP_t
*)pkt
;
716 * Construct an IP and ICMP header. (need to set no fragment bit - XXX)
718 ip
->ip_hl_v
= 0x45; /* IP_HDR_SIZE / 4 (not including UDP) */
720 ip
->ip_len
= htons(IP_HDR_SIZE_NO_UDP
+ 8);
721 ip
->ip_id
= htons(NetIPID
++);
722 ip
->ip_off
= htons(IP_FLAGS_DFRAG
); /* Don't fragment */
724 ip
->ip_p
= 0x01; /* ICMP */
726 NetCopyIP((void*)&ip
->ip_src
, &NetOurIP
); /* already in network byte order */
727 NetCopyIP((void*)&ip
->ip_dst
, &NetPingIP
); /* - "" - */
728 ip
->ip_sum
= ~NetCksum((uchar
*)ip
, IP_HDR_SIZE_NO_UDP
/ 2);
730 s
= &ip
->udp_src
; /* XXX ICMP starts here */
731 s
[0] = htons(0x0800); /* echo-request, code */
732 s
[1] = 0; /* checksum */
733 s
[2] = 0; /* identifier */
734 s
[3] = htons(PingSeqNo
++); /* sequence number */
735 s
[1] = ~NetCksum((uchar
*)s
, 8/2);
737 /* size of the waiting packet */
738 NetArpWaitTxPacketSize
= (pkt
- NetArpWaitTxPacket
) + IP_HDR_SIZE_NO_UDP
+ 8;
740 /* and do the ARP request */
742 NetArpWaitTimerStart
= get_timer(0);
744 return 1; /* waiting */
751 NetState
= NETLOOP_FAIL
; /* we did not get the reply */
755 PingHandler (uchar
* pkt
, unsigned dest
, unsigned src
, unsigned len
)
758 volatile IP_t
*ip
= (volatile IP_t
*)pkt
;
760 tmp
= NetReadIP((void *)&ip
->ip_src
);
761 if (tmp
!= NetPingIP
)
764 NetState
= NETLOOP_SUCCESS
;
767 static void PingStart(void)
769 #if defined(CONFIG_NET_MULTI)
770 printf ("Using %s device\n", eth_get_name());
771 #endif /* CONFIG_NET_MULTI */
772 NetSetTimeout (10000UL, PingTimeout
);
773 NetSetHandler (PingHandler
);
779 #if defined(CONFIG_CMD_CDP)
781 #define CDP_DEVICE_ID_TLV 0x0001
782 #define CDP_ADDRESS_TLV 0x0002
783 #define CDP_PORT_ID_TLV 0x0003
784 #define CDP_CAPABILITIES_TLV 0x0004
785 #define CDP_VERSION_TLV 0x0005
786 #define CDP_PLATFORM_TLV 0x0006
787 #define CDP_NATIVE_VLAN_TLV 0x000a
788 #define CDP_APPLIANCE_VLAN_TLV 0x000e
789 #define CDP_TRIGGER_TLV 0x000f
790 #define CDP_POWER_CONSUMPTION_TLV 0x0010
791 #define CDP_SYSNAME_TLV 0x0014
792 #define CDP_SYSOBJECT_TLV 0x0015
793 #define CDP_MANAGEMENT_ADDRESS_TLV 0x0016
795 #define CDP_TIMEOUT 250UL /* one packet every 250ms */
800 ushort CDPNativeVLAN
;
801 ushort CDPApplianceVLAN
;
803 static const uchar CDP_SNAP_hdr
[8] = { 0xAA, 0xAA, 0x03, 0x00, 0x00, 0x0C, 0x20, 0x00 };
805 static ushort
CDP_compute_csum(const uchar
*buff
, ushort len
)
814 odd
= 1 & (ulong
)buff
;
824 if (result
& 0x80000000)
825 result
= (result
& 0xFFFF) + (result
>> 16);
829 leftover
= (signed short)(*(const signed char *)buff
);
830 /* CISCO SUCKS big time! (and blows too):
831 * CDP uses the IP checksum algorithm with a twist;
832 * for the last byte it *sign* extends and sums.
834 result
= (result
& 0xffff0000) | ((result
+ leftover
) & 0x0000ffff);
837 result
= (result
& 0xFFFF) + (result
>> 16);
840 result
= ((result
>> 8) & 0xff) | ((result
& 0xff) << 8);
843 /* add up 16-bit and 17-bit words for 17+c bits */
844 result
= (result
& 0xffff) + (result
>> 16);
845 /* add up 16-bit and 2-bit for 16+c bit */
846 result
= (result
& 0xffff) + (result
>> 16);
848 result
= (result
& 0xffff) + (result
>> 16);
851 csum
= ~(ushort
)result
;
853 /* run time endian detection */
854 if (csum
!= htons(csum
)) /* little endian */
860 int CDPSendTrigger(void)
868 #if defined(CONFIG_CDP_DEVICE_ID) || defined(CONFIG_CDP_PORT_ID) || \
869 defined(CONFIG_CDP_VERSION) || defined(CONFIG_CDP_PLATFORM)
874 et
= (Ethernet_t
*)pkt
;
876 /* NOTE: trigger sent not on any VLAN */
878 /* form ethernet header */
879 memcpy(et
->et_dest
, NetCDPAddr
, 6);
880 memcpy(et
->et_src
, NetOurEther
, 6);
882 pkt
+= ETHER_HDR_SIZE
;
885 memcpy((uchar
*)pkt
, CDP_SNAP_hdr
, sizeof(CDP_SNAP_hdr
));
886 pkt
+= sizeof(CDP_SNAP_hdr
);
889 *pkt
++ = 0x02; /* CDP version 2 */
890 *pkt
++ = 180; /* TTL */
891 s
= (volatile ushort
*)pkt
;
893 *s
++ = htons(0); /* checksum (0 for later calculation) */
896 #ifdef CONFIG_CDP_DEVICE_ID
897 *s
++ = htons(CDP_DEVICE_ID_TLV
);
898 *s
++ = htons(CONFIG_CDP_DEVICE_ID
);
899 sprintf(buf
, CONFIG_CDP_DEVICE_ID_PREFIX
"%pm", NetOurEther
);
900 memcpy((uchar
*)s
, buf
, 16);
904 #ifdef CONFIG_CDP_PORT_ID
905 *s
++ = htons(CDP_PORT_ID_TLV
);
906 memset(buf
, 0, sizeof(buf
));
907 sprintf(buf
, CONFIG_CDP_PORT_ID
, eth_get_dev_index());
909 if (len
& 1) /* make it even */
911 *s
++ = htons(len
+ 4);
912 memcpy((uchar
*)s
, buf
, len
);
916 #ifdef CONFIG_CDP_CAPABILITIES
917 *s
++ = htons(CDP_CAPABILITIES_TLV
);
919 *(ulong
*)s
= htonl(CONFIG_CDP_CAPABILITIES
);
923 #ifdef CONFIG_CDP_VERSION
924 *s
++ = htons(CDP_VERSION_TLV
);
925 memset(buf
, 0, sizeof(buf
));
926 strcpy(buf
, CONFIG_CDP_VERSION
);
928 if (len
& 1) /* make it even */
930 *s
++ = htons(len
+ 4);
931 memcpy((uchar
*)s
, buf
, len
);
935 #ifdef CONFIG_CDP_PLATFORM
936 *s
++ = htons(CDP_PLATFORM_TLV
);
937 memset(buf
, 0, sizeof(buf
));
938 strcpy(buf
, CONFIG_CDP_PLATFORM
);
940 if (len
& 1) /* make it even */
942 *s
++ = htons(len
+ 4);
943 memcpy((uchar
*)s
, buf
, len
);
947 #ifdef CONFIG_CDP_TRIGGER
948 *s
++ = htons(CDP_TRIGGER_TLV
);
950 *(ulong
*)s
= htonl(CONFIG_CDP_TRIGGER
);
954 #ifdef CONFIG_CDP_POWER_CONSUMPTION
955 *s
++ = htons(CDP_POWER_CONSUMPTION_TLV
);
957 *s
++ = htons(CONFIG_CDP_POWER_CONSUMPTION
);
960 /* length of ethernet packet */
961 len
= (uchar
*)s
- ((uchar
*)NetTxPacket
+ ETHER_HDR_SIZE
);
962 et
->et_protlen
= htons(len
);
964 len
= ETHER_HDR_SIZE
+ sizeof(CDP_SNAP_hdr
);
965 chksum
= CDP_compute_csum((uchar
*)NetTxPacket
+ len
, (uchar
*)s
- (NetTxPacket
+ len
));
970 (void) eth_send(NetTxPacket
, (uchar
*)s
- NetTxPacket
);
980 NetSetTimeout (CDP_TIMEOUT
, CDPTimeout
);
985 /* if not OK try again */
989 NetState
= NETLOOP_SUCCESS
;
993 CDPDummyHandler (uchar
* pkt
, unsigned dest
, unsigned src
, unsigned len
)
999 CDPHandler(const uchar
* pkt
, unsigned len
)
1008 if (len
< sizeof(CDP_SNAP_hdr
) + 4)
1011 /* check for valid CDP SNAP header */
1012 if (memcmp(pkt
, CDP_SNAP_hdr
, sizeof(CDP_SNAP_hdr
)) != 0)
1015 pkt
+= sizeof(CDP_SNAP_hdr
);
1016 len
-= sizeof(CDP_SNAP_hdr
);
1018 /* Version of CDP protocol must be >= 2 and TTL != 0 */
1019 if (pkt
[0] < 0x02 || pkt
[1] == 0)
1022 /* if version is greater than 0x02 maybe we'll have a problem; output a warning */
1024 printf("** WARNING: CDP packet received with a protocol version %d > 2\n",
1027 if (CDP_compute_csum(pkt
, len
) != 0)
1039 ss
= (const ushort
*)pkt
;
1040 type
= ntohs(ss
[0]);
1041 tlen
= ntohs(ss
[1]);
1049 ss
+= 2; /* point ss to the data of the TLV */
1053 case CDP_DEVICE_ID_TLV
:
1055 case CDP_ADDRESS_TLV
:
1057 case CDP_PORT_ID_TLV
:
1059 case CDP_CAPABILITIES_TLV
:
1061 case CDP_VERSION_TLV
:
1063 case CDP_PLATFORM_TLV
:
1065 case CDP_NATIVE_VLAN_TLV
:
1068 case CDP_APPLIANCE_VLAN_TLV
:
1069 t
= (const uchar
*)ss
;
1075 ss
= (const ushort
*)(t
+ 1);
1077 #ifdef CONFIG_CDP_APPLIANCE_VLAN_TYPE
1078 if (applid
== CONFIG_CDP_APPLIANCE_VLAN_TYPE
)
1081 vlan
= ntohs(*ss
); /* XXX will this work; dunno */
1086 case CDP_TRIGGER_TLV
:
1088 case CDP_POWER_CONSUMPTION_TLV
:
1090 case CDP_SYSNAME_TLV
:
1092 case CDP_SYSOBJECT_TLV
:
1094 case CDP_MANAGEMENT_ADDRESS_TLV
:
1099 CDPApplianceVLAN
= vlan
;
1100 CDPNativeVLAN
= nvlan
;
1106 printf("** CDP packet is too short\n");
1110 static void CDPStart(void)
1112 #if defined(CONFIG_NET_MULTI)
1113 printf ("Using %s device\n", eth_get_name());
1118 CDPNativeVLAN
= htons(-1);
1119 CDPApplianceVLAN
= htons(-1);
1121 NetSetTimeout (CDP_TIMEOUT
, CDPTimeout
);
1122 NetSetHandler (CDPDummyHandler
);
1128 #ifdef CONFIG_IP_DEFRAG
1130 * This function collects fragments in a single packet, according
1131 * to the algorithm in RFC815. It returns NULL or the pointer to
1132 * a complete packet, in static storage
1134 #ifndef CONFIG_NET_MAXDEFRAG
1135 #define CONFIG_NET_MAXDEFRAG 16384
1138 * MAXDEFRAG, above, is chosen in the config file and is real data
1139 * so we need to add the NFS overhead, which is more than TFTP.
1140 * To use sizeof in the internal unnamed structures, we need a real
1141 * instance (can't do "sizeof(struct rpc_t.u.reply))", unfortunately).
1142 * The compiler doesn't complain nor allocates the actual structure
1144 static struct rpc_t rpc_specimen
;
1145 #define IP_PKTSIZE (CONFIG_NET_MAXDEFRAG + sizeof(rpc_specimen.u.reply))
1147 #define IP_MAXUDP (IP_PKTSIZE - IP_HDR_SIZE_NO_UDP)
1150 * this is the packet being assembled, either data or frag control.
1151 * Fragments go by 8 bytes, so this union must be 8 bytes long
1154 /* first_byte is address of this structure */
1155 u16 last_byte
; /* last byte in this hole + 1 (begin of next hole) */
1156 u16 next_hole
; /* index of next (in 8-b blocks), 0 == none */
1157 u16 prev_hole
; /* index of prev, 0 == none */
1161 static IP_t
*__NetDefragment(IP_t
*ip
, int *lenp
)
1163 static uchar pkt_buff
[IP_PKTSIZE
] __attribute__((aligned(PKTALIGN
)));
1164 static u16 first_hole
, total_len
;
1165 struct hole
*payload
, *thisfrag
, *h
, *newh
;
1166 IP_t
*localip
= (IP_t
*)pkt_buff
;
1167 uchar
*indata
= (uchar
*)ip
;
1168 int offset8
, start
, len
, done
= 0;
1169 u16 ip_off
= ntohs(ip
->ip_off
);
1171 /* payload starts after IP header, this fragment is in there */
1172 payload
= (struct hole
*)(pkt_buff
+ IP_HDR_SIZE_NO_UDP
);
1173 offset8
= (ip_off
& IP_OFFS
);
1174 thisfrag
= payload
+ offset8
;
1175 start
= offset8
* 8;
1176 len
= ntohs(ip
->ip_len
) - IP_HDR_SIZE_NO_UDP
;
1178 if (start
+ len
> IP_MAXUDP
) /* fragment extends too far */
1181 if (!total_len
|| localip
->ip_id
!= ip
->ip_id
) {
1182 /* new (or different) packet, reset structs */
1184 payload
[0].last_byte
= ~0;
1185 payload
[0].next_hole
= 0;
1186 payload
[0].prev_hole
= 0;
1188 /* any IP header will work, copy the first we received */
1189 memcpy(localip
, ip
, IP_HDR_SIZE_NO_UDP
);
1193 * What follows is the reassembly algorithm. We use the payload
1194 * array as a linked list of hole descriptors, as each hole starts
1195 * at a multiple of 8 bytes. However, last byte can be whatever value,
1196 * so it is represented as byte count, not as 8-byte blocks.
1199 h
= payload
+ first_hole
;
1200 while (h
->last_byte
< start
) {
1201 if (!h
->next_hole
) {
1202 /* no hole that far away */
1205 h
= payload
+ h
->next_hole
;
1208 /* last fragment may be 1..7 bytes, the "+7" forces acceptance */
1209 if (offset8
+ ((len
+ 7) / 8) <= h
- payload
) {
1210 /* no overlap with holes (dup fragment?) */
1214 if (!(ip_off
& IP_FLAGS_MFRAG
)) {
1215 /* no more fragmentss: truncate this (last) hole */
1216 total_len
= start
+ len
;
1217 h
->last_byte
= start
+ len
;
1221 * There is some overlap: fix the hole list. This code doesn't
1222 * deal with a fragment that overlaps with two different holes
1223 * (thus being a superset of a previously-received fragment).
1226 if ( (h
>= thisfrag
) && (h
->last_byte
<= start
+ len
) ) {
1227 /* complete overlap with hole: remove hole */
1228 if (!h
->prev_hole
&& !h
->next_hole
) {
1229 /* last remaining hole */
1231 } else if (!h
->prev_hole
) {
1233 first_hole
= h
->next_hole
;
1234 payload
[h
->next_hole
].prev_hole
= 0;
1235 } else if (!h
->next_hole
) {
1237 payload
[h
->prev_hole
].next_hole
= 0;
1239 /* in the middle of the list */
1240 payload
[h
->next_hole
].prev_hole
= h
->prev_hole
;
1241 payload
[h
->prev_hole
].next_hole
= h
->next_hole
;
1244 } else if (h
->last_byte
<= start
+ len
) {
1245 /* overlaps with final part of the hole: shorten this hole */
1246 h
->last_byte
= start
;
1248 } else if (h
>= thisfrag
) {
1249 /* overlaps with initial part of the hole: move this hole */
1250 newh
= thisfrag
+ (len
/ 8);
1254 payload
[h
->next_hole
].prev_hole
= (h
- payload
);
1256 payload
[h
->prev_hole
].next_hole
= (h
- payload
);
1258 first_hole
= (h
- payload
);
1261 /* fragment sits in the middle: split the hole */
1262 newh
= thisfrag
+ (len
/ 8);
1264 h
->last_byte
= start
;
1265 h
->next_hole
= (newh
- payload
);
1266 newh
->prev_hole
= (h
- payload
);
1267 if (newh
->next_hole
)
1268 payload
[newh
->next_hole
].prev_hole
= (newh
- payload
);
1271 /* finally copy this fragment and possibly return whole packet */
1272 memcpy((uchar
*)thisfrag
, indata
+ IP_HDR_SIZE_NO_UDP
, len
);
1276 localip
->ip_len
= htons(total_len
);
1277 *lenp
= total_len
+ IP_HDR_SIZE_NO_UDP
;
1281 static inline IP_t
*NetDefragment(IP_t
*ip
, int *lenp
)
1283 u16 ip_off
= ntohs(ip
->ip_off
);
1284 if (!(ip_off
& (IP_OFFS
| IP_FLAGS_MFRAG
)))
1285 return ip
; /* not a fragment */
1286 return __NetDefragment(ip
, lenp
);
1289 #else /* !CONFIG_IP_DEFRAG */
1291 static inline IP_t
*NetDefragment(IP_t
*ip
, int *lenp
)
1293 u16 ip_off
= ntohs(ip
->ip_off
);
1294 if (!(ip_off
& (IP_OFFS
| IP_FLAGS_MFRAG
)))
1295 return ip
; /* not a fragment */
1301 NetReceive(volatile uchar
* inpkt
, int len
)
1309 #if defined(CONFIG_CMD_CDP)
1312 ushort cti
= 0, vlanid
= VLAN_NONE
, myvlanid
, mynvlanid
;
1314 debug("packet received\n");
1316 NetRxPacket
= inpkt
;
1317 NetRxPacketLen
= len
;
1318 et
= (Ethernet_t
*)inpkt
;
1320 /* too small packet? */
1321 if (len
< ETHER_HDR_SIZE
)
1326 (*push_packet
)(inpkt
, len
);
1331 #if defined(CONFIG_CMD_CDP)
1332 /* keep track if packet is CDP */
1333 iscdp
= memcmp(et
->et_dest
, NetCDPAddr
, 6) == 0;
1336 myvlanid
= ntohs(NetOurVLAN
);
1337 if (myvlanid
== (ushort
)-1)
1338 myvlanid
= VLAN_NONE
;
1339 mynvlanid
= ntohs(NetOurNativeVLAN
);
1340 if (mynvlanid
== (ushort
)-1)
1341 mynvlanid
= VLAN_NONE
;
1343 x
= ntohs(et
->et_protlen
);
1345 debug("packet received\n");
1349 * Got a 802 packet. Check the other protocol field.
1351 x
= ntohs(et
->et_prot
);
1353 ip
= (IP_t
*)(inpkt
+ E802_HDR_SIZE
);
1354 len
-= E802_HDR_SIZE
;
1356 } else if (x
!= PROT_VLAN
) { /* normal packet */
1357 ip
= (IP_t
*)(inpkt
+ ETHER_HDR_SIZE
);
1358 len
-= ETHER_HDR_SIZE
;
1360 } else { /* VLAN packet */
1361 VLAN_Ethernet_t
*vet
= (VLAN_Ethernet_t
*)et
;
1363 debug("VLAN packet received\n");
1365 /* too small packet? */
1366 if (len
< VLAN_ETHER_HDR_SIZE
)
1369 /* if no VLAN active */
1370 if ((ntohs(NetOurVLAN
) & VLAN_IDMASK
) == VLAN_NONE
1371 #if defined(CONFIG_CMD_CDP)
1377 cti
= ntohs(vet
->vet_tag
);
1378 vlanid
= cti
& VLAN_IDMASK
;
1379 x
= ntohs(vet
->vet_type
);
1381 ip
= (IP_t
*)(inpkt
+ VLAN_ETHER_HDR_SIZE
);
1382 len
-= VLAN_ETHER_HDR_SIZE
;
1385 debug("Receive from protocol 0x%x\n", x
);
1387 #if defined(CONFIG_CMD_CDP)
1389 CDPHandler((uchar
*)ip
, len
);
1394 if ((myvlanid
& VLAN_IDMASK
) != VLAN_NONE
) {
1395 if (vlanid
== VLAN_NONE
)
1396 vlanid
= (mynvlanid
& VLAN_IDMASK
);
1398 if (vlanid
!= (myvlanid
& VLAN_IDMASK
))
1406 * We have to deal with two types of ARP packets:
1407 * - REQUEST packets will be answered by sending our
1408 * IP address - if we know it.
1409 * - REPLY packates are expected only after we asked
1410 * for the TFTP server's or the gateway's ethernet
1411 * address; so if we receive such a packet, we set
1412 * the server ethernet address
1417 if (len
< ARP_HDR_SIZE
) {
1418 printf("bad length %d < %d\n", len
, ARP_HDR_SIZE
);
1421 if (ntohs(arp
->ar_hrd
) != ARP_ETHER
) {
1424 if (ntohs(arp
->ar_pro
) != PROT_IP
) {
1427 if (arp
->ar_hln
!= 6) {
1430 if (arp
->ar_pln
!= 4) {
1434 if (NetOurIP
== 0) {
1438 if (NetReadIP(&arp
->ar_data
[16]) != NetOurIP
) {
1442 switch (ntohs(arp
->ar_op
)) {
1443 case ARPOP_REQUEST
: /* reply with our IP address */
1444 debug("Got ARP REQUEST, return our IP\n");
1446 pkt
+= NetSetEther(pkt
, et
->et_src
, PROT_ARP
);
1447 arp
->ar_op
= htons(ARPOP_REPLY
);
1448 memcpy (&arp
->ar_data
[10], &arp
->ar_data
[0], 6);
1449 NetCopyIP(&arp
->ar_data
[16], &arp
->ar_data
[6]);
1450 memcpy (&arp
->ar_data
[ 0], NetOurEther
, 6);
1451 NetCopyIP(&arp
->ar_data
[ 6], &NetOurIP
);
1452 (void) eth_send((uchar
*)et
, (pkt
- (uchar
*)et
) + ARP_HDR_SIZE
);
1455 case ARPOP_REPLY
: /* arp reply */
1456 /* are we waiting for a reply */
1457 if (!NetArpWaitPacketIP
|| !NetArpWaitPacketMAC
)
1460 #ifdef CONFIG_KEEP_SERVERADDR
1461 if (NetServerIP
== NetArpWaitPacketIP
) {
1463 sprintf(buf
, "%pM", arp
->ar_data
);
1464 setenv("serveraddr", buf
);
1468 debug("Got ARP REPLY, set server/gtwy eth addr (%pM)\n",
1471 tmp
= NetReadIP(&arp
->ar_data
[6]);
1473 /* matched waiting packet's address */
1474 if (tmp
== NetArpWaitReplyIP
) {
1476 /* save address for later use */
1477 memcpy(NetArpWaitPacketMAC
, &arp
->ar_data
[0], 6);
1479 #ifdef CONFIG_NETCONSOLE
1480 (*packetHandler
)(0,0,0,0);
1482 /* modify header, and transmit it */
1483 memcpy(((Ethernet_t
*)NetArpWaitTxPacket
)->et_dest
, NetArpWaitPacketMAC
, 6);
1484 (void) eth_send(NetArpWaitTxPacket
, NetArpWaitTxPacketSize
);
1486 /* no arp request pending now */
1487 NetArpWaitPacketIP
= 0;
1488 NetArpWaitTxPacketSize
= 0;
1489 NetArpWaitPacketMAC
= NULL
;
1494 debug("Unexpected ARP opcode 0x%x\n", ntohs(arp
->ar_op
));
1499 #ifdef CONFIG_CMD_RARP
1501 debug("Got RARP\n");
1503 if (len
< ARP_HDR_SIZE
) {
1504 printf("bad length %d < %d\n", len
, ARP_HDR_SIZE
);
1508 if ((ntohs(arp
->ar_op
) != RARPOP_REPLY
) ||
1509 (ntohs(arp
->ar_hrd
) != ARP_ETHER
) ||
1510 (ntohs(arp
->ar_pro
) != PROT_IP
) ||
1511 (arp
->ar_hln
!= 6) || (arp
->ar_pln
!= 4)) {
1513 puts ("invalid RARP header\n");
1515 NetCopyIP(&NetOurIP
, &arp
->ar_data
[16]);
1516 if (NetServerIP
== 0)
1517 NetCopyIP(&NetServerIP
, &arp
->ar_data
[ 6]);
1518 memcpy (NetServerEther
, &arp
->ar_data
[ 0], 6);
1520 (*packetHandler
)(0,0,0,0);
1526 /* Before we start poking the header, make sure it is there */
1527 if (len
< IP_HDR_SIZE
) {
1528 debug("len bad %d < %lu\n", len
, (ulong
)IP_HDR_SIZE
);
1531 /* Check the packet length */
1532 if (len
< ntohs(ip
->ip_len
)) {
1533 printf("len bad %d < %d\n", len
, ntohs(ip
->ip_len
));
1536 len
= ntohs(ip
->ip_len
);
1537 debug("len=%d, v=%02x\n", len
, ip
->ip_hl_v
& 0xff);
1539 /* Can't deal with anything except IPv4 */
1540 if ((ip
->ip_hl_v
& 0xf0) != 0x40) {
1543 /* Can't deal with IP options (headers != 20 bytes) */
1544 if ((ip
->ip_hl_v
& 0x0f) > 0x05) {
1547 /* Check the Checksum of the header */
1548 if (!NetCksumOk((uchar
*)ip
, IP_HDR_SIZE_NO_UDP
/ 2)) {
1549 puts ("checksum bad\n");
1552 /* If it is not for us, ignore it */
1553 tmp
= NetReadIP(&ip
->ip_dst
);
1554 if (NetOurIP
&& tmp
!= NetOurIP
&& tmp
!= 0xFFFFFFFF) {
1555 #ifdef CONFIG_MCAST_TFTP
1556 if (Mcast_addr
!= tmp
)
1561 * The function returns the unchanged packet if it's not
1562 * a fragment, and either the complete packet or NULL if
1563 * it is a fragment (if !CONFIG_IP_DEFRAG, it returns NULL)
1565 if (!(ip
= NetDefragment(ip
, &len
)))
1568 * watch for ICMP host redirects
1570 * There is no real handler code (yet). We just watch
1571 * for ICMP host redirect messages. In case anybody
1572 * sees these messages: please contact me
1573 * (wd@denx.de), or - even better - send me the
1574 * necessary fixes :-)
1576 * Note: in all cases where I have seen this so far
1577 * it was a problem with the router configuration,
1578 * for instance when a router was configured in the
1579 * BOOTP reply, but the TFTP server was on the same
1580 * subnet. So this is probably a warning that your
1581 * configuration might be wrong. But I'm not really
1582 * sure if there aren't any other situations.
1584 if (ip
->ip_p
== IPPROTO_ICMP
) {
1585 ICMP_t
*icmph
= (ICMP_t
*)&(ip
->udp_src
);
1587 switch (icmph
->type
) {
1589 if (icmph
->code
!= ICMP_REDIR_HOST
)
1591 printf (" ICMP Host Redirect to %pI4 ", &icmph
->un
.gateway
);
1593 #if defined(CONFIG_CMD_PING)
1594 case ICMP_ECHO_REPLY
:
1596 * IP header OK. Pass the packet to the current handler.
1598 /* XXX point to ip packet */
1599 (*packetHandler
)((uchar
*)ip
, 0, 0, 0);
1601 case ICMP_ECHO_REQUEST
:
1602 debug("Got ICMP ECHO REQUEST, return %d bytes \n",
1603 ETHER_HDR_SIZE
+ len
);
1605 memcpy (&et
->et_dest
[0], &et
->et_src
[0], 6);
1606 memcpy (&et
->et_src
[ 0], NetOurEther
, 6);
1610 NetCopyIP((void*)&ip
->ip_dst
, &ip
->ip_src
);
1611 NetCopyIP((void*)&ip
->ip_src
, &NetOurIP
);
1612 ip
->ip_sum
= ~NetCksum((uchar
*)ip
, IP_HDR_SIZE_NO_UDP
>> 1);
1614 icmph
->type
= ICMP_ECHO_REPLY
;
1615 icmph
->checksum
= 0;
1616 icmph
->checksum
= ~NetCksum((uchar
*)icmph
,
1617 (len
- IP_HDR_SIZE_NO_UDP
) >> 1);
1618 (void) eth_send((uchar
*)et
, ETHER_HDR_SIZE
+ len
);
1624 } else if (ip
->ip_p
!= IPPROTO_UDP
) { /* Only UDP packets */
1628 #ifdef CONFIG_UDP_CHECKSUM
1629 if (ip
->udp_xsum
!= 0) {
1635 xsum
+= (ntohs(ip
->udp_len
));
1636 xsum
+= (ntohl(ip
->ip_src
) >> 16) & 0x0000ffff;
1637 xsum
+= (ntohl(ip
->ip_src
) >> 0) & 0x0000ffff;
1638 xsum
+= (ntohl(ip
->ip_dst
) >> 16) & 0x0000ffff;
1639 xsum
+= (ntohl(ip
->ip_dst
) >> 0) & 0x0000ffff;
1641 sumlen
= ntohs(ip
->udp_len
);
1642 sumptr
= (ushort
*) &(ip
->udp_src
);
1644 while (sumlen
> 1) {
1647 sumdata
= *sumptr
++;
1648 xsum
+= ntohs(sumdata
);
1654 sumdata
= *(unsigned char *) sumptr
;
1655 sumdata
= (sumdata
<< 8) & 0xff00;
1658 while ((xsum
>> 16) != 0) {
1659 xsum
= (xsum
& 0x0000ffff) + ((xsum
>> 16) & 0x0000ffff);
1661 if ((xsum
!= 0x00000000) && (xsum
!= 0x0000ffff)) {
1662 printf(" UDP wrong checksum %08lx %08x\n",
1663 xsum
, ntohs(ip
->udp_xsum
));
1670 #ifdef CONFIG_NETCONSOLE
1671 nc_input_packet((uchar
*)ip
+IP_HDR_SIZE
,
1674 ntohs(ip
->udp_len
) - 8);
1677 * IP header OK. Pass the packet to the current handler.
1679 (*packetHandler
)((uchar
*)ip
+IP_HDR_SIZE
,
1682 ntohs(ip
->udp_len
) - 8);
1688 /**********************************************************************/
1690 static int net_check_prereq (proto_t protocol
)
1694 #if defined(CONFIG_CMD_PING)
1696 if (NetPingIP
== 0) {
1697 puts ("*** ERROR: ping address not given\n");
1702 #if defined(CONFIG_CMD_SNTP)
1704 if (NetNtpServerIP
== 0) {
1705 puts ("*** ERROR: NTP server address not given\n");
1710 #if defined(CONFIG_CMD_DNS)
1712 if (NetOurDNSIP
== 0) {
1713 puts("*** ERROR: DNS server address not given\n");
1718 #if defined(CONFIG_CMD_NFS)
1723 if (NetServerIP
== 0) {
1724 puts ("*** ERROR: `serverip' not set\n");
1727 #if defined(CONFIG_CMD_PING) || defined(CONFIG_CMD_SNTP) || \
1728 defined(CONFIG_CMD_DNS)
1732 if (NetOurIP
== 0) {
1733 puts ("*** ERROR: `ipaddr' not set\n");
1738 #ifdef CONFIG_CMD_RARP
1744 if (memcmp (NetOurEther
, "\0\0\0\0\0\0", 6) == 0) {
1745 #ifdef CONFIG_NET_MULTI
1746 extern int eth_get_dev_index (void);
1747 int num
= eth_get_dev_index ();
1751 puts ("*** ERROR: No ethernet found.\n");
1754 puts ("*** ERROR: `ethaddr' not set\n");
1757 printf ("*** ERROR: `eth%daddr' not set\n",
1765 puts ("*** ERROR: `ethaddr' not set\n");
1773 return (0); /* OK */
1775 /**********************************************************************/
1778 NetCksumOk(uchar
* ptr
, int len
)
1780 return !((NetCksum(ptr
, len
) + 1) & 0xfffe);
1785 NetCksum(uchar
* ptr
, int len
)
1788 ushort
*p
= (ushort
*)ptr
;
1793 xsum
= (xsum
& 0xffff) + (xsum
>> 16);
1794 xsum
= (xsum
& 0xffff) + (xsum
>> 16);
1795 return (xsum
& 0xffff);
1803 myvlanid
= ntohs(NetOurVLAN
);
1804 if (myvlanid
== (ushort
)-1)
1805 myvlanid
= VLAN_NONE
;
1807 return ((myvlanid
& VLAN_IDMASK
) == VLAN_NONE
) ? ETHER_HDR_SIZE
: VLAN_ETHER_HDR_SIZE
;
1811 NetSetEther(volatile uchar
* xet
, uchar
* addr
, uint prot
)
1813 Ethernet_t
*et
= (Ethernet_t
*)xet
;
1816 myvlanid
= ntohs(NetOurVLAN
);
1817 if (myvlanid
== (ushort
)-1)
1818 myvlanid
= VLAN_NONE
;
1820 memcpy (et
->et_dest
, addr
, 6);
1821 memcpy (et
->et_src
, NetOurEther
, 6);
1822 if ((myvlanid
& VLAN_IDMASK
) == VLAN_NONE
) {
1823 et
->et_protlen
= htons(prot
);
1824 return ETHER_HDR_SIZE
;
1826 VLAN_Ethernet_t
*vet
= (VLAN_Ethernet_t
*)xet
;
1828 vet
->vet_vlan_type
= htons(PROT_VLAN
);
1829 vet
->vet_tag
= htons((0 << 5) | (myvlanid
& VLAN_IDMASK
));
1830 vet
->vet_type
= htons(prot
);
1831 return VLAN_ETHER_HDR_SIZE
;
1836 NetSetIP(volatile uchar
* xip
, IPaddr_t dest
, int dport
, int sport
, int len
)
1838 IP_t
*ip
= (IP_t
*)xip
;
1841 * If the data is an odd number of bytes, zero the
1842 * byte after the last byte so that the checksum
1846 xip
[IP_HDR_SIZE
+ len
] = 0;
1849 * Construct an IP and UDP header.
1850 * (need to set no fragment bit - XXX)
1852 ip
->ip_hl_v
= 0x45; /* IP_HDR_SIZE / 4 (not including UDP) */
1854 ip
->ip_len
= htons(IP_HDR_SIZE
+ len
);
1855 ip
->ip_id
= htons(NetIPID
++);
1856 ip
->ip_off
= htons(IP_FLAGS_DFRAG
); /* Don't fragment */
1858 ip
->ip_p
= 17; /* UDP */
1860 NetCopyIP((void*)&ip
->ip_src
, &NetOurIP
); /* already in network byte order */
1861 NetCopyIP((void*)&ip
->ip_dst
, &dest
); /* - "" - */
1862 ip
->udp_src
= htons(sport
);
1863 ip
->udp_dst
= htons(dport
);
1864 ip
->udp_len
= htons(8 + len
);
1866 ip
->ip_sum
= ~NetCksum((uchar
*)ip
, IP_HDR_SIZE_NO_UDP
/ 2);
1869 void copy_filename (char *dst
, const char *src
, int size
)
1871 if (*src
&& (*src
== '"')) {
1876 while ((--size
> 0) && *src
&& (*src
!= '"')) {
1882 #if defined(CONFIG_CMD_NFS) || defined(CONFIG_CMD_SNTP) || defined(CONFIG_CMD_DNS)
1884 * make port a little random (1024-17407)
1885 * This keeps the math somewhat trivial to compute, and seems to work with
1886 * all supported protocols/clients/servers
1888 unsigned int random_port(void)
1890 return 1024 + (get_timer(0) % 0x4000);
1894 void ip_to_string (IPaddr_t x
, char *s
)
1897 sprintf (s
, "%d.%d.%d.%d",
1898 (int) ((x
>> 24) & 0xff),
1899 (int) ((x
>> 16) & 0xff),
1900 (int) ((x
>> 8) & 0xff), (int) ((x
>> 0) & 0xff)
1904 void VLAN_to_string(ushort x
, char *s
)
1908 if (x
== (ushort
)-1)
1914 sprintf(s
, "%d", x
& VLAN_IDMASK
);
1917 ushort
string_to_VLAN(const char *s
)
1922 return htons(VLAN_NONE
);
1924 if (*s
< '0' || *s
> '9')
1927 id
= (ushort
)simple_strtoul(s
, NULL
, 10);
1932 ushort
getenv_VLAN(char *var
)
1934 return (string_to_VLAN(getenv(var
)));