4 # https://unix.stackexchange.com/questions/149293/feed-all-traffic-through-openvpn-for-a-specific-network-namespace-only
6 # vpn_wrapper.sh passes the following variables through openvpn's
17 ip netns add
$NAMESPACE_NAME
18 ip netns
exec $NAMESPACE_NAME ip link
set dev lo up
19 ip link
set dev
"$1" up netns
$NAMESPACE_NAME mtu
"$2"
20 ip netns
exec $NAMESPACE_NAME ip addr add dev
"$1" \
21 "$4/${ifconfig_netmask:-30}" \
22 ${ifconfig_broadcast:+broadcast "$ifconfig_broadcast"}
23 if [ -n "$ifconfig_ipv6_local" ]; then
24 ip netns
exec $NAMESPACE_NAME ip addr add dev
"$1" \
25 "$ifconfig_ipv6_local"/112
28 # the following is done to enable some connections to bypass vpn
29 VETH0
=v0tdns
${WRAPPER_PID}_0
30 VETH1
=v0tdns
${WRAPPER_PID}_1
31 ip link add
$VETH0 type veth peer name
$VETH1
32 ip link
set $VETH1 netns
$NAMESPACE_NAME
33 ip addr add
$VETH_HOST0/30 dev
$VETH0
34 ip netns
exec $NAMESPACE_NAME ip addr add
$VETH_HOST1/30 dev
$VETH1
36 ip netns
exec $NAMESPACE_NAME ip link
set $VETH1 up
39 # TODO change to only forward from necessary interfaces
40 echo 1 > /proc
/sys
/net
/ipv
4/conf
/all
/forwarding
42 ip netns
exec $NAMESPACE_NAME ip route add default via
"$ifconfig_remote"
44 if [ -n "$ifconfig_ipv6_remote" ]; then
45 ip netns
exec $NAMESPACE_NAME ip route add default via \
46 "$ifconfig_ipv6_remote"
49 # here go routes for bypassing vpn
50 for ADDRESS
in $ROUTE_THROUGH_VETH; do
51 ip netns
exec $NAMESPACE_NAME ip route add
$ADDRESS via
$VETH_HOST0
52 iptables
-t nat
-A POSTROUTING
-s $VETH_HOST1/32 \
53 -j SNAT
--to-source $PHYSICAL_IP
57 # notify our sh process, that openvpn finished initializing
58 kill -usr1 $WRAPPER_PID
61 for ADDRESS
in $ROUTE_THROUGH_VETH; do
62 iptables
-t nat
-D POSTROUTING
-s $VETH_HOST1/32 \
63 -j SNAT
--to-source $PHYSICAL_IP
66 ip netns delete
$NAMESPACE_NAME