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 42KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499
  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, struct dhcp_packet *dhcppkt,
  146. struct sockaddr_in *peer );
  147. /**
  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. * @v pseudo_id DHCP server pseudo-ID
  156. */
  157. void ( * rx ) ( struct dhcp_session *dhcp, struct dhcp_packet *dhcppkt,
  158. struct sockaddr_in *peer, uint8_t msgtype,
  159. struct in_addr server_id, struct in_addr pseudo_id );
  160. /**
  161. * Handle timer expiry
  162. *
  163. * @v dhcp DHCP session
  164. */
  165. void ( * expired ) ( struct dhcp_session *dhcp );
  166. /** Transmitted message type */
  167. uint8_t tx_msgtype;
  168. /** Timeout parameters */
  169. uint8_t min_timeout_sec;
  170. uint8_t max_timeout_sec;
  171. };
  172. static struct dhcp_session_state dhcp_state_discover;
  173. static struct dhcp_session_state dhcp_state_request;
  174. static struct dhcp_session_state dhcp_state_proxy;
  175. static struct dhcp_session_state dhcp_state_pxebs;
  176. /** A DHCP session */
  177. struct dhcp_session {
  178. /** Reference counter */
  179. struct refcnt refcnt;
  180. /** Job control interface */
  181. struct interface job;
  182. /** Data transfer interface */
  183. struct interface xfer;
  184. /** Network device being configured */
  185. struct net_device *netdev;
  186. /** Local socket address */
  187. struct sockaddr_in local;
  188. /** State of the session */
  189. struct dhcp_session_state *state;
  190. /** Transaction ID (in network-endian order) */
  191. uint32_t xid;
  192. /** Offered IP address */
  193. struct in_addr offer;
  194. /** DHCP server */
  195. struct in_addr server;
  196. /** DHCP offer priority */
  197. int priority;
  198. /** ProxyDHCP protocol extensions should be ignored */
  199. int no_pxedhcp;
  200. /** ProxyDHCP server */
  201. struct in_addr proxy_server;
  202. /** ProxyDHCP offer */
  203. struct dhcp_packet *proxy_offer;
  204. /** ProxyDHCP offer priority */
  205. int proxy_priority;
  206. /** PXE Boot Server type */
  207. uint16_t pxe_type;
  208. /** List of PXE Boot Servers to attempt */
  209. struct in_addr *pxe_attempt;
  210. /** List of PXE Boot Servers to accept */
  211. struct in_addr *pxe_accept;
  212. /** Retransmission timer */
  213. struct retry_timer timer;
  214. /** Transmission counter */
  215. unsigned int count;
  216. /** Start time of the current state (in ticks) */
  217. unsigned long start;
  218. };
  219. /**
  220. * Free DHCP session
  221. *
  222. * @v refcnt Reference counter
  223. */
  224. static void dhcp_free ( struct refcnt *refcnt ) {
  225. struct dhcp_session *dhcp =
  226. container_of ( refcnt, struct dhcp_session, refcnt );
  227. netdev_put ( dhcp->netdev );
  228. dhcppkt_put ( dhcp->proxy_offer );
  229. free ( dhcp );
  230. }
  231. /**
  232. * Mark DHCP session as complete
  233. *
  234. * @v dhcp DHCP session
  235. * @v rc Return status code
  236. */
  237. static void dhcp_finished ( struct dhcp_session *dhcp, int rc ) {
  238. /* Stop retry timer */
  239. stop_timer ( &dhcp->timer );
  240. /* Shut down interfaces */
  241. intf_shutdown ( &dhcp->xfer, rc );
  242. intf_shutdown ( &dhcp->job, rc );
  243. }
  244. /**
  245. * Transition to new DHCP session state
  246. *
  247. * @v dhcp DHCP session
  248. * @v state New session state
  249. */
  250. static void dhcp_set_state ( struct dhcp_session *dhcp,
  251. struct dhcp_session_state *state ) {
  252. DBGC ( dhcp, "DHCP %p entering %s state\n", dhcp, state->name );
  253. dhcp->state = state;
  254. dhcp->start = currticks();
  255. stop_timer ( &dhcp->timer );
  256. set_timer_limits ( &dhcp->timer,
  257. ( state->min_timeout_sec * TICKS_PER_SEC ),
  258. ( state->max_timeout_sec * TICKS_PER_SEC ) );
  259. start_timer_nodelay ( &dhcp->timer );
  260. }
  261. /**
  262. * Check if DHCP packet contains PXE options
  263. *
  264. * @v dhcppkt DHCP packet
  265. * @ret has_pxeopts DHCP packet contains PXE options
  266. *
  267. * It is assumed that the packet is already known to contain option 60
  268. * set to "PXEClient".
  269. */
  270. static int dhcp_has_pxeopts ( struct dhcp_packet *dhcppkt ) {
  271. /* Check for a next-server and boot filename */
  272. if ( dhcppkt->dhcphdr->siaddr.s_addr &&
  273. ( dhcppkt_fetch ( dhcppkt, DHCP_BOOTFILE_NAME, NULL, 0 ) > 0 ) )
  274. return 1;
  275. /* Check for a PXE boot menu */
  276. if ( dhcppkt_fetch ( dhcppkt, DHCP_PXE_BOOT_MENU, NULL, 0 ) > 0 )
  277. return 1;
  278. return 0;
  279. }
  280. /****************************************************************************
  281. *
  282. * DHCP state machine
  283. *
  284. */
  285. /**
  286. * Construct transmitted packet for DHCP discovery
  287. *
  288. * @v dhcp DHCP session
  289. * @v dhcppkt DHCP packet
  290. * @v peer Destination address
  291. */
  292. static int dhcp_discovery_tx ( struct dhcp_session *dhcp,
  293. struct dhcp_packet *dhcppkt __unused,
  294. struct sockaddr_in *peer ) {
  295. DBGC ( dhcp, "DHCP %p DHCPDISCOVER\n", dhcp );
  296. /* Set server address */
  297. peer->sin_addr.s_addr = INADDR_BROADCAST;
  298. peer->sin_port = htons ( BOOTPS_PORT );
  299. return 0;
  300. }
  301. /**
  302. * Handle received packet during DHCP discovery
  303. *
  304. * @v dhcp DHCP session
  305. * @v dhcppkt DHCP packet
  306. * @v peer DHCP server address
  307. * @v msgtype DHCP message type
  308. * @v server_id DHCP server ID
  309. * @v pseudo_id DHCP server pseudo-ID
  310. */
  311. static void dhcp_discovery_rx ( struct dhcp_session *dhcp,
  312. struct dhcp_packet *dhcppkt,
  313. struct sockaddr_in *peer, uint8_t msgtype,
  314. struct in_addr server_id,
  315. struct in_addr pseudo_id ) {
  316. struct in_addr ip;
  317. char vci[9]; /* "PXEClient" */
  318. int vci_len;
  319. int has_pxeclient;
  320. int8_t priority = 0;
  321. uint8_t no_pxedhcp = 0;
  322. unsigned long elapsed;
  323. DBGC ( dhcp, "DHCP %p %s from %s:%d", dhcp,
  324. dhcp_msgtype_name ( msgtype ), inet_ntoa ( peer->sin_addr ),
  325. ntohs ( peer->sin_port ) );
  326. if ( ( server_id.s_addr != peer->sin_addr.s_addr ) ||
  327. ( pseudo_id.s_addr != peer->sin_addr.s_addr ) ) {
  328. DBGC ( dhcp, " (%s/", inet_ntoa ( server_id ) );
  329. DBGC ( dhcp, "%s)", inet_ntoa ( pseudo_id ) );
  330. }
  331. /* Identify offered IP address */
  332. ip = dhcppkt->dhcphdr->yiaddr;
  333. if ( ip.s_addr )
  334. DBGC ( dhcp, " for %s", inet_ntoa ( ip ) );
  335. /* Identify "PXEClient" vendor class */
  336. vci_len = dhcppkt_fetch ( dhcppkt, DHCP_VENDOR_CLASS_ID,
  337. vci, sizeof ( vci ) );
  338. has_pxeclient = ( ( vci_len >= ( int ) sizeof ( vci ) ) &&
  339. ( strncmp ( "PXEClient", vci, sizeof (vci) ) == 0 ));
  340. if ( has_pxeclient ) {
  341. DBGC ( dhcp, "%s",
  342. ( dhcp_has_pxeopts ( dhcppkt ) ? " pxe" : " proxy" ) );
  343. }
  344. /* Identify priority */
  345. dhcppkt_fetch ( dhcppkt, DHCP_EB_PRIORITY, &priority,
  346. sizeof ( priority ) );
  347. if ( priority )
  348. DBGC ( dhcp, " pri %d", priority );
  349. /* Identify ignore-PXE flag */
  350. dhcppkt_fetch ( dhcppkt, DHCP_EB_NO_PXEDHCP, &no_pxedhcp,
  351. sizeof ( no_pxedhcp ) );
  352. if ( no_pxedhcp )
  353. DBGC ( dhcp, " nopxe" );
  354. DBGC ( dhcp, "\n" );
  355. /* Select as DHCP offer, if applicable */
  356. if ( ip.s_addr && ( peer->sin_port == htons ( BOOTPS_PORT ) ) &&
  357. ( ( msgtype == DHCPOFFER ) || ( ! msgtype /* BOOTP */ ) ) &&
  358. ( priority >= dhcp->priority ) ) {
  359. dhcp->offer = ip;
  360. dhcp->server = server_id;
  361. dhcp->priority = priority;
  362. dhcp->no_pxedhcp = no_pxedhcp;
  363. }
  364. /* Select as ProxyDHCP offer, if applicable */
  365. if ( pseudo_id.s_addr && has_pxeclient &&
  366. ( priority >= dhcp->proxy_priority ) ) {
  367. dhcppkt_put ( dhcp->proxy_offer );
  368. dhcp->proxy_server = pseudo_id;
  369. dhcp->proxy_offer = dhcppkt_get ( dhcppkt );
  370. dhcp->proxy_priority = priority;
  371. }
  372. /* We can exit the discovery state when we have a valid
  373. * DHCPOFFER, and either:
  374. *
  375. * o The DHCPOFFER instructs us to ignore ProxyDHCPOFFERs, or
  376. * o We have a valid ProxyDHCPOFFER, or
  377. * o We have allowed sufficient time for ProxyDHCPOFFERs.
  378. */
  379. /* If we don't yet have a DHCPOFFER, do nothing */
  380. if ( ! dhcp->offer.s_addr )
  381. return;
  382. /* If we can't yet transition to DHCPREQUEST, do nothing */
  383. elapsed = ( currticks() - dhcp->start );
  384. if ( ! ( dhcp->no_pxedhcp || dhcp->proxy_offer ||
  385. ( elapsed > DHCP_DISC_PROXY_TIMEOUT_SEC * TICKS_PER_SEC ) ) )
  386. return;
  387. /* Transition to DHCPREQUEST */
  388. dhcp_set_state ( dhcp, &dhcp_state_request );
  389. }
  390. /**
  391. * Handle timer expiry during DHCP discovery
  392. *
  393. * @v dhcp DHCP session
  394. */
  395. static void dhcp_discovery_expired ( struct dhcp_session *dhcp ) {
  396. unsigned long elapsed = ( currticks() - dhcp->start );
  397. /* If link is blocked, defer DHCP discovery (and reset timeout) */
  398. if ( netdev_link_blocked ( dhcp->netdev ) ) {
  399. DBGC ( dhcp, "DHCP %p deferring discovery\n", dhcp );
  400. dhcp->start = currticks();
  401. start_timer_fixed ( &dhcp->timer,
  402. ( DHCP_DISC_START_TIMEOUT_SEC *
  403. TICKS_PER_SEC ) );
  404. return;
  405. }
  406. /* Give up waiting for ProxyDHCP before we reach the failure point */
  407. if ( dhcp->offer.s_addr &&
  408. ( elapsed > DHCP_DISC_PROXY_TIMEOUT_SEC * TICKS_PER_SEC ) ) {
  409. dhcp_set_state ( dhcp, &dhcp_state_request );
  410. return;
  411. }
  412. /* Otherwise, retransmit current packet */
  413. dhcp_tx ( dhcp );
  414. }
  415. /** DHCP discovery state operations */
  416. static struct dhcp_session_state dhcp_state_discover = {
  417. .name = "discovery",
  418. .tx = dhcp_discovery_tx,
  419. .rx = dhcp_discovery_rx,
  420. .expired = dhcp_discovery_expired,
  421. .tx_msgtype = DHCPDISCOVER,
  422. .min_timeout_sec = DHCP_DISC_START_TIMEOUT_SEC,
  423. .max_timeout_sec = DHCP_DISC_END_TIMEOUT_SEC,
  424. };
  425. /**
  426. * Construct transmitted packet for DHCP request
  427. *
  428. * @v dhcp DHCP session
  429. * @v dhcppkt DHCP packet
  430. * @v peer Destination address
  431. */
  432. static int dhcp_request_tx ( struct dhcp_session *dhcp,
  433. struct dhcp_packet *dhcppkt,
  434. struct sockaddr_in *peer ) {
  435. int rc;
  436. DBGC ( dhcp, "DHCP %p DHCPREQUEST to %s:%d",
  437. dhcp, inet_ntoa ( dhcp->server ), BOOTPS_PORT );
  438. DBGC ( dhcp, " for %s\n", inet_ntoa ( dhcp->offer ) );
  439. /* Set server ID */
  440. if ( ( rc = dhcppkt_store ( dhcppkt, DHCP_SERVER_IDENTIFIER,
  441. &dhcp->server,
  442. sizeof ( dhcp->server ) ) ) != 0 )
  443. return rc;
  444. /* Set requested IP address */
  445. if ( ( rc = dhcppkt_store ( dhcppkt, DHCP_REQUESTED_ADDRESS,
  446. &dhcp->offer,
  447. sizeof ( dhcp->offer ) ) ) != 0 )
  448. return rc;
  449. /* Set server address */
  450. peer->sin_addr.s_addr = INADDR_BROADCAST;
  451. peer->sin_port = htons ( BOOTPS_PORT );
  452. return 0;
  453. }
  454. /**
  455. * Handle received packet during DHCP request
  456. *
  457. * @v dhcp DHCP session
  458. * @v dhcppkt DHCP packet
  459. * @v peer DHCP server address
  460. * @v msgtype DHCP message type
  461. * @v server_id DHCP server ID
  462. * @v pseudo_id DHCP server pseudo-ID
  463. */
  464. static void dhcp_request_rx ( struct dhcp_session *dhcp,
  465. struct dhcp_packet *dhcppkt,
  466. struct sockaddr_in *peer, uint8_t msgtype,
  467. struct in_addr server_id,
  468. struct in_addr pseudo_id ) {
  469. struct in_addr ip;
  470. struct settings *parent;
  471. struct settings *settings;
  472. int rc;
  473. DBGC ( dhcp, "DHCP %p %s from %s:%d", dhcp,
  474. dhcp_msgtype_name ( msgtype ), inet_ntoa ( peer->sin_addr ),
  475. ntohs ( peer->sin_port ) );
  476. if ( ( server_id.s_addr != peer->sin_addr.s_addr ) ||
  477. ( pseudo_id.s_addr != peer->sin_addr.s_addr ) ) {
  478. DBGC ( dhcp, " (%s/", inet_ntoa ( server_id ) );
  479. DBGC ( dhcp, "%s)", inet_ntoa ( pseudo_id ) );
  480. }
  481. /* Identify leased IP address */
  482. ip = dhcppkt->dhcphdr->yiaddr;
  483. if ( ip.s_addr )
  484. DBGC ( dhcp, " for %s", inet_ntoa ( ip ) );
  485. DBGC ( dhcp, "\n" );
  486. /* Filter out unacceptable responses */
  487. if ( peer->sin_port != htons ( BOOTPS_PORT ) )
  488. return;
  489. if ( msgtype /* BOOTP */ && ( msgtype != DHCPACK ) )
  490. return;
  491. if ( server_id.s_addr != dhcp->server.s_addr )
  492. return;
  493. if ( ip.s_addr != dhcp->offer.s_addr )
  494. return;
  495. /* Record assigned address */
  496. dhcp->local.sin_addr = ip;
  497. /* Register settings */
  498. parent = netdev_settings ( dhcp->netdev );
  499. settings = &dhcppkt->settings;
  500. if ( ( rc = register_settings ( settings, parent,
  501. DHCP_SETTINGS_NAME ) ) != 0 ) {
  502. DBGC ( dhcp, "DHCP %p could not register settings: %s\n",
  503. dhcp, strerror ( rc ) );
  504. dhcp_finished ( dhcp, rc );
  505. return;
  506. }
  507. /* Perform ProxyDHCP if applicable */
  508. if ( dhcp->proxy_offer /* Have ProxyDHCP offer */ &&
  509. ( ! dhcp->no_pxedhcp ) /* ProxyDHCP not disabled */ ) {
  510. if ( dhcp_has_pxeopts ( dhcp->proxy_offer ) ) {
  511. /* PXE options already present; register settings
  512. * without performing a ProxyDHCPREQUEST
  513. */
  514. settings = &dhcp->proxy_offer->settings;
  515. if ( ( rc = register_settings ( settings, NULL,
  516. PROXYDHCP_SETTINGS_NAME ) ) != 0 ) {
  517. DBGC ( dhcp, "DHCP %p could not register "
  518. "proxy settings: %s\n",
  519. dhcp, strerror ( rc ) );
  520. dhcp_finished ( dhcp, rc );
  521. return;
  522. }
  523. } else {
  524. /* PXE options not present; use a ProxyDHCPREQUEST */
  525. dhcp_set_state ( dhcp, &dhcp_state_proxy );
  526. return;
  527. }
  528. }
  529. /* Terminate DHCP */
  530. dhcp_finished ( dhcp, 0 );
  531. }
  532. /**
  533. * Handle timer expiry during DHCP discovery
  534. *
  535. * @v dhcp DHCP session
  536. */
  537. static void dhcp_request_expired ( struct dhcp_session *dhcp ) {
  538. /* Retransmit current packet */
  539. dhcp_tx ( dhcp );
  540. }
  541. /** DHCP request state operations */
  542. static struct dhcp_session_state dhcp_state_request = {
  543. .name = "request",
  544. .tx = dhcp_request_tx,
  545. .rx = dhcp_request_rx,
  546. .expired = dhcp_request_expired,
  547. .tx_msgtype = DHCPREQUEST,
  548. .min_timeout_sec = DHCP_REQ_START_TIMEOUT_SEC,
  549. .max_timeout_sec = DHCP_REQ_END_TIMEOUT_SEC,
  550. };
  551. /**
  552. * Construct transmitted packet for ProxyDHCP request
  553. *
  554. * @v dhcp DHCP session
  555. * @v dhcppkt DHCP packet
  556. * @v peer Destination address
  557. */
  558. static int dhcp_proxy_tx ( struct dhcp_session *dhcp,
  559. struct dhcp_packet *dhcppkt,
  560. struct sockaddr_in *peer ) {
  561. int rc;
  562. DBGC ( dhcp, "DHCP %p ProxyDHCP REQUEST to %s\n", dhcp,
  563. inet_ntoa ( dhcp->proxy_server ) );
  564. /* Set server ID */
  565. if ( ( rc = dhcppkt_store ( dhcppkt, DHCP_SERVER_IDENTIFIER,
  566. &dhcp->proxy_server,
  567. sizeof ( dhcp->proxy_server ) ) ) != 0 )
  568. return rc;
  569. /* Set server address */
  570. peer->sin_addr = dhcp->proxy_server;
  571. peer->sin_port = htons ( PXE_PORT );
  572. return 0;
  573. }
  574. /**
  575. * Handle received packet during ProxyDHCP request
  576. *
  577. * @v dhcp DHCP session
  578. * @v dhcppkt DHCP packet
  579. * @v peer DHCP server address
  580. * @v msgtype DHCP message type
  581. * @v server_id DHCP server ID
  582. * @v pseudo_id DHCP server pseudo-ID
  583. */
  584. static void dhcp_proxy_rx ( struct dhcp_session *dhcp,
  585. struct dhcp_packet *dhcppkt,
  586. struct sockaddr_in *peer, uint8_t msgtype,
  587. struct in_addr server_id,
  588. struct in_addr pseudo_id ) {
  589. struct settings *settings = &dhcppkt->settings;
  590. int rc;
  591. DBGC ( dhcp, "DHCP %p %s from %s:%d", dhcp,
  592. dhcp_msgtype_name ( msgtype ), inet_ntoa ( peer->sin_addr ),
  593. ntohs ( peer->sin_port ) );
  594. if ( ( server_id.s_addr != peer->sin_addr.s_addr ) ||
  595. ( pseudo_id.s_addr != peer->sin_addr.s_addr ) ) {
  596. DBGC ( dhcp, " (%s/", inet_ntoa ( server_id ) );
  597. DBGC ( dhcp, "%s)", inet_ntoa ( pseudo_id ) );
  598. }
  599. if ( dhcp_has_pxeopts ( dhcppkt ) )
  600. DBGC ( dhcp, " pxe" );
  601. DBGC ( dhcp, "\n" );
  602. /* Filter out unacceptable responses */
  603. if ( peer->sin_port != ntohs ( PXE_PORT ) )
  604. return;
  605. if ( ( msgtype != DHCPOFFER ) && ( msgtype != DHCPACK ) )
  606. return;
  607. if ( ( pseudo_id.s_addr != dhcp->proxy_server.s_addr ) )
  608. return;
  609. if ( ! dhcp_has_pxeopts ( dhcppkt ) )
  610. return;
  611. /* Register settings */
  612. if ( ( rc = register_settings ( settings, NULL,
  613. PROXYDHCP_SETTINGS_NAME ) ) != 0 ) {
  614. DBGC ( dhcp, "DHCP %p could not register proxy settings: %s\n",
  615. dhcp, strerror ( rc ) );
  616. dhcp_finished ( dhcp, rc );
  617. return;
  618. }
  619. /* Terminate DHCP */
  620. dhcp_finished ( dhcp, 0 );
  621. }
  622. /**
  623. * Handle timer expiry during ProxyDHCP request
  624. *
  625. * @v dhcp DHCP session
  626. */
  627. static void dhcp_proxy_expired ( struct dhcp_session *dhcp ) {
  628. unsigned long elapsed = ( currticks() - dhcp->start );
  629. /* Give up waiting for ProxyDHCP before we reach the failure point */
  630. if ( elapsed > DHCP_REQ_PROXY_TIMEOUT_SEC * TICKS_PER_SEC ) {
  631. dhcp_finished ( dhcp, 0 );
  632. return;
  633. }
  634. /* Retransmit current packet */
  635. dhcp_tx ( dhcp );
  636. }
  637. /** ProxyDHCP request state operations */
  638. static struct dhcp_session_state dhcp_state_proxy = {
  639. .name = "ProxyDHCP",
  640. .tx = dhcp_proxy_tx,
  641. .rx = dhcp_proxy_rx,
  642. .expired = dhcp_proxy_expired,
  643. .tx_msgtype = DHCPREQUEST,
  644. .min_timeout_sec = DHCP_PROXY_START_TIMEOUT_SEC,
  645. .max_timeout_sec = DHCP_PROXY_END_TIMEOUT_SEC,
  646. };
  647. /**
  648. * Construct transmitted packet for PXE Boot Server Discovery
  649. *
  650. * @v dhcp DHCP session
  651. * @v dhcppkt DHCP packet
  652. * @v peer Destination address
  653. */
  654. static int dhcp_pxebs_tx ( struct dhcp_session *dhcp,
  655. struct dhcp_packet *dhcppkt,
  656. struct sockaddr_in *peer ) {
  657. struct dhcp_pxe_boot_menu_item menu_item = { 0, 0 };
  658. int rc;
  659. /* Set server address */
  660. peer->sin_addr = *(dhcp->pxe_attempt);
  661. peer->sin_port = ( ( peer->sin_addr.s_addr == INADDR_BROADCAST ) ?
  662. htons ( BOOTPS_PORT ) : htons ( PXE_PORT ) );
  663. DBGC ( dhcp, "DHCP %p PXEBS REQUEST to %s:%d for type %d\n",
  664. dhcp, inet_ntoa ( peer->sin_addr ), ntohs ( peer->sin_port ),
  665. le16_to_cpu ( dhcp->pxe_type ) );
  666. /* Set boot menu item */
  667. menu_item.type = dhcp->pxe_type;
  668. if ( ( rc = dhcppkt_store ( dhcppkt, DHCP_PXE_BOOT_MENU_ITEM,
  669. &menu_item, sizeof ( menu_item ) ) ) != 0 )
  670. return rc;
  671. return 0;
  672. }
  673. /**
  674. * Check to see if PXE Boot Server address is acceptable
  675. *
  676. * @v dhcp DHCP session
  677. * @v bs Boot Server address
  678. * @ret accept Boot Server is acceptable
  679. */
  680. static int dhcp_pxebs_accept ( struct dhcp_session *dhcp,
  681. struct in_addr bs ) {
  682. struct in_addr *accept;
  683. /* Accept if we have no acceptance filter */
  684. if ( ! dhcp->pxe_accept )
  685. return 1;
  686. /* Scan through acceptance list */
  687. for ( accept = dhcp->pxe_accept ; accept->s_addr ; accept++ ) {
  688. if ( accept->s_addr == bs.s_addr )
  689. return 1;
  690. }
  691. DBGC ( dhcp, "DHCP %p rejecting server %s\n",
  692. dhcp, inet_ntoa ( bs ) );
  693. return 0;
  694. }
  695. /**
  696. * Handle received packet during PXE Boot Server Discovery
  697. *
  698. * @v dhcp DHCP session
  699. * @v dhcppkt DHCP packet
  700. * @v peer DHCP server address
  701. * @v msgtype DHCP message type
  702. * @v server_id DHCP server ID
  703. * @v pseudo_id DHCP server pseudo-ID
  704. */
  705. static void dhcp_pxebs_rx ( struct dhcp_session *dhcp,
  706. struct dhcp_packet *dhcppkt,
  707. struct sockaddr_in *peer, uint8_t msgtype,
  708. struct in_addr server_id,
  709. struct in_addr pseudo_id ) {
  710. struct dhcp_pxe_boot_menu_item menu_item = { 0, 0 };
  711. int rc;
  712. DBGC ( dhcp, "DHCP %p %s from %s:%d", dhcp,
  713. dhcp_msgtype_name ( msgtype ), inet_ntoa ( peer->sin_addr ),
  714. ntohs ( peer->sin_port ) );
  715. if ( ( server_id.s_addr != peer->sin_addr.s_addr ) ||
  716. ( pseudo_id.s_addr != peer->sin_addr.s_addr ) ) {
  717. DBGC ( dhcp, " (%s/", inet_ntoa ( server_id ) );
  718. DBGC ( dhcp, "%s)", inet_ntoa ( pseudo_id ) );
  719. }
  720. /* Identify boot menu item */
  721. dhcppkt_fetch ( dhcppkt, DHCP_PXE_BOOT_MENU_ITEM,
  722. &menu_item, sizeof ( menu_item ) );
  723. if ( menu_item.type )
  724. DBGC ( dhcp, " for type %d", ntohs ( menu_item.type ) );
  725. DBGC ( dhcp, "\n" );
  726. /* Filter out unacceptable responses */
  727. if ( ( peer->sin_port != htons ( BOOTPS_PORT ) ) &&
  728. ( peer->sin_port != htons ( PXE_PORT ) ) )
  729. return;
  730. if ( msgtype != DHCPACK )
  731. return;
  732. if ( menu_item.type != dhcp->pxe_type )
  733. return;
  734. if ( ! dhcp_pxebs_accept ( dhcp, pseudo_id ) )
  735. return;
  736. /* Register settings */
  737. if ( ( rc = register_settings ( &dhcppkt->settings, NULL,
  738. PXEBS_SETTINGS_NAME ) ) != 0 ) {
  739. DBGC ( dhcp, "DHCP %p could not register settings: %s\n",
  740. dhcp, strerror ( rc ) );
  741. dhcp_finished ( dhcp, rc );
  742. return;
  743. }
  744. /* Terminate DHCP */
  745. dhcp_finished ( dhcp, 0 );
  746. }
  747. /**
  748. * Handle timer expiry during PXE Boot Server Discovery
  749. *
  750. * @v dhcp DHCP session
  751. */
  752. static void dhcp_pxebs_expired ( struct dhcp_session *dhcp ) {
  753. unsigned long elapsed = ( currticks() - dhcp->start );
  754. /* Give up waiting before we reach the failure point, and fail
  755. * over to the next server in the attempt list
  756. */
  757. if ( elapsed > PXEBS_MAX_TIMEOUT_SEC * TICKS_PER_SEC ) {
  758. dhcp->pxe_attempt++;
  759. if ( dhcp->pxe_attempt->s_addr ) {
  760. dhcp_set_state ( dhcp, &dhcp_state_pxebs );
  761. return;
  762. } else {
  763. dhcp_finished ( dhcp, -ETIMEDOUT );
  764. return;
  765. }
  766. }
  767. /* Retransmit current packet */
  768. dhcp_tx ( dhcp );
  769. }
  770. /** PXE Boot Server Discovery state operations */
  771. static struct dhcp_session_state dhcp_state_pxebs = {
  772. .name = "PXEBS",
  773. .tx = dhcp_pxebs_tx,
  774. .rx = dhcp_pxebs_rx,
  775. .expired = dhcp_pxebs_expired,
  776. .tx_msgtype = DHCPREQUEST,
  777. .min_timeout_sec = PXEBS_START_TIMEOUT_SEC,
  778. .max_timeout_sec = PXEBS_END_TIMEOUT_SEC,
  779. };
  780. /****************************************************************************
  781. *
  782. * Packet construction
  783. *
  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 net_device *netdev = dhcp->netdev;
  1025. struct ll_protocol *ll_protocol = netdev->ll_protocol;
  1026. struct sockaddr_in *peer;
  1027. size_t data_len;
  1028. struct dhcp_packet *dhcppkt;
  1029. struct dhcphdr *dhcphdr;
  1030. uint8_t msgtype = 0;
  1031. struct in_addr server_id = { 0 };
  1032. struct in_addr pseudo_id;
  1033. int rc = 0;
  1034. /* Sanity checks */
  1035. if ( ! meta->src ) {
  1036. DBGC ( dhcp, "DHCP %p received packet without source port\n",
  1037. dhcp );
  1038. rc = -EINVAL;
  1039. goto err_no_src;
  1040. }
  1041. peer = ( struct sockaddr_in * ) meta->src;
  1042. /* Create a DHCP packet containing the I/O buffer contents.
  1043. * Whilst we could just use the original buffer in situ, that
  1044. * would waste the unused space in the packet buffer, and also
  1045. * waste a relatively scarce fully-aligned I/O buffer.
  1046. */
  1047. data_len = iob_len ( iobuf );
  1048. dhcppkt = zalloc ( sizeof ( *dhcppkt ) + data_len );
  1049. if ( ! dhcppkt ) {
  1050. rc = -ENOMEM;
  1051. goto err_alloc_dhcppkt;
  1052. }
  1053. dhcphdr = ( ( ( void * ) dhcppkt ) + sizeof ( *dhcppkt ) );
  1054. memcpy ( dhcphdr, iobuf->data, data_len );
  1055. dhcppkt_init ( dhcppkt, dhcphdr, data_len );
  1056. /* Identify message type */
  1057. dhcppkt_fetch ( dhcppkt, DHCP_MESSAGE_TYPE, &msgtype,
  1058. sizeof ( msgtype ) );
  1059. /* Identify server ID */
  1060. dhcppkt_fetch ( dhcppkt, DHCP_SERVER_IDENTIFIER,
  1061. &server_id, sizeof ( server_id ) );
  1062. /* Identify server pseudo-ID */
  1063. pseudo_id = server_id;
  1064. if ( ! pseudo_id.s_addr )
  1065. pseudo_id = dhcppkt->dhcphdr->siaddr;
  1066. if ( ! pseudo_id.s_addr )
  1067. pseudo_id = peer->sin_addr;
  1068. /* Check for matching transaction ID */
  1069. if ( dhcphdr->xid != dhcp->xid ) {
  1070. DBGC ( dhcp, "DHCP %p %s from %s:%d has bad transaction "
  1071. "ID\n", dhcp, dhcp_msgtype_name ( msgtype ),
  1072. inet_ntoa ( peer->sin_addr ),
  1073. ntohs ( peer->sin_port ) );
  1074. rc = -EINVAL;
  1075. goto err_xid;
  1076. };
  1077. /* Check for matching client hardware address */
  1078. if ( memcmp ( dhcphdr->chaddr, netdev->ll_addr,
  1079. ll_protocol->ll_addr_len ) != 0 ) {
  1080. DBGC ( dhcp, "DHCP %p %s from %s:%d has bad chaddr %s\n",
  1081. dhcp, dhcp_msgtype_name ( msgtype ),
  1082. inet_ntoa ( peer->sin_addr ), ntohs ( peer->sin_port ),
  1083. ll_protocol->ntoa ( dhcphdr->chaddr ) );
  1084. rc = -EINVAL;
  1085. goto err_chaddr;
  1086. }
  1087. /* Handle packet based on current state */
  1088. dhcp->state->rx ( dhcp, dhcppkt, peer, msgtype, server_id, pseudo_id );
  1089. err_chaddr:
  1090. err_xid:
  1091. dhcppkt_put ( dhcppkt );
  1092. err_alloc_dhcppkt:
  1093. err_no_src:
  1094. free_iob ( iobuf );
  1095. return rc;
  1096. }
  1097. /** DHCP data transfer interface operations */
  1098. static struct interface_operation dhcp_xfer_operations[] = {
  1099. INTF_OP ( xfer_deliver, struct dhcp_session *, dhcp_deliver ),
  1100. };
  1101. /** DHCP data transfer interface descriptor */
  1102. static struct interface_descriptor dhcp_xfer_desc =
  1103. INTF_DESC ( struct dhcp_session, xfer, dhcp_xfer_operations );
  1104. /**
  1105. * Handle DHCP retry timer expiry
  1106. *
  1107. * @v timer DHCP retry timer
  1108. * @v fail Failure indicator
  1109. */
  1110. static void dhcp_timer_expired ( struct retry_timer *timer, int fail ) {
  1111. struct dhcp_session *dhcp =
  1112. container_of ( timer, struct dhcp_session, timer );
  1113. /* If we have failed, terminate DHCP */
  1114. if ( fail ) {
  1115. dhcp_finished ( dhcp, -ETIMEDOUT );
  1116. return;
  1117. }
  1118. /* Handle timer expiry based on current state */
  1119. dhcp->state->expired ( dhcp );
  1120. }
  1121. /****************************************************************************
  1122. *
  1123. * Job control interface
  1124. *
  1125. */
  1126. /** DHCP job control interface operations */
  1127. static struct interface_operation dhcp_job_op[] = {
  1128. INTF_OP ( intf_close, struct dhcp_session *, dhcp_finished ),
  1129. };
  1130. /** DHCP job control interface descriptor */
  1131. static struct interface_descriptor dhcp_job_desc =
  1132. INTF_DESC ( struct dhcp_session, job, dhcp_job_op );
  1133. /****************************************************************************
  1134. *
  1135. * Instantiators
  1136. *
  1137. */
  1138. /**
  1139. * DHCP peer address for socket opening
  1140. *
  1141. * This is a dummy address; the only useful portion is the socket
  1142. * family (so that we get a UDP connection). The DHCP client will set
  1143. * the IP address and source port explicitly on each transmission.
  1144. */
  1145. static struct sockaddr dhcp_peer = {
  1146. .sa_family = AF_INET,
  1147. };
  1148. /**
  1149. * Start DHCP state machine on a network device
  1150. *
  1151. * @v job Job control interface
  1152. * @v netdev Network device
  1153. * @ret rc Return status code
  1154. *
  1155. * Starts DHCP on the specified network device. If successful, the
  1156. * DHCPACK (and ProxyDHCPACK, if applicable) will be registered as
  1157. * option sources.
  1158. */
  1159. int start_dhcp ( struct interface *job, struct net_device *netdev ) {
  1160. struct dhcp_session *dhcp;
  1161. int rc;
  1162. /* Allocate and initialise structure */
  1163. dhcp = zalloc ( sizeof ( *dhcp ) );
  1164. if ( ! dhcp )
  1165. return -ENOMEM;
  1166. ref_init ( &dhcp->refcnt, dhcp_free );
  1167. intf_init ( &dhcp->job, &dhcp_job_desc, &dhcp->refcnt );
  1168. intf_init ( &dhcp->xfer, &dhcp_xfer_desc, &dhcp->refcnt );
  1169. timer_init ( &dhcp->timer, dhcp_timer_expired, &dhcp->refcnt );
  1170. dhcp->netdev = netdev_get ( netdev );
  1171. dhcp->local.sin_family = AF_INET;
  1172. dhcp->local.sin_port = htons ( BOOTPC_PORT );
  1173. dhcp->xid = random();
  1174. /* Store DHCP transaction ID for fakedhcp code */
  1175. dhcp_last_xid = dhcp->xid;
  1176. /* Instantiate child objects and attach to our interfaces */
  1177. if ( ( rc = xfer_open_socket ( &dhcp->xfer, SOCK_DGRAM, &dhcp_peer,
  1178. ( struct sockaddr * ) &dhcp->local ) ) != 0 )
  1179. goto err;
  1180. /* Enter DHCPDISCOVER state */
  1181. dhcp_set_state ( dhcp, &dhcp_state_discover );
  1182. /* Attach parent interface, mortalise self, and return */
  1183. intf_plug_plug ( &dhcp->job, job );
  1184. ref_put ( &dhcp->refcnt );
  1185. return 0;
  1186. err:
  1187. dhcp_finished ( dhcp, rc );
  1188. ref_put ( &dhcp->refcnt );
  1189. return rc;
  1190. }
  1191. /**
  1192. * Retrieve list of PXE boot servers for a given server type
  1193. *
  1194. * @v dhcp DHCP session
  1195. * @v raw DHCP PXE boot server list
  1196. * @v raw_len Length of DHCP PXE boot server list
  1197. * @v ip IP address list to fill in
  1198. *
  1199. * The caller must ensure that the IP address list has sufficient
  1200. * space.
  1201. */
  1202. static void pxebs_list ( struct dhcp_session *dhcp, void *raw,
  1203. size_t raw_len, struct in_addr *ip ) {
  1204. struct dhcp_pxe_boot_server *server = raw;
  1205. size_t server_len;
  1206. unsigned int i;
  1207. while ( raw_len ) {
  1208. if ( raw_len < sizeof ( *server ) ) {
  1209. DBGC ( dhcp, "DHCP %p malformed PXE server list\n",
  1210. dhcp );
  1211. break;
  1212. }
  1213. server_len = offsetof ( typeof ( *server ),
  1214. ip[ server->num_ip ] );
  1215. if ( raw_len < server_len ) {
  1216. DBGC ( dhcp, "DHCP %p malformed PXE server list\n",
  1217. dhcp );
  1218. break;
  1219. }
  1220. if ( server->type == dhcp->pxe_type ) {
  1221. for ( i = 0 ; i < server->num_ip ; i++ )
  1222. *(ip++) = server->ip[i];
  1223. }
  1224. server = ( ( ( void * ) server ) + server_len );
  1225. raw_len -= server_len;
  1226. }
  1227. }
  1228. /**
  1229. * Start PXE Boot Server Discovery on a network device
  1230. *
  1231. * @v job Job control interface
  1232. * @v netdev Network device
  1233. * @v pxe_type PXE server type
  1234. * @ret rc Return status code
  1235. *
  1236. * Starts PXE Boot Server Discovery on the specified network device.
  1237. * If successful, the Boot Server ACK will be registered as an option
  1238. * source.
  1239. */
  1240. int start_pxebs ( struct interface *job, struct net_device *netdev,
  1241. unsigned int pxe_type ) {
  1242. struct setting pxe_discovery_control_setting =
  1243. { .tag = DHCP_PXE_DISCOVERY_CONTROL };
  1244. struct setting pxe_boot_servers_setting =
  1245. { .tag = DHCP_PXE_BOOT_SERVERS };
  1246. struct setting pxe_boot_server_mcast_setting =
  1247. { .tag = DHCP_PXE_BOOT_SERVER_MCAST };
  1248. ssize_t pxebs_list_len;
  1249. struct dhcp_session *dhcp;
  1250. struct in_addr *ip;
  1251. unsigned int pxe_discovery_control;
  1252. int rc;
  1253. /* Get upper bound for PXE boot server IP address list */
  1254. pxebs_list_len = fetch_raw_setting ( NULL, &pxe_boot_servers_setting,
  1255. NULL, 0 );
  1256. if ( pxebs_list_len < 0 )
  1257. pxebs_list_len = 0;
  1258. /* Allocate and initialise structure */
  1259. dhcp = zalloc ( sizeof ( *dhcp ) + sizeof ( *ip ) /* mcast */ +
  1260. sizeof ( *ip ) /* bcast */ + pxebs_list_len +
  1261. sizeof ( *ip ) /* terminator */ );
  1262. if ( ! dhcp )
  1263. return -ENOMEM;
  1264. ref_init ( &dhcp->refcnt, dhcp_free );
  1265. intf_init ( &dhcp->job, &dhcp_job_desc, &dhcp->refcnt );
  1266. intf_init ( &dhcp->xfer, &dhcp_xfer_desc, &dhcp->refcnt );
  1267. timer_init ( &dhcp->timer, dhcp_timer_expired, &dhcp->refcnt );
  1268. dhcp->netdev = netdev_get ( netdev );
  1269. dhcp->local.sin_family = AF_INET;
  1270. fetch_ipv4_setting ( netdev_settings ( netdev ), &ip_setting,
  1271. &dhcp->local.sin_addr );
  1272. dhcp->local.sin_port = htons ( BOOTPC_PORT );
  1273. dhcp->pxe_type = cpu_to_le16 ( pxe_type );
  1274. /* Construct PXE boot server IP address lists */
  1275. pxe_discovery_control =
  1276. fetch_uintz_setting ( NULL, &pxe_discovery_control_setting );
  1277. ip = ( ( ( void * ) dhcp ) + sizeof ( *dhcp ) );
  1278. dhcp->pxe_attempt = ip;
  1279. if ( ! ( pxe_discovery_control & PXEBS_NO_MULTICAST ) ) {
  1280. fetch_ipv4_setting ( NULL, &pxe_boot_server_mcast_setting, ip);
  1281. if ( ip->s_addr )
  1282. ip++;
  1283. }
  1284. if ( ! ( pxe_discovery_control & PXEBS_NO_BROADCAST ) )
  1285. (ip++)->s_addr = INADDR_BROADCAST;
  1286. if ( pxe_discovery_control & PXEBS_NO_UNKNOWN_SERVERS )
  1287. dhcp->pxe_accept = ip;
  1288. if ( pxebs_list_len ) {
  1289. uint8_t buf[pxebs_list_len];
  1290. fetch_raw_setting ( NULL, &pxe_boot_servers_setting,
  1291. buf, sizeof ( buf ) );
  1292. pxebs_list ( dhcp, buf, sizeof ( buf ), ip );
  1293. }
  1294. if ( ! dhcp->pxe_attempt->s_addr ) {
  1295. DBGC ( dhcp, "DHCP %p has no PXE boot servers for type %04x\n",
  1296. dhcp, pxe_type );
  1297. rc = -EINVAL;
  1298. goto err;
  1299. }
  1300. /* Dump out PXE server lists */
  1301. DBGC ( dhcp, "DHCP %p attempting", dhcp );
  1302. for ( ip = dhcp->pxe_attempt ; ip->s_addr ; ip++ )
  1303. DBGC ( dhcp, " %s", inet_ntoa ( *ip ) );
  1304. DBGC ( dhcp, "\n" );
  1305. if ( dhcp->pxe_accept ) {
  1306. DBGC ( dhcp, "DHCP %p accepting", dhcp );
  1307. for ( ip = dhcp->pxe_accept ; ip->s_addr ; ip++ )
  1308. DBGC ( dhcp, " %s", inet_ntoa ( *ip ) );
  1309. DBGC ( dhcp, "\n" );
  1310. }
  1311. /* Instantiate child objects and attach to our interfaces */
  1312. if ( ( rc = xfer_open_socket ( &dhcp->xfer, SOCK_DGRAM, &dhcp_peer,
  1313. ( struct sockaddr * ) &dhcp->local ) ) != 0 )
  1314. goto err;
  1315. /* Enter PXEBS state */
  1316. dhcp_set_state ( dhcp, &dhcp_state_pxebs );
  1317. /* Attach parent interface, mortalise self, and return */
  1318. intf_plug_plug ( &dhcp->job, job );
  1319. ref_put ( &dhcp->refcnt );
  1320. return 0;
  1321. err:
  1322. dhcp_finished ( dhcp, rc );
  1323. ref_put ( &dhcp->refcnt );
  1324. return rc;
  1325. }
  1326. /** DHCP network device configurator */
  1327. struct net_device_configurator dhcp_configurator __net_device_configurator = {
  1328. .name = "dhcp",
  1329. .start = start_dhcp,
  1330. };