1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
|
// Copyright (C) 2012-2014 Internet Systems Consortium, Inc. ("ISC")
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
// AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
// PERFORMANCE OF THIS SOFTWARE.
#include <asiolink/io_address.h>
#include <dhcp/option_space.h>
#include <dhcpsrv/addr_utilities.h>
#include <dhcpsrv/subnet.h>
#include <sstream>
using namespace isc::asiolink;
namespace isc {
namespace dhcp {
// This is an initial value of subnet-id. See comments in subnet.h for details.
SubnetID Subnet::static_id_ = 1;
Subnet::Subnet(const isc::asiolink::IOAddress& prefix, uint8_t len,
const Triplet<uint32_t>& t1,
const Triplet<uint32_t>& t2,
const Triplet<uint32_t>& valid_lifetime,
const isc::dhcp::Subnet::RelayInfo& relay,
const SubnetID id)
:id_(id == 0 ? generateNextID() : id), prefix_(prefix), prefix_len_(len),
t1_(t1), t2_(t2), valid_(valid_lifetime),
last_allocated_ia_(lastAddrInPrefix(prefix, len)),
last_allocated_ta_(lastAddrInPrefix(prefix, len)),
last_allocated_pd_(lastAddrInPrefix(prefix, len)), relay_(relay)
{
if ((prefix.isV6() && len > 128) ||
(prefix.isV4() && len > 32)) {
isc_throw(BadValue,
"Invalid prefix length specified for subnet: " << len);
}
}
Subnet::RelayInfo::RelayInfo(const isc::asiolink::IOAddress& addr)
:addr_(addr) {
}
bool
Subnet::inRange(const isc::asiolink::IOAddress& addr) const {
IOAddress first = firstAddrInPrefix(prefix_, prefix_len_);
IOAddress last = lastAddrInPrefix(prefix_, prefix_len_);
return ((first <= addr) && (addr <= last));
}
void
Subnet::addOption(const OptionPtr& option, bool persistent,
const std::string& option_space) {
// Check that the option space name is valid.
if (!OptionSpace::validateName(option_space)) {
isc_throw(isc::BadValue, "invalid option space name: '"
<< option_space << "'");
}
validateOption(option);
// Actually add new option descriptor.
option_spaces_.addItem(OptionDescriptor(option, persistent), option_space);
}
void
Subnet::setRelayInfo(const isc::dhcp::Subnet::RelayInfo& relay) {
relay_ = relay;
}
bool
Subnet::clientSupported(const isc::dhcp::ClientClasses& classes) const {
if (white_list_.empty()) {
return (true); // There is no class defined for this subnet, so we do
// support everyone.
}
for (ClientClasses::const_iterator it = white_list_.begin();
it != white_list_.end(); ++it) {
if (classes.contains(*it)) {
return (true);
}
}
return (false);
}
void
Subnet::allowClientClass(const isc::dhcp::ClientClass& class_name) {
white_list_.insert(class_name);
}
void
Subnet::delOptions() {
option_spaces_.clearItems();
}
Subnet::OptionContainerPtr
Subnet::getOptionDescriptors(const std::string& option_space) const {
return (option_spaces_.getItems(option_space));
}
Subnet::OptionDescriptor
Subnet::getOptionDescriptor(const std::string& option_space,
const uint16_t option_code) {
OptionContainerPtr options = getOptionDescriptors(option_space);
if (!options || options->empty()) {
return (OptionDescriptor(false));
}
const OptionContainerTypeIndex& idx = options->get<1>();
const OptionContainerTypeRange& range = idx.equal_range(option_code);
if (std::distance(range.first, range.second) == 0) {
return (OptionDescriptor(false));
}
return (*range.first);
}
void Subnet::addVendorOption(const OptionPtr& option, bool persistent,
uint32_t vendor_id){
validateOption(option);
vendor_option_spaces_.addItem(OptionDescriptor(option, persistent), vendor_id);
}
Subnet::OptionContainerPtr
Subnet::getVendorOptionDescriptors(uint32_t vendor_id) const {
return (vendor_option_spaces_.getItems(vendor_id));
}
Subnet::OptionDescriptor
Subnet::getVendorOptionDescriptor(uint32_t vendor_id, uint16_t option_code) {
OptionContainerPtr options = getVendorOptionDescriptors(vendor_id);
if (!options || options->empty()) {
return (OptionDescriptor(false));
}
const OptionContainerTypeIndex& idx = options->get<1>();
const OptionContainerTypeRange& range = idx.equal_range(option_code);
if (std::distance(range.first, range.second) == 0) {
return (OptionDescriptor(false));
}
return (*range.first);
}
void Subnet::delVendorOptions() {
vendor_option_spaces_.clearItems();
}
isc::asiolink::IOAddress Subnet::getLastAllocated(Lease::Type type) const {
// check if the type is valid (and throw if it isn't)
checkType(type);
switch (type) {
case Lease::TYPE_V4:
case Lease::TYPE_NA:
return last_allocated_ia_;
case Lease::TYPE_TA:
return last_allocated_ta_;
case Lease::TYPE_PD:
return last_allocated_pd_;
default:
isc_throw(BadValue, "Pool type " << type << " not supported");
}
}
void Subnet::setLastAllocated(Lease::Type type,
const isc::asiolink::IOAddress& addr) {
// check if the type is valid (and throw if it isn't)
checkType(type);
switch (type) {
case Lease::TYPE_V4:
case Lease::TYPE_NA:
last_allocated_ia_ = addr;
return;
case Lease::TYPE_TA:
last_allocated_ta_ = addr;
return;
case Lease::TYPE_PD:
last_allocated_pd_ = addr;
return;
default:
isc_throw(BadValue, "Pool type " << type << " not supported");
}
}
std::string
Subnet::toText() const {
std::stringstream tmp;
tmp << prefix_ << "/" << static_cast<unsigned int>(prefix_len_);
return (tmp.str());
}
void Subnet4::checkType(Lease::Type type) const {
if (type != Lease::TYPE_V4) {
isc_throw(BadValue, "Only TYPE_V4 is allowed for Subnet4");
}
}
Subnet4::Subnet4(const isc::asiolink::IOAddress& prefix, uint8_t length,
const Triplet<uint32_t>& t1,
const Triplet<uint32_t>& t2,
const Triplet<uint32_t>& valid_lifetime,
const SubnetID id)
:Subnet(prefix, length, t1, t2, valid_lifetime,
RelayInfo(IOAddress("0.0.0.0")), id), siaddr_(IOAddress("0.0.0.0")) {
if (!prefix.isV4()) {
isc_throw(BadValue, "Non IPv4 prefix " << prefix.toText()
<< " specified in subnet4");
}
}
void Subnet4::setSiaddr(const isc::asiolink::IOAddress& siaddr) {
if (!siaddr.isV4()) {
isc_throw(BadValue, "Can't set siaddr to non-IPv4 address "
<< siaddr);
}
siaddr_ = siaddr;
}
isc::asiolink::IOAddress Subnet4::getSiaddr() const {
return (siaddr_);
}
const PoolCollection& Subnet::getPools(Lease::Type type) const {
// check if the type is valid (and throw if it isn't)
checkType(type);
switch (type) {
case Lease::TYPE_V4:
case Lease::TYPE_NA:
return (pools_);
case Lease::TYPE_TA:
return (pools_ta_);
case Lease::TYPE_PD:
return (pools_pd_);
default:
isc_throw(BadValue, "Unsupported pool type: "
<< static_cast<int>(type));
}
}
PoolCollection& Subnet::getPoolsWritable(Lease::Type type) {
// check if the type is valid (and throw if it isn't)
checkType(type);
switch (type) {
case Lease::TYPE_V4:
case Lease::TYPE_NA:
return (pools_);
case Lease::TYPE_TA:
return (pools_ta_);
case Lease::TYPE_PD:
return (pools_pd_);
default:
isc_throw(BadValue, "Invalid pool type specified: "
<< static_cast<int>(type));
}
}
const PoolPtr Subnet::getPool(Lease::Type type, const isc::asiolink::IOAddress& hint,
bool anypool /* true */) const {
// check if the type is valid (and throw if it isn't)
checkType(type);
const PoolCollection& pools = getPools(type);
PoolPtr candidate;
for (PoolCollection::const_iterator pool = pools.begin();
pool != pools.end(); ++pool) {
// if we won't find anything better, then let's just use the first pool
if (anypool && !candidate) {
candidate = *pool;
}
// if the client provided a pool and there's a pool that hint is valid
// in, then let's use that pool
if ((*pool)->inRange(hint)) {
return (*pool);
}
}
return (candidate);
}
void
Subnet::addPool(const PoolPtr& pool) {
IOAddress first_addr = pool->getFirstAddress();
IOAddress last_addr = pool->getLastAddress();
if (!inRange(first_addr) || !inRange(last_addr)) {
isc_throw(BadValue, "Pool (" << first_addr << "-" << last_addr
<< " does not belong in this (" << prefix_ << "/"
<< static_cast<int>(prefix_len_) << ") subnet");
}
/// @todo: Check that pools do not overlap
// check if the type is valid (and throw if it isn't)
checkType(pool->getType());
// Add the pool to the appropriate pools collection
getPoolsWritable(pool->getType()).push_back(pool);
}
void
Subnet::delPools(Lease::Type type) {
getPoolsWritable(type).clear();
}
void
Subnet::setIface(const std::string& iface_name) {
iface_ = iface_name;
}
std::string
Subnet::getIface() const {
return (iface_);
}
void
Subnet4::validateOption(const OptionPtr& option) const {
if (!option) {
isc_throw(isc::BadValue,
"option configured for subnet must not be NULL");
} else if (option->getUniverse() != Option::V4) {
isc_throw(isc::BadValue,
"expected V4 option to be added to the subnet");
}
}
bool
Subnet::inPool(Lease::Type type, const isc::asiolink::IOAddress& addr) const {
// Let's start with checking if it even belongs to that subnet.
if (!inRange(addr)) {
return (false);
}
const PoolCollection& pools = getPools(type);
for (PoolCollection::const_iterator pool = pools.begin();
pool != pools.end(); ++pool) {
if ((*pool)->inRange(addr)) {
return (true);
}
}
// There's no pool that address belongs to
return (false);
}
Subnet6::Subnet6(const isc::asiolink::IOAddress& prefix, uint8_t length,
const Triplet<uint32_t>& t1,
const Triplet<uint32_t>& t2,
const Triplet<uint32_t>& preferred_lifetime,
const Triplet<uint32_t>& valid_lifetime,
const SubnetID id)
:Subnet(prefix, length, t1, t2, valid_lifetime, RelayInfo(IOAddress("::")), id),
preferred_(preferred_lifetime) {
if (!prefix.isV6()) {
isc_throw(BadValue, "Non IPv6 prefix " << prefix
<< " specified in subnet6");
}
}
void Subnet6::checkType(Lease::Type type) const {
if ( (type != Lease::TYPE_NA) && (type != Lease::TYPE_TA) &&
(type != Lease::TYPE_PD)) {
isc_throw(BadValue, "Invalid Pool type: " << Lease::typeToText(type)
<< "(" << static_cast<int>(type)
<< "), must be TYPE_NA, TYPE_TA or TYPE_PD for Subnet6");
}
}
void
Subnet6::validateOption(const OptionPtr& option) const {
if (!option) {
isc_throw(isc::BadValue,
"option configured for subnet must not be NULL");
} else if (option->getUniverse() != Option::V6) {
isc_throw(isc::BadValue,
"expected V6 option to be added to the subnet");
}
}
} // end of isc::dhcp namespace
} // end of isc namespace
|