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
70 #if (CONFIG_COMMANDS & CFG_CMD_NET)
76 /** BOOTP EXTENTIONS **/
78 IPaddr_t NetOurSubnetMask=0; /* Our subnet mask (0=unknown) */
79 IPaddr_t NetOurGatewayIP=0; /* Our gateways IP address */
80 IPaddr_t NetOurDNSIP=0; /* Our DNS IP address */
81 char NetOurNISDomain[32]={0,}; /* Our NIS domain */
82 char NetOurHostName[32]={0,}; /* Our hostname */
83 char NetOurRootPath[64]={0,}; /* Our bootpath */
84 ushort NetBootFileSize=0; /* Our bootfile size in blocks */
86 /** END OF BOOTP EXTENTIONS **/
88 ulong NetBootFileXferSize; /* The actual transferred size of the bootfile (in bytes) */
89 uchar NetOurEther[6]; /* Our ethernet address */
90 uchar NetServerEther[6] = /* Boot server enet address */
91 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
92 IPaddr_t NetOurIP; /* Our IP addr (0 = unknown) */
93 IPaddr_t NetServerIP; /* Our IP addr (0 = unknown) */
94 volatile uchar *NetRxPkt; /* Current receive packet */
95 int NetRxPktLen; /* Current rx packet length */
96 unsigned NetIPID; /* IP packet ID */
97 uchar NetBcastAddr[6] = /* Ethernet bcast address */
98 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
99 int NetState; /* Network loop state */
100 #ifdef CONFIG_NET_MULTI
101 int NetRestartWrap = 0; /* Tried all network devices */
102 static int NetRestarted = 0; /* Network loop restarted */
103 static int NetDevExists = 0; /* At least one device configured */
106 char BootFile[128]; /* Boot File name */
108 volatile uchar PktBuf[(PKTBUFSRX+1) * PKTSIZE_ALIGN + PKTALIGN];
110 volatile uchar *NetRxPackets[PKTBUFSRX]; /* Receive packets */
112 static rxhand_f *packetHandler; /* Current RX packet handler */
113 static thand_f *timeHandler; /* Current timeout handler */
114 static ulong timeValue; /* Current timeout value */
115 volatile uchar *NetTxPacket = 0; /* THE transmit packet */
117 static int net_check_prereq (proto_t protocol);
119 /**********************************************************************/
121 * Main network processing loop.
125 NetLoop(proto_t protocol)
127 DECLARE_GLOBAL_DATA_PTR;
131 #ifdef CONFIG_NET_MULTI
140 * Setup packet buffers, aligned correctly.
142 NetTxPacket = &PktBuf[0] + (PKTALIGN - 1);
143 NetTxPacket -= (ulong)NetTxPacket % PKTALIGN;
144 for (i = 0; i < PKTBUFSRX; i++) {
145 NetRxPackets[i] = NetTxPacket + (i+1)*PKTSIZE_ALIGN;
153 #ifdef CONFIG_NET_MULTI
154 memcpy (NetOurEther, eth_get_dev()->enetaddr, 6);
156 memcpy (NetOurEther, bd->bi_enetaddr, 6);
159 NetState = NETLOOP_CONTINUE;
162 * Start the ball rolling with the given start function. From
163 * here on, this code is a state machine driven by received
164 * packets and timer events.
169 NetCopyIP(&NetOurIP, &bd->bi_ip_addr);
170 NetServerIP = getenv_IPaddr ("serverip");
171 NetOurGatewayIP = getenv_IPaddr ("gatewayip");
172 NetOurSubnetMask= getenv_IPaddr ("netmask");
177 * initialize our IP addr to 0 in order to accept ANY
178 * IP addr assigned to us by the BOOTP / RARP server
187 switch (net_check_prereq (protocol)) {
189 /* network not configured */
192 #ifdef CONFIG_NET_MULTI
194 /* network device not configured */
196 #endif /* CONFIG_NET_MULTI */
199 #ifdef CONFIG_NET_MULTI
204 /* always use ARP to get server ethernet address */
209 #if (CONFIG_COMMANDS & CFG_CMD_DHCP)
211 /* Start with a clean slate... */
214 DhcpRequest(); /* Basically same as BOOTP */
216 #endif /* CFG_CMD_DHCP */
231 NetBootFileXferSize = 0;
237 * Main packet reception loop. Loop receiving packets until
238 * someone sets `NetQuit'.
242 #ifdef CONFIG_SHOW_ACTIVITY
244 extern void show_activity(int arg);
249 * Check the ethernet for a new packet. The ethernet
250 * receive routine will process it.
255 * Abort if ctrl-c was pressed.
265 * Check for a timeout, and run the timeout handler
268 if (timeHandler && (get_timer(0) > timeValue)) {
272 timeHandler = (thand_f *)0;
279 case NETLOOP_RESTART:
280 #ifdef CONFIG_NET_MULTI
285 case NETLOOP_SUCCESS:
286 if (NetBootFileXferSize > 0) {
288 printf("Bytes transferred = %ld (%lx hex)\n",
290 NetBootFileXferSize);
291 sprintf(buf, "%lx", NetBootFileXferSize);
292 setenv("filesize", buf);
295 return NetBootFileXferSize;
303 /**********************************************************************/
306 startAgainTimeout(void)
308 NetState = NETLOOP_RESTART;
312 startAgainHandler(uchar * pkt, unsigned dest, unsigned src, unsigned len)
314 /* Totally ignore the packet */
320 #ifndef CONFIG_NET_MULTI
321 NetSetTimeout(10 * CFG_HZ, startAgainTimeout);
322 NetSetHandler(startAgainHandler);
324 DECLARE_GLOBAL_DATA_PTR;
327 eth_try_another(!NetRestarted);
334 NetSetTimeout(10 * CFG_HZ, startAgainTimeout);
335 NetSetHandler(startAgainHandler);
339 NetState = NETLOOP_FAIL;
344 NetState = NETLOOP_RESTART;
349 /**********************************************************************/
355 NetSetHandler(rxhand_f * f)
362 NetSetTimeout(int iv, thand_f * f)
365 timeHandler = (thand_f *)0;
368 timeValue = get_timer(0) + iv;
374 NetSendPacket(volatile uchar * pkt, int len)
376 (void) eth_send(pkt, len);
382 NetReceive(volatile uchar * pkt, int len)
393 et = (Ethernet_t *)pkt;
395 x = ntohs(et->et_protlen);
399 * Got a 802 packet. Check the other protocol field.
401 x = ntohs(et->et_prot);
402 ip = (IP_t *)(pkt + E802_HDR_SIZE);
403 len -= E802_HDR_SIZE;
405 ip = (IP_t *)(pkt + ETHER_HDR_SIZE);
406 len -= ETHER_HDR_SIZE;
410 printf("Receive from protocol 0x%x\n", x);
417 * We have to deal with two types of ARP packets:
418 * - REQUEST packets will be answered by sending our
419 * IP address - if we know it.
420 * - REPLY packates are expected only after we asked
421 * for the TFTP server's or the gateway's ethernet
422 * address; so if we receive such a packet, we set
423 * the server ethernet address
429 if (len < ARP_HDR_SIZE) {
430 printf("bad length %d < %d\n", len, ARP_HDR_SIZE);
433 if (ntohs(arp->ar_hrd) != ARP_ETHER) {
436 if (ntohs(arp->ar_pro) != PROT_IP) {
439 if (arp->ar_hln != 6) {
442 if (arp->ar_pln != 4) {
450 if (NetReadIP(&arp->ar_data[16]) != NetOurIP) {
454 switch (ntohs(arp->ar_op)) {
455 case ARPOP_REQUEST: /* reply with our IP address */
457 printf("Got ARP REQUEST, return our IP\n");
459 NetSetEther((uchar *)et, et->et_src, PROT_ARP);
460 arp->ar_op = htons(ARPOP_REPLY);
461 memcpy (&arp->ar_data[10], &arp->ar_data[0], 6);
462 NetCopyIP(&arp->ar_data[16], &arp->ar_data[6]);
463 memcpy (&arp->ar_data[ 0], NetOurEther, 6);
464 NetCopyIP(&arp->ar_data[ 6], &NetOurIP);
465 NetSendPacket((uchar *)et,((uchar *)arp-pkt)+ARP_HDR_SIZE);
467 case ARPOP_REPLY: /* set TFTP server eth addr */
469 printf("Got ARP REPLY, set server/gtwy eth addr\n");
471 memcpy (NetServerEther, &arp->ar_data[0], 6);
472 (*packetHandler)(0,0,0,0); /* start TFTP */
476 printf("Unexpected ARP opcode 0x%x\n", ntohs(arp->ar_op));
483 printf("Got RARP\n");
486 if (len < ARP_HDR_SIZE) {
487 printf("bad length %d < %d\n", len, ARP_HDR_SIZE);
491 if ((ntohs(arp->ar_op) != RARPOP_REPLY) ||
492 (ntohs(arp->ar_hrd) != ARP_ETHER) ||
493 (ntohs(arp->ar_pro) != PROT_IP) ||
494 (arp->ar_hln != 6) || (arp->ar_pln != 4)) {
496 printf("invalid RARP header\n");
498 NetCopyIP(&NetOurIP, &arp->ar_data[16]);
499 NetCopyIP(&NetServerIP, &arp->ar_data[ 6]);
500 memcpy (NetServerEther, &arp->ar_data[ 0], 6);
502 (*packetHandler)(0,0,0,0);
510 if (len < IP_HDR_SIZE) {
511 debug ("len bad %d < %d\n", len, IP_HDR_SIZE);
514 if (len < ntohs(ip->ip_len)) {
515 printf("len bad %d < %d\n", len, ntohs(ip->ip_len));
518 len = ntohs(ip->ip_len);
520 printf("len=%d, v=%02x\n", len, ip->ip_hl_v & 0xff);
522 if ((ip->ip_hl_v & 0xf0) != 0x40) {
525 if (ip->ip_off & htons(0x1fff)) { /* Can't deal w/ fragments */
528 if (!NetCksumOk((uchar *)ip, IP_HDR_SIZE_NO_UDP / 2)) {
529 printf("checksum bad\n");
532 tmp = NetReadIP(&ip->ip_dst);
533 if (NetOurIP && tmp != NetOurIP && tmp != 0xFFFFFFFF) {
537 * watch for ICMP host redirects
539 * There is no real handler code (yet). We just watch
540 * for ICMP host redirect messages. In case anybody
541 * sees these messages: please contact me
542 * (wd@denx.de), or - even better - send me the
543 * necessary fixes :-)
545 * Note: in all cases where I have seen this so far
546 * it was a problem with the router configuration,
547 * for instance when a router was configured in the
548 * BOOTP reply, but the TFTP server was on the same
549 * subnet. So this is probably a warning that your
550 * configuration might be wrong. But I'm not really
551 * sure if there aren't any other situations.
553 if (ip->ip_p == IPPROTO_ICMP) {
554 ICMP_t *icmph = (ICMP_t *)&(ip->udp_src);
556 if (icmph->type != ICMP_REDIRECT)
558 if (icmph->code != ICMP_REDIR_HOST)
560 puts (" ICMP Host Redirect to ");
561 print_IPaddr(icmph->un.gateway);
563 } else if (ip->ip_p != IPPROTO_UDP) { /* Only UDP packets */
568 * IP header OK. Pass the packet to the current handler.
570 (*packetHandler)((uchar *)ip +IP_HDR_SIZE,
573 ntohs(ip->udp_len) - 8);
580 /**********************************************************************/
582 static int net_check_prereq (proto_t protocol)
585 case ARP: /* nothing to do */
589 if (NetServerIP == 0) {
590 puts ("*** ERROR: `serverip' not set\n");
595 puts ("*** ERROR: `ipaddr' not set\n");
603 if (memcmp(NetOurEther, "\0\0\0\0\0\0", 6) == 0) {
604 #ifdef CONFIG_NET_MULTI
605 extern int eth_get_dev_index (void);
606 int num = eth_get_dev_index();
610 puts ("*** ERROR: No ethernet found.\n");
613 puts ("*** ERROR: `ethaddr' not set\n");
616 printf ("*** ERROR: `eth%daddr' not set\n",
624 puts ("*** ERROR: `ethaddr' not set\n");
632 /**********************************************************************/
635 NetCksumOk(uchar * ptr, int len)
637 return !((NetCksum(ptr, len) + 1) & 0xfffe);
642 NetCksum(uchar * ptr, int len)
648 xsum += *((ushort *)ptr)++;
649 xsum = (xsum & 0xffff) + (xsum >> 16);
650 xsum = (xsum & 0xffff) + (xsum >> 16);
651 return (xsum & 0xffff);
656 NetSetEther(volatile uchar * xet, uchar * addr, uint prot)
658 Ethernet_t *et = (Ethernet_t *)xet;
660 memcpy (et->et_dest, addr, 6);
661 memcpy (et->et_src, NetOurEther, 6);
662 et->et_protlen = htons(prot);
667 NetSetIP(volatile uchar * xip, IPaddr_t dest, int dport, int sport, int len)
669 volatile IP_t *ip = (IP_t *)xip;
672 * If the data is an odd number of bytes, zero the
673 * byte after the last byte so that the checksum
677 xip[IP_HDR_SIZE + len] = 0;
680 * Construct an IP and UDP header.
681 (need to set no fragment bit - XXX)
683 ip->ip_hl_v = 0x45; /* IP_HDR_SIZE / 4 (not including UDP) */
685 ip->ip_len = htons(IP_HDR_SIZE + len);
686 ip->ip_id = htons(NetIPID++);
687 ip->ip_off = htons(0x4000); /* No fragmentation */
689 ip->ip_p = 17; /* UDP */
691 NetCopyIP((void*)&ip->ip_src, &NetOurIP); /* already in network byte order */
692 NetCopyIP((void*)&ip->ip_dst, &dest); /* - "" - */
693 ip->udp_src = htons(sport);
694 ip->udp_dst = htons(dport);
695 ip->udp_len = htons(8 + len);
697 ip->ip_sum = ~NetCksum((uchar *)ip, IP_HDR_SIZE_NO_UDP / 2);
700 void copy_filename (uchar *dst, uchar *src, int size)
702 if (*src && (*src == '"')) {
707 while ((--size > 0) && *src && (*src != '"')) {
713 #endif /* CFG_CMD_NET */
715 void ip_to_string (IPaddr_t x, char *s)
718 sprintf (s,"%d.%d.%d.%d",
719 (int)((x >> 24) & 0xff),
720 (int)((x >> 16) & 0xff),
721 (int)((x >> 8) & 0xff),
722 (int)((x >> 0) & 0xff)
726 void print_IPaddr (IPaddr_t x)
730 ip_to_string(x, tmp);
735 IPaddr_t getenv_IPaddr (char *var)
743 for (addr=0, i=0; i<4; ++i) {
744 ulong val = s ? simple_strtoul(s, &e, 10) : 0;
746 addr |= (val & 0xFF);
752 return (htonl(addr));