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

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