dhcp6_srv.cc 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953
  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 <boost/foreach.hpp>
  38. #include <stdlib.h>
  39. #include <time.h>
  40. using namespace isc;
  41. using namespace isc::asiolink;
  42. using namespace isc::dhcp;
  43. using namespace isc::util;
  44. using namespace std;
  45. namespace isc {
  46. namespace dhcp {
  47. Dhcpv6Srv::Dhcpv6Srv(uint16_t port, const char* dbconfig)
  48. : alloc_engine_(), serverid_(), shutdown_(true) {
  49. LOG_DEBUG(dhcp6_logger, DBG_DHCP6_START, DHCP6_OPEN_SOCKET).arg(port);
  50. // Initialize objects required for DHCP server operation.
  51. try {
  52. // Port 0 is used for testing purposes. It means that the server should
  53. // not open any sockets at all. Some tests, e.g. configuration parser,
  54. // require Dhcpv6Srv object, but they don't really need it to do
  55. // anything. This speed up and simplifies the tests.
  56. if (port > 0) {
  57. if (IfaceMgr::instance().countIfaces() == 0) {
  58. LOG_ERROR(dhcp6_logger, DHCP6_NO_INTERFACES);
  59. return;
  60. }
  61. IfaceMgr::instance().openSockets6(port);
  62. }
  63. setServerID();
  64. // Instantiate LeaseMgr
  65. LeaseMgrFactory::create(dbconfig);
  66. LOG_INFO(dhcp6_logger, DHCP6_DB_BACKEND_STARTED)
  67. .arg(LeaseMgrFactory::instance().getType())
  68. .arg(LeaseMgrFactory::instance().getName());
  69. // Instantiate allocation engine
  70. alloc_engine_.reset(new AllocEngine(AllocEngine::ALLOC_ITERATIVE, 100));
  71. } catch (const std::exception &e) {
  72. LOG_ERROR(dhcp6_logger, DHCP6_SRV_CONSTRUCT_ERROR).arg(e.what());
  73. return;
  74. }
  75. // All done, so can proceed
  76. shutdown_ = false;
  77. }
  78. Dhcpv6Srv::~Dhcpv6Srv() {
  79. IfaceMgr::instance().closeSockets();
  80. LeaseMgrFactory::destroy();
  81. }
  82. void Dhcpv6Srv::shutdown() {
  83. LOG_DEBUG(dhcp6_logger, DBG_DHCP6_BASIC, DHCP6_SHUTDOWN_REQUEST);
  84. shutdown_ = true;
  85. }
  86. bool Dhcpv6Srv::run() {
  87. while (!shutdown_) {
  88. /// @todo: calculate actual timeout to the next event (e.g. lease
  89. /// expiration) once we have lease database. The idea here is that
  90. /// it is possible to do everything in a single process/thread.
  91. /// For now, we are just calling select for 1000 seconds. There
  92. /// were some issues reported on some systems when calling select()
  93. /// with too large values. Unfortunately, I don't recall the details.
  94. int timeout = 1000;
  95. // client's message and server's response
  96. Pkt6Ptr query;
  97. Pkt6Ptr rsp;
  98. try {
  99. query = IfaceMgr::instance().receive6(timeout);
  100. } catch (const std::exception& e) {
  101. LOG_ERROR(dhcp6_logger, DHCP6_PACKET_RECEIVE_FAIL).arg(e.what());
  102. }
  103. if (query) {
  104. if (!query->unpack()) {
  105. LOG_DEBUG(dhcp6_logger, DBG_DHCP6_DETAIL,
  106. DHCP6_PACKET_PARSE_FAIL);
  107. continue;
  108. }
  109. LOG_DEBUG(dhcp6_logger, DBG_DHCP6_DETAIL, DHCP6_PACKET_RECEIVED)
  110. .arg(query->getName());
  111. LOG_DEBUG(dhcp6_logger, DBG_DHCP6_DETAIL_DATA, DHCP6_QUERY_DATA)
  112. .arg(static_cast<int>(query->getType()))
  113. .arg(query->getBuffer().getLength())
  114. .arg(query->toText());
  115. try {
  116. switch (query->getType()) {
  117. case DHCPV6_SOLICIT:
  118. rsp = processSolicit(query);
  119. break;
  120. case DHCPV6_REQUEST:
  121. rsp = processRequest(query);
  122. break;
  123. case DHCPV6_RENEW:
  124. rsp = processRenew(query);
  125. break;
  126. case DHCPV6_REBIND:
  127. rsp = processRebind(query);
  128. break;
  129. case DHCPV6_CONFIRM:
  130. rsp = processConfirm(query);
  131. break;
  132. case DHCPV6_RELEASE:
  133. rsp = processRelease(query);
  134. break;
  135. case DHCPV6_DECLINE:
  136. rsp = processDecline(query);
  137. break;
  138. case DHCPV6_INFORMATION_REQUEST:
  139. rsp = processInfRequest(query);
  140. break;
  141. default:
  142. // Only action is to output a message if debug is enabled,
  143. // and that will be covered by the debug statement before
  144. // the "switch" statement.
  145. ;
  146. }
  147. } catch (const RFCViolation& e) {
  148. LOG_DEBUG(dhcp6_logger, DBG_DHCP6_BASIC, DHCP6_REQUIRED_OPTIONS_CHECK_FAIL)
  149. .arg(query->getName())
  150. .arg(query->getRemoteAddr())
  151. .arg(e.what());
  152. }
  153. if (rsp) {
  154. rsp->setRemoteAddr(query->getRemoteAddr());
  155. rsp->setLocalAddr(query->getLocalAddr());
  156. rsp->setRemotePort(DHCP6_CLIENT_PORT);
  157. rsp->setLocalPort(DHCP6_SERVER_PORT);
  158. rsp->setIndex(query->getIndex());
  159. rsp->setIface(query->getIface());
  160. LOG_DEBUG(dhcp6_logger, DBG_DHCP6_DETAIL_DATA,
  161. DHCP6_RESPONSE_DATA)
  162. .arg(rsp->getType()).arg(rsp->toText());
  163. if (rsp->pack()) {
  164. try {
  165. IfaceMgr::instance().send(rsp);
  166. } catch (const std::exception& e) {
  167. LOG_ERROR(dhcp6_logger, DHCP6_PACKET_SEND_FAIL).arg(e.what());
  168. }
  169. } else {
  170. LOG_ERROR(dhcp6_logger, DHCP6_PACK_FAIL);
  171. }
  172. }
  173. }
  174. }
  175. return (true);
  176. }
  177. void Dhcpv6Srv::setServerID() {
  178. /// @todo: DUID should be generated once and then stored, rather
  179. /// than generated each time
  180. /// @todo: This code implements support for DUID-LLT (the recommended one).
  181. /// We should eventually add support for other DUID types: DUID-LL, DUID-EN
  182. /// and DUID-UUID
  183. const IfaceMgr::IfaceCollection& ifaces = IfaceMgr::instance().getIfaces();
  184. // Let's find suitable interface.
  185. for (IfaceMgr::IfaceCollection::const_iterator iface = ifaces.begin();
  186. iface != ifaces.end(); ++iface) {
  187. // All the following checks could be merged into one multi-condition
  188. // statement, but let's keep them separated as perhaps one day
  189. // we will grow knobs to selectively turn them on or off. Also,
  190. // this code is used only *once* during first start on a new machine
  191. // and then server-id is stored. (or at least it will be once
  192. // DUID storage is implemente
  193. // I wish there was a this_is_a_real_physical_interface flag...
  194. // MAC address should be at least 6 bytes. Although there is no such
  195. // requirement in any RFC, all decent physical interfaces (Ethernet,
  196. // WiFi, Infiniband, etc.) have 6 bytes long MAC address. We want to
  197. // base our DUID on real hardware address, rather than virtual
  198. // interface that pretends that underlying IP address is its MAC.
  199. if (iface->getMacLen() < MIN_MAC_LEN) {
  200. continue;
  201. }
  202. // Let's don't use loopback.
  203. if (iface->flag_loopback_) {
  204. continue;
  205. }
  206. // Let's skip downed interfaces. It is better to use working ones.
  207. if (!iface->flag_up_) {
  208. continue;
  209. }
  210. // Some interfaces (like lo on Linux) report 6-bytes long
  211. // MAC adress 00:00:00:00:00:00. Let's not use such weird interfaces
  212. // to generate DUID.
  213. if (isRangeZero(iface->getMac(), iface->getMac() + iface->getMacLen())) {
  214. continue;
  215. }
  216. // Ok, we have useful MAC. Let's generate DUID-LLT based on
  217. // it. See RFC3315, Section 9.2 for details.
  218. // DUID uses seconds since midnight of 01-01-2000, time() returns
  219. // seconds since 01-01-1970. DUID_TIME_EPOCH substution corrects that.
  220. time_t seconds = time(NULL);
  221. seconds -= DUID_TIME_EPOCH;
  222. OptionBuffer srvid(8 + iface->getMacLen());
  223. writeUint16(DUID::DUID_LLT, &srvid[0]);
  224. writeUint16(HWTYPE_ETHERNET, &srvid[2]);
  225. writeUint32(static_cast<uint32_t>(seconds), &srvid[4]);
  226. memcpy(&srvid[0] + 8, iface->getMac(), iface->getMacLen());
  227. serverid_ = OptionPtr(new Option(Option::V6, D6O_SERVERID,
  228. srvid.begin(), srvid.end()));
  229. return;
  230. }
  231. // If we reached here, there are no suitable interfaces found.
  232. // Either interface detection is not supported on this platform or
  233. // this is really weird box. Let's use DUID-EN instead.
  234. // See Section 9.3 of RFC3315 for details.
  235. OptionBuffer srvid(12);
  236. writeUint16(DUID::DUID_EN, &srvid[0]);
  237. writeUint32(ENTERPRISE_ID_ISC, &srvid[2]);
  238. // Length of the identifier is company specific. I hereby declare
  239. // ISC "standard" of 6 bytes long pseudo-random numbers.
  240. srandom(time(NULL));
  241. fillRandom(&srvid[6], &srvid[12]);
  242. serverid_ = OptionPtr(new Option(Option::V6, D6O_SERVERID,
  243. srvid.begin(), srvid.end()));
  244. }
  245. void Dhcpv6Srv::copyDefaultOptions(const Pkt6Ptr& question, Pkt6Ptr& answer) {
  246. // Add client-id.
  247. OptionPtr clientid = question->getOption(D6O_CLIENTID);
  248. if (clientid) {
  249. answer->addOption(clientid);
  250. }
  251. // TODO: Should throw if there is no client-id (except anonymous INF-REQUEST)
  252. }
  253. void Dhcpv6Srv::appendDefaultOptions(const Pkt6Ptr& question, Pkt6Ptr& answer) {
  254. // add server-id
  255. answer->addOption(getServerID());
  256. // Get the subnet object. It holds options to be sent to the client
  257. // that belongs to the particular subnet.
  258. Subnet6Ptr subnet = CfgMgr::instance().getSubnet6(question->getRemoteAddr());
  259. // Warn if subnet is not supported and quit.
  260. if (!subnet) {
  261. LOG_WARN(dhcp6_logger, DHCP6_NO_SUBNET_DEF_OPT)
  262. .arg(question->getRemoteAddr().toText());
  263. return;
  264. }
  265. }
  266. void Dhcpv6Srv::appendRequestedOptions(const Pkt6Ptr& question, Pkt6Ptr& answer) {
  267. // Get the subnet for a particular address.
  268. Subnet6Ptr subnet = CfgMgr::instance().getSubnet6(question->getRemoteAddr());
  269. if (!subnet) {
  270. LOG_WARN(dhcp6_logger, DHCP6_NO_SUBNET_REQ_OPT)
  271. .arg(question->getRemoteAddr().toText());
  272. return;
  273. }
  274. // Client requests some options using ORO option. Try to
  275. // get this option from client's message.
  276. boost::shared_ptr<OptionIntArray<uint16_t> > option_oro =
  277. boost::dynamic_pointer_cast<OptionIntArray<uint16_t> >(question->getOption(D6O_ORO));
  278. // Option ORO not found. Don't do anything then.
  279. if (!option_oro) {
  280. return;
  281. }
  282. // Get the list of options that client requested.
  283. const std::vector<uint16_t>& requested_opts = option_oro->getValues();
  284. // Get the list of options configured for a subnet.
  285. Subnet::OptionContainerPtr options = subnet->getOptionDescriptors("dhcp6");
  286. const Subnet::OptionContainerTypeIndex& idx = options->get<1>();
  287. // Try to match requested options with those configured for a subnet.
  288. // If match is found, append configured option to the answer message.
  289. BOOST_FOREACH(uint16_t opt, requested_opts) {
  290. const Subnet::OptionContainerTypeRange& range = idx.equal_range(opt);
  291. BOOST_FOREACH(Subnet::OptionDescriptor desc, range) {
  292. answer->addOption(desc.option);
  293. }
  294. }
  295. }
  296. OptionPtr Dhcpv6Srv::createStatusCode(uint16_t code, const std::string& text) {
  297. // @todo This function uses OptionCustom class to manage contents
  298. // of the data fields. Since this this option is frequently used
  299. // it may be good to implement dedicated class to avoid performance
  300. // impact.
  301. // Get the definition of the option holding status code.
  302. OptionDefinitionPtr status_code_def =
  303. LibDHCP::getOptionDef(Option::V6, D6O_STATUS_CODE);
  304. // This definition is assumed to be initialized in LibDHCP.
  305. assert(status_code_def);
  306. // As there is no dedicated class to represent Status Code
  307. // the OptionCustom class should be returned here.
  308. boost::shared_ptr<OptionCustom> option_status =
  309. boost::dynamic_pointer_cast<
  310. OptionCustom>(status_code_def->optionFactory(Option::V6, D6O_STATUS_CODE));
  311. assert(option_status);
  312. // Set status code to 'code' (0 - means data field #0).
  313. option_status->writeInteger(code, 0);
  314. // Set a message (1 - means data field #1).
  315. option_status->writeString(text, 1);
  316. return (option_status);
  317. }
  318. void Dhcpv6Srv::sanityCheck(const Pkt6Ptr& pkt, RequirementLevel clientid,
  319. RequirementLevel serverid) {
  320. Option::OptionCollection client_ids = pkt->getOptions(D6O_CLIENTID);
  321. switch (clientid) {
  322. case MANDATORY:
  323. if (client_ids.size() != 1) {
  324. isc_throw(RFCViolation, "Exactly 1 client-id option expected in "
  325. << pkt->getName() << ", but " << client_ids.size()
  326. << " received");
  327. }
  328. break;
  329. case OPTIONAL:
  330. if (client_ids.size() > 1) {
  331. isc_throw(RFCViolation, "Too many (" << client_ids.size()
  332. << ") client-id options received in " << pkt->getName());
  333. }
  334. break;
  335. case FORBIDDEN:
  336. // doesn't make sense - client-id is always allowed
  337. break;
  338. }
  339. Option::OptionCollection server_ids = pkt->getOptions(D6O_SERVERID);
  340. switch (serverid) {
  341. case FORBIDDEN:
  342. if (!server_ids.empty()) {
  343. isc_throw(RFCViolation, "Server-id option was not expected, but "
  344. << server_ids.size() << " received in " << pkt->getName());
  345. }
  346. break;
  347. case MANDATORY:
  348. if (server_ids.size() != 1) {
  349. isc_throw(RFCViolation, "Invalid number of server-id options received ("
  350. << server_ids.size() << "), exactly 1 expected in message "
  351. << pkt->getName());
  352. }
  353. break;
  354. case OPTIONAL:
  355. if (server_ids.size() > 1) {
  356. isc_throw(RFCViolation, "Too many (" << server_ids.size()
  357. << ") server-id options received in " << pkt->getName());
  358. }
  359. }
  360. }
  361. Subnet6Ptr Dhcpv6Srv::selectSubnet(const Pkt6Ptr& question) {
  362. Subnet6Ptr subnet = CfgMgr::instance().getSubnet6(question->getRemoteAddr());
  363. return (subnet);
  364. }
  365. void Dhcpv6Srv::assignLeases(const Pkt6Ptr& question, Pkt6Ptr& answer) {
  366. // We need to allocate addresses for all IA_NA options in the client's
  367. // question (i.e. SOLICIT or REQUEST) message.
  368. // @todo add support for IA_TA
  369. // @todo add support for IA_PD
  370. // We need to select a subnet the client is connected in.
  371. Subnet6Ptr subnet = selectSubnet(question);
  372. if (!subnet) {
  373. // This particular client is out of luck today. We do not have
  374. // information about the subnet he is connected to. This likely means
  375. // misconfiguration of the server (or some relays). We will continue to
  376. // process this message, but our response will be almost useless: no
  377. // addresses or prefixes, no subnet specific configuration etc. The only
  378. // thing this client can get is some global information (like DNS
  379. // servers).
  380. // perhaps this should be logged on some higher level? This is most likely
  381. // configuration bug.
  382. LOG_ERROR(dhcp6_logger, DHCP6_SUBNET_SELECTION_FAILED)
  383. .arg(question->getRemoteAddr().toText())
  384. .arg(question->getName());
  385. } else {
  386. LOG_DEBUG(dhcp6_logger, DBG_DHCP6_DETAIL_DATA, DHCP6_SUBNET_SELECTED)
  387. .arg(subnet->toText());
  388. }
  389. // @todo: We should implement Option6Duid some day, but we can do without it
  390. // just fine for now
  391. // Let's find client's DUID. Client is supposed to include its client-id
  392. // option almost all the time (the only exception is an anonymous inf-request,
  393. // but that is mostly a theoretical case). Our allocation engine needs DUID
  394. // and will refuse to allocate anything to anonymous clients.
  395. DuidPtr duid;
  396. OptionPtr opt_duid = question->getOption(D6O_CLIENTID);
  397. if (opt_duid) {
  398. duid = DuidPtr(new DUID(opt_duid->getData()));
  399. } else {
  400. LOG_DEBUG(dhcp6_logger, DBG_DHCP6_BASIC, DHCP6_CLIENTID_MISSING);
  401. // Let's drop the message. This client is not sane.
  402. isc_throw(RFCViolation, "Mandatory client-id is missing in received message");
  403. }
  404. // Now that we have all information about the client, let's iterate over all
  405. // received options and handle IA_NA options one by one and store our
  406. // responses in answer message (ADVERTISE or REPLY).
  407. //
  408. // @todo: expand this to cover IA_PD and IA_TA once we implement support for
  409. // prefix delegation and temporary addresses.
  410. for (Option::OptionCollection::iterator opt = question->options_.begin();
  411. opt != question->options_.end(); ++opt) {
  412. switch (opt->second->getType()) {
  413. case D6O_IA_NA: {
  414. OptionPtr answer_opt = assignIA_NA(subnet, duid, question,
  415. boost::dynamic_pointer_cast<Option6IA>(opt->second));
  416. if (answer_opt) {
  417. answer->addOption(answer_opt);
  418. }
  419. break;
  420. }
  421. default:
  422. break;
  423. }
  424. }
  425. }
  426. OptionPtr Dhcpv6Srv::assignIA_NA(const Subnet6Ptr& subnet, const DuidPtr& duid,
  427. Pkt6Ptr question, boost::shared_ptr<Option6IA> ia) {
  428. // If there is no subnet selected for handling this IA_NA, the only thing to do left is
  429. // to say that we are sorry, but the user won't get an address. As a convenience, we
  430. // use a different status text to indicate that (compare to the same status code,
  431. // but different wording below)
  432. if (!subnet) {
  433. // Create empty IA_NA option with IAID matching the request.
  434. // Note that we don't use OptionDefinition class to create this option.
  435. // This is because we prefer using a constructor of Option6IA that
  436. // initializes IAID. Otherwise we would have to use setIAID() after
  437. // creation of the option which has some performance implications.
  438. boost::shared_ptr<Option6IA> ia_rsp(new Option6IA(D6O_IA_NA, ia->getIAID()));
  439. // Insert status code NoAddrsAvail.
  440. ia_rsp->addOption(createStatusCode(STATUS_NoAddrsAvail, "Sorry, no subnet available."));
  441. return (ia_rsp);
  442. }
  443. // Check if the client sent us a hint in his IA_NA. Clients may send an
  444. // address in their IA_NA options as a suggestion (e.g. the last address
  445. // they used before).
  446. boost::shared_ptr<Option6IAAddr> hintOpt = boost::dynamic_pointer_cast<Option6IAAddr>
  447. (ia->getOption(D6O_IAADDR));
  448. IOAddress hint("::");
  449. if (hintOpt) {
  450. hint = hintOpt->getAddress();
  451. }
  452. LOG_DEBUG(dhcp6_logger, DBG_DHCP6_DETAIL, DHCP6_PROCESS_IA_NA_REQUEST)
  453. .arg(duid?duid->toText():"(no-duid)").arg(ia->getIAID())
  454. .arg(hintOpt?hint.toText():"(no hint)");
  455. // "Fake" allocation is processing of SOLICIT message. We pretend to do an
  456. // allocation, but we do not put the lease in the database. That is ok,
  457. // because we do not guarantee that the user will get that exact lease. If
  458. // the user selects this server to do actual allocation (i.e. sends REQUEST)
  459. // it should include this hint. That will help us during the actual lease
  460. // allocation.
  461. bool fake_allocation = false;
  462. if (question->getType() == DHCPV6_SOLICIT) {
  463. /// @todo: Check if we support rapid commit
  464. fake_allocation = true;
  465. }
  466. // Use allocation engine to pick a lease for this client. Allocation engine
  467. // will try to honour the hint, but it is just a hint - some other address
  468. // may be used instead. If fake_allocation is set to false, the lease will
  469. // be inserted into the LeaseMgr as well.
  470. Lease6Ptr lease = alloc_engine_->allocateAddress6(subnet, duid, ia->getIAID(),
  471. hint, fake_allocation);
  472. // Create IA_NA that we will put in the response.
  473. // Do not use OptionDefinition to create option's instance so
  474. // as we can initialize IAID using a constructor.
  475. boost::shared_ptr<Option6IA> ia_rsp(new Option6IA(D6O_IA_NA, ia->getIAID()));
  476. if (lease) {
  477. // We have a lease! Let's wrap its content into IA_NA option
  478. // with IAADDR suboption.
  479. LOG_DEBUG(dhcp6_logger, DBG_DHCP6_DETAIL, fake_allocation?
  480. DHCP6_LEASE_ADVERT:DHCP6_LEASE_ALLOC)
  481. .arg(lease->addr_.toText())
  482. .arg(duid?duid->toText():"(no-duid)")
  483. .arg(ia->getIAID());
  484. ia_rsp->setT1(subnet->getT1());
  485. ia_rsp->setT2(subnet->getT2());
  486. boost::shared_ptr<Option6IAAddr>
  487. addr(new Option6IAAddr(D6O_IAADDR,
  488. lease->addr_,
  489. lease->preferred_lft_,
  490. lease->valid_lft_));
  491. ia_rsp->addOption(addr);
  492. // It would be possible to insert status code=0(success) as well,
  493. // but this is considered waste of bandwidth as absence of status
  494. // code is considered a success.
  495. } else {
  496. // Allocation engine did not allocate a lease. The engine logged
  497. // cause of that failure. The only thing left is to insert
  498. // status code to pass the sad news to the client.
  499. LOG_DEBUG(dhcp6_logger, DBG_DHCP6_DETAIL, fake_allocation?
  500. DHCP6_LEASE_ADVERT_FAIL:DHCP6_LEASE_ALLOC_FAIL)
  501. .arg(duid?duid->toText():"(no-duid)")
  502. .arg(ia->getIAID())
  503. .arg(subnet->toText());
  504. ia_rsp->addOption(createStatusCode(STATUS_NoAddrsAvail,
  505. "Sorry, no address could be allocated."));
  506. }
  507. return (ia_rsp);
  508. }
  509. OptionPtr Dhcpv6Srv::renewIA_NA(const Subnet6Ptr& subnet, const DuidPtr& duid,
  510. Pkt6Ptr question, boost::shared_ptr<Option6IA> ia) {
  511. Lease6Ptr lease = LeaseMgrFactory::instance().getLease6(*duid, ia->getIAID(),
  512. subnet->getID());
  513. if (!lease) {
  514. // client renewing a lease that we don't know about.
  515. // Create empty IA_NA option with IAID matching the request.
  516. boost::shared_ptr<Option6IA> ia_rsp(new Option6IA(D6O_IA_NA, ia->getIAID()));
  517. // Insert status code NoAddrsAvail.
  518. ia_rsp->addOption(createStatusCode(STATUS_NoBinding,
  519. "Sorry, no known leases for this duid/iaid."));
  520. LOG_DEBUG(dhcp6_logger, DBG_DHCP6_DETAIL, DHCP6_UNKNOWN_RENEW)
  521. .arg(duid->toText())
  522. .arg(ia->getIAID())
  523. .arg(subnet->toText());
  524. return (ia_rsp);
  525. }
  526. lease->preferred_lft_ = subnet->getPreferred();
  527. lease->valid_lft_ = subnet->getValid();
  528. lease->t1_ = subnet->getT1();
  529. lease->t2_ = subnet->getT2();
  530. lease->cltt_ = time(NULL);
  531. LeaseMgrFactory::instance().updateLease6(lease);
  532. // Create empty IA_NA option with IAID matching the request.
  533. boost::shared_ptr<Option6IA> ia_rsp(new Option6IA(D6O_IA_NA, ia->getIAID()));
  534. ia_rsp->setT1(subnet->getT1());
  535. ia_rsp->setT2(subnet->getT2());
  536. boost::shared_ptr<Option6IAAddr> addr(new Option6IAAddr(D6O_IAADDR,
  537. lease->addr_, lease->preferred_lft_,
  538. lease->valid_lft_));
  539. ia_rsp->addOption(addr);
  540. return (ia_rsp);
  541. }
  542. void Dhcpv6Srv::renewLeases(const Pkt6Ptr& renew, Pkt6Ptr& reply) {
  543. // We need to renew addresses for all IA_NA options in the client's
  544. // RENEW message.
  545. // @todo add support for IA_TA
  546. // @todo add support for IA_PD
  547. // We need to select a subnet the client is connected in.
  548. Subnet6Ptr subnet = selectSubnet(renew);
  549. if (!subnet) {
  550. // This particular client is out of luck today. We do not have
  551. // information about the subnet he is connected to. This likely means
  552. // misconfiguration of the server (or some relays). We will continue to
  553. // process this message, but our response will be almost useless: no
  554. // addresses or prefixes, no subnet specific configuration etc. The only
  555. // thing this client can get is some global information (like DNS
  556. // servers).
  557. // perhaps this should be logged on some higher level? This is most likely
  558. // configuration bug.
  559. LOG_ERROR(dhcp6_logger, DHCP6_SUBNET_SELECTION_FAILED);
  560. } else {
  561. LOG_DEBUG(dhcp6_logger, DBG_DHCP6_DETAIL_DATA, DHCP6_SUBNET_SELECTED)
  562. .arg(subnet->toText());
  563. }
  564. // Let's find client's DUID. Client is supposed to include its client-id
  565. // option almost all the time (the only exception is an anonymous inf-request,
  566. // but that is mostly a theoretical case). Our allocation engine needs DUID
  567. // and will refuse to allocate anything to anonymous clients.
  568. OptionPtr opt_duid = renew->getOption(D6O_CLIENTID);
  569. if (!opt_duid) {
  570. // This should not happen. We have checked this before.
  571. reply->addOption(createStatusCode(STATUS_UnspecFail,
  572. "You did not include mandatory client-id"));
  573. return;
  574. }
  575. DuidPtr duid(new DUID(opt_duid->getData()));
  576. for (Option::OptionCollection::iterator opt = renew->options_.begin();
  577. opt != renew->options_.end(); ++opt) {
  578. switch (opt->second->getType()) {
  579. case D6O_IA_NA: {
  580. OptionPtr answer_opt = renewIA_NA(subnet, duid, renew,
  581. boost::dynamic_pointer_cast<Option6IA>(opt->second));
  582. if (answer_opt) {
  583. reply->addOption(answer_opt);
  584. }
  585. break;
  586. }
  587. default:
  588. break;
  589. }
  590. }
  591. }
  592. void Dhcpv6Srv::releaseLeases(const Pkt6Ptr& release, Pkt6Ptr& reply) {
  593. // We need to release addresses for all IA_NA options in the client's
  594. // RELEASE message.
  595. // @todo Add support for IA_TA
  596. // @todo Add support for IA_PD
  597. // @todo Consider supporting more than one address in a single IA_NA.
  598. // That was envisaged by RFC3315, but it never happened. The only
  599. // software that supports that is Dibbler, but its author seriously doubts
  600. // if anyone is really using it. Clients that want more than one address
  601. // just include more instances of IA_NA options.
  602. // Let's find client's DUID. Client is supposed to include its client-id
  603. // option almost all the time (the only exception is an anonymous inf-request,
  604. // but that is mostly a theoretical case). Our allocation engine needs DUID
  605. // and will refuse to allocate anything to anonymous clients.
  606. OptionPtr opt_duid = release->getOption(D6O_CLIENTID);
  607. if (!opt_duid) {
  608. // This should not happen. We have checked this before.
  609. // see sanityCheck() called from processRelease()
  610. LOG_WARN(dhcp6_logger, DHCP6_RELEASE_MISSING_CLIENTID)
  611. .arg(release->getRemoteAddr().toText());
  612. reply->addOption(createStatusCode(STATUS_UnspecFail,
  613. "You did not include mandatory client-id"));
  614. return;
  615. }
  616. DuidPtr duid(new DUID(opt_duid->getData()));
  617. int general_status = STATUS_Success;
  618. for (Option::OptionCollection::iterator opt = release->options_.begin();
  619. opt != release->options_.end(); ++opt) {
  620. switch (opt->second->getType()) {
  621. case D6O_IA_NA: {
  622. OptionPtr answer_opt = releaseIA_NA(duid, release, general_status,
  623. boost::dynamic_pointer_cast<Option6IA>(opt->second));
  624. if (answer_opt) {
  625. reply->addOption(answer_opt);
  626. }
  627. break;
  628. }
  629. // @todo: add support for IA_PD
  630. // @todo: add support for IA_TA
  631. default:
  632. // remaining options are stateless and thus ignored in this context
  633. ;
  634. }
  635. }
  636. // To be pedantic, we should also include status code in the top-level
  637. // scope, not just in each IA_NA. See RFC3315, section 18.2.6.
  638. // This behavior will likely go away in RFC3315bis.
  639. reply->addOption(createStatusCode(general_status,
  640. "Summary status for all processed IA_NAs"));
  641. }
  642. OptionPtr Dhcpv6Srv::releaseIA_NA(const DuidPtr& duid, Pkt6Ptr question,
  643. int& general_status,
  644. boost::shared_ptr<Option6IA> ia) {
  645. // Release can be done in one of two ways:
  646. // Approach 1: extract address from client's IA_NA and see if it belongs
  647. // to this particular client.
  648. // Approach 2: find a subnet for this client, get a lease for
  649. // this subnet/duid/iaid and check if its content matches to what the
  650. // client is asking us to release.
  651. //
  652. // This method implements approach 1.
  653. // That's our response
  654. boost::shared_ptr<Option6IA> ia_rsp(new Option6IA(D6O_IA_NA, ia->getIAID()));
  655. boost::shared_ptr<Option6IAAddr> release_addr = boost::dynamic_pointer_cast<Option6IAAddr>
  656. (ia->getOption(D6O_IAADDR));
  657. if (!release_addr) {
  658. ia_rsp->addOption(createStatusCode(STATUS_NoBinding,
  659. "You did not include address in your RELEASE"));
  660. general_status = STATUS_NoBinding;
  661. return (ia_rsp);
  662. }
  663. Lease6Ptr lease = LeaseMgrFactory::instance().getLease6(release_addr->getAddress());
  664. if (!lease) {
  665. // client releasing a lease that we don't know about.
  666. // Insert status code NoAddrsAvail.
  667. ia_rsp->addOption(createStatusCode(STATUS_NoBinding,
  668. "Sorry, no known leases for this duid/iaid, can't release."));
  669. general_status = STATUS_NoBinding;
  670. LOG_INFO(dhcp6_logger, DHCP6_UNKNOWN_RELEASE)
  671. .arg(duid->toText())
  672. .arg(ia->getIAID());
  673. return (ia_rsp);
  674. }
  675. if (!lease->duid_) {
  676. // Something is gravely wrong here. We do have a lease, but it does not
  677. // have mandatory DUID information attached. Someone was messing with our
  678. // database.
  679. LOG_ERROR(dhcp6_logger, DHCP6_LEASE_WITHOUT_DUID)
  680. .arg(release_addr->getAddress().toText());
  681. general_status = STATUS_UnspecFail;
  682. ia_rsp->addOption(createStatusCode(STATUS_UnspecFail,
  683. "Database consistency check failed when trying to RELEASE"));
  684. return (ia_rsp);
  685. }
  686. if (*duid != *(lease->duid_)) {
  687. // Sorry, it's not your address. You can't release it.
  688. LOG_INFO(dhcp6_logger, DHCP6_RELEASE_FAIL_WRONG_DUID)
  689. .arg(duid->toText())
  690. .arg(release_addr->getAddress().toText())
  691. .arg(lease->duid_->toText());
  692. general_status = STATUS_NoBinding;
  693. ia_rsp->addOption(createStatusCode(STATUS_NoBinding,
  694. "This address does not belong to you, you can't release it"));
  695. return (ia_rsp);
  696. }
  697. if (ia->getIAID() != lease->iaid_) {
  698. // This address belongs to this client, but to a different IA
  699. LOG_WARN(dhcp6_logger, DHCP6_RELEASE_FAIL_WRONG_IAID)
  700. .arg(duid->toText())
  701. .arg(release_addr->getAddress().toText())
  702. .arg(lease->iaid_)
  703. .arg(ia->getIAID());
  704. ia_rsp->addOption(createStatusCode(STATUS_NoBinding,
  705. "This is your address, but you used wrong IAID"));
  706. general_status = STATUS_NoBinding;
  707. return (ia_rsp);
  708. }
  709. // It is not necessary to check if the address matches as we used
  710. // getLease6(addr) method that is supposed to return a proper lease.
  711. // Ok, we've passed all checks. Let's release this address.
  712. if (!LeaseMgrFactory::instance().deleteLease(lease->addr_)) {
  713. ia_rsp->addOption(createStatusCode(STATUS_UnspecFail,
  714. "Server failed to release a lease"));
  715. LOG_ERROR(dhcp6_logger, DHCP6_RELEASE_FAIL)
  716. .arg(lease->addr_.toText())
  717. .arg(duid->toText())
  718. .arg(lease->iaid_);
  719. general_status = STATUS_UnspecFail;
  720. return (ia_rsp);
  721. } else {
  722. LOG_DEBUG(dhcp6_logger, DBG_DHCP6_DETAIL, DHCP6_RELEASE)
  723. .arg(lease->addr_.toText())
  724. .arg(duid->toText())
  725. .arg(lease->iaid_);
  726. ia_rsp->addOption(createStatusCode(STATUS_Success,
  727. "Lease released. Thank you, please come again."));
  728. return (ia_rsp);
  729. }
  730. }
  731. Pkt6Ptr Dhcpv6Srv::processSolicit(const Pkt6Ptr& solicit) {
  732. sanityCheck(solicit, MANDATORY, FORBIDDEN);
  733. Pkt6Ptr advertise(new Pkt6(DHCPV6_ADVERTISE, solicit->getTransid()));
  734. copyDefaultOptions(solicit, advertise);
  735. appendDefaultOptions(solicit, advertise);
  736. appendRequestedOptions(solicit, advertise);
  737. assignLeases(solicit, advertise);
  738. return (advertise);
  739. }
  740. Pkt6Ptr Dhcpv6Srv::processRequest(const Pkt6Ptr& request) {
  741. sanityCheck(request, MANDATORY, MANDATORY);
  742. Pkt6Ptr reply(new Pkt6(DHCPV6_REPLY, request->getTransid()));
  743. copyDefaultOptions(request, reply);
  744. appendDefaultOptions(request, reply);
  745. appendRequestedOptions(request, reply);
  746. assignLeases(request, reply);
  747. return (reply);
  748. }
  749. Pkt6Ptr Dhcpv6Srv::processRenew(const Pkt6Ptr& renew) {
  750. sanityCheck(renew, MANDATORY, MANDATORY);
  751. Pkt6Ptr reply(new Pkt6(DHCPV6_REPLY, renew->getTransid()));
  752. copyDefaultOptions(renew, reply);
  753. appendDefaultOptions(renew, reply);
  754. appendRequestedOptions(renew, reply);
  755. renewLeases(renew, reply);
  756. return reply;
  757. }
  758. Pkt6Ptr Dhcpv6Srv::processRebind(const Pkt6Ptr& rebind) {
  759. /// @todo: Implement this
  760. Pkt6Ptr reply(new Pkt6(DHCPV6_REPLY, rebind->getTransid()));
  761. return reply;
  762. }
  763. Pkt6Ptr Dhcpv6Srv::processConfirm(const Pkt6Ptr& confirm) {
  764. /// @todo: Implement this
  765. Pkt6Ptr reply(new Pkt6(DHCPV6_REPLY, confirm->getTransid()));
  766. return reply;
  767. }
  768. Pkt6Ptr Dhcpv6Srv::processRelease(const Pkt6Ptr& release) {
  769. sanityCheck(release, MANDATORY, MANDATORY);
  770. Pkt6Ptr reply(new Pkt6(DHCPV6_REPLY, release->getTransid()));
  771. copyDefaultOptions(release, reply);
  772. appendDefaultOptions(release, reply);
  773. releaseLeases(release, reply);
  774. return reply;
  775. }
  776. Pkt6Ptr Dhcpv6Srv::processDecline(const Pkt6Ptr& decline) {
  777. /// @todo: Implement this
  778. Pkt6Ptr reply(new Pkt6(DHCPV6_REPLY, decline->getTransid()));
  779. return reply;
  780. }
  781. Pkt6Ptr Dhcpv6Srv::processInfRequest(const Pkt6Ptr& infRequest) {
  782. /// @todo: Implement this
  783. Pkt6Ptr reply(new Pkt6(DHCPV6_REPLY, infRequest->getTransid()));
  784. return reply;
  785. }
  786. };
  787. };