6 * Dynamic Host Configuration Protocol
12 #include <gpxe/list.h>
13 #include <gpxe/refcnt.h>
14 #include <gpxe/tables.h>
21 /** BOOTP/DHCP server port */
22 #define BOOTPS_PORT 67
24 /** BOOTP/DHCP client port */
25 #define BOOTPC_PORT 68
27 /** ProxyDHCP server port */
28 #define PROXYDHCP_PORT 4011
30 /** Construct a tag value for an encapsulated option
32 * This tag value can be passed to Etherboot functions when searching
33 * for DHCP options in order to search for a tag within an
34 * encapsulated options block.
36 #define DHCP_ENCAP_OPT( encapsulator, encapsulated ) \
37 ( ( (encapsulator) << 8 ) | (encapsulated) )
38 /** Extract encapsulating option block tag from encapsulated tag value */
39 #define DHCP_ENCAPSULATOR( encap_opt ) ( (encap_opt) >> 8 )
40 /** Extract encapsulated option tag from encapsulated tag value */
41 #define DHCP_ENCAPSULATED( encap_opt ) ( (encap_opt) & 0xff )
42 /** Option is encapsulated */
43 #define DHCP_IS_ENCAP_OPT( opt ) DHCP_ENCAPSULATOR( opt )
46 * @defgroup dhcpopts DHCP option tags
52 * This tag does not have a length field; it is always only a single
57 /** Minimum normal DHCP option */
58 #define DHCP_MIN_OPTION 1
61 #define DHCP_SUBNET_MASK 1
64 #define DHCP_ROUTERS 3
67 #define DHCP_DNS_SERVERS 6
70 #define DHCP_LOG_SERVERS 7
73 #define DHCP_HOST_NAME 12
76 #define DHCP_DOMAIN_NAME 15
79 #define DHCP_ROOT_PATH 17
81 /** Vendor encapsulated options */
82 #define DHCP_VENDOR_ENCAP 43
84 /** PXE boot server multicast address */
85 #define DHCP_PXE_BOOT_SERVER_MCAST DHCP_ENCAP_OPT ( DHCP_VENDOR_ENCAP, 7 )
87 /** Requested IP address */
88 #define DHCP_REQUESTED_ADDRESS 50
91 #define DHCP_LEASE_TIME 51
93 /** Option overloading
95 * The value of this option is the bitwise-OR of zero or more
96 * DHCP_OPTION_OVERLOAD_XXX constants.
98 #define DHCP_OPTION_OVERLOAD 52
100 /** The "file" field is overloaded to contain extra DHCP options */
101 #define DHCP_OPTION_OVERLOAD_FILE 1
103 /** The "sname" field is overloaded to contain extra DHCP options */
104 #define DHCP_OPTION_OVERLOAD_SNAME 2
106 /** DHCP message type */
107 #define DHCP_MESSAGE_TYPE 53
109 #define DHCPDISCOVER 1
111 #define DHCPREQUEST 3
112 #define DHCPDECLINE 4
115 #define DHCPRELEASE 7
118 /** DHCP server identifier */
119 #define DHCP_SERVER_IDENTIFIER 54
121 /** Parameter request list */
122 #define DHCP_PARAMETER_REQUEST_LIST 55
124 /** Maximum DHCP message size */
125 #define DHCP_MAX_MESSAGE_SIZE 57
127 /** Vendor class identifier */
128 #define DHCP_VENDOR_CLASS_ID 60
130 /** Client identifier */
131 #define DHCP_CLIENT_ID 61
135 * This option replaces the fixed "sname" field, when that field is
136 * used to contain overloaded options.
138 #define DHCP_TFTP_SERVER_NAME 66
142 * This option replaces the fixed "file" field, when that field is
143 * used to contain overloaded options.
145 #define DHCP_BOOTFILE_NAME 67
147 /** Client system architecture */
148 #define DHCP_CLIENT_ARCHITECTURE 93
150 /** Client network device interface */
151 #define DHCP_CLIENT_NDI 94
153 /** UUID client identifier */
154 #define DHCP_CLIENT_UUID 97
156 /** Etherboot-specific encapsulated options
158 * This encapsulated options field is used to contain all options
159 * specific to Etherboot (i.e. not assigned by IANA or other standards
162 #define DHCP_EB_ENCAP 175
164 /** Priority of this options block
166 * This is a signed 8-bit integer field indicating the priority of
167 * this block of options. It can be used to specify the relative
168 * priority of multiple option blocks (e.g. options from non-volatile
169 * storage versus options from a DHCP server).
171 #define DHCP_EB_PRIORITY DHCP_ENCAP_OPT ( DHCP_EB_ENCAP, 0x01 )
173 /** "Your" IP address
175 * This option is used internally to contain the value of the "yiaddr"
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_YIADDR DHCP_ENCAP_OPT ( DHCP_EB_ENCAP, 0x02 )
181 /** "Server" IP address
183 * This option is used internally to contain the value of the "siaddr"
184 * field, in order to provide a consistent approach to storing and
185 * processing options. It should never be present in a DHCP packet.
187 #define DHCP_EB_SIADDR DHCP_ENCAP_OPT ( DHCP_EB_ENCAP, 0x03 )
189 /** Keep SAN drive registered
191 * If set to a non-zero value, gPXE will not detach any SAN drive
192 * after failing to boot from it. (This option is required in order
193 * to perform a Windows Server 2008 installation direct to an iSCSI
196 #define DHCP_EB_KEEP_SAN DHCP_ENCAP_OPT ( DHCP_EB_ENCAP, 0x08 )
199 * Tags in the range 0x10-0x7f are reserved for feature markers
205 * If set to a non-zero value, gPXE will not wait for ProxyDHCP offers
206 * and will ignore any ProxyDHCP offers that it receives.
208 #define DHCP_EB_NO_PROXYDHCP DHCP_ENCAP_OPT ( DHCP_EB_ENCAP, 0xb0 )
210 /** Network device descriptor
212 * Byte 0 is the bus type ID; remaining bytes depend on the bus type.
216 * Byte 1 : PCI vendor ID MSB
217 * Byte 2 : PCI vendor ID LSB
218 * Byte 3 : PCI device ID MSB
219 * Byte 4 : PCI device ID LSB
221 #define DHCP_EB_BUS_ID DHCP_ENCAP_OPT ( DHCP_EB_ENCAP, 0xb1 )
223 /** BIOS drive number
225 * This is the drive number for a drive emulated via INT 13. 0x80 is
226 * the first hard disk, 0x81 is the second hard disk, etc.
228 #define DHCP_EB_BIOS_DRIVE DHCP_ENCAP_OPT ( DHCP_EB_ENCAP, 0xbd )
232 * This will be used as the username for any required authentication.
233 * It is expected that this option's value will be held in
234 * non-volatile storage, rather than transmitted as part of a DHCP
237 #define DHCP_EB_USERNAME DHCP_ENCAP_OPT ( DHCP_EB_ENCAP, 0xbe )
241 * This will be used as the password for any required authentication.
242 * It is expected that this option's value will be held in
243 * non-volatile storage, rather than transmitted as part of a DHCP
246 #define DHCP_EB_PASSWORD DHCP_ENCAP_OPT ( DHCP_EB_ENCAP, 0xbf )
250 * This will be used as the reverse username (i.e. the username
251 * provided by the server) for any required authentication. It is
252 * expected that this option's value will be held in non-volatile
253 * storage, rather than transmitted as part of a DHCP packet.
255 #define DHCP_EB_REVERSE_USERNAME DHCP_ENCAP_OPT ( DHCP_EB_ENCAP, 0xc0 )
259 * This will be used as the reverse password (i.e. the password
260 * provided by the server) for any required authentication. It is
261 * expected that this option's value will be held in non-volatile
262 * storage, rather than transmitted as part of a DHCP packet.
264 #define DHCP_EB_REVERSE_PASSWORD DHCP_ENCAP_OPT ( DHCP_EB_ENCAP, 0xc1 )
266 /** gPXE version number */
267 #define DHCP_EB_VERSION DHCP_ENCAP_OPT ( DHCP_EB_ENCAP, 0xeb )
269 /** iSCSI primary target IQN */
270 #define DHCP_ISCSI_PRIMARY_TARGET_IQN 201
272 /** iSCSI secondary target IQN */
273 #define DHCP_ISCSI_SECONDARY_TARGET_IQN 202
275 /** iSCSI initiator IQN */
276 #define DHCP_ISCSI_INITIATOR_IQN 203
278 /** Maximum normal DHCP option */
279 #define DHCP_MAX_OPTION 254
283 * This tag does not have a length field; it is always only a single
291 * Count number of arguments to a variadic macro
293 * This rather neat, non-iterative solution is courtesy of Laurent
297 #define _VA_ARG_COUNT( _1, _2, _3, _4, _5, _6, _7, _8, \
298 _9, _10, _11, _12, _13, _14, _15, _16, \
299 _17, _18, _19, _20, _21, _22, _23, _24, \
300 _25, _26, _27, _28, _29, _30, _31, _32, \
301 _33, _34, _35, _36, _37, _38, _39, _40, \
302 _41, _42, _43, _44, _45, _46, _47, _48, \
303 _49, _50, _51, _52, _53, _54, _55, _56, \
304 _57, _58, _59, _60, _61, _62, _63, N, ... ) N
305 #define VA_ARG_COUNT( ... ) \
306 _VA_ARG_COUNT ( __VA_ARGS__, \
307 63, 62, 61, 60, 59, 58, 57, 56, \
308 55, 54, 53, 52, 51, 50, 49, 48, \
309 47, 46, 45, 44, 43, 42, 41, 40, \
310 39, 38, 37, 36, 35, 34, 33, 32, \
311 31, 30, 29, 28, 27, 26, 25, 24, \
312 23, 22, 21, 20, 19, 18, 17, 16, \
313 15, 14, 13, 12, 11, 10, 9, 8, \
314 7, 6, 5, 4, 3, 2, 1, 0 )
316 /** Construct a DHCP option from a list of bytes */
317 #define DHCP_OPTION( ... ) VA_ARG_COUNT ( __VA_ARGS__ ), __VA_ARGS__
319 /** Construct a DHCP option from a list of characters */
320 #define DHCP_STRING( ... ) DHCP_OPTION ( __VA_ARGS__ )
322 /** Construct a byte-valued DHCP option */
323 #define DHCP_BYTE( value ) DHCP_OPTION ( value )
325 /** Construct a word-valued DHCP option */
326 #define DHCP_WORD( value ) DHCP_OPTION ( ( ( (value) >> 8 ) & 0xff ), \
327 ( ( (value) >> 0 ) & 0xff ) )
329 /** Construct a dword-valued DHCP option */
330 #define DHCP_DWORD( value ) DHCP_OPTION ( ( ( (value) >> 24 ) & 0xff ), \
331 ( ( (value) >> 16 ) & 0xff ), \
332 ( ( (value) >> 8 ) & 0xff ), \
333 ( ( (value) >> 0 ) & 0xff ) )
335 /** Construct a DHCP encapsulated options field */
336 #define DHCP_ENCAP( ... ) DHCP_OPTION ( __VA_ARGS__, DHCP_END )
341 * DHCP options consist of a mandatory tag, a length field that is
342 * mandatory for all options except @c DHCP_PAD and @c DHCP_END, and a
348 * Must be a @c DHCP_XXX value.
353 * This is the length of the data field (i.e. excluding the
354 * tag and length fields). For the two tags @c DHCP_PAD and
355 * @c DHCP_END, the length field is implicitly zero and is
356 * also missing, i.e. these DHCP options are only a single
362 } __attribute__ (( packed ));
365 * Length of a DHCP option header
367 * The header is the portion excluding the data, i.e. the tag and the
370 #define DHCP_OPTION_HEADER_LEN ( offsetof ( struct dhcp_option, data ) )
372 /** Maximum length for a single DHCP option */
373 #define DHCP_MAX_LEN 0xff
382 * This must be either @c BOOTP_REQUEST or @c BOOTP_REPLY.
385 /** Hardware address type
387 * This is an ARPHRD_XXX constant. Note that ARPHRD_XXX
388 * constants are nominally 16 bits wide; this could be
389 * considered to be a bug in the BOOTP/DHCP specification.
392 /** Hardware address length */
394 /** Number of hops from server */
396 /** Transaction ID */
398 /** Seconds since start of acquisition */
402 /** "Client" IP address
404 * This is filled in if the client already has an IP address
405 * assigned and can respond to ARP requests.
407 struct in_addr ciaddr;
408 /** "Your" IP address
410 * This is the IP address assigned by the server to the client.
412 struct in_addr yiaddr;
413 /** "Server" IP address
415 * This is the IP address of the next server to be used in the
418 struct in_addr siaddr;
419 /** "Gateway" IP address
421 * This is the IP address of the DHCP relay agent, if any.
423 struct in_addr giaddr;
424 /** Client hardware address */
426 /** Server host name (null terminated)
428 * This field may be overridden and contain DHCP options
431 /** Boot file name (null terminated)
433 * This field may be overridden and contain DHCP options
436 /** DHCP magic cookie
438 * Must have the value @c DHCP_MAGIC_COOKIE.
443 * Variable length; extends to the end of the packet. Minimum
444 * length (for the sake of sanity) is 1, to allow for a single
450 /** Opcode for a request from client to server */
451 #define BOOTP_REQUEST 1
453 /** Opcode for a reply from server to client */
454 #define BOOTP_REPLY 2
456 /** BOOTP reply must be broadcast
458 * Clients that cannot accept unicast BOOTP replies must set this
461 #define BOOTP_FL_BROADCAST 0x8000
463 /** DHCP magic cookie */
464 #define DHCP_MAGIC_COOKIE 0x63825363UL
466 /** DHCP minimum packet length
468 * This is the mandated minimum packet length that a DHCP participant
469 * must be prepared to receive.
471 #define DHCP_MIN_LEN 552
473 /** Maximum time that we will wait for ProxyDHCP responses */
474 #define PROXYDHCP_WAIT_TIME ( TICKS_PER_SEC * 1 )
476 /** Timeouts for sending DHCP packets */
477 #define DHCP_MIN_TIMEOUT ( 1 * TICKS_PER_SEC )
478 #define DHCP_MAX_TIMEOUT ( 10 * TICKS_PER_SEC )
480 /** Settings block name used for DHCP responses */
481 #define DHCP_SETTINGS_NAME "dhcp"
483 /** Settings block name used for ProxyDHCP responses */
484 #define PROXYDHCP_SETTINGS_NAME "proxydhcp"
486 /** Setting block name used for BootServerDHCP responses */
487 #define BSDHCP_SETTINGS_NAME "bs"
489 extern int dhcp_create_packet ( struct dhcp_packet *dhcppkt,
490 struct net_device *netdev, uint8_t msgtype,
491 struct dhcp_options *options,
492 void *data, size_t max_len );
493 extern int dhcp_create_request ( struct dhcp_packet *dhcppkt,
494 struct net_device *netdev,
495 struct in_addr ciaddr,
496 struct dhcp_packet *dhcpoffer,
497 void *data, size_t max_len );
498 extern int start_dhcp ( struct job_interface *job, struct net_device *netdev );
500 #endif /* _GPXE_DHCP_H */