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
18 #ifdef CONFIG_STATUS_LED
19 #include <status_led.h>
22 #define BOOTP_VENDOR_MAGIC 0x63825363 /* RFC1048 Magic Cookie */
24 #define TIMEOUT 5000UL /* Milliseconds before trying BOOTP again */
25 #ifndef CONFIG_NET_RETRY_COUNT
26 # define TIMEOUT_COUNT 5 /* # of timeouts before giving up */
28 # define TIMEOUT_COUNT (CONFIG_NET_RETRY_COUNT)
31 #define PORT_BOOTPS 67 /* BOOTP server UDP port */
32 #define PORT_BOOTPC 68 /* BOOTP client UDP port */
34 #ifndef CONFIG_DHCP_MIN_EXT_LEN /* minimal length of extension list */
35 #define CONFIG_DHCP_MIN_EXT_LEN 64
41 #if defined(CONFIG_CMD_DHCP)
42 dhcp_state_t dhcp_state = INIT;
43 unsigned long dhcp_leasetime;
44 IPaddr_t NetDHCPServerIP;
45 static void DhcpHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src,
50 static char *dhcpmsg2str(int type)
53 case 1: return "DHCPDISCOVER"; break;
54 case 2: return "DHCPOFFER"; break;
55 case 3: return "DHCPREQUEST"; break;
56 case 4: return "DHCPDECLINE"; break;
57 case 5: return "DHCPACK"; break;
58 case 6: return "DHCPNACK"; break;
59 case 7: return "DHCPRELEASE"; break;
60 default: return "UNKNOWN/INVALID MSG TYPE"; break;
66 static int BootpCheckPkt(uchar *pkt, unsigned dest, unsigned src, unsigned len)
68 struct Bootp_t *bp = (struct Bootp_t *) pkt;
71 if (dest != PORT_BOOTPC || src != PORT_BOOTPS)
73 else if (len < sizeof(struct Bootp_t) - OPT_FIELD_SIZE)
75 else if (bp->bp_op != OP_BOOTREQUEST &&
76 bp->bp_op != OP_BOOTREPLY &&
77 bp->bp_op != DHCP_OFFER &&
78 bp->bp_op != DHCP_ACK &&
79 bp->bp_op != DHCP_NAK)
81 else if (bp->bp_htype != HWT_ETHER)
83 else if (bp->bp_hlen != HWL_ETHER)
85 else if (NetReadLong((ulong *)&bp->bp_id) != BootpID)
88 debug("Filtering pkt = %d\n", retval);
94 * Copy parameters of interest from BOOTP_REPLY/DHCP_OFFER packet
96 static void BootpCopyNetParams(struct Bootp_t *bp)
98 #if !defined(CONFIG_BOOTP_SERVERIP)
101 NetCopyIP(&tmp_ip, &bp->bp_siaddr);
103 NetCopyIP(&NetServerIP, &bp->bp_siaddr);
104 memcpy(NetServerEther, ((struct ethernet_hdr *)NetRxPacket)->et_src, 6);
106 NetCopyIP(&NetOurIP, &bp->bp_yiaddr);
107 if (strlen(bp->bp_file) > 0)
108 copy_filename(BootFile, bp->bp_file, sizeof(BootFile));
110 debug("Bootfile: %s\n", BootFile);
112 /* Propagate to environment:
113 * don't delete exising entry when BOOTP / DHCP reply does
114 * not contain a new value
117 setenv("bootfile", BootFile);
120 static int truncate_sz(const char *name, int maxlen, int curlen)
122 if (curlen >= maxlen) {
123 printf("*** WARNING: %s is too long (%d - max: %d)"
124 " - truncated\n", name, curlen, maxlen);
130 #if !defined(CONFIG_CMD_DHCP)
132 static void BootpVendorFieldProcess(u8 *ext)
134 int size = *(ext + 1);
136 debug("[BOOTP] Processing extension %d... (%d bytes)\n", *ext,
142 /* Fixed length fields */
143 case 1: /* Subnet mask */
144 if (NetOurSubnetMask == 0)
145 NetCopyIP(&NetOurSubnetMask, (IPaddr_t *) (ext + 2));
147 case 2: /* Time offset - Not yet supported */
149 /* Variable length fields */
150 case 3: /* Gateways list */
151 if (NetOurGatewayIP == 0)
152 NetCopyIP(&NetOurGatewayIP, (IPaddr_t *) (ext + 2));
154 case 4: /* Time server - Not yet supported */
156 case 5: /* IEN-116 name server - Not yet supported */
159 if (NetOurDNSIP == 0)
160 NetCopyIP(&NetOurDNSIP, (IPaddr_t *) (ext + 2));
161 #if defined(CONFIG_BOOTP_DNS2)
162 if ((NetOurDNS2IP == 0) && (size > 4))
163 NetCopyIP(&NetOurDNS2IP, (IPaddr_t *) (ext + 2 + 4));
166 case 7: /* Log server - Not yet supported */
168 case 8: /* Cookie/Quote server - Not yet supported */
170 case 9: /* LPR server - Not yet supported */
172 case 10: /* Impress server - Not yet supported */
174 case 11: /* RPL server - Not yet supported */
176 case 12: /* Host name */
177 if (NetOurHostName[0] == 0) {
178 size = truncate_sz("Host Name",
179 sizeof(NetOurHostName), size);
180 memcpy(&NetOurHostName, ext + 2, size);
181 NetOurHostName[size] = 0;
184 case 13: /* Boot file size */
186 NetBootFileSize = ntohs(*(ushort *) (ext + 2));
188 NetBootFileSize = ntohl(*(ulong *) (ext + 2));
190 case 14: /* Merit dump file - Not yet supported */
192 case 15: /* Domain name - Not yet supported */
194 case 16: /* Swap server - Not yet supported */
196 case 17: /* Root path */
197 if (NetOurRootPath[0] == 0) {
198 size = truncate_sz("Root Path",
199 sizeof(NetOurRootPath), size);
200 memcpy(&NetOurRootPath, ext + 2, size);
201 NetOurRootPath[size] = 0;
204 case 18: /* Extension path - Not yet supported */
206 * This can be used to send the information of the
207 * vendor area in another file that the client can
211 /* IP host layer fields */
212 case 40: /* NIS Domain name */
213 if (NetOurNISDomain[0] == 0) {
214 size = truncate_sz("NIS Domain Name",
215 sizeof(NetOurNISDomain), size);
216 memcpy(&NetOurNISDomain, ext + 2, size);
217 NetOurNISDomain[size] = 0;
220 #if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_NTPSERVER)
221 case 42: /* NTP server IP */
222 NetCopyIP(&NetNtpServerIP, (IPaddr_t *) (ext + 2));
225 /* Application layer fields */
226 case 43: /* Vendor specific info - Not yet supported */
228 * Binary information to exchange specific
229 * product information.
232 /* Reserved (custom) fields (128..254) */
236 static void BootpVendorProcess(u8 *ext, int size)
238 u8 *end = ext + size;
240 debug("[BOOTP] Checking extension (%d bytes)...\n", size);
242 while ((ext < end) && (*ext != 0xff)) {
250 BootpVendorFieldProcess(opt);
254 debug("[BOOTP] Received fields:\n");
255 if (NetOurSubnetMask)
256 debug("NetOurSubnetMask : %pI4\n", &NetOurSubnetMask);
259 debug("NetOurGatewayIP : %pI4", &NetOurGatewayIP);
262 debug("NetBootFileSize : %d\n", NetBootFileSize);
264 if (NetOurHostName[0])
265 debug("NetOurHostName : %s\n", NetOurHostName);
267 if (NetOurRootPath[0])
268 debug("NetOurRootPath : %s\n", NetOurRootPath);
270 if (NetOurNISDomain[0])
271 debug("NetOurNISDomain : %s\n", NetOurNISDomain);
274 debug("NetBootFileSize: %d\n", NetBootFileSize);
276 #if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_NTPSERVER)
278 debug("NetNtpServerIP : %pI4\n", &NetNtpServerIP);
283 * Handle a BOOTP received packet.
286 BootpHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src,
291 debug("got BOOTP packet (src=%d, dst=%d, len=%d want_len=%zu)\n",
292 src, dest, len, sizeof(struct Bootp_t));
294 bp = (struct Bootp_t *)pkt;
296 /* Filter out pkts we don't want */
297 if (BootpCheckPkt(pkt, dest, src, len))
301 * Got a good BOOTP reply. Copy the data into our variables.
303 #ifdef CONFIG_STATUS_LED
304 status_led_set(STATUS_LED_BOOT, STATUS_LED_OFF);
307 BootpCopyNetParams(bp); /* Store net parameters from reply */
309 /* Retrieve extended information (we must parse the vendor area) */
310 if (NetReadLong((ulong *)&bp->bp_vend[0]) == htonl(BOOTP_VENDOR_MAGIC))
311 BootpVendorProcess((uchar *)&bp->bp_vend[4], len);
313 NetSetTimeout(0, (thand_f *)0);
314 bootstage_mark_name(BOOTSTAGE_ID_BOOTP_STOP, "bootp_stop");
316 debug("Got good BOOTP\n");
323 * Timeout on BOOTP/DHCP request.
328 if (BootpTry >= TIMEOUT_COUNT) {
329 #ifdef CONFIG_BOOTP_MAY_FAIL
330 puts("\nRetry count exceeded\n");
331 NetSetState(NETLOOP_FAIL);
333 puts("\nRetry count exceeded; starting again\n");
337 NetSetTimeout(TIMEOUT, BootpTimeout);
343 * Initialize BOOTP extension fields in the request.
345 #if defined(CONFIG_CMD_DHCP)
346 static int DhcpExtended(u8 *e, int message_type, IPaddr_t ServerID,
347 IPaddr_t RequestedIP)
351 #if defined(CONFIG_BOOTP_PXE)
357 #if defined(CONFIG_BOOTP_VENDOREX)
360 #if defined(CONFIG_BOOTP_SEND_HOSTNAME)
364 *e++ = 99; /* RFC1048 Magic Cookie */
369 *e++ = 53; /* DHCP Message Type */
373 *e++ = 57; /* Maximum DHCP Message Size */
375 *e++ = (576 - 312 + OPT_FIELD_SIZE) >> 8;
376 *e++ = (576 - 312 + OPT_FIELD_SIZE) & 0xff;
379 int tmp = ntohl(ServerID);
381 *e++ = 54; /* ServerID */
390 int tmp = ntohl(RequestedIP);
392 *e++ = 50; /* Requested IP */
399 #if defined(CONFIG_BOOTP_SEND_HOSTNAME)
400 hostname = getenv("hostname");
402 int hostnamelen = strlen(hostname);
404 *e++ = 12; /* Hostname */
406 memcpy(e, hostname, hostnamelen);
411 #if defined(CONFIG_BOOTP_PXE)
412 clientarch = CONFIG_BOOTP_PXE_CLIENTARCH;
413 *e++ = 93; /* Client System Architecture */
415 *e++ = (clientarch >> 8) & 0xff;
416 *e++ = clientarch & 0xff;
418 *e++ = 94; /* Client Network Interface Identifier */
420 *e++ = 1; /* type field for UNDI */
421 *e++ = 0; /* major revision */
422 *e++ = 0; /* minor revision */
424 uuid = getenv("pxeuuid");
427 if (uuid_str_valid(uuid)) {
428 *e++ = 97; /* Client Machine Identifier */
430 *e++ = 0; /* type 0 - UUID */
432 uuid_str_to_bin(uuid, e);
435 printf("Invalid pxeuuid: %s\n", uuid);
439 *e++ = 60; /* Vendor Class Identifier */
440 vci_strlen = strlen(CONFIG_BOOTP_VCI_STRING);
442 memcpy(e, CONFIG_BOOTP_VCI_STRING, vci_strlen);
446 #if defined(CONFIG_BOOTP_VENDOREX)
447 x = dhcp_vendorex_prep(e);
452 *e++ = 55; /* Parameter Request List */
453 cnt = e++; /* Pointer to count of requested items */
455 #if defined(CONFIG_BOOTP_SUBNETMASK)
456 *e++ = 1; /* Subnet Mask */
459 #if defined(CONFIG_BOOTP_TIMEOFFSET)
463 #if defined(CONFIG_BOOTP_GATEWAY)
464 *e++ = 3; /* Router Option */
467 #if defined(CONFIG_BOOTP_DNS)
468 *e++ = 6; /* DNS Server(s) */
471 #if defined(CONFIG_BOOTP_HOSTNAME)
472 *e++ = 12; /* Hostname */
475 #if defined(CONFIG_BOOTP_BOOTFILESIZE)
476 *e++ = 13; /* Boot File Size */
479 #if defined(CONFIG_BOOTP_BOOTPATH)
480 *e++ = 17; /* Boot path */
483 #if defined(CONFIG_BOOTP_NISDOMAIN)
484 *e++ = 40; /* NIS Domain name request */
487 #if defined(CONFIG_BOOTP_NTPSERVER)
491 /* no options, so back up to avoid sending an empty request list */
495 *e++ = 255; /* End of the list */
497 /* Pad to minimal length */
498 #ifdef CONFIG_DHCP_MIN_EXT_LEN
499 while ((e - start) < CONFIG_DHCP_MIN_EXT_LEN)
508 * Warning: no field size check - change CONFIG_BOOTP_* at your own risk!
510 static int BootpExtended(u8 *e)
514 *e++ = 99; /* RFC1048 Magic Cookie */
519 #if defined(CONFIG_CMD_DHCP)
520 *e++ = 53; /* DHCP Message Type */
522 *e++ = DHCP_DISCOVER;
524 *e++ = 57; /* Maximum DHCP Message Size */
526 *e++ = (576 - 312 + OPT_FIELD_SIZE) >> 16;
527 *e++ = (576 - 312 + OPT_FIELD_SIZE) & 0xff;
530 #if defined(CONFIG_BOOTP_SUBNETMASK)
531 *e++ = 1; /* Subnet mask request */
536 #if defined(CONFIG_BOOTP_GATEWAY)
537 *e++ = 3; /* Default gateway request */
542 #if defined(CONFIG_BOOTP_DNS)
543 *e++ = 6; /* Domain Name Server */
548 #if defined(CONFIG_BOOTP_HOSTNAME)
549 *e++ = 12; /* Host name request */
554 #if defined(CONFIG_BOOTP_BOOTFILESIZE)
555 *e++ = 13; /* Boot file size */
560 #if defined(CONFIG_BOOTP_BOOTPATH)
561 *e++ = 17; /* Boot path */
566 #if defined(CONFIG_BOOTP_NISDOMAIN)
567 *e++ = 40; /* NIS Domain name request */
571 #if defined(CONFIG_BOOTP_NTPSERVER)
577 *e++ = 255; /* End of the list */
588 int extlen, pktlen, iplen;
590 #ifdef CONFIG_BOOTP_RANDOM_DELAY
594 bootstage_mark_name(BOOTSTAGE_ID_BOOTP_START, "bootp_start");
595 #if defined(CONFIG_CMD_DHCP)
599 #ifdef CONFIG_BOOTP_RANDOM_DELAY /* Random BOOTP delay */
603 if (BootpTry <= 2) /* Start with max 1024 * 1ms */
604 rand_ms = rand() >> (22 - BootpTry);
605 else /* After 3rd BOOTP request max 8192 * 1ms */
606 rand_ms = rand() >> 19;
608 printf("Random delay: %ld ms...\n", rand_ms);
609 for (i = 0; i < rand_ms; i++)
610 udelay(1000); /*Wait 1ms*/
612 #endif /* CONFIG_BOOTP_RANDOM_DELAY */
614 printf("BOOTP broadcast %d\n", ++BootpTry);
616 memset((void *)pkt, 0, PKTSIZE);
618 eth_hdr_size = NetSetEther(pkt, NetBcastAddr, PROT_IP);
622 * Next line results in incorrect packet size being transmitted,
623 * resulting in errors in some DHCP servers, reporting missing bytes.
624 * Size must be set in packet header after extension length has been
626 * C. Hallinan, DS4.COM, Inc.
628 /* net_set_udp_header(pkt, 0xFFFFFFFFL, PORT_BOOTPS, PORT_BOOTPC,
629 sizeof (struct Bootp_t)); */
630 iphdr = pkt; /* We need this later for net_set_udp_header() */
631 pkt += IP_UDP_HDR_SIZE;
633 bp = (struct Bootp_t *)pkt;
634 bp->bp_op = OP_BOOTREQUEST;
635 bp->bp_htype = HWT_ETHER;
636 bp->bp_hlen = HWL_ETHER;
638 bp->bp_secs = htons(get_timer(0) / 1000);
639 NetWriteIP(&bp->bp_ciaddr, 0);
640 NetWriteIP(&bp->bp_yiaddr, 0);
641 NetWriteIP(&bp->bp_siaddr, 0);
642 NetWriteIP(&bp->bp_giaddr, 0);
643 memcpy(bp->bp_chaddr, NetOurEther, 6);
644 copy_filename(bp->bp_file, BootFile, sizeof(bp->bp_file));
646 /* Request additional information from the BOOTP/DHCP server */
647 #if defined(CONFIG_CMD_DHCP)
648 extlen = DhcpExtended((u8 *)bp->bp_vend, DHCP_DISCOVER, 0, 0);
650 extlen = BootpExtended((u8 *)bp->bp_vend);
654 * Bootp ID is the lower 4 bytes of our ethernet address
655 * plus the current time in ms.
657 BootpID = ((ulong)NetOurEther[2] << 24)
658 | ((ulong)NetOurEther[3] << 16)
659 | ((ulong)NetOurEther[4] << 8)
660 | (ulong)NetOurEther[5];
661 BootpID += get_timer(0);
662 BootpID = htonl(BootpID);
663 NetCopyLong(&bp->bp_id, &BootpID);
666 * Calculate proper packet lengths taking into account the
667 * variable size of the options field
669 iplen = BOOTP_HDR_SIZE - OPT_FIELD_SIZE + extlen;
670 pktlen = eth_hdr_size + IP_UDP_HDR_SIZE + iplen;
671 net_set_udp_header(iphdr, 0xFFFFFFFFL, PORT_BOOTPS, PORT_BOOTPC, iplen);
672 NetSetTimeout(SELECT_TIMEOUT, BootpTimeout);
674 #if defined(CONFIG_CMD_DHCP)
675 dhcp_state = SELECTING;
676 net_set_udp_handler(DhcpHandler);
678 net_set_udp_handler(BootpHandler);
680 NetSendPacket(NetTxPacket, pktlen);
683 #if defined(CONFIG_CMD_DHCP)
684 static void DhcpOptionsProcess(uchar *popt, struct Bootp_t *bp)
686 uchar *end = popt + BOOTP_HDR_SIZE;
688 #if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_TIMEOFFSET)
692 while (popt < end && *popt != 0xff) {
696 NetCopyIP(&NetOurSubnetMask, (popt + 2));
698 #if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_TIMEOFFSET)
699 case 2: /* Time offset */
700 to_ptr = &NetTimeOffset;
701 NetCopyLong((ulong *)to_ptr, (ulong *)(popt + 2));
702 NetTimeOffset = ntohl(NetTimeOffset);
706 NetCopyIP(&NetOurGatewayIP, (popt + 2));
709 NetCopyIP(&NetOurDNSIP, (popt + 2));
710 #if defined(CONFIG_BOOTP_DNS2)
712 NetCopyIP(&NetOurDNS2IP, (popt + 2 + 4));
716 size = truncate_sz("Host Name",
717 sizeof(NetOurHostName), oplen);
718 memcpy(&NetOurHostName, popt + 2, size);
719 NetOurHostName[size] = 0;
721 case 15: /* Ignore Domain Name Option */
724 size = truncate_sz("Root Path",
725 sizeof(NetOurRootPath), oplen);
726 memcpy(&NetOurRootPath, popt + 2, size);
727 NetOurRootPath[size] = 0;
729 #if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_NTPSERVER)
730 case 42: /* NTP server IP */
731 NetCopyIP(&NetNtpServerIP, (popt + 2));
735 NetCopyLong(&dhcp_leasetime, (ulong *) (popt + 2));
737 case 53: /* Ignore Message Type Option */
740 NetCopyIP(&NetDHCPServerIP, (popt + 2));
742 case 58: /* Ignore Renewal Time Option */
744 case 59: /* Ignore Rebinding Time Option */
746 case 66: /* Ignore TFTP server name */
748 case 67: /* vendor opt bootfile */
750 * I can't use dhcp_vendorex_proc here because I need
751 * to write into the bootp packet - even then I had to
752 * pass the bootp packet pointer into here as the
755 size = truncate_sz("Opt Boot File",
758 if (bp->bp_file[0] == '\0' && size > 0) {
760 * only use vendor boot file if we didn't
761 * receive a boot file in the main non-vendor
762 * part of the packet - god only knows why
763 * some vendors chose not to use this perfectly
764 * good spot to store the boot file (join on
765 * Tru64 Unix) it seems mind bogglingly crazy
768 printf("*** WARNING: using vendor "
769 "optional boot file\n");
770 memcpy(bp->bp_file, popt + 2, size);
771 bp->bp_file[size] = '\0';
775 #if defined(CONFIG_BOOTP_VENDOREX)
776 if (dhcp_vendorex_proc(popt))
779 printf("*** Unhandled DHCP Option in OFFER/ACK:"
783 popt += oplen + 2; /* Process next option */
787 static int DhcpMessageType(unsigned char *popt)
789 if (NetReadLong((ulong *)popt) != htonl(BOOTP_VENDOR_MAGIC))
793 while (*popt != 0xff) {
794 if (*popt == 53) /* DHCP Message Type */
796 popt += *(popt + 1) + 2; /* Scan through all options */
801 static void DhcpSendRequestPkt(struct Bootp_t *bp_offer)
805 int pktlen, iplen, extlen;
809 debug("DhcpSendRequestPkt: Sending DHCPREQUEST\n");
811 memset((void *)pkt, 0, PKTSIZE);
813 eth_hdr_size = NetSetEther(pkt, NetBcastAddr, PROT_IP);
816 iphdr = pkt; /* We'll need this later to set proper pkt size */
817 pkt += IP_UDP_HDR_SIZE;
819 bp = (struct Bootp_t *)pkt;
820 bp->bp_op = OP_BOOTREQUEST;
821 bp->bp_htype = HWT_ETHER;
822 bp->bp_hlen = HWL_ETHER;
824 bp->bp_secs = htons(get_timer(0) / 1000);
825 /* Do not set the client IP, your IP, or server IP yet, since it
826 * hasn't been ACK'ed by the server yet */
829 * RFC3046 requires Relay Agents to discard packets with
830 * nonzero and offered giaddr
832 NetWriteIP(&bp->bp_giaddr, 0);
834 memcpy(bp->bp_chaddr, NetOurEther, 6);
837 * ID is the id of the OFFER packet
840 NetCopyLong(&bp->bp_id, &bp_offer->bp_id);
843 * Copy options from OFFER packet if present
846 /* Copy offered IP into the parameters request list */
847 NetCopyIP(&OfferedIP, &bp_offer->bp_yiaddr);
848 extlen = DhcpExtended((u8 *)bp->bp_vend, DHCP_REQUEST,
849 NetDHCPServerIP, OfferedIP);
851 iplen = BOOTP_HDR_SIZE - OPT_FIELD_SIZE + extlen;
852 pktlen = eth_hdr_size + IP_UDP_HDR_SIZE + iplen;
853 net_set_udp_header(iphdr, 0xFFFFFFFFL, PORT_BOOTPS, PORT_BOOTPC, iplen);
855 #ifdef CONFIG_BOOTP_DHCP_REQUEST_DELAY
856 udelay(CONFIG_BOOTP_DHCP_REQUEST_DELAY);
857 #endif /* CONFIG_BOOTP_DHCP_REQUEST_DELAY */
858 debug("Transmitting DHCPREQUEST packet: len = %d\n", pktlen);
859 NetSendPacket(NetTxPacket, pktlen);
863 * Handle DHCP received packets.
866 DhcpHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src,
869 struct Bootp_t *bp = (struct Bootp_t *)pkt;
871 debug("DHCPHandler: got packet: (src=%d, dst=%d, len=%d) state: %d\n",
872 src, dest, len, dhcp_state);
874 /* Filter out pkts we don't want */
875 if (BootpCheckPkt(pkt, dest, src, len))
878 debug("DHCPHandler: got DHCP packet: (src=%d, dst=%d, len=%d) state:"
879 " %d\n", src, dest, len, dhcp_state);
881 switch (dhcp_state) {
884 * Wait an appropriate time for any potential DHCPOFFER packets
885 * to arrive. Then select one, and generate DHCPREQUEST
886 * response. If filename is in format we recognize, assume it
887 * is a valid OFFER from a server we want.
889 debug("DHCP: state=SELECTING bp_file: \"%s\"\n", bp->bp_file);
890 #ifdef CONFIG_SYS_BOOTFILE_PREFIX
891 if (strncmp(bp->bp_file,
892 CONFIG_SYS_BOOTFILE_PREFIX,
893 strlen(CONFIG_SYS_BOOTFILE_PREFIX)) == 0) {
894 #endif /* CONFIG_SYS_BOOTFILE_PREFIX */
896 debug("TRANSITIONING TO REQUESTING STATE\n");
897 dhcp_state = REQUESTING;
899 if (NetReadLong((ulong *)&bp->bp_vend[0]) ==
900 htonl(BOOTP_VENDOR_MAGIC))
901 DhcpOptionsProcess((u8 *)&bp->bp_vend[4], bp);
903 NetSetTimeout(TIMEOUT, BootpTimeout);
904 DhcpSendRequestPkt(bp);
905 #ifdef CONFIG_SYS_BOOTFILE_PREFIX
907 #endif /* CONFIG_SYS_BOOTFILE_PREFIX */
912 debug("DHCP State: REQUESTING\n");
914 if (DhcpMessageType((u8 *)bp->bp_vend) == DHCP_ACK) {
915 if (NetReadLong((ulong *)&bp->bp_vend[0]) ==
916 htonl(BOOTP_VENDOR_MAGIC))
917 DhcpOptionsProcess((u8 *)&bp->bp_vend[4], bp);
918 /* Store net params from reply */
919 BootpCopyNetParams(bp);
921 printf("DHCP client bound to address %pI4\n",
923 bootstage_mark_name(BOOTSTAGE_ID_BOOTP_STOP,
931 /* DHCP client bound to address */
934 puts("DHCP: INVALID STATE\n");
940 void DhcpRequest(void)
944 #endif /* CONFIG_CMD_DHCP */