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 38KB

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