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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569
  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 <byteswap.h>
  21. #include <errno.h>
  22. #include <string.h>
  23. #include <assert.h>
  24. #include <vsprintf.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. list_add_tail ( &options->list, &existing->list );
  265. }
  266. /**
  267. * Unregister DHCP option block
  268. *
  269. * @v options DHCP option block
  270. */
  271. void unregister_dhcp_options ( struct dhcp_option_block *options ) {
  272. list_del ( &options->list );
  273. }
  274. /**
  275. * Initialise empty block of DHCP options
  276. *
  277. * @v options Uninitialised DHCP option block
  278. * @v data Memory for DHCP option data
  279. * @v max_len Length of memory for DHCP option data
  280. *
  281. * Populates the DHCP option data with a single @c DHCP_END option and
  282. * fills in the fields of the @c dhcp_option_block structure.
  283. */
  284. void init_dhcp_options ( struct dhcp_option_block *options,
  285. void *data, size_t max_len ) {
  286. struct dhcp_option *option;
  287. options->data = data;
  288. options->max_len = max_len;
  289. option = options->data;
  290. option->tag = DHCP_END;
  291. options->len = 1;
  292. DBG ( "DHCP options block %p initialised (data %p max_len %#zx)\n",
  293. options, options->data, options->max_len );
  294. }
  295. /**
  296. * Allocate space for a block of DHCP options
  297. *
  298. * @v max_len Maximum length of option block
  299. * @ret options DHCP option block, or NULL
  300. *
  301. * Creates a new DHCP option block and populates it with an empty
  302. * options list. This call does not register the options block.
  303. */
  304. struct dhcp_option_block * alloc_dhcp_options ( size_t max_len ) {
  305. struct dhcp_option_block *options;
  306. options = malloc ( sizeof ( *options ) + max_len );
  307. if ( options ) {
  308. init_dhcp_options ( options,
  309. ( (void *) options + sizeof ( *options ) ),
  310. max_len );
  311. }
  312. return options;
  313. }
  314. /**
  315. * Free DHCP options block
  316. *
  317. * @v options DHCP option block
  318. */
  319. void free_dhcp_options ( struct dhcp_option_block *options ) {
  320. free ( 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. }