blob: a13d886585c0e2caa65050a9b7a71d6dfcee7683 [file] [log] [blame]
Alexander Afanasyeve4d745d2018-04-08 17:55:56 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
Davide Pesaventoe422f9e2022-06-03 01:30:23 -04003 * Copyright (c) 2014-2022, Regents of the University of California,
Alexander Afanasyeve4d745d2018-04-08 17:55:56 -04004 * Arizona Board of Regents,
5 * Colorado State University,
6 * University Pierre & Marie Curie, Sorbonne University,
7 * Washington University in St. Louis,
8 * Beijing Institute of Technology,
9 * The University of Memphis.
10 *
11 * This file is part of NFD (Named Data Networking Forwarding Daemon).
12 * See AUTHORS.md for complete list of NFD authors and contributors.
13 *
14 * NFD is free software: you can redistribute it and/or modify it under the terms
15 * of the GNU General Public License as published by the Free Software Foundation,
16 * either version 3 of the License, or (at your option) any later version.
17 *
18 * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
19 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
20 * PURPOSE. See the GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License along with
23 * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
24 */
25
26#include "network-predicate.hpp"
Davide Pesavento2cae8ca2019-04-18 20:48:05 -040027#include "common/config-file.hpp"
28#include "core/network.hpp"
Alexander Afanasyeve4d745d2018-04-08 17:55:56 -040029
30#include <fnmatch.h>
31
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040032namespace nfd::face {
Alexander Afanasyeve4d745d2018-04-08 17:55:56 -040033
34NetworkPredicateBase::NetworkPredicateBase()
35{
36 this->clear();
37}
38
39NetworkPredicateBase::~NetworkPredicateBase() = default;
40
41void
42NetworkPredicateBase::clear()
43{
44 m_whitelist = std::set<std::string>{"*"};
45 m_blacklist.clear();
46}
47
48void
Davide Pesavento19779d82019-02-14 13:40:04 -050049NetworkPredicateBase::parseList(std::set<std::string>& set,
50 const boost::property_tree::ptree& list,
51 const std::string& section)
Alexander Afanasyeve4d745d2018-04-08 17:55:56 -040052{
53 set.clear();
54
55 for (const auto& item : list) {
56 if (item.first == "*") {
57 // insert wildcard
58 set.insert(item.first);
59 }
60 else {
61 if (!isRuleSupported(item.first)) {
Davide Pesavento19779d82019-02-14 13:40:04 -050062 NDN_THROW(ConfigFile::Error("Unrecognized rule '" + item.first +
63 "' in section '" + section + "'"));
Alexander Afanasyeve4d745d2018-04-08 17:55:56 -040064 }
65
66 auto value = item.second.get_value<std::string>();
67 if (!isRuleValid(item.first, value)) {
Davide Pesavento19779d82019-02-14 13:40:04 -050068 NDN_THROW(ConfigFile::Error("Malformed " + item.first + " '" + value +
69 "' in section '" + section + "'"));
Alexander Afanasyeve4d745d2018-04-08 17:55:56 -040070 }
71 set.insert(value);
72 }
73 }
74}
75
76void
Davide Pesavento19779d82019-02-14 13:40:04 -050077NetworkPredicateBase::parseList(std::set<std::string>& set,
78 std::initializer_list<std::pair<std::string, std::string>> list)
Alexander Afanasyeve4d745d2018-04-08 17:55:56 -040079{
80 set.clear();
81
82 for (const auto& item : list) {
83 if (item.first == "*") {
84 // insert wildcard
85 set.insert(item.first);
86 }
87 else {
88 if (!isRuleSupported(item.first)) {
Davide Pesavento19779d82019-02-14 13:40:04 -050089 NDN_THROW(std::runtime_error("Unrecognized rule '" + item.first + "'"));
Alexander Afanasyeve4d745d2018-04-08 17:55:56 -040090 }
91
92 if (!isRuleValid(item.first, item.second)) {
Davide Pesavento19779d82019-02-14 13:40:04 -050093 NDN_THROW(std::runtime_error("Malformed " + item.first + " '" + item.second + "'"));
Alexander Afanasyeve4d745d2018-04-08 17:55:56 -040094 }
95 set.insert(item.second);
96 }
97 }
98}
99
100void
101NetworkPredicateBase::parseWhitelist(const boost::property_tree::ptree& list)
102{
103 parseList(m_whitelist, list, "whitelist");
104}
105
106void
107NetworkPredicateBase::parseBlacklist(const boost::property_tree::ptree& list)
108{
109 parseList(m_blacklist, list, "blacklist");
110}
111
112void
113NetworkPredicateBase::assign(std::initializer_list<std::pair<std::string, std::string>> whitelist,
114 std::initializer_list<std::pair<std::string, std::string>> blacklist)
115{
116 parseList(m_whitelist, whitelist);
117 parseList(m_blacklist, blacklist);
118}
119
120bool
121NetworkInterfacePredicate::isRuleSupported(const std::string& key)
122{
123 return key == "ifname" || key == "ether" || key == "subnet";
124}
125
126bool
127NetworkInterfacePredicate::isRuleValid(const std::string& key, const std::string& value)
128{
129 if (key == "ifname") {
130 // very basic sanity check for interface names
131 return !value.empty();
132 }
133 else if (key == "ether") {
134 // validate ethernet address
135 return !ndn::ethernet::Address::fromString(value).isNull();
136 }
137 else if (key == "subnet") {
138 // example subnet: 10.0.0.0/8
139 return Network::isValidCidr(value);
140 }
141 else {
Davide Pesavento19779d82019-02-14 13:40:04 -0500142 NDN_THROW(std::logic_error("Only supported rules are expected"));
Alexander Afanasyeve4d745d2018-04-08 17:55:56 -0400143 }
144}
145
146bool
147IpAddressPredicate::isRuleSupported(const std::string& key)
148{
149 return key == "subnet";
150}
151
152bool
153IpAddressPredicate::isRuleValid(const std::string& key, const std::string& value)
154{
155 if (key == "subnet") {
156 // example subnet: 10.0.0.0/8
157 return Network::isValidCidr(value);
158 }
159 else {
Davide Pesavento19779d82019-02-14 13:40:04 -0500160 NDN_THROW(std::logic_error("Only supported rules are expected"));
Alexander Afanasyeve4d745d2018-04-08 17:55:56 -0400161 }
162}
163
164bool
165NetworkPredicateBase::operator==(const NetworkPredicateBase& other) const
166{
167 return this->m_whitelist == other.m_whitelist &&
168 this->m_blacklist == other.m_blacklist;
169}
170
171static bool
172doesMatchPattern(const std::string& ifname, const std::string& pattern)
173{
174 // use fnmatch(3) to provide unix glob-style matching for interface names
175 // fnmatch returns 0 if there is a match
176 return ::fnmatch(pattern.data(), ifname.data(), 0) == 0;
177}
178
179static bool
180doesNetifMatchRule(const ndn::net::NetworkInterface& netif, const std::string& rule)
181{
182 // if '/' is in rule, this is a subnet, check if IP in subnet
183 if (rule.find('/') != std::string::npos) {
184 Network n = boost::lexical_cast<Network>(rule);
185 for (const auto& addr : netif.getNetworkAddresses()) {
186 if (n.doesContain(addr.getIp())) {
187 return true;
188 }
189 }
190 }
191
192 return rule == "*" ||
193 doesMatchPattern(netif.getName(), rule) ||
194 netif.getEthernetAddress().toString() == rule;
195}
196
197bool
198NetworkInterfacePredicate::operator()(const ndn::net::NetworkInterface& netif) const
199{
Davide Pesavento412c9822021-07-02 00:21:05 -0400200 return std::any_of(m_whitelist.begin(), m_whitelist.end(),
201 [&netif] (const auto& rule) { return doesNetifMatchRule(netif, rule); }) &&
202 std::none_of(m_blacklist.begin(), m_blacklist.end(),
203 [&netif] (const auto& rule) { return doesNetifMatchRule(netif, rule); });
Alexander Afanasyeve4d745d2018-04-08 17:55:56 -0400204}
205
206static bool
207doesAddressMatchRule(const boost::asio::ip::address& address, const std::string& rule)
208{
209 // if '/' is in rule, this is a subnet, check if IP in subnet
210 if (rule.find('/') != std::string::npos) {
211 Network n = boost::lexical_cast<Network>(rule);
212 if (n.doesContain(address)) {
213 return true;
214 }
215 }
216
217 return rule == "*";
218}
219
220bool
221IpAddressPredicate::operator()(const boost::asio::ip::address& address) const
222{
Davide Pesavento412c9822021-07-02 00:21:05 -0400223 return std::any_of(m_whitelist.begin(), m_whitelist.end(),
224 [&address] (const auto& rule) { return doesAddressMatchRule(address, rule); }) &&
225 std::none_of(m_blacklist.begin(), m_blacklist.end(),
226 [&address] (const auto& rule) { return doesAddressMatchRule(address, rule); });
Alexander Afanasyeve4d745d2018-04-08 17:55:56 -0400227}
228
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400229} // namespace nfd::face