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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069
  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. #include <string.h>
  19. #include <stdlib.h>
  20. #include <stdio.h>
  21. #include <errno.h>
  22. #include <assert.h>
  23. #include <byteswap.h>
  24. #include <gpxe/if_ether.h>
  25. #include <gpxe/netdevice.h>
  26. #include <gpxe/device.h>
  27. #include <gpxe/xfer.h>
  28. #include <gpxe/open.h>
  29. #include <gpxe/job.h>
  30. #include <gpxe/retry.h>
  31. #include <gpxe/tcpip.h>
  32. #include <gpxe/ip.h>
  33. #include <gpxe/uuid.h>
  34. #include <gpxe/dhcp.h>
  35. /** @file
  36. *
  37. * Dynamic Host Configuration Protocol
  38. *
  39. */
  40. /** DHCP operation types
  41. *
  42. * This table maps from DHCP message types (i.e. values of the @c
  43. * DHCP_MESSAGE_TYPE option) to values of the "op" field within a DHCP
  44. * packet.
  45. */
  46. static const uint8_t dhcp_op[] = {
  47. [DHCPDISCOVER] = BOOTP_REQUEST,
  48. [DHCPOFFER] = BOOTP_REPLY,
  49. [DHCPREQUEST] = BOOTP_REQUEST,
  50. [DHCPDECLINE] = BOOTP_REQUEST,
  51. [DHCPACK] = BOOTP_REPLY,
  52. [DHCPNAK] = BOOTP_REPLY,
  53. [DHCPRELEASE] = BOOTP_REQUEST,
  54. [DHCPINFORM] = BOOTP_REQUEST,
  55. };
  56. /** Raw option data for options common to all DHCP requests */
  57. static uint8_t dhcp_request_options_data[] = {
  58. DHCP_MAX_MESSAGE_SIZE, DHCP_WORD ( ETH_MAX_MTU ),
  59. DHCP_VENDOR_CLASS_ID,
  60. DHCP_STRING ( 'P', 'X', 'E', 'C', 'l', 'i', 'e', 'n', 't', ':',
  61. 'A', 'r', 'c', 'h', ':', '0', '0', '0', '0', '0', ':',
  62. 'U', 'N', 'D', 'I', ':', '0', '0', '2', '0', '0', '1' ),
  63. DHCP_CLIENT_ARCHITECTURE, DHCP_WORD ( 0 ),
  64. DHCP_CLIENT_NDI, DHCP_OPTION ( 1 /* UNDI */ , 2, 1 /* v2.1 */ ),
  65. DHCP_PARAMETER_REQUEST_LIST,
  66. DHCP_OPTION ( DHCP_SUBNET_MASK, DHCP_ROUTERS, DHCP_DNS_SERVERS,
  67. DHCP_LOG_SERVERS, DHCP_HOST_NAME, DHCP_DOMAIN_NAME,
  68. DHCP_ROOT_PATH, DHCP_VENDOR_ENCAP, DHCP_VENDOR_CLASS_ID,
  69. DHCP_TFTP_SERVER_NAME, DHCP_BOOTFILE_NAME,
  70. DHCP_EB_ENCAP, DHCP_ISCSI_INITIATOR_IQN ),
  71. DHCP_END
  72. };
  73. /** DHCP feature codes */
  74. static uint8_t dhcp_features[0] __table_start ( uint8_t, dhcp_features );
  75. static uint8_t dhcp_features_end[0] __table_end ( uint8_t, dhcp_features );
  76. /**
  77. * Name a DHCP packet type
  78. *
  79. * @v msgtype DHCP message type
  80. * @ret string DHCP mesasge type name
  81. */
  82. static inline const char * dhcp_msgtype_name ( unsigned int msgtype ) {
  83. switch ( msgtype ) {
  84. case 0: return "BOOTP"; /* Non-DHCP packet */
  85. case DHCPDISCOVER: return "DHCPDISCOVER";
  86. case DHCPOFFER: return "DHCPOFFER";
  87. case DHCPREQUEST: return "DHCPREQUEST";
  88. case DHCPDECLINE: return "DHCPDECLINE";
  89. case DHCPACK: return "DHCPACK";
  90. case DHCPNAK: return "DHCPNAK";
  91. case DHCPRELEASE: return "DHCPRELEASE";
  92. case DHCPINFORM: return "DHCPINFORM";
  93. default: return "DHCP<invalid>";
  94. }
  95. }
  96. /**
  97. * Calculate DHCP transaction ID for a network device
  98. *
  99. * @v netdev Network device
  100. * @ret xid DHCP XID
  101. *
  102. * Extract the least significant bits of the hardware address for use
  103. * as the transaction ID.
  104. */
  105. static uint32_t dhcp_xid ( struct net_device *netdev ) {
  106. uint32_t xid;
  107. memcpy ( &xid, ( netdev->ll_addr + netdev->ll_protocol->ll_addr_len
  108. - sizeof ( xid ) ), sizeof ( xid ) );
  109. return xid;
  110. }
  111. /** Options common to all DHCP requests */
  112. static struct dhcp_option_block dhcp_request_options = {
  113. .data = dhcp_request_options_data,
  114. .max_len = sizeof ( dhcp_request_options_data ),
  115. .len = sizeof ( dhcp_request_options_data ),
  116. };
  117. /**
  118. * Set option within DHCP packet
  119. *
  120. * @v dhcppkt DHCP packet
  121. * @v tag DHCP option tag
  122. * @v data New value for DHCP option
  123. * @v len Length of value, in bytes
  124. * @ret rc Return status code
  125. *
  126. * Sets the option within the first available options block within the
  127. * DHCP packet. Option blocks are tried in the order specified by @c
  128. * dhcp_option_block_fill_order.
  129. *
  130. * The magic options @c DHCP_EB_YIADDR and @c DHCP_EB_SIADDR are
  131. * intercepted and inserted into the appropriate fixed fields within
  132. * the DHCP packet. The option @c DHCP_OPTION_OVERLOAD is silently
  133. * ignored, since our DHCP packet assembly method relies on always
  134. * having option overloading in use.
  135. */
  136. static int set_dhcp_packet_option ( struct dhcp_packet *dhcppkt,
  137. unsigned int tag, const void *data,
  138. size_t len ) {
  139. struct dhcphdr *dhcphdr = dhcppkt->dhcphdr;
  140. struct dhcp_option_block *options = &dhcppkt->options;
  141. struct dhcp_option *option = NULL;
  142. /* Special-case the magic options */
  143. switch ( tag ) {
  144. case DHCP_OPTION_OVERLOAD:
  145. /* Hard-coded in packets we create; always ignore */
  146. return 0;
  147. case DHCP_EB_YIADDR:
  148. memcpy ( &dhcphdr->yiaddr, data, sizeof ( dhcphdr->yiaddr ) );
  149. return 0;
  150. case DHCP_EB_SIADDR:
  151. memcpy ( &dhcphdr->siaddr, data, sizeof ( dhcphdr->siaddr ) );
  152. return 0;
  153. case DHCP_TFTP_SERVER_NAME:
  154. memset ( dhcphdr->sname, 0, sizeof ( dhcphdr->sname ) );
  155. if ( len > sizeof ( dhcphdr->sname ) )
  156. len = sizeof ( dhcphdr->sname );
  157. memcpy ( dhcphdr->sname, data, len );
  158. return 0;
  159. case DHCP_BOOTFILE_NAME:
  160. memset ( dhcphdr->file, 0, sizeof ( dhcphdr->file ) );
  161. if ( len > sizeof ( dhcphdr->file ) )
  162. len = sizeof ( dhcphdr->file );
  163. memcpy ( dhcphdr->file, data, len );
  164. return 0;
  165. default:
  166. /* Continue processing as normal */
  167. break;
  168. }
  169. /* Set option */
  170. option = set_dhcp_option ( options, tag, data, len );
  171. /* Update DHCP packet length */
  172. dhcppkt->len = ( offsetof ( typeof ( *dhcppkt->dhcphdr ), options )
  173. + dhcppkt->options.len );
  174. return ( option ? 0 : -ENOSPC );
  175. }
  176. /**
  177. * Copy option into DHCP packet
  178. *
  179. * @v dhcppkt DHCP packet
  180. * @v options DHCP option block, or NULL
  181. * @v tag DHCP option tag to search for
  182. * @v new_tag DHCP option tag to use for copied option
  183. * @ret rc Return status code
  184. *
  185. * Copies a single option, if present, from the DHCP options block
  186. * into a DHCP packet. The tag for the option may be changed if
  187. * desired; this is required by other parts of the DHCP code.
  188. *
  189. * @c options may specify a single options block, or be left as NULL
  190. * in order to search for the option within all registered options
  191. * blocks.
  192. */
  193. static int copy_dhcp_packet_option ( struct dhcp_packet *dhcppkt,
  194. struct dhcp_option_block *options,
  195. unsigned int tag, unsigned int new_tag ) {
  196. struct dhcp_option *option;
  197. int rc;
  198. option = find_dhcp_option ( options, tag );
  199. if ( option ) {
  200. if ( ( rc = set_dhcp_packet_option ( dhcppkt, new_tag,
  201. &option->data,
  202. option->len ) ) != 0 )
  203. return rc;
  204. }
  205. return 0;
  206. }
  207. /**
  208. * Copy options into DHCP packet
  209. *
  210. * @v dhcppkt DHCP packet
  211. * @v options DHCP option block, or NULL
  212. * @v encapsulator Encapsulating option, or zero
  213. * @ret rc Return status code
  214. *
  215. * Copies options with the specified encapsulator from DHCP options
  216. * blocks into a DHCP packet. Most options are copied verbatim.
  217. * Recognised encapsulated options fields are handled as such.
  218. *
  219. * @c options may specify a single options block, or be left as NULL
  220. * in order to copy options from all registered options blocks.
  221. */
  222. static int copy_dhcp_packet_encap_options ( struct dhcp_packet *dhcppkt,
  223. struct dhcp_option_block *options,
  224. unsigned int encapsulator ) {
  225. unsigned int subtag;
  226. unsigned int tag;
  227. int rc;
  228. for ( subtag = DHCP_MIN_OPTION; subtag <= DHCP_MAX_OPTION; subtag++ ) {
  229. tag = DHCP_ENCAP_OPT ( encapsulator, subtag );
  230. switch ( tag ) {
  231. case DHCP_EB_ENCAP:
  232. case DHCP_VENDOR_ENCAP:
  233. /* Process encapsulated options field */
  234. if ( ( rc = copy_dhcp_packet_encap_options ( dhcppkt,
  235. options,
  236. tag)) !=0)
  237. return rc;
  238. break;
  239. default:
  240. /* Copy option to reassembled packet */
  241. if ( ( rc = copy_dhcp_packet_option ( dhcppkt, options,
  242. tag, tag ) ) !=0)
  243. return rc;
  244. break;
  245. };
  246. }
  247. return 0;
  248. }
  249. /**
  250. * Copy options into DHCP packet
  251. *
  252. * @v dhcppkt DHCP packet
  253. * @v options DHCP option block, or NULL
  254. * @ret rc Return status code
  255. *
  256. * Copies options from DHCP options blocks into a DHCP packet. Most
  257. * options are copied verbatim. Recognised encapsulated options
  258. * fields are handled as such.
  259. *
  260. * @c options may specify a single options block, or be left as NULL
  261. * in order to copy options from all registered options blocks.
  262. */
  263. static int copy_dhcp_packet_options ( struct dhcp_packet *dhcppkt,
  264. struct dhcp_option_block *options ) {
  265. return copy_dhcp_packet_encap_options ( dhcppkt, options, 0 );
  266. }
  267. /**
  268. * Create a DHCP packet
  269. *
  270. * @v netdev Network device
  271. * @v msgtype DHCP message type
  272. * @v data Buffer for DHCP packet
  273. * @v max_len Size of DHCP packet buffer
  274. * @v dhcppkt DHCP packet structure to fill in
  275. * @ret rc Return status code
  276. *
  277. * Creates a DHCP packet in the specified buffer, and fills out a @c
  278. * dhcp_packet structure that can be passed to
  279. * set_dhcp_packet_option() or copy_dhcp_packet_options().
  280. */
  281. static int create_dhcp_packet ( struct net_device *netdev, uint8_t msgtype,
  282. void *data, size_t max_len,
  283. struct dhcp_packet *dhcppkt ) {
  284. struct dhcphdr *dhcphdr = data;
  285. unsigned int hlen;
  286. int rc;
  287. /* Sanity check */
  288. if ( max_len < sizeof ( *dhcphdr ) )
  289. return -ENOSPC;
  290. /* Initialise DHCP packet content */
  291. memset ( dhcphdr, 0, max_len );
  292. dhcphdr->xid = dhcp_xid ( netdev );
  293. dhcphdr->magic = htonl ( DHCP_MAGIC_COOKIE );
  294. dhcphdr->htype = ntohs ( netdev->ll_protocol->ll_proto );
  295. dhcphdr->op = dhcp_op[msgtype];
  296. /* If hardware length exceeds the chaddr field length, don't
  297. * use the chaddr field. This is as per RFC4390.
  298. */
  299. hlen = netdev->ll_protocol->ll_addr_len;
  300. if ( hlen > sizeof ( dhcphdr->chaddr ) ) {
  301. hlen = 0;
  302. dhcphdr->flags = htons ( BOOTP_FL_BROADCAST );
  303. }
  304. dhcphdr->hlen = hlen;
  305. memcpy ( dhcphdr->chaddr, netdev->ll_addr, hlen );
  306. /* Initialise DHCP packet structure */
  307. dhcppkt->dhcphdr = dhcphdr;
  308. dhcppkt->max_len = max_len;
  309. init_dhcp_options ( &dhcppkt->options, dhcphdr->options,
  310. ( max_len -
  311. offsetof ( typeof ( *dhcphdr ), options ) ) );
  312. /* Set DHCP_MESSAGE_TYPE option */
  313. if ( ( rc = set_dhcp_packet_option ( dhcppkt, DHCP_MESSAGE_TYPE,
  314. &msgtype,
  315. sizeof ( msgtype ) ) ) != 0 )
  316. return rc;
  317. return 0;
  318. }
  319. /**
  320. * Calculate used length of a field containing DHCP options
  321. *
  322. * @v data Field containing DHCP options
  323. * @v max_len Field length
  324. * @ret len Used length (excluding the @c DHCP_END tag)
  325. */
  326. static size_t dhcp_field_len ( const void *data, size_t max_len ) {
  327. struct dhcp_option_block options;
  328. struct dhcp_option *end;
  329. options.data = ( ( void * ) data );
  330. options.len = max_len;
  331. end = find_dhcp_option ( &options, DHCP_END );
  332. return ( end ? ( ( ( void * ) end ) - data ) : 0 );
  333. }
  334. /**
  335. * Merge field containing DHCP options or string into DHCP options block
  336. *
  337. * @v options DHCP option block
  338. * @v data Field containing DHCP options
  339. * @v max_len Field length
  340. * @v tag DHCP option tag, or 0
  341. *
  342. * If @c tag is non-zero (and the field is not empty), the field will
  343. * be treated as a NUL-terminated string representing the value of the
  344. * specified DHCP option. If @c tag is zero, the field will be
  345. * treated as a block of DHCP options, and simply appended to the
  346. * existing options in the option block.
  347. *
  348. * The caller must ensure that there is enough space in the options
  349. * block to perform the merge.
  350. */
  351. static void merge_dhcp_field ( struct dhcp_option_block *options,
  352. const void *data, size_t max_len,
  353. unsigned int tag ) {
  354. size_t len;
  355. void *dest;
  356. struct dhcp_option *end;
  357. if ( tag ) {
  358. len = strlen ( data );
  359. if ( len )
  360. set_dhcp_option ( options, tag, data, len );
  361. } else {
  362. len = dhcp_field_len ( data, max_len );
  363. dest = ( options->data + options->len - 1 );
  364. memcpy ( dest, data, len );
  365. options->len += len;
  366. end = ( dest + len );
  367. end->tag = DHCP_END;
  368. }
  369. }
  370. /**
  371. * Parse DHCP packet and construct DHCP options block
  372. *
  373. * @v dhcphdr DHCP packet
  374. * @v len Length of DHCP packet
  375. * @ret options DHCP options block, or NULL
  376. *
  377. * Parses a received DHCP packet and canonicalises its contents into a
  378. * single DHCP options block. The "file" and "sname" fields are
  379. * converted into the corresponding DHCP options (@c
  380. * DHCP_BOOTFILE_NAME and @c DHCP_TFTP_SERVER_NAME respectively). If
  381. * these fields are used for option overloading, their options are
  382. * merged in to the options block.
  383. *
  384. * The values of the "yiaddr" and "siaddr" fields will be stored
  385. * within the options block as the magic options @c DHCP_EB_YIADDR and
  386. * @c DHCP_EB_SIADDR.
  387. *
  388. * Note that this call allocates new memory for the constructed DHCP
  389. * options block; it is the responsibility of the caller to eventually
  390. * free this memory.
  391. */
  392. static struct dhcp_option_block * dhcp_parse ( const struct dhcphdr *dhcphdr,
  393. size_t len ) {
  394. struct dhcp_option_block *options;
  395. size_t options_len;
  396. unsigned int overloading;
  397. /* Sanity check */
  398. if ( len < sizeof ( *dhcphdr ) )
  399. return NULL;
  400. /* Calculate size of resulting concatenated option block:
  401. *
  402. * The "options" field : length of the field minus the DHCP_END tag.
  403. *
  404. * The "file" field : maximum length of the field minus the
  405. * NUL terminator, plus a 2-byte DHCP header or, if used for
  406. * option overloading, the length of the field minus the
  407. * DHCP_END tag.
  408. *
  409. * The "sname" field : as for the "file" field.
  410. *
  411. * 15 bytes for an encapsulated options field to contain the
  412. * value of the "yiaddr" and "siaddr" fields
  413. *
  414. * 1 byte for a final terminating DHCP_END tag.
  415. */
  416. options_len = ( ( len - offsetof ( typeof ( *dhcphdr ), options ) ) - 1
  417. + ( sizeof ( dhcphdr->file ) + 1 )
  418. + ( sizeof ( dhcphdr->sname ) + 1 )
  419. + 15 /* yiaddr and siaddr */
  420. + 1 /* DHCP_END tag */ );
  421. /* Allocate empty options block of required size */
  422. options = alloc_dhcp_options ( options_len );
  423. if ( ! options ) {
  424. DBG ( "DHCP could not allocate %zd-byte option block\n",
  425. options_len );
  426. return NULL;
  427. }
  428. /* Merge in "options" field, if this is a DHCP packet */
  429. if ( dhcphdr->magic == htonl ( DHCP_MAGIC_COOKIE ) ) {
  430. merge_dhcp_field ( options, dhcphdr->options,
  431. ( len -
  432. offsetof ( typeof (*dhcphdr), options ) ),
  433. 0 /* Always contains options */ );
  434. }
  435. /* Identify overloaded fields */
  436. overloading = find_dhcp_num_option ( options, DHCP_OPTION_OVERLOAD );
  437. /* Merge in "file" and "sname" fields */
  438. merge_dhcp_field ( options, dhcphdr->file, sizeof ( dhcphdr->file ),
  439. ( ( overloading & DHCP_OPTION_OVERLOAD_FILE ) ?
  440. 0 : DHCP_BOOTFILE_NAME ) );
  441. merge_dhcp_field ( options, dhcphdr->sname, sizeof ( dhcphdr->sname ),
  442. ( ( overloading & DHCP_OPTION_OVERLOAD_SNAME ) ?
  443. 0 : DHCP_TFTP_SERVER_NAME ) );
  444. /* Set magic options for "yiaddr" and "siaddr", if present */
  445. if ( dhcphdr->yiaddr.s_addr ) {
  446. set_dhcp_option ( options, DHCP_EB_YIADDR,
  447. &dhcphdr->yiaddr, sizeof (dhcphdr->yiaddr) );
  448. }
  449. if ( dhcphdr->siaddr.s_addr ) {
  450. set_dhcp_option ( options, DHCP_EB_SIADDR,
  451. &dhcphdr->siaddr, sizeof (dhcphdr->siaddr) );
  452. }
  453. assert ( options->len <= options->max_len );
  454. return options;
  455. }
  456. /****************************************************************************
  457. *
  458. * Whole-packet construction
  459. *
  460. */
  461. /** DHCP network device descriptor */
  462. struct dhcp_netdev_desc {
  463. /** Bus type ID */
  464. uint8_t type;
  465. /** Vendor ID */
  466. uint16_t vendor;
  467. /** Device ID */
  468. uint16_t device;
  469. } __attribute__ (( packed ));
  470. /** DHCP client identifier */
  471. struct dhcp_client_id {
  472. /** Link-layer protocol */
  473. uint8_t ll_proto;
  474. /** Link-layer address */
  475. uint8_t ll_addr[MAX_LL_ADDR_LEN];
  476. } __attribute__ (( packed ));
  477. /** DHCP client UUID */
  478. struct dhcp_client_uuid {
  479. /** Identifier type */
  480. uint8_t type;
  481. /** UUID */
  482. union uuid uuid;
  483. } __attribute__ (( packed ));
  484. #define DHCP_CLIENT_UUID_TYPE 0
  485. /**
  486. * Create DHCP request
  487. *
  488. * @v netdev Network device
  489. * @v msgtype DHCP message type
  490. * @v options DHCP server response options, or NULL
  491. * @v data Buffer for DHCP packet
  492. * @v max_len Size of DHCP packet buffer
  493. * @v dhcppkt DHCP packet structure to fill in
  494. * @ret rc Return status code
  495. */
  496. int create_dhcp_request ( struct net_device *netdev, int msgtype,
  497. struct dhcp_option_block *options,
  498. void *data, size_t max_len,
  499. struct dhcp_packet *dhcppkt ) {
  500. struct device_description *desc = &netdev->dev->desc;
  501. struct dhcp_netdev_desc dhcp_desc;
  502. struct dhcp_client_id client_id;
  503. struct dhcp_client_uuid client_uuid;
  504. size_t dhcp_features_len;
  505. size_t ll_addr_len;
  506. int rc;
  507. /* Create DHCP packet */
  508. if ( ( rc = create_dhcp_packet ( netdev, msgtype, data, max_len,
  509. dhcppkt ) ) != 0 ) {
  510. DBG ( "DHCP could not create DHCP packet: %s\n",
  511. strerror ( rc ) );
  512. return rc;
  513. }
  514. /* Copy in options common to all requests */
  515. if ( ( rc = copy_dhcp_packet_options ( dhcppkt,
  516. &dhcp_request_options )) !=0 ){
  517. DBG ( "DHCP could not set common DHCP options: %s\n",
  518. strerror ( rc ) );
  519. return rc;
  520. }
  521. /* Copy any required options from previous server repsonse */
  522. if ( options ) {
  523. if ( ( rc = copy_dhcp_packet_option ( dhcppkt, options,
  524. DHCP_SERVER_IDENTIFIER,
  525. DHCP_SERVER_IDENTIFIER ) ) != 0 ) {
  526. DBG ( "DHCP could not set server identifier "
  527. "option: %s\n", strerror ( rc ) );
  528. return rc;
  529. }
  530. if ( ( rc = copy_dhcp_packet_option ( dhcppkt, options,
  531. DHCP_EB_YIADDR,
  532. DHCP_REQUESTED_ADDRESS ) ) != 0 ) {
  533. DBG ( "DHCP could not set requested address "
  534. "option: %s\n", strerror ( rc ) );
  535. return rc;
  536. }
  537. }
  538. /* Add options to identify the feature list */
  539. dhcp_features_len = ( dhcp_features_end - dhcp_features );
  540. if ( ( rc = set_dhcp_packet_option ( dhcppkt, DHCP_EB_ENCAP,
  541. dhcp_features,
  542. dhcp_features_len ) ) != 0 ) {
  543. DBG ( "DHCP could not set features list option: %s\n",
  544. strerror ( rc ) );
  545. return rc;
  546. }
  547. /* Add options to identify the network device */
  548. dhcp_desc.type = desc->bus_type;
  549. dhcp_desc.vendor = htons ( desc->vendor );
  550. dhcp_desc.device = htons ( desc->device );
  551. if ( ( rc = set_dhcp_packet_option ( dhcppkt, DHCP_EB_BUS_ID,
  552. &dhcp_desc,
  553. sizeof ( dhcp_desc ) ) ) != 0 ) {
  554. DBG ( "DHCP could not set bus ID option: %s\n",
  555. strerror ( rc ) );
  556. return rc;
  557. }
  558. /* Add DHCP client identifier. Required for Infiniband, and
  559. * doesn't hurt other link layers.
  560. */
  561. client_id.ll_proto = netdev->ll_protocol->ll_proto;
  562. ll_addr_len = netdev->ll_protocol->ll_addr_len;
  563. assert ( ll_addr_len <= sizeof ( client_id.ll_addr ) );
  564. memcpy ( client_id.ll_addr, netdev->ll_addr, ll_addr_len );
  565. if ( ( rc = set_dhcp_packet_option ( dhcppkt, DHCP_CLIENT_ID,
  566. &client_id,
  567. ( ll_addr_len + 1 ) ) ) != 0 ) {
  568. DBG ( "DHCP could not set client ID: %s\n",
  569. strerror ( rc ) );
  570. return rc;
  571. }
  572. /* Add client UUID, if we have one. Required for PXE. */
  573. client_uuid.type = DHCP_CLIENT_UUID_TYPE;
  574. if ( ( rc = get_uuid ( &client_uuid.uuid ) ) == 0 ) {
  575. if ( ( rc = set_dhcp_packet_option ( dhcppkt,
  576. DHCP_CLIENT_UUID, &client_uuid,
  577. sizeof ( client_uuid ) ) ) != 0 ) {
  578. DBG ( "DHCP could not set client UUID: %s\n",
  579. strerror ( rc ) );
  580. return rc;
  581. }
  582. }
  583. return 0;
  584. }
  585. /**
  586. * Create DHCP response
  587. *
  588. * @v netdev Network device
  589. * @v msgtype DHCP message type
  590. * @v options DHCP options, or NULL
  591. * @v data Buffer for DHCP packet
  592. * @v max_len Size of DHCP packet buffer
  593. * @v dhcppkt DHCP packet structure to fill in
  594. * @ret rc Return status code
  595. */
  596. int create_dhcp_response ( struct net_device *netdev, int msgtype,
  597. struct dhcp_option_block *options,
  598. void *data, size_t max_len,
  599. struct dhcp_packet *dhcppkt ) {
  600. int rc;
  601. /* Create packet and copy in options */
  602. if ( ( rc = create_dhcp_packet ( netdev, msgtype, data, max_len,
  603. dhcppkt ) ) != 0 ) {
  604. DBG ( " failed to build packet" );
  605. return rc;
  606. }
  607. if ( ( rc = copy_dhcp_packet_options ( dhcppkt, options ) ) != 0 ) {
  608. DBG ( " failed to copy options" );
  609. return rc;
  610. }
  611. return 0;
  612. }
  613. /****************************************************************************
  614. *
  615. * DHCP to UDP interface
  616. *
  617. */
  618. /** A DHCP session */
  619. struct dhcp_session {
  620. /** Reference counter */
  621. struct refcnt refcnt;
  622. /** Job control interface */
  623. struct job_interface job;
  624. /** Data transfer interface */
  625. struct xfer_interface xfer;
  626. /** Network device being configured */
  627. struct net_device *netdev;
  628. /** Option block registration routine */
  629. int ( * register_options ) ( struct net_device *netdev,
  630. struct dhcp_option_block *options );
  631. /** State of the session
  632. *
  633. * This is a value for the @c DHCP_MESSAGE_TYPE option
  634. * (e.g. @c DHCPDISCOVER).
  635. */
  636. int state;
  637. /** Options obtained from DHCP server */
  638. struct dhcp_option_block *options;
  639. /** Options obtained from ProxyDHCP server */
  640. struct dhcp_option_block *proxy_options;
  641. /** Retransmission timer */
  642. struct retry_timer timer;
  643. /** Session start time (in ticks) */
  644. unsigned long start;
  645. };
  646. /**
  647. * Free DHCP session
  648. *
  649. * @v refcnt Reference counter
  650. */
  651. static void dhcp_free ( struct refcnt *refcnt ) {
  652. struct dhcp_session *dhcp =
  653. container_of ( refcnt, struct dhcp_session, refcnt );
  654. netdev_put ( dhcp->netdev );
  655. dhcpopt_put ( dhcp->options );
  656. dhcpopt_put ( dhcp->proxy_options );
  657. free ( dhcp );
  658. }
  659. /**
  660. * Mark DHCP session as complete
  661. *
  662. * @v dhcp DHCP session
  663. * @v rc Return status code
  664. */
  665. static void dhcp_finished ( struct dhcp_session *dhcp, int rc ) {
  666. /* Block futher incoming messages */
  667. job_nullify ( &dhcp->job );
  668. xfer_nullify ( &dhcp->xfer );
  669. /* Stop retry timer */
  670. stop_timer ( &dhcp->timer );
  671. /* Free resources and close interfaces */
  672. xfer_close ( &dhcp->xfer, rc );
  673. job_done ( &dhcp->job, rc );
  674. }
  675. /****************************************************************************
  676. *
  677. * Data transfer interface
  678. *
  679. */
  680. /**
  681. * Transmit DHCP request
  682. *
  683. * @v dhcp DHCP session
  684. * @ret rc Return status code
  685. */
  686. static int dhcp_send_request ( struct dhcp_session *dhcp ) {
  687. struct xfer_metadata meta = {
  688. .netdev = dhcp->netdev,
  689. };
  690. struct io_buffer *iobuf;
  691. struct dhcp_packet dhcppkt;
  692. int rc;
  693. DBGC ( dhcp, "DHCP %p transmitting %s\n",
  694. dhcp, dhcp_msgtype_name ( dhcp->state ) );
  695. assert ( ( dhcp->state == DHCPDISCOVER ) ||
  696. ( dhcp->state == DHCPREQUEST ) );
  697. /* Start retry timer. Do this first so that failures to
  698. * transmit will be retried.
  699. */
  700. start_timer ( &dhcp->timer );
  701. /* Allocate buffer for packet */
  702. iobuf = xfer_alloc_iob ( &dhcp->xfer, DHCP_MIN_LEN );
  703. if ( ! iobuf )
  704. return -ENOMEM;
  705. /* Create DHCP packet in temporary buffer */
  706. if ( ( rc = create_dhcp_request ( dhcp->netdev, dhcp->state,
  707. dhcp->options, iobuf->data,
  708. iob_tailroom ( iobuf ),
  709. &dhcppkt ) ) != 0 ) {
  710. DBGC ( dhcp, "DHCP %p could not construct DHCP request: %s\n",
  711. dhcp, strerror ( rc ) );
  712. goto done;
  713. }
  714. /* Transmit the packet */
  715. iob_put ( iobuf, dhcppkt.len );
  716. rc = xfer_deliver_iob_meta ( &dhcp->xfer, iobuf, &meta );
  717. iobuf = NULL;
  718. if ( rc != 0 ) {
  719. DBGC ( dhcp, "DHCP %p could not transmit UDP packet: %s\n",
  720. dhcp, strerror ( rc ) );
  721. goto done;
  722. }
  723. done:
  724. free_iob ( iobuf );
  725. return rc;
  726. }
  727. /**
  728. * Handle DHCP retry timer expiry
  729. *
  730. * @v timer DHCP retry timer
  731. * @v fail Failure indicator
  732. */
  733. static void dhcp_timer_expired ( struct retry_timer *timer, int fail ) {
  734. struct dhcp_session *dhcp =
  735. container_of ( timer, struct dhcp_session, timer );
  736. if ( fail ) {
  737. dhcp_finished ( dhcp, -ETIMEDOUT );
  738. } else {
  739. dhcp_send_request ( dhcp );
  740. }
  741. }
  742. /**
  743. * Receive new data
  744. *
  745. * @v xfer Data transfer interface
  746. * @v iobuf I/O buffer
  747. * @v data Received data
  748. * @v len Length of received data
  749. * @ret rc Return status code
  750. */
  751. static int dhcp_deliver_raw ( struct xfer_interface *xfer,
  752. const void *data, size_t len ) {
  753. struct dhcp_session *dhcp =
  754. container_of ( xfer, struct dhcp_session, xfer );
  755. const struct dhcphdr *dhcphdr = data;
  756. struct dhcp_option_block *options;
  757. struct dhcp_option_block **store_options;
  758. unsigned int msgtype;
  759. unsigned long elapsed;
  760. int is_proxy;
  761. int ignore_proxy;
  762. /* Check for matching transaction ID */
  763. if ( dhcphdr->xid != dhcp_xid ( dhcp->netdev ) ) {
  764. DBGC ( dhcp, "DHCP %p wrong transaction ID (wanted %08lx, "
  765. "got %08lx)\n", dhcp, ntohl ( dhcphdr->xid ),
  766. ntohl ( dhcp_xid ( dhcp->netdev ) ) );
  767. return 0;
  768. };
  769. /* Parse packet and create options structure */
  770. options = dhcp_parse ( dhcphdr, len );
  771. if ( ! options ) {
  772. DBGC ( dhcp, "DHCP %p could not parse DHCP packet\n", dhcp );
  773. return -EINVAL;
  774. }
  775. /* Determine and verify message type */
  776. is_proxy = ( dhcphdr->yiaddr.s_addr == 0 );
  777. msgtype = find_dhcp_num_option ( options, DHCP_MESSAGE_TYPE );
  778. DBGC ( dhcp, "DHCP %p received %s%s\n", dhcp,
  779. ( is_proxy ? "Proxy" : "" ), dhcp_msgtype_name ( msgtype ) );
  780. if ( ( ( dhcp->state != DHCPDISCOVER ) || ( msgtype != DHCPOFFER ) ) &&
  781. ( ( dhcp->state != DHCPREQUEST ) || ( msgtype != DHCPACK ) ) ) {
  782. DBGC ( dhcp, "DHCP %p discarding %s while in %s state\n",
  783. dhcp, dhcp_msgtype_name ( msgtype ),
  784. dhcp_msgtype_name ( dhcp->state ) );
  785. goto out_discard;
  786. }
  787. /* Update stored standard/ProxyDHCP options, if the new
  788. * options have equal or higher priority than the
  789. * currently-stored options.
  790. */
  791. store_options = ( is_proxy ? &dhcp->proxy_options : &dhcp->options );
  792. if ( ( ! *store_options ) ||
  793. ( find_dhcp_num_option ( options, DHCP_EB_PRIORITY ) >=
  794. find_dhcp_num_option ( *store_options, DHCP_EB_PRIORITY ) ) ) {
  795. dhcpopt_put ( *store_options );
  796. *store_options = options;
  797. } else {
  798. dhcpopt_put ( options );
  799. }
  800. /* If we don't yet have a standard DHCP response (i.e. one
  801. * with an IP address), then just leave the timer running.
  802. */
  803. if ( ! dhcp->options )
  804. goto out;
  805. /* Handle DHCP response */
  806. ignore_proxy = find_dhcp_num_option ( dhcp->options,
  807. DHCP_EB_NO_PROXYDHCP );
  808. switch ( dhcp->state ) {
  809. case DHCPDISCOVER:
  810. /* If we have allowed sufficient time for ProxyDHCP
  811. * reponses, then transition to making the DHCPREQUEST.
  812. */
  813. elapsed = ( currticks() - dhcp->start );
  814. if ( ignore_proxy || ( elapsed > PROXYDHCP_WAIT_TIME ) ) {
  815. stop_timer ( &dhcp->timer );
  816. dhcp->state = DHCPREQUEST;
  817. dhcp_send_request ( dhcp );
  818. }
  819. break;
  820. case DHCPREQUEST:
  821. /* DHCP finished; register options and exit */
  822. if ( dhcp->proxy_options && ( ! ignore_proxy ) ) {
  823. dhcp->register_options ( dhcp->netdev,
  824. dhcp->proxy_options );
  825. }
  826. dhcp->register_options ( dhcp->netdev, dhcp->options );
  827. dhcp_finished ( dhcp, 0 );
  828. break;
  829. default:
  830. assert ( 0 );
  831. }
  832. out:
  833. return 0;
  834. out_discard:
  835. dhcpopt_put ( options );
  836. return 0;
  837. }
  838. /** DHCP data transfer interface operations */
  839. static struct xfer_interface_operations dhcp_xfer_operations = {
  840. .close = ignore_xfer_close,
  841. .vredirect = xfer_vopen,
  842. .seek = ignore_xfer_seek,
  843. .window = unlimited_xfer_window,
  844. .deliver_iob = xfer_deliver_as_raw,
  845. .deliver_raw = dhcp_deliver_raw,
  846. };
  847. /****************************************************************************
  848. *
  849. * Job control interface
  850. *
  851. */
  852. /**
  853. * Handle kill() event received via job control interface
  854. *
  855. * @v job DHCP job control interface
  856. */
  857. static void dhcp_job_kill ( struct job_interface *job ) {
  858. struct dhcp_session *dhcp =
  859. container_of ( job, struct dhcp_session, job );
  860. /* Terminate DHCP session */
  861. dhcp_finished ( dhcp, -ECANCELED );
  862. }
  863. /** DHCP job control interface operations */
  864. static struct job_interface_operations dhcp_job_operations = {
  865. .done = ignore_job_done,
  866. .kill = dhcp_job_kill,
  867. .progress = ignore_job_progress,
  868. };
  869. /****************************************************************************
  870. *
  871. * Instantiator
  872. *
  873. */
  874. /**
  875. * Start DHCP on a network device
  876. *
  877. * @v job Job control interface
  878. * @v netdev Network device
  879. * @v register_options DHCP option block registration routine
  880. * @ret rc Return status code
  881. *
  882. * Starts DHCP on the specified network device. If successful, the @c
  883. * register_options() routine will be called with the acquired
  884. * options.
  885. */
  886. int start_dhcp ( struct job_interface *job, struct net_device *netdev,
  887. int ( * register_options ) ( struct net_device *netdev,
  888. struct dhcp_option_block * ) ) {
  889. static struct sockaddr_in server = {
  890. .sin_family = AF_INET,
  891. .sin_addr.s_addr = INADDR_BROADCAST,
  892. .sin_port = htons ( BOOTPS_PORT ),
  893. };
  894. static struct sockaddr_in client = {
  895. .sin_family = AF_INET,
  896. .sin_port = htons ( BOOTPC_PORT ),
  897. };
  898. struct dhcp_session *dhcp;
  899. int rc;
  900. /* Allocate and initialise structure */
  901. dhcp = zalloc ( sizeof ( *dhcp ) );
  902. if ( ! dhcp )
  903. return -ENOMEM;
  904. dhcp->refcnt.free = dhcp_free;
  905. job_init ( &dhcp->job, &dhcp_job_operations, &dhcp->refcnt );
  906. xfer_init ( &dhcp->xfer, &dhcp_xfer_operations, &dhcp->refcnt );
  907. dhcp->netdev = netdev_get ( netdev );
  908. dhcp->register_options = register_options;
  909. dhcp->timer.expired = dhcp_timer_expired;
  910. dhcp->state = DHCPDISCOVER;
  911. dhcp->start = currticks();
  912. /* Instantiate child objects and attach to our interfaces */
  913. if ( ( rc = xfer_open_socket ( &dhcp->xfer, SOCK_DGRAM,
  914. ( struct sockaddr * ) &server,
  915. ( struct sockaddr * ) &client ) ) != 0 )
  916. goto err;
  917. /* Start timer to initiate initial DHCPREQUEST */
  918. start_timer_nodelay ( &dhcp->timer );
  919. /* Attach parent interface, mortalise self, and return */
  920. job_plug_plug ( &dhcp->job, job );
  921. ref_put ( &dhcp->refcnt );
  922. return 0;
  923. err:
  924. dhcp_finished ( dhcp, rc );
  925. ref_put ( &dhcp->refcnt );
  926. return rc;
  927. }
  928. /****************************************************************************
  929. *
  930. * Network device configurator
  931. *
  932. */
  933. /**
  934. * Configure network device from DHCP options
  935. *
  936. * @v netdev Network device
  937. * @v options DHCP options block
  938. * @ret rc Return status code
  939. */
  940. int dhcp_configure_netdev ( struct net_device *netdev,
  941. struct dhcp_option_block *options ) {
  942. struct in_addr address = { 0 };
  943. struct in_addr netmask = { 0 };
  944. struct in_addr gateway = { INADDR_NONE };
  945. int rc;
  946. /* Retrieve IP address configuration */
  947. find_dhcp_ipv4_option ( options, DHCP_EB_YIADDR, &address );
  948. find_dhcp_ipv4_option ( options, DHCP_SUBNET_MASK, &netmask );
  949. find_dhcp_ipv4_option ( options, DHCP_ROUTERS, &gateway );
  950. /* Do nothing unless we have at least an IP address to use */
  951. if ( ! address.s_addr )
  952. return 0;
  953. /* Clear any existing routing table entry */
  954. del_ipv4_address ( netdev );
  955. /* Set up new IP address configuration */
  956. if ( ( rc = add_ipv4_address ( netdev, address, netmask,
  957. gateway ) ) != 0 ) {
  958. DBG ( "Could not configure %s with DHCP results: %s\n",
  959. netdev->name, strerror ( rc ) );
  960. return rc;
  961. }
  962. return 0;
  963. }