You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

256 lines
20 KiB

  1. # Unbound Recursive DNS Server with UCI
  2. <!-- markdownlint-disable -->
  3. ## Unbound Description
  4. [Unbound](https://www.unbound.net/) is a validating, recursive, and caching DNS resolver. The C implementation of Unbound is developed and maintained by [NLnet Labs](https://www.nlnetlabs.nl/). It is based on ideas and algorithms taken from a java prototype developed by Verisign labs, Nominet, Kirei and ep.net. Unbound is designed as a set of modular components, so that also DNSSEC (secure DNS) validation and stub-resolvers (that do not run as a server, but are linked into an application) are easily possible.
  5. ## Package Overview
  6. OpenWrt default build uses [dnsmasq](http://www.thekelleys.org.uk/dnsmasq/docs/dnsmasq-man.html) for DNS forwarding and DHCP. With a forward only resolver, dependence on the upstream recursors may be cause for concern. They are often provided by the ISP, and some users have switched to public DNS providers. Either way may result in problems due to performance, "snoop-vertising", hijacking (MiM), and other causes. Running a recursive resolver or resolver capable of TLS may be a solution.
  7. Unbound may be useful on consumer grade embedded hardware. It is fully DNSSEC and TLS capable. It is _intended_ to be a recursive resolver only. NLnet Labs [NSD](https://www.nlnetlabs.nl/projects/nsd/) is _intended_ for the authoritative task. This is different than [ISC Bind](https://www.isc.org/downloads/bind/) and its inclusive functions. Unbound configuration effort and memory consumption may be easier to control. A consumer could have their own recursive resolver with 8/64 MB router, and remove potential issues from forwarding resolvers outside of their control.
  8. This package builds on Unbounds capabilities with OpenWrt UCI. Not every Unbound option is in UCI, but rather, UCI simplifies the combination of related options. Unbounds native options are bundled and balanced within a smaller set of choices. Options include resources, DNSSEC, access control, and some TTL tweaking. The UCI also provides an escape option and works at the raw `unbound.conf` level.
  9. ## HOW TO: Ad Blocking
  10. The UCI scripts will work with [net/adblock](https://github.com/openwrt/packages/blob/master/net/adblock/files/README.md), if it is installed and enabled. Its all detected and integrated automatically. In brief, the adblock scripts create distinct local-zone files that are simply included in the unbound conf file during UCI generation. If you don't want this, then disable adblock or reconfigure adblock to not send these files to Unbound.
  11. A few tweaks may be needed to enhance the realiability and effectiveness. Ad Block option for delay time may need to be set for upto one minute (adb_triggerdelay), because of boot up race conditions with interfaces calling Unbound restarts. Also many smart devices (TV, microwave, or refigerator) will also use public DNS servers either as a bypass or for certain connections in general. If you wish to force exclusive DNS to your router, then you will need a firewall rule for example:
  12. **/etc/config/firewall**:
  13. ```
  14. config rule
  15. option dest 'wan'
  16. option dest_port '53 853 5353'
  17. option enabled '1'
  18. option family 'any'
  19. option name 'Block-Public-DNS'
  20. option proto 'tcpudp'
  21. option src 'lan'
  22. option target 'REJECT'
  23. ```
  24. ## HOW TO: Integrate with DHCP
  25. Some UCI options and scripts help Unbound to work with DHCP servers to load the local DNS. The examples provided here are serial dnsmasq with unbound, parallel dnsmasq with unbound, and unbound scripted with odhcpd.
  26. ### Serial dnsmasq
  27. In this case, dnsmasq is not changed *much* with respect to the default [OpenWrt](https://openwrt.org/docs/guide-user/base-system/dns_configuration) configuration. Here dnsmasq is forced to use the local Unbound instance as the lone upstream DNS server, instead of your ISP. This may be the easiest implementation, but performance degradation can occur in high volume networks. Unbound and dnsmasq effectively have the same information in memory, and all transfers are double handled.
  28. **/etc/config/unbound**:
  29. ```
  30. config unbound
  31. option add_local_fqdn '0'
  32. option add_wan_fqdn '0'
  33. option dhcp_link 'none'
  34. # dnsmasq should not forward your domain to unbound, but if...
  35. option domain 'yourdomain'
  36. option domain_type 'refuse'
  37. option listen_port '1053'
  38. ...
  39. ```
  40. **/etc/config/dhcp**:
  41. ```
  42. config dnsmasq
  43. option domain 'yourdomain'
  44. option noresolv '1'
  45. option port '53'
  46. option resolvfile '/tmp/resolv.conf.auto'
  47. list server '127.0.0.1#1053'
  48. list server '::1#1053'
  49. ...
  50. ```
  51. ### Parallel dnsmasq
  52. In this case, Unbound serves your local network directly for all purposes. It will look over to dnsmasq for DHCP-DNS resolution. Unbound is generally accessible on port 53, and dnsmasq is only accessed at `127.0.0.1#1053` by Unbound. Although you can dig/drill/nslookup remotely with the proper directives.
  53. **/etc/config/unbound**:
  54. ```
  55. config unbound
  56. # likely you want to match domain option between Unbound and dnsmasq
  57. option dhcp_link 'dnsmasq'
  58. option domain 'yourdomain'
  59. option listen_port '53'
  60. ...
  61. ```
  62. **/etc/config/dhcp**:
  63. ```
  64. config dnsmasq
  65. option domain 'yourdomain'
  66. option noresolv '1'
  67. option port '1053'
  68. option resolvfile '/tmp/resolv.conf.auto'
  69. ...
  70. config dhcp 'lan'
  71. # dnsmasq may not issue DNS option if not std. configuration
  72. list dhcp_option 'option:dns-server,0.0.0.0'
  73. ...
  74. ```
  75. ### Unbound and odhcpd
  76. You may ask, "can Unbound replace dnsmasq?" You can have DHCP-DNS records with Unbound and [odhcpd](https://github.com/openwrt/odhcpd/blob/master/README) only. The UCI scripts will allow Unbound to act like dnsmasq. When odhcpd configures each DHCP lease, it will call a script. The script provided with Unbound will read the lease file for DHCP-DNS records. The unbound-control application is required, because simply rewriting conf-files and restarting unbound is too much overhead.
  77. - Default OpenWrt has dnsmasq+odhcpd with `odhcpd-ipv6only` limited to DHCPv6.
  78. - If you use dnsmasq+odhcpd together, then use dnsmasq serial or parallel methods above.
  79. - You must install package `odhcpd` (full) to use odhcpd alone.
  80. - You must install package `unbound-control` to load and unload leases.
  81. - Remember to uninstall (or disable) dnsmasq when you won't use it.
  82. **/etc/config/unbound**:
  83. ```
  84. config unbound
  85. # name your router in DNS
  86. option add_local_fqdn '1'
  87. option add_wan_fqdn '1'
  88. option dhcp_link 'odhcpd'
  89. # add SLAAC inferred from DHCPv4
  90. option dhcp4_slaac6 '1'
  91. option domain 'lan'
  92. option domain_type 'static'
  93. option listen_port '53'
  94. option rebind_protection '1'
  95. # install unbound-control and set this
  96. option unbound_control '1'
  97. ...
  98. ```
  99. **/etc/config/dhcp**:
  100. ```
  101. config dhcp 'lan'
  102. option dhcpv4 'server'
  103. option dhcpv6 'server'
  104. option interface 'lan'
  105. option leasetime '12h'
  106. option ra 'server'
  107. option ra_management '1'
  108. ...
  109. config odhcpd 'odhcpd'
  110. option leasefile '/var/lib/odhcpd/dhcp.leases'
  111. # this is where the magic happens
  112. option leasetrigger '/usr/lib/unbound/odhcpd.sh'
  113. option maindhcp '1'
  114. ```
  115. ## HOW TO: Manual Override
  116. Yes, there is a UCI to disable the rest of Unbound UCI. However, OpenWrt or LEDE are targeted at embedded machines with flash ROM. The initialization scripts do a few things to protect flash ROM.
  117. ### Completely Manual (almost)
  118. All of `/etc/unbound` (persistent, ROM) is copied to `/var/lib/unbound` (tmpfs, RAM). Edit your manual `/etc/unbound/unbound.conf` to reference this `/var/lib/unbound` location for included files. Note in preparation for a jail, `/var/lib/unbound` is `chown unbound`. Configure for security in`/etc/unbound/unbound.conf` with options `username:unbound` and `chroot:/var/lib/unbound`.
  119. Keep the DNSKEY updated with your choice of flash activity. `root.key` maintenance for DNSKEY RFC5011 would be hard on flash. Unbound natively updates frequently. It also creates and destroys working files in the process. In `/var/lib/unbound` this is no problem, but it would be gone at the next reboot. If you have DNSSEC (validator) active, then you should consider the age UCI option. Choose how many days to copy from `/var/lib/unbound/root.key` (tmpfs) to `/etc/unbound/root.key` (flash).
  120. **/etc/config/unbound**:
  121. ```
  122. config unbound
  123. option manual_conf '1'
  124. option root_age '9'
  125. # end
  126. ```
  127. ### Hybrid Manual/UCI
  128. You like the UCI. Yet, you need to add some difficult to standardize options, or just are not ready to make a UCI request yet. The files `/etc/unbound/unbound_srv.conf` and `/etc/unbound/unbound_ext.conf` will be copied to Unbounds chroot directory and included during auto generation.
  129. The file `unbound_srv.conf` will be added into the `server:` clause. The file `unbound_ext.conf` will be added to the end of all configuration. It is for extended `forward-zone:`, `stub-zone:`, `auth-zone:`, and `view:` clauses. You can also disable unbound-control in the UCI which only allows "localhost" connections unencrypted, and then add an encrypted remote `control:` clause.
  130. ## HOW TO: Cache Zone Files
  131. Unbound has the ability to AXFR a whole zone from an authoritative server to prefetch the zone. This can speed up access to common zones. Some may have special bandwidth concerns for DNSSEC overhead. The following is a generic example. UCI defaults include the [root](https://www.internic.net/domain/) zone, but it is disabled as a ready to go example.
  132. **/etc/config/unbound**:
  133. ```
  134. config zone
  135. option enabled '1'
  136. option fallback '1'
  137. list server 'ns1.it.example.com'
  138. list server 'ns2.it.example.com'
  139. option url_dir 'https://asset-management.it.example.com/zones/'
  140. option zone_type 'auth_zone'
  141. list zone_name 'example.com'
  142. ```
  143. ## HOW TO: TLS Over DNS
  144. Unbound can use TLS as a client or server. UCI supports Unbound as a forwarding client with TLS. Servers are more complex and need manual configuration. This may be desired for privacy against stealth tracking. Some public DNS servers seem to advertise help in this quest. If your looking for a better understanding, then some information can be found at [Cloudflare](https://www.cloudflare.com/) DNS [1.1.1.1](https://1.1.1.1/). The following is a generic example. You can mix providers by using complete server specificaiton to override the zones common port and certificate domain index.
  145. Update as of Unbound 1.9.1, all TLS functions work correctly with either OpenSSL 1.0.2 or 1.1.0. Please be sure to install `ca-bundle` package and use `opkg` to get updates regularly.
  146. **/etc/config/unbound**:
  147. ```
  148. config zone
  149. option enabled '1'
  150. # question: do you want to recurse when TLS fails or not?
  151. option fallback '0'
  152. option tls_index 'dns.example.net'
  153. option tls_port '853'
  154. option tls_upstream '1'
  155. # these servers assume a common TLS port/index
  156. list server '192.0.2.53'
  157. list server '2001:db8::53'
  158. # this alternate server is fully specified inline
  159. list server '192.0.2.153@443#dns.alternate.example.org'
  160. list zone_name '.'
  161. option zone_type 'forward_zone'
  162. ```
  163. ## Optional Compile Switches
  164. Unbound can be changed by toggling switches within `make menuconfig` Libraries/Network/libunbound. Disable libevent, libpthread, and ipset to attempt to gain performance and size on small single core targets. These downgrade options are well tested, but they are not needed unless Unbound will not fit. Take care before enabling subnetcache, dnscrypt, and python options. These enhancements are not fully tested within OpenWrt and python is a large dependency. These enhancements are default off and they do not have UCI. You will need to use the files `/etc/unbound/unbound_srv.conf` and `/etc/unbound/unbound_ext.conf` to configure these modules. The `server:` clause line `module: subnetcache validator python iterator` will be filled out if the modules are compiled in.
  165. Note: if you use python, then you will need to manual configure and you cannot use chroot. The scripts are not yet enhanced enough to set up the directory binding.
  166. ## Complete UCI
  167. ### config unbound
  168. One instance is supported currently.
  169. | UCI | Default | Units | Description | Unbound |
  170. | --- | ------- | ----- | ----------- | ------- |
  171. | add_extra_dns | 0 | level | Read OpenWrt traditional options for `dnsmasq`.<br>`0`: Disabled<br>`1`: Use only domain<br>`2`: Use domain, mxhost, and srvhost<br>`3`: Use all cname, domain, mxhost, and srvhost | local-data: |
  172. | add_local_fqdn | 0 | level | Each level puts a more detailed router entry within the LAN DNS (except link).<br>`0`: Disabled<br>`1`: Host name on the primary address<br>`2`: Host name on all addresses<br>`3`: FQDN and host name on all addresses<br>`4`: FQDN defined by "iface.hostname.domain" | local-zone: local-data: |
  173. | add_wan_fqdn | 0 | level | Same as `add_local_fqdn` but on WAN as listed in `iface_wan` | local-zone: local-data: |
  174. | dns64 | 0 | boolean | Enable DNS64 RFC6052 to bridge IPv4 and IPv6 networks. | module: dns64 |
  175. | dns64_prefix | 64:ff9b::/96 | subnet | DNS64 RFC6052 IPv4 in IPv6 well known prefix. | dns64-prefix: |
  176. | dhcp_link | none | program | Link to a DHCP server with supported scripts. See HOW TO above. | local-zone: local-data: forward-zone: |
  177. | dhcp4_slaac6 | 0 | boolean | Infer SLAAC IE64 IPv6 addresses from DHCPv4 MAC in DHCP link scripts. | - |
  178. | domain | lan | domain | This will suffix DHCP host records and be the default search domain. | local-zone: |
  179. | domain_insecure | (empty) | domain | **List** domains that you wish to skip DNSSEC. It is one way around NTP chicken and egg. Your DHCP domains are automatically included. | domain-insecure: |
  180. | domain_type | static | state | This allows you to lock down or allow forwarding of the local zone.<br>`static`: no forwarding like dnsmasq default<br>`refuse`: answer overtly with REFUSED<br>`deny`: covertly drop all queries<br>`transparent`: may continue forwarding or recusion | local-zone: |
  181. | edns_size | 1232 | bytes | Extended DNS is necessary for DNSSEC. Use this to manage MTU issues. | edns-size: |
  182. | extended_stats | 0 | boolean | Extended statistics are stored in Unbound memory for report by `unbound-control`. | extended-statistics: |
  183. | hide_binddata | 1 | boolean | Refuse possible attack queries like version.server, version.bind, id.server, and hostname.bind. | hide-identity: hide-version: |
  184. | iface_lan | lan | interface | **List** to add interafaces you wish to consider to be LAN beyond those served by DHCP | interface: access-control: |
  185. | iface_trig | lan wan | interface | **List** interfaces to watch IFUP to restart Unbound. This works around `netifd` and `procd` hyper activity with WAN DHCPv6 (else restart each 2-3 minutes). | - |
  186. | iface_wan | wan | interface | **List** interafaces you wish to consider to be WAN for masked local zone purposes | interface-outgoing: |
  187. | interface_auto | 1 | boolean | RECOMMEND ENABLED otherwise Unbound answers to any attached address regardless of query in-address. This also binds Unboud to the wild card address. | interface-automatic: |
  188. | listen_port | 53 | port | Inbound port where Unbound will listen for queries. | port: |
  189. | localservice | 1 | boolean | Prevent DNS amplification attacks. Only answer to subnets this machine has interfaces on. | access-control: |
  190. | manual_conf | 0 | boolean | Skip all this UCI nonsense. Manually edit the configuration in `/etc/unbound/unbound.conf`. | - |
  191. | num_threads | 1 | threads | Enable multithreading. Two are supported with only one cache slab. More becomes an industrial setup, and UCI simplificaitons may not be appropriate. | num-threads: |
  192. | protocol | mixed | state | Limit Unbound protocols for recursion or forwarding.<br>`ip4_only`: old fashioned IPv4 upstream and downstream<br>`ip6_only`: test environment only; could cauase problems<br>`ip6_local`: upstream IPv4 only and local network IPv4 and IPv6<br>`ip6_prefer`: both IPv4 and IPv6 but try IPv6 first<br>`mixed`: both IPv4 and IPv6<br>`default`: built-in defaults | do-ip4: do-ip6: prefer-ip6: interface: |
  193. | query_minimize | 0 | boolean | Enable a minor privacy option. Query one domain piece at a time (dot separator). | qname-minimisation: |
  194. | query_min_strict | 0 | boolean | Query minimize may fall back to normal. This prevents the fall back, but non-standard name servers will fail. | qname-minimisation-strict: |
  195. | rate_limit | 0 | query/s | Limit the total number of queries and queries per client by half that. Disabled by setting 0. | ratelimit: ip-ratelimit: |
  196. | rebind_localhost | 0 | boolean | Prevent loopback addresses `127.0.0.0/8` or `::1` in upstream responses. They could be a good black hole server or a spring board attack. | private-address: |
  197. | rebind_protection | 1 | level | Prevent your local addresses from being allowed in upstream responses. They could be a spring board attack.<br>`0`: Disabled<br>`1`: Only RFC 1918 and 4193 responses blocked<br>`2`: Above plus GLA /64 on machine<br>`3`: Above plus DHCP-PD passed down (not implemented) | private-address: |
  198. | recursion | passive | state | Unbound has many options for recrusion but UCI is bundled for simplicity.<br>`passive`: slower until cache fills but kind on CPU load<br>`default`: built-in defaults<br>`aggressive`: uses prefetching to handle more requests quickly | (many) |
  199. | resource | small | state | Unbound has many options for memory resources but UCI is bundled for simplicity.<br>`tiny`: similar to published memory restricted configuration<br>`small`: about half of medium<br>`medium`: similar to default<br>`default`: built-in defaults<br>`large`: about double of medium | \*-cache-size: |
  200. | root_age | 9 | day | >90 Disables. Age limit for root data like root DNSSEC key. Scripts will copy from `tmps` to flash ROM with this limit to save write life. | - |
  201. | ttl_min | 120 | second | Minimum TTL in cache to avoid abused low TTL for snoop-vertising and non-standard load balancing. Typical to configure maybe 0~300 but 1800 is the maximum accepted. | cache-min-ttl: |
  202. | unbound_control | 0 | level | Enables `unbound-control` application access ports.<br>`0`: None else add your own in unbound_ext.conf<br>`1`: Unencrypted Local Host Access<br>`2`: SSL Local Host Access w/ auto unbound-control-setup<br>`3`: SSL Network Access w/ auto unbound-control-setup<br>`4`: SSL Network Access; static key/pem files must already exist | unbound-control: ... (clause) |
  203. | validator | 0 | boolean | Enable DNSSEC validator module. | module: validator |
  204. | validator_ntp | 1 | boolean | Disable DNSSEC time checks at boot and restart when NTP locks in time. DNSSEC requires time validation and this breaks the chicken and egg. | - |
  205. | verbosity | 1 | level | Logging inensity. | verbosity: |
  206. ### config zone
  207. Confingure any mix of Unbound `forward-zone:`, `stub-zone:`, or `auth-zone:` clauses. These sections are more compact than Unbound and will unroll into Unbound's configuration syntax.
  208. | UCI | Default | Units | Description | Unbound |
  209. | --- | ------- | ----- | ----------- | ------- |
  210. | dns_assist | none | program | Check against local host forwarding by requiring a target program to exist and be enabled else do not permit forwarding `127.0.0.0/8` or `::1`. Includes bind, dnsmasq, http-proxy-dns, ipset-dns, and nsd. | forward-addr: |
  211. | enabled | 0 | boolean | turn zone on or off without deleting it | - |
  212. | fallback | 1 | boolean | Allow this zone to fall through to other zones or recursion. | forward-first: |
  213. | port | 53 | port | Target server's target port for plain DNS operations. | (auto 192.0.2.53 \#53)
  214. | resolv_conf | 0 | boolean | Use resolv.conf servers as it was filled by the DHCP client. For example your ISP services (mail.example.net) or co-located services (streamed-movies.example.com). | - |
  215. | server | (empty) | address domain | Required **list** to target servers. Full form is accepted but not required `192.0.2.53#53@dns.exmple.net`. Stub zones must use an IP address. | master: forward-host: forward-addr: stub-addr: |
  216. | tls_index | (empty) | domain | TLS certifiicated signature index. If ommitted, Unbound encrypts but does not validate. | (auto 192.0.2.53 \#853 \@dns.example.net) |
  217. | tls_port | 853 | port | Target server's target port for TLS DNS operations. | (auto 192.0.2.53 \#853) |
  218. | tls_upstream | 0 | boolean | Use TLS to contact the zone server. | forward-tls-upstream: |
  219. | url_dir | (empty) | string | Directory only path by http or https to get authoritative zone files. | url: |
  220. | zone_name | (empty) | domain | Required **list** to select domains subject to this zone section. You can forward the root `.` zone or just your organizaiton `example.com.` | name: |
  221. | zone_type | (empty) | state | Required option to create an Unbound zone clause.<br>`auth_zone`: prefetch whole zones from authoritative server<br>`forward_zone`: forward queries by domain name (like dnsmasq)<br>`stub_zone`: direct authoritative lookup by domain name | auth-zone: forward-zone: stub-zone: ... (clauses) |