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
14 #include <efi_loader.h>
19 #ifdef CONFIG_LED_STATUS
20 #include <status_led.h>
22 #ifdef CONFIG_BOOTP_RANDOM_DELAY
26 #define BOOTP_VENDOR_MAGIC 0x63825363 /* RFC1048 Magic Cookie */
29 * The timeout for the initial BOOTP/DHCP request used to be described by a
30 * counter of fixed-length timeout periods. TIMEOUT_COUNT represents
33 * Now that the timeout periods are variable (exponential backoff and retry)
34 * we convert the timeout count to the absolute time it would have take to
35 * execute that many retries, and keep sending retry packets until that time
38 #ifndef CONFIG_NET_RETRY_COUNT
39 # define TIMEOUT_COUNT 5 /* # of timeouts before giving up */
41 # define TIMEOUT_COUNT (CONFIG_NET_RETRY_COUNT)
43 #define TIMEOUT_MS ((3 + (TIMEOUT_COUNT * 5)) * 1000)
45 #define PORT_BOOTPS 67 /* BOOTP server UDP port */
46 #define PORT_BOOTPC 68 /* BOOTP client UDP port */
48 #ifndef CONFIG_DHCP_MIN_EXT_LEN /* minimal length of extension list */
49 #define CONFIG_DHCP_MIN_EXT_LEN 64
52 #ifndef CONFIG_BOOTP_ID_CACHE_SIZE
53 #define CONFIG_BOOTP_ID_CACHE_SIZE 4
56 u32 bootp_ids[CONFIG_BOOTP_ID_CACHE_SIZE];
57 unsigned int bootp_num_ids;
61 char net_nis_domain[32] = {0,}; /* Our NIS domain */
62 char net_hostname[32] = {0,}; /* Our hostname */
63 char net_root_path[64] = {0,}; /* Our bootpath */
65 static ulong time_taken_max;
67 #if defined(CONFIG_CMD_DHCP)
68 static dhcp_state_t dhcp_state = INIT;
69 static u32 dhcp_leasetime;
70 static struct in_addr dhcp_server_ip;
71 static u8 dhcp_option_overload;
72 #define OVERLOAD_FILE 1
73 #define OVERLOAD_SNAME 2
74 static void dhcp_handler(uchar *pkt, unsigned dest, struct in_addr sip,
75 unsigned src, unsigned len);
79 static char *dhcpmsg2str(int type)
82 case 1: return "DHCPDISCOVER"; break;
83 case 2: return "DHCPOFFER"; break;
84 case 3: return "DHCPREQUEST"; break;
85 case 4: return "DHCPDECLINE"; break;
86 case 5: return "DHCPACK"; break;
87 case 6: return "DHCPNACK"; break;
88 case 7: return "DHCPRELEASE"; break;
89 default: return "UNKNOWN/INVALID MSG TYPE"; break;
95 static void bootp_add_id(ulong id)
97 if (bootp_num_ids >= ARRAY_SIZE(bootp_ids)) {
98 size_t size = sizeof(bootp_ids) - sizeof(id);
100 memmove(bootp_ids, &bootp_ids[1], size);
101 bootp_ids[bootp_num_ids - 1] = id;
103 bootp_ids[bootp_num_ids] = id;
108 static bool bootp_match_id(ulong id)
112 for (i = 0; i < bootp_num_ids; i++)
113 if (bootp_ids[i] == id)
119 static int check_reply_packet(uchar *pkt, unsigned dest, unsigned src,
122 struct bootp_hdr *bp = (struct bootp_hdr *)pkt;
125 if (dest != PORT_BOOTPC || src != PORT_BOOTPS)
127 else if (len < sizeof(struct bootp_hdr) - OPT_FIELD_SIZE)
129 else if (bp->bp_op != OP_BOOTREPLY)
131 else if (bp->bp_htype != HWT_ETHER)
133 else if (bp->bp_hlen != HWL_ETHER)
135 else if (!bootp_match_id(net_read_u32(&bp->bp_id)))
137 else if (memcmp(bp->bp_chaddr, net_ethaddr, HWL_ETHER) != 0)
140 debug("Filtering pkt = %d\n", retval);
146 * Copy parameters of interest from BOOTP_REPLY/DHCP_OFFER packet
148 static void store_net_params(struct bootp_hdr *bp)
150 #if !defined(CONFIG_BOOTP_SERVERIP)
151 struct in_addr tmp_ip;
152 bool overwrite_serverip = true;
154 #if defined(CONFIG_BOOTP_PREFER_SERVERIP)
155 overwrite_serverip = false;
158 net_copy_ip(&tmp_ip, &bp->bp_siaddr);
159 if (tmp_ip.s_addr != 0 && (overwrite_serverip || !net_server_ip.s_addr))
160 net_copy_ip(&net_server_ip, &bp->bp_siaddr);
161 memcpy(net_server_ethaddr,
162 ((struct ethernet_hdr *)net_rx_packet)->et_src, 6);
164 #if defined(CONFIG_CMD_DHCP)
165 !(dhcp_option_overload & OVERLOAD_FILE) &&
167 (strlen(bp->bp_file) > 0) &&
168 !net_boot_file_name_explicit) {
169 copy_filename(net_boot_file_name, bp->bp_file,
170 sizeof(net_boot_file_name));
173 debug("net_boot_file_name: %s\n", net_boot_file_name);
175 /* Propagate to environment:
176 * don't delete exising entry when BOOTP / DHCP reply does
177 * not contain a new value
179 if (*net_boot_file_name)
180 env_set("bootfile", net_boot_file_name);
182 net_copy_ip(&net_ip, &bp->bp_yiaddr);
185 static int truncate_sz(const char *name, int maxlen, int curlen)
187 if (curlen >= maxlen) {
188 printf("*** WARNING: %s is too long (%d - max: %d)"
189 " - truncated\n", name, curlen, maxlen);
195 #if !defined(CONFIG_CMD_DHCP)
197 static void bootp_process_vendor_field(u8 *ext)
199 int size = *(ext + 1);
201 debug("[BOOTP] Processing extension %d... (%d bytes)\n", *ext,
204 net_boot_file_expected_size_in_blocks = 0;
207 /* Fixed length fields */
208 case 1: /* Subnet mask */
209 if (net_netmask.s_addr == 0)
210 net_copy_ip(&net_netmask, (struct in_addr *)(ext + 2));
212 case 2: /* Time offset - Not yet supported */
214 /* Variable length fields */
215 case 3: /* Gateways list */
216 if (net_gateway.s_addr == 0)
217 net_copy_ip(&net_gateway, (struct in_addr *)(ext + 2));
219 case 4: /* Time server - Not yet supported */
221 case 5: /* IEN-116 name server - Not yet supported */
224 if (net_dns_server.s_addr == 0)
225 net_copy_ip(&net_dns_server,
226 (struct in_addr *)(ext + 2));
227 #if defined(CONFIG_BOOTP_DNS2)
228 if ((net_dns_server2.s_addr == 0) && (size > 4))
229 net_copy_ip(&net_dns_server2,
230 (struct in_addr *)(ext + 2 + 4));
233 case 7: /* Log server - Not yet supported */
235 case 8: /* Cookie/Quote server - Not yet supported */
237 case 9: /* LPR server - Not yet supported */
239 case 10: /* Impress server - Not yet supported */
241 case 11: /* RPL server - Not yet supported */
243 case 12: /* Host name */
244 if (net_hostname[0] == 0) {
245 size = truncate_sz("Host Name",
246 sizeof(net_hostname), size);
247 memcpy(&net_hostname, ext + 2, size);
248 net_hostname[size] = 0;
251 case 13: /* Boot file size */
253 net_boot_file_expected_size_in_blocks =
254 ntohs(*(ushort *)(ext + 2));
256 net_boot_file_expected_size_in_blocks =
257 ntohl(*(ulong *)(ext + 2));
259 case 14: /* Merit dump file - Not yet supported */
261 case 15: /* Domain name - Not yet supported */
263 case 16: /* Swap server - Not yet supported */
265 case 17: /* Root path */
266 if (net_root_path[0] == 0) {
267 size = truncate_sz("Root Path",
268 sizeof(net_root_path), size);
269 memcpy(&net_root_path, ext + 2, size);
270 net_root_path[size] = 0;
273 case 18: /* Extension path - Not yet supported */
275 * This can be used to send the information of the
276 * vendor area in another file that the client can
280 /* IP host layer fields */
281 case 40: /* NIS Domain name */
282 if (net_nis_domain[0] == 0) {
283 size = truncate_sz("NIS Domain Name",
284 sizeof(net_nis_domain), size);
285 memcpy(&net_nis_domain, ext + 2, size);
286 net_nis_domain[size] = 0;
289 #if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_NTPSERVER)
290 case 42: /* NTP server IP */
291 net_copy_ip(&net_ntp_server, (struct in_addr *)(ext + 2));
294 /* Application layer fields */
295 case 43: /* Vendor specific info - Not yet supported */
297 * Binary information to exchange specific
298 * product information.
301 /* Reserved (custom) fields (128..254) */
305 static void bootp_process_vendor(u8 *ext, int size)
307 u8 *end = ext + size;
309 debug("[BOOTP] Checking extension (%d bytes)...\n", size);
311 while ((ext < end) && (*ext != 0xff)) {
319 bootp_process_vendor_field(opt);
323 debug("[BOOTP] Received fields:\n");
324 if (net_netmask.s_addr)
325 debug("net_netmask : %pI4\n", &net_netmask);
327 if (net_gateway.s_addr)
328 debug("net_gateway : %pI4", &net_gateway);
330 if (net_boot_file_expected_size_in_blocks)
331 debug("net_boot_file_expected_size_in_blocks : %d\n",
332 net_boot_file_expected_size_in_blocks);
335 debug("net_hostname : %s\n", net_hostname);
337 if (net_root_path[0])
338 debug("net_root_path : %s\n", net_root_path);
340 if (net_nis_domain[0])
341 debug("net_nis_domain : %s\n", net_nis_domain);
343 #if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_NTPSERVER)
344 if (net_ntp_server.s_addr)
345 debug("net_ntp_server : %pI4\n", &net_ntp_server);
350 * Handle a BOOTP received packet.
352 static void bootp_handler(uchar *pkt, unsigned dest, struct in_addr sip,
353 unsigned src, unsigned len)
355 struct bootp_hdr *bp;
357 debug("got BOOTP packet (src=%d, dst=%d, len=%d want_len=%zu)\n",
358 src, dest, len, sizeof(struct bootp_hdr));
360 bp = (struct bootp_hdr *)pkt;
362 /* Filter out pkts we don't want */
363 if (check_reply_packet(pkt, dest, src, len))
367 * Got a good BOOTP reply. Copy the data into our variables.
369 #if defined(CONFIG_LED_STATUS) && defined(CONFIG_LED_STATUS_BOOT_ENABLE)
370 status_led_set(CONFIG_LED_STATUS_BOOT, CONFIG_LED_STATUS_OFF);
373 store_net_params(bp); /* Store net parameters from reply */
375 /* Retrieve extended information (we must parse the vendor area) */
376 if (net_read_u32((u32 *)&bp->bp_vend[0]) == htonl(BOOTP_VENDOR_MAGIC))
377 bootp_process_vendor((uchar *)&bp->bp_vend[4], len);
379 net_set_timeout_handler(0, (thand_f *)0);
380 bootstage_mark_name(BOOTSTAGE_ID_BOOTP_STOP, "bootp_stop");
382 debug("Got good BOOTP\n");
389 * Timeout on BOOTP/DHCP request.
391 static void bootp_timeout_handler(void)
393 ulong time_taken = get_timer(bootp_start);
395 if (time_taken >= time_taken_max) {
396 #ifdef CONFIG_BOOTP_MAY_FAIL
399 ethrotate = env_get("ethrotate");
400 if ((ethrotate && strcmp(ethrotate, "no") == 0) ||
402 puts("\nRetry time exceeded\n");
403 net_set_state(NETLOOP_FAIL);
407 puts("\nRetry time exceeded; starting again\n");
412 if (bootp_timeout > 2000)
413 bootp_timeout = 2000;
414 net_set_timeout_handler(bootp_timeout, bootp_timeout_handler);
419 #define put_vci(e, str) \
421 size_t vci_strlen = strlen(str); \
422 *e++ = 60; /* Vendor Class Identifier */ \
424 memcpy(e, str, vci_strlen); \
428 static u8 *add_vci(u8 *e)
431 char *env_vci = env_get("bootp_vci");
433 #if defined(CONFIG_SPL_BUILD) && defined(CONFIG_SPL_NET_VCI_STRING)
434 vci = CONFIG_SPL_NET_VCI_STRING;
435 #elif defined(CONFIG_BOOTP_VCI_STRING)
436 vci = CONFIG_BOOTP_VCI_STRING;
449 * Initialize BOOTP extension fields in the request.
451 #if defined(CONFIG_CMD_DHCP)
452 static int dhcp_extended(u8 *e, int message_type, struct in_addr server_ip,
453 struct in_addr requested_ip)
457 #ifdef CONFIG_LIB_UUID
462 #if defined(CONFIG_BOOTP_VENDOREX)
465 #if defined(CONFIG_BOOTP_SEND_HOSTNAME)
469 *e++ = 99; /* RFC1048 Magic Cookie */
474 *e++ = 53; /* DHCP Message Type */
478 *e++ = 57; /* Maximum DHCP Message Size */
480 *e++ = (576 - 312 + OPT_FIELD_SIZE) >> 8;
481 *e++ = (576 - 312 + OPT_FIELD_SIZE) & 0xff;
483 if (server_ip.s_addr) {
484 int tmp = ntohl(server_ip.s_addr);
486 *e++ = 54; /* ServerID */
494 if (requested_ip.s_addr) {
495 int tmp = ntohl(requested_ip.s_addr);
497 *e++ = 50; /* Requested IP */
504 #if defined(CONFIG_BOOTP_SEND_HOSTNAME)
505 hostname = env_get("hostname");
507 int hostnamelen = strlen(hostname);
509 *e++ = 12; /* Hostname */
511 memcpy(e, hostname, hostnamelen);
516 #ifdef CONFIG_BOOTP_PXE_CLIENTARCH
517 clientarch = CONFIG_BOOTP_PXE_CLIENTARCH;
520 if (env_get("bootp_arch"))
521 clientarch = env_get_ulong("bootp_arch", 16, clientarch);
523 if (clientarch > 0) {
524 *e++ = 93; /* Client System Architecture */
526 *e++ = (clientarch >> 8) & 0xff;
527 *e++ = clientarch & 0xff;
530 *e++ = 94; /* Client Network Interface Identifier */
532 *e++ = 1; /* type field for UNDI */
533 *e++ = 0; /* major revision */
534 *e++ = 0; /* minor revision */
536 #ifdef CONFIG_LIB_UUID
537 uuid = env_get("pxeuuid");
540 if (uuid_str_valid(uuid)) {
541 *e++ = 97; /* Client Machine Identifier */
543 *e++ = 0; /* type 0 - UUID */
545 uuid_str_to_bin(uuid, e, UUID_STR_FORMAT_STD);
548 printf("Invalid pxeuuid: %s\n", uuid);
555 #if defined(CONFIG_BOOTP_VENDOREX)
556 x = dhcp_vendorex_prep(e);
561 *e++ = 55; /* Parameter Request List */
562 cnt = e++; /* Pointer to count of requested items */
564 #if defined(CONFIG_BOOTP_SUBNETMASK)
565 *e++ = 1; /* Subnet Mask */
568 #if defined(CONFIG_BOOTP_TIMEOFFSET)
572 #if defined(CONFIG_BOOTP_GATEWAY)
573 *e++ = 3; /* Router Option */
576 #if defined(CONFIG_BOOTP_DNS)
577 *e++ = 6; /* DNS Server(s) */
580 #if defined(CONFIG_BOOTP_HOSTNAME)
581 *e++ = 12; /* Hostname */
584 #if defined(CONFIG_BOOTP_BOOTFILESIZE)
585 *e++ = 13; /* Boot File Size */
588 #if defined(CONFIG_BOOTP_BOOTPATH)
589 *e++ = 17; /* Boot path */
592 #if defined(CONFIG_BOOTP_NISDOMAIN)
593 *e++ = 40; /* NIS Domain name request */
596 #if defined(CONFIG_BOOTP_NTPSERVER)
600 /* no options, so back up to avoid sending an empty request list */
604 *e++ = 255; /* End of the list */
606 /* Pad to minimal length */
607 #ifdef CONFIG_DHCP_MIN_EXT_LEN
608 while ((e - start) < CONFIG_DHCP_MIN_EXT_LEN)
617 * Warning: no field size check - change CONFIG_BOOTP_* at your own risk!
619 static int bootp_extended(u8 *e)
623 *e++ = 99; /* RFC1048 Magic Cookie */
628 #if defined(CONFIG_CMD_DHCP)
629 *e++ = 53; /* DHCP Message Type */
631 *e++ = DHCP_DISCOVER;
633 *e++ = 57; /* Maximum DHCP Message Size */
635 *e++ = (576 - 312 + OPT_FIELD_SIZE) >> 16;
636 *e++ = (576 - 312 + OPT_FIELD_SIZE) & 0xff;
641 #if defined(CONFIG_BOOTP_SUBNETMASK)
642 *e++ = 1; /* Subnet mask request */
647 #if defined(CONFIG_BOOTP_GATEWAY)
648 *e++ = 3; /* Default gateway request */
653 #if defined(CONFIG_BOOTP_DNS)
654 *e++ = 6; /* Domain Name Server */
659 #if defined(CONFIG_BOOTP_HOSTNAME)
660 *e++ = 12; /* Host name request */
665 #if defined(CONFIG_BOOTP_BOOTFILESIZE)
666 *e++ = 13; /* Boot file size */
671 #if defined(CONFIG_BOOTP_BOOTPATH)
672 *e++ = 17; /* Boot path */
677 #if defined(CONFIG_BOOTP_NISDOMAIN)
678 *e++ = 40; /* NIS Domain name request */
682 #if defined(CONFIG_BOOTP_NTPSERVER)
688 *e++ = 255; /* End of the list */
691 * If nothing in list, remove it altogether. Some DHCP servers get
692 * upset by this minor faux pas and do not respond at all.
694 if (e == start + 3) {
695 printf("*** Warning: no DHCP options requested\n");
703 void bootp_reset(void)
707 bootp_start = get_timer(0);
711 void bootp_request(void)
714 struct bootp_hdr *bp;
715 int extlen, pktlen, iplen;
717 #ifdef CONFIG_BOOTP_RANDOM_DELAY
721 struct in_addr zero_ip;
722 struct in_addr bcast_ip;
723 char *ep; /* Environment pointer */
725 bootstage_mark_name(BOOTSTAGE_ID_BOOTP_START, "bootp_start");
726 #if defined(CONFIG_CMD_DHCP)
730 ep = env_get("bootpretryperiod");
732 time_taken_max = simple_strtoul(ep, NULL, 10);
734 time_taken_max = TIMEOUT_MS;
736 #ifdef CONFIG_BOOTP_RANDOM_DELAY /* Random BOOTP delay */
740 if (bootp_try <= 2) /* Start with max 1024 * 1ms */
741 rand_ms = rand() >> (22 - bootp_try);
742 else /* After 3rd BOOTP request max 8192 * 1ms */
743 rand_ms = rand() >> 19;
745 printf("Random delay: %ld ms...\n", rand_ms);
748 #endif /* CONFIG_BOOTP_RANDOM_DELAY */
750 printf("BOOTP broadcast %d\n", ++bootp_try);
752 memset((void *)pkt, 0, PKTSIZE);
754 eth_hdr_size = net_set_ether(pkt, net_bcast_ethaddr, PROT_IP);
758 * Next line results in incorrect packet size being transmitted,
759 * resulting in errors in some DHCP servers, reporting missing bytes.
760 * Size must be set in packet header after extension length has been
762 * C. Hallinan, DS4.COM, Inc.
764 /* net_set_udp_header(pkt, 0xFFFFFFFFL, PORT_BOOTPS, PORT_BOOTPC,
765 sizeof (struct bootp_hdr)); */
766 iphdr = pkt; /* We need this later for net_set_udp_header() */
767 pkt += IP_UDP_HDR_SIZE;
769 bp = (struct bootp_hdr *)pkt;
770 bp->bp_op = OP_BOOTREQUEST;
771 bp->bp_htype = HWT_ETHER;
772 bp->bp_hlen = HWL_ETHER;
775 * according to RFC1542, should be 0 on first request, secs since
776 * first request otherwise
778 bp->bp_secs = htons(get_timer(bootp_start) / 1000);
780 net_write_ip(&bp->bp_ciaddr, zero_ip);
781 net_write_ip(&bp->bp_yiaddr, zero_ip);
782 net_write_ip(&bp->bp_siaddr, zero_ip);
783 net_write_ip(&bp->bp_giaddr, zero_ip);
784 memcpy(bp->bp_chaddr, net_ethaddr, 6);
785 copy_filename(bp->bp_file, net_boot_file_name, sizeof(bp->bp_file));
787 /* Request additional information from the BOOTP/DHCP server */
788 #if defined(CONFIG_CMD_DHCP)
789 extlen = dhcp_extended((u8 *)bp->bp_vend, DHCP_DISCOVER, zero_ip,
792 extlen = bootp_extended((u8 *)bp->bp_vend);
796 * Bootp ID is the lower 4 bytes of our ethernet address
797 * plus the current time in ms.
799 bootp_id = ((u32)net_ethaddr[2] << 24)
800 | ((u32)net_ethaddr[3] << 16)
801 | ((u32)net_ethaddr[4] << 8)
802 | (u32)net_ethaddr[5];
803 bootp_id += get_timer(0);
804 bootp_id = htonl(bootp_id);
805 bootp_add_id(bootp_id);
806 net_copy_u32(&bp->bp_id, &bootp_id);
809 * Calculate proper packet lengths taking into account the
810 * variable size of the options field
812 iplen = BOOTP_HDR_SIZE - OPT_FIELD_SIZE + extlen;
813 pktlen = eth_hdr_size + IP_UDP_HDR_SIZE + iplen;
814 bcast_ip.s_addr = 0xFFFFFFFFL;
815 net_set_udp_header(iphdr, bcast_ip, PORT_BOOTPS, PORT_BOOTPC, iplen);
816 net_set_timeout_handler(bootp_timeout, bootp_timeout_handler);
818 #if defined(CONFIG_CMD_DHCP)
819 dhcp_state = SELECTING;
820 net_set_udp_handler(dhcp_handler);
822 net_set_udp_handler(bootp_handler);
824 net_send_packet(net_tx_packet, pktlen);
827 #if defined(CONFIG_CMD_DHCP)
828 static void dhcp_process_options(uchar *popt, uchar *end)
831 #if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_TIMEOFFSET)
835 while (popt < end && *popt != 0xff) {
839 oplen = -1; /* Pad omits len byte */
842 net_copy_ip(&net_netmask, (popt + 2));
844 #if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_TIMEOFFSET)
845 case 2: /* Time offset */
846 to_ptr = &net_ntp_time_offset;
847 net_copy_u32((u32 *)to_ptr, (u32 *)(popt + 2));
848 net_ntp_time_offset = ntohl(net_ntp_time_offset);
852 net_copy_ip(&net_gateway, (popt + 2));
855 net_copy_ip(&net_dns_server, (popt + 2));
856 #if defined(CONFIG_BOOTP_DNS2)
858 net_copy_ip(&net_dns_server2, (popt + 2 + 4));
862 size = truncate_sz("Host Name",
863 sizeof(net_hostname), oplen);
864 memcpy(&net_hostname, popt + 2, size);
865 net_hostname[size] = 0;
867 case 15: /* Ignore Domain Name Option */
870 size = truncate_sz("Root Path",
871 sizeof(net_root_path), oplen);
872 memcpy(&net_root_path, popt + 2, size);
873 net_root_path[size] = 0;
875 case 28: /* Ignore Broadcast Address Option */
877 #if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_NTPSERVER)
878 case 42: /* NTP server IP */
879 net_copy_ip(&net_ntp_server, (popt + 2));
883 net_copy_u32(&dhcp_leasetime, (u32 *)(popt + 2));
886 dhcp_option_overload = popt[2];
888 case 53: /* Ignore Message Type Option */
891 net_copy_ip(&dhcp_server_ip, (popt + 2));
893 case 58: /* Ignore Renewal Time Option */
895 case 59: /* Ignore Rebinding Time Option */
897 case 66: /* Ignore TFTP server name */
899 case 67: /* Bootfile option */
900 if (!net_boot_file_name_explicit) {
901 size = truncate_sz("Bootfile",
902 sizeof(net_boot_file_name),
904 memcpy(&net_boot_file_name, popt + 2, size);
905 net_boot_file_name[size] = 0;
909 #if defined(CONFIG_BOOTP_VENDOREX)
910 if (dhcp_vendorex_proc(popt))
913 printf("*** Unhandled DHCP Option in OFFER/ACK:"
917 popt += oplen + 2; /* Process next option */
921 static void dhcp_packet_process_options(struct bootp_hdr *bp)
923 uchar *popt = (uchar *)&bp->bp_vend[4];
924 uchar *end = popt + BOOTP_HDR_SIZE;
926 if (net_read_u32((u32 *)&bp->bp_vend[0]) != htonl(BOOTP_VENDOR_MAGIC))
929 dhcp_option_overload = 0;
932 * The 'options' field MUST be interpreted first, 'file' next,
935 dhcp_process_options(popt, end);
937 if (dhcp_option_overload & OVERLOAD_FILE) {
938 popt = (uchar *)bp->bp_file;
939 end = popt + sizeof(bp->bp_file);
940 dhcp_process_options(popt, end);
943 if (dhcp_option_overload & OVERLOAD_SNAME) {
944 popt = (uchar *)bp->bp_sname;
945 end = popt + sizeof(bp->bp_sname);
946 dhcp_process_options(popt, end);
950 static int dhcp_message_type(unsigned char *popt)
952 if (net_read_u32((u32 *)popt) != htonl(BOOTP_VENDOR_MAGIC))
956 while (*popt != 0xff) {
957 if (*popt == 53) /* DHCP Message Type */
963 /* Scan through all options */
964 popt += *(popt + 1) + 2;
970 static void dhcp_send_request_packet(struct bootp_hdr *bp_offer)
973 struct bootp_hdr *bp;
974 int pktlen, iplen, extlen;
976 struct in_addr offered_ip;
977 struct in_addr zero_ip;
978 struct in_addr bcast_ip;
980 debug("dhcp_send_request_packet: Sending DHCPREQUEST\n");
982 memset((void *)pkt, 0, PKTSIZE);
984 eth_hdr_size = net_set_ether(pkt, net_bcast_ethaddr, PROT_IP);
987 iphdr = pkt; /* We'll need this later to set proper pkt size */
988 pkt += IP_UDP_HDR_SIZE;
990 bp = (struct bootp_hdr *)pkt;
991 bp->bp_op = OP_BOOTREQUEST;
992 bp->bp_htype = HWT_ETHER;
993 bp->bp_hlen = HWL_ETHER;
995 bp->bp_secs = htons(get_timer(bootp_start) / 1000);
996 /* Do not set the client IP, your IP, or server IP yet, since it
997 * hasn't been ACK'ed by the server yet */
1000 * RFC3046 requires Relay Agents to discard packets with
1001 * nonzero and offered giaddr
1004 net_write_ip(&bp->bp_giaddr, zero_ip);
1006 memcpy(bp->bp_chaddr, net_ethaddr, 6);
1007 copy_filename(bp->bp_file, net_boot_file_name, sizeof(bp->bp_file));
1010 * ID is the id of the OFFER packet
1013 net_copy_u32(&bp->bp_id, &bp_offer->bp_id);
1016 * Copy options from OFFER packet if present
1019 /* Copy offered IP into the parameters request list */
1020 net_copy_ip(&offered_ip, &bp_offer->bp_yiaddr);
1021 extlen = dhcp_extended((u8 *)bp->bp_vend, DHCP_REQUEST,
1022 dhcp_server_ip, offered_ip);
1024 iplen = BOOTP_HDR_SIZE - OPT_FIELD_SIZE + extlen;
1025 pktlen = eth_hdr_size + IP_UDP_HDR_SIZE + iplen;
1026 bcast_ip.s_addr = 0xFFFFFFFFL;
1027 net_set_udp_header(iphdr, bcast_ip, PORT_BOOTPS, PORT_BOOTPC, iplen);
1029 #ifdef CONFIG_BOOTP_DHCP_REQUEST_DELAY
1030 udelay(CONFIG_BOOTP_DHCP_REQUEST_DELAY);
1031 #endif /* CONFIG_BOOTP_DHCP_REQUEST_DELAY */
1032 debug("Transmitting DHCPREQUEST packet: len = %d\n", pktlen);
1033 net_send_packet(net_tx_packet, pktlen);
1037 * Handle DHCP received packets.
1039 static void dhcp_handler(uchar *pkt, unsigned dest, struct in_addr sip,
1040 unsigned src, unsigned len)
1042 struct bootp_hdr *bp = (struct bootp_hdr *)pkt;
1044 debug("DHCPHandler: got packet: (src=%d, dst=%d, len=%d) state: %d\n",
1045 src, dest, len, dhcp_state);
1047 /* Filter out pkts we don't want */
1048 if (check_reply_packet(pkt, dest, src, len))
1051 debug("DHCPHandler: got DHCP packet: (src=%d, dst=%d, len=%d) state: "
1052 "%d\n", src, dest, len, dhcp_state);
1054 if (net_read_ip(&bp->bp_yiaddr).s_addr == 0)
1057 switch (dhcp_state) {
1060 * Wait an appropriate time for any potential DHCPOFFER packets
1061 * to arrive. Then select one, and generate DHCPREQUEST
1062 * response. If filename is in format we recognize, assume it
1063 * is a valid OFFER from a server we want.
1065 debug("DHCP: state=SELECTING bp_file: \"%s\"\n", bp->bp_file);
1066 #ifdef CONFIG_SYS_BOOTFILE_PREFIX
1067 if (strncmp(bp->bp_file,
1068 CONFIG_SYS_BOOTFILE_PREFIX,
1069 strlen(CONFIG_SYS_BOOTFILE_PREFIX)) == 0) {
1070 #endif /* CONFIG_SYS_BOOTFILE_PREFIX */
1071 dhcp_packet_process_options(bp);
1072 efi_net_set_dhcp_ack(pkt, len);
1074 debug("TRANSITIONING TO REQUESTING STATE\n");
1075 dhcp_state = REQUESTING;
1077 net_set_timeout_handler(5000, bootp_timeout_handler);
1078 dhcp_send_request_packet(bp);
1079 #ifdef CONFIG_SYS_BOOTFILE_PREFIX
1081 #endif /* CONFIG_SYS_BOOTFILE_PREFIX */
1086 debug("DHCP State: REQUESTING\n");
1088 if (dhcp_message_type((u8 *)bp->bp_vend) == DHCP_ACK) {
1089 dhcp_packet_process_options(bp);
1090 /* Store net params from reply */
1091 store_net_params(bp);
1093 printf("DHCP client bound to address %pI4 (%lu ms)\n",
1094 &net_ip, get_timer(bootp_start));
1095 net_set_timeout_handler(0, (thand_f *)0);
1096 bootstage_mark_name(BOOTSTAGE_ID_BOOTP_STOP,
1104 /* DHCP client bound to address */
1107 puts("DHCP: INVALID STATE\n");
1112 void dhcp_request(void)
1116 #endif /* CONFIG_CMD_DHCP */