cfg_subnets4.cc 6.22 KB
Newer Older
1
// Copyright (C) 2014, 2015 Internet Systems Consortium, Inc. ("ISC")
2
//
3
4
5
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
6

7
#include <config.h>
8
9
10
#include <dhcp/iface_mgr.h>
#include <dhcpsrv/cfg_subnets4.h>
#include <dhcpsrv/dhcpsrv_log.h>
11
#include <dhcpsrv/subnet_id.h>
Tomek Mrugalski's avatar
Tomek Mrugalski committed
12
#include <stats/stats_mgr.h>
13
14
15
16
17
18
19
20
21
22
23

using namespace isc::asiolink;

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)) {
24
        isc_throw(isc::dhcp::DuplicateSubnetID, "ID of the new IPv4 subnet '"
25
26
27
28
29
30
31
32
                  << 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
33
CfgSubnets4::selectSubnet(const SubnetSelector& selector) const {
34
35
36
37
38
39
    // First use RAI link select sub-option or subnet select option
    if (!selector.option_select_.isV4Zero()) {
        return (selectSubnet(selector.option_select_,
                             selector.client_classes_));
    }

40
41
42
43
44
    // 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.
Francis Dupont's avatar
Francis Dupont committed
45
    if (!selector.giaddr_.isV4Zero()) {
46
47
        for (Subnet4Collection::const_iterator subnet = subnets_.begin();
             subnet != subnets_.end(); ++subnet) {
48
49
50

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

54
55
            // Eliminate those subnets that do not meet client class criteria.
            if ((*subnet)->clientSupported(selector.client_classes_)) {
56
57
58
59
60
61
62
63
64
65
                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.

Francis Dupont's avatar
Francis Dupont committed
66
    IOAddress address = IOAddress::IPV4_ZERO_ADDRESS();
67
    // If there is a giaddr, use it for subnet selection.
Francis Dupont's avatar
Francis Dupont committed
68
    if (!selector.giaddr_.isV4Zero()) {
69
70
71
        address = selector.giaddr_;

    // If it is a Renew or Rebind, use the ciaddr.
Francis Dupont's avatar
Francis Dupont committed
72
73
    } else if (!selector.ciaddr_.isV4Zero() &&
               !selector.local_address_.isV4Bcast()) {
74
75
76
        address = selector.ciaddr_;

    // If ciaddr is not specified, use the source address.
Francis Dupont's avatar
Francis Dupont committed
77
78
    } else if (!selector.remote_address_.isV4Zero() &&
               !selector.local_address_.isV4Bcast()) {
79
80
81
82
        address = selector.remote_address_;

    // If local interface name is known, use the local address on this
    // interface.
83
    } else if (!selector.iface_name_.empty()) {
84
        IfacePtr iface = IfaceMgr::instance().getIface(selector.iface_name_);
85
86
87
        // This should never happen in the real life. Hence we throw an
        // exception.
        if (iface == NULL) {
88
            isc_throw(isc::BadValue, "interface " << selector.iface_name_
89
90
91
92
93
94
95
                      << " 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.
Francis Dupont's avatar
Francis Dupont committed
96
    if (address.isV4Zero()) {
97
98
99
100
        return (Subnet4Ptr());
    }

    // We have identified an address in the client's packet that can be
Tomek Mrugalski's avatar
Tomek Mrugalski committed
101
    // used for subnet selection. Match this packet with the subnets.
102
    return (selectSubnet(address, selector.client_classes_));
103
104
105
}

Subnet4Ptr
106
CfgSubnets4::selectSubnet(const IOAddress& address,
107
                 const ClientClasses& client_classes) const {
108
109
    for (Subnet4Collection::const_iterator subnet = subnets_.begin();
         subnet != subnets_.end(); ++subnet) {
110

111
112
        // Address is in range for the subnet prefix, so return it.
        if (!(*subnet)->inRange(address)) {
113
114
115
            continue;
        }

116
117
        // Eliminate those subnets that do not meet client class criteria.
        if ((*subnet)->clientSupported(client_classes)) {
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
            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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
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"));
    }
}
156

Tomek Mrugalski's avatar
Tomek Mrugalski committed
157
158
159
void
CfgSubnets4::updateStatistics() {
    using namespace isc::stats;
160

Tomek Mrugalski's avatar
Tomek Mrugalski committed
161
162
163
164
165
166
167
168
169
170
    /// @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)));
    }
}
171
172
173

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