dhcp6_srv.cc 126 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249
  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/option6_status_code.h>
  28. #include <dhcp/option_custom.h>
  29. #include <dhcp/option_vendor.h>
  30. #include <dhcp/option_vendor_class.h>
  31. #include <dhcp/option_int_array.h>
  32. #include <dhcp/pkt6.h>
  33. #include <dhcp6/dhcp6_log.h>
  34. #include <dhcp6/dhcp6_srv.h>
  35. #include <dhcpsrv/callout_handle_store.h>
  36. #include <dhcpsrv/cfgmgr.h>
  37. #include <dhcpsrv/lease_mgr.h>
  38. #include <dhcpsrv/lease_mgr_factory.h>
  39. #include <dhcpsrv/ncr_generator.h>
  40. #include <dhcpsrv/subnet.h>
  41. #include <dhcpsrv/subnet_selector.h>
  42. #include <dhcpsrv/utils.h>
  43. #include <eval/evaluate.h>
  44. #include <eval/eval_messages.h>
  45. #include <exceptions/exceptions.h>
  46. #include <hooks/callout_handle.h>
  47. #include <hooks/hooks_log.h>
  48. #include <hooks/hooks_manager.h>
  49. #include <stats/stats_mgr.h>
  50. #include <util/encode/hex.h>
  51. #include <util/io_utilities.h>
  52. #include <util/range_utilities.h>
  53. #include <log/logger.h>
  54. #include <cryptolink/cryptolink.h>
  55. #include <cfgrpt/config_report.h>
  56. #ifdef HAVE_MYSQL
  57. #include <dhcpsrv/mysql_lease_mgr.h>
  58. #endif
  59. #ifdef HAVE_PGSQL
  60. #include <dhcpsrv/pgsql_lease_mgr.h>
  61. #endif
  62. #include <dhcpsrv/memfile_lease_mgr.h>
  63. #include <boost/asio.hpp>
  64. #include <boost/bind.hpp>
  65. #include <boost/foreach.hpp>
  66. #include <boost/tokenizer.hpp>
  67. #include <boost/algorithm/string/erase.hpp>
  68. #include <stdlib.h>
  69. #include <time.h>
  70. #include <iomanip>
  71. #include <fstream>
  72. #include <sstream>
  73. using namespace isc;
  74. using namespace isc::asiolink;
  75. using namespace isc::cryptolink;
  76. using namespace isc::dhcp;
  77. using namespace isc::dhcp_ddns;
  78. using namespace isc::hooks;
  79. using namespace isc::log;
  80. using namespace isc::stats;
  81. using namespace isc::util;
  82. using namespace std;
  83. namespace {
  84. /// Structure that holds registered hook indexes
  85. struct Dhcp6Hooks {
  86. int hook_index_buffer6_receive_;///< index for "buffer6_receive" hook point
  87. int hook_index_pkt6_receive_; ///< index for "pkt6_receive" hook point
  88. int hook_index_subnet6_select_; ///< index for "subnet6_select" hook point
  89. int hook_index_lease6_release_; ///< index for "lease6_release" hook point
  90. int hook_index_pkt6_send_; ///< index for "pkt6_send" hook point
  91. int hook_index_buffer6_send_; ///< index for "buffer6_send" hook point
  92. int hook_index_lease6_decline_; ///< index for "lease6_decline" hook point
  93. /// Constructor that registers hook points for DHCPv6 engine
  94. Dhcp6Hooks() {
  95. hook_index_buffer6_receive_= HooksManager::registerHook("buffer6_receive");
  96. hook_index_pkt6_receive_ = HooksManager::registerHook("pkt6_receive");
  97. hook_index_subnet6_select_ = HooksManager::registerHook("subnet6_select");
  98. hook_index_lease6_release_ = HooksManager::registerHook("lease6_release");
  99. hook_index_pkt6_send_ = HooksManager::registerHook("pkt6_send");
  100. hook_index_buffer6_send_ = HooksManager::registerHook("buffer6_send");
  101. hook_index_lease6_decline_ = HooksManager::registerHook("lease6_decline");
  102. }
  103. };
  104. // Declare a Hooks object. As this is outside any function or method, it
  105. // will be instantiated (and the constructor run) when the module is loaded.
  106. // As a result, the hook indexes will be defined before any method in this
  107. // module is called.
  108. Dhcp6Hooks Hooks;
  109. /// @brief Creates instance of the Status Code option.
  110. ///
  111. /// This variant of the function is used when the Status Code option
  112. /// is added as a top-level option. It logs the debug message and
  113. /// includes the information about the client and transaction.
  114. ///
  115. /// @param pkt Reference to the client's message.
  116. /// @param status_code Numeric status code.
  117. /// @param status_message Status message.
  118. ///
  119. /// @return Pointer to the Status Code option.
  120. OptionPtr
  121. createStatusCode(const Pkt6& pkt, const uint16_t status_code,
  122. const std::string& status_message) {
  123. Option6StatusCodePtr option_status(new Option6StatusCode(status_code,
  124. status_message));
  125. LOG_DEBUG(options6_logger, DBG_DHCP6_DETAIL, DHCP6_ADD_GLOBAL_STATUS_CODE)
  126. .arg(pkt.getLabel())
  127. .arg(option_status->dataToText());
  128. return (option_status);
  129. }
  130. /// @brief Creates instance of the Status Code option.
  131. ///
  132. /// This variant of the function is used when the Status Code option
  133. /// is added to one of the IA options. It logs the debug message and
  134. /// includes the information about the client and transaction as well
  135. /// as IAID of the IA option.
  136. ///
  137. /// @param pkt Reference to the client's message.
  138. /// param ia Reference to the IA option to which the Status Code is
  139. /// being added.
  140. /// @param status_code Numeric status code.
  141. /// @param status_message Status message.
  142. ///
  143. /// @return Pointer to the Status Code option.
  144. OptionPtr
  145. createStatusCode(const Pkt6& pkt, const Option6IA& ia, const uint16_t status_code,
  146. const std::string& status_message) {
  147. Option6StatusCodePtr option_status(new Option6StatusCode(status_code,
  148. status_message));
  149. LOG_DEBUG(options6_logger, DBG_DHCP6_DETAIL, DHCP6_ADD_STATUS_CODE_FOR_IA)
  150. .arg(pkt.getLabel())
  151. .arg(ia.getIAID())
  152. .arg(option_status->dataToText());
  153. return (option_status);
  154. }
  155. }; // anonymous namespace
  156. namespace isc {
  157. namespace dhcp {
  158. const std::string Dhcpv6Srv::VENDOR_CLASS_PREFIX("VENDOR_CLASS_");
  159. /// @brief file name of a server-id file
  160. ///
  161. /// Server must store its duid in persistent storage that must not change
  162. /// between restarts. This is name of the file that is created in dataDir
  163. /// (see isc::dhcp::CfgMgr::getDataDir()). It is a text file that uses
  164. /// double digit hex values separated by colons format, e.g.
  165. /// 01:ff:02:03:06:80:90:ab:cd:ef. Server will create it during first
  166. /// run and then use it afterwards.
  167. static const char* SERVER_DUID_FILE = "kea-dhcp6-serverid";
  168. Dhcpv6Srv::Dhcpv6Srv(uint16_t port)
  169. : serverid_(), port_(port), shutdown_(true), alloc_engine_()
  170. {
  171. LOG_DEBUG(dhcp6_logger, DBG_DHCP6_START, DHCP6_OPEN_SOCKET).arg(port);
  172. // Initialize objects required for DHCP server operation.
  173. try {
  174. // Port 0 is used for testing purposes where in most cases we don't
  175. // rely on the physical interfaces. Therefore, it should be possible
  176. // to create an object even when there are no usable interfaces.
  177. if ((port > 0) && (IfaceMgr::instance().countIfaces() == 0)) {
  178. LOG_ERROR(dhcp6_logger, DHCP6_NO_INTERFACES);
  179. return;
  180. }
  181. string duid_file = CfgMgr::instance().getDataDir() + "/" + string(SERVER_DUID_FILE);
  182. if (loadServerID(duid_file)) {
  183. LOG_DEBUG(dhcp6_logger, DBG_DHCP6_START, DHCP6_SERVERID_LOADED)
  184. .arg(duidToString(getServerID()))
  185. .arg(duid_file);
  186. } else {
  187. generateServerID();
  188. LOG_INFO(dhcp6_logger, DHCP6_SERVERID_GENERATED)
  189. .arg(duidToString(getServerID()))
  190. .arg(duid_file);
  191. if (!writeServerID(duid_file)) {
  192. LOG_WARN(dhcp6_logger, DHCP6_SERVERID_WRITE_FAIL)
  193. .arg(duid_file);
  194. }
  195. }
  196. // Instantiate allocation engine. The number of allocation attempts equal
  197. // to zero indicates that the allocation engine will use the number of
  198. // attempts depending on the pool size.
  199. alloc_engine_.reset(new AllocEngine(AllocEngine::ALLOC_ITERATIVE, 0));
  200. /// @todo call loadLibraries() when handling configuration changes
  201. } catch (const std::exception &e) {
  202. LOG_ERROR(dhcp6_logger, DHCP6_SRV_CONSTRUCT_ERROR).arg(e.what());
  203. return;
  204. }
  205. // All done, so can proceed
  206. shutdown_ = false;
  207. }
  208. Dhcpv6Srv::~Dhcpv6Srv() {
  209. try {
  210. stopD2();
  211. } catch(const std::exception& ex) {
  212. // Highly unlikely, but lets Report it but go on
  213. LOG_ERROR(dhcp6_logger, DHCP6_SRV_D2STOP_ERROR).arg(ex.what());
  214. }
  215. IfaceMgr::instance().closeSockets();
  216. LeaseMgrFactory::destroy();
  217. }
  218. void Dhcpv6Srv::shutdown() {
  219. LOG_DEBUG(dhcp6_logger, DBG_DHCP6_BASIC, DHCP6_SHUTDOWN_REQUEST);
  220. shutdown_ = true;
  221. }
  222. Pkt6Ptr Dhcpv6Srv::receivePacket(int timeout) {
  223. return (IfaceMgr::instance().receive6(timeout));
  224. }
  225. void Dhcpv6Srv::sendPacket(const Pkt6Ptr& packet) {
  226. IfaceMgr::instance().send(packet);
  227. }
  228. bool
  229. Dhcpv6Srv::testServerID(const Pkt6Ptr& pkt) {
  230. /// @todo Currently we always check server identifier regardless if
  231. /// it is allowed in the received message or not (per RFC3315).
  232. /// If the server identifier is not allowed in the message, the
  233. /// sanityCheck function should deal with it.
  234. OptionPtr server_id = pkt->getOption(D6O_SERVERID);
  235. if (server_id){
  236. // Let us test received ServerID if it is same as ServerID
  237. // which is being used by server
  238. if (getServerID()->getData() != server_id->getData()){
  239. LOG_DEBUG(bad_packet6_logger, DBG_DHCP6_DETAIL_DATA,
  240. DHCP6_PACKET_DROP_SERVERID_MISMATCH)
  241. .arg(pkt->getLabel())
  242. .arg(duidToString(server_id))
  243. .arg(duidToString(getServerID()));
  244. return (false);
  245. }
  246. }
  247. // return True if: no serverid received or ServerIDs matching
  248. return (true);
  249. }
  250. bool
  251. Dhcpv6Srv::testUnicast(const Pkt6Ptr& pkt) const {
  252. switch (pkt->getType()) {
  253. case DHCPV6_SOLICIT:
  254. case DHCPV6_CONFIRM:
  255. case DHCPV6_REBIND:
  256. case DHCPV6_INFORMATION_REQUEST:
  257. if (pkt->relay_info_.empty() && !pkt->getLocalAddr().isV6Multicast()) {
  258. LOG_DEBUG(bad_packet6_logger, DBG_DHCP6_DETAIL, DHCP6_PACKET_DROP_UNICAST)
  259. .arg(pkt->getLabel())
  260. .arg(pkt->getName());
  261. return (false);
  262. }
  263. break;
  264. default:
  265. // do nothing
  266. ;
  267. }
  268. return (true);
  269. }
  270. AllocEngine::ClientContext6
  271. Dhcpv6Srv::createContext(const Pkt6Ptr& pkt) {
  272. AllocEngine::ClientContext6 ctx;
  273. ctx.subnet_ = selectSubnet(pkt);
  274. ctx.duid_ = pkt->getClientId();
  275. ctx.hwaddr_ = getMAC(pkt);
  276. ctx.query_ = pkt;
  277. alloc_engine_->findReservation(ctx);
  278. return (ctx);
  279. }
  280. bool Dhcpv6Srv::run() {
  281. while (!shutdown_) {
  282. // client's message and server's response
  283. Pkt6Ptr query;
  284. Pkt6Ptr rsp;
  285. try {
  286. uint32_t timeout = 1000;
  287. LOG_DEBUG(packet6_logger, DBG_DHCP6_DETAIL, DHCP6_BUFFER_WAIT).arg(timeout);
  288. query = receivePacket(timeout);
  289. // Log if packet has arrived. We can't log the detailed information
  290. // about the DHCP message because it hasn't been unpacked/parsed
  291. // yet, and it can't be parsed at this point because hooks will
  292. // have to process it first. The only information available at this
  293. // point are: the interface, source address and destination addresses
  294. // and ports.
  295. if (query) {
  296. LOG_DEBUG(packet6_logger, DBG_DHCP6_BASIC, DHCP6_BUFFER_RECEIVED)
  297. .arg(query->getRemoteAddr().toText())
  298. .arg(query->getRemotePort())
  299. .arg(query->getLocalAddr().toText())
  300. .arg(query->getLocalPort())
  301. .arg(query->getIface());
  302. // Log reception of the packet. We need to increase it early, as
  303. // any failures in unpacking will cause the packet to be dropped.
  304. // we will increase type specific packets further down the road.
  305. // See processStatsReceived().
  306. StatsMgr::instance().addValue("pkt6-received", static_cast<int64_t>(1));
  307. } else {
  308. LOG_DEBUG(packet6_logger, DBG_DHCP6_DETAIL, DHCP6_BUFFER_WAIT_INTERRUPTED)
  309. .arg(timeout);
  310. }
  311. } catch (const SignalInterruptOnSelect) {
  312. // Packet reception interrupted because a signal has been received.
  313. // This is not an error because we might have received a SIGTERM,
  314. // SIGINT or SIGHUP which are handled by the server. For signals
  315. // that are not handled by the server we rely on the default
  316. // behavior of the system.
  317. LOG_DEBUG(packet6_logger, DBG_DHCP6_DETAIL, DHCP6_BUFFER_WAIT_SIGNAL)
  318. .arg(signal_set_->getNext());
  319. } catch (const std::exception& e) {
  320. LOG_ERROR(packet6_logger, DHCP6_PACKET_RECEIVE_FAIL).arg(e.what());
  321. }
  322. // Handle next signal received by the process. It must be called after
  323. // an attempt to receive a packet to properly handle server shut down.
  324. // The SIGTERM or SIGINT will be received prior to, or during execution
  325. // of select() (select is invoked by receivePacket()). When that happens,
  326. // select will be interrupted. The signal handler will be invoked
  327. // immediately after select(). The handler will set the shutdown flag
  328. // and cause the process to terminate before the next select() function
  329. // is called. If the function was called before receivePacket the
  330. // process could wait up to the duration of timeout of select() to
  331. // terminate.
  332. try {
  333. handleSignal();
  334. } catch (const std::exception& e) {
  335. // An (a standard or ISC) exception occurred.
  336. LOG_ERROR(dhcp6_logger, DHCP6_HANDLE_SIGNAL_EXCEPTION)
  337. .arg(e.what());
  338. }
  339. // Timeout may be reached or signal received, which breaks select()
  340. // with no packet received
  341. if (!query) {
  342. continue;
  343. }
  344. // In order to parse the DHCP options, the server needs to use some
  345. // configuration information such as: existing option spaces, option
  346. // definitions etc. This is the kind of information which is not
  347. // available in the libdhcp, so we need to supply our own implementation
  348. // of the option parsing function here, which would rely on the
  349. // configuration data.
  350. query->setCallback(boost::bind(&Dhcpv6Srv::unpackOptions, this, _1, _2,
  351. _3, _4, _5));
  352. bool skip_unpack = false;
  353. // The packet has just been received so contains the uninterpreted wire
  354. // data; execute callouts registered for buffer6_receive.
  355. if (HooksManager::calloutsPresent(Hooks.hook_index_buffer6_receive_)) {
  356. CalloutHandlePtr callout_handle = getCalloutHandle(query);
  357. // Delete previously set arguments
  358. callout_handle->deleteAllArguments();
  359. // Pass incoming packet as argument
  360. callout_handle->setArgument("query6", query);
  361. // Call callouts
  362. HooksManager::callCallouts(Hooks.hook_index_buffer6_receive_, *callout_handle);
  363. // Callouts decided to skip the next processing step. The next
  364. // processing step would to parse the packet, so skip at this
  365. // stage means that callouts did the parsing already, so server
  366. // should skip parsing.
  367. if (callout_handle->getStatus() == CalloutHandle::NEXT_STEP_SKIP) {
  368. LOG_DEBUG(hooks_logger, DBG_DHCP6_DETAIL, DHCP6_HOOK_BUFFER_RCVD_SKIP)
  369. .arg(query->getRemoteAddr().toText())
  370. .arg(query->getLocalAddr().toText())
  371. .arg(query->getIface());
  372. skip_unpack = true;
  373. }
  374. /// @todo: Add support for DROP status.
  375. callout_handle->getArgument("query6", query);
  376. }
  377. // Unpack the packet information unless the buffer6_receive callouts
  378. // indicated they did it
  379. if (!skip_unpack) {
  380. try {
  381. LOG_DEBUG(options6_logger, DBG_DHCP6_DETAIL, DHCP6_BUFFER_UNPACK)
  382. .arg(query->getRemoteAddr().toText())
  383. .arg(query->getLocalAddr().toText())
  384. .arg(query->getIface());
  385. query->unpack();
  386. } catch (const std::exception &e) {
  387. // Failed to parse the packet.
  388. LOG_DEBUG(bad_packet6_logger, DBG_DHCP6_DETAIL,
  389. DHCP6_PACKET_DROP_PARSE_FAIL)
  390. .arg(query->getRemoteAddr().toText())
  391. .arg(query->getLocalAddr().toText())
  392. .arg(query->getIface())
  393. .arg(e.what());
  394. // Increase the statistics of parse failures and dropped packets.
  395. StatsMgr::instance().addValue("pkt6-parse-failed",
  396. static_cast<int64_t>(1));
  397. StatsMgr::instance().addValue("pkt6-receive-drop",
  398. static_cast<int64_t>(1));
  399. continue;
  400. }
  401. }
  402. // Update statistics accordingly for received packet.
  403. processStatsReceived(query);
  404. // Check if received query carries server identifier matching
  405. // server identifier being used by the server.
  406. if (!testServerID(query)) {
  407. // Increase the statistic of dropped packets.
  408. StatsMgr::instance().addValue("pkt6-receive-drop", static_cast<int64_t>(1));
  409. continue;
  410. }
  411. // Check if the received query has been sent to unicast or multicast.
  412. // The Solicit, Confirm, Rebind and Information Request will be
  413. // discarded if sent to unicast address.
  414. if (!testUnicast(query)) {
  415. // Increase the statistic of dropped packets.
  416. StatsMgr::instance().addValue("pkt6-receive-drop", static_cast<int64_t>(1));
  417. continue;
  418. }
  419. LOG_DEBUG(packet6_logger, DBG_DHCP6_BASIC_DATA, DHCP6_PACKET_RECEIVED)
  420. .arg(query->getLabel())
  421. .arg(query->getName())
  422. .arg(static_cast<int>(query->getType()))
  423. .arg(query->getRemoteAddr())
  424. .arg(query->getLocalAddr())
  425. .arg(query->getIface());
  426. LOG_DEBUG(packet6_logger, DBG_DHCP6_DETAIL_DATA, DHCP6_QUERY_DATA)
  427. .arg(query->getLabel())
  428. .arg(query->toText());
  429. // At this point the information in the packet has been unpacked into
  430. // the various packet fields and option objects has been created.
  431. // Execute callouts registered for packet6_receive.
  432. if (HooksManager::calloutsPresent(Hooks.hook_index_pkt6_receive_)) {
  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("query6", query);
  438. // Call callouts
  439. HooksManager::callCallouts(Hooks.hook_index_pkt6_receive_, *callout_handle);
  440. // Callouts decided to skip the next processing step. The next
  441. // processing step would to process the packet, so skip at this
  442. // stage means drop.
  443. if (callout_handle->getStatus() == CalloutHandle::NEXT_STEP_SKIP) {
  444. LOG_DEBUG(hooks_logger, DBG_DHCP6_HOOKS, DHCP6_HOOK_PACKET_RCVD_SKIP)
  445. .arg(query->getLabel());
  446. continue;
  447. }
  448. /// @todo: Add support for DROP status.
  449. callout_handle->getArgument("query6", query);
  450. }
  451. // Assign this packet to a class, if possible
  452. classifyPacket(query);
  453. try {
  454. NameChangeRequestPtr ncr;
  455. switch (query->getType()) {
  456. case DHCPV6_SOLICIT:
  457. rsp = processSolicit(query);
  458. break;
  459. case DHCPV6_REQUEST:
  460. rsp = processRequest(query);
  461. break;
  462. case DHCPV6_RENEW:
  463. rsp = processRenew(query);
  464. break;
  465. case DHCPV6_REBIND:
  466. rsp = processRebind(query);
  467. break;
  468. case DHCPV6_CONFIRM:
  469. rsp = processConfirm(query);
  470. break;
  471. case DHCPV6_RELEASE:
  472. rsp = processRelease(query);
  473. break;
  474. case DHCPV6_DECLINE:
  475. rsp = processDecline(query);
  476. break;
  477. case DHCPV6_INFORMATION_REQUEST:
  478. rsp = processInfRequest(query);
  479. break;
  480. default:
  481. // We received a packet type that we do not recognize.
  482. LOG_DEBUG(bad_packet6_logger, DBG_DHCP6_BASIC, DHCP6_UNKNOWN_MSG_RECEIVED)
  483. .arg(static_cast<int>(query->getType()))
  484. .arg(query->getIface());
  485. // Only action is to output a message if debug is enabled,
  486. // and that will be covered by the debug statement before
  487. // the "switch" statement.
  488. ;
  489. }
  490. } catch (const RFCViolation& e) {
  491. LOG_DEBUG(bad_packet6_logger, DBG_DHCP6_BASIC, DHCP6_REQUIRED_OPTIONS_CHECK_FAIL)
  492. .arg(query->getName())
  493. .arg(query->getRemoteAddr().toText())
  494. .arg(e.what());
  495. // Increase the statistic of dropped packets.
  496. StatsMgr::instance().addValue("pkt6-receive-drop", static_cast<int64_t>(1));
  497. } catch (const std::exception& e) {
  498. // Catch-all exception (at least for ones based on the isc Exception
  499. // class, which covers more or less all that are explicitly raised
  500. // in the Kea code), but also the standard one, which may possibly be
  501. // thrown from boost code. Just log the problem and ignore the packet.
  502. // (The problem is logged as a debug message because debug is
  503. // disabled by default - it prevents a DDOS attack based on the
  504. // sending of problem packets.)
  505. LOG_DEBUG(bad_packet6_logger, DBG_DHCP6_BASIC, DHCP6_PACKET_PROCESS_FAIL)
  506. .arg(query->getName())
  507. .arg(query->getRemoteAddr().toText())
  508. .arg(e.what());
  509. // Increase the statistic of dropped packets.
  510. StatsMgr::instance().addValue("pkt6-receive-drop", static_cast<int64_t>(1));
  511. }
  512. if (rsp) {
  513. // Process relay-supplied options. It is important to call this very
  514. // late in the process, because we now have all the options the
  515. // server wanted to send already set. This is important, because
  516. // RFC6422, section 6 states:
  517. //
  518. // The server SHOULD discard any options that appear in the RSOO
  519. // for which it already has one or more candidates.
  520. //
  521. // So we ignore any RSOO options if there's an option with the same
  522. // code already present.
  523. processRSOO(query, rsp);
  524. rsp->setRemoteAddr(query->getRemoteAddr());
  525. rsp->setLocalAddr(query->getLocalAddr());
  526. if (rsp->relay_info_.empty()) {
  527. // Direct traffic, send back to the client directly
  528. rsp->setRemotePort(DHCP6_CLIENT_PORT);
  529. } else {
  530. // Relayed traffic, send back to the relay agent
  531. rsp->setRemotePort(DHCP6_SERVER_PORT);
  532. }
  533. rsp->setLocalPort(DHCP6_SERVER_PORT);
  534. rsp->setIndex(query->getIndex());
  535. rsp->setIface(query->getIface());
  536. // Specifies if server should do the packing
  537. bool skip_pack = false;
  538. // Server's reply packet now has all options and fields set.
  539. // Options are represented by individual objects, but the
  540. // output wire data has not been prepared yet.
  541. // Execute all callouts registered for packet6_send
  542. if (HooksManager::calloutsPresent(Hooks.hook_index_pkt6_send_)) {
  543. CalloutHandlePtr callout_handle = getCalloutHandle(query);
  544. // Delete all previous arguments
  545. callout_handle->deleteAllArguments();
  546. // Set our response
  547. callout_handle->setArgument("response6", rsp);
  548. // Call all installed callouts
  549. HooksManager::callCallouts(Hooks.hook_index_pkt6_send_, *callout_handle);
  550. // Callouts decided to skip the next processing step. The next
  551. // processing step would to pack the packet (create wire data).
  552. // That step will be skipped if any callout sets skip flag.
  553. // It essentially means that the callout already did packing,
  554. // so the server does not have to do it again.
  555. if (callout_handle->getStatus() == CalloutHandle::NEXT_STEP_SKIP) {
  556. LOG_DEBUG(hooks_logger, DBG_DHCP6_HOOKS, DHCP6_HOOK_PACKET_SEND_SKIP)
  557. .arg(rsp->getLabel());
  558. skip_pack = true;
  559. }
  560. /// @todo: Add support for DROP status
  561. }
  562. if (!skip_pack) {
  563. try {
  564. rsp->pack();
  565. } catch (const std::exception& e) {
  566. LOG_ERROR(options6_logger, DHCP6_PACK_FAIL)
  567. .arg(e.what());
  568. continue;
  569. }
  570. }
  571. try {
  572. // Now all fields and options are constructed into output wire buffer.
  573. // Option objects modification does not make sense anymore. Hooks
  574. // can only manipulate wire buffer at this stage.
  575. // Let's execute all callouts registered for buffer6_send
  576. if (HooksManager::calloutsPresent(Hooks.hook_index_buffer6_send_)) {
  577. CalloutHandlePtr callout_handle = getCalloutHandle(query);
  578. // Delete previously set arguments
  579. callout_handle->deleteAllArguments();
  580. // Pass incoming packet as argument
  581. callout_handle->setArgument("response6", rsp);
  582. // Call callouts
  583. HooksManager::callCallouts(Hooks.hook_index_buffer6_send_, *callout_handle);
  584. // Callouts decided to skip the next processing step. The next
  585. // processing step would to parse the packet, so skip at this
  586. // stage means drop.
  587. if (callout_handle->getStatus() == CalloutHandle::NEXT_STEP_SKIP) {
  588. LOG_DEBUG(hooks_logger, DBG_DHCP6_HOOKS, DHCP6_HOOK_BUFFER_SEND_SKIP)
  589. .arg(rsp->getLabel());
  590. continue;
  591. }
  592. /// @todo: Add support for DROP status
  593. callout_handle->getArgument("response6", rsp);
  594. }
  595. LOG_DEBUG(packet6_logger, DBG_DHCP6_DETAIL_DATA,
  596. DHCP6_RESPONSE_DATA)
  597. .arg(static_cast<int>(rsp->getType())).arg(rsp->toText());
  598. sendPacket(rsp);
  599. // Update statistics accordingly for sent packet.
  600. processStatsSent(rsp);
  601. } catch (const std::exception& e) {
  602. LOG_ERROR(packet6_logger, DHCP6_PACKET_SEND_FAIL)
  603. .arg(e.what());
  604. }
  605. }
  606. }
  607. return (true);
  608. }
  609. bool Dhcpv6Srv::loadServerID(const std::string& file_name) {
  610. // load content of the file into a string
  611. fstream f(file_name.c_str(), ios::in);
  612. if (!f.is_open()) {
  613. return (false);
  614. }
  615. string hex_string;
  616. f >> hex_string;
  617. f.close();
  618. // remove any spaces
  619. boost::algorithm::erase_all(hex_string, " ");
  620. // now remove :
  621. /// @todo: We should check first if the format is sane.
  622. /// Otherwise 1:2:3:4 will be converted to 0x12, 0x34
  623. boost::algorithm::erase_all(hex_string, ":");
  624. std::vector<uint8_t> bin;
  625. // Decode the hex string and store it in bin (which happens
  626. // to be OptionBuffer format)
  627. isc::util::encode::decodeHex(hex_string, bin);
  628. // Now create server-id option
  629. serverid_.reset(new Option(Option::V6, D6O_SERVERID, bin));
  630. return (true);
  631. }
  632. std::string
  633. Dhcpv6Srv::duidToString(const OptionPtr& opt) {
  634. stringstream tmp;
  635. OptionBuffer data = opt->getData();
  636. bool colon = false;
  637. for (OptionBufferConstIter it = data.begin(); it != data.end(); ++it) {
  638. if (colon) {
  639. tmp << ":";
  640. }
  641. tmp << hex << setw(2) << setfill('0') << static_cast<uint16_t>(*it);
  642. if (!colon) {
  643. colon = true;
  644. }
  645. }
  646. return tmp.str();
  647. }
  648. bool
  649. Dhcpv6Srv::writeServerID(const std::string& file_name) {
  650. fstream f(file_name.c_str(), ios::out | ios::trunc);
  651. if (!f.good()) {
  652. return (false);
  653. }
  654. f << duidToString(getServerID());
  655. f.close();
  656. return (true);
  657. }
  658. void
  659. Dhcpv6Srv::generateServerID() {
  660. /// @todo: This code implements support for DUID-LLT (the recommended one).
  661. /// We should eventually add support for other DUID types: DUID-LL, DUID-EN
  662. /// and DUID-UUID
  663. const IfaceMgr::IfaceCollection& ifaces = IfaceMgr::instance().getIfaces();
  664. // Let's find suitable interface.
  665. BOOST_FOREACH(IfacePtr iface, ifaces) {
  666. // All the following checks could be merged into one multi-condition
  667. // statement, but let's keep them separated as perhaps one day
  668. // we will grow knobs to selectively turn them on or off. Also,
  669. // this code is used only *once* during first start on a new machine
  670. // and then server-id is stored. (or at least it will be once
  671. // DUID storage is implemented)
  672. // I wish there was a this_is_a_real_physical_interface flag...
  673. // MAC address should be at least 6 bytes. Although there is no such
  674. // requirement in any RFC, all decent physical interfaces (Ethernet,
  675. // WiFi, InfiniBand, etc.) have 6 bytes long MAC address. We want to
  676. // base our DUID on real hardware address, rather than virtual
  677. // interface that pretends that underlying IP address is its MAC.
  678. if (iface->getMacLen() < MIN_MAC_LEN) {
  679. continue;
  680. }
  681. // Let's don't use loopback.
  682. if (iface->flag_loopback_) {
  683. continue;
  684. }
  685. // Let's skip downed interfaces. It is better to use working ones.
  686. if (!iface->flag_up_) {
  687. continue;
  688. }
  689. // Some interfaces (like lo on Linux) report 6-bytes long
  690. // MAC address 00:00:00:00:00:00. Let's not use such weird interfaces
  691. // to generate DUID.
  692. if (isRangeZero(iface->getMac(), iface->getMac() + iface->getMacLen())) {
  693. continue;
  694. }
  695. // Ok, we have useful MAC. Let's generate DUID-LLT based on
  696. // it. See RFC3315, Section 9.2 for details.
  697. // DUID uses seconds since midnight of 01-01-2000, time() returns
  698. // seconds since 01-01-1970. DUID_TIME_EPOCH substitution corrects
  699. // that.
  700. time_t seconds = time(NULL);
  701. seconds -= DUID_TIME_EPOCH;
  702. OptionBuffer srvid(8 + iface->getMacLen());
  703. // We know that the buffer is more than 8 bytes long at this point.
  704. writeUint16(DUID::DUID_LLT, &srvid[0], 2);
  705. writeUint16(HWTYPE_ETHERNET, &srvid[2], 2);
  706. writeUint32(static_cast<uint32_t>(seconds), &srvid[4], 4);
  707. memcpy(&srvid[8], iface->getMac(), iface->getMacLen());
  708. serverid_ = OptionPtr(new Option(Option::V6, D6O_SERVERID,
  709. srvid.begin(), srvid.end()));
  710. return;
  711. }
  712. // If we reached here, there are no suitable interfaces found.
  713. // Either interface detection is not supported on this platform or
  714. // this is really weird box. Let's use DUID-EN instead.
  715. // See Section 9.3 of RFC3315 for details.
  716. OptionBuffer srvid(12);
  717. writeUint16(DUID::DUID_EN, &srvid[0], srvid.size());
  718. writeUint32(ENTERPRISE_ID_ISC, &srvid[2], srvid.size() - 2);
  719. // Length of the identifier is company specific. I hereby declare
  720. // ISC "standard" of 6 bytes long pseudo-random numbers.
  721. srandom(time(NULL));
  722. fillRandom(&srvid[6], &srvid[12]);
  723. serverid_ = OptionPtr(new Option(Option::V6, D6O_SERVERID,
  724. srvid.begin(), srvid.end()));
  725. }
  726. void
  727. Dhcpv6Srv::copyClientOptions(const Pkt6Ptr& question, Pkt6Ptr& answer) {
  728. // Add client-id.
  729. OptionPtr clientid = question->getOption(D6O_CLIENTID);
  730. if (clientid) {
  731. answer->addOption(clientid);
  732. }
  733. /// @todo: Should throw if there is no client-id (except anonymous INF-REQUEST)
  734. // If this is a relayed message, we need to copy relay information
  735. if (!question->relay_info_.empty()) {
  736. answer->copyRelayInfo(question);
  737. }
  738. }
  739. void
  740. Dhcpv6Srv::appendDefaultOptions(const Pkt6Ptr&, Pkt6Ptr& answer,
  741. const CfgOptionList&) {
  742. // add server-id
  743. answer->addOption(getServerID());
  744. }
  745. void
  746. Dhcpv6Srv::buildCfgOptionList(const Pkt6Ptr& question,
  747. AllocEngine::ClientContext6& ctx,
  748. CfgOptionList& co_list) {
  749. // First subnet configured options
  750. if (ctx.subnet_) {
  751. co_list.push_back(ctx.subnet_->getCfgOption());
  752. }
  753. // Each class in the incoming packet
  754. const ClientClasses& classes = question->getClasses();
  755. for (ClientClasses::const_iterator cclass = classes.begin();
  756. cclass != classes.end(); ++cclass) {
  757. // Find the client class definition for this class
  758. const ClientClassDefPtr& ccdef = CfgMgr::instance().getCurrentCfg()->
  759. getClientClassDictionary()->findClass(*cclass);
  760. if (!ccdef) {
  761. // Not found: the class is not configured
  762. LOG_DEBUG(dhcp6_logger, DBG_DHCP6_BASIC, DHCP6_CLASS_UNCONFIGURED)
  763. .arg(*cclass);
  764. continue;
  765. }
  766. co_list.push_back(ccdef->getCfgOption());
  767. }
  768. // Last global options
  769. co_list.push_back(CfgMgr::instance().getCurrentCfg()->getCfgOption());
  770. }
  771. void
  772. Dhcpv6Srv::appendRequestedOptions(const Pkt6Ptr& question, Pkt6Ptr& answer,
  773. AllocEngine::ClientContext6& ctx,
  774. const CfgOptionList& co_list) {
  775. // Client requests some options using ORO option. Try to
  776. // get this option from client's message.
  777. boost::shared_ptr<OptionIntArray<uint16_t> > option_oro =
  778. boost::dynamic_pointer_cast<OptionIntArray<uint16_t> >
  779. (question->getOption(D6O_ORO));
  780. // Option ORO not found? We're done here then.
  781. if (!option_oro) {
  782. return;
  783. }
  784. // Get the list of options that client requested.
  785. const std::vector<uint16_t>& requested_opts = option_oro->getValues();
  786. BOOST_FOREACH(uint16_t opt, requested_opts) {
  787. // Iterate on the configured option list
  788. for (CfgOptionList::const_iterator copts = co_list.begin();
  789. copts != co_list.end(); ++copts) {
  790. OptionDescriptor desc = (*copts)->get("dhcp6", opt);
  791. // Got it: add it and jump to the outer loop
  792. if (desc.option_) {
  793. answer->addOption(desc.option_);
  794. break;
  795. }
  796. }
  797. }
  798. }
  799. void
  800. Dhcpv6Srv::appendRequestedVendorOptions(const Pkt6Ptr& question,
  801. Pkt6Ptr& answer,
  802. AllocEngine::ClientContext6& ctx,
  803. const CfgOptionList& co_list) {
  804. // Leave if there is no subnet matching the incoming packet.
  805. // There is no need to log the error message here because
  806. // it will be logged in the assignLease() when it fails to
  807. // pick the suitable subnet. We don't want to duplicate
  808. // error messages in such case.
  809. if (!ctx.subnet_) {
  810. return;
  811. }
  812. // Try to get the vendor option
  813. boost::shared_ptr<OptionVendor> vendor_req =
  814. boost::dynamic_pointer_cast<OptionVendor>(question->getOption(D6O_VENDOR_OPTS));
  815. if (!vendor_req) {
  816. return;
  817. }
  818. // Let's try to get ORO within that vendor-option
  819. /// @todo This is very specific to vendor-id=4491 (Cable Labs). Other vendors
  820. /// may have different policies.
  821. boost::shared_ptr<OptionUint16Array> oro =
  822. boost::dynamic_pointer_cast<OptionUint16Array>(vendor_req->getOption(DOCSIS3_V6_ORO));
  823. // Option ORO not found. Don't do anything then.
  824. if (!oro) {
  825. return;
  826. }
  827. uint32_t vendor_id = vendor_req->getVendorId();
  828. boost::shared_ptr<OptionVendor> vendor_rsp(new OptionVendor(Option::V6, vendor_id));
  829. // Get the list of options that client requested.
  830. bool added = false;
  831. const std::vector<uint16_t>& requested_opts = oro->getValues();
  832. BOOST_FOREACH(uint16_t opt, requested_opts) {
  833. for (CfgOptionList::const_iterator copts = co_list.begin();
  834. copts != co_list.end(); ++copts) {
  835. OptionDescriptor desc = (*copts)->get(vendor_id, opt);
  836. if (desc.option_) {
  837. vendor_rsp->addOption(desc.option_);
  838. added = true;
  839. break;
  840. }
  841. }
  842. }
  843. if (added) {
  844. answer->addOption(vendor_rsp);
  845. }
  846. }
  847. void
  848. Dhcpv6Srv::sanityCheck(const Pkt6Ptr& pkt, RequirementLevel clientid,
  849. RequirementLevel serverid) {
  850. OptionCollection client_ids = pkt->getOptions(D6O_CLIENTID);
  851. switch (clientid) {
  852. case MANDATORY:
  853. if (client_ids.size() != 1) {
  854. isc_throw(RFCViolation, "Exactly 1 client-id option expected in "
  855. << pkt->getName() << ", but " << client_ids.size()
  856. << " received");
  857. }
  858. break;
  859. case OPTIONAL:
  860. if (client_ids.size() > 1) {
  861. isc_throw(RFCViolation, "Too many (" << client_ids.size()
  862. << ") client-id options received in " << pkt->getName());
  863. }
  864. break;
  865. case FORBIDDEN:
  866. // doesn't make sense - client-id is always allowed
  867. break;
  868. }
  869. OptionCollection server_ids = pkt->getOptions(D6O_SERVERID);
  870. switch (serverid) {
  871. case FORBIDDEN:
  872. if (!server_ids.empty()) {
  873. isc_throw(RFCViolation, "Server-id option was not expected, but "
  874. << server_ids.size() << " received in " << pkt->getName());
  875. }
  876. break;
  877. case MANDATORY:
  878. if (server_ids.size() != 1) {
  879. isc_throw(RFCViolation, "Invalid number of server-id options received ("
  880. << server_ids.size() << "), exactly 1 expected in message "
  881. << pkt->getName());
  882. }
  883. break;
  884. case OPTIONAL:
  885. if (server_ids.size() > 1) {
  886. isc_throw(RFCViolation, "Too many (" << server_ids.size()
  887. << ") server-id options received in " << pkt->getName());
  888. }
  889. }
  890. }
  891. Subnet6Ptr
  892. Dhcpv6Srv::selectSubnet(const Pkt6Ptr& question) {
  893. // Initialize subnet selector with the values used to select the subnet.
  894. SubnetSelector selector;
  895. selector.iface_name_ = question->getIface();
  896. selector.remote_address_ = question->getRemoteAddr();
  897. selector.first_relay_linkaddr_ = IOAddress("::");
  898. selector.client_classes_ = question->classes_;
  899. // Initialize fields specific to relayed messages.
  900. if (!question->relay_info_.empty()) {
  901. BOOST_REVERSE_FOREACH(Pkt6::RelayInfo relay, question->relay_info_) {
  902. if (!relay.linkaddr_.isV6Zero() &&
  903. !relay.linkaddr_.isV6LinkLocal()) {
  904. selector.first_relay_linkaddr_ = relay.linkaddr_;
  905. break;
  906. }
  907. }
  908. selector.interface_id_ =
  909. question->getAnyRelayOption(D6O_INTERFACE_ID,
  910. Pkt6::RELAY_GET_FIRST);
  911. }
  912. Subnet6Ptr subnet = CfgMgr::instance().getCurrentCfg()->
  913. getCfgSubnets6()->selectSubnet(selector);
  914. // Let's execute all callouts registered for subnet6_receive
  915. if (HooksManager::calloutsPresent(Hooks.hook_index_subnet6_select_)) {
  916. CalloutHandlePtr callout_handle = getCalloutHandle(question);
  917. // We're reusing callout_handle from previous calls
  918. callout_handle->deleteAllArguments();
  919. // Set new arguments
  920. callout_handle->setArgument("query6", question);
  921. callout_handle->setArgument("subnet6", subnet);
  922. // We pass pointer to const collection for performance reasons.
  923. // Otherwise we would get a non-trivial performance penalty each
  924. // time subnet6_select is called.
  925. callout_handle->setArgument("subnet6collection",
  926. CfgMgr::instance().getCurrentCfg()->
  927. getCfgSubnets6()->getAll());
  928. // Call user (and server-side) callouts
  929. HooksManager::callCallouts(Hooks.hook_index_subnet6_select_, *callout_handle);
  930. // Callouts decided to skip this step. This means that no
  931. // subnet will be selected. Packet processing will continue,
  932. // but it will be severely limited (i.e. only global options
  933. // will be assigned)
  934. if (callout_handle->getStatus() == CalloutHandle::NEXT_STEP_SKIP) {
  935. LOG_DEBUG(hooks_logger, DBG_DHCP6_HOOKS, DHCP6_HOOK_SUBNET6_SELECT_SKIP)
  936. .arg(question->getLabel());
  937. return (Subnet6Ptr());
  938. }
  939. /// @todo: Add support for DROP status.
  940. // Use whatever subnet was specified by the callout
  941. callout_handle->getArgument("subnet6", subnet);
  942. }
  943. if (subnet) {
  944. // Log at higher debug level that subnet has been found.
  945. LOG_DEBUG(packet6_logger, DBG_DHCP6_BASIC_DATA, DHCP6_SUBNET_SELECTED)
  946. .arg(question->getLabel())
  947. .arg(subnet->getID());
  948. // Log detailed information about the selected subnet at the
  949. // lower debug level.
  950. LOG_DEBUG(packet6_logger, DBG_DHCP6_DETAIL_DATA, DHCP6_SUBNET_DATA)
  951. .arg(question->getLabel())
  952. .arg(subnet->toText());
  953. } else {
  954. LOG_DEBUG(packet6_logger, DBG_DHCP6_DETAIL, DHCP6_SUBNET_SELECTION_FAILED)
  955. .arg(question->getLabel());
  956. }
  957. return (subnet);
  958. }
  959. void
  960. Dhcpv6Srv::assignLeases(const Pkt6Ptr& question, Pkt6Ptr& answer,
  961. AllocEngine::ClientContext6& ctx) {
  962. // We need to allocate addresses for all IA_NA options in the client's
  963. // question (i.e. SOLICIT or REQUEST) message.
  964. // @todo add support for IA_TA
  965. // For the lease allocation it is critical that the client has sent
  966. // DUID. There is no need to check for the presence of the DUID here
  967. // because we have already checked it in the sanityCheck().
  968. // Now that we have all information about the client, let's iterate over all
  969. // received options and handle IA_NA options one by one and store our
  970. // responses in answer message (ADVERTISE or REPLY).
  971. //
  972. // @todo: IA_TA once we implement support for temporary addresses.
  973. for (OptionCollection::iterator opt = question->options_.begin();
  974. opt != question->options_.end(); ++opt) {
  975. switch (opt->second->getType()) {
  976. case D6O_IA_NA: {
  977. OptionPtr answer_opt = assignIA_NA(question, answer, ctx,
  978. boost::dynamic_pointer_cast<
  979. Option6IA>(opt->second));
  980. if (answer_opt) {
  981. answer->addOption(answer_opt);
  982. }
  983. break;
  984. }
  985. case D6O_IA_PD: {
  986. OptionPtr answer_opt = assignIA_PD(question, answer, ctx,
  987. boost::dynamic_pointer_cast<
  988. Option6IA>(opt->second));
  989. if (answer_opt) {
  990. answer->addOption(answer_opt);
  991. }
  992. }
  993. default:
  994. break;
  995. }
  996. }
  997. }
  998. void
  999. Dhcpv6Srv::processClientFqdn(const Pkt6Ptr& question, const Pkt6Ptr& answer,
  1000. AllocEngine::ClientContext6& ctx) {
  1001. // Get Client FQDN Option from the client's message. If this option hasn't
  1002. // been included, do nothing.
  1003. Option6ClientFqdnPtr fqdn = boost::dynamic_pointer_cast<
  1004. Option6ClientFqdn>(question->getOption(D6O_CLIENT_FQDN));
  1005. if (!fqdn) {
  1006. // No FQDN so lease hostname comes from host reservation if one
  1007. if (ctx.host_) {
  1008. ctx.hostname_ = ctx.host_->getHostname();
  1009. }
  1010. return;
  1011. }
  1012. LOG_DEBUG(ddns6_logger, DBG_DHCP6_DETAIL, DHCP6_DDNS_RECEIVE_FQDN)
  1013. .arg(question->getLabel())
  1014. .arg(fqdn->toText());
  1015. // Create the DHCPv6 Client FQDN Option to be included in the server's
  1016. // response to a client.
  1017. Option6ClientFqdnPtr fqdn_resp(new Option6ClientFqdn(*fqdn));
  1018. // Set the server S, N, and O flags based on client's flags and
  1019. // current configuration.
  1020. D2ClientMgr& d2_mgr = CfgMgr::instance().getD2ClientMgr();
  1021. d2_mgr.adjustFqdnFlags<Option6ClientFqdn>(*fqdn, *fqdn_resp);
  1022. // If there's a reservation and it has a hostname specified, use it!
  1023. if (ctx.host_ && !ctx.host_->getHostname().empty()) {
  1024. D2ClientMgr& d2_mgr = CfgMgr::instance().getD2ClientMgr();
  1025. // Add the qualifying suffix.
  1026. // After #3765, this will only occur if the suffix is not empty.
  1027. fqdn_resp->setDomainName(d2_mgr.qualifyName(ctx.host_->getHostname(),
  1028. true),
  1029. Option6ClientFqdn::FULL);
  1030. } else {
  1031. // Adjust the domain name based on domain name value and type sent by
  1032. // the client and current configuration.
  1033. d2_mgr.adjustDomainName<Option6ClientFqdn>(*fqdn, *fqdn_resp);
  1034. }
  1035. // Once we have the FQDN setup to use it for the lease hostname. This
  1036. // only gets replaced later if the FQDN is to be generated from the address.
  1037. ctx.hostname_ = fqdn_resp->getDomainName();
  1038. // The FQDN has been processed successfully. Let's append it to the
  1039. // response to be sent to a client. Note that the Client FQDN option is
  1040. // always sent back to the client if Client FQDN was included in the
  1041. // client's message.
  1042. LOG_DEBUG(ddns6_logger, DBG_DHCP6_DETAIL, DHCP6_DDNS_RESPONSE_FQDN_DATA)
  1043. .arg(question->getLabel())
  1044. .arg(fqdn_resp->toText());
  1045. answer->addOption(fqdn_resp);
  1046. }
  1047. void
  1048. Dhcpv6Srv::createNameChangeRequests(const Pkt6Ptr& answer) {
  1049. // Don't create NameChangeRequests if DNS updates are disabled.
  1050. if (!CfgMgr::instance().ddnsEnabled()) {
  1051. return;
  1052. }
  1053. // The response message instance is always required. For instance it
  1054. // holds the Client Identifier. It is a programming error if supplied
  1055. // message is NULL.
  1056. if (!answer) {
  1057. isc_throw(isc::Unexpected, "an instance of the object"
  1058. << " encapsulating server's message must not be"
  1059. << " NULL when creating DNS NameChangeRequest");
  1060. }
  1061. // It is likely that client haven't included the FQDN option. In such case,
  1062. // FQDN option will be NULL. This is valid state, so we simply return.
  1063. Option6ClientFqdnPtr opt_fqdn = boost::dynamic_pointer_cast<
  1064. Option6ClientFqdn>(answer->getOption(D6O_CLIENT_FQDN));
  1065. if (!opt_fqdn) {
  1066. return;
  1067. }
  1068. // Get the update directions that should be performed based on our
  1069. // response FQDN flags.
  1070. bool do_fwd = false;
  1071. bool do_rev = false;
  1072. CfgMgr::instance().getD2ClientMgr().getUpdateDirections(*opt_fqdn,
  1073. do_fwd, do_rev);
  1074. if (!do_fwd && !do_rev) {
  1075. // Flags indicate there is Nothing to do, get out now.
  1076. // The Most likely scenario is that we are honoring the client's
  1077. // request that no updates be done.
  1078. return;
  1079. }
  1080. // Get the Client Id. It is mandatory and a function creating a response
  1081. // would have thrown an exception if it was missing. Thus throwing
  1082. // Unexpected if it is missing as it is a programming error.
  1083. OptionPtr opt_duid = answer->getOption(D6O_CLIENTID);
  1084. if (!opt_duid) {
  1085. isc_throw(isc::Unexpected,
  1086. "client identifier is required when creating a new"
  1087. " DNS NameChangeRequest");
  1088. }
  1089. DuidPtr duid = DuidPtr(new DUID(opt_duid->getData()));
  1090. // Get the FQDN in the on-wire format. It will be needed to compute
  1091. // DHCID.
  1092. OutputBuffer name_buf(1);
  1093. opt_fqdn->packDomainName(name_buf);
  1094. const uint8_t* name_data = static_cast<const uint8_t*>(name_buf.getData());
  1095. // @todo currently D2Dhcid accepts a vector holding FQDN.
  1096. // However, it will be faster if we used a pointer name_data.
  1097. std::vector<uint8_t> buf_vec(name_data, name_data + name_buf.getLength());
  1098. // Compute DHCID from Client Identifier and FQDN.
  1099. isc::dhcp_ddns::D2Dhcid dhcid(*duid, buf_vec);
  1100. // Get all IAs from the answer. For each IA, holding an address we will
  1101. // create a corresponding NameChangeRequest.
  1102. OptionCollection answer_ias = answer->getOptions(D6O_IA_NA);
  1103. for (OptionCollection::const_iterator answer_ia =
  1104. answer_ias.begin(); answer_ia != answer_ias.end(); ++answer_ia) {
  1105. /// @todo IA_NA may contain multiple addresses. We should process
  1106. /// each address individually. Currently we get only one.
  1107. Option6IAAddrPtr iaaddr = boost::static_pointer_cast<
  1108. Option6IAAddr>(answer_ia->second->getOption(D6O_IAADDR));
  1109. // We need an address to create a name-to-address mapping.
  1110. // If address is missing for any reason, go to the next IA.
  1111. if (!iaaddr) {
  1112. continue;
  1113. }
  1114. // Create new NameChangeRequest. Use the domain name from the FQDN.
  1115. // This is an FQDN included in the response to the client, so it
  1116. // holds a fully qualified domain-name already (not partial).
  1117. // Get the IP address from the lease.
  1118. NameChangeRequestPtr ncr;
  1119. ncr.reset(new NameChangeRequest(isc::dhcp_ddns::CHG_ADD,
  1120. do_fwd, do_rev,
  1121. opt_fqdn->getDomainName(),
  1122. iaaddr->getAddress().toText(),
  1123. dhcid, 0, iaaddr->getValid()));
  1124. LOG_DEBUG(ddns6_logger, DBG_DHCP6_DETAIL,
  1125. DHCP6_DDNS_CREATE_ADD_NAME_CHANGE_REQUEST).arg(ncr->toText());
  1126. // Post the NCR to the D2ClientMgr.
  1127. CfgMgr::instance().getD2ClientMgr().sendRequest(ncr);
  1128. /// @todo Currently we create NCR with the first IPv6 address that
  1129. /// is carried in one of the IA_NAs. In the future, the NCR API should
  1130. /// be extended to map multiple IPv6 addresses to a single FQDN.
  1131. /// In such case, this return statement will be removed.
  1132. return;
  1133. }
  1134. }
  1135. HWAddrPtr
  1136. Dhcpv6Srv::getMAC(const Pkt6Ptr& pkt) {
  1137. CfgMACSources mac_sources = CfgMgr::instance().getCurrentCfg()->
  1138. getMACSources().get();
  1139. HWAddrPtr hwaddr;
  1140. for (CfgMACSources::const_iterator it = mac_sources.begin();
  1141. it != mac_sources.end(); ++it) {
  1142. hwaddr = pkt->getMAC(*it);
  1143. if (hwaddr) {
  1144. return (hwaddr);
  1145. }
  1146. }
  1147. return (hwaddr);
  1148. }
  1149. OptionPtr
  1150. Dhcpv6Srv::assignIA_NA(const Pkt6Ptr& query, const Pkt6Ptr& answer,
  1151. AllocEngine::ClientContext6& orig_ctx,
  1152. boost::shared_ptr<Option6IA> ia) {
  1153. // Check if the client sent us a hint in his IA_NA. Clients may send an
  1154. // address in their IA_NA options as a suggestion (e.g. the last address
  1155. // they used before).
  1156. Option6IAAddrPtr hint_opt =
  1157. boost::dynamic_pointer_cast<Option6IAAddr>(ia->getOption(D6O_IAADDR));
  1158. IOAddress hint = IOAddress::IPV6_ZERO_ADDRESS();
  1159. if (hint_opt) {
  1160. hint = hint_opt->getAddress();
  1161. }
  1162. LOG_DEBUG(lease6_logger, DBG_DHCP6_DETAIL, DHCP6_PROCESS_IA_NA_REQUEST)
  1163. .arg(query->getLabel())
  1164. .arg(ia->getIAID())
  1165. .arg(hint_opt ? hint.toText() : "(no hint)");
  1166. // convenience values
  1167. const Subnet6Ptr& subnet = orig_ctx.subnet_;
  1168. const DuidPtr& duid = orig_ctx.duid_;
  1169. // If there is no subnet selected for handling this IA_NA, the only thing left to do is
  1170. // to say that we are sorry, but the user won't get an address. As a convenience, we
  1171. // use a different status text to indicate that (compare to the same status code,
  1172. // but different wording below)
  1173. if (!subnet) {
  1174. // Create an empty IA_NA option with IAID matching the request.
  1175. // Note that we don't use OptionDefinition class to create this option.
  1176. // This is because we prefer using a constructor of Option6IA that
  1177. // initializes IAID. Otherwise we would have to use setIAID() after
  1178. // creation of the option which has some performance implications.
  1179. boost::shared_ptr<Option6IA> ia_rsp(new Option6IA(D6O_IA_NA, ia->getIAID()));
  1180. // Insert status code NoAddrsAvail.
  1181. ia_rsp->addOption(createStatusCode(*query, *ia_rsp, STATUS_NoAddrsAvail,
  1182. "Server could not select subnet for"
  1183. " this client"));
  1184. return (ia_rsp);
  1185. }
  1186. // "Fake" allocation is the case when the server is processing the Solicit
  1187. // message without the Rapid Commit option and advertises a lease to
  1188. // the client, but doesn't commit this lease to the lease database. If
  1189. // the Solicit contains the Rapid Commit option and the server is
  1190. // configured to honor the Rapid Commit option, or the client has sent
  1191. // the Request message, the lease will be committed to the lease
  1192. // database. The type of the server's response may be used to determine
  1193. // if this is the fake allocation case or not. When the server sends
  1194. // Reply message it means that it is committing leases. Other message
  1195. // type (Advertise) means that server is not committing leases (fake
  1196. // allocation).
  1197. bool fake_allocation = (answer->getType() != DHCPV6_REPLY);
  1198. // Get DDNS update direction flags
  1199. bool do_fwd = false;
  1200. bool do_rev = false;
  1201. Option6ClientFqdnPtr fqdn = boost::dynamic_pointer_cast<
  1202. Option6ClientFqdn>(answer->getOption(D6O_CLIENT_FQDN));
  1203. if (fqdn) {
  1204. CfgMgr::instance().getD2ClientMgr().getUpdateDirections(*fqdn, do_fwd,
  1205. do_rev);
  1206. }
  1207. // Use allocation engine to pick a lease for this client. Allocation engine
  1208. // will try to honor the hint, but it is just a hint - some other address
  1209. // may be used instead. If fake_allocation is set to false, the lease will
  1210. // be inserted into the LeaseMgr as well.
  1211. AllocEngine::ClientContext6 ctx(subnet, duid, ia->getIAID(),
  1212. hint, Lease::TYPE_NA, do_fwd, do_rev,
  1213. orig_ctx.hostname_, fake_allocation);
  1214. ctx.callout_handle_ = getCalloutHandle(query);
  1215. ctx.hwaddr_ = orig_ctx.hwaddr_;
  1216. ctx.host_ = orig_ctx.host_;
  1217. ctx.query_ = orig_ctx.query_;
  1218. Lease6Collection leases = alloc_engine_->allocateLeases6(ctx);
  1219. /// @todo: Handle more than one lease
  1220. Lease6Ptr lease;
  1221. if (!leases.empty()) {
  1222. lease = *leases.begin();
  1223. }
  1224. // Create IA_NA that we will put in the response.
  1225. // Do not use OptionDefinition to create option's instance so
  1226. // as we can initialize IAID using a constructor.
  1227. Option6IAPtr ia_rsp(new Option6IA(D6O_IA_NA, ia->getIAID()));
  1228. if (lease) {
  1229. // We have a lease! Let's wrap its content into IA_NA option
  1230. // with IAADDR suboption.
  1231. LOG_DEBUG(lease6_logger, DBG_DHCP6_DETAIL, fake_allocation ?
  1232. DHCP6_LEASE_ADVERT : DHCP6_LEASE_ALLOC)
  1233. .arg(query->getLabel())
  1234. .arg(lease->addr_.toText())
  1235. .arg(ia->getIAID());
  1236. LOG_DEBUG(lease6_logger, DBG_DHCP6_DETAIL_DATA, DHCP6_LEASE_DATA)
  1237. .arg(query->getLabel())
  1238. .arg(ia->getIAID())
  1239. .arg(lease->toText());
  1240. ia_rsp->setT1(subnet->getT1());
  1241. ia_rsp->setT2(subnet->getT2());
  1242. Option6IAAddrPtr addr(new Option6IAAddr(D6O_IAADDR, lease->addr_,
  1243. lease->preferred_lft_,
  1244. lease->valid_lft_));
  1245. ia_rsp->addOption(addr);
  1246. // It would be possible to insert status code=0(success) as well,
  1247. // but this is considered waste of bandwidth as absence of status
  1248. // code is considered a success.
  1249. } else {
  1250. // Allocation engine did not allocate a lease. The engine logged
  1251. // cause of that failure. The only thing left is to insert
  1252. // status code to pass the sad news to the client.
  1253. LOG_DEBUG(lease6_logger, DBG_DHCP6_DETAIL, fake_allocation ?
  1254. DHCP6_LEASE_ADVERT_FAIL : DHCP6_LEASE_ALLOC_FAIL)
  1255. .arg(query->getLabel())
  1256. .arg(ia->getIAID());
  1257. ia_rsp->addOption(createStatusCode(*query, *ia_rsp,
  1258. STATUS_NoAddrsAvail,
  1259. "Sorry, no address could be"
  1260. " allocated."));
  1261. }
  1262. return (ia_rsp);
  1263. }
  1264. OptionPtr
  1265. Dhcpv6Srv::assignIA_PD(const Pkt6Ptr& query, const Pkt6Ptr& answer,
  1266. AllocEngine::ClientContext6& orig_ctx,
  1267. boost::shared_ptr<Option6IA> ia) {
  1268. // Check if the client sent us a hint in his IA_PD. Clients may send an
  1269. // address in their IA_PD options as a suggestion (e.g. the last address
  1270. // they used before). While the hint consists of a full prefix (prefix +
  1271. // length), getting just the prefix is sufficient to identify a lease.
  1272. Option6IAPrefixPtr hint_opt =
  1273. boost::dynamic_pointer_cast<Option6IAPrefix>(ia->getOption(D6O_IAPREFIX));
  1274. IOAddress hint = IOAddress::IPV6_ZERO_ADDRESS();
  1275. if (hint_opt) {
  1276. hint = hint_opt->getAddress();
  1277. }
  1278. LOG_DEBUG(lease6_logger, DBG_DHCP6_DETAIL, DHCP6_PROCESS_IA_PD_REQUEST)
  1279. .arg(query->getLabel())
  1280. .arg(ia->getIAID())
  1281. .arg(hint_opt ? hint.toText() : "(no hint)");
  1282. const Subnet6Ptr& subnet = orig_ctx.subnet_;
  1283. const DuidPtr& duid = orig_ctx.duid_;
  1284. // Create IA_PD that we will put in the response.
  1285. // Do not use OptionDefinition to create option's instance so
  1286. // as we can initialize IAID using a constructor.
  1287. boost::shared_ptr<Option6IA> ia_rsp(new Option6IA(D6O_IA_PD, ia->getIAID()));
  1288. // If there is no subnet selected for handling this IA_PD, the only thing
  1289. // left to do is to say that we are sorry, but the user won't get an address.
  1290. // As a convenience, we use a different status text to indicate that
  1291. // (compare to the same status code, but different wording below)
  1292. if (!subnet) {
  1293. // Insert status code NoAddrsAvail.
  1294. ia_rsp->addOption(createStatusCode(*query, *ia_rsp, STATUS_NoPrefixAvail,
  1295. "Sorry, no subnet available."));
  1296. return (ia_rsp);
  1297. }
  1298. // "Fake" allocation is the case when the server is processing the Solicit
  1299. // message without the Rapid Commit option and advertises a lease to
  1300. // the client, but doesn't commit this lease to the lease database. If
  1301. // the Solicit contains the Rapid Commit option and the server is
  1302. // configured to honor the Rapid Commit option, or the client has sent
  1303. // the Request message, the lease will be committed to the lease
  1304. // database. The type of the server's response may be used to determine
  1305. // if this is the fake allocation case or not. When the server sends
  1306. // Reply message it means that it is committing leases. Other message
  1307. // type (Advertise) means that server is not committing leases (fake
  1308. // allocation).
  1309. bool fake_allocation = (answer->getType() != DHCPV6_REPLY);
  1310. // Use allocation engine to pick a lease for this client. Allocation engine
  1311. // will try to honor the hint, but it is just a hint - some other address
  1312. // may be used instead. If fake_allocation is set to false, the lease will
  1313. // be inserted into the LeaseMgr as well.
  1314. AllocEngine::ClientContext6 ctx(subnet, duid, ia->getIAID(), hint, Lease::TYPE_PD,
  1315. false, false, string(), fake_allocation);
  1316. ctx.callout_handle_ = getCalloutHandle(query);
  1317. ctx.hwaddr_ = orig_ctx.hwaddr_;
  1318. ctx.host_ = orig_ctx.host_;
  1319. ctx.query_ = orig_ctx.query_;
  1320. Lease6Collection leases = alloc_engine_->allocateLeases6(ctx);
  1321. if (!leases.empty()) {
  1322. ia_rsp->setT1(subnet->getT1());
  1323. ia_rsp->setT2(subnet->getT2());
  1324. for (Lease6Collection::iterator l = leases.begin();
  1325. l != leases.end(); ++l) {
  1326. // We have a lease! Let's wrap its content into IA_PD option
  1327. // with IAADDR suboption.
  1328. LOG_DEBUG(lease6_logger, DBG_DHCP6_DETAIL, fake_allocation ?
  1329. DHCP6_PD_LEASE_ADVERT : DHCP6_PD_LEASE_ALLOC)
  1330. .arg(query->getLabel())
  1331. .arg((*l)->addr_.toText())
  1332. .arg(static_cast<int>((*l)->prefixlen_))
  1333. .arg(ia->getIAID());
  1334. boost::shared_ptr<Option6IAPrefix>
  1335. addr(new Option6IAPrefix(D6O_IAPREFIX, (*l)->addr_,
  1336. (*l)->prefixlen_, (*l)->preferred_lft_,
  1337. (*l)->valid_lft_));
  1338. ia_rsp->addOption(addr);
  1339. }
  1340. // It would be possible to insert status code=0(success) as well,
  1341. // but this is considered waste of bandwidth as absence of status
  1342. // code is considered a success.
  1343. } else {
  1344. // Allocation engine did not allocate a lease. The engine logged
  1345. // cause of that failure. The only thing left is to insert
  1346. // status code to pass the sad news to the client.
  1347. LOG_DEBUG(lease6_logger, DBG_DHCP6_DETAIL, fake_allocation ?
  1348. DHCP6_PD_LEASE_ADVERT_FAIL : DHCP6_PD_LEASE_ALLOC_FAIL)
  1349. .arg(query->getLabel())
  1350. .arg(ia->getIAID());
  1351. ia_rsp->addOption(createStatusCode(*query, *ia_rsp,
  1352. STATUS_NoPrefixAvail,
  1353. "Sorry, no prefixes could"
  1354. " be allocated."));
  1355. }
  1356. return (ia_rsp);
  1357. }
  1358. OptionPtr
  1359. Dhcpv6Srv::extendIA_NA(const Pkt6Ptr& query, const Pkt6Ptr& answer,
  1360. AllocEngine::ClientContext6& orig_ctx,
  1361. boost::shared_ptr<Option6IA> ia) {
  1362. LOG_DEBUG(lease6_logger, DBG_DHCP6_DETAIL, DHCP6_PROCESS_IA_NA_EXTEND)
  1363. .arg(query->getLabel())
  1364. .arg(ia->getIAID());
  1365. // convenience values
  1366. const Subnet6Ptr& subnet = orig_ctx.subnet_;
  1367. const DuidPtr& duid = orig_ctx.duid_;
  1368. // Create empty IA_NA option with IAID matching the request.
  1369. Option6IAPtr ia_rsp(new Option6IA(D6O_IA_NA, ia->getIAID()));
  1370. if (!subnet) {
  1371. /// @todo For simplicity and due to limitations of LeaseMgr we don't
  1372. /// get the binding for the client for which we don't get subnet id.
  1373. /// Subnet id is a required value when searching for the bindings.
  1374. /// The fact that we can't identify the subnet for the returning client
  1375. /// doesn't really mean that the client has no binding. It is possible
  1376. /// that due to server's reconfiguration the subnet has been removed
  1377. /// or modified since the client has got his lease. We may need to
  1378. /// rethink whether it is appropriate to send no binding if the subnet
  1379. /// hasn't been found for the client.
  1380. ia_rsp->addOption(createStatusCode(*query, *ia_rsp, STATUS_NoBinding,
  1381. "Sorry, no known leases for this duid/iaid."));
  1382. return (ia_rsp);
  1383. }
  1384. // Set up T1, T2 timers
  1385. ia_rsp->setT1(subnet->getT1());
  1386. ia_rsp->setT2(subnet->getT2());
  1387. // Get DDNS udpate directions
  1388. bool do_fwd = false;
  1389. bool do_rev = false;
  1390. Option6ClientFqdnPtr fqdn = boost::dynamic_pointer_cast<
  1391. Option6ClientFqdn>(answer->getOption(D6O_CLIENT_FQDN));
  1392. if (fqdn) {
  1393. CfgMgr::instance().getD2ClientMgr().getUpdateDirections(*fqdn,
  1394. do_fwd, do_rev);
  1395. }
  1396. // Create client context for this renewal
  1397. AllocEngine::ClientContext6 ctx(subnet, duid, ia->getIAID(),
  1398. IOAddress::IPV6_ZERO_ADDRESS(), Lease::TYPE_NA,
  1399. do_fwd, do_rev, orig_ctx.hostname_, false);
  1400. ctx.callout_handle_ = getCalloutHandle(query);
  1401. ctx.query_ = query;
  1402. ctx.ia_rsp_ = ia_rsp;
  1403. ctx.hwaddr_ = orig_ctx.hwaddr_;
  1404. ctx.host_ = orig_ctx.host_;
  1405. // Extract the addresses that the client is trying to obtain.
  1406. OptionCollection addrs = ia->getOptions();
  1407. for (OptionCollection::const_iterator it = addrs.begin();
  1408. it != addrs.end(); ++it) {
  1409. if (it->second->getType() != D6O_IAADDR) {
  1410. continue;
  1411. }
  1412. Option6IAAddrPtr iaaddr = boost::dynamic_pointer_cast<Option6IAAddr>(it->second);
  1413. if (!iaaddr) {
  1414. // That's weird. Option code was ok, but the object type was not.
  1415. // As we use Dhcpv6Srv::unpackOptions() that is guaranteed to use
  1416. // Option6IAAddr for D6O_IAADDR, this should never happen. The only
  1417. // case would be with badly mis-implemented hook libraries that
  1418. // insert invalid option objects. There's no way to protect against
  1419. // this.
  1420. continue;
  1421. }
  1422. ctx.hints_.push_back(make_pair(iaaddr->getAddress(), 128));
  1423. }
  1424. Lease6Collection leases = alloc_engine_->renewLeases6(ctx);
  1425. // Ok, now we have the leases extended. We have:
  1426. // - what the client tried to renew in ctx.hints_
  1427. // - what we actually assigned in leases
  1428. // - old leases that are no longer valid in ctx.old_leases_
  1429. // For all leases we have now, add the IAADDR with non-zero lifetimes.
  1430. for (Lease6Collection::const_iterator l = leases.begin(); l != leases.end(); ++l) {
  1431. Option6IAAddrPtr iaaddr(new Option6IAAddr(D6O_IAADDR,
  1432. (*l)->addr_, (*l)->preferred_lft_, (*l)->valid_lft_));
  1433. ia_rsp->addOption(iaaddr);
  1434. // Now remove this address from the hints list.
  1435. AllocEngine::HintType tmp((*l)->addr_, 128);
  1436. ctx.hints_.erase(std::remove(ctx.hints_.begin(), ctx.hints_.end(), tmp),
  1437. ctx.hints_.end());
  1438. }
  1439. // For the leases that we just retired, send the addresses with 0 lifetimes.
  1440. for (Lease6Collection::const_iterator l = ctx.old_leases_.begin();
  1441. l != ctx.old_leases_.end(); ++l) {
  1442. Option6IAAddrPtr iaaddr(new Option6IAAddr(D6O_IAADDR,
  1443. (*l)->addr_, 0, 0));
  1444. ia_rsp->addOption(iaaddr);
  1445. // Now remove this address from the hints list.
  1446. AllocEngine::HintType tmp((*l)->addr_, 128);
  1447. ctx.hints_.erase(std::remove(ctx.hints_.begin(), ctx.hints_.end(), tmp),
  1448. ctx.hints_.end());
  1449. // If the new FQDN settings have changed for the lease, we need to
  1450. // delete any existing FQDN records for this lease.
  1451. if (((*l)->hostname_ != ctx.hostname_) || ((*l)->fqdn_fwd_ != do_fwd) ||
  1452. ((*l)->fqdn_rev_ != do_rev)) {
  1453. LOG_DEBUG(ddns6_logger, DBG_DHCP6_DETAIL,
  1454. DHCP6_DDNS_LEASE_RENEW_FQDN_CHANGE)
  1455. .arg(query->getLabel())
  1456. .arg((*l)->toText())
  1457. .arg(ctx.hostname_)
  1458. .arg(do_rev ? "true" : "false")
  1459. .arg(do_fwd ? "true" : "false");
  1460. queueNCR(CHG_REMOVE, *l);
  1461. }
  1462. }
  1463. // Finally, if there are any addresses requested that we haven't dealt with
  1464. // already, inform the client that he can't have them.
  1465. for (AllocEngine::HintContainer::const_iterator hint = ctx.hints_.begin();
  1466. hint != ctx.hints_.end(); ++hint) {
  1467. Option6IAAddrPtr iaaddr(new Option6IAAddr(D6O_IAADDR,
  1468. hint->first, 0, 0));
  1469. ia_rsp->addOption(iaaddr);
  1470. }
  1471. // All is left is to insert the status code.
  1472. if (leases.empty()) {
  1473. // The server wasn't able allocate new lease and renew an exising
  1474. // lease. In that case, the server sends NoAddrsAvail per RFC7550.
  1475. ia_rsp->addOption(createStatusCode(*query, *ia_rsp,
  1476. STATUS_NoAddrsAvail,
  1477. "Sorry, no addresses could be"
  1478. " assigned at this time."));
  1479. }
  1480. return (ia_rsp);
  1481. }
  1482. OptionPtr
  1483. Dhcpv6Srv::extendIA_PD(const Pkt6Ptr& query,
  1484. AllocEngine::ClientContext6& orig_ctx,
  1485. boost::shared_ptr<Option6IA> ia) {
  1486. LOG_DEBUG(lease6_logger, DBG_DHCP6_DETAIL, DHCP6_PROCESS_IA_PD_EXTEND)
  1487. .arg(query->getLabel())
  1488. .arg(ia->getIAID());
  1489. const Subnet6Ptr& subnet = orig_ctx.subnet_;
  1490. const DuidPtr& duid = orig_ctx.duid_;
  1491. // Let's create a IA_PD response and fill it in later
  1492. Option6IAPtr ia_rsp(new Option6IA(D6O_IA_PD, ia->getIAID()));
  1493. // If there is no subnet for the particular client, we can't retrieve
  1494. // information about client's leases from lease database. We treat this
  1495. // as no binding for the client.
  1496. if (!subnet) {
  1497. // Per RFC3633, section 12.2, if there is no binding and we are
  1498. // processing a Renew, the NoBinding status code should be returned.
  1499. if (query->getType() == DHCPV6_RENEW) {
  1500. // Insert status code NoBinding
  1501. ia_rsp->addOption(createStatusCode(*query, *ia_rsp, STATUS_NoBinding,
  1502. "Sorry, no known PD leases"
  1503. " for this duid/iaid."));
  1504. return (ia_rsp);
  1505. // Per RFC3633, section 12.2, if there is no binding and we are
  1506. // processing Rebind, the message has to be discarded (assuming that
  1507. // the server doesn't know if the prefix in the IA_PD option is
  1508. // appropriate for the client's link). The exception being thrown
  1509. // here should propagate to the main loop and cause the message to
  1510. // be discarded.
  1511. } else {
  1512. /// @todo: RFC3315bis will probably change that behavior. Client
  1513. /// may rebind prefixes and addresses at the same time.
  1514. isc_throw(DHCPv6DiscardMessageError, "no subnet found for the"
  1515. " client sending Rebind to extend lifetime of the"
  1516. " prefix (DUID=" << duid->toText() << ", IAID="
  1517. << ia->getIAID() << ")");
  1518. }
  1519. }
  1520. // Set up T1, T2 timers
  1521. ia_rsp->setT1(subnet->getT1());
  1522. ia_rsp->setT2(subnet->getT2());
  1523. // Create client context for this renewal
  1524. static const IOAddress none("::");
  1525. AllocEngine::ClientContext6 ctx(subnet, duid, ia->getIAID(), none,
  1526. Lease::TYPE_PD, false, false, string(""),
  1527. false);
  1528. ctx.callout_handle_ = getCalloutHandle(query);
  1529. ctx.query_ = query;
  1530. ctx.ia_rsp_ = ia_rsp;
  1531. ctx.hwaddr_ = orig_ctx.hwaddr_;
  1532. ctx.host_ = orig_ctx.host_;
  1533. // Extract prefixes that the client is trying to renew.
  1534. OptionCollection addrs = ia->getOptions();
  1535. for (OptionCollection::const_iterator it = addrs.begin();
  1536. it != addrs.end(); ++it) {
  1537. if (it->second->getType() != D6O_IAPREFIX) {
  1538. continue;
  1539. }
  1540. Option6IAPrefixPtr prf = boost::dynamic_pointer_cast<Option6IAPrefix>(it->second);
  1541. if (!prf) {
  1542. // That's weird. Option code was ok, but the object type was not.
  1543. // As we use Dhcpv6Srv::unpackOptions() that is guaranteed to use
  1544. // Option6IAPrefix for D6O_IAPREFIX, this should never happen. The only
  1545. // case would be with badly mis-implemented hook libraries that
  1546. // insert invalid option objects. There's no way to protect against
  1547. // this.
  1548. continue;
  1549. }
  1550. // Put the client's prefix into the hints list.
  1551. ctx.hints_.push_back(make_pair(prf->getAddress(), prf->getLength()));
  1552. }
  1553. // Call Allocation Engine and attempt to renew leases. Number of things
  1554. // may happen. Leases may be extended, revoked (if the lease is no longer
  1555. // valid or reserved for someone else), or new leases may be added.
  1556. // Important parameters are:
  1557. // - returned container - current valid leases
  1558. // - old_leases - leases that used to be, but are no longer valid
  1559. // - changed_leases - leases that have FQDN changed (not really important
  1560. // in PD context)
  1561. Lease6Collection leases = alloc_engine_->renewLeases6(ctx);
  1562. // For all the leases we have now, add the IAPPREFIX with non-zero lifetimes
  1563. for (Lease6Collection::const_iterator l = leases.begin(); l != leases.end(); ++l) {
  1564. Option6IAPrefixPtr prf(new Option6IAPrefix(D6O_IAPREFIX,
  1565. (*l)->addr_, (*l)->prefixlen_,
  1566. (*l)->preferred_lft_, (*l)->valid_lft_));
  1567. ia_rsp->addOption(prf);
  1568. // Now remove this address from the hints list.
  1569. AllocEngine::HintType tmp((*l)->addr_, (*l)->prefixlen_);
  1570. ctx.hints_.erase(std::remove(ctx.hints_.begin(), ctx.hints_.end(), tmp),
  1571. ctx.hints_.end());
  1572. }
  1573. /// @todo: Maybe we should iterate over ctx.old_leases_, i.e. the leases
  1574. /// that used to be valid, but they are not anymore.
  1575. // For all the leases the client had requested, but we didn't assign, put them with
  1576. // zero lifetimes
  1577. // Finally, if there are any addresses requested that we haven't dealt with
  1578. // already, inform the client that he can't have them.
  1579. for (AllocEngine::HintContainer::const_iterator prefix = ctx.hints_.begin();
  1580. prefix != ctx.hints_.end(); ++prefix) {
  1581. // Send the prefix with the zero lifetimes only if the prefix
  1582. // contains non-zero value. A zero value indicates that the hint was
  1583. // for the prefix length.
  1584. if (!prefix->first.isV6Zero()) {
  1585. OptionPtr prefix_opt(new Option6IAPrefix(D6O_IAPREFIX, prefix->first,
  1586. prefix->second, 0, 0));
  1587. ia_rsp->addOption(prefix_opt);
  1588. }
  1589. }
  1590. // All is left is to insert the status code.
  1591. if (leases.empty()) {
  1592. // The server wasn't able allocate new lease and renew an exising
  1593. // lease. In that case, the server sends NoPrefixAvail per RFC7550.
  1594. ia_rsp->addOption(createStatusCode(*query, *ia_rsp,
  1595. STATUS_NoPrefixAvail,
  1596. "Sorry, no prefixes could be"
  1597. " assigned at this time."));
  1598. }
  1599. return (ia_rsp);
  1600. }
  1601. void
  1602. Dhcpv6Srv::extendLeases(const Pkt6Ptr& query, Pkt6Ptr& reply,
  1603. AllocEngine::ClientContext6& ctx) {
  1604. // We will try to extend lease lifetime for all IA options in the client's
  1605. // Renew or Rebind message.
  1606. /// @todo add support for IA_TA
  1607. // For the lease extension it is critical that the client has sent
  1608. // DUID. There is no need to check for the presence of the DUID here
  1609. // because we have already checked it in the sanityCheck().
  1610. for (OptionCollection::iterator opt = query->options_.begin();
  1611. opt != query->options_.end(); ++opt) {
  1612. switch (opt->second->getType()) {
  1613. case D6O_IA_NA: {
  1614. OptionPtr answer_opt = extendIA_NA(query, reply, ctx,
  1615. boost::dynamic_pointer_cast<
  1616. Option6IA>(opt->second));
  1617. if (answer_opt) {
  1618. reply->addOption(answer_opt);
  1619. }
  1620. break;
  1621. }
  1622. case D6O_IA_PD: {
  1623. OptionPtr answer_opt = extendIA_PD(query, ctx,
  1624. boost::dynamic_pointer_cast<
  1625. Option6IA>(opt->second));
  1626. if (answer_opt) {
  1627. reply->addOption(answer_opt);
  1628. }
  1629. break;
  1630. }
  1631. default:
  1632. break;
  1633. }
  1634. }
  1635. }
  1636. void
  1637. Dhcpv6Srv::releaseLeases(const Pkt6Ptr& release, Pkt6Ptr& reply,
  1638. AllocEngine::ClientContext6& ctx) {
  1639. // We need to release addresses for all IA_NA options in the client's
  1640. // RELEASE message.
  1641. // @todo Add support for IA_TA
  1642. // @todo Add support for IA_PD
  1643. // @todo Consider supporting more than one address in a single IA_NA.
  1644. // That was envisaged by RFC3315, but it never happened. The only
  1645. // software that supports that is Dibbler, but its author seriously doubts
  1646. // if anyone is really using it. Clients that want more than one address
  1647. // just include more instances of IA_NA options.
  1648. // Let's set the status to be success by default. We can override it with
  1649. // error status if needed. The important thing to understand here is that
  1650. // the global status code may be set to success only if all IA options were
  1651. // handled properly. Therefore the releaseIA_NA and releaseIA_PD options
  1652. // may turn the status code to some error, but can't turn it back to success.
  1653. int general_status = STATUS_Success;
  1654. for (OptionCollection::iterator opt = release->options_.begin();
  1655. opt != release->options_.end(); ++opt) {
  1656. switch (opt->second->getType()) {
  1657. case D6O_IA_NA: {
  1658. OptionPtr answer_opt = releaseIA_NA(ctx.duid_, release, general_status,
  1659. boost::dynamic_pointer_cast<Option6IA>(opt->second));
  1660. if (answer_opt) {
  1661. reply->addOption(answer_opt);
  1662. }
  1663. break;
  1664. }
  1665. case D6O_IA_PD: {
  1666. OptionPtr answer_opt = releaseIA_PD(ctx.duid_, release, general_status,
  1667. boost::dynamic_pointer_cast<Option6IA>(opt->second));
  1668. if (answer_opt) {
  1669. reply->addOption(answer_opt);
  1670. }
  1671. break;
  1672. }
  1673. // @todo: add support for IA_TA
  1674. default:
  1675. // remaining options are stateless and thus ignored in this context
  1676. ;
  1677. }
  1678. }
  1679. // To be pedantic, we should also include status code in the top-level
  1680. // scope, not just in each IA_NA. See RFC3315, section 18.2.6.
  1681. // This behavior will likely go away in RFC3315bis.
  1682. reply->addOption(createStatusCode(*release, general_status,
  1683. "Summary status for all processed IA_NAs"));
  1684. }
  1685. OptionPtr
  1686. Dhcpv6Srv::releaseIA_NA(const DuidPtr& duid, const Pkt6Ptr& query,
  1687. int& general_status, boost::shared_ptr<Option6IA> ia) {
  1688. LOG_DEBUG(lease6_logger, DBG_DHCP6_DETAIL, DHCP6_PROCESS_IA_NA_RELEASE)
  1689. .arg(query->getLabel())
  1690. .arg(ia->getIAID());
  1691. // Release can be done in one of two ways:
  1692. // Approach 1: extract address from client's IA_NA and see if it belongs
  1693. // to this particular client.
  1694. // Approach 2: find a subnet for this client, get a lease for
  1695. // this subnet/duid/iaid and check if its content matches to what the
  1696. // client is asking us to release.
  1697. //
  1698. // This method implements approach 1.
  1699. // That's our response
  1700. boost::shared_ptr<Option6IA> ia_rsp(new Option6IA(D6O_IA_NA, ia->getIAID()));
  1701. Option6IAAddrPtr release_addr = boost::dynamic_pointer_cast<Option6IAAddr>
  1702. (ia->getOption(D6O_IAADDR));
  1703. if (!release_addr) {
  1704. ia_rsp->addOption(createStatusCode(*query, STATUS_NoBinding,
  1705. "You did not include an address in your RELEASE"));
  1706. general_status = STATUS_NoBinding;
  1707. return (ia_rsp);
  1708. }
  1709. Lease6Ptr lease = LeaseMgrFactory::instance().getLease6(Lease::TYPE_NA,
  1710. release_addr->getAddress());
  1711. if (!lease) {
  1712. // client releasing a lease that we don't know about.
  1713. // Insert status code NoBinding.
  1714. ia_rsp->addOption(createStatusCode(*query, *ia_rsp, STATUS_NoBinding,
  1715. "Sorry, no known leases for this duid/iaid, can't release."));
  1716. general_status = STATUS_NoBinding;
  1717. return (ia_rsp);
  1718. }
  1719. if (!lease->duid_) {
  1720. // Something is gravely wrong here. We do have a lease, but it does not
  1721. // have mandatory DUID information attached. Someone was messing with our
  1722. // database.
  1723. LOG_ERROR(lease6_logger, DHCP6_LEASE_NA_WITHOUT_DUID)
  1724. .arg(query->getLabel())
  1725. .arg(release_addr->getAddress().toText());
  1726. general_status = STATUS_UnspecFail;
  1727. ia_rsp->addOption(createStatusCode(*query, *ia_rsp, STATUS_UnspecFail,
  1728. "Database consistency check failed when trying to RELEASE"));
  1729. return (ia_rsp);
  1730. }
  1731. if (*duid != *(lease->duid_)) {
  1732. // Sorry, it's not your address. You can't release it.
  1733. LOG_INFO(lease6_logger, DHCP6_RELEASE_NA_FAIL_WRONG_DUID)
  1734. .arg(query->getLabel())
  1735. .arg(release_addr->getAddress().toText())
  1736. .arg(lease->duid_->toText());
  1737. general_status = STATUS_NoBinding;
  1738. ia_rsp->addOption(createStatusCode(*query, *ia_rsp, STATUS_NoBinding,
  1739. "This address does not belong to you, you can't release it"));
  1740. return (ia_rsp);
  1741. }
  1742. if (ia->getIAID() != lease->iaid_) {
  1743. // This address belongs to this client, but to a different IA
  1744. LOG_WARN(lease6_logger, DHCP6_RELEASE_NA_FAIL_WRONG_IAID)
  1745. .arg(query->getLabel())
  1746. .arg(release_addr->getAddress().toText())
  1747. .arg(lease->iaid_)
  1748. .arg(ia->getIAID());
  1749. ia_rsp->addOption(createStatusCode(*query, *ia_rsp, STATUS_NoBinding,
  1750. "This is your address, but you used wrong IAID"));
  1751. general_status = STATUS_NoBinding;
  1752. return (ia_rsp);
  1753. }
  1754. // It is not necessary to check if the address matches as we used
  1755. // getLease6(addr) method that is supposed to return a proper lease.
  1756. bool skip = false;
  1757. // Execute all callouts registered for packet6_send
  1758. if (HooksManager::calloutsPresent(Hooks.hook_index_lease6_release_)) {
  1759. CalloutHandlePtr callout_handle = getCalloutHandle(query);
  1760. // Delete all previous arguments
  1761. callout_handle->deleteAllArguments();
  1762. // Pass the original packet
  1763. callout_handle->setArgument("query6", query);
  1764. // Pass the lease to be updated
  1765. callout_handle->setArgument("lease6", lease);
  1766. // Call all installed callouts
  1767. HooksManager::callCallouts(Hooks.hook_index_lease6_release_, *callout_handle);
  1768. // Callouts decided to skip the next processing step. The next
  1769. // processing step would to send the packet, so skip at this
  1770. // stage means "drop response".
  1771. if (callout_handle->getStatus() == CalloutHandle::NEXT_STEP_SKIP) {
  1772. skip = true;
  1773. LOG_DEBUG(hooks_logger, DBG_DHCP6_HOOKS, DHCP6_HOOK_LEASE6_RELEASE_NA_SKIP)
  1774. .arg(query->getLabel());
  1775. }
  1776. /// @todo: Add support for DROP status
  1777. }
  1778. // Ok, we've passed all checks. Let's release this address.
  1779. bool success = false; // was the removal operation successful?
  1780. if (!skip) {
  1781. success = LeaseMgrFactory::instance().deleteLease(lease->addr_);
  1782. }
  1783. // Here the success should be true if we removed lease successfully
  1784. // and false if skip flag was set or the removal failed for whatever reason
  1785. if (!success) {
  1786. ia_rsp->addOption(createStatusCode(*query, *ia_rsp, STATUS_UnspecFail,
  1787. "Server failed to release a lease"));
  1788. LOG_ERROR(lease6_logger, DHCP6_RELEASE_NA_FAIL)
  1789. .arg(query->getLabel())
  1790. .arg(lease->addr_.toText())
  1791. .arg(lease->iaid_);
  1792. general_status = STATUS_UnspecFail;
  1793. return (ia_rsp);
  1794. } else {
  1795. LOG_DEBUG(lease6_logger, DBG_DHCP6_DETAIL, DHCP6_RELEASE_NA)
  1796. .arg(query->getLabel())
  1797. .arg(lease->addr_.toText())
  1798. .arg(lease->iaid_);
  1799. ia_rsp->addOption(createStatusCode(*query, *ia_rsp, STATUS_Success,
  1800. "Lease released. Thank you, please come again."));
  1801. // Need to decrease statistic for assigned addresses.
  1802. StatsMgr::instance().addValue(
  1803. StatsMgr::generateName("subnet", lease->subnet_id_, "assigned-nas"),
  1804. static_cast<int64_t>(-1));
  1805. // Check if a lease has flags indicating that the FQDN update has
  1806. // been performed. If so, create NameChangeRequest which removes
  1807. // the entries.
  1808. queueNCR(CHG_REMOVE, lease);
  1809. return (ia_rsp);
  1810. }
  1811. }
  1812. OptionPtr
  1813. Dhcpv6Srv::releaseIA_PD(const DuidPtr& duid, const Pkt6Ptr& query,
  1814. int& general_status, boost::shared_ptr<Option6IA> ia) {
  1815. // Release can be done in one of two ways:
  1816. // Approach 1: extract address from client's IA_NA and see if it belongs
  1817. // to this particular client.
  1818. // Approach 2: find a subnet for this client, get a lease for
  1819. // this subnet/duid/iaid and check if its content matches to what the
  1820. // client is asking us to release.
  1821. //
  1822. // This method implements approach 1.
  1823. // That's our response. We will fill it in as we check the lease to be
  1824. // released.
  1825. boost::shared_ptr<Option6IA> ia_rsp(new Option6IA(D6O_IA_PD, ia->getIAID()));
  1826. boost::shared_ptr<Option6IAPrefix> release_prefix =
  1827. boost::dynamic_pointer_cast<Option6IAPrefix>(ia->getOption(D6O_IAPREFIX));
  1828. if (!release_prefix) {
  1829. ia_rsp->addOption(createStatusCode(*query, *ia_rsp, STATUS_NoBinding,
  1830. "You did not include a prefix in your RELEASE"));
  1831. general_status = STATUS_NoBinding;
  1832. return (ia_rsp);
  1833. }
  1834. Lease6Ptr lease = LeaseMgrFactory::instance().getLease6(Lease::TYPE_PD,
  1835. release_prefix->getAddress());
  1836. if (!lease) {
  1837. // Client releasing a lease that we don't know about.
  1838. // Insert status code NoBinding.
  1839. ia_rsp->addOption(createStatusCode(*query, *ia_rsp, STATUS_NoBinding,
  1840. "Sorry, no known leases for this duid/iaid, can't release."));
  1841. general_status = STATUS_NoBinding;
  1842. return (ia_rsp);
  1843. }
  1844. if (!lease->duid_) {
  1845. // Something is gravely wrong here. We do have a lease, but it does not
  1846. // have mandatory DUID information attached. Someone was messing with our
  1847. // database.
  1848. LOG_ERROR(lease6_logger, DHCP6_LEASE_PD_WITHOUT_DUID)
  1849. .arg(query->getLabel())
  1850. .arg(release_prefix->getAddress().toText())
  1851. .arg(static_cast<int>(release_prefix->getLength()));
  1852. general_status = STATUS_UnspecFail;
  1853. ia_rsp->addOption(createStatusCode(*query, *ia_rsp, STATUS_UnspecFail,
  1854. "Database consistency check failed when trying to RELEASE"));
  1855. return (ia_rsp);
  1856. }
  1857. if (*duid != *(lease->duid_)) {
  1858. // Sorry, it's not your address. You can't release it.
  1859. LOG_INFO(lease6_logger, DHCP6_RELEASE_PD_FAIL_WRONG_DUID)
  1860. .arg(query->getLabel())
  1861. .arg(release_prefix->getAddress().toText())
  1862. .arg(static_cast<int>(release_prefix->getLength()))
  1863. .arg(lease->duid_->toText());
  1864. general_status = STATUS_NoBinding;
  1865. ia_rsp->addOption(createStatusCode(*query, *ia_rsp, STATUS_NoBinding,
  1866. "This address does not belong to you, you can't release it"));
  1867. return (ia_rsp);
  1868. }
  1869. if (ia->getIAID() != lease->iaid_) {
  1870. // This address belongs to this client, but to a different IA
  1871. LOG_WARN(lease6_logger, DHCP6_RELEASE_PD_FAIL_WRONG_IAID)
  1872. .arg(query->getLabel())
  1873. .arg(release_prefix->getAddress().toText())
  1874. .arg(static_cast<int>(release_prefix->getLength()))
  1875. .arg(lease->iaid_)
  1876. .arg(ia->getIAID());
  1877. ia_rsp->addOption(createStatusCode(*query, *ia_rsp, STATUS_NoBinding,
  1878. "This is your address, but you used wrong IAID"));
  1879. general_status = STATUS_NoBinding;
  1880. return (ia_rsp);
  1881. }
  1882. // It is not necessary to check if the address matches as we used
  1883. // getLease6(addr) method that is supposed to return a proper lease.
  1884. bool skip = false;
  1885. // Execute all callouts registered for packet6_send
  1886. if (HooksManager::calloutsPresent(Hooks.hook_index_lease6_release_)) {
  1887. CalloutHandlePtr callout_handle = getCalloutHandle(query);
  1888. // Delete all previous arguments
  1889. callout_handle->deleteAllArguments();
  1890. // Pass the original packet
  1891. callout_handle->setArgument("query6", query);
  1892. // Pass the lease to be updated
  1893. callout_handle->setArgument("lease6", lease);
  1894. // Call all installed callouts
  1895. HooksManager::callCallouts(Hooks.hook_index_lease6_release_, *callout_handle);
  1896. skip = callout_handle->getStatus() == CalloutHandle::NEXT_STEP_SKIP;
  1897. }
  1898. // Ok, we've passed all checks. Let's release this prefix.
  1899. bool success = false; // was the removal operation successful?
  1900. if (!skip) {
  1901. success = LeaseMgrFactory::instance().deleteLease(lease->addr_);
  1902. } else {
  1903. // Callouts decided to skip the next processing step. The next
  1904. // processing step would to send the packet, so skip at this
  1905. // stage means "drop response".
  1906. LOG_DEBUG(hooks_logger, DBG_DHCP6_HOOKS, DHCP6_HOOK_LEASE6_RELEASE_PD_SKIP)
  1907. .arg(query->getLabel());
  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(*query, *ia_rsp, STATUS_UnspecFail,
  1913. "Server failed to release a lease"));
  1914. LOG_ERROR(lease6_logger, DHCP6_RELEASE_PD_FAIL)
  1915. .arg(query->getLabel())
  1916. .arg(lease->addr_.toText())
  1917. .arg(static_cast<int>(lease->prefixlen_))
  1918. .arg(lease->iaid_);
  1919. general_status = STATUS_UnspecFail;
  1920. } else {
  1921. LOG_DEBUG(lease6_logger, DBG_DHCP6_DETAIL, DHCP6_RELEASE_PD)
  1922. .arg(query->getLabel())
  1923. .arg(lease->addr_.toText())
  1924. .arg(static_cast<int>(lease->prefixlen_))
  1925. .arg(lease->iaid_);
  1926. ia_rsp->addOption(createStatusCode(*query, *ia_rsp, STATUS_Success,
  1927. "Lease released. Thank you, please come again."));
  1928. // Need to decrease statistic for assigned prefixes.
  1929. StatsMgr::instance().addValue(
  1930. StatsMgr::generateName("subnet", lease->subnet_id_, "assigned-pds"),
  1931. static_cast<int64_t>(-1));
  1932. }
  1933. return (ia_rsp);
  1934. }
  1935. Pkt6Ptr
  1936. Dhcpv6Srv::processSolicit(const Pkt6Ptr& solicit) {
  1937. sanityCheck(solicit, MANDATORY, FORBIDDEN);
  1938. // Let's create a simplified client context here.
  1939. AllocEngine::ClientContext6 ctx = createContext(solicit);
  1940. Pkt6Ptr response(new Pkt6(DHCPV6_ADVERTISE, solicit->getTransid()));
  1941. // Handle Rapid Commit option, if present.
  1942. if (ctx.subnet_ && ctx.subnet_->getRapidCommit()) {
  1943. OptionPtr opt_rapid_commit = solicit->getOption(D6O_RAPID_COMMIT);
  1944. if (opt_rapid_commit) {
  1945. LOG_DEBUG(options6_logger, DBG_DHCP6_DETAIL, DHCP6_RAPID_COMMIT)
  1946. .arg(solicit->getLabel());
  1947. // If Rapid Commit has been sent by the client, change the
  1948. // response type to Reply and include Rapid Commit option.
  1949. response->setType(DHCPV6_REPLY);
  1950. response->addOption(opt_rapid_commit);
  1951. }
  1952. }
  1953. copyClientOptions(solicit, response);
  1954. CfgOptionList co_list;
  1955. buildCfgOptionList(solicit, ctx, co_list);
  1956. appendDefaultOptions(solicit, response, co_list);
  1957. appendRequestedOptions(solicit, response, ctx, co_list);
  1958. appendRequestedVendorOptions(solicit, response, ctx, co_list);
  1959. processClientFqdn(solicit, response, ctx);
  1960. assignLeases(solicit, response, ctx);
  1961. // Only generate name change requests if sending a Reply as a result
  1962. // of receiving Rapid Commit option.
  1963. if (response->getType() == DHCPV6_REPLY) {
  1964. createNameChangeRequests(response);
  1965. }
  1966. return (response);
  1967. }
  1968. Pkt6Ptr
  1969. Dhcpv6Srv::processRequest(const Pkt6Ptr& request) {
  1970. sanityCheck(request, MANDATORY, MANDATORY);
  1971. // Let's create a simplified client context here.
  1972. AllocEngine::ClientContext6 ctx = createContext(request);
  1973. Pkt6Ptr reply(new Pkt6(DHCPV6_REPLY, request->getTransid()));
  1974. copyClientOptions(request, reply);
  1975. CfgOptionList co_list;
  1976. buildCfgOptionList(request, ctx, co_list);
  1977. appendDefaultOptions(request, reply, co_list);
  1978. appendRequestedOptions(request, reply, ctx, co_list);
  1979. appendRequestedVendorOptions(request, reply, ctx, co_list);
  1980. processClientFqdn(request, reply, ctx);
  1981. assignLeases(request, reply, ctx);
  1982. generateFqdn(reply);
  1983. createNameChangeRequests(reply);
  1984. return (reply);
  1985. }
  1986. Pkt6Ptr
  1987. Dhcpv6Srv::processRenew(const Pkt6Ptr& renew) {
  1988. sanityCheck(renew, MANDATORY, MANDATORY);
  1989. // Let's create a simplified client context here.
  1990. AllocEngine::ClientContext6 ctx = createContext(renew);
  1991. Pkt6Ptr reply(new Pkt6(DHCPV6_REPLY, renew->getTransid()));
  1992. copyClientOptions(renew, reply);
  1993. CfgOptionList co_list;
  1994. buildCfgOptionList(renew, ctx, co_list);
  1995. appendDefaultOptions(renew, reply, co_list);
  1996. appendRequestedOptions(renew, reply, ctx, co_list);
  1997. processClientFqdn(renew, reply, ctx);
  1998. extendLeases(renew, reply, ctx);
  1999. generateFqdn(reply);
  2000. createNameChangeRequests(reply);
  2001. return (reply);
  2002. }
  2003. Pkt6Ptr
  2004. Dhcpv6Srv::processRebind(const Pkt6Ptr& rebind) {
  2005. sanityCheck(rebind, MANDATORY, FORBIDDEN);
  2006. // Let's create a simplified client context here.
  2007. AllocEngine::ClientContext6 ctx = createContext(rebind);
  2008. Pkt6Ptr reply(new Pkt6(DHCPV6_REPLY, rebind->getTransid()));
  2009. copyClientOptions(rebind, reply);
  2010. CfgOptionList co_list;
  2011. buildCfgOptionList(rebind, ctx, co_list);
  2012. appendDefaultOptions(rebind, reply, co_list);
  2013. appendRequestedOptions(rebind, reply, ctx, co_list);
  2014. processClientFqdn(rebind, reply, ctx);
  2015. extendLeases(rebind, reply, ctx);
  2016. generateFqdn(reply);
  2017. createNameChangeRequests(rebind);
  2018. return (reply);
  2019. }
  2020. Pkt6Ptr
  2021. Dhcpv6Srv::processConfirm(const Pkt6Ptr& confirm) {
  2022. sanityCheck(confirm, MANDATORY, FORBIDDEN);
  2023. // Let's create a simplified client context here.
  2024. AllocEngine::ClientContext6 ctx = createContext(confirm);
  2025. // Get IA_NAs from the Confirm. If there are none, the message is
  2026. // invalid and must be discarded. There is nothing more to do.
  2027. OptionCollection ias = confirm->getOptions(D6O_IA_NA);
  2028. if (ias.empty()) {
  2029. return (Pkt6Ptr());
  2030. }
  2031. // The server sends Reply message in response to Confirm.
  2032. Pkt6Ptr reply(new Pkt6(DHCPV6_REPLY, confirm->getTransid()));
  2033. // Make sure that the necessary options are included.
  2034. copyClientOptions(confirm, reply);
  2035. CfgOptionList co_list;
  2036. buildCfgOptionList(confirm, ctx, co_list);
  2037. appendDefaultOptions(confirm, reply, co_list);
  2038. appendRequestedOptions(confirm, reply, ctx, co_list);
  2039. // Indicates if at least one address has been verified. If no addresses
  2040. // are verified it means that the client has sent no IA_NA options
  2041. // or no IAAddr options and that client's message has to be discarded.
  2042. bool verified = false;
  2043. // Check if subnet was selected for the message. If no subnet
  2044. // has been selected, the client is not on link.
  2045. SubnetPtr subnet = ctx.subnet_;
  2046. // Regardless if the subnet has been selected or not, we will iterate
  2047. // over the IA_NA options to check if they hold any addresses. If there
  2048. // are no, the Confirm is discarded.
  2049. // Check addresses in IA_NA options and make sure they are appropriate.
  2050. for (OptionCollection::const_iterator ia = ias.begin();
  2051. ia != ias.end(); ++ia) {
  2052. const OptionCollection& opts = ia->second->getOptions();
  2053. for (OptionCollection::const_iterator opt = opts.begin();
  2054. opt != opts.end(); ++opt) {
  2055. // Ignore options other than IAAddr.
  2056. if (opt->second->getType() == D6O_IAADDR) {
  2057. // Check that the address is in range in the subnet selected.
  2058. Option6IAAddrPtr iaaddr = boost::dynamic_pointer_cast<
  2059. Option6IAAddr>(opt->second);
  2060. // If there is subnet selected and the address has been included
  2061. // in IA_NA, mark it verified and verify that it belongs to the
  2062. // subnet.
  2063. if (iaaddr) {
  2064. // If at least one address is not in range, then return
  2065. // the NotOnLink status code.
  2066. if (subnet && !subnet->inRange(iaaddr->getAddress())) {
  2067. std::ostringstream status_msg;
  2068. status_msg << "Address " << iaaddr->getAddress()
  2069. << " is not on link.";
  2070. reply->addOption(createStatusCode(*confirm,
  2071. STATUS_NotOnLink,
  2072. status_msg.str()));
  2073. return (reply);
  2074. }
  2075. verified = true;
  2076. } else {
  2077. isc_throw(Unexpected, "failed to cast the IA Address option"
  2078. " to the Option6IAAddrPtr. This is programming"
  2079. " error and should be reported");
  2080. }
  2081. }
  2082. }
  2083. }
  2084. // It seems that the client hasn't included any addresses in which case
  2085. // the Confirm must be discarded.
  2086. if (!verified) {
  2087. return (Pkt6Ptr());
  2088. }
  2089. // If there is a subnet, there were addresses in IA_NA options and the
  2090. // addresses where consistent with the subnet then the client is on link.
  2091. if (subnet) {
  2092. // All addresses in range, so return success.
  2093. reply->addOption(createStatusCode(*confirm, STATUS_Success,
  2094. "All addresses are on-link"));
  2095. } else {
  2096. reply->addOption(createStatusCode(*confirm, STATUS_NotOnLink,
  2097. "No subnet selected"));
  2098. }
  2099. return (reply);
  2100. }
  2101. Pkt6Ptr
  2102. Dhcpv6Srv::processRelease(const Pkt6Ptr& release) {
  2103. sanityCheck(release, MANDATORY, MANDATORY);
  2104. // Let's create a simplified client context here.
  2105. AllocEngine::ClientContext6 ctx = createContext(release);
  2106. Pkt6Ptr reply(new Pkt6(DHCPV6_REPLY, release->getTransid()));
  2107. copyClientOptions(release, reply);
  2108. CfgOptionList co_list;
  2109. // buildCfgOptionList(release, ctx, co_list);
  2110. appendDefaultOptions(release, reply, co_list);
  2111. releaseLeases(release, reply, ctx);
  2112. /// @todo If client sent a release and we should remove outstanding
  2113. /// DNS records.
  2114. return (reply);
  2115. }
  2116. Pkt6Ptr
  2117. Dhcpv6Srv::processDecline(const Pkt6Ptr& decline) {
  2118. // Do sanity check.
  2119. sanityCheck(decline, MANDATORY, MANDATORY);
  2120. // Create an empty Reply message.
  2121. Pkt6Ptr reply(new Pkt6(DHCPV6_REPLY, decline->getTransid()));
  2122. // Let's create a simplified client context here.
  2123. AllocEngine::ClientContext6 ctx = createContext(decline);
  2124. // Copy client options (client-id, also relay information if present)
  2125. copyClientOptions(decline, reply);
  2126. // Get the configured option list
  2127. CfgOptionList co_list;
  2128. buildCfgOptionList(decline, ctx, co_list);
  2129. // Include server-id
  2130. appendDefaultOptions(decline, reply, co_list);
  2131. if (declineLeases(decline, reply, ctx)) {
  2132. return (reply);
  2133. } else {
  2134. // declineLeases returns false only if the hooks set the next step
  2135. // status to DROP. We'll just doing as requested.
  2136. return (Pkt6Ptr());
  2137. }
  2138. }
  2139. bool
  2140. Dhcpv6Srv::declineLeases(const Pkt6Ptr& decline, Pkt6Ptr& reply,
  2141. AllocEngine::ClientContext6& ctx) {
  2142. // We need to decline addresses for all IA_NA options in the client's
  2143. // RELEASE message.
  2144. // Let's set the status to be success by default. We can override it with
  2145. // error status if needed. The important thing to understand here is that
  2146. // the global status code may be set to success only if all IA options were
  2147. // handled properly. Therefore the declineIA options
  2148. // may turn the status code to some error, but can't turn it back to success.
  2149. int general_status = STATUS_Success;
  2150. for (OptionCollection::iterator opt = decline->options_.begin();
  2151. opt != decline->options_.end(); ++opt) {
  2152. switch (opt->second->getType()) {
  2153. case D6O_IA_NA: {
  2154. OptionPtr answer_opt = declineIA(decline, ctx.duid_, general_status,
  2155. boost::dynamic_pointer_cast<Option6IA>(opt->second));
  2156. if (answer_opt) {
  2157. // We have an answer, let's use it.
  2158. reply->addOption(answer_opt);
  2159. } else {
  2160. // The only case when declineIA could return NULL is if one of the
  2161. // hook callouts set next step status to DROP. We just need to drop
  2162. // this packet.
  2163. return (false);
  2164. }
  2165. break;
  2166. }
  2167. default:
  2168. // We don't care for the remaining options
  2169. ;
  2170. }
  2171. }
  2172. return (true);
  2173. }
  2174. OptionPtr
  2175. Dhcpv6Srv::declineIA(const Pkt6Ptr& decline, const DuidPtr& duid,
  2176. int& general_status, boost::shared_ptr<Option6IA> ia) {
  2177. LOG_DEBUG(lease6_logger, DBG_DHCP6_DETAIL, DHCP6_DECLINE_PROCESS_IA)
  2178. .arg(decline->getLabel())
  2179. .arg(ia->getIAID());
  2180. // Decline can be done in one of two ways:
  2181. // Approach 1: extract address from client's IA_NA and see if it belongs
  2182. // to this particular client.
  2183. // Approach 2: find a subnet for this client, get a lease for
  2184. // this subnet/duid/iaid and check if its content matches to what the
  2185. // client is asking us to decline.
  2186. //
  2187. // This method implements approach 1.
  2188. // That's our response
  2189. boost::shared_ptr<Option6IA> ia_rsp(new Option6IA(D6O_IA_NA, ia->getIAID()));
  2190. const OptionCollection& opts = ia->getOptions();
  2191. int total_addrs = 0; // Let's count the total number of addresses.
  2192. for (OptionCollection::const_iterator opt = opts.begin(); opt != opts.end();
  2193. ++opt) {
  2194. // Let's ignore nested options other than IAADDR (there shouldn't be anything
  2195. // else in IA_NA in Decline message, but let's be on the safe side).
  2196. if (opt->second->getType() != D6O_IAADDR) {
  2197. continue;
  2198. }
  2199. Option6IAAddrPtr decline_addr = boost::dynamic_pointer_cast<Option6IAAddr>
  2200. (opt->second);
  2201. if (!decline_addr) {
  2202. continue;
  2203. }
  2204. total_addrs++;
  2205. Lease6Ptr lease = LeaseMgrFactory::instance().getLease6(Lease::TYPE_NA,
  2206. decline_addr->getAddress());
  2207. if (!lease) {
  2208. // Client trying to decline a lease that we don't know about.
  2209. LOG_INFO(lease6_logger, DHCP6_DECLINE_FAIL_NO_LEASE)
  2210. .arg(decline->getLabel()).arg(decline_addr->getAddress().toText());
  2211. // RFC3315, section 18.2.7: "For each IA in the Decline message for
  2212. // which the server has no binding information, the server adds an
  2213. // IA option using the IAID from the Release message and includes
  2214. // a Status Code option with the value NoBinding in the IA option.
  2215. setStatusCode(ia_rsp, createStatusCode(*decline, *ia_rsp, STATUS_NoBinding,
  2216. "Server does not know about such an address."));
  2217. // RFC3315, section 18.2.7: The server ignores addresses not
  2218. // assigned to the IA (though it may choose to log an error if it
  2219. // finds such an address).
  2220. continue; // There may be other addresses.
  2221. }
  2222. if (!lease->duid_) {
  2223. // Something is gravely wrong here. We do have a lease, but it does not
  2224. // have mandatory DUID information attached. Someone was messing with our
  2225. // database.
  2226. LOG_ERROR(lease6_logger, DHCP6_DECLINE_FAIL_LEASE_WITHOUT_DUID)
  2227. .arg(decline->getLabel())
  2228. .arg(decline_addr->getAddress().toText());
  2229. ia_rsp->addOption(createStatusCode(*decline, *ia_rsp, STATUS_UnspecFail,
  2230. "Database consistency check failed when attempting Decline."));
  2231. continue;
  2232. }
  2233. // Ok, there's a sane lease with an address. Let's check if DUID matches first.
  2234. if (*duid != *(lease->duid_)) {
  2235. // Sorry, it's not your address. You can't release it.
  2236. LOG_INFO(lease6_logger, DHCP6_DECLINE_FAIL_DUID_MISMATCH)
  2237. .arg(decline->getLabel())
  2238. .arg(decline_addr->getAddress().toText())
  2239. .arg(lease->duid_->toText());
  2240. ia_rsp->addOption(createStatusCode(*decline, *ia_rsp, STATUS_NoBinding,
  2241. "This address does not belong to you, you can't decline it"));
  2242. continue;
  2243. }
  2244. // Let's check if IAID matches.
  2245. if (ia->getIAID() != lease->iaid_) {
  2246. // This address belongs to this client, but to a different IA
  2247. LOG_INFO(lease6_logger, DHCP6_DECLINE_FAIL_IAID_MISMATCH)
  2248. .arg(decline->getLabel())
  2249. .arg(lease->addr_.toText())
  2250. .arg(ia->getIAID())
  2251. .arg(lease->iaid_);
  2252. setStatusCode(ia_rsp, createStatusCode(*decline, *ia_rsp, STATUS_NoBinding,
  2253. "This is your address, but you used wrong IAID"));
  2254. continue;
  2255. }
  2256. // Ok, all is good. Decline this lease.
  2257. if (!declineLease(decline, lease, ia_rsp)) {
  2258. // declineLease returns false only when hook callouts set the next
  2259. // step status to drop. We just propagate the bad news here.
  2260. return (OptionPtr());
  2261. }
  2262. }
  2263. if (total_addrs == 0) {
  2264. setStatusCode(ia_rsp, createStatusCode(*decline, *ia_rsp, STATUS_NoBinding,
  2265. "No addresses sent in IA_NA"));
  2266. general_status = STATUS_NoBinding;
  2267. }
  2268. return (ia_rsp);
  2269. }
  2270. void
  2271. Dhcpv6Srv::setStatusCode(boost::shared_ptr<isc::dhcp::Option6IA>& container,
  2272. const OptionPtr& status) {
  2273. // Let's delete any old status code we may have.
  2274. container->delOption(D6O_STATUS_CODE);
  2275. container->addOption(status);
  2276. }
  2277. bool
  2278. Dhcpv6Srv::declineLease(const Pkt6Ptr& decline, const Lease6Ptr lease,
  2279. boost::shared_ptr<Option6IA> ia_rsp) {
  2280. // We do not want to decrease the assigned-nas at this time. While
  2281. // technically a declined address is no longer allocated, the primary usage
  2282. // of the assigned-addresses statistic is to monitor pool utilization. Most
  2283. // people would forget to include declined-addresses in the calculation,
  2284. // and simply do assigned-addresses/total-addresses. This would have a bias
  2285. // towards under-representing pool utilization, if we decreased allocated
  2286. // immediately after receiving DHCPDECLINE, rather than later when we recover
  2287. // the address.
  2288. // Let's call lease6_decline hooks if necessary.
  2289. if (HooksManager::calloutsPresent(Hooks.hook_index_lease6_decline_)) {
  2290. CalloutHandlePtr callout_handle = getCalloutHandle(decline);
  2291. // Delete previously set arguments
  2292. callout_handle->deleteAllArguments();
  2293. // Pass incoming packet as argument
  2294. callout_handle->setArgument("query6", decline);
  2295. callout_handle->setArgument("lease6", lease);
  2296. // Call callouts
  2297. HooksManager::callCallouts(Hooks.hook_index_lease6_decline_,
  2298. *callout_handle);
  2299. // Callouts decided to SKIP the next processing step. The next
  2300. // processing step would to actually decline the lease, so we'll
  2301. // keep the lease as is.
  2302. if (callout_handle->getStatus() == CalloutHandle::NEXT_STEP_SKIP) {
  2303. LOG_DEBUG(hooks_logger, DBG_DHCP6_DETAIL, DHCP6_HOOK_DECLINE_SKIP)
  2304. .arg(decline->getLabel())
  2305. .arg(decline->getIface())
  2306. .arg(lease->addr_.toText());
  2307. return (true);
  2308. }
  2309. // Callouts decided to DROP the packet. Let's simply log it and
  2310. // return false, so callers will act accordingly.
  2311. if (callout_handle->getStatus() == CalloutHandle::NEXT_STEP_DROP) {
  2312. LOG_DEBUG(hooks_logger, DBG_DHCP6_DETAIL, DHCP6_HOOK_DECLINE_DROP)
  2313. .arg(decline->getLabel())
  2314. .arg(decline->getIface())
  2315. .arg(lease->addr_.toText());
  2316. return (false);
  2317. }
  2318. }
  2319. // Check if a lease has flags indicating that the FQDN update has
  2320. // been performed. If so, create NameChangeRequest which removes
  2321. // the entries. This method does all necessary checks.
  2322. queueNCR(CHG_REMOVE, lease);
  2323. // Bump up the subnet-specific statistic.
  2324. StatsMgr::instance().addValue(
  2325. StatsMgr::generateName("subnet", lease->subnet_id_, "declined-addresses"),
  2326. static_cast<int64_t>(1));
  2327. // Global declined addresses counter.
  2328. StatsMgr::instance().addValue("declined-addresses", static_cast<int64_t>(1));
  2329. // We need to disassociate the lease from the client. Once we move a lease
  2330. // to declined state, it is no longer associated with the client in any
  2331. // way.
  2332. lease->decline(CfgMgr::instance().getCurrentCfg()->getDeclinePeriod());
  2333. LeaseMgrFactory::instance().updateLease6(lease);
  2334. LOG_INFO(dhcp6_logger, DHCP6_DECLINE_LEASE).arg(decline->getLabel())
  2335. .arg(lease->addr_.toText()).arg(lease->valid_lft_);
  2336. ia_rsp->addOption(createStatusCode(*decline, *ia_rsp, STATUS_Success,
  2337. "Lease declined. Hopefully the next one will be better."));
  2338. return (true);
  2339. }
  2340. Pkt6Ptr
  2341. Dhcpv6Srv::processInfRequest(const Pkt6Ptr& inf_request) {
  2342. sanityCheck(inf_request, OPTIONAL, OPTIONAL);
  2343. // Let's create a simplified client context here.
  2344. AllocEngine::ClientContext6 ctx = createContext(inf_request);
  2345. // Create a Reply packet, with the same trans-id as the client's.
  2346. Pkt6Ptr reply(new Pkt6(DHCPV6_REPLY, inf_request->getTransid()));
  2347. // Copy client options (client-id, also relay information if present)
  2348. copyClientOptions(inf_request, reply);
  2349. // Build the configured option list for append methods
  2350. CfgOptionList co_list;
  2351. buildCfgOptionList(inf_request, ctx, co_list);
  2352. // Append default options, i.e. options that the server is supposed
  2353. // to put in all messages it sends (server-id for now, but possibly other
  2354. // options once we start supporting authentication)
  2355. appendDefaultOptions(inf_request, reply, co_list);
  2356. // Try to assign options that were requested by the client.
  2357. appendRequestedOptions(inf_request, reply, ctx, co_list);
  2358. return (reply);
  2359. }
  2360. size_t
  2361. Dhcpv6Srv::unpackOptions(const OptionBuffer& buf,
  2362. const std::string& option_space,
  2363. isc::dhcp::OptionCollection& options,
  2364. size_t* relay_msg_offset,
  2365. size_t* relay_msg_len) {
  2366. size_t offset = 0;
  2367. size_t length = buf.size();
  2368. OptionDefContainer option_defs;
  2369. if (option_space == "dhcp6") {
  2370. // Get the list of standard option definitions.
  2371. option_defs = LibDHCP::getOptionDefs(Option::V6);
  2372. } else if (!option_space.empty()) {
  2373. OptionDefContainerPtr option_defs_ptr =
  2374. CfgMgr::instance().getCurrentCfg()->getCfgOptionDef()->
  2375. getAll(option_space);
  2376. if (option_defs_ptr != NULL) {
  2377. option_defs = *option_defs_ptr;
  2378. }
  2379. }
  2380. // Get the search index #1. It allows to search for option definitions
  2381. // using option code.
  2382. const OptionDefContainerTypeIndex& idx = option_defs.get<1>();
  2383. // The buffer being read comprises a set of options, each starting with
  2384. // a two-byte type code and a two-byte length field.
  2385. while (offset + 4 <= length) {
  2386. // At this point, from the while condition, we know that there
  2387. // are at least 4 bytes available following offset in the
  2388. // buffer.
  2389. uint16_t opt_type = isc::util::readUint16(&buf[offset], 2);
  2390. offset += 2;
  2391. uint16_t opt_len = isc::util::readUint16(&buf[offset], 2);
  2392. offset += 2;
  2393. if (offset + opt_len > length) {
  2394. // @todo: consider throwing exception here.
  2395. // We peeked at the option header of the next option, but discovered
  2396. // that it would end up beyond buffer end, so the option is
  2397. // truncated. Hence we can't parse it. Therefore we revert
  2398. // by by those four bytes (as if we never parsed them).
  2399. return (offset - 4);
  2400. }
  2401. if (opt_type == D6O_RELAY_MSG && relay_msg_offset && relay_msg_len) {
  2402. // remember offset of the beginning of the relay-msg option
  2403. *relay_msg_offset = offset;
  2404. *relay_msg_len = opt_len;
  2405. // do not create that relay-msg option
  2406. offset += opt_len;
  2407. continue;
  2408. }
  2409. // Get all definitions with the particular option code. Note that option
  2410. // code is non-unique within this container however at this point we
  2411. // expect to get one option definition with the particular code. If more
  2412. // are returned we report an error.
  2413. const OptionDefContainerTypeRange& range = idx.equal_range(opt_type);
  2414. // Get the number of returned option definitions for the option code.
  2415. size_t num_defs = distance(range.first, range.second);
  2416. OptionPtr opt;
  2417. if (num_defs > 1) {
  2418. // Multiple options of the same code are not supported right now!
  2419. isc_throw(isc::Unexpected, "Internal error: multiple option definitions"
  2420. " for option type " << opt_type << " returned. Currently it is not"
  2421. " supported to initialize multiple option definitions"
  2422. " for the same option code. This will be supported once"
  2423. " support for option spaces is implemented");
  2424. } else if (num_defs == 0) {
  2425. // @todo Don't crash if definition does not exist because only a few
  2426. // option definitions are initialized right now. In the future
  2427. // we will initialize definitions for all options and we will
  2428. // remove this elseif. For now, return generic option.
  2429. opt = OptionPtr(new Option(Option::V6, opt_type,
  2430. buf.begin() + offset,
  2431. buf.begin() + offset + opt_len));
  2432. opt->setEncapsulatedSpace("dhcp6");
  2433. } else {
  2434. // The option definition has been found. Use it to create
  2435. // the option instance from the provided buffer chunk.
  2436. const OptionDefinitionPtr& def = *(range.first);
  2437. assert(def);
  2438. opt = def->optionFactory(Option::V6, opt_type,
  2439. buf.begin() + offset,
  2440. buf.begin() + offset + opt_len,
  2441. boost::bind(&Dhcpv6Srv::unpackOptions, this, _1, _2,
  2442. _3, _4, _5));
  2443. }
  2444. // add option to options
  2445. options.insert(std::make_pair(opt_type, opt));
  2446. offset += opt_len;
  2447. }
  2448. return (offset);
  2449. }
  2450. void Dhcpv6Srv::classifyByVendor(const Pkt6Ptr& pkt, std::string& classes) {
  2451. OptionVendorClassPtr vclass = boost::dynamic_pointer_cast<
  2452. OptionVendorClass>(pkt->getOption(D6O_VENDOR_CLASS));
  2453. if (!vclass || vclass->getTuplesNum() == 0) {
  2454. return;
  2455. }
  2456. if (vclass->hasTuple(DOCSIS3_CLASS_MODEM)) {
  2457. pkt->addClass(VENDOR_CLASS_PREFIX + DOCSIS3_CLASS_MODEM);
  2458. classes += VENDOR_CLASS_PREFIX + DOCSIS3_CLASS_MODEM + " ";
  2459. } else if (vclass->hasTuple(DOCSIS3_CLASS_EROUTER)) {
  2460. pkt->addClass(VENDOR_CLASS_PREFIX + DOCSIS3_CLASS_EROUTER);
  2461. classes += VENDOR_CLASS_PREFIX + DOCSIS3_CLASS_EROUTER + " ";
  2462. } else {
  2463. pkt->addClass(VENDOR_CLASS_PREFIX + vclass->getTuple(0).getText());
  2464. classes + VENDOR_CLASS_PREFIX + vclass->getTuple(0).getText() + " ";
  2465. }
  2466. }
  2467. void Dhcpv6Srv::classifyPacket(const Pkt6Ptr& pkt) {
  2468. string classes = "";
  2469. // First phase: built-in vendor class processing
  2470. classifyByVendor(pkt, classes);
  2471. // Run match expressions
  2472. // Note getClientClassDictionary() cannot be null
  2473. const ClientClassDefMapPtr& defs_ptr = CfgMgr::instance().getCurrentCfg()->
  2474. getClientClassDictionary()->getClasses();
  2475. for (ClientClassDefMap::const_iterator it = defs_ptr->begin();
  2476. it != defs_ptr->end(); ++it) {
  2477. // Note second cannot be null
  2478. const ExpressionPtr& expr_ptr = it->second->getMatchExpr();
  2479. // Nothing to do without an expression to evaluate
  2480. if (!expr_ptr) {
  2481. continue;
  2482. }
  2483. // Evaluate the expression which can return false (no match),
  2484. // true (match) or raise an exception (error)
  2485. try {
  2486. bool status = evaluate(*expr_ptr, *pkt);
  2487. if (status) {
  2488. LOG_INFO(dhcp6_logger, EVAL_RESULT)
  2489. .arg(it->first)
  2490. .arg(status);
  2491. // Matching: add the class
  2492. pkt->addClass(it->first);
  2493. classes += it->first + " ";
  2494. } else {
  2495. LOG_DEBUG(dhcp6_logger, DBG_DHCP6_DETAIL, EVAL_RESULT)
  2496. .arg(it->first)
  2497. .arg(status);
  2498. }
  2499. } catch (const Exception& ex) {
  2500. LOG_ERROR(dhcp6_logger, EVAL_RESULT)
  2501. .arg(it->first)
  2502. .arg(ex.what());
  2503. } catch (...) {
  2504. LOG_ERROR(dhcp6_logger, EVAL_RESULT)
  2505. .arg(it->first)
  2506. .arg("get exception?");
  2507. }
  2508. }
  2509. if (!classes.empty()) {
  2510. LOG_DEBUG(dhcp6_logger, DBG_DHCP6_BASIC, DHCP6_CLASS_ASSIGNED)
  2511. .arg(classes);
  2512. }
  2513. }
  2514. void
  2515. Dhcpv6Srv::generateFqdn(const Pkt6Ptr& answer) {
  2516. if (!answer) {
  2517. isc_throw(isc::Unexpected, "an instance of the object encapsulating"
  2518. " a message must not be NULL when generating FQDN");
  2519. }
  2520. /// @todo Add proper logging for cases when we can't generate FQDN.
  2521. /// See #3885 for details.
  2522. // It is likely that client hasn't included the FQDN option. In such case,
  2523. // FQDN option will be NULL. Also, there is nothing to do if the option
  2524. // is present and conveys the non-empty FQDN.
  2525. Option6ClientFqdnPtr fqdn = boost::dynamic_pointer_cast<
  2526. Option6ClientFqdn>(answer->getOption(D6O_CLIENT_FQDN));
  2527. if (!fqdn || !fqdn->getDomainName().empty()) {
  2528. return;
  2529. }
  2530. // Get the first IA_NA acquired for the client.
  2531. OptionPtr ia = answer->getOption(D6O_IA_NA);
  2532. if (!ia) {
  2533. return;
  2534. }
  2535. // If it has any IAAddr, use the first one to generate unique FQDN.
  2536. Option6IAAddrPtr iaaddr = boost::dynamic_pointer_cast<
  2537. Option6IAAddr>(ia->getOption(D6O_IAADDR));
  2538. if (!iaaddr) {
  2539. return;
  2540. }
  2541. // Get the IPv6 address acquired by the client.
  2542. IOAddress addr = iaaddr->getAddress();
  2543. std::string generated_name =
  2544. CfgMgr::instance().getD2ClientMgr().generateFqdn(addr);
  2545. LOG_DEBUG(ddns6_logger, DBG_DHCP6_DETAIL_DATA, DHCP6_DDNS_FQDN_GENERATED)
  2546. .arg(answer->getLabel())
  2547. .arg(generated_name);
  2548. try {
  2549. // The lease has been acquired but the FQDN for this lease hasn't
  2550. // been updated in the lease database. We now have new FQDN
  2551. // generated, so the lease database has to be updated here.
  2552. // However, never update lease database for Advertise, just send
  2553. // our notion of client's FQDN in the Client FQDN option.
  2554. if (answer->getType() != DHCPV6_ADVERTISE) {
  2555. Lease6Ptr lease =
  2556. LeaseMgrFactory::instance().getLease6(Lease::TYPE_NA, addr);
  2557. if (lease) {
  2558. lease->hostname_ = generated_name;
  2559. LeaseMgrFactory::instance().updateLease6(lease);
  2560. } else {
  2561. isc_throw(isc::Unexpected, "there is no lease in the database "
  2562. " for address " << addr << ", so as it is impossible"
  2563. " to update FQDN data. This is a programmatic error"
  2564. " as the given address is now being handed to the"
  2565. " client");
  2566. }
  2567. }
  2568. // Set the generated FQDN in the Client FQDN option.
  2569. fqdn->setDomainName(generated_name, Option6ClientFqdn::FULL);
  2570. } catch (const Exception& ex) {
  2571. LOG_ERROR(ddns6_logger, DHCP6_DDNS_GENERATED_FQDN_UPDATE_FAIL)
  2572. .arg(answer->getLabel())
  2573. .arg(addr.toText())
  2574. .arg(ex.what());
  2575. }
  2576. }
  2577. void
  2578. Dhcpv6Srv::startD2() {
  2579. D2ClientMgr& d2_mgr = CfgMgr::instance().getD2ClientMgr();
  2580. if (d2_mgr.ddnsEnabled()) {
  2581. // Updates are enabled, so lets start the sender, passing in
  2582. // our error handler.
  2583. // This may throw so wherever this is called needs to ready.
  2584. d2_mgr.startSender(boost::bind(&Dhcpv6Srv::d2ClientErrorHandler,
  2585. this, _1, _2));
  2586. }
  2587. }
  2588. void
  2589. Dhcpv6Srv::stopD2() {
  2590. D2ClientMgr& d2_mgr = CfgMgr::instance().getD2ClientMgr();
  2591. if (d2_mgr.ddnsEnabled()) {
  2592. // Updates are enabled, so lets stop the sender
  2593. d2_mgr.stopSender();
  2594. }
  2595. }
  2596. void
  2597. Dhcpv6Srv::d2ClientErrorHandler(const
  2598. dhcp_ddns::NameChangeSender::Result result,
  2599. dhcp_ddns::NameChangeRequestPtr& ncr) {
  2600. LOG_ERROR(ddns6_logger, DHCP6_DDNS_REQUEST_SEND_FAILED).
  2601. arg(result).arg((ncr ? ncr->toText() : " NULL "));
  2602. // We cannot communicate with kea-dhcp-ddns, suspend further updates.
  2603. /// @todo We may wish to revisit this, but for now we will simply turn
  2604. /// them off.
  2605. CfgMgr::instance().getD2ClientMgr().suspendUpdates();
  2606. }
  2607. // Refer to config_report so it will be embedded in the binary
  2608. const char* const* dhcp6_config_report = isc::detail::config_report;
  2609. std::string
  2610. Dhcpv6Srv::getVersion(bool extended) {
  2611. std::stringstream tmp;
  2612. tmp << VERSION;
  2613. if (extended) {
  2614. tmp << endl << EXTENDED_VERSION << endl;
  2615. tmp << "linked with:" << endl;
  2616. tmp << Logger::getVersion() << endl;
  2617. tmp << CryptoLink::getVersion() << endl;
  2618. tmp << "database:" << endl;
  2619. #ifdef HAVE_MYSQL
  2620. tmp << MySqlLeaseMgr::getDBVersion() << endl;
  2621. #endif
  2622. #ifdef HAVE_PGSQL
  2623. tmp << PgSqlLeaseMgr::getDBVersion() << endl;
  2624. #endif
  2625. tmp << Memfile_LeaseMgr::getDBVersion();
  2626. // @todo: more details about database runtime
  2627. }
  2628. return (tmp.str());
  2629. }
  2630. void Dhcpv6Srv::processRSOO(const Pkt6Ptr& query, const Pkt6Ptr& rsp) {
  2631. if (query->relay_info_.empty()) {
  2632. // RSOO is inserted by relay agents, nothing to do here if it's
  2633. // a direct message.
  2634. return;
  2635. }
  2636. // Get RSOO configuration.
  2637. ConstCfgRSOOPtr cfg_rsoo = CfgMgr::instance().getCurrentCfg()->getCfgRSOO();
  2638. // Let's get over all relays (encapsulation levels). We need to do
  2639. // it in the same order as the client packet traversed the relays.
  2640. for (int i = query->relay_info_.size(); i > 0 ; --i) {
  2641. OptionPtr rsoo_container = query->getRelayOption(D6O_RSOO, i - 1);
  2642. if (rsoo_container) {
  2643. // There are RSOO options. Let's get through them one by one
  2644. // and if it's RSOO-enabled and there's no such option provided yet,
  2645. // copy it to the server's response
  2646. const OptionCollection& rsoo = rsoo_container->getOptions();
  2647. for (OptionCollection::const_iterator opt = rsoo.begin();
  2648. opt != rsoo.end(); ++opt) {
  2649. // Echo option if it is RSOO enabled option and there is no such
  2650. // option added yet.
  2651. if (cfg_rsoo->enabled(opt->second->getType()) &&
  2652. !rsp->getOption(opt->second->getType())) {
  2653. rsp->addOption(opt->second);
  2654. }
  2655. }
  2656. }
  2657. }
  2658. }
  2659. void Dhcpv6Srv::processStatsReceived(const Pkt6Ptr& query) {
  2660. // Note that we're not bumping pkt6-received statistic as it was
  2661. // increased early in the packet reception code.
  2662. string stat_name = "pkt6-unknown-received";
  2663. switch (query->getType()) {
  2664. case DHCPV6_SOLICIT:
  2665. stat_name = "pkt6-solicit-received";
  2666. break;
  2667. case DHCPV6_ADVERTISE:
  2668. // Should not happen, but let's keep a counter for it
  2669. stat_name = "pkt6-advertise-received";
  2670. break;
  2671. case DHCPV6_REQUEST:
  2672. stat_name = "pkt6-request-received";
  2673. break;
  2674. case DHCPV6_CONFIRM:
  2675. stat_name = "pkt6-confirm-received";
  2676. break;
  2677. case DHCPV6_RENEW:
  2678. stat_name = "pkt6-renew-received";
  2679. break;
  2680. case DHCPV6_REBIND:
  2681. stat_name = "pkt6-rebind-received";
  2682. break;
  2683. case DHCPV6_REPLY:
  2684. // Should not happen, but let's keep a counter for it
  2685. stat_name = "pkt6-reply-received";
  2686. break;
  2687. case DHCPV6_RELEASE:
  2688. stat_name = "pkt6-release-received";
  2689. break;
  2690. case DHCPV6_DECLINE:
  2691. stat_name = "pkt6-decline-received";
  2692. break;
  2693. case DHCPV6_RECONFIGURE:
  2694. stat_name = "pkt6-reconfigure-received";
  2695. break;
  2696. case DHCPV6_INFORMATION_REQUEST:
  2697. stat_name = "pkt6-infrequest-received";
  2698. break;
  2699. default:
  2700. ; // do nothing
  2701. }
  2702. StatsMgr::instance().addValue(stat_name, static_cast<int64_t>(1));
  2703. }
  2704. void Dhcpv6Srv::processStatsSent(const Pkt6Ptr& response) {
  2705. // Increase generic counter for sent packets.
  2706. StatsMgr::instance().addValue("pkt6-sent", static_cast<int64_t>(1));
  2707. // Increase packet type specific counter for packets sent.
  2708. string stat_name;
  2709. switch (response->getType()) {
  2710. case DHCPV6_ADVERTISE:
  2711. stat_name = "pkt6-advertise-sent";
  2712. break;
  2713. case DHCPV6_REPLY:
  2714. stat_name = "pkt6-reply-sent";
  2715. break;
  2716. default:
  2717. // That should never happen
  2718. return;
  2719. }
  2720. StatsMgr::instance().addValue(stat_name, static_cast<int64_t>(1));
  2721. }
  2722. };
  2723. };