Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| gentoo:wireguard [2025/02/06 07:27] – willy | gentoo:wireguard [2025/03/13 13:26] (current) – [WireGuard] willy | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| - | ====== WireGuard ====== | + | ======  | 
| [[https:// | [[https:// | ||
| Line 74: | Line 74: | ||
| There are tons of WireGuard tutorials online on how to use WireGuard to connect your mobile device securely to your home network, i do not plan to cover this topic here. | There are tons of WireGuard tutorials online on how to use WireGuard to connect your mobile device securely to your home network, i do not plan to cover this topic here. | ||
| - | |||
| - | |||
| - | ===== Port Forwarding ===== | ||
| - | |||
| - | Using WireGuard for port-forwarding between an external, public accessible host, and an internal non-accessible host (like behind a CGNAT) is a less known topic and i will cover it here. | ||
| - | |||
| - | You will need to use NFTables, see [[[[gentoo: | ||
| - | |||
| - | Assumptions: | ||
| - | * You have one **internal** host which is behind CGNAT, and is not accessible from the internet | ||
| - | * You have one **external** host which has a public IP address | ||
| - | * Both hosts have WireGuard setup according to the configuration above | ||
| - | * Internal host expose port 22, 80 and 8443 | ||
| - | * External host needs to route those three ports to the internal host | ||
| - |   * The WireGuard subnet is // | ||
| - | * External host **public** network interface is called **enp1s0** | ||
| - | * Both hosts WireGuard network iunterface is called **wg0** | ||
| - | |||
| - | What we need to do is create nftable rules to ensure that: | ||
| - | * packets reaching external host on enp1s0 on port 2022 get routed to 10.100.0.1 port 22 | ||
| - | * packets reaching external host on enp1s0 on port 80 get routed to 10.100.0.1 port 80 | ||
| - | * packets reaching external host on enp1s0 on port 443 get routed to 10.100.0.1 port 8443 | ||
| - | * return packets from the internal host get properly re-routed to the original sender out from enp1s0 | ||
| - | |||
| - | **Note:** see [[selfhost: | ||
| - | |||
| - | What we need: | ||
| - | * A dedicated table called **wg** | ||
| - |   * A **prerouting** chain to apply DNAT to incoming packes  | ||
| - | * Rules to route port 2022/80/443 to the wg tunnel ports | ||
| - | * A **postrouting** chain to ensure that all reply packets are properly SNAT back to outside | ||
| - | * Return masquerading rules to ensure the return packets get sent back out of enp1s0 | ||
| - | |||
| - | Create the wg table: | ||
| - | <code bash> | ||
| - | nft add table ip wg | ||
| - | </ | ||
| - | |||
| - | Create the base chains: | ||
| - | <code bash> | ||
| - | nft 'add chain ip wg prerouting { type nat hook prerouting priority -100 ; }' | ||
| - | nft 'add chain ip wg postrouting { type nat hook postrouting priority 100 ; }' | ||
| - | </ | ||
| - | |||
| - | Create the in-bound rules: | ||
| - | <code bash> | ||
| - | # nft 'add rule ip wg prerouting iifname enp1s0 tcp dport 80 counter' | ||
| - | # nft 'add rule ip wg prerouting iifname enp1s0 tcp dport 443 counter' | ||
| - | # nft 'add rule ip wg prerouting iifname enp1s0 tcp dport 2022 counter' | ||
| - | nft 'add rule ip wg prerouting iifname enp1s0 dnat to tcp dport map { 2022 : 10.100.0.1 . 22 }' | ||
| - | nft 'add rule ip wg prerouting iifname enp1s0 dnat to tcp dport map { 80 : 10.100.0.1 . 80 }' | ||
| - | nft 'add rule ip wg prerouting iifname enp1s0 dnat to tcp dport map { 443 : 10.100.0.1 . 8443 }' | ||
| - | </ | ||
| - | |||
| - | Create the SNAT return rule (the counter rule is only for debugging, you can omit that rule): | ||
| - | <code bash> | ||
| - | # nft 'add rule ip wg postrouting ip daddr 10.100.0.1 counter' | ||
| - | nft 'add rule ip wg postrouting ip daddr 10.100.0.1 masquerade' | ||
| - | </ | ||
| - | |||
| - | This is the resulting NFTables setup: | ||
| - | <code bash> | ||
| - | nft list table wg | ||
| - | table ip wg { | ||
| - | chain prerouting { | ||
| - | type nat hook prerouting priority dstnat; policy accept; | ||
| - |                 iifname " | ||
| - |                 iifname " | ||
| - |                 iifname " | ||
| - |                 iifname " | ||
| - |                 iifname " | ||
| - |                 iifname " | ||
| - | } | ||
| - | |||
| - | chain postrouting { | ||
| - | type nat hook postrouting priority srcnat; policy accept; | ||
| - | ip daddr 10.100.0.1 counter packets 390 bytes 25945 | ||
| - | ip daddr 10.100.0.1 masquerade | ||
| - | } | ||
| - | } | ||
| - | </ | ||
| - | |||
| - | |||