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 44KB

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