選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

dhcp.c 37KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372
  1. /*
  2. * Copyright (C) 2006 Michael Brown <mbrown@fensystems.co.uk>.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation; either version 2 of the
  7. * License, or any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18. #include <string.h>
  19. #include <stdlib.h>
  20. #include <stdio.h>
  21. #include <ctype.h>
  22. #include <errno.h>
  23. #include <assert.h>
  24. #include <byteswap.h>
  25. #include <gpxe/if_ether.h>
  26. #include <gpxe/netdevice.h>
  27. #include <gpxe/device.h>
  28. #include <gpxe/xfer.h>
  29. #include <gpxe/open.h>
  30. #include <gpxe/job.h>
  31. #include <gpxe/retry.h>
  32. #include <gpxe/tcpip.h>
  33. #include <gpxe/ip.h>
  34. #include <gpxe/uuid.h>
  35. #include <gpxe/timer.h>
  36. #include <gpxe/settings.h>
  37. #include <gpxe/dhcp.h>
  38. #include <gpxe/dhcpopts.h>
  39. #include <gpxe/dhcppkt.h>
  40. #include <gpxe/features.h>
  41. /** @file
  42. *
  43. * Dynamic Host Configuration Protocol
  44. *
  45. */
  46. struct dhcp_session;
  47. static int dhcp_tx ( struct dhcp_session *dhcp );
  48. /**
  49. * DHCP operation types
  50. *
  51. * This table maps from DHCP message types (i.e. values of the @c
  52. * DHCP_MESSAGE_TYPE option) to values of the "op" field within a DHCP
  53. * packet.
  54. */
  55. static const uint8_t dhcp_op[] = {
  56. [DHCPDISCOVER] = BOOTP_REQUEST,
  57. [DHCPOFFER] = BOOTP_REPLY,
  58. [DHCPREQUEST] = BOOTP_REQUEST,
  59. [DHCPDECLINE] = BOOTP_REQUEST,
  60. [DHCPACK] = BOOTP_REPLY,
  61. [DHCPNAK] = BOOTP_REPLY,
  62. [DHCPRELEASE] = BOOTP_REQUEST,
  63. [DHCPINFORM] = BOOTP_REQUEST,
  64. };
  65. /** Raw option data for options common to all DHCP requests */
  66. static uint8_t dhcp_request_options_data[] = {
  67. DHCP_MAX_MESSAGE_SIZE,
  68. DHCP_WORD ( ETH_MAX_MTU - 20 /* IP header */ - 8 /* UDP header */ ),
  69. DHCP_CLIENT_ARCHITECTURE, DHCP_WORD ( 0 ),
  70. DHCP_CLIENT_NDI, DHCP_OPTION ( 1 /* UNDI */ , 2, 1 /* v2.1 */ ),
  71. DHCP_VENDOR_CLASS_ID,
  72. DHCP_STRING ( 'P', 'X', 'E', 'C', 'l', 'i', 'e', 'n', 't', ':',
  73. 'A', 'r', 'c', 'h', ':', '0', '0', '0', '0', '0', ':',
  74. 'U', 'N', 'D', 'I', ':', '0', '0', '2', '0', '0', '1' ),
  75. DHCP_USER_CLASS_ID,
  76. DHCP_STRING ( 'g', 'P', 'X', 'E' ),
  77. DHCP_PARAMETER_REQUEST_LIST,
  78. DHCP_OPTION ( DHCP_SUBNET_MASK, DHCP_ROUTERS, DHCP_DNS_SERVERS,
  79. DHCP_LOG_SERVERS, DHCP_HOST_NAME, DHCP_DOMAIN_NAME,
  80. DHCP_ROOT_PATH, DHCP_VENDOR_ENCAP, DHCP_VENDOR_CLASS_ID,
  81. DHCP_TFTP_SERVER_NAME, DHCP_BOOTFILE_NAME,
  82. DHCP_EB_ENCAP, DHCP_ISCSI_INITIATOR_IQN ),
  83. DHCP_END
  84. };
  85. /** DHCP feature codes */
  86. static uint8_t dhcp_features[0] __table_start ( uint8_t, dhcp_features );
  87. static uint8_t dhcp_features_end[0] __table_end ( uint8_t, dhcp_features );
  88. /** Version number feature */
  89. FEATURE_VERSION ( VERSION_MAJOR, VERSION_MINOR, VERSION_PATCH );
  90. /** DHCP server address setting */
  91. struct setting dhcp_server_setting __setting = {
  92. .name = "dhcp-server",
  93. .description = "DHCP server address",
  94. .tag = DHCP_SERVER_IDENTIFIER,
  95. .type = &setting_type_ipv4,
  96. };
  97. /** DHCP user class setting */
  98. struct setting user_class_setting __setting = {
  99. .name = "user-class",
  100. .description = "User class identifier",
  101. .tag = DHCP_USER_CLASS_ID,
  102. .type = &setting_type_string,
  103. };
  104. /**
  105. * Name a DHCP packet type
  106. *
  107. * @v msgtype DHCP message type
  108. * @ret string DHCP mesasge type name
  109. */
  110. static inline const char * dhcp_msgtype_name ( unsigned int msgtype ) {
  111. switch ( msgtype ) {
  112. case DHCPNONE: return "BOOTP"; /* Non-DHCP packet */
  113. case DHCPDISCOVER: return "DHCPDISCOVER";
  114. case DHCPOFFER: return "DHCPOFFER";
  115. case DHCPREQUEST: return "DHCPREQUEST";
  116. case DHCPDECLINE: return "DHCPDECLINE";
  117. case DHCPACK: return "DHCPACK";
  118. case DHCPNAK: return "DHCPNAK";
  119. case DHCPRELEASE: return "DHCPRELEASE";
  120. case DHCPINFORM: return "DHCPINFORM";
  121. default: return "DHCP<invalid>";
  122. }
  123. }
  124. /**
  125. * Calculate DHCP transaction ID for a network device
  126. *
  127. * @v netdev Network device
  128. * @ret xid DHCP XID
  129. *
  130. * Extract the least significant bits of the hardware address for use
  131. * as the transaction ID.
  132. */
  133. static uint32_t dhcp_xid ( struct net_device *netdev ) {
  134. uint32_t xid;
  135. memcpy ( &xid, ( netdev->ll_addr + netdev->ll_protocol->ll_addr_len
  136. - sizeof ( xid ) ), sizeof ( xid ) );
  137. return xid;
  138. }
  139. /****************************************************************************
  140. *
  141. * DHCP session
  142. *
  143. */
  144. struct dhcp_session;
  145. /** DHCP session state operations */
  146. struct dhcp_session_state {
  147. /** State name */
  148. const char *name;
  149. /**
  150. * Construct transmitted packet
  151. *
  152. * @v dhcp DHCP session
  153. * @v dhcppkt DHCP packet
  154. * @v peer Destination address
  155. */
  156. int ( * tx ) ( struct dhcp_session *dhcp,
  157. struct dhcp_packet *dhcppkt,
  158. struct sockaddr_in *peer );
  159. /** Handle received packet
  160. *
  161. * @v dhcp DHCP session
  162. * @v dhcppkt DHCP packet
  163. * @v peer DHCP server address
  164. * @v msgtype DHCP message type
  165. * @v server_id DHCP server ID
  166. */
  167. void ( * rx ) ( struct dhcp_session *dhcp,
  168. struct dhcp_packet *dhcppkt,
  169. struct sockaddr_in *peer,
  170. uint8_t msgtype, struct in_addr server_id );
  171. /** Handle timer expiry
  172. *
  173. * @v dhcp DHCP session
  174. */
  175. void ( * expired ) ( struct dhcp_session *dhcp );
  176. /** Transmitted message type */
  177. uint8_t tx_msgtype;
  178. /** Apply minimum timeout */
  179. uint8_t apply_min_timeout;
  180. };
  181. static struct dhcp_session_state dhcp_state_discover;
  182. static struct dhcp_session_state dhcp_state_request;
  183. static struct dhcp_session_state dhcp_state_proxy;
  184. static struct dhcp_session_state dhcp_state_pxebs;
  185. /** A DHCP session */
  186. struct dhcp_session {
  187. /** Reference counter */
  188. struct refcnt refcnt;
  189. /** Job control interface */
  190. struct job_interface job;
  191. /** Data transfer interface */
  192. struct xfer_interface xfer;
  193. /** Network device being configured */
  194. struct net_device *netdev;
  195. /** Local socket address */
  196. struct sockaddr_in local;
  197. /** State of the session */
  198. struct dhcp_session_state *state;
  199. /** Offered IP address */
  200. struct in_addr offer;
  201. /** DHCP server */
  202. struct in_addr server;
  203. /** DHCP offer priority */
  204. int priority;
  205. /** ProxyDHCP protocol extensions should be ignored */
  206. int no_pxedhcp;
  207. /** ProxyDHCP server */
  208. struct in_addr proxy_server;
  209. /** ProxyDHCP server 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. /** Start time of the current state (in ticks) */
  220. unsigned long start;
  221. };
  222. /**
  223. * Free DHCP session
  224. *
  225. * @v refcnt Reference counter
  226. */
  227. static void dhcp_free ( struct refcnt *refcnt ) {
  228. struct dhcp_session *dhcp =
  229. container_of ( refcnt, struct dhcp_session, refcnt );
  230. netdev_put ( dhcp->netdev );
  231. free ( dhcp );
  232. }
  233. /**
  234. * Mark DHCP session as complete
  235. *
  236. * @v dhcp DHCP session
  237. * @v rc Return status code
  238. */
  239. static void dhcp_finished ( struct dhcp_session *dhcp, int rc ) {
  240. /* Block futher incoming messages */
  241. job_nullify ( &dhcp->job );
  242. xfer_nullify ( &dhcp->xfer );
  243. /* Stop retry timer */
  244. stop_timer ( &dhcp->timer );
  245. /* Free resources and close interfaces */
  246. xfer_close ( &dhcp->xfer, rc );
  247. job_done ( &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. *
  268. * DHCP state machine
  269. *
  270. */
  271. /**
  272. * Construct transmitted packet for DHCP discovery
  273. *
  274. * @v dhcp DHCP session
  275. * @v dhcppkt DHCP packet
  276. * @v peer Destination address
  277. */
  278. static int dhcp_discovery_tx ( struct dhcp_session *dhcp,
  279. struct dhcp_packet *dhcppkt __unused,
  280. struct sockaddr_in *peer ) {
  281. DBGC ( dhcp, "DHCP %p DHCPDISCOVER\n", dhcp );
  282. /* Set server address */
  283. peer->sin_addr.s_addr = INADDR_BROADCAST;
  284. peer->sin_port = htons ( BOOTPS_PORT );
  285. return 0;
  286. }
  287. /**
  288. * Handle received packet during DHCP discovery
  289. *
  290. * @v dhcp DHCP session
  291. * @v dhcppkt DHCP packet
  292. * @v peer DHCP server address
  293. * @v msgtype DHCP message type
  294. * @v server_id DHCP server ID
  295. */
  296. static void dhcp_discovery_rx ( struct dhcp_session *dhcp,
  297. struct dhcp_packet *dhcppkt,
  298. struct sockaddr_in *peer, uint8_t msgtype,
  299. struct in_addr server_id ) {
  300. struct in_addr ip;
  301. char vci[9]; /* "PXEClient" */
  302. int vci_len;
  303. int has_pxeclient;
  304. int8_t priority = 0;
  305. uint8_t no_pxedhcp = 0;
  306. unsigned long elapsed;
  307. DBGC ( dhcp, "DHCP %p %s from %s:%d", dhcp,
  308. dhcp_msgtype_name ( msgtype ), inet_ntoa ( peer->sin_addr ),
  309. ntohs ( peer->sin_port ) );
  310. if ( server_id.s_addr != peer->sin_addr.s_addr )
  311. DBGC ( dhcp, " (%s)", inet_ntoa ( server_id ) );
  312. /* Identify offered IP address */
  313. ip = dhcppkt->dhcphdr->yiaddr;
  314. if ( ip.s_addr )
  315. DBGC ( dhcp, " for %s", inet_ntoa ( ip ) );
  316. /* Identify "PXEClient" vendor class */
  317. vci_len = dhcppkt_fetch ( dhcppkt, DHCP_VENDOR_CLASS_ID,
  318. vci, sizeof ( vci ) );
  319. has_pxeclient = ( ( vci_len >= ( int ) sizeof ( vci ) ) &&
  320. ( strncmp ( "PXEClient", vci, sizeof (vci) ) == 0 ));
  321. if ( has_pxeclient )
  322. DBGC ( dhcp, " pxe" );
  323. /* Identify priority */
  324. dhcppkt_fetch ( dhcppkt, DHCP_EB_PRIORITY, &priority,
  325. sizeof ( priority ) );
  326. if ( priority )
  327. DBGC ( dhcp, " pri %d", priority );
  328. /* Identify ignore-PXE flag */
  329. dhcppkt_fetch ( dhcppkt, DHCP_EB_NO_PXEDHCP, &no_pxedhcp,
  330. sizeof ( no_pxedhcp ) );
  331. if ( no_pxedhcp )
  332. DBGC ( dhcp, " nopxe" );
  333. DBGC ( dhcp, "\n" );
  334. /* Select as DHCP offer, if applicable */
  335. if ( ip.s_addr && ( peer->sin_port == htons ( BOOTPS_PORT ) ) &&
  336. ( ( msgtype == DHCPOFFER ) || ( ! msgtype /* BOOTP */ ) ) &&
  337. ( priority >= dhcp->priority ) ) {
  338. dhcp->offer = ip;
  339. dhcp->server = server_id;
  340. dhcp->priority = priority;
  341. dhcp->no_pxedhcp = no_pxedhcp;
  342. }
  343. /* Select as ProxyDHCP offer, if applicable */
  344. if ( has_pxeclient && ( msgtype == DHCPOFFER ) &&
  345. ( priority >= dhcp->proxy_priority ) ) {
  346. dhcp->proxy_server = server_id;
  347. dhcp->proxy_priority = priority;
  348. }
  349. /* We can exit the discovery state when we have a valid
  350. * DHCPOFFER, and either:
  351. *
  352. * o The DHCPOFFER instructs us to ignore ProxyDHCPOFFERs, or
  353. * o We have a valid ProxyDHCPOFFER, or
  354. * o We have allowed sufficient time for ProxyDHCPOFFERs.
  355. */
  356. /* If we don't yet have a DHCPOFFER, do nothing */
  357. if ( ! dhcp->offer.s_addr )
  358. return;
  359. /* If we can't yet transition to DHCPREQUEST, do nothing */
  360. elapsed = ( currticks() - dhcp->start );
  361. if ( ! ( dhcp->no_pxedhcp || dhcp->proxy_server.s_addr ||
  362. ( elapsed > PROXYDHCP_MAX_TIMEOUT ) ) )
  363. return;
  364. /* Transition to DHCPREQUEST */
  365. dhcp_set_state ( dhcp, &dhcp_state_request );
  366. }
  367. /**
  368. * Handle timer expiry during DHCP discovery
  369. *
  370. * @v dhcp DHCP session
  371. */
  372. static void dhcp_discovery_expired ( struct dhcp_session *dhcp ) {
  373. unsigned long elapsed = ( currticks() - dhcp->start );
  374. /* Give up waiting for ProxyDHCP before we reach the failure point */
  375. if ( dhcp->offer.s_addr && ( elapsed > PROXYDHCP_MAX_TIMEOUT ) ) {
  376. dhcp_set_state ( dhcp, &dhcp_state_request );
  377. return;
  378. }
  379. /* Otherwise, retransmit current packet */
  380. dhcp_tx ( dhcp );
  381. }
  382. /** DHCP discovery state operations */
  383. static struct dhcp_session_state dhcp_state_discover = {
  384. .name = "discovery",
  385. .tx = dhcp_discovery_tx,
  386. .rx = dhcp_discovery_rx,
  387. .expired = dhcp_discovery_expired,
  388. .tx_msgtype = DHCPDISCOVER,
  389. .apply_min_timeout = 1,
  390. };
  391. /**
  392. * Construct transmitted packet for DHCP request
  393. *
  394. * @v dhcp DHCP session
  395. * @v dhcppkt DHCP packet
  396. * @v peer Destination address
  397. */
  398. static int dhcp_request_tx ( struct dhcp_session *dhcp,
  399. struct dhcp_packet *dhcppkt,
  400. struct sockaddr_in *peer ) {
  401. int rc;
  402. DBGC ( dhcp, "DHCP %p DHCPREQUEST to %s:%d",
  403. dhcp, inet_ntoa ( dhcp->server ), BOOTPS_PORT );
  404. DBGC ( dhcp, " for %s\n", inet_ntoa ( dhcp->offer ) );
  405. /* Set server ID */
  406. if ( ( rc = dhcppkt_store ( dhcppkt, DHCP_SERVER_IDENTIFIER,
  407. &dhcp->server,
  408. sizeof ( dhcp->server ) ) ) != 0 )
  409. return rc;
  410. /* Set requested IP address */
  411. if ( ( rc = dhcppkt_store ( dhcppkt, DHCP_REQUESTED_ADDRESS,
  412. &dhcp->offer,
  413. sizeof ( dhcp->offer ) ) ) != 0 )
  414. return rc;
  415. /* Set server address */
  416. peer->sin_addr.s_addr = INADDR_BROADCAST;
  417. peer->sin_port = htons ( BOOTPS_PORT );
  418. return 0;
  419. }
  420. /**
  421. * Handle received packet during DHCP request
  422. *
  423. * @v dhcp DHCP session
  424. * @v dhcppkt DHCP packet
  425. * @v peer DHCP server address
  426. * @v msgtype DHCP message type
  427. * @v server_id DHCP server ID
  428. */
  429. static void dhcp_request_rx ( struct dhcp_session *dhcp,
  430. struct dhcp_packet *dhcppkt,
  431. struct sockaddr_in *peer, uint8_t msgtype,
  432. struct in_addr server_id ) {
  433. struct in_addr ip;
  434. struct settings *parent;
  435. int rc;
  436. DBGC ( dhcp, "DHCP %p %s from %s:%d", dhcp,
  437. dhcp_msgtype_name ( msgtype ), inet_ntoa ( peer->sin_addr ),
  438. ntohs ( peer->sin_port ) );
  439. if ( server_id.s_addr != peer->sin_addr.s_addr )
  440. DBGC ( dhcp, " (%s)", inet_ntoa ( server_id ) );
  441. /* Identify leased IP address */
  442. ip = dhcppkt->dhcphdr->yiaddr;
  443. if ( ip.s_addr )
  444. DBGC ( dhcp, " for %s", inet_ntoa ( ip ) );
  445. DBGC ( dhcp, "\n" );
  446. /* Filter out unacceptable responses */
  447. if ( peer->sin_port != htons ( BOOTPS_PORT ) )
  448. return;
  449. if ( msgtype /* BOOTP */ && ( msgtype != DHCPACK ) )
  450. return;
  451. if ( server_id.s_addr != dhcp->server.s_addr )
  452. return;
  453. /* Record assigned address */
  454. dhcp->local.sin_addr = ip;
  455. /* Register settings */
  456. parent = netdev_settings ( dhcp->netdev );
  457. if ( ( rc = register_settings ( &dhcppkt->settings, parent ) ) != 0 ){
  458. DBGC ( dhcp, "DHCP %p could not register settings: %s\n",
  459. dhcp, strerror ( rc ) );
  460. dhcp_finished ( dhcp, rc );
  461. return;
  462. }
  463. /* Start ProxyDHCPREQUEST if applicable */
  464. if ( dhcp->proxy_server.s_addr && ( ! dhcp->no_pxedhcp ) ) {
  465. dhcp_set_state ( dhcp, &dhcp_state_proxy );
  466. return;
  467. }
  468. /* Terminate DHCP */
  469. dhcp_finished ( dhcp, 0 );
  470. }
  471. /**
  472. * Handle timer expiry during DHCP discovery
  473. *
  474. * @v dhcp DHCP session
  475. */
  476. static void dhcp_request_expired ( struct dhcp_session *dhcp ) {
  477. /* Retransmit current packet */
  478. dhcp_tx ( dhcp );
  479. }
  480. /** DHCP request state operations */
  481. static struct dhcp_session_state dhcp_state_request = {
  482. .name = "request",
  483. .tx = dhcp_request_tx,
  484. .rx = dhcp_request_rx,
  485. .expired = dhcp_request_expired,
  486. .tx_msgtype = DHCPREQUEST,
  487. .apply_min_timeout = 0,
  488. };
  489. /**
  490. * Construct transmitted packet for ProxyDHCP request
  491. *
  492. * @v dhcp DHCP session
  493. * @v dhcppkt DHCP packet
  494. * @v peer Destination address
  495. */
  496. static int dhcp_proxy_tx ( struct dhcp_session *dhcp,
  497. struct dhcp_packet *dhcppkt,
  498. struct sockaddr_in *peer ) {
  499. int rc;
  500. DBGC ( dhcp, "DHCP %p ProxyDHCP REQUEST to %s:%d\n",
  501. dhcp, inet_ntoa ( dhcp->proxy_server ), PXE_PORT );
  502. /* Set server ID */
  503. if ( ( rc = dhcppkt_store ( dhcppkt, DHCP_SERVER_IDENTIFIER,
  504. &dhcp->proxy_server,
  505. sizeof ( dhcp->proxy_server ) ) ) != 0 )
  506. return rc;
  507. /* Set server address */
  508. peer->sin_addr = dhcp->proxy_server;
  509. peer->sin_port = htons ( PXE_PORT );
  510. return 0;
  511. }
  512. /**
  513. * Handle received packet during ProxyDHCP request
  514. *
  515. * @v dhcp DHCP session
  516. * @v dhcppkt DHCP packet
  517. * @v peer DHCP server address
  518. * @v msgtype DHCP message type
  519. * @v server_id DHCP server ID
  520. */
  521. static void dhcp_proxy_rx ( struct dhcp_session *dhcp,
  522. struct dhcp_packet *dhcppkt,
  523. struct sockaddr_in *peer, uint8_t msgtype,
  524. struct in_addr server_id ) {
  525. int rc;
  526. DBGC ( dhcp, "DHCP %p %s from %s:%d", dhcp,
  527. dhcp_msgtype_name ( msgtype ), inet_ntoa ( peer->sin_addr ),
  528. ntohs ( peer->sin_port ) );
  529. if ( server_id.s_addr != peer->sin_addr.s_addr )
  530. DBGC ( dhcp, " (%s)", inet_ntoa ( server_id ) );
  531. DBGC ( dhcp, "\n" );
  532. /* Filter out unacceptable responses */
  533. if ( peer->sin_port != htons ( PXE_PORT ) )
  534. return;
  535. if ( msgtype != DHCPACK )
  536. return;
  537. if ( server_id.s_addr /* Linux PXE server omits server ID */ &&
  538. ( server_id.s_addr != dhcp->proxy_server.s_addr ) )
  539. return;
  540. /* Register settings */
  541. dhcppkt->settings.name = PROXYDHCP_SETTINGS_NAME;
  542. if ( ( rc = register_settings ( &dhcppkt->settings, NULL ) ) != 0 ) {
  543. DBGC ( dhcp, "DHCP %p could not register settings: %s\n",
  544. dhcp, strerror ( rc ) );
  545. dhcp_finished ( dhcp, rc );
  546. return;
  547. }
  548. /* Terminate DHCP */
  549. dhcp_finished ( dhcp, 0 );
  550. }
  551. /**
  552. * Handle timer expiry during ProxyDHCP request
  553. *
  554. * @v dhcp DHCP session
  555. */
  556. static void dhcp_proxy_expired ( struct dhcp_session *dhcp ) {
  557. unsigned long elapsed = ( currticks() - dhcp->start );
  558. /* Give up waiting for ProxyDHCP before we reach the failure point */
  559. if ( elapsed > PROXYDHCP_MAX_TIMEOUT ) {
  560. dhcp_finished ( dhcp, 0 );
  561. return;
  562. }
  563. /* Retransmit current packet */
  564. dhcp_tx ( dhcp );
  565. }
  566. /** ProxyDHCP request state operations */
  567. static struct dhcp_session_state dhcp_state_proxy = {
  568. .name = "ProxyDHCP",
  569. .tx = dhcp_proxy_tx,
  570. .rx = dhcp_proxy_rx,
  571. .expired = dhcp_proxy_expired,
  572. .tx_msgtype = DHCPREQUEST,
  573. .apply_min_timeout = 0,
  574. };
  575. /**
  576. * Construct transmitted packet for PXE Boot Server Discovery
  577. *
  578. * @v dhcp DHCP session
  579. * @v dhcppkt DHCP packet
  580. * @v peer Destination address
  581. */
  582. static int dhcp_pxebs_tx ( struct dhcp_session *dhcp,
  583. struct dhcp_packet *dhcppkt,
  584. struct sockaddr_in *peer ) {
  585. struct dhcp_pxe_boot_menu_item menu_item = { 0, 0 };
  586. int rc;
  587. DBGC ( dhcp, "DHCP %p PXEBS REQUEST to %s:%d for type %d\n",
  588. dhcp, inet_ntoa ( *(dhcp->pxe_attempt) ), PXE_PORT,
  589. ntohs ( dhcp->pxe_type ) );
  590. /* Set boot menu item */
  591. menu_item.type = dhcp->pxe_type;
  592. if ( ( rc = dhcppkt_store ( dhcppkt, DHCP_PXE_BOOT_MENU_ITEM,
  593. &menu_item, sizeof ( menu_item ) ) ) != 0 )
  594. return rc;
  595. /* Set server address */
  596. peer->sin_addr = *(dhcp->pxe_attempt);
  597. peer->sin_port = htons ( PXE_PORT );
  598. return 0;
  599. }
  600. /**
  601. * Check to see if PXE Boot Server address is acceptable
  602. *
  603. * @v dhcp DHCP session
  604. * @v bs Boot Server address
  605. * @ret accept Boot Server is acceptable
  606. */
  607. static int dhcp_pxebs_accept ( struct dhcp_session *dhcp,
  608. struct in_addr bs ) {
  609. struct in_addr *accept;
  610. /* Accept if we have no acceptance filter */
  611. if ( ! dhcp->pxe_accept )
  612. return 1;
  613. /* Scan through acceptance list */
  614. for ( accept = dhcp->pxe_accept ; accept->s_addr ; accept++ ) {
  615. if ( accept->s_addr == bs.s_addr )
  616. return 1;
  617. }
  618. DBGC ( dhcp, "DHCP %p rejecting server %s\n",
  619. dhcp, inet_ntoa ( bs ) );
  620. return 0;
  621. }
  622. /**
  623. * Handle received packet during PXE Boot Server Discovery
  624. *
  625. * @v dhcp DHCP session
  626. * @v dhcppkt DHCP packet
  627. * @v peer DHCP server address
  628. * @v msgtype DHCP message type
  629. * @v server_id DHCP server ID
  630. */
  631. static void dhcp_pxebs_rx ( struct dhcp_session *dhcp,
  632. struct dhcp_packet *dhcppkt,
  633. struct sockaddr_in *peer, uint8_t msgtype,
  634. struct in_addr server_id ) {
  635. struct dhcp_pxe_boot_menu_item menu_item = { 0, 0 };
  636. int rc;
  637. DBGC ( dhcp, "DHCP %p %s from %s:%d", dhcp,
  638. dhcp_msgtype_name ( msgtype ), inet_ntoa ( peer->sin_addr ),
  639. ntohs ( peer->sin_port ) );
  640. if ( server_id.s_addr != peer->sin_addr.s_addr )
  641. DBGC ( dhcp, " (%s)", inet_ntoa ( server_id ) );
  642. /* Identify boot menu item */
  643. dhcppkt_fetch ( dhcppkt, DHCP_PXE_BOOT_MENU_ITEM,
  644. &menu_item, sizeof ( menu_item ) );
  645. if ( menu_item.type )
  646. DBGC ( dhcp, " for type %d", ntohs ( menu_item.type ) );
  647. DBGC ( dhcp, "\n" );
  648. /* Filter out unacceptable responses */
  649. if ( peer->sin_port != htons ( PXE_PORT ) )
  650. return;
  651. if ( msgtype != DHCPACK )
  652. return;
  653. if ( menu_item.type != dhcp->pxe_type )
  654. return;
  655. if ( ! dhcp_pxebs_accept ( dhcp, ( server_id.s_addr ?
  656. server_id : peer->sin_addr ) ) )
  657. return;
  658. /* Register settings */
  659. dhcppkt->settings.name = PXEBS_SETTINGS_NAME;
  660. if ( ( rc = register_settings ( &dhcppkt->settings, NULL ) ) != 0 ) {
  661. DBGC ( dhcp, "DHCP %p could not register settings: %s\n",
  662. dhcp, strerror ( rc ) );
  663. dhcp_finished ( dhcp, rc );
  664. return;
  665. }
  666. /* Terminate DHCP */
  667. dhcp_finished ( dhcp, 0 );
  668. }
  669. /**
  670. * Handle timer expiry during PXE Boot Server Discovery
  671. *
  672. * @v dhcp DHCP session
  673. */
  674. static void dhcp_pxebs_expired ( struct dhcp_session *dhcp ) {
  675. unsigned long elapsed = ( currticks() - dhcp->start );
  676. /* Give up waiting before we reach the failure point, and fail
  677. * over to the next server in the attempt list
  678. */
  679. if ( elapsed > PXEBS_MAX_TIMEOUT ) {
  680. dhcp->pxe_attempt++;
  681. if ( dhcp->pxe_attempt->s_addr ) {
  682. dhcp_set_state ( dhcp, &dhcp_state_pxebs );
  683. return;
  684. } else {
  685. dhcp_finished ( dhcp, -ETIMEDOUT );
  686. return;
  687. }
  688. }
  689. /* Retransmit current packet */
  690. dhcp_tx ( dhcp );
  691. }
  692. /** PXE Boot Server Discovery state operations */
  693. static struct dhcp_session_state dhcp_state_pxebs = {
  694. .name = "PXEBS",
  695. .tx = dhcp_pxebs_tx,
  696. .rx = dhcp_pxebs_rx,
  697. .expired = dhcp_pxebs_expired,
  698. .tx_msgtype = DHCPREQUEST,
  699. .apply_min_timeout = 1,
  700. };
  701. /****************************************************************************
  702. *
  703. * Packet construction
  704. *
  705. */
  706. /**
  707. * Create a DHCP packet
  708. *
  709. * @v dhcppkt DHCP packet structure to fill in
  710. * @v netdev Network device
  711. * @v msgtype DHCP message type
  712. * @v options Initial options to include (or NULL)
  713. * @v options_len Length of initial options
  714. * @v data Buffer for DHCP packet
  715. * @v max_len Size of DHCP packet buffer
  716. * @ret rc Return status code
  717. *
  718. * Creates a DHCP packet in the specified buffer, and initialise a
  719. * DHCP packet structure.
  720. */
  721. int dhcp_create_packet ( struct dhcp_packet *dhcppkt,
  722. struct net_device *netdev, uint8_t msgtype,
  723. const void *options, size_t options_len,
  724. void *data, size_t max_len ) {
  725. struct dhcphdr *dhcphdr = data;
  726. unsigned int hlen;
  727. int rc;
  728. /* Sanity check */
  729. if ( max_len < ( sizeof ( *dhcphdr ) + options_len ) )
  730. return -ENOSPC;
  731. /* Initialise DHCP packet content */
  732. memset ( dhcphdr, 0, max_len );
  733. dhcphdr->xid = dhcp_xid ( netdev );
  734. dhcphdr->magic = htonl ( DHCP_MAGIC_COOKIE );
  735. dhcphdr->htype = ntohs ( netdev->ll_protocol->ll_proto );
  736. dhcphdr->op = dhcp_op[msgtype];
  737. /* If hardware length exceeds the chaddr field length, don't
  738. * use the chaddr field. This is as per RFC4390.
  739. */
  740. hlen = netdev->ll_protocol->ll_addr_len;
  741. if ( hlen > sizeof ( dhcphdr->chaddr ) ) {
  742. hlen = 0;
  743. dhcphdr->flags = htons ( BOOTP_FL_BROADCAST );
  744. }
  745. dhcphdr->hlen = hlen;
  746. memcpy ( dhcphdr->chaddr, netdev->ll_addr, hlen );
  747. memcpy ( dhcphdr->options, options, options_len );
  748. /* Initialise DHCP packet structure */
  749. memset ( dhcppkt, 0, sizeof ( *dhcppkt ) );
  750. dhcppkt_init ( dhcppkt, data, max_len );
  751. /* Set DHCP_MESSAGE_TYPE option */
  752. if ( ( rc = dhcppkt_store ( dhcppkt, DHCP_MESSAGE_TYPE,
  753. &msgtype, sizeof ( msgtype ) ) ) != 0 )
  754. return rc;
  755. return 0;
  756. }
  757. /**
  758. * Create DHCP request packet
  759. *
  760. * @v dhcppkt DHCP packet structure to fill in
  761. * @v netdev Network device
  762. * @v msgtype DHCP message type
  763. * @v ciaddr Client IP address
  764. * @v data Buffer for DHCP packet
  765. * @v max_len Size of DHCP packet buffer
  766. * @ret rc Return status code
  767. *
  768. * Creates a DHCP request packet in the specified buffer, and
  769. * initialise a DHCP packet structure.
  770. */
  771. int dhcp_create_request ( struct dhcp_packet *dhcppkt,
  772. struct net_device *netdev, unsigned int msgtype,
  773. struct in_addr ciaddr, void *data, size_t max_len ) {
  774. struct device_description *desc = &netdev->dev->desc;
  775. struct dhcp_netdev_desc dhcp_desc;
  776. struct dhcp_client_id client_id;
  777. struct dhcp_client_uuid client_uuid;
  778. size_t dhcp_features_len;
  779. size_t ll_addr_len;
  780. ssize_t len;
  781. int rc;
  782. /* Create DHCP packet */
  783. if ( ( rc = dhcp_create_packet ( dhcppkt, netdev, msgtype,
  784. dhcp_request_options_data,
  785. sizeof ( dhcp_request_options_data ),
  786. data, max_len ) ) != 0 ) {
  787. DBG ( "DHCP could not create DHCP packet: %s\n",
  788. strerror ( rc ) );
  789. return rc;
  790. }
  791. /* Set client IP address */
  792. dhcppkt->dhcphdr->ciaddr = ciaddr;
  793. /* Add options to identify the feature list */
  794. dhcp_features_len = ( dhcp_features_end - dhcp_features );
  795. if ( ( rc = dhcppkt_store ( dhcppkt, DHCP_EB_ENCAP, dhcp_features,
  796. dhcp_features_len ) ) != 0 ) {
  797. DBG ( "DHCP could not set features list option: %s\n",
  798. strerror ( rc ) );
  799. return rc;
  800. }
  801. /* Add options to identify the network device */
  802. dhcp_desc.type = desc->bus_type;
  803. dhcp_desc.vendor = htons ( desc->vendor );
  804. dhcp_desc.device = htons ( desc->device );
  805. if ( ( rc = dhcppkt_store ( dhcppkt, DHCP_EB_BUS_ID, &dhcp_desc,
  806. sizeof ( dhcp_desc ) ) ) != 0 ) {
  807. DBG ( "DHCP could not set bus ID option: %s\n",
  808. strerror ( rc ) );
  809. return rc;
  810. }
  811. /* Add DHCP client identifier. Required for Infiniband, and
  812. * doesn't hurt other link layers.
  813. */
  814. client_id.ll_proto = ntohs ( netdev->ll_protocol->ll_proto );
  815. ll_addr_len = netdev->ll_protocol->ll_addr_len;
  816. assert ( ll_addr_len <= sizeof ( client_id.ll_addr ) );
  817. memcpy ( client_id.ll_addr, netdev->ll_addr, ll_addr_len );
  818. if ( ( rc = dhcppkt_store ( dhcppkt, DHCP_CLIENT_ID, &client_id,
  819. ( ll_addr_len + 1 ) ) ) != 0 ) {
  820. DBG ( "DHCP could not set client ID: %s\n",
  821. strerror ( rc ) );
  822. return rc;
  823. }
  824. /* Add client UUID, if we have one. Required for PXE. */
  825. client_uuid.type = DHCP_CLIENT_UUID_TYPE;
  826. if ( ( len = fetch_uuid_setting ( NULL, &uuid_setting,
  827. &client_uuid.uuid ) ) >= 0 ) {
  828. if ( ( rc = dhcppkt_store ( dhcppkt, DHCP_CLIENT_UUID,
  829. &client_uuid,
  830. sizeof ( client_uuid ) ) ) != 0 ) {
  831. DBG ( "DHCP could not set client UUID: %s\n",
  832. strerror ( rc ) );
  833. return rc;
  834. }
  835. }
  836. /* Add user class, if we have one. */
  837. if ( ( len = fetch_setting_len ( NULL, &user_class_setting ) ) >= 0 ) {
  838. char user_class[len];
  839. fetch_setting ( NULL, &user_class_setting, user_class,
  840. sizeof ( user_class ) );
  841. if ( ( rc = dhcppkt_store ( dhcppkt, DHCP_USER_CLASS_ID,
  842. &user_class,
  843. sizeof ( user_class ) ) ) != 0 ) {
  844. DBG ( "DHCP could not set user class: %s\n",
  845. strerror ( rc ) );
  846. return rc;
  847. }
  848. }
  849. return 0;
  850. }
  851. /****************************************************************************
  852. *
  853. * Data transfer interface
  854. *
  855. */
  856. /**
  857. * Transmit DHCP request
  858. *
  859. * @v dhcp DHCP session
  860. * @ret rc Return status code
  861. */
  862. static int dhcp_tx ( struct dhcp_session *dhcp ) {
  863. static struct sockaddr_in peer = {
  864. .sin_family = AF_INET,
  865. };
  866. struct xfer_metadata meta = {
  867. .netdev = dhcp->netdev,
  868. .src = ( struct sockaddr * ) &dhcp->local,
  869. .dest = ( struct sockaddr * ) &peer,
  870. };
  871. struct io_buffer *iobuf;
  872. uint8_t msgtype = dhcp->state->tx_msgtype;
  873. struct dhcp_packet dhcppkt;
  874. int rc;
  875. /* Start retry timer. Do this first so that failures to
  876. * transmit will be retried.
  877. */
  878. start_timer ( &dhcp->timer );
  879. /* Allocate buffer for packet */
  880. iobuf = xfer_alloc_iob ( &dhcp->xfer, DHCP_MIN_LEN );
  881. if ( ! iobuf )
  882. return -ENOMEM;
  883. /* Create basic DHCP packet in temporary buffer */
  884. if ( ( rc = dhcp_create_request ( &dhcppkt, dhcp->netdev, msgtype,
  885. dhcp->local.sin_addr, iobuf->data,
  886. iob_tailroom ( iobuf ) ) ) != 0 ) {
  887. DBGC ( dhcp, "DHCP %p could not construct DHCP request: %s\n",
  888. dhcp, strerror ( rc ) );
  889. goto done;
  890. }
  891. /* Fill in packet based on current state */
  892. if ( ( rc = dhcp->state->tx ( dhcp, &dhcppkt, &peer ) ) != 0 ) {
  893. DBGC ( dhcp, "DHCP %p could not fill DHCP request: %s\n",
  894. dhcp, strerror ( rc ) );
  895. goto done;
  896. }
  897. /* Transmit the packet */
  898. iob_put ( iobuf, dhcppkt.len );
  899. if ( ( rc = xfer_deliver_iob_meta ( &dhcp->xfer, iob_disown ( iobuf ),
  900. &meta ) ) != 0 ) {
  901. DBGC ( dhcp, "DHCP %p could not transmit UDP packet: %s\n",
  902. dhcp, strerror ( rc ) );
  903. goto done;
  904. }
  905. done:
  906. free_iob ( iobuf );
  907. return rc;
  908. }
  909. /**
  910. * Receive new data
  911. *
  912. * @v xfer Data transfer interface
  913. * @v iobuf I/O buffer
  914. * @v meta Transfer metadata
  915. * @ret rc Return status code
  916. */
  917. static int dhcp_deliver_iob ( struct xfer_interface *xfer,
  918. struct io_buffer *iobuf,
  919. struct xfer_metadata *meta ) {
  920. struct dhcp_session *dhcp =
  921. container_of ( xfer, struct dhcp_session, xfer );
  922. struct sockaddr_in *peer;
  923. size_t data_len;
  924. struct dhcp_packet *dhcppkt;
  925. struct dhcphdr *dhcphdr;
  926. uint8_t msgtype = 0;
  927. struct in_addr server_id = { 0 };
  928. int rc = 0;
  929. /* Sanity checks */
  930. if ( ! meta->src ) {
  931. DBGC ( dhcp, "DHCP %p received packet without source port\n",
  932. dhcp );
  933. rc = -EINVAL;
  934. goto err_no_src;
  935. }
  936. peer = ( struct sockaddr_in * ) meta->src;
  937. /* Create a DHCP packet containing the I/O buffer contents.
  938. * Whilst we could just use the original buffer in situ, that
  939. * would waste the unused space in the packet buffer, and also
  940. * waste a relatively scarce fully-aligned I/O buffer.
  941. */
  942. data_len = iob_len ( iobuf );
  943. dhcppkt = zalloc ( sizeof ( *dhcppkt ) + data_len );
  944. if ( ! dhcppkt ) {
  945. rc = -ENOMEM;
  946. goto err_alloc_dhcppkt;
  947. }
  948. dhcphdr = ( ( ( void * ) dhcppkt ) + sizeof ( *dhcppkt ) );
  949. memcpy ( dhcphdr, iobuf->data, data_len );
  950. dhcppkt_init ( dhcppkt, dhcphdr, data_len );
  951. /* Identify message type */
  952. dhcppkt_fetch ( dhcppkt, DHCP_MESSAGE_TYPE, &msgtype,
  953. sizeof ( msgtype ) );
  954. /* Identify server ID */
  955. dhcppkt_fetch ( dhcppkt, DHCP_SERVER_IDENTIFIER,
  956. &server_id, sizeof ( server_id ) );
  957. /* Check for matching transaction ID */
  958. if ( dhcphdr->xid != dhcp_xid ( dhcp->netdev ) ) {
  959. DBGC ( dhcp, "DHCP %p %s from %s:%d has bad transaction "
  960. "ID\n", dhcp, dhcp_msgtype_name ( msgtype ),
  961. inet_ntoa ( peer->sin_addr ),
  962. ntohs ( peer->sin_port ) );
  963. rc = -EINVAL;
  964. goto err_xid;
  965. };
  966. /* Handle packet based on current state */
  967. dhcp->state->rx ( dhcp, dhcppkt, peer, msgtype, server_id );
  968. err_xid:
  969. dhcppkt_put ( dhcppkt );
  970. err_alloc_dhcppkt:
  971. err_no_src:
  972. free_iob ( iobuf );
  973. return rc;
  974. }
  975. /** DHCP data transfer interface operations */
  976. static struct xfer_interface_operations dhcp_xfer_operations = {
  977. .close = ignore_xfer_close,
  978. .vredirect = xfer_vopen,
  979. .window = unlimited_xfer_window,
  980. .alloc_iob = default_xfer_alloc_iob,
  981. .deliver_iob = dhcp_deliver_iob,
  982. .deliver_raw = xfer_deliver_as_iob,
  983. };
  984. /**
  985. * Handle DHCP retry timer expiry
  986. *
  987. * @v timer DHCP retry timer
  988. * @v fail Failure indicator
  989. */
  990. static void dhcp_timer_expired ( struct retry_timer *timer, int fail ) {
  991. struct dhcp_session *dhcp =
  992. container_of ( timer, struct dhcp_session, timer );
  993. /* If we have failed, terminate DHCP */
  994. if ( fail ) {
  995. dhcp_finished ( dhcp, -ETIMEDOUT );
  996. return;
  997. }
  998. /* Handle timer expiry based on current state */
  999. dhcp->state->expired ( dhcp );
  1000. }
  1001. /****************************************************************************
  1002. *
  1003. * Job control interface
  1004. *
  1005. */
  1006. /**
  1007. * Handle kill() event received via job control interface
  1008. *
  1009. * @v job DHCP job control interface
  1010. */
  1011. static void dhcp_job_kill ( struct job_interface *job ) {
  1012. struct dhcp_session *dhcp =
  1013. container_of ( job, struct dhcp_session, job );
  1014. /* Terminate DHCP session */
  1015. dhcp_finished ( dhcp, -ECANCELED );
  1016. }
  1017. /** DHCP job control interface operations */
  1018. static struct job_interface_operations dhcp_job_operations = {
  1019. .done = ignore_job_done,
  1020. .kill = dhcp_job_kill,
  1021. .progress = ignore_job_progress,
  1022. };
  1023. /****************************************************************************
  1024. *
  1025. * Instantiators
  1026. *
  1027. */
  1028. /**
  1029. * DHCP peer address for socket opening
  1030. *
  1031. * This is a dummy address; the only useful portion is the socket
  1032. * family (so that we get a UDP connection). The DHCP client will set
  1033. * the IP address and source port explicitly on each transmission.
  1034. */
  1035. static struct sockaddr dhcp_peer = {
  1036. .sa_family = AF_INET,
  1037. };
  1038. /**
  1039. * Start DHCP state machine on a network device
  1040. *
  1041. * @v job Job control interface
  1042. * @v netdev Network device
  1043. * @ret rc Return status code
  1044. *
  1045. * Starts DHCP on the specified network device. If successful, the
  1046. * DHCPACK (and ProxyDHCPACK, if applicable) will be registered as
  1047. * option sources.
  1048. */
  1049. int start_dhcp ( struct job_interface *job, struct net_device *netdev ) {
  1050. struct dhcp_session *dhcp;
  1051. int rc;
  1052. /* Allocate and initialise structure */
  1053. dhcp = zalloc ( sizeof ( *dhcp ) );
  1054. if ( ! dhcp )
  1055. return -ENOMEM;
  1056. dhcp->refcnt.free = dhcp_free;
  1057. job_init ( &dhcp->job, &dhcp_job_operations, &dhcp->refcnt );
  1058. xfer_init ( &dhcp->xfer, &dhcp_xfer_operations, &dhcp->refcnt );
  1059. dhcp->netdev = netdev_get ( netdev );
  1060. dhcp->local.sin_family = AF_INET;
  1061. dhcp->local.sin_port = htons ( BOOTPC_PORT );
  1062. dhcp->timer.expired = dhcp_timer_expired;
  1063. /* Instantiate child objects and attach to our interfaces */
  1064. if ( ( rc = xfer_open_socket ( &dhcp->xfer, SOCK_DGRAM, &dhcp_peer,
  1065. ( struct sockaddr * ) &dhcp->local ) ) != 0 )
  1066. goto err;
  1067. /* Enter DHCPDISCOVER state */
  1068. dhcp_set_state ( dhcp, &dhcp_state_discover );
  1069. /* Attach parent interface, mortalise self, and return */
  1070. job_plug_plug ( &dhcp->job, job );
  1071. ref_put ( &dhcp->refcnt );
  1072. return 0;
  1073. err:
  1074. dhcp_finished ( dhcp, rc );
  1075. ref_put ( &dhcp->refcnt );
  1076. return rc;
  1077. }
  1078. /**
  1079. * Retrieve list of PXE boot servers for a given server type
  1080. *
  1081. * @v dhcp DHCP session
  1082. * @v raw DHCP PXE boot server list
  1083. * @v raw_len Length of DHCP PXE boot server list
  1084. * @v ip IP address list to fill in
  1085. *
  1086. * The caller must ensure that the IP address list has sufficient
  1087. * space.
  1088. */
  1089. static void pxebs_list ( struct dhcp_session *dhcp, void *raw,
  1090. size_t raw_len, struct in_addr *ip ) {
  1091. struct dhcp_pxe_boot_server *server = raw;
  1092. size_t server_len;
  1093. unsigned int i;
  1094. while ( raw_len ) {
  1095. if ( raw_len < sizeof ( *server ) ) {
  1096. DBGC ( dhcp, "DHCP %p malformed PXE server list\n",
  1097. dhcp );
  1098. break;
  1099. }
  1100. server_len = offsetof ( typeof ( *server ),
  1101. ip[ server->num_ip ] );
  1102. if ( raw_len < server_len ) {
  1103. DBGC ( dhcp, "DHCP %p malformed PXE server list\n",
  1104. dhcp );
  1105. break;
  1106. }
  1107. if ( server->type == dhcp->pxe_type ) {
  1108. for ( i = 0 ; i < server->num_ip ; i++ )
  1109. *(ip++) = server->ip[i];
  1110. }
  1111. server = ( ( ( void * ) server ) + server_len );
  1112. raw_len -= server_len;
  1113. }
  1114. }
  1115. /**
  1116. * Start PXE Boot Server Discovery on a network device
  1117. *
  1118. * @v job Job control interface
  1119. * @v netdev Network device
  1120. * @v pxe_type PXE server type
  1121. * @ret rc Return status code
  1122. *
  1123. * Starts PXE Boot Server Discovery on the specified network device.
  1124. * If successful, the Boot Server ACK will be registered as an option
  1125. * source.
  1126. */
  1127. int start_pxebs ( struct job_interface *job, struct net_device *netdev,
  1128. unsigned int pxe_type ) {
  1129. struct setting pxe_discovery_control_setting =
  1130. { .tag = DHCP_PXE_DISCOVERY_CONTROL };
  1131. struct setting pxe_boot_servers_setting =
  1132. { .tag = DHCP_PXE_BOOT_SERVERS };
  1133. struct setting pxe_boot_server_mcast_setting =
  1134. { .tag = DHCP_PXE_BOOT_SERVER_MCAST };
  1135. ssize_t pxebs_list_len;
  1136. struct dhcp_session *dhcp;
  1137. struct in_addr *ip;
  1138. unsigned int pxe_discovery_control;
  1139. int rc;
  1140. /* Get upper bound for PXE boot server IP address list */
  1141. pxebs_list_len = fetch_setting_len ( NULL, &pxe_boot_servers_setting );
  1142. if ( pxebs_list_len < 0 )
  1143. pxebs_list_len = 0;
  1144. /* Allocate and initialise structure */
  1145. dhcp = zalloc ( sizeof ( *dhcp ) + sizeof ( *ip ) /* mcast */ +
  1146. sizeof ( *ip ) /* bcast */ + pxebs_list_len +
  1147. sizeof ( *ip ) /* terminator */ );
  1148. if ( ! dhcp )
  1149. return -ENOMEM;
  1150. dhcp->refcnt.free = dhcp_free;
  1151. job_init ( &dhcp->job, &dhcp_job_operations, &dhcp->refcnt );
  1152. xfer_init ( &dhcp->xfer, &dhcp_xfer_operations, &dhcp->refcnt );
  1153. dhcp->netdev = netdev_get ( netdev );
  1154. dhcp->local.sin_family = AF_INET;
  1155. fetch_ipv4_setting ( netdev_settings ( netdev ), &ip_setting,
  1156. &dhcp->local.sin_addr );
  1157. dhcp->local.sin_port = htons ( BOOTPC_PORT );
  1158. dhcp->pxe_type = htons ( pxe_type );
  1159. dhcp->timer.expired = dhcp_timer_expired;
  1160. /* Construct PXE boot server IP address lists */
  1161. pxe_discovery_control =
  1162. fetch_uintz_setting ( NULL, &pxe_discovery_control_setting );
  1163. ip = ( ( ( void * ) dhcp ) + sizeof ( *dhcp ) );
  1164. dhcp->pxe_attempt = ip;
  1165. if ( ! ( pxe_discovery_control & PXEBS_NO_MULTICAST ) ) {
  1166. fetch_ipv4_setting ( NULL, &pxe_boot_server_mcast_setting, ip);
  1167. if ( ip->s_addr )
  1168. ip++;
  1169. }
  1170. if ( ! ( pxe_discovery_control & PXEBS_NO_BROADCAST ) )
  1171. (ip++)->s_addr = INADDR_BROADCAST;
  1172. if ( pxe_discovery_control & PXEBS_NO_UNKNOWN_SERVERS )
  1173. dhcp->pxe_accept = ip;
  1174. if ( pxebs_list_len ) {
  1175. uint8_t buf[pxebs_list_len];
  1176. fetch_setting ( NULL, &pxe_boot_servers_setting,
  1177. buf, sizeof ( buf ) );
  1178. pxebs_list ( dhcp, buf, sizeof ( buf ), ip );
  1179. }
  1180. if ( ! dhcp->pxe_attempt->s_addr ) {
  1181. DBGC ( dhcp, "DHCP %p has no PXE boot servers for type %04x\n",
  1182. dhcp, pxe_type );
  1183. rc = -EINVAL;
  1184. goto err;
  1185. }
  1186. /* Dump out PXE server lists */
  1187. DBGC ( dhcp, "DHCP %p attempting", dhcp );
  1188. for ( ip = dhcp->pxe_attempt ; ip->s_addr ; ip++ )
  1189. DBGC ( dhcp, " %s", inet_ntoa ( *ip ) );
  1190. DBGC ( dhcp, "\n" );
  1191. if ( dhcp->pxe_accept ) {
  1192. DBGC ( dhcp, "DHCP %p accepting", dhcp );
  1193. for ( ip = dhcp->pxe_accept ; ip->s_addr ; ip++ )
  1194. DBGC ( dhcp, " %s", inet_ntoa ( *ip ) );
  1195. DBGC ( dhcp, "\n" );
  1196. }
  1197. /* Instantiate child objects and attach to our interfaces */
  1198. if ( ( rc = xfer_open_socket ( &dhcp->xfer, SOCK_DGRAM, &dhcp_peer,
  1199. ( struct sockaddr * ) &dhcp->local ) ) != 0 )
  1200. goto err;
  1201. /* Enter PXEBS state */
  1202. dhcp_set_state ( dhcp, &dhcp_state_pxebs );
  1203. /* Attach parent interface, mortalise self, and return */
  1204. job_plug_plug ( &dhcp->job, job );
  1205. ref_put ( &dhcp->refcnt );
  1206. return 0;
  1207. err:
  1208. dhcp_finished ( dhcp, rc );
  1209. ref_put ( &dhcp->refcnt );
  1210. return rc;
  1211. }