dhcp4_srv_unittest.cc 134 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700
  1. // Copyright (C) 2011-2017 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 <sstream>
  8. #include <asiolink/io_address.h>
  9. #include <cc/command_interpreter.h>
  10. #include <config/command_mgr.h>
  11. #include <dhcp4/tests/dhcp4_test_utils.h>
  12. #include <dhcp4/tests/dhcp4_client.h>
  13. #include <dhcp/tests/pkt_captures.h>
  14. #include <dhcp/dhcp4.h>
  15. #include <dhcp/iface_mgr.h>
  16. #include <dhcp/libdhcp++.h>
  17. #include <dhcp/option.h>
  18. #include <dhcp/option_int.h>
  19. #include <dhcp/option4_addrlst.h>
  20. #include <dhcp/option_custom.h>
  21. #include <dhcp/option_int_array.h>
  22. #include <dhcp/option_vendor.h>
  23. #include <dhcp/pkt_filter.h>
  24. #include <dhcp/pkt_filter_inet.h>
  25. #include <dhcp/docsis3_option_defs.h>
  26. #include <dhcp/tests/iface_mgr_test_config.h>
  27. #include <dhcp4/dhcp4_srv.h>
  28. #include <dhcp4/dhcp4_log.h>
  29. #include <dhcp4/json_config_parser.h>
  30. #include <dhcpsrv/cfgmgr.h>
  31. #include <dhcpsrv/lease_mgr.h>
  32. #include <dhcpsrv/lease_mgr_factory.h>
  33. #include <dhcpsrv/utils.h>
  34. #include <dhcpsrv/host_mgr.h>
  35. #include <gtest/gtest.h>
  36. #include <stats/stats_mgr.h>
  37. #include <boost/scoped_ptr.hpp>
  38. #include <iostream>
  39. #include <arpa/inet.h>
  40. using namespace std;
  41. using namespace isc;
  42. using namespace isc::dhcp;
  43. using namespace isc::data;
  44. using namespace isc::asiolink;
  45. using namespace isc::config;
  46. using namespace isc::dhcp::test;
  47. namespace {
  48. const char* CONFIGS[] = {
  49. // Configuration 0:
  50. // - 1 subnet: 10.254.226.0/25
  51. // - used for recorded traffic (see PktCaptures::captureRelayedDiscover)
  52. "{ \"interfaces-config\": {"
  53. " \"interfaces\": [ \"*\" ]"
  54. "},"
  55. "\"rebind-timer\": 2000, "
  56. "\"renew-timer\": 1000, "
  57. "\"subnet4\": [ { "
  58. " \"pools\": [ { \"pool\": \"10.254.226.0/25\" } ],"
  59. " \"subnet\": \"10.254.226.0/24\", "
  60. " \"rebind-timer\": 2000, "
  61. " \"renew-timer\": 1000, "
  62. " \"valid-lifetime\": 4000,"
  63. " \"interface\": \"eth0\" "
  64. " } ],"
  65. "\"valid-lifetime\": 4000 }",
  66. // Configuration 1:
  67. // - 1 subnet: 192.0.2.0/24
  68. // - MySQL Host Data Source configured
  69. "{ \"interfaces-config\": {"
  70. " \"interfaces\": [ \"*\" ]"
  71. "},"
  72. "\"hosts-database\": {"
  73. " \"type\": \"mysql\","
  74. " \"name\": \"keatest\","
  75. " \"user\": \"keatest\","
  76. " \"password\": \"keatest\""
  77. "},"
  78. "\"rebind-timer\": 2000, "
  79. "\"renew-timer\": 1000, "
  80. "\"subnet4\": [ { "
  81. " \"pools\": [ { \"pool\": \"192.0.2.1 - 192.0.2.100\" } ],"
  82. " \"subnet\": \"192.0.2.0/24\", "
  83. " \"rebind-timer\": 2000, "
  84. " \"renew-timer\": 1000, "
  85. " \"valid-lifetime\": 4000,"
  86. " \"interface\": \"eth0\" "
  87. " } ],"
  88. "\"valid-lifetime\": 4000 }",
  89. // Configuration 2:
  90. // - 1 subnet, 2 global options (one forced with always-send)
  91. "{"
  92. " \"interfaces-config\": {"
  93. " \"interfaces\": [ \"*\" ] }, "
  94. " \"rebind-timer\": 2000, "
  95. " \"renew-timer\": 1000, "
  96. " \"valid-lifetime\": 4000, "
  97. " \"subnet4\": [ {"
  98. " \"pools\": [ { \"pool\": \"192.0.2.1 - 192.0.2.100\" } ], "
  99. " \"subnet\": \"192.0.2.0/24\""
  100. " } ], "
  101. " \"option-data\": ["
  102. " {"
  103. " \"name\": \"default-ip-ttl\", "
  104. " \"data\": \"FF\", "
  105. " \"csv-format\": false"
  106. " }, "
  107. " {"
  108. " \"name\": \"ip-forwarding\", "
  109. " \"data\": \"false\", "
  110. " \"always-send\": true"
  111. " }"
  112. " ]"
  113. "}",
  114. // Configuration 3:
  115. // - one subnet, with one pool
  116. // - user-contexts defined in both subnet and pool
  117. "{"
  118. " \"subnet4\": [ { "
  119. " \"pools\": [ { \"pool\": \"10.254.226.0/25\","
  120. " \"user-context\": { \"value\": 42 } } ],"
  121. " \"subnet\": \"10.254.226.0/24\", "
  122. " \"user-context\": {"
  123. " \"secure\": false"
  124. " }"
  125. " } ],"
  126. "\"valid-lifetime\": 4000 }",
  127. };
  128. // This test verifies that the destination address of the response
  129. // message is set to giaddr, when giaddr is set to non-zero address
  130. // in the received message.
  131. TEST_F(Dhcpv4SrvTest, adjustIfaceDataRelay) {
  132. IfaceMgrTestConfig test_config(true);
  133. IfaceMgr::instance().openSockets4();
  134. // Create the instance of the incoming packet.
  135. boost::shared_ptr<Pkt4> req(new Pkt4(DHCPDISCOVER, 1234));
  136. // Set the giaddr to non-zero address and hops to non-zero value
  137. // as if it was relayed.
  138. req->setGiaddr(IOAddress("192.0.1.1"));
  139. req->setHops(2);
  140. // Set ciaddr to zero. This simulates the client which applies
  141. // for the new lease.
  142. req->setCiaddr(IOAddress("0.0.0.0"));
  143. // Clear broadcast flag.
  144. req->setFlags(0x0000);
  145. // Set local address, port and interface.
  146. req->setLocalAddr(IOAddress("192.0.2.5"));
  147. req->setLocalPort(1001);
  148. req->setIface("eth1");
  149. req->setIndex(1);
  150. // Create the exchange using the req.
  151. Dhcpv4Exchange ex = createExchange(req);
  152. Pkt4Ptr resp = ex.getResponse();
  153. resp->setYiaddr(IOAddress("192.0.1.100"));
  154. // Clear the remote address.
  155. resp->setRemoteAddr(IOAddress("0.0.0.0"));
  156. // Set hops value for the response.
  157. resp->setHops(req->getHops());
  158. // This function never throws.
  159. ASSERT_NO_THROW(NakedDhcpv4Srv::adjustIfaceData(ex));
  160. // Now the destination address should be relay's address.
  161. EXPECT_EQ("192.0.1.1", resp->getRemoteAddr().toText());
  162. // The query has been relayed, so the response must be sent to the port 67.
  163. EXPECT_EQ(DHCP4_SERVER_PORT, resp->getRemotePort());
  164. // Local address should be the address assigned to interface eth1.
  165. EXPECT_EQ("192.0.2.5", resp->getLocalAddr().toText());
  166. // The local port is always DHCPv4 server port 67.
  167. EXPECT_EQ(DHCP4_SERVER_PORT, resp->getLocalPort());
  168. // We will send response over the same interface which was used to receive
  169. // query.
  170. EXPECT_EQ("eth1", resp->getIface());
  171. EXPECT_EQ(1, resp->getIndex());
  172. // Let's do another test and set other fields: ciaddr and
  173. // flags. By doing it, we want to make sure that the relay
  174. // address will take precedence.
  175. req->setGiaddr(IOAddress("192.0.1.50"));
  176. req->setCiaddr(IOAddress("192.0.1.11"));
  177. req->setFlags(Pkt4::FLAG_BROADCAST_MASK);
  178. resp->setYiaddr(IOAddress("192.0.1.100"));
  179. // Clear remote address.
  180. resp->setRemoteAddr(IOAddress("0.0.0.0"));
  181. ASSERT_NO_THROW(NakedDhcpv4Srv::adjustIfaceData(ex));
  182. // Response should be sent back to the relay address.
  183. EXPECT_EQ("192.0.1.50", resp->getRemoteAddr().toText());
  184. }
  185. // This test verifies that the destination address of the response message
  186. // is set to ciaddr when giaddr is set to zero and the ciaddr is set to
  187. // non-zero address in the received message. This is the case when the
  188. // client is in Renew or Rebind state.
  189. TEST_F(Dhcpv4SrvTest, adjustIfaceDataRenew) {
  190. IfaceMgrTestConfig test_config(true);
  191. IfaceMgr::instance().openSockets4();
  192. // Create instance of the incoming packet.
  193. boost::shared_ptr<Pkt4> req(new Pkt4(DHCPDISCOVER, 1234));
  194. // Clear giaddr to simulate direct packet.
  195. req->setGiaddr(IOAddress("0.0.0.0"));
  196. // Set ciaddr to non-zero address. The response should be sent to this
  197. // address as the client is in renewing or rebinding state (it is fully
  198. // configured).
  199. req->setCiaddr(IOAddress("192.0.1.15"));
  200. // Let's configure broadcast flag. It should be ignored because
  201. // we are responding directly to the client having an address
  202. // and trying to extend his lease. Broadcast flag is only used
  203. // when new lease is acquired and server must make a decision
  204. // whether to unicast the response to the acquired address or
  205. // broadcast it.
  206. req->setFlags(Pkt4::FLAG_BROADCAST_MASK);
  207. // This is a direct message, so the hops should be cleared.
  208. req->setHops(0);
  209. // Set local unicast address as if we are renewing a lease.
  210. req->setLocalAddr(IOAddress("192.0.2.1"));
  211. // Request is received on the DHCPv4 server port.
  212. req->setLocalPort(DHCP4_SERVER_PORT);
  213. // Set the interface. The response should be sent over the same interface.
  214. req->setIface("eth1");
  215. req->setIndex(1);
  216. // Create the exchange using the req.
  217. Dhcpv4Exchange ex = createExchange(req);
  218. Pkt4Ptr resp = ex.getResponse();
  219. // Let's extend the lease for the client in such a way that
  220. // it will actually get different address. The response
  221. // should not be sent to this address but rather to ciaddr
  222. // as client still have ciaddr configured.
  223. resp->setYiaddr(IOAddress("192.0.1.13"));
  224. // Clear the remote address.
  225. resp->setRemoteAddr(IOAddress("0.0.0.0"));
  226. // Copy hops value from the query.
  227. resp->setHops(req->getHops());
  228. ASSERT_NO_THROW(NakedDhcpv4Srv::adjustIfaceData(ex));
  229. // Check that server responds to ciaddr
  230. EXPECT_EQ("192.0.1.15", resp->getRemoteAddr().toText());
  231. // The query was non-relayed, so the response should be sent to a DHCPv4
  232. // client port 68.
  233. EXPECT_EQ(DHCP4_CLIENT_PORT, resp->getRemotePort());
  234. // The response should be sent from the unicast address on which the
  235. // query has been received.
  236. EXPECT_EQ("192.0.2.1", resp->getLocalAddr().toText());
  237. // The response should be sent from the DHCPv4 server port.
  238. EXPECT_EQ(DHCP4_SERVER_PORT, resp->getLocalPort());
  239. // The interface data should match the data in the query.
  240. EXPECT_EQ("eth1", resp->getIface());
  241. EXPECT_EQ(1, resp->getIndex());
  242. }
  243. // This test verifies that the destination address of the response message
  244. // is set correctly when giaddr and ciaddr is zeroed in the received message
  245. // and the new lease is acquired. The lease address is carried in the
  246. // response message in the yiaddr field. In this case destination address
  247. // of the response should be set to yiaddr if server supports direct responses
  248. // to the client which doesn't have an address yet or broadcast if the server
  249. // doesn't support direct responses.
  250. TEST_F(Dhcpv4SrvTest, adjustIfaceDataSelect) {
  251. IfaceMgrTestConfig test_config(true);
  252. IfaceMgr::instance().openSockets4();
  253. // Create instance of the incoming packet.
  254. boost::shared_ptr<Pkt4> req(new Pkt4(DHCPDISCOVER, 1234));
  255. // Clear giaddr to simulate direct packet.
  256. req->setGiaddr(IOAddress("0.0.0.0"));
  257. // Clear client address as it hasn't got any address configured yet.
  258. req->setCiaddr(IOAddress("0.0.0.0"));
  259. // Let's clear the broadcast flag.
  260. req->setFlags(0);
  261. // This is a non-relayed message, so let's clear hops count.
  262. req->setHops(0);
  263. // The query is sent to the broadcast address in the Select state.
  264. req->setLocalAddr(IOAddress("255.255.255.255"));
  265. // The query has been received on the DHCPv4 server port 67.
  266. req->setLocalPort(DHCP4_SERVER_PORT);
  267. // Set the interface. The response should be sent via the same interface.
  268. req->setIface("eth1");
  269. req->setIndex(1);
  270. // Create the exchange using the req.
  271. Dhcpv4Exchange ex = createExchange(req);
  272. Pkt4Ptr resp = ex.getResponse();
  273. // Assign some new address for this client.
  274. resp->setYiaddr(IOAddress("192.0.1.13"));
  275. // Clear the remote address.
  276. resp->setRemoteAddr(IOAddress("0.0.0.0"));
  277. // Copy hops count.
  278. resp->setHops(req->getHops());
  279. // We want to test the case, when the server (packet filter) doesn't support
  280. // ddirect responses to the client which doesn't have an address yet. In
  281. // case, the server should send its response to the broadcast address.
  282. // We can control whether the current packet filter returns that its support
  283. // direct responses or not.
  284. test_config.setDirectResponse(false);
  285. // When running unit tests, the IfaceMgr is using the default Packet
  286. // Filtering class, PktFilterInet. This class does not support direct
  287. // responses to clients without address assigned. When giaddr and ciaddr
  288. // are zero and client has just got new lease, the assigned address is
  289. // carried in yiaddr. In order to send this address to the client,
  290. // server must broadcast its response.
  291. ASSERT_NO_THROW(NakedDhcpv4Srv::adjustIfaceData(ex));
  292. // Check that the response is sent to broadcast address as the
  293. // server doesn't have capability to respond directly.
  294. EXPECT_EQ("255.255.255.255", resp->getRemoteAddr().toText());
  295. // Although the query has been sent to the broadcast address, the
  296. // server should select a unicast address on the particular interface
  297. // as a source address for the response.
  298. EXPECT_EQ("192.0.2.3", resp->getLocalAddr().toText());
  299. // The response should be sent from the DHCPv4 server port.
  300. EXPECT_EQ(DHCP4_SERVER_PORT, resp->getLocalPort());
  301. // The response should be sent via the same interface through which
  302. // query has been received.
  303. EXPECT_EQ("eth1", resp->getIface());
  304. EXPECT_EQ(1, resp->getIndex());
  305. // We also want to test the case when the server has capability to
  306. // respond directly to the client which is not configured. Server
  307. // makes decision whether it responds directly or broadcast its
  308. // response based on the capability reported by IfaceMgr. We can
  309. // control whether the current packet filter returns that it supports
  310. // direct responses or not.
  311. test_config.setDirectResponse(true);
  312. // Now we expect that the server will send its response to the
  313. // address assigned for the client.
  314. ASSERT_NO_THROW(NakedDhcpv4Srv::adjustIfaceData(ex));
  315. EXPECT_EQ("192.0.1.13", resp->getRemoteAddr().toText());
  316. }
  317. // This test verifies that the destination address of the response message
  318. // is set to broadcast address when client set broadcast flag in its
  319. // query. Client sets this flag to indicate that it can't receive direct
  320. // responses from the server when it doesn't have its interface configured.
  321. // Server must respect broadcast flag.
  322. TEST_F(Dhcpv4SrvTest, adjustIfaceDataBroadcast) {
  323. IfaceMgrTestConfig test_config(true);
  324. IfaceMgr::instance().openSockets4();
  325. // Create instance of the incoming packet.
  326. boost::shared_ptr<Pkt4> req(new Pkt4(DHCPDISCOVER, 1234));
  327. // Clear giaddr to simulate direct packet.
  328. req->setGiaddr(IOAddress("0.0.0.0"));
  329. // Clear client address as it hasn't got any address configured yet.
  330. req->setCiaddr(IOAddress("0.0.0.0"));
  331. // The query is sent to the broadcast address in the Select state.
  332. req->setLocalAddr(IOAddress("255.255.255.255"));
  333. // The query has been received on the DHCPv4 server port 67.
  334. req->setLocalPort(DHCP4_SERVER_PORT);
  335. // Set the interface. The response should be sent via the same interface.
  336. req->setIface("eth1");
  337. req->setIndex(1);
  338. // Let's set the broadcast flag.
  339. req->setFlags(Pkt4::FLAG_BROADCAST_MASK);
  340. // Create the exchange using the req.
  341. Dhcpv4Exchange ex = createExchange(req);
  342. Pkt4Ptr resp = ex.getResponse();
  343. // Assign some new address for this client.
  344. resp->setYiaddr(IOAddress("192.0.1.13"));
  345. // Clear the remote address.
  346. resp->setRemoteAddr(IOAddress("0.0.0.0"));
  347. ASSERT_NO_THROW(NakedDhcpv4Srv::adjustIfaceData(ex));
  348. // Server must respond to broadcast address when client desired that
  349. // by setting the broadcast flag in its request.
  350. EXPECT_EQ("255.255.255.255", resp->getRemoteAddr().toText());
  351. // Although the query has been sent to the broadcast address, the
  352. // server should select a unicast address on the particular interface
  353. // as a source address for the response.
  354. EXPECT_EQ("192.0.2.3", resp->getLocalAddr().toText());
  355. // The response should be sent from the DHCPv4 server port.
  356. EXPECT_EQ(DHCP4_SERVER_PORT, resp->getLocalPort());
  357. // The response should be sent via the same interface through which
  358. // query has been received.
  359. EXPECT_EQ("eth1", resp->getIface());
  360. EXPECT_EQ(1, resp->getIndex());
  361. }
  362. // This test verifies that the mandatory to copy fields and options
  363. // are really copied into the response.
  364. TEST_F(Dhcpv4SrvTest, initResponse) {
  365. Pkt4Ptr query(new Pkt4(DHCPDISCOVER, 1234));
  366. // Set fields which must be copied
  367. query->setIface("foo");
  368. query->setIndex(111);
  369. query->setHops(5);
  370. const HWAddr& hw = HWAddr::fromText("11:22:33:44:55:66:77:88", 10);
  371. HWAddrPtr hw_addr(new HWAddr(hw));
  372. query->setHWAddr(hw_addr);
  373. query->setGiaddr(IOAddress("10.10.10.10"));
  374. const HWAddr& src_hw = HWAddr::fromText("e4:ce:8f:12:34:56");
  375. HWAddrPtr src_hw_addr(new HWAddr(src_hw));
  376. query->setLocalHWAddr(src_hw_addr);
  377. const HWAddr& dst_hw = HWAddr::fromText("e8:ab:cd:78:9a:bc");
  378. HWAddrPtr dst_hw_addr(new HWAddr(dst_hw));
  379. query->setRemoteHWAddr(dst_hw_addr);
  380. query->setFlags(BOOTP_BROADCAST);
  381. // Add options which must be copied
  382. // client-id echo is optional
  383. // rai echo is done in relayAgentInfoEcho
  384. // Do subnet selection option
  385. OptionDefinitionPtr sbnsel_def = LibDHCP::getOptionDef(DHCP4_OPTION_SPACE,
  386. DHO_SUBNET_SELECTION);
  387. ASSERT_TRUE(sbnsel_def);
  388. OptionCustomPtr sbnsel(new OptionCustom(*sbnsel_def, Option::V4));
  389. ASSERT_TRUE(sbnsel);
  390. sbnsel->writeAddress(IOAddress("192.0.2.3"));
  391. query->addOption(sbnsel);
  392. // Create exchange and get Response
  393. Dhcpv4Exchange ex = createExchange(query);
  394. Pkt4Ptr response = ex.getResponse();
  395. ASSERT_TRUE(response);
  396. // Check fields
  397. EXPECT_EQ("foo", response->getIface());
  398. EXPECT_EQ(111, response->getIndex());
  399. EXPECT_TRUE(response->getSiaddr().isV4Zero());
  400. EXPECT_TRUE(response->getCiaddr().isV4Zero());
  401. EXPECT_EQ(5, response->getHops());
  402. EXPECT_TRUE(hw == *response->getHWAddr());
  403. EXPECT_EQ(IOAddress("10.10.10.10"), response->getGiaddr());
  404. EXPECT_TRUE(src_hw == *response->getLocalHWAddr());
  405. EXPECT_TRUE(dst_hw == *response->getRemoteHWAddr());
  406. EXPECT_TRUE(BOOTP_BROADCAST == response->getFlags());
  407. // Check options (i.e., subnet selection option)
  408. OptionPtr resp_sbnsel = response->getOption(DHO_SUBNET_SELECTION);
  409. ASSERT_TRUE(resp_sbnsel);
  410. OptionCustomPtr resp_custom =
  411. boost::dynamic_pointer_cast<OptionCustom>(resp_sbnsel);
  412. ASSERT_TRUE(resp_custom);
  413. IOAddress subnet_addr("0.0.0.0");
  414. ASSERT_NO_THROW(subnet_addr = resp_custom->readAddress());
  415. EXPECT_EQ(IOAddress("192.0.2.3"), subnet_addr);
  416. }
  417. // This test verifies that the server identifier option is appended to
  418. // a specified DHCPv4 message and the server identifier is correct.
  419. TEST_F(Dhcpv4SrvTest, appendServerID) {
  420. Pkt4Ptr query(new Pkt4(DHCPDISCOVER, 1234));
  421. Dhcpv4Exchange ex = createExchange(query);
  422. Pkt4Ptr response = ex.getResponse();
  423. // Set a local address. It is required by the function under test
  424. // to create the Server Identifier option.
  425. response->setLocalAddr(IOAddress("192.0.3.1"));
  426. // Append the Server Identifier.
  427. ASSERT_NO_THROW(NakedDhcpv4Srv::appendServerID(ex));
  428. // Make sure that the option has been added.
  429. OptionPtr opt = response->getOption(DHO_DHCP_SERVER_IDENTIFIER);
  430. ASSERT_TRUE(opt);
  431. Option4AddrLstPtr opt_server_id =
  432. boost::dynamic_pointer_cast<Option4AddrLst>(opt);
  433. ASSERT_TRUE(opt_server_id);
  434. // The option is represented as a list of IPv4 addresses but with
  435. // only one address added.
  436. Option4AddrLst::AddressContainer addrs = opt_server_id->getAddresses();
  437. ASSERT_EQ(1, addrs.size());
  438. // This address should match the local address of the packet.
  439. EXPECT_EQ("192.0.3.1", addrs[0].toText());
  440. }
  441. // Sanity check. Verifies that both Dhcpv4Srv and its derived
  442. // class NakedDhcpv4Srv can be instantiated and destroyed.
  443. TEST_F(Dhcpv4SrvTest, basic) {
  444. // Check that the base class can be instantiated
  445. boost::scoped_ptr<Dhcpv4Srv> srv;
  446. ASSERT_NO_THROW(srv.reset(new Dhcpv4Srv(DHCP4_SERVER_PORT + 10000, false,
  447. false)));
  448. srv.reset();
  449. // We have to close open sockets because further in this test we will
  450. // call the Dhcpv4Srv constructor again. This constructor will try to
  451. // set the appropriate packet filter class for IfaceMgr. This requires
  452. // that all sockets are closed.
  453. IfaceMgr::instance().closeSockets();
  454. // Check that the derived class can be instantiated
  455. boost::scoped_ptr<NakedDhcpv4Srv> naked_srv;
  456. ASSERT_NO_THROW(
  457. naked_srv.reset(new NakedDhcpv4Srv(DHCP4_SERVER_PORT + 10000)));
  458. // Close sockets again for the next test.
  459. IfaceMgr::instance().closeSockets();
  460. ASSERT_NO_THROW(naked_srv.reset(new NakedDhcpv4Srv(0)));
  461. }
  462. // Verifies that DISCOVER message can be processed correctly,
  463. // that the OFFER message generated in response is valid and
  464. // contains necessary options.
  465. //
  466. // Note: this test focuses on the packet correctness. There
  467. // are other tests that verify correctness of the allocation
  468. // engine. See DiscoverBasic, DiscoverHint, DiscoverNoClientId
  469. // and DiscoverInvalidHint.
  470. TEST_F(Dhcpv4SrvTest, processDiscover) {
  471. testDiscoverRequest(DHCPDISCOVER);
  472. }
  473. // Verifies that REQUEST message can be processed correctly,
  474. // that the OFFER message generated in response is valid and
  475. // contains necessary options.
  476. //
  477. // Note: this test focuses on the packet correctness. There
  478. // are other tests that verify correctness of the allocation
  479. // engine. See DiscoverBasic, DiscoverHint, DiscoverNoClientId
  480. // and DiscoverInvalidHint.
  481. TEST_F(Dhcpv4SrvTest, processRequest) {
  482. testDiscoverRequest(DHCPREQUEST);
  483. }
  484. TEST_F(Dhcpv4SrvTest, processRelease) {
  485. NakedDhcpv4Srv srv;
  486. Pkt4Ptr pkt(new Pkt4(DHCPRELEASE, 1234));
  487. // Should not throw
  488. EXPECT_NO_THROW(srv.processRelease(pkt));
  489. }
  490. // This test verifies that incoming DISCOVER can be handled properly, that an
  491. // OFFER is generated, that the response has an address and that address
  492. // really belongs to the configured pool.
  493. //
  494. // constructed very simple DISCOVER message with:
  495. // - client-id option
  496. //
  497. // expected returned OFFER message:
  498. // - copy of client-id
  499. // - server-id
  500. // - offered address
  501. TEST_F(Dhcpv4SrvTest, DiscoverBasic) {
  502. IfaceMgrTestConfig test_config(true);
  503. IfaceMgr::instance().openSockets4();
  504. boost::scoped_ptr<NakedDhcpv4Srv> srv;
  505. ASSERT_NO_THROW(srv.reset(new NakedDhcpv4Srv(0)));
  506. Pkt4Ptr dis = Pkt4Ptr(new Pkt4(DHCPDISCOVER, 1234));
  507. dis->setRemoteAddr(IOAddress("192.0.2.1"));
  508. OptionPtr clientid = generateClientId();
  509. dis->addOption(clientid);
  510. dis->setIface("eth1");
  511. // Pass it to the server and get an offer
  512. Pkt4Ptr offer = srv->processDiscover(dis);
  513. // Check if we get response at all
  514. checkResponse(offer, DHCPOFFER, 1234);
  515. // Check that address was returned from proper range, that its lease
  516. // lifetime is correct, that T1 and T2 are returned properly
  517. checkAddressParams(offer, subnet_, true, true);
  518. // Check identifiers
  519. checkServerId(offer, srv->getServerID());
  520. checkClientId(offer, clientid);
  521. }
  522. // Check that option 58 and 59 are not included if they are not specified.
  523. TEST_F(Dhcpv4SrvTest, DiscoverNoTimers) {
  524. IfaceMgrTestConfig test_config(true);
  525. IfaceMgr::instance().openSockets4();
  526. boost::scoped_ptr<NakedDhcpv4Srv> srv;
  527. ASSERT_NO_THROW(srv.reset(new NakedDhcpv4Srv(0)));
  528. Pkt4Ptr dis = Pkt4Ptr(new Pkt4(DHCPDISCOVER, 1234));
  529. dis->setRemoteAddr(IOAddress("192.0.2.1"));
  530. OptionPtr clientid = generateClientId();
  531. dis->addOption(clientid);
  532. dis->setIface("eth1");
  533. // Recreate a subnet but set T1 and T2 to "unspecified".
  534. subnet_.reset(new Subnet4(IOAddress("192.0.2.0"), 24,
  535. Triplet<uint32_t>(),
  536. Triplet<uint32_t>(),
  537. 3000));
  538. pool_ = Pool4Ptr(new Pool4(IOAddress("192.0.2.100"),
  539. IOAddress("192.0.2.110")));
  540. subnet_->addPool(pool_);
  541. CfgMgr::instance().clear();
  542. CfgMgr::instance().getStagingCfg()->getCfgSubnets4()->add(subnet_);
  543. CfgMgr::instance().commit();
  544. // Pass it to the server and get an offer
  545. Pkt4Ptr offer = srv->processDiscover(dis);
  546. // Check if we get response at all
  547. checkResponse(offer, DHCPOFFER, 1234);
  548. // T1 and T2 timers must not be present.
  549. checkAddressParams(offer, subnet_, false, false);
  550. // Check identifiers
  551. checkServerId(offer, srv->getServerID());
  552. checkClientId(offer, clientid);
  553. }
  554. // This test verifies that incoming DISCOVER can be handled properly, that an
  555. // OFFER is generated, that the response has an address and that address
  556. // really belongs to the configured pool.
  557. //
  558. // constructed very simple DISCOVER message with:
  559. // - client-id option
  560. // - address set to specific value as hint, but that hint is invalid
  561. //
  562. // expected returned OFFER message:
  563. // - copy of client-id
  564. // - server-id
  565. // - offered address (!= hint)
  566. TEST_F(Dhcpv4SrvTest, DiscoverInvalidHint) {
  567. IfaceMgrTestConfig test_config(true);
  568. IfaceMgr::instance().openSockets4();
  569. boost::scoped_ptr<NakedDhcpv4Srv> srv;
  570. ASSERT_NO_THROW(srv.reset(new NakedDhcpv4Srv(0)));
  571. IOAddress hint("10.1.2.3");
  572. Pkt4Ptr dis = Pkt4Ptr(new Pkt4(DHCPDISCOVER, 1234));
  573. dis->setRemoteAddr(IOAddress("192.0.2.107"));
  574. OptionPtr clientid = generateClientId();
  575. dis->addOption(clientid);
  576. dis->setYiaddr(hint);
  577. dis->setIface("eth1");
  578. // Pass it to the server and get an offer
  579. Pkt4Ptr offer = srv->processDiscover(dis);
  580. // Check if we get response at all
  581. checkResponse(offer, DHCPOFFER, 1234);
  582. // Check that address was returned from proper range, that its lease
  583. // lifetime is correct, that T1 and T2 are returned properly
  584. checkAddressParams(offer, subnet_, true, true);
  585. EXPECT_NE(offer->getYiaddr(), hint);
  586. // Check identifiers
  587. checkServerId(offer, srv->getServerID());
  588. checkClientId(offer, clientid);
  589. }
  590. /// @todo: Add a test that client sends hint that is in pool, but currently
  591. /// being used by a different client.
  592. // This test checks that the server is offering different addresses to different
  593. // clients in OFFERs. Please note that OFFER is not a guarantee that such
  594. // an address will be assigned. Had the pool was very small and contained only
  595. // 2 addresses, the third client would get the same offer as the first one
  596. // and this is a correct behavior. It is REQUEST that will fail for the third
  597. // client. OFFER is basically saying "if you send me a request, you will
  598. // probably get an address like this" (there are no guarantees).
  599. TEST_F(Dhcpv4SrvTest, ManyDiscovers) {
  600. IfaceMgrTestConfig test_config(true);
  601. IfaceMgr::instance().openSockets4();
  602. boost::scoped_ptr<NakedDhcpv4Srv> srv;
  603. ASSERT_NO_THROW(srv.reset(new NakedDhcpv4Srv(0)));
  604. Pkt4Ptr dis1 = Pkt4Ptr(new Pkt4(DHCPDISCOVER, 1234));
  605. Pkt4Ptr dis2 = Pkt4Ptr(new Pkt4(DHCPDISCOVER, 2345));
  606. Pkt4Ptr dis3 = Pkt4Ptr(new Pkt4(DHCPDISCOVER, 3456));
  607. dis1->setRemoteAddr(IOAddress("192.0.2.1"));
  608. dis2->setRemoteAddr(IOAddress("192.0.2.2"));
  609. dis3->setRemoteAddr(IOAddress("192.0.2.3"));
  610. // Assign interfaces
  611. dis1->setIface("eth1");
  612. dis2->setIface("eth1");
  613. dis3->setIface("eth1");
  614. // Different client-id sizes
  615. OptionPtr clientid1 = generateClientId(4); // length 4
  616. OptionPtr clientid2 = generateClientId(5); // length 5
  617. OptionPtr clientid3 = generateClientId(6); // length 6
  618. dis1->addOption(clientid1);
  619. dis2->addOption(clientid2);
  620. dis3->addOption(clientid3);
  621. // Pass it to the server and get an offer
  622. Pkt4Ptr offer1 = srv->processDiscover(dis1);
  623. Pkt4Ptr offer2 = srv->processDiscover(dis2);
  624. Pkt4Ptr offer3 = srv->processDiscover(dis3);
  625. // Check if we get response at all
  626. checkResponse(offer1, DHCPOFFER, 1234);
  627. checkResponse(offer2, DHCPOFFER, 2345);
  628. checkResponse(offer3, DHCPOFFER, 3456);
  629. IOAddress addr1 = offer1->getYiaddr();
  630. IOAddress addr2 = offer2->getYiaddr();
  631. IOAddress addr3 = offer3->getYiaddr();
  632. // Check that the assigned address is indeed from the configured pool
  633. checkAddressParams(offer1, subnet_, true, true);
  634. checkAddressParams(offer2, subnet_, true, true);
  635. checkAddressParams(offer3, subnet_, true, true);
  636. // Check server-ids
  637. checkServerId(offer1, srv->getServerID());
  638. checkServerId(offer2, srv->getServerID());
  639. checkServerId(offer3, srv->getServerID());
  640. checkClientId(offer1, clientid1);
  641. checkClientId(offer2, clientid2);
  642. checkClientId(offer3, clientid3);
  643. // Finally check that the addresses offered are different
  644. EXPECT_NE(addr1, addr2);
  645. EXPECT_NE(addr2, addr3);
  646. EXPECT_NE(addr3, addr1);
  647. cout << "Offered address to client1=" << addr1 << endl;
  648. cout << "Offered address to client2=" << addr2 << endl;
  649. cout << "Offered address to client3=" << addr3 << endl;
  650. }
  651. // Checks whether echoing back client-id is controllable, i.e.
  652. // whether the server obeys echo-client-id and sends (or not)
  653. // client-id
  654. TEST_F(Dhcpv4SrvTest, discoverEchoClientId) {
  655. IfaceMgrTestConfig test_config(true);
  656. IfaceMgr::instance().openSockets4();
  657. NakedDhcpv4Srv srv(0);
  658. Pkt4Ptr dis = Pkt4Ptr(new Pkt4(DHCPDISCOVER, 1234));
  659. dis->setRemoteAddr(IOAddress("192.0.2.1"));
  660. OptionPtr clientid = generateClientId();
  661. dis->addOption(clientid);
  662. dis->setIface("eth1");
  663. // Pass it to the server and get an offer
  664. Pkt4Ptr offer = srv.processDiscover(dis);
  665. // Check if we get response at all
  666. checkResponse(offer, DHCPOFFER, 1234);
  667. checkClientId(offer, clientid);
  668. ConstSrvConfigPtr cfg = CfgMgr::instance().getCurrentCfg();
  669. const Subnet4Collection* subnets = cfg->getCfgSubnets4()->getAll();
  670. ASSERT_EQ(1, subnets->size());
  671. CfgMgr::instance().clear();
  672. CfgMgr::instance().getStagingCfg()->getCfgSubnets4()->add(subnets->at(0));
  673. CfgMgr::instance().getStagingCfg()->setEchoClientId(false);
  674. CfgMgr::instance().commit();
  675. offer = srv.processDiscover(dis);
  676. // Check if we get response at all
  677. checkResponse(offer, DHCPOFFER, 1234);
  678. checkClientId(offer, clientid);
  679. }
  680. // Check that option 58 and 59 are not included if they are not specified.
  681. TEST_F(Dhcpv4SrvTest, RequestNoTimers) {
  682. IfaceMgrTestConfig test_config(true);
  683. IfaceMgr::instance().openSockets4();
  684. boost::scoped_ptr<NakedDhcpv4Srv> srv;
  685. ASSERT_NO_THROW(srv.reset(new NakedDhcpv4Srv(0)));
  686. Pkt4Ptr req = Pkt4Ptr(new Pkt4(DHCPREQUEST, 1234));
  687. req->setRemoteAddr(IOAddress("192.0.2.1"));
  688. OptionPtr clientid = generateClientId();
  689. req->addOption(clientid);
  690. req->setIface("eth1");
  691. // Recreate a subnet but set T1 and T2 to "unspecified".
  692. subnet_.reset(new Subnet4(IOAddress("192.0.2.0"), 24,
  693. Triplet<uint32_t>(),
  694. Triplet<uint32_t>(),
  695. 3000));
  696. pool_ = Pool4Ptr(new Pool4(IOAddress("192.0.2.100"),
  697. IOAddress("192.0.2.110")));
  698. subnet_->addPool(pool_);
  699. CfgMgr::instance().clear();
  700. CfgMgr::instance().getStagingCfg()->getCfgSubnets4()->add(subnet_);
  701. CfgMgr::instance().commit();
  702. // Pass it to the server and get an ACK.
  703. Pkt4Ptr ack = srv->processRequest(req);
  704. // Check if we get response at all
  705. checkResponse(ack, DHCPACK, 1234);
  706. // T1 and T2 timers must not be present.
  707. checkAddressParams(ack, subnet_, false, false);
  708. // Check identifiers
  709. checkServerId(ack, srv->getServerID());
  710. checkClientId(ack, clientid);
  711. }
  712. // Checks whether echoing back client-id is controllable
  713. TEST_F(Dhcpv4SrvTest, requestEchoClientId) {
  714. IfaceMgrTestConfig test_config(true);
  715. IfaceMgr::instance().openSockets4();
  716. NakedDhcpv4Srv srv(0);
  717. Pkt4Ptr dis = Pkt4Ptr(new Pkt4(DHCPREQUEST, 1234));
  718. dis->setRemoteAddr(IOAddress("192.0.2.1"));
  719. OptionPtr clientid = generateClientId();
  720. dis->addOption(clientid);
  721. dis->setIface("eth1");
  722. // Pass it to the server and get ACK
  723. Pkt4Ptr ack = srv.processRequest(dis);
  724. // Check if we get response at all
  725. checkResponse(ack, DHCPACK, 1234);
  726. checkClientId(ack, clientid);
  727. ConstSrvConfigPtr cfg = CfgMgr::instance().getCurrentCfg();
  728. const Subnet4Collection* subnets = cfg->getCfgSubnets4()->getAll();
  729. ASSERT_EQ(1, subnets->size());
  730. CfgMgr::instance().clear();
  731. CfgMgr::instance().getStagingCfg()->getCfgSubnets4()->add(subnets->at(0));
  732. CfgMgr::instance().getStagingCfg()->setEchoClientId(false);
  733. CfgMgr::instance().commit();
  734. ack = srv.processRequest(dis);
  735. // Check if we get response at all
  736. checkResponse(ack, DHCPACK, 1234);
  737. checkClientId(ack, clientid);
  738. }
  739. // This test verifies that incoming (positive) REQUEST/Renewing can be handled properly, that a
  740. // REPLY is generated, that the response has an address and that address
  741. // really belongs to the configured pool and that lease is actually renewed.
  742. //
  743. // expected:
  744. // - returned REPLY message has copy of client-id
  745. // - returned REPLY message has server-id
  746. // - returned REPLY message has IA that includes IAADDR
  747. // - lease is actually renewed in LeaseMgr
  748. TEST_F(Dhcpv4SrvTest, RenewBasic) {
  749. IfaceMgrTestConfig test_config(true);
  750. IfaceMgr::instance().openSockets4();
  751. boost::scoped_ptr<NakedDhcpv4Srv> srv;
  752. ASSERT_NO_THROW(srv.reset(new NakedDhcpv4Srv(0)));
  753. const IOAddress addr("192.0.2.106");
  754. const uint32_t temp_t1 = 50;
  755. const uint32_t temp_t2 = 75;
  756. const uint32_t temp_valid = 100;
  757. const time_t temp_timestamp = time(NULL) - 10;
  758. // Generate client-id also sets client_id_ member
  759. OptionPtr clientid = generateClientId();
  760. // Check that the address we are about to use is indeed in pool
  761. ASSERT_TRUE(subnet_->inPool(Lease::TYPE_V4, addr));
  762. // let's create a lease and put it in the LeaseMgr
  763. uint8_t hwaddr2_data[] = { 0, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe};
  764. HWAddrPtr hwaddr2(new HWAddr(hwaddr2_data, sizeof(hwaddr2_data), HTYPE_ETHER));
  765. Lease4Ptr used(new Lease4(IOAddress("192.0.2.106"), hwaddr2,
  766. &client_id_->getDuid()[0], client_id_->getDuid().size(),
  767. temp_valid, temp_t1, temp_t2, temp_timestamp,
  768. subnet_->getID()));
  769. ASSERT_TRUE(LeaseMgrFactory::instance().addLease(used));
  770. // Check that the lease is really in the database
  771. Lease4Ptr l = LeaseMgrFactory::instance().getLease4(addr);
  772. ASSERT_TRUE(l);
  773. // Check that T1, T2, preferred, valid and cltt really set.
  774. // Constructed lease looks as if it was assigned 10 seconds ago
  775. // EXPECT_EQ(l->t1_, temp_t1);
  776. // EXPECT_EQ(l->t2_, temp_t2);
  777. EXPECT_EQ(l->valid_lft_, temp_valid);
  778. EXPECT_EQ(l->cltt_, temp_timestamp);
  779. // Let's create a RENEW
  780. Pkt4Ptr req = Pkt4Ptr(new Pkt4(DHCPREQUEST, 1234));
  781. req->setRemoteAddr(IOAddress(addr));
  782. req->setYiaddr(addr);
  783. req->setCiaddr(addr); // client's address
  784. req->setIface("eth0");
  785. req->setHWAddr(hwaddr2);
  786. req->addOption(clientid);
  787. req->addOption(srv->getServerID());
  788. // Pass it to the server and hope for a REPLY
  789. Pkt4Ptr ack = srv->processRequest(req);
  790. // Check if we get response at all
  791. checkResponse(ack, DHCPACK, 1234);
  792. EXPECT_EQ(addr, ack->getYiaddr());
  793. // Check that address was returned from proper range, that its lease
  794. // lifetime is correct, that T1 and T2 are returned properly
  795. checkAddressParams(ack, subnet_, true, true);
  796. // Check identifiers
  797. checkServerId(ack, srv->getServerID());
  798. checkClientId(ack, clientid);
  799. // Check that the lease is really in the database
  800. l = checkLease(ack, clientid, req->getHWAddr(), addr);
  801. ASSERT_TRUE(l);
  802. // Check that T1, T2, preferred, valid and cltt were really updated
  803. EXPECT_EQ(l->t1_, subnet_->getT1());
  804. EXPECT_EQ(l->t2_, subnet_->getT2());
  805. EXPECT_EQ(l->valid_lft_, subnet_->getValid());
  806. // Checking for CLTT is a bit tricky if we want to avoid off by 1 errors
  807. int32_t cltt = static_cast<int32_t>(l->cltt_);
  808. int32_t expected = static_cast<int32_t>(time(NULL));
  809. // Equality or difference by 1 between cltt and expected is ok.
  810. EXPECT_GE(1, abs(cltt - expected));
  811. EXPECT_TRUE(LeaseMgrFactory::instance().deleteLease(addr));
  812. }
  813. // This test verifies that the logic which matches server identifier in the
  814. // received message with server identifiers used by a server works correctly:
  815. // - a message with no server identifier is accepted,
  816. // - a message with a server identifier which matches one of the server
  817. // identifiers used by a server is accepted,
  818. // - a message with a server identifier which doesn't match any server
  819. // identifier used by a server, is not accepted.
  820. TEST_F(Dhcpv4SrvTest, acceptServerId) {
  821. IfaceMgrTestConfig test_config(true);
  822. IfaceMgr::instance().openSockets4();
  823. NakedDhcpv4Srv srv(0);
  824. Pkt4Ptr pkt(new Pkt4(DHCPREQUEST, 1234));
  825. // If no server identifier option is present, the message is always
  826. // accepted.
  827. EXPECT_TRUE(srv.acceptServerId(pkt));
  828. // Create definition of the server identifier option.
  829. OptionDefinition def("server-identifier", DHO_DHCP_SERVER_IDENTIFIER,
  830. "ipv4-address", false);
  831. // Add a server identifier option which doesn't match server ids being
  832. // used by the server. The accepted server ids are the IPv4 addresses
  833. // configured on the interfaces. The 10.1.2.3 is not configured on
  834. // any interfaces.
  835. OptionCustomPtr other_serverid(new OptionCustom(def, Option::V6));
  836. other_serverid->writeAddress(IOAddress("10.1.2.3"));
  837. pkt->addOption(other_serverid);
  838. EXPECT_FALSE(srv.acceptServerId(pkt));
  839. // Remove the server identifier.
  840. ASSERT_NO_THROW(pkt->delOption(DHO_DHCP_SERVER_IDENTIFIER));
  841. // Add a server id being an IPv4 address configured on eth0 interface.
  842. // A DHCPv4 message holding this server identifier should be accepted.
  843. OptionCustomPtr eth0_serverid(new OptionCustom(def, Option::V6));
  844. eth0_serverid->writeAddress(IOAddress("192.0.2.3"));
  845. ASSERT_NO_THROW(pkt->addOption(eth0_serverid));
  846. EXPECT_TRUE(srv.acceptServerId(pkt));
  847. // Remove the server identifier.
  848. ASSERT_NO_THROW(pkt->delOption(DHO_DHCP_SERVER_IDENTIFIER));
  849. // Add a server id being an IPv4 address configured on eth1 interface.
  850. // A DHCPv4 message holding this server identifier should be accepted.
  851. OptionCustomPtr eth1_serverid(new OptionCustom(def, Option::V6));
  852. eth1_serverid->writeAddress(IOAddress("10.0.0.1"));
  853. ASSERT_NO_THROW(pkt->addOption(eth1_serverid));
  854. EXPECT_TRUE(srv.acceptServerId(pkt));
  855. }
  856. // @todo: Implement tests for rejecting renewals
  857. // This test verifies if the sanityCheck() really checks options presence.
  858. TEST_F(Dhcpv4SrvTest, sanityCheck) {
  859. boost::scoped_ptr<NakedDhcpv4Srv> srv;
  860. ASSERT_NO_THROW(srv.reset(new NakedDhcpv4Srv(0)));
  861. Pkt4Ptr pkt = Pkt4Ptr(new Pkt4(DHCPDISCOVER, 1234));
  862. pkt->setHWAddr(generateHWAddr(6));
  863. // Server-id is optional for information-request, so
  864. EXPECT_NO_THROW(NakedDhcpv4Srv::sanityCheck(pkt, Dhcpv4Srv::OPTIONAL));
  865. // Empty packet, no server-id
  866. EXPECT_THROW(NakedDhcpv4Srv::sanityCheck(pkt, Dhcpv4Srv::MANDATORY),
  867. RFCViolation);
  868. pkt->addOption(srv->getServerID());
  869. // Server-id is mandatory and present = no exception
  870. EXPECT_NO_THROW(NakedDhcpv4Srv::sanityCheck(pkt, Dhcpv4Srv::MANDATORY));
  871. // Server-id is forbidden, but present => exception
  872. EXPECT_THROW(NakedDhcpv4Srv::sanityCheck(pkt, Dhcpv4Srv::FORBIDDEN),
  873. RFCViolation);
  874. // There's no client-id and no HWADDR. Server needs something to
  875. // identify the client
  876. pkt->setHWAddr(generateHWAddr(0));
  877. EXPECT_THROW(NakedDhcpv4Srv::sanityCheck(pkt, Dhcpv4Srv::MANDATORY),
  878. RFCViolation);
  879. }
  880. // Checks if received relay agent info option is echoed back to the client
  881. TEST_F(Dhcpv4SrvTest, relayAgentInfoEcho) {
  882. IfaceMgrTestConfig test_config(true);
  883. IfaceMgr::instance().openSockets4();
  884. NakedDhcpv4Srv srv(0);
  885. // Use of the captured DHCPDISCOVER packet requires that
  886. // subnet 10.254.226.0/24 is in use, because this packet
  887. // contains the giaddr which belongs to this subnet and
  888. // this giaddr is used to select the subnet
  889. configure(CONFIGS[0]);
  890. // Let's create a relayed DISCOVER. This particular relayed DISCOVER has
  891. // added option 82 (relay agent info) with 3 suboptions. The server
  892. // is supposed to echo it back in its response.
  893. Pkt4Ptr dis;
  894. ASSERT_NO_THROW(dis = PktCaptures::captureRelayedDiscover());
  895. // Simulate that we have received that traffic
  896. srv.fakeReceive(dis);
  897. // Server will now process to run its normal loop, but instead of calling
  898. // IfaceMgr::receive4(), it will read all packets from the list set by
  899. // fakeReceive()
  900. // In particular, it should call registered buffer4_receive callback.
  901. srv.run();
  902. // Check that the server did send a response
  903. ASSERT_EQ(1, srv.fake_sent_.size());
  904. // Make sure that we received a response
  905. Pkt4Ptr offer = srv.fake_sent_.front();
  906. ASSERT_TRUE(offer);
  907. // Get Relay Agent Info from query...
  908. OptionPtr rai_query = dis->getOption(DHO_DHCP_AGENT_OPTIONS);
  909. ASSERT_TRUE(rai_query);
  910. // Get Relay Agent Info from response...
  911. OptionPtr rai_response = offer->getOption(DHO_DHCP_AGENT_OPTIONS);
  912. ASSERT_TRUE(rai_response);
  913. EXPECT_TRUE(rai_response->equals(rai_query));
  914. }
  915. /// @todo move vendor options tests to a separate file.
  916. /// @todo Add more extensive vendor options tests, including multiple
  917. /// vendor options
  918. // Checks if vendor options are parsed correctly and requested vendor options
  919. // are echoed back.
  920. TEST_F(Dhcpv4SrvTest, vendorOptionsDocsis) {
  921. IfaceMgrTestConfig test_config(true);
  922. IfaceMgr::instance().openSockets4();
  923. NakedDhcpv4Srv srv(0);
  924. string config = "{ \"interfaces-config\": {"
  925. " \"interfaces\": [ \"*\" ]"
  926. "},"
  927. "\"rebind-timer\": 2000, "
  928. "\"renew-timer\": 1000, "
  929. " \"option-data\": [ {"
  930. " \"name\": \"tftp-servers\","
  931. " \"space\": \"vendor-4491\","
  932. " \"code\": 2,"
  933. " \"data\": \"10.253.175.16\","
  934. " \"csv-format\": true"
  935. " }],"
  936. "\"subnet4\": [ { "
  937. " \"pools\": [ { \"pool\": \"10.254.226.0/25\" } ],"
  938. " \"subnet\": \"10.254.226.0/24\", "
  939. " \"rebind-timer\": 2000, "
  940. " \"renew-timer\": 1000, "
  941. " \"valid-lifetime\": 4000,"
  942. " \"interface\": \"eth0\" "
  943. " } ],"
  944. "\"valid-lifetime\": 4000 }";
  945. ConstElementPtr json;
  946. ASSERT_NO_THROW(json = parseDHCP4(config));
  947. ConstElementPtr status;
  948. // Configure the server and make sure the config is accepted
  949. EXPECT_NO_THROW(status = configureDhcp4Server(srv, json));
  950. ASSERT_TRUE(status);
  951. comment_ = config::parseAnswer(rcode_, status);
  952. ASSERT_EQ(0, rcode_);
  953. CfgMgr::instance().commit();
  954. // Let's create a relayed DISCOVER. This particular relayed DISCOVER has
  955. // added option 82 (relay agent info) with 3 suboptions. The server
  956. // is supposed to echo it back in its response.
  957. Pkt4Ptr dis;
  958. ASSERT_NO_THROW(dis = PktCaptures::captureRelayedDiscover());
  959. // Simulate that we have received that traffic
  960. srv.fakeReceive(dis);
  961. // Server will now process to run its normal loop, but instead of calling
  962. // IfaceMgr::receive4(), it will read all packets from the list set by
  963. // fakeReceive()
  964. // In particular, it should call registered buffer4_receive callback.
  965. srv.run();
  966. // Check that the server did send a response
  967. ASSERT_EQ(1, srv.fake_sent_.size());
  968. // Make sure that we received a response
  969. Pkt4Ptr offer = srv.fake_sent_.front();
  970. ASSERT_TRUE(offer);
  971. // Get Relay Agent Info from query...
  972. OptionPtr vendor_opt_response = offer->getOption(DHO_VIVSO_SUBOPTIONS);
  973. ASSERT_TRUE(vendor_opt_response);
  974. // Check if it's of a correct type
  975. boost::shared_ptr<OptionVendor> vendor_opt =
  976. boost::dynamic_pointer_cast<OptionVendor>(vendor_opt_response);
  977. ASSERT_TRUE(vendor_opt);
  978. // Get Relay Agent Info from response...
  979. OptionPtr tftp_servers_generic = vendor_opt->getOption(DOCSIS3_V4_TFTP_SERVERS);
  980. ASSERT_TRUE(tftp_servers_generic);
  981. Option4AddrLstPtr tftp_servers =
  982. boost::dynamic_pointer_cast<Option4AddrLst>(tftp_servers_generic);
  983. ASSERT_TRUE(tftp_servers);
  984. Option4AddrLst::AddressContainer addrs = tftp_servers->getAddresses();
  985. ASSERT_EQ(1, addrs.size());
  986. EXPECT_EQ("10.253.175.16", addrs[0].toText());
  987. }
  988. /// @todo Implement tests for subnetSelect See tests in dhcp6_srv_unittest.cc:
  989. /// selectSubnetAddr, selectSubnetIface, selectSubnetRelayLinkaddr,
  990. /// selectSubnetRelayInterfaceId. Note that the concept of interface-id is not
  991. /// present in the DHCPv4, so not everything is applicable directly.
  992. /// See ticket #3057
  993. // Checks whether the server uses default (0.0.0.0) siaddr value, unless
  994. // explicitly specified
  995. TEST_F(Dhcpv4SrvTest, siaddrDefault) {
  996. IfaceMgrTestConfig test_config(true);
  997. IfaceMgr::instance().openSockets4();
  998. boost::scoped_ptr<NakedDhcpv4Srv> srv;
  999. ASSERT_NO_THROW(srv.reset(new NakedDhcpv4Srv(0)));
  1000. IOAddress hint("192.0.2.107");
  1001. Pkt4Ptr dis = Pkt4Ptr(new Pkt4(DHCPDISCOVER, 1234));
  1002. dis->setRemoteAddr(IOAddress("192.0.2.1"));
  1003. OptionPtr clientid = generateClientId();
  1004. dis->addOption(clientid);
  1005. dis->setYiaddr(hint);
  1006. dis->setIface("eth1");
  1007. // Pass it to the server and get an offer
  1008. Pkt4Ptr offer = srv->processDiscover(dis);
  1009. ASSERT_TRUE(offer);
  1010. // Check if we get response at all
  1011. checkResponse(offer, DHCPOFFER, 1234);
  1012. // Verify that it is 0.0.0.0
  1013. EXPECT_EQ("0.0.0.0", offer->getSiaddr().toText());
  1014. }
  1015. // Checks whether the server uses specified siaddr value
  1016. TEST_F(Dhcpv4SrvTest, siaddr) {
  1017. IfaceMgrTestConfig test_config(true);
  1018. IfaceMgr::instance().openSockets4();
  1019. boost::scoped_ptr<NakedDhcpv4Srv> srv;
  1020. ASSERT_NO_THROW(srv.reset(new NakedDhcpv4Srv(0)));
  1021. subnet_->setSiaddr(IOAddress("192.0.2.123"));
  1022. Pkt4Ptr dis = Pkt4Ptr(new Pkt4(DHCPDISCOVER, 1234));
  1023. dis->setRemoteAddr(IOAddress("192.0.2.1"));
  1024. dis->setIface("eth1");
  1025. OptionPtr clientid = generateClientId();
  1026. dis->addOption(clientid);
  1027. // Pass it to the server and get an offer
  1028. Pkt4Ptr offer = srv->processDiscover(dis);
  1029. ASSERT_TRUE(offer);
  1030. // Check if we get response at all
  1031. checkResponse(offer, DHCPOFFER, 1234);
  1032. // Verify that its value is proper
  1033. EXPECT_EQ("192.0.2.123", offer->getSiaddr().toText());
  1034. }
  1035. // Checks if the next-server defined as global value is overridden by subnet
  1036. // specific value and returned in server messages. There's also similar test for
  1037. // checking parser only configuration, see Dhcp4ParserTest.nextServerOverride in
  1038. // config_parser_unittest.cc.
  1039. TEST_F(Dhcpv4SrvTest, nextServerOverride) {
  1040. IfaceMgrTestConfig test_config(true);
  1041. IfaceMgr::instance().openSockets4();
  1042. NakedDhcpv4Srv srv(0);
  1043. ConstElementPtr status;
  1044. string config = "{ \"interfaces-config\": {"
  1045. " \"interfaces\": [ \"*\" ]"
  1046. "},"
  1047. "\"rebind-timer\": 2000, "
  1048. "\"renew-timer\": 1000, "
  1049. "\"next-server\": \"192.0.0.1\", "
  1050. "\"subnet4\": [ { "
  1051. " \"pools\": [ { \"pool\": \"192.0.2.1 - 192.0.2.100\" } ],"
  1052. " \"next-server\": \"1.2.3.4\", "
  1053. " \"subnet\": \"192.0.2.0/24\" } ],"
  1054. "\"valid-lifetime\": 4000 }";
  1055. ConstElementPtr json;
  1056. ASSERT_NO_THROW(json = parseDHCP4(config, true));
  1057. EXPECT_NO_THROW(status = configureDhcp4Server(srv, json));
  1058. CfgMgr::instance().commit();
  1059. // check if returned status is OK
  1060. ASSERT_TRUE(status);
  1061. comment_ = config::parseAnswer(rcode_, status);
  1062. ASSERT_EQ(0, rcode_);
  1063. Pkt4Ptr dis = Pkt4Ptr(new Pkt4(DHCPDISCOVER, 1234));
  1064. dis->setRemoteAddr(IOAddress("192.0.2.1"));
  1065. dis->setIface("eth1");
  1066. OptionPtr clientid = generateClientId();
  1067. dis->addOption(clientid);
  1068. // Pass it to the server and get an offer
  1069. Pkt4Ptr offer = srv.processDiscover(dis);
  1070. ASSERT_TRUE(offer);
  1071. EXPECT_EQ(DHCPOFFER, offer->getType());
  1072. EXPECT_EQ("1.2.3.4", offer->getSiaddr().toText());
  1073. }
  1074. // Checks if the next-server defined as global value is used in responses
  1075. // when there is no specific value defined in subnet and returned to the client
  1076. // properly. There's also similar test for checking parser only configuration,
  1077. // see Dhcp4ParserTest.nextServerGlobal in config_parser_unittest.cc.
  1078. TEST_F(Dhcpv4SrvTest, nextServerGlobal) {
  1079. IfaceMgrTestConfig test_config(true);
  1080. IfaceMgr::instance().openSockets4();
  1081. NakedDhcpv4Srv srv(0);
  1082. ConstElementPtr status;
  1083. string config = "{ \"interfaces-config\": {"
  1084. " \"interfaces\": [ \"*\" ]"
  1085. "},"
  1086. "\"rebind-timer\": 2000, "
  1087. "\"renew-timer\": 1000, "
  1088. "\"next-server\": \"192.0.0.1\", "
  1089. "\"subnet4\": [ { "
  1090. " \"pools\": [ { \"pool\": \"192.0.2.1 - 192.0.2.100\" } ],"
  1091. " \"subnet\": \"192.0.2.0/24\" } ],"
  1092. "\"valid-lifetime\": 4000 }";
  1093. ConstElementPtr json;
  1094. ASSERT_NO_THROW(json = parseDHCP4(config, true));
  1095. EXPECT_NO_THROW(status = configureDhcp4Server(srv, json));
  1096. CfgMgr::instance().commit();
  1097. // check if returned status is OK
  1098. ASSERT_TRUE(status);
  1099. comment_ = config::parseAnswer(rcode_, status);
  1100. ASSERT_EQ(0, rcode_);
  1101. Pkt4Ptr dis = Pkt4Ptr(new Pkt4(DHCPDISCOVER, 1234));
  1102. dis->setRemoteAddr(IOAddress("192.0.2.1"));
  1103. dis->setIface("eth1");
  1104. OptionPtr clientid = generateClientId();
  1105. dis->addOption(clientid);
  1106. // Pass it to the server and get an offer
  1107. Pkt4Ptr offer = srv.processDiscover(dis);
  1108. ASSERT_TRUE(offer);
  1109. EXPECT_EQ(DHCPOFFER, offer->getType());
  1110. EXPECT_EQ("192.0.0.1", offer->getSiaddr().toText());
  1111. }
  1112. // Checks if server is able to handle a relayed traffic from DOCSIS3.0 modems
  1113. TEST_F(Dhcpv4SrvTest, docsisVendorOptionsParse) {
  1114. // Let's get a traffic capture from DOCSIS3.0 modem
  1115. Pkt4Ptr dis = PktCaptures::captureRelayedDiscover();
  1116. ASSERT_NO_THROW(dis->unpack());
  1117. // Check if the packet contain
  1118. OptionPtr opt = dis->getOption(DHO_VIVSO_SUBOPTIONS);
  1119. ASSERT_TRUE(opt);
  1120. boost::shared_ptr<OptionVendor> vendor = boost::dynamic_pointer_cast<OptionVendor>(opt);
  1121. ASSERT_TRUE(vendor);
  1122. // This particular capture that we have included options 1 and 5
  1123. EXPECT_TRUE(vendor->getOption(1));
  1124. EXPECT_TRUE(vendor->getOption(5));
  1125. // It did not include options any other options
  1126. EXPECT_FALSE(vendor->getOption(2));
  1127. EXPECT_FALSE(vendor->getOption(3));
  1128. EXPECT_FALSE(vendor->getOption(17));
  1129. }
  1130. // Checks if server is able to parse incoming docsis option and extract suboption 1 (docsis ORO)
  1131. TEST_F(Dhcpv4SrvTest, docsisVendorORO) {
  1132. // Let's get a traffic capture from DOCSIS3.0 modem
  1133. Pkt4Ptr dis = PktCaptures::captureRelayedDiscover();
  1134. EXPECT_NO_THROW(dis->unpack());
  1135. // Check if the packet contains vendor specific information option
  1136. OptionPtr opt = dis->getOption(DHO_VIVSO_SUBOPTIONS);
  1137. ASSERT_TRUE(opt);
  1138. boost::shared_ptr<OptionVendor> vendor = boost::dynamic_pointer_cast<OptionVendor>(opt);
  1139. ASSERT_TRUE(vendor);
  1140. opt = vendor->getOption(DOCSIS3_V4_ORO);
  1141. ASSERT_TRUE(opt);
  1142. OptionUint8ArrayPtr oro = boost::dynamic_pointer_cast<OptionUint8Array>(opt);
  1143. EXPECT_TRUE(oro);
  1144. }
  1145. // This test checks if Option Request Option (ORO) in docsis (vendor-id=4491)
  1146. // vendor options is parsed correctly and the requested options are actually assigned.
  1147. TEST_F(Dhcpv4SrvTest, vendorOptionsORO) {
  1148. IfaceMgrTestConfig test_config(true);
  1149. IfaceMgr::instance().openSockets4();
  1150. NakedDhcpv4Srv srv(0);
  1151. ConstElementPtr x;
  1152. string config = "{ \"interfaces-config\": {"
  1153. " \"interfaces\": [ \"*\" ]"
  1154. "},"
  1155. "\"rebind-timer\": 2000, "
  1156. "\"renew-timer\": 1000, "
  1157. " \"option-data\": [ {"
  1158. " \"name\": \"tftp-servers\","
  1159. " \"space\": \"vendor-4491\","
  1160. " \"code\": 2,"
  1161. " \"data\": \"192.0.2.1, 192.0.2.2\","
  1162. " \"csv-format\": true"
  1163. " }],"
  1164. "\"subnet4\": [ { "
  1165. " \"pools\": [ { \"pool\": \"192.0.2.0/25\" } ],"
  1166. " \"subnet\": \"192.0.2.0/24\", "
  1167. " \"rebind-timer\": 2000, "
  1168. " \"renew-timer\": 1000, "
  1169. " \"valid-lifetime\": 4000,"
  1170. " \"interface\": \"eth0\" "
  1171. " } ],"
  1172. "\"valid-lifetime\": 4000 }";
  1173. ConstElementPtr json;
  1174. ASSERT_NO_THROW(json = parseDHCP4(config));
  1175. EXPECT_NO_THROW(x = configureDhcp4Server(srv, json));
  1176. ASSERT_TRUE(x);
  1177. comment_ = isc::config::parseAnswer(rcode_, x);
  1178. ASSERT_EQ(0, rcode_);
  1179. CfgMgr::instance().commit();
  1180. boost::shared_ptr<Pkt4> dis(new Pkt4(DHCPDISCOVER, 1234));
  1181. // Set the giaddr and hops to non-zero address as if it was relayed.
  1182. dis->setGiaddr(IOAddress("192.0.2.1"));
  1183. dis->setHops(1);
  1184. OptionPtr clientid = generateClientId();
  1185. dis->addOption(clientid);
  1186. // Set interface. It is required by the server to generate server id.
  1187. dis->setIface("eth0");
  1188. // Pass it to the server and get an advertise
  1189. Pkt4Ptr offer = srv.processDiscover(dis);
  1190. // check if we get response at all
  1191. ASSERT_TRUE(offer);
  1192. // We did not include any vendor opts in DISCOVER, so there should be none
  1193. // in OFFER.
  1194. ASSERT_FALSE(offer->getOption(DHO_VIVSO_SUBOPTIONS));
  1195. // Let's add a vendor-option (vendor-id=4491) with a single sub-option.
  1196. // That suboption has code 1 and is a docsis ORO option.
  1197. boost::shared_ptr<OptionUint8Array> vendor_oro(new OptionUint8Array(Option::V4,
  1198. DOCSIS3_V4_ORO));
  1199. vendor_oro->addValue(DOCSIS3_V4_TFTP_SERVERS); // Request option 33
  1200. OptionPtr vendor(new OptionVendor(Option::V4, 4491));
  1201. vendor->addOption(vendor_oro);
  1202. dis->addOption(vendor);
  1203. // Need to process SOLICIT again after requesting new option.
  1204. offer = srv.processDiscover(dis);
  1205. ASSERT_TRUE(offer);
  1206. // Check if there is a vendor option response
  1207. OptionPtr tmp = offer->getOption(DHO_VIVSO_SUBOPTIONS);
  1208. ASSERT_TRUE(tmp);
  1209. // The response should be OptionVendor object
  1210. boost::shared_ptr<OptionVendor> vendor_resp =
  1211. boost::dynamic_pointer_cast<OptionVendor>(tmp);
  1212. ASSERT_TRUE(vendor_resp);
  1213. OptionPtr docsis2 = vendor_resp->getOption(DOCSIS3_V4_TFTP_SERVERS);
  1214. ASSERT_TRUE(docsis2);
  1215. Option4AddrLstPtr tftp_srvs = boost::dynamic_pointer_cast<Option4AddrLst>(docsis2);
  1216. ASSERT_TRUE(tftp_srvs);
  1217. Option4AddrLst::AddressContainer addrs = tftp_srvs->getAddresses();
  1218. ASSERT_EQ(2, addrs.size());
  1219. EXPECT_EQ("192.0.2.1", addrs[0].toText());
  1220. EXPECT_EQ("192.0.2.2", addrs[1].toText());
  1221. }
  1222. // This test checks if Option Request Option (ORO) in docsis (vendor-id=4491)
  1223. // vendor options is parsed correctly and persistent options are actually assigned.
  1224. TEST_F(Dhcpv4SrvTest, vendorPersistentOptions) {
  1225. IfaceMgrTestConfig test_config(true);
  1226. IfaceMgr::instance().openSockets4();
  1227. NakedDhcpv4Srv srv(0);
  1228. ConstElementPtr x;
  1229. string config = "{ \"interfaces-config\": {"
  1230. " \"interfaces\": [ \"*\" ]"
  1231. "},"
  1232. "\"rebind-timer\": 2000, "
  1233. "\"renew-timer\": 1000, "
  1234. " \"option-data\": [ {"
  1235. " \"name\": \"tftp-servers\","
  1236. " \"space\": \"vendor-4491\","
  1237. " \"code\": 2,"
  1238. " \"data\": \"192.0.2.1, 192.0.2.2\","
  1239. " \"csv-format\": true,"
  1240. " \"always-send\": true"
  1241. " }],"
  1242. "\"subnet4\": [ { "
  1243. " \"pools\": [ { \"pool\": \"192.0.2.0/25\" } ],"
  1244. " \"subnet\": \"192.0.2.0/24\", "
  1245. " \"rebind-timer\": 2000, "
  1246. " \"renew-timer\": 1000, "
  1247. " \"valid-lifetime\": 4000,"
  1248. " \"interface\": \"eth0\" "
  1249. " } ],"
  1250. "\"valid-lifetime\": 4000 }";
  1251. ConstElementPtr json;
  1252. ASSERT_NO_THROW(json = parseDHCP4(config));
  1253. EXPECT_NO_THROW(x = configureDhcp4Server(srv, json));
  1254. ASSERT_TRUE(x);
  1255. comment_ = isc::config::parseAnswer(rcode_, x);
  1256. ASSERT_EQ(0, rcode_);
  1257. CfgMgr::instance().commit();
  1258. boost::shared_ptr<Pkt4> dis(new Pkt4(DHCPDISCOVER, 1234));
  1259. // Set the giaddr and hops to non-zero address as if it was relayed.
  1260. dis->setGiaddr(IOAddress("192.0.2.1"));
  1261. dis->setHops(1);
  1262. OptionPtr clientid = generateClientId();
  1263. dis->addOption(clientid);
  1264. // Set interface. It is required by the server to generate server id.
  1265. dis->setIface("eth0");
  1266. // Let's add a vendor-option (vendor-id=4491).
  1267. OptionPtr vendor(new OptionVendor(Option::V4, 4491));
  1268. dis->addOption(vendor);
  1269. // Pass it to the server and get an advertise
  1270. Pkt4Ptr offer = srv.processDiscover(dis);
  1271. // check if we get response at all
  1272. ASSERT_TRUE(offer);
  1273. // Check if there is a vendor option response
  1274. OptionPtr tmp = offer->getOption(DHO_VIVSO_SUBOPTIONS);
  1275. ASSERT_TRUE(tmp);
  1276. // The response should be OptionVendor object
  1277. boost::shared_ptr<OptionVendor> vendor_resp =
  1278. boost::dynamic_pointer_cast<OptionVendor>(tmp);
  1279. ASSERT_TRUE(vendor_resp);
  1280. OptionPtr docsis2 = vendor_resp->getOption(DOCSIS3_V4_TFTP_SERVERS);
  1281. ASSERT_TRUE(docsis2);
  1282. Option4AddrLstPtr tftp_srvs = boost::dynamic_pointer_cast<Option4AddrLst>(docsis2);
  1283. ASSERT_TRUE(tftp_srvs);
  1284. Option4AddrLst::AddressContainer addrs = tftp_srvs->getAddresses();
  1285. ASSERT_EQ(2, addrs.size());
  1286. EXPECT_EQ("192.0.2.1", addrs[0].toText());
  1287. EXPECT_EQ("192.0.2.2", addrs[1].toText());
  1288. }
  1289. // Test checks whether it is possible to use option definitions defined in
  1290. // src/lib/dhcp/docsis3_option_defs.h.
  1291. TEST_F(Dhcpv4SrvTest, vendorOptionsDocsisDefinitions) {
  1292. ConstElementPtr x;
  1293. string config_prefix = "{ \"interfaces-config\": {"
  1294. " \"interfaces\": [ ]"
  1295. "},"
  1296. "\"rebind-timer\": 2000, "
  1297. "\"renew-timer\": 1000, "
  1298. " \"option-data\": [ {"
  1299. " \"name\": \"tftp-servers\","
  1300. " \"space\": \"vendor-4491\","
  1301. " \"code\": ";
  1302. string config_postfix = ","
  1303. " \"data\": \"192.0.2.1\","
  1304. " \"csv-format\": true"
  1305. " }],"
  1306. "\"subnet4\": [ { "
  1307. " \"pools\": [ { \"pool\": \"192.0.2.1 - 192.0.2.50\" } ],"
  1308. " \"subnet\": \"192.0.2.0/24\", "
  1309. " \"renew-timer\": 1000, "
  1310. " \"rebind-timer\": 1000, "
  1311. " \"valid-lifetime\": 4000,"
  1312. " \"interface\": \"\""
  1313. " } ],"
  1314. "\"valid-lifetime\": 4000 }";
  1315. // There is docsis3 (vendor-id=4491) vendor option 2, which is a
  1316. // tftp-server. Its format is list of IPv4 addresses.
  1317. string config_valid = config_prefix + "2" + config_postfix;
  1318. // There is no option 99 defined in vendor-id=4491. As there is no
  1319. // definition, the config should fail.
  1320. string config_bogus = config_prefix + "99" + config_postfix;
  1321. ConstElementPtr json_bogus;
  1322. ASSERT_NO_THROW(json_bogus = parseDHCP4(config_bogus));
  1323. ConstElementPtr json_valid;
  1324. ASSERT_NO_THROW(json_valid = parseDHCP4(config_valid));
  1325. NakedDhcpv4Srv srv(0);
  1326. // This should fail (missing option definition)
  1327. EXPECT_NO_THROW(x = configureDhcp4Server(srv, json_bogus));
  1328. ASSERT_TRUE(x);
  1329. comment_ = isc::config::parseAnswer(rcode_, x);
  1330. ASSERT_EQ(1, rcode_);
  1331. // This should work (option definition present)
  1332. EXPECT_NO_THROW(x = configureDhcp4Server(srv, json_valid));
  1333. ASSERT_TRUE(x);
  1334. comment_ = isc::config::parseAnswer(rcode_, x);
  1335. ASSERT_EQ(0, rcode_);
  1336. }
  1337. /// Checks if DOCSIS client packets are classified properly
  1338. ///
  1339. /// @todo: With the change in #4626 the vendorClassSpecificProcessing
  1340. /// code was removed and replaced with generic classification. One day
  1341. /// we should rewrite this test to use classes. It would check that the
  1342. /// classification system can be used for docsis packets.
  1343. TEST_F(Dhcpv4SrvTest, DISABLED_docsisClientClassification) {
  1344. NakedDhcpv4Srv srv(0);
  1345. // Let's create a relayed DISCOVER. This particular relayed DISCOVER has
  1346. // vendor-class set to docsis3.0
  1347. Pkt4Ptr dis1;
  1348. ASSERT_NO_THROW(dis1 = PktCaptures::captureRelayedDiscover());
  1349. ASSERT_NO_THROW(dis1->unpack());
  1350. srv.classifyPacket(dis1);
  1351. EXPECT_TRUE(dis1->inClass(srv.VENDOR_CLASS_PREFIX + "docsis3.0"));
  1352. EXPECT_FALSE(dis1->inClass(srv.VENDOR_CLASS_PREFIX + "eRouter1.0"));
  1353. // Let's create a relayed DISCOVER. This particular relayed DISCOVER has
  1354. // vendor-class set to eRouter1.0
  1355. Pkt4Ptr dis2;
  1356. ASSERT_NO_THROW(dis2 = PktCaptures::captureRelayedDiscover2());
  1357. ASSERT_NO_THROW(dis2->unpack());
  1358. srv.classifyPacket(dis2);
  1359. EXPECT_TRUE(dis2->inClass(srv.VENDOR_CLASS_PREFIX + "eRouter1.0"));
  1360. EXPECT_FALSE(dis2->inClass(srv.VENDOR_CLASS_PREFIX + "docsis3.0"));
  1361. }
  1362. // Checks if client packets are classified properly using match expressions.
  1363. TEST_F(Dhcpv4SrvTest, matchClassification) {
  1364. IfaceMgrTestConfig test_config(true);
  1365. IfaceMgr::instance().openSockets4();
  1366. NakedDhcpv4Srv srv(0);
  1367. // The router class matches incoming packets with foo in a host-name
  1368. // option (code 12) and sets an ip-forwarding option in the response.
  1369. string config = "{ \"interfaces-config\": {"
  1370. " \"interfaces\": [ \"*\" ] }, "
  1371. "\"rebind-timer\": 2000, "
  1372. "\"renew-timer\": 1000, "
  1373. "\"valid-lifetime\": 4000, "
  1374. "\"subnet4\": [ "
  1375. "{ \"pools\": [ { \"pool\": \"192.0.2.1 - 192.0.2.100\" } ], "
  1376. " \"subnet\": \"192.0.2.0/24\" } ], "
  1377. "\"client-classes\": [ "
  1378. "{ \"name\": \"router\", "
  1379. " \"option-data\": ["
  1380. " { \"name\": \"ip-forwarding\", "
  1381. " \"data\": \"true\" } ], "
  1382. " \"test\": \"option[12].text == 'foo'\" } ] }";
  1383. ConstElementPtr json;
  1384. ASSERT_NO_THROW(json = parseDHCP4(config));
  1385. ConstElementPtr status;
  1386. // Configure the server and make sure the config is accepted
  1387. EXPECT_NO_THROW(status = configureDhcp4Server(srv, json));
  1388. ASSERT_TRUE(status);
  1389. comment_ = config::parseAnswer(rcode_, status);
  1390. ASSERT_EQ(0, rcode_);
  1391. CfgMgr::instance().commit();
  1392. // Create packets with enough to select the subnet
  1393. OptionPtr clientid = generateClientId();
  1394. Pkt4Ptr query1(new Pkt4(DHCPDISCOVER, 1234));
  1395. query1->setRemoteAddr(IOAddress("192.0.2.1"));
  1396. query1->addOption(clientid);
  1397. query1->setIface("eth1");
  1398. Pkt4Ptr query2(new Pkt4(DHCPDISCOVER, 1234));
  1399. query2->setRemoteAddr(IOAddress("192.0.2.1"));
  1400. query2->addOption(clientid);
  1401. query2->setIface("eth1");
  1402. Pkt4Ptr query3(new Pkt4(DHCPDISCOVER, 1234));
  1403. query3->setRemoteAddr(IOAddress("192.0.2.1"));
  1404. query3->addOption(clientid);
  1405. query3->setIface("eth1");
  1406. // Create and add a PRL option to the first 2 queries
  1407. OptionUint8ArrayPtr prl(new OptionUint8Array(Option::V4,
  1408. DHO_DHCP_PARAMETER_REQUEST_LIST));
  1409. ASSERT_TRUE(prl);
  1410. prl->addValue(DHO_IP_FORWARDING);
  1411. query1->addOption(prl);
  1412. query2->addOption(prl);
  1413. // Create and add a host-name option to the first and last queries
  1414. OptionStringPtr hostname(new OptionString(Option::V4, 12, "foo"));
  1415. ASSERT_TRUE(hostname);
  1416. query1->addOption(hostname);
  1417. query3->addOption(hostname);
  1418. // Classify packets
  1419. srv.classifyPacket(query1);
  1420. srv.classifyPacket(query2);
  1421. srv.classifyPacket(query3);
  1422. // Packets with the exception of the second should be in the router class
  1423. EXPECT_TRUE(query1->inClass("router"));
  1424. EXPECT_FALSE(query2->inClass("router"));
  1425. EXPECT_TRUE(query3->inClass("router"));
  1426. // Process queries
  1427. Pkt4Ptr response1 = srv.processDiscover(query1);
  1428. Pkt4Ptr response2 = srv.processDiscover(query2);
  1429. Pkt4Ptr response3 = srv.processDiscover(query3);
  1430. // Classification processing should add an ip-forwarding option
  1431. OptionPtr opt1 = response1->getOption(DHO_IP_FORWARDING);
  1432. EXPECT_TRUE(opt1);
  1433. // But only for the first query: second was not classified
  1434. OptionPtr opt2 = response2->getOption(DHO_IP_FORWARDING);
  1435. EXPECT_FALSE(opt2);
  1436. // But only for the first query: third has no PRL
  1437. OptionPtr opt3 = response3->getOption(DHO_IP_FORWARDING);
  1438. EXPECT_FALSE(opt3);
  1439. }
  1440. // Checks if client packets are classified properly using match expressions
  1441. // using option names
  1442. TEST_F(Dhcpv4SrvTest, matchClassificationOptionName) {
  1443. NakedDhcpv4Srv srv(0);
  1444. // The router class matches incoming packets with foo in a host-name
  1445. string config = "{ \"interfaces-config\": {"
  1446. " \"interfaces\": [ \"*\" ] }, "
  1447. "\"rebind-timer\": 2000, "
  1448. "\"renew-timer\": 1000, "
  1449. "\"valid-lifetime\": 4000, "
  1450. "\"subnet4\": [ "
  1451. "{ \"pools\": [ { \"pool\": \"192.0.2.1 - 192.0.2.100\" } ], "
  1452. " \"subnet\": \"192.0.2.0/24\" } ], "
  1453. "\"client-classes\": [ "
  1454. "{ \"name\": \"router\", "
  1455. " \"test\": \"option[host-name].text == 'foo'\" } ] }";
  1456. ConstElementPtr json;
  1457. ASSERT_NO_THROW(json = parseDHCP4(config));
  1458. ConstElementPtr status;
  1459. // Configure the server and make sure the config is accepted
  1460. EXPECT_NO_THROW(status = configureDhcp4Server(srv, json));
  1461. ASSERT_TRUE(status);
  1462. comment_ = config::parseAnswer(rcode_, status);
  1463. ASSERT_EQ(0, rcode_);
  1464. CfgMgr::instance().commit();
  1465. // Create a packet with enough to select the subnet
  1466. Pkt4Ptr query(new Pkt4(DHCPDISCOVER, 1234));
  1467. query->setRemoteAddr(IOAddress("192.0.2.1"));
  1468. // Create and add a host-name option to the query
  1469. OptionStringPtr hostname(new OptionString(Option::V4, 12, "foo"));
  1470. ASSERT_TRUE(hostname);
  1471. query->addOption(hostname);
  1472. // Classify packets
  1473. srv.classifyPacket(query);
  1474. // The query should be in the router class
  1475. EXPECT_TRUE(query->inClass("router"));
  1476. }
  1477. // Checks if client packets are classified properly using match expressions
  1478. // using option names and definitions
  1479. TEST_F(Dhcpv4SrvTest, matchClassificationOptionDef) {
  1480. NakedDhcpv4Srv srv(0);
  1481. // The router class matches incoming packets with foo in a defined
  1482. // option
  1483. string config = "{ \"interfaces-config\": {"
  1484. " \"interfaces\": [ \"*\" ] }, "
  1485. "\"rebind-timer\": 2000, "
  1486. "\"renew-timer\": 1000, "
  1487. "\"valid-lifetime\": 4000, "
  1488. "\"subnet4\": [ "
  1489. "{ \"pools\": [ { \"pool\": \"192.0.2.1 - 192.0.2.100\" } ], "
  1490. " \"subnet\": \"192.0.2.0/24\" } ], "
  1491. "\"client-classes\": [ "
  1492. "{ \"name\": \"router\", "
  1493. " \"test\": \"option[my-host-name].text == 'foo'\" } ], "
  1494. "\"option-def\": [ {"
  1495. " \"name\": \"my-host-name\", "
  1496. " \"code\": 250, "
  1497. " \"type\": \"string\" } ] }";
  1498. ConstElementPtr json;
  1499. ASSERT_NO_THROW(json = parseDHCP4(config));
  1500. ConstElementPtr status;
  1501. // Configure the server and make sure the config is accepted
  1502. EXPECT_NO_THROW(status = configureDhcp4Server(srv, json));
  1503. ASSERT_TRUE(status);
  1504. comment_ = config::parseAnswer(rcode_, status);
  1505. ASSERT_EQ(0, rcode_);
  1506. CfgMgr::instance().commit();
  1507. // Create a packet with enough to select the subnet
  1508. Pkt4Ptr query(new Pkt4(DHCPDISCOVER, 1234));
  1509. query->setRemoteAddr(IOAddress("192.0.2.1"));
  1510. // Create and add a my-host-name option to the query
  1511. OptionStringPtr hostname(new OptionString(Option::V4, 250, "foo"));
  1512. ASSERT_TRUE(hostname);
  1513. query->addOption(hostname);
  1514. // Classify packets
  1515. srv.classifyPacket(query);
  1516. // The query should be in the router class
  1517. EXPECT_TRUE(query->inClass("router"));
  1518. }
  1519. // Checks subnet options have the priority over class options
  1520. TEST_F(Dhcpv4SrvTest, subnetClassPriority) {
  1521. IfaceMgrTestConfig test_config(true);
  1522. IfaceMgr::instance().openSockets4();
  1523. NakedDhcpv4Srv srv(0);
  1524. // Subnet sets an ip-forwarding option in the response.
  1525. // The router class matches incoming packets with foo in a host-name
  1526. // option (code 12) and sets an ip-forwarding option in the response.
  1527. string config = "{ \"interfaces-config\": {"
  1528. " \"interfaces\": [ \"*\" ] }, "
  1529. "\"rebind-timer\": 2000, "
  1530. "\"renew-timer\": 1000, "
  1531. "\"valid-lifetime\": 4000, "
  1532. "\"subnet4\": [ "
  1533. "{ \"pools\": [ { \"pool\": \"192.0.2.1 - 192.0.2.100\" } ], "
  1534. " \"subnet\": \"192.0.2.0/24\", "
  1535. " \"option-data\": ["
  1536. " { \"name\": \"ip-forwarding\", "
  1537. " \"data\": \"false\" } ] } ], "
  1538. "\"client-classes\": [ "
  1539. "{ \"name\": \"router\","
  1540. " \"option-data\": ["
  1541. " { \"name\": \"ip-forwarding\", "
  1542. " \"data\": \"true\" } ], "
  1543. " \"test\": \"option[12].text == 'foo'\" } ] }";
  1544. ConstElementPtr json;
  1545. ASSERT_NO_THROW(json = parseDHCP4(config));
  1546. ConstElementPtr status;
  1547. // Configure the server and make sure the config is accepted
  1548. EXPECT_NO_THROW(status = configureDhcp4Server(srv, json));
  1549. ASSERT_TRUE(status);
  1550. comment_ = config::parseAnswer(rcode_, status);
  1551. ASSERT_EQ(0, rcode_);
  1552. CfgMgr::instance().commit();
  1553. // Create a packet with enough to select the subnet and go through
  1554. // the DISCOVER processing
  1555. Pkt4Ptr query(new Pkt4(DHCPDISCOVER, 1234));
  1556. query->setRemoteAddr(IOAddress("192.0.2.1"));
  1557. OptionPtr clientid = generateClientId();
  1558. query->addOption(clientid);
  1559. query->setIface("eth1");
  1560. // Create and add a PRL option to the query
  1561. OptionUint8ArrayPtr prl(new OptionUint8Array(Option::V4,
  1562. DHO_DHCP_PARAMETER_REQUEST_LIST));
  1563. ASSERT_TRUE(prl);
  1564. prl->addValue(DHO_IP_FORWARDING);
  1565. query->addOption(prl);
  1566. // Create and add a host-name option to the query
  1567. OptionStringPtr hostname(new OptionString(Option::V4, 12, "foo"));
  1568. ASSERT_TRUE(hostname);
  1569. query->addOption(hostname);
  1570. // Classify the packet
  1571. srv.classifyPacket(query);
  1572. // The packet should be in the router class
  1573. EXPECT_TRUE(query->inClass("router"));
  1574. // Process the query
  1575. Pkt4Ptr response = srv.processDiscover(query);
  1576. // Processing should add an ip-forwarding option
  1577. OptionPtr opt = response->getOption(DHO_IP_FORWARDING);
  1578. ASSERT_TRUE(opt);
  1579. ASSERT_GT(opt->len(), opt->getHeaderLen());
  1580. // Classification sets the value to true/1, subnet to false/0
  1581. // Here subnet has the priority
  1582. EXPECT_EQ(0, opt->getUint8());
  1583. }
  1584. // Checks subnet options have the priority over global options
  1585. TEST_F(Dhcpv4SrvTest, subnetGlobalPriority) {
  1586. IfaceMgrTestConfig test_config(true);
  1587. IfaceMgr::instance().openSockets4();
  1588. NakedDhcpv4Srv srv(0);
  1589. // Subnet and global set an ip-forwarding option in the response.
  1590. string config = "{ \"interfaces-config\": {"
  1591. " \"interfaces\": [ \"*\" ] }, "
  1592. "\"rebind-timer\": 2000, "
  1593. "\"renew-timer\": 1000, "
  1594. "\"valid-lifetime\": 4000, "
  1595. "\"subnet4\": [ "
  1596. "{ \"pools\": [ { \"pool\": \"192.0.2.1 - 192.0.2.100\" } ], "
  1597. " \"subnet\": \"192.0.2.0/24\", "
  1598. " \"option-data\": ["
  1599. " { \"name\": \"ip-forwarding\", "
  1600. " \"data\": \"false\" } ] } ], "
  1601. "\"option-data\": ["
  1602. " { \"name\": \"ip-forwarding\", "
  1603. " \"data\": \"true\" } ] }";
  1604. ConstElementPtr json;
  1605. ASSERT_NO_THROW(json = parseDHCP4(config));
  1606. ConstElementPtr status;
  1607. // Configure the server and make sure the config is accepted
  1608. EXPECT_NO_THROW(status = configureDhcp4Server(srv, json));
  1609. ASSERT_TRUE(status);
  1610. comment_ = config::parseAnswer(rcode_, status);
  1611. ASSERT_EQ(0, rcode_);
  1612. CfgMgr::instance().commit();
  1613. // Create a packet with enough to select the subnet and go through
  1614. // the DISCOVER processing
  1615. Pkt4Ptr query(new Pkt4(DHCPDISCOVER, 1234));
  1616. query->setRemoteAddr(IOAddress("192.0.2.1"));
  1617. OptionPtr clientid = generateClientId();
  1618. query->addOption(clientid);
  1619. query->setIface("eth1");
  1620. // Create and add a PRL option to the query
  1621. OptionUint8ArrayPtr prl(new OptionUint8Array(Option::V4,
  1622. DHO_DHCP_PARAMETER_REQUEST_LIST));
  1623. ASSERT_TRUE(prl);
  1624. prl->addValue(DHO_IP_FORWARDING);
  1625. query->addOption(prl);
  1626. // Create and add a host-name option to the query
  1627. OptionStringPtr hostname(new OptionString(Option::V4, 12, "foo"));
  1628. ASSERT_TRUE(hostname);
  1629. query->addOption(hostname);
  1630. // Process the query
  1631. Pkt4Ptr response = srv.processDiscover(query);
  1632. // Processing should add an ip-forwarding option
  1633. OptionPtr opt = response->getOption(DHO_IP_FORWARDING);
  1634. ASSERT_TRUE(opt);
  1635. ASSERT_GT(opt->len(), opt->getHeaderLen());
  1636. // Global sets the value to true/1, subnet to false/0
  1637. // Here subnet has the priority
  1638. EXPECT_EQ(0, opt->getUint8());
  1639. }
  1640. // Checks class options have the priority over global options
  1641. TEST_F(Dhcpv4SrvTest, classGlobalPriority) {
  1642. IfaceMgrTestConfig test_config(true);
  1643. IfaceMgr::instance().openSockets4();
  1644. NakedDhcpv4Srv srv(0);
  1645. // A global ip-forwarding option is set in the response.
  1646. // The router class matches incoming packets with foo in a host-name
  1647. // option (code 12) and sets an ip-forwarding option in the response.
  1648. string config = "{ \"interfaces-config\": {"
  1649. " \"interfaces\": [ \"*\" ] }, "
  1650. "\"rebind-timer\": 2000, "
  1651. "\"renew-timer\": 1000, "
  1652. "\"valid-lifetime\": 4000, "
  1653. "\"subnet4\": [ "
  1654. "{ \"pools\": [ { \"pool\": \"192.0.2.1 - 192.0.2.100\" } ], "
  1655. " \"subnet\": \"192.0.2.0/24\" } ], "
  1656. "\"option-data\": ["
  1657. " { \"name\": \"ip-forwarding\", "
  1658. " \"data\": \"false\" } ], "
  1659. "\"client-classes\": [ "
  1660. "{ \"name\": \"router\","
  1661. " \"option-data\": ["
  1662. " { \"name\": \"ip-forwarding\", "
  1663. " \"data\": \"true\" } ], "
  1664. " \"test\": \"option[12].text == 'foo'\" } ] }";
  1665. ConstElementPtr json;
  1666. ASSERT_NO_THROW(json = parseDHCP4(config));
  1667. ConstElementPtr status;
  1668. // Configure the server and make sure the config is accepted
  1669. EXPECT_NO_THROW(status = configureDhcp4Server(srv, json));
  1670. ASSERT_TRUE(status);
  1671. comment_ = config::parseAnswer(rcode_, status);
  1672. ASSERT_EQ(0, rcode_);
  1673. CfgMgr::instance().commit();
  1674. // Create a packet with enough to select the subnet and go through
  1675. // the DISCOVER processing
  1676. Pkt4Ptr query(new Pkt4(DHCPDISCOVER, 1234));
  1677. query->setRemoteAddr(IOAddress("192.0.2.1"));
  1678. OptionPtr clientid = generateClientId();
  1679. query->addOption(clientid);
  1680. query->setIface("eth1");
  1681. // Create and add a PRL option to the query
  1682. OptionUint8ArrayPtr prl(new OptionUint8Array(Option::V4,
  1683. DHO_DHCP_PARAMETER_REQUEST_LIST));
  1684. ASSERT_TRUE(prl);
  1685. prl->addValue(DHO_IP_FORWARDING);
  1686. query->addOption(prl);
  1687. // Create and add a host-name option to the query
  1688. OptionStringPtr hostname(new OptionString(Option::V4, 12, "foo"));
  1689. ASSERT_TRUE(hostname);
  1690. query->addOption(hostname);
  1691. // Classify the packet
  1692. srv.classifyPacket(query);
  1693. // The packet should be in the router class
  1694. EXPECT_TRUE(query->inClass("router"));
  1695. // Process the query
  1696. Pkt4Ptr response = srv.processDiscover(query);
  1697. // Processing should add an ip-forwarding option
  1698. OptionPtr opt = response->getOption(DHO_IP_FORWARDING);
  1699. ASSERT_TRUE(opt);
  1700. ASSERT_GT(opt->len(), opt->getHeaderLen());
  1701. // Classification sets the value to true/1, global to false/0
  1702. // Here class has the priority
  1703. EXPECT_NE(0, opt->getUint8());
  1704. }
  1705. // Checks class options have the priority over global persistent options
  1706. TEST_F(Dhcpv4SrvTest, classGlobalPersistency) {
  1707. IfaceMgrTestConfig test_config(true);
  1708. IfaceMgr::instance().openSockets4();
  1709. NakedDhcpv4Srv srv(0);
  1710. // A global ip-forwarding option is set in the response.
  1711. // The router class matches incoming packets with foo in a host-name
  1712. // option (code 12) and sets an ip-forwarding option in the response.
  1713. // Note the persistency flag follows a "OR" semantic so to set
  1714. // it to false (or to leave the default) has no effect.
  1715. string config = "{ \"interfaces-config\": {"
  1716. " \"interfaces\": [ \"*\" ] }, "
  1717. "\"rebind-timer\": 2000, "
  1718. "\"renew-timer\": 1000, "
  1719. "\"valid-lifetime\": 4000, "
  1720. "\"subnet4\": [ "
  1721. "{ \"pools\": [ { \"pool\": \"192.0.2.1 - 192.0.2.100\" } ], "
  1722. " \"subnet\": \"192.0.2.0/24\" } ], "
  1723. "\"option-data\": ["
  1724. " { \"name\": \"ip-forwarding\", "
  1725. " \"data\": \"false\", "
  1726. " \"always-send\": true } ], "
  1727. "\"client-classes\": [ "
  1728. "{ \"name\": \"router\","
  1729. " \"option-data\": ["
  1730. " { \"name\": \"ip-forwarding\", "
  1731. " \"data\": \"true\", "
  1732. " \"always-send\": false } ], "
  1733. " \"test\": \"option[12].text == 'foo'\" } ] }";
  1734. ConstElementPtr json;
  1735. ASSERT_NO_THROW(json = parseDHCP4(config));
  1736. ConstElementPtr status;
  1737. // Configure the server and make sure the config is accepted
  1738. EXPECT_NO_THROW(status = configureDhcp4Server(srv, json));
  1739. ASSERT_TRUE(status);
  1740. comment_ = config::parseAnswer(rcode_, status);
  1741. ASSERT_EQ(0, rcode_);
  1742. CfgMgr::instance().commit();
  1743. // Create a packet with enough to select the subnet and go through
  1744. // the DISCOVER processing
  1745. Pkt4Ptr query(new Pkt4(DHCPDISCOVER, 1234));
  1746. query->setRemoteAddr(IOAddress("192.0.2.1"));
  1747. OptionPtr clientid = generateClientId();
  1748. query->addOption(clientid);
  1749. query->setIface("eth1");
  1750. // Do not add a PRL
  1751. OptionPtr prl = query->getOption(DHO_DHCP_PARAMETER_REQUEST_LIST);
  1752. EXPECT_FALSE(prl);
  1753. // Create and add a host-name option to the query
  1754. OptionStringPtr hostname(new OptionString(Option::V4, 12, "foo"));
  1755. ASSERT_TRUE(hostname);
  1756. query->addOption(hostname);
  1757. // Classify the packet
  1758. srv.classifyPacket(query);
  1759. // The packet should be in the router class
  1760. EXPECT_TRUE(query->inClass("router"));
  1761. // Process the query
  1762. Pkt4Ptr response = srv.processDiscover(query);
  1763. // Processing should add an ip-forwarding option
  1764. OptionPtr opt = response->getOption(DHO_IP_FORWARDING);
  1765. ASSERT_TRUE(opt);
  1766. ASSERT_GT(opt->len(), opt->getHeaderLen());
  1767. // Classification sets the value to true/1, global to false/0
  1768. // Here class has the priority
  1769. EXPECT_NE(0, opt->getUint8());
  1770. }
  1771. // Checks if the client-class field is indeed used for subnet selection.
  1772. // Note that packet classification is already checked in Dhcpv4SrvTest
  1773. // .*Classification above.
  1774. TEST_F(Dhcpv4SrvTest, clientClassify) {
  1775. // This test configures 2 subnets. We actually only need the
  1776. // first one, but since there's still this ugly hack that picks
  1777. // the pool if there is only one, we must use more than one
  1778. // subnet. That ugly hack will be removed in #3242, currently
  1779. // under review.
  1780. // The second subnet does not play any role here. The client's
  1781. // IP address belongs to the first subnet, so only that first
  1782. // subnet is being tested.
  1783. string config = "{ \"interfaces-config\": {"
  1784. " \"interfaces\": [ \"*\" ]"
  1785. "},"
  1786. "\"rebind-timer\": 2000, "
  1787. "\"renew-timer\": 1000, "
  1788. "\"subnet4\": [ "
  1789. "{ \"pools\": [ { \"pool\": \"192.0.2.1 - 192.0.2.100\" } ],"
  1790. " \"client-class\": \"foo\", "
  1791. " \"subnet\": \"192.0.2.0/24\" }, "
  1792. "{ \"pools\": [ { \"pool\": \"192.0.3.1 - 192.0.3.100\" } ],"
  1793. " \"client-class\": \"xyzzy\", "
  1794. " \"subnet\": \"192.0.3.0/24\" } "
  1795. "],"
  1796. "\"valid-lifetime\": 4000 }";
  1797. ASSERT_NO_THROW(configure(config));
  1798. // Create a simple packet that we'll use for classification
  1799. Pkt4Ptr dis = Pkt4Ptr(new Pkt4(DHCPDISCOVER, 1234));
  1800. dis->setRemoteAddr(IOAddress("192.0.2.1"));
  1801. dis->setCiaddr(IOAddress("192.0.2.1"));
  1802. dis->setIface("eth0");
  1803. OptionPtr clientid = generateClientId();
  1804. dis->addOption(clientid);
  1805. // This discover does not belong to foo class, so it will not
  1806. // be serviced
  1807. EXPECT_FALSE(srv_.selectSubnet(dis));
  1808. // Let's add the packet to bar class and try again.
  1809. dis->addClass("bar");
  1810. // Still not supported, because it belongs to wrong class.
  1811. EXPECT_FALSE(srv_.selectSubnet(dis));
  1812. // Let's add it to matching class.
  1813. dis->addClass("foo");
  1814. // This time it should work
  1815. EXPECT_TRUE(srv_.selectSubnet(dis));
  1816. }
  1817. // Verifies last resort option 43 is backward compatible
  1818. TEST_F(Dhcpv4SrvTest, option43LastResort) {
  1819. IfaceMgrTestConfig test_config(true);
  1820. IfaceMgr::instance().openSockets4();
  1821. NakedDhcpv4Srv srv(0);
  1822. // If there is no definition for option 43 a last resort
  1823. // one is applied. This definition was used by Kea <= 1.2
  1824. // so should be backward compatible.
  1825. string config = "{ \"interfaces-config\": {"
  1826. " \"interfaces\": [ \"*\" ] }, "
  1827. "\"rebind-timer\": 2000, "
  1828. "\"renew-timer\": 1000, "
  1829. "\"valid-lifetime\": 4000, "
  1830. "\"subnet4\": [ "
  1831. "{ \"pools\": [ { \"pool\": \"192.0.2.1 - 192.0.2.100\" } ], "
  1832. " \"subnet\": \"192.0.2.0/24\" } ],"
  1833. "\"option-def\": [ "
  1834. "{ \"code\": 1, "
  1835. " \"name\": \"foo\", "
  1836. " \"space\": \"vendor-encapsulated-options-space\", "
  1837. " \"type\": \"uint32\" } ],"
  1838. "\"option-data\": [ "
  1839. "{ \"name\": \"foo\", "
  1840. " \"space\": \"vendor-encapsulated-options-space\", "
  1841. " \"data\": \"12345678\" }, "
  1842. "{ \"name\": \"vendor-class-identifier\", "
  1843. " \"data\": \"bar\" }, "
  1844. "{ \"name\": \"vendor-encapsulated-options\" } ] }";
  1845. ConstElementPtr json;
  1846. ASSERT_NO_THROW(json = parseDHCP4(config));
  1847. ConstElementPtr status;
  1848. // Configure the server and make sure the config is accepted
  1849. EXPECT_NO_THROW(status = configureDhcp4Server(srv, json));
  1850. ASSERT_TRUE(status);
  1851. comment_ = config::parseAnswer(rcode_, status);
  1852. ASSERT_EQ(0, rcode_);
  1853. CfgMgr::instance().commit();
  1854. // Create a packet with enough to select the subnet and go through
  1855. // the DISCOVER processing
  1856. Pkt4Ptr query(new Pkt4(DHCPDISCOVER, 1234));
  1857. query->setRemoteAddr(IOAddress("192.0.2.1"));
  1858. OptionPtr clientid = generateClientId();
  1859. query->addOption(clientid);
  1860. query->setIface("eth1");
  1861. // Create and add a PRL option to the query
  1862. OptionUint8ArrayPtr prl(new OptionUint8Array(Option::V4,
  1863. DHO_DHCP_PARAMETER_REQUEST_LIST));
  1864. ASSERT_TRUE(prl);
  1865. prl->addValue(DHO_VENDOR_ENCAPSULATED_OPTIONS);
  1866. prl->addValue(DHO_VENDOR_CLASS_IDENTIFIER);
  1867. query->addOption(prl);
  1868. srv.classifyPacket(query);
  1869. srv.deferredUnpack(query);
  1870. // Pass it to the server and get an offer
  1871. Pkt4Ptr offer = srv.processDiscover(query);
  1872. // Check if we get response at all
  1873. checkResponse(offer, DHCPOFFER, 1234);
  1874. // Processing should add a vendor-class-identifier (code 60)
  1875. OptionPtr opt = offer->getOption(DHO_VENDOR_CLASS_IDENTIFIER);
  1876. EXPECT_TRUE(opt);
  1877. // And a vendor-encapsulated-options (code 43)
  1878. opt = offer->getOption(DHO_VENDOR_ENCAPSULATED_OPTIONS);
  1879. ASSERT_TRUE(opt);
  1880. const OptionCollection& opts = opt->getOptions();
  1881. ASSERT_EQ(1, opts.size());
  1882. OptionPtr sopt = opts.begin()->second;
  1883. ASSERT_TRUE(sopt);
  1884. EXPECT_EQ(1, sopt->getType());
  1885. }
  1886. // Checks effect of raw not compatible option 43
  1887. TEST_F(Dhcpv4SrvTest, option43BadRaw) {
  1888. IfaceMgrTestConfig test_config(true);
  1889. IfaceMgr::instance().openSockets4();
  1890. NakedDhcpv4Srv srv(0);
  1891. // The vendor-encapsulated-options has an incompatible data
  1892. // so won't have the expected content.
  1893. string config = "{ \"interfaces-config\": {"
  1894. " \"interfaces\": [ \"*\" ] }, "
  1895. "\"rebind-timer\": 2000, "
  1896. "\"renew-timer\": 1000, "
  1897. "\"valid-lifetime\": 4000, "
  1898. "\"subnet4\": [ "
  1899. "{ \"pools\": [ { \"pool\": \"192.0.2.1 - 192.0.2.100\" } ], "
  1900. " \"subnet\": \"192.0.2.0/24\" } ],"
  1901. "\"option-data\": [ "
  1902. "{ \"name\": \"vendor-class-identifier\", "
  1903. " \"data\": \"bar\" }, "
  1904. "{ \"name\": \"vendor-encapsulated-options\", "
  1905. " \"csv-format\": false, "
  1906. " \"data\": \"0102\" } ] }";
  1907. ConstElementPtr json;
  1908. ASSERT_NO_THROW(json = parseDHCP4(config));
  1909. ConstElementPtr status;
  1910. // Configure the server and make sure the config is accepted
  1911. EXPECT_NO_THROW(status = configureDhcp4Server(srv, json));
  1912. ASSERT_TRUE(status);
  1913. comment_ = config::parseAnswer(rcode_, status);
  1914. ASSERT_EQ(0, rcode_);
  1915. CfgMgr::instance().commit();
  1916. // Create a packet with enough to select the subnet and go through
  1917. // the DISCOVER processing
  1918. Pkt4Ptr query(new Pkt4(DHCPDISCOVER, 1234));
  1919. query->setRemoteAddr(IOAddress("192.0.2.1"));
  1920. OptionPtr clientid = generateClientId();
  1921. query->addOption(clientid);
  1922. query->setIface("eth1");
  1923. // Create and add a vendor-encapsulated-options (code 43)
  1924. // with not compatible (not parsable as suboptions) content
  1925. OptionBuffer buf;
  1926. buf.push_back(0x01);
  1927. buf.push_back(0x02);
  1928. OptionPtr vopt(new Option(Option::V4, DHO_VENDOR_ENCAPSULATED_OPTIONS, buf));
  1929. query->addOption(vopt);
  1930. query->deferredOptions().push_back(DHO_VENDOR_ENCAPSULATED_OPTIONS);
  1931. // Create and add a PRL option to the query
  1932. OptionUint8ArrayPtr prl(new OptionUint8Array(Option::V4,
  1933. DHO_DHCP_PARAMETER_REQUEST_LIST));
  1934. ASSERT_TRUE(prl);
  1935. prl->addValue(DHO_VENDOR_ENCAPSULATED_OPTIONS);
  1936. prl->addValue(DHO_VENDOR_CLASS_IDENTIFIER);
  1937. query->addOption(prl);
  1938. srv.classifyPacket(query);
  1939. srv.deferredUnpack(query);
  1940. // Check if the option was (uncorrectly) re-unpacked
  1941. vopt = query->getOption(DHO_VENDOR_ENCAPSULATED_OPTIONS);
  1942. OptionCustomPtr custom = boost::dynamic_pointer_cast<OptionCustom>(vopt);
  1943. EXPECT_TRUE(custom);
  1944. // Pass it to the server and get an offer
  1945. Pkt4Ptr offer = srv.processDiscover(query);
  1946. // Check if we get response at all
  1947. checkResponse(offer, DHCPOFFER, 1234);
  1948. // Processing should add a vendor-class-identifier (code 60)
  1949. OptionPtr opt = offer->getOption(DHO_VENDOR_CLASS_IDENTIFIER);
  1950. EXPECT_TRUE(opt);
  1951. // And a vendor-encapsulated-options (code 43)
  1952. opt = offer->getOption(DHO_VENDOR_ENCAPSULATED_OPTIONS);
  1953. ASSERT_TRUE(opt);
  1954. // But truncated.
  1955. EXPECT_EQ(0, opt->len() - opt->getHeaderLen());
  1956. }
  1957. // Verifies raw option 43 can be handled (global)
  1958. TEST_F(Dhcpv4SrvTest, option43RawGlobal) {
  1959. IfaceMgrTestConfig test_config(true);
  1960. IfaceMgr::instance().openSockets4();
  1961. NakedDhcpv4Srv srv(0);
  1962. // The vendor-encapsulated-options is redefined as raw binary
  1963. // in a global definition.
  1964. string config = "{ \"interfaces-config\": {"
  1965. " \"interfaces\": [ \"*\" ] }, "
  1966. "\"rebind-timer\": 2000, "
  1967. "\"renew-timer\": 1000, "
  1968. "\"valid-lifetime\": 4000, "
  1969. "\"subnet4\": [ "
  1970. "{ \"pools\": [ { \"pool\": \"192.0.2.1 - 192.0.2.100\" } ], "
  1971. " \"subnet\": \"192.0.2.0/24\" } ],"
  1972. "\"option-def\": [ "
  1973. "{ \"code\": 43, "
  1974. " \"name\": \"vendor-encapsulated-options\", "
  1975. " \"type\": \"binary\" } ],"
  1976. "\"option-data\": [ "
  1977. "{ \"name\": \"vendor-class-identifier\", "
  1978. " \"data\": \"bar\" }, "
  1979. "{ \"name\": \"vendor-encapsulated-options\", "
  1980. " \"csv-format\": false, "
  1981. " \"data\": \"0102\" } ] }";
  1982. ConstElementPtr json;
  1983. ASSERT_NO_THROW(json = parseDHCP4(config));
  1984. ConstElementPtr status;
  1985. // Configure the server and make sure the config is accepted
  1986. EXPECT_NO_THROW(status = configureDhcp4Server(srv, json));
  1987. ASSERT_TRUE(status);
  1988. comment_ = config::parseAnswer(rcode_, status);
  1989. ASSERT_EQ(0, rcode_);
  1990. CfgMgr::instance().commit();
  1991. // Create a packet with enough to select the subnet and go through
  1992. // the DISCOVER processing
  1993. Pkt4Ptr query(new Pkt4(DHCPDISCOVER, 1234));
  1994. query->setRemoteAddr(IOAddress("192.0.2.1"));
  1995. OptionPtr clientid = generateClientId();
  1996. query->addOption(clientid);
  1997. query->setIface("eth1");
  1998. // Create and add a vendor-encapsulated-options (code 43)
  1999. // with not compatible (not parsable as suboptions) content
  2000. OptionBuffer buf;
  2001. buf.push_back(0x02);
  2002. buf.push_back(0x03);
  2003. OptionPtr vopt(new Option(Option::V4, DHO_VENDOR_ENCAPSULATED_OPTIONS, buf));
  2004. query->addOption(vopt);
  2005. query->deferredOptions().push_back(DHO_VENDOR_ENCAPSULATED_OPTIONS);
  2006. // Create and add a PRL option to the query
  2007. OptionUint8ArrayPtr prl(new OptionUint8Array(Option::V4,
  2008. DHO_DHCP_PARAMETER_REQUEST_LIST));
  2009. ASSERT_TRUE(prl);
  2010. prl->addValue(DHO_VENDOR_ENCAPSULATED_OPTIONS);
  2011. prl->addValue(DHO_VENDOR_CLASS_IDENTIFIER);
  2012. query->addOption(prl);
  2013. srv.classifyPacket(query);
  2014. srv.deferredUnpack(query);
  2015. // Check if the option was (correctly) re-unpacked
  2016. vopt = query->getOption(DHO_VENDOR_ENCAPSULATED_OPTIONS);
  2017. OptionCustomPtr custom = boost::dynamic_pointer_cast<OptionCustom>(vopt);
  2018. EXPECT_FALSE(custom);
  2019. // Pass it to the server and get an offer
  2020. Pkt4Ptr offer = srv.processDiscover(query);
  2021. // Check if we get response at all
  2022. checkResponse(offer, DHCPOFFER, 1234);
  2023. // Processing should add a vendor-class-identifier (code 60)
  2024. OptionPtr opt = offer->getOption(DHO_VENDOR_CLASS_IDENTIFIER);
  2025. EXPECT_TRUE(opt);
  2026. // And a vendor-encapsulated-options (code 43)
  2027. opt = offer->getOption(DHO_VENDOR_ENCAPSULATED_OPTIONS);
  2028. ASSERT_TRUE(opt);
  2029. // Verifies the content
  2030. ASSERT_EQ(2, opt->len() - opt->getHeaderLen());
  2031. EXPECT_EQ(0x01, opt->getData()[0]);
  2032. EXPECT_EQ(0x02, opt->getData()[1]);
  2033. }
  2034. // Verifies raw option 43 can be handled (catch-all class)
  2035. TEST_F(Dhcpv4SrvTest, option43RawClass) {
  2036. IfaceMgrTestConfig test_config(true);
  2037. IfaceMgr::instance().openSockets4();
  2038. NakedDhcpv4Srv srv(0);
  2039. // The vendor-encapsulated-options is redefined as raw binary
  2040. // in a class definition.
  2041. string config = "{ \"interfaces-config\": {"
  2042. " \"interfaces\": [ \"*\" ] }, "
  2043. "\"rebind-timer\": 2000, "
  2044. "\"renew-timer\": 1000, "
  2045. "\"valid-lifetime\": 4000, "
  2046. "\"subnet4\": [ "
  2047. "{ \"pools\": [ { \"pool\": \"192.0.2.1 - 192.0.2.100\" } ], "
  2048. " \"subnet\": \"192.0.2.0/24\" } ],"
  2049. "\"client-classes\": [ "
  2050. "{ \"name\": \"vendor\", "
  2051. " \"test\": \"option[vendor-encapsulated-options].exists\", "
  2052. " \"option-def\": [ "
  2053. " { \"code\": 43, "
  2054. " \"name\": \"vendor-encapsulated-options\", "
  2055. " \"type\": \"binary\" } ],"
  2056. " \"option-data\": [ "
  2057. " { \"name\": \"vendor-class-identifier\", "
  2058. " \"data\": \"bar\" }, "
  2059. " { \"name\": \"vendor-encapsulated-options\", "
  2060. " \"csv-format\": false, "
  2061. " \"data\": \"0102\" } ] } ] }";
  2062. ConstElementPtr json;
  2063. ASSERT_NO_THROW(json = parseDHCP4(config));
  2064. ConstElementPtr status;
  2065. // Configure the server and make sure the config is accepted
  2066. EXPECT_NO_THROW(status = configureDhcp4Server(srv, json));
  2067. ASSERT_TRUE(status);
  2068. comment_ = config::parseAnswer(rcode_, status);
  2069. ASSERT_EQ(0, rcode_);
  2070. CfgMgr::instance().commit();
  2071. // Create a packet with enough to select the subnet and go through
  2072. // the DISCOVER processing
  2073. Pkt4Ptr query(new Pkt4(DHCPDISCOVER, 1234));
  2074. query->setRemoteAddr(IOAddress("192.0.2.1"));
  2075. OptionPtr clientid = generateClientId();
  2076. query->addOption(clientid);
  2077. query->setIface("eth1");
  2078. // Create and add a vendor-encapsulated-options (code 43)
  2079. // with not compatible (not parsable as suboptions) content
  2080. OptionBuffer buf;
  2081. buf.push_back(0x02);
  2082. buf.push_back(0x03);
  2083. OptionPtr vopt(new Option(Option::V4, DHO_VENDOR_ENCAPSULATED_OPTIONS, buf));
  2084. query->addOption(vopt);
  2085. query->deferredOptions().push_back(DHO_VENDOR_ENCAPSULATED_OPTIONS);
  2086. // Create and add a PRL option to the query
  2087. OptionUint8ArrayPtr prl(new OptionUint8Array(Option::V4,
  2088. DHO_DHCP_PARAMETER_REQUEST_LIST));
  2089. ASSERT_TRUE(prl);
  2090. prl->addValue(DHO_VENDOR_ENCAPSULATED_OPTIONS);
  2091. prl->addValue(DHO_VENDOR_CLASS_IDENTIFIER);
  2092. query->addOption(prl);
  2093. srv.classifyPacket(query);
  2094. srv.deferredUnpack(query);
  2095. // Check if the option was (correctly) re-unpacked
  2096. vopt = query->getOption(DHO_VENDOR_ENCAPSULATED_OPTIONS);
  2097. OptionCustomPtr custom = boost::dynamic_pointer_cast<OptionCustom>(vopt);
  2098. EXPECT_FALSE(custom);
  2099. // Pass it to the server and get an offer
  2100. Pkt4Ptr offer = srv.processDiscover(query);
  2101. // Check if we get response at all
  2102. checkResponse(offer, DHCPOFFER, 1234);
  2103. // Processing should add a vendor-class-identifier (code 60)
  2104. OptionPtr opt = offer->getOption(DHO_VENDOR_CLASS_IDENTIFIER);
  2105. EXPECT_TRUE(opt);
  2106. // And a vendor-encapsulated-options (code 43)
  2107. opt = offer->getOption(DHO_VENDOR_ENCAPSULATED_OPTIONS);
  2108. ASSERT_TRUE(opt);
  2109. // Verifies the content
  2110. ASSERT_EQ(2, opt->len() - opt->getHeaderLen());
  2111. EXPECT_EQ(0x01, opt->getData()[0]);
  2112. EXPECT_EQ(0x02, opt->getData()[1]);
  2113. }
  2114. // Verifies option 43 deferred processing (one class)
  2115. TEST_F(Dhcpv4SrvTest, option43Class) {
  2116. IfaceMgrTestConfig test_config(true);
  2117. IfaceMgr::instance().openSockets4();
  2118. NakedDhcpv4Srv srv(0);
  2119. // A client class defines vendor-encapsulated-options (code 43)
  2120. // and data for it and its sub-option.
  2121. string config = "{ \"interfaces-config\": {"
  2122. " \"interfaces\": [ \"*\" ] }, "
  2123. "\"rebind-timer\": 2000, "
  2124. "\"renew-timer\": 1000, "
  2125. "\"valid-lifetime\": 4000, "
  2126. "\"option-def\": [ "
  2127. "{ \"code\": 1, "
  2128. " \"name\": \"foo\", "
  2129. " \"space\": \"alpha\", "
  2130. " \"type\": \"uint32\" } ],"
  2131. "\"subnet4\": [ "
  2132. "{ \"pools\": [ { \"pool\": \"192.0.2.1 - 192.0.2.100\" } ], "
  2133. " \"subnet\": \"192.0.2.0/24\" } ],"
  2134. "\"client-classes\": [ "
  2135. "{ \"name\": \"alpha\", "
  2136. " \"test\": \"option[vendor-class-identifier].text == 'alpha'\", "
  2137. " \"option-def\": [ "
  2138. " { \"code\": 43, "
  2139. " \"name\": \"vendor-encapsulated-options\", "
  2140. " \"type\": \"empty\", "
  2141. " \"encapsulate\": \"alpha\" } ],"
  2142. " \"option-data\": [ "
  2143. " { \"name\": \"vendor-class-identifier\", "
  2144. " \"data\": \"alpha\" }, "
  2145. " { \"name\": \"vendor-encapsulated-options\" }, "
  2146. " { \"name\": \"foo\", "
  2147. " \"space\": \"alpha\", "
  2148. " \"data\": \"12345678\" } ] } ] }";
  2149. ConstElementPtr json;
  2150. ASSERT_NO_THROW(json = parseDHCP4(config));
  2151. ConstElementPtr status;
  2152. // Configure the server and make sure the config is accepted
  2153. EXPECT_NO_THROW(status = configureDhcp4Server(srv, json));
  2154. ASSERT_TRUE(status);
  2155. comment_ = config::parseAnswer(rcode_, status);
  2156. ASSERT_EQ(0, rcode_);
  2157. CfgMgr::instance().commit();
  2158. // Create a packet with enough to select the subnet and go through
  2159. // the DISCOVER processing
  2160. Pkt4Ptr query(new Pkt4(DHCPDISCOVER, 1234));
  2161. query->setRemoteAddr(IOAddress("192.0.2.1"));
  2162. OptionPtr clientid = generateClientId();
  2163. query->addOption(clientid);
  2164. query->setIface("eth1");
  2165. // Create and add a vendor-encapsulated-options (code 43)
  2166. OptionBuffer buf;
  2167. buf.push_back(0x01);
  2168. buf.push_back(0x04);
  2169. buf.push_back(0x87);
  2170. buf.push_back(0x65);
  2171. buf.push_back(0x43);
  2172. buf.push_back(0x21);
  2173. OptionPtr vopt(new Option(Option::V4, DHO_VENDOR_ENCAPSULATED_OPTIONS, buf));
  2174. query->addOption(vopt);
  2175. query->deferredOptions().push_back(DHO_VENDOR_ENCAPSULATED_OPTIONS);
  2176. // Create and add a vendor-class-identifier (code 60)
  2177. OptionStringPtr iopt(new OptionString(Option::V4,
  2178. DHO_VENDOR_CLASS_IDENTIFIER,
  2179. "alpha"));
  2180. query->addOption(iopt);
  2181. // Create and add a PRL option to the query
  2182. OptionUint8ArrayPtr prl(new OptionUint8Array(Option::V4,
  2183. DHO_DHCP_PARAMETER_REQUEST_LIST));
  2184. ASSERT_TRUE(prl);
  2185. prl->addValue(DHO_VENDOR_ENCAPSULATED_OPTIONS);
  2186. prl->addValue(DHO_VENDOR_CLASS_IDENTIFIER);
  2187. query->addOption(prl);
  2188. srv.classifyPacket(query);
  2189. srv.deferredUnpack(query);
  2190. // Check if the option was (correctly) re-unpacked
  2191. vopt = query->getOption(DHO_VENDOR_ENCAPSULATED_OPTIONS);
  2192. OptionCustomPtr custom = boost::dynamic_pointer_cast<OptionCustom>(vopt);
  2193. EXPECT_TRUE(custom);
  2194. EXPECT_EQ(1, vopt->getOptions().size());
  2195. // Pass it to the server and get an offer
  2196. Pkt4Ptr offer = srv.processDiscover(query);
  2197. // Check if we get response at all
  2198. checkResponse(offer, DHCPOFFER, 1234);
  2199. // Processing should add a vendor-class-identifier (code 60)
  2200. OptionPtr opt = offer->getOption(DHO_VENDOR_CLASS_IDENTIFIER);
  2201. EXPECT_TRUE(opt);
  2202. // And a vendor-encapsulated-options (code 43)
  2203. opt = offer->getOption(DHO_VENDOR_ENCAPSULATED_OPTIONS);
  2204. ASSERT_TRUE(opt);
  2205. // Verifies the content
  2206. const OptionCollection& opts = opt->getOptions();
  2207. ASSERT_EQ(1, opts.size());
  2208. OptionPtr sopt = opts.begin()->second;
  2209. ASSERT_TRUE(sopt);
  2210. EXPECT_EQ(1, sopt->getType());
  2211. OptionUint32Ptr sopt32 = boost::dynamic_pointer_cast<OptionUint32>(sopt);
  2212. ASSERT_TRUE(sopt32);
  2213. EXPECT_EQ(12345678, sopt32->getValue());
  2214. }
  2215. // Verifies option 43 priority
  2216. TEST_F(Dhcpv4SrvTest, option43ClassPriority) {
  2217. IfaceMgrTestConfig test_config(true);
  2218. IfaceMgr::instance().openSockets4();
  2219. NakedDhcpv4Srv srv(0);
  2220. // Both global and client-class scopes get vendor-encapsulated-options
  2221. // (code 43) definition and data. The client-class has precedence.
  2222. // Note it does not work without the vendor-encapsulated-options
  2223. // option-data in the client-class.
  2224. string config = "{ \"interfaces-config\": {"
  2225. " \"interfaces\": [ \"*\" ] }, "
  2226. "\"rebind-timer\": 2000, "
  2227. "\"renew-timer\": 1000, "
  2228. "\"valid-lifetime\": 4000, "
  2229. "\"option-def\": [ "
  2230. "{ \"code\": 1, "
  2231. " \"name\": \"foo\", "
  2232. " \"space\": \"alpha\", "
  2233. " \"type\": \"uint32\" },"
  2234. "{ \"code\": 1, "
  2235. " \"name\": \"bar\", "
  2236. " \"space\": \"beta\", "
  2237. " \"type\": \"uint8\" }, "
  2238. "{ \"code\": 43, "
  2239. " \"name\": \"vendor-encapsulated-options\", "
  2240. " \"type\": \"empty\", "
  2241. " \"encapsulate\": \"beta\" } ],"
  2242. "\"option-data\": [ "
  2243. "{ \"name\": \"vendor-encapsulated-options\" }, "
  2244. "{ \"name\": \"vendor-class-identifier\", "
  2245. " \"data\": \"beta\" }, "
  2246. "{ \"name\": \"bar\", "
  2247. " \"space\": \"beta\", "
  2248. " \"data\": \"33\" } ],"
  2249. "\"subnet4\": [ "
  2250. "{ \"pools\": [ { \"pool\": \"192.0.2.1 - 192.0.2.100\" } ], "
  2251. " \"subnet\": \"192.0.2.0/24\" } ],"
  2252. "\"client-classes\": [ "
  2253. "{ \"name\": \"alpha\", "
  2254. " \"test\": \"option[vendor-class-identifier].text == 'alpha'\", "
  2255. " \"option-def\": [ "
  2256. " { \"code\": 43, "
  2257. " \"name\": \"vendor-encapsulated-options\", "
  2258. " \"type\": \"empty\", "
  2259. " \"encapsulate\": \"alpha\" } ],"
  2260. " \"option-data\": [ "
  2261. "{ \"name\": \"vendor-encapsulated-options\" }, "
  2262. " { \"name\": \"vendor-class-identifier\", "
  2263. " \"data\": \"alpha\" }, "
  2264. " { \"name\": \"foo\", "
  2265. " \"space\": \"alpha\", "
  2266. " \"data\": \"12345678\" } ] } ] }";
  2267. ConstElementPtr json;
  2268. ASSERT_NO_THROW(json = parseDHCP4(config));
  2269. ConstElementPtr status;
  2270. // Configure the server and make sure the config is accepted
  2271. EXPECT_NO_THROW(status = configureDhcp4Server(srv, json));
  2272. ASSERT_TRUE(status);
  2273. comment_ = config::parseAnswer(rcode_, status);
  2274. ASSERT_EQ(0, rcode_);
  2275. CfgMgr::instance().commit();
  2276. // Create a packet with enough to select the subnet and go through
  2277. // the DISCOVER processing
  2278. Pkt4Ptr query(new Pkt4(DHCPDISCOVER, 1234));
  2279. query->setRemoteAddr(IOAddress("192.0.2.1"));
  2280. OptionPtr clientid = generateClientId();
  2281. query->addOption(clientid);
  2282. query->setIface("eth1");
  2283. // Create and add a vendor-encapsulated-options (code 43)
  2284. OptionBuffer buf;
  2285. buf.push_back(0x01);
  2286. buf.push_back(0x04);
  2287. buf.push_back(0x87);
  2288. buf.push_back(0x65);
  2289. buf.push_back(0x43);
  2290. buf.push_back(0x21);
  2291. OptionPtr vopt(new Option(Option::V4, DHO_VENDOR_ENCAPSULATED_OPTIONS, buf));
  2292. query->addOption(vopt);
  2293. query->deferredOptions().push_back(DHO_VENDOR_ENCAPSULATED_OPTIONS);
  2294. // Create and add a vendor-class-identifier (code 60)
  2295. OptionStringPtr iopt(new OptionString(Option::V4,
  2296. DHO_VENDOR_CLASS_IDENTIFIER,
  2297. "alpha"));
  2298. query->addOption(iopt);
  2299. // Create and add a PRL option to the query
  2300. OptionUint8ArrayPtr prl(new OptionUint8Array(Option::V4,
  2301. DHO_DHCP_PARAMETER_REQUEST_LIST));
  2302. ASSERT_TRUE(prl);
  2303. prl->addValue(DHO_VENDOR_ENCAPSULATED_OPTIONS);
  2304. prl->addValue(DHO_VENDOR_CLASS_IDENTIFIER);
  2305. query->addOption(prl);
  2306. srv.classifyPacket(query);
  2307. srv.deferredUnpack(query);
  2308. // Check if the option was (correctly) re-unpacked
  2309. vopt = query->getOption(DHO_VENDOR_ENCAPSULATED_OPTIONS);
  2310. OptionCustomPtr custom = boost::dynamic_pointer_cast<OptionCustom>(vopt);
  2311. EXPECT_TRUE(custom);
  2312. EXPECT_EQ(1, vopt->getOptions().size());
  2313. // Pass it to the server and get an offer
  2314. Pkt4Ptr offer = srv.processDiscover(query);
  2315. // Check if we get response at all
  2316. checkResponse(offer, DHCPOFFER, 1234);
  2317. // Processing should add a vendor-class-identifier (code 60)
  2318. OptionPtr opt = offer->getOption(DHO_VENDOR_CLASS_IDENTIFIER);
  2319. EXPECT_TRUE(opt);
  2320. OptionStringPtr id = boost::dynamic_pointer_cast<OptionString>(opt);
  2321. ASSERT_TRUE(id);
  2322. EXPECT_EQ("alpha", id->getValue());
  2323. // And a vendor-encapsulated-options (code 43)
  2324. opt = offer->getOption(DHO_VENDOR_ENCAPSULATED_OPTIONS);
  2325. ASSERT_TRUE(opt);
  2326. // Verifies the content
  2327. const OptionCollection& opts = opt->getOptions();
  2328. ASSERT_EQ(1, opts.size());
  2329. OptionPtr sopt = opts.begin()->second;
  2330. ASSERT_TRUE(sopt);
  2331. EXPECT_EQ(1, sopt->getType());
  2332. EXPECT_EQ(2 + 4, sopt->len());
  2333. OptionUint32Ptr sopt32 = boost::dynamic_pointer_cast<OptionUint32>(sopt);
  2334. ASSERT_TRUE(sopt32);
  2335. EXPECT_EQ(12345678, sopt32->getValue());
  2336. }
  2337. // Verifies option 43 deferred processing (two classes)
  2338. TEST_F(Dhcpv4SrvTest, option43Classes) {
  2339. IfaceMgrTestConfig test_config(true);
  2340. IfaceMgr::instance().openSockets4();
  2341. NakedDhcpv4Srv srv(0);
  2342. // Two client-class scopes get vendor-encapsulated-options
  2343. // (code 43) definition and data. The first matching client-class
  2344. // (from a set?) applies.
  2345. string config = "{ \"interfaces-config\": {"
  2346. " \"interfaces\": [ \"*\" ] }, "
  2347. "\"rebind-timer\": 2000, "
  2348. "\"renew-timer\": 1000, "
  2349. "\"valid-lifetime\": 4000, "
  2350. "\"option-def\": [ "
  2351. "{ \"code\": 1, "
  2352. " \"name\": \"foo\", "
  2353. " \"space\": \"alpha\", "
  2354. " \"type\": \"uint32\" },"
  2355. "{ \"code\": 1, "
  2356. " \"name\": \"bar\", "
  2357. " \"space\": \"beta\", "
  2358. " \"type\": \"uint8\" } ],"
  2359. "\"subnet4\": [ "
  2360. "{ \"pools\": [ { \"pool\": \"192.0.2.1 - 192.0.2.100\" } ], "
  2361. " \"subnet\": \"192.0.2.0/24\" } ],"
  2362. "\"client-classes\": [ "
  2363. "{ \"name\": \"alpha\", "
  2364. " \"test\": \"option[vendor-class-identifier].text == 'alpha'\", "
  2365. " \"option-def\": [ "
  2366. " { \"code\": 43, "
  2367. " \"name\": \"vendor-encapsulated-options\", "
  2368. " \"type\": \"empty\", "
  2369. " \"encapsulate\": \"alpha\" } ],"
  2370. " \"option-data\": [ "
  2371. "{ \"name\": \"vendor-encapsulated-options\" }, "
  2372. " { \"name\": \"vendor-class-identifier\", "
  2373. " \"data\": \"alpha\" }, "
  2374. " { \"name\": \"foo\", "
  2375. " \"space\": \"alpha\", "
  2376. " \"data\": \"12345678\" } ] },"
  2377. "{ \"name\": \"beta\", "
  2378. " \"test\": \"option[vendor-class-identifier].text == 'beta'\", "
  2379. " \"option-def\": [ "
  2380. " { \"code\": 43, "
  2381. " \"name\": \"vendor-encapsulated-options\", "
  2382. " \"type\": \"empty\", "
  2383. " \"encapsulate\": \"beta\" } ],"
  2384. " \"option-data\": [ "
  2385. "{ \"name\": \"vendor-encapsulated-options\" }, "
  2386. " { \"name\": \"vendor-class-identifier\", "
  2387. " \"data\": \"beta\" }, "
  2388. " { \"name\": \"bar\", "
  2389. " \"space\": \"beta\", "
  2390. " \"data\": \"33\" } ] } ] }";
  2391. ConstElementPtr json;
  2392. ASSERT_NO_THROW(json = parseDHCP4(config));
  2393. ConstElementPtr status;
  2394. // Configure the server and make sure the config is accepted
  2395. EXPECT_NO_THROW(status = configureDhcp4Server(srv, json));
  2396. ASSERT_TRUE(status);
  2397. comment_ = config::parseAnswer(rcode_, status);
  2398. ASSERT_EQ(0, rcode_);
  2399. CfgMgr::instance().commit();
  2400. // Create a packet with enough to select the subnet and go through
  2401. // the DISCOVER processing
  2402. Pkt4Ptr query(new Pkt4(DHCPDISCOVER, 1234));
  2403. query->setRemoteAddr(IOAddress("192.0.2.1"));
  2404. OptionPtr clientid = generateClientId();
  2405. query->addOption(clientid);
  2406. query->setIface("eth1");
  2407. // Create and add a vendor-encapsulated-options (code 43)
  2408. OptionBuffer buf;
  2409. buf.push_back(0x01);
  2410. buf.push_back(0x04);
  2411. buf.push_back(0x87);
  2412. buf.push_back(0x65);
  2413. buf.push_back(0x43);
  2414. buf.push_back(0x21);
  2415. OptionPtr vopt(new Option(Option::V4, DHO_VENDOR_ENCAPSULATED_OPTIONS, buf));
  2416. query->addOption(vopt);
  2417. query->deferredOptions().push_back(DHO_VENDOR_ENCAPSULATED_OPTIONS);
  2418. // Create and add a vendor-class-identifier (code 60)
  2419. OptionStringPtr iopt(new OptionString(Option::V4,
  2420. DHO_VENDOR_CLASS_IDENTIFIER,
  2421. "alpha"));
  2422. query->addOption(iopt);
  2423. // Create and add a PRL option to the query
  2424. OptionUint8ArrayPtr prl(new OptionUint8Array(Option::V4,
  2425. DHO_DHCP_PARAMETER_REQUEST_LIST));
  2426. ASSERT_TRUE(prl);
  2427. prl->addValue(DHO_VENDOR_ENCAPSULATED_OPTIONS);
  2428. prl->addValue(DHO_VENDOR_CLASS_IDENTIFIER);
  2429. query->addOption(prl);
  2430. srv.classifyPacket(query);
  2431. srv.deferredUnpack(query);
  2432. // Check if the option was (correctly) re-unpacked
  2433. vopt = query->getOption(DHO_VENDOR_ENCAPSULATED_OPTIONS);
  2434. OptionCustomPtr custom = boost::dynamic_pointer_cast<OptionCustom>(vopt);
  2435. EXPECT_TRUE(custom);
  2436. EXPECT_EQ(1, vopt->getOptions().size());
  2437. // Pass it to the server and get an offer
  2438. Pkt4Ptr offer = srv.processDiscover(query);
  2439. // Check if we get response at all
  2440. checkResponse(offer, DHCPOFFER, 1234);
  2441. // Processing should add a vendor-class-identifier (code 60)
  2442. OptionPtr opt = offer->getOption(DHO_VENDOR_CLASS_IDENTIFIER);
  2443. EXPECT_TRUE(opt);
  2444. OptionStringPtr id = boost::dynamic_pointer_cast<OptionString>(opt);
  2445. ASSERT_TRUE(id);
  2446. EXPECT_EQ("alpha", id->getValue());
  2447. // And a vendor-encapsulated-options (code 43)
  2448. opt = offer->getOption(DHO_VENDOR_ENCAPSULATED_OPTIONS);
  2449. ASSERT_TRUE(opt);
  2450. // Verifies the content
  2451. const OptionCollection& opts = opt->getOptions();
  2452. ASSERT_EQ(1, opts.size());
  2453. OptionPtr sopt = opts.begin()->second;
  2454. ASSERT_TRUE(sopt);
  2455. EXPECT_EQ(1, sopt->getType());
  2456. EXPECT_EQ(2 + 4, sopt->len());
  2457. OptionUint32Ptr sopt32 = boost::dynamic_pointer_cast<OptionUint32>(sopt);
  2458. ASSERT_TRUE(sopt32);
  2459. EXPECT_EQ(12345678, sopt32->getValue());
  2460. }
  2461. // Verifies private option deferred processing
  2462. TEST_F(Dhcpv4SrvTest, privateOption) {
  2463. IfaceMgrTestConfig test_config(true);
  2464. IfaceMgr::instance().openSockets4();
  2465. NakedDhcpv4Srv srv(0);
  2466. // Same than option43Class but with private options
  2467. string config = "{ \"interfaces-config\": {"
  2468. " \"interfaces\": [ \"*\" ] }, "
  2469. "\"rebind-timer\": 2000, "
  2470. "\"renew-timer\": 1000, "
  2471. "\"valid-lifetime\": 4000, "
  2472. "\"subnet4\": [ "
  2473. "{ \"pools\": [ { \"pool\": \"192.0.2.1 - 192.0.2.100\" } ], "
  2474. " \"subnet\": \"192.0.2.0/24\" } ],"
  2475. "\"client-classes\": [ "
  2476. "{ \"name\": \"private\", "
  2477. " \"test\": \"option[234].exists\", "
  2478. " \"option-def\": [ "
  2479. " { \"code\": 245, "
  2480. " \"name\": \"privint\", "
  2481. " \"type\": \"uint32\" } ],"
  2482. " \"option-data\": [ "
  2483. " { \"code\": 234, "
  2484. " \"data\": \"01\" }, "
  2485. " { \"name\": \"privint\", "
  2486. " \"data\": \"12345678\" } ] } ] }";
  2487. ConstElementPtr json;
  2488. ASSERT_NO_THROW(json = parseDHCP4(config));
  2489. ConstElementPtr status;
  2490. // Configure the server and make sure the config is accepted
  2491. EXPECT_NO_THROW(status = configureDhcp4Server(srv, json));
  2492. ASSERT_TRUE(status);
  2493. comment_ = config::parseAnswer(rcode_, status);
  2494. ASSERT_EQ(0, rcode_);
  2495. CfgMgr::instance().commit();
  2496. // Create a packet with enough to select the subnet and go through
  2497. // the DISCOVER processing
  2498. Pkt4Ptr query(new Pkt4(DHCPDISCOVER, 1234));
  2499. query->setRemoteAddr(IOAddress("192.0.2.1"));
  2500. OptionPtr clientid = generateClientId();
  2501. query->addOption(clientid);
  2502. query->setIface("eth1");
  2503. // Create and add a private option with code 234
  2504. OptionBuffer buf;
  2505. buf.push_back(0x01);
  2506. OptionPtr opt1(new Option(Option::V4, 234, buf));
  2507. query->addOption(opt1);
  2508. query->deferredOptions().push_back(234);
  2509. // Create and add a private option with code 245
  2510. buf.clear();
  2511. buf.push_back(0x87);
  2512. buf.push_back(0x65);
  2513. buf.push_back(0x43);
  2514. buf.push_back(0x21);
  2515. OptionPtr opt2(new Option(Option::V4, 245, buf));
  2516. query->addOption(opt2);
  2517. query->deferredOptions().push_back(245);
  2518. // Create and add a PRL option to the query
  2519. OptionUint8ArrayPtr prl(new OptionUint8Array(Option::V4,
  2520. DHO_DHCP_PARAMETER_REQUEST_LIST));
  2521. ASSERT_TRUE(prl);
  2522. prl->addValue(234);
  2523. prl->addValue(245);
  2524. query->addOption(prl);
  2525. srv.classifyPacket(query);
  2526. srv.deferredUnpack(query);
  2527. // Check if the option 245 was re-unpacked
  2528. opt2 = query->getOption(245);
  2529. OptionUint32Ptr opt32 = boost::dynamic_pointer_cast<OptionUint32>(opt2);
  2530. EXPECT_TRUE(opt32);
  2531. // Pass it to the server and get an offer
  2532. Pkt4Ptr offer = srv.processDiscover(query);
  2533. // Check if we get response at all
  2534. checkResponse(offer, DHCPOFFER, 1234);
  2535. // Processing should add an option with code 234
  2536. OptionPtr opt = offer->getOption(234);
  2537. EXPECT_TRUE(opt);
  2538. // And an option with code 245
  2539. opt = offer->getOption(245);
  2540. ASSERT_TRUE(opt);
  2541. // Verifies the content
  2542. opt32 = boost::dynamic_pointer_cast<OptionUint32>(opt);
  2543. ASSERT_TRUE(opt32);
  2544. EXPECT_EQ(12345678, opt32->getValue());
  2545. }
  2546. // Checks effect of persistency (aka always-true) flag on the PRL
  2547. TEST_F(Dhcpv4SrvTest, prlPersistency) {
  2548. IfaceMgrTestConfig test_config(true);
  2549. IfaceMgr::instance().openSockets4();
  2550. ASSERT_NO_THROW(configure(CONFIGS[2]));
  2551. // Create a packet with enough to select the subnet and go through
  2552. // the DISCOVER processing
  2553. Pkt4Ptr query(new Pkt4(DHCPDISCOVER, 1234));
  2554. query->setRemoteAddr(IOAddress("192.0.2.1"));
  2555. OptionPtr clientid = generateClientId();
  2556. query->addOption(clientid);
  2557. query->setIface("eth1");
  2558. // Create and add a PRL option for another option
  2559. OptionUint8ArrayPtr prl(new OptionUint8Array(Option::V4,
  2560. DHO_DHCP_PARAMETER_REQUEST_LIST));
  2561. ASSERT_TRUE(prl);
  2562. prl->addValue(DHO_ARP_CACHE_TIMEOUT);
  2563. query->addOption(prl);
  2564. // Create and add a host-name option to the query
  2565. OptionStringPtr hostname(new OptionString(Option::V4, 12, "foo"));
  2566. ASSERT_TRUE(hostname);
  2567. query->addOption(hostname);
  2568. // Let the server process it.
  2569. Pkt4Ptr response = srv_.processDiscover(query);
  2570. // Processing should add an ip-forwarding option
  2571. ASSERT_TRUE(response->getOption(DHO_IP_FORWARDING));
  2572. // But no default-ip-ttl
  2573. ASSERT_FALSE(response->getOption(DHO_DEFAULT_IP_TTL));
  2574. // Nor an arp-cache-timeout
  2575. ASSERT_FALSE(response->getOption(DHO_ARP_CACHE_TIMEOUT));
  2576. // Reset PRL adding default-ip-ttl
  2577. query->delOption(DHO_DHCP_PARAMETER_REQUEST_LIST);
  2578. prl->addValue(DHO_DEFAULT_IP_TTL);
  2579. query->addOption(prl);
  2580. // Let the server process it again.
  2581. response = srv_.processDiscover(query);
  2582. // Processing should add an ip-forwarding option
  2583. ASSERT_TRUE(response->getOption(DHO_IP_FORWARDING));
  2584. // and now a default-ip-ttl
  2585. ASSERT_TRUE(response->getOption(DHO_DEFAULT_IP_TTL));
  2586. // and still no arp-cache-timeout
  2587. ASSERT_FALSE(response->getOption(DHO_ARP_CACHE_TIMEOUT));
  2588. }
  2589. // Checks if relay IP address specified in the relay-info structure in
  2590. // subnet4 is being used properly.
  2591. TEST_F(Dhcpv4SrvTest, relayOverride) {
  2592. // We have 2 subnets defined. Note that both have a relay address
  2593. // defined. Both are not belonging to the subnets. That is
  2594. // important, because if the relay belongs to the subnet, there's
  2595. // no need to specify relay override.
  2596. string config = "{ \"interfaces-config\": {"
  2597. " \"interfaces\": [ \"*\" ]"
  2598. "},"
  2599. "\"rebind-timer\": 2000, "
  2600. "\"renew-timer\": 1000, "
  2601. "\"subnet4\": [ "
  2602. "{ \"pools\": [ { \"pool\": \"192.0.2.2 - 192.0.2.100\" } ],"
  2603. " \"relay\": { "
  2604. " \"ip-address\": \"192.0.5.1\""
  2605. " },"
  2606. " \"subnet\": \"192.0.2.0/24\" }, "
  2607. "{ \"pools\": [ { \"pool\": \"192.0.3.1 - 192.0.3.100\" } ],"
  2608. " \"relay\": { "
  2609. " \"ip-address\": \"192.0.5.2\""
  2610. " },"
  2611. " \"subnet\": \"192.0.3.0/24\" } "
  2612. "],"
  2613. "\"valid-lifetime\": 4000 }";
  2614. // Use this config to set up the server
  2615. ASSERT_NO_THROW(configure(config));
  2616. // Let's get the subnet configuration objects
  2617. const Subnet4Collection* subnets =
  2618. CfgMgr::instance().getCurrentCfg()->getCfgSubnets4()->getAll();
  2619. ASSERT_EQ(2, subnets->size());
  2620. // Let's get them for easy reference
  2621. Subnet4Ptr subnet1 = (*subnets)[0];
  2622. Subnet4Ptr subnet2 = (*subnets)[1];
  2623. ASSERT_TRUE(subnet1);
  2624. ASSERT_TRUE(subnet2);
  2625. // Let's create a packet.
  2626. Pkt4Ptr dis = Pkt4Ptr(new Pkt4(DHCPDISCOVER, 1234));
  2627. dis->setRemoteAddr(IOAddress("192.0.2.1"));
  2628. dis->setIface("eth0");
  2629. dis->setHops(1);
  2630. OptionPtr clientid = generateClientId();
  2631. dis->addOption(clientid);
  2632. // This is just a sanity check, we're using regular method: ciaddr 192.0.2.1
  2633. // belongs to the first subnet, so it is selected
  2634. dis->setGiaddr(IOAddress("192.0.2.1"));
  2635. EXPECT_TRUE(subnet1 == srv_.selectSubnet(dis));
  2636. // Relay belongs to the second subnet, so it should be selected.
  2637. dis->setGiaddr(IOAddress("192.0.3.1"));
  2638. EXPECT_TRUE(subnet2 == srv_.selectSubnet(dis));
  2639. // Now let's check if the relay override for the first subnets works
  2640. dis->setGiaddr(IOAddress("192.0.5.1"));
  2641. EXPECT_TRUE(subnet1 == srv_.selectSubnet(dis));
  2642. // The same check for the second subnet...
  2643. dis->setGiaddr(IOAddress("192.0.5.2"));
  2644. EXPECT_TRUE(subnet2 == srv_.selectSubnet(dis));
  2645. // And finally, let's check if mis-matched relay address will end up
  2646. // in not selecting a subnet at all
  2647. dis->setGiaddr(IOAddress("192.0.5.3"));
  2648. EXPECT_FALSE(srv_.selectSubnet(dis));
  2649. // Finally, check that the relay override works only with relay address
  2650. // (GIADDR) and does not affect client address (CIADDR)
  2651. dis->setGiaddr(IOAddress("0.0.0.0"));
  2652. dis->setHops(0);
  2653. dis->setCiaddr(IOAddress("192.0.5.1"));
  2654. EXPECT_FALSE(srv_.selectSubnet(dis));
  2655. }
  2656. // Checks if relay IP address specified in the relay-info structure can be
  2657. // used together with client-classification.
  2658. TEST_F(Dhcpv4SrvTest, relayOverrideAndClientClass) {
  2659. // This test configures 2 subnets. They both are on the same link, so they
  2660. // have the same relay-ip address. Furthermore, the first subnet is
  2661. // reserved for clients that belong to class "foo".
  2662. string config = "{ \"interfaces-config\": {"
  2663. " \"interfaces\": [ \"*\" ]"
  2664. "},"
  2665. "\"rebind-timer\": 2000, "
  2666. "\"renew-timer\": 1000, "
  2667. "\"subnet4\": [ "
  2668. "{ \"pools\": [ { \"pool\": \"192.0.2.2 - 192.0.2.100\" } ],"
  2669. " \"client-class\": \"foo\", "
  2670. " \"relay\": { "
  2671. " \"ip-address\": \"192.0.5.1\""
  2672. " },"
  2673. " \"subnet\": \"192.0.2.0/24\" }, "
  2674. "{ \"pools\": [ { \"pool\": \"192.0.3.1 - 192.0.3.100\" } ],"
  2675. " \"relay\": { "
  2676. " \"ip-address\": \"192.0.5.1\""
  2677. " },"
  2678. " \"subnet\": \"192.0.3.0/24\" } "
  2679. "],"
  2680. "\"valid-lifetime\": 4000 }";
  2681. // Use this config to set up the server
  2682. ASSERT_NO_THROW(configure(config));
  2683. const Subnet4Collection* subnets =
  2684. CfgMgr::instance().getCurrentCfg()->getCfgSubnets4()->getAll();
  2685. ASSERT_EQ(2, subnets->size());
  2686. // Let's get them for easy reference
  2687. Subnet4Ptr subnet1 = (*subnets)[0];
  2688. Subnet4Ptr subnet2 = (*subnets)[1];
  2689. ASSERT_TRUE(subnet1);
  2690. ASSERT_TRUE(subnet2);
  2691. // Let's create a packet.
  2692. Pkt4Ptr dis = Pkt4Ptr(new Pkt4(DHCPDISCOVER, 1234));
  2693. dis->setRemoteAddr(IOAddress("192.0.2.1"));
  2694. dis->setIface("eth0");
  2695. dis->setHops(1);
  2696. dis->setGiaddr(IOAddress("192.0.5.1"));
  2697. OptionPtr clientid = generateClientId();
  2698. dis->addOption(clientid);
  2699. // This packet does not belong to class foo, so it should be rejected in
  2700. // subnet[0], even though the relay-ip matches. It should be accepted in
  2701. // subnet[1], because the subnet matches and there are no class
  2702. // requirements.
  2703. EXPECT_TRUE(subnet2 == srv_.selectSubnet(dis));
  2704. // Now let's add this packet to class foo and recheck. This time it should
  2705. // be accepted in the first subnet, because both class and relay-ip match.
  2706. dis->addClass("foo");
  2707. EXPECT_TRUE(subnet1 == srv_.selectSubnet(dis));
  2708. }
  2709. // Checks if a RAI link selection sub-option works as expected
  2710. TEST_F(Dhcpv4SrvTest, relayLinkSelect) {
  2711. // We have 3 subnets defined.
  2712. string config = "{ \"interfaces-config\": {"
  2713. " \"interfaces\": [ \"*\" ]"
  2714. "},"
  2715. "\"rebind-timer\": 2000, "
  2716. "\"renew-timer\": 1000, "
  2717. "\"subnet4\": [ "
  2718. "{ \"pools\": [ { \"pool\": \"192.0.2.2 - 192.0.2.100\" } ],"
  2719. " \"relay\": { "
  2720. " \"ip-address\": \"192.0.5.1\""
  2721. " },"
  2722. " \"subnet\": \"192.0.2.0/24\" }, "
  2723. "{ \"pools\": [ { \"pool\": \"192.0.3.1 - 192.0.3.100\" } ],"
  2724. " \"subnet\": \"192.0.3.0/24\" }, "
  2725. "{ \"pools\": [ { \"pool\": \"192.0.4.1 - 192.0.4.100\" } ],"
  2726. " \"client-class\": \"foo\", "
  2727. " \"subnet\": \"192.0.4.0/24\" } "
  2728. "],"
  2729. "\"valid-lifetime\": 4000 }";
  2730. // Use this config to set up the server
  2731. ASSERT_NO_THROW(configure(config));
  2732. // Let's get the subnet configuration objects
  2733. const Subnet4Collection* subnets =
  2734. CfgMgr::instance().getCurrentCfg()->getCfgSubnets4()->getAll();
  2735. ASSERT_EQ(3, subnets->size());
  2736. // Let's get them for easy reference
  2737. Subnet4Ptr subnet1 = (*subnets)[0];
  2738. Subnet4Ptr subnet2 = (*subnets)[1];
  2739. Subnet4Ptr subnet3 = (*subnets)[2];
  2740. ASSERT_TRUE(subnet1);
  2741. ASSERT_TRUE(subnet2);
  2742. ASSERT_TRUE(subnet3);
  2743. // Let's create a packet.
  2744. Pkt4Ptr dis = Pkt4Ptr(new Pkt4(DHCPDISCOVER, 1234));
  2745. dis->setRemoteAddr(IOAddress("192.0.2.1"));
  2746. dis->setIface("eth0");
  2747. dis->setHops(1);
  2748. OptionPtr clientid = generateClientId();
  2749. dis->addOption(clientid);
  2750. // Let's create a Relay Agent Information option
  2751. OptionDefinitionPtr rai_def = LibDHCP::getOptionDef(DHCP4_OPTION_SPACE,
  2752. DHO_DHCP_AGENT_OPTIONS);
  2753. ASSERT_TRUE(rai_def);
  2754. OptionCustomPtr rai(new OptionCustom(*rai_def, Option::V4));
  2755. ASSERT_TRUE(rai);
  2756. IOAddress addr("192.0.3.2");
  2757. OptionPtr ols(new Option(Option::V4,
  2758. RAI_OPTION_LINK_SELECTION,
  2759. addr.toBytes()));
  2760. ASSERT_TRUE(ols);
  2761. rai->addOption(ols);
  2762. // This is just a sanity check, we're using regular method: ciaddr 192.0.3.1
  2763. // belongs to the second subnet, so it is selected
  2764. dis->setGiaddr(IOAddress("192.0.3.1"));
  2765. EXPECT_TRUE(subnet2 == srv_.selectSubnet(dis));
  2766. // Setup a relay override for the first subnet as it has a high precedence
  2767. dis->setGiaddr(IOAddress("192.0.5.1"));
  2768. EXPECT_TRUE(subnet1 == srv_.selectSubnet(dis));
  2769. // Put a RAI to select back the second subnet as it has
  2770. // the highest precedence
  2771. dis->addOption(rai);
  2772. EXPECT_TRUE(subnet2 == srv_.selectSubnet(dis));
  2773. // Subnet select option has a lower precedence
  2774. OptionDefinitionPtr sbnsel_def = LibDHCP::getOptionDef(DHCP4_OPTION_SPACE,
  2775. DHO_SUBNET_SELECTION);
  2776. ASSERT_TRUE(sbnsel_def);
  2777. OptionCustomPtr sbnsel(new OptionCustom(*sbnsel_def, Option::V4));
  2778. ASSERT_TRUE(sbnsel);
  2779. sbnsel->writeAddress(IOAddress("192.0.2.3"));
  2780. dis->addOption(sbnsel);
  2781. EXPECT_TRUE(subnet2 == srv_.selectSubnet(dis));
  2782. dis->delOption(DHO_SUBNET_SELECTION);
  2783. // Check client-classification still applies
  2784. IOAddress addr_foo("192.0.4.2");
  2785. ols.reset(new Option(Option::V4, RAI_OPTION_LINK_SELECTION,
  2786. addr_foo.toBytes()));
  2787. rai->delOption(RAI_OPTION_LINK_SELECTION);
  2788. dis->delOption(DHO_DHCP_AGENT_OPTIONS);
  2789. rai->addOption(ols);
  2790. dis->addOption(rai);
  2791. // Note it shall fail (vs. try the next criterion).
  2792. EXPECT_FALSE(srv_.selectSubnet(dis));
  2793. // Add the packet to the class and check again: now it shall succeed
  2794. dis->addClass("foo");
  2795. EXPECT_TRUE(subnet3 == srv_.selectSubnet(dis));
  2796. // Check it fails with a bad address in the sub-option
  2797. IOAddress addr_bad("10.0.0.1");
  2798. ols.reset(new Option(Option::V4, RAI_OPTION_LINK_SELECTION,
  2799. addr_bad.toBytes()));
  2800. rai->delOption(RAI_OPTION_LINK_SELECTION);
  2801. dis->delOption(DHO_DHCP_AGENT_OPTIONS);
  2802. rai->addOption(ols);
  2803. dis->addOption(rai);
  2804. EXPECT_FALSE(srv_.selectSubnet(dis));
  2805. }
  2806. // Checks if a subnet selection option works as expected
  2807. TEST_F(Dhcpv4SrvTest, subnetSelect) {
  2808. // We have 3 subnets defined.
  2809. string config = "{ \"interfaces-config\": {"
  2810. " \"interfaces\": [ \"*\" ]"
  2811. "},"
  2812. "\"rebind-timer\": 2000, "
  2813. "\"renew-timer\": 1000, "
  2814. "\"subnet4\": [ "
  2815. "{ \"pools\": [ { \"pool\": \"192.0.2.2 - 192.0.2.100\" } ],"
  2816. " \"relay\": { "
  2817. " \"ip-address\": \"192.0.5.1\""
  2818. " },"
  2819. " \"subnet\": \"192.0.2.0/24\" }, "
  2820. "{ \"pools\": [ { \"pool\": \"192.0.3.1 - 192.0.3.100\" } ],"
  2821. " \"subnet\": \"192.0.3.0/24\" }, "
  2822. "{ \"pools\": [ { \"pool\": \"192.0.4.1 - 192.0.4.100\" } ],"
  2823. " \"client-class\": \"foo\", "
  2824. " \"subnet\": \"192.0.4.0/24\" } "
  2825. "],"
  2826. "\"valid-lifetime\": 4000 }";
  2827. // Use this config to set up the server
  2828. ASSERT_NO_THROW(configure(config));
  2829. // Let's get the subnet configuration objects
  2830. const Subnet4Collection* subnets =
  2831. CfgMgr::instance().getCurrentCfg()->getCfgSubnets4()->getAll();
  2832. ASSERT_EQ(3, subnets->size());
  2833. // Let's get them for easy reference
  2834. Subnet4Ptr subnet1 = (*subnets)[0];
  2835. Subnet4Ptr subnet2 = (*subnets)[1];
  2836. Subnet4Ptr subnet3 = (*subnets)[2];
  2837. ASSERT_TRUE(subnet1);
  2838. ASSERT_TRUE(subnet2);
  2839. ASSERT_TRUE(subnet3);
  2840. // Let's create a packet.
  2841. Pkt4Ptr dis = Pkt4Ptr(new Pkt4(DHCPDISCOVER, 1234));
  2842. dis->setRemoteAddr(IOAddress("192.0.2.1"));
  2843. dis->setIface("eth0");
  2844. dis->setHops(1);
  2845. OptionPtr clientid = generateClientId();
  2846. dis->addOption(clientid);
  2847. // Let's create a Subnet Selection option
  2848. OptionDefinitionPtr sbnsel_def = LibDHCP::getOptionDef(DHCP4_OPTION_SPACE,
  2849. DHO_SUBNET_SELECTION);
  2850. ASSERT_TRUE(sbnsel_def);
  2851. OptionCustomPtr sbnsel(new OptionCustom(*sbnsel_def, Option::V4));
  2852. ASSERT_TRUE(sbnsel);
  2853. sbnsel->writeAddress(IOAddress("192.0.3.2"));
  2854. // This is just a sanity check, we're using regular method: ciaddr 192.0.3.1
  2855. // belongs to the second subnet, so it is selected
  2856. dis->setGiaddr(IOAddress("192.0.3.1"));
  2857. EXPECT_TRUE(subnet2 == srv_.selectSubnet(dis));
  2858. // Setup a relay override for the first subnet as it has a high precedence
  2859. dis->setGiaddr(IOAddress("192.0.5.1"));
  2860. EXPECT_TRUE(subnet1 == srv_.selectSubnet(dis));
  2861. // Put a subnet select option to select back the second subnet as
  2862. // it has the second highest precedence
  2863. dis->addOption(sbnsel);
  2864. EXPECT_TRUE(subnet2 == srv_.selectSubnet(dis));
  2865. // Check client-classification still applies
  2866. sbnsel->writeAddress(IOAddress("192.0.4.2"));
  2867. // Note it shall fail (vs. try the next criterion).
  2868. EXPECT_FALSE(srv_.selectSubnet(dis));
  2869. // Add the packet to the class and check again: now it shall succeed
  2870. dis->addClass("foo");
  2871. EXPECT_TRUE(subnet3 == srv_.selectSubnet(dis));
  2872. // Check it fails with a bad address in the sub-option
  2873. sbnsel->writeAddress(IOAddress("10.0.0.1"));
  2874. EXPECT_FALSE(srv_.selectSubnet(dis));
  2875. }
  2876. // This test verifies that the direct message is dropped when it has been
  2877. // received by the server via an interface for which there is no subnet
  2878. // configured. It also checks that the message is not dropped (is processed)
  2879. // when it is relayed or unicast.
  2880. TEST_F(Dhcpv4SrvTest, acceptDirectRequest) {
  2881. IfaceMgrTestConfig test_config(true);
  2882. IfaceMgr::instance().openSockets4();
  2883. NakedDhcpv4Srv srv(0);
  2884. Pkt4Ptr pkt(new Pkt4(DHCPDISCOVER, 1234));
  2885. // Set Giaddr and local server's unicast address, but don't set hops.
  2886. // Hops value should not matter. The server will treat the message
  2887. // with the hops value of 0 and non-zero giaddr as relayed.
  2888. pkt->setGiaddr(IOAddress("192.0.10.1"));
  2889. pkt->setRemoteAddr(IOAddress("0.0.0.0"));
  2890. pkt->setLocalAddr(IOAddress("192.0.2.3"));
  2891. pkt->setIface("eth1");
  2892. EXPECT_TRUE(srv.accept(pkt));
  2893. // Let's set hops and check that the message is still accepted as
  2894. // a relayed message.
  2895. pkt->setHops(1);
  2896. EXPECT_TRUE(srv.accept(pkt));
  2897. // Make it a direct message but keep unicast server's address. The
  2898. // messages sent to unicast address should be accepted as they are
  2899. // most likely to renew existing leases. The server should respond
  2900. // to renews so they have to be accepted and processed.
  2901. pkt->setHops(0);
  2902. pkt->setGiaddr(IOAddress("0.0.0.0"));
  2903. EXPECT_TRUE(srv.accept(pkt));
  2904. // Direct message is now sent to a broadcast address. The server
  2905. // should accept this message because it has been received via
  2906. // eth1 for which there is a subnet configured (see test fixture
  2907. // class constructor).
  2908. pkt->setLocalAddr(IOAddress("255.255.255.255"));
  2909. EXPECT_TRUE(srv.accept(pkt));
  2910. // For eth0, there is no subnet configured. Such message is expected
  2911. // to be silently dropped.
  2912. pkt->setIface("eth0");
  2913. EXPECT_FALSE(srv.accept(pkt));
  2914. // But, if the message is unicast it should be accepted, even though
  2915. // it has been received via eth0.
  2916. pkt->setLocalAddr(IOAddress("10.0.0.1"));
  2917. EXPECT_TRUE(srv.accept(pkt));
  2918. // For the DHCPINFORM the ciaddr should be set or at least the source
  2919. // address.
  2920. pkt->setType(DHCPINFORM);
  2921. pkt->setRemoteAddr(IOAddress("10.0.0.101"));
  2922. EXPECT_TRUE(srv.accept(pkt));
  2923. // When neither ciaddr nor source address is present, the packet should
  2924. // be dropped.
  2925. pkt->setRemoteAddr(IOAddress("0.0.0.0"));
  2926. EXPECT_FALSE(srv.accept(pkt));
  2927. // When ciaddr is set, the packet should be accepted.
  2928. pkt->setCiaddr(IOAddress("10.0.0.1"));
  2929. EXPECT_TRUE(srv.accept(pkt));
  2930. }
  2931. // This test checks that the server rejects a message with invalid type.
  2932. TEST_F(Dhcpv4SrvTest, acceptMessageType) {
  2933. IfaceMgrTestConfig test_config(true);
  2934. IfaceMgr::instance().openSockets4();
  2935. NakedDhcpv4Srv srv(0);
  2936. // Specify messages to be accepted by the server.
  2937. int allowed[] = {
  2938. DHCPDISCOVER,
  2939. DHCPREQUEST,
  2940. DHCPRELEASE,
  2941. DHCPDECLINE,
  2942. DHCPINFORM
  2943. };
  2944. size_t allowed_size = sizeof(allowed) / sizeof(allowed[0]);
  2945. // Check that the server actually accepts these message types.
  2946. for (size_t i = 0; i < allowed_size; ++i) {
  2947. EXPECT_TRUE(srv.acceptMessageType(Pkt4Ptr(new Pkt4(allowed[i], 1234))))
  2948. << "Test failed for message type " << i;
  2949. }
  2950. // Specify messages which server is supposed to drop.
  2951. int not_allowed[] = {
  2952. DHCPOFFER,
  2953. DHCPACK,
  2954. DHCPNAK,
  2955. DHCPLEASEQUERY,
  2956. DHCPLEASEUNASSIGNED,
  2957. DHCPLEASEUNKNOWN,
  2958. DHCPLEASEACTIVE,
  2959. DHCPBULKLEASEQUERY,
  2960. DHCPLEASEQUERYDONE
  2961. };
  2962. size_t not_allowed_size = sizeof(not_allowed) / sizeof(not_allowed[0]);
  2963. // Actually check that the server will drop these messages.
  2964. for (size_t i = 0; i < not_allowed_size; ++i) {
  2965. EXPECT_FALSE(srv.acceptMessageType(Pkt4Ptr(new Pkt4(not_allowed[i],
  2966. 1234))))
  2967. << "Test failed for message type " << i;
  2968. }
  2969. }
  2970. // Test checks whether statistic is bumped up appropriately when Decline
  2971. // message is received.
  2972. TEST_F(Dhcpv4SrvTest, statisticsDecline) {
  2973. NakedDhcpv4Srv srv(0);
  2974. pretendReceivingPkt(srv, CONFIGS[0], DHCPDECLINE, "pkt4-decline-received");
  2975. }
  2976. // Test checks whether statistic is bumped up appropriately when Offer
  2977. // message is received (this should never happen in a sane network).
  2978. TEST_F(Dhcpv4SrvTest, statisticsOfferRcvd) {
  2979. NakedDhcpv4Srv srv(0);
  2980. pretendReceivingPkt(srv, CONFIGS[0], DHCPOFFER, "pkt4-offer-received");
  2981. }
  2982. // Test checks whether statistic is bumped up appropriately when Ack
  2983. // message is received (this should never happen in a sane network).
  2984. TEST_F(Dhcpv4SrvTest, statisticsAckRcvd) {
  2985. NakedDhcpv4Srv srv(0);
  2986. pretendReceivingPkt(srv, CONFIGS[0], DHCPACK, "pkt4-ack-received");
  2987. }
  2988. // Test checks whether statistic is bumped up appropriately when Nak
  2989. // message is received (this should never happen in a sane network).
  2990. TEST_F(Dhcpv4SrvTest, statisticsNakRcvd) {
  2991. NakedDhcpv4Srv srv(0);
  2992. pretendReceivingPkt(srv, CONFIGS[0], DHCPNAK, "pkt4-nak-received");
  2993. }
  2994. // Test checks whether statistic is bumped up appropriately when Release
  2995. // message is received.
  2996. TEST_F(Dhcpv4SrvTest, statisticsReleaseRcvd) {
  2997. NakedDhcpv4Srv srv(0);
  2998. pretendReceivingPkt(srv, CONFIGS[0], DHCPRELEASE, "pkt4-release-received");
  2999. }
  3000. // Test checks whether statistic is bumped up appropriately when unknown
  3001. // message is received.
  3002. TEST_F(Dhcpv4SrvTest, statisticsUnknownRcvd) {
  3003. NakedDhcpv4Srv srv(0);
  3004. pretendReceivingPkt(srv, CONFIGS[0], 200, "pkt4-unknown-received");
  3005. // There should also be pkt4-receive-drop stat bumped up
  3006. using namespace isc::stats;
  3007. StatsMgr& mgr = StatsMgr::instance();
  3008. ObservationPtr drop_stat = mgr.getObservation("pkt4-receive-drop");
  3009. // This statistic must be present and must be set to 1.
  3010. ASSERT_TRUE(drop_stat);
  3011. EXPECT_EQ(1, drop_stat->getInteger().first);
  3012. }
  3013. // This test verifies that the server is able to handle an empty client-id
  3014. // in incoming client message.
  3015. TEST_F(Dhcpv4SrvTest, emptyClientId) {
  3016. IfaceMgrTestConfig test_config(true);
  3017. IfaceMgr::instance().openSockets4();
  3018. Dhcp4Client client;
  3019. EXPECT_NO_THROW(configure(CONFIGS[0], *client.getServer()));
  3020. // Tell the client to not send client-id on its own.
  3021. client.includeClientId("");
  3022. // Instead, tell him to send this extra option, which happens to be
  3023. // an empty client-id.
  3024. OptionPtr empty_client_id(new Option(Option::V4, DHO_DHCP_CLIENT_IDENTIFIER));
  3025. client.addExtraOption(empty_client_id);
  3026. // Let's check whether the server is able to process this packet without
  3027. // throwing any exceptions. We don't care whether the server sent any
  3028. // responses or not. The goal is to check that the server didn't throw
  3029. // any exceptions.
  3030. EXPECT_NO_THROW(client.doDORA());
  3031. }
  3032. // This test verifies that the server is able to handle too long client-id
  3033. // in incoming client message.
  3034. TEST_F(Dhcpv4SrvTest, tooLongClientId) {
  3035. IfaceMgrTestConfig test_config(true);
  3036. IfaceMgr::instance().openSockets4();
  3037. Dhcp4Client client;
  3038. EXPECT_NO_THROW(configure(CONFIGS[0], *client.getServer()));
  3039. // Tell the client to not send client-id on its own.
  3040. client.includeClientId("");
  3041. // Instead, tell him to send this extra option, which happens to be
  3042. // an empty client-id.
  3043. std::vector<uint8_t> data(250, 250);
  3044. OptionPtr long_client_id(new Option(Option::V4, DHO_DHCP_CLIENT_IDENTIFIER,
  3045. data));
  3046. client.addExtraOption(long_client_id);
  3047. // Let's check whether the server is able to process this packet without
  3048. // throwing any exceptions. We don't care whether the server sent any
  3049. // responses or not. The goal is to check that the server didn't throw
  3050. // any exceptions.
  3051. EXPECT_NO_THROW(client.doDORA());
  3052. }
  3053. // Checks if user-contexts are parsed properly.
  3054. TEST_F(Dhcpv4SrvTest, userContext) {
  3055. IfaceMgrTestConfig test_config(true);
  3056. NakedDhcpv4Srv srv(0);
  3057. // This config has one subnet with user-context with one
  3058. // pool (also with context). Make sure the configuration could be accepted.
  3059. cout << CONFIGS[3] << endl;
  3060. EXPECT_NO_THROW(configure(CONFIGS[3]));
  3061. // Now make sure the data was not lost.
  3062. ConstSrvConfigPtr cfg = CfgMgr::instance().getCurrentCfg();
  3063. const Subnet4Collection* subnets = cfg->getCfgSubnets4()->getAll();
  3064. ASSERT_TRUE(subnets);
  3065. ASSERT_EQ(1, subnets->size());
  3066. // Let's get the subnet and check its context.
  3067. Subnet4Ptr subnet1 = (*subnets)[0];
  3068. ASSERT_TRUE(subnet1);
  3069. ASSERT_TRUE(subnet1->getContext());
  3070. EXPECT_EQ("{ \"secure\": false }", subnet1->getContext()->str());
  3071. // Ok, not get the sole pool in it and check its context, too.
  3072. PoolCollection pools = subnet1->getPools(Lease::TYPE_V4);
  3073. ASSERT_EQ(1, pools.size());
  3074. ASSERT_TRUE(pools[0]);
  3075. ASSERT_TRUE(pools[0]->getContext());
  3076. EXPECT_EQ("{ \"value\": 42 }", pools[0]->getContext()->str());
  3077. }
  3078. /// @todo: Implement proper tests for MySQL lease/host database,
  3079. /// see ticket #4214.
  3080. }; // end of anonymous namespace