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
31 #define BOOTP_VENDOR_MAGIC 0x63825363 /* RFC1048 Magic Cookie */
34 * The timeout for the initial BOOTP/DHCP request used to be described by a
35 * counter of fixed-length timeout periods. CONFIG_NET_RETRY_COUNT represents
38 * Now that the timeout periods are variable (exponential backoff and retry)
39 * we convert the timeout count to the absolute time it would have take to
40 * execute that many retries, and keep sending retry packets until that time
43 #define TIMEOUT_MS ((3 + (CONFIG_NET_RETRY_COUNT * 5)) * 1000)
45 #ifndef CFG_DHCP_MIN_EXT_LEN /* minimal length of extension list */
46 #define CFG_DHCP_MIN_EXT_LEN 64
49 #ifndef CFG_BOOTP_ID_CACHE_SIZE
50 #define CFG_BOOTP_ID_CACHE_SIZE 4
53 u32 bootp_ids
[CFG_BOOTP_ID_CACHE_SIZE
];
54 unsigned int bootp_num_ids
;
58 char net_nis_domain
[32] = {0,}; /* Our NIS domain */
59 char net_hostname
[32] = {0,}; /* Our hostname */
60 char net_root_path
[CONFIG_BOOTP_MAX_ROOT_PATH_LEN
] = {0,}; /* Our bootpath */
62 static ulong time_taken_max
;
64 #if defined(CONFIG_CMD_DHCP)
65 static dhcp_state_t dhcp_state
= INIT
;
66 static u32 dhcp_leasetime
;
67 static struct in_addr dhcp_server_ip
;
68 static u8 dhcp_option_overload
;
69 #define OVERLOAD_FILE 1
70 #define OVERLOAD_SNAME 2
71 static void dhcp_handler(uchar
*pkt
, unsigned dest
, struct in_addr sip
,
72 unsigned src
, unsigned len
);
76 static char *dhcpmsg2str(int type
)
79 case 1: return "DHCPDISCOVER"; break;
80 case 2: return "DHCPOFFER"; break;
81 case 3: return "DHCPREQUEST"; break;
82 case 4: return "DHCPDECLINE"; break;
83 case 5: return "DHCPACK"; break;
84 case 6: return "DHCPNACK"; break;
85 case 7: return "DHCPRELEASE"; break;
86 default: return "UNKNOWN/INVALID MSG TYPE"; break;
92 static void bootp_add_id(ulong id
)
94 if (bootp_num_ids
>= ARRAY_SIZE(bootp_ids
)) {
95 size_t size
= sizeof(bootp_ids
) - sizeof(id
);
97 memmove(bootp_ids
, &bootp_ids
[1], size
);
98 bootp_ids
[bootp_num_ids
- 1] = id
;
100 bootp_ids
[bootp_num_ids
] = id
;
105 static bool bootp_match_id(ulong id
)
109 for (i
= 0; i
< bootp_num_ids
; i
++)
110 if (bootp_ids
[i
] == id
)
116 static int check_reply_packet(uchar
*pkt
, unsigned dest
, unsigned src
,
119 struct bootp_hdr
*bp
= (struct bootp_hdr
*)pkt
;
122 if (dest
!= PORT_BOOTPC
|| src
!= PORT_BOOTPS
)
124 else if (len
< sizeof(struct bootp_hdr
) - OPT_FIELD_SIZE
)
126 else if (bp
->bp_op
!= OP_BOOTREPLY
)
128 else if (bp
->bp_htype
!= HWT_ETHER
)
130 else if (bp
->bp_hlen
!= HWL_ETHER
)
132 else if (!bootp_match_id(net_read_u32(&bp
->bp_id
)))
134 else if (memcmp(bp
->bp_chaddr
, net_ethaddr
, HWL_ETHER
) != 0)
137 debug("Filtering pkt = %d\n", retval
);
142 static void store_bootp_params(struct bootp_hdr
*bp
)
144 struct in_addr tmp_ip
;
145 bool overwrite_serverip
= true;
147 if (IS_ENABLED(CONFIG_BOOTP_SERVERIP
))
150 #if defined(CONFIG_BOOTP_PREFER_SERVERIP)
151 overwrite_serverip
= false;
154 net_copy_ip(&tmp_ip
, &bp
->bp_siaddr
);
155 if (tmp_ip
.s_addr
!= 0 && (overwrite_serverip
|| !net_server_ip
.s_addr
))
156 net_copy_ip(&net_server_ip
, &bp
->bp_siaddr
);
157 memcpy(net_server_ethaddr
,
158 ((struct ethernet_hdr
*)net_rx_packet
)->et_src
, 6);
160 #if defined(CONFIG_CMD_DHCP)
161 !(dhcp_option_overload
& OVERLOAD_FILE
) &&
163 (strlen(bp
->bp_file
) > 0) &&
164 !net_boot_file_name_explicit
) {
165 copy_filename(net_boot_file_name
, bp
->bp_file
,
166 sizeof(net_boot_file_name
));
169 debug("net_boot_file_name: %s\n", net_boot_file_name
);
171 /* Propagate to environment:
172 * don't delete exising entry when BOOTP / DHCP reply does
173 * not contain a new value
175 if (*net_boot_file_name
)
176 env_set("bootfile", net_boot_file_name
);
180 * Copy parameters of interest from BOOTP_REPLY/DHCP_OFFER packet
182 static void store_net_params(struct bootp_hdr
*bp
)
184 #if !defined(CONFIG_SERVERIP_FROM_PROXYDHCP)
185 store_bootp_params(bp
);
187 net_copy_ip(&net_ip
, &bp
->bp_yiaddr
);
190 static int truncate_sz(const char *name
, int maxlen
, int curlen
)
192 if (curlen
>= maxlen
) {
193 printf("*** WARNING: %s is too long (%d - max: %d)"
194 " - truncated\n", name
, curlen
, maxlen
);
200 #if !defined(CONFIG_CMD_DHCP)
202 static void bootp_process_vendor_field(u8
*ext
)
204 int size
= *(ext
+ 1);
206 debug("[BOOTP] Processing extension %d... (%d bytes)\n", *ext
,
209 net_boot_file_expected_size_in_blocks
= 0;
212 /* Fixed length fields */
213 case 1: /* Subnet mask */
214 if (net_netmask
.s_addr
== 0)
215 net_copy_ip(&net_netmask
, (struct in_addr
*)(ext
+ 2));
217 case 2: /* Time offset - Not yet supported */
219 /* Variable length fields */
220 case 3: /* Gateways list */
221 if (net_gateway
.s_addr
== 0)
222 net_copy_ip(&net_gateway
, (struct in_addr
*)(ext
+ 2));
224 case 4: /* Time server - Not yet supported */
226 case 5: /* IEN-116 name server - Not yet supported */
229 if (net_dns_server
.s_addr
== 0)
230 net_copy_ip(&net_dns_server
,
231 (struct in_addr
*)(ext
+ 2));
232 #if defined(CONFIG_BOOTP_DNS2)
233 if ((net_dns_server2
.s_addr
== 0) && (size
> 4))
234 net_copy_ip(&net_dns_server2
,
235 (struct in_addr
*)(ext
+ 2 + 4));
238 case 7: /* Log server - Not yet supported */
240 case 8: /* Cookie/Quote server - Not yet supported */
242 case 9: /* LPR server - Not yet supported */
244 case 10: /* Impress server - Not yet supported */
246 case 11: /* RPL server - Not yet supported */
248 case 12: /* Host name */
249 if (net_hostname
[0] == 0) {
250 size
= truncate_sz("Host Name",
251 sizeof(net_hostname
), size
);
252 memcpy(&net_hostname
, ext
+ 2, size
);
253 net_hostname
[size
] = 0;
256 case 13: /* Boot file size */
258 net_boot_file_expected_size_in_blocks
=
259 ntohs(*(ushort
*)(ext
+ 2));
261 net_boot_file_expected_size_in_blocks
=
262 ntohl(*(ulong
*)(ext
+ 2));
264 case 14: /* Merit dump file - Not yet supported */
266 case 15: /* Domain name - Not yet supported */
268 case 16: /* Swap server - Not yet supported */
270 case 17: /* Root path */
271 if (net_root_path
[0] == 0) {
272 size
= truncate_sz("Root Path",
273 sizeof(net_root_path
), size
);
274 memcpy(&net_root_path
, ext
+ 2, size
);
275 net_root_path
[size
] = 0;
278 case 18: /* Extension path - Not yet supported */
280 * This can be used to send the information of the
281 * vendor area in another file that the client can
285 /* IP host layer fields */
286 case 40: /* NIS Domain name */
287 if (net_nis_domain
[0] == 0) {
288 size
= truncate_sz("NIS Domain Name",
289 sizeof(net_nis_domain
), size
);
290 memcpy(&net_nis_domain
, ext
+ 2, size
);
291 net_nis_domain
[size
] = 0;
294 #if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_NTPSERVER)
295 case 42: /* NTP server IP */
296 net_copy_ip(&net_ntp_server
, (struct in_addr
*)(ext
+ 2));
299 /* Application layer fields */
300 case 43: /* Vendor specific info - Not yet supported */
302 * Binary information to exchange specific
303 * product information.
306 /* Reserved (custom) fields (128..254) */
310 static void bootp_process_vendor(u8
*ext
, int size
)
312 u8
*end
= ext
+ size
;
314 debug("[BOOTP] Checking extension (%d bytes)...\n", size
);
316 while ((ext
< end
) && (*ext
!= 0xff)) {
324 bootp_process_vendor_field(opt
);
328 debug("[BOOTP] Received fields:\n");
329 if (net_netmask
.s_addr
)
330 debug("net_netmask : %pI4\n", &net_netmask
);
332 if (net_gateway
.s_addr
)
333 debug("net_gateway : %pI4", &net_gateway
);
335 if (net_boot_file_expected_size_in_blocks
)
336 debug("net_boot_file_expected_size_in_blocks : %d\n",
337 net_boot_file_expected_size_in_blocks
);
340 debug("net_hostname : %s\n", net_hostname
);
342 if (net_root_path
[0])
343 debug("net_root_path : %s\n", net_root_path
);
345 if (net_nis_domain
[0])
346 debug("net_nis_domain : %s\n", net_nis_domain
);
348 #if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_NTPSERVER)
349 if (net_ntp_server
.s_addr
)
350 debug("net_ntp_server : %pI4\n", &net_ntp_server
);
355 * Handle a BOOTP received packet.
357 static void bootp_handler(uchar
*pkt
, unsigned dest
, struct in_addr sip
,
358 unsigned src
, unsigned len
)
360 struct bootp_hdr
*bp
;
362 debug("got BOOTP packet (src=%d, dst=%d, len=%d want_len=%zu)\n",
363 src
, dest
, len
, sizeof(struct bootp_hdr
));
365 bp
= (struct bootp_hdr
*)pkt
;
367 /* Filter out pkts we don't want */
368 if (check_reply_packet(pkt
, dest
, src
, len
))
372 * Got a good BOOTP reply. Copy the data into our variables.
374 #if defined(CONFIG_LED_STATUS) && defined(CONFIG_LED_STATUS_BOOT_ENABLE)
375 status_led_set(CONFIG_LED_STATUS_BOOT
, CONFIG_LED_STATUS_OFF
);
378 store_net_params(bp
); /* Store net parameters from reply */
380 /* Retrieve extended information (we must parse the vendor area) */
381 if (net_read_u32((u32
*)&bp
->bp_vend
[0]) == htonl(BOOTP_VENDOR_MAGIC
))
382 bootp_process_vendor((uchar
*)&bp
->bp_vend
[4], len
);
384 net_set_timeout_handler(0, (thand_f
*)0);
385 bootstage_mark_name(BOOTSTAGE_ID_BOOTP_STOP
, "bootp_stop");
387 debug("Got good BOOTP\n");
394 * Timeout on BOOTP/DHCP request.
396 static void bootp_timeout_handler(void)
398 ulong time_taken
= get_timer(bootp_start
);
400 if (time_taken
>= time_taken_max
) {
401 #ifdef CONFIG_BOOTP_MAY_FAIL
404 ethrotate
= env_get("ethrotate");
405 if ((ethrotate
&& strcmp(ethrotate
, "no") == 0) ||
407 puts("\nRetry time exceeded\n");
408 net_set_state(NETLOOP_FAIL
);
412 puts("\nRetry time exceeded; starting again\n");
417 if (bootp_timeout
> 2000)
418 bootp_timeout
= 2000;
419 net_set_timeout_handler(bootp_timeout
, bootp_timeout_handler
);
424 #define put_vci(e, str) \
426 size_t vci_strlen = strlen(str); \
427 *e++ = 60; /* Vendor Class Identifier */ \
429 memcpy(e, str, vci_strlen); \
433 static u8
*add_vci(u8
*e
)
436 char *env_vci
= env_get("bootp_vci");
438 #if defined(CONFIG_SPL_BUILD) && defined(CONFIG_SPL_NET_VCI_STRING)
439 vci
= CONFIG_SPL_NET_VCI_STRING
;
440 #elif defined(CONFIG_BOOTP_VCI_STRING)
441 vci
= CONFIG_BOOTP_VCI_STRING
;
454 * Initialize BOOTP extension fields in the request.
456 #if defined(CONFIG_CMD_DHCP)
457 static int dhcp_extended(u8
*e
, int message_type
, struct in_addr server_ip
,
458 struct in_addr requested_ip
)
462 #ifdef CONFIG_LIB_UUID
467 #if defined(CONFIG_BOOTP_VENDOREX)
470 #if defined(CONFIG_BOOTP_SEND_HOSTNAME)
474 *e
++ = 99; /* RFC1048 Magic Cookie */
479 *e
++ = 53; /* DHCP Message Type */
483 *e
++ = 57; /* Maximum DHCP Message Size */
485 *e
++ = (576 - 312 + OPT_FIELD_SIZE
) >> 8;
486 *e
++ = (576 - 312 + OPT_FIELD_SIZE
) & 0xff;
488 if (server_ip
.s_addr
) {
489 int tmp
= ntohl(server_ip
.s_addr
);
491 *e
++ = 54; /* ServerID */
499 if (requested_ip
.s_addr
) {
500 int tmp
= ntohl(requested_ip
.s_addr
);
502 *e
++ = 50; /* Requested IP */
509 #if defined(CONFIG_BOOTP_SEND_HOSTNAME)
510 hostname
= env_get("hostname");
512 int hostnamelen
= strlen(hostname
);
514 *e
++ = 12; /* Hostname */
516 memcpy(e
, hostname
, hostnamelen
);
521 #ifdef CONFIG_BOOTP_PXE_CLIENTARCH
522 clientarch
= CONFIG_BOOTP_PXE_CLIENTARCH
;
525 if (env_get("bootp_arch"))
526 clientarch
= env_get_ulong("bootp_arch", 16, clientarch
);
528 if (clientarch
> 0) {
529 *e
++ = 93; /* Client System Architecture */
531 *e
++ = (clientarch
>> 8) & 0xff;
532 *e
++ = clientarch
& 0xff;
535 *e
++ = 94; /* Client Network Interface Identifier */
537 *e
++ = 1; /* type field for UNDI */
538 *e
++ = 0; /* major revision */
539 *e
++ = 0; /* minor revision */
541 #ifdef CONFIG_LIB_UUID
542 uuid
= env_get("pxeuuid");
545 if (uuid_str_valid(uuid
)) {
546 *e
++ = 97; /* Client Machine Identifier */
548 *e
++ = 0; /* type 0 - UUID */
550 uuid_str_to_bin(uuid
, e
, UUID_STR_FORMAT_STD
);
553 printf("Invalid pxeuuid: %s\n", uuid
);
560 #if defined(CONFIG_BOOTP_VENDOREX)
561 x
= dhcp_vendorex_prep(e
);
566 *e
++ = 55; /* Parameter Request List */
567 cnt
= e
++; /* Pointer to count of requested items */
569 #if defined(CONFIG_BOOTP_SUBNETMASK)
570 *e
++ = 1; /* Subnet Mask */
573 #if defined(CONFIG_BOOTP_TIMEOFFSET)
577 #if defined(CONFIG_BOOTP_GATEWAY)
578 *e
++ = 3; /* Router Option */
581 #if defined(CONFIG_BOOTP_DNS)
582 *e
++ = 6; /* DNS Server(s) */
585 #if defined(CONFIG_BOOTP_HOSTNAME)
586 *e
++ = 12; /* Hostname */
589 #if defined(CONFIG_BOOTP_BOOTFILESIZE)
590 *e
++ = 13; /* Boot File Size */
593 #if defined(CONFIG_BOOTP_BOOTPATH)
594 *e
++ = 17; /* Boot path */
597 #if defined(CONFIG_BOOTP_NISDOMAIN)
598 *e
++ = 40; /* NIS Domain name request */
601 #if defined(CONFIG_BOOTP_NTPSERVER)
605 if (IS_ENABLED(CONFIG_BOOTP_PXE_DHCP_OPTION
)) {
606 *e
++ = 209; /* PXELINUX Config File */
609 /* no options, so back up to avoid sending an empty request list */
613 *e
++ = 255; /* End of the list */
615 /* Pad to minimal length */
616 #ifdef CFG_DHCP_MIN_EXT_LEN
617 while ((e
- start
) < CFG_DHCP_MIN_EXT_LEN
)
626 * Warning: no field size check - change CONFIG_BOOTP_* at your own risk!
628 static int bootp_extended(u8
*e
)
632 *e
++ = 99; /* RFC1048 Magic Cookie */
637 #if defined(CONFIG_CMD_DHCP)
638 *e
++ = 53; /* DHCP Message Type */
640 *e
++ = DHCP_DISCOVER
;
642 *e
++ = 57; /* Maximum DHCP Message Size */
644 *e
++ = (576 - 312 + OPT_FIELD_SIZE
) >> 16;
645 *e
++ = (576 - 312 + OPT_FIELD_SIZE
) & 0xff;
650 #if defined(CONFIG_BOOTP_SUBNETMASK)
651 *e
++ = 1; /* Subnet mask request */
656 #if defined(CONFIG_BOOTP_GATEWAY)
657 *e
++ = 3; /* Default gateway request */
662 #if defined(CONFIG_BOOTP_DNS)
663 *e
++ = 6; /* Domain Name Server */
668 #if defined(CONFIG_BOOTP_HOSTNAME)
669 *e
++ = 12; /* Host name request */
674 #if defined(CONFIG_BOOTP_BOOTFILESIZE)
675 *e
++ = 13; /* Boot file size */
680 #if defined(CONFIG_BOOTP_BOOTPATH)
681 *e
++ = 17; /* Boot path */
686 #if defined(CONFIG_BOOTP_NISDOMAIN)
687 *e
++ = 40; /* NIS Domain name request */
691 #if defined(CONFIG_BOOTP_NTPSERVER)
697 *e
++ = 255; /* End of the list */
700 * If nothing in list, remove it altogether. Some DHCP servers get
701 * upset by this minor faux pas and do not respond at all.
703 if (e
== start
+ 3) {
704 printf("*** Warning: no DHCP options requested\n");
712 void bootp_reset(void)
716 bootp_start
= get_timer(0);
720 void bootp_request(void)
723 struct bootp_hdr
*bp
;
724 int extlen
, pktlen
, iplen
;
726 #ifdef CONFIG_BOOTP_RANDOM_DELAY
730 struct in_addr zero_ip
;
731 struct in_addr bcast_ip
;
732 char *ep
; /* Environment pointer */
734 bootstage_mark_name(BOOTSTAGE_ID_BOOTP_START
, "bootp_start");
735 #if defined(CONFIG_CMD_DHCP)
739 ep
= env_get("bootpretryperiod");
741 time_taken_max
= dectoul(ep
, NULL
);
743 time_taken_max
= TIMEOUT_MS
;
745 #ifdef CONFIG_BOOTP_RANDOM_DELAY /* Random BOOTP delay */
749 if (bootp_try
<= 2) /* Start with max 1024 * 1ms */
750 rand_ms
= rand() >> (22 - bootp_try
);
751 else /* After 3rd BOOTP request max 8192 * 1ms */
752 rand_ms
= rand() >> 19;
754 printf("Random delay: %ld ms...\n", rand_ms
);
757 #endif /* CONFIG_BOOTP_RANDOM_DELAY */
759 printf("BOOTP broadcast %d\n", ++bootp_try
);
761 memset((void *)pkt
, 0, PKTSIZE
);
763 eth_hdr_size
= net_set_ether(pkt
, net_bcast_ethaddr
, PROT_IP
);
767 * Next line results in incorrect packet size being transmitted,
768 * resulting in errors in some DHCP servers, reporting missing bytes.
769 * Size must be set in packet header after extension length has been
771 * C. Hallinan, DS4.COM, Inc.
773 /* net_set_udp_header(pkt, 0xFFFFFFFFL, PORT_BOOTPS, PORT_BOOTPC,
774 sizeof (struct bootp_hdr)); */
775 iphdr
= pkt
; /* We need this later for net_set_udp_header() */
776 pkt
+= IP_UDP_HDR_SIZE
;
778 bp
= (struct bootp_hdr
*)pkt
;
779 bp
->bp_op
= OP_BOOTREQUEST
;
780 bp
->bp_htype
= HWT_ETHER
;
781 bp
->bp_hlen
= HWL_ETHER
;
784 * according to RFC1542, should be 0 on first request, secs since
785 * first request otherwise
787 bp
->bp_secs
= htons(get_timer(bootp_start
) / 1000);
789 net_write_ip(&bp
->bp_ciaddr
, zero_ip
);
790 net_write_ip(&bp
->bp_yiaddr
, zero_ip
);
791 net_write_ip(&bp
->bp_siaddr
, zero_ip
);
792 net_write_ip(&bp
->bp_giaddr
, zero_ip
);
793 memcpy(bp
->bp_chaddr
, net_ethaddr
, 6);
794 copy_filename(bp
->bp_file
, net_boot_file_name
, sizeof(bp
->bp_file
));
796 /* Request additional information from the BOOTP/DHCP server */
797 #if defined(CONFIG_CMD_DHCP)
798 extlen
= dhcp_extended((u8
*)bp
->bp_vend
, DHCP_DISCOVER
, zero_ip
,
801 extlen
= bootp_extended((u8
*)bp
->bp_vend
);
805 * Bootp ID is the lower 4 bytes of our ethernet address
806 * plus the current time in ms.
808 bootp_id
= ((u32
)net_ethaddr
[2] << 24)
809 | ((u32
)net_ethaddr
[3] << 16)
810 | ((u32
)net_ethaddr
[4] << 8)
811 | (u32
)net_ethaddr
[5];
812 bootp_id
+= get_timer(0);
813 bootp_id
= htonl(bootp_id
);
814 bootp_add_id(bootp_id
);
815 net_copy_u32(&bp
->bp_id
, &bootp_id
);
818 * Calculate proper packet lengths taking into account the
819 * variable size of the options field
821 iplen
= BOOTP_HDR_SIZE
- OPT_FIELD_SIZE
+ extlen
;
822 pktlen
= eth_hdr_size
+ IP_UDP_HDR_SIZE
+ iplen
;
823 bcast_ip
.s_addr
= 0xFFFFFFFFL
;
824 net_set_udp_header(iphdr
, bcast_ip
, PORT_BOOTPS
, PORT_BOOTPC
, iplen
);
825 net_set_timeout_handler(bootp_timeout
, bootp_timeout_handler
);
827 #if defined(CONFIG_CMD_DHCP)
828 dhcp_state
= SELECTING
;
829 net_set_udp_handler(dhcp_handler
);
831 net_set_udp_handler(bootp_handler
);
833 net_send_packet(net_tx_packet
, pktlen
);
836 #if defined(CONFIG_CMD_DHCP)
837 static void dhcp_process_options(uchar
*popt
, uchar
*end
)
840 #if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_TIMEOFFSET)
844 while (popt
< end
&& *popt
!= 0xff) {
848 oplen
= -1; /* Pad omits len byte */
851 net_copy_ip(&net_netmask
, (popt
+ 2));
853 #if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_TIMEOFFSET)
854 case 2: /* Time offset */
855 to_ptr
= &net_ntp_time_offset
;
856 net_copy_u32((u32
*)to_ptr
, (u32
*)(popt
+ 2));
857 net_ntp_time_offset
= ntohl(net_ntp_time_offset
);
861 net_copy_ip(&net_gateway
, (popt
+ 2));
864 net_copy_ip(&net_dns_server
, (popt
+ 2));
865 #if defined(CONFIG_BOOTP_DNS2)
867 net_copy_ip(&net_dns_server2
, (popt
+ 2 + 4));
871 size
= truncate_sz("Host Name",
872 sizeof(net_hostname
), oplen
);
873 memcpy(&net_hostname
, popt
+ 2, size
);
874 net_hostname
[size
] = 0;
876 case 15: /* Ignore Domain Name Option */
879 size
= truncate_sz("Root Path",
880 sizeof(net_root_path
), oplen
);
881 memcpy(&net_root_path
, popt
+ 2, size
);
882 net_root_path
[size
] = 0;
884 case 28: /* Ignore Broadcast Address Option */
886 #if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_NTPSERVER)
887 case 42: /* NTP server IP */
888 net_copy_ip(&net_ntp_server
, (popt
+ 2));
892 net_copy_u32(&dhcp_leasetime
, (u32
*)(popt
+ 2));
895 dhcp_option_overload
= popt
[2];
897 case 53: /* Ignore Message Type Option */
900 net_copy_ip(&dhcp_server_ip
, (popt
+ 2));
902 case 58: /* Ignore Renewal Time Option */
904 case 59: /* Ignore Rebinding Time Option */
906 case 66: /* Ignore TFTP server name */
908 case 67: /* Bootfile option */
909 if (!net_boot_file_name_explicit
) {
910 size
= truncate_sz("Bootfile",
911 sizeof(net_boot_file_name
),
913 memcpy(&net_boot_file_name
, popt
+ 2, size
);
914 net_boot_file_name
[size
] = 0;
917 case 209: /* PXELINUX Config File */
918 if (IS_ENABLED(CONFIG_BOOTP_PXE_DHCP_OPTION
)) {
919 /* In case it has already been allocated when get DHCP Offer packet,
920 * free first to avoid memory leak.
922 if (pxelinux_configfile
)
923 free(pxelinux_configfile
);
925 pxelinux_configfile
= (char *)malloc((oplen
+ 1) * sizeof(char));
927 if (pxelinux_configfile
)
928 strlcpy(pxelinux_configfile
, popt
+ 2, oplen
+ 1);
930 printf("Error: Failed to allocate pxelinux_configfile\n");
934 #if defined(CONFIG_BOOTP_VENDOREX)
935 if (dhcp_vendorex_proc(popt
))
938 printf("*** Unhandled DHCP Option in OFFER/ACK:"
942 popt
+= oplen
+ 2; /* Process next option */
946 static void dhcp_packet_process_options(struct bootp_hdr
*bp
)
948 uchar
*popt
= (uchar
*)&bp
->bp_vend
[4];
949 uchar
*end
= popt
+ BOOTP_HDR_SIZE
;
951 if (net_read_u32((u32
*)&bp
->bp_vend
[0]) != htonl(BOOTP_VENDOR_MAGIC
))
954 dhcp_option_overload
= 0;
957 * The 'options' field MUST be interpreted first, 'file' next,
960 dhcp_process_options(popt
, end
);
962 if (dhcp_option_overload
& OVERLOAD_FILE
) {
963 popt
= (uchar
*)bp
->bp_file
;
964 end
= popt
+ sizeof(bp
->bp_file
);
965 dhcp_process_options(popt
, end
);
968 if (dhcp_option_overload
& OVERLOAD_SNAME
) {
969 popt
= (uchar
*)bp
->bp_sname
;
970 end
= popt
+ sizeof(bp
->bp_sname
);
971 dhcp_process_options(popt
, end
);
975 static int dhcp_message_type(unsigned char *popt
)
977 if (net_read_u32((u32
*)popt
) != htonl(BOOTP_VENDOR_MAGIC
))
981 while (*popt
!= 0xff) {
982 if (*popt
== 53) /* DHCP Message Type */
988 /* Scan through all options */
989 popt
+= *(popt
+ 1) + 2;
995 static void dhcp_send_request_packet(struct bootp_hdr
*bp_offer
)
998 struct bootp_hdr
*bp
;
999 int pktlen
, iplen
, extlen
;
1001 struct in_addr offered_ip
;
1002 struct in_addr zero_ip
;
1003 struct in_addr bcast_ip
;
1005 debug("dhcp_send_request_packet: Sending DHCPREQUEST\n");
1006 pkt
= net_tx_packet
;
1007 memset((void *)pkt
, 0, PKTSIZE
);
1009 eth_hdr_size
= net_set_ether(pkt
, net_bcast_ethaddr
, PROT_IP
);
1010 pkt
+= eth_hdr_size
;
1012 iphdr
= pkt
; /* We'll need this later to set proper pkt size */
1013 pkt
+= IP_UDP_HDR_SIZE
;
1015 bp
= (struct bootp_hdr
*)pkt
;
1016 bp
->bp_op
= OP_BOOTREQUEST
;
1017 bp
->bp_htype
= HWT_ETHER
;
1018 bp
->bp_hlen
= HWL_ETHER
;
1020 bp
->bp_secs
= htons(get_timer(bootp_start
) / 1000);
1021 /* Do not set the client IP, your IP, or server IP yet, since it
1022 * hasn't been ACK'ed by the server yet */
1025 * RFC3046 requires Relay Agents to discard packets with
1026 * nonzero and offered giaddr
1029 net_write_ip(&bp
->bp_giaddr
, zero_ip
);
1031 memcpy(bp
->bp_chaddr
, net_ethaddr
, 6);
1032 copy_filename(bp
->bp_file
, net_boot_file_name
, sizeof(bp
->bp_file
));
1035 * ID is the id of the OFFER packet
1038 net_copy_u32(&bp
->bp_id
, &bp_offer
->bp_id
);
1041 * Copy options from OFFER packet if present
1044 /* Copy offered IP into the parameters request list */
1045 net_copy_ip(&offered_ip
, &bp_offer
->bp_yiaddr
);
1046 extlen
= dhcp_extended((u8
*)bp
->bp_vend
, DHCP_REQUEST
,
1047 dhcp_server_ip
, offered_ip
);
1049 iplen
= BOOTP_HDR_SIZE
- OPT_FIELD_SIZE
+ extlen
;
1050 pktlen
= eth_hdr_size
+ IP_UDP_HDR_SIZE
+ iplen
;
1051 bcast_ip
.s_addr
= 0xFFFFFFFFL
;
1052 net_set_udp_header(iphdr
, bcast_ip
, PORT_BOOTPS
, PORT_BOOTPC
, iplen
);
1054 debug("Transmitting DHCPREQUEST packet: len = %d\n", pktlen
);
1055 net_send_packet(net_tx_packet
, pktlen
);
1059 * Handle DHCP received packets.
1061 static void dhcp_handler(uchar
*pkt
, unsigned dest
, struct in_addr sip
,
1062 unsigned src
, unsigned len
)
1064 struct bootp_hdr
*bp
= (struct bootp_hdr
*)pkt
;
1066 debug("DHCPHandler: got packet: (src=%d, dst=%d, len=%d) state: %d\n",
1067 src
, dest
, len
, dhcp_state
);
1069 /* Filter out pkts we don't want */
1070 if (check_reply_packet(pkt
, dest
, src
, len
))
1073 debug("DHCPHandler: got DHCP packet: (src=%d, dst=%d, len=%d) state: "
1074 "%d\n", src
, dest
, len
, dhcp_state
);
1076 if (net_read_ip(&bp
->bp_yiaddr
).s_addr
== 0) {
1077 #if defined(CONFIG_SERVERIP_FROM_PROXYDHCP)
1078 store_bootp_params(bp
);
1083 switch (dhcp_state
) {
1086 * Wait an appropriate time for any potential DHCPOFFER packets
1087 * to arrive. Then select one, and generate DHCPREQUEST
1088 * response. If filename is in format we recognize, assume it
1089 * is a valid OFFER from a server we want.
1091 debug("DHCP: state=SELECTING bp_file: \"%s\"\n", bp
->bp_file
);
1092 #ifdef CONFIG_SYS_BOOTFILE_PREFIX
1093 if (strncmp(bp
->bp_file
,
1094 CONFIG_SYS_BOOTFILE_PREFIX
,
1095 strlen(CONFIG_SYS_BOOTFILE_PREFIX
)) == 0) {
1096 #endif /* CONFIG_SYS_BOOTFILE_PREFIX */
1097 if (CONFIG_IS_ENABLED(UNIT_TEST
) &&
1098 dhcp_message_type((u8
*)bp
->bp_vend
) == -1) {
1099 debug("got BOOTP response; transitioning to BOUND\n");
1100 goto dhcp_got_bootp
;
1102 dhcp_packet_process_options(bp
);
1103 if (CONFIG_IS_ENABLED(EFI_LOADER
) &&
1104 IS_ENABLED(CONFIG_NETDEVICES
))
1105 efi_net_set_dhcp_ack(pkt
, len
);
1107 #if defined(CONFIG_SERVERIP_FROM_PROXYDHCP)
1108 if (!net_server_ip
.s_addr
)
1109 udelay(CONFIG_SERVERIP_FROM_PROXYDHCP_DELAY_MS
*
1111 #endif /* CONFIG_SERVERIP_FROM_PROXYDHCP */
1113 debug("TRANSITIONING TO REQUESTING STATE\n");
1114 dhcp_state
= REQUESTING
;
1116 net_set_timeout_handler(5000, bootp_timeout_handler
);
1117 dhcp_send_request_packet(bp
);
1118 #ifdef CONFIG_SYS_BOOTFILE_PREFIX
1120 #endif /* CONFIG_SYS_BOOTFILE_PREFIX */
1125 debug("DHCP State: REQUESTING\n");
1127 if (dhcp_message_type((u8
*)bp
->bp_vend
) == DHCP_ACK
) {
1129 dhcp_packet_process_options(bp
);
1130 /* Store net params from reply */
1131 store_net_params(bp
);
1133 printf("DHCP client bound to address %pI4 (%lu ms)\n",
1134 &net_ip
, get_timer(bootp_start
));
1135 net_set_timeout_handler(0, (thand_f
*)0);
1136 bootstage_mark_name(BOOTSTAGE_ID_BOOTP_STOP
,
1144 /* DHCP client bound to address */
1147 puts("DHCP: INVALID STATE\n");
1152 void dhcp_request(void)
1156 #endif /* CONFIG_CMD_DHCP */