dhcp6_srv.cc 107 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726
  1. // Copyright (C) 2011-2015 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/docsis3_option_defs.h>
  19. #include <dhcp/duid.h>
  20. #include <dhcp/iface_mgr.h>
  21. #include <dhcp/libdhcp++.h>
  22. #include <dhcp/option6_addrlst.h>
  23. #include <dhcp/option6_client_fqdn.h>
  24. #include <dhcp/option6_ia.h>
  25. #include <dhcp/option6_iaaddr.h>
  26. #include <dhcp/option6_iaprefix.h>
  27. #include <dhcp/option_custom.h>
  28. #include <dhcp/option_vendor.h>
  29. #include <dhcp/option_vendor_class.h>
  30. #include <dhcp/option_int_array.h>
  31. #include <dhcp/pkt6.h>
  32. #include <dhcp6/dhcp6_log.h>
  33. #include <dhcp6/dhcp6_srv.h>
  34. #include <dhcpsrv/callout_handle_store.h>
  35. #include <dhcpsrv/cfgmgr.h>
  36. #include <dhcpsrv/lease_mgr.h>
  37. #include <dhcpsrv/lease_mgr_factory.h>
  38. #include <dhcpsrv/subnet.h>
  39. #include <dhcpsrv/subnet_selector.h>
  40. #include <dhcpsrv/utils.h>
  41. #include <exceptions/exceptions.h>
  42. #include <hooks/callout_handle.h>
  43. #include <hooks/hooks_manager.h>
  44. #include <util/encode/hex.h>
  45. #include <util/io_utilities.h>
  46. #include <util/range_utilities.h>
  47. #include <asio.hpp>
  48. #include <boost/bind.hpp>
  49. #include <boost/foreach.hpp>
  50. #include <boost/tokenizer.hpp>
  51. #include <boost/algorithm/string/erase.hpp>
  52. #include <stdlib.h>
  53. #include <time.h>
  54. #include <iomanip>
  55. #include <fstream>
  56. #include <sstream>
  57. using namespace isc;
  58. using namespace isc::asiolink;
  59. using namespace isc::dhcp_ddns;
  60. using namespace isc::dhcp;
  61. using namespace isc::hooks;
  62. using namespace isc::util;
  63. using namespace std;
  64. namespace {
  65. /// Structure that holds registered hook indexes
  66. struct Dhcp6Hooks {
  67. int hook_index_buffer6_receive_;///< index for "buffer6_receive" hook point
  68. int hook_index_pkt6_receive_; ///< index for "pkt6_receive" hook point
  69. int hook_index_subnet6_select_; ///< index for "subnet6_select" hook point
  70. int hook_index_lease6_release_; ///< index for "lease6_release" hook point
  71. int hook_index_pkt6_send_; ///< index for "pkt6_send" hook point
  72. int hook_index_buffer6_send_; ///< index for "buffer6_send" hook point
  73. /// Constructor that registers hook points for DHCPv6 engine
  74. Dhcp6Hooks() {
  75. hook_index_buffer6_receive_= HooksManager::registerHook("buffer6_receive");
  76. hook_index_pkt6_receive_ = HooksManager::registerHook("pkt6_receive");
  77. hook_index_subnet6_select_ = HooksManager::registerHook("subnet6_select");
  78. hook_index_lease6_release_ = HooksManager::registerHook("lease6_release");
  79. hook_index_pkt6_send_ = HooksManager::registerHook("pkt6_send");
  80. hook_index_buffer6_send_ = HooksManager::registerHook("buffer6_send");
  81. }
  82. };
  83. // Declare a Hooks object. As this is outside any function or method, it
  84. // will be instantiated (and the constructor run) when the module is loaded.
  85. // As a result, the hook indexes will be defined before any method in this
  86. // module is called.
  87. Dhcp6Hooks Hooks;
  88. }; // anonymous namespace
  89. namespace isc {
  90. namespace dhcp {
  91. const std::string Dhcpv6Srv::VENDOR_CLASS_PREFIX("VENDOR_CLASS_");
  92. /// @brief file name of a server-id file
  93. ///
  94. /// Server must store its duid in persistent storage that must not change
  95. /// between restarts. This is name of the file that is created in dataDir
  96. /// (see isc::dhcp::CfgMgr::getDataDir()). It is a text file that uses
  97. /// double digit hex values separated by colons format, e.g.
  98. /// 01:ff:02:03:06:80:90:ab:cd:ef. Server will create it during first
  99. /// run and then use it afterwards.
  100. static const char* SERVER_DUID_FILE = "kea-dhcp6-serverid";
  101. Dhcpv6Srv::Dhcpv6Srv(uint16_t port)
  102. :alloc_engine_(), serverid_(), port_(port), shutdown_(true)
  103. {
  104. LOG_DEBUG(dhcp6_logger, DBG_DHCP6_START, DHCP6_OPEN_SOCKET).arg(port);
  105. // Initialize objects required for DHCP server operation.
  106. try {
  107. // Port 0 is used for testing purposes where in most cases we don't
  108. // rely on the physical interfaces. Therefore, it should be possible
  109. // to create an object even when there are no usable interfaces.
  110. if ((port > 0) && (IfaceMgr::instance().countIfaces() == 0)) {
  111. LOG_ERROR(dhcp6_logger, DHCP6_NO_INTERFACES);
  112. return;
  113. }
  114. string duid_file = CfgMgr::instance().getDataDir() + "/" + string(SERVER_DUID_FILE);
  115. if (loadServerID(duid_file)) {
  116. LOG_DEBUG(dhcp6_logger, DBG_DHCP6_START, DHCP6_SERVERID_LOADED)
  117. .arg(duidToString(getServerID()))
  118. .arg(duid_file);
  119. } else {
  120. generateServerID();
  121. LOG_INFO(dhcp6_logger, DHCP6_SERVERID_GENERATED)
  122. .arg(duidToString(getServerID()))
  123. .arg(duid_file);
  124. if (!writeServerID(duid_file)) {
  125. LOG_WARN(dhcp6_logger, DHCP6_SERVERID_WRITE_FAIL)
  126. .arg(duid_file);
  127. }
  128. }
  129. // Instantiate allocation engine
  130. alloc_engine_.reset(new AllocEngine(AllocEngine::ALLOC_ITERATIVE, 100));
  131. /// @todo call loadLibraries() when handling configuration changes
  132. } catch (const std::exception &e) {
  133. LOG_ERROR(dhcp6_logger, DHCP6_SRV_CONSTRUCT_ERROR).arg(e.what());
  134. return;
  135. }
  136. // All done, so can proceed
  137. shutdown_ = false;
  138. }
  139. Dhcpv6Srv::~Dhcpv6Srv() {
  140. IfaceMgr::instance().closeSockets();
  141. LeaseMgrFactory::destroy();
  142. }
  143. void Dhcpv6Srv::shutdown() {
  144. LOG_DEBUG(dhcp6_logger, DBG_DHCP6_BASIC, DHCP6_SHUTDOWN_REQUEST);
  145. shutdown_ = true;
  146. }
  147. Pkt6Ptr Dhcpv6Srv::receivePacket(int timeout) {
  148. return (IfaceMgr::instance().receive6(timeout));
  149. }
  150. void Dhcpv6Srv::sendPacket(const Pkt6Ptr& packet) {
  151. IfaceMgr::instance().send(packet);
  152. }
  153. bool
  154. Dhcpv6Srv::testServerID(const Pkt6Ptr& pkt) {
  155. /// @todo Currently we always check server identifier regardless if
  156. /// it is allowed in the received message or not (per RFC3315).
  157. /// If the server identifier is not allowed in the message, the
  158. /// sanityCheck function should deal with it. We may rethink this
  159. /// design if we decide that it is appropriate to check at this stage
  160. /// of message processing that the server identifier must or must not
  161. /// be present. In such case however, the logic checking server id
  162. /// will have to be removed from sanityCheck and placed here instead,
  163. /// to avoid duplicate checks.
  164. OptionPtr server_id = pkt->getOption(D6O_SERVERID);
  165. if (server_id){
  166. // Let us test received ServerID if it is same as ServerID
  167. // which is beeing used by server
  168. if (getServerID()->getData() != server_id->getData()){
  169. LOG_DEBUG(dhcp6_logger, DBG_DHCP6_DETAIL_DATA, DHCP6_PACKET_MISMATCH_SERVERID_DROP)
  170. .arg(pkt->getName())
  171. .arg(pkt->getTransid())
  172. .arg(pkt->getIface());
  173. return (false);
  174. }
  175. }
  176. // retun True if: no serverid received or ServerIDs matching
  177. return (true);
  178. }
  179. bool
  180. Dhcpv6Srv::testUnicast(const Pkt6Ptr& pkt) const {
  181. switch (pkt->getType()) {
  182. case DHCPV6_SOLICIT:
  183. case DHCPV6_CONFIRM:
  184. case DHCPV6_REBIND:
  185. case DHCPV6_INFORMATION_REQUEST:
  186. if (pkt->relay_info_.empty() && !pkt->getLocalAddr().isV6Multicast()) {
  187. return (false);
  188. }
  189. break;
  190. default:
  191. // do nothing
  192. ;
  193. }
  194. return (true);
  195. }
  196. bool Dhcpv6Srv::run() {
  197. while (!shutdown_) {
  198. // client's message and server's response
  199. Pkt6Ptr query;
  200. Pkt6Ptr rsp;
  201. try {
  202. // The lease database backend may install some timers for which
  203. // the handlers need to be executed periodically. Retrieve the
  204. // maximum interval at which the handlers must be executed from
  205. // the lease manager.
  206. uint32_t timeout = LeaseMgrFactory::instance().getIOServiceExecInterval();
  207. // If the returned value is zero it means that there are no
  208. // timers installed, so use a default value.
  209. if (timeout == 0) {
  210. timeout = 1000;
  211. }
  212. query = receivePacket(timeout);
  213. } catch (const SignalInterruptOnSelect) {
  214. // Packet reception interrupted because a signal has been received.
  215. // This is not an error because we might have received a SIGTERM,
  216. // SIGINT or SIGHUP which are handled by the server. For signals
  217. // that are not handled by the server we rely on the default
  218. // behavior of the system, but there is nothing we should log here.
  219. } catch (const std::exception& e) {
  220. LOG_ERROR(dhcp6_logger, DHCP6_PACKET_RECEIVE_FAIL).arg(e.what());
  221. }
  222. // Handle next signal received by the process. It must be called after
  223. // an attempt to receive a packet to properly handle server shut down.
  224. // The SIGTERM or SIGINT will be received prior to, or during execution
  225. // of select() (select is invoked by recivePacket()). When that happens,
  226. // select will be interrupted. The signal handler will be invoked
  227. // immediately after select(). The handler will set the shutdown flag
  228. // and cause the process to terminate before the next select() function
  229. // is called. If the function was called before receivePacket the
  230. // process could wait up to the duration of timeout of select() to
  231. // terminate.
  232. handleSignal();
  233. // Execute ready timers for the lease database, e.g. Lease File Cleanup.
  234. try {
  235. LeaseMgrFactory::instance().getIOService()->poll();
  236. } catch (const std::exception& ex) {
  237. LOG_WARN(dhcp6_logger, DHCP6_LEASE_DATABASE_TIMERS_EXEC_FAIL)
  238. .arg(ex.what());
  239. }
  240. // Timeout may be reached or signal received, which breaks select()
  241. // with no packet received
  242. if (!query) {
  243. continue;
  244. }
  245. // In order to parse the DHCP options, the server needs to use some
  246. // configuration information such as: existing option spaces, option
  247. // definitions etc. This is the kind of information which is not
  248. // available in the libdhcp, so we need to supply our own implementation
  249. // of the option parsing function here, which would rely on the
  250. // configuration data.
  251. query->setCallback(boost::bind(&Dhcpv6Srv::unpackOptions, this, _1, _2,
  252. _3, _4, _5));
  253. bool skip_unpack = false;
  254. // The packet has just been received so contains the uninterpreted wire
  255. // data; execute callouts registered for buffer6_receive.
  256. if (HooksManager::calloutsPresent(Hooks.hook_index_buffer6_receive_)) {
  257. CalloutHandlePtr callout_handle = getCalloutHandle(query);
  258. // Delete previously set arguments
  259. callout_handle->deleteAllArguments();
  260. // Pass incoming packet as argument
  261. callout_handle->setArgument("query6", query);
  262. // Call callouts
  263. HooksManager::callCallouts(Hooks.hook_index_buffer6_receive_, *callout_handle);
  264. // Callouts decided to skip the next processing step. The next
  265. // processing step would to parse the packet, so skip at this
  266. // stage means that callouts did the parsing already, so server
  267. // should skip parsing.
  268. if (callout_handle->getSkip()) {
  269. LOG_DEBUG(dhcp6_logger, DBG_DHCP6_HOOKS, DHCP6_HOOK_BUFFER_RCVD_SKIP);
  270. skip_unpack = true;
  271. }
  272. callout_handle->getArgument("query6", query);
  273. }
  274. // Unpack the packet information unless the buffer6_receive callouts
  275. // indicated they did it
  276. if (!skip_unpack) {
  277. try {
  278. query->unpack();
  279. } catch (const std::exception &e) {
  280. LOG_DEBUG(dhcp6_logger, DBG_DHCP6_DETAIL,
  281. DHCP6_PACKET_PARSE_FAIL).arg(e.what());
  282. continue;
  283. }
  284. }
  285. // Check if received query carries server identifier matching
  286. // server identifier being used by the server.
  287. if (!testServerID(query)) {
  288. continue;
  289. }
  290. // Check if the received query has been sent to unicast or multicast.
  291. // The Solicit, Confirm, Rebind and Information Request will be
  292. // discarded if sent to unicast address.
  293. if (!testUnicast(query)) {
  294. continue;
  295. }
  296. LOG_DEBUG(dhcp6_logger, DBG_DHCP6_DETAIL, DHCP6_PACKET_RECEIVED)
  297. .arg(query->getName());
  298. LOG_DEBUG(dhcp6_logger, DBG_DHCP6_DETAIL_DATA, DHCP6_QUERY_DATA)
  299. .arg(static_cast<int>(query->getType()))
  300. .arg(query->getBuffer().getLength())
  301. .arg(query->toText());
  302. // At this point the information in the packet has been unpacked into
  303. // the various packet fields and option objects has been cretated.
  304. // Execute callouts registered for packet6_receive.
  305. if (HooksManager::calloutsPresent(Hooks.hook_index_pkt6_receive_)) {
  306. CalloutHandlePtr callout_handle = getCalloutHandle(query);
  307. // Delete previously set arguments
  308. callout_handle->deleteAllArguments();
  309. // Pass incoming packet as argument
  310. callout_handle->setArgument("query6", query);
  311. // Call callouts
  312. HooksManager::callCallouts(Hooks.hook_index_pkt6_receive_, *callout_handle);
  313. // Callouts decided to skip the next processing step. The next
  314. // processing step would to process the packet, so skip at this
  315. // stage means drop.
  316. if (callout_handle->getSkip()) {
  317. LOG_DEBUG(dhcp6_logger, DBG_DHCP6_HOOKS, DHCP6_HOOK_PACKET_RCVD_SKIP);
  318. continue;
  319. }
  320. callout_handle->getArgument("query6", query);
  321. }
  322. // Assign this packet to a class, if possible
  323. classifyPacket(query);
  324. try {
  325. NameChangeRequestPtr ncr;
  326. switch (query->getType()) {
  327. case DHCPV6_SOLICIT:
  328. rsp = processSolicit(query);
  329. break;
  330. case DHCPV6_REQUEST:
  331. rsp = processRequest(query);
  332. break;
  333. case DHCPV6_RENEW:
  334. rsp = processRenew(query);
  335. break;
  336. case DHCPV6_REBIND:
  337. rsp = processRebind(query);
  338. break;
  339. case DHCPV6_CONFIRM:
  340. rsp = processConfirm(query);
  341. break;
  342. case DHCPV6_RELEASE:
  343. rsp = processRelease(query);
  344. break;
  345. case DHCPV6_DECLINE:
  346. rsp = processDecline(query);
  347. break;
  348. case DHCPV6_INFORMATION_REQUEST:
  349. rsp = processInfRequest(query);
  350. break;
  351. default:
  352. // We received a packet type that we do not recognize.
  353. LOG_DEBUG(dhcp6_logger, DBG_DHCP6_BASIC, DHCP6_UNKNOWN_MSG_RECEIVED)
  354. .arg(static_cast<int>(query->getType()))
  355. .arg(query->getIface());
  356. // Only action is to output a message if debug is enabled,
  357. // and that will be covered by the debug statement before
  358. // the "switch" statement.
  359. ;
  360. }
  361. } catch (const RFCViolation& e) {
  362. LOG_DEBUG(dhcp6_logger, DBG_DHCP6_BASIC, DHCP6_REQUIRED_OPTIONS_CHECK_FAIL)
  363. .arg(query->getName())
  364. .arg(query->getRemoteAddr().toText())
  365. .arg(e.what());
  366. } catch (const isc::Exception& e) {
  367. // Catch-all exception (at least for ones based on the isc Exception
  368. // class, which covers more or less all that are explicitly raised
  369. // in the Kea code). Just log the problem and ignore the packet.
  370. // (The problem is logged as a debug message because debug is
  371. // disabled by default - it prevents a DDOS attack based on the
  372. // sending of problem packets.)
  373. LOG_DEBUG(dhcp6_logger, DBG_DHCP6_BASIC, DHCP6_PACKET_PROCESS_FAIL)
  374. .arg(query->getName())
  375. .arg(query->getRemoteAddr().toText())
  376. .arg(e.what());
  377. }
  378. if (rsp) {
  379. rsp->setRemoteAddr(query->getRemoteAddr());
  380. rsp->setLocalAddr(query->getLocalAddr());
  381. if (rsp->relay_info_.empty()) {
  382. // Direct traffic, send back to the client directly
  383. rsp->setRemotePort(DHCP6_CLIENT_PORT);
  384. } else {
  385. // Relayed traffic, send back to the relay agent
  386. rsp->setRemotePort(DHCP6_SERVER_PORT);
  387. }
  388. rsp->setLocalPort(DHCP6_SERVER_PORT);
  389. rsp->setIndex(query->getIndex());
  390. rsp->setIface(query->getIface());
  391. // Specifies if server should do the packing
  392. bool skip_pack = false;
  393. // Server's reply packet now has all options and fields set.
  394. // Options are represented by individual objects, but the
  395. // output wire data has not been prepared yet.
  396. // Execute all callouts registered for packet6_send
  397. if (HooksManager::calloutsPresent(Hooks.hook_index_pkt6_send_)) {
  398. CalloutHandlePtr callout_handle = getCalloutHandle(query);
  399. // Delete all previous arguments
  400. callout_handle->deleteAllArguments();
  401. // Set our response
  402. callout_handle->setArgument("response6", rsp);
  403. // Call all installed callouts
  404. HooksManager::callCallouts(Hooks.hook_index_pkt6_send_, *callout_handle);
  405. // Callouts decided to skip the next processing step. The next
  406. // processing step would to pack the packet (create wire data).
  407. // That step will be skipped if any callout sets skip flag.
  408. // It essentially means that the callout already did packing,
  409. // so the server does not have to do it again.
  410. if (callout_handle->getSkip()) {
  411. LOG_DEBUG(dhcp6_logger, DBG_DHCP6_HOOKS, DHCP6_HOOK_PACKET_SEND_SKIP);
  412. skip_pack = true;
  413. }
  414. }
  415. LOG_DEBUG(dhcp6_logger, DBG_DHCP6_DETAIL_DATA,
  416. DHCP6_RESPONSE_DATA)
  417. .arg(static_cast<int>(rsp->getType())).arg(rsp->toText());
  418. if (!skip_pack) {
  419. try {
  420. rsp->pack();
  421. } catch (const std::exception& e) {
  422. LOG_ERROR(dhcp6_logger, DHCP6_PACK_FAIL)
  423. .arg(e.what());
  424. continue;
  425. }
  426. }
  427. try {
  428. // Now all fields and options are constructed into output wire buffer.
  429. // Option objects modification does not make sense anymore. Hooks
  430. // can only manipulate wire buffer at this stage.
  431. // Let's execute all callouts registered for buffer6_send
  432. if (HooksManager::calloutsPresent(Hooks.hook_index_buffer6_send_)) {
  433. CalloutHandlePtr callout_handle = getCalloutHandle(query);
  434. // Delete previously set arguments
  435. callout_handle->deleteAllArguments();
  436. // Pass incoming packet as argument
  437. callout_handle->setArgument("response6", rsp);
  438. // Call callouts
  439. HooksManager::callCallouts(Hooks.hook_index_buffer6_send_, *callout_handle);
  440. // Callouts decided to skip the next processing step. The next
  441. // processing step would to parse the packet, so skip at this
  442. // stage means drop.
  443. if (callout_handle->getSkip()) {
  444. LOG_DEBUG(dhcp6_logger, DBG_DHCP6_HOOKS, DHCP6_HOOK_BUFFER_SEND_SKIP);
  445. continue;
  446. }
  447. callout_handle->getArgument("response6", rsp);
  448. }
  449. LOG_DEBUG(dhcp6_logger, DBG_DHCP6_DETAIL_DATA,
  450. DHCP6_RESPONSE_DATA)
  451. .arg(static_cast<int>(rsp->getType())).arg(rsp->toText());
  452. sendPacket(rsp);
  453. } catch (const std::exception& e) {
  454. LOG_ERROR(dhcp6_logger, DHCP6_PACKET_SEND_FAIL)
  455. .arg(e.what());
  456. }
  457. }
  458. }
  459. return (true);
  460. }
  461. bool Dhcpv6Srv::loadServerID(const std::string& file_name) {
  462. // load content of the file into a string
  463. fstream f(file_name.c_str(), ios::in);
  464. if (!f.is_open()) {
  465. return (false);
  466. }
  467. string hex_string;
  468. f >> hex_string;
  469. f.close();
  470. // remove any spaces
  471. boost::algorithm::erase_all(hex_string, " ");
  472. // now remove :
  473. /// @todo: We should check first if the format is sane.
  474. /// Otherwise 1:2:3:4 will be converted to 0x12, 0x34
  475. boost::algorithm::erase_all(hex_string, ":");
  476. std::vector<uint8_t> bin;
  477. // Decode the hex string and store it in bin (which happens
  478. // to be OptionBuffer format)
  479. isc::util::encode::decodeHex(hex_string, bin);
  480. // Now create server-id option
  481. serverid_.reset(new Option(Option::V6, D6O_SERVERID, bin));
  482. return (true);
  483. }
  484. std::string
  485. Dhcpv6Srv::duidToString(const OptionPtr& opt) {
  486. stringstream tmp;
  487. OptionBuffer data = opt->getData();
  488. bool colon = false;
  489. for (OptionBufferConstIter it = data.begin(); it != data.end(); ++it) {
  490. if (colon) {
  491. tmp << ":";
  492. }
  493. tmp << hex << setw(2) << setfill('0') << static_cast<uint16_t>(*it);
  494. if (!colon) {
  495. colon = true;
  496. }
  497. }
  498. return tmp.str();
  499. }
  500. bool
  501. Dhcpv6Srv::writeServerID(const std::string& file_name) {
  502. fstream f(file_name.c_str(), ios::out | ios::trunc);
  503. if (!f.good()) {
  504. return (false);
  505. }
  506. f << duidToString(getServerID());
  507. f.close();
  508. return (true);
  509. }
  510. void
  511. Dhcpv6Srv::generateServerID() {
  512. /// @todo: This code implements support for DUID-LLT (the recommended one).
  513. /// We should eventually add support for other DUID types: DUID-LL, DUID-EN
  514. /// and DUID-UUID
  515. const IfaceMgr::IfaceCollection& ifaces = IfaceMgr::instance().getIfaces();
  516. // Let's find suitable interface.
  517. for (IfaceMgr::IfaceCollection::const_iterator iface = ifaces.begin();
  518. iface != ifaces.end(); ++iface) {
  519. // All the following checks could be merged into one multi-condition
  520. // statement, but let's keep them separated as perhaps one day
  521. // we will grow knobs to selectively turn them on or off. Also,
  522. // this code is used only *once* during first start on a new machine
  523. // and then server-id is stored. (or at least it will be once
  524. // DUID storage is implemented)
  525. // I wish there was a this_is_a_real_physical_interface flag...
  526. // MAC address should be at least 6 bytes. Although there is no such
  527. // requirement in any RFC, all decent physical interfaces (Ethernet,
  528. // WiFi, Infiniband, etc.) have 6 bytes long MAC address. We want to
  529. // base our DUID on real hardware address, rather than virtual
  530. // interface that pretends that underlying IP address is its MAC.
  531. if (iface->getMacLen() < MIN_MAC_LEN) {
  532. continue;
  533. }
  534. // Let's don't use loopback.
  535. if (iface->flag_loopback_) {
  536. continue;
  537. }
  538. // Let's skip downed interfaces. It is better to use working ones.
  539. if (!iface->flag_up_) {
  540. continue;
  541. }
  542. // Some interfaces (like lo on Linux) report 6-bytes long
  543. // MAC address 00:00:00:00:00:00. Let's not use such weird interfaces
  544. // to generate DUID.
  545. if (isRangeZero(iface->getMac(), iface->getMac() + iface->getMacLen())) {
  546. continue;
  547. }
  548. // Ok, we have useful MAC. Let's generate DUID-LLT based on
  549. // it. See RFC3315, Section 9.2 for details.
  550. // DUID uses seconds since midnight of 01-01-2000, time() returns
  551. // seconds since 01-01-1970. DUID_TIME_EPOCH substution corrects that.
  552. time_t seconds = time(NULL);
  553. seconds -= DUID_TIME_EPOCH;
  554. OptionBuffer srvid(8 + iface->getMacLen());
  555. // We know that the buffer is more than 8 bytes long at this point.
  556. writeUint16(DUID::DUID_LLT, &srvid[0], 2);
  557. writeUint16(HWTYPE_ETHERNET, &srvid[2], 2);
  558. writeUint32(static_cast<uint32_t>(seconds), &srvid[4], 4);
  559. memcpy(&srvid[8], iface->getMac(), iface->getMacLen());
  560. serverid_ = OptionPtr(new Option(Option::V6, D6O_SERVERID,
  561. srvid.begin(), srvid.end()));
  562. return;
  563. }
  564. // If we reached here, there are no suitable interfaces found.
  565. // Either interface detection is not supported on this platform or
  566. // this is really weird box. Let's use DUID-EN instead.
  567. // See Section 9.3 of RFC3315 for details.
  568. OptionBuffer srvid(12);
  569. writeUint16(DUID::DUID_EN, &srvid[0], srvid.size());
  570. writeUint32(ENTERPRISE_ID_ISC, &srvid[2], srvid.size() - 2);
  571. // Length of the identifier is company specific. I hereby declare
  572. // ISC "standard" of 6 bytes long pseudo-random numbers.
  573. srandom(time(NULL));
  574. fillRandom(&srvid[6], &srvid[12]);
  575. serverid_ = OptionPtr(new Option(Option::V6, D6O_SERVERID,
  576. srvid.begin(), srvid.end()));
  577. }
  578. void
  579. Dhcpv6Srv::copyClientOptions(const Pkt6Ptr& question, Pkt6Ptr& answer) {
  580. // Add client-id.
  581. OptionPtr clientid = question->getOption(D6O_CLIENTID);
  582. if (clientid) {
  583. answer->addOption(clientid);
  584. }
  585. /// @todo: Should throw if there is no client-id (except anonymous INF-REQUEST)
  586. // If this is a relayed message, we need to copy relay information
  587. if (!question->relay_info_.empty()) {
  588. answer->copyRelayInfo(question);
  589. }
  590. }
  591. void
  592. Dhcpv6Srv::appendDefaultOptions(const Pkt6Ptr&, Pkt6Ptr& answer) {
  593. // add server-id
  594. answer->addOption(getServerID());
  595. }
  596. void
  597. Dhcpv6Srv::appendRequestedOptions(const Pkt6Ptr& question, Pkt6Ptr& answer) {
  598. // Client requests some options using ORO option. Try to
  599. // get this option from client's message.
  600. boost::shared_ptr<OptionIntArray<uint16_t> > option_oro =
  601. boost::dynamic_pointer_cast<OptionIntArray<uint16_t> >
  602. (question->getOption(D6O_ORO));
  603. // Option ORO not found? We're done here then.
  604. if (!option_oro) {
  605. return;
  606. }
  607. // Get global option definitions (i.e. options defined to apply to all,
  608. // unless overwritten on a subnet or host level)
  609. ConstCfgOptionPtr global_opts = CfgMgr::instance().getCurrentCfg()->
  610. getCfgOption();
  611. // Get the configured subnet suitable for the incoming packet.
  612. // It may be NULL (if server is misconfigured or the client was rejected
  613. // using client classes).
  614. Subnet6Ptr subnet = selectSubnet(question);
  615. // Get the list of options that client requested.
  616. const std::vector<uint16_t>& requested_opts = option_oro->getValues();
  617. BOOST_FOREACH(uint16_t opt, requested_opts) {
  618. // If we found a subnet for this client, try subnet first.
  619. if (subnet) {
  620. OptionDescriptor desc = subnet->getCfgOption()->get("dhcp6", opt);
  621. if (desc.option_) {
  622. // Attempt to assign an option from subnet first.
  623. answer->addOption(desc.option_);
  624. continue;
  625. }
  626. }
  627. // If subnet specific option is not there, try global.
  628. OptionDescriptor desc = global_opts->get("dhcp6", opt);
  629. if (desc.option_) {
  630. answer->addOption(desc.option_);
  631. }
  632. }
  633. }
  634. void
  635. Dhcpv6Srv::appendRequestedVendorOptions(const Pkt6Ptr& question, Pkt6Ptr& answer) {
  636. // Get the configured subnet suitable for the incoming packet.
  637. Subnet6Ptr subnet = selectSubnet(question);
  638. // Leave if there is no subnet matching the incoming packet.
  639. // There is no need to log the error message here because
  640. // it will be logged in the assignLease() when it fails to
  641. // pick the suitable subnet. We don't want to duplicate
  642. // error messages in such case.
  643. if (!subnet) {
  644. return;
  645. }
  646. // Try to get the vendor option
  647. boost::shared_ptr<OptionVendor> vendor_req =
  648. boost::dynamic_pointer_cast<OptionVendor>(question->getOption(D6O_VENDOR_OPTS));
  649. if (!vendor_req) {
  650. return;
  651. }
  652. // Let's try to get ORO within that vendor-option
  653. /// @todo This is very specific to vendor-id=4491 (Cable Labs). Other vendors
  654. /// may have different policies.
  655. boost::shared_ptr<OptionUint16Array> oro =
  656. boost::dynamic_pointer_cast<OptionUint16Array>(vendor_req->getOption(DOCSIS3_V6_ORO));
  657. // Option ORO not found. Don't do anything then.
  658. if (!oro) {
  659. return;
  660. }
  661. uint32_t vendor_id = vendor_req->getVendorId();
  662. boost::shared_ptr<OptionVendor> vendor_rsp(new OptionVendor(Option::V6, vendor_id));
  663. // Get the list of options that client requested.
  664. bool added = false;
  665. const std::vector<uint16_t>& requested_opts = oro->getValues();
  666. BOOST_FOREACH(uint16_t opt, requested_opts) {
  667. OptionDescriptor desc = subnet->getCfgOption()->get(vendor_id, opt);
  668. if (desc.option_) {
  669. vendor_rsp->addOption(desc.option_);
  670. added = true;
  671. }
  672. }
  673. if (added) {
  674. answer->addOption(vendor_rsp);
  675. }
  676. }
  677. OptionPtr
  678. Dhcpv6Srv::createStatusCode(uint16_t code, const std::string& text) {
  679. // @todo This function uses OptionCustom class to manage contents
  680. // of the data fields. Since this this option is frequently used
  681. // it may be good to implement dedicated class to avoid performance
  682. // impact.
  683. // Get the definition of the option holding status code.
  684. OptionDefinitionPtr status_code_def =
  685. LibDHCP::getOptionDef(Option::V6, D6O_STATUS_CODE);
  686. // This definition is assumed to be initialized in LibDHCP.
  687. assert(status_code_def);
  688. // As there is no dedicated class to represent Status Code
  689. // the OptionCustom class is used here instead.
  690. OptionCustomPtr option_status =
  691. OptionCustomPtr(new OptionCustom(*status_code_def, Option::V6));
  692. assert(option_status);
  693. // Set status code to 'code' (0 - means data field #0).
  694. option_status->writeInteger(code, 0);
  695. // Set a message (1 - means data field #1).
  696. option_status->writeString(text, 1);
  697. return (option_status);
  698. }
  699. void
  700. Dhcpv6Srv::sanityCheck(const Pkt6Ptr& pkt, RequirementLevel clientid,
  701. RequirementLevel serverid) {
  702. OptionCollection client_ids = pkt->getOptions(D6O_CLIENTID);
  703. switch (clientid) {
  704. case MANDATORY:
  705. if (client_ids.size() != 1) {
  706. isc_throw(RFCViolation, "Exactly 1 client-id option expected in "
  707. << pkt->getName() << ", but " << client_ids.size()
  708. << " received");
  709. }
  710. break;
  711. case OPTIONAL:
  712. if (client_ids.size() > 1) {
  713. isc_throw(RFCViolation, "Too many (" << client_ids.size()
  714. << ") client-id options received in " << pkt->getName());
  715. }
  716. break;
  717. case FORBIDDEN:
  718. // doesn't make sense - client-id is always allowed
  719. break;
  720. }
  721. OptionCollection server_ids = pkt->getOptions(D6O_SERVERID);
  722. switch (serverid) {
  723. case FORBIDDEN:
  724. if (!server_ids.empty()) {
  725. isc_throw(RFCViolation, "Server-id option was not expected, but "
  726. << server_ids.size() << " received in " << pkt->getName());
  727. }
  728. break;
  729. case MANDATORY:
  730. if (server_ids.size() != 1) {
  731. isc_throw(RFCViolation, "Invalid number of server-id options received ("
  732. << server_ids.size() << "), exactly 1 expected in message "
  733. << pkt->getName());
  734. }
  735. break;
  736. case OPTIONAL:
  737. if (server_ids.size() > 1) {
  738. isc_throw(RFCViolation, "Too many (" << server_ids.size()
  739. << ") server-id options received in " << pkt->getName());
  740. }
  741. }
  742. }
  743. Subnet6Ptr
  744. Dhcpv6Srv::selectSubnet(const Pkt6Ptr& question) {
  745. // Initialize subnet selector with the values used to select the subnet.
  746. SubnetSelector selector;
  747. selector.iface_name_ = question->getIface();
  748. selector.remote_address_ = question->getRemoteAddr();
  749. selector.first_relay_linkaddr_ = IOAddress("::");
  750. selector.client_classes_ = question->classes_;
  751. // Initialize fields specific to relayed messages.
  752. if (!question->relay_info_.empty()) {
  753. selector.first_relay_linkaddr_ = question->relay_info_.back().linkaddr_;
  754. selector.interface_id_ =
  755. question->getAnyRelayOption(D6O_INTERFACE_ID,
  756. Pkt6::RELAY_GET_FIRST);
  757. }
  758. Subnet6Ptr subnet = CfgMgr::instance().getCurrentCfg()->
  759. getCfgSubnets6()->selectSubnet(selector);
  760. // Let's execute all callouts registered for subnet6_receive
  761. if (HooksManager::calloutsPresent(Hooks.hook_index_subnet6_select_)) {
  762. CalloutHandlePtr callout_handle = getCalloutHandle(question);
  763. // We're reusing callout_handle from previous calls
  764. callout_handle->deleteAllArguments();
  765. // Set new arguments
  766. callout_handle->setArgument("query6", question);
  767. callout_handle->setArgument("subnet6", subnet);
  768. // We pass pointer to const collection for performance reasons.
  769. // Otherwise we would get a non-trivial performance penalty each
  770. // time subnet6_select is called.
  771. callout_handle->setArgument("subnet6collection",
  772. CfgMgr::instance().getCurrentCfg()->
  773. getCfgSubnets6()->getAll());
  774. // Call user (and server-side) callouts
  775. HooksManager::callCallouts(Hooks.hook_index_subnet6_select_, *callout_handle);
  776. // Callouts decided to skip this step. This means that no subnet will be
  777. // selected. Packet processing will continue, but it will be severly limited
  778. // (i.e. only global options will be assigned)
  779. if (callout_handle->getSkip()) {
  780. LOG_DEBUG(dhcp6_logger, DBG_DHCP6_HOOKS, DHCP6_HOOK_SUBNET6_SELECT_SKIP);
  781. return (Subnet6Ptr());
  782. }
  783. // Use whatever subnet was specified by the callout
  784. callout_handle->getArgument("subnet6", subnet);
  785. }
  786. return (subnet);
  787. }
  788. void
  789. Dhcpv6Srv::assignLeases(const Pkt6Ptr& question, Pkt6Ptr& answer) {
  790. // We need to allocate addresses for all IA_NA options in the client's
  791. // question (i.e. SOLICIT or REQUEST) message.
  792. // @todo add support for IA_TA
  793. // We need to select a subnet the client is connected in.
  794. Subnet6Ptr subnet = selectSubnet(question);
  795. if (!subnet) {
  796. // This particular client is out of luck today. We do not have
  797. // information about the subnet he is connected to. This likely means
  798. // misconfiguration of the server (or some relays). We will continue to
  799. // process this message, but our response will be almost useless: no
  800. // addresses or prefixes, no subnet specific configuration etc. The only
  801. // thing this client can get is some global information (like DNS
  802. // servers).
  803. LOG_WARN(dhcp6_logger, DHCP6_SUBNET_SELECTION_FAILED)
  804. .arg(question->getRemoteAddr().toText())
  805. .arg(question->getName());
  806. } else {
  807. LOG_DEBUG(dhcp6_logger, DBG_DHCP6_DETAIL_DATA, DHCP6_SUBNET_SELECTED)
  808. .arg(subnet->toText());
  809. }
  810. // @todo: We should implement Option6Duid some day, but we can do without it
  811. // just fine for now
  812. // Let's find client's DUID. Client is supposed to include its client-id
  813. // option almost all the time (the only exception is an anonymous inf-request,
  814. // but that is mostly a theoretical case). Our allocation engine needs DUID
  815. // and will refuse to allocate anything to anonymous clients.
  816. DuidPtr duid;
  817. OptionPtr opt_duid = question->getOption(D6O_CLIENTID);
  818. if (opt_duid) {
  819. duid = DuidPtr(new DUID(opt_duid->getData()));
  820. } else {
  821. LOG_DEBUG(dhcp6_logger, DBG_DHCP6_BASIC, DHCP6_CLIENTID_MISSING);
  822. // Let's drop the message. This client is not sane.
  823. isc_throw(RFCViolation, "Mandatory client-id is missing in received message");
  824. }
  825. // Now that we have all information about the client, let's iterate over all
  826. // received options and handle IA_NA options one by one and store our
  827. // responses in answer message (ADVERTISE or REPLY).
  828. //
  829. // @todo: IA_TA once we implement support for temporary addresses.
  830. for (OptionCollection::iterator opt = question->options_.begin();
  831. opt != question->options_.end(); ++opt) {
  832. switch (opt->second->getType()) {
  833. case D6O_IA_NA: {
  834. OptionPtr answer_opt = assignIA_NA(subnet, duid, question, answer,
  835. boost::dynamic_pointer_cast<
  836. Option6IA>(opt->second));
  837. if (answer_opt) {
  838. answer->addOption(answer_opt);
  839. }
  840. break;
  841. }
  842. case D6O_IA_PD: {
  843. OptionPtr answer_opt = assignIA_PD(subnet, duid, question,
  844. boost::dynamic_pointer_cast<
  845. Option6IA>(opt->second));
  846. if (answer_opt) {
  847. answer->addOption(answer_opt);
  848. }
  849. }
  850. default:
  851. break;
  852. }
  853. }
  854. }
  855. void
  856. Dhcpv6Srv::processClientFqdn(const Pkt6Ptr& question, const Pkt6Ptr& answer) {
  857. // Get Client FQDN Option from the client's message. If this option hasn't
  858. // been included, do nothing.
  859. Option6ClientFqdnPtr fqdn = boost::dynamic_pointer_cast<
  860. Option6ClientFqdn>(question->getOption(D6O_CLIENT_FQDN));
  861. if (!fqdn) {
  862. return;
  863. }
  864. LOG_DEBUG(dhcp6_logger, DBG_DHCP6_DETAIL,
  865. DHCP6_DDNS_RECEIVE_FQDN).arg(fqdn->toText());
  866. // Create the DHCPv6 Client FQDN Option to be included in the server's
  867. // response to a client.
  868. Option6ClientFqdnPtr fqdn_resp(new Option6ClientFqdn(*fqdn));
  869. // Set the server S, N, and O flags based on client's flags and
  870. // current configuration.
  871. D2ClientMgr& d2_mgr = CfgMgr::instance().getD2ClientMgr();
  872. d2_mgr.adjustFqdnFlags<Option6ClientFqdn>(*fqdn, *fqdn_resp);
  873. // Adjust the domain name based on domain name value and type sent by the
  874. // client and current configuration.
  875. d2_mgr.adjustDomainName<Option6ClientFqdn>(*fqdn, *fqdn_resp);
  876. // The FQDN has been processed successfully. Let's append it to the
  877. // response to be sent to a client. Note that the Client FQDN option is
  878. // always sent back to the client if Client FQDN was included in the
  879. // client's message.
  880. answer->addOption(fqdn_resp);
  881. }
  882. void
  883. Dhcpv6Srv::createNameChangeRequests(const Pkt6Ptr& answer) {
  884. // Don't create NameChangeRequests if DNS updates are disabled.
  885. if (!CfgMgr::instance().ddnsEnabled()) {
  886. return;
  887. }
  888. // The response message instance is always required. For instance it
  889. // holds the Client Identifier. It is a programming error if supplied
  890. // message is NULL.
  891. if (!answer) {
  892. isc_throw(isc::Unexpected, "an instance of the object"
  893. << " encapsulating server's message must not be"
  894. << " NULL when creating DNS NameChangeRequest");
  895. }
  896. // It is likely that client haven't included the FQDN option. In such case,
  897. // FQDN option will be NULL. This is valid state, so we simply return.
  898. Option6ClientFqdnPtr opt_fqdn = boost::dynamic_pointer_cast<
  899. Option6ClientFqdn>(answer->getOption(D6O_CLIENT_FQDN));
  900. if (!opt_fqdn) {
  901. return;
  902. }
  903. // Get the update directions that should be performed based on our
  904. // response FQDN flags.
  905. bool do_fwd = false;
  906. bool do_rev = false;
  907. CfgMgr::instance().getD2ClientMgr().getUpdateDirections(*opt_fqdn,
  908. do_fwd, do_rev);
  909. if (!do_fwd && !do_rev) {
  910. // Flags indicate there is Nothing to do, get out now.
  911. // The Most likely scenario is that we are honoring the client's
  912. // request that no updates be done.
  913. return;
  914. }
  915. // Get the Client Id. It is mandatory and a function creating a response
  916. // would have thrown an exception if it was missing. Thus throwning
  917. // Unexpected if it is missing as it is a programming error.
  918. OptionPtr opt_duid = answer->getOption(D6O_CLIENTID);
  919. if (!opt_duid) {
  920. isc_throw(isc::Unexpected,
  921. "client identifier is required when creating a new"
  922. " DNS NameChangeRequest");
  923. }
  924. DuidPtr duid = DuidPtr(new DUID(opt_duid->getData()));
  925. // Get the FQDN in the on-wire format. It will be needed to compute
  926. // DHCID.
  927. OutputBuffer name_buf(1);
  928. opt_fqdn->packDomainName(name_buf);
  929. const uint8_t* name_data = static_cast<const uint8_t*>(name_buf.getData());
  930. // @todo currently D2Dhcid accepts a vector holding FQDN.
  931. // However, it will be faster if we used a pointer name_data.
  932. std::vector<uint8_t> buf_vec(name_data, name_data + name_buf.getLength());
  933. // Compute DHCID from Client Identifier and FQDN.
  934. isc::dhcp_ddns::D2Dhcid dhcid(*duid, buf_vec);
  935. // Get all IAs from the answer. For each IA, holding an address we will
  936. // create a corresponding NameChangeRequest.
  937. OptionCollection answer_ias = answer->getOptions(D6O_IA_NA);
  938. for (OptionCollection::const_iterator answer_ia =
  939. answer_ias.begin(); answer_ia != answer_ias.end(); ++answer_ia) {
  940. /// @todo IA_NA may contain multiple addresses. We should process
  941. /// each address individually. Currently we get only one.
  942. Option6IAAddrPtr iaaddr = boost::static_pointer_cast<
  943. Option6IAAddr>(answer_ia->second->getOption(D6O_IAADDR));
  944. // We need an address to create a name-to-address mapping.
  945. // If address is missing for any reason, go to the next IA.
  946. if (!iaaddr) {
  947. continue;
  948. }
  949. // Create new NameChangeRequest. Use the domain name from the FQDN.
  950. // This is an FQDN included in the response to the client, so it
  951. // holds a fully qualified domain-name already (not partial).
  952. // Get the IP address from the lease.
  953. NameChangeRequestPtr ncr;
  954. ncr.reset(new NameChangeRequest(isc::dhcp_ddns::CHG_ADD,
  955. do_fwd, do_rev,
  956. opt_fqdn->getDomainName(),
  957. iaaddr->getAddress().toText(),
  958. dhcid, 0, iaaddr->getValid()));
  959. LOG_DEBUG(dhcp6_logger, DBG_DHCP6_DETAIL,
  960. DHCP6_DDNS_CREATE_ADD_NAME_CHANGE_REQUEST).arg(ncr->toText());
  961. // Post the NCR to the D2ClientMgr.
  962. CfgMgr::instance().getD2ClientMgr().sendRequest(ncr);
  963. /// @todo Currently we create NCR with the first IPv6 address that
  964. /// is carried in one of the IA_NAs. In the future, the NCR API should
  965. /// be extended to map multiple IPv6 addresses to a single FQDN.
  966. /// In such case, this return statement will be removed.
  967. return;
  968. }
  969. }
  970. void
  971. Dhcpv6Srv::createRemovalNameChangeRequest(const Lease6Ptr& lease) {
  972. // Don't create NameChangeRequests if DNS updates are disabled.
  973. if (!CfgMgr::instance().ddnsEnabled()) {
  974. return;
  975. }
  976. // If we haven't performed a DNS Update when lease was acquired,
  977. // there is nothing to do here.
  978. if (!lease->fqdn_fwd_ && !lease->fqdn_rev_) {
  979. return;
  980. }
  981. // If hostname is non-empty, try to convert it to wire format so as
  982. // DHCID can be computed from it. This may throw an exception if hostname
  983. // has invalid format or is empty. Again, this should be only possible
  984. // in case of manual intervention in the database. Note that the last
  985. // parameter passed to the writeFqdn function forces conversion of the FQDN
  986. // to lower case. This is required by the RFC4701, section 3.5.
  987. // The DHCID computation is further in this function.
  988. std::vector<uint8_t> hostname_wire;
  989. try {
  990. OptionDataTypeUtil::writeFqdn(lease->hostname_, hostname_wire, true);
  991. } catch (const Exception& ex) {
  992. LOG_ERROR(dhcp6_logger, DHCP6_DDNS_REMOVE_INVALID_HOSTNAME)
  993. .arg(lease->hostname_.empty() ? "(empty)" : lease->hostname_)
  994. .arg(lease->addr_.toText());
  995. return;
  996. }
  997. // DUID must have been checked already by the caller of this function.
  998. // Let's be on the safe side and make sure it is non-NULL and throw
  999. // an exception if it is NULL.
  1000. if (!lease->duid_) {
  1001. isc_throw(isc::Unexpected, "DUID must be set when creating"
  1002. << " NameChangeRequest for DNS records removal for "
  1003. << lease->addr_);
  1004. }
  1005. isc::dhcp_ddns::D2Dhcid dhcid(*lease->duid_, hostname_wire);
  1006. // Create a NameChangeRequest to remove the entry.
  1007. NameChangeRequestPtr ncr;
  1008. ncr.reset(new NameChangeRequest(isc::dhcp_ddns::CHG_REMOVE,
  1009. lease->fqdn_fwd_, lease->fqdn_rev_,
  1010. lease->hostname_,
  1011. lease->addr_.toText(),
  1012. dhcid, 0, lease->valid_lft_));
  1013. LOG_DEBUG(dhcp6_logger, DBG_DHCP6_DETAIL,
  1014. DHCP6_DDNS_CREATE_REMOVE_NAME_CHANGE_REQUEST).arg(ncr->toText());
  1015. // Post the NCR to the D2ClientMgr.
  1016. CfgMgr::instance().getD2ClientMgr().sendRequest(ncr);
  1017. }
  1018. HWAddrPtr
  1019. Dhcpv6Srv::getMAC(const Pkt6Ptr& pkt) {
  1020. CfgMACSources mac_sources = CfgMgr::instance().getCurrentCfg()->
  1021. getMACSources().get();
  1022. HWAddrPtr hwaddr;
  1023. for (CfgMACSources::const_iterator it = mac_sources.begin();
  1024. it != mac_sources.end(); ++it) {
  1025. hwaddr = pkt->getMAC(*it);
  1026. if (hwaddr) {
  1027. return (hwaddr);
  1028. }
  1029. }
  1030. return (hwaddr);
  1031. }
  1032. OptionPtr
  1033. Dhcpv6Srv::assignIA_NA(const Subnet6Ptr& subnet, const DuidPtr& duid,
  1034. const Pkt6Ptr& query, const Pkt6Ptr& answer,
  1035. boost::shared_ptr<Option6IA> ia) {
  1036. // If there is no subnet selected for handling this IA_NA, the only thing to do left is
  1037. // to say that we are sorry, but the user won't get an address. As a convenience, we
  1038. // use a different status text to indicate that (compare to the same status code,
  1039. // but different wording below)
  1040. if (!subnet) {
  1041. // Creatasse empty IA_NA option with IAID matching the request.
  1042. // Note that we don't use OptionDefinition class to create this option.
  1043. // This is because we prefer using a constructor of Option6IA that
  1044. // initializes IAID. Otherwise we would have to use setIAID() after
  1045. // creation of the option which has some performance implications.
  1046. boost::shared_ptr<Option6IA> ia_rsp(new Option6IA(D6O_IA_NA, ia->getIAID()));
  1047. // Insert status code NoAddrsAvail.
  1048. ia_rsp->addOption(createStatusCode(STATUS_NoAddrsAvail, "Sorry, no subnet available."));
  1049. return (ia_rsp);
  1050. }
  1051. // Check if the client sent us a hint in his IA_NA. Clients may send an
  1052. // address in their IA_NA options as a suggestion (e.g. the last address
  1053. // they used before).
  1054. boost::shared_ptr<Option6IAAddr> hint_opt =
  1055. boost::dynamic_pointer_cast<Option6IAAddr>(ia->getOption(D6O_IAADDR));
  1056. IOAddress hint("::");
  1057. if (hint_opt) {
  1058. hint = hint_opt->getAddress();
  1059. }
  1060. LOG_DEBUG(dhcp6_logger, DBG_DHCP6_DETAIL, DHCP6_PROCESS_IA_NA_REQUEST)
  1061. .arg(duid ? duid->toText() : "(no-duid)").arg(ia->getIAID())
  1062. .arg(hint_opt ? hint.toText() : "(no hint)");
  1063. // "Fake" allocation is processing of SOLICIT message. We pretend to do an
  1064. // allocation, but we do not put the lease in the database. That is ok,
  1065. // because we do not guarantee that the user will get that exact lease. If
  1066. // the user selects this server to do actual allocation (i.e. sends REQUEST)
  1067. // it should include this hint. That will help us during the actual lease
  1068. // allocation.
  1069. bool fake_allocation = false;
  1070. if (query->getType() == DHCPV6_SOLICIT) {
  1071. /// @todo: Check if we support rapid commit
  1072. fake_allocation = true;
  1073. }
  1074. CalloutHandlePtr callout_handle = getCalloutHandle(query);
  1075. // At this point, we have to make make some decisions with respect to the
  1076. // FQDN option that we have generated as a result of receiving client's
  1077. // FQDN. In particular, we have to get to know if the DNS update will be
  1078. // performed or not. It is possible that option is NULL, which is valid
  1079. // condition if client didn't request DNS updates and server didn't force
  1080. // the update.
  1081. bool do_fwd = false;
  1082. bool do_rev = false;
  1083. Option6ClientFqdnPtr fqdn = boost::dynamic_pointer_cast<
  1084. Option6ClientFqdn>(answer->getOption(D6O_CLIENT_FQDN));
  1085. if (fqdn) {
  1086. CfgMgr::instance().getD2ClientMgr().getUpdateDirections(*fqdn,
  1087. do_fwd, do_rev);
  1088. }
  1089. // Set hostname only in case any of the updates is being performed.
  1090. std::string hostname;
  1091. if (do_fwd || do_rev) {
  1092. hostname = fqdn->getDomainName();
  1093. }
  1094. // Use allocation engine to pick a lease for this client. Allocation engine
  1095. // will try to honour the hint, but it is just a hint - some other address
  1096. // may be used instead. If fake_allocation is set to false, the lease will
  1097. // be inserted into the LeaseMgr as well.
  1098. AllocEngine::ClientContext6 ctx(subnet, duid, ia->getIAID(),
  1099. hint, Lease::TYPE_NA, do_fwd, do_rev,
  1100. hostname, fake_allocation);
  1101. ctx.callout_handle_ = callout_handle;
  1102. // Attempt to get MAC address using configured mechanisms.
  1103. // It's ok if there response is NULL. Hardware address is optional in Lease6.
  1104. ctx.hwaddr_ = getMAC(query);
  1105. Lease6Collection leases = alloc_engine_->allocateLeases6(ctx);
  1106. /// @todo: Handle more than one lease
  1107. Lease6Ptr lease;
  1108. if (!leases.empty()) {
  1109. lease = *leases.begin();
  1110. }
  1111. // Create IA_NA that we will put in the response.
  1112. // Do not use OptionDefinition to create option's instance so
  1113. // as we can initialize IAID using a constructor.
  1114. boost::shared_ptr<Option6IA> ia_rsp(new Option6IA(D6O_IA_NA, ia->getIAID()));
  1115. if (lease) {
  1116. // We have a lease! Let's wrap its content into IA_NA option
  1117. // with IAADDR suboption.
  1118. LOG_DEBUG(dhcp6_logger, DBG_DHCP6_DETAIL, fake_allocation?
  1119. DHCP6_LEASE_ADVERT:DHCP6_LEASE_ALLOC)
  1120. .arg(lease->addr_.toText())
  1121. .arg(duid?duid->toText():"(no-duid)")
  1122. .arg(ia->getIAID());
  1123. ia_rsp->setT1(subnet->getT1());
  1124. ia_rsp->setT2(subnet->getT2());
  1125. boost::shared_ptr<Option6IAAddr>
  1126. addr(new Option6IAAddr(D6O_IAADDR,
  1127. lease->addr_,
  1128. lease->preferred_lft_,
  1129. lease->valid_lft_));
  1130. ia_rsp->addOption(addr);
  1131. // It would be possible to insert status code=0(success) as well,
  1132. // but this is considered waste of bandwidth as absence of status
  1133. // code is considered a success.
  1134. Lease6Ptr old_lease;
  1135. if (!ctx.changed_leases_.empty()) {
  1136. old_lease = *ctx.changed_leases_.begin();
  1137. }
  1138. // Allocation engine may have returned an existing lease. If so, we
  1139. // have to check that the FQDN settings we provided are the same
  1140. // that were set. If they aren't, we will have to remove existing
  1141. // DNS records and update the lease with the new settings.
  1142. if (!fake_allocation && old_lease) {
  1143. conditionalNCRRemoval(old_lease, lease, hostname, do_fwd, do_rev);
  1144. }
  1145. old_lease.reset();
  1146. // We need to repeat that check for leases that used to be used, but
  1147. // are no longer valid.
  1148. if (!ctx.old_leases_.empty()) {
  1149. old_lease = *ctx.old_leases_.begin();
  1150. }
  1151. if (!fake_allocation && old_lease) {
  1152. conditionalNCRRemoval(old_lease, lease, hostname, do_fwd, do_rev);
  1153. }
  1154. } else {
  1155. // Allocation engine did not allocate a lease. The engine logged
  1156. // cause of that failure. The only thing left is to insert
  1157. // status code to pass the sad news to the client.
  1158. LOG_DEBUG(dhcp6_logger, DBG_DHCP6_DETAIL, fake_allocation ?
  1159. DHCP6_LEASE_ADVERT_FAIL : DHCP6_LEASE_ALLOC_FAIL)
  1160. .arg(duid?duid->toText():"(no-duid)")
  1161. .arg(ia->getIAID());
  1162. ia_rsp->addOption(createStatusCode(STATUS_NoAddrsAvail,
  1163. "Sorry, no address could be allocated."));
  1164. }
  1165. return (ia_rsp);
  1166. }
  1167. void
  1168. Dhcpv6Srv::conditionalNCRRemoval(Lease6Ptr& old_lease, Lease6Ptr& new_lease,
  1169. const std::string& hostname, bool do_fwd,
  1170. bool do_rev) {
  1171. if (!old_lease) {
  1172. return;
  1173. }
  1174. if (!new_lease->hasIdenticalFqdn(*old_lease)) {
  1175. LOG_DEBUG(dhcp6_logger, DBG_DHCP6_DETAIL, DHCP6_DDNS_LEASE_ASSIGN_FQDN_CHANGE)
  1176. .arg(old_lease->toText())
  1177. .arg(hostname)
  1178. .arg(do_rev ? "true" : "false")
  1179. .arg(do_fwd ? "true" : "false");
  1180. // Schedule removal of the existing lease.
  1181. createRemovalNameChangeRequest(old_lease);
  1182. }
  1183. }
  1184. OptionPtr
  1185. Dhcpv6Srv::assignIA_PD(const Subnet6Ptr& subnet, const DuidPtr& duid,
  1186. const Pkt6Ptr& query, boost::shared_ptr<Option6IA> ia) {
  1187. // Create IA_PD that we will put in the response.
  1188. // Do not use OptionDefinition to create option's instance so
  1189. // as we can initialize IAID using a constructor.
  1190. boost::shared_ptr<Option6IA> ia_rsp(new Option6IA(D6O_IA_PD, ia->getIAID()));
  1191. // If there is no subnet selected for handling this IA_PD, the only thing to
  1192. // do left is to say that we are sorry, but the user won't get an address.
  1193. // As a convenience, we use a different status text to indicate that
  1194. // (compare to the same status code, but different wording below)
  1195. if (!subnet) {
  1196. // Insert status code NoAddrsAvail.
  1197. ia_rsp->addOption(createStatusCode(STATUS_NoPrefixAvail,
  1198. "Sorry, no subnet available."));
  1199. return (ia_rsp);
  1200. }
  1201. // Check if the client sent us a hint in his IA_PD. Clients may send an
  1202. // address in their IA_NA options as a suggestion (e.g. the last address
  1203. // they used before).
  1204. boost::shared_ptr<Option6IAPrefix> hint_opt =
  1205. boost::dynamic_pointer_cast<Option6IAPrefix>(ia->getOption(D6O_IAPREFIX));
  1206. IOAddress hint("::");
  1207. if (hint_opt) {
  1208. hint = hint_opt->getAddress();
  1209. }
  1210. LOG_DEBUG(dhcp6_logger, DBG_DHCP6_DETAIL, DHCP6_PROCESS_IA_PD_REQUEST)
  1211. .arg(duid ? duid->toText() : "(no-duid)").arg(ia->getIAID())
  1212. .arg(hint_opt ? hint.toText() : "(no hint)");
  1213. // "Fake" allocation is processing of SOLICIT message. We pretend to do an
  1214. // allocation, but we do not put the lease in the database. That is ok,
  1215. // because we do not guarantee that the user will get that exact lease. If
  1216. // the user selects this server to do actual allocation (i.e. sends REQUEST)
  1217. // it should include this hint. That will help us during the actual lease
  1218. // allocation.
  1219. bool fake_allocation = (query->getType() == DHCPV6_SOLICIT);
  1220. CalloutHandlePtr callout_handle = getCalloutHandle(query);
  1221. // Use allocation engine to pick a lease for this client. Allocation engine
  1222. // will try to honour the hint, but it is just a hint - some other address
  1223. // may be used instead. If fake_allocation is set to false, the lease will
  1224. // be inserted into the LeaseMgr as well.
  1225. Lease6Collection old_leases;
  1226. AllocEngine::ClientContext6 ctx(subnet, duid, ia->getIAID(), hint, Lease::TYPE_PD,
  1227. false, false, string(), fake_allocation);
  1228. ctx.callout_handle_ = callout_handle;
  1229. // Attempt to get MAC address using any of available mechanisms.
  1230. // It's ok if there response is NULL. Hardware address is optional in Lease6
  1231. ctx.hwaddr_ = getMAC(query);
  1232. Lease6Collection leases = alloc_engine_->allocateLeases6(ctx);
  1233. if (!leases.empty()) {
  1234. ia_rsp->setT1(subnet->getT1());
  1235. ia_rsp->setT2(subnet->getT2());
  1236. for (Lease6Collection::iterator l = leases.begin();
  1237. l != leases.end(); ++l) {
  1238. // We have a lease! Let's wrap its content into IA_PD option
  1239. // with IAADDR suboption.
  1240. LOG_DEBUG(dhcp6_logger, DBG_DHCP6_DETAIL, fake_allocation ?
  1241. DHCP6_PD_LEASE_ADVERT : DHCP6_PD_LEASE_ALLOC)
  1242. .arg((*l)->addr_.toText())
  1243. .arg(static_cast<int>((*l)->prefixlen_))
  1244. .arg(duid ? duid->toText() : "(no-duid)")
  1245. .arg(ia->getIAID());
  1246. boost::shared_ptr<Option6IAPrefix>
  1247. addr(new Option6IAPrefix(D6O_IAPREFIX, (*l)->addr_,
  1248. (*l)->prefixlen_, (*l)->preferred_lft_,
  1249. (*l)->valid_lft_));
  1250. ia_rsp->addOption(addr);
  1251. }
  1252. // It would be possible to insert status code=0(success) as well,
  1253. // but this is considered waste of bandwidth as absence of status
  1254. // code is considered a success.
  1255. } else {
  1256. // Allocation engine did not allocate a lease. The engine logged
  1257. // cause of that failure. The only thing left is to insert
  1258. // status code to pass the sad news to the client.
  1259. LOG_DEBUG(dhcp6_logger, DBG_DHCP6_DETAIL, fake_allocation ?
  1260. DHCP6_PD_LEASE_ADVERT_FAIL : DHCP6_PD_LEASE_ALLOC_FAIL)
  1261. .arg(duid ? duid->toText() : "(no-duid)")
  1262. .arg(ia->getIAID());
  1263. ia_rsp->addOption(createStatusCode(STATUS_NoPrefixAvail,
  1264. "Sorry, no prefixes could be allocated."));
  1265. }
  1266. return (ia_rsp);
  1267. }
  1268. OptionPtr
  1269. Dhcpv6Srv::extendIA_NA(const Subnet6Ptr& subnet, const DuidPtr& duid,
  1270. const Pkt6Ptr& query, const Pkt6Ptr& answer,
  1271. boost::shared_ptr<Option6IA> ia) {
  1272. // Create empty IA_NA option with IAID matching the request.
  1273. Option6IAPtr ia_rsp(new Option6IA(D6O_IA_NA, ia->getIAID()));
  1274. if (!subnet) {
  1275. /// @todo For simpliclty and due to limitations of LeaseMgr we don't
  1276. /// get the binding for the client for which we don't get subnet id.
  1277. /// Subnet id is a required value when searching for the bindings.
  1278. /// The fact that we can't identify the subnet for the returning client
  1279. /// doesn't really mean that the client has no binding. It is possible
  1280. /// that due to server's reconfiguration the subnet has been removed
  1281. /// or modified since the client has got his lease. We may need to
  1282. /// rethink whether it is appropriate to send no binding if the subnet
  1283. // hasn't been found for the client.
  1284. ia_rsp->addOption(createStatusCode(STATUS_NoBinding,
  1285. "Sorry, no known leases for this duid/iaid."));
  1286. LOG_DEBUG(dhcp6_logger, DBG_DHCP6_DETAIL,
  1287. DHCP6_EXTEND_NA_UNKNOWN_SUBNET)
  1288. .arg(query->getName())
  1289. .arg(duid->toText())
  1290. .arg(ia->getIAID());
  1291. return (ia_rsp);
  1292. }
  1293. // Set up T1, T2 timers
  1294. ia_rsp->setT1(subnet->getT1());
  1295. ia_rsp->setT2(subnet->getT2());
  1296. // At this point, we have to make make some decisions with respect to
  1297. // the FQDN option that we have generated as a result of receiving
  1298. // client's FQDN. In particular, we have to get to know if the DNS
  1299. // update will be performed or not. It is possible that option is NULL,
  1300. // which is valid condition if client didn't request DNS updates and
  1301. // server didn't force the update.
  1302. bool do_fwd = false;
  1303. bool do_rev = false;
  1304. Option6ClientFqdnPtr fqdn = boost::dynamic_pointer_cast<
  1305. Option6ClientFqdn>(answer->getOption(D6O_CLIENT_FQDN));
  1306. if (fqdn) {
  1307. CfgMgr::instance().getD2ClientMgr().getUpdateDirections(*fqdn, do_fwd, do_rev);
  1308. }
  1309. std::string hostname;
  1310. if (fqdn && (do_fwd || do_rev)) {
  1311. hostname = fqdn->getDomainName();
  1312. }
  1313. // Create client context for this renewal
  1314. static const IOAddress none("::");
  1315. AllocEngine::ClientContext6 ctx(subnet, duid, ia->getIAID(),
  1316. none, Lease::TYPE_NA, do_fwd, do_rev,
  1317. hostname, false);
  1318. ctx.callout_handle_ = getCalloutHandle(query);
  1319. ctx.query_ = query;
  1320. ctx.ia_rsp_ = ia_rsp;
  1321. // Attempt to get MAC address using configured mechanisms.
  1322. // It's ok if there response is NULL. Hardware address is optional in Lease6.
  1323. ctx.hwaddr_ = getMAC(query);
  1324. // Extract the addresses that the client is trying to obtain.
  1325. int hints_count = 0;
  1326. OptionCollection addrs = ia->getOptions();
  1327. for (OptionCollection::const_iterator it = addrs.begin();
  1328. it != addrs.end(); ++it) {
  1329. if (it->second->getType() != D6O_IAADDR) {
  1330. continue;
  1331. }
  1332. Option6IAAddrPtr iaaddr = boost::dynamic_pointer_cast<Option6IAAddr>(it->second);
  1333. if (!iaaddr) {
  1334. // That's weird. Option code was ok, but the object type was not.
  1335. continue;
  1336. }
  1337. ctx.hints_.push_back(make_pair(iaaddr->getAddress(), 128));
  1338. // We need to remember it as we'll be removing hints from this list as
  1339. // we extend, cancel or otherwise deal with the leases.
  1340. hints_count++;
  1341. }
  1342. /// @todo: This was clarfied in draft-ietf-dhc-dhcpv6-stateful-issues that
  1343. /// the server is allowed to assign new leases in both Renew and Rebind. For
  1344. /// now, we only support it in Renew, because it breaks a lot of Rebind
  1345. /// unit-tests. Ultimately, whether we allow it or not, should be exposed
  1346. /// as configurable policy. See ticket #3717.
  1347. if (query->getType() == DHCPV6_RENEW) {
  1348. ctx.allow_new_leases_in_renewals_ = true;
  1349. }
  1350. Lease6Collection leases = alloc_engine_->renewLeases6(ctx);
  1351. // Ok, now we have the leases extended. We have:
  1352. // - what the client tried to renew in ctx.hints_
  1353. // - what we actually assigned in leases
  1354. // - old leases that are no longer valid in ctx.old_leases_
  1355. // For all leases we have now, add the IAADDR with non-zero lifetimes.
  1356. for (Lease6Collection::const_iterator l = leases.begin(); l != leases.end(); ++l) {
  1357. Option6IAAddrPtr iaaddr(new Option6IAAddr(D6O_IAADDR,
  1358. (*l)->addr_, (*l)->preferred_lft_, (*l)->valid_lft_));
  1359. ia_rsp->addOption(iaaddr);
  1360. // Now remove this address from the hints list.
  1361. AllocEngine::HintType tmp((*l)->addr_, 128);
  1362. ctx.hints_.erase(std::remove(ctx.hints_.begin(), ctx.hints_.end(), tmp),
  1363. ctx.hints_.end());
  1364. }
  1365. // For the leases that we just retired, send the addresses with 0 lifetimes.
  1366. for (Lease6Collection::const_iterator l = ctx.old_leases_.begin();
  1367. l != ctx.old_leases_.end(); ++l) {
  1368. Option6IAAddrPtr iaaddr(new Option6IAAddr(D6O_IAADDR,
  1369. (*l)->addr_, 0, 0));
  1370. ia_rsp->addOption(iaaddr);
  1371. // Now remove this address from the hints list.
  1372. AllocEngine::HintType tmp((*l)->addr_, 128);
  1373. ctx.hints_.erase(std::remove(ctx.hints_.begin(), ctx.hints_.end(), tmp),
  1374. ctx.hints_.end());
  1375. // If the new FQDN settings have changed for the lease, we need to
  1376. // delete any existing FQDN records for this lease.
  1377. if (((*l)->hostname_ != hostname) || ((*l)->fqdn_fwd_ != do_fwd) ||
  1378. ((*l)->fqdn_rev_ != do_rev)) {
  1379. LOG_DEBUG(dhcp6_logger, DBG_DHCP6_DETAIL,
  1380. DHCP6_DDNS_LEASE_RENEW_FQDN_CHANGE)
  1381. .arg((*l)->toText())
  1382. .arg(hostname)
  1383. .arg(do_rev ? "true" : "false")
  1384. .arg(do_fwd ? "true" : "false");
  1385. createRemovalNameChangeRequest(*l);
  1386. }
  1387. }
  1388. // Finally, if there are any addresses requested that we haven't dealt with
  1389. // already, inform the client that he can't have them.
  1390. for (AllocEngine::HintContainerConstIter hint = ctx.hints_.begin();
  1391. hint != ctx.hints_.end(); ++hint) {
  1392. Option6IAAddrPtr iaaddr(new Option6IAAddr(D6O_IAADDR,
  1393. hint->first, 0, 0));
  1394. ia_rsp->addOption(iaaddr);
  1395. }
  1396. // All is left is to insert the status code.
  1397. if (leases.empty()) {
  1398. // We did not assign anything. If client has sent something, then
  1399. // the status code is NoBinding, if he sent an empty IA_NA, then it's
  1400. // NoAddrsAvailable
  1401. if (hints_count) {
  1402. // Insert status code NoBinding to indicate that the lease does not
  1403. // exist for this client.
  1404. ia_rsp->addOption(createStatusCode(STATUS_NoBinding,
  1405. "Sorry, no known leases for this duid/iaid/subnet."));
  1406. LOG_DEBUG(dhcp6_logger, DBG_DHCP6_DETAIL, DHCP6_EXTEND_NA_UNKNOWN)
  1407. .arg(duid->toText())
  1408. .arg(ia->getIAID())
  1409. .arg(subnet->toText());
  1410. } else {
  1411. ia_rsp->addOption(createStatusCode(STATUS_NoAddrsAvail,
  1412. "Sorry, no addresses could be assigned at this time."));
  1413. }
  1414. } else {
  1415. // Yay, the client still has something. For now, let's not insert
  1416. // status-code=success to conserve bandwidth.
  1417. }
  1418. return (ia_rsp);
  1419. }
  1420. OptionPtr
  1421. Dhcpv6Srv::extendIA_PD(const Subnet6Ptr& subnet, const DuidPtr& duid,
  1422. const Pkt6Ptr& query, boost::shared_ptr<Option6IA> ia) {
  1423. // Let's create a IA_PD response and fill it in later
  1424. Option6IAPtr ia_rsp(new Option6IA(D6O_IA_PD, ia->getIAID()));
  1425. // If there is no subnet for the particular client, we can't retrieve
  1426. // information about client's leases from lease database. We treat this
  1427. // as no binding for the client.
  1428. if (!subnet) {
  1429. // Per RFC3633, section 12.2, if there is no binding and we are
  1430. // processing a Renew, the NoBinding status code should be returned.
  1431. if (query->getType() == DHCPV6_RENEW) {
  1432. // Insert status code NoBinding
  1433. ia_rsp->addOption(createStatusCode(STATUS_NoBinding,
  1434. "Sorry, no known PD leases"
  1435. " for this duid/iaid."));
  1436. LOG_DEBUG(dhcp6_logger, DBG_DHCP6_DETAIL,
  1437. DHCP6_EXTEND_PD_UNKNOWN_SUBNET)
  1438. .arg(duid->toText())
  1439. .arg(ia->getIAID());
  1440. return (ia_rsp);
  1441. // Per RFC3633, section 12.2, if there is no binding and we are
  1442. // processing Rebind, the message has to be discarded (assuming that
  1443. // the server doesn't know if the prefix in the IA_PD option is
  1444. // appropriate for the client's link). The exception being thrown
  1445. // here should propagate to the main loop and cause the message to
  1446. // be discarded.
  1447. } else {
  1448. /// @todo: RFC3315bis will probably change that behavior. Client
  1449. /// may rebind prefixes and addresses at the same time.
  1450. isc_throw(DHCPv6DiscardMessageError, "no subnet found for the"
  1451. " client sending Rebind to extend lifetime of the"
  1452. " prefix (DUID=" << duid->toText() << ", IAID="
  1453. << ia->getIAID() << ")");
  1454. }
  1455. }
  1456. // Set up T1, T2 timers
  1457. ia_rsp->setT1(subnet->getT1());
  1458. ia_rsp->setT2(subnet->getT2());
  1459. // Create client context for this renewal
  1460. static const IOAddress none("::");
  1461. AllocEngine::ClientContext6 ctx(subnet, duid, ia->getIAID(), none,
  1462. Lease::TYPE_PD, false, false, string(""),
  1463. false);
  1464. ctx.callout_handle_ = getCalloutHandle(query);
  1465. ctx.query_ = query;
  1466. ctx.ia_rsp_ = ia_rsp;
  1467. // Attempt to get MAC address using configured mechanisms.
  1468. // It's ok if there response is NULL. Hardware address is optional in Lease6.
  1469. ctx.hwaddr_ = getMAC(query);
  1470. // Extract prefixes that the client is trying to renew.
  1471. int hints_count = 0;
  1472. OptionCollection addrs = ia->getOptions();
  1473. for (OptionCollection::const_iterator it = addrs.begin();
  1474. it != addrs.end(); ++it) {
  1475. if (it->second->getType() != D6O_IAPREFIX) {
  1476. continue;
  1477. }
  1478. Option6IAPrefixPtr prf = boost::dynamic_pointer_cast<Option6IAPrefix>(it->second);
  1479. if (!prf) {
  1480. // That's weird. Option code was ok, but the object type was not.
  1481. continue;
  1482. }
  1483. // Put the client's prefix into the hints list.
  1484. ctx.hints_.push_back(make_pair(prf->getAddress(), prf->getLength()));
  1485. // We need to remember it as we'll be removing hints from this list as
  1486. // we extend, cancel or otherwise deal with the leases.
  1487. hints_count++;
  1488. }
  1489. /// @todo: The draft-ietf-dhc-dhcpv6-stateful-issues added a new capability
  1490. /// of the server to to assign new PD leases in both Renew and Rebind.
  1491. /// There's allow_new_leases_in_renewals_ in the ClientContext6, but we
  1492. /// currently not use it in PD yet. This should be implemented as part
  1493. /// of the stateful-issues implementation effort. See ticket #3718.
  1494. // Call Allocation Engine and attempt to renew leases. Number of things
  1495. // may happen. Leases may be extended, revoked (if the lease is no longer
  1496. // valid or reserved for someone else), or new leases may be added.
  1497. // Important parameters are:
  1498. // - returned container - current valid leases
  1499. // - old_leases - leases that used to be, but are no longer valid
  1500. // - changed_leases - leases that have FQDN changed (not really important
  1501. // in PD context)
  1502. Lease6Collection leases = alloc_engine_->renewLeases6(ctx);
  1503. // For all the leases we have now, add the IAPPREFIX with non-zero lifetimes
  1504. for (Lease6Collection::const_iterator l = leases.begin(); l != leases.end(); ++l) {
  1505. Option6IAPrefixPtr prf(new Option6IAPrefix(D6O_IAPREFIX,
  1506. (*l)->addr_, (*l)->prefixlen_,
  1507. (*l)->preferred_lft_, (*l)->valid_lft_));
  1508. ia_rsp->addOption(prf);
  1509. // Now remove this address from the hints list.
  1510. AllocEngine::HintType tmp((*l)->addr_, (*l)->prefixlen_);
  1511. ctx.hints_.erase(std::remove(ctx.hints_.begin(), ctx.hints_.end(), tmp),
  1512. ctx.hints_.end());
  1513. }
  1514. /// @todo: Maybe we should iterate over ctx.old_leases_, i.e. the leases
  1515. /// that used to be valid, but they are not anymore.
  1516. // For all the leases the client had requested, but we didn't assign, put them with
  1517. // zero lifetimes
  1518. // Finally, if there are any addresses requested that we haven't dealt with
  1519. // already, inform the client that he can't have them.
  1520. for (AllocEngine::HintContainerIter prefix = ctx.hints_.begin();
  1521. prefix != ctx.hints_.end(); ++prefix) {
  1522. OptionPtr prefix_opt(new Option6IAPrefix(D6O_IAPREFIX, prefix->first,
  1523. prefix->second, 0, 0));
  1524. ia_rsp->addOption(prefix_opt);
  1525. }
  1526. // All is left is to insert the status code.
  1527. if (leases.empty()) {
  1528. if (query->getType() == DHCPV6_RENEW) {
  1529. // We did not assign anything. If client has sent something, then
  1530. // the status code is NoBinding, if he sent an empty IA_NA, then it's
  1531. // NoAddrsAvailable
  1532. if (hints_count) {
  1533. // Insert status code NoBinding to indicate that the lease does not
  1534. // exist for this client.
  1535. ia_rsp->addOption(createStatusCode(STATUS_NoBinding,
  1536. "Sorry, no known PD leases for this duid/iaid/subnet."));
  1537. LOG_DEBUG(dhcp6_logger, DBG_DHCP6_DETAIL, DHCP6_EXTEND_PD_UNKNOWN_SUBNET)
  1538. .arg(duid->toText())
  1539. .arg(ia->getIAID())
  1540. .arg(subnet->toText());
  1541. } else {
  1542. LOG_DEBUG(dhcp6_logger, DBG_DHCP6_DETAIL, DHCP6_EXTEND_PD_NO_BINDING)
  1543. .arg(query->getName()).arg(duid->toText()).arg(ia->getIAID())
  1544. .arg(subnet->toText());
  1545. ia_rsp->addOption(createStatusCode(STATUS_NoPrefixAvail,
  1546. "Sorry, no prefixes could be assigned at this time."));
  1547. }
  1548. } else {
  1549. // Per RFC3633, section 12.2, if there is no binding and we are
  1550. // processing Rebind, the message has to be discarded (assuming that
  1551. // the server doesn't know if the prefix in the IA_PD option is
  1552. // appropriate for the client's link). The exception being thrown
  1553. // here should propagate to the main loop and cause the message to
  1554. // be discarded.
  1555. isc_throw(DHCPv6DiscardMessageError, "no binding found for the"
  1556. " DUID=" << duid->toText() << ", IAID="
  1557. << ia->getIAID() << ", subnet="
  1558. << subnet->toText() << " when processing a Rebind"
  1559. " message with IA_PD option");
  1560. }
  1561. }
  1562. return (ia_rsp);
  1563. }
  1564. void
  1565. Dhcpv6Srv::extendLeases(const Pkt6Ptr& query, Pkt6Ptr& reply) {
  1566. // We will try to extend lease lifetime for all IA options in the client's
  1567. // Renew or Rebind message.
  1568. /// @todo add support for IA_TA
  1569. // We need to select a subnet the client is connected in. This is needed
  1570. // to get the client's bindings from the lease database. The subnet id
  1571. // is one of the lease search parameters.
  1572. Subnet6Ptr subnet = selectSubnet(query);
  1573. if (!subnet) {
  1574. // This particular client is out of luck today. We do not have
  1575. // information about the subnet he is connected to. This likely means
  1576. // misconfiguration of the server (or some relays). We will continue to
  1577. // process this message, but our response will be almost useless: no
  1578. // addresses or prefixes, no subnet specific configuration etc. The only
  1579. // thing this client can get is some global information (like DNS
  1580. // servers).
  1581. LOG_WARN(dhcp6_logger, DHCP6_EXTEND_LEASE_SUBNET_SELECT_FAILED)
  1582. .arg(query->getName())
  1583. .arg(query->getRemoteAddr().toText())
  1584. .arg(query->getName());
  1585. } else {
  1586. LOG_DEBUG(dhcp6_logger, DBG_DHCP6_DETAIL_DATA,
  1587. DHCP6_EXTEND_LEASE_SUBNET_SELECTED)
  1588. .arg(subnet->toText());
  1589. }
  1590. // Let's find client's DUID. Client is supposed to include its client-id
  1591. // option almost all the time (the only exception is an anonymous
  1592. // inf-request, but that is mostly a theoretical case). Our allocation
  1593. // engine needs DUID and will refuse to allocate anything to anonymous
  1594. // clients.
  1595. /// @todo Consider removing this check from here and rely on what we have
  1596. /// checked on the earlier processing stage.
  1597. OptionPtr opt_duid = query->getOption(D6O_CLIENTID);
  1598. if (!opt_duid) {
  1599. // This should not happen. We have checked this before.
  1600. reply->addOption(createStatusCode(STATUS_UnspecFail,
  1601. "You did not include mandatory client-id"));
  1602. return;
  1603. }
  1604. DuidPtr duid(new DUID(opt_duid->getData()));
  1605. for (OptionCollection::iterator opt = query->options_.begin();
  1606. opt != query->options_.end(); ++opt) {
  1607. switch (opt->second->getType()) {
  1608. case D6O_IA_NA: {
  1609. OptionPtr answer_opt = extendIA_NA(subnet, duid, query, reply,
  1610. boost::dynamic_pointer_cast<
  1611. Option6IA>(opt->second));
  1612. if (answer_opt) {
  1613. reply->addOption(answer_opt);
  1614. }
  1615. break;
  1616. }
  1617. case D6O_IA_PD: {
  1618. OptionPtr answer_opt = extendIA_PD(subnet, duid, query,
  1619. boost::dynamic_pointer_cast<
  1620. Option6IA>(opt->second));
  1621. if (answer_opt) {
  1622. reply->addOption(answer_opt);
  1623. }
  1624. break;
  1625. }
  1626. default:
  1627. break;
  1628. }
  1629. }
  1630. }
  1631. void
  1632. Dhcpv6Srv::releaseLeases(const Pkt6Ptr& release, Pkt6Ptr& reply) {
  1633. // We need to release addresses for all IA_NA options in the client's
  1634. // RELEASE message.
  1635. // @todo Add support for IA_TA
  1636. // @todo Add support for IA_PD
  1637. // @todo Consider supporting more than one address in a single IA_NA.
  1638. // That was envisaged by RFC3315, but it never happened. The only
  1639. // software that supports that is Dibbler, but its author seriously doubts
  1640. // if anyone is really using it. Clients that want more than one address
  1641. // just include more instances of IA_NA options.
  1642. // Let's find client's DUID. Client is supposed to include its client-id
  1643. // option almost all the time (the only exception is an anonymous inf-request,
  1644. // but that is mostly a theoretical case). Our allocation engine needs DUID
  1645. // and will refuse to allocate anything to anonymous clients.
  1646. OptionPtr opt_duid = release->getOption(D6O_CLIENTID);
  1647. if (!opt_duid) {
  1648. // This should not happen. We have checked this before.
  1649. // see sanityCheck() called from processRelease()
  1650. LOG_WARN(dhcp6_logger, DHCP6_RELEASE_MISSING_CLIENTID)
  1651. .arg(release->getRemoteAddr().toText());
  1652. reply->addOption(createStatusCode(STATUS_UnspecFail,
  1653. "You did not include mandatory client-id"));
  1654. return;
  1655. }
  1656. DuidPtr duid(new DUID(opt_duid->getData()));
  1657. // Let's set the status to be success by default. We can override it with
  1658. // error status if needed. The important thing to understand here is that
  1659. // the global status code may be set to success only if all IA options were
  1660. // handled properly. Therefore the releaseIA_NA and releaseIA_PD options
  1661. // may turn the status code to some error, but can't turn it back to success.
  1662. int general_status = STATUS_Success;
  1663. for (OptionCollection::iterator opt = release->options_.begin();
  1664. opt != release->options_.end(); ++opt) {
  1665. switch (opt->second->getType()) {
  1666. case D6O_IA_NA: {
  1667. OptionPtr answer_opt = releaseIA_NA(duid, release, general_status,
  1668. boost::dynamic_pointer_cast<Option6IA>(opt->second));
  1669. if (answer_opt) {
  1670. reply->addOption(answer_opt);
  1671. }
  1672. break;
  1673. }
  1674. case D6O_IA_PD: {
  1675. OptionPtr answer_opt = releaseIA_PD(duid, release, general_status,
  1676. boost::dynamic_pointer_cast<Option6IA>(opt->second));
  1677. if (answer_opt) {
  1678. reply->addOption(answer_opt);
  1679. }
  1680. break;
  1681. }
  1682. // @todo: add support for IA_TA
  1683. default:
  1684. // remaining options are stateless and thus ignored in this context
  1685. ;
  1686. }
  1687. }
  1688. // To be pedantic, we should also include status code in the top-level
  1689. // scope, not just in each IA_NA. See RFC3315, section 18.2.6.
  1690. // This behavior will likely go away in RFC3315bis.
  1691. reply->addOption(createStatusCode(general_status,
  1692. "Summary status for all processed IA_NAs"));
  1693. }
  1694. OptionPtr
  1695. Dhcpv6Srv::releaseIA_NA(const DuidPtr& duid, const Pkt6Ptr& query,
  1696. int& general_status, boost::shared_ptr<Option6IA> ia) {
  1697. // Release can be done in one of two ways:
  1698. // Approach 1: extract address from client's IA_NA and see if it belongs
  1699. // to this particular client.
  1700. // Approach 2: find a subnet for this client, get a lease for
  1701. // this subnet/duid/iaid and check if its content matches to what the
  1702. // client is asking us to release.
  1703. //
  1704. // This method implements approach 1.
  1705. // That's our response
  1706. boost::shared_ptr<Option6IA> ia_rsp(new Option6IA(D6O_IA_NA, ia->getIAID()));
  1707. boost::shared_ptr<Option6IAAddr> release_addr = boost::dynamic_pointer_cast<Option6IAAddr>
  1708. (ia->getOption(D6O_IAADDR));
  1709. if (!release_addr) {
  1710. ia_rsp->addOption(createStatusCode(STATUS_NoBinding,
  1711. "You did not include an address in your RELEASE"));
  1712. general_status = STATUS_NoBinding;
  1713. return (ia_rsp);
  1714. }
  1715. Lease6Ptr lease = LeaseMgrFactory::instance().getLease6(Lease::TYPE_NA,
  1716. release_addr->getAddress());
  1717. if (!lease) {
  1718. // client releasing a lease that we don't know about.
  1719. // Insert status code NoAddrsAvail.
  1720. ia_rsp->addOption(createStatusCode(STATUS_NoBinding,
  1721. "Sorry, no known leases for this duid/iaid, can't release."));
  1722. general_status = STATUS_NoBinding;
  1723. LOG_INFO(dhcp6_logger, DHCP6_UNKNOWN_RELEASE_NA)
  1724. .arg(duid->toText())
  1725. .arg(ia->getIAID());
  1726. return (ia_rsp);
  1727. }
  1728. if (!lease->duid_) {
  1729. // Something is gravely wrong here. We do have a lease, but it does not
  1730. // have mandatory DUID information attached. Someone was messing with our
  1731. // database.
  1732. LOG_ERROR(dhcp6_logger, DHCP6_LEASE_NA_WITHOUT_DUID)
  1733. .arg(release_addr->getAddress().toText());
  1734. general_status = STATUS_UnspecFail;
  1735. ia_rsp->addOption(createStatusCode(STATUS_UnspecFail,
  1736. "Database consistency check failed when trying to RELEASE"));
  1737. return (ia_rsp);
  1738. }
  1739. if (*duid != *(lease->duid_)) {
  1740. // Sorry, it's not your address. You can't release it.
  1741. LOG_INFO(dhcp6_logger, DHCP6_RELEASE_NA_FAIL_WRONG_DUID)
  1742. .arg(duid->toText())
  1743. .arg(release_addr->getAddress().toText())
  1744. .arg(lease->duid_->toText());
  1745. general_status = STATUS_NoBinding;
  1746. ia_rsp->addOption(createStatusCode(STATUS_NoBinding,
  1747. "This address does not belong to you, you can't release it"));
  1748. return (ia_rsp);
  1749. }
  1750. if (ia->getIAID() != lease->iaid_) {
  1751. // This address belongs to this client, but to a different IA
  1752. LOG_WARN(dhcp6_logger, DHCP6_RELEASE_PD_FAIL_WRONG_IAID)
  1753. .arg(duid->toText())
  1754. .arg(release_addr->getAddress().toText())
  1755. .arg(lease->iaid_)
  1756. .arg(ia->getIAID());
  1757. ia_rsp->addOption(createStatusCode(STATUS_NoBinding,
  1758. "This is your address, but you used wrong IAID"));
  1759. general_status = STATUS_NoBinding;
  1760. return (ia_rsp);
  1761. }
  1762. // It is not necessary to check if the address matches as we used
  1763. // getLease6(addr) method that is supposed to return a proper lease.
  1764. bool skip = false;
  1765. // Execute all callouts registered for packet6_send
  1766. if (HooksManager::calloutsPresent(Hooks.hook_index_lease6_release_)) {
  1767. CalloutHandlePtr callout_handle = getCalloutHandle(query);
  1768. // Delete all previous arguments
  1769. callout_handle->deleteAllArguments();
  1770. // Pass the original packet
  1771. callout_handle->setArgument("query6", query);
  1772. // Pass the lease to be updated
  1773. callout_handle->setArgument("lease6", lease);
  1774. // Call all installed callouts
  1775. HooksManager::callCallouts(Hooks.hook_index_lease6_release_, *callout_handle);
  1776. // Callouts decided to skip the next processing step. The next
  1777. // processing step would to send the packet, so skip at this
  1778. // stage means "drop response".
  1779. if (callout_handle->getSkip()) {
  1780. skip = true;
  1781. LOG_DEBUG(dhcp6_logger, DBG_DHCP6_HOOKS, DHCP6_HOOK_LEASE6_RELEASE_NA_SKIP);
  1782. }
  1783. }
  1784. // Ok, we've passed all checks. Let's release this address.
  1785. bool success = false; // was the removal operation succeessful?
  1786. if (!skip) {
  1787. success = LeaseMgrFactory::instance().deleteLease(lease->addr_);
  1788. }
  1789. // Here the success should be true if we removed lease successfully
  1790. // and false if skip flag was set or the removal failed for whatever reason
  1791. if (!success) {
  1792. ia_rsp->addOption(createStatusCode(STATUS_UnspecFail,
  1793. "Server failed to release a lease"));
  1794. LOG_ERROR(dhcp6_logger, DHCP6_RELEASE_NA_FAIL)
  1795. .arg(lease->addr_.toText())
  1796. .arg(duid->toText())
  1797. .arg(lease->iaid_);
  1798. general_status = STATUS_UnspecFail;
  1799. return (ia_rsp);
  1800. } else {
  1801. LOG_DEBUG(dhcp6_logger, DBG_DHCP6_DETAIL, DHCP6_RELEASE_NA)
  1802. .arg(lease->addr_.toText())
  1803. .arg(duid->toText())
  1804. .arg(lease->iaid_);
  1805. ia_rsp->addOption(createStatusCode(STATUS_Success,
  1806. "Lease released. Thank you, please come again."));
  1807. // Check if a lease has flags indicating that the FQDN update has
  1808. // been performed. If so, create NameChangeRequest which removes
  1809. // the entries.
  1810. createRemovalNameChangeRequest(lease);
  1811. return (ia_rsp);
  1812. }
  1813. }
  1814. OptionPtr
  1815. Dhcpv6Srv::releaseIA_PD(const DuidPtr& duid, const Pkt6Ptr& query,
  1816. int& general_status, boost::shared_ptr<Option6IA> ia) {
  1817. // Release can be done in one of two ways:
  1818. // Approach 1: extract address from client's IA_NA and see if it belongs
  1819. // to this particular client.
  1820. // Approach 2: find a subnet for this client, get a lease for
  1821. // this subnet/duid/iaid and check if its content matches to what the
  1822. // client is asking us to release.
  1823. //
  1824. // This method implements approach 1.
  1825. // That's our response. We will fill it in as we check the lease to be
  1826. // released.
  1827. boost::shared_ptr<Option6IA> ia_rsp(new Option6IA(D6O_IA_PD, ia->getIAID()));
  1828. boost::shared_ptr<Option6IAPrefix> release_prefix =
  1829. boost::dynamic_pointer_cast<Option6IAPrefix>(ia->getOption(D6O_IAPREFIX));
  1830. if (!release_prefix) {
  1831. ia_rsp->addOption(createStatusCode(STATUS_NoBinding,
  1832. "You did not include a prefix in your RELEASE"));
  1833. general_status = STATUS_NoBinding;
  1834. return (ia_rsp);
  1835. }
  1836. Lease6Ptr lease = LeaseMgrFactory::instance().getLease6(Lease::TYPE_PD,
  1837. release_prefix->getAddress());
  1838. if (!lease) {
  1839. // Client releasing a lease that we don't know about.
  1840. // Insert status code NoBinding.
  1841. ia_rsp->addOption(createStatusCode(STATUS_NoBinding,
  1842. "Sorry, no known leases for this duid/iaid, can't release."));
  1843. general_status = STATUS_NoBinding;
  1844. LOG_INFO(dhcp6_logger, DHCP6_UNKNOWN_RELEASE_PD)
  1845. .arg(duid->toText())
  1846. .arg(ia->getIAID());
  1847. return (ia_rsp);
  1848. }
  1849. if (!lease->duid_) {
  1850. // Something is gravely wrong here. We do have a lease, but it does not
  1851. // have mandatory DUID information attached. Someone was messing with our
  1852. // database.
  1853. LOG_ERROR(dhcp6_logger, DHCP6_LEASE_PD_WITHOUT_DUID)
  1854. .arg(release_prefix->getAddress().toText());
  1855. general_status = STATUS_UnspecFail;
  1856. ia_rsp->addOption(createStatusCode(STATUS_UnspecFail,
  1857. "Database consistency check failed when trying to RELEASE"));
  1858. return (ia_rsp);
  1859. }
  1860. if (*duid != *(lease->duid_)) {
  1861. // Sorry, it's not your address. You can't release it.
  1862. LOG_INFO(dhcp6_logger, DHCP6_RELEASE_PD_FAIL_WRONG_DUID)
  1863. .arg(duid->toText())
  1864. .arg(release_prefix->getAddress().toText())
  1865. .arg(lease->duid_->toText());
  1866. general_status = STATUS_NoBinding;
  1867. ia_rsp->addOption(createStatusCode(STATUS_NoBinding,
  1868. "This address does not belong to you, you can't release it"));
  1869. return (ia_rsp);
  1870. }
  1871. if (ia->getIAID() != lease->iaid_) {
  1872. // This address belongs to this client, but to a different IA
  1873. LOG_WARN(dhcp6_logger, DHCP6_RELEASE_PD_FAIL_WRONG_IAID)
  1874. .arg(duid->toText())
  1875. .arg(release_prefix->getAddress().toText())
  1876. .arg(lease->iaid_)
  1877. .arg(ia->getIAID());
  1878. ia_rsp->addOption(createStatusCode(STATUS_NoBinding,
  1879. "This is your address, but you used wrong IAID"));
  1880. general_status = STATUS_NoBinding;
  1881. return (ia_rsp);
  1882. }
  1883. // It is not necessary to check if the address matches as we used
  1884. // getLease6(addr) method that is supposed to return a proper lease.
  1885. bool skip = false;
  1886. // Execute all callouts registered for packet6_send
  1887. if (HooksManager::calloutsPresent(Hooks.hook_index_lease6_release_)) {
  1888. CalloutHandlePtr callout_handle = getCalloutHandle(query);
  1889. // Delete all previous arguments
  1890. callout_handle->deleteAllArguments();
  1891. // Pass the original packet
  1892. callout_handle->setArgument("query6", query);
  1893. // Pass the lease to be updated
  1894. callout_handle->setArgument("lease6", lease);
  1895. // Call all installed callouts
  1896. HooksManager::callCallouts(Hooks.hook_index_lease6_release_, *callout_handle);
  1897. skip = callout_handle->getSkip();
  1898. }
  1899. // Ok, we've passed all checks. Let's release this prefix.
  1900. bool success = false; // was the removal operation succeessful?
  1901. if (!skip) {
  1902. success = LeaseMgrFactory::instance().deleteLease(lease->addr_);
  1903. } else {
  1904. // Callouts decided to skip the next processing step. The next
  1905. // processing step would to send the packet, so skip at this
  1906. // stage means "drop response".
  1907. LOG_DEBUG(dhcp6_logger, DBG_DHCP6_HOOKS, DHCP6_HOOK_LEASE6_RELEASE_PD_SKIP);
  1908. }
  1909. // Here the success should be true if we removed lease successfully
  1910. // and false if skip flag was set or the removal failed for whatever reason
  1911. if (!success) {
  1912. ia_rsp->addOption(createStatusCode(STATUS_UnspecFail,
  1913. "Server failed to release a lease"));
  1914. LOG_ERROR(dhcp6_logger, DHCP6_RELEASE_PD_FAIL)
  1915. .arg(lease->addr_.toText())
  1916. .arg(duid->toText())
  1917. .arg(lease->iaid_);
  1918. general_status = STATUS_UnspecFail;
  1919. } else {
  1920. LOG_DEBUG(dhcp6_logger, DBG_DHCP6_DETAIL, DHCP6_RELEASE_PD)
  1921. .arg(lease->addr_.toText())
  1922. .arg(duid->toText())
  1923. .arg(lease->iaid_);
  1924. ia_rsp->addOption(createStatusCode(STATUS_Success,
  1925. "Lease released. Thank you, please come again."));
  1926. }
  1927. return (ia_rsp);
  1928. }
  1929. Pkt6Ptr
  1930. Dhcpv6Srv::processSolicit(const Pkt6Ptr& solicit) {
  1931. sanityCheck(solicit, MANDATORY, FORBIDDEN);
  1932. Pkt6Ptr advertise(new Pkt6(DHCPV6_ADVERTISE, solicit->getTransid()));
  1933. copyClientOptions(solicit, advertise);
  1934. appendDefaultOptions(solicit, advertise);
  1935. appendRequestedOptions(solicit, advertise);
  1936. appendRequestedVendorOptions(solicit, advertise);
  1937. processClientFqdn(solicit, advertise);
  1938. assignLeases(solicit, advertise);
  1939. // Note, that we don't create NameChangeRequests here because we don't
  1940. // perform DNS Updates for Solicit. Client must send Request to update
  1941. // DNS.
  1942. generateFqdn(advertise);
  1943. return (advertise);
  1944. }
  1945. Pkt6Ptr
  1946. Dhcpv6Srv::processRequest(const Pkt6Ptr& request) {
  1947. sanityCheck(request, MANDATORY, MANDATORY);
  1948. Pkt6Ptr reply(new Pkt6(DHCPV6_REPLY, request->getTransid()));
  1949. copyClientOptions(request, reply);
  1950. appendDefaultOptions(request, reply);
  1951. appendRequestedOptions(request, reply);
  1952. appendRequestedVendorOptions(request, reply);
  1953. processClientFqdn(request, reply);
  1954. assignLeases(request, reply);
  1955. generateFqdn(reply);
  1956. createNameChangeRequests(reply);
  1957. return (reply);
  1958. }
  1959. Pkt6Ptr
  1960. Dhcpv6Srv::processRenew(const Pkt6Ptr& renew) {
  1961. sanityCheck(renew, MANDATORY, MANDATORY);
  1962. Pkt6Ptr reply(new Pkt6(DHCPV6_REPLY, renew->getTransid()));
  1963. copyClientOptions(renew, reply);
  1964. appendDefaultOptions(renew, reply);
  1965. appendRequestedOptions(renew, reply);
  1966. processClientFqdn(renew, reply);
  1967. extendLeases(renew, reply);
  1968. generateFqdn(reply);
  1969. createNameChangeRequests(reply);
  1970. return (reply);
  1971. }
  1972. Pkt6Ptr
  1973. Dhcpv6Srv::processRebind(const Pkt6Ptr& rebind) {
  1974. Pkt6Ptr reply(new Pkt6(DHCPV6_REPLY, rebind->getTransid()));
  1975. copyClientOptions(rebind, reply);
  1976. appendDefaultOptions(rebind, reply);
  1977. appendRequestedOptions(rebind, reply);
  1978. processClientFqdn(rebind, reply);
  1979. extendLeases(rebind, reply);
  1980. generateFqdn(reply);
  1981. createNameChangeRequests(rebind);
  1982. return (reply);
  1983. }
  1984. Pkt6Ptr
  1985. Dhcpv6Srv::processConfirm(const Pkt6Ptr& confirm) {
  1986. // Get IA_NAs from the Confirm. If there are none, the message is
  1987. // invalid and must be discarded. There is nothing more to do.
  1988. OptionCollection ias = confirm->getOptions(D6O_IA_NA);
  1989. if (ias.empty()) {
  1990. return (Pkt6Ptr());
  1991. }
  1992. // The server sends Reply message in response to Confirm.
  1993. Pkt6Ptr reply(new Pkt6(DHCPV6_REPLY, confirm->getTransid()));
  1994. // Make sure that the necessary options are included.
  1995. copyClientOptions(confirm, reply);
  1996. appendDefaultOptions(confirm, reply);
  1997. // Indicates if at least one address has been verified. If no addresses
  1998. // are verified it means that the client has sent no IA_NA options
  1999. // or no IAAddr options and that client's message has to be discarded.
  2000. bool verified = false;
  2001. // Check if subnet can be selected for the message. If no subnet
  2002. // has been selected, the client is not on link.
  2003. SubnetPtr subnet = selectSubnet(confirm);
  2004. // Regardless if the subnet has been selected or not, we will iterate
  2005. // over the IA_NA options to check if they hold any addresses. If there
  2006. // are no, the Confirm is discarded.
  2007. // Check addresses in IA_NA options and make sure they are appropriate.
  2008. for (OptionCollection::const_iterator ia = ias.begin();
  2009. ia != ias.end(); ++ia) {
  2010. const OptionCollection& opts = ia->second->getOptions();
  2011. for (OptionCollection::const_iterator opt = opts.begin();
  2012. opt != opts.end(); ++opt) {
  2013. // Ignore options other than IAAddr.
  2014. if (opt->second->getType() == D6O_IAADDR) {
  2015. // Check that the address is in range in the subnet selected.
  2016. Option6IAAddrPtr iaaddr = boost::dynamic_pointer_cast<
  2017. Option6IAAddr>(opt->second);
  2018. // If there is subnet selected and the address has been included
  2019. // in IA_NA, mark it verified and verify that it belongs to the
  2020. // subnet.
  2021. if (iaaddr) {
  2022. // If at least one address is not in range, then return
  2023. // the NotOnLink status code.
  2024. if (subnet && !subnet->inRange(iaaddr->getAddress())) {
  2025. std::ostringstream status_msg;
  2026. status_msg << "Address " << iaaddr->getAddress()
  2027. << " is not on link.";
  2028. reply->addOption(createStatusCode(STATUS_NotOnLink,
  2029. status_msg.str()));
  2030. return (reply);
  2031. }
  2032. verified = true;
  2033. } else {
  2034. isc_throw(Unexpected, "failed to cast the IA Address option"
  2035. " to the Option6IAAddrPtr. This is programming"
  2036. " error and should be reported");
  2037. }
  2038. }
  2039. }
  2040. }
  2041. // It seems that the client hasn't included any addresses in which case
  2042. // the Confirm must be discarded.
  2043. if (!verified) {
  2044. return (Pkt6Ptr());
  2045. }
  2046. // If there is a subnet, there were addresses in IA_NA options and the
  2047. // addresses where consistent with the subnet then the client is on link.
  2048. if (subnet) {
  2049. // All addresses in range, so return success.
  2050. reply->addOption(createStatusCode(STATUS_Success,
  2051. "All addresses are on-link"));
  2052. } else {
  2053. reply->addOption(createStatusCode(STATUS_NotOnLink,
  2054. "No subnet selected"));
  2055. }
  2056. return (reply);
  2057. }
  2058. Pkt6Ptr
  2059. Dhcpv6Srv::processRelease(const Pkt6Ptr& release) {
  2060. sanityCheck(release, MANDATORY, MANDATORY);
  2061. Pkt6Ptr reply(new Pkt6(DHCPV6_REPLY, release->getTransid()));
  2062. copyClientOptions(release, reply);
  2063. appendDefaultOptions(release, reply);
  2064. releaseLeases(release, reply);
  2065. // @todo If client sent a release and we should remove outstanding
  2066. // DNS records.
  2067. return (reply);
  2068. }
  2069. Pkt6Ptr
  2070. Dhcpv6Srv::processDecline(const Pkt6Ptr& decline) {
  2071. /// @todo: Implement this
  2072. Pkt6Ptr reply(new Pkt6(DHCPV6_REPLY, decline->getTransid()));
  2073. return (reply);
  2074. }
  2075. Pkt6Ptr
  2076. Dhcpv6Srv::processInfRequest(const Pkt6Ptr& infRequest) {
  2077. // Create a Reply packet, with the same trans-id as the client's.
  2078. Pkt6Ptr reply(new Pkt6(DHCPV6_REPLY, infRequest->getTransid()));
  2079. // Copy client options (client-id, also relay information if present)
  2080. copyClientOptions(infRequest, reply);
  2081. // Append default options, i.e. options that the server is supposed
  2082. // to put in all messages it sends (server-id for now, but possibly other
  2083. // options once we start supporting authentication)
  2084. appendDefaultOptions(infRequest, reply);
  2085. // Try to assign options that were requested by the client.
  2086. appendRequestedOptions(infRequest, reply);
  2087. return (reply);
  2088. }
  2089. size_t
  2090. Dhcpv6Srv::unpackOptions(const OptionBuffer& buf,
  2091. const std::string& option_space,
  2092. isc::dhcp::OptionCollection& options,
  2093. size_t* relay_msg_offset,
  2094. size_t* relay_msg_len) {
  2095. size_t offset = 0;
  2096. size_t length = buf.size();
  2097. OptionDefContainer option_defs;
  2098. if (option_space == "dhcp6") {
  2099. // Get the list of stdandard option definitions.
  2100. option_defs = LibDHCP::getOptionDefs(Option::V6);
  2101. } else if (!option_space.empty()) {
  2102. OptionDefContainerPtr option_defs_ptr =
  2103. CfgMgr::instance().getCurrentCfg()->getCfgOptionDef()->
  2104. getAll(option_space);
  2105. if (option_defs_ptr != NULL) {
  2106. option_defs = *option_defs_ptr;
  2107. }
  2108. }
  2109. // Get the search index #1. It allows to search for option definitions
  2110. // using option code.
  2111. const OptionDefContainerTypeIndex& idx = option_defs.get<1>();
  2112. // The buffer being read comprises a set of options, each starting with
  2113. // a two-byte type code and a two-byte length field.
  2114. while (offset + 4 <= length) {
  2115. // At this point, from the while condition, we know that there
  2116. // are at least 4 bytes available following offset in the
  2117. // buffer.
  2118. uint16_t opt_type = isc::util::readUint16(&buf[offset], 2);
  2119. offset += 2;
  2120. uint16_t opt_len = isc::util::readUint16(&buf[offset], 2);
  2121. offset += 2;
  2122. if (offset + opt_len > length) {
  2123. // @todo: consider throwing exception here.
  2124. // We peeked at the option header of the next option, but discovered
  2125. // that it would end up beyond buffer end, so the option is
  2126. // truncated. Hence we can't parse it. Therefore we revert
  2127. // by by those four bytes (as if we never parsed them).
  2128. return (offset - 4);
  2129. }
  2130. if (opt_type == D6O_RELAY_MSG && relay_msg_offset && relay_msg_len) {
  2131. // remember offset of the beginning of the relay-msg option
  2132. *relay_msg_offset = offset;
  2133. *relay_msg_len = opt_len;
  2134. // do not create that relay-msg option
  2135. offset += opt_len;
  2136. continue;
  2137. }
  2138. // Get all definitions with the particular option code. Note that option
  2139. // code is non-unique within this container however at this point we
  2140. // expect to get one option definition with the particular code. If more
  2141. // are returned we report an error.
  2142. const OptionDefContainerTypeRange& range = idx.equal_range(opt_type);
  2143. // Get the number of returned option definitions for the option code.
  2144. size_t num_defs = distance(range.first, range.second);
  2145. OptionPtr opt;
  2146. if (num_defs > 1) {
  2147. // Multiple options of the same code are not supported right now!
  2148. isc_throw(isc::Unexpected, "Internal error: multiple option definitions"
  2149. " for option type " << opt_type << " returned. Currently it is not"
  2150. " supported to initialize multiple option definitions"
  2151. " for the same option code. This will be supported once"
  2152. " support for option spaces is implemented");
  2153. } else if (num_defs == 0) {
  2154. // @todo Don't crash if definition does not exist because only a few
  2155. // option definitions are initialized right now. In the future
  2156. // we will initialize definitions for all options and we will
  2157. // remove this elseif. For now, return generic option.
  2158. opt = OptionPtr(new Option(Option::V6, opt_type,
  2159. buf.begin() + offset,
  2160. buf.begin() + offset + opt_len));
  2161. opt->setEncapsulatedSpace("dhcp6");
  2162. } else {
  2163. // The option definition has been found. Use it to create
  2164. // the option instance from the provided buffer chunk.
  2165. const OptionDefinitionPtr& def = *(range.first);
  2166. assert(def);
  2167. opt = def->optionFactory(Option::V6, opt_type,
  2168. buf.begin() + offset,
  2169. buf.begin() + offset + opt_len,
  2170. boost::bind(&Dhcpv6Srv::unpackOptions, this, _1, _2,
  2171. _3, _4, _5));
  2172. }
  2173. // add option to options
  2174. options.insert(std::make_pair(opt_type, opt));
  2175. offset += opt_len;
  2176. }
  2177. return (offset);
  2178. }
  2179. void
  2180. Dhcpv6Srv::ifaceMgrSocket6ErrorHandler(const std::string& errmsg) {
  2181. // Log the reason for socket opening failure and return.
  2182. LOG_WARN(dhcp6_logger, DHCP6_OPEN_SOCKET_FAIL).arg(errmsg);
  2183. }
  2184. void Dhcpv6Srv::classifyPacket(const Pkt6Ptr& pkt) {
  2185. OptionVendorClassPtr vclass = boost::dynamic_pointer_cast<
  2186. OptionVendorClass>(pkt->getOption(D6O_VENDOR_CLASS));
  2187. if (!vclass || vclass->getTuplesNum() == 0) {
  2188. return;
  2189. }
  2190. std::ostringstream classes;
  2191. if (vclass->hasTuple(DOCSIS3_CLASS_MODEM)) {
  2192. classes << VENDOR_CLASS_PREFIX << DOCSIS3_CLASS_MODEM;
  2193. } else if (vclass->hasTuple(DOCSIS3_CLASS_EROUTER)) {
  2194. classes << VENDOR_CLASS_PREFIX << DOCSIS3_CLASS_EROUTER;
  2195. } else {
  2196. classes << VENDOR_CLASS_PREFIX << vclass->getTuple(0).getText();
  2197. }
  2198. // If there is no class identified, leave.
  2199. if (!classes.str().empty()) {
  2200. pkt->addClass(classes.str());
  2201. LOG_DEBUG(dhcp6_logger, DBG_DHCP6_BASIC, DHCP6_CLASS_ASSIGNED)
  2202. .arg(classes.str());
  2203. }
  2204. }
  2205. void
  2206. Dhcpv6Srv::generateFqdn(const Pkt6Ptr& answer) {
  2207. if (!answer) {
  2208. isc_throw(isc::Unexpected, "an instance of the object encapsulating"
  2209. " a message must not be NULL when generating FQDN");
  2210. }
  2211. // It is likely that client haven't included the FQDN option. In such case,
  2212. // FQDN option will be NULL. Also, there is nothing to do if the option
  2213. // is present and conveys the non-empty FQDN.
  2214. Option6ClientFqdnPtr fqdn = boost::dynamic_pointer_cast<
  2215. Option6ClientFqdn>(answer->getOption(D6O_CLIENT_FQDN));
  2216. if (!fqdn || !fqdn->getDomainName().empty()) {
  2217. return;
  2218. }
  2219. // Get the first IA_NA acquired for the client.
  2220. OptionPtr ia = answer->getOption(D6O_IA_NA);
  2221. if (!ia) {
  2222. return;
  2223. }
  2224. // If it has any IAAddr, use the first one to generate unique FQDN.
  2225. Option6IAAddrPtr iaaddr = boost::dynamic_pointer_cast<
  2226. Option6IAAddr>(ia->getOption(D6O_IAADDR));
  2227. if (!iaaddr) {
  2228. return;
  2229. }
  2230. // Get the IPv6 address acquired by the client.
  2231. IOAddress addr = iaaddr->getAddress();
  2232. std::string generated_name =
  2233. CfgMgr::instance().getD2ClientMgr().generateFqdn(addr);
  2234. try {
  2235. // The lease has been acquired but the FQDN for this lease hasn't
  2236. // been updated in the lease database. We now have new FQDN
  2237. // generated, so the lease database has to be updated here.
  2238. // However, never update lease database for Advertise, just send
  2239. // our notion of client's FQDN in the Client FQDN option.
  2240. if (answer->getType() != DHCPV6_ADVERTISE) {
  2241. Lease6Ptr lease =
  2242. LeaseMgrFactory::instance().getLease6(Lease::TYPE_NA, addr);
  2243. if (lease) {
  2244. lease->hostname_ = generated_name;
  2245. LeaseMgrFactory::instance().updateLease6(lease);
  2246. } else {
  2247. isc_throw(isc::Unexpected, "there is no lease in the database "
  2248. " for address " << addr << ", so as it is impossible"
  2249. " to update FQDN data. This is a programmatic error"
  2250. " as the given address is now being handed to the"
  2251. " client");
  2252. }
  2253. }
  2254. // Set the generated FQDN in the Client FQDN option.
  2255. fqdn->setDomainName(generated_name, Option6ClientFqdn::FULL);
  2256. } catch (const Exception& ex) {
  2257. LOG_ERROR(dhcp6_logger, DHCP6_NAME_GEN_UPDATE_FAIL)
  2258. .arg(addr.toText())
  2259. .arg(ex.what());
  2260. }
  2261. }
  2262. void
  2263. Dhcpv6Srv::startD2() {
  2264. D2ClientMgr& d2_mgr = CfgMgr::instance().getD2ClientMgr();
  2265. if (d2_mgr.ddnsEnabled()) {
  2266. // Updates are enabled, so lets start the sender, passing in
  2267. // our error handler.
  2268. // This may throw so wherever this is called needs to ready.
  2269. d2_mgr.startSender(boost::bind(&Dhcpv6Srv::d2ClientErrorHandler,
  2270. this, _1, _2));
  2271. }
  2272. }
  2273. void
  2274. Dhcpv6Srv::d2ClientErrorHandler(const
  2275. dhcp_ddns::NameChangeSender::Result result,
  2276. dhcp_ddns::NameChangeRequestPtr& ncr) {
  2277. LOG_ERROR(dhcp6_logger, DHCP6_DDNS_REQUEST_SEND_FAILED).
  2278. arg(result).arg((ncr ? ncr->toText() : " NULL "));
  2279. // We cannot communicate with kea-dhcp-ddns, suspend futher updates.
  2280. /// @todo We may wish to revisit this, but for now we will simpy turn
  2281. /// them off.
  2282. CfgMgr::instance().getD2ClientMgr().suspendUpdates();
  2283. }
  2284. std::string
  2285. Daemon::getVersion(bool extended) {
  2286. std::stringstream tmp;
  2287. tmp << VERSION;
  2288. if (extended) {
  2289. tmp << endl << EXTENDED_VERSION;
  2290. // @todo print more details (is it Botan or OpenSSL build,
  2291. // with or without MySQL/Postgres? What compilation options were
  2292. // used? etc)
  2293. }
  2294. return (tmp.str());
  2295. }
  2296. };
  2297. };