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.
23 #include <gpxe/if_ether.h>
24 #include <gpxe/netdevice.h>
26 #include <gpxe/dhcp.h>
30 * Dynamic Host Configuration Protocol
34 /** DHCP operation types
36 * This table maps from DHCP message types (i.e. values of the @c
37 * DHCP_MESSAGE_TYPE option) to values of the "op" field within a DHCP
40 static const uint8_t dhcp_op[] = {
41 [DHCPDISCOVER] = BOOTP_REQUEST,
42 [DHCPOFFER] = BOOTP_REPLY,
43 [DHCPREQUEST] = BOOTP_REQUEST,
44 [DHCPDECLINE] = BOOTP_REQUEST,
45 [DHCPACK] = BOOTP_REPLY,
46 [DHCPNAK] = BOOTP_REPLY,
47 [DHCPRELEASE] = BOOTP_REQUEST,
48 [DHCPINFORM] = BOOTP_REQUEST,
51 /** Raw option data for options common to all DHCP requests */
52 static uint8_t dhcp_request_options_data[] = {
53 DHCP_MAX_MESSAGE_SIZE, DHCP_WORD ( ETH_MAX_MTU ),
55 DHCP_STRING ( 'E', 't', 'h', 'e', 'r', 'b', 'o', 'o', 't' ),
56 DHCP_PARAMETER_REQUEST_LIST,
57 DHCP_OPTION ( DHCP_SUBNET_MASK, DHCP_ROUTERS, DHCP_DNS_SERVERS,
58 DHCP_HOST_NAME, DHCP_DOMAIN_NAME, DHCP_ROOT_PATH,
59 DHCP_VENDOR_ENCAP, DHCP_TFTP_SERVER_NAME,
60 DHCP_BOOTFILE_NAME, DHCP_EB_ENCAP,
61 DHCP_ISCSI_INITIATOR_IQN ),
66 * Name a DHCP packet type
68 * @v msgtype DHCP message type
69 * @ret string DHCP mesasge type name
71 static inline const char * dhcp_msgtype_name ( unsigned int msgtype ) {
73 case 0: return "BOOTP"; /* Non-DHCP packet */
74 case DHCPDISCOVER: return "DHCPDISCOVER";
75 case DHCPOFFER: return "DHCPOFFER";
76 case DHCPREQUEST: return "DHCPREQUEST";
77 case DHCPDECLINE: return "DHCPDECLINE";
78 case DHCPACK: return "DHCPACK";
79 case DHCPNAK: return "DHCPNAK";
80 case DHCPRELEASE: return "DHCPRELEASE";
81 case DHCPINFORM: return "DHCPINFORM";
82 default: return "DHCP<invalid>";
87 * Calculate DHCP transaction ID for a network device
89 * @v netdev Network device
92 * Extract the least significant bits of the hardware address for use
93 * as the transaction ID.
95 static uint32_t dhcp_xid ( struct net_device *netdev ) {
98 memcpy ( &xid, ( netdev->ll_addr + netdev->ll_protocol->ll_addr_len
99 - sizeof ( xid ) ), sizeof ( xid ) );
103 /** Options common to all DHCP requests */
104 struct dhcp_option_block dhcp_request_options = {
105 .data = dhcp_request_options_data,
106 .max_len = sizeof ( dhcp_request_options_data ),
107 .len = sizeof ( dhcp_request_options_data ),
111 * Set option within DHCP packet
113 * @v dhcppkt DHCP packet
114 * @v tag DHCP option tag
115 * @v data New value for DHCP option
116 * @v len Length of value, in bytes
117 * @ret rc Return status code
119 * Sets the option within the first available options block within the
120 * DHCP packet. Option blocks are tried in the order specified by @c
121 * dhcp_option_block_fill_order.
123 * The magic options @c DHCP_EB_YIADDR and @c DHCP_EB_SIADDR are
124 * intercepted and inserted into the appropriate fixed fields within
125 * the DHCP packet. The option @c DHCP_OPTION_OVERLOAD is silently
126 * ignored, since our DHCP packet assembly method relies on always
127 * having option overloading in use.
129 static int set_dhcp_packet_option ( struct dhcp_packet *dhcppkt,
130 unsigned int tag, const void *data,
132 struct dhcphdr *dhcphdr = dhcppkt->dhcphdr;
133 struct dhcp_option_block *options = dhcppkt->options;
134 struct dhcp_option *option = NULL;
136 /* Special-case the magic options */
138 case DHCP_OPTION_OVERLOAD:
139 /* Hard-coded in packets we create; always ignore */
142 memcpy ( &dhcphdr->yiaddr, data, sizeof ( dhcphdr->yiaddr ) );
145 memcpy ( &dhcphdr->siaddr, data, sizeof ( dhcphdr->siaddr ) );
147 case DHCP_MESSAGE_TYPE:
148 case DHCP_REQUESTED_ADDRESS:
149 case DHCP_PARAMETER_REQUEST_LIST:
150 /* These options have to be within the main options
151 * block. This doesn't seem to be required by the
152 * RFCs, but at least ISC dhcpd refuses to recognise
155 options = &dhcppkt->options[OPTS_MAIN];
158 /* Continue processing as normal */
162 /* Set option in first available options block */
163 for ( ; options < &dhcppkt->options[NUM_OPT_BLOCKS] ; options++ ) {
164 option = set_dhcp_option ( options, tag, data, len );
169 /* Update DHCP packet length */
170 dhcppkt->len = ( offsetof ( typeof ( *dhcppkt->dhcphdr ), options )
171 + dhcppkt->options[OPTS_MAIN].len );
173 return ( option ? 0 : -ENOSPC );
177 * Copy option into DHCP packet
179 * @v dhcppkt DHCP packet
180 * @v options DHCP option block, or NULL
181 * @v tag DHCP option tag to search for
182 * @v new_tag DHCP option tag to use for copied option
183 * @ret rc Return status code
185 * Copies a single option, if present, from the DHCP options block
186 * into a DHCP packet. The tag for the option may be changed if
187 * desired; this is required by other parts of the DHCP code.
189 * @c options may specify a single options block, or be left as NULL
190 * in order to search for the option within all registered options
193 static int copy_dhcp_packet_option ( struct dhcp_packet *dhcppkt,
194 struct dhcp_option_block *options,
195 unsigned int tag, unsigned int new_tag ) {
196 struct dhcp_option *option;
199 option = find_dhcp_option ( options, tag );
201 if ( ( rc = set_dhcp_packet_option ( dhcppkt, new_tag,
203 option->len ) ) != 0 )
210 * Copy options into DHCP packet
212 * @v dhcppkt DHCP packet
213 * @v options DHCP option block, or NULL
214 * @v encapsulator Encapsulating option, or zero
215 * @ret rc Return status code
217 * Copies options with the specified encapsulator from DHCP options
218 * blocks into a DHCP packet. Most options are copied verbatim.
219 * Recognised encapsulated options fields are handled as such.
221 * @c options may specify a single options block, or be left as NULL
222 * in order to copy options from all registered options blocks.
224 static int copy_dhcp_packet_encap_options ( struct dhcp_packet *dhcppkt,
225 struct dhcp_option_block *options,
226 unsigned int encapsulator ) {
231 for ( subtag = DHCP_MIN_OPTION; subtag <= DHCP_MAX_OPTION; subtag++ ) {
232 tag = DHCP_ENCAP_OPT ( encapsulator, subtag );
235 case DHCP_VENDOR_ENCAP:
236 /* Process encapsulated options field */
237 if ( ( rc = copy_dhcp_packet_encap_options ( dhcppkt,
243 /* Copy option to reassembled packet */
244 if ( ( rc = copy_dhcp_packet_option ( dhcppkt, options,
255 * Copy options into DHCP packet
257 * @v dhcppkt DHCP packet
258 * @v options DHCP option block, or NULL
259 * @ret rc Return status code
261 * Copies options from DHCP options blocks into a DHCP packet. Most
262 * options are copied verbatim. Recognised encapsulated options
263 * fields are handled as such.
265 * @c options may specify a single options block, or be left as NULL
266 * in order to copy options from all registered options blocks.
268 int copy_dhcp_packet_options ( struct dhcp_packet *dhcppkt,
269 struct dhcp_option_block *options ) {
270 return copy_dhcp_packet_encap_options ( dhcppkt, options, 0 );
274 * Create a DHCP packet
276 * @v netdev Network device
277 * @v msgtype DHCP message type
278 * @v data Buffer for DHCP packet
279 * @v max_len Size of DHCP packet buffer
280 * @v dhcppkt DHCP packet structure to fill in
281 * @ret rc Return status code
283 * Creates a DHCP packet in the specified buffer, and fills out a @c
284 * dhcp_packet structure that can be passed to
285 * set_dhcp_packet_option() or copy_dhcp_packet_options().
287 int create_dhcp_packet ( struct net_device *netdev, uint8_t msgtype,
288 void *data, size_t max_len,
289 struct dhcp_packet *dhcppkt ) {
290 struct dhcphdr *dhcphdr = data;
291 static const uint8_t overloading = ( DHCP_OPTION_OVERLOAD_FILE |
292 DHCP_OPTION_OVERLOAD_SNAME );
296 if ( max_len < sizeof ( *dhcphdr ) )
299 /* Initialise DHCP packet content */
300 memset ( dhcphdr, 0, max_len );
301 dhcphdr->xid = dhcp_xid ( netdev );
302 dhcphdr->magic = htonl ( DHCP_MAGIC_COOKIE );
303 dhcphdr->htype = ntohs ( netdev->ll_protocol->ll_proto );
304 dhcphdr->hlen = netdev->ll_protocol->ll_addr_len;
305 memcpy ( dhcphdr->chaddr, netdev->ll_addr, dhcphdr->hlen );
306 dhcphdr->op = dhcp_op[msgtype];
308 /* Initialise DHCP packet structure */
309 dhcppkt->dhcphdr = dhcphdr;
310 dhcppkt->max_len = max_len;
311 init_dhcp_options ( &dhcppkt->options[OPTS_MAIN], dhcphdr->options,
313 offsetof ( typeof ( *dhcphdr ), options ) ) );
314 init_dhcp_options ( &dhcppkt->options[OPTS_FILE], dhcphdr->file,
315 sizeof ( dhcphdr->file ) );
316 init_dhcp_options ( &dhcppkt->options[OPTS_SNAME], dhcphdr->sname,
317 sizeof ( dhcphdr->sname ) );
319 /* Set DHCP_OPTION_OVERLOAD option within the main options block */
320 if ( set_dhcp_option ( &dhcppkt->options[OPTS_MAIN],
321 DHCP_OPTION_OVERLOAD, &overloading,
322 sizeof ( overloading ) ) == NULL )
325 /* Set DHCP_MESSAGE_TYPE option */
326 if ( ( rc = set_dhcp_packet_option ( dhcppkt, DHCP_MESSAGE_TYPE,
328 sizeof ( msgtype ) ) ) != 0 )
335 * Calculate used length of a field containing DHCP options
337 * @v data Field containing DHCP options
338 * @v max_len Field length
339 * @ret len Used length (excluding the @c DHCP_END tag)
341 static size_t dhcp_field_len ( const void *data, size_t max_len ) {
342 struct dhcp_option_block options;
343 struct dhcp_option *end;
345 options.data = ( ( void * ) data );
346 options.len = max_len;
347 end = find_dhcp_option ( &options, DHCP_END );
348 return ( end ? ( ( ( void * ) end ) - data ) : 0 );
352 * Merge field containing DHCP options or string into DHCP options block
354 * @v options DHCP option block
355 * @v data Field containing DHCP options
356 * @v max_len Field length
357 * @v tag DHCP option tag, or 0
359 * If @c tag is non-zero, the field will be treated as a
360 * NUL-terminated string representing the value of the specified DHCP
361 * option. If @c tag is zero, the field will be treated as a block of
362 * DHCP options, and simply appended to the existing options in the
365 * The caller must ensure that there is enough space in the options
366 * block to perform the merge.
368 static void merge_dhcp_field ( struct dhcp_option_block *options,
369 const void *data, size_t max_len,
373 struct dhcp_option *end;
376 set_dhcp_option ( options, tag, data, strlen ( data ) );
378 len = dhcp_field_len ( data, max_len );
379 dest = ( options->data + options->len - 1 );
380 memcpy ( dest, data, len );
382 end = ( dest + len );
388 * Parse DHCP packet and construct DHCP options block
390 * @v dhcphdr DHCP packet
391 * @v len Length of DHCP packet
392 * @ret options DHCP options block, or NULL
394 * Parses a received DHCP packet and canonicalises its contents into a
395 * single DHCP options block. The "file" and "sname" fields are
396 * converted into the corresponding DHCP options (@c
397 * DHCP_BOOTFILE_NAME and @c DHCP_TFTP_SERVER_NAME respectively). If
398 * these fields are used for option overloading, their options are
399 * merged in to the options block.
401 * The values of the "yiaddr" and "siaddr" fields will be stored
402 * within the options block as the magic options @c DHCP_EB_YIADDR and
405 * Note that this call allocates new memory for the constructed DHCP
406 * options block; it is the responsibility of the caller to eventually
409 static struct dhcp_option_block * dhcp_parse ( struct dhcphdr *dhcphdr,
411 struct dhcp_option_block *options;
413 unsigned int overloading;
416 if ( len < sizeof ( *dhcphdr ) )
419 /* Calculate size of resulting concatenated option block:
421 * The "options" field : length of the field minus the DHCP_END tag.
423 * The "file" field : maximum length of the field minus the
424 * NUL terminator, plus a 2-byte DHCP header or, if used for
425 * option overloading, the length of the field minus the
428 * The "sname" field : as for the "file" field.
430 * 15 bytes for an encapsulated options field to contain the
431 * value of the "yiaddr" and "siaddr" fields
433 * 1 byte for a final terminating DHCP_END tag.
435 options_len = ( ( len - offsetof ( typeof ( *dhcphdr ), options ) ) - 1
436 + ( sizeof ( dhcphdr->file ) + 1 )
437 + ( sizeof ( dhcphdr->sname ) + 1 )
438 + 15 /* yiaddr and siaddr */
439 + 1 /* DHCP_END tag */ );
441 /* Allocate empty options block of required size */
442 options = alloc_dhcp_options ( options_len );
444 DBG ( "DHCP could not allocate %d-byte option block\n",
449 /* Merge in "options" field, if this is a DHCP packet */
450 if ( dhcphdr->magic == htonl ( DHCP_MAGIC_COOKIE ) ) {
451 merge_dhcp_field ( options, dhcphdr->options,
453 offsetof ( typeof (*dhcphdr), options ) ),
454 0 /* Always contains options */ );
457 /* Identify overloaded fields */
458 overloading = find_dhcp_num_option ( options, DHCP_OPTION_OVERLOAD );
460 /* Merge in "file" and "sname" fields */
461 merge_dhcp_field ( options, dhcphdr->file, sizeof ( dhcphdr->file ),
462 ( ( overloading & DHCP_OPTION_OVERLOAD_FILE ) ?
463 0 : DHCP_BOOTFILE_NAME ) );
464 merge_dhcp_field ( options, dhcphdr->sname, sizeof ( dhcphdr->sname ),
465 ( ( overloading & DHCP_OPTION_OVERLOAD_SNAME ) ?
466 0 : DHCP_TFTP_SERVER_NAME ) );
468 /* Set magic options for "yiaddr" and "siaddr", if present */
469 if ( dhcphdr->yiaddr.s_addr ) {
470 set_dhcp_option ( options, DHCP_EB_YIADDR,
471 &dhcphdr->yiaddr, sizeof (dhcphdr->yiaddr) );
473 if ( dhcphdr->siaddr.s_addr ) {
474 set_dhcp_option ( options, DHCP_EB_SIADDR,
475 &dhcphdr->siaddr, sizeof (dhcphdr->siaddr) );
478 assert ( options->len <= options->max_len );
483 /****************************************************************************
485 * DHCP to UDP interface
489 static inline struct dhcp_session *
490 udp_to_dhcp ( struct udp_connection *conn ) {
491 return container_of ( conn, struct dhcp_session, udp );
495 * Mark DHCP session as complete
497 * @v dhcp DHCP session
498 * @v rc Return status code
500 static void dhcp_done ( struct dhcp_session *dhcp, int rc ) {
501 /* Free up options if we failed */
503 if ( dhcp->options ) {
504 free_dhcp_options ( dhcp->options );
505 dhcp->options = NULL;
509 /* Close UDP connection */
510 udp_close ( &dhcp->udp );
512 /* Mark async operation as complete */
513 async_done ( &dhcp->aop, rc );
516 /** Address for transmitting DHCP requests */
518 struct sockaddr_tcpip st;
519 struct sockaddr_in sin;
522 .sin_family = AF_INET,
523 .sin_addr.s_addr = INADDR_BROADCAST,
524 .sin_port = htons ( BOOTPS_PORT ),
529 * Transmit DHCP request
531 * @v conn UDP connection
532 * @v buf Temporary data buffer
533 * @v len Length of temporary data buffer
534 * @ret rc Return status code
536 static int dhcp_senddata ( struct udp_connection *conn,
537 void *buf, size_t len ) {
538 struct dhcp_session *dhcp = udp_to_dhcp ( conn );
539 struct dhcp_packet dhcppkt;
542 DBG ( "Transmitting %s\n", dhcp_msgtype_name ( dhcp->state ) );
544 assert ( ( dhcp->state == DHCPDISCOVER ) ||
545 ( dhcp->state == DHCPREQUEST ) );
547 /* Create DHCP packet in temporary buffer */
548 if ( ( rc = create_dhcp_packet ( dhcp->netdev, dhcp->state, buf, len,
549 &dhcppkt ) ) != 0 ) {
550 DBG ( "Could not create DHCP packet\n" );
554 /* Copy in options common to all requests */
555 if ( ( rc = copy_dhcp_packet_options ( &dhcppkt,
556 &dhcp_request_options ) ) != 0){
557 DBG ( "Could not set common DHCP options\n" );
561 /* Copy any required options from previous server repsonse */
562 if ( dhcp->options ) {
563 if ( ( rc = copy_dhcp_packet_option ( &dhcppkt, dhcp->options,
564 DHCP_SERVER_IDENTIFIER,
565 DHCP_SERVER_IDENTIFIER ) ) != 0 ) {
566 DBG ( "Could not set server identifier option\n" );
569 if ( ( rc = copy_dhcp_packet_option ( &dhcppkt, dhcp->options,
571 DHCP_REQUESTED_ADDRESS ) ) != 0 ) {
572 DBG ( "Could not set requested address option\n" );
577 /* Transmit the packet */
578 if ( ( rc = udp_sendto ( conn, &sa_dhcp_server.st,
579 dhcppkt.dhcphdr, dhcppkt.len ) ) != 0 ) {
580 DBG ( "Could not transmit UDP packet\n" );
588 * Transmit DHCP request
590 * @v dhcp DHCP session
592 static void dhcp_send_request ( struct dhcp_session *dhcp ) {
593 start_timer ( &dhcp->timer );
594 udp_senddata ( &dhcp->udp );
598 * Handle DHCP retry timer expiry
600 * @v timer DHCP retry timer
601 * @v fail Failure indicator
603 static void dhcp_timer_expired ( struct retry_timer *timer, int fail ) {
604 struct dhcp_session *dhcp =
605 container_of ( timer, struct dhcp_session, timer );
608 dhcp_done ( dhcp, -ETIMEDOUT );
610 dhcp_send_request ( dhcp );
617 * @v udp UDP connection
618 * @v data Received data
619 * @v len Length of received data
620 * @v st_src Partially-filled source address
621 * @v st_dest Partially-filled destination address
623 static int dhcp_newdata ( struct udp_connection *conn, void *data, size_t len,
624 struct sockaddr_tcpip *st_src __unused,
625 struct sockaddr_tcpip *st_dest __unused ) {
626 struct dhcp_session *dhcp = udp_to_dhcp ( conn );
627 struct dhcphdr *dhcphdr = data;
628 struct dhcp_option_block *options;
629 unsigned int msgtype;
631 /* Check for matching transaction ID */
632 if ( dhcphdr->xid != dhcp_xid ( dhcp->netdev ) ) {
633 DBG ( "DHCP wrong transaction ID (wanted %08lx, got %08lx)\n",
634 ntohl ( dhcphdr->xid ),
635 ntohl ( dhcp_xid ( dhcp->netdev ) ) );
639 /* Parse packet and create options structure */
640 options = dhcp_parse ( dhcphdr, len );
642 DBG ( "Could not parse DHCP packet\n" );
646 /* Determine message type */
647 msgtype = find_dhcp_num_option ( options, DHCP_MESSAGE_TYPE );
648 DBG ( "Received %s\n", dhcp_msgtype_name ( msgtype ) );
650 /* Handle DHCP reply */
651 switch ( dhcp->state ) {
653 if ( msgtype != DHCPOFFER )
655 dhcp->state = DHCPREQUEST;
658 if ( msgtype != DHCPACK )
660 dhcp->state = DHCPACK;
667 /* Stop timer and update stored options */
668 stop_timer ( &dhcp->timer );
670 free_dhcp_options ( dhcp->options );
671 dhcp->options = options;
673 /* Transmit next packet, or terminate session */
674 if ( dhcp->state < DHCPACK ) {
675 dhcp_send_request ( dhcp );
677 dhcp_done ( dhcp, 0 );
682 free_dhcp_options ( options );
686 /** DHCP UDP operations */
687 static struct udp_operations dhcp_udp_operations = {
688 .senddata = dhcp_senddata,
689 .newdata = dhcp_newdata,
693 * Initiate DHCP on a network interface
695 * @v dhcp DHCP session
696 * @ret aop Asynchronous operation
698 * If the DHCP operation completes successfully, the
699 * dhcp_session::options field will be filled in with the resulting
700 * options block. The caller takes responsibility for eventually
701 * calling free_dhcp_options().
703 struct async_operation * start_dhcp ( struct dhcp_session *dhcp ) {
706 /* Initialise DHCP session */
707 dhcp->udp.udp_op = &dhcp_udp_operations;
708 dhcp->timer.expired = dhcp_timer_expired;
709 dhcp->state = DHCPDISCOVER;
711 /* Bind to local port */
712 if ( ( rc = udp_open ( &dhcp->udp, htons ( BOOTPC_PORT ) ) ) != 0 ) {
713 async_done ( &dhcp->aop, rc );
717 /* Proof of concept: just send a single DHCPDISCOVER */
718 dhcp_send_request ( dhcp );