]> Git — Sourcephile - sourcephile-nix.git/blob - hosts/mermet/unbound.nix
move var/ to networking/
[sourcephile-nix.git] / hosts / mermet / unbound.nix
1 { pkgs, lib, config, host, ... }:
2 let
3 inherit (config.services) unbound;
4 inherit (config.users) users;
5 stateDir = "/var/lib/unbound";
6 in
7 {
8 networking.resolvconf.useLocalResolver = true;
9 services.unbound = {
10 enable = true;
11 # DOC: https://calomel.org/unbound_dns.html
12 settings = {
13 remote-control = {
14 control-enable = true;
15 control-interface = "/run/unbound/unbound.socket";
16 };
17 server = {
18 log-queries = false;
19 verbosity = 1;
20 interface = [ "127.0.0.1" "::1" ];
21 prefer-ip4 = !config.networking.enableIPv6;
22 port = 53;
23
24 # The file which contains the listing of primary root DNS servers.
25 # To be updated once every six months.
26 root-hints = "/var/lib/unbound/named.root";
27
28 # Do no answer id.server and hostname.bind queries.
29 hide-identity = true;
30 # Do not answer version.server and version.bind queries.
31 hide-version = true;
32
33 # Will trust glue only if it is within the servers authority.
34 # Harden against out of zone rrsets, to avoid spoofing attempts.
35 # Hardening queries multiple name servers for the same data to make
36 # spoofing significantly harder and does not mandate dnssec.
37 harden-glue = true;
38
39 # Require DNSSEC data for trust-anchored zones, if such data is absent, the
40 # zone becomes bogus. Harden against receiving dnssec-stripped data. If you
41 # turn it off, failing to validate dnskey data for a trustanchor will trigger
42 # insecure mode for that zone (like without a trustanchor). Default on,
43 # which insists on dnssec data for trust-anchored zones.
44 harden-dnssec-stripped = true;
45
46 # Use 0x20-encoded random bits in the query to foil spoof attempts.
47 # http://tools.ietf.org/html/draft-vixie-dnsext-dns0x20-00
48 #
49 # When Unbound sends a query to a remote server it sends the hostname
50 # string in random upper and lower characters. The remote server must
51 # resolve the hostname as if all the characters were lower case. The remote
52 # server must then send the query back to Unbound in the same random upper
53 # and lower characters that Unbound sent. If the characters of the hostname
54 # in the response are in the same format as the query then the dns-0x20
55 # check is satisfied.
56 # Attackers hoping to poison a Unbound DNS cache must therefore guess the
57 # mixed-case encoding of the query and the timing of the return dns answer
58 # in addition to all other fields required in a DNS poisoning attack.
59 # dns-0x20 increases the difficulty of the attack significantly.
60 #
61 # It may result in maybe 0.4% of domains getting no answers
62 # due to no support on the authoritative server side
63 use-caps-for-id = true;
64
65 #cache-min-ttl = 3600;
66 cache-max-ttl = 86400;
67
68 # Perform prefetching of close to expired message cache entries. If a client
69 # requests the dns lookup and the TTL of the cached hostname is going to
70 # expire in less than 10% of its TTL, unbound will (1st) return the IP of the
71 # host to the client and (2nd) pre-fetch the DNS request from the remote DNS server.
72 # This method has been shown to increase the amount of cached hits by
73 # local clients by 10% on average.
74 prefetch = true;
75
76 # Number of threads to create. 1 disables threading.
77 # This should equal the number of CPU cores in the host.
78 num-threads = host.CPUs;
79
80 # The number of slabs to use for cache and must be a power of 2 times the
81 # number of num-threads set above. more slabs reduce lock contention,
82 # but fragment memory usage.
83 msg-cache-slabs = 8;
84 rrset-cache-slabs = 8;
85 infra-cache-slabs = 8;
86 key-cache-slabs = 8;
87
88 # Increase the memory size of the cache. Use roughly twice as much rrset cache
89 # memory as you use msg cache memory. Due to malloc overhead, the total memory
90 # usage is likely to rise to double (or 2.5x) the total cache memory.
91 rrset-cache-size = "32m";
92 msg-cache-size = "16m";
93
94 # buffer size for UDP port 53 incoming (SO_RCVBUF socket option). This sets
95 # the kernel buffer larger so that no messages are lost in spikes in the traffic.
96 so-rcvbuf = "1m";
97
98 # Enforce privacy of these addresses. Strips them away from answers.
99 # It may cause DNSSEC validation to additionally mark it as bogus.
100 # Protects against 'DNS Rebinding' (uses browser as network proxy).
101 # Only 'private-domain' and 'local-data' names are allowed
102 # to have these private addresses. No default.
103 private-address = [
104 "192.168.0.0/16"
105 "172.16.0.0/12"
106 "10.0.0.0/8"
107 ];
108
109 # Allow the domain (and its subdomains) to contain private addresses.
110 # local-data statements are allowed to contain private addresses too.
111 #private-domain = "home.lan";
112
113 # If nonzero, unwanted replies are not only reported in statistics, but also
114 # a running total is kept per thread. If it reaches the threshold, a warning
115 # is printed and a defensive action is taken, the cache is cleared to flush
116 # potential poison out of it. A suggested value is 10000000, the default is
117 # 0 (turned off). calomel.org thinks 10K is a good value.
118 unwanted-reply-threshold = 10000;
119
120 # IMPORTANT FOR TESTING: If you are testing and setup NSD or BIND on
121 # localhost you will want to allow the resolver to send queries to localhost.
122 # Make sure to set do-not-query-localhost = true;.
123 do-not-query-localhost = true;
124
125 # Should additional section of secure message also be kept clean of unsecure
126 # data. Useful to shield the users of this validator from potential bogus
127 # data in the additional section. All unsigned data in the additional section
128 # is removed from secure messages.
129 val-clean-additional = true;
130 };
131 };
132 };
133 networking.nftables.ruleset = ''
134 add rule inet filter fw2net tcp dport 53 skuid ${users.unbound.name} counter accept comment "Unbound"
135 add rule inet filter fw2net udp dport 53 skuid ${users.unbound.name} counter accept comment "Unbound"
136 '';
137 systemd.services.unbound = {
138 serviceConfig = {
139 RuntimeDirectory = "unbound";
140 RuntimeDirectoryMode = "0700";
141 };
142 preStart = ''
143 install -m 444 \
144 ${../../networking/named.root} \
145 /var/lib/unbound/named.root
146 '';
147 };
148 }