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

dhcp.c 37KB

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