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_USER_CLASS_ID,
  76. DHCP_STRING ( 'g', 'P', 'X', 'E' ),
  77. DHCP_PARAMETER_REQUEST_LIST,
  78. DHCP_OPTION ( DHCP_SUBNET_MASK, DHCP_ROUTERS, DHCP_DNS_SERVERS,
  79. DHCP_LOG_SERVERS, DHCP_HOST_NAME, DHCP_DOMAIN_NAME,
  80. DHCP_ROOT_PATH, DHCP_VENDOR_ENCAP, DHCP_VENDOR_CLASS_ID,
  81. DHCP_TFTP_SERVER_NAME, DHCP_BOOTFILE_NAME,
  82. DHCP_EB_ENCAP, DHCP_ISCSI_INITIATOR_IQN ),
  83. DHCP_END
  84. };
  85. /** Version number feature */
  86. FEATURE_VERSION ( VERSION_MAJOR, VERSION_MINOR, VERSION_PATCH );
  87. /** DHCP server address setting */
  88. struct setting dhcp_server_setting __setting = {
  89. .name = "dhcp-server",
  90. .description = "DHCP server address",
  91. .tag = DHCP_SERVER_IDENTIFIER,
  92. .type = &setting_type_ipv4,
  93. };
  94. /** DHCP user class setting */
  95. struct setting user_class_setting __setting = {
  96. .name = "user-class",
  97. .description = "User class identifier",
  98. .tag = DHCP_USER_CLASS_ID,
  99. .type = &setting_type_string,
  100. };
  101. /**
  102. * Name a DHCP packet type
  103. *
  104. * @v msgtype DHCP message type
  105. * @ret string DHCP mesasge type name
  106. */
  107. static inline const char * dhcp_msgtype_name ( unsigned int msgtype ) {
  108. switch ( msgtype ) {
  109. case DHCPNONE: return "BOOTP"; /* Non-DHCP packet */
  110. case DHCPDISCOVER: return "DHCPDISCOVER";
  111. case DHCPOFFER: return "DHCPOFFER";
  112. case DHCPREQUEST: return "DHCPREQUEST";
  113. case DHCPDECLINE: return "DHCPDECLINE";
  114. case DHCPACK: return "DHCPACK";
  115. case DHCPNAK: return "DHCPNAK";
  116. case DHCPRELEASE: return "DHCPRELEASE";
  117. case DHCPINFORM: return "DHCPINFORM";
  118. default: return "DHCP<invalid>";
  119. }
  120. }
  121. /**
  122. * Calculate DHCP transaction ID for a network device
  123. *
  124. * @v netdev Network device
  125. * @ret xid DHCP XID
  126. *
  127. * Extract the least significant bits of the hardware address for use
  128. * as the transaction ID.
  129. */
  130. static uint32_t dhcp_xid ( struct net_device *netdev ) {
  131. uint32_t xid;
  132. memcpy ( &xid, ( netdev->ll_addr + netdev->ll_protocol->ll_addr_len
  133. - sizeof ( xid ) ), sizeof ( xid ) );
  134. return xid;
  135. }
  136. /****************************************************************************
  137. *
  138. * DHCP session
  139. *
  140. */
  141. struct dhcp_session;
  142. /** DHCP session state operations */
  143. struct dhcp_session_state {
  144. /** State name */
  145. const char *name;
  146. /**
  147. * Construct transmitted packet
  148. *
  149. * @v dhcp DHCP session
  150. * @v dhcppkt DHCP packet
  151. * @v peer Destination address
  152. */
  153. int ( * tx ) ( struct dhcp_session *dhcp,
  154. struct dhcp_packet *dhcppkt,
  155. struct sockaddr_in *peer );
  156. /** Handle received packet
  157. *
  158. * @v dhcp DHCP session
  159. * @v dhcppkt DHCP packet
  160. * @v peer DHCP server address
  161. * @v msgtype DHCP message type
  162. * @v server_id DHCP server ID
  163. */
  164. void ( * rx ) ( struct dhcp_session *dhcp,
  165. struct dhcp_packet *dhcppkt,
  166. struct sockaddr_in *peer,
  167. uint8_t msgtype, struct in_addr server_id );
  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 type */
  209. uint16_t pxe_type;
  210. /** List of PXE Boot Servers to attempt */
  211. struct in_addr *pxe_attempt;
  212. /** List of PXE Boot Servers to accept */
  213. struct in_addr *pxe_accept;
  214. /** Retransmission timer */
  215. struct retry_timer timer;
  216. /** Start time of the current state (in ticks) */
  217. unsigned long start;
  218. };
  219. /**
  220. * Free DHCP session
  221. *
  222. * @v refcnt Reference counter
  223. */
  224. static void dhcp_free ( struct refcnt *refcnt ) {
  225. struct dhcp_session *dhcp =
  226. container_of ( refcnt, struct dhcp_session, refcnt );
  227. netdev_put ( dhcp->netdev );
  228. free ( dhcp );
  229. }
  230. /**
  231. * Mark DHCP session as complete
  232. *
  233. * @v dhcp DHCP session
  234. * @v rc Return status code
  235. */
  236. static void dhcp_finished ( struct dhcp_session *dhcp, int rc ) {
  237. /* Block futher incoming messages */
  238. job_nullify ( &dhcp->job );
  239. xfer_nullify ( &dhcp->xfer );
  240. /* Stop retry timer */
  241. stop_timer ( &dhcp->timer );
  242. /* Free resources and close interfaces */
  243. xfer_close ( &dhcp->xfer, rc );
  244. job_done ( &dhcp->job, rc );
  245. }
  246. /**
  247. * Transition to new DHCP session state
  248. *
  249. * @v dhcp DHCP session
  250. * @v state New session state
  251. */
  252. static void dhcp_set_state ( struct dhcp_session *dhcp,
  253. struct dhcp_session_state *state ) {
  254. DBGC ( dhcp, "DHCP %p entering %s state\n", dhcp, state->name );
  255. dhcp->state = state;
  256. dhcp->start = currticks();
  257. stop_timer ( &dhcp->timer );
  258. dhcp->timer.min_timeout =
  259. ( state->apply_min_timeout ? DHCP_MIN_TIMEOUT : 0 );
  260. dhcp->timer.max_timeout = DHCP_MAX_TIMEOUT;
  261. start_timer_nodelay ( &dhcp->timer );
  262. }
  263. /****************************************************************************
  264. *
  265. * DHCP state machine
  266. *
  267. */
  268. /**
  269. * Construct transmitted packet for DHCP discovery
  270. *
  271. * @v dhcp DHCP session
  272. * @v dhcppkt DHCP packet
  273. * @v peer Destination address
  274. */
  275. static int dhcp_discovery_tx ( struct dhcp_session *dhcp,
  276. struct dhcp_packet *dhcppkt __unused,
  277. struct sockaddr_in *peer ) {
  278. DBGC ( dhcp, "DHCP %p DHCPDISCOVER\n", dhcp );
  279. /* Set server address */
  280. peer->sin_addr.s_addr = INADDR_BROADCAST;
  281. peer->sin_port = htons ( BOOTPS_PORT );
  282. return 0;
  283. }
  284. /**
  285. * Handle received packet during DHCP discovery
  286. *
  287. * @v dhcp DHCP session
  288. * @v dhcppkt DHCP packet
  289. * @v peer DHCP server address
  290. * @v msgtype DHCP message type
  291. * @v server_id DHCP server ID
  292. */
  293. static void dhcp_discovery_rx ( struct dhcp_session *dhcp,
  294. struct dhcp_packet *dhcppkt,
  295. struct sockaddr_in *peer, uint8_t msgtype,
  296. struct in_addr server_id ) {
  297. struct in_addr ip;
  298. char vci[9]; /* "PXEClient" */
  299. int vci_len;
  300. int has_pxeclient;
  301. int8_t priority = 0;
  302. uint8_t no_pxedhcp = 0;
  303. unsigned long elapsed;
  304. DBGC ( dhcp, "DHCP %p %s from %s:%d", dhcp,
  305. dhcp_msgtype_name ( msgtype ), inet_ntoa ( peer->sin_addr ),
  306. ntohs ( peer->sin_port ) );
  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. * @v server_id DHCP server ID
  425. */
  426. static void dhcp_request_rx ( struct dhcp_session *dhcp,
  427. struct dhcp_packet *dhcppkt,
  428. struct sockaddr_in *peer, uint8_t msgtype,
  429. struct in_addr server_id ) {
  430. struct in_addr ip;
  431. struct settings *parent;
  432. int rc;
  433. DBGC ( dhcp, "DHCP %p %s from %s:%d", dhcp,
  434. dhcp_msgtype_name ( msgtype ), inet_ntoa ( peer->sin_addr ),
  435. ntohs ( peer->sin_port ) );
  436. if ( server_id.s_addr != peer->sin_addr.s_addr )
  437. DBGC ( dhcp, " (%s)", inet_ntoa ( server_id ) );
  438. /* Identify leased IP address */
  439. ip = dhcppkt->dhcphdr->yiaddr;
  440. if ( ip.s_addr )
  441. DBGC ( dhcp, " for %s", inet_ntoa ( ip ) );
  442. DBGC ( dhcp, "\n" );
  443. /* Filter out unacceptable responses */
  444. if ( peer->sin_port != htons ( BOOTPS_PORT ) )
  445. return;
  446. if ( msgtype /* BOOTP */ && ( msgtype != DHCPACK ) )
  447. return;
  448. if ( server_id.s_addr != dhcp->server.s_addr )
  449. return;
  450. /* Record assigned address */
  451. dhcp->local.sin_addr = ip;
  452. /* Register settings */
  453. parent = netdev_settings ( dhcp->netdev );
  454. if ( ( rc = register_settings ( &dhcppkt->settings, parent ) ) != 0 ){
  455. DBGC ( dhcp, "DHCP %p could not register settings: %s\n",
  456. dhcp, strerror ( rc ) );
  457. dhcp_finished ( dhcp, rc );
  458. return;
  459. }
  460. /* Start ProxyDHCPREQUEST if applicable */
  461. if ( dhcp->proxy_server.s_addr && ( ! dhcp->no_pxedhcp ) ) {
  462. dhcp_set_state ( dhcp, &dhcp_state_proxy );
  463. return;
  464. }
  465. /* Terminate DHCP */
  466. dhcp_finished ( dhcp, 0 );
  467. }
  468. /**
  469. * Handle timer expiry during DHCP discovery
  470. *
  471. * @v dhcp DHCP session
  472. */
  473. static void dhcp_request_expired ( struct dhcp_session *dhcp ) {
  474. /* Retransmit current packet */
  475. dhcp_tx ( dhcp );
  476. }
  477. /** DHCP request state operations */
  478. static struct dhcp_session_state dhcp_state_request = {
  479. .name = "request",
  480. .tx = dhcp_request_tx,
  481. .rx = dhcp_request_rx,
  482. .expired = dhcp_request_expired,
  483. .tx_msgtype = DHCPREQUEST,
  484. .apply_min_timeout = 0,
  485. };
  486. /**
  487. * Construct transmitted packet for ProxyDHCP request
  488. *
  489. * @v dhcp DHCP session
  490. * @v dhcppkt DHCP packet
  491. * @v peer Destination address
  492. */
  493. static int dhcp_proxy_tx ( struct dhcp_session *dhcp,
  494. struct dhcp_packet *dhcppkt,
  495. struct sockaddr_in *peer ) {
  496. int rc;
  497. DBGC ( dhcp, "DHCP %p ProxyDHCP REQUEST to %s:%d\n",
  498. dhcp, inet_ntoa ( dhcp->proxy_server ), PXE_PORT );
  499. /* Set server ID */
  500. if ( ( rc = dhcppkt_store ( dhcppkt, DHCP_SERVER_IDENTIFIER,
  501. &dhcp->proxy_server,
  502. sizeof ( dhcp->proxy_server ) ) ) != 0 )
  503. return rc;
  504. /* Set server address */
  505. peer->sin_addr = dhcp->proxy_server;
  506. peer->sin_port = htons ( PXE_PORT );
  507. return 0;
  508. }
  509. /**
  510. * Handle received packet during ProxyDHCP request
  511. *
  512. * @v dhcp DHCP session
  513. * @v dhcppkt DHCP packet
  514. * @v peer DHCP server address
  515. * @v msgtype DHCP message type
  516. * @v server_id DHCP server ID
  517. */
  518. static void dhcp_proxy_rx ( struct dhcp_session *dhcp,
  519. struct dhcp_packet *dhcppkt,
  520. struct sockaddr_in *peer, uint8_t msgtype,
  521. struct in_addr server_id ) {
  522. int rc;
  523. DBGC ( dhcp, "DHCP %p %s from %s:%d", dhcp,
  524. dhcp_msgtype_name ( msgtype ), inet_ntoa ( peer->sin_addr ),
  525. ntohs ( peer->sin_port ) );
  526. if ( server_id.s_addr != peer->sin_addr.s_addr )
  527. DBGC ( dhcp, " (%s)", inet_ntoa ( server_id ) );
  528. DBGC ( dhcp, "\n" );
  529. /* Filter out unacceptable responses */
  530. if ( peer->sin_port != htons ( PXE_PORT ) )
  531. return;
  532. if ( msgtype != DHCPACK )
  533. return;
  534. if ( server_id.s_addr /* Linux PXE server omits server ID */ &&
  535. ( server_id.s_addr != dhcp->proxy_server.s_addr ) )
  536. return;
  537. /* Register settings */
  538. dhcppkt->settings.name = PROXYDHCP_SETTINGS_NAME;
  539. if ( ( rc = register_settings ( &dhcppkt->settings, NULL ) ) != 0 ) {
  540. DBGC ( dhcp, "DHCP %p could not register settings: %s\n",
  541. dhcp, strerror ( rc ) );
  542. dhcp_finished ( dhcp, rc );
  543. return;
  544. }
  545. /* Terminate DHCP */
  546. dhcp_finished ( dhcp, 0 );
  547. }
  548. /**
  549. * Handle timer expiry during ProxyDHCP request
  550. *
  551. * @v dhcp DHCP session
  552. */
  553. static void dhcp_proxy_expired ( struct dhcp_session *dhcp ) {
  554. unsigned long elapsed = ( currticks() - dhcp->start );
  555. /* Give up waiting for ProxyDHCP before we reach the failure point */
  556. if ( elapsed > PROXYDHCP_MAX_TIMEOUT ) {
  557. dhcp_finished ( dhcp, 0 );
  558. return;
  559. }
  560. /* Retransmit current packet */
  561. dhcp_tx ( dhcp );
  562. }
  563. /** ProxyDHCP request state operations */
  564. static struct dhcp_session_state dhcp_state_proxy = {
  565. .name = "ProxyDHCP",
  566. .tx = dhcp_proxy_tx,
  567. .rx = dhcp_proxy_rx,
  568. .expired = dhcp_proxy_expired,
  569. .tx_msgtype = DHCPREQUEST,
  570. .apply_min_timeout = 0,
  571. };
  572. /**
  573. * Construct transmitted packet for PXE Boot Server Discovery
  574. *
  575. * @v dhcp DHCP session
  576. * @v dhcppkt DHCP packet
  577. * @v peer Destination address
  578. */
  579. static int dhcp_pxebs_tx ( struct dhcp_session *dhcp,
  580. struct dhcp_packet *dhcppkt,
  581. struct sockaddr_in *peer ) {
  582. struct dhcp_pxe_boot_menu_item menu_item = { 0, 0 };
  583. int rc;
  584. DBGC ( dhcp, "DHCP %p PXEBS REQUEST to %s:%d for type %d\n",
  585. dhcp, inet_ntoa ( *(dhcp->pxe_attempt) ), PXE_PORT,
  586. ntohs ( dhcp->pxe_type ) );
  587. /* Set boot menu item */
  588. menu_item.type = dhcp->pxe_type;
  589. if ( ( rc = dhcppkt_store ( dhcppkt, DHCP_PXE_BOOT_MENU_ITEM,
  590. &menu_item, sizeof ( menu_item ) ) ) != 0 )
  591. return rc;
  592. /* Set server address */
  593. peer->sin_addr = *(dhcp->pxe_attempt);
  594. peer->sin_port = htons ( PXE_PORT );
  595. return 0;
  596. }
  597. /**
  598. * Check to see if PXE Boot Server address is acceptable
  599. *
  600. * @v dhcp DHCP session
  601. * @v bs Boot Server address
  602. * @ret accept Boot Server is acceptable
  603. */
  604. static int dhcp_pxebs_accept ( struct dhcp_session *dhcp,
  605. struct in_addr bs ) {
  606. struct in_addr *accept;
  607. /* Accept if we have no acceptance filter */
  608. if ( ! dhcp->pxe_accept )
  609. return 1;
  610. /* Scan through acceptance list */
  611. for ( accept = dhcp->pxe_accept ; accept->s_addr ; accept++ ) {
  612. if ( accept->s_addr == bs.s_addr )
  613. return 1;
  614. }
  615. DBGC ( dhcp, "DHCP %p rejecting server %s\n",
  616. dhcp, inet_ntoa ( bs ) );
  617. return 0;
  618. }
  619. /**
  620. * Handle received packet during PXE Boot Server Discovery
  621. *
  622. * @v dhcp DHCP session
  623. * @v dhcppkt DHCP packet
  624. * @v peer DHCP server address
  625. * @v msgtype DHCP message type
  626. * @v server_id DHCP server ID
  627. */
  628. static void dhcp_pxebs_rx ( struct dhcp_session *dhcp,
  629. struct dhcp_packet *dhcppkt,
  630. struct sockaddr_in *peer, uint8_t msgtype,
  631. struct in_addr server_id ) {
  632. struct dhcp_pxe_boot_menu_item menu_item = { 0, 0 };
  633. int rc;
  634. DBGC ( dhcp, "DHCP %p %s from %s:%d", dhcp,
  635. dhcp_msgtype_name ( msgtype ), inet_ntoa ( peer->sin_addr ),
  636. ntohs ( peer->sin_port ) );
  637. if ( server_id.s_addr != peer->sin_addr.s_addr )
  638. DBGC ( dhcp, " (%s)", inet_ntoa ( server_id ) );
  639. /* Identify boot menu item */
  640. dhcppkt_fetch ( dhcppkt, DHCP_PXE_BOOT_MENU_ITEM,
  641. &menu_item, sizeof ( menu_item ) );
  642. if ( menu_item.type )
  643. DBGC ( dhcp, " for type %d", ntohs ( menu_item.type ) );
  644. DBGC ( dhcp, "\n" );
  645. /* Filter out unacceptable responses */
  646. if ( peer->sin_port != htons ( PXE_PORT ) )
  647. return;
  648. if ( msgtype != DHCPACK )
  649. return;
  650. if ( menu_item.type != dhcp->pxe_type )
  651. return;
  652. if ( ! dhcp_pxebs_accept ( dhcp, ( server_id.s_addr ?
  653. server_id : peer->sin_addr ) ) )
  654. return;
  655. /* Register settings */
  656. dhcppkt->settings.name = PXEBS_SETTINGS_NAME;
  657. if ( ( rc = register_settings ( &dhcppkt->settings, NULL ) ) != 0 ) {
  658. DBGC ( dhcp, "DHCP %p could not register settings: %s\n",
  659. dhcp, strerror ( rc ) );
  660. dhcp_finished ( dhcp, rc );
  661. return;
  662. }
  663. /* Terminate DHCP */
  664. dhcp_finished ( dhcp, 0 );
  665. }
  666. /**
  667. * Handle timer expiry during PXE Boot Server Discovery
  668. *
  669. * @v dhcp DHCP session
  670. */
  671. static void dhcp_pxebs_expired ( struct dhcp_session *dhcp ) {
  672. unsigned long elapsed = ( currticks() - dhcp->start );
  673. /* Give up waiting before we reach the failure point, and fail
  674. * over to the next server in the attempt list
  675. */
  676. if ( elapsed > PXEBS_MAX_TIMEOUT ) {
  677. dhcp->pxe_attempt++;
  678. if ( dhcp->pxe_attempt->s_addr ) {
  679. dhcp_set_state ( dhcp, &dhcp_state_pxebs );
  680. return;
  681. } else {
  682. dhcp_finished ( dhcp, -ETIMEDOUT );
  683. return;
  684. }
  685. }
  686. /* Retransmit current packet */
  687. dhcp_tx ( dhcp );
  688. }
  689. /** PXE Boot Server Discovery state operations */
  690. static struct dhcp_session_state dhcp_state_pxebs = {
  691. .name = "PXEBS",
  692. .tx = dhcp_pxebs_tx,
  693. .rx = dhcp_pxebs_rx,
  694. .expired = dhcp_pxebs_expired,
  695. .tx_msgtype = DHCPREQUEST,
  696. .apply_min_timeout = 1,
  697. };
  698. /****************************************************************************
  699. *
  700. * Packet construction
  701. *
  702. */
  703. /**
  704. * Create a DHCP packet
  705. *
  706. * @v dhcppkt DHCP packet structure to fill in
  707. * @v netdev Network device
  708. * @v msgtype DHCP message type
  709. * @v options Initial options to include (or NULL)
  710. * @v options_len Length of initial options
  711. * @v data Buffer for DHCP packet
  712. * @v max_len Size of DHCP packet buffer
  713. * @ret rc Return status code
  714. *
  715. * Creates a DHCP packet in the specified buffer, and initialise a
  716. * DHCP packet structure.
  717. */
  718. int dhcp_create_packet ( struct dhcp_packet *dhcppkt,
  719. struct net_device *netdev, uint8_t msgtype,
  720. const void *options, size_t options_len,
  721. void *data, size_t max_len ) {
  722. struct dhcphdr *dhcphdr = data;
  723. unsigned int hlen;
  724. int rc;
  725. /* Sanity check */
  726. if ( max_len < ( sizeof ( *dhcphdr ) + options_len ) )
  727. return -ENOSPC;
  728. /* Initialise DHCP packet content */
  729. memset ( dhcphdr, 0, max_len );
  730. dhcphdr->xid = dhcp_xid ( netdev );
  731. dhcphdr->magic = htonl ( DHCP_MAGIC_COOKIE );
  732. dhcphdr->htype = ntohs ( netdev->ll_protocol->ll_proto );
  733. dhcphdr->op = dhcp_op[msgtype];
  734. /* If hardware length exceeds the chaddr field length, don't
  735. * use the chaddr field. This is as per RFC4390.
  736. */
  737. hlen = netdev->ll_protocol->ll_addr_len;
  738. if ( hlen > sizeof ( dhcphdr->chaddr ) ) {
  739. hlen = 0;
  740. dhcphdr->flags = htons ( BOOTP_FL_BROADCAST );
  741. }
  742. dhcphdr->hlen = hlen;
  743. memcpy ( dhcphdr->chaddr, netdev->ll_addr, hlen );
  744. memcpy ( dhcphdr->options, options, options_len );
  745. /* Initialise DHCP packet structure */
  746. memset ( dhcppkt, 0, sizeof ( *dhcppkt ) );
  747. dhcppkt_init ( dhcppkt, data, max_len );
  748. /* Set DHCP_MESSAGE_TYPE option */
  749. if ( ( rc = dhcppkt_store ( dhcppkt, DHCP_MESSAGE_TYPE,
  750. &msgtype, sizeof ( msgtype ) ) ) != 0 )
  751. return rc;
  752. return 0;
  753. }
  754. /**
  755. * Create DHCP request packet
  756. *
  757. * @v dhcppkt DHCP packet structure to fill in
  758. * @v netdev Network device
  759. * @v msgtype DHCP message type
  760. * @v ciaddr Client IP address
  761. * @v data Buffer for DHCP packet
  762. * @v max_len Size of DHCP packet buffer
  763. * @ret rc Return status code
  764. *
  765. * Creates a DHCP request packet in the specified buffer, and
  766. * initialise a DHCP packet structure.
  767. */
  768. int dhcp_create_request ( struct dhcp_packet *dhcppkt,
  769. struct net_device *netdev, unsigned int msgtype,
  770. struct in_addr ciaddr, void *data, size_t max_len ) {
  771. struct device_description *desc = &netdev->dev->desc;
  772. struct dhcp_netdev_desc dhcp_desc;
  773. struct dhcp_client_id client_id;
  774. struct dhcp_client_uuid client_uuid;
  775. uint8_t *dhcp_features;
  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 = table_start ( DHCP_FEATURES );
  793. dhcp_features_len = table_num_entries ( DHCP_FEATURES );
  794. if ( ( rc = dhcppkt_store ( dhcppkt, DHCP_EB_ENCAP, dhcp_features,
  795. dhcp_features_len ) ) != 0 ) {
  796. DBG ( "DHCP could not set features list option: %s\n",
  797. strerror ( rc ) );
  798. return rc;
  799. }
  800. /* Add options to identify the network device */
  801. dhcp_desc.type = desc->bus_type;
  802. dhcp_desc.vendor = htons ( desc->vendor );
  803. dhcp_desc.device = htons ( desc->device );
  804. if ( ( rc = dhcppkt_store ( dhcppkt, DHCP_EB_BUS_ID, &dhcp_desc,
  805. sizeof ( dhcp_desc ) ) ) != 0 ) {
  806. DBG ( "DHCP could not set bus ID option: %s\n",
  807. strerror ( rc ) );
  808. return rc;
  809. }
  810. /* Add DHCP client identifier. Required for Infiniband, and
  811. * doesn't hurt other link layers.
  812. */
  813. client_id.ll_proto = ntohs ( netdev->ll_protocol->ll_proto );
  814. ll_addr_len = netdev->ll_protocol->ll_addr_len;
  815. assert ( ll_addr_len <= sizeof ( client_id.ll_addr ) );
  816. memcpy ( client_id.ll_addr, netdev->ll_addr, ll_addr_len );
  817. if ( ( rc = dhcppkt_store ( dhcppkt, DHCP_CLIENT_ID, &client_id,
  818. ( ll_addr_len + 1 ) ) ) != 0 ) {
  819. DBG ( "DHCP could not set client ID: %s\n",
  820. strerror ( rc ) );
  821. return rc;
  822. }
  823. /* Add client UUID, if we have one. Required for PXE. */
  824. client_uuid.type = DHCP_CLIENT_UUID_TYPE;
  825. if ( ( len = fetch_uuid_setting ( NULL, &uuid_setting,
  826. &client_uuid.uuid ) ) >= 0 ) {
  827. if ( ( rc = dhcppkt_store ( dhcppkt, DHCP_CLIENT_UUID,
  828. &client_uuid,
  829. sizeof ( client_uuid ) ) ) != 0 ) {
  830. DBG ( "DHCP could not set client UUID: %s\n",
  831. strerror ( rc ) );
  832. return rc;
  833. }
  834. }
  835. /* Add user class, if we have one. */
  836. if ( ( len = fetch_setting_len ( NULL, &user_class_setting ) ) >= 0 ) {
  837. char user_class[len];
  838. fetch_setting ( NULL, &user_class_setting, user_class,
  839. sizeof ( user_class ) );
  840. if ( ( rc = dhcppkt_store ( dhcppkt, DHCP_USER_CLASS_ID,
  841. &user_class,
  842. sizeof ( user_class ) ) ) != 0 ) {
  843. DBG ( "DHCP could not set user class: %s\n",
  844. strerror ( rc ) );
  845. return rc;
  846. }
  847. }
  848. return 0;
  849. }
  850. /****************************************************************************
  851. *
  852. * Data transfer interface
  853. *
  854. */
  855. /**
  856. * Transmit DHCP request
  857. *
  858. * @v dhcp DHCP session
  859. * @ret rc Return status code
  860. */
  861. static int dhcp_tx ( struct dhcp_session *dhcp ) {
  862. static struct sockaddr_in peer = {
  863. .sin_family = AF_INET,
  864. };
  865. struct xfer_metadata meta = {
  866. .netdev = dhcp->netdev,
  867. .src = ( struct sockaddr * ) &dhcp->local,
  868. .dest = ( struct sockaddr * ) &peer,
  869. };
  870. struct io_buffer *iobuf;
  871. uint8_t msgtype = dhcp->state->tx_msgtype;
  872. struct dhcp_packet dhcppkt;
  873. int rc;
  874. /* Start retry timer. Do this first so that failures to
  875. * transmit will be retried.
  876. */
  877. start_timer ( &dhcp->timer );
  878. /* Allocate buffer for packet */
  879. iobuf = xfer_alloc_iob ( &dhcp->xfer, DHCP_MIN_LEN );
  880. if ( ! iobuf )
  881. return -ENOMEM;
  882. /* Create basic DHCP packet in temporary buffer */
  883. if ( ( rc = dhcp_create_request ( &dhcppkt, dhcp->netdev, msgtype,
  884. dhcp->local.sin_addr, iobuf->data,
  885. iob_tailroom ( iobuf ) ) ) != 0 ) {
  886. DBGC ( dhcp, "DHCP %p could not construct DHCP request: %s\n",
  887. dhcp, strerror ( rc ) );
  888. goto done;
  889. }
  890. /* Fill in packet based on current state */
  891. if ( ( rc = dhcp->state->tx ( dhcp, &dhcppkt, &peer ) ) != 0 ) {
  892. DBGC ( dhcp, "DHCP %p could not fill DHCP request: %s\n",
  893. dhcp, strerror ( rc ) );
  894. goto done;
  895. }
  896. /* Transmit the packet */
  897. iob_put ( iobuf, dhcppkt.len );
  898. if ( ( rc = xfer_deliver_iob_meta ( &dhcp->xfer, iob_disown ( iobuf ),
  899. &meta ) ) != 0 ) {
  900. DBGC ( dhcp, "DHCP %p could not transmit UDP packet: %s\n",
  901. dhcp, strerror ( rc ) );
  902. goto done;
  903. }
  904. done:
  905. free_iob ( iobuf );
  906. return rc;
  907. }
  908. /**
  909. * Receive new data
  910. *
  911. * @v xfer Data transfer interface
  912. * @v iobuf I/O buffer
  913. * @v meta Transfer metadata
  914. * @ret rc Return status code
  915. */
  916. static int dhcp_deliver_iob ( struct xfer_interface *xfer,
  917. struct io_buffer *iobuf,
  918. struct xfer_metadata *meta ) {
  919. struct dhcp_session *dhcp =
  920. container_of ( xfer, struct dhcp_session, xfer );
  921. struct sockaddr_in *peer;
  922. size_t data_len;
  923. struct dhcp_packet *dhcppkt;
  924. struct dhcphdr *dhcphdr;
  925. uint8_t msgtype = 0;
  926. struct in_addr server_id = { 0 };
  927. int rc = 0;
  928. /* Sanity checks */
  929. if ( ! meta->src ) {
  930. DBGC ( dhcp, "DHCP %p received packet without source port\n",
  931. dhcp );
  932. rc = -EINVAL;
  933. goto err_no_src;
  934. }
  935. peer = ( struct sockaddr_in * ) meta->src;
  936. /* Create a DHCP packet containing the I/O buffer contents.
  937. * Whilst we could just use the original buffer in situ, that
  938. * would waste the unused space in the packet buffer, and also
  939. * waste a relatively scarce fully-aligned I/O buffer.
  940. */
  941. data_len = iob_len ( iobuf );
  942. dhcppkt = zalloc ( sizeof ( *dhcppkt ) + data_len );
  943. if ( ! dhcppkt ) {
  944. rc = -ENOMEM;
  945. goto err_alloc_dhcppkt;
  946. }
  947. dhcphdr = ( ( ( void * ) dhcppkt ) + sizeof ( *dhcppkt ) );
  948. memcpy ( dhcphdr, iobuf->data, data_len );
  949. dhcppkt_init ( dhcppkt, dhcphdr, data_len );
  950. /* Identify message type */
  951. dhcppkt_fetch ( dhcppkt, DHCP_MESSAGE_TYPE, &msgtype,
  952. sizeof ( msgtype ) );
  953. /* Identify server ID */
  954. dhcppkt_fetch ( dhcppkt, DHCP_SERVER_IDENTIFIER,
  955. &server_id, sizeof ( server_id ) );
  956. /* Check for matching transaction ID */
  957. if ( dhcphdr->xid != dhcp_xid ( dhcp->netdev ) ) {
  958. DBGC ( dhcp, "DHCP %p %s from %s:%d has bad transaction "
  959. "ID\n", dhcp, dhcp_msgtype_name ( msgtype ),
  960. inet_ntoa ( peer->sin_addr ),
  961. ntohs ( peer->sin_port ) );
  962. rc = -EINVAL;
  963. goto err_xid;
  964. };
  965. /* Handle packet based on current state */
  966. dhcp->state->rx ( dhcp, dhcppkt, peer, msgtype, server_id );
  967. err_xid:
  968. dhcppkt_put ( dhcppkt );
  969. err_alloc_dhcppkt:
  970. err_no_src:
  971. free_iob ( iobuf );
  972. return rc;
  973. }
  974. /** DHCP data transfer interface operations */
  975. static struct xfer_interface_operations dhcp_xfer_operations = {
  976. .close = ignore_xfer_close,
  977. .vredirect = xfer_vreopen,
  978. .window = unlimited_xfer_window,
  979. .alloc_iob = default_xfer_alloc_iob,
  980. .deliver_iob = dhcp_deliver_iob,
  981. .deliver_raw = xfer_deliver_as_iob,
  982. };
  983. /**
  984. * Handle DHCP retry timer expiry
  985. *
  986. * @v timer DHCP retry timer
  987. * @v fail Failure indicator
  988. */
  989. static void dhcp_timer_expired ( struct retry_timer *timer, int fail ) {
  990. struct dhcp_session *dhcp =
  991. container_of ( timer, struct dhcp_session, timer );
  992. /* If we have failed, terminate DHCP */
  993. if ( fail ) {
  994. dhcp_finished ( dhcp, -ETIMEDOUT );
  995. return;
  996. }
  997. /* Handle timer expiry based on current state */
  998. dhcp->state->expired ( dhcp );
  999. }
  1000. /****************************************************************************
  1001. *
  1002. * Job control interface
  1003. *
  1004. */
  1005. /**
  1006. * Handle kill() event received via job control interface
  1007. *
  1008. * @v job DHCP job control interface
  1009. */
  1010. static void dhcp_job_kill ( struct job_interface *job ) {
  1011. struct dhcp_session *dhcp =
  1012. container_of ( job, struct dhcp_session, job );
  1013. /* Terminate DHCP session */
  1014. dhcp_finished ( dhcp, -ECANCELED );
  1015. }
  1016. /** DHCP job control interface operations */
  1017. static struct job_interface_operations dhcp_job_operations = {
  1018. .done = ignore_job_done,
  1019. .kill = dhcp_job_kill,
  1020. .progress = ignore_job_progress,
  1021. };
  1022. /****************************************************************************
  1023. *
  1024. * Instantiators
  1025. *
  1026. */
  1027. /**
  1028. * DHCP peer address for socket opening
  1029. *
  1030. * This is a dummy address; the only useful portion is the socket
  1031. * family (so that we get a UDP connection). The DHCP client will set
  1032. * the IP address and source port explicitly on each transmission.
  1033. */
  1034. static struct sockaddr dhcp_peer = {
  1035. .sa_family = AF_INET,
  1036. };
  1037. /**
  1038. * Start DHCP state machine on a network device
  1039. *
  1040. * @v job Job control interface
  1041. * @v netdev Network device
  1042. * @ret rc Return status code
  1043. *
  1044. * Starts DHCP on the specified network device. If successful, the
  1045. * DHCPACK (and ProxyDHCPACK, if applicable) will be registered as
  1046. * option sources.
  1047. */
  1048. int start_dhcp ( struct job_interface *job, struct net_device *netdev ) {
  1049. struct dhcp_session *dhcp;
  1050. int rc;
  1051. /* Allocate and initialise structure */
  1052. dhcp = zalloc ( sizeof ( *dhcp ) );
  1053. if ( ! dhcp )
  1054. return -ENOMEM;
  1055. dhcp->refcnt.free = dhcp_free;
  1056. job_init ( &dhcp->job, &dhcp_job_operations, &dhcp->refcnt );
  1057. xfer_init ( &dhcp->xfer, &dhcp_xfer_operations, &dhcp->refcnt );
  1058. dhcp->netdev = netdev_get ( netdev );
  1059. dhcp->local.sin_family = AF_INET;
  1060. dhcp->local.sin_port = htons ( BOOTPC_PORT );
  1061. dhcp->timer.expired = dhcp_timer_expired;
  1062. /* Instantiate child objects and attach to our interfaces */
  1063. if ( ( rc = xfer_open_socket ( &dhcp->xfer, SOCK_DGRAM, &dhcp_peer,
  1064. ( struct sockaddr * ) &dhcp->local ) ) != 0 )
  1065. goto err;
  1066. /* Enter DHCPDISCOVER state */
  1067. dhcp_set_state ( dhcp, &dhcp_state_discover );
  1068. /* Attach parent interface, mortalise self, and return */
  1069. job_plug_plug ( &dhcp->job, job );
  1070. ref_put ( &dhcp->refcnt );
  1071. return 0;
  1072. err:
  1073. dhcp_finished ( dhcp, rc );
  1074. ref_put ( &dhcp->refcnt );
  1075. return rc;
  1076. }
  1077. /**
  1078. * Retrieve list of PXE boot servers for a given server type
  1079. *
  1080. * @v dhcp DHCP session
  1081. * @v raw DHCP PXE boot server list
  1082. * @v raw_len Length of DHCP PXE boot server list
  1083. * @v ip IP address list to fill in
  1084. *
  1085. * The caller must ensure that the IP address list has sufficient
  1086. * space.
  1087. */
  1088. static void pxebs_list ( struct dhcp_session *dhcp, void *raw,
  1089. size_t raw_len, struct in_addr *ip ) {
  1090. struct dhcp_pxe_boot_server *server = raw;
  1091. size_t server_len;
  1092. unsigned int i;
  1093. while ( raw_len ) {
  1094. if ( raw_len < sizeof ( *server ) ) {
  1095. DBGC ( dhcp, "DHCP %p malformed PXE server list\n",
  1096. dhcp );
  1097. break;
  1098. }
  1099. server_len = offsetof ( typeof ( *server ),
  1100. ip[ server->num_ip ] );
  1101. if ( raw_len < server_len ) {
  1102. DBGC ( dhcp, "DHCP %p malformed PXE server list\n",
  1103. dhcp );
  1104. break;
  1105. }
  1106. if ( server->type == dhcp->pxe_type ) {
  1107. for ( i = 0 ; i < server->num_ip ; i++ )
  1108. *(ip++) = server->ip[i];
  1109. }
  1110. server = ( ( ( void * ) server ) + server_len );
  1111. raw_len -= server_len;
  1112. }
  1113. }
  1114. /**
  1115. * Start PXE Boot Server Discovery on a network device
  1116. *
  1117. * @v job Job control interface
  1118. * @v netdev Network device
  1119. * @v pxe_type PXE server type
  1120. * @ret rc Return status code
  1121. *
  1122. * Starts PXE Boot Server Discovery on the specified network device.
  1123. * If successful, the Boot Server ACK will be registered as an option
  1124. * source.
  1125. */
  1126. int start_pxebs ( struct job_interface *job, struct net_device *netdev,
  1127. unsigned int pxe_type ) {
  1128. struct setting pxe_discovery_control_setting =
  1129. { .tag = DHCP_PXE_DISCOVERY_CONTROL };
  1130. struct setting pxe_boot_servers_setting =
  1131. { .tag = DHCP_PXE_BOOT_SERVERS };
  1132. struct setting pxe_boot_server_mcast_setting =
  1133. { .tag = DHCP_PXE_BOOT_SERVER_MCAST };
  1134. ssize_t pxebs_list_len;
  1135. struct dhcp_session *dhcp;
  1136. struct in_addr *ip;
  1137. unsigned int pxe_discovery_control;
  1138. int rc;
  1139. /* Get upper bound for PXE boot server IP address list */
  1140. pxebs_list_len = fetch_setting_len ( NULL, &pxe_boot_servers_setting );
  1141. if ( pxebs_list_len < 0 )
  1142. pxebs_list_len = 0;
  1143. /* Allocate and initialise structure */
  1144. dhcp = zalloc ( sizeof ( *dhcp ) + sizeof ( *ip ) /* mcast */ +
  1145. sizeof ( *ip ) /* bcast */ + pxebs_list_len +
  1146. sizeof ( *ip ) /* terminator */ );
  1147. if ( ! dhcp )
  1148. return -ENOMEM;
  1149. dhcp->refcnt.free = dhcp_free;
  1150. job_init ( &dhcp->job, &dhcp_job_operations, &dhcp->refcnt );
  1151. xfer_init ( &dhcp->xfer, &dhcp_xfer_operations, &dhcp->refcnt );
  1152. dhcp->netdev = netdev_get ( netdev );
  1153. dhcp->local.sin_family = AF_INET;
  1154. fetch_ipv4_setting ( netdev_settings ( netdev ), &ip_setting,
  1155. &dhcp->local.sin_addr );
  1156. dhcp->local.sin_port = htons ( BOOTPC_PORT );
  1157. dhcp->pxe_type = htons ( pxe_type );
  1158. dhcp->timer.expired = dhcp_timer_expired;
  1159. /* Construct PXE boot server IP address lists */
  1160. pxe_discovery_control =
  1161. fetch_uintz_setting ( NULL, &pxe_discovery_control_setting );
  1162. ip = ( ( ( void * ) dhcp ) + sizeof ( *dhcp ) );
  1163. dhcp->pxe_attempt = ip;
  1164. if ( ! ( pxe_discovery_control & PXEBS_NO_MULTICAST ) ) {
  1165. fetch_ipv4_setting ( NULL, &pxe_boot_server_mcast_setting, ip);
  1166. if ( ip->s_addr )
  1167. ip++;
  1168. }
  1169. if ( ! ( pxe_discovery_control & PXEBS_NO_BROADCAST ) )
  1170. (ip++)->s_addr = INADDR_BROADCAST;
  1171. if ( pxe_discovery_control & PXEBS_NO_UNKNOWN_SERVERS )
  1172. dhcp->pxe_accept = ip;
  1173. if ( pxebs_list_len ) {
  1174. uint8_t buf[pxebs_list_len];
  1175. fetch_setting ( NULL, &pxe_boot_servers_setting,
  1176. buf, sizeof ( buf ) );
  1177. pxebs_list ( dhcp, buf, sizeof ( buf ), ip );
  1178. }
  1179. if ( ! dhcp->pxe_attempt->s_addr ) {
  1180. DBGC ( dhcp, "DHCP %p has no PXE boot servers for type %04x\n",
  1181. dhcp, pxe_type );
  1182. rc = -EINVAL;
  1183. goto err;
  1184. }
  1185. /* Dump out PXE server lists */
  1186. DBGC ( dhcp, "DHCP %p attempting", dhcp );
  1187. for ( ip = dhcp->pxe_attempt ; ip->s_addr ; ip++ )
  1188. DBGC ( dhcp, " %s", inet_ntoa ( *ip ) );
  1189. DBGC ( dhcp, "\n" );
  1190. if ( dhcp->pxe_accept ) {
  1191. DBGC ( dhcp, "DHCP %p accepting", dhcp );
  1192. for ( ip = dhcp->pxe_accept ; ip->s_addr ; ip++ )
  1193. DBGC ( dhcp, " %s", inet_ntoa ( *ip ) );
  1194. DBGC ( dhcp, "\n" );
  1195. }
  1196. /* Instantiate child objects and attach to our interfaces */
  1197. if ( ( rc = xfer_open_socket ( &dhcp->xfer, SOCK_DGRAM, &dhcp_peer,
  1198. ( struct sockaddr * ) &dhcp->local ) ) != 0 )
  1199. goto err;
  1200. /* Enter PXEBS state */
  1201. dhcp_set_state ( dhcp, &dhcp_state_pxebs );
  1202. /* Attach parent interface, mortalise self, and return */
  1203. job_plug_plug ( &dhcp->job, job );
  1204. ref_put ( &dhcp->refcnt );
  1205. return 0;
  1206. err:
  1207. dhcp_finished ( dhcp, rc );
  1208. ref_put ( &dhcp->refcnt );
  1209. return rc;
  1210. }