dhcp4_srv_unittest.cc 136 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772
  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. ASSERT_NO_THROW(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 (no failure)
  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. // Checks effect of raw not compatible option 43 (failure)
  1958. TEST_F(Dhcpv4SrvTest, option43FailRaw) {
  1959. IfaceMgrTestConfig test_config(true);
  1960. IfaceMgr::instance().openSockets4();
  1961. NakedDhcpv4Srv srv(0);
  1962. // The vendor-encapsulated-options has an incompatible data
  1963. // so won't have the expected content.
  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\": 1, "
  1974. " \"name\": \"foo\", "
  1975. " \"space\": \"vendor-encapsulated-options-space\", "
  1976. " \"type\": \"uint32\" } ],"
  1977. "\"option-data\": [ "
  1978. "{ \"name\": \"vendor-class-identifier\", "
  1979. " \"data\": \"bar\" }, "
  1980. "{ \"name\": \"vendor-encapsulated-options\", "
  1981. " \"csv-format\": false, "
  1982. " \"data\": \"0102\" } ] }";
  1983. ConstElementPtr json;
  1984. ASSERT_NO_THROW(json = parseDHCP4(config));
  1985. ConstElementPtr status;
  1986. // Configure the server and make sure the config is accepted
  1987. EXPECT_NO_THROW(status = configureDhcp4Server(srv, json));
  1988. ASSERT_TRUE(status);
  1989. comment_ = config::parseAnswer(rcode_, status);
  1990. ASSERT_EQ(0, rcode_);
  1991. CfgMgr::instance().commit();
  1992. // Create a packet with enough to select the subnet and go through
  1993. // the DISCOVER processing
  1994. Pkt4Ptr query(new Pkt4(DHCPDISCOVER, 1234));
  1995. query->setRemoteAddr(IOAddress("192.0.2.1"));
  1996. OptionPtr clientid = generateClientId();
  1997. query->addOption(clientid);
  1998. query->setIface("eth1");
  1999. // Create and add a vendor-encapsulated-options (code 43)
  2000. // with not compatible (not parsable as suboptions) content
  2001. // which will raise an exception
  2002. OptionBuffer buf;
  2003. buf.push_back(0x01);
  2004. buf.push_back(0x01);
  2005. buf.push_back(0x01);
  2006. OptionPtr vopt(new Option(Option::V4, DHO_VENDOR_ENCAPSULATED_OPTIONS, buf));
  2007. query->addOption(vopt);
  2008. query->deferredOptions().push_back(DHO_VENDOR_ENCAPSULATED_OPTIONS);
  2009. // Create and add a PRL option to the query
  2010. OptionUint8ArrayPtr prl(new OptionUint8Array(Option::V4,
  2011. DHO_DHCP_PARAMETER_REQUEST_LIST));
  2012. ASSERT_TRUE(prl);
  2013. prl->addValue(DHO_VENDOR_ENCAPSULATED_OPTIONS);
  2014. prl->addValue(DHO_VENDOR_CLASS_IDENTIFIER);
  2015. query->addOption(prl);
  2016. srv.classifyPacket(query);
  2017. EXPECT_THROW(srv.deferredUnpack(query), InvalidOptionValue);
  2018. }
  2019. // Verifies raw option 43 can be handled (global)
  2020. TEST_F(Dhcpv4SrvTest, option43RawGlobal) {
  2021. IfaceMgrTestConfig test_config(true);
  2022. IfaceMgr::instance().openSockets4();
  2023. NakedDhcpv4Srv srv(0);
  2024. // The vendor-encapsulated-options is redefined as raw binary
  2025. // in a global definition.
  2026. string config = "{ \"interfaces-config\": {"
  2027. " \"interfaces\": [ \"*\" ] }, "
  2028. "\"rebind-timer\": 2000, "
  2029. "\"renew-timer\": 1000, "
  2030. "\"valid-lifetime\": 4000, "
  2031. "\"subnet4\": [ "
  2032. "{ \"pools\": [ { \"pool\": \"192.0.2.1 - 192.0.2.100\" } ], "
  2033. " \"subnet\": \"192.0.2.0/24\" } ],"
  2034. "\"option-def\": [ "
  2035. "{ \"code\": 43, "
  2036. " \"name\": \"vendor-encapsulated-options\", "
  2037. " \"type\": \"binary\" } ],"
  2038. "\"option-data\": [ "
  2039. "{ \"name\": \"vendor-class-identifier\", "
  2040. " \"data\": \"bar\" }, "
  2041. "{ \"name\": \"vendor-encapsulated-options\", "
  2042. " \"csv-format\": false, "
  2043. " \"data\": \"0102\" } ] }";
  2044. ConstElementPtr json;
  2045. ASSERT_NO_THROW(json = parseDHCP4(config));
  2046. ConstElementPtr status;
  2047. // Configure the server and make sure the config is accepted
  2048. EXPECT_NO_THROW(status = configureDhcp4Server(srv, json));
  2049. ASSERT_TRUE(status);
  2050. comment_ = config::parseAnswer(rcode_, status);
  2051. ASSERT_EQ(0, rcode_);
  2052. CfgMgr::instance().commit();
  2053. // Create a packet with enough to select the subnet and go through
  2054. // the DISCOVER processing
  2055. Pkt4Ptr query(new Pkt4(DHCPDISCOVER, 1234));
  2056. query->setRemoteAddr(IOAddress("192.0.2.1"));
  2057. OptionPtr clientid = generateClientId();
  2058. query->addOption(clientid);
  2059. query->setIface("eth1");
  2060. // Create and add a vendor-encapsulated-options (code 43)
  2061. // with not compatible (not parsable as suboptions) content
  2062. OptionBuffer buf;
  2063. buf.push_back(0x02);
  2064. buf.push_back(0x03);
  2065. OptionPtr vopt(new Option(Option::V4, DHO_VENDOR_ENCAPSULATED_OPTIONS, buf));
  2066. query->addOption(vopt);
  2067. query->deferredOptions().push_back(DHO_VENDOR_ENCAPSULATED_OPTIONS);
  2068. // Create and add a PRL option to the query
  2069. OptionUint8ArrayPtr prl(new OptionUint8Array(Option::V4,
  2070. DHO_DHCP_PARAMETER_REQUEST_LIST));
  2071. ASSERT_TRUE(prl);
  2072. prl->addValue(DHO_VENDOR_ENCAPSULATED_OPTIONS);
  2073. prl->addValue(DHO_VENDOR_CLASS_IDENTIFIER);
  2074. query->addOption(prl);
  2075. srv.classifyPacket(query);
  2076. ASSERT_NO_THROW(srv.deferredUnpack(query));
  2077. // Check if the option was (correctly) re-unpacked
  2078. vopt = query->getOption(DHO_VENDOR_ENCAPSULATED_OPTIONS);
  2079. OptionCustomPtr custom = boost::dynamic_pointer_cast<OptionCustom>(vopt);
  2080. EXPECT_FALSE(custom);
  2081. // Pass it to the server and get an offer
  2082. Pkt4Ptr offer = srv.processDiscover(query);
  2083. // Check if we get response at all
  2084. checkResponse(offer, DHCPOFFER, 1234);
  2085. // Processing should add a vendor-class-identifier (code 60)
  2086. OptionPtr opt = offer->getOption(DHO_VENDOR_CLASS_IDENTIFIER);
  2087. EXPECT_TRUE(opt);
  2088. // And a vendor-encapsulated-options (code 43)
  2089. opt = offer->getOption(DHO_VENDOR_ENCAPSULATED_OPTIONS);
  2090. ASSERT_TRUE(opt);
  2091. // Verifies the content
  2092. ASSERT_EQ(2, opt->len() - opt->getHeaderLen());
  2093. EXPECT_EQ(0x01, opt->getData()[0]);
  2094. EXPECT_EQ(0x02, opt->getData()[1]);
  2095. }
  2096. // Verifies raw option 43 can be handled (catch-all class)
  2097. TEST_F(Dhcpv4SrvTest, option43RawClass) {
  2098. IfaceMgrTestConfig test_config(true);
  2099. IfaceMgr::instance().openSockets4();
  2100. NakedDhcpv4Srv srv(0);
  2101. // The vendor-encapsulated-options is redefined as raw binary
  2102. // in a class definition.
  2103. string config = "{ \"interfaces-config\": {"
  2104. " \"interfaces\": [ \"*\" ] }, "
  2105. "\"rebind-timer\": 2000, "
  2106. "\"renew-timer\": 1000, "
  2107. "\"valid-lifetime\": 4000, "
  2108. "\"subnet4\": [ "
  2109. "{ \"pools\": [ { \"pool\": \"192.0.2.1 - 192.0.2.100\" } ], "
  2110. " \"subnet\": \"192.0.2.0/24\" } ],"
  2111. "\"client-classes\": [ "
  2112. "{ \"name\": \"vendor\", "
  2113. " \"test\": \"option[vendor-encapsulated-options].exists\", "
  2114. " \"option-def\": [ "
  2115. " { \"code\": 43, "
  2116. " \"name\": \"vendor-encapsulated-options\", "
  2117. " \"type\": \"binary\" } ],"
  2118. " \"option-data\": [ "
  2119. " { \"name\": \"vendor-class-identifier\", "
  2120. " \"data\": \"bar\" }, "
  2121. " { \"name\": \"vendor-encapsulated-options\", "
  2122. " \"csv-format\": false, "
  2123. " \"data\": \"0102\" } ] } ] }";
  2124. ConstElementPtr json;
  2125. ASSERT_NO_THROW(json = parseDHCP4(config));
  2126. ConstElementPtr status;
  2127. // Configure the server and make sure the config is accepted
  2128. EXPECT_NO_THROW(status = configureDhcp4Server(srv, json));
  2129. ASSERT_TRUE(status);
  2130. comment_ = config::parseAnswer(rcode_, status);
  2131. ASSERT_EQ(0, rcode_);
  2132. CfgMgr::instance().commit();
  2133. // Create a packet with enough to select the subnet and go through
  2134. // the DISCOVER processing
  2135. Pkt4Ptr query(new Pkt4(DHCPDISCOVER, 1234));
  2136. query->setRemoteAddr(IOAddress("192.0.2.1"));
  2137. OptionPtr clientid = generateClientId();
  2138. query->addOption(clientid);
  2139. query->setIface("eth1");
  2140. // Create and add a vendor-encapsulated-options (code 43)
  2141. // with not compatible (not parsable as suboptions) content
  2142. OptionBuffer buf;
  2143. buf.push_back(0x02);
  2144. buf.push_back(0x03);
  2145. OptionPtr vopt(new Option(Option::V4, DHO_VENDOR_ENCAPSULATED_OPTIONS, buf));
  2146. query->addOption(vopt);
  2147. query->deferredOptions().push_back(DHO_VENDOR_ENCAPSULATED_OPTIONS);
  2148. // Create and add a PRL option to the query
  2149. OptionUint8ArrayPtr prl(new OptionUint8Array(Option::V4,
  2150. DHO_DHCP_PARAMETER_REQUEST_LIST));
  2151. ASSERT_TRUE(prl);
  2152. prl->addValue(DHO_VENDOR_ENCAPSULATED_OPTIONS);
  2153. prl->addValue(DHO_VENDOR_CLASS_IDENTIFIER);
  2154. query->addOption(prl);
  2155. srv.classifyPacket(query);
  2156. ASSERT_NO_THROW(srv.deferredUnpack(query));
  2157. // Check if the option was (correctly) re-unpacked
  2158. vopt = query->getOption(DHO_VENDOR_ENCAPSULATED_OPTIONS);
  2159. OptionCustomPtr custom = boost::dynamic_pointer_cast<OptionCustom>(vopt);
  2160. EXPECT_FALSE(custom);
  2161. // Pass it to the server and get an offer
  2162. Pkt4Ptr offer = srv.processDiscover(query);
  2163. // Check if we get response at all
  2164. checkResponse(offer, DHCPOFFER, 1234);
  2165. // Processing should add a vendor-class-identifier (code 60)
  2166. OptionPtr opt = offer->getOption(DHO_VENDOR_CLASS_IDENTIFIER);
  2167. EXPECT_TRUE(opt);
  2168. // And a vendor-encapsulated-options (code 43)
  2169. opt = offer->getOption(DHO_VENDOR_ENCAPSULATED_OPTIONS);
  2170. ASSERT_TRUE(opt);
  2171. // Verifies the content
  2172. ASSERT_EQ(2, opt->len() - opt->getHeaderLen());
  2173. EXPECT_EQ(0x01, opt->getData()[0]);
  2174. EXPECT_EQ(0x02, opt->getData()[1]);
  2175. }
  2176. // Verifies option 43 deferred processing (one class)
  2177. TEST_F(Dhcpv4SrvTest, option43Class) {
  2178. IfaceMgrTestConfig test_config(true);
  2179. IfaceMgr::instance().openSockets4();
  2180. NakedDhcpv4Srv srv(0);
  2181. // A client class defines vendor-encapsulated-options (code 43)
  2182. // and data for it and its sub-option.
  2183. string config = "{ \"interfaces-config\": {"
  2184. " \"interfaces\": [ \"*\" ] }, "
  2185. "\"rebind-timer\": 2000, "
  2186. "\"renew-timer\": 1000, "
  2187. "\"valid-lifetime\": 4000, "
  2188. "\"option-def\": [ "
  2189. "{ \"code\": 1, "
  2190. " \"name\": \"foo\", "
  2191. " \"space\": \"alpha\", "
  2192. " \"type\": \"uint32\" } ],"
  2193. "\"subnet4\": [ "
  2194. "{ \"pools\": [ { \"pool\": \"192.0.2.1 - 192.0.2.100\" } ], "
  2195. " \"subnet\": \"192.0.2.0/24\" } ],"
  2196. "\"client-classes\": [ "
  2197. "{ \"name\": \"alpha\", "
  2198. " \"test\": \"option[vendor-class-identifier].text == 'alpha'\", "
  2199. " \"option-def\": [ "
  2200. " { \"code\": 43, "
  2201. " \"name\": \"vendor-encapsulated-options\", "
  2202. " \"type\": \"empty\", "
  2203. " \"encapsulate\": \"alpha\" } ],"
  2204. " \"option-data\": [ "
  2205. " { \"name\": \"vendor-class-identifier\", "
  2206. " \"data\": \"alpha\" }, "
  2207. " { \"name\": \"vendor-encapsulated-options\" }, "
  2208. " { \"name\": \"foo\", "
  2209. " \"space\": \"alpha\", "
  2210. " \"data\": \"12345678\" } ] } ] }";
  2211. ConstElementPtr json;
  2212. ASSERT_NO_THROW(json = parseDHCP4(config));
  2213. ConstElementPtr status;
  2214. // Configure the server and make sure the config is accepted
  2215. EXPECT_NO_THROW(status = configureDhcp4Server(srv, json));
  2216. ASSERT_TRUE(status);
  2217. comment_ = config::parseAnswer(rcode_, status);
  2218. ASSERT_EQ(0, rcode_);
  2219. CfgMgr::instance().commit();
  2220. // Create a packet with enough to select the subnet and go through
  2221. // the DISCOVER processing
  2222. Pkt4Ptr query(new Pkt4(DHCPDISCOVER, 1234));
  2223. query->setRemoteAddr(IOAddress("192.0.2.1"));
  2224. OptionPtr clientid = generateClientId();
  2225. query->addOption(clientid);
  2226. query->setIface("eth1");
  2227. // Create and add a vendor-encapsulated-options (code 43)
  2228. OptionBuffer buf;
  2229. buf.push_back(0x01);
  2230. buf.push_back(0x04);
  2231. buf.push_back(0x87);
  2232. buf.push_back(0x65);
  2233. buf.push_back(0x43);
  2234. buf.push_back(0x21);
  2235. OptionPtr vopt(new Option(Option::V4, DHO_VENDOR_ENCAPSULATED_OPTIONS, buf));
  2236. query->addOption(vopt);
  2237. query->deferredOptions().push_back(DHO_VENDOR_ENCAPSULATED_OPTIONS);
  2238. // Create and add a vendor-class-identifier (code 60)
  2239. OptionStringPtr iopt(new OptionString(Option::V4,
  2240. DHO_VENDOR_CLASS_IDENTIFIER,
  2241. "alpha"));
  2242. query->addOption(iopt);
  2243. // Create and add a PRL option to the query
  2244. OptionUint8ArrayPtr prl(new OptionUint8Array(Option::V4,
  2245. DHO_DHCP_PARAMETER_REQUEST_LIST));
  2246. ASSERT_TRUE(prl);
  2247. prl->addValue(DHO_VENDOR_ENCAPSULATED_OPTIONS);
  2248. prl->addValue(DHO_VENDOR_CLASS_IDENTIFIER);
  2249. query->addOption(prl);
  2250. srv.classifyPacket(query);
  2251. ASSERT_NO_THROW(srv.deferredUnpack(query));
  2252. // Check if the option was (correctly) re-unpacked
  2253. vopt = query->getOption(DHO_VENDOR_ENCAPSULATED_OPTIONS);
  2254. OptionCustomPtr custom = boost::dynamic_pointer_cast<OptionCustom>(vopt);
  2255. EXPECT_TRUE(custom);
  2256. EXPECT_EQ(1, vopt->getOptions().size());
  2257. // Pass it to the server and get an offer
  2258. Pkt4Ptr offer = srv.processDiscover(query);
  2259. // Check if we get response at all
  2260. checkResponse(offer, DHCPOFFER, 1234);
  2261. // Processing should add a vendor-class-identifier (code 60)
  2262. OptionPtr opt = offer->getOption(DHO_VENDOR_CLASS_IDENTIFIER);
  2263. EXPECT_TRUE(opt);
  2264. // And a vendor-encapsulated-options (code 43)
  2265. opt = offer->getOption(DHO_VENDOR_ENCAPSULATED_OPTIONS);
  2266. ASSERT_TRUE(opt);
  2267. // Verifies the content
  2268. const OptionCollection& opts = opt->getOptions();
  2269. ASSERT_EQ(1, opts.size());
  2270. OptionPtr sopt = opts.begin()->second;
  2271. ASSERT_TRUE(sopt);
  2272. EXPECT_EQ(1, sopt->getType());
  2273. OptionUint32Ptr sopt32 = boost::dynamic_pointer_cast<OptionUint32>(sopt);
  2274. ASSERT_TRUE(sopt32);
  2275. EXPECT_EQ(12345678, sopt32->getValue());
  2276. }
  2277. // Verifies option 43 priority
  2278. TEST_F(Dhcpv4SrvTest, option43ClassPriority) {
  2279. IfaceMgrTestConfig test_config(true);
  2280. IfaceMgr::instance().openSockets4();
  2281. NakedDhcpv4Srv srv(0);
  2282. // Both global and client-class scopes get vendor-encapsulated-options
  2283. // (code 43) definition and data. The client-class has precedence.
  2284. // Note it does not work without the vendor-encapsulated-options
  2285. // option-data in the client-class.
  2286. string config = "{ \"interfaces-config\": {"
  2287. " \"interfaces\": [ \"*\" ] }, "
  2288. "\"rebind-timer\": 2000, "
  2289. "\"renew-timer\": 1000, "
  2290. "\"valid-lifetime\": 4000, "
  2291. "\"option-def\": [ "
  2292. "{ \"code\": 1, "
  2293. " \"name\": \"foo\", "
  2294. " \"space\": \"alpha\", "
  2295. " \"type\": \"uint32\" },"
  2296. "{ \"code\": 1, "
  2297. " \"name\": \"bar\", "
  2298. " \"space\": \"beta\", "
  2299. " \"type\": \"uint8\" }, "
  2300. "{ \"code\": 43, "
  2301. " \"name\": \"vendor-encapsulated-options\", "
  2302. " \"type\": \"empty\", "
  2303. " \"encapsulate\": \"beta\" } ],"
  2304. "\"option-data\": [ "
  2305. "{ \"name\": \"vendor-encapsulated-options\" }, "
  2306. "{ \"name\": \"vendor-class-identifier\", "
  2307. " \"data\": \"beta\" }, "
  2308. "{ \"name\": \"bar\", "
  2309. " \"space\": \"beta\", "
  2310. " \"data\": \"33\" } ],"
  2311. "\"subnet4\": [ "
  2312. "{ \"pools\": [ { \"pool\": \"192.0.2.1 - 192.0.2.100\" } ], "
  2313. " \"subnet\": \"192.0.2.0/24\" } ],"
  2314. "\"client-classes\": [ "
  2315. "{ \"name\": \"alpha\", "
  2316. " \"test\": \"option[vendor-class-identifier].text == 'alpha'\", "
  2317. " \"option-def\": [ "
  2318. " { \"code\": 43, "
  2319. " \"name\": \"vendor-encapsulated-options\", "
  2320. " \"type\": \"empty\", "
  2321. " \"encapsulate\": \"alpha\" } ],"
  2322. " \"option-data\": [ "
  2323. "{ \"name\": \"vendor-encapsulated-options\" }, "
  2324. " { \"name\": \"vendor-class-identifier\", "
  2325. " \"data\": \"alpha\" }, "
  2326. " { \"name\": \"foo\", "
  2327. " \"space\": \"alpha\", "
  2328. " \"data\": \"12345678\" } ] } ] }";
  2329. ConstElementPtr json;
  2330. ASSERT_NO_THROW(json = parseDHCP4(config));
  2331. ConstElementPtr status;
  2332. // Configure the server and make sure the config is accepted
  2333. EXPECT_NO_THROW(status = configureDhcp4Server(srv, json));
  2334. ASSERT_TRUE(status);
  2335. comment_ = config::parseAnswer(rcode_, status);
  2336. ASSERT_EQ(0, rcode_);
  2337. CfgMgr::instance().commit();
  2338. // Create a packet with enough to select the subnet and go through
  2339. // the DISCOVER processing
  2340. Pkt4Ptr query(new Pkt4(DHCPDISCOVER, 1234));
  2341. query->setRemoteAddr(IOAddress("192.0.2.1"));
  2342. OptionPtr clientid = generateClientId();
  2343. query->addOption(clientid);
  2344. query->setIface("eth1");
  2345. // Create and add a vendor-encapsulated-options (code 43)
  2346. OptionBuffer buf;
  2347. buf.push_back(0x01);
  2348. buf.push_back(0x04);
  2349. buf.push_back(0x87);
  2350. buf.push_back(0x65);
  2351. buf.push_back(0x43);
  2352. buf.push_back(0x21);
  2353. OptionPtr vopt(new Option(Option::V4, DHO_VENDOR_ENCAPSULATED_OPTIONS, buf));
  2354. query->addOption(vopt);
  2355. query->deferredOptions().push_back(DHO_VENDOR_ENCAPSULATED_OPTIONS);
  2356. // Create and add a vendor-class-identifier (code 60)
  2357. OptionStringPtr iopt(new OptionString(Option::V4,
  2358. DHO_VENDOR_CLASS_IDENTIFIER,
  2359. "alpha"));
  2360. query->addOption(iopt);
  2361. // Create and add a PRL option to the query
  2362. OptionUint8ArrayPtr prl(new OptionUint8Array(Option::V4,
  2363. DHO_DHCP_PARAMETER_REQUEST_LIST));
  2364. ASSERT_TRUE(prl);
  2365. prl->addValue(DHO_VENDOR_ENCAPSULATED_OPTIONS);
  2366. prl->addValue(DHO_VENDOR_CLASS_IDENTIFIER);
  2367. query->addOption(prl);
  2368. srv.classifyPacket(query);
  2369. ASSERT_NO_THROW(srv.deferredUnpack(query));
  2370. // Check if the option was (correctly) re-unpacked
  2371. vopt = query->getOption(DHO_VENDOR_ENCAPSULATED_OPTIONS);
  2372. OptionCustomPtr custom = boost::dynamic_pointer_cast<OptionCustom>(vopt);
  2373. EXPECT_TRUE(custom);
  2374. EXPECT_EQ(1, vopt->getOptions().size());
  2375. // Pass it to the server and get an offer
  2376. Pkt4Ptr offer = srv.processDiscover(query);
  2377. // Check if we get response at all
  2378. checkResponse(offer, DHCPOFFER, 1234);
  2379. // Processing should add a vendor-class-identifier (code 60)
  2380. OptionPtr opt = offer->getOption(DHO_VENDOR_CLASS_IDENTIFIER);
  2381. EXPECT_TRUE(opt);
  2382. OptionStringPtr id = boost::dynamic_pointer_cast<OptionString>(opt);
  2383. ASSERT_TRUE(id);
  2384. EXPECT_EQ("alpha", id->getValue());
  2385. // And a vendor-encapsulated-options (code 43)
  2386. opt = offer->getOption(DHO_VENDOR_ENCAPSULATED_OPTIONS);
  2387. ASSERT_TRUE(opt);
  2388. // Verifies the content
  2389. const OptionCollection& opts = opt->getOptions();
  2390. ASSERT_EQ(1, opts.size());
  2391. OptionPtr sopt = opts.begin()->second;
  2392. ASSERT_TRUE(sopt);
  2393. EXPECT_EQ(1, sopt->getType());
  2394. EXPECT_EQ(2 + 4, sopt->len());
  2395. OptionUint32Ptr sopt32 = boost::dynamic_pointer_cast<OptionUint32>(sopt);
  2396. ASSERT_TRUE(sopt32);
  2397. EXPECT_EQ(12345678, sopt32->getValue());
  2398. }
  2399. // Verifies option 43 deferred processing (two classes)
  2400. TEST_F(Dhcpv4SrvTest, option43Classes) {
  2401. IfaceMgrTestConfig test_config(true);
  2402. IfaceMgr::instance().openSockets4();
  2403. NakedDhcpv4Srv srv(0);
  2404. // Two client-class scopes get vendor-encapsulated-options
  2405. // (code 43) definition and data. The first matching client-class
  2406. // (from a set?) applies.
  2407. string config = "{ \"interfaces-config\": {"
  2408. " \"interfaces\": [ \"*\" ] }, "
  2409. "\"rebind-timer\": 2000, "
  2410. "\"renew-timer\": 1000, "
  2411. "\"valid-lifetime\": 4000, "
  2412. "\"option-def\": [ "
  2413. "{ \"code\": 1, "
  2414. " \"name\": \"foo\", "
  2415. " \"space\": \"alpha\", "
  2416. " \"type\": \"uint32\" },"
  2417. "{ \"code\": 1, "
  2418. " \"name\": \"bar\", "
  2419. " \"space\": \"beta\", "
  2420. " \"type\": \"uint8\" } ],"
  2421. "\"subnet4\": [ "
  2422. "{ \"pools\": [ { \"pool\": \"192.0.2.1 - 192.0.2.100\" } ], "
  2423. " \"subnet\": \"192.0.2.0/24\" } ],"
  2424. "\"client-classes\": [ "
  2425. "{ \"name\": \"alpha\", "
  2426. " \"test\": \"option[vendor-class-identifier].text == 'alpha'\", "
  2427. " \"option-def\": [ "
  2428. " { \"code\": 43, "
  2429. " \"name\": \"vendor-encapsulated-options\", "
  2430. " \"type\": \"empty\", "
  2431. " \"encapsulate\": \"alpha\" } ],"
  2432. " \"option-data\": [ "
  2433. "{ \"name\": \"vendor-encapsulated-options\" }, "
  2434. " { \"name\": \"vendor-class-identifier\", "
  2435. " \"data\": \"alpha\" }, "
  2436. " { \"name\": \"foo\", "
  2437. " \"space\": \"alpha\", "
  2438. " \"data\": \"12345678\" } ] },"
  2439. "{ \"name\": \"beta\", "
  2440. " \"test\": \"option[vendor-class-identifier].text == 'beta'\", "
  2441. " \"option-def\": [ "
  2442. " { \"code\": 43, "
  2443. " \"name\": \"vendor-encapsulated-options\", "
  2444. " \"type\": \"empty\", "
  2445. " \"encapsulate\": \"beta\" } ],"
  2446. " \"option-data\": [ "
  2447. "{ \"name\": \"vendor-encapsulated-options\" }, "
  2448. " { \"name\": \"vendor-class-identifier\", "
  2449. " \"data\": \"beta\" }, "
  2450. " { \"name\": \"bar\", "
  2451. " \"space\": \"beta\", "
  2452. " \"data\": \"33\" } ] } ] }";
  2453. ConstElementPtr json;
  2454. ASSERT_NO_THROW(json = parseDHCP4(config));
  2455. ConstElementPtr status;
  2456. // Configure the server and make sure the config is accepted
  2457. EXPECT_NO_THROW(status = configureDhcp4Server(srv, json));
  2458. ASSERT_TRUE(status);
  2459. comment_ = config::parseAnswer(rcode_, status);
  2460. ASSERT_EQ(0, rcode_);
  2461. CfgMgr::instance().commit();
  2462. // Create a packet with enough to select the subnet and go through
  2463. // the DISCOVER processing
  2464. Pkt4Ptr query(new Pkt4(DHCPDISCOVER, 1234));
  2465. query->setRemoteAddr(IOAddress("192.0.2.1"));
  2466. OptionPtr clientid = generateClientId();
  2467. query->addOption(clientid);
  2468. query->setIface("eth1");
  2469. // Create and add a vendor-encapsulated-options (code 43)
  2470. OptionBuffer buf;
  2471. buf.push_back(0x01);
  2472. buf.push_back(0x04);
  2473. buf.push_back(0x87);
  2474. buf.push_back(0x65);
  2475. buf.push_back(0x43);
  2476. buf.push_back(0x21);
  2477. OptionPtr vopt(new Option(Option::V4, DHO_VENDOR_ENCAPSULATED_OPTIONS, buf));
  2478. query->addOption(vopt);
  2479. query->deferredOptions().push_back(DHO_VENDOR_ENCAPSULATED_OPTIONS);
  2480. // Create and add a vendor-class-identifier (code 60)
  2481. OptionStringPtr iopt(new OptionString(Option::V4,
  2482. DHO_VENDOR_CLASS_IDENTIFIER,
  2483. "alpha"));
  2484. query->addOption(iopt);
  2485. // Create and add a PRL option to the query
  2486. OptionUint8ArrayPtr prl(new OptionUint8Array(Option::V4,
  2487. DHO_DHCP_PARAMETER_REQUEST_LIST));
  2488. ASSERT_TRUE(prl);
  2489. prl->addValue(DHO_VENDOR_ENCAPSULATED_OPTIONS);
  2490. prl->addValue(DHO_VENDOR_CLASS_IDENTIFIER);
  2491. query->addOption(prl);
  2492. srv.classifyPacket(query);
  2493. ASSERT_NO_THROW(srv.deferredUnpack(query));
  2494. // Check if the option was (correctly) re-unpacked
  2495. vopt = query->getOption(DHO_VENDOR_ENCAPSULATED_OPTIONS);
  2496. OptionCustomPtr custom = boost::dynamic_pointer_cast<OptionCustom>(vopt);
  2497. EXPECT_TRUE(custom);
  2498. EXPECT_EQ(1, vopt->getOptions().size());
  2499. // Pass it to the server and get an offer
  2500. Pkt4Ptr offer = srv.processDiscover(query);
  2501. // Check if we get response at all
  2502. checkResponse(offer, DHCPOFFER, 1234);
  2503. // Processing should add a vendor-class-identifier (code 60)
  2504. OptionPtr opt = offer->getOption(DHO_VENDOR_CLASS_IDENTIFIER);
  2505. EXPECT_TRUE(opt);
  2506. OptionStringPtr id = boost::dynamic_pointer_cast<OptionString>(opt);
  2507. ASSERT_TRUE(id);
  2508. EXPECT_EQ("alpha", id->getValue());
  2509. // And a vendor-encapsulated-options (code 43)
  2510. opt = offer->getOption(DHO_VENDOR_ENCAPSULATED_OPTIONS);
  2511. ASSERT_TRUE(opt);
  2512. // Verifies the content
  2513. const OptionCollection& opts = opt->getOptions();
  2514. ASSERT_EQ(1, opts.size());
  2515. OptionPtr sopt = opts.begin()->second;
  2516. ASSERT_TRUE(sopt);
  2517. EXPECT_EQ(1, sopt->getType());
  2518. EXPECT_EQ(2 + 4, sopt->len());
  2519. OptionUint32Ptr sopt32 = boost::dynamic_pointer_cast<OptionUint32>(sopt);
  2520. ASSERT_TRUE(sopt32);
  2521. EXPECT_EQ(12345678, sopt32->getValue());
  2522. }
  2523. // Verifies private option deferred processing
  2524. TEST_F(Dhcpv4SrvTest, privateOption) {
  2525. IfaceMgrTestConfig test_config(true);
  2526. IfaceMgr::instance().openSockets4();
  2527. NakedDhcpv4Srv srv(0);
  2528. // Same than option43Class but with private options
  2529. string config = "{ \"interfaces-config\": {"
  2530. " \"interfaces\": [ \"*\" ] }, "
  2531. "\"rebind-timer\": 2000, "
  2532. "\"renew-timer\": 1000, "
  2533. "\"valid-lifetime\": 4000, "
  2534. "\"subnet4\": [ "
  2535. "{ \"pools\": [ { \"pool\": \"192.0.2.1 - 192.0.2.100\" } ], "
  2536. " \"subnet\": \"192.0.2.0/24\" } ],"
  2537. "\"client-classes\": [ "
  2538. "{ \"name\": \"private\", "
  2539. " \"test\": \"option[234].exists\", "
  2540. " \"option-def\": [ "
  2541. " { \"code\": 245, "
  2542. " \"name\": \"privint\", "
  2543. " \"type\": \"uint32\" } ],"
  2544. " \"option-data\": [ "
  2545. " { \"code\": 234, "
  2546. " \"data\": \"01\" }, "
  2547. " { \"name\": \"privint\", "
  2548. " \"data\": \"12345678\" } ] } ] }";
  2549. ConstElementPtr json;
  2550. ASSERT_NO_THROW(json = parseDHCP4(config));
  2551. ConstElementPtr status;
  2552. // Configure the server and make sure the config is accepted
  2553. EXPECT_NO_THROW(status = configureDhcp4Server(srv, json));
  2554. ASSERT_TRUE(status);
  2555. comment_ = config::parseAnswer(rcode_, status);
  2556. ASSERT_EQ(0, rcode_);
  2557. CfgMgr::instance().commit();
  2558. // Create a packet with enough to select the subnet and go through
  2559. // the DISCOVER processing
  2560. Pkt4Ptr query(new Pkt4(DHCPDISCOVER, 1234));
  2561. query->setRemoteAddr(IOAddress("192.0.2.1"));
  2562. OptionPtr clientid = generateClientId();
  2563. query->addOption(clientid);
  2564. query->setIface("eth1");
  2565. // Create and add a private option with code 234
  2566. OptionBuffer buf;
  2567. buf.push_back(0x01);
  2568. OptionPtr opt1(new Option(Option::V4, 234, buf));
  2569. query->addOption(opt1);
  2570. query->deferredOptions().push_back(234);
  2571. // Create and add a private option with code 245
  2572. buf.clear();
  2573. buf.push_back(0x87);
  2574. buf.push_back(0x65);
  2575. buf.push_back(0x43);
  2576. buf.push_back(0x21);
  2577. OptionPtr opt2(new Option(Option::V4, 245, buf));
  2578. query->addOption(opt2);
  2579. query->deferredOptions().push_back(245);
  2580. // Create and add a PRL option to the query
  2581. OptionUint8ArrayPtr prl(new OptionUint8Array(Option::V4,
  2582. DHO_DHCP_PARAMETER_REQUEST_LIST));
  2583. ASSERT_TRUE(prl);
  2584. prl->addValue(234);
  2585. prl->addValue(245);
  2586. query->addOption(prl);
  2587. srv.classifyPacket(query);
  2588. ASSERT_NO_THROW(srv.deferredUnpack(query));
  2589. // Check if the option 245 was re-unpacked
  2590. opt2 = query->getOption(245);
  2591. OptionUint32Ptr opt32 = boost::dynamic_pointer_cast<OptionUint32>(opt2);
  2592. EXPECT_TRUE(opt32);
  2593. // Pass it to the server and get an offer
  2594. Pkt4Ptr offer = srv.processDiscover(query);
  2595. // Check if we get response at all
  2596. checkResponse(offer, DHCPOFFER, 1234);
  2597. // Processing should add an option with code 234
  2598. OptionPtr opt = offer->getOption(234);
  2599. EXPECT_TRUE(opt);
  2600. // And an option with code 245
  2601. opt = offer->getOption(245);
  2602. ASSERT_TRUE(opt);
  2603. // Verifies the content
  2604. opt32 = boost::dynamic_pointer_cast<OptionUint32>(opt);
  2605. ASSERT_TRUE(opt32);
  2606. EXPECT_EQ(12345678, opt32->getValue());
  2607. }
  2608. // Checks effect of persistency (aka always-true) flag on the PRL
  2609. TEST_F(Dhcpv4SrvTest, prlPersistency) {
  2610. IfaceMgrTestConfig test_config(true);
  2611. IfaceMgr::instance().openSockets4();
  2612. ASSERT_NO_THROW(configure(CONFIGS[2]));
  2613. // Create a packet with enough to select the subnet and go through
  2614. // the DISCOVER processing
  2615. Pkt4Ptr query(new Pkt4(DHCPDISCOVER, 1234));
  2616. query->setRemoteAddr(IOAddress("192.0.2.1"));
  2617. OptionPtr clientid = generateClientId();
  2618. query->addOption(clientid);
  2619. query->setIface("eth1");
  2620. // Create and add a PRL option for another option
  2621. OptionUint8ArrayPtr prl(new OptionUint8Array(Option::V4,
  2622. DHO_DHCP_PARAMETER_REQUEST_LIST));
  2623. ASSERT_TRUE(prl);
  2624. prl->addValue(DHO_ARP_CACHE_TIMEOUT);
  2625. query->addOption(prl);
  2626. // Create and add a host-name option to the query
  2627. OptionStringPtr hostname(new OptionString(Option::V4, 12, "foo"));
  2628. ASSERT_TRUE(hostname);
  2629. query->addOption(hostname);
  2630. // Let the server process it.
  2631. Pkt4Ptr response = srv_.processDiscover(query);
  2632. // Processing should add an ip-forwarding option
  2633. ASSERT_TRUE(response->getOption(DHO_IP_FORWARDING));
  2634. // But no default-ip-ttl
  2635. ASSERT_FALSE(response->getOption(DHO_DEFAULT_IP_TTL));
  2636. // Nor an arp-cache-timeout
  2637. ASSERT_FALSE(response->getOption(DHO_ARP_CACHE_TIMEOUT));
  2638. // Reset PRL adding default-ip-ttl
  2639. query->delOption(DHO_DHCP_PARAMETER_REQUEST_LIST);
  2640. prl->addValue(DHO_DEFAULT_IP_TTL);
  2641. query->addOption(prl);
  2642. // Let the server process it again.
  2643. response = srv_.processDiscover(query);
  2644. // Processing should add an ip-forwarding option
  2645. ASSERT_TRUE(response->getOption(DHO_IP_FORWARDING));
  2646. // and now a default-ip-ttl
  2647. ASSERT_TRUE(response->getOption(DHO_DEFAULT_IP_TTL));
  2648. // and still no arp-cache-timeout
  2649. ASSERT_FALSE(response->getOption(DHO_ARP_CACHE_TIMEOUT));
  2650. }
  2651. // Checks if relay IP address specified in the relay-info structure in
  2652. // subnet4 is being used properly.
  2653. TEST_F(Dhcpv4SrvTest, relayOverride) {
  2654. // We have 2 subnets defined. Note that both have a relay address
  2655. // defined. Both are not belonging to the subnets. That is
  2656. // important, because if the relay belongs to the subnet, there's
  2657. // no need to specify relay override.
  2658. string config = "{ \"interfaces-config\": {"
  2659. " \"interfaces\": [ \"*\" ]"
  2660. "},"
  2661. "\"rebind-timer\": 2000, "
  2662. "\"renew-timer\": 1000, "
  2663. "\"subnet4\": [ "
  2664. "{ \"pools\": [ { \"pool\": \"192.0.2.2 - 192.0.2.100\" } ],"
  2665. " \"relay\": { "
  2666. " \"ip-address\": \"192.0.5.1\""
  2667. " },"
  2668. " \"subnet\": \"192.0.2.0/24\" }, "
  2669. "{ \"pools\": [ { \"pool\": \"192.0.3.1 - 192.0.3.100\" } ],"
  2670. " \"relay\": { "
  2671. " \"ip-address\": \"192.0.5.2\""
  2672. " },"
  2673. " \"subnet\": \"192.0.3.0/24\" } "
  2674. "],"
  2675. "\"valid-lifetime\": 4000 }";
  2676. // Use this config to set up the server
  2677. ASSERT_NO_THROW(configure(config));
  2678. // Let's get the subnet configuration objects
  2679. const Subnet4Collection* subnets =
  2680. CfgMgr::instance().getCurrentCfg()->getCfgSubnets4()->getAll();
  2681. ASSERT_EQ(2, subnets->size());
  2682. // Let's get them for easy reference
  2683. Subnet4Ptr subnet1 = (*subnets)[0];
  2684. Subnet4Ptr subnet2 = (*subnets)[1];
  2685. ASSERT_TRUE(subnet1);
  2686. ASSERT_TRUE(subnet2);
  2687. // Let's create a packet.
  2688. Pkt4Ptr dis = Pkt4Ptr(new Pkt4(DHCPDISCOVER, 1234));
  2689. dis->setRemoteAddr(IOAddress("192.0.2.1"));
  2690. dis->setIface("eth0");
  2691. dis->setHops(1);
  2692. OptionPtr clientid = generateClientId();
  2693. dis->addOption(clientid);
  2694. // This is just a sanity check, we're using regular method: ciaddr 192.0.2.1
  2695. // belongs to the first subnet, so it is selected
  2696. dis->setGiaddr(IOAddress("192.0.2.1"));
  2697. EXPECT_TRUE(subnet1 == srv_.selectSubnet(dis));
  2698. // Relay belongs to the second subnet, so it should be selected.
  2699. dis->setGiaddr(IOAddress("192.0.3.1"));
  2700. EXPECT_TRUE(subnet2 == srv_.selectSubnet(dis));
  2701. // Now let's check if the relay override for the first subnets works
  2702. dis->setGiaddr(IOAddress("192.0.5.1"));
  2703. EXPECT_TRUE(subnet1 == srv_.selectSubnet(dis));
  2704. // The same check for the second subnet...
  2705. dis->setGiaddr(IOAddress("192.0.5.2"));
  2706. EXPECT_TRUE(subnet2 == srv_.selectSubnet(dis));
  2707. // And finally, let's check if mis-matched relay address will end up
  2708. // in not selecting a subnet at all
  2709. dis->setGiaddr(IOAddress("192.0.5.3"));
  2710. EXPECT_FALSE(srv_.selectSubnet(dis));
  2711. // Finally, check that the relay override works only with relay address
  2712. // (GIADDR) and does not affect client address (CIADDR)
  2713. dis->setGiaddr(IOAddress("0.0.0.0"));
  2714. dis->setHops(0);
  2715. dis->setCiaddr(IOAddress("192.0.5.1"));
  2716. EXPECT_FALSE(srv_.selectSubnet(dis));
  2717. }
  2718. // Checks if relay IP address specified in the relay-info structure can be
  2719. // used together with client-classification.
  2720. TEST_F(Dhcpv4SrvTest, relayOverrideAndClientClass) {
  2721. // This test configures 2 subnets. They both are on the same link, so they
  2722. // have the same relay-ip address. Furthermore, the first subnet is
  2723. // reserved for clients that belong to class "foo".
  2724. string config = "{ \"interfaces-config\": {"
  2725. " \"interfaces\": [ \"*\" ]"
  2726. "},"
  2727. "\"rebind-timer\": 2000, "
  2728. "\"renew-timer\": 1000, "
  2729. "\"subnet4\": [ "
  2730. "{ \"pools\": [ { \"pool\": \"192.0.2.2 - 192.0.2.100\" } ],"
  2731. " \"client-class\": \"foo\", "
  2732. " \"relay\": { "
  2733. " \"ip-address\": \"192.0.5.1\""
  2734. " },"
  2735. " \"subnet\": \"192.0.2.0/24\" }, "
  2736. "{ \"pools\": [ { \"pool\": \"192.0.3.1 - 192.0.3.100\" } ],"
  2737. " \"relay\": { "
  2738. " \"ip-address\": \"192.0.5.1\""
  2739. " },"
  2740. " \"subnet\": \"192.0.3.0/24\" } "
  2741. "],"
  2742. "\"valid-lifetime\": 4000 }";
  2743. // Use this config to set up the server
  2744. ASSERT_NO_THROW(configure(config));
  2745. const Subnet4Collection* subnets =
  2746. CfgMgr::instance().getCurrentCfg()->getCfgSubnets4()->getAll();
  2747. ASSERT_EQ(2, subnets->size());
  2748. // Let's get them for easy reference
  2749. Subnet4Ptr subnet1 = (*subnets)[0];
  2750. Subnet4Ptr subnet2 = (*subnets)[1];
  2751. ASSERT_TRUE(subnet1);
  2752. ASSERT_TRUE(subnet2);
  2753. // Let's create a packet.
  2754. Pkt4Ptr dis = Pkt4Ptr(new Pkt4(DHCPDISCOVER, 1234));
  2755. dis->setRemoteAddr(IOAddress("192.0.2.1"));
  2756. dis->setIface("eth0");
  2757. dis->setHops(1);
  2758. dis->setGiaddr(IOAddress("192.0.5.1"));
  2759. OptionPtr clientid = generateClientId();
  2760. dis->addOption(clientid);
  2761. // This packet does not belong to class foo, so it should be rejected in
  2762. // subnet[0], even though the relay-ip matches. It should be accepted in
  2763. // subnet[1], because the subnet matches and there are no class
  2764. // requirements.
  2765. EXPECT_TRUE(subnet2 == srv_.selectSubnet(dis));
  2766. // Now let's add this packet to class foo and recheck. This time it should
  2767. // be accepted in the first subnet, because both class and relay-ip match.
  2768. dis->addClass("foo");
  2769. EXPECT_TRUE(subnet1 == srv_.selectSubnet(dis));
  2770. }
  2771. // Checks if a RAI link selection sub-option works as expected
  2772. TEST_F(Dhcpv4SrvTest, relayLinkSelect) {
  2773. // We have 3 subnets defined.
  2774. string config = "{ \"interfaces-config\": {"
  2775. " \"interfaces\": [ \"*\" ]"
  2776. "},"
  2777. "\"rebind-timer\": 2000, "
  2778. "\"renew-timer\": 1000, "
  2779. "\"subnet4\": [ "
  2780. "{ \"pools\": [ { \"pool\": \"192.0.2.2 - 192.0.2.100\" } ],"
  2781. " \"relay\": { "
  2782. " \"ip-address\": \"192.0.5.1\""
  2783. " },"
  2784. " \"subnet\": \"192.0.2.0/24\" }, "
  2785. "{ \"pools\": [ { \"pool\": \"192.0.3.1 - 192.0.3.100\" } ],"
  2786. " \"subnet\": \"192.0.3.0/24\" }, "
  2787. "{ \"pools\": [ { \"pool\": \"192.0.4.1 - 192.0.4.100\" } ],"
  2788. " \"client-class\": \"foo\", "
  2789. " \"subnet\": \"192.0.4.0/24\" } "
  2790. "],"
  2791. "\"valid-lifetime\": 4000 }";
  2792. // Use this config to set up the server
  2793. ASSERT_NO_THROW(configure(config));
  2794. // Let's get the subnet configuration objects
  2795. const Subnet4Collection* subnets =
  2796. CfgMgr::instance().getCurrentCfg()->getCfgSubnets4()->getAll();
  2797. ASSERT_EQ(3, subnets->size());
  2798. // Let's get them for easy reference
  2799. Subnet4Ptr subnet1 = (*subnets)[0];
  2800. Subnet4Ptr subnet2 = (*subnets)[1];
  2801. Subnet4Ptr subnet3 = (*subnets)[2];
  2802. ASSERT_TRUE(subnet1);
  2803. ASSERT_TRUE(subnet2);
  2804. ASSERT_TRUE(subnet3);
  2805. // Let's create a packet.
  2806. Pkt4Ptr dis = Pkt4Ptr(new Pkt4(DHCPDISCOVER, 1234));
  2807. dis->setRemoteAddr(IOAddress("192.0.2.1"));
  2808. dis->setIface("eth0");
  2809. dis->setHops(1);
  2810. OptionPtr clientid = generateClientId();
  2811. dis->addOption(clientid);
  2812. // Let's create a Relay Agent Information option
  2813. OptionDefinitionPtr rai_def = LibDHCP::getOptionDef(DHCP4_OPTION_SPACE,
  2814. DHO_DHCP_AGENT_OPTIONS);
  2815. ASSERT_TRUE(rai_def);
  2816. OptionCustomPtr rai(new OptionCustom(*rai_def, Option::V4));
  2817. ASSERT_TRUE(rai);
  2818. IOAddress addr("192.0.3.2");
  2819. OptionPtr ols(new Option(Option::V4,
  2820. RAI_OPTION_LINK_SELECTION,
  2821. addr.toBytes()));
  2822. ASSERT_TRUE(ols);
  2823. rai->addOption(ols);
  2824. // This is just a sanity check, we're using regular method: ciaddr 192.0.3.1
  2825. // belongs to the second subnet, so it is selected
  2826. dis->setGiaddr(IOAddress("192.0.3.1"));
  2827. EXPECT_TRUE(subnet2 == srv_.selectSubnet(dis));
  2828. // Setup a relay override for the first subnet as it has a high precedence
  2829. dis->setGiaddr(IOAddress("192.0.5.1"));
  2830. EXPECT_TRUE(subnet1 == srv_.selectSubnet(dis));
  2831. // Put a RAI to select back the second subnet as it has
  2832. // the highest precedence
  2833. dis->addOption(rai);
  2834. EXPECT_TRUE(subnet2 == srv_.selectSubnet(dis));
  2835. // Subnet select option has a lower precedence
  2836. OptionDefinitionPtr sbnsel_def = LibDHCP::getOptionDef(DHCP4_OPTION_SPACE,
  2837. DHO_SUBNET_SELECTION);
  2838. ASSERT_TRUE(sbnsel_def);
  2839. OptionCustomPtr sbnsel(new OptionCustom(*sbnsel_def, Option::V4));
  2840. ASSERT_TRUE(sbnsel);
  2841. sbnsel->writeAddress(IOAddress("192.0.2.3"));
  2842. dis->addOption(sbnsel);
  2843. EXPECT_TRUE(subnet2 == srv_.selectSubnet(dis));
  2844. dis->delOption(DHO_SUBNET_SELECTION);
  2845. // Check client-classification still applies
  2846. IOAddress addr_foo("192.0.4.2");
  2847. ols.reset(new Option(Option::V4, RAI_OPTION_LINK_SELECTION,
  2848. addr_foo.toBytes()));
  2849. rai->delOption(RAI_OPTION_LINK_SELECTION);
  2850. dis->delOption(DHO_DHCP_AGENT_OPTIONS);
  2851. rai->addOption(ols);
  2852. dis->addOption(rai);
  2853. // Note it shall fail (vs. try the next criterion).
  2854. EXPECT_FALSE(srv_.selectSubnet(dis));
  2855. // Add the packet to the class and check again: now it shall succeed
  2856. dis->addClass("foo");
  2857. EXPECT_TRUE(subnet3 == srv_.selectSubnet(dis));
  2858. // Check it fails with a bad address in the sub-option
  2859. IOAddress addr_bad("10.0.0.1");
  2860. ols.reset(new Option(Option::V4, RAI_OPTION_LINK_SELECTION,
  2861. addr_bad.toBytes()));
  2862. rai->delOption(RAI_OPTION_LINK_SELECTION);
  2863. dis->delOption(DHO_DHCP_AGENT_OPTIONS);
  2864. rai->addOption(ols);
  2865. dis->addOption(rai);
  2866. EXPECT_FALSE(srv_.selectSubnet(dis));
  2867. }
  2868. // Checks if a subnet selection option works as expected
  2869. TEST_F(Dhcpv4SrvTest, subnetSelect) {
  2870. // We have 3 subnets defined.
  2871. string config = "{ \"interfaces-config\": {"
  2872. " \"interfaces\": [ \"*\" ]"
  2873. "},"
  2874. "\"rebind-timer\": 2000, "
  2875. "\"renew-timer\": 1000, "
  2876. "\"subnet4\": [ "
  2877. "{ \"pools\": [ { \"pool\": \"192.0.2.2 - 192.0.2.100\" } ],"
  2878. " \"relay\": { "
  2879. " \"ip-address\": \"192.0.5.1\""
  2880. " },"
  2881. " \"subnet\": \"192.0.2.0/24\" }, "
  2882. "{ \"pools\": [ { \"pool\": \"192.0.3.1 - 192.0.3.100\" } ],"
  2883. " \"subnet\": \"192.0.3.0/24\" }, "
  2884. "{ \"pools\": [ { \"pool\": \"192.0.4.1 - 192.0.4.100\" } ],"
  2885. " \"client-class\": \"foo\", "
  2886. " \"subnet\": \"192.0.4.0/24\" } "
  2887. "],"
  2888. "\"valid-lifetime\": 4000 }";
  2889. // Use this config to set up the server
  2890. ASSERT_NO_THROW(configure(config));
  2891. // Let's get the subnet configuration objects
  2892. const Subnet4Collection* subnets =
  2893. CfgMgr::instance().getCurrentCfg()->getCfgSubnets4()->getAll();
  2894. ASSERT_EQ(3, subnets->size());
  2895. // Let's get them for easy reference
  2896. Subnet4Ptr subnet1 = (*subnets)[0];
  2897. Subnet4Ptr subnet2 = (*subnets)[1];
  2898. Subnet4Ptr subnet3 = (*subnets)[2];
  2899. ASSERT_TRUE(subnet1);
  2900. ASSERT_TRUE(subnet2);
  2901. ASSERT_TRUE(subnet3);
  2902. // Let's create a packet.
  2903. Pkt4Ptr dis = Pkt4Ptr(new Pkt4(DHCPDISCOVER, 1234));
  2904. dis->setRemoteAddr(IOAddress("192.0.2.1"));
  2905. dis->setIface("eth0");
  2906. dis->setHops(1);
  2907. OptionPtr clientid = generateClientId();
  2908. dis->addOption(clientid);
  2909. // Let's create a Subnet Selection option
  2910. OptionDefinitionPtr sbnsel_def = LibDHCP::getOptionDef(DHCP4_OPTION_SPACE,
  2911. DHO_SUBNET_SELECTION);
  2912. ASSERT_TRUE(sbnsel_def);
  2913. OptionCustomPtr sbnsel(new OptionCustom(*sbnsel_def, Option::V4));
  2914. ASSERT_TRUE(sbnsel);
  2915. sbnsel->writeAddress(IOAddress("192.0.3.2"));
  2916. // This is just a sanity check, we're using regular method: ciaddr 192.0.3.1
  2917. // belongs to the second subnet, so it is selected
  2918. dis->setGiaddr(IOAddress("192.0.3.1"));
  2919. EXPECT_TRUE(subnet2 == srv_.selectSubnet(dis));
  2920. // Setup a relay override for the first subnet as it has a high precedence
  2921. dis->setGiaddr(IOAddress("192.0.5.1"));
  2922. EXPECT_TRUE(subnet1 == srv_.selectSubnet(dis));
  2923. // Put a subnet select option to select back the second subnet as
  2924. // it has the second highest precedence
  2925. dis->addOption(sbnsel);
  2926. EXPECT_TRUE(subnet2 == srv_.selectSubnet(dis));
  2927. // Check client-classification still applies
  2928. sbnsel->writeAddress(IOAddress("192.0.4.2"));
  2929. // Note it shall fail (vs. try the next criterion).
  2930. EXPECT_FALSE(srv_.selectSubnet(dis));
  2931. // Add the packet to the class and check again: now it shall succeed
  2932. dis->addClass("foo");
  2933. EXPECT_TRUE(subnet3 == srv_.selectSubnet(dis));
  2934. // Check it fails with a bad address in the sub-option
  2935. sbnsel->writeAddress(IOAddress("10.0.0.1"));
  2936. EXPECT_FALSE(srv_.selectSubnet(dis));
  2937. }
  2938. // This test verifies that the direct message is dropped when it has been
  2939. // received by the server via an interface for which there is no subnet
  2940. // configured. It also checks that the message is not dropped (is processed)
  2941. // when it is relayed or unicast.
  2942. TEST_F(Dhcpv4SrvTest, acceptDirectRequest) {
  2943. IfaceMgrTestConfig test_config(true);
  2944. IfaceMgr::instance().openSockets4();
  2945. NakedDhcpv4Srv srv(0);
  2946. Pkt4Ptr pkt(new Pkt4(DHCPDISCOVER, 1234));
  2947. // Set Giaddr and local server's unicast address, but don't set hops.
  2948. // Hops value should not matter. The server will treat the message
  2949. // with the hops value of 0 and non-zero giaddr as relayed.
  2950. pkt->setGiaddr(IOAddress("192.0.10.1"));
  2951. pkt->setRemoteAddr(IOAddress("0.0.0.0"));
  2952. pkt->setLocalAddr(IOAddress("192.0.2.3"));
  2953. pkt->setIface("eth1");
  2954. EXPECT_TRUE(srv.accept(pkt));
  2955. // Let's set hops and check that the message is still accepted as
  2956. // a relayed message.
  2957. pkt->setHops(1);
  2958. EXPECT_TRUE(srv.accept(pkt));
  2959. // Make it a direct message but keep unicast server's address. The
  2960. // messages sent to unicast address should be accepted as they are
  2961. // most likely to renew existing leases. The server should respond
  2962. // to renews so they have to be accepted and processed.
  2963. pkt->setHops(0);
  2964. pkt->setGiaddr(IOAddress("0.0.0.0"));
  2965. EXPECT_TRUE(srv.accept(pkt));
  2966. // Direct message is now sent to a broadcast address. The server
  2967. // should accept this message because it has been received via
  2968. // eth1 for which there is a subnet configured (see test fixture
  2969. // class constructor).
  2970. pkt->setLocalAddr(IOAddress("255.255.255.255"));
  2971. EXPECT_TRUE(srv.accept(pkt));
  2972. // For eth0, there is no subnet configured. Such message is expected
  2973. // to be silently dropped.
  2974. pkt->setIface("eth0");
  2975. EXPECT_FALSE(srv.accept(pkt));
  2976. // But, if the message is unicast it should be accepted, even though
  2977. // it has been received via eth0.
  2978. pkt->setLocalAddr(IOAddress("10.0.0.1"));
  2979. EXPECT_TRUE(srv.accept(pkt));
  2980. // For the DHCPINFORM the ciaddr should be set or at least the source
  2981. // address.
  2982. pkt->setType(DHCPINFORM);
  2983. pkt->setRemoteAddr(IOAddress("10.0.0.101"));
  2984. EXPECT_TRUE(srv.accept(pkt));
  2985. // When neither ciaddr nor source address is present, the packet should
  2986. // be dropped.
  2987. pkt->setRemoteAddr(IOAddress("0.0.0.0"));
  2988. EXPECT_FALSE(srv.accept(pkt));
  2989. // When ciaddr is set, the packet should be accepted.
  2990. pkt->setCiaddr(IOAddress("10.0.0.1"));
  2991. EXPECT_TRUE(srv.accept(pkt));
  2992. }
  2993. // This test checks that the server rejects a message with invalid type.
  2994. TEST_F(Dhcpv4SrvTest, acceptMessageType) {
  2995. IfaceMgrTestConfig test_config(true);
  2996. IfaceMgr::instance().openSockets4();
  2997. NakedDhcpv4Srv srv(0);
  2998. // Specify messages to be accepted by the server.
  2999. int allowed[] = {
  3000. DHCPDISCOVER,
  3001. DHCPREQUEST,
  3002. DHCPRELEASE,
  3003. DHCPDECLINE,
  3004. DHCPINFORM
  3005. };
  3006. size_t allowed_size = sizeof(allowed) / sizeof(allowed[0]);
  3007. // Check that the server actually accepts these message types.
  3008. for (size_t i = 0; i < allowed_size; ++i) {
  3009. EXPECT_TRUE(srv.acceptMessageType(Pkt4Ptr(new Pkt4(allowed[i], 1234))))
  3010. << "Test failed for message type " << i;
  3011. }
  3012. // Specify messages which server is supposed to drop.
  3013. int not_allowed[] = {
  3014. DHCPOFFER,
  3015. DHCPACK,
  3016. DHCPNAK,
  3017. DHCPLEASEQUERY,
  3018. DHCPLEASEUNASSIGNED,
  3019. DHCPLEASEUNKNOWN,
  3020. DHCPLEASEACTIVE,
  3021. DHCPBULKLEASEQUERY,
  3022. DHCPLEASEQUERYDONE
  3023. };
  3024. size_t not_allowed_size = sizeof(not_allowed) / sizeof(not_allowed[0]);
  3025. // Actually check that the server will drop these messages.
  3026. for (size_t i = 0; i < not_allowed_size; ++i) {
  3027. EXPECT_FALSE(srv.acceptMessageType(Pkt4Ptr(new Pkt4(not_allowed[i],
  3028. 1234))))
  3029. << "Test failed for message type " << i;
  3030. }
  3031. }
  3032. // Test checks whether statistic is bumped up appropriately when Decline
  3033. // message is received.
  3034. TEST_F(Dhcpv4SrvTest, statisticsDecline) {
  3035. NakedDhcpv4Srv srv(0);
  3036. pretendReceivingPkt(srv, CONFIGS[0], DHCPDECLINE, "pkt4-decline-received");
  3037. }
  3038. // Test checks whether statistic is bumped up appropriately when Offer
  3039. // message is received (this should never happen in a sane network).
  3040. TEST_F(Dhcpv4SrvTest, statisticsOfferRcvd) {
  3041. NakedDhcpv4Srv srv(0);
  3042. pretendReceivingPkt(srv, CONFIGS[0], DHCPOFFER, "pkt4-offer-received");
  3043. }
  3044. // Test checks whether statistic is bumped up appropriately when Ack
  3045. // message is received (this should never happen in a sane network).
  3046. TEST_F(Dhcpv4SrvTest, statisticsAckRcvd) {
  3047. NakedDhcpv4Srv srv(0);
  3048. pretendReceivingPkt(srv, CONFIGS[0], DHCPACK, "pkt4-ack-received");
  3049. }
  3050. // Test checks whether statistic is bumped up appropriately when Nak
  3051. // message is received (this should never happen in a sane network).
  3052. TEST_F(Dhcpv4SrvTest, statisticsNakRcvd) {
  3053. NakedDhcpv4Srv srv(0);
  3054. pretendReceivingPkt(srv, CONFIGS[0], DHCPNAK, "pkt4-nak-received");
  3055. }
  3056. // Test checks whether statistic is bumped up appropriately when Release
  3057. // message is received.
  3058. TEST_F(Dhcpv4SrvTest, statisticsReleaseRcvd) {
  3059. NakedDhcpv4Srv srv(0);
  3060. pretendReceivingPkt(srv, CONFIGS[0], DHCPRELEASE, "pkt4-release-received");
  3061. }
  3062. // Test checks whether statistic is bumped up appropriately when unknown
  3063. // message is received.
  3064. TEST_F(Dhcpv4SrvTest, statisticsUnknownRcvd) {
  3065. NakedDhcpv4Srv srv(0);
  3066. pretendReceivingPkt(srv, CONFIGS[0], 200, "pkt4-unknown-received");
  3067. // There should also be pkt4-receive-drop stat bumped up
  3068. using namespace isc::stats;
  3069. StatsMgr& mgr = StatsMgr::instance();
  3070. ObservationPtr drop_stat = mgr.getObservation("pkt4-receive-drop");
  3071. // This statistic must be present and must be set to 1.
  3072. ASSERT_TRUE(drop_stat);
  3073. EXPECT_EQ(1, drop_stat->getInteger().first);
  3074. }
  3075. // This test verifies that the server is able to handle an empty client-id
  3076. // in incoming client message.
  3077. TEST_F(Dhcpv4SrvTest, emptyClientId) {
  3078. IfaceMgrTestConfig test_config(true);
  3079. IfaceMgr::instance().openSockets4();
  3080. Dhcp4Client client;
  3081. EXPECT_NO_THROW(configure(CONFIGS[0], *client.getServer()));
  3082. // Tell the client to not send client-id on its own.
  3083. client.includeClientId("");
  3084. // Instead, tell him to send this extra option, which happens to be
  3085. // an empty client-id.
  3086. OptionPtr empty_client_id(new Option(Option::V4, DHO_DHCP_CLIENT_IDENTIFIER));
  3087. client.addExtraOption(empty_client_id);
  3088. // Let's check whether the server is able to process this packet without
  3089. // throwing any exceptions. We don't care whether the server sent any
  3090. // responses or not. The goal is to check that the server didn't throw
  3091. // any exceptions.
  3092. EXPECT_NO_THROW(client.doDORA());
  3093. }
  3094. // This test verifies that the server is able to handle too long client-id
  3095. // in incoming client message.
  3096. TEST_F(Dhcpv4SrvTest, tooLongClientId) {
  3097. IfaceMgrTestConfig test_config(true);
  3098. IfaceMgr::instance().openSockets4();
  3099. Dhcp4Client client;
  3100. EXPECT_NO_THROW(configure(CONFIGS[0], *client.getServer()));
  3101. // Tell the client to not send client-id on its own.
  3102. client.includeClientId("");
  3103. // Instead, tell him to send this extra option, which happens to be
  3104. // an empty client-id.
  3105. std::vector<uint8_t> data(250, 250);
  3106. OptionPtr long_client_id(new Option(Option::V4, DHO_DHCP_CLIENT_IDENTIFIER,
  3107. data));
  3108. client.addExtraOption(long_client_id);
  3109. // Let's check whether the server is able to process this packet without
  3110. // throwing any exceptions. We don't care whether the server sent any
  3111. // responses or not. The goal is to check that the server didn't throw
  3112. // any exceptions.
  3113. EXPECT_NO_THROW(client.doDORA());
  3114. }
  3115. // Checks if user-contexts are parsed properly.
  3116. TEST_F(Dhcpv4SrvTest, userContext) {
  3117. IfaceMgrTestConfig test_config(true);
  3118. NakedDhcpv4Srv srv(0);
  3119. // This config has one subnet with user-context with one
  3120. // pool (also with context). Make sure the configuration could be accepted.
  3121. cout << CONFIGS[3] << endl;
  3122. EXPECT_NO_THROW(configure(CONFIGS[3]));
  3123. // Now make sure the data was not lost.
  3124. ConstSrvConfigPtr cfg = CfgMgr::instance().getCurrentCfg();
  3125. const Subnet4Collection* subnets = cfg->getCfgSubnets4()->getAll();
  3126. ASSERT_TRUE(subnets);
  3127. ASSERT_EQ(1, subnets->size());
  3128. // Let's get the subnet and check its context.
  3129. Subnet4Ptr subnet1 = (*subnets)[0];
  3130. ASSERT_TRUE(subnet1);
  3131. ASSERT_TRUE(subnet1->getContext());
  3132. EXPECT_EQ("{ \"secure\": false }", subnet1->getContext()->str());
  3133. // Ok, not get the sole pool in it and check its context, too.
  3134. PoolCollection pools = subnet1->getPools(Lease::TYPE_V4);
  3135. ASSERT_EQ(1, pools.size());
  3136. ASSERT_TRUE(pools[0]);
  3137. ASSERT_TRUE(pools[0]->getContext());
  3138. EXPECT_EQ("{ \"value\": 42 }", pools[0]->getContext()->str());
  3139. }
  3140. /// @todo: Implement proper tests for MySQL lease/host database,
  3141. /// see ticket #4214.
  3142. }; // end of anonymous namespace