dhcp6_srv_unittest.cc 104 KB

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