Score:0

Wireguard client and redirect VPN traffic to ETH

gw flag

I have for project to create a site to site VPN with wireguard and two Raspberry Pi 4

Site A- UniFi UDM PRO - Raspberry Pi WG server ------ Site B UDM PRO Raspberry Pi WG client.

the Pi have only one NIC but I suppose that's won't be an issue for the server, for the client I bought an additional NIC (over the Pi's HAT).

Can I redirect the VPN connection (full tunnel) so if a client is connecting the the second ethernet port of the Raspberry Pi only received the WG VPN connection (and an IP from site A) ?

Score:2
th flag

When connecting to a Wireguard server on your Linux machine, a new network interface is brought up, and all traffic is forwarded through it.

Usually, to connect two interfaces together, you would just easily set up a bridge and connect the two interfaces two it.

However, Bridges work on the OSI Layer-2 (forwarding frames, which are directed to destination MAC Addresses), while Wireguard is a Layer-3 protocol (forwarding IP packets with destination IP addresses) (more info).

So you cannot connect an interface that operates on L3 with a bridge. So you have several options:

  1. Use a Layer-2 VPN tunnel (like L2TP+IPSec), with the configuration hassle that incurs.
  2. Set up the Raspberry Pi as a Socks proxy that tunnels traffic to Wireguard and bridge the full internet connection to the connected device. This would mean traffic would not go through the VPN by default, which most likely isn't what you want.
  3. Set up the Raspberry Pi as a gateway router and forward IP packets to the VPN:

To achieve this, you'd need the following:

  • Enable packet forwarding on the client Pi (set net.ipv4.ip_forward=1 in /etc/sysctl.conf and updating it with sysctl -p
  • Set up the NAT on your client Pi between both interfaces. This would forward packets from your WG subnet (i.e. 10.0.0.0/24 to the new subnet your Raspberry Pi will take care of and you set up in the DHCP Server (i.e 10.10.0.0/24). There are multiple ways to configure this, but in 2022 using Ubuntu, you should give Netplan a try, which allows you to configure all these networks with a YAML file intuitively. There is an example on Netplan's website achieving exactly this.
  • Set up the raspberry pi as a DHCP server (so the IP configuration is automatic and devices with no manual config also have traffic forwarded): set the option routers yourRaspberryPisWireguardIP; in /etc/dhcp/dhcpd.conf and choose a new subnet to assign IP addresses to the clients. guide. You should prepare to make isc-dhcp-server play nice with Netplan. This askubuntu answer does just that.
  • Remember using Netplan will probably override your Wg-Quick network setup, but that's no problem. Just add your WG client configuration on that same Netplan file in the tunnels: section.
  • You might want to set up a DNS server on the Raspberry Pi that points to your WG server DNS to avoid leaks.

As you might have guessed, with this last approach all clients connected to your raspberry would seem to come from the same client IP (your Raspberry's) due to NAT. You might be able to change this (Wireguard's server config AllowedIPs can be set to a range, but I'm not sure how to exactly achieve that.


TLDR: Configure the wg client tunnel using Netplan. Set up NAT using Netplan between the WG Client interface and the secondary ethernet iface you plan to have your clients on. Set up a DHCP server on your pi on a separate subnet for your clients. Essentially, build your own router.

Kevin avatar
gw flag
thank a lot for all this detailed informations, this is exactly what I was looking for.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.