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 /** Client system architecture */
139 #define DHCP_CLIENT_ARCHITECTURE 93
141 /** Client network device interface */
142 #define DHCP_CLIENT_NDI 94
144 /** UUID client identifier */
145 #define DHCP_CLIENT_UUID 97
147 /** Etherboot-specific encapsulated options
149 * This encapsulated options field is used to contain all options
150 * specific to Etherboot (i.e. not assigned by IANA or other standards
153 #define DHCP_EB_ENCAP 175
155 /** Priority of this options block
157 * This is a signed 8-bit integer field indicating the priority of
158 * this block of options. It can be used to specify the relative
159 * priority of multiple option blocks (e.g. options from non-volatile
160 * storage versus options from a DHCP server).
162 #define DHCP_EB_PRIORITY DHCP_ENCAP_OPT ( DHCP_EB_ENCAP, 1 )
164 /** "Your" IP address
166 * This option is used internally to contain the value of the "yiaddr"
167 * field, in order to provide a consistent approach to storing and
168 * processing options. It should never be present in a DHCP packet.
170 #define DHCP_EB_YIADDR DHCP_ENCAP_OPT ( DHCP_EB_ENCAP, 2 )
172 /** "Server" IP address
174 * This option is used internally to contain the value of the "siaddr"
175 * field, in order to provide a consistent approach to storing and
176 * processing options. It should never be present in a DHCP packet.
178 #define DHCP_EB_SIADDR DHCP_ENCAP_OPT ( DHCP_EB_ENCAP, 3 )
181 * Tags in the range 0x10-0x7f are reserved for feature markers
187 * If set to a non-zero value, gPXE will not wait for ProxyDHCP offers
188 * and will ignore any ProxyDHCP offers that it receives.
190 #define DHCP_EB_NO_PROXYDHCP DHCP_ENCAP_OPT ( DHCP_EB_ENCAP, 0xb0 )
192 /** Network device descriptor
194 * Byte 0 is the bus type ID; remaining bytes depend on the bus type.
198 * Byte 1 : PCI vendor ID MSB
199 * Byte 2 : PCI vendor ID LSB
200 * Byte 3 : PCI device ID MSB
201 * Byte 4 : PCI device ID LSB
203 #define DHCP_EB_BUS_ID DHCP_ENCAP_OPT ( DHCP_EB_ENCAP, 0xb1 )
205 /** BIOS drive number
207 * This is the drive number for a drive emulated via INT 13. 0x80 is
208 * the first hard disk, 0x81 is the second hard disk, etc.
210 #define DHCP_EB_BIOS_DRIVE DHCP_ENCAP_OPT ( DHCP_EB_ENCAP, 0xbd )
214 * This will be used as the username for any required authentication.
215 * It is expected that this option's value will be held in
216 * non-volatile storage, rather than transmitted as part of a DHCP
219 #define DHCP_EB_USERNAME DHCP_ENCAP_OPT ( DHCP_EB_ENCAP, 0xbe )
223 * This will be used as the password for any required authentication.
224 * It is expected that this option's value will be held in
225 * non-volatile storage, rather than transmitted as part of a DHCP
228 #define DHCP_EB_PASSWORD DHCP_ENCAP_OPT ( DHCP_EB_ENCAP, 0xbf )
230 /** iSCSI primary target IQN */
231 #define DHCP_ISCSI_PRIMARY_TARGET_IQN 201
233 /** iSCSI secondary target IQN */
234 #define DHCP_ISCSI_SECONDARY_TARGET_IQN 202
236 /** iSCSI initiator IQN */
237 #define DHCP_ISCSI_INITIATOR_IQN 203
239 /** Maximum normal DHCP option */
240 #define DHCP_MAX_OPTION 254
244 * This tag does not have a length field; it is always only a single
252 * Count number of arguments to a variadic macro
254 * This rather neat, non-iterative solution is courtesy of Laurent
258 #define _VA_ARG_COUNT( _1, _2, _3, _4, _5, _6, _7, _8, \
259 _9, _10, _11, _12, _13, _14, _15, _16, \
260 _17, _18, _19, _20, _21, _22, _23, _24, \
261 _25, _26, _27, _28, _29, _30, _31, _32, \
262 _33, _34, _35, _36, _37, _38, _39, _40, \
263 _41, _42, _43, _44, _45, _46, _47, _48, \
264 _49, _50, _51, _52, _53, _54, _55, _56, \
265 _57, _58, _59, _60, _61, _62, _63, N, ... ) N
266 #define VA_ARG_COUNT( ... ) \
267 _VA_ARG_COUNT ( __VA_ARGS__, \
268 63, 62, 61, 60, 59, 58, 57, 56, \
269 55, 54, 53, 52, 51, 50, 49, 48, \
270 47, 46, 45, 44, 43, 42, 41, 40, \
271 39, 38, 37, 36, 35, 34, 33, 32, \
272 31, 30, 29, 28, 27, 26, 25, 24, \
273 23, 22, 21, 20, 19, 18, 17, 16, \
274 15, 14, 13, 12, 11, 10, 9, 8, \
275 7, 6, 5, 4, 3, 2, 1, 0 )
277 /** Construct a DHCP option from a list of bytes */
278 #define DHCP_OPTION( ... ) VA_ARG_COUNT ( __VA_ARGS__ ), __VA_ARGS__
280 /** Construct a DHCP option from a list of characters */
281 #define DHCP_STRING( ... ) DHCP_OPTION ( __VA_ARGS__ )
283 /** Construct a byte-valued DHCP option */
284 #define DHCP_BYTE( value ) DHCP_OPTION ( value )
286 /** Construct a word-valued DHCP option */
287 #define DHCP_WORD( value ) DHCP_OPTION ( ( ( (value) >> 8 ) & 0xff ), \
288 ( ( (value) >> 0 ) & 0xff ) )
290 /** Construct a dword-valued DHCP option */
291 #define DHCP_DWORD( value ) DHCP_OPTION ( ( ( (value) >> 24 ) & 0xff ), \
292 ( ( (value) >> 16 ) & 0xff ), \
293 ( ( (value) >> 8 ) & 0xff ), \
294 ( ( (value) >> 0 ) & 0xff ) )
296 /** Construct a DHCP encapsulated options field */
297 #define DHCP_ENCAP( ... ) DHCP_OPTION ( __VA_ARGS__, DHCP_END )
302 * DHCP options consist of a mandatory tag, a length field that is
303 * mandatory for all options except @c DHCP_PAD and @c DHCP_END, and a
309 * Must be a @c DHCP_XXX value.
314 * This is the length of the data field (i.e. excluding the
315 * tag and length fields). For the two tags @c DHCP_PAD and
316 * @c DHCP_END, the length field is implicitly zero and is
317 * also missing, i.e. these DHCP options are only a single
323 * Interpretation of the content is entirely dependent upon
324 * the tag. For fields containing a multi-byte integer, the
325 * field is defined to be in network-endian order (unless you
326 * are Intel and feel like violating the spec for fun).
336 } __attribute__ (( packed ));
339 * Length of a DHCP option header
341 * The header is the portion excluding the data, i.e. the tag and the
344 #define DHCP_OPTION_HEADER_LEN ( offsetof ( struct dhcp_option, data ) )
346 /** Maximum length for a single DHCP option */
347 #define DHCP_MAX_LEN 0xff
349 /** A DHCP options block */
350 struct dhcp_option_block {
351 /** Reference counter */
352 struct refcnt refcnt;
353 /** List of option blocks */
354 struct list_head list;
355 /** Option block raw data */
357 /** Option block length */
359 /** Option block maximum length */
363 * This is determined at the time of the call to
364 * register_options() by searching for the @c DHCP_EB_PRIORITY
377 * This must be either @c BOOTP_REQUEST or @c BOOTP_REPLY.
380 /** Hardware address type
382 * This is an ARPHRD_XXX constant. Note that ARPHRD_XXX
383 * constants are nominally 16 bits wide; this could be
384 * considered to be a bug in the BOOTP/DHCP specification.
387 /** Hardware address length */
389 /** Number of hops from server */
391 /** Transaction ID */
393 /** Seconds since start of acquisition */
397 /** "Client" IP address
399 * This is filled in if the client already has an IP address
400 * assigned and can respond to ARP requests.
402 struct in_addr ciaddr;
403 /** "Your" IP address
405 * This is the IP address assigned by the server to the client.
407 struct in_addr yiaddr;
408 /** "Server" IP address
410 * This is the IP address of the next server to be used in the
413 struct in_addr siaddr;
414 /** "Gateway" IP address
416 * This is the IP address of the DHCP relay agent, if any.
418 struct in_addr giaddr;
419 /** Client hardware address */
421 /** Server host name (null terminated)
423 * This field may be overridden and contain DHCP options
426 /** Boot file name (null terminated)
428 * This field may be overridden and contain DHCP options
431 /** DHCP magic cookie
433 * Must have the value @c DHCP_MAGIC_COOKIE.
438 * Variable length; extends to the end of the packet. Minimum
439 * length (for the sake of sanity) is 1, to allow for a single
445 /** Opcode for a request from client to server */
446 #define BOOTP_REQUEST 1
448 /** Opcode for a reply from server to client */
449 #define BOOTP_REPLY 2
451 /** BOOTP reply must be broadcast
453 * Clients that cannot accept unicast BOOTP replies must set this
456 #define BOOTP_FL_BROADCAST 0x8000
458 /** DHCP magic cookie */
459 #define DHCP_MAGIC_COOKIE 0x63825363UL
461 /** DHCP minimum packet length
463 * This is the mandated minimum packet length that a DHCP participant
464 * must be prepared to receive.
466 #define DHCP_MIN_LEN 552
473 /** The DHCP packet contents */
474 struct dhcphdr *dhcphdr;
475 /** Maximum length of the DHCP packet buffer */
477 /** Used length of the DHCP packet buffer */
480 struct dhcp_option_block options;
483 /** A DHCP option applicator */
484 struct dhcp_option_applicator {
485 /** DHCP option tag */
489 * @v tag DHCP option tag
490 * @v option DHCP option
491 * @ret rc Return status code
493 int ( * apply ) ( unsigned int tag, struct dhcp_option *option );
496 /** Declare a DHCP option applicator */
497 #define __dhcp_applicator \
498 __table ( struct dhcp_option_applicator, dhcp_applicators, 01 )
501 * Get reference to DHCP options block
503 * @v options DHCP options block
504 * @ret options DHCP options block
506 static inline __attribute__ (( always_inline )) struct dhcp_option_block *
507 dhcpopt_get ( struct dhcp_option_block *options ) {
508 ref_get ( &options->refcnt );
513 * Drop reference to DHCP options block
515 * @v options DHCP options block, or NULL
517 static inline __attribute__ (( always_inline )) void
518 dhcpopt_put ( struct dhcp_option_block *options ) {
519 ref_put ( &options->refcnt );
522 /** Maximum time that we will wait for ProxyDHCP offers */
523 #define PROXYDHCP_WAIT_TIME ( TICKS_PER_SEC * 1 )
525 extern struct list_head dhcp_option_blocks;
527 extern unsigned long dhcp_num_option ( struct dhcp_option *option );
528 extern void dhcp_ipv4_option ( struct dhcp_option *option,
529 struct in_addr *inp );
530 extern int dhcp_snprintf ( char *buf, size_t size,
531 struct dhcp_option *option );
532 extern struct dhcp_option *
533 find_dhcp_option ( struct dhcp_option_block *options, unsigned int tag );
534 extern void register_dhcp_options ( struct dhcp_option_block *options );
535 extern void unregister_dhcp_options ( struct dhcp_option_block *options );
536 extern void init_dhcp_options ( struct dhcp_option_block *options,
537 void *data, size_t max_len );
538 extern struct dhcp_option_block * __malloc alloc_dhcp_options ( size_t max_len );
539 extern struct dhcp_option *
540 set_dhcp_option ( struct dhcp_option_block *options, unsigned int tag,
541 const void *data, size_t len );
542 extern struct dhcp_option * find_global_dhcp_option ( unsigned int tag );
543 extern unsigned long find_dhcp_num_option ( struct dhcp_option_block *options,
545 extern unsigned long find_global_dhcp_num_option ( unsigned int tag );
546 extern void find_dhcp_ipv4_option ( struct dhcp_option_block *options,
547 unsigned int tag, struct in_addr *inp );
548 extern void find_global_dhcp_ipv4_option ( unsigned int tag,
549 struct in_addr *inp );
550 extern void delete_dhcp_option ( struct dhcp_option_block *options,
553 extern int apply_dhcp_options ( struct dhcp_option_block *options );
554 extern int apply_global_dhcp_options ( void );
556 extern int create_dhcp_request ( struct net_device *netdev, int msgtype,
557 struct dhcp_option_block *options,
558 void *data, size_t max_len,
559 struct dhcp_packet *dhcppkt );
560 extern int create_dhcp_response ( struct net_device *netdev, int msgtype,
561 struct dhcp_option_block *options,
562 void *data, size_t max_len,
563 struct dhcp_packet *dhcppkt );
565 extern int start_dhcp ( struct job_interface *job, struct net_device *netdev,
566 int (*register_options) ( struct net_device *,
567 struct dhcp_option_block * ));
568 extern int dhcp_configure_netdev ( struct net_device *netdev,
569 struct dhcp_option_block *options );
571 #endif /* _GPXE_DHCP_H */