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_HOST_NAME,
58 DHCP_BOOTFILE_NAME, DHCP_ROOT_PATH, DHCP_DNS_SERVERS,
64 * Name a DHCP packet type
66 * @v msgtype DHCP message type
67 * @ret string DHCP mesasge type name
69 static inline const char * dhcp_msgtype_name ( unsigned int msgtype ) {
71 case 0: return "BOOTP"; /* Non-DHCP packet */
72 case DHCPDISCOVER: return "DHCPDISCOVER";
73 case DHCPOFFER: return "DHCPOFFER";
74 case DHCPREQUEST: return "DHCPREQUEST";
75 case DHCPDECLINE: return "DHCPDECLINE";
76 case DHCPACK: return "DHCPACK";
77 case DHCPNAK: return "DHCPNAK";
78 case DHCPRELEASE: return "DHCPRELEASE";
79 case DHCPINFORM: return "DHCPINFORM";
80 default: return "DHCP<invalid>";
85 * Calculate DHCP transaction ID for a network device
87 * @v netdev Network device
90 * Extract the least significant bits of the hardware address for use
91 * as the transaction ID.
93 static uint32_t dhcp_xid ( struct net_device *netdev ) {
96 memcpy ( &xid, ( netdev->ll_addr + netdev->ll_protocol->ll_addr_len
97 - sizeof ( xid ) ), sizeof ( xid ) );
101 /** Options common to all DHCP requests */
102 struct dhcp_option_block dhcp_request_options = {
103 .data = dhcp_request_options_data,
104 .max_len = sizeof ( dhcp_request_options_data ),
105 .len = sizeof ( dhcp_request_options_data ),
109 * Set option within DHCP packet
111 * @v dhcppkt DHCP packet
112 * @v tag DHCP option tag
113 * @v data New value for DHCP option
114 * @v len Length of value, in bytes
115 * @ret rc Return status code
117 * Sets the option within the first available options block within the
118 * DHCP packet. Option blocks are tried in the order specified by @c
119 * dhcp_option_block_fill_order.
121 * The magic options @c DHCP_EB_YIADDR and @c DHCP_EB_SIADDR are
122 * intercepted and inserted into the appropriate fixed fields within
123 * the DHCP packet. The option @c DHCP_OPTION_OVERLOAD is silently
124 * ignored, since our DHCP packet assembly method relies on always
125 * having option overloading in use.
127 static int set_dhcp_packet_option ( struct dhcp_packet *dhcppkt,
128 unsigned int tag, const void *data,
130 struct dhcphdr *dhcphdr = dhcppkt->dhcphdr;
131 struct dhcp_option_block *options = dhcppkt->options;
132 struct dhcp_option *option = NULL;
134 /* Special-case the magic options */
136 case DHCP_OPTION_OVERLOAD:
137 /* Hard-coded in packets we create; always ignore */
140 memcpy ( &dhcphdr->yiaddr, data, sizeof ( dhcphdr->yiaddr ) );
143 memcpy ( &dhcphdr->siaddr, data, sizeof ( dhcphdr->siaddr ) );
145 case DHCP_MESSAGE_TYPE:
146 case DHCP_REQUESTED_ADDRESS:
147 case DHCP_PARAMETER_REQUEST_LIST:
148 /* These options have to be within the main options
149 * block. This doesn't seem to be required by the
150 * RFCs, but at least ISC dhcpd refuses to recognise
153 options = &dhcppkt->options[OPTS_MAIN];
156 /* Continue processing as normal */
160 /* Set option in first available options block */
161 for ( ; options < &dhcppkt->options[NUM_OPT_BLOCKS] ; options++ ) {
162 option = set_dhcp_option ( options, tag, data, len );
167 /* Update DHCP packet length */
168 dhcppkt->len = ( offsetof ( typeof ( *dhcppkt->dhcphdr ), options )
169 + dhcppkt->options[OPTS_MAIN].len );
171 return ( option ? 0 : -ENOSPC );
175 * Copy option into DHCP packet
177 * @v dhcppkt DHCP packet
178 * @v options DHCP option block, or NULL
179 * @v tag DHCP option tag to search for
180 * @v new_tag DHCP option tag to use for copied option
181 * @ret rc Return status code
183 * Copies a single option, if present, from the DHCP options block
184 * into a DHCP packet. The tag for the option may be changed if
185 * desired; this is required by other parts of the DHCP code.
187 * @c options may specify a single options block, or be left as NULL
188 * in order to search for the option within all registered options
191 static int copy_dhcp_packet_option ( struct dhcp_packet *dhcppkt,
192 struct dhcp_option_block *options,
193 unsigned int tag, unsigned int new_tag ) {
194 struct dhcp_option *option;
197 option = find_dhcp_option ( options, tag );
199 if ( ( rc = set_dhcp_packet_option ( dhcppkt, new_tag,
201 option->len ) ) != 0 )
208 * Copy options into DHCP packet
210 * @v dhcppkt DHCP packet
211 * @v options DHCP option block, or NULL
212 * @v encapsulator Encapsulating option, or zero
213 * @ret rc Return status code
215 * Copies options with the specified encapsulator from DHCP options
216 * blocks into a DHCP packet. Most options are copied verbatim.
217 * Recognised encapsulated options fields are handled as such.
219 * @c options may specify a single options block, or be left as NULL
220 * in order to copy options from all registered options blocks.
222 static int copy_dhcp_packet_encap_options ( struct dhcp_packet *dhcppkt,
223 struct dhcp_option_block *options,
224 unsigned int encapsulator ) {
229 for ( subtag = DHCP_MIN_OPTION; subtag <= DHCP_MAX_OPTION; subtag++ ) {
230 tag = DHCP_ENCAP_OPT ( encapsulator, subtag );
233 case DHCP_VENDOR_ENCAP:
234 /* Process encapsulated options field */
235 if ( ( rc = copy_dhcp_packet_encap_options ( dhcppkt,
241 /* Copy option to reassembled packet */
242 if ( ( rc = copy_dhcp_packet_option ( dhcppkt, options,
253 * Copy options into DHCP packet
255 * @v dhcppkt DHCP packet
256 * @v options DHCP option block, or NULL
257 * @ret rc Return status code
259 * Copies options from DHCP options blocks into a DHCP packet. Most
260 * options are copied verbatim. Recognised encapsulated options
261 * fields are handled as such.
263 * @c options may specify a single options block, or be left as NULL
264 * in order to copy options from all registered options blocks.
266 int copy_dhcp_packet_options ( struct dhcp_packet *dhcppkt,
267 struct dhcp_option_block *options ) {
268 return copy_dhcp_packet_encap_options ( dhcppkt, options, 0 );
272 * Create a DHCP packet
274 * @v netdev Network device
275 * @v msgtype DHCP message type
276 * @v data Buffer for DHCP packet
277 * @v max_len Size of DHCP packet buffer
278 * @v dhcppkt DHCP packet structure to fill in
279 * @ret rc Return status code
281 * Creates a DHCP packet in the specified buffer, and fills out a @c
282 * dhcp_packet structure that can be passed to
283 * set_dhcp_packet_option() or copy_dhcp_packet_options().
285 int create_dhcp_packet ( struct net_device *netdev, uint8_t msgtype,
286 void *data, size_t max_len,
287 struct dhcp_packet *dhcppkt ) {
288 struct dhcphdr *dhcphdr = data;
289 static const uint8_t overloading = ( DHCP_OPTION_OVERLOAD_FILE |
290 DHCP_OPTION_OVERLOAD_SNAME );
294 if ( max_len < sizeof ( *dhcphdr ) )
297 /* Initialise DHCP packet content */
298 memset ( dhcphdr, 0, max_len );
299 dhcphdr->xid = dhcp_xid ( netdev );
300 dhcphdr->magic = htonl ( DHCP_MAGIC_COOKIE );
301 dhcphdr->htype = ntohs ( netdev->ll_protocol->ll_proto );
302 dhcphdr->hlen = netdev->ll_protocol->ll_addr_len;
303 memcpy ( dhcphdr->chaddr, netdev->ll_addr, dhcphdr->hlen );
304 dhcphdr->op = dhcp_op[msgtype];
306 /* Initialise DHCP packet structure */
307 dhcppkt->dhcphdr = dhcphdr;
308 dhcppkt->max_len = max_len;
309 init_dhcp_options ( &dhcppkt->options[OPTS_MAIN], dhcphdr->options,
311 offsetof ( typeof ( *dhcphdr ), options ) ) );
312 init_dhcp_options ( &dhcppkt->options[OPTS_FILE], dhcphdr->file,
313 sizeof ( dhcphdr->file ) );
314 init_dhcp_options ( &dhcppkt->options[OPTS_SNAME], dhcphdr->sname,
315 sizeof ( dhcphdr->sname ) );
317 /* Set DHCP_OPTION_OVERLOAD option within the main options block */
318 if ( set_dhcp_option ( &dhcppkt->options[OPTS_MAIN],
319 DHCP_OPTION_OVERLOAD, &overloading,
320 sizeof ( overloading ) ) == NULL )
323 /* Set DHCP_MESSAGE_TYPE option */
324 if ( ( rc = set_dhcp_packet_option ( dhcppkt, DHCP_MESSAGE_TYPE,
326 sizeof ( msgtype ) ) ) != 0 )
333 * Calculate used length of a field containing DHCP options
335 * @v data Field containing DHCP options
336 * @v max_len Field length
337 * @ret len Used length (excluding the @c DHCP_END tag)
339 static size_t dhcp_field_len ( const void *data, size_t max_len ) {
340 struct dhcp_option_block options;
341 struct dhcp_option *end;
343 options.data = ( ( void * ) data );
344 options.len = max_len;
345 end = find_dhcp_option ( &options, DHCP_END );
346 return ( end ? ( ( ( void * ) end ) - data ) : 0 );
350 * Merge field containing DHCP options or string into DHCP options block
352 * @v options DHCP option block
353 * @v data Field containing DHCP options
354 * @v max_len Field length
355 * @v tag DHCP option tag, or 0
357 * If @c tag is non-zero, the field will be treated as a
358 * NUL-terminated string representing the value of the specified DHCP
359 * option. If @c tag is zero, the field will be treated as a block of
360 * DHCP options, and simply appended to the existing options in the
363 * The caller must ensure that there is enough space in the options
364 * block to perform the merge.
366 static void merge_dhcp_field ( struct dhcp_option_block *options,
367 const void *data, size_t max_len,
371 struct dhcp_option *end;
374 set_dhcp_option ( options, tag, data, strlen ( data ) );
376 len = dhcp_field_len ( data, max_len );
377 dest = ( options->data + options->len - 1 );
378 memcpy ( dest, data, len );
380 end = ( dest + len );
386 * Parse DHCP packet and construct DHCP options block
388 * @v dhcphdr DHCP packet
389 * @v len Length of DHCP packet
390 * @ret options DHCP options block, or NULL
392 * Parses a received DHCP packet and canonicalises its contents into a
393 * single DHCP options block. The "file" and "sname" fields are
394 * converted into the corresponding DHCP options (@c
395 * DHCP_BOOTFILE_NAME and @c DHCP_TFTP_SERVER_NAME respectively). If
396 * these fields are used for option overloading, their options are
397 * merged in to the options block.
399 * The values of the "yiaddr" and "siaddr" fields will be stored
400 * within the options block as the magic options @c DHCP_EB_YIADDR and
403 * Note that this call allocates new memory for the constructed DHCP
404 * options block; it is the responsibility of the caller to eventually
407 static struct dhcp_option_block * dhcp_parse ( struct dhcphdr *dhcphdr,
409 struct dhcp_option_block *options;
411 unsigned int overloading;
414 if ( len < sizeof ( *dhcphdr ) )
417 /* Calculate size of resulting concatenated option block:
419 * The "options" field : length of the field minus the DHCP_END tag.
421 * The "file" field : maximum length of the field minus the
422 * NUL terminator, plus a 2-byte DHCP header or, if used for
423 * option overloading, the length of the field minus the
426 * The "sname" field : as for the "file" field.
428 * 15 bytes for an encapsulated options field to contain the
429 * value of the "yiaddr" and "siaddr" fields
431 * 1 byte for a final terminating DHCP_END tag.
433 options_len = ( ( len - offsetof ( typeof ( *dhcphdr ), options ) ) - 1
434 + ( sizeof ( dhcphdr->file ) + 1 )
435 + ( sizeof ( dhcphdr->sname ) + 1 )
436 + 15 /* yiaddr and siaddr */
437 + 1 /* DHCP_END tag */ );
439 /* Allocate empty options block of required size */
440 options = alloc_dhcp_options ( options_len );
442 DBG ( "DHCP could not allocate %d-byte option block\n",
447 /* Merge in "options" field, if this is a DHCP packet */
448 if ( dhcphdr->magic == htonl ( DHCP_MAGIC_COOKIE ) ) {
449 merge_dhcp_field ( options, dhcphdr->options,
451 offsetof ( typeof (*dhcphdr), options ) ),
452 0 /* Always contains options */ );
455 /* Identify overloaded fields */
456 overloading = find_dhcp_num_option ( options, DHCP_OPTION_OVERLOAD );
458 /* Merge in "file" and "sname" fields */
459 merge_dhcp_field ( options, dhcphdr->file, sizeof ( dhcphdr->file ),
460 ( ( overloading & DHCP_OPTION_OVERLOAD_FILE ) ?
461 0 : DHCP_BOOTFILE_NAME ) );
462 merge_dhcp_field ( options, dhcphdr->sname, sizeof ( dhcphdr->sname ),
463 ( ( overloading & DHCP_OPTION_OVERLOAD_SNAME ) ?
464 0 : DHCP_TFTP_SERVER_NAME ) );
466 /* Set magic options for "yiaddr" and "siaddr", if present */
467 if ( dhcphdr->yiaddr.s_addr ) {
468 set_dhcp_option ( options, DHCP_EB_YIADDR,
469 &dhcphdr->yiaddr, sizeof (dhcphdr->yiaddr) );
471 if ( dhcphdr->siaddr.s_addr ) {
472 set_dhcp_option ( options, DHCP_EB_SIADDR,
473 &dhcphdr->siaddr, sizeof (dhcphdr->siaddr) );
476 assert ( options->len <= options->max_len );
481 /****************************************************************************
483 * DHCP to UDP interface
487 static inline struct dhcp_session *
488 udp_to_dhcp ( struct udp_connection *conn ) {
489 return container_of ( conn, struct dhcp_session, udp );
493 * Mark DHCP session as complete
495 * @v dhcp DHCP session
496 * @v rc Return status code
498 static void dhcp_done ( struct dhcp_session *dhcp, int rc ) {
499 /* Free up options if we failed */
501 if ( dhcp->options ) {
502 free_dhcp_options ( dhcp->options );
503 dhcp->options = NULL;
507 /* Mark async operation as complete */
508 async_done ( &dhcp->aop, rc );
511 /** Address for transmitting DHCP requests */
513 struct sockaddr_tcpip st;
514 struct sockaddr_in sin;
517 .sin_family = AF_INET,
518 .sin_addr.s_addr = INADDR_BROADCAST,
519 .sin_port = htons ( BOOTPS_PORT ),
524 * Transmit DHCP request
526 * @v conn UDP connection
527 * @v buf Temporary data buffer
528 * @v len Length of temporary data buffer
529 * @ret rc Return status code
531 static int dhcp_senddata ( struct udp_connection *conn,
532 void *buf, size_t len ) {
533 struct dhcp_session *dhcp = udp_to_dhcp ( conn );
534 struct dhcp_packet dhcppkt;
537 DBG ( "Transmitting %s\n", dhcp_msgtype_name ( dhcp->state ) );
539 assert ( ( dhcp->state == DHCPDISCOVER ) ||
540 ( dhcp->state == DHCPREQUEST ) );
542 /* Create DHCP packet in temporary buffer */
543 if ( ( rc = create_dhcp_packet ( dhcp->netdev, dhcp->state, buf, len,
544 &dhcppkt ) ) != 0 ) {
545 DBG ( "Could not create DHCP packet\n" );
549 /* Copy in options common to all requests */
550 if ( ( rc = copy_dhcp_packet_options ( &dhcppkt,
551 &dhcp_request_options ) ) != 0){
552 DBG ( "Could not set common DHCP options\n" );
556 /* Copy any required options from previous server repsonse */
557 if ( dhcp->options ) {
558 if ( ( rc = copy_dhcp_packet_option ( &dhcppkt, dhcp->options,
559 DHCP_SERVER_IDENTIFIER,
560 DHCP_SERVER_IDENTIFIER ) ) != 0 ) {
561 DBG ( "Could not set server identifier option\n" );
564 if ( ( rc = copy_dhcp_packet_option ( &dhcppkt, dhcp->options,
566 DHCP_REQUESTED_ADDRESS ) ) != 0 ) {
567 DBG ( "Could not set requested address option\n" );
572 /* Transmit the packet */
573 if ( ( rc = udp_sendto ( conn, &sa_dhcp_server.st,
574 dhcppkt.dhcphdr, dhcppkt.len ) ) != 0 ) {
575 DBG ( "Could not transmit UDP packet\n" );
583 * Transmit DHCP request
585 * @v dhcp DHCP session
587 static void dhcp_send_request ( struct dhcp_session *dhcp ) {
588 start_timer ( &dhcp->timer );
589 udp_senddata ( &dhcp->udp );
593 * Handle DHCP retry timer expiry
595 * @v timer DHCP retry timer
596 * @v fail Failure indicator
598 static void dhcp_timer_expired ( struct retry_timer *timer, int fail ) {
599 struct dhcp_session *dhcp =
600 container_of ( timer, struct dhcp_session, timer );
603 dhcp_done ( dhcp, -ETIMEDOUT );
605 dhcp_send_request ( dhcp );
612 * @v udp UDP connection
613 * @v data Received data
614 * @v len Length of received data
615 * @v st_src Partially-filled source address
616 * @v st_dest Partially-filled destination address
618 static int dhcp_newdata ( struct udp_connection *conn, void *data, size_t len,
619 struct sockaddr_tcpip *st_src __unused,
620 struct sockaddr_tcpip *st_dest __unused ) {
621 struct dhcp_session *dhcp = udp_to_dhcp ( conn );
622 struct dhcphdr *dhcphdr = data;
623 struct dhcp_option_block *options;
624 unsigned int msgtype;
626 /* Check for matching transaction ID */
627 if ( dhcphdr->xid != dhcp_xid ( dhcp->netdev ) ) {
628 DBG ( "DHCP wrong transaction ID (wanted %08lx, got %08lx)\n",
629 ntohl ( dhcphdr->xid ),
630 ntohl ( dhcp_xid ( dhcp->netdev ) ) );
634 /* Parse packet and create options structure */
635 options = dhcp_parse ( dhcphdr, len );
637 DBG ( "Could not parse DHCP packet\n" );
641 /* Determine message type */
642 msgtype = find_dhcp_num_option ( options, DHCP_MESSAGE_TYPE );
643 DBG ( "Received %s\n", dhcp_msgtype_name ( msgtype ) );
645 /* Handle DHCP reply */
646 switch ( dhcp->state ) {
648 if ( msgtype != DHCPOFFER )
650 dhcp->state = DHCPREQUEST;
653 if ( msgtype != DHCPACK )
655 dhcp->state = DHCPACK;
662 /* Stop timer and update stored options */
663 stop_timer ( &dhcp->timer );
665 free_dhcp_options ( dhcp->options );
666 dhcp->options = options;
668 /* Transmit next packet, or terminate session */
669 if ( dhcp->state < DHCPACK ) {
670 dhcp_send_request ( dhcp );
672 dhcp_done ( dhcp, 0 );
677 free_dhcp_options ( options );
681 /** DHCP UDP operations */
682 static struct udp_operations dhcp_udp_operations = {
683 .senddata = dhcp_senddata,
684 .newdata = dhcp_newdata,
688 * Initiate DHCP on a network interface
690 * @v dhcp DHCP session
691 * @ret aop Asynchronous operation
693 * If the DHCP operation completes successfully, the
694 * dhcp_session::options field will be filled in with the resulting
695 * options block. The caller takes responsibility for eventually
696 * calling free_dhcp_options().
698 struct async_operation * start_dhcp ( struct dhcp_session *dhcp ) {
701 /* Initialise DHCP session */
702 dhcp->udp.udp_op = &dhcp_udp_operations;
703 dhcp->timer.expired = dhcp_timer_expired;
704 dhcp->state = DHCPDISCOVER;
706 /* Bind to local port */
707 if ( ( rc = udp_open ( &dhcp->udp, htons ( BOOTPC_PORT ) ) ) != 0 ) {
708 async_done ( &dhcp->aop, rc );
712 /* Proof of concept: just send a single DHCPDISCOVER */
713 dhcp_send_request ( dhcp );