選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

dhcpopts.c 13KB

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