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