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

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