-
Notifications
You must be signed in to change notification settings - Fork 881
/
dnsdist-dynblocks.hh
331 lines (279 loc) · 9.19 KB
/
dnsdist-dynblocks.hh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
/*
* This file is part of PowerDNS or dnsdist.
* Copyright -- PowerDNS.COM B.V. and its contributors
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of version 2 of the GNU General Public License as
* published by the Free Software Foundation.
*
* In addition, for the avoidance of any doubt, permission is granted to
* link this program with OpenSSL and to (re)distribute the binaries
* produced as the result of such linking.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#pragma once
#include "dolog.hh"
#include "dnsdist-rings.hh"
class DynBlockRulesGroup
{
private:
struct Counts
{
std::map<uint8_t, uint64_t> d_rcodeCounts;
std::map<uint16_t, uint64_t> d_qtypeCounts;
uint64_t queries{0};
uint64_t respBytes{0};
};
struct DynBlockRule
{
DynBlockRule(): d_enabled(false)
{
}
DynBlockRule(const std::string& blockReason, unsigned int blockDuration, unsigned int rate, unsigned int seconds, DNSAction::Action action): d_blockReason(blockReason), d_blockDuration(blockDuration), d_rate(rate), d_seconds(seconds), d_action(action), d_enabled(true)
{
}
bool matches(const struct timespec& when)
{
if (!d_enabled) {
return false;
}
if (d_seconds && when < d_cutOff) {
return false;
}
if (when < d_minTime) {
d_minTime = when;
}
return true;
}
bool rateExceeded(unsigned int count, const struct timespec& now) const
{
if (!d_enabled) {
return false;
}
double delta = d_seconds ? d_seconds : DiffTime(now, d_minTime);
double limit = delta * d_rate;
return (count > limit);
}
bool isEnabled() const
{
return d_enabled;
}
std::string d_blockReason;
struct timespec d_cutOff;
struct timespec d_minTime;
unsigned int d_blockDuration{0};
unsigned int d_rate{0};
unsigned int d_seconds{0};
DNSAction::Action d_action{DNSAction::Action::None};
bool d_enabled{false};
};
typedef std::unordered_map<ComboAddress, Counts, ComboAddress::addressOnlyHash, ComboAddress::addressOnlyEqual> counts_t;
public:
DynBlockRulesGroup()
{
}
void setQueryRate(unsigned int rate, unsigned int seconds, std::string reason, unsigned int blockDuration, DNSAction::Action action)
{
d_queryRateRule = DynBlockRule(reason, blockDuration, rate, seconds, action);
}
void setResponseByteRate(unsigned int rate, unsigned int seconds, std::string reason, unsigned int blockDuration, DNSAction::Action action)
{
d_respRateRule = DynBlockRule(reason, blockDuration, rate, seconds, action);
}
void setRCodeRate(uint8_t rcode, unsigned int rate, unsigned int seconds, std::string reason, unsigned int blockDuration, DNSAction::Action action)
{
auto& entry = d_rcodeRules[rcode];
entry = DynBlockRule(reason, blockDuration, rate, seconds, action);
}
void setQTypeRate(uint16_t qtype, unsigned int rate, unsigned int seconds, std::string reason, unsigned int blockDuration, DNSAction::Action action)
{
auto& entry = d_qtypeRules[qtype];
entry = DynBlockRule(reason, blockDuration, rate, seconds, action);
}
void apply()
{
counts_t counts;
size_t entriesCount = 0;
if (hasQueryRules()) {
entriesCount += g_rings.getNumberOfQueryEntries();
}
if (hasResponseRules()) {
entriesCount += g_rings.getNumberOfResponseEntries();
}
counts.reserve(entriesCount);
processQueryRules(counts);
processResponseRules(counts);
if (counts.empty()) {
return;
}
boost::optional<NetmaskTree<DynBlock> > blocks;
bool updated = false;
struct timespec now;
gettime(&now);
for (const auto& entry : counts) {
if (d_queryRateRule.rateExceeded(entry.second.queries, now)) {
addBlock(blocks, now, entry.first, d_queryRateRule, updated);
continue;
}
if (d_respRateRule.rateExceeded(entry.second.respBytes, now)) {
addBlock(blocks, now, entry.first, d_respRateRule, updated);
continue;
}
for (const auto& rule : d_qtypeRules) {
const auto& typeIt = entry.second.d_qtypeCounts.find(rule.first);
if (typeIt != entry.second.d_qtypeCounts.cend() && rule.second.rateExceeded(typeIt->second, now)) {
addBlock(blocks, now, entry.first, rule.second, updated);
break;
}
}
for (const auto& rule : d_rcodeRules) {
const auto& rcodeIt = entry.second.d_rcodeCounts.find(rule.first);
if (rcodeIt != entry.second.d_rcodeCounts.cend() && rule.second.rateExceeded(rcodeIt->second, now)) {
addBlock(blocks, now, entry.first, rule.second, updated);
break;
}
}
}
if (updated && blocks) {
g_dynblockNMG.setState(*blocks);
}
}
private:
bool checkIfQueryTypeMatches(const Rings::Query& query)
{
auto rule = d_qtypeRules.find(query.qtype);
if (rule == d_qtypeRules.end()) {
return false;
}
return rule->second.matches(query.when);
}
bool checkIfResponseCodeMatches(const Rings::Response& response)
{
auto rule = d_rcodeRules.find(response.dh.rcode);
if (rule == d_rcodeRules.end()) {
return false;
}
return rule->second.matches(response.when);
}
void addBlock(boost::optional<NetmaskTree<DynBlock> >& blocks, const struct timespec& now, const ComboAddress& requestor, const DynBlockRule& rule, bool& updated)
{
if (!blocks) {
blocks = g_dynblockNMG.getCopy();
}
struct timespec until = now;
until.tv_sec += rule.d_blockDuration;
unsigned int count = 0;
const auto& got = blocks->lookup(Netmask(requestor));
bool expired = false;
if (got) {
if (until < got->second.until) {
// had a longer policy
return;
}
if (now < got->second.until) {
// only inherit count on fresh query we are extending
count = got->second.blocks;
}
else {
expired = true;
}
}
DynBlock db{rule.d_blockReason, until, DNSName(), rule.d_action};
db.blocks = count;
if (!got || expired) {
warnlog("Inserting dynamic block for %s for %d seconds: %s", requestor.toString(), rule.d_blockDuration, rule.d_blockReason);
}
blocks->insert(Netmask(requestor)).second = db;
updated = true;
}
bool hasQueryRules() const
{
return d_queryRateRule.isEnabled() || !d_qtypeRules.empty();
}
bool hasResponseRules() const
{
return d_respRateRule.isEnabled() || !d_rcodeRules.empty();
}
bool hasRules() const
{
return hasQueryRules() || hasResponseRules();
}
void processQueryRules(counts_t& counts)
{
if (!hasQueryRules()) {
return;
}
struct timespec now;
gettime(&now);
d_queryRateRule.d_cutOff = d_queryRateRule.d_minTime = now;
d_queryRateRule.d_cutOff.tv_sec -= d_queryRateRule.d_seconds;
for (auto& rule : d_qtypeRules) {
rule.second.d_cutOff = rule.second.d_minTime = now;
rule.second.d_cutOff.tv_sec -= rule.second.d_seconds;
}
for (const auto& shard : g_rings.d_shards) {
std::lock_guard<std::mutex> rl(shard->queryLock);
for(const auto& c : shard->queryRing) {
if (now < c.when) {
continue;
}
bool qRateMatches = d_queryRateRule.matches(c.when);
bool typeRuleMatches = checkIfQueryTypeMatches(c);
if (qRateMatches || typeRuleMatches) {
auto& entry = counts[c.requestor];
if (qRateMatches) {
entry.queries++;
}
if (typeRuleMatches) {
entry.d_qtypeCounts[c.qtype]++;
}
}
}
}
}
void processResponseRules(counts_t& counts)
{
if (!hasResponseRules()) {
return;
}
struct timespec now;
gettime(&now);
d_respRateRule.d_cutOff = d_respRateRule.d_minTime = now;
d_respRateRule.d_cutOff.tv_sec -= d_respRateRule.d_seconds;
for (auto& rule : d_rcodeRules) {
rule.second.d_cutOff = rule.second.d_minTime = now;
rule.second.d_cutOff.tv_sec -= rule.second.d_seconds;
}
for (const auto& shard : g_rings.d_shards) {
std::lock_guard<std::mutex> rl(shard->respLock);
for(const auto& c : shard->respRing) {
if (now < c.when) {
continue;
}
bool respRateMatches = d_respRateRule.matches(c.when);
bool rcodeRuleMatches = checkIfResponseCodeMatches(c);
if (respRateMatches || rcodeRuleMatches) {
auto& entry = counts[c.requestor];
if (respRateMatches) {
entry.respBytes += c.size;
}
if (rcodeRuleMatches) {
entry.d_rcodeCounts[c.dh.rcode]++;
}
}
}
}
}
std::map<uint8_t, DynBlockRule> d_rcodeRules;
std::map<uint16_t, DynBlockRule> d_qtypeRules;
DynBlockRule d_queryRateRule;
DynBlockRule d_respRateRule;
};