dhcp.c 42KB

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