2 * Based on LiMon - BOOTP.
4 * Copyright 1994, 1995, 2000 Neil Russell.
6 * Copyright 2000 Roland Borde
7 * Copyright 2000 Paolo Scaffardi
8 * Copyright 2000-2004 Wolfgang Denk, wd@denx.de
12 #define DEBUG 1 /* general debug */
13 #define DEBUG_BOOTP_EXT 1 /* Debug received vendor fields */
16 #ifdef DEBUG_BOOTP_EXT
17 #define debug_ext(fmt,args...) printf (fmt ,##args)
19 #define debug_ext(fmt,args...)
28 #ifdef CONFIG_STATUS_LED
29 #include <status_led.h>
32 #define BOOTP_VENDOR_MAGIC 0x63825363 /* RFC1048 Magic Cookie */
34 #if defined(CONFIG_CMD_NET)
36 #define TIMEOUT 5UL /* Seconds before trying BOOTP again */
37 #ifndef CONFIG_NET_RETRY_COUNT
38 # define TIMEOUT_COUNT 5 /* # of timeouts before giving up */
40 # define TIMEOUT_COUNT (CONFIG_NET_RETRY_COUNT)
43 #define PORT_BOOTPS 67 /* BOOTP server UDP port */
44 #define PORT_BOOTPC 68 /* BOOTP client UDP port */
46 #ifndef CONFIG_DHCP_MIN_EXT_LEN /* minimal length of extension list */
47 #define CONFIG_DHCP_MIN_EXT_LEN 64
52 #ifdef CONFIG_BOOTP_RANDOM_DELAY
56 #if defined(CONFIG_CMD_DHCP)
57 dhcp_state_t dhcp_state = INIT;
58 unsigned long dhcp_leasetime = 0;
59 IPaddr_t NetDHCPServerIP = 0;
60 static void DhcpHandler(uchar * pkt, unsigned dest, unsigned src, unsigned len);
64 static char *dhcpmsg2str(int type)
67 case 1: return "DHCPDISCOVER"; break;
68 case 2: return "DHCPOFFER"; break;
69 case 3: return "DHCPREQUEST"; break;
70 case 4: return "DHCPDECLINE"; break;
71 case 5: return "DHCPACK"; break;
72 case 6: return "DHCPNACK"; break;
73 case 7: return "DHCPRELEASE"; break;
74 default: return "UNKNOWN/INVALID MSG TYPE"; break;
79 #if defined(CONFIG_BOOTP_VENDOREX)
80 extern u8 *dhcp_vendorex_prep (u8 *e); /*rtn new e after add own opts. */
81 extern u8 *dhcp_vendorex_proc (u8 *e); /*rtn next e if mine,else NULL */
86 static int BootpCheckPkt(uchar *pkt, unsigned dest, unsigned src, unsigned len)
88 Bootp_t *bp = (Bootp_t *) pkt;
91 if (dest != PORT_BOOTPC || src != PORT_BOOTPS)
93 else if (len < sizeof (Bootp_t) - OPT_SIZE)
95 else if (bp->bp_op != OP_BOOTREQUEST &&
96 bp->bp_op != OP_BOOTREPLY &&
97 bp->bp_op != DHCP_OFFER &&
98 bp->bp_op != DHCP_ACK &&
99 bp->bp_op != DHCP_NAK ) {
102 else if (bp->bp_htype != HWT_ETHER)
104 else if (bp->bp_hlen != HWL_ETHER)
106 else if (NetReadLong((ulong*)&bp->bp_id) != BootpID) {
110 debug ("Filtering pkt = %d\n", retval);
116 * Copy parameters of interest from BOOTP_REPLY/DHCP_OFFER packet
118 static void BootpCopyNetParams(Bootp_t *bp)
122 NetCopyIP(&NetOurIP, &bp->bp_yiaddr);
123 #if !defined(CONFIG_BOOTP_SERVERIP)
124 NetCopyIP(&tmp_ip, &bp->bp_siaddr);
126 NetCopyIP(&NetServerIP, &bp->bp_siaddr);
127 memcpy (NetServerEther, ((Ethernet_t *)NetRxPkt)->et_src, 6);
129 if (strlen(bp->bp_file) > 0)
130 copy_filename (BootFile, bp->bp_file, sizeof(BootFile));
132 debug ("Bootfile: %s\n", BootFile);
134 /* Propagate to environment:
135 * don't delete exising entry when BOOTP / DHCP reply does
136 * not contain a new value
139 setenv ("bootfile", BootFile);
143 static int truncate_sz (const char *name, int maxlen, int curlen)
145 if (curlen >= maxlen) {
146 printf("*** WARNING: %s is too long (%d - max: %d) - truncated\n",
147 name, curlen, maxlen);
153 #if !defined(CONFIG_CMD_DHCP)
155 static void BootpVendorFieldProcess (u8 * ext)
157 int size = *(ext + 1);
159 debug_ext ("[BOOTP] Processing extension %d... (%d bytes)\n", *ext,
165 /* Fixed length fields */
166 case 1: /* Subnet mask */
167 if (NetOurSubnetMask == 0)
168 NetCopyIP (&NetOurSubnetMask, (IPaddr_t *) (ext + 2));
170 case 2: /* Time offset - Not yet supported */
172 /* Variable length fields */
173 case 3: /* Gateways list */
174 if (NetOurGatewayIP == 0) {
175 NetCopyIP (&NetOurGatewayIP, (IPaddr_t *) (ext + 2));
178 case 4: /* Time server - Not yet supported */
180 case 5: /* IEN-116 name server - Not yet supported */
183 if (NetOurDNSIP == 0) {
184 NetCopyIP (&NetOurDNSIP, (IPaddr_t *) (ext + 2));
186 #if defined(CONFIG_BOOTP_DNS2)
187 if ((NetOurDNS2IP == 0) && (size > 4)) {
188 NetCopyIP (&NetOurDNS2IP, (IPaddr_t *) (ext + 2 + 4));
192 case 7: /* Log server - Not yet supported */
194 case 8: /* Cookie/Quote server - Not yet supported */
196 case 9: /* LPR server - Not yet supported */
198 case 10: /* Impress server - Not yet supported */
200 case 11: /* RPL server - Not yet supported */
202 case 12: /* Host name */
203 if (NetOurHostName[0] == 0) {
204 size = truncate_sz ("Host Name", sizeof (NetOurHostName), size);
205 memcpy (&NetOurHostName, ext + 2, size);
206 NetOurHostName[size] = 0;
209 case 13: /* Boot file size */
211 NetBootFileSize = ntohs (*(ushort *) (ext + 2));
213 NetBootFileSize = ntohl (*(ulong *) (ext + 2));
215 case 14: /* Merit dump file - Not yet supported */
217 case 15: /* Domain name - Not yet supported */
219 case 16: /* Swap server - Not yet supported */
221 case 17: /* Root path */
222 if (NetOurRootPath[0] == 0) {
223 size = truncate_sz ("Root Path", sizeof (NetOurRootPath), size);
224 memcpy (&NetOurRootPath, ext + 2, size);
225 NetOurRootPath[size] = 0;
228 case 18: /* Extension path - Not yet supported */
230 * This can be used to send the information of the
231 * vendor area in another file that the client can
235 /* IP host layer fields */
236 case 40: /* NIS Domain name */
237 if (NetOurNISDomain[0] == 0) {
238 size = truncate_sz ("NIS Domain Name", sizeof (NetOurNISDomain), size);
239 memcpy (&NetOurNISDomain, ext + 2, size);
240 NetOurNISDomain[size] = 0;
243 /* Application layer fields */
244 case 43: /* Vendor specific info - Not yet supported */
246 * Binary information to exchange specific
247 * product information.
250 /* Reserved (custom) fields (128..254) */
254 static void BootpVendorProcess (u8 * ext, int size)
256 u8 *end = ext + size;
258 debug_ext ("[BOOTP] Checking extension (%d bytes)...\n", size);
260 while ((ext < end) && (*ext != 0xff)) {
268 BootpVendorFieldProcess (opt);
272 #ifdef DEBUG_BOOTP_EXT
273 puts ("[BOOTP] Received fields: \n");
274 if (NetOurSubnetMask) {
275 puts ("NetOurSubnetMask : ");
276 print_IPaddr (NetOurSubnetMask);
280 if (NetOurGatewayIP) {
281 puts ("NetOurGatewayIP : ");
282 print_IPaddr (NetOurGatewayIP);
286 if (NetBootFileSize) {
287 printf ("NetBootFileSize : %d\n", NetBootFileSize);
290 if (NetOurHostName[0]) {
291 printf ("NetOurHostName : %s\n", NetOurHostName);
294 if (NetOurRootPath[0]) {
295 printf ("NetOurRootPath : %s\n", NetOurRootPath);
298 if (NetOurNISDomain[0]) {
299 printf ("NetOurNISDomain : %s\n", NetOurNISDomain);
302 if (NetBootFileSize) {
303 printf ("NetBootFileSize: %d\n", NetBootFileSize);
305 #endif /* DEBUG_BOOTP_EXT */
308 * Handle a BOOTP received packet.
311 BootpHandler(uchar * pkt, unsigned dest, unsigned src, unsigned len)
316 debug ("got BOOTP packet (src=%d, dst=%d, len=%d want_len=%zu)\n",
317 src, dest, len, sizeof (Bootp_t));
321 if (BootpCheckPkt(pkt, dest, src, len)) /* Filter out pkts we don't want */
325 * Got a good BOOTP reply. Copy the data into our variables.
327 #ifdef CONFIG_STATUS_LED
328 status_led_set (STATUS_LED_BOOT, STATUS_LED_OFF);
331 BootpCopyNetParams(bp); /* Store net parameters from reply */
333 /* Retrieve extended information (we must parse the vendor area) */
334 if (NetReadLong((ulong*)&bp->bp_vend[0]) == htonl(BOOTP_VENDOR_MAGIC))
335 BootpVendorProcess((uchar *)&bp->bp_vend[4], len);
337 NetSetTimeout(0, (thand_f *)0);
339 debug ("Got good BOOTP\n");
341 if ((s = getenv("autoload")) != NULL) {
344 * Just use BOOTP to configure system;
345 * Do not use TFTP to load the bootfile.
347 NetState = NETLOOP_SUCCESS;
349 #if defined(CONFIG_CMD_NFS)
350 } else if (strcmp(s, "NFS") == 0) {
352 * Use NFS to load the bootfile.
365 * Timeout on BOOTP/DHCP request.
370 if (BootpTry >= TIMEOUT_COUNT) {
371 puts ("\nRetry count exceeded; starting again\n");
374 NetSetTimeout (TIMEOUT * CFG_HZ, BootpTimeout);
380 * Initialize BOOTP extension fields in the request.
382 #if defined(CONFIG_CMD_DHCP)
383 static int DhcpExtended (u8 * e, int message_type, IPaddr_t ServerID, IPaddr_t RequestedIP)
388 #if defined(CONFIG_BOOTP_VENDOREX)
391 #if defined(CONFIG_BOOTP_SEND_HOSTNAME)
395 *e++ = 99; /* RFC1048 Magic Cookie */
400 *e++ = 53; /* DHCP Message Type */
404 *e++ = 57; /* Maximum DHCP Message Size */
406 *e++ = (576 - 312 + OPT_SIZE) >> 8;
407 *e++ = (576 - 312 + OPT_SIZE) & 0xff;
410 int tmp = ntohl (ServerID);
412 *e++ = 54; /* ServerID */
421 int tmp = ntohl (RequestedIP);
423 *e++ = 50; /* Requested IP */
430 #if defined(CONFIG_BOOTP_SEND_HOSTNAME)
431 if ((hostname = getenv ("hostname"))) {
432 int hostnamelen = strlen (hostname);
434 *e++ = 12; /* Hostname */
436 memcpy (e, hostname, hostnamelen);
441 #if defined(CONFIG_BOOTP_VENDOREX)
442 if ((x = dhcp_vendorex_prep (e)))
446 *e++ = 55; /* Parameter Request List */
447 cnt = e++; /* Pointer to count of requested items */
449 #if defined(CONFIG_BOOTP_SUBNETMASK)
450 *e++ = 1; /* Subnet Mask */
453 #if defined(CONFIG_BOOTP_TIMEOFFSET)
457 #if defined(CONFIG_BOOTP_GATEWAY)
458 *e++ = 3; /* Router Option */
461 #if defined(CONFIG_BOOTP_DNS)
462 *e++ = 6; /* DNS Server(s) */
465 #if defined(CONFIG_BOOTP_HOSTNAME)
466 *e++ = 12; /* Hostname */
469 #if defined(CONFIG_BOOTP_BOOTFILESIZE)
470 *e++ = 13; /* Boot File Size */
473 #if defined(CONFIG_BOOTP_BOOTPATH)
474 *e++ = 17; /* Boot path */
477 #if defined(CONFIG_BOOTP_NISDOMAIN)
478 *e++ = 40; /* NIS Domain name request */
481 #if defined(CONFIG_BOOTP_NTPSERVER)
485 *e++ = 255; /* End of the list */
487 /* Pad to minimal length */
488 #ifdef CONFIG_DHCP_MIN_EXT_LEN
489 while ((e - start) <= CONFIG_DHCP_MIN_EXT_LEN)
498 * Warning: no field size check - change CONFIG_BOOTP_* at your own risk!
500 static int BootpExtended (u8 * e)
504 *e++ = 99; /* RFC1048 Magic Cookie */
509 #if defined(CONFIG_CMD_DHCP)
510 *e++ = 53; /* DHCP Message Type */
512 *e++ = DHCP_DISCOVER;
514 *e++ = 57; /* Maximum DHCP Message Size */
516 *e++ = (576 - 312 + OPT_SIZE) >> 16;
517 *e++ = (576 - 312 + OPT_SIZE) & 0xff;
520 #if defined(CONFIG_BOOTP_SUBNETMASK)
521 *e++ = 1; /* Subnet mask request */
526 #if defined(CONFIG_BOOTP_GATEWAY)
527 *e++ = 3; /* Default gateway request */
532 #if defined(CONFIG_BOOTP_DNS)
533 *e++ = 6; /* Domain Name Server */
538 #if defined(CONFIG_BOOTP_HOSTNAME)
539 *e++ = 12; /* Host name request */
544 #if defined(CONFIG_BOOTP_BOOTFILESIZE)
545 *e++ = 13; /* Boot file size */
550 #if defined(CONFIG_BOOTP_BOOTPATH)
551 *e++ = 17; /* Boot path */
556 #if defined(CONFIG_BOOTP_NISDOMAIN)
557 *e++ = 40; /* NIS Domain name request */
562 *e++ = 255; /* End of the list */
571 volatile uchar *pkt, *iphdr;
573 int ext_len, pktlen, iplen;
575 #if defined(CONFIG_CMD_DHCP)
579 #ifdef CONFIG_BOOTP_RANDOM_DELAY /* Random BOOTP delay */
580 unsigned char bi_enetaddr[6];
584 ulong tst1, tst2, sum, m_mask, m_value = 0;
588 reg = getenv_r ("ethaddr", tmp, sizeof(tmp));
589 s = (reg > 0) ? tmp : NULL;
591 for (reg=0; reg<6; ++reg) {
592 bi_enetaddr[reg] = s ? simple_strtoul(s, &e, 16) : 0;
598 puts ("BootpRequest => Our Mac: ");
599 for (reg=0; reg<6; reg++) {
602 reg==5 ? '\n' : ':');
606 /* Mac-Manipulation 2 get seed1 */
609 for (reg=2; reg<6; reg++) {
611 tst1 = tst1 | bi_enetaddr[reg];
613 for (reg=0; reg<2; reg++) {
614 tst2 = tst2 | bi_enetaddr[reg];
622 for (reg=1;reg<=32;reg++) {
623 m_value |= (m_mask & seed1);
625 m_value = m_value << 1;
631 /* Random Number Generator */
633 for (reg=0;reg<=0;reg++) {
635 if (sum < seed1 || sum < seed2)
640 if (BootpTry<=2) { /* Start with max 1024 * 1ms */
641 sum = sum >> (22-BootpTry);
642 } else { /*After 3rd BOOTP request max 8192 * 1ms */
647 printf ("Random delay: %ld ms...\n", sum);
648 for (reg=0; reg <sum; reg++) {
649 udelay(1000); /*Wait 1ms*/
651 #endif /* CONFIG_BOOTP_RANDOM_DELAY */
653 printf("BOOTP broadcast %d\n", ++BootpTry);
655 memset ((void*)pkt, 0, PKTSIZE);
657 pkt += NetSetEther(pkt, NetBcastAddr, PROT_IP);
660 * Next line results in incorrect packet size being transmitted, resulting
661 * in errors in some DHCP servers, reporting missing bytes. Size must be
662 * set in packet header after extension length has been determined.
663 * C. Hallinan, DS4.COM, Inc.
665 /* NetSetIP(pkt, 0xFFFFFFFFL, PORT_BOOTPS, PORT_BOOTPC, sizeof (Bootp_t)); */
666 iphdr = pkt; /* We need this later for NetSetIP() */
670 bp->bp_op = OP_BOOTREQUEST;
671 bp->bp_htype = HWT_ETHER;
672 bp->bp_hlen = HWL_ETHER;
674 bp->bp_secs = htons(get_timer(0) / CFG_HZ);
675 NetWriteIP(&bp->bp_ciaddr, 0);
676 NetWriteIP(&bp->bp_yiaddr, 0);
677 NetWriteIP(&bp->bp_siaddr, 0);
678 NetWriteIP(&bp->bp_giaddr, 0);
679 memcpy (bp->bp_chaddr, NetOurEther, 6);
680 copy_filename (bp->bp_file, BootFile, sizeof(bp->bp_file));
682 /* Request additional information from the BOOTP/DHCP server */
683 #if defined(CONFIG_CMD_DHCP)
684 ext_len = DhcpExtended((u8 *)bp->bp_vend, DHCP_DISCOVER, 0, 0);
686 ext_len = BootpExtended((u8 *)bp->bp_vend);
690 * Bootp ID is the lower 4 bytes of our ethernet address
691 * plus the current time in HZ.
693 BootpID = ((ulong)NetOurEther[2] << 24)
694 | ((ulong)NetOurEther[3] << 16)
695 | ((ulong)NetOurEther[4] << 8)
696 | (ulong)NetOurEther[5];
697 BootpID += get_timer(0);
698 BootpID = htonl(BootpID);
699 NetCopyLong(&bp->bp_id, &BootpID);
702 * Calculate proper packet lengths taking into account the
703 * variable size of the options field
705 pktlen = BOOTP_SIZE - sizeof(bp->bp_vend) + ext_len;
706 iplen = BOOTP_HDR_SIZE - sizeof(bp->bp_vend) + ext_len;
707 NetSetIP(iphdr, 0xFFFFFFFFL, PORT_BOOTPS, PORT_BOOTPC, iplen);
708 NetSetTimeout(SELECT_TIMEOUT * CFG_HZ, BootpTimeout);
710 #if defined(CONFIG_CMD_DHCP)
711 dhcp_state = SELECTING;
712 NetSetHandler(DhcpHandler);
714 NetSetHandler(BootpHandler);
716 NetSendPacket(NetTxPacket, pktlen);
719 #if defined(CONFIG_CMD_DHCP)
720 static void DhcpOptionsProcess (uchar * popt, Bootp_t *bp)
722 uchar *end = popt + BOOTP_HDR_SIZE;
725 while (popt < end && *popt != 0xff) {
729 NetCopyIP (&NetOurSubnetMask, (popt + 2));
731 #if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_TIMEOFFSET)
732 case 2: /* Time offset */
733 NetCopyLong ((ulong *)&NetTimeOffset, (ulong *) (popt + 2));
734 NetTimeOffset = ntohl (NetTimeOffset);
738 NetCopyIP (&NetOurGatewayIP, (popt + 2));
741 NetCopyIP (&NetOurDNSIP, (popt + 2));
742 #if defined(CONFIG_BOOTP_DNS2)
743 if (*(popt + 1) > 4) {
744 NetCopyIP (&NetOurDNS2IP, (popt + 2 + 4));
749 size = truncate_sz ("Host Name", sizeof (NetOurHostName), oplen);
750 memcpy (&NetOurHostName, popt + 2, size);
751 NetOurHostName[size] = 0;
753 case 15: /* Ignore Domain Name Option */
756 size = truncate_sz ("Root Path", sizeof (NetOurRootPath), oplen);
757 memcpy (&NetOurRootPath, popt + 2, size);
758 NetOurRootPath[size] = 0;
760 #if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_NTPSERVER)
761 case 42: /* NTP server IP */
762 NetCopyIP (&NetNtpServerIP, (popt + 2));
766 NetCopyLong (&dhcp_leasetime, (ulong *) (popt + 2));
768 case 53: /* Ignore Message Type Option */
771 NetCopyIP (&NetDHCPServerIP, (popt + 2));
773 case 58: /* Ignore Renewal Time Option */
775 case 59: /* Ignore Rebinding Time Option */
777 case 66: /* Ignore TFTP server name */
779 case 67: /* vendor opt bootfile */
781 * I can't use dhcp_vendorex_proc here because I need
782 * to write into the bootp packet - even then I had to
783 * pass the bootp packet pointer into here as the
786 size = truncate_sz ("Opt Boot File",
789 if (bp->bp_file[0] == '\0' && size > 0) {
791 * only use vendor boot file if we didn't
792 * receive a boot file in the main non-vendor
793 * part of the packet - god only knows why
794 * some vendors chose not to use this perfectly
795 * good spot to store the boot file (join on
796 * Tru64 Unix) it seems mind bogglingly crazy
799 printf("*** WARNING: using vendor "
800 "optional boot file\n");
801 memcpy(bp->bp_file, popt + 2, size);
802 bp->bp_file[size] = '\0';
806 #if defined(CONFIG_BOOTP_VENDOREX)
807 if (dhcp_vendorex_proc (popt))
810 printf ("*** Unhandled DHCP Option in OFFER/ACK: %d\n", *popt);
813 popt += oplen + 2; /* Process next option */
817 static int DhcpMessageType(unsigned char *popt)
819 if (NetReadLong((ulong*)popt) != htonl(BOOTP_VENDOR_MAGIC))
823 while ( *popt != 0xff ) {
824 if ( *popt == 53 ) /* DHCP Message Type */
826 popt += *(popt + 1) + 2; /* Scan through all options */
831 static void DhcpSendRequestPkt(Bootp_t *bp_offer)
833 volatile uchar *pkt, *iphdr;
835 int pktlen, iplen, extlen;
838 debug ("DhcpSendRequestPkt: Sending DHCPREQUEST\n");
840 memset ((void*)pkt, 0, PKTSIZE);
842 pkt += NetSetEther(pkt, NetBcastAddr, PROT_IP);
844 iphdr = pkt; /* We'll need this later to set proper pkt size */
848 bp->bp_op = OP_BOOTREQUEST;
849 bp->bp_htype = HWT_ETHER;
850 bp->bp_hlen = HWL_ETHER;
852 bp->bp_secs = htons(get_timer(0) / CFG_HZ);
853 /* Do not set the client IP, your IP, or server IP yet, since it hasn't been ACK'ed by
857 * RFC3046 requires Relay Agents to discard packets with
858 * nonzero and offered giaddr
860 NetWriteIP(&bp->bp_giaddr, 0);
862 memcpy (bp->bp_chaddr, NetOurEther, 6);
865 * ID is the id of the OFFER packet
868 NetCopyLong(&bp->bp_id, &bp_offer->bp_id);
871 * Copy options from OFFER packet if present
874 /* Copy offered IP into the parameters request list */
875 NetCopyIP(&OfferedIP, &bp_offer->bp_yiaddr);
876 extlen = DhcpExtended((u8 *)bp->bp_vend, DHCP_REQUEST, NetDHCPServerIP, OfferedIP);
878 pktlen = BOOTP_SIZE - sizeof(bp->bp_vend) + extlen;
879 iplen = BOOTP_HDR_SIZE - sizeof(bp->bp_vend) + extlen;
880 NetSetIP(iphdr, 0xFFFFFFFFL, PORT_BOOTPS, PORT_BOOTPC, iplen);
882 debug ("Transmitting DHCPREQUEST packet: len = %d\n", pktlen);
883 #ifdef CONFIG_BOOTP_DHCP_REQUEST_DELAY
884 udelay(CONFIG_BOOTP_DHCP_REQUEST_DELAY);
885 #endif /* CONFIG_BOOTP_DHCP_REQUEST_DELAY */
886 NetSendPacket(NetTxPacket, pktlen);
890 * Handle DHCP received packets.
893 DhcpHandler(uchar * pkt, unsigned dest, unsigned src, unsigned len)
895 Bootp_t *bp = (Bootp_t *)pkt;
897 debug ("DHCPHandler: got packet: (src=%d, dst=%d, len=%d) state: %d\n",
898 src, dest, len, dhcp_state);
900 if (BootpCheckPkt(pkt, dest, src, len)) /* Filter out pkts we don't want */
903 debug ("DHCPHandler: got DHCP packet: (src=%d, dst=%d, len=%d) state: %d\n",
904 src, dest, len, dhcp_state);
906 switch (dhcp_state) {
909 * Wait an appropriate time for any potential DHCPOFFER packets
910 * to arrive. Then select one, and generate DHCPREQUEST response.
911 * If filename is in format we recognize, assume it is a valid
912 * OFFER from a server we want.
914 debug ("DHCP: state=SELECTING bp_file: \"%s\"\n", bp->bp_file);
915 #ifdef CFG_BOOTFILE_PREFIX
916 if (strncmp(bp->bp_file,
918 strlen(CFG_BOOTFILE_PREFIX)) == 0 ) {
919 #endif /* CFG_BOOTFILE_PREFIX */
921 debug ("TRANSITIONING TO REQUESTING STATE\n");
922 dhcp_state = REQUESTING;
924 if (NetReadLong((ulong*)&bp->bp_vend[0]) == htonl(BOOTP_VENDOR_MAGIC))
925 DhcpOptionsProcess((u8 *)&bp->bp_vend[4], bp);
927 NetSetTimeout(TIMEOUT * CFG_HZ, BootpTimeout);
928 DhcpSendRequestPkt(bp);
929 #ifdef CFG_BOOTFILE_PREFIX
931 #endif /* CFG_BOOTFILE_PREFIX */
936 debug ("DHCP State: REQUESTING\n");
938 if ( DhcpMessageType((u8 *)bp->bp_vend) == DHCP_ACK ) {
941 if (NetReadLong((ulong*)&bp->bp_vend[0]) == htonl(BOOTP_VENDOR_MAGIC))
942 DhcpOptionsProcess((u8 *)&bp->bp_vend[4], bp);
943 BootpCopyNetParams(bp); /* Store net params from reply */
945 puts ("DHCP client bound to address ");
946 print_IPaddr(NetOurIP);
949 /* Obey the 'autoload' setting */
950 if ((s = getenv("autoload")) != NULL) {
953 * Just use BOOTP to configure system;
954 * Do not use TFTP to load the bootfile.
956 NetState = NETLOOP_SUCCESS;
958 #if defined(CONFIG_CMD_NFS)
959 } else if (strcmp(s, "NFS") == 0) {
961 * Use NFS to load the bootfile.
973 /* DHCP client bound to address */
976 puts ("DHCP: INVALID STATE\n");
982 void DhcpRequest(void)
986 #endif /* CONFIG_CMD_DHCP */
988 #endif /* CONFIG_CMD_NET */