Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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