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