libdhcp++.cc 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. // Copyright (C) 2011-2012 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // Permission to use, copy, modify, and/or distribute this software for any
  4. // purpose with or without fee is hereby granted, provided that the above
  5. // copyright notice and this permission notice appear in all copies.
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  8. // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  9. // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  10. // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  11. // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  12. // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  13. // PERFORMANCE OF THIS SOFTWARE.
  14. #include <config.h>
  15. #include <dhcp/dhcp4.h>
  16. #include <dhcp/dhcp6.h>
  17. #include <dhcp/libdhcp++.h>
  18. #include <dhcp/option.h>
  19. #include <dhcp/option6_ia.h>
  20. #include <dhcp/option6_iaaddr.h>
  21. #include <dhcp/option_definition.h>
  22. #include <dhcp/option_int_array.h>
  23. #include <dhcp/std_option_defs.h>
  24. #include <exceptions/exceptions.h>
  25. #include <util/buffer.h>
  26. #include <boost/shared_array.hpp>
  27. #include <boost/shared_ptr.hpp>
  28. using namespace std;
  29. using namespace isc::dhcp;
  30. using namespace isc::util;
  31. // static array with factories for options
  32. std::map<unsigned short, Option::Factory*> LibDHCP::v4factories_;
  33. // static array with factories for options
  34. std::map<unsigned short, Option::Factory*> LibDHCP::v6factories_;
  35. // Static container with DHCPv4 option definitions.
  36. OptionDefContainer LibDHCP::v4option_defs_;
  37. // Static container with DHCPv6 option definitions.
  38. OptionDefContainer LibDHCP::v6option_defs_;
  39. const OptionDefContainer&
  40. LibDHCP::getOptionDefs(const Option::Universe u) {
  41. switch (u) {
  42. case Option::V4:
  43. if (v4option_defs_.empty()) {
  44. initStdOptionDefs4();
  45. }
  46. return (v4option_defs_);
  47. case Option::V6:
  48. if (v6option_defs_.empty()) {
  49. initStdOptionDefs6();
  50. }
  51. return (v6option_defs_);
  52. default:
  53. isc_throw(isc::BadValue, "invalid universe " << u << " specified");
  54. }
  55. }
  56. OptionDefinitionPtr
  57. LibDHCP::getOptionDef(const Option::Universe u, const uint16_t code) {
  58. const OptionDefContainer& defs = getOptionDefs(u);
  59. const OptionDefContainerTypeIndex& idx = defs.get<1>();
  60. const OptionDefContainerTypeRange& range = idx.equal_range(code);
  61. if (range.first != range.second) {
  62. return (*range.first);
  63. }
  64. return (OptionDefinitionPtr());
  65. }
  66. bool
  67. LibDHCP::isStandardOption(const Option::Universe u, const uint16_t code) {
  68. if (u == Option::V6) {
  69. if (code < 79 &&
  70. code != 10 &&
  71. code != 35) {
  72. return (true);
  73. }
  74. } else if (u == Option::V4) {
  75. if (!(code == 84 ||
  76. code == 96 ||
  77. (code > 101 && code < 112) ||
  78. code == 115 ||
  79. code == 126 ||
  80. code == 127 ||
  81. (code > 146 && code < 150) ||
  82. (code > 177 && code < 208) ||
  83. (code > 213 && code < 220) ||
  84. (code > 221 && code < 224))) {
  85. return (true);
  86. }
  87. }
  88. return (false);
  89. }
  90. OptionPtr
  91. LibDHCP::optionFactory(Option::Universe u,
  92. uint16_t type,
  93. const OptionBuffer& buf) {
  94. FactoryMap::iterator it;
  95. if (u == Option::V4) {
  96. it = v4factories_.find(type);
  97. if (it == v4factories_.end()) {
  98. isc_throw(BadValue, "factory function not registered "
  99. "for DHCP v4 option type " << type);
  100. }
  101. } else if (u == Option::V6) {
  102. it = v6factories_.find(type);
  103. if (it == v6factories_.end()) {
  104. isc_throw(BadValue, "factory function not registered "
  105. "for DHCPv6 option type " << type);
  106. }
  107. } else {
  108. isc_throw(BadValue, "invalid universe specified (expected "
  109. "Option::V4 or Option::V6");
  110. }
  111. return (it->second(u, type, buf));
  112. }
  113. size_t LibDHCP::unpackOptions6(const OptionBuffer& buf,
  114. isc::dhcp::Option::OptionCollection& options) {
  115. size_t offset = 0;
  116. size_t length = buf.size();
  117. // Get the list of stdandard option definitions.
  118. const OptionDefContainer& option_defs = LibDHCP::getOptionDefs(Option::V6);
  119. // Get the search index #1. It allows to search for option definitions
  120. // using option code.
  121. const OptionDefContainerTypeIndex& idx = option_defs.get<1>();
  122. // The buffer being read comprises a set of options, each starting with
  123. // a two-byte type code and a two-byte length field.
  124. while (offset + 4 <= length) {
  125. uint16_t opt_type = isc::util::readUint16(&buf[offset]);
  126. offset += 2;
  127. uint16_t opt_len = isc::util::readUint16(&buf[offset]);
  128. offset += 2;
  129. if (offset + opt_len > length) {
  130. // @todo: consider throwing exception here.
  131. return (offset);
  132. }
  133. // Get all definitions with the particular option code. Note that option
  134. // code is non-unique within this container however at this point we
  135. // expect to get one option definition with the particular code. If more
  136. // are returned we report an error.
  137. const OptionDefContainerTypeRange& range = idx.equal_range(opt_type);
  138. // Get the number of returned option definitions for the option code.
  139. size_t num_defs = distance(range.first, range.second);
  140. OptionPtr opt;
  141. if (num_defs > 1) {
  142. // Multiple options of the same code are not supported right now!
  143. isc_throw(isc::Unexpected, "Internal error: multiple option definitions"
  144. " for option type " << opt_type << " returned. Currently it is not"
  145. " supported to initialize multiple option definitions"
  146. " for the same option code. This will be supported once"
  147. " support for option spaces is implemented");
  148. } else if (num_defs == 0) {
  149. // @todo Don't crash if definition does not exist because only a few
  150. // option definitions are initialized right now. In the future
  151. // we will initialize definitions for all options and we will
  152. // remove this elseif. For now, return generic option.
  153. opt = OptionPtr(new Option(Option::V6, opt_type,
  154. buf.begin() + offset,
  155. buf.begin() + offset + opt_len));
  156. } else {
  157. // The option definition has been found. Use it to create
  158. // the option instance from the provided buffer chunk.
  159. const OptionDefinitionPtr& def = *(range.first);
  160. assert(def);
  161. opt = def->optionFactory(Option::V6, opt_type,
  162. buf.begin() + offset,
  163. buf.begin() + offset + opt_len);
  164. }
  165. // add option to options
  166. options.insert(std::make_pair(opt_type, opt));
  167. offset += opt_len;
  168. }
  169. return (offset);
  170. }
  171. size_t LibDHCP::unpackOptions4(const OptionBuffer& buf,
  172. isc::dhcp::Option::OptionCollection& options) {
  173. size_t offset = 0;
  174. // Get the list of stdandard option definitions.
  175. const OptionDefContainer& option_defs = LibDHCP::getOptionDefs(Option::V4);
  176. // Get the search index #1. It allows to search for option definitions
  177. // using option code.
  178. const OptionDefContainerTypeIndex& idx = option_defs.get<1>();
  179. // The buffer being read comprises a set of options, each starting with
  180. // a one-byte type code and a one-byte length field.
  181. while (offset + 1 <= buf.size()) {
  182. uint8_t opt_type = buf[offset++];
  183. // DHO_END is a special, one octet long option
  184. if (opt_type == DHO_END)
  185. return (offset); // just return. Don't need to add DHO_END option
  186. // DHO_PAD is just a padding after DHO_END. Let's continue parsing
  187. // in case we receive a message without DHO_END.
  188. if (opt_type == DHO_PAD)
  189. continue;
  190. if (offset + 1 >= buf.size()) {
  191. // opt_type must be cast to integer so as it is not treated as
  192. // unsigned char value (a number is presented in error message).
  193. isc_throw(OutOfRange, "Attempt to parse truncated option "
  194. << static_cast<int>(opt_type));
  195. }
  196. uint8_t opt_len = buf[offset++];
  197. if (offset + opt_len > buf.size()) {
  198. isc_throw(OutOfRange, "Option parse failed. Tried to parse "
  199. << offset + opt_len << " bytes from " << buf.size()
  200. << "-byte long buffer.");
  201. }
  202. // Get all definitions with the particular option code. Note that option code
  203. // is non-unique within this container however at this point we expect
  204. // to get one option definition with the particular code. If more are
  205. // returned we report an error.
  206. const OptionDefContainerTypeRange& range = idx.equal_range(opt_type);
  207. // Get the number of returned option definitions for the option code.
  208. size_t num_defs = distance(range.first, range.second);
  209. OptionPtr opt;
  210. if (num_defs > 1) {
  211. // Multiple options of the same code are not supported right now!
  212. isc_throw(isc::Unexpected, "Internal error: multiple option definitions"
  213. " for option type " << static_cast<int>(opt_type)
  214. << " returned. Currently it is not supported to initialize"
  215. << " multiple option definitions for the same option code."
  216. << " This will be supported once support for option spaces"
  217. << " is implemented");
  218. } else if (num_defs == 0) {
  219. opt = OptionPtr(new Option(Option::V4, opt_type,
  220. buf.begin() + offset,
  221. buf.begin() + offset + opt_len));
  222. } else {
  223. // The option definition has been found. Use it to create
  224. // the option instance from the provided buffer chunk.
  225. const OptionDefinitionPtr& def = *(range.first);
  226. assert(def);
  227. opt = def->optionFactory(Option::V4, opt_type,
  228. buf.begin() + offset,
  229. buf.begin() + offset + opt_len);
  230. }
  231. options.insert(std::make_pair(opt_type, opt));
  232. offset += opt_len;
  233. }
  234. return (offset);
  235. }
  236. void LibDHCP::packOptions6(isc::util::OutputBuffer &buf,
  237. const isc::dhcp::Option::OptionCollection& options) {
  238. for (Option::OptionCollection::const_iterator it = options.begin();
  239. it != options.end(); ++it) {
  240. it->second->pack(buf);
  241. }
  242. }
  243. void
  244. LibDHCP::packOptions(isc::util::OutputBuffer& buf,
  245. const Option::OptionCollection& options) {
  246. for (Option::OptionCollection::const_iterator it = options.begin();
  247. it != options.end(); ++it) {
  248. it->second->pack4(buf);
  249. }
  250. }
  251. void LibDHCP::OptionFactoryRegister(Option::Universe u,
  252. uint16_t opt_type,
  253. Option::Factory* factory) {
  254. switch (u) {
  255. case Option::V6: {
  256. if (v6factories_.find(opt_type) != v6factories_.end()) {
  257. isc_throw(BadValue, "There is already DHCPv6 factory registered "
  258. << "for option type " << opt_type);
  259. }
  260. v6factories_[opt_type]=factory;
  261. return;
  262. }
  263. case Option::V4:
  264. {
  265. // Option 0 is special (a one octet-long, equal 0) PAD option. It is never
  266. // instantiated as an Option object, but rather consumed during packet parsing.
  267. if (opt_type == 0) {
  268. isc_throw(BadValue, "Cannot redefine PAD option (code=0)");
  269. }
  270. // Option 255 is never instantiated as an option object. It is special
  271. // (a one-octet equal 255) option that is added at the end of all options
  272. // during packet assembly. It is also silently consumed during packet parsing.
  273. if (opt_type > 254) {
  274. isc_throw(BadValue, "Too big option type for DHCPv4, only 0-254 allowed.");
  275. }
  276. if (v4factories_.find(opt_type)!=v4factories_.end()) {
  277. isc_throw(BadValue, "There is already DHCPv4 factory registered "
  278. << "for option type " << opt_type);
  279. }
  280. v4factories_[opt_type]=factory;
  281. return;
  282. }
  283. default:
  284. isc_throw(BadValue, "Invalid universe type specified.");
  285. }
  286. return;
  287. }
  288. void
  289. LibDHCP::initStdOptionDefs4() {
  290. v4option_defs_.clear();
  291. // Now let's add all option definitions.
  292. for (int i = 0; i < OPTION_DEF_PARAMS_SIZE4; ++i) {
  293. OptionDefinitionPtr definition(new OptionDefinition(OPTION_DEF_PARAMS4[i].name,
  294. OPTION_DEF_PARAMS4[i].code,
  295. OPTION_DEF_PARAMS4[i].type,
  296. OPTION_DEF_PARAMS4[i].array));
  297. for (int rec = 0; rec < OPTION_DEF_PARAMS4[i].records_size; ++rec) {
  298. definition->addRecordField(OPTION_DEF_PARAMS4[i].records[rec]);
  299. }
  300. // Sanity check if the option is valid.
  301. try {
  302. definition->validate();
  303. } catch (const Exception& ex) {
  304. // This is unlikely event that validation fails and may
  305. // be only caused by programming error. To guarantee the
  306. // data consistency we clear all option definitions that
  307. // have been added so far and pass the exception forward.
  308. v4option_defs_.clear();
  309. throw;
  310. }
  311. v4option_defs_.push_back(definition);
  312. }
  313. }
  314. void
  315. LibDHCP::initStdOptionDefs6() {
  316. v6option_defs_.clear();
  317. for (int i = 0; i < OPTION_DEF_PARAMS_SIZE6; ++i) {
  318. OptionDefinitionPtr definition(new OptionDefinition(OPTION_DEF_PARAMS6[i].name,
  319. OPTION_DEF_PARAMS6[i].code,
  320. OPTION_DEF_PARAMS6[i].type,
  321. OPTION_DEF_PARAMS6[i].array));
  322. for (int rec = 0; rec < OPTION_DEF_PARAMS6[i].records_size; ++rec) {
  323. definition->addRecordField(OPTION_DEF_PARAMS6[i].records[rec]);
  324. }
  325. try {
  326. definition->validate();
  327. } catch (const Exception& ex) {
  328. // This is unlikely event that validation fails and may
  329. // be only caused by programming error. To guarantee the
  330. // data consistency we clear all option definitions that
  331. // have been added so far and pass the exception forward.
  332. v6option_defs_.clear();
  333. throw;
  334. }
  335. v6option_defs_.push_back(definition);
  336. }
  337. }