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

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