libdhcp++.cc 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  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. { "ia-ta", D6O_IA_TA, OPT_UINT32_TYPE, false },
  237. { "iaaddr", D6O_IAADDR, OPT_RECORD_TYPE, false },
  238. { "oro", D6O_ORO, OPT_UINT16_TYPE, true },
  239. { "preference", D6O_PREFERENCE, OPT_UINT8_TYPE, false },
  240. { "elapsed-time", D6O_ELAPSED_TIME, OPT_UINT16_TYPE, false },
  241. { "relay-msg", D6O_RELAY_MSG, OPT_BINARY_TYPE, false },
  242. // { "AUTH", D6O_AUTH, D6O_AUTH, OPT_RECORD_TYPE, false },
  243. { "unicast", D6O_UNICAST, OPT_IPV6_ADDRESS_TYPE, false },
  244. { "status-code", D6O_STATUS_CODE, OPT_RECORD_TYPE, false },
  245. { "rapid-commit", D6O_RAPID_COMMIT, OPT_EMPTY_TYPE, false },
  246. { "user-class", D6O_USER_CLASS, OPT_BINARY_TYPE, false },
  247. { "vendor-class", D6O_VENDOR_CLASS, OPT_RECORD_TYPE, false },
  248. { "vendor-opts", D6O_VENDOR_OPTS, OPT_RECORD_TYPE, false },
  249. { "interface-id", D6O_INTERFACE_ID, OPT_BINARY_TYPE, false },
  250. { "reconf-msg", D6O_RECONF_MSG, OPT_UINT8_TYPE, false },
  251. { "reconf-accept", D6O_RECONF_ACCEPT, OPT_EMPTY_TYPE, false },
  252. { "sip-server-dns", D6O_SIP_SERVERS_DNS, OPT_FQDN_TYPE, true },
  253. { "sip-server-addr", D6O_SIP_SERVERS_ADDR, OPT_IPV6_ADDRESS_TYPE, true },
  254. { "dns-servers", D6O_NAME_SERVERS, OPT_IPV6_ADDRESS_TYPE, true },
  255. { "domain-search", D6O_DOMAIN_SEARCH, OPT_FQDN_TYPE, true },
  256. { "ia-pd", D6O_IA_PD, OPT_RECORD_TYPE, false },
  257. { "iaprefix", D6O_IAPREFIX, OPT_RECORD_TYPE, false },
  258. { "nis-servers", D6O_NIS_SERVERS, OPT_IPV6_ADDRESS_TYPE, true },
  259. { "nisp-servers", D6O_NISP_SERVERS, OPT_IPV6_ADDRESS_TYPE, true },
  260. { "nis-domain-name", D6O_NIS_DOMAIN_NAME, OPT_FQDN_TYPE, true },
  261. { "nisp-domain-name", D6O_NISP_DOMAIN_NAME, OPT_FQDN_TYPE, true },
  262. { "sntp-servers", D6O_SNTP_SERVERS, OPT_IPV6_ADDRESS_TYPE, true },
  263. { "information-refresh-time", D6O_INFORMATION_REFRESH_TIME,
  264. OPT_UINT32_TYPE, false },
  265. { "bcmcs-server-dns", D6O_BCMCS_SERVER_D, OPT_FQDN_TYPE, true },
  266. { "bcmcs-server-addr", D6O_BCMCS_SERVER_A, OPT_IPV6_ADDRESS_TYPE, true },
  267. { "geoconf-civic", D6O_GEOCONF_CIVIC, OPT_RECORD_TYPE, false },
  268. { "remote-id", D6O_REMOTE_ID, OPT_RECORD_TYPE, false },
  269. { "subscriber-id", D6O_SUBSCRIBER_ID, OPT_BINARY_TYPE, false },
  270. { "client-fqdn", D6O_CLIENT_FQDN, OPT_RECORD_TYPE, false },
  271. { "pana-agent", D6O_PANA_AGENT, OPT_IPV6_ADDRESS_TYPE, true },
  272. { "new-posix-timezone", D6O_NEW_POSIX_TIMEZONE, OPT_STRING_TYPE, false },
  273. { "new-tzdb-timezone", D6O_NEW_TZDB_TIMEZONE, OPT_STRING_TYPE, false },
  274. { "ero", D6O_ERO, OPT_UINT16_TYPE, true },
  275. { "lq-query", D6O_LQ_QUERY, OPT_RECORD_TYPE, false },
  276. { "client-data", D6O_CLIENT_DATA, OPT_EMPTY_TYPE, false },
  277. { "clt-time", D6O_CLT_TIME, OPT_UINT32_TYPE, false },
  278. { "lq-relay-data", D6O_LQ_RELAY_DATA, OPT_RECORD_TYPE, false },
  279. { "lq-client-link", D6O_LQ_CLIENT_LINK, OPT_IPV6_ADDRESS_TYPE, true },
  280. /* { "MIP6_HNIDF", D6O_MIP6_HNIDF, OPT_FQDN_TYPE, false },
  281. { "MIP6_VDINF", D6O_MIP6_VDINF, OPT_EMPTY_TYPE, false },
  282. { "V6_LOST", D6O_V6_LOST, OPT_FQDN_TYPE, false },
  283. { "CAPWAP_AC_V6", D6O_CAPWAP_AC_V6, OPT_IPV6_ADDRESS_TYPE, true },
  284. { "RELAY_ID", D6O_RELAY_ID, OPT_BINARY_TYPE, false },
  285. { "IPV6_ADDRESS_MOS", */
  286. };
  287. const int params_size = sizeof(params) / sizeof(params[0]);
  288. for (int i = 0; i < params_size; ++i) {
  289. OptionDefinitionPtr definition(new OptionDefinition(params[i].name,
  290. params[i].code,
  291. params[i].type,
  292. params[i].array));
  293. switch(params[i].code) {
  294. case D6O_IA_NA:
  295. case D6O_IA_PD:
  296. for (int j = 0; j < 3; ++j) {
  297. definition->addRecordField(OPT_UINT32_TYPE);
  298. }
  299. break;
  300. case D6O_IAADDR:
  301. definition->addRecordField(OPT_IPV6_ADDRESS_TYPE);
  302. definition->addRecordField(OPT_UINT32_TYPE);
  303. definition->addRecordField(OPT_UINT32_TYPE);
  304. break;
  305. case D6O_STATUS_CODE:
  306. definition->addRecordField(OPT_UINT16_TYPE);
  307. definition->addRecordField(OPT_STRING_TYPE);
  308. break;
  309. case D6O_VENDOR_CLASS:
  310. definition->addRecordField(OPT_UINT32_TYPE);
  311. definition->addRecordField(OPT_BINARY_TYPE);
  312. break;
  313. case D6O_VENDOR_OPTS:
  314. definition->addRecordField(OPT_UINT32_TYPE);
  315. definition->addRecordField(OPT_BINARY_TYPE);
  316. break;
  317. case D6O_IAPREFIX:
  318. definition->addRecordField(OPT_UINT32_TYPE);
  319. definition->addRecordField(OPT_UINT32_TYPE);
  320. definition->addRecordField(OPT_UINT8_TYPE);
  321. definition->addRecordField(OPT_BINARY_TYPE);
  322. break;
  323. case D6O_GEOCONF_CIVIC:
  324. definition->addRecordField(OPT_UINT8_TYPE);
  325. definition->addRecordField(OPT_UINT16_TYPE);
  326. definition->addRecordField(OPT_BINARY_TYPE);
  327. break;
  328. case D6O_REMOTE_ID:
  329. definition->addRecordField(OPT_UINT32_TYPE);
  330. definition->addRecordField(OPT_BINARY_TYPE);
  331. break;
  332. case D6O_CLIENT_FQDN:
  333. definition->addRecordField(OPT_UINT8_TYPE);
  334. definition->addRecordField(OPT_FQDN_TYPE);
  335. break;
  336. case D6O_LQ_QUERY:
  337. definition->addRecordField(OPT_UINT8_TYPE);
  338. definition->addRecordField(OPT_IPV6_ADDRESS_TYPE);
  339. break;
  340. case D6O_LQ_RELAY_DATA:
  341. definition->addRecordField(OPT_IPV6_ADDRESS_TYPE);
  342. definition->addRecordField(OPT_BINARY_TYPE);
  343. break;
  344. default:
  345. // The default case is intentionally left empty
  346. // as it does not need any processing.
  347. ;
  348. }
  349. try {
  350. definition->validate();
  351. } catch (const Exception& ex) {
  352. // This is unlikely event that validation fails and may
  353. // be only caused by programming error. To guarantee the
  354. // data consistency we clear all option definitions that
  355. // have been added so far and pass the exception forward.
  356. v6option_defs_.clear();
  357. throw;
  358. }
  359. v6option_defs_.push_back(definition);
  360. }
  361. }