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

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