dhcp4_srv.h 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717
  1. // Copyright (C) 2011-2013 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // Permission to use, copy, modify, and/or distribute this software for any
  4. // purpose with or without fee is hereby granted, provided that the above
  5. // copyright notice and this permission notice appear in all copies.
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  8. // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  9. // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  10. // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  11. // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  12. // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  13. // PERFORMANCE OF THIS SOFTWARE.
  14. #ifndef DHCPV4_SRV_H
  15. #define DHCPV4_SRV_H
  16. #include <dhcp/dhcp4.h>
  17. #include <dhcp/pkt4.h>
  18. #include <dhcp/option.h>
  19. #include <dhcp/option_string.h>
  20. #include <dhcp/option4_client_fqdn.h>
  21. #include <dhcp/option_custom.h>
  22. #include <dhcp_ddns/ncr_msg.h>
  23. #include <dhcpsrv/d2_client_mgr.h>
  24. #include <dhcpsrv/subnet.h>
  25. #include <dhcpsrv/alloc_engine.h>
  26. #include <hooks/callout_handle.h>
  27. #include <boost/noncopyable.hpp>
  28. #include <iostream>
  29. #include <queue>
  30. namespace isc {
  31. namespace dhcp {
  32. /// @brief Exception thrown when DHCID computation failed.
  33. class DhcidComputeError : public isc::Exception {
  34. public:
  35. DhcidComputeError(const char* file, size_t line, const char* what) :
  36. isc::Exception(file, line, what) { };
  37. };
  38. /// @brief DHCPv4 server service.
  39. ///
  40. /// This singleton class represents DHCPv4 server. It contains all
  41. /// top-level methods and routines necessary for server operation.
  42. /// In particular, it instantiates IfaceMgr, loads or generates DUID
  43. /// that is going to be used as server-identifier, receives incoming
  44. /// packets, processes them, manages leases assignment and generates
  45. /// appropriate responses.
  46. ///
  47. /// This class does not support any controlling mechanisms directly.
  48. /// See the derived \ref ControlledDhcpv4Srv class for support for
  49. /// command and configuration updates over msgq.
  50. ///
  51. /// For detailed explanation or relations between main(), ControlledDhcpv4Srv,
  52. /// Dhcpv4Srv and other classes, see \ref dhcpv4Session.
  53. class Dhcpv4Srv : public boost::noncopyable {
  54. public:
  55. /// @brief defines if certain option may, must or must not appear
  56. typedef enum {
  57. FORBIDDEN,
  58. MANDATORY,
  59. OPTIONAL
  60. } RequirementLevel;
  61. /// @brief Default constructor.
  62. ///
  63. /// Instantiates necessary services, required to run DHCPv4 server.
  64. /// In particular, creates IfaceMgr that will be responsible for
  65. /// network interaction. Will instantiate lease manager, and load
  66. /// old or create new DUID. It is possible to specify alternate
  67. /// port on which DHCPv4 server will listen on. That is mostly useful
  68. /// for testing purposes. The Last two arguments of the constructor
  69. /// should be left at default values for normal server operation.
  70. /// They should be set to 'false' when creating an instance of this
  71. /// class for unit testing because features they enable require
  72. /// root privileges.
  73. ///
  74. /// @param port specifies port number to listen on
  75. /// @param dbconfig Lease manager configuration string. The default
  76. /// of the "memfile" manager is used for testing.
  77. /// @param use_bcast configure sockets to support broadcast messages.
  78. /// @param direct_response_desired specifies if it is desired to
  79. /// use direct V4 traffic.
  80. Dhcpv4Srv(uint16_t port = DHCP4_SERVER_PORT,
  81. const char* dbconfig = "type=memfile",
  82. const bool use_bcast = true,
  83. const bool direct_response_desired = true);
  84. /// @brief Destructor. Used during DHCPv4 service shutdown.
  85. virtual ~Dhcpv4Srv();
  86. /// @brief Main server processing loop.
  87. ///
  88. /// Main server processing loop. Receives incoming packets, verifies
  89. /// their correctness, generates appropriate answer (if needed) and
  90. /// transmits respones.
  91. ///
  92. /// @return true, if being shut down gracefully, fail if experienced
  93. /// critical error.
  94. bool run();
  95. /// @brief Instructs the server to shut down.
  96. void shutdown();
  97. /// @brief Return textual type of packet received by server
  98. ///
  99. /// Returns the name of valid packet received by the server (e.g. DISCOVER).
  100. /// If the packet is unknown - or if it is a valid DHCP packet but not one
  101. /// expected to be received by the server (such as an OFFER), the string
  102. /// "UNKNOWN" is returned. This method is used in debug messages.
  103. ///
  104. /// As the operation of the method does not depend on any server state, it
  105. /// is declared static.
  106. ///
  107. /// @todo: This should be named static Pkt4::getName()
  108. ///
  109. /// @param type DHCPv4 packet type
  110. ///
  111. /// @return Pointer to "const" string containing the packet name.
  112. /// Note that this string is statically allocated and MUST NOT
  113. /// be freed by the caller.
  114. static const char* serverReceivedPacketName(uint8_t type);
  115. ///
  116. /// @name Public accessors returning values required to (re)open sockets.
  117. ///
  118. /// These accessors must be public because sockets are reopened from the
  119. /// static configuration callback handler. This callback handler invokes
  120. /// @c ControlledDhcpv4Srv::openActiveSockets which requires parameters
  121. /// which has to be retrieved from the @c ControlledDhcpv4Srv object.
  122. /// They are retrieved using these public functions
  123. //@{
  124. ///
  125. /// @brief Get UDP port on which server should listen.
  126. ///
  127. /// Typically, server listens on UDP port number 67. Other ports are used
  128. /// for testing purposes only.
  129. ///
  130. /// @return UDP port on which server should listen.
  131. uint16_t getPort() const {
  132. return (port_);
  133. }
  134. /// @brief Return bool value indicating that broadcast flags should be set
  135. /// on sockets.
  136. ///
  137. /// @return A bool value indicating that broadcast should be used (if true).
  138. bool useBroadcast() const {
  139. return (use_bcast_);
  140. }
  141. //@}
  142. /// @brief Open sockets which are marked as active in @c CfgMgr.
  143. ///
  144. /// This function reopens sockets according to the current settings in the
  145. /// Configuration Manager. It holds the list of the interfaces which server
  146. /// should listen on. This function will open sockets on these interfaces
  147. /// only. This function is not exception safe.
  148. ///
  149. /// @param port UDP port on which server should listen.
  150. /// @param use_bcast should broadcast flags be set on the sockets.
  151. static void openActiveSockets(const uint16_t port, const bool use_bcast);
  152. /// @brief Starts DHCP_DDNS client IO if DDNS updates are enabled.
  153. ///
  154. /// If updates are enabled, it Instructs the D2ClientMgr singleton to
  155. /// enter send mode. If D2ClientMgr encounters errors it may throw
  156. /// D2ClientErrors. This method does not catch exceptions.
  157. void startD2();
  158. /// @brief Implements the error handler for DHCP_DDNS IO errors
  159. ///
  160. /// Invoked when a NameChangeRequest send to b10-dhcp-ddns completes with
  161. /// a failed status. These are communications errors, not data related
  162. /// failures.
  163. ///
  164. /// This method logs the failure and then suspends all further updates.
  165. /// Updating can only be restored by reconfiguration or restarting the
  166. /// server. There is currently no retry logic so the first IO error that
  167. /// occurs will suspend updates.
  168. /// @todo We may wish to make this more robust or sophisticated.
  169. ///
  170. /// @param result Result code of the send operation.
  171. /// @param ncr NameChangeRequest which failed to send.
  172. virtual void d2ClientErrorHandler(const dhcp_ddns::
  173. NameChangeSender::Result result,
  174. dhcp_ddns::NameChangeRequestPtr& ncr);
  175. protected:
  176. /// @name Functions filtering and sanity-checking received messages.
  177. ///
  178. /// @todo These functions are supposed to be moved to a new class which
  179. /// will manage different rules for accepting and rejecting messages.
  180. /// Perhaps ticket #3116 is a good opportunity to do it.
  181. ///
  182. //@{
  183. /// @brief Checks whether received message should be processed or discarded.
  184. ///
  185. /// This function checks whether received message should be processed or
  186. /// discarded. It should be called on the beginning of message processing
  187. /// (just after the message has been decoded). This message calls a number
  188. /// of other functions which check whether message should be processed,
  189. /// using different criteria.
  190. ///
  191. /// This function should be extended when new criteria for accepting
  192. /// received message have to be implemented. This function is meant to
  193. /// aggregate all early filtering checks on the received message. By having
  194. /// a single function like this, we are avoiding bloat of the server's main
  195. /// loop.
  196. ///
  197. /// @warning This function should remain exception safe.
  198. ///
  199. /// @param query Received message.
  200. ///
  201. /// @return true if the message should be further processed, or false if
  202. /// the message should be discarded.
  203. bool accept(const Pkt4Ptr& query) const;
  204. /// @brief Check if a message sent by directly connected client should be
  205. /// accepted or discared.
  206. ///
  207. /// This function checks if the received message is from directly connected
  208. /// client. If it is, it checks that it should be processed or discarded.
  209. ///
  210. /// Note that this function doesn't validate all addresses being carried in
  211. /// the message. The primary purpose of this function is to filter out
  212. /// direct messages in the local network for which there is no suitable
  213. /// subnet configured. For example, this function accepts unicast messages
  214. /// because unicasts may be used by clients located in remote networks to
  215. /// to renew existing leases. If their notion of address is wrong, the
  216. /// server will have to sent a NAK, instead of dropping the message.
  217. /// Detailed validation of such messages is performed at later stage of
  218. /// processing.
  219. ///
  220. /// This function accepts the following messages:
  221. /// - all valid relayed messages,
  222. /// - all unicast messages,
  223. /// - all broadcast messages received on the interface for which the
  224. /// suitable subnet exists (is configured).
  225. ///
  226. /// @param query Message sent by a client.
  227. ///
  228. /// @return true if message is accepted for further processing, false
  229. /// otherwise.
  230. bool acceptDirectRequest(const Pkt4Ptr& query) const;
  231. /// @brief Check if received message type is valid for the server to
  232. /// process.
  233. ///
  234. /// This function checks that the received message type belongs to the range
  235. /// of types regonized by the server and that the message of this type
  236. /// should be processed by the server.
  237. ///
  238. /// The messages types accepted for processing are:
  239. /// - Discover
  240. /// - Request
  241. /// - Release
  242. /// - Decline
  243. /// - Inform
  244. ///
  245. /// @param query Message sent by a client.
  246. ///
  247. /// @return true if message is accepted for further processing, false
  248. /// otherwise.
  249. bool acceptMessageType(const Pkt4Ptr& query) const;
  250. /// @brief Verifies if the server id belongs to our server.
  251. ///
  252. /// This function checks if the server identifier carried in the specified
  253. /// DHCPv4 message belongs to this server. If the server identifier option
  254. /// is absent or the value carried by this option is equal to one of the
  255. /// server identifiers used by the server, the true is returned. If the
  256. /// server identifier option is present, but it doesn't match any server
  257. /// identifier used by this server, the false value is returned.
  258. ///
  259. /// @param pkt DHCPv4 message which server identifier is to be checked.
  260. ///
  261. /// @return true, if the server identifier is absent or matches one of the
  262. /// server identifiers that the server is using; false otherwise.
  263. bool acceptServerId(const Pkt4Ptr& pkt) const;
  264. //@}
  265. /// @brief verifies if specified packet meets RFC requirements
  266. ///
  267. /// Checks if mandatory option is really there, that forbidden option
  268. /// is not there, and that client-id or server-id appears only once.
  269. ///
  270. /// @param pkt packet to be checked
  271. /// @param serverid expectation regarding server-id option
  272. /// @throw RFCViolation if any issues are detected
  273. static void sanityCheck(const Pkt4Ptr& pkt, RequirementLevel serverid);
  274. /// @brief Processes incoming DISCOVER and returns response.
  275. ///
  276. /// Processes received DISCOVER message and verifies that its sender
  277. /// should be served. In particular, a lease is selected and sent
  278. /// as an offer to a client if it should be served.
  279. ///
  280. /// @param discover DISCOVER message received from client
  281. ///
  282. /// @return OFFER message or NULL
  283. Pkt4Ptr processDiscover(Pkt4Ptr& discover);
  284. /// @brief Processes incoming REQUEST and returns REPLY response.
  285. ///
  286. /// Processes incoming REQUEST message and verifies that its sender
  287. /// should be served. In particular, verifies that requested lease
  288. /// is valid, not expired, not reserved, not used by other client and
  289. /// that requesting client is allowed to use it.
  290. ///
  291. /// Returns ACK message, NAK message, or NULL
  292. ///
  293. /// @param request a message received from client
  294. ///
  295. /// @return ACK or NAK message
  296. Pkt4Ptr processRequest(Pkt4Ptr& request);
  297. /// @brief Stub function that will handle incoming RELEASE messages.
  298. ///
  299. /// In DHCPv4, server does not respond to RELEASE messages, therefore
  300. /// this function does not return anything.
  301. ///
  302. /// @param release message received from client
  303. void processRelease(Pkt4Ptr& release);
  304. /// @brief Stub function that will handle incoming DHCPDECLINE messages.
  305. ///
  306. /// @param decline message received from client
  307. void processDecline(Pkt4Ptr& decline);
  308. /// @brief Stub function that will handle incoming INFORM messages.
  309. ///
  310. /// @param inform message received from client
  311. Pkt4Ptr processInform(Pkt4Ptr& inform);
  312. /// @brief Copies default parameters from client's to server's message
  313. ///
  314. /// Some fields are copied from client's message into server's response,
  315. /// e.g. client HW address, number of hops, transaction-id etc.
  316. ///
  317. /// @param question any message sent by client
  318. /// @param answer any message server is going to send as response
  319. void copyDefaultFields(const Pkt4Ptr& question, Pkt4Ptr& answer);
  320. /// @brief Appends options requested by client.
  321. ///
  322. /// This method assigns options that were requested by client
  323. /// (sent in PRL) or are enforced by server.
  324. ///
  325. /// @param question DISCOVER or REQUEST message from a client.
  326. /// @param msg outgoing message (options will be added here)
  327. void appendRequestedOptions(const Pkt4Ptr& question, Pkt4Ptr& msg);
  328. /// @brief Appends requested vendor options as requested by client.
  329. ///
  330. /// This method is similar to \ref appendRequestedOptions(), but uses
  331. /// vendor options. The major difference is that vendor-options use
  332. /// its own option spaces (there may be more than one distinct set of vendor
  333. /// options, each with unique vendor-id). Vendor options are requested
  334. /// using separate options within their respective vendor-option spaces.
  335. ///
  336. /// @param question DISCOVER or REQUEST message from a client.
  337. /// @param answer outgoing message (options will be added here)
  338. void appendRequestedVendorOptions(const Pkt4Ptr& question, Pkt4Ptr& answer);
  339. /// @brief Assigns a lease and appends corresponding options
  340. ///
  341. /// This method chooses the most appropriate lease for reqesting
  342. /// client and assigning it. Options corresponding to the lease
  343. /// are added to specific message.
  344. ///
  345. /// @param question DISCOVER or REQUEST message from client
  346. /// @param answer OFFER or ACK/NAK message (lease options will be added here)
  347. void assignLease(const Pkt4Ptr& question, Pkt4Ptr& answer);
  348. /// @brief Append basic options if they are not present.
  349. ///
  350. /// This function adds the following basic options if they
  351. /// are not yet added to the message:
  352. /// - Subnet Mask,
  353. /// - Router,
  354. /// - Name Server,
  355. /// - Domain Name.
  356. ///
  357. /// @param question DISCOVER or REQUEST message from a client.
  358. /// @param msg the message to add options to.
  359. void appendBasicOptions(const Pkt4Ptr& question, Pkt4Ptr& msg);
  360. /// @brief Processes Client FQDN and Hostname Options sent by a client.
  361. ///
  362. /// Client may send Client FQDN or Hostname option to communicate its name
  363. /// to the server. Server may use this name to perform DNS update for the
  364. /// lease being assigned to a client. If server takes responsibility for
  365. /// updating DNS for a client it may communicate it by sending the Client
  366. /// FQDN or Hostname %Option back to the client. Server select a different
  367. /// name than requested by a client to update DNS. In such case, the server
  368. /// stores this different name in its response.
  369. ///
  370. /// Client should not send both Client FQDN and Hostname options. However,
  371. /// if client sends both options, server should prefer Client FQDN option
  372. /// and ignore the Hostname option. If Client FQDN option is not present,
  373. /// the Hostname option is processed.
  374. ///
  375. /// The Client FQDN %Option is processed by this function as described in
  376. /// RFC4702.
  377. ///
  378. /// In response to a Hostname %Option sent by a client, the server may send
  379. /// Hostname option with the same or different hostname. If different
  380. /// hostname is sent, it is an indication to the client that server has
  381. /// overridden the client's preferred name and will rather use this
  382. /// different name to update DNS. However, since Hostname option doesn't
  383. /// carry an information whether DNS update will be carried by the server
  384. /// or not, the client is responsible for checking whether DNS update
  385. /// has been performed.
  386. ///
  387. /// After successful processing options stored in the first parameter,
  388. /// this function may add Client FQDN or Hostname option to the response
  389. /// message. In some cases, server may cease to add any options to the
  390. /// response, i.e. when server doesn't support DNS updates.
  391. ///
  392. /// This function does not throw. It simply logs the debug message if the
  393. /// processing of the FQDN or Hostname failed.
  394. ///
  395. /// @param query A DISCOVER or REQUEST message from a cient.
  396. /// @param [out] answer A response message to be sent to a client.
  397. void processClientName(const Pkt4Ptr& query, Pkt4Ptr& answer);
  398. /// @brief this is a prefix added to the contend of vendor-class option
  399. ///
  400. /// If incoming packet has a vendor class option, its content is
  401. /// prepended with this prefix and then interpreted as a class.
  402. /// For example, a packet that sends vendor class with value of "FOO"
  403. /// will cause the packet to be assigned to class VENDOR_CLASS_FOO.
  404. static const std::string VENDOR_CLASS_PREFIX;
  405. private:
  406. /// @brief Process Client FQDN %Option sent by a client.
  407. ///
  408. /// This function is called by the @c Dhcpv4Srv::processClientName when
  409. /// the client has sent the FQDN option in its message to the server.
  410. /// It comprises the actual logic to parse the FQDN option and prepare
  411. /// the FQDN option to be sent back to the client in the server's
  412. /// response.
  413. ///
  414. /// @param fqdn An DHCPv4 Client FQDN %Option sent by a client.
  415. /// @param [out] answer A response message to be sent to a client.
  416. void processClientFqdnOption(const Option4ClientFqdnPtr& fqdn,
  417. Pkt4Ptr& answer);
  418. /// @brief Process Hostname %Option sent by a client.
  419. ///
  420. /// This function is called by the @c DHcpv4Srv::processClientName when
  421. /// the client has sent the Hostname option in its message to the server.
  422. /// It comprises the actual logic to parse the Hostname option and
  423. /// prepare the Hostname option to be sent back to the client in the
  424. /// server's response.
  425. ///
  426. /// @param opt_hostname An @c OptionString object encapsulating the Hostname
  427. /// %Option.
  428. /// @param [out] answer A response message to be sent to a client.
  429. void processHostnameOption(const OptionStringPtr& opt_hostname,
  430. Pkt4Ptr& answer);
  431. protected:
  432. /// @brief Creates NameChangeRequests which correspond to the lease
  433. /// which has been acquired.
  434. ///
  435. /// If this function is called whe an existing lease is renewed, it
  436. /// may generate NameChangeRequest to remove existing DNS entries which
  437. /// correspond to the old lease instance. This function may cease to
  438. /// generate NameChangeRequests if the notion of the client's FQDN hasn't
  439. /// changed between an old and new lease.
  440. ///
  441. /// @param lease A pointer to the new lease which has been acquired.
  442. /// @param old_lease A pointer to the instance of the old lease which has
  443. /// been replaced by the new lease passed in the first argument. The NULL
  444. /// value indicates that the new lease has been allocated, rather than
  445. /// lease being renewed.
  446. void createNameChangeRequests(const Lease4Ptr& lease,
  447. const Lease4Ptr& old_lease);
  448. /// @brief Creates the NameChangeRequest and adds to the queue for
  449. /// processing.
  450. ///
  451. /// This creates the @c isc::dhcp_ddns::NameChangeRequest; emits a
  452. /// the debug message which indicates whether the request being added is
  453. /// to remove DNS entry or add a new entry; and then sends the request
  454. /// to the D2ClientMgr for transmission to b10-dhcp-ddns.
  455. ///
  456. /// @param chg_type A type of the NameChangeRequest (ADD or REMOVE).
  457. /// @param lease A lease for which the NameChangeRequest is created and
  458. /// queued.
  459. void queueNameChangeRequest(const isc::dhcp_ddns::NameChangeType chg_type,
  460. const Lease4Ptr& lease);
  461. /// @brief Attempts to renew received addresses
  462. ///
  463. /// Attempts to renew existing lease. This typically includes finding a lease that
  464. /// corresponds to the received address. If no such lease is found, a status code
  465. /// response is generated.
  466. ///
  467. /// @param renew client's message asking for renew
  468. /// @param reply server's response (ACK or NAK)
  469. void renewLease(const Pkt4Ptr& renew, Pkt4Ptr& reply);
  470. /// @brief Appends default options to a message
  471. ///
  472. /// Currently it is only a Message Type option. This function does not add
  473. /// the Server Identifier option as this option must be added using
  474. /// @c Dhcpv4Srv::appendServerID.
  475. ///
  476. ///
  477. /// @param msg message object (options will be added to it)
  478. /// @param msg_type specifies message type
  479. void appendDefaultOptions(Pkt4Ptr& msg, uint8_t msg_type);
  480. /// @brief Adds server identifier option to the server's response.
  481. ///
  482. /// This method adds a server identifier to the DHCPv4 message. It epxects
  483. /// that the local (source) address is set for this message. If address is
  484. /// not set, it will throw an exception. This method also expects that the
  485. /// server identifier option is not present in the specified message.
  486. /// Otherwise, it will throw an exception on attempt to add a duplicate
  487. /// server identifier option.
  488. ///
  489. /// @note This method doesn't throw exceptions by itself but the underlying
  490. /// classes being used my throw. The reason for this method to not sanity
  491. /// check the specified message is that it is meant to be called internally
  492. /// by the @c Dhcpv4Srv class.
  493. ///
  494. /// @note This method is static because it is not dependent on the class
  495. /// state.
  496. ///
  497. /// @param [out] response DHCPv4 message to which the server identifier
  498. /// option should be added.
  499. static void appendServerID(const Pkt4Ptr& response);
  500. /// @brief Set IP/UDP and interface parameters for the DHCPv4 response.
  501. ///
  502. /// This method sets the following parameters for the DHCPv4 message being
  503. /// sent to a client:
  504. /// - client unicast or a broadcast address,
  505. /// - client or relay port,
  506. /// - server address,
  507. /// - server port,
  508. /// - name and index of the interface which is to be used to send the
  509. /// message.
  510. ///
  511. /// Internally it calls the @c Dhcpv4Srv::adjustRemoteAddr to figure
  512. /// out the destination address (client unicast address or broadcast
  513. /// address).
  514. ///
  515. /// The destination port is always DHCPv4 client (68) or relay (67) port,
  516. /// depending if the response will be sent directly to a client.
  517. ///
  518. /// The source port is always set to DHCPv4 server port (67).
  519. ///
  520. /// The interface selected for the response is always the same as the
  521. /// one through which the query has been received.
  522. ///
  523. /// The source address for the response is the IPv4 address assigned to
  524. /// the interface being used to send the response. This function uses
  525. /// @c IfaceMgr to get the socket bound to the IPv4 address on the
  526. /// particular interface.
  527. ///
  528. /// @note This method is static because it is not dependent on the class
  529. /// state.
  530. static void adjustIfaceData(const Pkt4Ptr& query, const Pkt4Ptr& response);
  531. /// @brief Sets remote addresses for outgoing packet.
  532. ///
  533. /// This method sets the local and remote addresses on outgoing packet.
  534. /// The addresses being set depend on the following conditions:
  535. /// - has incoming packet been relayed,
  536. /// - is direct response to a client without address supported,
  537. /// - type of the outgoing packet,
  538. /// - broadcast flag set in the incoming packet.
  539. ///
  540. /// @warning This method does not check whether provided packet pointers
  541. /// are valid. Make sure that pointers are correct before calling this
  542. /// function.
  543. ///
  544. /// @note This method is static because it is not dependent on the class
  545. /// state.
  546. ///
  547. /// @param question instance of a packet received by a server.
  548. /// @param [out] response response packet which addresses are to be
  549. /// adjusted.
  550. static void adjustRemoteAddr(const Pkt4Ptr& question,
  551. const Pkt4Ptr& response);
  552. /// @brief converts server-id to text
  553. /// Converts content of server-id option to a text representation, e.g.
  554. /// "192.0.2.1"
  555. ///
  556. /// @param opt option that contains server-id
  557. /// @return string representation
  558. static std::string srvidToString(const OptionPtr& opt);
  559. /// @brief Computes DHCID from a lease.
  560. ///
  561. /// This method creates an object which represents DHCID. The DHCID value
  562. /// is computed as described in RFC4701. The section 3.3. of RFC4701
  563. /// specifies the DHCID RR Identifier Type codes:
  564. /// - 0x0000 The 1 octet htype followed by glen octets of chaddr
  565. /// - 0x0001 The data octets from the DHCPv4 client's Client Identifier
  566. /// option.
  567. /// - 0x0002 The client's DUID.
  568. ///
  569. /// Currently this function supports first two of these identifiers.
  570. /// The 0x0001 is preferred over the 0x0000 - if the client identifier
  571. /// option is present, the former is used. If the client identifier
  572. /// is absent, the latter is used.
  573. ///
  574. /// @todo Implement support for 0x0002 DHCID identifier type.
  575. ///
  576. /// @param lease A pointer to the structure describing a lease.
  577. /// @return An object encapsulating DHCID to be used for DNS updates.
  578. /// @throw DhcidComputeError If the computation of the DHCID failed.
  579. static isc::dhcp_ddns::D2Dhcid computeDhcid(const Lease4Ptr& lease);
  580. /// @brief Selects a subnet for a given client's packet.
  581. ///
  582. /// @param question client's message
  583. /// @return selected subnet (or NULL if no suitable subnet was found)
  584. isc::dhcp::Subnet4Ptr selectSubnet(const Pkt4Ptr& question) const;
  585. /// indicates if shutdown is in progress. Setting it to true will
  586. /// initiate server shutdown procedure.
  587. volatile bool shutdown_;
  588. /// @brief dummy wrapper around IfaceMgr::receive4
  589. ///
  590. /// This method is useful for testing purposes, where its replacement
  591. /// simulates reception of a packet. For that purpose it is protected.
  592. virtual Pkt4Ptr receivePacket(int timeout);
  593. /// @brief dummy wrapper around IfaceMgr::send()
  594. ///
  595. /// This method is useful for testing purposes, where its replacement
  596. /// simulates transmission of a packet. For that purpose it is protected.
  597. virtual void sendPacket(const Pkt4Ptr& pkt);
  598. /// @brief Implements a callback function to parse options in the message.
  599. ///
  600. /// @param buf a A buffer holding options in on-wire format.
  601. /// @param option_space A name of the option space which holds definitions
  602. /// of to be used to parse options in the packets.
  603. /// @param [out] options A reference to the collection where parsed options
  604. /// will be stored.
  605. /// @return An offset to the first byte after last parsed option.
  606. size_t unpackOptions(const OptionBuffer& buf,
  607. const std::string& option_space,
  608. isc::dhcp::OptionCollection& options);
  609. /// @brief Assigns incoming packet to zero or more classes.
  610. ///
  611. /// @note For now, the client classification is very simple. It just uses
  612. /// content of the vendor-class-identifier option as a class. The resulting
  613. /// class will be stored in packet (see @ref isc::dhcp::Pkt4::classes_ and
  614. /// @ref isc::dhcp::Pkt4::inClass).
  615. ///
  616. /// @param pkt packet to be classified
  617. void classifyPacket(const Pkt4Ptr& pkt);
  618. /// @brief Performs packet processing specific to a class
  619. ///
  620. /// This processing is a likely candidate to be pushed into hooks.
  621. ///
  622. /// @param query incoming client's packet
  623. /// @param rsp server's response
  624. /// @return true if successful, false otherwise (will prevent sending response)
  625. bool classSpecificProcessing(const Pkt4Ptr& query, const Pkt4Ptr& rsp);
  626. private:
  627. /// @brief Constructs netmask option based on subnet4
  628. /// @param subnet subnet for which the netmask will be calculated
  629. ///
  630. /// @return Option that contains netmask information
  631. static OptionPtr getNetmaskOption(const Subnet4Ptr& subnet);
  632. /// @brief Implements the error handler for socket open failure.
  633. ///
  634. /// This callback function is installed on the @c isc::dhcp::IfaceMgr
  635. /// when IPv4 sockets are being open. When socket fails to open for
  636. /// any reason, this function is called. It simply logs the error message.
  637. ///
  638. /// @param errmsg An error message containing a cause of the failure.
  639. static void ifaceMgrSocket4ErrorHandler(const std::string& errmsg);
  640. /// @brief Allocation Engine.
  641. /// Pointer to the allocation engine that we are currently using
  642. /// It must be a pointer, because we will support changing engines
  643. /// during normal operation (e.g. to use different allocators)
  644. boost::shared_ptr<AllocEngine> alloc_engine_;
  645. uint16_t port_; ///< UDP port number on which server listens.
  646. bool use_bcast_; ///< Should broadcast be enabled on sockets (if true).
  647. /// Indexes for registered hook points
  648. int hook_index_pkt4_receive_;
  649. int hook_index_subnet4_select_;
  650. int hook_index_pkt4_send_;
  651. };
  652. }; // namespace isc::dhcp
  653. }; // namespace isc
  654. #endif // DHCP4_SRV_H