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 12KB

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