Blame

e776ae Samuli Seppänen 2025-02-27 12:48:05 1
# Introduction 
2
3
For a brief introduction on bridging and routing, look at these links:
4
5
* [Determining whether to use a routed or bridged VPN](http://openvpn.net/index.php/open-source/documentation/howto.html#vpntype) (in OpenVPN HOWTO)
6
* [What are the fundamental differences between bridging and routing in terms of configuration?](http://openvpn.net/index.php/open-source/faq/75-general/311-what-are-the-fundamental-differences-between-bridging-and-routing-in-terms-of-configuration.html) (in OpenVPN FAQ)
7
* [OpenVPN Routing](http://www.secure-computing.net/wiki/index.php/OpenVPN/Routing) (in Secure-Computing Wiki)
8
9
**NOTE:** The remaining sections are mostly based on [this email for dazo](http://thread.gmane.org/gmane.network.openvpn.user/32935).
10
11
# Bridging vs. routing
12
13
This discussion needs to start with TAP vs TUN devices. You want TAP if:
14
15
* You want to transport non-IP based traffic, or IPv6 traffic on OpenVPN 2.2 or older releases
16
* You want to bridge
17
18
And you want to bridge if:
19
20
* You want your LAN and VPN clients to be in the same broadcast domain
21
* You want your LAN DHCP server to provide DHCP addresses to your VPN client
22
* You have Windows server(s) you want to access and require network neighbourhood discovery to work via VPN **and** WINS is not an option to implement. If you have WINS, you don't want bridging. Really.
23
24
It might be a few more reasons, but these are the most typical ones. And as you see, TAP is a requirement for bridging. TUN devices cannot be used for bridges and non-IP traffic.
25
26
Bridging looks easier at first glance, but it brings a completely different can of worms. Make no mistake: **There are no shortcuts in making networking easier** - except learning how to do it properly.
27
28
Now lets see benefits and drawbacks of TAP vs TUN.
29
30
TAP benefits:
31
32
* behaves like a real network adapter (except it is a virtual network adapter)
33
* can transport any network protocols (IPv4, IPv6, Netalk, IPX, etc, etc)
34
* Works in layer 2, meaning Ethernet frames are passed over the VPN tunnel
35
* *Can* be used in bridges
36
37
TAP drawbacks
38
39
* causes much more broadcast overhead on the VPN tunnel
40
* adds the overhead of Ethernet headers on all packets transported over the VPN tunnel
41
* scales poorly
42
* can not be used with Android or iOS devices
43
44
TUN benefits:
45
* A lower traffic overhead, transports only traffic which is destined for the VPN client
46
* Transports only layer 3 IP packets
47
48
TUN drawbacks:
49
50
* Broadcast traffic is not normally transported
51
* Can only transport IPv4 (OpenVPN 2.3 adds IPv6)
52
* **Cannot** be used in bridges
53
54
Please understand that in both setups, **basic networking knowledge is a must**. You do need to understand basic network *routing* and *firewalling*, no matter if you use routing, bridging, TUN or TAP. Both TUN and TAP devices supports traditional network routing, so you are not required to use bridging with TAP. But using bridges, you need in addition to know how bridges work and how this changes your firewalling. To say it simple: Bridging will complicate your setup further. Of course, there are scenarios where bridging really is the right solution. But in most cases you will most likely solve your VPN setup with basic routing.
55
685cee Samuli Seppänen 2025-02-27 12:49:41 56
For more information about TCP/IP networking, the [TCP/IP Tutorial and Technical Overview](http://www.redbooks.ibm.com/redbooks/pdfs/gg243376.pdf) (IBM Red Book) is recommended reading, especially chapter 3.1.
e776ae Samuli Seppänen 2025-02-27 12:48:05 57
58
# Using routing
59
60
```
61
<disclaimer>
62
NONE OF THIS IS TESTED. But this is the basic theory behind it.
63
It might be syntax errors here, or other stupid mistakes.
64
</disclaimer>
65
```
66
67
To set up a TUN setup with routing and masquerading for the VPN subnet, one approach could be something like this. This example is based on a pretty standard network with a single Linux based firewall with two Ethernet cards:
68
69
```
685cee Samuli Seppänen 2025-02-27 12:49:41 70
. +--------------------------------+
e776ae Samuli Seppänen 2025-02-27 12:48:05 71
| FIREWALL |
72
(public IP)| |192.168.0.1
73
{INTERNET}=============={eth1 eth0}=============<internal network / 192.168.0.0/24>
74
| \ / |
75
| +----------------------+ |
76
| | iptables and | |
77
| | routing engine | |
78
| +--+----------------+--+ |
79
| |*1 |*2 |
80
| (openvpn)-------{tun0} |
81
| 10.8.0.1 |
82
+--------------------------------+
83
84
*1 Only encrypted traffic will pass here, over UDP or TCP and only to the remote OpenVPN client
85
*2 The unencrypted traffic will pass here. This is the exit/entry point for the VPN tunnel.
86
```
87
88
Here tun0 is configured as 10.8.0.1 as a VPN, with the whole VPN network configured as 10.8.0.0/24.
89
90
What happens with OpenVPN is that it accepts OpenVPN clients from eth1, OpenVPN will decrypt the data and put it to the tun0 interface, and the iptables and routing engine will pick up that traffic again, filter/masquerade it and send it further to eth0 or eth1, depending on the routing table. When the routing engine sends traffic destined for the tun0 network, OpenVPN will pick it up, encrypt it and send it out on eth1, towards the proper OpenVPN client.
91
92
First we need to be sure that IP forwarding is enabled. Very often this is disabled by default. This is done by running the following command line as root:
93
94
```
95
[root@host ~] # sysctl -w net.ipv4.ip_forward=1
96
net.ipv4.ip_forward = 1
97
[root@host ~] #
98
```
99
100
This change is only temporary, so if you reboot your box this will be reset back to the default value. To make this change persistent you need to modify */etc/sysctl.conf*. In this file you should have a line stating:
101
102
```
103
net.ipv4.ip_forward = 1
104
```
105
106
So, lets look at the iptables rules required for this to work.
107
108
```
109
# Allow traffic initiated from VPN to access LAN
110
iptables -I FORWARD -i tun0 -o eth0 \
111
-s 10.8.0.0/24 -d 192.168.0.0/24 \
112
-m conntrack --ctstate NEW -j ACCEPT
113
114
# Allow traffic initiated from VPN to access "the world"
115
iptables -I FORWARD -i tun0 -o eth1 \
116
-s 10.8.0.0/24 -m conntrack --ctstate NEW -j ACCEPT
117
118
# Allow traffic initiated from LAN to access "the world"
119
iptables -I FORWARD -i eth0 -o eth1 \
120
-s 192.168.0.0/24 -m conntrack --ctstate NEW -j ACCEPT
121
122
# Allow established traffic to pass back and forth
123
iptables -I FORWARD -m conntrack --ctstate RELATED,ESTABLISHED \
124
-j ACCEPT
125
126
# Notice that -I is used, so when listing it (iptables -vxnL) it
127
# will be reversed. This is intentional in this demonstration.
128
129
# Masquerade traffic from VPN to "the world" -- done in the nat table
130
iptables -t nat -I POSTROUTING -o eth1 \
131
-s 10.8.0.0/24 -j MASQUERADE
132
133
# Masquerade traffic from LAN to "the world"
134
iptables -t nat -I POSTROUTING -o eth1 \
135
-s 192.168.0.0/24 -j MASQUERADE
136
```
137
138
Again, these changes are only temporary. If you reboot your box, these rules will be cleared out. Please read the documentation to your distribution on how to save the iptables configuration. In worst case you can also use *iptables-save* and *iptables-restore* to dump and restore the iptables configuration to/from a file.
139
140
```
141
# Save the iptables setup
142
[root@host ~] # iptables-save > iptables-dump.ipt
143
144
# Restore the iptables setup
145
[root@host ~] # iptables-restore < iptables-dump.ipt
146
```
147
148
*(This iptables dump can also easily be edited manually if you want to do bigger changes in one go. Just use iptables-restore on the modified file to activate your new iptables configuration.)*
149
150
In the openvpn server config you will need these lines:
151
152
```
153
dev tun
154
topology subnet
155
server 10.8.0.0 255.255.255.0
156
push "route 192.168.0.0 255.255.255.0"
157
```
158
159
*(this is not a complete configuration file, but it should cover the network part of the configuration)*
160
161
This will provide the needed route for all VPN clients to the internal LAN. If you want to all your VPN clients to send all the internet traffic via the VPN as well (so it looks like they sit behind the LAN when surfing the net), you need this line in addition:
162
163
```
164
push "redirect-gateway def1"
165
```
166
167
And that's basically it. Not much more extra trickery. Routing setups are often much easier than people generally believe. The firewall is generally a bit more tricky, but bridging doesn't make that easier.
168
169
# Using routing and OpenVPN not running on the default gateway
170
This setup have much of the same requirements as the previous example. But there are a few minor modifications you need to make.
171
172
```
685cee Samuli Seppänen 2025-02-27 12:49:41 173
. +-------------------------+
e776ae Samuli Seppänen 2025-02-27 12:48:05 174
(public IP)| |
175
{INTERNET}=============={ Router |
176
| |
177
| LAN switch |
178
+------------+------------+
179
| (192.168.0.1)
180
|
181
| +-----------------------+
182
| | |
183
| | OpenVPN | eth0: 192.168.0.10/24
184
+--------------{eth0 server | tun0: 10.8.0.1/24
185
| | |
186
| | {tun0} |
187
| +-----------------------+
188
|
189
+--------+-----------+
190
| |
191
| Other LAN clients |
192
| |
193
| 192.168.0.0/24 |
194
| (internal net) |
195
+--------------------+
196
```
197
198
The Router needs to have a port forwarding for the port you want to use for OpenVPN and forward that port to 192.168.0.10, which is the IP address of the OpenVPN on the internal network.
199
200
The next thing you need to do on the router is to add a route for your VPN subnet. In the routing table on your router, add 10.8.0.0/24 to be sent via 192.168.0.10. This is needed for the traffic from your LAN clients to be able to find their way back to the VPN clients. If this is not possible, you need add such routes explicitly on all the LAN clients you want to access via the VPN.
201
202
The firewall rules will also need to be different, and less extensive. Here you just need to add rules which opens up traffic from the VPN subnet and into your local LAN.
203
204
```
205
# Allow traffic initiated from VPN to access LAN
206
iptables -I FORWARD -i tun0 -o eth0 \
207
-s 10.8.0.0/24 -d 192.168.0.0/24 \
208
-m conntrack --ctstate NEW -j ACCEPT
209
210
# Allow established traffic to pass back and forth
211
iptables -I FORWARD -m conntrack --ctstate RELATED,ESTABLISHED \
212
-j ACCEPT
213
```
214
215
If you also want your VPN clients to access the complete Internet, just remove the *-d 192.168.0.0/24* part from the first iptables example above.
216
217
In some situations it is not possible to modify the routing table on the main router or on each client. Then the alternative is to masquerade all VPN clients as coming from 192.168.0.10. The drawback of this approach is that all VPN clients looks like coming from the VPN server itself - you will **not** see the IP address of the VPN client at all. This approach is generally considered as a last option if proper routing is not feasible.
218
219
```
220
# Masquerade all traffic from VPN clients -- done in the nat table
221
iptables -t nat -I POSTROUTING -o eth0 \
222
-s 10.8.0.0/24 -j MASQUERADE
223
```
224
225
The rest of the configuration will be as the very first routing example. You need to set net.ipv4.ip_forward=1 and you need the extracts for the OpenVPN configuration as indicated.
226
227
228
*(If others see obvious mistakes, typos, or there are important details which are missing, please correct my errors.)*