libdhcp++.cc 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926
  1. // Copyright (C) 2011-2016 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 <dhcp/dhcp4.h>
  8. #include <dhcp/dhcp6.h>
  9. #include <dhcp/libdhcp++.h>
  10. #include <dhcp/option.h>
  11. #include <dhcp/option_vendor.h>
  12. #include <dhcp/option6_ia.h>
  13. #include <dhcp/option6_iaaddr.h>
  14. #include <dhcp/option_definition.h>
  15. #include <dhcp/option_int_array.h>
  16. #include <dhcp/std_option_defs.h>
  17. #include <dhcp/docsis3_option_defs.h>
  18. #include <exceptions/exceptions.h>
  19. #include <util/buffer.h>
  20. #include <dhcp/option_definition.h>
  21. #include <boost/lexical_cast.hpp>
  22. #include <boost/shared_array.hpp>
  23. #include <boost/shared_ptr.hpp>
  24. #include <limits>
  25. #include <list>
  26. using namespace std;
  27. using namespace isc::dhcp;
  28. using namespace isc::util;
  29. // static array with factories for options
  30. std::map<unsigned short, Option::Factory*> LibDHCP::v4factories_;
  31. // static array with factories for options
  32. std::map<unsigned short, Option::Factory*> LibDHCP::v6factories_;
  33. // Static container with DHCPv4 option definitions.
  34. OptionDefContainer LibDHCP::v4option_defs_;
  35. // Static container with DHCPv6 option definitions.
  36. OptionDefContainer LibDHCP::v6option_defs_;
  37. VendorOptionDefContainers LibDHCP::vendor4_defs_;
  38. VendorOptionDefContainers LibDHCP::vendor6_defs_;
  39. // Static container with option definitions created in runtime.
  40. StagedValue<OptionDefSpaceContainer> LibDHCP::runtime_option_defs_;
  41. // Those two vendor classes are used for cable modems:
  42. /// DOCSIS3.0 compatible cable modem
  43. const char* isc::dhcp::DOCSIS3_CLASS_MODEM = "docsis3.0";
  44. /// DOCSIS3.0 cable modem that has router built-in
  45. const char* isc::dhcp::DOCSIS3_CLASS_EROUTER = "eRouter1.0";
  46. // Let's keep it in .cc file. Moving it to .h would require including optionDefParams
  47. // definitions there
  48. void initOptionSpace(OptionDefContainer& defs,
  49. const OptionDefParams* params,
  50. size_t params_size);
  51. const OptionDefContainer&
  52. LibDHCP::getOptionDefs(const Option::Universe u) {
  53. switch (u) {
  54. case Option::V4:
  55. if (v4option_defs_.empty()) {
  56. initStdOptionDefs4();
  57. initVendorOptsDocsis4();
  58. }
  59. return (v4option_defs_);
  60. case Option::V6:
  61. if (v6option_defs_.empty()) {
  62. initStdOptionDefs6();
  63. initVendorOptsDocsis6();
  64. }
  65. return (v6option_defs_);
  66. default:
  67. isc_throw(isc::BadValue, "invalid universe " << u << " specified");
  68. }
  69. }
  70. const OptionDefContainer*
  71. LibDHCP::getVendorOption4Defs(const uint32_t vendor_id) {
  72. if (vendor_id == VENDOR_ID_CABLE_LABS &&
  73. vendor4_defs_.find(VENDOR_ID_CABLE_LABS) == vendor4_defs_.end()) {
  74. initVendorOptsDocsis4();
  75. }
  76. VendorOptionDefContainers::const_iterator def = vendor4_defs_.find(vendor_id);
  77. if (def == vendor4_defs_.end()) {
  78. // No such vendor-id space
  79. return (NULL);
  80. }
  81. return (&(def->second));
  82. }
  83. const OptionDefContainer*
  84. LibDHCP::getVendorOption6Defs(const uint32_t vendor_id) {
  85. if (vendor_id == VENDOR_ID_CABLE_LABS &&
  86. vendor6_defs_.find(VENDOR_ID_CABLE_LABS) == vendor6_defs_.end()) {
  87. initVendorOptsDocsis6();
  88. }
  89. if (vendor_id == ENTERPRISE_ID_ISC &&
  90. vendor6_defs_.find(ENTERPRISE_ID_ISC) == vendor6_defs_.end()) {
  91. initVendorOptsIsc6();
  92. }
  93. VendorOptionDefContainers::const_iterator def = vendor6_defs_.find(vendor_id);
  94. if (def == vendor6_defs_.end()) {
  95. // No such vendor-id space
  96. return (NULL);
  97. }
  98. return (&(def->second));
  99. }
  100. OptionDefinitionPtr
  101. LibDHCP::getOptionDef(const Option::Universe u, const uint16_t code) {
  102. const OptionDefContainer& defs = getOptionDefs(u);
  103. const OptionDefContainerTypeIndex& idx = defs.get<1>();
  104. const OptionDefContainerTypeRange& range = idx.equal_range(code);
  105. if (range.first != range.second) {
  106. return (*range.first);
  107. }
  108. return (OptionDefinitionPtr());
  109. }
  110. OptionDefinitionPtr
  111. LibDHCP::getOptionDef(const Option::Universe u, const std::string& name) {
  112. const OptionDefContainer& defs = getOptionDefs(u);
  113. const OptionDefContainerNameIndex& idx = defs.get<2>();
  114. const OptionDefContainerNameRange& range = idx.equal_range(name);
  115. if (range.first != range.second) {
  116. return (*range.first);
  117. }
  118. return (OptionDefinitionPtr());
  119. }
  120. OptionDefinitionPtr
  121. LibDHCP::getVendorOptionDef(const Option::Universe u, const uint32_t vendor_id,
  122. const std::string& name) {
  123. const OptionDefContainer* defs = NULL;
  124. if (u == Option::V4) {
  125. defs = getVendorOption4Defs(vendor_id);
  126. } else if (u == Option::V6) {
  127. defs = getVendorOption6Defs(vendor_id);
  128. }
  129. if (!defs) {
  130. return (OptionDefinitionPtr());
  131. }
  132. const OptionDefContainerNameIndex& idx = defs->get<2>();
  133. const OptionDefContainerNameRange& range = idx.equal_range(name);
  134. if (range.first != range.second) {
  135. return (*range.first);
  136. }
  137. return (OptionDefinitionPtr());
  138. }
  139. OptionDefinitionPtr
  140. LibDHCP::getVendorOptionDef(const Option::Universe u, const uint32_t vendor_id,
  141. const uint16_t code) {
  142. const OptionDefContainer* defs = NULL;
  143. if (u == Option::V4) {
  144. defs = getVendorOption4Defs(vendor_id);
  145. } else if (u == Option::V6) {
  146. defs = getVendorOption6Defs(vendor_id);
  147. }
  148. if (!defs) {
  149. // Weird universe or unknown vendor_id. We don't care. No definitions
  150. // one way or another
  151. // What is it anyway?
  152. return (OptionDefinitionPtr());
  153. }
  154. const OptionDefContainerTypeIndex& idx = defs->get<1>();
  155. const OptionDefContainerTypeRange& range = idx.equal_range(code);
  156. if (range.first != range.second) {
  157. return (*range.first);
  158. }
  159. return (OptionDefinitionPtr());
  160. }
  161. OptionDefinitionPtr
  162. LibDHCP::getRuntimeOptionDef(const std::string& space, const uint16_t code) {
  163. OptionDefContainerPtr container = runtime_option_defs_.getValue().getItems(space);
  164. const OptionDefContainerTypeIndex& index = container->get<1>();
  165. const OptionDefContainerTypeRange& range = index.equal_range(code);
  166. if (range.first != range.second) {
  167. return (*range.first);
  168. }
  169. return (OptionDefinitionPtr());
  170. }
  171. OptionDefinitionPtr
  172. LibDHCP::getRuntimeOptionDef(const std::string& space, const std::string& name) {
  173. OptionDefContainerPtr container = runtime_option_defs_.getValue().getItems(space);
  174. const OptionDefContainerNameIndex& index = container->get<2>();
  175. const OptionDefContainerNameRange& range = index.equal_range(name);
  176. if (range.first != range.second) {
  177. return (*range.first);
  178. }
  179. return (OptionDefinitionPtr());
  180. }
  181. OptionDefContainerPtr
  182. LibDHCP::getRuntimeOptionDefs(const std::string& space) {
  183. return (runtime_option_defs_.getValue().getItems(space));
  184. }
  185. void
  186. LibDHCP::setRuntimeOptionDefs(const OptionDefSpaceContainer& defs) {
  187. OptionDefSpaceContainer defs_copy;
  188. std::list<std::string> option_space_names = defs.getOptionSpaceNames();
  189. for (std::list<std::string>::const_iterator name = option_space_names.begin();
  190. name != option_space_names.end(); ++name) {
  191. OptionDefContainerPtr container = defs.getItems(*name);
  192. for (OptionDefContainer::const_iterator def = container->begin();
  193. def != container->end(); ++def) {
  194. OptionDefinitionPtr def_copy(new OptionDefinition(**def));
  195. defs_copy.addItem(def_copy, *name);
  196. }
  197. }
  198. runtime_option_defs_ = defs_copy;
  199. }
  200. void
  201. LibDHCP::clearRuntimeOptionDefs() {
  202. runtime_option_defs_.reset();
  203. }
  204. void
  205. LibDHCP::revertRuntimeOptionDefs() {
  206. runtime_option_defs_.revert();
  207. }
  208. void
  209. LibDHCP::commitRuntimeOptionDefs() {
  210. runtime_option_defs_.commit();
  211. }
  212. bool
  213. LibDHCP::isStandardOption(const Option::Universe u, const uint16_t code) {
  214. if (u == Option::V6) {
  215. if (code < 79 &&
  216. code != 10 &&
  217. code != 35) {
  218. return (true);
  219. }
  220. } else if (u == Option::V4) {
  221. if (!(code == 84 ||
  222. code == 96 ||
  223. (code > 101 && code < 112) ||
  224. code == 115 ||
  225. code == 126 ||
  226. code == 127 ||
  227. (code > 146 && code < 150) ||
  228. (code > 177 && code < 208) ||
  229. (code > 213 && code < 220) ||
  230. (code > 221 && code < 255))) {
  231. return (true);
  232. }
  233. }
  234. return (false);
  235. }
  236. OptionPtr
  237. LibDHCP::optionFactory(Option::Universe u,
  238. uint16_t type,
  239. const OptionBuffer& buf) {
  240. FactoryMap::iterator it;
  241. if (u == Option::V4) {
  242. it = v4factories_.find(type);
  243. if (it == v4factories_.end()) {
  244. isc_throw(BadValue, "factory function not registered "
  245. "for DHCP v4 option type " << type);
  246. }
  247. } else if (u == Option::V6) {
  248. it = v6factories_.find(type);
  249. if (it == v6factories_.end()) {
  250. isc_throw(BadValue, "factory function not registered "
  251. "for DHCPv6 option type " << type);
  252. }
  253. } else {
  254. isc_throw(BadValue, "invalid universe specified (expected "
  255. "Option::V4 or Option::V6");
  256. }
  257. return (it->second(u, type, buf));
  258. }
  259. size_t LibDHCP::unpackOptions6(const OptionBuffer& buf,
  260. const std::string& option_space,
  261. isc::dhcp::OptionCollection& options,
  262. size_t* relay_msg_offset /* = 0 */,
  263. size_t* relay_msg_len /* = 0 */) {
  264. size_t offset = 0;
  265. size_t length = buf.size();
  266. size_t last_offset = 0;
  267. // Get the list of standard option definitions.
  268. OptionDefContainer option_defs;
  269. if (option_space == "dhcp6") {
  270. option_defs = LibDHCP::getOptionDefs(Option::V6);
  271. } else {
  272. OptionDefContainerPtr option_defs_ptr =
  273. LibDHCP::getRuntimeOptionDefs(option_space);
  274. if (option_defs_ptr) {
  275. option_defs = *option_defs_ptr;
  276. }
  277. }
  278. // @todo Once we implement other option spaces we should add else clause
  279. // here and gather option definitions for them. For now leaving option_defs
  280. // empty will imply creation of generic Option.
  281. // Get the search index #1. It allows to search for option definitions
  282. // using option code.
  283. const OptionDefContainerTypeIndex& idx = option_defs.get<1>();
  284. // The buffer being read comprises a set of options, each starting with
  285. // a two-byte type code and a two-byte length field.
  286. while (offset < length) {
  287. // Save the current offset for backtracking
  288. last_offset = offset;
  289. // Check if there is room for another option
  290. if (offset + 4 > length) {
  291. // Still something but smaller than an option
  292. return (last_offset);
  293. }
  294. // Parse the option header
  295. uint16_t opt_type = isc::util::readUint16(&buf[offset], 2);
  296. offset += 2;
  297. uint16_t opt_len = isc::util::readUint16(&buf[offset], 2);
  298. offset += 2;
  299. if (offset + opt_len > length) {
  300. // We peeked at the option header of the next option, but
  301. // discovered that it would end up beyond buffer end, so
  302. // the option is truncated. Hence we can't parse
  303. // it. Therefore we revert back by those bytes (as if
  304. // we never parsed them).
  305. //
  306. // @note it is the responsibility of the caller to throw
  307. // an exception on partial parsing
  308. return (last_offset);
  309. }
  310. if (opt_type == D6O_RELAY_MSG && relay_msg_offset && relay_msg_len) {
  311. // remember offset of the beginning of the relay-msg option
  312. *relay_msg_offset = offset;
  313. *relay_msg_len = opt_len;
  314. // do not create that relay-msg option
  315. offset += opt_len;
  316. continue;
  317. }
  318. if (opt_type == D6O_VENDOR_OPTS) {
  319. if (offset + 4 > length) {
  320. // Truncated vendor-option. We expect at least
  321. // 4 bytes for the enterprise-id field. Let's roll back
  322. // option code + option length (4 bytes) and return.
  323. return (last_offset);
  324. }
  325. // Parse this as vendor option
  326. OptionPtr vendor_opt(new OptionVendor(Option::V6, buf.begin() + offset,
  327. buf.begin() + offset + opt_len));
  328. options.insert(std::make_pair(opt_type, vendor_opt));
  329. offset += opt_len;
  330. continue;
  331. }
  332. // Get all definitions with the particular option code. Note
  333. // that option code is non-unique within this container
  334. // however at this point we expect to get one option
  335. // definition with the particular code. If more are returned
  336. // we report an error.
  337. const OptionDefContainerTypeRange& range = idx.equal_range(opt_type);
  338. // Get the number of returned option definitions for the option code.
  339. size_t num_defs = distance(range.first, range.second);
  340. OptionPtr opt;
  341. if (num_defs > 1) {
  342. // Multiple options of the same code are not supported right now!
  343. isc_throw(isc::Unexpected, "Internal error: multiple option"
  344. " definitions for option type " << opt_type <<
  345. " returned. Currently it is not supported to initialize"
  346. " multiple option definitions for the same option code."
  347. " This will be supported once support for option spaces"
  348. " is implemented");
  349. } else if (num_defs == 0) {
  350. // @todo Don't crash if definition does not exist because
  351. // only a few option definitions are initialized right
  352. // now. In the future we will initialize definitions for
  353. // all options and we will remove this elseif. For now,
  354. // return generic option.
  355. opt = OptionPtr(new Option(Option::V6, opt_type,
  356. buf.begin() + offset,
  357. buf.begin() + offset + opt_len));
  358. } else {
  359. // The option definition has been found. Use it to create
  360. // the option instance from the provided buffer chunk.
  361. const OptionDefinitionPtr& def = *(range.first);
  362. assert(def);
  363. opt = def->optionFactory(Option::V6, opt_type,
  364. buf.begin() + offset,
  365. buf.begin() + offset + opt_len);
  366. }
  367. // add option to options
  368. options.insert(std::make_pair(opt_type, opt));
  369. offset += opt_len;
  370. }
  371. last_offset = offset;
  372. return (last_offset);
  373. }
  374. size_t LibDHCP::unpackOptions4(const OptionBuffer& buf,
  375. const std::string& option_space,
  376. isc::dhcp::OptionCollection& options) {
  377. size_t offset = 0;
  378. size_t last_offset = 0;
  379. // Get the list of standard option definitions.
  380. OptionDefContainer option_defs;
  381. if (option_space == "dhcp4") {
  382. option_defs = LibDHCP::getOptionDefs(Option::V4);
  383. }
  384. // @todo Once we implement other option spaces we should add else clause
  385. // here and gather option definitions for them. For now leaving option_defs
  386. // empty will imply creation of generic Option.
  387. // Get the search index #1. It allows to search for option definitions
  388. // using option code.
  389. const OptionDefContainerTypeIndex& idx = option_defs.get<1>();
  390. // The buffer being read comprises a set of options, each starting with
  391. // a one-byte type code and a one-byte length field.
  392. while (offset < buf.size()) {
  393. // Save the current offset for backtracking
  394. last_offset = offset;
  395. // Get the option type
  396. uint8_t opt_type = buf[offset++];
  397. // DHO_END is a special, one octet long option
  398. if (opt_type == DHO_END) {
  399. // just return. Don't need to add DHO_END option
  400. // Don't return offset because it makes this condition
  401. // and partial parsing impossible to recognize.
  402. return (last_offset);
  403. }
  404. // DHO_PAD is just a padding after DHO_END. Let's continue parsing
  405. // in case we receive a message without DHO_END.
  406. if (opt_type == DHO_PAD)
  407. continue;
  408. if (offset + 1 > buf.size()) {
  409. // We peeked at the option header of the next option, but
  410. // discovered that it would end up beyond buffer end, so
  411. // the option is truncated. Hence we can't parse
  412. // it. Therefore we revert back (as if we never parsed it).
  413. //
  414. // @note it is the responsibility of the caller to throw
  415. // an exception on partial parsing
  416. return (last_offset);
  417. }
  418. uint8_t opt_len = buf[offset++];
  419. if (offset + opt_len > buf.size()) {
  420. // We peeked at the option header of the next option, but
  421. // discovered that it would end up beyond buffer end, so
  422. // the option is truncated. Hence we can't parse
  423. // it. Therefore we revert back (as if we never parsed it).
  424. return (last_offset);
  425. }
  426. // Get all definitions with the particular option code. Note
  427. // that option code is non-unique within this container
  428. // however at this point we expect to get one option
  429. // definition with the particular code. If more are returned
  430. // we report an error.
  431. const OptionDefContainerTypeRange& range = idx.equal_range(opt_type);
  432. // Get the number of returned option definitions for the option code.
  433. size_t num_defs = distance(range.first, range.second);
  434. OptionPtr opt;
  435. if (num_defs > 1) {
  436. // Multiple options of the same code are not supported right now!
  437. isc_throw(isc::Unexpected, "Internal error: multiple option"
  438. " definitions for option type " <<
  439. static_cast<int>(opt_type) <<
  440. " returned. Currently it is not supported to initialize"
  441. " multiple option definitions for the same option code."
  442. " This will be supported once support for option spaces"
  443. " is implemented");
  444. } else if (num_defs == 0) {
  445. opt = OptionPtr(new Option(Option::V4, opt_type,
  446. buf.begin() + offset,
  447. buf.begin() + offset + opt_len));
  448. } else {
  449. // The option definition has been found. Use it to create
  450. // the option instance from the provided buffer chunk.
  451. const OptionDefinitionPtr& def = *(range.first);
  452. assert(def);
  453. opt = def->optionFactory(Option::V4, opt_type,
  454. buf.begin() + offset,
  455. buf.begin() + offset + opt_len);
  456. }
  457. options.insert(std::make_pair(opt_type, opt));
  458. offset += opt_len;
  459. }
  460. last_offset = offset;
  461. return (last_offset);
  462. }
  463. size_t LibDHCP::unpackVendorOptions6(const uint32_t vendor_id,
  464. const OptionBuffer& buf,
  465. isc::dhcp::OptionCollection& options) {
  466. size_t offset = 0;
  467. size_t length = buf.size();
  468. // Get the list of option definitions for this particular vendor-id
  469. const OptionDefContainer* option_defs =
  470. LibDHCP::getVendorOption6Defs(vendor_id);
  471. // Get the search index #1. It allows to search for option definitions
  472. // using option code. If there's no such vendor-id space, we're out of luck
  473. // anyway.
  474. const OptionDefContainerTypeIndex* idx = NULL;
  475. if (option_defs) {
  476. idx = &(option_defs->get<1>());
  477. }
  478. // The buffer being read comprises a set of options, each starting with
  479. // a two-byte type code and a two-byte length field.
  480. while (offset < length) {
  481. if (offset + 4 > length) {
  482. isc_throw(OutOfRange,
  483. "Vendor option parse failed: truncated header");
  484. }
  485. uint16_t opt_type = isc::util::readUint16(&buf[offset], 2);
  486. offset += 2;
  487. uint16_t opt_len = isc::util::readUint16(&buf[offset], 2);
  488. offset += 2;
  489. if (offset + opt_len > length) {
  490. isc_throw(OutOfRange, "Vendor option parse failed. Tried to parse "
  491. << offset + opt_len << " bytes from " << length
  492. << "-byte long buffer.");
  493. }
  494. OptionPtr opt;
  495. opt.reset();
  496. // If there is a definition for such a vendor option...
  497. if (idx) {
  498. // Get all definitions with the particular option
  499. // code. Note that option code is non-unique within this
  500. // container however at this point we expect to get one
  501. // option definition with the particular code. If more are
  502. // returned we report an error.
  503. const OptionDefContainerTypeRange& range =
  504. idx->equal_range(opt_type);
  505. // Get the number of returned option definitions for the
  506. // option code.
  507. size_t num_defs = distance(range.first, range.second);
  508. if (num_defs > 1) {
  509. // Multiple options of the same code are not supported
  510. // right now!
  511. isc_throw(isc::Unexpected, "Internal error: multiple option"
  512. " definitions for option type " << opt_type <<
  513. " returned. Currently it is not supported to"
  514. " initialize multiple option definitions for the"
  515. " same option code. This will be supported once"
  516. " support for option spaces is implemented");
  517. } else if (num_defs == 1) {
  518. // The option definition has been found. Use it to create
  519. // the option instance from the provided buffer chunk.
  520. const OptionDefinitionPtr& def = *(range.first);
  521. assert(def);
  522. opt = def->optionFactory(Option::V6, opt_type,
  523. buf.begin() + offset,
  524. buf.begin() + offset + opt_len);
  525. }
  526. }
  527. // This can happen in one of 2 cases:
  528. // 1. we do not have definitions for that vendor-space
  529. // 2. we do have definitions, but that particular option was
  530. // not defined
  531. if (!opt) {
  532. opt = OptionPtr(new Option(Option::V6, opt_type,
  533. buf.begin() + offset,
  534. buf.begin() + offset + opt_len));
  535. }
  536. // add option to options
  537. if (opt) {
  538. options.insert(std::make_pair(opt_type, opt));
  539. }
  540. offset += opt_len;
  541. }
  542. return (offset);
  543. }
  544. size_t LibDHCP::unpackVendorOptions4(const uint32_t vendor_id, const OptionBuffer& buf,
  545. isc::dhcp::OptionCollection& options) {
  546. size_t offset = 0;
  547. // Get the list of stdandard option definitions.
  548. const OptionDefContainer* option_defs =
  549. LibDHCP::getVendorOption4Defs(vendor_id);
  550. // Get the search index #1. It allows to search for option definitions
  551. // using option code.
  552. const OptionDefContainerTypeIndex* idx = NULL;
  553. if (option_defs) {
  554. idx = &(option_defs->get<1>());
  555. }
  556. // The buffer being read comprises a set of options, each starting with
  557. // a one-byte type code and a one-byte length field.
  558. while (offset < buf.size()) {
  559. // Note that Vendor-Specific info option (RFC3925) has a
  560. // different option format than Vendor-Spec info for
  561. // DHCPv6. (there's additional layer of data-length)
  562. uint8_t data_len = buf[offset++];
  563. if (offset + data_len > buf.size()) {
  564. // The option is truncated.
  565. isc_throw(OutOfRange, "Attempt to parse truncated vendor option");
  566. }
  567. uint8_t offset_end = offset + data_len;
  568. // beginning of data-chunk parser
  569. while (offset < offset_end) {
  570. uint8_t opt_type = buf[offset++];
  571. // No DHO_END or DHO_PAD in vendor options
  572. if (offset + 1 > offset_end) {
  573. // opt_type must be cast to integer so as it is not
  574. // treated as unsigned char value (a number is
  575. // presented in error message).
  576. isc_throw(OutOfRange,
  577. "Attempt to parse truncated vendor option "
  578. << static_cast<int>(opt_type));
  579. }
  580. uint8_t opt_len = buf[offset++];
  581. if (offset + opt_len > offset_end) {
  582. isc_throw(OutOfRange, "Option parse failed. Tried to parse "
  583. << offset + opt_len << " bytes from " << buf.size()
  584. << "-byte long buffer.");
  585. }
  586. OptionPtr opt;
  587. opt.reset();
  588. if (idx) {
  589. // Get all definitions with the particular option
  590. // code. Note that option code is non-unique within
  591. // this container however at this point we expect to
  592. // get one option definition with the particular
  593. // code. If more are returned we report an error.
  594. const OptionDefContainerTypeRange& range =
  595. idx->equal_range(opt_type);
  596. // Get the number of returned option definitions for
  597. // the option code.
  598. size_t num_defs = distance(range.first, range.second);
  599. if (num_defs > 1) {
  600. // Multiple options of the same code are not
  601. // supported right now!
  602. isc_throw(isc::Unexpected, "Internal error: multiple"
  603. " option definitions for option type "
  604. << opt_type << " returned. Currently it is"
  605. " not supported to initialize multiple option"
  606. " definitions for the same option code."
  607. " This will be supported once support for"
  608. " option spaces is implemented");
  609. } else if (num_defs == 1) {
  610. // The option definition has been found. Use it to create
  611. // the option instance from the provided buffer chunk.
  612. const OptionDefinitionPtr& def = *(range.first);
  613. assert(def);
  614. opt = def->optionFactory(Option::V4, opt_type,
  615. buf.begin() + offset,
  616. buf.begin() + offset + opt_len);
  617. }
  618. }
  619. if (!opt) {
  620. opt = OptionPtr(new Option(Option::V4, opt_type,
  621. buf.begin() + offset,
  622. buf.begin() + offset + opt_len));
  623. }
  624. options.insert(std::make_pair(opt_type, opt));
  625. offset += opt_len;
  626. } // end of data-chunk
  627. break; // end of the vendor block.
  628. }
  629. return (offset);
  630. }
  631. void
  632. LibDHCP::packOptions4(isc::util::OutputBuffer& buf,
  633. const OptionCollection& options) {
  634. OptionPtr agent;
  635. OptionPtr end;
  636. for (OptionCollection::const_iterator it = options.begin();
  637. it != options.end(); ++it) {
  638. // RAI and END options must be last.
  639. switch (it->first) {
  640. case DHO_DHCP_AGENT_OPTIONS:
  641. agent = it->second;
  642. break;
  643. case DHO_END:
  644. end = it->second;
  645. break;
  646. default:
  647. it->second->pack(buf);
  648. break;
  649. }
  650. }
  651. // Add the RAI option if it exists.
  652. if (agent) {
  653. agent->pack(buf);
  654. }
  655. // And at the end the END option.
  656. if (end) {
  657. end->pack(buf);
  658. }
  659. }
  660. void
  661. LibDHCP::packOptions6(isc::util::OutputBuffer& buf,
  662. const OptionCollection& options) {
  663. for (OptionCollection::const_iterator it = options.begin();
  664. it != options.end(); ++it) {
  665. it->second->pack(buf);
  666. }
  667. }
  668. void LibDHCP::OptionFactoryRegister(Option::Universe u,
  669. uint16_t opt_type,
  670. Option::Factory* factory) {
  671. switch (u) {
  672. case Option::V6: {
  673. if (v6factories_.find(opt_type) != v6factories_.end()) {
  674. isc_throw(BadValue, "There is already DHCPv6 factory registered "
  675. << "for option type " << opt_type);
  676. }
  677. v6factories_[opt_type]=factory;
  678. return;
  679. }
  680. case Option::V4:
  681. {
  682. // Option 0 is special (a one octet-long, equal 0) PAD option. It is never
  683. // instantiated as an Option object, but rather consumed during packet parsing.
  684. if (opt_type == 0) {
  685. isc_throw(BadValue, "Cannot redefine PAD option (code=0)");
  686. }
  687. // Option 255 is never instantiated as an option object. It is special
  688. // (a one-octet equal 255) option that is added at the end of all options
  689. // during packet assembly. It is also silently consumed during packet parsing.
  690. if (opt_type > 254) {
  691. isc_throw(BadValue, "Too big option type for DHCPv4, only 0-254 allowed.");
  692. }
  693. if (v4factories_.find(opt_type)!=v4factories_.end()) {
  694. isc_throw(BadValue, "There is already DHCPv4 factory registered "
  695. << "for option type " << opt_type);
  696. }
  697. v4factories_[opt_type]=factory;
  698. return;
  699. }
  700. default:
  701. isc_throw(BadValue, "Invalid universe type specified.");
  702. }
  703. return;
  704. }
  705. void
  706. LibDHCP::initStdOptionDefs4() {
  707. initOptionSpace(v4option_defs_, OPTION_DEF_PARAMS4, OPTION_DEF_PARAMS_SIZE4);
  708. }
  709. void
  710. LibDHCP::initStdOptionDefs6() {
  711. initOptionSpace(v6option_defs_, OPTION_DEF_PARAMS6, OPTION_DEF_PARAMS_SIZE6);
  712. }
  713. void
  714. LibDHCP::initVendorOptsDocsis4() {
  715. initOptionSpace(vendor4_defs_[VENDOR_ID_CABLE_LABS], DOCSIS3_V4_DEFS, DOCSIS3_V4_DEFS_SIZE);
  716. }
  717. void
  718. LibDHCP::initVendorOptsDocsis6() {
  719. vendor6_defs_[VENDOR_ID_CABLE_LABS] = OptionDefContainer();
  720. initOptionSpace(vendor6_defs_[VENDOR_ID_CABLE_LABS], DOCSIS3_V6_DEFS, DOCSIS3_V6_DEFS_SIZE);
  721. }
  722. void
  723. LibDHCP::initVendorOptsIsc6() {
  724. vendor6_defs_[ENTERPRISE_ID_ISC] = OptionDefContainer();
  725. initOptionSpace(vendor6_defs_[ENTERPRISE_ID_ISC], ISC_V6_DEFS, ISC_V6_DEFS_SIZE);
  726. }
  727. uint32_t
  728. LibDHCP::optionSpaceToVendorId(const std::string& option_space) {
  729. // 8 is a minimal length of "vendor-X" format
  730. if ((option_space.size() < 8) || (option_space.substr(0,7) != "vendor-")) {
  731. return (0);
  732. }
  733. int64_t check;
  734. try {
  735. // text after "vendor-", supposedly numbers only
  736. std::string x = option_space.substr(7);
  737. check = boost::lexical_cast<int64_t>(x);
  738. } catch (const boost::bad_lexical_cast &) {
  739. return (0);
  740. }
  741. if ((check < 0) || (check > std::numeric_limits<uint32_t>::max())) {
  742. return (0);
  743. }
  744. // value is small enough to fit
  745. return (static_cast<uint32_t>(check));
  746. }
  747. void initOptionSpace(OptionDefContainer& defs,
  748. const OptionDefParams* params,
  749. size_t params_size) {
  750. defs.clear();
  751. for (size_t i = 0; i < params_size; ++i) {
  752. std::string encapsulates(params[i].encapsulates);
  753. if (!encapsulates.empty() && params[i].array) {
  754. isc_throw(isc::BadValue, "invalid standard option definition: "
  755. << "option with code '" << params[i].code
  756. << "' may not encapsulate option space '"
  757. << encapsulates << "' because the definition"
  758. << " indicates that this option comprises an array"
  759. << " of values");
  760. }
  761. // Depending whether an option encapsulates an option space or not
  762. // we pick different constructor to create an instance of the option
  763. // definition.
  764. OptionDefinitionPtr definition;
  765. if (encapsulates.empty()) {
  766. // Option does not encapsulate any option space.
  767. definition.reset(new OptionDefinition(params[i].name,
  768. params[i].code,
  769. params[i].type,
  770. params[i].array));
  771. } else {
  772. // Option does encapsulate an option space.
  773. definition.reset(new OptionDefinition(params[i].name,
  774. params[i].code,
  775. params[i].type,
  776. params[i].encapsulates));
  777. }
  778. for (size_t rec = 0; rec < params[i].records_size; ++rec) {
  779. definition->addRecordField(params[i].records[rec]);
  780. }
  781. try {
  782. definition->validate();
  783. } catch (const isc::Exception&) {
  784. // This is unlikely event that validation fails and may
  785. // be only caused by programming error. To guarantee the
  786. // data consistency we clear all option definitions that
  787. // have been added so far and pass the exception forward.
  788. defs.clear();
  789. throw;
  790. }
  791. defs.push_back(definition);
  792. }
  793. }