3 `dnsdist` is a highly DNS-, DoS- and abuse-aware loadbalancer. Its goal in
4 life is to route traffic to the best server, delivering top performance
5 to legitimate users while shunting or blocking abusive traffic.
7 `dnsdist` is dynamic, in the sense that its configuration can be changed at
8 runtime, and that its statistics can be queried from a console-like
13 `dnsdist` depends on boost, Lua or LuaJIT and a pretty recent C++
14 compiler (g++ 4.8 or higher, clang 3.5 or higher). It can optionally use libsodium
15 for encrypted communications with its client, protobuf for remote logging and re2
16 for regular expression matching.
18 Should `dnsdist` be run on a system with systemd, it is highly recommended to have
19 the systemd header files (`libsystemd-dev` on debian and `systemd-devel` on CentOS)
20 installed to have `dnsdist` support systemd-notify.
22 To compile on CentOS 6 / RHEL6, use this script to install a working compiler:
25 wget -O /etc/yum.repos.d/slc6-devtoolset.repo http://linuxsoft.cern.ch/cern/devtoolset/slc6-devtoolset.repo
26 yum install devtoolset-2
27 scl enable devtoolset-2 bash
32 To build on OS X, `./configure LIBEDIT_LIBS='-L/usr/lib -ledit' LIBEDIT_CFLAGS=-I/usr/include/editline`
34 To build on OpenBSD, `./configure CXX=eg++ CPP=ecpp LIBEDIT_LIBS='-ledit -lcurses' LIBEDIT_CFLAGS=' '`
36 On other recent platforms, installing a Lua and the system C++ compiler should be enough.
38 `dnsdist` can drop privileges using the `--uid` and `--gid` commandline-switches
39 to ensure it does not run with root privileges after binding its listen-sockets.
40 It is highly recommended to create a system user and group for `dnsdist`. Note that
41 most packaged versions of `dnsdist` already create this user.
45 We build packages for `dnsdist` on our [repositories](https://repo.powerdns.com). In addition
46 `dnsdist` has been packaged for FreeBSD and can be found on https://freshports.org/dns/dnsdist
51 The absolute minimum configuration:
54 # dnsdist 2001:4860:4860::8888 8.8.8.8
57 This will listen on 0.0.0.0:53 and forward queries to the two listed IP
58 addresses, with a sensible load balancing policy.
60 Here is a more complete configuration:
63 $ cat /etc/dnsdist.conf
64 newServer({address="2001:4860:4860::8888", qps=1})
65 newServer({address="2001:4860:4860::8844", qps=1})
66 newServer({address="2620:0:ccc::2", qps=10})
67 newServer({address="2620:0:ccd::2", name="dns1", qps=10})
68 newServer("192.168.1.2")
69 setServerPolicy(firstAvailable) -- first server within its QPS limit
71 $ dnsdist --local=0.0.0.0:5200
72 Marking downstream [2001:4860:4860::8888]:53 as 'up'
73 Marking downstream [2001:4860:4860::8844]:53 as 'up'
74 Marking downstream [2620:0:ccc::2]:53 as 'up'
75 Marking downstream [2620:0:ccd::2]:53 as 'up'
76 Marking downstream 192.168.1.2:53 as 'up'
77 Listening on 0.0.0.0:5200
81 We can now send queries to port 5200, and get answers:
84 $ dig -t aaaa powerdns.com @127.0.0.1 -p 5200 +short
88 Note that `dnsdist` offered us a prompt above, and on it we can get some
93 # Address State Qps Qlim Ord Wt Queries Drops Drate Lat Pools
94 0 [2001:4860:4860::8888]:53 up 0.0 1 1 1 1 0 0.0 0.0
95 1 [2001:4860:4860::8844]:53 up 0.0 1 1 1 0 0 0.0 0.0
96 2 [2620:0:ccc::2]:53 up 0.0 10 1 1 0 0 0.0 0.0
97 3 [2620:0:ccd::2]:53 up 0.0 10 1 1 0 0 0.0 0.0
98 4 192.168.1.2:53 up 0.0 0 1 1 0 0 0.0 0.0
102 Here we also see our configuration. 5 downstream servers have been configured, of
103 which the first 4 have a QPS limit (of 1, 1, 10 and 10 queries per second,
104 respectively). The final server has no limit, which we can easily test:
107 $ for a in {0..1000}; do dig powerdns.com @127.0.0.1 -p 5200 +noall > /dev/null; done
109 # Address State Qps Qlim Ord Wt Queries Drops Drate Lat Pools
110 0 [2001:4860:4860::8888]:53 up 1.0 1 1 1 7 0 0.0 1.6
111 1 [2001:4860:4860::8844]:53 up 1.0 1 1 1 6 0 0.0 0.6
112 2 [2620:0:ccc::2]:53 up 10.3 10 1 1 64 0 0.0 2.4
113 3 [2620:0:ccd::2]:53 up 10.3 10 1 1 63 0 0.0 2.4
114 4 192.168.1.2:53 up 125.8 0 1 1 671 0 0.0 0.4
118 Note that the first 4 servers were all limited to near their configured QPS,
119 and that our final server was taking up most of the traffic. No queries were
120 dropped, and all servers remain up.
122 To force a server down, try:
125 > getServer(0):setDown()
127 # Address State Qps Qlim Ord Wt Queries Drops Drate Lat Pools
128 0 [2001:4860:4860::8888]:53 DOWN 0.0 1 1 1 8 0 0.0 0.0
132 The 'DOWN' in all caps means it was forced down. A lower case 'down'
133 would've meant that `dnsdist` itself had concluded the server was down.
134 Similarly, setUp() forces a server to be up, and setAuto() returns it to the
135 default availability-probing.
137 To change the QPS for a server:
139 > getServer(0):setQPS(1000)
142 By default, the availability of a downstream server is checked by regularly
143 sending an A query for "a.root-servers.net.". A different query type and target
144 can be specified by passing, respectively, the `checkType` and `checkName`
145 parameters to `newServer`. The default behavior is to consider any valid response
146 with a RCODE different from ServFail as valid. If the `mustResolve` parameter
147 of `newServer` is set to true, a response will only be considered valid if
148 its RCODE differs from NXDomain, ServFail and Refused.
149 The number of health check failures before a server is considered down is
150 configurable via the`maxCheckFailures` parameter, defaulting to 1.
151 The `CD` flag can be set on the query by setting `setCD` to true.
154 newServer({address="192.0.2.1", checkType="AAAA", checkName="a.root-servers.net.", mustResolve=true})
157 In order to provide the downstream server with the address of the real client,
158 or at least the one talking to `dnsdist`, the `useClientSubnet` parameter can be used
159 when declaring a new server. This parameter indicates whether an EDNS Client Subnet option
160 should be added to the request. If the incoming request already contains an EDNS Client Subnet value,
161 it will not be overriden unless `setECSOverride()` is set to true.
162 The default source prefix-length is 24 for IPv4 and 56 for IPv6, meaning that for a query
163 received from 192.0.2.42, the EDNS Client Subnet value sent to the backend will
164 be 192.0.2.0. This can be changed with:
166 > setECSSourcePrefixV4(24)
167 > setECSSourcePrefixV6(56)
170 In addition to the global settings, rules and Lua bindings can alter this behavior per query:
172 * calling `DisableECSAction()` or setting `dq.useECS` to false prevent the sending of the ECS option
173 * calling `ECSOverrideAction(bool)` or setting `dq.ecsOverride` will override the global `setECSOverride()` value
174 * calling `ECSPrefixLengthAction(v4, v6)` or setting `dq.ecsPrefixLength` will override the global
175 `setECSSourcePrefixV4()` and `setECSSourcePrefixV6()` values
177 In effect this means that for the EDNS Client Subnet option to be added to the request, `useClientSubnet`
178 should be set to true for the backend used (default to false) and ECS should not have been disabled by calling
179 `DisableECSAction()` or setting `dq.useECS` to false (default to true).
184 By default, a 2 seconds timeout is enforced on the TCP connection from the client,
185 meaning that a connection will be closed if the query can't be read in less than 2s
186 or if the answer can't be sent in less than 2s. This can be configured with:
188 > setTCPRecvTimeout(5)
189 > setTCPSendTimeout(5)
192 The same kind of timeouts is enforced on the TCP connections to the downstream servers.
193 The default value of 30s can be modified by passing the `tcpRecvTimeout` and `tcpSendTimeout`
194 parameters to `newServer`. If the TCP connection to a downstream server fails, `dnsdist`
195 will try to establish a new one up to `retries` times before giving up.
197 newServer({address="192.0.2.1", tcpRecvTimeout=10, tcpSendTimeout=10, retries=5})
203 In multi-homed setups, it can be useful to be able to select the source address or the outgoing
204 interface used by `dnsdist` to contact a downstream server.
205 This can be done by using the `source` parameter:
207 newServer({address="192.0.2.1", source="192.0.2.127"})
208 newServer({address="192.0.2.1", source="eth1"})
209 newServer({address="192.0.2.1", source="192.0.2.127@eth1"})
212 The supported values for `source` are:
214 * an IPv4 or IPv6 address, which must exist on the system
216 * an IPv4 or IPv6 address followed by '@' then an interface name
218 Specifying the interface name is only supported on system having IP_PKTINFO.
221 Configuration management
222 ------------------------
223 At startup, configuration is read from the command line and the
224 configuration file. The config can also be inspected and changed from the
225 console. Sadly, our architecture does not allow us to serialize the running
226 configuration for you. However, we do try to offer the next best thing:
229 `delta()` shows all commands entered that changed the configuration. So
230 adding a new downstream server with `newServer()` would show up, but
231 `showServers()` or even `delta()` itself would not.
233 It is suggested to study the output of `delta()` carefully before appending
234 it to your configuration file.
237 > setACL("192.0.2.0/24")
241 -- Wed Dec 23 2015 15:15:35 CET
242 setACL("192.0.2.0/24")
243 > addACL("127.0.0.1/8")
248 -- Wed Dec 23 2015 15:15:35 CET
249 setACL("192.0.2.0/24")
250 -- Wed Dec 23 2015 15:15:44 CET
251 addACL("127.0.0.1/8")
258 To visually interact with `dnsdist`, try adding:
260 webserver("127.0.0.1:8083", "supersecretpassword", "supersecretAPIkey")
263 to the configuration, and point your browser at http://127.0.0.1:8083 and
264 log in with any username, and that password. Enjoy!
266 By default, our web server sends some security-related headers:
268 * X-Content-Type-Options: nosniff
269 * X-Frame-Options: deny
270 * X-Permitted-Cross-Domain-Policies: none
271 * X-XSS-Protection: 1; mode=block
272 * Content-Security-Policy: default-src 'self'; style-src 'self' 'unsafe-inline'
274 You can override those headers, or add custom headers by using the last parameter to
275 `webserver()`. For example, to remove the `X-Frame-Options` header and add a
278 webserver("127.0.0.1:8080", "supersecret", "apikey", {["X-Frame-Options"]= "", ["X-Custom"]="custom"})
284 Now for some cool stuff. Let's say we know we're getting a whole bunch of
285 traffic for a domain used in DoS attacks, for example 'sh43354.cn'. We can
286 do two things with this kind of traffic. Either we block it outright, like
290 > addDomainBlock("sh43354.cn.")
293 Or we configure a server pool dedicated to receiving the nasty stuff:
296 > newServer({address="192.168.1.3", pool="abuse"})
297 > addPoolRule({"sh43353.cn.", "ezdns.it."}, "abuse")
300 The wonderful thing about this last solution is that it can also be used for
301 things where a domain might possibly be legit, but it is still causing load
302 on the system and slowing down the internet for everyone. With such an abuse
303 server, 'bad traffic' still gets a chance of an answer, but without
304 impacting the rest of the world (too much).
306 We can similarly add clients to the abuse server:
309 > addPoolRule({"192.168.12.0/24", "192.168.13.14"}, "abuse")
312 To define a pool that should receive only a QPS-limited amount of traffic, do:
315 > addQPSPoolRule("com.", 10000, "gtld-cluster")
318 Traffic exceeding the QPS limit will not match that rule, and subsequent
319 rules will apply normally.
321 Both `addDomainBlock` and `addPoolRule` end up the list of Rules
322 and Actions (for which see below).
324 Servers can be added or removed to pools with:
326 > getServer(7):addPool("abuse")
327 > getServer(4):rmPool("abuse")
333 Rules can be inspected with `showRules()`, and can be deleted with
334 `rmRule()`. Rules are evaluated in order, and this order can be changed
335 with `mvRule(from, to)` (see below for exact semantics).
337 Rules have selectors and actions. Current selectors are:
343 * QPS Limit per IP address or subnet
344 * QClass (QClassRule)
346 * RegexRule on query name
347 * RE2Rule on query name (optional)
349 * Packet requests DNSSEC processing
350 * Query received over UDP or TCP
351 * Opcode (OpcodeRule)
352 * Number of entries in a given section (RecordsCountRule)
353 * Number of entries of a specific type in a given section (RecordsTypeCountRule)
354 * Presence of trailing data (TrailingDataRule)
355 * Number of labels in the qname (QNameLabelsCountRule)
356 * Wire length of the qname (QNameWireLengthRule)
360 * `AndRule{rule1, rule2}`, which only matches if all of its subrules match
361 * `OrRule{rule1, rule2}`, which matches if at least one of its subrules match
362 * `NotRule(rule)`, which matches if its subrule does not match
367 * Route to a pool (PoolAction)
368 * Return with TC=1 (truncated, ie, instruction to retry with TCP)
369 * Force a ServFail, NotImp or Refused answer
370 * Send out a crafted response (NXDOMAIN or "real" data)
371 * Delay a response by n milliseconds (DelayAction), over UDP only
372 * Modify query to clear the RD or CD bit
373 * Add the source MAC address to the query (MacAddrAction, only supported on Linux)
374 * Skip the cache, if any
375 * Log query content to a remote server (RemoteLogAction)
376 * Alter the EDNS Client Subnet parameters (DisableECSAction, ECSOverrideAction, ECSPrefixLengthAction)
378 Current response actions are:
380 * Allow (AllowResponseAction)
381 * Delay a response by n milliseconds (DelayResponseAction), over UDP only
382 * Drop (DropResponseAction)
383 * Log response content to a remote server (RemoteLogResponseAction)
385 Rules can be added via:
387 * addAction(DNS rule, DNS Action)
389 * addDelay(DNS rule, delay in milliseconds)
390 * addDisableValidationRule(DNS rule)
391 * addDomainBlock(domain)
392 * addDomainSpoof(domain, IPv4[, IPv6]) or addDomainSpoof(domain, {IP, IP, IP..})
393 * addDomainCNAMESpoof(domain, CNAME)
394 * addLuaAction(DNS rule, lua function)
395 * addNoRecurseRule(DNS rule)
396 * addPoolRule(DNS rule, destination pool)
397 * addQPSLimit(DNS rule, qps limit)
398 * addQPSPoolRule(DNS rule, qps limit, destination pool)
400 Response rules can be added via:
402 * addResponseAction(DNS rule, DNS Response Action)
415 * a QNameLabelsCountRule
416 * a QNameWireLengthRule
422 * a RecordsTypeCountRule
423 * a SuffixMatchNodeRule
427 Some specific actions do not stop the processing when they match, contrary to all other actions:
439 A convenience function `makeRule()` is supplied which will make a NetmaskGroupRule for you or a SuffixMatchNodeRule
440 depending on how you call it. `makeRule("0.0.0.0/0")` will for example match all IPv4 traffic, `makeRule({"be","nl","lu"})` will
441 match all Benelux DNS traffic.
443 All the current rules can be removed at once with:
449 It is also possible to replace the current rules by a list of new ones in a
450 single operation with `setRules()`:
453 > setRules( { newRuleAction(TCPRule(), AllowAction()), newRuleAction(AllRule(), DropAction()) } )
459 More powerful things can be achieved by defining a function called
460 `blockFilter()` in the configuration file, which can decide to drop traffic
461 on any reason it wants. If you return 'true' from there, the query will get
464 A demo on how to do this and many other things can be found on
465 https://github.com/powerdns/pdns/blob/master/pdns/dnsdistconf.lua and
466 the exact definition of `blockFilter()` is at the end of this document.
468 ANY or whatever to TC
469 ---------------------
470 The `blockFilter()` also gets passed read/writable copy of the DNS Header,
472 If you invoke setQR(1) on that, `dnsdist` knows you turned the packet into
473 a response, and will send the answer directly to the original client.
475 If you also called setTC(1), this will tell the remote client to move to
476 TCP, and in this way you can implement ANY-to-TCP even for downstream
477 servers that lack this feature.
479 Note that calling `addAnyTCRule()` achieves the same thing, without
482 Rules for traffic exceeding QPS limits
483 --------------------------------------
484 Traffic that exceeds a QPS limit, in total or per IP (subnet) can be matched by a rule.
489 addDelay(MaxQPSIPRule(5, 32, 48), 100)
492 This measures traffic per IPv4 address and per /48 of IPv6, and if traffic for such
493 an address (range) exceeds 5 qps, it gets delayed by 100ms.
498 addAction(MaxQPSIPRule(5), NoRecurseAction())
501 This strips the Recursion Desired (RD) bit from any traffic per IPv4 or IPv6 /64
502 that exceeds 5 qps. This means any those traffic bins is allowed to make a recursor do 'work'
505 If this is not enough, try:
508 addAction(MaxQPSIPRule(5), DropAction())
510 addAction(MaxQPSIPRule(5), TCAction())
513 This will respectively drop traffic exceeding that 5 QPS limit per IP or range, or return it with TC=1, forcing
514 clients to fall back to TCP.
516 To turn this per IP or range limit into a global limit, use NotRule(MaxQPSRule(5000)) instead of MaxQPSIPRule.
520 This action sends off a copy of a UDP query to another server, and keeps statistics
521 on the responses received. Sample use:
524 > addAction(AllRule(), TeeAction("192.168.1.54"))
525 > getAction(0):printStats()
538 It is also possible to share a TeeAction between several rules. Statistics
539 will be combined in that case.
543 While we can pass every packet through the `blockFilter()` functions, it is also
544 possible to configure `dnsdist` to only hand off some packets for Lua inspection.
545 If you think Lua is too slow for your query load, or if you are doing heavy processing in Lua,
548 To select specific packets for Lua attention, use `addLuaAction(x, func)`,
549 where x is either a netmask, or a domain suffix, or a table of netmasks or a
550 table of domain suffixes. This is identical to how `addPoolRule()` selects.
552 The function should look like this:
555 if(dq.qtype==35) -- NAPTR
557 return DNSAction.Pool, "abuse" -- send to abuse pool
559 return DNSAction.None, "" -- no action
564 Valid return values for `LuaAction` functions are:
566 * DNSAction.Allow: let the query pass, skipping other rules
567 * DNSAction.Delay: delay the response for the specified milliseconds (UDP-only), continue to the next rule
568 * DNSAction.Drop: drop the query
569 * DNSAction.HeaderModify: indicate that the query has been turned into a response
570 * DNSAction.None: continue to the next rule
571 * DNSAction.Nxdomain: return a response with a NXDomain rcode
572 * DNSAction.Pool: use the specified pool to forward this query
573 * DNSAction.Spoof: spoof the response using the supplied IPv4 (A), IPv6 (AAAA) or string (CNAME) value
577 To provide DNSSEC service from a separate pool, try:
579 newServer({address="2001:888:2000:1d::2", pool="dnssec"})
580 newServer({address="2a01:4f8:110:4389::2", pool="dnssec"})
581 setDNSSECPool("dnssec")
585 This routes all queries with a DNSSEC OK (DO) or CD bit set to on to the "dnssec" pool.
586 The final `topRule()` command moves this rule to the top, so it gets evaluated first.
590 `RegexRule()` matches a regular expression on the query name, and it works like this:
593 addAction(RegexRule("[0-9]{5,}"), DelayAction(750)) -- milliseconds
594 addAction(RegexRule("[0-9]{4,}\\.cn$"), DropAction())
597 This delays any query for a domain name with 5 or more consecutive digits in it.
598 The second rule drops anything with more than 4 consecutive digits within a .CN domain.
600 Note that the query name is presented without a trailing dot to the regex.
601 The regex is applied case insensitively.
603 Alternatively, if compiled in, RE2Rule provides similar functionality, but against libre2.
605 Inspecting live traffic
606 -----------------------
607 This is still much in flux, but for now, try:
609 * `grepq(Netmask|DNS Name|100ms [, n])`: shows the last n queries and responses matching the specified client address or range (Netmask), or the specified DNS Name, or slower than 100ms
610 * `grepq({"::1", "powerdns.com", "100ms"} [, n])`: shows the last n queries and responses matching the specified client address AND range (Netmask) AND the specified DNS Name AND slower than 100ms
611 * `topBandwidth(top)`: show top-`top` clients that consume the most bandwidth over length of ringbuffer
612 * `topClients(n)`: show top-`n` clients sending the most queries over length of ringbuffer
613 * `topQueries(20)`: shows the top-20 queries
614 * `topQueries(20,2)`: shows the top-20 two-level domain queries (so `topQueries(20,1)` only shows TLDs)
615 * `topResponses(20, 2)`: top-20 servfail responses (use ,3 for NXDOMAIN)
616 * `topSlow([top][, limit][, labels])`: show `top` queries slower than `limit` milliseconds, grouped by last `labels` labels
620 > grepq("127.0.0.1/24")
621 Time Client Server ID Name Type Lat. TC RD AA Rcode
622 -11.9 127.0.0.1:52599 16127 nxdomain.powerdns.com. A RD Question
623 -11.7 127.0.0.1:52599 127.0.0.1:53 16127 nxdomain.powerdns.com. A 175.6 RD Non-Existent domain
624 > grepq("powerdns.com")
625 Time Client Server ID Name Type Lat. TC RD AA Rcode
626 -38.7 127.0.0.1:52599 16127 nxdomain.powerdns.com. A RD Question
627 -38.6 127.0.0.1:52599 127.0.0.1:53 16127 nxdomain.powerdns.com. A 175.6 RD Non-Existent domain
630 Live histogram of latency
631 -------------------------
633 > showResponseLatency()
634 Average response latency: 78.84 msec
638 0.40 **********************
646 102.40 **********************************************************************
647 204.80 *************************
653 Where : stands for 'half a star' and . for 'less than half a star, but
654 something was there'.
656 Per domain or subnet QPS limiting
657 ---------------------------------
658 If certain domains or source addresses are generating onerous amounts of
659 traffic, you can put ceilings on the amount of traffic you are willing to
663 > addQPSLimit("h4xorbooter.xyz.", 10)
664 > addQPSLimit({"130.161.0.0/16", "145.14.0.0/16"} , 20)
665 > addQPSLimit({"nl.", "be."}, 1)
667 # Matches Rule Action
668 0 0 h4xorbooter.xyz. qps limit to 10
669 1 0 130.161.0.0/16, 145.14.0.0/16 qps limit to 20
670 2 0 nl., be. qps limit to 1
673 To delete a limit (or a rule in general):
677 # Matches Rule Action
678 0 0 h4xorbooter.xyz. qps limit to 10
679 1 0 nl., be. qps limit to 1
684 Sometimes, runaway scripts will hammer your servers with back-to-back
685 queries. While it is possible to drop such packets, this may paradoxically
686 lead to more traffic.
688 An attractive middleground is to delay answers to such back-to-back queries,
689 causing a slowdown on the side of the source of the traffic.
693 > addDelay("yourdomain.in.ua.", 500)
694 > addDelay({"65.55.37.0/24"}, 500)
696 This will delay responses for questions to the mentioned domain, or coming
697 from the configured subnet, by half a second.
699 Like the QPSLimits and other rules, the delaying instructions can be
700 inspected or edited using showRule(), rmRule(), topRule(), mvRule() etc.
702 Dynamic load balancing
703 ----------------------
704 The default load balancing policy is called `leastOutstanding`, which means
705 we pick the server with the least queries 'in the air' (and within those,
706 the one with the lowest `order`, and within those, the one with the lowest latency).
708 Another policy, `firstAvailable`, picks the server with the lowest `order` that has not
709 exceeded its QPS limit. For now this is the only policy using the QPS limit.
711 A further policy, `wrandom` assigns queries randomly, but based on the
712 `weight` parameter passed to `newServer`. `whashed` is a similar weighted policy,
713 but assigns questions with identical hash to identical servers, allowing for
714 better cache concentration ('sticky queries').
716 If you don't like the default policies you can create your own, like this
721 function luaroundrobin(servers, dq)
723 return servers[1+(counter % #servers)]
726 setServerPolicyLua("luaroundrobin", luaroundrobin)
729 Incidentally, this is similar to setting: `setServerPolicy(roundrobin)`
730 which uses the C++ based roundrobin policy.
734 If the built in rules do not suffice to pick a server pool, full flexibility is available from Lua. For example:
737 newServer("192.168.1.2")
738 newServer({address="8.8.4.4", pool="numbered"})
740 function splitSetup(servers, dq)
741 if(string.match(dq.qname:toString(), "%d"))
743 print("numbered pool")
744 return leastOutstanding.policy(getPoolServers("numbered"), dq)
746 print("standard pool")
747 return leastOutstanding.policy(servers, dq)
751 setServerPolicyLua("splitsetup", splitSetup)
754 This will forward queries containing a number to the pool of "numbered"
755 servers, and will apply the default load balancing policy to all other
758 Dynamic Rule Generation
759 -----------------------
760 To set dynamic rules, based on recent traffic, define a function called `maintenance()` in Lua. It will
761 get called every second, and from this function you can set rules to block traffic based on statistics.
766 function maintenance()
767 addDynBlocks(exceedQRate(20, 10), "Exceeded query rate", 60)
771 This will dynamically block all hosts that exceeded 20 queries/s as measured
772 over the past 10 seconds, and the dynamic block will last for 60 seconds.
774 Dynamic blocks in force are displayed with `showDynBlocks()` and can be cleared
775 with `clearDynBlocks()`. Full set of `exceed` functions is listed in the table of
778 Dynamic blocks drop matched queries by default, but this behavior can be changed
779 with `setDynBlocksAction()`. For example, to send a REFUSED code instead of droppping
783 setDynBlocksAction(DNSAction.Refused)
788 First run on the command line, and generate a key:
793 setKey("sepuCcHcQnSAZgNbNPCCpDWbujZ5esZJmrt/wh6ldkQ=")
796 Now add this setKey line to `dnsdist.conf`, and also add:
799 controlSocket("0.0.0.0") -- or add portnumber too
802 Then start `dnsdist` as a daemon, and then connect to it:
809 Please note that, without libsodium support, 'makeKey()' will return
810 setKey("plaintext") and the communication between the client and the
811 server will not be encrypted.
813 Some versions of libedit, notably the CentOS 6 one, may require the following
814 addition to ~/.editrc in order to support searching through the history:
817 bind "^R" em-inc-search-prev
820 ACL, who can use dnsdist
821 ------------------------
822 For safety reasons, by default only private networks can use `dnsdist`, see below
823 how to query and change the ACL:
833 > addACL("130.161.0.0/16")
834 > setACL({"::/0"}) -- resets the list to this array
841 `dnsdist` implements a simple but effective packet cache, not enabled by default.
842 It is enabled per-pool, but the same cache can be shared between several pools.
843 The first step is to define a cache, then to assign that cache to the chosen pool,
844 the default one being represented by the empty string:
847 pc = newPacketCache(10000, 86400, 0, 60, 60)
848 getPool(""):setCache(pc)
851 The first parameter is the maximum number of entries stored in the cache, and is the
852 only one required. All the others parameters are optional and in seconds.
853 The second one is the maximum lifetime of an entry in the cache, the third one is
854 the minimum TTL an entry should have to be considered for insertion in the cache,
855 the fourth one is the TTL used for a Server Failure response. The last one is the
856 TTL that will be used when a stale cache entry is returned.
858 The `setStaleCacheEntriesTTL(n)` directive can be used to allow `dnsdist` to use
859 expired entries from the cache when no backend is available. Only entries that have
860 expired for less than `n` seconds will be used, and the returned TTL can be set
861 when creating a new cache with `newPacketCache()`.
863 A reference to the cache affected to a specific pool can be retrieved with:
866 getPool("poolname"):getCache()
872 getPool("poolname"):unsetCache()
875 Cache usage stats (hits, misses, deferred inserts and lookups, collisions)
876 can be displayed by using the `printStats()` method:
879 getPool("poolname"):getCache():printStats()
882 Expired cached entries can be removed from a cache using the `purgeExpired(n)`
883 method, which will remove expired entries from the cache until at least `n`
884 entries remain in the cache. For example, to remove all expired entries:
887 getPool("poolname"):getCache():purgeExpired(0)
890 Specific entries can also be removed using the `expungeByName(DNSName [, qtype=ANY])`
894 getPool("poolname"):getCache():expungeByName(newDNSName("powerdns.com"), dnsdist.A)
897 Finally, the `expunge(n)` method will remove all entries until at most `n`
898 entries remain in the cache:
901 getPool("poolname"):getCache():expunge(0)
907 First, a few words about `dnsdist` architecture:
909 * Each local bind has its own thread listening for incoming UDP queries
910 * and its own thread listening for incoming TCP connections,
911 dispatching them right away to a pool of threads
912 * Each backend has its own thread listening for UDP responses
913 * A maintenance thread calls the `maintenance()` Lua function every second
914 if any, and is responsible for cleaning the cache
915 * A health check thread checks the backends availability
916 * A control thread handles console connections
917 * A carbon thread exports statistics to carbon servers if needed
918 * One or more webserver threads handle queries to the internal webserver
920 The maximum number of threads in the TCP pool is controlled by the
921 `setMaxTCPClientThreads()` directive, and defaults to 10. This number can be
922 increased to handle a large number of simultaneous TCP connections.
923 If all the TCP threads are busy, new TCP connections are queued while
924 they wait to be picked up. The maximum number of queued connections
925 can be configured with `setMaxTCPQueuedConnections()` and defaults to 1000.
926 Any value larger than 0 will cause new connections to be dropped if there are
927 already too many queued.
929 When dispatching UDP queries to backend servers, `dnsdist` keeps track of at
930 most `n` outstanding queries for each backend. This number `n` can be tuned by
931 the `setMaxUDPOutstanding()` directive, defaulting to 10240, with a maximum
932 value of 65535. Large installations are advised to increase the default value
933 at the cost of a slightly increased memory usage.
935 Most of the query processing is done in C++ for maximum performance,
936 but some operations are executed in Lua for maximum flexibility:
938 * the `blockfilter()` function
939 * rules added by `addLuaAction()`
940 * server selection policies defined via `setServerPolicyLua()` or `newServerPolicy()`
942 While Lua is fast, its use should be restricted to the strict necessary in order
943 to achieve maximum performance, it might be worth considering using LuaJIT instead
944 of Lua. When Lua inspection is needed, the best course of action is to restrict
945 the queries sent to Lua inspection by using `addLuaAction()` instead of inspecting
946 all queries in the `blockfilter()` function.
948 `dnsdist` design choices mean that the processing of UDP queries is done by only
949 one thread per local bind. This is great to keep lock contention to a low level,
950 but might not be optimal for setups using a lot of processing power, caused for
951 example by a large number of complicated rules. To be able to use more CPU cores
952 for UDP queries processing, it is possible to use the `reuseport` parameter of
953 the `addLocal()` and `setLocal()` directives to be able to add several identical
954 local binds to `dnsdist`:
957 addLocal("192.0.2.1:53", true, true)
958 addLocal("192.0.2.1:53", true, true)
959 addLocal("192.0.2.1:53", true, true)
960 addLocal("192.0.2.1:53", true, true)
963 `dnsdist` will then add four identical local binds as if they were different IPs
964 or ports, start four threads to handle incoming queries and let the kernel load
965 balance those randomly to the threads, thus using four CPU cores for rules
966 processing. Note that this require SO_REUSEPORT support in the underlying
967 operating system (added for example in Linux 3.9).
968 Please also be aware that doing so will increase lock contention and might not
969 therefore scale linearly. This is especially true for Lua-intensive setups,
970 because Lua processing in `dnsdist` is serialized by an unique lock for all
973 Another possibility is to use the reuseport option to run several `dnsdist`
974 processes in parallel on the same host, thus avoiding the lock contention issue
975 at the cost of having to deal with the fact that the different processes will
976 not share informations, like statistics or DDoS offenders.
978 The UDP threads handling the responses from the backends do not use a lot of CPU,
979 but if needed it is also possible to add the same backend several times to the
980 `dnsdist` configuration to distribute the load over several responder threads.
983 newServer({address="192.0.2.127:53", name="Backend1"})
984 newServer({address="192.0.2.127:53", name="Backend2"})
985 newServer({address="192.0.2.127:53", name="Backend3"})
986 newServer({address="192.0.2.127:53", name="Backend4"})
990 Carbon/Graphite/Metronome
991 -------------------------
992 To emit metrics to Graphite, or any other software supporting the Carbon protocol, use:
994 carbonServer('ip-address-of-carbon-server', 'ourname', 30)
997 Where 'ourname' can be used to override your hostname, and '30' is the
998 reporting interval in seconds. The last two arguments can be omitted. The
999 latest version of [PowerDNS
1000 Metronome](https://github.com/ahupowerdns/metronome) comes with attractive
1001 graphs for `dnsdist` by default.
1005 When using `carbonServer`, it is also possible to send per-records statistics of
1006 the amount of queries by using `setQueryCount(true)`. With query counting enabled,
1007 `dnsdist` will increase a counter for every unique record or the behaviour you define
1008 in a custom Lua function by setting `setQueryCountFilter(func)`. This filter can decide
1009 whether to keep count on a query at all or rewrite for which query the counter will be
1011 An example of a `QueryCountFilter` would be:
1015 qname = dq.qname:toString()
1017 -- don't count PTRs at all
1018 if(qname:match('in%-addr.arpa$')) then
1022 -- count these queries as if they were queried without leading www.
1023 if(qname:match('^www.')) then
1024 qname = qname:gsub('^www.', '')
1027 -- count queries by default
1031 setQueryCountFilter(filter)
1034 Valid return values for `QueryCountFilter` functions are:
1036 * `true`: count the specified query
1037 * `false`: don't count the query
1039 Note that the query counters are buffered and flushed each time statistics are
1040 sent to the carbon server. The current content of the buffer can be inspected
1041 with `getQueryCounters()`. If you decide to enable query counting without
1042 `carbonServer`, make sure you implement clearing the log from `maintenance()`
1043 by issuing `clearQueryCounters()`.
1047 `dnsdist`, when compiled with --enable-dnscrypt, can be used as a DNSCrypt server,
1048 uncurving queries before forwarding them to downstream servers and curving responses back.
1049 To make `dnsdist` listen to incoming DNSCrypt queries on 127.0.0.1 port 8443,
1050 with a provider name of "2.providername", using a resolver certificate and associated key
1051 stored respectively in the `resolver.cert` and `resolver.key` files, the `addDnsCryptBind()`
1052 directive can be used:
1055 addDNSCryptBind("127.0.0.1:8443", "2.providername", "/path/to/resolver.cert", "/path/to/resolver.key")
1058 To generate the provider and resolver certificates and keys, you can simply do:
1061 > generateDNSCryptProviderKeys("/path/to/providerPublic.key", "/path/to/providerPrivate.key")
1062 Provider fingerprint is: E1D7:2108:9A59:BF8D:F101:16FA:ED5E:EA6A:9F6C:C78F:7F91:AF6B:027E:62F4:69C3:B1AA
1063 > generateDNSCryptCertificate("/path/to/providerPrivate.key", "/path/to/resolver.cert", "/path/to/resolver.key", serial, validFrom, validUntil)
1066 Ideally, the certificates and keys should be generated on an offline dedicated hardware and not on the resolver.
1067 The resolver key should be regularly rotated and should never touch persistent storage, being stored in a tmpfs
1068 with no swap configured.
1070 You can display the currently configured DNSCrypt binds with:
1072 > showDNSCryptBinds()
1073 # Address Provider Name Serial Validity P. Serial P. Validity
1074 0 127.0.0.1:8443 2.name 14 2016-04-10 08:14:15 0 -
1077 If you forgot to write down the provider fingerprint value after generating the provider keys, you can use `printDNSCryptProviderFingerprint()` to retrieve it later:
1079 > printDNSCryptProviderFingerprint("/path/to/providerPublic.key")
1080 Provider fingerprint is: E1D7:2108:9A59:BF8D:F101:16FA:ED5E:EA6A:9F6C:C78F:7F91:AF6B:027E:62F4:69C3:B1AA
1083 AXFR, IXFR and NOTIFY
1084 ---------------------
1085 When `dnsdist` is deployed in front of a master authoritative server, it might
1086 receive AXFR or IXFR queries destined to this master. There are two issues
1087 that can arise in this kind of setup:
1089 * If the master is part of a pool of servers, the first SOA query can be directed
1090 by `dnsdist` to a different server than the following AXFR/IXFR one, which might
1091 fail if the servers are not perfectly synchronised.
1092 * If the master only allows AXFR/IXFR based on the source address of the requestor,
1093 it might be confused by the fact that the source address will be the one from
1094 the `dnsdist` server.
1096 The first issue can be solved by routing SOA, AXFR and IXFR requests explicitely
1100 > newServer({address="192.168.1.2", name="master", pool={"master", "otherpool"}})
1101 > addAction(OrRule({QTypeRule(dnsdist.SOA), QTypeRule(dnsdist.AXFR), QTypeRule(dnsdist.IXFR)}), PoolAction("master"))
1104 The second one might require allowing AXFR/IXFR from the `dnsdist` source address
1105 and moving the source address check on `dnsdist`'s side:
1108 > addAction(AndRule({OrRule({QTypeRule(dnsdist.AXFR), QTypeRule(dnsdist.IXFR)}), NotRule(makeRule("192.168.1.0/24"))}), RCodeAction(dnsdist.REFUSED))
1111 When `dnsdist` is deployed in front of slaves, however, an issue might arise with NOTIFY
1112 queries, because the slave will receive a notification coming from the `dnsdist` address,
1113 and not the master's one. One way to fix this issue is to allow NOTIFY from the `dnsdist`
1114 address on the slave side (for example with PowerDNS's `trusted-notification-proxy`) and
1115 move the address check on `dnsdist`'s side:
1118 > addAction(AndRule({OpcodeRule(DNSOpcode.Notify), NotRule(makeRule("192.168.1.0/24"))}), RCodeAction(dnsdist.REFUSED))
1121 eBPF Socket Filtering
1122 ---------------------
1123 `dnsdist` can use eBPF socket filtering on recent Linux kernels (4.1+) built with eBPF
1124 support (`CONFIG_BPF`, `CONFIG_BPF_SYSCALL`, ideally `CONFIG_BPF_JIT`).
1125 This feature might require an increase of the memory limit associated to a socket, via
1126 the `sysctl` setting `net.core.optmem_max`. When attaching an eBPF program to a socket,
1127 the size of the program is checked against this limit, and the default value might not be
1128 enough. Large map sizes might also require an increase of `RLIMIT_MEMLOCK`.
1130 This feature allows `dnsdist` to ask the kernel to discard incoming packets in kernel-space
1131 instead of them being copied to userspace just to be dropped, thus being a lot of faster.
1133 The BPF filter can be used to block incoming queries manually:
1136 > bpf = newBPFFilter(1024, 1024, 1024)
1137 > bpf:attachToAllBinds()
1138 > bpf:block(newCA("2001:DB8::42"))
1139 > bpf:blockQName(newDNSName("evildomain.com"), 255)
1142 evildomain.com. 255: 0
1143 > bpf:unblock(newCA("2001:DB8::42"))
1144 > bpf:unblockQName(newDNSName("evildomain.com"), 255)
1149 The `blockQName()` method can be used to block queries based on the exact qname supplied,
1150 in a case-insensitive way, and an optional qtype. Using the 255 (ANY) qtype will block all
1151 queries for the qname, regardless of the qtype.
1152 Contrary to source address filtering, qname filtering only works over UDP. TCP qname
1153 filtering can be done the usual way:
1156 > addAction(AndRule({TCPRule(true), makeRule("evildomain.com")}), DropAction())
1159 The `attachToAllBinds()` method attaches the filter to every existing bind at runtime,
1160 but it's also possible to define a default BPF filter at configuration time, so
1161 it's automatically attached to every bind:
1164 bpf = newBPFFilter(1024, 1024, 1024)
1165 setDefaultBPFFilter(bpf)
1168 Finally, it's also possible to attach it to specific binds at runtime:
1171 > bpf = newBPFFilter(1024, 1024, 1024)
1173 # Address Protocol Queries
1177 > bd:attachFilter(bpf)
1180 `dnsdist` also supports adding dynamic, expiring blocks to a BPF filter:
1183 bpf = newBPFFilter(1024, 1024, 1024)
1184 setDefaultBPFFilter(bpf)
1185 dbpf = newDynBPFFilter(bpf)
1186 function maintenance()
1187 addBPFFilterDynBlocks(exceedQRate(20, 10), dbpf, 60)
1192 This will dynamically block all hosts that exceeded 20 queries/s as measured
1193 over the past 10 seconds, and the dynamic block will last for 60 seconds.
1195 This feature has been successfully tested on Arch Linux, Arch Linux ARM,
1196 Fedora Core 23 and Ubuntu Xenial.
1198 All functions and types
1199 -----------------------
1200 Within `dnsdist` several core object types exist:
1202 * Server: generated with newServer, represents a downstream server
1203 * ComboAddress: represents an IP address and port
1204 * DNSName: represents a domain name
1205 * NetmaskGroup: represents a group of netmasks
1206 * QPSLimiter: implements a QPS-based filter
1207 * SuffixMatchNode: represents a group of domain suffixes for rapid testing of membership
1208 * DNSHeader: represents the header of a DNS packet
1210 The existence of most of these objects can mostly be ignored, unless you
1211 plan to write your own hooks and policies, but it helps to understand an
1215 > getServer(0).order=12 -- set order of server 0 to 12
1216 > getServer(0):addPool("abuse") -- add this server to the abuse pool
1218 The '.' means 'order' is a data member, while the ':' meand addPool is a member function.
1220 Here are all functions:
1223 * `shutdown()`: shut down `dnsdist`
1224 * quit or ^D: exit the console
1225 * `webserver(address:port, password [, apiKey [, customHeaders ]])`: launch a webserver with stats on that address with that password
1227 * `addACL(netmask)`: add to the ACL set who can use this server
1228 * `setACL({netmask, netmask})`: replace the ACL set with these netmasks. Use `setACL({})` to reset the list, meaning no one can use us
1229 * `showACL()`: show our ACL set
1230 * ClientState related:
1231 * function `showBinds()`: list every local bind
1232 * function `getBind(n)`: return the corresponding `ClientState` object
1233 * member `attachFilter(BPFFilter)`: attach a BPF Filter to this bind
1234 * member `detachFilter()`: detach the BPF Filter attached to this bind, if any
1235 * member `toString()`: print the address this bind listens to
1237 * `addLocal(netmask, [true], [false], [TCP Fast Open queue size])`: add to addresses we listen on. Second optional parameter sets TCP or not. Third optional parameter sets SO_REUSEPORT when available. Last parameter sets the TCP Fast Open queue size, enabling TCP Fast Open when available and the value is larger than 0.
1238 * `setLocal(netmask, [true], [false], [TCP Fast Open queue size])`: reset list of addresses we listen on to this address. Second optional parameter sets TCP or not. Third optional parameter sets SO_REUSEPORT when available. Last parameter sets the TCP Fast Open queue size, enabling TCP Fast Open when available and the value is larger than 0.
1240 * `addDomainBlock(domain)`: block queries within this domain
1241 * Carbon/Graphite/Metronome statistics related:
1242 * `carbonServer(serverIP, [ourname], [interval])`: report statistics to serverIP using our hostname, or 'ourname' if provided, every 'interval' seconds
1243 * Query counting related:
1244 * `clearQueryCounters()`: clears the query counter buffer.
1245 * `getQueryCounters([max])`: show current buffer of query counters, limited by `max` if provided.
1246 * `setQueryCount(bool)`: set whether queries should be counted.
1247 * `setQueryCountFilter(func)`: filter queries that would be counted, where `func` is a function with parameter `dq` which decides whether a query should and how it should be counted.
1248 * Control socket related:
1249 * `makeKey()`: generate a new server access key, emit configuration line ready for pasting
1250 * `setKey(key)`: set access key to that key.
1251 * `testCrypto()`: test of the crypto all works
1252 * `controlSocket(addr)`: open a control socket on this address / connect to this address in client mode
1253 * Diagnostics and statistics
1254 * `dumpStats()`: print all statistics we gather
1255 * `getStatisticsCounters()`: return the statistics counters as a Lua table
1256 * `grepq(Netmask|DNS Name|100ms [, n])`: shows the last n queries and responses matching the specified client address or range (Netmask), or the specified DNS Name, or slower than 100ms
1257 * `grepq({"::1", "powerdns.com", "100ms"} [, n])`: shows the last n queries and responses matching the specified client address AND range (Netmask) AND the specified DNS Name AND slower than 100ms
1258 * `topQueries(n[, labels])`: show top 'n' queries, as grouped when optionally cut down to 'labels' labels
1259 * `topResponses(n, kind[, labels])`: show top 'n' responses with RCODE=kind (0=NO Error, 2=ServFail, 3=ServFail), as grouped when optionally cut down to 'labels' labels
1260 * `topSlow([top][, limit][, labels])`: show `top` queries slower than `limit` milliseconds, grouped by last `labels` labels
1261 * `topBandwidth(top)`: show top-`top` clients that consume the most bandwidth over length of ringbuffer
1262 * `topClients(n)`: show top-`n` clients sending the most queries over length of ringbuffer
1263 * `showResponseLatency()`: show a plot of the response time latency distribution
1264 * `showTCPStats()`: show some statistics regarding TCP
1265 * `showVersion()`: show the current version of dnsdist
1267 * `infolog(string)`: log at level info
1268 * `warnlog(string)`: log at level warning
1269 * `errlog(string)`: log at level error
1270 * `setVerboseHealthChecks(bool)`: set whether health check errors will be logged
1272 * `newServer("ip:port")`: instantiate a new downstream server with default settings
1273 * `newServer({address="ip:port", qps=1000, order=1, weight=10, pool="abuse", retries=5, tcpSendTimeout=30, tcpRecvTimeout=30, checkName="a.root-servers.net.", checkType="A", setCD=false, maxCheckFailures=1, mustResolve=false, useClientSubnet=true, source="address|interface name|address@interface"})`:
1274 instantiate a server with additional parameters
1275 * `showServers()`: output all servers
1276 * `getServer(n)`: returns server with index n
1277 * `getServers()`: returns a table with all defined servers
1278 * `rmServer(n)`: remove server with index n
1279 * `rmServer(server)`: remove this server object
1280 * Server member functions:
1281 * `addPool(pool)`: add this server to that pool
1282 * `getName()`: return the server name if any
1283 * `getNameWithAddr()`: return a string containing the server name if any plus the server address and port
1284 * `getOutstanding()`: this *returns* the number of outstanding queries (doesn't print it!)
1285 * `rmPool(pool)`: remove server from that pool
1286 * `setQPS(n)`: set the QPS setting to n
1287 * `setAuto()`: set this server to automatic availability testing
1288 * `setDown()`: force this server to be down
1289 * `setUp()`: force this server to be UP
1290 * `isUp()`: if this server is available
1291 * Server member data:
1292 * `upStatus`: if `dnsdist` considers this server available (overridden by `setDown()` and `setUp()`)
1293 * `name`: name of the server
1294 * `order`: order of this server in order-based server selection policies
1295 * `weight`: weight of this server in weighted server selection policies
1297 * `AllRule()`: matches all traffic
1298 * `AndRule()`: matches if all sub-rules matches
1299 * `DNSSECRule()`: matches queries with the DO flag set
1300 * `MaxQPSIPRule(qps, v4Mask=32, v6Mask=64)`: matches traffic exceeding the qps limit per subnet
1301 * `MaxQPSRule(qps)`: matches traffic **not** exceeding this qps limit
1302 * `NetmaskGroupRule(nmg, [src-bool])`: matches traffic from the specified network range. Pass `false` as second parameter to match NetmaskGroup against destination address instead of source address
1303 * `NotRule()`: matches if the sub-rule does not match
1304 * `OrRule()`: matches if at least one of the sub-rules matches
1305 * `OpcodeRule()`: matches queries with the specified opcode
1306 * `QClassRule(qclass)`: matches queries with the specified qclass (numeric)
1307 * `QNameLabelsCountRule(min, max)`: matches if the qname has less than `min` or more than `max` labels
1308 * `QNameWireLengthRule(min, max)`: matches if the qname's length on the wire is less than `min` or more than `max` bytes
1309 * `QTypeRule(qtype)`: matches queries with the specified qtype
1310 * `RCodeRule(rcode)`: matches queries or responses the specified rcode
1311 * `RegexRule(regex)`: matches the query name against the supplied regex
1312 * `RecordsCountRule(section, minCount, maxCount)`: matches if there is at least `minCount` and at most `maxCount` records in the `section` section
1313 * `RecordsTypeCountRule(section, type, minCount, maxCount)`: matches if there is at least `minCount` and at most `maxCount` records of type `type` in the `section` section
1314 * `RE2Rule(regex)`: matches the query name against the supplied regex using the RE2 engine
1315 * `SuffixMatchNodeRule(smn, [quiet-bool])`: matches based on a group of domain suffixes for rapid testing of membership. Pass `true` as second parameter to prevent listing of all domains matched.
1316 * `TCPRule(tcp)`: matches question received over TCP if `tcp` is true, over UDP otherwise
1317 * `TrailingDataRule()`: matches if the query has trailing data
1318 * Rule management related:
1319 * `clearRules()`: remove all current rules
1320 * `getAction(num)`: returns the Action associate with rule 'num'.
1321 * `mvResponseRule(from, to)`: move response rule 'from' to a position where it is in front of 'to'. 'to' can be one larger than the largest rule,
1322 in which case the rule will be moved to the last position.
1323 * `mvRule(from, to)`: move rule 'from' to a position where it is in front of 'to'. 'to' can be one larger than the largest rule,
1324 in which case the rule will be moved to the last position.
1325 * `newRuleAction(DNS Rule, DNS Action)`: return a pair of DNS Rule and DNS Action, to be used with `setRules()`
1326 * `rmResponseRule(n)`: remove response rule n
1327 * `rmRule(n)`: remove rule n
1328 * `setRules(list)`: replace the current rules with the supplied list of pairs of DNS Rules and DNS Actions (see `newRuleAction()`)
1329 * `showResponseRules()`: show all defined response rules
1330 * `showRules()`: show all defined rules
1331 * `topResponseRule()`: move the last response rule to the first position
1332 * `topRule()`: move the last rule to the first position
1333 * Built-in Actions for Rules:
1334 * `AllowAction()`: let these packets go through
1335 * `AllowResponseAction()`: let these packets go through
1336 * `DelayAction(milliseconds)`: delay the response by the specified amount of milliseconds (UDP-only)
1337 * `DelayResponseAction(milliseconds)`: delay the response by the specified amount of milliseconds (UDP-only)
1338 * `DisableECSAction()`: disable the sending of ECS to the backend
1339 * `DisableValidationAction()`: set the CD bit in the question, let it go through
1340 * `DropAction()`: drop these packets
1341 * `DropResponseAction()`: drop these packets
1342 * `ECSOverrideAction(bool)`: whether an existing ECS value should be overriden (true) or not (false)
1343 * `ECSPrefixLengthAction(v4, v6)`: set the ECS prefix length
1344 * `LogAction([filename], [binary], [append], [buffered])`: Log a line for each query, to the specified file if any, to the console (require verbose) otherwise. When logging to a file, the `binary` optional parameter specifies whether we log in binary form (default) or in textual form, the `append` optional parameter specifies whether we open the file for appending or truncate each time (default), and the `buffered` optional parameter specifies whether writes to the file are buffered (default) or not.
1345 * `MacAddrAction(option code)`: add the source MAC address to the query as EDNS0 option `option code`. This action is currently only supported on Linux
1346 * `NoRecurseAction()`: strip RD bit from the question, let it go through
1347 * `PoolAction(poolname)`: set the packet into the specified pool
1348 * `QPSPoolAction(maxqps, poolname)`: set the packet into the specified pool only if it **does not** exceed the specified QPS limits, letting the subsequent rules apply otherwise
1349 * `QPSAction(rule, maxqps)`: drop these packets if the QPS limits are exceeded
1350 * `RCodeAction(rcode)`: reply immediatly by turning the query into a response with the specified rcode
1351 * `RemoteLogAction(RemoteLogger [, alterFunction])`: send the content of this query to a remote logger via Protocol Buffer. `alterFunction` is a callback, receiving a DNSQuestion and a DNSDistProtoBufMessage, that can be used to modify the Protocol Buffer content, for example for anonymization purposes
1352 * `RemoteLogResponseAction(RemoteLogger [,alterFunction])`: send the content of this response to a remote logger via Protocol Buffer. `alterFunction` is the same callback than the one in `RemoteLogAction`
1353 * `SkipCacheAction()`: don't lookup the cache for this query, don't store the answer
1354 * `SpoofAction(ip[, ip])` or `SpoofAction({ip, ip, ..}): forge a response with the specified IPv4 (for an A query) or IPv6 (for an AAAA). If you specify multiple addresses, all that match the query type (A, AAAA or ANY) will get spoofed in
1355 * `SpoofCNAMEAction(cname)`: forge a response with the specified CNAME value
1356 * `TCAction()`: create answer to query with TC and RD bits set, to move to TCP
1357 * `TeeAction(remote[, addECS])`: send copy of query to remote, keep stats on responses. If `addECS` is set to `true`, EDNS Client Subnet information will be added to the query
1358 * Specialist rule generators
1359 * `addAnyTCRule()`: generate TC=1 answers to ANY queries received over UDP, moving them to TCP
1360 * `addDomainSpoof(domain, ip[, ip6])` or `addDomainSpoof(domain, {IP, IP, IP..})`: generate answers for A/AAAA/ANY queries using the ip parameters
1361 * `addDomainCNAMESpoof(domain, cname)`: generate CNAME answers for queries using the specified value
1362 * `addDisableValidationRule(domain)`: set the CD flags to 1 for all queries matching the specified domain
1363 * `addNoRecurseRule(domain)`: clear the RD flag for all queries matching the specified domain
1364 * `setDNSSECPool(pool)`: move queries requesting DNSSEC processing to this pool
1365 * Policy member data:
1366 * `name`: the policy name
1367 * `policy`: the policy function
1369 * `addPoolRule(domain, pool)`: send queries to this domain to that pool
1370 * `addPoolRule({domain, domain}, pool)`: send queries to these domains to that pool
1371 * `addPoolRule(netmask, pool)`: send queries to this netmask to that pool
1372 * `addPoolRule({netmask, netmask}, pool)`: send queries to these netmasks to that pool
1373 * `addQPSPoolRule(x, limit, pool)`: like `addPoolRule`, but only select at most 'limit' queries/s for this pool, letting the subsequent rules apply otherwise
1374 * `getPool(poolname)`: return the ServerPool named `poolname`
1375 * `getPoolServers(pool)`: return servers part of this pool
1376 * `showPools()`: list the current server pools
1377 * Lua Action related:
1378 * `addLuaAction(x, func)`: where 'x' is all the combinations from `addPoolRule`, and func is a
1379 function with the parameter `dq`, which returns an action to be taken on this packet.
1380 Good for rare packets but where you want to do a lot of processing.
1381 * Server selection policy related:
1382 * `setServerPolicy(policy)`: set server selection policy to that policy
1383 * `setServerPolicyLua(name, function)`: set server selection policy to one named 'name' and provided by 'function'
1384 * `showServerPolicy()`: show name of currently operational server selection policy
1385 * `newServerPolicy(name, function)`: create a policy object from a Lua function
1386 * Available policies:
1387 * `firstAvailable`: Pick first server that has not exceeded its QPS limit, ordered by the server 'order' parameter
1388 * `whashed`: Weighted hashed ('sticky') distribution over available servers, based on the server 'weight' parameter
1389 * `wrandom`: Weighted random over available servers, based on the server 'weight' parameter
1390 * `roundrobin`: Simple round robin over available servers
1391 * `leastOutstanding`: Send traffic to downstream server with least outstanding queries, with the lowest 'order', and within that the lowest recent latency
1393 * `addQPSLimit(domain, n)`: limit queries within that domain to n per second
1394 * `addQPSLimit({domain, domain}, n)`: limit queries within those domains (together) to n per second
1395 * `addQPSLimit(netmask, n)`: limit queries within that netmask to n per second
1396 * `addQPSLimit({netmask, netmask}, n)`: limit queries within those netmasks (together) to n per second
1398 * `addDelay(domain, n)`: delay answers within that domain by n milliseconds
1399 * `addDelay({domain, domain}, n)`: delay answers within those domains (together) by n milliseconds
1400 * `addDelay(netmask, n)`: delay answers within that netmask by n milliseconds
1401 * `addDelay({netmask, netmask}, n)`: delay answers within those netmasks (together) by n milliseconds
1402 * Answer changing functions:
1403 * `truncateTC(bool)`: if set (default) truncate TC=1 answers so they are actually empty. Fixes an issue for PowerDNS Authoritative Server 2.9.22.
1404 * `fixupCase(bool)`: if set (default to no), rewrite the first qname of the question part of the answer to match the one from the query. It is only useful when you have a downstream server that messes up the case of the question qname in the answer
1405 * Dynamic Block related:
1406 * `maintenance()`: called every second by dnsdist if defined, call functions below from it
1407 * `clearDynBlocks()`: clear all dynamic blocks
1408 * `showDynBlocks()`: show dynamic blocks in force
1409 * `addDynBlocks(addresses, message[, seconds])`: block the set of addresses with message `msg`, for `seconds` seconds (10 by default)
1410 * `setDynBlocksAction(DNSAction)`: set which action is performed when a query is blocked. Only DNSAction.Drop (the default) and DNSAction.Refused are supported
1411 * `addBPFFilterDynBlocks(addresses, DynBPFFilter[, seconds])`: block the set of addresses using the supplied BPF Filter, for `seconds` seconds (10 by default)
1412 * `exceedServFails(rate, seconds)`: get set of addresses that exceed `rate` servails/s over `seconds` seconds
1413 * `exceedNXDOMAINs(rate, seconds)`: get set of addresses that exceed `rate` NXDOMAIN/s over `seconds` seconds
1414 * `exceedRespByterate(rate, seconds)`: get set of addresses that exeeded `rate` bytes/s answers over `seconds` seconds
1415 * `exceedQRate(rate, seconds)`: get set of address that exceed `rate` queries/s over `seconds` seconds
1416 * `exceedQTypeRate(type, rate, seconds)`: get set of address that exceed `rate` queries/s for queries of type `type` over `seconds` seconds
1417 * ServerPool related:
1418 * `getCache()`: return the current packet cache, if any
1419 * `setCache(PacketCache)`: set the cache for this pool
1420 * `unsetCache()`: remove the packet cache from this pool
1421 * PacketCache related:
1422 * `expunge(n)`: remove entries from the cache, leaving at most `n` entries
1423 * `expungeByName(DNSName [, qtype=ANY])`: remove entries matching the supplied DNSName and type from the cache
1424 * `isFull()`: return true if the cache has reached the maximum number of entries
1425 * `newPacketCache(maxEntries[, maxTTL=86400, minTTL=0, servFailTTL=60, stateTTL=60])`: return a new PacketCache
1426 * `printStats()`: print the cache stats (hits, misses, deferred lookups and deferred inserts)
1427 * `purgeExpired(n)`: remove expired entries from the cache until there is at most `n` entries remaining in the cache
1428 * `toString()`: return the number of entries in the Packet Cache, and the maximum number of entries
1429 * Advanced functions for writing your own policies and hooks
1430 * ComboAddress related:
1431 * `newCA(address)`: return a new ComboAddress
1432 * `getPort()`: return the port number
1433 * `isIPv4()`: return true if the address is an IPv4, false otherwise
1434 * `isIPv6()`: return true if the address is an IPv6, false otherwise
1435 * `isMappedIPv4()`: return true if the address is an IPv4 mapped into an IPv6, false otherwise
1436 * `mapToIPv4()`: convert an IPv4 address mapped in a v6 one into an IPv4
1437 * `tostring()`: return in human-friendly format
1438 * `toString()`: alias for `tostring()`
1439 * `tostringWithPort()`: return in human-friendly format, with port number
1440 * `toStringWithPort()`: alias for `tostringWithPort()`
1441 * `truncate(bits)`: truncate the address to the specified number of bits
1443 * `newDNSName(name)`: make a DNSName based on this .-terminated name
1444 * member `countLabels()`: return the number of labels
1445 * member `isPartOf(dnsname)`: is this dnsname part of that dnsname
1446 * member `tostring()`: return as a human friendly . terminated string
1447 * member `toString()`: alias for `tostring()`
1448 * member `wirelength()`: return the length on the wire
1449 * DNSQuestion related:
1450 * member `dh`: DNSHeader
1451 * member `ecsOverride`: whether an existing ECS value should be overriden (settable)
1452 * member `ecsPrefixLength`: the ECS prefix length to use (settable)
1453 * member `len`: the question length
1454 * member `localaddr`: ComboAddress of the local bind this question was received on
1455 * member `opcode`: the question opcode
1456 * member `qname`: DNSName of this question
1457 * member `qclass`: QClass (as an unsigned integer) of this question
1458 * member `qtype`: QType (as an unsigned integer) of this question
1459 * member `remoteaddr`: ComboAddress of the remote client
1460 * member `rcode`: RCode of this question
1461 * member `size`: the total size of the buffer starting at `dh`
1462 * member `skipCache`: whether to skip cache lookup / storing the answer for this question (settable)
1463 * member `tcp`: whether this question was received over a TCP socket
1464 * member `useECS`: whether to send ECS to the backend (settable)
1466 * member `getRD()`: get recursion desired flag
1467 * member `setRD(bool)`: set recursion desired flag
1468 * member `setTC(bool)`: set truncation flag (TC)
1469 * member `setQR(bool)`: set Query Response flag (setQR(true) indicates an *answer* packet)
1470 * member `getCD()`: get checking disabled flag
1471 * member `setCD(bool)`: set checking disabled flag
1472 * NetmaskGroup related
1473 * function `newNMG()`: returns a NetmaskGroup
1474 * member `addMask(mask)`: adds `mask` to the NetmaskGroup
1475 * member `match(ComboAddress)`: checks if ComboAddress is matched by this NetmaskGroup
1476 * member `clear()`: clears the NetmaskGroup
1477 * member `size()`: returns number of netmasks in this NetmaskGroup
1478 * QPSLimiter related:
1479 * `newQPSLimiter(rate, burst)`: configure a QPS limiter with that rate and that burst capacity
1480 * member `check()`: check if this QPSLimiter has a token for us. If yes, you must use it.
1481 * SuffixMatchNode related:
1482 * `newSuffixMatchNode()`: returns a new SuffixMatchNode
1483 * member `check(DNSName)`: returns true if DNSName is matched by this group
1484 * member `add(DNSName)`: add this DNSName to the node
1486 * `setTCPRecvTimeout(n)`: set the read timeout on TCP connections from the client, in seconds
1487 * `setTCPSendTimeout(n)`: set the write timeout on TCP connections from the client, in seconds
1488 * `setMaxTCPClientThreads(n)`: set the maximum of TCP client threads, handling TCP connections
1489 * `setMaxTCPQueuedConnections(n)`: set the maximum number of TCP connections queued (waiting to be picked up by a client thread), defaults to 1000. 0 means unlimited
1490 * `setMaxUDPOutstanding(n)`: set the maximum number of outstanding UDP queries to a given backend server. This can only be set at configuration time and defaults to 10240
1491 * `setCacheCleaningDelay(n)`: set the interval in seconds between two runs of the cache cleaning algorithm, removing expired entries
1492 * `setStaleCacheEntriesTTL(n)`: allows using cache entries expired for at most `n` seconds when no backend available to answer for a query
1494 * `addDNSCryptBind("127.0.0.1:8443", "provider name", "/path/to/resolver.cert", "/path/to/resolver.key", [false], [TCP Fast Open queue size]):` listen to incoming DNSCrypt queries on 127.0.0.1 port 8443, with a provider name of "provider name", using a resolver certificate and associated key stored respectively in the `resolver.cert` and `resolver.key` files. The fifth optional parameter sets SO_REUSEPORT when available. The last parameter sets the TCP Fast Open queue size, enabling TCP Fast Open when available and the value is larger than 0.
1495 * `generateDNSCryptProviderKeys("/path/to/providerPublic.key", "/path/to/providerPrivate.key"):` generate a new provider keypair
1496 * `generateDNSCryptCertificate("/path/to/providerPrivate.key", "/path/to/resolver.cert", "/path/to/resolver.key", serial, validFrom, validUntil):` generate a new resolver private key and related certificate, valid from the `validFrom` timestamp until the `validUntil` one, signed with the provider private key
1497 * `printDNSCryptProviderFingerprint("/path/to/providerPublic.key")`: display the fingerprint of the provided resolver public key
1498 * `showDNSCryptBinds():`: display the currently configured DNSCrypt binds
1499 * BPFFilter related:
1500 * function `newBPFFilter(maxV4, maxV6, maxQNames)`: return a new eBPF socket filter with a maximum of maxV4 IPv4, maxV6 IPv6 and maxQNames qname entries in the block tables
1501 * function `setDefaultBPFFilter(BPFFilter)`: when used at configuration time, the corresponding BPFFilter will be attached to every bind
1502 * member `attachToAllBinds()`: attach this filter to every bind already defined. This is the run-time equivalent of `setDefaultBPFFilter(bpf)`
1503 * member `block(ComboAddress)`: block this address
1504 * member `blockQName(DNSName [, qtype=255])`: block queries for this exact qname. An optional qtype can be used, default to 255
1505 * member `getStats()`: print the block tables
1506 * member `unblock(ComboAddress)`: unblock this address
1507 * member `unblockQName(DNSName [, qtype=255])`: remove this qname from the block list
1508 * DNSDistProtoBufMessage related:
1509 * member `setBytes(bytes)`: set the size of the query
1510 * member `setEDNSSubnet(Netmask)`: set the EDNS Subnet
1511 * member `setQueryTime(sec, usec)`: in a response message, set the time at which the query has been received
1512 * member `setQuestion(DNSName, qtype, qclass)`: set the question
1513 * member `setRequestor(ComboAddress)`: set the requestor
1514 * member `setRequestorFromString(string)`: set the requestor
1515 * member `setResponder(ComboAddress)`: set the responder
1516 * member `setResponderFromString(string)`: set the responder
1517 * member `setResponseCode(rcode)`: set the response code
1518 * member `setTime(sec, usec)`: set the time at which the query or response has been received
1519 * member `toDebugString()`: return an string containing the content of the message
1520 * DynBPFFilter related:
1521 * function `newDynBPFFilter(BPFFilter)`: return a new DynBPFFilter object using this BPF Filter
1522 * member `block(ComboAddress[, seconds]): add this address to the underlying BPF Filter for `seconds` seconds (default to 10 seconds)
1523 * member `purgeExpired()`: remove expired entries
1524 * function `registerDynBPFFilter(DynBPFFilter)`: register this dynamic BPF filter into the web interface so that its counters are displayed
1525 * function `unregisterDynBPFFilter(DynBPFFilter)`: unregister this dynamic BPF filter
1526 * RemoteLogger related:
1527 * `newRemoteLogger(address:port [, timeout=2, maxQueuedEntries=100, reconnectWaitTime=1])`: create a Remote Logger object, to use with `RemoteLogAction()` and `RemoteLogResponseAction()`
1531 `dnsdist` can call Lua per packet if so configured, and will do so with the following hooks:
1533 * `bool blockFilter(dq)`: if defined, called for every packet. If this
1534 returns true, the packet is dropped. If false is returned, `dnsdist` will check if the DNSHeader indicates
1535 the packet is now a query response. If so, `dnsdist` will answer the client directly with the modified packet.
1536 * `server policy(candidates, DNSQuestion)`: if configured with `setServerPolicyLua()`
1537 gets called for every packet. Candidates is a table of potential servers to pick from, ComboAddress is the
1538 address of the requestor, DNSName and qtype describe name and type of query. DNSHeader meanwhile is available for