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.

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