Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017
  1. /*
  2. * Copyright (C) 2013 Michael Brown <mbrown@fensystems.co.uk>.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation; either version 2 of the
  7. * License, or any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301, USA.
  18. *
  19. * You can also choose to distribute this program under the terms of
  20. * the Unmodified Binary Distribution Licence (as given in the file
  21. * COPYING.UBDL), provided that you have satisfied its requirements.
  22. */
  23. FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
  24. #include <stdlib.h>
  25. #include <stdio.h>
  26. #include <string.h>
  27. #include <errno.h>
  28. #include <byteswap.h>
  29. #include <ipxe/interface.h>
  30. #include <ipxe/xfer.h>
  31. #include <ipxe/iobuf.h>
  32. #include <ipxe/open.h>
  33. #include <ipxe/netdevice.h>
  34. #include <ipxe/settings.h>
  35. #include <ipxe/retry.h>
  36. #include <ipxe/timer.h>
  37. #include <ipxe/in.h>
  38. #include <ipxe/crc32.h>
  39. #include <ipxe/errortab.h>
  40. #include <ipxe/ipv6.h>
  41. #include <ipxe/dhcp_arch.h>
  42. #include <ipxe/dhcpv6.h>
  43. /** @file
  44. *
  45. * Dynamic Host Configuration Protocol for IPv6
  46. *
  47. */
  48. /* Disambiguate the various error causes */
  49. #define EPROTO_UNSPECFAIL __einfo_error ( EINFO_EPROTO_UNSPECFAIL )
  50. #define EINFO_EPROTO_UNSPECFAIL \
  51. __einfo_uniqify ( EINFO_EPROTO, 1, "Unspecified server failure" )
  52. #define EPROTO_NOADDRSAVAIL __einfo_error ( EINFO_EPROTO_NOADDRSAVAIL )
  53. #define EINFO_EPROTO_NOADDRSAVAIL \
  54. __einfo_uniqify ( EINFO_EPROTO, 2, "No addresses available" )
  55. #define EPROTO_NOBINDING __einfo_error ( EINFO_EPROTO_NOBINDING )
  56. #define EINFO_EPROTO_NOBINDING \
  57. __einfo_uniqify ( EINFO_EPROTO, 3, "Client record unavailable" )
  58. #define EPROTO_NOTONLINK __einfo_error ( EINFO_EPROTO_NOTONLINK )
  59. #define EINFO_EPROTO_NOTONLINK \
  60. __einfo_uniqify ( EINFO_EPROTO, 4, "Prefix not on link" )
  61. #define EPROTO_USEMULTICAST __einfo_error ( EINFO_EPROTO_USEMULTICAST )
  62. #define EINFO_EPROTO_USEMULTICAST \
  63. __einfo_uniqify ( EINFO_EPROTO, 5, "Use multicast address" )
  64. #define EPROTO_STATUS( status ) \
  65. EUNIQ ( EINFO_EPROTO, ( (status) & 0x0f ), EPROTO_UNSPECFAIL, \
  66. EPROTO_NOADDRSAVAIL, EPROTO_NOBINDING, \
  67. EPROTO_NOTONLINK, EPROTO_USEMULTICAST )
  68. /** Human-readable error messages */
  69. struct errortab dhcpv6_errors[] __errortab = {
  70. __einfo_errortab ( EINFO_EPROTO_NOADDRSAVAIL ),
  71. };
  72. /****************************************************************************
  73. *
  74. * DHCPv6 option lists
  75. *
  76. */
  77. /** A DHCPv6 option list */
  78. struct dhcpv6_option_list {
  79. /** Data buffer */
  80. const void *data;
  81. /** Length of data buffer */
  82. size_t len;
  83. };
  84. /**
  85. * Find DHCPv6 option
  86. *
  87. * @v options DHCPv6 option list
  88. * @v code Option code
  89. * @ret option DHCPv6 option, or NULL if not found
  90. */
  91. static const union dhcpv6_any_option *
  92. dhcpv6_option ( struct dhcpv6_option_list *options, unsigned int code ) {
  93. const union dhcpv6_any_option *option = options->data;
  94. size_t remaining = options->len;
  95. size_t data_len;
  96. /* Scan through list of options */
  97. while ( remaining >= sizeof ( option->header ) ) {
  98. /* Calculate and validate option length */
  99. remaining -= sizeof ( option->header );
  100. data_len = ntohs ( option->header.len );
  101. if ( data_len > remaining ) {
  102. /* Malformed option list */
  103. return NULL;
  104. }
  105. /* Return if we have found the specified option */
  106. if ( option->header.code == htons ( code ) )
  107. return option;
  108. /* Otherwise, move to the next option */
  109. option = ( ( ( void * ) option->header.data ) + data_len );
  110. remaining -= data_len;
  111. }
  112. return NULL;
  113. }
  114. /**
  115. * Check DHCPv6 client or server identifier
  116. *
  117. * @v options DHCPv6 option list
  118. * @v code Option code
  119. * @v expected Expected value
  120. * @v len Length of expected value
  121. * @ret rc Return status code
  122. */
  123. static int dhcpv6_check_duid ( struct dhcpv6_option_list *options,
  124. unsigned int code, const void *expected,
  125. size_t len ) {
  126. const union dhcpv6_any_option *option;
  127. const struct dhcpv6_duid_option *duid;
  128. /* Find option */
  129. option = dhcpv6_option ( options, code );
  130. if ( ! option )
  131. return -ENOENT;
  132. duid = &option->duid;
  133. /* Check option length */
  134. if ( ntohs ( duid->header.len ) != len )
  135. return -EINVAL;
  136. /* Compare option value */
  137. if ( memcmp ( duid->duid, expected, len ) != 0 )
  138. return -EINVAL;
  139. return 0;
  140. }
  141. /**
  142. * Get DHCPv6 status code
  143. *
  144. * @v options DHCPv6 option list
  145. * @ret rc Return status code
  146. */
  147. static int dhcpv6_status_code ( struct dhcpv6_option_list *options ) {
  148. const union dhcpv6_any_option *option;
  149. const struct dhcpv6_status_code_option *status_code;
  150. unsigned int status;
  151. /* Find status code option, if present */
  152. option = dhcpv6_option ( options, DHCPV6_STATUS_CODE );
  153. if ( ! option ) {
  154. /* Omitted status code should be treated as "success" */
  155. return 0;
  156. }
  157. status_code = &option->status_code;
  158. /* Sanity check */
  159. if ( ntohs ( status_code->header.len ) <
  160. ( sizeof ( *status_code ) - sizeof ( status_code->header ) ) ) {
  161. return -EINVAL;
  162. }
  163. /* Calculate iPXE error code from DHCPv6 status code */
  164. status = ntohs ( status_code->status );
  165. return ( status ? -EPROTO_STATUS ( status ) : 0 );
  166. }
  167. /**
  168. * Get DHCPv6 identity association address
  169. *
  170. * @v options DHCPv6 option list
  171. * @v iaid Identity association ID
  172. * @v address IPv6 address to fill in
  173. * @ret rc Return status code
  174. */
  175. static int dhcpv6_iaaddr ( struct dhcpv6_option_list *options, uint32_t iaid,
  176. struct in6_addr *address ) {
  177. const union dhcpv6_any_option *option;
  178. const struct dhcpv6_ia_na_option *ia_na;
  179. const struct dhcpv6_iaaddr_option *iaaddr;
  180. struct dhcpv6_option_list suboptions;
  181. size_t len;
  182. int rc;
  183. /* Find identity association option, if present */
  184. option = dhcpv6_option ( options, DHCPV6_IA_NA );
  185. if ( ! option )
  186. return -ENOENT;
  187. ia_na = &option->ia_na;
  188. /* Sanity check */
  189. len = ntohs ( ia_na->header.len );
  190. if ( len < ( sizeof ( *ia_na ) - sizeof ( ia_na->header ) ) )
  191. return -EINVAL;
  192. /* Check identity association ID */
  193. if ( ia_na->iaid != htonl ( iaid ) )
  194. return -EINVAL;
  195. /* Construct IA_NA sub-options list */
  196. suboptions.data = ia_na->options;
  197. suboptions.len = ( len + sizeof ( ia_na->header ) -
  198. offsetof ( typeof ( *ia_na ), options ) );
  199. /* Check IA_NA status code */
  200. if ( ( rc = dhcpv6_status_code ( &suboptions ) ) != 0 )
  201. return rc;
  202. /* Find identity association address, if present */
  203. option = dhcpv6_option ( &suboptions, DHCPV6_IAADDR );
  204. if ( ! option )
  205. return -ENOENT;
  206. iaaddr = &option->iaaddr;
  207. /* Sanity check */
  208. len = ntohs ( iaaddr->header.len );
  209. if ( len < ( sizeof ( *iaaddr ) - sizeof ( iaaddr->header ) ) )
  210. return -EINVAL;
  211. /* Construct IAADDR sub-options list */
  212. suboptions.data = iaaddr->options;
  213. suboptions.len = ( len + sizeof ( iaaddr->header ) -
  214. offsetof ( typeof ( *iaaddr ), options ) );
  215. /* Check IAADDR status code */
  216. if ( ( rc = dhcpv6_status_code ( &suboptions ) ) != 0 )
  217. return rc;
  218. /* Extract IPv6 address */
  219. memcpy ( address, &iaaddr->address, sizeof ( *address ) );
  220. return 0;
  221. }
  222. /****************************************************************************
  223. *
  224. * DHCPv6 settings blocks
  225. *
  226. */
  227. /** A DHCPv6 settings block */
  228. struct dhcpv6_settings {
  229. /** Reference count */
  230. struct refcnt refcnt;
  231. /** Settings block */
  232. struct settings settings;
  233. /** Leased address */
  234. struct in6_addr lease;
  235. /** Option list */
  236. struct dhcpv6_option_list options;
  237. };
  238. /**
  239. * Check applicability of DHCPv6 setting
  240. *
  241. * @v settings Settings block
  242. * @v setting Setting
  243. * @ret applies Setting applies within this settings block
  244. */
  245. static int dhcpv6_applies ( struct settings *settings __unused,
  246. const struct setting *setting ) {
  247. return ( ( setting->scope == &dhcpv6_scope ) ||
  248. ( setting_cmp ( setting, &ip6_setting ) == 0 ) );
  249. }
  250. /**
  251. * Fetch value of DHCPv6 leased address
  252. *
  253. * @v dhcpset DHCPv6 settings
  254. * @v data Buffer to fill with setting data
  255. * @v len Length of buffer
  256. * @ret len Length of setting data, or negative error
  257. */
  258. static int dhcpv6_fetch_lease ( struct dhcpv6_settings *dhcpv6set,
  259. void *data, size_t len ) {
  260. struct in6_addr *lease = &dhcpv6set->lease;
  261. /* Do nothing unless a leased address exists */
  262. if ( IN6_IS_ADDR_UNSPECIFIED ( lease ) )
  263. return -ENOENT;
  264. /* Copy leased address */
  265. if ( len > sizeof ( *lease ) )
  266. len = sizeof ( *lease );
  267. memcpy ( data, lease, len );
  268. return sizeof ( *lease );
  269. }
  270. /**
  271. * Fetch value of DHCPv6 setting
  272. *
  273. * @v settings Settings block
  274. * @v setting Setting to fetch
  275. * @v data Buffer to fill with setting data
  276. * @v len Length of buffer
  277. * @ret len Length of setting data, or negative error
  278. */
  279. static int dhcpv6_fetch ( struct settings *settings,
  280. struct setting *setting,
  281. void *data, size_t len ) {
  282. struct dhcpv6_settings *dhcpv6set =
  283. container_of ( settings, struct dhcpv6_settings, settings );
  284. const union dhcpv6_any_option *option;
  285. size_t option_len;
  286. /* Handle leased address */
  287. if ( setting_cmp ( setting, &ip6_setting ) == 0 )
  288. return dhcpv6_fetch_lease ( dhcpv6set, data, len );
  289. /* Find option */
  290. option = dhcpv6_option ( &dhcpv6set->options, setting->tag );
  291. if ( ! option )
  292. return -ENOENT;
  293. /* Copy option to data buffer */
  294. option_len = ntohs ( option->header.len );
  295. if ( len > option_len )
  296. len = option_len;
  297. memcpy ( data, option->header.data, len );
  298. return option_len;
  299. }
  300. /** DHCPv6 settings operations */
  301. static struct settings_operations dhcpv6_settings_operations = {
  302. .applies = dhcpv6_applies,
  303. .fetch = dhcpv6_fetch,
  304. };
  305. /**
  306. * Register DHCPv6 options as network device settings
  307. *
  308. * @v lease DHCPv6 leased address
  309. * @v options DHCPv6 option list
  310. * @v parent Parent settings block
  311. * @ret rc Return status code
  312. */
  313. static int dhcpv6_register ( struct in6_addr *lease,
  314. struct dhcpv6_option_list *options,
  315. struct settings *parent ) {
  316. struct dhcpv6_settings *dhcpv6set;
  317. void *data;
  318. size_t len;
  319. int rc;
  320. /* Allocate and initialise structure */
  321. dhcpv6set = zalloc ( sizeof ( *dhcpv6set ) + options->len );
  322. if ( ! dhcpv6set ) {
  323. rc = -ENOMEM;
  324. goto err_alloc;
  325. }
  326. ref_init ( &dhcpv6set->refcnt, NULL );
  327. settings_init ( &dhcpv6set->settings, &dhcpv6_settings_operations,
  328. &dhcpv6set->refcnt, &dhcpv6_scope );
  329. dhcpv6set->settings.order = IPV6_ORDER_DHCPV6;
  330. data = ( ( ( void * ) dhcpv6set ) + sizeof ( *dhcpv6set ) );
  331. len = options->len;
  332. memcpy ( data, options->data, len );
  333. dhcpv6set->options.data = data;
  334. dhcpv6set->options.len = len;
  335. memcpy ( &dhcpv6set->lease, lease, sizeof ( dhcpv6set->lease ) );
  336. /* Register settings */
  337. if ( ( rc = register_settings ( &dhcpv6set->settings, parent,
  338. DHCPV6_SETTINGS_NAME ) ) != 0 )
  339. goto err_register;
  340. err_register:
  341. ref_put ( &dhcpv6set->refcnt );
  342. err_alloc:
  343. return rc;
  344. }
  345. /****************************************************************************
  346. *
  347. * DHCPv6 protocol
  348. *
  349. */
  350. /** Raw option data for options common to all DHCPv6 requests */
  351. static uint8_t dhcpv6_request_options_data[] = {
  352. DHCPV6_CODE ( DHCPV6_OPTION_REQUEST ),
  353. DHCPV6_OPTION ( DHCPV6_CODE ( DHCPV6_DNS_SERVERS ),
  354. DHCPV6_CODE ( DHCPV6_DOMAIN_LIST ),
  355. DHCPV6_CODE ( DHCPV6_BOOTFILE_URL ),
  356. DHCPV6_CODE ( DHCPV6_BOOTFILE_PARAM ) ),
  357. DHCPV6_CODE ( DHCPV6_VENDOR_CLASS ),
  358. DHCPV6_OPTION ( DHCPV6_DWORD_VALUE ( DHCPV6_VENDOR_CLASS_PXE ),
  359. DHCPV6_STRING (
  360. DHCP_VENDOR_PXECLIENT ( DHCP_ARCH_CLIENT_ARCHITECTURE,
  361. DHCP_ARCH_CLIENT_NDI ) ) ),
  362. DHCPV6_CODE ( DHCPV6_CLIENT_ARCHITECTURE ),
  363. DHCPV6_WORD ( DHCP_ARCH_CLIENT_ARCHITECTURE ),
  364. DHCPV6_CODE ( DHCPV6_CLIENT_NDI ),
  365. DHCPV6_OPTION ( DHCP_ARCH_CLIENT_NDI )
  366. };
  367. /**
  368. * Name a DHCPv6 packet type
  369. *
  370. * @v type DHCPv6 packet type
  371. * @ret name DHCPv6 packet type name
  372. */
  373. static __attribute__ (( unused )) const char *
  374. dhcpv6_type_name ( unsigned int type ) {
  375. static char buf[ 12 /* "UNKNOWN-xxx" + NUL */ ];
  376. switch ( type ) {
  377. case DHCPV6_SOLICIT: return "SOLICIT";
  378. case DHCPV6_ADVERTISE: return "ADVERTISE";
  379. case DHCPV6_REQUEST: return "REQUEST";
  380. case DHCPV6_REPLY: return "REPLY";
  381. case DHCPV6_INFORMATION_REQUEST: return "INFORMATION-REQUEST";
  382. default:
  383. snprintf ( buf, sizeof ( buf ), "UNKNOWN-%d", type );
  384. return buf;
  385. }
  386. }
  387. /** A DHCPv6 session state */
  388. struct dhcpv6_session_state {
  389. /** Current transmitted packet type */
  390. uint8_t tx_type;
  391. /** Current expected received packet type */
  392. uint8_t rx_type;
  393. /** Flags */
  394. uint8_t flags;
  395. /** Next state (or NULL to terminate) */
  396. struct dhcpv6_session_state *next;
  397. };
  398. /** DHCPv6 session state flags */
  399. enum dhcpv6_session_state_flags {
  400. /** Include identity association within request */
  401. DHCPV6_TX_IA_NA = 0x01,
  402. /** Include leased IPv6 address within request */
  403. DHCPV6_TX_IAADDR = 0x02,
  404. /** Record received server ID */
  405. DHCPV6_RX_RECORD_SERVER_ID = 0x04,
  406. /** Record received IPv6 address */
  407. DHCPV6_RX_RECORD_IAADDR = 0x08,
  408. };
  409. /** DHCPv6 request state */
  410. static struct dhcpv6_session_state dhcpv6_request = {
  411. .tx_type = DHCPV6_REQUEST,
  412. .rx_type = DHCPV6_REPLY,
  413. .flags = ( DHCPV6_TX_IA_NA | DHCPV6_TX_IAADDR |
  414. DHCPV6_RX_RECORD_IAADDR ),
  415. .next = NULL,
  416. };
  417. /** DHCPv6 solicitation state */
  418. static struct dhcpv6_session_state dhcpv6_solicit = {
  419. .tx_type = DHCPV6_SOLICIT,
  420. .rx_type = DHCPV6_ADVERTISE,
  421. .flags = ( DHCPV6_TX_IA_NA | DHCPV6_RX_RECORD_SERVER_ID |
  422. DHCPV6_RX_RECORD_IAADDR ),
  423. .next = &dhcpv6_request,
  424. };
  425. /** DHCPv6 information request state */
  426. static struct dhcpv6_session_state dhcpv6_information_request = {
  427. .tx_type = DHCPV6_INFORMATION_REQUEST,
  428. .rx_type = DHCPV6_REPLY,
  429. .flags = 0,
  430. .next = NULL,
  431. };
  432. /** A DHCPv6 session */
  433. struct dhcpv6_session {
  434. /** Reference counter */
  435. struct refcnt refcnt;
  436. /** Job control interface */
  437. struct interface job;
  438. /** Data transfer interface */
  439. struct interface xfer;
  440. /** Network device being configured */
  441. struct net_device *netdev;
  442. /** Transaction ID */
  443. uint8_t xid[3];
  444. /** Identity association ID */
  445. uint32_t iaid;
  446. /** Start time (in ticks) */
  447. unsigned long start;
  448. /** Client DUID */
  449. struct dhcpv6_duid_uuid client_duid;
  450. /** Server DUID, if known */
  451. void *server_duid;
  452. /** Server DUID length */
  453. size_t server_duid_len;
  454. /** Leased IPv6 address */
  455. struct in6_addr lease;
  456. /** Retransmission timer */
  457. struct retry_timer timer;
  458. /** Current session state */
  459. struct dhcpv6_session_state *state;
  460. /** Current timeout status code */
  461. int rc;
  462. };
  463. /**
  464. * Free DHCPv6 session
  465. *
  466. * @v refcnt Reference count
  467. */
  468. static void dhcpv6_free ( struct refcnt *refcnt ) {
  469. struct dhcpv6_session *dhcpv6 =
  470. container_of ( refcnt, struct dhcpv6_session, refcnt );
  471. netdev_put ( dhcpv6->netdev );
  472. free ( dhcpv6->server_duid );
  473. free ( dhcpv6 );
  474. }
  475. /**
  476. * Terminate DHCPv6 session
  477. *
  478. * @v dhcpv6 DHCPv6 session
  479. * @v rc Reason for close
  480. */
  481. static void dhcpv6_finished ( struct dhcpv6_session *dhcpv6, int rc ) {
  482. /* Stop timer */
  483. stop_timer ( &dhcpv6->timer );
  484. /* Shut down interfaces */
  485. intf_shutdown ( &dhcpv6->xfer, rc );
  486. intf_shutdown ( &dhcpv6->job, rc );
  487. }
  488. /**
  489. * Transition to new DHCPv6 session state
  490. *
  491. * @v dhcpv6 DHCPv6 session
  492. * @v state New session state
  493. */
  494. static void dhcpv6_set_state ( struct dhcpv6_session *dhcpv6,
  495. struct dhcpv6_session_state *state ) {
  496. DBGC ( dhcpv6, "DHCPv6 %s entering %s state\n", dhcpv6->netdev->name,
  497. dhcpv6_type_name ( state->tx_type ) );
  498. /* Record state */
  499. dhcpv6->state = state;
  500. /* Default to -ETIMEDOUT if no more specific error is recorded */
  501. dhcpv6->rc = -ETIMEDOUT;
  502. /* Start timer to trigger transmission */
  503. start_timer_nodelay ( &dhcpv6->timer );
  504. }
  505. /**
  506. * Get DHCPv6 user class
  507. *
  508. * @v data Data buffer
  509. * @v len Length of data buffer
  510. * @ret len Length of user class
  511. */
  512. static size_t dhcpv6_user_class ( void *data, size_t len ) {
  513. static const char default_user_class[4] = { 'i', 'P', 'X', 'E' };
  514. int actual_len;
  515. /* Fetch user-class setting, if defined */
  516. actual_len = fetch_raw_setting ( NULL, &user_class_setting, data, len );
  517. if ( actual_len >= 0 )
  518. return actual_len;
  519. /* Otherwise, use the default user class ("iPXE") */
  520. if ( len > sizeof ( default_user_class ) )
  521. len = sizeof ( default_user_class );
  522. memcpy ( data, default_user_class, len );
  523. return sizeof ( default_user_class );
  524. }
  525. /**
  526. * Transmit current request
  527. *
  528. * @v dhcpv6 DHCPv6 session
  529. * @ret rc Return status code
  530. */
  531. static int dhcpv6_tx ( struct dhcpv6_session *dhcpv6 ) {
  532. struct dhcpv6_duid_option *client_id;
  533. struct dhcpv6_duid_option *server_id;
  534. struct dhcpv6_ia_na_option *ia_na;
  535. struct dhcpv6_iaaddr_option *iaaddr;
  536. struct dhcpv6_user_class_option *user_class;
  537. struct dhcpv6_elapsed_time_option *elapsed;
  538. struct dhcpv6_header *dhcphdr;
  539. struct io_buffer *iobuf;
  540. void *options;
  541. size_t client_id_len;
  542. size_t server_id_len;
  543. size_t ia_na_len;
  544. size_t user_class_string_len;
  545. size_t user_class_len;
  546. size_t elapsed_len;
  547. size_t total_len;
  548. int rc;
  549. /* Calculate lengths */
  550. client_id_len = ( sizeof ( *client_id ) +
  551. sizeof ( dhcpv6->client_duid ) );
  552. server_id_len = ( dhcpv6->server_duid ? ( sizeof ( *server_id ) +
  553. dhcpv6->server_duid_len ) :0);
  554. if ( dhcpv6->state->flags & DHCPV6_TX_IA_NA ) {
  555. ia_na_len = sizeof ( *ia_na );
  556. if ( dhcpv6->state->flags & DHCPV6_TX_IAADDR )
  557. ia_na_len += sizeof ( *iaaddr );
  558. } else {
  559. ia_na_len = 0;
  560. }
  561. user_class_string_len = dhcpv6_user_class ( NULL, 0 );
  562. user_class_len = ( sizeof ( *user_class ) +
  563. sizeof ( user_class->user_class[0] ) +
  564. user_class_string_len );
  565. elapsed_len = sizeof ( *elapsed );
  566. total_len = ( sizeof ( *dhcphdr ) + client_id_len + server_id_len +
  567. ia_na_len + sizeof ( dhcpv6_request_options_data ) +
  568. user_class_len + elapsed_len );
  569. /* Allocate packet */
  570. iobuf = xfer_alloc_iob ( &dhcpv6->xfer, total_len );
  571. if ( ! iobuf )
  572. return -ENOMEM;
  573. /* Construct header */
  574. dhcphdr = iob_put ( iobuf, sizeof ( *dhcphdr ) );
  575. dhcphdr->type = dhcpv6->state->tx_type;
  576. memcpy ( dhcphdr->xid, dhcpv6->xid, sizeof ( dhcphdr->xid ) );
  577. /* Construct client identifier */
  578. client_id = iob_put ( iobuf, client_id_len );
  579. client_id->header.code = htons ( DHCPV6_CLIENT_ID );
  580. client_id->header.len = htons ( client_id_len -
  581. sizeof ( client_id->header ) );
  582. memcpy ( client_id->duid, &dhcpv6->client_duid,
  583. sizeof ( dhcpv6->client_duid ) );
  584. /* Construct server identifier, if applicable */
  585. if ( server_id_len ) {
  586. server_id = iob_put ( iobuf, server_id_len );
  587. server_id->header.code = htons ( DHCPV6_SERVER_ID );
  588. server_id->header.len = htons ( server_id_len -
  589. sizeof ( server_id->header ) );
  590. memcpy ( server_id->duid, dhcpv6->server_duid,
  591. dhcpv6->server_duid_len );
  592. }
  593. /* Construct identity association, if applicable */
  594. if ( ia_na_len ) {
  595. ia_na = iob_put ( iobuf, ia_na_len );
  596. ia_na->header.code = htons ( DHCPV6_IA_NA );
  597. ia_na->header.len = htons ( ia_na_len -
  598. sizeof ( ia_na->header ) );
  599. ia_na->iaid = htonl ( dhcpv6->iaid );
  600. ia_na->renew = htonl ( 0 );
  601. ia_na->rebind = htonl ( 0 );
  602. if ( dhcpv6->state->flags & DHCPV6_TX_IAADDR ) {
  603. iaaddr = ( ( void * ) ia_na->options );
  604. iaaddr->header.code = htons ( DHCPV6_IAADDR );
  605. iaaddr->header.len = htons ( sizeof ( *iaaddr ) -
  606. sizeof ( iaaddr->header ));
  607. memcpy ( &iaaddr->address, &dhcpv6->lease,
  608. sizeof ( iaaddr->address ) );
  609. iaaddr->preferred = htonl ( 0 );
  610. iaaddr->valid = htonl ( 0 );
  611. }
  612. }
  613. /* Construct fixed request options */
  614. options = iob_put ( iobuf, sizeof ( dhcpv6_request_options_data ) );
  615. memcpy ( options, dhcpv6_request_options_data,
  616. sizeof ( dhcpv6_request_options_data ) );
  617. /* Construct user class */
  618. user_class = iob_put ( iobuf, user_class_len );
  619. user_class->header.code = htons ( DHCPV6_USER_CLASS );
  620. user_class->header.len = htons ( user_class_len -
  621. sizeof ( user_class->header ) );
  622. user_class->user_class[0].len = htons ( user_class_string_len );
  623. dhcpv6_user_class ( user_class->user_class[0].string,
  624. user_class_string_len );
  625. /* Construct elapsed time */
  626. elapsed = iob_put ( iobuf, elapsed_len );
  627. elapsed->header.code = htons ( DHCPV6_ELAPSED_TIME );
  628. elapsed->header.len = htons ( elapsed_len -
  629. sizeof ( elapsed->header ) );
  630. elapsed->elapsed = htons ( ( ( currticks() - dhcpv6->start ) * 100 ) /
  631. TICKS_PER_SEC );
  632. /* Sanity check */
  633. assert ( iob_len ( iobuf ) == total_len );
  634. /* Transmit packet */
  635. if ( ( rc = xfer_deliver_iob ( &dhcpv6->xfer, iobuf ) ) != 0 ) {
  636. DBGC ( dhcpv6, "DHCPv6 %s could not transmit: %s\n",
  637. dhcpv6->netdev->name, strerror ( rc ) );
  638. return rc;
  639. }
  640. return 0;
  641. }
  642. /**
  643. * Handle timer expiry
  644. *
  645. * @v timer Retransmission timer
  646. * @v fail Failure indicator
  647. */
  648. static void dhcpv6_timer_expired ( struct retry_timer *timer, int fail ) {
  649. struct dhcpv6_session *dhcpv6 =
  650. container_of ( timer, struct dhcpv6_session, timer );
  651. /* If we have failed, terminate DHCPv6 */
  652. if ( fail ) {
  653. dhcpv6_finished ( dhcpv6, dhcpv6->rc );
  654. return;
  655. }
  656. /* Restart timer */
  657. start_timer ( &dhcpv6->timer );
  658. /* (Re)transmit current request */
  659. dhcpv6_tx ( dhcpv6 );
  660. }
  661. /**
  662. * Receive new data
  663. *
  664. * @v dhcpv6 DHCPv6 session
  665. * @v iobuf I/O buffer
  666. * @v meta Data transfer metadata
  667. * @ret rc Return status code
  668. */
  669. static int dhcpv6_rx ( struct dhcpv6_session *dhcpv6,
  670. struct io_buffer *iobuf,
  671. struct xfer_metadata *meta ) {
  672. struct settings *parent = netdev_settings ( dhcpv6->netdev );
  673. struct sockaddr_in6 *src = ( ( struct sockaddr_in6 * ) meta->src );
  674. struct dhcpv6_header *dhcphdr = iobuf->data;
  675. struct dhcpv6_option_list options;
  676. const union dhcpv6_any_option *option;
  677. int rc;
  678. /* Sanity checks */
  679. if ( iob_len ( iobuf ) < sizeof ( *dhcphdr ) ) {
  680. DBGC ( dhcpv6, "DHCPv6 %s received packet too short (%zd "
  681. "bytes, min %zd bytes)\n", dhcpv6->netdev->name,
  682. iob_len ( iobuf ), sizeof ( *dhcphdr ) );
  683. rc = -EINVAL;
  684. goto done;
  685. }
  686. assert ( src != NULL );
  687. assert ( src->sin6_family == AF_INET6 );
  688. DBGC ( dhcpv6, "DHCPv6 %s received %s from %s\n",
  689. dhcpv6->netdev->name, dhcpv6_type_name ( dhcphdr->type ),
  690. inet6_ntoa ( &src->sin6_addr ) );
  691. /* Construct option list */
  692. options.data = dhcphdr->options;
  693. options.len = ( iob_len ( iobuf ) -
  694. offsetof ( typeof ( *dhcphdr ), options ) );
  695. /* Verify client identifier */
  696. if ( ( rc = dhcpv6_check_duid ( &options, DHCPV6_CLIENT_ID,
  697. &dhcpv6->client_duid,
  698. sizeof ( dhcpv6->client_duid ) ) ) !=0){
  699. DBGC ( dhcpv6, "DHCPv6 %s received %s without correct client "
  700. "ID: %s\n", dhcpv6->netdev->name,
  701. dhcpv6_type_name ( dhcphdr->type ), strerror ( rc ) );
  702. goto done;
  703. }
  704. /* Verify server identifier, if applicable */
  705. if ( dhcpv6->server_duid &&
  706. ( ( rc = dhcpv6_check_duid ( &options, DHCPV6_SERVER_ID,
  707. dhcpv6->server_duid,
  708. dhcpv6->server_duid_len ) ) != 0 ) ) {
  709. DBGC ( dhcpv6, "DHCPv6 %s received %s without correct server "
  710. "ID: %s\n", dhcpv6->netdev->name,
  711. dhcpv6_type_name ( dhcphdr->type ), strerror ( rc ) );
  712. goto done;
  713. }
  714. /* Check message type */
  715. if ( dhcphdr->type != dhcpv6->state->rx_type ) {
  716. DBGC ( dhcpv6, "DHCPv6 %s received %s while expecting %s\n",
  717. dhcpv6->netdev->name, dhcpv6_type_name ( dhcphdr->type ),
  718. dhcpv6_type_name ( dhcpv6->state->rx_type ) );
  719. rc = -ENOTTY;
  720. goto done;
  721. }
  722. /* Fetch status code, if present */
  723. if ( ( rc = dhcpv6_status_code ( &options ) ) != 0 ) {
  724. DBGC ( dhcpv6, "DHCPv6 %s received %s with error status: %s\n",
  725. dhcpv6->netdev->name, dhcpv6_type_name ( dhcphdr->type ),
  726. strerror ( rc ) );
  727. /* This is plausibly the error we want to return */
  728. dhcpv6->rc = rc;
  729. goto done;
  730. }
  731. /* Record identity association address, if applicable */
  732. if ( dhcpv6->state->flags & DHCPV6_RX_RECORD_IAADDR ) {
  733. if ( ( rc = dhcpv6_iaaddr ( &options, dhcpv6->iaid,
  734. &dhcpv6->lease ) ) != 0 ) {
  735. DBGC ( dhcpv6, "DHCPv6 %s received %s with unusable "
  736. "IAADDR: %s\n", dhcpv6->netdev->name,
  737. dhcpv6_type_name ( dhcphdr->type ),
  738. strerror ( rc ) );
  739. /* This is plausibly the error we want to return */
  740. dhcpv6->rc = rc;
  741. goto done;
  742. }
  743. DBGC ( dhcpv6, "DHCPv6 %s received %s is for %s\n",
  744. dhcpv6->netdev->name, dhcpv6_type_name ( dhcphdr->type ),
  745. inet6_ntoa ( &dhcpv6->lease ) );
  746. }
  747. /* Record server ID, if applicable */
  748. if ( dhcpv6->state->flags & DHCPV6_RX_RECORD_SERVER_ID ) {
  749. assert ( dhcpv6->server_duid == NULL );
  750. option = dhcpv6_option ( &options, DHCPV6_SERVER_ID );
  751. if ( ! option ) {
  752. DBGC ( dhcpv6, "DHCPv6 %s received %s missing server "
  753. "ID\n", dhcpv6->netdev->name,
  754. dhcpv6_type_name ( dhcphdr->type ) );
  755. rc = -EINVAL;
  756. goto done;
  757. }
  758. dhcpv6->server_duid_len = ntohs ( option->duid.header.len );
  759. dhcpv6->server_duid = malloc ( dhcpv6->server_duid_len );
  760. if ( ! dhcpv6->server_duid ) {
  761. rc = -ENOMEM;
  762. goto done;
  763. }
  764. memcpy ( dhcpv6->server_duid, option->duid.duid,
  765. dhcpv6->server_duid_len );
  766. }
  767. /* Transition to next state, if applicable */
  768. if ( dhcpv6->state->next ) {
  769. dhcpv6_set_state ( dhcpv6, dhcpv6->state->next );
  770. rc = 0;
  771. goto done;
  772. }
  773. /* Register settings */
  774. if ( ( rc = dhcpv6_register ( &dhcpv6->lease, &options,
  775. parent ) ) != 0 ) {
  776. DBGC ( dhcpv6, "DHCPv6 %s could not register settings: %s\n",
  777. dhcpv6->netdev->name, strerror ( rc ) );
  778. goto done;
  779. }
  780. /* Mark as complete */
  781. dhcpv6_finished ( dhcpv6, 0 );
  782. DBGC ( dhcpv6, "DHCPv6 %s complete\n", dhcpv6->netdev->name );
  783. done:
  784. free_iob ( iobuf );
  785. return rc;
  786. }
  787. /** DHCPv6 job control interface operations */
  788. static struct interface_operation dhcpv6_job_op[] = {
  789. INTF_OP ( intf_close, struct dhcpv6_session *, dhcpv6_finished ),
  790. };
  791. /** DHCPv6 job control interface descriptor */
  792. static struct interface_descriptor dhcpv6_job_desc =
  793. INTF_DESC ( struct dhcpv6_session, job, dhcpv6_job_op );
  794. /** DHCPv6 data transfer interface operations */
  795. static struct interface_operation dhcpv6_xfer_op[] = {
  796. INTF_OP ( xfer_deliver, struct dhcpv6_session *, dhcpv6_rx ),
  797. };
  798. /** DHCPv6 data transfer interface descriptor */
  799. static struct interface_descriptor dhcpv6_xfer_desc =
  800. INTF_DESC ( struct dhcpv6_session, xfer, dhcpv6_xfer_op );
  801. /**
  802. * Start DHCPv6
  803. *
  804. * @v job Job control interface
  805. * @v netdev Network device
  806. * @v stateful Perform stateful address autoconfiguration
  807. * @ret rc Return status code
  808. */
  809. int start_dhcpv6 ( struct interface *job, struct net_device *netdev,
  810. int stateful ) {
  811. struct ll_protocol *ll_protocol = netdev->ll_protocol;
  812. struct dhcpv6_session *dhcpv6;
  813. struct {
  814. union {
  815. struct sockaddr_in6 sin6;
  816. struct sockaddr sa;
  817. } client;
  818. union {
  819. struct sockaddr_in6 sin6;
  820. struct sockaddr sa;
  821. } server;
  822. } addresses;
  823. uint32_t xid;
  824. int len;
  825. int rc;
  826. /* Allocate and initialise structure */
  827. dhcpv6 = zalloc ( sizeof ( *dhcpv6 ) );
  828. if ( ! dhcpv6 )
  829. return -ENOMEM;
  830. ref_init ( &dhcpv6->refcnt, dhcpv6_free );
  831. intf_init ( &dhcpv6->job, &dhcpv6_job_desc, &dhcpv6->refcnt );
  832. intf_init ( &dhcpv6->xfer, &dhcpv6_xfer_desc, &dhcpv6->refcnt );
  833. dhcpv6->netdev = netdev_get ( netdev );
  834. xid = random();
  835. memcpy ( dhcpv6->xid, &xid, sizeof ( dhcpv6->xid ) );
  836. dhcpv6->start = currticks();
  837. timer_init ( &dhcpv6->timer, dhcpv6_timer_expired, &dhcpv6->refcnt );
  838. /* Construct client and server addresses */
  839. memset ( &addresses, 0, sizeof ( addresses ) );
  840. addresses.client.sin6.sin6_family = AF_INET6;
  841. addresses.client.sin6.sin6_port = htons ( DHCPV6_CLIENT_PORT );
  842. addresses.server.sin6.sin6_family = AF_INET6;
  843. ipv6_all_dhcp_relay_and_servers ( &addresses.server.sin6.sin6_addr );
  844. addresses.server.sin6.sin6_scope_id = netdev->index;
  845. addresses.server.sin6.sin6_port = htons ( DHCPV6_SERVER_PORT );
  846. /* Construct client DUID from system UUID */
  847. dhcpv6->client_duid.type = htons ( DHCPV6_DUID_UUID );
  848. if ( ( len = fetch_uuid_setting ( NULL, &uuid_setting,
  849. &dhcpv6->client_duid.uuid ) ) < 0 ) {
  850. rc = len;
  851. DBGC ( dhcpv6, "DHCPv6 %s could not create DUID-UUID: %s\n",
  852. dhcpv6->netdev->name, strerror ( rc ) );
  853. goto err_client_duid;
  854. }
  855. /* Construct IAID from link-layer address */
  856. dhcpv6->iaid = crc32_le ( 0, netdev->ll_addr, ll_protocol->ll_addr_len);
  857. DBGC ( dhcpv6, "DHCPv6 %s has XID %02x%02x%02x\n", dhcpv6->netdev->name,
  858. dhcpv6->xid[0], dhcpv6->xid[1], dhcpv6->xid[2] );
  859. /* Enter initial state */
  860. dhcpv6_set_state ( dhcpv6, ( stateful ? &dhcpv6_solicit :
  861. &dhcpv6_information_request ) );
  862. /* Open socket */
  863. if ( ( rc = xfer_open_socket ( &dhcpv6->xfer, SOCK_DGRAM,
  864. &addresses.server.sa,
  865. &addresses.client.sa ) ) != 0 ) {
  866. DBGC ( dhcpv6, "DHCPv6 %s could not open socket: %s\n",
  867. dhcpv6->netdev->name, strerror ( rc ) );
  868. goto err_open_socket;
  869. }
  870. /* Attach parent interface, mortalise self, and return */
  871. intf_plug_plug ( &dhcpv6->job, job );
  872. ref_put ( &dhcpv6->refcnt );
  873. return 0;
  874. err_open_socket:
  875. dhcpv6_finished ( dhcpv6, rc );
  876. err_client_duid:
  877. ref_put ( &dhcpv6->refcnt );
  878. return rc;
  879. }
  880. /** Boot filename setting */
  881. const struct setting filename6_setting __setting ( SETTING_BOOT, filename ) = {
  882. .name = "filename",
  883. .description = "Boot filename",
  884. .tag = DHCPV6_BOOTFILE_URL,
  885. .type = &setting_type_string,
  886. .scope = &dhcpv6_scope,
  887. };
  888. /** DNS search list setting */
  889. const struct setting dnssl6_setting __setting ( SETTING_IP_EXTRA, dnssl ) = {
  890. .name = "dnssl",
  891. .description = "DNS search list",
  892. .tag = DHCPV6_DOMAIN_LIST,
  893. .type = &setting_type_dnssl,
  894. .scope = &dhcpv6_scope,
  895. };