dhcp6_srv_unittest.cc 101 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715
  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/dhcp6.h>
  17. #include <dhcp/duid.h>
  18. #include <dhcp/option.h>
  19. #include <dhcp/option6_addrlst.h>
  20. #include <dhcp/option6_ia.h>
  21. #include <dhcp/option6_iaaddr.h>
  22. #include <dhcp/option_int.h>
  23. #include <dhcp/option_int_array.h>
  24. #include <dhcp/option_string.h>
  25. #include <dhcp/option_vendor.h>
  26. #include <dhcp/option_vendor_class.h>
  27. #include <dhcp/iface_mgr.h>
  28. #include <dhcp6/json_config_parser.h>
  29. #include <dhcp/dhcp6.h>
  30. #include <dhcp/docsis3_option_defs.h>
  31. #include <dhcp/tests/iface_mgr_test_config.h>
  32. #include <dhcpsrv/cfgmgr.h>
  33. #include <dhcpsrv/lease_mgr.h>
  34. #include <dhcpsrv/lease_mgr_factory.h>
  35. #include <dhcpsrv/utils.h>
  36. #include <util/buffer.h>
  37. #include <util/range_utilities.h>
  38. #include <stats/stats_mgr.h>
  39. #include <dhcp6/tests/dhcp6_test_utils.h>
  40. #include <dhcp6/tests/dhcp6_client.h>
  41. #include <dhcp/tests/pkt_captures.h>
  42. #include <cc/command_interpreter.h>
  43. #include <boost/pointer_cast.hpp>
  44. #include <boost/scoped_ptr.hpp>
  45. #include <gtest/gtest.h>
  46. #include <unistd.h>
  47. #include <fstream>
  48. #include <iostream>
  49. #include <sstream>
  50. using namespace isc;
  51. using namespace isc::data;
  52. using namespace isc::asiolink;
  53. using namespace isc::dhcp;
  54. using namespace isc::dhcp::test;
  55. using namespace isc::util;
  56. using namespace std;
  57. namespace {
  58. // This test verifies that incoming SOLICIT can be handled properly when
  59. // there are no subnets configured.
  60. //
  61. // This test sends a SOLICIT and the expected response
  62. // is an ADVERTISE with STATUS_NoAddrsAvail and no address provided in the
  63. // response
  64. TEST_F(NakedDhcpv6SrvTest, SolicitNoSubnet) {
  65. NakedDhcpv6Srv srv(0);
  66. Pkt6Ptr sol = Pkt6Ptr(new Pkt6(DHCPV6_SOLICIT, 1234));
  67. sol->setRemoteAddr(IOAddress("fe80::abcd"));
  68. sol->addOption(generateIA(D6O_IA_NA, 234, 1500, 3000));
  69. OptionPtr clientid = generateClientId();
  70. sol->addOption(clientid);
  71. // Pass it to the server and get an advertise
  72. Pkt6Ptr reply = srv.processSolicit(sol);
  73. // check that we get the right NAK
  74. checkNakResponse(reply, DHCPV6_ADVERTISE, 1234, STATUS_NoAddrsAvail,
  75. 0, 0);
  76. }
  77. // This test verifies that incoming REQUEST can be handled properly when
  78. // there are no subnets configured.
  79. //
  80. // This test sends a REQUEST and the expected response
  81. // is an REPLY with STATUS_NoAddrsAvail and no address provided in the
  82. // response
  83. TEST_F(NakedDhcpv6SrvTest, RequestNoSubnet) {
  84. NakedDhcpv6Srv srv(0);
  85. // Let's create a REQUEST
  86. Pkt6Ptr req = Pkt6Ptr(new Pkt6(DHCPV6_REQUEST, 1234));
  87. req->setRemoteAddr(IOAddress("fe80::abcd"));
  88. boost::shared_ptr<Option6IA> ia = generateIA(D6O_IA_NA, 234, 1500, 3000);
  89. // with a hint
  90. IOAddress hint("2001:db8:1:1::dead:beef");
  91. OptionPtr hint_opt(new Option6IAAddr(D6O_IAADDR, hint, 300, 500));
  92. ia->addOption(hint_opt);
  93. req->addOption(ia);
  94. OptionPtr clientid = generateClientId();
  95. req->addOption(clientid);
  96. // server-id is mandatory in REQUEST
  97. req->addOption(srv.getServerID());
  98. // Pass it to the server and hope for a REPLY
  99. Pkt6Ptr reply = srv.processRequest(req);
  100. // check that we get the right NAK
  101. checkNakResponse (reply, DHCPV6_REPLY, 1234, STATUS_NoAddrsAvail,
  102. 0, 0);
  103. }
  104. // This test verifies that incoming RENEW can be handled properly, even when
  105. // no subnets are configured.
  106. //
  107. // This test sends a RENEW and the expected response
  108. // is an REPLY with STATUS_NoBinding and no address provided in the
  109. // response
  110. TEST_F(NakedDhcpv6SrvTest, RenewNoSubnet) {
  111. NakedDhcpv6Srv srv(0);
  112. const IOAddress addr("2001:db8:1:1::cafe:babe");
  113. const uint32_t iaid = 234;
  114. // Generate client-id also duid_
  115. OptionPtr clientid = generateClientId();
  116. // Let's create a RENEW
  117. Pkt6Ptr req = Pkt6Ptr(new Pkt6(DHCPV6_RENEW, 1234));
  118. req->setRemoteAddr(IOAddress("fe80::abcd"));
  119. boost::shared_ptr<Option6IA> ia = generateIA(D6O_IA_NA, iaid, 1500, 3000);
  120. OptionPtr renewed_addr_opt(new Option6IAAddr(D6O_IAADDR, addr, 300, 500));
  121. ia->addOption(renewed_addr_opt);
  122. req->addOption(ia);
  123. req->addOption(clientid);
  124. // Server-id is mandatory in RENEW
  125. req->addOption(srv.getServerID());
  126. // Pass it to the server and hope for a REPLY
  127. Pkt6Ptr reply = srv.processRenew(req);
  128. // check that we get the right NAK
  129. checkNakResponse (reply, DHCPV6_REPLY, 1234, STATUS_NoBinding,
  130. 0, 0);
  131. }
  132. // This test verifies that incoming RELEASE can be handled properly, even when
  133. // no subnets are configured.
  134. //
  135. // This test sends a RELEASE and the expected response
  136. // is an REPLY with STATUS_NoBinding and no address provided in the
  137. // response
  138. TEST_F(NakedDhcpv6SrvTest, ReleaseNoSubnet) {
  139. NakedDhcpv6Srv srv(0);
  140. const IOAddress addr("2001:db8:1:1::cafe:babe");
  141. const uint32_t iaid = 234;
  142. // Generate client-id also duid_
  143. OptionPtr clientid = generateClientId();
  144. // Let's create a RELEASE
  145. Pkt6Ptr req = Pkt6Ptr(new Pkt6(DHCPV6_RELEASE, 1234));
  146. req->setRemoteAddr(IOAddress("fe80::abcd"));
  147. boost::shared_ptr<Option6IA> ia = generateIA(D6O_IA_NA, iaid, 1500, 3000);
  148. OptionPtr released_addr_opt(new Option6IAAddr(D6O_IAADDR, addr, 300, 500));
  149. ia->addOption(released_addr_opt);
  150. req->addOption(ia);
  151. req->addOption(clientid);
  152. // Server-id is mandatory in RELEASE
  153. req->addOption(srv.getServerID());
  154. // Pass it to the server and hope for a REPLY
  155. Pkt6Ptr reply = srv.processRelease(req);
  156. // check that we get the right NAK
  157. checkNakResponse (reply, DHCPV6_REPLY, 1234, STATUS_NoBinding, 0, 0);
  158. }
  159. // Test verifies that the Dhcpv6_srv class can be instantiated. It checks a mode
  160. // without open sockets and with sockets opened on a high port (to not require
  161. // root privileges).
  162. TEST_F(Dhcpv6SrvTest, basic) {
  163. boost::scoped_ptr<Dhcpv6Srv> srv;
  164. ASSERT_NO_THROW( {
  165. // Skip opening any sockets
  166. srv.reset(new NakedDhcpv6Srv(0));
  167. });
  168. srv.reset();
  169. ASSERT_NO_THROW({
  170. // open an unpriviledged port
  171. srv.reset(new NakedDhcpv6Srv(DHCP6_SERVER_PORT + 10000));
  172. });
  173. }
  174. // Test checks that DUID is generated properly
  175. TEST_F(Dhcpv6SrvTest, DUID) {
  176. boost::scoped_ptr<Dhcpv6Srv> srv;
  177. ASSERT_NO_THROW( {
  178. srv.reset(new NakedDhcpv6Srv(0));
  179. });
  180. OptionPtr srvid = srv->getServerID();
  181. ASSERT_TRUE(srvid);
  182. EXPECT_EQ(D6O_SERVERID, srvid->getType());
  183. OutputBuffer buf(32);
  184. srvid->pack(buf);
  185. // length of the actual DUID
  186. size_t len = buf.getLength() - srvid->getHeaderLen();
  187. InputBuffer data(buf.getData(), buf.getLength());
  188. // ignore first four bytes (standard DHCPv6 header)
  189. data.readUint32();
  190. uint16_t duid_type = data.readUint16();
  191. cout << "Duid-type=" << duid_type << endl;
  192. switch(duid_type) {
  193. case DUID::DUID_LLT: {
  194. // DUID must contain at least 6 bytes long MAC
  195. // + 8 bytes of fixed header
  196. EXPECT_GE(14, len);
  197. uint16_t hw_type = data.readUint16();
  198. // there's no real way to find out "correct"
  199. // hardware type
  200. EXPECT_GT(hw_type, 0);
  201. // check that timer is counted since 1.1.2000,
  202. // not from 1.1.1970.
  203. uint32_t seconds = data.readUint32();
  204. EXPECT_LE(seconds, DUID_TIME_EPOCH);
  205. // this test will start failing after 2030.
  206. // Hopefully we will be at BIND12 by then.
  207. // MAC must not be zeros
  208. vector<uint8_t> mac(len-8);
  209. vector<uint8_t> zeros(len-8, 0);
  210. data.readVector(mac, len-8);
  211. EXPECT_TRUE(mac != zeros);
  212. break;
  213. }
  214. case DUID::DUID_EN: {
  215. // there's not much we can check. Just simple
  216. // check if it is not all zeros
  217. vector<uint8_t> content(len-2);
  218. data.readVector(content, len-2);
  219. EXPECT_FALSE(isRangeZero(content.begin(), content.end()));
  220. break;
  221. }
  222. case DUID::DUID_LL: {
  223. // not supported yet
  224. cout << "Test not implemented for DUID-LL." << endl;
  225. // No failure here. There's really no way for test LL DUID. It doesn't
  226. // even make sense to check if that Link Layer is actually present on
  227. // a physical interface. RFC3315 says a server should write its DUID
  228. // and keep it despite hardware changes.
  229. break;
  230. }
  231. case DUID::DUID_UUID: // not supported yet
  232. default:
  233. ADD_FAILURE() << "Not supported duid type=" << duid_type << endl;
  234. break;
  235. }
  236. }
  237. // This test checks if Option Request Option (ORO) is parsed correctly
  238. // and the requested options are actually assigned.
  239. TEST_F(Dhcpv6SrvTest, advertiseOptions) {
  240. IfaceMgrTestConfig test_config(true);
  241. ConstElementPtr x;
  242. string config = "{ \"interfaces-config\": {"
  243. " \"interfaces\": [ \"*\" ]"
  244. "},"
  245. "\"preferred-lifetime\": 3000,"
  246. "\"rebind-timer\": 2000, "
  247. "\"renew-timer\": 1000, "
  248. "\"subnet6\": [ { "
  249. " \"pools\": [ { \"pool\": \"2001:db8:1::/64\" } ],"
  250. " \"subnet\": \"2001:db8:1::/48\", "
  251. " \"interface\": \"eth0\", "
  252. " \"option-data\": [ {"
  253. " \"name\": \"dns-servers\","
  254. " \"data\": \"2001:db8:1234:FFFF::1, 2001:db8:1234:FFFF::2\""
  255. " },"
  256. " {"
  257. " \"name\": \"subscriber-id\","
  258. " \"data\": \"1234\","
  259. " \"csv-format\": False"
  260. " } ]"
  261. " } ],"
  262. "\"valid-lifetime\": 4000 }";
  263. ASSERT_NO_THROW(configure(config));
  264. Pkt6Ptr sol = Pkt6Ptr(new Pkt6(DHCPV6_SOLICIT, 1234));
  265. sol->setRemoteAddr(IOAddress("fe80::abcd"));
  266. sol->setIface("eth0");
  267. sol->addOption(generateIA(D6O_IA_NA, 234, 1500, 3000));
  268. OptionPtr clientid = generateClientId();
  269. sol->addOption(clientid);
  270. // Pass it to the server and get an advertise
  271. Pkt6Ptr adv = srv_.processSolicit(sol);
  272. // check if we get response at all
  273. ASSERT_TRUE(adv);
  274. // We have not requested any options so they should not
  275. // be included in the response.
  276. ASSERT_FALSE(adv->getOption(D6O_SUBSCRIBER_ID));
  277. ASSERT_FALSE(adv->getOption(D6O_NAME_SERVERS));
  278. // Let's now request some options. We expect that the server
  279. // will include them in its response.
  280. boost::shared_ptr<OptionIntArray<uint16_t> >
  281. option_oro(new OptionIntArray<uint16_t>(Option::V6, D6O_ORO));
  282. // Create vector with two option codes.
  283. std::vector<uint16_t> codes(2);
  284. codes[0] = D6O_SUBSCRIBER_ID;
  285. codes[1] = D6O_NAME_SERVERS;
  286. // Pass this code to option.
  287. option_oro->setValues(codes);
  288. // Append ORO to SOLICIT message.
  289. sol->addOption(option_oro);
  290. // Need to process SOLICIT again after requesting new option.
  291. adv = srv_.processSolicit(sol);
  292. ASSERT_TRUE(adv);
  293. OptionPtr tmp = adv->getOption(D6O_NAME_SERVERS);
  294. ASSERT_TRUE(tmp);
  295. boost::shared_ptr<Option6AddrLst> reply_nameservers =
  296. boost::dynamic_pointer_cast<Option6AddrLst>(tmp);
  297. ASSERT_TRUE(reply_nameservers);
  298. Option6AddrLst::AddressContainer addrs = reply_nameservers->getAddresses();
  299. ASSERT_EQ(2, addrs.size());
  300. EXPECT_TRUE(addrs[0] == IOAddress("2001:db8:1234:FFFF::1"));
  301. EXPECT_TRUE(addrs[1] == IOAddress("2001:db8:1234:FFFF::2"));
  302. // There is a dummy option with code 1000 we requested from a server.
  303. // Expect that this option is in server's response.
  304. tmp = adv->getOption(D6O_SUBSCRIBER_ID);
  305. ASSERT_TRUE(tmp);
  306. // Check that the option contains valid data (from configuration).
  307. std::vector<uint8_t> data = tmp->getData();
  308. ASSERT_EQ(2, data.size());
  309. const uint8_t foo_expected[] = {
  310. 0x12, 0x34
  311. };
  312. EXPECT_EQ(0, memcmp(&data[0], foo_expected, 2));
  313. // more checks to be implemented
  314. }
  315. // There are no dedicated tests for Dhcpv6Srv::handleIA_NA and Dhcpv6Srv::assignLeases
  316. // as they are indirectly tested in Solicit and Request tests.
  317. // This test verifies that incoming SOLICIT can be handled properly, that an
  318. // ADVERTISE is generated, that the response has an address and that address
  319. // really belongs to the configured pool.
  320. //
  321. // This test sends a SOLICIT without any hint in IA_NA.
  322. //
  323. // constructed very simple SOLICIT message with:
  324. // - client-id option (mandatory)
  325. // - IA option (a request for address, without any addresses)
  326. //
  327. // expected returned ADVERTISE message:
  328. // - copy of client-id
  329. // - server-id
  330. // - IA that includes IAADDR
  331. TEST_F(Dhcpv6SrvTest, SolicitBasic) {
  332. NakedDhcpv6Srv srv(0);
  333. Pkt6Ptr sol = Pkt6Ptr(new Pkt6(DHCPV6_SOLICIT, 1234));
  334. sol->setRemoteAddr(IOAddress("fe80::abcd"));
  335. sol->setIface("eth0");
  336. sol->addOption(generateIA(D6O_IA_NA, 234, 1500, 3000));
  337. OptionPtr clientid = generateClientId();
  338. sol->addOption(clientid);
  339. // Pass it to the server and get an advertise
  340. Pkt6Ptr reply = srv.processSolicit(sol);
  341. // check if we get response at all
  342. checkResponse(reply, DHCPV6_ADVERTISE, 1234);
  343. // check that IA_NA was returned and that there's an address included
  344. boost::shared_ptr<Option6IAAddr> addr = checkIA_NA(reply, 234, subnet_->getT1(),
  345. subnet_->getT2());
  346. ASSERT_TRUE(addr);
  347. // Check that the assigned address is indeed from the configured pool
  348. checkIAAddr(addr, addr->getAddress(), Lease::TYPE_NA);
  349. // check DUIDs
  350. checkServerId(reply, srv.getServerID());
  351. checkClientId(reply, clientid);
  352. }
  353. // This test verifies that incoming SOLICIT can be handled properly, that an
  354. // ADVERTISE is generated, that the response has a prefix and that prefix
  355. // really belongs to the configured pool.
  356. //
  357. // This test sends a SOLICIT without any hint in IA_PD.
  358. //
  359. // constructed very simple SOLICIT message with:
  360. // - client-id option (mandatory)
  361. // - IA option (a request for address, without any addresses)
  362. //
  363. // expected returned ADVERTISE message:
  364. // - copy of client-id
  365. // - server-id
  366. // - IA that includes IAPREFIX
  367. TEST_F(Dhcpv6SrvTest, pdSolicitBasic) {
  368. NakedDhcpv6Srv srv(0);
  369. Pkt6Ptr sol = Pkt6Ptr(new Pkt6(DHCPV6_SOLICIT, 1234));
  370. sol->setRemoteAddr(IOAddress("fe80::abcd"));
  371. sol->setIface("eth0");
  372. sol->addOption(generateIA(D6O_IA_PD, 234, 1500, 3000));
  373. OptionPtr clientid = generateClientId();
  374. sol->addOption(clientid);
  375. // Pass it to the server and get an advertise
  376. Pkt6Ptr reply = srv.processSolicit(sol);
  377. // check if we get response at all
  378. checkResponse(reply, DHCPV6_ADVERTISE, 1234);
  379. // check that IA_NA was returned and that there's an address included
  380. boost::shared_ptr<Option6IAPrefix> prefix = checkIA_PD(reply, 234, subnet_->getT1(),
  381. subnet_->getT2());
  382. ASSERT_TRUE(prefix);
  383. // Check that the assigned prefix is indeed from the configured pool
  384. checkIAAddr(prefix, prefix->getAddress(), Lease::TYPE_PD);
  385. EXPECT_EQ(pd_pool_->getLength(), prefix->getLength());
  386. // check DUIDs
  387. checkServerId(reply, srv.getServerID());
  388. checkClientId(reply, clientid);
  389. }
  390. // This test verifies that incoming SOLICIT can be handled properly, that an
  391. // ADVERTISE is generated, that the response has an address and that address
  392. // really belongs to the configured pool.
  393. //
  394. // This test sends a SOLICIT with IA_NA that contains a valid hint.
  395. //
  396. // constructed very simple SOLICIT message with:
  397. // - client-id option (mandatory)
  398. // - IA option (a request for address, with an address that belongs to the
  399. // configured pool, i.e. is valid as hint)
  400. //
  401. // expected returned ADVERTISE message:
  402. // - copy of client-id
  403. // - server-id
  404. // - IA that includes IAADDR
  405. TEST_F(Dhcpv6SrvTest, SolicitHint) {
  406. NakedDhcpv6Srv srv(0);
  407. // Let's create a SOLICIT
  408. Pkt6Ptr sol = Pkt6Ptr(new Pkt6(DHCPV6_SOLICIT, 1234));
  409. sol->setRemoteAddr(IOAddress("fe80::abcd"));
  410. sol->setIface("eth0");
  411. boost::shared_ptr<Option6IA> ia = generateIA(D6O_IA_NA, 234, 1500, 3000);
  412. // with a valid hint
  413. IOAddress hint("2001:db8:1:1::dead:beef");
  414. ASSERT_TRUE(subnet_->inPool(Lease::TYPE_NA, hint));
  415. OptionPtr hint_opt(new Option6IAAddr(D6O_IAADDR, hint, 300, 500));
  416. ia->addOption(hint_opt);
  417. sol->addOption(ia);
  418. OptionPtr clientid = generateClientId();
  419. sol->addOption(clientid);
  420. // Pass it to the server and get an advertise
  421. Pkt6Ptr reply = srv.processSolicit(sol);
  422. // check if we get response at all
  423. checkResponse(reply, DHCPV6_ADVERTISE, 1234);
  424. OptionPtr tmp = reply->getOption(D6O_IA_NA);
  425. ASSERT_TRUE(tmp);
  426. // check that IA_NA was returned and that there's an address included
  427. boost::shared_ptr<Option6IAAddr> addr = checkIA_NA(reply, 234, subnet_->getT1(),
  428. subnet_->getT2());
  429. ASSERT_TRUE(addr);
  430. // check that we've got the address we requested
  431. checkIAAddr(addr, hint, Lease::TYPE_NA);
  432. // check DUIDs
  433. checkServerId(reply, srv.getServerID());
  434. checkClientId(reply, clientid);
  435. }
  436. // This test verifies that incoming SOLICIT can be handled properly, that an
  437. // ADVERTISE is generated, that the response has an address and that address
  438. // really belongs to the configured pool.
  439. //
  440. // This test sends a SOLICIT with IA_NA that contains an invalid hint.
  441. //
  442. // constructed very simple SOLICIT message with:
  443. // - client-id option (mandatory)
  444. // - IA option (a request for address, with an address that does not
  445. // belong to the configured pool, i.e. is valid as hint)
  446. //
  447. // expected returned ADVERTISE message:
  448. // - copy of client-id
  449. // - server-id
  450. // - IA that includes IAADDR
  451. TEST_F(Dhcpv6SrvTest, SolicitInvalidHint) {
  452. NakedDhcpv6Srv srv(0);
  453. // Let's create a SOLICIT
  454. Pkt6Ptr sol = Pkt6Ptr(new Pkt6(DHCPV6_SOLICIT, 1234));
  455. sol->setRemoteAddr(IOAddress("fe80::abcd"));
  456. sol->setIface("eth0");
  457. boost::shared_ptr<Option6IA> ia = generateIA(D6O_IA_NA, 234, 1500, 3000);
  458. IOAddress hint("2001:db8:1::cafe:babe");
  459. ASSERT_FALSE(subnet_->inPool(Lease::TYPE_NA, hint));
  460. OptionPtr hint_opt(new Option6IAAddr(D6O_IAADDR, hint, 300, 500));
  461. ia->addOption(hint_opt);
  462. sol->addOption(ia);
  463. OptionPtr clientid = generateClientId();
  464. sol->addOption(clientid);
  465. // Pass it to the server and get an advertise
  466. Pkt6Ptr reply = srv.processSolicit(sol);
  467. // check if we get response at all
  468. checkResponse(reply, DHCPV6_ADVERTISE, 1234);
  469. // check that IA_NA was returned and that there's an address included
  470. boost::shared_ptr<Option6IAAddr> addr = checkIA_NA(reply, 234, subnet_->getT1(),
  471. subnet_->getT2());
  472. ASSERT_TRUE(addr);
  473. // Check that the assigned address is indeed from the configured pool
  474. checkIAAddr(addr, addr->getAddress(), Lease::TYPE_NA);
  475. EXPECT_TRUE(subnet_->inPool(Lease::TYPE_NA, addr->getAddress()));
  476. // check DUIDs
  477. checkServerId(reply, srv.getServerID());
  478. checkClientId(reply, clientid);
  479. }
  480. /// @todo: Add a test that client sends hint that is in pool, but currently
  481. /// being used by a different client.
  482. // This test checks that the server is offering different addresses to different
  483. // clients in ADVERTISEs. Please note that ADVERTISE is not a guarantee that such
  484. // an address will be assigned. Had the pool was very small and contained only
  485. // 2 addresses, the third client would get the same advertise as the first one
  486. // and this is a correct behavior. It is REQUEST that will fail for the third
  487. // client. ADVERTISE is basically saying "if you send me a request, you will
  488. // probably get an address like this" (there are no guarantees).
  489. TEST_F(Dhcpv6SrvTest, ManySolicits) {
  490. NakedDhcpv6Srv srv(0);
  491. Pkt6Ptr sol1 = Pkt6Ptr(new Pkt6(DHCPV6_SOLICIT, 1234));
  492. Pkt6Ptr sol2 = Pkt6Ptr(new Pkt6(DHCPV6_SOLICIT, 2345));
  493. Pkt6Ptr sol3 = Pkt6Ptr(new Pkt6(DHCPV6_SOLICIT, 3456));
  494. sol1->setRemoteAddr(IOAddress("fe80::abcd"));
  495. sol2->setRemoteAddr(IOAddress("fe80::1223"));
  496. sol3->setRemoteAddr(IOAddress("fe80::3467"));
  497. sol1->setIface("eth0");
  498. sol2->setIface("eth0");
  499. sol3->setIface("eth0");
  500. sol1->addOption(generateIA(D6O_IA_NA, 1, 1500, 3000));
  501. sol2->addOption(generateIA(D6O_IA_NA, 2, 1500, 3000));
  502. sol3->addOption(generateIA(D6O_IA_NA, 3, 1500, 3000));
  503. // different client-id sizes
  504. OptionPtr clientid1 = generateClientId(12);
  505. OptionPtr clientid2 = generateClientId(14);
  506. OptionPtr clientid3 = generateClientId(16);
  507. sol1->addOption(clientid1);
  508. sol2->addOption(clientid2);
  509. sol3->addOption(clientid3);
  510. // Pass it to the server and get an advertise
  511. Pkt6Ptr reply1 = srv.processSolicit(sol1);
  512. Pkt6Ptr reply2 = srv.processSolicit(sol2);
  513. Pkt6Ptr reply3 = srv.processSolicit(sol3);
  514. // check if we get response at all
  515. checkResponse(reply1, DHCPV6_ADVERTISE, 1234);
  516. checkResponse(reply2, DHCPV6_ADVERTISE, 2345);
  517. checkResponse(reply3, DHCPV6_ADVERTISE, 3456);
  518. // check that IA_NA was returned and that there's an address included
  519. boost::shared_ptr<Option6IAAddr> addr1 = checkIA_NA(reply1, 1, subnet_->getT1(),
  520. subnet_->getT2());
  521. boost::shared_ptr<Option6IAAddr> addr2 = checkIA_NA(reply2, 2, subnet_->getT1(),
  522. subnet_->getT2());
  523. boost::shared_ptr<Option6IAAddr> addr3 = checkIA_NA(reply3, 3, subnet_->getT1(),
  524. subnet_->getT2());
  525. ASSERT_TRUE(addr1);
  526. ASSERT_TRUE(addr2);
  527. ASSERT_TRUE(addr3);
  528. // Check that the assigned address is indeed from the configured pool
  529. checkIAAddr(addr1, addr1->getAddress(), Lease::TYPE_NA);
  530. checkIAAddr(addr2, addr2->getAddress(), Lease::TYPE_NA);
  531. checkIAAddr(addr3, addr3->getAddress(), Lease::TYPE_NA);
  532. // check DUIDs
  533. checkServerId(reply1, srv.getServerID());
  534. checkServerId(reply2, srv.getServerID());
  535. checkServerId(reply3, srv.getServerID());
  536. checkClientId(reply1, clientid1);
  537. checkClientId(reply2, clientid2);
  538. checkClientId(reply3, clientid3);
  539. // Finally check that the addresses offered are different
  540. EXPECT_NE(addr1->getAddress(), addr2->getAddress());
  541. EXPECT_NE(addr2->getAddress(), addr3->getAddress());
  542. EXPECT_NE(addr3->getAddress(), addr1->getAddress());
  543. cout << "Offered address to client1=" << addr1->getAddress() << endl;
  544. cout << "Offered address to client2=" << addr2->getAddress() << endl;
  545. cout << "Offered address to client3=" << addr3->getAddress() << endl;
  546. }
  547. // This test verifies that incoming REQUEST can be handled properly, that a
  548. // REPLY is generated, that the response has an address and that address
  549. // really belongs to the configured pool.
  550. //
  551. // This test sends a REQUEST with IA_NA that contains a valid hint.
  552. //
  553. // constructed very simple REQUEST message with:
  554. // - client-id option (mandatory)
  555. // - IA option (a request for address, with an address that belongs to the
  556. // configured pool, i.e. is valid as hint)
  557. //
  558. // expected returned REPLY message:
  559. // - copy of client-id
  560. // - server-id
  561. // - IA that includes IAADDR
  562. TEST_F(Dhcpv6SrvTest, RequestBasic) {
  563. NakedDhcpv6Srv srv(0);
  564. // Let's create a REQUEST
  565. Pkt6Ptr req = Pkt6Ptr(new Pkt6(DHCPV6_REQUEST, 1234));
  566. req->setRemoteAddr(IOAddress("fe80::abcd"));
  567. req->setIface("eth0");
  568. boost::shared_ptr<Option6IA> ia = generateIA(D6O_IA_NA, 234, 1500, 3000);
  569. // with a valid hint
  570. IOAddress hint("2001:db8:1:1::dead:beef");
  571. ASSERT_TRUE(subnet_->inPool(Lease::TYPE_NA, hint));
  572. OptionPtr hint_opt(new Option6IAAddr(D6O_IAADDR, hint, 300, 500));
  573. ia->addOption(hint_opt);
  574. req->addOption(ia);
  575. OptionPtr clientid = generateClientId();
  576. req->addOption(clientid);
  577. // server-id is mandatory in REQUEST
  578. req->addOption(srv.getServerID());
  579. // Pass it to the server and hope for a REPLY
  580. Pkt6Ptr reply = srv.processRequest(req);
  581. // check if we get response at all
  582. checkResponse(reply, DHCPV6_REPLY, 1234);
  583. OptionPtr tmp = reply->getOption(D6O_IA_NA);
  584. ASSERT_TRUE(tmp);
  585. // check that IA_NA was returned and that there's an address included
  586. boost::shared_ptr<Option6IAAddr> addr = checkIA_NA(reply, 234,
  587. subnet_->getT1(),
  588. subnet_->getT2());
  589. ASSERT_TRUE(addr);
  590. // check that we've got the address we requested
  591. checkIAAddr(addr, hint, Lease::TYPE_NA);
  592. // check DUIDs
  593. checkServerId(reply, srv.getServerID());
  594. checkClientId(reply, clientid);
  595. // check that the lease is really in the database
  596. Lease6Ptr l = checkLease(duid_, reply->getOption(D6O_IA_NA), addr);
  597. EXPECT_TRUE(l);
  598. LeaseMgrFactory::instance().deleteLease(addr->getAddress());
  599. }
  600. // This test verifies that incoming REQUEST can be handled properly, that a
  601. // REPLY is generated, that the response has a prefix and that prefix
  602. // really belongs to the configured pool.
  603. //
  604. // This test sends a REQUEST with IA_PD that contains a valid hint.
  605. //
  606. // constructed very simple REQUEST message with:
  607. // - client-id option (mandatory)
  608. // - IA option (a request for address, with an address that belongs to the
  609. // configured pool, i.e. is valid as hint)
  610. //
  611. // expected returned REPLY message:
  612. // - copy of client-id
  613. // - server-id
  614. // - IA that includes IAPREFIX
  615. TEST_F(Dhcpv6SrvTest, pdRequestBasic) {
  616. NakedDhcpv6Srv srv(0);
  617. // Let's create a REQUEST
  618. Pkt6Ptr req = Pkt6Ptr(new Pkt6(DHCPV6_REQUEST, 1234));
  619. req->setRemoteAddr(IOAddress("fe80::abcd"));
  620. req->setIface("eth0");
  621. boost::shared_ptr<Option6IA> ia = generateIA(D6O_IA_PD, 234, 1500, 3000);
  622. // with a valid hint
  623. IOAddress hint("2001:db8:1:2:f::");
  624. ASSERT_TRUE(subnet_->inPool(Lease::TYPE_PD, hint));
  625. OptionPtr hint_opt(new Option6IAPrefix(D6O_IAPREFIX, hint, 64, 300, 500));
  626. ia->addOption(hint_opt);
  627. req->addOption(ia);
  628. OptionPtr clientid = generateClientId();
  629. req->addOption(clientid);
  630. // server-id is mandatory in REQUEST
  631. req->addOption(srv.getServerID());
  632. // Pass it to the server and hope for a REPLY
  633. Pkt6Ptr reply = srv.processRequest(req);
  634. // check if we get response at all
  635. checkResponse(reply, DHCPV6_REPLY, 1234);
  636. OptionPtr tmp = reply->getOption(D6O_IA_PD);
  637. ASSERT_TRUE(tmp);
  638. // check that IA_NA was returned and that there's an address included
  639. boost::shared_ptr<Option6IAPrefix> prf = checkIA_PD(reply, 234,
  640. subnet_->getT1(),
  641. subnet_->getT2());
  642. ASSERT_TRUE(prf);
  643. // check that we've got the address we requested
  644. checkIAAddr(prf, hint, Lease::TYPE_PD);
  645. EXPECT_EQ(pd_pool_->getLength(), prf->getLength());
  646. // check DUIDs
  647. checkServerId(reply, srv.getServerID());
  648. checkClientId(reply, clientid);
  649. // check that the lease is really in the database
  650. Lease6Ptr l = checkPdLease(duid_, reply->getOption(D6O_IA_PD), prf);
  651. EXPECT_TRUE(l);
  652. EXPECT_TRUE(LeaseMgrFactory::instance().deleteLease(prf->getAddress()));
  653. }
  654. // This test checks that the server is offering different addresses to different
  655. // clients in REQUEST. Please note that ADVERTISE is not a guarantee that such
  656. // and address will be assigned. Had the pool was very small and contained only
  657. // 2 addresses, the third client would get the same advertise as the first one
  658. // and this is a correct behavior. It is REQUEST that will fail for the third
  659. // client. ADVERTISE is basically saying "if you send me a request, you will
  660. // probably get an address like this" (there are no guarantees).
  661. TEST_F(Dhcpv6SrvTest, ManyRequests) {
  662. NakedDhcpv6Srv srv(0);
  663. ASSERT_TRUE(subnet_);
  664. Pkt6Ptr req1 = Pkt6Ptr(new Pkt6(DHCPV6_REQUEST, 1234));
  665. Pkt6Ptr req2 = Pkt6Ptr(new Pkt6(DHCPV6_REQUEST, 2345));
  666. Pkt6Ptr req3 = Pkt6Ptr(new Pkt6(DHCPV6_REQUEST, 3456));
  667. req1->setRemoteAddr(IOAddress("fe80::abcd"));
  668. req2->setRemoteAddr(IOAddress("fe80::1223"));
  669. req3->setRemoteAddr(IOAddress("fe80::3467"));
  670. req1->setIface("eth0");
  671. req2->setIface("eth0");
  672. req3->setIface("eth0");
  673. req1->addOption(generateIA(D6O_IA_NA, 1, 1500, 3000));
  674. req2->addOption(generateIA(D6O_IA_NA, 2, 1500, 3000));
  675. req3->addOption(generateIA(D6O_IA_NA, 3, 1500, 3000));
  676. // different client-id sizes
  677. OptionPtr clientid1 = generateClientId(12);
  678. OptionPtr clientid2 = generateClientId(14);
  679. OptionPtr clientid3 = generateClientId(16);
  680. req1->addOption(clientid1);
  681. req2->addOption(clientid2);
  682. req3->addOption(clientid3);
  683. // server-id is mandatory in REQUEST
  684. req1->addOption(srv.getServerID());
  685. req2->addOption(srv.getServerID());
  686. req3->addOption(srv.getServerID());
  687. // Pass it to the server and get an advertise
  688. Pkt6Ptr reply1 = srv.processRequest(req1);
  689. Pkt6Ptr reply2 = srv.processRequest(req2);
  690. Pkt6Ptr reply3 = srv.processRequest(req3);
  691. // check if we get response at all
  692. checkResponse(reply1, DHCPV6_REPLY, 1234);
  693. checkResponse(reply2, DHCPV6_REPLY, 2345);
  694. checkResponse(reply3, DHCPV6_REPLY, 3456);
  695. // check that IA_NA was returned and that there's an address included
  696. boost::shared_ptr<Option6IAAddr> addr1 = checkIA_NA(reply1, 1, subnet_->getT1(),
  697. subnet_->getT2());
  698. boost::shared_ptr<Option6IAAddr> addr2 = checkIA_NA(reply2, 2, subnet_->getT1(),
  699. subnet_->getT2());
  700. boost::shared_ptr<Option6IAAddr> addr3 = checkIA_NA(reply3, 3, subnet_->getT1(),
  701. subnet_->getT2());
  702. ASSERT_TRUE(addr1);
  703. ASSERT_TRUE(addr2);
  704. ASSERT_TRUE(addr3);
  705. // Check that the assigned address is indeed from the configured pool
  706. checkIAAddr(addr1, addr1->getAddress(), Lease::TYPE_NA);
  707. checkIAAddr(addr2, addr2->getAddress(), Lease::TYPE_NA);
  708. checkIAAddr(addr3, addr3->getAddress(), Lease::TYPE_NA);
  709. // check DUIDs
  710. checkServerId(reply1, srv.getServerID());
  711. checkServerId(reply2, srv.getServerID());
  712. checkServerId(reply3, srv.getServerID());
  713. checkClientId(reply1, clientid1);
  714. checkClientId(reply2, clientid2);
  715. checkClientId(reply3, clientid3);
  716. // Finally check that the addresses offered are different
  717. EXPECT_NE(addr1->getAddress(), addr2->getAddress());
  718. EXPECT_NE(addr2->getAddress(), addr3->getAddress());
  719. EXPECT_NE(addr3->getAddress(), addr1->getAddress());
  720. cout << "Assigned address to client1=" << addr1->getAddress() << endl;
  721. cout << "Assigned address to client2=" << addr2->getAddress() << endl;
  722. cout << "Assigned address to client3=" << addr3->getAddress() << endl;
  723. }
  724. // This test verifies that incoming (positive) RENEW can be handled properly, that a
  725. // REPLY is generated, that the response has an address and that address
  726. // really belongs to the configured pool and that lease is actually renewed.
  727. //
  728. // expected:
  729. // - returned REPLY message has copy of client-id
  730. // - returned REPLY message has server-id
  731. // - returned REPLY message has IA_NA that includes IAADDR
  732. // - lease is actually renewed in LeaseMgr
  733. TEST_F(Dhcpv6SrvTest, renewBasic) {
  734. testRenewBasic(Lease::TYPE_NA, "2001:db8:1:1::cafe:babe",
  735. "2001:db8:1:1::cafe:babe", 128);
  736. }
  737. // This test verifies that incoming (positive) PD RENEW can be handled properly,
  738. // that a REPLY is generated, that the response has a prefix and that prefix
  739. // really belongs to the configured pool and that lease is actually renewed.
  740. //
  741. // expected:
  742. // - returned REPLY message has copy of client-id
  743. // - returned REPLY message has server-id
  744. // - returned REPLY message has IA_PD that includes IAPREFIX
  745. // - lease is actually renewed in LeaseMgr
  746. TEST_F(Dhcpv6SrvTest, pdRenewBasic) {
  747. testRenewBasic(Lease::TYPE_PD, "2001:db8:1:2::",
  748. "2001:db8:1:2::", pd_pool_->getLength());
  749. }
  750. // This test verifies that incoming (invalid) RENEW with an address
  751. // can be handled properly. This has changed with #3565. The server
  752. // is now able to allocate a lease in Renew if it's available.
  753. // Previous testRenewReject is now split into 3 tests.
  754. //
  755. // This test checks the first scenario: There is no lease at all.
  756. // The server will try to assign it. Since it is not used by anyone else,
  757. // the server will assign it. This is convenient for various types
  758. // of recoveries, e.g. when the server lost its database.
  759. TEST_F(Dhcpv6SrvTest, RenewUnknown) {
  760. // False means that the lease should not be created before renewal attempt
  761. testRenewBasic(Lease::TYPE_NA, "2001:db8:1:1::abc", "2001:db8:1:1::abc",
  762. 128, false);
  763. }
  764. // This test checks that a client that renews existing lease, but uses
  765. // a wrong IAID, will be processed correctly. As there is no lease for
  766. // this (duid, type, iaid) tuple, this is treated as a new IA, regardless
  767. // if the client inserted an address that is used in a different IA.
  768. // After #3565 was implemented, the server will attempt to assign a lease.
  769. // The one that client requested is already used with different IAID, so
  770. // it will just pick a different lease. This is the second out of three
  771. // scenarios tests by old RenewReject test.
  772. TEST_F(Dhcpv6SrvTest, RenewWrongIAID) {
  773. testRenewWrongIAID(Lease::TYPE_NA, IOAddress("2001:db8:1:1::abc"));
  774. }
  775. // This test checks whether client A can renew an address that is currently
  776. // leased by client B. The server should detect that the lease belong to
  777. // someone else and assign a different lease. This is the third out of three
  778. // scenarios tests by old RenewReject test.
  779. TEST_F(Dhcpv6SrvTest, RenewSomeoneElesesLease) {
  780. testRenewSomeoneElsesLease(Lease::TYPE_NA, IOAddress("2001:db8::1"));
  781. }
  782. // This test verifies that incoming (positive) RELEASE with address can be
  783. // handled properly, that a REPLY is generated, that the response has status
  784. // code and that the lease is indeed removed from the database.
  785. //
  786. // expected:
  787. // - returned REPLY message has copy of client-id
  788. // - returned REPLY message has server-id
  789. // - returned REPLY message has IA_NA that does not include an IAADDR
  790. // - lease is actually removed from LeaseMgr
  791. // - assigned-nas stats counter is properly decremented
  792. TEST_F(Dhcpv6SrvTest, ReleaseBasic) {
  793. testReleaseBasic(Lease::TYPE_NA, IOAddress("2001:db8:1:1::cafe:babe"),
  794. IOAddress("2001:db8:1:1::cafe:babe"));
  795. }
  796. // This test verifies that incoming (positive) RELEASE with prefix can be
  797. // handled properly, that a REPLY is generated, that the response has
  798. // status code and that the lease is indeed removed from the database.
  799. //
  800. // expected:
  801. // - returned REPLY message has copy of client-id
  802. // - returned REPLY message has server-id
  803. // - returned REPLY message has IA_PD that does not include an IAPREFIX
  804. // - lease is actually removed from LeaseMgr
  805. // - assigned-pds stats counter is properly decremented
  806. TEST_F(Dhcpv6SrvTest, pdReleaseBasic) {
  807. testReleaseBasic(Lease::TYPE_PD, IOAddress("2001:db8:1:2::"),
  808. IOAddress("2001:db8:1:2::"));
  809. }
  810. // This test verifies that incoming (invalid) RELEASE with an address
  811. // can be handled properly.
  812. //
  813. // This test checks 3 scenarios:
  814. // 1. there is no such lease at all
  815. // 2. there is such a lease, but it is assigned to a different IAID
  816. // 3. there is such a lease, but it belongs to a different client
  817. //
  818. // expected:
  819. // - returned REPLY message has copy of client-id
  820. // - returned REPLY message has server-id
  821. // - returned REPLY message has IA_NA that includes STATUS-CODE
  822. // - No lease in LeaseMgr
  823. // - assigned-nas stats counter is properly not decremented
  824. TEST_F(Dhcpv6SrvTest, ReleaseReject) {
  825. testReleaseReject(Lease::TYPE_NA, IOAddress("2001:db8:1:1::dead"));
  826. }
  827. // This test verifies that incoming (invalid) RELEASE with a prefix
  828. // can be handled properly.
  829. //
  830. // This test checks 3 scenarios:
  831. // 1. there is no such lease at all
  832. // 2. there is such a lease, but it is assigned to a different IAID
  833. // 3. there is such a lease, but it belongs to a different client
  834. //
  835. // expected:
  836. // - returned REPLY message has copy of client-id
  837. // - returned REPLY message has server-id
  838. // - returned REPLY message has IA_PD that includes STATUS-CODE
  839. // - No lease in LeaseMgr
  840. // - assigned-pds stats counter is properly not decremented
  841. TEST_F(Dhcpv6SrvTest, pdReleaseReject) {
  842. testReleaseReject(Lease::TYPE_PD, IOAddress("2001:db8:1:2::"));
  843. }
  844. // This test verifies if the sanityCheck() really checks options presence.
  845. TEST_F(Dhcpv6SrvTest, sanityCheck) {
  846. NakedDhcpv6Srv srv(0);
  847. Pkt6Ptr pkt = Pkt6Ptr(new Pkt6(DHCPV6_SOLICIT, 1234));
  848. // Set link-local sender address, so appropriate subnet can be
  849. // selected for this packet.
  850. pkt->setRemoteAddr(IOAddress("fe80::abcd"));
  851. // client-id is optional for information-request, so
  852. EXPECT_NO_THROW(srv.sanityCheck(pkt, Dhcpv6Srv::OPTIONAL, Dhcpv6Srv::OPTIONAL));
  853. // empty packet, no client-id, no server-id
  854. EXPECT_THROW(srv.sanityCheck(pkt, Dhcpv6Srv::MANDATORY, Dhcpv6Srv::FORBIDDEN),
  855. RFCViolation);
  856. // This doesn't make much sense, but let's check it for completeness
  857. EXPECT_NO_THROW(srv.sanityCheck(pkt, Dhcpv6Srv::FORBIDDEN, Dhcpv6Srv::FORBIDDEN));
  858. OptionPtr clientid = generateClientId();
  859. pkt->addOption(clientid);
  860. // client-id is mandatory, server-id is forbidden (as in SOLICIT or REBIND)
  861. EXPECT_NO_THROW(srv.sanityCheck(pkt, Dhcpv6Srv::MANDATORY, Dhcpv6Srv::FORBIDDEN));
  862. pkt->addOption(srv.getServerID());
  863. // both client-id and server-id are mandatory (as in REQUEST, RENEW, RELEASE, DECLINE)
  864. EXPECT_NO_THROW(srv.sanityCheck(pkt, Dhcpv6Srv::MANDATORY, Dhcpv6Srv::MANDATORY));
  865. // sane section ends here, let's do some negative tests as well
  866. pkt->addOption(clientid);
  867. pkt->addOption(clientid);
  868. // with more than one client-id it should throw, no matter what
  869. EXPECT_THROW(srv.sanityCheck(pkt, Dhcpv6Srv::OPTIONAL, Dhcpv6Srv::OPTIONAL),
  870. RFCViolation);
  871. EXPECT_THROW(srv.sanityCheck(pkt, Dhcpv6Srv::MANDATORY, Dhcpv6Srv::OPTIONAL),
  872. RFCViolation);
  873. EXPECT_THROW(srv.sanityCheck(pkt, Dhcpv6Srv::OPTIONAL, Dhcpv6Srv::MANDATORY),
  874. RFCViolation);
  875. EXPECT_THROW(srv.sanityCheck(pkt, Dhcpv6Srv::MANDATORY, Dhcpv6Srv::MANDATORY),
  876. RFCViolation);
  877. pkt->delOption(D6O_CLIENTID);
  878. pkt->delOption(D6O_CLIENTID);
  879. // again we have only one client-id
  880. // let's try different type of insanity - several server-ids
  881. pkt->addOption(srv.getServerID());
  882. pkt->addOption(srv.getServerID());
  883. // with more than one server-id it should throw, no matter what
  884. EXPECT_THROW(srv.sanityCheck(pkt, Dhcpv6Srv::OPTIONAL, Dhcpv6Srv::OPTIONAL),
  885. RFCViolation);
  886. EXPECT_THROW(srv.sanityCheck(pkt, Dhcpv6Srv::MANDATORY, Dhcpv6Srv::OPTIONAL),
  887. RFCViolation);
  888. EXPECT_THROW(srv.sanityCheck(pkt, Dhcpv6Srv::OPTIONAL, Dhcpv6Srv::MANDATORY),
  889. RFCViolation);
  890. EXPECT_THROW(srv.sanityCheck(pkt, Dhcpv6Srv::MANDATORY, Dhcpv6Srv::MANDATORY),
  891. RFCViolation);
  892. }
  893. // Check that the server is testing if server identifier received in the
  894. // query, matches server identifier used by the server.
  895. TEST_F(Dhcpv6SrvTest, testServerID) {
  896. NakedDhcpv6Srv srv(0);
  897. Pkt6Ptr req = Pkt6Ptr(new Pkt6(DHCPV6_REQUEST, 1234));
  898. std::vector<uint8_t> bin;
  899. // duid_llt constructed with: time = 0, macaddress = 00:00:00:00:00:00
  900. // it's necessary to generate server identifier option
  901. isc::util::encode::decodeHex("0001000100000000000000000000", bin);
  902. // Now create server identifier option
  903. OptionPtr serverid = OptionPtr(new Option(Option::V6, D6O_SERVERID, bin));
  904. // Server identifier option is MANDATORY in Request message.
  905. // Add server identifier option with different value from one that
  906. // server is using.
  907. req->addOption(serverid);
  908. // Message should be dropped
  909. EXPECT_FALSE(srv.testServerID(req));
  910. // Delete server identifier option and add new one, with same value as
  911. // server's server identifier.
  912. req->delOption(D6O_SERVERID);
  913. req->addOption(srv.getServerID());
  914. // With proper server identifier we expect true
  915. EXPECT_TRUE(srv.testServerID(req));
  916. // server-id MUST NOT appear in Solicit, so check if server is
  917. // not dropping a message without server id.
  918. Pkt6Ptr pkt = Pkt6Ptr(new Pkt6(DHCPV6_SOLICIT, 1234));
  919. EXPECT_TRUE(srv.testServerID(req));
  920. }
  921. // Test that some messages are discarded by the server if they are sent to
  922. // unicast address.
  923. TEST_F(Dhcpv6SrvTest, testUnicast) {
  924. NakedDhcpv6Srv srv(0);
  925. // Explicitly list client's message types which must be discarded if
  926. // sent to unicast address.
  927. const uint8_t not_allowed_unicast[] = {
  928. DHCPV6_SOLICIT,
  929. DHCPV6_CONFIRM,
  930. DHCPV6_REBIND,
  931. DHCPV6_INFORMATION_REQUEST
  932. };
  933. // Iterate over these messages and make sure they are discarded.
  934. for (int i = 0; i < sizeof(not_allowed_unicast); ++i) {
  935. Pkt6Ptr msg = Pkt6Ptr(new Pkt6(not_allowed_unicast[i], 1234));
  936. msg->setLocalAddr(IOAddress("2001:db8:1::1"));
  937. EXPECT_FALSE(srv.testUnicast(msg))
  938. << "server accepts message type "
  939. << static_cast<int>(not_allowed_unicast[i])
  940. << "being sent to unicast address; this message should"
  941. " be discarded according to section 15 of RFC3315";
  942. }
  943. // Explicitly list client/relay message types which are allowed to
  944. // be sent to unicast.
  945. const uint8_t allowed_unicast[] = {
  946. DHCPV6_REQUEST,
  947. DHCPV6_RENEW,
  948. DHCPV6_RELEASE,
  949. DHCPV6_DECLINE,
  950. DHCPV6_RELAY_FORW
  951. };
  952. // Iterate over these messages and check that they are accepted being
  953. // sent to unicast.
  954. for (int i = 0; i < sizeof(allowed_unicast); ++i) {
  955. Pkt6Ptr msg = Pkt6Ptr(new Pkt6(allowed_unicast[i], 1234));
  956. msg->setLocalAddr(IOAddress("2001:db8:1::1"));
  957. msg->addOption(srv.getServerID());
  958. EXPECT_TRUE(srv.testUnicast(msg))
  959. << "server doesn't accept message type "
  960. << static_cast<int>(allowed_unicast[i])
  961. << "being sent to unicast address";
  962. }
  963. }
  964. // This test verifies if selectSubnet() selects proper subnet for a given
  965. // source address.
  966. TEST_F(Dhcpv6SrvTest, selectSubnetAddr) {
  967. NakedDhcpv6Srv srv(0);
  968. Subnet6Ptr subnet1(new Subnet6(IOAddress("2001:db8:1::"), 48, 1, 2, 3, 4));
  969. Subnet6Ptr subnet2(new Subnet6(IOAddress("2001:db8:2::"), 48, 1, 2, 3, 4));
  970. Subnet6Ptr subnet3(new Subnet6(IOAddress("2001:db8:3::"), 48, 1, 2, 3, 4));
  971. // CASE 1: We have only one subnet defined and we received local traffic.
  972. // The only available subnet used to be picked, but not anymore
  973. CfgMgr::instance().clear();
  974. CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->add(subnet1); // just a single subnet
  975. CfgMgr::instance().commit();
  976. Pkt6Ptr pkt = Pkt6Ptr(new Pkt6(DHCPV6_SOLICIT, 1234));
  977. pkt->setRemoteAddr(IOAddress("fe80::abcd"));
  978. // The clause for assuming local subnet if there is only one subnet is was
  979. // removed.
  980. EXPECT_FALSE(srv.selectSubnet(pkt));
  981. // CASE 2: We have only one subnet defined and we received relayed traffic.
  982. // We should NOT select it.
  983. // Identical steps as in case 1, but repeated for clarity
  984. CfgMgr::instance().clear();
  985. CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->add(subnet1); // just a single subnet
  986. CfgMgr::instance().commit();
  987. pkt->setRemoteAddr(IOAddress("2001:db8:abcd::2345"));
  988. Subnet6Ptr selected = srv.selectSubnet(pkt);
  989. EXPECT_FALSE(selected);
  990. // CASE 3: We have three subnets defined and we received local traffic.
  991. // Nothing should be selected.
  992. CfgMgr::instance().clear();
  993. CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->add(subnet1);
  994. CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->add(subnet2);
  995. CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->add(subnet3);
  996. CfgMgr::instance().commit();
  997. pkt->setRemoteAddr(IOAddress("fe80::abcd"));
  998. selected = srv.selectSubnet(pkt);
  999. EXPECT_FALSE(selected);
  1000. // CASE 4: We have three subnets defined and we received relayed traffic
  1001. // that came out of subnet 2. We should select subnet2 then
  1002. CfgMgr::instance().clear();
  1003. CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->add(subnet1);
  1004. CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->add(subnet2);
  1005. CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->add(subnet3);
  1006. CfgMgr::instance().commit();
  1007. pkt->setRemoteAddr(IOAddress("2001:db8:2::baca"));
  1008. selected = srv.selectSubnet(pkt);
  1009. EXPECT_EQ(selected, subnet2);
  1010. // CASE 5: We have three subnets defined and we received relayed traffic
  1011. // that came out of undefined subnet. We should select nothing
  1012. CfgMgr::instance().clear();
  1013. CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->add(subnet1);
  1014. CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->add(subnet2);
  1015. CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->add(subnet3);
  1016. CfgMgr::instance().commit();
  1017. pkt->setRemoteAddr(IOAddress("2001:db8:4::baca"));
  1018. EXPECT_FALSE(srv.selectSubnet(pkt));
  1019. }
  1020. // This test verifies if selectSubnet() selects proper subnet for a given
  1021. // network interface name.
  1022. TEST_F(Dhcpv6SrvTest, selectSubnetIface) {
  1023. NakedDhcpv6Srv srv(0);
  1024. Subnet6Ptr subnet1(new Subnet6(IOAddress("2001:db8:1::"), 48, 1, 2, 3, 4));
  1025. Subnet6Ptr subnet2(new Subnet6(IOAddress("2001:db8:2::"), 48, 1, 2, 3, 4));
  1026. Subnet6Ptr subnet3(new Subnet6(IOAddress("2001:db8:3::"), 48, 1, 2, 3, 4));
  1027. subnet1->setIface("eth0");
  1028. subnet3->setIface("wifi1");
  1029. // CASE 1: We have only one subnet defined and it is available via eth0.
  1030. // Packet came from eth0. The only available subnet should be selected
  1031. CfgMgr::instance().clear();
  1032. CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->add(subnet1); // just a single subnet
  1033. CfgMgr::instance().commit();
  1034. Pkt6Ptr pkt = Pkt6Ptr(new Pkt6(DHCPV6_SOLICIT, 1234));
  1035. pkt->setIface("eth0");
  1036. Subnet6Ptr selected = srv.selectSubnet(pkt);
  1037. EXPECT_EQ(selected, subnet1);
  1038. // CASE 2: We have only one subnet defined and it is available via eth0.
  1039. // Packet came from eth1. We should not select it
  1040. CfgMgr::instance().clear();
  1041. CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->add(subnet1); // just a single subnet
  1042. CfgMgr::instance().commit();
  1043. pkt->setIface("eth1");
  1044. selected = srv.selectSubnet(pkt);
  1045. EXPECT_FALSE(selected);
  1046. // CASE 3: We have only 3 subnets defined, one over eth0, one remote and
  1047. // one over wifi1.
  1048. // Packet came from eth1. We should not select it
  1049. CfgMgr::instance().clear();
  1050. CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->add(subnet1);
  1051. CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->add(subnet2);
  1052. CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->add(subnet3);
  1053. CfgMgr::instance().commit();
  1054. pkt->setIface("eth0");
  1055. EXPECT_EQ(subnet1, srv.selectSubnet(pkt));
  1056. pkt->setIface("eth3"); // no such interface
  1057. EXPECT_EQ(Subnet6Ptr(), srv.selectSubnet(pkt)); // nothing selected
  1058. pkt->setIface("wifi1");
  1059. EXPECT_EQ(subnet3, srv.selectSubnet(pkt));
  1060. }
  1061. // This test verifies if selectSubnet() selects proper subnet for a given
  1062. // linkaddr in RELAY-FORW message
  1063. TEST_F(Dhcpv6SrvTest, selectSubnetRelayLinkaddr) {
  1064. NakedDhcpv6Srv srv(0);
  1065. Subnet6Ptr subnet1(new Subnet6(IOAddress("2001:db8:1::"), 48, 1, 2, 3, 4));
  1066. Subnet6Ptr subnet2(new Subnet6(IOAddress("2001:db8:2::"), 48, 1, 2, 3, 4));
  1067. Subnet6Ptr subnet3(new Subnet6(IOAddress("2001:db8:3::"), 48, 1, 2, 3, 4));
  1068. Pkt6::RelayInfo relay;
  1069. relay.linkaddr_ = IOAddress("2001:db8:2::1234");
  1070. relay.peeraddr_ = IOAddress("fe80::1");
  1071. // CASE 1: We have only one subnet defined and we received relayed traffic.
  1072. // The only available subnet should NOT be selected.
  1073. CfgMgr::instance().clear();
  1074. CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->add(subnet1); // just a single subnet
  1075. CfgMgr::instance().commit();
  1076. Pkt6Ptr pkt = Pkt6Ptr(new Pkt6(DHCPV6_SOLICIT, 1234));
  1077. pkt->relay_info_.push_back(relay);
  1078. Subnet6Ptr selected = srv.selectSubnet(pkt);
  1079. EXPECT_FALSE(selected);
  1080. // CASE 2: We have three subnets defined and we received relayed traffic
  1081. // that came out of subnet 2. We should select subnet2 then
  1082. CfgMgr::instance().clear();
  1083. CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->add(subnet1);
  1084. CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->add(subnet2);
  1085. CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->add(subnet3);
  1086. CfgMgr::instance().commit();
  1087. selected = srv.selectSubnet(pkt);
  1088. EXPECT_EQ(selected, subnet2);
  1089. // Source of the packet should have no meaning. Selection is based
  1090. // on linkaddr field in the relay
  1091. pkt->setRemoteAddr(IOAddress("2001:db8:1::baca"));
  1092. selected = srv.selectSubnet(pkt);
  1093. EXPECT_EQ(selected, subnet2);
  1094. // But not when this linkaddr field is not usable.
  1095. Pkt6::RelayInfo relay2;
  1096. relay2.peeraddr_ = IOAddress("fe80::1");
  1097. pkt->relay_info_.clear();
  1098. pkt->relay_info_.push_back(relay2);
  1099. selected = srv.selectSubnet(pkt);
  1100. EXPECT_EQ(selected, subnet1);
  1101. // CASE 3: We have three subnets defined and we received relayed traffic
  1102. // that came out a layer 2 relay on subnet 2. We should select subnet2 then
  1103. CfgMgr::instance().clear();
  1104. CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->add(subnet1);
  1105. CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->add(subnet2);
  1106. CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->add(subnet3);
  1107. CfgMgr::instance().commit();
  1108. pkt->relay_info_.clear();
  1109. pkt->relay_info_.push_back(relay);
  1110. relay2.hop_count_ = 1;
  1111. pkt->relay_info_.push_back(relay2);
  1112. selected = srv.selectSubnet(pkt);
  1113. EXPECT_EQ(selected, subnet2);
  1114. // The number of level 2 relay doesn't matter
  1115. pkt->relay_info_.clear();
  1116. Pkt6::RelayInfo relay20;
  1117. relay20.peeraddr_ = IOAddress("fe80::1");
  1118. pkt->relay_info_.push_back(relay20);
  1119. Pkt6::RelayInfo relay21;
  1120. relay21.peeraddr_ = IOAddress("fe80::1");
  1121. relay21.hop_count_ = 1;
  1122. pkt->relay_info_.push_back(relay21);
  1123. relay.hop_count_ = 2;
  1124. pkt->relay_info_.push_back(relay);
  1125. Pkt6::RelayInfo relay22;
  1126. relay22.peeraddr_ = IOAddress("fe80::1");
  1127. relay22.hop_count_ = 3;
  1128. pkt->relay_info_.push_back(relay22);
  1129. Pkt6::RelayInfo relay23;
  1130. relay23.peeraddr_ = IOAddress("fe80::1");
  1131. relay23.hop_count_ = 4;
  1132. pkt->relay_info_.push_back(relay23);
  1133. selected = srv.selectSubnet(pkt);
  1134. EXPECT_EQ(selected, subnet2);
  1135. // Only the inner/last relay with a usable address matters
  1136. pkt->relay_info_.clear();
  1137. pkt->relay_info_.push_back(relay20);
  1138. pkt->relay_info_.push_back(relay21);
  1139. pkt->relay_info_.push_back(relay);
  1140. pkt->relay_info_.push_back(relay22);
  1141. Pkt6::RelayInfo relay3;
  1142. relay3.linkaddr_ = IOAddress("2001:db8:3::1234");
  1143. relay3.peeraddr_ = IOAddress("fe80::1");
  1144. relay3.hop_count_ = 4;
  1145. pkt->relay_info_.push_back(relay3);
  1146. selected = srv.selectSubnet(pkt);
  1147. EXPECT_EQ(selected, subnet3);
  1148. // CASE 4: We have three subnets defined and we received relayed traffic
  1149. // that came out of undefined subnet. We should select nothing
  1150. CfgMgr::instance().clear();
  1151. CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->add(subnet1);
  1152. CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->add(subnet2);
  1153. CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->add(subnet3);
  1154. CfgMgr::instance().commit();
  1155. pkt->relay_info_.clear();
  1156. relay.hop_count_ = 0;
  1157. relay.linkaddr_ = IOAddress("2001:db8:4::1234");
  1158. pkt->relay_info_.push_back(relay);
  1159. selected = srv.selectSubnet(pkt);
  1160. EXPECT_FALSE(selected);
  1161. }
  1162. // This test verifies if selectSubnet() selects proper subnet for a given
  1163. // interface-id option
  1164. TEST_F(Dhcpv6SrvTest, selectSubnetRelayInterfaceId) {
  1165. NakedDhcpv6Srv srv(0);
  1166. Subnet6Ptr subnet1(new Subnet6(IOAddress("2001:db8:1::"), 48, 1, 2, 3, 4));
  1167. Subnet6Ptr subnet2(new Subnet6(IOAddress("2001:db8:2::"), 48, 1, 2, 3, 4));
  1168. Subnet6Ptr subnet3(new Subnet6(IOAddress("2001:db8:3::"), 48, 1, 2, 3, 4));
  1169. subnet1->setInterfaceId(generateInterfaceId("relay1"));
  1170. subnet2->setInterfaceId(generateInterfaceId("relay2"));
  1171. // CASE 1: We have only one subnet defined and it is for interface-id "relay1"
  1172. // Packet came with interface-id "relay2". We should not select subnet1
  1173. CfgMgr::instance().clear();
  1174. CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->add(subnet1); // just a single subnet
  1175. CfgMgr::instance().commit();
  1176. Pkt6Ptr pkt = Pkt6Ptr(new Pkt6(DHCPV6_SOLICIT, 1234));
  1177. Pkt6::RelayInfo relay;
  1178. relay.linkaddr_ = IOAddress("2001:db8:2::1234");
  1179. relay.peeraddr_ = IOAddress("fe80::1");
  1180. OptionPtr opt = generateInterfaceId("relay2");
  1181. relay.options_.insert(make_pair(opt->getType(), opt));
  1182. pkt->relay_info_.push_back(relay);
  1183. // There is only one subnet configured and we are outside of that subnet
  1184. Subnet6Ptr selected = srv.selectSubnet(pkt);
  1185. EXPECT_FALSE(selected);
  1186. // CASE 2: We have only one subnet defined and it is for interface-id "relay2"
  1187. // Packet came with interface-id "relay2". We should select it
  1188. CfgMgr::instance().clear();
  1189. CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->add(subnet2); // just a single subnet
  1190. CfgMgr::instance().commit();
  1191. selected = srv.selectSubnet(pkt);
  1192. EXPECT_EQ(selected, subnet2);
  1193. // CASE 3: We have only 3 subnets defined: one remote for interface-id "relay1",
  1194. // one remote for interface-id "relay2" and third local
  1195. // packet comes with interface-id "relay2". We should select subnet2
  1196. CfgMgr::instance().clear();
  1197. CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->add(subnet1);
  1198. CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->add(subnet2);
  1199. CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->add(subnet3);
  1200. CfgMgr::instance().commit();
  1201. EXPECT_EQ(subnet2, srv.selectSubnet(pkt));
  1202. }
  1203. // Checks if server responses are sent to the proper port.
  1204. TEST_F(Dhcpv6SrvTest, portsDirectTraffic) {
  1205. NakedDhcpv6Srv srv(0);
  1206. // Let's create a simple SOLICIT
  1207. Pkt6Ptr sol = PktCaptures::captureSimpleSolicit();
  1208. // Simulate that we have received that traffic
  1209. srv.fakeReceive(sol);
  1210. // Server will now process to run its normal loop, but instead of calling
  1211. // IfaceMgr::receive6(), it will read all packets from the list set by
  1212. // fakeReceive()
  1213. srv.run();
  1214. // Get Advertise...
  1215. ASSERT_FALSE(srv.fake_sent_.empty());
  1216. Pkt6Ptr adv = srv.fake_sent_.front();
  1217. ASSERT_TRUE(adv);
  1218. // This is sent back to client directly, should be port 546
  1219. EXPECT_EQ(DHCP6_CLIENT_PORT, adv->getRemotePort());
  1220. }
  1221. // Checks if server responses are sent to the proper port.
  1222. TEST_F(Dhcpv6SrvTest, portsRelayedTraffic) {
  1223. NakedDhcpv6Srv srv(0);
  1224. // Let's create a simple SOLICIT
  1225. Pkt6Ptr sol = PktCaptures::captureRelayedSolicit();
  1226. // Simulate that we have received that traffic
  1227. srv.fakeReceive(sol);
  1228. // Server will now process to run its normal loop, but instead of calling
  1229. // IfaceMgr::receive6(), it will read all packets from the list set by
  1230. // fakeReceive()
  1231. srv.run();
  1232. // Get Advertise...
  1233. ASSERT_FALSE(srv.fake_sent_.empty());
  1234. Pkt6Ptr adv = srv.fake_sent_.front();
  1235. ASSERT_TRUE(adv);
  1236. // This is sent back to relay, so port is 547
  1237. EXPECT_EQ(DHCP6_SERVER_PORT, adv->getRemotePort());
  1238. }
  1239. // Checks if server is able to handle a relayed traffic from DOCSIS3.0 modems
  1240. // @todo Uncomment this test as part of #3180 work.
  1241. // Kea code currently fails to handle docsis traffic.
  1242. TEST_F(Dhcpv6SrvTest, docsisTraffic) {
  1243. NakedDhcpv6Srv srv(0);
  1244. // Let's get a traffic capture from DOCSIS3.0 modem
  1245. Pkt6Ptr sol = PktCaptures::captureDocsisRelayedSolicit();
  1246. // Simulate that we have received that traffic
  1247. srv.fakeReceive(sol);
  1248. // Server will now process to run its normal loop, but instead of calling
  1249. // IfaceMgr::receive6(), it will read all packets from the list set by
  1250. // fakeReceive()
  1251. srv.run();
  1252. // We should have an Advertise in response
  1253. ASSERT_FALSE(srv.fake_sent_.empty());
  1254. Pkt6Ptr adv = srv.fake_sent_.front();
  1255. ASSERT_TRUE(adv);
  1256. }
  1257. // Checks if server is able to handle a relayed traffic from DOCSIS3.0 modems
  1258. TEST_F(Dhcpv6SrvTest, docsisVendorOptionsParse) {
  1259. // Let's get a traffic capture from DOCSIS3.0 modem
  1260. Pkt6Ptr sol = PktCaptures::captureDocsisRelayedSolicit();
  1261. EXPECT_NO_THROW(sol->unpack());
  1262. // Check if the packet contain
  1263. OptionPtr opt = sol->getOption(D6O_VENDOR_OPTS);
  1264. ASSERT_TRUE(opt);
  1265. boost::shared_ptr<OptionVendor> vendor = boost::dynamic_pointer_cast<OptionVendor>(opt);
  1266. ASSERT_TRUE(vendor);
  1267. EXPECT_TRUE(vendor->getOption(DOCSIS3_V6_ORO));
  1268. EXPECT_TRUE(vendor->getOption(36));
  1269. EXPECT_TRUE(vendor->getOption(35));
  1270. EXPECT_TRUE(vendor->getOption(DOCSIS3_V6_DEVICE_TYPE));
  1271. EXPECT_TRUE(vendor->getOption(3));
  1272. EXPECT_TRUE(vendor->getOption(4));
  1273. EXPECT_TRUE(vendor->getOption(5));
  1274. EXPECT_TRUE(vendor->getOption(6));
  1275. EXPECT_TRUE(vendor->getOption(7));
  1276. EXPECT_TRUE(vendor->getOption(8));
  1277. EXPECT_TRUE(vendor->getOption(9));
  1278. EXPECT_TRUE(vendor->getOption(DOCSIS3_V6_VENDOR_NAME));
  1279. EXPECT_TRUE(vendor->getOption(15));
  1280. EXPECT_FALSE(vendor->getOption(20));
  1281. EXPECT_FALSE(vendor->getOption(11));
  1282. EXPECT_FALSE(vendor->getOption(17));
  1283. }
  1284. // Checks if server is able to parse incoming docsis option and extract suboption 1 (docsis ORO)
  1285. TEST_F(Dhcpv6SrvTest, docsisVendorORO) {
  1286. NakedDhcpv6Srv srv(0);
  1287. // Let's get a traffic capture from DOCSIS3.0 modem
  1288. Pkt6Ptr sol = PktCaptures::captureDocsisRelayedSolicit();
  1289. ASSERT_NO_THROW(sol->unpack());
  1290. // Check if the packet contains vendor options option
  1291. OptionPtr opt = sol->getOption(D6O_VENDOR_OPTS);
  1292. ASSERT_TRUE(opt);
  1293. boost::shared_ptr<OptionVendor> vendor = boost::dynamic_pointer_cast<OptionVendor>(opt);
  1294. ASSERT_TRUE(vendor);
  1295. opt = vendor->getOption(DOCSIS3_V6_ORO);
  1296. ASSERT_TRUE(opt);
  1297. OptionUint16ArrayPtr oro = boost::dynamic_pointer_cast<OptionUint16Array>(opt);
  1298. EXPECT_TRUE(oro);
  1299. }
  1300. // This test checks if Option Request Option (ORO) in docsis (vendor-id=4491)
  1301. // vendor options is parsed correctly and the requested options are actually assigned.
  1302. TEST_F(Dhcpv6SrvTest, vendorOptionsORO) {
  1303. IfaceMgrTestConfig test_config(true);
  1304. string config = "{ \"interfaces-config\": {"
  1305. " \"interfaces\": [ \"*\" ]"
  1306. "},"
  1307. "\"preferred-lifetime\": 3000,"
  1308. "\"rebind-timer\": 2000, "
  1309. "\"renew-timer\": 1000, "
  1310. " \"option-def\": [ {"
  1311. " \"name\": \"config-file\","
  1312. " \"code\": 33,"
  1313. " \"type\": \"string\","
  1314. " \"space\": \"vendor-4491\""
  1315. " } ],"
  1316. " \"option-data\": [ {"
  1317. " \"name\": \"config-file\","
  1318. " \"space\": \"vendor-4491\","
  1319. " \"data\": \"normal_erouter_v6.cm\""
  1320. " }],"
  1321. "\"subnet6\": [ { "
  1322. " \"pools\": [ { \"pool\": \"2001:db8:1::/64\" } ],"
  1323. " \"subnet\": \"2001:db8:1::/48\", "
  1324. " \"renew-timer\": 1000, "
  1325. " \"rebind-timer\": 1000, "
  1326. " \"preferred-lifetime\": 3000,"
  1327. " \"valid-lifetime\": 4000,"
  1328. " \"interface-id\": \"\","
  1329. " \"interface\": \"eth0\""
  1330. " } ],"
  1331. "\"valid-lifetime\": 4000 }";
  1332. ASSERT_NO_THROW(configure(config));
  1333. Pkt6Ptr sol = Pkt6Ptr(new Pkt6(DHCPV6_SOLICIT, 1234));
  1334. sol->setRemoteAddr(IOAddress("fe80::abcd"));
  1335. sol->setIface("eth0");
  1336. sol->addOption(generateIA(D6O_IA_NA, 234, 1500, 3000));
  1337. OptionPtr clientid = generateClientId();
  1338. sol->addOption(clientid);
  1339. // Pass it to the server and get an advertise
  1340. Pkt6Ptr adv = srv_.processSolicit(sol);
  1341. // check if we get response at all
  1342. ASSERT_TRUE(adv);
  1343. // We did not include any vendor opts in SOLCIT, so there should be none
  1344. // in ADVERTISE.
  1345. ASSERT_FALSE(adv->getOption(D6O_VENDOR_OPTS));
  1346. // Let's add a vendor-option (vendor-id=4491) with a single sub-option.
  1347. // That suboption has code 1 and is a docsis ORO option.
  1348. boost::shared_ptr<OptionUint16Array> vendor_oro(new OptionUint16Array(Option::V6,
  1349. DOCSIS3_V6_ORO));
  1350. vendor_oro->addValue(DOCSIS3_V6_CONFIG_FILE); // Request option 33
  1351. OptionPtr vendor(new OptionVendor(Option::V6, 4491));
  1352. vendor->addOption(vendor_oro);
  1353. sol->addOption(vendor);
  1354. // Need to process SOLICIT again after requesting new option.
  1355. adv = srv_.processSolicit(sol);
  1356. ASSERT_TRUE(adv);
  1357. // Check if there is vendor option response
  1358. OptionPtr tmp = adv->getOption(D6O_VENDOR_OPTS);
  1359. ASSERT_TRUE(tmp);
  1360. // The response should be OptionVendor object
  1361. boost::shared_ptr<OptionVendor> vendor_resp =
  1362. boost::dynamic_pointer_cast<OptionVendor>(tmp);
  1363. ASSERT_TRUE(vendor_resp);
  1364. OptionPtr docsis33 = vendor_resp->getOption(33);
  1365. ASSERT_TRUE(docsis33);
  1366. OptionStringPtr config_file = boost::dynamic_pointer_cast<OptionString>(docsis33);
  1367. ASSERT_TRUE(config_file);
  1368. EXPECT_EQ("normal_erouter_v6.cm", config_file->getValue());
  1369. }
  1370. // Test checks whether it is possible to use option definitions defined in
  1371. // src/lib/dhcp/docsis3_option_defs.h.
  1372. TEST_F(Dhcpv6SrvTest, vendorOptionsDocsisDefinitions) {
  1373. ConstElementPtr x;
  1374. string config_prefix = "{ \"interfaces-config\": {"
  1375. " \"interfaces\": [ ]"
  1376. "},"
  1377. "\"preferred-lifetime\": 3000,"
  1378. "\"rebind-timer\": 2000, "
  1379. "\"renew-timer\": 1000, "
  1380. " \"option-data\": [ {"
  1381. " \"name\": \"config-file\","
  1382. " \"space\": \"vendor-4491\","
  1383. " \"code\": ";
  1384. string config_postfix = ","
  1385. " \"data\": \"normal_erouter_v6.cm\","
  1386. " \"csv-format\": True"
  1387. " }],"
  1388. "\"subnet6\": [ { "
  1389. " \"pools\": [ { \"pool\": \"2001:db8:1::/64\" } ],"
  1390. " \"subnet\": \"2001:db8:1::/48\", "
  1391. " \"renew-timer\": 1000, "
  1392. " \"rebind-timer\": 1000, "
  1393. " \"preferred-lifetime\": 3000,"
  1394. " \"valid-lifetime\": 4000,"
  1395. " \"interface-id\": \"\","
  1396. " \"interface\": \"\""
  1397. " } ],"
  1398. "\"valid-lifetime\": 4000 }";
  1399. // There is docsis3 (vendor-id=4491) vendor option 33, which is a
  1400. // config-file. Its format is a single string.
  1401. string config_valid = config_prefix + "33" + config_postfix;
  1402. // There is no option 99 defined in vendor-id=4491. As there is no
  1403. // definition, the config should fail.
  1404. string config_bogus = config_prefix + "99" + config_postfix;
  1405. ElementPtr json_bogus = Element::fromJSON(config_bogus);
  1406. ElementPtr json_valid = Element::fromJSON(config_valid);
  1407. NakedDhcpv6Srv srv(0);
  1408. // This should fail (missing option definition)
  1409. EXPECT_NO_THROW(x = configureDhcp6Server(srv, json_bogus));
  1410. ASSERT_TRUE(x);
  1411. comment_ = isc::config::parseAnswer(rcode_, x);
  1412. ASSERT_EQ(1, rcode_);
  1413. // This should work (option definition present)
  1414. EXPECT_NO_THROW(x = configureDhcp6Server(srv, json_valid));
  1415. ASSERT_TRUE(x);
  1416. comment_ = isc::config::parseAnswer(rcode_, x);
  1417. ASSERT_EQ(0, rcode_);
  1418. }
  1419. // This test verifies that the following option structure can be parsed:
  1420. // - option (option space 'foobar')
  1421. // - sub option (option space 'foo')
  1422. // - sub option (option space 'bar')
  1423. TEST_F(Dhcpv6SrvTest, unpackOptions) {
  1424. // Create option definition for each level of encapsulation. Each option
  1425. // definition is for the option code 1. Options may have the same
  1426. // option code because they belong to different option spaces.
  1427. // Top level option encapsulates options which belong to 'space-foo'.
  1428. OptionDefinitionPtr opt_def(new OptionDefinition("option-foobar", 1, "uint32",
  1429. "space-foo"));\
  1430. // Middle option encapsulates options which belong to 'space-bar'
  1431. OptionDefinitionPtr opt_def2(new OptionDefinition("option-foo", 1, "uint16",
  1432. "space-bar"));
  1433. // Low level option doesn't encapsulate any option space.
  1434. OptionDefinitionPtr opt_def3(new OptionDefinition("option-bar", 1,
  1435. "uint8"));
  1436. // Add option definitions to the Configuration Manager. Each goes under
  1437. // different option space.
  1438. CfgOptionDefPtr cfg_option_def =
  1439. CfgMgr::instance().getStagingCfg()->getCfgOptionDef();
  1440. ASSERT_NO_THROW(cfg_option_def->add(opt_def, "space-foobar"));
  1441. ASSERT_NO_THROW(cfg_option_def->add(opt_def2, "space-foo"));
  1442. ASSERT_NO_THROW(cfg_option_def->add(opt_def3, "space-bar"));
  1443. CfgMgr::instance().commit();
  1444. // Create the buffer holding the structure of options.
  1445. const char raw_data[] = {
  1446. // First option starts here.
  1447. 0x00, 0x01, // option code = 1
  1448. 0x00, 0x0F, // option length = 15
  1449. 0x00, 0x01, 0x02, 0x03, // This option carries uint32 value
  1450. // Sub option starts here.
  1451. 0x00, 0x01, // option code = 1
  1452. 0x00, 0x07, // option length = 7
  1453. 0x01, 0x02, // this option carries uint16 value
  1454. // Last option starts here.
  1455. 0x00, 0x01, // option code = 1
  1456. 0x00, 0x01, // option length = 1
  1457. 0x00 // This option carries a single uint8 value and has no sub options.
  1458. };
  1459. OptionBuffer buf(raw_data, raw_data + sizeof(raw_data));
  1460. // Parse options.
  1461. NakedDhcpv6Srv srv(0);
  1462. OptionCollection options;
  1463. ASSERT_NO_THROW(srv.unpackOptions(buf, "space-foobar", options, 0, 0));
  1464. // There should be one top level option.
  1465. ASSERT_EQ(1, options.size());
  1466. boost::shared_ptr<OptionInt<uint32_t> > option_foobar =
  1467. boost::dynamic_pointer_cast<OptionInt<uint32_t> >(options.begin()->
  1468. second);
  1469. ASSERT_TRUE(option_foobar);
  1470. EXPECT_EQ(1, option_foobar->getType());
  1471. EXPECT_EQ(0x00010203, option_foobar->getValue());
  1472. // There should be a middle level option held in option_foobar.
  1473. boost::shared_ptr<OptionInt<uint16_t> > option_foo =
  1474. boost::dynamic_pointer_cast<OptionInt<uint16_t> >(option_foobar->
  1475. getOption(1));
  1476. ASSERT_TRUE(option_foo);
  1477. EXPECT_EQ(1, option_foo->getType());
  1478. EXPECT_EQ(0x0102, option_foo->getValue());
  1479. // Finally, there should be a low level option under option_foo.
  1480. boost::shared_ptr<OptionInt<uint8_t> > option_bar =
  1481. boost::dynamic_pointer_cast<OptionInt<uint8_t> >(option_foo->getOption(1));
  1482. ASSERT_TRUE(option_bar);
  1483. EXPECT_EQ(1, option_bar->getType());
  1484. EXPECT_EQ(0x0, option_bar->getValue());
  1485. }
  1486. // Checks if DOCSIS client packets are classified properly
  1487. TEST_F(Dhcpv6SrvTest, docsisClientClassification) {
  1488. NakedDhcpv6Srv srv(0);
  1489. // Let's create a relayed SOLICIT. This particular relayed SOLICIT has
  1490. // vendor-class set to docsis3.0
  1491. Pkt6Ptr sol1;
  1492. ASSERT_NO_THROW(sol1 = PktCaptures::captureDocsisRelayedSolicit());
  1493. ASSERT_NO_THROW(sol1->unpack());
  1494. srv.classifyPacket(sol1);
  1495. // It should belong to docsis3.0 class. It should not belong to eRouter1.0
  1496. EXPECT_TRUE(sol1->inClass("VENDOR_CLASS_docsis3.0"));
  1497. EXPECT_FALSE(sol1->inClass("eRouter1.0"));
  1498. // Let's get a relayed SOLICIT. This particular relayed SOLICIT has
  1499. // vendor-class set to eRouter1.0
  1500. Pkt6Ptr sol2;
  1501. ASSERT_NO_THROW(sol2 = PktCaptures::captureeRouterRelayedSolicit());
  1502. ASSERT_NO_THROW(sol2->unpack());
  1503. srv.classifyPacket(sol2);
  1504. EXPECT_TRUE(sol2->inClass(srv.VENDOR_CLASS_PREFIX + "eRouter1.0"));
  1505. EXPECT_FALSE(sol2->inClass(srv.VENDOR_CLASS_PREFIX + "docsis3.0"));
  1506. }
  1507. // Checks if client packets are classified properly using match expressions.
  1508. // Note option names and definitions are used.
  1509. TEST_F(Dhcpv6SrvTest, matchClassification) {
  1510. IfaceMgrTestConfig test_config(true);
  1511. NakedDhcpv6Srv srv(0);
  1512. // The router class matches incoming packets with foo in a host-name
  1513. // option (code 1234) and sets an ipv6-forwarding option in the response.
  1514. string config = "{ \"interfaces-config\": {"
  1515. " \"interfaces\": [ \"*\" ] }, "
  1516. "\"preferred-lifetime\": 3000,"
  1517. "\"rebind-timer\": 2000, "
  1518. "\"renew-timer\": 1000, "
  1519. "\"valid-lifetime\": 4000, "
  1520. "\"option-def\": [ "
  1521. "{ \"name\": \"host-name\","
  1522. " \"code\": 1234,"
  1523. " \"type\": \"string\" },"
  1524. "{ \"name\": \"ipv6-forwarding\","
  1525. " \"code\": 2345,"
  1526. " \"type\": \"boolean\" }],"
  1527. "\"subnet6\": [ "
  1528. "{ \"pools\": [ { \"pool\": \"2001:db8:1::/64\" } ], "
  1529. " \"subnet\": \"2001:db8:1::/48\", "
  1530. " \"interface\": \"eth1\" } ],"
  1531. "\"client-classes\": [ "
  1532. "{ \"name\": \"router\", "
  1533. " \"option-data\": ["
  1534. " { \"name\": \"ipv6-forwarding\", "
  1535. " \"data\": \"true\" } ], "
  1536. " \"test\": \"option[host-name].text == 'foo'\" } ] }";
  1537. ASSERT_NO_THROW(configure(config));
  1538. // Create packets with enough to select the subnet
  1539. OptionPtr clientid = generateClientId();
  1540. Pkt6Ptr query1(new Pkt6(DHCPV6_SOLICIT, 1234));
  1541. query1->setRemoteAddr(IOAddress("fe80::abcd"));
  1542. query1->addOption(clientid);
  1543. query1->setIface("eth1");
  1544. query1->addOption(generateIA(D6O_IA_NA, 123, 1500, 3000));
  1545. Pkt6Ptr query2(new Pkt6(DHCPV6_SOLICIT, 1234));
  1546. query2->setRemoteAddr(IOAddress("fe80::abcd"));
  1547. query2->addOption(clientid);
  1548. query2->setIface("eth1");
  1549. query2->addOption(generateIA(D6O_IA_NA, 234, 1500, 3000));
  1550. Pkt6Ptr query3(new Pkt6(DHCPV6_SOLICIT, 1234));
  1551. query3->setRemoteAddr(IOAddress("fe80::abcd"));
  1552. query3->addOption(clientid);
  1553. query3->setIface("eth1");
  1554. query3->addOption(generateIA(D6O_IA_NA, 345, 1500, 3000));
  1555. // Create and add an ORO option to the first 2 queries
  1556. OptionUint16ArrayPtr oro(new OptionUint16Array(Option::V6, D6O_ORO));
  1557. ASSERT_TRUE(oro);
  1558. oro->addValue(2345);
  1559. query1->addOption(oro);
  1560. query2->addOption(oro);
  1561. // Create and add a host-name option to the first and last queries
  1562. OptionStringPtr hostname(new OptionString(Option::V6, 1234, "foo"));
  1563. ASSERT_TRUE(hostname);
  1564. query1->addOption(hostname);
  1565. query3->addOption(hostname);
  1566. // Classify packets
  1567. srv.classifyPacket(query1);
  1568. srv.classifyPacket(query2);
  1569. srv.classifyPacket(query3);
  1570. // Packets with the exception of the second should be in the router class
  1571. EXPECT_TRUE(query1->inClass("router"));
  1572. EXPECT_FALSE(query2->inClass("router"));
  1573. EXPECT_TRUE(query3->inClass("router"));
  1574. // Process queries
  1575. Pkt6Ptr response1 = srv.processSolicit(query1);
  1576. Pkt6Ptr response2 = srv.processSolicit(query2);
  1577. Pkt6Ptr response3 = srv.processSolicit(query3);
  1578. // Classification processing should add an ip-forwarding option
  1579. OptionPtr opt1 = response1->getOption(2345);
  1580. EXPECT_TRUE(opt1);
  1581. // But only for the first query: second was not classified
  1582. OptionPtr opt2 = response2->getOption(2345);
  1583. EXPECT_FALSE(opt2);
  1584. // But only for the first query: third has no ORO
  1585. OptionPtr opt3 = response3->getOption(2345);
  1586. EXPECT_FALSE(opt3);
  1587. }
  1588. // Checks subnet options have the priority over class options
  1589. TEST_F(Dhcpv6SrvTest, subnetClassPriority) {
  1590. IfaceMgrTestConfig test_config(true);
  1591. NakedDhcpv6Srv srv(0);
  1592. // Subnet sets an ipv6-forwarding option in the response.
  1593. // The router class matches incoming packets with foo in a host-name
  1594. // option (code 1234) and sets an ipv6-forwarding option in the response.
  1595. string config = "{ \"interfaces-config\": {"
  1596. " \"interfaces\": [ \"*\" ] }, "
  1597. "\"preferred-lifetime\": 3000,"
  1598. "\"rebind-timer\": 2000, "
  1599. "\"renew-timer\": 1000, "
  1600. "\"valid-lifetime\": 4000, "
  1601. "\"option-def\": [ "
  1602. "{ \"name\": \"host-name\","
  1603. " \"code\": 1234,"
  1604. " \"type\": \"string\" },"
  1605. "{ \"name\": \"ipv6-forwarding\","
  1606. " \"code\": 2345,"
  1607. " \"type\": \"boolean\" }],"
  1608. "\"subnet6\": [ "
  1609. "{ \"pools\": [ { \"pool\": \"2001:db8:1::/64\" } ], "
  1610. " \"subnet\": \"2001:db8:1::/48\", "
  1611. " \"interface\": \"eth1\", "
  1612. " \"option-data\": ["
  1613. " { \"name\": \"ipv6-forwarding\", "
  1614. " \"data\": \"false\" } ] } ], "
  1615. "\"client-classes\": [ "
  1616. "{ \"name\": \"router\","
  1617. " \"option-data\": ["
  1618. " { \"name\": \"ipv6-forwarding\", "
  1619. " \"data\": \"true\" } ], "
  1620. " \"test\": \"option[1234].text == 'foo'\" } ] }";
  1621. ASSERT_NO_THROW(configure(config));
  1622. // Create a packet with enough to select the subnet and go through
  1623. // the SOLICIT processing
  1624. Pkt6Ptr query(new Pkt6(DHCPV6_SOLICIT, 1234));
  1625. query->setRemoteAddr(IOAddress("fe80::abcd"));
  1626. OptionPtr clientid = generateClientId();
  1627. query->addOption(clientid);
  1628. query->setIface("eth1");
  1629. query->addOption(generateIA(D6O_IA_NA, 123, 1500, 3000));
  1630. // Create and add an ORO option to the query
  1631. OptionUint16ArrayPtr oro(new OptionUint16Array(Option::V6, D6O_ORO));
  1632. ASSERT_TRUE(oro);
  1633. oro->addValue(2345);
  1634. query->addOption(oro);
  1635. // Create and add a host-name option to the query
  1636. OptionStringPtr hostname(new OptionString(Option::V6, 1234, "foo"));
  1637. ASSERT_TRUE(hostname);
  1638. query->addOption(hostname);
  1639. // Classify the packet
  1640. srv.classifyPacket(query);
  1641. // The packet should be in the router class
  1642. EXPECT_TRUE(query->inClass("router"));
  1643. // Process the query
  1644. Pkt6Ptr response = srv.processSolicit(query);
  1645. // Processing should add an ip-forwarding option
  1646. OptionPtr opt = response->getOption(2345);
  1647. ASSERT_TRUE(opt);
  1648. ASSERT_GT(opt->len(), opt->getHeaderLen());
  1649. // Classification sets the value to true/1, subnet to false/0
  1650. // Here subnet has the priority
  1651. EXPECT_EQ(0, opt->getUint8());
  1652. }
  1653. // Checks subnet options have the priority over global options
  1654. TEST_F(Dhcpv6SrvTest, subnetGlobalPriority) {
  1655. IfaceMgrTestConfig test_config(true);
  1656. NakedDhcpv6Srv srv(0);
  1657. // Subnet sets an ipv6-forwarding option in the response.
  1658. // The router class matches incoming packets with foo in a host-name
  1659. // option (code 1234) and sets an ipv6-forwarding option in the response.
  1660. string config = "{ \"interfaces-config\": {"
  1661. " \"interfaces\": [ \"*\" ] }, "
  1662. "\"preferred-lifetime\": 3000,"
  1663. "\"rebind-timer\": 2000, "
  1664. "\"renew-timer\": 1000, "
  1665. "\"valid-lifetime\": 4000, "
  1666. "\"option-def\": [ "
  1667. "{ \"name\": \"host-name\","
  1668. " \"code\": 1234,"
  1669. " \"type\": \"string\" },"
  1670. "{ \"name\": \"ipv6-forwarding\","
  1671. " \"code\": 2345,"
  1672. " \"type\": \"boolean\" }],"
  1673. "\"option-data\": ["
  1674. " { \"name\": \"ipv6-forwarding\", "
  1675. " \"data\": \"false\" } ], "
  1676. "\"subnet6\": [ "
  1677. "{ \"pools\": [ { \"pool\": \"2001:db8:1::/64\" } ], "
  1678. " \"subnet\": \"2001:db8:1::/48\", "
  1679. " \"interface\": \"eth1\", "
  1680. " \"option-data\": ["
  1681. " { \"name\": \"ipv6-forwarding\", "
  1682. " \"data\": \"false\" } ] } ] }";
  1683. ASSERT_NO_THROW(configure(config));
  1684. // Create a packet with enough to select the subnet and go through
  1685. // the SOLICIT processing
  1686. Pkt6Ptr query(new Pkt6(DHCPV6_SOLICIT, 1234));
  1687. query->setRemoteAddr(IOAddress("fe80::abcd"));
  1688. OptionPtr clientid = generateClientId();
  1689. query->addOption(clientid);
  1690. query->setIface("eth1");
  1691. query->addOption(generateIA(D6O_IA_NA, 123, 1500, 3000));
  1692. // Create and add an ORO option to the query
  1693. OptionUint16ArrayPtr oro(new OptionUint16Array(Option::V6, D6O_ORO));
  1694. ASSERT_TRUE(oro);
  1695. oro->addValue(2345);
  1696. query->addOption(oro);
  1697. // Create and add a host-name option to the query
  1698. OptionStringPtr hostname(new OptionString(Option::V6, 1234, "foo"));
  1699. ASSERT_TRUE(hostname);
  1700. query->addOption(hostname);
  1701. // Process the query
  1702. Pkt6Ptr response = srv.processSolicit(query);
  1703. // Processing should add an ip-forwarding option
  1704. OptionPtr opt = response->getOption(2345);
  1705. ASSERT_TRUE(opt);
  1706. ASSERT_GT(opt->len(), opt->getHeaderLen());
  1707. // Global sets the value to true/1, subnet to false/0
  1708. // Here subnet has the priority
  1709. EXPECT_EQ(0, opt->getUint8());
  1710. }
  1711. // Checks class options have the priority over global options
  1712. TEST_F(Dhcpv6SrvTest, classGlobalPriority) {
  1713. IfaceMgrTestConfig test_config(true);
  1714. NakedDhcpv6Srv srv(0);
  1715. // A global ipv6-forwarding option is set in the response.
  1716. // The router class matches incoming packets with foo in a host-name
  1717. // option (code 1234) and sets an ipv6-forwarding option in the response.
  1718. string config = "{ \"interfaces-config\": {"
  1719. " \"interfaces\": [ \"*\" ] }, "
  1720. "\"preferred-lifetime\": 3000,"
  1721. "\"rebind-timer\": 2000, "
  1722. "\"renew-timer\": 1000, "
  1723. "\"valid-lifetime\": 4000, "
  1724. "\"option-def\": [ "
  1725. "{ \"name\": \"host-name\","
  1726. " \"code\": 1234,"
  1727. " \"type\": \"string\" },"
  1728. "{ \"name\": \"ipv6-forwarding\","
  1729. " \"code\": 2345,"
  1730. " \"type\": \"boolean\" }],"
  1731. "\"subnet6\": [ "
  1732. "{ \"pools\": [ { \"pool\": \"2001:db8:1::/64\" } ], "
  1733. " \"subnet\": \"2001:db8:1::/48\", "
  1734. " \"interface\": \"eth1\" } ],"
  1735. "\"option-data\": ["
  1736. " { \"name\": \"ipv6-forwarding\", "
  1737. " \"data\": \"false\" } ], "
  1738. "\"client-classes\": [ "
  1739. "{ \"name\": \"router\","
  1740. " \"option-data\": ["
  1741. " { \"name\": \"ipv6-forwarding\", "
  1742. " \"data\": \"true\" } ], "
  1743. " \"test\": \"option[1234].text == 'foo'\" } ] }";
  1744. ASSERT_NO_THROW(configure(config));
  1745. // Create a packet with enough to select the subnet and go through
  1746. // the SOLICIT processing
  1747. Pkt6Ptr query(new Pkt6(DHCPV6_SOLICIT, 1234));
  1748. query->setRemoteAddr(IOAddress("fe80::abcd"));
  1749. OptionPtr clientid = generateClientId();
  1750. query->addOption(clientid);
  1751. query->setIface("eth1");
  1752. query->addOption(generateIA(D6O_IA_NA, 123, 1500, 3000));
  1753. // Create and add an ORO option to the query
  1754. OptionUint16ArrayPtr oro(new OptionUint16Array(Option::V6, D6O_ORO));
  1755. ASSERT_TRUE(oro);
  1756. oro->addValue(2345);
  1757. query->addOption(oro);
  1758. // Create and add a host-name option to the query
  1759. OptionStringPtr hostname(new OptionString(Option::V6, 1234, "foo"));
  1760. ASSERT_TRUE(hostname);
  1761. query->addOption(hostname);
  1762. // Classify the packet
  1763. srv.classifyPacket(query);
  1764. // The packet should be in the router class
  1765. EXPECT_TRUE(query->inClass("router"));
  1766. // Process the query
  1767. Pkt6Ptr response = srv.processSolicit(query);
  1768. // Processing should add an ip-forwarding option
  1769. OptionPtr opt = response->getOption(2345);
  1770. ASSERT_TRUE(opt);
  1771. ASSERT_GT(opt->len(), opt->getHeaderLen());
  1772. // Classification sets the value to true/1, global to false/0
  1773. // Here class has the priority
  1774. EXPECT_NE(0, opt->getUint8());
  1775. }
  1776. // Checks if the client-class field is indeed used for subnet selection.
  1777. // Note that packet classification is already checked in Dhcpv6SrvTest
  1778. // .*Classification above.
  1779. TEST_F(Dhcpv6SrvTest, clientClassifySubnet) {
  1780. // This test configures 2 subnets. We actually only need the
  1781. // first one, but since there's still this ugly hack that picks
  1782. // the pool if there is only one, we must use more than one
  1783. // subnet. That ugly hack will be removed in #3242, currently
  1784. // under review.
  1785. // The second subnet does not play any role here. The client's
  1786. // IP address belongs to the first subnet, so only that first
  1787. // subnet is being tested.
  1788. string config = "{ \"interfaces-config\": {"
  1789. " \"interfaces\": [ \"*\" ]"
  1790. "},"
  1791. "\"preferred-lifetime\": 3000,"
  1792. "\"rebind-timer\": 2000, "
  1793. "\"renew-timer\": 1000, "
  1794. "\"subnet6\": [ "
  1795. " { \"pools\": [ { \"pool\": \"2001:db8:1::/64\" } ],"
  1796. " \"subnet\": \"2001:db8:1::/48\", "
  1797. " \"client-class\": \"foo\" "
  1798. " }, "
  1799. " { \"pools\": [ { \"pool\": \"2001:db8:2::/64\" } ],"
  1800. " \"subnet\": \"2001:db8:2::/48\", "
  1801. " \"client-class\": \"xyzzy\" "
  1802. " } "
  1803. "],"
  1804. "\"valid-lifetime\": 4000 }";
  1805. ASSERT_NO_THROW(configure(config));
  1806. Pkt6Ptr sol = Pkt6Ptr(new Pkt6(DHCPV6_SOLICIT, 1234));
  1807. sol->setRemoteAddr(IOAddress("2001:db8:1::3"));
  1808. sol->addOption(generateIA(D6O_IA_NA, 234, 1500, 3000));
  1809. OptionPtr clientid = generateClientId();
  1810. sol->addOption(clientid);
  1811. // This discover does not belong to foo class, so it will not
  1812. // be serviced
  1813. EXPECT_FALSE(srv_.selectSubnet(sol));
  1814. // Let's add the packet to bar class and try again.
  1815. sol->addClass("bar");
  1816. // Still not supported, because it belongs to wrong class.
  1817. EXPECT_FALSE(srv_.selectSubnet(sol));
  1818. // Let's add it to matching class.
  1819. sol->addClass("foo");
  1820. // This time it should work
  1821. EXPECT_TRUE(srv_.selectSubnet(sol));
  1822. }
  1823. // Tests whether a packet with custom vendor-class (not erouter or docsis)
  1824. // is classified properly.
  1825. TEST_F(Dhcpv6SrvTest, vendorClientClassification2) {
  1826. NakedDhcpv6Srv srv(0);
  1827. // Let's create a SOLICIT.
  1828. Pkt6Ptr sol = Pkt6Ptr(new Pkt6(DHCPV6_SOLICIT, 1234));
  1829. sol->setRemoteAddr(IOAddress("2001:db8:1::3"));
  1830. sol->addOption(generateIA(D6O_IA_NA, 234, 1500, 3000));
  1831. OptionPtr clientid = generateClientId();
  1832. sol->addOption(clientid);
  1833. // Now let's add a vendor-class with id=1234 and content "foo"
  1834. OptionVendorClassPtr vendor_class(new OptionVendorClass(Option::V6, 1234));
  1835. OpaqueDataTuple tuple(OpaqueDataTuple::LENGTH_2_BYTES);
  1836. tuple = "foo";
  1837. vendor_class->addTuple(tuple);
  1838. sol->addOption(vendor_class);
  1839. // Now the server classifies the packet.
  1840. srv.classifyPacket(sol);
  1841. // The packet should now belong to VENDOR_CLASS_foo.
  1842. EXPECT_TRUE(sol->inClass(srv.VENDOR_CLASS_PREFIX + "foo"));
  1843. // It should not belong to "foo"
  1844. EXPECT_FALSE(sol->inClass("foo"));
  1845. }
  1846. // This test checks that the server will handle a Solicit with the Vendor Class
  1847. // having a length of 4 (enterprise-id only).
  1848. TEST_F(Dhcpv6SrvTest, cableLabsShortVendorClass) {
  1849. NakedDhcpv6Srv srv(0);
  1850. // Create a simple Solicit with the 4-byte long vendor class option.
  1851. Pkt6Ptr sol = PktCaptures::captureCableLabsShortVendorClass();
  1852. // Simulate that we have received that traffic
  1853. srv.fakeReceive(sol);
  1854. // Server will now process to run its normal loop, but instead of calling
  1855. // IfaceMgr::receive6(), it will read all packets from the list set by
  1856. // fakeReceive()
  1857. srv.run();
  1858. // Get Advertise...
  1859. ASSERT_FALSE(srv.fake_sent_.empty());
  1860. Pkt6Ptr adv = srv.fake_sent_.front();
  1861. ASSERT_TRUE(adv);
  1862. // This is sent back to client, so port is 546
  1863. EXPECT_EQ(DHCP6_CLIENT_PORT, adv->getRemotePort());
  1864. }
  1865. // Checks if relay IP address specified in the relay-info structure in
  1866. // subnet6 is being used properly.
  1867. TEST_F(Dhcpv6SrvTest, relayOverride) {
  1868. // We have 2 subnets defined. Note that both have a relay address
  1869. // defined. Both are not belonging to the subnets. That is
  1870. // important, because if the relay belongs to the subnet, there's
  1871. // no need to specify relay override.
  1872. string config = "{ \"interfaces-config\": {"
  1873. " \"interfaces\": [ \"*\" ]"
  1874. "},"
  1875. "\"preferred-lifetime\": 3000,"
  1876. "\"rebind-timer\": 2000, "
  1877. "\"renew-timer\": 1000, "
  1878. "\"subnet6\": [ "
  1879. " { \"pools\": [ { \"pool\": \"2001:db8:1::/64\" } ],"
  1880. " \"subnet\": \"2001:db8:1::/48\", "
  1881. " \"relay\": { "
  1882. " \"ip-address\": \"2001:db8:3::1\""
  1883. " }"
  1884. " }, "
  1885. " { \"pools\": [ { \"pool\": \"2001:db8:2::/64\" } ],"
  1886. " \"subnet\": \"2001:db8:2::/48\", "
  1887. " \"relay\": { "
  1888. " \"ip-address\": \"2001:db8:3::2\""
  1889. " }"
  1890. " } "
  1891. "],"
  1892. "\"valid-lifetime\": 4000 }";
  1893. // Use this config to set up the server
  1894. ASSERT_NO_THROW(configure(config));
  1895. // Let's get the subnet configuration objects
  1896. const Subnet6Collection* subnets =
  1897. CfgMgr::instance().getCurrentCfg()->getCfgSubnets6()->getAll();
  1898. ASSERT_EQ(2, subnets->size());
  1899. // Let's get them for easy reference
  1900. Subnet6Ptr subnet1 = (*subnets)[0];
  1901. Subnet6Ptr subnet2 = (*subnets)[1];
  1902. ASSERT_TRUE(subnet1);
  1903. ASSERT_TRUE(subnet2);
  1904. Pkt6Ptr sol = Pkt6Ptr(new Pkt6(DHCPV6_SOLICIT, 1234));
  1905. sol->setRemoteAddr(IOAddress("2001:db8:1::3"));
  1906. sol->addOption(generateIA(D6O_IA_NA, 234, 1500, 3000));
  1907. OptionPtr clientid = generateClientId();
  1908. sol->addOption(clientid);
  1909. // Now pretend the packet came via one relay.
  1910. Pkt6::RelayInfo relay;
  1911. relay.linkaddr_ = IOAddress("2001:db8:1::1");
  1912. relay.peeraddr_ = IOAddress("fe80::1");
  1913. sol->relay_info_.push_back(relay);
  1914. // This is just a sanity check, we're using regular method: the relay
  1915. // belongs to the first (2001:db8:1::/64) subnet, so it's an easy decision.
  1916. EXPECT_TRUE(subnet1 == srv_.selectSubnet(sol));
  1917. // Relay belongs to the second subnet, so it should be selected.
  1918. sol->relay_info_.back().linkaddr_ = IOAddress("2001:db8:2::1");
  1919. EXPECT_TRUE(subnet2 == srv_.selectSubnet(sol));
  1920. // Now let's check if the relay override for the first subnets works
  1921. sol->relay_info_.back().linkaddr_ = IOAddress("2001:db8:3::1");
  1922. EXPECT_TRUE(subnet1 == srv_.selectSubnet(sol));
  1923. // Now repeat that for relay matching the second subnet.
  1924. sol->relay_info_.back().linkaddr_ = IOAddress("2001:db8:3::2");
  1925. EXPECT_TRUE(subnet2 == srv_.selectSubnet(sol));
  1926. // Finally, let's check that completely mismatched relay will not get us
  1927. // anything
  1928. sol->relay_info_.back().linkaddr_ = IOAddress("2001:db8:1234::1");
  1929. EXPECT_FALSE(srv_.selectSubnet(sol));
  1930. }
  1931. // Checks if relay IP address specified in the relay-info structure can be
  1932. // used together with client-classification.
  1933. TEST_F(Dhcpv6SrvTest, relayOverrideAndClientClass) {
  1934. // This test configures 2 subnets. They both are on the same link, so they
  1935. // have the same relay-ip address. Furthermore, the first subnet is
  1936. // reserved for clients that belong to class "foo".
  1937. string config = "{ \"interfaces-config\": {"
  1938. " \"interfaces\": [ \"*\" ]"
  1939. "},"
  1940. "\"preferred-lifetime\": 3000,"
  1941. "\"rebind-timer\": 2000, "
  1942. "\"renew-timer\": 1000, "
  1943. "\"subnet6\": [ "
  1944. " { \"pools\": [ { \"pool\": \"2001:db8:1::/64\" } ],"
  1945. " \"subnet\": \"2001:db8:1::/48\", "
  1946. " \"client-class\": \"foo\", "
  1947. " \"relay\": { "
  1948. " \"ip-address\": \"2001:db8:3::1\""
  1949. " }"
  1950. " }, "
  1951. " { \"pools\": [ { \"pool\": \"2001:db8:2::/64\" } ],"
  1952. " \"subnet\": \"2001:db8:2::/48\", "
  1953. " \"relay\": { "
  1954. " \"ip-address\": \"2001:db8:3::1\""
  1955. " }"
  1956. " } "
  1957. "],"
  1958. "\"valid-lifetime\": 4000 }";
  1959. // Use this config to set up the server
  1960. ASSERT_NO_THROW(configure(config));
  1961. // Let's get the subnet configuration objects
  1962. const Subnet6Collection* subnets =
  1963. CfgMgr::instance().getCurrentCfg()->getCfgSubnets6()->getAll();
  1964. ASSERT_EQ(2, subnets->size());
  1965. // Let's get them for easy reference
  1966. Subnet6Ptr subnet1 = (*subnets)[0];
  1967. Subnet6Ptr subnet2 = (*subnets)[1];
  1968. ASSERT_TRUE(subnet1);
  1969. ASSERT_TRUE(subnet2);
  1970. Pkt6Ptr sol = Pkt6Ptr(new Pkt6(DHCPV6_SOLICIT, 1234));
  1971. sol->setRemoteAddr(IOAddress("2001:db8:1::3"));
  1972. sol->addOption(generateIA(D6O_IA_NA, 234, 1500, 3000));
  1973. OptionPtr clientid = generateClientId();
  1974. sol->addOption(clientid);
  1975. // Now pretend the packet came via one relay.
  1976. Pkt6::RelayInfo relay;
  1977. relay.linkaddr_ = IOAddress("2001:db8:3::1");
  1978. relay.peeraddr_ = IOAddress("fe80::1");
  1979. sol->relay_info_.push_back(relay);
  1980. // This packet does not belong to class foo, so it should be rejected in
  1981. // subnet[0], even though the relay-ip matches. It should be accepted in
  1982. // subnet[1], because the subnet matches and there are no class
  1983. // requirements.
  1984. EXPECT_TRUE(subnet2 == srv_.selectSubnet(sol));
  1985. // Now let's add this packet to class foo and recheck. This time it should
  1986. // be accepted in the first subnet, because both class and relay-ip match.
  1987. sol->addClass("foo");
  1988. EXPECT_TRUE(subnet1 == srv_.selectSubnet(sol));
  1989. }
  1990. /// @brief Creates RSOO option with suboptions
  1991. ///
  1992. /// Creates Relay-Supplied Options option that includes nested options. The
  1993. /// codes of those nested options are specified in codes parameter. Content of
  1994. /// the options is controlled with payload parameter. When it is zero, option
  1995. /// code will be used (e.g. option 100 will contain repeating bytes of value 100).
  1996. /// When non-zero is used, payload will be used. Each suboption length is always
  1997. /// set to the arbitrarily chosen value of 10.
  1998. ///
  1999. /// @param codes a vector of option codes to be created
  2000. /// @param payload specified payload (0 = fill payload with repeating option code)
  2001. /// @return RSOO with nested options
  2002. OptionPtr createRSOO(const std::vector<uint16_t>& codes, uint8_t payload = 0) {
  2003. OptionDefinitionPtr def = LibDHCP::getOptionDef(Option::V6, D6O_RSOO);
  2004. if (!def) {
  2005. isc_throw(BadValue, "Can't find RSOO definition");
  2006. }
  2007. OptionPtr rsoo_container(new OptionCustom(*def, Option::V6));
  2008. for (size_t i = 0; i < codes.size(); ++i) {
  2009. OptionBuffer buf(10, payload ? payload : codes[i]); // let's make the option 10 bytes long
  2010. rsoo_container->addOption(OptionPtr(new Option(Option::V6, codes[i], buf)));
  2011. }
  2012. return (rsoo_container);
  2013. }
  2014. // Test that the server processes RSOO (Relay Supplied Options option) correctly,
  2015. // i.e. it includes in its response the options that are inserted by the relay.
  2016. // The server must do this only for options that are RSOO-enabled.
  2017. TEST_F(Dhcpv6SrvTest, rsoo) {
  2018. Dhcp6Client client;
  2019. string config =
  2020. "{"
  2021. " \"relay-supplied-options\": [ \"110\", \"120\", \"130\" ],"
  2022. " \"preferred-lifetime\": 3000,"
  2023. " \"rebind-timer\": 2000, "
  2024. " \"renew-timer\": 1000, "
  2025. " \"subnet6\": [ { "
  2026. " \"pools\": [ { \"pool\": \"2001:db8::/64\" } ],"
  2027. " \"subnet\": \"2001:db8::/48\" "
  2028. " } ],"
  2029. " \"valid-lifetime\": 4000"
  2030. "}";
  2031. EXPECT_NO_THROW(configure(config, *client.getServer()));
  2032. // Now pretend the packet came via one relay.
  2033. Pkt6::RelayInfo relay;
  2034. relay.msg_type_ = DHCPV6_RELAY_FORW;
  2035. relay.hop_count_ = 1;
  2036. relay.linkaddr_ = IOAddress("2001:db8::1");
  2037. relay.peeraddr_ = IOAddress("fe80::1");
  2038. vector<uint16_t> rsoo1;
  2039. rsoo1.push_back(109);
  2040. rsoo1.push_back(110);
  2041. rsoo1.push_back(111);
  2042. // The relay will request echoing back 3 options: 109, 110, 111.
  2043. // The configuration allows echoing back only 110.
  2044. OptionPtr opt = createRSOO(rsoo1);
  2045. relay.options_.insert(make_pair(opt->getType(), opt));
  2046. client.relay_info_.push_back(relay);
  2047. client.doSARR();
  2048. // Option 110 should be copied to the client
  2049. EXPECT_FALSE(client.config_.options_.find(110) == client.config_.options_.end());
  2050. // Options 109 and 111 should not be copied (they are not RSOO-enabled)
  2051. EXPECT_TRUE(client.config_.options_.find(109) == client.config_.options_.end());
  2052. EXPECT_TRUE(client.config_.options_.find(111) == client.config_.options_.end());
  2053. }
  2054. // Test that the server processes RSOO (Relay Supplied Options option) correctly
  2055. // when there are more relays. In particular, the following case is tested:
  2056. // if relay1 inserts option A and B, relay2 inserts option B and C, the response
  2057. // should include options A, B and C. The server must use instance of option B
  2058. // that comes from the first relay, not the second one.
  2059. TEST_F(Dhcpv6SrvTest, rsoo2relays) {
  2060. Dhcp6Client client;
  2061. string config =
  2062. "{"
  2063. " \"relay-supplied-options\": [ \"110\", \"120\", \"130\" ],"
  2064. " \"preferred-lifetime\": 3000,"
  2065. " \"rebind-timer\": 2000, "
  2066. " \"renew-timer\": 1000, "
  2067. " \"subnet6\": [ { "
  2068. " \"pools\": [ { \"pool\": \"2001:db8::/64\" } ],"
  2069. " \"subnet\": \"2001:db8::/48\" "
  2070. " } ],"
  2071. " \"valid-lifetime\": 4000"
  2072. "}";
  2073. EXPECT_NO_THROW(configure(config, *client.getServer()));
  2074. // Now pretend the packet came via two relays.
  2075. // This situation reflects the following case:
  2076. // client----relay1----relay2----server
  2077. // Fabricate the first relay.
  2078. Pkt6::RelayInfo relay1;
  2079. relay1.msg_type_ = DHCPV6_RELAY_FORW;
  2080. relay1.hop_count_ = 1;
  2081. relay1.linkaddr_ = IOAddress("2001:db8::1");
  2082. relay1.peeraddr_ = IOAddress("fe80::1");
  2083. vector<uint16_t> rsoo1;
  2084. rsoo1.push_back(110); // The relay1 will send 2 options: 110, 120
  2085. rsoo1.push_back(120);
  2086. OptionPtr opt = createRSOO(rsoo1, 1); // use 0x1 as payload
  2087. relay1.options_.insert(make_pair(opt->getType(), opt));
  2088. // Now the second relay.
  2089. Pkt6::RelayInfo relay2;
  2090. relay2.msg_type_ = DHCPV6_RELAY_FORW;
  2091. relay2.hop_count_ = 2;
  2092. relay2.linkaddr_ = IOAddress("2001:db8::2");
  2093. relay2.peeraddr_ = IOAddress("fe80::2");
  2094. vector<uint16_t> rsoo2;
  2095. rsoo2.push_back(120); // The relay2 will send 2 options: 120, 130
  2096. rsoo2.push_back(130);
  2097. opt = createRSOO(rsoo2, 2); // use 0x2 as payload
  2098. relay2.options_.insert(make_pair(opt->getType(), opt));
  2099. // The relays ecapsulate packet in this order: relay1, relay2, but the server
  2100. // decapsulates the packet in reverse order.
  2101. client.relay_info_.push_back(relay2);
  2102. client.relay_info_.push_back(relay1);
  2103. // There's a conflict here. Both relays want the server to echo back option
  2104. // 120. According to RFC6422, section 6:
  2105. //
  2106. // When such a conflict exists, the DHCP server MUST choose no more than
  2107. // one of these options to forward to the client. The DHCP server MUST
  2108. // NOT forward more than one of these options to the client.
  2109. //
  2110. // By default, the DHCP server MUST choose the innermost value -- the
  2111. // value supplied by the relay agent closest to the DHCP client -- to
  2112. // forward to the DHCP client.
  2113. // Let the client do his thing.
  2114. client.doSARR();
  2115. int count110 = 0; // Let's count how many times option 110 was echoed back
  2116. int count120 = 0; // Let's count how many times option 120 was echoed back
  2117. int count130 = 0; // Let's count how many times option 130 was echoed back
  2118. OptionPtr opt120;
  2119. for (OptionCollection::const_iterator it = client.config_.options_.begin();
  2120. it != client.config_.options_.end(); ++it) {
  2121. switch (it->second->getType()) {
  2122. case 110:
  2123. count110++;
  2124. break;
  2125. case 120:
  2126. count120++;
  2127. opt120 = it->second;
  2128. break;
  2129. case 130:
  2130. count130++;
  2131. break;
  2132. default:
  2133. break;
  2134. }
  2135. }
  2136. // We expect to have exactly one instance of each option code.
  2137. EXPECT_EQ(1, count110);
  2138. EXPECT_EQ(1, count120);
  2139. EXPECT_EQ(1, count130);
  2140. // Now, let's check if the proper instance of option 120 was sent. It should
  2141. // match the content of what the first relay had sent.
  2142. ASSERT_TRUE(opt120);
  2143. vector<uint8_t> expected(10, 1);
  2144. EXPECT_TRUE(expected == opt120->getData());
  2145. }
  2146. // This test verifies that the server will send the option for which it
  2147. // has a candidate, rather than the option sent by the relay in the RSOO.
  2148. TEST_F(Dhcpv6SrvTest, rsooOverride) {
  2149. Dhcp6Client client;
  2150. // The client will be requesting specific options.
  2151. client.useORO(true);
  2152. // The following configuration enables RSOO options: 110 and 120.
  2153. // It also configures the server with option 120 which should
  2154. // "override" the option 120 sent in the RSOO by the relay.
  2155. string config =
  2156. "{"
  2157. " \"relay-supplied-options\": [ \"110\", \"120\" ],"
  2158. " \"option-def\": [ {"
  2159. " \"name\": \"foo\","
  2160. " \"code\": 120,"
  2161. " \"type\": \"binary\""
  2162. " } ],"
  2163. " \"option-data\": [ {"
  2164. " \"code\": 120,"
  2165. " \"data\": \"05\""
  2166. " } ],"
  2167. " \"preferred-lifetime\": 3000,"
  2168. " \"rebind-timer\": 2000, "
  2169. " \"renew-timer\": 1000, "
  2170. " \"subnet6\": [ { "
  2171. " \"pools\": [ { \"pool\": \"2001:db8::/64\" } ],"
  2172. " \"subnet\": \"2001:db8::/48\" "
  2173. " } ],"
  2174. " \"valid-lifetime\": 4000"
  2175. "}";
  2176. EXPECT_NO_THROW(configure(config, *client.getServer()));
  2177. // Fabricate the relay.
  2178. Pkt6::RelayInfo relay;
  2179. relay.msg_type_ = DHCPV6_RELAY_FORW;
  2180. relay.hop_count_ = 1;
  2181. relay.linkaddr_ = IOAddress("2001:db8::1");
  2182. relay.peeraddr_ = IOAddress("fe80::1");
  2183. vector<uint16_t> rsoo;
  2184. // The relay will send 2 options: 110, 120
  2185. rsoo.push_back(110);
  2186. rsoo.push_back(120);
  2187. // Use 0x1 as payload
  2188. OptionPtr opt = createRSOO(rsoo, 1);
  2189. relay.options_.insert(make_pair(opt->getType(), opt));
  2190. client.relay_info_.push_back(relay);
  2191. // Client should request option 120 in the ORO so as the server
  2192. // sends the configured option 120 to the client.
  2193. client.requestOption(120);
  2194. client.doSARR();
  2195. // The option 110 should be the one injected by the relay.
  2196. opt = client.config_.findOption(110);
  2197. ASSERT_TRUE(opt);
  2198. // We check that this is the option injected by the relay by
  2199. // checking option length. It should have 10 bytes long payload.
  2200. ASSERT_EQ(10, opt->getData().size());
  2201. // The second option should be the one configured on the server,
  2202. // rather than the one injected by the relay.
  2203. opt = client.config_.findOption(120);
  2204. ASSERT_TRUE(opt);
  2205. // It should have the size of 1.
  2206. ASSERT_EQ(1, opt->getData().size());
  2207. }
  2208. // Test checks if pkt6-advertise-received is bumped up correctly.
  2209. // Note that in properly configured network the server never receives Advertise
  2210. // messages.
  2211. TEST_F(Dhcpv6SrvTest, receiveAdvertiseStat) {
  2212. testReceiveStats(DHCPV6_ADVERTISE, "pkt6-advertise-received");
  2213. }
  2214. // Test checks if pkt6-reply-received is bumped up correctly.
  2215. // Note that in properly configured network the server never receives Reply
  2216. // messages.
  2217. TEST_F(Dhcpv6SrvTest, receiveReplyStat) {
  2218. testReceiveStats(DHCPV6_REPLY, "pkt6-reply-received");
  2219. }
  2220. // Test checks if pkt6-unknown-received is bumped up correctly.
  2221. TEST_F(Dhcpv6SrvTest, receiveUnknownStat) {
  2222. testReceiveStats(123, "pkt6-unknown-received");
  2223. }
  2224. // Test checks if pkt6-renew-received is bumped up correctly.
  2225. TEST_F(Dhcpv6SrvTest, receiveRenewStat) {
  2226. testReceiveStats(DHCPV6_RENEW, "pkt6-renew-received");
  2227. }
  2228. // Test checks if pkt6-rebind-received is bumped up correctly.
  2229. TEST_F(Dhcpv6SrvTest, receiveRebindStat) {
  2230. testReceiveStats(DHCPV6_REBIND, "pkt6-rebind-received");
  2231. }
  2232. // Test checks if pkt6-release-received is bumped up correctly.
  2233. TEST_F(Dhcpv6SrvTest, receiveReleaseStat) {
  2234. testReceiveStats(DHCPV6_RELEASE, "pkt6-release-received");
  2235. }
  2236. // Test checks if pkt6-decline-received is bumped up correctly.
  2237. TEST_F(Dhcpv6SrvTest, receiveDeclineStat) {
  2238. testReceiveStats(DHCPV6_DECLINE, "pkt6-decline-received");
  2239. }
  2240. // Test checks if reception of a malformed packet increases pkt-parse-failed
  2241. // and pkt6-receive-drop
  2242. TEST_F(Dhcpv6SrvTest, receiveParseFailedStat) {
  2243. using namespace isc::stats;
  2244. StatsMgr& mgr = StatsMgr::instance();
  2245. NakedDhcpv6Srv srv(0);
  2246. // Let's get a simple SOLICIT...
  2247. Pkt6Ptr pkt = PktCaptures::captureSimpleSolicit();
  2248. // And pretend it's packet is only 3 bytes long.
  2249. pkt->data_.resize(3);
  2250. // Check that those statistics are not set before the test
  2251. ObservationPtr pkt6_rcvd = mgr.getObservation("pkt6-received");
  2252. ObservationPtr parse_fail = mgr.getObservation("pkt6-parse-failed");
  2253. ObservationPtr recv_drop = mgr.getObservation("pkt6-receive-drop");
  2254. EXPECT_FALSE(pkt6_rcvd);
  2255. EXPECT_FALSE(parse_fail);
  2256. EXPECT_FALSE(recv_drop);
  2257. // Simulate that we have received that traffic
  2258. srv.fakeReceive(pkt);
  2259. // Server will now process to run its normal loop, but instead of calling
  2260. // IfaceMgr::receive6(), it will read all packets from the list set by
  2261. // fakeReceive()
  2262. srv.run();
  2263. // All expected statstics must be present.
  2264. pkt6_rcvd = mgr.getObservation("pkt6-received");
  2265. parse_fail = mgr.getObservation("pkt6-parse-failed");
  2266. recv_drop = mgr.getObservation("pkt6-receive-drop");
  2267. ASSERT_TRUE(pkt6_rcvd);
  2268. ASSERT_TRUE(parse_fail);
  2269. ASSERT_TRUE(recv_drop);
  2270. // They also must have expected values.
  2271. EXPECT_EQ(1, pkt6_rcvd->getInteger().first);
  2272. EXPECT_EQ(1, parse_fail->getInteger().first);
  2273. EXPECT_EQ(1, recv_drop->getInteger().first);
  2274. }
  2275. /// @todo: Add more negative tests for processX(), e.g. extend sanityCheck() test
  2276. /// to call processX() methods.
  2277. } // end of anonymous namespace