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