dhcp6_srv.cc 71 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857
  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_ddns/ncr_msg.h>
  17. #include <dhcp/dhcp6.h>
  18. #include <dhcp/duid.h>
  19. #include <dhcp/iface_mgr.h>
  20. #include <dhcp/libdhcp++.h>
  21. #include <dhcp/option6_addrlst.h>
  22. #include <dhcp/option6_client_fqdn.h>
  23. #include <dhcp/option6_ia.h>
  24. #include <dhcp/option6_iaaddr.h>
  25. #include <dhcp/option6_iaaddr.h>
  26. #include <dhcp/option_custom.h>
  27. #include <dhcp/option_int_array.h>
  28. #include <dhcp/pkt6.h>
  29. #include <dhcp6/dhcp6_log.h>
  30. #include <dhcp6/dhcp6_srv.h>
  31. #include <dhcpsrv/callout_handle_store.h>
  32. #include <dhcpsrv/cfgmgr.h>
  33. #include <dhcpsrv/lease_mgr.h>
  34. #include <dhcpsrv/lease_mgr_factory.h>
  35. #include <dhcpsrv/subnet.h>
  36. #include <dhcpsrv/utils.h>
  37. #include <exceptions/exceptions.h>
  38. #include <hooks/callout_handle.h>
  39. #include <hooks/hooks_manager.h>
  40. #include <util/encode/hex.h>
  41. #include <util/io_utilities.h>
  42. #include <util/range_utilities.h>
  43. #include <boost/foreach.hpp>
  44. #include <boost/tokenizer.hpp>
  45. #include <boost/algorithm/string/erase.hpp>
  46. #include <stdlib.h>
  47. #include <time.h>
  48. #include <iomanip>
  49. #include <fstream>
  50. using namespace isc;
  51. using namespace isc::asiolink;
  52. using namespace isc::dhcp_ddns;
  53. using namespace isc::dhcp;
  54. using namespace isc::hooks;
  55. using namespace isc::util;
  56. using namespace std;
  57. namespace {
  58. /// Structure that holds registered hook indexes
  59. struct Dhcp6Hooks {
  60. int hook_index_buffer6_receive_;///< index for "buffer6_receive" hook point
  61. int hook_index_pkt6_receive_; ///< index for "pkt6_receive" hook point
  62. int hook_index_subnet6_select_; ///< index for "subnet6_select" hook point
  63. int hook_index_lease6_renew_; ///< index for "lease6_renew" hook point
  64. int hook_index_lease6_release_; ///< index for "lease6_release" hook point
  65. int hook_index_pkt6_send_; ///< index for "pkt6_send" hook point
  66. int hook_index_buffer6_send_; ///< index for "buffer6_send" hook point
  67. /// Constructor that registers hook points for DHCPv6 engine
  68. Dhcp6Hooks() {
  69. hook_index_buffer6_receive_= HooksManager::registerHook("buffer6_receive");
  70. hook_index_pkt6_receive_ = HooksManager::registerHook("pkt6_receive");
  71. hook_index_subnet6_select_ = HooksManager::registerHook("subnet6_select");
  72. hook_index_lease6_renew_ = HooksManager::registerHook("lease6_renew");
  73. hook_index_lease6_release_ = HooksManager::registerHook("lease6_release");
  74. hook_index_pkt6_send_ = HooksManager::registerHook("pkt6_send");
  75. hook_index_buffer6_send_ = HooksManager::registerHook("buffer6_send");
  76. }
  77. };
  78. // Declare a Hooks object. As this is outside any function or method, it
  79. // will be instantiated (and the constructor run) when the module is loaded.
  80. // As a result, the hook indexes will be defined before any method in this
  81. // module is called.
  82. Dhcp6Hooks Hooks;
  83. }; // anonymous namespace
  84. namespace isc {
  85. namespace dhcp {
  86. namespace {
  87. // The following constants describe server's behavior with respect to the
  88. // DHCPv6 Client FQDN Option sent by a client. They will be removed
  89. // when DDNS parameters for DHCPv6 are implemented with the ticket #3034.
  90. // Should server always include the FQDN option in its response, regardless
  91. // if it has been requested in ORO (Disabled).
  92. const bool FQDN_ALWAYS_INCLUDE = false;
  93. // Enable AAAA RR update delegation to the client (Disabled).
  94. const bool FQDN_ALLOW_CLIENT_UPDATE = false;
  95. // Globally enable updates (Enabled).
  96. const bool FQDN_ENABLE_UPDATE = true;
  97. // The partial name generated for the client if empty name has been
  98. // supplied.
  99. const char* FQDN_GENERATED_PARTIAL_NAME = "myhost";
  100. // Do update, even if client requested no updates with N flag (Disabled).
  101. const bool FQDN_OVERRIDE_NO_UPDATE = false;
  102. // Server performs an update when client requested delegation (Enabled).
  103. const bool FQDN_OVERRIDE_CLIENT_UPDATE = true;
  104. // The fully qualified domain-name suffix if partial name provided by
  105. // a client.
  106. const char* FQDN_PARTIAL_SUFFIX = "example.com";
  107. // Should server replace the domain-name supplied by the client (Disabled).
  108. const bool FQDN_REPLACE_CLIENT_NAME = false;
  109. }
  110. /// @brief file name of a server-id file
  111. ///
  112. /// Server must store its duid in persistent storage that must not change
  113. /// between restarts. This is name of the file that is created in dataDir
  114. /// (see isc::dhcp::CfgMgr::getDataDir()). It is a text file that uses
  115. /// double digit hex values separated by colons format, e.g.
  116. /// 01:ff:02:03:06:80:90:ab:cd:ef. Server will create it during first
  117. /// run and then use it afterwards.
  118. static const char* SERVER_DUID_FILE = "b10-dhcp6-serverid";
  119. Dhcpv6Srv::Dhcpv6Srv(uint16_t port)
  120. :alloc_engine_(), serverid_(), shutdown_(true), port_(port)
  121. {
  122. LOG_DEBUG(dhcp6_logger, DBG_DHCP6_START, DHCP6_OPEN_SOCKET).arg(port);
  123. // Initialize objects required for DHCP server operation.
  124. try {
  125. // Port 0 is used for testing purposes. It means that the server should
  126. // not open any sockets at all. Some tests, e.g. configuration parser,
  127. // require Dhcpv6Srv object, but they don't really need it to do
  128. // anything. This speed up and simplifies the tests.
  129. if (port > 0) {
  130. if (IfaceMgr::instance().countIfaces() == 0) {
  131. LOG_ERROR(dhcp6_logger, DHCP6_NO_INTERFACES);
  132. return;
  133. }
  134. IfaceMgr::instance().openSockets6(port_);
  135. }
  136. string duid_file = CfgMgr::instance().getDataDir() + "/" + string(SERVER_DUID_FILE);
  137. if (loadServerID(duid_file)) {
  138. LOG_DEBUG(dhcp6_logger, DBG_DHCP6_START, DHCP6_SERVERID_LOADED)
  139. .arg(duidToString(getServerID()))
  140. .arg(duid_file);
  141. } else {
  142. generateServerID();
  143. LOG_INFO(dhcp6_logger, DHCP6_SERVERID_GENERATED)
  144. .arg(duidToString(getServerID()))
  145. .arg(duid_file);
  146. if (!writeServerID(duid_file)) {
  147. LOG_WARN(dhcp6_logger, DHCP6_SERVERID_WRITE_FAIL)
  148. .arg(duid_file);
  149. }
  150. }
  151. // Instantiate allocation engine
  152. alloc_engine_.reset(new AllocEngine(AllocEngine::ALLOC_ITERATIVE, 100));
  153. /// @todo call loadLibraries() when handling configuration changes
  154. } catch (const std::exception &e) {
  155. LOG_ERROR(dhcp6_logger, DHCP6_SRV_CONSTRUCT_ERROR).arg(e.what());
  156. return;
  157. }
  158. // All done, so can proceed
  159. shutdown_ = false;
  160. }
  161. Dhcpv6Srv::~Dhcpv6Srv() {
  162. IfaceMgr::instance().closeSockets();
  163. LeaseMgrFactory::destroy();
  164. }
  165. void Dhcpv6Srv::shutdown() {
  166. LOG_DEBUG(dhcp6_logger, DBG_DHCP6_BASIC, DHCP6_SHUTDOWN_REQUEST);
  167. shutdown_ = true;
  168. }
  169. Pkt6Ptr Dhcpv6Srv::receivePacket(int timeout) {
  170. return (IfaceMgr::instance().receive6(timeout));
  171. }
  172. void Dhcpv6Srv::sendPacket(const Pkt6Ptr& packet) {
  173. IfaceMgr::instance().send(packet);
  174. }
  175. bool Dhcpv6Srv::run() {
  176. while (!shutdown_) {
  177. /// @todo Calculate actual timeout to the next event (e.g. lease
  178. /// expiration) once we have lease database. The idea here is that
  179. /// it is possible to do everything in a single process/thread.
  180. /// For now, we are just calling select for 1000 seconds. There
  181. /// were some issues reported on some systems when calling select()
  182. /// with too large values. Unfortunately, I don't recall the details.
  183. //cppcheck-suppress variableScope This is temporary anyway
  184. const int timeout = 1000;
  185. // client's message and server's response
  186. Pkt6Ptr query;
  187. Pkt6Ptr rsp;
  188. try {
  189. query = receivePacket(timeout);
  190. } catch (const std::exception& e) {
  191. LOG_ERROR(dhcp6_logger, DHCP6_PACKET_RECEIVE_FAIL).arg(e.what());
  192. }
  193. // Timeout may be reached or signal received, which breaks select() with no packet received
  194. if (!query) {
  195. continue;
  196. }
  197. bool skip_unpack = false;
  198. // The packet has just been received so contains the uninterpreted wire
  199. // data; execute callouts registered for buffer6_receive.
  200. if (HooksManager::getHooksManager().calloutsPresent(Hooks.hook_index_buffer6_receive_)) {
  201. CalloutHandlePtr callout_handle = getCalloutHandle(query);
  202. // Delete previously set arguments
  203. callout_handle->deleteAllArguments();
  204. // Pass incoming packet as argument
  205. callout_handle->setArgument("query6", query);
  206. // Call callouts
  207. HooksManager::callCallouts(Hooks.hook_index_buffer6_receive_, *callout_handle);
  208. // Callouts decided to skip the next processing step. The next
  209. // processing step would to parse the packet, so skip at this
  210. // stage means that callouts did the parsing already, so server
  211. // should skip parsing.
  212. if (callout_handle->getSkip()) {
  213. LOG_DEBUG(dhcp6_logger, DBG_DHCP6_HOOKS, DHCP6_HOOK_BUFFER_RCVD_SKIP);
  214. skip_unpack = true;
  215. }
  216. callout_handle->getArgument("query6", query);
  217. }
  218. // Unpack the packet information unless the buffer6_receive callouts
  219. // indicated they did it
  220. if (!skip_unpack) {
  221. if (!query->unpack()) {
  222. LOG_DEBUG(dhcp6_logger, DBG_DHCP6_DETAIL,
  223. DHCP6_PACKET_PARSE_FAIL);
  224. continue;
  225. }
  226. }
  227. LOG_DEBUG(dhcp6_logger, DBG_DHCP6_DETAIL, DHCP6_PACKET_RECEIVED)
  228. .arg(query->getName());
  229. LOG_DEBUG(dhcp6_logger, DBG_DHCP6_DETAIL_DATA, DHCP6_QUERY_DATA)
  230. .arg(static_cast<int>(query->getType()))
  231. .arg(query->getBuffer().getLength())
  232. .arg(query->toText());
  233. // At this point the information in the packet has been unpacked into
  234. // the various packet fields and option objects has been cretated.
  235. // Execute callouts registered for packet6_receive.
  236. if (HooksManager::getHooksManager().calloutsPresent(Hooks.hook_index_pkt6_receive_)) {
  237. CalloutHandlePtr callout_handle = getCalloutHandle(query);
  238. // Delete previously set arguments
  239. callout_handle->deleteAllArguments();
  240. // Pass incoming packet as argument
  241. callout_handle->setArgument("query6", query);
  242. // Call callouts
  243. HooksManager::callCallouts(Hooks.hook_index_pkt6_receive_, *callout_handle);
  244. // Callouts decided to skip the next processing step. The next
  245. // processing step would to process the packet, so skip at this
  246. // stage means drop.
  247. if (callout_handle->getSkip()) {
  248. LOG_DEBUG(dhcp6_logger, DBG_DHCP6_HOOKS, DHCP6_HOOK_PACKET_RCVD_SKIP);
  249. continue;
  250. }
  251. callout_handle->getArgument("query6", query);
  252. }
  253. try {
  254. NameChangeRequestPtr ncr;
  255. switch (query->getType()) {
  256. case DHCPV6_SOLICIT:
  257. rsp = processSolicit(query);
  258. break;
  259. case DHCPV6_REQUEST:
  260. rsp = processRequest(query);
  261. break;
  262. case DHCPV6_RENEW:
  263. rsp = processRenew(query);
  264. break;
  265. case DHCPV6_REBIND:
  266. rsp = processRebind(query);
  267. break;
  268. case DHCPV6_CONFIRM:
  269. rsp = processConfirm(query);
  270. break;
  271. case DHCPV6_RELEASE:
  272. rsp = processRelease(query);
  273. break;
  274. case DHCPV6_DECLINE:
  275. rsp = processDecline(query);
  276. break;
  277. case DHCPV6_INFORMATION_REQUEST:
  278. rsp = processInfRequest(query);
  279. break;
  280. default:
  281. // We received a packet type that we do not recognize.
  282. LOG_DEBUG(dhcp6_logger, DBG_DHCP6_BASIC, DHCP6_UNKNOWN_MSG_RECEIVED)
  283. .arg(static_cast<int>(query->getType()))
  284. .arg(query->getIface());
  285. // Only action is to output a message if debug is enabled,
  286. // and that will be covered by the debug statement before
  287. // the "switch" statement.
  288. ;
  289. }
  290. } catch (const RFCViolation& e) {
  291. LOG_DEBUG(dhcp6_logger, DBG_DHCP6_BASIC, DHCP6_REQUIRED_OPTIONS_CHECK_FAIL)
  292. .arg(query->getName())
  293. .arg(query->getRemoteAddr().toText())
  294. .arg(e.what());
  295. } catch (const isc::Exception& e) {
  296. // Catch-all exception (at least for ones based on the isc
  297. // Exception class, which covers more or less all that
  298. // are explicitly raised in the BIND 10 code). Just log
  299. // the problem and ignore the packet. (The problem is logged
  300. // as a debug message because debug is disabled by default -
  301. // it prevents a DDOS attack based on the sending of problem
  302. // packets.)
  303. LOG_DEBUG(dhcp6_logger, DBG_DHCP6_BASIC, DHCP6_PACKET_PROCESS_FAIL)
  304. .arg(query->getName())
  305. .arg(query->getRemoteAddr().toText())
  306. .arg(e.what());
  307. }
  308. if (rsp) {
  309. rsp->setRemoteAddr(query->getRemoteAddr());
  310. rsp->setLocalAddr(query->getLocalAddr());
  311. rsp->setRemotePort(DHCP6_CLIENT_PORT);
  312. rsp->setLocalPort(DHCP6_SERVER_PORT);
  313. rsp->setIndex(query->getIndex());
  314. rsp->setIface(query->getIface());
  315. // Specifies if server should do the packing
  316. bool skip_pack = false;
  317. // Server's reply packet now has all options and fields set.
  318. // Options are represented by individual objects, but the
  319. // output wire data has not been prepared yet.
  320. // Execute all callouts registered for packet6_send
  321. if (HooksManager::getHooksManager().calloutsPresent(Hooks.hook_index_pkt6_send_)) {
  322. CalloutHandlePtr callout_handle = getCalloutHandle(query);
  323. // Delete all previous arguments
  324. callout_handle->deleteAllArguments();
  325. // Set our response
  326. callout_handle->setArgument("response6", rsp);
  327. // Call all installed callouts
  328. HooksManager::callCallouts(Hooks.hook_index_pkt6_send_, *callout_handle);
  329. // Callouts decided to skip the next processing step. The next
  330. // processing step would to pack the packet (create wire data).
  331. // That step will be skipped if any callout sets skip flag.
  332. // It essentially means that the callout already did packing,
  333. // so the server does not have to do it again.
  334. if (callout_handle->getSkip()) {
  335. LOG_DEBUG(dhcp6_logger, DBG_DHCP6_HOOKS, DHCP6_HOOK_PACKET_SEND_SKIP);
  336. skip_pack = true;
  337. }
  338. }
  339. LOG_DEBUG(dhcp6_logger, DBG_DHCP6_DETAIL_DATA,
  340. DHCP6_RESPONSE_DATA)
  341. .arg(static_cast<int>(rsp->getType())).arg(rsp->toText());
  342. if (!skip_pack) {
  343. try {
  344. rsp->pack();
  345. } catch (const std::exception& e) {
  346. LOG_ERROR(dhcp6_logger, DHCP6_PACK_FAIL)
  347. .arg(e.what());
  348. continue;
  349. }
  350. }
  351. try {
  352. // Now all fields and options are constructed into output wire buffer.
  353. // Option objects modification does not make sense anymore. Hooks
  354. // can only manipulate wire buffer at this stage.
  355. // Let's execute all callouts registered for buffer6_send
  356. if (HooksManager::getHooksManager().calloutsPresent(Hooks.hook_index_buffer6_send_)) {
  357. CalloutHandlePtr callout_handle = getCalloutHandle(query);
  358. // Delete previously set arguments
  359. callout_handle->deleteAllArguments();
  360. // Pass incoming packet as argument
  361. callout_handle->setArgument("response6", rsp);
  362. // Call callouts
  363. HooksManager::callCallouts(Hooks.hook_index_buffer6_send_, *callout_handle);
  364. // Callouts decided to skip the next processing step. The next
  365. // processing step would to parse the packet, so skip at this
  366. // stage means drop.
  367. if (callout_handle->getSkip()) {
  368. LOG_DEBUG(dhcp6_logger, DBG_DHCP6_HOOKS, DHCP6_HOOK_BUFFER_SEND_SKIP);
  369. continue;
  370. }
  371. callout_handle->getArgument("response6", rsp);
  372. }
  373. LOG_DEBUG(dhcp6_logger, DBG_DHCP6_DETAIL_DATA,
  374. DHCP6_RESPONSE_DATA)
  375. .arg(static_cast<int>(rsp->getType())).arg(rsp->toText());
  376. sendPacket(rsp);
  377. } catch (const std::exception& e) {
  378. LOG_ERROR(dhcp6_logger, DHCP6_PACKET_SEND_FAIL)
  379. .arg(e.what());
  380. }
  381. // Send NameChangeRequests to the b10-dhcp-ddns module.
  382. sendNameChangeRequests();
  383. }
  384. }
  385. return (true);
  386. }
  387. bool Dhcpv6Srv::loadServerID(const std::string& file_name) {
  388. // load content of the file into a string
  389. fstream f(file_name.c_str(), ios::in);
  390. if (!f.is_open()) {
  391. return (false);
  392. }
  393. string hex_string;
  394. f >> hex_string;
  395. f.close();
  396. // remove any spaces
  397. boost::algorithm::erase_all(hex_string, " ");
  398. // now remove :
  399. /// @todo: We should check first if the format is sane.
  400. /// Otherwise 1:2:3:4 will be converted to 0x12, 0x34
  401. boost::algorithm::erase_all(hex_string, ":");
  402. std::vector<uint8_t> bin;
  403. // Decode the hex string and store it in bin (which happens
  404. // to be OptionBuffer format)
  405. isc::util::encode::decodeHex(hex_string, bin);
  406. // Now create server-id option
  407. serverid_.reset(new Option(Option::V6, D6O_SERVERID, bin));
  408. return (true);
  409. }
  410. std::string
  411. Dhcpv6Srv::duidToString(const OptionPtr& opt) {
  412. stringstream tmp;
  413. OptionBuffer data = opt->getData();
  414. bool colon = false;
  415. for (OptionBufferConstIter it = data.begin(); it != data.end(); ++it) {
  416. if (colon) {
  417. tmp << ":";
  418. }
  419. tmp << hex << setw(2) << setfill('0') << static_cast<uint16_t>(*it);
  420. if (!colon) {
  421. colon = true;
  422. }
  423. }
  424. return tmp.str();
  425. }
  426. bool
  427. Dhcpv6Srv::writeServerID(const std::string& file_name) {
  428. fstream f(file_name.c_str(), ios::out | ios::trunc);
  429. if (!f.good()) {
  430. return (false);
  431. }
  432. f << duidToString(getServerID());
  433. f.close();
  434. return (true);
  435. }
  436. void
  437. Dhcpv6Srv::generateServerID() {
  438. /// @todo: This code implements support for DUID-LLT (the recommended one).
  439. /// We should eventually add support for other DUID types: DUID-LL, DUID-EN
  440. /// and DUID-UUID
  441. const IfaceMgr::IfaceCollection& ifaces = IfaceMgr::instance().getIfaces();
  442. // Let's find suitable interface.
  443. for (IfaceMgr::IfaceCollection::const_iterator iface = ifaces.begin();
  444. iface != ifaces.end(); ++iface) {
  445. // All the following checks could be merged into one multi-condition
  446. // statement, but let's keep them separated as perhaps one day
  447. // we will grow knobs to selectively turn them on or off. Also,
  448. // this code is used only *once* during first start on a new machine
  449. // and then server-id is stored. (or at least it will be once
  450. // DUID storage is implemented)
  451. // I wish there was a this_is_a_real_physical_interface flag...
  452. // MAC address should be at least 6 bytes. Although there is no such
  453. // requirement in any RFC, all decent physical interfaces (Ethernet,
  454. // WiFi, Infiniband, etc.) have 6 bytes long MAC address. We want to
  455. // base our DUID on real hardware address, rather than virtual
  456. // interface that pretends that underlying IP address is its MAC.
  457. if (iface->getMacLen() < MIN_MAC_LEN) {
  458. continue;
  459. }
  460. // Let's don't use loopback.
  461. if (iface->flag_loopback_) {
  462. continue;
  463. }
  464. // Let's skip downed interfaces. It is better to use working ones.
  465. if (!iface->flag_up_) {
  466. continue;
  467. }
  468. // Some interfaces (like lo on Linux) report 6-bytes long
  469. // MAC address 00:00:00:00:00:00. Let's not use such weird interfaces
  470. // to generate DUID.
  471. if (isRangeZero(iface->getMac(), iface->getMac() + iface->getMacLen())) {
  472. continue;
  473. }
  474. // Ok, we have useful MAC. Let's generate DUID-LLT based on
  475. // it. See RFC3315, Section 9.2 for details.
  476. // DUID uses seconds since midnight of 01-01-2000, time() returns
  477. // seconds since 01-01-1970. DUID_TIME_EPOCH substution corrects that.
  478. time_t seconds = time(NULL);
  479. seconds -= DUID_TIME_EPOCH;
  480. OptionBuffer srvid(8 + iface->getMacLen());
  481. writeUint16(DUID::DUID_LLT, &srvid[0]);
  482. writeUint16(HWTYPE_ETHERNET, &srvid[2]);
  483. writeUint32(static_cast<uint32_t>(seconds), &srvid[4]);
  484. memcpy(&srvid[0] + 8, iface->getMac(), iface->getMacLen());
  485. serverid_ = OptionPtr(new Option(Option::V6, D6O_SERVERID,
  486. srvid.begin(), srvid.end()));
  487. return;
  488. }
  489. // If we reached here, there are no suitable interfaces found.
  490. // Either interface detection is not supported on this platform or
  491. // this is really weird box. Let's use DUID-EN instead.
  492. // See Section 9.3 of RFC3315 for details.
  493. OptionBuffer srvid(12);
  494. writeUint16(DUID::DUID_EN, &srvid[0]);
  495. writeUint32(ENTERPRISE_ID_ISC, &srvid[2]);
  496. // Length of the identifier is company specific. I hereby declare
  497. // ISC "standard" of 6 bytes long pseudo-random numbers.
  498. srandom(time(NULL));
  499. fillRandom(&srvid[6], &srvid[12]);
  500. serverid_ = OptionPtr(new Option(Option::V6, D6O_SERVERID,
  501. srvid.begin(), srvid.end()));
  502. }
  503. void
  504. Dhcpv6Srv::copyDefaultOptions(const Pkt6Ptr& question, Pkt6Ptr& answer) {
  505. // Add client-id.
  506. OptionPtr clientid = question->getOption(D6O_CLIENTID);
  507. if (clientid) {
  508. answer->addOption(clientid);
  509. }
  510. /// @todo: Should throw if there is no client-id (except anonymous INF-REQUEST)
  511. // If this is a relayed message, we need to copy relay information
  512. if (!question->relay_info_.empty()) {
  513. answer->copyRelayInfo(question);
  514. }
  515. }
  516. void
  517. Dhcpv6Srv::appendDefaultOptions(const Pkt6Ptr&, Pkt6Ptr& answer) {
  518. // add server-id
  519. answer->addOption(getServerID());
  520. }
  521. void
  522. Dhcpv6Srv::appendRequestedOptions(const Pkt6Ptr& question, Pkt6Ptr& answer) {
  523. // Get the configured subnet suitable for the incoming packet.
  524. Subnet6Ptr subnet = selectSubnet(question);
  525. // Leave if there is no subnet matching the incoming packet.
  526. // There is no need to log the error message here because
  527. // it will be logged in the assignLease() when it fails to
  528. // pick the suitable subnet. We don't want to duplicate
  529. // error messages in such case.
  530. if (!subnet) {
  531. return;
  532. }
  533. // Client requests some options using ORO option. Try to
  534. // get this option from client's message.
  535. boost::shared_ptr<OptionIntArray<uint16_t> > option_oro =
  536. boost::dynamic_pointer_cast<OptionIntArray<uint16_t> >(question->getOption(D6O_ORO));
  537. // Option ORO not found. Don't do anything then.
  538. if (!option_oro) {
  539. return;
  540. }
  541. // Get the list of options that client requested.
  542. const std::vector<uint16_t>& requested_opts = option_oro->getValues();
  543. BOOST_FOREACH(uint16_t opt, requested_opts) {
  544. Subnet::OptionDescriptor desc = subnet->getOptionDescriptor("dhcp6", opt);
  545. if (desc.option) {
  546. answer->addOption(desc.option);
  547. }
  548. }
  549. }
  550. OptionPtr
  551. Dhcpv6Srv::createStatusCode(uint16_t code, const std::string& text) {
  552. // @todo This function uses OptionCustom class to manage contents
  553. // of the data fields. Since this this option is frequently used
  554. // it may be good to implement dedicated class to avoid performance
  555. // impact.
  556. // Get the definition of the option holding status code.
  557. OptionDefinitionPtr status_code_def =
  558. LibDHCP::getOptionDef(Option::V6, D6O_STATUS_CODE);
  559. // This definition is assumed to be initialized in LibDHCP.
  560. assert(status_code_def);
  561. // As there is no dedicated class to represent Status Code
  562. // the OptionCustom class is used here instead.
  563. OptionCustomPtr option_status =
  564. OptionCustomPtr(new OptionCustom(*status_code_def, Option::V6));
  565. assert(option_status);
  566. // Set status code to 'code' (0 - means data field #0).
  567. option_status->writeInteger(code, 0);
  568. // Set a message (1 - means data field #1).
  569. option_status->writeString(text, 1);
  570. return (option_status);
  571. }
  572. void
  573. Dhcpv6Srv::sanityCheck(const Pkt6Ptr& pkt, RequirementLevel clientid,
  574. RequirementLevel serverid) {
  575. Option::OptionCollection client_ids = pkt->getOptions(D6O_CLIENTID);
  576. switch (clientid) {
  577. case MANDATORY:
  578. if (client_ids.size() != 1) {
  579. isc_throw(RFCViolation, "Exactly 1 client-id option expected in "
  580. << pkt->getName() << ", but " << client_ids.size()
  581. << " received");
  582. }
  583. break;
  584. case OPTIONAL:
  585. if (client_ids.size() > 1) {
  586. isc_throw(RFCViolation, "Too many (" << client_ids.size()
  587. << ") client-id options received in " << pkt->getName());
  588. }
  589. break;
  590. case FORBIDDEN:
  591. // doesn't make sense - client-id is always allowed
  592. break;
  593. }
  594. Option::OptionCollection server_ids = pkt->getOptions(D6O_SERVERID);
  595. switch (serverid) {
  596. case FORBIDDEN:
  597. if (!server_ids.empty()) {
  598. isc_throw(RFCViolation, "Server-id option was not expected, but "
  599. << server_ids.size() << " received in " << pkt->getName());
  600. }
  601. break;
  602. case MANDATORY:
  603. if (server_ids.size() != 1) {
  604. isc_throw(RFCViolation, "Invalid number of server-id options received ("
  605. << server_ids.size() << "), exactly 1 expected in message "
  606. << pkt->getName());
  607. }
  608. break;
  609. case OPTIONAL:
  610. if (server_ids.size() > 1) {
  611. isc_throw(RFCViolation, "Too many (" << server_ids.size()
  612. << ") server-id options received in " << pkt->getName());
  613. }
  614. }
  615. }
  616. Subnet6Ptr
  617. Dhcpv6Srv::selectSubnet(const Pkt6Ptr& question) {
  618. Subnet6Ptr subnet;
  619. if (question->relay_info_.empty()) {
  620. // This is a direct (non-relayed) message
  621. // Try to find a subnet if received packet from a directly connected client
  622. subnet = CfgMgr::instance().getSubnet6(question->getIface());
  623. if (!subnet) {
  624. // If no subnet was found, try to find it based on remote address
  625. subnet = CfgMgr::instance().getSubnet6(question->getRemoteAddr());
  626. }
  627. } else {
  628. // This is a relayed message
  629. OptionPtr interface_id = question->getAnyRelayOption(D6O_INTERFACE_ID,
  630. Pkt6::RELAY_GET_FIRST);
  631. if (interface_id) {
  632. subnet = CfgMgr::instance().getSubnet6(interface_id);
  633. }
  634. if (!subnet) {
  635. // If no interface-id was specified (or not configured on server), let's
  636. // try address matching
  637. IOAddress link_addr = question->relay_info_.back().linkaddr_;
  638. // if relay filled in link_addr field, then let's use it
  639. if (link_addr != IOAddress("::")) {
  640. subnet = CfgMgr::instance().getSubnet6(link_addr);
  641. }
  642. }
  643. }
  644. // Let's execute all callouts registered for subnet6_receive
  645. if (HooksManager::getHooksManager().calloutsPresent(Hooks.hook_index_subnet6_select_)) {
  646. CalloutHandlePtr callout_handle = getCalloutHandle(question);
  647. // We're reusing callout_handle from previous calls
  648. callout_handle->deleteAllArguments();
  649. // Set new arguments
  650. callout_handle->setArgument("query6", question);
  651. callout_handle->setArgument("subnet6", subnet);
  652. // We pass pointer to const collection for performance reasons.
  653. // Otherwise we would get a non-trivial performance penalty each
  654. // time subnet6_select is called.
  655. callout_handle->setArgument("subnet6collection", CfgMgr::instance().getSubnets6());
  656. // Call user (and server-side) callouts
  657. HooksManager::callCallouts(Hooks.hook_index_subnet6_select_, *callout_handle);
  658. // Callouts decided to skip this step. This means that no subnet will be
  659. // selected. Packet processing will continue, but it will be severly limited
  660. // (i.e. only global options will be assigned)
  661. if (callout_handle->getSkip()) {
  662. LOG_DEBUG(dhcp6_logger, DBG_DHCP6_HOOKS, DHCP6_HOOK_SUBNET6_SELECT_SKIP);
  663. return (Subnet6Ptr());
  664. }
  665. // Use whatever subnet was specified by the callout
  666. callout_handle->getArgument("subnet6", subnet);
  667. }
  668. return (subnet);
  669. }
  670. void
  671. Dhcpv6Srv::assignLeases(const Pkt6Ptr& question, Pkt6Ptr& answer,
  672. const Option6ClientFqdnPtr& fqdn) {
  673. // We need to allocate addresses for all IA_NA options in the client's
  674. // question (i.e. SOLICIT or REQUEST) message.
  675. // @todo add support for IA_TA
  676. // @todo add support for IA_PD
  677. // We need to select a subnet the client is connected in.
  678. Subnet6Ptr subnet = selectSubnet(question);
  679. if (!subnet) {
  680. // This particular client is out of luck today. We do not have
  681. // information about the subnet he is connected to. This likely means
  682. // misconfiguration of the server (or some relays). We will continue to
  683. // process this message, but our response will be almost useless: no
  684. // addresses or prefixes, no subnet specific configuration etc. The only
  685. // thing this client can get is some global information (like DNS
  686. // servers).
  687. LOG_WARN(dhcp6_logger, DHCP6_SUBNET_SELECTION_FAILED)
  688. .arg(question->getRemoteAddr().toText())
  689. .arg(question->getName());
  690. } else {
  691. LOG_DEBUG(dhcp6_logger, DBG_DHCP6_DETAIL_DATA, DHCP6_SUBNET_SELECTED)
  692. .arg(subnet->toText());
  693. }
  694. // @todo: We should implement Option6Duid some day, but we can do without it
  695. // just fine for now
  696. // Let's find client's DUID. Client is supposed to include its client-id
  697. // option almost all the time (the only exception is an anonymous inf-request,
  698. // but that is mostly a theoretical case). Our allocation engine needs DUID
  699. // and will refuse to allocate anything to anonymous clients.
  700. DuidPtr duid;
  701. OptionPtr opt_duid = question->getOption(D6O_CLIENTID);
  702. if (opt_duid) {
  703. duid = DuidPtr(new DUID(opt_duid->getData()));
  704. } else {
  705. LOG_DEBUG(dhcp6_logger, DBG_DHCP6_BASIC, DHCP6_CLIENTID_MISSING);
  706. // Let's drop the message. This client is not sane.
  707. isc_throw(RFCViolation, "Mandatory client-id is missing in received message");
  708. }
  709. // Now that we have all information about the client, let's iterate over all
  710. // received options and handle IA_NA options one by one and store our
  711. // responses in answer message (ADVERTISE or REPLY).
  712. //
  713. // @todo: expand this to cover IA_PD and IA_TA once we implement support for
  714. // prefix delegation and temporary addresses.
  715. for (Option::OptionCollection::iterator opt = question->options_.begin();
  716. opt != question->options_.end(); ++opt) {
  717. switch (opt->second->getType()) {
  718. case D6O_IA_NA: {
  719. OptionPtr answer_opt = assignIA_NA(subnet, duid, question,
  720. boost::dynamic_pointer_cast<
  721. Option6IA>(opt->second),
  722. fqdn);
  723. if (answer_opt) {
  724. answer->addOption(answer_opt);
  725. }
  726. break;
  727. }
  728. default:
  729. break;
  730. }
  731. }
  732. }
  733. Option6ClientFqdnPtr
  734. Dhcpv6Srv::processClientFqdn(const Pkt6Ptr& question) {
  735. // Get Client FQDN Option from the client's message. If this option hasn't
  736. // been included, do nothing.
  737. Option6ClientFqdnPtr fqdn = boost::dynamic_pointer_cast<
  738. Option6ClientFqdn>(question->getOption(D6O_CLIENT_FQDN));
  739. if (!fqdn) {
  740. return (fqdn);
  741. }
  742. LOG_DEBUG(dhcp6_logger, DBG_DHCP6_DETAIL,
  743. DHCP6_DDNS_RECEIVE_FQDN).arg(fqdn->toText());
  744. // Prepare the FQDN option which will be included in the response to
  745. // the client.
  746. Option6ClientFqdnPtr fqdn_resp(new Option6ClientFqdn(*fqdn));
  747. // RFC 4704, section 6. - all flags set to 0.
  748. fqdn_resp->resetFlags();
  749. // Conditions when N flag has to be set to indicate that server will not
  750. // perform DNS updates:
  751. // 1. Updates are globally disabled,
  752. // 2. Client requested no update and server respects it,
  753. // 3. Client requested that the AAAA update is delegated to the client but
  754. // server neither respects delegation of updates nor it is configured
  755. // to send update on its own when client requested delegation.
  756. if (!FQDN_ENABLE_UPDATE ||
  757. (fqdn->getFlag(Option6ClientFqdn::FLAG_N) &&
  758. !FQDN_OVERRIDE_NO_UPDATE) ||
  759. (!fqdn->getFlag(Option6ClientFqdn::FLAG_S) &&
  760. !FQDN_ALLOW_CLIENT_UPDATE && !FQDN_OVERRIDE_CLIENT_UPDATE)) {
  761. fqdn_resp->setFlag(Option6ClientFqdn::FLAG_N, true);
  762. // Conditions when S flag is set to indicate that server will perform
  763. // DNS update on its own:
  764. // 1. Client requested that server performs DNS update and DNS updates are
  765. // globally enabled
  766. // 2. Client requested that server delegates AAAA update to the client but
  767. // server doesn't respect delegation and it is configured to perform
  768. // an update on its own when client requested delegation.
  769. } else if (fqdn->getFlag(Option6ClientFqdn::FLAG_S) ||
  770. (!fqdn->getFlag(Option6ClientFqdn::FLAG_S) &&
  771. !FQDN_ALLOW_CLIENT_UPDATE && FQDN_OVERRIDE_CLIENT_UPDATE)) {
  772. fqdn_resp->setFlag(Option6ClientFqdn::FLAG_S, true);
  773. }
  774. // Server MUST set the O flag if it has overridden the client's setting
  775. // of S flag.
  776. if (fqdn->getFlag(Option6ClientFqdn::FLAG_S) !=
  777. fqdn_resp->getFlag(Option6ClientFqdn::FLAG_S)) {
  778. fqdn_resp->setFlag(Option6ClientFqdn::FLAG_O, true);
  779. }
  780. // If client supplied partial or empty domain-name, server should
  781. // generate one.
  782. if (fqdn->getDomainNameType() == Option6ClientFqdn::PARTIAL) {
  783. std::ostringstream name;
  784. if (fqdn->getDomainName().empty()) {
  785. name << FQDN_GENERATED_PARTIAL_NAME;
  786. } else {
  787. name << fqdn->getDomainName();
  788. }
  789. name << "." << FQDN_PARTIAL_SUFFIX;
  790. fqdn_resp->setDomainName(name.str(), Option6ClientFqdn::FULL);
  791. // Server may be configured to replace a name supplied by a client,
  792. // even if client supplied fully qualified domain-name.
  793. } else if (FQDN_REPLACE_CLIENT_NAME) {
  794. std::ostringstream name;
  795. name << FQDN_GENERATED_PARTIAL_NAME << "." << FQDN_PARTIAL_SUFFIX;
  796. fqdn_resp->setDomainName(name.str(), Option6ClientFqdn::FULL);
  797. }
  798. // Return the FQDN option which can be included in the server's response.
  799. // Note that it doesn't have to be included, if client didn't request
  800. // it using ORO and server is not configured to always include it.
  801. return (fqdn_resp);
  802. }
  803. void
  804. Dhcpv6Srv::appendClientFqdn(const Pkt6Ptr& question,
  805. Pkt6Ptr& answer,
  806. const Option6ClientFqdnPtr& fqdn) {
  807. // If FQDN is NULL, it means that client did not request DNS Update, plus
  808. // server doesn't force updates.
  809. if (fqdn) {
  810. return;
  811. }
  812. // Server sends back the FQDN option to the client if client has requested
  813. // it using Option Request Option. However, server may be configured to
  814. // send the FQDN option in its response, regardless whether client requested
  815. // it or not.
  816. bool include_fqdn = FQDN_ALWAYS_INCLUDE;
  817. if (!include_fqdn) {
  818. OptionUint16ArrayPtr oro = boost::dynamic_pointer_cast<
  819. OptionUint16Array>(question->getOption(D6O_ORO));
  820. if (oro) {
  821. const std::vector<uint16_t>& values = oro->getValues();
  822. for (int i = 0; i < values.size(); ++i) {
  823. if (values[i] == D6O_CLIENT_FQDN) {
  824. include_fqdn = true;
  825. }
  826. }
  827. }
  828. }
  829. if (include_fqdn) {
  830. LOG_DEBUG(dhcp6_logger, DBG_DHCP6_DETAIL,
  831. DHCP6_DDNS_SEND_FQDN).arg(fqdn->toText());
  832. answer->addOption(fqdn);
  833. }
  834. }
  835. void
  836. Dhcpv6Srv::createNameChangeRequests(const Pkt6Ptr& answer,
  837. const Option6ClientFqdnPtr& opt_fqdn) {
  838. // It is likely that client haven't included the FQDN option in the message
  839. // and server is not configured to always update DNS. In such cases,
  840. // FQDN option will be NULL. This is valid state, so we simply return.
  841. if (!opt_fqdn) {
  842. return;
  843. }
  844. // The response message instance is always required. For instance it
  845. // holds the Client Identifier. It is a programming error if supplied
  846. // message is NULL.
  847. if (!answer) {
  848. isc_throw(isc::Unexpected, "an instance of the object"
  849. << " encapsulating server's message must not be"
  850. << " NULL when creating DNS NameChangeRequest");
  851. }
  852. // Get the Client Id. It is mandatory and a function creating a response
  853. // would have thrown an exception if it was missing. Thus throwning
  854. // Unexpected if it is missing as it is a programming error.
  855. OptionPtr opt_duid = answer->getOption(D6O_CLIENTID);
  856. if (!opt_duid) {
  857. isc_throw(isc::Unexpected,
  858. "client identifier is required when creating a new"
  859. " DNS NameChangeRequest");
  860. }
  861. DuidPtr duid = DuidPtr(new DUID(opt_duid->getData()));
  862. // Get the FQDN in the on-wire format. It will be needed to compute
  863. // DHCID.
  864. OutputBuffer name_buf(1);
  865. opt_fqdn->packDomainName(name_buf);
  866. const uint8_t* name_data = static_cast<const uint8_t*>(name_buf.getData());
  867. // @todo currently D2Dhcid accepts a vector holding FQDN.
  868. // However, it will be faster if we used a pointer name_data.
  869. std::vector<uint8_t> buf_vec(name_data, name_data + name_buf.getLength());
  870. // Compute DHCID from Client Identifier and FQDN.
  871. isc::dhcp_ddns::D2Dhcid dhcid(*duid, buf_vec);
  872. // Get all IAs from the answer. For each IA, holding an address we will
  873. // create a corresponding NameChangeRequest.
  874. Option::OptionCollection answer_ias = answer->getOptions(D6O_IA_NA);
  875. for (Option::OptionCollection::const_iterator answer_ia =
  876. answer_ias.begin(); answer_ia != answer_ias.end(); ++answer_ia) {
  877. // @todo IA_NA may contain multiple addresses. We should process
  878. // each address individually. Currently we get only one.
  879. Option6IAAddrPtr iaaddr = boost::static_pointer_cast<
  880. Option6IAAddr>(answer_ia->second->getOption(D6O_IAADDR));
  881. // We need an address to create a name-to-address mapping.
  882. // If address is missing for any reason, go to the next IA.
  883. if (!iaaddr) {
  884. continue;
  885. }
  886. // Create new NameChangeRequest. Use the domain name from the FQDN.
  887. // This is an FQDN included in the response to the client, so it
  888. // holds a fully qualified domain-name already (not partial).
  889. // Get the IP address from the lease. Also, use the S flag to determine
  890. // if forward change should be performed. This flag will always be
  891. // set if server has taken responsibility for the forward update.
  892. NameChangeRequest ncr(isc::dhcp_ddns::CHG_ADD,
  893. opt_fqdn->getFlag(Option6ClientFqdn::FLAG_S),
  894. true, opt_fqdn->getDomainName(),
  895. iaaddr->getAddress().toText(),
  896. dhcid, 0, iaaddr->getValid());
  897. // Add the request to the queue. This queue will be read elsewhere in
  898. // the code and all requests from this queue will be sent to the
  899. // D2 module.
  900. name_change_reqs_.push(ncr);
  901. LOG_DEBUG(dhcp6_logger, DBG_DHCP6_DETAIL,
  902. DHCP6_DDNS_CREATE_ADD_NAME_CHANGE_REQUEST).arg(ncr.toText());
  903. }
  904. }
  905. void
  906. Dhcpv6Srv::createRemovalNameChangeRequest(const Lease6Ptr& lease) {
  907. // If we haven't performed a DNS Update when lease was acquired,
  908. // there is nothing to do here.
  909. if (!lease->fqdn_fwd_ && !lease->fqdn_rev_) {
  910. return;
  911. }
  912. // When lease was added into a database the host name should have
  913. // been added. The hostname can be empty if someone messed up in the
  914. // lease data base and removed the hostname.
  915. if (lease->hostname_.empty()) {
  916. LOG_ERROR(dhcp6_logger, DHCP6_DDNS_REMOVE_EMPTY_HOSTNAME)
  917. .arg(lease->addr_.toText());
  918. return;
  919. }
  920. // If hostname is non-empty, try to convert it to wire format so as
  921. // DHCID can be computed from it. This may throw an exception if hostname
  922. // has invalid format. Again, this should be only possible in case of
  923. // manual intervention in the database. Note that the last parameter
  924. // passed to the writeFqdn function forces conversion of the FQDN
  925. // to lower case. This is required by the RFC4701, section 3.5.
  926. // The DHCID computation is further in this function.
  927. std::vector<uint8_t> hostname_wire;
  928. try {
  929. OptionDataTypeUtil::writeFqdn(lease->hostname_, hostname_wire, true);
  930. } catch (const Exception& ex) {
  931. LOG_ERROR(dhcp6_logger, DHCP6_DDNS_REMOVE_INVALID_HOSTNAME)
  932. .arg(lease->hostname_);
  933. return;
  934. }
  935. // DUID must have been checked already by the caller of this function.
  936. // Let's be on the safe side and make sure it is non-NULL and throw
  937. // an exception if it is NULL.
  938. if (!lease->duid_) {
  939. isc_throw(isc::Unexpected, "DUID must be set when creating"
  940. << " NameChangeRequest for DNS records removal for "
  941. << lease->addr_.toText());
  942. }
  943. isc::dhcp_ddns::D2Dhcid dhcid(*lease->duid_, hostname_wire);
  944. // Create a NameChangeRequest to remove the entry.
  945. NameChangeRequest ncr(isc::dhcp_ddns::CHG_REMOVE,
  946. lease->fqdn_fwd_, lease->fqdn_rev_,
  947. lease->hostname_,
  948. lease->addr_.toText(),
  949. dhcid, 0, lease->valid_lft_);
  950. name_change_reqs_.push(ncr);
  951. LOG_DEBUG(dhcp6_logger, DBG_DHCP6_DETAIL,
  952. DHCP6_DDNS_CREATE_REMOVE_NAME_CHANGE_REQUEST).arg(ncr.toText());
  953. }
  954. void
  955. Dhcpv6Srv::sendNameChangeRequests() {
  956. while (!name_change_reqs_.empty()) {
  957. // @todo Once next NameChangeRequest is picked from the queue
  958. // we should send it to the b10-dhcp_ddns module. Currently we
  959. // just drop it.
  960. name_change_reqs_.pop();
  961. }
  962. }
  963. OptionPtr
  964. Dhcpv6Srv::assignIA_NA(const Subnet6Ptr& subnet, const DuidPtr& duid,
  965. const Pkt6Ptr& query, boost::shared_ptr<Option6IA> ia,
  966. const Option6ClientFqdnPtr& fqdn) {
  967. // If there is no subnet selected for handling this IA_NA, the only thing to do left is
  968. // to say that we are sorry, but the user won't get an address. As a convenience, we
  969. // use a different status text to indicate that (compare to the same status code,
  970. // but different wording below)
  971. if (!subnet) {
  972. // Create empty IA_NA option with IAID matching the request.
  973. // Note that we don't use OptionDefinition class to create this option.
  974. // This is because we prefer using a constructor of Option6IA that
  975. // initializes IAID. Otherwise we would have to use setIAID() after
  976. // creation of the option which has some performance implications.
  977. boost::shared_ptr<Option6IA> ia_rsp(new Option6IA(D6O_IA_NA, ia->getIAID()));
  978. // Insert status code NoAddrsAvail.
  979. ia_rsp->addOption(createStatusCode(STATUS_NoAddrsAvail, "Sorry, no subnet available."));
  980. return (ia_rsp);
  981. }
  982. // Check if the client sent us a hint in his IA_NA. Clients may send an
  983. // address in their IA_NA options as a suggestion (e.g. the last address
  984. // they used before).
  985. boost::shared_ptr<Option6IAAddr> hintOpt = boost::dynamic_pointer_cast<Option6IAAddr>
  986. (ia->getOption(D6O_IAADDR));
  987. IOAddress hint("::");
  988. if (hintOpt) {
  989. hint = hintOpt->getAddress();
  990. }
  991. LOG_DEBUG(dhcp6_logger, DBG_DHCP6_DETAIL, DHCP6_PROCESS_IA_NA_REQUEST)
  992. .arg(duid?duid->toText():"(no-duid)").arg(ia->getIAID())
  993. .arg(hintOpt?hint.toText():"(no hint)");
  994. // "Fake" allocation is processing of SOLICIT message. We pretend to do an
  995. // allocation, but we do not put the lease in the database. That is ok,
  996. // because we do not guarantee that the user will get that exact lease. If
  997. // the user selects this server to do actual allocation (i.e. sends REQUEST)
  998. // it should include this hint. That will help us during the actual lease
  999. // allocation.
  1000. bool fake_allocation = false;
  1001. if (query->getType() == DHCPV6_SOLICIT) {
  1002. /// @todo: Check if we support rapid commit
  1003. fake_allocation = true;
  1004. }
  1005. CalloutHandlePtr callout_handle = getCalloutHandle(query);
  1006. // At this point, we have to make make some decisions with respect to the
  1007. // FQDN option that we have generated as a result of receiving client's
  1008. // FQDN. In particular, we have to get to know if the DNS update will be
  1009. // performed or not. It is possible that option is NULL, which is valid
  1010. // condition if client didn't request DNS updates and server didn't force
  1011. // the update.
  1012. bool do_fwd = false;
  1013. bool do_rev = false;
  1014. if (fqdn) {
  1015. // Flag S must not coexist with flag N being set to 1, so if S=1
  1016. // server takes responsibility for both reverse and forward updates.
  1017. // Otherwise, we have to check N.
  1018. if (fqdn->getFlag(Option6ClientFqdn::FLAG_S)) {
  1019. do_fwd = true;
  1020. do_rev = true;
  1021. } else if (!fqdn->getFlag(Option6ClientFqdn::FLAG_N)) {
  1022. do_rev = true;
  1023. }
  1024. }
  1025. // Set hostname only in case any of the updates is being performed.
  1026. std::string hostname;
  1027. if (do_fwd || do_rev) {
  1028. hostname = fqdn->getDomainName();
  1029. }
  1030. // Use allocation engine to pick a lease for this client. Allocation engine
  1031. // will try to honour the hint, but it is just a hint - some other address
  1032. // may be used instead. If fake_allocation is set to false, the lease will
  1033. // be inserted into the LeaseMgr as well.
  1034. Lease6Ptr lease = alloc_engine_->allocateAddress6(subnet, duid,
  1035. ia->getIAID(),
  1036. hint,
  1037. do_fwd, do_rev,
  1038. hostname,
  1039. fake_allocation,
  1040. callout_handle);
  1041. // Create IA_NA that we will put in the response.
  1042. // Do not use OptionDefinition to create option's instance so
  1043. // as we can initialize IAID using a constructor.
  1044. boost::shared_ptr<Option6IA> ia_rsp(new Option6IA(D6O_IA_NA, ia->getIAID()));
  1045. if (lease) {
  1046. // We have a lease! Let's wrap its content into IA_NA option
  1047. // with IAADDR suboption.
  1048. LOG_DEBUG(dhcp6_logger, DBG_DHCP6_DETAIL, fake_allocation?
  1049. DHCP6_LEASE_ADVERT:DHCP6_LEASE_ALLOC)
  1050. .arg(lease->addr_.toText())
  1051. .arg(duid?duid->toText():"(no-duid)")
  1052. .arg(ia->getIAID());
  1053. ia_rsp->setT1(subnet->getT1());
  1054. ia_rsp->setT2(subnet->getT2());
  1055. boost::shared_ptr<Option6IAAddr>
  1056. addr(new Option6IAAddr(D6O_IAADDR,
  1057. lease->addr_,
  1058. lease->preferred_lft_,
  1059. lease->valid_lft_));
  1060. ia_rsp->addOption(addr);
  1061. // It would be possible to insert status code=0(success) as well,
  1062. // but this is considered waste of bandwidth as absence of status
  1063. // code is considered a success.
  1064. // Allocation engine may have returned an existing lease. If so, we
  1065. // have to check that the FQDN settings we provided are the same
  1066. // that were set. If they aren't, we will have to remove existing
  1067. // DNS records and update the lease with the new settings.
  1068. if ((lease->hostname_ != hostname) || (lease->fqdn_fwd_ != do_fwd) ||
  1069. (lease->fqdn_rev_ != do_rev)) {
  1070. LOG_DEBUG(dhcp6_logger, DBG_DHCP6_DETAIL,
  1071. DHCP6_DDNS_LEASE_ASSIGN_FQDN_CHANGE)
  1072. .arg(lease->toText())
  1073. .arg(hostname)
  1074. .arg(do_rev ? "true" : "false")
  1075. .arg(do_fwd ? "true" : "false");
  1076. // Schedule removal of the existing lease.
  1077. createRemovalNameChangeRequest(lease);
  1078. // Set the new lease properties and update.
  1079. lease->hostname_ = hostname;
  1080. lease->fqdn_fwd_ = do_fwd;
  1081. lease->fqdn_rev_ = do_rev;
  1082. LeaseMgrFactory::instance().updateLease6(lease);
  1083. }
  1084. } else {
  1085. // Allocation engine did not allocate a lease. The engine logged
  1086. // cause of that failure. The only thing left is to insert
  1087. // status code to pass the sad news to the client.
  1088. LOG_DEBUG(dhcp6_logger, DBG_DHCP6_DETAIL, fake_allocation ?
  1089. DHCP6_LEASE_ADVERT_FAIL : DHCP6_LEASE_ALLOC_FAIL)
  1090. .arg(duid?duid->toText():"(no-duid)")
  1091. .arg(ia->getIAID());
  1092. ia_rsp->addOption(createStatusCode(STATUS_NoAddrsAvail,
  1093. "Sorry, no address could be allocated."));
  1094. }
  1095. return (ia_rsp);
  1096. }
  1097. OptionPtr
  1098. Dhcpv6Srv::renewIA_NA(const Subnet6Ptr& subnet, const DuidPtr& duid,
  1099. const Pkt6Ptr& query, boost::shared_ptr<Option6IA> ia,
  1100. const Option6ClientFqdnPtr& fqdn) {
  1101. if (!subnet) {
  1102. // There's no subnet select for this client. There's nothing to renew.
  1103. boost::shared_ptr<Option6IA> ia_rsp(new Option6IA(D6O_IA_NA, ia->getIAID()));
  1104. // Insert status code NoAddrsAvail.
  1105. ia_rsp->addOption(createStatusCode(STATUS_NoBinding,
  1106. "Sorry, no known leases for this duid/iaid."));
  1107. LOG_DEBUG(dhcp6_logger, DBG_DHCP6_DETAIL, DHCP6_RENEW_UNKNOWN_SUBNET)
  1108. .arg(duid->toText())
  1109. .arg(ia->getIAID());
  1110. return (ia_rsp);
  1111. }
  1112. Lease6Ptr lease = LeaseMgrFactory::instance().getLease6(*duid, ia->getIAID(),
  1113. subnet->getID());
  1114. if (!lease) {
  1115. // client renewing a lease that we don't know about.
  1116. // Create empty IA_NA option with IAID matching the request.
  1117. boost::shared_ptr<Option6IA> ia_rsp(new Option6IA(D6O_IA_NA, ia->getIAID()));
  1118. // Insert status code NoAddrsAvail.
  1119. ia_rsp->addOption(createStatusCode(STATUS_NoBinding,
  1120. "Sorry, no known leases for this duid/iaid."));
  1121. LOG_DEBUG(dhcp6_logger, DBG_DHCP6_DETAIL, DHCP6_UNKNOWN_RENEW)
  1122. .arg(duid->toText())
  1123. .arg(ia->getIAID())
  1124. .arg(subnet->toText());
  1125. return (ia_rsp);
  1126. }
  1127. // Keep the old data in case the callout tells us to skip update
  1128. Lease6 old_data = *lease;
  1129. // At this point, we have to make make some decisions with respect to the
  1130. // FQDN option that we have generated as a result of receiving client's
  1131. // FQDN. In particular, we have to get to know if the DNS update will be
  1132. // performed or not. It is possible that option is NULL, which is valid
  1133. // condition if client didn't request DNS updates and server didn't force
  1134. // the update.
  1135. bool do_fwd = false;
  1136. bool do_rev = false;
  1137. if (fqdn) {
  1138. if (fqdn->getFlag(Option6ClientFqdn::FLAG_S)) {
  1139. do_fwd = true;
  1140. do_rev = true;
  1141. } else if (!fqdn->getFlag(Option6ClientFqdn::FLAG_N)) {
  1142. do_rev = true;
  1143. }
  1144. }
  1145. std::string hostname;
  1146. if (do_fwd || do_rev) {
  1147. hostname = fqdn->getDomainName();
  1148. }
  1149. // If the new FQDN settings have changed for the lease, we need to
  1150. // delete any existing FQDN records for this lease.
  1151. if ((lease->hostname_ != hostname) || (lease->fqdn_fwd_ != do_fwd) ||
  1152. (lease->fqdn_rev_ != do_rev)) {
  1153. LOG_DEBUG(dhcp6_logger, DBG_DHCP6_DETAIL,
  1154. DHCP6_DDNS_LEASE_RENEW_FQDN_CHANGE)
  1155. .arg(lease->toText())
  1156. .arg(hostname)
  1157. .arg(do_rev ? "true" : "false")
  1158. .arg(do_fwd ? "true" : "false");
  1159. createRemovalNameChangeRequest(lease);
  1160. }
  1161. lease->preferred_lft_ = subnet->getPreferred();
  1162. lease->valid_lft_ = subnet->getValid();
  1163. lease->t1_ = subnet->getT1();
  1164. lease->t2_ = subnet->getT2();
  1165. lease->cltt_ = time(NULL);
  1166. lease->hostname_ = hostname;
  1167. lease->fqdn_fwd_ = do_fwd;
  1168. lease->fqdn_rev_ = do_rev;
  1169. // Create empty IA_NA option with IAID matching the request.
  1170. boost::shared_ptr<Option6IA> ia_rsp(new Option6IA(D6O_IA_NA, ia->getIAID()));
  1171. ia_rsp->setT1(subnet->getT1());
  1172. ia_rsp->setT2(subnet->getT2());
  1173. boost::shared_ptr<Option6IAAddr> addr(new Option6IAAddr(D6O_IAADDR,
  1174. lease->addr_, lease->preferred_lft_,
  1175. lease->valid_lft_));
  1176. ia_rsp->addOption(addr);
  1177. bool skip = false;
  1178. // Execute all callouts registered for packet6_send
  1179. if (HooksManager::getHooksManager().calloutsPresent(Hooks.hook_index_lease6_renew_)) {
  1180. CalloutHandlePtr callout_handle = getCalloutHandle(query);
  1181. // Delete all previous arguments
  1182. callout_handle->deleteAllArguments();
  1183. // Pass the original packet
  1184. callout_handle->setArgument("query6", query);
  1185. // Pass the lease to be updated
  1186. callout_handle->setArgument("lease6", lease);
  1187. // Pass the IA option to be sent in response
  1188. callout_handle->setArgument("ia_na", ia_rsp);
  1189. // Call all installed callouts
  1190. HooksManager::callCallouts(Hooks.hook_index_lease6_renew_, *callout_handle);
  1191. // Callouts decided to skip the next processing step. The next
  1192. // processing step would to actually renew the lease, so skip at this
  1193. // stage means "keep the old lease as it is".
  1194. if (callout_handle->getSkip()) {
  1195. skip = true;
  1196. LOG_DEBUG(dhcp6_logger, DBG_DHCP6_HOOKS, DHCP6_HOOK_LEASE6_RENEW_SKIP);
  1197. }
  1198. }
  1199. if (!skip) {
  1200. LeaseMgrFactory::instance().updateLease6(lease);
  1201. } else {
  1202. // Copy back the original date to the lease. For MySQL it doesn't make
  1203. // much sense, but for memfile, the Lease6Ptr points to the actual lease
  1204. // in memfile, so the actual update is performed when we manipulate fields
  1205. // of returned Lease6Ptr, the actual updateLease6() is no-op.
  1206. *lease = old_data;
  1207. }
  1208. return (ia_rsp);
  1209. }
  1210. void
  1211. Dhcpv6Srv::renewLeases(const Pkt6Ptr& renew, Pkt6Ptr& reply,
  1212. const Option6ClientFqdnPtr& fqdn) {
  1213. // We need to renew addresses for all IA_NA options in the client's
  1214. // RENEW message.
  1215. // @todo add support for IA_TA
  1216. // @todo add support for IA_PD
  1217. // We need to select a subnet the client is connected in.
  1218. Subnet6Ptr subnet = selectSubnet(renew);
  1219. if (!subnet) {
  1220. // This particular client is out of luck today. We do not have
  1221. // information about the subnet he is connected to. This likely means
  1222. // misconfiguration of the server (or some relays). We will continue to
  1223. // process this message, but our response will be almost useless: no
  1224. // addresses or prefixes, no subnet specific configuration etc. The only
  1225. // thing this client can get is some global information (like DNS
  1226. // servers).
  1227. LOG_WARN(dhcp6_logger, DHCP6_SUBNET_SELECTION_FAILED)
  1228. .arg(renew->getRemoteAddr().toText())
  1229. .arg(renew->getName());
  1230. } else {
  1231. LOG_DEBUG(dhcp6_logger, DBG_DHCP6_DETAIL_DATA, DHCP6_SUBNET_SELECTED)
  1232. .arg(subnet->toText());
  1233. }
  1234. // Let's find client's DUID. Client is supposed to include its client-id
  1235. // option almost all the time (the only exception is an anonymous inf-request,
  1236. // but that is mostly a theoretical case). Our allocation engine needs DUID
  1237. // and will refuse to allocate anything to anonymous clients.
  1238. OptionPtr opt_duid = renew->getOption(D6O_CLIENTID);
  1239. if (!opt_duid) {
  1240. // This should not happen. We have checked this before.
  1241. reply->addOption(createStatusCode(STATUS_UnspecFail,
  1242. "You did not include mandatory client-id"));
  1243. return;
  1244. }
  1245. DuidPtr duid(new DUID(opt_duid->getData()));
  1246. for (Option::OptionCollection::iterator opt = renew->options_.begin();
  1247. opt != renew->options_.end(); ++opt) {
  1248. switch (opt->second->getType()) {
  1249. case D6O_IA_NA: {
  1250. OptionPtr answer_opt = renewIA_NA(subnet, duid, renew,
  1251. boost::dynamic_pointer_cast<
  1252. Option6IA>(opt->second),
  1253. fqdn);
  1254. if (answer_opt) {
  1255. reply->addOption(answer_opt);
  1256. }
  1257. break;
  1258. }
  1259. default:
  1260. break;
  1261. }
  1262. }
  1263. }
  1264. void
  1265. Dhcpv6Srv::releaseLeases(const Pkt6Ptr& release, Pkt6Ptr& reply) {
  1266. // We need to release addresses for all IA_NA options in the client's
  1267. // RELEASE message.
  1268. // @todo Add support for IA_TA
  1269. // @todo Add support for IA_PD
  1270. // @todo Consider supporting more than one address in a single IA_NA.
  1271. // That was envisaged by RFC3315, but it never happened. The only
  1272. // software that supports that is Dibbler, but its author seriously doubts
  1273. // if anyone is really using it. Clients that want more than one address
  1274. // just include more instances of IA_NA options.
  1275. // Let's find client's DUID. Client is supposed to include its client-id
  1276. // option almost all the time (the only exception is an anonymous inf-request,
  1277. // but that is mostly a theoretical case). Our allocation engine needs DUID
  1278. // and will refuse to allocate anything to anonymous clients.
  1279. OptionPtr opt_duid = release->getOption(D6O_CLIENTID);
  1280. if (!opt_duid) {
  1281. // This should not happen. We have checked this before.
  1282. // see sanityCheck() called from processRelease()
  1283. LOG_WARN(dhcp6_logger, DHCP6_RELEASE_MISSING_CLIENTID)
  1284. .arg(release->getRemoteAddr().toText());
  1285. reply->addOption(createStatusCode(STATUS_UnspecFail,
  1286. "You did not include mandatory client-id"));
  1287. return;
  1288. }
  1289. DuidPtr duid(new DUID(opt_duid->getData()));
  1290. int general_status = STATUS_Success;
  1291. for (Option::OptionCollection::iterator opt = release->options_.begin();
  1292. opt != release->options_.end(); ++opt) {
  1293. switch (opt->second->getType()) {
  1294. case D6O_IA_NA: {
  1295. OptionPtr answer_opt = releaseIA_NA(duid, release, general_status,
  1296. boost::dynamic_pointer_cast<Option6IA>(opt->second));
  1297. if (answer_opt) {
  1298. reply->addOption(answer_opt);
  1299. }
  1300. break;
  1301. }
  1302. // @todo: add support for IA_PD
  1303. // @todo: add support for IA_TA
  1304. default:
  1305. // remaining options are stateless and thus ignored in this context
  1306. ;
  1307. }
  1308. }
  1309. // To be pedantic, we should also include status code in the top-level
  1310. // scope, not just in each IA_NA. See RFC3315, section 18.2.6.
  1311. // This behavior will likely go away in RFC3315bis.
  1312. reply->addOption(createStatusCode(general_status,
  1313. "Summary status for all processed IA_NAs"));
  1314. }
  1315. OptionPtr
  1316. Dhcpv6Srv::releaseIA_NA(const DuidPtr& duid, const Pkt6Ptr& query,
  1317. int& general_status, boost::shared_ptr<Option6IA> ia) {
  1318. // Release can be done in one of two ways:
  1319. // Approach 1: extract address from client's IA_NA and see if it belongs
  1320. // to this particular client.
  1321. // Approach 2: find a subnet for this client, get a lease for
  1322. // this subnet/duid/iaid and check if its content matches to what the
  1323. // client is asking us to release.
  1324. //
  1325. // This method implements approach 1.
  1326. // That's our response
  1327. boost::shared_ptr<Option6IA> ia_rsp(new Option6IA(D6O_IA_NA, ia->getIAID()));
  1328. boost::shared_ptr<Option6IAAddr> release_addr = boost::dynamic_pointer_cast<Option6IAAddr>
  1329. (ia->getOption(D6O_IAADDR));
  1330. if (!release_addr) {
  1331. ia_rsp->addOption(createStatusCode(STATUS_NoBinding,
  1332. "You did not include address in your RELEASE"));
  1333. general_status = STATUS_NoBinding;
  1334. return (ia_rsp);
  1335. }
  1336. Lease6Ptr lease = LeaseMgrFactory::instance().getLease6(release_addr->getAddress());
  1337. if (!lease) {
  1338. // client releasing a lease that we don't know about.
  1339. // Insert status code NoAddrsAvail.
  1340. ia_rsp->addOption(createStatusCode(STATUS_NoBinding,
  1341. "Sorry, no known leases for this duid/iaid, can't release."));
  1342. general_status = STATUS_NoBinding;
  1343. LOG_INFO(dhcp6_logger, DHCP6_UNKNOWN_RELEASE)
  1344. .arg(duid->toText())
  1345. .arg(ia->getIAID());
  1346. return (ia_rsp);
  1347. }
  1348. if (!lease->duid_) {
  1349. // Something is gravely wrong here. We do have a lease, but it does not
  1350. // have mandatory DUID information attached. Someone was messing with our
  1351. // database.
  1352. LOG_ERROR(dhcp6_logger, DHCP6_LEASE_WITHOUT_DUID)
  1353. .arg(release_addr->getAddress().toText());
  1354. general_status = STATUS_UnspecFail;
  1355. ia_rsp->addOption(createStatusCode(STATUS_UnspecFail,
  1356. "Database consistency check failed when trying to RELEASE"));
  1357. return (ia_rsp);
  1358. }
  1359. if (*duid != *(lease->duid_)) {
  1360. // Sorry, it's not your address. You can't release it.
  1361. LOG_INFO(dhcp6_logger, DHCP6_RELEASE_FAIL_WRONG_DUID)
  1362. .arg(duid->toText())
  1363. .arg(release_addr->getAddress().toText())
  1364. .arg(lease->duid_->toText());
  1365. general_status = STATUS_NoBinding;
  1366. ia_rsp->addOption(createStatusCode(STATUS_NoBinding,
  1367. "This address does not belong to you, you can't release it"));
  1368. return (ia_rsp);
  1369. }
  1370. if (ia->getIAID() != lease->iaid_) {
  1371. // This address belongs to this client, but to a different IA
  1372. LOG_WARN(dhcp6_logger, DHCP6_RELEASE_FAIL_WRONG_IAID)
  1373. .arg(duid->toText())
  1374. .arg(release_addr->getAddress().toText())
  1375. .arg(lease->iaid_)
  1376. .arg(ia->getIAID());
  1377. ia_rsp->addOption(createStatusCode(STATUS_NoBinding,
  1378. "This is your address, but you used wrong IAID"));
  1379. general_status = STATUS_NoBinding;
  1380. return (ia_rsp);
  1381. }
  1382. // It is not necessary to check if the address matches as we used
  1383. // getLease6(addr) method that is supposed to return a proper lease.
  1384. bool skip = false;
  1385. // Execute all callouts registered for packet6_send
  1386. if (HooksManager::getHooksManager().calloutsPresent(Hooks.hook_index_lease6_release_)) {
  1387. CalloutHandlePtr callout_handle = getCalloutHandle(query);
  1388. // Delete all previous arguments
  1389. callout_handle->deleteAllArguments();
  1390. // Pass the original packet
  1391. callout_handle->setArgument("query6", query);
  1392. // Pass the lease to be updated
  1393. callout_handle->setArgument("lease6", lease);
  1394. // Call all installed callouts
  1395. HooksManager::callCallouts(Hooks.hook_index_lease6_release_, *callout_handle);
  1396. // Callouts decided to skip the next processing step. The next
  1397. // processing step would to send the packet, so skip at this
  1398. // stage means "drop response".
  1399. if (callout_handle->getSkip()) {
  1400. skip = true;
  1401. LOG_DEBUG(dhcp6_logger, DBG_DHCP6_HOOKS, DHCP6_HOOK_LEASE6_RELEASE_SKIP);
  1402. }
  1403. }
  1404. // Ok, we've passed all checks. Let's release this address.
  1405. bool success = false; // was the removal operation succeessful?
  1406. if (!skip) {
  1407. success = LeaseMgrFactory::instance().deleteLease(lease->addr_);
  1408. }
  1409. // Here the success should be true if we removed lease successfully
  1410. // and false if skip flag was set or the removal failed for whatever reason
  1411. if (!success) {
  1412. ia_rsp->addOption(createStatusCode(STATUS_UnspecFail,
  1413. "Server failed to release a lease"));
  1414. LOG_ERROR(dhcp6_logger, DHCP6_RELEASE_FAIL)
  1415. .arg(lease->addr_.toText())
  1416. .arg(duid->toText())
  1417. .arg(lease->iaid_);
  1418. general_status = STATUS_UnspecFail;
  1419. return (ia_rsp);
  1420. } else {
  1421. LOG_DEBUG(dhcp6_logger, DBG_DHCP6_DETAIL, DHCP6_RELEASE)
  1422. .arg(lease->addr_.toText())
  1423. .arg(duid->toText())
  1424. .arg(lease->iaid_);
  1425. ia_rsp->addOption(createStatusCode(STATUS_Success,
  1426. "Lease released. Thank you, please come again."));
  1427. // Check if a lease has flags indicating that the FQDN update has
  1428. // been performed. If so, create NameChangeRequest which removes
  1429. // the entries.
  1430. createRemovalNameChangeRequest(lease);
  1431. return (ia_rsp);
  1432. }
  1433. }
  1434. Pkt6Ptr
  1435. Dhcpv6Srv::processSolicit(const Pkt6Ptr& solicit) {
  1436. sanityCheck(solicit, MANDATORY, FORBIDDEN);
  1437. Pkt6Ptr advertise(new Pkt6(DHCPV6_ADVERTISE, solicit->getTransid()));
  1438. copyDefaultOptions(solicit, advertise);
  1439. appendDefaultOptions(solicit, advertise);
  1440. appendRequestedOptions(solicit, advertise);
  1441. Option6ClientFqdnPtr fqdn = processClientFqdn(solicit);
  1442. assignLeases(solicit, advertise, fqdn);
  1443. appendClientFqdn(solicit, advertise, fqdn);
  1444. // Note, that we don't create NameChangeRequests here because we don't
  1445. // perform DNS Updates for Solicit. Client must send Request to update
  1446. // DNS.
  1447. return (advertise);
  1448. }
  1449. Pkt6Ptr
  1450. Dhcpv6Srv::processRequest(const Pkt6Ptr& request) {
  1451. sanityCheck(request, MANDATORY, MANDATORY);
  1452. Pkt6Ptr reply(new Pkt6(DHCPV6_REPLY, request->getTransid()));
  1453. copyDefaultOptions(request, reply);
  1454. appendDefaultOptions(request, reply);
  1455. appendRequestedOptions(request, reply);
  1456. Option6ClientFqdnPtr fqdn = processClientFqdn(request);
  1457. assignLeases(request, reply, fqdn);
  1458. appendClientFqdn(request, reply, fqdn);
  1459. createNameChangeRequests(reply, fqdn);
  1460. return (reply);
  1461. }
  1462. Pkt6Ptr
  1463. Dhcpv6Srv::processRenew(const Pkt6Ptr& renew) {
  1464. sanityCheck(renew, MANDATORY, MANDATORY);
  1465. Pkt6Ptr reply(new Pkt6(DHCPV6_REPLY, renew->getTransid()));
  1466. copyDefaultOptions(renew, reply);
  1467. appendDefaultOptions(renew, reply);
  1468. appendRequestedOptions(renew, reply);
  1469. Option6ClientFqdnPtr fqdn = processClientFqdn(renew);
  1470. renewLeases(renew, reply, fqdn);
  1471. appendClientFqdn(renew, reply, fqdn);
  1472. createNameChangeRequests(reply, fqdn);
  1473. return reply;
  1474. }
  1475. Pkt6Ptr
  1476. Dhcpv6Srv::processRebind(const Pkt6Ptr& rebind) {
  1477. /// @todo: Implement this
  1478. Pkt6Ptr reply(new Pkt6(DHCPV6_REPLY, rebind->getTransid()));
  1479. return reply;
  1480. }
  1481. Pkt6Ptr
  1482. Dhcpv6Srv::processConfirm(const Pkt6Ptr& confirm) {
  1483. /// @todo: Implement this
  1484. Pkt6Ptr reply(new Pkt6(DHCPV6_REPLY, confirm->getTransid()));
  1485. return reply;
  1486. }
  1487. Pkt6Ptr
  1488. Dhcpv6Srv::processRelease(const Pkt6Ptr& release) {
  1489. sanityCheck(release, MANDATORY, MANDATORY);
  1490. Pkt6Ptr reply(new Pkt6(DHCPV6_REPLY, release->getTransid()));
  1491. copyDefaultOptions(release, reply);
  1492. appendDefaultOptions(release, reply);
  1493. releaseLeases(release, reply);
  1494. // @todo If client sent a release and we should remove outstanding
  1495. // DNS records.
  1496. return (reply);
  1497. }
  1498. Pkt6Ptr
  1499. Dhcpv6Srv::processDecline(const Pkt6Ptr& decline) {
  1500. /// @todo: Implement this
  1501. Pkt6Ptr reply(new Pkt6(DHCPV6_REPLY, decline->getTransid()));
  1502. return reply;
  1503. }
  1504. Pkt6Ptr
  1505. Dhcpv6Srv::processInfRequest(const Pkt6Ptr& infRequest) {
  1506. /// @todo: Implement this
  1507. Pkt6Ptr reply(new Pkt6(DHCPV6_REPLY, infRequest->getTransid()));
  1508. return reply;
  1509. }
  1510. void
  1511. Dhcpv6Srv::openActiveSockets(const uint16_t port) {
  1512. IfaceMgr::instance().closeSockets();
  1513. // Get the reference to the collection of interfaces. This reference should be
  1514. // valid as long as the program is run because IfaceMgr is a singleton.
  1515. // Therefore we can safely iterate over instances of all interfaces and modify
  1516. // their flags. Here we modify flags which indicate wheter socket should be
  1517. // open for a particular interface or not.
  1518. const IfaceMgr::IfaceCollection& ifaces = IfaceMgr::instance().getIfaces();
  1519. for (IfaceMgr::IfaceCollection::const_iterator iface = ifaces.begin();
  1520. iface != ifaces.end(); ++iface) {
  1521. Iface* iface_ptr = IfaceMgr::instance().getIface(iface->getName());
  1522. if (iface_ptr == NULL) {
  1523. isc_throw(isc::Unexpected, "Interface Manager returned NULL"
  1524. << " instance of the interface when DHCPv6 server was"
  1525. << " trying to reopen sockets after reconfiguration");
  1526. }
  1527. if (CfgMgr::instance().isActiveIface(iface->getName())) {
  1528. iface_ptr->inactive4_ = false;
  1529. LOG_INFO(dhcp6_logger, DHCP6_ACTIVATE_INTERFACE)
  1530. .arg(iface->getFullName());
  1531. } else {
  1532. // For deactivating interface, it should be sufficient to log it
  1533. // on the debug level because it is more useful to know what
  1534. // interface is activated which is logged on the info level.
  1535. LOG_DEBUG(dhcp6_logger, DBG_DHCP6_BASIC,
  1536. DHCP6_DEACTIVATE_INTERFACE).arg(iface->getName());
  1537. iface_ptr->inactive6_ = true;
  1538. }
  1539. }
  1540. // Let's reopen active sockets. openSockets6 will check internally whether
  1541. // sockets are marked active or inactive.
  1542. // @todo Optimization: we should not reopen all sockets but rather select
  1543. // those that have been affected by the new configuration.
  1544. if (!IfaceMgr::instance().openSockets6(port)) {
  1545. LOG_WARN(dhcp6_logger, DHCP6_NO_SOCKETS_OPEN);
  1546. }
  1547. }
  1548. };
  1549. };