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

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