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 ( 'P', 'X', 'E', 'C', 'l', 'i', 'e', 'n', 't', ':',
65 'A', 'r', 'c', 'h', ':', '0', '0', '0', '0', '0', ':',
66 'U', 'N', 'D', 'I', ':', '0', '0', '2', '0', '0', '1' ),
67 DHCP_CLIENT_ARCHITECTURE, DHCP_WORD ( 0 ),
68 DHCP_CLIENT_NDI, DHCP_OPTION ( 1 /* UNDI */ , 2, 1 /* v2.1 */ ),
69 DHCP_PARAMETER_REQUEST_LIST,
70 DHCP_OPTION ( DHCP_SUBNET_MASK, DHCP_ROUTERS, DHCP_DNS_SERVERS,
71 DHCP_LOG_SERVERS, DHCP_HOST_NAME, DHCP_DOMAIN_NAME,
72 DHCP_ROOT_PATH, DHCP_VENDOR_ENCAP, DHCP_VENDOR_CLASS_ID,
73 DHCP_TFTP_SERVER_NAME, DHCP_BOOTFILE_NAME,
74 DHCP_EB_ENCAP, DHCP_ISCSI_INITIATOR_IQN ),
78 /** DHCP feature codes */
79 static uint8_t dhcp_features[0] __table_start ( uint8_t, dhcp_features );
80 static uint8_t dhcp_features_end[0] __table_end ( uint8_t, dhcp_features );
83 * Name a DHCP packet type
85 * @v msgtype DHCP message type
86 * @ret string DHCP mesasge type name
88 static inline const char * dhcp_msgtype_name ( unsigned int msgtype ) {
90 case 0: return "BOOTP"; /* Non-DHCP packet */
91 case DHCPDISCOVER: return "DHCPDISCOVER";
92 case DHCPOFFER: return "DHCPOFFER";
93 case DHCPREQUEST: return "DHCPREQUEST";
94 case DHCPDECLINE: return "DHCPDECLINE";
95 case DHCPACK: return "DHCPACK";
96 case DHCPNAK: return "DHCPNAK";
97 case DHCPRELEASE: return "DHCPRELEASE";
98 case DHCPINFORM: return "DHCPINFORM";
99 default: return "DHCP<invalid>";
104 * Calculate DHCP transaction ID for a network device
106 * @v netdev Network device
109 * Extract the least significant bits of the hardware address for use
110 * as the transaction ID.
112 static uint32_t dhcp_xid ( struct net_device *netdev ) {
115 memcpy ( &xid, ( netdev->ll_addr + netdev->ll_protocol->ll_addr_len
116 - sizeof ( xid ) ), sizeof ( xid ) );
120 /** Options common to all DHCP requests */
121 static struct dhcp_option_block dhcp_request_options = {
122 .data = dhcp_request_options_data,
123 .max_len = sizeof ( dhcp_request_options_data ),
124 .len = sizeof ( dhcp_request_options_data ),
128 * Set option within DHCP packet
130 * @v dhcppkt DHCP packet
131 * @v tag DHCP option tag
132 * @v data New value for DHCP option
133 * @v len Length of value, in bytes
134 * @ret rc Return status code
136 * Sets the option within the first available options block within the
137 * DHCP packet. Option blocks are tried in the order specified by @c
138 * dhcp_option_block_fill_order.
140 * The magic options @c DHCP_EB_YIADDR and @c DHCP_EB_SIADDR are
141 * intercepted and inserted into the appropriate fixed fields within
142 * the DHCP packet. The option @c DHCP_OPTION_OVERLOAD is silently
143 * ignored, since our DHCP packet assembly method relies on always
144 * having option overloading in use.
146 static int set_dhcp_packet_option ( struct dhcp_packet *dhcppkt,
147 unsigned int tag, const void *data,
149 struct dhcphdr *dhcphdr = dhcppkt->dhcphdr;
150 struct dhcp_option_block *options = &dhcppkt->options;
151 struct dhcp_option *option = NULL;
153 /* Special-case the magic options */
155 case DHCP_OPTION_OVERLOAD:
156 /* Hard-coded in packets we create; always ignore */
159 memcpy ( &dhcphdr->yiaddr, data, sizeof ( dhcphdr->yiaddr ) );
162 memcpy ( &dhcphdr->siaddr, data, sizeof ( dhcphdr->siaddr ) );
164 case DHCP_TFTP_SERVER_NAME:
165 memset ( dhcphdr->sname, 0, sizeof ( dhcphdr->sname ) );
166 if ( len > sizeof ( dhcphdr->sname ) )
167 len = sizeof ( dhcphdr->sname );
168 memcpy ( dhcphdr->sname, data, len );
170 case DHCP_BOOTFILE_NAME:
171 memset ( dhcphdr->file, 0, sizeof ( dhcphdr->file ) );
172 if ( len > sizeof ( dhcphdr->file ) )
173 len = sizeof ( dhcphdr->file );
174 memcpy ( dhcphdr->file, data, len );
177 /* Continue processing as normal */
182 option = set_dhcp_option ( options, tag, data, len );
184 /* Update DHCP packet length */
185 dhcppkt->len = ( offsetof ( typeof ( *dhcppkt->dhcphdr ), options )
186 + dhcppkt->options.len );
188 return ( option ? 0 : -ENOSPC );
192 * Copy option into DHCP packet
194 * @v dhcppkt DHCP packet
195 * @v options DHCP option block, or NULL
196 * @v tag DHCP option tag to search for
197 * @v new_tag DHCP option tag to use for copied option
198 * @ret rc Return status code
200 * Copies a single option, if present, from the DHCP options block
201 * into a DHCP packet. The tag for the option may be changed if
202 * desired; this is required by other parts of the DHCP code.
204 * @c options may specify a single options block, or be left as NULL
205 * in order to search for the option within all registered options
208 static int copy_dhcp_packet_option ( struct dhcp_packet *dhcppkt,
209 struct dhcp_option_block *options,
210 unsigned int tag, unsigned int new_tag ) {
211 struct dhcp_option *option;
214 option = find_dhcp_option ( options, tag );
216 if ( ( rc = set_dhcp_packet_option ( dhcppkt, new_tag,
218 option->len ) ) != 0 )
225 * Copy options into DHCP packet
227 * @v dhcppkt DHCP packet
228 * @v options DHCP option block, or NULL
229 * @v encapsulator Encapsulating option, or zero
230 * @ret rc Return status code
232 * Copies options with the specified encapsulator from DHCP options
233 * blocks into a DHCP packet. Most options are copied verbatim.
234 * Recognised encapsulated options fields are handled as such.
236 * @c options may specify a single options block, or be left as NULL
237 * in order to copy options from all registered options blocks.
239 static int copy_dhcp_packet_encap_options ( struct dhcp_packet *dhcppkt,
240 struct dhcp_option_block *options,
241 unsigned int encapsulator ) {
246 for ( subtag = DHCP_MIN_OPTION; subtag <= DHCP_MAX_OPTION; subtag++ ) {
247 tag = DHCP_ENCAP_OPT ( encapsulator, subtag );
250 case DHCP_VENDOR_ENCAP:
251 /* Process encapsulated options field */
252 if ( ( rc = copy_dhcp_packet_encap_options ( dhcppkt,
258 /* Copy option to reassembled packet */
259 if ( ( rc = copy_dhcp_packet_option ( dhcppkt, options,
270 * Copy options into DHCP packet
272 * @v dhcppkt DHCP packet
273 * @v options DHCP option block, or NULL
274 * @ret rc Return status code
276 * Copies options from DHCP options blocks into a DHCP packet. Most
277 * options are copied verbatim. Recognised encapsulated options
278 * fields are handled as such.
280 * @c options may specify a single options block, or be left as NULL
281 * in order to copy options from all registered options blocks.
283 static int copy_dhcp_packet_options ( struct dhcp_packet *dhcppkt,
284 struct dhcp_option_block *options ) {
285 return copy_dhcp_packet_encap_options ( dhcppkt, options, 0 );
289 * Create a DHCP packet
291 * @v netdev Network device
292 * @v msgtype DHCP message type
293 * @v data Buffer for DHCP packet
294 * @v max_len Size of DHCP packet buffer
295 * @v dhcppkt DHCP packet structure to fill in
296 * @ret rc Return status code
298 * Creates a DHCP packet in the specified buffer, and fills out a @c
299 * dhcp_packet structure that can be passed to
300 * set_dhcp_packet_option() or copy_dhcp_packet_options().
302 static int create_dhcp_packet ( struct net_device *netdev, uint8_t msgtype,
303 void *data, size_t max_len,
304 struct dhcp_packet *dhcppkt ) {
305 struct dhcphdr *dhcphdr = data;
310 if ( max_len < sizeof ( *dhcphdr ) )
313 /* Initialise DHCP packet content */
314 memset ( dhcphdr, 0, max_len );
315 dhcphdr->xid = dhcp_xid ( netdev );
316 dhcphdr->magic = htonl ( DHCP_MAGIC_COOKIE );
317 dhcphdr->htype = ntohs ( netdev->ll_protocol->ll_proto );
318 dhcphdr->op = dhcp_op[msgtype];
319 /* If hardware length exceeds the chaddr field length, don't
320 * use the chaddr field. This is as per RFC4390.
322 hlen = netdev->ll_protocol->ll_addr_len;
323 if ( hlen > sizeof ( dhcphdr->chaddr ) ) {
325 dhcphdr->flags = htons ( BOOTP_FL_BROADCAST );
327 dhcphdr->hlen = hlen;
328 memcpy ( dhcphdr->chaddr, netdev->ll_addr, hlen );
330 /* Initialise DHCP packet structure */
331 dhcppkt->dhcphdr = dhcphdr;
332 dhcppkt->max_len = max_len;
333 init_dhcp_options ( &dhcppkt->options, dhcphdr->options,
335 offsetof ( typeof ( *dhcphdr ), options ) ) );
337 /* Set DHCP_MESSAGE_TYPE option */
338 if ( ( rc = set_dhcp_packet_option ( dhcppkt, DHCP_MESSAGE_TYPE,
340 sizeof ( msgtype ) ) ) != 0 )
347 * Calculate used length of a field containing DHCP options
349 * @v data Field containing DHCP options
350 * @v max_len Field length
351 * @ret len Used length (excluding the @c DHCP_END tag)
353 static size_t dhcp_field_len ( const void *data, size_t max_len ) {
354 struct dhcp_option_block options;
355 struct dhcp_option *end;
357 options.data = ( ( void * ) data );
358 options.len = max_len;
359 end = find_dhcp_option ( &options, DHCP_END );
360 return ( end ? ( ( ( void * ) end ) - data ) : 0 );
364 * Merge field containing DHCP options or string into DHCP options block
366 * @v options DHCP option block
367 * @v data Field containing DHCP options
368 * @v max_len Field length
369 * @v tag DHCP option tag, or 0
371 * If @c tag is non-zero, the field will be treated as a
372 * NUL-terminated string representing the value of the specified DHCP
373 * option. If @c tag is zero, the field will be treated as a block of
374 * DHCP options, and simply appended to the existing options in the
377 * The caller must ensure that there is enough space in the options
378 * block to perform the merge.
380 static void merge_dhcp_field ( struct dhcp_option_block *options,
381 const void *data, size_t max_len,
385 struct dhcp_option *end;
388 set_dhcp_option ( options, tag, data, strlen ( data ) );
390 len = dhcp_field_len ( data, max_len );
391 dest = ( options->data + options->len - 1 );
392 memcpy ( dest, data, len );
394 end = ( dest + len );
400 * Parse DHCP packet and construct DHCP options block
402 * @v dhcphdr DHCP packet
403 * @v len Length of DHCP packet
404 * @ret options DHCP options block, or NULL
406 * Parses a received DHCP packet and canonicalises its contents into a
407 * single DHCP options block. The "file" and "sname" fields are
408 * converted into the corresponding DHCP options (@c
409 * DHCP_BOOTFILE_NAME and @c DHCP_TFTP_SERVER_NAME respectively). If
410 * these fields are used for option overloading, their options are
411 * merged in to the options block.
413 * The values of the "yiaddr" and "siaddr" fields will be stored
414 * within the options block as the magic options @c DHCP_EB_YIADDR and
417 * Note that this call allocates new memory for the constructed DHCP
418 * options block; it is the responsibility of the caller to eventually
421 static struct dhcp_option_block * dhcp_parse ( const struct dhcphdr *dhcphdr,
423 struct dhcp_option_block *options;
425 unsigned int overloading;
428 if ( len < sizeof ( *dhcphdr ) )
431 /* Calculate size of resulting concatenated option block:
433 * The "options" field : length of the field minus the DHCP_END tag.
435 * The "file" field : maximum length of the field minus the
436 * NUL terminator, plus a 2-byte DHCP header or, if used for
437 * option overloading, the length of the field minus the
440 * The "sname" field : as for the "file" field.
442 * 15 bytes for an encapsulated options field to contain the
443 * value of the "yiaddr" and "siaddr" fields
445 * 1 byte for a final terminating DHCP_END tag.
447 options_len = ( ( len - offsetof ( typeof ( *dhcphdr ), options ) ) - 1
448 + ( sizeof ( dhcphdr->file ) + 1 )
449 + ( sizeof ( dhcphdr->sname ) + 1 )
450 + 15 /* yiaddr and siaddr */
451 + 1 /* DHCP_END tag */ );
453 /* Allocate empty options block of required size */
454 options = alloc_dhcp_options ( options_len );
456 DBG ( "DHCP could not allocate %d-byte option block\n",
461 /* Merge in "options" field, if this is a DHCP packet */
462 if ( dhcphdr->magic == htonl ( DHCP_MAGIC_COOKIE ) ) {
463 merge_dhcp_field ( options, dhcphdr->options,
465 offsetof ( typeof (*dhcphdr), options ) ),
466 0 /* Always contains options */ );
469 /* Identify overloaded fields */
470 overloading = find_dhcp_num_option ( options, DHCP_OPTION_OVERLOAD );
472 /* Merge in "file" and "sname" fields */
473 merge_dhcp_field ( options, dhcphdr->file, sizeof ( dhcphdr->file ),
474 ( ( overloading & DHCP_OPTION_OVERLOAD_FILE ) ?
475 0 : DHCP_BOOTFILE_NAME ) );
476 merge_dhcp_field ( options, dhcphdr->sname, sizeof ( dhcphdr->sname ),
477 ( ( overloading & DHCP_OPTION_OVERLOAD_SNAME ) ?
478 0 : DHCP_TFTP_SERVER_NAME ) );
480 /* Set magic options for "yiaddr" and "siaddr", if present */
481 if ( dhcphdr->yiaddr.s_addr ) {
482 set_dhcp_option ( options, DHCP_EB_YIADDR,
483 &dhcphdr->yiaddr, sizeof (dhcphdr->yiaddr) );
485 if ( dhcphdr->siaddr.s_addr ) {
486 set_dhcp_option ( options, DHCP_EB_SIADDR,
487 &dhcphdr->siaddr, sizeof (dhcphdr->siaddr) );
490 assert ( options->len <= options->max_len );
495 /****************************************************************************
497 * Whole-packet construction
501 /** DHCP network device descriptor */
502 struct dhcp_netdev_desc {
509 } __attribute__ (( packed ));
511 /** DHCP client identifier */
512 struct dhcp_client_id {
513 /** Link-layer protocol */
515 /** Link-layer address */
516 uint8_t ll_addr[MAX_LL_ADDR_LEN];
517 } __attribute__ (( packed ));
519 /** DHCP client UUID */
520 struct dhcp_client_uuid {
521 /** Identifier type */
525 } __attribute__ (( packed ));
527 #define DHCP_CLIENT_UUID_TYPE 0
530 * Create DHCP request
532 * @v netdev Network device
533 * @v msgtype DHCP message type
534 * @v options DHCP server response options, or NULL
535 * @v data Buffer for DHCP packet
536 * @v max_len Size of DHCP packet buffer
537 * @v dhcppkt DHCP packet structure to fill in
538 * @ret rc Return status code
540 int create_dhcp_request ( struct net_device *netdev, int msgtype,
541 struct dhcp_option_block *options,
542 void *data, size_t max_len,
543 struct dhcp_packet *dhcppkt ) {
544 struct device_description *desc = &netdev->dev->desc;
545 struct dhcp_netdev_desc dhcp_desc;
546 struct dhcp_client_id client_id;
547 struct dhcp_client_uuid client_uuid;
548 size_t dhcp_features_len;
552 /* Create DHCP packet */
553 if ( ( rc = create_dhcp_packet ( netdev, msgtype, data, max_len,
555 DBG ( "DHCP could not create DHCP packet: %s\n",
560 /* Copy in options common to all requests */
561 if ( ( rc = copy_dhcp_packet_options ( dhcppkt,
562 &dhcp_request_options )) !=0 ){
563 DBG ( "DHCP could not set common DHCP options: %s\n",
568 /* Copy any required options from previous server repsonse */
570 if ( ( rc = copy_dhcp_packet_option ( dhcppkt, options,
571 DHCP_SERVER_IDENTIFIER,
572 DHCP_SERVER_IDENTIFIER ) ) != 0 ) {
573 DBG ( "DHCP could not set server identifier "
574 "option: %s\n", strerror ( rc ) );
577 if ( ( rc = copy_dhcp_packet_option ( dhcppkt, options,
579 DHCP_REQUESTED_ADDRESS ) ) != 0 ) {
580 DBG ( "DHCP could not set requested address "
581 "option: %s\n", strerror ( rc ) );
586 /* Add options to identify the feature list */
587 dhcp_features_len = ( dhcp_features_end - dhcp_features );
588 if ( ( rc = set_dhcp_packet_option ( dhcppkt, DHCP_EB_ENCAP,
590 dhcp_features_len ) ) != 0 ) {
591 DBG ( "DHCP could not set features list option: %s\n",
596 /* Add options to identify the network device */
597 dhcp_desc.type = desc->bus_type;
598 dhcp_desc.vendor = htons ( desc->vendor );
599 dhcp_desc.device = htons ( desc->device );
600 if ( ( rc = set_dhcp_packet_option ( dhcppkt, DHCP_EB_BUS_ID,
602 sizeof ( dhcp_desc ) ) ) != 0 ) {
603 DBG ( "DHCP could not set bus ID option: %s\n",
608 /* Add DHCP client identifier. Required for Infiniband, and
609 * doesn't hurt other link layers.
611 client_id.ll_proto = netdev->ll_protocol->ll_proto;
612 ll_addr_len = netdev->ll_protocol->ll_addr_len;
613 assert ( ll_addr_len <= sizeof ( client_id.ll_addr ) );
614 memcpy ( client_id.ll_addr, netdev->ll_addr, ll_addr_len );
615 if ( ( rc = set_dhcp_packet_option ( dhcppkt, DHCP_CLIENT_ID,
617 ( ll_addr_len + 1 ) ) ) != 0 ) {
618 DBG ( "DHCP could not set client ID: %s\n",
623 /* Add client UUID, if we have one. Required for PXE. */
624 client_uuid.type = DHCP_CLIENT_UUID_TYPE;
625 if ( ( rc = get_uuid ( &client_uuid.uuid ) ) == 0 ) {
626 if ( ( rc = set_dhcp_packet_option ( dhcppkt,
627 DHCP_CLIENT_UUID, &client_uuid,
628 sizeof ( client_uuid ) ) ) != 0 ) {
629 DBG ( "DHCP could not set client UUID: %s\n",
639 * Create DHCP response
641 * @v netdev Network device
642 * @v msgtype DHCP message type
643 * @v options DHCP options, or NULL
644 * @v data Buffer for DHCP packet
645 * @v max_len Size of DHCP packet buffer
646 * @v dhcppkt DHCP packet structure to fill in
647 * @ret rc Return status code
649 int create_dhcp_response ( struct net_device *netdev, int msgtype,
650 struct dhcp_option_block *options,
651 void *data, size_t max_len,
652 struct dhcp_packet *dhcppkt ) {
655 /* Create packet and copy in options */
656 if ( ( rc = create_dhcp_packet ( netdev, msgtype, data, max_len,
658 DBG ( " failed to build packet" );
661 if ( ( rc = copy_dhcp_packet_options ( dhcppkt, options ) ) != 0 ) {
662 DBG ( " failed to copy options" );
669 /****************************************************************************
671 * DHCP to UDP interface
675 /** A DHCP session */
676 struct dhcp_session {
677 /** Reference counter */
678 struct refcnt refcnt;
679 /** Job control interface */
680 struct job_interface job;
681 /** Data transfer interface */
682 struct xfer_interface xfer;
684 /** Network device being configured */
685 struct net_device *netdev;
686 /** Option block registration routine */
687 int ( * register_options ) ( struct net_device *netdev,
688 struct dhcp_option_block *options );
690 /** State of the session
692 * This is a value for the @c DHCP_MESSAGE_TYPE option
693 * (e.g. @c DHCPDISCOVER).
696 /** Options obtained from DHCP server */
697 struct dhcp_option_block *options;
698 /** Options obtained from ProxyDHCP server */
699 struct dhcp_option_block *proxy_options;
700 /** Retransmission timer */
701 struct retry_timer timer;
702 /** Session start time (in ticks) */
709 * @v refcnt Reference counter
711 static void dhcp_free ( struct refcnt *refcnt ) {
712 struct dhcp_session *dhcp =
713 container_of ( refcnt, struct dhcp_session, refcnt );
715 netdev_put ( dhcp->netdev );
716 dhcpopt_put ( dhcp->options );
717 dhcpopt_put ( dhcp->proxy_options );
722 * Mark DHCP session as complete
724 * @v dhcp DHCP session
725 * @v rc Return status code
727 static void dhcp_finished ( struct dhcp_session *dhcp, int rc ) {
729 /* Block futher incoming messages */
730 job_nullify ( &dhcp->job );
731 xfer_nullify ( &dhcp->xfer );
733 /* Stop retry timer */
734 stop_timer ( &dhcp->timer );
736 /* Free resources and close interfaces */
737 xfer_close ( &dhcp->xfer, rc );
738 job_done ( &dhcp->job, rc );
741 /****************************************************************************
743 * Data transfer interface
748 * Transmit DHCP request
750 * @v dhcp DHCP session
751 * @ret rc Return status code
753 static int dhcp_send_request ( struct dhcp_session *dhcp ) {
754 struct xfer_metadata meta = {
755 .netdev = dhcp->netdev,
757 struct io_buffer *iobuf;
758 struct dhcp_packet dhcppkt;
761 DBGC ( dhcp, "DHCP %p transmitting %s\n",
762 dhcp, dhcp_msgtype_name ( dhcp->state ) );
764 assert ( ( dhcp->state == DHCPDISCOVER ) ||
765 ( dhcp->state == DHCPREQUEST ) );
767 /* Start retry timer. Do this first so that failures to
768 * transmit will be retried.
770 start_timer ( &dhcp->timer );
772 /* Allocate buffer for packet */
773 iobuf = xfer_alloc_iob ( &dhcp->xfer, DHCP_MIN_LEN );
777 /* Create DHCP packet in temporary buffer */
778 if ( ( rc = create_dhcp_request ( dhcp->netdev, dhcp->state,
779 dhcp->options, iobuf->data,
780 iob_tailroom ( iobuf ),
781 &dhcppkt ) ) != 0 ) {
782 DBGC ( dhcp, "DHCP %p could not construct DHCP request: %s\n",
783 dhcp, strerror ( rc ) );
787 /* Transmit the packet */
788 iob_put ( iobuf, dhcppkt.len );
789 rc = xfer_deliver_iob_meta ( &dhcp->xfer, iobuf, &meta );
792 DBGC ( dhcp, "DHCP %p could not transmit UDP packet: %s\n",
793 dhcp, strerror ( rc ) );
803 * Handle DHCP retry timer expiry
805 * @v timer DHCP retry timer
806 * @v fail Failure indicator
808 static void dhcp_timer_expired ( struct retry_timer *timer, int fail ) {
809 struct dhcp_session *dhcp =
810 container_of ( timer, struct dhcp_session, timer );
813 dhcp_finished ( dhcp, -ETIMEDOUT );
815 dhcp_send_request ( dhcp );
822 * @v xfer Data transfer interface
823 * @v iobuf I/O buffer
824 * @v data Received data
825 * @v len Length of received data
826 * @ret rc Return status code
828 static int dhcp_deliver_raw ( struct xfer_interface *xfer,
829 const void *data, size_t len ) {
830 struct dhcp_session *dhcp =
831 container_of ( xfer, struct dhcp_session, xfer );
832 const struct dhcphdr *dhcphdr = data;
833 struct dhcp_option_block *options;
834 struct dhcp_option_block **store_options;
836 unsigned int msgtype;
837 unsigned long elapsed;
839 /* Check for matching transaction ID */
840 if ( dhcphdr->xid != dhcp_xid ( dhcp->netdev ) ) {
841 DBGC ( dhcp, "DHCP %p wrong transaction ID (wanted %08lx, "
842 "got %08lx)\n", dhcp, ntohl ( dhcphdr->xid ),
843 ntohl ( dhcp_xid ( dhcp->netdev ) ) );
847 /* Parse packet and create options structure */
848 options = dhcp_parse ( dhcphdr, len );
850 DBGC ( dhcp, "DHCP %p could not parse DHCP packet\n", dhcp );
854 /* Determine and verify message type */
855 is_proxy = ( dhcphdr->yiaddr.s_addr == 0 );
856 msgtype = find_dhcp_num_option ( options, DHCP_MESSAGE_TYPE );
857 DBGC ( dhcp, "DHCP %p received %s%s\n", dhcp,
858 ( is_proxy ? "Proxy" : "" ), dhcp_msgtype_name ( msgtype ) );
859 if ( ( ( dhcp->state != DHCPDISCOVER ) || ( msgtype != DHCPOFFER ) ) &&
860 ( ( dhcp->state != DHCPREQUEST ) || ( msgtype != DHCPACK ) ) ) {
861 DBGC ( dhcp, "DHCP %p discarding %s while in %s state\n",
862 dhcp, dhcp_msgtype_name ( msgtype ),
863 dhcp_msgtype_name ( dhcp->state ) );
867 /* Update stored standard/ProxyDHCP options, if the new
868 * options have equal or higher priority than the
869 * currently-stored options.
871 store_options = ( is_proxy ? &dhcp->proxy_options : &dhcp->options );
872 if ( ( ! *store_options ) ||
873 ( find_dhcp_num_option ( options, DHCP_EB_PRIORITY ) >=
874 find_dhcp_num_option ( *store_options, DHCP_EB_PRIORITY ) ) ) {
875 dhcpopt_put ( *store_options );
876 *store_options = options;
878 dhcpopt_put ( options );
881 /* Handle DHCP response */
882 switch ( dhcp->state ) {
884 /* If we have received a valid standard DHCP response
885 * (i.e. one with an IP address), and we have allowed
886 * sufficient time for ProxyDHCP reponses, then
887 * transition to making the DHCPREQUEST.
889 elapsed = ( currticks() - dhcp->start );
890 if ( dhcp->options &&
891 ( elapsed > PROXYDHCP_WAIT_TIME ) ) {
892 stop_timer ( &dhcp->timer );
893 dhcp->state = DHCPREQUEST;
894 dhcp_send_request ( dhcp );
898 /* DHCP finished; register options and exit */
899 if ( dhcp->proxy_options )
900 dhcp->register_options ( dhcp->netdev,
901 dhcp->proxy_options );
902 dhcp->register_options ( dhcp->netdev, dhcp->options );
903 dhcp_finished ( dhcp, 0 );
912 dhcpopt_put ( options );
916 /** DHCP data transfer interface operations */
917 static struct xfer_interface_operations dhcp_xfer_operations = {
918 .close = ignore_xfer_close,
919 .vredirect = xfer_vopen,
920 .seek = ignore_xfer_seek,
921 .window = unlimited_xfer_window,
922 .deliver_iob = xfer_deliver_as_raw,
923 .deliver_raw = dhcp_deliver_raw,
926 /****************************************************************************
928 * Job control interface
933 * Handle kill() event received via job control interface
935 * @v job DHCP job control interface
937 static void dhcp_job_kill ( struct job_interface *job ) {
938 struct dhcp_session *dhcp =
939 container_of ( job, struct dhcp_session, job );
941 /* Terminate DHCP session */
942 dhcp_finished ( dhcp, -ECANCELED );
945 /** DHCP job control interface operations */
946 static struct job_interface_operations dhcp_job_operations = {
947 .done = ignore_job_done,
948 .kill = dhcp_job_kill,
949 .progress = ignore_job_progress,
952 /****************************************************************************
959 * Start DHCP on a network device
961 * @v job Job control interface
962 * @v netdev Network device
963 * @v register_options DHCP option block registration routine
964 * @ret rc Return status code
966 * Starts DHCP on the specified network device. If successful, the @c
967 * register_options() routine will be called with the acquired
970 int start_dhcp ( struct job_interface *job, struct net_device *netdev,
971 int ( * register_options ) ( struct net_device *netdev,
972 struct dhcp_option_block * ) ) {
973 static struct sockaddr_in server = {
974 .sin_family = AF_INET,
975 .sin_addr.s_addr = INADDR_BROADCAST,
976 .sin_port = htons ( BOOTPS_PORT ),
978 static struct sockaddr_in client = {
979 .sin_family = AF_INET,
980 .sin_port = htons ( BOOTPC_PORT ),
982 struct dhcp_session *dhcp;
985 /* Allocate and initialise structure */
986 dhcp = zalloc ( sizeof ( *dhcp ) );
989 dhcp->refcnt.free = dhcp_free;
990 job_init ( &dhcp->job, &dhcp_job_operations, &dhcp->refcnt );
991 xfer_init ( &dhcp->xfer, &dhcp_xfer_operations, &dhcp->refcnt );
992 dhcp->netdev = netdev_get ( netdev );
993 dhcp->register_options = register_options;
994 dhcp->timer.expired = dhcp_timer_expired;
995 dhcp->state = DHCPDISCOVER;
996 dhcp->start = currticks();
998 /* Instantiate child objects and attach to our interfaces */
999 if ( ( rc = xfer_open_socket ( &dhcp->xfer, SOCK_DGRAM,
1000 ( struct sockaddr * ) &server,
1001 ( struct sockaddr * ) &client ) ) != 0 )
1004 /* Start timer to initiate initial DHCPREQUEST */
1005 start_timer_nodelay ( &dhcp->timer );
1007 /* Attach parent interface, mortalise self, and return */
1008 job_plug_plug ( &dhcp->job, job );
1009 ref_put ( &dhcp->refcnt );
1013 dhcp_finished ( dhcp, rc );
1014 ref_put ( &dhcp->refcnt );
1018 /****************************************************************************
1020 * Network device configurator
1025 * Configure network device from DHCP options
1027 * @v netdev Network device
1028 * @v options DHCP options block
1029 * @ret rc Return status code
1031 int dhcp_configure_netdev ( struct net_device *netdev,
1032 struct dhcp_option_block *options ) {
1033 struct in_addr address = { 0 };
1034 struct in_addr netmask = { 0 };
1035 struct in_addr gateway = { INADDR_NONE };
1038 /* Retrieve IP address configuration */
1039 find_dhcp_ipv4_option ( options, DHCP_EB_YIADDR, &address );
1040 find_dhcp_ipv4_option ( options, DHCP_SUBNET_MASK, &netmask );
1041 find_dhcp_ipv4_option ( options, DHCP_ROUTERS, &gateway );
1043 /* Do nothing unless we have at least an IP address to use */
1044 if ( ! address.s_addr )
1047 /* Clear any existing routing table entry */
1048 del_ipv4_address ( netdev );
1050 /* Set up new IP address configuration */
1051 if ( ( rc = add_ipv4_address ( netdev, address, netmask,
1052 gateway ) ) != 0 ) {
1053 DBG ( "Could not configure %s with DHCP results: %s\n",
1054 netdev->name, strerror ( rc ) );