dhcpv6.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /* SPDX-License-Identifier: GPL-2.0+ */
  2. /*
  3. * Copyright (C) Microsoft Corporation
  4. * Author: Sean Edmond <seanedmond@microsoft.com>
  5. *
  6. */
  7. #ifndef __DHCP6_H__
  8. #define __DHCP6_H__
  9. /* Message types */
  10. #define DHCP6_MSG_SOLICIT 1
  11. #define DHCP6_MSG_ADVERTISE 2
  12. #define DHCP6_MSG_REQUEST 3
  13. #define DHCP6_MSG_REPLY 7
  14. /* Option Codes */
  15. #define DHCP6_OPTION_CLIENTID 1
  16. #define DHCP6_OPTION_SERVERID 2
  17. #define DHCP6_OPTION_IA_NA 3
  18. #define DHCP6_OPTION_IA_TA 4
  19. #define DHCP6_OPTION_IAADDR 5
  20. #define DHCP6_OPTION_ORO 6
  21. #define DHCP6_OPTION_PREFERENCE 7
  22. #define DHCP6_OPTION_ELAPSED_TIME 8
  23. #define DHCP6_OPTION_STATUS_CODE 13
  24. #define DHCP6_OPTION_OPT_BOOTFILE_URL 59
  25. #define DHCP6_OPTION_OPT_BOOTFILE_PARAM 60
  26. #define DHCP6_OPTION_SOL_MAX_RT 82
  27. #define DHCP6_OPTION_CLIENT_ARCH_TYPE 61
  28. #define DHCP6_OPTION_VENDOR_CLASS 16
  29. #define DHCP6_OPTION_NII 62
  30. /* DUID */
  31. #define DUID_TYPE_LL 3
  32. #define DUID_HW_TYPE_ENET 1
  33. #define DUID_LL_SIZE (sizeof(struct dhcp6_option_duid_ll) + ETH_ALEN)
  34. #define DUID_MAX_SIZE DUID_LL_SIZE /* only supports DUID-LL currently */
  35. /* vendor-class-data to send in vendor clas option */
  36. #define DHCP6_VCI_STRING "U-Boot"
  37. #define DHCP6_MULTICAST_ADDR "ff02::1:2" /* DHCP multicast address */
  38. /* DHCP6 States supported */
  39. enum dhcp6_state {
  40. DHCP6_INIT,
  41. DHCP6_SOLICIT,
  42. DHCP6_REQUEST,
  43. DHCP6_DONE,
  44. DHCP6_FAIL,
  45. };
  46. /* DHCP6 Status codes */
  47. enum dhcp6_status {
  48. DHCP6_SUCCESS = 0,
  49. DHCP6_UNSPEC_FAIL = 1,
  50. DHCP6_NO_ADDRS_AVAIL = 2,
  51. DHCP6_NO_BINDING = 3,
  52. DHCP6_NOT_ON_LINK = 4,
  53. DHCP6_USE_MULTICAST = 5,
  54. DHCP6_NO_PREFIX_AVAIL = 6,
  55. };
  56. /* DHCP6 message header format */
  57. struct dhcp6_hdr {
  58. unsigned int msg_type : 8; /* message type */
  59. unsigned int trans_id : 24; /* transaction ID */
  60. } __packed;
  61. /* DHCP6 option header format */
  62. struct dhcp6_option_hdr {
  63. __be16 option_id; /* option id */
  64. __be16 option_len; /* Option length */
  65. u8 option_data[0]; /* Option data */
  66. } __packed;
  67. /* DHCP6_OPTION_CLIENTID option (DUID-LL) */
  68. struct dhcp6_option_duid_ll {
  69. __be16 duid_type;
  70. __be16 hw_type;
  71. u8 ll_addr[0];
  72. } __packed;
  73. /* DHCP6_OPTION_ELAPSED_TIME option */
  74. struct dhcp6_option_elapsed_time {
  75. __be16 elapsed_time;
  76. } __packed;
  77. /* DHCP6_OPTION_IA_TA option */
  78. struct dhcp6_option_ia_ta {
  79. __be32 iaid;
  80. u8 ia_ta_options[0];
  81. } __packed;
  82. /* DHCP6_OPTION_IA_NA option */
  83. struct dhcp6_option_ia_na {
  84. __be32 iaid;
  85. __be32 t1;
  86. __be32 t2;
  87. u8 ia_na_options[0];
  88. } __packed;
  89. /* OPTION_ORO option */
  90. struct dhcp6_option_oro {
  91. __be16 req_option_code[0];
  92. } __packed;
  93. /* DHCP6_OPTION_CLIENT_ARCH_TYPE option */
  94. struct dhcp6_option_client_arch {
  95. __be16 arch_type[0];
  96. } __packed;
  97. /* vendor-class-data inside OPTION_VENDOR_CLASS option */
  98. struct vendor_class_data {
  99. __be16 vendor_class_len;
  100. u8 opaque_data[0];
  101. } __packed;
  102. /* DHCP6_OPTION_VENDOR_CLASS option */
  103. struct dhcp6_option_vendor_class {
  104. __be32 enterprise_number;
  105. struct vendor_class_data vendor_class_data[0];
  106. } __packed;
  107. /**
  108. * struct dhcp6_rx_pkt_status - Structure that holds status
  109. * from a received message
  110. * @client_id_match: Client ID was found and matches DUID sent
  111. * @server_id_found: Server ID was found in the message
  112. * @server_uid_ptr: Pointer to received server ID
  113. * @server_uid_size: Size of received server ID
  114. * @ia_addr_found: IA addr option was found in received message
  115. * @ia_addr_ipv6: The IPv6 address received in IA
  116. * @ia_status_code: Status code received in the IA
  117. * @status_code: Top-level status code received
  118. * @preference: Preference code received
  119. */
  120. struct dhcp6_rx_pkt_status {
  121. bool client_id_match;
  122. bool server_id_found;
  123. uchar *server_uid_ptr;
  124. u16 server_uid_size;
  125. bool ia_addr_found;
  126. struct in6_addr ia_addr_ipv6;
  127. enum dhcp6_status ia_status_code;
  128. enum dhcp6_status status_code;
  129. u8 preference;
  130. };
  131. /**
  132. * struct dhcp6_server_uid - Structure that holds the server UID
  133. * received from an ADVERTISE and saved
  134. * given the server selection criteria.
  135. * @uid_ptr: Dynamically allocated and copied server UID
  136. * @uid_size: Size of the server UID in uid_ptr (in bytes)
  137. * @preference: Preference code associated with this server UID
  138. */
  139. struct dhcp6_server_uid {
  140. uchar *uid_ptr;
  141. u16 uid_size;
  142. u8 preference;
  143. };
  144. /**
  145. * struct dhcp6_sm_params - Structure that holds DHCP6
  146. * state machine parameters
  147. * @curr_state: current DHCP6 state
  148. * @next_state: next DHCP6 state
  149. * @dhcp6_start_ms: timestamp DHCP6 start
  150. * @dhcp6_retry_start_ms: timestamp of current TX message start
  151. * @dhcp6_retry_ms: timestamp of last retransmission
  152. * @retry_cnt: retry count
  153. * @trans_id: transaction ID
  154. * @ia_id: transmitted IA ID
  155. * @irt_ms: Initial retransmission time (in ms)
  156. * @mrt_ms: Maximum retransmission time (in ms)
  157. * @mrc: Maximum retransmission count
  158. * @mrd_ms: Maximum retransmission duration (in ms)
  159. * @rt_ms: retransmission timeout (is ms)
  160. * @rt_prev_ms: previous retransmission timeout
  161. * @rx_status: Status from received message
  162. * @server_uid: Saved Server UID for selected server
  163. * @duid: pointer to transmitted Client DUID
  164. */
  165. struct dhcp6_sm_params {
  166. enum dhcp6_state curr_state;
  167. enum dhcp6_state next_state;
  168. ulong dhcp6_start_ms;
  169. ulong dhcp6_retry_start_ms;
  170. ulong dhcp6_retry_ms;
  171. u32 retry_cnt;
  172. u32 trans_id;
  173. u32 ia_id;
  174. int irt_ms;
  175. int mrt_ms;
  176. int mrc;
  177. int mrd_ms;
  178. int rt_ms;
  179. int rt_prev_ms;
  180. struct dhcp6_rx_pkt_status rx_status;
  181. struct dhcp6_server_uid server_uid;
  182. char duid[DUID_MAX_SIZE];
  183. };
  184. /* Starts a DHCPv6 4-message exchange as a DHCPv6 client. On successful exchange,
  185. * the DHCPv6 state machine will transition from internal states:
  186. * DHCP6_INIT->DHCP6_SOLICIT->DHCP6_REQUEST->DHCP6_DONE
  187. *
  188. * Transmitted SOLICIT and REQUEST packets will set/request the minimum required
  189. * DHCPv6 options to PXE boot.
  190. *
  191. * After a successful exchange, the DHCPv6 assigned address will be set in net_ip6
  192. *
  193. * Additionally, the following will be set after receiving these options:
  194. * DHCP6_OPTION_OPT_BOOTFILE_URL (option 59) -> net_server_ip6, net_boot_file_name
  195. * DHCP6_OPTION_OPT_BOOTFILE_PARAM (option 60) - > pxelinux_configfile
  196. *
  197. * Illustration of a 4-message exchange with 2 servers (copied from
  198. * https://www.rfc-editor.org/rfc/rfc8415):
  199. *
  200. * Server Server
  201. * (not selected) Client (selected)
  202. *
  203. * v v v
  204. * | | |
  205. * | Begins initialization |
  206. * | | |
  207. * start of | _____________/|\_____________ |
  208. * 4-message |/ Solicit | Solicit \|
  209. * exchange | | |
  210. * Determines | Determines
  211. * configuration | configuration
  212. * | | |
  213. * |\ | ____________/|
  214. * | \________ | /Advertise |
  215. * | Advertise\ |/ |
  216. * | \ | |
  217. * | Collects Advertises |
  218. * | \ | |
  219. * | Selects configuration |
  220. * | | |
  221. * | _____________/|\_____________ |
  222. * |/ Request | Request \|
  223. * | | |
  224. * | | Commits configuration
  225. * | | |
  226. * end of | | _____________/|
  227. * 4-message | |/ Reply |
  228. * exchange | | |
  229. * | Initialization complete |
  230. * | | |
  231. */
  232. void dhcp6_start(void);
  233. #endif /* __DHCP6_H__ */