Blame

050f47 Samuli Seppänen 2025-02-28 13:51:53 1
**NOTE:** this article is very old. That said, most of the content here is very generic and is still applicable as of 2025.
2
3
# Bridging in OpenVPN
4
5
OpenVPN allows two different modes of operation: *routed mode* and *bridged mode*. This article is about the latter.
6
7
*Bridged mode* means that the VPN tunnel encapsulates full ethernet frames (up to 1514 bytes long), rather than IP packets (up to 1500 bytes). In itself, this would just add some overhead to the VPN traffic; but in practice, together with some special configuration in the OS (described later), this allows to connect the VPN and its users to a real, physical ethernet network at the data-link level, effectively turning the whole system (ethernet network + VPN) into a single broadcast domain.
8
9
## Do you need bridging?
10
11
This question comes up regularly when somebody asks for advice in configuring a bridged VPN. Bridged mode has a higher traffic overhead, since it works at layer 2 and as such broadcasts are sent into the VPN, and also, as already mentioned, data packets can be up to 1514 bytes. Normally bridged mode is needed only in two cases:
12
13
* You really need to create a layer 2 domain. This may be because you need to use protocols that rely on broadcasts or multicasts (eg netBIOS, LAN games)
14
* You need to transport non-IP traffic (eg IPX, AppleTalk)
15
16
If you don't have any of those requirements, you can almost certainly use routed mode. Otherwise, read on.
17
18
## Bridge setup
19
20
For this example, we will assume the following scenario:
21
22
```
23
+---------+
24
| client1 +--------\ +---------------------------------
25
+---------+ \ +-------------+ / LAN
26
\ + + / 192.168.111.0/24
27
+---------+ + + +---------+
28
| client2 +-----------+ INTERNET +---- ppp0 + gateway +eth0
29
+---------+ + + +---------+ (192.168.111.254/24)
30
/ + + \
31
+---------+ / +-------------+ \
32
| clientN +---------/ +---------------------------------
33
+---------+
34
```
35
36
We want VPN clients to connect and feel as if they were physically on the ethernet network, including using IP addresses in the 192.168.111.0/24 range, just like the stations in the LAN.
37
38
The first step to achieve this goal is to create a special interface on the VPN gateway. This interface (also known as a bridge) is what connects, or bridges, together the "real" layer 2 domain (ie the LAN) and the layer 2 VPN. Basically a bridge like this can be thought as a mini-ethernet switch internal to the OS, whose ports are connected to ethernet interfaces on the host. So for example, on a host with two ethernet interfaces (eth0 and eth1), a two-ports bridge could be created by enslaving eth0 and eth1 into the special bridge interface (let's call it br0). The enslaved interfaces can usually be added and removed dynamically from the bridge.
39
The bridge interface behaves like an ethernet switch (well, because it effectively *is* an ethernet switch): it learns which MAC addresses are on each ports, and forwards frames accordingly (broadcasts and unknown MACs are flooded on all ports). The interfaces that are enslaved into the bridge (eth0 and eth1 in our example) operate purely at layer 2, and can not have IP addresses of their own. However, the bridge interface can have an IP address and is otherwise a normal interface, and as such can have firewall rules, routes etc.
40
41
To get to OpenVPN: the virtual tap interface that OpenVPN uses in bridged mode ***is an ethernet interface***, and as such can be part of a bridge. This is key: for our scenario, we are going to create a bridge interface that includes the gateway's eth0 LAN interface, and OpenVPN's tap0 interface. This is what bridges the VPN with the LAN.
42
43
While the concept of a bridge interface is common, the methods used to actually create a bridge interface are OS dependent. In the following paragraphs instructions are provided for the most common systems, using the addresses and interface names from the above example.
44
45
#### Permanent vs. transient bridge
46
47
There are two ways to use a bridge with OpenVPN. One is to create the bridge on the fly just before OpenVPN starts, adding eth0 and tap0 to it, and destroy it when OpenVPN terminates. The second way is to have a permanent bridge interface comprising just eth0, to which OpenVPN's tap0 is added during the time OpenVPN is running.
48
49
The first method has several drawbacks: the biggest one is that it involves removing the IP address from the eth0 interface and assigning it to the bridge interface (and the reverse when the bridge is destroyed). On most systems, deleting an IP address from an interface has the effect of removing all routes pointing out that interface, which means that these routes have to be recreated to point to the bridge when it is set up, and again recreated to point to eth0 again when the bridge is destroyed.
50
Also, in some operating systems, there is a delay of some seconds when bringing up the bridge due to STP, and this is turn delays the start of the OpenVPN daemon.
51
Thus setting up a transient bridge is not only complicated to get right, but it also disrupts connectivity for some time.
52
53
So, having a permanent bridge created at boot time and only adding/removing tap0 is largely preferable, and this is what the following instructions do.
54
55
### Windows
56
57
Under Windows, the tap interface exists as soon as OpenVPN is installed, even if OpenVPN is not running, and so the bridge can permanently include both interfaces. Also, interfaces are not named "eth0" and "tap0" under Windows, but it should be possible to tel which is which by their descriptions.
58
59
Under Windows, open the network interface screen, and select (using CTRL-click) the interfaces that you want to bridge (in our example, the LAN interface and the tap-32 adapter). When they are selected, right-click on the selection and choose "Bridge...". The configuration dialog for the bridge interface will appear, complete it with the information that was previously applied to the regular ethernet interface (ie IP address 192.168.111.254/24 and so).
60
61
(to be verified and completed)
62
63
### Linux
64
65
Under Linux, the tool needed to manage bridge interfaces is **brctl**, which is usually provided in a package called bridge-utils. If we want to use the network configuration facilities provided by the distribution, then the details vary. Here are instructions for Debian-like and Redhat-like distros.
66
67
#### Debian-like (Debian, Ubuntu and derivatives)
68
69
Install bridge-utils:
70
71
```
72
apt-get install bridge-utils
73
```
74
75
The actual configuration is done in the file /etc/network/interfaces
76
77
```
78
auto br0
79
iface br0 inet static
80
address 192.168.111.254
81
netmask 255.255.255.0
82
network 192.168.111.0
83
broadcast 192.168.111.255
84
bridge_ports eth0
85
```
86
87
#### Redhat-like (RHEL, CentOS, Fedora)
88
89
Install bridge-utils
90
91
```
92
yum install bridge-utils
93
```
94
95
96
File: **/etc/sysconfig/network-scripts/ifcfg-eth0**
97
98
```
99
DEVICE=eth0
100
ONBOOT=yes
101
TYPE=Ethernet
102
BRIDGE=br0
103
```
104
105
File: **/etc/sysconfig/network-scripts/ifcfg-br0**
106
107
```
108
DEVICE=br0
109
TYPE=Bridge
110
ONBOOT=yes
111
STP=on
112
IPADDR=192.168.111.254
113
NETMASK=255.255.255.0
114
```
115
116
If the resulting network topology has no loops (which is probably likely), STP can be disabled.
117
118
### Mac OS X
119
120
(to be completed - need info)
121
122
### FreeBSD
123
124
(to be completed - need info)
125
126
### Solaris
127
128
(to be completed - need info)
129
130
131
132
## OpenVPN configurations
133
134
Once the bridge interface is in place, here are sample configurations for the server and for the client(s).
135
136
### Server configuration
137
138
```
139
port 1194
140
proto udp
141
dev tap
142
# Windows needs the TAP-Win32 adapter name
143
# from the Network Connections panel if you
144
# have more than one.
145
# Non-Windows systems usually don't need this.
146
;dev-node MyTap
147
148
# script used to add the tap interface to the bridge
149
# windows servers comment this out
150
up up.sh
151
152
ca ca.crt
153
cert server.crt
154
key server.key
155
dh dh1024.pem
156
ifconfig-pool-persist ipp.txt
157
158
# addresses from .100 to .200 are reserved for VPN clients.
159
# Obviously, they should not be used by real LAN clients.
160
server-bridge 192.168.111.254 255.255.255.0 192.168.111.100 192.168.111.200
161
162
# Certain Windows-specific network settings
163
# can be pushed to clients, such as DNS
164
# or WINS server addresses. CAVEAT:
165
# http://openvpn.net/faq.html#dhcpcaveats
166
;push "dhcp-option DNS 192.168.111.1"
167
;push "dhcp-option WINS 192.168.111.2"
168
169
# can clients see each other? (regardless of firewalling on the server)
170
;client-to-client
171
172
keepalive 10 120
173
comp-lzo
174
persist-key
175
persist-tun
176
status openvpn-status.log
177
verb 3
178
```
179
180
Following are sample **up.sh** scripts for UNIX systems (in Windows it's not needed since the bridge permanently include both interfaces)
181
182
#### Linux
183
184
```
185
#!/bin/sh
186
187
# the tap interface name is passed as first argument
188
189
bridge=br0
190
191
brctl addif "$bridge" "$1"
192
```
193
194
#### Mac OS X
195
196
(to be completed, need info)
197
198
#### FreeBSD
199
200
(to be completed, need info)
201
202
#### Solaris
203
204
(to be completed, need info)
205
206
207
### Client configuration
208
209
```
210
client
211
dev tap
212
# Windows needs the TAP-Win32 adapter name
213
# from the Network Connections panel
214
# if you have more than one.
215
;dev-node MyTap
216
217
proto udp
218
219
remote my-server-1 1194
220
resolv-retry infinite
221
nobind
222
persist-key
223
persist-tun
224
225
ca ca.crt
226
cert client.crt
227
key client.key
228
229
ns-cert-type server
230
comp-lzo
231
verb 3
232
```