Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  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 encapsulator 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. break;
  130. /* Check for matching tag */
  131. if ( option->tag == tag ) {
  132. DBGC ( options, "DHCPOPT %p found %s (length %d)\n",
  133. options, dhcp_tag_name ( original_tag ),
  134. option_len );
  135. return offset;
  136. }
  137. /* Check for start of matching encapsulation block */
  138. if ( DHCP_IS_ENCAP_OPT ( tag ) &&
  139. ( option->tag == DHCP_ENCAPSULATOR ( tag ) ) ) {
  140. if ( encap_offset )
  141. *encap_offset = offset;
  142. /* Continue search within encapsulated option block */
  143. tag = DHCP_ENCAPSULATED ( tag );
  144. remaining = option_len;
  145. offset += DHCP_OPTION_HEADER_LEN;
  146. continue;
  147. }
  148. offset += option_len;
  149. }
  150. return -ENOENT;
  151. }
  152. /**
  153. * Resize a DHCP option
  154. *
  155. * @v options DHCP option block
  156. * @v offset Offset of option to resize
  157. * @v encap_offset Offset of encapsulating offset (or -ve for none)
  158. * @v old_len Old length (including header)
  159. * @v new_len New length (including header)
  160. * @v can_realloc Can reallocate options data if necessary
  161. * @ret rc Return status code
  162. */
  163. static int resize_dhcp_option ( struct dhcp_options *options,
  164. int offset, int encap_offset,
  165. size_t old_len, size_t new_len,
  166. int can_realloc ) {
  167. struct dhcp_option *encapsulator;
  168. struct dhcp_option *option;
  169. ssize_t delta = ( new_len - old_len );
  170. size_t new_options_len;
  171. size_t new_encapsulator_len;
  172. void *new_data;
  173. void *source;
  174. void *dest;
  175. void *end;
  176. /* Check for sufficient space, and update length fields */
  177. if ( new_len > DHCP_MAX_LEN ) {
  178. DBGC ( options, "DHCPOPT %p overlength option\n", options );
  179. return -ENOSPC;
  180. }
  181. new_options_len = ( options->len + delta );
  182. if ( new_options_len > options->max_len ) {
  183. /* Reallocate options block if allowed to do so. */
  184. if ( can_realloc ) {
  185. new_data = realloc ( options->data, new_options_len );
  186. if ( ! new_data ) {
  187. DBGC ( options, "DHCPOPT %p could not "
  188. "reallocate to %zd bytes\n", options,
  189. new_options_len );
  190. return -ENOMEM;
  191. }
  192. options->data = new_data;
  193. options->max_len = new_options_len;
  194. } else {
  195. DBGC ( options, "DHCPOPT %p out of space\n", options );
  196. return -ENOMEM;
  197. }
  198. }
  199. if ( encap_offset >= 0 ) {
  200. encapsulator = dhcp_option ( options, encap_offset );
  201. new_encapsulator_len = ( encapsulator->len + delta );
  202. if ( new_encapsulator_len > DHCP_MAX_LEN ) {
  203. DBGC ( options, "DHCPOPT %p overlength encapsulator\n",
  204. options );
  205. return -ENOSPC;
  206. }
  207. encapsulator->len = new_encapsulator_len;
  208. }
  209. options->len = new_options_len;
  210. /* Move remainder of option data */
  211. option = dhcp_option ( options, offset );
  212. source = ( ( ( void * ) option ) + old_len );
  213. dest = ( ( ( void * ) option ) + new_len );
  214. end = ( options->data + options->max_len );
  215. memmove ( dest, source, ( end - dest ) );
  216. return 0;
  217. }
  218. /**
  219. * Set value of DHCP option
  220. *
  221. * @v options DHCP option block
  222. * @v tag DHCP option tag
  223. * @v data New value for DHCP option
  224. * @v len Length of value, in bytes
  225. * @v can_realloc Can reallocate options data if necessary
  226. * @ret offset Offset of DHCP option, or negative error
  227. *
  228. * Sets the value of a DHCP option within the options block. The
  229. * option may or may not already exist. Encapsulators will be created
  230. * (and deleted) as necessary.
  231. *
  232. * This call may fail due to insufficient space in the options block.
  233. * If it does fail, and the option existed previously, the option will
  234. * be left with its original value.
  235. */
  236. static int set_dhcp_option ( struct dhcp_options *options, unsigned int tag,
  237. const void *data, size_t len,
  238. int can_realloc ) {
  239. static const uint8_t empty_encapsulator[] = { DHCP_END };
  240. int offset;
  241. int encap_offset = -1;
  242. int creation_offset = 0;
  243. struct dhcp_option *option;
  244. unsigned int encap_tag = DHCP_ENCAPSULATOR ( tag );
  245. size_t old_len = 0;
  246. size_t new_len = ( len ? ( len + DHCP_OPTION_HEADER_LEN ) : 0 );
  247. int rc;
  248. /* Sanity check */
  249. if ( tag == DHCP_PAD )
  250. return -ENOTTY;
  251. /* Find old instance of this option, if any */
  252. offset = find_dhcp_option_with_encap ( options, tag, &encap_offset );
  253. if ( offset >= 0 ) {
  254. old_len = dhcp_option_len ( dhcp_option ( options, offset ) );
  255. DBGC ( options, "DHCPOPT %p resizing %s from %zd to %zd\n",
  256. options, dhcp_tag_name ( tag ), old_len, new_len );
  257. } else {
  258. DBGC ( options, "DHCPOPT %p creating %s (length %zd)\n",
  259. options, dhcp_tag_name ( tag ), new_len );
  260. }
  261. /* Ensure that encapsulator exists, if required */
  262. if ( encap_tag ) {
  263. if ( encap_offset < 0 )
  264. encap_offset = set_dhcp_option ( options, encap_tag,
  265. empty_encapsulator, 1,
  266. can_realloc );
  267. if ( encap_offset < 0 )
  268. return encap_offset;
  269. creation_offset = ( encap_offset + DHCP_OPTION_HEADER_LEN );
  270. }
  271. /* Create new option if necessary */
  272. if ( offset < 0 )
  273. offset = creation_offset;
  274. /* Resize option to fit new data */
  275. if ( ( rc = resize_dhcp_option ( options, offset, encap_offset,
  276. old_len, new_len,
  277. can_realloc ) ) != 0 )
  278. return rc;
  279. /* Copy new data into option, if applicable */
  280. if ( len ) {
  281. option = dhcp_option ( options, offset );
  282. option->tag = tag;
  283. option->len = len;
  284. memcpy ( &option->data, data, len );
  285. }
  286. /* Delete encapsulator if there's nothing else left in it */
  287. if ( encap_offset >= 0 ) {
  288. option = dhcp_option ( options, encap_offset );
  289. if ( option->len <= 1 )
  290. set_dhcp_option ( options, encap_tag, NULL, 0, 0 );
  291. }
  292. return offset;
  293. }
  294. /**
  295. * Store value of DHCP option setting
  296. *
  297. * @v options DHCP option block
  298. * @v tag Setting tag number
  299. * @v data Setting data, or NULL to clear setting
  300. * @v len Length of setting data
  301. * @ret rc Return status code
  302. */
  303. int dhcpopt_store ( struct dhcp_options *options, unsigned int tag,
  304. const void *data, size_t len ) {
  305. int offset;
  306. offset = set_dhcp_option ( options, tag, data, len, 0 );
  307. if ( offset < 0 )
  308. return offset;
  309. return 0;
  310. }
  311. /**
  312. * Store value of DHCP option setting, extending options block if necessary
  313. *
  314. * @v options DHCP option block
  315. * @v tag Setting tag number
  316. * @v data Setting data, or NULL to clear setting
  317. * @v len Length of setting data
  318. * @ret rc Return status code
  319. */
  320. int dhcpopt_extensible_store ( struct dhcp_options *options, unsigned int tag,
  321. const void *data, size_t len ) {
  322. int offset;
  323. offset = set_dhcp_option ( options, tag, data, len, 1 );
  324. if ( offset < 0 )
  325. return offset;
  326. return 0;
  327. }
  328. /**
  329. * Fetch value of DHCP option setting
  330. *
  331. * @v options DHCP option block
  332. * @v tag Setting tag number
  333. * @v data Buffer to fill with setting data
  334. * @v len Length of buffer
  335. * @ret len Length of setting data, or negative error
  336. */
  337. int dhcpopt_fetch ( struct dhcp_options *options, unsigned int tag,
  338. void *data, size_t len ) {
  339. int offset;
  340. struct dhcp_option *option;
  341. size_t option_len;
  342. offset = find_dhcp_option_with_encap ( options, tag, NULL );
  343. if ( offset < 0 )
  344. return offset;
  345. option = dhcp_option ( options, offset );
  346. option_len = option->len;
  347. if ( len > option_len )
  348. len = option_len;
  349. memcpy ( data, option->data, len );
  350. return option_len;
  351. }
  352. /**
  353. * Recalculate length of DHCP options block
  354. *
  355. * @v options Uninitialised DHCP option block
  356. *
  357. * The "used length" field will be updated based on scanning through
  358. * the block to find the end of the options.
  359. */
  360. static void dhcpopt_update_len ( struct dhcp_options *options ) {
  361. struct dhcp_option *option;
  362. int offset = 0;
  363. ssize_t remaining = options->max_len;
  364. unsigned int option_len;
  365. /* Find last non-pad option */
  366. options->len = 0;
  367. while ( remaining ) {
  368. option = dhcp_option ( options, offset );
  369. option_len = dhcp_option_len ( option );
  370. remaining -= option_len;
  371. if ( remaining < 0 )
  372. break;
  373. offset += option_len;
  374. if ( option->tag != DHCP_PAD )
  375. options->len = offset;
  376. }
  377. }
  378. /**
  379. * Initialise prepopulated block of DHCP options
  380. *
  381. * @v options Uninitialised DHCP option block
  382. * @v data Memory for DHCP option data
  383. * @v max_len Length of memory for DHCP option data
  384. *
  385. * The memory content must already be filled with valid DHCP options.
  386. * A zeroed block counts as a block of valid DHCP options.
  387. */
  388. void dhcpopt_init ( struct dhcp_options *options, void *data,
  389. size_t max_len ) {
  390. /* Fill in fields */
  391. options->data = data;
  392. options->max_len = max_len;
  393. /* Update length */
  394. dhcpopt_update_len ( options );
  395. DBGC ( options, "DHCPOPT %p created (data %p len %#zx max_len %#zx)\n",
  396. options, options->data, options->len, options->max_len );
  397. }