pkt6.cc 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  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 <dhcp/dhcp6.h>
  15. #include <dhcp/libdhcp++.h>
  16. #include <dhcp/pkt6.h>
  17. #include <exceptions/exceptions.h>
  18. #include <iostream>
  19. #include <sstream>
  20. using namespace std;
  21. using namespace isc::asiolink;
  22. namespace isc {
  23. namespace dhcp {
  24. Pkt6::RelayInfo::RelayInfo()
  25. :msg_type_(0), hop_count_(0), linkaddr_("::"), peeraddr_(""), relay_msg_len_(0) {
  26. // interface_id_, subscriber_id_, remote_id_ initialized to NULL
  27. // echo_options_ initialized to empty collection
  28. }
  29. Pkt6::Pkt6(const uint8_t* buf, uint32_t buf_len, DHCPv6Proto proto /* = UDP */) :
  30. proto_(proto),
  31. msg_type_(0),
  32. transid_(rand()%0xffffff),
  33. iface_(""),
  34. ifindex_(-1),
  35. local_addr_("::"),
  36. remote_addr_("::"),
  37. local_port_(0),
  38. remote_port_(0),
  39. bufferOut_(0) {
  40. data_.resize(buf_len);
  41. memcpy(&data_[0], buf, buf_len);
  42. }
  43. Pkt6::Pkt6(uint8_t msg_type, uint32_t transid, DHCPv6Proto proto /*= UDP*/) :
  44. proto_(proto),
  45. msg_type_(msg_type),
  46. transid_(transid),
  47. iface_(""),
  48. ifindex_(-1),
  49. local_addr_("::"),
  50. remote_addr_("::"),
  51. local_port_(0),
  52. remote_port_(0),
  53. bufferOut_(0) {
  54. }
  55. uint16_t Pkt6::len() {
  56. if (relay_info_.empty()) {
  57. return (directLen());
  58. } else {
  59. calculateRelaySizes();
  60. return (relay_info_[0].relay_msg_len_ + getRelayOverhead(relay_info_[0]));
  61. }
  62. }
  63. uint16_t Pkt6::getRelayOverhead(const RelayInfo& relay) {
  64. uint16_t len = DHCPV6_RELAY_HDR_LEN // fixed header
  65. + Option::OPTION6_HDR_LEN; // header of the relay-msg option
  66. if (relay.interface_id_) {
  67. len += relay.interface_id_->len();
  68. }
  69. if (relay.subscriber_id_) {
  70. len += relay.subscriber_id_->len();
  71. }
  72. if (relay.remote_id_) {
  73. len += relay.remote_id_->len();
  74. }
  75. for (Option::OptionCollection::const_iterator opt = relay.echo_options_.begin();
  76. opt != relay.echo_options_.end(); ++opt) {
  77. len += (opt->second)->len();
  78. }
  79. return (len);
  80. }
  81. uint16_t Pkt6::calculateRelaySizes() {
  82. uint16_t len = directLen(); // start with length of all options
  83. int relay_index = relay_info_.size();
  84. while (relay_index) {
  85. relay_info_[relay_index - 1].relay_msg_len_ = len;
  86. len += getRelayOverhead(relay_info_[relay_index - 1]);
  87. --relay_index;
  88. }
  89. return (len);
  90. }
  91. uint16_t Pkt6::directLen() {
  92. uint16_t length = DHCPV6_PKT_HDR_LEN; // DHCPv6 header
  93. for (Option::OptionCollection::iterator it = options_.begin();
  94. it != options_.end();
  95. ++it) {
  96. length += (*it).second->len();
  97. }
  98. return (length);
  99. }
  100. bool
  101. Pkt6::pack() {
  102. switch (proto_) {
  103. case UDP:
  104. return packUDP();
  105. case TCP:
  106. return packTCP();
  107. default:
  108. isc_throw(BadValue, "Invalid protocol specified (non-TCP, non-UDP)");
  109. }
  110. return (false); // never happens
  111. }
  112. bool
  113. Pkt6::packUDP() {
  114. try {
  115. if (!relay_info_.empty()) {
  116. calculateRelaySizes();
  117. for (vector<RelayInfo>::iterator relay = relay_info_.begin();
  118. relay != relay_info_.end(); ++relay) {
  119. bufferOut_.writeUint8(relay->msg_type_);
  120. bufferOut_.writeUint8(relay->hop_count_);
  121. bufferOut_.writeData(&(relay->linkaddr_.toBytes()[0]),
  122. isc::asiolink::V6ADDRESS_LEN);
  123. bufferOut_.writeData(&relay->peeraddr_.toBytes()[0],
  124. isc::asiolink::V6ADDRESS_LEN);
  125. if (relay->interface_id_) {
  126. relay->interface_id_->pack(bufferOut_);
  127. }
  128. if (relay->subscriber_id_) {
  129. relay->subscriber_id_->pack(bufferOut_);
  130. }
  131. if (relay->remote_id_) {
  132. relay->remote_id_->pack(bufferOut_);
  133. }
  134. for (Option::OptionCollection::const_iterator opt =
  135. relay->echo_options_.begin();
  136. opt != relay->echo_options_.end(); ++opt) {
  137. (opt->second)->pack(bufferOut_);
  138. }
  139. bufferOut_.writeUint16(D6O_RELAY_MSG);
  140. bufferOut_.writeUint16(relay->relay_msg_len_);
  141. }
  142. }
  143. // DHCPv6 header: message-type (1 octect) + transaction id (3 octets)
  144. bufferOut_.writeUint8(msg_type_);
  145. // store 3-octet transaction-id
  146. bufferOut_.writeUint8( (transid_ >> 16) & 0xff );
  147. bufferOut_.writeUint8( (transid_ >> 8) & 0xff );
  148. bufferOut_.writeUint8( (transid_) & 0xff );
  149. // the rest are options
  150. LibDHCP::packOptions(bufferOut_, options_);
  151. }
  152. catch (const Exception& e) {
  153. /// @todo: throw exception here once we turn this function to void.
  154. return (false);
  155. }
  156. return (true);
  157. }
  158. bool
  159. Pkt6::packTCP() {
  160. /// TODO Implement this function.
  161. isc_throw(Unexpected, "DHCPv6 over TCP (bulk leasequery and failover)"
  162. "not implemented yet.");
  163. }
  164. bool
  165. Pkt6::unpack() {
  166. switch (proto_) {
  167. case UDP:
  168. return unpackUDP();
  169. case TCP:
  170. return unpackTCP();
  171. default:
  172. isc_throw(BadValue, "Invalid protocol specified (non-TCP, non-UDP)");
  173. }
  174. return (false); // never happens
  175. }
  176. bool
  177. Pkt6::unpackUDP() {
  178. if (data_.size() < 4) {
  179. // @todo: throw exception here informing that packet is truncated
  180. // once we turn this function to void.
  181. return (false);
  182. }
  183. msg_type_ = data_[0];
  184. switch (msg_type_) {
  185. case DHCPV6_SOLICIT:
  186. case DHCPV6_ADVERTISE:
  187. case DHCPV6_REQUEST:
  188. case DHCPV6_CONFIRM:
  189. case DHCPV6_RENEW:
  190. case DHCPV6_REBIND:
  191. case DHCPV6_REPLY:
  192. case DHCPV6_DECLINE:
  193. case DHCPV6_RECONFIGURE:
  194. case DHCPV6_INFORMATION_REQUEST:
  195. default: // assume that uknown messages are not using relay format
  196. {
  197. return (unpackMsg(data_.begin(), data_.end()));
  198. }
  199. case DHCPV6_RELAY_FORW:
  200. case DHCPV6_RELAY_REPL:
  201. return (unpackRelayMsg());
  202. }
  203. }
  204. bool
  205. Pkt6::unpackMsg(OptionBuffer::const_iterator begin,
  206. OptionBuffer::const_iterator end) {
  207. if (std::distance(begin, end) < 4) {
  208. // truncated message (less than 4 bytes)
  209. return (false);
  210. }
  211. msg_type_ = *begin++;
  212. transid_ = ( (*begin++) << 16 ) +
  213. ((*begin++) << 8) + (*begin++);
  214. transid_ = transid_ & 0xffffff;
  215. try {
  216. OptionBuffer opt_buffer(begin, end);
  217. LibDHCP::unpackOptions6(opt_buffer, options_);
  218. } catch (const Exception& e) {
  219. // @todo: throw exception here once we turn this function to void.
  220. return (false);
  221. }
  222. return (true);
  223. }
  224. bool
  225. Pkt6::unpackRelayMsg() {
  226. // we use offset + bufsize, because we want to avoid creating unnecessary
  227. // copies. There may be up to 32 relays. While using InputBuffer would
  228. // be probably a bit cleaner, copying data up to 32 times is unacceptable
  229. // price here. Hence a single buffer with offets and lengths.
  230. size_t bufsize = data_.size();
  231. size_t offset = 0;
  232. size_t relay_msg_offset = 0;
  233. while (bufsize >= DHCPV6_RELAY_HDR_LEN) {
  234. RelayInfo relay;
  235. // parse fixed header first (first 34 bytes)
  236. relay.msg_type_ = data_[offset++];
  237. relay.hop_count_ = data_[offset++];
  238. relay.linkaddr_ = IOAddress::fromBytes(AF_INET6, &data_[offset]);
  239. offset += isc::asiolink::V6ADDRESS_LEN;
  240. relay.peeraddr_ = IOAddress::fromBytes(AF_INET6, &data_[offset]);
  241. offset += isc::asiolink::V6ADDRESS_LEN;
  242. bufsize -= DHCPV6_RELAY_HDR_LEN; // 34 bytes (1+1+16+16)
  243. try {
  244. Option::OptionCollection options;
  245. // parse the rest as options
  246. OptionBuffer opt_buffer(&data_[offset], &data_[offset+bufsize]);
  247. LibDHCP::unpackOptions6(opt_buffer, options, &relay_msg_offset);
  248. /// @todo: check that each option appears at most once
  249. //relay.interface_id_ = options->getOption(D6O_INTERFACE_ID);
  250. //relay.subscriber_id_ = options->getOption(D6O_SUBSCRIBER_ID);
  251. //relay.remote_id_ = options->getOption(D6O_REMOTE_ID);
  252. Option::OptionCollection::const_iterator relay_iter = options.find(D6O_RELAY_MSG);
  253. if (relay_iter == options.end()) {
  254. // there's nothing to decapsulate. We give up.
  255. isc_throw(InvalidOperation, "Mandatory relay_msg missing");
  256. }
  257. OptionPtr relay_msg = relay_iter->second;
  258. // store relay information parsed so far
  259. addRelayInfo(relay);
  260. /// @todo: implement ERO here
  261. size_t inner_len = relay_msg->len() - relay_msg->getHeaderLen();
  262. if (inner_len >= bufsize) {
  263. // length of the relay_msg option extends beyond end of the message
  264. isc_throw(Unexpected, "Relay-msg option is truncated.");
  265. return false;
  266. }
  267. uint8_t inner_type = relay_msg->getUint8();
  268. offset += relay_msg_offset;
  269. bufsize = inner_len;
  270. if ( (inner_type != DHCPV6_RELAY_FORW) && (inner_type != DHCPV6_RELAY_REPL)) {
  271. // Ok, the inner message is not encapsulated, let's decode it directly
  272. return (unpackMsg(data_.begin() + offset, data_.begin() + offset + inner_len));
  273. }
  274. // Oh well, there's inner relay-forw or relay-repl inside. Let's unpack it as well
  275. } catch (const Exception& e) {
  276. /// @todo: throw exception here once we turn this function to void.
  277. return (false);
  278. }
  279. }
  280. if ( (offset == data_.size()) && (bufsize == 0) ) {
  281. // message has been parsed completely
  282. return (true);
  283. }
  284. /// @todo: log here that there are additional unparsed bytes
  285. return (true);
  286. }
  287. void
  288. Pkt6::addRelayInfo(const RelayInfo& relay) {
  289. if (relay_info_.size() > 32) {
  290. isc_throw(BadValue, "Massage cannot be encapsulated more than 32 times");
  291. }
  292. /// @todo: Implement type checks here (e.g. we could receive relay-forw in relay-repl)
  293. relay_info_.push_back(relay);
  294. }
  295. bool
  296. Pkt6::unpackTCP() {
  297. isc_throw(Unexpected, "DHCPv6 over TCP (bulk leasequery and failover) "
  298. "not implemented yet.");
  299. }
  300. std::string
  301. Pkt6::toText() {
  302. stringstream tmp;
  303. tmp << "localAddr=[" << local_addr_.toText() << "]:" << local_port_
  304. << " remoteAddr=[" << remote_addr_.toText()
  305. << "]:" << remote_port_ << endl;
  306. tmp << "msgtype=" << static_cast<int>(msg_type_) << ", transid=0x" <<
  307. hex << transid_ << dec << endl;
  308. for (isc::dhcp::Option::OptionCollection::iterator opt=options_.begin();
  309. opt != options_.end();
  310. ++opt) {
  311. tmp << opt->second->toText() << std::endl;
  312. }
  313. return tmp.str();
  314. }
  315. OptionPtr
  316. Pkt6::getOption(uint16_t opt_type) {
  317. isc::dhcp::Option::OptionCollection::const_iterator x = options_.find(opt_type);
  318. if (x!=options_.end()) {
  319. return (*x).second;
  320. }
  321. return OptionPtr(); // NULL
  322. }
  323. isc::dhcp::Option::OptionCollection
  324. Pkt6::getOptions(uint16_t opt_type) {
  325. isc::dhcp::Option::OptionCollection found;
  326. for (Option::OptionCollection::const_iterator x = options_.begin();
  327. x != options_.end(); ++x) {
  328. if (x->first == opt_type) {
  329. found.insert(make_pair(opt_type, x->second));
  330. }
  331. }
  332. return (found);
  333. }
  334. void
  335. Pkt6::addOption(const OptionPtr& opt) {
  336. options_.insert(pair<int, boost::shared_ptr<Option> >(opt->getType(), opt));
  337. }
  338. bool
  339. Pkt6::delOption(uint16_t type) {
  340. isc::dhcp::Option::OptionCollection::iterator x = options_.find(type);
  341. if (x!=options_.end()) {
  342. options_.erase(x);
  343. return (true); // delete successful
  344. }
  345. return (false); // can't find option to be deleted
  346. }
  347. void Pkt6::repack() {
  348. bufferOut_.writeData(&data_[0], data_.size());
  349. }
  350. void
  351. Pkt6::updateTimestamp() {
  352. timestamp_ = boost::posix_time::microsec_clock::universal_time();
  353. }
  354. const char*
  355. Pkt6::getName(uint8_t type) {
  356. static const char* CONFIRM = "CONFIRM";
  357. static const char* DECLINE = "DECLINE";
  358. static const char* INFORMATION_REQUEST = "INFORMATION_REQUEST";
  359. static const char* REBIND = "REBIND";
  360. static const char* RELEASE = "RELEASE";
  361. static const char* RENEW = "RENEW";
  362. static const char* REQUEST = "REQUEST";
  363. static const char* SOLICIT = "SOLICIT";
  364. static const char* UNKNOWN = "UNKNOWN";
  365. switch (type) {
  366. case DHCPV6_CONFIRM:
  367. return (CONFIRM);
  368. case DHCPV6_DECLINE:
  369. return (DECLINE);
  370. case DHCPV6_INFORMATION_REQUEST:
  371. return (INFORMATION_REQUEST);
  372. case DHCPV6_REBIND:
  373. return (REBIND);
  374. case DHCPV6_RELEASE:
  375. return (RELEASE);
  376. case DHCPV6_RENEW:
  377. return (RENEW);
  378. case DHCPV6_REQUEST:
  379. return (REQUEST);
  380. case DHCPV6_SOLICIT:
  381. return (SOLICIT);
  382. default:
  383. ;
  384. }
  385. return (UNKNOWN);
  386. }
  387. const char* Pkt6::getName() const {
  388. return (getName(getType()));
  389. }
  390. } // end of isc::dhcp namespace
  391. } // end of isc namespace