2 * This file is part of PowerDNS or dnsdist.
3 * Copyright -- PowerDNS.COM B.V. and its contributors
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of version 2 of the GNU General Public License as
7 * published by the Free Software Foundation.
9 * In addition, for the avoidance of any doubt, permission is granted to
10 * link this program with OpenSSL and to (re)distribute the binaries
11 * produced as the result of such linking.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 #ifndef DISABLE_DYNBLOCKS
25 #include <unordered_set>
28 #include "dnsdist-rings.hh"
29 #include "statnode.hh"
32 #include "dnsdist-lua-inspection-ffi.h"
35 // dnsdist_ffi_stat_node_t is a lightuserdata
37 struct LuaContext::Pusher<dnsdist_ffi_stat_node_t*> {
38 static const int minSize = 1;
39 static const int maxSize = 1;
41 static PushedObject push(lua_State* state, dnsdist_ffi_stat_node_t* ptr) noexcept {
42 lua_pushlightuserdata(state, ptr);
43 return PushedObject{state, 1};
47 typedef std::function<bool(dnsdist_ffi_stat_node_t*)> dnsdist_ffi_stat_node_visitor_t;
49 struct dnsdist_ffi_stat_node_t
51 dnsdist_ffi_stat_node_t(const StatNode& node_, const StatNode::Stat& self_, const StatNode::Stat& children_, std::optional<std::string>& reason_): node(node_), self(self_), children(children_), reason(reason_)
56 const StatNode::Stat& self;
57 const StatNode::Stat& children;
58 std::optional<std::string>& reason;
61 class DynBlockRulesGroup
67 std::map<uint8_t, uint64_t> d_rcodeCounts;
68 std::map<uint16_t, uint64_t> d_qtypeCounts;
70 uint64_t responses{0};
71 uint64_t respBytes{0};
76 DynBlockRule(): d_enabled(false)
80 DynBlockRule(const std::string& blockReason, unsigned int blockDuration, unsigned int rate, unsigned int warningRate, unsigned int seconds, DNSAction::Action action): d_blockReason(blockReason), d_blockDuration(blockDuration), d_rate(rate), d_warningRate(warningRate), d_seconds(seconds), d_action(action), d_enabled(true)
84 bool matches(const struct timespec& when)
90 if (d_seconds && when < d_cutOff) {
94 if (when < d_minTime) {
101 bool rateExceeded(unsigned int count, const struct timespec& now) const
107 double delta = d_seconds ? d_seconds : DiffTime(now, d_minTime);
108 double limit = delta * d_rate;
109 return (count > limit);
112 bool warningRateExceeded(unsigned int count, const struct timespec& now) const
118 if (d_warningRate == 0) {
122 double delta = d_seconds ? d_seconds : DiffTime(now, d_minTime);
123 double limit = delta * d_warningRate;
124 return (count > limit);
127 bool isEnabled() const
132 std::string toString() const
138 std::stringstream result;
139 if (d_action != DNSAction::Action::None) {
140 result << DNSAction::typeToString(d_action) << " ";
143 result << "Apply the global DynBlock action ";
145 result << "for " << std::to_string(d_blockDuration) << " seconds when over " << std::to_string(d_rate) << " during the last " << d_seconds << " seconds, reason: '" << d_blockReason << "'";
150 std::string d_blockReason;
151 struct timespec d_cutOff;
152 struct timespec d_minTime;
153 unsigned int d_blockDuration{0};
154 unsigned int d_rate{0};
155 unsigned int d_warningRate{0};
156 unsigned int d_seconds{0};
157 DNSAction::Action d_action{DNSAction::Action::None};
158 bool d_enabled{false};
161 struct DynBlockRatioRule: DynBlockRule
163 DynBlockRatioRule(): DynBlockRule()
167 DynBlockRatioRule(const std::string& blockReason, unsigned int blockDuration, double ratio, double warningRatio, unsigned int seconds, DNSAction::Action action, size_t minimumNumberOfResponses): DynBlockRule(blockReason, blockDuration, 0, 0, seconds, action), d_minimumNumberOfResponses(minimumNumberOfResponses), d_ratio(ratio), d_warningRatio(warningRatio)
171 bool ratioExceeded(unsigned int total, unsigned int count) const
177 if (total < d_minimumNumberOfResponses) {
181 double allowed = d_ratio * static_cast<double>(total);
182 return (count > allowed);
185 bool warningRatioExceeded(unsigned int total, unsigned int count) const
191 if (d_warningRatio == 0.0) {
195 if (total < d_minimumNumberOfResponses) {
199 double allowed = d_warningRatio * static_cast<double>(total);
200 return (count > allowed);
203 std::string toString() const
209 std::stringstream result;
210 if (d_action != DNSAction::Action::None) {
211 result << DNSAction::typeToString(d_action) << " ";
214 result << "Apply the global DynBlock action ";
216 result << "for " << std::to_string(d_blockDuration) << " seconds when over " << std::to_string(d_ratio) << " ratio during the last " << d_seconds << " seconds, reason: '" << d_blockReason << "'";
221 size_t d_minimumNumberOfResponses{0};
223 double d_warningRatio{0.0};
226 typedef std::unordered_map<AddressAndPortRange, Counts, AddressAndPortRange::hash> counts_t;
233 void setQueryRate(unsigned int rate, unsigned int warningRate, unsigned int seconds, const std::string& reason, unsigned int blockDuration, DNSAction::Action action)
235 d_queryRateRule = DynBlockRule(reason, blockDuration, rate, warningRate, seconds, action);
238 /* rate is in bytes per second */
239 void setResponseByteRate(unsigned int rate, unsigned int warningRate, unsigned int seconds, const std::string& reason, unsigned int blockDuration, DNSAction::Action action)
241 d_respRateRule = DynBlockRule(reason, blockDuration, rate, warningRate, seconds, action);
244 void setRCodeRate(uint8_t rcode, unsigned int rate, unsigned int warningRate, unsigned int seconds, const std::string& reason, unsigned int blockDuration, DNSAction::Action action)
246 auto& entry = d_rcodeRules[rcode];
247 entry = DynBlockRule(reason, blockDuration, rate, warningRate, seconds, action);
250 void setRCodeRatio(uint8_t rcode, double ratio, double warningRatio, unsigned int seconds, const std::string& reason, unsigned int blockDuration, DNSAction::Action action, size_t minimumNumberOfResponses)
252 auto& entry = d_rcodeRatioRules[rcode];
253 entry = DynBlockRatioRule(reason, blockDuration, ratio, warningRatio, seconds, action, minimumNumberOfResponses);
256 void setQTypeRate(uint16_t qtype, unsigned int rate, unsigned int warningRate, unsigned int seconds, const std::string& reason, unsigned int blockDuration, DNSAction::Action action)
258 auto& entry = d_qtypeRules[qtype];
259 entry = DynBlockRule(reason, blockDuration, rate, warningRate, seconds, action);
262 typedef std::function<std::tuple<bool, boost::optional<std::string>>(const StatNode&, const StatNode::Stat&, const StatNode::Stat&)> smtVisitor_t;
264 void setSuffixMatchRule(unsigned int seconds, const std::string& reason, unsigned int blockDuration, DNSAction::Action action, smtVisitor_t visitor)
266 d_suffixMatchRule = DynBlockRule(reason, blockDuration, 0, 0, seconds, action);
267 d_smtVisitor = std::move(visitor);
270 void setSuffixMatchRuleFFI(unsigned int seconds, const std::string& reason, unsigned int blockDuration, DNSAction::Action action, dnsdist_ffi_stat_node_visitor_t visitor)
272 d_suffixMatchRule = DynBlockRule(reason, blockDuration, 0, 0, seconds, action);
273 d_smtVisitorFFI = std::move(visitor);
276 void setMasks(uint8_t v4, uint8_t v6, uint8_t port)
291 void apply(const struct timespec& now);
293 void excludeRange(const Netmask& range)
295 d_excludedSubnets.addMask(range);
298 void excludeRange(const NetmaskGroup& group)
300 d_excludedSubnets.addMasks(group, true);
303 void includeRange(const Netmask& range)
305 d_excludedSubnets.addMask(range, false);
308 void includeRange(const NetmaskGroup& group)
310 d_excludedSubnets.addMasks(group, false);
313 void excludeDomain(const DNSName& domain)
315 d_excludedDomains.add(domain);
318 std::string toString() const
320 std::stringstream result;
322 result << "Query rate rule: " << d_queryRateRule.toString() << std::endl;
323 result << "Response rate rule: " << d_respRateRule.toString() << std::endl;
324 result << "SuffixMatch rule: " << d_suffixMatchRule.toString() << std::endl;
325 result << "RCode rules: " << std::endl;
326 for (const auto& rule : d_rcodeRules) {
327 result << "- " << RCode::to_s(rule.first) << ": " << rule.second.toString() << std::endl;
329 for (const auto& rule : d_rcodeRatioRules) {
330 result << "- " << RCode::to_s(rule.first) << ": " << rule.second.toString() << std::endl;
332 result << "QType rules: " << std::endl;
333 for (const auto& rule : d_qtypeRules) {
334 result << "- " << QType(rule.first).toString() << ": " << rule.second.toString() << std::endl;
336 result << "Excluded Subnets: " << d_excludedSubnets.toString() << std::endl;
337 result << "Excluded Domains: " << d_excludedDomains.toString() << std::endl;
342 void setQuiet(bool quiet)
349 bool checkIfQueryTypeMatches(const Rings::Query& query);
350 bool checkIfResponseCodeMatches(const Rings::Response& response);
351 void addOrRefreshBlock(boost::optional<NetmaskTree<DynBlock, AddressAndPortRange> >& blocks, const struct timespec& now, const AddressAndPortRange& requestor, const DynBlockRule& rule, bool& updated, bool warning);
352 void addOrRefreshBlockSMT(SuffixMatchTree<DynBlock>& blocks, const struct timespec& now, const DNSName& name, const DynBlockRule& rule, bool& updated);
354 void addBlock(boost::optional<NetmaskTree<DynBlock, AddressAndPortRange> >& blocks, const struct timespec& now, const AddressAndPortRange& requestor, const DynBlockRule& rule, bool& updated)
356 addOrRefreshBlock(blocks, now, requestor, rule, updated, false);
359 void handleWarning(boost::optional<NetmaskTree<DynBlock, AddressAndPortRange> >& blocks, const struct timespec& now, const AddressAndPortRange& requestor, const DynBlockRule& rule, bool& updated)
361 addOrRefreshBlock(blocks, now, requestor, rule, updated, true);
364 bool hasQueryRules() const
366 return d_queryRateRule.isEnabled() || !d_qtypeRules.empty();
369 bool hasResponseRules() const
371 return d_respRateRule.isEnabled() || !d_rcodeRules.empty() || !d_rcodeRatioRules.empty();
374 bool hasSuffixMatchRules() const
376 return d_suffixMatchRule.isEnabled();
379 bool hasRules() const
381 return hasQueryRules() || hasResponseRules();
384 void processQueryRules(counts_t& counts, const struct timespec& now);
385 void processResponseRules(counts_t& counts, StatNode& root, const struct timespec& now);
387 std::map<uint8_t, DynBlockRule> d_rcodeRules;
388 std::map<uint8_t, DynBlockRatioRule> d_rcodeRatioRules;
389 std::map<uint16_t, DynBlockRule> d_qtypeRules;
390 DynBlockRule d_queryRateRule;
391 DynBlockRule d_respRateRule;
392 DynBlockRule d_suffixMatchRule;
393 NetmaskGroup d_excludedSubnets;
394 SuffixMatchNode d_excludedDomains;
395 smtVisitor_t d_smtVisitor;
396 dnsdist_ffi_stat_node_visitor_t d_smtVisitorFFI;
397 uint8_t d_v6Mask{128};
398 uint8_t d_v4Mask{32};
399 uint8_t d_portMask{0};
400 bool d_beQuiet{false};
403 class DynBlockMaintenance
408 /* return the (cached) number of hits per second for the top offenders, averaged over 60s */
409 static std::map<std::string, std::list<std::pair<AddressAndPortRange, unsigned int>>> getHitsForTopNetmasks();
410 static std::map<std::string, std::list<std::pair<DNSName, unsigned int>>> getHitsForTopSuffixes();
412 /* get the the top offenders based on the current value of the counters */
413 static std::map<std::string, std::list<std::pair<AddressAndPortRange, unsigned int>>> getTopNetmasks(size_t topN);
414 static std::map<std::string, std::list<std::pair<DNSName, unsigned int>>> getTopSuffixes(size_t topN);
415 static void purgeExpired(const struct timespec& now);
417 static time_t s_expiredDynBlocksPurgeInterval;
420 static void collectMetrics();
421 static void generateMetrics();
423 struct MetricsSnapshot
425 std::map<std::string, std::list<std::pair<AddressAndPortRange, unsigned int>>> nmgData;
426 std::map<std::string, std::list<std::pair<DNSName, unsigned int>>> smtData;
431 std::map<std::string, std::list<std::pair<AddressAndPortRange, unsigned int>>> topNMGsByReason;
432 std::map<std::string, std::list<std::pair<DNSName, unsigned int>>> topSMTsByReason;
435 static LockGuarded<Tops> s_tops;
436 /* s_metricsData should only be accessed by the dynamic blocks maintenance thread so it does not need a lock */
437 // need N+1 datapoints to be able to do the diff after a collection point has been reached
438 static std::list<MetricsSnapshot> s_metricsData;
439 static size_t s_topN;
442 #endif /* DISABLE_DYNBLOCKS */