Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

dhcpopts.c 14KB

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