dhcpv6.c 23 KB

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