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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498
  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., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301, USA.
  18. */
  19. FILE_LICENCE ( GPL2_OR_LATER );
  20. #include <string.h>
  21. #include <stdlib.h>
  22. #include <stdio.h>
  23. #include <ctype.h>
  24. #include <errno.h>
  25. #include <assert.h>
  26. #include <byteswap.h>
  27. #include <ipxe/if_ether.h>
  28. #include <ipxe/iobuf.h>
  29. #include <ipxe/netdevice.h>
  30. #include <ipxe/device.h>
  31. #include <ipxe/xfer.h>
  32. #include <ipxe/open.h>
  33. #include <ipxe/job.h>
  34. #include <ipxe/retry.h>
  35. #include <ipxe/tcpip.h>
  36. #include <ipxe/ip.h>
  37. #include <ipxe/uuid.h>
  38. #include <ipxe/timer.h>
  39. #include <ipxe/settings.h>
  40. #include <ipxe/dhcp.h>
  41. #include <ipxe/dhcpopts.h>
  42. #include <ipxe/dhcppkt.h>
  43. #include <ipxe/dhcp_arch.h>
  44. #include <ipxe/features.h>
  45. /** @file
  46. *
  47. * Dynamic Host Configuration Protocol
  48. *
  49. */
  50. struct dhcp_session;
  51. static int dhcp_tx ( struct dhcp_session *dhcp );
  52. /**
  53. * DHCP operation types
  54. *
  55. * This table maps from DHCP message types (i.e. values of the @c
  56. * DHCP_MESSAGE_TYPE option) to values of the "op" field within a DHCP
  57. * packet.
  58. */
  59. static const uint8_t dhcp_op[] = {
  60. [DHCPDISCOVER] = BOOTP_REQUEST,
  61. [DHCPOFFER] = BOOTP_REPLY,
  62. [DHCPREQUEST] = BOOTP_REQUEST,
  63. [DHCPDECLINE] = BOOTP_REQUEST,
  64. [DHCPACK] = BOOTP_REPLY,
  65. [DHCPNAK] = BOOTP_REPLY,
  66. [DHCPRELEASE] = BOOTP_REQUEST,
  67. [DHCPINFORM] = BOOTP_REQUEST,
  68. };
  69. /** Raw option data for options common to all DHCP requests */
  70. static uint8_t dhcp_request_options_data[] = {
  71. DHCP_MESSAGE_TYPE, DHCP_BYTE ( 0 ),
  72. DHCP_MAX_MESSAGE_SIZE,
  73. DHCP_WORD ( ETH_MAX_MTU - 20 /* IP header */ - 8 /* UDP header */ ),
  74. DHCP_CLIENT_ARCHITECTURE, DHCP_ARCH_CLIENT_ARCHITECTURE,
  75. DHCP_CLIENT_NDI, DHCP_ARCH_CLIENT_NDI,
  76. DHCP_VENDOR_CLASS_ID, DHCP_ARCH_VENDOR_CLASS_ID,
  77. DHCP_USER_CLASS_ID, DHCP_STRING ( 'i', 'P', 'X', 'E' ),
  78. DHCP_PARAMETER_REQUEST_LIST,
  79. DHCP_OPTION ( DHCP_SUBNET_MASK, DHCP_ROUTERS, DHCP_DNS_SERVERS,
  80. DHCP_LOG_SERVERS, DHCP_HOST_NAME, DHCP_DOMAIN_NAME,
  81. DHCP_ROOT_PATH, DHCP_VENDOR_ENCAP, DHCP_VENDOR_CLASS_ID,
  82. DHCP_TFTP_SERVER_NAME, DHCP_BOOTFILE_NAME,
  83. 128, 129, 130, 131, 132, 133, 134, 135, /* for PXE */
  84. DHCP_EB_ENCAP, DHCP_ISCSI_INITIATOR_IQN ),
  85. DHCP_END
  86. };
  87. /** DHCP server address setting */
  88. struct setting dhcp_server_setting __setting ( SETTING_MISC ) = {
  89. .name = "dhcp-server",
  90. .description = "DHCP server",
  91. .tag = DHCP_SERVER_IDENTIFIER,
  92. .type = &setting_type_ipv4,
  93. };
  94. /** DHCP user class setting */
  95. struct setting user_class_setting __setting ( SETTING_HOST_EXTRA ) = {
  96. .name = "user-class",
  97. .description = "DHCP user class",
  98. .tag = DHCP_USER_CLASS_ID,
  99. .type = &setting_type_string,
  100. };
  101. /** Use cached network settings */
  102. struct setting use_cached_setting __setting ( SETTING_MISC ) = {
  103. .name = "use-cached",
  104. .description = "Use cached settings",
  105. .tag = DHCP_EB_USE_CACHED,
  106. .type = &setting_type_uint8,
  107. };
  108. /**
  109. * Most recent DHCP transaction ID
  110. *
  111. * This is exposed for use by the fakedhcp code when reconstructing
  112. * DHCP packets for PXE NBPs.
  113. */
  114. uint32_t dhcp_last_xid;
  115. /**
  116. * Name a DHCP packet type
  117. *
  118. * @v msgtype DHCP message type
  119. * @ret string DHCP mesasge type name
  120. */
  121. static inline const char * dhcp_msgtype_name ( unsigned int msgtype ) {
  122. switch ( msgtype ) {
  123. case DHCPNONE: return "BOOTP"; /* Non-DHCP packet */
  124. case DHCPDISCOVER: return "DHCPDISCOVER";
  125. case DHCPOFFER: return "DHCPOFFER";
  126. case DHCPREQUEST: return "DHCPREQUEST";
  127. case DHCPDECLINE: return "DHCPDECLINE";
  128. case DHCPACK: return "DHCPACK";
  129. case DHCPNAK: return "DHCPNAK";
  130. case DHCPRELEASE: return "DHCPRELEASE";
  131. case DHCPINFORM: return "DHCPINFORM";
  132. default: return "DHCP<invalid>";
  133. }
  134. }
  135. /****************************************************************************
  136. *
  137. * DHCP session
  138. *
  139. */
  140. struct dhcp_session;
  141. /** DHCP session state operations */
  142. struct dhcp_session_state {
  143. /** State name */
  144. const char *name;
  145. /**
  146. * Construct transmitted packet
  147. *
  148. * @v dhcp DHCP session
  149. * @v dhcppkt DHCP packet
  150. * @v peer Destination address
  151. */
  152. int ( * tx ) ( struct dhcp_session *dhcp,
  153. struct dhcp_packet *dhcppkt,
  154. struct sockaddr_in *peer );
  155. /** Handle received packet
  156. *
  157. * @v dhcp DHCP session
  158. * @v dhcppkt DHCP packet
  159. * @v peer DHCP server address
  160. * @v msgtype DHCP message type
  161. * @v server_id DHCP server ID
  162. */
  163. void ( * rx ) ( struct dhcp_session *dhcp,
  164. struct dhcp_packet *dhcppkt,
  165. struct sockaddr_in *peer,
  166. uint8_t msgtype, struct in_addr server_id );
  167. /** Handle timer expiry
  168. *
  169. * @v dhcp DHCP session
  170. */
  171. void ( * expired ) ( struct dhcp_session *dhcp );
  172. /** Transmitted message type */
  173. uint8_t tx_msgtype;
  174. /** Apply minimum timeout */
  175. uint8_t apply_min_timeout;
  176. };
  177. static struct dhcp_session_state dhcp_state_discover;
  178. static struct dhcp_session_state dhcp_state_request;
  179. static struct dhcp_session_state dhcp_state_proxy;
  180. static struct dhcp_session_state dhcp_state_pxebs;
  181. /** A DHCP session */
  182. struct dhcp_session {
  183. /** Reference counter */
  184. struct refcnt refcnt;
  185. /** Job control interface */
  186. struct interface job;
  187. /** Data transfer interface */
  188. struct interface xfer;
  189. /** Network device being configured */
  190. struct net_device *netdev;
  191. /** Local socket address */
  192. struct sockaddr_in local;
  193. /** State of the session */
  194. struct dhcp_session_state *state;
  195. /** Transaction ID (in network-endian order) */
  196. uint32_t xid;
  197. /** Offered IP address */
  198. struct in_addr offer;
  199. /** DHCP server */
  200. struct in_addr server;
  201. /** DHCP offer priority */
  202. int priority;
  203. /** ProxyDHCP protocol extensions should be ignored */
  204. int no_pxedhcp;
  205. /** ProxyDHCP server */
  206. struct in_addr proxy_server;
  207. /** ProxyDHCP offer */
  208. struct dhcp_packet *proxy_offer;
  209. /** ProxyDHCP offer priority */
  210. int proxy_priority;
  211. /** PXE Boot Server type */
  212. uint16_t pxe_type;
  213. /** List of PXE Boot Servers to attempt */
  214. struct in_addr *pxe_attempt;
  215. /** List of PXE Boot Servers to accept */
  216. struct in_addr *pxe_accept;
  217. /** Retransmission timer */
  218. struct retry_timer timer;
  219. /** Transmission counter */
  220. unsigned int count;
  221. /** Start time of the current state (in ticks) */
  222. unsigned long start;
  223. };
  224. /**
  225. * Free DHCP session
  226. *
  227. * @v refcnt Reference counter
  228. */
  229. static void dhcp_free ( struct refcnt *refcnt ) {
  230. struct dhcp_session *dhcp =
  231. container_of ( refcnt, struct dhcp_session, refcnt );
  232. netdev_put ( dhcp->netdev );
  233. dhcppkt_put ( dhcp->proxy_offer );
  234. free ( dhcp );
  235. }
  236. /**
  237. * Mark DHCP session as complete
  238. *
  239. * @v dhcp DHCP session
  240. * @v rc Return status code
  241. */
  242. static void dhcp_finished ( struct dhcp_session *dhcp, int rc ) {
  243. /* Stop retry timer */
  244. stop_timer ( &dhcp->timer );
  245. /* Shut down interfaces */
  246. intf_shutdown ( &dhcp->xfer, rc );
  247. intf_shutdown ( &dhcp->job, rc );
  248. }
  249. /**
  250. * Transition to new DHCP session state
  251. *
  252. * @v dhcp DHCP session
  253. * @v state New session state
  254. */
  255. static void dhcp_set_state ( struct dhcp_session *dhcp,
  256. struct dhcp_session_state *state ) {
  257. DBGC ( dhcp, "DHCP %p entering %s state\n", dhcp, state->name );
  258. dhcp->state = state;
  259. dhcp->start = currticks();
  260. stop_timer ( &dhcp->timer );
  261. dhcp->timer.min_timeout =
  262. ( state->apply_min_timeout ? DHCP_MIN_TIMEOUT : 0 );
  263. dhcp->timer.max_timeout = DHCP_MAX_TIMEOUT;
  264. start_timer_nodelay ( &dhcp->timer );
  265. }
  266. /**
  267. * Check if DHCP packet contains PXE options
  268. *
  269. * @v dhcppkt DHCP packet
  270. * @ret has_pxeopts DHCP packet contains PXE options
  271. *
  272. * It is assumed that the packet is already known to contain option 60
  273. * set to "PXEClient".
  274. */
  275. static int dhcp_has_pxeopts ( struct dhcp_packet *dhcppkt ) {
  276. /* Check for a boot filename */
  277. if ( dhcppkt_fetch ( dhcppkt, DHCP_BOOTFILE_NAME, NULL, 0 ) > 0 )
  278. return 1;
  279. /* Check for a PXE boot menu */
  280. if ( dhcppkt_fetch ( dhcppkt, DHCP_PXE_BOOT_MENU, NULL, 0 ) > 0 )
  281. return 1;
  282. return 0;
  283. }
  284. /****************************************************************************
  285. *
  286. * DHCP state machine
  287. *
  288. */
  289. /**
  290. * Construct transmitted packet for DHCP discovery
  291. *
  292. * @v dhcp DHCP session
  293. * @v dhcppkt DHCP packet
  294. * @v peer Destination address
  295. */
  296. static int dhcp_discovery_tx ( struct dhcp_session *dhcp,
  297. struct dhcp_packet *dhcppkt __unused,
  298. struct sockaddr_in *peer ) {
  299. DBGC ( dhcp, "DHCP %p DHCPDISCOVER\n", dhcp );
  300. /* Set server address */
  301. peer->sin_addr.s_addr = INADDR_BROADCAST;
  302. peer->sin_port = htons ( BOOTPS_PORT );
  303. return 0;
  304. }
  305. /**
  306. * Handle received packet during DHCP discovery
  307. *
  308. * @v dhcp DHCP session
  309. * @v dhcppkt DHCP packet
  310. * @v peer DHCP server address
  311. * @v msgtype DHCP message type
  312. * @v server_id DHCP server ID
  313. */
  314. static void dhcp_discovery_rx ( struct dhcp_session *dhcp,
  315. struct dhcp_packet *dhcppkt,
  316. struct sockaddr_in *peer, uint8_t msgtype,
  317. struct in_addr server_id ) {
  318. struct in_addr ip;
  319. char vci[9]; /* "PXEClient" */
  320. int vci_len;
  321. int has_pxeclient;
  322. int8_t priority = 0;
  323. uint8_t no_pxedhcp = 0;
  324. unsigned long elapsed;
  325. DBGC ( dhcp, "DHCP %p %s from %s:%d", dhcp,
  326. dhcp_msgtype_name ( msgtype ), inet_ntoa ( peer->sin_addr ),
  327. ntohs ( peer->sin_port ) );
  328. if ( server_id.s_addr != peer->sin_addr.s_addr )
  329. DBGC ( dhcp, " (%s)", inet_ntoa ( server_id ) );
  330. /* Identify offered IP address */
  331. ip = dhcppkt->dhcphdr->yiaddr;
  332. if ( ip.s_addr )
  333. DBGC ( dhcp, " for %s", inet_ntoa ( ip ) );
  334. /* Identify "PXEClient" vendor class */
  335. vci_len = dhcppkt_fetch ( dhcppkt, DHCP_VENDOR_CLASS_ID,
  336. vci, sizeof ( vci ) );
  337. has_pxeclient = ( ( vci_len >= ( int ) sizeof ( vci ) ) &&
  338. ( strncmp ( "PXEClient", vci, sizeof (vci) ) == 0 ));
  339. if ( has_pxeclient ) {
  340. DBGC ( dhcp, "%s",
  341. ( dhcp_has_pxeopts ( dhcppkt ) ? " pxe" : " proxy" ) );
  342. }
  343. /* Identify priority */
  344. dhcppkt_fetch ( dhcppkt, DHCP_EB_PRIORITY, &priority,
  345. sizeof ( priority ) );
  346. if ( priority )
  347. DBGC ( dhcp, " pri %d", priority );
  348. /* Identify ignore-PXE flag */
  349. dhcppkt_fetch ( dhcppkt, DHCP_EB_NO_PXEDHCP, &no_pxedhcp,
  350. sizeof ( no_pxedhcp ) );
  351. if ( no_pxedhcp )
  352. DBGC ( dhcp, " nopxe" );
  353. DBGC ( dhcp, "\n" );
  354. /* Select as DHCP offer, if applicable */
  355. if ( ip.s_addr && ( peer->sin_port == htons ( BOOTPS_PORT ) ) &&
  356. ( ( msgtype == DHCPOFFER ) || ( ! msgtype /* BOOTP */ ) ) &&
  357. ( priority >= dhcp->priority ) ) {
  358. dhcp->offer = ip;
  359. dhcp->server = server_id;
  360. dhcp->priority = priority;
  361. dhcp->no_pxedhcp = no_pxedhcp;
  362. }
  363. /* Select as ProxyDHCP offer, if applicable */
  364. if ( server_id.s_addr && has_pxeclient &&
  365. ( priority >= dhcp->proxy_priority ) ) {
  366. dhcppkt_put ( dhcp->proxy_offer );
  367. dhcp->proxy_server = server_id;
  368. dhcp->proxy_offer = dhcppkt_get ( dhcppkt );
  369. dhcp->proxy_priority = priority;
  370. }
  371. /* We can exit the discovery state when we have a valid
  372. * DHCPOFFER, and either:
  373. *
  374. * o The DHCPOFFER instructs us to ignore ProxyDHCPOFFERs, or
  375. * o We have a valid ProxyDHCPOFFER, or
  376. * o We have allowed sufficient time for ProxyDHCPOFFERs.
  377. */
  378. /* If we don't yet have a DHCPOFFER, do nothing */
  379. if ( ! dhcp->offer.s_addr )
  380. return;
  381. /* If we can't yet transition to DHCPREQUEST, do nothing */
  382. elapsed = ( currticks() - dhcp->start );
  383. if ( ! ( dhcp->no_pxedhcp || dhcp->proxy_offer ||
  384. ( elapsed > PROXYDHCP_MAX_TIMEOUT ) ) )
  385. return;
  386. /* Transition to DHCPREQUEST */
  387. dhcp_set_state ( dhcp, &dhcp_state_request );
  388. }
  389. /**
  390. * Handle timer expiry during DHCP discovery
  391. *
  392. * @v dhcp DHCP session
  393. */
  394. static void dhcp_discovery_expired ( struct dhcp_session *dhcp ) {
  395. unsigned long elapsed = ( currticks() - dhcp->start );
  396. /* Give up waiting for ProxyDHCP before we reach the failure point */
  397. if ( dhcp->offer.s_addr && ( elapsed > PROXYDHCP_MAX_TIMEOUT ) ) {
  398. dhcp_set_state ( dhcp, &dhcp_state_request );
  399. return;
  400. }
  401. /* Otherwise, retransmit current packet */
  402. dhcp_tx ( dhcp );
  403. }
  404. /** DHCP discovery state operations */
  405. static struct dhcp_session_state dhcp_state_discover = {
  406. .name = "discovery",
  407. .tx = dhcp_discovery_tx,
  408. .rx = dhcp_discovery_rx,
  409. .expired = dhcp_discovery_expired,
  410. .tx_msgtype = DHCPDISCOVER,
  411. .apply_min_timeout = 1,
  412. };
  413. /**
  414. * Construct transmitted packet for DHCP request
  415. *
  416. * @v dhcp DHCP session
  417. * @v dhcppkt DHCP packet
  418. * @v peer Destination address
  419. */
  420. static int dhcp_request_tx ( struct dhcp_session *dhcp,
  421. struct dhcp_packet *dhcppkt,
  422. struct sockaddr_in *peer ) {
  423. int rc;
  424. DBGC ( dhcp, "DHCP %p DHCPREQUEST to %s:%d",
  425. dhcp, inet_ntoa ( dhcp->server ), BOOTPS_PORT );
  426. DBGC ( dhcp, " for %s\n", inet_ntoa ( dhcp->offer ) );
  427. /* Set server ID */
  428. if ( ( rc = dhcppkt_store ( dhcppkt, DHCP_SERVER_IDENTIFIER,
  429. &dhcp->server,
  430. sizeof ( dhcp->server ) ) ) != 0 )
  431. return rc;
  432. /* Set requested IP address */
  433. if ( ( rc = dhcppkt_store ( dhcppkt, DHCP_REQUESTED_ADDRESS,
  434. &dhcp->offer,
  435. sizeof ( dhcp->offer ) ) ) != 0 )
  436. return rc;
  437. /* Set server address */
  438. peer->sin_addr.s_addr = INADDR_BROADCAST;
  439. peer->sin_port = htons ( BOOTPS_PORT );
  440. return 0;
  441. }
  442. /**
  443. * Handle received packet during DHCP request
  444. *
  445. * @v dhcp DHCP session
  446. * @v dhcppkt DHCP packet
  447. * @v peer DHCP server address
  448. * @v msgtype DHCP message type
  449. * @v server_id DHCP server ID
  450. */
  451. static void dhcp_request_rx ( struct dhcp_session *dhcp,
  452. struct dhcp_packet *dhcppkt,
  453. struct sockaddr_in *peer, uint8_t msgtype,
  454. struct in_addr server_id ) {
  455. struct in_addr ip;
  456. struct settings *parent;
  457. struct settings *settings;
  458. int rc;
  459. DBGC ( dhcp, "DHCP %p %s from %s:%d", dhcp,
  460. dhcp_msgtype_name ( msgtype ), inet_ntoa ( peer->sin_addr ),
  461. ntohs ( peer->sin_port ) );
  462. if ( server_id.s_addr != peer->sin_addr.s_addr )
  463. DBGC ( dhcp, " (%s)", inet_ntoa ( server_id ) );
  464. /* Identify leased IP address */
  465. ip = dhcppkt->dhcphdr->yiaddr;
  466. if ( ip.s_addr )
  467. DBGC ( dhcp, " for %s", inet_ntoa ( ip ) );
  468. DBGC ( dhcp, "\n" );
  469. /* Filter out unacceptable responses */
  470. if ( peer->sin_port != htons ( BOOTPS_PORT ) )
  471. return;
  472. if ( msgtype /* BOOTP */ && ( msgtype != DHCPACK ) )
  473. return;
  474. if ( server_id.s_addr != dhcp->server.s_addr )
  475. return;
  476. if ( ip.s_addr != dhcp->offer.s_addr )
  477. return;
  478. /* Record assigned address */
  479. dhcp->local.sin_addr = ip;
  480. /* Register settings */
  481. parent = netdev_settings ( dhcp->netdev );
  482. settings = &dhcppkt->settings;
  483. if ( ( rc = register_settings ( settings, parent,
  484. DHCP_SETTINGS_NAME ) ) != 0 ) {
  485. DBGC ( dhcp, "DHCP %p could not register settings: %s\n",
  486. dhcp, strerror ( rc ) );
  487. dhcp_finished ( dhcp, rc );
  488. return;
  489. }
  490. /* Perform ProxyDHCP if applicable */
  491. if ( dhcp->proxy_offer /* Have ProxyDHCP offer */ &&
  492. ( ! dhcp->no_pxedhcp ) /* ProxyDHCP not disabled */ ) {
  493. if ( dhcp_has_pxeopts ( dhcp->proxy_offer ) ) {
  494. /* PXE options already present; register settings
  495. * without performing a ProxyDHCPREQUEST
  496. */
  497. settings = &dhcp->proxy_offer->settings;
  498. if ( ( rc = register_settings ( settings, NULL,
  499. PROXYDHCP_SETTINGS_NAME ) ) != 0 ) {
  500. DBGC ( dhcp, "DHCP %p could not register "
  501. "proxy settings: %s\n",
  502. dhcp, strerror ( rc ) );
  503. dhcp_finished ( dhcp, rc );
  504. return;
  505. }
  506. } else {
  507. /* PXE options not present; use a ProxyDHCPREQUEST */
  508. dhcp_set_state ( dhcp, &dhcp_state_proxy );
  509. return;
  510. }
  511. }
  512. /* Terminate DHCP */
  513. dhcp_finished ( dhcp, 0 );
  514. }
  515. /**
  516. * Handle timer expiry during DHCP discovery
  517. *
  518. * @v dhcp DHCP session
  519. */
  520. static void dhcp_request_expired ( struct dhcp_session *dhcp ) {
  521. /* Retransmit current packet */
  522. dhcp_tx ( dhcp );
  523. }
  524. /** DHCP request state operations */
  525. static struct dhcp_session_state dhcp_state_request = {
  526. .name = "request",
  527. .tx = dhcp_request_tx,
  528. .rx = dhcp_request_rx,
  529. .expired = dhcp_request_expired,
  530. .tx_msgtype = DHCPREQUEST,
  531. .apply_min_timeout = 0,
  532. };
  533. /**
  534. * Construct transmitted packet for ProxyDHCP request
  535. *
  536. * @v dhcp DHCP session
  537. * @v dhcppkt DHCP packet
  538. * @v peer Destination address
  539. */
  540. static int dhcp_proxy_tx ( struct dhcp_session *dhcp,
  541. struct dhcp_packet *dhcppkt,
  542. struct sockaddr_in *peer ) {
  543. int rc;
  544. DBGC ( dhcp, "DHCP %p ProxyDHCP REQUEST to %s\n", dhcp,
  545. inet_ntoa ( dhcp->proxy_server ) );
  546. /* Set server ID */
  547. if ( ( rc = dhcppkt_store ( dhcppkt, DHCP_SERVER_IDENTIFIER,
  548. &dhcp->proxy_server,
  549. sizeof ( dhcp->proxy_server ) ) ) != 0 )
  550. return rc;
  551. /* Set server address */
  552. peer->sin_addr = dhcp->proxy_server;
  553. peer->sin_port = htons ( PXE_PORT );
  554. return 0;
  555. }
  556. /**
  557. * Handle received packet during ProxyDHCP request
  558. *
  559. * @v dhcp DHCP session
  560. * @v dhcppkt DHCP packet
  561. * @v peer DHCP server address
  562. * @v msgtype DHCP message type
  563. * @v server_id DHCP server ID
  564. */
  565. static void dhcp_proxy_rx ( struct dhcp_session *dhcp,
  566. struct dhcp_packet *dhcppkt,
  567. struct sockaddr_in *peer, uint8_t msgtype,
  568. struct in_addr server_id ) {
  569. struct settings *settings = &dhcppkt->settings;
  570. int rc;
  571. DBGC ( dhcp, "DHCP %p %s from %s:%d", dhcp,
  572. dhcp_msgtype_name ( msgtype ), inet_ntoa ( peer->sin_addr ),
  573. ntohs ( peer->sin_port ) );
  574. if ( server_id.s_addr != peer->sin_addr.s_addr )
  575. DBGC ( dhcp, " (%s)", inet_ntoa ( server_id ) );
  576. DBGC ( dhcp, "\n" );
  577. /* Filter out unacceptable responses */
  578. if ( peer->sin_port != ntohs ( PXE_PORT ) )
  579. return;
  580. if ( ( msgtype != DHCPOFFER ) && ( msgtype != DHCPACK ) )
  581. return;
  582. if ( server_id.s_addr /* Linux PXE server omits server ID */ &&
  583. ( server_id.s_addr != dhcp->proxy_server.s_addr ) )
  584. return;
  585. /* Register settings */
  586. if ( ( rc = register_settings ( settings, NULL,
  587. PROXYDHCP_SETTINGS_NAME ) ) != 0 ) {
  588. DBGC ( dhcp, "DHCP %p could not register proxy settings: %s\n",
  589. dhcp, strerror ( rc ) );
  590. dhcp_finished ( dhcp, rc );
  591. return;
  592. }
  593. /* Terminate DHCP */
  594. dhcp_finished ( dhcp, 0 );
  595. }
  596. /**
  597. * Handle timer expiry during ProxyDHCP request
  598. *
  599. * @v dhcp DHCP session
  600. */
  601. static void dhcp_proxy_expired ( struct dhcp_session *dhcp ) {
  602. unsigned long elapsed = ( currticks() - dhcp->start );
  603. /* Give up waiting for ProxyDHCP before we reach the failure point */
  604. if ( elapsed > PROXYDHCP_MAX_TIMEOUT ) {
  605. dhcp_finished ( dhcp, 0 );
  606. return;
  607. }
  608. /* Retransmit current packet */
  609. dhcp_tx ( dhcp );
  610. }
  611. /** ProxyDHCP request state operations */
  612. static struct dhcp_session_state dhcp_state_proxy = {
  613. .name = "ProxyDHCP",
  614. .tx = dhcp_proxy_tx,
  615. .rx = dhcp_proxy_rx,
  616. .expired = dhcp_proxy_expired,
  617. .tx_msgtype = DHCPREQUEST,
  618. .apply_min_timeout = 0,
  619. };
  620. /**
  621. * Construct transmitted packet for PXE Boot Server Discovery
  622. *
  623. * @v dhcp DHCP session
  624. * @v dhcppkt DHCP packet
  625. * @v peer Destination address
  626. */
  627. static int dhcp_pxebs_tx ( struct dhcp_session *dhcp,
  628. struct dhcp_packet *dhcppkt,
  629. struct sockaddr_in *peer ) {
  630. struct dhcp_pxe_boot_menu_item menu_item = { 0, 0 };
  631. int rc;
  632. /* Set server address */
  633. peer->sin_addr = *(dhcp->pxe_attempt);
  634. peer->sin_port = ( ( peer->sin_addr.s_addr == INADDR_BROADCAST ) ?
  635. htons ( BOOTPS_PORT ) : htons ( PXE_PORT ) );
  636. DBGC ( dhcp, "DHCP %p PXEBS REQUEST to %s:%d for type %d\n",
  637. dhcp, inet_ntoa ( peer->sin_addr ), ntohs ( peer->sin_port ),
  638. le16_to_cpu ( dhcp->pxe_type ) );
  639. /* Set boot menu item */
  640. menu_item.type = dhcp->pxe_type;
  641. if ( ( rc = dhcppkt_store ( dhcppkt, DHCP_PXE_BOOT_MENU_ITEM,
  642. &menu_item, sizeof ( menu_item ) ) ) != 0 )
  643. return rc;
  644. return 0;
  645. }
  646. /**
  647. * Check to see if PXE Boot Server address is acceptable
  648. *
  649. * @v dhcp DHCP session
  650. * @v bs Boot Server address
  651. * @ret accept Boot Server is acceptable
  652. */
  653. static int dhcp_pxebs_accept ( struct dhcp_session *dhcp,
  654. struct in_addr bs ) {
  655. struct in_addr *accept;
  656. /* Accept if we have no acceptance filter */
  657. if ( ! dhcp->pxe_accept )
  658. return 1;
  659. /* Scan through acceptance list */
  660. for ( accept = dhcp->pxe_accept ; accept->s_addr ; accept++ ) {
  661. if ( accept->s_addr == bs.s_addr )
  662. return 1;
  663. }
  664. DBGC ( dhcp, "DHCP %p rejecting server %s\n",
  665. dhcp, inet_ntoa ( bs ) );
  666. return 0;
  667. }
  668. /**
  669. * Handle received packet during PXE Boot Server Discovery
  670. *
  671. * @v dhcp DHCP session
  672. * @v dhcppkt DHCP packet
  673. * @v peer DHCP server address
  674. * @v msgtype DHCP message type
  675. * @v server_id DHCP server ID
  676. */
  677. static void dhcp_pxebs_rx ( struct dhcp_session *dhcp,
  678. struct dhcp_packet *dhcppkt,
  679. struct sockaddr_in *peer, uint8_t msgtype,
  680. struct in_addr server_id ) {
  681. struct dhcp_pxe_boot_menu_item menu_item = { 0, 0 };
  682. int rc;
  683. DBGC ( dhcp, "DHCP %p %s from %s:%d", dhcp,
  684. dhcp_msgtype_name ( msgtype ), inet_ntoa ( peer->sin_addr ),
  685. ntohs ( peer->sin_port ) );
  686. if ( server_id.s_addr != peer->sin_addr.s_addr )
  687. DBGC ( dhcp, " (%s)", inet_ntoa ( server_id ) );
  688. /* Identify boot menu item */
  689. dhcppkt_fetch ( dhcppkt, DHCP_PXE_BOOT_MENU_ITEM,
  690. &menu_item, sizeof ( menu_item ) );
  691. if ( menu_item.type )
  692. DBGC ( dhcp, " for type %d", ntohs ( menu_item.type ) );
  693. DBGC ( dhcp, "\n" );
  694. /* Filter out unacceptable responses */
  695. if ( ( peer->sin_port != htons ( BOOTPS_PORT ) ) &&
  696. ( peer->sin_port != htons ( PXE_PORT ) ) )
  697. return;
  698. if ( msgtype != DHCPACK )
  699. return;
  700. if ( menu_item.type != dhcp->pxe_type )
  701. return;
  702. if ( ! dhcp_pxebs_accept ( dhcp, ( server_id.s_addr ?
  703. server_id : peer->sin_addr ) ) )
  704. return;
  705. /* Register settings */
  706. if ( ( rc = register_settings ( &dhcppkt->settings, NULL,
  707. PXEBS_SETTINGS_NAME ) ) != 0 ) {
  708. DBGC ( dhcp, "DHCP %p could not register settings: %s\n",
  709. dhcp, strerror ( rc ) );
  710. dhcp_finished ( dhcp, rc );
  711. return;
  712. }
  713. /* Terminate DHCP */
  714. dhcp_finished ( dhcp, 0 );
  715. }
  716. /**
  717. * Handle timer expiry during PXE Boot Server Discovery
  718. *
  719. * @v dhcp DHCP session
  720. */
  721. static void dhcp_pxebs_expired ( struct dhcp_session *dhcp ) {
  722. unsigned long elapsed = ( currticks() - dhcp->start );
  723. /* Give up waiting before we reach the failure point, and fail
  724. * over to the next server in the attempt list
  725. */
  726. if ( elapsed > PXEBS_MAX_TIMEOUT ) {
  727. dhcp->pxe_attempt++;
  728. if ( dhcp->pxe_attempt->s_addr ) {
  729. dhcp_set_state ( dhcp, &dhcp_state_pxebs );
  730. return;
  731. } else {
  732. dhcp_finished ( dhcp, -ETIMEDOUT );
  733. return;
  734. }
  735. }
  736. /* Retransmit current packet */
  737. dhcp_tx ( dhcp );
  738. }
  739. /** PXE Boot Server Discovery state operations */
  740. static struct dhcp_session_state dhcp_state_pxebs = {
  741. .name = "PXEBS",
  742. .tx = dhcp_pxebs_tx,
  743. .rx = dhcp_pxebs_rx,
  744. .expired = dhcp_pxebs_expired,
  745. .tx_msgtype = DHCPREQUEST,
  746. .apply_min_timeout = 1,
  747. };
  748. /****************************************************************************
  749. *
  750. * Packet construction
  751. *
  752. */
  753. /**
  754. * Construct DHCP client hardware address field and broadcast flag
  755. *
  756. * @v netdev Network device
  757. * @v chaddr Hardware address buffer
  758. * @v flags Flags to set (or NULL)
  759. * @ret hlen Hardware address length
  760. */
  761. unsigned int dhcp_chaddr ( struct net_device *netdev, void *chaddr,
  762. uint16_t *flags ) {
  763. struct ll_protocol *ll_protocol = netdev->ll_protocol;
  764. struct dhcphdr *dhcphdr;
  765. int rc;
  766. /* If the link-layer address cannot fit into the chaddr field
  767. * (as is the case for IPoIB) then try using the Ethernet-
  768. * compatible link-layer address. If we do this, set the
  769. * broadcast flag, since chaddr then does not represent a
  770. * valid link-layer address for the return path.
  771. *
  772. * If we cannot produce an Ethernet-compatible link-layer
  773. * address, try using the hardware address.
  774. *
  775. * If even the hardware address is too large, use an empty
  776. * chaddr field and set the broadcast flag.
  777. *
  778. * This goes against RFC4390, but RFC4390 mandates that we use
  779. * a DHCP client identifier that conforms with RFC4361, which
  780. * we cannot do without either persistent (NIC-independent)
  781. * storage, or by eliminating the hardware address completely
  782. * from the DHCP packet, which seems unfriendly to users.
  783. */
  784. if ( ll_protocol->ll_addr_len <= sizeof ( dhcphdr->chaddr ) ) {
  785. memcpy ( chaddr, netdev->ll_addr, ll_protocol->ll_addr_len );
  786. return ll_protocol->ll_addr_len;
  787. }
  788. if ( flags )
  789. *flags |= htons ( BOOTP_FL_BROADCAST );
  790. if ( ( rc = ll_protocol->eth_addr ( netdev->ll_addr, chaddr ) ) == 0 )
  791. return ETH_ALEN;
  792. if ( ll_protocol->hw_addr_len <= sizeof ( dhcphdr->chaddr ) ) {
  793. memcpy ( chaddr, netdev->hw_addr, ll_protocol->hw_addr_len );
  794. return ll_protocol->hw_addr_len;
  795. }
  796. return 0;
  797. }
  798. /**
  799. * Create a DHCP packet
  800. *
  801. * @v dhcppkt DHCP packet structure to fill in
  802. * @v netdev Network device
  803. * @v msgtype DHCP message type
  804. * @v xid Transaction ID (in network-endian order)
  805. * @v options Initial options to include (or NULL)
  806. * @v options_len Length of initial options
  807. * @v data Buffer for DHCP packet
  808. * @v max_len Size of DHCP packet buffer
  809. * @ret rc Return status code
  810. *
  811. * Creates a DHCP packet in the specified buffer, and initialise a
  812. * DHCP packet structure.
  813. */
  814. int dhcp_create_packet ( struct dhcp_packet *dhcppkt,
  815. struct net_device *netdev, uint8_t msgtype,
  816. uint32_t xid, const void *options, size_t options_len,
  817. void *data, size_t max_len ) {
  818. struct dhcphdr *dhcphdr = data;
  819. int rc;
  820. /* Sanity check */
  821. if ( max_len < ( sizeof ( *dhcphdr ) + options_len ) )
  822. return -ENOSPC;
  823. /* Initialise DHCP packet content */
  824. memset ( dhcphdr, 0, max_len );
  825. dhcphdr->xid = xid;
  826. dhcphdr->magic = htonl ( DHCP_MAGIC_COOKIE );
  827. dhcphdr->htype = ntohs ( netdev->ll_protocol->ll_proto );
  828. dhcphdr->op = dhcp_op[msgtype];
  829. dhcphdr->hlen = netdev->ll_protocol->ll_addr_len;
  830. memcpy ( dhcphdr->chaddr, netdev->ll_addr,
  831. netdev->ll_protocol->ll_addr_len );
  832. memcpy ( dhcphdr->options, options, options_len );
  833. /* If the local link-layer address functions only as a name
  834. * (i.e. cannot be used as a destination address), then
  835. * request broadcast responses.
  836. */
  837. if ( netdev->ll_protocol->flags & LL_NAME_ONLY )
  838. dhcphdr->flags |= htons ( BOOTP_FL_BROADCAST );
  839. /* If the network device already has an IPv4 address then
  840. * unicast responses from the DHCP server may be rejected, so
  841. * request broadcast responses.
  842. */
  843. if ( ipv4_has_any_addr ( netdev ) )
  844. dhcphdr->flags |= htons ( BOOTP_FL_BROADCAST );
  845. /* Initialise DHCP packet structure */
  846. memset ( dhcppkt, 0, sizeof ( *dhcppkt ) );
  847. dhcppkt_init ( dhcppkt, data, max_len );
  848. /* Set DHCP_MESSAGE_TYPE option */
  849. if ( ( rc = dhcppkt_store ( dhcppkt, DHCP_MESSAGE_TYPE,
  850. &msgtype, sizeof ( msgtype ) ) ) != 0 )
  851. return rc;
  852. return 0;
  853. }
  854. /**
  855. * Create DHCP request packet
  856. *
  857. * @v dhcppkt DHCP packet structure to fill in
  858. * @v netdev Network device
  859. * @v msgtype DHCP message type
  860. * @v xid Transaction ID (in network-endian order)
  861. * @v ciaddr Client IP address
  862. * @v data Buffer for DHCP packet
  863. * @v max_len Size of DHCP packet buffer
  864. * @ret rc Return status code
  865. *
  866. * Creates a DHCP request packet in the specified buffer, and
  867. * initialise a DHCP packet structure.
  868. */
  869. int dhcp_create_request ( struct dhcp_packet *dhcppkt,
  870. struct net_device *netdev, unsigned int msgtype,
  871. uint32_t xid, struct in_addr ciaddr,
  872. void *data, size_t max_len ) {
  873. struct dhcp_netdev_desc dhcp_desc;
  874. struct dhcp_client_id client_id;
  875. struct dhcp_client_uuid client_uuid;
  876. uint8_t *dhcp_features;
  877. size_t dhcp_features_len;
  878. size_t ll_addr_len;
  879. ssize_t len;
  880. int rc;
  881. /* Create DHCP packet */
  882. if ( ( rc = dhcp_create_packet ( dhcppkt, netdev, msgtype, xid,
  883. dhcp_request_options_data,
  884. sizeof ( dhcp_request_options_data ),
  885. data, max_len ) ) != 0 ) {
  886. DBG ( "DHCP could not create DHCP packet: %s\n",
  887. strerror ( rc ) );
  888. return rc;
  889. }
  890. /* Set client IP address */
  891. dhcppkt->dhcphdr->ciaddr = ciaddr;
  892. /* Add options to identify the feature list */
  893. dhcp_features = table_start ( DHCP_FEATURES );
  894. dhcp_features_len = table_num_entries ( DHCP_FEATURES );
  895. if ( ( rc = dhcppkt_store ( dhcppkt, DHCP_EB_ENCAP, dhcp_features,
  896. dhcp_features_len ) ) != 0 ) {
  897. DBG ( "DHCP could not set features list option: %s\n",
  898. strerror ( rc ) );
  899. return rc;
  900. }
  901. /* Add options to identify the network device */
  902. fetch_setting ( &netdev->settings.settings, &busid_setting, &dhcp_desc,
  903. sizeof ( dhcp_desc ) );
  904. if ( ( rc = dhcppkt_store ( dhcppkt, DHCP_EB_BUS_ID, &dhcp_desc,
  905. sizeof ( dhcp_desc ) ) ) != 0 ) {
  906. DBG ( "DHCP could not set bus ID option: %s\n",
  907. strerror ( rc ) );
  908. return rc;
  909. }
  910. /* Add DHCP client identifier. Required for Infiniband, and
  911. * doesn't hurt other link layers.
  912. */
  913. client_id.ll_proto = ntohs ( netdev->ll_protocol->ll_proto );
  914. ll_addr_len = netdev->ll_protocol->ll_addr_len;
  915. assert ( ll_addr_len <= sizeof ( client_id.ll_addr ) );
  916. memcpy ( client_id.ll_addr, netdev->ll_addr, ll_addr_len );
  917. if ( ( rc = dhcppkt_store ( dhcppkt, DHCP_CLIENT_ID, &client_id,
  918. ( ll_addr_len + 1 ) ) ) != 0 ) {
  919. DBG ( "DHCP could not set client ID: %s\n",
  920. strerror ( rc ) );
  921. return rc;
  922. }
  923. /* Add client UUID, if we have one. Required for PXE. The
  924. * PXE spec does not specify a byte ordering for UUIDs, but
  925. * RFC4578 suggests that it follows the EFI spec, in which the
  926. * first three fields are little-endian.
  927. */
  928. client_uuid.type = DHCP_CLIENT_UUID_TYPE;
  929. if ( ( len = fetch_uuid_setting ( NULL, &uuid_setting,
  930. &client_uuid.uuid ) ) >= 0 ) {
  931. uuid_mangle ( &client_uuid.uuid );
  932. if ( ( rc = dhcppkt_store ( dhcppkt, DHCP_CLIENT_UUID,
  933. &client_uuid,
  934. sizeof ( client_uuid ) ) ) != 0 ) {
  935. DBG ( "DHCP could not set client UUID: %s\n",
  936. strerror ( rc ) );
  937. return rc;
  938. }
  939. }
  940. /* Add user class, if we have one. */
  941. if ( ( len = fetch_setting_len ( NULL, &user_class_setting ) ) >= 0 ) {
  942. char user_class[len];
  943. fetch_setting ( NULL, &user_class_setting, user_class,
  944. sizeof ( user_class ) );
  945. if ( ( rc = dhcppkt_store ( dhcppkt, DHCP_USER_CLASS_ID,
  946. &user_class,
  947. sizeof ( user_class ) ) ) != 0 ) {
  948. DBG ( "DHCP could not set user class: %s\n",
  949. strerror ( rc ) );
  950. return rc;
  951. }
  952. }
  953. return 0;
  954. }
  955. /****************************************************************************
  956. *
  957. * Data transfer interface
  958. *
  959. */
  960. /**
  961. * Transmit DHCP request
  962. *
  963. * @v dhcp DHCP session
  964. * @ret rc Return status code
  965. */
  966. static int dhcp_tx ( struct dhcp_session *dhcp ) {
  967. static struct sockaddr_in peer = {
  968. .sin_family = AF_INET,
  969. };
  970. struct xfer_metadata meta = {
  971. .netdev = dhcp->netdev,
  972. .src = ( struct sockaddr * ) &dhcp->local,
  973. .dest = ( struct sockaddr * ) &peer,
  974. };
  975. struct io_buffer *iobuf;
  976. uint8_t msgtype = dhcp->state->tx_msgtype;
  977. struct dhcp_packet dhcppkt;
  978. int rc;
  979. /* Start retry timer. Do this first so that failures to
  980. * transmit will be retried.
  981. */
  982. start_timer ( &dhcp->timer );
  983. /* Allocate buffer for packet */
  984. iobuf = xfer_alloc_iob ( &dhcp->xfer, DHCP_MIN_LEN );
  985. if ( ! iobuf )
  986. return -ENOMEM;
  987. /* Create basic DHCP packet in temporary buffer */
  988. if ( ( rc = dhcp_create_request ( &dhcppkt, dhcp->netdev, msgtype,
  989. dhcp->xid, dhcp->local.sin_addr,
  990. iobuf->data,
  991. iob_tailroom ( iobuf ) ) ) != 0 ) {
  992. DBGC ( dhcp, "DHCP %p could not construct DHCP request: %s\n",
  993. dhcp, strerror ( rc ) );
  994. goto done;
  995. }
  996. /* (Ab)use the "secs" field to convey metadata about the DHCP
  997. * session state into packet traces. Useful for extracting
  998. * debug information from non-debug builds.
  999. */
  1000. dhcppkt.dhcphdr->secs = htons ( ( ++(dhcp->count) << 2 ) |
  1001. ( dhcp->offer.s_addr ? 0x02 : 0 ) |
  1002. ( dhcp->proxy_offer ? 0x01 : 0 ) );
  1003. /* Fill in packet based on current state */
  1004. if ( ( rc = dhcp->state->tx ( dhcp, &dhcppkt, &peer ) ) != 0 ) {
  1005. DBGC ( dhcp, "DHCP %p could not fill DHCP request: %s\n",
  1006. dhcp, strerror ( rc ) );
  1007. goto done;
  1008. }
  1009. /* Transmit the packet */
  1010. iob_put ( iobuf, dhcppkt_len ( &dhcppkt ) );
  1011. if ( ( rc = xfer_deliver ( &dhcp->xfer, iob_disown ( iobuf ),
  1012. &meta ) ) != 0 ) {
  1013. DBGC ( dhcp, "DHCP %p could not transmit UDP packet: %s\n",
  1014. dhcp, strerror ( rc ) );
  1015. goto done;
  1016. }
  1017. done:
  1018. free_iob ( iobuf );
  1019. return rc;
  1020. }
  1021. /**
  1022. * Receive new data
  1023. *
  1024. * @v dhcp DHCP session
  1025. * @v iobuf I/O buffer
  1026. * @v meta Transfer metadata
  1027. * @ret rc Return status code
  1028. */
  1029. static int dhcp_deliver ( struct dhcp_session *dhcp,
  1030. struct io_buffer *iobuf,
  1031. struct xfer_metadata *meta ) {
  1032. struct sockaddr_in *peer;
  1033. size_t data_len;
  1034. struct dhcp_packet *dhcppkt;
  1035. struct dhcphdr *dhcphdr;
  1036. uint8_t msgtype = 0;
  1037. struct in_addr server_id = { 0 };
  1038. int rc = 0;
  1039. /* Sanity checks */
  1040. if ( ! meta->src ) {
  1041. DBGC ( dhcp, "DHCP %p received packet without source port\n",
  1042. dhcp );
  1043. rc = -EINVAL;
  1044. goto err_no_src;
  1045. }
  1046. peer = ( struct sockaddr_in * ) meta->src;
  1047. /* Create a DHCP packet containing the I/O buffer contents.
  1048. * Whilst we could just use the original buffer in situ, that
  1049. * would waste the unused space in the packet buffer, and also
  1050. * waste a relatively scarce fully-aligned I/O buffer.
  1051. */
  1052. data_len = iob_len ( iobuf );
  1053. dhcppkt = zalloc ( sizeof ( *dhcppkt ) + data_len );
  1054. if ( ! dhcppkt ) {
  1055. rc = -ENOMEM;
  1056. goto err_alloc_dhcppkt;
  1057. }
  1058. dhcphdr = ( ( ( void * ) dhcppkt ) + sizeof ( *dhcppkt ) );
  1059. memcpy ( dhcphdr, iobuf->data, data_len );
  1060. dhcppkt_init ( dhcppkt, dhcphdr, data_len );
  1061. /* Identify message type */
  1062. dhcppkt_fetch ( dhcppkt, DHCP_MESSAGE_TYPE, &msgtype,
  1063. sizeof ( msgtype ) );
  1064. /* Identify server ID */
  1065. dhcppkt_fetch ( dhcppkt, DHCP_SERVER_IDENTIFIER,
  1066. &server_id, sizeof ( server_id ) );
  1067. /* Check for matching transaction ID */
  1068. if ( dhcphdr->xid != dhcp->xid ) {
  1069. DBGC ( dhcp, "DHCP %p %s from %s:%d has bad transaction "
  1070. "ID\n", dhcp, dhcp_msgtype_name ( msgtype ),
  1071. inet_ntoa ( peer->sin_addr ),
  1072. ntohs ( peer->sin_port ) );
  1073. rc = -EINVAL;
  1074. goto err_xid;
  1075. };
  1076. /* Handle packet based on current state */
  1077. dhcp->state->rx ( dhcp, dhcppkt, peer, msgtype, server_id );
  1078. err_xid:
  1079. dhcppkt_put ( dhcppkt );
  1080. err_alloc_dhcppkt:
  1081. err_no_src:
  1082. free_iob ( iobuf );
  1083. return rc;
  1084. }
  1085. /** DHCP data transfer interface operations */
  1086. static struct interface_operation dhcp_xfer_operations[] = {
  1087. INTF_OP ( xfer_deliver, struct dhcp_session *, dhcp_deliver ),
  1088. };
  1089. /** DHCP data transfer interface descriptor */
  1090. static struct interface_descriptor dhcp_xfer_desc =
  1091. INTF_DESC ( struct dhcp_session, xfer, dhcp_xfer_operations );
  1092. /**
  1093. * Handle DHCP retry timer expiry
  1094. *
  1095. * @v timer DHCP retry timer
  1096. * @v fail Failure indicator
  1097. */
  1098. static void dhcp_timer_expired ( struct retry_timer *timer, int fail ) {
  1099. struct dhcp_session *dhcp =
  1100. container_of ( timer, struct dhcp_session, timer );
  1101. /* If we have failed, terminate DHCP */
  1102. if ( fail ) {
  1103. dhcp_finished ( dhcp, -ETIMEDOUT );
  1104. return;
  1105. }
  1106. /* Handle timer expiry based on current state */
  1107. dhcp->state->expired ( dhcp );
  1108. }
  1109. /****************************************************************************
  1110. *
  1111. * Job control interface
  1112. *
  1113. */
  1114. /** DHCP job control interface operations */
  1115. static struct interface_operation dhcp_job_op[] = {
  1116. INTF_OP ( intf_close, struct dhcp_session *, dhcp_finished ),
  1117. };
  1118. /** DHCP job control interface descriptor */
  1119. static struct interface_descriptor dhcp_job_desc =
  1120. INTF_DESC ( struct dhcp_session, job, dhcp_job_op );
  1121. /****************************************************************************
  1122. *
  1123. * Instantiators
  1124. *
  1125. */
  1126. /**
  1127. * DHCP peer address for socket opening
  1128. *
  1129. * This is a dummy address; the only useful portion is the socket
  1130. * family (so that we get a UDP connection). The DHCP client will set
  1131. * the IP address and source port explicitly on each transmission.
  1132. */
  1133. static struct sockaddr dhcp_peer = {
  1134. .sa_family = AF_INET,
  1135. };
  1136. /**
  1137. * Get cached DHCPACK where none exists
  1138. */
  1139. __weak void get_cached_dhcpack ( void ) { __keepme }
  1140. /**
  1141. * Start DHCP state machine on a network device
  1142. *
  1143. * @v job Job control interface
  1144. * @v netdev Network device
  1145. * @ret rc Return status code, or positive if cached
  1146. *
  1147. * Starts DHCP on the specified network device. If successful, the
  1148. * DHCPACK (and ProxyDHCPACK, if applicable) will be registered as
  1149. * option sources.
  1150. *
  1151. * On a return of 0, a background job has been started to perform the
  1152. * DHCP request. Any nonzero return means the job has not been
  1153. * started; a positive return value indicates the success condition of
  1154. * having fetched the appropriate data from cached information.
  1155. */
  1156. int start_dhcp ( struct interface *job, struct net_device *netdev ) {
  1157. struct dhcp_session *dhcp;
  1158. int rc;
  1159. /* Check for cached DHCP information */
  1160. get_cached_dhcpack();
  1161. if ( fetch_uintz_setting ( NULL, &use_cached_setting ) ) {
  1162. DBG ( "DHCP using cached network settings\n" );
  1163. return 1;
  1164. }
  1165. /* Allocate and initialise structure */
  1166. dhcp = zalloc ( sizeof ( *dhcp ) );
  1167. if ( ! dhcp )
  1168. return -ENOMEM;
  1169. ref_init ( &dhcp->refcnt, dhcp_free );
  1170. intf_init ( &dhcp->job, &dhcp_job_desc, &dhcp->refcnt );
  1171. intf_init ( &dhcp->xfer, &dhcp_xfer_desc, &dhcp->refcnt );
  1172. timer_init ( &dhcp->timer, dhcp_timer_expired, &dhcp->refcnt );
  1173. dhcp->netdev = netdev_get ( netdev );
  1174. dhcp->local.sin_family = AF_INET;
  1175. dhcp->local.sin_port = htons ( BOOTPC_PORT );
  1176. dhcp->xid = random();
  1177. /* Store DHCP transaction ID for fakedhcp code */
  1178. dhcp_last_xid = dhcp->xid;
  1179. /* Instantiate child objects and attach to our interfaces */
  1180. if ( ( rc = xfer_open_socket ( &dhcp->xfer, SOCK_DGRAM, &dhcp_peer,
  1181. ( struct sockaddr * ) &dhcp->local ) ) != 0 )
  1182. goto err;
  1183. /* Enter DHCPDISCOVER state */
  1184. dhcp_set_state ( dhcp, &dhcp_state_discover );
  1185. /* Attach parent interface, mortalise self, and return */
  1186. intf_plug_plug ( &dhcp->job, job );
  1187. ref_put ( &dhcp->refcnt );
  1188. return 0;
  1189. err:
  1190. dhcp_finished ( dhcp, rc );
  1191. ref_put ( &dhcp->refcnt );
  1192. return rc;
  1193. }
  1194. /**
  1195. * Retrieve list of PXE boot servers for a given server type
  1196. *
  1197. * @v dhcp DHCP session
  1198. * @v raw DHCP PXE boot server list
  1199. * @v raw_len Length of DHCP PXE boot server list
  1200. * @v ip IP address list to fill in
  1201. *
  1202. * The caller must ensure that the IP address list has sufficient
  1203. * space.
  1204. */
  1205. static void pxebs_list ( struct dhcp_session *dhcp, void *raw,
  1206. size_t raw_len, struct in_addr *ip ) {
  1207. struct dhcp_pxe_boot_server *server = raw;
  1208. size_t server_len;
  1209. unsigned int i;
  1210. while ( raw_len ) {
  1211. if ( raw_len < sizeof ( *server ) ) {
  1212. DBGC ( dhcp, "DHCP %p malformed PXE server list\n",
  1213. dhcp );
  1214. break;
  1215. }
  1216. server_len = offsetof ( typeof ( *server ),
  1217. ip[ server->num_ip ] );
  1218. if ( raw_len < server_len ) {
  1219. DBGC ( dhcp, "DHCP %p malformed PXE server list\n",
  1220. dhcp );
  1221. break;
  1222. }
  1223. if ( server->type == dhcp->pxe_type ) {
  1224. for ( i = 0 ; i < server->num_ip ; i++ )
  1225. *(ip++) = server->ip[i];
  1226. }
  1227. server = ( ( ( void * ) server ) + server_len );
  1228. raw_len -= server_len;
  1229. }
  1230. }
  1231. /**
  1232. * Start PXE Boot Server Discovery on a network device
  1233. *
  1234. * @v job Job control interface
  1235. * @v netdev Network device
  1236. * @v pxe_type PXE server type
  1237. * @ret rc Return status code
  1238. *
  1239. * Starts PXE Boot Server Discovery on the specified network device.
  1240. * If successful, the Boot Server ACK will be registered as an option
  1241. * source.
  1242. */
  1243. int start_pxebs ( struct interface *job, struct net_device *netdev,
  1244. unsigned int pxe_type ) {
  1245. struct setting pxe_discovery_control_setting =
  1246. { .tag = DHCP_PXE_DISCOVERY_CONTROL };
  1247. struct setting pxe_boot_servers_setting =
  1248. { .tag = DHCP_PXE_BOOT_SERVERS };
  1249. struct setting pxe_boot_server_mcast_setting =
  1250. { .tag = DHCP_PXE_BOOT_SERVER_MCAST };
  1251. ssize_t pxebs_list_len;
  1252. struct dhcp_session *dhcp;
  1253. struct in_addr *ip;
  1254. unsigned int pxe_discovery_control;
  1255. int rc;
  1256. /* Get upper bound for PXE boot server IP address list */
  1257. pxebs_list_len = fetch_setting_len ( NULL, &pxe_boot_servers_setting );
  1258. if ( pxebs_list_len < 0 )
  1259. pxebs_list_len = 0;
  1260. /* Allocate and initialise structure */
  1261. dhcp = zalloc ( sizeof ( *dhcp ) + sizeof ( *ip ) /* mcast */ +
  1262. sizeof ( *ip ) /* bcast */ + pxebs_list_len +
  1263. sizeof ( *ip ) /* terminator */ );
  1264. if ( ! dhcp )
  1265. return -ENOMEM;
  1266. ref_init ( &dhcp->refcnt, dhcp_free );
  1267. intf_init ( &dhcp->job, &dhcp_job_desc, &dhcp->refcnt );
  1268. intf_init ( &dhcp->xfer, &dhcp_xfer_desc, &dhcp->refcnt );
  1269. timer_init ( &dhcp->timer, dhcp_timer_expired, &dhcp->refcnt );
  1270. dhcp->netdev = netdev_get ( netdev );
  1271. dhcp->local.sin_family = AF_INET;
  1272. fetch_ipv4_setting ( netdev_settings ( netdev ), &ip_setting,
  1273. &dhcp->local.sin_addr );
  1274. dhcp->local.sin_port = htons ( BOOTPC_PORT );
  1275. dhcp->pxe_type = cpu_to_le16 ( pxe_type );
  1276. /* Construct PXE boot server IP address lists */
  1277. pxe_discovery_control =
  1278. fetch_uintz_setting ( NULL, &pxe_discovery_control_setting );
  1279. ip = ( ( ( void * ) dhcp ) + sizeof ( *dhcp ) );
  1280. dhcp->pxe_attempt = ip;
  1281. if ( ! ( pxe_discovery_control & PXEBS_NO_MULTICAST ) ) {
  1282. fetch_ipv4_setting ( NULL, &pxe_boot_server_mcast_setting, ip);
  1283. if ( ip->s_addr )
  1284. ip++;
  1285. }
  1286. if ( ! ( pxe_discovery_control & PXEBS_NO_BROADCAST ) )
  1287. (ip++)->s_addr = INADDR_BROADCAST;
  1288. if ( pxe_discovery_control & PXEBS_NO_UNKNOWN_SERVERS )
  1289. dhcp->pxe_accept = ip;
  1290. if ( pxebs_list_len ) {
  1291. uint8_t buf[pxebs_list_len];
  1292. fetch_setting ( NULL, &pxe_boot_servers_setting,
  1293. buf, sizeof ( buf ) );
  1294. pxebs_list ( dhcp, buf, sizeof ( buf ), ip );
  1295. }
  1296. if ( ! dhcp->pxe_attempt->s_addr ) {
  1297. DBGC ( dhcp, "DHCP %p has no PXE boot servers for type %04x\n",
  1298. dhcp, pxe_type );
  1299. rc = -EINVAL;
  1300. goto err;
  1301. }
  1302. /* Dump out PXE server lists */
  1303. DBGC ( dhcp, "DHCP %p attempting", dhcp );
  1304. for ( ip = dhcp->pxe_attempt ; ip->s_addr ; ip++ )
  1305. DBGC ( dhcp, " %s", inet_ntoa ( *ip ) );
  1306. DBGC ( dhcp, "\n" );
  1307. if ( dhcp->pxe_accept ) {
  1308. DBGC ( dhcp, "DHCP %p accepting", dhcp );
  1309. for ( ip = dhcp->pxe_accept ; ip->s_addr ; ip++ )
  1310. DBGC ( dhcp, " %s", inet_ntoa ( *ip ) );
  1311. DBGC ( dhcp, "\n" );
  1312. }
  1313. /* Instantiate child objects and attach to our interfaces */
  1314. if ( ( rc = xfer_open_socket ( &dhcp->xfer, SOCK_DGRAM, &dhcp_peer,
  1315. ( struct sockaddr * ) &dhcp->local ) ) != 0 )
  1316. goto err;
  1317. /* Enter PXEBS state */
  1318. dhcp_set_state ( dhcp, &dhcp_state_pxebs );
  1319. /* Attach parent interface, mortalise self, and return */
  1320. intf_plug_plug ( &dhcp->job, job );
  1321. ref_put ( &dhcp->refcnt );
  1322. return 0;
  1323. err:
  1324. dhcp_finished ( dhcp, rc );
  1325. ref_put ( &dhcp->refcnt );
  1326. return rc;
  1327. }