You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

dhcp.c 42KB

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