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>
34 #include <gpxe/dhcp.h>
38 * Dynamic Host Configuration Protocol
42 /** DHCP operation types
44 * This table maps from DHCP message types (i.e. values of the @c
45 * DHCP_MESSAGE_TYPE option) to values of the "op" field within a DHCP
48 static const uint8_t dhcp_op[] = {
49 [DHCPDISCOVER] = BOOTP_REQUEST,
50 [DHCPOFFER] = BOOTP_REPLY,
51 [DHCPREQUEST] = BOOTP_REQUEST,
52 [DHCPDECLINE] = BOOTP_REQUEST,
53 [DHCPACK] = BOOTP_REPLY,
54 [DHCPNAK] = BOOTP_REPLY,
55 [DHCPRELEASE] = BOOTP_REQUEST,
56 [DHCPINFORM] = BOOTP_REQUEST,
59 /** Raw option data for options common to all DHCP requests */
60 static uint8_t dhcp_request_options_data[] = {
61 DHCP_MAX_MESSAGE_SIZE, DHCP_WORD ( ETH_MAX_MTU ),
63 DHCP_STRING ( 'E', 't', 'h', 'e', 'r', 'b', 'o', 'o', 't' ),
64 DHCP_PARAMETER_REQUEST_LIST,
65 DHCP_OPTION ( DHCP_SUBNET_MASK, DHCP_ROUTERS, DHCP_DNS_SERVERS, DHCP_LOG_SERVERS,
66 DHCP_HOST_NAME, DHCP_DOMAIN_NAME, DHCP_ROOT_PATH,
67 DHCP_VENDOR_ENCAP, DHCP_TFTP_SERVER_NAME,
68 DHCP_BOOTFILE_NAME, DHCP_EB_ENCAP,
69 DHCP_ISCSI_INITIATOR_IQN ),
74 * Name a DHCP packet type
76 * @v msgtype DHCP message type
77 * @ret string DHCP mesasge type name
79 static inline const char * dhcp_msgtype_name ( unsigned int msgtype ) {
81 case 0: return "BOOTP"; /* Non-DHCP packet */
82 case DHCPDISCOVER: return "DHCPDISCOVER";
83 case DHCPOFFER: return "DHCPOFFER";
84 case DHCPREQUEST: return "DHCPREQUEST";
85 case DHCPDECLINE: return "DHCPDECLINE";
86 case DHCPACK: return "DHCPACK";
87 case DHCPNAK: return "DHCPNAK";
88 case DHCPRELEASE: return "DHCPRELEASE";
89 case DHCPINFORM: return "DHCPINFORM";
90 default: return "DHCP<invalid>";
95 * Calculate DHCP transaction ID for a network device
97 * @v netdev Network device
100 * Extract the least significant bits of the hardware address for use
101 * as the transaction ID.
103 static uint32_t dhcp_xid ( struct net_device *netdev ) {
106 memcpy ( &xid, ( netdev->ll_addr + netdev->ll_protocol->ll_addr_len
107 - sizeof ( xid ) ), sizeof ( xid ) );
111 /** Options common to all DHCP requests */
112 struct dhcp_option_block dhcp_request_options = {
113 .data = dhcp_request_options_data,
114 .max_len = sizeof ( dhcp_request_options_data ),
115 .len = sizeof ( dhcp_request_options_data ),
119 * Set option within DHCP packet
121 * @v dhcppkt DHCP packet
122 * @v tag DHCP option tag
123 * @v data New value for DHCP option
124 * @v len Length of value, in bytes
125 * @ret rc Return status code
127 * Sets the option within the first available options block within the
128 * DHCP packet. Option blocks are tried in the order specified by @c
129 * dhcp_option_block_fill_order.
131 * The magic options @c DHCP_EB_YIADDR and @c DHCP_EB_SIADDR are
132 * intercepted and inserted into the appropriate fixed fields within
133 * the DHCP packet. The option @c DHCP_OPTION_OVERLOAD is silently
134 * ignored, since our DHCP packet assembly method relies on always
135 * having option overloading in use.
137 static int set_dhcp_packet_option ( struct dhcp_packet *dhcppkt,
138 unsigned int tag, const void *data,
140 struct dhcphdr *dhcphdr = dhcppkt->dhcphdr;
141 struct dhcp_option_block *options = dhcppkt->options;
142 struct dhcp_option *option = NULL;
144 /* Special-case the magic options */
146 case DHCP_OPTION_OVERLOAD:
147 /* Hard-coded in packets we create; always ignore */
150 memcpy ( &dhcphdr->yiaddr, data, sizeof ( dhcphdr->yiaddr ) );
153 memcpy ( &dhcphdr->siaddr, data, sizeof ( dhcphdr->siaddr ) );
155 case DHCP_MESSAGE_TYPE:
156 case DHCP_REQUESTED_ADDRESS:
157 case DHCP_PARAMETER_REQUEST_LIST:
158 /* These options have to be within the main options
159 * block. This doesn't seem to be required by the
160 * RFCs, but at least ISC dhcpd refuses to recognise
163 options = &dhcppkt->options[OPTS_MAIN];
166 /* Continue processing as normal */
170 /* Set option in first available options block */
171 for ( ; options < &dhcppkt->options[NUM_OPT_BLOCKS] ; options++ ) {
172 option = set_dhcp_option ( options, tag, data, len );
177 /* Update DHCP packet length */
178 dhcppkt->len = ( offsetof ( typeof ( *dhcppkt->dhcphdr ), options )
179 + dhcppkt->options[OPTS_MAIN].len );
181 return ( option ? 0 : -ENOSPC );
185 * Copy option into DHCP packet
187 * @v dhcppkt DHCP packet
188 * @v options DHCP option block, or NULL
189 * @v tag DHCP option tag to search for
190 * @v new_tag DHCP option tag to use for copied option
191 * @ret rc Return status code
193 * Copies a single option, if present, from the DHCP options block
194 * into a DHCP packet. The tag for the option may be changed if
195 * desired; this is required by other parts of the DHCP code.
197 * @c options may specify a single options block, or be left as NULL
198 * in order to search for the option within all registered options
201 static int copy_dhcp_packet_option ( struct dhcp_packet *dhcppkt,
202 struct dhcp_option_block *options,
203 unsigned int tag, unsigned int new_tag ) {
204 struct dhcp_option *option;
207 option = find_dhcp_option ( options, tag );
209 if ( ( rc = set_dhcp_packet_option ( dhcppkt, new_tag,
211 option->len ) ) != 0 )
218 * Copy options into DHCP packet
220 * @v dhcppkt DHCP packet
221 * @v options DHCP option block, or NULL
222 * @v encapsulator Encapsulating option, or zero
223 * @ret rc Return status code
225 * Copies options with the specified encapsulator from DHCP options
226 * blocks into a DHCP packet. Most options are copied verbatim.
227 * Recognised encapsulated options fields are handled as such.
229 * @c options may specify a single options block, or be left as NULL
230 * in order to copy options from all registered options blocks.
232 static int copy_dhcp_packet_encap_options ( struct dhcp_packet *dhcppkt,
233 struct dhcp_option_block *options,
234 unsigned int encapsulator ) {
239 for ( subtag = DHCP_MIN_OPTION; subtag <= DHCP_MAX_OPTION; subtag++ ) {
240 tag = DHCP_ENCAP_OPT ( encapsulator, subtag );
243 case DHCP_VENDOR_ENCAP:
244 /* Process encapsulated options field */
245 if ( ( rc = copy_dhcp_packet_encap_options ( dhcppkt,
251 /* Copy option to reassembled packet */
252 if ( ( rc = copy_dhcp_packet_option ( dhcppkt, options,
263 * Copy options into DHCP packet
265 * @v dhcppkt DHCP packet
266 * @v options DHCP option block, or NULL
267 * @ret rc Return status code
269 * Copies options from DHCP options blocks into a DHCP packet. Most
270 * options are copied verbatim. Recognised encapsulated options
271 * fields are handled as such.
273 * @c options may specify a single options block, or be left as NULL
274 * in order to copy options from all registered options blocks.
276 int copy_dhcp_packet_options ( struct dhcp_packet *dhcppkt,
277 struct dhcp_option_block *options ) {
278 return copy_dhcp_packet_encap_options ( dhcppkt, options, 0 );
282 * Create a DHCP packet
284 * @v netdev Network device
285 * @v msgtype DHCP message type
286 * @v data Buffer for DHCP packet
287 * @v max_len Size of DHCP packet buffer
288 * @v dhcppkt DHCP packet structure to fill in
289 * @ret rc Return status code
291 * Creates a DHCP packet in the specified buffer, and fills out a @c
292 * dhcp_packet structure that can be passed to
293 * set_dhcp_packet_option() or copy_dhcp_packet_options().
295 int create_dhcp_packet ( struct net_device *netdev, uint8_t msgtype,
296 void *data, size_t max_len,
297 struct dhcp_packet *dhcppkt ) {
298 struct dhcphdr *dhcphdr = data;
299 static const uint8_t overloading = ( DHCP_OPTION_OVERLOAD_FILE |
300 DHCP_OPTION_OVERLOAD_SNAME );
304 if ( max_len < sizeof ( *dhcphdr ) )
307 /* Initialise DHCP packet content */
308 memset ( dhcphdr, 0, max_len );
309 dhcphdr->xid = dhcp_xid ( netdev );
310 dhcphdr->magic = htonl ( DHCP_MAGIC_COOKIE );
311 dhcphdr->htype = ntohs ( netdev->ll_protocol->ll_proto );
312 dhcphdr->hlen = netdev->ll_protocol->ll_addr_len;
313 memcpy ( dhcphdr->chaddr, netdev->ll_addr, dhcphdr->hlen );
314 dhcphdr->op = dhcp_op[msgtype];
316 /* Initialise DHCP packet structure */
317 dhcppkt->dhcphdr = dhcphdr;
318 dhcppkt->max_len = max_len;
319 init_dhcp_options ( &dhcppkt->options[OPTS_MAIN], dhcphdr->options,
321 offsetof ( typeof ( *dhcphdr ), options ) ) );
322 init_dhcp_options ( &dhcppkt->options[OPTS_FILE], dhcphdr->file,
323 sizeof ( dhcphdr->file ) );
324 init_dhcp_options ( &dhcppkt->options[OPTS_SNAME], dhcphdr->sname,
325 sizeof ( dhcphdr->sname ) );
327 /* Set DHCP_OPTION_OVERLOAD option within the main options block */
328 if ( set_dhcp_option ( &dhcppkt->options[OPTS_MAIN],
329 DHCP_OPTION_OVERLOAD, &overloading,
330 sizeof ( overloading ) ) == NULL )
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 * DHCP to UDP interface
497 /** A DHCP session */
498 struct dhcp_session {
499 /** Reference counter */
500 struct refcnt refcnt;
501 /** Job control interface */
502 struct job_interface job;
503 /** Data transfer interface */
504 struct xfer_interface xfer;
506 /** Network device being configured */
507 struct net_device *netdev;
508 /** Option block registration routine */
509 int ( * register_options ) ( struct net_device *netdev,
510 struct dhcp_option_block *options );
512 /** State of the session
514 * This is a value for the @c DHCP_MESSAGE_TYPE option
515 * (e.g. @c DHCPDISCOVER).
518 /** Options obtained from server */
519 struct dhcp_option_block *options;
520 /** Retransmission timer */
521 struct retry_timer timer;
527 * @v refcnt Reference counter
529 static void dhcp_free ( struct refcnt *refcnt ) {
530 struct dhcp_session *dhcp =
531 container_of ( refcnt, struct dhcp_session, refcnt );
533 netdev_put ( dhcp->netdev );
534 dhcpopt_put ( dhcp->options );
539 * Mark DHCP session as complete
541 * @v dhcp DHCP session
542 * @v rc Return status code
544 static void dhcp_finished ( struct dhcp_session *dhcp, int rc ) {
546 /* Block futher incoming messages */
547 job_nullify ( &dhcp->job );
548 xfer_nullify ( &dhcp->xfer );
550 /* Stop retry timer */
551 stop_timer ( &dhcp->timer );
553 /* Free resources and close interfaces */
554 xfer_close ( &dhcp->xfer, rc );
555 job_done ( &dhcp->job, rc );
558 /****************************************************************************
560 * Data transfer interface
565 * Transmit DHCP request
567 * @v dhcp DHCP session
568 * @ret rc Return status code
570 static int dhcp_send_request ( struct dhcp_session *dhcp ) {
571 struct xfer_metadata meta = {
572 .netdev = dhcp->netdev,
574 struct dhcp_packet dhcppkt;
575 struct io_buffer *iobuf;
578 DBGC ( dhcp, "DHCP %p transmitting %s\n",
579 dhcp, dhcp_msgtype_name ( dhcp->state ) );
581 assert ( ( dhcp->state == DHCPDISCOVER ) ||
582 ( dhcp->state == DHCPREQUEST ) );
584 /* Start retry timer. Do this first so that failures to
585 * transmit will be retried.
587 start_timer ( &dhcp->timer );
589 /* Allocate buffer for packet */
590 iobuf = xfer_alloc_iob ( &dhcp->xfer, DHCP_MIN_LEN );
594 /* Create DHCP packet in temporary buffer */
595 if ( ( rc = create_dhcp_packet ( dhcp->netdev, dhcp->state,
596 iobuf->data, iob_tailroom ( iobuf ),
597 &dhcppkt ) ) != 0 ) {
598 DBGC ( dhcp, "DHCP %p could not create DHCP packet: %s\n",
599 dhcp, strerror ( rc ) );
603 /* Copy in options common to all requests */
604 if ( ( rc = copy_dhcp_packet_options ( &dhcppkt,
605 &dhcp_request_options ) ) != 0){
606 DBGC ( dhcp, "DHCP %p could not set common DHCP options: %s\n",
607 dhcp, strerror ( rc ) );
611 /* Copy any required options from previous server repsonse */
612 if ( dhcp->options ) {
613 if ( ( rc = copy_dhcp_packet_option ( &dhcppkt, dhcp->options,
614 DHCP_SERVER_IDENTIFIER,
615 DHCP_SERVER_IDENTIFIER ) ) != 0 ) {
616 DBGC ( dhcp, "DHCP %p could not set server identifier "
617 "option: %s\n", dhcp, strerror ( rc ) );
620 if ( ( rc = copy_dhcp_packet_option ( &dhcppkt, dhcp->options,
622 DHCP_REQUESTED_ADDRESS ) ) != 0 ) {
623 DBGC ( dhcp, "DHCP %p could not set requested address "
624 "option: %s\n", dhcp, strerror ( rc ) );
629 /* Transmit the packet */
630 iob_put ( iobuf, dhcppkt.len );
631 rc = xfer_deliver_iob_meta ( &dhcp->xfer, iobuf, &meta );
634 DBGC ( dhcp, "DHCP %p could not transmit UDP packet: %s\n",
635 dhcp, strerror ( rc ) );
645 * Handle DHCP retry timer expiry
647 * @v timer DHCP retry timer
648 * @v fail Failure indicator
650 static void dhcp_timer_expired ( struct retry_timer *timer, int fail ) {
651 struct dhcp_session *dhcp =
652 container_of ( timer, struct dhcp_session, timer );
655 dhcp_finished ( dhcp, -ETIMEDOUT );
657 dhcp_send_request ( dhcp );
664 * @v xfer Data transfer interface
665 * @v iobuf I/O buffer
666 * @v data Received data
667 * @v len Length of received data
668 * @ret rc Return status code
670 static int dhcp_deliver_raw ( struct xfer_interface *xfer,
671 const void *data, size_t len ) {
672 struct dhcp_session *dhcp =
673 container_of ( xfer, struct dhcp_session, xfer );
674 const struct dhcphdr *dhcphdr = data;
675 struct dhcp_option_block *options;
676 unsigned int msgtype;
678 /* Check for matching transaction ID */
679 if ( dhcphdr->xid != dhcp_xid ( dhcp->netdev ) ) {
680 DBGC ( dhcp, "DHCP %p wrong transaction ID (wanted %08lx, "
681 "got %08lx)\n", dhcp, ntohl ( dhcphdr->xid ),
682 ntohl ( dhcp_xid ( dhcp->netdev ) ) );
686 /* Parse packet and create options structure */
687 options = dhcp_parse ( dhcphdr, len );
689 DBGC ( dhcp, "DHCP %p could not parse DHCP packet\n", dhcp );
693 /* Determine message type */
694 msgtype = find_dhcp_num_option ( options, DHCP_MESSAGE_TYPE );
695 DBGC ( dhcp, "DHCP %p received %s\n",
696 dhcp, dhcp_msgtype_name ( msgtype ) );
698 /* Handle DHCP reply */
699 switch ( dhcp->state ) {
701 if ( msgtype != DHCPOFFER )
703 dhcp->state = DHCPREQUEST;
706 if ( msgtype != DHCPACK )
708 dhcp->state = DHCPACK;
715 /* Stop timer and update stored options */
716 stop_timer ( &dhcp->timer );
718 dhcpopt_put ( dhcp->options );
719 dhcp->options = options;
721 /* Transmit next packet, or terminate session */
722 if ( dhcp->state < DHCPACK ) {
723 dhcp_send_request ( dhcp );
725 dhcp->register_options ( dhcp->netdev, dhcp->options );
726 dhcp_finished ( dhcp, 0 );
731 dhcpopt_put ( options );
735 /** DHCP data transfer interface operations */
736 static struct xfer_interface_operations dhcp_xfer_operations = {
737 .close = ignore_xfer_close,
738 .vredirect = xfer_vopen,
739 .request = ignore_xfer_request,
740 .seek = ignore_xfer_seek,
741 .deliver_iob = xfer_deliver_as_raw,
742 .deliver_raw = dhcp_deliver_raw,
745 /****************************************************************************
747 * Job control interface
752 * Handle kill() event received via job control interface
754 * @v job DHCP job control interface
756 static void dhcp_job_kill ( struct job_interface *job ) {
757 struct dhcp_session *dhcp =
758 container_of ( job, struct dhcp_session, job );
760 /* Terminate DHCP session */
761 dhcp_finished ( dhcp, -ECANCELED );
764 /** DHCP job control interface operations */
765 static struct job_interface_operations dhcp_job_operations = {
766 .done = ignore_job_done,
767 .kill = dhcp_job_kill,
768 .progress = ignore_job_progress,
771 /****************************************************************************
778 * Start DHCP on a network device
780 * @v job Job control interface
781 * @v netdev Network device
782 * @v register_options DHCP option block registration routine
783 * @ret rc Return status code
785 * Starts DHCP on the specified network device. If successful, the @c
786 * register_options() routine will be called with the acquired
789 int start_dhcp ( struct job_interface *job, struct net_device *netdev,
790 int ( * register_options ) ( struct net_device *netdev,
791 struct dhcp_option_block * ) ) {
792 static struct sockaddr_in server = {
793 .sin_family = AF_INET,
794 .sin_addr.s_addr = INADDR_BROADCAST,
795 .sin_port = htons ( BOOTPS_PORT ),
797 static struct sockaddr_in client = {
798 .sin_family = AF_INET,
799 .sin_port = htons ( BOOTPC_PORT ),
801 struct dhcp_session *dhcp;
804 /* Allocate and initialise structure */
805 dhcp = malloc ( sizeof ( *dhcp ) );
808 memset ( dhcp, 0, sizeof ( *dhcp ) );
809 dhcp->refcnt.free = dhcp_free;
810 job_init ( &dhcp->job, &dhcp_job_operations, &dhcp->refcnt );
811 xfer_init ( &dhcp->xfer, &dhcp_xfer_operations, &dhcp->refcnt );
812 dhcp->netdev = netdev_get ( netdev );
813 dhcp->register_options = register_options;
814 dhcp->timer.expired = dhcp_timer_expired;
815 dhcp->state = DHCPDISCOVER;
817 /* Instantiate child objects and attach to our interfaces */
818 if ( ( rc = xfer_open_socket ( &dhcp->xfer, SOCK_DGRAM,
819 ( struct sockaddr * ) &server,
820 ( struct sockaddr * ) &client ) ) != 0 )
823 /* Start timer to initiate initial DHCPREQUEST */
824 start_timer ( &dhcp->timer );
826 /* Attach parent interface, mortalise self, and return */
827 job_plug_plug ( &dhcp->job, job );
828 ref_put ( &dhcp->refcnt );
832 dhcp_finished ( dhcp, rc );
833 ref_put ( &dhcp->refcnt );
837 /****************************************************************************
839 * Network device configurator
843 /* Avoid dragging in dns.o */
844 struct sockaddr_tcpip nameserver;
846 /* Avoid dragging in syslog.o */
847 struct in_addr syslogserver;
850 * Configure network device from DHCP options
852 * @v netdev Network device
853 * @v options DHCP options block
854 * @ret rc Return status code
856 int dhcp_configure_netdev ( struct net_device *netdev,
857 struct dhcp_option_block *options ) {
858 struct in_addr address = { 0 };
859 struct in_addr netmask = { 0 };
860 struct in_addr gateway = { INADDR_NONE };
861 struct sockaddr_in *sin_nameserver;
862 struct in_addr tftp_server;
867 /* Clear any existing routing table entry */
868 del_ipv4_address ( netdev );
870 /* Retrieve IP address configuration */
871 find_dhcp_ipv4_option ( options, DHCP_EB_YIADDR, &address );
872 find_dhcp_ipv4_option ( options, DHCP_SUBNET_MASK, &netmask );
873 find_dhcp_ipv4_option ( options, DHCP_ROUTERS, &gateway );
875 /* Set up new IP address configuration */
876 if ( ( rc = add_ipv4_address ( netdev, address, netmask,
878 DBG ( "Could not configure %s with DHCP results: %s\n",
879 netdev->name, strerror ( rc ) );
883 /* Retrieve other DHCP options that we care about */
884 sin_nameserver = ( struct sockaddr_in * ) &nameserver;
885 sin_nameserver->sin_family = AF_INET;
886 find_dhcp_ipv4_option ( options, DHCP_DNS_SERVERS,
887 &sin_nameserver->sin_addr );
888 find_dhcp_ipv4_option ( options, DHCP_LOG_SERVERS,
891 /* Set current working URI based on TFTP server */
892 find_dhcp_ipv4_option ( options, DHCP_EB_SIADDR, &tftp_server );
893 snprintf ( uri_string, sizeof ( uri_string ),
894 "tftp://%s/", inet_ntoa ( tftp_server ) );
895 uri = parse_uri ( uri_string );