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.

dhcp.h 14KB

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