cfg_iface.cc 10.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
// Copyright (C) 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 <dhcp/iface_mgr.h>
16
#include <dhcpsrv/dhcpsrv_log.h>
17
#include <dhcpsrv/cfg_iface.h>
18
#include <util/strutil.h>
19
#include <boost/bind.hpp>
20

21 22
using namespace isc::asiolink;

23 24 25
namespace isc {
namespace dhcp {

26
const char* CfgIface::ALL_IFACES_KEYWORD = "*";
27

28 29
CfgIface::CfgIface()
    : wildcard_used_(false) {
30 31 32
}

void
33
CfgIface::closeSockets() const {
34 35 36
    IfaceMgr::instance().closeSockets();
}

37 38 39 40 41 42 43
bool
CfgIface::equals(const CfgIface& other) const {
    return (iface_set_ == other.iface_set_ &&
            unicast_map_ == other.unicast_map_ &&
            wildcard_used_ == other.wildcard_used_);
}

44
void
45
CfgIface::openSockets(const uint16_t family, const uint16_t port,
46
                      const bool use_bcast) const {
47 48 49
    // If wildcard interface '*' was not specified, set all interfaces to
    // inactive state. We will later enable them selectively using the
    // interface names specified by the user. If wildcard interface was
50 51
    // specified, mark all interfaces active. In all cases, mark loopback
    // inactive.
52
    setState(family, !wildcard_used_, true);
53 54
    // Remove selection of unicast addresses from all interfaces.
    IfaceMgr::instance().clearUnicasts();
55 56
    // If there is no wildcard interface specified, we will have to iterate
    // over the names specified by the caller and enable them.
57 58 59 60
    if (!wildcard_used_) {
        for (IfaceSet::const_iterator iface_name = iface_set_.begin();
             iface_name != iface_set_.end(); ++iface_name) {
            Iface* iface = IfaceMgr::instance().getIface(*iface_name);
61 62 63 64
            // This shouldn't really happen because we are checking the
            // names of interfaces when they are being added (use()
            // function). But, if someone has triggered detection of
            // interfaces since then, some interfaces may have disappeared.
65 66 67 68 69 70
            if (iface == NULL) {
                isc_throw(Unexpected,
                          "fail to open socket on interface '"
                          << *iface_name << "' as this interface doesn't"
                          " exist");

71
            } else if (family == AF_INET) {
72 73 74 75 76 77
                iface->inactive4_ = false;

            } else {
                iface->inactive6_ = false;
            }
        }
78
    }
79

80
    // Select unicast sockets. It works only for V6. Ignore for V4.
81
    if (family == AF_INET6) {
82 83 84 85 86 87 88 89 90 91
        for (UnicastMap::const_iterator unicast = unicast_map_.begin();
             unicast != unicast_map_.end(); ++unicast) {
            Iface* iface = IfaceMgr::instance().getIface(unicast->first);
            if (iface == NULL) {
                isc_throw(Unexpected,
                          "fail to open unicast socket on interface '"
                          << unicast->first << "' as this interface doesn't"
                          " exist");
            }
            iface->addUnicast(unicast->second);
92
            iface->inactive6_ = false;
93 94 95
        }
    }

96 97 98
    // Before opening any sockets, close existing ones.
    closeSockets();

99 100 101 102
    // Set the callback which is called when the socket fails to open
    // for some specific interface. This callback will simply log a
    // warning message.
    IfaceMgrErrorMsgCallback error_callback =
103
        boost::bind(&CfgIface::socketOpenErrorHandler, _1);
104
    bool sopen;
105
    if (family == AF_INET) {
106 107
        sopen = IfaceMgr::instance().openSockets4(port, use_bcast,
                                                  error_callback);
108
    } else {
109 110
        // use_bcast is ignored for V6.
        sopen = IfaceMgr::instance().openSockets6(port, error_callback);
111 112
    }

113 114 115 116 117 118
    // If no socket were opened, log a warning because the server will
    // not respond to any queries.
    if (!sopen) {
        LOG_WARN(dhcpsrv_logger, DHCPSRV_NO_SOCKETS_OPEN);
    }
}
119

120
void
121
CfgIface::reset() {
122 123
    wildcard_used_ = false;
    iface_set_.clear();
124 125 126
}

void
127
CfgIface::setState(const uint16_t family, const bool inactive,
128
                   const bool loopback_inactive) const {
129 130
    IfaceMgr::IfaceCollection ifaces = IfaceMgr::instance().getIfaces();
    for (IfaceMgr::IfaceCollection::iterator iface = ifaces.begin();
131
         iface != ifaces.end(); ++iface) {
132
        Iface* iface_ptr = IfaceMgr::instance().getIface(iface->getName());
133
        if (family == AF_INET) {
134 135
            iface_ptr->inactive4_ = iface_ptr->flag_loopback_ ?
                loopback_inactive : inactive;
136
        } else {
137 138
            iface_ptr->inactive6_ = iface_ptr->flag_loopback_ ?
                loopback_inactive : inactive;
139 140 141 142
        }
    }
}

143
void
144
CfgIface::socketOpenErrorHandler(const std::string& errmsg) {
145 146 147
    LOG_WARN(dhcpsrv_logger, DHCPSRV_OPEN_SOCKET_FAIL).arg(errmsg);
}

148
void
149
CfgIface::use(const uint16_t family, const std::string& iface_name) {
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
    // The interface name specified may have two formats, e.g.:
    // - eth0
    // - eth0/2001:db8:1::1.
    // The latter format is used to open unicast socket on the specified
    // interface. Here we are detecting which format was used and we strip
    // all extraneous spaces.
    size_t pos = iface_name.find("/");
    std::string name;
    std::string addr_str;
    // There is no unicast address so the whole string is an interface name.
    if (pos == std::string::npos) {
        name = util::str::trim(iface_name);
        if (name.empty()) {
            isc_throw(InvalidIfaceName,
                      "empty interface name used in configuration");

        } if (name != ALL_IFACES_KEYWORD) {
            if (IfaceMgr::instance().getIface(name) == NULL) {
                isc_throw(NoSuchIface, "interface '" << name
                          << "' doesn't exist in the system");
            }

172 173
            // If interface has already been specified.
            if (iface_set_.find(name) != iface_set_.end()) {
174 175
                isc_throw(DuplicateIfaceName, "interface '" << name
                          << "' has already been specified");
176

177 178
            }

179
            // All ok, add interface.
180
            LOG_INFO(dhcpsrv_logger, DHCPSRV_CFGMGR_ADD_IFACE)
181 182 183
                .arg(name);
            iface_set_.insert(name);

184 185 186 187 188
        } else if (wildcard_used_) {
            isc_throw(DuplicateIfaceName, "the wildcard interface '"
                      << ALL_IFACES_KEYWORD << "' can only be specified once");

        } else {
189 190
            LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE,
                      DHCPSRV_CFGMGR_ALL_IFACES_ACTIVE);
191 192 193 194
            wildcard_used_ = true;

        }

195
    } else if (family == AF_INET) {
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
        isc_throw(InvalidIfaceName, "unicast addresses in the format of: "
                  "iface-name/unicast-addr_stress can only be specified for"
                  " IPv6 addr_stress family");

    } else {
        // The interface name includes the unicast addr_stress, so we split
        // interface name and the unicast addr_stress to two variables.
        name = util::str::trim(iface_name.substr(0, pos));
        addr_str = util::str::trim(iface_name.substr(pos + 1));

        // Interface name must not be empty.
        if (name.empty()) {
            isc_throw(InvalidIfaceName,
                      "empty interface name specified in the"
                      " interface configuration");

        }
        // Unicast addr_stress following the interface name must not be empty.
        if (addr_str.empty()) {
            isc_throw(InvalidIfaceName,
                      "empty unicast addr_stress specified in the interface"
                      << " configuration");

        }

        // Interface name must not be the wildcard name.
        if (name == ALL_IFACES_KEYWORD) {
            isc_throw(InvalidIfaceName,
                      "wildcard interface name '" << ALL_IFACES_KEYWORD
                      << "' must not be used in conjunction with a"
                      " unicast addr_stress");

        }

        // Interface must exist.
        Iface* iface = IfaceMgr::instance().getIface(name);
        if (iface == NULL) {
233 234
            isc_throw(NoSuchIface, "interface '" << name
                      << "' doesn't exist in the system");
235

236 237
        }

238 239 240 241 242 243 244 245
        // Convert address string. This may throw an exception if the address
        // is invalid.
        IOAddress addr(addr_str);

        // Check that the address is a valid unicast address.
        if (!addr.isV6() || addr.isV6LinkLocal() || addr.isV6Multicast()) {
            isc_throw(InvalidIfaceName, "address '" << addr << "' is not"
                      " a valid IPv6 unicast address");
246 247
        }

248 249 250 251 252 253 254
        // There are valid cases where link local address can be specified to
        // receive unicast traffic, e.g. sent by relay agent.
        if (addr.isV6LinkLocal()) {
            LOG_WARN(dhcpsrv_logger, DHCPSRV_CFGMGR_UNICAST_LINK_LOCAL)
                .arg(addr.toText()).arg(name);
        }

255 256 257 258 259 260
        // Interface must have this address assigned.
        if (!iface->hasAddress(addr)) {
            isc_throw(NoSuchAddress,
                      "interface '" << name << "' doesn't have address '"
                      << addr << "' assigned");
        }
261

262 263
        // Insert address and the interface to the collection of unicast
        // addresses.
264
        if (unicast_map_.find(name) != unicast_map_.end()) {
265 266 267 268 269
            isc_throw(DuplicateIfaceName, "must not specify unicast address '"
                      << addr << "' for interface '" << name << "' "
                      "because other unicast address has already been"
                      " specified for this interface");
        }
270
        LOG_INFO(dhcpsrv_logger, DHCPSRV_CFGMGR_ADD_UNICAST)
271 272
            .arg(addr.toText()).arg(name);
        unicast_map_.insert(std::pair<std::string, IOAddress>(name, addr));
273
    }
274

275 276 277 278
}

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