You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

dhcp.c 41KB

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