6 * Dynamic Host Configuration Protocol
11 #include <gpxe/list.h>
13 #include <gpxe/refcnt.h>
14 #include <gpxe/tables.h>
20 /** BOOTP/DHCP server port */
21 #define BOOTPS_PORT 67
23 /** BOOTP/DHCP client port */
24 #define BOOTPC_PORT 68
26 /** Construct a tag value for an encapsulated option
28 * This tag value can be passed to Etherboot functions when searching
29 * for DHCP options in order to search for a tag within an
30 * encapsulated options block.
32 #define DHCP_ENCAP_OPT( encapsulator, encapsulated ) \
33 ( ( (encapsulator) << 8 ) | (encapsulated) )
34 /** Extract encapsulating option block tag from encapsulated tag value */
35 #define DHCP_ENCAPSULATOR( encap_opt ) ( (encap_opt) >> 8 )
36 /** Extract encapsulated option tag from encapsulated tag value */
37 #define DHCP_ENCAPSULATED( encap_opt ) ( (encap_opt) & 0xff )
38 /** Option is encapsulated */
39 #define DHCP_IS_ENCAP_OPT( opt ) DHCP_ENCAPSULATOR( opt )
42 * @defgroup dhcpopts DHCP option tags
48 * This tag does not have a length field; it is always only a single
53 /** Minimum normal DHCP option */
54 #define DHCP_MIN_OPTION 1
57 #define DHCP_SUBNET_MASK 1
60 #define DHCP_ROUTERS 3
63 #define DHCP_DNS_SERVERS 6
66 #define DHCP_LOG_SERVERS 7
69 #define DHCP_HOST_NAME 12
72 #define DHCP_DOMAIN_NAME 15
75 #define DHCP_ROOT_PATH 17
77 /** Vendor encapsulated options */
78 #define DHCP_VENDOR_ENCAP 43
80 /** Requested IP address */
81 #define DHCP_REQUESTED_ADDRESS 50
84 #define DHCP_LEASE_TIME 51
86 /** Option overloading
88 * The value of this option is the bitwise-OR of zero or more
89 * DHCP_OPTION_OVERLOAD_XXX constants.
91 #define DHCP_OPTION_OVERLOAD 52
93 /** The "file" field is overloaded to contain extra DHCP options */
94 #define DHCP_OPTION_OVERLOAD_FILE 1
96 /** The "sname" field is overloaded to contain extra DHCP options */
97 #define DHCP_OPTION_OVERLOAD_SNAME 2
99 /** DHCP message type */
100 #define DHCP_MESSAGE_TYPE 53
101 #define DHCPDISCOVER 1
103 #define DHCPREQUEST 3
104 #define DHCPDECLINE 4
107 #define DHCPRELEASE 7
110 /** DHCP server identifier */
111 #define DHCP_SERVER_IDENTIFIER 54
113 /** Parameter request list */
114 #define DHCP_PARAMETER_REQUEST_LIST 55
116 /** Maximum DHCP message size */
117 #define DHCP_MAX_MESSAGE_SIZE 57
119 /** Vendor class identifier */
120 #define DHCP_VENDOR_CLASS_ID 60
122 /** Client identifier */
123 #define DHCP_CLIENT_ID 61
127 * This option replaces the fixed "sname" field, when that field is
128 * used to contain overloaded options.
130 #define DHCP_TFTP_SERVER_NAME 66
134 * This option replaces the fixed "file" field, when that field is
135 * used to contain overloaded options.
137 #define DHCP_BOOTFILE_NAME 67
139 /** Client system architecture */
140 #define DHCP_CLIENT_ARCHITECTURE 93
142 /** Client network device interface */
143 #define DHCP_CLIENT_NDI 94
145 /** UUID client identifier */
146 #define DHCP_CLIENT_UUID 97
148 /** Etherboot-specific encapsulated options
150 * This encapsulated options field is used to contain all options
151 * specific to Etherboot (i.e. not assigned by IANA or other standards
154 #define DHCP_EB_ENCAP 175
156 /** Priority of this options block
158 * This is a signed 8-bit integer field indicating the priority of
159 * this block of options. It can be used to specify the relative
160 * priority of multiple option blocks (e.g. options from non-volatile
161 * storage versus options from a DHCP server).
163 #define DHCP_EB_PRIORITY DHCP_ENCAP_OPT ( DHCP_EB_ENCAP, 1 )
165 /** "Your" IP address
167 * This option is used internally to contain the value of the "yiaddr"
168 * field, in order to provide a consistent approach to storing and
169 * processing options. It should never be present in a DHCP packet.
171 #define DHCP_EB_YIADDR DHCP_ENCAP_OPT ( DHCP_EB_ENCAP, 2 )
173 /** "Server" IP address
175 * This option is used internally to contain the value of the "siaddr"
176 * field, in order to provide a consistent approach to storing and
177 * processing options. It should never be present in a DHCP packet.
179 #define DHCP_EB_SIADDR DHCP_ENCAP_OPT ( DHCP_EB_ENCAP, 3 )
182 * Tags in the range 0x10-0x7f are reserved for feature markers
186 /** Network device descriptor
188 * Byte 0 is the bus type ID; remaining bytes depend on the bus type.
192 * Byte 1 : PCI vendor ID MSB
193 * Byte 2 : PCI vendor ID LSB
194 * Byte 3 : PCI device ID MSB
195 * Byte 4 : PCI device ID LSB
197 #define DHCP_EB_BUS_ID DHCP_ENCAP_OPT ( DHCP_EB_ENCAP, 0xb1 )
199 /** BIOS drive number
201 * This is the drive number for a drive emulated via INT 13. 0x80 is
202 * the first hard disk, 0x81 is the second hard disk, etc.
204 #define DHCP_EB_BIOS_DRIVE DHCP_ENCAP_OPT ( DHCP_EB_ENCAP, 0xbd )
208 * This will be used as the username for any required authentication.
209 * It is expected that this option's value will be held in
210 * non-volatile storage, rather than transmitted as part of a DHCP
213 #define DHCP_EB_USERNAME DHCP_ENCAP_OPT ( DHCP_EB_ENCAP, 0xbe )
217 * This will be used as the password for any required authentication.
218 * It is expected that this option's value will be held in
219 * non-volatile storage, rather than transmitted as part of a DHCP
222 #define DHCP_EB_PASSWORD DHCP_ENCAP_OPT ( DHCP_EB_ENCAP, 0xbf )
224 /** iSCSI primary target IQN */
225 #define DHCP_ISCSI_PRIMARY_TARGET_IQN 201
227 /** iSCSI secondary target IQN */
228 #define DHCP_ISCSI_SECONDARY_TARGET_IQN 202
230 /** iSCSI initiator IQN */
231 #define DHCP_ISCSI_INITIATOR_IQN 203
233 /** Maximum normal DHCP option */
234 #define DHCP_MAX_OPTION 254
238 * This tag does not have a length field; it is always only a single
246 * Count number of arguments to a variadic macro
248 * This rather neat, non-iterative solution is courtesy of Laurent
252 #define _VA_ARG_COUNT( _1, _2, _3, _4, _5, _6, _7, _8, \
253 _9, _10, _11, _12, _13, _14, _15, _16, \
254 _17, _18, _19, _20, _21, _22, _23, _24, \
255 _25, _26, _27, _28, _29, _30, _31, _32, \
256 _33, _34, _35, _36, _37, _38, _39, _40, \
257 _41, _42, _43, _44, _45, _46, _47, _48, \
258 _49, _50, _51, _52, _53, _54, _55, _56, \
259 _57, _58, _59, _60, _61, _62, _63, N, ... ) N
260 #define VA_ARG_COUNT( ... ) \
261 _VA_ARG_COUNT ( __VA_ARGS__, \
262 63, 62, 61, 60, 59, 58, 57, 56, \
263 55, 54, 53, 52, 51, 50, 49, 48, \
264 47, 46, 45, 44, 43, 42, 41, 40, \
265 39, 38, 37, 36, 35, 34, 33, 32, \
266 31, 30, 29, 28, 27, 26, 25, 24, \
267 23, 22, 21, 20, 19, 18, 17, 16, \
268 15, 14, 13, 12, 11, 10, 9, 8, \
269 7, 6, 5, 4, 3, 2, 1, 0 )
271 /** Construct a DHCP option from a list of bytes */
272 #define DHCP_OPTION( ... ) VA_ARG_COUNT ( __VA_ARGS__ ), __VA_ARGS__
274 /** Construct a DHCP option from a list of characters */
275 #define DHCP_STRING( ... ) DHCP_OPTION ( __VA_ARGS__ )
277 /** Construct a byte-valued DHCP option */
278 #define DHCP_BYTE( value ) DHCP_OPTION ( value )
280 /** Construct a word-valued DHCP option */
281 #define DHCP_WORD( value ) DHCP_OPTION ( ( ( (value) >> 8 ) & 0xff ), \
282 ( ( (value) >> 0 ) & 0xff ) )
284 /** Construct a dword-valued DHCP option */
285 #define DHCP_DWORD( value ) DHCP_OPTION ( ( ( (value) >> 24 ) & 0xff ), \
286 ( ( (value) >> 16 ) & 0xff ), \
287 ( ( (value) >> 8 ) & 0xff ), \
288 ( ( (value) >> 0 ) & 0xff ) )
290 /** Construct a DHCP encapsulated options field */
291 #define DHCP_ENCAP( ... ) DHCP_OPTION ( __VA_ARGS__, DHCP_END )
296 * DHCP options consist of a mandatory tag, a length field that is
297 * mandatory for all options except @c DHCP_PAD and @c DHCP_END, and a
303 * Must be a @c DHCP_XXX value.
308 * This is the length of the data field (i.e. excluding the
309 * tag and length fields). For the two tags @c DHCP_PAD and
310 * @c DHCP_END, the length field is implicitly zero and is
311 * also missing, i.e. these DHCP options are only a single
317 * Interpretation of the content is entirely dependent upon
318 * the tag. For fields containing a multi-byte integer, the
319 * field is defined to be in network-endian order (unless you
320 * are Intel and feel like violating the spec for fun).
330 } __attribute__ (( packed ));
333 * Length of a DHCP option header
335 * The header is the portion excluding the data, i.e. the tag and the
338 #define DHCP_OPTION_HEADER_LEN ( offsetof ( struct dhcp_option, data ) )
340 /** Maximum length for a single DHCP option */
341 #define DHCP_MAX_LEN 0xff
343 /** A DHCP options block */
344 struct dhcp_option_block {
345 /** Reference counter */
346 struct refcnt refcnt;
347 /** List of option blocks */
348 struct list_head list;
349 /** Option block raw data */
351 /** Option block length */
353 /** Option block maximum length */
357 * This is determined at the time of the call to
358 * register_options() by searching for the @c DHCP_EB_PRIORITY
371 * This must be either @c BOOTP_REQUEST or @c BOOTP_REPLY.
374 /** Hardware address type
376 * This is an ARPHRD_XXX constant. Note that ARPHRD_XXX
377 * constants are nominally 16 bits wide; this could be
378 * considered to be a bug in the BOOTP/DHCP specification.
381 /** Hardware address length */
383 /** Number of hops from server */
385 /** Transaction ID */
387 /** Seconds since start of acquisition */
391 /** "Client" IP address
393 * This is filled in if the client already has an IP address
394 * assigned and can respond to ARP requests.
396 struct in_addr ciaddr;
397 /** "Your" IP address
399 * This is the IP address assigned by the server to the client.
401 struct in_addr yiaddr;
402 /** "Server" IP address
404 * This is the IP address of the next server to be used in the
407 struct in_addr siaddr;
408 /** "Gateway" IP address
410 * This is the IP address of the DHCP relay agent, if any.
412 struct in_addr giaddr;
413 /** Client hardware address */
415 /** Server host name (null terminated)
417 * This field may be overridden and contain DHCP options
420 /** Boot file name (null terminated)
422 * This field may be overridden and contain DHCP options
425 /** DHCP magic cookie
427 * Must have the value @c DHCP_MAGIC_COOKIE.
432 * Variable length; extends to the end of the packet. Minimum
433 * length (for the sake of sanity) is 1, to allow for a single
439 /** Opcode for a request from client to server */
440 #define BOOTP_REQUEST 1
442 /** Opcode for a reply from server to client */
443 #define BOOTP_REPLY 2
445 /** BOOTP reply must be broadcast
447 * Clients that cannot accept unicast BOOTP replies must set this
450 #define BOOTP_FL_BROADCAST 0x8000
452 /** DHCP magic cookie */
453 #define DHCP_MAGIC_COOKIE 0x63825363UL
455 /** DHCP minimum packet length
457 * This is the mandated minimum packet length that a DHCP participant
458 * must be prepared to receive.
460 #define DHCP_MIN_LEN 552
467 /** The DHCP packet contents */
468 struct dhcphdr *dhcphdr;
469 /** Maximum length of the DHCP packet buffer */
471 /** Used length of the DHCP packet buffer */
474 struct dhcp_option_block options;
477 /** A DHCP option applicator */
478 struct dhcp_option_applicator {
479 /** DHCP option tag */
483 * @v tag DHCP option tag
484 * @v option DHCP option
485 * @ret rc Return status code
487 int ( * apply ) ( unsigned int tag, struct dhcp_option *option );
490 /** Declare a DHCP option applicator */
491 #define __dhcp_applicator \
492 __table ( struct dhcp_option_applicator, dhcp_applicators, 01 )
495 * Get reference to DHCP options block
497 * @v options DHCP options block
498 * @ret options DHCP options block
500 static inline __attribute__ (( always_inline )) struct dhcp_option_block *
501 dhcpopt_get ( struct dhcp_option_block *options ) {
502 ref_get ( &options->refcnt );
507 * Drop reference to DHCP options block
509 * @v options DHCP options block, or NULL
511 static inline __attribute__ (( always_inline )) void
512 dhcpopt_put ( struct dhcp_option_block *options ) {
513 ref_put ( &options->refcnt );
516 /** Maximum time that we will wait for ProxyDHCP offers */
517 #define PROXYDHCP_WAIT_TIME ( TICKS_PER_SEC * 2 )
519 extern struct list_head dhcp_option_blocks;
521 extern unsigned long dhcp_num_option ( struct dhcp_option *option );
522 extern void dhcp_ipv4_option ( struct dhcp_option *option,
523 struct in_addr *inp );
524 extern int dhcp_snprintf ( char *buf, size_t size,
525 struct dhcp_option *option );
526 extern struct dhcp_option *
527 find_dhcp_option ( struct dhcp_option_block *options, unsigned int tag );
528 extern void register_dhcp_options ( struct dhcp_option_block *options );
529 extern void unregister_dhcp_options ( struct dhcp_option_block *options );
530 extern void init_dhcp_options ( struct dhcp_option_block *options,
531 void *data, size_t max_len );
532 extern struct dhcp_option_block * __malloc alloc_dhcp_options ( size_t max_len );
533 extern struct dhcp_option *
534 set_dhcp_option ( struct dhcp_option_block *options, unsigned int tag,
535 const void *data, size_t len );
536 extern struct dhcp_option * find_global_dhcp_option ( unsigned int tag );
537 extern unsigned long find_dhcp_num_option ( struct dhcp_option_block *options,
539 extern unsigned long find_global_dhcp_num_option ( unsigned int tag );
540 extern void find_dhcp_ipv4_option ( struct dhcp_option_block *options,
541 unsigned int tag, struct in_addr *inp );
542 extern void find_global_dhcp_ipv4_option ( unsigned int tag,
543 struct in_addr *inp );
544 extern void delete_dhcp_option ( struct dhcp_option_block *options,
547 extern int apply_dhcp_options ( struct dhcp_option_block *options );
548 extern int apply_global_dhcp_options ( void );
550 extern int create_dhcp_request ( struct net_device *netdev, int msgtype,
551 struct dhcp_option_block *options,
552 void *data, size_t max_len,
553 struct dhcp_packet *dhcppkt );
554 extern int create_dhcp_response ( struct net_device *netdev, int msgtype,
555 struct dhcp_option_block *options,
556 void *data, size_t max_len,
557 struct dhcp_packet *dhcppkt );
559 extern int start_dhcp ( struct job_interface *job, struct net_device *netdev,
560 int (*register_options) ( struct net_device *,
561 struct dhcp_option_block * ));
562 extern int dhcp_configure_netdev ( struct net_device *netdev,
563 struct dhcp_option_block *options );
565 #endif /* _GPXE_DHCP_H */