1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) Microsoft Corporation
4 * Author: Sean Edmond <seanedmond@microsoft.com>
8 /* Simple DHCP6 network layer implementation. */
13 #include <linux/delay.h>
17 #define PORT_DHCP6_S 547 /* DHCP6 server UDP port */
18 #define PORT_DHCP6_C 546 /* DHCP6 client UDP port */
20 /* default timeout parameters (in ms) */
21 #define SOL_MAX_DELAY_MS 1000
22 #define SOL_TIMEOUT_MS 1000
23 #define SOL_MAX_RT_MS 3600000
24 #define REQ_TIMEOUT_MS 1000
25 #define REQ_MAX_RT_MS 30000
27 #define MAX_WAIT_TIME_MS 60000
29 /* global variable to track any updates from DHCP6 server */
30 int updated_sol_max_rt_ms
= SOL_MAX_RT_MS
;
31 /* state machine parameters/variables */
32 struct dhcp6_sm_params sm_params
;
34 static void dhcp6_state_machine(bool timeout
, uchar
*rx_pkt
, unsigned int len
);
36 /* Handle DHCP received packets (set as UDP handler) */
37 static void dhcp6_handler(uchar
*pkt
, unsigned int dest
, struct in_addr sip
,
38 unsigned int src
, unsigned int len
)
40 /* return if ports don't match DHCPv6 ports */
41 if (dest
!= PORT_DHCP6_C
|| src
!= PORT_DHCP6_S
)
44 dhcp6_state_machine(false, pkt
, len
);
48 * dhcp6_add_option() - Adds DHCP6 option to a packet
49 * @option_id: The option ID to add (See DHCP6_OPTION_* definitions)
50 * @pkt: A pointer to the current write location of the TX packet
52 * Return: The number of bytes written into "*pkt"
54 static int dhcp6_add_option(int option_id
, uchar
*pkt
)
56 struct dhcp6_option_duid_ll
*duid_opt
;
57 struct dhcp6_option_elapsed_time
*elapsed_time_opt
;
58 struct dhcp6_option_ia_ta
*ia_ta_opt
;
59 struct dhcp6_option_ia_na
*ia_na_opt
;
60 struct dhcp6_option_oro
*oro_opt
;
61 struct dhcp6_option_client_arch
*client_arch_opt
;
62 struct dhcp6_option_vendor_class
*vendor_class_opt
;
67 int num_client_arch
= 0;
69 struct dhcp6_option_hdr
*dhcp_option
= (struct dhcp6_option_hdr
*)pkt
;
70 uchar
*dhcp_option_start
= pkt
+ sizeof(struct dhcp6_option_hdr
);
72 dhcp_option
->option_id
= htons(option_id
);
75 case DHCP6_OPTION_CLIENTID
:
76 /* Only support for DUID-LL in Client ID option for now */
77 duid_opt
= (struct dhcp6_option_duid_ll
*)dhcp_option_start
;
78 duid_opt
->duid_type
= htons(DUID_TYPE_LL
);
79 duid_opt
->hw_type
= htons(DUID_HW_TYPE_ENET
);
80 memcpy(duid_opt
->ll_addr
, net_ethaddr
, ETH_ALEN
);
81 opt_len
= sizeof(struct dhcp6_option_duid_ll
) + ETH_ALEN
;
83 /* Save DUID for comparison later */
84 memcpy(sm_params
.duid
, duid_opt
, opt_len
);
86 case DHCP6_OPTION_ELAPSED_TIME
:
87 /* calculate elapsed time in 1/100th of a second */
88 elapsed_time
= (sm_params
.dhcp6_retry_ms
-
89 sm_params
.dhcp6_start_ms
) / 10;
90 if (elapsed_time
> 0xFFFF)
91 elapsed_time
= 0xFFFF;
93 elapsed_time_opt
= (struct dhcp6_option_elapsed_time
*)dhcp_option_start
;
94 elapsed_time_opt
->elapsed_time
= htons(elapsed_time
);
96 opt_len
= sizeof(struct dhcp6_option_elapsed_time
);
98 case DHCP6_OPTION_IA_TA
:
99 ia_ta_opt
= (struct dhcp6_option_ia_ta
*)dhcp_option_start
;
100 ia_ta_opt
->iaid
= htonl(sm_params
.ia_id
);
102 opt_len
= sizeof(struct dhcp6_option_ia_ta
);
104 case DHCP6_OPTION_IA_NA
:
105 ia_na_opt
= (struct dhcp6_option_ia_na
*)dhcp_option_start
;
106 ia_na_opt
->iaid
= htonl(sm_params
.ia_id
);
107 /* In a message sent by a client to a server,
108 * the T1 and T2 fields SHOULD be set to 0
113 opt_len
= sizeof(struct dhcp6_option_ia_na
);
115 case DHCP6_OPTION_ORO
:
116 oro_opt
= (struct dhcp6_option_oro
*)dhcp_option_start
;
117 oro_opt
->req_option_code
[num_oro
++] = htons(DHCP6_OPTION_OPT_BOOTFILE_URL
);
118 oro_opt
->req_option_code
[num_oro
++] = htons(DHCP6_OPTION_SOL_MAX_RT
);
119 if (IS_ENABLED(CONFIG_DHCP6_PXE_DHCP_OPTION
)) {
120 oro_opt
->req_option_code
[num_oro
++] =
121 htons(DHCP6_OPTION_OPT_BOOTFILE_PARAM
);
124 opt_len
= sizeof(__be16
) * num_oro
;
126 case DHCP6_OPTION_CLIENT_ARCH_TYPE
:
127 client_arch_opt
= (struct dhcp6_option_client_arch
*)dhcp_option_start
;
128 client_arch_opt
->arch_type
[num_client_arch
++] = htons(CONFIG_DHCP6_PXE_CLIENTARCH
);
130 opt_len
= sizeof(__be16
) * num_client_arch
;
132 case DHCP6_OPTION_VENDOR_CLASS
:
133 vendor_class_opt
= (struct dhcp6_option_vendor_class
*)dhcp_option_start
;
134 vendor_class_opt
->enterprise_number
= htonl(CONFIG_DHCP6_ENTERPRISE_ID
);
136 vci_strlen
= strlen(DHCP6_VCI_STRING
);
137 vendor_class_opt
->vendor_class_data
[num_vc_data
].vendor_class_len
=
139 memcpy(vendor_class_opt
->vendor_class_data
[num_vc_data
].opaque_data
,
140 DHCP6_VCI_STRING
, vci_strlen
);
143 opt_len
= sizeof(struct dhcp6_option_vendor_class
) +
144 sizeof(struct vendor_class_data
) * num_vc_data
+
147 case DHCP6_OPTION_NII
:
148 dhcp_option_start
[0] = 1;
149 dhcp_option_start
[1] = 0;
150 dhcp_option_start
[2] = 0;
155 printf("***Warning unknown DHCP6 option %d. Not adding to message\n", option_id
);
158 dhcp_option
->option_len
= htons(opt_len
);
160 return opt_len
+ sizeof(struct dhcp6_option_hdr
);
164 * dhcp6_send_solicit_packet() - Send a SOLICIT packet
166 * Implements RFC 8415:
167 * - 16.2. Solicit Message
168 * - 18.2.1. Creation and Transmission of Solicit Messages
170 * Adds DHCP6 header and DHCP6 options. Sends the UDP packet
171 * and sets the UDP handler.
173 static void dhcp6_send_solicit_packet(void)
175 struct in6_addr dhcp_bcast_ip6
;
178 uchar
*dhcp_pkt_start_ptr
;
179 struct dhcp6_hdr
*dhcp_hdr
;
181 pkt
= net_tx_packet
+ net_eth_hdr_size() + IP6_HDR_SIZE
+ UDP_HDR_SIZE
;
182 dhcp_pkt_start_ptr
= pkt
;
184 /* Add the DHCP6 header */
185 dhcp_hdr
= (struct dhcp6_hdr
*)pkt
;
186 dhcp_hdr
->msg_type
= DHCP6_MSG_SOLICIT
;
187 dhcp_hdr
->trans_id
= htons(sm_params
.trans_id
);
188 pkt
+= sizeof(struct dhcp6_hdr
);
190 /* Add the options */
191 pkt
+= dhcp6_add_option(DHCP6_OPTION_CLIENTID
, pkt
);
192 pkt
+= dhcp6_add_option(DHCP6_OPTION_ELAPSED_TIME
, pkt
);
193 pkt
+= dhcp6_add_option(DHCP6_OPTION_IA_NA
, pkt
);
194 pkt
+= dhcp6_add_option(DHCP6_OPTION_ORO
, pkt
);
195 if (CONFIG_DHCP6_PXE_CLIENTARCH
!= 0xFF)
196 pkt
+= dhcp6_add_option(DHCP6_OPTION_CLIENT_ARCH_TYPE
, pkt
);
197 pkt
+= dhcp6_add_option(DHCP6_OPTION_VENDOR_CLASS
, pkt
);
198 pkt
+= dhcp6_add_option(DHCP6_OPTION_NII
, pkt
);
200 /* calculate packet length */
201 len
= pkt
- dhcp_pkt_start_ptr
;
203 /* send UDP packet to DHCP6 multicast address */
204 string_to_ip6(DHCP6_MULTICAST_ADDR
, sizeof(DHCP6_MULTICAST_ADDR
), &dhcp_bcast_ip6
);
205 net_set_udp_handler(dhcp6_handler
);
206 net_send_udp_packet6((uchar
*)net_bcast_ethaddr
, &dhcp_bcast_ip6
,
207 PORT_DHCP6_S
, PORT_DHCP6_C
, len
);
211 * dhcp6_send_request_packet() - Send a REQUEST packet
213 * * Implements RFC 8415:
214 * - 16.4. Request Message
215 * - 18.2.2. Creation and Transmission of Request Messages
217 * Adds DHCP6 header and DHCP6 options. Sends the UDP packet
218 * and sets the UDP handler.
220 static void dhcp6_send_request_packet(void)
222 struct in6_addr dhcp_bcast_ip6
;
225 uchar
*dhcp_pkt_start_ptr
;
226 struct dhcp6_hdr
*dhcp_hdr
;
228 pkt
= net_tx_packet
+ net_eth_hdr_size() + IP6_HDR_SIZE
+ UDP_HDR_SIZE
;
229 dhcp_pkt_start_ptr
= pkt
;
231 /* Add the DHCP6 header */
232 dhcp_hdr
= (struct dhcp6_hdr
*)pkt
;
233 dhcp_hdr
->msg_type
= DHCP6_MSG_REQUEST
;
234 dhcp_hdr
->trans_id
= htons(sm_params
.trans_id
);
235 pkt
+= sizeof(struct dhcp6_hdr
);
237 /* add the options */
238 pkt
+= dhcp6_add_option(DHCP6_OPTION_CLIENTID
, pkt
);
239 pkt
+= dhcp6_add_option(DHCP6_OPTION_ELAPSED_TIME
, pkt
);
240 pkt
+= dhcp6_add_option(DHCP6_OPTION_IA_NA
, pkt
);
241 pkt
+= dhcp6_add_option(DHCP6_OPTION_ORO
, pkt
);
242 /* copy received IA_TA/IA_NA into the REQUEST packet */
243 if (sm_params
.server_uid
.uid_ptr
) {
244 memcpy(pkt
, sm_params
.server_uid
.uid_ptr
, sm_params
.server_uid
.uid_size
);
245 pkt
+= sm_params
.server_uid
.uid_size
;
247 if (CONFIG_DHCP6_PXE_CLIENTARCH
!= 0xFF)
248 pkt
+= dhcp6_add_option(DHCP6_OPTION_CLIENT_ARCH_TYPE
, pkt
);
249 pkt
+= dhcp6_add_option(DHCP6_OPTION_VENDOR_CLASS
, pkt
);
250 pkt
+= dhcp6_add_option(DHCP6_OPTION_NII
, pkt
);
252 /* calculate packet length */
253 len
= pkt
- dhcp_pkt_start_ptr
;
255 /* send UDP packet to DHCP6 multicast address */
256 string_to_ip6(DHCP6_MULTICAST_ADDR
, strlen(DHCP6_MULTICAST_ADDR
), &dhcp_bcast_ip6
);
257 net_set_udp_handler(dhcp6_handler
);
258 net_send_udp_packet6((uchar
*)net_bcast_ethaddr
, &dhcp_bcast_ip6
,
259 PORT_DHCP6_S
, PORT_DHCP6_C
, len
);
262 static void dhcp6_parse_ia_options(struct dhcp6_option_hdr
*ia_ptr
, uchar
*ia_option_ptr
)
264 struct dhcp6_option_hdr
*ia_option_hdr
;
266 ia_option_hdr
= (struct dhcp6_option_hdr
*)ia_option_ptr
;
268 /* Search for options encapsulated in IA_NA/IA_TA (DHCP6_OPTION_IAADDR
269 * or DHCP6_OPTION_STATUS_CODE)
271 while (ia_option_ptr
< ((uchar
*)ia_ptr
+ ntohs(ia_ptr
->option_len
))) {
272 switch (ntohs(ia_option_hdr
->option_id
)) {
273 case DHCP6_OPTION_IAADDR
:
274 sm_params
.rx_status
.ia_addr_found
= true;
275 net_copy_ip6(&sm_params
.rx_status
.ia_addr_ipv6
,
276 (ia_option_ptr
+ sizeof(struct dhcp6_hdr
)));
277 debug("DHCP6_OPTION_IAADDR FOUND\n");
279 case DHCP6_OPTION_STATUS_CODE
:
280 sm_params
.rx_status
.ia_status_code
=
281 ntohs(*((u16
*)(ia_option_ptr
+ sizeof(struct dhcp6_hdr
))));
282 printf("ERROR : IA STATUS %d\n", sm_params
.rx_status
.ia_status_code
);
285 debug("Unknown Option in IA, skipping\n");
289 ia_option_ptr
+= ntohs(((struct dhcp6_option_hdr
*)ia_option_ptr
)->option_len
);
294 * dhcp6_parse_options() - Parse the DHCP6 options
296 * @rx_pkt: pointer to beginning of received DHCP6 packet
297 * @len: Total length of the DHCP6 packet
299 * Parses the DHCP options from a received DHCP packet. Perform error checking
300 * on the options received. Any relevant status is available in:
301 * "sm_params.rx_status"
304 static void dhcp6_parse_options(uchar
*rx_pkt
, unsigned int len
)
307 int sol_max_rt_sec
, option_len
, param_len_1
;
309 struct dhcp6_option_hdr
*option_hdr
;
311 memset(&sm_params
.rx_status
, 0, sizeof(struct dhcp6_rx_pkt_status
));
313 option_hdr
= (struct dhcp6_option_hdr
*)(rx_pkt
+ sizeof(struct dhcp6_hdr
));
314 /* check that required options exist */
315 while (option_hdr
< (struct dhcp6_option_hdr
*)(rx_pkt
+ len
)) {
316 option_ptr
= ((uchar
*)option_hdr
) + sizeof(struct dhcp6_hdr
);
317 option_len
= ntohs(option_hdr
->option_len
);
319 if (option_ptr
+ option_len
> rx_pkt
+ len
) {
320 debug("Invalid option length\n");
324 switch (ntohs(option_hdr
->option_id
)) {
325 case DHCP6_OPTION_CLIENTID
:
326 if (memcmp(option_ptr
, sm_params
.duid
, option_len
)
328 debug("CLIENT ID DOESN'T MATCH\n");
330 debug("CLIENT ID FOUND and MATCHES\n");
331 sm_params
.rx_status
.client_id_match
= true;
334 case DHCP6_OPTION_SERVERID
:
335 sm_params
.rx_status
.server_id_found
= true;
336 sm_params
.rx_status
.server_uid_ptr
= (uchar
*)option_hdr
;
337 sm_params
.rx_status
.server_uid_size
= option_len
+
338 sizeof(struct dhcp6_option_hdr
);
339 debug("SERVER ID FOUND\n");
341 case DHCP6_OPTION_IA_TA
:
342 case DHCP6_OPTION_IA_NA
:
343 /* check the IA_ID */
344 if (*((u32
*)option_ptr
) != htonl(sm_params
.ia_id
)) {
345 debug("IA_ID mismatch 0x%08x 0x%08x\n",
346 *((u32
*)option_ptr
), htonl(sm_params
.ia_id
));
350 if (ntohs(option_hdr
->option_id
) == DHCP6_OPTION_IA_NA
) {
351 /* skip past IA_ID/T1/T2 */
352 option_ptr
+= 3 * sizeof(u32
);
353 } else if (ntohs(option_hdr
->option_id
) == DHCP6_OPTION_IA_TA
) {
354 /* skip past IA_ID */
355 option_ptr
+= sizeof(u32
);
357 /* parse the IA_NA/IA_TA encapsulated options */
358 dhcp6_parse_ia_options(option_hdr
, option_ptr
);
360 case DHCP6_OPTION_STATUS_CODE
:
361 debug("DHCP6_OPTION_STATUS_CODE FOUND\n");
362 sm_params
.rx_status
.status_code
= ntohs(*((u16
*)option_ptr
));
363 debug("DHCP6 top-level status code %d\n", sm_params
.rx_status
.status_code
);
364 debug("DHCP6 status message: %.*s\n", len
, option_ptr
+ 2);
366 case DHCP6_OPTION_SOL_MAX_RT
:
367 debug("DHCP6_OPTION_SOL_MAX_RT FOUND\n");
368 sol_max_rt_sec
= ntohl(*((u32
*)option_ptr
));
370 /* A DHCP client MUST ignore any SOL_MAX_RT option values that are less
371 * than 60 or more than 86400
373 if (sol_max_rt_sec
>= 60 && sol_max_rt_sec
<= 86400) {
374 updated_sol_max_rt_ms
= sol_max_rt_sec
* 1000;
375 if (sm_params
.curr_state
== DHCP6_SOLICIT
)
376 sm_params
.mrt_ms
= updated_sol_max_rt_ms
;
379 case DHCP6_OPTION_OPT_BOOTFILE_URL
:
380 debug("DHCP6_OPTION_OPT_BOOTFILE_URL FOUND\n");
381 copy_filename(net_boot_file_name
, option_ptr
, option_len
+ 1);
382 debug("net_boot_file_name: %s\n", net_boot_file_name
);
384 /* copy server_ip6 (required for PXE) */
385 s
= strchr(net_boot_file_name
, '[');
386 e
= strchr(net_boot_file_name
, ']');
388 string_to_ip6(s
+ 1, e
- s
- 1, &net_server_ip6
);
390 case DHCP6_OPTION_OPT_BOOTFILE_PARAM
:
391 if (IS_ENABLED(CONFIG_DHCP6_PXE_DHCP_OPTION
)) {
392 debug("DHCP6_OPTION_OPT_BOOTFILE_PARAM FOUND\n");
393 /* if CONFIG_DHCP6_PXE_DHCP_OPTION is set the PXE config file path
394 * is contained in the first OPT_BOOTFILE_PARAM argument
396 param_len_1
= ntohs(*((u16
*)option_ptr
));
397 option_ptr
+= sizeof(u16
);
398 if (param_len_1
+ sizeof(u16
) > option_len
) {
399 debug("Invalid BOOTFILE_PARAM param_len_1. Skipping\n");
403 if (pxelinux_configfile
)
404 free(pxelinux_configfile
);
406 pxelinux_configfile
= (char *)malloc((param_len_1
+ 1) *
408 if (pxelinux_configfile
)
409 strlcpy(pxelinux_configfile
, option_ptr
, param_len_1
+ 1);
411 printf("Error: Failed to allocate pxelinux_configfile\n");
413 debug("PXE CONFIG FILE %s\n", pxelinux_configfile
);
416 case DHCP6_OPTION_PREFERENCE
:
417 debug("DHCP6_OPTION_PREFERENCE FOUND\n");
418 sm_params
.rx_status
.preference
= *option_ptr
;
421 debug("Unknown Option ID: %d, skipping parsing\n",
422 ntohs(option_hdr
->option_id
));
425 /* Increment to next option header */
426 option_hdr
= (struct dhcp6_option_hdr
*)(((uchar
*)option_hdr
) +
427 sizeof(struct dhcp6_option_hdr
) + option_len
);
432 * dhcp6_check_advertise_packet() - Perform error checking on an expected
435 * @rx_pkt: pointer to beginning of received DHCP6 packet
436 * @len: Total length of the DHCP6 packet
438 * Implements RFC 8415:
439 * - 16.3. Advertise Message
440 * - 18.2.10. Receipt of Reply Messages
442 * Return : 0 : ADVERTISE packet was received with no errors.
443 * State machine can progress
444 * 1 : - packet received is not an ADVERTISE packet
445 * - there were errors in the packet received,
446 * - this is the first SOLICIT packet, but
447 * received preference is not 255, so we have
448 * to wait for more server responses.
450 static int dhcp6_check_advertise_packet(uchar
*rx_pkt
, unsigned int len
)
453 struct dhcp6_hdr
*dhcp6_hdr
= (struct dhcp6_hdr
*)rx_pkt
;
455 /* Ignore message if msg-type != advertise */
456 if (dhcp6_hdr
->msg_type
!= DHCP6_MSG_ADVERTISE
)
458 /* Ignore message if transaction ID doesn't match */
459 if (dhcp6_hdr
->trans_id
!= htons(sm_params
.trans_id
))
462 dhcp6_parse_options(rx_pkt
, len
);
464 /* Ignore advertise if any of these conditions met */
465 if (!sm_params
.rx_status
.server_id_found
||
466 !sm_params
.rx_status
.client_id_match
||
467 sm_params
.rx_status
.status_code
!= DHCP6_SUCCESS
) {
471 if (sm_params
.rx_status
.server_id_found
) {
472 /* if no server UID has been received yet, or if the server UID
473 * received has a higher preference value than the currently saved
474 * server UID, save the new server UID and preference
476 if (!sm_params
.server_uid
.uid_ptr
||
477 (sm_params
.server_uid
.uid_ptr
&&
478 sm_params
.server_uid
.preference
< sm_params
.rx_status
.preference
)) {
479 rx_uid_size
= sm_params
.rx_status
.server_uid_size
;
480 if (sm_params
.server_uid
.uid_ptr
)
481 free(sm_params
.server_uid
.uid_ptr
);
482 sm_params
.server_uid
.uid_ptr
= malloc(rx_uid_size
* sizeof(uchar
));
483 if (sm_params
.server_uid
.uid_ptr
)
484 memcpy(sm_params
.server_uid
.uid_ptr
,
485 sm_params
.rx_status
.server_uid_ptr
, rx_uid_size
);
487 sm_params
.server_uid
.uid_size
= rx_uid_size
;
488 sm_params
.server_uid
.preference
= sm_params
.rx_status
.preference
;
491 /* If the first SOLICIT and preference code is 255, use right away.
492 * Otherwise, wait for the first SOLICIT period for more
493 * DHCP6 servers to respond.
495 if (sm_params
.retry_cnt
== 1 &&
496 sm_params
.server_uid
.preference
!= 255) {
497 debug("valid ADVERTISE, waiting for first SOLICIT period\n");
506 * dhcp6_check_reply_packet() - Perform error checking on an expected
509 * @rx_pkt: pointer to beginning of received DHCP6 packet
510 * @len: Total length of the DHCP6 packet
512 * Implements RFC 8415:
513 * - 16.10. Reply Message
514 * - 18.2.10. Receipt of Reply Messages
516 * Return : 0 - REPLY packet was received with no errors
517 * 1 - packet received is not an REPLY packet,
518 * or there were errors in the packet received
520 static int dhcp6_check_reply_packet(uchar
*rx_pkt
, unsigned int len
)
522 struct dhcp6_hdr
*dhcp6_hdr
= (struct dhcp6_hdr
*)rx_pkt
;
524 /* Ignore message if msg-type != reply */
525 if (dhcp6_hdr
->msg_type
!= DHCP6_MSG_REPLY
)
527 /* check that transaction ID matches */
528 if (dhcp6_hdr
->trans_id
!= htons(sm_params
.trans_id
))
531 dhcp6_parse_options(rx_pkt
, len
);
533 /* if no addresses found, restart DHCP */
534 if (!sm_params
.rx_status
.ia_addr_found
||
535 sm_params
.rx_status
.ia_status_code
== DHCP6_NO_ADDRS_AVAIL
||
536 sm_params
.rx_status
.status_code
== DHCP6_NOT_ON_LINK
) {
538 debug("No address found in reply. Restarting DHCP\n");
542 /* ignore reply if any of these conditions met */
543 if (!sm_params
.rx_status
.server_id_found
||
544 !sm_params
.rx_status
.client_id_match
||
545 sm_params
.rx_status
.status_code
== DHCP6_UNSPEC_FAIL
) {
552 /* Timeout for DHCP6 SOLICIT/REQUEST */
553 static void dhcp6_timeout_handler(void)
555 /* call state machine with the timeout flag */
556 dhcp6_state_machine(true, NULL
, 0);
560 * dhcp6_state_machine() - DHCP6 state machine
562 * @timeout: TRUE : timeout waiting for response from
564 * FALSE : init or received response from DHCP6 server
565 * @rx_pkt: Pointer to the beginning of received DHCP6 packet.
566 * Will be NULL if called as part of init
568 * @len: Total length of the DHCP6 packet if rx_pkt != NULL
570 * Implements RFC 8415:
571 * - 5.2. Client/Server Exchanges Involving Four Messages
572 * - 15. Reliability of Client-Initiated Message Exchanges
575 * - transmission of SOLICIT and REQUEST packets
576 * - retransmission of SOLICIT and REQUEST packets if no
577 * response is received within the timeout window
578 * - checking received ADVERTISE and REPLY packets to
579 * assess if the DHCP state machine can progress
581 static void dhcp6_state_machine(bool timeout
, uchar
*rx_pkt
, unsigned int len
)
583 int rand_minus_plus_100
;
585 switch (sm_params
.curr_state
) {
587 sm_params
.next_state
= DHCP6_SOLICIT
;
591 /* check the rx packet and determine if we can transition to next
594 if (dhcp6_check_advertise_packet(rx_pkt
, len
))
597 debug("ADVERTISE good, transition to REQUEST\n");
598 sm_params
.next_state
= DHCP6_REQUEST
;
599 } else if (sm_params
.retry_cnt
== 1) {
600 /* If a server UID was received in the first SOLICIT period
601 * transition to REQUEST
603 if (sm_params
.server_uid
.uid_ptr
)
604 sm_params
.next_state
= DHCP6_REQUEST
;
609 /* check the rx packet and determine if we can transition to next state */
610 if (dhcp6_check_reply_packet(rx_pkt
, len
))
613 debug("REPLY good, transition to DONE\n");
614 sm_params
.next_state
= DHCP6_DONE
;
619 /* Shouldn't get here, as state machine should exit
620 * immediately when DHCP6_DONE or DHCP6_FAIL is entered.
621 * Proceed anyway to proceed DONE/FAIL actions
623 debug("Unexpected DHCP6 state : %d\n", sm_params
.curr_state
);
626 /* re-seed the RNG */
627 srand(get_ticks() + rand());
629 /* handle state machine entry conditions */
630 if (sm_params
.curr_state
!= sm_params
.next_state
) {
631 sm_params
.retry_cnt
= 0;
633 if (sm_params
.next_state
== DHCP6_SOLICIT
) {
634 /* delay a random ammount (special for SOLICIT) */
635 udelay((rand() % SOL_MAX_DELAY_MS
) * 1000);
636 /* init timestamp variables after SOLICIT delay */
637 sm_params
.dhcp6_start_ms
= get_timer(0);
638 sm_params
.dhcp6_retry_start_ms
= sm_params
.dhcp6_start_ms
;
639 sm_params
.dhcp6_retry_ms
= sm_params
.dhcp6_start_ms
;
640 /* init transaction and ia_id */
641 sm_params
.trans_id
= rand() & 0xFFFFFF;
642 sm_params
.ia_id
= rand();
643 /* initialize retransmission parameters */
644 sm_params
.irt_ms
= SOL_TIMEOUT_MS
;
645 sm_params
.mrt_ms
= updated_sol_max_rt_ms
;
646 /* RFCs default MRC is be 0 (try infinitely)
647 * give up after CONFIG_NET_RETRY_COUNT number of tries (same as DHCPv4)
649 sm_params
.mrc
= CONFIG_NET_RETRY_COUNT
;
650 sm_params
.mrd_ms
= 0;
652 } else if (sm_params
.next_state
== DHCP6_REQUEST
) {
653 /* init timestamp variables */
654 sm_params
.dhcp6_retry_start_ms
= get_timer(0);
655 sm_params
.dhcp6_retry_ms
= sm_params
.dhcp6_start_ms
;
656 /* initialize retransmission parameters */
657 sm_params
.irt_ms
= REQ_TIMEOUT_MS
;
658 sm_params
.mrt_ms
= REQ_MAX_RT_MS
;
659 sm_params
.mrc
= REQ_MAX_RC
;
660 sm_params
.mrd_ms
= 0;
665 sm_params
.dhcp6_retry_ms
= get_timer(0);
667 /* Check if MRC or MRD have been passed */
668 if ((sm_params
.mrc
!= 0 &&
669 sm_params
.retry_cnt
>= sm_params
.mrc
) ||
670 (sm_params
.mrd_ms
!= 0 &&
671 ((sm_params
.dhcp6_retry_ms
- sm_params
.dhcp6_retry_start_ms
) >= sm_params
.mrd_ms
))) {
672 sm_params
.next_state
= DHCP6_FAIL
;
675 /* calculate retransmission timeout (RT) */
676 rand_minus_plus_100
= ((rand() % 200) - 100);
677 if (sm_params
.retry_cnt
== 0) {
678 sm_params
.rt_ms
= sm_params
.irt_ms
+
679 ((sm_params
.irt_ms
* rand_minus_plus_100
) / 1000);
681 sm_params
.rt_ms
= (2 * sm_params
.rt_prev_ms
) +
682 ((sm_params
.rt_prev_ms
* rand_minus_plus_100
) / 1000);
685 if (sm_params
.rt_ms
> sm_params
.mrt_ms
) {
686 sm_params
.rt_ms
= sm_params
.mrt_ms
+
687 ((sm_params
.mrt_ms
* rand_minus_plus_100
) / 1000);
690 sm_params
.rt_prev_ms
= sm_params
.rt_ms
;
692 net_set_timeout_handler(sm_params
.rt_ms
, dhcp6_timeout_handler
);
694 /* send transmit/retransmit message or fail */
695 sm_params
.curr_state
= sm_params
.next_state
;
697 if (sm_params
.curr_state
== DHCP6_SOLICIT
) {
698 /* send solicit packet */
699 dhcp6_send_solicit_packet();
700 printf("DHCP6 SOLICIT %d\n", sm_params
.retry_cnt
);
701 } else if (sm_params
.curr_state
== DHCP6_REQUEST
) {
702 /* send request packet */
703 dhcp6_send_request_packet();
704 printf("DHCP6 REQUEST %d\n", sm_params
.retry_cnt
);
705 } else if (sm_params
.curr_state
== DHCP6_DONE
) {
706 net_set_timeout_handler(0, NULL
);
708 /* Duplicate address detection (DAD) should be
709 * performed here before setting net_ip6
710 * (enhancement should be considered)
712 net_copy_ip6(&net_ip6
, &sm_params
.rx_status
.ia_addr_ipv6
);
713 printf("DHCP6 client bound to %pI6c\n", &net_ip6
);
714 /* will load with TFTP6 */
716 } else if (sm_params
.curr_state
== DHCP6_FAIL
) {
717 printf("DHCP6 FAILED, TERMINATING\n");
718 net_set_state(NETLOOP_FAIL
);
720 sm_params
.retry_cnt
++;
723 /* Start or restart DHCP6 */
724 void dhcp6_start(void)
726 memset(&sm_params
, 0, sizeof(struct dhcp6_sm_params
));
728 /* seed the RNG with MAC address */
731 sm_params
.curr_state
= DHCP6_INIT
;
732 dhcp6_state_machine(false, NULL
, 0);