dhcpopts.c 19KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611
  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. static LIST_HEAD ( 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, &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, &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. }
  273. /**
  274. * Unregister DHCP option block
  275. *
  276. * @v options DHCP option block
  277. */
  278. void unregister_dhcp_options ( struct dhcp_option_block *options ) {
  279. list_del ( &options->list );
  280. dhcpopt_put ( options );
  281. }
  282. /**
  283. * Initialise empty block of DHCP options
  284. *
  285. * @v options Uninitialised DHCP option block
  286. * @v data Memory for DHCP option data
  287. * @v max_len Length of memory for DHCP option data
  288. *
  289. * Populates the DHCP option data with a single @c DHCP_END option and
  290. * fills in the fields of the @c dhcp_option_block structure.
  291. */
  292. void init_dhcp_options ( struct dhcp_option_block *options,
  293. void *data, size_t max_len ) {
  294. struct dhcp_option *option;
  295. options->data = data;
  296. options->max_len = max_len;
  297. option = options->data;
  298. option->tag = DHCP_END;
  299. options->len = 1;
  300. DBG ( "DHCP options block %p initialised (data %p max_len %#zx)\n",
  301. options, options->data, options->max_len );
  302. }
  303. /**
  304. * Allocate space for a block of DHCP options
  305. *
  306. * @v max_len Maximum length of option block
  307. * @ret options DHCP option block, or NULL
  308. *
  309. * Creates a new DHCP option block and populates it with an empty
  310. * options list. This call does not register the options block.
  311. */
  312. struct dhcp_option_block * alloc_dhcp_options ( size_t max_len ) {
  313. struct dhcp_option_block *options;
  314. options = malloc ( sizeof ( *options ) + max_len );
  315. if ( options ) {
  316. init_dhcp_options ( options,
  317. ( (void *) options + sizeof ( *options ) ),
  318. max_len );
  319. }
  320. return options;
  321. }
  322. /**
  323. * Resize a DHCP option
  324. *
  325. * @v options DHCP option block
  326. * @v option DHCP option to resize
  327. * @v encapsulator Encapsulating option (or NULL)
  328. * @v old_len Old length (including header)
  329. * @v new_len New length (including header)
  330. * @ret rc Return status code
  331. */
  332. static int resize_dhcp_option ( struct dhcp_option_block *options,
  333. struct dhcp_option *option,
  334. struct dhcp_option *encapsulator,
  335. size_t old_len, size_t new_len ) {
  336. void *source = ( ( ( void * ) option ) + old_len );
  337. void *dest = ( ( ( void * ) option ) + new_len );
  338. void *end = ( options->data + options->max_len );
  339. ssize_t delta = ( new_len - old_len );
  340. size_t new_options_len;
  341. size_t new_encapsulator_len;
  342. /* Check for sufficient space, and update length fields */
  343. if ( new_len > DHCP_MAX_LEN )
  344. return -ENOMEM;
  345. new_options_len = ( options->len + delta );
  346. if ( new_options_len > options->max_len )
  347. return -ENOMEM;
  348. if ( encapsulator ) {
  349. new_encapsulator_len = ( encapsulator->len + delta );
  350. if ( new_encapsulator_len > DHCP_MAX_LEN )
  351. return -ENOMEM;
  352. encapsulator->len = new_encapsulator_len;
  353. }
  354. options->len = new_options_len;
  355. /* Move remainder of option data */
  356. memmove ( dest, source, ( end - dest ) );
  357. return 0;
  358. }
  359. /**
  360. * Set value of DHCP option
  361. *
  362. * @v options DHCP option block
  363. * @v tag DHCP option tag
  364. * @v data New value for DHCP option
  365. * @v len Length of value, in bytes
  366. * @ret option DHCP option, or NULL
  367. *
  368. * Sets the value of a DHCP option within the options block. The
  369. * option may or may not already exist. Encapsulators will be created
  370. * (and deleted) as necessary.
  371. *
  372. * This call may fail due to insufficient space in the options block.
  373. * If it does fail, and the option existed previously, the option will
  374. * be left with its original value.
  375. */
  376. struct dhcp_option * set_dhcp_option ( struct dhcp_option_block *options,
  377. unsigned int tag,
  378. const void *data, size_t len ) {
  379. static const uint8_t empty_encapsulator[] = { DHCP_END };
  380. struct dhcp_option *option;
  381. void *insertion_point;
  382. struct dhcp_option *encapsulator = NULL;
  383. unsigned int encap_tag = DHCP_ENCAPSULATOR ( tag );
  384. size_t old_len = 0;
  385. size_t new_len = ( len ? ( len + DHCP_OPTION_HEADER_LEN ) : 0 );
  386. /* Return NULL if no options block specified */
  387. if ( ! options )
  388. return NULL;
  389. /* Find old instance of this option, if any */
  390. option = find_dhcp_option_with_encap ( options, tag, &encapsulator );
  391. if ( option ) {
  392. old_len = dhcp_option_len ( option );
  393. DBG ( "Resizing DHCP option %s from length %d to %d in block "
  394. "%p\n", dhcp_tag_name (tag), option->len, len, options );
  395. } else {
  396. old_len = 0;
  397. DBG ( "Creating DHCP option %s (length %d) in block %p\n",
  398. dhcp_tag_name ( tag ), len, options );
  399. }
  400. /* Ensure that encapsulator exists, if required */
  401. insertion_point = options->data;
  402. if ( DHCP_IS_ENCAP_OPT ( tag ) ) {
  403. if ( ! encapsulator )
  404. encapsulator = set_dhcp_option ( options, encap_tag,
  405. empty_encapsulator,
  406. sizeof ( empty_encapsulator) );
  407. if ( ! encapsulator )
  408. return NULL;
  409. insertion_point = &encapsulator->data;
  410. }
  411. /* Create new option if necessary */
  412. if ( ! option )
  413. option = insertion_point;
  414. /* Resize option to fit new data */
  415. if ( resize_dhcp_option ( options, option, encapsulator,
  416. old_len, new_len ) != 0 )
  417. return NULL;
  418. /* Copy new data into option, if applicable */
  419. if ( len ) {
  420. option->tag = tag;
  421. option->len = len;
  422. memcpy ( &option->data, data, len );
  423. }
  424. /* Delete encapsulator if there's nothing else left in it */
  425. if ( encapsulator && ( encapsulator->len <= 1 ) )
  426. set_dhcp_option ( options, encap_tag, NULL, 0 );
  427. return option;
  428. }
  429. /**
  430. * Find DHCP option within all registered DHCP options blocks
  431. *
  432. * @v tag DHCP option tag to search for
  433. * @ret option DHCP option, or NULL if not found
  434. *
  435. * This function exists merely as a notational shorthand for
  436. * find_dhcp_option() with @c options set to NULL.
  437. */
  438. struct dhcp_option * find_global_dhcp_option ( unsigned int tag ) {
  439. return find_dhcp_option ( NULL, tag );
  440. }
  441. /**
  442. * Find DHCP numerical option, and return its value
  443. *
  444. * @v options DHCP options block
  445. * @v tag DHCP option tag to search for
  446. * @ret value Numerical value of the option, or 0 if not found
  447. *
  448. * This function exists merely as a notational shorthand for a call to
  449. * find_dhcp_option() followed by a call to dhcp_num_option(). It is
  450. * not possible to distinguish between the cases "option not found"
  451. * and "option has a value of zero" using this function; if this
  452. * matters to you then issue the two constituent calls directly and
  453. * check that find_dhcp_option() returns a non-NULL value.
  454. */
  455. unsigned long find_dhcp_num_option ( struct dhcp_option_block *options,
  456. unsigned int tag ) {
  457. return dhcp_num_option ( find_dhcp_option ( options, tag ) );
  458. }
  459. /**
  460. * Find DHCP numerical option, and return its value
  461. *
  462. * @v tag DHCP option tag to search for
  463. * @ret value Numerical value of the option, or 0 if not found
  464. *
  465. * This function exists merely as a notational shorthand for a call to
  466. * find_global_dhcp_option() followed by a call to dhcp_num_option().
  467. * It is not possible to distinguish between the cases "option not
  468. * found" and "option has a value of zero" using this function; if
  469. * this matters to you then issue the two constituent calls directly
  470. * and check that find_global_dhcp_option() returns a non-NULL value.
  471. */
  472. unsigned long find_global_dhcp_num_option ( unsigned int tag ) {
  473. return dhcp_num_option ( find_global_dhcp_option ( tag ) );
  474. }
  475. /**
  476. * Find DHCP IPv4-address option, and return its value
  477. *
  478. * @v options DHCP options block
  479. * @v tag DHCP option tag to search for
  480. * @v inp IPv4 address to fill in
  481. * @ret value Numerical value of the option, or 0 if not found
  482. *
  483. * This function exists merely as a notational shorthand for a call to
  484. * find_dhcp_option() followed by a call to dhcp_ipv4_option(). It is
  485. * not possible to distinguish between the cases "option not found"
  486. * and "option has a value of 0.0.0.0" using this function; if this
  487. * matters to you then issue the two constituent calls directly and
  488. * check that find_dhcp_option() returns a non-NULL value.
  489. */
  490. void find_dhcp_ipv4_option ( struct dhcp_option_block *options,
  491. unsigned int tag, struct in_addr *inp ) {
  492. dhcp_ipv4_option ( find_dhcp_option ( options, tag ), inp );
  493. }
  494. /**
  495. * Find DHCP IPv4-address option, and return its value
  496. *
  497. * @v options DHCP options block
  498. * @v tag DHCP option tag to search for
  499. * @v inp IPv4 address to fill in
  500. * @ret value Numerical value of the option, or 0 if not found
  501. *
  502. * This function exists merely as a notational shorthand for a call to
  503. * find_dhcp_option() followed by a call to dhcp_ipv4_option(). It is
  504. * not possible to distinguish between the cases "option not found"
  505. * and "option has a value of 0.0.0.0" using this function; if this
  506. * matters to you then issue the two constituent calls directly and
  507. * check that find_dhcp_option() returns a non-NULL value.
  508. */
  509. void find_global_dhcp_ipv4_option ( unsigned int tag, struct in_addr *inp ) {
  510. dhcp_ipv4_option ( find_global_dhcp_option ( tag ), inp );
  511. }
  512. /**
  513. * Delete DHCP option
  514. *
  515. * @v options DHCP options block
  516. * @v tag DHCP option tag
  517. *
  518. * This function exists merely as a notational shorthand for a call to
  519. * set_dhcp_option() with @c len set to zero.
  520. */
  521. void delete_dhcp_option ( struct dhcp_option_block *options,
  522. unsigned int tag ) {
  523. set_dhcp_option ( options, tag, NULL, 0 );
  524. }
  525. /**
  526. * Apply DHCP options
  527. *
  528. * @v options DHCP options block, or NULL
  529. * @ret rc Return status code
  530. */
  531. int apply_dhcp_options ( struct dhcp_option_block *options ) {
  532. struct dhcp_option_applicator *applicator;
  533. struct dhcp_option *option;
  534. struct in_addr tftp_server;
  535. struct uri *uri;
  536. char uri_string[32];
  537. unsigned int tag;
  538. int rc;
  539. /* Set current working URI based on TFTP server */
  540. find_dhcp_ipv4_option ( options, DHCP_EB_SIADDR, &tftp_server );
  541. snprintf ( uri_string, sizeof ( uri_string ),
  542. "tftp://%s/", inet_ntoa ( tftp_server ) );
  543. uri = parse_uri ( uri_string );
  544. if ( ! uri )
  545. return -ENOMEM;
  546. churi ( uri );
  547. uri_put ( uri );
  548. /* Call all registered DHCP option applicators */
  549. for ( applicator = dhcp_option_applicators ;
  550. applicator < dhcp_option_applicators_end ; applicator++ ) {
  551. tag = applicator->tag;
  552. option = find_dhcp_option ( options, tag );
  553. if ( ! option )
  554. continue;
  555. if ( ( rc = applicator->apply ( tag, option ) ) != 0 ) {
  556. DBG ( "Could not apply DHCP option %s: %s\n",
  557. dhcp_tag_name ( tag ), strerror ( rc ) );
  558. return rc;
  559. }
  560. }
  561. return 0;
  562. }