convert to vimwiki
[robinkrens.nl] / vimwiki / FastD.wiki
1 = FastD =
2
3 == Redirecting traffic using FastD ==
4
5 FastD is a VPN daemon that has many features of OpenVPN and Tinc and is optimized for small code size and small number of dependencies. Fastd became popular on small devices like routers. In this tutorial we will configure a listening peer (alpha) and a connecting peer (anyremote). On a side note, with fastD you can setup mesh networks (n:n), as opposed to classical clients server networks (1:n). This configuration can be seen as a simple (1:1) setup between the listening alpha peer and our connecting client anyremote. All traffic from anyremote is redirected to alpha, making alpha the default gateway. This configuration has a lot of similarities with the tinc tutorial. Documentation and manual pages of fastd can be found here http://fastd.readthedocs.io
6
7 === Alpha peer ===
8
9 To run the daemon you only need one
10 configuration file. You can place it in
11 fastd's default directory /etc/fastd/fastd.conf. Here we show a standard configuration of fastd.conf with some minor changes:
12 {{{
13 # Log warnings and errors to stderr
14 log level warn;
15 # Log everything to syslog
16 log to syslog level debug;
17 # tunnel mode (default is tap). 
18 # We use tunneling mode, since we are dealing with routing
19 mode tun;
20 # Set the interface name
21 # you can use any name you like
22 # this is the name to configure your interface wit
23 interface "vpngateway";
24 # encryption method to use
25 falls back to null if salsa is not chosen.
26 method "salsa2012+umac";
27 method "null";
28 # Bind to a fixed port, IPv4 only
29 # If your remote ip is 1.2.3.4, make sure 1.2.3.4:10000 is accesible
30 bind 0.0.0.0:10000;
31 # Secret key generated by `fastd --generate-key`
32 # --generate-key outputs a file with a secret and public key
33 # secret key goes in here. Public keys is distributed amongst other peers
34 # read about PKI infrastructures if you don't know about this.
35 secret "supersecretkey";
36 # (see MTU selection documentation)
37 # base MTU is 1500 and you want to use TUN mode over IPv4 with any 
38 # crypto method: Choose 1500 - 52 = 1448 bytes.
39 mtu 1448;
40 # on up: shell script to configure the tun interface on daemon start
41 on up "./interface-up";
42 # on down: shell script when daemon is terminated
43 on down "./interface-down"; 
44 # Include peers from the directory 'peers'
45 # anyremote is a peer trying to connect to alpha
46 include peer "peers/anyremote";
47 }}}
48
49 Keys can be generate by running --generate-key (written to stdout):
50
51 {{{
52 root@alpha:~$ fastd --generate-key > keys
53 root@alpha:~$ cat keys
54 2018-04-30 19:25:57 +0800 --- Info: Reading 32 bytes from /dev/random...
55 Secret: 5035de5b4ea448b74e9a373765207095057a9485fd9dca5fadb9c1b86347bd75
56 Public: 8cb5e8d70d34f52716b6c4de518af2edfd6794e68ef1b3f0608cf05dd6a2ef42
57 }}}
58
59 The secret key needs to be added to the above fastd.conf file. The public needs to be spread amongst peers (as we explain later). on up "./interface-up" will run a simple shell script and configures our network interface vpngateway (make sure this script is executable). This is our interface.up script: We create a virtual IP: 172.16.16.1.
60
61 {{{
62 #!/bin/bash
63 ip link set $INTERFACE up
64 ip addr add 172.16.16.1/24 dev $INTERFACE
65 }}}
66
67 If we terminate fastd, we run a similar script as defined in interface-down:
68
69 {{{
70 #!/bin/sh
71 ip addr del 172.16.16.1/24 dev $INTERFACE
72 ip link set $INTERFACE down
73 }}}
74
75 Note: We will create the peer/anyremote file after we finished configuring anyremote and its public key
76
77 === Anyremote peer ===
78 Similar to the alpha host, we create /etc/fastd/fastd.conf. Since we only need to connect to alpha we don't need to bind to a fixed port.
79
80 {{{
81 # log arnings and errors to stderr
82 log level warn;
83 # Log everything to syslog
84 log to syslog level debug;
85 # tunnel mode (default is tap)
86 mode tun;
87 # Set the interface name
88 interface "vpngateway";
89 # Support salsa2012+umac and null methods, prefer salsa2012+umac
90 method "salsa2012+umac";
91 method "null";
92 # Secret key generated by `fastd --generate-key`
93 secret "supersecretkey";
94 # (see MTU selection documentation)
95 mtu 1448;
96 # daemon start
97 on up "./interface-up";
98 # daemon terminated
99 on down "./interface-down";
100 # if a connection is established set up the gateway
101 on establish "./set-gateway";
102 # if the connection is lost restore the default gateway
103 on disestablish "./restore-gateway";
104 # Include peers from the directory 'peers'
105 include peer "peers/alpha";
106 }}}
107
108 For anyremote we also need to generate a key pair and replace the "supersecretkey" with the secret key value. The public key will be given to alpha (explained in a little while)
109
110 {{{
111 root@anyremote~ $ fastd --generate-keys > anyremote-keypair
112 root@anyremote~ $ cat anyremote-keypair
113 2018-05-01 19:48:49 +0800 --- Info: Reading 32 bytes from /dev/random...
114 Secret: c0a611e0d4f3075b45cf172d3221c8427008e2c6f541b5b6adda0368cb79f271
115 Public: 2598c5d7e72f171731658ce35734ff7599e1840367422e1a9c5943c327ab5ea9
116 }}}
117
118 on up and on down are similar to alpha (except the ip address). interface-up:
119
120 {{{
121 #!/bin/bash
122 ip link set $INTERFACE up
123 ip addr add 172.16.16.2/24 dev $INTERFACE
124 }}}
125
126 interface-down:
127
128 {{{
129 #!/bin/sh
130 ip addr del 172.16.16.1/24 dev $INTERFACE
131 ip link set $INTERFACE down
132 }}}
133
134 We need to include some information about how to connect to alpha. We define in a file (/etc/fastd/peers/alpha):
135
136 {{{ 
137 root@anyremote:/etc/fastd/peers/ $ cat alpha
138 # alpha 
139 key "8cb5e8d70d34f52716b6c4de518af2edfd6794e68ef1b3f0608cf05dd6a2ef42";
140 remote 1.2.3.4:10000;
141 }}}
142 key here means the public key we just created with --generate-keys the alpha section. Here we add a remote ip to which anyremote tries to connect to. Make sure port numbers are the same. Don't forget to also add our our just created public key to our alpha server:
143
144 {{{
145 root@alpha:/etc/fastd/peers/ $ cat anyremote
146 # anyremote
147 key "2598c5d7e72f171731658ce35734ff7599e1840367422e1a9c5943c327ab5ea9";
148 }}}
149
150 This will allow alpha to accept connections from anyremote. Note: you don't need to specify a remote address, this will make it more dynamic and you can connect with anyremote from anywhere as long as you have the private key.
151
152 After these steps you should be able to run both alpha and anyremote. You can run the daemon as follows:
153
154 {{{
155 root@alpha:~ $ fastd -c /etc/fastd/fastd.conf &
156 root@anyremote:~ $ fastd -c /etc/fastd/fastd.conf &
157 }}}
158
159 The interface vpngateway should show up and you should be able to ping to both hosts us.
160
161 Now, in our config file of anyremote we see two additionals values: on establish and on disestablish. Once the connection is (dis)established, fastd will execute these scripts. This brings us two the last step: setting the default gateway of anyremote to point to alpha
162
163 === Alpha as gateway for anyremote ===
164
165 Have a look at the tinc tutorial (gateway section) about the theory of routing and gateways. We add the following scripts in /etc/fastd of anyremote if a connection with alpha is established: (set-gateway)
166
167 {{{ #!/bin/bash
168 #ip link set $INTERFACE up
169 #ip addr add 172.16.16.2/24 dev $INTERFACE
170 VPN_GATEWAY=172.16.16.1
171 ORIGINAL_GATEWAY=`ip route show | grep ^default | cut -d ' ' -f 2-5`
172 REMOTEADDRESS=1.2.3.4
173 ip route add $REMOTEADDRESS $ORIGINAL_GATEWAY
174 ip route add $VPN_GATEWAY dev $INTERFACE
175 ip route add 0.0.0.0/1 via $VPN_GATEWAY dev $INTERFACE
176 ip route add 128.0.0.0/1 via $VPN_GATEWAY dev $INTERFACE
177 }}}
178
179 And, similar, if the connecting is lost: (restore-gateway):
180
181 {{{
182 #!/bin/sh
183 #ip addr del 172.16.16.2/24 dev $INTERFACE
184 #ip link set $INTERFACE down
185 ORIGINAL_GATEWAY=`ip route show | grep ^default | cut -d ' ' -f 2-5`
186 REMOTEADDRESS=45.76.159.1
187 ip route del $REMOTEADDRESS $ORIGINAL_GATEWAY
188 ip route del $VPN_GATEWAY dev $INTERFACE
189 ip route del 0.0.0.0/1 dev $INTERFACE
190 ip route del 128.0.0.0/1 dev $INTERFACE
191 }}}
192
193 === Setup firewall ===
194
195 Make sure forwarding is enabled on alpha. Make sure you have masquerading or another form of routing set up on alpha. If you don't masquerade outgoing (forwarded anyremote) packets, the source address in in the TCP/UDP package will still remain 172.16.16.2. Please have a look here: http://www.tldp.org/LDP/nag2/x-087-2-ipmasq.html if you don't know about NAT and masquerading.
196
197 {{{ 
198 #!/bin/sh
199 # iptables config line to masquerade
200
201 echo "Enabling IPv4 forwarding"
202 echo 1 >/proc/sys/net/ipv4/ip_forward
203
204 echo "Appending Masquerade rule to iptables"
205 iptables -t nat -A POSTROUTING -s 172.16.16.0/255.255.255.0 -o eth0 -j MASQUERADE
206 }}}
207
208 I use iptables to masquerade the (-s) source address on the (-o) interface eth0.
209
210 === Test the gateway ===
211 Restart the daemon on alpha and anyremote. Use route -n to see check your routing tables. Ping both 172.16.16.1 and 1.2.3.4 (external address). In case of problems, trace the connections or analyze the data with tools like wireshark.
212
213 === Troubleshooting help ===
214
215 DNS request are not forwarded through the
216 gateway. Check your resolver config files
217 (/etc/resolv.conf). Debian-based systems
218 might have the following configuration:
219
220 {{{
221 root@anyremote:~$ cat /etc/resolv.conf  
222 # resolv.conf file
223 nameserver 127.0.1.0
224 }}}
225
226 and in your routing table you might have the following entry. A local / caching DNS server might still send packages to your router. Use wireshark to see if there are any DNS queries, not going to the VPN gateway
227
228 {{{
229 IP ROUTING TABLE
230 link-local      *               255.255.0.0     U     1000   0        0 wlp7s0
231 }}}
232
233 A simple fix would to change your resolv.conf and point it to nameserver 8.8.8.8 Fastd's log to /var/log/syslog You can define these locations in your fast.conf file. You can also change the log level, in case you need more information:
234
235 {{{
236 --log-level error|warn|info|verbose|debug|debug2
237 }}}
238
239 Sets the stderr log level; default is info if no alternative log
240 destination is configured. Use tcpdump or wireshark to analyze your network devices