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.

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