dhcp4_srv.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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 <dhcpsrv/subnet.h>
  20. #include <dhcpsrv/alloc_engine.h>
  21. #include <boost/noncopyable.hpp>
  22. #include <iostream>
  23. namespace isc {
  24. namespace dhcp {
  25. /// @brief DHCPv4 server service.
  26. ///
  27. /// This singleton class represents DHCPv4 server. It contains all
  28. /// top-level methods and routines necessary for server operation.
  29. /// In particular, it instantiates IfaceMgr, loads or generates DUID
  30. /// that is going to be used as server-identifier, receives incoming
  31. /// packets, processes them, manages leases assignment and generates
  32. /// appropriate responses.
  33. ///
  34. /// This class does not support any controlling mechanisms directly.
  35. /// See the derived \ref ControlledDhcpv4Srv class for support for
  36. /// command and configuration updates over msgq.
  37. ///
  38. /// For detailed explanation or relations between main(), ControlledDhcpv4Srv,
  39. /// Dhcpv4Srv and other classes, see \ref dhcpv4Session.
  40. class Dhcpv4Srv : public boost::noncopyable {
  41. public:
  42. /// @brief defines if certain option may, must or must not appear
  43. typedef enum {
  44. FORBIDDEN,
  45. MANDATORY,
  46. OPTIONAL
  47. } RequirementLevel;
  48. /// @brief Default constructor.
  49. ///
  50. /// Instantiates necessary services, required to run DHCPv4 server.
  51. /// In particular, creates IfaceMgr that will be responsible for
  52. /// network interaction. Will instantiate lease manager, and load
  53. /// old or create new DUID. It is possible to specify alternate
  54. /// port on which DHCPv4 server will listen on. That is mostly useful
  55. /// for testing purposes. The Last two arguments of the constructor
  56. /// should be left at default values for normal server operation.
  57. /// They should be set to 'false' when creating an instance of this
  58. /// class for unit testing because features they enable require
  59. /// root privileges.
  60. ///
  61. /// @param port specifies port number to listen on
  62. /// @param dbconfig Lease manager configuration string. The default
  63. /// of the "memfile" manager is used for testing.
  64. /// @param use_bcast configure sockets to support broadcast messages.
  65. /// @param direct_response_desired specifies if it is desired to
  66. /// use direct V4 traffic.
  67. Dhcpv4Srv(uint16_t port = DHCP4_SERVER_PORT,
  68. const char* dbconfig = "type=memfile",
  69. const bool use_bcast = true,
  70. const bool direct_response_desired = true);
  71. /// @brief Destructor. Used during DHCPv4 service shutdown.
  72. ~Dhcpv4Srv();
  73. /// @brief Main server processing loop.
  74. ///
  75. /// Main server processing loop. Receives incoming packets, verifies
  76. /// their correctness, generates appropriate answer (if needed) and
  77. /// transmits respones.
  78. ///
  79. /// @return true, if being shut down gracefully, fail if experienced
  80. /// critical error.
  81. bool run();
  82. /// @brief Instructs the server to shut down.
  83. void shutdown();
  84. /// @brief Return textual type of packet received by server
  85. ///
  86. /// Returns the name of valid packet received by the server (e.g. DISCOVER).
  87. /// If the packet is unknown - or if it is a valid DHCP packet but not one
  88. /// expected to be received by the server (such as an OFFER), the string
  89. /// "UNKNOWN" is returned. This method is used in debug messages.
  90. ///
  91. /// As the operation of the method does not depend on any server state, it
  92. /// is declared static.
  93. ///
  94. /// @todo: This should be named static Pkt4::getName()
  95. ///
  96. /// @param type DHCPv4 packet type
  97. ///
  98. /// @return Pointer to "const" string containing the packet name.
  99. /// Note that this string is statically allocated and MUST NOT
  100. /// be freed by the caller.
  101. static const char* serverReceivedPacketName(uint8_t type);
  102. protected:
  103. /// @brief verifies if specified packet meets RFC requirements
  104. ///
  105. /// Checks if mandatory option is really there, that forbidden option
  106. /// is not there, and that client-id or server-id appears only once.
  107. ///
  108. /// @param pkt packet to be checked
  109. /// @param serverid expectation regarding server-id option
  110. /// @throw RFCViolation if any issues are detected
  111. void sanityCheck(const Pkt4Ptr& pkt, RequirementLevel serverid);
  112. /// @brief Processes incoming DISCOVER and returns response.
  113. ///
  114. /// Processes received DISCOVER message and verifies that its sender
  115. /// should be served. In particular, a lease is selected and sent
  116. /// as an offer to a client if it should be served.
  117. ///
  118. /// @param discover DISCOVER message received from client
  119. ///
  120. /// @return OFFER message or NULL
  121. Pkt4Ptr processDiscover(Pkt4Ptr& discover);
  122. /// @brief Processes incoming REQUEST and returns REPLY response.
  123. ///
  124. /// Processes incoming REQUEST message and verifies that its sender
  125. /// should be served. In particular, verifies that requested lease
  126. /// is valid, not expired, not reserved, not used by other client and
  127. /// that requesting client is allowed to use it.
  128. ///
  129. /// Returns ACK message, NAK message, or NULL
  130. ///
  131. /// @param request a message received from client
  132. ///
  133. /// @return ACK or NAK message
  134. Pkt4Ptr processRequest(Pkt4Ptr& request);
  135. /// @brief Stub function that will handle incoming RELEASE messages.
  136. ///
  137. /// In DHCPv4, server does not respond to RELEASE messages, therefore
  138. /// this function does not return anything.
  139. ///
  140. /// @param release message received from client
  141. void processRelease(Pkt4Ptr& release);
  142. /// @brief Stub function that will handle incoming DHCPDECLINE messages.
  143. ///
  144. /// @param decline message received from client
  145. void processDecline(Pkt4Ptr& decline);
  146. /// @brief Stub function that will handle incoming INFORM messages.
  147. ///
  148. /// @param inform message received from client
  149. Pkt4Ptr processInform(Pkt4Ptr& inform);
  150. /// @brief Copies default parameters from client's to server's message
  151. ///
  152. /// Some fields are copied from client's message into server's response,
  153. /// e.g. client HW address, number of hops, transaction-id etc.
  154. ///
  155. /// @param question any message sent by client
  156. /// @param answer any message server is going to send as response
  157. void copyDefaultFields(const Pkt4Ptr& question, Pkt4Ptr& answer);
  158. /// @brief Appends options requested by client.
  159. ///
  160. /// This method assigns options that were requested by client
  161. /// (sent in PRL) or are enforced by server.
  162. ///
  163. /// @param question DISCOVER or REQUEST message from a client.
  164. /// @param msg outgoing message (options will be added here)
  165. void appendRequestedOptions(const Pkt4Ptr& question, Pkt4Ptr& msg);
  166. /// @brief Assigns a lease and appends corresponding options
  167. ///
  168. /// This method chooses the most appropriate lease for reqesting
  169. /// client and assigning it. Options corresponding to the lease
  170. /// are added to specific message.
  171. ///
  172. /// @param question DISCOVER or REQUEST message from client
  173. /// @param answer OFFER or ACK/NAK message (lease options will be added here)
  174. void assignLease(const Pkt4Ptr& question, Pkt4Ptr& answer);
  175. /// @brief Append basic options if they are not present.
  176. ///
  177. /// This function adds the following basic options if they
  178. /// are not yet added to the message:
  179. /// - Subnet Mask,
  180. /// - Router,
  181. /// - Name Server,
  182. /// - Domain Name.
  183. ///
  184. /// @param question DISCOVER or REQUEST message from a client.
  185. /// @param msg the message to add options to.
  186. void appendBasicOptions(const Pkt4Ptr& question, Pkt4Ptr& msg);
  187. /// @brief Attempts to renew received addresses
  188. ///
  189. /// Attempts to renew existing lease. This typically includes finding a lease that
  190. /// corresponds to the received address. If no such lease is found, a status code
  191. /// response is generated.
  192. ///
  193. /// @param renew client's message asking for renew
  194. /// @param reply server's response (ACK or NAK)
  195. void renewLease(const Pkt4Ptr& renew, Pkt4Ptr& reply);
  196. /// @brief Appends default options to a message
  197. ///
  198. /// @param msg message object (options will be added to it)
  199. /// @param msg_type specifies message type
  200. void appendDefaultOptions(Pkt4Ptr& msg, uint8_t msg_type);
  201. /// @brief Sets remote addresses for outgoing packet.
  202. ///
  203. /// This method sets the local and remote addresses on outgoing packet.
  204. /// The addresses being set depend on the following conditions:
  205. /// - has incoming packet been relayed,
  206. /// - is direct response to a client without address supported,
  207. /// - type of the outgoing packet,
  208. /// - broadcast flag set in the incoming packet.
  209. ///
  210. /// @warning This method does not check whether provided packet pointers
  211. /// are valid. Make sure that pointers are correct before calling this
  212. /// function.
  213. ///
  214. /// @param question instance of a packet received by a server.
  215. /// @param [out] msg response packet which addresses are to be adjusted.
  216. void adjustRemoteAddr(const Pkt4Ptr& question, Pkt4Ptr& msg);
  217. /// @brief Returns server-identifier option
  218. ///
  219. /// @return server-id option
  220. OptionPtr getServerID() { return serverid_; }
  221. /// @brief Sets server-identifier.
  222. ///
  223. /// This method attempts to set server-identifier DUID. It tries to
  224. /// load previously stored IP from configuration. If there is no previously
  225. /// stored server identifier, it will pick up one address from configured
  226. /// and supported network interfaces.
  227. ///
  228. /// @throws isc::Unexpected Failed to obtain server identifier (i.e. no
  229. // previously stored configuration and no network interfaces available)
  230. void generateServerID();
  231. /// @brief attempts to load server-id from a file
  232. ///
  233. /// Tries to load duid from a text file. If the load is successful,
  234. /// it creates server-id option and stores it in serverid_ (to be used
  235. /// later by getServerID()).
  236. ///
  237. /// @param file_name name of the server-id file to load
  238. /// @return true if load was successful, false otherwise
  239. bool loadServerID(const std::string& file_name);
  240. /// @brief attempts to write server-id to a file
  241. /// Tries to write server-id content (stored in serverid_) to a text file.
  242. ///
  243. /// @param file_name name of the server-id file to write
  244. /// @return true if write was successful, false otherwise
  245. bool writeServerID(const std::string& file_name);
  246. /// @brief converts server-id to text
  247. /// Converts content of server-id option to a text representation, e.g.
  248. /// "192.0.2.1"
  249. ///
  250. /// @param opt option that contains server-id
  251. /// @return string representation
  252. static std::string srvidToString(const OptionPtr& opt);
  253. /// @brief Selects a subnet for a given client's packet.
  254. ///
  255. /// @param question client's message
  256. /// @return selected subnet (or NULL if no suitable subnet was found)
  257. isc::dhcp::Subnet4Ptr selectSubnet(const Pkt4Ptr& question);
  258. /// server DUID (to be sent in server-identifier option)
  259. OptionPtr serverid_;
  260. /// indicates if shutdown is in progress. Setting it to true will
  261. /// initiate server shutdown procedure.
  262. volatile bool shutdown_;
  263. private:
  264. /// @brief Constructs netmask option based on subnet4
  265. /// @param subnet subnet for which the netmask will be calculated
  266. ///
  267. /// @return Option that contains netmask information
  268. static OptionPtr getNetmaskOption(const Subnet4Ptr& subnet);
  269. /// @brief Allocation Engine.
  270. /// Pointer to the allocation engine that we are currently using
  271. /// It must be a pointer, because we will support changing engines
  272. /// during normal operation (e.g. to use different allocators)
  273. boost::shared_ptr<AllocEngine> alloc_engine_;
  274. };
  275. }; // namespace isc::dhcp
  276. }; // namespace isc
  277. #endif // DHCP4_SRV_H