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
68 #ifdef CONFIG_STATUS_LED
69 #include <status_led.h>
73 #if (CONFIG_COMMANDS & CFG_CMD_NET)
75 #define ARP_TIMEOUT 5 /* Seconds before trying ARP again */
76 #ifndef CONFIG_NET_RETRY_COUNT
77 # define ARP_TIMEOUT_COUNT 5 /* # of timeouts before giving up */
79 # define ARP_TIMEOUT_COUNT (CONFIG_NET_RETRY_COUNT)
86 /** BOOTP EXTENTIONS **/
88 IPaddr_t NetOurSubnetMask
=0; /* Our subnet mask (0=unknown) */
89 IPaddr_t NetOurGatewayIP
=0; /* Our gateways IP address */
90 IPaddr_t NetOurDNSIP
=0; /* Our DNS IP address */
91 #if (CONFIG_BOOTP_MASK & CONFIG_BOOTP_DNS2)
92 IPaddr_t NetOurDNS2IP
=0; /* Our 2nd DNS IP address */
94 char NetOurNISDomain
[32]={0,}; /* Our NIS domain */
95 char NetOurHostName
[32]={0,}; /* Our hostname */
96 char NetOurRootPath
[64]={0,}; /* Our bootpath */
97 ushort NetBootFileSize
=0; /* Our bootfile size in blocks */
99 /** END OF BOOTP EXTENTIONS **/
101 ulong NetBootFileXferSize
; /* The actual transferred size of the bootfile (in bytes) */
102 uchar NetOurEther
[6]; /* Our ethernet address */
103 uchar NetServerEther
[6] = /* Boot server enet address */
104 { 0, 0, 0, 0, 0, 0 };
105 IPaddr_t NetOurIP
; /* Our IP addr (0 = unknown) */
106 IPaddr_t NetServerIP
; /* Our IP addr (0 = unknown) */
107 volatile uchar
*NetRxPkt
; /* Current receive packet */
108 int NetRxPktLen
; /* Current rx packet length */
109 unsigned NetIPID
; /* IP packet ID */
110 uchar NetBcastAddr
[6] = /* Ethernet bcast address */
111 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
112 uchar NetEtherNullAddr
[6] =
113 { 0, 0, 0, 0, 0, 0 };
114 int NetState
; /* Network loop state */
115 #ifdef CONFIG_NET_MULTI
116 int NetRestartWrap
= 0; /* Tried all network devices */
117 static int NetRestarted
= 0; /* Network loop restarted */
118 static int NetDevExists
= 0; /* At least one device configured */
121 char BootFile
[128]; /* Boot File name */
123 #if (CONFIG_COMMANDS & CFG_CMD_PING)
124 IPaddr_t NetPingIP
; /* the ip address to ping */
126 static void PingStart(void);
129 volatile uchar PktBuf
[(PKTBUFSRX
+1) * PKTSIZE_ALIGN
+ PKTALIGN
];
131 volatile uchar
*NetRxPackets
[PKTBUFSRX
]; /* Receive packets */
133 static rxhand_f
*packetHandler
; /* Current RX packet handler */
134 static thand_f
*timeHandler
; /* Current timeout handler */
135 static ulong timeStart
; /* Time base value */
136 static ulong timeDelta
; /* Current timeout value */
137 volatile uchar
*NetTxPacket
= 0; /* THE transmit packet */
139 static int net_check_prereq (proto_t protocol
);
141 /**********************************************************************/
143 IPaddr_t NetArpWaitPacketIP
;
144 IPaddr_t NetArpWaitReplyIP
;
145 uchar
*NetArpWaitPacketMAC
; /* MAC address of waiting packet's destination */
146 uchar
*NetArpWaitTxPacket
; /* THE transmit packet */
147 int NetArpWaitTxPacketSize
;
148 uchar NetArpWaitPacketBuf
[PKTSIZE_ALIGN
+ PKTALIGN
];
149 ulong NetArpWaitTimerStart
;
152 void ArpRequest(void)
159 printf("ARP broadcast %d\n", NetArpWaitTry
);
163 NetSetEther(pkt
, NetBcastAddr
, PROT_ARP
);
164 pkt
+= ETHER_HDR_SIZE
;
168 arp
->ar_hrd
= htons(ARP_ETHER
);
169 arp
->ar_pro
= htons(PROT_IP
);
172 arp
->ar_op
= htons(ARPOP_REQUEST
);
174 memcpy (&arp
->ar_data
[0], NetOurEther
, 6); /* source ET addr */
175 NetWriteIP((uchar
*)&arp
->ar_data
[6], NetOurIP
); /* source IP addr */
176 for (i
=10; i
<16; ++i
) {
177 arp
->ar_data
[i
] = 0; /* dest ET addr = 0 */
180 if((NetArpWaitPacketIP
& NetOurSubnetMask
) != (NetOurIP
& NetOurSubnetMask
)) {
181 if (NetOurGatewayIP
== 0) {
182 puts ("## Warning: gatewayip needed but not set\n");
184 NetArpWaitReplyIP
= NetOurGatewayIP
;
186 NetArpWaitReplyIP
= NetArpWaitPacketIP
;
188 NetWriteIP((uchar
*)&arp
->ar_data
[16], NetArpWaitReplyIP
);
189 (void) eth_send(NetTxPacket
, ETHER_HDR_SIZE
+ ARP_HDR_SIZE
);
192 void ArpTimeoutCheck(void)
196 if (!NetArpWaitPacketIP
)
201 /* check for arp timeout */
202 if ((t
- NetArpWaitTimerStart
) > ARP_TIMEOUT
* CFG_HZ
) {
205 if (NetArpWaitTry
>= ARP_TIMEOUT_COUNT
) {
206 puts ("\nARP Retry count exceeded; starting again\n");
210 NetArpWaitTimerStart
= t
;
216 /**********************************************************************/
218 * Main network processing loop.
222 NetLoop(proto_t protocol
)
224 DECLARE_GLOBAL_DATA_PTR
;
228 #ifdef CONFIG_NET_MULTI
233 /* XXX problem with bss workaround */
234 NetArpWaitPacketMAC
= NULL
;
235 NetArpWaitTxPacket
= NULL
;
236 NetArpWaitPacketIP
= 0;
237 NetArpWaitReplyIP
= 0;
238 NetArpWaitTxPacket
= NULL
;
245 * Setup packet buffers, aligned correctly.
247 NetTxPacket
= &PktBuf
[0] + (PKTALIGN
- 1);
248 NetTxPacket
-= (ulong
)NetTxPacket
% PKTALIGN
;
249 for (i
= 0; i
< PKTBUFSRX
; i
++) {
250 NetRxPackets
[i
] = NetTxPacket
+ (i
+1)*PKTSIZE_ALIGN
;
255 if (!NetArpWaitTxPacket
) {
256 NetArpWaitTxPacket
= &NetArpWaitPacketBuf
[0] + (PKTALIGN
- 1);
257 NetArpWaitTxPacket
-= (ulong
)NetArpWaitTxPacket
% PKTALIGN
;
258 NetArpWaitTxPacketSize
= 0;
266 #ifdef CONFIG_NET_MULTI
267 memcpy (NetOurEther
, eth_get_dev()->enetaddr
, 6);
269 memcpy (NetOurEther
, bd
->bi_enetaddr
, 6);
272 NetState
= NETLOOP_CONTINUE
;
275 * Start the ball rolling with the given start function. From
276 * here on, this code is a state machine driven by received
277 * packets and timer events.
281 #if (CONFIG_COMMANDS & CFG_CMD_PING)
285 NetCopyIP(&NetOurIP
, &bd
->bi_ip_addr
);
286 NetOurGatewayIP
= getenv_IPaddr ("gatewayip");
287 NetOurSubnetMask
= getenv_IPaddr ("netmask");
291 NetServerIP
= getenv_IPaddr ("serverip");
293 #if (CONFIG_COMMANDS & CFG_CMD_PING)
306 * initialize our IP addr to 0 in order to accept ANY
307 * IP addr assigned to us by the BOOTP / RARP server
316 switch (net_check_prereq (protocol
)) {
318 /* network not configured */
321 #ifdef CONFIG_NET_MULTI
323 /* network device not configured */
325 #endif /* CONFIG_NET_MULTI */
328 #ifdef CONFIG_NET_MULTI
333 /* always use ARP to get server ethernet address */
337 #if (CONFIG_COMMANDS & CFG_CMD_DHCP)
339 /* Start with a clean slate... */
342 DhcpRequest(); /* Basically same as BOOTP */
344 #endif /* CFG_CMD_DHCP */
355 #if (CONFIG_COMMANDS & CFG_CMD_PING)
364 NetBootFileXferSize
= 0;
368 #if defined(CONFIG_MII) || (CONFIG_COMMANDS & CFG_CMD_MII)
369 #if defined(CFG_FAULT_ECHO_LINK_DOWN) && defined(CONFIG_STATUS_LED) && defined(STATUS_LED_RED)
371 * Echo the inverted link state to the fault LED.
373 if(miiphy_link(CFG_FAULT_MII_ADDR
)) {
374 status_led_set (STATUS_LED_RED
, STATUS_LED_OFF
);
376 status_led_set (STATUS_LED_RED
, STATUS_LED_ON
);
378 #endif /* CFG_FAULT_ECHO_LINK_DOWN, ... */
379 #endif /* CONFIG_MII, ... */
382 * Main packet reception loop. Loop receiving packets until
383 * someone sets `NetQuit'.
387 #ifdef CONFIG_SHOW_ACTIVITY
389 extern void show_activity(int arg
);
394 * Check the ethernet for a new packet. The ethernet
395 * receive routine will process it.
400 * Abort if ctrl-c was pressed.
411 * Check for a timeout, and run the timeout handler
414 if (timeHandler
&& ((get_timer(0) - timeStart
) > timeDelta
)) {
417 #if defined(CONFIG_MII) || (CONFIG_COMMANDS & CFG_CMD_MII)
418 #if defined(CFG_FAULT_ECHO_LINK_DOWN) && defined(CONFIG_STATUS_LED) && defined(STATUS_LED_RED)
420 * Echo the inverted link state to the fault LED.
422 if(miiphy_link(CFG_FAULT_MII_ADDR
)) {
423 status_led_set (STATUS_LED_RED
, STATUS_LED_OFF
);
425 status_led_set (STATUS_LED_RED
, STATUS_LED_ON
);
427 #endif /* CFG_FAULT_ECHO_LINK_DOWN, ... */
428 #endif /* CONFIG_MII, ... */
430 timeHandler
= (thand_f
*)0;
437 case NETLOOP_RESTART
:
438 #ifdef CONFIG_NET_MULTI
443 case NETLOOP_SUCCESS
:
444 if (NetBootFileXferSize
> 0) {
446 printf("Bytes transferred = %ld (%lx hex)\n",
448 NetBootFileXferSize
);
449 sprintf(buf
, "%lx", NetBootFileXferSize
);
450 setenv("filesize", buf
);
453 return NetBootFileXferSize
;
461 /**********************************************************************/
464 startAgainTimeout(void)
466 NetState
= NETLOOP_RESTART
;
470 startAgainHandler(uchar
* pkt
, unsigned dest
, unsigned src
, unsigned len
)
472 /* Totally ignore the packet */
478 #ifndef CONFIG_NET_MULTI
479 NetSetTimeout(10 * CFG_HZ
, startAgainTimeout
);
480 NetSetHandler(startAgainHandler
);
482 DECLARE_GLOBAL_DATA_PTR
;
485 eth_try_another(!NetRestarted
);
492 NetSetTimeout(10 * CFG_HZ
, startAgainTimeout
);
493 NetSetHandler(startAgainHandler
);
497 NetState
= NETLOOP_FAIL
;
502 NetState
= NETLOOP_RESTART
;
507 /**********************************************************************/
513 NetSetHandler(rxhand_f
* f
)
520 NetSetTimeout(int iv
, thand_f
* f
)
523 timeHandler
= (thand_f
*)0;
526 timeStart
= get_timer(0);
533 NetSendPacket(volatile uchar
* pkt
, int len
)
535 (void) eth_send(pkt
, len
);
539 NetSendUDPPacket(uchar
*ether
, IPaddr_t dest
, int dport
, int sport
, int len
)
541 /* convert to new style broadcast */
545 /* if broadcast, make the ether address a broadcast and don't do ARP */
546 if (dest
== 0xFFFFFFFF)
547 ether
= NetBcastAddr
;
549 /* if MAC address was not discovered yet, save the packet and do an ARP request */
550 if (memcmp(ether
, NetEtherNullAddr
, 6) == 0) {
553 printf("sending ARP for %08lx\n", dest
);
556 NetArpWaitPacketIP
= dest
;
557 NetArpWaitPacketMAC
= ether
;
558 NetSetEther (NetArpWaitTxPacket
, NetArpWaitPacketMAC
, PROT_IP
);
559 NetSetIP (NetArpWaitTxPacket
+ ETHER_HDR_SIZE
, dest
, dport
, sport
, len
);
560 memcpy(NetArpWaitTxPacket
+ ETHER_HDR_SIZE
+ IP_HDR_SIZE
,
561 (uchar
*)NetTxPacket
+ ETHER_HDR_SIZE
+ IP_HDR_SIZE
, len
);
563 /* size of the waiting packet */
564 NetArpWaitTxPacketSize
= ETHER_HDR_SIZE
+ IP_HDR_SIZE
+ len
;
566 /* and do the ARP request */
568 NetArpWaitTimerStart
= get_timer(0);
570 return 1; /* waiting */
574 printf("sending UDP to %08lx/%02x:%02x:%02x:%02x:%02x:%02x\n",
575 dest
, ether
[0], ether
[1], ether
[2], ether
[3], ether
[4], ether
[5]);
578 NetSetEther (NetTxPacket
, ether
, PROT_IP
);
579 NetSetIP (NetTxPacket
+ ETHER_HDR_SIZE
, dest
, dport
, sport
, len
);
580 (void) eth_send(NetTxPacket
, ETHER_HDR_SIZE
+ IP_HDR_SIZE
+ len
);
582 return 0; /* transmited */
585 #if (CONFIG_COMMANDS & CFG_CMD_PING)
586 static ushort PingSeqNo
;
594 /* XXX always send arp request */
596 memcpy(mac
, NetEtherNullAddr
, 6);
599 printf("sending ARP for %08lx\n", NetPingIP
);
602 NetArpWaitPacketIP
= NetPingIP
;
603 NetArpWaitPacketMAC
= mac
;
605 NetSetEther(NetArpWaitTxPacket
, mac
, PROT_IP
);
607 ip
= (volatile IP_t
*)(NetArpWaitTxPacket
+ ETHER_HDR_SIZE
);
610 * Construct an IP and ICMP header. (need to set no fragment bit - XXX)
612 ip
->ip_hl_v
= 0x45; /* IP_HDR_SIZE / 4 (not including UDP) */
614 ip
->ip_len
= htons(IP_HDR_SIZE_NO_UDP
+ 8);
615 ip
->ip_id
= htons(NetIPID
++);
616 ip
->ip_off
= htons(0x4000); /* No fragmentation */
618 ip
->ip_p
= 0x01; /* ICMP */
620 NetCopyIP((void*)&ip
->ip_src
, &NetOurIP
); /* already in network byte order */
621 NetCopyIP((void*)&ip
->ip_dst
, &NetPingIP
); /* - "" - */
622 ip
->ip_sum
= ~NetCksum((uchar
*)ip
, IP_HDR_SIZE_NO_UDP
/ 2);
624 s
= &ip
->udp_src
; /* XXX ICMP starts here */
625 s
[0] = htons(0x0800); /* echo-request, code */
626 s
[1] = 0; /* checksum */
627 s
[2] = 0; /* identifier */
628 s
[3] = htons(PingSeqNo
++); /* sequence number */
629 s
[1] = ~NetCksum((uchar
*)s
, 8/2);
631 /* size of the waiting packet */
632 NetArpWaitTxPacketSize
= ETHER_HDR_SIZE
+ IP_HDR_SIZE_NO_UDP
+ 8;
634 /* and do the ARP request */
636 NetArpWaitTimerStart
= get_timer(0);
638 return 1; /* waiting */
645 NetState
= NETLOOP_FAIL
; /* we did not get the reply */
649 PingHandler (uchar
* pkt
, unsigned dest
, unsigned src
, unsigned len
)
652 volatile IP_t
*ip
= (volatile IP_t
*)pkt
;
654 tmp
= NetReadIP((void *)&ip
->ip_src
);
655 if (tmp
!= NetPingIP
)
658 NetState
= NETLOOP_SUCCESS
;
661 static void PingStart(void)
663 NetSetTimeout (10 * CFG_HZ
, PingTimeout
);
664 NetSetHandler (PingHandler
);
672 NetReceive(volatile uchar
* pkt
, int len
)
682 et
= (Ethernet_t
*)pkt
;
684 x
= ntohs(et
->et_protlen
);
688 * Got a 802 packet. Check the other protocol field.
690 x
= ntohs(et
->et_prot
);
691 ip
= (IP_t
*)(pkt
+ E802_HDR_SIZE
);
692 len
-= E802_HDR_SIZE
;
694 ip
= (IP_t
*)(pkt
+ ETHER_HDR_SIZE
);
695 len
-= ETHER_HDR_SIZE
;
699 printf("Receive from protocol 0x%x\n", x
);
706 * We have to deal with two types of ARP packets:
707 * - REQUEST packets will be answered by sending our
708 * IP address - if we know it.
709 * - REPLY packates are expected only after we asked
710 * for the TFTP server's or the gateway's ethernet
711 * address; so if we receive such a packet, we set
712 * the server ethernet address
718 if (len
< ARP_HDR_SIZE
) {
719 printf("bad length %d < %d\n", len
, ARP_HDR_SIZE
);
722 if (ntohs(arp
->ar_hrd
) != ARP_ETHER
) {
725 if (ntohs(arp
->ar_pro
) != PROT_IP
) {
728 if (arp
->ar_hln
!= 6) {
731 if (arp
->ar_pln
!= 4) {
739 if (NetReadIP(&arp
->ar_data
[16]) != NetOurIP
) {
743 switch (ntohs(arp
->ar_op
)) {
744 case ARPOP_REQUEST
: /* reply with our IP address */
746 printf("Got ARP REQUEST, return our IP\n");
748 NetSetEther((uchar
*)et
, et
->et_src
, PROT_ARP
);
749 arp
->ar_op
= htons(ARPOP_REPLY
);
750 memcpy (&arp
->ar_data
[10], &arp
->ar_data
[0], 6);
751 NetCopyIP(&arp
->ar_data
[16], &arp
->ar_data
[6]);
752 memcpy (&arp
->ar_data
[ 0], NetOurEther
, 6);
753 NetCopyIP(&arp
->ar_data
[ 6], &NetOurIP
);
754 (void) eth_send((uchar
*)et
, ((uchar
*)arp
-pkt
) + ARP_HDR_SIZE
);
757 case ARPOP_REPLY
: /* arp reply */
758 /* are we waiting for a reply */
759 if (!NetArpWaitPacketIP
|| !NetArpWaitPacketMAC
)
762 printf("Got ARP REPLY, set server/gtwy eth addr (%02x:%02x:%02x:%02x:%02x:%02x)\n",
763 arp
->ar_data
[0], arp
->ar_data
[1],
764 arp
->ar_data
[2], arp
->ar_data
[3],
765 arp
->ar_data
[4], arp
->ar_data
[5]);
768 tmp
= NetReadIP(&arp
->ar_data
[6]);
770 /* matched waiting packet's address */
771 if (tmp
== NetArpWaitReplyIP
) {
775 /* save address for later use */
776 memcpy(NetArpWaitPacketMAC
, &arp
->ar_data
[0], 6);
778 /* modify header, and transmit it */
779 memcpy(((Ethernet_t
*)NetArpWaitTxPacket
)->et_dest
, NetArpWaitPacketMAC
, 6);
780 (void) eth_send(NetArpWaitTxPacket
, NetArpWaitTxPacketSize
);
782 /* no arp request pending now */
783 NetArpWaitPacketIP
= 0;
784 NetArpWaitTxPacketSize
= 0;
785 NetArpWaitPacketMAC
= NULL
;
791 printf("Unexpected ARP opcode 0x%x\n", ntohs(arp
->ar_op
));
798 printf("Got RARP\n");
801 if (len
< ARP_HDR_SIZE
) {
802 printf("bad length %d < %d\n", len
, ARP_HDR_SIZE
);
806 if ((ntohs(arp
->ar_op
) != RARPOP_REPLY
) ||
807 (ntohs(arp
->ar_hrd
) != ARP_ETHER
) ||
808 (ntohs(arp
->ar_pro
) != PROT_IP
) ||
809 (arp
->ar_hln
!= 6) || (arp
->ar_pln
!= 4)) {
811 printf("invalid RARP header\n");
813 NetCopyIP(&NetOurIP
, &arp
->ar_data
[16]);
814 NetCopyIP(&NetServerIP
, &arp
->ar_data
[ 6]);
815 memcpy (NetServerEther
, &arp
->ar_data
[ 0], 6);
817 (*packetHandler
)(0,0,0,0);
825 if (len
< IP_HDR_SIZE
) {
826 debug ("len bad %d < %d\n", len
, IP_HDR_SIZE
);
829 if (len
< ntohs(ip
->ip_len
)) {
830 printf("len bad %d < %d\n", len
, ntohs(ip
->ip_len
));
833 len
= ntohs(ip
->ip_len
);
835 printf("len=%d, v=%02x\n", len
, ip
->ip_hl_v
& 0xff);
837 if ((ip
->ip_hl_v
& 0xf0) != 0x40) {
840 if (ip
->ip_off
& htons(0x1fff)) { /* Can't deal w/ fragments */
843 if (!NetCksumOk((uchar
*)ip
, IP_HDR_SIZE_NO_UDP
/ 2)) {
844 printf("checksum bad\n");
847 tmp
= NetReadIP(&ip
->ip_dst
);
848 if (NetOurIP
&& tmp
!= NetOurIP
&& tmp
!= 0xFFFFFFFF) {
852 * watch for ICMP host redirects
854 * There is no real handler code (yet). We just watch
855 * for ICMP host redirect messages. In case anybody
856 * sees these messages: please contact me
857 * (wd@denx.de), or - even better - send me the
858 * necessary fixes :-)
860 * Note: in all cases where I have seen this so far
861 * it was a problem with the router configuration,
862 * for instance when a router was configured in the
863 * BOOTP reply, but the TFTP server was on the same
864 * subnet. So this is probably a warning that your
865 * configuration might be wrong. But I'm not really
866 * sure if there aren't any other situations.
868 if (ip
->ip_p
== IPPROTO_ICMP
) {
869 ICMP_t
*icmph
= (ICMP_t
*)&(ip
->udp_src
);
871 switch (icmph
->type
) {
873 if (icmph
->code
!= ICMP_REDIR_HOST
)
875 puts (" ICMP Host Redirect to ");
876 print_IPaddr(icmph
->un
.gateway
);
879 #if (CONFIG_COMMANDS & CFG_CMD_PING)
880 case ICMP_ECHO_REPLY
:
882 * IP header OK. Pass the packet to the current handler.
884 /* XXX point to ip packet */
885 (*packetHandler
)((uchar
*)ip
, 0, 0, 0);
891 } else if (ip
->ip_p
!= IPPROTO_UDP
) { /* Only UDP packets */
896 * IP header OK. Pass the packet to the current handler.
898 (*packetHandler
)((uchar
*)ip
+IP_HDR_SIZE
,
901 ntohs(ip
->udp_len
) - 8);
908 /**********************************************************************/
910 static int net_check_prereq (proto_t protocol
)
914 #if (CONFIG_COMMANDS & CFG_CMD_PING)
916 if (NetPingIP
== 0) {
917 puts ("*** ERROR: ping address not given\n");
923 if (NetServerIP
== 0) {
924 puts ("*** ERROR: `serverip' not set\n");
928 #if (CONFIG_COMMANDS & CFG_CMD_PING)
933 puts ("*** ERROR: `ipaddr' not set\n");
941 if (memcmp(NetOurEther
, "\0\0\0\0\0\0", 6) == 0) {
942 #ifdef CONFIG_NET_MULTI
943 extern int eth_get_dev_index (void);
944 int num
= eth_get_dev_index();
948 puts ("*** ERROR: No ethernet found.\n");
951 puts ("*** ERROR: `ethaddr' not set\n");
954 printf ("*** ERROR: `eth%daddr' not set\n",
962 puts ("*** ERROR: `ethaddr' not set\n");
972 /**********************************************************************/
975 NetCksumOk(uchar
* ptr
, int len
)
977 return !((NetCksum(ptr
, len
) + 1) & 0xfffe);
982 NetCksum(uchar
* ptr
, int len
)
988 xsum
+= *((ushort
*)ptr
)++;
989 xsum
= (xsum
& 0xffff) + (xsum
>> 16);
990 xsum
= (xsum
& 0xffff) + (xsum
>> 16);
991 return (xsum
& 0xffff);
996 NetSetEther(volatile uchar
* xet
, uchar
* addr
, uint prot
)
998 Ethernet_t
*et
= (Ethernet_t
*)xet
;
1000 memcpy (et
->et_dest
, addr
, 6);
1001 memcpy (et
->et_src
, NetOurEther
, 6);
1002 et
->et_protlen
= htons(prot
);
1007 NetSetIP(volatile uchar
* xip
, IPaddr_t dest
, int dport
, int sport
, int len
)
1009 volatile IP_t
*ip
= (IP_t
*)xip
;
1012 * If the data is an odd number of bytes, zero the
1013 * byte after the last byte so that the checksum
1017 xip
[IP_HDR_SIZE
+ len
] = 0;
1020 * Construct an IP and UDP header.
1021 (need to set no fragment bit - XXX)
1023 ip
->ip_hl_v
= 0x45; /* IP_HDR_SIZE / 4 (not including UDP) */
1025 ip
->ip_len
= htons(IP_HDR_SIZE
+ len
);
1026 ip
->ip_id
= htons(NetIPID
++);
1027 ip
->ip_off
= htons(0x4000); /* No fragmentation */
1029 ip
->ip_p
= 17; /* UDP */
1031 NetCopyIP((void*)&ip
->ip_src
, &NetOurIP
); /* already in network byte order */
1032 NetCopyIP((void*)&ip
->ip_dst
, &dest
); /* - "" - */
1033 ip
->udp_src
= htons(sport
);
1034 ip
->udp_dst
= htons(dport
);
1035 ip
->udp_len
= htons(8 + len
);
1037 ip
->ip_sum
= ~NetCksum((uchar
*)ip
, IP_HDR_SIZE_NO_UDP
/ 2);
1040 void copy_filename (uchar
*dst
, uchar
*src
, int size
)
1042 if (*src
&& (*src
== '"')) {
1047 while ((--size
> 0) && *src
&& (*src
!= '"')) {
1053 #endif /* CFG_CMD_NET */
1055 void ip_to_string (IPaddr_t x
, char *s
)
1058 sprintf (s
,"%d.%d.%d.%d",
1059 (int)((x
>> 24) & 0xff),
1060 (int)((x
>> 16) & 0xff),
1061 (int)((x
>> 8) & 0xff),
1062 (int)((x
>> 0) & 0xff)
1066 IPaddr_t
string_to_ip(char *s
)
1075 for (addr
=0, i
=0; i
<4; ++i
) {
1076 ulong val
= s
? simple_strtoul(s
, &e
, 10) : 0;
1078 addr
|= (val
& 0xFF);
1084 return (htonl(addr
));
1087 void print_IPaddr (IPaddr_t x
)
1091 ip_to_string(x
, tmp
);
1096 IPaddr_t
getenv_IPaddr (char *var
)
1098 return (string_to_ip(getenv(var
)));