Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059
  1. /*
  2. * Copyright (C) 2006 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 <string.h>
  19. #include <stdlib.h>
  20. #include <stdio.h>
  21. #include <errno.h>
  22. #include <assert.h>
  23. #include <byteswap.h>
  24. #include <gpxe/if_ether.h>
  25. #include <gpxe/netdevice.h>
  26. #include <gpxe/device.h>
  27. #include <gpxe/xfer.h>
  28. #include <gpxe/open.h>
  29. #include <gpxe/job.h>
  30. #include <gpxe/retry.h>
  31. #include <gpxe/tcpip.h>
  32. #include <gpxe/ip.h>
  33. #include <gpxe/uuid.h>
  34. #include <gpxe/dhcp.h>
  35. /** @file
  36. *
  37. * Dynamic Host Configuration Protocol
  38. *
  39. */
  40. /** DHCP operation types
  41. *
  42. * This table maps from DHCP message types (i.e. values of the @c
  43. * DHCP_MESSAGE_TYPE option) to values of the "op" field within a DHCP
  44. * packet.
  45. */
  46. static const uint8_t dhcp_op[] = {
  47. [DHCPDISCOVER] = BOOTP_REQUEST,
  48. [DHCPOFFER] = BOOTP_REPLY,
  49. [DHCPREQUEST] = BOOTP_REQUEST,
  50. [DHCPDECLINE] = BOOTP_REQUEST,
  51. [DHCPACK] = BOOTP_REPLY,
  52. [DHCPNAK] = BOOTP_REPLY,
  53. [DHCPRELEASE] = BOOTP_REQUEST,
  54. [DHCPINFORM] = BOOTP_REQUEST,
  55. };
  56. /** Raw option data for options common to all DHCP requests */
  57. static uint8_t dhcp_request_options_data[] = {
  58. DHCP_MAX_MESSAGE_SIZE, DHCP_WORD ( ETH_MAX_MTU ),
  59. DHCP_VENDOR_CLASS_ID,
  60. DHCP_STRING ( 'P', 'X', 'E', 'C', 'l', 'i', 'e', 'n', 't', ':',
  61. 'A', 'r', 'c', 'h', ':', '0', '0', '0', '0', '0', ':',
  62. 'U', 'N', 'D', 'I', ':', '0', '0', '2', '0', '0', '1' ),
  63. DHCP_CLIENT_ARCHITECTURE, DHCP_WORD ( 0 ),
  64. DHCP_CLIENT_NDI, DHCP_OPTION ( 1 /* UNDI */ , 2, 1 /* v2.1 */ ),
  65. DHCP_PARAMETER_REQUEST_LIST,
  66. DHCP_OPTION ( DHCP_SUBNET_MASK, DHCP_ROUTERS, DHCP_DNS_SERVERS,
  67. DHCP_LOG_SERVERS, DHCP_HOST_NAME, DHCP_DOMAIN_NAME,
  68. DHCP_ROOT_PATH, DHCP_VENDOR_ENCAP, DHCP_VENDOR_CLASS_ID,
  69. DHCP_TFTP_SERVER_NAME, DHCP_BOOTFILE_NAME,
  70. DHCP_EB_ENCAP, DHCP_ISCSI_INITIATOR_IQN ),
  71. DHCP_END
  72. };
  73. /** DHCP feature codes */
  74. static uint8_t dhcp_features[0] __table_start ( uint8_t, dhcp_features );
  75. static uint8_t dhcp_features_end[0] __table_end ( uint8_t, dhcp_features );
  76. /**
  77. * Name a DHCP packet type
  78. *
  79. * @v msgtype DHCP message type
  80. * @ret string DHCP mesasge type name
  81. */
  82. static inline const char * dhcp_msgtype_name ( unsigned int msgtype ) {
  83. switch ( msgtype ) {
  84. case 0: return "BOOTP"; /* Non-DHCP packet */
  85. case DHCPDISCOVER: return "DHCPDISCOVER";
  86. case DHCPOFFER: return "DHCPOFFER";
  87. case DHCPREQUEST: return "DHCPREQUEST";
  88. case DHCPDECLINE: return "DHCPDECLINE";
  89. case DHCPACK: return "DHCPACK";
  90. case DHCPNAK: return "DHCPNAK";
  91. case DHCPRELEASE: return "DHCPRELEASE";
  92. case DHCPINFORM: return "DHCPINFORM";
  93. default: return "DHCP<invalid>";
  94. }
  95. }
  96. /**
  97. * Calculate DHCP transaction ID for a network device
  98. *
  99. * @v netdev Network device
  100. * @ret xid DHCP XID
  101. *
  102. * Extract the least significant bits of the hardware address for use
  103. * as the transaction ID.
  104. */
  105. static uint32_t dhcp_xid ( struct net_device *netdev ) {
  106. uint32_t xid;
  107. memcpy ( &xid, ( netdev->ll_addr + netdev->ll_protocol->ll_addr_len
  108. - sizeof ( xid ) ), sizeof ( xid ) );
  109. return xid;
  110. }
  111. /** Options common to all DHCP requests */
  112. static struct dhcp_option_block dhcp_request_options = {
  113. .data = dhcp_request_options_data,
  114. .max_len = sizeof ( dhcp_request_options_data ),
  115. .len = sizeof ( dhcp_request_options_data ),
  116. };
  117. /**
  118. * Set option within DHCP packet
  119. *
  120. * @v dhcppkt DHCP packet
  121. * @v tag DHCP option tag
  122. * @v data New value for DHCP option
  123. * @v len Length of value, in bytes
  124. * @ret rc Return status code
  125. *
  126. * Sets the option within the first available options block within the
  127. * DHCP packet. Option blocks are tried in the order specified by @c
  128. * dhcp_option_block_fill_order.
  129. *
  130. * The magic options @c DHCP_EB_YIADDR and @c DHCP_EB_SIADDR are
  131. * intercepted and inserted into the appropriate fixed fields within
  132. * the DHCP packet. The option @c DHCP_OPTION_OVERLOAD is silently
  133. * ignored, since our DHCP packet assembly method relies on always
  134. * having option overloading in use.
  135. */
  136. static int set_dhcp_packet_option ( struct dhcp_packet *dhcppkt,
  137. unsigned int tag, const void *data,
  138. size_t len ) {
  139. struct dhcphdr *dhcphdr = dhcppkt->dhcphdr;
  140. struct dhcp_option_block *options = &dhcppkt->options;
  141. struct dhcp_option *option = NULL;
  142. /* Special-case the magic options */
  143. switch ( tag ) {
  144. case DHCP_OPTION_OVERLOAD:
  145. /* Hard-coded in packets we create; always ignore */
  146. return 0;
  147. case DHCP_EB_YIADDR:
  148. memcpy ( &dhcphdr->yiaddr, data, sizeof ( dhcphdr->yiaddr ) );
  149. return 0;
  150. case DHCP_EB_SIADDR:
  151. memcpy ( &dhcphdr->siaddr, data, sizeof ( dhcphdr->siaddr ) );
  152. return 0;
  153. case DHCP_TFTP_SERVER_NAME:
  154. memset ( dhcphdr->sname, 0, sizeof ( dhcphdr->sname ) );
  155. if ( len > sizeof ( dhcphdr->sname ) )
  156. len = sizeof ( dhcphdr->sname );
  157. memcpy ( dhcphdr->sname, data, len );
  158. return 0;
  159. case DHCP_BOOTFILE_NAME:
  160. memset ( dhcphdr->file, 0, sizeof ( dhcphdr->file ) );
  161. if ( len > sizeof ( dhcphdr->file ) )
  162. len = sizeof ( dhcphdr->file );
  163. memcpy ( dhcphdr->file, data, len );
  164. return 0;
  165. default:
  166. /* Continue processing as normal */
  167. break;
  168. }
  169. /* Set option */
  170. option = set_dhcp_option ( options, tag, data, len );
  171. /* Update DHCP packet length */
  172. dhcppkt->len = ( offsetof ( typeof ( *dhcppkt->dhcphdr ), options )
  173. + dhcppkt->options.len );
  174. return ( option ? 0 : -ENOSPC );
  175. }
  176. /**
  177. * Copy option into DHCP packet
  178. *
  179. * @v dhcppkt DHCP packet
  180. * @v options DHCP option block, or NULL
  181. * @v tag DHCP option tag to search for
  182. * @v new_tag DHCP option tag to use for copied option
  183. * @ret rc Return status code
  184. *
  185. * Copies a single option, if present, from the DHCP options block
  186. * into a DHCP packet. The tag for the option may be changed if
  187. * desired; this is required by other parts of the DHCP code.
  188. *
  189. * @c options may specify a single options block, or be left as NULL
  190. * in order to search for the option within all registered options
  191. * blocks.
  192. */
  193. static int copy_dhcp_packet_option ( struct dhcp_packet *dhcppkt,
  194. struct dhcp_option_block *options,
  195. unsigned int tag, unsigned int new_tag ) {
  196. struct dhcp_option *option;
  197. int rc;
  198. option = find_dhcp_option ( options, tag );
  199. if ( option ) {
  200. if ( ( rc = set_dhcp_packet_option ( dhcppkt, new_tag,
  201. &option->data,
  202. option->len ) ) != 0 )
  203. return rc;
  204. }
  205. return 0;
  206. }
  207. /**
  208. * Copy options into DHCP packet
  209. *
  210. * @v dhcppkt DHCP packet
  211. * @v options DHCP option block, or NULL
  212. * @v encapsulator Encapsulating option, or zero
  213. * @ret rc Return status code
  214. *
  215. * Copies options with the specified encapsulator from DHCP options
  216. * blocks into a DHCP packet. Most options are copied verbatim.
  217. * Recognised encapsulated options fields are handled as such.
  218. *
  219. * @c options may specify a single options block, or be left as NULL
  220. * in order to copy options from all registered options blocks.
  221. */
  222. static int copy_dhcp_packet_encap_options ( struct dhcp_packet *dhcppkt,
  223. struct dhcp_option_block *options,
  224. unsigned int encapsulator ) {
  225. unsigned int subtag;
  226. unsigned int tag;
  227. int rc;
  228. for ( subtag = DHCP_MIN_OPTION; subtag <= DHCP_MAX_OPTION; subtag++ ) {
  229. tag = DHCP_ENCAP_OPT ( encapsulator, subtag );
  230. switch ( tag ) {
  231. case DHCP_EB_ENCAP:
  232. case DHCP_VENDOR_ENCAP:
  233. /* Process encapsulated options field */
  234. if ( ( rc = copy_dhcp_packet_encap_options ( dhcppkt,
  235. options,
  236. tag)) !=0)
  237. return rc;
  238. break;
  239. default:
  240. /* Copy option to reassembled packet */
  241. if ( ( rc = copy_dhcp_packet_option ( dhcppkt, options,
  242. tag, tag ) ) !=0)
  243. return rc;
  244. break;
  245. };
  246. }
  247. return 0;
  248. }
  249. /**
  250. * Copy options into DHCP packet
  251. *
  252. * @v dhcppkt DHCP packet
  253. * @v options DHCP option block, or NULL
  254. * @ret rc Return status code
  255. *
  256. * Copies options from DHCP options blocks into a DHCP packet. Most
  257. * options are copied verbatim. Recognised encapsulated options
  258. * fields are handled as such.
  259. *
  260. * @c options may specify a single options block, or be left as NULL
  261. * in order to copy options from all registered options blocks.
  262. */
  263. static int copy_dhcp_packet_options ( struct dhcp_packet *dhcppkt,
  264. struct dhcp_option_block *options ) {
  265. return copy_dhcp_packet_encap_options ( dhcppkt, options, 0 );
  266. }
  267. /**
  268. * Create a DHCP packet
  269. *
  270. * @v netdev Network device
  271. * @v msgtype DHCP message type
  272. * @v data Buffer for DHCP packet
  273. * @v max_len Size of DHCP packet buffer
  274. * @v dhcppkt DHCP packet structure to fill in
  275. * @ret rc Return status code
  276. *
  277. * Creates a DHCP packet in the specified buffer, and fills out a @c
  278. * dhcp_packet structure that can be passed to
  279. * set_dhcp_packet_option() or copy_dhcp_packet_options().
  280. */
  281. static int create_dhcp_packet ( struct net_device *netdev, uint8_t msgtype,
  282. void *data, size_t max_len,
  283. struct dhcp_packet *dhcppkt ) {
  284. struct dhcphdr *dhcphdr = data;
  285. unsigned int hlen;
  286. int rc;
  287. /* Sanity check */
  288. if ( max_len < sizeof ( *dhcphdr ) )
  289. return -ENOSPC;
  290. /* Initialise DHCP packet content */
  291. memset ( dhcphdr, 0, max_len );
  292. dhcphdr->xid = dhcp_xid ( netdev );
  293. dhcphdr->magic = htonl ( DHCP_MAGIC_COOKIE );
  294. dhcphdr->htype = ntohs ( netdev->ll_protocol->ll_proto );
  295. dhcphdr->op = dhcp_op[msgtype];
  296. /* If hardware length exceeds the chaddr field length, don't
  297. * use the chaddr field. This is as per RFC4390.
  298. */
  299. hlen = netdev->ll_protocol->ll_addr_len;
  300. if ( hlen > sizeof ( dhcphdr->chaddr ) ) {
  301. hlen = 0;
  302. dhcphdr->flags = htons ( BOOTP_FL_BROADCAST );
  303. }
  304. dhcphdr->hlen = hlen;
  305. memcpy ( dhcphdr->chaddr, netdev->ll_addr, hlen );
  306. /* Initialise DHCP packet structure */
  307. dhcppkt->dhcphdr = dhcphdr;
  308. dhcppkt->max_len = max_len;
  309. init_dhcp_options ( &dhcppkt->options, dhcphdr->options,
  310. ( max_len -
  311. offsetof ( typeof ( *dhcphdr ), options ) ) );
  312. /* Set DHCP_MESSAGE_TYPE option */
  313. if ( ( rc = set_dhcp_packet_option ( dhcppkt, DHCP_MESSAGE_TYPE,
  314. &msgtype,
  315. sizeof ( msgtype ) ) ) != 0 )
  316. return rc;
  317. return 0;
  318. }
  319. /**
  320. * Calculate used length of a field containing DHCP options
  321. *
  322. * @v data Field containing DHCP options
  323. * @v max_len Field length
  324. * @ret len Used length (excluding the @c DHCP_END tag)
  325. */
  326. static size_t dhcp_field_len ( const void *data, size_t max_len ) {
  327. struct dhcp_option_block options;
  328. struct dhcp_option *end;
  329. options.data = ( ( void * ) data );
  330. options.len = max_len;
  331. end = find_dhcp_option ( &options, DHCP_END );
  332. return ( end ? ( ( ( void * ) end ) - data ) : 0 );
  333. }
  334. /**
  335. * Merge field containing DHCP options or string into DHCP options block
  336. *
  337. * @v options DHCP option block
  338. * @v data Field containing DHCP options
  339. * @v max_len Field length
  340. * @v tag DHCP option tag, or 0
  341. *
  342. * If @c tag is non-zero, the field will be treated as a
  343. * NUL-terminated string representing the value of the specified DHCP
  344. * option. If @c tag is zero, the field will be treated as a block of
  345. * DHCP options, and simply appended to the existing options in the
  346. * option block.
  347. *
  348. * The caller must ensure that there is enough space in the options
  349. * block to perform the merge.
  350. */
  351. static void merge_dhcp_field ( struct dhcp_option_block *options,
  352. const void *data, size_t max_len,
  353. unsigned int tag ) {
  354. size_t len;
  355. void *dest;
  356. struct dhcp_option *end;
  357. if ( tag ) {
  358. set_dhcp_option ( options, tag, data, strlen ( data ) );
  359. } else {
  360. len = dhcp_field_len ( data, max_len );
  361. dest = ( options->data + options->len - 1 );
  362. memcpy ( dest, data, len );
  363. options->len += len;
  364. end = ( dest + len );
  365. end->tag = DHCP_END;
  366. }
  367. }
  368. /**
  369. * Parse DHCP packet and construct DHCP options block
  370. *
  371. * @v dhcphdr DHCP packet
  372. * @v len Length of DHCP packet
  373. * @ret options DHCP options block, or NULL
  374. *
  375. * Parses a received DHCP packet and canonicalises its contents into a
  376. * single DHCP options block. The "file" and "sname" fields are
  377. * converted into the corresponding DHCP options (@c
  378. * DHCP_BOOTFILE_NAME and @c DHCP_TFTP_SERVER_NAME respectively). If
  379. * these fields are used for option overloading, their options are
  380. * merged in to the options block.
  381. *
  382. * The values of the "yiaddr" and "siaddr" fields will be stored
  383. * within the options block as the magic options @c DHCP_EB_YIADDR and
  384. * @c DHCP_EB_SIADDR.
  385. *
  386. * Note that this call allocates new memory for the constructed DHCP
  387. * options block; it is the responsibility of the caller to eventually
  388. * free this memory.
  389. */
  390. static struct dhcp_option_block * dhcp_parse ( const struct dhcphdr *dhcphdr,
  391. size_t len ) {
  392. struct dhcp_option_block *options;
  393. size_t options_len;
  394. unsigned int overloading;
  395. /* Sanity check */
  396. if ( len < sizeof ( *dhcphdr ) )
  397. return NULL;
  398. /* Calculate size of resulting concatenated option block:
  399. *
  400. * The "options" field : length of the field minus the DHCP_END tag.
  401. *
  402. * The "file" field : maximum length of the field minus the
  403. * NUL terminator, plus a 2-byte DHCP header or, if used for
  404. * option overloading, the length of the field minus the
  405. * DHCP_END tag.
  406. *
  407. * The "sname" field : as for the "file" field.
  408. *
  409. * 15 bytes for an encapsulated options field to contain the
  410. * value of the "yiaddr" and "siaddr" fields
  411. *
  412. * 1 byte for a final terminating DHCP_END tag.
  413. */
  414. options_len = ( ( len - offsetof ( typeof ( *dhcphdr ), options ) ) - 1
  415. + ( sizeof ( dhcphdr->file ) + 1 )
  416. + ( sizeof ( dhcphdr->sname ) + 1 )
  417. + 15 /* yiaddr and siaddr */
  418. + 1 /* DHCP_END tag */ );
  419. /* Allocate empty options block of required size */
  420. options = alloc_dhcp_options ( options_len );
  421. if ( ! options ) {
  422. DBG ( "DHCP could not allocate %d-byte option block\n",
  423. options_len );
  424. return NULL;
  425. }
  426. /* Merge in "options" field, if this is a DHCP packet */
  427. if ( dhcphdr->magic == htonl ( DHCP_MAGIC_COOKIE ) ) {
  428. merge_dhcp_field ( options, dhcphdr->options,
  429. ( len -
  430. offsetof ( typeof (*dhcphdr), options ) ),
  431. 0 /* Always contains options */ );
  432. }
  433. /* Identify overloaded fields */
  434. overloading = find_dhcp_num_option ( options, DHCP_OPTION_OVERLOAD );
  435. /* Merge in "file" and "sname" fields */
  436. merge_dhcp_field ( options, dhcphdr->file, sizeof ( dhcphdr->file ),
  437. ( ( overloading & DHCP_OPTION_OVERLOAD_FILE ) ?
  438. 0 : DHCP_BOOTFILE_NAME ) );
  439. merge_dhcp_field ( options, dhcphdr->sname, sizeof ( dhcphdr->sname ),
  440. ( ( overloading & DHCP_OPTION_OVERLOAD_SNAME ) ?
  441. 0 : DHCP_TFTP_SERVER_NAME ) );
  442. /* Set magic options for "yiaddr" and "siaddr", if present */
  443. if ( dhcphdr->yiaddr.s_addr ) {
  444. set_dhcp_option ( options, DHCP_EB_YIADDR,
  445. &dhcphdr->yiaddr, sizeof (dhcphdr->yiaddr) );
  446. }
  447. if ( dhcphdr->siaddr.s_addr ) {
  448. set_dhcp_option ( options, DHCP_EB_SIADDR,
  449. &dhcphdr->siaddr, sizeof (dhcphdr->siaddr) );
  450. }
  451. assert ( options->len <= options->max_len );
  452. return options;
  453. }
  454. /****************************************************************************
  455. *
  456. * Whole-packet construction
  457. *
  458. */
  459. /** DHCP network device descriptor */
  460. struct dhcp_netdev_desc {
  461. /** Bus type ID */
  462. uint8_t type;
  463. /** Vendor ID */
  464. uint16_t vendor;
  465. /** Device ID */
  466. uint16_t device;
  467. } __attribute__ (( packed ));
  468. /** DHCP client identifier */
  469. struct dhcp_client_id {
  470. /** Link-layer protocol */
  471. uint8_t ll_proto;
  472. /** Link-layer address */
  473. uint8_t ll_addr[MAX_LL_ADDR_LEN];
  474. } __attribute__ (( packed ));
  475. /** DHCP client UUID */
  476. struct dhcp_client_uuid {
  477. /** Identifier type */
  478. uint8_t type;
  479. /** UUID */
  480. union uuid uuid;
  481. } __attribute__ (( packed ));
  482. #define DHCP_CLIENT_UUID_TYPE 0
  483. /**
  484. * Create DHCP request
  485. *
  486. * @v netdev Network device
  487. * @v msgtype DHCP message type
  488. * @v options DHCP server response options, or NULL
  489. * @v data Buffer for DHCP packet
  490. * @v max_len Size of DHCP packet buffer
  491. * @v dhcppkt DHCP packet structure to fill in
  492. * @ret rc Return status code
  493. */
  494. int create_dhcp_request ( struct net_device *netdev, int msgtype,
  495. struct dhcp_option_block *options,
  496. void *data, size_t max_len,
  497. struct dhcp_packet *dhcppkt ) {
  498. struct device_description *desc = &netdev->dev->desc;
  499. struct dhcp_netdev_desc dhcp_desc;
  500. struct dhcp_client_id client_id;
  501. struct dhcp_client_uuid client_uuid;
  502. size_t dhcp_features_len;
  503. size_t ll_addr_len;
  504. int rc;
  505. /* Create DHCP packet */
  506. if ( ( rc = create_dhcp_packet ( netdev, msgtype, data, max_len,
  507. dhcppkt ) ) != 0 ) {
  508. DBG ( "DHCP could not create DHCP packet: %s\n",
  509. strerror ( rc ) );
  510. return rc;
  511. }
  512. /* Copy in options common to all requests */
  513. if ( ( rc = copy_dhcp_packet_options ( dhcppkt,
  514. &dhcp_request_options )) !=0 ){
  515. DBG ( "DHCP could not set common DHCP options: %s\n",
  516. strerror ( rc ) );
  517. return rc;
  518. }
  519. /* Copy any required options from previous server repsonse */
  520. if ( options ) {
  521. if ( ( rc = copy_dhcp_packet_option ( dhcppkt, options,
  522. DHCP_SERVER_IDENTIFIER,
  523. DHCP_SERVER_IDENTIFIER ) ) != 0 ) {
  524. DBG ( "DHCP could not set server identifier "
  525. "option: %s\n", strerror ( rc ) );
  526. return rc;
  527. }
  528. if ( ( rc = copy_dhcp_packet_option ( dhcppkt, options,
  529. DHCP_EB_YIADDR,
  530. DHCP_REQUESTED_ADDRESS ) ) != 0 ) {
  531. DBG ( "DHCP could not set requested address "
  532. "option: %s\n", strerror ( rc ) );
  533. return rc;
  534. }
  535. }
  536. /* Add options to identify the feature list */
  537. dhcp_features_len = ( dhcp_features_end - dhcp_features );
  538. if ( ( rc = set_dhcp_packet_option ( dhcppkt, DHCP_EB_ENCAP,
  539. dhcp_features,
  540. dhcp_features_len ) ) != 0 ) {
  541. DBG ( "DHCP could not set features list option: %s\n",
  542. strerror ( rc ) );
  543. return rc;
  544. }
  545. /* Add options to identify the network device */
  546. dhcp_desc.type = desc->bus_type;
  547. dhcp_desc.vendor = htons ( desc->vendor );
  548. dhcp_desc.device = htons ( desc->device );
  549. if ( ( rc = set_dhcp_packet_option ( dhcppkt, DHCP_EB_BUS_ID,
  550. &dhcp_desc,
  551. sizeof ( dhcp_desc ) ) ) != 0 ) {
  552. DBG ( "DHCP could not set bus ID option: %s\n",
  553. strerror ( rc ) );
  554. return rc;
  555. }
  556. /* Add DHCP client identifier. Required for Infiniband, and
  557. * doesn't hurt other link layers.
  558. */
  559. client_id.ll_proto = netdev->ll_protocol->ll_proto;
  560. ll_addr_len = netdev->ll_protocol->ll_addr_len;
  561. assert ( ll_addr_len <= sizeof ( client_id.ll_addr ) );
  562. memcpy ( client_id.ll_addr, netdev->ll_addr, ll_addr_len );
  563. if ( ( rc = set_dhcp_packet_option ( dhcppkt, DHCP_CLIENT_ID,
  564. &client_id,
  565. ( ll_addr_len + 1 ) ) ) != 0 ) {
  566. DBG ( "DHCP could not set client ID: %s\n",
  567. strerror ( rc ) );
  568. return rc;
  569. }
  570. /* Add client UUID, if we have one. Required for PXE. */
  571. client_uuid.type = DHCP_CLIENT_UUID_TYPE;
  572. if ( ( rc = get_uuid ( &client_uuid.uuid ) ) == 0 ) {
  573. if ( ( rc = set_dhcp_packet_option ( dhcppkt,
  574. DHCP_CLIENT_UUID, &client_uuid,
  575. sizeof ( client_uuid ) ) ) != 0 ) {
  576. DBG ( "DHCP could not set client UUID: %s\n",
  577. strerror ( rc ) );
  578. return rc;
  579. }
  580. }
  581. return 0;
  582. }
  583. /**
  584. * Create DHCP response
  585. *
  586. * @v netdev Network device
  587. * @v msgtype DHCP message type
  588. * @v options DHCP options, or NULL
  589. * @v data Buffer for DHCP packet
  590. * @v max_len Size of DHCP packet buffer
  591. * @v dhcppkt DHCP packet structure to fill in
  592. * @ret rc Return status code
  593. */
  594. int create_dhcp_response ( struct net_device *netdev, int msgtype,
  595. struct dhcp_option_block *options,
  596. void *data, size_t max_len,
  597. struct dhcp_packet *dhcppkt ) {
  598. int rc;
  599. /* Create packet and copy in options */
  600. if ( ( rc = create_dhcp_packet ( netdev, msgtype, data, max_len,
  601. dhcppkt ) ) != 0 ) {
  602. DBG ( " failed to build packet" );
  603. return rc;
  604. }
  605. if ( ( rc = copy_dhcp_packet_options ( dhcppkt, options ) ) != 0 ) {
  606. DBG ( " failed to copy options" );
  607. return rc;
  608. }
  609. return 0;
  610. }
  611. /****************************************************************************
  612. *
  613. * DHCP to UDP interface
  614. *
  615. */
  616. /** A DHCP session */
  617. struct dhcp_session {
  618. /** Reference counter */
  619. struct refcnt refcnt;
  620. /** Job control interface */
  621. struct job_interface job;
  622. /** Data transfer interface */
  623. struct xfer_interface xfer;
  624. /** Network device being configured */
  625. struct net_device *netdev;
  626. /** Option block registration routine */
  627. int ( * register_options ) ( struct net_device *netdev,
  628. struct dhcp_option_block *options );
  629. /** State of the session
  630. *
  631. * This is a value for the @c DHCP_MESSAGE_TYPE option
  632. * (e.g. @c DHCPDISCOVER).
  633. */
  634. int state;
  635. /** Options obtained from DHCP server */
  636. struct dhcp_option_block *options;
  637. /** Options obtained from ProxyDHCP server */
  638. struct dhcp_option_block *proxy_options;
  639. /** Retransmission timer */
  640. struct retry_timer timer;
  641. /** Session start time (in ticks) */
  642. unsigned long start;
  643. };
  644. /**
  645. * Free DHCP session
  646. *
  647. * @v refcnt Reference counter
  648. */
  649. static void dhcp_free ( struct refcnt *refcnt ) {
  650. struct dhcp_session *dhcp =
  651. container_of ( refcnt, struct dhcp_session, refcnt );
  652. netdev_put ( dhcp->netdev );
  653. dhcpopt_put ( dhcp->options );
  654. dhcpopt_put ( dhcp->proxy_options );
  655. free ( dhcp );
  656. }
  657. /**
  658. * Mark DHCP session as complete
  659. *
  660. * @v dhcp DHCP session
  661. * @v rc Return status code
  662. */
  663. static void dhcp_finished ( struct dhcp_session *dhcp, int rc ) {
  664. /* Block futher incoming messages */
  665. job_nullify ( &dhcp->job );
  666. xfer_nullify ( &dhcp->xfer );
  667. /* Stop retry timer */
  668. stop_timer ( &dhcp->timer );
  669. /* Free resources and close interfaces */
  670. xfer_close ( &dhcp->xfer, rc );
  671. job_done ( &dhcp->job, rc );
  672. }
  673. /****************************************************************************
  674. *
  675. * Data transfer interface
  676. *
  677. */
  678. /**
  679. * Transmit DHCP request
  680. *
  681. * @v dhcp DHCP session
  682. * @ret rc Return status code
  683. */
  684. static int dhcp_send_request ( struct dhcp_session *dhcp ) {
  685. struct xfer_metadata meta = {
  686. .netdev = dhcp->netdev,
  687. };
  688. struct io_buffer *iobuf;
  689. struct dhcp_packet dhcppkt;
  690. int rc;
  691. DBGC ( dhcp, "DHCP %p transmitting %s\n",
  692. dhcp, dhcp_msgtype_name ( dhcp->state ) );
  693. assert ( ( dhcp->state == DHCPDISCOVER ) ||
  694. ( dhcp->state == DHCPREQUEST ) );
  695. /* Start retry timer. Do this first so that failures to
  696. * transmit will be retried.
  697. */
  698. start_timer ( &dhcp->timer );
  699. /* Allocate buffer for packet */
  700. iobuf = xfer_alloc_iob ( &dhcp->xfer, DHCP_MIN_LEN );
  701. if ( ! iobuf )
  702. return -ENOMEM;
  703. /* Create DHCP packet in temporary buffer */
  704. if ( ( rc = create_dhcp_request ( dhcp->netdev, dhcp->state,
  705. dhcp->options, iobuf->data,
  706. iob_tailroom ( iobuf ),
  707. &dhcppkt ) ) != 0 ) {
  708. DBGC ( dhcp, "DHCP %p could not construct DHCP request: %s\n",
  709. dhcp, strerror ( rc ) );
  710. goto done;
  711. }
  712. /* Transmit the packet */
  713. iob_put ( iobuf, dhcppkt.len );
  714. rc = xfer_deliver_iob_meta ( &dhcp->xfer, iobuf, &meta );
  715. iobuf = NULL;
  716. if ( rc != 0 ) {
  717. DBGC ( dhcp, "DHCP %p could not transmit UDP packet: %s\n",
  718. dhcp, strerror ( rc ) );
  719. goto done;
  720. }
  721. done:
  722. free_iob ( iobuf );
  723. return rc;
  724. }
  725. /**
  726. * Handle DHCP retry timer expiry
  727. *
  728. * @v timer DHCP retry timer
  729. * @v fail Failure indicator
  730. */
  731. static void dhcp_timer_expired ( struct retry_timer *timer, int fail ) {
  732. struct dhcp_session *dhcp =
  733. container_of ( timer, struct dhcp_session, timer );
  734. if ( fail ) {
  735. dhcp_finished ( dhcp, -ETIMEDOUT );
  736. } else {
  737. dhcp_send_request ( dhcp );
  738. }
  739. }
  740. /**
  741. * Receive new data
  742. *
  743. * @v xfer Data transfer interface
  744. * @v iobuf I/O buffer
  745. * @v data Received data
  746. * @v len Length of received data
  747. * @ret rc Return status code
  748. */
  749. static int dhcp_deliver_raw ( struct xfer_interface *xfer,
  750. const void *data, size_t len ) {
  751. struct dhcp_session *dhcp =
  752. container_of ( xfer, struct dhcp_session, xfer );
  753. const struct dhcphdr *dhcphdr = data;
  754. struct dhcp_option_block *options;
  755. struct dhcp_option_block **store_options;
  756. int is_proxy;
  757. unsigned int msgtype;
  758. unsigned long elapsed;
  759. /* Check for matching transaction ID */
  760. if ( dhcphdr->xid != dhcp_xid ( dhcp->netdev ) ) {
  761. DBGC ( dhcp, "DHCP %p wrong transaction ID (wanted %08lx, "
  762. "got %08lx)\n", dhcp, ntohl ( dhcphdr->xid ),
  763. ntohl ( dhcp_xid ( dhcp->netdev ) ) );
  764. return 0;
  765. };
  766. /* Parse packet and create options structure */
  767. options = dhcp_parse ( dhcphdr, len );
  768. if ( ! options ) {
  769. DBGC ( dhcp, "DHCP %p could not parse DHCP packet\n", dhcp );
  770. return -EINVAL;
  771. }
  772. /* Determine and verify message type */
  773. is_proxy = ( dhcphdr->yiaddr.s_addr == 0 );
  774. msgtype = find_dhcp_num_option ( options, DHCP_MESSAGE_TYPE );
  775. DBGC ( dhcp, "DHCP %p received %s%s\n", dhcp,
  776. ( is_proxy ? "Proxy" : "" ), dhcp_msgtype_name ( msgtype ) );
  777. if ( ( ( dhcp->state != DHCPDISCOVER ) || ( msgtype != DHCPOFFER ) ) &&
  778. ( ( dhcp->state != DHCPREQUEST ) || ( msgtype != DHCPACK ) ) ) {
  779. DBGC ( dhcp, "DHCP %p discarding %s while in %s state\n",
  780. dhcp, dhcp_msgtype_name ( msgtype ),
  781. dhcp_msgtype_name ( dhcp->state ) );
  782. goto out_discard;
  783. }
  784. /* Update stored standard/ProxyDHCP options, if the new
  785. * options have equal or higher priority than the
  786. * currently-stored options.
  787. */
  788. store_options = ( is_proxy ? &dhcp->proxy_options : &dhcp->options );
  789. if ( ( ! *store_options ) ||
  790. ( find_dhcp_num_option ( options, DHCP_EB_PRIORITY ) >=
  791. find_dhcp_num_option ( *store_options, DHCP_EB_PRIORITY ) ) ) {
  792. dhcpopt_put ( *store_options );
  793. *store_options = options;
  794. } else {
  795. dhcpopt_put ( options );
  796. }
  797. /* Handle DHCP response */
  798. switch ( dhcp->state ) {
  799. case DHCPDISCOVER:
  800. /* If we have received a valid standard DHCP response
  801. * (i.e. one with an IP address), and we have allowed
  802. * sufficient time for ProxyDHCP reponses, then
  803. * transition to making the DHCPREQUEST.
  804. */
  805. elapsed = ( currticks() - dhcp->start );
  806. if ( dhcp->options &&
  807. ( elapsed > PROXYDHCP_WAIT_TIME ) ) {
  808. stop_timer ( &dhcp->timer );
  809. dhcp->state = DHCPREQUEST;
  810. dhcp_send_request ( dhcp );
  811. }
  812. break;
  813. case DHCPREQUEST:
  814. /* DHCP finished; register options and exit */
  815. if ( dhcp->proxy_options )
  816. dhcp->register_options ( dhcp->netdev,
  817. dhcp->proxy_options );
  818. dhcp->register_options ( dhcp->netdev, dhcp->options );
  819. dhcp_finished ( dhcp, 0 );
  820. break;
  821. default:
  822. assert ( 0 );
  823. }
  824. return 0;
  825. out_discard:
  826. dhcpopt_put ( options );
  827. return 0;
  828. }
  829. /** DHCP data transfer interface operations */
  830. static struct xfer_interface_operations dhcp_xfer_operations = {
  831. .close = ignore_xfer_close,
  832. .vredirect = xfer_vopen,
  833. .seek = ignore_xfer_seek,
  834. .window = unlimited_xfer_window,
  835. .deliver_iob = xfer_deliver_as_raw,
  836. .deliver_raw = dhcp_deliver_raw,
  837. };
  838. /****************************************************************************
  839. *
  840. * Job control interface
  841. *
  842. */
  843. /**
  844. * Handle kill() event received via job control interface
  845. *
  846. * @v job DHCP job control interface
  847. */
  848. static void dhcp_job_kill ( struct job_interface *job ) {
  849. struct dhcp_session *dhcp =
  850. container_of ( job, struct dhcp_session, job );
  851. /* Terminate DHCP session */
  852. dhcp_finished ( dhcp, -ECANCELED );
  853. }
  854. /** DHCP job control interface operations */
  855. static struct job_interface_operations dhcp_job_operations = {
  856. .done = ignore_job_done,
  857. .kill = dhcp_job_kill,
  858. .progress = ignore_job_progress,
  859. };
  860. /****************************************************************************
  861. *
  862. * Instantiator
  863. *
  864. */
  865. /**
  866. * Start DHCP on a network device
  867. *
  868. * @v job Job control interface
  869. * @v netdev Network device
  870. * @v register_options DHCP option block registration routine
  871. * @ret rc Return status code
  872. *
  873. * Starts DHCP on the specified network device. If successful, the @c
  874. * register_options() routine will be called with the acquired
  875. * options.
  876. */
  877. int start_dhcp ( struct job_interface *job, struct net_device *netdev,
  878. int ( * register_options ) ( struct net_device *netdev,
  879. struct dhcp_option_block * ) ) {
  880. static struct sockaddr_in server = {
  881. .sin_family = AF_INET,
  882. .sin_addr.s_addr = INADDR_BROADCAST,
  883. .sin_port = htons ( BOOTPS_PORT ),
  884. };
  885. static struct sockaddr_in client = {
  886. .sin_family = AF_INET,
  887. .sin_port = htons ( BOOTPC_PORT ),
  888. };
  889. struct dhcp_session *dhcp;
  890. int rc;
  891. /* Allocate and initialise structure */
  892. dhcp = zalloc ( sizeof ( *dhcp ) );
  893. if ( ! dhcp )
  894. return -ENOMEM;
  895. dhcp->refcnt.free = dhcp_free;
  896. job_init ( &dhcp->job, &dhcp_job_operations, &dhcp->refcnt );
  897. xfer_init ( &dhcp->xfer, &dhcp_xfer_operations, &dhcp->refcnt );
  898. dhcp->netdev = netdev_get ( netdev );
  899. dhcp->register_options = register_options;
  900. dhcp->timer.expired = dhcp_timer_expired;
  901. dhcp->state = DHCPDISCOVER;
  902. dhcp->start = currticks();
  903. /* Instantiate child objects and attach to our interfaces */
  904. if ( ( rc = xfer_open_socket ( &dhcp->xfer, SOCK_DGRAM,
  905. ( struct sockaddr * ) &server,
  906. ( struct sockaddr * ) &client ) ) != 0 )
  907. goto err;
  908. /* Start timer to initiate initial DHCPREQUEST */
  909. start_timer_nodelay ( &dhcp->timer );
  910. /* Attach parent interface, mortalise self, and return */
  911. job_plug_plug ( &dhcp->job, job );
  912. ref_put ( &dhcp->refcnt );
  913. return 0;
  914. err:
  915. dhcp_finished ( dhcp, rc );
  916. ref_put ( &dhcp->refcnt );
  917. return rc;
  918. }
  919. /****************************************************************************
  920. *
  921. * Network device configurator
  922. *
  923. */
  924. /**
  925. * Configure network device from DHCP options
  926. *
  927. * @v netdev Network device
  928. * @v options DHCP options block
  929. * @ret rc Return status code
  930. */
  931. int dhcp_configure_netdev ( struct net_device *netdev,
  932. struct dhcp_option_block *options ) {
  933. struct in_addr address = { 0 };
  934. struct in_addr netmask = { 0 };
  935. struct in_addr gateway = { INADDR_NONE };
  936. int rc;
  937. /* Retrieve IP address configuration */
  938. find_dhcp_ipv4_option ( options, DHCP_EB_YIADDR, &address );
  939. find_dhcp_ipv4_option ( options, DHCP_SUBNET_MASK, &netmask );
  940. find_dhcp_ipv4_option ( options, DHCP_ROUTERS, &gateway );
  941. /* Do nothing unless we have at least an IP address to use */
  942. if ( ! address.s_addr )
  943. return 0;
  944. /* Clear any existing routing table entry */
  945. del_ipv4_address ( netdev );
  946. /* Set up new IP address configuration */
  947. if ( ( rc = add_ipv4_address ( netdev, address, netmask,
  948. gateway ) ) != 0 ) {
  949. DBG ( "Could not configure %s with DHCP results: %s\n",
  950. netdev->name, strerror ( rc ) );
  951. return rc;
  952. }
  953. return 0;
  954. }