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/xfer.h>
28 #include <gpxe/open.h>
30 #include <gpxe/retry.h>
31 #include <gpxe/tcpip.h>
33 #include <gpxe/dhcp.h>
37 * Dynamic Host Configuration Protocol
41 /** DHCP operation types
43 * This table maps from DHCP message types (i.e. values of the @c
44 * DHCP_MESSAGE_TYPE option) to values of the "op" field within a DHCP
47 static const uint8_t dhcp_op[] = {
48 [DHCPDISCOVER] = BOOTP_REQUEST,
49 [DHCPOFFER] = BOOTP_REPLY,
50 [DHCPREQUEST] = BOOTP_REQUEST,
51 [DHCPDECLINE] = BOOTP_REQUEST,
52 [DHCPACK] = BOOTP_REPLY,
53 [DHCPNAK] = BOOTP_REPLY,
54 [DHCPRELEASE] = BOOTP_REQUEST,
55 [DHCPINFORM] = BOOTP_REQUEST,
58 /** Raw option data for options common to all DHCP requests */
59 static uint8_t dhcp_request_options_data[] = {
60 DHCP_MAX_MESSAGE_SIZE, DHCP_WORD ( ETH_MAX_MTU ),
62 DHCP_STRING ( 'E', 't', 'h', 'e', 'r', 'b', 'o', 'o', 't' ),
63 DHCP_PARAMETER_REQUEST_LIST,
64 DHCP_OPTION ( DHCP_SUBNET_MASK, DHCP_ROUTERS, DHCP_DNS_SERVERS, DHCP_LOG_SERVERS,
65 DHCP_HOST_NAME, DHCP_DOMAIN_NAME, DHCP_ROOT_PATH,
66 DHCP_VENDOR_ENCAP, DHCP_TFTP_SERVER_NAME,
67 DHCP_BOOTFILE_NAME, DHCP_EB_ENCAP,
68 DHCP_ISCSI_INITIATOR_IQN ),
73 * Name a DHCP packet type
75 * @v msgtype DHCP message type
76 * @ret string DHCP mesasge type name
78 static inline const char * dhcp_msgtype_name ( unsigned int msgtype ) {
80 case 0: return "BOOTP"; /* Non-DHCP packet */
81 case DHCPDISCOVER: return "DHCPDISCOVER";
82 case DHCPOFFER: return "DHCPOFFER";
83 case DHCPREQUEST: return "DHCPREQUEST";
84 case DHCPDECLINE: return "DHCPDECLINE";
85 case DHCPACK: return "DHCPACK";
86 case DHCPNAK: return "DHCPNAK";
87 case DHCPRELEASE: return "DHCPRELEASE";
88 case DHCPINFORM: return "DHCPINFORM";
89 default: return "DHCP<invalid>";
94 * Calculate DHCP transaction ID for a network device
96 * @v netdev Network device
99 * Extract the least significant bits of the hardware address for use
100 * as the transaction ID.
102 static uint32_t dhcp_xid ( struct net_device *netdev ) {
105 memcpy ( &xid, ( netdev->ll_addr + netdev->ll_protocol->ll_addr_len
106 - sizeof ( xid ) ), sizeof ( xid ) );
110 /** Options common to all DHCP requests */
111 static struct dhcp_option_block dhcp_request_options = {
112 .data = dhcp_request_options_data,
113 .max_len = sizeof ( dhcp_request_options_data ),
114 .len = sizeof ( dhcp_request_options_data ),
118 * Set option within DHCP packet
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
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.
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.
136 static int set_dhcp_packet_option ( struct dhcp_packet *dhcppkt,
137 unsigned int tag, const void *data,
139 struct dhcphdr *dhcphdr = dhcppkt->dhcphdr;
140 struct dhcp_option_block *options = &dhcppkt->options;
141 struct dhcp_option *option = NULL;
143 /* Special-case the magic options */
145 case DHCP_OPTION_OVERLOAD:
146 /* Hard-coded in packets we create; always ignore */
149 memcpy ( &dhcphdr->yiaddr, data, sizeof ( dhcphdr->yiaddr ) );
152 memcpy ( &dhcphdr->siaddr, data, sizeof ( dhcphdr->siaddr ) );
154 case DHCP_TFTP_SERVER_NAME:
155 memset ( dhcphdr->sname, 0, sizeof ( dhcphdr->sname ) );
156 if ( len > sizeof ( dhcphdr->sname ) )
157 len = sizeof ( dhcphdr->sname );
158 memcpy ( dhcphdr->sname, data, len );
160 case DHCP_BOOTFILE_NAME:
161 memset ( dhcphdr->file, 0, sizeof ( dhcphdr->file ) );
162 if ( len > sizeof ( dhcphdr->file ) )
163 len = sizeof ( dhcphdr->file );
164 memcpy ( dhcphdr->file, data, len );
167 /* Continue processing as normal */
172 option = set_dhcp_option ( options, tag, data, len );
174 /* Update DHCP packet length */
175 dhcppkt->len = ( offsetof ( typeof ( *dhcppkt->dhcphdr ), options )
176 + dhcppkt->options.len );
178 return ( option ? 0 : -ENOSPC );
182 * Copy option into DHCP packet
184 * @v dhcppkt DHCP packet
185 * @v options DHCP option block, or NULL
186 * @v tag DHCP option tag to search for
187 * @v new_tag DHCP option tag to use for copied option
188 * @ret rc Return status code
190 * Copies a single option, if present, from the DHCP options block
191 * into a DHCP packet. The tag for the option may be changed if
192 * desired; this is required by other parts of the DHCP code.
194 * @c options may specify a single options block, or be left as NULL
195 * in order to search for the option within all registered options
198 static int copy_dhcp_packet_option ( struct dhcp_packet *dhcppkt,
199 struct dhcp_option_block *options,
200 unsigned int tag, unsigned int new_tag ) {
201 struct dhcp_option *option;
204 option = find_dhcp_option ( options, tag );
206 if ( ( rc = set_dhcp_packet_option ( dhcppkt, new_tag,
208 option->len ) ) != 0 )
215 * Copy options into DHCP packet
217 * @v dhcppkt DHCP packet
218 * @v options DHCP option block, or NULL
219 * @v encapsulator Encapsulating option, or zero
220 * @ret rc Return status code
222 * Copies options with the specified encapsulator from DHCP options
223 * blocks into a DHCP packet. Most options are copied verbatim.
224 * Recognised encapsulated options fields are handled as such.
226 * @c options may specify a single options block, or be left as NULL
227 * in order to copy options from all registered options blocks.
229 static int copy_dhcp_packet_encap_options ( struct dhcp_packet *dhcppkt,
230 struct dhcp_option_block *options,
231 unsigned int encapsulator ) {
236 for ( subtag = DHCP_MIN_OPTION; subtag <= DHCP_MAX_OPTION; subtag++ ) {
237 tag = DHCP_ENCAP_OPT ( encapsulator, subtag );
240 case DHCP_VENDOR_ENCAP:
241 /* Process encapsulated options field */
242 if ( ( rc = copy_dhcp_packet_encap_options ( dhcppkt,
248 /* Copy option to reassembled packet */
249 if ( ( rc = copy_dhcp_packet_option ( dhcppkt, options,
260 * Copy options into DHCP packet
262 * @v dhcppkt DHCP packet
263 * @v options DHCP option block, or NULL
264 * @ret rc Return status code
266 * Copies options from DHCP options blocks into a DHCP packet. Most
267 * options are copied verbatim. Recognised encapsulated options
268 * fields are handled as such.
270 * @c options may specify a single options block, or be left as NULL
271 * in order to copy options from all registered options blocks.
273 static int copy_dhcp_packet_options ( struct dhcp_packet *dhcppkt,
274 struct dhcp_option_block *options ) {
275 return copy_dhcp_packet_encap_options ( dhcppkt, options, 0 );
279 * Create a DHCP packet
281 * @v netdev Network device
282 * @v msgtype DHCP message type
283 * @v data Buffer for DHCP packet
284 * @v max_len Size of DHCP packet buffer
285 * @v dhcppkt DHCP packet structure to fill in
286 * @ret rc Return status code
288 * Creates a DHCP packet in the specified buffer, and fills out a @c
289 * dhcp_packet structure that can be passed to
290 * set_dhcp_packet_option() or copy_dhcp_packet_options().
292 static int create_dhcp_packet ( struct net_device *netdev,
293 unsigned int msgtype,
294 void *data, size_t max_len,
295 struct dhcp_packet *dhcppkt ) {
296 struct dhcphdr *dhcphdr = data;
300 if ( max_len < sizeof ( *dhcphdr ) )
303 /* Initialise DHCP packet content */
304 memset ( dhcphdr, 0, max_len );
305 dhcphdr->xid = dhcp_xid ( netdev );
306 dhcphdr->magic = htonl ( DHCP_MAGIC_COOKIE );
307 dhcphdr->htype = ntohs ( netdev->ll_protocol->ll_proto );
308 dhcphdr->hlen = netdev->ll_protocol->ll_addr_len;
309 memcpy ( dhcphdr->chaddr, netdev->ll_addr, dhcphdr->hlen );
310 dhcphdr->op = dhcp_op[msgtype];
312 /* Initialise DHCP packet structure */
313 dhcppkt->dhcphdr = dhcphdr;
314 dhcppkt->max_len = max_len;
315 init_dhcp_options ( &dhcppkt->options, dhcphdr->options,
317 offsetof ( typeof ( *dhcphdr ), options ) ) );
319 /* Set DHCP_MESSAGE_TYPE option */
320 if ( ( rc = set_dhcp_packet_option ( dhcppkt, DHCP_MESSAGE_TYPE,
322 sizeof ( msgtype ) ) ) != 0 )
329 * Calculate used length of a field containing DHCP options
331 * @v data Field containing DHCP options
332 * @v max_len Field length
333 * @ret len Used length (excluding the @c DHCP_END tag)
335 static size_t dhcp_field_len ( const void *data, size_t max_len ) {
336 struct dhcp_option_block options;
337 struct dhcp_option *end;
339 options.data = ( ( void * ) data );
340 options.len = max_len;
341 end = find_dhcp_option ( &options, DHCP_END );
342 return ( end ? ( ( ( void * ) end ) - data ) : 0 );
346 * Merge field containing DHCP options or string into DHCP options block
348 * @v options DHCP option block
349 * @v data Field containing DHCP options
350 * @v max_len Field length
351 * @v tag DHCP option tag, or 0
353 * If @c tag is non-zero, the field will be treated as a
354 * NUL-terminated string representing the value of the specified DHCP
355 * option. If @c tag is zero, the field will be treated as a block of
356 * DHCP options, and simply appended to the existing options in the
359 * The caller must ensure that there is enough space in the options
360 * block to perform the merge.
362 static void merge_dhcp_field ( struct dhcp_option_block *options,
363 const void *data, size_t max_len,
367 struct dhcp_option *end;
370 set_dhcp_option ( options, tag, data, strlen ( data ) );
372 len = dhcp_field_len ( data, max_len );
373 dest = ( options->data + options->len - 1 );
374 memcpy ( dest, data, len );
376 end = ( dest + len );
382 * Parse DHCP packet and construct DHCP options block
384 * @v dhcphdr DHCP packet
385 * @v len Length of DHCP packet
386 * @ret options DHCP options block, or NULL
388 * Parses a received DHCP packet and canonicalises its contents into a
389 * single DHCP options block. The "file" and "sname" fields are
390 * converted into the corresponding DHCP options (@c
391 * DHCP_BOOTFILE_NAME and @c DHCP_TFTP_SERVER_NAME respectively). If
392 * these fields are used for option overloading, their options are
393 * merged in to the options block.
395 * The values of the "yiaddr" and "siaddr" fields will be stored
396 * within the options block as the magic options @c DHCP_EB_YIADDR and
399 * Note that this call allocates new memory for the constructed DHCP
400 * options block; it is the responsibility of the caller to eventually
403 static struct dhcp_option_block * dhcp_parse ( const struct dhcphdr *dhcphdr,
405 struct dhcp_option_block *options;
407 unsigned int overloading;
410 if ( len < sizeof ( *dhcphdr ) )
413 /* Calculate size of resulting concatenated option block:
415 * The "options" field : length of the field minus the DHCP_END tag.
417 * The "file" field : maximum length of the field minus the
418 * NUL terminator, plus a 2-byte DHCP header or, if used for
419 * option overloading, the length of the field minus the
422 * The "sname" field : as for the "file" field.
424 * 15 bytes for an encapsulated options field to contain the
425 * value of the "yiaddr" and "siaddr" fields
427 * 1 byte for a final terminating DHCP_END tag.
429 options_len = ( ( len - offsetof ( typeof ( *dhcphdr ), options ) ) - 1
430 + ( sizeof ( dhcphdr->file ) + 1 )
431 + ( sizeof ( dhcphdr->sname ) + 1 )
432 + 15 /* yiaddr and siaddr */
433 + 1 /* DHCP_END tag */ );
435 /* Allocate empty options block of required size */
436 options = alloc_dhcp_options ( options_len );
438 DBG ( "DHCP could not allocate %d-byte option block\n",
443 /* Merge in "options" field, if this is a DHCP packet */
444 if ( dhcphdr->magic == htonl ( DHCP_MAGIC_COOKIE ) ) {
445 merge_dhcp_field ( options, dhcphdr->options,
447 offsetof ( typeof (*dhcphdr), options ) ),
448 0 /* Always contains options */ );
451 /* Identify overloaded fields */
452 overloading = find_dhcp_num_option ( options, DHCP_OPTION_OVERLOAD );
454 /* Merge in "file" and "sname" fields */
455 merge_dhcp_field ( options, dhcphdr->file, sizeof ( dhcphdr->file ),
456 ( ( overloading & DHCP_OPTION_OVERLOAD_FILE ) ?
457 0 : DHCP_BOOTFILE_NAME ) );
458 merge_dhcp_field ( options, dhcphdr->sname, sizeof ( dhcphdr->sname ),
459 ( ( overloading & DHCP_OPTION_OVERLOAD_SNAME ) ?
460 0 : DHCP_TFTP_SERVER_NAME ) );
462 /* Set magic options for "yiaddr" and "siaddr", if present */
463 if ( dhcphdr->yiaddr.s_addr ) {
464 set_dhcp_option ( options, DHCP_EB_YIADDR,
465 &dhcphdr->yiaddr, sizeof (dhcphdr->yiaddr) );
467 if ( dhcphdr->siaddr.s_addr ) {
468 set_dhcp_option ( options, DHCP_EB_SIADDR,
469 &dhcphdr->siaddr, sizeof (dhcphdr->siaddr) );
472 assert ( options->len <= options->max_len );
477 /****************************************************************************
479 * Whole-packet construction
484 * Create DHCP request
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
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 ) {
500 /* Create DHCP packet */
501 if ( ( rc = create_dhcp_packet ( netdev, msgtype, data, max_len,
503 DBG ( "DHCP could not create DHCP packet: %s\n",
508 /* Copy in options common to all requests */
509 if ( ( rc = copy_dhcp_packet_options ( dhcppkt,
510 &dhcp_request_options )) !=0 ){
511 DBG ( "DHCP could not set common DHCP options: %s\n",
516 /* Copy any required options from previous server repsonse */
518 if ( ( rc = copy_dhcp_packet_option ( dhcppkt, options,
519 DHCP_SERVER_IDENTIFIER,
520 DHCP_SERVER_IDENTIFIER ) ) != 0 ) {
521 DBG ( "DHCP could not set server identifier "
522 "option: %s\n", strerror ( rc ) );
525 if ( ( rc = copy_dhcp_packet_option ( dhcppkt, options,
527 DHCP_REQUESTED_ADDRESS ) ) != 0 ) {
528 DBG ( "DHCP could not set requested address "
529 "option: %s\n", strerror ( rc ) );
538 * Create DHCP response
540 * @v netdev Network device
541 * @v msgtype DHCP message type
542 * @v options DHCP options, or NULL
543 * @v data Buffer for DHCP packet
544 * @v max_len Size of DHCP packet buffer
545 * @v dhcppkt DHCP packet structure to fill in
546 * @ret rc Return status code
548 int create_dhcp_response ( struct net_device *netdev, int msgtype,
549 struct dhcp_option_block *options,
550 void *data, size_t max_len,
551 struct dhcp_packet *dhcppkt ) {
554 /* Create packet and copy in options */
555 if ( ( rc = create_dhcp_packet ( netdev, msgtype, data, max_len,
557 DBG ( " failed to build packet" );
560 if ( ( rc = copy_dhcp_packet_options ( dhcppkt, options ) ) != 0 ) {
561 DBG ( " failed to copy options" );
568 /****************************************************************************
570 * DHCP to UDP interface
574 /** A DHCP session */
575 struct dhcp_session {
576 /** Reference counter */
577 struct refcnt refcnt;
578 /** Job control interface */
579 struct job_interface job;
580 /** Data transfer interface */
581 struct xfer_interface xfer;
583 /** Network device being configured */
584 struct net_device *netdev;
585 /** Option block registration routine */
586 int ( * register_options ) ( struct net_device *netdev,
587 struct dhcp_option_block *options );
589 /** State of the session
591 * This is a value for the @c DHCP_MESSAGE_TYPE option
592 * (e.g. @c DHCPDISCOVER).
595 /** Options obtained from server */
596 struct dhcp_option_block *options;
597 /** Retransmission timer */
598 struct retry_timer timer;
604 * @v refcnt Reference counter
606 static void dhcp_free ( struct refcnt *refcnt ) {
607 struct dhcp_session *dhcp =
608 container_of ( refcnt, struct dhcp_session, refcnt );
610 netdev_put ( dhcp->netdev );
611 dhcpopt_put ( dhcp->options );
616 * Mark DHCP session as complete
618 * @v dhcp DHCP session
619 * @v rc Return status code
621 static void dhcp_finished ( struct dhcp_session *dhcp, int rc ) {
623 /* Block futher incoming messages */
624 job_nullify ( &dhcp->job );
625 xfer_nullify ( &dhcp->xfer );
627 /* Stop retry timer */
628 stop_timer ( &dhcp->timer );
630 /* Free resources and close interfaces */
631 xfer_close ( &dhcp->xfer, rc );
632 job_done ( &dhcp->job, rc );
635 /****************************************************************************
637 * Data transfer interface
642 * Transmit DHCP request
644 * @v dhcp DHCP session
645 * @ret rc Return status code
647 static int dhcp_send_request ( struct dhcp_session *dhcp ) {
648 struct xfer_metadata meta = {
649 .netdev = dhcp->netdev,
651 struct io_buffer *iobuf;
652 struct dhcp_packet dhcppkt;
655 DBGC ( dhcp, "DHCP %p transmitting %s\n",
656 dhcp, dhcp_msgtype_name ( dhcp->state ) );
658 assert ( ( dhcp->state == DHCPDISCOVER ) ||
659 ( dhcp->state == DHCPREQUEST ) );
661 /* Start retry timer. Do this first so that failures to
662 * transmit will be retried.
664 start_timer ( &dhcp->timer );
666 /* Allocate buffer for packet */
667 iobuf = xfer_alloc_iob ( &dhcp->xfer, DHCP_MIN_LEN );
671 /* Create DHCP packet in temporary buffer */
672 if ( ( rc = create_dhcp_request ( dhcp->netdev, dhcp->state,
673 dhcp->options, iobuf->data,
674 iob_tailroom ( iobuf ),
675 &dhcppkt ) ) != 0 ) {
676 DBGC ( dhcp, "DHCP %p could not construct DHCP request: %s\n",
677 dhcp, strerror ( rc ) );
681 /* Transmit the packet */
682 iob_put ( iobuf, dhcppkt.len );
683 rc = xfer_deliver_iob_meta ( &dhcp->xfer, iobuf, &meta );
686 DBGC ( dhcp, "DHCP %p could not transmit UDP packet: %s\n",
687 dhcp, strerror ( rc ) );
697 * Handle DHCP retry timer expiry
699 * @v timer DHCP retry timer
700 * @v fail Failure indicator
702 static void dhcp_timer_expired ( struct retry_timer *timer, int fail ) {
703 struct dhcp_session *dhcp =
704 container_of ( timer, struct dhcp_session, timer );
707 dhcp_finished ( dhcp, -ETIMEDOUT );
709 dhcp_send_request ( dhcp );
716 * @v xfer Data transfer interface
717 * @v iobuf I/O buffer
718 * @v data Received data
719 * @v len Length of received data
720 * @ret rc Return status code
722 static int dhcp_deliver_raw ( struct xfer_interface *xfer,
723 const void *data, size_t len ) {
724 struct dhcp_session *dhcp =
725 container_of ( xfer, struct dhcp_session, xfer );
726 const struct dhcphdr *dhcphdr = data;
727 struct dhcp_option_block *options;
728 unsigned int msgtype;
730 /* Check for matching transaction ID */
731 if ( dhcphdr->xid != dhcp_xid ( dhcp->netdev ) ) {
732 DBGC ( dhcp, "DHCP %p wrong transaction ID (wanted %08lx, "
733 "got %08lx)\n", dhcp, ntohl ( dhcphdr->xid ),
734 ntohl ( dhcp_xid ( dhcp->netdev ) ) );
738 /* Parse packet and create options structure */
739 options = dhcp_parse ( dhcphdr, len );
741 DBGC ( dhcp, "DHCP %p could not parse DHCP packet\n", dhcp );
745 /* Determine message type */
746 msgtype = find_dhcp_num_option ( options, DHCP_MESSAGE_TYPE );
747 DBGC ( dhcp, "DHCP %p received %s\n",
748 dhcp, dhcp_msgtype_name ( msgtype ) );
750 /* Handle DHCP reply */
751 switch ( dhcp->state ) {
753 if ( msgtype != DHCPOFFER )
755 dhcp->state = DHCPREQUEST;
758 if ( msgtype != DHCPACK )
760 dhcp->state = DHCPACK;
767 /* Stop timer and update stored options */
768 stop_timer ( &dhcp->timer );
770 dhcpopt_put ( dhcp->options );
771 dhcp->options = options;
773 /* Transmit next packet, or terminate session */
774 if ( dhcp->state < DHCPACK ) {
775 dhcp_send_request ( dhcp );
777 dhcp->register_options ( dhcp->netdev, dhcp->options );
778 dhcp_finished ( dhcp, 0 );
783 dhcpopt_put ( options );
787 /** DHCP data transfer interface operations */
788 static struct xfer_interface_operations dhcp_xfer_operations = {
789 .close = ignore_xfer_close,
790 .vredirect = xfer_vopen,
791 .seek = ignore_xfer_seek,
792 .window = unlimited_xfer_window,
793 .deliver_iob = xfer_deliver_as_raw,
794 .deliver_raw = dhcp_deliver_raw,
797 /****************************************************************************
799 * Job control interface
804 * Handle kill() event received via job control interface
806 * @v job DHCP job control interface
808 static void dhcp_job_kill ( struct job_interface *job ) {
809 struct dhcp_session *dhcp =
810 container_of ( job, struct dhcp_session, job );
812 /* Terminate DHCP session */
813 dhcp_finished ( dhcp, -ECANCELED );
816 /** DHCP job control interface operations */
817 static struct job_interface_operations dhcp_job_operations = {
818 .done = ignore_job_done,
819 .kill = dhcp_job_kill,
820 .progress = ignore_job_progress,
823 /****************************************************************************
830 * Start DHCP on a network device
832 * @v job Job control interface
833 * @v netdev Network device
834 * @v register_options DHCP option block registration routine
835 * @ret rc Return status code
837 * Starts DHCP on the specified network device. If successful, the @c
838 * register_options() routine will be called with the acquired
841 int start_dhcp ( struct job_interface *job, struct net_device *netdev,
842 int ( * register_options ) ( struct net_device *netdev,
843 struct dhcp_option_block * ) ) {
844 static struct sockaddr_in server = {
845 .sin_family = AF_INET,
846 .sin_addr.s_addr = INADDR_BROADCAST,
847 .sin_port = htons ( BOOTPS_PORT ),
849 static struct sockaddr_in client = {
850 .sin_family = AF_INET,
851 .sin_port = htons ( BOOTPC_PORT ),
853 struct dhcp_session *dhcp;
856 /* Allocate and initialise structure */
857 dhcp = zalloc ( sizeof ( *dhcp ) );
860 dhcp->refcnt.free = dhcp_free;
861 job_init ( &dhcp->job, &dhcp_job_operations, &dhcp->refcnt );
862 xfer_init ( &dhcp->xfer, &dhcp_xfer_operations, &dhcp->refcnt );
863 dhcp->netdev = netdev_get ( netdev );
864 dhcp->register_options = register_options;
865 dhcp->timer.expired = dhcp_timer_expired;
866 dhcp->state = DHCPDISCOVER;
868 /* Instantiate child objects and attach to our interfaces */
869 if ( ( rc = xfer_open_socket ( &dhcp->xfer, SOCK_DGRAM,
870 ( struct sockaddr * ) &server,
871 ( struct sockaddr * ) &client ) ) != 0 )
874 /* Start timer to initiate initial DHCPREQUEST */
875 start_timer ( &dhcp->timer );
877 /* Attach parent interface, mortalise self, and return */
878 job_plug_plug ( &dhcp->job, job );
879 ref_put ( &dhcp->refcnt );
883 dhcp_finished ( dhcp, rc );
884 ref_put ( &dhcp->refcnt );
888 /****************************************************************************
890 * Network device configurator
895 * Configure network device from DHCP options
897 * @v netdev Network device
898 * @v options DHCP options block
899 * @ret rc Return status code
901 int dhcp_configure_netdev ( struct net_device *netdev,
902 struct dhcp_option_block *options ) {
903 struct in_addr address = { 0 };
904 struct in_addr netmask = { 0 };
905 struct in_addr gateway = { INADDR_NONE };
908 /* Clear any existing routing table entry */
909 del_ipv4_address ( netdev );
911 /* Retrieve IP address configuration */
912 find_dhcp_ipv4_option ( options, DHCP_EB_YIADDR, &address );
913 find_dhcp_ipv4_option ( options, DHCP_SUBNET_MASK, &netmask );
914 find_dhcp_ipv4_option ( options, DHCP_ROUTERS, &gateway );
916 /* Set up new IP address configuration */
917 if ( ( rc = add_ipv4_address ( netdev, address, netmask,
919 DBG ( "Could not configure %s with DHCP results: %s\n",
920 netdev->name, strerror ( rc ) );