lease_cmds.cc 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605
  1. // Copyright (C) 2017 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. #include <config.h>
  7. #include <lease_cmds.h>
  8. #include <config/command_mgr.h>
  9. #include <lease_parser.h>
  10. #include <cc/command_interpreter.h>
  11. #include <cc/data.h>
  12. #include <asiolink/io_address.h>
  13. #include <lease_cmds_log.h>
  14. #include <dhcpsrv/cfgmgr.h>
  15. #include <dhcpsrv/lease_mgr.h>
  16. #include <dhcpsrv/lease_mgr_factory.h>
  17. #include <dhcpsrv/subnet_id.h>
  18. #include <dhcp/duid.h>
  19. #include <util/encode/hex.h>
  20. #include <util/strutil.h>
  21. #include <exceptions/exceptions.h>
  22. #include <boost/bind.hpp>
  23. #include <string>
  24. using namespace isc::dhcp;
  25. using namespace isc::data;
  26. using namespace isc::config;
  27. using namespace isc::asiolink;
  28. using namespace std;
  29. namespace isc {
  30. namespace lease_cmds {
  31. /// @brief Wrapper class around reservation command handlers.
  32. class LeaseCmdsImpl {
  33. public:
  34. LeaseCmdsImpl();
  35. ~LeaseCmdsImpl();
  36. /// @brief Parameters specified for reservation-get and reservation-del
  37. ///
  38. /// As both call types (get and delete) need specify which reservation to
  39. /// act on, they have the same set of parameters. In particular, those
  40. /// two call types support the following sets of parameters:
  41. /// - address
  42. /// - subnet-id, identifier-type, identifier-value (v4)
  43. /// - subnet-id, lease-type, iaid, identifier-type, identifier-value (v6)
  44. ///
  45. /// This class stores those parameters and is used to pass them around.
  46. class Parameters {
  47. public:
  48. /// @brief specifies type of query (by IP addr, by hwaddr, by DUID)
  49. typedef enum {
  50. TYPE_ADDR, ///< query by IP address (either v4 or v6)
  51. TYPE_HWADDR, ///< query by hardware address (v4 only)
  52. TYPE_DUID ///< query by DUID (v6 only)
  53. } Type;
  54. /// @brief Specifies subnet-id (always used)
  55. SubnetID subnet_id;
  56. /// @brief Specifies IPv4/v6 address (used when query_type is TYPE_ADDR)
  57. IOAddress addr;
  58. /// @brief Specifies hardware address (used when query_type is TYPE_HWADDR)
  59. HWAddrPtr hwaddr;
  60. /// @brief Specifies identifier value (used when query_type is TYPE_DUID)
  61. isc::dhcp::DuidPtr duid;
  62. /// @brief Attempts to covert text to one of specified types
  63. ///
  64. /// Supported values are: "address", hw-address and duid.
  65. ///
  66. /// @param txt text to be converted
  67. /// @return value converted to Parameters::Type
  68. /// @throw BadValue if unsupported type is specified
  69. static Type txtToType(const std::string& txt) {
  70. if (txt == "address") {
  71. return (Parameters::TYPE_ADDR);
  72. } else if (txt == "hw-address") {
  73. return (Parameters::TYPE_HWADDR);
  74. } else if (txt == "duid") {
  75. return (Parameters::TYPE_DUID);
  76. } else {
  77. isc_throw(BadValue, "Incorrect identifier type: "
  78. << txt << ", the only supported values are: "
  79. "address, hw-address, duid");
  80. }
  81. }
  82. /// @brief specifies parameter types (true = query by address, false =
  83. /// query by identifier-type,identifier)
  84. Type query_type;
  85. /// @brief Lease type (NA,TA or PD) used for v6 leases
  86. Lease::Type lease_type;
  87. /// @brief IAID identifier used for v6 leases
  88. uint32_t iaid;
  89. /// @brief Default constructor.
  90. Parameters()
  91. :addr("::"), query_type(TYPE_ADDR), lease_type(Lease::TYPE_NA),
  92. iaid(0) {
  93. }
  94. };
  95. private:
  96. /// @brief Registers commands:
  97. ///
  98. /// Registers:
  99. /// - lease4-add
  100. /// - lease6-add
  101. /// - lease4-get
  102. /// - lease6-get
  103. /// - lease4-del
  104. /// - lease6-del
  105. /// - lease4-update
  106. /// - lease6-update
  107. /// - lease4-del-all
  108. /// - lease6-del-all
  109. /// @throw Unexpected if CommandMgr is not available (should not happen)
  110. void registerCommands();
  111. /// @brief Deregisters commands:
  112. ///
  113. /// Deregisters:
  114. /// - lease4-add
  115. /// - lease6-add
  116. /// - lease4-get
  117. /// - lease6-get
  118. /// - lease4-del
  119. /// - lease6-del
  120. /// - lease4-update
  121. /// - lease6-update
  122. /// - lease4-del-all
  123. /// - lease6-del-all
  124. ///
  125. /// @throw Unexpected if CommandMgr is not available (should not happen)
  126. void deregisterCommands();
  127. /// @brief lease4-add, lease6-add command handler
  128. ///
  129. /// This command attempts to add a lease.
  130. ///
  131. /// An example full command looks as follows. Note that the args
  132. /// parameter is expected to contain the "arguments" portion of it.
  133. /// This function covers both v4 and v6 leases.
  134. ///
  135. /// Example command for v4:
  136. /// {
  137. /// "command": "lease4-add",
  138. /// "parameters": {
  139. /// "address": "192.0.2.1",
  140. /// "hwaddr": "00:01:02:03:04:05",
  141. /// "client-id": "this-is-a-client",
  142. /// "valid-lft": 3600,
  143. /// "expire": 12345678,
  144. /// "subnet-id": 1,
  145. /// "fqdn-fwd": true,
  146. /// "fqdn-rev": true,
  147. /// "hostname": "myhost.example.org",
  148. /// "state": 0
  149. /// }
  150. /// }
  151. /// Example command for v6:
  152. /// {
  153. /// "command": "lease6-add",
  154. /// "arguments": {
  155. /// "subnet-id": 66,
  156. /// "ip-address": "2001:db8:abcd::",
  157. /// "type": "IA_PD",
  158. /// "prefix-len": 48,
  159. /// "duid": "01:02:03:04:05:06:07:08",
  160. /// "iaid": 1234,
  161. /// "preferred-lft": 500,
  162. /// "valid-lft": 1000,
  163. /// "expire": 12345678,
  164. /// "fqdn-fwd": true,
  165. /// "fqdn-rev": true,
  166. /// "hostname": "urania.example.org""
  167. /// }
  168. /// }
  169. ///
  170. /// @param command should be 'lease4-add' or 'lease6-add'
  171. /// @param args must contain host reservation definition.
  172. /// @return result of the operation
  173. static ConstElementPtr
  174. leaseAddHandler(const string& command, ConstElementPtr args);
  175. /// @brief lease4-get, lease6-get command handler
  176. ///
  177. /// This command attempts to retrieve a lease that match selected criteria.
  178. /// The following types of parameters are supported:
  179. /// - (subnet-id, address) for both v4 and v6
  180. /// - (subnet-id, identifier-type, identifier) for v4
  181. /// - (subnet-id, type, iana, identifier-type, identifier) for v6
  182. ///
  183. /// Example command for query by (subnet-id, address):
  184. /// {
  185. /// "command": "lease4-get",
  186. /// "arguments": {
  187. /// "subnet-id": 1,
  188. /// "ip-address": "192.0.2.202"
  189. /// }
  190. /// }
  191. ///
  192. /// Example command for query by (subnet-id, identifier-type, identifier)
  193. /// {
  194. /// "command": "lease4-get",
  195. /// "arguments": {
  196. /// "subnet-id": 1,
  197. /// "identifier-type": "hw-address",
  198. /// "identifier": "00:01:02:03:04:05"
  199. /// }
  200. /// }
  201. ///
  202. /// Example command for query by (subnet-id, type, iana, identifier-type,
  203. /// identifier):
  204. /// {
  205. /// "command": "lease6-get",
  206. /// "arguments": {
  207. /// "subnet-id": 66,
  208. /// "iaid": 42,
  209. /// "type": "IA_NA",
  210. /// "identifier-type": "duid",
  211. /// "identifier": "77:77:77:77:77:77:77:77"
  212. /// }
  213. /// }
  214. /// @param command "lease4-get" or "lease6-get"
  215. /// @param args must contain host reservation definition.
  216. /// @return result of the operation (includes lease details, if found)
  217. static ConstElementPtr
  218. leaseGetHandler(const string& command, ConstElementPtr args);
  219. /// @brief lease4-del, lease6-del command handler
  220. ///
  221. /// This command attempts to delete a lease that match selected criteria.
  222. /// The following types of parameters are supported:
  223. /// - (subnet-id, address) for both v4 and v6
  224. /// - (subnet-id, identifier-type, identifier) for v4
  225. /// - (subnet-id, type, iana, identifier-type, identifier) for v6
  226. ///
  227. /// Example command for command by (subnet-id, address):
  228. /// {
  229. /// "command": "lease4-del",
  230. /// "arguments": {
  231. /// "subnet-id": 1,
  232. /// "ip-address": "192.0.2.202"
  233. /// }
  234. /// }
  235. ///
  236. /// Example command for query by (subnet-id, identifier-type, identifier)
  237. /// {
  238. /// "command": "lease4-del",
  239. /// "arguments": {
  240. /// "subnet-id": 1,
  241. /// "identifier-type": "hw-address",
  242. /// "identifier": "00:01:02:03:04:05"
  243. /// }
  244. /// }
  245. ///
  246. /// Example command for query by (subnet-id, type, iana, identifier-type,
  247. /// identifier):
  248. /// {
  249. /// "command": "lease6-del",
  250. /// "arguments": {
  251. /// "subnet-id": 66,
  252. /// "iaid": 42,
  253. /// "type": "IA_NA",
  254. /// "identifier-type": "duid",
  255. /// "identifier": "77:77:77:77:77:77:77:77"
  256. /// }
  257. /// }
  258. /// @param command 'lease4-del' or 'lease6-del'
  259. /// @param args must contain lease parameters
  260. /// @return result of the operation
  261. static ConstElementPtr
  262. leaseDelHandler(const string& command, ConstElementPtr args);
  263. /// @brief Not implemented yet.
  264. static ConstElementPtr
  265. leaseUpdateHandler(const string& command, ConstElementPtr args);
  266. /// @brief Not implemented yet.
  267. static ConstElementPtr
  268. leaseWipeHandler(const string& command, ConstElementPtr args);
  269. /// @brief Extracts parameters required for reservation-get and reservation-del
  270. ///
  271. /// See @ref Parameters class for detailed description of what is expected
  272. /// in the args structure.
  273. ///
  274. /// @param args - arguments passed to command
  275. /// @return parsed parameters
  276. /// @throw BadValue if input arguments don't make sense.
  277. static Parameters getParameters(const ConstElementPtr& args);
  278. };
  279. LeaseCmdsImpl::LeaseCmdsImpl() {
  280. registerCommands();
  281. }
  282. LeaseCmdsImpl::~LeaseCmdsImpl() {
  283. deregisterCommands();
  284. }
  285. void LeaseCmdsImpl::registerCommands() {
  286. /// @todo: Use registration mechanism once #5314 is merged.
  287. /// See #5321 discussion.
  288. CommandMgr::instance().registerCommand("lease4-add",
  289. boost::bind(&LeaseCmdsImpl::leaseAddHandler, _1, _2));
  290. CommandMgr::instance().registerCommand("lease6-add",
  291. boost::bind(&LeaseCmdsImpl::leaseAddHandler, _1, _2));
  292. CommandMgr::instance().registerCommand("lease4-get",
  293. boost::bind(&LeaseCmdsImpl::leaseGetHandler, _1, _2));
  294. CommandMgr::instance().registerCommand("lease6-get",
  295. boost::bind(&LeaseCmdsImpl::leaseGetHandler, _1, _2));
  296. CommandMgr::instance().registerCommand("lease4-del",
  297. boost::bind(&LeaseCmdsImpl::leaseDelHandler, _1, _2));
  298. CommandMgr::instance().registerCommand("lease6-del",
  299. boost::bind(&LeaseCmdsImpl::leaseDelHandler, _1, _2));
  300. CommandMgr::instance().registerCommand("lease4-update",
  301. boost::bind(&LeaseCmdsImpl::leaseUpdateHandler, _1, _2));
  302. CommandMgr::instance().registerCommand("lease6-update",
  303. boost::bind(&LeaseCmdsImpl::leaseUpdateHandler, _1, _2));
  304. CommandMgr::instance().registerCommand("lease4-del-all",
  305. boost::bind(&LeaseCmdsImpl::leaseWipeHandler, _1, _2));
  306. CommandMgr::instance().registerCommand("lease6-del-all",
  307. boost::bind(&LeaseCmdsImpl::leaseWipeHandler, _1, _2));
  308. }
  309. void LeaseCmdsImpl::deregisterCommands() {
  310. /// @todo: Use deregistration mechanism once #5321 discussion is done
  311. CommandMgr::instance().deregisterCommand("lease4-add");
  312. CommandMgr::instance().deregisterCommand("lease6-add");
  313. CommandMgr::instance().deregisterCommand("lease4-get");
  314. CommandMgr::instance().deregisterCommand("lease6-get");
  315. CommandMgr::instance().deregisterCommand("lease4-del");
  316. CommandMgr::instance().deregisterCommand("lease6-del");
  317. CommandMgr::instance().deregisterCommand("lease4-update");
  318. CommandMgr::instance().deregisterCommand("lease6-update");
  319. CommandMgr::instance().deregisterCommand("lease4-del-all");
  320. CommandMgr::instance().deregisterCommand("lease6-del-all");
  321. }
  322. ConstElementPtr
  323. LeaseCmdsImpl::leaseAddHandler(const std::string& name,
  324. ConstElementPtr params) {
  325. bool v4 = (name == "lease4-add");
  326. string txt = "(missing parameters)";
  327. if (params) {
  328. txt = params->str();
  329. }
  330. try {
  331. if (!params) {
  332. isc_throw(isc::BadValue, "no parameters specified for the command");
  333. }
  334. ConstSrvConfigPtr config = CfgMgr::instance().getCurrentCfg();
  335. Lease4Ptr lease4;
  336. Lease6Ptr lease6;
  337. if (v4) {
  338. Lease4Parser parser;
  339. lease4 = parser.parse(config, params);
  340. // checkLeaseIntegrity(config, lease4);
  341. if (lease4) {
  342. LeaseMgrFactory::instance().addLease(lease4);
  343. }
  344. } else {
  345. Lease6Parser parser;
  346. lease6 = parser.parse(config, params);
  347. // checkLeaseIntegrity(config, lease6);
  348. if (lease6) {
  349. LeaseMgrFactory::instance().addLease(lease6);
  350. }
  351. }
  352. } catch (const std::exception& ex) {
  353. LOG_ERROR(lease_cmds_logger, v4 ? LEASE_CMDS_ADD4_FAILED : LEASE_CMDS_ADD6_FAILED)
  354. .arg(txt)
  355. .arg(ex.what());
  356. return (createAnswer(CONTROL_RESULT_ERROR, ex.what()));
  357. }
  358. LOG_INFO(lease_cmds_logger,
  359. v4 ? LEASE_CMDS_ADD4 : LEASE_CMDS_ADD6).arg(txt);
  360. return (createAnswer(CONTROL_RESULT_SUCCESS, "Lease added."));
  361. }
  362. LeaseCmdsImpl::Parameters
  363. LeaseCmdsImpl::getParameters(const ConstElementPtr& params) {
  364. Parameters x;
  365. if (!params || params->getType() != Element::map) {
  366. isc_throw(BadValue, "Parameters missing or are not a map.");
  367. }
  368. // We support several sets of parameters for leaseX-get/lease-del:
  369. // lease-get(type, address)
  370. // lease-get(type, subnet-id, identifier-type, identifier)
  371. if (params->contains("type")) {
  372. string t = params->get("type")->stringValue();
  373. if (t == "IA_NA" || t == "0") {
  374. x.lease_type = Lease::TYPE_NA;
  375. } else if (t == "IA_TA" || t == "1") {
  376. x.lease_type = Lease::TYPE_TA;
  377. } else if (t == "IA_PD" || t == "2") {
  378. x.lease_type = Lease::TYPE_PD;
  379. } else if (t == "V4" || t == "3") {
  380. x.lease_type = Lease::TYPE_V4;
  381. } else {
  382. isc_throw(BadValue, "Invalid lease type specified: "
  383. << t << ", only supported values are: IA_NA, IA_TA,"
  384. << " IA_PD and V4");
  385. }
  386. }
  387. ConstElementPtr tmp = params->get("ip-address");
  388. if (tmp) {
  389. if (tmp->getType() != Element::string) {
  390. isc_throw(BadValue, "'ip-address' is not a string.");
  391. }
  392. x.addr = IOAddress(tmp->stringValue());
  393. x.query_type = Parameters::TYPE_ADDR;
  394. return (x);
  395. }
  396. tmp = params->get("subnet-id");
  397. if (!tmp) {
  398. isc_throw(BadValue, "Mandatory 'subnet-id' parameter missing.");
  399. }
  400. if (tmp->getType() != Element::integer) {
  401. isc_throw(BadValue, "'subnet-id' parameter is not integer.");
  402. }
  403. x.subnet_id = tmp->intValue();
  404. if (params->contains("iaid")) {
  405. x.iaid = params->get("iaid")->intValue();
  406. }
  407. // No address specified. Ok, so it must be identifier based query.
  408. // "identifier-type": "duid",
  409. // "identifier": "aa:bb:cc:dd:ee:..."
  410. ConstElementPtr type = params->get("identifier-type");
  411. ConstElementPtr ident = params->get("identifier");
  412. if (!type || type->getType() != Element::string) {
  413. isc_throw(BadValue, "No 'ip-address' provided"
  414. " and 'identifier-type' is either missing or not a string.");
  415. }
  416. if (!ident || ident->getType() != Element::string) {
  417. isc_throw(BadValue, "No 'ip-address' provided"
  418. " and 'identifier' is either missing or not a string.");
  419. }
  420. // Got the parameters. Let's see if their values make sense.
  421. // Try to convert identifier-type
  422. x.query_type = Parameters::txtToType(type->stringValue());
  423. switch (x.query_type) {
  424. case Parameters::TYPE_HWADDR: {
  425. HWAddr hw = HWAddr::fromText(ident->stringValue());
  426. x.hwaddr = HWAddrPtr(new HWAddr(hw));
  427. break;
  428. }
  429. case Parameters::TYPE_DUID: {
  430. DUID duid = DUID::fromText(ident->stringValue());
  431. x.duid = DuidPtr(new DUID(duid));
  432. break;
  433. }
  434. case Parameters::TYPE_ADDR: {
  435. // We should never get here. The address clause should have been caught
  436. // earlier.
  437. return (x);
  438. }
  439. default: {
  440. isc_throw(BadValue, "Identifier type " << type->stringValue() <<
  441. " is not supported.");
  442. }
  443. }
  444. return (x);
  445. }
  446. ConstElementPtr
  447. LeaseCmdsImpl::leaseGetHandler(const std::string& name, ConstElementPtr params) {
  448. Parameters p;
  449. Lease4Ptr lease4;
  450. Lease6Ptr lease6;
  451. bool v4 = (name == "lease4-get");
  452. try {
  453. p = getParameters(params);
  454. switch (p.query_type) {
  455. case Parameters::TYPE_ADDR: {
  456. // Query by address
  457. if (v4) {
  458. lease4 = LeaseMgrFactory::instance().getLease4(p.addr);
  459. } else {
  460. lease6 = LeaseMgrFactory::instance().getLease6(p.lease_type, p.addr);
  461. }
  462. break;
  463. }
  464. case Parameters::TYPE_HWADDR:
  465. if (v4) {
  466. if (!p.hwaddr) {
  467. return (createAnswer(CONTROL_RESULT_ERROR,
  468. "Program error: Query by hw-address "
  469. "requires hwaddr to be specified"));
  470. }
  471. lease4 = LeaseMgrFactory::instance().getLease4(*p.hwaddr, p.subnet_id);
  472. } else {
  473. return (createAnswer(CONTROL_RESULT_ERROR,
  474. "Query by hw-address is not allowed in v6."));
  475. }
  476. break;
  477. case Parameters::TYPE_DUID:
  478. if (!v4) {
  479. if (!p.duid) {
  480. return (createAnswer(CONTROL_RESULT_ERROR,
  481. "Program error: Query by duid "
  482. "requires duid to be specified"));
  483. }
  484. lease6 = LeaseMgrFactory::instance().getLease6(p.lease_type, *p.duid,
  485. p.iaid, p.subnet_id);
  486. } else {
  487. return (createAnswer(CONTROL_RESULT_ERROR,
  488. "Query by duid is not allowed in v4."));
  489. }
  490. break;
  491. default: {
  492. stringstream tmp;
  493. tmp << "Unknown query type: " << static_cast<int>(p.query_type);
  494. return (createAnswer(CONTROL_RESULT_ERROR, tmp.str()));
  495. }
  496. }
  497. } catch (const std::exception& ex) {
  498. return (createAnswer(CONTROL_RESULT_ERROR, ex.what()));
  499. }
  500. ElementPtr lease_json;
  501. if (v4 && lease4) {
  502. lease_json = lease4->toElement();
  503. return (createAnswer(CONTROL_RESULT_SUCCESS, "IPv4 lease found.", lease_json));
  504. }
  505. if (!v4 && lease6) {
  506. lease_json = lease6->toElement();
  507. return (createAnswer(CONTROL_RESULT_SUCCESS, "IPv6 lease found.", lease_json));
  508. }
  509. // If we got here, the lease has not been found.
  510. return (createAnswer(CONTROL_RESULT_EMPTY, "Lease not found."));
  511. }
  512. ConstElementPtr
  513. LeaseCmdsImpl::leaseDelHandler(const std::string& cmd, ConstElementPtr args) {
  514. return (createAnswer(CONTROL_RESULT_ERROR, "not implemented yet."));
  515. }
  516. ConstElementPtr
  517. LeaseCmdsImpl::leaseUpdateHandler(const string& cmd, ConstElementPtr args) {
  518. return (createAnswer(CONTROL_RESULT_ERROR, "not implemented yet."));
  519. }
  520. ConstElementPtr
  521. LeaseCmdsImpl::leaseWipeHandler(const string& cmd, ConstElementPtr args) {
  522. return (createAnswer(CONTROL_RESULT_ERROR, "not implemented yet."));
  523. }
  524. LeaseCmds::LeaseCmds()
  525. :impl_(new LeaseCmdsImpl()) {
  526. }
  527. LeaseCmds::~LeaseCmds() {
  528. impl_.reset();
  529. }
  530. };
  531. };