/src/pdns/pdns/dnsdistdist/dnsdist-rules-factory.hh
Line | Count | Source |
1 | | /* |
2 | | * This file is part of PowerDNS or dnsdist. |
3 | | * Copyright -- PowerDNS.COM B.V. and its contributors |
4 | | * |
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. |
8 | | * |
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. |
12 | | * |
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. |
17 | | * |
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. |
21 | | */ |
22 | | #pragma once |
23 | | |
24 | | #include <boost/multi_index_container.hpp> |
25 | | #include <boost/multi_index/ordered_index.hpp> |
26 | | #include <boost/multi_index/sequenced_index.hpp> |
27 | | #include <boost/multi_index/key_extractors.hpp> |
28 | | |
29 | | #include "cachecleaner.hh" |
30 | | #include "dnsdist-ecs.hh" |
31 | | #include "dnsdist-kvs.hh" |
32 | | #include "dnsdist-lua.hh" |
33 | | #include "dnsdist-lua-ffi.hh" |
34 | | #include "dnsdist-rules.hh" |
35 | | #include "dolog.hh" |
36 | | #include "dnsparser.hh" |
37 | | #include "dns_random.hh" |
38 | | #include "uuid-utils.hh" |
39 | | |
40 | | namespace dnsdist::selectors |
41 | | { |
42 | | using LuaSelectorFunction = std::function<bool(const DNSQuestion* dq)>; |
43 | | using LuaSelectorFFIFunction = std::function<bool(dnsdist_ffi_dnsquestion_t* dq)>; |
44 | | } |
45 | | |
46 | | class MaxQPSIPRule : public DNSRule |
47 | | { |
48 | | public: |
49 | | MaxQPSIPRule(unsigned int qps, unsigned int ipv4trunc = 32, unsigned int ipv6trunc = 64, unsigned int burst = 0, unsigned int expiration = 300, unsigned int cleanupDelay = 60, unsigned int scanFraction = 10, size_t shardsCount = 10) : |
50 | 0 | d_shards(shardsCount), d_qps(qps), d_burst(burst == 0 ? qps : burst), d_ipv4trunc(ipv4trunc), d_ipv6trunc(ipv6trunc), d_cleanupDelay(cleanupDelay), d_expiration(expiration), d_scanFraction(scanFraction) |
51 | 0 | { |
52 | 0 | d_cleaningUp.clear(); |
53 | 0 | gettime(&d_lastCleanup, true); |
54 | 0 | } |
55 | | |
56 | | void clear() |
57 | 0 | { |
58 | 0 | for (auto& shard : d_shards) { |
59 | 0 | shard.lock()->clear(); |
60 | 0 | } |
61 | 0 | } |
62 | | |
63 | | size_t cleanup(const struct timespec& cutOff, size_t* scannedCount = nullptr) const |
64 | 0 | { |
65 | 0 | size_t removed = 0; |
66 | 0 | if (scannedCount != nullptr) { |
67 | 0 | *scannedCount = 0; |
68 | 0 | } |
69 | |
|
70 | 0 | for (auto& shard : d_shards) { |
71 | 0 | auto limits = shard.lock(); |
72 | 0 | const size_t toLook = std::round((1.0 * limits->size()) / d_scanFraction) + 1; |
73 | 0 | size_t lookedAt = 0; |
74 | |
|
75 | 0 | auto& sequence = limits->get<SequencedTag>(); |
76 | 0 | for (auto entry = sequence.begin(); entry != sequence.end() && lookedAt < toLook; lookedAt++) { |
77 | 0 | if (entry->d_limiter.seenSince(cutOff)) { |
78 | | /* entries are ordered from least recently seen to more recently |
79 | | seen, as soon as we see one that has not expired yet, we are |
80 | | done */ |
81 | 0 | lookedAt++; |
82 | 0 | break; |
83 | 0 | } |
84 | | |
85 | 0 | entry = sequence.erase(entry); |
86 | 0 | removed++; |
87 | 0 | } |
88 | |
|
89 | 0 | if (scannedCount != nullptr) { |
90 | 0 | *scannedCount += lookedAt; |
91 | 0 | } |
92 | 0 | } |
93 | |
|
94 | 0 | return removed; |
95 | 0 | } |
96 | | |
97 | | void cleanupIfNeeded(const struct timespec& now) const |
98 | 0 | { |
99 | 0 | if (d_cleanupDelay > 0) { |
100 | 0 | struct timespec cutOff = d_lastCleanup; |
101 | 0 | cutOff.tv_sec += d_cleanupDelay; |
102 | |
|
103 | 0 | if (cutOff < now) { |
104 | 0 | try { |
105 | 0 | if (d_cleaningUp.test_and_set()) { |
106 | 0 | return; |
107 | 0 | } |
108 | | |
109 | 0 | d_lastCleanup = now; |
110 | | /* the QPS Limiter doesn't use realtime, be careful! */ |
111 | 0 | gettime(&cutOff, false); |
112 | 0 | cutOff.tv_sec -= d_expiration; |
113 | |
|
114 | 0 | cleanup(cutOff); |
115 | 0 | d_cleaningUp.clear(); |
116 | 0 | } |
117 | 0 | catch (...) { |
118 | 0 | d_cleaningUp.clear(); |
119 | 0 | throw; |
120 | 0 | } |
121 | 0 | } |
122 | 0 | } |
123 | 0 | } |
124 | | |
125 | | bool matches(const DNSQuestion* dq) const override |
126 | 0 | { |
127 | 0 | cleanupIfNeeded(dq->getQueryRealTime()); |
128 | |
|
129 | 0 | ComboAddress zeroport(dq->ids.origRemote); |
130 | 0 | zeroport.sin4.sin_port = 0; |
131 | 0 | zeroport.truncate(zeroport.sin4.sin_family == AF_INET ? d_ipv4trunc : d_ipv6trunc); |
132 | 0 | auto hash = ComboAddress::addressOnlyHash()(zeroport); |
133 | 0 | auto& shard = d_shards[hash % d_shards.size()]; |
134 | 0 | { |
135 | 0 | auto limits = shard.lock(); |
136 | 0 | auto iter = limits->find(zeroport); |
137 | 0 | if (iter == limits->end()) { |
138 | 0 | Entry e(zeroport, QPSLimiter(d_qps, d_burst)); |
139 | 0 | iter = limits->insert(e).first; |
140 | 0 | } |
141 | |
|
142 | 0 | moveCacheItemToBack<SequencedTag>(*limits, iter); |
143 | 0 | return !iter->d_limiter.check(d_qps, d_burst); |
144 | 0 | } |
145 | 0 | } |
146 | | |
147 | | string toString() const override |
148 | 0 | { |
149 | 0 | return "IP (/" + std::to_string(d_ipv4trunc) + ", /" + std::to_string(d_ipv6trunc) + ") match for QPS over " + std::to_string(d_qps) + " burst " + std::to_string(d_burst); |
150 | 0 | } |
151 | | |
152 | | size_t getEntriesCount() const |
153 | 0 | { |
154 | 0 | size_t count = 0; |
155 | 0 | for (auto& shard : d_shards) { |
156 | 0 | count += shard.lock()->size(); |
157 | 0 | } |
158 | 0 | return count; |
159 | 0 | } |
160 | | |
161 | | size_t getNumberOfShards() const |
162 | 0 | { |
163 | 0 | return d_shards.size(); |
164 | 0 | } |
165 | | |
166 | | private: |
167 | | struct HashedTag |
168 | | { |
169 | | }; |
170 | | struct SequencedTag |
171 | | { |
172 | | }; |
173 | | struct Entry |
174 | | { |
175 | | Entry(const ComboAddress& addr, BasicQPSLimiter&& limiter) : |
176 | 0 | d_limiter(limiter), d_addr(addr) |
177 | 0 | { |
178 | 0 | } |
179 | | mutable BasicQPSLimiter d_limiter; |
180 | | ComboAddress d_addr; |
181 | | }; |
182 | | |
183 | | using qpsContainer_t = multi_index_container< |
184 | | Entry, |
185 | | indexed_by< |
186 | | hashed_unique<tag<HashedTag>, member<Entry, ComboAddress, &Entry::d_addr>, ComboAddress::addressOnlyHash>, |
187 | | sequenced<tag<SequencedTag>>>>; |
188 | | |
189 | | mutable std::vector<LockGuarded<qpsContainer_t>> d_shards; |
190 | | mutable struct timespec d_lastCleanup; |
191 | | const unsigned int d_qps, d_burst, d_ipv4trunc, d_ipv6trunc, d_cleanupDelay, d_expiration; |
192 | | const unsigned int d_scanFraction{10}; |
193 | | mutable std::atomic_flag d_cleaningUp; |
194 | | }; |
195 | | |
196 | | class MaxQPSRule : public DNSRule |
197 | | { |
198 | | public: |
199 | | MaxQPSRule(unsigned int qps) : |
200 | | d_qps(qps, qps) |
201 | 0 | { |
202 | 0 | } |
203 | | |
204 | | MaxQPSRule(unsigned int qps, unsigned int burst) : |
205 | 0 | d_qps(qps, burst > 0 ? burst : qps) |
206 | 0 | { |
207 | 0 | } |
208 | | |
209 | | bool matches(const DNSQuestion* dnsQuestion) const override |
210 | 0 | { |
211 | 0 | (void)dnsQuestion; |
212 | 0 | return d_qps.check(); |
213 | 0 | } |
214 | | |
215 | | string toString() const override |
216 | 0 | { |
217 | 0 | return "Max " + std::to_string(d_qps.getRate()) + " qps"; |
218 | 0 | } |
219 | | |
220 | | private: |
221 | | mutable QPSLimiter d_qps; |
222 | | }; |
223 | | |
224 | | class NetmaskGroupRule : public DNSRule |
225 | | { |
226 | | public: |
227 | | NetmaskGroupRule(const NetmaskGroup& nmg, bool src, bool quiet = false) : |
228 | 0 | d_nmg(nmg) |
229 | 0 | { |
230 | 0 | d_src = src; |
231 | 0 | d_quiet = quiet; |
232 | 0 | } |
233 | | bool matches(const DNSQuestion* dq) const override |
234 | 0 | { |
235 | 0 | if (!d_src) { |
236 | 0 | return d_nmg.match(dq->ids.origDest); |
237 | 0 | } |
238 | 0 | return d_nmg.match(dq->ids.origRemote); |
239 | 0 | } |
240 | | |
241 | | string toString() const override |
242 | 0 | { |
243 | 0 | string ret = "Src: "; |
244 | 0 | if (!d_src) { |
245 | 0 | ret = "Dst: "; |
246 | 0 | } |
247 | 0 | if (d_quiet) { |
248 | 0 | return ret + "in-group"; |
249 | 0 | } |
250 | 0 | return ret + d_nmg.toString(); |
251 | 0 | } |
252 | | |
253 | | private: |
254 | | NetmaskGroup d_nmg; |
255 | | bool d_src; |
256 | | bool d_quiet; |
257 | | }; |
258 | | |
259 | | class TimedIPSetRule : public DNSRule, boost::noncopyable |
260 | | { |
261 | | private: |
262 | | struct IPv6 |
263 | | { |
264 | | IPv6(const ComboAddress& ca) |
265 | 0 | { |
266 | 0 | static_assert(sizeof(*this) == 16, "IPv6 struct has wrong size"); |
267 | 0 | memcpy((char*)this, ca.sin6.sin6_addr.s6_addr, 16); |
268 | 0 | } |
269 | | bool operator==(const IPv6& rhs) const |
270 | 0 | { |
271 | 0 | return a == rhs.a && b == rhs.b; |
272 | 0 | } |
273 | | uint64_t a, b; |
274 | | }; |
275 | | |
276 | | public: |
277 | | TimedIPSetRule() |
278 | 0 | { |
279 | 0 | } |
280 | | ~TimedIPSetRule() |
281 | 0 | { |
282 | 0 | } |
283 | | bool matches(const DNSQuestion* dq) const override |
284 | 0 | { |
285 | 0 | if (dq->ids.origRemote.sin4.sin_family == AF_INET) { |
286 | 0 | auto ip4s = d_ip4s.read_lock(); |
287 | 0 | auto fnd = ip4s->find(dq->ids.origRemote.sin4.sin_addr.s_addr); |
288 | 0 | if (fnd == ip4s->end()) { |
289 | 0 | return false; |
290 | 0 | } |
291 | 0 | return time(nullptr) < fnd->second; |
292 | 0 | } |
293 | 0 | else { |
294 | 0 | auto ip6s = d_ip6s.read_lock(); |
295 | 0 | auto fnd = ip6s->find({dq->ids.origRemote}); |
296 | 0 | if (fnd == ip6s->end()) { |
297 | 0 | return false; |
298 | 0 | } |
299 | 0 | return time(nullptr) < fnd->second; |
300 | 0 | } |
301 | 0 | } |
302 | | |
303 | | void add(const ComboAddress& ca, time_t ttd) |
304 | 0 | { |
305 | | // think twice before adding templates here |
306 | 0 | if (ca.sin4.sin_family == AF_INET) { |
307 | 0 | auto res = d_ip4s.write_lock()->insert({ca.sin4.sin_addr.s_addr, ttd}); |
308 | 0 | if (!res.second && (time_t)res.first->second < ttd) { |
309 | 0 | res.first->second = (uint32_t)ttd; |
310 | 0 | } |
311 | 0 | } |
312 | 0 | else { |
313 | 0 | auto res = d_ip6s.write_lock()->insert({{ca}, ttd}); |
314 | 0 | if (!res.second && (time_t)res.first->second < ttd) { |
315 | | // coverity[store_truncates_time_t] |
316 | 0 | res.first->second = (uint32_t)ttd; |
317 | 0 | } |
318 | 0 | } |
319 | 0 | } |
320 | | |
321 | | void remove(const ComboAddress& ca) |
322 | 0 | { |
323 | 0 | if (ca.sin4.sin_family == AF_INET) { |
324 | 0 | d_ip4s.write_lock()->erase(ca.sin4.sin_addr.s_addr); |
325 | 0 | } |
326 | 0 | else { |
327 | 0 | d_ip6s.write_lock()->erase({ca}); |
328 | 0 | } |
329 | 0 | } |
330 | | |
331 | | void clear() |
332 | 0 | { |
333 | 0 | d_ip4s.write_lock()->clear(); |
334 | 0 | d_ip6s.write_lock()->clear(); |
335 | 0 | } |
336 | | |
337 | | void cleanup() |
338 | 0 | { |
339 | 0 | time_t now = time(nullptr); |
340 | 0 | { |
341 | 0 | auto ip4s = d_ip4s.write_lock(); |
342 | 0 | for (auto iter = ip4s->begin(); iter != ip4s->end();) { |
343 | 0 | if (iter->second < now) { |
344 | 0 | iter = ip4s->erase(iter); |
345 | 0 | } |
346 | 0 | else { |
347 | 0 | ++iter; |
348 | 0 | } |
349 | 0 | } |
350 | 0 | } |
351 | |
|
352 | 0 | { |
353 | 0 | auto ip6s = d_ip6s.write_lock(); |
354 | 0 | for (auto iter = ip6s->begin(); iter != ip6s->end();) { |
355 | 0 | if (iter->second < now) { |
356 | 0 | iter = ip6s->erase(iter); |
357 | 0 | } |
358 | 0 | else { |
359 | 0 | ++iter; |
360 | 0 | } |
361 | 0 | } |
362 | 0 | } |
363 | 0 | } |
364 | | |
365 | | string toString() const override |
366 | 0 | { |
367 | 0 | time_t now = time(nullptr); |
368 | 0 | uint64_t count = 0; |
369 | |
|
370 | 0 | for (const auto& ip : *(d_ip4s.read_lock())) { |
371 | 0 | if (now < ip.second) { |
372 | 0 | ++count; |
373 | 0 | } |
374 | 0 | } |
375 | |
|
376 | 0 | for (const auto& ip : *(d_ip6s.read_lock())) { |
377 | 0 | if (now < ip.second) { |
378 | 0 | ++count; |
379 | 0 | } |
380 | 0 | } |
381 | |
|
382 | 0 | return "Src: " + std::to_string(count) + " ips"; |
383 | 0 | } |
384 | | |
385 | | private: |
386 | | struct IPv6Hash |
387 | | { |
388 | | std::size_t operator()(const IPv6& ip) const |
389 | 0 | { |
390 | 0 | auto ah = std::hash<uint64_t>{}(ip.a); |
391 | 0 | auto bh = std::hash<uint64_t>{}(ip.b); |
392 | 0 | return ah & (bh << 1); |
393 | 0 | } |
394 | | }; |
395 | | mutable SharedLockGuarded<std::unordered_map<IPv6, time_t, IPv6Hash>> d_ip6s; |
396 | | mutable SharedLockGuarded<std::unordered_map<uint32_t, time_t>> d_ip4s; |
397 | | }; |
398 | | |
399 | | class AllRule : public DNSRule |
400 | | { |
401 | | public: |
402 | 0 | AllRule() {} |
403 | | bool matches(const DNSQuestion* dnsQuestion) const override |
404 | 0 | { |
405 | 0 | (void)dnsQuestion; |
406 | 0 | return true; |
407 | 0 | } |
408 | | |
409 | | string toString() const override |
410 | 0 | { |
411 | 0 | return "All"; |
412 | 0 | } |
413 | | }; |
414 | | |
415 | | class DNSSECRule : public DNSRule |
416 | | { |
417 | | public: |
418 | | DNSSECRule() |
419 | 0 | { |
420 | 0 | } |
421 | | bool matches(const DNSQuestion* dq) const override |
422 | 0 | { |
423 | 0 | return dq->getHeader()->cd || (dnsdist::getEDNSZ(*dq) & EDNS_HEADER_FLAG_DO); // turns out dig sets ad by default.. |
424 | 0 | } |
425 | | |
426 | | string toString() const override |
427 | 0 | { |
428 | 0 | return "DNSSEC"; |
429 | 0 | } |
430 | | }; |
431 | | |
432 | | class AndRule : public DNSRule |
433 | | { |
434 | | public: |
435 | | AndRule(const std::vector<std::shared_ptr<DNSRule>>& rules) : |
436 | 0 | d_rules(rules) |
437 | 0 | { |
438 | 0 | } |
439 | | |
440 | | bool matches(const DNSQuestion* dq) const override |
441 | 0 | { |
442 | 0 | for (const auto& rule : d_rules) { |
443 | 0 | if (!rule->matches(dq)) { |
444 | 0 | return false; |
445 | 0 | } |
446 | 0 | } |
447 | 0 | return true; |
448 | 0 | } |
449 | | |
450 | | string toString() const override |
451 | 0 | { |
452 | 0 | string ret; |
453 | 0 | for (const auto& rule : d_rules) { |
454 | 0 | if (!ret.empty()) { |
455 | 0 | ret += " && "; |
456 | 0 | } |
457 | 0 | ret += "(" + rule->toString() + ")"; |
458 | 0 | } |
459 | 0 | return ret; |
460 | 0 | } |
461 | | |
462 | | private: |
463 | | std::vector<std::shared_ptr<DNSRule>> d_rules; |
464 | | }; |
465 | | |
466 | | class OrRule : public DNSRule |
467 | | { |
468 | | public: |
469 | | OrRule(const std::vector<std::shared_ptr<DNSRule>>& rules) : |
470 | 0 | d_rules(rules) |
471 | 0 | { |
472 | 0 | } |
473 | | |
474 | | bool matches(const DNSQuestion* dq) const override |
475 | 0 | { |
476 | 0 | for (const auto& rule : d_rules) { |
477 | 0 | if (rule->matches(dq)) { |
478 | 0 | return true; |
479 | 0 | } |
480 | 0 | } |
481 | 0 | return false; |
482 | 0 | } |
483 | | |
484 | | string toString() const override |
485 | 0 | { |
486 | 0 | string ret; |
487 | 0 | for (const auto& rule : d_rules) { |
488 | 0 | if (!ret.empty()) { |
489 | 0 | ret += " || "; |
490 | 0 | } |
491 | 0 | ret += "(" + rule->toString() + ")"; |
492 | 0 | } |
493 | 0 | return ret; |
494 | 0 | } |
495 | | |
496 | | private: |
497 | | std::vector<std::shared_ptr<DNSRule>> d_rules; |
498 | | }; |
499 | | |
500 | | class RegexRule : public DNSRule |
501 | | { |
502 | | public: |
503 | | RegexRule(const std::string& regex) : |
504 | 0 | d_visual(regex) |
505 | 0 | { |
506 | 0 | try { |
507 | 0 | d_regex = Regex(regex); |
508 | 0 | } |
509 | 0 | catch (const std::runtime_error& exp) { |
510 | 0 | throw std::runtime_error("Error compiling expression in RegexRule: " + std::string(exp.what())); |
511 | 0 | } |
512 | 0 | } |
513 | | bool matches(const DNSQuestion* dq) const override |
514 | 0 | { |
515 | 0 | return d_regex->match(dq->ids.qname.toStringNoDot()); |
516 | 0 | } |
517 | | |
518 | | string toString() const override |
519 | 0 | { |
520 | 0 | return "Regex: " + d_visual; |
521 | 0 | } |
522 | | |
523 | | private: |
524 | | std::optional<Regex> d_regex{std::nullopt}; |
525 | | string d_visual; |
526 | | }; |
527 | | |
528 | | #if defined(HAVE_RE2) |
529 | | #include <re2/re2.h> |
530 | | class RE2Rule : public DNSRule |
531 | | { |
532 | | public: |
533 | | RE2Rule(const std::string& re2) : |
534 | | d_re2(re2, RE2::Latin1), d_visual(re2) |
535 | | { |
536 | | } |
537 | | bool matches(const DNSQuestion* dq) const override |
538 | | { |
539 | | return RE2::FullMatch(dq->ids.qname.toStringNoDot(), d_re2); |
540 | | } |
541 | | |
542 | | string toString() const override |
543 | | { |
544 | | return "RE2 match: " + d_visual; |
545 | | } |
546 | | |
547 | | private: |
548 | | RE2 d_re2; |
549 | | string d_visual; |
550 | | }; |
551 | | #else /* HAVE_RE2 */ |
552 | | class RE2Rule : public DNSRule |
553 | | { |
554 | | public: |
555 | | RE2Rule(const std::string& /* re2 */) |
556 | 0 | { |
557 | 0 | throw std::runtime_error("RE2 support is disabled"); |
558 | 0 | } |
559 | | bool matches(const DNSQuestion* /* dq */) const override |
560 | 0 | { |
561 | 0 | return false; |
562 | 0 | } |
563 | | |
564 | | string toString() const override |
565 | 0 | { |
566 | 0 | return "Unsupported RE2"; |
567 | 0 | } |
568 | | }; |
569 | | #endif /* HAVE_RE2 */ |
570 | | |
571 | | class HTTPHeaderRule : public DNSRule |
572 | | { |
573 | | public: |
574 | | HTTPHeaderRule(const std::string& header, const std::string& regex); |
575 | | bool matches(const DNSQuestion* dnsQuestion) const override; |
576 | | string toString() const override; |
577 | | |
578 | | private: |
579 | | string d_header; |
580 | | std::optional<Regex> d_regex{std::nullopt}; |
581 | | string d_visual; |
582 | | }; |
583 | | |
584 | | class HTTPPathRule : public DNSRule |
585 | | { |
586 | | public: |
587 | | HTTPPathRule(std::string path); |
588 | | bool matches(const DNSQuestion* dnsQuestion) const override; |
589 | | string toString() const override; |
590 | | |
591 | | private: |
592 | | string d_path; |
593 | | }; |
594 | | |
595 | | class HTTPPathRegexRule : public DNSRule |
596 | | { |
597 | | public: |
598 | | HTTPPathRegexRule(const std::string& regex); |
599 | | bool matches(const DNSQuestion* dnsQuestion) const override; |
600 | | string toString() const override; |
601 | | |
602 | | private: |
603 | | std::optional<Regex> d_regex{std::nullopt}; |
604 | | std::string d_visual; |
605 | | }; |
606 | | |
607 | | class SNIRule : public DNSRule |
608 | | { |
609 | | public: |
610 | | SNIRule(const std::string& name) : |
611 | 0 | d_sni(name) |
612 | 0 | { |
613 | 0 | } |
614 | | bool matches(const DNSQuestion* dq) const override |
615 | 0 | { |
616 | 0 | return dq->sni == d_sni; |
617 | 0 | } |
618 | | string toString() const override |
619 | 0 | { |
620 | 0 | return "SNI == " + d_sni; |
621 | 0 | } |
622 | | |
623 | | private: |
624 | | std::string d_sni; |
625 | | }; |
626 | | |
627 | | class SuffixMatchNodeRule : public DNSRule |
628 | | { |
629 | | public: |
630 | | SuffixMatchNodeRule(const SuffixMatchNode& smn, bool quiet = false) : |
631 | 0 | d_smn(smn), d_quiet(quiet) |
632 | 0 | { |
633 | 0 | } |
634 | | bool matches(const DNSQuestion* dq) const override |
635 | 0 | { |
636 | 0 | return d_smn.check(dq->ids.qname); |
637 | 0 | } |
638 | | string toString() const override |
639 | 0 | { |
640 | 0 | if (d_quiet) |
641 | 0 | return "qname==in-set"; |
642 | 0 | else |
643 | 0 | return "qname in " + d_smn.toString(); |
644 | 0 | } |
645 | | |
646 | | private: |
647 | | SuffixMatchNode d_smn; |
648 | | bool d_quiet; |
649 | | }; |
650 | | |
651 | | class QNameRule : public DNSRule |
652 | | { |
653 | | public: |
654 | | QNameRule(const DNSName& qname) : |
655 | 0 | d_qname(qname) |
656 | 0 | { |
657 | 0 | } |
658 | | |
659 | | bool matches(const DNSQuestion* dq) const override |
660 | 0 | { |
661 | 0 | return d_qname == dq->ids.qname; |
662 | 0 | } |
663 | | string toString() const override |
664 | 0 | { |
665 | 0 | return "qname==" + d_qname.toString(); |
666 | 0 | } |
667 | | |
668 | | private: |
669 | | DNSName d_qname; |
670 | | }; |
671 | | |
672 | | class QNameSetRule : public DNSRule |
673 | | { |
674 | | public: |
675 | | QNameSetRule(const DNSNameSet& names) : |
676 | 0 | qname_idx(names) {} |
677 | | |
678 | | bool matches(const DNSQuestion* dq) const override |
679 | 0 | { |
680 | 0 | return qname_idx.find(dq->ids.qname) != qname_idx.end(); |
681 | 0 | } |
682 | | |
683 | | string toString() const override |
684 | 0 | { |
685 | 0 | std::stringstream ss; |
686 | 0 | ss << "qname in DNSNameSet(" << qname_idx.size() << " FQDNs)"; |
687 | 0 | return ss.str(); |
688 | 0 | } |
689 | | |
690 | | private: |
691 | | DNSNameSet qname_idx; |
692 | | }; |
693 | | |
694 | | class QTypeRule : public DNSRule |
695 | | { |
696 | | public: |
697 | | QTypeRule(uint16_t qtype) : |
698 | 0 | d_qtype(qtype) |
699 | 0 | { |
700 | 0 | } |
701 | | bool matches(const DNSQuestion* dq) const override |
702 | 0 | { |
703 | 0 | return d_qtype == dq->ids.qtype; |
704 | 0 | } |
705 | | string toString() const override |
706 | 0 | { |
707 | 0 | QType qt(d_qtype); |
708 | 0 | return "qtype==" + qt.toString(); |
709 | 0 | } |
710 | | |
711 | | private: |
712 | | uint16_t d_qtype; |
713 | | }; |
714 | | |
715 | | class QClassRule : public DNSRule |
716 | | { |
717 | | public: |
718 | | QClassRule(uint16_t qclass) : |
719 | 0 | d_qclass(qclass) |
720 | 0 | { |
721 | 0 | } |
722 | | bool matches(const DNSQuestion* dq) const override |
723 | 0 | { |
724 | 0 | return d_qclass == dq->ids.qclass; |
725 | 0 | } |
726 | | string toString() const override |
727 | 0 | { |
728 | 0 | return "qclass==" + std::to_string(d_qclass); |
729 | 0 | } |
730 | | |
731 | | private: |
732 | | uint16_t d_qclass; |
733 | | }; |
734 | | |
735 | | class OpcodeRule : public DNSRule |
736 | | { |
737 | | public: |
738 | | OpcodeRule(uint8_t opcode) : |
739 | 0 | d_opcode(opcode) |
740 | 0 | { |
741 | 0 | } |
742 | | bool matches(const DNSQuestion* dq) const override |
743 | 0 | { |
744 | 0 | return d_opcode == dq->getHeader()->opcode; |
745 | 0 | } |
746 | | string toString() const override |
747 | 0 | { |
748 | 0 | return "opcode==" + std::to_string(d_opcode); |
749 | 0 | } |
750 | | |
751 | | private: |
752 | | uint8_t d_opcode; |
753 | | }; |
754 | | |
755 | | class DSTPortRule : public DNSRule |
756 | | { |
757 | | public: |
758 | | DSTPortRule(uint16_t port) : |
759 | 0 | d_port(port) |
760 | 0 | { |
761 | 0 | } |
762 | | bool matches(const DNSQuestion* dq) const override |
763 | 0 | { |
764 | 0 | return htons(d_port) == dq->ids.origDest.sin4.sin_port; |
765 | 0 | } |
766 | | string toString() const override |
767 | 0 | { |
768 | 0 | return "dst port==" + std::to_string(d_port); |
769 | 0 | } |
770 | | |
771 | | private: |
772 | | uint16_t d_port; |
773 | | }; |
774 | | |
775 | | class TCPRule : public DNSRule |
776 | | { |
777 | | public: |
778 | | TCPRule(bool tcp) : |
779 | 0 | d_tcp(tcp) |
780 | 0 | { |
781 | 0 | } |
782 | | bool matches(const DNSQuestion* dq) const override |
783 | 0 | { |
784 | 0 | return dq->overTCP() == d_tcp; |
785 | 0 | } |
786 | | string toString() const override |
787 | 0 | { |
788 | 0 | return (d_tcp ? "TCP" : "UDP"); |
789 | 0 | } |
790 | | |
791 | | private: |
792 | | bool d_tcp; |
793 | | }; |
794 | | |
795 | | class NotRule : public DNSRule |
796 | | { |
797 | | public: |
798 | | NotRule(const std::shared_ptr<DNSRule>& rule) : |
799 | 0 | d_rule(rule) |
800 | 0 | { |
801 | 0 | } |
802 | | bool matches(const DNSQuestion* dq) const override |
803 | 0 | { |
804 | 0 | return !d_rule->matches(dq); |
805 | 0 | } |
806 | | string toString() const override |
807 | 0 | { |
808 | 0 | return "!(" + d_rule->toString() + ")"; |
809 | 0 | } |
810 | | |
811 | | private: |
812 | | std::shared_ptr<DNSRule> d_rule; |
813 | | }; |
814 | | |
815 | | class RecordsCountRule : public DNSRule |
816 | | { |
817 | | public: |
818 | | RecordsCountRule(uint8_t section, uint16_t minCount, uint16_t maxCount) : |
819 | 0 | d_minCount(minCount), d_maxCount(maxCount), d_section(section) |
820 | 0 | { |
821 | 0 | } |
822 | | bool matches(const DNSQuestion* dq) const override |
823 | 0 | { |
824 | 0 | uint16_t count = 0; |
825 | 0 | switch (d_section) { |
826 | 0 | case 0: |
827 | 0 | count = ntohs(dq->getHeader()->qdcount); |
828 | 0 | break; |
829 | 0 | case 1: |
830 | 0 | count = ntohs(dq->getHeader()->ancount); |
831 | 0 | break; |
832 | 0 | case 2: |
833 | 0 | count = ntohs(dq->getHeader()->nscount); |
834 | 0 | break; |
835 | 0 | case 3: |
836 | 0 | count = ntohs(dq->getHeader()->arcount); |
837 | 0 | break; |
838 | 0 | } |
839 | 0 | return count >= d_minCount && count <= d_maxCount; |
840 | 0 | } |
841 | | string toString() const override |
842 | 0 | { |
843 | 0 | string section; |
844 | 0 | switch (d_section) { |
845 | 0 | case 0: |
846 | 0 | section = "QD"; |
847 | 0 | break; |
848 | 0 | case 1: |
849 | 0 | section = "AN"; |
850 | 0 | break; |
851 | 0 | case 2: |
852 | 0 | section = "NS"; |
853 | 0 | break; |
854 | 0 | case 3: |
855 | 0 | section = "AR"; |
856 | 0 | break; |
857 | 0 | } |
858 | 0 | return std::to_string(d_minCount) + " <= records in " + section + " <= " + std::to_string(d_maxCount); |
859 | 0 | } |
860 | | |
861 | | private: |
862 | | uint16_t d_minCount; |
863 | | uint16_t d_maxCount; |
864 | | uint8_t d_section; |
865 | | }; |
866 | | |
867 | | class RecordsTypeCountRule : public DNSRule |
868 | | { |
869 | | public: |
870 | | RecordsTypeCountRule(uint8_t section, uint16_t type, uint16_t minCount, uint16_t maxCount) : |
871 | 0 | d_type(type), d_minCount(minCount), d_maxCount(maxCount), d_section(section) |
872 | 0 | { |
873 | 0 | } |
874 | | bool matches(const DNSQuestion* dq) const override |
875 | 0 | { |
876 | 0 | uint16_t count = 0; |
877 | 0 | switch (d_section) { |
878 | 0 | case 0: |
879 | 0 | count = ntohs(dq->getHeader()->qdcount); |
880 | 0 | break; |
881 | 0 | case 1: |
882 | 0 | count = ntohs(dq->getHeader()->ancount); |
883 | 0 | break; |
884 | 0 | case 2: |
885 | 0 | count = ntohs(dq->getHeader()->nscount); |
886 | 0 | break; |
887 | 0 | case 3: |
888 | 0 | count = ntohs(dq->getHeader()->arcount); |
889 | 0 | break; |
890 | 0 | } |
891 | 0 | if (count < d_minCount) { |
892 | 0 | return false; |
893 | 0 | } |
894 | 0 | count = getRecordsOfTypeCount(reinterpret_cast<const char*>(dq->getData().data()), dq->getData().size(), d_section, d_type); |
895 | 0 | return count >= d_minCount && count <= d_maxCount; |
896 | 0 | } |
897 | | string toString() const override |
898 | 0 | { |
899 | 0 | string section; |
900 | 0 | switch (d_section) { |
901 | 0 | case 0: |
902 | 0 | section = "QD"; |
903 | 0 | break; |
904 | 0 | case 1: |
905 | 0 | section = "AN"; |
906 | 0 | break; |
907 | 0 | case 2: |
908 | 0 | section = "NS"; |
909 | 0 | break; |
910 | 0 | case 3: |
911 | 0 | section = "AR"; |
912 | 0 | break; |
913 | 0 | } |
914 | 0 | return std::to_string(d_minCount) + " <= " + QType(d_type).toString() + " records in " + section + " <= " + std::to_string(d_maxCount); |
915 | 0 | } |
916 | | |
917 | | private: |
918 | | uint16_t d_type; |
919 | | uint16_t d_minCount; |
920 | | uint16_t d_maxCount; |
921 | | uint8_t d_section; |
922 | | }; |
923 | | |
924 | | class TrailingDataRule : public DNSRule |
925 | | { |
926 | | public: |
927 | | TrailingDataRule() |
928 | 0 | { |
929 | 0 | } |
930 | | bool matches(const DNSQuestion* dq) const override |
931 | 0 | { |
932 | 0 | uint16_t length = getDNSPacketLength(reinterpret_cast<const char*>(dq->getData().data()), dq->getData().size()); |
933 | 0 | return length < dq->getData().size(); |
934 | 0 | } |
935 | | string toString() const override |
936 | 0 | { |
937 | 0 | return "trailing data"; |
938 | 0 | } |
939 | | }; |
940 | | |
941 | | class QNameLabelsCountRule : public DNSRule |
942 | | { |
943 | | public: |
944 | | QNameLabelsCountRule(unsigned int minLabelsCount, unsigned int maxLabelsCount) : |
945 | 0 | d_min(minLabelsCount), d_max(maxLabelsCount) |
946 | 0 | { |
947 | 0 | } |
948 | | bool matches(const DNSQuestion* dq) const override |
949 | 0 | { |
950 | 0 | unsigned int count = dq->ids.qname.countLabels(); |
951 | 0 | return count < d_min || count > d_max; |
952 | 0 | } |
953 | | string toString() const override |
954 | 0 | { |
955 | 0 | return "labels count < " + std::to_string(d_min) + " || labels count > " + std::to_string(d_max); |
956 | 0 | } |
957 | | |
958 | | private: |
959 | | unsigned int d_min; |
960 | | unsigned int d_max; |
961 | | }; |
962 | | |
963 | | class QNameWireLengthRule : public DNSRule |
964 | | { |
965 | | public: |
966 | | QNameWireLengthRule(size_t min, size_t max) : |
967 | 0 | d_min(min), d_max(max) |
968 | 0 | { |
969 | 0 | } |
970 | | bool matches(const DNSQuestion* dq) const override |
971 | 0 | { |
972 | 0 | size_t const wirelength = dq->ids.qname.wirelength(); |
973 | 0 | return wirelength < d_min || wirelength > d_max; |
974 | 0 | } |
975 | | string toString() const override |
976 | 0 | { |
977 | 0 | return "wire length < " + std::to_string(d_min) + " || wire length > " + std::to_string(d_max); |
978 | 0 | } |
979 | | |
980 | | private: |
981 | | size_t d_min; |
982 | | size_t d_max; |
983 | | }; |
984 | | |
985 | | class RCodeRule : public DNSRule |
986 | | { |
987 | | public: |
988 | | RCodeRule(uint8_t rcode) : |
989 | 0 | d_rcode(rcode) |
990 | 0 | { |
991 | 0 | } |
992 | | bool matches(const DNSQuestion* dq) const override |
993 | 0 | { |
994 | 0 | return d_rcode == dq->getHeader()->rcode; |
995 | 0 | } |
996 | | string toString() const override |
997 | 0 | { |
998 | 0 | return "rcode==" + RCode::to_s(d_rcode); |
999 | 0 | } |
1000 | | |
1001 | | private: |
1002 | | uint8_t d_rcode; |
1003 | | }; |
1004 | | |
1005 | | class ERCodeRule : public DNSRule |
1006 | | { |
1007 | | public: |
1008 | | ERCodeRule(uint8_t rcode) : |
1009 | 0 | d_rcode(rcode & 0xF), d_extrcode(rcode >> 4) |
1010 | 0 | { |
1011 | 0 | } |
1012 | | bool matches(const DNSQuestion* dq) const override |
1013 | 0 | { |
1014 | | // avoid parsing EDNS OPT RR when not needed. |
1015 | 0 | if (d_rcode != dq->getHeader()->rcode) { |
1016 | 0 | return false; |
1017 | 0 | } |
1018 | | |
1019 | 0 | EDNS0Record edns0; |
1020 | 0 | if (!getEDNS0Record(dq->getData(), edns0)) { |
1021 | 0 | return false; |
1022 | 0 | } |
1023 | | |
1024 | 0 | return d_extrcode == edns0.extRCode; |
1025 | 0 | } |
1026 | | string toString() const override |
1027 | 0 | { |
1028 | 0 | return "ercode==" + ERCode::to_s(d_rcode | (d_extrcode << 4)); |
1029 | 0 | } |
1030 | | |
1031 | | private: |
1032 | | uint8_t d_rcode; // plain DNS Rcode |
1033 | | uint8_t d_extrcode; // upper bits in EDNS0 record |
1034 | | }; |
1035 | | |
1036 | | class EDNSVersionRule : public DNSRule |
1037 | | { |
1038 | | public: |
1039 | | EDNSVersionRule(uint8_t version) : |
1040 | 0 | d_version(version) |
1041 | 0 | { |
1042 | 0 | } |
1043 | | bool matches(const DNSQuestion* dq) const override |
1044 | 0 | { |
1045 | 0 | EDNS0Record edns0; |
1046 | 0 | if (!getEDNS0Record(dq->getData(), edns0)) { |
1047 | 0 | return false; |
1048 | 0 | } |
1049 | | |
1050 | 0 | return d_version < edns0.version; |
1051 | 0 | } |
1052 | | string toString() const override |
1053 | 0 | { |
1054 | 0 | return "ednsversion>" + std::to_string(d_version); |
1055 | 0 | } |
1056 | | |
1057 | | private: |
1058 | | uint8_t d_version; |
1059 | | }; |
1060 | | |
1061 | | class EDNSOptionRule : public DNSRule |
1062 | | { |
1063 | | public: |
1064 | | EDNSOptionRule(uint16_t optcode) : |
1065 | 0 | d_optcode(optcode) |
1066 | 0 | { |
1067 | 0 | } |
1068 | | bool matches(const DNSQuestion* dq) const override |
1069 | 0 | { |
1070 | 0 | uint16_t optStart; |
1071 | 0 | size_t optLen = 0; |
1072 | 0 | bool last = false; |
1073 | 0 | int res = locateEDNSOptRR(dq->getData(), &optStart, &optLen, &last); |
1074 | 0 | if (res != 0) { |
1075 | | // no EDNS OPT RR |
1076 | 0 | return false; |
1077 | 0 | } |
1078 | | |
1079 | 0 | if (optLen < optRecordMinimumSize) { |
1080 | 0 | return false; |
1081 | 0 | } |
1082 | | |
1083 | 0 | if (optStart < dq->getData().size() && dq->getData().at(optStart) != 0) { |
1084 | | // OPT RR Name != '.' |
1085 | 0 | return false; |
1086 | 0 | } |
1087 | | |
1088 | 0 | return isEDNSOptionInOpt(dq->getData(), optStart, optLen, d_optcode); |
1089 | 0 | } |
1090 | | string toString() const override |
1091 | 0 | { |
1092 | 0 | return "ednsoptcode==" + std::to_string(d_optcode); |
1093 | 0 | } |
1094 | | |
1095 | | private: |
1096 | | uint16_t d_optcode; |
1097 | | }; |
1098 | | |
1099 | | class RDRule : public DNSRule |
1100 | | { |
1101 | | public: |
1102 | | RDRule() |
1103 | 0 | { |
1104 | 0 | } |
1105 | | bool matches(const DNSQuestion* dq) const override |
1106 | 0 | { |
1107 | 0 | return dq->getHeader()->rd == 1; |
1108 | 0 | } |
1109 | | string toString() const override |
1110 | 0 | { |
1111 | 0 | return "rd==1"; |
1112 | 0 | } |
1113 | | }; |
1114 | | |
1115 | | class ProbaRule : public DNSRule |
1116 | | { |
1117 | | public: |
1118 | | ProbaRule(double proba) : |
1119 | 0 | d_proba(proba) |
1120 | 0 | { |
1121 | 0 | } |
1122 | | bool matches(const DNSQuestion* dnsQuestion) const override |
1123 | 0 | { |
1124 | 0 | (void)dnsQuestion; |
1125 | 0 | if (d_proba == 1.0) { |
1126 | 0 | return true; |
1127 | 0 | } |
1128 | 0 | double rnd = 1.0 * dns_random_uint32() / UINT32_MAX; |
1129 | 0 | return rnd > (1.0 - d_proba); |
1130 | 0 | } |
1131 | | string toString() const override |
1132 | 0 | { |
1133 | 0 | return "match with prob. " + (boost::format("%0.2f") % d_proba).str(); |
1134 | 0 | } |
1135 | | |
1136 | | private: |
1137 | | double d_proba; |
1138 | | }; |
1139 | | |
1140 | | class TagRule : public DNSRule |
1141 | | { |
1142 | | public: |
1143 | | TagRule(const std::string& tag, std::optional<std::string> value, bool emptyAsWildcard) : |
1144 | 0 | d_value(std::move(value)), d_tag(tag), d_emptyAsWildcard(emptyAsWildcard) |
1145 | 0 | { |
1146 | 0 | } |
1147 | | bool matches(const DNSQuestion* dq) const override |
1148 | 0 | { |
1149 | 0 | if (!dq->ids.qTag) { |
1150 | 0 | return false; |
1151 | 0 | } |
1152 | | |
1153 | 0 | const auto it = dq->ids.qTag->find(d_tag); |
1154 | 0 | if (it == dq->ids.qTag->cend()) { |
1155 | 0 | return false; |
1156 | 0 | } |
1157 | | |
1158 | 0 | if (!d_value || (d_emptyAsWildcard && d_value->empty())) { |
1159 | 0 | return true; |
1160 | 0 | } |
1161 | | |
1162 | 0 | return it->second == *d_value; |
1163 | 0 | } |
1164 | | |
1165 | | string toString() const override |
1166 | 0 | { |
1167 | 0 | return "tag '" + d_tag + "' is set" + ((d_value && (!d_value->empty() || !d_emptyAsWildcard)) ? (" to '" + *d_value + "'") : ""); |
1168 | 0 | } |
1169 | | |
1170 | | private: |
1171 | | std::optional<std::string> d_value; |
1172 | | std::string d_tag; |
1173 | | bool d_emptyAsWildcard; |
1174 | | }; |
1175 | | |
1176 | | class PoolAvailableRule : public DNSRule |
1177 | | { |
1178 | | public: |
1179 | | PoolAvailableRule(const std::string& poolname) : |
1180 | 0 | d_poolname(poolname) |
1181 | 0 | { |
1182 | 0 | } |
1183 | | |
1184 | | bool matches(const DNSQuestion* dnsQuestion) const override |
1185 | 0 | { |
1186 | 0 | (void)dnsQuestion; |
1187 | 0 | return (getPool(d_poolname).countServers(true) > 0); |
1188 | 0 | } |
1189 | | |
1190 | | string toString() const override |
1191 | 0 | { |
1192 | 0 | return "pool '" + d_poolname + "' is available"; |
1193 | 0 | } |
1194 | | |
1195 | | private: |
1196 | | std::string d_poolname; |
1197 | | }; |
1198 | | |
1199 | | class PoolOutstandingRule : public DNSRule |
1200 | | { |
1201 | | public: |
1202 | | PoolOutstandingRule(const std::string& poolname, const size_t limit) : |
1203 | 0 | d_poolname(poolname), d_limit(limit) |
1204 | 0 | { |
1205 | 0 | } |
1206 | | |
1207 | | bool matches(const DNSQuestion* dnsQuestion) const override |
1208 | 0 | { |
1209 | 0 | (void)dnsQuestion; |
1210 | 0 | return (getPool(d_poolname).poolLoad()) > d_limit; |
1211 | 0 | } |
1212 | | |
1213 | | string toString() const override |
1214 | 0 | { |
1215 | 0 | return "pool '" + d_poolname + "' outstanding > " + std::to_string(d_limit); |
1216 | 0 | } |
1217 | | |
1218 | | private: |
1219 | | std::string d_poolname; |
1220 | | size_t d_limit; |
1221 | | }; |
1222 | | |
1223 | | class KeyValueStoreLookupRule : public DNSRule |
1224 | | { |
1225 | | public: |
1226 | | KeyValueStoreLookupRule(const std::shared_ptr<KeyValueStore>& kvs, const std::shared_ptr<KeyValueLookupKey>& lookupKey) : |
1227 | 0 | d_kvs(kvs), d_key(lookupKey) |
1228 | 0 | { |
1229 | 0 | } |
1230 | | |
1231 | | bool matches(const DNSQuestion* dq) const override |
1232 | 0 | { |
1233 | 0 | std::vector<std::string> keys = d_key->getKeys(*dq); |
1234 | 0 | for (const auto& key : keys) { |
1235 | 0 | if (d_kvs->keyExists(key) == true) { |
1236 | 0 | return true; |
1237 | 0 | } |
1238 | 0 | } |
1239 | | |
1240 | 0 | return false; |
1241 | 0 | } |
1242 | | |
1243 | | string toString() const override |
1244 | 0 | { |
1245 | 0 | return "lookup key-value store based on '" + d_key->toString() + "'"; |
1246 | 0 | } |
1247 | | |
1248 | | private: |
1249 | | std::shared_ptr<KeyValueStore> d_kvs; |
1250 | | std::shared_ptr<KeyValueLookupKey> d_key; |
1251 | | }; |
1252 | | |
1253 | | class KeyValueStoreRangeLookupRule : public DNSRule |
1254 | | { |
1255 | | public: |
1256 | | KeyValueStoreRangeLookupRule(const std::shared_ptr<KeyValueStore>& kvs, const std::shared_ptr<KeyValueLookupKey>& lookupKey) : |
1257 | 0 | d_kvs(kvs), d_key(lookupKey) |
1258 | 0 | { |
1259 | 0 | } |
1260 | | |
1261 | | bool matches(const DNSQuestion* dq) const override |
1262 | 0 | { |
1263 | 0 | std::vector<std::string> keys = d_key->getKeys(*dq); |
1264 | 0 | for (const auto& key : keys) { |
1265 | 0 | std::string value; |
1266 | 0 | if (d_kvs->getRangeValue(key, value) == true) { |
1267 | 0 | return true; |
1268 | 0 | } |
1269 | 0 | } |
1270 | | |
1271 | 0 | return false; |
1272 | 0 | } |
1273 | | |
1274 | | string toString() const override |
1275 | 0 | { |
1276 | 0 | return "range-based lookup key-value store based on '" + d_key->toString() + "'"; |
1277 | 0 | } |
1278 | | |
1279 | | private: |
1280 | | std::shared_ptr<KeyValueStore> d_kvs; |
1281 | | std::shared_ptr<KeyValueLookupKey> d_key; |
1282 | | }; |
1283 | | |
1284 | | class LuaRule : public DNSRule |
1285 | | { |
1286 | | public: |
1287 | | LuaRule(const dnsdist::selectors::LuaSelectorFunction& func) : |
1288 | 0 | d_func(func) |
1289 | 0 | {} |
1290 | | |
1291 | | bool matches(const DNSQuestion* dq) const override |
1292 | 0 | { |
1293 | 0 | try { |
1294 | 0 | auto lock = g_lua.lock(); |
1295 | 0 | return d_func(dq); |
1296 | 0 | } |
1297 | 0 | catch (const std::exception& e) { |
1298 | 0 | SLOG(warnlog("LuaRule failed inside Lua: %s", e.what()), |
1299 | 0 | dq->getLogger()->error(Logr::Warning, e.what(), "LuaRule failed inside Lua")); |
1300 | 0 | } |
1301 | 0 | catch (...) { |
1302 | 0 | SLOG(warnlog("LuaRule failed inside Lua: [unknown exception]"), |
1303 | 0 | dq->getLogger()->info(Logr::Warning, "LuaRule failed inside Lua with an unknown exception")); |
1304 | 0 | } |
1305 | 0 | return false; |
1306 | 0 | } |
1307 | | |
1308 | | string toString() const override |
1309 | 0 | { |
1310 | 0 | return "Lua script"; |
1311 | 0 | } |
1312 | | |
1313 | | private: |
1314 | | dnsdist::selectors::LuaSelectorFunction d_func; |
1315 | | }; |
1316 | | |
1317 | | class LuaFFIRule : public DNSRule |
1318 | | { |
1319 | | public: |
1320 | | LuaFFIRule(const dnsdist::selectors::LuaSelectorFFIFunction& func) : |
1321 | 0 | d_func(func) |
1322 | 0 | {} |
1323 | | |
1324 | | bool matches(const DNSQuestion* dq) const override |
1325 | 0 | { |
1326 | 0 | dnsdist_ffi_dnsquestion_t dqffi(const_cast<DNSQuestion*>(dq)); |
1327 | 0 | try { |
1328 | 0 | auto lock = g_lua.lock(); |
1329 | 0 | return d_func(&dqffi); |
1330 | 0 | } |
1331 | 0 | catch (const std::exception& e) { |
1332 | 0 | SLOG(warnlog("LuaFFIRule failed inside Lua: %s", e.what()), |
1333 | 0 | dq->getLogger()->error(Logr::Warning, e.what(), "LuaFFIRule failed inside Lua")); |
1334 | 0 | } |
1335 | 0 | catch (...) { |
1336 | 0 | SLOG(warnlog("LuaFFIRule failed inside Lua: [unknown exception]"), |
1337 | 0 | dq->getLogger()->info(Logr::Warning, "LuaFFIRule failed inside Lua with an unknown exception")); |
1338 | 0 | } |
1339 | 0 | return false; |
1340 | 0 | } |
1341 | | |
1342 | | string toString() const override |
1343 | 0 | { |
1344 | 0 | return "Lua FFI script"; |
1345 | 0 | } |
1346 | | |
1347 | | private: |
1348 | | dnsdist::selectors::LuaSelectorFFIFunction d_func; |
1349 | | }; |
1350 | | |
1351 | | class LuaFFIPerThreadRule : public DNSRule |
1352 | | { |
1353 | | public: |
1354 | | LuaFFIPerThreadRule(const std::string& code) : |
1355 | 0 | d_functionCode(code), d_functionID(s_functionsCounter++) |
1356 | 0 | { |
1357 | 0 | } |
1358 | | |
1359 | | bool matches(const DNSQuestion* dq) const override |
1360 | 0 | { |
1361 | 0 | try { |
1362 | 0 | auto& state = t_perThreadStates[d_functionID]; |
1363 | 0 | if (!state.d_initialized) { |
1364 | 0 | setupLuaFFIPerThreadContext(state.d_luaContext); |
1365 | | /* mark the state as initialized first so if there is a syntax error |
1366 | | we only try to execute the code once */ |
1367 | 0 | state.d_initialized = true; |
1368 | 0 | state.d_func = state.d_luaContext.executeCode<dnsdist::selectors::LuaSelectorFFIFunction>(d_functionCode); |
1369 | 0 | } |
1370 | |
|
1371 | 0 | if (!state.d_func) { |
1372 | | /* the function was not properly initialized */ |
1373 | 0 | return false; |
1374 | 0 | } |
1375 | | |
1376 | 0 | dnsdist_ffi_dnsquestion_t dqffi(const_cast<DNSQuestion*>(dq)); |
1377 | 0 | return state.d_func(&dqffi); |
1378 | 0 | } |
1379 | 0 | catch (const std::exception& e) { |
1380 | 0 | SLOG(warnlog("LuaFFIPerthreadRule failed inside Lua: %s", e.what()), |
1381 | 0 | dq->getLogger()->error(Logr::Warning, e.what(), "LuaFFIPerThreadRule failed inside Lua")); |
1382 | 0 | } |
1383 | 0 | catch (...) { |
1384 | 0 | SLOG(warnlog("LuaFFIPerThreadRule failed inside Lua: [unknown exception]"), |
1385 | 0 | dq->getLogger()->info(Logr::Warning, "LuaFFIPerThreadRule failed inside Lua with an unknown exception")); |
1386 | 0 | } |
1387 | 0 | return false; |
1388 | 0 | } |
1389 | | |
1390 | | string toString() const override |
1391 | 0 | { |
1392 | 0 | return "Lua FFI per-thread script"; |
1393 | 0 | } |
1394 | | |
1395 | | private: |
1396 | | struct PerThreadState |
1397 | | { |
1398 | | LuaContext d_luaContext; |
1399 | | dnsdist::selectors::LuaSelectorFFIFunction d_func; |
1400 | | bool d_initialized{false}; |
1401 | | }; |
1402 | | |
1403 | | static std::atomic<uint64_t> s_functionsCounter; |
1404 | | static thread_local std::map<uint64_t, PerThreadState> t_perThreadStates; |
1405 | | const std::string d_functionCode; |
1406 | | const uint64_t d_functionID; |
1407 | | }; |
1408 | | |
1409 | | class ProxyProtocolValueRule : public DNSRule |
1410 | | { |
1411 | | public: |
1412 | | ProxyProtocolValueRule(uint8_t type, std::optional<std::string> value) : |
1413 | 0 | d_value(std::move(value)), d_type(type) |
1414 | 0 | { |
1415 | 0 | } |
1416 | | |
1417 | | bool matches(const DNSQuestion* dq) const override |
1418 | 0 | { |
1419 | 0 | if (!dq->proxyProtocolValues) { |
1420 | 0 | return false; |
1421 | 0 | } |
1422 | | |
1423 | 0 | for (const auto& entry : *dq->proxyProtocolValues) { |
1424 | 0 | if (entry.type == d_type && (!d_value || d_value->empty() || entry.content == *d_value)) { |
1425 | 0 | return true; |
1426 | 0 | } |
1427 | 0 | } |
1428 | | |
1429 | 0 | return false; |
1430 | 0 | } |
1431 | | |
1432 | | string toString() const override |
1433 | 0 | { |
1434 | 0 | if (d_value) { |
1435 | 0 | return "proxy protocol value of type " + std::to_string(d_type) + " matches"; |
1436 | 0 | } |
1437 | 0 | return "proxy protocol value of type " + std::to_string(d_type) + " is present"; |
1438 | 0 | } |
1439 | | |
1440 | | private: |
1441 | | std::optional<std::string> d_value; |
1442 | | uint8_t d_type; |
1443 | | }; |
1444 | | |
1445 | | class PayloadSizeRule : public DNSRule |
1446 | | { |
1447 | | enum class Comparisons : uint8_t |
1448 | | { |
1449 | | equal, |
1450 | | greater, |
1451 | | greaterOrEqual, |
1452 | | smaller, |
1453 | | smallerOrEqual |
1454 | | }; |
1455 | | |
1456 | | public: |
1457 | | PayloadSizeRule(const std::string& comparison, uint16_t size) : |
1458 | 0 | d_size(size) |
1459 | 0 | { |
1460 | 0 | if (comparison == "equal") { |
1461 | 0 | d_comparison = Comparisons::equal; |
1462 | 0 | } |
1463 | 0 | else if (comparison == "greater") { |
1464 | 0 | d_comparison = Comparisons::greater; |
1465 | 0 | } |
1466 | 0 | else if (comparison == "greaterOrEqual") { |
1467 | 0 | d_comparison = Comparisons::greaterOrEqual; |
1468 | 0 | } |
1469 | 0 | else if (comparison == "smaller") { |
1470 | 0 | d_comparison = Comparisons::smaller; |
1471 | 0 | } |
1472 | 0 | else if (comparison == "smallerOrEqual") { |
1473 | 0 | d_comparison = Comparisons::smallerOrEqual; |
1474 | 0 | } |
1475 | 0 | else { |
1476 | 0 | throw std::runtime_error("Unsupported comparison '" + comparison + "'"); |
1477 | 0 | } |
1478 | 0 | } |
1479 | | |
1480 | | bool matches(const DNSQuestion* dq) const override |
1481 | 0 | { |
1482 | 0 | const auto size = dq->getData().size(); |
1483 | |
|
1484 | 0 | switch (d_comparison) { |
1485 | 0 | case Comparisons::equal: |
1486 | 0 | return size == d_size; |
1487 | 0 | case Comparisons::greater: |
1488 | 0 | return size > d_size; |
1489 | 0 | case Comparisons::greaterOrEqual: |
1490 | 0 | return size >= d_size; |
1491 | 0 | case Comparisons::smaller: |
1492 | 0 | return size < d_size; |
1493 | 0 | case Comparisons::smallerOrEqual: |
1494 | 0 | return size <= d_size; |
1495 | 0 | default: |
1496 | 0 | return false; |
1497 | 0 | } |
1498 | 0 | } |
1499 | | |
1500 | | string toString() const override |
1501 | 0 | { |
1502 | 0 | static const std::array<const std::string, 5> comparisonStr{ |
1503 | 0 | "equal to", |
1504 | 0 | "greater than", |
1505 | 0 | "equal to or greater than", |
1506 | 0 | "smaller than", |
1507 | 0 | "equal to or smaller than"}; |
1508 | 0 | return "payload size is " + comparisonStr.at(static_cast<size_t>(d_comparison)) + " " + std::to_string(d_size); |
1509 | 0 | } |
1510 | | |
1511 | | private: |
1512 | | uint16_t d_size; |
1513 | | Comparisons d_comparison; |
1514 | | }; |
1515 | | |
1516 | | class IncomingProtocolRule : public DNSRule |
1517 | | { |
1518 | | public: |
1519 | | IncomingProtocolRule(const std::string& protocol) : |
1520 | 0 | d_protocol(protocol) |
1521 | 0 | { |
1522 | 0 | } |
1523 | | |
1524 | | bool matches(const DNSQuestion* dq) const override |
1525 | 0 | { |
1526 | 0 | return dq->getProtocol() == d_protocol; |
1527 | 0 | } |
1528 | | |
1529 | | string toString() const override |
1530 | 0 | { |
1531 | 0 | return "incoming protocol is " + d_protocol.toString(); |
1532 | 0 | } |
1533 | | |
1534 | | private: |
1535 | | dnsdist::Protocol d_protocol; |
1536 | | }; |
1537 | | |
1538 | | namespace dnsdist::selectors |
1539 | | { |
1540 | | std::shared_ptr<AndRule> getAndSelector(const std::vector<std::shared_ptr<DNSRule>>& rules); |
1541 | | std::shared_ptr<OrRule> getOrSelector(const std::vector<std::shared_ptr<DNSRule>>& rules); |
1542 | | std::shared_ptr<NotRule> getNotSelector(const std::shared_ptr<DNSRule>& rule); |
1543 | | std::shared_ptr<LuaRule> getLuaSelector(const dnsdist::selectors::LuaSelectorFunction& func); |
1544 | | std::shared_ptr<LuaFFIRule> getLuaFFISelector(const dnsdist::selectors::LuaSelectorFFIFunction& func); |
1545 | | std::shared_ptr<QNameRule> getQNameSelector(const DNSName& qname); |
1546 | | std::shared_ptr<QNameSetRule> getQNameSetSelector(const DNSNameSet& qnames); |
1547 | | std::shared_ptr<SuffixMatchNodeRule> getQNameSuffixSelector(const SuffixMatchNode& suffixes, bool quiet); |
1548 | | std::shared_ptr<QTypeRule> getQTypeSelector(const std::string& qtypeStr, uint16_t qtypeCode); |
1549 | | std::shared_ptr<QClassRule> getQClassSelector(const std::string& qclassStr, uint16_t qclassCode); |
1550 | | std::shared_ptr<NetmaskGroupRule> getNetmaskGroupSelector(const NetmaskGroup& nmg, bool source, bool quiet); |
1551 | | std::shared_ptr<KeyValueStoreLookupRule> getKeyValueStoreLookupSelector(const std::shared_ptr<KeyValueStore>& kvs, const std::shared_ptr<KeyValueLookupKey>& lookupKey); |
1552 | | std::shared_ptr<KeyValueStoreRangeLookupRule> getKeyValueStoreRangeLookupSelector(const std::shared_ptr<KeyValueStore>& kvs, const std::shared_ptr<KeyValueLookupKey>& lookupKey); |
1553 | | |
1554 | | #include "dnsdist-selectors-factory-generated.hh" |
1555 | | } |