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
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 #if !defined(CONFIG_BOOTP_SERVERIP)
152 struct in_addr tmp_ip;
153 bool overwrite_serverip = true;
155 #if defined(CONFIG_BOOTP_PREFER_SERVERIP)
156 overwrite_serverip = false;
159 net_copy_ip(&tmp_ip, &bp->bp_siaddr);
160 if (tmp_ip.s_addr != 0 && (overwrite_serverip || !net_server_ip.s_addr))
161 net_copy_ip(&net_server_ip, &bp->bp_siaddr);
162 memcpy(net_server_ethaddr,
163 ((struct ethernet_hdr *)net_rx_packet)->et_src, 6);
165 #if defined(CONFIG_CMD_DHCP)
166 !(dhcp_option_overload & OVERLOAD_FILE) &&
168 (strlen(bp->bp_file) > 0) &&
169 !net_boot_file_name_explicit) {
170 copy_filename(net_boot_file_name, bp->bp_file,
171 sizeof(net_boot_file_name));
174 debug("net_boot_file_name: %s\n", net_boot_file_name);
176 /* Propagate to environment:
177 * don't delete exising entry when BOOTP / DHCP reply does
178 * not contain a new value
180 if (*net_boot_file_name)
181 env_set("bootfile", net_boot_file_name);
186 * Copy parameters of interest from BOOTP_REPLY/DHCP_OFFER packet
188 static void store_net_params(struct bootp_hdr *bp)
190 #if !defined(CONFIG_SERVERIP_FROM_PROXYDHCP)
191 store_bootp_params(bp);
193 net_copy_ip(&net_ip, &bp->bp_yiaddr);
196 static int truncate_sz(const char *name, int maxlen, int curlen)
198 if (curlen >= maxlen) {
199 printf("*** WARNING: %s is too long (%d - max: %d)"
200 " - truncated\n", name, curlen, maxlen);
206 #if !defined(CONFIG_CMD_DHCP)
208 static void bootp_process_vendor_field(u8 *ext)
210 int size = *(ext + 1);
212 debug("[BOOTP] Processing extension %d... (%d bytes)\n", *ext,
215 net_boot_file_expected_size_in_blocks = 0;
218 /* Fixed length fields */
219 case 1: /* Subnet mask */
220 if (net_netmask.s_addr == 0)
221 net_copy_ip(&net_netmask, (struct in_addr *)(ext + 2));
223 case 2: /* Time offset - Not yet supported */
225 /* Variable length fields */
226 case 3: /* Gateways list */
227 if (net_gateway.s_addr == 0)
228 net_copy_ip(&net_gateway, (struct in_addr *)(ext + 2));
230 case 4: /* Time server - Not yet supported */
232 case 5: /* IEN-116 name server - Not yet supported */
235 if (net_dns_server.s_addr == 0)
236 net_copy_ip(&net_dns_server,
237 (struct in_addr *)(ext + 2));
238 #if defined(CONFIG_BOOTP_DNS2)
239 if ((net_dns_server2.s_addr == 0) && (size > 4))
240 net_copy_ip(&net_dns_server2,
241 (struct in_addr *)(ext + 2 + 4));
244 case 7: /* Log server - Not yet supported */
246 case 8: /* Cookie/Quote server - Not yet supported */
248 case 9: /* LPR server - Not yet supported */
250 case 10: /* Impress server - Not yet supported */
252 case 11: /* RPL server - Not yet supported */
254 case 12: /* Host name */
255 if (net_hostname[0] == 0) {
256 size = truncate_sz("Host Name",
257 sizeof(net_hostname), size);
258 memcpy(&net_hostname, ext + 2, size);
259 net_hostname[size] = 0;
262 case 13: /* Boot file size */
264 net_boot_file_expected_size_in_blocks =
265 ntohs(*(ushort *)(ext + 2));
267 net_boot_file_expected_size_in_blocks =
268 ntohl(*(ulong *)(ext + 2));
270 case 14: /* Merit dump file - Not yet supported */
272 case 15: /* Domain name - Not yet supported */
274 case 16: /* Swap server - Not yet supported */
276 case 17: /* Root path */
277 if (net_root_path[0] == 0) {
278 size = truncate_sz("Root Path",
279 sizeof(net_root_path), size);
280 memcpy(&net_root_path, ext + 2, size);
281 net_root_path[size] = 0;
284 case 18: /* Extension path - Not yet supported */
286 * This can be used to send the information of the
287 * vendor area in another file that the client can
291 /* IP host layer fields */
292 case 40: /* NIS Domain name */
293 if (net_nis_domain[0] == 0) {
294 size = truncate_sz("NIS Domain Name",
295 sizeof(net_nis_domain), size);
296 memcpy(&net_nis_domain, ext + 2, size);
297 net_nis_domain[size] = 0;
300 #if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_NTPSERVER)
301 case 42: /* NTP server IP */
302 net_copy_ip(&net_ntp_server, (struct in_addr *)(ext + 2));
305 /* Application layer fields */
306 case 43: /* Vendor specific info - Not yet supported */
308 * Binary information to exchange specific
309 * product information.
312 /* Reserved (custom) fields (128..254) */
316 static void bootp_process_vendor(u8 *ext, int size)
318 u8 *end = ext + size;
320 debug("[BOOTP] Checking extension (%d bytes)...\n", size);
322 while ((ext < end) && (*ext != 0xff)) {
330 bootp_process_vendor_field(opt);
334 debug("[BOOTP] Received fields:\n");
335 if (net_netmask.s_addr)
336 debug("net_netmask : %pI4\n", &net_netmask);
338 if (net_gateway.s_addr)
339 debug("net_gateway : %pI4", &net_gateway);
341 if (net_boot_file_expected_size_in_blocks)
342 debug("net_boot_file_expected_size_in_blocks : %d\n",
343 net_boot_file_expected_size_in_blocks);
346 debug("net_hostname : %s\n", net_hostname);
348 if (net_root_path[0])
349 debug("net_root_path : %s\n", net_root_path);
351 if (net_nis_domain[0])
352 debug("net_nis_domain : %s\n", net_nis_domain);
354 #if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_NTPSERVER)
355 if (net_ntp_server.s_addr)
356 debug("net_ntp_server : %pI4\n", &net_ntp_server);
361 * Handle a BOOTP received packet.
363 static void bootp_handler(uchar *pkt, unsigned dest, struct in_addr sip,
364 unsigned src, unsigned len)
366 struct bootp_hdr *bp;
368 debug("got BOOTP packet (src=%d, dst=%d, len=%d want_len=%zu)\n",
369 src, dest, len, sizeof(struct bootp_hdr));
371 bp = (struct bootp_hdr *)pkt;
373 /* Filter out pkts we don't want */
374 if (check_reply_packet(pkt, dest, src, len))
378 * Got a good BOOTP reply. Copy the data into our variables.
380 #if defined(CONFIG_LED_STATUS) && defined(CONFIG_LED_STATUS_BOOT_ENABLE)
381 status_led_set(CONFIG_LED_STATUS_BOOT, CONFIG_LED_STATUS_OFF);
384 store_net_params(bp); /* Store net parameters from reply */
386 /* Retrieve extended information (we must parse the vendor area) */
387 if (net_read_u32((u32 *)&bp->bp_vend[0]) == htonl(BOOTP_VENDOR_MAGIC))
388 bootp_process_vendor((uchar *)&bp->bp_vend[4], len);
390 net_set_timeout_handler(0, (thand_f *)0);
391 bootstage_mark_name(BOOTSTAGE_ID_BOOTP_STOP, "bootp_stop");
393 debug("Got good BOOTP\n");
400 * Timeout on BOOTP/DHCP request.
402 static void bootp_timeout_handler(void)
404 ulong time_taken = get_timer(bootp_start);
406 if (time_taken >= time_taken_max) {
407 #ifdef CONFIG_BOOTP_MAY_FAIL
410 ethrotate = env_get("ethrotate");
411 if ((ethrotate && strcmp(ethrotate, "no") == 0) ||
413 puts("\nRetry time exceeded\n");
414 net_set_state(NETLOOP_FAIL);
418 puts("\nRetry time exceeded; starting again\n");
423 if (bootp_timeout > 2000)
424 bootp_timeout = 2000;
425 net_set_timeout_handler(bootp_timeout, bootp_timeout_handler);
430 #define put_vci(e, str) \
432 size_t vci_strlen = strlen(str); \
433 *e++ = 60; /* Vendor Class Identifier */ \
435 memcpy(e, str, vci_strlen); \
439 static u8 *add_vci(u8 *e)
442 char *env_vci = env_get("bootp_vci");
444 #if defined(CONFIG_SPL_BUILD) && defined(CONFIG_SPL_NET_VCI_STRING)
445 vci = CONFIG_SPL_NET_VCI_STRING;
446 #elif defined(CONFIG_BOOTP_VCI_STRING)
447 vci = CONFIG_BOOTP_VCI_STRING;
460 * Initialize BOOTP extension fields in the request.
462 #if defined(CONFIG_CMD_DHCP)
463 static int dhcp_extended(u8 *e, int message_type, struct in_addr server_ip,
464 struct in_addr requested_ip)
468 #ifdef CONFIG_LIB_UUID
473 #if defined(CONFIG_BOOTP_VENDOREX)
476 #if defined(CONFIG_BOOTP_SEND_HOSTNAME)
480 *e++ = 99; /* RFC1048 Magic Cookie */
485 *e++ = 53; /* DHCP Message Type */
489 *e++ = 57; /* Maximum DHCP Message Size */
491 *e++ = (576 - 312 + OPT_FIELD_SIZE) >> 8;
492 *e++ = (576 - 312 + OPT_FIELD_SIZE) & 0xff;
494 if (server_ip.s_addr) {
495 int tmp = ntohl(server_ip.s_addr);
497 *e++ = 54; /* ServerID */
505 if (requested_ip.s_addr) {
506 int tmp = ntohl(requested_ip.s_addr);
508 *e++ = 50; /* Requested IP */
515 #if defined(CONFIG_BOOTP_SEND_HOSTNAME)
516 hostname = env_get("hostname");
518 int hostnamelen = strlen(hostname);
520 *e++ = 12; /* Hostname */
522 memcpy(e, hostname, hostnamelen);
527 #ifdef CONFIG_BOOTP_PXE_CLIENTARCH
528 clientarch = CONFIG_BOOTP_PXE_CLIENTARCH;
531 if (env_get("bootp_arch"))
532 clientarch = env_get_ulong("bootp_arch", 16, clientarch);
534 if (clientarch > 0) {
535 *e++ = 93; /* Client System Architecture */
537 *e++ = (clientarch >> 8) & 0xff;
538 *e++ = clientarch & 0xff;
541 *e++ = 94; /* Client Network Interface Identifier */
543 *e++ = 1; /* type field for UNDI */
544 *e++ = 0; /* major revision */
545 *e++ = 0; /* minor revision */
547 #ifdef CONFIG_LIB_UUID
548 uuid = env_get("pxeuuid");
551 if (uuid_str_valid(uuid)) {
552 *e++ = 97; /* Client Machine Identifier */
554 *e++ = 0; /* type 0 - UUID */
556 uuid_str_to_bin(uuid, e, UUID_STR_FORMAT_STD);
559 printf("Invalid pxeuuid: %s\n", uuid);
566 #if defined(CONFIG_BOOTP_VENDOREX)
567 x = dhcp_vendorex_prep(e);
572 *e++ = 55; /* Parameter Request List */
573 cnt = e++; /* Pointer to count of requested items */
575 #if defined(CONFIG_BOOTP_SUBNETMASK)
576 *e++ = 1; /* Subnet Mask */
579 #if defined(CONFIG_BOOTP_TIMEOFFSET)
583 #if defined(CONFIG_BOOTP_GATEWAY)
584 *e++ = 3; /* Router Option */
587 #if defined(CONFIG_BOOTP_DNS)
588 *e++ = 6; /* DNS Server(s) */
591 #if defined(CONFIG_BOOTP_HOSTNAME)
592 *e++ = 12; /* Hostname */
595 #if defined(CONFIG_BOOTP_BOOTFILESIZE)
596 *e++ = 13; /* Boot File Size */
599 #if defined(CONFIG_BOOTP_BOOTPATH)
600 *e++ = 17; /* Boot path */
603 #if defined(CONFIG_BOOTP_NISDOMAIN)
604 *e++ = 40; /* NIS Domain name request */
607 #if defined(CONFIG_BOOTP_NTPSERVER)
611 /* no options, so back up to avoid sending an empty request list */
615 *e++ = 255; /* End of the list */
617 /* Pad to minimal length */
618 #ifdef CONFIG_DHCP_MIN_EXT_LEN
619 while ((e - start) < CONFIG_DHCP_MIN_EXT_LEN)
628 * Warning: no field size check - change CONFIG_BOOTP_* at your own risk!
630 static int bootp_extended(u8 *e)
634 *e++ = 99; /* RFC1048 Magic Cookie */
639 #if defined(CONFIG_CMD_DHCP)
640 *e++ = 53; /* DHCP Message Type */
642 *e++ = DHCP_DISCOVER;
644 *e++ = 57; /* Maximum DHCP Message Size */
646 *e++ = (576 - 312 + OPT_FIELD_SIZE) >> 16;
647 *e++ = (576 - 312 + OPT_FIELD_SIZE) & 0xff;
652 #if defined(CONFIG_BOOTP_SUBNETMASK)
653 *e++ = 1; /* Subnet mask request */
658 #if defined(CONFIG_BOOTP_GATEWAY)
659 *e++ = 3; /* Default gateway request */
664 #if defined(CONFIG_BOOTP_DNS)
665 *e++ = 6; /* Domain Name Server */
670 #if defined(CONFIG_BOOTP_HOSTNAME)
671 *e++ = 12; /* Host name request */
676 #if defined(CONFIG_BOOTP_BOOTFILESIZE)
677 *e++ = 13; /* Boot file size */
682 #if defined(CONFIG_BOOTP_BOOTPATH)
683 *e++ = 17; /* Boot path */
688 #if defined(CONFIG_BOOTP_NISDOMAIN)
689 *e++ = 40; /* NIS Domain name request */
693 #if defined(CONFIG_BOOTP_NTPSERVER)
699 *e++ = 255; /* End of the list */
702 * If nothing in list, remove it altogether. Some DHCP servers get
703 * upset by this minor faux pas and do not respond at all.
705 if (e == start + 3) {
706 printf("*** Warning: no DHCP options requested\n");
714 void bootp_reset(void)
718 bootp_start = get_timer(0);
722 void bootp_request(void)
725 struct bootp_hdr *bp;
726 int extlen, pktlen, iplen;
728 #ifdef CONFIG_BOOTP_RANDOM_DELAY
732 struct in_addr zero_ip;
733 struct in_addr bcast_ip;
734 char *ep; /* Environment pointer */
736 bootstage_mark_name(BOOTSTAGE_ID_BOOTP_START, "bootp_start");
737 #if defined(CONFIG_CMD_DHCP)
741 ep = env_get("bootpretryperiod");
743 time_taken_max = dectoul(ep, NULL);
745 time_taken_max = TIMEOUT_MS;
747 #ifdef CONFIG_BOOTP_RANDOM_DELAY /* Random BOOTP delay */
751 if (bootp_try <= 2) /* Start with max 1024 * 1ms */
752 rand_ms = rand() >> (22 - bootp_try);
753 else /* After 3rd BOOTP request max 8192 * 1ms */
754 rand_ms = rand() >> 19;
756 printf("Random delay: %ld ms...\n", rand_ms);
759 #endif /* CONFIG_BOOTP_RANDOM_DELAY */
761 printf("BOOTP broadcast %d\n", ++bootp_try);
763 memset((void *)pkt, 0, PKTSIZE);
765 eth_hdr_size = net_set_ether(pkt, net_bcast_ethaddr, PROT_IP);
769 * Next line results in incorrect packet size being transmitted,
770 * resulting in errors in some DHCP servers, reporting missing bytes.
771 * Size must be set in packet header after extension length has been
773 * C. Hallinan, DS4.COM, Inc.
775 /* net_set_udp_header(pkt, 0xFFFFFFFFL, PORT_BOOTPS, PORT_BOOTPC,
776 sizeof (struct bootp_hdr)); */
777 iphdr = pkt; /* We need this later for net_set_udp_header() */
778 pkt += IP_UDP_HDR_SIZE;
780 bp = (struct bootp_hdr *)pkt;
781 bp->bp_op = OP_BOOTREQUEST;
782 bp->bp_htype = HWT_ETHER;
783 bp->bp_hlen = HWL_ETHER;
786 * according to RFC1542, should be 0 on first request, secs since
787 * first request otherwise
789 bp->bp_secs = htons(get_timer(bootp_start) / 1000);
791 net_write_ip(&bp->bp_ciaddr, zero_ip);
792 net_write_ip(&bp->bp_yiaddr, zero_ip);
793 net_write_ip(&bp->bp_siaddr, zero_ip);
794 net_write_ip(&bp->bp_giaddr, zero_ip);
795 memcpy(bp->bp_chaddr, net_ethaddr, 6);
796 copy_filename(bp->bp_file, net_boot_file_name, sizeof(bp->bp_file));
798 /* Request additional information from the BOOTP/DHCP server */
799 #if defined(CONFIG_CMD_DHCP)
800 extlen = dhcp_extended((u8 *)bp->bp_vend, DHCP_DISCOVER, zero_ip,
803 extlen = bootp_extended((u8 *)bp->bp_vend);
807 * Bootp ID is the lower 4 bytes of our ethernet address
808 * plus the current time in ms.
810 bootp_id = ((u32)net_ethaddr[2] << 24)
811 | ((u32)net_ethaddr[3] << 16)
812 | ((u32)net_ethaddr[4] << 8)
813 | (u32)net_ethaddr[5];
814 bootp_id += get_timer(0);
815 bootp_id = htonl(bootp_id);
816 bootp_add_id(bootp_id);
817 net_copy_u32(&bp->bp_id, &bootp_id);
820 * Calculate proper packet lengths taking into account the
821 * variable size of the options field
823 iplen = BOOTP_HDR_SIZE - OPT_FIELD_SIZE + extlen;
824 pktlen = eth_hdr_size + IP_UDP_HDR_SIZE + iplen;
825 bcast_ip.s_addr = 0xFFFFFFFFL;
826 net_set_udp_header(iphdr, bcast_ip, PORT_BOOTPS, PORT_BOOTPC, iplen);
827 net_set_timeout_handler(bootp_timeout, bootp_timeout_handler);
829 #if defined(CONFIG_CMD_DHCP)
830 dhcp_state = SELECTING;
831 net_set_udp_handler(dhcp_handler);
833 net_set_udp_handler(bootp_handler);
835 net_send_packet(net_tx_packet, pktlen);
838 #if defined(CONFIG_CMD_DHCP)
839 static void dhcp_process_options(uchar *popt, uchar *end)
842 #if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_TIMEOFFSET)
846 while (popt < end && *popt != 0xff) {
850 oplen = -1; /* Pad omits len byte */
853 net_copy_ip(&net_netmask, (popt + 2));
855 #if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_TIMEOFFSET)
856 case 2: /* Time offset */
857 to_ptr = &net_ntp_time_offset;
858 net_copy_u32((u32 *)to_ptr, (u32 *)(popt + 2));
859 net_ntp_time_offset = ntohl(net_ntp_time_offset);
863 net_copy_ip(&net_gateway, (popt + 2));
866 net_copy_ip(&net_dns_server, (popt + 2));
867 #if defined(CONFIG_BOOTP_DNS2)
869 net_copy_ip(&net_dns_server2, (popt + 2 + 4));
873 size = truncate_sz("Host Name",
874 sizeof(net_hostname), oplen);
875 memcpy(&net_hostname, popt + 2, size);
876 net_hostname[size] = 0;
878 case 15: /* Ignore Domain Name Option */
881 size = truncate_sz("Root Path",
882 sizeof(net_root_path), oplen);
883 memcpy(&net_root_path, popt + 2, size);
884 net_root_path[size] = 0;
886 case 28: /* Ignore Broadcast Address Option */
888 #if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_NTPSERVER)
889 case 42: /* NTP server IP */
890 net_copy_ip(&net_ntp_server, (popt + 2));
894 net_copy_u32(&dhcp_leasetime, (u32 *)(popt + 2));
897 dhcp_option_overload = popt[2];
899 case 53: /* Ignore Message Type Option */
902 net_copy_ip(&dhcp_server_ip, (popt + 2));
904 case 58: /* Ignore Renewal Time Option */
906 case 59: /* Ignore Rebinding Time Option */
908 case 66: /* Ignore TFTP server name */
910 case 67: /* Bootfile option */
911 if (!net_boot_file_name_explicit) {
912 size = truncate_sz("Bootfile",
913 sizeof(net_boot_file_name),
915 memcpy(&net_boot_file_name, popt + 2, size);
916 net_boot_file_name[size] = 0;
920 #if defined(CONFIG_BOOTP_VENDOREX)
921 if (dhcp_vendorex_proc(popt))
924 printf("*** Unhandled DHCP Option in OFFER/ACK:"
928 popt += oplen + 2; /* Process next option */
932 static void dhcp_packet_process_options(struct bootp_hdr *bp)
934 uchar *popt = (uchar *)&bp->bp_vend[4];
935 uchar *end = popt + BOOTP_HDR_SIZE;
937 if (net_read_u32((u32 *)&bp->bp_vend[0]) != htonl(BOOTP_VENDOR_MAGIC))
940 dhcp_option_overload = 0;
943 * The 'options' field MUST be interpreted first, 'file' next,
946 dhcp_process_options(popt, end);
948 if (dhcp_option_overload & OVERLOAD_FILE) {
949 popt = (uchar *)bp->bp_file;
950 end = popt + sizeof(bp->bp_file);
951 dhcp_process_options(popt, end);
954 if (dhcp_option_overload & OVERLOAD_SNAME) {
955 popt = (uchar *)bp->bp_sname;
956 end = popt + sizeof(bp->bp_sname);
957 dhcp_process_options(popt, end);
961 static int dhcp_message_type(unsigned char *popt)
963 if (net_read_u32((u32 *)popt) != htonl(BOOTP_VENDOR_MAGIC))
967 while (*popt != 0xff) {
968 if (*popt == 53) /* DHCP Message Type */
974 /* Scan through all options */
975 popt += *(popt + 1) + 2;
981 static void dhcp_send_request_packet(struct bootp_hdr *bp_offer)
984 struct bootp_hdr *bp;
985 int pktlen, iplen, extlen;
987 struct in_addr offered_ip;
988 struct in_addr zero_ip;
989 struct in_addr bcast_ip;
991 debug("dhcp_send_request_packet: Sending DHCPREQUEST\n");
993 memset((void *)pkt, 0, PKTSIZE);
995 eth_hdr_size = net_set_ether(pkt, net_bcast_ethaddr, PROT_IP);
998 iphdr = pkt; /* We'll need this later to set proper pkt size */
999 pkt += IP_UDP_HDR_SIZE;
1001 bp = (struct bootp_hdr *)pkt;
1002 bp->bp_op = OP_BOOTREQUEST;
1003 bp->bp_htype = HWT_ETHER;
1004 bp->bp_hlen = HWL_ETHER;
1006 bp->bp_secs = htons(get_timer(bootp_start) / 1000);
1007 /* Do not set the client IP, your IP, or server IP yet, since it
1008 * hasn't been ACK'ed by the server yet */
1011 * RFC3046 requires Relay Agents to discard packets with
1012 * nonzero and offered giaddr
1015 net_write_ip(&bp->bp_giaddr, zero_ip);
1017 memcpy(bp->bp_chaddr, net_ethaddr, 6);
1018 copy_filename(bp->bp_file, net_boot_file_name, sizeof(bp->bp_file));
1021 * ID is the id of the OFFER packet
1024 net_copy_u32(&bp->bp_id, &bp_offer->bp_id);
1027 * Copy options from OFFER packet if present
1030 /* Copy offered IP into the parameters request list */
1031 net_copy_ip(&offered_ip, &bp_offer->bp_yiaddr);
1032 extlen = dhcp_extended((u8 *)bp->bp_vend, DHCP_REQUEST,
1033 dhcp_server_ip, offered_ip);
1035 iplen = BOOTP_HDR_SIZE - OPT_FIELD_SIZE + extlen;
1036 pktlen = eth_hdr_size + IP_UDP_HDR_SIZE + iplen;
1037 bcast_ip.s_addr = 0xFFFFFFFFL;
1038 net_set_udp_header(iphdr, bcast_ip, PORT_BOOTPS, PORT_BOOTPC, iplen);
1040 #ifdef CONFIG_BOOTP_DHCP_REQUEST_DELAY
1041 udelay(CONFIG_BOOTP_DHCP_REQUEST_DELAY);
1042 #endif /* CONFIG_BOOTP_DHCP_REQUEST_DELAY */
1043 debug("Transmitting DHCPREQUEST packet: len = %d\n", pktlen);
1044 net_send_packet(net_tx_packet, pktlen);
1048 * Handle DHCP received packets.
1050 static void dhcp_handler(uchar *pkt, unsigned dest, struct in_addr sip,
1051 unsigned src, unsigned len)
1053 struct bootp_hdr *bp = (struct bootp_hdr *)pkt;
1055 debug("DHCPHandler: got packet: (src=%d, dst=%d, len=%d) state: %d\n",
1056 src, dest, len, dhcp_state);
1058 /* Filter out pkts we don't want */
1059 if (check_reply_packet(pkt, dest, src, len))
1062 debug("DHCPHandler: got DHCP packet: (src=%d, dst=%d, len=%d) state: "
1063 "%d\n", src, dest, len, dhcp_state);
1065 if (net_read_ip(&bp->bp_yiaddr).s_addr == 0) {
1066 #if defined(CONFIG_SERVERIP_FROM_PROXYDHCP)
1067 store_bootp_params(bp);
1072 switch (dhcp_state) {
1075 * Wait an appropriate time for any potential DHCPOFFER packets
1076 * to arrive. Then select one, and generate DHCPREQUEST
1077 * response. If filename is in format we recognize, assume it
1078 * is a valid OFFER from a server we want.
1080 debug("DHCP: state=SELECTING bp_file: \"%s\"\n", bp->bp_file);
1081 #ifdef CONFIG_SYS_BOOTFILE_PREFIX
1082 if (strncmp(bp->bp_file,
1083 CONFIG_SYS_BOOTFILE_PREFIX,
1084 strlen(CONFIG_SYS_BOOTFILE_PREFIX)) == 0) {
1085 #endif /* CONFIG_SYS_BOOTFILE_PREFIX */
1086 dhcp_packet_process_options(bp);
1087 efi_net_set_dhcp_ack(pkt, len);
1089 #if defined(CONFIG_SERVERIP_FROM_PROXYDHCP)
1090 if (!net_server_ip.s_addr)
1091 udelay(CONFIG_SERVERIP_FROM_PROXYDHCP_DELAY_MS *
1093 #endif /* CONFIG_SERVERIP_FROM_PROXYDHCP */
1095 debug("TRANSITIONING TO REQUESTING STATE\n");
1096 dhcp_state = REQUESTING;
1098 net_set_timeout_handler(5000, bootp_timeout_handler);
1099 dhcp_send_request_packet(bp);
1100 #ifdef CONFIG_SYS_BOOTFILE_PREFIX
1102 #endif /* CONFIG_SYS_BOOTFILE_PREFIX */
1107 debug("DHCP State: REQUESTING\n");
1109 if (dhcp_message_type((u8 *)bp->bp_vend) == DHCP_ACK) {
1110 dhcp_packet_process_options(bp);
1111 /* Store net params from reply */
1112 store_net_params(bp);
1114 printf("DHCP client bound to address %pI4 (%lu ms)\n",
1115 &net_ip, get_timer(bootp_start));
1116 net_set_timeout_handler(0, (thand_f *)0);
1117 bootstage_mark_name(BOOTSTAGE_ID_BOOTP_STOP,
1125 /* DHCP client bound to address */
1128 puts("DHCP: INVALID STATE\n");
1133 void dhcp_request(void)
1137 #endif /* CONFIG_CMD_DHCP */