libdhcp++.cc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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/option6_int_array.h>
  22. #include <dhcp/option_definition.h>
  23. #include <exceptions/exceptions.h>
  24. #include <util/buffer.h>
  25. #include <boost/shared_array.hpp>
  26. #include <boost/shared_ptr.hpp>
  27. using namespace std;
  28. using namespace isc::dhcp;
  29. using namespace isc::util;
  30. // static array with factories for options
  31. std::map<unsigned short, Option::Factory*> LibDHCP::v4factories_;
  32. // static array with factories for options
  33. std::map<unsigned short, Option::Factory*> LibDHCP::v6factories_;
  34. // Static container with DHCPv4 option definitions.
  35. OptionDefContainer LibDHCP::v4option_defs_;
  36. // Static container with DHCPv6 option definitions.
  37. OptionDefContainer LibDHCP::v6option_defs_;
  38. const OptionDefContainer&
  39. LibDHCP::getOptionDefs(Option::Universe u) {
  40. switch (u) {
  41. case Option::V4:
  42. initStdOptionDefs4();
  43. return (v4option_defs_);
  44. case Option::V6:
  45. if (v6option_defs_.size() == 0) {
  46. initStdOptionDefs6();
  47. }
  48. return (v6option_defs_);
  49. default:
  50. isc_throw(isc::BadValue, "invalid universe " << u << " specified");
  51. }
  52. }
  53. OptionPtr
  54. LibDHCP::optionFactory(Option::Universe u,
  55. uint16_t type,
  56. const OptionBuffer& buf) {
  57. FactoryMap::iterator it;
  58. if (u == Option::V4) {
  59. it = v4factories_.find(type);
  60. if (it == v4factories_.end()) {
  61. isc_throw(BadValue, "factory function not registered "
  62. "for DHCP v4 option type " << type);
  63. }
  64. } else if (u == Option::V6) {
  65. it = v6factories_.find(type);
  66. if (it == v6factories_.end()) {
  67. isc_throw(BadValue, "factory function not registered "
  68. "for DHCPv6 option type " << type);
  69. }
  70. } else {
  71. isc_throw(BadValue, "invalid universe specified (expected "
  72. "Option::V4 or Option::V6");
  73. }
  74. return (it->second(u, type, buf));
  75. }
  76. size_t LibDHCP::unpackOptions6(const OptionBuffer& buf,
  77. isc::dhcp::Option::OptionCollection& options) {
  78. size_t offset = 0;
  79. size_t end = buf.size();
  80. while (offset +4 <= end) {
  81. uint16_t opt_type = buf[offset] * 256 + buf[offset + 1];
  82. offset += 2;
  83. uint16_t opt_len = buf[offset] * 256 + buf[offset + 1];
  84. offset += 2;
  85. if (offset + opt_len > end) {
  86. // @todo: consider throwing exception here.
  87. return (offset);
  88. }
  89. // Get the list of stdandard option definitions.
  90. OptionDefContainer option_defs = LibDHCP::getOptionDefs(Option::V6);
  91. // Get the search index #1. It allows to search for option definitions
  92. // using option code.
  93. const OptionDefContainerTypeIndex& idx = option_defs.get<1>();
  94. // Get all options with the particular option code. Note that option code
  95. // is non-unique within this container however at this point we expect
  96. // to get one option definition with the particular code. If more are
  97. // returned we report an error.
  98. const OptionDefContainerTypeRange& range = idx.equal_range(opt_type);
  99. // Get the number of returned option definitions for the option code.
  100. size_t num_defs = distance(range.first, range.second);
  101. OptionPtr opt;
  102. if (num_defs > 1) {
  103. // Multiple options of the same code are not supported right now!
  104. isc_throw(isc::Unexpected, "Internal error: multiple option definitions"
  105. " for option type " << opt_type << " returned. Currently it is not"
  106. " supported to initialize multiple option definitions"
  107. " for the same option code. This will be supported once"
  108. " support for option spaces is implemented");
  109. } else if (num_defs == 0) {
  110. // @todo Don't crash if definition does not exist because only a few
  111. // option definitions are initialized right now. In the future
  112. // we will initialize definitions for all options and we will
  113. // remove this elseif. For now, return generic option.
  114. opt = OptionPtr(new Option(Option::V6, opt_type,
  115. buf.begin() + offset,
  116. buf.begin() + offset + opt_len));
  117. } else {
  118. // The option definition has been found. Use it to create
  119. // the option instance from the provided buffer chunk.
  120. const OptionDefinitionPtr& def = *(range.first);
  121. assert(def);
  122. opt = def->optionFactory(Option::V6, opt_type,
  123. buf.begin() + offset,
  124. buf.begin() + offset + opt_len);
  125. }
  126. // add option to options
  127. options.insert(std::make_pair(opt_type, opt));
  128. offset += opt_len;
  129. }
  130. return (offset);
  131. }
  132. size_t LibDHCP::unpackOptions4(const OptionBuffer& buf,
  133. isc::dhcp::Option::OptionCollection& options) {
  134. size_t offset = 0;
  135. // 2 byte - header of DHCPv4 option
  136. while (offset + 1 <= buf.size()) {
  137. uint8_t opt_type = buf[offset++];
  138. // DHO_END is a special, one octet long option
  139. if (opt_type == DHO_END)
  140. return (offset); // just return. Don't need to add DHO_END option
  141. // DHO_PAD is just a padding after DHO_END. Let's continue parsing
  142. // in case we receive a message without DHO_END.
  143. if (opt_type == DHO_PAD)
  144. continue;
  145. if (offset + 1 >= buf.size()) {
  146. isc_throw(OutOfRange, "Attempt to parse truncated option "
  147. << opt_type);
  148. }
  149. uint8_t opt_len = buf[offset++];
  150. if (offset + opt_len > buf.size()) {
  151. isc_throw(OutOfRange, "Option parse failed. Tried to parse "
  152. << offset + opt_len << " bytes from " << buf.size()
  153. << "-byte long buffer.");
  154. }
  155. OptionPtr opt;
  156. switch(opt_type) {
  157. default:
  158. opt = OptionPtr(new Option(Option::V4, opt_type,
  159. buf.begin()+offset,
  160. buf.begin()+offset+opt_len));
  161. }
  162. options.insert(std::make_pair(opt_type, opt));
  163. offset += opt_len;
  164. }
  165. return (offset);
  166. }
  167. void LibDHCP::packOptions6(isc::util::OutputBuffer &buf,
  168. const isc::dhcp::Option::OptionCollection& options) {
  169. for (Option::OptionCollection::const_iterator it = options.begin();
  170. it != options.end(); ++it) {
  171. it->second->pack(buf);
  172. }
  173. }
  174. void
  175. LibDHCP::packOptions(isc::util::OutputBuffer& buf,
  176. const Option::OptionCollection& options) {
  177. for (Option::OptionCollection::const_iterator it = options.begin();
  178. it != options.end(); ++it) {
  179. it->second->pack4(buf);
  180. }
  181. }
  182. void LibDHCP::OptionFactoryRegister(Option::Universe u,
  183. uint16_t opt_type,
  184. Option::Factory* factory) {
  185. switch (u) {
  186. case Option::V6: {
  187. if (v6factories_.find(opt_type) != v6factories_.end()) {
  188. isc_throw(BadValue, "There is already DHCPv6 factory registered "
  189. << "for option type " << opt_type);
  190. }
  191. v6factories_[opt_type]=factory;
  192. return;
  193. }
  194. case Option::V4:
  195. {
  196. // Option 0 is special (a one octet-long, equal 0) PAD option. It is never
  197. // instantiated as an Option object, but rather consumed during packet parsing.
  198. if (opt_type == 0) {
  199. isc_throw(BadValue, "Cannot redefine PAD option (code=0)");
  200. }
  201. // Option 255 is never instantiated as an option object. It is special
  202. // (a one-octet equal 255) option that is added at the end of all options
  203. // during packet assembly. It is also silently consumed during packet parsing.
  204. if (opt_type > 254) {
  205. isc_throw(BadValue, "Too big option type for DHCPv4, only 0-254 allowed.");
  206. }
  207. if (v4factories_.find(opt_type)!=v4factories_.end()) {
  208. isc_throw(BadValue, "There is already DHCPv4 factory registered "
  209. << "for option type " << opt_type);
  210. }
  211. v4factories_[opt_type]=factory;
  212. return;
  213. }
  214. default:
  215. isc_throw(BadValue, "Invalid universe type specified.");
  216. }
  217. return;
  218. }
  219. void
  220. LibDHCP::initStdOptionDefs4() {
  221. isc_throw(isc::NotImplemented, "initStdOptionDefs4 is not implemented");
  222. }
  223. void
  224. LibDHCP::initStdOptionDefs6() {
  225. v6option_defs_.clear();
  226. struct OptionParams {
  227. std::string name;
  228. uint16_t code;
  229. OptionDataType type;
  230. bool array;
  231. };
  232. OptionParams params[] = {
  233. { "CLIENTID", D6O_CLIENTID, OPT_BINARY_TYPE, false },
  234. { "SERVERID", D6O_SERVERID, OPT_BINARY_TYPE, false },
  235. { "IA_NA", D6O_IA_NA, OPT_RECORD_TYPE, false },
  236. { "IAADDR", D6O_IAADDR, OPT_RECORD_TYPE, false },
  237. { "ORO", D6O_ORO, OPT_UINT16_TYPE, true },
  238. { "ELAPSED_TIME", D6O_ELAPSED_TIME, OPT_UINT16_TYPE, false },
  239. { "STATUS_CODE", D6O_STATUS_CODE, OPT_RECORD_TYPE, false },
  240. { "RAPID_COMMIT", D6O_RAPID_COMMIT, OPT_EMPTY_TYPE, false },
  241. { "DNS_SERVERS", D6O_NAME_SERVERS, OPT_IPV6_ADDRESS_TYPE, true },
  242. { "IA_PD", D6O_IA_PD, OPT_RECORD_TYPE, false }
  243. };
  244. const int params_size = sizeof(params) / sizeof(params[0]);
  245. for (int i = 0; i < params_size; ++i) {
  246. OptionDefinitionPtr definition(new OptionDefinition(params[i].name,
  247. params[i].code,
  248. params[i].type,
  249. params[i].array));
  250. switch(params[i].code) {
  251. case D6O_IA_NA:
  252. case D6O_IA_PD:
  253. for (int j = 0; j < 3; ++j) {
  254. definition->addRecordField(OPT_UINT32_TYPE);
  255. }
  256. break;
  257. case D6O_IAADDR:
  258. definition->addRecordField(OPT_IPV6_ADDRESS_TYPE);
  259. definition->addRecordField(OPT_UINT32_TYPE);
  260. definition->addRecordField(OPT_UINT32_TYPE);
  261. break;
  262. case D6O_STATUS_CODE:
  263. definition->addRecordField(OPT_UINT16_TYPE);
  264. definition->addRecordField(OPT_STRING_TYPE);
  265. break;
  266. default:
  267. // The default case is intentionally left empty
  268. // as it does not need any processing.
  269. ;
  270. }
  271. try {
  272. definition->validate();
  273. } catch (const Exception& ex) {
  274. // This is unlikely event that validation fails and may
  275. // be only caused by programming error. To guarantee the
  276. // data consistency we clear all option definitions that
  277. // have been added so far and pass the exception forward.
  278. v6option_defs_.clear();
  279. throw;
  280. }
  281. v6option_defs_.push_back(definition);
  282. }
  283. }