Kea 2.5.4
pkt.cc
Go to the documentation of this file.
1// Copyright (C) 2014-2023 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#include <utility>
9#include <dhcp/pkt.h>
10#include <dhcp/iface_mgr.h>
11#include <dhcp/hwaddr.h>
12#include <vector>
13
14namespace isc {
15namespace dhcp {
16
17Pkt::Pkt(uint32_t transid, const isc::asiolink::IOAddress& local_addr,
18 const isc::asiolink::IOAddress& remote_addr, uint16_t local_port,
19 uint16_t remote_port)
20 : transid_(transid), iface_(""), ifindex_(UNSET_IFINDEX), local_addr_(local_addr),
21 remote_addr_(remote_addr), local_port_(local_port),
22 remote_port_(remote_port), buffer_out_(0), copy_retrieved_options_(false) {
23}
24
25Pkt::Pkt(const uint8_t* buf, uint32_t len, const isc::asiolink::IOAddress& local_addr,
26 const isc::asiolink::IOAddress& remote_addr, uint16_t local_port,
27 uint16_t remote_port)
28 : transid_(0), iface_(""), ifindex_(UNSET_IFINDEX), local_addr_(local_addr),
29 remote_addr_(remote_addr), local_port_(local_port),
30 remote_port_(remote_port), buffer_out_(0), copy_retrieved_options_(false) {
31 if (len != 0) {
32 if (buf == NULL) {
33 isc_throw(InvalidParameter, "data buffer passed to Pkt is NULL");
34 }
35 data_.resize(len);
36 memcpy(&data_[0], buf, len);
37 }
38}
39
42 OptionCollection options;
43 for (auto const& option : options_) {
44 options.emplace(std::make_pair(option.second->getType(), option.second->clone()));
45 }
46 return (options);
47}
48
49void
51 options_.insert(std::pair<int, OptionPtr>(opt->getType(), opt));
52}
53
55Pkt::getNonCopiedOption(const uint16_t type) const {
56 const auto& x = options_.find(type);
57 if (x != options_.end()) {
58 return (x->second);
59 }
60 return (OptionPtr());
61}
62
64Pkt::getOption(const uint16_t type) {
65 const auto& x = options_.find(type);
66 if (x != options_.end()) {
68 OptionPtr option_copy = x->second->clone();
69 x->second = option_copy;
70 }
71 return (x->second);
72 }
73 return (OptionPtr()); // NULL
74}
75
77Pkt::getNonCopiedOptions(const uint16_t opt_type) const {
78 std::pair<OptionCollection::const_iterator,
79 OptionCollection::const_iterator> range = options_.equal_range(opt_type);
80 return (OptionCollection(range.first, range.second));
81}
82
84Pkt::getOptions(const uint16_t opt_type) {
85 OptionCollection options_copy;
86
87 std::pair<OptionCollection::iterator,
88 OptionCollection::iterator> range = options_.equal_range(opt_type);
89 // If options should be copied on retrieval, we should now iterate over
90 // matching options, copy them and replace the original ones with new
91 // instances.
93 for (OptionCollection::iterator opt_it = range.first;
94 opt_it != range.second; ++opt_it) {
95 OptionPtr option_copy = opt_it->second->clone();
96 opt_it->second = option_copy;
97 }
98 }
99 // Finally, return updated options. This can also be empty in some cases.
100 return (OptionCollection(range.first, range.second));
101}
102
103bool
104Pkt::delOption(uint16_t type) {
105 const auto& x = options_.find(type);
106 if (x != options_.end()) {
107 options_.erase(x);
108 return (true); // delete successful
109 } else {
110 return (false); // can't find option to be deleted
111 }
112}
113
114bool
115Pkt::inClass(const ClientClass& client_class) {
116 return (classes_.contains(client_class));
117}
118
119void
120Pkt::addClass(const ClientClass& client_class, bool required) {
121 ClientClasses& classes = !required ? classes_ : required_classes_;
122 if (!classes.contains(client_class)) {
123 classes.insert(client_class);
124 static_cast<void>(subclasses_.push_back(SubClassRelation(client_class, client_class)));
125 }
126}
127
128void
129Pkt::addSubClass(const ClientClass& class_def, const ClientClass& subclass) {
130 if (!classes_.contains(class_def)) {
131 classes_.insert(class_def);
132 static_cast<void>(subclasses_.push_back(SubClassRelation(class_def, subclass)));
133 }
134 if (!classes_.contains(subclass)) {
135 classes_.insert(subclass);
136 static_cast<void>(subclasses_.push_back(SubClassRelation(subclass, subclass)));
137 }
138}
139
140void
142 timestamp_ = boost::posix_time::microsec_clock::universal_time();
143}
144
146 if (!data_.empty()) {
147 buffer_out_.writeData(&data_[0], data_.size());
148 }
149}
150
151void
152Pkt::setRemoteHWAddr(const uint8_t htype, const uint8_t hlen,
153 const std::vector<uint8_t>& hw_addr) {
154 setHWAddrMember(htype, hlen, hw_addr, remote_hwaddr_);
155}
156
157void
159 if (!hw_addr) {
160 isc_throw(BadValue, "Setting remote HW address to NULL is"
161 << " forbidden.");
162 }
163 remote_hwaddr_ = hw_addr;
164}
165
166void
167Pkt::setHWAddrMember(const uint8_t htype, const uint8_t,
168 const std::vector<uint8_t>& hw_addr,
169 HWAddrPtr& storage) {
170 storage.reset(new HWAddr(hw_addr, htype));
171}
172
174Pkt::getMAC(uint32_t hw_addr_src) {
175 HWAddrPtr mac;
176
178
179 // Method 1: from raw sockets.
180 if (hw_addr_src & HWAddr::HWADDR_SOURCE_RAW) {
181 mac = getRemoteHWAddr();
182 if (mac) {
183 mac->source_ = HWAddr::HWADDR_SOURCE_RAW;
184 return (mac);
185 } else if (hw_addr_src == HWAddr::HWADDR_SOURCE_RAW) {
186 // If we're interested only in RAW sockets as source of that info,
187 // there's no point in trying other options.
188 return (HWAddrPtr());
189 }
190 }
191
192 // Method 2: From client link-layer address option inserted by a relay
195 if (mac) {
196 return (mac);
197 } else if (hw_addr_src == HWAddr::HWADDR_SOURCE_CLIENT_ADDR_RELAY_OPTION) {
198 // If we're interested only in RFC6939 link layer address as source
199 // of that info, there's no point in trying other options.
200 return (HWAddrPtr());
201 }
202 }
203
204 // Method 3: Extracted from DUID-LLT or DUID-LL
205 if(hw_addr_src & HWAddr::HWADDR_SOURCE_DUID) {
206 mac = getMACFromDUID();
207 if (mac) {
208 return (mac);
209 } else if (hw_addr_src == HWAddr::HWADDR_SOURCE_DUID) {
210 // If the only source allowed is DUID then we can skip the other
211 // methods.
212 return (HWAddrPtr());
213 }
214 }
215
216 // Method 4: Extracted from source IPv6 link-local address
217 if (hw_addr_src & HWAddr::HWADDR_SOURCE_IPV6_LINK_LOCAL) {
219 if (mac) {
220 return (mac);
221 } else if (hw_addr_src == HWAddr::HWADDR_SOURCE_IPV6_LINK_LOCAL) {
222 // If we're interested only in link-local addr as source of that
223 // info, there's no point in trying other options.
224 return (HWAddrPtr());
225 }
226 }
227
228 // Method 5: From remote-id option inserted by a relay
229 if(hw_addr_src & HWAddr::HWADDR_SOURCE_REMOTE_ID) {
231 if (mac) {
232 return (mac);
233 } else if (hw_addr_src == HWAddr::HWADDR_SOURCE_REMOTE_ID) {
234 // If the only source allowed is remote-id option then we can skip
235 // the other methods.
236 return (HWAddrPtr());
237 }
238 }
239
240 // Method 6: From subscriber-id option inserted by a relay
241
242 // Method 7: From docsis options
243 if (hw_addr_src & HWAddr::HWADDR_SOURCE_DOCSIS_CMTS) {
244 mac = getMACFromDocsisCMTS();
245 if (mac) {
246 return (mac);
247 } else if (hw_addr_src == HWAddr::HWADDR_SOURCE_DOCSIS_CMTS) {
248 // If we're interested only in CMTS options as a source of that
249 // info, there's no point in trying other options.
250 return (HWAddrPtr());
251 }
252 }
253
254 // Method 8: From docsis options
255 if (hw_addr_src & HWAddr::HWADDR_SOURCE_DOCSIS_MODEM) {
256 mac = getMACFromDocsisModem();
257 if (mac) {
258 return (mac);
259 } else if (hw_addr_src == HWAddr::HWADDR_SOURCE_DOCSIS_MODEM) {
260 // If we're interested only in CMTS options as a source of that
261 // info, there's no point in trying other options.
262 return (HWAddrPtr());
263 }
264 }
265
266 // Ok, none of the methods were suitable. Return NULL.
267 return (HWAddrPtr());
268}
269
272 HWAddrPtr mac;
273
274 if (addr.isV6LinkLocal()) {
275 std::vector<uint8_t> bin = addr.toBytes();
276
277 // Double check that it's of appropriate size
278 if ((bin.size() == isc::asiolink::V6ADDRESS_LEN) &&
279 // Check that it's link-local (starts with fe80).
280 (bin[0] == 0xfe) && (bin[1] == 0x80) &&
281 // Check that u bit is set and g is clear.
282 // See Section 2.5.1 of RFC2373 for details.
283 ((bin[8] & 3) == 2) &&
284 // And that the IID is of EUI-64 type.
285 (bin[11] == 0xff) && (bin[12] == 0xfe)) {
286
287 // Remove 8 most significant bytes
288 bin.erase(bin.begin(), bin.begin() + 8);
289
290 // Ok, we're down to EUI-64 only now: XX:XX:XX:ff:fe:XX:XX:XX
291 bin.erase(bin.begin() + 3, bin.begin() + 5);
292
293 // MAC-48 to EUI-64 involves inverting u bit (see explanation
294 // in Section 2.5.1 of RFC2373). We need to revert that.
295 bin[0] = bin[0] ^ 2;
296
297 // Let's get the interface this packet was received on.
298 // We need it to get hardware type
300 uint16_t hwtype = 0; // not specified
301 if (iface) {
302 hwtype = iface->getHWType();
303 }
304
305 mac.reset(new HWAddr(bin, hwtype));
307 }
308 }
309
310 return (mac);
311}
312
313} // end of namespace isc::dhcp
314} // end of namespace isc
A generic exception that is thrown if a parameter given to a method is considered invalid in that con...
A generic exception that is thrown if a parameter given to a method or function is considered invalid...
Container for storing client class names.
Definition: classify.h:108
bool contains(const ClientClass &x) const
returns if class x belongs to the defined classes
Definition: classify.cc:49
void insert(const ClientClass &class_name)
Insert an element.
Definition: classify.h:128
IfacePtr getIface(const unsigned int ifindex)
Returns interface specified interface index.
Definition: iface_mgr.cc:866
static IfaceMgr & instance()
IfaceMgr is a singleton class.
Definition: iface_mgr.cc:54
bool delOption(uint16_t type)
Attempts to delete first suboption of requested type.
Definition: pkt.cc:104
virtual HWAddrPtr getMACFromDocsisModem()=0
Attempts to extract MAC/Hardware address from DOCSIS options inserted by the modem itself.
isc::dhcp::OptionCollection getOptions(const uint16_t type)
Returns all instances of specified type.
Definition: pkt.cc:84
virtual HWAddrPtr getMACFromDocsisCMTS()=0
Attempts to extract MAC/Hardware address from DOCSIS options inserted by the CMTS (the relay agent)
ClientClasses required_classes_
Classes which are required to be evaluated.
Definition: pkt.h:663
void repack()
Copies content of input buffer to output buffer.
Definition: pkt.cc:145
virtual HWAddrPtr getMACFromRemoteIdRelayOption()=0
Attempts to obtain MAC address from remote-id relay option.
OptionBuffer data_
Unparsed data (in received packets).
Definition: pkt.h:340
HWAddrPtr getRemoteHWAddr() const
Returns the remote HW address obtained from raw sockets.
Definition: pkt.h:618
virtual HWAddrPtr getMACFromSrcLinkLocalAddr()=0
Attempts to obtain MAC address from source link-local IPv6 address.
ClientClasses classes_
Classes this packet belongs to.
Definition: pkt.h:655
virtual size_t len()=0
Returns packet size in binary format.
HWAddrPtr remote_hwaddr_
Definition: pkt.h:843
isc::dhcp::OptionCollection options_
Collection of options present in this message.
Definition: pkt.h:681
isc::util::OutputBuffer buffer_out_
Output buffer (used during message transmission)
Definition: pkt.h:831
virtual HWAddrPtr getMACFromDUID()=0
Attempts to obtain MAC address from DUID-LL or DUID-LLT.
SubClassRelationContainer subclasses_
SubClasses this packet belongs to.
Definition: pkt.h:671
OptionCollection getNonCopiedOptions(const uint16_t opt_type) const
Returns all option instances of specified type without copying.
Definition: pkt.cc:77
Pkt(uint32_t transid, const isc::asiolink::IOAddress &local_addr, const isc::asiolink::IOAddress &remote_addr, uint16_t local_port, uint16_t remote_port)
Constructor.
Definition: pkt.cc:17
OptionCollection cloneOptions()
Clones all options so that they can be safely modified.
Definition: pkt.cc:41
OptionPtr getOption(const uint16_t type)
Returns the first option of specified type.
Definition: pkt.cc:64
void setRemoteHWAddr(const HWAddrPtr &hw_addr)
Sets remote hardware address.
Definition: pkt.cc:158
void addSubClass(const isc::dhcp::ClientClass &class_def, const isc::dhcp::ClientClass &subclass)
Adds a specified subclass to the packet.
Definition: pkt.cc:129
bool inClass(const isc::dhcp::ClientClass &client_class)
Checks whether a client belongs to a given class.
Definition: pkt.cc:115
virtual HWAddrPtr getMACFromIPv6RelayOpt()=0
Attempts to obtain MAC address from relay option client-linklayer-addr.
boost::posix_time::ptime timestamp_
packet timestamp
Definition: pkt.h:840
HWAddrPtr getMAC(uint32_t hw_addr_src)
Returns MAC address.
Definition: pkt.cc:174
void updateTimestamp()
Update packet timestamp.
Definition: pkt.cc:141
bool copy_retrieved_options_
Indicates if a copy of the retrieved option should be returned when Pkt::getOption is called.
Definition: pkt.h:837
std::string iface_
Name of the network interface the packet was received/to be sent over.
Definition: pkt.h:796
void addClass(const isc::dhcp::ClientClass &client_class, bool required=false)
Adds a specified class to the packet.
Definition: pkt.cc:120
OptionPtr getNonCopiedOption(const uint16_t type) const
Returns the first option of specified type without copying.
Definition: pkt.cc:55
HWAddrPtr getMACFromIPv6(const isc::asiolink::IOAddress &addr)
Attempts to convert IPv6 address into MAC.
Definition: pkt.cc:271
virtual void addOption(const OptionPtr &opt)
Adds an option to this packet.
Definition: pkt.cc:50
void writeData(const void *data, size_t len)
Copy an arbitrary length of data into the buffer.
Definition: buffer.h:550
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
static const uint32_t HWADDR_SOURCE_RAW
Obtained first hand from raw socket (100% reliable).
Definition: hwaddr.h:44
static const uint32_t HWADDR_SOURCE_REMOTE_ID
A relay can insert remote-id.
Definition: hwaddr.h:63
static const uint32_t HWADDR_SOURCE_CLIENT_ADDR_RELAY_OPTION
Get it from RFC6939 option.
Definition: hwaddr.h:59
static const uint32_t HWADDR_SOURCE_IPV6_LINK_LOCAL
Extracted from IPv6 link-local address.
Definition: hwaddr.h:53
static const uint32_t HWADDR_SOURCE_DOCSIS_MODEM
A cable modem (acting as DHCP client) that supports DOCSIS standard can insert DOCSIS options that co...
Definition: hwaddr.h:79
static const uint32_t HWADDR_SOURCE_DUID
Extracted from DUID-LL or DUID-LLT (not 100% reliable as the client can send fake DUID).
Definition: hwaddr.h:48
static const uint32_t HWADDR_SOURCE_DOCSIS_CMTS
A CMTS (acting as DHCP relay agent) that supports DOCSIS standard can insert DOCSIS options that cont...
Definition: hwaddr.h:73
std::string ClientClass
Defines a single class name.
Definition: classify.h:42
boost::shared_ptr< Iface > IfacePtr
Type definition for the pointer to an Iface object.
Definition: iface_mgr.h:487
std::multimap< unsigned int, OptionPtr > OptionCollection
A collection of DHCP (v4 or v6) options.
Definition: option.h:40
boost::shared_ptr< HWAddr > HWAddrPtr
Shared pointer to a hardware address structure.
Definition: hwaddr.h:154
constexpr unsigned int UNSET_IFINDEX
A value used to signal that the interface index was not set.
Definition: pkt.h:30
boost::shared_ptr< Option > OptionPtr
Definition: option.h:37
Defines the logger used by the top-level component of kea-lfc.
Hardware type that represents information from DHCPv4 packet.
Definition: hwaddr.h:20
Defines a subclass to template class relation.
Definition: classify.h:67