If you want
P1 <--> S <--> P2 <--> Internet
where S is a "Wireguard server", P1 and P2 are Wireguard peers (assumed to be behind NAT with no port forwarding configured) "connecting" to S and P2 acts as internet gateway for P1, you need first of all the following basic settings:
P1 Wireguard config
[Interface]
PrivateKey = ...
Address = 10.200.1.5/32
[Peer] # S
PublicKey = ...
Endpoint = ...:51820
AllowedIPs = 0.0.0.0/0
S Wireguard config
[Interface]
ListenPort = 51820
PrivateKey = ...
Address = 10.200.1.1/32
[Peer] # P1
PublicKey = ...
AllowedIPs = 10.200.1.5
[Peer] # P2
PublicKey = ...
AllowedIPs = 0.0.0.0/0
P2 Wireguard config
[Interface]
PrivateKey = ...
Address = 10.200.1.3/32
[Peer] # S
PublicKey = ...
Endpoint = ...:51820
PersistentKeepalive = 25 # allows S to reach P2
AllowedIPs = 10.200.1.0/24
Also, make sure that IP forwarding is enabled on S as well as P2 and that P2 performs the necessary NAT/masquerading when the forwarded packets from S1 leave P2 on its internet-facing network interface.
This should already work in that P1's internet traffic is forwarded all the way to P2. However, now the internet access of S is also via P2. This might be undesirable. For example, you will have problems SSH-ing into S over the internet because S would try to respond via P2 (asymmetric routing with NAT along the way). If you don't want S itself to use P2 as gateway you can configure policy-based routing on S manually like this:
S config with custom policy-based routing
[Interface]
ListenPort = 51820
PrivateKey = ...
Address = 10.200.1.1/32
Table = 123 # <-- AllowedIPs-based routes end up here
PostUp = ip rule add from 10.200.1.0/24 table 123
PreDown = ip rule del from 10.200.1.0/24 table 123
[Peer] # P1
PublicKey = ...
AllowedIPs = 10.200.1.5
[Peer] # P2
PublicKey = ...
AllowedIPs = 0.0.0.0/0
This makes wg-quick
add the AllowedIPs
-based routes to a custom routing table (table 123
) and conditionally "enables" this routing table based on the source IP addresses using ip rule
.
You also might want to add some firewall rules on S, to, for example, make sure traffic coming in over Wireguard is not escaping anywhere and is just forwarded to the same device. You could do so by adding
PostUp = iptables -I FORWARD -i %i ! -o %i -j REJECT
PreDown = iptables -D FORWARD -i %i ! -o %i -j REJECT
to S' config.