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.

dhcpv6.c 28KB

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