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
69 #if (CONFIG_COMMANDS & CFG_CMD_NET)
71 #define ARP_TIMEOUT 5 /* Seconds before trying ARP again */
72 #ifndef CONFIG_NET_RETRY_COUNT
73 # define ARP_TIMEOUT_COUNT 5 /* # of timeouts before giving up */
75 # define ARP_TIMEOUT_COUNT (CONFIG_NET_RETRY_COUNT)
82 /** BOOTP EXTENTIONS **/
84 IPaddr_t NetOurSubnetMask
=0; /* Our subnet mask (0=unknown) */
85 IPaddr_t NetOurGatewayIP
=0; /* Our gateways IP address */
86 IPaddr_t NetOurDNSIP
=0; /* Our DNS IP address */
87 #if (CONFIG_BOOTP_MASK & CONFIG_BOOTP_DNS2)
88 IPaddr_t NetOurDNS2IP
=0; /* Our 2nd DNS IP address */
90 char NetOurNISDomain
[32]={0,}; /* Our NIS domain */
91 char NetOurHostName
[32]={0,}; /* Our hostname */
92 char NetOurRootPath
[64]={0,}; /* Our bootpath */
93 ushort NetBootFileSize
=0; /* Our bootfile size in blocks */
95 /** END OF BOOTP EXTENTIONS **/
97 ulong NetBootFileXferSize
; /* The actual transferred size of the bootfile (in bytes) */
98 uchar NetOurEther
[6]; /* Our ethernet address */
99 uchar NetServerEther
[6] = /* Boot server enet address */
100 { 0, 0, 0, 0, 0, 0 };
101 IPaddr_t NetOurIP
; /* Our IP addr (0 = unknown) */
102 IPaddr_t NetServerIP
; /* Our IP addr (0 = unknown) */
103 volatile uchar
*NetRxPkt
; /* Current receive packet */
104 int NetRxPktLen
; /* Current rx packet length */
105 unsigned NetIPID
; /* IP packet ID */
106 uchar NetBcastAddr
[6] = /* Ethernet bcast address */
107 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
108 uchar NetEtherNullAddr
[6] =
109 { 0, 0, 0, 0, 0, 0 };
110 int NetState
; /* Network loop state */
111 #ifdef CONFIG_NET_MULTI
112 int NetRestartWrap
= 0; /* Tried all network devices */
113 static int NetRestarted
= 0; /* Network loop restarted */
114 static int NetDevExists
= 0; /* At least one device configured */
117 char BootFile
[128]; /* Boot File name */
119 #if (CONFIG_COMMANDS & CFG_CMD_PING)
120 IPaddr_t NetPingIP
; /* the ip address to ping */
122 static void PingStart(void);
125 volatile uchar PktBuf
[(PKTBUFSRX
+1) * PKTSIZE_ALIGN
+ PKTALIGN
];
127 volatile uchar
*NetRxPackets
[PKTBUFSRX
]; /* Receive packets */
129 static rxhand_f
*packetHandler
; /* Current RX packet handler */
130 static thand_f
*timeHandler
; /* Current timeout handler */
131 static ulong timeStart
; /* Time base value */
132 static ulong timeDelta
; /* Current timeout value */
133 volatile uchar
*NetTxPacket
= 0; /* THE transmit packet */
135 static int net_check_prereq (proto_t protocol
);
137 /**********************************************************************/
139 IPaddr_t NetArpWaitPacketIP
;
140 IPaddr_t NetArpWaitReplyIP
;
141 uchar
*NetArpWaitPacketMAC
; /* MAC address of waiting packet's destination */
142 uchar
*NetArpWaitTxPacket
; /* THE transmit packet */
143 int NetArpWaitTxPacketSize
;
144 uchar NetArpWaitPacketBuf
[PKTSIZE_ALIGN
+ PKTALIGN
];
145 ulong NetArpWaitTimerStart
;
148 void ArpRequest(void)
155 printf("ARP broadcast %d\n", NetArpWaitTry
);
159 NetSetEther(pkt
, NetBcastAddr
, PROT_ARP
);
160 pkt
+= ETHER_HDR_SIZE
;
164 arp
->ar_hrd
= htons(ARP_ETHER
);
165 arp
->ar_pro
= htons(PROT_IP
);
168 arp
->ar_op
= htons(ARPOP_REQUEST
);
170 memcpy (&arp
->ar_data
[0], NetOurEther
, 6); /* source ET addr */
171 NetWriteIP((uchar
*)&arp
->ar_data
[6], NetOurIP
); /* source IP addr */
172 for (i
=10; i
<16; ++i
) {
173 arp
->ar_data
[i
] = 0; /* dest ET addr = 0 */
176 if((NetArpWaitPacketIP
& NetOurSubnetMask
) != (NetOurIP
& NetOurSubnetMask
)) {
177 if (NetOurGatewayIP
== 0) {
178 puts ("## Warning: gatewayip needed but not set\n");
180 NetArpWaitReplyIP
= NetOurGatewayIP
;
182 NetArpWaitReplyIP
= NetArpWaitPacketIP
;
184 NetWriteIP((uchar
*)&arp
->ar_data
[16], NetArpWaitReplyIP
);
185 (void) eth_send(NetTxPacket
, ETHER_HDR_SIZE
+ ARP_HDR_SIZE
);
188 void ArpTimeoutCheck(void)
192 if (!NetArpWaitPacketIP
)
197 /* check for arp timeout */
198 if ((t
- NetArpWaitTimerStart
) > ARP_TIMEOUT
* CFG_HZ
) {
201 if (NetArpWaitTry
>= ARP_TIMEOUT_COUNT
) {
202 puts ("\nARP Retry count exceeded; starting again\n");
206 NetArpWaitTimerStart
= t
;
212 /**********************************************************************/
214 * Main network processing loop.
218 NetLoop(proto_t protocol
)
220 DECLARE_GLOBAL_DATA_PTR
;
224 #ifdef CONFIG_NET_MULTI
229 /* XXX problem with bss workaround */
230 NetArpWaitPacketMAC
= NULL
;
231 NetArpWaitTxPacket
= NULL
;
232 NetArpWaitPacketIP
= 0;
233 NetArpWaitReplyIP
= 0;
234 NetArpWaitTxPacket
= NULL
;
241 * Setup packet buffers, aligned correctly.
243 NetTxPacket
= &PktBuf
[0] + (PKTALIGN
- 1);
244 NetTxPacket
-= (ulong
)NetTxPacket
% PKTALIGN
;
245 for (i
= 0; i
< PKTBUFSRX
; i
++) {
246 NetRxPackets
[i
] = NetTxPacket
+ (i
+1)*PKTSIZE_ALIGN
;
251 if (!NetArpWaitTxPacket
) {
252 NetArpWaitTxPacket
= &NetArpWaitPacketBuf
[0] + (PKTALIGN
- 1);
253 NetArpWaitTxPacket
-= (ulong
)NetArpWaitTxPacket
% PKTALIGN
;
254 NetArpWaitTxPacketSize
= 0;
262 #ifdef CONFIG_NET_MULTI
263 memcpy (NetOurEther
, eth_get_dev()->enetaddr
, 6);
265 memcpy (NetOurEther
, bd
->bi_enetaddr
, 6);
268 NetState
= NETLOOP_CONTINUE
;
271 * Start the ball rolling with the given start function. From
272 * here on, this code is a state machine driven by received
273 * packets and timer events.
277 #if (CONFIG_COMMANDS & CFG_CMD_PING)
281 NetCopyIP(&NetOurIP
, &bd
->bi_ip_addr
);
282 NetOurGatewayIP
= getenv_IPaddr ("gatewayip");
283 NetOurSubnetMask
= getenv_IPaddr ("netmask");
287 NetServerIP
= getenv_IPaddr ("serverip");
289 #if (CONFIG_COMMANDS & CFG_CMD_PING)
302 * initialize our IP addr to 0 in order to accept ANY
303 * IP addr assigned to us by the BOOTP / RARP server
312 switch (net_check_prereq (protocol
)) {
314 /* network not configured */
317 #ifdef CONFIG_NET_MULTI
319 /* network device not configured */
321 #endif /* CONFIG_NET_MULTI */
324 #ifdef CONFIG_NET_MULTI
329 /* always use ARP to get server ethernet address */
333 #if (CONFIG_COMMANDS & CFG_CMD_DHCP)
335 /* Start with a clean slate... */
338 DhcpRequest(); /* Basically same as BOOTP */
340 #endif /* CFG_CMD_DHCP */
351 #if (CONFIG_COMMANDS & CFG_CMD_PING)
360 NetBootFileXferSize
= 0;
366 * Main packet reception loop. Loop receiving packets until
367 * someone sets `NetQuit'.
371 #ifdef CONFIG_SHOW_ACTIVITY
373 extern void show_activity(int arg
);
378 * Check the ethernet for a new packet. The ethernet
379 * receive routine will process it.
384 * Abort if ctrl-c was pressed.
395 * Check for a timeout, and run the timeout handler
398 if (timeHandler
&& ((get_timer(0) - timeStart
) > timeDelta
)) {
402 timeHandler
= (thand_f
*)0;
409 case NETLOOP_RESTART
:
410 #ifdef CONFIG_NET_MULTI
415 case NETLOOP_SUCCESS
:
416 if (NetBootFileXferSize
> 0) {
418 printf("Bytes transferred = %ld (%lx hex)\n",
420 NetBootFileXferSize
);
421 sprintf(buf
, "%lx", NetBootFileXferSize
);
422 setenv("filesize", buf
);
425 return NetBootFileXferSize
;
433 /**********************************************************************/
436 startAgainTimeout(void)
438 NetState
= NETLOOP_RESTART
;
442 startAgainHandler(uchar
* pkt
, unsigned dest
, unsigned src
, unsigned len
)
444 /* Totally ignore the packet */
450 #ifndef CONFIG_NET_MULTI
451 NetSetTimeout(10 * CFG_HZ
, startAgainTimeout
);
452 NetSetHandler(startAgainHandler
);
454 DECLARE_GLOBAL_DATA_PTR
;
457 eth_try_another(!NetRestarted
);
464 NetSetTimeout(10 * CFG_HZ
, startAgainTimeout
);
465 NetSetHandler(startAgainHandler
);
469 NetState
= NETLOOP_FAIL
;
474 NetState
= NETLOOP_RESTART
;
479 /**********************************************************************/
485 NetSetHandler(rxhand_f
* f
)
492 NetSetTimeout(int iv
, thand_f
* f
)
495 timeHandler
= (thand_f
*)0;
498 timeStart
= get_timer(0);
505 NetSendPacket(volatile uchar
* pkt
, int len
)
507 (void) eth_send(pkt
, len
);
511 NetSendUDPPacket(uchar
*ether
, IPaddr_t dest
, int dport
, int sport
, int len
)
513 /* convert to new style broadcast */
517 /* if broadcast, make the ether address a broadcast and don't do ARP */
518 if (dest
== 0xFFFFFFFF)
519 ether
= NetBcastAddr
;
521 /* if MAC address was not discovered yet, save the packet and do an ARP request */
522 if (memcmp(ether
, NetEtherNullAddr
, 6) == 0) {
525 printf("sending ARP for %08lx\n", dest
);
528 NetArpWaitPacketIP
= dest
;
529 NetArpWaitPacketMAC
= ether
;
530 NetSetEther (NetArpWaitTxPacket
, NetArpWaitPacketMAC
, PROT_IP
);
531 NetSetIP (NetArpWaitTxPacket
+ ETHER_HDR_SIZE
, dest
, dport
, sport
, len
);
532 memcpy(NetArpWaitTxPacket
+ ETHER_HDR_SIZE
+ IP_HDR_SIZE
,
533 (uchar
*)NetTxPacket
+ ETHER_HDR_SIZE
+ IP_HDR_SIZE
, len
);
535 /* size of the waiting packet */
536 NetArpWaitTxPacketSize
= ETHER_HDR_SIZE
+ IP_HDR_SIZE
+ len
;
538 /* and do the ARP request */
540 NetArpWaitTimerStart
= get_timer(0);
542 return 1; /* waiting */
546 printf("sending UDP to %08lx/%02x:%02x:%02x:%02x:%02x:%02x\n",
547 dest
, ether
[0], ether
[1], ether
[2], ether
[3], ether
[4], ether
[5]);
550 NetSetEther (NetTxPacket
, ether
, PROT_IP
);
551 NetSetIP (NetTxPacket
+ ETHER_HDR_SIZE
, dest
, dport
, sport
, len
);
552 (void) eth_send(NetTxPacket
, ETHER_HDR_SIZE
+ IP_HDR_SIZE
+ len
);
554 return 0; /* transmited */
557 #if (CONFIG_COMMANDS & CFG_CMD_PING)
558 static ushort PingSeqNo
;
566 /* XXX always send arp request */
568 memcpy(mac
, NetEtherNullAddr
, 6);
571 printf("sending ARP for %08lx\n", NetPingIP
);
574 NetArpWaitPacketIP
= NetPingIP
;
575 NetArpWaitPacketMAC
= mac
;
577 NetSetEther(NetArpWaitTxPacket
, mac
, PROT_IP
);
579 ip
= (volatile IP_t
*)(NetArpWaitTxPacket
+ ETHER_HDR_SIZE
);
582 * Construct an IP and ICMP header. (need to set no fragment bit - XXX)
584 ip
->ip_hl_v
= 0x45; /* IP_HDR_SIZE / 4 (not including UDP) */
586 ip
->ip_len
= htons(IP_HDR_SIZE_NO_UDP
+ 8);
587 ip
->ip_id
= htons(NetIPID
++);
588 ip
->ip_off
= htons(0x4000); /* No fragmentation */
590 ip
->ip_p
= 0x01; /* ICMP */
592 NetCopyIP((void*)&ip
->ip_src
, &NetOurIP
); /* already in network byte order */
593 NetCopyIP((void*)&ip
->ip_dst
, &NetPingIP
); /* - "" - */
594 ip
->ip_sum
= ~NetCksum((uchar
*)ip
, IP_HDR_SIZE_NO_UDP
/ 2);
596 s
= &ip
->udp_src
; /* XXX ICMP starts here */
597 s
[0] = htons(0x0800); /* echo-request, code */
598 s
[1] = 0; /* checksum */
599 s
[2] = 0; /* identifier */
600 s
[3] = htons(PingSeqNo
++); /* sequence number */
601 s
[1] = ~NetCksum((uchar
*)s
, 8/2);
603 /* size of the waiting packet */
604 NetArpWaitTxPacketSize
= ETHER_HDR_SIZE
+ IP_HDR_SIZE_NO_UDP
+ 8;
606 /* and do the ARP request */
608 NetArpWaitTimerStart
= get_timer(0);
610 return 1; /* waiting */
617 NetState
= NETLOOP_FAIL
; /* we did not get the reply */
621 PingHandler (uchar
* pkt
, unsigned dest
, unsigned src
, unsigned len
)
624 volatile IP_t
*ip
= (volatile IP_t
*)pkt
;
626 tmp
= NetReadIP((void *)&ip
->ip_src
);
627 if (tmp
!= NetPingIP
)
630 NetState
= NETLOOP_SUCCESS
;
633 static void PingStart(void)
635 NetSetTimeout (10 * CFG_HZ
, PingTimeout
);
636 NetSetHandler (PingHandler
);
644 NetReceive(volatile uchar
* pkt
, int len
)
654 et
= (Ethernet_t
*)pkt
;
656 x
= ntohs(et
->et_protlen
);
660 * Got a 802 packet. Check the other protocol field.
662 x
= ntohs(et
->et_prot
);
663 ip
= (IP_t
*)(pkt
+ E802_HDR_SIZE
);
664 len
-= E802_HDR_SIZE
;
666 ip
= (IP_t
*)(pkt
+ ETHER_HDR_SIZE
);
667 len
-= ETHER_HDR_SIZE
;
671 printf("Receive from protocol 0x%x\n", x
);
678 * We have to deal with two types of ARP packets:
679 * - REQUEST packets will be answered by sending our
680 * IP address - if we know it.
681 * - REPLY packates are expected only after we asked
682 * for the TFTP server's or the gateway's ethernet
683 * address; so if we receive such a packet, we set
684 * the server ethernet address
690 if (len
< ARP_HDR_SIZE
) {
691 printf("bad length %d < %d\n", len
, ARP_HDR_SIZE
);
694 if (ntohs(arp
->ar_hrd
) != ARP_ETHER
) {
697 if (ntohs(arp
->ar_pro
) != PROT_IP
) {
700 if (arp
->ar_hln
!= 6) {
703 if (arp
->ar_pln
!= 4) {
711 if (NetReadIP(&arp
->ar_data
[16]) != NetOurIP
) {
715 switch (ntohs(arp
->ar_op
)) {
716 case ARPOP_REQUEST
: /* reply with our IP address */
718 printf("Got ARP REQUEST, return our IP\n");
720 NetSetEther((uchar
*)et
, et
->et_src
, PROT_ARP
);
721 arp
->ar_op
= htons(ARPOP_REPLY
);
722 memcpy (&arp
->ar_data
[10], &arp
->ar_data
[0], 6);
723 NetCopyIP(&arp
->ar_data
[16], &arp
->ar_data
[6]);
724 memcpy (&arp
->ar_data
[ 0], NetOurEther
, 6);
725 NetCopyIP(&arp
->ar_data
[ 6], &NetOurIP
);
726 (void) eth_send((uchar
*)et
, ((uchar
*)arp
-pkt
) + ARP_HDR_SIZE
);
729 case ARPOP_REPLY
: /* arp reply */
730 /* are we waiting for a reply */
731 if (!NetArpWaitPacketIP
|| !NetArpWaitPacketMAC
)
734 printf("Got ARP REPLY, set server/gtwy eth addr (%02x:%02x:%02x:%02x:%02x:%02x)\n",
735 arp
->ar_data
[0], arp
->ar_data
[1],
736 arp
->ar_data
[2], arp
->ar_data
[3],
737 arp
->ar_data
[4], arp
->ar_data
[5]);
740 tmp
= NetReadIP(&arp
->ar_data
[6]);
742 /* matched waiting packet's address */
743 if (tmp
== NetArpWaitReplyIP
) {
747 /* save address for later use */
748 memcpy(NetArpWaitPacketMAC
, &arp
->ar_data
[0], 6);
750 /* modify header, and transmit it */
751 memcpy(((Ethernet_t
*)NetArpWaitTxPacket
)->et_dest
, NetArpWaitPacketMAC
, 6);
752 (void) eth_send(NetArpWaitTxPacket
, NetArpWaitTxPacketSize
);
754 /* no arp request pending now */
755 NetArpWaitPacketIP
= 0;
756 NetArpWaitTxPacketSize
= 0;
757 NetArpWaitPacketMAC
= NULL
;
763 printf("Unexpected ARP opcode 0x%x\n", ntohs(arp
->ar_op
));
770 printf("Got RARP\n");
773 if (len
< ARP_HDR_SIZE
) {
774 printf("bad length %d < %d\n", len
, ARP_HDR_SIZE
);
778 if ((ntohs(arp
->ar_op
) != RARPOP_REPLY
) ||
779 (ntohs(arp
->ar_hrd
) != ARP_ETHER
) ||
780 (ntohs(arp
->ar_pro
) != PROT_IP
) ||
781 (arp
->ar_hln
!= 6) || (arp
->ar_pln
!= 4)) {
783 printf("invalid RARP header\n");
785 NetCopyIP(&NetOurIP
, &arp
->ar_data
[16]);
786 NetCopyIP(&NetServerIP
, &arp
->ar_data
[ 6]);
787 memcpy (NetServerEther
, &arp
->ar_data
[ 0], 6);
789 (*packetHandler
)(0,0,0,0);
797 if (len
< IP_HDR_SIZE
) {
798 debug ("len bad %d < %d\n", len
, IP_HDR_SIZE
);
801 if (len
< ntohs(ip
->ip_len
)) {
802 printf("len bad %d < %d\n", len
, ntohs(ip
->ip_len
));
805 len
= ntohs(ip
->ip_len
);
807 printf("len=%d, v=%02x\n", len
, ip
->ip_hl_v
& 0xff);
809 if ((ip
->ip_hl_v
& 0xf0) != 0x40) {
812 if (ip
->ip_off
& htons(0x1fff)) { /* Can't deal w/ fragments */
815 if (!NetCksumOk((uchar
*)ip
, IP_HDR_SIZE_NO_UDP
/ 2)) {
816 printf("checksum bad\n");
819 tmp
= NetReadIP(&ip
->ip_dst
);
820 if (NetOurIP
&& tmp
!= NetOurIP
&& tmp
!= 0xFFFFFFFF) {
824 * watch for ICMP host redirects
826 * There is no real handler code (yet). We just watch
827 * for ICMP host redirect messages. In case anybody
828 * sees these messages: please contact me
829 * (wd@denx.de), or - even better - send me the
830 * necessary fixes :-)
832 * Note: in all cases where I have seen this so far
833 * it was a problem with the router configuration,
834 * for instance when a router was configured in the
835 * BOOTP reply, but the TFTP server was on the same
836 * subnet. So this is probably a warning that your
837 * configuration might be wrong. But I'm not really
838 * sure if there aren't any other situations.
840 if (ip
->ip_p
== IPPROTO_ICMP
) {
841 ICMP_t
*icmph
= (ICMP_t
*)&(ip
->udp_src
);
843 switch (icmph
->type
) {
845 if (icmph
->code
!= ICMP_REDIR_HOST
)
847 puts (" ICMP Host Redirect to ");
848 print_IPaddr(icmph
->un
.gateway
);
851 #if (CONFIG_COMMANDS & CFG_CMD_PING)
852 case ICMP_ECHO_REPLY
:
854 * IP header OK. Pass the packet to the current handler.
856 /* XXX point to ip packet */
857 (*packetHandler
)((uchar
*)ip
, 0, 0, 0);
863 } else if (ip
->ip_p
!= IPPROTO_UDP
) { /* Only UDP packets */
868 * IP header OK. Pass the packet to the current handler.
870 (*packetHandler
)((uchar
*)ip
+IP_HDR_SIZE
,
873 ntohs(ip
->udp_len
) - 8);
880 /**********************************************************************/
882 static int net_check_prereq (proto_t protocol
)
886 #if (CONFIG_COMMANDS & CFG_CMD_PING)
888 if (NetPingIP
== 0) {
889 puts ("*** ERROR: ping address not given\n");
895 if (NetServerIP
== 0) {
896 puts ("*** ERROR: `serverip' not set\n");
900 #if (CONFIG_COMMANDS & CFG_CMD_PING)
905 puts ("*** ERROR: `ipaddr' not set\n");
913 if (memcmp(NetOurEther
, "\0\0\0\0\0\0", 6) == 0) {
914 #ifdef CONFIG_NET_MULTI
915 extern int eth_get_dev_index (void);
916 int num
= eth_get_dev_index();
920 puts ("*** ERROR: No ethernet found.\n");
923 puts ("*** ERROR: `ethaddr' not set\n");
926 printf ("*** ERROR: `eth%daddr' not set\n",
934 puts ("*** ERROR: `ethaddr' not set\n");
944 /**********************************************************************/
947 NetCksumOk(uchar
* ptr
, int len
)
949 return !((NetCksum(ptr
, len
) + 1) & 0xfffe);
954 NetCksum(uchar
* ptr
, int len
)
960 xsum
+= *((ushort
*)ptr
)++;
961 xsum
= (xsum
& 0xffff) + (xsum
>> 16);
962 xsum
= (xsum
& 0xffff) + (xsum
>> 16);
963 return (xsum
& 0xffff);
968 NetSetEther(volatile uchar
* xet
, uchar
* addr
, uint prot
)
970 Ethernet_t
*et
= (Ethernet_t
*)xet
;
972 memcpy (et
->et_dest
, addr
, 6);
973 memcpy (et
->et_src
, NetOurEther
, 6);
974 et
->et_protlen
= htons(prot
);
979 NetSetIP(volatile uchar
* xip
, IPaddr_t dest
, int dport
, int sport
, int len
)
981 volatile IP_t
*ip
= (IP_t
*)xip
;
984 * If the data is an odd number of bytes, zero the
985 * byte after the last byte so that the checksum
989 xip
[IP_HDR_SIZE
+ len
] = 0;
992 * Construct an IP and UDP header.
993 (need to set no fragment bit - XXX)
995 ip
->ip_hl_v
= 0x45; /* IP_HDR_SIZE / 4 (not including UDP) */
997 ip
->ip_len
= htons(IP_HDR_SIZE
+ len
);
998 ip
->ip_id
= htons(NetIPID
++);
999 ip
->ip_off
= htons(0x4000); /* No fragmentation */
1001 ip
->ip_p
= 17; /* UDP */
1003 NetCopyIP((void*)&ip
->ip_src
, &NetOurIP
); /* already in network byte order */
1004 NetCopyIP((void*)&ip
->ip_dst
, &dest
); /* - "" - */
1005 ip
->udp_src
= htons(sport
);
1006 ip
->udp_dst
= htons(dport
);
1007 ip
->udp_len
= htons(8 + len
);
1009 ip
->ip_sum
= ~NetCksum((uchar
*)ip
, IP_HDR_SIZE_NO_UDP
/ 2);
1012 void copy_filename (uchar
*dst
, uchar
*src
, int size
)
1014 if (*src
&& (*src
== '"')) {
1019 while ((--size
> 0) && *src
&& (*src
!= '"')) {
1025 #endif /* CFG_CMD_NET */
1027 void ip_to_string (IPaddr_t x
, char *s
)
1030 sprintf (s
,"%d.%d.%d.%d",
1031 (int)((x
>> 24) & 0xff),
1032 (int)((x
>> 16) & 0xff),
1033 (int)((x
>> 8) & 0xff),
1034 (int)((x
>> 0) & 0xff)
1038 IPaddr_t
string_to_ip(char *s
)
1047 for (addr
=0, i
=0; i
<4; ++i
) {
1048 ulong val
= s
? simple_strtoul(s
, &e
, 10) : 0;
1050 addr
|= (val
& 0xFF);
1056 return (htonl(addr
));
1059 void print_IPaddr (IPaddr_t x
)
1063 ip_to_string(x
, tmp
);
1068 IPaddr_t
getenv_IPaddr (char *var
)
1070 return (string_to_ip(getenv(var
)));