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>
36 #include <gpxe/timer.h>
37 #include <gpxe/settings.h>
38 #include <gpxe/dhcp.h>
39 #include <gpxe/dhcpopts.h>
40 #include <gpxe/dhcppkt.h>
44 * Dynamic Host Configuration Protocol
49 * DHCP operation types
51 * This table maps from DHCP message types (i.e. values of the @c
52 * DHCP_MESSAGE_TYPE option) to values of the "op" field within a DHCP
55 static const uint8_t dhcp_op[] = {
56 [DHCPDISCOVER] = BOOTP_REQUEST,
57 [DHCPOFFER] = BOOTP_REPLY,
58 [DHCPREQUEST] = BOOTP_REQUEST,
59 [DHCPDECLINE] = BOOTP_REQUEST,
60 [DHCPACK] = BOOTP_REPLY,
61 [DHCPNAK] = BOOTP_REPLY,
62 [DHCPRELEASE] = BOOTP_REQUEST,
63 [DHCPINFORM] = BOOTP_REQUEST,
66 /** Raw option data for options common to all DHCP requests */
67 static uint8_t dhcp_request_options_data[] = {
68 DHCP_MAX_MESSAGE_SIZE, DHCP_WORD ( ETH_MAX_MTU ),
70 DHCP_STRING ( 'P', 'X', 'E', 'C', 'l', 'i', 'e', 'n', 't', ':',
71 'A', 'r', 'c', 'h', ':', '0', '0', '0', '0', '0', ':',
72 'U', 'N', 'D', 'I', ':', '0', '0', '2', '0', '0', '1' ),
73 DHCP_CLIENT_ARCHITECTURE, DHCP_WORD ( 0 ),
74 DHCP_CLIENT_NDI, DHCP_OPTION ( 1 /* UNDI */ , 2, 1 /* v2.1 */ ),
75 DHCP_PARAMETER_REQUEST_LIST,
76 DHCP_OPTION ( DHCP_SUBNET_MASK, DHCP_ROUTERS, DHCP_DNS_SERVERS,
77 DHCP_LOG_SERVERS, DHCP_HOST_NAME, DHCP_DOMAIN_NAME,
78 DHCP_ROOT_PATH, DHCP_VENDOR_ENCAP, DHCP_VENDOR_CLASS_ID,
79 DHCP_TFTP_SERVER_NAME, DHCP_BOOTFILE_NAME,
80 DHCP_EB_ENCAP, DHCP_ISCSI_INITIATOR_IQN ),
84 /** Options common to all DHCP requests */
85 static struct dhcp_options dhcp_request_options = {
86 .data = dhcp_request_options_data,
87 .max_len = sizeof ( dhcp_request_options_data ),
88 .len = sizeof ( dhcp_request_options_data ),
91 /** DHCP feature codes */
92 static uint8_t dhcp_features[0] __table_start ( uint8_t, dhcp_features );
93 static uint8_t dhcp_features_end[0] __table_end ( uint8_t, dhcp_features );
96 * Name a DHCP packet type
98 * @v msgtype DHCP message type
99 * @ret string DHCP mesasge type name
101 static inline const char * dhcp_msgtype_name ( unsigned int msgtype ) {
103 case 0: return "BOOTP"; /* Non-DHCP packet */
104 case DHCPDISCOVER: return "DHCPDISCOVER";
105 case DHCPOFFER: return "DHCPOFFER";
106 case DHCPREQUEST: return "DHCPREQUEST";
107 case DHCPDECLINE: return "DHCPDECLINE";
108 case DHCPACK: return "DHCPACK";
109 case DHCPNAK: return "DHCPNAK";
110 case DHCPRELEASE: return "DHCPRELEASE";
111 case DHCPINFORM: return "DHCPINFORM";
112 default: return "DHCP<invalid>";
117 * Calculate DHCP transaction ID for a network device
119 * @v netdev Network device
122 * Extract the least significant bits of the hardware address for use
123 * as the transaction ID.
125 static uint32_t dhcp_xid ( struct net_device *netdev ) {
128 memcpy ( &xid, ( netdev->ll_addr + netdev->ll_protocol->ll_addr_len
129 - sizeof ( xid ) ), sizeof ( xid ) );
133 /** Settings block name used for ProxyDHCP responses */
134 #define PROXYDHCP_SETTINGS_NAME "proxydhcp"
137 * Create a DHCP packet
139 * @v dhcppkt DHCP packet structure to fill in
140 * @v netdev Network device
141 * @v msgtype DHCP message type
142 * @v options Initial options to include (or NULL)
143 * @v data Buffer for DHCP packet
144 * @v max_len Size of DHCP packet buffer
145 * @ret rc Return status code
147 * Creates a DHCP packet in the specified buffer, and fills out a @c
148 * dhcp_packet structure that can be passed to
149 * set_dhcp_packet_option() or copy_dhcp_packet_options().
151 int create_dhcp_packet ( struct dhcp_packet *dhcppkt,
152 struct net_device *netdev, uint8_t msgtype,
153 struct dhcp_options *options,
154 void *data, size_t max_len ) {
155 struct dhcphdr *dhcphdr = data;
161 options_len = ( options ? options->len : 0 );
162 if ( max_len < ( sizeof ( *dhcphdr ) + options_len ) )
165 /* Initialise DHCP packet content */
166 memset ( dhcphdr, 0, max_len );
167 dhcphdr->xid = dhcp_xid ( netdev );
168 dhcphdr->magic = htonl ( DHCP_MAGIC_COOKIE );
169 dhcphdr->htype = ntohs ( netdev->ll_protocol->ll_proto );
170 dhcphdr->op = dhcp_op[msgtype];
171 /* If hardware length exceeds the chaddr field length, don't
172 * use the chaddr field. This is as per RFC4390.
174 hlen = netdev->ll_protocol->ll_addr_len;
175 if ( hlen > sizeof ( dhcphdr->chaddr ) ) {
177 dhcphdr->flags = htons ( BOOTP_FL_BROADCAST );
179 dhcphdr->hlen = hlen;
180 memcpy ( dhcphdr->chaddr, netdev->ll_addr, hlen );
181 memcpy ( dhcphdr->options, options->data, options_len );
183 /* Initialise DHCP packet structure and settings interface */
184 memset ( dhcppkt, 0, sizeof ( *dhcppkt ) );
185 dhcppkt_init ( dhcppkt, NULL, data, max_len );
187 /* Set DHCP_MESSAGE_TYPE option */
188 if ( ( rc = store_setting ( &dhcppkt->settings, DHCP_MESSAGE_TYPE,
189 &msgtype, sizeof ( msgtype ) ) ) != 0 )
195 /** DHCP network device descriptor */
196 struct dhcp_netdev_desc {
203 } __attribute__ (( packed ));
205 /** DHCP client identifier */
206 struct dhcp_client_id {
207 /** Link-layer protocol */
209 /** Link-layer address */
210 uint8_t ll_addr[MAX_LL_ADDR_LEN];
211 } __attribute__ (( packed ));
213 /** DHCP client UUID */
214 struct dhcp_client_uuid {
215 /** Identifier type */
219 } __attribute__ (( packed ));
221 #define DHCP_CLIENT_UUID_TYPE 0
224 * Create DHCP request packet
226 * @v dhcppkt DHCP packet structure to fill in
227 * @v netdev Network device
228 * @v dhcpoffer DHCPOFFER packet received from server
229 * @v data Buffer for DHCP packet
230 * @v max_len Size of DHCP packet buffer
231 * @ret rc Return status code
233 static int create_dhcp_request ( struct dhcp_packet *dhcppkt,
234 struct net_device *netdev,
235 struct dhcp_packet *dhcpoffer,
236 void *data, size_t max_len ) {
237 struct device_description *desc = &netdev->dev->desc;
238 struct dhcp_netdev_desc dhcp_desc;
239 struct dhcp_client_id client_id;
240 struct dhcp_client_uuid client_uuid;
241 unsigned int msgtype;
242 size_t dhcp_features_len;
246 /* Create DHCP packet */
247 msgtype = ( dhcpoffer ? DHCPREQUEST : DHCPDISCOVER );
248 if ( ( rc = create_dhcp_packet ( dhcppkt, netdev, msgtype,
249 &dhcp_request_options, data,
251 DBG ( "DHCP could not create DHCP packet: %s\n",
256 /* Copy any required options from previous server repsonse */
258 if ( ( rc = copy_setting ( &dhcppkt->settings,
259 DHCP_SERVER_IDENTIFIER,
260 &dhcpoffer->settings,
261 DHCP_SERVER_IDENTIFIER ) ) != 0 ) {
262 DBG ( "DHCP could not set server identifier "
263 "option: %s\n", strerror ( rc ) );
266 if ( ( rc = copy_setting ( &dhcppkt->settings,
267 DHCP_REQUESTED_ADDRESS,
268 &dhcpoffer->settings,
269 DHCP_EB_YIADDR ) ) != 0 ) {
270 DBG ( "DHCP could not set requested address "
271 "option: %s\n", strerror ( rc ) );
276 /* Add options to identify the feature list */
277 dhcp_features_len = ( dhcp_features_end - dhcp_features );
278 if ( ( rc = store_setting ( &dhcppkt->settings, DHCP_EB_ENCAP,
279 dhcp_features, dhcp_features_len ) ) !=0 ){
280 DBG ( "DHCP could not set features list option: %s\n",
285 /* Add options to identify the network device */
286 dhcp_desc.type = desc->bus_type;
287 dhcp_desc.vendor = htons ( desc->vendor );
288 dhcp_desc.device = htons ( desc->device );
289 if ( ( rc = store_setting ( &dhcppkt->settings, DHCP_EB_BUS_ID,
290 &dhcp_desc, sizeof ( dhcp_desc ) ) ) !=0 ){
291 DBG ( "DHCP could not set bus ID option: %s\n",
296 /* Add DHCP client identifier. Required for Infiniband, and
297 * doesn't hurt other link layers.
299 client_id.ll_proto = ntohs ( netdev->ll_protocol->ll_proto );
300 ll_addr_len = netdev->ll_protocol->ll_addr_len;
301 assert ( ll_addr_len <= sizeof ( client_id.ll_addr ) );
302 memcpy ( client_id.ll_addr, netdev->ll_addr, ll_addr_len );
303 if ( ( rc = store_setting ( &dhcppkt->settings, DHCP_CLIENT_ID,
304 &client_id, ( ll_addr_len + 1 ) ) ) != 0 ){
305 DBG ( "DHCP could not set client ID: %s\n",
310 /* Add client UUID, if we have one. Required for PXE. */
311 client_uuid.type = DHCP_CLIENT_UUID_TYPE;
312 if ( ( rc = get_uuid ( &client_uuid.uuid ) ) == 0 ) {
313 if ( ( rc = store_setting ( &dhcppkt->settings,
314 DHCP_CLIENT_UUID, &client_uuid,
315 sizeof ( client_uuid ) ) ) != 0 ) {
316 DBG ( "DHCP could not set client UUID: %s\n",
326 * Create DHCPDISCOVER packet
328 * @v netdev Network device
329 * @v data Buffer for DHCP packet
330 * @v max_len Size of DHCP packet buffer
331 * @ret rc Return status code
333 * Used by external code.
335 int create_dhcpdiscover ( struct net_device *netdev,
336 void *data, size_t max_len ) {
337 struct dhcp_packet dhcppkt;
339 return create_dhcp_request ( &dhcppkt, netdev, NULL, data, max_len );
343 * Create DHCPACK packet
345 * @v netdev Network device
346 * @v data Buffer for DHCP packet
347 * @v max_len Size of DHCP packet buffer
348 * @ret rc Return status code
350 * Used by external code.
352 int create_dhcpack ( struct net_device *netdev,
353 void *data, size_t max_len ) {
354 struct dhcp_packet dhcppkt;
357 /* Create base DHCPACK packet */
358 if ( ( rc = create_dhcp_packet ( &dhcppkt, netdev, DHCPACK, NULL,
359 data, max_len ) ) != 0 )
362 /* Merge in globally-scoped settings, then netdev-specific
363 * settings. Do it in this order so that netdev-specific
364 * settings take precedence regardless of stated priorities.
366 if ( ( rc = copy_settings ( &dhcppkt.settings, NULL ) ) != 0 )
368 if ( ( rc = copy_settings ( &dhcppkt.settings,
369 netdev_settings ( netdev ) ) ) != 0 )
376 * Create ProxyDHCPACK packet
378 * @v netdev Network device
379 * @v data Buffer for DHCP packet
380 * @v max_len Size of DHCP packet buffer
381 * @ret rc Return status code
383 * Used by external code.
385 int create_proxydhcpack ( struct net_device *netdev,
386 void *data, size_t max_len ) {
387 struct dhcp_packet dhcppkt;
388 struct settings *settings;
391 /* Identify ProxyDHCP settings */
392 settings = find_settings ( PROXYDHCP_SETTINGS_NAME );
394 /* No ProxyDHCP settings => return empty block */
396 memset ( data, 0, max_len );
400 /* Create base DHCPACK packet */
401 if ( ( rc = create_dhcp_packet ( &dhcppkt, netdev, DHCPACK, NULL,
402 data, max_len ) ) != 0 )
405 /* Merge in ProxyDHCP options */
406 if ( ( rc = copy_settings ( &dhcppkt.settings, settings ) ) != 0 )
412 /****************************************************************************
414 * DHCP packets contained in I/O buffers
418 /** A DHCP packet contained in an I/O buffer */
419 struct dhcp_iobuf_packet {
421 struct dhcp_packet dhcppkt;
422 /** Reference counter */
423 struct refcnt refcnt;
424 /** Containing I/O buffer */
425 struct io_buffer *iobuf;
429 * Free DHCP packet contained in an I/O buffer
431 * @v refcnt Reference counter
433 static void dhcpiob_free ( struct refcnt *refcnt ) {
434 struct dhcp_iobuf_packet *dhcpiob =
435 container_of ( refcnt, struct dhcp_iobuf_packet, refcnt );
437 free_iob ( dhcpiob->iobuf );
442 * Create DHCP packet from I/O buffer
444 * @v iobuf I/O buffer
445 * @ret dhcpiob DHCP packet contained in I/O buffer
447 * This function takes ownership of the I/O buffer. Future accesses
448 * must be via the @c dhcpiob data structure.
450 static struct dhcp_iobuf_packet * dhcpiob_create ( struct io_buffer *iobuf ) {
451 struct dhcp_iobuf_packet *dhcpiob;
453 dhcpiob = zalloc ( sizeof ( *dhcpiob ) );
455 dhcpiob->refcnt.free = dhcpiob_free;
456 dhcpiob->iobuf = iobuf;
457 dhcppkt_init ( &dhcpiob->dhcppkt, &dhcpiob->refcnt,
458 iobuf->data, iob_len ( iobuf ) );
463 static void dhcpiob_put ( struct dhcp_iobuf_packet *dhcpiob ) {
465 ref_put ( &dhcpiob->refcnt );
468 /****************************************************************************
470 * DHCP to UDP interface
474 /** A DHCP session */
475 struct dhcp_session {
476 /** Reference counter */
477 struct refcnt refcnt;
478 /** Job control interface */
479 struct job_interface job;
480 /** Data transfer interface */
481 struct xfer_interface xfer;
483 /** Network device being configured */
484 struct net_device *netdev;
486 /** State of the session
488 * This is a value for the @c DHCP_MESSAGE_TYPE option
489 * (e.g. @c DHCPDISCOVER).
492 /** Response obtained from DHCP server */
493 struct dhcp_iobuf_packet *response;
494 /** Response obtained from ProxyDHCP server */
495 struct dhcp_iobuf_packet *proxy_response;
496 /** Retransmission timer */
497 struct retry_timer timer;
498 /** Session start time (in ticks) */
505 * @v refcnt Reference counter
507 static void dhcp_free ( struct refcnt *refcnt ) {
508 struct dhcp_session *dhcp =
509 container_of ( refcnt, struct dhcp_session, refcnt );
511 netdev_put ( dhcp->netdev );
512 dhcpiob_put ( dhcp->response );
513 dhcpiob_put ( dhcp->proxy_response );
518 * Mark DHCP session as complete
520 * @v dhcp DHCP session
521 * @v rc Return status code
523 static void dhcp_finished ( struct dhcp_session *dhcp, int rc ) {
525 /* Block futher incoming messages */
526 job_nullify ( &dhcp->job );
527 xfer_nullify ( &dhcp->xfer );
529 /* Stop retry timer */
530 stop_timer ( &dhcp->timer );
532 /* Free resources and close interfaces */
533 xfer_close ( &dhcp->xfer, rc );
534 job_done ( &dhcp->job, rc );
538 * Register options received via DHCP
540 * @v dhcp DHCP session
541 * @ret rc Return status code
543 static int dhcp_register_settings ( struct dhcp_session *dhcp ) {
544 struct settings *old_settings;
545 struct settings *settings;
546 struct settings *parent;
549 /* Register ProxyDHCP settings, if present */
550 if ( dhcp->proxy_response ) {
551 settings = &dhcp->proxy_response->dhcppkt.settings;
552 settings->name = PROXYDHCP_SETTINGS_NAME;
553 old_settings = find_settings ( settings->name );
555 unregister_settings ( old_settings );
556 if ( ( rc = register_settings ( settings, NULL ) ) != 0 )
560 /* Register DHCP settings */
561 parent = netdev_settings ( dhcp->netdev );
562 settings = &dhcp->response->dhcppkt.settings;
563 old_settings = find_child_settings ( parent, settings->name );
565 unregister_settings ( old_settings );
566 if ( ( rc = register_settings ( settings, parent ) ) != 0 )
572 /****************************************************************************
574 * Data transfer interface
579 * Transmit DHCP request
581 * @v dhcp DHCP session
582 * @ret rc Return status code
584 static int dhcp_send_request ( struct dhcp_session *dhcp ) {
585 struct xfer_metadata meta = {
586 .netdev = dhcp->netdev,
588 struct io_buffer *iobuf;
589 struct dhcp_packet *dhcpoffer;
590 struct dhcp_packet dhcppkt;
593 DBGC ( dhcp, "DHCP %p transmitting %s\n",
594 dhcp, dhcp_msgtype_name ( dhcp->state ) );
596 assert ( ( dhcp->state == DHCPDISCOVER ) ||
597 ( dhcp->state == DHCPREQUEST ) );
599 /* Start retry timer. Do this first so that failures to
600 * transmit will be retried.
602 start_timer ( &dhcp->timer );
604 /* Allocate buffer for packet */
605 iobuf = xfer_alloc_iob ( &dhcp->xfer, DHCP_MIN_LEN );
609 /* Create DHCP packet in temporary buffer */
610 dhcpoffer = ( dhcp->response ? &dhcp->response->dhcppkt : NULL );
611 if ( ( rc = create_dhcp_request ( &dhcppkt, dhcp->netdev,
612 dhcpoffer, iobuf->data,
613 iob_tailroom ( iobuf ) ) ) != 0 ) {
614 DBGC ( dhcp, "DHCP %p could not construct DHCP request: %s\n",
615 dhcp, strerror ( rc ) );
619 /* Transmit the packet */
620 iob_put ( iobuf, dhcppkt.len );
621 rc = xfer_deliver_iob_meta ( &dhcp->xfer, iobuf, &meta );
624 DBGC ( dhcp, "DHCP %p could not transmit UDP packet: %s\n",
625 dhcp, strerror ( rc ) );
635 * Handle DHCP retry timer expiry
637 * @v timer DHCP retry timer
638 * @v fail Failure indicator
640 static void dhcp_timer_expired ( struct retry_timer *timer, int fail ) {
641 struct dhcp_session *dhcp =
642 container_of ( timer, struct dhcp_session, timer );
645 dhcp_finished ( dhcp, -ETIMEDOUT );
647 dhcp_send_request ( dhcp );
654 * @v xfer Data transfer interface
655 * @v iobuf I/O buffer
656 * @v data Received data
657 * @v len Length of received data
658 * @ret rc Return status code
660 static int dhcp_deliver_iob ( struct xfer_interface *xfer,
661 struct io_buffer *iobuf,
662 struct xfer_metadata *meta __unused ) {
663 struct dhcp_session *dhcp =
664 container_of ( xfer, struct dhcp_session, xfer );
665 struct dhcp_iobuf_packet *response;
666 struct dhcp_iobuf_packet **store_response;
667 struct dhcphdr *dhcphdr;
668 struct settings *settings;
669 unsigned int msgtype;
670 unsigned long elapsed;
675 /* Convert packet into a DHCP-packet-in-iobuf */
676 response = dhcpiob_create ( iobuf );
678 DBGC ( dhcp, "DHCP %p could not store DHCP packet\n", dhcp );
681 dhcphdr = response->dhcppkt.dhcphdr;
682 settings = &response->dhcppkt.settings;
684 /* Check for matching transaction ID */
685 if ( dhcphdr->xid != dhcp_xid ( dhcp->netdev ) ) {
686 DBGC ( dhcp, "DHCP %p wrong transaction ID (wanted %08lx, "
687 "got %08lx)\n", dhcp, ntohl ( dhcphdr->xid ),
688 ntohl ( dhcp_xid ( dhcp->netdev ) ) );
692 /* Determine and verify message type */
693 is_proxy = ( dhcphdr->yiaddr.s_addr == 0 );
694 msgtype = fetch_uintz_setting ( settings, DHCP_MESSAGE_TYPE );
695 DBGC ( dhcp, "DHCP %p received %s%s\n", dhcp,
696 ( is_proxy ? "Proxy" : "" ), dhcp_msgtype_name ( msgtype ) );
697 if ( ( ( dhcp->state != DHCPDISCOVER ) || ( msgtype != DHCPOFFER ) ) &&
698 ( ( dhcp->state != DHCPREQUEST ) || ( msgtype != DHCPACK ) ) ) {
699 DBGC ( dhcp, "DHCP %p discarding %s while in %s state\n",
700 dhcp, dhcp_msgtype_name ( msgtype ),
701 dhcp_msgtype_name ( dhcp->state ) );
705 /* Update stored standard/ProxyDHCP options, if the new
706 * options have equal or higher priority than the
707 * currently-stored options.
709 store_response = ( is_proxy ? &dhcp->proxy_response : &dhcp->response);
710 if ( ( ! *store_response ) ||
711 ( fetch_uintz_setting ( settings, DHCP_EB_PRIORITY ) >=
712 fetch_uintz_setting ( &(*store_response)->dhcppkt.settings,
713 DHCP_EB_PRIORITY ) ) ) {
714 dhcpiob_put ( *store_response );
715 *store_response = response;
717 dhcpiob_put ( response );
720 /* If we don't yet have a standard DHCP response (i.e. one
721 * with an IP address), then just leave the timer running.
723 if ( ! dhcp->response )
726 /* Handle DHCP response */
727 ignore_proxy = fetch_uintz_setting ( &dhcp->response->dhcppkt.settings,
728 DHCP_EB_NO_PROXYDHCP );
729 switch ( dhcp->state ) {
731 /* If we have allowed sufficient time for ProxyDHCP
732 * reponses, then transition to making the DHCPREQUEST.
734 elapsed = ( currticks() - dhcp->start );
735 if ( ignore_proxy || ( elapsed > PROXYDHCP_WAIT_TIME ) ) {
736 stop_timer ( &dhcp->timer );
737 dhcp->state = DHCPREQUEST;
738 dhcp_send_request ( dhcp );
742 /* DHCP finished; register options and exit */
743 if ( ignore_proxy && dhcp->proxy_response ) {
744 dhcpiob_put ( dhcp->proxy_response );
745 dhcp->proxy_response = NULL;
747 if ( ( rc = dhcp_register_settings ( dhcp ) ) != 0 ) {
748 dhcp_finished ( dhcp, rc );
751 dhcp_finished ( dhcp, 0 );
761 dhcpiob_put ( response );
765 /** DHCP data transfer interface operations */
766 static struct xfer_interface_operations dhcp_xfer_operations = {
767 .close = ignore_xfer_close,
768 .vredirect = xfer_vopen,
769 .window = unlimited_xfer_window,
770 .alloc_iob = default_xfer_alloc_iob,
771 .deliver_iob = dhcp_deliver_iob,
772 .deliver_raw = xfer_deliver_as_iob,
775 /****************************************************************************
777 * Job control interface
782 * Handle kill() event received via job control interface
784 * @v job DHCP job control interface
786 static void dhcp_job_kill ( struct job_interface *job ) {
787 struct dhcp_session *dhcp =
788 container_of ( job, struct dhcp_session, job );
790 /* Terminate DHCP session */
791 dhcp_finished ( dhcp, -ECANCELED );
794 /** DHCP job control interface operations */
795 static struct job_interface_operations dhcp_job_operations = {
796 .done = ignore_job_done,
797 .kill = dhcp_job_kill,
798 .progress = ignore_job_progress,
801 /****************************************************************************
808 * Start DHCP on a network device
810 * @v job Job control interface
811 * @v netdev Network device
812 * @v register_options DHCP option block registration routine
813 * @ret rc Return status code
815 * Starts DHCP on the specified network device. If successful, the @c
816 * register_options() routine will be called with the acquired
819 int start_dhcp ( struct job_interface *job, struct net_device *netdev ) {
820 static struct sockaddr_in server = {
821 .sin_family = AF_INET,
822 .sin_addr.s_addr = INADDR_BROADCAST,
823 .sin_port = htons ( BOOTPS_PORT ),
825 static struct sockaddr_in client = {
826 .sin_family = AF_INET,
827 .sin_port = htons ( BOOTPC_PORT ),
829 struct dhcp_session *dhcp;
832 /* Allocate and initialise structure */
833 dhcp = zalloc ( sizeof ( *dhcp ) );
836 dhcp->refcnt.free = dhcp_free;
837 job_init ( &dhcp->job, &dhcp_job_operations, &dhcp->refcnt );
838 xfer_init ( &dhcp->xfer, &dhcp_xfer_operations, &dhcp->refcnt );
839 dhcp->netdev = netdev_get ( netdev );
840 dhcp->timer.expired = dhcp_timer_expired;
841 dhcp->state = DHCPDISCOVER;
842 dhcp->start = currticks();
844 /* Instantiate child objects and attach to our interfaces */
845 if ( ( rc = xfer_open_socket ( &dhcp->xfer, SOCK_DGRAM,
846 ( struct sockaddr * ) &server,
847 ( struct sockaddr * ) &client ) ) != 0 )
850 /* Start timer to initiate initial DHCPREQUEST */
851 start_timer_nodelay ( &dhcp->timer );
853 /* Attach parent interface, mortalise self, and return */
854 job_plug_plug ( &dhcp->job, job );
855 ref_put ( &dhcp->refcnt );
859 dhcp_finished ( dhcp, rc );
860 ref_put ( &dhcp->refcnt );