summaryrefslogtreecommitdiff
path: root/hosts/vidhar/network/dsl.nix
blob: 6ba774e436cf18a733265fef2fdb0199ea267821 (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
{ config, lib, pkgs, ... }:

with lib;

let
  pppInterface = config.networking.pppInterface;
in {
  options = {
    networking.pppInterface = mkOption {
      type = types.str;
      default = "dsl";
    };
  };
  
  config = {
    networking.vlans = {
      telekom = {
        id = 7;
        interface = "eno2";
      };
    };
    
    services.pppd = {
      enable = true;
      peers.telekom.config = ''
        nodefaultroute
        ifname ${pppInterface}
        lcp-echo-adaptive
        lcp-echo-failure 2
        lcp-echo-interval 1
        maxfail 0
        mtu 1492
        mru 1492
        plugin rp-pppoe.so
        name telekom
        user 002576900250551137425220#0001@t-online.de
        telekom
        debug
      '';
    };
    systemd.services."pppd-telekom" = {
      stopIfChanged = true;
      
      serviceConfig = lib.mkForce {
        Type = "notify";
        PIDFile = "/run/pppd/${pppInterface}.pid";
        ExecStart = "${lib.getBin pkgs.ppp}/sbin/pppd call telekom up_sdnotify nolog +ipv6";
        Restart = "always";
        RestartSec = 5;

        RuntimeDirectory = "pppd";
        RuntimeDirectoryPreserve = true;
      };
    };
    sops.secrets."pap-secrets" = {
      format = "binary";
      sopsFile = ./pap-secrets;
      path = "/etc/ppp/pap-secrets";
    };

    environment.etc = {
      "ppp/ip-up" = {
        text = ''
          #!${pkgs.runtimeShell}
          ${pkgs.iproute}/bin/ip route add default via "$5" dev "${pppInterface}" metric 512
        '';
        mode = "0555";
      };
    };

    systemd.network.networks.${pppInterface} = {
      matchConfig = {
        Name = pppInterface;
      };
      dns = [ "::1" "127.0.0.1" ];
      domains = [ "~." ];
      networkConfig = {
        LinkLocalAddressing = "no";
        DNSSEC = true;
      };
    };

    services.corerad = {
      enable = true;
      settings = {
        interfaces = [
          { name = pppInterface;
            monitor = true;
            verbose = true;
          }
          { name = "lan";
            advertise = true;
            verbose = true;
            prefix = [{ prefix = "::/64"; }];
            route = [{ prefix = "::/0"; }];
            rdnss = [{ servers = ["::"]; }];
            dnssl = [{ domain_names = ["yggdrasil"]; }];
          }
          { name = "dmz01";
            advertise = true;
            verbose = true;
            prefix = [{ prefix = "::/64"; }];
            route = [{ prefix = "::/0"; }];
            rdnss = [{ servers = ["::"]; }];
          }
        ];

        debug = {
          address = "localhost:9430";
          prometheus = true;
        };
      };
    };
    services.ndppd = {
      enable = true;
      proxies = {
        ${pppInterface} = {
          router = true;
          rules = {
            lan = {
              method = "iface";
              interface = "lan";
              network = "::/0";
            };
            dmz01 = {
              method = "iface";
              interface = "dmz01";
              network = "::/0";
            };
          };
        };
      };
    };
    boot.kernel.sysctl = {
      "net.ipv6.conf.all.forwarding" = true;
      "net.ipv6.conf.default.forwarding" = true;
      "net.ipv4.conf.all.forwarding" = true;
      "net.ipv4.conf.default.forwarding" = true;

      "net.core.rmem_max" = "4194304";
      "net.core.wmem_max" = "4194304";
    };
    systemd.services."pppd-telekom" = {
      bindsTo = [ "sys-subsystem-net-devices-telekom.device" ];
      after = [ "sys-subsystem-net-devices-telekom.device" ];
    };
    systemd.services."dhcpcd-${pppInterface}" = {
      wantedBy = [ "multi-user.target" "network-online.target" "pppd-telekom.service" ];
      bindsTo = [ "pppd-telekom.service" "sys-subsystem-net-devices-${pppInterface}.device" ];
      after = [ "pppd-telekom.service" "sys-subsystem-net-devices-${pppInterface}.device" ];
      wants = [ "network.target" ];
      before = [ "network-online.target" ];

      path = with pkgs; [ dhcpcd nettools openresolv ];
      unitConfig.ConditionCapability = "CAP_NET_ADMIN";

      stopIfChanged = true;

      preStart = ''
        i=0

        while [[ -z "$(${pkgs.iproute2}/bin/ip -6 addr show dev ${pppInterface} scope link)" ]]; do
          ${pkgs.coreutils}/bin/sleep 0.1
          i=$((i + 1))
          if [[ "$i" -ge 10 ]]; then
            exit 1
          fi
        done
      '';

      postStop = ''
        for dev in lan dmz01; do
          ${pkgs.iproute2}/bin/ip -6 a show dev "''${dev}" scope global | ${pkgs.gnugrep}/bin/grep inet6 | ${pkgs.gawk}/bin/awk '{ print $2; }' | ${pkgs.findutils}/bin/xargs -I '{}' -- ${pkgs.iproute2}/bin/ip addr del '{}' dev "''${dev}"
        done
      '';

      serviceConfig = let
        dhcpcdConf = pkgs.writeText "dhcpcd.conf" ''
          duid
          vendorclassid
          ipv6only

          nooption domain_name_servers, domain_name, domain_search
          option classless_static_routes
          option interface_mtu

          option host_name
          option rapid_commit
          require dhcp_server_identifier
          slaac private

          nohook resolv.conf
          ipv6ra_autoconf
          iaid 1195061668
          ipv6rs                 # enable routing solicitation for WAN adapter
          ia_pd 1 lan/0/64/0     # request a PD and assign it to the LAN
          ia_pd 1 dmz01/1/64/0   # request a PD and assign it to dmz01

          reboot 0

          waitip 6
        '';
      in {
        Type = "forking";
        PIDFile = "/var/run/dhcpcd/${pppInterface}.pid";
        RuntimeDirectory = "dhcpcd";
        ExecStart = "@${pkgs.dhcpcd}/sbin/dhcpcd dhcpcd -q --config ${dhcpcdConf} ${pppInterface}";
        ExecReload = "${pkgs.dhcpcd}/sbin/dhcpcd --rebind ${pppInterface}";
        Restart = "always";
        RestartSec = "5";
      };
    };
    systemd.services.ndppd = {
      wantedBy = [ "dhcpcd-${pppInterface}.service" ];
      bindsTo = [ "dhcpcd-${pppInterface}.service" ];
      after = [ "dhcpcd-${pppInterface}.service" ];

      serviceConfig = {
        Restart = "always";
        RestartSec = "5";
      };
    };
    systemd.services.corerad = {
      wantedBy = [ "dhcpcd-${pppInterface}.service" ];
      bindsTo = [ "dhcpcd-${pppInterface}.service" ];
      after = [ "dhcpcd-${pppInterface}.service" ];

      serviceConfig = {
        Restart = lib.mkForce "always";
        RestartSec = "5";
      };
    };
    users.users.dhcpcd = {
      isSystemUser = true;
      group = "dhcpcd";
    };
    users.groups.dhcpcd = {};
  };
}