6 * Dynamic Host Configuration Protocol
11 #include <gpxe/list.h>
13 #include <gpxe/refcnt.h>
14 #include <gpxe/tables.h>
19 /** BOOTP/DHCP server port */
20 #define BOOTPS_PORT 67
22 /** BOOTP/DHCP client port */
23 #define BOOTPC_PORT 68
25 /** Construct a tag value for an encapsulated option
27 * This tag value can be passed to Etherboot functions when searching
28 * for DHCP options in order to search for a tag within an
29 * encapsulated options block.
31 #define DHCP_ENCAP_OPT( encapsulator, encapsulated ) \
32 ( ( (encapsulator) << 8 ) | (encapsulated) )
33 /** Extract encapsulating option block tag from encapsulated tag value */
34 #define DHCP_ENCAPSULATOR( encap_opt ) ( (encap_opt) >> 8 )
35 /** Extract encapsulated option tag from encapsulated tag value */
36 #define DHCP_ENCAPSULATED( encap_opt ) ( (encap_opt) & 0xff )
37 /** Option is encapsulated */
38 #define DHCP_IS_ENCAP_OPT( opt ) DHCP_ENCAPSULATOR( opt )
41 * @defgroup dhcpopts DHCP option tags
47 * This tag does not have a length field; it is always only a single
52 /** Minimum normal DHCP option */
53 #define DHCP_MIN_OPTION 1
56 #define DHCP_SUBNET_MASK 1
59 #define DHCP_ROUTERS 3
62 #define DHCP_DNS_SERVERS 6
65 #define DHCP_LOG_SERVERS 7
68 #define DHCP_HOST_NAME 12
71 #define DHCP_DOMAIN_NAME 15
74 #define DHCP_ROOT_PATH 17
76 /** Vendor encapsulated options */
77 #define DHCP_VENDOR_ENCAP 43
79 /** Requested IP address */
80 #define DHCP_REQUESTED_ADDRESS 50
83 #define DHCP_LEASE_TIME 51
85 /** Option overloading
87 * The value of this option is the bitwise-OR of zero or more
88 * DHCP_OPTION_OVERLOAD_XXX constants.
90 #define DHCP_OPTION_OVERLOAD 52
92 /** The "file" field is overloaded to contain extra DHCP options */
93 #define DHCP_OPTION_OVERLOAD_FILE 1
95 /** The "sname" field is overloaded to contain extra DHCP options */
96 #define DHCP_OPTION_OVERLOAD_SNAME 2
98 /** DHCP message type */
99 #define DHCP_MESSAGE_TYPE 53
100 #define DHCPDISCOVER 1
102 #define DHCPREQUEST 3
103 #define DHCPDECLINE 4
106 #define DHCPRELEASE 7
109 /** DHCP server identifier */
110 #define DHCP_SERVER_IDENTIFIER 54
112 /** Parameter request list */
113 #define DHCP_PARAMETER_REQUEST_LIST 55
115 /** Maximum DHCP message size */
116 #define DHCP_MAX_MESSAGE_SIZE 57
118 /** Vendor class identifier */
119 #define DHCP_VENDOR_CLASS_ID 60
121 /** Client identifier */
122 #define DHCP_CLIENT_ID 61
126 * This option replaces the fixed "sname" field, when that field is
127 * used to contain overloaded options.
129 #define DHCP_TFTP_SERVER_NAME 66
133 * This option replaces the fixed "file" field, when that field is
134 * used to contain overloaded options.
136 #define DHCP_BOOTFILE_NAME 67
138 /** Etherboot-specific encapsulated options
140 * This encapsulated options field is used to contain all options
141 * specific to Etherboot (i.e. not assigned by IANA or other standards
144 #define DHCP_EB_ENCAP 175
146 /** Priority of this options block
148 * This is a signed 8-bit integer field indicating the priority of
149 * this block of options. It can be used to specify the relative
150 * priority of multiple option blocks (e.g. options from non-volatile
151 * storage versus options from a DHCP server).
153 #define DHCP_EB_PRIORITY DHCP_ENCAP_OPT ( DHCP_EB_ENCAP, 1 )
155 /** "Your" IP address
157 * This option is used internally to contain the value of the "yiaddr"
158 * field, in order to provide a consistent approach to storing and
159 * processing options. It should never be present in a DHCP packet.
161 #define DHCP_EB_YIADDR DHCP_ENCAP_OPT ( DHCP_EB_ENCAP, 2 )
163 /** "Server" IP address
165 * This option is used internally to contain the value of the "siaddr"
166 * field, in order to provide a consistent approach to storing and
167 * processing options. It should never be present in a DHCP packet.
169 #define DHCP_EB_SIADDR DHCP_ENCAP_OPT ( DHCP_EB_ENCAP, 3 )
172 * Tags in the range 0x10-0x7f are reserved for feature markers
176 /** Network device descriptor
178 * Byte 0 is the bus type ID; remaining bytes depend on the bus type.
182 * Byte 1 : PCI vendor ID MSB
183 * Byte 2 : PCI vendor ID LSB
184 * Byte 3 : PCI device ID MSB
185 * Byte 4 : PCI device ID LSB
187 #define DHCP_EB_BUS_ID DHCP_ENCAP_OPT ( DHCP_EB_ENCAP, 0xb1 )
189 /** BIOS drive number
191 * This is the drive number for a drive emulated via INT 13. 0x80 is
192 * the first hard disk, 0x81 is the second hard disk, etc.
194 #define DHCP_EB_BIOS_DRIVE DHCP_ENCAP_OPT ( DHCP_EB_ENCAP, 0xbd )
198 * This will be used as the username for any required authentication.
199 * It is expected that this option's value will be held in
200 * non-volatile storage, rather than transmitted as part of a DHCP
203 #define DHCP_EB_USERNAME DHCP_ENCAP_OPT ( DHCP_EB_ENCAP, 0xbe )
207 * This will be used as the password for any required authentication.
208 * It is expected that this option's value will be held in
209 * non-volatile storage, rather than transmitted as part of a DHCP
212 #define DHCP_EB_PASSWORD DHCP_ENCAP_OPT ( DHCP_EB_ENCAP, 0xbf )
214 /** iSCSI primary target IQN */
215 #define DHCP_ISCSI_PRIMARY_TARGET_IQN 201
217 /** iSCSI secondary target IQN */
218 #define DHCP_ISCSI_SECONDARY_TARGET_IQN 202
220 /** iSCSI initiator IQN */
221 #define DHCP_ISCSI_INITIATOR_IQN 203
223 /** Maximum normal DHCP option */
224 #define DHCP_MAX_OPTION 254
228 * This tag does not have a length field; it is always only a single
236 * Count number of arguments to a variadic macro
238 * This rather neat, non-iterative solution is courtesy of Laurent
242 #define _VA_ARG_COUNT( _1, _2, _3, _4, _5, _6, _7, _8, \
243 _9, _10, _11, _12, _13, _14, _15, _16, \
244 _17, _18, _19, _20, _21, _22, _23, _24, \
245 _25, _26, _27, _28, _29, _30, _31, _32, \
246 _33, _34, _35, _36, _37, _38, _39, _40, \
247 _41, _42, _43, _44, _45, _46, _47, _48, \
248 _49, _50, _51, _52, _53, _54, _55, _56, \
249 _57, _58, _59, _60, _61, _62, _63, N, ... ) N
250 #define VA_ARG_COUNT( ... ) \
251 _VA_ARG_COUNT ( __VA_ARGS__, \
252 63, 62, 61, 60, 59, 58, 57, 56, \
253 55, 54, 53, 52, 51, 50, 49, 48, \
254 47, 46, 45, 44, 43, 42, 41, 40, \
255 39, 38, 37, 36, 35, 34, 33, 32, \
256 31, 30, 29, 28, 27, 26, 25, 24, \
257 23, 22, 21, 20, 19, 18, 17, 16, \
258 15, 14, 13, 12, 11, 10, 9, 8, \
259 7, 6, 5, 4, 3, 2, 1, 0 )
261 /** Construct a DHCP option from a list of bytes */
262 #define DHCP_OPTION( ... ) VA_ARG_COUNT ( __VA_ARGS__ ), __VA_ARGS__
264 /** Construct a DHCP option from a list of characters */
265 #define DHCP_STRING( ... ) DHCP_OPTION ( __VA_ARGS__ )
267 /** Construct a byte-valued DHCP option */
268 #define DHCP_BYTE( value ) DHCP_OPTION ( value )
270 /** Construct a word-valued DHCP option */
271 #define DHCP_WORD( value ) DHCP_OPTION ( ( ( (value) >> 8 ) & 0xff ), \
272 ( ( (value) >> 0 ) & 0xff ) )
274 /** Construct a dword-valued DHCP option */
275 #define DHCP_DWORD( value ) DHCP_OPTION ( ( ( (value) >> 24 ) & 0xff ), \
276 ( ( (value) >> 16 ) & 0xff ), \
277 ( ( (value) >> 8 ) & 0xff ), \
278 ( ( (value) >> 0 ) & 0xff ) )
280 /** Construct a DHCP encapsulated options field */
281 #define DHCP_ENCAP( ... ) DHCP_OPTION ( __VA_ARGS__, DHCP_END )
286 * DHCP options consist of a mandatory tag, a length field that is
287 * mandatory for all options except @c DHCP_PAD and @c DHCP_END, and a
293 * Must be a @c DHCP_XXX value.
298 * This is the length of the data field (i.e. excluding the
299 * tag and length fields). For the two tags @c DHCP_PAD and
300 * @c DHCP_END, the length field is implicitly zero and is
301 * also missing, i.e. these DHCP options are only a single
307 * Interpretation of the content is entirely dependent upon
308 * the tag. For fields containing a multi-byte integer, the
309 * field is defined to be in network-endian order (unless you
310 * are Intel and feel like violating the spec for fun).
320 } __attribute__ (( packed ));
323 * Length of a DHCP option header
325 * The header is the portion excluding the data, i.e. the tag and the
328 #define DHCP_OPTION_HEADER_LEN ( offsetof ( struct dhcp_option, data ) )
330 /** Maximum length for a single DHCP option */
331 #define DHCP_MAX_LEN 0xff
333 /** A DHCP options block */
334 struct dhcp_option_block {
335 /** Reference counter */
336 struct refcnt refcnt;
337 /** List of option blocks */
338 struct list_head list;
339 /** Option block raw data */
341 /** Option block length */
343 /** Option block maximum length */
347 * This is determined at the time of the call to
348 * register_options() by searching for the @c DHCP_EB_PRIORITY
361 * This must be either @c BOOTP_REQUEST or @c BOOTP_REPLY.
364 /** Hardware address type
366 * This is an ARPHRD_XXX constant. Note that ARPHRD_XXX
367 * constants are nominally 16 bits wide; this could be
368 * considered to be a bug in the BOOTP/DHCP specification.
371 /** Hardware address length */
373 /** Number of hops from server */
375 /** Transaction ID */
377 /** Seconds since start of acquisition */
381 /** "Client" IP address
383 * This is filled in if the client already has an IP address
384 * assigned and can respond to ARP requests.
386 struct in_addr ciaddr;
387 /** "Your" IP address
389 * This is the IP address assigned by the server to the client.
391 struct in_addr yiaddr;
392 /** "Server" IP address
394 * This is the IP address of the next server to be used in the
397 struct in_addr siaddr;
398 /** "Gateway" IP address
400 * This is the IP address of the DHCP relay agent, if any.
402 struct in_addr giaddr;
403 /** Client hardware address */
405 /** Server host name (null terminated)
407 * This field may be overridden and contain DHCP options
410 /** Boot file name (null terminated)
412 * This field may be overridden and contain DHCP options
415 /** DHCP magic cookie
417 * Must have the value @c DHCP_MAGIC_COOKIE.
422 * Variable length; extends to the end of the packet. Minimum
423 * length (for the sake of sanity) is 1, to allow for a single
429 /** Opcode for a request from client to server */
430 #define BOOTP_REQUEST 1
432 /** Opcode for a reply from server to client */
433 #define BOOTP_REPLY 2
435 /** BOOTP reply must be broadcast
437 * Clients that cannot accept unicast BOOTP replies must set this
440 #define BOOTP_FL_BROADCAST 0x8000
442 /** DHCP magic cookie */
443 #define DHCP_MAGIC_COOKIE 0x63825363UL
445 /** DHCP minimum packet length
447 * This is the mandated minimum packet length that a DHCP participant
448 * must be prepared to receive.
450 #define DHCP_MIN_LEN 552
457 /** The DHCP packet contents */
458 struct dhcphdr *dhcphdr;
459 /** Maximum length of the DHCP packet buffer */
461 /** Used length of the DHCP packet buffer */
464 struct dhcp_option_block options;
467 /** A DHCP option applicator */
468 struct dhcp_option_applicator {
469 /** DHCP option tag */
473 * @v tag DHCP option tag
474 * @v option DHCP option
475 * @ret rc Return status code
477 int ( * apply ) ( unsigned int tag, struct dhcp_option *option );
480 /** Declare a DHCP option applicator */
481 #define __dhcp_applicator \
482 __table ( struct dhcp_option_applicator, dhcp_applicators, 01 )
485 * Get reference to DHCP options block
487 * @v options DHCP options block
488 * @ret options DHCP options block
490 static inline __attribute__ (( always_inline )) struct dhcp_option_block *
491 dhcpopt_get ( struct dhcp_option_block *options ) {
492 ref_get ( &options->refcnt );
497 * Drop reference to DHCP options block
499 * @v options DHCP options block
501 static inline __attribute__ (( always_inline )) void
502 dhcpopt_put ( struct dhcp_option_block *options ) {
503 ref_put ( &options->refcnt );
506 extern unsigned long dhcp_num_option ( struct dhcp_option *option );
507 extern void dhcp_ipv4_option ( struct dhcp_option *option,
508 struct in_addr *inp );
509 extern int dhcp_snprintf ( char *buf, size_t size,
510 struct dhcp_option *option );
511 extern struct dhcp_option *
512 find_dhcp_option ( struct dhcp_option_block *options, unsigned int tag );
513 extern void register_dhcp_options ( struct dhcp_option_block *options );
514 extern void unregister_dhcp_options ( struct dhcp_option_block *options );
515 extern void init_dhcp_options ( struct dhcp_option_block *options,
516 void *data, size_t max_len );
517 extern struct dhcp_option_block * __malloc alloc_dhcp_options ( size_t max_len );
518 extern struct dhcp_option *
519 set_dhcp_option ( struct dhcp_option_block *options, unsigned int tag,
520 const void *data, size_t len );
521 extern struct dhcp_option * find_global_dhcp_option ( unsigned int tag );
522 extern unsigned long find_dhcp_num_option ( struct dhcp_option_block *options,
524 extern unsigned long find_global_dhcp_num_option ( unsigned int tag );
525 extern void find_dhcp_ipv4_option ( struct dhcp_option_block *options,
526 unsigned int tag, struct in_addr *inp );
527 extern void find_global_dhcp_ipv4_option ( unsigned int tag,
528 struct in_addr *inp );
529 extern void delete_dhcp_option ( struct dhcp_option_block *options,
532 extern int apply_dhcp_options ( struct dhcp_option_block *options );
533 extern int apply_global_dhcp_options ( void );
535 extern int create_dhcp_request ( struct net_device *netdev, int msgtype,
536 struct dhcp_option_block *options,
537 void *data, size_t max_len,
538 struct dhcp_packet *dhcppkt );
539 extern int create_dhcp_response ( struct net_device *netdev, int msgtype,
540 struct dhcp_option_block *options,
541 void *data, size_t max_len,
542 struct dhcp_packet *dhcppkt );
544 extern int start_dhcp ( struct job_interface *job, struct net_device *netdev,
545 int (*register_options) ( struct net_device *,
546 struct dhcp_option_block * ));
547 extern int dhcp_configure_netdev ( struct net_device *netdev,
548 struct dhcp_option_block *options );
550 #endif /* _GPXE_DHCP_H */