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.

dhcpopts.c 19KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623
  1. /*
  2. * Copyright (C) 2006 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., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18. #include <stdint.h>
  19. #include <stdlib.h>
  20. #include <stdio.h>
  21. #include <byteswap.h>
  22. #include <errno.h>
  23. #include <string.h>
  24. #include <assert.h>
  25. #include <gpxe/list.h>
  26. #include <gpxe/in.h>
  27. #include <gpxe/uri.h>
  28. #include <gpxe/dhcp.h>
  29. /** @file
  30. *
  31. * DHCP options
  32. *
  33. */
  34. /** List of registered DHCP option blocks */
  35. LIST_HEAD ( dhcp_option_blocks );
  36. /** Registered DHCP option applicators */
  37. static struct dhcp_option_applicator dhcp_option_applicators[0]
  38. __table_start ( struct dhcp_option_applicator, dhcp_applicators );
  39. static struct dhcp_option_applicator dhcp_option_applicators_end[0]
  40. __table_end ( struct dhcp_option_applicator, dhcp_applicators );
  41. /**
  42. * Obtain printable version of a DHCP option tag
  43. *
  44. * @v tag DHCP option tag
  45. * @ret name String representation of the tag
  46. *
  47. */
  48. static inline char * dhcp_tag_name ( unsigned int tag ) {
  49. static char name[8];
  50. if ( DHCP_IS_ENCAP_OPT ( tag ) ) {
  51. snprintf ( name, sizeof ( name ), "%d.%d",
  52. DHCP_ENCAPSULATOR ( tag ),
  53. DHCP_ENCAPSULATED ( tag ) );
  54. } else {
  55. snprintf ( name, sizeof ( name ), "%d", tag );
  56. }
  57. return name;
  58. }
  59. /**
  60. * Obtain value of a numerical DHCP option
  61. *
  62. * @v option DHCP option, or NULL
  63. * @ret value Numerical value of the option, or 0
  64. *
  65. * Parses the numerical value from a DHCP option, if present. It is
  66. * permitted to call dhcp_num_option() with @c option set to NULL; in
  67. * this case 0 will be returned.
  68. *
  69. * The caller does not specify the size of the DHCP option data; this
  70. * is implied by the length field stored within the DHCP option
  71. * itself.
  72. */
  73. unsigned long dhcp_num_option ( struct dhcp_option *option ) {
  74. unsigned long value = 0;
  75. uint8_t *data;
  76. if ( option ) {
  77. /* This is actually smaller code than using htons()
  78. * etc., and will also cope well with malformed
  79. * options (such as zero-length options).
  80. */
  81. for ( data = option->data.bytes ;
  82. data < ( option->data.bytes + option->len ) ; data++ )
  83. value = ( ( value << 8 ) | *data );
  84. }
  85. return value;
  86. }
  87. /**
  88. * Obtain value of an IPv4-address DHCP option
  89. *
  90. * @v option DHCP option, or NULL
  91. * @v inp IPv4 address to fill in
  92. *
  93. * Parses the IPv4 address value from a DHCP option, if present. It
  94. * is permitted to call dhcp_ipv4_option() with @c option set to NULL;
  95. * in this case the address will be set to 0.0.0.0.
  96. */
  97. void dhcp_ipv4_option ( struct dhcp_option *option, struct in_addr *inp ) {
  98. if ( option )
  99. *inp = option->data.in;
  100. }
  101. /**
  102. * Print DHCP string option value into buffer
  103. *
  104. * @v buf Buffer into which to write the string
  105. * @v size Size of buffer
  106. * @v option DHCP option, or NULL
  107. * @ret len Length of formatted string
  108. *
  109. * DHCP option strings are stored without a NUL terminator. This
  110. * function provides a convenient way to extract these DHCP strings
  111. * into standard C strings. It is permitted to call dhcp_snprintf()
  112. * with @c option set to NULL; in this case the buffer will be filled
  113. * with an empty string.
  114. *
  115. * The usual snprintf() semantics apply with regard to buffer size,
  116. * return value when the buffer is too small, etc.
  117. */
  118. int dhcp_snprintf ( char *buf, size_t size, struct dhcp_option *option ) {
  119. size_t len;
  120. char *content = "";
  121. if ( option ) {
  122. /* Shrink buffer size so that it is only just large
  123. * enough to contain the option data. snprintf() will
  124. * take care of everything else (inserting the NUL etc.)
  125. */
  126. len = ( option->len + 1 );
  127. if ( len < size )
  128. size = len;
  129. content = option->data.string;
  130. }
  131. return snprintf ( buf, size, "%s", content );
  132. }
  133. /**
  134. * Calculate length of a normal DHCP option
  135. *
  136. * @v option DHCP option
  137. * @ret len Length (including tag and length field)
  138. *
  139. * @c option may not be a @c DHCP_PAD or @c DHCP_END option.
  140. */
  141. static inline unsigned int dhcp_option_len ( struct dhcp_option *option ) {
  142. assert ( option->tag != DHCP_PAD );
  143. assert ( option->tag != DHCP_END );
  144. return ( option->len + DHCP_OPTION_HEADER_LEN );
  145. }
  146. /**
  147. * Calculate length of any DHCP option
  148. *
  149. * @v option DHCP option
  150. * @ret len Length (including tag and length field)
  151. */
  152. static inline unsigned int dhcp_any_option_len ( struct dhcp_option *option ) {
  153. if ( ( option->tag == DHCP_END ) || ( option->tag == DHCP_PAD ) ) {
  154. return 1;
  155. } else {
  156. return dhcp_option_len ( option );
  157. }
  158. }
  159. /**
  160. * Find DHCP option within DHCP options block, and its encapsulator (if any)
  161. *
  162. * @v options DHCP options block
  163. * @v tag DHCP option tag to search for
  164. * @ret encapsulator Encapsulating DHCP option
  165. * @ret option DHCP option, or NULL if not found
  166. *
  167. * Searches for the DHCP option matching the specified tag within the
  168. * DHCP option block. Encapsulated options may be searched for by
  169. * using DHCP_ENCAP_OPT() to construct the tag value.
  170. *
  171. * If the option is encapsulated, and @c encapsulator is non-NULL, it
  172. * will be filled in with a pointer to the encapsulating option.
  173. *
  174. * This routine is designed to be paranoid. It does not assume that
  175. * the option data is well-formatted, and so must guard against flaws
  176. * such as options missing a @c DHCP_END terminator, or options whose
  177. * length would take them beyond the end of the data block.
  178. */
  179. static struct dhcp_option *
  180. find_dhcp_option_with_encap ( struct dhcp_option_block *options,
  181. unsigned int tag,
  182. struct dhcp_option **encapsulator ) {
  183. unsigned int original_tag __attribute__ (( unused )) = tag;
  184. struct dhcp_option *option = options->data;
  185. ssize_t remaining = options->len;
  186. unsigned int option_len;
  187. while ( remaining ) {
  188. /* Calculate length of this option. Abort processing
  189. * if the length is malformed (i.e. takes us beyond
  190. * the end of the data block).
  191. */
  192. option_len = dhcp_any_option_len ( option );
  193. remaining -= option_len;
  194. if ( remaining < 0 )
  195. break;
  196. /* Check for matching tag */
  197. if ( option->tag == tag ) {
  198. DBG ( "Found DHCP option %s (length %d) in block %p\n",
  199. dhcp_tag_name ( original_tag ), option->len,
  200. options );
  201. return option;
  202. }
  203. /* Check for explicit end marker */
  204. if ( option->tag == DHCP_END )
  205. break;
  206. /* Check for start of matching encapsulation block */
  207. if ( DHCP_IS_ENCAP_OPT ( tag ) &&
  208. ( option->tag == DHCP_ENCAPSULATOR ( tag ) ) ) {
  209. if ( encapsulator )
  210. *encapsulator = option;
  211. /* Continue search within encapsulated option block */
  212. tag = DHCP_ENCAPSULATED ( tag );
  213. remaining = option->len;
  214. option = ( void * ) &option->data;
  215. continue;
  216. }
  217. option = ( ( ( void * ) option ) + option_len );
  218. }
  219. return NULL;
  220. }
  221. /**
  222. * Find DHCP option within DHCP options block
  223. *
  224. * @v options DHCP options block, or NULL
  225. * @v tag DHCP option tag to search for
  226. * @ret option DHCP option, or NULL if not found
  227. *
  228. * Searches for the DHCP option matching the specified tag within the
  229. * DHCP option block. Encapsulated options may be searched for by
  230. * using DHCP_ENCAP_OPT() to construct the tag value.
  231. *
  232. * If @c options is NULL, all registered option blocks will be
  233. * searched. Where multiple option blocks contain the same DHCP
  234. * option, the option from the highest-priority block will be
  235. * returned. (Priority of an options block is determined by the value
  236. * of the @c DHCP_EB_PRIORITY option within the block, if present; the
  237. * default priority is zero).
  238. */
  239. struct dhcp_option * find_dhcp_option ( struct dhcp_option_block *options,
  240. unsigned int tag ) {
  241. struct dhcp_option *option;
  242. if ( options ) {
  243. return find_dhcp_option_with_encap ( options, tag, NULL );
  244. } else {
  245. list_for_each_entry ( options, &dhcp_option_blocks, list ) {
  246. if ( ( option = find_dhcp_option ( options, tag ) ) )
  247. return option;
  248. }
  249. return NULL;
  250. }
  251. }
  252. /**
  253. * Register DHCP option block
  254. *
  255. * @v options DHCP option block
  256. *
  257. * Register a block of DHCP options.
  258. */
  259. void register_dhcp_options ( struct dhcp_option_block *options ) {
  260. struct dhcp_option_block *existing;
  261. /* Determine priority of new block */
  262. options->priority = find_dhcp_num_option ( options, DHCP_EB_PRIORITY );
  263. DBG ( "Registering DHCP options block %p with priority %d\n",
  264. options, options->priority );
  265. /* Insert after any existing blocks which have a higher priority */
  266. list_for_each_entry ( existing, &dhcp_option_blocks, list ) {
  267. if ( options->priority > existing->priority )
  268. break;
  269. }
  270. dhcpopt_get ( options );
  271. list_add_tail ( &options->list, &existing->list );
  272. /* Apply all registered DHCP options */
  273. apply_global_dhcp_options();
  274. }
  275. /**
  276. * Unregister DHCP option block
  277. *
  278. * @v options DHCP option block
  279. */
  280. void unregister_dhcp_options ( struct dhcp_option_block *options ) {
  281. list_del ( &options->list );
  282. dhcpopt_put ( options );
  283. }
  284. /**
  285. * Initialise empty block of DHCP options
  286. *
  287. * @v options Uninitialised DHCP option block
  288. * @v data Memory for DHCP option data
  289. * @v max_len Length of memory for DHCP option data
  290. *
  291. * Populates the DHCP option data with a single @c DHCP_END option and
  292. * fills in the fields of the @c dhcp_option_block structure.
  293. */
  294. void init_dhcp_options ( struct dhcp_option_block *options,
  295. void *data, size_t max_len ) {
  296. struct dhcp_option *option;
  297. options->data = data;
  298. options->max_len = max_len;
  299. option = options->data;
  300. option->tag = DHCP_END;
  301. options->len = 1;
  302. DBG ( "DHCP options block %p initialised (data %p max_len %#zx)\n",
  303. options, options->data, options->max_len );
  304. }
  305. /**
  306. * Allocate space for a block of DHCP options
  307. *
  308. * @v max_len Maximum length of option block
  309. * @ret options DHCP option block, or NULL
  310. *
  311. * Creates a new DHCP option block and populates it with an empty
  312. * options list. This call does not register the options block.
  313. */
  314. struct dhcp_option_block * alloc_dhcp_options ( size_t max_len ) {
  315. struct dhcp_option_block *options;
  316. options = malloc ( sizeof ( *options ) + max_len );
  317. if ( options ) {
  318. init_dhcp_options ( options,
  319. ( (void *) options + sizeof ( *options ) ),
  320. max_len );
  321. }
  322. return options;
  323. }
  324. /**
  325. * Resize a DHCP option
  326. *
  327. * @v options DHCP option block
  328. * @v option DHCP option to resize
  329. * @v encapsulator Encapsulating option (or NULL)
  330. * @v old_len Old length (including header)
  331. * @v new_len New length (including header)
  332. * @ret rc Return status code
  333. */
  334. static int resize_dhcp_option ( struct dhcp_option_block *options,
  335. struct dhcp_option *option,
  336. struct dhcp_option *encapsulator,
  337. size_t old_len, size_t new_len ) {
  338. void *source = ( ( ( void * ) option ) + old_len );
  339. void *dest = ( ( ( void * ) option ) + new_len );
  340. void *end = ( options->data + options->max_len );
  341. ssize_t delta = ( new_len - old_len );
  342. size_t new_options_len;
  343. size_t new_encapsulator_len;
  344. /* Check for sufficient space, and update length fields */
  345. if ( new_len > DHCP_MAX_LEN )
  346. return -ENOMEM;
  347. new_options_len = ( options->len + delta );
  348. if ( new_options_len > options->max_len )
  349. return -ENOMEM;
  350. if ( encapsulator ) {
  351. new_encapsulator_len = ( encapsulator->len + delta );
  352. if ( new_encapsulator_len > DHCP_MAX_LEN )
  353. return -ENOMEM;
  354. encapsulator->len = new_encapsulator_len;
  355. }
  356. options->len = new_options_len;
  357. /* Move remainder of option data */
  358. memmove ( dest, source, ( end - dest ) );
  359. return 0;
  360. }
  361. /**
  362. * Set value of DHCP option
  363. *
  364. * @v options DHCP option block
  365. * @v tag DHCP option tag
  366. * @v data New value for DHCP option
  367. * @v len Length of value, in bytes
  368. * @ret option DHCP option, or NULL
  369. *
  370. * Sets the value of a DHCP option within the options block. The
  371. * option may or may not already exist. Encapsulators will be created
  372. * (and deleted) as necessary.
  373. *
  374. * This call may fail due to insufficient space in the options block.
  375. * If it does fail, and the option existed previously, the option will
  376. * be left with its original value.
  377. */
  378. struct dhcp_option * set_dhcp_option ( struct dhcp_option_block *options,
  379. unsigned int tag,
  380. const void *data, size_t len ) {
  381. static const uint8_t empty_encapsulator[] = { DHCP_END };
  382. struct dhcp_option *option;
  383. void *insertion_point;
  384. struct dhcp_option *encapsulator = NULL;
  385. unsigned int encap_tag = DHCP_ENCAPSULATOR ( tag );
  386. size_t old_len = 0;
  387. size_t new_len = ( len ? ( len + DHCP_OPTION_HEADER_LEN ) : 0 );
  388. /* Return NULL if no options block specified */
  389. if ( ! options )
  390. return NULL;
  391. /* Find old instance of this option, if any */
  392. option = find_dhcp_option_with_encap ( options, tag, &encapsulator );
  393. if ( option ) {
  394. old_len = dhcp_option_len ( option );
  395. DBG ( "Resizing DHCP option %s from length %d to %zd in block "
  396. "%p\n", dhcp_tag_name (tag), option->len, len, options );
  397. } else {
  398. old_len = 0;
  399. DBG ( "Creating DHCP option %s (length %zd) in block %p\n",
  400. dhcp_tag_name ( tag ), len, options );
  401. }
  402. /* Ensure that encapsulator exists, if required */
  403. insertion_point = options->data;
  404. if ( DHCP_IS_ENCAP_OPT ( tag ) ) {
  405. if ( ! encapsulator )
  406. encapsulator = set_dhcp_option ( options, encap_tag,
  407. empty_encapsulator,
  408. sizeof ( empty_encapsulator) );
  409. if ( ! encapsulator )
  410. return NULL;
  411. insertion_point = &encapsulator->data;
  412. }
  413. /* Create new option if necessary */
  414. if ( ! option )
  415. option = insertion_point;
  416. /* Resize option to fit new data */
  417. if ( resize_dhcp_option ( options, option, encapsulator,
  418. old_len, new_len ) != 0 )
  419. return NULL;
  420. /* Copy new data into option, if applicable */
  421. if ( len ) {
  422. option->tag = tag;
  423. option->len = len;
  424. memcpy ( &option->data, data, len );
  425. }
  426. /* Delete encapsulator if there's nothing else left in it */
  427. if ( encapsulator && ( encapsulator->len <= 1 ) )
  428. set_dhcp_option ( options, encap_tag, NULL, 0 );
  429. return option;
  430. }
  431. /**
  432. * Find DHCP option within all registered DHCP options blocks
  433. *
  434. * @v tag DHCP option tag to search for
  435. * @ret option DHCP option, or NULL if not found
  436. *
  437. * This function exists merely as a notational shorthand for
  438. * find_dhcp_option() with @c options set to NULL.
  439. */
  440. struct dhcp_option * find_global_dhcp_option ( unsigned int tag ) {
  441. return find_dhcp_option ( NULL, tag );
  442. }
  443. /**
  444. * Find DHCP numerical option, and return its value
  445. *
  446. * @v options DHCP options block
  447. * @v tag DHCP option tag to search for
  448. * @ret value Numerical value of the option, or 0 if not found
  449. *
  450. * This function exists merely as a notational shorthand for a call to
  451. * find_dhcp_option() followed by a call to dhcp_num_option(). It is
  452. * not possible to distinguish between the cases "option not found"
  453. * and "option has a value of zero" using this function; if this
  454. * matters to you then issue the two constituent calls directly and
  455. * check that find_dhcp_option() returns a non-NULL value.
  456. */
  457. unsigned long find_dhcp_num_option ( struct dhcp_option_block *options,
  458. unsigned int tag ) {
  459. return dhcp_num_option ( find_dhcp_option ( options, tag ) );
  460. }
  461. /**
  462. * Find DHCP numerical option, and return its value
  463. *
  464. * @v tag DHCP option tag to search for
  465. * @ret value Numerical value of the option, or 0 if not found
  466. *
  467. * This function exists merely as a notational shorthand for a call to
  468. * find_global_dhcp_option() followed by a call to dhcp_num_option().
  469. * It is not possible to distinguish between the cases "option not
  470. * found" and "option has a value of zero" using this function; if
  471. * this matters to you then issue the two constituent calls directly
  472. * and check that find_global_dhcp_option() returns a non-NULL value.
  473. */
  474. unsigned long find_global_dhcp_num_option ( unsigned int tag ) {
  475. return dhcp_num_option ( find_global_dhcp_option ( tag ) );
  476. }
  477. /**
  478. * Find DHCP IPv4-address option, and return its value
  479. *
  480. * @v options DHCP options block
  481. * @v tag DHCP option tag to search for
  482. * @v inp IPv4 address to fill in
  483. * @ret value Numerical value of the option, or 0 if not found
  484. *
  485. * This function exists merely as a notational shorthand for a call to
  486. * find_dhcp_option() followed by a call to dhcp_ipv4_option(). It is
  487. * not possible to distinguish between the cases "option not found"
  488. * and "option has a value of 0.0.0.0" using this function; if this
  489. * matters to you then issue the two constituent calls directly and
  490. * check that find_dhcp_option() returns a non-NULL value.
  491. */
  492. void find_dhcp_ipv4_option ( struct dhcp_option_block *options,
  493. unsigned int tag, struct in_addr *inp ) {
  494. dhcp_ipv4_option ( find_dhcp_option ( options, tag ), inp );
  495. }
  496. /**
  497. * Find DHCP IPv4-address option, and return its value
  498. *
  499. * @v options DHCP options block
  500. * @v tag DHCP option tag to search for
  501. * @v inp IPv4 address to fill in
  502. * @ret value Numerical value of the option, or 0 if not found
  503. *
  504. * This function exists merely as a notational shorthand for a call to
  505. * find_dhcp_option() followed by a call to dhcp_ipv4_option(). It is
  506. * not possible to distinguish between the cases "option not found"
  507. * and "option has a value of 0.0.0.0" using this function; if this
  508. * matters to you then issue the two constituent calls directly and
  509. * check that find_dhcp_option() returns a non-NULL value.
  510. */
  511. void find_global_dhcp_ipv4_option ( unsigned int tag, struct in_addr *inp ) {
  512. dhcp_ipv4_option ( find_global_dhcp_option ( tag ), inp );
  513. }
  514. /**
  515. * Delete DHCP option
  516. *
  517. * @v options DHCP options block
  518. * @v tag DHCP option tag
  519. *
  520. * This function exists merely as a notational shorthand for a call to
  521. * set_dhcp_option() with @c len set to zero.
  522. */
  523. void delete_dhcp_option ( struct dhcp_option_block *options,
  524. unsigned int tag ) {
  525. set_dhcp_option ( options, tag, NULL, 0 );
  526. }
  527. /**
  528. * Apply DHCP options
  529. *
  530. * @v options DHCP options block, or NULL
  531. * @ret rc Return status code
  532. */
  533. int apply_dhcp_options ( struct dhcp_option_block *options ) {
  534. struct dhcp_option_applicator *applicator;
  535. struct dhcp_option *option;
  536. struct in_addr tftp_server;
  537. struct uri *uri;
  538. char uri_string[32];
  539. unsigned int tag;
  540. int rc;
  541. /* Set current working URI based on TFTP server */
  542. find_dhcp_ipv4_option ( options, DHCP_EB_SIADDR, &tftp_server );
  543. snprintf ( uri_string, sizeof ( uri_string ),
  544. "tftp://%s/", inet_ntoa ( tftp_server ) );
  545. uri = parse_uri ( uri_string );
  546. if ( ! uri )
  547. return -ENOMEM;
  548. churi ( uri );
  549. uri_put ( uri );
  550. /* Call all registered DHCP option applicators */
  551. for ( applicator = dhcp_option_applicators ;
  552. applicator < dhcp_option_applicators_end ; applicator++ ) {
  553. tag = applicator->tag;
  554. option = find_dhcp_option ( options, tag );
  555. if ( ! option )
  556. continue;
  557. if ( ( rc = applicator->apply ( tag, option ) ) != 0 ) {
  558. DBG ( "Could not apply DHCP option %s: %s\n",
  559. dhcp_tag_name ( tag ), strerror ( rc ) );
  560. return rc;
  561. }
  562. }
  563. return 0;
  564. }
  565. /**
  566. * Apply global DHCP options
  567. *
  568. * @ret rc Return status code
  569. */
  570. int apply_global_dhcp_options ( void ) {
  571. return apply_dhcp_options ( NULL );
  572. }