您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

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