dhcp6_srv.cc 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263
  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. #include <config.h>
  15. #include <asiolink/io_address.h>
  16. #include <dhcp/dhcp6.h>
  17. #include <dhcp/duid.h>
  18. #include <dhcp/iface_mgr.h>
  19. #include <dhcp/libdhcp++.h>
  20. #include <dhcp/option6_addrlst.h>
  21. #include <dhcp/option6_ia.h>
  22. #include <dhcp/option6_iaaddr.h>
  23. #include <dhcp/option6_iaaddr.h>
  24. #include <dhcp/option_custom.h>
  25. #include <dhcp/option_int_array.h>
  26. #include <dhcp/pkt6.h>
  27. #include <dhcp6/dhcp6_log.h>
  28. #include <dhcp6/dhcp6_srv.h>
  29. #include <dhcpsrv/cfgmgr.h>
  30. #include <dhcpsrv/lease_mgr.h>
  31. #include <dhcpsrv/lease_mgr_factory.h>
  32. #include <dhcpsrv/subnet.h>
  33. #include <dhcpsrv/utils.h>
  34. #include <exceptions/exceptions.h>
  35. #include <util/io_utilities.h>
  36. #include <util/range_utilities.h>
  37. #include <util/encode/hex.h>
  38. #include <hooks/hooks_manager.h>
  39. #include <hooks/callout_handle.h>
  40. #include <boost/foreach.hpp>
  41. #include <boost/tokenizer.hpp>
  42. #include <boost/algorithm/string/erase.hpp>
  43. #include <stdlib.h>
  44. #include <time.h>
  45. #include <iomanip>
  46. #include <fstream>
  47. using namespace isc;
  48. using namespace isc::asiolink;
  49. using namespace isc::dhcp;
  50. using namespace isc::hooks;
  51. using namespace isc::util;
  52. using namespace std;
  53. namespace {
  54. /// Structure that holds registered hook indexes
  55. struct Dhcp6Hooks {
  56. int hook_index_pkt6_receive_; ///< index for "pkt6_receive" hook point
  57. int hook_index_subnet6_select_; ///< index for "subnet6_select" hook point
  58. int hook_index_pkt6_send_; ///< index for "pkt6_send" hook point
  59. /// Constructor that registers hook points for DHCPv6 engine
  60. Dhcp6Hooks() {
  61. hook_index_pkt6_receive_ = HooksManager::registerHook("pkt6_receive");
  62. hook_index_subnet6_select_ = HooksManager::registerHook("subnet6_select");
  63. hook_index_pkt6_send_ = HooksManager::registerHook("pkt6_send");
  64. }
  65. };
  66. // Declare a Hooks object. As this is outside any function or method, it
  67. // will be instantiated (and the constructor run) when the module is loaded.
  68. // As a result, the hook indexes will be defined before any method in this
  69. // module is called.
  70. Dhcp6Hooks Hooks;
  71. }; // anonymous namespace
  72. namespace isc {
  73. namespace dhcp {
  74. /// @brief file name of a server-id file
  75. ///
  76. /// Server must store its duid in persistent storage that must not change
  77. /// between restarts. This is name of the file that is created in dataDir
  78. /// (see isc::dhcp::CfgMgr::getDataDir()). It is a text file that uses
  79. /// double digit hex values separated by colons format, e.g.
  80. /// 01:ff:02:03:06:80:90:ab:cd:ef. Server will create it during first
  81. /// run and then use it afterwards.
  82. static const char* SERVER_DUID_FILE = "b10-dhcp6-serverid";
  83. Dhcpv6Srv::Dhcpv6Srv(uint16_t port)
  84. :alloc_engine_(), serverid_(), shutdown_(true), hook_index_pkt6_receive_(-1),
  85. hook_index_subnet6_select_(-1), hook_index_pkt6_send_(-1)
  86. {
  87. LOG_DEBUG(dhcp6_logger, DBG_DHCP6_START, DHCP6_OPEN_SOCKET).arg(port);
  88. // Initialize objects required for DHCP server operation.
  89. try {
  90. // Port 0 is used for testing purposes. It means that the server should
  91. // not open any sockets at all. Some tests, e.g. configuration parser,
  92. // require Dhcpv6Srv object, but they don't really need it to do
  93. // anything. This speed up and simplifies the tests.
  94. if (port > 0) {
  95. if (IfaceMgr::instance().countIfaces() == 0) {
  96. LOG_ERROR(dhcp6_logger, DHCP6_NO_INTERFACES);
  97. return;
  98. }
  99. IfaceMgr::instance().openSockets6(port);
  100. }
  101. string duid_file = CfgMgr::instance().getDataDir() + "/" + string(SERVER_DUID_FILE);
  102. if (loadServerID(duid_file)) {
  103. LOG_DEBUG(dhcp6_logger, DBG_DHCP6_START, DHCP6_SERVERID_LOADED)
  104. .arg(duidToString(getServerID()))
  105. .arg(duid_file);
  106. } else {
  107. generateServerID();
  108. LOG_INFO(dhcp6_logger, DHCP6_SERVERID_GENERATED)
  109. .arg(duidToString(getServerID()))
  110. .arg(duid_file);
  111. if (!writeServerID(duid_file)) {
  112. LOG_WARN(dhcp6_logger, DHCP6_SERVERID_WRITE_FAIL)
  113. .arg(duid_file);
  114. }
  115. }
  116. // Instantiate allocation engine
  117. alloc_engine_.reset(new AllocEngine(AllocEngine::ALLOC_ITERATIVE, 100));
  118. // Register hook points
  119. hook_index_pkt6_receive_ = Hooks.hook_index_pkt6_receive_;
  120. hook_index_subnet6_select_ = Hooks.hook_index_subnet6_select_;
  121. hook_index_pkt6_send_ = Hooks.hook_index_pkt6_send_;
  122. /// @todo call loadLibraries() when handling configuration changes
  123. vector<string> libraries; // no libraries at this time
  124. HooksManager::loadLibraries(libraries);
  125. } catch (const std::exception &e) {
  126. LOG_ERROR(dhcp6_logger, DHCP6_SRV_CONSTRUCT_ERROR).arg(e.what());
  127. return;
  128. }
  129. // All done, so can proceed
  130. shutdown_ = false;
  131. }
  132. Dhcpv6Srv::~Dhcpv6Srv() {
  133. IfaceMgr::instance().closeSockets();
  134. LeaseMgrFactory::destroy();
  135. }
  136. void Dhcpv6Srv::shutdown() {
  137. LOG_DEBUG(dhcp6_logger, DBG_DHCP6_BASIC, DHCP6_SHUTDOWN_REQUEST);
  138. shutdown_ = true;
  139. }
  140. Pkt6Ptr Dhcpv6Srv::receivePacket(int timeout) {
  141. return (IfaceMgr::instance().receive6(timeout));
  142. }
  143. void Dhcpv6Srv::sendPacket(const Pkt6Ptr& packet) {
  144. IfaceMgr::instance().send(packet);
  145. }
  146. bool Dhcpv6Srv::run() {
  147. while (!shutdown_) {
  148. /// @todo Calculate actual timeout to the next event (e.g. lease
  149. /// expiration) once we have lease database. The idea here is that
  150. /// it is possible to do everything in a single process/thread.
  151. /// For now, we are just calling select for 1000 seconds. There
  152. /// were some issues reported on some systems when calling select()
  153. /// with too large values. Unfortunately, I don't recall the details.
  154. //cppcheck-suppress variableScope This is temporary anyway
  155. const int timeout = 1000;
  156. // client's message and server's response
  157. Pkt6Ptr query;
  158. Pkt6Ptr rsp;
  159. try {
  160. query = receivePacket(timeout);
  161. } catch (const std::exception& e) {
  162. LOG_ERROR(dhcp6_logger, DHCP6_PACKET_RECEIVE_FAIL).arg(e.what());
  163. }
  164. if (query) {
  165. if (!query->unpack()) {
  166. LOG_DEBUG(dhcp6_logger, DBG_DHCP6_DETAIL,
  167. DHCP6_PACKET_PARSE_FAIL);
  168. continue;
  169. }
  170. LOG_DEBUG(dhcp6_logger, DBG_DHCP6_DETAIL, DHCP6_PACKET_RECEIVED)
  171. .arg(query->getName());
  172. LOG_DEBUG(dhcp6_logger, DBG_DHCP6_DETAIL_DATA, DHCP6_QUERY_DATA)
  173. .arg(static_cast<int>(query->getType()))
  174. .arg(query->getBuffer().getLength())
  175. .arg(query->toText());
  176. // Let's execute all callouts registered for packet_received
  177. if (HooksManager::getHooksManager().calloutsPresent(hook_index_pkt6_receive_)) {
  178. CalloutHandlePtr callout_handle = getCalloutHandle(query);
  179. // Delete previously set arguments
  180. callout_handle->deleteAllArguments();
  181. // Pass incoming packet as argument
  182. callout_handle->setArgument("query6", query);
  183. // Call callouts
  184. HooksManager::getHooksManager().callCallouts(hook_index_pkt6_receive_,
  185. *callout_handle);
  186. // Callouts decided to skip the next processing step. The next
  187. // processing step would to process the packet, so skip at this
  188. // stage means drop.
  189. if (callout_handle->getSkip()) {
  190. LOG_DEBUG(dhcp6_logger, DBG_DHCP6_HOOKS, DHCP6_HOOK_PACKET_RCVD_SKIP);
  191. continue;
  192. }
  193. callout_handle->getArgument("query6", query);
  194. }
  195. try {
  196. switch (query->getType()) {
  197. case DHCPV6_SOLICIT:
  198. rsp = processSolicit(query);
  199. break;
  200. case DHCPV6_REQUEST:
  201. rsp = processRequest(query);
  202. break;
  203. case DHCPV6_RENEW:
  204. rsp = processRenew(query);
  205. break;
  206. case DHCPV6_REBIND:
  207. rsp = processRebind(query);
  208. break;
  209. case DHCPV6_CONFIRM:
  210. rsp = processConfirm(query);
  211. break;
  212. case DHCPV6_RELEASE:
  213. rsp = processRelease(query);
  214. break;
  215. case DHCPV6_DECLINE:
  216. rsp = processDecline(query);
  217. break;
  218. case DHCPV6_INFORMATION_REQUEST:
  219. rsp = processInfRequest(query);
  220. break;
  221. default:
  222. // Only action is to output a message if debug is enabled,
  223. // and that will be covered by the debug statement before
  224. // the "switch" statement.
  225. ;
  226. }
  227. } catch (const RFCViolation& e) {
  228. LOG_DEBUG(dhcp6_logger, DBG_DHCP6_BASIC, DHCP6_REQUIRED_OPTIONS_CHECK_FAIL)
  229. .arg(query->getName())
  230. .arg(query->getRemoteAddr().toText())
  231. .arg(e.what());
  232. } catch (const isc::Exception& e) {
  233. // Catch-all exception (at least for ones based on the isc
  234. // Exception class, which covers more or less all that
  235. // are explicitly raised in the BIND 10 code). Just log
  236. // the problem and ignore the packet. (The problem is logged
  237. // as a debug message because debug is disabled by default -
  238. // it prevents a DDOS attack based on the sending of problem
  239. // packets.)
  240. LOG_DEBUG(dhcp6_logger, DBG_DHCP6_BASIC, DHCP6_PACKET_PROCESS_FAIL)
  241. .arg(query->getName())
  242. .arg(query->getRemoteAddr().toText())
  243. .arg(e.what());
  244. }
  245. if (rsp) {
  246. rsp->setRemoteAddr(query->getRemoteAddr());
  247. rsp->setLocalAddr(query->getLocalAddr());
  248. rsp->setRemotePort(DHCP6_CLIENT_PORT);
  249. rsp->setLocalPort(DHCP6_SERVER_PORT);
  250. rsp->setIndex(query->getIndex());
  251. rsp->setIface(query->getIface());
  252. // Execute all callouts registered for packet6_send
  253. if (HooksManager::getHooksManager().calloutsPresent(hook_index_pkt6_send_)) {
  254. CalloutHandlePtr callout_handle = getCalloutHandle(query);
  255. // Delete all previous arguments
  256. callout_handle->deleteAllArguments();
  257. // Clear skip flag if it was set in previous callouts
  258. callout_handle->setSkip(false);
  259. // Set our response
  260. callout_handle->setArgument("response6", rsp);
  261. // Call all installed callouts
  262. HooksManager::getHooksManager().callCallouts(hook_index_pkt6_send_,
  263. *callout_handle);
  264. // Callouts decided to skip the next processing step. The next
  265. // processing step would to send the packet, so skip at this
  266. // stage means "drop response".
  267. if (callout_handle->getSkip()) {
  268. LOG_DEBUG(dhcp6_logger, DBG_DHCP6_HOOKS, DHCP6_HOOK_PACKET_SEND_SKIP);
  269. continue;
  270. }
  271. }
  272. LOG_DEBUG(dhcp6_logger, DBG_DHCP6_DETAIL_DATA,
  273. DHCP6_RESPONSE_DATA)
  274. .arg(static_cast<int>(rsp->getType())).arg(rsp->toText());
  275. if (rsp->pack()) {
  276. try {
  277. sendPacket(rsp);
  278. } catch (const std::exception& e) {
  279. LOG_ERROR(dhcp6_logger, DHCP6_PACKET_SEND_FAIL).arg(e.what());
  280. }
  281. } else {
  282. LOG_ERROR(dhcp6_logger, DHCP6_PACK_FAIL);
  283. }
  284. }
  285. }
  286. }
  287. return (true);
  288. }
  289. bool Dhcpv6Srv::loadServerID(const std::string& file_name) {
  290. // load content of the file into a string
  291. fstream f(file_name.c_str(), ios::in);
  292. if (!f.is_open()) {
  293. return (false);
  294. }
  295. string hex_string;
  296. f >> hex_string;
  297. f.close();
  298. // remove any spaces
  299. boost::algorithm::erase_all(hex_string, " ");
  300. // now remove :
  301. /// @todo: We should check first if the format is sane.
  302. /// Otherwise 1:2:3:4 will be converted to 0x12, 0x34
  303. boost::algorithm::erase_all(hex_string, ":");
  304. std::vector<uint8_t> bin;
  305. // Decode the hex string and store it in bin (which happens
  306. // to be OptionBuffer format)
  307. isc::util::encode::decodeHex(hex_string, bin);
  308. // Now create server-id option
  309. serverid_.reset(new Option(Option::V6, D6O_SERVERID, bin));
  310. return (true);
  311. }
  312. std::string
  313. Dhcpv6Srv::duidToString(const OptionPtr& opt) {
  314. stringstream tmp;
  315. OptionBuffer data = opt->getData();
  316. bool colon = false;
  317. for (OptionBufferConstIter it = data.begin(); it != data.end(); ++it) {
  318. if (colon) {
  319. tmp << ":";
  320. }
  321. tmp << hex << setw(2) << setfill('0') << static_cast<uint16_t>(*it);
  322. if (!colon) {
  323. colon = true;
  324. }
  325. }
  326. return tmp.str();
  327. }
  328. bool
  329. Dhcpv6Srv::writeServerID(const std::string& file_name) {
  330. fstream f(file_name.c_str(), ios::out | ios::trunc);
  331. if (!f.good()) {
  332. return (false);
  333. }
  334. f << duidToString(getServerID());
  335. f.close();
  336. return (true);
  337. }
  338. void
  339. Dhcpv6Srv::generateServerID() {
  340. /// @todo: This code implements support for DUID-LLT (the recommended one).
  341. /// We should eventually add support for other DUID types: DUID-LL, DUID-EN
  342. /// and DUID-UUID
  343. const IfaceMgr::IfaceCollection& ifaces = IfaceMgr::instance().getIfaces();
  344. // Let's find suitable interface.
  345. for (IfaceMgr::IfaceCollection::const_iterator iface = ifaces.begin();
  346. iface != ifaces.end(); ++iface) {
  347. // All the following checks could be merged into one multi-condition
  348. // statement, but let's keep them separated as perhaps one day
  349. // we will grow knobs to selectively turn them on or off. Also,
  350. // this code is used only *once* during first start on a new machine
  351. // and then server-id is stored. (or at least it will be once
  352. // DUID storage is implemented)
  353. // I wish there was a this_is_a_real_physical_interface flag...
  354. // MAC address should be at least 6 bytes. Although there is no such
  355. // requirement in any RFC, all decent physical interfaces (Ethernet,
  356. // WiFi, Infiniband, etc.) have 6 bytes long MAC address. We want to
  357. // base our DUID on real hardware address, rather than virtual
  358. // interface that pretends that underlying IP address is its MAC.
  359. if (iface->getMacLen() < MIN_MAC_LEN) {
  360. continue;
  361. }
  362. // Let's don't use loopback.
  363. if (iface->flag_loopback_) {
  364. continue;
  365. }
  366. // Let's skip downed interfaces. It is better to use working ones.
  367. if (!iface->flag_up_) {
  368. continue;
  369. }
  370. // Some interfaces (like lo on Linux) report 6-bytes long
  371. // MAC address 00:00:00:00:00:00. Let's not use such weird interfaces
  372. // to generate DUID.
  373. if (isRangeZero(iface->getMac(), iface->getMac() + iface->getMacLen())) {
  374. continue;
  375. }
  376. // Ok, we have useful MAC. Let's generate DUID-LLT based on
  377. // it. See RFC3315, Section 9.2 for details.
  378. // DUID uses seconds since midnight of 01-01-2000, time() returns
  379. // seconds since 01-01-1970. DUID_TIME_EPOCH substution corrects that.
  380. time_t seconds = time(NULL);
  381. seconds -= DUID_TIME_EPOCH;
  382. OptionBuffer srvid(8 + iface->getMacLen());
  383. writeUint16(DUID::DUID_LLT, &srvid[0]);
  384. writeUint16(HWTYPE_ETHERNET, &srvid[2]);
  385. writeUint32(static_cast<uint32_t>(seconds), &srvid[4]);
  386. memcpy(&srvid[0] + 8, iface->getMac(), iface->getMacLen());
  387. serverid_ = OptionPtr(new Option(Option::V6, D6O_SERVERID,
  388. srvid.begin(), srvid.end()));
  389. return;
  390. }
  391. // If we reached here, there are no suitable interfaces found.
  392. // Either interface detection is not supported on this platform or
  393. // this is really weird box. Let's use DUID-EN instead.
  394. // See Section 9.3 of RFC3315 for details.
  395. OptionBuffer srvid(12);
  396. writeUint16(DUID::DUID_EN, &srvid[0]);
  397. writeUint32(ENTERPRISE_ID_ISC, &srvid[2]);
  398. // Length of the identifier is company specific. I hereby declare
  399. // ISC "standard" of 6 bytes long pseudo-random numbers.
  400. srandom(time(NULL));
  401. fillRandom(&srvid[6], &srvid[12]);
  402. serverid_ = OptionPtr(new Option(Option::V6, D6O_SERVERID,
  403. srvid.begin(), srvid.end()));
  404. }
  405. void
  406. Dhcpv6Srv::copyDefaultOptions(const Pkt6Ptr& question, Pkt6Ptr& answer) {
  407. // Add client-id.
  408. OptionPtr clientid = question->getOption(D6O_CLIENTID);
  409. if (clientid) {
  410. answer->addOption(clientid);
  411. }
  412. /// @todo: Should throw if there is no client-id (except anonymous INF-REQUEST)
  413. // If this is a relayed message, we need to copy relay information
  414. if (!question->relay_info_.empty()) {
  415. answer->copyRelayInfo(question);
  416. }
  417. }
  418. void
  419. Dhcpv6Srv::appendDefaultOptions(const Pkt6Ptr&, Pkt6Ptr& answer) {
  420. // add server-id
  421. answer->addOption(getServerID());
  422. }
  423. void
  424. Dhcpv6Srv::appendRequestedOptions(const Pkt6Ptr& question, Pkt6Ptr& answer) {
  425. // Get the configured subnet suitable for the incoming packet.
  426. Subnet6Ptr subnet = selectSubnet(question);
  427. // Leave if there is no subnet matching the incoming packet.
  428. // There is no need to log the error message here because
  429. // it will be logged in the assignLease() when it fails to
  430. // pick the suitable subnet. We don't want to duplicate
  431. // error messages in such case.
  432. if (!subnet) {
  433. return;
  434. }
  435. // Client requests some options using ORO option. Try to
  436. // get this option from client's message.
  437. boost::shared_ptr<OptionIntArray<uint16_t> > option_oro =
  438. boost::dynamic_pointer_cast<OptionIntArray<uint16_t> >(question->getOption(D6O_ORO));
  439. // Option ORO not found. Don't do anything then.
  440. if (!option_oro) {
  441. return;
  442. }
  443. // Get the list of options that client requested.
  444. const std::vector<uint16_t>& requested_opts = option_oro->getValues();
  445. BOOST_FOREACH(uint16_t opt, requested_opts) {
  446. Subnet::OptionDescriptor desc = subnet->getOptionDescriptor("dhcp6", opt);
  447. if (desc.option) {
  448. answer->addOption(desc.option);
  449. }
  450. }
  451. }
  452. OptionPtr
  453. Dhcpv6Srv::createStatusCode(uint16_t code, const std::string& text) {
  454. // @todo This function uses OptionCustom class to manage contents
  455. // of the data fields. Since this this option is frequently used
  456. // it may be good to implement dedicated class to avoid performance
  457. // impact.
  458. // Get the definition of the option holding status code.
  459. OptionDefinitionPtr status_code_def =
  460. LibDHCP::getOptionDef(Option::V6, D6O_STATUS_CODE);
  461. // This definition is assumed to be initialized in LibDHCP.
  462. assert(status_code_def);
  463. // As there is no dedicated class to represent Status Code
  464. // the OptionCustom class is used here instead.
  465. OptionCustomPtr option_status =
  466. OptionCustomPtr(new OptionCustom(*status_code_def, Option::V6));
  467. assert(option_status);
  468. // Set status code to 'code' (0 - means data field #0).
  469. option_status->writeInteger(code, 0);
  470. // Set a message (1 - means data field #1).
  471. option_status->writeString(text, 1);
  472. return (option_status);
  473. }
  474. void
  475. Dhcpv6Srv::sanityCheck(const Pkt6Ptr& pkt, RequirementLevel clientid,
  476. RequirementLevel serverid) {
  477. Option::OptionCollection client_ids = pkt->getOptions(D6O_CLIENTID);
  478. switch (clientid) {
  479. case MANDATORY:
  480. if (client_ids.size() != 1) {
  481. isc_throw(RFCViolation, "Exactly 1 client-id option expected in "
  482. << pkt->getName() << ", but " << client_ids.size()
  483. << " received");
  484. }
  485. break;
  486. case OPTIONAL:
  487. if (client_ids.size() > 1) {
  488. isc_throw(RFCViolation, "Too many (" << client_ids.size()
  489. << ") client-id options received in " << pkt->getName());
  490. }
  491. break;
  492. case FORBIDDEN:
  493. // doesn't make sense - client-id is always allowed
  494. break;
  495. }
  496. Option::OptionCollection server_ids = pkt->getOptions(D6O_SERVERID);
  497. switch (serverid) {
  498. case FORBIDDEN:
  499. if (!server_ids.empty()) {
  500. isc_throw(RFCViolation, "Server-id option was not expected, but "
  501. << server_ids.size() << " received in " << pkt->getName());
  502. }
  503. break;
  504. case MANDATORY:
  505. if (server_ids.size() != 1) {
  506. isc_throw(RFCViolation, "Invalid number of server-id options received ("
  507. << server_ids.size() << "), exactly 1 expected in message "
  508. << pkt->getName());
  509. }
  510. break;
  511. case OPTIONAL:
  512. if (server_ids.size() > 1) {
  513. isc_throw(RFCViolation, "Too many (" << server_ids.size()
  514. << ") server-id options received in " << pkt->getName());
  515. }
  516. }
  517. }
  518. Subnet6Ptr
  519. Dhcpv6Srv::selectSubnet(const Pkt6Ptr& question) {
  520. Subnet6Ptr subnet;
  521. if (question->relay_info_.empty()) {
  522. // This is a direct (non-relayed) message
  523. // Try to find a subnet if received packet from a directly connected client
  524. subnet = CfgMgr::instance().getSubnet6(question->getIface());
  525. if (!subnet) {
  526. // If no subnet was found, try to find it based on remote address
  527. subnet = CfgMgr::instance().getSubnet6(question->getRemoteAddr());
  528. }
  529. } else {
  530. // This is a relayed message
  531. OptionPtr interface_id = question->getAnyRelayOption(D6O_INTERFACE_ID,
  532. Pkt6::RELAY_GET_FIRST);
  533. if (interface_id) {
  534. subnet = CfgMgr::instance().getSubnet6(interface_id);
  535. }
  536. if (!subnet) {
  537. // If no interface-id was specified (or not configured on server), let's
  538. // try address matching
  539. IOAddress link_addr = question->relay_info_.back().linkaddr_;
  540. // if relay filled in link_addr field, then let's use it
  541. if (link_addr != IOAddress("::")) {
  542. subnet = CfgMgr::instance().getSubnet6(link_addr);
  543. }
  544. }
  545. }
  546. // Let's execute all callouts registered for packet_received
  547. if (HooksManager::getHooksManager().calloutsPresent(hook_index_subnet6_select_)) {
  548. CalloutHandlePtr callout_handle = getCalloutHandle(question);
  549. // We're reusing callout_handle from previous calls
  550. callout_handle->deleteAllArguments();
  551. // Set new arguments
  552. callout_handle->setArgument("query6", question);
  553. callout_handle->setArgument("subnet6", subnet);
  554. callout_handle->setArgument("subnet6collection", CfgMgr::instance().getSubnets6());
  555. // Call user (and server-side) callouts
  556. HooksManager::getHooksManager().callCallouts(hook_index_subnet6_select_,
  557. *callout_handle);
  558. // Callouts decided to skip this step. This means that no subnet will be
  559. // selected. Packet processing will continue, but it will be severly limited
  560. // (i.e. only global options will be assigned)
  561. if (callout_handle->getSkip()) {
  562. LOG_DEBUG(dhcp6_logger, DBG_DHCP6_HOOKS, DHCP6_HOOK_SUBNET6_SELECT_SKIP);
  563. return (Subnet6Ptr());
  564. }
  565. // Use whatever subnet was specified by the callout
  566. callout_handle->getArgument("subnet6", subnet);
  567. }
  568. return (subnet);
  569. }
  570. void
  571. Dhcpv6Srv::assignLeases(const Pkt6Ptr& question, Pkt6Ptr& answer) {
  572. // We need to allocate addresses for all IA_NA options in the client's
  573. // question (i.e. SOLICIT or REQUEST) message.
  574. // @todo add support for IA_TA
  575. // @todo add support for IA_PD
  576. // We need to select a subnet the client is connected in.
  577. Subnet6Ptr subnet = selectSubnet(question);
  578. if (!subnet) {
  579. // This particular client is out of luck today. We do not have
  580. // information about the subnet he is connected to. This likely means
  581. // misconfiguration of the server (or some relays). We will continue to
  582. // process this message, but our response will be almost useless: no
  583. // addresses or prefixes, no subnet specific configuration etc. The only
  584. // thing this client can get is some global information (like DNS
  585. // servers).
  586. LOG_WARN(dhcp6_logger, DHCP6_SUBNET_SELECTION_FAILED)
  587. .arg(question->getRemoteAddr().toText())
  588. .arg(question->getName());
  589. } else {
  590. LOG_DEBUG(dhcp6_logger, DBG_DHCP6_DETAIL_DATA, DHCP6_SUBNET_SELECTED)
  591. .arg(subnet->toText());
  592. }
  593. // @todo: We should implement Option6Duid some day, but we can do without it
  594. // just fine for now
  595. // Let's find client's DUID. Client is supposed to include its client-id
  596. // option almost all the time (the only exception is an anonymous inf-request,
  597. // but that is mostly a theoretical case). Our allocation engine needs DUID
  598. // and will refuse to allocate anything to anonymous clients.
  599. DuidPtr duid;
  600. OptionPtr opt_duid = question->getOption(D6O_CLIENTID);
  601. if (opt_duid) {
  602. duid = DuidPtr(new DUID(opt_duid->getData()));
  603. } else {
  604. LOG_DEBUG(dhcp6_logger, DBG_DHCP6_BASIC, DHCP6_CLIENTID_MISSING);
  605. // Let's drop the message. This client is not sane.
  606. isc_throw(RFCViolation, "Mandatory client-id is missing in received message");
  607. }
  608. // Now that we have all information about the client, let's iterate over all
  609. // received options and handle IA_NA options one by one and store our
  610. // responses in answer message (ADVERTISE or REPLY).
  611. //
  612. // @todo: expand this to cover IA_PD and IA_TA once we implement support for
  613. // prefix delegation and temporary addresses.
  614. for (Option::OptionCollection::iterator opt = question->options_.begin();
  615. opt != question->options_.end(); ++opt) {
  616. switch (opt->second->getType()) {
  617. case D6O_IA_NA: {
  618. OptionPtr answer_opt = assignIA_NA(subnet, duid, question,
  619. boost::dynamic_pointer_cast<Option6IA>(opt->second),
  620. question);
  621. if (answer_opt) {
  622. answer->addOption(answer_opt);
  623. }
  624. break;
  625. }
  626. default:
  627. break;
  628. }
  629. }
  630. }
  631. OptionPtr
  632. Dhcpv6Srv::assignIA_NA(const Subnet6Ptr& subnet, const DuidPtr& duid,
  633. Pkt6Ptr question, boost::shared_ptr<Option6IA> ia, const Pkt6Ptr& query) {
  634. // If there is no subnet selected for handling this IA_NA, the only thing to do left is
  635. // to say that we are sorry, but the user won't get an address. As a convenience, we
  636. // use a different status text to indicate that (compare to the same status code,
  637. // but different wording below)
  638. if (!subnet) {
  639. // Create empty IA_NA option with IAID matching the request.
  640. // Note that we don't use OptionDefinition class to create this option.
  641. // This is because we prefer using a constructor of Option6IA that
  642. // initializes IAID. Otherwise we would have to use setIAID() after
  643. // creation of the option which has some performance implications.
  644. boost::shared_ptr<Option6IA> ia_rsp(new Option6IA(D6O_IA_NA, ia->getIAID()));
  645. // Insert status code NoAddrsAvail.
  646. ia_rsp->addOption(createStatusCode(STATUS_NoAddrsAvail, "Sorry, no subnet available."));
  647. return (ia_rsp);
  648. }
  649. // Check if the client sent us a hint in his IA_NA. Clients may send an
  650. // address in their IA_NA options as a suggestion (e.g. the last address
  651. // they used before).
  652. boost::shared_ptr<Option6IAAddr> hintOpt = boost::dynamic_pointer_cast<Option6IAAddr>
  653. (ia->getOption(D6O_IAADDR));
  654. IOAddress hint("::");
  655. if (hintOpt) {
  656. hint = hintOpt->getAddress();
  657. }
  658. LOG_DEBUG(dhcp6_logger, DBG_DHCP6_DETAIL, DHCP6_PROCESS_IA_NA_REQUEST)
  659. .arg(duid?duid->toText():"(no-duid)").arg(ia->getIAID())
  660. .arg(hintOpt?hint.toText():"(no hint)");
  661. // "Fake" allocation is processing of SOLICIT message. We pretend to do an
  662. // allocation, but we do not put the lease in the database. That is ok,
  663. // because we do not guarantee that the user will get that exact lease. If
  664. // the user selects this server to do actual allocation (i.e. sends REQUEST)
  665. // it should include this hint. That will help us during the actual lease
  666. // allocation.
  667. bool fake_allocation = false;
  668. if (question->getType() == DHCPV6_SOLICIT) {
  669. /// @todo: Check if we support rapid commit
  670. fake_allocation = true;
  671. }
  672. CalloutHandlePtr callout_handle = getCalloutHandle(query);
  673. // Use allocation engine to pick a lease for this client. Allocation engine
  674. // will try to honour the hint, but it is just a hint - some other address
  675. // may be used instead. If fake_allocation is set to false, the lease will
  676. // be inserted into the LeaseMgr as well.
  677. Lease6Ptr lease = alloc_engine_->allocateAddress6(subnet, duid, ia->getIAID(),
  678. hint, fake_allocation,
  679. callout_handle);
  680. // Create IA_NA that we will put in the response.
  681. // Do not use OptionDefinition to create option's instance so
  682. // as we can initialize IAID using a constructor.
  683. boost::shared_ptr<Option6IA> ia_rsp(new Option6IA(D6O_IA_NA, ia->getIAID()));
  684. if (lease) {
  685. // We have a lease! Let's wrap its content into IA_NA option
  686. // with IAADDR suboption.
  687. LOG_DEBUG(dhcp6_logger, DBG_DHCP6_DETAIL, fake_allocation?
  688. DHCP6_LEASE_ADVERT:DHCP6_LEASE_ALLOC)
  689. .arg(lease->addr_.toText())
  690. .arg(duid?duid->toText():"(no-duid)")
  691. .arg(ia->getIAID());
  692. ia_rsp->setT1(subnet->getT1());
  693. ia_rsp->setT2(subnet->getT2());
  694. boost::shared_ptr<Option6IAAddr>
  695. addr(new Option6IAAddr(D6O_IAADDR,
  696. lease->addr_,
  697. lease->preferred_lft_,
  698. lease->valid_lft_));
  699. ia_rsp->addOption(addr);
  700. // It would be possible to insert status code=0(success) as well,
  701. // but this is considered waste of bandwidth as absence of status
  702. // code is considered a success.
  703. } else {
  704. // Allocation engine did not allocate a lease. The engine logged
  705. // cause of that failure. The only thing left is to insert
  706. // status code to pass the sad news to the client.
  707. LOG_DEBUG(dhcp6_logger, DBG_DHCP6_DETAIL, fake_allocation ?
  708. DHCP6_LEASE_ADVERT_FAIL : DHCP6_LEASE_ALLOC_FAIL)
  709. .arg(duid?duid->toText():"(no-duid)")
  710. .arg(ia->getIAID());
  711. ia_rsp->addOption(createStatusCode(STATUS_NoAddrsAvail,
  712. "Sorry, no address could be allocated."));
  713. }
  714. return (ia_rsp);
  715. }
  716. OptionPtr
  717. Dhcpv6Srv::renewIA_NA(const Subnet6Ptr& subnet, const DuidPtr& duid,
  718. Pkt6Ptr /* question */, boost::shared_ptr<Option6IA> ia) {
  719. if (!subnet) {
  720. // There's no subnet select for this client. There's nothing to renew.
  721. boost::shared_ptr<Option6IA> ia_rsp(new Option6IA(D6O_IA_NA, ia->getIAID()));
  722. // Insert status code NoAddrsAvail.
  723. ia_rsp->addOption(createStatusCode(STATUS_NoBinding,
  724. "Sorry, no known leases for this duid/iaid."));
  725. LOG_DEBUG(dhcp6_logger, DBG_DHCP6_DETAIL, DHCP6_RENEW_UNKNOWN_SUBNET)
  726. .arg(duid->toText())
  727. .arg(ia->getIAID());
  728. return (ia_rsp);
  729. }
  730. Lease6Ptr lease = LeaseMgrFactory::instance().getLease6(*duid, ia->getIAID(),
  731. subnet->getID());
  732. if (!lease) {
  733. // client renewing a lease that we don't know about.
  734. // Create empty IA_NA option with IAID matching the request.
  735. boost::shared_ptr<Option6IA> ia_rsp(new Option6IA(D6O_IA_NA, ia->getIAID()));
  736. // Insert status code NoAddrsAvail.
  737. ia_rsp->addOption(createStatusCode(STATUS_NoBinding,
  738. "Sorry, no known leases for this duid/iaid."));
  739. LOG_DEBUG(dhcp6_logger, DBG_DHCP6_DETAIL, DHCP6_UNKNOWN_RENEW)
  740. .arg(duid->toText())
  741. .arg(ia->getIAID())
  742. .arg(subnet->toText());
  743. return (ia_rsp);
  744. }
  745. lease->preferred_lft_ = subnet->getPreferred();
  746. lease->valid_lft_ = subnet->getValid();
  747. lease->t1_ = subnet->getT1();
  748. lease->t2_ = subnet->getT2();
  749. lease->cltt_ = time(NULL);
  750. LeaseMgrFactory::instance().updateLease6(lease);
  751. // Create empty IA_NA option with IAID matching the request.
  752. boost::shared_ptr<Option6IA> ia_rsp(new Option6IA(D6O_IA_NA, ia->getIAID()));
  753. ia_rsp->setT1(subnet->getT1());
  754. ia_rsp->setT2(subnet->getT2());
  755. boost::shared_ptr<Option6IAAddr> addr(new Option6IAAddr(D6O_IAADDR,
  756. lease->addr_, lease->preferred_lft_,
  757. lease->valid_lft_));
  758. ia_rsp->addOption(addr);
  759. return (ia_rsp);
  760. }
  761. void
  762. Dhcpv6Srv::renewLeases(const Pkt6Ptr& renew, Pkt6Ptr& reply) {
  763. // We need to renew addresses for all IA_NA options in the client's
  764. // RENEW message.
  765. // @todo add support for IA_TA
  766. // @todo add support for IA_PD
  767. // We need to select a subnet the client is connected in.
  768. Subnet6Ptr subnet = selectSubnet(renew);
  769. if (!subnet) {
  770. // This particular client is out of luck today. We do not have
  771. // information about the subnet he is connected to. This likely means
  772. // misconfiguration of the server (or some relays). We will continue to
  773. // process this message, but our response will be almost useless: no
  774. // addresses or prefixes, no subnet specific configuration etc. The only
  775. // thing this client can get is some global information (like DNS
  776. // servers).
  777. LOG_WARN(dhcp6_logger, DHCP6_SUBNET_SELECTION_FAILED)
  778. .arg(renew->getRemoteAddr().toText())
  779. .arg(renew->getName());
  780. } else {
  781. LOG_DEBUG(dhcp6_logger, DBG_DHCP6_DETAIL_DATA, DHCP6_SUBNET_SELECTED)
  782. .arg(subnet->toText());
  783. }
  784. // Let's find client's DUID. Client is supposed to include its client-id
  785. // option almost all the time (the only exception is an anonymous inf-request,
  786. // but that is mostly a theoretical case). Our allocation engine needs DUID
  787. // and will refuse to allocate anything to anonymous clients.
  788. OptionPtr opt_duid = renew->getOption(D6O_CLIENTID);
  789. if (!opt_duid) {
  790. // This should not happen. We have checked this before.
  791. reply->addOption(createStatusCode(STATUS_UnspecFail,
  792. "You did not include mandatory client-id"));
  793. return;
  794. }
  795. DuidPtr duid(new DUID(opt_duid->getData()));
  796. for (Option::OptionCollection::iterator opt = renew->options_.begin();
  797. opt != renew->options_.end(); ++opt) {
  798. switch (opt->second->getType()) {
  799. case D6O_IA_NA: {
  800. OptionPtr answer_opt = renewIA_NA(subnet, duid, renew,
  801. boost::dynamic_pointer_cast<Option6IA>(opt->second));
  802. if (answer_opt) {
  803. reply->addOption(answer_opt);
  804. }
  805. break;
  806. }
  807. default:
  808. break;
  809. }
  810. }
  811. }
  812. void
  813. Dhcpv6Srv::releaseLeases(const Pkt6Ptr& release, Pkt6Ptr& reply) {
  814. // We need to release addresses for all IA_NA options in the client's
  815. // RELEASE message.
  816. // @todo Add support for IA_TA
  817. // @todo Add support for IA_PD
  818. // @todo Consider supporting more than one address in a single IA_NA.
  819. // That was envisaged by RFC3315, but it never happened. The only
  820. // software that supports that is Dibbler, but its author seriously doubts
  821. // if anyone is really using it. Clients that want more than one address
  822. // just include more instances of IA_NA options.
  823. // Let's find client's DUID. Client is supposed to include its client-id
  824. // option almost all the time (the only exception is an anonymous inf-request,
  825. // but that is mostly a theoretical case). Our allocation engine needs DUID
  826. // and will refuse to allocate anything to anonymous clients.
  827. OptionPtr opt_duid = release->getOption(D6O_CLIENTID);
  828. if (!opt_duid) {
  829. // This should not happen. We have checked this before.
  830. // see sanityCheck() called from processRelease()
  831. LOG_WARN(dhcp6_logger, DHCP6_RELEASE_MISSING_CLIENTID)
  832. .arg(release->getRemoteAddr().toText());
  833. reply->addOption(createStatusCode(STATUS_UnspecFail,
  834. "You did not include mandatory client-id"));
  835. return;
  836. }
  837. DuidPtr duid(new DUID(opt_duid->getData()));
  838. int general_status = STATUS_Success;
  839. for (Option::OptionCollection::iterator opt = release->options_.begin();
  840. opt != release->options_.end(); ++opt) {
  841. switch (opt->second->getType()) {
  842. case D6O_IA_NA: {
  843. OptionPtr answer_opt = releaseIA_NA(duid, release, general_status,
  844. boost::dynamic_pointer_cast<Option6IA>(opt->second));
  845. if (answer_opt) {
  846. reply->addOption(answer_opt);
  847. }
  848. break;
  849. }
  850. // @todo: add support for IA_PD
  851. // @todo: add support for IA_TA
  852. default:
  853. // remaining options are stateless and thus ignored in this context
  854. ;
  855. }
  856. }
  857. // To be pedantic, we should also include status code in the top-level
  858. // scope, not just in each IA_NA. See RFC3315, section 18.2.6.
  859. // This behavior will likely go away in RFC3315bis.
  860. reply->addOption(createStatusCode(general_status,
  861. "Summary status for all processed IA_NAs"));
  862. }
  863. OptionPtr
  864. Dhcpv6Srv::releaseIA_NA(const DuidPtr& duid, Pkt6Ptr /* question */,
  865. int& general_status, boost::shared_ptr<Option6IA> ia) {
  866. // Release can be done in one of two ways:
  867. // Approach 1: extract address from client's IA_NA and see if it belongs
  868. // to this particular client.
  869. // Approach 2: find a subnet for this client, get a lease for
  870. // this subnet/duid/iaid and check if its content matches to what the
  871. // client is asking us to release.
  872. //
  873. // This method implements approach 1.
  874. // That's our response
  875. boost::shared_ptr<Option6IA> ia_rsp(new Option6IA(D6O_IA_NA, ia->getIAID()));
  876. boost::shared_ptr<Option6IAAddr> release_addr = boost::dynamic_pointer_cast<Option6IAAddr>
  877. (ia->getOption(D6O_IAADDR));
  878. if (!release_addr) {
  879. ia_rsp->addOption(createStatusCode(STATUS_NoBinding,
  880. "You did not include address in your RELEASE"));
  881. general_status = STATUS_NoBinding;
  882. return (ia_rsp);
  883. }
  884. Lease6Ptr lease = LeaseMgrFactory::instance().getLease6(release_addr->getAddress());
  885. if (!lease) {
  886. // client releasing a lease that we don't know about.
  887. // Insert status code NoAddrsAvail.
  888. ia_rsp->addOption(createStatusCode(STATUS_NoBinding,
  889. "Sorry, no known leases for this duid/iaid, can't release."));
  890. general_status = STATUS_NoBinding;
  891. LOG_INFO(dhcp6_logger, DHCP6_UNKNOWN_RELEASE)
  892. .arg(duid->toText())
  893. .arg(ia->getIAID());
  894. return (ia_rsp);
  895. }
  896. if (!lease->duid_) {
  897. // Something is gravely wrong here. We do have a lease, but it does not
  898. // have mandatory DUID information attached. Someone was messing with our
  899. // database.
  900. LOG_ERROR(dhcp6_logger, DHCP6_LEASE_WITHOUT_DUID)
  901. .arg(release_addr->getAddress().toText());
  902. general_status = STATUS_UnspecFail;
  903. ia_rsp->addOption(createStatusCode(STATUS_UnspecFail,
  904. "Database consistency check failed when trying to RELEASE"));
  905. return (ia_rsp);
  906. }
  907. if (*duid != *(lease->duid_)) {
  908. // Sorry, it's not your address. You can't release it.
  909. LOG_INFO(dhcp6_logger, DHCP6_RELEASE_FAIL_WRONG_DUID)
  910. .arg(duid->toText())
  911. .arg(release_addr->getAddress().toText())
  912. .arg(lease->duid_->toText());
  913. general_status = STATUS_NoBinding;
  914. ia_rsp->addOption(createStatusCode(STATUS_NoBinding,
  915. "This address does not belong to you, you can't release it"));
  916. return (ia_rsp);
  917. }
  918. if (ia->getIAID() != lease->iaid_) {
  919. // This address belongs to this client, but to a different IA
  920. LOG_WARN(dhcp6_logger, DHCP6_RELEASE_FAIL_WRONG_IAID)
  921. .arg(duid->toText())
  922. .arg(release_addr->getAddress().toText())
  923. .arg(lease->iaid_)
  924. .arg(ia->getIAID());
  925. ia_rsp->addOption(createStatusCode(STATUS_NoBinding,
  926. "This is your address, but you used wrong IAID"));
  927. general_status = STATUS_NoBinding;
  928. return (ia_rsp);
  929. }
  930. // It is not necessary to check if the address matches as we used
  931. // getLease6(addr) method that is supposed to return a proper lease.
  932. // Ok, we've passed all checks. Let's release this address.
  933. if (!LeaseMgrFactory::instance().deleteLease(lease->addr_)) {
  934. ia_rsp->addOption(createStatusCode(STATUS_UnspecFail,
  935. "Server failed to release a lease"));
  936. LOG_ERROR(dhcp6_logger, DHCP6_RELEASE_FAIL)
  937. .arg(lease->addr_.toText())
  938. .arg(duid->toText())
  939. .arg(lease->iaid_);
  940. general_status = STATUS_UnspecFail;
  941. return (ia_rsp);
  942. } else {
  943. LOG_DEBUG(dhcp6_logger, DBG_DHCP6_DETAIL, DHCP6_RELEASE)
  944. .arg(lease->addr_.toText())
  945. .arg(duid->toText())
  946. .arg(lease->iaid_);
  947. ia_rsp->addOption(createStatusCode(STATUS_Success,
  948. "Lease released. Thank you, please come again."));
  949. return (ia_rsp);
  950. }
  951. }
  952. Pkt6Ptr
  953. Dhcpv6Srv::processSolicit(const Pkt6Ptr& solicit) {
  954. sanityCheck(solicit, MANDATORY, FORBIDDEN);
  955. Pkt6Ptr advertise(new Pkt6(DHCPV6_ADVERTISE, solicit->getTransid()));
  956. copyDefaultOptions(solicit, advertise);
  957. appendDefaultOptions(solicit, advertise);
  958. appendRequestedOptions(solicit, advertise);
  959. assignLeases(solicit, advertise);
  960. return (advertise);
  961. }
  962. Pkt6Ptr
  963. Dhcpv6Srv::processRequest(const Pkt6Ptr& request) {
  964. sanityCheck(request, MANDATORY, MANDATORY);
  965. Pkt6Ptr reply(new Pkt6(DHCPV6_REPLY, request->getTransid()));
  966. copyDefaultOptions(request, reply);
  967. appendDefaultOptions(request, reply);
  968. appendRequestedOptions(request, reply);
  969. assignLeases(request, reply);
  970. return (reply);
  971. }
  972. Pkt6Ptr
  973. Dhcpv6Srv::processRenew(const Pkt6Ptr& renew) {
  974. sanityCheck(renew, MANDATORY, MANDATORY);
  975. Pkt6Ptr reply(new Pkt6(DHCPV6_REPLY, renew->getTransid()));
  976. copyDefaultOptions(renew, reply);
  977. appendDefaultOptions(renew, reply);
  978. appendRequestedOptions(renew, reply);
  979. renewLeases(renew, reply);
  980. return reply;
  981. }
  982. Pkt6Ptr
  983. Dhcpv6Srv::processRebind(const Pkt6Ptr& rebind) {
  984. /// @todo: Implement this
  985. Pkt6Ptr reply(new Pkt6(DHCPV6_REPLY, rebind->getTransid()));
  986. return reply;
  987. }
  988. Pkt6Ptr
  989. Dhcpv6Srv::processConfirm(const Pkt6Ptr& confirm) {
  990. /// @todo: Implement this
  991. Pkt6Ptr reply(new Pkt6(DHCPV6_REPLY, confirm->getTransid()));
  992. return reply;
  993. }
  994. Pkt6Ptr
  995. Dhcpv6Srv::processRelease(const Pkt6Ptr& release) {
  996. sanityCheck(release, MANDATORY, MANDATORY);
  997. Pkt6Ptr reply(new Pkt6(DHCPV6_REPLY, release->getTransid()));
  998. copyDefaultOptions(release, reply);
  999. appendDefaultOptions(release, reply);
  1000. releaseLeases(release, reply);
  1001. return reply;
  1002. }
  1003. Pkt6Ptr
  1004. Dhcpv6Srv::processDecline(const Pkt6Ptr& decline) {
  1005. /// @todo: Implement this
  1006. Pkt6Ptr reply(new Pkt6(DHCPV6_REPLY, decline->getTransid()));
  1007. return reply;
  1008. }
  1009. Pkt6Ptr
  1010. Dhcpv6Srv::processInfRequest(const Pkt6Ptr& infRequest) {
  1011. /// @todo: Implement this
  1012. Pkt6Ptr reply(new Pkt6(DHCPV6_REPLY, infRequest->getTransid()));
  1013. return reply;
  1014. }
  1015. isc::hooks::CalloutHandlePtr Dhcpv6Srv::getCalloutHandle(const Pkt6Ptr& pkt) {
  1016. // This method returns a CalloutHandle for a given packet. It is guaranteed
  1017. // to return the same callout_handle (so user library contexts are
  1018. // preserved). This method works well if the server processes one packet
  1019. // at a time. Once the server architecture is extended to cover parallel
  1020. // packets processing (e.g. delayed-ack, some form of buffering etc.), this
  1021. // method has to be extended (e.g. store callouts in a map and use pkt as
  1022. // a key). Additional code would be required to release the callout handle
  1023. // once the server finished processing.
  1024. CalloutHandlePtr callout_handle;
  1025. static Pkt6Ptr old_pointer;
  1026. if (!callout_handle ||
  1027. old_pointer != pkt) {
  1028. // This is the first packet or a different packet than previously
  1029. // passed to getCalloutHandle()
  1030. // Remember the pointer to this packet
  1031. old_pointer = pkt;
  1032. callout_handle = HooksManager::getHooksManager().createCalloutHandle();
  1033. }
  1034. return (callout_handle);
  1035. }
  1036. };
  1037. };