dhcp6_srv_unittest.cc 105 KB

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