cfg_subnets4.cc 6.74 KB
Newer Older
1
// Copyright (C) 2014, 2015 Internet Systems Consortium, Inc. ("ISC")
2
3
4
5
6
7
8
9
10
11
12
13
14
//
// 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.

15
#include <config.h>
16
17
18
#include <dhcp/iface_mgr.h>
#include <dhcpsrv/cfg_subnets4.h>
#include <dhcpsrv/dhcpsrv_log.h>
19
#include <dhcpsrv/subnet_id.h>
Tomek Mrugalski's avatar
Tomek Mrugalski committed
20
#include <stats/stats_mgr.h>
21
22
23
24
25

using namespace isc::asiolink;

namespace {

26
27
/// @brief Holds IPv4 address set to "0.0.0.0".
const IOAddress ZERO_ADDRESS("0.0.0.0");
28

29
30
/// @brief Holds IPv4 broadcast address.
const IOAddress BCAST_ADDRESS("255.255.255.255");
31

32
33
34
35
36
37
38
39
40
41
} // end of anonymous namespace

namespace isc {
namespace dhcp {

void
CfgSubnets4::add(const Subnet4Ptr& subnet) {
    /// @todo: Check that this new subnet does not cross boundaries of any
    /// other already defined subnet.
    if (isDuplicate(*subnet)) {
42
        isc_throw(isc::dhcp::DuplicateSubnetID, "ID of the new IPv4 subnet '"
43
44
45
46
47
48
49
50
                  << subnet->getID() << "' is already in use");
    }
    LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE, DHCPSRV_CFGMGR_ADD_SUBNET4)
              .arg(subnet->toText());
    subnets_.push_back(subnet);
}

Subnet4Ptr
51
CfgSubnets4::selectSubnet(const SubnetSelector& selector) const {
52
53
54
55
56
    // If relayed message has been received, try to match the giaddr with the
    // relay address specified for a subnet. It is also possible that the relay
    // address will not match with any of the relay addresses accross all
    // subnets, but we need to verify that for all subnets before we can try
    // to use the giaddr to match with the subnet prefix.
57
    if (selector.giaddr_ != ZERO_ADDRESS) {
58
59
        for (Subnet4Collection::const_iterator subnet = subnets_.begin();
             subnet != subnets_.end(); ++subnet) {
60
61
62

            // Check if the giaddr is equal to the one defined for the subnet.
            if (selector.giaddr_ != (*subnet)->getRelayInfo().addr_) {
63
64
65
                continue;
            }

66
67
            // Eliminate those subnets that do not meet client class criteria.
            if ((*subnet)->clientSupported(selector.client_classes_)) {
68
69
70
71
72
73
74
75
76
77
                return (*subnet);
            }
        }
    }

    // If we got to this point it means that we were not able to match the
    // giaddr with any of the addresses specified for subnets. Let's determine
    // what address from the client's packet to use to match with the
    // subnets' prefixes.

78
    IOAddress address = ZERO_ADDRESS;
79
    // If there is a giaddr, use it for subnet selection.
80
    if (selector.giaddr_ != ZERO_ADDRESS) {
81
82
83
        address = selector.giaddr_;

    // If it is a Renew or Rebind, use the ciaddr.
84
85
    } else if ((selector.ciaddr_ != ZERO_ADDRESS) &&
               (selector.local_address_ != BCAST_ADDRESS)) {
86
87
88
        address = selector.ciaddr_;

    // If ciaddr is not specified, use the source address.
89
90
    } else if ((selector.remote_address_ != ZERO_ADDRESS) &&
               (selector.local_address_ != BCAST_ADDRESS)) {
91
92
93
94
        address = selector.remote_address_;

    // If local interface name is known, use the local address on this
    // interface.
95
    } else if (!selector.iface_name_.empty()) {
96
        IfacePtr iface = IfaceMgr::instance().getIface(selector.iface_name_);
97
98
99
        // This should never happen in the real life. Hence we throw an
        // exception.
        if (iface == NULL) {
100
            isc_throw(isc::BadValue, "interface " << selector.iface_name_
101
102
103
104
105
106
107
                      << " doesn't exist and therefore it is impossible"
                      " to find a suitable subnet for its IPv4 address");
        }
        iface->getAddress4(address);
    }

    // Unable to find a suitable address to use for subnet selection.
108
    if (address == ZERO_ADDRESS) {
109
110
111
112
        return (Subnet4Ptr());
    }

    // We have identified an address in the client's packet that can be
Tomek Mrugalski's avatar
Tomek Mrugalski committed
113
    // used for subnet selection. Match this packet with the subnets.
114
    return (selectSubnet(address, selector.client_classes_));
115
116
117
}

Subnet4Ptr
118
CfgSubnets4::selectSubnet(const IOAddress& address,
119
                 const ClientClasses& client_classes) const {
120
121
    for (Subnet4Collection::const_iterator subnet = subnets_.begin();
         subnet != subnets_.end(); ++subnet) {
122

123
124
        // Address is in range for the subnet prefix, so return it.
        if (!(*subnet)->inRange(address)) {
125
126
127
            continue;
        }

128
129
        // Eliminate those subnets that do not meet client class criteria.
        if ((*subnet)->clientSupported(client_classes)) {
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
            return (*subnet);
        }
    }

    // Failed to find a subnet.
    return (Subnet4Ptr());
}

bool
CfgSubnets4::isDuplicate(const Subnet4& subnet) const {
    for (Subnet4Collection::const_iterator subnet_it = subnets_.begin();
         subnet_it != subnets_.end(); ++subnet_it) {
        if ((*subnet_it)->getID() == subnet.getID()) {
            return (true);
        }
    }
    return (false);
}

Tomek Mrugalski's avatar
Tomek Mrugalski committed
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
void
CfgSubnets4::removeStatistics() {
    using namespace isc::stats;

    // For each v4 subnet currently configured, remove the statistic.
    /// @todo: May move this to CfgSubnets4 class if there will be more
    /// statistics here.
    for (Subnet4Collection::const_iterator subnet4 = subnets_.begin();
         subnet4 != subnets_.end(); ++subnet4) {

        StatsMgr::instance().del(StatsMgr::generateName("subnet",
                                                        (*subnet4)->getID(),
                                                        "total-addresses"));

        StatsMgr::instance().del(StatsMgr::generateName("subnet",
                                                        (*subnet4)->getID(),
                                                        "assigned-addresses"));
    }
}
168

Tomek Mrugalski's avatar
Tomek Mrugalski committed
169
170
171
void
CfgSubnets4::updateStatistics() {
    using namespace isc::stats;
172

Tomek Mrugalski's avatar
Tomek Mrugalski committed
173
174
175
176
177
178
179
180
181
182
    /// @todo: May move this to CfgSubnets4 class if there will be more
    /// statistics here.
    for (Subnet4Collection::const_iterator subnet = subnets_.begin();
         subnet != subnets_.end(); ++subnet) {

        StatsMgr::instance().setValue(
            StatsMgr::generateName("subnet", (*subnet)->getID(), "total-addresses"),
            static_cast<int64_t>((*subnet)->getPoolCapacity(Lease::TYPE_V4)));
    }
}
183
184
185

} // end of namespace isc::dhcp
} // end of namespace isc