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

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