Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

dhcpv6.c 29KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998
  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. /** Option list */
  234. struct dhcpv6_option_list options;
  235. };
  236. /**
  237. * Check applicability of DHCPv6 setting
  238. *
  239. * @v settings Settings block
  240. * @v setting Setting
  241. * @ret applies Setting applies within this settings block
  242. */
  243. static int dhcpv6_applies ( struct settings *settings __unused,
  244. const struct setting *setting ) {
  245. return ( setting->scope == &ipv6_scope );
  246. }
  247. /**
  248. * Fetch value of DHCPv6 setting
  249. *
  250. * @v settings Settings block
  251. * @v setting Setting to fetch
  252. * @v data Buffer to fill with setting data
  253. * @v len Length of buffer
  254. * @ret len Length of setting data, or negative error
  255. */
  256. static int dhcpv6_fetch ( struct settings *settings,
  257. struct setting *setting,
  258. void *data, size_t len ) {
  259. struct dhcpv6_settings *dhcpv6set =
  260. container_of ( settings, struct dhcpv6_settings, settings );
  261. const union dhcpv6_any_option *option;
  262. size_t option_len;
  263. /* Find option */
  264. option = dhcpv6_option ( &dhcpv6set->options, setting->tag );
  265. if ( ! option )
  266. return -ENOENT;
  267. /* Copy option to data buffer */
  268. option_len = ntohs ( option->header.len );
  269. if ( len > option_len )
  270. len = option_len;
  271. memcpy ( data, option->header.data, len );
  272. return option_len;
  273. }
  274. /** DHCPv6 settings operations */
  275. static struct settings_operations dhcpv6_settings_operations = {
  276. .applies = dhcpv6_applies,
  277. .fetch = dhcpv6_fetch,
  278. };
  279. /**
  280. * Register DHCPv6 options as network device settings
  281. *
  282. * @v options DHCPv6 option list
  283. * @v parent Parent settings block
  284. * @ret rc Return status code
  285. */
  286. static int dhcpv6_register ( struct dhcpv6_option_list *options,
  287. struct settings *parent ) {
  288. struct dhcpv6_settings *dhcpv6set;
  289. void *data;
  290. size_t len;
  291. int rc;
  292. /* Allocate and initialise structure */
  293. dhcpv6set = zalloc ( sizeof ( *dhcpv6set ) + options->len );
  294. if ( ! dhcpv6set ) {
  295. rc = -ENOMEM;
  296. goto err_alloc;
  297. }
  298. ref_init ( &dhcpv6set->refcnt, NULL );
  299. settings_init ( &dhcpv6set->settings, &dhcpv6_settings_operations,
  300. &dhcpv6set->refcnt, &ipv6_scope );
  301. data = ( ( ( void * ) dhcpv6set ) + sizeof ( *dhcpv6set ) );
  302. len = options->len;
  303. memcpy ( data, options->data, len );
  304. dhcpv6set->options.data = data;
  305. dhcpv6set->options.len = len;
  306. /* Register settings */
  307. if ( ( rc = register_settings ( &dhcpv6set->settings, parent,
  308. DHCPV6_SETTINGS_NAME ) ) != 0 )
  309. goto err_register;
  310. err_register:
  311. ref_put ( &dhcpv6set->refcnt );
  312. err_alloc:
  313. return rc;
  314. }
  315. /****************************************************************************
  316. *
  317. * DHCPv6 protocol
  318. *
  319. */
  320. /** Raw option data for options common to all DHCPv6 requests */
  321. static uint8_t dhcpv6_request_options_data[] = {
  322. DHCPV6_CODE ( DHCPV6_OPTION_REQUEST ),
  323. DHCPV6_OPTION ( DHCPV6_CODE ( DHCPV6_DNS_SERVERS ),
  324. DHCPV6_CODE ( DHCPV6_DOMAIN_LIST ),
  325. DHCPV6_CODE ( DHCPV6_BOOTFILE_URL ),
  326. DHCPV6_CODE ( DHCPV6_BOOTFILE_PARAM ) ),
  327. DHCPV6_CODE ( DHCPV6_VENDOR_CLASS ),
  328. DHCPV6_OPTION ( DHCPV6_DWORD_VALUE ( DHCPV6_VENDOR_CLASS_PXE ),
  329. DHCPV6_STRING ( DHCP_ARCH_VENDOR_CLASS_ID ) ),
  330. DHCPV6_CODE ( DHCPV6_CLIENT_ARCHITECTURE ),
  331. DHCPV6_WORD ( DHCP_ARCH_CLIENT_ARCHITECTURE ),
  332. DHCPV6_CODE ( DHCPV6_CLIENT_NDI ),
  333. DHCPV6_OPTION ( DHCP_ARCH_CLIENT_NDI )
  334. };
  335. /**
  336. * Name a DHCPv6 packet type
  337. *
  338. * @v type DHCPv6 packet type
  339. * @ret name DHCPv6 packet type name
  340. */
  341. static __attribute__ (( unused )) const char *
  342. dhcpv6_type_name ( unsigned int type ) {
  343. static char buf[ 12 /* "UNKNOWN-xxx" + NUL */ ];
  344. switch ( type ) {
  345. case DHCPV6_SOLICIT: return "SOLICIT";
  346. case DHCPV6_ADVERTISE: return "ADVERTISE";
  347. case DHCPV6_REQUEST: return "REQUEST";
  348. case DHCPV6_REPLY: return "REPLY";
  349. case DHCPV6_INFORMATION_REQUEST: return "INFORMATION-REQUEST";
  350. default:
  351. snprintf ( buf, sizeof ( buf ), "UNKNOWN-%d", type );
  352. return buf;
  353. }
  354. }
  355. /** A DHCPv6 session state */
  356. struct dhcpv6_session_state {
  357. /** Current transmitted packet type */
  358. uint8_t tx_type;
  359. /** Current expected received packet type */
  360. uint8_t rx_type;
  361. /** Flags */
  362. uint8_t flags;
  363. /** Next state (or NULL to terminate) */
  364. struct dhcpv6_session_state *next;
  365. };
  366. /** DHCPv6 session state flags */
  367. enum dhcpv6_session_state_flags {
  368. /** Include identity association within request */
  369. DHCPV6_TX_IA_NA = 0x01,
  370. /** Include leased IPv6 address within request */
  371. DHCPV6_TX_IAADDR = 0x02,
  372. /** Record received server ID */
  373. DHCPV6_RX_RECORD_SERVER_ID = 0x04,
  374. /** Record received IPv6 address */
  375. DHCPV6_RX_RECORD_IAADDR = 0x08,
  376. /** Apply received IPv6 address */
  377. DHCPV6_RX_APPLY_IAADDR = 0x10,
  378. };
  379. /** DHCPv6 request state */
  380. static struct dhcpv6_session_state dhcpv6_request = {
  381. .tx_type = DHCPV6_REQUEST,
  382. .rx_type = DHCPV6_REPLY,
  383. .flags = ( DHCPV6_TX_IA_NA | DHCPV6_TX_IAADDR |
  384. DHCPV6_RX_RECORD_IAADDR | DHCPV6_RX_APPLY_IAADDR ),
  385. .next = NULL,
  386. };
  387. /** DHCPv6 solicitation state */
  388. static struct dhcpv6_session_state dhcpv6_solicit = {
  389. .tx_type = DHCPV6_SOLICIT,
  390. .rx_type = DHCPV6_ADVERTISE,
  391. .flags = ( DHCPV6_TX_IA_NA | DHCPV6_RX_RECORD_SERVER_ID |
  392. DHCPV6_RX_RECORD_IAADDR ),
  393. .next = &dhcpv6_request,
  394. };
  395. /** DHCPv6 information request state */
  396. static struct dhcpv6_session_state dhcpv6_information_request = {
  397. .tx_type = DHCPV6_INFORMATION_REQUEST,
  398. .rx_type = DHCPV6_REPLY,
  399. .flags = 0,
  400. .next = NULL,
  401. };
  402. /** A DHCPv6 session */
  403. struct dhcpv6_session {
  404. /** Reference counter */
  405. struct refcnt refcnt;
  406. /** Job control interface */
  407. struct interface job;
  408. /** Data transfer interface */
  409. struct interface xfer;
  410. /** Network device being configured */
  411. struct net_device *netdev;
  412. /** Transaction ID */
  413. uint8_t xid[3];
  414. /** Identity association ID */
  415. uint32_t iaid;
  416. /** Start time (in ticks) */
  417. unsigned long start;
  418. /** Client DUID */
  419. struct dhcpv6_duid_uuid client_duid;
  420. /** Server DUID, if known */
  421. void *server_duid;
  422. /** Server DUID length */
  423. size_t server_duid_len;
  424. /** Leased IPv6 address */
  425. struct in6_addr lease;
  426. /** Retransmission timer */
  427. struct retry_timer timer;
  428. /** Current session state */
  429. struct dhcpv6_session_state *state;
  430. /** Current timeout status code */
  431. int rc;
  432. };
  433. /**
  434. * Free DHCPv6 session
  435. *
  436. * @v refcnt Reference count
  437. */
  438. static void dhcpv6_free ( struct refcnt *refcnt ) {
  439. struct dhcpv6_session *dhcpv6 =
  440. container_of ( refcnt, struct dhcpv6_session, refcnt );
  441. netdev_put ( dhcpv6->netdev );
  442. free ( dhcpv6->server_duid );
  443. free ( dhcpv6 );
  444. }
  445. /**
  446. * Terminate DHCPv6 session
  447. *
  448. * @v dhcpv6 DHCPv6 session
  449. * @v rc Reason for close
  450. */
  451. static void dhcpv6_finished ( struct dhcpv6_session *dhcpv6, int rc ) {
  452. /* Stop timer */
  453. stop_timer ( &dhcpv6->timer );
  454. /* Shut down interfaces */
  455. intf_shutdown ( &dhcpv6->xfer, rc );
  456. intf_shutdown ( &dhcpv6->job, rc );
  457. }
  458. /**
  459. * Transition to new DHCPv6 session state
  460. *
  461. * @v dhcpv6 DHCPv6 session
  462. * @v state New session state
  463. */
  464. static void dhcpv6_set_state ( struct dhcpv6_session *dhcpv6,
  465. struct dhcpv6_session_state *state ) {
  466. DBGC ( dhcpv6, "DHCPv6 %s entering %s state\n", dhcpv6->netdev->name,
  467. dhcpv6_type_name ( state->tx_type ) );
  468. /* Record state */
  469. dhcpv6->state = state;
  470. /* Default to -ETIMEDOUT if no more specific error is recorded */
  471. dhcpv6->rc = -ETIMEDOUT;
  472. /* Start timer to trigger transmission */
  473. start_timer_nodelay ( &dhcpv6->timer );
  474. }
  475. /**
  476. * Get DHCPv6 user class
  477. *
  478. * @v data Data buffer
  479. * @v len Length of data buffer
  480. * @ret len Length of user class
  481. */
  482. static size_t dhcpv6_user_class ( void *data, size_t len ) {
  483. static const char default_user_class[4] = { 'i', 'P', 'X', 'E' };
  484. int actual_len;
  485. /* Fetch user-class setting, if defined */
  486. actual_len = fetch_raw_setting ( NULL, &user_class_setting, data, len );
  487. if ( actual_len >= 0 )
  488. return actual_len;
  489. /* Otherwise, use the default user class ("iPXE") */
  490. if ( len > sizeof ( default_user_class ) )
  491. len = sizeof ( default_user_class );
  492. memcpy ( data, default_user_class, len );
  493. return sizeof ( default_user_class );
  494. }
  495. /**
  496. * Transmit current request
  497. *
  498. * @v dhcpv6 DHCPv6 session
  499. * @ret rc Return status code
  500. */
  501. static int dhcpv6_tx ( struct dhcpv6_session *dhcpv6 ) {
  502. struct dhcpv6_duid_option *client_id;
  503. struct dhcpv6_duid_option *server_id;
  504. struct dhcpv6_ia_na_option *ia_na;
  505. struct dhcpv6_iaaddr_option *iaaddr;
  506. struct dhcpv6_user_class_option *user_class;
  507. struct dhcpv6_elapsed_time_option *elapsed;
  508. struct dhcpv6_header *dhcphdr;
  509. struct io_buffer *iobuf;
  510. void *options;
  511. size_t client_id_len;
  512. size_t server_id_len;
  513. size_t ia_na_len;
  514. size_t user_class_string_len;
  515. size_t user_class_len;
  516. size_t elapsed_len;
  517. size_t total_len;
  518. int rc;
  519. /* Calculate lengths */
  520. client_id_len = ( sizeof ( *client_id ) +
  521. sizeof ( dhcpv6->client_duid ) );
  522. server_id_len = ( dhcpv6->server_duid ? ( sizeof ( *server_id ) +
  523. dhcpv6->server_duid_len ) :0);
  524. if ( dhcpv6->state->flags & DHCPV6_TX_IA_NA ) {
  525. ia_na_len = sizeof ( *ia_na );
  526. if ( dhcpv6->state->flags & DHCPV6_TX_IAADDR )
  527. ia_na_len += sizeof ( *iaaddr );
  528. } else {
  529. ia_na_len = 0;
  530. }
  531. user_class_string_len = dhcpv6_user_class ( NULL, 0 );
  532. user_class_len = ( sizeof ( *user_class ) +
  533. sizeof ( user_class->user_class[0] ) +
  534. user_class_string_len );
  535. elapsed_len = sizeof ( *elapsed );
  536. total_len = ( sizeof ( *dhcphdr ) + client_id_len + server_id_len +
  537. ia_na_len + sizeof ( dhcpv6_request_options_data ) +
  538. user_class_len + elapsed_len );
  539. /* Allocate packet */
  540. iobuf = xfer_alloc_iob ( &dhcpv6->xfer, total_len );
  541. if ( ! iobuf )
  542. return -ENOMEM;
  543. /* Construct header */
  544. dhcphdr = iob_put ( iobuf, sizeof ( *dhcphdr ) );
  545. dhcphdr->type = dhcpv6->state->tx_type;
  546. memcpy ( dhcphdr->xid, dhcpv6->xid, sizeof ( dhcphdr->xid ) );
  547. /* Construct client identifier */
  548. client_id = iob_put ( iobuf, client_id_len );
  549. client_id->header.code = htons ( DHCPV6_CLIENT_ID );
  550. client_id->header.len = htons ( client_id_len -
  551. sizeof ( client_id->header ) );
  552. memcpy ( client_id->duid, &dhcpv6->client_duid,
  553. sizeof ( dhcpv6->client_duid ) );
  554. /* Construct server identifier, if applicable */
  555. if ( server_id_len ) {
  556. server_id = iob_put ( iobuf, server_id_len );
  557. server_id->header.code = htons ( DHCPV6_SERVER_ID );
  558. server_id->header.len = htons ( server_id_len -
  559. sizeof ( server_id->header ) );
  560. memcpy ( server_id->duid, dhcpv6->server_duid,
  561. dhcpv6->server_duid_len );
  562. }
  563. /* Construct identity association, if applicable */
  564. if ( ia_na_len ) {
  565. ia_na = iob_put ( iobuf, ia_na_len );
  566. ia_na->header.code = htons ( DHCPV6_IA_NA );
  567. ia_na->header.len = htons ( ia_na_len -
  568. sizeof ( ia_na->header ) );
  569. ia_na->iaid = htonl ( dhcpv6->iaid );
  570. ia_na->renew = htonl ( 0 );
  571. ia_na->rebind = htonl ( 0 );
  572. if ( dhcpv6->state->flags & DHCPV6_TX_IAADDR ) {
  573. iaaddr = ( ( void * ) ia_na->options );
  574. iaaddr->header.code = htons ( DHCPV6_IAADDR );
  575. iaaddr->header.len = htons ( sizeof ( *iaaddr ) -
  576. sizeof ( iaaddr->header ));
  577. memcpy ( &iaaddr->address, &dhcpv6->lease,
  578. sizeof ( iaaddr->address ) );
  579. iaaddr->preferred = htonl ( 0 );
  580. iaaddr->valid = htonl ( 0 );
  581. }
  582. }
  583. /* Construct fixed request options */
  584. options = iob_put ( iobuf, sizeof ( dhcpv6_request_options_data ) );
  585. memcpy ( options, dhcpv6_request_options_data,
  586. sizeof ( dhcpv6_request_options_data ) );
  587. /* Construct user class */
  588. user_class = iob_put ( iobuf, user_class_len );
  589. user_class->header.code = htons ( DHCPV6_USER_CLASS );
  590. user_class->header.len = htons ( user_class_len -
  591. sizeof ( user_class->header ) );
  592. user_class->user_class[0].len = htons ( user_class_string_len );
  593. dhcpv6_user_class ( user_class->user_class[0].string,
  594. user_class_string_len );
  595. /* Construct elapsed time */
  596. elapsed = iob_put ( iobuf, elapsed_len );
  597. elapsed->header.code = htons ( DHCPV6_ELAPSED_TIME );
  598. elapsed->header.len = htons ( elapsed_len -
  599. sizeof ( elapsed->header ) );
  600. elapsed->elapsed = htons ( ( ( currticks() - dhcpv6->start ) * 100 ) /
  601. TICKS_PER_SEC );
  602. /* Sanity check */
  603. assert ( iob_len ( iobuf ) == total_len );
  604. /* Transmit packet */
  605. if ( ( rc = xfer_deliver_iob ( &dhcpv6->xfer, iobuf ) ) != 0 ) {
  606. DBGC ( dhcpv6, "DHCPv6 %s could not transmit: %s\n",
  607. dhcpv6->netdev->name, strerror ( rc ) );
  608. return rc;
  609. }
  610. return 0;
  611. }
  612. /**
  613. * Handle timer expiry
  614. *
  615. * @v timer Retransmission timer
  616. * @v fail Failure indicator
  617. */
  618. static void dhcpv6_timer_expired ( struct retry_timer *timer, int fail ) {
  619. struct dhcpv6_session *dhcpv6 =
  620. container_of ( timer, struct dhcpv6_session, timer );
  621. /* If we have failed, terminate DHCPv6 */
  622. if ( fail ) {
  623. dhcpv6_finished ( dhcpv6, dhcpv6->rc );
  624. return;
  625. }
  626. /* Restart timer */
  627. start_timer ( &dhcpv6->timer );
  628. /* (Re)transmit current request */
  629. dhcpv6_tx ( dhcpv6 );
  630. }
  631. /**
  632. * Receive new data
  633. *
  634. * @v dhcpv6 DHCPv6 session
  635. * @v iobuf I/O buffer
  636. * @v meta Data transfer metadata
  637. * @ret rc Return status code
  638. */
  639. static int dhcpv6_rx ( struct dhcpv6_session *dhcpv6,
  640. struct io_buffer *iobuf,
  641. struct xfer_metadata *meta ) {
  642. struct settings *parent = netdev_settings ( dhcpv6->netdev );
  643. struct sockaddr_in6 *src = ( ( struct sockaddr_in6 * ) meta->src );
  644. struct dhcpv6_header *dhcphdr = iobuf->data;
  645. struct dhcpv6_option_list options;
  646. const union dhcpv6_any_option *option;
  647. int rc;
  648. /* Sanity checks */
  649. if ( iob_len ( iobuf ) < sizeof ( *dhcphdr ) ) {
  650. DBGC ( dhcpv6, "DHCPv6 %s received packet too short (%zd "
  651. "bytes, min %zd bytes)\n", dhcpv6->netdev->name,
  652. iob_len ( iobuf ), sizeof ( *dhcphdr ) );
  653. rc = -EINVAL;
  654. goto done;
  655. }
  656. assert ( src != NULL );
  657. assert ( src->sin6_family == AF_INET6 );
  658. DBGC ( dhcpv6, "DHCPv6 %s received %s from %s\n",
  659. dhcpv6->netdev->name, dhcpv6_type_name ( dhcphdr->type ),
  660. inet6_ntoa ( &src->sin6_addr ) );
  661. /* Construct option list */
  662. options.data = dhcphdr->options;
  663. options.len = ( iob_len ( iobuf ) -
  664. offsetof ( typeof ( *dhcphdr ), options ) );
  665. /* Verify client identifier */
  666. if ( ( rc = dhcpv6_check_duid ( &options, DHCPV6_CLIENT_ID,
  667. &dhcpv6->client_duid,
  668. sizeof ( dhcpv6->client_duid ) ) ) !=0){
  669. DBGC ( dhcpv6, "DHCPv6 %s received %s without correct client "
  670. "ID: %s\n", dhcpv6->netdev->name,
  671. dhcpv6_type_name ( dhcphdr->type ), strerror ( rc ) );
  672. goto done;
  673. }
  674. /* Verify server identifier, if applicable */
  675. if ( dhcpv6->server_duid &&
  676. ( ( rc = dhcpv6_check_duid ( &options, DHCPV6_SERVER_ID,
  677. dhcpv6->server_duid,
  678. dhcpv6->server_duid_len ) ) != 0 ) ) {
  679. DBGC ( dhcpv6, "DHCPv6 %s received %s without correct server "
  680. "ID: %s\n", dhcpv6->netdev->name,
  681. dhcpv6_type_name ( dhcphdr->type ), strerror ( rc ) );
  682. goto done;
  683. }
  684. /* Check message type */
  685. if ( dhcphdr->type != dhcpv6->state->rx_type ) {
  686. DBGC ( dhcpv6, "DHCPv6 %s received %s while expecting %s\n",
  687. dhcpv6->netdev->name, dhcpv6_type_name ( dhcphdr->type ),
  688. dhcpv6_type_name ( dhcpv6->state->rx_type ) );
  689. rc = -ENOTTY;
  690. goto done;
  691. }
  692. /* Fetch status code, if present */
  693. if ( ( rc = dhcpv6_status_code ( &options ) ) != 0 ) {
  694. DBGC ( dhcpv6, "DHCPv6 %s received %s with error status: %s\n",
  695. dhcpv6->netdev->name, dhcpv6_type_name ( dhcphdr->type ),
  696. strerror ( rc ) );
  697. /* This is plausibly the error we want to return */
  698. dhcpv6->rc = rc;
  699. goto done;
  700. }
  701. /* Record identity association address, if applicable */
  702. if ( dhcpv6->state->flags & DHCPV6_RX_RECORD_IAADDR ) {
  703. if ( ( rc = dhcpv6_iaaddr ( &options, dhcpv6->iaid,
  704. &dhcpv6->lease ) ) != 0 ) {
  705. DBGC ( dhcpv6, "DHCPv6 %s received %s with unusable "
  706. "IAADDR: %s\n", dhcpv6->netdev->name,
  707. dhcpv6_type_name ( dhcphdr->type ),
  708. strerror ( rc ) );
  709. /* This is plausibly the error we want to return */
  710. dhcpv6->rc = rc;
  711. goto done;
  712. }
  713. DBGC ( dhcpv6, "DHCPv6 %s received %s is for %s\n",
  714. dhcpv6->netdev->name, dhcpv6_type_name ( dhcphdr->type ),
  715. inet6_ntoa ( &dhcpv6->lease ) );
  716. }
  717. /* Record server ID, if applicable */
  718. if ( dhcpv6->state->flags & DHCPV6_RX_RECORD_SERVER_ID ) {
  719. assert ( dhcpv6->server_duid == NULL );
  720. option = dhcpv6_option ( &options, DHCPV6_SERVER_ID );
  721. if ( ! option ) {
  722. DBGC ( dhcpv6, "DHCPv6 %s received %s missing server "
  723. "ID\n", dhcpv6->netdev->name,
  724. dhcpv6_type_name ( dhcphdr->type ) );
  725. rc = -EINVAL;
  726. goto done;
  727. }
  728. dhcpv6->server_duid_len = ntohs ( option->duid.header.len );
  729. dhcpv6->server_duid = malloc ( dhcpv6->server_duid_len );
  730. if ( ! dhcpv6->server_duid ) {
  731. rc = -ENOMEM;
  732. goto done;
  733. }
  734. memcpy ( dhcpv6->server_duid, option->duid.duid,
  735. dhcpv6->server_duid_len );
  736. }
  737. /* Apply identity association address, if applicable */
  738. if ( dhcpv6->state->flags & DHCPV6_RX_APPLY_IAADDR ) {
  739. if ( ( rc = ipv6_set_address ( dhcpv6->netdev,
  740. &dhcpv6->lease ) ) != 0 ) {
  741. DBGC ( dhcpv6, "DHCPv6 %s could not apply %s: %s\n",
  742. dhcpv6->netdev->name,
  743. inet6_ntoa ( &dhcpv6->lease ), strerror ( rc ) );
  744. /* This is plausibly the error we want to return */
  745. dhcpv6->rc = rc;
  746. goto done;
  747. }
  748. }
  749. /* Transition to next state or complete DHCPv6, as applicable */
  750. if ( dhcpv6->state->next ) {
  751. /* Transition to next state */
  752. dhcpv6_set_state ( dhcpv6, dhcpv6->state->next );
  753. rc = 0;
  754. } else {
  755. /* Register settings */
  756. if ( ( rc = dhcpv6_register ( &options, parent ) ) != 0 ) {
  757. DBGC ( dhcpv6, "DHCPv6 %s could not register "
  758. "settings: %s\n", dhcpv6->netdev->name,
  759. strerror ( rc ) );
  760. goto done;
  761. }
  762. /* Mark as complete */
  763. dhcpv6_finished ( dhcpv6, 0 );
  764. DBGC ( dhcpv6, "DHCPv6 %s complete\n", dhcpv6->netdev->name );
  765. }
  766. done:
  767. free_iob ( iobuf );
  768. return rc;
  769. }
  770. /** DHCPv6 job control interface operations */
  771. static struct interface_operation dhcpv6_job_op[] = {
  772. INTF_OP ( intf_close, struct dhcpv6_session *, dhcpv6_finished ),
  773. };
  774. /** DHCPv6 job control interface descriptor */
  775. static struct interface_descriptor dhcpv6_job_desc =
  776. INTF_DESC ( struct dhcpv6_session, job, dhcpv6_job_op );
  777. /** DHCPv6 data transfer interface operations */
  778. static struct interface_operation dhcpv6_xfer_op[] = {
  779. INTF_OP ( xfer_deliver, struct dhcpv6_session *, dhcpv6_rx ),
  780. };
  781. /** DHCPv6 data transfer interface descriptor */
  782. static struct interface_descriptor dhcpv6_xfer_desc =
  783. INTF_DESC ( struct dhcpv6_session, xfer, dhcpv6_xfer_op );
  784. /**
  785. * Start DHCPv6
  786. *
  787. * @v job Job control interface
  788. * @v netdev Network device
  789. * @v stateful Perform stateful address autoconfiguration
  790. * @ret rc Return status code
  791. */
  792. int start_dhcpv6 ( struct interface *job, struct net_device *netdev,
  793. int stateful ) {
  794. struct ll_protocol *ll_protocol = netdev->ll_protocol;
  795. struct dhcpv6_session *dhcpv6;
  796. struct {
  797. union {
  798. struct sockaddr_in6 sin6;
  799. struct sockaddr sa;
  800. } client;
  801. union {
  802. struct sockaddr_in6 sin6;
  803. struct sockaddr sa;
  804. } server;
  805. } addresses;
  806. uint32_t xid;
  807. int len;
  808. int rc;
  809. /* Allocate and initialise structure */
  810. dhcpv6 = zalloc ( sizeof ( *dhcpv6 ) );
  811. if ( ! dhcpv6 )
  812. return -ENOMEM;
  813. ref_init ( &dhcpv6->refcnt, dhcpv6_free );
  814. intf_init ( &dhcpv6->job, &dhcpv6_job_desc, &dhcpv6->refcnt );
  815. intf_init ( &dhcpv6->xfer, &dhcpv6_xfer_desc, &dhcpv6->refcnt );
  816. dhcpv6->netdev = netdev_get ( netdev );
  817. xid = random();
  818. memcpy ( dhcpv6->xid, &xid, sizeof ( dhcpv6->xid ) );
  819. dhcpv6->start = currticks();
  820. timer_init ( &dhcpv6->timer, dhcpv6_timer_expired, &dhcpv6->refcnt );
  821. /* Construct client and server addresses */
  822. memset ( &addresses, 0, sizeof ( addresses ) );
  823. addresses.client.sin6.sin6_family = AF_INET6;
  824. addresses.client.sin6.sin6_port = htons ( DHCPV6_CLIENT_PORT );
  825. addresses.server.sin6.sin6_family = AF_INET6;
  826. ipv6_all_dhcp_relay_and_servers ( &addresses.server.sin6.sin6_addr );
  827. addresses.server.sin6.sin6_scope_id = netdev->index;
  828. addresses.server.sin6.sin6_port = htons ( DHCPV6_SERVER_PORT );
  829. /* Construct client DUID from system UUID */
  830. dhcpv6->client_duid.type = htons ( DHCPV6_DUID_UUID );
  831. if ( ( len = fetch_uuid_setting ( NULL, &uuid_setting,
  832. &dhcpv6->client_duid.uuid ) ) < 0 ) {
  833. rc = len;
  834. DBGC ( dhcpv6, "DHCPv6 %s could not create DUID-UUID: %s\n",
  835. dhcpv6->netdev->name, strerror ( rc ) );
  836. goto err_client_duid;
  837. }
  838. /* Construct IAID from link-layer address */
  839. dhcpv6->iaid = crc32_le ( 0, netdev->ll_addr, ll_protocol->ll_addr_len);
  840. DBGC ( dhcpv6, "DHCPv6 %s has XID %02x%02x%02x\n", dhcpv6->netdev->name,
  841. dhcpv6->xid[0], dhcpv6->xid[1], dhcpv6->xid[2] );
  842. /* Enter initial state */
  843. dhcpv6_set_state ( dhcpv6, ( stateful ? &dhcpv6_solicit :
  844. &dhcpv6_information_request ) );
  845. /* Open socket */
  846. if ( ( rc = xfer_open_socket ( &dhcpv6->xfer, SOCK_DGRAM,
  847. &addresses.server.sa,
  848. &addresses.client.sa ) ) != 0 ) {
  849. DBGC ( dhcpv6, "DHCPv6 %s could not open socket: %s\n",
  850. dhcpv6->netdev->name, strerror ( rc ) );
  851. goto err_open_socket;
  852. }
  853. /* Attach parent interface, mortalise self, and return */
  854. intf_plug_plug ( &dhcpv6->job, job );
  855. ref_put ( &dhcpv6->refcnt );
  856. return 0;
  857. err_open_socket:
  858. dhcpv6_finished ( dhcpv6, rc );
  859. err_client_duid:
  860. ref_put ( &dhcpv6->refcnt );
  861. return rc;
  862. }
  863. /** Boot filename setting */
  864. const struct setting filename6_setting __setting ( SETTING_BOOT, filename ) = {
  865. .name = "filename",
  866. .description = "Boot filename",
  867. .tag = DHCPV6_BOOTFILE_URL,
  868. .type = &setting_type_string,
  869. .scope = &ipv6_scope,
  870. };
  871. /** DNS search list setting */
  872. const struct setting dnssl6_setting __setting ( SETTING_IP_EXTRA, dnssl ) = {
  873. .name = "dnssl",
  874. .description = "DNS search list",
  875. .tag = DHCPV6_DOMAIN_LIST,
  876. .type = &setting_type_dnssl,
  877. .scope = &ipv6_scope,
  878. };