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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067
  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, the field will be treated as a
  343. * NUL-terminated string representing the value of the specified DHCP
  344. * option. If @c tag is zero, the field will be treated as a block of
  345. * DHCP options, and simply appended to the existing options in the
  346. * 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. set_dhcp_option ( options, tag, data, strlen ( data ) );
  359. } else {
  360. len = dhcp_field_len ( data, max_len );
  361. dest = ( options->data + options->len - 1 );
  362. memcpy ( dest, data, len );
  363. options->len += len;
  364. end = ( dest + len );
  365. end->tag = DHCP_END;
  366. }
  367. }
  368. /**
  369. * Parse DHCP packet and construct DHCP options block
  370. *
  371. * @v dhcphdr DHCP packet
  372. * @v len Length of DHCP packet
  373. * @ret options DHCP options block, or NULL
  374. *
  375. * Parses a received DHCP packet and canonicalises its contents into a
  376. * single DHCP options block. The "file" and "sname" fields are
  377. * converted into the corresponding DHCP options (@c
  378. * DHCP_BOOTFILE_NAME and @c DHCP_TFTP_SERVER_NAME respectively). If
  379. * these fields are used for option overloading, their options are
  380. * merged in to the options block.
  381. *
  382. * The values of the "yiaddr" and "siaddr" fields will be stored
  383. * within the options block as the magic options @c DHCP_EB_YIADDR and
  384. * @c DHCP_EB_SIADDR.
  385. *
  386. * Note that this call allocates new memory for the constructed DHCP
  387. * options block; it is the responsibility of the caller to eventually
  388. * free this memory.
  389. */
  390. static struct dhcp_option_block * dhcp_parse ( const struct dhcphdr *dhcphdr,
  391. size_t len ) {
  392. struct dhcp_option_block *options;
  393. size_t options_len;
  394. unsigned int overloading;
  395. /* Sanity check */
  396. if ( len < sizeof ( *dhcphdr ) )
  397. return NULL;
  398. /* Calculate size of resulting concatenated option block:
  399. *
  400. * The "options" field : length of the field minus the DHCP_END tag.
  401. *
  402. * The "file" field : maximum length of the field minus the
  403. * NUL terminator, plus a 2-byte DHCP header or, if used for
  404. * option overloading, the length of the field minus the
  405. * DHCP_END tag.
  406. *
  407. * The "sname" field : as for the "file" field.
  408. *
  409. * 15 bytes for an encapsulated options field to contain the
  410. * value of the "yiaddr" and "siaddr" fields
  411. *
  412. * 1 byte for a final terminating DHCP_END tag.
  413. */
  414. options_len = ( ( len - offsetof ( typeof ( *dhcphdr ), options ) ) - 1
  415. + ( sizeof ( dhcphdr->file ) + 1 )
  416. + ( sizeof ( dhcphdr->sname ) + 1 )
  417. + 15 /* yiaddr and siaddr */
  418. + 1 /* DHCP_END tag */ );
  419. /* Allocate empty options block of required size */
  420. options = alloc_dhcp_options ( options_len );
  421. if ( ! options ) {
  422. DBG ( "DHCP could not allocate %zd-byte option block\n",
  423. options_len );
  424. return NULL;
  425. }
  426. /* Merge in "options" field, if this is a DHCP packet */
  427. if ( dhcphdr->magic == htonl ( DHCP_MAGIC_COOKIE ) ) {
  428. merge_dhcp_field ( options, dhcphdr->options,
  429. ( len -
  430. offsetof ( typeof (*dhcphdr), options ) ),
  431. 0 /* Always contains options */ );
  432. }
  433. /* Identify overloaded fields */
  434. overloading = find_dhcp_num_option ( options, DHCP_OPTION_OVERLOAD );
  435. /* Merge in "file" and "sname" fields */
  436. merge_dhcp_field ( options, dhcphdr->file, sizeof ( dhcphdr->file ),
  437. ( ( overloading & DHCP_OPTION_OVERLOAD_FILE ) ?
  438. 0 : DHCP_BOOTFILE_NAME ) );
  439. merge_dhcp_field ( options, dhcphdr->sname, sizeof ( dhcphdr->sname ),
  440. ( ( overloading & DHCP_OPTION_OVERLOAD_SNAME ) ?
  441. 0 : DHCP_TFTP_SERVER_NAME ) );
  442. /* Set magic options for "yiaddr" and "siaddr", if present */
  443. if ( dhcphdr->yiaddr.s_addr ) {
  444. set_dhcp_option ( options, DHCP_EB_YIADDR,
  445. &dhcphdr->yiaddr, sizeof (dhcphdr->yiaddr) );
  446. }
  447. if ( dhcphdr->siaddr.s_addr ) {
  448. set_dhcp_option ( options, DHCP_EB_SIADDR,
  449. &dhcphdr->siaddr, sizeof (dhcphdr->siaddr) );
  450. }
  451. assert ( options->len <= options->max_len );
  452. return options;
  453. }
  454. /****************************************************************************
  455. *
  456. * Whole-packet construction
  457. *
  458. */
  459. /** DHCP network device descriptor */
  460. struct dhcp_netdev_desc {
  461. /** Bus type ID */
  462. uint8_t type;
  463. /** Vendor ID */
  464. uint16_t vendor;
  465. /** Device ID */
  466. uint16_t device;
  467. } __attribute__ (( packed ));
  468. /** DHCP client identifier */
  469. struct dhcp_client_id {
  470. /** Link-layer protocol */
  471. uint8_t ll_proto;
  472. /** Link-layer address */
  473. uint8_t ll_addr[MAX_LL_ADDR_LEN];
  474. } __attribute__ (( packed ));
  475. /** DHCP client UUID */
  476. struct dhcp_client_uuid {
  477. /** Identifier type */
  478. uint8_t type;
  479. /** UUID */
  480. union uuid uuid;
  481. } __attribute__ (( packed ));
  482. #define DHCP_CLIENT_UUID_TYPE 0
  483. /**
  484. * Create DHCP request
  485. *
  486. * @v netdev Network device
  487. * @v msgtype DHCP message type
  488. * @v options DHCP server response options, or NULL
  489. * @v data Buffer for DHCP packet
  490. * @v max_len Size of DHCP packet buffer
  491. * @v dhcppkt DHCP packet structure to fill in
  492. * @ret rc Return status code
  493. */
  494. int create_dhcp_request ( struct net_device *netdev, int msgtype,
  495. struct dhcp_option_block *options,
  496. void *data, size_t max_len,
  497. struct dhcp_packet *dhcppkt ) {
  498. struct device_description *desc = &netdev->dev->desc;
  499. struct dhcp_netdev_desc dhcp_desc;
  500. struct dhcp_client_id client_id;
  501. struct dhcp_client_uuid client_uuid;
  502. size_t dhcp_features_len;
  503. size_t ll_addr_len;
  504. int rc;
  505. /* Create DHCP packet */
  506. if ( ( rc = create_dhcp_packet ( netdev, msgtype, data, max_len,
  507. dhcppkt ) ) != 0 ) {
  508. DBG ( "DHCP could not create DHCP packet: %s\n",
  509. strerror ( rc ) );
  510. return rc;
  511. }
  512. /* Copy in options common to all requests */
  513. if ( ( rc = copy_dhcp_packet_options ( dhcppkt,
  514. &dhcp_request_options )) !=0 ){
  515. DBG ( "DHCP could not set common DHCP options: %s\n",
  516. strerror ( rc ) );
  517. return rc;
  518. }
  519. /* Copy any required options from previous server repsonse */
  520. if ( options ) {
  521. if ( ( rc = copy_dhcp_packet_option ( dhcppkt, options,
  522. DHCP_SERVER_IDENTIFIER,
  523. DHCP_SERVER_IDENTIFIER ) ) != 0 ) {
  524. DBG ( "DHCP could not set server identifier "
  525. "option: %s\n", strerror ( rc ) );
  526. return rc;
  527. }
  528. if ( ( rc = copy_dhcp_packet_option ( dhcppkt, options,
  529. DHCP_EB_YIADDR,
  530. DHCP_REQUESTED_ADDRESS ) ) != 0 ) {
  531. DBG ( "DHCP could not set requested address "
  532. "option: %s\n", strerror ( rc ) );
  533. return rc;
  534. }
  535. }
  536. /* Add options to identify the feature list */
  537. dhcp_features_len = ( dhcp_features_end - dhcp_features );
  538. if ( ( rc = set_dhcp_packet_option ( dhcppkt, DHCP_EB_ENCAP,
  539. dhcp_features,
  540. dhcp_features_len ) ) != 0 ) {
  541. DBG ( "DHCP could not set features list option: %s\n",
  542. strerror ( rc ) );
  543. return rc;
  544. }
  545. /* Add options to identify the network device */
  546. dhcp_desc.type = desc->bus_type;
  547. dhcp_desc.vendor = htons ( desc->vendor );
  548. dhcp_desc.device = htons ( desc->device );
  549. if ( ( rc = set_dhcp_packet_option ( dhcppkt, DHCP_EB_BUS_ID,
  550. &dhcp_desc,
  551. sizeof ( dhcp_desc ) ) ) != 0 ) {
  552. DBG ( "DHCP could not set bus ID option: %s\n",
  553. strerror ( rc ) );
  554. return rc;
  555. }
  556. /* Add DHCP client identifier. Required for Infiniband, and
  557. * doesn't hurt other link layers.
  558. */
  559. client_id.ll_proto = netdev->ll_protocol->ll_proto;
  560. ll_addr_len = netdev->ll_protocol->ll_addr_len;
  561. assert ( ll_addr_len <= sizeof ( client_id.ll_addr ) );
  562. memcpy ( client_id.ll_addr, netdev->ll_addr, ll_addr_len );
  563. if ( ( rc = set_dhcp_packet_option ( dhcppkt, DHCP_CLIENT_ID,
  564. &client_id,
  565. ( ll_addr_len + 1 ) ) ) != 0 ) {
  566. DBG ( "DHCP could not set client ID: %s\n",
  567. strerror ( rc ) );
  568. return rc;
  569. }
  570. /* Add client UUID, if we have one. Required for PXE. */
  571. client_uuid.type = DHCP_CLIENT_UUID_TYPE;
  572. if ( ( rc = get_uuid ( &client_uuid.uuid ) ) == 0 ) {
  573. if ( ( rc = set_dhcp_packet_option ( dhcppkt,
  574. DHCP_CLIENT_UUID, &client_uuid,
  575. sizeof ( client_uuid ) ) ) != 0 ) {
  576. DBG ( "DHCP could not set client UUID: %s\n",
  577. strerror ( rc ) );
  578. return rc;
  579. }
  580. }
  581. return 0;
  582. }
  583. /**
  584. * Create DHCP response
  585. *
  586. * @v netdev Network device
  587. * @v msgtype DHCP message type
  588. * @v options DHCP options, or NULL
  589. * @v data Buffer for DHCP packet
  590. * @v max_len Size of DHCP packet buffer
  591. * @v dhcppkt DHCP packet structure to fill in
  592. * @ret rc Return status code
  593. */
  594. int create_dhcp_response ( struct net_device *netdev, int msgtype,
  595. struct dhcp_option_block *options,
  596. void *data, size_t max_len,
  597. struct dhcp_packet *dhcppkt ) {
  598. int rc;
  599. /* Create packet and copy in options */
  600. if ( ( rc = create_dhcp_packet ( netdev, msgtype, data, max_len,
  601. dhcppkt ) ) != 0 ) {
  602. DBG ( " failed to build packet" );
  603. return rc;
  604. }
  605. if ( ( rc = copy_dhcp_packet_options ( dhcppkt, options ) ) != 0 ) {
  606. DBG ( " failed to copy options" );
  607. return rc;
  608. }
  609. return 0;
  610. }
  611. /****************************************************************************
  612. *
  613. * DHCP to UDP interface
  614. *
  615. */
  616. /** A DHCP session */
  617. struct dhcp_session {
  618. /** Reference counter */
  619. struct refcnt refcnt;
  620. /** Job control interface */
  621. struct job_interface job;
  622. /** Data transfer interface */
  623. struct xfer_interface xfer;
  624. /** Network device being configured */
  625. struct net_device *netdev;
  626. /** Option block registration routine */
  627. int ( * register_options ) ( struct net_device *netdev,
  628. struct dhcp_option_block *options );
  629. /** State of the session
  630. *
  631. * This is a value for the @c DHCP_MESSAGE_TYPE option
  632. * (e.g. @c DHCPDISCOVER).
  633. */
  634. int state;
  635. /** Options obtained from DHCP server */
  636. struct dhcp_option_block *options;
  637. /** Options obtained from ProxyDHCP server */
  638. struct dhcp_option_block *proxy_options;
  639. /** Retransmission timer */
  640. struct retry_timer timer;
  641. /** Session start time (in ticks) */
  642. unsigned long start;
  643. };
  644. /**
  645. * Free DHCP session
  646. *
  647. * @v refcnt Reference counter
  648. */
  649. static void dhcp_free ( struct refcnt *refcnt ) {
  650. struct dhcp_session *dhcp =
  651. container_of ( refcnt, struct dhcp_session, refcnt );
  652. netdev_put ( dhcp->netdev );
  653. dhcpopt_put ( dhcp->options );
  654. dhcpopt_put ( dhcp->proxy_options );
  655. free ( dhcp );
  656. }
  657. /**
  658. * Mark DHCP session as complete
  659. *
  660. * @v dhcp DHCP session
  661. * @v rc Return status code
  662. */
  663. static void dhcp_finished ( struct dhcp_session *dhcp, int rc ) {
  664. /* Block futher incoming messages */
  665. job_nullify ( &dhcp->job );
  666. xfer_nullify ( &dhcp->xfer );
  667. /* Stop retry timer */
  668. stop_timer ( &dhcp->timer );
  669. /* Free resources and close interfaces */
  670. xfer_close ( &dhcp->xfer, rc );
  671. job_done ( &dhcp->job, rc );
  672. }
  673. /****************************************************************************
  674. *
  675. * Data transfer interface
  676. *
  677. */
  678. /**
  679. * Transmit DHCP request
  680. *
  681. * @v dhcp DHCP session
  682. * @ret rc Return status code
  683. */
  684. static int dhcp_send_request ( struct dhcp_session *dhcp ) {
  685. struct xfer_metadata meta = {
  686. .netdev = dhcp->netdev,
  687. };
  688. struct io_buffer *iobuf;
  689. struct dhcp_packet dhcppkt;
  690. int rc;
  691. DBGC ( dhcp, "DHCP %p transmitting %s\n",
  692. dhcp, dhcp_msgtype_name ( dhcp->state ) );
  693. assert ( ( dhcp->state == DHCPDISCOVER ) ||
  694. ( dhcp->state == DHCPREQUEST ) );
  695. /* Start retry timer. Do this first so that failures to
  696. * transmit will be retried.
  697. */
  698. start_timer ( &dhcp->timer );
  699. /* Allocate buffer for packet */
  700. iobuf = xfer_alloc_iob ( &dhcp->xfer, DHCP_MIN_LEN );
  701. if ( ! iobuf )
  702. return -ENOMEM;
  703. /* Create DHCP packet in temporary buffer */
  704. if ( ( rc = create_dhcp_request ( dhcp->netdev, dhcp->state,
  705. dhcp->options, iobuf->data,
  706. iob_tailroom ( iobuf ),
  707. &dhcppkt ) ) != 0 ) {
  708. DBGC ( dhcp, "DHCP %p could not construct DHCP request: %s\n",
  709. dhcp, strerror ( rc ) );
  710. goto done;
  711. }
  712. /* Transmit the packet */
  713. iob_put ( iobuf, dhcppkt.len );
  714. rc = xfer_deliver_iob_meta ( &dhcp->xfer, iobuf, &meta );
  715. iobuf = NULL;
  716. if ( rc != 0 ) {
  717. DBGC ( dhcp, "DHCP %p could not transmit UDP packet: %s\n",
  718. dhcp, strerror ( rc ) );
  719. goto done;
  720. }
  721. done:
  722. free_iob ( iobuf );
  723. return rc;
  724. }
  725. /**
  726. * Handle DHCP retry timer expiry
  727. *
  728. * @v timer DHCP retry timer
  729. * @v fail Failure indicator
  730. */
  731. static void dhcp_timer_expired ( struct retry_timer *timer, int fail ) {
  732. struct dhcp_session *dhcp =
  733. container_of ( timer, struct dhcp_session, timer );
  734. if ( fail ) {
  735. dhcp_finished ( dhcp, -ETIMEDOUT );
  736. } else {
  737. dhcp_send_request ( dhcp );
  738. }
  739. }
  740. /**
  741. * Receive new data
  742. *
  743. * @v xfer Data transfer interface
  744. * @v iobuf I/O buffer
  745. * @v data Received data
  746. * @v len Length of received data
  747. * @ret rc Return status code
  748. */
  749. static int dhcp_deliver_raw ( struct xfer_interface *xfer,
  750. const void *data, size_t len ) {
  751. struct dhcp_session *dhcp =
  752. container_of ( xfer, struct dhcp_session, xfer );
  753. const struct dhcphdr *dhcphdr = data;
  754. struct dhcp_option_block *options;
  755. struct dhcp_option_block **store_options;
  756. unsigned int msgtype;
  757. unsigned long elapsed;
  758. int is_proxy;
  759. int ignore_proxy;
  760. /* Check for matching transaction ID */
  761. if ( dhcphdr->xid != dhcp_xid ( dhcp->netdev ) ) {
  762. DBGC ( dhcp, "DHCP %p wrong transaction ID (wanted %08lx, "
  763. "got %08lx)\n", dhcp, ntohl ( dhcphdr->xid ),
  764. ntohl ( dhcp_xid ( dhcp->netdev ) ) );
  765. return 0;
  766. };
  767. /* Parse packet and create options structure */
  768. options = dhcp_parse ( dhcphdr, len );
  769. if ( ! options ) {
  770. DBGC ( dhcp, "DHCP %p could not parse DHCP packet\n", dhcp );
  771. return -EINVAL;
  772. }
  773. /* Determine and verify message type */
  774. is_proxy = ( dhcphdr->yiaddr.s_addr == 0 );
  775. msgtype = find_dhcp_num_option ( options, DHCP_MESSAGE_TYPE );
  776. DBGC ( dhcp, "DHCP %p received %s%s\n", dhcp,
  777. ( is_proxy ? "Proxy" : "" ), dhcp_msgtype_name ( msgtype ) );
  778. if ( ( ( dhcp->state != DHCPDISCOVER ) || ( msgtype != DHCPOFFER ) ) &&
  779. ( ( dhcp->state != DHCPREQUEST ) || ( msgtype != DHCPACK ) ) ) {
  780. DBGC ( dhcp, "DHCP %p discarding %s while in %s state\n",
  781. dhcp, dhcp_msgtype_name ( msgtype ),
  782. dhcp_msgtype_name ( dhcp->state ) );
  783. goto out_discard;
  784. }
  785. /* Update stored standard/ProxyDHCP options, if the new
  786. * options have equal or higher priority than the
  787. * currently-stored options.
  788. */
  789. store_options = ( is_proxy ? &dhcp->proxy_options : &dhcp->options );
  790. if ( ( ! *store_options ) ||
  791. ( find_dhcp_num_option ( options, DHCP_EB_PRIORITY ) >=
  792. find_dhcp_num_option ( *store_options, DHCP_EB_PRIORITY ) ) ) {
  793. dhcpopt_put ( *store_options );
  794. *store_options = options;
  795. } else {
  796. dhcpopt_put ( options );
  797. }
  798. /* If we don't yet have a standard DHCP response (i.e. one
  799. * with an IP address), then just leave the timer running.
  800. */
  801. if ( ! dhcp->options )
  802. goto out;
  803. /* Handle DHCP response */
  804. ignore_proxy = find_dhcp_num_option ( dhcp->options,
  805. DHCP_EB_NO_PROXYDHCP );
  806. switch ( dhcp->state ) {
  807. case DHCPDISCOVER:
  808. /* If we have allowed sufficient time for ProxyDHCP
  809. * reponses, then transition to making the DHCPREQUEST.
  810. */
  811. elapsed = ( currticks() - dhcp->start );
  812. if ( ignore_proxy || ( elapsed > PROXYDHCP_WAIT_TIME ) ) {
  813. stop_timer ( &dhcp->timer );
  814. dhcp->state = DHCPREQUEST;
  815. dhcp_send_request ( dhcp );
  816. }
  817. break;
  818. case DHCPREQUEST:
  819. /* DHCP finished; register options and exit */
  820. if ( dhcp->proxy_options && ( ! ignore_proxy ) ) {
  821. dhcp->register_options ( dhcp->netdev,
  822. dhcp->proxy_options );
  823. }
  824. dhcp->register_options ( dhcp->netdev, dhcp->options );
  825. dhcp_finished ( dhcp, 0 );
  826. break;
  827. default:
  828. assert ( 0 );
  829. }
  830. out:
  831. return 0;
  832. out_discard:
  833. dhcpopt_put ( options );
  834. return 0;
  835. }
  836. /** DHCP data transfer interface operations */
  837. static struct xfer_interface_operations dhcp_xfer_operations = {
  838. .close = ignore_xfer_close,
  839. .vredirect = xfer_vopen,
  840. .seek = ignore_xfer_seek,
  841. .window = unlimited_xfer_window,
  842. .deliver_iob = xfer_deliver_as_raw,
  843. .deliver_raw = dhcp_deliver_raw,
  844. };
  845. /****************************************************************************
  846. *
  847. * Job control interface
  848. *
  849. */
  850. /**
  851. * Handle kill() event received via job control interface
  852. *
  853. * @v job DHCP job control interface
  854. */
  855. static void dhcp_job_kill ( struct job_interface *job ) {
  856. struct dhcp_session *dhcp =
  857. container_of ( job, struct dhcp_session, job );
  858. /* Terminate DHCP session */
  859. dhcp_finished ( dhcp, -ECANCELED );
  860. }
  861. /** DHCP job control interface operations */
  862. static struct job_interface_operations dhcp_job_operations = {
  863. .done = ignore_job_done,
  864. .kill = dhcp_job_kill,
  865. .progress = ignore_job_progress,
  866. };
  867. /****************************************************************************
  868. *
  869. * Instantiator
  870. *
  871. */
  872. /**
  873. * Start DHCP on a network device
  874. *
  875. * @v job Job control interface
  876. * @v netdev Network device
  877. * @v register_options DHCP option block registration routine
  878. * @ret rc Return status code
  879. *
  880. * Starts DHCP on the specified network device. If successful, the @c
  881. * register_options() routine will be called with the acquired
  882. * options.
  883. */
  884. int start_dhcp ( struct job_interface *job, struct net_device *netdev,
  885. int ( * register_options ) ( struct net_device *netdev,
  886. struct dhcp_option_block * ) ) {
  887. static struct sockaddr_in server = {
  888. .sin_family = AF_INET,
  889. .sin_addr.s_addr = INADDR_BROADCAST,
  890. .sin_port = htons ( BOOTPS_PORT ),
  891. };
  892. static struct sockaddr_in client = {
  893. .sin_family = AF_INET,
  894. .sin_port = htons ( BOOTPC_PORT ),
  895. };
  896. struct dhcp_session *dhcp;
  897. int rc;
  898. /* Allocate and initialise structure */
  899. dhcp = zalloc ( sizeof ( *dhcp ) );
  900. if ( ! dhcp )
  901. return -ENOMEM;
  902. dhcp->refcnt.free = dhcp_free;
  903. job_init ( &dhcp->job, &dhcp_job_operations, &dhcp->refcnt );
  904. xfer_init ( &dhcp->xfer, &dhcp_xfer_operations, &dhcp->refcnt );
  905. dhcp->netdev = netdev_get ( netdev );
  906. dhcp->register_options = register_options;
  907. dhcp->timer.expired = dhcp_timer_expired;
  908. dhcp->state = DHCPDISCOVER;
  909. dhcp->start = currticks();
  910. /* Instantiate child objects and attach to our interfaces */
  911. if ( ( rc = xfer_open_socket ( &dhcp->xfer, SOCK_DGRAM,
  912. ( struct sockaddr * ) &server,
  913. ( struct sockaddr * ) &client ) ) != 0 )
  914. goto err;
  915. /* Start timer to initiate initial DHCPREQUEST */
  916. start_timer_nodelay ( &dhcp->timer );
  917. /* Attach parent interface, mortalise self, and return */
  918. job_plug_plug ( &dhcp->job, job );
  919. ref_put ( &dhcp->refcnt );
  920. return 0;
  921. err:
  922. dhcp_finished ( dhcp, rc );
  923. ref_put ( &dhcp->refcnt );
  924. return rc;
  925. }
  926. /****************************************************************************
  927. *
  928. * Network device configurator
  929. *
  930. */
  931. /**
  932. * Configure network device from DHCP options
  933. *
  934. * @v netdev Network device
  935. * @v options DHCP options block
  936. * @ret rc Return status code
  937. */
  938. int dhcp_configure_netdev ( struct net_device *netdev,
  939. struct dhcp_option_block *options ) {
  940. struct in_addr address = { 0 };
  941. struct in_addr netmask = { 0 };
  942. struct in_addr gateway = { INADDR_NONE };
  943. int rc;
  944. /* Retrieve IP address configuration */
  945. find_dhcp_ipv4_option ( options, DHCP_EB_YIADDR, &address );
  946. find_dhcp_ipv4_option ( options, DHCP_SUBNET_MASK, &netmask );
  947. find_dhcp_ipv4_option ( options, DHCP_ROUTERS, &gateway );
  948. /* Do nothing unless we have at least an IP address to use */
  949. if ( ! address.s_addr )
  950. return 0;
  951. /* Clear any existing routing table entry */
  952. del_ipv4_address ( netdev );
  953. /* Set up new IP address configuration */
  954. if ( ( rc = add_ipv4_address ( netdev, address, netmask,
  955. gateway ) ) != 0 ) {
  956. DBG ( "Could not configure %s with DHCP results: %s\n",
  957. netdev->name, strerror ( rc ) );
  958. return rc;
  959. }
  960. return 0;
  961. }