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

dhcp.c 33KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237
  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_PARAMETER_REQUEST_LIST,
  76. DHCP_OPTION ( DHCP_SUBNET_MASK, DHCP_ROUTERS, DHCP_DNS_SERVERS,
  77. DHCP_LOG_SERVERS, DHCP_HOST_NAME, DHCP_DOMAIN_NAME,
  78. DHCP_ROOT_PATH, DHCP_VENDOR_ENCAP, DHCP_VENDOR_CLASS_ID,
  79. DHCP_TFTP_SERVER_NAME, DHCP_BOOTFILE_NAME,
  80. DHCP_EB_ENCAP, DHCP_ISCSI_INITIATOR_IQN ),
  81. DHCP_END
  82. };
  83. /** DHCP feature codes */
  84. static uint8_t dhcp_features[0] __table_start ( uint8_t, dhcp_features );
  85. static uint8_t dhcp_features_end[0] __table_end ( uint8_t, dhcp_features );
  86. /** Version number feature */
  87. FEATURE_VERSION ( VERSION_MAJOR, VERSION_MINOR, VERSION_PATCH );
  88. /** DHCP server address setting */
  89. struct setting dhcp_server_setting __setting = {
  90. .name = "dhcp-server",
  91. .description = "DHCP server address",
  92. .tag = DHCP_SERVER_IDENTIFIER,
  93. .type = &setting_type_ipv4,
  94. };
  95. /** DHCP user class setting */
  96. struct setting user_class_setting __setting = {
  97. .name = "user-class",
  98. .description = "User class identifier",
  99. .tag = DHCP_USER_CLASS_ID,
  100. .type = &setting_type_string,
  101. };
  102. /**
  103. * Name a DHCP packet type
  104. *
  105. * @v msgtype DHCP message type
  106. * @ret string DHCP mesasge type name
  107. */
  108. static inline const char * dhcp_msgtype_name ( unsigned int msgtype ) {
  109. switch ( msgtype ) {
  110. case DHCPNONE: return "BOOTP"; /* Non-DHCP packet */
  111. case DHCPDISCOVER: return "DHCPDISCOVER";
  112. case DHCPOFFER: return "DHCPOFFER";
  113. case DHCPREQUEST: return "DHCPREQUEST";
  114. case DHCPDECLINE: return "DHCPDECLINE";
  115. case DHCPACK: return "DHCPACK";
  116. case DHCPNAK: return "DHCPNAK";
  117. case DHCPRELEASE: return "DHCPRELEASE";
  118. case DHCPINFORM: return "DHCPINFORM";
  119. default: return "DHCP<invalid>";
  120. }
  121. }
  122. /**
  123. * Calculate DHCP transaction ID for a network device
  124. *
  125. * @v netdev Network device
  126. * @ret xid DHCP XID
  127. *
  128. * Extract the least significant bits of the hardware address for use
  129. * as the transaction ID.
  130. */
  131. static uint32_t dhcp_xid ( struct net_device *netdev ) {
  132. uint32_t xid;
  133. memcpy ( &xid, ( netdev->ll_addr + netdev->ll_protocol->ll_addr_len
  134. - sizeof ( xid ) ), sizeof ( xid ) );
  135. return xid;
  136. }
  137. /****************************************************************************
  138. *
  139. * DHCP session
  140. *
  141. */
  142. struct dhcp_session;
  143. /** DHCP session state operations */
  144. struct dhcp_session_state {
  145. /** State name */
  146. const char *name;
  147. /**
  148. * Construct transmitted packet
  149. *
  150. * @v dhcp DHCP session
  151. * @v dhcppkt DHCP packet
  152. * @v peer Destination address
  153. */
  154. int ( * tx ) ( struct dhcp_session *dhcp,
  155. struct dhcp_packet *dhcppkt,
  156. struct sockaddr_in *peer );
  157. /** Handle received packet
  158. *
  159. * @v dhcp DHCP session
  160. * @v dhcppkt DHCP packet
  161. * @v peer DHCP server address
  162. * @v msgtype DHCP message type
  163. */
  164. void ( * rx ) ( struct dhcp_session *dhcp,
  165. struct dhcp_packet *dhcppkt,
  166. struct sockaddr_in *peer,
  167. uint8_t msgtype );
  168. /** Handle timer expiry
  169. *
  170. * @v dhcp DHCP session
  171. */
  172. void ( * expired ) ( struct dhcp_session *dhcp );
  173. /** Transmitted message type */
  174. uint8_t tx_msgtype;
  175. /** Apply minimum timeout */
  176. uint8_t apply_min_timeout;
  177. };
  178. static struct dhcp_session_state dhcp_state_discover;
  179. static struct dhcp_session_state dhcp_state_request;
  180. static struct dhcp_session_state dhcp_state_proxy;
  181. static struct dhcp_session_state dhcp_state_pxebs;
  182. /** A DHCP session */
  183. struct dhcp_session {
  184. /** Reference counter */
  185. struct refcnt refcnt;
  186. /** Job control interface */
  187. struct job_interface job;
  188. /** Data transfer interface */
  189. struct xfer_interface xfer;
  190. /** Network device being configured */
  191. struct net_device *netdev;
  192. /** Local socket address */
  193. struct sockaddr_in local;
  194. /** State of the session */
  195. struct dhcp_session_state *state;
  196. /** Offered IP address */
  197. struct in_addr offer;
  198. /** DHCP server */
  199. struct in_addr server;
  200. /** DHCP offer priority */
  201. int priority;
  202. /** ProxyDHCP protocol extensions should be ignored */
  203. int no_pxedhcp;
  204. /** ProxyDHCP server */
  205. struct in_addr proxy_server;
  206. /** ProxyDHCP server priority */
  207. int proxy_priority;
  208. /** PXE Boot Server */
  209. struct in_addr pxe_server;
  210. /** PXE Boot Server type */
  211. uint16_t pxe_type;
  212. /** Retransmission timer */
  213. struct retry_timer timer;
  214. /** Start time of the current state (in ticks) */
  215. unsigned long start;
  216. };
  217. /**
  218. * Free DHCP session
  219. *
  220. * @v refcnt Reference counter
  221. */
  222. static void dhcp_free ( struct refcnt *refcnt ) {
  223. struct dhcp_session *dhcp =
  224. container_of ( refcnt, struct dhcp_session, refcnt );
  225. netdev_put ( dhcp->netdev );
  226. free ( dhcp );
  227. }
  228. /**
  229. * Mark DHCP session as complete
  230. *
  231. * @v dhcp DHCP session
  232. * @v rc Return status code
  233. */
  234. static void dhcp_finished ( struct dhcp_session *dhcp, int rc ) {
  235. /* Block futher incoming messages */
  236. job_nullify ( &dhcp->job );
  237. xfer_nullify ( &dhcp->xfer );
  238. /* Stop retry timer */
  239. stop_timer ( &dhcp->timer );
  240. /* Free resources and close interfaces */
  241. xfer_close ( &dhcp->xfer, rc );
  242. job_done ( &dhcp->job, rc );
  243. }
  244. /**
  245. * Transition to new DHCP session state
  246. *
  247. * @v dhcp DHCP session
  248. * @v state New session state
  249. */
  250. static void dhcp_set_state ( struct dhcp_session *dhcp,
  251. struct dhcp_session_state *state ) {
  252. DBGC ( dhcp, "DHCP %p entering %s state\n", dhcp, state->name );
  253. dhcp->state = state;
  254. dhcp->start = currticks();
  255. stop_timer ( &dhcp->timer );
  256. dhcp->timer.min_timeout =
  257. ( state->apply_min_timeout ? DHCP_MIN_TIMEOUT : 0 );
  258. dhcp->timer.max_timeout = DHCP_MAX_TIMEOUT;
  259. start_timer_nodelay ( &dhcp->timer );
  260. }
  261. /****************************************************************************
  262. *
  263. * DHCP state machine
  264. *
  265. */
  266. /**
  267. * Construct transmitted packet for DHCP discovery
  268. *
  269. * @v dhcp DHCP session
  270. * @v dhcppkt DHCP packet
  271. * @v peer Destination address
  272. */
  273. static int dhcp_discovery_tx ( struct dhcp_session *dhcp,
  274. struct dhcp_packet *dhcppkt __unused,
  275. struct sockaddr_in *peer ) {
  276. DBGC ( dhcp, "DHCP %p DHCPDISCOVER\n", dhcp );
  277. /* Set server address */
  278. peer->sin_addr.s_addr = INADDR_BROADCAST;
  279. peer->sin_port = htons ( BOOTPS_PORT );
  280. return 0;
  281. }
  282. /**
  283. * Handle received packet during DHCP discovery
  284. *
  285. * @v dhcp DHCP session
  286. * @v dhcppkt DHCP packet
  287. * @v peer DHCP server address
  288. * @v msgtype DHCP message type
  289. */
  290. static void dhcp_discovery_rx ( struct dhcp_session *dhcp,
  291. struct dhcp_packet *dhcppkt,
  292. struct sockaddr_in *peer, uint8_t msgtype ) {
  293. struct in_addr server_id = { 0 };
  294. struct in_addr ip;
  295. char vci[9]; /* "PXEClient" */
  296. int vci_len;
  297. int has_pxeclient;
  298. int8_t priority = 0;
  299. uint8_t no_pxedhcp = 0;
  300. unsigned long elapsed;
  301. DBGC ( dhcp, "DHCP %p %s from %s:%d", dhcp,
  302. dhcp_msgtype_name ( msgtype ), inet_ntoa ( peer->sin_addr ),
  303. ntohs ( peer->sin_port ) );
  304. /* Identify server ID */
  305. dhcppkt_fetch ( dhcppkt, DHCP_SERVER_IDENTIFIER,
  306. &server_id, sizeof ( server_id ) );
  307. if ( server_id.s_addr != peer->sin_addr.s_addr )
  308. DBGC ( dhcp, " (%s)", inet_ntoa ( server_id ) );
  309. /* Identify offered IP address */
  310. ip = dhcppkt->dhcphdr->yiaddr;
  311. if ( ip.s_addr )
  312. DBGC ( dhcp, " for %s", inet_ntoa ( ip ) );
  313. /* Identify "PXEClient" vendor class */
  314. vci_len = dhcppkt_fetch ( dhcppkt, DHCP_VENDOR_CLASS_ID,
  315. vci, sizeof ( vci ) );
  316. has_pxeclient = ( ( vci_len >= ( int ) sizeof ( vci ) ) &&
  317. ( strncmp ( "PXEClient", vci, sizeof (vci) ) == 0 ));
  318. if ( has_pxeclient )
  319. DBGC ( dhcp, " pxe" );
  320. /* Identify priority */
  321. dhcppkt_fetch ( dhcppkt, DHCP_EB_PRIORITY, &priority,
  322. sizeof ( priority ) );
  323. if ( priority )
  324. DBGC ( dhcp, " pri %d", priority );
  325. /* Identify ignore-PXE flag */
  326. dhcppkt_fetch ( dhcppkt, DHCP_EB_NO_PXEDHCP, &no_pxedhcp,
  327. sizeof ( no_pxedhcp ) );
  328. if ( no_pxedhcp )
  329. DBGC ( dhcp, " nopxe" );
  330. DBGC ( dhcp, "\n" );
  331. /* Select as DHCP offer, if applicable */
  332. if ( ip.s_addr && ( peer->sin_port == htons ( BOOTPS_PORT ) ) &&
  333. ( ( msgtype == DHCPOFFER ) || ( ! msgtype /* BOOTP */ ) ) &&
  334. ( priority >= dhcp->priority ) ) {
  335. dhcp->offer = ip;
  336. dhcp->server = server_id;
  337. dhcp->priority = priority;
  338. dhcp->no_pxedhcp = no_pxedhcp;
  339. }
  340. /* Select as ProxyDHCP offer, if applicable */
  341. if ( has_pxeclient && ( msgtype == DHCPOFFER ) &&
  342. ( priority >= dhcp->proxy_priority ) ) {
  343. dhcp->proxy_server = server_id;
  344. dhcp->proxy_priority = priority;
  345. }
  346. /* We can exit the discovery state when we have a valid
  347. * DHCPOFFER, and either:
  348. *
  349. * o The DHCPOFFER instructs us to ignore ProxyDHCPOFFERs, or
  350. * o We have a valid ProxyDHCPOFFER, or
  351. * o We have allowed sufficient time for ProxyDHCPOFFERs.
  352. */
  353. /* If we don't yet have a DHCPOFFER, do nothing */
  354. if ( ! dhcp->offer.s_addr )
  355. return;
  356. /* If we can't yet transition to DHCPREQUEST, do nothing */
  357. elapsed = ( currticks() - dhcp->start );
  358. if ( ! ( dhcp->no_pxedhcp || dhcp->proxy_server.s_addr ||
  359. ( elapsed > PROXYDHCP_MAX_TIMEOUT ) ) )
  360. return;
  361. /* Transition to DHCPREQUEST */
  362. dhcp_set_state ( dhcp, &dhcp_state_request );
  363. }
  364. /**
  365. * Handle timer expiry during DHCP discovery
  366. *
  367. * @v dhcp DHCP session
  368. */
  369. static void dhcp_discovery_expired ( struct dhcp_session *dhcp ) {
  370. unsigned long elapsed = ( currticks() - dhcp->start );
  371. /* Give up waiting for ProxyDHCP before we reach the failure point */
  372. if ( dhcp->offer.s_addr && ( elapsed > PROXYDHCP_MAX_TIMEOUT ) ) {
  373. dhcp_set_state ( dhcp, &dhcp_state_request );
  374. return;
  375. }
  376. /* Otherwise, retransmit current packet */
  377. dhcp_tx ( dhcp );
  378. }
  379. /** DHCP discovery state operations */
  380. static struct dhcp_session_state dhcp_state_discover = {
  381. .name = "discovery",
  382. .tx = dhcp_discovery_tx,
  383. .rx = dhcp_discovery_rx,
  384. .expired = dhcp_discovery_expired,
  385. .tx_msgtype = DHCPDISCOVER,
  386. .apply_min_timeout = 1,
  387. };
  388. /**
  389. * Construct transmitted packet for DHCP request
  390. *
  391. * @v dhcp DHCP session
  392. * @v dhcppkt DHCP packet
  393. * @v peer Destination address
  394. */
  395. static int dhcp_request_tx ( struct dhcp_session *dhcp,
  396. struct dhcp_packet *dhcppkt,
  397. struct sockaddr_in *peer ) {
  398. int rc;
  399. DBGC ( dhcp, "DHCP %p DHCPREQUEST to %s:%d",
  400. dhcp, inet_ntoa ( dhcp->server ), BOOTPS_PORT );
  401. DBGC ( dhcp, " for %s\n", inet_ntoa ( dhcp->offer ) );
  402. /* Set server ID */
  403. if ( ( rc = dhcppkt_store ( dhcppkt, DHCP_SERVER_IDENTIFIER,
  404. &dhcp->server,
  405. sizeof ( dhcp->server ) ) ) != 0 )
  406. return rc;
  407. /* Set requested IP address */
  408. if ( ( rc = dhcppkt_store ( dhcppkt, DHCP_REQUESTED_ADDRESS,
  409. &dhcp->offer,
  410. sizeof ( dhcp->offer ) ) ) != 0 )
  411. return rc;
  412. /* Set server address */
  413. peer->sin_addr.s_addr = INADDR_BROADCAST;
  414. peer->sin_port = htons ( BOOTPS_PORT );
  415. return 0;
  416. }
  417. /**
  418. * Handle received packet during DHCP request
  419. *
  420. * @v dhcp DHCP session
  421. * @v dhcppkt DHCP packet
  422. * @v peer DHCP server address
  423. * @v msgtype DHCP message type
  424. */
  425. static void dhcp_request_rx ( struct dhcp_session *dhcp,
  426. struct dhcp_packet *dhcppkt,
  427. struct sockaddr_in *peer, uint8_t msgtype ) {
  428. struct in_addr server_id = { 0 };
  429. struct in_addr ip;
  430. struct settings *parent;
  431. int rc;
  432. DBGC ( dhcp, "DHCP %p %s from %s:%d", dhcp,
  433. dhcp_msgtype_name ( msgtype ), inet_ntoa ( peer->sin_addr ),
  434. ntohs ( peer->sin_port ) );
  435. /* Identify server ID */
  436. dhcppkt_fetch ( dhcppkt, DHCP_SERVER_IDENTIFIER,
  437. &server_id, sizeof ( server_id ) );
  438. if ( server_id.s_addr != peer->sin_addr.s_addr )
  439. DBGC ( dhcp, " (%s)", inet_ntoa ( server_id ) );
  440. /* Identify leased IP address */
  441. ip = dhcppkt->dhcphdr->yiaddr;
  442. if ( ip.s_addr )
  443. DBGC ( dhcp, " for %s", inet_ntoa ( ip ) );
  444. DBGC ( dhcp, "\n" );
  445. /* Filter out unacceptable responses */
  446. if ( peer->sin_port != htons ( BOOTPS_PORT ) )
  447. return;
  448. if ( msgtype /* BOOTP */ && ( msgtype != DHCPACK ) )
  449. return;
  450. if ( server_id.s_addr != dhcp->server.s_addr )
  451. return;
  452. /* Record assigned address */
  453. dhcp->local.sin_addr = ip;
  454. /* Register settings */
  455. parent = netdev_settings ( dhcp->netdev );
  456. if ( ( rc = register_settings ( &dhcppkt->settings, parent ) ) != 0 ){
  457. DBGC ( dhcp, "DHCP %p could not register settings: %s\n",
  458. dhcp, strerror ( rc ) );
  459. dhcp_finished ( dhcp, rc );
  460. return;
  461. }
  462. /* Start ProxyDHCPREQUEST if applicable */
  463. if ( dhcp->proxy_server.s_addr && ( ! dhcp->no_pxedhcp ) ) {
  464. dhcp_set_state ( dhcp, &dhcp_state_proxy );
  465. return;
  466. }
  467. /* Terminate DHCP */
  468. dhcp_finished ( dhcp, 0 );
  469. }
  470. /**
  471. * Handle timer expiry during DHCP discovery
  472. *
  473. * @v dhcp DHCP session
  474. */
  475. static void dhcp_request_expired ( struct dhcp_session *dhcp ) {
  476. /* Retransmit current packet */
  477. dhcp_tx ( dhcp );
  478. }
  479. /** DHCP request state operations */
  480. static struct dhcp_session_state dhcp_state_request = {
  481. .name = "request",
  482. .tx = dhcp_request_tx,
  483. .rx = dhcp_request_rx,
  484. .expired = dhcp_request_expired,
  485. .tx_msgtype = DHCPREQUEST,
  486. .apply_min_timeout = 0,
  487. };
  488. /**
  489. * Construct transmitted packet for ProxyDHCP request
  490. *
  491. * @v dhcp DHCP session
  492. * @v dhcppkt DHCP packet
  493. * @v peer Destination address
  494. */
  495. static int dhcp_proxy_tx ( struct dhcp_session *dhcp,
  496. struct dhcp_packet *dhcppkt,
  497. struct sockaddr_in *peer ) {
  498. int rc;
  499. DBGC ( dhcp, "DHCP %p ProxyDHCP REQUEST to %s:%d\n",
  500. dhcp, inet_ntoa ( dhcp->proxy_server ), PXE_PORT );
  501. /* Set server ID */
  502. if ( ( rc = dhcppkt_store ( dhcppkt, DHCP_SERVER_IDENTIFIER,
  503. &dhcp->proxy_server,
  504. sizeof ( dhcp->proxy_server ) ) ) != 0 )
  505. return rc;
  506. /* Set server address */
  507. peer->sin_addr = dhcp->proxy_server;
  508. peer->sin_port = htons ( PXE_PORT );
  509. return 0;
  510. }
  511. /**
  512. * Handle received packet during ProxyDHCP request
  513. *
  514. * @v dhcp DHCP session
  515. * @v dhcppkt DHCP packet
  516. * @v peer DHCP server address
  517. * @v msgtype DHCP message type
  518. */
  519. static void dhcp_proxy_rx ( struct dhcp_session *dhcp,
  520. struct dhcp_packet *dhcppkt,
  521. struct sockaddr_in *peer, uint8_t msgtype ) {
  522. struct in_addr server_id = { 0 };
  523. int rc;
  524. DBGC ( dhcp, "DHCP %p %s from %s:%d", dhcp,
  525. dhcp_msgtype_name ( msgtype ), inet_ntoa ( peer->sin_addr ),
  526. ntohs ( peer->sin_port ) );
  527. /* Identify server ID */
  528. dhcppkt_fetch ( dhcppkt, DHCP_SERVER_IDENTIFIER,
  529. &server_id, sizeof ( server_id ) );
  530. if ( server_id.s_addr != peer->sin_addr.s_addr )
  531. DBGC ( dhcp, " (%s)", inet_ntoa ( server_id ) );
  532. DBGC ( dhcp, "\n" );
  533. /* Filter out unacceptable responses */
  534. if ( peer->sin_port != htons ( PXE_PORT ) )
  535. return;
  536. if ( msgtype != DHCPACK )
  537. return;
  538. if ( server_id.s_addr /* Linux PXE server omits server ID */ &&
  539. ( server_id.s_addr != dhcp->proxy_server.s_addr ) )
  540. return;
  541. /* Register settings */
  542. dhcppkt->settings.name = PROXYDHCP_SETTINGS_NAME;
  543. if ( ( rc = register_settings ( &dhcppkt->settings, NULL ) ) != 0 ) {
  544. DBGC ( dhcp, "DHCP %p could not register settings: %s\n",
  545. dhcp, strerror ( rc ) );
  546. dhcp_finished ( dhcp, rc );
  547. return;
  548. }
  549. /* Terminate DHCP */
  550. dhcp_finished ( dhcp, 0 );
  551. }
  552. /**
  553. * Handle timer expiry during ProxyDHCP request
  554. *
  555. * @v dhcp DHCP session
  556. */
  557. static void dhcp_proxy_expired ( struct dhcp_session *dhcp ) {
  558. unsigned long elapsed = ( currticks() - dhcp->start );
  559. /* Give up waiting for ProxyDHCP before we reach the failure point */
  560. if ( elapsed > PROXYDHCP_MAX_TIMEOUT ) {
  561. dhcp_finished ( dhcp, 0 );
  562. return;
  563. }
  564. /* Retransmit current packet */
  565. dhcp_tx ( dhcp );
  566. }
  567. /** ProxyDHCP request state operations */
  568. static struct dhcp_session_state dhcp_state_proxy = {
  569. .name = "ProxyDHCP",
  570. .tx = dhcp_proxy_tx,
  571. .rx = dhcp_proxy_rx,
  572. .expired = dhcp_proxy_expired,
  573. .tx_msgtype = DHCPREQUEST,
  574. .apply_min_timeout = 0,
  575. };
  576. /**
  577. * Construct transmitted packet for PXE Boot Server Discovery
  578. *
  579. * @v dhcp DHCP session
  580. * @v dhcppkt DHCP packet
  581. * @v peer Destination address
  582. */
  583. static int dhcp_pxebs_tx ( struct dhcp_session *dhcp,
  584. struct dhcp_packet *dhcppkt,
  585. struct sockaddr_in *peer ) {
  586. struct dhcp_pxe_boot_menu_item menu_item = { 0, 0 };
  587. int rc;
  588. DBGC ( dhcp, "DHCP %p PXEBS REQUEST to %s:%d for type %d\n",
  589. dhcp, inet_ntoa ( dhcp->pxe_server ), PXE_PORT,
  590. ntohs ( dhcp->pxe_type ) );
  591. /* Set boot menu item */
  592. menu_item.type = dhcp->pxe_type;
  593. if ( ( rc = dhcppkt_store ( dhcppkt, DHCP_PXE_BOOT_MENU_ITEM,
  594. &menu_item, sizeof ( menu_item ) ) ) != 0 )
  595. return rc;
  596. /* Set server address */
  597. peer->sin_addr = dhcp->pxe_server;
  598. peer->sin_port = htons ( PXE_PORT );
  599. return 0;
  600. }
  601. /**
  602. * Handle received packet during PXE Boot Server Discovery
  603. *
  604. * @v dhcp DHCP session
  605. * @v dhcppkt DHCP packet
  606. * @v peer DHCP server address
  607. * @v msgtype DHCP message type
  608. */
  609. static void dhcp_pxebs_rx ( struct dhcp_session *dhcp,
  610. struct dhcp_packet *dhcppkt,
  611. struct sockaddr_in *peer, uint8_t msgtype ) {
  612. struct dhcp_pxe_boot_menu_item menu_item = { 0, 0 };
  613. int rc;
  614. DBGC ( dhcp, "DHCP %p %s from %s:%d", dhcp,
  615. dhcp_msgtype_name ( msgtype ), inet_ntoa ( peer->sin_addr ),
  616. ntohs ( peer->sin_port ) );
  617. /* Identify boot menu item */
  618. dhcppkt_fetch ( dhcppkt, DHCP_PXE_BOOT_MENU_ITEM,
  619. &menu_item, sizeof ( menu_item ) );
  620. if ( menu_item.type )
  621. DBGC ( dhcp, " for type %d", ntohs ( menu_item.type ) );
  622. DBGC ( dhcp, "\n" );
  623. /* Filter out unacceptable responses */
  624. if ( peer->sin_port != htons ( PXE_PORT ) )
  625. return;
  626. if ( msgtype != DHCPACK )
  627. return;
  628. if ( menu_item.type != dhcp->pxe_type )
  629. return;
  630. /* Register settings */
  631. dhcppkt->settings.name = PXEBS_SETTINGS_NAME;
  632. if ( ( rc = register_settings ( &dhcppkt->settings, NULL ) ) != 0 ) {
  633. DBGC ( dhcp, "DHCP %p could not register settings: %s\n",
  634. dhcp, strerror ( rc ) );
  635. dhcp_finished ( dhcp, rc );
  636. return;
  637. }
  638. /* Terminate DHCP */
  639. dhcp_finished ( dhcp, 0 );
  640. }
  641. /**
  642. * Handle timer expiry during PXE Boot Server Discovery
  643. *
  644. * @v dhcp DHCP session
  645. */
  646. static void dhcp_pxebs_expired ( struct dhcp_session *dhcp ) {
  647. /* Retransmit current packet */
  648. dhcp_tx ( dhcp );
  649. }
  650. /** PXE Boot Server Discovery state operations */
  651. static struct dhcp_session_state dhcp_state_pxebs = {
  652. .name = "PXEBS",
  653. .tx = dhcp_pxebs_tx,
  654. .rx = dhcp_pxebs_rx,
  655. .expired = dhcp_pxebs_expired,
  656. .tx_msgtype = DHCPREQUEST,
  657. .apply_min_timeout = 1,
  658. };
  659. /****************************************************************************
  660. *
  661. * Packet construction
  662. *
  663. */
  664. /**
  665. * Create a DHCP packet
  666. *
  667. * @v dhcppkt DHCP packet structure to fill in
  668. * @v netdev Network device
  669. * @v msgtype DHCP message type
  670. * @v options Initial options to include (or NULL)
  671. * @v options_len Length of initial options
  672. * @v data Buffer for DHCP packet
  673. * @v max_len Size of DHCP packet buffer
  674. * @ret rc Return status code
  675. *
  676. * Creates a DHCP packet in the specified buffer, and initialise a
  677. * DHCP packet structure.
  678. */
  679. int dhcp_create_packet ( struct dhcp_packet *dhcppkt,
  680. struct net_device *netdev, uint8_t msgtype,
  681. const void *options, size_t options_len,
  682. void *data, size_t max_len ) {
  683. struct dhcphdr *dhcphdr = data;
  684. unsigned int hlen;
  685. int rc;
  686. /* Sanity check */
  687. if ( max_len < ( sizeof ( *dhcphdr ) + options_len ) )
  688. return -ENOSPC;
  689. /* Initialise DHCP packet content */
  690. memset ( dhcphdr, 0, max_len );
  691. dhcphdr->xid = dhcp_xid ( netdev );
  692. dhcphdr->magic = htonl ( DHCP_MAGIC_COOKIE );
  693. dhcphdr->htype = ntohs ( netdev->ll_protocol->ll_proto );
  694. dhcphdr->op = dhcp_op[msgtype];
  695. /* If hardware length exceeds the chaddr field length, don't
  696. * use the chaddr field. This is as per RFC4390.
  697. */
  698. hlen = netdev->ll_protocol->ll_addr_len;
  699. if ( hlen > sizeof ( dhcphdr->chaddr ) ) {
  700. hlen = 0;
  701. dhcphdr->flags = htons ( BOOTP_FL_BROADCAST );
  702. }
  703. dhcphdr->hlen = hlen;
  704. memcpy ( dhcphdr->chaddr, netdev->ll_addr, hlen );
  705. memcpy ( dhcphdr->options, options, options_len );
  706. /* Initialise DHCP packet structure */
  707. memset ( dhcppkt, 0, sizeof ( *dhcppkt ) );
  708. dhcppkt_init ( dhcppkt, data, max_len );
  709. /* Set DHCP_MESSAGE_TYPE option */
  710. if ( ( rc = dhcppkt_store ( dhcppkt, DHCP_MESSAGE_TYPE,
  711. &msgtype, sizeof ( msgtype ) ) ) != 0 )
  712. return rc;
  713. return 0;
  714. }
  715. /**
  716. * Create DHCP request packet
  717. *
  718. * @v dhcppkt DHCP packet structure to fill in
  719. * @v netdev Network device
  720. * @v msgtype DHCP message type
  721. * @v ciaddr Client IP address
  722. * @v data Buffer for DHCP packet
  723. * @v max_len Size of DHCP packet buffer
  724. * @ret rc Return status code
  725. *
  726. * Creates a DHCP request packet in the specified buffer, and
  727. * initialise a DHCP packet structure.
  728. */
  729. int dhcp_create_request ( struct dhcp_packet *dhcppkt,
  730. struct net_device *netdev, unsigned int msgtype,
  731. struct in_addr ciaddr, void *data, size_t max_len ) {
  732. struct device_description *desc = &netdev->dev->desc;
  733. struct dhcp_netdev_desc dhcp_desc;
  734. struct dhcp_client_id client_id;
  735. struct dhcp_client_uuid client_uuid;
  736. size_t dhcp_features_len;
  737. size_t ll_addr_len;
  738. ssize_t len;
  739. int rc;
  740. /* Create DHCP packet */
  741. if ( ( rc = dhcp_create_packet ( dhcppkt, netdev, msgtype,
  742. dhcp_request_options_data,
  743. sizeof ( dhcp_request_options_data ),
  744. data, max_len ) ) != 0 ) {
  745. DBG ( "DHCP could not create DHCP packet: %s\n",
  746. strerror ( rc ) );
  747. return rc;
  748. }
  749. /* Set client IP address */
  750. dhcppkt->dhcphdr->ciaddr = ciaddr;
  751. /* Add options to identify the feature list */
  752. dhcp_features_len = ( dhcp_features_end - dhcp_features );
  753. if ( ( rc = dhcppkt_store ( dhcppkt, DHCP_EB_ENCAP, dhcp_features,
  754. dhcp_features_len ) ) != 0 ) {
  755. DBG ( "DHCP could not set features list option: %s\n",
  756. strerror ( rc ) );
  757. return rc;
  758. }
  759. /* Add options to identify the network device */
  760. dhcp_desc.type = desc->bus_type;
  761. dhcp_desc.vendor = htons ( desc->vendor );
  762. dhcp_desc.device = htons ( desc->device );
  763. if ( ( rc = dhcppkt_store ( dhcppkt, DHCP_EB_BUS_ID, &dhcp_desc,
  764. sizeof ( dhcp_desc ) ) ) != 0 ) {
  765. DBG ( "DHCP could not set bus ID option: %s\n",
  766. strerror ( rc ) );
  767. return rc;
  768. }
  769. /* Add DHCP client identifier. Required for Infiniband, and
  770. * doesn't hurt other link layers.
  771. */
  772. client_id.ll_proto = ntohs ( netdev->ll_protocol->ll_proto );
  773. ll_addr_len = netdev->ll_protocol->ll_addr_len;
  774. assert ( ll_addr_len <= sizeof ( client_id.ll_addr ) );
  775. memcpy ( client_id.ll_addr, netdev->ll_addr, ll_addr_len );
  776. if ( ( rc = dhcppkt_store ( dhcppkt, DHCP_CLIENT_ID, &client_id,
  777. ( ll_addr_len + 1 ) ) ) != 0 ) {
  778. DBG ( "DHCP could not set client ID: %s\n",
  779. strerror ( rc ) );
  780. return rc;
  781. }
  782. /* Add client UUID, if we have one. Required for PXE. */
  783. client_uuid.type = DHCP_CLIENT_UUID_TYPE;
  784. if ( ( len = fetch_uuid_setting ( NULL, &uuid_setting,
  785. &client_uuid.uuid ) ) >= 0 ) {
  786. if ( ( rc = dhcppkt_store ( dhcppkt, DHCP_CLIENT_UUID,
  787. &client_uuid,
  788. sizeof ( client_uuid ) ) ) != 0 ) {
  789. DBG ( "DHCP could not set client UUID: %s\n",
  790. strerror ( rc ) );
  791. return rc;
  792. }
  793. }
  794. /* Add user class, if we have one. */
  795. if ( ( len = fetch_setting_len ( NULL, &user_class_setting ) ) >= 0 ) {
  796. char user_class[len];
  797. fetch_setting ( NULL, &user_class_setting, user_class,
  798. sizeof ( user_class ) );
  799. if ( ( rc = dhcppkt_store ( dhcppkt, DHCP_USER_CLASS_ID,
  800. &user_class,
  801. sizeof ( user_class ) ) ) != 0 ) {
  802. DBG ( "DHCP could not set user class: %s\n",
  803. strerror ( rc ) );
  804. return rc;
  805. }
  806. }
  807. return 0;
  808. }
  809. /****************************************************************************
  810. *
  811. * Data transfer interface
  812. *
  813. */
  814. /**
  815. * Transmit DHCP request
  816. *
  817. * @v dhcp DHCP session
  818. * @ret rc Return status code
  819. */
  820. static int dhcp_tx ( struct dhcp_session *dhcp ) {
  821. static struct sockaddr_in peer = {
  822. .sin_family = AF_INET,
  823. };
  824. struct xfer_metadata meta = {
  825. .netdev = dhcp->netdev,
  826. .src = ( struct sockaddr * ) &dhcp->local,
  827. .dest = ( struct sockaddr * ) &peer,
  828. };
  829. struct io_buffer *iobuf;
  830. uint8_t msgtype = dhcp->state->tx_msgtype;
  831. struct dhcp_packet dhcppkt;
  832. int rc;
  833. /* Start retry timer. Do this first so that failures to
  834. * transmit will be retried.
  835. */
  836. start_timer ( &dhcp->timer );
  837. /* Allocate buffer for packet */
  838. iobuf = xfer_alloc_iob ( &dhcp->xfer, DHCP_MIN_LEN );
  839. if ( ! iobuf )
  840. return -ENOMEM;
  841. /* Create basic DHCP packet in temporary buffer */
  842. if ( ( rc = dhcp_create_request ( &dhcppkt, dhcp->netdev, msgtype,
  843. dhcp->local.sin_addr, iobuf->data,
  844. iob_tailroom ( iobuf ) ) ) != 0 ) {
  845. DBGC ( dhcp, "DHCP %p could not construct DHCP request: %s\n",
  846. dhcp, strerror ( rc ) );
  847. goto done;
  848. }
  849. /* Fill in packet based on current state */
  850. if ( ( rc = dhcp->state->tx ( dhcp, &dhcppkt, &peer ) ) != 0 ) {
  851. DBGC ( dhcp, "DHCP %p could not fill DHCP request: %s\n",
  852. dhcp, strerror ( rc ) );
  853. goto done;
  854. }
  855. /* Transmit the packet */
  856. iob_put ( iobuf, dhcppkt.len );
  857. if ( ( rc = xfer_deliver_iob_meta ( &dhcp->xfer, iob_disown ( iobuf ),
  858. &meta ) ) != 0 ) {
  859. DBGC ( dhcp, "DHCP %p could not transmit UDP packet: %s\n",
  860. dhcp, strerror ( rc ) );
  861. goto done;
  862. }
  863. done:
  864. free_iob ( iobuf );
  865. return rc;
  866. }
  867. /**
  868. * Receive new data
  869. *
  870. * @v xfer Data transfer interface
  871. * @v iobuf I/O buffer
  872. * @v meta Transfer metadata
  873. * @ret rc Return status code
  874. */
  875. static int dhcp_deliver_iob ( struct xfer_interface *xfer,
  876. struct io_buffer *iobuf,
  877. struct xfer_metadata *meta ) {
  878. struct dhcp_session *dhcp =
  879. container_of ( xfer, struct dhcp_session, xfer );
  880. struct sockaddr_in *peer;
  881. size_t data_len;
  882. struct dhcp_packet *dhcppkt;
  883. struct dhcphdr *dhcphdr;
  884. uint8_t msgtype = 0;
  885. int rc = 0;
  886. /* Sanity checks */
  887. if ( ! meta ) {
  888. DBGC ( dhcp, "DHCP %p received packet without metadata\n",
  889. dhcp );
  890. rc = -EINVAL;
  891. goto err_no_meta;
  892. }
  893. if ( ! meta->src ) {
  894. DBGC ( dhcp, "DHCP %p received packet without source port\n",
  895. dhcp );
  896. rc = -EINVAL;
  897. goto err_no_src;
  898. }
  899. peer = ( struct sockaddr_in * ) meta->src;
  900. /* Create a DHCP packet containing the I/O buffer contents.
  901. * Whilst we could just use the original buffer in situ, that
  902. * would waste the unused space in the packet buffer, and also
  903. * waste a relatively scarce fully-aligned I/O buffer.
  904. */
  905. data_len = iob_len ( iobuf );
  906. dhcppkt = zalloc ( sizeof ( *dhcppkt ) + data_len );
  907. if ( ! dhcppkt ) {
  908. rc = -ENOMEM;
  909. goto err_alloc_dhcppkt;
  910. }
  911. dhcphdr = ( ( ( void * ) dhcppkt ) + sizeof ( *dhcppkt ) );
  912. memcpy ( dhcphdr, iobuf->data, data_len );
  913. dhcppkt_init ( dhcppkt, dhcphdr, data_len );
  914. /* Identify message type */
  915. dhcppkt_fetch ( dhcppkt, DHCP_MESSAGE_TYPE, &msgtype,
  916. sizeof ( msgtype ) );
  917. /* Check for matching transaction ID */
  918. if ( dhcphdr->xid != dhcp_xid ( dhcp->netdev ) ) {
  919. DBGC ( dhcp, "DHCP %p %s from %s:%d has bad transaction "
  920. "ID\n", dhcp, dhcp_msgtype_name ( msgtype ),
  921. inet_ntoa ( peer->sin_addr ),
  922. ntohs ( peer->sin_port ) );
  923. rc = -EINVAL;
  924. goto err_xid;
  925. };
  926. /* Handle packet based on current state */
  927. dhcp->state->rx ( dhcp, dhcppkt, peer, msgtype );
  928. err_xid:
  929. dhcppkt_put ( dhcppkt );
  930. err_alloc_dhcppkt:
  931. err_no_src:
  932. err_no_meta:
  933. free_iob ( iobuf );
  934. return rc;
  935. }
  936. /** DHCP data transfer interface operations */
  937. static struct xfer_interface_operations dhcp_xfer_operations = {
  938. .close = ignore_xfer_close,
  939. .vredirect = xfer_vopen,
  940. .window = unlimited_xfer_window,
  941. .alloc_iob = default_xfer_alloc_iob,
  942. .deliver_iob = dhcp_deliver_iob,
  943. .deliver_raw = xfer_deliver_as_iob,
  944. };
  945. /**
  946. * Handle DHCP retry timer expiry
  947. *
  948. * @v timer DHCP retry timer
  949. * @v fail Failure indicator
  950. */
  951. static void dhcp_timer_expired ( struct retry_timer *timer, int fail ) {
  952. struct dhcp_session *dhcp =
  953. container_of ( timer, struct dhcp_session, timer );
  954. /* If we have failed, terminate DHCP */
  955. if ( fail ) {
  956. dhcp_finished ( dhcp, -ETIMEDOUT );
  957. return;
  958. }
  959. /* Handle timer expiry based on current state */
  960. dhcp->state->expired ( dhcp );
  961. }
  962. /****************************************************************************
  963. *
  964. * Job control interface
  965. *
  966. */
  967. /**
  968. * Handle kill() event received via job control interface
  969. *
  970. * @v job DHCP job control interface
  971. */
  972. static void dhcp_job_kill ( struct job_interface *job ) {
  973. struct dhcp_session *dhcp =
  974. container_of ( job, struct dhcp_session, job );
  975. /* Terminate DHCP session */
  976. dhcp_finished ( dhcp, -ECANCELED );
  977. }
  978. /** DHCP job control interface operations */
  979. static struct job_interface_operations dhcp_job_operations = {
  980. .done = ignore_job_done,
  981. .kill = dhcp_job_kill,
  982. .progress = ignore_job_progress,
  983. };
  984. /****************************************************************************
  985. *
  986. * Instantiators
  987. *
  988. */
  989. /**
  990. * DHCP peer address for socket opening
  991. *
  992. * This is a dummy address; the only useful portion is the socket
  993. * family (so that we get a UDP connection). The DHCP client will set
  994. * the IP address and source port explicitly on each transmission.
  995. */
  996. static struct sockaddr dhcp_peer = {
  997. .sa_family = AF_INET,
  998. };
  999. /**
  1000. * Start DHCP state machine on a network device
  1001. *
  1002. * @v job Job control interface
  1003. * @v netdev Network device
  1004. * @ret rc Return status code
  1005. *
  1006. * Starts DHCP on the specified network device. If successful, the
  1007. * DHCPACK (and ProxyDHCPACK, if applicable) will be registered as
  1008. * option sources.
  1009. */
  1010. int start_dhcp ( struct job_interface *job, struct net_device *netdev ) {
  1011. struct dhcp_session *dhcp;
  1012. int rc;
  1013. /* Allocate and initialise structure */
  1014. dhcp = zalloc ( sizeof ( *dhcp ) );
  1015. if ( ! dhcp )
  1016. return -ENOMEM;
  1017. dhcp->refcnt.free = dhcp_free;
  1018. job_init ( &dhcp->job, &dhcp_job_operations, &dhcp->refcnt );
  1019. xfer_init ( &dhcp->xfer, &dhcp_xfer_operations, &dhcp->refcnt );
  1020. dhcp->netdev = netdev_get ( netdev );
  1021. dhcp->local.sin_family = AF_INET;
  1022. dhcp->local.sin_port = htons ( BOOTPC_PORT );
  1023. dhcp->timer.expired = dhcp_timer_expired;
  1024. /* Instantiate child objects and attach to our interfaces */
  1025. if ( ( rc = xfer_open_socket ( &dhcp->xfer, SOCK_DGRAM, &dhcp_peer,
  1026. ( struct sockaddr * ) &dhcp->local ) ) != 0 )
  1027. goto err;
  1028. /* Enter DHCPDISCOVER state */
  1029. dhcp_set_state ( dhcp, &dhcp_state_discover );
  1030. /* Attach parent interface, mortalise self, and return */
  1031. job_plug_plug ( &dhcp->job, job );
  1032. ref_put ( &dhcp->refcnt );
  1033. return 0;
  1034. err:
  1035. dhcp_finished ( dhcp, rc );
  1036. ref_put ( &dhcp->refcnt );
  1037. return rc;
  1038. }
  1039. /**
  1040. * Start PXE Boot Server Discovery on a network device
  1041. *
  1042. * @v job Job control interface
  1043. * @v netdev Network device
  1044. * @v pxe_server PXE server (may be a multicast address)
  1045. * @v pxe_type PXE server type
  1046. * @ret rc Return status code
  1047. *
  1048. * Starts PXE Boot Server Discovery on the specified network device.
  1049. * If successful, the Boot Server ACK will be registered as an option
  1050. * source.
  1051. */
  1052. int start_pxebs ( struct job_interface *job, struct net_device *netdev,
  1053. struct in_addr pxe_server, unsigned int pxe_type ) {
  1054. struct dhcp_session *dhcp;
  1055. int rc;
  1056. /* Allocate and initialise structure */
  1057. dhcp = zalloc ( sizeof ( *dhcp ) );
  1058. if ( ! dhcp )
  1059. return -ENOMEM;
  1060. dhcp->refcnt.free = dhcp_free;
  1061. job_init ( &dhcp->job, &dhcp_job_operations, &dhcp->refcnt );
  1062. xfer_init ( &dhcp->xfer, &dhcp_xfer_operations, &dhcp->refcnt );
  1063. dhcp->netdev = netdev_get ( netdev );
  1064. dhcp->local.sin_family = AF_INET;
  1065. fetch_ipv4_setting ( netdev_settings ( netdev ), &ip_setting,
  1066. &dhcp->local.sin_addr );
  1067. dhcp->local.sin_port = htons ( BOOTPC_PORT );
  1068. dhcp->pxe_server = pxe_server;
  1069. dhcp->pxe_type = htons ( pxe_type );
  1070. dhcp->timer.expired = dhcp_timer_expired;
  1071. /* Instantiate child objects and attach to our interfaces */
  1072. if ( ( rc = xfer_open_socket ( &dhcp->xfer, SOCK_DGRAM, &dhcp_peer,
  1073. ( struct sockaddr * ) &dhcp->local ) ) != 0 )
  1074. goto err;
  1075. /* Enter PXEBS state */
  1076. dhcp_set_state ( dhcp, &dhcp_state_pxebs );
  1077. /* Attach parent interface, mortalise self, and return */
  1078. job_plug_plug ( &dhcp->job, job );
  1079. ref_put ( &dhcp->refcnt );
  1080. return 0;
  1081. err:
  1082. dhcp_finished ( dhcp, rc );
  1083. ref_put ( &dhcp->refcnt );
  1084. return rc;
  1085. }