option6_iaprefix.cc 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. // Copyright (C) 2013-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 <asiolink/io_address.h>
  8. #include <dhcp/dhcp6.h>
  9. #include <dhcp/libdhcp++.h>
  10. #include <dhcp/option6_iaprefix.h>
  11. #include <dhcp/option_space.h>
  12. #include <exceptions/exceptions.h>
  13. #include <util/io_utilities.h>
  14. #include <sstream>
  15. #include <stdint.h>
  16. #include <arpa/inet.h>
  17. using namespace std;
  18. using namespace isc::asiolink;
  19. using namespace isc::util;
  20. namespace isc {
  21. namespace dhcp {
  22. Option6IAPrefix::Option6IAPrefix(uint16_t type, const isc::asiolink::IOAddress& prefix,
  23. uint8_t prefix_len, uint32_t pref, uint32_t valid)
  24. :Option6IAAddr(type, prefix, pref, valid), prefix_len_(prefix_len) {
  25. setEncapsulatedSpace(DHCP6_OPTION_SPACE);
  26. // Option6IAAddr will check if prefix is IPv6 and will throw if it is not
  27. if (prefix_len > 128) {
  28. isc_throw(BadValue, static_cast<unsigned>(prefix_len)
  29. << " is not a valid prefix length. "
  30. << "Allowed range is 0..128");
  31. }
  32. }
  33. Option6IAPrefix::Option6IAPrefix(uint32_t type, OptionBuffer::const_iterator begin,
  34. OptionBuffer::const_iterator end)
  35. :Option6IAAddr(type, begin, end) {
  36. setEncapsulatedSpace(DHCP6_OPTION_SPACE);
  37. unpack(begin, end);
  38. }
  39. OptionPtr
  40. Option6IAPrefix::clone() const {
  41. return (cloneInternal<Option6IAPrefix>());
  42. }
  43. void Option6IAPrefix::pack(isc::util::OutputBuffer& buf) const {
  44. if (!addr_.isV6()) {
  45. isc_throw(isc::BadValue, addr_ << " is not an IPv6 address");
  46. }
  47. buf.writeUint16(type_);
  48. // len() returns complete option length. len field contains
  49. // length without 4-byte option header
  50. buf.writeUint16(len() - getHeaderLen());
  51. buf.writeUint32(preferred_);
  52. buf.writeUint32(valid_);
  53. buf.writeUint8(prefix_len_);
  54. buf.writeData(&addr_.toBytes()[0], isc::asiolink::V6ADDRESS_LEN);
  55. // store encapsulated options (the only defined so far is PD_EXCLUDE)
  56. packOptions(buf);
  57. }
  58. void Option6IAPrefix::unpack(OptionBuffer::const_iterator begin,
  59. OptionBuffer::const_iterator end) {
  60. if ( distance(begin, end) < OPTION6_IAPREFIX_LEN) {
  61. isc_throw(OutOfRange, "Option " << type_ << " truncated");
  62. }
  63. preferred_ = readUint32(&(*begin), distance(begin, end));
  64. begin += sizeof(uint32_t);
  65. valid_ = readUint32(&(*begin), distance(begin, end));
  66. begin += sizeof(uint32_t);
  67. prefix_len_ = *begin;
  68. begin += sizeof(uint8_t);
  69. // 16 bytes: IPv6 address
  70. OptionBuffer address_with_mask;
  71. mask(begin, begin + V6ADDRESS_LEN, prefix_len_, address_with_mask);
  72. addr_ = IOAddress::fromBytes(AF_INET6, &(*address_with_mask.begin()));
  73. begin += V6ADDRESS_LEN;
  74. // unpack encapsulated options (the only defined so far is PD_EXCLUDE)
  75. unpackOptions(OptionBuffer(begin, end));
  76. }
  77. std::string Option6IAPrefix::toText(int indent) const {
  78. std::stringstream output;
  79. output << headerToText(indent, "IAPREFIX") << ": "
  80. << "prefix=" << addr_ << "/" << static_cast<int>(prefix_len_)
  81. << ", preferred-lft=" << preferred_
  82. << ", valid-lft=" << valid_;
  83. output << suboptionsToText(indent + 2);
  84. return (output.str());
  85. }
  86. uint16_t Option6IAPrefix::len() const {
  87. uint16_t length = OPTION6_HDR_LEN + OPTION6_IAPREFIX_LEN;
  88. // length of all suboptions
  89. for (OptionCollection::const_iterator it = options_.begin();
  90. it != options_.end(); ++it) {
  91. length += (*it).second->len();
  92. }
  93. return (length);
  94. }
  95. void
  96. Option6IAPrefix::mask(OptionBuffer::const_iterator begin,
  97. OptionBuffer::const_iterator end,
  98. const uint8_t len,
  99. OptionBuffer& output_address) const {
  100. output_address.resize(16, 0);
  101. if (len >= 128) {
  102. std::copy(begin, end, output_address.begin());
  103. } else if (len > 0) {
  104. // All the bits that represent whole octets of the prefix are copied with
  105. // no change.
  106. std::copy(begin, begin + static_cast<uint8_t>(len/8), output_address.begin());
  107. // The remaining significant bits of the last octet have to be left unchanged,
  108. // but the remaining bits of this octet must be set to zero. The number of
  109. // significant bits is calculated as a reminder from the devision of the
  110. // prefix length by 8 (by size of the octet). The number of bits to be set
  111. // to zero is therefore calculated as: 8 - (len % 8).
  112. // Next, the mask is created by shifting the 0xFF by the number of bits
  113. // to be set to 0. By performing logical AND of this mask with the original
  114. // value of the last octet we get the final value for the new octet.
  115. output_address[len/8] = (*(begin + len/8) & (0xFF << (8 - (len % 8))));
  116. }
  117. }
  118. } // end of namespace isc::dhcp
  119. } // end of namespace isc