You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238
  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. rc = xfer_deliver_iob_meta ( &dhcp->xfer, iobuf, &meta );
  858. iobuf = NULL;
  859. if ( rc != 0 ) {
  860. DBGC ( dhcp, "DHCP %p could not transmit UDP packet: %s\n",
  861. dhcp, strerror ( rc ) );
  862. goto done;
  863. }
  864. done:
  865. free_iob ( iobuf );
  866. return rc;
  867. }
  868. /**
  869. * Receive new data
  870. *
  871. * @v xfer Data transfer interface
  872. * @v iobuf I/O buffer
  873. * @v meta Transfer metadata
  874. * @ret rc Return status code
  875. */
  876. static int dhcp_deliver_iob ( struct xfer_interface *xfer,
  877. struct io_buffer *iobuf,
  878. struct xfer_metadata *meta ) {
  879. struct dhcp_session *dhcp =
  880. container_of ( xfer, struct dhcp_session, xfer );
  881. struct sockaddr_in *peer;
  882. size_t data_len;
  883. struct dhcp_packet *dhcppkt;
  884. struct dhcphdr *dhcphdr;
  885. uint8_t msgtype = 0;
  886. int rc = 0;
  887. /* Sanity checks */
  888. if ( ! meta ) {
  889. DBGC ( dhcp, "DHCP %p received packet without metadata\n",
  890. dhcp );
  891. rc = -EINVAL;
  892. goto err_no_meta;
  893. }
  894. if ( ! meta->src ) {
  895. DBGC ( dhcp, "DHCP %p received packet without source port\n",
  896. dhcp );
  897. rc = -EINVAL;
  898. goto err_no_src;
  899. }
  900. peer = ( struct sockaddr_in * ) meta->src;
  901. /* Create a DHCP packet containing the I/O buffer contents.
  902. * Whilst we could just use the original buffer in situ, that
  903. * would waste the unused space in the packet buffer, and also
  904. * waste a relatively scarce fully-aligned I/O buffer.
  905. */
  906. data_len = iob_len ( iobuf );
  907. dhcppkt = zalloc ( sizeof ( *dhcppkt ) + data_len );
  908. if ( ! dhcppkt ) {
  909. rc = -ENOMEM;
  910. goto err_alloc_dhcppkt;
  911. }
  912. dhcphdr = ( ( ( void * ) dhcppkt ) + sizeof ( *dhcppkt ) );
  913. memcpy ( dhcphdr, iobuf->data, data_len );
  914. dhcppkt_init ( dhcppkt, dhcphdr, data_len );
  915. /* Identify message type */
  916. dhcppkt_fetch ( dhcppkt, DHCP_MESSAGE_TYPE, &msgtype,
  917. sizeof ( msgtype ) );
  918. /* Check for matching transaction ID */
  919. if ( dhcphdr->xid != dhcp_xid ( dhcp->netdev ) ) {
  920. DBGC ( dhcp, "DHCP %p %s from %s:%d has bad transaction "
  921. "ID\n", dhcp, dhcp_msgtype_name ( msgtype ),
  922. inet_ntoa ( peer->sin_addr ),
  923. ntohs ( peer->sin_port ) );
  924. rc = -EINVAL;
  925. goto err_xid;
  926. };
  927. /* Handle packet based on current state */
  928. dhcp->state->rx ( dhcp, dhcppkt, peer, msgtype );
  929. err_xid:
  930. dhcppkt_put ( dhcppkt );
  931. err_alloc_dhcppkt:
  932. err_no_src:
  933. err_no_meta:
  934. free_iob ( iobuf );
  935. return rc;
  936. }
  937. /** DHCP data transfer interface operations */
  938. static struct xfer_interface_operations dhcp_xfer_operations = {
  939. .close = ignore_xfer_close,
  940. .vredirect = xfer_vopen,
  941. .window = unlimited_xfer_window,
  942. .alloc_iob = default_xfer_alloc_iob,
  943. .deliver_iob = dhcp_deliver_iob,
  944. .deliver_raw = xfer_deliver_as_iob,
  945. };
  946. /**
  947. * Handle DHCP retry timer expiry
  948. *
  949. * @v timer DHCP retry timer
  950. * @v fail Failure indicator
  951. */
  952. static void dhcp_timer_expired ( struct retry_timer *timer, int fail ) {
  953. struct dhcp_session *dhcp =
  954. container_of ( timer, struct dhcp_session, timer );
  955. /* If we have failed, terminate DHCP */
  956. if ( fail ) {
  957. dhcp_finished ( dhcp, -ETIMEDOUT );
  958. return;
  959. }
  960. /* Handle timer expiry based on current state */
  961. dhcp->state->expired ( dhcp );
  962. }
  963. /****************************************************************************
  964. *
  965. * Job control interface
  966. *
  967. */
  968. /**
  969. * Handle kill() event received via job control interface
  970. *
  971. * @v job DHCP job control interface
  972. */
  973. static void dhcp_job_kill ( struct job_interface *job ) {
  974. struct dhcp_session *dhcp =
  975. container_of ( job, struct dhcp_session, job );
  976. /* Terminate DHCP session */
  977. dhcp_finished ( dhcp, -ECANCELED );
  978. }
  979. /** DHCP job control interface operations */
  980. static struct job_interface_operations dhcp_job_operations = {
  981. .done = ignore_job_done,
  982. .kill = dhcp_job_kill,
  983. .progress = ignore_job_progress,
  984. };
  985. /****************************************************************************
  986. *
  987. * Instantiators
  988. *
  989. */
  990. /**
  991. * DHCP peer address for socket opening
  992. *
  993. * This is a dummy address; the only useful portion is the socket
  994. * family (so that we get a UDP connection). The DHCP client will set
  995. * the IP address and source port explicitly on each transmission.
  996. */
  997. static struct sockaddr dhcp_peer = {
  998. .sa_family = AF_INET,
  999. };
  1000. /**
  1001. * Start DHCP state machine on a network device
  1002. *
  1003. * @v job Job control interface
  1004. * @v netdev Network device
  1005. * @ret rc Return status code
  1006. *
  1007. * Starts DHCP on the specified network device. If successful, the
  1008. * DHCPACK (and ProxyDHCPACK, if applicable) will be registered as
  1009. * option sources.
  1010. */
  1011. int start_dhcp ( struct job_interface *job, struct net_device *netdev ) {
  1012. struct dhcp_session *dhcp;
  1013. int rc;
  1014. /* Allocate and initialise structure */
  1015. dhcp = zalloc ( sizeof ( *dhcp ) );
  1016. if ( ! dhcp )
  1017. return -ENOMEM;
  1018. dhcp->refcnt.free = dhcp_free;
  1019. job_init ( &dhcp->job, &dhcp_job_operations, &dhcp->refcnt );
  1020. xfer_init ( &dhcp->xfer, &dhcp_xfer_operations, &dhcp->refcnt );
  1021. dhcp->netdev = netdev_get ( netdev );
  1022. dhcp->local.sin_family = AF_INET;
  1023. dhcp->local.sin_port = htons ( BOOTPC_PORT );
  1024. dhcp->timer.expired = dhcp_timer_expired;
  1025. /* Instantiate child objects and attach to our interfaces */
  1026. if ( ( rc = xfer_open_socket ( &dhcp->xfer, SOCK_DGRAM, &dhcp_peer,
  1027. ( struct sockaddr * ) &dhcp->local ) ) != 0 )
  1028. goto err;
  1029. /* Enter DHCPDISCOVER state */
  1030. dhcp_set_state ( dhcp, &dhcp_state_discover );
  1031. /* Attach parent interface, mortalise self, and return */
  1032. job_plug_plug ( &dhcp->job, job );
  1033. ref_put ( &dhcp->refcnt );
  1034. return 0;
  1035. err:
  1036. dhcp_finished ( dhcp, rc );
  1037. ref_put ( &dhcp->refcnt );
  1038. return rc;
  1039. }
  1040. /**
  1041. * Start PXE Boot Server Discovery on a network device
  1042. *
  1043. * @v job Job control interface
  1044. * @v netdev Network device
  1045. * @v pxe_server PXE server (may be a multicast address)
  1046. * @v pxe_type PXE server type
  1047. * @ret rc Return status code
  1048. *
  1049. * Starts PXE Boot Server Discovery on the specified network device.
  1050. * If successful, the Boot Server ACK will be registered as an option
  1051. * source.
  1052. */
  1053. int start_pxebs ( struct job_interface *job, struct net_device *netdev,
  1054. struct in_addr pxe_server, unsigned int pxe_type ) {
  1055. struct dhcp_session *dhcp;
  1056. int rc;
  1057. /* Allocate and initialise structure */
  1058. dhcp = zalloc ( sizeof ( *dhcp ) );
  1059. if ( ! dhcp )
  1060. return -ENOMEM;
  1061. dhcp->refcnt.free = dhcp_free;
  1062. job_init ( &dhcp->job, &dhcp_job_operations, &dhcp->refcnt );
  1063. xfer_init ( &dhcp->xfer, &dhcp_xfer_operations, &dhcp->refcnt );
  1064. dhcp->netdev = netdev_get ( netdev );
  1065. dhcp->local.sin_family = AF_INET;
  1066. fetch_ipv4_setting ( netdev_settings ( netdev ), &ip_setting,
  1067. &dhcp->local.sin_addr );
  1068. dhcp->local.sin_port = htons ( BOOTPC_PORT );
  1069. dhcp->pxe_server = pxe_server;
  1070. dhcp->pxe_type = htons ( pxe_type );
  1071. dhcp->timer.expired = dhcp_timer_expired;
  1072. /* Instantiate child objects and attach to our interfaces */
  1073. if ( ( rc = xfer_open_socket ( &dhcp->xfer, SOCK_DGRAM, &dhcp_peer,
  1074. ( struct sockaddr * ) &dhcp->local ) ) != 0 )
  1075. goto err;
  1076. /* Enter PXEBS state */
  1077. dhcp_set_state ( dhcp, &dhcp_state_pxebs );
  1078. /* Attach parent interface, mortalise self, and return */
  1079. job_plug_plug ( &dhcp->job, job );
  1080. ref_put ( &dhcp->refcnt );
  1081. return 0;
  1082. err:
  1083. dhcp_finished ( dhcp, rc );
  1084. ref_put ( &dhcp->refcnt );
  1085. return rc;
  1086. }