Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

dhcpopts.c 13KB

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