]> Git — Sourcephile - sourcephile-nix.git/blob - hosts/losurdo/networking/wireguard/intranet.nix
nftables: wg-intra: cleanup
[sourcephile-nix.git] / hosts / losurdo / networking / wireguard / intranet.nix
1 { pkgs, lib, config, inputs, ... }:
2 let
3 inherit (config.boot) initrd;
4 iface = "wg-intra";
5 wg = config.networking.wireguard.interfaces.${iface};
6 wg-intra-peers = import (inputs.julm-nix + "/nixos/profiles/wireguard/wg-intra/peers.nix");
7 relay = wg-intra-peers.mermet;
8 in
9 {
10 imports = [
11 (inputs.julm-nix + "/nixos/profiles/wireguard/wg-intra.nix")
12 ];
13 networking.wireguard.${iface}.peers = {
14 mermet.enable = true;
15 oignon.enable = true;
16 patate.enable = true;
17 carotte.enable = true;
18 };
19 systemd.services."wireguard-${iface}" = {
20 unitConfig.Upholds = [ "upnpc-${toString wg.listenPort}.service" ];
21 };
22 networking.nftables.ruleset = ''
23 # Allow initiating connection to and from other peers
24 add rule inet filter fw2net udp sport ${toString wg.listenPort} counter accept comment "WireGuard ${iface} output to peers"
25 add rule inet filter net2fw udp dport ${toString wg.listenPort} counter accept comment "WireGuard ${iface} input from peers"
26
27 # Hook ${iface} into relevant chains
28 add chain inet filter intra2fw
29 #add rule inet filter intra2fw counter accept
30 add rule inet filter input iifname "${iface}" jump intra2fw
31 add rule inet filter input iifname "${iface}" log level warn prefix "intra2fw: " counter drop
32 add rule inet filter output oifname "${iface}" jump fw2intra
33 add rule inet filter output oifname "${iface}" log level warn prefix "fw2intra: " counter drop
34
35 # ${iface} firewalling
36 add chain inet filter fw2intra
37 #add rule inet filter fw2intra counter accept
38 add rule inet filter fw2intra counter accept
39 ${lib.concatMapStringsSep "\n" (ip: ''
40 add rule inet filter intra2fw ip saddr ${ip} counter accept comment "relay"
41 '') relay.ips}
42
43 add chain inet filter fwd-intra
44 #add rule inet filter fwd-intra counter accept
45 add rule inet filter forward iifname "${iface}" jump fwd-intra
46 '';
47 # Apparently required to get NAT reflection.
48 services.upnpc.redirections = [
49 { description = "WireGuard"; externalPort = wg.listenPort; protocol = "UDP"; duration = 30 * 60;
50 service.requiredBy = [ "wireguard-${iface}.service" ];
51 service.before = [ "wireguard-${iface}.service" ];
52 }
53 ];
54 boot.kernel.sysctl."net.ipv4.ip_forward" = 1;
55
56 # Open a wireguard tunnel to a relay
57 # in case the host is hosted behind a NAT and has no SSH port forwarding.
58 # This enables to send the disk password to the initrd, like that:
59 # ssh -J mermet.sourcephile.fr root@losurdo.wg -p 2222
60 boot.initrd.secrets."/root/initrd/${iface}.key" = "/root/initrd/${iface}.key";
61 boot.initrd.kernelModules = [ "wireguard" ];
62 boot.initrd.extraUtilsCommands = ''
63 #copy_bin_and_libs ${pkgs.wireguard-tools}/bin/wg
64 cp -fpdv ${pkgs.wireguard-tools}/bin/.wg-wrapped $out/bin/wg
65 '';
66 boot.initrd.network.postCommands = ''
67 ip link add dev ${iface} type wireguard
68 ${lib.concatMapStringsSep "\n" (ip: ''
69 ip address add ${ip} dev ${iface}
70 '') wg.ips}
71 wg set ${iface} private-key /root/initrd/${iface}.key \
72 listen-port ${toString wg.listenPort}
73 ip link set up dev ${iface} mtu 1280
74 wg set ${iface} peer ${relay.peer.publicKey} \
75 endpoint ${relay.ipv4}:${toString relay.listenPort} \
76 allowed-ips ${relay.ipv4}/32 \
77 persistent-keepalive 5
78 ip route replace ${relay.ipv4}/32 dev ${iface} table main
79 '';
80 boot.initrd.postMountCommands = lib.mkIf initrd.network.flushBeforeStage2 ''
81 ip link del dev ${iface}
82 '';
83 }