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 21KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787
  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. #include <gpxe/timer.h>
  36. #include <gpxe/settings.h>
  37. #include <gpxe/dhcp.h>
  38. #include <gpxe/dhcpopts.h>
  39. #include <gpxe/dhcppkt.h>
  40. /** @file
  41. *
  42. * Dynamic Host Configuration Protocol
  43. *
  44. */
  45. /**
  46. * DHCP operation types
  47. *
  48. * This table maps from DHCP message types (i.e. values of the @c
  49. * DHCP_MESSAGE_TYPE option) to values of the "op" field within a DHCP
  50. * packet.
  51. */
  52. static const uint8_t dhcp_op[] = {
  53. [DHCPDISCOVER] = BOOTP_REQUEST,
  54. [DHCPOFFER] = BOOTP_REPLY,
  55. [DHCPREQUEST] = BOOTP_REQUEST,
  56. [DHCPDECLINE] = BOOTP_REQUEST,
  57. [DHCPACK] = BOOTP_REPLY,
  58. [DHCPNAK] = BOOTP_REPLY,
  59. [DHCPRELEASE] = BOOTP_REQUEST,
  60. [DHCPINFORM] = BOOTP_REQUEST,
  61. };
  62. /** Raw option data for options common to all DHCP requests */
  63. static uint8_t dhcp_request_options_data[] = {
  64. DHCP_MAX_MESSAGE_SIZE, DHCP_WORD ( ETH_MAX_MTU ),
  65. DHCP_VENDOR_CLASS_ID,
  66. DHCP_STRING ( 'P', 'X', 'E', 'C', 'l', 'i', 'e', 'n', 't', ':',
  67. 'A', 'r', 'c', 'h', ':', '0', '0', '0', '0', '0', ':',
  68. 'U', 'N', 'D', 'I', ':', '0', '0', '2', '0', '0', '1' ),
  69. DHCP_CLIENT_ARCHITECTURE, DHCP_WORD ( 0 ),
  70. DHCP_CLIENT_NDI, DHCP_OPTION ( 1 /* UNDI */ , 2, 1 /* v2.1 */ ),
  71. DHCP_PARAMETER_REQUEST_LIST,
  72. DHCP_OPTION ( DHCP_SUBNET_MASK, DHCP_ROUTERS, DHCP_DNS_SERVERS,
  73. DHCP_LOG_SERVERS, DHCP_HOST_NAME, DHCP_DOMAIN_NAME,
  74. DHCP_ROOT_PATH, DHCP_VENDOR_ENCAP, DHCP_VENDOR_CLASS_ID,
  75. DHCP_TFTP_SERVER_NAME, DHCP_BOOTFILE_NAME,
  76. DHCP_EB_ENCAP, DHCP_ISCSI_INITIATOR_IQN ),
  77. DHCP_END
  78. };
  79. /** Options common to all DHCP requests */
  80. static struct dhcp_options dhcp_request_options = {
  81. .data = dhcp_request_options_data,
  82. .max_len = sizeof ( dhcp_request_options_data ),
  83. .len = sizeof ( dhcp_request_options_data ),
  84. };
  85. /** DHCP feature codes */
  86. static uint8_t dhcp_features[0] __table_start ( uint8_t, dhcp_features );
  87. static uint8_t dhcp_features_end[0] __table_end ( uint8_t, dhcp_features );
  88. /**
  89. * Name a DHCP packet type
  90. *
  91. * @v msgtype DHCP message type
  92. * @ret string DHCP mesasge type name
  93. */
  94. static inline const char * dhcp_msgtype_name ( unsigned int msgtype ) {
  95. switch ( msgtype ) {
  96. case 0: return "BOOTP"; /* Non-DHCP packet */
  97. case DHCPDISCOVER: return "DHCPDISCOVER";
  98. case DHCPOFFER: return "DHCPOFFER";
  99. case DHCPREQUEST: return "DHCPREQUEST";
  100. case DHCPDECLINE: return "DHCPDECLINE";
  101. case DHCPACK: return "DHCPACK";
  102. case DHCPNAK: return "DHCPNAK";
  103. case DHCPRELEASE: return "DHCPRELEASE";
  104. case DHCPINFORM: return "DHCPINFORM";
  105. default: return "DHCP<invalid>";
  106. }
  107. }
  108. /**
  109. * Calculate DHCP transaction ID for a network device
  110. *
  111. * @v netdev Network device
  112. * @ret xid DHCP XID
  113. *
  114. * Extract the least significant bits of the hardware address for use
  115. * as the transaction ID.
  116. */
  117. static uint32_t dhcp_xid ( struct net_device *netdev ) {
  118. uint32_t xid;
  119. memcpy ( &xid, ( netdev->ll_addr + netdev->ll_protocol->ll_addr_len
  120. - sizeof ( xid ) ), sizeof ( xid ) );
  121. return xid;
  122. }
  123. /**
  124. * Create a DHCP packet
  125. *
  126. * @v dhcppkt DHCP packet structure to fill in
  127. * @v netdev Network device
  128. * @v msgtype DHCP message type
  129. * @v options Initial options to include (or NULL)
  130. * @v data Buffer for DHCP packet
  131. * @v max_len Size of DHCP packet buffer
  132. * @ret rc Return status code
  133. *
  134. * Creates a DHCP packet in the specified buffer, and fills out a @c
  135. * dhcp_packet structure that can be passed to
  136. * set_dhcp_packet_option() or copy_dhcp_packet_options().
  137. */
  138. static int create_dhcp_packet ( struct dhcp_packet *dhcppkt,
  139. struct net_device *netdev, uint8_t msgtype,
  140. struct dhcp_options *options,
  141. void *data, size_t max_len ) {
  142. struct dhcphdr *dhcphdr = data;
  143. size_t options_len;
  144. unsigned int hlen;
  145. int rc;
  146. /* Sanity check */
  147. options_len = ( options ? options->len : 0 );
  148. if ( max_len < ( sizeof ( *dhcphdr ) + options_len ) )
  149. return -ENOSPC;
  150. /* Initialise DHCP packet content */
  151. memset ( dhcphdr, 0, max_len );
  152. dhcphdr->xid = dhcp_xid ( netdev );
  153. dhcphdr->magic = htonl ( DHCP_MAGIC_COOKIE );
  154. dhcphdr->htype = ntohs ( netdev->ll_protocol->ll_proto );
  155. dhcphdr->op = dhcp_op[msgtype];
  156. /* If hardware length exceeds the chaddr field length, don't
  157. * use the chaddr field. This is as per RFC4390.
  158. */
  159. hlen = netdev->ll_protocol->ll_addr_len;
  160. if ( hlen > sizeof ( dhcphdr->chaddr ) ) {
  161. hlen = 0;
  162. dhcphdr->flags = htons ( BOOTP_FL_BROADCAST );
  163. }
  164. dhcphdr->hlen = hlen;
  165. memcpy ( dhcphdr->chaddr, netdev->ll_addr, hlen );
  166. memcpy ( dhcphdr->options, options, options_len );
  167. /* Initialise DHCP packet structure and settings interface */
  168. dhcppkt_init ( dhcppkt, NULL, data, max_len );
  169. /* Set DHCP_MESSAGE_TYPE option */
  170. if ( ( rc = store_setting ( &dhcppkt->settings, DHCP_MESSAGE_TYPE,
  171. &msgtype, sizeof ( msgtype ) ) ) != 0 )
  172. return rc;
  173. return 0;
  174. }
  175. /** DHCP network device descriptor */
  176. struct dhcp_netdev_desc {
  177. /** Bus type ID */
  178. uint8_t type;
  179. /** Vendor ID */
  180. uint16_t vendor;
  181. /** Device ID */
  182. uint16_t device;
  183. } __attribute__ (( packed ));
  184. /** DHCP client identifier */
  185. struct dhcp_client_id {
  186. /** Link-layer protocol */
  187. uint8_t ll_proto;
  188. /** Link-layer address */
  189. uint8_t ll_addr[MAX_LL_ADDR_LEN];
  190. } __attribute__ (( packed ));
  191. /** DHCP client UUID */
  192. struct dhcp_client_uuid {
  193. /** Identifier type */
  194. uint8_t type;
  195. /** UUID */
  196. union uuid uuid;
  197. } __attribute__ (( packed ));
  198. #define DHCP_CLIENT_UUID_TYPE 0
  199. /**
  200. * Create DHCP request
  201. *
  202. * @v dhcppkt DHCP packet structure to fill in
  203. * @v netdev Network device
  204. * @v msgtype DHCP message type
  205. * @v offer_settings Settings received in DHCPOFFER, or NULL
  206. * @v data Buffer for DHCP packet
  207. * @v max_len Size of DHCP packet buffer
  208. * @ret rc Return status code
  209. */
  210. int create_dhcp_request ( struct dhcp_packet *dhcppkt,
  211. struct net_device *netdev, int msgtype,
  212. struct settings *offer_settings,
  213. void *data, size_t max_len ) {
  214. struct device_description *desc = &netdev->dev->desc;
  215. struct dhcp_netdev_desc dhcp_desc;
  216. struct dhcp_client_id client_id;
  217. struct dhcp_client_uuid client_uuid;
  218. size_t dhcp_features_len;
  219. size_t ll_addr_len;
  220. int rc;
  221. /* Create DHCP packet */
  222. if ( ( rc = create_dhcp_packet ( dhcppkt, netdev, msgtype,
  223. &dhcp_request_options, data,
  224. max_len ) ) != 0 ) {
  225. DBG ( "DHCP could not create DHCP packet: %s\n",
  226. strerror ( rc ) );
  227. return rc;
  228. }
  229. /* Copy any required options from previous server repsonse */
  230. if ( offer_settings ) {
  231. if ( ( rc = copy_setting ( &dhcppkt->settings,
  232. DHCP_SERVER_IDENTIFIER,
  233. offer_settings,
  234. DHCP_SERVER_IDENTIFIER ) ) != 0 ) {
  235. DBG ( "DHCP could not set server identifier "
  236. "option: %s\n", strerror ( rc ) );
  237. return rc;
  238. }
  239. if ( ( rc = copy_setting ( &dhcppkt->settings, DHCP_EB_YIADDR,
  240. offer_settings,
  241. DHCP_REQUESTED_ADDRESS ) ) != 0 ) {
  242. DBG ( "DHCP could not set requested address "
  243. "option: %s\n", strerror ( rc ) );
  244. return rc;
  245. }
  246. }
  247. /* Add options to identify the feature list */
  248. dhcp_features_len = ( dhcp_features_end - dhcp_features );
  249. if ( ( rc = store_setting ( &dhcppkt->settings, DHCP_EB_ENCAP,
  250. dhcp_features, dhcp_features_len ) ) !=0 ){
  251. DBG ( "DHCP could not set features list option: %s\n",
  252. strerror ( rc ) );
  253. return rc;
  254. }
  255. /* Add options to identify the network device */
  256. dhcp_desc.type = desc->bus_type;
  257. dhcp_desc.vendor = htons ( desc->vendor );
  258. dhcp_desc.device = htons ( desc->device );
  259. if ( ( rc = store_setting ( &dhcppkt->settings, DHCP_EB_BUS_ID,
  260. &dhcp_desc, sizeof ( dhcp_desc ) ) ) !=0 ){
  261. DBG ( "DHCP could not set bus ID option: %s\n",
  262. strerror ( rc ) );
  263. return rc;
  264. }
  265. /* Add DHCP client identifier. Required for Infiniband, and
  266. * doesn't hurt other link layers.
  267. */
  268. client_id.ll_proto = ntohs ( netdev->ll_protocol->ll_proto );
  269. ll_addr_len = netdev->ll_protocol->ll_addr_len;
  270. assert ( ll_addr_len <= sizeof ( client_id.ll_addr ) );
  271. memcpy ( client_id.ll_addr, netdev->ll_addr, ll_addr_len );
  272. if ( ( rc = store_setting ( &dhcppkt->settings, DHCP_CLIENT_ID,
  273. &client_id, ( ll_addr_len + 1 ) ) ) != 0 ){
  274. DBG ( "DHCP could not set client ID: %s\n",
  275. strerror ( rc ) );
  276. return rc;
  277. }
  278. /* Add client UUID, if we have one. Required for PXE. */
  279. client_uuid.type = DHCP_CLIENT_UUID_TYPE;
  280. if ( ( rc = get_uuid ( &client_uuid.uuid ) ) == 0 ) {
  281. if ( ( rc = store_setting ( &dhcppkt->settings,
  282. DHCP_CLIENT_UUID, &client_uuid,
  283. sizeof ( client_uuid ) ) ) != 0 ) {
  284. DBG ( "DHCP could not set client UUID: %s\n",
  285. strerror ( rc ) );
  286. return rc;
  287. }
  288. }
  289. return 0;
  290. }
  291. /**
  292. * Create DHCP response
  293. *
  294. * @v dhcppkt DHCP packet structure to fill in
  295. * @v netdev Network device
  296. * @v msgtype DHCP message type
  297. * @v settings Settings to include, or NULL
  298. * @v data Buffer for DHCP packet
  299. * @v max_len Size of DHCP packet buffer
  300. * @ret rc Return status code
  301. */
  302. int create_dhcp_response ( struct dhcp_packet *dhcppkt,
  303. struct net_device *netdev, int msgtype,
  304. struct settings *settings,
  305. void *data, size_t max_len ) {
  306. int rc;
  307. /* Create packet and copy in options */
  308. if ( ( rc = create_dhcp_packet ( dhcppkt, netdev, msgtype, NULL,
  309. data, max_len ) ) != 0 )
  310. return rc;
  311. if ( ( rc = copy_settings ( &dhcppkt->settings, settings ) ) != 0 )
  312. return rc;
  313. return 0;
  314. }
  315. /****************************************************************************
  316. *
  317. * DHCP packets contained in I/O buffers
  318. *
  319. */
  320. /** A DHCP packet contained in an I/O buffer */
  321. struct dhcp_iobuf_packet {
  322. /** Reference counter */
  323. struct refcnt refcnt;
  324. /** DHCP packet */
  325. struct dhcp_packet dhcppkt;
  326. /** Containing I/O buffer */
  327. struct io_buffer *iobuf;
  328. };
  329. /**
  330. * Free DHCP packet contained in an I/O buffer
  331. *
  332. * @v refcnt Reference counter
  333. */
  334. static void dhcpiob_free ( struct refcnt *refcnt ) {
  335. struct dhcp_iobuf_packet *dhcpiob =
  336. container_of ( refcnt, struct dhcp_iobuf_packet, refcnt );
  337. free_iob ( dhcpiob->iobuf );
  338. free ( dhcpiob );
  339. }
  340. /**
  341. * Create DHCP packet from I/O buffer
  342. *
  343. * @v iobuf I/O buffer
  344. * @ret dhcpiob DHCP packet contained in I/O buffer
  345. *
  346. * This function takes ownership of the I/O buffer. Future accesses
  347. * must be via the @c dhcpiob data structure.
  348. */
  349. static struct dhcp_iobuf_packet * dhcpiob_create ( struct io_buffer *iobuf ) {
  350. struct dhcp_iobuf_packet *dhcpiob;
  351. dhcpiob = zalloc ( sizeof ( *dhcpiob ) );
  352. if ( dhcpiob ) {
  353. dhcpiob->refcnt.free = dhcpiob_free;
  354. dhcpiob->iobuf = iobuf;
  355. dhcppkt_init ( &dhcpiob->dhcppkt, &dhcpiob->refcnt,
  356. iobuf->data, iob_len ( iobuf ) );
  357. }
  358. return dhcpiob;
  359. }
  360. static void dhcpiob_put ( struct dhcp_iobuf_packet *dhcpiob ) {
  361. if ( dhcpiob )
  362. ref_put ( &dhcpiob->refcnt );
  363. }
  364. /****************************************************************************
  365. *
  366. * DHCP to UDP interface
  367. *
  368. */
  369. /** A DHCP session */
  370. struct dhcp_session {
  371. /** Reference counter */
  372. struct refcnt refcnt;
  373. /** Job control interface */
  374. struct job_interface job;
  375. /** Data transfer interface */
  376. struct xfer_interface xfer;
  377. /** Network device being configured */
  378. struct net_device *netdev;
  379. /** State of the session
  380. *
  381. * This is a value for the @c DHCP_MESSAGE_TYPE option
  382. * (e.g. @c DHCPDISCOVER).
  383. */
  384. int state;
  385. /** Response obtained from DHCP server */
  386. struct dhcp_iobuf_packet *response;
  387. /** Response obtained from ProxyDHCP server */
  388. struct dhcp_iobuf_packet *proxy_response;
  389. /** Retransmission timer */
  390. struct retry_timer timer;
  391. /** Session start time (in ticks) */
  392. unsigned long start;
  393. };
  394. /**
  395. * Free DHCP session
  396. *
  397. * @v refcnt Reference counter
  398. */
  399. static void dhcp_free ( struct refcnt *refcnt ) {
  400. struct dhcp_session *dhcp =
  401. container_of ( refcnt, struct dhcp_session, refcnt );
  402. netdev_put ( dhcp->netdev );
  403. dhcpiob_put ( dhcp->response );
  404. dhcpiob_put ( dhcp->proxy_response );
  405. free ( dhcp );
  406. }
  407. /**
  408. * Mark DHCP session as complete
  409. *
  410. * @v dhcp DHCP session
  411. * @v rc Return status code
  412. */
  413. static void dhcp_finished ( struct dhcp_session *dhcp, int rc ) {
  414. /* Block futher incoming messages */
  415. job_nullify ( &dhcp->job );
  416. xfer_nullify ( &dhcp->xfer );
  417. /* Stop retry timer */
  418. stop_timer ( &dhcp->timer );
  419. /* Free resources and close interfaces */
  420. xfer_close ( &dhcp->xfer, rc );
  421. job_done ( &dhcp->job, rc );
  422. }
  423. /**
  424. * Register options received via DHCP
  425. *
  426. * @v dhcp DHCP session
  427. * @ret rc Return status code
  428. */
  429. static int dhcp_register_settings ( struct dhcp_session *dhcp ) {
  430. struct settings *settings;
  431. struct settings *parent;
  432. int rc;
  433. if ( dhcp->proxy_response ) {
  434. settings = &dhcp->proxy_response->dhcppkt.settings;
  435. if ( ( rc = register_settings ( settings, NULL ) ) != 0 )
  436. return rc;
  437. }
  438. settings = &dhcp->response->dhcppkt.settings;
  439. parent = netdev_settings ( dhcp->netdev );
  440. if ( ( rc = register_settings ( settings, parent ) ) != 0 )
  441. return rc;
  442. return 0;
  443. }
  444. /****************************************************************************
  445. *
  446. * Data transfer interface
  447. *
  448. */
  449. /**
  450. * Transmit DHCP request
  451. *
  452. * @v dhcp DHCP session
  453. * @ret rc Return status code
  454. */
  455. static int dhcp_send_request ( struct dhcp_session *dhcp ) {
  456. struct xfer_metadata meta = {
  457. .netdev = dhcp->netdev,
  458. };
  459. struct settings *offer_settings = NULL;
  460. struct io_buffer *iobuf;
  461. struct dhcp_packet dhcppkt;
  462. int rc;
  463. DBGC ( dhcp, "DHCP %p transmitting %s\n",
  464. dhcp, dhcp_msgtype_name ( dhcp->state ) );
  465. assert ( ( dhcp->state == DHCPDISCOVER ) ||
  466. ( dhcp->state == DHCPREQUEST ) );
  467. /* Start retry timer. Do this first so that failures to
  468. * transmit will be retried.
  469. */
  470. start_timer ( &dhcp->timer );
  471. /* Allocate buffer for packet */
  472. iobuf = xfer_alloc_iob ( &dhcp->xfer, DHCP_MIN_LEN );
  473. if ( ! iobuf )
  474. return -ENOMEM;
  475. /* Create DHCP packet in temporary buffer */
  476. if ( dhcp->response )
  477. offer_settings = &dhcp->response->dhcppkt.settings;
  478. if ( ( rc = create_dhcp_request ( &dhcppkt, dhcp->netdev, dhcp->state,
  479. offer_settings, iobuf->data,
  480. iob_tailroom ( iobuf ) ) ) != 0 ) {
  481. DBGC ( dhcp, "DHCP %p could not construct DHCP request: %s\n",
  482. dhcp, strerror ( rc ) );
  483. goto done;
  484. }
  485. /* Transmit the packet */
  486. iob_put ( iobuf, dhcppkt.len );
  487. rc = xfer_deliver_iob_meta ( &dhcp->xfer, iobuf, &meta );
  488. iobuf = NULL;
  489. if ( rc != 0 ) {
  490. DBGC ( dhcp, "DHCP %p could not transmit UDP packet: %s\n",
  491. dhcp, strerror ( rc ) );
  492. goto done;
  493. }
  494. done:
  495. free_iob ( iobuf );
  496. return rc;
  497. }
  498. /**
  499. * Handle DHCP retry timer expiry
  500. *
  501. * @v timer DHCP retry timer
  502. * @v fail Failure indicator
  503. */
  504. static void dhcp_timer_expired ( struct retry_timer *timer, int fail ) {
  505. struct dhcp_session *dhcp =
  506. container_of ( timer, struct dhcp_session, timer );
  507. if ( fail ) {
  508. dhcp_finished ( dhcp, -ETIMEDOUT );
  509. } else {
  510. dhcp_send_request ( dhcp );
  511. }
  512. }
  513. /**
  514. * Receive new data
  515. *
  516. * @v xfer Data transfer interface
  517. * @v iobuf I/O buffer
  518. * @v data Received data
  519. * @v len Length of received data
  520. * @ret rc Return status code
  521. */
  522. static int dhcp_deliver_iob ( struct xfer_interface *xfer,
  523. struct io_buffer *iobuf,
  524. struct xfer_metadata *meta __unused ) {
  525. struct dhcp_session *dhcp =
  526. container_of ( xfer, struct dhcp_session, xfer );
  527. struct dhcp_iobuf_packet *response;
  528. struct dhcp_iobuf_packet **store_response;
  529. struct dhcphdr *dhcphdr;
  530. struct settings *settings;
  531. unsigned int msgtype;
  532. unsigned long elapsed;
  533. int is_proxy;
  534. int ignore_proxy;
  535. int rc;
  536. /* Convert packet into a DHCP-packet-in-iobuf */
  537. response = dhcpiob_create ( iobuf );
  538. if ( ! response ) {
  539. DBGC ( dhcp, "DHCP %p could not store DHCP packet\n", dhcp );
  540. return -ENOMEM;
  541. }
  542. dhcphdr = response->dhcppkt.dhcphdr;
  543. settings = &response->dhcppkt.settings;
  544. /* Check for matching transaction ID */
  545. if ( dhcphdr->xid != dhcp_xid ( dhcp->netdev ) ) {
  546. DBGC ( dhcp, "DHCP %p wrong transaction ID (wanted %08lx, "
  547. "got %08lx)\n", dhcp, ntohl ( dhcphdr->xid ),
  548. ntohl ( dhcp_xid ( dhcp->netdev ) ) );
  549. goto out_discard;
  550. };
  551. /* Determine and verify message type */
  552. is_proxy = ( dhcphdr->yiaddr.s_addr == 0 );
  553. msgtype = fetch_uintz_setting ( settings, DHCP_MESSAGE_TYPE );
  554. DBGC ( dhcp, "DHCP %p received %s%s\n", dhcp,
  555. ( is_proxy ? "Proxy" : "" ), dhcp_msgtype_name ( msgtype ) );
  556. if ( ( ( dhcp->state != DHCPDISCOVER ) || ( msgtype != DHCPOFFER ) ) &&
  557. ( ( dhcp->state != DHCPREQUEST ) || ( msgtype != DHCPACK ) ) ) {
  558. DBGC ( dhcp, "DHCP %p discarding %s while in %s state\n",
  559. dhcp, dhcp_msgtype_name ( msgtype ),
  560. dhcp_msgtype_name ( dhcp->state ) );
  561. goto out_discard;
  562. }
  563. /* Update stored standard/ProxyDHCP options, if the new
  564. * options have equal or higher priority than the
  565. * currently-stored options.
  566. */
  567. store_response = ( is_proxy ? &dhcp->proxy_response : &dhcp->response);
  568. if ( ( ! *store_response ) ||
  569. ( fetch_uintz_setting ( settings, DHCP_EB_PRIORITY ) >=
  570. fetch_uintz_setting ( &(*store_response)->dhcppkt.settings,
  571. DHCP_EB_PRIORITY ) ) ) {
  572. dhcpiob_put ( *store_response );
  573. *store_response = response;
  574. } else {
  575. dhcpiob_put ( response );
  576. }
  577. /* If we don't yet have a standard DHCP response (i.e. one
  578. * with an IP address), then just leave the timer running.
  579. */
  580. if ( ! dhcp->response )
  581. goto out;
  582. /* Handle DHCP response */
  583. ignore_proxy = fetch_uintz_setting ( &dhcp->response->dhcppkt.settings,
  584. DHCP_EB_NO_PROXYDHCP );
  585. switch ( dhcp->state ) {
  586. case DHCPDISCOVER:
  587. /* If we have allowed sufficient time for ProxyDHCP
  588. * reponses, then transition to making the DHCPREQUEST.
  589. */
  590. elapsed = ( currticks() - dhcp->start );
  591. if ( ignore_proxy || ( elapsed > PROXYDHCP_WAIT_TIME ) ) {
  592. stop_timer ( &dhcp->timer );
  593. dhcp->state = DHCPREQUEST;
  594. dhcp_send_request ( dhcp );
  595. }
  596. break;
  597. case DHCPREQUEST:
  598. /* DHCP finished; register options and exit */
  599. if ( ignore_proxy && dhcp->proxy_response ) {
  600. dhcpiob_put ( dhcp->proxy_response );
  601. dhcp->proxy_response = NULL;
  602. }
  603. if ( ( rc = dhcp_register_settings ( dhcp ) ) != 0 ) {
  604. dhcp_finished ( dhcp, rc );
  605. break;
  606. }
  607. dhcp_finished ( dhcp, 0 );
  608. break;
  609. default:
  610. assert ( 0 );
  611. }
  612. out:
  613. return 0;
  614. out_discard:
  615. dhcpiob_put ( response );
  616. return 0;
  617. }
  618. /** DHCP data transfer interface operations */
  619. static struct xfer_interface_operations dhcp_xfer_operations = {
  620. .close = ignore_xfer_close,
  621. .vredirect = xfer_vopen,
  622. .window = unlimited_xfer_window,
  623. .alloc_iob = default_xfer_alloc_iob,
  624. .deliver_iob = dhcp_deliver_iob,
  625. .deliver_raw = xfer_deliver_as_iob,
  626. };
  627. /****************************************************************************
  628. *
  629. * Job control interface
  630. *
  631. */
  632. /**
  633. * Handle kill() event received via job control interface
  634. *
  635. * @v job DHCP job control interface
  636. */
  637. static void dhcp_job_kill ( struct job_interface *job ) {
  638. struct dhcp_session *dhcp =
  639. container_of ( job, struct dhcp_session, job );
  640. /* Terminate DHCP session */
  641. dhcp_finished ( dhcp, -ECANCELED );
  642. }
  643. /** DHCP job control interface operations */
  644. static struct job_interface_operations dhcp_job_operations = {
  645. .done = ignore_job_done,
  646. .kill = dhcp_job_kill,
  647. .progress = ignore_job_progress,
  648. };
  649. /****************************************************************************
  650. *
  651. * Instantiator
  652. *
  653. */
  654. /**
  655. * Start DHCP on a network device
  656. *
  657. * @v job Job control interface
  658. * @v netdev Network device
  659. * @v register_options DHCP option block registration routine
  660. * @ret rc Return status code
  661. *
  662. * Starts DHCP on the specified network device. If successful, the @c
  663. * register_options() routine will be called with the acquired
  664. * options.
  665. */
  666. int start_dhcp ( struct job_interface *job, struct net_device *netdev ) {
  667. static struct sockaddr_in server = {
  668. .sin_family = AF_INET,
  669. .sin_addr.s_addr = INADDR_BROADCAST,
  670. .sin_port = htons ( BOOTPS_PORT ),
  671. };
  672. static struct sockaddr_in client = {
  673. .sin_family = AF_INET,
  674. .sin_port = htons ( BOOTPC_PORT ),
  675. };
  676. struct dhcp_session *dhcp;
  677. int rc;
  678. /* Allocate and initialise structure */
  679. dhcp = zalloc ( sizeof ( *dhcp ) );
  680. if ( ! dhcp )
  681. return -ENOMEM;
  682. dhcp->refcnt.free = dhcp_free;
  683. job_init ( &dhcp->job, &dhcp_job_operations, &dhcp->refcnt );
  684. xfer_init ( &dhcp->xfer, &dhcp_xfer_operations, &dhcp->refcnt );
  685. dhcp->netdev = netdev_get ( netdev );
  686. dhcp->timer.expired = dhcp_timer_expired;
  687. dhcp->state = DHCPDISCOVER;
  688. dhcp->start = currticks();
  689. /* Instantiate child objects and attach to our interfaces */
  690. if ( ( rc = xfer_open_socket ( &dhcp->xfer, SOCK_DGRAM,
  691. ( struct sockaddr * ) &server,
  692. ( struct sockaddr * ) &client ) ) != 0 )
  693. goto err;
  694. /* Start timer to initiate initial DHCPREQUEST */
  695. start_timer_nodelay ( &dhcp->timer );
  696. /* Attach parent interface, mortalise self, and return */
  697. job_plug_plug ( &dhcp->job, job );
  698. ref_put ( &dhcp->refcnt );
  699. return 0;
  700. err:
  701. dhcp_finished ( dhcp, rc );
  702. ref_put ( &dhcp->refcnt );
  703. return rc;
  704. }