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

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