Kea: option6_iaprefix.cc Source File

| Kea 3.2.0-git

option6_iaprefix.cc
Go to the documentation of this file.
1// Copyright (C) 2013-2026 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
7#include <config.h>
8
9#include <asiolink/addr_utilities.h>
10#include <asiolink/io_address.h>
11#include <dhcp/dhcp6.h>
12#include <dhcp/libdhcp++.h>
13#include <dhcp/option6_iaprefix.h>
14#include <dhcp/option_space.h>
15#include <exceptions/exceptions.h>
16#include <util/io.h>
17
18#include <sstream>
19
20#include <stdint.h>
21#include <arpa/inet.h>
22
23using namespace std;
24using namespace isc::asiolink;
25using namespace isc::util;
26
27namespace isc {
28namespace dhcp {
29
30Option6IAPrefix::Option6IAPrefix(uint16_t type, const isc::asiolink::IOAddress& prefix,
31 uint8_t prefix_len, uint32_t pref, uint32_t valid)
32 : Option6IAAddr(type, prefix, pref, valid), prefix_len_(prefix_len) {
33 setEncapsulatedSpace(DHCP6_OPTION_SPACE);
34 // Option6IAAddr will check if prefix is IPv6 and will throw if it is not
35 if (prefix_len > 128) {
36 isc_throw(BadValue, static_cast<unsigned>(prefix_len)
37 << " is not a valid prefix length. "
38 << "Allowed range is 0..128");
39 }
40 IOAddress first_address = firstAddrInPrefix(addr_, prefix_len_);
41 if (first_address != addr_) {
42 isc_throw(BadValue, "Prefix address: " << addr_
43 << " exceeds prefix/prefix-len pair: " << first_address
44 << "/" << static_cast<uint32_t>(prefix_len_));
45 }
46}
47
48 Option6IAPrefix::Option6IAPrefix(uint32_t type,
49 OptionBuffer::const_iterator begin,
50 OptionBuffer::const_iterator end,
51 size_t rec_level)
52 :Option6IAAddr(type, begin, end) {
53 setEncapsulatedSpace(DHCP6_OPTION_SPACE);
54 unpack(begin, end, rec_level);
55 // The unpack function always drops the extra bits in prefix so there
56 // is no need to check if address matches the first address in prefix.
57}
58
59 OptionPtr
60Option6IAPrefix::clone() const {
61 return (cloneInternal<Option6IAPrefix>());
62}
63
64 void Option6IAPrefix::pack(isc::util::OutputBuffer& buf, bool) const {
65 if (!addr_.isV6()) {
66 isc_throw(isc::BadValue, addr_ << " is not an IPv6 address");
67 }
68
69 buf.writeUint16(type_);
70
71 // len() returns complete option length. len field contains
72 // length without 4-byte option header
73 buf.writeUint16(len() - getHeaderLen());
74
75 buf.writeUint32(preferred_);
76 buf.writeUint32(valid_);
77 buf.writeUint8(prefix_len_);
78
79 buf.writeData(&addr_.toBytes()[0], isc::asiolink::V6ADDRESS_LEN);
80
81 // store encapsulated options (the only defined so far is PD_EXCLUDE)
82 packOptions(buf);
83}
84
85 void Option6IAPrefix::unpack(OptionBuffer::const_iterator begin,
86 OptionBuffer::const_iterator end) {
87 unpack(begin, end, 0);
88}
89
90 void Option6IAPrefix::unpack(OptionBuffer::const_iterator begin,
91 OptionBuffer::const_iterator end,
92 size_t rec_level) {
93 if (static_cast<size_t>(distance(begin, end)) < OPTION6_IAPREFIX_LEN) {
94 isc_throw(OutOfRange, "Option " << type_ << " truncated");
95 }
96
97 preferred_ = readUint32(&(*begin), distance(begin, end));
98 begin += sizeof(uint32_t);
99
100 valid_ = readUint32(&(*begin), distance(begin, end));
101 begin += sizeof(uint32_t);
102
103 prefix_len_ = *begin;
104 begin += sizeof(uint8_t);
105
106 // 16 bytes: IPv6 address
107 OptionBuffer address_with_mask;
108 mask(begin, begin + V6ADDRESS_LEN, prefix_len_, address_with_mask);
109 addr_ = IOAddress::fromBytes(AF_INET6, &(*address_with_mask.begin()));
110 begin += V6ADDRESS_LEN;
111
112 // unpack encapsulated options (the only defined so far is PD_EXCLUDE)
113 unpackOptions(OptionBuffer(begin, end), rec_level);
114}
115
116std::string Option6IAPrefix::toText(int indent) const {
117 std::stringstream output;
118 output << headerToText(indent, "IAPREFIX") << ": "
119 << "prefix=" << addr_ << "/" << static_cast<int>(prefix_len_)
120 << ", preferred-lft=" << preferred_
121 << ", valid-lft=" << valid_;
122
123 output << suboptionsToText(indent + 2);
124 return (output.str());
125}
126
127uint16_t Option6IAPrefix::len() const {
128
129 uint16_t length = OPTION6_HDR_LEN + OPTION6_IAPREFIX_LEN;
130
131 // length of all suboptions
132 for (auto const& it : options_) {
133 length += it.second->len();
134 }
135 return (length);
136}
137
138void
139Option6IAPrefix::mask(OptionBuffer::const_iterator begin,
140 OptionBuffer::const_iterator end,
141 const uint8_t len,
142 OptionBuffer& output_address) const {
143 output_address.resize(16, 0);
144 if (len >= 128) {
145 std::copy(begin, end, output_address.begin());
146 } else if (len > 0) {
147 // All the bits that represent whole octets of the prefix are copied with
148 // no change.
149 std::copy(begin, begin + static_cast<uint8_t>(len/8), output_address.begin());
150 // The remaining significant bits of the last octet have to be left unchanged,
151 // but the remaining bits of this octet must be set to zero. The number of
152 // significant bits is calculated as a reminder from the division of the
153 // prefix length by 8 (by size of the octet). The number of bits to be set
154 // to zero is therefore calculated as: 8 - (len % 8).
155 // Next, the mask is created by shifting the 0xFF by the number of bits
156 // to be set to 0. By performing logical AND of this mask with the original
157 // value of the last octet we get the final value for the new octet.
158 output_address[len/8] = (*(begin + len/8) & (0xFF << (8 - (len % 8))));
159 }
160}
161
162
163} // end of namespace isc::dhcp
164} // end of namespace isc
addr_utilities.h
isc::BadValue
A generic exception that is thrown if a parameter given to a method is considered invalid in that con...
Definition exceptions/exceptions.h:132
isc::OutOfRange
A generic exception that is thrown if a parameter given to a method would refer to or modify out-of-r...
Definition exceptions/exceptions.h:115
isc::asiolink::IOAddress
The IOAddress class represents an IP addresses (version agnostic).
Definition io_address.h:53
isc::asiolink::IOAddress::fromBytes
static IOAddress fromBytes(short family, const uint8_t *data)
Creates an address from over wire data.
Definition io_address.cc:65
isc::dhcp::Option6IAAddr::valid_
unsigned int valid_
contains valid-lifetime timer (in seconds)
Definition option6_iaaddr.h:133
isc::dhcp::Option6IAAddr::Option6IAAddr
Option6IAAddr(uint16_t type, const isc::asiolink::IOAddress &addr, uint32_t preferred, uint32_t valid)
Constructor, used for options constructed (during transmission).
Definition option6_iaaddr.cc:29
isc::dhcp::Option6IAAddr::addr_
isc::asiolink::IOAddress addr_
contains an IPv6 address
Definition option6_iaaddr.h:127
isc::dhcp::Option6IAAddr::preferred_
unsigned int preferred_
contains preferred-lifetime timer (in seconds)
Definition option6_iaaddr.h:130
isc::dhcp::Option6IAPrefix::OPTION6_IAPREFIX_LEN
static const size_t OPTION6_IAPREFIX_LEN
length of the fixed part of the IAPREFIX option
Definition option6_iaprefix.h:55
isc::dhcp::Option6IAPrefix::toText
virtual std::string toText(int indent=0) const
Returns string representation of the option.
Definition option6_iaprefix.cc:116
isc::dhcp::Option6IAPrefix::unpack
virtual void unpack(OptionBufferConstIter begin, OptionBufferConstIter end)
Parses received buffer.
Definition option6_iaprefix.cc:85
isc::dhcp::Option6IAPrefix::Option6IAPrefix
Option6IAPrefix(uint16_t type, const isc::asiolink::IOAddress &addr, uint8_t prefix_length, uint32_t preferred, uint32_t valid)
Constructor, used for options constructed (during transmission).
Definition option6_iaprefix.cc:30
isc::dhcp::Option6IAPrefix::clone
virtual OptionPtr clone() const
Copies this option and returns a pointer to the copy.
Definition option6_iaprefix.cc:60
isc::dhcp::Option6IAPrefix::len
virtual uint16_t len() const
returns data length (data length + DHCPv4/DHCPv6 option header)
Definition option6_iaprefix.cc:127
isc::dhcp::Option6IAPrefix::pack
void pack(isc::util::OutputBuffer &buf, bool check=true) const
Writes option in wire-format.
Definition option6_iaprefix.cc:64
isc::dhcp::Option::type_
uint16_t type_
option type (0-255 for DHCPv4, 0-65535 for DHCPv6)
Definition option.h:598
isc::dhcp::Option::headerToText
std::string headerToText(const int indent=0, const std::string &type_name="") const
Returns option header in the textual format.
Definition option.cc:295
isc::dhcp::Option::suboptionsToText
std::string suboptionsToText(const int indent=0) const
Returns collection of suboptions in the textual format.
Definition option.cc:314
isc::dhcp::Option::setEncapsulatedSpace
void setEncapsulatedSpace(const std::string &encapsulated_space)
Sets the name of the option space encapsulated by this option.
Definition option.h:442
isc::dhcp::Option::getHeaderLen
virtual uint16_t getHeaderLen() const
Returns length of header (2 for v4, 4 for v6).
Definition option.cc:328
isc::dhcp::Option::packOptions
void packOptions(isc::util::OutputBuffer &buf, bool check=true) const
Store sub options in a buffer.
Definition option.cc:136
isc::dhcp::Option::OPTION6_HDR_LEN
static const size_t OPTION6_HDR_LEN
length of any DHCPv6 option header
Definition option.h:87
isc::dhcp::Option::valid
virtual bool valid() const
returns if option is valid (e.g.
Definition option.cc:191
isc::dhcp::Option::options_
OptionCollection options_
collection for storing suboptions
Definition option.h:604
isc::dhcp::Option::unpackOptions
void unpackOptions(const OptionBuffer &buf, size_t rec_level=0)
Builds a collection of sub options from the buffer.
Definition option.cc:155
isc::dhcp::Option::cloneInternal
OptionPtr cloneInternal() const
Copies this option and returns a pointer to the copy.
Definition option.h:504
isc::util::OutputBuffer
The OutputBuffer class is a buffer abstraction for manipulating mutable data.
Definition buffer.h:346
isc::util::OutputBuffer::writeUint8
void writeUint8(uint8_t data)
Write an unsigned 8-bit integer into the buffer.
Definition buffer.h:476
isc::util::OutputBuffer::writeUint16
void writeUint16(uint16_t data)
Write an unsigned 16-bit integer in host byte order into the buffer in network byte order.
Definition buffer.h:501
isc::util::OutputBuffer::writeData
void writeData(const void *data, size_t len)
Copy an arbitrary length of data into the buffer.
Definition buffer.h:559
isc::util::OutputBuffer::writeUint32
void writeUint32(uint32_t data)
Write an unsigned 32-bit integer in host byte order into the buffer in network byte order.
Definition buffer.h:531
dhcp6.h
exceptions.h
isc_throw
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
Definition exceptions/exceptions.h:210
io.h
io_address.h
libdhcp++.h
isc::asiolink
Definition addr_utilities.cc:170
isc::asiolink::firstAddrInPrefix
IOAddress firstAddrInPrefix(const IOAddress &prefix, uint8_t len)
This code is based on similar code from the Dibbler project.
Definition addr_utilities.cc:172
isc::dhcp
Definition dhcp4/client_handler.cc:20
isc::dhcp::OptionBuffer
std::vector< uint8_t > OptionBuffer
buffer types used in DHCP code.
Definition option.h:24
isc::dhcp::OptionPtr
boost::shared_ptr< Option > OptionPtr
Definition option.h:37
isc::util
Definition time_utils.cc:43
isc::util::readUint32
uint32_t readUint32(void const *const buffer, size_t const length)
uint32_t wrapper over readUint.
Definition io.h:82
isc
Defines the logger used by the top-level component of kea-lfc.
Definition check_exists_add.cc:18
option6_iaprefix.h
option_space.h
DHCP6_OPTION_SPACE
#define DHCP6_OPTION_SPACE
Definition std_option_defs.h:17