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