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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  1. /*
  2. * Copyright (C) 2008 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., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301, USA.
  18. *
  19. * You can also choose to distribute this program under the terms of
  20. * the Unmodified Binary Distribution Licence (as given in the file
  21. * COPYING.UBDL), provided that you have satisfied its requirements.
  22. */
  23. FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
  24. #include <stdint.h>
  25. #include <stdlib.h>
  26. #include <stdio.h>
  27. #include <errno.h>
  28. #include <string.h>
  29. #include <ipxe/dhcp.h>
  30. #include <ipxe/dhcpopts.h>
  31. /** @file
  32. *
  33. * DHCP options
  34. *
  35. */
  36. /**
  37. * Obtain printable version of a DHCP option tag
  38. *
  39. * @v tag DHCP option tag
  40. * @ret name String representation of the tag
  41. *
  42. */
  43. static inline char * dhcp_tag_name ( unsigned int tag ) {
  44. static char name[8];
  45. if ( DHCP_IS_ENCAP_OPT ( tag ) ) {
  46. snprintf ( name, sizeof ( name ), "%d.%d",
  47. DHCP_ENCAPSULATOR ( tag ),
  48. DHCP_ENCAPSULATED ( tag ) );
  49. } else {
  50. snprintf ( name, sizeof ( name ), "%d", tag );
  51. }
  52. return name;
  53. }
  54. /**
  55. * Get pointer to DHCP option
  56. *
  57. * @v options DHCP options block
  58. * @v offset Offset within options block
  59. * @ret option DHCP option
  60. */
  61. static inline __attribute__ (( always_inline )) struct dhcp_option *
  62. dhcp_option ( struct dhcp_options *options, unsigned int offset ) {
  63. return ( ( struct dhcp_option * ) ( options->data + offset ) );
  64. }
  65. /**
  66. * Get offset of a DHCP option
  67. *
  68. * @v options DHCP options block
  69. * @v option DHCP option
  70. * @ret offset Offset within options block
  71. */
  72. static inline __attribute__ (( always_inline )) int
  73. dhcp_option_offset ( struct dhcp_options *options,
  74. struct dhcp_option *option ) {
  75. return ( ( ( void * ) option ) - options->data );
  76. }
  77. /**
  78. * Calculate length of any DHCP option
  79. *
  80. * @v option DHCP option
  81. * @ret len Length (including tag and length field)
  82. */
  83. static unsigned int dhcp_option_len ( struct dhcp_option *option ) {
  84. if ( ( option->tag == DHCP_END ) || ( option->tag == DHCP_PAD ) ) {
  85. return 1;
  86. } else {
  87. return ( option->len + DHCP_OPTION_HEADER_LEN );
  88. }
  89. }
  90. /**
  91. * Find DHCP option within DHCP options block, and its encapsulator (if any)
  92. *
  93. * @v options DHCP options block
  94. * @v tag DHCP option tag to search for
  95. * @ret encap_offset Offset of encapsulating DHCP option
  96. * @ret offset Offset of DHCP option, or negative error
  97. *
  98. * Searches for the DHCP option matching the specified tag within the
  99. * DHCP option block. Encapsulated options may be searched for by
  100. * using DHCP_ENCAP_OPT() to construct the tag value.
  101. *
  102. * If the option is encapsulated, and @c encap_offset is non-NULL, it
  103. * will be filled in with the offset of the encapsulating option.
  104. *
  105. * This routine is designed to be paranoid. It does not assume that
  106. * the option data is well-formatted, and so must guard against flaws
  107. * such as options missing a @c DHCP_END terminator, or options whose
  108. * length would take them beyond the end of the data block.
  109. */
  110. static int find_dhcp_option_with_encap ( struct dhcp_options *options,
  111. unsigned int tag,
  112. int *encap_offset ) {
  113. unsigned int original_tag __attribute__ (( unused )) = tag;
  114. struct dhcp_option *option;
  115. int offset = 0;
  116. ssize_t remaining = options->used_len;
  117. unsigned int option_len;
  118. /* Sanity check */
  119. if ( tag == DHCP_PAD )
  120. return -ENOENT;
  121. /* Search for option */
  122. while ( remaining ) {
  123. /* Calculate length of this option. Abort processing
  124. * if the length is malformed (i.e. takes us beyond
  125. * the end of the data block).
  126. */
  127. option = dhcp_option ( options, offset );
  128. option_len = dhcp_option_len ( option );
  129. remaining -= option_len;
  130. if ( remaining < 0 )
  131. break;
  132. /* Check for explicit end marker */
  133. if ( option->tag == DHCP_END ) {
  134. if ( tag == DHCP_END )
  135. /* Special case where the caller is interested
  136. * in whether we have this marker or not.
  137. */
  138. return offset;
  139. else
  140. break;
  141. }
  142. /* Check for matching tag */
  143. if ( option->tag == tag ) {
  144. DBGC ( options, "DHCPOPT %p found %s (length %d)\n",
  145. options, dhcp_tag_name ( original_tag ),
  146. option_len );
  147. return offset;
  148. }
  149. /* Check for start of matching encapsulation block */
  150. if ( DHCP_IS_ENCAP_OPT ( tag ) &&
  151. ( option->tag == DHCP_ENCAPSULATOR ( tag ) ) ) {
  152. if ( encap_offset )
  153. *encap_offset = offset;
  154. /* Continue search within encapsulated option block */
  155. tag = DHCP_ENCAPSULATED ( tag );
  156. remaining = option_len;
  157. offset += DHCP_OPTION_HEADER_LEN;
  158. continue;
  159. }
  160. offset += option_len;
  161. }
  162. return -ENOENT;
  163. }
  164. /**
  165. * Refuse to reallocate DHCP option block
  166. *
  167. * @v options DHCP option block
  168. * @v len New length
  169. * @ret rc Return status code
  170. */
  171. int dhcpopt_no_realloc ( struct dhcp_options *options, size_t len ) {
  172. return ( ( len <= options->alloc_len ) ? 0 : -ENOSPC );
  173. }
  174. /**
  175. * Resize a DHCP option
  176. *
  177. * @v options DHCP option block
  178. * @v offset Offset of option to resize
  179. * @v encap_offset Offset of encapsulating offset (or -ve for none)
  180. * @v old_len Old length (including header)
  181. * @v new_len New length (including header)
  182. * @ret rc Return status code
  183. */
  184. static int resize_dhcp_option ( struct dhcp_options *options,
  185. int offset, int encap_offset,
  186. size_t old_len, size_t new_len ) {
  187. struct dhcp_option *encapsulator;
  188. struct dhcp_option *option;
  189. ssize_t delta = ( new_len - old_len );
  190. size_t old_alloc_len;
  191. size_t new_used_len;
  192. size_t new_encapsulator_len;
  193. void *source;
  194. void *dest;
  195. int rc;
  196. /* Check for sufficient space */
  197. if ( new_len > DHCP_MAX_LEN ) {
  198. DBGC ( options, "DHCPOPT %p overlength option\n", options );
  199. return -ENOSPC;
  200. }
  201. new_used_len = ( options->used_len + delta );
  202. /* Expand options block, if necessary */
  203. if ( new_used_len > options->alloc_len ) {
  204. /* Reallocate options block */
  205. old_alloc_len = options->alloc_len;
  206. if ( ( rc = options->realloc ( options, new_used_len ) ) != 0 ){
  207. DBGC ( options, "DHCPOPT %p could not reallocate to "
  208. "%zd bytes\n", options, new_used_len );
  209. return rc;
  210. }
  211. /* Clear newly allocated space */
  212. memset ( ( options->data + old_alloc_len ), 0,
  213. ( options->alloc_len - old_alloc_len ) );
  214. }
  215. /* Update encapsulator, if applicable */
  216. if ( encap_offset >= 0 ) {
  217. encapsulator = dhcp_option ( options, encap_offset );
  218. new_encapsulator_len = ( encapsulator->len + delta );
  219. if ( new_encapsulator_len > DHCP_MAX_LEN ) {
  220. DBGC ( options, "DHCPOPT %p overlength encapsulator\n",
  221. options );
  222. return -ENOSPC;
  223. }
  224. encapsulator->len = new_encapsulator_len;
  225. }
  226. /* Update used length */
  227. options->used_len = new_used_len;
  228. /* Move remainder of option data */
  229. option = dhcp_option ( options, offset );
  230. source = ( ( ( void * ) option ) + old_len );
  231. dest = ( ( ( void * ) option ) + new_len );
  232. memmove ( dest, source, ( new_used_len - offset - new_len ) );
  233. /* Shrink options block, if applicable */
  234. if ( new_used_len < options->alloc_len ) {
  235. if ( ( rc = options->realloc ( options, new_used_len ) ) != 0 ){
  236. DBGC ( options, "DHCPOPT %p could not reallocate to "
  237. "%zd bytes\n", options, new_used_len );
  238. return rc;
  239. }
  240. }
  241. return 0;
  242. }
  243. /**
  244. * Set value of DHCP option
  245. *
  246. * @v options DHCP option block
  247. * @v tag DHCP option tag
  248. * @v data New value for DHCP option
  249. * @v len Length of value, in bytes
  250. * @ret offset Offset of DHCP option, or negative error
  251. *
  252. * Sets the value of a DHCP option within the options block. The
  253. * option may or may not already exist. Encapsulators will be created
  254. * (and deleted) as necessary.
  255. *
  256. * This call may fail due to insufficient space in the options block.
  257. * If it does fail, and the option existed previously, the option will
  258. * be left with its original value.
  259. */
  260. static int set_dhcp_option ( struct dhcp_options *options, unsigned int tag,
  261. const void *data, size_t len ) {
  262. static const uint8_t empty_encap[] = { DHCP_END };
  263. int offset;
  264. int encap_offset = -1;
  265. int creation_offset;
  266. struct dhcp_option *option;
  267. unsigned int encap_tag = DHCP_ENCAPSULATOR ( tag );
  268. size_t old_len = 0;
  269. size_t new_len = ( len ? ( len + DHCP_OPTION_HEADER_LEN ) : 0 );
  270. int rc;
  271. /* Sanity check */
  272. if ( tag == DHCP_PAD )
  273. return -ENOTTY;
  274. creation_offset = find_dhcp_option_with_encap ( options, DHCP_END,
  275. NULL );
  276. if ( creation_offset < 0 )
  277. creation_offset = options->used_len;
  278. /* Find old instance of this option, if any */
  279. offset = find_dhcp_option_with_encap ( options, tag, &encap_offset );
  280. if ( offset >= 0 ) {
  281. old_len = dhcp_option_len ( dhcp_option ( options, offset ) );
  282. DBGC ( options, "DHCPOPT %p resizing %s from %zd to %zd\n",
  283. options, dhcp_tag_name ( tag ), old_len, new_len );
  284. } else {
  285. DBGC ( options, "DHCPOPT %p creating %s (length %zd)\n",
  286. options, dhcp_tag_name ( tag ), new_len );
  287. }
  288. /* Ensure that encapsulator exists, if required */
  289. if ( encap_tag ) {
  290. if ( encap_offset < 0 ) {
  291. encap_offset =
  292. set_dhcp_option ( options, encap_tag,
  293. empty_encap,
  294. sizeof ( empty_encap ) );
  295. }
  296. if ( encap_offset < 0 )
  297. return encap_offset;
  298. creation_offset = ( encap_offset + DHCP_OPTION_HEADER_LEN );
  299. }
  300. /* Create new option if necessary */
  301. if ( offset < 0 )
  302. offset = creation_offset;
  303. /* Resize option to fit new data */
  304. if ( ( rc = resize_dhcp_option ( options, offset, encap_offset,
  305. old_len, new_len ) ) != 0 )
  306. return rc;
  307. /* Copy new data into option, if applicable */
  308. if ( len ) {
  309. option = dhcp_option ( options, offset );
  310. option->tag = tag;
  311. option->len = len;
  312. memcpy ( &option->data, data, len );
  313. }
  314. /* Delete encapsulator if there's nothing else left in it */
  315. if ( encap_offset >= 0 ) {
  316. option = dhcp_option ( options, encap_offset );
  317. if ( option->len <= 1 )
  318. set_dhcp_option ( options, encap_tag, NULL, 0 );
  319. }
  320. return offset;
  321. }
  322. /**
  323. * Check applicability of DHCP option setting
  324. *
  325. * @v tag Setting tag number
  326. * @ret applies Setting applies to this option block
  327. */
  328. int dhcpopt_applies ( unsigned int tag ) {
  329. return ( tag && ( tag <= DHCP_ENCAP_OPT ( DHCP_MAX_OPTION,
  330. DHCP_MAX_OPTION ) ) );
  331. }
  332. /**
  333. * Store value of DHCP option setting
  334. *
  335. * @v options DHCP option block
  336. * @v tag Setting tag number
  337. * @v data Setting data, or NULL to clear setting
  338. * @v len Length of setting data
  339. * @ret rc Return status code
  340. */
  341. int dhcpopt_store ( struct dhcp_options *options, unsigned int tag,
  342. const void *data, size_t len ) {
  343. int offset;
  344. offset = set_dhcp_option ( options, tag, data, len );
  345. if ( offset < 0 )
  346. return offset;
  347. return 0;
  348. }
  349. /**
  350. * Fetch value of DHCP option setting
  351. *
  352. * @v options DHCP option block
  353. * @v tag Setting tag number
  354. * @v data Buffer to fill with setting data
  355. * @v len Length of buffer
  356. * @ret len Length of setting data, or negative error
  357. */
  358. int dhcpopt_fetch ( struct dhcp_options *options, unsigned int tag,
  359. void *data, size_t len ) {
  360. int offset;
  361. struct dhcp_option *option;
  362. size_t option_len;
  363. offset = find_dhcp_option_with_encap ( options, tag, NULL );
  364. if ( offset < 0 )
  365. return offset;
  366. option = dhcp_option ( options, offset );
  367. option_len = option->len;
  368. if ( len > option_len )
  369. len = option_len;
  370. memcpy ( data, option->data, len );
  371. return option_len;
  372. }
  373. /**
  374. * Recalculate length of DHCP options block
  375. *
  376. * @v options Uninitialised DHCP option block
  377. *
  378. * The "used length" field will be updated based on scanning through
  379. * the block to find the end of the options.
  380. */
  381. void dhcpopt_update_used_len ( struct dhcp_options *options ) {
  382. struct dhcp_option *option;
  383. int offset = 0;
  384. ssize_t remaining = options->alloc_len;
  385. unsigned int option_len;
  386. /* Find last non-pad option */
  387. options->used_len = 0;
  388. while ( remaining ) {
  389. option = dhcp_option ( options, offset );
  390. option_len = dhcp_option_len ( option );
  391. remaining -= option_len;
  392. if ( remaining < 0 )
  393. break;
  394. offset += option_len;
  395. if ( option->tag != DHCP_PAD )
  396. options->used_len = offset;
  397. }
  398. }
  399. /**
  400. * Initialise prepopulated block of DHCP options
  401. *
  402. * @v options Uninitialised DHCP option block
  403. * @v data Memory for DHCP option data
  404. * @v alloc_len Length of memory for DHCP option data
  405. * @v realloc DHCP option block reallocator
  406. *
  407. * The memory content must already be filled with valid DHCP options.
  408. * A zeroed block counts as a block of valid DHCP options.
  409. */
  410. void dhcpopt_init ( struct dhcp_options *options, void *data, size_t alloc_len,
  411. int ( * realloc ) ( struct dhcp_options *options,
  412. size_t len ) ) {
  413. /* Fill in fields */
  414. options->data = data;
  415. options->alloc_len = alloc_len;
  416. options->realloc = realloc;
  417. /* Update length */
  418. dhcpopt_update_used_len ( options );
  419. DBGC ( options, "DHCPOPT %p created (data %p lengths %#zx,%#zx)\n",
  420. options, options->data, options->used_len, options->alloc_len );
  421. }