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

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