dhcp4_srv.h 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841
  1. // Copyright (C) 2011-2016 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this
  5. // file, You can obtain one at http://mozilla.org/MPL/2.0/.
  6. #ifndef DHCPV4_SRV_H
  7. #define DHCPV4_SRV_H
  8. #include <dhcp/dhcp4.h>
  9. #include <dhcp/pkt4.h>
  10. #include <dhcp/option.h>
  11. #include <dhcp/option_string.h>
  12. #include <dhcp/option4_client_fqdn.h>
  13. #include <dhcp/option_custom.h>
  14. #include <dhcp_ddns/ncr_msg.h>
  15. #include <dhcpsrv/d2_client_mgr.h>
  16. #include <dhcpsrv/subnet.h>
  17. #include <dhcpsrv/alloc_engine.h>
  18. #include <dhcpsrv/cfg_option.h>
  19. #include <hooks/callout_handle.h>
  20. #include <dhcpsrv/daemon.h>
  21. #include <boost/noncopyable.hpp>
  22. #include <iostream>
  23. #include <queue>
  24. // Undefine the macro OPTIONAL which is defined in some operating
  25. // systems but conflicts with a member of the RequirementLevel enum in
  26. // the server class.
  27. #ifdef OPTIONAL
  28. #undef OPTIONAL
  29. #endif
  30. namespace isc {
  31. namespace dhcp {
  32. /// @brief DHCPv4 message exchange.
  33. ///
  34. /// This class represents the DHCPv4 message exchange. The message exchange
  35. /// consists of the single client message, server response to this message
  36. /// and the mechanisms to generate the server's response. The server creates
  37. /// the instance of the @c Dhcpv4Exchange for each inbound message that it
  38. /// accepts for processing.
  39. ///
  40. /// The use of the @c Dhcpv4Exchange object as a central repository of
  41. /// information about the message exchange simplifies the API of the
  42. /// @c Dhcpv4Srv class.
  43. ///
  44. /// Another benefit of using this class is that different methods of the
  45. /// @c Dhcpv4Srv may share information. For example, the constructor of this
  46. /// class selects the subnet and multiple methods of @c Dhcpv4Srv use this
  47. /// subnet, without the need to select it again.
  48. ///
  49. /// @todo This is the initial version of this class. In the future a lot of
  50. /// code from the @c Dhcpv4Srv class will be migrated here.
  51. class Dhcpv4Exchange {
  52. public:
  53. /// @brief Constructor.
  54. ///
  55. /// The constructor selects the subnet for the query and checks for the
  56. /// static host reservations for the client which has sent the message.
  57. /// The information about the reservations is stored in the
  58. /// @c AllocEngine::ClientContext4 object, which can be obtained by
  59. /// calling the @c getContext.
  60. ///
  61. /// @param alloc_engine Pointer to the instance of the Allocation Engine
  62. /// used by the server.
  63. /// @param query Pointer to the client message.
  64. /// @param subnet Pointer to the subnet to which the client belongs.
  65. Dhcpv4Exchange(const AllocEnginePtr& alloc_engine, const Pkt4Ptr& query,
  66. const Subnet4Ptr& subnet);
  67. /// @brief Initializes the instance of the response message.
  68. ///
  69. /// The type of the response depends on the type of the query message.
  70. /// For the DHCPDISCOVER the DHCPOFFER is created. For the DHCPREQUEST
  71. /// and DHCPINFORM the DHCPACK is created. For the DHCPRELEASE the
  72. /// response is not initialized.
  73. void initResponse();
  74. /// @brief Initializes the DHCPv6 part of the response message
  75. ///
  76. /// Called by initResponse() when the query is a DHCP4o6 message
  77. void initResponse4o6();
  78. /// @brief Returns the pointer to the query from the client.
  79. Pkt4Ptr getQuery() const {
  80. return (query_);
  81. }
  82. /// @brief Returns the pointer to the server's response.
  83. ///
  84. /// The returned pointer is NULL if the query type is DHCPRELEASE or DHCPDECLINE.
  85. Pkt4Ptr getResponse() const {
  86. return (resp_);
  87. }
  88. /// @brief Removes the response message by resetting the pointer to NULL.
  89. void deleteResponse() {
  90. resp_.reset();
  91. }
  92. /// @brief Returns the copy of the context for the Allocation engine.
  93. AllocEngine::ClientContext4Ptr getContext() const {
  94. return (context_);
  95. }
  96. /// @brief Returns the configured option list (non-const version)
  97. CfgOptionList& getCfgOptionList() {
  98. return (cfg_option_list_);
  99. }
  100. /// @brief Returns the configured option list (const version)
  101. const CfgOptionList& getCfgOptionList() const {
  102. return (cfg_option_list_);
  103. }
  104. private:
  105. /// @brief Copies default parameters from client's to server's message
  106. ///
  107. /// Some fields are copied from client's message into server's response,
  108. /// e.g. client HW address, number of hops, transaction-id etc.
  109. ///
  110. /// @warning This message is called internally by @c initResponse and
  111. /// thus it doesn't check if the resp_ value has been initialized. The
  112. /// calling method is responsible for making sure that @c resp_ is
  113. /// not NULL.
  114. void copyDefaultFields();
  115. /// @brief Copies default options from client's to server's message
  116. ///
  117. /// Some options are copied from client's message into server's response,
  118. /// e.g. Relay Agent Info option, Subnet Selection option etc.
  119. ///
  120. /// @warning This message is called internally by @c initResponse and
  121. /// thus it doesn't check if the resp_ value has been initialized. The
  122. /// calling method is responsible for making sure that @c resp_ is
  123. /// not NULL.
  124. void copyDefaultOptions();
  125. /// @brief Set host identifiers within a context.
  126. ///
  127. /// This method sets an ordered list of host identifier types and
  128. /// values which the server should use to find host reservations.
  129. /// The order of the set is determined by the configuration parameter,
  130. /// host-reservation-identifiers
  131. void setHostIdentifiers();
  132. /// @brief Sets reserved values of siaddr, sname and file in the
  133. /// server's response.
  134. void setReservedMessageFields();
  135. /// @brief Pointer to the allocation engine used by the server.
  136. AllocEnginePtr alloc_engine_;
  137. /// @brief Pointer to the DHCPv4 message sent by the client.
  138. Pkt4Ptr query_;
  139. /// @brief Pointer to the DHCPv4 message to be sent to the client.
  140. Pkt4Ptr resp_;
  141. /// @brief Context for use with allocation engine.
  142. AllocEngine::ClientContext4Ptr context_;
  143. /// @brief Configured option list.
  144. /// @note The configured option list is an *ordered* list of
  145. /// @c CfgOption objects used to append options to the response.
  146. CfgOptionList cfg_option_list_;
  147. };
  148. /// @brief Type representing the pointer to the @c Dhcpv4Exchange.
  149. typedef boost::shared_ptr<Dhcpv4Exchange> Dhcpv4ExchangePtr;
  150. /// @brief DHCPv4 server service.
  151. ///
  152. /// This singleton class represents DHCPv4 server. It contains all
  153. /// top-level methods and routines necessary for server operation.
  154. /// In particular, it instantiates IfaceMgr, loads or generates DUID
  155. /// that is going to be used as server-identifier, receives incoming
  156. /// packets, processes them, manages leases assignment and generates
  157. /// appropriate responses.
  158. ///
  159. /// This class does not support any controlling mechanisms directly.
  160. /// See the derived \ref ControlledDhcpv4Srv class for support for
  161. /// command and configuration updates over msgq.
  162. class Dhcpv4Srv : public Daemon {
  163. public:
  164. /// @brief defines if certain option may, must or must not appear
  165. typedef enum {
  166. FORBIDDEN,
  167. MANDATORY,
  168. OPTIONAL
  169. } RequirementLevel;
  170. /// @brief Default constructor.
  171. ///
  172. /// Instantiates necessary services, required to run DHCPv4 server.
  173. /// In particular, creates IfaceMgr that will be responsible for
  174. /// network interaction. Will instantiate lease manager, and load
  175. /// old or create new DUID. It is possible to specify alternate
  176. /// port on which DHCPv4 server will listen on. That is mostly useful
  177. /// for testing purposes. The Last two arguments of the constructor
  178. /// should be left at default values for normal server operation.
  179. /// They should be set to 'false' when creating an instance of this
  180. /// class for unit testing because features they enable require
  181. /// root privileges.
  182. ///
  183. /// @param port specifies port number to listen on
  184. /// @param use_bcast configure sockets to support broadcast messages.
  185. /// @param direct_response_desired specifies if it is desired to
  186. /// use direct V4 traffic.
  187. Dhcpv4Srv(uint16_t port = DHCP4_SERVER_PORT,
  188. const bool use_bcast = true,
  189. const bool direct_response_desired = true);
  190. /// @brief Destructor. Used during DHCPv4 service shutdown.
  191. virtual ~Dhcpv4Srv();
  192. /// @brief returns Kea version on stdout and exit.
  193. /// redeclaration/redefinition. @ref Daemon::getVersion()
  194. static std::string getVersion(bool extended);
  195. /// @brief Main server processing loop.
  196. ///
  197. /// Main server processing loop. Call the processing step routine
  198. /// until shut down.
  199. ///
  200. /// @return true, if being shut down gracefully, never fail.
  201. bool run();
  202. /// @brief Main server processing step.
  203. ///
  204. /// Main server processing step. Receives one incoming packet, calls
  205. /// the processing packet routing and (if necessary) transmits
  206. /// a response.
  207. void run_one();
  208. /// @brief Process a single incoming DHCPv4 packet.
  209. ///
  210. /// It verifies correctness of the passed packet, call per-type processXXX
  211. /// methods, generates appropriate answer.
  212. ///
  213. /// @param query A pointer to the packet to be processed.
  214. /// @param rsp A pointer to the response
  215. void processPacket(Pkt4Ptr& query, Pkt4Ptr& rsp);
  216. /// @brief Instructs the server to shut down.
  217. void shutdown();
  218. ///
  219. /// @name Public accessors returning values required to (re)open sockets.
  220. ///
  221. //@{
  222. ///
  223. /// @brief Get UDP port on which server should listen.
  224. ///
  225. /// Typically, server listens on UDP port number 67. Other ports are used
  226. /// for testing purposes only.
  227. ///
  228. /// @return UDP port on which server should listen.
  229. uint16_t getPort() const {
  230. return (port_);
  231. }
  232. /// @brief Return bool value indicating that broadcast flags should be set
  233. /// on sockets.
  234. ///
  235. /// @return A bool value indicating that broadcast should be used (if true).
  236. bool useBroadcast() const {
  237. return (use_bcast_);
  238. }
  239. //@}
  240. /// @brief Starts DHCP_DDNS client IO if DDNS updates are enabled.
  241. ///
  242. /// If updates are enabled, it instructs the D2ClientMgr singleton to
  243. /// enter send mode. If D2ClientMgr encounters errors it may throw
  244. /// D2ClientError. This method does not catch exceptions.
  245. void startD2();
  246. /// @brief Stops DHCP_DDNS client IO if DDNS updates are enabled.
  247. ///
  248. /// If updates are enabled, it instructs the D2ClientMgr singleton to
  249. /// leave send mode. If D2ClientMgr encounters errors it may throw
  250. /// D2ClientError. This method does not catch exceptions.
  251. void stopD2();
  252. /// @brief Implements the error handler for DHCP_DDNS IO errors
  253. ///
  254. /// Invoked when a NameChangeRequest send to kea-dhcp-ddns completes with
  255. /// a failed status. These are communications errors, not data related
  256. /// failures.
  257. ///
  258. /// This method logs the failure and then suspends all further updates.
  259. /// Updating can only be restored by reconfiguration or restarting the
  260. /// server. There is currently no retry logic so the first IO error that
  261. /// occurs will suspend updates.
  262. /// @todo We may wish to make this more robust or sophisticated.
  263. ///
  264. /// @param result Result code of the send operation.
  265. /// @param ncr NameChangeRequest which failed to send.
  266. virtual void d2ClientErrorHandler(const dhcp_ddns::
  267. NameChangeSender::Result result,
  268. dhcp_ddns::NameChangeRequestPtr& ncr);
  269. protected:
  270. /// @name Functions filtering and sanity-checking received messages.
  271. ///
  272. /// @todo These functions are supposed to be moved to a new class which
  273. /// will manage different rules for accepting and rejecting messages.
  274. /// Perhaps ticket #3116 is a good opportunity to do it.
  275. ///
  276. //@{
  277. /// @brief Checks whether received message should be processed or discarded.
  278. ///
  279. /// This function checks whether received message should be processed or
  280. /// discarded. It should be called on the beginning of message processing
  281. /// (just after the message has been decoded). This message calls a number
  282. /// of other functions which check whether message should be processed,
  283. /// using different criteria.
  284. ///
  285. /// This function should be extended when new criteria for accepting
  286. /// received message have to be implemented. This function is meant to
  287. /// aggregate all early filtering checks on the received message. By having
  288. /// a single function like this, we are avoiding bloat of the server's main
  289. /// loop.
  290. ///
  291. /// @warning This function should remain exception safe.
  292. ///
  293. /// @param query Received message.
  294. ///
  295. /// @return true if the message should be further processed, or false if
  296. /// the message should be discarded.
  297. bool accept(const Pkt4Ptr& query) const;
  298. /// @brief Check if a message sent by directly connected client should be
  299. /// accepted or discarded.
  300. ///
  301. /// This function checks if the received message is from directly connected
  302. /// client. If it is, it checks that it should be processed or discarded.
  303. ///
  304. /// Note that this function doesn't validate all addresses being carried in
  305. /// the message. The primary purpose of this function is to filter out
  306. /// direct messages in the local network for which there is no suitable
  307. /// subnet configured. For example, this function accepts unicast messages
  308. /// because unicasts may be used by clients located in remote networks to
  309. /// to renew existing leases. If their notion of address is wrong, the
  310. /// server will have to sent a NAK, instead of dropping the message.
  311. /// Detailed validation of such messages is performed at later stage of
  312. /// processing.
  313. ///
  314. /// This function accepts the following messages:
  315. /// - all valid relayed messages,
  316. /// - all unicast messages,
  317. /// - all broadcast messages except DHCPINFORM received on the interface
  318. /// for which the suitable subnet exists (is configured).
  319. /// - all DHCPINFORM messages with source address or ciaddr set.
  320. ///
  321. /// @param query Message sent by a client.
  322. ///
  323. /// @return true if message is accepted for further processing, false
  324. /// otherwise.
  325. bool acceptDirectRequest(const Pkt4Ptr& query) const;
  326. /// @brief Check if received message type is valid for the server to
  327. /// process.
  328. ///
  329. /// This function checks that the received message type belongs to
  330. /// the range of types recognized by the server and that the
  331. /// message of this type should be processed by the server.
  332. ///
  333. /// The messages types accepted for processing are:
  334. /// - Discover
  335. /// - Request
  336. /// - Release
  337. /// - Decline
  338. /// - Inform
  339. ///
  340. /// @param query Message sent by a client.
  341. ///
  342. /// @return true if message is accepted for further processing, false
  343. /// otherwise.
  344. bool acceptMessageType(const Pkt4Ptr& query) const;
  345. /// @brief Verifies if the server id belongs to our server.
  346. ///
  347. /// This function checks if the server identifier carried in the specified
  348. /// DHCPv4 message belongs to this server. If the server identifier option
  349. /// is absent or the value carried by this option is equal to one of the
  350. /// server identifiers used by the server, the true is returned. If the
  351. /// server identifier option is present, but it doesn't match any server
  352. /// identifier used by this server, the false value is returned.
  353. ///
  354. /// @param pkt DHCPv4 message which server identifier is to be checked.
  355. ///
  356. /// @return true, if the server identifier is absent or matches one of the
  357. /// server identifiers that the server is using; false otherwise.
  358. bool acceptServerId(const Pkt4Ptr& pkt) const;
  359. //@}
  360. /// @brief Verifies if specified packet meets RFC requirements
  361. ///
  362. /// Checks if mandatory option is really there, that forbidden option
  363. /// is not there, and that client-id or server-id appears only once.
  364. ///
  365. /// @param query Pointer to the client's message.
  366. /// @param serverid expectation regarding server-id option
  367. /// @throw RFCViolation if any issues are detected
  368. static void sanityCheck(const Pkt4Ptr& query, RequirementLevel serverid);
  369. /// @brief Processes incoming DISCOVER and returns response.
  370. ///
  371. /// Processes received DISCOVER message and verifies that its sender
  372. /// should be served. In particular, a lease is selected and sent
  373. /// as an offer to a client if it should be served.
  374. ///
  375. /// @param discover DISCOVER message received from client
  376. ///
  377. /// @return OFFER message or NULL
  378. Pkt4Ptr processDiscover(Pkt4Ptr& discover);
  379. /// @brief Processes incoming REQUEST and returns REPLY response.
  380. ///
  381. /// Processes incoming REQUEST message and verifies that its sender
  382. /// should be served. In particular, verifies that requested lease
  383. /// is valid, not expired, not reserved, not used by other client and
  384. /// that requesting client is allowed to use it.
  385. ///
  386. /// Returns ACK message, NAK message, or NULL
  387. ///
  388. /// @param request a message received from client
  389. ///
  390. /// @return ACK or NAK message
  391. Pkt4Ptr processRequest(Pkt4Ptr& request);
  392. /// @brief Processes incoming DHCPRELEASE messages.
  393. ///
  394. /// In DHCPv4, server does not respond to RELEASE messages, therefore
  395. /// this function does not return anything.
  396. ///
  397. /// @param release message received from client
  398. void processRelease(Pkt4Ptr& release);
  399. /// @brief Process incoming DHCPDECLINE messages.
  400. ///
  401. /// This method processes incoming DHCPDECLINE. In particular, it extracts
  402. /// Requested IP Address option, checks that the address really belongs to
  403. /// the client and if it does, calls @ref declineLease.
  404. ///
  405. /// @param decline message received from client
  406. void processDecline(Pkt4Ptr& decline);
  407. /// @brief Processes incoming DHCPINFORM messages.
  408. ///
  409. /// @param inform message received from client
  410. ///
  411. /// @return DHCPACK to be sent to the client.
  412. Pkt4Ptr processInform(Pkt4Ptr& inform);
  413. /// @brief Build the configured option list
  414. ///
  415. /// @note The configured option list is an *ordered* list of
  416. /// @c CfgOption objects used to append options to the response.
  417. ///
  418. /// @param ex The exchange where the configured option list is cached
  419. void buildCfgOptionList(Dhcpv4Exchange& ex);
  420. /// @brief Appends options requested by client.
  421. ///
  422. /// This method assigns options that were requested by client
  423. /// (sent in PRL) or are enforced by server.
  424. ///
  425. /// @param ex The exchange holding both the client's message and the
  426. /// server's response.
  427. void appendRequestedOptions(Dhcpv4Exchange& ex);
  428. /// @brief Appends requested vendor options as requested by client.
  429. ///
  430. /// This method is similar to \ref appendRequestedOptions(), but uses
  431. /// vendor options. The major difference is that vendor-options use
  432. /// its own option spaces (there may be more than one distinct set of vendor
  433. /// options, each with unique vendor-id). Vendor options are requested
  434. /// using separate options within their respective vendor-option spaces.
  435. ///
  436. /// @param ex The exchange holding both the client's message and the
  437. /// server's response.
  438. void appendRequestedVendorOptions(Dhcpv4Exchange& ex);
  439. /// @brief Assigns a lease and appends corresponding options
  440. ///
  441. /// This method chooses the most appropriate lease for requesting
  442. /// client and assigning it. Options corresponding to the lease
  443. /// are added to specific message.
  444. ///
  445. /// This method may reset the pointer to the response in the @c ex object
  446. /// to indicate that the response should not be sent to the client.
  447. /// The caller must check if the response is is null after calling
  448. /// this method.
  449. ///
  450. /// The response type in the @c ex object may be set to DHCPACK or DHCPNAK.
  451. ///
  452. /// @param ex DHCPv4 exchange holding the client's message to be checked.
  453. void assignLease(Dhcpv4Exchange& ex);
  454. /// @brief Append basic options if they are not present.
  455. ///
  456. /// This function adds the following basic options if they
  457. /// are not yet added to the response message:
  458. /// - Subnet Mask,
  459. /// - Router,
  460. /// - Name Server,
  461. /// - Domain Name.
  462. ///
  463. /// @param ex DHCPv4 exchange holding the client's message to be checked.
  464. void appendBasicOptions(Dhcpv4Exchange& ex);
  465. /// @brief Processes Client FQDN and Hostname Options sent by a client.
  466. ///
  467. /// Client may send Client FQDN or Hostname option to communicate its name
  468. /// to the server. Server may use this name to perform DNS update for the
  469. /// lease being assigned to a client. If server takes responsibility for
  470. /// updating DNS for a client it may communicate it by sending the Client
  471. /// FQDN or Hostname %Option back to the client. Server select a different
  472. /// name than requested by a client to update DNS. In such case, the server
  473. /// stores this different name in its response.
  474. ///
  475. /// Client should not send both Client FQDN and Hostname options. However,
  476. /// if client sends both options, server should prefer Client FQDN option
  477. /// and ignore the Hostname option. If Client FQDN option is not present,
  478. /// the Hostname option is processed.
  479. ///
  480. /// The Client FQDN %Option is processed by this function as described in
  481. /// RFC4702.
  482. ///
  483. /// In response to a Hostname %Option sent by a client, the server may send
  484. /// Hostname option with the same or different hostname. If different
  485. /// hostname is sent, it is an indication to the client that server has
  486. /// overridden the client's preferred name and will rather use this
  487. /// different name to update DNS. However, since Hostname option doesn't
  488. /// carry an information whether DNS update will be carried by the server
  489. /// or not, the client is responsible for checking whether DNS update
  490. /// has been performed.
  491. ///
  492. /// After successful processing options stored in the first parameter,
  493. /// this function may add Client FQDN or Hostname option to the response
  494. /// message. In some cases, server may cease to add any options to the
  495. /// response, i.e. when server doesn't support DNS updates.
  496. ///
  497. /// This function does not throw. It simply logs the debug message if the
  498. /// processing of the FQDN or Hostname failed.
  499. ///
  500. /// @param ex The exchange holding both the client's message and the
  501. /// server's response.
  502. void processClientName(Dhcpv4Exchange& ex);
  503. /// @brief this is a prefix added to the contend of vendor-class option
  504. ///
  505. /// If incoming packet has a vendor class option, its content is
  506. /// prepended with this prefix and then interpreted as a class.
  507. /// For example, a packet that sends vendor class with value of "FOO"
  508. /// will cause the packet to be assigned to class VENDOR_CLASS_FOO.
  509. static const std::string VENDOR_CLASS_PREFIX;
  510. private:
  511. /// @brief Process Client FQDN %Option sent by a client.
  512. ///
  513. /// This function is called by the @c Dhcpv4Srv::processClientName when
  514. /// the client has sent the FQDN option in its message to the server.
  515. /// It comprises the actual logic to parse the FQDN option and prepare
  516. /// the FQDN option to be sent back to the client in the server's
  517. /// response.
  518. ///
  519. /// @param ex The exchange holding both the client's message and the
  520. /// server's response.
  521. void processClientFqdnOption(Dhcpv4Exchange& ex);
  522. /// @brief Process Hostname %Option sent by a client.
  523. ///
  524. /// This function is called by the @c Dhcpv4Srv::processClientName when
  525. /// the client has sent the Hostname option in its message to the server.
  526. /// It comprises the actual logic to parse the Hostname option and
  527. /// prepare the Hostname option to be sent back to the client in the
  528. /// server's response.
  529. ///
  530. /// @param ex The exchange holding both the client's message and the
  531. /// server's response.
  532. void processHostnameOption(Dhcpv4Exchange& ex);
  533. /// @public
  534. /// @brief Marks lease as declined.
  535. ///
  536. /// This method moves a lease to declined state with all the steps involved:
  537. /// - trigger DNS removal (if necessary)
  538. /// - disassociate the client information
  539. /// - update lease in the database (switch to DECLINED state)
  540. /// - increase necessary statistics
  541. /// - call lease4_decline hook
  542. ///
  543. /// @param lease lease to be declined
  544. /// @param decline client's message
  545. void declineLease(const Lease4Ptr& lease, const Pkt4Ptr& decline);
  546. protected:
  547. /// @brief Creates NameChangeRequests which correspond to the lease
  548. /// which has been acquired.
  549. ///
  550. /// If this function is called when an existing lease is renewed, it
  551. /// may generate NameChangeRequest to remove existing DNS entries which
  552. /// correspond to the old lease instance. This function may cease to
  553. /// generate NameChangeRequests if the notion of the client's FQDN hasn't
  554. /// changed between an old and new lease.
  555. ///
  556. /// @param lease A pointer to the new lease which has been acquired.
  557. /// @param old_lease A pointer to the instance of the old lease which has
  558. /// been replaced by the new lease passed in the first argument. The NULL
  559. /// value indicates that the new lease has been allocated, rather than
  560. /// lease being renewed.
  561. void createNameChangeRequests(const Lease4Ptr& lease,
  562. const Lease4Ptr& old_lease);
  563. /// @brief Attempts to renew received addresses
  564. ///
  565. /// Attempts to renew existing lease. This typically includes finding a lease that
  566. /// corresponds to the received address. If no such lease is found, a status code
  567. /// response is generated.
  568. ///
  569. /// @param renew client's message asking for renew
  570. /// @param reply server's response (ACK or NAK)
  571. void renewLease(const Pkt4Ptr& renew, Pkt4Ptr& reply);
  572. /// @brief Adds server identifier option to the server's response.
  573. ///
  574. /// This method adds a server identifier to the DHCPv4 message. It expects
  575. /// that the local (source) address is set for this message. If address is
  576. /// not set, it will throw an exception. This method also expects that the
  577. /// server identifier option is not present in the specified message.
  578. /// Otherwise, it will throw an exception on attempt to add a duplicate
  579. /// server identifier option.
  580. ///
  581. /// @note This method doesn't throw exceptions by itself but the underlying
  582. /// classes being used my throw. The reason for this method to not sanity
  583. /// check the specified message is that it is meant to be called internally
  584. /// by the @c Dhcpv4Srv class.
  585. ///
  586. /// @note This method is static because it is not dependent on the class
  587. /// state.
  588. ///
  589. /// @param ex The exchange holding both the client's message and the
  590. /// server's response.
  591. static void appendServerID(Dhcpv4Exchange& ex);
  592. /// @brief Set IP/UDP and interface parameters for the DHCPv4 response.
  593. ///
  594. /// This method sets the following parameters for the DHCPv4 message being
  595. /// sent to a client:
  596. /// - client unicast or a broadcast address,
  597. /// - client or relay port,
  598. /// - server address,
  599. /// - server port,
  600. /// - name and index of the interface which is to be used to send the
  601. /// message.
  602. ///
  603. /// Internally it calls the @c Dhcpv4Srv::adjustRemoteAddr to figure
  604. /// out the destination address (client unicast address or broadcast
  605. /// address).
  606. ///
  607. /// The destination port is always DHCPv4 client (68) or relay (67) port,
  608. /// depending if the response will be sent directly to a client.
  609. ///
  610. /// The source port is always set to DHCPv4 server port (67).
  611. ///
  612. /// The interface selected for the response is always the same as the
  613. /// one through which the query has been received.
  614. ///
  615. /// The source address for the response is the IPv4 address assigned to
  616. /// the interface being used to send the response. This function uses
  617. /// @c IfaceMgr to get the socket bound to the IPv4 address on the
  618. /// particular interface.
  619. ///
  620. /// @note This method is static because it is not dependent on the class
  621. /// state.
  622. ///
  623. /// @param ex The exchange holding both the client's message and the
  624. /// server's response.
  625. static void adjustIfaceData(Dhcpv4Exchange& ex);
  626. /// @brief Sets remote addresses for outgoing packet.
  627. ///
  628. /// This method sets the local and remote addresses on outgoing packet.
  629. /// The addresses being set depend on the following conditions:
  630. /// - has incoming packet been relayed,
  631. /// - is direct response to a client without address supported,
  632. /// - type of the outgoing packet,
  633. /// - broadcast flag set in the incoming packet.
  634. ///
  635. /// @warning This method does not check whether provided packet pointers
  636. /// are valid. Make sure that pointers are correct before calling this
  637. /// function.
  638. ///
  639. /// @note This method is static because it is not dependent on the class
  640. /// state.
  641. ///
  642. /// @param ex The exchange holding both the client's message and the
  643. /// server's response.
  644. static void adjustRemoteAddr(Dhcpv4Exchange& ex);
  645. /// @brief converts server-id to text
  646. /// Converts content of server-id option to a text representation, e.g.
  647. /// "192.0.2.1"
  648. ///
  649. /// @param opt option that contains server-id
  650. /// @return string representation
  651. static std::string srvidToString(const OptionPtr& opt);
  652. /// @brief Selects a subnet for a given client's packet.
  653. ///
  654. /// @param query client's message
  655. /// @return selected subnet (or NULL if no suitable subnet was found)
  656. isc::dhcp::Subnet4Ptr selectSubnet(const Pkt4Ptr& query) const;
  657. /// @brief Selects a subnet for a given client's DHCP4o6 packet.
  658. ///
  659. /// @param query client's message
  660. /// @return selected subnet (or NULL if no suitable subnet was found)
  661. isc::dhcp::Subnet4Ptr selectSubnet4o6(const Pkt4Ptr& query) const;
  662. /// indicates if shutdown is in progress. Setting it to true will
  663. /// initiate server shutdown procedure.
  664. volatile bool shutdown_;
  665. /// @brief dummy wrapper around IfaceMgr::receive4
  666. ///
  667. /// This method is useful for testing purposes, where its replacement
  668. /// simulates reception of a packet. For that purpose it is protected.
  669. virtual Pkt4Ptr receivePacket(int timeout);
  670. /// @brief dummy wrapper around IfaceMgr::send()
  671. ///
  672. /// This method is useful for testing purposes, where its replacement
  673. /// simulates transmission of a packet. For that purpose it is protected.
  674. virtual void sendPacket(const Pkt4Ptr& pkt);
  675. /// @brief Assigns incoming packet to zero or more classes.
  676. ///
  677. /// @note This is done in two phases: first the content of the
  678. /// vendor-class-identifier option is used as a class, by
  679. /// calling @ref classifyByVendor(). Second classification match
  680. /// expressions are evaluated. The resulting classes will be stored
  681. /// in the packet (see @ref isc::dhcp::Pkt4::classes_ and
  682. /// @ref isc::dhcp::Pkt4::inClass).
  683. ///
  684. /// @param pkt packet to be classified
  685. void classifyPacket(const Pkt4Ptr& pkt);
  686. /// @brief Performs packet processing specific to a vendor class
  687. ///
  688. /// If the selected subnet, query or response in the @c ex object is NULL
  689. /// this method returns immediately and returns true.
  690. ///
  691. /// @note This processing is a likely candidate to be pushed into hooks.
  692. ///
  693. /// @param ex The exchange holding both the client's message and the
  694. /// server's response.
  695. /// @return true if successful, false otherwise (will prevent sending response)
  696. bool vendorClassSpecificProcessing(const Dhcpv4Exchange& ex);
  697. /// @brief Allocation Engine.
  698. /// Pointer to the allocation engine that we are currently using
  699. /// It must be a pointer, because we will support changing engines
  700. /// during normal operation (e.g. to use different allocators)
  701. boost::shared_ptr<AllocEngine> alloc_engine_;
  702. private:
  703. /// @public
  704. /// @brief Assign class using vendor-class-identifier option
  705. ///
  706. /// @note This is the first part of @ref classifyPacket
  707. ///
  708. /// @param pkt packet to be classified
  709. /// @param classes a reference to added class names for logging
  710. void classifyByVendor(const Pkt4Ptr& pkt, std::string& classes);
  711. /// @private
  712. /// @brief Constructs netmask option based on subnet4
  713. /// @param subnet subnet for which the netmask will be calculated
  714. ///
  715. /// @return Option that contains netmask information
  716. static OptionPtr getNetmaskOption(const Subnet4Ptr& subnet);
  717. uint16_t port_; ///< UDP port number on which server listens.
  718. bool use_bcast_; ///< Should broadcast be enabled on sockets (if true).
  719. public:
  720. /// Class methods for DHCPv4-over-DHCPv6 handler
  721. /// @brief Updates statistics for received packets
  722. /// @param query packet received
  723. static void processStatsReceived(const Pkt4Ptr& query);
  724. /// @brief Updates statistics for transmitted packets
  725. /// @param response packet transmitted
  726. static void processStatsSent(const Pkt4Ptr& response);
  727. /// @brief Returns the index for "buffer4_receive" hook point
  728. /// @return the index for "buffer4_receive" hook point
  729. static int getHookIndexBuffer4Receive();
  730. /// @brief Returns the index for "pkt4_receive" hook point
  731. /// @return the index for "pkt4_receive" hook point
  732. static int getHookIndexPkt4Receive();
  733. /// @brief Returns the index for "subnet4_select" hook point
  734. /// @return the index for "subnet4_select" hook point
  735. static int getHookIndexSubnet4Select();
  736. /// @brief Returns the index for "lease4_release" hook point
  737. /// @return the index for "lease4_release" hook point
  738. static int getHookIndexLease4Release();
  739. /// @brief Returns the index for "pkt4_send" hook point
  740. /// @return the index for "pkt4_send" hook point
  741. static int getHookIndexPkt4Send();
  742. /// @brief Returns the index for "buffer4_send" hook point
  743. /// @return the index for "buffer4_send" hook point
  744. static int getHookIndexBuffer4Send();
  745. /// @brief Returns the index for "lease4_decline" hook point
  746. /// @return the index for "lease4_decline" hook point
  747. static int getHookIndexLease4Decline();
  748. };
  749. }; // namespace isc::dhcp
  750. }; // namespace isc
  751. #endif // DHCP4_SRV_H