You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

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