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 */
27 * The timeout for the initial BOOTP/DHCP request used to be described by a
28 * counter of fixed-length timeout periods. TIMEOUT_COUNT represents
31 * Now that the timeout periods are variable (exponential backoff and retry)
32 * we convert the timeout count to the absolute time it would have take to
33 * execute that many retries, and keep sending retry packets until that time
36 #ifndef CONFIG_NET_RETRY_COUNT
37 # define TIMEOUT_COUNT 5 /* # of timeouts before giving up */
39 # define TIMEOUT_COUNT (CONFIG_NET_RETRY_COUNT)
41 #define TIMEOUT_MS ((3 + (TIMEOUT_COUNT * 5)) * 1000)
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
55 #if defined(CONFIG_CMD_DHCP)
56 static dhcp_state_t dhcp_state = INIT;
57 static unsigned long dhcp_leasetime;
58 static IPaddr_t NetDHCPServerIP;
59 static void DhcpHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src,
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;
80 static int BootpCheckPkt(uchar *pkt, unsigned dest, unsigned src, unsigned len)
82 struct Bootp_t *bp = (struct Bootp_t *) pkt;
85 if (dest != PORT_BOOTPC || src != PORT_BOOTPS)
87 else if (len < sizeof(struct Bootp_t) - OPT_FIELD_SIZE)
89 else if (bp->bp_op != OP_BOOTREQUEST &&
90 bp->bp_op != OP_BOOTREPLY &&
91 bp->bp_op != DHCP_OFFER &&
92 bp->bp_op != DHCP_ACK &&
93 bp->bp_op != DHCP_NAK)
95 else if (bp->bp_htype != HWT_ETHER)
97 else if (bp->bp_hlen != HWL_ETHER)
99 else if (NetReadLong((ulong *)&bp->bp_id) != BootpID)
102 debug("Filtering pkt = %d\n", retval);
108 * Copy parameters of interest from BOOTP_REPLY/DHCP_OFFER packet
110 static void BootpCopyNetParams(struct Bootp_t *bp)
112 #if !defined(CONFIG_BOOTP_SERVERIP)
115 NetCopyIP(&tmp_ip, &bp->bp_siaddr);
117 NetCopyIP(&NetServerIP, &bp->bp_siaddr);
118 memcpy(NetServerEther, ((struct ethernet_hdr *)NetRxPacket)->et_src, 6);
120 NetCopyIP(&NetOurIP, &bp->bp_yiaddr);
121 if (strlen(bp->bp_file) > 0)
122 copy_filename(BootFile, bp->bp_file, sizeof(BootFile));
124 debug("Bootfile: %s\n", BootFile);
126 /* Propagate to environment:
127 * don't delete exising entry when BOOTP / DHCP reply does
128 * not contain a new value
131 setenv("bootfile", BootFile);
134 static int truncate_sz(const char *name, int maxlen, int curlen)
136 if (curlen >= maxlen) {
137 printf("*** WARNING: %s is too long (%d - max: %d)"
138 " - truncated\n", name, curlen, maxlen);
144 #if !defined(CONFIG_CMD_DHCP)
146 static void BootpVendorFieldProcess(u8 *ext)
148 int size = *(ext + 1);
150 debug("[BOOTP] Processing extension %d... (%d bytes)\n", *ext,
156 /* Fixed length fields */
157 case 1: /* Subnet mask */
158 if (NetOurSubnetMask == 0)
159 NetCopyIP(&NetOurSubnetMask, (IPaddr_t *) (ext + 2));
161 case 2: /* Time offset - Not yet supported */
163 /* Variable length fields */
164 case 3: /* Gateways list */
165 if (NetOurGatewayIP == 0)
166 NetCopyIP(&NetOurGatewayIP, (IPaddr_t *) (ext + 2));
168 case 4: /* Time server - Not yet supported */
170 case 5: /* IEN-116 name server - Not yet supported */
173 if (NetOurDNSIP == 0)
174 NetCopyIP(&NetOurDNSIP, (IPaddr_t *) (ext + 2));
175 #if defined(CONFIG_BOOTP_DNS2)
176 if ((NetOurDNS2IP == 0) && (size > 4))
177 NetCopyIP(&NetOurDNS2IP, (IPaddr_t *) (ext + 2 + 4));
180 case 7: /* Log server - Not yet supported */
182 case 8: /* Cookie/Quote server - Not yet supported */
184 case 9: /* LPR server - Not yet supported */
186 case 10: /* Impress server - Not yet supported */
188 case 11: /* RPL server - Not yet supported */
190 case 12: /* Host name */
191 if (NetOurHostName[0] == 0) {
192 size = truncate_sz("Host Name",
193 sizeof(NetOurHostName), size);
194 memcpy(&NetOurHostName, ext + 2, size);
195 NetOurHostName[size] = 0;
198 case 13: /* Boot file size */
200 NetBootFileSize = ntohs(*(ushort *) (ext + 2));
202 NetBootFileSize = ntohl(*(ulong *) (ext + 2));
204 case 14: /* Merit dump file - Not yet supported */
206 case 15: /* Domain name - Not yet supported */
208 case 16: /* Swap server - Not yet supported */
210 case 17: /* Root path */
211 if (NetOurRootPath[0] == 0) {
212 size = truncate_sz("Root Path",
213 sizeof(NetOurRootPath), size);
214 memcpy(&NetOurRootPath, ext + 2, size);
215 NetOurRootPath[size] = 0;
218 case 18: /* Extension path - Not yet supported */
220 * This can be used to send the information of the
221 * vendor area in another file that the client can
225 /* IP host layer fields */
226 case 40: /* NIS Domain name */
227 if (NetOurNISDomain[0] == 0) {
228 size = truncate_sz("NIS Domain Name",
229 sizeof(NetOurNISDomain), size);
230 memcpy(&NetOurNISDomain, ext + 2, size);
231 NetOurNISDomain[size] = 0;
234 #if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_NTPSERVER)
235 case 42: /* NTP server IP */
236 NetCopyIP(&NetNtpServerIP, (IPaddr_t *) (ext + 2));
239 /* Application layer fields */
240 case 43: /* Vendor specific info - Not yet supported */
242 * Binary information to exchange specific
243 * product information.
246 /* Reserved (custom) fields (128..254) */
250 static void BootpVendorProcess(u8 *ext, int size)
252 u8 *end = ext + size;
254 debug("[BOOTP] Checking extension (%d bytes)...\n", size);
256 while ((ext < end) && (*ext != 0xff)) {
264 BootpVendorFieldProcess(opt);
268 debug("[BOOTP] Received fields:\n");
269 if (NetOurSubnetMask)
270 debug("NetOurSubnetMask : %pI4\n", &NetOurSubnetMask);
273 debug("NetOurGatewayIP : %pI4", &NetOurGatewayIP);
276 debug("NetBootFileSize : %d\n", NetBootFileSize);
278 if (NetOurHostName[0])
279 debug("NetOurHostName : %s\n", NetOurHostName);
281 if (NetOurRootPath[0])
282 debug("NetOurRootPath : %s\n", NetOurRootPath);
284 if (NetOurNISDomain[0])
285 debug("NetOurNISDomain : %s\n", NetOurNISDomain);
288 debug("NetBootFileSize: %d\n", NetBootFileSize);
290 #if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_NTPSERVER)
292 debug("NetNtpServerIP : %pI4\n", &NetNtpServerIP);
297 * Handle a BOOTP received packet.
300 BootpHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src,
305 debug("got BOOTP packet (src=%d, dst=%d, len=%d want_len=%zu)\n",
306 src, dest, len, sizeof(struct Bootp_t));
308 bp = (struct Bootp_t *)pkt;
310 /* Filter out pkts we don't want */
311 if (BootpCheckPkt(pkt, dest, src, len))
315 * Got a good BOOTP reply. Copy the data into our variables.
317 #ifdef CONFIG_STATUS_LED
318 status_led_set(STATUS_LED_BOOT, STATUS_LED_OFF);
321 BootpCopyNetParams(bp); /* Store net parameters from reply */
323 /* Retrieve extended information (we must parse the vendor area) */
324 if (NetReadLong((ulong *)&bp->bp_vend[0]) == htonl(BOOTP_VENDOR_MAGIC))
325 BootpVendorProcess((uchar *)&bp->bp_vend[4], len);
327 NetSetTimeout(0, (thand_f *)0);
328 bootstage_mark_name(BOOTSTAGE_ID_BOOTP_STOP, "bootp_stop");
330 debug("Got good BOOTP\n");
337 * Timeout on BOOTP/DHCP request.
342 ulong time_taken = get_timer(bootp_start);
344 if (time_taken >= TIMEOUT_MS) {
345 #ifdef CONFIG_BOOTP_MAY_FAIL
346 puts("\nRetry time exceeded\n");
347 net_set_state(NETLOOP_FAIL);
349 puts("\nRetry time exceeded; starting again\n");
354 if (bootp_timeout > 1000)
355 bootp_timeout = 1000;
356 NetSetTimeout(bootp_timeout, BootpTimeout);
361 #define put_vci(e, str) \
363 size_t vci_strlen = strlen(str); \
364 *e++ = 60; /* Vendor Class Identifier */ \
366 memcpy(e, str, vci_strlen); \
371 * Initialize BOOTP extension fields in the request.
373 #if defined(CONFIG_CMD_DHCP)
374 static int DhcpExtended(u8 *e, int message_type, IPaddr_t ServerID,
375 IPaddr_t RequestedIP)
379 #if defined(CONFIG_BOOTP_PXE)
384 #if defined(CONFIG_BOOTP_VENDOREX)
387 #if defined(CONFIG_BOOTP_SEND_HOSTNAME)
391 *e++ = 99; /* RFC1048 Magic Cookie */
396 *e++ = 53; /* DHCP Message Type */
400 *e++ = 57; /* Maximum DHCP Message Size */
402 *e++ = (576 - 312 + OPT_FIELD_SIZE) >> 8;
403 *e++ = (576 - 312 + OPT_FIELD_SIZE) & 0xff;
406 int tmp = ntohl(ServerID);
408 *e++ = 54; /* ServerID */
417 int tmp = ntohl(RequestedIP);
419 *e++ = 50; /* Requested IP */
426 #if defined(CONFIG_BOOTP_SEND_HOSTNAME)
427 hostname = getenv("hostname");
429 int hostnamelen = strlen(hostname);
431 *e++ = 12; /* Hostname */
433 memcpy(e, hostname, hostnamelen);
438 #if defined(CONFIG_BOOTP_PXE)
439 clientarch = CONFIG_BOOTP_PXE_CLIENTARCH;
440 *e++ = 93; /* Client System Architecture */
442 *e++ = (clientarch >> 8) & 0xff;
443 *e++ = clientarch & 0xff;
445 *e++ = 94; /* Client Network Interface Identifier */
447 *e++ = 1; /* type field for UNDI */
448 *e++ = 0; /* major revision */
449 *e++ = 0; /* minor revision */
451 uuid = getenv("pxeuuid");
454 if (uuid_str_valid(uuid)) {
455 *e++ = 97; /* Client Machine Identifier */
457 *e++ = 0; /* type 0 - UUID */
459 uuid_str_to_bin(uuid, e, UUID_STR_FORMAT_STD);
462 printf("Invalid pxeuuid: %s\n", uuid);
467 #ifdef CONFIG_BOOTP_VCI_STRING
468 put_vci(e, CONFIG_BOOTP_VCI_STRING);
471 #if defined(CONFIG_BOOTP_VENDOREX)
472 x = dhcp_vendorex_prep(e);
477 *e++ = 55; /* Parameter Request List */
478 cnt = e++; /* Pointer to count of requested items */
480 #if defined(CONFIG_BOOTP_SUBNETMASK)
481 *e++ = 1; /* Subnet Mask */
484 #if defined(CONFIG_BOOTP_TIMEOFFSET)
488 #if defined(CONFIG_BOOTP_GATEWAY)
489 *e++ = 3; /* Router Option */
492 #if defined(CONFIG_BOOTP_DNS)
493 *e++ = 6; /* DNS Server(s) */
496 #if defined(CONFIG_BOOTP_HOSTNAME)
497 *e++ = 12; /* Hostname */
500 #if defined(CONFIG_BOOTP_BOOTFILESIZE)
501 *e++ = 13; /* Boot File Size */
504 #if defined(CONFIG_BOOTP_BOOTPATH)
505 *e++ = 17; /* Boot path */
508 #if defined(CONFIG_BOOTP_NISDOMAIN)
509 *e++ = 40; /* NIS Domain name request */
512 #if defined(CONFIG_BOOTP_NTPSERVER)
516 /* no options, so back up to avoid sending an empty request list */
520 *e++ = 255; /* End of the list */
522 /* Pad to minimal length */
523 #ifdef CONFIG_DHCP_MIN_EXT_LEN
524 while ((e - start) < CONFIG_DHCP_MIN_EXT_LEN)
533 * Warning: no field size check - change CONFIG_BOOTP_* at your own risk!
535 static int BootpExtended(u8 *e)
539 *e++ = 99; /* RFC1048 Magic Cookie */
544 #if defined(CONFIG_CMD_DHCP)
545 *e++ = 53; /* DHCP Message Type */
547 *e++ = DHCP_DISCOVER;
549 *e++ = 57; /* Maximum DHCP Message Size */
551 *e++ = (576 - 312 + OPT_FIELD_SIZE) >> 16;
552 *e++ = (576 - 312 + OPT_FIELD_SIZE) & 0xff;
555 #if defined(CONFIG_BOOTP_VCI_STRING) || \
556 (defined(CONFIG_SPL_BUILD) && defined(CONFIG_SPL_NET_VCI_STRING))
557 #ifdef CONFIG_SPL_BUILD
558 put_vci(e, CONFIG_SPL_NET_VCI_STRING);
560 put_vci(e, CONFIG_BOOTP_VCI_STRING);
564 #if defined(CONFIG_BOOTP_SUBNETMASK)
565 *e++ = 1; /* Subnet mask request */
570 #if defined(CONFIG_BOOTP_GATEWAY)
571 *e++ = 3; /* Default gateway request */
576 #if defined(CONFIG_BOOTP_DNS)
577 *e++ = 6; /* Domain Name Server */
582 #if defined(CONFIG_BOOTP_HOSTNAME)
583 *e++ = 12; /* Host name request */
588 #if defined(CONFIG_BOOTP_BOOTFILESIZE)
589 *e++ = 13; /* Boot file size */
594 #if defined(CONFIG_BOOTP_BOOTPATH)
595 *e++ = 17; /* Boot path */
600 #if defined(CONFIG_BOOTP_NISDOMAIN)
601 *e++ = 40; /* NIS Domain name request */
605 #if defined(CONFIG_BOOTP_NTPSERVER)
611 *e++ = 255; /* End of the list */
617 void BootpReset(void)
620 bootp_start = get_timer(0);
629 int extlen, pktlen, iplen;
631 #ifdef CONFIG_BOOTP_RANDOM_DELAY
635 bootstage_mark_name(BOOTSTAGE_ID_BOOTP_START, "bootp_start");
636 #if defined(CONFIG_CMD_DHCP)
640 #ifdef CONFIG_BOOTP_RANDOM_DELAY /* Random BOOTP delay */
644 if (BootpTry <= 2) /* Start with max 1024 * 1ms */
645 rand_ms = rand() >> (22 - BootpTry);
646 else /* After 3rd BOOTP request max 8192 * 1ms */
647 rand_ms = rand() >> 19;
649 printf("Random delay: %ld ms...\n", rand_ms);
652 #endif /* CONFIG_BOOTP_RANDOM_DELAY */
654 printf("BOOTP broadcast %d\n", ++BootpTry);
656 memset((void *)pkt, 0, PKTSIZE);
658 eth_hdr_size = NetSetEther(pkt, NetBcastAddr, PROT_IP);
662 * Next line results in incorrect packet size being transmitted,
663 * resulting in errors in some DHCP servers, reporting missing bytes.
664 * Size must be set in packet header after extension length has been
666 * C. Hallinan, DS4.COM, Inc.
668 /* net_set_udp_header(pkt, 0xFFFFFFFFL, PORT_BOOTPS, PORT_BOOTPC,
669 sizeof (struct Bootp_t)); */
670 iphdr = pkt; /* We need this later for net_set_udp_header() */
671 pkt += IP_UDP_HDR_SIZE;
673 bp = (struct Bootp_t *)pkt;
674 bp->bp_op = OP_BOOTREQUEST;
675 bp->bp_htype = HWT_ETHER;
676 bp->bp_hlen = HWL_ETHER;
678 bp->bp_secs = htons(get_timer(0) / 1000);
679 NetWriteIP(&bp->bp_ciaddr, 0);
680 NetWriteIP(&bp->bp_yiaddr, 0);
681 NetWriteIP(&bp->bp_siaddr, 0);
682 NetWriteIP(&bp->bp_giaddr, 0);
683 memcpy(bp->bp_chaddr, NetOurEther, 6);
684 copy_filename(bp->bp_file, BootFile, sizeof(bp->bp_file));
686 /* Request additional information from the BOOTP/DHCP server */
687 #if defined(CONFIG_CMD_DHCP)
688 extlen = DhcpExtended((u8 *)bp->bp_vend, DHCP_DISCOVER, 0, 0);
690 extlen = BootpExtended((u8 *)bp->bp_vend);
694 * Bootp ID is the lower 4 bytes of our ethernet address
695 * plus the current time in ms.
697 BootpID = ((ulong)NetOurEther[2] << 24)
698 | ((ulong)NetOurEther[3] << 16)
699 | ((ulong)NetOurEther[4] << 8)
700 | (ulong)NetOurEther[5];
701 BootpID += get_timer(0);
702 BootpID = htonl(BootpID);
703 NetCopyLong(&bp->bp_id, &BootpID);
706 * Calculate proper packet lengths taking into account the
707 * variable size of the options field
709 iplen = BOOTP_HDR_SIZE - OPT_FIELD_SIZE + extlen;
710 pktlen = eth_hdr_size + IP_UDP_HDR_SIZE + iplen;
711 net_set_udp_header(iphdr, 0xFFFFFFFFL, PORT_BOOTPS, PORT_BOOTPC, iplen);
712 NetSetTimeout(bootp_timeout, BootpTimeout);
714 #if defined(CONFIG_CMD_DHCP)
715 dhcp_state = SELECTING;
716 net_set_udp_handler(DhcpHandler);
718 net_set_udp_handler(BootpHandler);
720 NetSendPacket(NetTxPacket, pktlen);
723 #if defined(CONFIG_CMD_DHCP)
724 static void DhcpOptionsProcess(uchar *popt, struct Bootp_t *bp)
726 uchar *end = popt + BOOTP_HDR_SIZE;
728 #if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_TIMEOFFSET)
732 while (popt < end && *popt != 0xff) {
736 NetCopyIP(&NetOurSubnetMask, (popt + 2));
738 #if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_TIMEOFFSET)
739 case 2: /* Time offset */
740 to_ptr = &NetTimeOffset;
741 NetCopyLong((ulong *)to_ptr, (ulong *)(popt + 2));
742 NetTimeOffset = ntohl(NetTimeOffset);
746 NetCopyIP(&NetOurGatewayIP, (popt + 2));
749 NetCopyIP(&NetOurDNSIP, (popt + 2));
750 #if defined(CONFIG_BOOTP_DNS2)
752 NetCopyIP(&NetOurDNS2IP, (popt + 2 + 4));
756 size = truncate_sz("Host Name",
757 sizeof(NetOurHostName), oplen);
758 memcpy(&NetOurHostName, popt + 2, size);
759 NetOurHostName[size] = 0;
761 case 15: /* Ignore Domain Name Option */
764 size = truncate_sz("Root Path",
765 sizeof(NetOurRootPath), oplen);
766 memcpy(&NetOurRootPath, popt + 2, size);
767 NetOurRootPath[size] = 0;
769 case 28: /* Ignore Broadcast Address Option */
771 #if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_NTPSERVER)
772 case 42: /* NTP server IP */
773 NetCopyIP(&NetNtpServerIP, (popt + 2));
777 NetCopyLong(&dhcp_leasetime, (ulong *) (popt + 2));
779 case 53: /* Ignore Message Type Option */
782 NetCopyIP(&NetDHCPServerIP, (popt + 2));
784 case 58: /* Ignore Renewal Time Option */
786 case 59: /* Ignore Rebinding Time Option */
788 case 66: /* Ignore TFTP server name */
790 case 67: /* vendor opt bootfile */
792 * I can't use dhcp_vendorex_proc here because I need
793 * to write into the bootp packet - even then I had to
794 * pass the bootp packet pointer into here as the
797 size = truncate_sz("Opt Boot File",
800 if (bp->bp_file[0] == '\0' && size > 0) {
802 * only use vendor boot file if we didn't
803 * receive a boot file in the main non-vendor
804 * part of the packet - god only knows why
805 * some vendors chose not to use this perfectly
806 * good spot to store the boot file (join on
807 * Tru64 Unix) it seems mind bogglingly crazy
810 printf("*** WARNING: using vendor "
811 "optional boot file\n");
812 memcpy(bp->bp_file, popt + 2, size);
813 bp->bp_file[size] = '\0';
817 #if defined(CONFIG_BOOTP_VENDOREX)
818 if (dhcp_vendorex_proc(popt))
821 printf("*** Unhandled DHCP Option in OFFER/ACK:"
825 popt += oplen + 2; /* Process next option */
829 static int DhcpMessageType(unsigned char *popt)
831 if (NetReadLong((ulong *)popt) != htonl(BOOTP_VENDOR_MAGIC))
835 while (*popt != 0xff) {
836 if (*popt == 53) /* DHCP Message Type */
838 popt += *(popt + 1) + 2; /* Scan through all options */
843 static void DhcpSendRequestPkt(struct Bootp_t *bp_offer)
847 int pktlen, iplen, extlen;
851 debug("DhcpSendRequestPkt: Sending DHCPREQUEST\n");
853 memset((void *)pkt, 0, PKTSIZE);
855 eth_hdr_size = NetSetEther(pkt, NetBcastAddr, PROT_IP);
858 iphdr = pkt; /* We'll need this later to set proper pkt size */
859 pkt += IP_UDP_HDR_SIZE;
861 bp = (struct Bootp_t *)pkt;
862 bp->bp_op = OP_BOOTREQUEST;
863 bp->bp_htype = HWT_ETHER;
864 bp->bp_hlen = HWL_ETHER;
866 bp->bp_secs = htons(get_timer(0) / 1000);
867 /* Do not set the client IP, your IP, or server IP yet, since it
868 * hasn't been ACK'ed by the server yet */
871 * RFC3046 requires Relay Agents to discard packets with
872 * nonzero and offered giaddr
874 NetWriteIP(&bp->bp_giaddr, 0);
876 memcpy(bp->bp_chaddr, NetOurEther, 6);
879 * ID is the id of the OFFER packet
882 NetCopyLong(&bp->bp_id, &bp_offer->bp_id);
885 * Copy options from OFFER packet if present
888 /* Copy offered IP into the parameters request list */
889 NetCopyIP(&OfferedIP, &bp_offer->bp_yiaddr);
890 extlen = DhcpExtended((u8 *)bp->bp_vend, DHCP_REQUEST,
891 NetDHCPServerIP, OfferedIP);
893 iplen = BOOTP_HDR_SIZE - OPT_FIELD_SIZE + extlen;
894 pktlen = eth_hdr_size + IP_UDP_HDR_SIZE + iplen;
895 net_set_udp_header(iphdr, 0xFFFFFFFFL, PORT_BOOTPS, PORT_BOOTPC, iplen);
897 #ifdef CONFIG_BOOTP_DHCP_REQUEST_DELAY
898 udelay(CONFIG_BOOTP_DHCP_REQUEST_DELAY);
899 #endif /* CONFIG_BOOTP_DHCP_REQUEST_DELAY */
900 debug("Transmitting DHCPREQUEST packet: len = %d\n", pktlen);
901 NetSendPacket(NetTxPacket, pktlen);
905 * Handle DHCP received packets.
908 DhcpHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src,
911 struct Bootp_t *bp = (struct Bootp_t *)pkt;
913 debug("DHCPHandler: got packet: (src=%d, dst=%d, len=%d) state: %d\n",
914 src, dest, len, dhcp_state);
916 /* Filter out pkts we don't want */
917 if (BootpCheckPkt(pkt, dest, src, len))
920 debug("DHCPHandler: got DHCP packet: (src=%d, dst=%d, len=%d) state:"
921 " %d\n", src, dest, len, dhcp_state);
923 switch (dhcp_state) {
926 * Wait an appropriate time for any potential DHCPOFFER packets
927 * to arrive. Then select one, and generate DHCPREQUEST
928 * response. If filename is in format we recognize, assume it
929 * is a valid OFFER from a server we want.
931 debug("DHCP: state=SELECTING bp_file: \"%s\"\n", bp->bp_file);
932 #ifdef CONFIG_SYS_BOOTFILE_PREFIX
933 if (strncmp(bp->bp_file,
934 CONFIG_SYS_BOOTFILE_PREFIX,
935 strlen(CONFIG_SYS_BOOTFILE_PREFIX)) == 0) {
936 #endif /* CONFIG_SYS_BOOTFILE_PREFIX */
938 debug("TRANSITIONING TO REQUESTING STATE\n");
939 dhcp_state = REQUESTING;
941 if (NetReadLong((ulong *)&bp->bp_vend[0]) ==
942 htonl(BOOTP_VENDOR_MAGIC))
943 DhcpOptionsProcess((u8 *)&bp->bp_vend[4], bp);
945 NetSetTimeout(5000, BootpTimeout);
946 DhcpSendRequestPkt(bp);
947 #ifdef CONFIG_SYS_BOOTFILE_PREFIX
949 #endif /* CONFIG_SYS_BOOTFILE_PREFIX */
954 debug("DHCP State: REQUESTING\n");
956 if (DhcpMessageType((u8 *)bp->bp_vend) == DHCP_ACK) {
957 if (NetReadLong((ulong *)&bp->bp_vend[0]) ==
958 htonl(BOOTP_VENDOR_MAGIC))
959 DhcpOptionsProcess((u8 *)&bp->bp_vend[4], bp);
960 /* Store net params from reply */
961 BootpCopyNetParams(bp);
963 printf("DHCP client bound to address %pI4\n",
965 bootstage_mark_name(BOOTSTAGE_ID_BOOTP_STOP,
973 /* DHCP client bound to address */
976 puts("DHCP: INVALID STATE\n");
982 void DhcpRequest(void)
986 #endif /* CONFIG_CMD_DHCP */