選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

dhcp.c 40KB

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