summaryrefslogtreecommitdiff
path: root/modules/yggdrasil-wg/default.nix
blob: c27eb2866ebf7f133aaa79ea438c9a1fabbfdc36 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
{ config, hostName, lib, pkgs, ... }:

with lib;

let
  listenPort = {
    "4" = 51820;
    "6" = 51821;
  };
  wgSubnet = {
    "4" = "2a03:4000:52:ada:2";
    "6" = "2a03:4000:52:ada:3";
  };
  wgSubnetLength = 80;
  wgHostLength = wgSubnetLength + 16;
  batSubnet = "2a03:4000:52:ada:1";
  batSubnetLength = 80;
  batHostLength = batSubnetLength + 16;

  links = mkLinks [
    { from = "vidhar";
      to = "surtr";
      endpointHost = "202.61.241.61";
      PersistentKeepalive = 25;
      family = "4";
    }
    { from = "vidhar";
      to = "surtr";
      endpointHost = "2a03:4000:52:ada::";
      PersistentKeepalive = 25;
      family = "6";
    }
    { from = "sif";
      to = "surtr";
      endpointHost = "202.61.241.61";
      PersistentKeepalive = 25;
      family = "4";
    }
    { from = "sif";
      to = "surtr";
      endpointHost = "2a03:4000:52:ada::";
      PersistentKeepalive = 25;
      family = "6";
    }
    { from = "sif";
      to = "vidhar";
      endpointHost = "10.141.0.1";
      PersistentKeepalive = 25;
      family = "4";
    }
  ];
  wgHostIPs = mapAttrs (_family: wgSubnet: {
    surtr = "${wgSubnet}::/${toString wgHostLength}";
    vidhar = "${wgSubnet}:1::/${toString wgHostLength}";
    sif = "${wgSubnet}:2::/${toString wgHostLength}";
  }) wgSubnet;
  greHostMACPrefixes = {
    "4" = {
      surtr = "02:00:01:00:00";
      vidhar = "02:00:01:00:01";
      sif = "02:00:01:00:02";
    };
    "6" = {
      surtr = "02:00:02:00:00";
      vidhar = "02:00:02:00:01";
      sif = "02:00:02:00:02";
    };
  };
  batHostMACs = {
    surtr = "02:00:00:00:00:00";
    vidhar = "02:00:00:01:00:00";
    sif = "02:00:00:02:00:00";
  };
  batHostIPs = {
    surtr = ["${batSubnet}::/${toString batHostLength}"];
    vidhar = ["${batSubnet}:1::/${toString batHostLength}"];
    sif = ["${batSubnet}:2::/${toString batHostLength}"];
  };
  routers = [ "surtr" ];

  mkPublicKeyPath = family: host: ./hosts + "/${family}" + "/${host}.pub";
  mkPrivateKeyPath = family: host: ./hosts + "/${family}" + "/${host}.priv";

  kernel = config.boot.kernelPackages;
  
  publicKeyPath = family: mkPublicKeyPath family hostName;
  privateKeyPath = family: mkPrivateKeyPath family hostName;
  inNetwork' = family: pathExists (privateKeyPath family) && pathExists (publicKeyPath family);
  inNetwork = any inNetwork' families;
  hostLinks = filterAttrs (_family: links: links != []) (mapAttrs (_family: filter ({ from, to, ... }: thisHost from || thisHost to)) links);
  linkToPeer = family: opts@{from, to, ...}:
    let
      other = if thisHost from then to else from;
    in {
      AllowedIPs = if elem other routers then ["::/0"] else wgHostIPs.${family}.${other};
      PublicKey = trim (readFile (mkPublicKeyPath family other));
    } // (optionalAttrs (thisHost from) (linkCfgFilterCustom opts // linkMkEndpointCfg family opts));
  linkCfgFilterCustom = filterAttrs (n: _v: !(elem n ["from" "to" "endpointHost"]));
  linkMkEndpointCfg = family: opts@{from, ...}: optionalAttrs (opts ? "endpointHost" && thisHost from) { Endpoint = "${opts.endpointHost}:${toString listenPort.${family}}"; };
  linkToGreDev = family: opts@{from, to, ...}:
    let
      other = if thisHost from then to else from;
    in nameValuePair "yggre-${other}-${family}" {
      netdevConfig = {
        Name = "yggre-${other}-${family}";
        Kind = "ip6gretap";
      };
      tunnelConfig = {
        Local = stripSubnet wgHostIPs.${family}.${hostName};
        Remote = stripSubnet wgHostIPs.${family}.${other};
      };
    };
  linkToGreNetwork = family: ix: opts@{from, to, ...}:
    let
      other = if thisHost from then to else from;
    in nameValuePair "yggre-${other}-${family}" {
      matchConfig = {
        Name = "yggre-${other}-${family}";
      };
      linkConfig = {
        MACAddress = "${greHostMACPrefixes.${family}.${hostName}}:${toHexByte ix}";
        RequiredForOnline = false;
      };
      networkConfig = {
        BatmanAdvanced = "yggdrasil";
        LinkLocalAddressing = "no";
      };
    };
  familyToYggdrasilDev = family: nameValuePair "yggdrasil-wg-${family}" {
    netdevConfig = {
      Name = "yggdrasil-wg-${family}";
      Kind = "wireguard";
    };
    wireguardConfig = {
      PrivateKeyFile = "/run/credentials/systemd-networkd.service/yggdrasil-wg-${family}.priv";
      ListenPort = listenPort.${family};
    };
    wireguardPeers = map (opts@{to, from, ...}: { wireguardPeerConfig = linkToPeer family opts; }) hostLinks.${family};
  };
  familyToLoadCred = family: "yggdrasil-wg-${family}.priv:${config.sops.secrets."yggdrasil-wg-${family}.priv".path}";
  familyToYggdrasilNetwork = family: nameValuePair "yggdrasil-wg-${family}" {
    name = "yggdrasil-wg-${family}";
    matchConfig = {
      Name = "yggdrasil-wg-${family}";
    };
    address = [wgHostIPs.${family}.${hostName}];
    routes = [
      { routeConfig = {
          Destination = "${wgSubnet.${family}}::/${toString wgSubnetLength}";
        };
      }
    ];
    linkConfig = {
      RequiredForOnline = false;
    };
    networkConfig = {
      Tunnel = map (opts@{from, to, ...}: let other = if thisHost from then to else from; in "yggre-${other}-${family}") hostLinks.${family};
    };
  };
  familyToSopsSecret = family: nameValuePair "yggdrasil-wg-${family}.priv" (mkIf (pathExists (privateKeyPath family)) {
    format = "binary";
    sopsFile = privateKeyPath family;
  });

  thisHost = host: host == hostName;
  trim = str: if hasSuffix "\n" str then trim (removeSuffix "\n" str) else str;
  stripSubnet = addr: let matchRes = builtins.match "^(.*)/[0-9]+$" addr; in if matchRes == null then addr else elemAt matchRes 0;
  optIx = optName: xs: let
    withOpts = listToAttrs (imap0 (ix: x: nameValuePair x.name (x.value // { ${optName} = ix; })) (filter (x: x.value.${optName} or false) (imap0 (ix: nameValuePair (toString ix)) xs)));
    withoutOpts = listToAttrs (map (nv: nameValuePair nv.name (removeAttrs nv.value [optName])) (filter (x: !(x.value.${optName} or false)) (imap0 (ix: nameValuePair (toString ix)) xs)));
  in genList (ix: withOpts.${toString ix} or withoutOpts.${toString ix}) (length xs);
  groupFamilies = links: mapAttrs (_name: value: map (filterAttrs (k: _v: k != "family")) value) (groupBy (x: x.family) links);
  mkLinks = groupFamilies;
  families = attrNames links;
  hostFamilies = attrNames hostLinks;
  toHexByte = n: let
    hex = toHexString n;
  in if (stringLength hex < 2) then "0${hex}" else hex;
in {
  config = {
    systemd.network = mkIf inNetwork {
      enable = true;
      netdevs = {
        yggdrasil = {
          netdevConfig = {
            Name = "yggdrasil";
            Kind = "batadv";
          };
          extraConfig = ''
            [BatmanAdvanced]
            Fragmentation=no
          '';
        };
      } // listToAttrs (map familyToYggdrasilDev hostFamilies) // listToAttrs (concatMap (family: map (linkToGreDev family) hostLinks.${family}) hostFamilies);

      networks = {
        yggdrasil = {
          name = "yggdrasil";
          matchConfig = {
            Name = "yggdrasil";
          };
          address = batHostIPs.${hostName};
          dns = ["2a03:4000:52:ada:1:1::"];
          domains = ["yggdrasil"];
          routes = [
            { routeConfig = {
                Destination = "${batSubnet}::/${toString batSubnetLength}";
                GatewayOnLink = true;
              };
            }
            { routeConfig = {
                Destination = "${batSubnet}::/${toString batSubnetLength}";
                GatewayOnLink = true;
                Table = "yggdrasil";
              };
            }
            { routeConfig = {
                Destination = batHostIPs.${hostName};
                GatewayOnLink = true;
                Table = "yggdrasil";
              };
            }
          ] ++ (concatMap (router: map (rAddr: { routeConfig = { Destination = "::/0"; Gateway = stripSubnet rAddr; GatewayOnLink = true; Table = "yggdrasil"; }; }) batHostIPs.${router}) (filter (router: router != hostName) routers));
          routingPolicyRules = map (addr: { routingPolicyRuleConfig = { Table = "yggdrasil"; From = stripSubnet addr; Priority = 1; }; }) batHostIPs.${hostName};
          linkConfig = {
            MACAddress = "${batHostMACs.${hostName}}";
            RequiredForOnline = false;
            MTUBytes = "1280";
          };
          networkConfig = {
            LLMNR = false;
            MulticastDNS = false;
            DNSSEC = false;
          };
        };
      } // listToAttrs (map familyToYggdrasilNetwork hostFamilies) // listToAttrs (concatMap (family: imap0 (linkToGreNetwork family) hostLinks.${family}) hostFamilies);

      config.routeTables.yggdrasil = 1024;
    };

    systemd.services."systemd-networkd".serviceConfig.LoadCredential = mkIf inNetwork (map familyToLoadCred hostFamilies);

    sops.secrets = listToAttrs (map familyToSopsSecret hostFamilies);

    boot.extraModulePackages = optional (versionOlder kernel.kernel.version "5.6") kernel.wireguard ++ [kernel.batman_adv];
    environment.systemPackages = with pkgs; [ wireguard-tools batctl ];
    networking.networkmanager.unmanaged = ["yggdrasil" "ip6gre0" "ip6tnl0"] ++ map (family: "yggdrasil-wg-${family}") hostFamilies ++ concatMap (family: map ({from, to, ...}: let other = if thisHost from then to else from; in "yggre-${other}-${family}") hostLinks.${family}) hostFamilies;
  };
}