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
17 #ifdef CONFIG_STATUS_LED
18 #include <status_led.h>
20 #ifdef CONFIG_BOOTP_RANDOM_DELAY
24 #define BOOTP_VENDOR_MAGIC 0x63825363 /* RFC1048 Magic Cookie */
26 #define TIMEOUT 5000UL /* Milliseconds before trying BOOTP again */
27 #ifndef CONFIG_NET_RETRY_COUNT
28 # define TIMEOUT_COUNT 5 /* # of timeouts before giving up */
30 # define TIMEOUT_COUNT (CONFIG_NET_RETRY_COUNT)
33 #define PORT_BOOTPS 67 /* BOOTP server UDP port */
34 #define PORT_BOOTPC 68 /* BOOTP client UDP port */
36 #ifndef CONFIG_DHCP_MIN_EXT_LEN /* minimal length of extension list */
37 #define CONFIG_DHCP_MIN_EXT_LEN 64
43 #if defined(CONFIG_CMD_DHCP)
44 dhcp_state_t dhcp_state = INIT;
45 unsigned long dhcp_leasetime;
46 IPaddr_t NetDHCPServerIP;
47 static void DhcpHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src,
52 static char *dhcpmsg2str(int type)
55 case 1: return "DHCPDISCOVER"; break;
56 case 2: return "DHCPOFFER"; break;
57 case 3: return "DHCPREQUEST"; break;
58 case 4: return "DHCPDECLINE"; break;
59 case 5: return "DHCPACK"; break;
60 case 6: return "DHCPNACK"; break;
61 case 7: return "DHCPRELEASE"; break;
62 default: return "UNKNOWN/INVALID MSG TYPE"; break;
68 static int BootpCheckPkt(uchar *pkt, unsigned dest, unsigned src, unsigned len)
70 struct Bootp_t *bp = (struct Bootp_t *) pkt;
73 if (dest != PORT_BOOTPC || src != PORT_BOOTPS)
75 else if (len < sizeof(struct Bootp_t) - OPT_FIELD_SIZE)
77 else if (bp->bp_op != OP_BOOTREQUEST &&
78 bp->bp_op != OP_BOOTREPLY &&
79 bp->bp_op != DHCP_OFFER &&
80 bp->bp_op != DHCP_ACK &&
81 bp->bp_op != DHCP_NAK)
83 else if (bp->bp_htype != HWT_ETHER)
85 else if (bp->bp_hlen != HWL_ETHER)
87 else if (NetReadLong((ulong *)&bp->bp_id) != BootpID)
90 debug("Filtering pkt = %d\n", retval);
96 * Copy parameters of interest from BOOTP_REPLY/DHCP_OFFER packet
98 static void BootpCopyNetParams(struct Bootp_t *bp)
100 #if !defined(CONFIG_BOOTP_SERVERIP)
103 NetCopyIP(&tmp_ip, &bp->bp_siaddr);
105 NetCopyIP(&NetServerIP, &bp->bp_siaddr);
106 memcpy(NetServerEther, ((struct ethernet_hdr *)NetRxPacket)->et_src, 6);
108 NetCopyIP(&NetOurIP, &bp->bp_yiaddr);
109 if (strlen(bp->bp_file) > 0)
110 copy_filename(BootFile, bp->bp_file, sizeof(BootFile));
112 debug("Bootfile: %s\n", BootFile);
114 /* Propagate to environment:
115 * don't delete exising entry when BOOTP / DHCP reply does
116 * not contain a new value
119 setenv("bootfile", BootFile);
122 static int truncate_sz(const char *name, int maxlen, int curlen)
124 if (curlen >= maxlen) {
125 printf("*** WARNING: %s is too long (%d - max: %d)"
126 " - truncated\n", name, curlen, maxlen);
132 #if !defined(CONFIG_CMD_DHCP)
134 static void BootpVendorFieldProcess(u8 *ext)
136 int size = *(ext + 1);
138 debug("[BOOTP] Processing extension %d... (%d bytes)\n", *ext,
144 /* Fixed length fields */
145 case 1: /* Subnet mask */
146 if (NetOurSubnetMask == 0)
147 NetCopyIP(&NetOurSubnetMask, (IPaddr_t *) (ext + 2));
149 case 2: /* Time offset - Not yet supported */
151 /* Variable length fields */
152 case 3: /* Gateways list */
153 if (NetOurGatewayIP == 0)
154 NetCopyIP(&NetOurGatewayIP, (IPaddr_t *) (ext + 2));
156 case 4: /* Time server - Not yet supported */
158 case 5: /* IEN-116 name server - Not yet supported */
161 if (NetOurDNSIP == 0)
162 NetCopyIP(&NetOurDNSIP, (IPaddr_t *) (ext + 2));
163 #if defined(CONFIG_BOOTP_DNS2)
164 if ((NetOurDNS2IP == 0) && (size > 4))
165 NetCopyIP(&NetOurDNS2IP, (IPaddr_t *) (ext + 2 + 4));
168 case 7: /* Log server - Not yet supported */
170 case 8: /* Cookie/Quote server - Not yet supported */
172 case 9: /* LPR server - Not yet supported */
174 case 10: /* Impress server - Not yet supported */
176 case 11: /* RPL server - Not yet supported */
178 case 12: /* Host name */
179 if (NetOurHostName[0] == 0) {
180 size = truncate_sz("Host Name",
181 sizeof(NetOurHostName), size);
182 memcpy(&NetOurHostName, ext + 2, size);
183 NetOurHostName[size] = 0;
186 case 13: /* Boot file size */
188 NetBootFileSize = ntohs(*(ushort *) (ext + 2));
190 NetBootFileSize = ntohl(*(ulong *) (ext + 2));
192 case 14: /* Merit dump file - Not yet supported */
194 case 15: /* Domain name - Not yet supported */
196 case 16: /* Swap server - Not yet supported */
198 case 17: /* Root path */
199 if (NetOurRootPath[0] == 0) {
200 size = truncate_sz("Root Path",
201 sizeof(NetOurRootPath), size);
202 memcpy(&NetOurRootPath, ext + 2, size);
203 NetOurRootPath[size] = 0;
206 case 18: /* Extension path - Not yet supported */
208 * This can be used to send the information of the
209 * vendor area in another file that the client can
213 /* IP host layer fields */
214 case 40: /* NIS Domain name */
215 if (NetOurNISDomain[0] == 0) {
216 size = truncate_sz("NIS Domain Name",
217 sizeof(NetOurNISDomain), size);
218 memcpy(&NetOurNISDomain, ext + 2, size);
219 NetOurNISDomain[size] = 0;
222 #if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_NTPSERVER)
223 case 42: /* NTP server IP */
224 NetCopyIP(&NetNtpServerIP, (IPaddr_t *) (ext + 2));
227 /* Application layer fields */
228 case 43: /* Vendor specific info - Not yet supported */
230 * Binary information to exchange specific
231 * product information.
234 /* Reserved (custom) fields (128..254) */
238 static void BootpVendorProcess(u8 *ext, int size)
240 u8 *end = ext + size;
242 debug("[BOOTP] Checking extension (%d bytes)...\n", size);
244 while ((ext < end) && (*ext != 0xff)) {
252 BootpVendorFieldProcess(opt);
256 debug("[BOOTP] Received fields:\n");
257 if (NetOurSubnetMask)
258 debug("NetOurSubnetMask : %pI4\n", &NetOurSubnetMask);
261 debug("NetOurGatewayIP : %pI4", &NetOurGatewayIP);
264 debug("NetBootFileSize : %d\n", NetBootFileSize);
266 if (NetOurHostName[0])
267 debug("NetOurHostName : %s\n", NetOurHostName);
269 if (NetOurRootPath[0])
270 debug("NetOurRootPath : %s\n", NetOurRootPath);
272 if (NetOurNISDomain[0])
273 debug("NetOurNISDomain : %s\n", NetOurNISDomain);
276 debug("NetBootFileSize: %d\n", NetBootFileSize);
278 #if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_NTPSERVER)
280 debug("NetNtpServerIP : %pI4\n", &NetNtpServerIP);
285 * Handle a BOOTP received packet.
288 BootpHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src,
293 debug("got BOOTP packet (src=%d, dst=%d, len=%d want_len=%zu)\n",
294 src, dest, len, sizeof(struct Bootp_t));
296 bp = (struct Bootp_t *)pkt;
298 /* Filter out pkts we don't want */
299 if (BootpCheckPkt(pkt, dest, src, len))
303 * Got a good BOOTP reply. Copy the data into our variables.
305 #ifdef CONFIG_STATUS_LED
306 status_led_set(STATUS_LED_BOOT, STATUS_LED_OFF);
309 BootpCopyNetParams(bp); /* Store net parameters from reply */
311 /* Retrieve extended information (we must parse the vendor area) */
312 if (NetReadLong((ulong *)&bp->bp_vend[0]) == htonl(BOOTP_VENDOR_MAGIC))
313 BootpVendorProcess((uchar *)&bp->bp_vend[4], len);
315 NetSetTimeout(0, (thand_f *)0);
316 bootstage_mark_name(BOOTSTAGE_ID_BOOTP_STOP, "bootp_stop");
318 debug("Got good BOOTP\n");
325 * Timeout on BOOTP/DHCP request.
330 if (BootpTry >= TIMEOUT_COUNT) {
331 #ifdef CONFIG_BOOTP_MAY_FAIL
332 puts("\nRetry count exceeded\n");
333 NetSetState(NETLOOP_FAIL);
335 puts("\nRetry count exceeded; starting again\n");
339 NetSetTimeout(TIMEOUT, BootpTimeout);
345 * Initialize BOOTP extension fields in the request.
347 #if defined(CONFIG_CMD_DHCP)
348 static int DhcpExtended(u8 *e, int message_type, IPaddr_t ServerID,
349 IPaddr_t RequestedIP)
353 #if defined(CONFIG_BOOTP_PXE)
359 #if defined(CONFIG_BOOTP_VENDOREX)
362 #if defined(CONFIG_BOOTP_SEND_HOSTNAME)
366 *e++ = 99; /* RFC1048 Magic Cookie */
371 *e++ = 53; /* DHCP Message Type */
375 *e++ = 57; /* Maximum DHCP Message Size */
377 *e++ = (576 - 312 + OPT_FIELD_SIZE) >> 8;
378 *e++ = (576 - 312 + OPT_FIELD_SIZE) & 0xff;
381 int tmp = ntohl(ServerID);
383 *e++ = 54; /* ServerID */
392 int tmp = ntohl(RequestedIP);
394 *e++ = 50; /* Requested IP */
401 #if defined(CONFIG_BOOTP_SEND_HOSTNAME)
402 hostname = getenv("hostname");
404 int hostnamelen = strlen(hostname);
406 *e++ = 12; /* Hostname */
408 memcpy(e, hostname, hostnamelen);
413 #if defined(CONFIG_BOOTP_PXE)
414 clientarch = CONFIG_BOOTP_PXE_CLIENTARCH;
415 *e++ = 93; /* Client System Architecture */
417 *e++ = (clientarch >> 8) & 0xff;
418 *e++ = clientarch & 0xff;
420 *e++ = 94; /* Client Network Interface Identifier */
422 *e++ = 1; /* type field for UNDI */
423 *e++ = 0; /* major revision */
424 *e++ = 0; /* minor revision */
426 uuid = getenv("pxeuuid");
429 if (uuid_str_valid(uuid)) {
430 *e++ = 97; /* Client Machine Identifier */
432 *e++ = 0; /* type 0 - UUID */
434 uuid_str_to_bin(uuid, e);
437 printf("Invalid pxeuuid: %s\n", uuid);
441 *e++ = 60; /* Vendor Class Identifier */
442 vci_strlen = strlen(CONFIG_BOOTP_VCI_STRING);
444 memcpy(e, CONFIG_BOOTP_VCI_STRING, vci_strlen);
448 #if defined(CONFIG_BOOTP_VENDOREX)
449 x = dhcp_vendorex_prep(e);
454 *e++ = 55; /* Parameter Request List */
455 cnt = e++; /* Pointer to count of requested items */
457 #if defined(CONFIG_BOOTP_SUBNETMASK)
458 *e++ = 1; /* Subnet Mask */
461 #if defined(CONFIG_BOOTP_TIMEOFFSET)
465 #if defined(CONFIG_BOOTP_GATEWAY)
466 *e++ = 3; /* Router Option */
469 #if defined(CONFIG_BOOTP_DNS)
470 *e++ = 6; /* DNS Server(s) */
473 #if defined(CONFIG_BOOTP_HOSTNAME)
474 *e++ = 12; /* Hostname */
477 #if defined(CONFIG_BOOTP_BOOTFILESIZE)
478 *e++ = 13; /* Boot File Size */
481 #if defined(CONFIG_BOOTP_BOOTPATH)
482 *e++ = 17; /* Boot path */
485 #if defined(CONFIG_BOOTP_NISDOMAIN)
486 *e++ = 40; /* NIS Domain name request */
489 #if defined(CONFIG_BOOTP_NTPSERVER)
493 /* no options, so back up to avoid sending an empty request list */
497 *e++ = 255; /* End of the list */
499 /* Pad to minimal length */
500 #ifdef CONFIG_DHCP_MIN_EXT_LEN
501 while ((e - start) < CONFIG_DHCP_MIN_EXT_LEN)
510 * Warning: no field size check - change CONFIG_BOOTP_* at your own risk!
512 static int BootpExtended(u8 *e)
516 *e++ = 99; /* RFC1048 Magic Cookie */
521 #if defined(CONFIG_CMD_DHCP)
522 *e++ = 53; /* DHCP Message Type */
524 *e++ = DHCP_DISCOVER;
526 *e++ = 57; /* Maximum DHCP Message Size */
528 *e++ = (576 - 312 + OPT_FIELD_SIZE) >> 16;
529 *e++ = (576 - 312 + OPT_FIELD_SIZE) & 0xff;
532 #if defined(CONFIG_BOOTP_SUBNETMASK)
533 *e++ = 1; /* Subnet mask request */
538 #if defined(CONFIG_BOOTP_GATEWAY)
539 *e++ = 3; /* Default gateway request */
544 #if defined(CONFIG_BOOTP_DNS)
545 *e++ = 6; /* Domain Name Server */
550 #if defined(CONFIG_BOOTP_HOSTNAME)
551 *e++ = 12; /* Host name request */
556 #if defined(CONFIG_BOOTP_BOOTFILESIZE)
557 *e++ = 13; /* Boot file size */
562 #if defined(CONFIG_BOOTP_BOOTPATH)
563 *e++ = 17; /* Boot path */
568 #if defined(CONFIG_BOOTP_NISDOMAIN)
569 *e++ = 40; /* NIS Domain name request */
573 #if defined(CONFIG_BOOTP_NTPSERVER)
579 *e++ = 255; /* End of the list */
590 int extlen, pktlen, iplen;
592 #ifdef CONFIG_BOOTP_RANDOM_DELAY
596 bootstage_mark_name(BOOTSTAGE_ID_BOOTP_START, "bootp_start");
597 #if defined(CONFIG_CMD_DHCP)
601 #ifdef CONFIG_BOOTP_RANDOM_DELAY /* Random BOOTP delay */
605 if (BootpTry <= 2) /* Start with max 1024 * 1ms */
606 rand_ms = rand() >> (22 - BootpTry);
607 else /* After 3rd BOOTP request max 8192 * 1ms */
608 rand_ms = rand() >> 19;
610 printf("Random delay: %ld ms...\n", rand_ms);
611 for (i = 0; i < rand_ms; i++)
612 udelay(1000); /*Wait 1ms*/
614 #endif /* CONFIG_BOOTP_RANDOM_DELAY */
616 printf("BOOTP broadcast %d\n", ++BootpTry);
618 memset((void *)pkt, 0, PKTSIZE);
620 eth_hdr_size = NetSetEther(pkt, NetBcastAddr, PROT_IP);
624 * Next line results in incorrect packet size being transmitted,
625 * resulting in errors in some DHCP servers, reporting missing bytes.
626 * Size must be set in packet header after extension length has been
628 * C. Hallinan, DS4.COM, Inc.
630 /* net_set_udp_header(pkt, 0xFFFFFFFFL, PORT_BOOTPS, PORT_BOOTPC,
631 sizeof (struct Bootp_t)); */
632 iphdr = pkt; /* We need this later for net_set_udp_header() */
633 pkt += IP_UDP_HDR_SIZE;
635 bp = (struct Bootp_t *)pkt;
636 bp->bp_op = OP_BOOTREQUEST;
637 bp->bp_htype = HWT_ETHER;
638 bp->bp_hlen = HWL_ETHER;
640 bp->bp_secs = htons(get_timer(0) / 1000);
641 NetWriteIP(&bp->bp_ciaddr, 0);
642 NetWriteIP(&bp->bp_yiaddr, 0);
643 NetWriteIP(&bp->bp_siaddr, 0);
644 NetWriteIP(&bp->bp_giaddr, 0);
645 memcpy(bp->bp_chaddr, NetOurEther, 6);
646 copy_filename(bp->bp_file, BootFile, sizeof(bp->bp_file));
648 /* Request additional information from the BOOTP/DHCP server */
649 #if defined(CONFIG_CMD_DHCP)
650 extlen = DhcpExtended((u8 *)bp->bp_vend, DHCP_DISCOVER, 0, 0);
652 extlen = BootpExtended((u8 *)bp->bp_vend);
656 * Bootp ID is the lower 4 bytes of our ethernet address
657 * plus the current time in ms.
659 BootpID = ((ulong)NetOurEther[2] << 24)
660 | ((ulong)NetOurEther[3] << 16)
661 | ((ulong)NetOurEther[4] << 8)
662 | (ulong)NetOurEther[5];
663 BootpID += get_timer(0);
664 BootpID = htonl(BootpID);
665 NetCopyLong(&bp->bp_id, &BootpID);
668 * Calculate proper packet lengths taking into account the
669 * variable size of the options field
671 iplen = BOOTP_HDR_SIZE - OPT_FIELD_SIZE + extlen;
672 pktlen = eth_hdr_size + IP_UDP_HDR_SIZE + iplen;
673 net_set_udp_header(iphdr, 0xFFFFFFFFL, PORT_BOOTPS, PORT_BOOTPC, iplen);
674 NetSetTimeout(SELECT_TIMEOUT, BootpTimeout);
676 #if defined(CONFIG_CMD_DHCP)
677 dhcp_state = SELECTING;
678 net_set_udp_handler(DhcpHandler);
680 net_set_udp_handler(BootpHandler);
682 NetSendPacket(NetTxPacket, pktlen);
685 #if defined(CONFIG_CMD_DHCP)
686 static void DhcpOptionsProcess(uchar *popt, struct Bootp_t *bp)
688 uchar *end = popt + BOOTP_HDR_SIZE;
690 #if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_TIMEOFFSET)
694 while (popt < end && *popt != 0xff) {
698 NetCopyIP(&NetOurSubnetMask, (popt + 2));
700 #if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_TIMEOFFSET)
701 case 2: /* Time offset */
702 to_ptr = &NetTimeOffset;
703 NetCopyLong((ulong *)to_ptr, (ulong *)(popt + 2));
704 NetTimeOffset = ntohl(NetTimeOffset);
708 NetCopyIP(&NetOurGatewayIP, (popt + 2));
711 NetCopyIP(&NetOurDNSIP, (popt + 2));
712 #if defined(CONFIG_BOOTP_DNS2)
714 NetCopyIP(&NetOurDNS2IP, (popt + 2 + 4));
718 size = truncate_sz("Host Name",
719 sizeof(NetOurHostName), oplen);
720 memcpy(&NetOurHostName, popt + 2, size);
721 NetOurHostName[size] = 0;
723 case 15: /* Ignore Domain Name Option */
726 size = truncate_sz("Root Path",
727 sizeof(NetOurRootPath), oplen);
728 memcpy(&NetOurRootPath, popt + 2, size);
729 NetOurRootPath[size] = 0;
731 #if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_NTPSERVER)
732 case 42: /* NTP server IP */
733 NetCopyIP(&NetNtpServerIP, (popt + 2));
737 NetCopyLong(&dhcp_leasetime, (ulong *) (popt + 2));
739 case 53: /* Ignore Message Type Option */
742 NetCopyIP(&NetDHCPServerIP, (popt + 2));
744 case 58: /* Ignore Renewal Time Option */
746 case 59: /* Ignore Rebinding Time Option */
748 case 66: /* Ignore TFTP server name */
750 case 67: /* vendor opt bootfile */
752 * I can't use dhcp_vendorex_proc here because I need
753 * to write into the bootp packet - even then I had to
754 * pass the bootp packet pointer into here as the
757 size = truncate_sz("Opt Boot File",
760 if (bp->bp_file[0] == '\0' && size > 0) {
762 * only use vendor boot file if we didn't
763 * receive a boot file in the main non-vendor
764 * part of the packet - god only knows why
765 * some vendors chose not to use this perfectly
766 * good spot to store the boot file (join on
767 * Tru64 Unix) it seems mind bogglingly crazy
770 printf("*** WARNING: using vendor "
771 "optional boot file\n");
772 memcpy(bp->bp_file, popt + 2, size);
773 bp->bp_file[size] = '\0';
777 #if defined(CONFIG_BOOTP_VENDOREX)
778 if (dhcp_vendorex_proc(popt))
781 printf("*** Unhandled DHCP Option in OFFER/ACK:"
785 popt += oplen + 2; /* Process next option */
789 static int DhcpMessageType(unsigned char *popt)
791 if (NetReadLong((ulong *)popt) != htonl(BOOTP_VENDOR_MAGIC))
795 while (*popt != 0xff) {
796 if (*popt == 53) /* DHCP Message Type */
798 popt += *(popt + 1) + 2; /* Scan through all options */
803 static void DhcpSendRequestPkt(struct Bootp_t *bp_offer)
807 int pktlen, iplen, extlen;
811 debug("DhcpSendRequestPkt: Sending DHCPREQUEST\n");
813 memset((void *)pkt, 0, PKTSIZE);
815 eth_hdr_size = NetSetEther(pkt, NetBcastAddr, PROT_IP);
818 iphdr = pkt; /* We'll need this later to set proper pkt size */
819 pkt += IP_UDP_HDR_SIZE;
821 bp = (struct Bootp_t *)pkt;
822 bp->bp_op = OP_BOOTREQUEST;
823 bp->bp_htype = HWT_ETHER;
824 bp->bp_hlen = HWL_ETHER;
826 bp->bp_secs = htons(get_timer(0) / 1000);
827 /* Do not set the client IP, your IP, or server IP yet, since it
828 * hasn't been ACK'ed by the server yet */
831 * RFC3046 requires Relay Agents to discard packets with
832 * nonzero and offered giaddr
834 NetWriteIP(&bp->bp_giaddr, 0);
836 memcpy(bp->bp_chaddr, NetOurEther, 6);
839 * ID is the id of the OFFER packet
842 NetCopyLong(&bp->bp_id, &bp_offer->bp_id);
845 * Copy options from OFFER packet if present
848 /* Copy offered IP into the parameters request list */
849 NetCopyIP(&OfferedIP, &bp_offer->bp_yiaddr);
850 extlen = DhcpExtended((u8 *)bp->bp_vend, DHCP_REQUEST,
851 NetDHCPServerIP, OfferedIP);
853 iplen = BOOTP_HDR_SIZE - OPT_FIELD_SIZE + extlen;
854 pktlen = eth_hdr_size + IP_UDP_HDR_SIZE + iplen;
855 net_set_udp_header(iphdr, 0xFFFFFFFFL, PORT_BOOTPS, PORT_BOOTPC, iplen);
857 #ifdef CONFIG_BOOTP_DHCP_REQUEST_DELAY
858 udelay(CONFIG_BOOTP_DHCP_REQUEST_DELAY);
859 #endif /* CONFIG_BOOTP_DHCP_REQUEST_DELAY */
860 debug("Transmitting DHCPREQUEST packet: len = %d\n", pktlen);
861 NetSendPacket(NetTxPacket, pktlen);
865 * Handle DHCP received packets.
868 DhcpHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src,
871 struct Bootp_t *bp = (struct Bootp_t *)pkt;
873 debug("DHCPHandler: got packet: (src=%d, dst=%d, len=%d) state: %d\n",
874 src, dest, len, dhcp_state);
876 /* Filter out pkts we don't want */
877 if (BootpCheckPkt(pkt, dest, src, len))
880 debug("DHCPHandler: got DHCP packet: (src=%d, dst=%d, len=%d) state:"
881 " %d\n", src, dest, len, dhcp_state);
883 switch (dhcp_state) {
886 * Wait an appropriate time for any potential DHCPOFFER packets
887 * to arrive. Then select one, and generate DHCPREQUEST
888 * response. If filename is in format we recognize, assume it
889 * is a valid OFFER from a server we want.
891 debug("DHCP: state=SELECTING bp_file: \"%s\"\n", bp->bp_file);
892 #ifdef CONFIG_SYS_BOOTFILE_PREFIX
893 if (strncmp(bp->bp_file,
894 CONFIG_SYS_BOOTFILE_PREFIX,
895 strlen(CONFIG_SYS_BOOTFILE_PREFIX)) == 0) {
896 #endif /* CONFIG_SYS_BOOTFILE_PREFIX */
898 debug("TRANSITIONING TO REQUESTING STATE\n");
899 dhcp_state = REQUESTING;
901 if (NetReadLong((ulong *)&bp->bp_vend[0]) ==
902 htonl(BOOTP_VENDOR_MAGIC))
903 DhcpOptionsProcess((u8 *)&bp->bp_vend[4], bp);
905 NetSetTimeout(TIMEOUT, BootpTimeout);
906 DhcpSendRequestPkt(bp);
907 #ifdef CONFIG_SYS_BOOTFILE_PREFIX
909 #endif /* CONFIG_SYS_BOOTFILE_PREFIX */
914 debug("DHCP State: REQUESTING\n");
916 if (DhcpMessageType((u8 *)bp->bp_vend) == DHCP_ACK) {
917 if (NetReadLong((ulong *)&bp->bp_vend[0]) ==
918 htonl(BOOTP_VENDOR_MAGIC))
919 DhcpOptionsProcess((u8 *)&bp->bp_vend[4], bp);
920 /* Store net params from reply */
921 BootpCopyNetParams(bp);
923 printf("DHCP client bound to address %pI4\n",
925 bootstage_mark_name(BOOTSTAGE_ID_BOOTP_STOP,
933 /* DHCP client bound to address */
936 puts("DHCP: INVALID STATE\n");
942 void DhcpRequest(void)
946 #endif /* CONFIG_CMD_DHCP */