blob: 0d1c600632fb0cb2dc5c9f396e555a8cc344c7d1 [file] [log] [blame]
Sean Edmonde8c43832023-04-11 10:48:46 -07001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright (C) Microsoft Corporation
4 * Author: Sean Edmond <seanedmond@microsoft.com>
5 *
6 */
7
8/* Simple DHCP6 network layer implementation. */
9
10#include <common.h>
11#include <net6.h>
12#include <malloc.h>
13#include <linux/delay.h>
14#include "net_rand.h"
15#include "dhcpv6.h"
16
17#define PORT_DHCP6_S 547 /* DHCP6 server UDP port */
18#define PORT_DHCP6_C 546 /* DHCP6 client UDP port */
19
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
26#define REQ_MAX_RC 10
27#define MAX_WAIT_TIME_MS 60000
28
29/* global variable to track any updates from DHCP6 server */
30int updated_sol_max_rt_ms = SOL_MAX_RT_MS;
31/* state machine parameters/variables */
32struct dhcp6_sm_params sm_params;
33
34static void dhcp6_state_machine(bool timeout, uchar *rx_pkt, unsigned int len);
35
36/* Handle DHCP received packets (set as UDP handler) */
37static void dhcp6_handler(uchar *pkt, unsigned int dest, struct in_addr sip,
38 unsigned int src, unsigned int len)
39{
40 /* return if ports don't match DHCPv6 ports */
41 if (dest != PORT_DHCP6_C || src != PORT_DHCP6_S)
42 return;
43
44 dhcp6_state_machine(false, pkt, len);
45}
46
47/**
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
51 *
52 * Return: The number of bytes written into "*pkt"
53 */
54static int dhcp6_add_option(int option_id, uchar *pkt)
55{
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;
63 int opt_len;
64 long elapsed_time;
65 size_t vci_strlen;
66 int num_oro = 0;
67 int num_client_arch = 0;
68 int num_vc_data = 0;
69 struct dhcp6_option_hdr *dhcp_option = (struct dhcp6_option_hdr *)pkt;
70 uchar *dhcp_option_start = pkt + sizeof(struct dhcp6_option_hdr);
71
72 dhcp_option->option_id = htons(option_id);
73
74 switch (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;
82
83 /* Save DUID for comparison later */
84 memcpy(sm_params.duid, duid_opt, opt_len);
85 break;
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;
92
93 elapsed_time_opt = (struct dhcp6_option_elapsed_time *)dhcp_option_start;
94 elapsed_time_opt->elapsed_time = htons(elapsed_time);
95
96 opt_len = sizeof(struct dhcp6_option_elapsed_time);
97 break;
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);
101
102 opt_len = sizeof(struct dhcp6_option_ia_ta);
103 break;
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
109 */
110 ia_na_opt->t1 = 0;
111 ia_na_opt->t2 = 0;
112
113 opt_len = sizeof(struct dhcp6_option_ia_na);
114 break;
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);
122 }
123
124 opt_len = sizeof(__be16) * num_oro;
125 break;
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);
129
130 opt_len = sizeof(__be16) * num_client_arch;
131 break;
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);
135
136 vci_strlen = strlen(DHCP6_VCI_STRING);
137 vendor_class_opt->vendor_class_data[num_vc_data].vendor_class_len =
138 htons(vci_strlen);
139 memcpy(vendor_class_opt->vendor_class_data[num_vc_data].opaque_data,
140 DHCP6_VCI_STRING, vci_strlen);
141 num_vc_data++;
142
143 opt_len = sizeof(struct dhcp6_option_vendor_class) +
144 sizeof(struct vendor_class_data) * num_vc_data +
145 vci_strlen;
146 break;
147 case DHCP6_OPTION_NII:
148 dhcp_option_start[0] = 1;
149 dhcp_option_start[1] = 0;
150 dhcp_option_start[2] = 0;
151
152 opt_len = 3;
153 break;
154 default:
155 printf("***Warning unknown DHCP6 option %d. Not adding to message\n", option_id);
156 return 0;
157 }
158 dhcp_option->option_len = htons(opt_len);
159
160 return opt_len + sizeof(struct dhcp6_option_hdr);
161}
162
163/**
164 * dhcp6_send_solicit_packet() - Send a SOLICIT packet
165 *
166 * Implements RFC 8415:
167 * - 16.2. Solicit Message
168 * - 18.2.1. Creation and Transmission of Solicit Messages
169 *
170 * Adds DHCP6 header and DHCP6 options. Sends the UDP packet
171 * and sets the UDP handler.
172 */
173static void dhcp6_send_solicit_packet(void)
174{
175 struct in6_addr dhcp_bcast_ip6;
176 int len = 0;
177 uchar *pkt;
178 uchar *dhcp_pkt_start_ptr;
179 struct dhcp6_hdr *dhcp_hdr;
180
181 pkt = net_tx_packet + net_eth_hdr_size() + IP6_HDR_SIZE + UDP_HDR_SIZE;
182 dhcp_pkt_start_ptr = pkt;
183
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);
189
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);
199
200 /* calculate packet length */
201 len = pkt - dhcp_pkt_start_ptr;
202
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);
208}
209
210/**
211 * dhcp6_send_request_packet() - Send a REQUEST packet
212 *
213 * * Implements RFC 8415:
214 * - 16.4. Request Message
215 * - 18.2.2. Creation and Transmission of Request Messages
216 *
217 * Adds DHCP6 header and DHCP6 options. Sends the UDP packet
218 * and sets the UDP handler.
219 */
220static void dhcp6_send_request_packet(void)
221{
222 struct in6_addr dhcp_bcast_ip6;
223 int len = 0;
224 uchar *pkt;
225 uchar *dhcp_pkt_start_ptr;
226 struct dhcp6_hdr *dhcp_hdr;
227
228 pkt = net_tx_packet + net_eth_hdr_size() + IP6_HDR_SIZE + UDP_HDR_SIZE;
229 dhcp_pkt_start_ptr = pkt;
230
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);
236
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;
246 }
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);
251
252 /* calculate packet length */
253 len = pkt - dhcp_pkt_start_ptr;
254
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);
260}
261
262static void dhcp6_parse_ia_options(struct dhcp6_option_hdr *ia_ptr, uchar *ia_option_ptr)
263{
264 struct dhcp6_option_hdr *ia_option_hdr;
265
266 ia_option_hdr = (struct dhcp6_option_hdr *)ia_option_ptr;
267
268 /* Search for options encapsulated in IA_NA/IA_TA (DHCP6_OPTION_IAADDR
269 * or DHCP6_OPTION_STATUS_CODE)
270 */
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");
278 break;
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);
283 break;
284 default:
285 debug("Unknown Option in IA, skipping\n");
286 break;
287 }
288
289 ia_option_ptr += ntohs(((struct dhcp6_option_hdr *)ia_option_ptr)->option_len);
290 }
291}
292
293/**
294 * dhcp6_parse_options() - Parse the DHCP6 options
295 *
296 * @rx_pkt: pointer to beginning of received DHCP6 packet
297 * @len: Total length of the DHCP6 packet
298 *
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"
302 *
303 */
304static void dhcp6_parse_options(uchar *rx_pkt, unsigned int len)
305{
306 uchar *option_ptr;
307 int sol_max_rt_sec, option_len;
308 char *s, *e;
309 struct dhcp6_option_hdr *option_hdr;
310
311 memset(&sm_params.rx_status, 0, sizeof(struct dhcp6_rx_pkt_status));
312
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);
318
319 switch (ntohs(option_hdr->option_id)) {
320 case DHCP6_OPTION_CLIENTID:
321 if (memcmp(option_ptr, sm_params.duid, option_len)
322 != 0) {
323 debug("CLIENT ID DOESN'T MATCH\n");
324 } else {
325 debug("CLIENT ID FOUND and MATCHES\n");
326 sm_params.rx_status.client_id_match = true;
327 }
328 break;
329 case DHCP6_OPTION_SERVERID:
330 sm_params.rx_status.server_id_found = true;
331 sm_params.rx_status.server_uid_ptr = (uchar *)option_hdr;
332 sm_params.rx_status.server_uid_size = option_len +
333 sizeof(struct dhcp6_option_hdr);
334 debug("SERVER ID FOUND\n");
335 break;
336 case DHCP6_OPTION_IA_TA:
337 case DHCP6_OPTION_IA_NA:
338 /* check the IA_ID */
339 if (*((u32 *)option_ptr) != htonl(sm_params.ia_id)) {
340 debug("IA_ID mismatch 0x%08x 0x%08x\n",
341 *((u32 *)option_ptr), htonl(sm_params.ia_id));
342 break;
343 }
344
345 if (ntohs(option_hdr->option_id) == DHCP6_OPTION_IA_NA) {
346 /* skip past IA_ID/T1/T2 */
347 option_ptr += 3 * sizeof(u32);
348 } else if (ntohs(option_hdr->option_id) == DHCP6_OPTION_IA_TA) {
349 /* skip past IA_ID */
350 option_ptr += sizeof(u32);
351 }
352 /* parse the IA_NA/IA_TA encapsulated options */
353 dhcp6_parse_ia_options(option_hdr, option_ptr);
354 break;
355 case DHCP6_OPTION_STATUS_CODE:
356 debug("DHCP6_OPTION_STATUS_CODE FOUND\n");
357 sm_params.rx_status.status_code = ntohs(*((u16 *)option_ptr));
358 debug("DHCP6 top-level status code %d\n", sm_params.rx_status.status_code);
359 debug("DHCP6 status message: %.*s\n", len, option_ptr + 2);
360 break;
361 case DHCP6_OPTION_SOL_MAX_RT:
362 debug("DHCP6_OPTION_SOL_MAX_RT FOUND\n");
363 sol_max_rt_sec = ntohl(*((u32 *)option_ptr));
364
365 /* A DHCP client MUST ignore any SOL_MAX_RT option values that are less
366 * than 60 or more than 86400
367 */
368 if (sol_max_rt_sec >= 60 && sol_max_rt_sec <= 86400) {
369 updated_sol_max_rt_ms = sol_max_rt_sec * 1000;
370 if (sm_params.curr_state == DHCP6_SOLICIT)
371 sm_params.mrt_ms = updated_sol_max_rt_ms;
372 }
373 break;
374 case DHCP6_OPTION_OPT_BOOTFILE_URL:
375 debug("DHCP6_OPTION_OPT_BOOTFILE_URL FOUND\n");
376 copy_filename(net_boot_file_name, option_ptr, option_len + 1);
377 debug("net_boot_file_name: %s\n", net_boot_file_name);
378
379 /* copy server_ip6 (required for PXE) */
380 s = strchr(net_boot_file_name, '[');
381 e = strchr(net_boot_file_name, ']');
382 if (s && e && e > s)
383 string_to_ip6(s + 1, e - s - 1, &net_server_ip6);
384 break;
385 case DHCP6_OPTION_OPT_BOOTFILE_PARAM:
386 if (IS_ENABLED(CONFIG_DHCP6_PXE_DHCP_OPTION)) {
387 debug("DHCP6_OPTION_OPT_BOOTFILE_PARAM FOUND\n");
388
389 if (pxelinux_configfile)
390 free(pxelinux_configfile);
391
392 pxelinux_configfile = (char *)malloc((option_len + 1) *
393 sizeof(char));
394 if (pxelinux_configfile)
395 strlcpy(pxelinux_configfile, option_ptr, option_len + 1);
396 else
397 printf("Error: Failed to allocate pxelinux_configfile\n");
398
399 debug("PXE CONFIG FILE %s\n", pxelinux_configfile);
400 }
401 break;
402 case DHCP6_OPTION_PREFERENCE:
403 debug("DHCP6_OPTION_PREFERENCE FOUND\n");
404 sm_params.rx_status.preference = *option_ptr;
405 break;
406 default:
407 debug("Unknown Option ID: %d, skipping parsing\n",
408 ntohs(option_hdr->option_id));
409 break;
410 }
411 /* Increment to next option header */
412 option_hdr = (struct dhcp6_option_hdr *)(((uchar *)option_hdr) +
413 sizeof(struct dhcp6_option_hdr) + option_len);
414 }
415}
416
417/**
418 * dhcp6_check_advertise_packet() - Perform error checking on an expected
419 * ADVERTISE packet.
420 *
421 * @rx_pkt: pointer to beginning of received DHCP6 packet
422 * @len: Total length of the DHCP6 packet
423 *
424 * Implements RFC 8415:
425 * - 16.3. Advertise Message
426 * - 18.2.10. Receipt of Reply Messages
427 *
428 * Return : 0 : ADVERTISE packet was received with no errors.
429 * State machine can progress
430 * 1 : - packet received is not an ADVERTISE packet
431 * - there were errors in the packet received,
432 * - this is the first SOLICIT packet, but
433 * received preference is not 255, so we have
434 * to wait for more server responses.
435 */
436static int dhcp6_check_advertise_packet(uchar *rx_pkt, unsigned int len)
437{
438 u16 rx_uid_size;
439 struct dhcp6_hdr *dhcp6_hdr = (struct dhcp6_hdr *)rx_pkt;
440
441 /* Ignore message if msg-type != advertise */
442 if (dhcp6_hdr->msg_type != DHCP6_MSG_ADVERTISE)
443 return 1;
444 /* Ignore message if transaction ID doesn't match */
445 if (dhcp6_hdr->trans_id != htons(sm_params.trans_id))
446 return 1;
447
448 dhcp6_parse_options(rx_pkt, len);
449
450 /* Ignore advertise if any of these conditions met */
451 if (!sm_params.rx_status.server_id_found ||
452 !sm_params.rx_status.client_id_match ||
453 sm_params.rx_status.status_code != DHCP6_SUCCESS) {
454 return 1;
455 }
456
457 if (sm_params.rx_status.server_id_found) {
458 /* if no server UID has been received yet, or if the server UID
459 * received has a higher preference value than the currently saved
460 * server UID, save the new server UID and preference
461 */
462 if (!sm_params.server_uid.uid_ptr ||
463 (sm_params.server_uid.uid_ptr &&
464 sm_params.server_uid.preference < sm_params.rx_status.preference)) {
465 rx_uid_size = sm_params.rx_status.server_uid_size;
466 if (sm_params.server_uid.uid_ptr)
467 free(sm_params.server_uid.uid_ptr);
468 sm_params.server_uid.uid_ptr = malloc(rx_uid_size * sizeof(uchar));
469 if (sm_params.server_uid.uid_ptr)
470 memcpy(sm_params.server_uid.uid_ptr,
471 sm_params.rx_status.server_uid_ptr, rx_uid_size);
472
473 sm_params.server_uid.uid_size = rx_uid_size;
474 sm_params.server_uid.preference = sm_params.rx_status.preference;
475 }
476
477 /* If the first SOLICIT and preference code is 255, use right away.
478 * Otherwise, wait for the first SOLICIT period for more
479 * DHCP6 servers to respond.
480 */
481 if (sm_params.retry_cnt == 1 &&
482 sm_params.server_uid.preference != 255) {
483 debug("valid ADVERTISE, waiting for first SOLICIT period\n");
484 return 1;
485 }
486 }
487
488 return 0;
489}
490
491/**
492 * dhcp6_check_reply_packet() - Perform error checking on an expected
493 * REPLY packet.
494 *
495 * @rx_pkt: pointer to beginning of received DHCP6 packet
496 * @len: Total length of the DHCP6 packet
497 *
498 * Implements RFC 8415:
499 * - 16.10. Reply Message
500 * - 18.2.10. Receipt of Reply Messages
501 *
502 * Return : 0 - REPLY packet was received with no errors
503 * 1 - packet received is not an REPLY packet,
504 * or there were errors in the packet received
505 */
506static int dhcp6_check_reply_packet(uchar *rx_pkt, unsigned int len)
507{
508 struct dhcp6_hdr *dhcp6_hdr = (struct dhcp6_hdr *)rx_pkt;
509
510 /* Ignore message if msg-type != reply */
511 if (dhcp6_hdr->msg_type != DHCP6_MSG_REPLY)
512 return 1;
513 /* check that transaction ID matches */
514 if (dhcp6_hdr->trans_id != htons(sm_params.trans_id))
515 return 1;
516
517 dhcp6_parse_options(rx_pkt, len);
518
519 /* if no addresses found, restart DHCP */
520 if (!sm_params.rx_status.ia_addr_found ||
521 sm_params.rx_status.ia_status_code == DHCP6_NO_ADDRS_AVAIL ||
522 sm_params.rx_status.status_code == DHCP6_NOT_ON_LINK) {
523 /* restart DHCP */
524 debug("No address found in reply. Restarting DHCP\n");
525 dhcp6_start();
526 }
527
528 /* ignore reply if any of these conditions met */
529 if (!sm_params.rx_status.server_id_found ||
530 !sm_params.rx_status.client_id_match ||
531 sm_params.rx_status.status_code == DHCP6_UNSPEC_FAIL) {
532 return 1;
533 }
534
535 return 0;
536}
537
538/* Timeout for DHCP6 SOLICIT/REQUEST */
539static void dhcp6_timeout_handler(void)
540{
541 /* call state machine with the timeout flag */
542 dhcp6_state_machine(true, NULL, 0);
543}
544
545/**
546 * dhcp6_state_machine() - DHCP6 state machine
547 *
548 * @timeout: TRUE : timeout waiting for response from
549 * DHCP6 server
550 * FALSE : init or received response from DHCP6 server
551 * @rx_pkt: Pointer to the beginning of received DHCP6 packet.
552 * Will be NULL if called as part of init
553 * or timeout==TRUE
554 * @len: Total length of the DHCP6 packet if rx_pkt != NULL
555 *
556 * Implements RFC 8415:
557 * - 5.2. Client/Server Exchanges Involving Four Messages
558 * - 15. Reliability of Client-Initiated Message Exchanges
559 *
560 * Handles:
561 * - transmission of SOLICIT and REQUEST packets
562 * - retransmission of SOLICIT and REQUEST packets if no
563 * response is received within the timeout window
564 * - checking received ADVERTISE and REPLY packets to
565 * assess if the DHCP state machine can progress
566 */
567static void dhcp6_state_machine(bool timeout, uchar *rx_pkt, unsigned int len)
568{
569 int rand_minus_plus_100;
570
571 switch (sm_params.curr_state) {
572 case DHCP6_INIT:
573 sm_params.next_state = DHCP6_SOLICIT;
574 break;
575 case DHCP6_SOLICIT:
576 if (!timeout) {
577 /* check the rx packet and determine if we can transition to next
578 * state.
579 */
580 if (dhcp6_check_advertise_packet(rx_pkt, len))
581 return;
582
583 debug("ADVERTISE good, transition to REQUEST\n");
584 sm_params.next_state = DHCP6_REQUEST;
585 } else if (sm_params.retry_cnt == 1) {
586 /* If a server UID was received in the first SOLICIT period
587 * transition to REQUEST
588 */
589 if (sm_params.server_uid.uid_ptr)
590 sm_params.next_state = DHCP6_REQUEST;
591 }
592 break;
593 case DHCP6_REQUEST:
594 if (!timeout) {
595 /* check the rx packet and determine if we can transition to next state */
596 if (dhcp6_check_reply_packet(rx_pkt, len))
597 return;
598
599 debug("REPLY good, transition to DONE\n");
600 sm_params.next_state = DHCP6_DONE;
601 }
602 break;
603 case DHCP6_DONE:
604 case DHCP6_FAIL:
605 /* Shouldn't get here, as state machine should exit
606 * immediately when DHCP6_DONE or DHCP6_FAIL is entered.
607 * Proceed anyway to proceed DONE/FAIL actions
608 */
609 debug("Unexpected DHCP6 state : %d\n", sm_params.curr_state);
610 break;
611 }
612 /* re-seed the RNG */
613 srand(get_ticks() + rand());
614
615 /* handle state machine entry conditions */
616 if (sm_params.curr_state != sm_params.next_state) {
617 sm_params.retry_cnt = 0;
618
619 if (sm_params.next_state == DHCP6_SOLICIT) {
620 /* delay a random ammount (special for SOLICIT) */
621 udelay((rand() % SOL_MAX_DELAY_MS) * 1000);
622 /* init timestamp variables after SOLICIT delay */
623 sm_params.dhcp6_start_ms = get_timer(0);
624 sm_params.dhcp6_retry_start_ms = sm_params.dhcp6_start_ms;
625 sm_params.dhcp6_retry_ms = sm_params.dhcp6_start_ms;
626 /* init transaction and ia_id */
627 sm_params.trans_id = rand() & 0xFFFFFF;
628 sm_params.ia_id = rand();
629 /* initialize retransmission parameters */
630 sm_params.irt_ms = SOL_TIMEOUT_MS;
631 sm_params.mrt_ms = updated_sol_max_rt_ms;
632 /* RFCs default MRC is be 0 (try infinitely)
633 * give up after CONFIG_NET_RETRY_COUNT number of tries (same as DHCPv4)
634 */
635 sm_params.mrc = CONFIG_NET_RETRY_COUNT;
636 sm_params.mrd_ms = 0;
637
638 } else if (sm_params.next_state == DHCP6_REQUEST) {
639 /* init timestamp variables */
640 sm_params.dhcp6_retry_start_ms = get_timer(0);
641 sm_params.dhcp6_retry_ms = sm_params.dhcp6_start_ms;
642 /* initialize retransmission parameters */
643 sm_params.irt_ms = REQ_TIMEOUT_MS;
644 sm_params.mrt_ms = REQ_MAX_RT_MS;
645 sm_params.mrc = REQ_MAX_RC;
646 sm_params.mrd_ms = 0;
647 }
648 }
649
650 if (timeout)
651 sm_params.dhcp6_retry_ms = get_timer(0);
652
653 /* Check if MRC or MRD have been passed */
654 if ((sm_params.mrc != 0 &&
655 sm_params.retry_cnt >= sm_params.mrc) ||
656 (sm_params.mrd_ms != 0 &&
657 ((sm_params.dhcp6_retry_ms - sm_params.dhcp6_retry_start_ms) >= sm_params.mrd_ms))) {
658 sm_params.next_state = DHCP6_FAIL;
659 }
660
661 /* calculate retransmission timeout (RT) */
662 rand_minus_plus_100 = ((rand() % 200) - 100);
663 if (sm_params.retry_cnt == 0) {
664 sm_params.rt_ms = sm_params.irt_ms +
665 ((sm_params.irt_ms * rand_minus_plus_100) / 1000);
666 } else {
667 sm_params.rt_ms = (2 * sm_params.rt_prev_ms) +
668 ((sm_params.rt_prev_ms * rand_minus_plus_100) / 1000);
669 }
670
671 if (sm_params.rt_ms > sm_params.mrt_ms) {
672 sm_params.rt_ms = sm_params.mrt_ms +
673 ((sm_params.mrt_ms * rand_minus_plus_100) / 1000);
674 }
675
676 sm_params.rt_prev_ms = sm_params.rt_ms;
677
678 net_set_timeout_handler(sm_params.rt_ms, dhcp6_timeout_handler);
679
680 /* send transmit/retransmit message or fail */
681 sm_params.curr_state = sm_params.next_state;
682
683 if (sm_params.curr_state == DHCP6_SOLICIT) {
684 /* send solicit packet */
685 dhcp6_send_solicit_packet();
686 printf("DHCP6 SOLICIT %d\n", sm_params.retry_cnt);
687 } else if (sm_params.curr_state == DHCP6_REQUEST) {
688 /* send request packet */
689 dhcp6_send_request_packet();
690 printf("DHCP6 REQUEST %d\n", sm_params.retry_cnt);
691 } else if (sm_params.curr_state == DHCP6_DONE) {
692 net_set_timeout_handler(0, NULL);
693
694 /* Duplicate address detection (DAD) should be
695 * performed here before setting net_ip6
696 * (enhancement should be considered)
697 */
698 net_copy_ip6(&net_ip6, &sm_params.rx_status.ia_addr_ipv6);
699 printf("DHCP6 client bound to %pI6c\n", &net_ip6);
700 /* will load with TFTP6 */
701 net_auto_load();
702 } else if (sm_params.curr_state == DHCP6_FAIL) {
703 printf("DHCP6 FAILED, TERMINATING\n");
704 net_set_state(NETLOOP_FAIL);
705 }
706 sm_params.retry_cnt++;
707}
708
709/* Start or restart DHCP6 */
710void dhcp6_start(void)
711{
712 memset(&sm_params, 0, sizeof(struct dhcp6_sm_params));
713
714 /* seed the RNG with MAC address */
715 srand_mac();
716
717 sm_params.curr_state = DHCP6_INIT;
718 dhcp6_state_machine(false, NULL, 0);
719}