subnet.h 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580
  1. // Copyright (C) 2012-2014 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 SUBNET_H
  15. #define SUBNET_H
  16. #include <asiolink/io_address.h>
  17. #include <dhcp/option.h>
  18. #include <dhcp/classify.h>
  19. #include <dhcpsrv/cfg_option.h>
  20. #include <dhcpsrv/option_space_container.h>
  21. #include <dhcpsrv/pool.h>
  22. #include <dhcpsrv/triplet.h>
  23. #include <dhcpsrv/lease.h>
  24. #include <boost/shared_ptr.hpp>
  25. namespace isc {
  26. namespace dhcp {
  27. /// @brief a base class for Subnet4 and Subnet6
  28. ///
  29. /// This class presents a common base for IPv4 and IPv6 subnets.
  30. /// In a physical sense, a subnet defines a single network link with all devices
  31. /// attached to it. In most cases all devices attached to a single link can
  32. /// share the same parameters. Therefore Subnet holds several values that are
  33. /// typically shared by all hosts: renew timer (T1), rebind timer (T2) and
  34. /// leased addresses lifetime (valid-lifetime). It also holds the set
  35. /// of DHCP option instances configured for the subnet. These options are
  36. /// included in DHCP messages being sent to clients which are connected
  37. /// to the particular subnet.
  38. ///
  39. /// @todo: Implement support for options here
  40. /// @brief Unique identifier for a subnet (both v4 and v6)
  41. typedef uint32_t SubnetID;
  42. class Subnet {
  43. public:
  44. /// @brief Holds optional information about relay.
  45. ///
  46. /// In some cases it is beneficial to have additional information about
  47. /// a relay configured in the subnet. For now, the structure holds only
  48. /// IP address, but there may potentially be additional parameters added
  49. /// later, e.g. relay interface-id or relay-id.
  50. struct RelayInfo {
  51. /// @brief default and the only constructor
  52. ///
  53. /// @param addr an IP address of the relay (may be :: or 0.0.0.0)
  54. RelayInfo(const isc::asiolink::IOAddress& addr);
  55. /// @brief IP address of the relay
  56. isc::asiolink::IOAddress addr_;
  57. };
  58. /// Pointer to the RelayInfo structure
  59. typedef boost::shared_ptr<Subnet::RelayInfo> RelayInfoPtr;
  60. /// @brief checks if specified address is in range
  61. bool inRange(const isc::asiolink::IOAddress& addr) const;
  62. /// @brief checks if the specified address is in pools
  63. ///
  64. /// Note the difference between inSubnet() and inPool(). For a given
  65. /// subnet (e.g. 2001::/64) there may be one or more pools defined
  66. /// that may or may not cover entire subnet, e.g. pool 2001::1-2001::10).
  67. /// inPool() returning true implies inSubnet(), but the reverse implication
  68. /// is not always true. For the given example, 2001::1234:abcd would return
  69. /// true for inSubnet(), but false for inPool() check.
  70. ///
  71. /// @param type type of pools to iterate over
  72. /// @param addr this address will be checked if it belongs to any pools in
  73. /// that subnet
  74. /// @return true if the address is in any of the pools
  75. bool inPool(Lease::Type type, const isc::asiolink::IOAddress& addr) const;
  76. /// @brief Return valid-lifetime for addresses in that prefix
  77. Triplet<uint32_t> getValid() const {
  78. return (valid_);
  79. }
  80. /// @brief Returns T1 (renew timer), expressed in seconds
  81. Triplet<uint32_t> getT1() const {
  82. return (t1_);
  83. }
  84. /// @brief Returns T2 (rebind timer), expressed in seconds
  85. Triplet<uint32_t> getT2() const {
  86. return (t2_);
  87. }
  88. /// @brief Returns pointer to the option data configuration for this subnet.
  89. CfgOptionPtr getCfgOption() {
  90. return (cfg_option_);
  91. }
  92. /// @brief Returns const pointer to the option data configuration for this
  93. /// subnet.
  94. ConstCfgOptionPtr getCfgOption() const {
  95. return (cfg_option_);
  96. }
  97. /// @brief returns the last address that was tried from this pool
  98. ///
  99. /// This method returns the last address that was attempted to be allocated
  100. /// from this subnet. This is used as helper information for the next
  101. /// iteration of the allocation algorithm.
  102. ///
  103. /// @todo: Define map<SubnetID, IOAddress> somewhere in the
  104. /// AllocEngine::IterativeAllocator and keep the data there
  105. ///
  106. /// @param type lease type to be returned
  107. /// @return address/prefix that was last tried from this pool
  108. isc::asiolink::IOAddress getLastAllocated(Lease::Type type) const;
  109. /// @brief sets the last address that was tried from this pool
  110. ///
  111. /// This method sets the last address that was attempted to be allocated
  112. /// from this subnet. This is used as helper information for the next
  113. /// iteration of the allocation algorithm.
  114. ///
  115. /// @todo: Define map<SubnetID, IOAddress> somewhere in the
  116. /// AllocEngine::IterativeAllocator and keep the data there
  117. /// @param addr address/prefix to that was tried last
  118. /// @param type lease type to be set
  119. void setLastAllocated(Lease::Type type,
  120. const isc::asiolink::IOAddress& addr);
  121. /// @brief Returns unique ID for that subnet
  122. /// @return unique ID for that subnet
  123. SubnetID getID() const { return (id_); }
  124. /// @brief Returns subnet parameters (prefix and prefix length)
  125. ///
  126. /// @return (prefix, prefix length) pair
  127. std::pair<isc::asiolink::IOAddress, uint8_t> get() const {
  128. return (std::make_pair(prefix_, prefix_len_));
  129. }
  130. /// @brief Adds a new pool.
  131. /// @param pool pool to be added
  132. void addPool(const PoolPtr& pool);
  133. /// @brief Deletes all pools of specified type
  134. ///
  135. /// This method is used for testing purposes only
  136. /// @param type type of pools to be deleted
  137. void delPools(Lease::Type type);
  138. /// @brief Returns a pool that specified address belongs to
  139. ///
  140. /// If there is no pool that the address belongs to (hint is invalid), other
  141. /// pool of specified type will be returned.
  142. ///
  143. /// With anypool set to true, this is means give me a pool, preferably
  144. /// the one that addr belongs to. With anypool set to false, it means
  145. /// give me a pool that addr belongs to (or NULL if here is no such pool)
  146. ///
  147. /// @param type pool type that the pool is looked for
  148. /// @param addr address that the returned pool should cover (optional)
  149. /// @param anypool other pool may be returned as well, not only the one
  150. /// that addr belongs to
  151. /// @return found pool (or NULL)
  152. const PoolPtr getPool(Lease::Type type, const isc::asiolink::IOAddress& addr,
  153. bool anypool = true) const;
  154. /// @brief Returns a pool without any address specified
  155. ///
  156. /// @param type pool type that the pool is looked for
  157. /// @return returns one of the pools defined
  158. PoolPtr getAnyPool(Lease::Type type) {
  159. return (getPool(type, default_pool()));
  160. }
  161. /// @brief Returns the default address that will be used for pool selection
  162. ///
  163. /// It must be implemented in derived classes (should return :: for Subnet6
  164. /// and 0.0.0.0 for Subnet4)
  165. virtual isc::asiolink::IOAddress default_pool() const = 0;
  166. /// @brief Returns all pools (const variant)
  167. ///
  168. /// The reference is only valid as long as the object that returned it.
  169. ///
  170. /// @param type lease type to be set
  171. /// @return a collection of all pools
  172. const PoolCollection& getPools(Lease::Type type) const;
  173. /// @brief Sets name of the network interface for directly attached networks
  174. ///
  175. /// @param iface_name name of the interface
  176. void setIface(const std::string& iface_name);
  177. /// @brief Network interface name used to reach subnet (or "" for remote
  178. /// subnets)
  179. /// @return network interface name for directly attached subnets or ""
  180. std::string getIface() const;
  181. /// @brief Returns textual representation of the subnet (e.g.
  182. /// "2001:db8::/64")
  183. ///
  184. /// @return textual representation
  185. virtual std::string toText() const;
  186. /// @brief Resets subnet-id counter to its initial value (1)
  187. ///
  188. /// This should be called during reconfiguration, before any new
  189. /// subnet objects are created. It will ensure that the subnet_id will
  190. /// be consistent between reconfigures.
  191. static void resetSubnetID() {
  192. static_id_ = 1;
  193. }
  194. /// @brief Sets information about relay
  195. ///
  196. /// In some situations where there are shared subnets (i.e. two different
  197. /// subnets are available on the same physical link), there is only one
  198. /// relay that handles incoming requests from clients. In such a case,
  199. /// the usual subnet selection criteria based on relay belonging to the
  200. /// subnet being selected are no longer sufficient and we need to explicitly
  201. /// specify a relay. One notable example of such uncommon, but valid
  202. /// scenario is a cable network, where there is only one CMTS (one relay),
  203. /// but there are 2 distinct subnets behind it: one for cable modems
  204. /// and another one for CPEs and other user equipment behind modems.
  205. /// From manageability perspective, it is essential that modems get addresses
  206. /// from different subnet, so users won't tinker with their modems.
  207. ///
  208. /// Setting this parameter is not needed in most deployments.
  209. /// This structure holds IP address only for now, but it is expected to
  210. /// be extended in the future.
  211. ///
  212. /// @param relay structure that contains relay information
  213. void setRelayInfo(const isc::dhcp::Subnet::RelayInfo& relay);
  214. /// @brief Returns const reference to relay information
  215. ///
  216. /// @note The returned reference is only valid as long as the object
  217. /// returned it is valid.
  218. ///
  219. /// @return const reference to the relay information
  220. const isc::dhcp::Subnet::RelayInfo& getRelayInfo() {
  221. return (relay_);
  222. }
  223. /// @brief checks whether this subnet supports client that belongs to
  224. /// specified classes.
  225. ///
  226. /// This method checks whether a client that belongs to given classes can
  227. /// use this subnet. For example, if this class is reserved for client
  228. /// class "foo" and the client belongs to classes "foo", "bar" and "baz",
  229. /// it is supported. On the other hand, client belonging to classes
  230. /// "foobar" and "zyxxy" is not supported.
  231. ///
  232. /// @todo: Currently the logic is simple: client is supported if it belongs
  233. /// to any class mentioned in white_list_. We will eventually need a
  234. /// way to specify more fancy logic (e.g. to meet all classes, not just
  235. /// any)
  236. ///
  237. /// @param client_classes list of all classes the client belongs to
  238. /// @return true if client can be supported, false otherwise
  239. bool
  240. clientSupported(const isc::dhcp::ClientClasses& client_classes) const;
  241. /// @brief adds class class_name to the list of supported classes
  242. ///
  243. /// Also see explanation note in @ref white_list_.
  244. ///
  245. /// @param class_name client class to be supported by this subnet
  246. void
  247. allowClientClass(const isc::dhcp::ClientClass& class_name);
  248. protected:
  249. /// @brief Returns all pools (non-const variant)
  250. ///
  251. /// The reference is only valid as long as the object that returned it.
  252. ///
  253. /// @param type lease type to be set
  254. /// @return a collection of all pools
  255. PoolCollection& getPoolsWritable(Lease::Type type);
  256. /// @brief Protected constructor
  257. //
  258. /// By making the constructor protected, we make sure that no one will
  259. /// ever instantiate that class. Subnet4 and Subnet6 should be used instead.
  260. ///
  261. /// This constructor assigns a new subnet-id (see @ref generateNextID).
  262. /// This subnet-id has unique value that is strictly monotonously increasing
  263. /// for each subnet, until it is explicitly reset back to 1 during
  264. /// reconfiguration process.
  265. ///
  266. /// @param prefix subnet prefix
  267. /// @param len prefix length for the subnet
  268. /// @param t1 T1 (renewal-time) timer, expressed in seconds
  269. /// @param t2 T2 (rebind-time) timer, expressed in seconds
  270. /// @param valid_lifetime valid lifetime of leases in this subnet (in seconds)
  271. /// @param relay optional relay information (currently with address only)
  272. /// @param id arbitraty subnet id, value of 0 triggers autogeneration
  273. /// of subnet id
  274. Subnet(const isc::asiolink::IOAddress& prefix, uint8_t len,
  275. const Triplet<uint32_t>& t1,
  276. const Triplet<uint32_t>& t2,
  277. const Triplet<uint32_t>& valid_lifetime,
  278. const isc::dhcp::Subnet::RelayInfo& relay,
  279. const SubnetID id);
  280. /// @brief virtual destructor
  281. ///
  282. /// A virtual destructor is needed because other classes
  283. /// derive from this class.
  284. virtual ~Subnet() { };
  285. /// @brief keeps the subnet-id value
  286. ///
  287. /// It is inreased every time a new Subnet object is created. It is reset
  288. /// (@ref resetSubnetID) every time reconfiguration
  289. /// occurs.
  290. ///
  291. /// Static value initialized in subnet.cc.
  292. static SubnetID static_id_;
  293. /// @brief returns the next unique Subnet-ID
  294. ///
  295. /// This method generates and returns the next unique subnet-id.
  296. /// It is a strictly monotonously increasing value (1,2,3,...) for
  297. /// each new Subnet object created. It can be explicitly reset
  298. /// back to 1 during reconfiguration (@ref resetSubnetID).
  299. ///
  300. /// @return the next unique Subnet-ID
  301. static SubnetID generateNextID() {
  302. return (static_id_++);
  303. }
  304. /// @brief Checks if used pool type is valid
  305. ///
  306. /// Allowed type for Subnet4 is Pool::TYPE_V4.
  307. /// Allowed types for Subnet6 are Pool::TYPE_{IA,TA,PD}.
  308. /// This method is implemented in derived classes.
  309. ///
  310. /// @param type type to be checked
  311. /// @throw BadValue if invalid value is used
  312. virtual void checkType(Lease::Type type) const = 0;
  313. /// @brief subnet-id
  314. ///
  315. /// Subnet-id is a unique value that can be used to find or identify
  316. /// a Subnet4 or Subnet6.
  317. SubnetID id_;
  318. /// @brief collection of IPv4 or non-temporary IPv6 pools in that subnet
  319. PoolCollection pools_;
  320. /// @brief collection of IPv6 temporary address pools in that subnet
  321. PoolCollection pools_ta_;
  322. /// @brief collection of IPv6 prefix pools in that subnet
  323. PoolCollection pools_pd_;
  324. /// @brief a prefix of the subnet
  325. isc::asiolink::IOAddress prefix_;
  326. /// @brief a prefix length of the subnet
  327. uint8_t prefix_len_;
  328. /// @brief a tripet (min/default/max) holding allowed renew timer values
  329. Triplet<uint32_t> t1_;
  330. /// @brief a tripet (min/default/max) holding allowed rebind timer values
  331. Triplet<uint32_t> t2_;
  332. /// @brief a tripet (min/default/max) holding allowed valid lifetime values
  333. Triplet<uint32_t> valid_;
  334. /// @brief last allocated address
  335. ///
  336. /// This is the last allocated address that was previously allocated from
  337. /// this particular subnet. Some allocation algorithms (e.g. iterative) use
  338. /// that value, others do not. It should be noted that although the value
  339. /// is usually correct, there are cases when it is invalid, e.g. after
  340. /// removing a pool, restarting or changing allocation algorithms. For
  341. /// that purpose it should be only considered a help that should not be
  342. /// fully trusted.
  343. isc::asiolink::IOAddress last_allocated_ia_;
  344. /// @brief last allocated temporary address
  345. ///
  346. /// See @ref last_allocated_ia_ for details.
  347. isc::asiolink::IOAddress last_allocated_ta_;
  348. /// @brief last allocated IPv6 prefix
  349. ///
  350. /// See @ref last_allocated_ia_ for details.
  351. isc::asiolink::IOAddress last_allocated_pd_;
  352. /// @brief Name of the network interface (if connected directly)
  353. std::string iface_;
  354. /// @brief Relay information
  355. ///
  356. /// See @ref RelayInfo for detailed description. This structure is public,
  357. /// so its fields are easily accessible. Making it protected would bring in
  358. /// the issue of returning references that may become stale after its parent
  359. /// subnet object disappears.
  360. RelayInfo relay_;
  361. /// @brief optional definition of a client class
  362. ///
  363. /// If defined, only clients belonging to that class will be allowed to use
  364. /// this particular subnet. The default value for this is an empty list,
  365. /// which means that any client is allowed, regardless of its class.
  366. ///
  367. /// @todo This is just a single list of allowed classes. We'll also need
  368. /// to add a black-list (only classes on the list are rejected, the rest
  369. /// are allowed). Implementing this will require more fancy parser logic,
  370. /// so it may be a while until we support this.
  371. ClientClasses white_list_;
  372. private:
  373. /// @brief Pointer to the option data configuration for this subnet.
  374. CfgOptionPtr cfg_option_;
  375. };
  376. /// @brief A generic pointer to either Subnet4 or Subnet6 object
  377. typedef boost::shared_ptr<Subnet> SubnetPtr;
  378. /// @brief A configuration holder for IPv4 subnet.
  379. ///
  380. /// This class represents an IPv4 subnet.
  381. class Subnet4 : public Subnet {
  382. public:
  383. /// @brief Constructor with all parameters
  384. ///
  385. /// This constructor calls Subnet::Subnet, where subnet-id is generated.
  386. ///
  387. /// @param prefix Subnet4 prefix
  388. /// @param length prefix length
  389. /// @param t1 renewal timer (in seconds)
  390. /// @param t2 rebind timer (in seconds)
  391. /// @param valid_lifetime preferred lifetime of leases (in seconds)
  392. /// @param id arbitraty subnet id, default value of 0 triggers
  393. /// autogeneration of subnet id
  394. Subnet4(const isc::asiolink::IOAddress& prefix, uint8_t length,
  395. const Triplet<uint32_t>& t1,
  396. const Triplet<uint32_t>& t2,
  397. const Triplet<uint32_t>& valid_lifetime,
  398. const SubnetID id = 0);
  399. /// @brief Sets siaddr for the Subnet4
  400. ///
  401. /// Will be used for siaddr field (the next server) that typically is used
  402. /// as TFTP server. If not specified, the default value of 0.0.0.0 is
  403. /// used.
  404. void setSiaddr(const isc::asiolink::IOAddress& siaddr);
  405. /// @brief Returns siaddr for this subnet
  406. ///
  407. /// @return siaddr value
  408. isc::asiolink::IOAddress getSiaddr() const;
  409. protected:
  410. /// @brief Returns default address for pool selection
  411. /// @return ANY IPv4 address
  412. virtual isc::asiolink::IOAddress default_pool() const {
  413. return (isc::asiolink::IOAddress("0.0.0.0"));
  414. }
  415. /// @brief Checks if used pool type is valid
  416. ///
  417. /// Allowed type for Subnet4 is Pool::TYPE_V4.
  418. ///
  419. /// @param type type to be checked
  420. /// @throw BadValue if invalid value is used
  421. virtual void checkType(Lease::Type type) const;
  422. /// @brief siaddr value for this subnet
  423. isc::asiolink::IOAddress siaddr_;
  424. };
  425. /// @brief A pointer to a Subnet4 object
  426. typedef boost::shared_ptr<Subnet4> Subnet4Ptr;
  427. /// @brief A collection of Subnet6 objects
  428. typedef std::vector<Subnet4Ptr> Subnet4Collection;
  429. /// @brief A configuration holder for IPv6 subnet.
  430. ///
  431. /// This class represents an IPv6 subnet.
  432. class Subnet6 : public Subnet {
  433. public:
  434. /// @brief Constructor with all parameters
  435. ///
  436. /// This constructor calls Subnet::Subnet, where subnet-id is generated.
  437. ///
  438. /// @param prefix Subnet6 prefix
  439. /// @param length prefix length
  440. /// @param t1 renewal timer (in seconds)
  441. /// @param t2 rebind timer (in seconds)
  442. /// @param preferred_lifetime preferred lifetime of leases (in seconds)
  443. /// @param valid_lifetime preferred lifetime of leases (in seconds)
  444. /// @param id arbitraty subnet id, default value of 0 triggers
  445. /// autogeneration of subnet id
  446. Subnet6(const isc::asiolink::IOAddress& prefix, uint8_t length,
  447. const Triplet<uint32_t>& t1,
  448. const Triplet<uint32_t>& t2,
  449. const Triplet<uint32_t>& preferred_lifetime,
  450. const Triplet<uint32_t>& valid_lifetime,
  451. const SubnetID id = 0);
  452. /// @brief Returns preverred lifetime (in seconds)
  453. ///
  454. /// @return a triplet with preferred lifetime
  455. Triplet<uint32_t> getPreferred() const {
  456. return (preferred_);
  457. }
  458. /// @brief sets interface-id option (if defined)
  459. ///
  460. /// @param ifaceid pointer to interface-id option
  461. void setInterfaceId(const OptionPtr& ifaceid) {
  462. interface_id_ = ifaceid;
  463. }
  464. /// @brief returns interface-id value (if specified)
  465. /// @return interface-id option (if defined)
  466. OptionPtr getInterfaceId() const {
  467. return interface_id_;
  468. }
  469. protected:
  470. /// @brief Returns default address for pool selection
  471. /// @return ANY IPv6 address
  472. virtual isc::asiolink::IOAddress default_pool() const {
  473. return (isc::asiolink::IOAddress("::"));
  474. }
  475. /// @brief Checks if used pool type is valid
  476. ///
  477. /// allowed types for Subnet6 are Pool::TYPE_{IA,TA,PD}.
  478. ///
  479. /// @param type type to be checked
  480. /// @throw BadValue if invalid value is used
  481. virtual void checkType(Lease::Type type) const;
  482. /// @brief specifies optional interface-id
  483. OptionPtr interface_id_;
  484. /// @brief a triplet with preferred lifetime (in seconds)
  485. Triplet<uint32_t> preferred_;
  486. };
  487. /// @brief A pointer to a Subnet6 object
  488. typedef boost::shared_ptr<Subnet6> Subnet6Ptr;
  489. /// @brief A collection of Subnet6 objects
  490. typedef std::vector<Subnet6Ptr> Subnet6Collection;
  491. } // end of isc::dhcp namespace
  492. } // end of isc namespace
  493. #endif // SUBNET_H