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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027
  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. /** DHCP network device descriptor */
  89. struct dhcp_netdev_desc {
  90. /** Bus type ID */
  91. uint8_t type;
  92. /** Vendor ID */
  93. uint16_t vendor;
  94. /** Device ID */
  95. uint16_t device;
  96. } __attribute__ (( packed ));
  97. /** DHCP client identifier */
  98. struct dhcp_client_id {
  99. /** Link-layer protocol */
  100. uint8_t ll_proto;
  101. /** Link-layer address */
  102. uint8_t ll_addr[MAX_LL_ADDR_LEN];
  103. } __attribute__ (( packed ));
  104. /** DHCP client UUID */
  105. struct dhcp_client_uuid {
  106. /** Identifier type */
  107. uint8_t type;
  108. /** UUID */
  109. union uuid uuid;
  110. } __attribute__ (( packed ));
  111. #define DHCP_CLIENT_UUID_TYPE 0
  112. /**
  113. * Name a DHCP packet type
  114. *
  115. * @v msgtype DHCP message type
  116. * @ret string DHCP mesasge type name
  117. */
  118. static inline const char * dhcp_msgtype_name ( unsigned int msgtype ) {
  119. switch ( msgtype ) {
  120. case 0: return "BOOTP"; /* Non-DHCP packet */
  121. case DHCPDISCOVER: return "DHCPDISCOVER";
  122. case DHCPOFFER: return "DHCPOFFER";
  123. case DHCPREQUEST: return "DHCPREQUEST";
  124. case DHCPDECLINE: return "DHCPDECLINE";
  125. case DHCPACK: return "DHCPACK";
  126. case DHCPNAK: return "DHCPNAK";
  127. case DHCPRELEASE: return "DHCPRELEASE";
  128. case DHCPINFORM: return "DHCPINFORM";
  129. default: return "DHCP<invalid>";
  130. }
  131. }
  132. /**
  133. * Calculate DHCP transaction ID for a network device
  134. *
  135. * @v netdev Network device
  136. * @ret xid DHCP XID
  137. *
  138. * Extract the least significant bits of the hardware address for use
  139. * as the transaction ID.
  140. */
  141. static uint32_t dhcp_xid ( struct net_device *netdev ) {
  142. uint32_t xid;
  143. memcpy ( &xid, ( netdev->ll_addr + netdev->ll_protocol->ll_addr_len
  144. - sizeof ( xid ) ), sizeof ( xid ) );
  145. return xid;
  146. }
  147. /****************************************************************************
  148. *
  149. * DHCP settings
  150. *
  151. */
  152. /** A DHCP settings block */
  153. struct dhcp_settings {
  154. /** Reference counter */
  155. struct refcnt refcnt;
  156. /** DHCP packet */
  157. struct dhcp_packet dhcppkt;
  158. /** Setting interface */
  159. struct settings settings;
  160. };
  161. /**
  162. * Increment reference count on DHCP settings block
  163. *
  164. * @v dhcpset DHCP settings block
  165. * @ret dhcpset DHCP settings block
  166. */
  167. static inline __attribute__ (( always_inline )) struct dhcp_settings *
  168. dhcpset_get ( struct dhcp_settings *dhcpset ) {
  169. ref_get ( &dhcpset->refcnt );
  170. return dhcpset;
  171. }
  172. /**
  173. * Decrement reference count on DHCP settings block
  174. *
  175. * @v dhcpset DHCP settings block
  176. */
  177. static inline __attribute__ (( always_inline )) void
  178. dhcpset_put ( struct dhcp_settings *dhcpset ) {
  179. ref_put ( &dhcpset->refcnt );
  180. }
  181. /**
  182. * Store value of DHCP setting
  183. *
  184. * @v settings Settings block
  185. * @v setting Setting to store
  186. * @v data Setting data, or NULL to clear setting
  187. * @v len Length of setting data
  188. * @ret rc Return status code
  189. */
  190. static int dhcpset_store ( struct settings *settings, struct setting *setting,
  191. const void *data, size_t len ) {
  192. struct dhcp_settings *dhcpset =
  193. container_of ( settings, struct dhcp_settings, settings );
  194. return dhcppkt_store ( &dhcpset->dhcppkt, setting->tag, data, len );
  195. }
  196. /**
  197. * Fetch value of DHCP setting
  198. *
  199. * @v settings Settings block, or NULL to search all blocks
  200. * @v setting Setting to fetch
  201. * @v data Buffer to fill with setting data
  202. * @v len Length of buffer
  203. * @ret len Length of setting data, or negative error
  204. */
  205. static int dhcpset_fetch ( struct settings *settings, struct setting *setting,
  206. void *data, size_t len ) {
  207. struct dhcp_settings *dhcpset =
  208. container_of ( settings, struct dhcp_settings, settings );
  209. return dhcppkt_fetch ( &dhcpset->dhcppkt, setting->tag, data, len );
  210. }
  211. /** DHCP settings operations */
  212. static struct settings_operations dhcpset_settings_operations = {
  213. .store = dhcpset_store,
  214. .fetch = dhcpset_fetch,
  215. };
  216. /**
  217. * Create DHCP setting block
  218. *
  219. * @v dhcphdr DHCP packet
  220. * @v len Length of DHCP packet
  221. * @ret dhcpset DHCP settings block
  222. */
  223. static struct dhcp_settings * dhcpset_create ( const struct dhcphdr *dhcphdr,
  224. size_t len ) {
  225. struct dhcp_settings *dhcpset;
  226. void *data;
  227. dhcpset = zalloc ( sizeof ( *dhcpset ) + len );
  228. if ( dhcpset ) {
  229. data = ( ( ( void * ) dhcpset ) + sizeof ( *dhcpset ) );
  230. memcpy ( data, dhcphdr, len );
  231. dhcppkt_init ( &dhcpset->dhcppkt, data, len );
  232. settings_init ( &dhcpset->settings,
  233. &dhcpset_settings_operations, &dhcpset->refcnt,
  234. DHCP_SETTINGS_NAME );
  235. }
  236. return dhcpset;
  237. }
  238. /****************************************************************************
  239. *
  240. * DHCP session
  241. *
  242. */
  243. /** DHCP session states */
  244. enum dhcp_session_state {
  245. /** Sending DHCPDISCOVERs, collecting DHCPOFFERs and ProxyDHCPOFFERs */
  246. DHCP_STATE_DISCOVER = 0,
  247. /** Sending DHCPREQUESTs, waiting for DHCPACK */
  248. DHCP_STATE_REQUEST,
  249. /** Sending ProxyDHCPREQUESTs, waiting for ProxyDHCPACK */
  250. DHCP_STATE_PROXYREQUEST,
  251. };
  252. /**
  253. * Name a DHCP session state
  254. *
  255. * @v state DHCP session state
  256. * @ret string DHCP session state name
  257. */
  258. static inline const char * dhcp_state_name ( enum dhcp_session_state state ) {
  259. switch ( state ) {
  260. case DHCP_STATE_DISCOVER: return "DHCPDISCOVER";
  261. case DHCP_STATE_REQUEST: return "DHCPREQUEST";
  262. case DHCP_STATE_PROXYREQUEST: return "ProxyDHCPREQUEST";
  263. default: return "<invalid>";
  264. }
  265. }
  266. /** A DHCP session */
  267. struct dhcp_session {
  268. /** Reference counter */
  269. struct refcnt refcnt;
  270. /** Job control interface */
  271. struct job_interface job;
  272. /** Data transfer interface */
  273. struct xfer_interface xfer;
  274. /** Network device being configured */
  275. struct net_device *netdev;
  276. /** State of the session
  277. *
  278. * This is a value for the @c DHCP_MESSAGE_TYPE option
  279. * (e.g. @c DHCPDISCOVER).
  280. */
  281. enum dhcp_session_state state;
  282. /** DHCPOFFER obtained during DHCPDISCOVER */
  283. struct dhcp_settings *dhcpoffer;
  284. /** ProxyDHCPOFFER obtained during DHCPDISCOVER */
  285. struct dhcp_settings *proxydhcpoffer;
  286. /** Retransmission timer */
  287. struct retry_timer timer;
  288. /** Start time of the current state (in ticks) */
  289. unsigned long start;
  290. };
  291. /**
  292. * Free DHCP session
  293. *
  294. * @v refcnt Reference counter
  295. */
  296. static void dhcp_free ( struct refcnt *refcnt ) {
  297. struct dhcp_session *dhcp =
  298. container_of ( refcnt, struct dhcp_session, refcnt );
  299. netdev_put ( dhcp->netdev );
  300. dhcpset_put ( dhcp->dhcpoffer );
  301. dhcpset_put ( dhcp->proxydhcpoffer );
  302. free ( dhcp );
  303. }
  304. /**
  305. * Mark DHCP session as complete
  306. *
  307. * @v dhcp DHCP session
  308. * @v rc Return status code
  309. */
  310. static void dhcp_finished ( struct dhcp_session *dhcp, int rc ) {
  311. /* Block futher incoming messages */
  312. job_nullify ( &dhcp->job );
  313. xfer_nullify ( &dhcp->xfer );
  314. /* Stop retry timer */
  315. stop_timer ( &dhcp->timer );
  316. /* Free resources and close interfaces */
  317. xfer_close ( &dhcp->xfer, rc );
  318. job_done ( &dhcp->job, rc );
  319. }
  320. /****************************************************************************
  321. *
  322. * Data transfer interface
  323. *
  324. */
  325. /**
  326. * Create a DHCP packet
  327. *
  328. * @v dhcppkt DHCP packet structure to fill in
  329. * @v netdev Network device
  330. * @v msgtype DHCP message type
  331. * @v options Initial options to include (or NULL)
  332. * @v data Buffer for DHCP packet
  333. * @v max_len Size of DHCP packet buffer
  334. * @ret rc Return status code
  335. *
  336. * Creates a DHCP packet in the specified buffer, and fills out a @c
  337. * dhcp_packet structure.
  338. */
  339. int dhcp_create_packet ( struct dhcp_packet *dhcppkt,
  340. struct net_device *netdev, uint8_t msgtype,
  341. struct dhcp_options *options,
  342. void *data, size_t max_len ) {
  343. struct dhcphdr *dhcphdr = data;
  344. size_t options_len;
  345. unsigned int hlen;
  346. int rc;
  347. /* Sanity check */
  348. options_len = ( options ? options->len : 0 );
  349. if ( max_len < ( sizeof ( *dhcphdr ) + options_len ) )
  350. return -ENOSPC;
  351. /* Initialise DHCP packet content */
  352. memset ( dhcphdr, 0, max_len );
  353. dhcphdr->xid = dhcp_xid ( netdev );
  354. dhcphdr->magic = htonl ( DHCP_MAGIC_COOKIE );
  355. dhcphdr->htype = ntohs ( netdev->ll_protocol->ll_proto );
  356. dhcphdr->op = dhcp_op[msgtype];
  357. /* If hardware length exceeds the chaddr field length, don't
  358. * use the chaddr field. This is as per RFC4390.
  359. */
  360. hlen = netdev->ll_protocol->ll_addr_len;
  361. if ( hlen > sizeof ( dhcphdr->chaddr ) ) {
  362. hlen = 0;
  363. dhcphdr->flags = htons ( BOOTP_FL_BROADCAST );
  364. }
  365. dhcphdr->hlen = hlen;
  366. memcpy ( dhcphdr->chaddr, netdev->ll_addr, hlen );
  367. memcpy ( dhcphdr->options, options->data, options_len );
  368. /* Initialise DHCP packet structure */
  369. memset ( dhcppkt, 0, sizeof ( *dhcppkt ) );
  370. dhcppkt_init ( dhcppkt, data, max_len );
  371. /* Set DHCP_MESSAGE_TYPE option */
  372. if ( ( rc = dhcppkt_store ( dhcppkt, DHCP_MESSAGE_TYPE,
  373. &msgtype, sizeof ( msgtype ) ) ) != 0 )
  374. return rc;
  375. return 0;
  376. }
  377. /**
  378. * Create DHCP request packet
  379. *
  380. * @v dhcppkt DHCP packet structure to fill in
  381. * @v netdev Network device
  382. * @v ciaddr Client IP address
  383. * @v offer DHCP offer, if applicable
  384. * @v data Buffer for DHCP packet
  385. * @v max_len Size of DHCP packet buffer
  386. * @ret rc Return status code
  387. */
  388. int dhcp_create_request ( struct dhcp_packet *dhcppkt,
  389. struct net_device *netdev, struct in_addr ciaddr,
  390. struct dhcp_packet *offer,
  391. void *data, size_t max_len ) {
  392. struct device_description *desc = &netdev->dev->desc;
  393. struct dhcp_netdev_desc dhcp_desc;
  394. struct dhcp_client_id client_id;
  395. struct dhcp_client_uuid client_uuid;
  396. unsigned int msgtype;
  397. size_t dhcp_features_len;
  398. size_t ll_addr_len;
  399. int rc;
  400. /* Create DHCP packet */
  401. msgtype = ( offer ? DHCPREQUEST : DHCPDISCOVER );
  402. if ( ( rc = dhcp_create_packet ( dhcppkt, netdev, msgtype,
  403. &dhcp_request_options, data,
  404. max_len ) ) != 0 ) {
  405. DBG ( "DHCP could not create DHCP packet: %s\n",
  406. strerror ( rc ) );
  407. return rc;
  408. }
  409. /* Set client IP address */
  410. dhcppkt->dhcphdr->ciaddr = ciaddr;
  411. /* Copy any required options from previous server repsonse */
  412. if ( offer ) {
  413. struct in_addr server = { 0 };
  414. struct in_addr *ip = &offer->dhcphdr->yiaddr;
  415. /* Copy server identifier, if present */
  416. if ( ( dhcppkt_fetch ( offer, DHCP_SERVER_IDENTIFIER, &server,
  417. sizeof ( server ) ) >= 0 ) &&
  418. ( ( rc = dhcppkt_store ( dhcppkt, DHCP_SERVER_IDENTIFIER,
  419. &server,
  420. sizeof ( server ) ) ) != 0 ) ) {
  421. DBG ( "DHCP could not set server ID: %s\n",
  422. strerror ( rc ) );
  423. return rc;
  424. }
  425. /* Copy requested IP address, if present */
  426. if ( ( ip->s_addr != 0 ) &&
  427. ( ( rc = dhcppkt_store ( dhcppkt, DHCP_REQUESTED_ADDRESS,
  428. ip, sizeof ( *ip ) ) ) != 0 ) ) {
  429. DBG ( "DHCP could not set requested address: %s\n",
  430. strerror ( rc ) );
  431. return rc;
  432. }
  433. }
  434. /* Add options to identify the feature list */
  435. dhcp_features_len = ( dhcp_features_end - dhcp_features );
  436. if ( ( rc = dhcppkt_store ( dhcppkt, DHCP_EB_ENCAP, dhcp_features,
  437. dhcp_features_len ) ) != 0 ) {
  438. DBG ( "DHCP could not set features list option: %s\n",
  439. strerror ( rc ) );
  440. return rc;
  441. }
  442. /* Add options to identify the network device */
  443. dhcp_desc.type = desc->bus_type;
  444. dhcp_desc.vendor = htons ( desc->vendor );
  445. dhcp_desc.device = htons ( desc->device );
  446. if ( ( rc = dhcppkt_store ( dhcppkt, DHCP_EB_BUS_ID, &dhcp_desc,
  447. sizeof ( dhcp_desc ) ) ) != 0 ) {
  448. DBG ( "DHCP could not set bus ID option: %s\n",
  449. strerror ( rc ) );
  450. return rc;
  451. }
  452. /* Add DHCP client identifier. Required for Infiniband, and
  453. * doesn't hurt other link layers.
  454. */
  455. client_id.ll_proto = ntohs ( netdev->ll_protocol->ll_proto );
  456. ll_addr_len = netdev->ll_protocol->ll_addr_len;
  457. assert ( ll_addr_len <= sizeof ( client_id.ll_addr ) );
  458. memcpy ( client_id.ll_addr, netdev->ll_addr, ll_addr_len );
  459. if ( ( rc = dhcppkt_store ( dhcppkt, DHCP_CLIENT_ID, &client_id,
  460. ( ll_addr_len + 1 ) ) ) != 0 ) {
  461. DBG ( "DHCP could not set client ID: %s\n",
  462. strerror ( rc ) );
  463. return rc;
  464. }
  465. /* Add client UUID, if we have one. Required for PXE. */
  466. client_uuid.type = DHCP_CLIENT_UUID_TYPE;
  467. if ( ( rc = fetch_uuid_setting ( NULL, &uuid_setting,
  468. &client_uuid.uuid ) ) >= 0 ) {
  469. if ( ( rc = dhcppkt_store ( dhcppkt, DHCP_CLIENT_UUID,
  470. &client_uuid,
  471. sizeof ( client_uuid ) ) ) != 0 ) {
  472. DBG ( "DHCP could not set client UUID: %s\n",
  473. strerror ( rc ) );
  474. return rc;
  475. }
  476. }
  477. return 0;
  478. }
  479. /**
  480. * Transmit DHCP request
  481. *
  482. * @v dhcp DHCP session
  483. * @ret rc Return status code
  484. */
  485. static int dhcp_tx ( struct dhcp_session *dhcp ) {
  486. static struct sockaddr_in proxydhcp_server = {
  487. .sin_family = AF_INET,
  488. .sin_port = htons ( PROXYDHCP_PORT ),
  489. };
  490. struct xfer_metadata meta = {
  491. .netdev = dhcp->netdev,
  492. };
  493. struct io_buffer *iobuf;
  494. struct dhcp_packet dhcppkt;
  495. struct dhcp_packet *offer = NULL;
  496. struct in_addr ciaddr = { 0 };
  497. int check_len;
  498. int rc;
  499. /* Start retry timer. Do this first so that failures to
  500. * transmit will be retried.
  501. */
  502. start_timer ( &dhcp->timer );
  503. /* Determine packet contents based on current state */
  504. switch ( dhcp->state ) {
  505. case DHCP_STATE_DISCOVER:
  506. DBGC ( dhcp, "DHCP %p transmitting DHCPDISCOVER\n", dhcp );
  507. break;
  508. case DHCP_STATE_REQUEST:
  509. DBGC ( dhcp, "DHCP %p transmitting DHCPREQUEST\n", dhcp );
  510. assert ( dhcp->dhcpoffer );
  511. offer = &dhcp->dhcpoffer->dhcppkt;
  512. break;
  513. case DHCP_STATE_PROXYREQUEST:
  514. DBGC ( dhcp, "DHCP %p transmitting ProxyDHCPREQUEST\n", dhcp );
  515. assert ( dhcp->dhcpoffer );
  516. assert ( dhcp->proxydhcpoffer );
  517. offer = &dhcp->proxydhcpoffer->dhcppkt;
  518. ciaddr = dhcp->dhcpoffer->dhcppkt.dhcphdr->yiaddr;
  519. check_len = dhcppkt_fetch ( offer, DHCP_SERVER_IDENTIFIER,
  520. &proxydhcp_server.sin_addr,
  521. sizeof(proxydhcp_server.sin_addr));
  522. meta.dest = ( struct sockaddr * ) &proxydhcp_server;
  523. assert ( ciaddr.s_addr != 0 );
  524. assert ( proxydhcp_server.sin_addr.s_addr != 0 );
  525. assert ( check_len == sizeof ( proxydhcp_server.sin_addr ) );
  526. break;
  527. default:
  528. assert ( 0 );
  529. break;
  530. }
  531. /* Allocate buffer for packet */
  532. iobuf = xfer_alloc_iob ( &dhcp->xfer, DHCP_MIN_LEN );
  533. if ( ! iobuf )
  534. return -ENOMEM;
  535. /* Create DHCP packet in temporary buffer */
  536. if ( ( rc = dhcp_create_request ( &dhcppkt, dhcp->netdev,
  537. ciaddr, offer, iobuf->data,
  538. iob_tailroom ( iobuf ) ) ) != 0 ) {
  539. DBGC ( dhcp, "DHCP %p could not construct DHCP request: %s\n",
  540. dhcp, strerror ( rc ) );
  541. goto done;
  542. }
  543. /* Transmit the packet */
  544. iob_put ( iobuf, dhcppkt.len );
  545. rc = xfer_deliver_iob_meta ( &dhcp->xfer, iobuf, &meta );
  546. iobuf = NULL;
  547. if ( rc != 0 ) {
  548. DBGC ( dhcp, "DHCP %p could not transmit UDP packet: %s\n",
  549. dhcp, strerror ( rc ) );
  550. goto done;
  551. }
  552. done:
  553. free_iob ( iobuf );
  554. return rc;
  555. }
  556. /**
  557. * Transition to new DHCP session state
  558. *
  559. * @v dhcp DHCP session
  560. * @v state New session state
  561. */
  562. static void dhcp_set_state ( struct dhcp_session *dhcp,
  563. enum dhcp_session_state state ) {
  564. DBGC ( dhcp, "DHCP %p entering %s state\n",
  565. dhcp, dhcp_state_name ( state ) );
  566. dhcp->state = state;
  567. dhcp->start = currticks();
  568. start_timer_nodelay ( &dhcp->timer );
  569. }
  570. /**
  571. * Store received DHCPOFFER
  572. *
  573. * @v dhcp DHCP session
  574. * @v dhcpoffer Received DHCPOFFER
  575. * @v stored_dhcpoffer Location to store DHCPOFFER
  576. *
  577. * The DHCPOFFER will be stored in place of the existing stored
  578. * DHCPOFFER if its priority is equal to or greater than the stored
  579. * DHCPOFFER.
  580. */
  581. static void dhcp_store_dhcpoffer ( struct dhcp_session *dhcp,
  582. struct dhcp_settings *dhcpoffer,
  583. struct dhcp_settings **stored_dhcpoffer ) {
  584. uint8_t stored_priority = 0;
  585. uint8_t priority = 0;
  586. /* Get priorities of the two DHCPOFFERs */
  587. if ( *stored_dhcpoffer ) {
  588. dhcppkt_fetch ( &(*stored_dhcpoffer)->dhcppkt,
  589. DHCP_EB_PRIORITY, &stored_priority,
  590. sizeof ( stored_priority ) );
  591. }
  592. dhcppkt_fetch ( &dhcpoffer->dhcppkt, DHCP_EB_PRIORITY, &priority,
  593. sizeof ( priority ) );
  594. /* Replace stored offer only if priority is equal or greater */
  595. if ( priority >= stored_priority ) {
  596. if ( *stored_dhcpoffer ) {
  597. DBGC ( dhcp, "DHCP %p stored DHCPOFFER %p discarded\n",
  598. dhcp, *stored_dhcpoffer );
  599. }
  600. DBGC ( dhcp, "DHCP %p received DHCPOFFER %p stored\n",
  601. dhcp, dhcpoffer );
  602. dhcpset_put ( *stored_dhcpoffer );
  603. *stored_dhcpoffer = dhcpset_get ( dhcpoffer );
  604. }
  605. }
  606. /**
  607. * Handle received DHCPOFFER
  608. *
  609. * @v dhcp DHCP session
  610. * @v dhcpoffer Received DHCPOFFER
  611. */
  612. static void dhcp_rx_dhcpoffer ( struct dhcp_session *dhcp,
  613. struct dhcp_settings *dhcpoffer ) {
  614. char vci[9]; /* "PXEClient" */
  615. int len;
  616. uint8_t ignore_proxy = 0;
  617. unsigned long elapsed;
  618. /* Check for presence of DHCP server ID */
  619. if ( dhcppkt_fetch ( &dhcpoffer->dhcppkt, DHCP_SERVER_IDENTIFIER,
  620. NULL, 0 ) != sizeof ( struct in_addr ) ) {
  621. DBGC ( dhcp, "DHCP %p received DHCPOFFER %p missing server "
  622. "identifier\n", dhcp, dhcpoffer );
  623. return;
  624. }
  625. /* If there is an IP address, it's a normal DHCPOFFER */
  626. if ( dhcpoffer->dhcppkt.dhcphdr->yiaddr.s_addr != 0 ) {
  627. DBGC ( dhcp, "DHCP %p received DHCPOFFER %p has IP address\n",
  628. dhcp, dhcpoffer );
  629. dhcp_store_dhcpoffer ( dhcp, dhcpoffer, &dhcp->dhcpoffer );
  630. }
  631. /* If there is a "PXEClient" vendor class ID, it's a
  632. * ProxyDHCPOFFER. Note that it could be both a normal
  633. * DHCPOFFER and a ProxyDHCPOFFER.
  634. */
  635. len = dhcppkt_fetch ( &dhcpoffer->dhcppkt, DHCP_VENDOR_CLASS_ID,
  636. vci, sizeof ( vci ) );
  637. if ( ( len >= ( int ) sizeof ( vci ) ) &&
  638. ( strncmp ( "PXEClient", vci, sizeof ( vci ) ) == 0 ) ) {
  639. DBGC ( dhcp, "DHCP %p received DHCPOFFER %p is a "
  640. "ProxyDHCPOFFER\n", dhcp, dhcpoffer );
  641. dhcp_store_dhcpoffer ( dhcp, dhcpoffer,
  642. &dhcp->proxydhcpoffer );
  643. }
  644. /* We can transition to making the DHCPREQUEST when we have a
  645. * valid DHCPOFFER, and either:
  646. *
  647. * o The DHCPOFFER instructs us to not wait for ProxyDHCP, or
  648. * o We have a valid ProxyDHCPOFFER, or
  649. * o We have allowed sufficient time for ProxyDHCPOFFERs.
  650. */
  651. /* If we don't yet have a DHCPOFFER, do nothing */
  652. if ( ! dhcp->dhcpoffer )
  653. return;
  654. /* If the DHCPOFFER instructs us to ignore ProxyDHCP, discard
  655. * any ProxyDHCPOFFER
  656. */
  657. dhcppkt_fetch ( &dhcp->dhcpoffer->dhcppkt, DHCP_EB_NO_PROXYDHCP,
  658. &ignore_proxy, sizeof ( ignore_proxy ) );
  659. if ( ignore_proxy && dhcp->proxydhcpoffer ) {
  660. DBGC ( dhcp, "DHCP %p discarding ProxyDHCPOFFER\n", dhcp );
  661. dhcpset_put ( dhcp->proxydhcpoffer );
  662. dhcp->proxydhcpoffer = NULL;
  663. }
  664. /* If we can't yet transition to DHCPREQUEST, do nothing */
  665. elapsed = ( currticks() - dhcp->start );
  666. if ( ! ( ignore_proxy || dhcp->proxydhcpoffer ||
  667. ( elapsed > PROXYDHCP_WAIT_TIME ) ) )
  668. return;
  669. /* Transition to DHCPREQUEST */
  670. dhcp_set_state ( dhcp, DHCP_STATE_REQUEST );
  671. }
  672. /**
  673. * Store received DHCPACK
  674. *
  675. * @v dhcp DHCP session
  676. * @v dhcpack Received DHCPACK
  677. *
  678. * The DHCPACK will be registered as a settings block.
  679. */
  680. static int dhcp_store_dhcpack ( struct dhcp_session *dhcp,
  681. struct dhcp_settings *dhcpack,
  682. struct settings *parent ) {
  683. struct settings *settings = &dhcpack->settings;
  684. struct settings *old_settings;
  685. int rc;
  686. /* Unregister any old settings obtained via DHCP */
  687. if ( ( old_settings = find_child_settings ( parent, settings->name ) ))
  688. unregister_settings ( old_settings );
  689. /* Register new settings */
  690. if ( ( rc = register_settings ( settings, parent ) ) != 0 ) {
  691. DBGC ( dhcp, "DHCP %p could not register settings: %s\n",
  692. dhcp, strerror ( rc ) );
  693. dhcp_finished ( dhcp, rc ); /* This is a fatal error */
  694. return rc;
  695. }
  696. return 0;
  697. }
  698. /**
  699. * Handle received DHCPACK
  700. *
  701. * @v dhcp DHCP session
  702. * @v dhcpack Received DHCPACK
  703. */
  704. static void dhcp_rx_dhcpack ( struct dhcp_session *dhcp,
  705. struct dhcp_settings *dhcpack ) {
  706. struct settings *parent;
  707. struct in_addr offer_server_id = { 0 };
  708. struct in_addr ack_server_id = { 0 };
  709. int rc;
  710. /* Verify server ID matches */
  711. assert ( dhcp->dhcpoffer != NULL );
  712. dhcppkt_fetch ( &dhcp->dhcpoffer->dhcppkt, DHCP_SERVER_IDENTIFIER,
  713. &offer_server_id, sizeof ( offer_server_id ) );
  714. dhcppkt_fetch ( &dhcpack->dhcppkt, DHCP_SERVER_IDENTIFIER,
  715. &ack_server_id, sizeof ( ack_server_id ) );
  716. if ( offer_server_id.s_addr != ack_server_id.s_addr ) {
  717. DBGC ( dhcp, "DHCP %p ignoring DHCPACK with wrong server ID\n",
  718. dhcp );
  719. return;
  720. }
  721. /* Register settings */
  722. parent = netdev_settings ( dhcp->netdev );
  723. if ( ( rc = dhcp_store_dhcpack ( dhcp, dhcpack, parent ) ) !=0 )
  724. return;
  725. /* If we have a ProxyDHCPOFFER, transition to PROXYDHCPREQUEST */
  726. if ( dhcp->proxydhcpoffer ) {
  727. dhcp_set_state ( dhcp, DHCP_STATE_PROXYREQUEST );
  728. return;
  729. }
  730. /* Terminate DHCP */
  731. dhcp_finished ( dhcp, 0 );
  732. }
  733. /**
  734. * Handle received ProxyDHCPACK
  735. *
  736. * @v dhcp DHCP session
  737. * @v proxydhcpack Received ProxyDHCPACK
  738. */
  739. static void dhcp_rx_proxydhcpack ( struct dhcp_session *dhcp,
  740. struct dhcp_settings *proxydhcpack ) {
  741. int rc;
  742. /* Rename settings */
  743. proxydhcpack->settings.name = PROXYDHCP_SETTINGS_NAME;
  744. /* Register settings */
  745. if ( ( rc = dhcp_store_dhcpack ( dhcp, proxydhcpack, NULL ) ) != 0 )
  746. return;
  747. /* Terminate DHCP */
  748. dhcp_finished ( dhcp, 0 );
  749. }
  750. /**
  751. * Receive new data
  752. *
  753. * @v xfer Data transfer interface
  754. * @v data Received data
  755. * @v len Length of received data
  756. * @ret rc Return status code
  757. */
  758. static int dhcp_deliver_raw ( struct xfer_interface *xfer,
  759. const void *data, size_t len ) {
  760. struct dhcp_session *dhcp =
  761. container_of ( xfer, struct dhcp_session, xfer );
  762. struct dhcp_settings *dhcpset;
  763. struct dhcphdr *dhcphdr;
  764. uint8_t msgtype = 0;
  765. /* Convert packet into a DHCP settings block */
  766. dhcpset = dhcpset_create ( data, len );
  767. if ( ! dhcpset ) {
  768. DBGC ( dhcp, "DHCP %p could not store DHCP packet\n", dhcp );
  769. return -ENOMEM;
  770. }
  771. dhcphdr = dhcpset->dhcppkt.dhcphdr;
  772. /* Identify message type */
  773. dhcppkt_fetch ( &dhcpset->dhcppkt, DHCP_MESSAGE_TYPE, &msgtype,
  774. sizeof ( msgtype ) );
  775. DBGC ( dhcp, "DHCP %p received %s %p\n",
  776. dhcp, dhcp_msgtype_name ( msgtype ), dhcpset );
  777. /* Check for matching transaction ID */
  778. if ( dhcphdr->xid != dhcp_xid ( dhcp->netdev ) ) {
  779. DBGC ( dhcp, "DHCP %p received %s %p has bad transaction ID\n",
  780. dhcp, dhcp_msgtype_name ( msgtype ), dhcpset );
  781. goto out;
  782. };
  783. /* Handle packet based on current state */
  784. switch ( dhcp->state ) {
  785. case DHCP_STATE_DISCOVER:
  786. if ( msgtype == DHCPOFFER )
  787. dhcp_rx_dhcpoffer ( dhcp, dhcpset );
  788. break;
  789. case DHCP_STATE_REQUEST:
  790. if ( msgtype == DHCPACK )
  791. dhcp_rx_dhcpack ( dhcp, dhcpset );
  792. break;
  793. case DHCP_STATE_PROXYREQUEST:
  794. if ( msgtype == DHCPACK )
  795. dhcp_rx_proxydhcpack ( dhcp, dhcpset );
  796. break;
  797. default:
  798. assert ( 0 );
  799. break;
  800. }
  801. out:
  802. dhcpset_put ( dhcpset );
  803. return 0;
  804. }
  805. /** DHCP data transfer interface operations */
  806. static struct xfer_interface_operations dhcp_xfer_operations = {
  807. .close = ignore_xfer_close,
  808. .vredirect = xfer_vopen,
  809. .window = unlimited_xfer_window,
  810. .alloc_iob = default_xfer_alloc_iob,
  811. .deliver_iob = xfer_deliver_as_raw,
  812. .deliver_raw = dhcp_deliver_raw,
  813. };
  814. /**
  815. * Handle DHCP retry timer expiry
  816. *
  817. * @v timer DHCP retry timer
  818. * @v fail Failure indicator
  819. */
  820. static void dhcp_timer_expired ( struct retry_timer *timer, int fail ) {
  821. struct dhcp_session *dhcp =
  822. container_of ( timer, struct dhcp_session, timer );
  823. unsigned long elapsed = ( currticks() - dhcp->start );
  824. /* If we have failed, terminate DHCP */
  825. if ( fail ) {
  826. dhcp_finished ( dhcp, -ETIMEDOUT );
  827. return;
  828. }
  829. /* Give up waiting for ProxyDHCP before we reach the failure point */
  830. if ( elapsed > PROXYDHCP_WAIT_TIME ) {
  831. if ( dhcp->state == DHCP_STATE_DISCOVER ) {
  832. dhcp_set_state ( dhcp, DHCP_STATE_REQUEST );
  833. return;
  834. } else if ( dhcp->state == DHCP_STATE_PROXYREQUEST ) {
  835. dhcp_finished ( dhcp, 0 );
  836. return;
  837. }
  838. }
  839. /* Otherwise, retransmit current packet */
  840. dhcp_tx ( dhcp );
  841. }
  842. /****************************************************************************
  843. *
  844. * Job control interface
  845. *
  846. */
  847. /**
  848. * Handle kill() event received via job control interface
  849. *
  850. * @v job DHCP job control interface
  851. */
  852. static void dhcp_job_kill ( struct job_interface *job ) {
  853. struct dhcp_session *dhcp =
  854. container_of ( job, struct dhcp_session, job );
  855. /* Terminate DHCP session */
  856. dhcp_finished ( dhcp, -ECANCELED );
  857. }
  858. /** DHCP job control interface operations */
  859. static struct job_interface_operations dhcp_job_operations = {
  860. .done = ignore_job_done,
  861. .kill = dhcp_job_kill,
  862. .progress = ignore_job_progress,
  863. };
  864. /****************************************************************************
  865. *
  866. * Instantiator
  867. *
  868. */
  869. /**
  870. * Start DHCP on a network device
  871. *
  872. * @v job Job control interface
  873. * @v netdev Network device
  874. * @v register_options DHCP option block registration routine
  875. * @ret rc Return status code
  876. *
  877. * Starts DHCP on the specified network device. If successful, the @c
  878. * register_options() routine will be called with the acquired
  879. * options.
  880. */
  881. int start_dhcp ( struct job_interface *job, struct net_device *netdev ) {
  882. static struct sockaddr_in server = {
  883. .sin_family = AF_INET,
  884. .sin_addr.s_addr = INADDR_BROADCAST,
  885. .sin_port = htons ( BOOTPS_PORT ),
  886. };
  887. static struct sockaddr_in client = {
  888. .sin_family = AF_INET,
  889. .sin_port = htons ( BOOTPC_PORT ),
  890. };
  891. struct dhcp_session *dhcp;
  892. int rc;
  893. /* Allocate and initialise structure */
  894. dhcp = zalloc ( sizeof ( *dhcp ) );
  895. if ( ! dhcp )
  896. return -ENOMEM;
  897. dhcp->refcnt.free = dhcp_free;
  898. job_init ( &dhcp->job, &dhcp_job_operations, &dhcp->refcnt );
  899. xfer_init ( &dhcp->xfer, &dhcp_xfer_operations, &dhcp->refcnt );
  900. dhcp->netdev = netdev_get ( netdev );
  901. dhcp->timer.expired = dhcp_timer_expired;
  902. dhcp->start = currticks();
  903. /* Instantiate child objects and attach to our interfaces */
  904. if ( ( rc = xfer_open_socket ( &dhcp->xfer, SOCK_DGRAM,
  905. ( struct sockaddr * ) &server,
  906. ( struct sockaddr * ) &client ) ) != 0 )
  907. goto err;
  908. /* Start timer to initiate initial DHCPREQUEST */
  909. start_timer_nodelay ( &dhcp->timer );
  910. /* Attach parent interface, mortalise self, and return */
  911. job_plug_plug ( &dhcp->job, job );
  912. ref_put ( &dhcp->refcnt );
  913. return 0;
  914. err:
  915. dhcp_finished ( dhcp, rc );
  916. ref_put ( &dhcp->refcnt );
  917. return rc;
  918. }