You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  1. #ifndef _GPXE_DHCP_H
  2. #define _GPXE_DHCP_H
  3. /** @file
  4. *
  5. * Dynamic Host Configuration Protocol
  6. *
  7. */
  8. #include <stdint.h>
  9. #include <gpxe/list.h>
  10. #include <gpxe/in.h>
  11. #include <gpxe/refcnt.h>
  12. #include <gpxe/tables.h>
  13. struct net_device;
  14. struct job_interface;
  15. /** BOOTP/DHCP server port */
  16. #define BOOTPS_PORT 67
  17. /** BOOTP/DHCP client port */
  18. #define BOOTPC_PORT 68
  19. /** Construct a tag value for an encapsulated option
  20. *
  21. * This tag value can be passed to Etherboot functions when searching
  22. * for DHCP options in order to search for a tag within an
  23. * encapsulated options block.
  24. */
  25. #define DHCP_ENCAP_OPT( encapsulator, encapsulated ) \
  26. ( ( (encapsulator) << 8 ) | (encapsulated) )
  27. /** Extract encapsulating option block tag from encapsulated tag value */
  28. #define DHCP_ENCAPSULATOR( encap_opt ) ( (encap_opt) >> 8 )
  29. /** Extract encapsulated option tag from encapsulated tag value */
  30. #define DHCP_ENCAPSULATED( encap_opt ) ( (encap_opt) & 0xff )
  31. /** Option is encapsulated */
  32. #define DHCP_IS_ENCAP_OPT( opt ) DHCP_ENCAPSULATOR( opt )
  33. /**
  34. * @defgroup dhcpopts DHCP option tags
  35. * @{
  36. */
  37. /** Padding
  38. *
  39. * This tag does not have a length field; it is always only a single
  40. * byte in length.
  41. */
  42. #define DHCP_PAD 0
  43. /** Minimum normal DHCP option */
  44. #define DHCP_MIN_OPTION 1
  45. /** Subnet mask */
  46. #define DHCP_SUBNET_MASK 1
  47. /** Routers */
  48. #define DHCP_ROUTERS 3
  49. /** DNS servers */
  50. #define DHCP_DNS_SERVERS 6
  51. /** Syslog servers */
  52. #define DHCP_LOG_SERVERS 7
  53. /** Host name */
  54. #define DHCP_HOST_NAME 12
  55. /** Domain name */
  56. #define DHCP_DOMAIN_NAME 15
  57. /** Root path */
  58. #define DHCP_ROOT_PATH 17
  59. /** Vendor encapsulated options */
  60. #define DHCP_VENDOR_ENCAP 43
  61. /** Requested IP address */
  62. #define DHCP_REQUESTED_ADDRESS 50
  63. /** Lease time */
  64. #define DHCP_LEASE_TIME 51
  65. /** Option overloading
  66. *
  67. * The value of this option is the bitwise-OR of zero or more
  68. * DHCP_OPTION_OVERLOAD_XXX constants.
  69. */
  70. #define DHCP_OPTION_OVERLOAD 52
  71. /** The "file" field is overloaded to contain extra DHCP options */
  72. #define DHCP_OPTION_OVERLOAD_FILE 1
  73. /** The "sname" field is overloaded to contain extra DHCP options */
  74. #define DHCP_OPTION_OVERLOAD_SNAME 2
  75. /** DHCP message type */
  76. #define DHCP_MESSAGE_TYPE 53
  77. #define DHCPDISCOVER 1
  78. #define DHCPOFFER 2
  79. #define DHCPREQUEST 3
  80. #define DHCPDECLINE 4
  81. #define DHCPACK 5
  82. #define DHCPNAK 6
  83. #define DHCPRELEASE 7
  84. #define DHCPINFORM 8
  85. /** DHCP server identifier */
  86. #define DHCP_SERVER_IDENTIFIER 54
  87. /** Parameter request list */
  88. #define DHCP_PARAMETER_REQUEST_LIST 55
  89. /** Maximum DHCP message size */
  90. #define DHCP_MAX_MESSAGE_SIZE 57
  91. /** Vendor class identifier */
  92. #define DHCP_VENDOR_CLASS_ID 60
  93. /** Client identifier */
  94. #define DHCP_CLIENT_ID 61
  95. /** TFTP server name
  96. *
  97. * This option replaces the fixed "sname" field, when that field is
  98. * used to contain overloaded options.
  99. */
  100. #define DHCP_TFTP_SERVER_NAME 66
  101. /** Bootfile name
  102. *
  103. * This option replaces the fixed "file" field, when that field is
  104. * used to contain overloaded options.
  105. */
  106. #define DHCP_BOOTFILE_NAME 67
  107. /** Etherboot-specific encapsulated options
  108. *
  109. * This encapsulated options field is used to contain all options
  110. * specific to Etherboot (i.e. not assigned by IANA or other standards
  111. * bodies).
  112. */
  113. #define DHCP_EB_ENCAP 175
  114. /** Priority of this options block
  115. *
  116. * This is a signed 8-bit integer field indicating the priority of
  117. * this block of options. It can be used to specify the relative
  118. * priority of multiple option blocks (e.g. options from non-volatile
  119. * storage versus options from a DHCP server).
  120. */
  121. #define DHCP_EB_PRIORITY DHCP_ENCAP_OPT ( DHCP_EB_ENCAP, 1 )
  122. /** "Your" IP address
  123. *
  124. * This option is used internally to contain the value of the "yiaddr"
  125. * field, in order to provide a consistent approach to storing and
  126. * processing options. It should never be present in a DHCP packet.
  127. */
  128. #define DHCP_EB_YIADDR DHCP_ENCAP_OPT ( DHCP_EB_ENCAP, 2 )
  129. /** "Server" IP address
  130. *
  131. * This option is used internally to contain the value of the "siaddr"
  132. * field, in order to provide a consistent approach to storing and
  133. * processing options. It should never be present in a DHCP packet.
  134. */
  135. #define DHCP_EB_SIADDR DHCP_ENCAP_OPT ( DHCP_EB_ENCAP, 3 )
  136. /*
  137. * Tags in the range 0x10-0x7f are reserved for feature markers
  138. *
  139. */
  140. /** Network device descriptor
  141. *
  142. * Byte 0 is the bus type ID; remaining bytes depend on the bus type.
  143. *
  144. * PCI devices:
  145. * Byte 0 : 1 (PCI)
  146. * Byte 1 : PCI vendor ID MSB
  147. * Byte 2 : PCI vendor ID LSB
  148. * Byte 3 : PCI device ID MSB
  149. * Byte 4 : PCI device ID LSB
  150. */
  151. #define DHCP_EB_BUS_ID DHCP_ENCAP_OPT ( DHCP_EB_ENCAP, 0xb1 )
  152. /** BIOS drive number
  153. *
  154. * This is the drive number for a drive emulated via INT 13. 0x80 is
  155. * the first hard disk, 0x81 is the second hard disk, etc.
  156. */
  157. #define DHCP_EB_BIOS_DRIVE DHCP_ENCAP_OPT ( DHCP_EB_ENCAP, 0xbd )
  158. /** Username
  159. *
  160. * This will be used as the username for any required authentication.
  161. * It is expected that this option's value will be held in
  162. * non-volatile storage, rather than transmitted as part of a DHCP
  163. * packet.
  164. */
  165. #define DHCP_EB_USERNAME DHCP_ENCAP_OPT ( DHCP_EB_ENCAP, 0xbe )
  166. /** Password
  167. *
  168. * This will be used as the password for any required authentication.
  169. * It is expected that this option's value will be held in
  170. * non-volatile storage, rather than transmitted as part of a DHCP
  171. * packet.
  172. */
  173. #define DHCP_EB_PASSWORD DHCP_ENCAP_OPT ( DHCP_EB_ENCAP, 0xbf )
  174. /** iSCSI primary target IQN */
  175. #define DHCP_ISCSI_PRIMARY_TARGET_IQN 201
  176. /** iSCSI secondary target IQN */
  177. #define DHCP_ISCSI_SECONDARY_TARGET_IQN 202
  178. /** iSCSI initiator IQN */
  179. #define DHCP_ISCSI_INITIATOR_IQN 203
  180. /** Maximum normal DHCP option */
  181. #define DHCP_MAX_OPTION 254
  182. /** End of options
  183. *
  184. * This tag does not have a length field; it is always only a single
  185. * byte in length.
  186. */
  187. #define DHCP_END 255
  188. /** @} */
  189. /**
  190. * Count number of arguments to a variadic macro
  191. *
  192. * This rather neat, non-iterative solution is courtesy of Laurent
  193. * Deniau.
  194. *
  195. */
  196. #define _VA_ARG_COUNT( _1, _2, _3, _4, _5, _6, _7, _8, \
  197. _9, _10, _11, _12, _13, _14, _15, _16, \
  198. _17, _18, _19, _20, _21, _22, _23, _24, \
  199. _25, _26, _27, _28, _29, _30, _31, _32, \
  200. _33, _34, _35, _36, _37, _38, _39, _40, \
  201. _41, _42, _43, _44, _45, _46, _47, _48, \
  202. _49, _50, _51, _52, _53, _54, _55, _56, \
  203. _57, _58, _59, _60, _61, _62, _63, N, ... ) N
  204. #define VA_ARG_COUNT( ... ) \
  205. _VA_ARG_COUNT ( __VA_ARGS__, \
  206. 63, 62, 61, 60, 59, 58, 57, 56, \
  207. 55, 54, 53, 52, 51, 50, 49, 48, \
  208. 47, 46, 45, 44, 43, 42, 41, 40, \
  209. 39, 38, 37, 36, 35, 34, 33, 32, \
  210. 31, 30, 29, 28, 27, 26, 25, 24, \
  211. 23, 22, 21, 20, 19, 18, 17, 16, \
  212. 15, 14, 13, 12, 11, 10, 9, 8, \
  213. 7, 6, 5, 4, 3, 2, 1, 0 )
  214. /** Construct a DHCP option from a list of bytes */
  215. #define DHCP_OPTION( ... ) VA_ARG_COUNT ( __VA_ARGS__ ), __VA_ARGS__
  216. /** Construct a DHCP option from a list of characters */
  217. #define DHCP_STRING( ... ) DHCP_OPTION ( __VA_ARGS__ )
  218. /** Construct a byte-valued DHCP option */
  219. #define DHCP_BYTE( value ) DHCP_OPTION ( value )
  220. /** Construct a word-valued DHCP option */
  221. #define DHCP_WORD( value ) DHCP_OPTION ( ( ( (value) >> 8 ) & 0xff ), \
  222. ( ( (value) >> 0 ) & 0xff ) )
  223. /** Construct a dword-valued DHCP option */
  224. #define DHCP_DWORD( value ) DHCP_OPTION ( ( ( (value) >> 24 ) & 0xff ), \
  225. ( ( (value) >> 16 ) & 0xff ), \
  226. ( ( (value) >> 8 ) & 0xff ), \
  227. ( ( (value) >> 0 ) & 0xff ) )
  228. /** Construct a DHCP encapsulated options field */
  229. #define DHCP_ENCAP( ... ) DHCP_OPTION ( __VA_ARGS__, DHCP_END )
  230. /**
  231. * A DHCP option
  232. *
  233. * DHCP options consist of a mandatory tag, a length field that is
  234. * mandatory for all options except @c DHCP_PAD and @c DHCP_END, and a
  235. * payload.
  236. */
  237. struct dhcp_option {
  238. /** Tag
  239. *
  240. * Must be a @c DHCP_XXX value.
  241. */
  242. uint8_t tag;
  243. /** Length
  244. *
  245. * This is the length of the data field (i.e. excluding the
  246. * tag and length fields). For the two tags @c DHCP_PAD and
  247. * @c DHCP_END, the length field is implicitly zero and is
  248. * also missing, i.e. these DHCP options are only a single
  249. * byte in length.
  250. */
  251. uint8_t len;
  252. /** Option data
  253. *
  254. * Interpretation of the content is entirely dependent upon
  255. * the tag. For fields containing a multi-byte integer, the
  256. * field is defined to be in network-endian order (unless you
  257. * are Intel and feel like violating the spec for fun).
  258. */
  259. union {
  260. uint8_t byte;
  261. uint16_t word;
  262. uint32_t dword;
  263. struct in_addr in;
  264. uint8_t bytes[0];
  265. char string[0];
  266. } data;
  267. } __attribute__ (( packed ));
  268. /**
  269. * Length of a DHCP option header
  270. *
  271. * The header is the portion excluding the data, i.e. the tag and the
  272. * length.
  273. */
  274. #define DHCP_OPTION_HEADER_LEN ( offsetof ( struct dhcp_option, data ) )
  275. /** Maximum length for a single DHCP option */
  276. #define DHCP_MAX_LEN 0xff
  277. /** A DHCP options block */
  278. struct dhcp_option_block {
  279. /** Reference counter */
  280. struct refcnt refcnt;
  281. /** List of option blocks */
  282. struct list_head list;
  283. /** Option block raw data */
  284. void *data;
  285. /** Option block length */
  286. size_t len;
  287. /** Option block maximum length */
  288. size_t max_len;
  289. /** Block priority
  290. *
  291. * This is determined at the time of the call to
  292. * register_options() by searching for the @c DHCP_EB_PRIORITY
  293. * option.
  294. */
  295. signed int priority;
  296. };
  297. /**
  298. * A DHCP header
  299. *
  300. */
  301. struct dhcphdr {
  302. /** Operation
  303. *
  304. * This must be either @c BOOTP_REQUEST or @c BOOTP_REPLY.
  305. */
  306. uint8_t op;
  307. /** Hardware address type
  308. *
  309. * This is an ARPHRD_XXX constant. Note that ARPHRD_XXX
  310. * constants are nominally 16 bits wide; this could be
  311. * considered to be a bug in the BOOTP/DHCP specification.
  312. */
  313. uint8_t htype;
  314. /** Hardware address length */
  315. uint8_t hlen;
  316. /** Number of hops from server */
  317. uint8_t hops;
  318. /** Transaction ID */
  319. uint32_t xid;
  320. /** Seconds since start of acquisition */
  321. uint16_t secs;
  322. /** Flags */
  323. uint16_t flags;
  324. /** "Client" IP address
  325. *
  326. * This is filled in if the client already has an IP address
  327. * assigned and can respond to ARP requests.
  328. */
  329. struct in_addr ciaddr;
  330. /** "Your" IP address
  331. *
  332. * This is the IP address assigned by the server to the client.
  333. */
  334. struct in_addr yiaddr;
  335. /** "Server" IP address
  336. *
  337. * This is the IP address of the next server to be used in the
  338. * boot process.
  339. */
  340. struct in_addr siaddr;
  341. /** "Gateway" IP address
  342. *
  343. * This is the IP address of the DHCP relay agent, if any.
  344. */
  345. struct in_addr giaddr;
  346. /** Client hardware address */
  347. uint8_t chaddr[16];
  348. /** Server host name (null terminated)
  349. *
  350. * This field may be overridden and contain DHCP options
  351. */
  352. char sname[64];
  353. /** Boot file name (null terminated)
  354. *
  355. * This field may be overridden and contain DHCP options
  356. */
  357. char file[128];
  358. /** DHCP magic cookie
  359. *
  360. * Must have the value @c DHCP_MAGIC_COOKIE.
  361. */
  362. uint32_t magic;
  363. /** DHCP options
  364. *
  365. * Variable length; extends to the end of the packet. Minimum
  366. * length (for the sake of sanity) is 1, to allow for a single
  367. * @c DHCP_END tag.
  368. */
  369. uint8_t options[1];
  370. };
  371. /** Opcode for a request from client to server */
  372. #define BOOTP_REQUEST 1
  373. /** Opcode for a reply from server to client */
  374. #define BOOTP_REPLY 2
  375. /** BOOTP reply must be broadcast
  376. *
  377. * Clients that cannot accept unicast BOOTP replies must set this
  378. * flag.
  379. */
  380. #define BOOTP_FL_BROADCAST 0x8000
  381. /** DHCP magic cookie */
  382. #define DHCP_MAGIC_COOKIE 0x63825363UL
  383. /** DHCP minimum packet length
  384. *
  385. * This is the mandated minimum packet length that a DHCP participant
  386. * must be prepared to receive.
  387. */
  388. #define DHCP_MIN_LEN 552
  389. /**
  390. * A DHCP packet
  391. *
  392. */
  393. struct dhcp_packet {
  394. /** The DHCP packet contents */
  395. struct dhcphdr *dhcphdr;
  396. /** Maximum length of the DHCP packet buffer */
  397. size_t max_len;
  398. /** Used length of the DHCP packet buffer */
  399. size_t len;
  400. /** DHCP options */
  401. struct dhcp_option_block options;
  402. };
  403. /** A DHCP option applicator */
  404. struct dhcp_option_applicator {
  405. /** DHCP option tag */
  406. unsigned int tag;
  407. /** Applicator
  408. *
  409. * @v tag DHCP option tag
  410. * @v option DHCP option
  411. * @ret rc Return status code
  412. */
  413. int ( * apply ) ( unsigned int tag, struct dhcp_option *option );
  414. };
  415. /** Declare a DHCP option applicator */
  416. #define __dhcp_applicator \
  417. __table ( struct dhcp_option_applicator, dhcp_applicators, 01 )
  418. /**
  419. * Get reference to DHCP options block
  420. *
  421. * @v options DHCP options block
  422. * @ret options DHCP options block
  423. */
  424. static inline __attribute__ (( always_inline )) struct dhcp_option_block *
  425. dhcpopt_get ( struct dhcp_option_block *options ) {
  426. ref_get ( &options->refcnt );
  427. return options;
  428. }
  429. /**
  430. * Drop reference to DHCP options block
  431. *
  432. * @v options DHCP options block
  433. */
  434. static inline __attribute__ (( always_inline )) void
  435. dhcpopt_put ( struct dhcp_option_block *options ) {
  436. ref_put ( &options->refcnt );
  437. }
  438. extern unsigned long dhcp_num_option ( struct dhcp_option *option );
  439. extern void dhcp_ipv4_option ( struct dhcp_option *option,
  440. struct in_addr *inp );
  441. extern int dhcp_snprintf ( char *buf, size_t size,
  442. struct dhcp_option *option );
  443. extern struct dhcp_option *
  444. find_dhcp_option ( struct dhcp_option_block *options, unsigned int tag );
  445. extern void register_dhcp_options ( struct dhcp_option_block *options );
  446. extern void unregister_dhcp_options ( struct dhcp_option_block *options );
  447. extern void init_dhcp_options ( struct dhcp_option_block *options,
  448. void *data, size_t max_len );
  449. extern struct dhcp_option_block * __malloc alloc_dhcp_options ( size_t max_len );
  450. extern struct dhcp_option *
  451. set_dhcp_option ( struct dhcp_option_block *options, unsigned int tag,
  452. const void *data, size_t len );
  453. extern struct dhcp_option * find_global_dhcp_option ( unsigned int tag );
  454. extern unsigned long find_dhcp_num_option ( struct dhcp_option_block *options,
  455. unsigned int tag );
  456. extern unsigned long find_global_dhcp_num_option ( unsigned int tag );
  457. extern void find_dhcp_ipv4_option ( struct dhcp_option_block *options,
  458. unsigned int tag, struct in_addr *inp );
  459. extern void find_global_dhcp_ipv4_option ( unsigned int tag,
  460. struct in_addr *inp );
  461. extern void delete_dhcp_option ( struct dhcp_option_block *options,
  462. unsigned int tag );
  463. extern int apply_dhcp_options ( struct dhcp_option_block *options );
  464. extern int apply_global_dhcp_options ( void );
  465. extern int create_dhcp_request ( struct net_device *netdev, int msgtype,
  466. struct dhcp_option_block *options,
  467. void *data, size_t max_len,
  468. struct dhcp_packet *dhcppkt );
  469. extern int create_dhcp_response ( struct net_device *netdev, int msgtype,
  470. struct dhcp_option_block *options,
  471. void *data, size_t max_len,
  472. struct dhcp_packet *dhcppkt );
  473. extern int start_dhcp ( struct job_interface *job, struct net_device *netdev,
  474. int (*register_options) ( struct net_device *,
  475. struct dhcp_option_block * ));
  476. extern int dhcp_configure_netdev ( struct net_device *netdev,
  477. struct dhcp_option_block *options );
  478. #endif /* _GPXE_DHCP_H */