e8d9064ffc5a33260965c14ad80f962251fc85d6
[librecmc/librecmc.git] / docs / Layer_2_OpenVPN.md
1 # Layer 2 OpenVPN
2
3 ## Introduction
4
5 Librecmc can operate as an OpenVPN server. OpenVPN technology connects
6 two networks via an encrypted tunnel. With proper server, network, and
7 client configuration, OpenVPN allows a client outside of your LAN to
8 see the LAN as though it were physically connected to the LAN.
9
10 OpenVPN can run in layer 2 or layer 3 mode. In layer 3 mode, the
11 remote client sees your LAN as though it is on the other side of an IP
12 router. In layer 2 mode, the remote client sees your LAN as though
13 they are both on the same Data Link segment (e.g., the same Ethernet
14 link). Layer 3 mode is easier to set up, but layer 2 mode is sometimes
15 desired to give clients a more direct exposure to services on the LAN.
16
17 The scenario we are targeting here is to have:
18
19 - An OpenVPN server running on a libreCMC system which can receive
20   traffic from the Internet.
21 - An OpenVPN client running on a libreCMC system somewhere else in the
22   Internet. The client can be hidden behind a NAT.
23
24 ## Warnings
25
26 This information is provided for educational purposes only and is not
27 meant to be a guide to best network security practices. Readers are
28 advised to study all relevant OpenVPN and network security
29 documentation.
30
31 ## Server Setup and Configuration
32
33 ### Required LibreCMC packages
34
35 * openvpn-openssl
36 * openvpn-easy-rsa
37 * luci-app-openvpn
38
39 ### Interface Setup
40
41 In LuCi, select `Network` >> `Interfaces` and then `Add New Interface`.
42
43 - Set `Name of the new interface` to `l2server`.
44 - Set `Protocol of the new interface` to unmanaged.
45 - Set `Cover the following interface` to `Custom Interface: vpn0`.
46
47 In my working configuration, I added tap0 into the LAN bridge
48 interface, and deleted the WAN interface. However, my vpn server is a
49 separate unit on my network, intended to operate in "bridge mode",
50 where if you server is your gateway router, a different configuration
51 might be necessary.
52
53 ### Certificate and Key Setup Instructions
54
55 ```
56 cd /etc/easy-rsa
57 source vars
58 clean-all
59 build-ca
60 build-dh
61 build-key-server l2server
62 openvpn --genkey --secret /etc/easy-rsa/keys/ta.key
63 mkdir -m 700 /etc/openvpn/keys
64 mv ca.crt l2server.crt l2server.key dh2018.pem /etc/openvpn/keys
65 ```
66
67 N.B.: Using easy-rsa is a straightforward approach, but it may be
68 possible to produce more secure certificates using openssl directly.
69
70 ### Generating a Client Key
71
72 ```
73 build-key l2client
74 ```
75
76 In the server-client configuration we are aiming for here, your client
77 key needs to be unencrypted (i.e., not password protected).
78
79 ### Server configuration
80
81 For the `server bridge` option: The first two parameters are the ip
82 and netmask of the gateway on the bridged subnet. The next two
83 parameters indicate the pool-start-IP and pool-end-IP, which is the
84 part of your IP address pool that you have reserved just for VPN
85 clients. You must to make sure that the DHCP server for your LAN is
86 not leasing out those IP addresses to local (non-vpn) clients.
87
88 ```
89 uci set openvpn.l2server=openvpn
90 uci set openvpn.l2server.enabled='1'
91 uci set openvpn.l2server.dev='tap0'
92 uci set openvpn.l2server.port='1194'
93 uci set openvpn.l2server.proto='udp'
94 uci set openvpn.l2server.keepalive='10 120'
95 uci set openvpn.l2server.persist_key='1'
96 uci set openvpn.l2server.persist_tun='1'
97 uci set openvpn.l2server.user='nobody'
98 uci set openvpn.l2server.group='nogroup'
99 uci set openvpn.l2server.ca='/etc/openvpn/keys/ca.crt'
100 uci set openvpn.l2server.cert='/etc/openvpn/keys/l2server.crt'
101 uci set openvpn.l2server.key='/etc/openvpn/keys/l2server.key'
102 uci set openvpn.l2server.dh='/etc/openvpn/keys/dh2048.pem'
103 uci set openvpn.l2server.tls_server='1'
104 uci set openvpn.l2server.tls_auth='/etc/openvpn/keys/ta.key 0'
105 uci set openvpn.l2server.server_bridge='192.168.1.1 255.255.255.0 192.168.1.201 192.168.1.220'
106 uci set openvpn.l2server.client_to_client='1'
107 uci set openvpn.l2server.push='persist-key' 'persist-tun' 'redirect-gateway def1'
108 uci set 'route 192.168.1.0 255.255.255.0' 'dhcp-option DNS 192.168.1.1'
109 uci set openvpn.l2server.mute='15'
110 uci set openvpn.l2server.verb='3'
111 uci commit
112 ```
113
114 ### Firewall
115
116 I do not want to describe firewall adjustments in this document, but
117 the important point is that your server needs to be able to receive
118 UDP packages from the Internet on port 1194.
119
120 ## Client Setup and Configuration
121
122 This assumes the above configuration for the server.
123
124 ### Required LibreCMC packages
125
126 * openvpn-openssl
127 * luci-app-openvpn
128
129 ### Interface setup
130
131 In LuCi, select `Network` >> `Interfaces` and then `Add New Interface`.
132
133 - Set `Name of the new interface` to `l2client`.
134 - Set `Protocol of the new interface` to unmanaged.
135 - Set `Cover the following interface` to `Custom Interface: vpn0`.
136
137 Adjust the LAN interface so that it bridges over the `vpn0` physical
138 interface as well as the default `eth0` and `wlan0` interfaces. This
139 is done from the `Network` >> `Interfaces` menu, pressing the `Edit`
140 button next to `LAN`, and selecting the `Physical Settings` tab.
141
142 ### Certificate and key storage
143
144 ```
145 mkdir -m 700 /etc/openvpn/keys
146 ```
147
148 Client will the need `ca.crt`, `l2client.crt`, `l2client.key', and
149 `ta.key` you generated in the server section, stored in the
150 `/etc/openvpn/keys` directory.
151
152 ### Client configuration
153
154 ```
155 uci set openvpn.l2client=openvpn
156 uci set openvpn.l2client.float='1'
157 uci set openvpn.l2client.client='1'
158 uci set openvpn.l2client.dev='tap'
159 uci set openvpn.l2client.reneg_sec='0'
160 uci set openvpn.l2client.persist_key='1'
161 uci set openvpn.l2client.nobind='1'
162 uci set openvpn.l2client.remote_cert_tls='server'
163 uci set openvpn.l2client.remote='remote.alaskasi.com'
164 uci set openvpn.l2client.proto='udp'
165 uci set openvpn.l2client.rport='1194'
166 uci set openvpn.l2client.resolv_retry='infinite'
167 uci set openvpn.l2client.mute_replay_warnings='1'
168 uci set openvpn.l2client.key_direction='1'
169 uci set openvpn.l2client.redirect_gateway='def1'
170 uci set openvpn.l2client.enabled='1'
171 uci set openvpn.l2client.ca='/etc/openvpn/keys/ca.crt'
172 uci set openvpn.l2client.cert='/etc/openvpn/keys/l2client.crt'
173 uci set openvpn.l2client.key='/etc/openvpn/keys/l2client.key'
174 uci set openvpn.l2client.tls_auth='/etc/openvpn/keys/ta.key 1'
175 uci set openvpn.l2client.mute='15'
176 uci set openvpn.l2client.verb='3'
177 uci commit
178 ```
179
180 ## Troubleshooting
181
182 You are likely to run into one of two issues:
183
184 - Either your client or your server is not receiving UDP packets from the other.
185 - You have an error in the server or client configurations.
186 - There is some problem with the keys or certificates, or they are
187   in the wrong location.
188
189 These are some useful tools:
190
191 - Without OpenVPN even running, you can use the `nc` program (netcat)
192   to send UDP packets from the client to the server, and then use the
193   `tcpdump` program on the server to see if the UDP packets are
194   arriving at port 1194. The syntax of these programs will not be
195   covered in this document.
196
197 - The log output on the server and on the client is very helpful. Run
198   `logread` to view the log or `logread && logread -f` to monitor for
199   log messages. If you find an OpenVPN error, use that in conjunction
200   with the OpenVPN manual page, to figure out what needs to be
201   tweaked.[1]
202
203 [1] [https://openvpn.net/index.php/open-source/documentation/manuals.html]