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 29KB

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