OPNsense Forum

English Forums => 24.7, 24.10 Legacy Series => Topic started by: ThisUsernameHasBeenTaken on November 10, 2024, 08:25:50 AM

Title: Configuring firewall and routing for a standalone wireguard server in LAN
Post by: ThisUsernameHasBeenTaken on November 10, 2024, 08:25:50 AM
Hello!

For some reason I have to use a standalone Wireguard server located on a dedicated host in my LAN.
All the Wireguard peers should have access to all the LAN hosts and vice-versa.

I was able to set everything up in a NAT (Masquarade) mode, but I want all the peers to be "visible" and manageable by their Wireguard IPs inside the OPNsense. Thus I am trying to set up the Wireguard server without a NAT (Masquerading).

Unfortunately I can't figure out how to set up firewall and routing properly.

My network setup:

Steps I took:

The results are: I can connect to the Wireguard server, but I cannot access any host in the LAN or Internet.
If I try to ping on make nslookup any host using 172.17.17.2 I get messages that these requests are blocked (see screenshot #2) by the Default deny rule which I don't understand because I have defined the Pass rules to allow this kind of traffic.

I appreciate any help how to make this setup working. I believe some firewall rules and/or routes are missing, but I can't figure what exactly is wrong.
Title: Re: Configuring firewall and routing for a standalone wireguard server in LAN
Post by: dseven on November 10, 2024, 09:14:33 AM
I think you have an asymmetric routing situation. Your log screenshot appears to be showing DNS *responses* from 172.17.17.2 (port 53) back to 172.17.21.2, which are trying to go via the firewall (because presumably that's the detault route / gateway on the DNS server), but the requests from the client (172.17.21.2) to the DNS server would not have passed through the firewall, because your wireguard server has a local address on 172.17.17.0/24. The firewall can't track this, because it's only seeing half of the conversation.

You might be able to hack around this with static routes, but it'd probably be better if you could place the wireshark server on a dedicated subnet, separate from LAN, so both directions will always pass through the firewall.
Title: Re: Configuring firewall and routing for a standalone wireguard server in LAN
Post by: ThisUsernameHasBeenTaken on November 10, 2024, 09:21:58 AM
Thank you very much for the suggestion!

I think I can do it. OPNSense is installed onto a VM so I can create another interface, assign a new network to it and move the Wireguard server into the new network.
A few questions about this setup:
Title: Re: Configuring firewall and routing for a standalone wireguard server in LAN
Post by: dseven on November 10, 2024, 09:26:12 AM
I would expect that wireguard would be managing address assignment, so no need for DHCP, and there wouldn't be any non-wireguard hosts on that network, so nothing to conflict with...?
Title: Re: Configuring firewall and routing for a standalone wireguard server in LAN
Post by: Monviech (Cedrik) on November 10, 2024, 09:28:10 AM
Just use the built in wireguard server of the OPNsense, it will cut down all complexity and fix all routing issues.
Title: Re: Configuring firewall and routing for a standalone wireguard server in LAN
Post by: ThisUsernameHasBeenTaken on November 10, 2024, 09:31:58 AM
Except for the Wireguard server itself and the OPNSense, yes: there won't be any other non-Wireguard hosts in this network.
So technically there might be 1 interference: with the OPNsense. Because Wireguard doesn't know anything about it.

Regarding using a build in Wireguard server. I'd like to, but I can't: there is some specific software (like DPI or something) which should be running next to the Wireguard server.
Just to clarify: for now it is not installed and cannot interfere with the current setup.
Title: Re: Configuring firewall and routing for a standalone wireguard server in LAN
Post by: ThisUsernameHasBeenTaken on November 10, 2024, 09:47:32 AM
Hmm. But before creating a new network: what about a hack with routes? What routes should I add to test if it works?
Title: Re: Configuring firewall and routing for a standalone wireguard server in LAN
Post by: dseven on November 10, 2024, 10:09:00 AM
You'd probably need a static route on the firewall for 172.17.21.0/24 pointing to the wireguard server (172.17.17.8). That won't fix access to the DNS server, though, unless maybe you create a firewall rule on your LAN interface to allow destination 172.17.21.0/24 with "State Type" (under "Advanced features") set to "sloppy state". I'm not sure if that'll work, but it might. You might also need a sloppy rule for the other direction. Alternatively you could also add a static route on the DNS server, but then you'd have to do that on any LAN how that you need to be accessible from wireguard clients.
Title: Re: Configuring firewall and routing for a standalone wireguard server in LAN
Post by: ThisUsernameHasBeenTaken on November 10, 2024, 10:22:03 AM
I got a little (partial) successes with the hack.

1. I have created a new Gateway in System -> Gateways -> Configuration pointing to 172.17.17.8 (Wireguard server)
2. I have created a new route in System -> Routes -> Configuration: to 172.17.21.0/24 via freshly created gateway.
3. In Firewall -> Settings -> Advanced I have enabled this option:
Bypass firewall rules for traffic on the same interface
This option only applies if you have defined one or more static routes. If it is enabled, traffic that enters and leaves through the same interface will not be checked by the firewall. This may be desirable in some situations where multiple subnets are connected to the same interface.


After that I now have access to all the LAN hosts from the Wireguard! Yay!

Unfortunately I don't have Internet access from the Wireguard. I have created an outbound NAT for 172.17.21.0/24, but it didn't help...
Title: Re: Configuring firewall and routing for a standalone wireguard server in LAN
Post by: ThisUsernameHasBeenTaken on November 10, 2024, 11:00:37 AM
Hmm.

Unfortunately, it didn't work: I cannot access any hosts at all.
The same situation happens if I create a new gateway pointing to 172.17.18.254 and a route: to 172.17.18.0/24 via freshly created gateway.

The firewall is all green though: it shows connections, but no traffic flows...
Title: Re: Configuring firewall and routing for a standalone wireguard server in LAN
Post by: ThisUsernameHasBeenTaken on November 10, 2024, 03:43:39 PM
I got both the Internet and LAN working, but I have no idea why is this working.
I was experimenting with NAT rules and accidentally mistyped IP: 127.17.21.0/21 instead of 172.17.21.0/24 and... it worked.
If I disable the rule - I loose the Internet connection for the Wireguard clients. If I enable it, the connection is back.

I don't know why: this rule doesn't make sense to me. Can anybody explain it?
Title: Re: Configuring firewall and routing for a standalone wireguard server in LAN
Post by: dseven on November 10, 2024, 03:49:34 PM
I assume you mean 172.17, not 127.17...

172.17.21.0/21 would actually be 172.17.16.0/21, which encompasses 172.17.18.0/24. Is your wireguard server masquerading?

Edit: Scratch that - I just looked at the screenshot.

Looks like you have the source address inverted - why?
Title: Re: Configuring firewall and routing for a standalone wireguard server in LAN
Post by: ThisUsernameHasBeenTaken on November 10, 2024, 03:55:01 PM
At first yes, I saw the typo: 127 instead of 172. Fixed it and the Internet gone... So I put it back - exactly as you can see on the screenshot and the Internet came back again.
That is why I am confused. It shouldn't work. It even makes no sense, but it works! And I don't know why.

No, my Wireguard server is not masquerading - that's the whole point of the setup.
Title: Re: Configuring firewall and routing for a standalone wireguard server in LAN
Post by: dseven on November 10, 2024, 03:56:23 PM
See my edited post above (you read the original one too quickly:)
Title: Re: Configuring firewall and routing for a standalone wireguard server in LAN
Post by: ThisUsernameHasBeenTaken on November 10, 2024, 03:58:34 PM
I was just experimenting with the rules: like just try everything one by one.
I cannot explain the logic behind this rule and it really confuses me.
Title: Re: Configuring firewall and routing for a standalone wireguard server in LAN
Post by: dseven on November 10, 2024, 03:59:33 PM
So if you use the correct address for your WG network, and don't invert, does that work?
Title: Re: Configuring firewall and routing for a standalone wireguard server in LAN
Post by: ThisUsernameHasBeenTaken on November 10, 2024, 04:18:23 PM
Nevermind. I have rebooted the OPNsense and everything went back to normal. I.e. the rule which makes sense now is working and I got the Internet and all access.

I really don't know what happened - cache maybe?

The configuration that works for me (maybe it will be useful for somebody):


@dseven, thank you very much for your help and advice!!!