2 * Based on LiMon - BOOTP.
4 * Copyright 1994, 1995, 2000 Neil Russell.
6 * Copyright 2000 Roland Borde
7 * Copyright 2000 Paolo Scaffardi
12 #include <bootstage.h>
15 #include <efi_loader.h>
20 #include <linux/delay.h>
23 #ifdef CONFIG_LED_STATUS
24 #include <status_led.h>
26 #ifdef CONFIG_BOOTP_RANDOM_DELAY
30 #define BOOTP_VENDOR_MAGIC 0x63825363 /* RFC1048 Magic Cookie */
33 * The timeout for the initial BOOTP/DHCP request used to be described by a
34 * counter of fixed-length timeout periods. TIMEOUT_COUNT represents
37 * Now that the timeout periods are variable (exponential backoff and retry)
38 * we convert the timeout count to the absolute time it would have take to
39 * execute that many retries, and keep sending retry packets until that time
42 #ifndef CONFIG_NET_RETRY_COUNT
43 # define TIMEOUT_COUNT 5 /* # of timeouts before giving up */
45 # define TIMEOUT_COUNT (CONFIG_NET_RETRY_COUNT)
47 #define TIMEOUT_MS ((3 + (TIMEOUT_COUNT * 5)) * 1000)
49 #define PORT_BOOTPS 67 /* BOOTP server UDP port */
50 #define PORT_BOOTPC 68 /* BOOTP client UDP port */
52 #ifndef CONFIG_DHCP_MIN_EXT_LEN /* minimal length of extension list */
53 #define CONFIG_DHCP_MIN_EXT_LEN 64
56 #ifndef CONFIG_BOOTP_ID_CACHE_SIZE
57 #define CONFIG_BOOTP_ID_CACHE_SIZE 4
60 u32 bootp_ids[CONFIG_BOOTP_ID_CACHE_SIZE];
61 unsigned int bootp_num_ids;
65 char net_nis_domain[32] = {0,}; /* Our NIS domain */
66 char net_hostname[32] = {0,}; /* Our hostname */
67 char net_root_path[64] = {0,}; /* Our bootpath */
69 static ulong time_taken_max;
71 #if defined(CONFIG_CMD_DHCP)
72 static dhcp_state_t dhcp_state = INIT;
73 static u32 dhcp_leasetime;
74 static struct in_addr dhcp_server_ip;
75 static u8 dhcp_option_overload;
76 #define OVERLOAD_FILE 1
77 #define OVERLOAD_SNAME 2
78 static void dhcp_handler(uchar *pkt, unsigned dest, struct in_addr sip,
79 unsigned src, unsigned len);
83 static char *dhcpmsg2str(int type)
86 case 1: return "DHCPDISCOVER"; break;
87 case 2: return "DHCPOFFER"; break;
88 case 3: return "DHCPREQUEST"; break;
89 case 4: return "DHCPDECLINE"; break;
90 case 5: return "DHCPACK"; break;
91 case 6: return "DHCPNACK"; break;
92 case 7: return "DHCPRELEASE"; break;
93 default: return "UNKNOWN/INVALID MSG TYPE"; break;
99 static void bootp_add_id(ulong id)
101 if (bootp_num_ids >= ARRAY_SIZE(bootp_ids)) {
102 size_t size = sizeof(bootp_ids) - sizeof(id);
104 memmove(bootp_ids, &bootp_ids[1], size);
105 bootp_ids[bootp_num_ids - 1] = id;
107 bootp_ids[bootp_num_ids] = id;
112 static bool bootp_match_id(ulong id)
116 for (i = 0; i < bootp_num_ids; i++)
117 if (bootp_ids[i] == id)
123 static int check_reply_packet(uchar *pkt, unsigned dest, unsigned src,
126 struct bootp_hdr *bp = (struct bootp_hdr *)pkt;
129 if (dest != PORT_BOOTPC || src != PORT_BOOTPS)
131 else if (len < sizeof(struct bootp_hdr) - OPT_FIELD_SIZE)
133 else if (bp->bp_op != OP_BOOTREPLY)
135 else if (bp->bp_htype != HWT_ETHER)
137 else if (bp->bp_hlen != HWL_ETHER)
139 else if (!bootp_match_id(net_read_u32(&bp->bp_id)))
141 else if (memcmp(bp->bp_chaddr, net_ethaddr, HWL_ETHER) != 0)
144 debug("Filtering pkt = %d\n", retval);
149 static void store_bootp_params(struct bootp_hdr *bp)
151 struct in_addr tmp_ip;
152 bool overwrite_serverip = true;
154 if (IS_ENABLED(CONFIG_BOOTP_SERVERIP))
157 #if defined(CONFIG_BOOTP_PREFER_SERVERIP)
158 overwrite_serverip = false;
161 net_copy_ip(&tmp_ip, &bp->bp_siaddr);
162 if (tmp_ip.s_addr != 0 && (overwrite_serverip || !net_server_ip.s_addr))
163 net_copy_ip(&net_server_ip, &bp->bp_siaddr);
164 memcpy(net_server_ethaddr,
165 ((struct ethernet_hdr *)net_rx_packet)->et_src, 6);
167 #if defined(CONFIG_CMD_DHCP)
168 !(dhcp_option_overload & OVERLOAD_FILE) &&
170 (strlen(bp->bp_file) > 0) &&
171 !net_boot_file_name_explicit) {
172 copy_filename(net_boot_file_name, bp->bp_file,
173 sizeof(net_boot_file_name));
176 debug("net_boot_file_name: %s\n", net_boot_file_name);
178 /* Propagate to environment:
179 * don't delete exising entry when BOOTP / DHCP reply does
180 * not contain a new value
182 if (*net_boot_file_name)
183 env_set("bootfile", net_boot_file_name);
187 * Copy parameters of interest from BOOTP_REPLY/DHCP_OFFER packet
189 static void store_net_params(struct bootp_hdr *bp)
191 #if !defined(CONFIG_SERVERIP_FROM_PROXYDHCP)
192 store_bootp_params(bp);
194 net_copy_ip(&net_ip, &bp->bp_yiaddr);
197 static int truncate_sz(const char *name, int maxlen, int curlen)
199 if (curlen >= maxlen) {
200 printf("*** WARNING: %s is too long (%d - max: %d)"
201 " - truncated\n", name, curlen, maxlen);
207 #if !defined(CONFIG_CMD_DHCP)
209 static void bootp_process_vendor_field(u8 *ext)
211 int size = *(ext + 1);
213 debug("[BOOTP] Processing extension %d... (%d bytes)\n", *ext,
216 net_boot_file_expected_size_in_blocks = 0;
219 /* Fixed length fields */
220 case 1: /* Subnet mask */
221 if (net_netmask.s_addr == 0)
222 net_copy_ip(&net_netmask, (struct in_addr *)(ext + 2));
224 case 2: /* Time offset - Not yet supported */
226 /* Variable length fields */
227 case 3: /* Gateways list */
228 if (net_gateway.s_addr == 0)
229 net_copy_ip(&net_gateway, (struct in_addr *)(ext + 2));
231 case 4: /* Time server - Not yet supported */
233 case 5: /* IEN-116 name server - Not yet supported */
236 if (net_dns_server.s_addr == 0)
237 net_copy_ip(&net_dns_server,
238 (struct in_addr *)(ext + 2));
239 #if defined(CONFIG_BOOTP_DNS2)
240 if ((net_dns_server2.s_addr == 0) && (size > 4))
241 net_copy_ip(&net_dns_server2,
242 (struct in_addr *)(ext + 2 + 4));
245 case 7: /* Log server - Not yet supported */
247 case 8: /* Cookie/Quote server - Not yet supported */
249 case 9: /* LPR server - Not yet supported */
251 case 10: /* Impress server - Not yet supported */
253 case 11: /* RPL server - Not yet supported */
255 case 12: /* Host name */
256 if (net_hostname[0] == 0) {
257 size = truncate_sz("Host Name",
258 sizeof(net_hostname), size);
259 memcpy(&net_hostname, ext + 2, size);
260 net_hostname[size] = 0;
263 case 13: /* Boot file size */
265 net_boot_file_expected_size_in_blocks =
266 ntohs(*(ushort *)(ext + 2));
268 net_boot_file_expected_size_in_blocks =
269 ntohl(*(ulong *)(ext + 2));
271 case 14: /* Merit dump file - Not yet supported */
273 case 15: /* Domain name - Not yet supported */
275 case 16: /* Swap server - Not yet supported */
277 case 17: /* Root path */
278 if (net_root_path[0] == 0) {
279 size = truncate_sz("Root Path",
280 sizeof(net_root_path), size);
281 memcpy(&net_root_path, ext + 2, size);
282 net_root_path[size] = 0;
285 case 18: /* Extension path - Not yet supported */
287 * This can be used to send the information of the
288 * vendor area in another file that the client can
292 /* IP host layer fields */
293 case 40: /* NIS Domain name */
294 if (net_nis_domain[0] == 0) {
295 size = truncate_sz("NIS Domain Name",
296 sizeof(net_nis_domain), size);
297 memcpy(&net_nis_domain, ext + 2, size);
298 net_nis_domain[size] = 0;
301 #if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_NTPSERVER)
302 case 42: /* NTP server IP */
303 net_copy_ip(&net_ntp_server, (struct in_addr *)(ext + 2));
306 /* Application layer fields */
307 case 43: /* Vendor specific info - Not yet supported */
309 * Binary information to exchange specific
310 * product information.
313 /* Reserved (custom) fields (128..254) */
317 static void bootp_process_vendor(u8 *ext, int size)
319 u8 *end = ext + size;
321 debug("[BOOTP] Checking extension (%d bytes)...\n", size);
323 while ((ext < end) && (*ext != 0xff)) {
331 bootp_process_vendor_field(opt);
335 debug("[BOOTP] Received fields:\n");
336 if (net_netmask.s_addr)
337 debug("net_netmask : %pI4\n", &net_netmask);
339 if (net_gateway.s_addr)
340 debug("net_gateway : %pI4", &net_gateway);
342 if (net_boot_file_expected_size_in_blocks)
343 debug("net_boot_file_expected_size_in_blocks : %d\n",
344 net_boot_file_expected_size_in_blocks);
347 debug("net_hostname : %s\n", net_hostname);
349 if (net_root_path[0])
350 debug("net_root_path : %s\n", net_root_path);
352 if (net_nis_domain[0])
353 debug("net_nis_domain : %s\n", net_nis_domain);
355 #if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_NTPSERVER)
356 if (net_ntp_server.s_addr)
357 debug("net_ntp_server : %pI4\n", &net_ntp_server);
362 * Handle a BOOTP received packet.
364 static void bootp_handler(uchar *pkt, unsigned dest, struct in_addr sip,
365 unsigned src, unsigned len)
367 struct bootp_hdr *bp;
369 debug("got BOOTP packet (src=%d, dst=%d, len=%d want_len=%zu)\n",
370 src, dest, len, sizeof(struct bootp_hdr));
372 bp = (struct bootp_hdr *)pkt;
374 /* Filter out pkts we don't want */
375 if (check_reply_packet(pkt, dest, src, len))
379 * Got a good BOOTP reply. Copy the data into our variables.
381 #if defined(CONFIG_LED_STATUS) && defined(CONFIG_LED_STATUS_BOOT_ENABLE)
382 status_led_set(CONFIG_LED_STATUS_BOOT, CONFIG_LED_STATUS_OFF);
385 store_net_params(bp); /* Store net parameters from reply */
387 /* Retrieve extended information (we must parse the vendor area) */
388 if (net_read_u32((u32 *)&bp->bp_vend[0]) == htonl(BOOTP_VENDOR_MAGIC))
389 bootp_process_vendor((uchar *)&bp->bp_vend[4], len);
391 net_set_timeout_handler(0, (thand_f *)0);
392 bootstage_mark_name(BOOTSTAGE_ID_BOOTP_STOP, "bootp_stop");
394 debug("Got good BOOTP\n");
401 * Timeout on BOOTP/DHCP request.
403 static void bootp_timeout_handler(void)
405 ulong time_taken = get_timer(bootp_start);
407 if (time_taken >= time_taken_max) {
408 #ifdef CONFIG_BOOTP_MAY_FAIL
411 ethrotate = env_get("ethrotate");
412 if ((ethrotate && strcmp(ethrotate, "no") == 0) ||
414 puts("\nRetry time exceeded\n");
415 net_set_state(NETLOOP_FAIL);
419 puts("\nRetry time exceeded; starting again\n");
424 if (bootp_timeout > 2000)
425 bootp_timeout = 2000;
426 net_set_timeout_handler(bootp_timeout, bootp_timeout_handler);
431 #define put_vci(e, str) \
433 size_t vci_strlen = strlen(str); \
434 *e++ = 60; /* Vendor Class Identifier */ \
436 memcpy(e, str, vci_strlen); \
440 static u8 *add_vci(u8 *e)
443 char *env_vci = env_get("bootp_vci");
445 #if defined(CONFIG_SPL_BUILD) && defined(CONFIG_SPL_NET_VCI_STRING)
446 vci = CONFIG_SPL_NET_VCI_STRING;
447 #elif defined(CONFIG_BOOTP_VCI_STRING)
448 vci = CONFIG_BOOTP_VCI_STRING;
461 * Initialize BOOTP extension fields in the request.
463 #if defined(CONFIG_CMD_DHCP)
464 static int dhcp_extended(u8 *e, int message_type, struct in_addr server_ip,
465 struct in_addr requested_ip)
469 #ifdef CONFIG_LIB_UUID
474 #if defined(CONFIG_BOOTP_VENDOREX)
477 #if defined(CONFIG_BOOTP_SEND_HOSTNAME)
481 *e++ = 99; /* RFC1048 Magic Cookie */
486 *e++ = 53; /* DHCP Message Type */
490 *e++ = 57; /* Maximum DHCP Message Size */
492 *e++ = (576 - 312 + OPT_FIELD_SIZE) >> 8;
493 *e++ = (576 - 312 + OPT_FIELD_SIZE) & 0xff;
495 if (server_ip.s_addr) {
496 int tmp = ntohl(server_ip.s_addr);
498 *e++ = 54; /* ServerID */
506 if (requested_ip.s_addr) {
507 int tmp = ntohl(requested_ip.s_addr);
509 *e++ = 50; /* Requested IP */
516 #if defined(CONFIG_BOOTP_SEND_HOSTNAME)
517 hostname = env_get("hostname");
519 int hostnamelen = strlen(hostname);
521 *e++ = 12; /* Hostname */
523 memcpy(e, hostname, hostnamelen);
528 #ifdef CONFIG_BOOTP_PXE_CLIENTARCH
529 clientarch = CONFIG_BOOTP_PXE_CLIENTARCH;
532 if (env_get("bootp_arch"))
533 clientarch = env_get_ulong("bootp_arch", 16, clientarch);
535 if (clientarch > 0) {
536 *e++ = 93; /* Client System Architecture */
538 *e++ = (clientarch >> 8) & 0xff;
539 *e++ = clientarch & 0xff;
542 *e++ = 94; /* Client Network Interface Identifier */
544 *e++ = 1; /* type field for UNDI */
545 *e++ = 0; /* major revision */
546 *e++ = 0; /* minor revision */
548 #ifdef CONFIG_LIB_UUID
549 uuid = env_get("pxeuuid");
552 if (uuid_str_valid(uuid)) {
553 *e++ = 97; /* Client Machine Identifier */
555 *e++ = 0; /* type 0 - UUID */
557 uuid_str_to_bin(uuid, e, UUID_STR_FORMAT_STD);
560 printf("Invalid pxeuuid: %s\n", uuid);
567 #if defined(CONFIG_BOOTP_VENDOREX)
568 x = dhcp_vendorex_prep(e);
573 *e++ = 55; /* Parameter Request List */
574 cnt = e++; /* Pointer to count of requested items */
576 #if defined(CONFIG_BOOTP_SUBNETMASK)
577 *e++ = 1; /* Subnet Mask */
580 #if defined(CONFIG_BOOTP_TIMEOFFSET)
584 #if defined(CONFIG_BOOTP_GATEWAY)
585 *e++ = 3; /* Router Option */
588 #if defined(CONFIG_BOOTP_DNS)
589 *e++ = 6; /* DNS Server(s) */
592 #if defined(CONFIG_BOOTP_HOSTNAME)
593 *e++ = 12; /* Hostname */
596 #if defined(CONFIG_BOOTP_BOOTFILESIZE)
597 *e++ = 13; /* Boot File Size */
600 #if defined(CONFIG_BOOTP_BOOTPATH)
601 *e++ = 17; /* Boot path */
604 #if defined(CONFIG_BOOTP_NISDOMAIN)
605 *e++ = 40; /* NIS Domain name request */
608 #if defined(CONFIG_BOOTP_NTPSERVER)
612 /* no options, so back up to avoid sending an empty request list */
616 *e++ = 255; /* End of the list */
618 /* Pad to minimal length */
619 #ifdef CONFIG_DHCP_MIN_EXT_LEN
620 while ((e - start) < CONFIG_DHCP_MIN_EXT_LEN)
629 * Warning: no field size check - change CONFIG_BOOTP_* at your own risk!
631 static int bootp_extended(u8 *e)
635 *e++ = 99; /* RFC1048 Magic Cookie */
640 #if defined(CONFIG_CMD_DHCP)
641 *e++ = 53; /* DHCP Message Type */
643 *e++ = DHCP_DISCOVER;
645 *e++ = 57; /* Maximum DHCP Message Size */
647 *e++ = (576 - 312 + OPT_FIELD_SIZE) >> 16;
648 *e++ = (576 - 312 + OPT_FIELD_SIZE) & 0xff;
653 #if defined(CONFIG_BOOTP_SUBNETMASK)
654 *e++ = 1; /* Subnet mask request */
659 #if defined(CONFIG_BOOTP_GATEWAY)
660 *e++ = 3; /* Default gateway request */
665 #if defined(CONFIG_BOOTP_DNS)
666 *e++ = 6; /* Domain Name Server */
671 #if defined(CONFIG_BOOTP_HOSTNAME)
672 *e++ = 12; /* Host name request */
677 #if defined(CONFIG_BOOTP_BOOTFILESIZE)
678 *e++ = 13; /* Boot file size */
683 #if defined(CONFIG_BOOTP_BOOTPATH)
684 *e++ = 17; /* Boot path */
689 #if defined(CONFIG_BOOTP_NISDOMAIN)
690 *e++ = 40; /* NIS Domain name request */
694 #if defined(CONFIG_BOOTP_NTPSERVER)
700 *e++ = 255; /* End of the list */
703 * If nothing in list, remove it altogether. Some DHCP servers get
704 * upset by this minor faux pas and do not respond at all.
706 if (e == start + 3) {
707 printf("*** Warning: no DHCP options requested\n");
715 void bootp_reset(void)
719 bootp_start = get_timer(0);
723 void bootp_request(void)
726 struct bootp_hdr *bp;
727 int extlen, pktlen, iplen;
729 #ifdef CONFIG_BOOTP_RANDOM_DELAY
733 struct in_addr zero_ip;
734 struct in_addr bcast_ip;
735 char *ep; /* Environment pointer */
737 bootstage_mark_name(BOOTSTAGE_ID_BOOTP_START, "bootp_start");
738 #if defined(CONFIG_CMD_DHCP)
742 ep = env_get("bootpretryperiod");
744 time_taken_max = dectoul(ep, NULL);
746 time_taken_max = TIMEOUT_MS;
748 #ifdef CONFIG_BOOTP_RANDOM_DELAY /* Random BOOTP delay */
752 if (bootp_try <= 2) /* Start with max 1024 * 1ms */
753 rand_ms = rand() >> (22 - bootp_try);
754 else /* After 3rd BOOTP request max 8192 * 1ms */
755 rand_ms = rand() >> 19;
757 printf("Random delay: %ld ms...\n", rand_ms);
760 #endif /* CONFIG_BOOTP_RANDOM_DELAY */
762 printf("BOOTP broadcast %d\n", ++bootp_try);
764 memset((void *)pkt, 0, PKTSIZE);
766 eth_hdr_size = net_set_ether(pkt, net_bcast_ethaddr, PROT_IP);
770 * Next line results in incorrect packet size being transmitted,
771 * resulting in errors in some DHCP servers, reporting missing bytes.
772 * Size must be set in packet header after extension length has been
774 * C. Hallinan, DS4.COM, Inc.
776 /* net_set_udp_header(pkt, 0xFFFFFFFFL, PORT_BOOTPS, PORT_BOOTPC,
777 sizeof (struct bootp_hdr)); */
778 iphdr = pkt; /* We need this later for net_set_udp_header() */
779 pkt += IP_UDP_HDR_SIZE;
781 bp = (struct bootp_hdr *)pkt;
782 bp->bp_op = OP_BOOTREQUEST;
783 bp->bp_htype = HWT_ETHER;
784 bp->bp_hlen = HWL_ETHER;
787 * according to RFC1542, should be 0 on first request, secs since
788 * first request otherwise
790 bp->bp_secs = htons(get_timer(bootp_start) / 1000);
792 net_write_ip(&bp->bp_ciaddr, zero_ip);
793 net_write_ip(&bp->bp_yiaddr, zero_ip);
794 net_write_ip(&bp->bp_siaddr, zero_ip);
795 net_write_ip(&bp->bp_giaddr, zero_ip);
796 memcpy(bp->bp_chaddr, net_ethaddr, 6);
797 copy_filename(bp->bp_file, net_boot_file_name, sizeof(bp->bp_file));
799 /* Request additional information from the BOOTP/DHCP server */
800 #if defined(CONFIG_CMD_DHCP)
801 extlen = dhcp_extended((u8 *)bp->bp_vend, DHCP_DISCOVER, zero_ip,
804 extlen = bootp_extended((u8 *)bp->bp_vend);
808 * Bootp ID is the lower 4 bytes of our ethernet address
809 * plus the current time in ms.
811 bootp_id = ((u32)net_ethaddr[2] << 24)
812 | ((u32)net_ethaddr[3] << 16)
813 | ((u32)net_ethaddr[4] << 8)
814 | (u32)net_ethaddr[5];
815 bootp_id += get_timer(0);
816 bootp_id = htonl(bootp_id);
817 bootp_add_id(bootp_id);
818 net_copy_u32(&bp->bp_id, &bootp_id);
821 * Calculate proper packet lengths taking into account the
822 * variable size of the options field
824 iplen = BOOTP_HDR_SIZE - OPT_FIELD_SIZE + extlen;
825 pktlen = eth_hdr_size + IP_UDP_HDR_SIZE + iplen;
826 bcast_ip.s_addr = 0xFFFFFFFFL;
827 net_set_udp_header(iphdr, bcast_ip, PORT_BOOTPS, PORT_BOOTPC, iplen);
828 net_set_timeout_handler(bootp_timeout, bootp_timeout_handler);
830 #if defined(CONFIG_CMD_DHCP)
831 dhcp_state = SELECTING;
832 net_set_udp_handler(dhcp_handler);
834 net_set_udp_handler(bootp_handler);
836 net_send_packet(net_tx_packet, pktlen);
839 #if defined(CONFIG_CMD_DHCP)
840 static void dhcp_process_options(uchar *popt, uchar *end)
843 #if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_TIMEOFFSET)
847 while (popt < end && *popt != 0xff) {
851 oplen = -1; /* Pad omits len byte */
854 net_copy_ip(&net_netmask, (popt + 2));
856 #if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_TIMEOFFSET)
857 case 2: /* Time offset */
858 to_ptr = &net_ntp_time_offset;
859 net_copy_u32((u32 *)to_ptr, (u32 *)(popt + 2));
860 net_ntp_time_offset = ntohl(net_ntp_time_offset);
864 net_copy_ip(&net_gateway, (popt + 2));
867 net_copy_ip(&net_dns_server, (popt + 2));
868 #if defined(CONFIG_BOOTP_DNS2)
870 net_copy_ip(&net_dns_server2, (popt + 2 + 4));
874 size = truncate_sz("Host Name",
875 sizeof(net_hostname), oplen);
876 memcpy(&net_hostname, popt + 2, size);
877 net_hostname[size] = 0;
879 case 15: /* Ignore Domain Name Option */
882 size = truncate_sz("Root Path",
883 sizeof(net_root_path), oplen);
884 memcpy(&net_root_path, popt + 2, size);
885 net_root_path[size] = 0;
887 case 28: /* Ignore Broadcast Address Option */
889 #if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_NTPSERVER)
890 case 42: /* NTP server IP */
891 net_copy_ip(&net_ntp_server, (popt + 2));
895 net_copy_u32(&dhcp_leasetime, (u32 *)(popt + 2));
898 dhcp_option_overload = popt[2];
900 case 53: /* Ignore Message Type Option */
903 net_copy_ip(&dhcp_server_ip, (popt + 2));
905 case 58: /* Ignore Renewal Time Option */
907 case 59: /* Ignore Rebinding Time Option */
909 case 66: /* Ignore TFTP server name */
911 case 67: /* Bootfile option */
912 if (!net_boot_file_name_explicit) {
913 size = truncate_sz("Bootfile",
914 sizeof(net_boot_file_name),
916 memcpy(&net_boot_file_name, popt + 2, size);
917 net_boot_file_name[size] = 0;
921 #if defined(CONFIG_BOOTP_VENDOREX)
922 if (dhcp_vendorex_proc(popt))
925 printf("*** Unhandled DHCP Option in OFFER/ACK:"
929 popt += oplen + 2; /* Process next option */
933 static void dhcp_packet_process_options(struct bootp_hdr *bp)
935 uchar *popt = (uchar *)&bp->bp_vend[4];
936 uchar *end = popt + BOOTP_HDR_SIZE;
938 if (net_read_u32((u32 *)&bp->bp_vend[0]) != htonl(BOOTP_VENDOR_MAGIC))
941 dhcp_option_overload = 0;
944 * The 'options' field MUST be interpreted first, 'file' next,
947 dhcp_process_options(popt, end);
949 if (dhcp_option_overload & OVERLOAD_FILE) {
950 popt = (uchar *)bp->bp_file;
951 end = popt + sizeof(bp->bp_file);
952 dhcp_process_options(popt, end);
955 if (dhcp_option_overload & OVERLOAD_SNAME) {
956 popt = (uchar *)bp->bp_sname;
957 end = popt + sizeof(bp->bp_sname);
958 dhcp_process_options(popt, end);
962 static int dhcp_message_type(unsigned char *popt)
964 if (net_read_u32((u32 *)popt) != htonl(BOOTP_VENDOR_MAGIC))
968 while (*popt != 0xff) {
969 if (*popt == 53) /* DHCP Message Type */
975 /* Scan through all options */
976 popt += *(popt + 1) + 2;
982 static void dhcp_send_request_packet(struct bootp_hdr *bp_offer)
985 struct bootp_hdr *bp;
986 int pktlen, iplen, extlen;
988 struct in_addr offered_ip;
989 struct in_addr zero_ip;
990 struct in_addr bcast_ip;
992 debug("dhcp_send_request_packet: Sending DHCPREQUEST\n");
994 memset((void *)pkt, 0, PKTSIZE);
996 eth_hdr_size = net_set_ether(pkt, net_bcast_ethaddr, PROT_IP);
999 iphdr = pkt; /* We'll need this later to set proper pkt size */
1000 pkt += IP_UDP_HDR_SIZE;
1002 bp = (struct bootp_hdr *)pkt;
1003 bp->bp_op = OP_BOOTREQUEST;
1004 bp->bp_htype = HWT_ETHER;
1005 bp->bp_hlen = HWL_ETHER;
1007 bp->bp_secs = htons(get_timer(bootp_start) / 1000);
1008 /* Do not set the client IP, your IP, or server IP yet, since it
1009 * hasn't been ACK'ed by the server yet */
1012 * RFC3046 requires Relay Agents to discard packets with
1013 * nonzero and offered giaddr
1016 net_write_ip(&bp->bp_giaddr, zero_ip);
1018 memcpy(bp->bp_chaddr, net_ethaddr, 6);
1019 copy_filename(bp->bp_file, net_boot_file_name, sizeof(bp->bp_file));
1022 * ID is the id of the OFFER packet
1025 net_copy_u32(&bp->bp_id, &bp_offer->bp_id);
1028 * Copy options from OFFER packet if present
1031 /* Copy offered IP into the parameters request list */
1032 net_copy_ip(&offered_ip, &bp_offer->bp_yiaddr);
1033 extlen = dhcp_extended((u8 *)bp->bp_vend, DHCP_REQUEST,
1034 dhcp_server_ip, offered_ip);
1036 iplen = BOOTP_HDR_SIZE - OPT_FIELD_SIZE + extlen;
1037 pktlen = eth_hdr_size + IP_UDP_HDR_SIZE + iplen;
1038 bcast_ip.s_addr = 0xFFFFFFFFL;
1039 net_set_udp_header(iphdr, bcast_ip, PORT_BOOTPS, PORT_BOOTPC, iplen);
1041 #ifdef CONFIG_BOOTP_DHCP_REQUEST_DELAY
1042 udelay(CONFIG_BOOTP_DHCP_REQUEST_DELAY);
1043 #endif /* CONFIG_BOOTP_DHCP_REQUEST_DELAY */
1044 debug("Transmitting DHCPREQUEST packet: len = %d\n", pktlen);
1045 net_send_packet(net_tx_packet, pktlen);
1049 * Handle DHCP received packets.
1051 static void dhcp_handler(uchar *pkt, unsigned dest, struct in_addr sip,
1052 unsigned src, unsigned len)
1054 struct bootp_hdr *bp = (struct bootp_hdr *)pkt;
1056 debug("DHCPHandler: got packet: (src=%d, dst=%d, len=%d) state: %d\n",
1057 src, dest, len, dhcp_state);
1059 /* Filter out pkts we don't want */
1060 if (check_reply_packet(pkt, dest, src, len))
1063 debug("DHCPHandler: got DHCP packet: (src=%d, dst=%d, len=%d) state: "
1064 "%d\n", src, dest, len, dhcp_state);
1066 if (net_read_ip(&bp->bp_yiaddr).s_addr == 0) {
1067 #if defined(CONFIG_SERVERIP_FROM_PROXYDHCP)
1068 store_bootp_params(bp);
1073 switch (dhcp_state) {
1076 * Wait an appropriate time for any potential DHCPOFFER packets
1077 * to arrive. Then select one, and generate DHCPREQUEST
1078 * response. If filename is in format we recognize, assume it
1079 * is a valid OFFER from a server we want.
1081 debug("DHCP: state=SELECTING bp_file: \"%s\"\n", bp->bp_file);
1082 #ifdef CONFIG_SYS_BOOTFILE_PREFIX
1083 if (strncmp(bp->bp_file,
1084 CONFIG_SYS_BOOTFILE_PREFIX,
1085 strlen(CONFIG_SYS_BOOTFILE_PREFIX)) == 0) {
1086 #endif /* CONFIG_SYS_BOOTFILE_PREFIX */
1087 dhcp_packet_process_options(bp);
1088 efi_net_set_dhcp_ack(pkt, len);
1090 #if defined(CONFIG_SERVERIP_FROM_PROXYDHCP)
1091 if (!net_server_ip.s_addr)
1092 udelay(CONFIG_SERVERIP_FROM_PROXYDHCP_DELAY_MS *
1094 #endif /* CONFIG_SERVERIP_FROM_PROXYDHCP */
1096 debug("TRANSITIONING TO REQUESTING STATE\n");
1097 dhcp_state = REQUESTING;
1099 net_set_timeout_handler(5000, bootp_timeout_handler);
1100 dhcp_send_request_packet(bp);
1101 #ifdef CONFIG_SYS_BOOTFILE_PREFIX
1103 #endif /* CONFIG_SYS_BOOTFILE_PREFIX */
1108 debug("DHCP State: REQUESTING\n");
1110 if (dhcp_message_type((u8 *)bp->bp_vend) == DHCP_ACK) {
1111 dhcp_packet_process_options(bp);
1112 /* Store net params from reply */
1113 store_net_params(bp);
1115 printf("DHCP client bound to address %pI4 (%lu ms)\n",
1116 &net_ip, get_timer(bootp_start));
1117 net_set_timeout_handler(0, (thand_f *)0);
1118 bootstage_mark_name(BOOTSTAGE_ID_BOOTP_STOP,
1126 /* DHCP client bound to address */
1129 puts("DHCP: INVALID STATE\n");
1134 void dhcp_request(void)
1138 #endif /* CONFIG_CMD_DHCP */