Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

dhcp.c 40KB

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