2 * Copyright (C) 2006 Michael Brown <mbrown@fensystems.co.uk>.
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.
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.
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.
25 #include <gpxe/if_ether.h>
26 #include <gpxe/netdevice.h>
27 #include <gpxe/device.h>
28 #include <gpxe/xfer.h>
29 #include <gpxe/open.h>
31 #include <gpxe/retry.h>
32 #include <gpxe/tcpip.h>
34 #include <gpxe/uuid.h>
35 #include <gpxe/dhcp.h>
39 * Dynamic Host Configuration Protocol
43 /** DHCP operation types
45 * This table maps from DHCP message types (i.e. values of the @c
46 * DHCP_MESSAGE_TYPE option) to values of the "op" field within a DHCP
49 static const uint8_t dhcp_op[] = {
50 [DHCPDISCOVER] = BOOTP_REQUEST,
51 [DHCPOFFER] = BOOTP_REPLY,
52 [DHCPREQUEST] = BOOTP_REQUEST,
53 [DHCPDECLINE] = BOOTP_REQUEST,
54 [DHCPACK] = BOOTP_REPLY,
55 [DHCPNAK] = BOOTP_REPLY,
56 [DHCPRELEASE] = BOOTP_REQUEST,
57 [DHCPINFORM] = BOOTP_REQUEST,
60 /** Raw option data for options common to all DHCP requests */
61 static uint8_t dhcp_request_options_data[] = {
62 DHCP_MAX_MESSAGE_SIZE, DHCP_WORD ( ETH_MAX_MTU ),
64 DHCP_STRING ( 'E', 't', 'h', 'e', 'r', 'b', 'o', 'o', 't' ),
65 DHCP_PARAMETER_REQUEST_LIST,
66 DHCP_OPTION ( DHCP_SUBNET_MASK, DHCP_ROUTERS, DHCP_DNS_SERVERS, DHCP_LOG_SERVERS,
67 DHCP_HOST_NAME, DHCP_DOMAIN_NAME, DHCP_ROOT_PATH,
68 DHCP_VENDOR_ENCAP, DHCP_TFTP_SERVER_NAME,
69 DHCP_BOOTFILE_NAME, DHCP_EB_ENCAP,
70 DHCP_ISCSI_INITIATOR_IQN ),
74 /** DHCP feature codes */
75 static uint8_t dhcp_features[0] __table_start ( uint8_t, dhcp_features );
76 static uint8_t dhcp_features_end[0] __table_end ( uint8_t, dhcp_features );
79 * Name a DHCP packet type
81 * @v msgtype DHCP message type
82 * @ret string DHCP mesasge type name
84 static inline const char * dhcp_msgtype_name ( unsigned int msgtype ) {
86 case 0: return "BOOTP"; /* Non-DHCP packet */
87 case DHCPDISCOVER: return "DHCPDISCOVER";
88 case DHCPOFFER: return "DHCPOFFER";
89 case DHCPREQUEST: return "DHCPREQUEST";
90 case DHCPDECLINE: return "DHCPDECLINE";
91 case DHCPACK: return "DHCPACK";
92 case DHCPNAK: return "DHCPNAK";
93 case DHCPRELEASE: return "DHCPRELEASE";
94 case DHCPINFORM: return "DHCPINFORM";
95 default: return "DHCP<invalid>";
100 * Calculate DHCP transaction ID for a network device
102 * @v netdev Network device
105 * Extract the least significant bits of the hardware address for use
106 * as the transaction ID.
108 static uint32_t dhcp_xid ( struct net_device *netdev ) {
111 memcpy ( &xid, ( netdev->ll_addr + netdev->ll_protocol->ll_addr_len
112 - sizeof ( xid ) ), sizeof ( xid ) );
116 /** Options common to all DHCP requests */
117 static struct dhcp_option_block dhcp_request_options = {
118 .data = dhcp_request_options_data,
119 .max_len = sizeof ( dhcp_request_options_data ),
120 .len = sizeof ( dhcp_request_options_data ),
124 * Set option within DHCP packet
126 * @v dhcppkt DHCP packet
127 * @v tag DHCP option tag
128 * @v data New value for DHCP option
129 * @v len Length of value, in bytes
130 * @ret rc Return status code
132 * Sets the option within the first available options block within the
133 * DHCP packet. Option blocks are tried in the order specified by @c
134 * dhcp_option_block_fill_order.
136 * The magic options @c DHCP_EB_YIADDR and @c DHCP_EB_SIADDR are
137 * intercepted and inserted into the appropriate fixed fields within
138 * the DHCP packet. The option @c DHCP_OPTION_OVERLOAD is silently
139 * ignored, since our DHCP packet assembly method relies on always
140 * having option overloading in use.
142 static int set_dhcp_packet_option ( struct dhcp_packet *dhcppkt,
143 unsigned int tag, const void *data,
145 struct dhcphdr *dhcphdr = dhcppkt->dhcphdr;
146 struct dhcp_option_block *options = &dhcppkt->options;
147 struct dhcp_option *option = NULL;
149 /* Special-case the magic options */
151 case DHCP_OPTION_OVERLOAD:
152 /* Hard-coded in packets we create; always ignore */
155 memcpy ( &dhcphdr->yiaddr, data, sizeof ( dhcphdr->yiaddr ) );
158 memcpy ( &dhcphdr->siaddr, data, sizeof ( dhcphdr->siaddr ) );
160 case DHCP_TFTP_SERVER_NAME:
161 memset ( dhcphdr->sname, 0, sizeof ( dhcphdr->sname ) );
162 if ( len > sizeof ( dhcphdr->sname ) )
163 len = sizeof ( dhcphdr->sname );
164 memcpy ( dhcphdr->sname, data, len );
166 case DHCP_BOOTFILE_NAME:
167 memset ( dhcphdr->file, 0, sizeof ( dhcphdr->file ) );
168 if ( len > sizeof ( dhcphdr->file ) )
169 len = sizeof ( dhcphdr->file );
170 memcpy ( dhcphdr->file, data, len );
173 /* Continue processing as normal */
178 option = set_dhcp_option ( options, tag, data, len );
180 /* Update DHCP packet length */
181 dhcppkt->len = ( offsetof ( typeof ( *dhcppkt->dhcphdr ), options )
182 + dhcppkt->options.len );
184 return ( option ? 0 : -ENOSPC );
188 * Copy option into DHCP packet
190 * @v dhcppkt DHCP packet
191 * @v options DHCP option block, or NULL
192 * @v tag DHCP option tag to search for
193 * @v new_tag DHCP option tag to use for copied option
194 * @ret rc Return status code
196 * Copies a single option, if present, from the DHCP options block
197 * into a DHCP packet. The tag for the option may be changed if
198 * desired; this is required by other parts of the DHCP code.
200 * @c options may specify a single options block, or be left as NULL
201 * in order to search for the option within all registered options
204 static int copy_dhcp_packet_option ( struct dhcp_packet *dhcppkt,
205 struct dhcp_option_block *options,
206 unsigned int tag, unsigned int new_tag ) {
207 struct dhcp_option *option;
210 option = find_dhcp_option ( options, tag );
212 if ( ( rc = set_dhcp_packet_option ( dhcppkt, new_tag,
214 option->len ) ) != 0 )
221 * Copy options into DHCP packet
223 * @v dhcppkt DHCP packet
224 * @v options DHCP option block, or NULL
225 * @v encapsulator Encapsulating option, or zero
226 * @ret rc Return status code
228 * Copies options with the specified encapsulator from DHCP options
229 * blocks into a DHCP packet. Most options are copied verbatim.
230 * Recognised encapsulated options fields are handled as such.
232 * @c options may specify a single options block, or be left as NULL
233 * in order to copy options from all registered options blocks.
235 static int copy_dhcp_packet_encap_options ( struct dhcp_packet *dhcppkt,
236 struct dhcp_option_block *options,
237 unsigned int encapsulator ) {
242 for ( subtag = DHCP_MIN_OPTION; subtag <= DHCP_MAX_OPTION; subtag++ ) {
243 tag = DHCP_ENCAP_OPT ( encapsulator, subtag );
246 case DHCP_VENDOR_ENCAP:
247 /* Process encapsulated options field */
248 if ( ( rc = copy_dhcp_packet_encap_options ( dhcppkt,
254 /* Copy option to reassembled packet */
255 if ( ( rc = copy_dhcp_packet_option ( dhcppkt, options,
266 * Copy options into DHCP packet
268 * @v dhcppkt DHCP packet
269 * @v options DHCP option block, or NULL
270 * @ret rc Return status code
272 * Copies options from DHCP options blocks into a DHCP packet. Most
273 * options are copied verbatim. Recognised encapsulated options
274 * fields are handled as such.
276 * @c options may specify a single options block, or be left as NULL
277 * in order to copy options from all registered options blocks.
279 static int copy_dhcp_packet_options ( struct dhcp_packet *dhcppkt,
280 struct dhcp_option_block *options ) {
281 return copy_dhcp_packet_encap_options ( dhcppkt, options, 0 );
285 * Create a DHCP packet
287 * @v netdev Network device
288 * @v msgtype DHCP message type
289 * @v data Buffer for DHCP packet
290 * @v max_len Size of DHCP packet buffer
291 * @v dhcppkt DHCP packet structure to fill in
292 * @ret rc Return status code
294 * Creates a DHCP packet in the specified buffer, and fills out a @c
295 * dhcp_packet structure that can be passed to
296 * set_dhcp_packet_option() or copy_dhcp_packet_options().
298 static int create_dhcp_packet ( struct net_device *netdev, uint8_t msgtype,
299 void *data, size_t max_len,
300 struct dhcp_packet *dhcppkt ) {
301 struct dhcphdr *dhcphdr = data;
306 if ( max_len < sizeof ( *dhcphdr ) )
309 /* Initialise DHCP packet content */
310 memset ( dhcphdr, 0, max_len );
311 dhcphdr->xid = dhcp_xid ( netdev );
312 dhcphdr->magic = htonl ( DHCP_MAGIC_COOKIE );
313 dhcphdr->htype = ntohs ( netdev->ll_protocol->ll_proto );
314 dhcphdr->op = dhcp_op[msgtype];
315 /* If hardware length exceeds the chaddr field length, don't
316 * use the chaddr field. This is as per RFC4390.
318 hlen = netdev->ll_protocol->ll_addr_len;
319 if ( hlen > sizeof ( dhcphdr->chaddr ) ) {
321 dhcphdr->flags = htons ( BOOTP_FL_BROADCAST );
323 dhcphdr->hlen = hlen;
324 memcpy ( dhcphdr->chaddr, netdev->ll_addr, hlen );
326 /* Initialise DHCP packet structure */
327 dhcppkt->dhcphdr = dhcphdr;
328 dhcppkt->max_len = max_len;
329 init_dhcp_options ( &dhcppkt->options, dhcphdr->options,
331 offsetof ( typeof ( *dhcphdr ), options ) ) );
333 /* Set DHCP_MESSAGE_TYPE option */
334 if ( ( rc = set_dhcp_packet_option ( dhcppkt, DHCP_MESSAGE_TYPE,
336 sizeof ( msgtype ) ) ) != 0 )
343 * Calculate used length of a field containing DHCP options
345 * @v data Field containing DHCP options
346 * @v max_len Field length
347 * @ret len Used length (excluding the @c DHCP_END tag)
349 static size_t dhcp_field_len ( const void *data, size_t max_len ) {
350 struct dhcp_option_block options;
351 struct dhcp_option *end;
353 options.data = ( ( void * ) data );
354 options.len = max_len;
355 end = find_dhcp_option ( &options, DHCP_END );
356 return ( end ? ( ( ( void * ) end ) - data ) : 0 );
360 * Merge field containing DHCP options or string into DHCP options block
362 * @v options DHCP option block
363 * @v data Field containing DHCP options
364 * @v max_len Field length
365 * @v tag DHCP option tag, or 0
367 * If @c tag is non-zero, the field will be treated as a
368 * NUL-terminated string representing the value of the specified DHCP
369 * option. If @c tag is zero, the field will be treated as a block of
370 * DHCP options, and simply appended to the existing options in the
373 * The caller must ensure that there is enough space in the options
374 * block to perform the merge.
376 static void merge_dhcp_field ( struct dhcp_option_block *options,
377 const void *data, size_t max_len,
381 struct dhcp_option *end;
384 set_dhcp_option ( options, tag, data, strlen ( data ) );
386 len = dhcp_field_len ( data, max_len );
387 dest = ( options->data + options->len - 1 );
388 memcpy ( dest, data, len );
390 end = ( dest + len );
396 * Parse DHCP packet and construct DHCP options block
398 * @v dhcphdr DHCP packet
399 * @v len Length of DHCP packet
400 * @ret options DHCP options block, or NULL
402 * Parses a received DHCP packet and canonicalises its contents into a
403 * single DHCP options block. The "file" and "sname" fields are
404 * converted into the corresponding DHCP options (@c
405 * DHCP_BOOTFILE_NAME and @c DHCP_TFTP_SERVER_NAME respectively). If
406 * these fields are used for option overloading, their options are
407 * merged in to the options block.
409 * The values of the "yiaddr" and "siaddr" fields will be stored
410 * within the options block as the magic options @c DHCP_EB_YIADDR and
413 * Note that this call allocates new memory for the constructed DHCP
414 * options block; it is the responsibility of the caller to eventually
417 static struct dhcp_option_block * dhcp_parse ( const struct dhcphdr *dhcphdr,
419 struct dhcp_option_block *options;
421 unsigned int overloading;
424 if ( len < sizeof ( *dhcphdr ) )
427 /* Calculate size of resulting concatenated option block:
429 * The "options" field : length of the field minus the DHCP_END tag.
431 * The "file" field : maximum length of the field minus the
432 * NUL terminator, plus a 2-byte DHCP header or, if used for
433 * option overloading, the length of the field minus the
436 * The "sname" field : as for the "file" field.
438 * 15 bytes for an encapsulated options field to contain the
439 * value of the "yiaddr" and "siaddr" fields
441 * 1 byte for a final terminating DHCP_END tag.
443 options_len = ( ( len - offsetof ( typeof ( *dhcphdr ), options ) ) - 1
444 + ( sizeof ( dhcphdr->file ) + 1 )
445 + ( sizeof ( dhcphdr->sname ) + 1 )
446 + 15 /* yiaddr and siaddr */
447 + 1 /* DHCP_END tag */ );
449 /* Allocate empty options block of required size */
450 options = alloc_dhcp_options ( options_len );
452 DBG ( "DHCP could not allocate %d-byte option block\n",
457 /* Merge in "options" field, if this is a DHCP packet */
458 if ( dhcphdr->magic == htonl ( DHCP_MAGIC_COOKIE ) ) {
459 merge_dhcp_field ( options, dhcphdr->options,
461 offsetof ( typeof (*dhcphdr), options ) ),
462 0 /* Always contains options */ );
465 /* Identify overloaded fields */
466 overloading = find_dhcp_num_option ( options, DHCP_OPTION_OVERLOAD );
468 /* Merge in "file" and "sname" fields */
469 merge_dhcp_field ( options, dhcphdr->file, sizeof ( dhcphdr->file ),
470 ( ( overloading & DHCP_OPTION_OVERLOAD_FILE ) ?
471 0 : DHCP_BOOTFILE_NAME ) );
472 merge_dhcp_field ( options, dhcphdr->sname, sizeof ( dhcphdr->sname ),
473 ( ( overloading & DHCP_OPTION_OVERLOAD_SNAME ) ?
474 0 : DHCP_TFTP_SERVER_NAME ) );
476 /* Set magic options for "yiaddr" and "siaddr", if present */
477 if ( dhcphdr->yiaddr.s_addr ) {
478 set_dhcp_option ( options, DHCP_EB_YIADDR,
479 &dhcphdr->yiaddr, sizeof (dhcphdr->yiaddr) );
481 if ( dhcphdr->siaddr.s_addr ) {
482 set_dhcp_option ( options, DHCP_EB_SIADDR,
483 &dhcphdr->siaddr, sizeof (dhcphdr->siaddr) );
486 assert ( options->len <= options->max_len );
491 /****************************************************************************
493 * Whole-packet construction
497 /** DHCP network device descriptor */
498 struct dhcp_netdev_desc {
505 } __attribute__ (( packed ));
507 /** DHCP client identifier */
508 struct dhcp_client_id {
509 /** Link-layer protocol */
511 /** Link-layer address */
512 uint8_t ll_addr[MAX_LL_ADDR_LEN];
513 } __attribute__ (( packed ));
516 * Create DHCP request
518 * @v netdev Network device
519 * @v msgtype DHCP message type
520 * @v options DHCP server response options, or NULL
521 * @v data Buffer for DHCP packet
522 * @v max_len Size of DHCP packet buffer
523 * @v dhcppkt DHCP packet structure to fill in
524 * @ret rc Return status code
526 int create_dhcp_request ( struct net_device *netdev, int msgtype,
527 struct dhcp_option_block *options,
528 void *data, size_t max_len,
529 struct dhcp_packet *dhcppkt ) {
530 struct device_description *desc = &netdev->dev->desc;
531 struct dhcp_netdev_desc dhcp_desc;
532 struct dhcp_client_id client_id;
534 size_t dhcp_features_len;
538 /* Create DHCP packet */
539 if ( ( rc = create_dhcp_packet ( netdev, msgtype, data, max_len,
541 DBG ( "DHCP could not create DHCP packet: %s\n",
546 /* Copy in options common to all requests */
547 if ( ( rc = copy_dhcp_packet_options ( dhcppkt,
548 &dhcp_request_options )) !=0 ){
549 DBG ( "DHCP could not set common DHCP options: %s\n",
554 /* Copy any required options from previous server repsonse */
556 if ( ( rc = copy_dhcp_packet_option ( dhcppkt, options,
557 DHCP_SERVER_IDENTIFIER,
558 DHCP_SERVER_IDENTIFIER ) ) != 0 ) {
559 DBG ( "DHCP could not set server identifier "
560 "option: %s\n", strerror ( rc ) );
563 if ( ( rc = copy_dhcp_packet_option ( dhcppkt, options,
565 DHCP_REQUESTED_ADDRESS ) ) != 0 ) {
566 DBG ( "DHCP could not set requested address "
567 "option: %s\n", strerror ( rc ) );
572 /* Add options to identify the feature list */
573 dhcp_features_len = ( dhcp_features_end - dhcp_features );
574 if ( ( rc = set_dhcp_packet_option ( dhcppkt, DHCP_EB_ENCAP,
576 dhcp_features_len ) ) != 0 ) {
577 DBG ( "DHCP could not set features list option: %s\n",
582 /* Add options to identify the network device */
583 dhcp_desc.type = desc->bus_type;
584 dhcp_desc.vendor = htons ( desc->vendor );
585 dhcp_desc.device = htons ( desc->device );
586 if ( ( rc = set_dhcp_packet_option ( dhcppkt, DHCP_EB_BUS_ID,
588 sizeof ( dhcp_desc ) ) ) != 0 ) {
589 DBG ( "DHCP could not set bus ID option: %s\n",
594 /* Add DHCP client identifier. Required for Infiniband, and
595 * doesn't hurt other link layers.
597 client_id.ll_proto = netdev->ll_protocol->ll_proto;
598 ll_addr_len = netdev->ll_protocol->ll_addr_len;
599 assert ( ll_addr_len <= sizeof ( client_id.ll_addr ) );
600 memcpy ( client_id.ll_addr, netdev->ll_addr, ll_addr_len );
601 if ( ( rc = set_dhcp_packet_option ( dhcppkt, DHCP_CLIENT_ID,
603 ( ll_addr_len + 1 ) ) ) != 0 ) {
604 DBG ( "DHCP could not set client ID: %s\n",
609 /* Add client UUID, if we have one. Required for PXE. */
610 if ( ( rc = get_uuid ( &uuid ) ) == 0 ) {
611 if ( ( rc = set_dhcp_packet_option ( dhcppkt,
612 DHCP_CLIENT_UUID, &uuid,
613 sizeof ( uuid ) ) ) !=0){
614 DBG ( "DHCP could not set client UUID: %s\n",
624 * Create DHCP response
626 * @v netdev Network device
627 * @v msgtype DHCP message type
628 * @v options DHCP options, or NULL
629 * @v data Buffer for DHCP packet
630 * @v max_len Size of DHCP packet buffer
631 * @v dhcppkt DHCP packet structure to fill in
632 * @ret rc Return status code
634 int create_dhcp_response ( struct net_device *netdev, int msgtype,
635 struct dhcp_option_block *options,
636 void *data, size_t max_len,
637 struct dhcp_packet *dhcppkt ) {
640 /* Create packet and copy in options */
641 if ( ( rc = create_dhcp_packet ( netdev, msgtype, data, max_len,
643 DBG ( " failed to build packet" );
646 if ( ( rc = copy_dhcp_packet_options ( dhcppkt, options ) ) != 0 ) {
647 DBG ( " failed to copy options" );
654 /****************************************************************************
656 * DHCP to UDP interface
660 /** A DHCP session */
661 struct dhcp_session {
662 /** Reference counter */
663 struct refcnt refcnt;
664 /** Job control interface */
665 struct job_interface job;
666 /** Data transfer interface */
667 struct xfer_interface xfer;
669 /** Network device being configured */
670 struct net_device *netdev;
671 /** Option block registration routine */
672 int ( * register_options ) ( struct net_device *netdev,
673 struct dhcp_option_block *options );
675 /** State of the session
677 * This is a value for the @c DHCP_MESSAGE_TYPE option
678 * (e.g. @c DHCPDISCOVER).
681 /** Options obtained from server */
682 struct dhcp_option_block *options;
683 /** Retransmission timer */
684 struct retry_timer timer;
690 * @v refcnt Reference counter
692 static void dhcp_free ( struct refcnt *refcnt ) {
693 struct dhcp_session *dhcp =
694 container_of ( refcnt, struct dhcp_session, refcnt );
696 netdev_put ( dhcp->netdev );
697 dhcpopt_put ( dhcp->options );
702 * Mark DHCP session as complete
704 * @v dhcp DHCP session
705 * @v rc Return status code
707 static void dhcp_finished ( struct dhcp_session *dhcp, int rc ) {
709 /* Block futher incoming messages */
710 job_nullify ( &dhcp->job );
711 xfer_nullify ( &dhcp->xfer );
713 /* Stop retry timer */
714 stop_timer ( &dhcp->timer );
716 /* Free resources and close interfaces */
717 xfer_close ( &dhcp->xfer, rc );
718 job_done ( &dhcp->job, rc );
721 /****************************************************************************
723 * Data transfer interface
728 * Transmit DHCP request
730 * @v dhcp DHCP session
731 * @ret rc Return status code
733 static int dhcp_send_request ( struct dhcp_session *dhcp ) {
734 struct xfer_metadata meta = {
735 .netdev = dhcp->netdev,
737 struct io_buffer *iobuf;
738 struct dhcp_packet dhcppkt;
741 DBGC ( dhcp, "DHCP %p transmitting %s\n",
742 dhcp, dhcp_msgtype_name ( dhcp->state ) );
744 assert ( ( dhcp->state == DHCPDISCOVER ) ||
745 ( dhcp->state == DHCPREQUEST ) );
747 /* Start retry timer. Do this first so that failures to
748 * transmit will be retried.
750 start_timer ( &dhcp->timer );
752 /* Allocate buffer for packet */
753 iobuf = xfer_alloc_iob ( &dhcp->xfer, DHCP_MIN_LEN );
757 /* Create DHCP packet in temporary buffer */
758 if ( ( rc = create_dhcp_request ( dhcp->netdev, dhcp->state,
759 dhcp->options, iobuf->data,
760 iob_tailroom ( iobuf ),
761 &dhcppkt ) ) != 0 ) {
762 DBGC ( dhcp, "DHCP %p could not construct DHCP request: %s\n",
763 dhcp, strerror ( rc ) );
767 /* Transmit the packet */
768 iob_put ( iobuf, dhcppkt.len );
769 rc = xfer_deliver_iob_meta ( &dhcp->xfer, iobuf, &meta );
772 DBGC ( dhcp, "DHCP %p could not transmit UDP packet: %s\n",
773 dhcp, strerror ( rc ) );
783 * Handle DHCP retry timer expiry
785 * @v timer DHCP retry timer
786 * @v fail Failure indicator
788 static void dhcp_timer_expired ( struct retry_timer *timer, int fail ) {
789 struct dhcp_session *dhcp =
790 container_of ( timer, struct dhcp_session, timer );
793 dhcp_finished ( dhcp, -ETIMEDOUT );
795 dhcp_send_request ( dhcp );
802 * @v xfer Data transfer interface
803 * @v iobuf I/O buffer
804 * @v data Received data
805 * @v len Length of received data
806 * @ret rc Return status code
808 static int dhcp_deliver_raw ( struct xfer_interface *xfer,
809 const void *data, size_t len ) {
810 struct dhcp_session *dhcp =
811 container_of ( xfer, struct dhcp_session, xfer );
812 const struct dhcphdr *dhcphdr = data;
813 struct dhcp_option_block *options;
814 unsigned int msgtype;
816 /* Check for matching transaction ID */
817 if ( dhcphdr->xid != dhcp_xid ( dhcp->netdev ) ) {
818 DBGC ( dhcp, "DHCP %p wrong transaction ID (wanted %08lx, "
819 "got %08lx)\n", dhcp, ntohl ( dhcphdr->xid ),
820 ntohl ( dhcp_xid ( dhcp->netdev ) ) );
824 /* Parse packet and create options structure */
825 options = dhcp_parse ( dhcphdr, len );
827 DBGC ( dhcp, "DHCP %p could not parse DHCP packet\n", dhcp );
831 /* Determine message type */
832 msgtype = find_dhcp_num_option ( options, DHCP_MESSAGE_TYPE );
833 DBGC ( dhcp, "DHCP %p received %s\n",
834 dhcp, dhcp_msgtype_name ( msgtype ) );
836 /* Handle DHCP reply */
837 switch ( dhcp->state ) {
839 if ( msgtype != DHCPOFFER )
841 dhcp->state = DHCPREQUEST;
844 if ( msgtype != DHCPACK )
846 dhcp->state = DHCPACK;
853 /* Stop timer and update stored options */
854 stop_timer ( &dhcp->timer );
856 dhcpopt_put ( dhcp->options );
857 dhcp->options = options;
859 /* Transmit next packet, or terminate session */
860 if ( dhcp->state < DHCPACK ) {
861 dhcp_send_request ( dhcp );
863 dhcp->register_options ( dhcp->netdev, dhcp->options );
864 dhcp_finished ( dhcp, 0 );
869 dhcpopt_put ( options );
873 /** DHCP data transfer interface operations */
874 static struct xfer_interface_operations dhcp_xfer_operations = {
875 .close = ignore_xfer_close,
876 .vredirect = xfer_vopen,
877 .seek = ignore_xfer_seek,
878 .window = unlimited_xfer_window,
879 .deliver_iob = xfer_deliver_as_raw,
880 .deliver_raw = dhcp_deliver_raw,
883 /****************************************************************************
885 * Job control interface
890 * Handle kill() event received via job control interface
892 * @v job DHCP job control interface
894 static void dhcp_job_kill ( struct job_interface *job ) {
895 struct dhcp_session *dhcp =
896 container_of ( job, struct dhcp_session, job );
898 /* Terminate DHCP session */
899 dhcp_finished ( dhcp, -ECANCELED );
902 /** DHCP job control interface operations */
903 static struct job_interface_operations dhcp_job_operations = {
904 .done = ignore_job_done,
905 .kill = dhcp_job_kill,
906 .progress = ignore_job_progress,
909 /****************************************************************************
916 * Start DHCP on a network device
918 * @v job Job control interface
919 * @v netdev Network device
920 * @v register_options DHCP option block registration routine
921 * @ret rc Return status code
923 * Starts DHCP on the specified network device. If successful, the @c
924 * register_options() routine will be called with the acquired
927 int start_dhcp ( struct job_interface *job, struct net_device *netdev,
928 int ( * register_options ) ( struct net_device *netdev,
929 struct dhcp_option_block * ) ) {
930 static struct sockaddr_in server = {
931 .sin_family = AF_INET,
932 .sin_addr.s_addr = INADDR_BROADCAST,
933 .sin_port = htons ( BOOTPS_PORT ),
935 static struct sockaddr_in client = {
936 .sin_family = AF_INET,
937 .sin_port = htons ( BOOTPC_PORT ),
939 struct dhcp_session *dhcp;
942 /* Allocate and initialise structure */
943 dhcp = zalloc ( sizeof ( *dhcp ) );
946 dhcp->refcnt.free = dhcp_free;
947 job_init ( &dhcp->job, &dhcp_job_operations, &dhcp->refcnt );
948 xfer_init ( &dhcp->xfer, &dhcp_xfer_operations, &dhcp->refcnt );
949 dhcp->netdev = netdev_get ( netdev );
950 dhcp->register_options = register_options;
951 dhcp->timer.expired = dhcp_timer_expired;
952 dhcp->state = DHCPDISCOVER;
954 /* Instantiate child objects and attach to our interfaces */
955 if ( ( rc = xfer_open_socket ( &dhcp->xfer, SOCK_DGRAM,
956 ( struct sockaddr * ) &server,
957 ( struct sockaddr * ) &client ) ) != 0 )
960 /* Start timer to initiate initial DHCPREQUEST */
961 start_timer_nodelay ( &dhcp->timer );
963 /* Attach parent interface, mortalise self, and return */
964 job_plug_plug ( &dhcp->job, job );
965 ref_put ( &dhcp->refcnt );
969 dhcp_finished ( dhcp, rc );
970 ref_put ( &dhcp->refcnt );
974 /****************************************************************************
976 * Network device configurator
981 * Configure network device from DHCP options
983 * @v netdev Network device
984 * @v options DHCP options block
985 * @ret rc Return status code
987 int dhcp_configure_netdev ( struct net_device *netdev,
988 struct dhcp_option_block *options ) {
989 struct in_addr address = { 0 };
990 struct in_addr netmask = { 0 };
991 struct in_addr gateway = { INADDR_NONE };
994 /* Clear any existing routing table entry */
995 del_ipv4_address ( netdev );
997 /* Retrieve IP address configuration */
998 find_dhcp_ipv4_option ( options, DHCP_EB_YIADDR, &address );
999 find_dhcp_ipv4_option ( options, DHCP_SUBNET_MASK, &netmask );
1000 find_dhcp_ipv4_option ( options, DHCP_ROUTERS, &gateway );
1002 /* Set up new IP address configuration */
1003 if ( ( rc = add_ipv4_address ( netdev, address, netmask,
1004 gateway ) ) != 0 ) {
1005 DBG ( "Could not configure %s with DHCP results: %s\n",
1006 netdev->name, strerror ( rc ) );