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.

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