1---
2title: systemd-resolved and VPNs
3category: Networking
4layout: default
5SPDX-License-Identifier: LGPL-2.1-or-later
6---
7
8# `systemd-resolved.service` and VPNs
9
10`systemd-resolved.service` supports routing lookups for specific domains to specific
11interfaces. This is useful for hooking up VPN software with systemd-resolved
12and making sure the exact right lookups end up on the VPN and on the other
13interfaces.
14
15For a verbose explanation of `systemd-resolved.service`'s domain routing logic,
16see its [man
17page](https://www.freedesktop.org/software/systemd/man/systemd-resolved.service.html). This
18document is supposed to provide examples to use the concepts for the specific
19purpose of managing VPN DNS configuration.
20
21Let's first define two distinct VPN use-cases:
22
231. *Corporate* VPNs, i.e. VPNs that open access to a specific set of additional
24   hosts. Only specific domains should be resolved via the VPN's DNS servers,
25   and everything that is not related to the company's domain names should go
26   to regular, non-VPN DNS instead.
27
282. *Privacy* VPNs, i.e. VPNs that should be used for basically all DNS traffic,
29   once they are up. If this type of VPN is used, any regular, non-VPN DNS
30   servers should not get any traffic anymore.
31
32Then, let's briefly introduce three DNS routing concepts that software managing
33a network interface may configure.
34
351. Search domains: these are traditional DNS configuration parameters and are
36   used to suffix non-qualified domain names (i.e. single-label ones), to turn
37   them into fully qualified domain names. Traditionally (before
38   `systemd-resolved.service`), search domain names are attached to a system's
39   IP configuration as a whole, in `systemd-resolved.service` they are
40   associated to individual interfaces instead, since they are typically
41   acquired through some network associated concept, such as a DHCP, IPv6RA or
42   PPP lease. Most importantly though: in `systemd-resolved.service` they are
43   not just used to suffix single-label domain names, but also for routing
44   domain name lookups: if a network interface has a search domain `foo.com`
45   configured on it, then any lookups for names ending in `.foo.com` (or for
46   `foo.com` itself) are preferably routed to the DNS servers configured on the
47   same network interface.
48
492. Routing domains: these are very similar to search domains, but are purely
50   about DNS domain name lookup routing — they are not used for qualifying
51   single-label domain names. When it comes to routing assigning a routing
52   domain to a network interface is identical to assigning a search domain to
53   it.
54
55   Why the need to have both concepts, i.e. search *and* routing domains?
56   Mostly because in many cases the qualifying of single-label names is not
57   desirable (since security-sensitive), but needs to be supported for specific
58   use-cases. Routing domains are a concept `systemd-resolved.service`
59   introduced, while search domains are traditionally available and are part of
60   DHCP/IPv6RA/PPP leases and thus universally supported. In many cases routing
61   domains are probably the more appropriate concept, but not easily available,
62   since not part of DHCP/IPv6RA/PPP.
63
64   Routing domains for `systemd-resolved.service` are usually presented along
65   with search domains in mostly the same way, but prefixed with `~` to
66   differentiate them. i.e. `~foo.com` is a configured routing domain, while
67   `foo.com` would be a configured search domain.
68
69   One routing domain is particularly interesting: `~.` — the catch-all routing
70   domain. (The *dot* domain `.` is how DNS denotes the "root" domain, i.e. the
71   parent domain of all domains, but itself.) When used on an interface any DNS
72   traffic is preferably routed to its DNS servers. (A search domain – i.e. `.`
73   instead of `~.` — would have the same effect, but given that it's mostly
74   pointless to suffix an unqualified domain with `.`, we generally declare it
75   as a routing domain, not a search domain).
76
77   Routing domains also have particular relevance when it comes to the reverse
78   lookup DNS domains `.in-addr.arpa` and `.ip6.arpa`. An interface that has
79   these (or sub-domains thereof) defined as routing domains, will be preferably
80   used for doing reverse IP to domain name lookups. e.g. declaring
81   `~168.192.in-addr.arpa` on an interface means that all lookups to find the
82   domain names for IPv4 addresses 192.168.x.y are preferable routed to it.
83
843. The `default-route` boolean. This is a simple boolean value that may be set
85   on an interface. If true (the default), any DNS lookups for which no
86   matching routing or search domains are defined are routed to interfaces
87   marked like this. If false then the DNS servers on this interface are not
88   considered for routing lookups to except for the ones listed in the
89   search/routing domain list. An interface that has no search/routing domain
90   associated and also has this boolean off is not considered for *any*
91   lookups.
92
93One more thing to mention: in `systemd-resolved.service` if lookups match the
94search/routing domains of multiple interfaces at once, then they are sent to
95all of them in parallel, and the first positive reply used. If all lookups fail
96the last negative reply is used. This means the DNS zones on the relevant
97interfaces are "merged": domains existing on one but not the other will "just
98work" and vice versa.
99
100And one more note: the domain routing logic implemented is a tiny bit more
101complex that what described above: if there two interfaces have search domains
102that are suffix of each other, and a name is looked up that matches both, the
103interface with the longer match will win and get the lookup routed to is DNS
104servers. Only if the match has the same length, then both will be used in
105parallel. Example: one interface has `~foo.example.com` as routing domain, and
106another one `example.com` has search domain. A lookup for
107`waldo.foo.example.com` is the exclusively routed to the first interface's DNS
108server, since it matches by three suffix labels instead of just two. The fact
109that the matching length is taken into consideration for the routing decision
110is particularly relevant if you have one interface with the `~.` routing domain
111and another one with `~corp.company.example` — both suffixes match a lookup for
112`foo.corp.company.example`, but the latter interface wins, since the match is
113for four labels, while the other is for zero labels.
114
115## Putting it Together
116
117Let's discuss how the three DNS routing concepts above are best used for a
118reasonably complex scenario consisting of:
119
1201. One VPN interface of the *corporate* kind, maybe called `company0`. It makes
121   available a bunch of servers, all in the domain `corp.company.example`.
122
1232. One VPN interface of the *privacy* kind, maybe called `privacy0`. When it is
124   up all DNS traffic shall preferably routed to its DNS servers.
125
1263. One regular WiFi interface, maybe called `wifi0`. It has a regular DNS
127   server on it.
128
129Here's how to best configure this for `systemd-resolved.service`:
130
1311. `company0` should get a routing domain `~corp.company.example`
132   configured. (A search domain `corp.company.example` would work too, if
133   qualifying of single-label names is desired or the VPN lease information
134   does not provide for the concept of routing domains, but does support search
135   domains.) This interface should also set `default-route` to false, to ensure
136   that really only the DNS lookups for the company's servers are routed there
137   and nothing else. Finally, it might make sense to also configure a routing
138   domain `~2.0.192.in-addr.arpa` on the interface, ensuring that all IPv4
139   addresses from the 192.0.2.x range are preferably resolved via the DNS
140   server on this interface (assuming that that's the IPv4 address range the
141   company uses internally).
142
1432. `privacy0` should get a routing domain `~.` configured. The setting of
144   `default-route` for this interface is then irrelevant. This means: once the
145   interface is up, all DNS traffic is preferably routed there.
146
1473. `wifi0` should not get any special settings, except possibly whatever the
148   local WiFi router considers suitable as search domain, for example
149   `fritz.box`. The default `true` setting for `default-route` is good too.
150
151With this configuration if only `wifi0` is up, all DNS traffic goes to its DNS
152server, since there are no other interfaces with better matching DNS
153configuration. If `privacy0` is then upped, all DNS traffic will exclusively go
154to this interface now — with the exception of names below the `fritz.box`
155domain, which will continue to go directly to `wifi0`, as the search domain
156there says so. Now, if `company0` is also upped, it will receive DNS traffic
157for the company's internal domain and internal IP subnet range, but nothing
158else.  If `privacy0` is then downed again, `wifi0` will get the regular DNS
159traffic again, and `company0` will still get the company's internal domain and
160IP subnet traffic and nothing else. Everything hence works as intended.
161
162## How to Implement this in Your VPN Software
163
164Most likely you want to expose a boolean in some way that declares whether a
165specific VPN is of the *corporate* or the *privacy* kind:
166
1671. If managing a *corporate* VPN, you configure any search domains the user or
168   the VPN contact point provided. And you set `default-route` to false. If you
169   have IP subnet information for the VPN, it might make sense to insert
170   `~….in-addr.arpa` and `~….ip6.arpa` reverse lookup routing domains for it.
171
1722. If managing a *privacy* VPN, you include `~.` in the routing domains, the
173   value for `default-route` is actually irrelevant, but I'd set it to true. No
174   need to configure any reverse lookup routing domains for it.
175
176(If you also manage regular WiFi/Ethernet devices, just configure them as
177traditional, i.e. with any search domains as acquired, do not set `~.` though,
178and do not disable `default-route`.)
179
180## The APIs
181
182Now we determined how we want to configure things, but how do you actually get
183the configuration to `systemd-resolved.service`? There are three relevant
184interfaces:
185
1861. Ideally, you use D-Bus and talk to [`systemd-resolved.service`'s D-Bus
187   API](https://www.freedesktop.org/software/systemd/man/org.freedesktop.resolve1.html)
188   directly. Use `SetLinkDomains()` to set the per-interface search and routing
189   domains on the interfaces you manage, and `SetLinkDefaultRoute()` to manage
190   the `default-route` boolean, all on the `org.freedesktop.resolve1.Manager`
191   interface of the `/org/freedesktop/resolve1` object.
192
1932. If that's not in the cards, you may shell out to
194   [`resolvectl`](https://www.freedesktop.org/software/systemd/man/resolvectl.html),
195   which is a thin wrapper around the D-Bus interface mentioned above. Use
196   `resolvectl domain <iface> …` to set the search/routing domains and
197   `resolvectl default-route <iface> …` to set the `default-route` boolean.
198
199   Example use from a shell callout of your VPN software for a *corporate* VPN:
200
201        resolvectl domain corporate0 '~corp-company.example' '~2.0.192.in-addr.arpa'
202        resolvectl default-route corporate0 false
203        resolvectl dns corporate0 192.0.2.1
204
205   Example use from a shell callout of your VPN software for a *privacy* VPN:
206
207        resolvectl domain privacy0 '~.'
208        resolvectl default-route privacy0 true
209        resolvectl dns privacy0 8.8.8.8
210
2113. If you don't want to use any `systemd-resolved` commands, you may use the
212   `resolvconf` wrapper we provide. `resolvectl` is actually a multi-call
213   binary and may be symlinked to `resolvconf`, and when invoked like that
214   behaves in a way that is largely compatible with FreeBSD's and
215   Ubuntu's/Debian's
216   [`resolvconf(8)`](https://manpages.ubuntu.com/manpages/trusty/man8/resolvconf.8.html)
217   tool. When the `-x` switch is specified, the `~.` routing domain is
218   automatically appended to the domain list configured, as appropriate for a
219   *privacy* VPN. Note that the `resolvconf` interface only covers *privacy*
220   VPNs and regular network interfaces (such as WiFi or Ethernet) well. The
221   *corporate* kind of VPN is not well covered, since the interface cannot
222   propagate the `default-route` boolean, nor can be used to configure the
223   `~….in-addr.arpa` or `~.ip6.arpa` routing domains.
224
225## Ordering
226
227When configuring per-interface DNS configuration settings it is wise to
228configure everything *before* actually upping the interface. Once the interface
229is up `systemd-resolved.service` might start using it, and hence it's important
230to have everything configured properly (this is particularly relevant when
231LLMNR or MulticastDNS is enabled, since that works without any explicitly
232configured DNS configuration). It is also wise to configure search/routing
233domains and the `default-route` boolean *before* configuring the DNS servers,
234as the former without the latter has no effect, but the latter without the
235former will result in DNS traffic possibly being generated, in a non-desirable
236way given that the routing information is not set yet.
237
238## Downgrading Search Domains to Routing Domains
239
240Many VPN implementations provide a way how VPN servers can inform VPN clients
241about search domains to use. In some cases it might make sense to install those
242as routing domains instead of search domains. Unqualified domain names usually
243imply a context of locality: the same unqualified name typically is expected to
244resolve to one system in one local network, and to another one in a different
245network.  Search domains thus generally come with security implications: they
246might cause that unqualified domains are resolved in a different (possibly
247remote) context, contradicting user expectations. Thus it might be wise to
248downgrade *search domains* provided by VPN servers to *routing domains*, so
249that local unqualified name resolution remains untouched and strictly maintains
250its local focus — in particular in the aforementioned less trusted *corporate*
251VPN scenario.
252
253To illustrate this further, here's an example for an attack scenario using
254search domains: a user assumes the printer system they daily contact under the
255unqualified name "printer" is the network printer in their basement (with the
256fully qualified domain name "printer.home"). Sometimes the user joins the
257corporate VPN of their employer, which comes with a search domain
258"foocorp.example", so that the user's confidential documents (maybe a job
259application to a competing company) might end up being printed on
260"printer.foocorp.example" instead of "printer.home". If the local VPN software
261had downgraded the VPN's search domain to a routing domain "~foocorp.example",
262this mismapping would not have happened.
263
264When connecting to untrusted WiFi networks it might be wise to go one step
265further even: suppress installation of search/routing domains by the network
266entirely, to ensure that the local DNS information is only used for name
267resolution of qualified names and only when no better DNS configuration is
268available.
269