dhcp.c 20KB

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