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 #include <linux/compiler.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
40 #ifdef CONFIG_BOOTP_RANDOM_DELAY
44 #if defined(CONFIG_CMD_DHCP)
45 dhcp_state_t dhcp_state = INIT;
46 unsigned long dhcp_leasetime;
47 IPaddr_t NetDHCPServerIP;
48 static void DhcpHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src,
53 static char *dhcpmsg2str(int type)
56 case 1: return "DHCPDISCOVER"; break;
57 case 2: return "DHCPOFFER"; break;
58 case 3: return "DHCPREQUEST"; break;
59 case 4: return "DHCPDECLINE"; break;
60 case 5: return "DHCPACK"; break;
61 case 6: return "DHCPNACK"; break;
62 case 7: return "DHCPRELEASE"; break;
63 default: return "UNKNOWN/INVALID MSG TYPE"; break;
69 static int BootpCheckPkt(uchar *pkt, unsigned dest, unsigned src, unsigned len)
71 struct Bootp_t *bp = (struct Bootp_t *) pkt;
74 if (dest != PORT_BOOTPC || src != PORT_BOOTPS)
76 else if (len < sizeof(struct Bootp_t) - OPT_SIZE)
78 else if (bp->bp_op != OP_BOOTREQUEST &&
79 bp->bp_op != OP_BOOTREPLY &&
80 bp->bp_op != DHCP_OFFER &&
81 bp->bp_op != DHCP_ACK &&
82 bp->bp_op != DHCP_NAK)
84 else if (bp->bp_htype != HWT_ETHER)
86 else if (bp->bp_hlen != HWL_ETHER)
88 else if (NetReadLong((ulong *)&bp->bp_id) != BootpID)
91 debug("Filtering pkt = %d\n", retval);
97 * Copy parameters of interest from BOOTP_REPLY/DHCP_OFFER packet
99 static void BootpCopyNetParams(struct Bootp_t *bp)
101 __maybe_unused IPaddr_t tmp_ip;
103 NetCopyIP(&NetOurIP, &bp->bp_yiaddr);
104 #if !defined(CONFIG_BOOTP_SERVERIP)
105 NetCopyIP(&tmp_ip, &bp->bp_siaddr);
107 NetCopyIP(&NetServerIP, &bp->bp_siaddr);
108 memcpy(NetServerEther, ((Ethernet_t *)NetRxPacket)->et_src, 6);
110 if (strlen(bp->bp_file) > 0)
111 copy_filename(BootFile, bp->bp_file, sizeof(BootFile));
113 debug("Bootfile: %s\n", BootFile);
115 /* Propagate to environment:
116 * don't delete exising entry when BOOTP / DHCP reply does
117 * not contain a new value
120 setenv("bootfile", BootFile);
123 static int truncate_sz(const char *name, int maxlen, int curlen)
125 if (curlen >= maxlen) {
126 printf("*** WARNING: %s is too long (%d - max: %d)"
127 " - truncated\n", name, curlen, maxlen);
133 #if !defined(CONFIG_CMD_DHCP)
135 static void BootpVendorFieldProcess(u8 *ext)
137 int size = *(ext + 1);
139 debug("[BOOTP] Processing extension %d... (%d bytes)\n", *ext,
145 /* Fixed length fields */
146 case 1: /* Subnet mask */
147 if (NetOurSubnetMask == 0)
148 NetCopyIP(&NetOurSubnetMask, (IPaddr_t *) (ext + 2));
150 case 2: /* Time offset - Not yet supported */
152 /* Variable length fields */
153 case 3: /* Gateways list */
154 if (NetOurGatewayIP == 0)
155 NetCopyIP(&NetOurGatewayIP, (IPaddr_t *) (ext + 2));
157 case 4: /* Time server - Not yet supported */
159 case 5: /* IEN-116 name server - Not yet supported */
162 if (NetOurDNSIP == 0)
163 NetCopyIP(&NetOurDNSIP, (IPaddr_t *) (ext + 2));
164 #if defined(CONFIG_BOOTP_DNS2)
165 if ((NetOurDNS2IP == 0) && (size > 4))
166 NetCopyIP(&NetOurDNS2IP, (IPaddr_t *) (ext + 2 + 4));
169 case 7: /* Log server - Not yet supported */
171 case 8: /* Cookie/Quote server - Not yet supported */
173 case 9: /* LPR server - Not yet supported */
175 case 10: /* Impress server - Not yet supported */
177 case 11: /* RPL server - Not yet supported */
179 case 12: /* Host name */
180 if (NetOurHostName[0] == 0) {
181 size = truncate_sz("Host Name",
182 sizeof(NetOurHostName), size);
183 memcpy(&NetOurHostName, ext + 2, size);
184 NetOurHostName[size] = 0;
187 case 13: /* Boot file size */
189 NetBootFileSize = ntohs(*(ushort *) (ext + 2));
191 NetBootFileSize = ntohl(*(ulong *) (ext + 2));
193 case 14: /* Merit dump file - Not yet supported */
195 case 15: /* Domain name - Not yet supported */
197 case 16: /* Swap server - Not yet supported */
199 case 17: /* Root path */
200 if (NetOurRootPath[0] == 0) {
201 size = truncate_sz("Root Path",
202 sizeof(NetOurRootPath), size);
203 memcpy(&NetOurRootPath, ext + 2, size);
204 NetOurRootPath[size] = 0;
207 case 18: /* Extension path - Not yet supported */
209 * This can be used to send the information of the
210 * vendor area in another file that the client can
214 /* IP host layer fields */
215 case 40: /* NIS Domain name */
216 if (NetOurNISDomain[0] == 0) {
217 size = truncate_sz("NIS Domain Name",
218 sizeof(NetOurNISDomain), size);
219 memcpy(&NetOurNISDomain, ext + 2, size);
220 NetOurNISDomain[size] = 0;
223 #if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_NTPSERVER)
224 case 42: /* NTP server IP */
225 NetCopyIP(&NetNtpServerIP, (IPaddr_t *) (ext + 2));
228 /* Application layer fields */
229 case 43: /* Vendor specific info - Not yet supported */
231 * Binary information to exchange specific
232 * product information.
235 /* Reserved (custom) fields (128..254) */
239 static void BootpVendorProcess(u8 *ext, int size)
241 u8 *end = ext + size;
243 debug("[BOOTP] Checking extension (%d bytes)...\n", size);
245 while ((ext < end) && (*ext != 0xff)) {
253 BootpVendorFieldProcess(opt);
257 debug("[BOOTP] Received fields:\n");
258 if (NetOurSubnetMask)
259 debug("NetOurSubnetMask : %pI4\n", &NetOurSubnetMask);
262 debug("NetOurGatewayIP : %pI4", &NetOurGatewayIP);
265 debug("NetBootFileSize : %d\n", NetBootFileSize);
267 if (NetOurHostName[0])
268 debug("NetOurHostName : %s\n", NetOurHostName);
270 if (NetOurRootPath[0])
271 debug("NetOurRootPath : %s\n", NetOurRootPath);
273 if (NetOurNISDomain[0])
274 debug("NetOurNISDomain : %s\n", NetOurNISDomain);
277 debug("NetBootFileSize: %d\n", NetBootFileSize);
279 #if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_NTPSERVER)
281 debug("NetNtpServerIP : %pI4\n", &NetNtpServerIP);
286 * Handle a BOOTP received packet.
289 BootpHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src,
294 debug("got BOOTP packet (src=%d, dst=%d, len=%d want_len=%zu)\n",
295 src, dest, len, sizeof(struct Bootp_t));
297 bp = (struct Bootp_t *)pkt;
299 /* Filter out pkts we don't want */
300 if (BootpCheckPkt(pkt, dest, src, len))
304 * Got a good BOOTP reply. Copy the data into our variables.
306 #ifdef CONFIG_STATUS_LED
307 status_led_set(STATUS_LED_BOOT, STATUS_LED_OFF);
310 BootpCopyNetParams(bp); /* Store net parameters from reply */
312 /* Retrieve extended information (we must parse the vendor area) */
313 if (NetReadLong((ulong *)&bp->bp_vend[0]) == htonl(BOOTP_VENDOR_MAGIC))
314 BootpVendorProcess((uchar *)&bp->bp_vend[4], len);
316 NetSetTimeout(0, (thand_f *)0);
317 bootstage_mark_name(BOOTSTAGE_ID_BOOTP_STOP, "bootp_stop");
319 debug("Got good BOOTP\n");
326 * Timeout on BOOTP/DHCP request.
331 if (BootpTry >= TIMEOUT_COUNT) {
332 puts("\nRetry count exceeded; starting again\n");
335 NetSetTimeout(TIMEOUT, BootpTimeout);
341 * Initialize BOOTP extension fields in the request.
343 #if defined(CONFIG_CMD_DHCP)
344 static int DhcpExtended(u8 *e, int message_type, IPaddr_t ServerID,
345 IPaddr_t RequestedIP)
349 #if defined(CONFIG_BOOTP_PXE)
355 #if defined(CONFIG_BOOTP_VENDOREX)
358 #if defined(CONFIG_BOOTP_SEND_HOSTNAME)
362 *e++ = 99; /* RFC1048 Magic Cookie */
367 *e++ = 53; /* DHCP Message Type */
371 *e++ = 57; /* Maximum DHCP Message Size */
373 *e++ = (576 - 312 + OPT_SIZE) >> 8;
374 *e++ = (576 - 312 + OPT_SIZE) & 0xff;
377 int tmp = ntohl(ServerID);
379 *e++ = 54; /* ServerID */
388 int tmp = ntohl(RequestedIP);
390 *e++ = 50; /* Requested IP */
397 #if defined(CONFIG_BOOTP_SEND_HOSTNAME)
398 hostname = getenv("hostname");
400 int hostnamelen = strlen(hostname);
402 *e++ = 12; /* Hostname */
404 memcpy(e, hostname, hostnamelen);
409 #if defined(CONFIG_BOOTP_PXE)
410 clientarch = CONFIG_BOOTP_PXE_CLIENTARCH;
411 *e++ = 93; /* Client System Architecture */
413 *e++ = (clientarch >> 8) & 0xff;
414 *e++ = clientarch & 0xff;
416 *e++ = 94; /* Client Network Interface Identifier */
418 *e++ = 1; /* type field for UNDI */
419 *e++ = 0; /* major revision */
420 *e++ = 0; /* minor revision */
422 uuid = getenv("pxeuuid");
425 if (uuid_str_valid(uuid)) {
426 *e++ = 97; /* Client Machine Identifier */
428 *e++ = 0; /* type 0 - UUID */
430 uuid_str_to_bin(uuid, e);
433 printf("Invalid pxeuuid: %s\n", uuid);
437 *e++ = 60; /* Vendor Class Identifier */
438 vci_strlen = strlen(CONFIG_BOOTP_VCI_STRING);
440 memcpy(e, CONFIG_BOOTP_VCI_STRING, vci_strlen);
444 #if defined(CONFIG_BOOTP_VENDOREX)
445 x = dhcp_vendorex_prep(e);
450 *e++ = 55; /* Parameter Request List */
451 cnt = e++; /* Pointer to count of requested items */
453 #if defined(CONFIG_BOOTP_SUBNETMASK)
454 *e++ = 1; /* Subnet Mask */
457 #if defined(CONFIG_BOOTP_TIMEOFFSET)
461 #if defined(CONFIG_BOOTP_GATEWAY)
462 *e++ = 3; /* Router Option */
465 #if defined(CONFIG_BOOTP_DNS)
466 *e++ = 6; /* DNS Server(s) */
469 #if defined(CONFIG_BOOTP_HOSTNAME)
470 *e++ = 12; /* Hostname */
473 #if defined(CONFIG_BOOTP_BOOTFILESIZE)
474 *e++ = 13; /* Boot File Size */
477 #if defined(CONFIG_BOOTP_BOOTPATH)
478 *e++ = 17; /* Boot path */
481 #if defined(CONFIG_BOOTP_NISDOMAIN)
482 *e++ = 40; /* NIS Domain name request */
485 #if defined(CONFIG_BOOTP_NTPSERVER)
489 /* no options, so back up to avoid sending an empty request list */
493 *e++ = 255; /* End of the list */
495 /* Pad to minimal length */
496 #ifdef CONFIG_DHCP_MIN_EXT_LEN
497 while ((e - start) < CONFIG_DHCP_MIN_EXT_LEN)
506 * Warning: no field size check - change CONFIG_BOOTP_* at your own risk!
508 static int BootpExtended(u8 *e)
512 *e++ = 99; /* RFC1048 Magic Cookie */
517 #if defined(CONFIG_CMD_DHCP)
518 *e++ = 53; /* DHCP Message Type */
520 *e++ = DHCP_DISCOVER;
522 *e++ = 57; /* Maximum DHCP Message Size */
524 *e++ = (576 - 312 + OPT_SIZE) >> 16;
525 *e++ = (576 - 312 + OPT_SIZE) & 0xff;
528 #if defined(CONFIG_BOOTP_SUBNETMASK)
529 *e++ = 1; /* Subnet mask request */
534 #if defined(CONFIG_BOOTP_GATEWAY)
535 *e++ = 3; /* Default gateway request */
540 #if defined(CONFIG_BOOTP_DNS)
541 *e++ = 6; /* Domain Name Server */
546 #if defined(CONFIG_BOOTP_HOSTNAME)
547 *e++ = 12; /* Host name request */
552 #if defined(CONFIG_BOOTP_BOOTFILESIZE)
553 *e++ = 13; /* Boot file size */
558 #if defined(CONFIG_BOOTP_BOOTPATH)
559 *e++ = 17; /* Boot path */
564 #if defined(CONFIG_BOOTP_NISDOMAIN)
565 *e++ = 40; /* NIS Domain name request */
569 #if defined(CONFIG_BOOTP_NTPSERVER)
575 *e++ = 255; /* End of the list */
586 int ext_len, pktlen, iplen;
588 bootstage_mark_name(BOOTSTAGE_ID_BOOTP_START, "bootp_start");
589 #if defined(CONFIG_CMD_DHCP)
593 #ifdef CONFIG_BOOTP_RANDOM_DELAY /* Random BOOTP delay */
594 unsigned char bi_enetaddr[6];
596 ulong tst1, tst2, sum, m_mask, m_value = 0;
600 eth_getenv_enetaddr("ethaddr", bi_enetaddr);
602 debug("BootpRequest => Our Mac: ");
603 for (reg = 0; reg < 6; reg++)
604 debug("%x%c", bi_enetaddr[reg], 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 */
632 for (reg = 0; reg <= 0; reg++) {
634 if (sum < seed1 || sum < seed2)
639 if (BootpTry <= 2) { /* Start with max 1024 * 1ms */
640 sum = sum >> (22-BootpTry);
641 } else { /*After 3rd BOOTP request max 8192 * 1ms */
646 printf("Random delay: %ld ms...\n", sum);
647 for (reg = 0; reg < sum; reg++)
648 udelay(1000); /*Wait 1ms*/
650 #endif /* CONFIG_BOOTP_RANDOM_DELAY */
652 printf("BOOTP broadcast %d\n", ++BootpTry);
654 memset((void *)pkt, 0, PKTSIZE);
656 pkt += NetSetEther(pkt, NetBcastAddr, PROT_IP);
659 * Next line results in incorrect packet size being transmitted,
660 * resulting in errors in some DHCP servers, reporting missing bytes.
661 * Size must be set in packet header after extension length has been
663 * C. Hallinan, DS4.COM, Inc.
665 /* NetSetIP(pkt, 0xFFFFFFFFL, PORT_BOOTPS, PORT_BOOTPC,
666 sizeof (struct Bootp_t)); */
667 iphdr = pkt; /* We need this later for NetSetIP() */
670 bp = (struct Bootp_t *)pkt;
671 bp->bp_op = OP_BOOTREQUEST;
672 bp->bp_htype = HWT_ETHER;
673 bp->bp_hlen = HWL_ETHER;
675 bp->bp_secs = htons(get_timer(0) / 1000);
676 NetWriteIP(&bp->bp_ciaddr, 0);
677 NetWriteIP(&bp->bp_yiaddr, 0);
678 NetWriteIP(&bp->bp_siaddr, 0);
679 NetWriteIP(&bp->bp_giaddr, 0);
680 memcpy(bp->bp_chaddr, NetOurEther, 6);
681 copy_filename(bp->bp_file, BootFile, sizeof(bp->bp_file));
683 /* Request additional information from the BOOTP/DHCP server */
684 #if defined(CONFIG_CMD_DHCP)
685 ext_len = DhcpExtended((u8 *)bp->bp_vend, DHCP_DISCOVER, 0, 0);
687 ext_len = BootpExtended((u8 *)bp->bp_vend);
691 * Bootp ID is the lower 4 bytes of our ethernet address
692 * plus the current time in ms.
694 BootpID = ((ulong)NetOurEther[2] << 24)
695 | ((ulong)NetOurEther[3] << 16)
696 | ((ulong)NetOurEther[4] << 8)
697 | (ulong)NetOurEther[5];
698 BootpID += get_timer(0);
699 BootpID = htonl(BootpID);
700 NetCopyLong(&bp->bp_id, &BootpID);
703 * Calculate proper packet lengths taking into account the
704 * variable size of the options field
706 pktlen = ((int)(pkt-NetTxPacket)) + BOOTP_HDR_SIZE -
707 sizeof(bp->bp_vend) + ext_len;
708 iplen = BOOTP_HDR_SIZE - sizeof(bp->bp_vend) + ext_len;
709 NetSetIP(iphdr, 0xFFFFFFFFL, PORT_BOOTPS, PORT_BOOTPC, iplen);
710 NetSetTimeout(SELECT_TIMEOUT, BootpTimeout);
712 #if defined(CONFIG_CMD_DHCP)
713 dhcp_state = SELECTING;
714 NetSetHandler(DhcpHandler);
716 NetSetHandler(BootpHandler);
718 NetSendPacket(NetTxPacket, pktlen);
721 #if defined(CONFIG_CMD_DHCP)
722 static void DhcpOptionsProcess(uchar *popt, struct Bootp_t *bp)
724 uchar *end = popt + BOOTP_HDR_SIZE;
726 #if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_TIMEOFFSET)
730 while (popt < end && *popt != 0xff) {
734 NetCopyIP(&NetOurSubnetMask, (popt + 2));
736 #if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_TIMEOFFSET)
737 case 2: /* Time offset */
738 to_ptr = &NetTimeOffset;
739 NetCopyLong((ulong *)to_ptr, (ulong *)(popt + 2));
740 NetTimeOffset = ntohl(NetTimeOffset);
744 NetCopyIP(&NetOurGatewayIP, (popt + 2));
747 NetCopyIP(&NetOurDNSIP, (popt + 2));
748 #if defined(CONFIG_BOOTP_DNS2)
750 NetCopyIP(&NetOurDNS2IP, (popt + 2 + 4));
754 size = truncate_sz("Host Name",
755 sizeof(NetOurHostName), oplen);
756 memcpy(&NetOurHostName, popt + 2, size);
757 NetOurHostName[size] = 0;
759 case 15: /* Ignore Domain Name Option */
762 size = truncate_sz("Root Path",
763 sizeof(NetOurRootPath), oplen);
764 memcpy(&NetOurRootPath, popt + 2, size);
765 NetOurRootPath[size] = 0;
767 #if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_NTPSERVER)
768 case 42: /* NTP server IP */
769 NetCopyIP(&NetNtpServerIP, (popt + 2));
773 NetCopyLong(&dhcp_leasetime, (ulong *) (popt + 2));
775 case 53: /* Ignore Message Type Option */
778 NetCopyIP(&NetDHCPServerIP, (popt + 2));
780 case 58: /* Ignore Renewal Time Option */
782 case 59: /* Ignore Rebinding Time Option */
784 case 66: /* Ignore TFTP server name */
786 case 67: /* vendor opt bootfile */
788 * I can't use dhcp_vendorex_proc here because I need
789 * to write into the bootp packet - even then I had to
790 * pass the bootp packet pointer into here as the
793 size = truncate_sz("Opt Boot File",
796 if (bp->bp_file[0] == '\0' && size > 0) {
798 * only use vendor boot file if we didn't
799 * receive a boot file in the main non-vendor
800 * part of the packet - god only knows why
801 * some vendors chose not to use this perfectly
802 * good spot to store the boot file (join on
803 * Tru64 Unix) it seems mind bogglingly crazy
806 printf("*** WARNING: using vendor "
807 "optional boot file\n");
808 memcpy(bp->bp_file, popt + 2, size);
809 bp->bp_file[size] = '\0';
813 #if defined(CONFIG_BOOTP_VENDOREX)
814 if (dhcp_vendorex_proc(popt))
817 printf("*** Unhandled DHCP Option in OFFER/ACK:"
821 popt += oplen + 2; /* Process next option */
825 static int DhcpMessageType(unsigned char *popt)
827 if (NetReadLong((ulong *)popt) != htonl(BOOTP_VENDOR_MAGIC))
831 while (*popt != 0xff) {
832 if (*popt == 53) /* DHCP Message Type */
834 popt += *(popt + 1) + 2; /* Scan through all options */
839 static void DhcpSendRequestPkt(struct Bootp_t *bp_offer)
843 int pktlen, iplen, extlen;
846 debug("DhcpSendRequestPkt: Sending DHCPREQUEST\n");
848 memset((void *)pkt, 0, PKTSIZE);
850 pkt += NetSetEther(pkt, NetBcastAddr, PROT_IP);
852 iphdr = pkt; /* We'll need this later to set proper pkt size */
855 bp = (struct Bootp_t *)pkt;
856 bp->bp_op = OP_BOOTREQUEST;
857 bp->bp_htype = HWT_ETHER;
858 bp->bp_hlen = HWL_ETHER;
860 bp->bp_secs = htons(get_timer(0) / 1000);
861 /* Do not set the client IP, your IP, or server IP yet, since it
862 * hasn't been ACK'ed by the server yet */
865 * RFC3046 requires Relay Agents to discard packets with
866 * nonzero and offered giaddr
868 NetWriteIP(&bp->bp_giaddr, 0);
870 memcpy(bp->bp_chaddr, NetOurEther, 6);
873 * ID is the id of the OFFER packet
876 NetCopyLong(&bp->bp_id, &bp_offer->bp_id);
879 * Copy options from OFFER packet if present
882 /* Copy offered IP into the parameters request list */
883 NetCopyIP(&OfferedIP, &bp_offer->bp_yiaddr);
884 extlen = DhcpExtended((u8 *)bp->bp_vend, DHCP_REQUEST,
885 NetDHCPServerIP, OfferedIP);
887 pktlen = ((int)(pkt-NetTxPacket)) + BOOTP_HDR_SIZE -
888 sizeof(bp->bp_vend) + extlen;
889 iplen = BOOTP_HDR_SIZE - sizeof(bp->bp_vend) + extlen;
890 NetSetIP(iphdr, 0xFFFFFFFFL, PORT_BOOTPS, PORT_BOOTPC, iplen);
892 debug("Transmitting DHCPREQUEST packet: len = %d\n", pktlen);
893 #ifdef CONFIG_BOOTP_DHCP_REQUEST_DELAY
894 udelay(CONFIG_BOOTP_DHCP_REQUEST_DELAY);
895 #endif /* CONFIG_BOOTP_DHCP_REQUEST_DELAY */
896 NetSendPacket(NetTxPacket, pktlen);
900 * Handle DHCP received packets.
903 DhcpHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src,
906 struct Bootp_t *bp = (struct Bootp_t *)pkt;
908 debug("DHCPHandler: got packet: (src=%d, dst=%d, len=%d) state: %d\n",
909 src, dest, len, dhcp_state);
911 /* Filter out pkts we don't want */
912 if (BootpCheckPkt(pkt, dest, src, len))
915 debug("DHCPHandler: got DHCP packet: (src=%d, dst=%d, len=%d) state:"
916 " %d\n", src, dest, len, dhcp_state);
918 switch (dhcp_state) {
921 * Wait an appropriate time for any potential DHCPOFFER packets
922 * to arrive. Then select one, and generate DHCPREQUEST
923 * response. If filename is in format we recognize, assume it
924 * is a valid OFFER from a server we want.
926 debug("DHCP: state=SELECTING bp_file: \"%s\"\n", bp->bp_file);
927 #ifdef CONFIG_SYS_BOOTFILE_PREFIX
928 if (strncmp(bp->bp_file,
929 CONFIG_SYS_BOOTFILE_PREFIX,
930 strlen(CONFIG_SYS_BOOTFILE_PREFIX)) == 0) {
931 #endif /* CONFIG_SYS_BOOTFILE_PREFIX */
933 debug("TRANSITIONING TO REQUESTING STATE\n");
934 dhcp_state = REQUESTING;
936 if (NetReadLong((ulong *)&bp->bp_vend[0]) ==
937 htonl(BOOTP_VENDOR_MAGIC))
938 DhcpOptionsProcess((u8 *)&bp->bp_vend[4], bp);
940 NetSetTimeout(TIMEOUT, BootpTimeout);
941 DhcpSendRequestPkt(bp);
942 #ifdef CONFIG_SYS_BOOTFILE_PREFIX
944 #endif /* CONFIG_SYS_BOOTFILE_PREFIX */
949 debug("DHCP State: REQUESTING\n");
951 if (DhcpMessageType((u8 *)bp->bp_vend) == DHCP_ACK) {
952 if (NetReadLong((ulong *)&bp->bp_vend[0]) ==
953 htonl(BOOTP_VENDOR_MAGIC))
954 DhcpOptionsProcess((u8 *)&bp->bp_vend[4], bp);
955 /* Store net params from reply */
956 BootpCopyNetParams(bp);
958 printf("DHCP client bound to address %pI4\n",
960 bootstage_mark_name(BOOTSTAGE_ID_BOOTP_STOP,
968 /* DHCP client bound to address */
971 puts("DHCP: INVALID STATE\n");
977 void DhcpRequest(void)
981 #endif /* CONFIG_CMD_DHCP */