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

dhcpopts.c 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  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. void *end;
  192. int rc;
  193. /* Check for sufficient space */
  194. if ( new_len > DHCP_MAX_LEN ) {
  195. DBGC ( options, "DHCPOPT %p overlength option\n", options );
  196. return -ENOSPC;
  197. }
  198. new_used_len = ( options->used_len + delta );
  199. /* Expand options block, if necessary */
  200. if ( new_used_len > options->alloc_len ) {
  201. /* Reallocate options block */
  202. old_alloc_len = options->alloc_len;
  203. if ( ( rc = options->realloc ( options, new_used_len ) ) != 0 ){
  204. DBGC ( options, "DHCPOPT %p could not reallocate to "
  205. "%zd bytes\n", options, new_used_len );
  206. return rc;
  207. }
  208. /* Clear newly allocated space */
  209. memset ( ( options->data + old_alloc_len ), 0,
  210. ( options->alloc_len - old_alloc_len ) );
  211. }
  212. /* Update encapsulator, if applicable */
  213. if ( encap_offset >= 0 ) {
  214. encapsulator = dhcp_option ( options, encap_offset );
  215. new_encapsulator_len = ( encapsulator->len + delta );
  216. if ( new_encapsulator_len > DHCP_MAX_LEN ) {
  217. DBGC ( options, "DHCPOPT %p overlength encapsulator\n",
  218. options );
  219. return -ENOSPC;
  220. }
  221. encapsulator->len = new_encapsulator_len;
  222. }
  223. /* Update used length */
  224. options->used_len = new_used_len;
  225. /* Move remainder of option data */
  226. option = dhcp_option ( options, offset );
  227. source = ( ( ( void * ) option ) + old_len );
  228. dest = ( ( ( void * ) option ) + new_len );
  229. end = ( options->data + options->alloc_len );
  230. memmove ( dest, source, ( end - dest ) );
  231. /* Shrink options block, if applicable */
  232. if ( new_used_len < options->alloc_len ) {
  233. if ( ( rc = options->realloc ( options, new_used_len ) ) != 0 ){
  234. DBGC ( options, "DHCPOPT %p could not reallocate to "
  235. "%zd bytes\n", options, new_used_len );
  236. return rc;
  237. }
  238. }
  239. return 0;
  240. }
  241. /**
  242. * Set value of DHCP option
  243. *
  244. * @v options DHCP option block
  245. * @v tag DHCP option tag
  246. * @v data New value for DHCP option
  247. * @v len Length of value, in bytes
  248. * @ret offset Offset of DHCP option, or negative error
  249. *
  250. * Sets the value of a DHCP option within the options block. The
  251. * option may or may not already exist. Encapsulators will be created
  252. * (and deleted) as necessary.
  253. *
  254. * This call may fail due to insufficient space in the options block.
  255. * If it does fail, and the option existed previously, the option will
  256. * be left with its original value.
  257. */
  258. static int set_dhcp_option ( struct dhcp_options *options, unsigned int tag,
  259. const void *data, size_t len ) {
  260. static const uint8_t empty_encap[] = { DHCP_END };
  261. int offset;
  262. int encap_offset = -1;
  263. int creation_offset;
  264. struct dhcp_option *option;
  265. unsigned int encap_tag = DHCP_ENCAPSULATOR ( tag );
  266. size_t old_len = 0;
  267. size_t new_len = ( len ? ( len + DHCP_OPTION_HEADER_LEN ) : 0 );
  268. int rc;
  269. /* Sanity check */
  270. if ( tag == DHCP_PAD )
  271. return -ENOTTY;
  272. creation_offset = find_dhcp_option_with_encap ( options, DHCP_END,
  273. NULL );
  274. if ( creation_offset < 0 )
  275. creation_offset = options->used_len;
  276. /* Find old instance of this option, if any */
  277. offset = find_dhcp_option_with_encap ( options, tag, &encap_offset );
  278. if ( offset >= 0 ) {
  279. old_len = dhcp_option_len ( dhcp_option ( options, offset ) );
  280. DBGC ( options, "DHCPOPT %p resizing %s from %zd to %zd\n",
  281. options, dhcp_tag_name ( tag ), old_len, new_len );
  282. } else {
  283. DBGC ( options, "DHCPOPT %p creating %s (length %zd)\n",
  284. options, dhcp_tag_name ( tag ), new_len );
  285. }
  286. /* Ensure that encapsulator exists, if required */
  287. if ( encap_tag ) {
  288. if ( encap_offset < 0 ) {
  289. encap_offset =
  290. set_dhcp_option ( options, encap_tag,
  291. empty_encap,
  292. sizeof ( empty_encap ) );
  293. }
  294. if ( encap_offset < 0 )
  295. return encap_offset;
  296. creation_offset = ( encap_offset + DHCP_OPTION_HEADER_LEN );
  297. }
  298. /* Create new option if necessary */
  299. if ( offset < 0 )
  300. offset = creation_offset;
  301. /* Resize option to fit new data */
  302. if ( ( rc = resize_dhcp_option ( options, offset, encap_offset,
  303. old_len, new_len ) ) != 0 )
  304. return rc;
  305. /* Copy new data into option, if applicable */
  306. if ( len ) {
  307. option = dhcp_option ( options, offset );
  308. option->tag = tag;
  309. option->len = len;
  310. memcpy ( &option->data, data, len );
  311. }
  312. /* Delete encapsulator if there's nothing else left in it */
  313. if ( encap_offset >= 0 ) {
  314. option = dhcp_option ( options, encap_offset );
  315. if ( option->len <= 1 )
  316. set_dhcp_option ( options, encap_tag, NULL, 0 );
  317. }
  318. return offset;
  319. }
  320. /**
  321. * Check applicability of DHCP option setting
  322. *
  323. * @v tag Setting tag number
  324. * @ret applies Setting applies to this option block
  325. */
  326. int dhcpopt_applies ( unsigned int tag ) {
  327. return ( tag && ( tag <= DHCP_ENCAP_OPT ( DHCP_MAX_OPTION,
  328. DHCP_MAX_OPTION ) ) );
  329. }
  330. /**
  331. * Store value of DHCP option setting
  332. *
  333. * @v options DHCP option block
  334. * @v tag Setting tag number
  335. * @v data Setting data, or NULL to clear setting
  336. * @v len Length of setting data
  337. * @ret rc Return status code
  338. */
  339. int dhcpopt_store ( struct dhcp_options *options, unsigned int tag,
  340. const void *data, size_t len ) {
  341. int offset;
  342. offset = set_dhcp_option ( options, tag, data, len );
  343. if ( offset < 0 )
  344. return offset;
  345. return 0;
  346. }
  347. /**
  348. * Fetch value of DHCP option setting
  349. *
  350. * @v options DHCP option block
  351. * @v tag Setting tag number
  352. * @v data Buffer to fill with setting data
  353. * @v len Length of buffer
  354. * @ret len Length of setting data, or negative error
  355. */
  356. int dhcpopt_fetch ( struct dhcp_options *options, unsigned int tag,
  357. void *data, size_t len ) {
  358. int offset;
  359. struct dhcp_option *option;
  360. size_t option_len;
  361. offset = find_dhcp_option_with_encap ( options, tag, NULL );
  362. if ( offset < 0 )
  363. return offset;
  364. option = dhcp_option ( options, offset );
  365. option_len = option->len;
  366. if ( len > option_len )
  367. len = option_len;
  368. memcpy ( data, option->data, len );
  369. return option_len;
  370. }
  371. /**
  372. * Recalculate length of DHCP options block
  373. *
  374. * @v options Uninitialised DHCP option block
  375. *
  376. * The "used length" field will be updated based on scanning through
  377. * the block to find the end of the options.
  378. */
  379. void dhcpopt_update_used_len ( struct dhcp_options *options ) {
  380. struct dhcp_option *option;
  381. int offset = 0;
  382. ssize_t remaining = options->alloc_len;
  383. unsigned int option_len;
  384. /* Find last non-pad option */
  385. options->used_len = 0;
  386. while ( remaining ) {
  387. option = dhcp_option ( options, offset );
  388. option_len = dhcp_option_len ( option );
  389. remaining -= option_len;
  390. if ( remaining < 0 )
  391. break;
  392. offset += option_len;
  393. if ( option->tag != DHCP_PAD )
  394. options->used_len = offset;
  395. }
  396. }
  397. /**
  398. * Initialise prepopulated block of DHCP options
  399. *
  400. * @v options Uninitialised DHCP option block
  401. * @v data Memory for DHCP option data
  402. * @v alloc_len Length of memory for DHCP option data
  403. * @v realloc DHCP option block reallocator
  404. *
  405. * The memory content must already be filled with valid DHCP options.
  406. * A zeroed block counts as a block of valid DHCP options.
  407. */
  408. void dhcpopt_init ( struct dhcp_options *options, void *data, size_t alloc_len,
  409. int ( * realloc ) ( struct dhcp_options *options,
  410. size_t len ) ) {
  411. /* Fill in fields */
  412. options->data = data;
  413. options->alloc_len = alloc_len;
  414. options->realloc = realloc;
  415. /* Update length */
  416. dhcpopt_update_used_len ( options );
  417. DBGC ( options, "DHCPOPT %p created (data %p lengths %#zx,%#zx)\n",
  418. options, options->data, options->used_len, options->alloc_len );
  419. }