Projet

Général

Profil

Openbsd wireguard rdomain » Historique » Version 3

sacha, 11/11/2020 21:49

1 2 sacha
2 1 sacha
# Setting up a WireGuard® client with routing domains on OpenBSD
3
4
_Matthieu Herrb - last edited october 24, 2020._
5
6
OpenBSD-current recently got native support for the [Wireguard®](https://wireguard.com/) VPN, thanks to the work of Matt Dunwoodie and Jason A. Donefeld. Their code was merged by David Gwynne.
7
This is great, especially since I've been using WireGuard for various purposes already.
8
A local assocative ISP here in Toulouse is offering WireGuard based VPNs for 5€/month.
9
In this note, I'm going to show how to setup a machine running OpenBSD to connect to this kind of VPN, using routing domains to make the VPN the default interface.
10
11
This configuration can be used to get a fixed public IPv4 and IPv6 address for a self-hosted server to run any kind of internet services.
12
13
## General principle
14
15
The client machine is going to connect to the internet via any kind of LAN that provides IPv4 DHCP auto-configuration.
16
It can be an ADSL or fiber modem at home or some enterprise network.
17
The only serious requirement is that UDP traffic on the WireGuard (51820) port goes out.
18
This connexion is only going to be used to connect to the VPN, and will be setup in routing domain 1.
19
The Wireguard connexion is going to be the _egress_ connexion in the default routing domain (0).
20
21
In this setup there is no need to provide complicated routing or packet filter configuration.
22
All traffic will go through the VPN "naturally" as the default routing domain only knows about the VPN interface.
23
24
### Routing Domains and routing tables
25
26
For those not familiar with routing domains and tables on OpenBSD, it is recommended to read the [rdomain(4)](https://man.openbsd.org/rdomain.4) manual page.
27
Routing domains are separated network address spaces in the OpenBSD kernel.
28
Each routing domain has at least one routing table, separated from the routing tables of other domains.
29
Network interfaces belong to one domain and are not usable by the others.
30
Also, processes are running inside a routing domain, using by default the default routing table of their domain.
31
So their network traffic can only use the interfaces in this domain.
32
33
So routing domains and tables are a great way to isolate network trafic between processes.
34
35
### WireGuard® at Tetaneutral.net
36
37
The [Tetaneutral.net](https://www.tetaneutral.net/) non-profit ISP provides WireGuard based VPN connections with one fixed IPv4 address and a /56 IPv6 prefix.
38
When you subscribe to the service, you provide the public key for your end point, and Tetaneutral gives you back the assigned IP adresses and the IP address, port number and public key of the VPN endpoint.
39
40
It is likely that many other ISP providing WireGuard based VPNs will work approxymately the same as Tetaneutral, and thus this can be expaned easily.
41
You can even setup your own WireGuard VPN service using OpenBSD, but this is out of the scope of this note.
42
43
## Let's go !
44
45
### OpenBSD installation
46
47
OpenBSD is installed as usual on the machine, setting up the network connection for the network on which it is going to have its internet access.
48
In this example the __re0__ interface is going to be used for this. So with DHCP `/etc/hostname.re0` looks like:
49
50
    dhcp
51
52
### Obtaining VPN parameters
53
54
Install the `wireguard-tools` package, to have access to the [wg(8)](https://git.zx2c4.com/wireguard-tools/about/src/man/wg.8) command:
55
56
    $ pkg_add wireguard-tools
57
58
Now the parameters for the VPN are needed. For this a private key will be generated using the following commands:
59
60
    $ umask 077
61
    $ wg genkey > /etc/wg0.key
62
63
The corresponding public key is displayed by the following command:
64
65
    $ wg pubkey < /etc/wg0.key
66
67
_Note:_ if you don't want to install the `wireguard-tool` package, the [wg(4)](https://man.openbsd.org/wg.4) manual page describes an alternate way to generate the private key and display the public key using tools in the base system.
68
69
To obtain the remaining of the parameters, contact the VPN provider and send them the public key shown above.
70
They should reply providing:
71
72
(the values below are fake examples)
73
74
- the IP and port number of their endpoint: `198.51.100.116 51820`
75
- the public key of their endpoint: `Qay+wLckmRMr08t/9psjhrltJ0T4xb0m8++gQj0FpEA=`
76
- the IPv4 and IPv6 prefix assigned to your endpoint: `203.0.113.3/32` and `2001:db8:8087:300::/56` respectively.
77
- the addresses of the DNS servers that can be used inside the VPN: `198.31.100.10` and `198.51.100.11`
78
79
Keep those informations somewhere, to use them later.
80
81
### Moving the default interface to rdomain 1
82
83
Now edit `/etc/hostname.re0` to add a `rdomain` directive to move this interface to rdomain 1:
84
85
    rdomain 1
86
    dhcp
87
88
That's it. If the machine is rebooted at this point, most processes won't have network access since only routing domain 1 has network access. So do not reboot for now.
89
90
### Creating the VPN interface
91
92
A __wg0__ interface for the VPN is going to be setup. For this create the `/etc/hostname.wg0` file, containing (using the fake information form the provider shown above):
93
94
    up
95
    wgkey <your wg private key>
96
    wgpeer Qay+wLckmRMr08t/9psjhrltJ0T4xb0m8++gQj0FpEA= wgrtable 1 wgendpoint 198.51.100.116 51820 wgaip 0.0.0.0/0 wgaip ::/0
97
    inet 203.0.113.3 255.255.255.255
98
    inet6  2001:db8:8087:300::1 56
99
    ! route add default -link -iface wg0
100
    ! route add -inet6 default -link -iface wg0
101
102
The last two lines set up the default route for rdomain 0 via the `wg0` interface.
103
104
Once `/etc/hostname.wg0` is created, the `/etc/wg0.key` file created previously can be removed.
105
It was only used to keep the private key while obtaining the remaining information needed to create `hostname.wg0`
106
107
### Setting up the DNS
108
109
There are different options to tell the VPN client which DNS resolvers to use.
110
The issue is that `/et/resolv.conf` still contains the adresses of the DNS servers on the LAN, provided by dhclient(8).
111
Generally when using a VPN you don't want to use those, because they will leak the domains that the users of the VPN are connecting to.
112
113
#### Simple forwarding
114
115
The simplest solution is to use the _supersede_ option of [`dhclient.conf(5)`](https://man.openbsd.org/dhclient.conf.5) to replace the DHCP provided servers by the ones provided by the VPN provider. This will be the contents of `/etc/dhclient.conf`:
116
117
    supersede domain-name-servers 198.51.100.10, 192.51.100.11;
118
119
#### Using unwind(8)
120
121
Another option is to set up [unwind(8)](https://man.openbsd.org/unwind.8), with an [`unwind.conf(5)`](https://man.openbsd.org/unwind.conf.5) configuration file specifying the VPN name servers as forwarders.
122
123
Add the DNS servers provided by the VPN provider in `/etc/unwind.conf` (see [`unwind.conf(5)`](https://man.openbsd.org/unwind.conf.5) for details):
124
125
    forwarder { 198.51.100.10 192.51.200.11 }
126
    
127
Activate unwind with
128
129
    rcctl enable unwind
130
    
131
And edit `/etc/dhclient.conf` to contain:
132
133
    prepend domain-name-servers 127.0.0.1 ;
134
    
135
This solution also allows processes in routing domain 1 to use the DNS servers provided by the DHCP server. 
136
137
### SSH access from the LAN
138
139
If you want to be able to continue accessing the machine from the lan it is connected to, `/etc/ssh/sshd_config` needs to be told to also listen to routing domain 1.
140
141
    ListenAddress 0.0.0.0 rdomain 0
142
    ListenAddress :: rdomain 0
143
144
    ListenAddress 0.0.0.0 rdomain 1
145
146
Otherwise, after reboot, the machine will only be accessible from the console and with SSH though the VPN.
147
148
### Rebooting
149
150
And that's it. The machine can now be rebooted.
151
All processes in the default routing domain (0) will only see the `lo0` and `wg0` interfaces, while the dhclient process needed to bring up `re0` will be in routing domain 1.
152
The `wgrtable` keyword in the wg0 interface configuration tells it ue use the routing table of routing domain 1 to reach the remote VPN endpoint.
153
154
## Checking that it works
155
156
After rebooting, let's review the setup by cheching the `wg0` interface:
157
158
    # ifconfig wg0
159
    wg0: flags=80c3<UP,BROADCAST,RUNNING,NOARP,MULTICAST> mtu 1420
160
        index 6 priority 0 llprio 3
161
        wgport 47326
162
        wgrtable 1
163
        wgpubkey <public key>
164
        wgpeer Qay+wLckmRMr08t/9psjhrltJ0T4xb0m8++gQj0FpEA=
165
                wgendpoint 198.51.100.116 51820
166
                tx: 45564, rx: 71200
167
                last handshake: 107 seconds ago
168
                wgaip ::/0
169
                wgaip 0.0.0.0/0
170
        groups: wg egress
171
        inet  203.0.113.3 netmask 0xffffffff
172
        inet6 2001:db8:8087:300::1 prefixlen 56
173
174
And the default routes:
175
176
    # route -n show  | grep default
177
    default            link#6             ULS        5      229     -     8 wg0  
178
    default                            link#6                         ULS        0        4     -     8 wg0
179
180
You can test that all traffic will go through the VPN using [traceroute(8)](https://man.openbsd.org/traceroute.8).
181
The machine is reachable from the internet using both its public IPv4 and IPv6 addresses.
182
183
Finally, the `ps` command with option `-o rtable` will show routing table information in the last column: 
184
185
    # ps xau -o rtable
186
    USER       PID %CPU %MEM   VSZ   RSS TT  STAT   STARTED       TIME COMMAND          RTABLE
187
    root         1  0.0  0.0   468   408 ??  I       4:43PM    0:01.02 /sbin/init            0
188
    root     95458  0.0  0.0   788   620 ??  Ip      4:43PM    0:00.24 /sbin/slaacd          0
189
    _slaacd  30350  0.0  0.0   792   676 ??  Ip      4:43PM    0:00.08 slaacd: engine (      0
190
    _slaacd  69167  0.0  0.0   800   720 ??  Ip      4:43PM    0:00.08 slaacd: frontend      0
191
    root     59774  0.0  0.0   704   528 ??  IU      4:43PM    0:00.02 dhclient: re0 [p      1
192
    _dhcp    30649  0.0  0.0   828   656 ??  Ip      4:43PM    0:02.77 dhclient: re0 (d      1
193
    root     35197  0.0  0.1   548  2252 ??  IpU     4:44PM    0:00.02 syslogd: [priv]       0
194
    _syslogd  1991  0.0  0.1  1120  1384 ??  Ip      4:44PM    0:00.14 /usr/sbin/syslog      0
195
    root     76166  0.0  0.0   836   584 ??  IU      4:44PM    0:00.01 pflogd: [priv] (      0
196
    _pflogd   9499  0.0  0.0   884   564 ??  Sp      4:44PM    0:01.26 pflogd: [running      0
197
    _ntp     45580  0.0  0.1  1044  2872 ??  I<p     4:44PM    0:00.72 ntpd: ntp engine      0
198
    _ntp     59996  0.0  0.1   888  2560 ??  Ip      4:44PM    0:00.09 ntpd: dns engine      0
199
    root     30954  0.0  0.1   848  1616 ??  I<pU    4:44PM    0:00.06 /usr/sbin/ntpd        0
200
    root     75315  0.0  0.1  1328  1496 ??  I       4:44PM    0:00.16 sshd: /usr/sbin/      0
201
    root     60900  0.0  0.1  1716  2028 ??  Ip      4:44PM    0:00.07 /usr/sbin/smtpd       0
202
    _smtpd   65700  0.0  0.2  1448  3832 ??  Ip      4:44PM    0:00.08 smtpd: klondike       0
203
    _smtpd   90699  0.0  0.2  1656  4044 ??  Ip      4:44PM    0:00.07 smtpd: control (      0
204
    _smtpd   31369  0.0  0.2  1536  3936 ??  Ip      4:44PM    0:00.07 smtpd: lookup (s      0
205
    _smtpd   45982  0.0  0.2  1596  4064 ??  Ip      4:44PM    0:00.06 smtpd: pony expr      0
206
    _smtpq   17106  0.0  0.2  1664  4048 ??  Ip      4:44PM    0:00.08 smtpd: queue (sm      0
207
    _smtpd   94920  0.0  0.2  1444  3872 ??  Ip      4:44PM    0:00.05 smtpd: scheduler      0
208
    _sndiop  35803  0.0  0.0   524   936 ??  IpU     4:44PM    0:00.00 sndiod: helper (      0
209
    _sndio   61376  0.0  0.0   536   808 ??  I<p     4:44PM    0:00.00 /usr/bin/sndiod       0
210
    root     97224  0.0  0.0   204   836 ??  I       4:44PM    0:00.01 /usr/sbin/apmd -      0
211
    root     98386  0.0  0.1   668  1312 ??  Sp      4:44PM    0:00.08 /usr/sbin/cron        0
212
    root     27323  0.0  0.0   940   932 p0  Ip      5:13PM    0:00.36 -ksh (ksh)            0
213
    root     49147  0.0  0.0   484   432 p0  R+pU/3  6:30PM    0:00.00 ps -xau -o rtabl      0
214
    root     42404  0.0  0.1   316  1312 C1  I+pU    4:44PM    0:00.03 /usr/libexec/get      0
215
    root     52228  0.0  0.1   320  1308 C2  I+pU    4:44PM    0:00.02 /usr/libexec/get      0
216
    root     71695  0.0  0.1   316  1308 C3  I+pU    4:44PM    0:00.03 /usr/libexec/get      0
217
    root     90445  0.0  0.1   320  1320 C5  I+pU    4:44PM    0:00.03 /usr/libexec/get      0
218
219
Only the dhclient(8) processes are in the non-default routing domain 1.
220
221
## What's next ?
222
223
As already said above, the machine is now reachable from the internet.
224
So perhaps [pf(4)](https://man.openbsd.org/pf.4) needs to be configured to be more restrictive. 
225
[sshd(8)](https://man.openbsd.org/sshd.8) can be configured to refuse password based authentication, in order to avoid brute force attacks.
226
227
You can then point a domain (leased from a registrar) to the IP addresses of the machine and [httpd(8)](https://man.openbsd.org/httpd.8) can be set-up to provide web based services on this domain.
228
229
Or another interface can be added to that host to convert it into a router to connect to another network.
230
This can be either another physical interface or a vlan interface over the existing re0 interface.