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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692
  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 <errno.h>
  20. #include <assert.h>
  21. #include <byteswap.h>
  22. #include <gpxe/if_ether.h>
  23. #include <gpxe/netdevice.h>
  24. #include <gpxe/udp.h>
  25. #include <gpxe/dhcp.h>
  26. /** @file
  27. *
  28. * Dynamic Host Configuration Protocol
  29. *
  30. */
  31. /** DHCP operation types
  32. *
  33. * This table maps from DHCP message types (i.e. values of the @c
  34. * DHCP_MESSAGE_TYPE option) to values of the "op" field within a DHCP
  35. * packet.
  36. */
  37. static const uint8_t dhcp_op[] = {
  38. [DHCPDISCOVER] = BOOTP_REQUEST,
  39. [DHCPOFFER] = BOOTP_REPLY,
  40. [DHCPREQUEST] = BOOTP_REQUEST,
  41. [DHCPDECLINE] = BOOTP_REQUEST,
  42. [DHCPACK] = BOOTP_REPLY,
  43. [DHCPNAK] = BOOTP_REPLY,
  44. [DHCPRELEASE] = BOOTP_REQUEST,
  45. [DHCPINFORM] = BOOTP_REQUEST,
  46. };
  47. /** Raw option data for options common to all DHCP requests */
  48. static uint8_t dhcp_request_options_data[] = {
  49. DHCP_MAX_MESSAGE_SIZE, DHCP_WORD ( ETH_MAX_MTU ),
  50. DHCP_VENDOR_CLASS_ID,
  51. DHCP_STRING ( 'E', 't', 'h', 'e', 'r', 'b', 'o', 'o', 't' ),
  52. DHCP_PARAMETER_REQUEST_LIST,
  53. DHCP_OPTION ( DHCP_SUBNET_MASK, DHCP_ROUTERS, DHCP_HOST_NAME,
  54. DHCP_EB_ENCAP ),
  55. DHCP_END
  56. };
  57. /**
  58. * Name a DHCP packet type
  59. *
  60. * @v msgtype DHCP message type
  61. * @ret string DHCP mesasge type name
  62. */
  63. static inline const char * dhcp_msgtype_name ( unsigned int msgtype ) {
  64. switch ( msgtype ) {
  65. case 0: return "BOOTP"; /* Non-DHCP packet */
  66. case DHCPDISCOVER: return "DHCPDISCOVER";
  67. case DHCPOFFER: return "DHCPOFFER";
  68. case DHCPREQUEST: return "DHCPREQUEST";
  69. case DHCPDECLINE: return "DHCPDECLINE";
  70. case DHCPACK: return "DHCPACK";
  71. case DHCPNAK: return "DHCPNAK";
  72. case DHCPRELEASE: return "DHCPRELEASE";
  73. case DHCPINFORM: return "DHCPINFORM";
  74. default: return "DHCP<invalid>";
  75. }
  76. }
  77. /** Options common to all DHCP requests */
  78. static struct dhcp_option_block dhcp_request_options = {
  79. .data = dhcp_request_options_data,
  80. .max_len = sizeof ( dhcp_request_options_data ),
  81. .len = sizeof ( dhcp_request_options_data ),
  82. };
  83. /**
  84. * Set option within DHCP packet
  85. *
  86. * @v dhcppkt DHCP packet
  87. * @v tag DHCP option tag
  88. * @v data New value for DHCP option
  89. * @v len Length of value, in bytes
  90. * @ret rc Return status code
  91. *
  92. * Sets the option within the first available options block within the
  93. * DHCP packet. Option blocks are tried in the order specified by @c
  94. * dhcp_option_block_fill_order.
  95. *
  96. * The magic options @c DHCP_EB_YIADDR and @c DHCP_EB_SIADDR are
  97. * intercepted and inserted into the appropriate fixed fields within
  98. * the DHCP packet. The option @c DHCP_OPTION_OVERLOAD is silently
  99. * ignored, since our DHCP packet assembly method relies on always
  100. * having option overloading in use.
  101. */
  102. static int set_dhcp_packet_option ( struct dhcp_packet *dhcppkt,
  103. unsigned int tag, const void *data,
  104. size_t len ) {
  105. struct dhcphdr *dhcphdr = dhcppkt->dhcphdr;
  106. struct dhcp_option_block *options = dhcppkt->options;
  107. struct dhcp_option *option = NULL;
  108. /* Special-case the magic options */
  109. switch ( tag ) {
  110. case DHCP_OPTION_OVERLOAD:
  111. /* Hard-coded in packets we create; always ignore */
  112. return 0;
  113. case DHCP_EB_YIADDR:
  114. memcpy ( &dhcphdr->yiaddr, data, sizeof ( dhcphdr->yiaddr ) );
  115. return 0;
  116. case DHCP_EB_SIADDR:
  117. memcpy ( &dhcphdr->siaddr, data, sizeof ( dhcphdr->siaddr ) );
  118. return 0;
  119. case DHCP_MESSAGE_TYPE:
  120. case DHCP_REQUESTED_ADDRESS:
  121. case DHCP_PARAMETER_REQUEST_LIST:
  122. /* These options have to be within the main options
  123. * block. This doesn't seem to be required by the
  124. * RFCs, but at least ISC dhcpd refuses to recognise
  125. * them otherwise.
  126. */
  127. options = &dhcppkt->options[OPTS_MAIN];
  128. break;
  129. default:
  130. /* Continue processing as normal */
  131. break;
  132. }
  133. /* Set option in first available options block */
  134. for ( ; options < &dhcppkt->options[NUM_OPT_BLOCKS] ; options++ ) {
  135. option = set_dhcp_option ( options, tag, data, len );
  136. if ( option )
  137. break;
  138. }
  139. /* Update DHCP packet length */
  140. dhcppkt->len = ( offsetof ( typeof ( *dhcppkt->dhcphdr ), options )
  141. + dhcppkt->options[OPTS_MAIN].len );
  142. return ( option ? 0 : -ENOSPC );
  143. }
  144. /**
  145. * Copy option into DHCP packet
  146. *
  147. * @v dhcppkt DHCP packet
  148. * @v options DHCP option block, or NULL
  149. * @v tag DHCP option tag to search for
  150. * @v new_tag DHCP option tag to use for copied option
  151. * @ret rc Return status code
  152. *
  153. * Copies a single option, if present, from the DHCP options block
  154. * into a DHCP packet. The tag for the option may be changed if
  155. * desired; this is required by other parts of the DHCP code.
  156. *
  157. * @c options may specify a single options block, or be left as NULL
  158. * in order to search for the option within all registered options
  159. * blocks.
  160. */
  161. static int copy_dhcp_packet_option ( struct dhcp_packet *dhcppkt,
  162. struct dhcp_option_block *options,
  163. unsigned int tag, unsigned int new_tag ) {
  164. struct dhcp_option *option;
  165. int rc;
  166. option = find_dhcp_option ( options, tag );
  167. if ( option ) {
  168. if ( ( rc = set_dhcp_packet_option ( dhcppkt, new_tag,
  169. &option->data,
  170. option->len ) ) != 0 )
  171. return rc;
  172. }
  173. return 0;
  174. }
  175. /**
  176. * Copy options into DHCP packet
  177. *
  178. * @v dhcppkt DHCP packet
  179. * @v options DHCP option block, or NULL
  180. * @v encapsulator Encapsulating option, or zero
  181. * @ret rc Return status code
  182. *
  183. * Copies options with the specified encapsulator from DHCP options
  184. * blocks into a DHCP packet. Most options are copied verbatim.
  185. * Recognised encapsulated options fields are handled as such.
  186. *
  187. * @c options may specify a single options block, or be left as NULL
  188. * in order to copy options from all registered options blocks.
  189. */
  190. static int copy_dhcp_packet_encap_options ( struct dhcp_packet *dhcppkt,
  191. struct dhcp_option_block *options,
  192. unsigned int encapsulator ) {
  193. unsigned int subtag;
  194. unsigned int tag;
  195. int rc;
  196. for ( subtag = DHCP_MIN_OPTION; subtag <= DHCP_MAX_OPTION; subtag++ ) {
  197. tag = DHCP_ENCAP_OPT ( encapsulator, subtag );
  198. switch ( tag ) {
  199. case DHCP_EB_ENCAP:
  200. case DHCP_VENDOR_ENCAP:
  201. /* Process encapsulated options field */
  202. if ( ( rc = copy_dhcp_packet_encap_options ( dhcppkt,
  203. options,
  204. tag)) !=0)
  205. return rc;
  206. break;
  207. default:
  208. /* Copy option to reassembled packet */
  209. if ( ( rc = copy_dhcp_packet_option ( dhcppkt, options,
  210. tag, tag ) ) !=0)
  211. return rc;
  212. break;
  213. };
  214. }
  215. return 0;
  216. }
  217. /**
  218. * Copy options into DHCP packet
  219. *
  220. * @v dhcppkt DHCP packet
  221. * @v options DHCP option block, or NULL
  222. * @ret rc Return status code
  223. *
  224. * Copies options from DHCP options blocks into a DHCP packet. Most
  225. * options are copied verbatim. Recognised encapsulated options
  226. * fields are handled as such.
  227. *
  228. * @c options may specify a single options block, or be left as NULL
  229. * in order to copy options from all registered options blocks.
  230. */
  231. static int copy_dhcp_packet_options ( struct dhcp_packet *dhcppkt,
  232. struct dhcp_option_block *options ) {
  233. return copy_dhcp_packet_encap_options ( dhcppkt, options, 0 );
  234. }
  235. /**
  236. * Create a DHCP packet
  237. *
  238. * @v dhcp DHCP session
  239. * @v msgtype DHCP message type
  240. * @v data Buffer for DHCP packet
  241. * @v max_len Size of DHCP packet buffer
  242. * @v dhcppkt DHCP packet structure to fill in
  243. * @ret rc Return status code
  244. *
  245. * Creates a DHCP packet in the specified buffer, and fills out a @c
  246. * dhcp_packet structure that can be passed to
  247. * set_dhcp_packet_option() or copy_dhcp_packet_options().
  248. */
  249. static int create_dhcp_packet ( struct dhcp_session *dhcp, uint8_t msgtype,
  250. void *data, size_t max_len,
  251. struct dhcp_packet *dhcppkt ) {
  252. struct dhcphdr *dhcphdr = data;
  253. static const uint8_t overloading = ( DHCP_OPTION_OVERLOAD_FILE |
  254. DHCP_OPTION_OVERLOAD_SNAME );
  255. int rc;
  256. /* Sanity check */
  257. if ( max_len < sizeof ( *dhcphdr ) )
  258. return -ENOSPC;
  259. /* Initialise DHCP packet content */
  260. memset ( dhcphdr, 0, max_len );
  261. dhcphdr->xid = dhcp->xid;
  262. dhcphdr->magic = htonl ( DHCP_MAGIC_COOKIE );
  263. dhcphdr->htype = ntohs ( dhcp->netdev->ll_protocol->ll_proto );
  264. dhcphdr->hlen = dhcp->netdev->ll_protocol->ll_addr_len;
  265. memcpy ( dhcphdr->chaddr, dhcp->netdev->ll_addr, dhcphdr->hlen );
  266. dhcphdr->op = dhcp_op[msgtype];
  267. /* Initialise DHCP packet structure */
  268. dhcppkt->dhcphdr = dhcphdr;
  269. dhcppkt->max_len = max_len;
  270. init_dhcp_options ( &dhcppkt->options[OPTS_MAIN], dhcphdr->options,
  271. ( max_len -
  272. offsetof ( typeof ( *dhcphdr ), options ) ) );
  273. init_dhcp_options ( &dhcppkt->options[OPTS_FILE], dhcphdr->file,
  274. sizeof ( dhcphdr->file ) );
  275. init_dhcp_options ( &dhcppkt->options[OPTS_SNAME], dhcphdr->sname,
  276. sizeof ( dhcphdr->sname ) );
  277. /* Set DHCP_OPTION_OVERLOAD option within the main options block */
  278. if ( set_dhcp_option ( &dhcppkt->options[OPTS_MAIN],
  279. DHCP_OPTION_OVERLOAD, &overloading,
  280. sizeof ( overloading ) ) == NULL )
  281. return -ENOSPC;
  282. /* Set DHCP_MESSAGE_TYPE option */
  283. if ( ( rc = set_dhcp_packet_option ( dhcppkt, DHCP_MESSAGE_TYPE,
  284. &msgtype,
  285. sizeof ( msgtype ) ) ) != 0 )
  286. return rc;
  287. return 0;
  288. }
  289. /**
  290. * Calculate used length of a field containing DHCP options
  291. *
  292. * @v data Field containing DHCP options
  293. * @v max_len Field length
  294. * @ret len Used length (excluding the @c DHCP_END tag)
  295. */
  296. static size_t dhcp_field_len ( const void *data, size_t max_len ) {
  297. struct dhcp_option_block options;
  298. struct dhcp_option *end;
  299. options.data = ( ( void * ) data );
  300. options.len = max_len;
  301. end = find_dhcp_option ( &options, DHCP_END );
  302. return ( end ? ( ( ( void * ) end ) - data ) : 0 );
  303. }
  304. /**
  305. * Merge field containing DHCP options or string into DHCP options block
  306. *
  307. * @v options DHCP option block
  308. * @v data Field containing DHCP options
  309. * @v max_len Field length
  310. * @v tag DHCP option tag, or 0
  311. *
  312. * If @c tag is non-zero, the field will be treated as a
  313. * NUL-terminated string representing the value of the specified DHCP
  314. * option. If @c tag is zero, the field will be treated as a block of
  315. * DHCP options, and simply appended to the existing options in the
  316. * option block.
  317. *
  318. * The caller must ensure that there is enough space in the options
  319. * block to perform the merge.
  320. */
  321. static void merge_dhcp_field ( struct dhcp_option_block *options,
  322. const void *data, size_t max_len,
  323. unsigned int tag ) {
  324. size_t len;
  325. void *dest;
  326. struct dhcp_option *end;
  327. if ( tag ) {
  328. set_dhcp_option ( options, tag, data, strlen ( data ) );
  329. } else {
  330. len = dhcp_field_len ( data, max_len );
  331. dest = ( options->data + options->len - 1 );
  332. memcpy ( dest, data, len );
  333. options->len += len;
  334. end = ( dest + len );
  335. end->tag = DHCP_END;
  336. }
  337. }
  338. /**
  339. * Parse DHCP packet and construct DHCP options block
  340. *
  341. * @v dhcphdr DHCP packet
  342. * @v len Length of DHCP packet
  343. * @ret options DHCP options block, or NULL
  344. *
  345. * Parses a received DHCP packet and canonicalises its contents into a
  346. * single DHCP options block. The "file" and "sname" fields are
  347. * converted into the corresponding DHCP options (@c
  348. * DHCP_BOOTFILE_NAME and @c DHCP_TFTP_SERVER_NAME respectively). If
  349. * these fields are used for option overloading, their options are
  350. * merged in to the options block.
  351. *
  352. * The values of the "yiaddr" and "siaddr" fields will be stored
  353. * within the options block as the magic options @c DHCP_EB_YIADDR and
  354. * @c DHCP_EB_SIADDR.
  355. *
  356. * Note that this call allocates new memory for the constructed DHCP
  357. * options block; it is the responsibility of the caller to eventually
  358. * free this memory.
  359. */
  360. static struct dhcp_option_block * dhcp_parse ( struct dhcphdr *dhcphdr,
  361. size_t len ) {
  362. struct dhcp_option_block *options;
  363. size_t options_len;
  364. unsigned int overloading;
  365. /* Sanity check */
  366. if ( len < sizeof ( *dhcphdr ) )
  367. return NULL;
  368. /* Calculate size of resulting concatenated option block:
  369. *
  370. * The "options" field : length of the field minus the DHCP_END tag.
  371. *
  372. * The "file" field : maximum length of the field minus the
  373. * NUL terminator, plus a 2-byte DHCP header or, if used for
  374. * option overloading, the length of the field minus the
  375. * DHCP_END tag.
  376. *
  377. * The "sname" field : as for the "file" field.
  378. *
  379. * 15 bytes for an encapsulated options field to contain the
  380. * value of the "yiaddr" and "siaddr" fields
  381. *
  382. * 1 byte for a final terminating DHCP_END tag.
  383. */
  384. options_len = ( ( len - offsetof ( typeof ( *dhcphdr ), options ) ) - 1
  385. + ( sizeof ( dhcphdr->file ) + 1 )
  386. + ( sizeof ( dhcphdr->sname ) + 1 )
  387. + 15 /* yiaddr and siaddr */
  388. + 1 /* DHCP_END tag */ );
  389. /* Allocate empty options block of required size */
  390. options = alloc_dhcp_options ( options_len );
  391. if ( ! options ) {
  392. DBG ( "DHCP could not allocate %d-byte option block\n",
  393. options_len );
  394. return NULL;
  395. }
  396. /* Merge in "options" field, if this is a DHCP packet */
  397. if ( dhcphdr->magic == htonl ( DHCP_MAGIC_COOKIE ) ) {
  398. merge_dhcp_field ( options, dhcphdr->options,
  399. ( len -
  400. offsetof ( typeof (*dhcphdr), options ) ),
  401. 0 /* Always contains options */ );
  402. }
  403. /* Identify overloaded fields */
  404. overloading = find_dhcp_num_option ( options, DHCP_OPTION_OVERLOAD );
  405. /* Merge in "file" and "sname" fields */
  406. merge_dhcp_field ( options, dhcphdr->file, sizeof ( dhcphdr->file ),
  407. ( ( overloading & DHCP_OPTION_OVERLOAD_FILE ) ?
  408. 0 : DHCP_BOOTFILE_NAME ) );
  409. merge_dhcp_field ( options, dhcphdr->sname, sizeof ( dhcphdr->sname ),
  410. ( ( overloading & DHCP_OPTION_OVERLOAD_SNAME ) ?
  411. 0 : DHCP_TFTP_SERVER_NAME ) );
  412. /* Set magic options for "yiaddr" and "siaddr", if present */
  413. if ( dhcphdr->yiaddr.s_addr ) {
  414. set_dhcp_option ( options, DHCP_EB_YIADDR,
  415. &dhcphdr->yiaddr, sizeof (dhcphdr->yiaddr) );
  416. }
  417. if ( dhcphdr->siaddr.s_addr ) {
  418. set_dhcp_option ( options, DHCP_EB_SIADDR,
  419. &dhcphdr->siaddr, sizeof (dhcphdr->siaddr) );
  420. }
  421. assert ( options->len <= options->max_len );
  422. return options;
  423. }
  424. /****************************************************************************
  425. *
  426. * DHCP to UDP interface
  427. *
  428. */
  429. static inline struct dhcp_session *
  430. udp_to_dhcp ( struct udp_connection *conn ) {
  431. return container_of ( conn, struct dhcp_session, udp );
  432. }
  433. /**
  434. * Mark DHCP session as complete
  435. *
  436. * @v dhcp DHCP session
  437. * @v rc Return status code
  438. */
  439. static void dhcp_done ( struct dhcp_session *dhcp, int rc ) {
  440. /* Free up options if we failed */
  441. if ( rc != 0 ) {
  442. if ( dhcp->options ) {
  443. free_dhcp_options ( dhcp->options );
  444. dhcp->options = NULL;
  445. }
  446. }
  447. /* Mark async operation as complete */
  448. async_done ( &dhcp->aop, rc );
  449. }
  450. /** Address for transmitting DHCP requests */
  451. static struct sockaddr sa_dhcp_server = {
  452. .sa_family = AF_INET,
  453. .sin = {
  454. .sin_addr.s_addr = INADDR_BROADCAST,
  455. .sin_port = htons ( BOOTPS_PORT ),
  456. },
  457. };
  458. /**
  459. * Transmit DHCP request
  460. *
  461. * @v conn UDP connection
  462. * @v buf Temporary data buffer
  463. * @v len Length of temporary data buffer
  464. */
  465. static void dhcp_senddata ( struct udp_connection *conn,
  466. void *buf, size_t len ) {
  467. struct dhcp_session *dhcp = udp_to_dhcp ( conn );
  468. struct dhcp_packet dhcppkt;
  469. int rc;
  470. DBG ( "Transmitting %s\n", dhcp_msgtype_name ( dhcp->state ) );
  471. assert ( ( dhcp->state == DHCPDISCOVER ) ||
  472. ( dhcp->state == DHCPREQUEST ) );
  473. /* Create DHCP packet in temporary buffer */
  474. if ( ( rc = create_dhcp_packet ( dhcp, dhcp->state, buf, len,
  475. &dhcppkt ) ) != 0 ) {
  476. DBG ( "Could not create DHCP packet\n" );
  477. return;
  478. }
  479. /* Copy in options common to all requests */
  480. if ( ( rc = copy_dhcp_packet_options ( &dhcppkt,
  481. &dhcp_request_options ) ) != 0){
  482. DBG ( "Could not set common DHCP options\n" );
  483. return;
  484. }
  485. /* Copy any required options from previous server repsonse */
  486. if ( dhcp->options ) {
  487. if ( ( rc = copy_dhcp_packet_option ( &dhcppkt, dhcp->options,
  488. DHCP_SERVER_IDENTIFIER,
  489. DHCP_SERVER_IDENTIFIER ) ) != 0 ) {
  490. DBG ( "Could not set server identifier option\n" );
  491. return;
  492. }
  493. if ( ( rc = copy_dhcp_packet_option ( &dhcppkt, dhcp->options,
  494. DHCP_EB_YIADDR,
  495. DHCP_REQUESTED_ADDRESS ) ) != 0 ) {
  496. DBG ( "Could not set requested address option\n" );
  497. return;
  498. }
  499. }
  500. /* Transmit the packet */
  501. if ( ( rc = udp_sendto ( conn, &sa_dhcp_server,
  502. dhcppkt.dhcphdr, dhcppkt.len ) ) != 0 ) {
  503. DBG ( "Could not transmit UDP packet\n" );
  504. return;
  505. }
  506. }
  507. /**
  508. * Transmit DHCP request
  509. *
  510. * @v dhcp DHCP session
  511. */
  512. static void dhcp_send_request ( struct dhcp_session *dhcp ) {
  513. start_timer ( &dhcp->timer );
  514. udp_senddata ( &dhcp->udp );
  515. }
  516. /**
  517. * Handle DHCP retry timer expiry
  518. *
  519. * @v timer DHCP retry timer
  520. * @v fail Failure indicator
  521. */
  522. static void dhcp_timer_expired ( struct retry_timer *timer, int fail ) {
  523. struct dhcp_session *dhcp =
  524. container_of ( timer, struct dhcp_session, timer );
  525. if ( fail ) {
  526. dhcp_done ( dhcp, -ETIMEDOUT );
  527. } else {
  528. dhcp_send_request ( dhcp );
  529. }
  530. }
  531. /**
  532. * Receive new data
  533. *
  534. * @v udp UDP connection
  535. * @v data Received data
  536. * @v len Length of received data
  537. */
  538. static void dhcp_newdata ( struct udp_connection *conn,
  539. void *data, size_t len ) {
  540. struct dhcp_session *dhcp = udp_to_dhcp ( conn );
  541. struct dhcphdr *dhcphdr = data;
  542. struct dhcp_option_block *options;
  543. unsigned int msgtype;
  544. /* Check for matching transaction ID */
  545. if ( dhcphdr->xid != dhcp->xid ) {
  546. DBG ( "DHCP wrong transaction ID (wanted %08lx, got %08lx)\n",
  547. ntohl ( dhcphdr->xid ), ntohl ( dhcp->xid ) );
  548. return;
  549. };
  550. /* Parse packet and create options structure */
  551. options = dhcp_parse ( dhcphdr, len );
  552. if ( ! options ) {
  553. DBG ( "Could not parse DHCP packet\n" );
  554. return;
  555. }
  556. /* Determine message type */
  557. msgtype = find_dhcp_num_option ( options, DHCP_MESSAGE_TYPE );
  558. DBG ( "Received %s\n", dhcp_msgtype_name ( msgtype ) );
  559. /* Handle DHCP reply */
  560. switch ( dhcp->state ) {
  561. case DHCPDISCOVER:
  562. if ( msgtype != DHCPOFFER )
  563. goto out_discard;
  564. dhcp->state = DHCPREQUEST;
  565. break;
  566. case DHCPREQUEST:
  567. if ( msgtype != DHCPACK )
  568. goto out_discard;
  569. dhcp->state = DHCPACK;
  570. break;
  571. default:
  572. assert ( 0 );
  573. goto out_discard;
  574. }
  575. /* Stop timer and update stored options */
  576. stop_timer ( &dhcp->timer );
  577. if ( dhcp->options )
  578. free_dhcp_options ( dhcp->options );
  579. dhcp->options = options;
  580. /* Transmit next packet, or terminate session */
  581. if ( dhcp->state < DHCPACK ) {
  582. dhcp_send_request ( dhcp );
  583. } else {
  584. dhcp_done ( dhcp, 0 );
  585. }
  586. return;
  587. out_discard:
  588. free_dhcp_options ( options );
  589. }
  590. /** DHCP UDP operations */
  591. static struct udp_operations dhcp_udp_operations = {
  592. .senddata = dhcp_senddata,
  593. .newdata = dhcp_newdata,
  594. };
  595. /**
  596. * Initiate DHCP on a network interface
  597. *
  598. * @v dhcp DHCP session
  599. * @ret aop Asynchronous operation
  600. *
  601. * If the DHCP operation completes successfully, the
  602. * dhcp_session::options field will be filled in with the resulting
  603. * options block. The caller takes responsibility for eventually
  604. * calling free_dhcp_options().
  605. */
  606. struct async_operation * start_dhcp ( struct dhcp_session *dhcp ) {
  607. int rc;
  608. /* Initialise DHCP session */
  609. dhcp->udp.udp_op = &dhcp_udp_operations;
  610. dhcp->timer.expired = dhcp_timer_expired;
  611. dhcp->state = DHCPDISCOVER;
  612. /* Use least significant 32 bits of link-layer address as XID */
  613. memcpy ( &dhcp->xid, ( dhcp->netdev->ll_addr
  614. + dhcp->netdev->ll_protocol->ll_addr_len
  615. - sizeof ( dhcp->xid ) ), sizeof ( dhcp->xid ));
  616. /* Bind to local port */
  617. if ( ( rc = udp_open ( &dhcp->udp, htons ( BOOTPC_PORT ) ) ) != 0 ) {
  618. async_done ( &dhcp->aop, rc );
  619. goto out;
  620. }
  621. /* Proof of concept: just send a single DHCPDISCOVER */
  622. dhcp_send_request ( dhcp );
  623. out:
  624. return &dhcp->aop;
  625. }