dhcp4_srv.h 36 KB

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