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

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