Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

dhcp.c 42KB

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