Kea 2.5.8
pkt.cc
Go to the documentation of this file.
1// Copyright (C) 2014-2024 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 <boost/foreach.hpp>
13#include <vector>
14
15using namespace boost::posix_time;
16
17namespace isc {
18namespace dhcp {
19
20const std::string PktEvent::SOCKET_RECEIVED("socket_received");
21const std::string PktEvent::BUFFER_READ("buffer_read");
22const std::string PktEvent::RESPONSE_SENT("response_sent");
23
24Pkt::Pkt(uint32_t transid, const isc::asiolink::IOAddress& local_addr,
25 const isc::asiolink::IOAddress& remote_addr, uint16_t local_port,
26 uint16_t remote_port)
27 : transid_(transid), iface_(""), ifindex_(UNSET_IFINDEX), local_addr_(local_addr),
28 remote_addr_(remote_addr), local_port_(local_port),
29 remote_port_(remote_port), buffer_out_(0), copy_retrieved_options_(false) {
30}
31
32Pkt::Pkt(const uint8_t* buf, uint32_t len, const isc::asiolink::IOAddress& local_addr,
33 const isc::asiolink::IOAddress& remote_addr, uint16_t local_port,
34 uint16_t remote_port)
35 : transid_(0), iface_(""), ifindex_(UNSET_IFINDEX), local_addr_(local_addr),
36 remote_addr_(remote_addr), local_port_(local_port),
37 remote_port_(remote_port), buffer_out_(0), copy_retrieved_options_(false) {
38 if (len != 0) {
39 if (buf == NULL) {
40 isc_throw(InvalidParameter, "data buffer passed to Pkt is NULL");
41 }
42 data_.resize(len);
43 memcpy(&data_[0], buf, len);
44 }
45}
46
49 OptionCollection options;
50 for (auto const& option : options_) {
51 options.emplace(std::make_pair(option.second->getType(), option.second->clone()));
52 }
53 return (options);
54}
55
56void
58 options_.insert(std::pair<int, OptionPtr>(opt->getType(), opt));
59}
60
62Pkt::getNonCopiedOption(const uint16_t type) const {
63 auto const& x = options_.find(type);
64 if (x != options_.end()) {
65 return (x->second);
66 }
67 return (OptionPtr());
68}
69
71Pkt::getOption(const uint16_t type) {
72 auto const& x = options_.find(type);
73 if (x != options_.end()) {
75 OptionPtr option_copy = x->second->clone();
76 x->second = option_copy;
77 }
78 return (x->second);
79 }
80 return (OptionPtr()); // NULL
81}
82
84Pkt::getNonCopiedOptions(const uint16_t opt_type) const {
85 std::pair<OptionCollection::const_iterator,
86 OptionCollection::const_iterator> range = options_.equal_range(opt_type);
87 return (OptionCollection(range.first, range.second));
88}
89
91Pkt::getOptions(const uint16_t opt_type) {
92 OptionCollection options_copy;
93
94 std::pair<OptionCollection::iterator,
95 OptionCollection::iterator> range = options_.equal_range(opt_type);
96 // If options should be copied on retrieval, we should now iterate over
97 // matching options, copy them and replace the original ones with new
98 // instances.
100 BOOST_FOREACH(auto& opt_it, range) {
101 OptionPtr option_copy = opt_it.second->clone();
102 opt_it.second = option_copy;
103 }
104 }
105 // Finally, return updated options. This can also be empty in some cases.
106 return (OptionCollection(range.first, range.second));
107}
108
109bool
110Pkt::delOption(uint16_t type) {
111 auto const& x = options_.find(type);
112 if (x != options_.end()) {
113 options_.erase(x);
114 return (true); // delete successful
115 } else {
116 return (false); // can't find option to be deleted
117 }
118}
119
120bool
121Pkt::inClass(const ClientClass& client_class) {
122 return (classes_.contains(client_class));
123}
124
125void
126Pkt::addClass(const ClientClass& client_class, bool required) {
127 ClientClasses& classes = !required ? classes_ : required_classes_;
128 if (!classes.contains(client_class)) {
129 classes.insert(client_class);
130 static_cast<void>(subclasses_.push_back(SubClassRelation(client_class, client_class)));
131 }
132}
133
134void
135Pkt::addSubClass(const ClientClass& class_def, const ClientClass& subclass) {
136 if (!classes_.contains(class_def)) {
137 classes_.insert(class_def);
138 static_cast<void>(subclasses_.push_back(SubClassRelation(class_def, subclass)));
139 }
140 if (!classes_.contains(subclass)) {
141 classes_.insert(subclass);
142 static_cast<void>(subclasses_.push_back(SubClassRelation(subclass, subclass)));
143 }
144}
145
146void
148 timestamp_ = boost::posix_time::microsec_clock::universal_time();
149}
150
152 if (!data_.empty()) {
153 buffer_out_.writeData(&data_[0], data_.size());
154 }
155}
156
157void
158Pkt::setRemoteHWAddr(const uint8_t htype, const uint8_t hlen,
159 const std::vector<uint8_t>& hw_addr) {
160 setHWAddrMember(htype, hlen, hw_addr, remote_hwaddr_);
161}
162
163void
165 if (!hw_addr) {
166 isc_throw(BadValue, "Setting remote HW address to NULL is"
167 << " forbidden.");
168 }
169 remote_hwaddr_ = hw_addr;
170}
171
172void
173Pkt::setHWAddrMember(const uint8_t htype, const uint8_t,
174 const std::vector<uint8_t>& hw_addr,
175 HWAddrPtr& storage) {
176 storage.reset(new HWAddr(hw_addr, htype));
177}
178
180Pkt::getMAC(uint32_t hw_addr_src) {
181 HWAddrPtr mac;
182
184
185 // Method 1: from raw sockets.
186 if (hw_addr_src & HWAddr::HWADDR_SOURCE_RAW) {
187 mac = getRemoteHWAddr();
188 if (mac) {
189 mac->source_ = HWAddr::HWADDR_SOURCE_RAW;
190 return (mac);
191 } else if (hw_addr_src == HWAddr::HWADDR_SOURCE_RAW) {
192 // If we're interested only in RAW sockets as source of that info,
193 // there's no point in trying other options.
194 return (HWAddrPtr());
195 }
196 }
197
198 // Method 2: From client link-layer address option inserted by a relay
201 if (mac) {
202 return (mac);
203 } else if (hw_addr_src == HWAddr::HWADDR_SOURCE_CLIENT_ADDR_RELAY_OPTION) {
204 // If we're interested only in RFC6939 link layer address as source
205 // of that info, there's no point in trying other options.
206 return (HWAddrPtr());
207 }
208 }
209
210 // Method 3: Extracted from DUID-LLT or DUID-LL
211 if(hw_addr_src & HWAddr::HWADDR_SOURCE_DUID) {
212 mac = getMACFromDUID();
213 if (mac) {
214 return (mac);
215 } else if (hw_addr_src == HWAddr::HWADDR_SOURCE_DUID) {
216 // If the only source allowed is DUID then we can skip the other
217 // methods.
218 return (HWAddrPtr());
219 }
220 }
221
222 // Method 4: Extracted from source IPv6 link-local address
223 if (hw_addr_src & HWAddr::HWADDR_SOURCE_IPV6_LINK_LOCAL) {
225 if (mac) {
226 return (mac);
227 } else if (hw_addr_src == HWAddr::HWADDR_SOURCE_IPV6_LINK_LOCAL) {
228 // If we're interested only in link-local addr as source of that
229 // info, there's no point in trying other options.
230 return (HWAddrPtr());
231 }
232 }
233
234 // Method 5: From remote-id option inserted by a relay
235 if(hw_addr_src & HWAddr::HWADDR_SOURCE_REMOTE_ID) {
237 if (mac) {
238 return (mac);
239 } else if (hw_addr_src == HWAddr::HWADDR_SOURCE_REMOTE_ID) {
240 // If the only source allowed is remote-id option then we can skip
241 // the other methods.
242 return (HWAddrPtr());
243 }
244 }
245
246 // Method 6: From subscriber-id option inserted by a relay
247
248 // Method 7: From docsis options
249 if (hw_addr_src & HWAddr::HWADDR_SOURCE_DOCSIS_CMTS) {
250 mac = getMACFromDocsisCMTS();
251 if (mac) {
252 return (mac);
253 } else if (hw_addr_src == HWAddr::HWADDR_SOURCE_DOCSIS_CMTS) {
254 // If we're interested only in CMTS options as a source of that
255 // info, there's no point in trying other options.
256 return (HWAddrPtr());
257 }
258 }
259
260 // Method 8: From docsis options
261 if (hw_addr_src & HWAddr::HWADDR_SOURCE_DOCSIS_MODEM) {
262 mac = getMACFromDocsisModem();
263 if (mac) {
264 return (mac);
265 } else if (hw_addr_src == HWAddr::HWADDR_SOURCE_DOCSIS_MODEM) {
266 // If we're interested only in CMTS options as a source of that
267 // info, there's no point in trying other options.
268 return (HWAddrPtr());
269 }
270 }
271
272 // Ok, none of the methods were suitable. Return NULL.
273 return (HWAddrPtr());
274}
275
278 HWAddrPtr mac;
279
280 if (addr.isV6LinkLocal()) {
281 std::vector<uint8_t> bin = addr.toBytes();
282
283 // Double check that it's of appropriate size
284 if ((bin.size() == isc::asiolink::V6ADDRESS_LEN) &&
285 // Check that it's link-local (starts with fe80).
286 (bin[0] == 0xfe) && (bin[1] == 0x80) &&
287 // Check that u bit is set and g is clear.
288 // See Section 2.5.1 of RFC2373 for details.
289 ((bin[8] & 3) == 2) &&
290 // And that the IID is of EUI-64 type.
291 (bin[11] == 0xff) && (bin[12] == 0xfe)) {
292
293 // Remove 8 most significant bytes
294 bin.erase(bin.begin(), bin.begin() + 8);
295
296 // Ok, we're down to EUI-64 only now: XX:XX:XX:ff:fe:XX:XX:XX
297 bin.erase(bin.begin() + 3, bin.begin() + 5);
298
299 // MAC-48 to EUI-64 involves inverting u bit (see explanation
300 // in Section 2.5.1 of RFC2373). We need to revert that.
301 bin[0] = bin[0] ^ 2;
302
303 // Let's get the interface this packet was received on.
304 // We need it to get hardware type
306 uint16_t hwtype = 0; // not specified
307 if (iface) {
308 hwtype = iface->getHWType();
309 }
310
311 mac.reset(new HWAddr(bin, hwtype));
313 }
314 }
315
316 return (mac);
317}
318
319
320void
321Pkt::addPktEvent(const std::string& label, const boost::posix_time::ptime& timestamp) {
322 events_.push_back(PktEvent(label, timestamp));
323}
324
325void
326Pkt::setPktEvent(const std::string& label, const ptime& timestamp) {
327 for (auto& event : events_) {
328 if (event.label_ == label) {
329 event.timestamp_ = timestamp;
330 return;
331 }
332 }
333
334 events_.push_back(PktEvent(label, timestamp));
335}
336
337void
338Pkt::addPktEvent(const std::string& label, const struct timeval& tv) {
339 time_t time_t_secs = tv.tv_sec;
340 ptime timestamp = from_time_t(time_t_secs);
341 time_duration usecs(0, 0, 0, tv.tv_usec);
342 timestamp += usecs;
343 addPktEvent(label, timestamp);
344}
345
346ptime
347Pkt::getPktEventTime(const std::string& label) const {
348 for (auto const& event : events_) {
349 if (event.label_ == label) {
350 return (event.timestamp_);
351 }
352 }
353
354 return (PktEvent::EMPTY_TIME());
355}
356
357void
359 events_.clear();
360}
361
362std::string
363Pkt::dumpPktEvents(bool verbose /* = false */) const {
364 std::stringstream oss;
365 if (verbose) {
366 oss << "Event log: " << std::endl;
367 }
368
369 bool first_pass = true;
370 boost::posix_time::ptime beg_time;
371 boost::posix_time::ptime prev_time;
372 for (auto const& event : events_) {
373 if (!verbose) {
374 oss << (first_pass ? "" : ", ") << event.timestamp_ << " : " << event.label_;
375 } else {
376 oss << event.timestamp_ << " : " << event.label_;
377 if (first_pass) {
378 oss << std::endl;
379 beg_time = event.timestamp_;
380 } else {
381 oss << " elapsed: " << event.timestamp_ - prev_time << std::endl;
382 }
383
384 prev_time = event.timestamp_;
385 }
386
387 first_pass = false;
388 }
389
390 if (verbose) {
391 oss << "total elapsed: " << prev_time - beg_time;
392 }
393
394 return (oss.str());
395}
396
397} // end of namespace isc::dhcp
398} // 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:879
static IfaceMgr & instance()
IfaceMgr is a singleton class.
Definition: iface_mgr.cc:54
Describes an event during the life cycle of a packet.
Definition: pkt.h:89
static const std::string BUFFER_READ
Event that marks when a packet is read from the socket buffer by application.
Definition: pkt.h:97
static const std::string SOCKET_RECEIVED
Event that marks when a packet is placed in the socket buffer by the kernel.
Definition: pkt.h:93
static const std::string RESPONSE_SENT
Event that marks when a packet is been written to the socket by application.
Definition: pkt.h:101
static boost::posix_time::ptime & EMPTY_TIME()
Fetch an empty timestamp, used for logic comparisons.
Definition: pkt.h:124
bool delOption(uint16_t type)
Attempts to delete first suboption of requested type.
Definition: pkt.cc:110
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:91
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:778
void repack()
Copies content of input buffer to output buffer.
Definition: pkt.cc:151
virtual HWAddrPtr getMACFromRemoteIdRelayOption()=0
Attempts to obtain MAC address from remote-id relay option.
OptionBuffer data_
Unparsed data (in received packets).
Definition: pkt.h:404
HWAddrPtr getRemoteHWAddr() const
Returns the remote HW address obtained from raw sockets.
Definition: pkt.h:733
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:770
void setPktEvent(const std::string &label, const boost::posix_time::ptime &timestamp=PktEvent::now())
Updates (or adds) an event in the event stack.
Definition: pkt.cc:326
virtual size_t len()=0
Returns packet size in binary format.
HWAddrPtr remote_hwaddr_
Definition: pkt.h:958
isc::dhcp::OptionCollection options_
Collection of options present in this message.
Definition: pkt.h:796
isc::util::OutputBuffer buffer_out_
Output buffer (used during message transmission)
Definition: pkt.h:946
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:786
void addPktEvent(const std::string &label, const boost::posix_time::ptime &timestamp=PktEvent::now())
Adds an event to the end of the event stack.
Definition: pkt.cc:321
void clearPktEvents()
Discards contents of the packet event stack.
Definition: pkt.cc:358
OptionCollection getNonCopiedOptions(const uint16_t opt_type) const
Returns all option instances of specified type without copying.
Definition: pkt.cc:84
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:24
boost::posix_time::ptime getPktEventTime(const std::string &label) const
Fetches the timestamp for a given event in the stack.
Definition: pkt.cc:347
OptionCollection cloneOptions()
Clones all options so that they can be safely modified.
Definition: pkt.cc:48
OptionPtr getOption(const uint16_t type)
Returns the first option of specified type.
Definition: pkt.cc:71
void setRemoteHWAddr(const HWAddrPtr &hw_addr)
Sets remote hardware address.
Definition: pkt.cc:164
void addSubClass(const isc::dhcp::ClientClass &class_def, const isc::dhcp::ClientClass &subclass)
Adds a specified subclass to the packet.
Definition: pkt.cc:135
bool inClass(const isc::dhcp::ClientClass &client_class)
Checks whether a client belongs to a given class.
Definition: pkt.cc:121
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:955
HWAddrPtr getMAC(uint32_t hw_addr_src)
Returns MAC address.
Definition: pkt.cc:180
void updateTimestamp()
Update packet timestamp.
Definition: pkt.cc:147
bool copy_retrieved_options_
Indicates if a copy of the retrieved option should be returned when Pkt::getOption is called.
Definition: pkt.h:952
std::string iface_
Name of the network interface the packet was received/to be sent over.
Definition: pkt.h:911
std::string dumpPktEvents(bool verbose=false) const
Creates a dump of the stack contents to a string for logging.
Definition: pkt.cc:363
void addClass(const isc::dhcp::ClientClass &client_class, bool required=false)
Adds a specified class to the packet.
Definition: pkt.cc:126
OptionPtr getNonCopiedOption(const uint16_t type) const
Returns the first option of specified type without copying.
Definition: pkt.cc:62
HWAddrPtr getMACFromIPv6(const isc::asiolink::IOAddress &addr)
Attempts to convert IPv6 address into MAC.
Definition: pkt.cc:277
virtual void addOption(const OptionPtr &opt)
Adds an option to this packet.
Definition: pkt.cc:57
void writeData(const void *data, size_t len)
Copy an arbitrary length of data into the buffer.
Definition: buffer.h:556
#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