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
;
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");
394 if (pxelinux_configfile
)
395 free(pxelinux_configfile
);
397 pxelinux_configfile
= (char *)malloc((option_len
+ 1) *
399 if (pxelinux_configfile
)
400 strlcpy(pxelinux_configfile
, option_ptr
, option_len
+ 1);
402 printf("Error: Failed to allocate pxelinux_configfile\n");
404 debug("PXE CONFIG FILE %s\n", pxelinux_configfile
);
407 case DHCP6_OPTION_PREFERENCE
:
408 debug("DHCP6_OPTION_PREFERENCE FOUND\n");
409 sm_params
.rx_status
.preference
= *option_ptr
;
412 debug("Unknown Option ID: %d, skipping parsing\n",
413 ntohs(option_hdr
->option_id
));
416 /* Increment to next option header */
417 option_hdr
= (struct dhcp6_option_hdr
*)(((uchar
*)option_hdr
) +
418 sizeof(struct dhcp6_option_hdr
) + option_len
);
423 * dhcp6_check_advertise_packet() - Perform error checking on an expected
426 * @rx_pkt: pointer to beginning of received DHCP6 packet
427 * @len: Total length of the DHCP6 packet
429 * Implements RFC 8415:
430 * - 16.3. Advertise Message
431 * - 18.2.10. Receipt of Reply Messages
433 * Return : 0 : ADVERTISE packet was received with no errors.
434 * State machine can progress
435 * 1 : - packet received is not an ADVERTISE packet
436 * - there were errors in the packet received,
437 * - this is the first SOLICIT packet, but
438 * received preference is not 255, so we have
439 * to wait for more server responses.
441 static int dhcp6_check_advertise_packet(uchar
*rx_pkt
, unsigned int len
)
444 struct dhcp6_hdr
*dhcp6_hdr
= (struct dhcp6_hdr
*)rx_pkt
;
446 /* Ignore message if msg-type != advertise */
447 if (dhcp6_hdr
->msg_type
!= DHCP6_MSG_ADVERTISE
)
449 /* Ignore message if transaction ID doesn't match */
450 if (dhcp6_hdr
->trans_id
!= htons(sm_params
.trans_id
))
453 dhcp6_parse_options(rx_pkt
, len
);
455 /* Ignore advertise if any of these conditions met */
456 if (!sm_params
.rx_status
.server_id_found
||
457 !sm_params
.rx_status
.client_id_match
||
458 sm_params
.rx_status
.status_code
!= DHCP6_SUCCESS
) {
462 if (sm_params
.rx_status
.server_id_found
) {
463 /* if no server UID has been received yet, or if the server UID
464 * received has a higher preference value than the currently saved
465 * server UID, save the new server UID and preference
467 if (!sm_params
.server_uid
.uid_ptr
||
468 (sm_params
.server_uid
.uid_ptr
&&
469 sm_params
.server_uid
.preference
< sm_params
.rx_status
.preference
)) {
470 rx_uid_size
= sm_params
.rx_status
.server_uid_size
;
471 if (sm_params
.server_uid
.uid_ptr
)
472 free(sm_params
.server_uid
.uid_ptr
);
473 sm_params
.server_uid
.uid_ptr
= malloc(rx_uid_size
* sizeof(uchar
));
474 if (sm_params
.server_uid
.uid_ptr
)
475 memcpy(sm_params
.server_uid
.uid_ptr
,
476 sm_params
.rx_status
.server_uid_ptr
, rx_uid_size
);
478 sm_params
.server_uid
.uid_size
= rx_uid_size
;
479 sm_params
.server_uid
.preference
= sm_params
.rx_status
.preference
;
482 /* If the first SOLICIT and preference code is 255, use right away.
483 * Otherwise, wait for the first SOLICIT period for more
484 * DHCP6 servers to respond.
486 if (sm_params
.retry_cnt
== 1 &&
487 sm_params
.server_uid
.preference
!= 255) {
488 debug("valid ADVERTISE, waiting for first SOLICIT period\n");
497 * dhcp6_check_reply_packet() - Perform error checking on an expected
500 * @rx_pkt: pointer to beginning of received DHCP6 packet
501 * @len: Total length of the DHCP6 packet
503 * Implements RFC 8415:
504 * - 16.10. Reply Message
505 * - 18.2.10. Receipt of Reply Messages
507 * Return : 0 - REPLY packet was received with no errors
508 * 1 - packet received is not an REPLY packet,
509 * or there were errors in the packet received
511 static int dhcp6_check_reply_packet(uchar
*rx_pkt
, unsigned int len
)
513 struct dhcp6_hdr
*dhcp6_hdr
= (struct dhcp6_hdr
*)rx_pkt
;
515 /* Ignore message if msg-type != reply */
516 if (dhcp6_hdr
->msg_type
!= DHCP6_MSG_REPLY
)
518 /* check that transaction ID matches */
519 if (dhcp6_hdr
->trans_id
!= htons(sm_params
.trans_id
))
522 dhcp6_parse_options(rx_pkt
, len
);
524 /* if no addresses found, restart DHCP */
525 if (!sm_params
.rx_status
.ia_addr_found
||
526 sm_params
.rx_status
.ia_status_code
== DHCP6_NO_ADDRS_AVAIL
||
527 sm_params
.rx_status
.status_code
== DHCP6_NOT_ON_LINK
) {
529 debug("No address found in reply. Restarting DHCP\n");
533 /* ignore reply if any of these conditions met */
534 if (!sm_params
.rx_status
.server_id_found
||
535 !sm_params
.rx_status
.client_id_match
||
536 sm_params
.rx_status
.status_code
== DHCP6_UNSPEC_FAIL
) {
543 /* Timeout for DHCP6 SOLICIT/REQUEST */
544 static void dhcp6_timeout_handler(void)
546 /* call state machine with the timeout flag */
547 dhcp6_state_machine(true, NULL
, 0);
551 * dhcp6_state_machine() - DHCP6 state machine
553 * @timeout: TRUE : timeout waiting for response from
555 * FALSE : init or received response from DHCP6 server
556 * @rx_pkt: Pointer to the beginning of received DHCP6 packet.
557 * Will be NULL if called as part of init
559 * @len: Total length of the DHCP6 packet if rx_pkt != NULL
561 * Implements RFC 8415:
562 * - 5.2. Client/Server Exchanges Involving Four Messages
563 * - 15. Reliability of Client-Initiated Message Exchanges
566 * - transmission of SOLICIT and REQUEST packets
567 * - retransmission of SOLICIT and REQUEST packets if no
568 * response is received within the timeout window
569 * - checking received ADVERTISE and REPLY packets to
570 * assess if the DHCP state machine can progress
572 static void dhcp6_state_machine(bool timeout
, uchar
*rx_pkt
, unsigned int len
)
574 int rand_minus_plus_100
;
576 switch (sm_params
.curr_state
) {
578 sm_params
.next_state
= DHCP6_SOLICIT
;
582 /* check the rx packet and determine if we can transition to next
585 if (dhcp6_check_advertise_packet(rx_pkt
, len
))
588 debug("ADVERTISE good, transition to REQUEST\n");
589 sm_params
.next_state
= DHCP6_REQUEST
;
590 } else if (sm_params
.retry_cnt
== 1) {
591 /* If a server UID was received in the first SOLICIT period
592 * transition to REQUEST
594 if (sm_params
.server_uid
.uid_ptr
)
595 sm_params
.next_state
= DHCP6_REQUEST
;
600 /* check the rx packet and determine if we can transition to next state */
601 if (dhcp6_check_reply_packet(rx_pkt
, len
))
604 debug("REPLY good, transition to DONE\n");
605 sm_params
.next_state
= DHCP6_DONE
;
610 /* Shouldn't get here, as state machine should exit
611 * immediately when DHCP6_DONE or DHCP6_FAIL is entered.
612 * Proceed anyway to proceed DONE/FAIL actions
614 debug("Unexpected DHCP6 state : %d\n", sm_params
.curr_state
);
617 /* re-seed the RNG */
618 srand(get_ticks() + rand());
620 /* handle state machine entry conditions */
621 if (sm_params
.curr_state
!= sm_params
.next_state
) {
622 sm_params
.retry_cnt
= 0;
624 if (sm_params
.next_state
== DHCP6_SOLICIT
) {
625 /* delay a random ammount (special for SOLICIT) */
626 udelay((rand() % SOL_MAX_DELAY_MS
) * 1000);
627 /* init timestamp variables after SOLICIT delay */
628 sm_params
.dhcp6_start_ms
= get_timer(0);
629 sm_params
.dhcp6_retry_start_ms
= sm_params
.dhcp6_start_ms
;
630 sm_params
.dhcp6_retry_ms
= sm_params
.dhcp6_start_ms
;
631 /* init transaction and ia_id */
632 sm_params
.trans_id
= rand() & 0xFFFFFF;
633 sm_params
.ia_id
= rand();
634 /* initialize retransmission parameters */
635 sm_params
.irt_ms
= SOL_TIMEOUT_MS
;
636 sm_params
.mrt_ms
= updated_sol_max_rt_ms
;
637 /* RFCs default MRC is be 0 (try infinitely)
638 * give up after CONFIG_NET_RETRY_COUNT number of tries (same as DHCPv4)
640 sm_params
.mrc
= CONFIG_NET_RETRY_COUNT
;
641 sm_params
.mrd_ms
= 0;
643 } else if (sm_params
.next_state
== DHCP6_REQUEST
) {
644 /* init timestamp variables */
645 sm_params
.dhcp6_retry_start_ms
= get_timer(0);
646 sm_params
.dhcp6_retry_ms
= sm_params
.dhcp6_start_ms
;
647 /* initialize retransmission parameters */
648 sm_params
.irt_ms
= REQ_TIMEOUT_MS
;
649 sm_params
.mrt_ms
= REQ_MAX_RT_MS
;
650 sm_params
.mrc
= REQ_MAX_RC
;
651 sm_params
.mrd_ms
= 0;
656 sm_params
.dhcp6_retry_ms
= get_timer(0);
658 /* Check if MRC or MRD have been passed */
659 if ((sm_params
.mrc
!= 0 &&
660 sm_params
.retry_cnt
>= sm_params
.mrc
) ||
661 (sm_params
.mrd_ms
!= 0 &&
662 ((sm_params
.dhcp6_retry_ms
- sm_params
.dhcp6_retry_start_ms
) >= sm_params
.mrd_ms
))) {
663 sm_params
.next_state
= DHCP6_FAIL
;
666 /* calculate retransmission timeout (RT) */
667 rand_minus_plus_100
= ((rand() % 200) - 100);
668 if (sm_params
.retry_cnt
== 0) {
669 sm_params
.rt_ms
= sm_params
.irt_ms
+
670 ((sm_params
.irt_ms
* rand_minus_plus_100
) / 1000);
672 sm_params
.rt_ms
= (2 * sm_params
.rt_prev_ms
) +
673 ((sm_params
.rt_prev_ms
* rand_minus_plus_100
) / 1000);
676 if (sm_params
.rt_ms
> sm_params
.mrt_ms
) {
677 sm_params
.rt_ms
= sm_params
.mrt_ms
+
678 ((sm_params
.mrt_ms
* rand_minus_plus_100
) / 1000);
681 sm_params
.rt_prev_ms
= sm_params
.rt_ms
;
683 net_set_timeout_handler(sm_params
.rt_ms
, dhcp6_timeout_handler
);
685 /* send transmit/retransmit message or fail */
686 sm_params
.curr_state
= sm_params
.next_state
;
688 if (sm_params
.curr_state
== DHCP6_SOLICIT
) {
689 /* send solicit packet */
690 dhcp6_send_solicit_packet();
691 printf("DHCP6 SOLICIT %d\n", sm_params
.retry_cnt
);
692 } else if (sm_params
.curr_state
== DHCP6_REQUEST
) {
693 /* send request packet */
694 dhcp6_send_request_packet();
695 printf("DHCP6 REQUEST %d\n", sm_params
.retry_cnt
);
696 } else if (sm_params
.curr_state
== DHCP6_DONE
) {
697 net_set_timeout_handler(0, NULL
);
699 /* Duplicate address detection (DAD) should be
700 * performed here before setting net_ip6
701 * (enhancement should be considered)
703 net_copy_ip6(&net_ip6
, &sm_params
.rx_status
.ia_addr_ipv6
);
704 printf("DHCP6 client bound to %pI6c\n", &net_ip6
);
705 /* will load with TFTP6 */
707 } else if (sm_params
.curr_state
== DHCP6_FAIL
) {
708 printf("DHCP6 FAILED, TERMINATING\n");
709 net_set_state(NETLOOP_FAIL
);
711 sm_params
.retry_cnt
++;
714 /* Start or restart DHCP6 */
715 void dhcp6_start(void)
717 memset(&sm_params
, 0, sizeof(struct dhcp6_sm_params
));
719 /* seed the RNG with MAC address */
722 sm_params
.curr_state
= DHCP6_INIT
;
723 dhcp6_state_machine(false, NULL
, 0);