summaryrefslogtreecommitdiff
path: root/config/router.nix
blob: 2b05b7b52b88dce0751ef09c3c44e36a2a32cf0b (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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
{ config, lib, pkgs, ... }:
let
  wan-dev = "bond0.10";
  lan-dev = "bond0";
  wireguardPorts =
    [ config.orbekk.mullvad.listenPort config.orbekk.nycmesh.listenPort ];
  mullvadMark = 30;
  nycmeshMark = 32;
  aliases = import ../data/aliases.nix;
in {
  orbekk.mullvad.enable = true;
  orbekk.nycmesh.enable = true;

  services.tftpd.enable = true;
  services.openntpd.enable = true;

  networking.useDHCP = false;

  networking.networkmanager.enable = lib.mkForce false;

  networking.nameservers = [ "8.8.8.8" ];

  networking.vlans = builtins.listToAttrs (map (id: {
    name = "${lan-dev}.${toString id}";
    value = {
      inherit id;
      interface = lan-dev;
    };
  }) [ 10 30 32 100 255 ]);

  networking.bonds.bond0 = {
    interfaces = [ "eno1" "eno2" ];
    driverOptions = {
      miimon = "1000";
      mode = "balance-rr";
    };
  };

  systemd.services.update-dynamic-dns = {
    description = "Update dynamic dns records";
    path = with pkgs; [ bash dnsutils nettools gawk iproute curl ];
    startLimitIntervalSec = 5;
    script = toString ../tools/update-dns.sh;
  };

  networking.dhcpcd.runHook = ''
    systemctl restart update-dynamic-dns.service
  '';

  networking.firewall = {
    enable = lib.mkForce false;
    allowedTCPPorts = lib.mkForce [ ];
    allowedUDPPorts = lib.mkForce [ ];
    allowPing = true;
    logRefusedConnections = false;
    checkReversePath = false;
  };

  services.ddclient = {
    enable = true;
    configFile = "/opt/secret/he-ddclient.conf";
  };
  # FIXME: Workaround for ddclient.conf not being available to ddclient.
  systemd.services.ddclient.serviceConfig.DynamicUser = lib.mkForce false;

  services.ferm = {
    enable = true;
    config = ''
      @def $DEV_UNTRUSTED_LAN = (${lan-dev}.30 ${lan-dev}.32);
      @def $DEV_LAN = (${lan-dev}.100);
      @def $DEV_ADMIN = (${lan-dev}.255);
      @def $DEV_WAN = (${wan-dev} he0 mullvad nycmesh);
      @def $NET_LAN = (172.20.0.0/16);

      # Forward dns queries to dnsmasq on LAN interfaces.
      domain (ip ip6) table nat chain PREROUTING {
        interface ($DEV_LAN $DEV_UNTRUSTED_LAN) daddr $NET_LAN proto (tcp udp) dport 53 REDIRECT to-ports 2053;
      }

      domain (ip ip6) table filter {
        chain INPUT {
          policy DROP;

          mod state state INVALID DROP;
          mod state state (ESTABLISHED RELATED) ACCEPT;

          interface lo ACCEPT;
          proto icmp ACCEPT;

          proto (udp udp) dport dhcpv6-client ACCEPT;

          proto 41 ACCEPT;  # IPv6 sit tunnel

          interface ($DEV_WAN $DEV_LAN $DEV_UNTRUSTED_LAN) @subchain "wan_services" {
            # Valheim
            proto udp dport (3400 3401 3402) ACCEPT;

            proto (tcp udp) dport 2053 ACCEPT;
            proto (tcp udp) dport (bootpc bootps) ACCEPT;
            proto tcp dport ssh ACCEPT;
            proto (tcp udp) dport domain ACCEPT;
            proto tcp dport (http https) ACCEPT;
            proto udp dport (${lib.concatStringsSep " " (map toString wireguardPorts)}) ACCEPT;
          }

          interface ($DEV_LAN $DEV_ADMIN) @subchain "lan_services" {
            proto (tcp udp) dport 5000 ACCEPT; # random debugging

            proto (tcp udp) dport (ssh domain bootpc bootps ntp) ACCEPT;
            # prometheus temp rule
            proto tcp dport 11112 ACCEPT;
            proto (tcp udp) dport tftp ACCEPT;

            # RTMP streaming
            proto (tcp udp) dport 1935 ACCEPT;

            # Chromecast
            # proto udp dport 32768:61000 ACCEPT;
            # proto udp dport (5353 1900) ACCEPT;
            # proto tcp dport (8008 8009) ACCEPT;

            interface $DEV_LAN jump logdrop;
          }
        }

        chain OUTPUT policy ACCEPT;

        chain FORWARD {
          policy DROP;

          mod state state INVALID DROP;
          mod state state (ESTABLISHED RELATED) ACCEPT;

          interface $DEV_UNTRUSTED_LAN outerface $DEV_WAN ACCEPT;
          interface $DEV_LAN ACCEPT;

          jump logdrop;
        }
      }

      domain ip6 table filter chain INPUT {
        proto ipv6-icmp ACCEPT;
      }

      domain (ip ip6) table filter chain logdrop {
        LOG log-level warning log-prefix "dropped "; 
        DROP;
      }

      domain (ip ip6) table filter chain INPUT {
        interface $DEV_WAN DROP;
        jump logdrop;
      }

      domain ip table nat {
        chain POSTROUTING {
          saddr $NET_LAN outerface $DEV_WAN MASQUERADE;
        }
      }

      domain (ip ip6) table mangle {
        chain PREROUTING {
          interface ${lan-dev}.30 MARK set-mark ${toString mullvadMark};
        }
      }
      domain ip table mangle {
        chain PREROUTING {
          interface ${lan-dev}.32 MARK set-mark ${toString nycmeshMark};
        }
      }
    '';
  };

  services = {
    openssh.enable = lib.mkDefault true;
    openssh.passwordAuthentication = false;
  };

  boot.kernel.sysctl = {
    "net.ipv4.conf.all.forwarding" = true;
    "net.ipv4.conf.default.forwarding" = true;
    "net.ipv6.conf.all.forwarding" = true;
    "net.ipv6.conf.default.forwarding" = true;
  };

  #services.hostapd = {
  #  enable = true;
  #  # driver = "iwlwifi";
  #  ssid = "2c";
  #  wpaPassphrase = "mintchip";
  #  interface = "${lan-dev}";
  #  hwMode = "g";
  #  channel = 11;
  #  extraConfig = ''
  #    country_code=US
  #    wpa_key_mgmt=WPA-PSK  
  #    rsn_pairwise=CCMP
  #  '';
  #};

  networking.dhcpcd = {
    # Wait for v4 and v6 addresses.
    # wait = "both";
    extraConfig = ''
      debug
      noipv6rs
      interface ${wan-dev}
        dhcp
        ipv6rs
        ia_na 0
        # ia_pd 1/::/64 ${lan-dev}.100/0/64
    '';
  };
  systemd.services.dhcpcd = {
    after = [ "network-addresses-${wan-dev}.service" ];
    preStart = lib.mkAfter ''
      ${pkgs.iproute}/bin/ip link set dev ${wan-dev} address ${
        config.networking.interfaces.${wan-dev}.macAddress
      };
    '';
  };

  services.dnsmasq = {
    enable = true;
    servers = [ "1.1.1.1" "8.8.8.8" "8.8.4.4" ];
    resolveLocalQueries = false;
    extraConfig = ''
      port=2053

      no-resolv
      address=/test-dns.orbekk.com/::1

      enable-ra
      dhcp-authoritative

      # Null AAAA response on these domains
      server=/netflix.com/#
      address=/netflix.com/::
      server=/netflix.net/#
      address=/netflix.net/::
      server=/nflxext.com/#
      address=/nflxext.com/::
      server=/nflximg.net/#
      address=/nflximg.net/::
      server=/nflxvideo.net/#
      address=/nflxvideo.net/::
      server=/nflxso.net/#
      address=/nflxso.net/::

      dhcp-range=vlan30,172.20.30.50,172.20.30.254,5m
      dhcp-option=net:vlan30,option:router,172.20.30.1
      dhcp-option=net:vlan30,option:dns-server,172.20.30.1

      dhcp-range=vlan100,172.20.100.50,172.20.100.254,5m
      dhcp-host=vlan100,d8:3b:bf:59:22:de,172.20.100.10
      dhcp-range=vlan100,::100,::500,constructor:bond0.100,ra-only
      dhcp-option=net:vlan100,option:router,172.20.100.1
      dhcp-option=net:vlan100,option:dns-server,172.20.100.1

      dhcp-range=vlan32,172.20.32.50,172.20.32.254,5m
      dhcp-range=vlan32,::100,::500,constructor:bond0.32,ra-only
      dhcp-option=net:vlan32,option:router,172.20.32.1
      dhcp-option=net:vlan32,option:dns-server,172.20.32.1
    '';
  };

  networking.sits.he0 = {
    dev = wan-dev;
    remote = "209.51.161.14";
  };

  networking.iproute2.enable = true;
  networking.iproute2.rttablesExtraConfig = ''
     ${toString mullvadMark} mullvad
     ${toString nycmeshMark} nycmesh
    200 he
  '';

  systemd.services.network-route-setup = {
    description = "HE tunnel route setup";
    requires = [ "network-online.target" ];
    after = [ "network.target" "network-online.target" ];
    wantedBy = [ "multi-user.target" ];
    path = [ pkgs.iproute ];
    script = ''
      # ip -6 rule add from 2001:470:8e2e::/48 lookup he prio 0 || true
      # ip -6 route replace default dev he0 src 2001:470:8e2e:20::d table he
      # ip -6 route flush cache
    '';
  };

  networking.wireguard.interfaces.mullvad.postSetup = ''
    ip rule add fwmark ${toString mullvadMark} table mullvad
    ip route replace default dev mullvad table mullvad
    ip route flush cache
  '';

  networking.wireguard.interfaces.nycmesh.postSetup = ''
    ip rule add fwmark ${toString nycmeshMark} table nycmesh
    ip route replace default via 10.70.73.1 onlink dev nycmesh table nycmesh
    ip route flush cache
  '';

  # boot.kernel.sysctl."net.ipv6.conf.${wan-dev}.disable_ipv6" = true;

  networking.interfaces.${wan-dev} = {
    macAddress = "3c:97:0e:19:7e:5c";
    useDHCP = true;
  };

  networking.interfaces.he0.ipv6 = {
    addresses = [
      {
        address = "2001:470:1f06:1195::2";
        prefixLength = 64;
      }
      {
        address = "2001:470:8e2e:20::d";
        prefixLength = 64;
      }
    ];
    routes = [{ address = "::"; prefixLength = 0; }];
  };

  networking.interfaces."${lan-dev}".useDHCP = false;
  networking.interfaces."${lan-dev}.255" = {
    ipv4.addresses = [{
      address = "10.10.255.3";
      prefixLength = 24;
    }];
    ipv6.addresses = [{
      address = "2001:470:8e2e:ffff::3";
      prefixLength = 64;
    }];
    useDHCP = false;
  };
  networking.interfaces."${lan-dev}.100" = {
    ipv4.addresses = [{
      address = "172.20.100.1";
      prefixLength = 24;
    }];
    ipv6.addresses = [{
      address = "2001:470:8e2e:100::1";
      prefixLength = 64;
    }];
    useDHCP = false;
  };
  networking.interfaces."${lan-dev}.30" = {
    ipv4.addresses = [{
      address = "172.20.30.1";
      prefixLength = 24;
    }];
    useDHCP = false;
  };
  networking.interfaces."${lan-dev}.32" = {
    ipv4.addresses = [{
      address = "172.20.32.1";
      prefixLength = 23;
    }];
    ipv6.addresses = [{
      address = "2001:470:8e2e:32::1";
      prefixLength = 64;
    }];
    useDHCP = false;
  };
}