|
| 1 | +//go:build linux |
| 2 | + |
| 3 | +package wireguard |
| 4 | + |
| 5 | +import ( |
| 6 | + "fmt" |
| 7 | + "log" |
| 8 | + "os" |
| 9 | + "os/exec" |
| 10 | + "strings" |
| 11 | +) |
| 12 | + |
| 13 | +const ( |
| 14 | + envHostRouting = "PG_NODE_WG_HOST_ROUTING" |
| 15 | + envNATOutputInterface = "PG_NODE_WG_NAT_OUTPUT_INTERFACE" |
| 16 | +) |
| 17 | + |
| 18 | +// applyLinuxHostRouting enables IPv4/IPv6 forwarding and installs an nftables masquerade |
| 19 | +// rule for traffic from the WireGuard interface to the IPv4 default-route egress interface. |
| 20 | +// |
| 21 | +// wgInterfaceName comes from core JSON interface_name (e.g. wg0, wg1); never hardcoded here. |
| 22 | +// The NAT egress interface is resolved in order: |
| 23 | +// 1. PG_NODE_WG_NAT_OUTPUT_INTERFACE if set |
| 24 | +// 2. IPv4 default route interface (ip -4 -j route, else /proc/net/route) |
| 25 | +// 3. eth0 as last-resort fallback |
| 26 | +// |
| 27 | +// Disable all of this with PG_NODE_WG_HOST_ROUTING=0. |
| 28 | +func applyLinuxHostRouting(wgInterfaceName string) { |
| 29 | + if v := strings.TrimSpace(os.Getenv(envHostRouting)); v == "0" || strings.EqualFold(v, "false") { |
| 30 | + return |
| 31 | + } |
| 32 | + |
| 33 | + wgIf := strings.TrimSpace(wgInterfaceName) |
| 34 | + if wgIf == "" { |
| 35 | + wgIf = "wg0" |
| 36 | + } |
| 37 | + |
| 38 | + outIf := strings.TrimSpace(os.Getenv(envNATOutputInterface)) |
| 39 | + if outIf == "" { |
| 40 | + var ok bool |
| 41 | + outIf, ok = linuxDefaultRouteInterfaceIPv4() |
| 42 | + if !ok { |
| 43 | + outIf = "eth0" |
| 44 | + log.Printf( |
| 45 | + "wireguard host routing: could not detect default IPv4 egress interface; using fallback %q (set %s)", |
| 46 | + outIf, |
| 47 | + envNATOutputInterface, |
| 48 | + ) |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + if err := writeSysctl("net/ipv4/ip_forward", "1"); err != nil { |
| 53 | + log.Printf("wireguard host routing: ipv4 forwarding: %v", err) |
| 54 | + } |
| 55 | + if err := writeSysctl("net/ipv6/conf/all/forwarding", "1"); err != nil { |
| 56 | + log.Printf("wireguard host routing: ipv6 forwarding: %v", err) |
| 57 | + } |
| 58 | + |
| 59 | + log.Printf("wireguard host routing: wg interface %q, NAT egress %q (masquerade)", wgIf, outIf) |
| 60 | + |
| 61 | + if err := ensureNFTMasquerade(wgIf, outIf); err != nil { |
| 62 | + log.Printf("wireguard host routing: nftables masquerade (optional): %v", err) |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +func writeSysctl(relPath, value string) error { |
| 67 | + path := "/proc/sys/" + relPath |
| 68 | + return os.WriteFile(path, []byte(value+"\n"), 0) |
| 69 | +} |
| 70 | + |
| 71 | +// ensureNFTMasquerade replaces table ip pasarguard_wg. Traffic is matched from the |
| 72 | +// configured WireGuard interface to the detected/configured egress interface only. |
| 73 | +func ensureNFTMasquerade(wgIface, outputIface string) error { |
| 74 | + del := exec.Command("nft", "delete", "table", "ip", "pasarguard_wg") |
| 75 | + _ = del.Run() |
| 76 | + |
| 77 | + cfg := fmt.Sprintf(`table ip pasarguard_wg { |
| 78 | + chain postrouting { |
| 79 | + type nat hook postrouting priority 100; |
| 80 | + policy accept; |
| 81 | + iifname %q oifname %q masquerade |
| 82 | + } |
| 83 | +} |
| 84 | +`, wgIface, outputIface) |
| 85 | + |
| 86 | + cmd := exec.Command("nft", "-f", "-") |
| 87 | + cmd.Stdin = strings.NewReader(cfg) |
| 88 | + out, err := cmd.CombinedOutput() |
| 89 | + if err != nil { |
| 90 | + return fmt.Errorf("%w: %s", err, strings.TrimSpace(string(out))) |
| 91 | + } |
| 92 | + return nil |
| 93 | +} |
0 commit comments