You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

dhcp.c 25KB

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