]> git.ipfire.org Git - ipfire-2.x.git/blob - src/initscripts/system/unbound
unbound: Double buffer size
[ipfire-2.x.git] / src / initscripts / system / unbound
1 #!/bin/sh
2 # Begin $rc_base/init.d/unbound
3
4 # Description : Unbound DNS resolver boot script for IPfire
5 # Author : Marcel Lorenz <marcel.lorenz@ipfire.org>
6
7 . /etc/sysconfig/rc
8 . ${rc_functions}
9
10 TEST_DOMAIN="ipfire.org"
11
12 # This domain will never validate
13 TEST_DOMAIN_FAIL="dnssec-failed.org"
14
15 INSECURE_ZONES=
16 USE_FORWARDERS=1
17
18 # Cache any local zones for 60 seconds
19 LOCAL_TTL=60
20
21 # EDNS buffer size
22 EDNS_DEFAULT_BUFFER_SIZE=4096
23
24 # Load optional configuration
25 [ -e "/etc/sysconfig/unbound" ] && . /etc/sysconfig/unbound
26
27 function cidr() {
28 local cidr nbits IFS;
29 IFS=. read -r i1 i2 i3 i4 <<< ${1}
30 IFS=. read -r m1 m2 m3 m4 <<< ${2}
31 cidr=$(printf "%d.%d.%d.%d\n" "$((i1 & m1))" "$((i2 & m2))" "$((i3 & m3))" "$((i4 & m4))")
32 nbits=0
33 IFS=.
34 for dec in $2 ; do
35 case $dec in
36 255) let nbits+=8;;
37 254) let nbits+=7;;
38 252) let nbits+=6;;
39 248) let nbits+=5;;
40 240) let nbits+=4;;
41 224) let nbits+=3;;
42 192) let nbits+=2;;
43 128) let nbits+=1;;
44 0);;
45 *) echo "Error: $dec is not recognised"; exit 1
46 esac
47 done
48 echo "${cidr}/${nbits}"
49 }
50
51 ip_address_revptr() {
52 local addr=${1}
53
54 local a1 a2 a3 a4
55 IFS=. read -r a1 a2 a3 a4 <<< ${addr}
56
57 echo "${a4}.${a3}.${a2}.${a1}.in-addr.arpa"
58 }
59
60 read_name_servers() {
61 local i
62 for i in 1 2; do
63 echo "$(</var/ipfire/red/dns${i})"
64 done | xargs echo
65 }
66
67 config_header() {
68 echo "# This file is automatically generated and any changes"
69 echo "# will be overwritten. DO NOT EDIT!"
70 echo
71 }
72
73 update_forwarders() {
74 if [ "${USE_FORWARDERS}" = "1" -a -e "/var/ipfire/red/active" ]; then
75 local forwarders
76 local broken_forwarders
77
78 local ns
79 for ns in $(read_name_servers); do
80 test_name_server ${ns} &>/dev/null
81 case "$?" in
82 # Only use DNSSEC-validating or DNSSEC-aware name servers
83 0|2)
84 forwarders="${forwarders} ${ns}"
85 ;;
86 *)
87 broken_forwarders="${broken_forwarders} ${ns}"
88 ;;
89 esac
90 done
91
92 # Determine EDNS buffer size
93 local new_edns_buffer_size=${EDNS_DEFAULT_BUFFER_SIZE}
94
95 for ns in ${forwarders}; do
96 local edns_buffer_size=$(ns_determine_edns_buffer_size ${ns})
97 if [ -n "${edns_buffer_size}" ]; then
98 if [ ${edns_buffer_size} -lt ${new_edns_buffer_size} ]; then
99 new_edns_buffer_size=${edns_buffer_size}
100 fi
101 fi
102 done
103
104 if [ ${new_edns_buffer_size} -lt ${EDNS_DEFAULT_BUFFER_SIZE} ]; then
105 boot_mesg "EDNS buffer size reduced to ${new_edns_buffer_size}" ${WARNING}
106 echo_warning
107
108 unbound-control -q set_option edns-buffer-size: ${new_edns_buffer_size}
109 fi
110
111 # Show warning for any broken upstream name servers
112 if [ -n "${broken_forwarders}" ]; then
113 boot_mesg "Ignoring broken upstream name server(s): ${broken_forwarders:1}" ${WARNING}
114 echo_warning
115 fi
116
117 if [ -n "${forwarders}" ]; then
118 boot_mesg "Configuring upstream name server(s): ${forwarders:1}" ${INFO}
119 echo_ok
120
121 # Make sure DNSSEC is activated
122 enable_dnssec
123
124 echo "${forwarders}" > /var/ipfire/red/dns
125 unbound-control -q forward ${forwarders}
126 return 0
127
128 # In case we have found no working forwarders
129 else
130 # Test if the recursor mode is available
131 if can_resolve_root +bufsize=${new_edns_buffer_size}; then
132 # Make sure DNSSEC is activated
133 enable_dnssec
134
135 boot_mesg "Falling back to recursor mode" ${WARNING}
136 echo_warning
137
138 # If not, we set DNSSEC in permissive mode and allow using all recursors
139 elif [ -n "${broken_forwarders}" ]; then
140 disable_dnssec
141
142 boot_mesg "DNSSEC has been set to permissive mode" ${FAILURE}
143 echo_failure
144
145 echo "${broken_forwarders}" > /var/ipfire/red/dns
146 unbound-control -q forward ${broken_forwarders}
147 return 0
148 fi
149 fi
150 fi
151
152 # If forwarders cannot be used we run in recursor mode
153 echo "local recursor" > /var/ipfire/red/dns
154 unbound-control -q forward off
155 }
156
157 own_hostname() {
158 local hostname=$(hostname -f)
159 # 1.1.1.1 is reserved for unused green, skip this
160 if [ -n "${GREEN_ADDRESS}" -a "${GREEN_ADDRESS}" != "1.1.1.1" ]; then
161 unbound-control -q local_data "${hostname} ${LOCAL_TTL} IN A ${GREEN_ADDRESS}"
162 fi
163
164 local address
165 for address in ${GREEN_ADDRESS} ${BLUE_ADDRESS} ${ORANGE_ADDRESS}; do
166 [ -n "${address}" ] || continue
167 [ "${address}" = "1.1.1.1" ] && continue
168
169 address=$(ip_address_revptr ${address})
170 unbound-control -q local_data "${address} ${LOCAL_TTL} IN PTR ${hostname}"
171 done
172 }
173
174 update_hosts() {
175 local enabled address hostname domainname
176
177 while IFS="," read -r enabled address hostname domainname; do
178 [ "${enabled}" = "on" ] || continue
179
180 # Build FQDN
181 local fqdn="${hostname}.${domainname}"
182
183 unbound-control -q local_data "${fqdn} ${LOCAL_TTL} IN A ${address}"
184
185 # Skip reverse resolution if the address equals the GREEN address
186 [ "${address}" = "${GREEN_ADDRESS}" ] && continue
187
188 # Add RDNS
189 address=$(ip_address_revptr ${address})
190 unbound-control -q local_data "${address} ${LOCAL_TTL} IN PTR ${fqdn}"
191 done < /var/ipfire/main/hosts
192 }
193
194 write_forward_conf() {
195 (
196 config_header
197
198 local insecure_zones="${INSECURE_ZONES}"
199
200 local enabled zone server remark
201 while IFS="," read -r enabled zone server remark; do
202 # Line must be enabled.
203 [ "${enabled}" = "on" ] || continue
204
205 # Zones that end with .local are commonly used for internal
206 # zones and therefore not signed
207 case "${zone}" in
208 *.local)
209 insecure_zones="${insecure_zones} ${zone}"
210 ;;
211 esac
212
213 echo "forward-zone:"
214 echo " name: ${zone}"
215 echo " forward-addr: ${server}"
216 echo
217 done < /var/ipfire/dnsforward/config
218
219 if [ -n "${insecure_zones}" ]; then
220 echo "server:"
221
222 for zone in ${insecure_zones}; do
223 echo " domain-insecure: ${zone}"
224 done
225 fi
226 ) > /etc/unbound/forward.conf
227 }
228
229 write_tuning_conf() {
230 # https://www.unbound.net/documentation/howto_optimise.html
231
232 # Determine number of online processors
233 local processors=$(getconf _NPROCESSORS_ONLN)
234
235 # Determine number of slabs
236 local slabs=1
237 while [ ${slabs} -lt ${processors} ]; do
238 slabs=$(( ${slabs} * 2 ))
239 done
240
241 # Determine amount of system memory
242 local mem=$(get_memory_amount)
243
244 # In the worst case scenario, unbound can use double the
245 # amount of memory allocated to a cache due to malloc overhead
246
247 # Large systems with more than 2GB of RAM
248 if [ ${mem} -ge 2048 ]; then
249 mem=256
250
251 # Small systems with less than 256MB of RAM
252 elif [ ${mem} -le 256 ]; then
253 mem=16
254
255 # Everything else
256 else
257 mem=64
258 fi
259
260 (
261 config_header
262
263 # We run one thread per processor
264 echo "num-threads: ${processors}"
265 echo "so-reuseport: yes"
266
267 # Adjust number of slabs
268 echo "infra-cache-slabs: ${slabs}"
269 echo "key-cache-slabs: ${slabs}"
270 echo "msg-cache-slabs: ${slabs}"
271 echo "rrset-cache-slabs: ${slabs}"
272
273 # Slice up the cache
274 echo "rrset-cache-size: $(( ${mem} / 2 ))m"
275 echo "msg-cache-size: $(( ${mem} / 4 ))m"
276 echo "key-cache-size: $(( ${mem} / 4 ))m"
277
278 # Increase parallel queries
279 echo "outgoing-range: 8192"
280 echo "num-queries-per-thread: 4096"
281
282 # Use larger send/receive buffers
283 echo "so-sndbuf: 4m"
284 echo "so-rcvbuf: 4m"
285 ) > /etc/unbound/tuning.conf
286 }
287
288 get_memory_amount() {
289 local key val unit
290
291 while read -r key val unit; do
292 case "${key}" in
293 MemTotal:*)
294 # Convert to MB
295 echo "$(( ${val} / 1024 ))"
296 break
297 ;;
298 esac
299 done < /proc/meminfo
300 }
301
302 test_name_server() {
303 local ns=${1}
304 local args
305
306 # Return codes:
307 # 0 DNSSEC validating
308 # 1 Error: unreachable, etc.
309 # 2 DNSSEC aware
310 # 3 NOT DNSSEC-aware
311
312 # Exit when the server is not reachable
313 ns_is_online ${ns} || return 1
314
315 # Determine the maximum edns buffer size that works
316 local edns_buffer_size=$(ns_determine_edns_buffer_size ${ns})
317 if [ -n "${edns_buffer_size}" ]; then
318 args="${args} +bufsize=${edns_buffer_size}"
319 fi
320
321 local errors
322 for rr in DNSKEY DS RRSIG; do
323 if ! ns_forwards_${rr} ${ns} ${args}; then
324 errors="${errors} ${rr}"
325 fi
326 done
327
328 if [ -n "${errors}" ]; then
329 echo >&2 "Unable to retrieve the following resource records from ${ns}: ${errors:1}"
330 return 3
331 fi
332
333 if ns_is_validating ${ns} ${args}; then
334 # Return 0 if validating
335 return 0
336 else
337 # Is DNSSEC-aware
338 return 2
339 fi
340 }
341
342 # Sends an A query to the nameserver w/o DNSSEC
343 ns_is_online() {
344 local ns=${1}
345 shift
346
347 dig @${ns} +nodnssec A ${TEST_DOMAIN} $@ >/dev/null
348 }
349
350 # Resolving ${TEST_DOMAIN_FAIL} will fail if the nameserver is validating
351 ns_is_validating() {
352 local ns=${1}
353 shift
354
355 dig @${ns} A ${TEST_DOMAIN_FAIL} $@ | grep -q SERVFAIL
356 }
357
358 # Checks if we can retrieve the DNSKEY for this domain.
359 # dig will print the SOA if nothing was found
360 ns_forwards_DNSKEY() {
361 local ns=${1}
362 shift
363
364 dig @${ns} DNSKEY ${TEST_DOMAIN} $@ | grep -qv SOA
365 }
366
367 ns_forwards_DS() {
368 local ns=${1}
369 shift
370
371 dig @${ns} DS ${TEST_DOMAIN} $@ | grep -qv SOA
372 }
373
374 ns_forwards_RRSIG() {
375 local ns=${1}
376 shift
377
378 dig @${ns} +dnssec A ${TEST_DOMAIN} $@ | grep -q RRSIG
379 }
380
381 ns_supports_tcp() {
382 local ns=${1}
383 shift
384
385 dig @${ns} +tcp A ${TEST_DOMAIN} $@ >/dev/null || return 1
386 }
387
388 ns_determine_edns_buffer_size() {
389 local ns=${1}
390 shift
391
392 local b
393 for b in 4096 2048 1500 1480 1464 1400 1280 512; do
394 if dig @${ns} +dnssec +bufsize=${b} A ${TEST_DOMAIN} $@ >/dev/null; then
395 echo "${b}"
396 return 0
397 fi
398 done
399
400 return 1
401 }
402
403 get_root_nameservers() {
404 while read -r hostname ttl record address; do
405 # Searching for A records
406 [ "${record}" = "A" ] || continue
407
408 echo "${address}"
409 done < /etc/unbound/root.hints
410 }
411
412 can_resolve_root() {
413 local ns
414 for ns in $(get_root_nameservers); do
415 if dig @${ns} +dnssec SOA . $@ >/dev/null; then
416 return 0
417 fi
418 done
419
420 # none of the servers was reachable
421 return 1
422 }
423
424 enable_dnssec() {
425 local status=$(unbound-control get_option val-permissive-mode)
426
427 # Don't do anything if DNSSEC is already activated
428 [ "${status}" = "no" ] && return 0
429
430 # Activate DNSSEC and flush cache with any stale and unvalidated data
431 unbound-control -q set_option val-permissive-mode: no
432 unbound-control -q flush_zone .
433 }
434
435 disable_dnssec() {
436 unbound-control -q set_option val-permissive-mode: yes
437 }
438
439 case "$1" in
440 start)
441 # Print a nicer messagen when unbound is already running
442 if pidofproc -s unbound; then
443 statusproc /usr/sbin/unbound
444 exit 0
445 fi
446
447 eval $(/usr/local/bin/readhash /var/ipfire/ethernet/settings)
448
449 # Create control keys at first run
450 if [ ! -r "/etc/unbound/unbound_control.key" ]; then
451 unbound-control-setup -d /etc/unbound &>/dev/null
452 fi
453
454 # Update configuration files
455 write_tuning_conf
456 write_forward_conf
457
458 boot_mesg "Starting Unbound DNS Proxy..."
459 loadproc /usr/sbin/unbound || exit $?
460
461 # Make own hostname resolveable
462 own_hostname
463
464 # Update any known forwarding name servers
465 update_forwarders
466
467 # Update hosts
468 update_hosts
469 ;;
470
471 stop)
472 boot_mesg "Stopping Unbound DNS Proxy..."
473 killproc /usr/sbin/unbound
474 ;;
475
476 restart)
477 $0 stop
478 sleep 1
479 $0 start
480 ;;
481
482 status)
483 statusproc /usr/sbin/unbound
484 ;;
485
486 update-forwarders)
487 # Do not try updating forwarders when unbound is not running
488 if ! pgrep unbound &>/dev/null; then
489 exit 0
490 fi
491
492 update_forwarders
493 ;;
494
495 test-name-server)
496 ns=${2}
497
498 test_name_server ${ns}
499 ret=${?}
500
501 case "${ret}" in
502 0)
503 echo "${ns} is validating"
504 ;;
505 2)
506 echo "${ns} is DNSSEC-aware"
507 ;;
508 3)
509 echo "${ns} is NOT DNSSEC-aware"
510 ;;
511 *)
512 echo "Test failed for an unknown reason"
513 exit ${ret}
514 ;;
515 esac
516
517 if ns_supports_tcp ${ns}; then
518 echo "${ns} supports TCP fallback"
519 else
520 echo "${ns} does not support TCP fallback"
521 fi
522
523 edns_buffer_size=$(ns_determine_edns_buffer_size ${ns})
524 if [ -n "${edns_buffer_size}" ]; then
525 echo "EDNS buffer size for ${ns}: ${edns_buffer_size}"
526 fi
527
528 exit ${ret}
529 ;;
530
531 *)
532 echo "Usage: $0 {start|stop|restart|status|update-forwarders|test-name-server}"
533 exit 1
534 ;;
535 esac
536
537 # End $rc_base/init.d/unbound