您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

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