summaryrefslogtreecommitdiff
path: root/hosts/vidhar/network/gpon.nix
diff options
context:
space:
mode:
Diffstat (limited to 'hosts/vidhar/network/gpon.nix')
-rw-r--r--hosts/vidhar/network/gpon.nix271
1 files changed, 271 insertions, 0 deletions
diff --git a/hosts/vidhar/network/gpon.nix b/hosts/vidhar/network/gpon.nix
new file mode 100644
index 00000000..1628159c
--- /dev/null
+++ b/hosts/vidhar/network/gpon.nix
@@ -0,0 +1,271 @@
1{ config, lib, pkgs, ... }:
2
3with lib;
4
5let
6 pppInterface = config.networking.pppInterface;
7in {
8 options = {
9 networking.pppInterface = mkOption {
10 type = types.str;
11 default = "gpon";
12 };
13 };
14
15 config = {
16 networking.vlans = {
17 telekom = {
18 id = 7;
19 interface = "eno2";
20 };
21 };
22
23 services.pppd = {
24 enable = true;
25 peers.telekom.config = ''
26 nodefaultroute
27 ifname ${pppInterface}
28 lcp-echo-adaptive
29 lcp-echo-failure 5
30 lcp-echo-interval 1
31 maxfail 0
32 mtu 1492
33 mru 1492
34 plugin pppoe.so
35 name telekom
36 user 002576900250551137425220#0001@t-online.de
37 nic-telekom
38 debug
39 +ipv6
40 '';
41 };
42 systemd.services."pppd-telekom" = {
43 stopIfChanged = true;
44
45 serviceConfig = {
46 PIDFile = "/run/pppd/${pppInterface}.pid";
47 };
48 restartTriggers = with config; [
49 environment.etc."ppp/ip-pre-up".source
50 environment.etc."ppp/ip-up".source
51 environment.etc."ppp/ip-down".source
52 # sops.secrets."pap-secrets".sopsFile
53 ];
54 };
55 sops.secrets."pap-secrets" = {
56 format = "binary";
57 sopsFile = ./pap-secrets;
58 path = "/etc/ppp/pap-secrets";
59 };
60
61 environment.etc = {
62 "ppp/ip-pre-up".source = let
63 app = pkgs.writeShellApplication {
64 name = "ip-pre-up";
65 runtimeInputs = with pkgs; [ iproute2 ethtool ];
66 text = ''
67 ethtool -K telekom tso off gso off gro off
68
69 ip link del "ifb4${pppInterface}" || true
70 ip link add name "ifb4${pppInterface}" type ifb
71 ip link set "ifb4${pppInterface}" up
72
73 tc qdisc del dev "ifb4${pppInterface}" root || true
74 tc qdisc del dev "${pppInterface}" ingress || true
75 tc qdisc del dev "${pppInterface}" root || true
76
77 tc qdisc add dev "${pppInterface}" handle ffff: ingress
78 tc filter add dev "${pppInterface}" parent ffff: basic action ctinfo dscp 0x0000003f 0x00000040 action mirred egress redirect dev "ifb4${pppInterface}"
79 tc qdisc replace dev "ifb4${pppInterface}" root cake memlimit 128Mb overhead 35 mpu 74 regional diffserv4 bandwidth 285mbit
80 tc qdisc replace dev "${pppInterface}" root cake memlimit 128Mb overhead 35 mpu 74 regional nat diffserv4 wash bandwidth 143mbit
81 '';
82 };
83 in "${app}/bin/${app.meta.mainProgram}";
84 "ppp/ip-up".source = let
85 app = pkgs.writeShellApplication {
86 name = "ip-up";
87 runtimeInputs = with pkgs; [ iproute2 ];
88 text = ''
89 ip route add default via "$5" dev "${pppInterface}" metric 512
90 '';
91 };
92 in "${app}/bin/${app.meta.mainProgram}";
93 "ppp/ip-down".source = let
94 app = pkgs.writeShellApplication {
95 name = "ip-down";
96 runtimeInputs = with pkgs; [ iproute2 ];
97 text = ''
98 ip link del "ifb4${pppInterface}"
99 '';
100 };
101 in "${app}/bin/${app.meta.mainProgram}";
102 };
103
104 systemd.network.networks.${pppInterface} = {
105 matchConfig = {
106 Name = pppInterface;
107 };
108 dns = [ "::1" "127.0.0.1" ];
109 domains = [ "~." ];
110 networkConfig = {
111 LinkLocalAddressing = "no";
112 DNSSEC = true;
113 };
114 };
115
116 services.corerad = {
117 enable = true;
118 settings = {
119 interfaces = [
120 { name = pppInterface;
121 monitor = true;
122 verbose = true;
123 }
124 { name = "lan";
125 advertise = true;
126 verbose = true;
127 prefix = [{ prefix = "::/64"; }];
128 route = [{ prefix = "::/0"; }];
129 rdnss = [{ servers = ["::"]; }];
130 dnssl = [{ domain_names = ["yggdrasil"]; }];
131 # other_config = true;
132 }
133 ];
134
135 debug = {
136 address = "localhost:9430";
137 prometheus = true;
138 };
139 };
140 };
141 services.ndppd = {
142 enable = true;
143 proxies = {
144 ${pppInterface} = {
145 router = true;
146 rules = {
147 lan = {
148 method = "iface";
149 interface = "lan";
150 network = "::/0";
151 };
152 };
153 };
154 };
155 };
156 boot.kernelModules = [ "ifb" ];
157 boot.kernel.sysctl = {
158 "net.ipv6.conf.all.forwarding" = true;
159 "net.ipv6.conf.default.forwarding" = true;
160 "net.ipv4.conf.all.forwarding" = true;
161 "net.ipv4.conf.default.forwarding" = true;
162
163 "net.core.rmem_max" = 4194304;
164 "net.core.wmem_max" = 4194304;
165 };
166 systemd.services."pppd-telekom" = {
167 bindsTo = [ "sys-subsystem-net-devices-telekom.device" ];
168 after = [ "sys-subsystem-net-devices-telekom.device" ];
169 };
170 systemd.services."dhcpcd-${pppInterface}" = {
171 wantedBy = [ "multi-user.target" "network-online.target" "pppd-telekom.service" ];
172 bindsTo = [ "pppd-telekom.service" ];
173 after = [ "pppd-telekom.service" ];
174 wants = [ "network.target" ];
175 before = [ "network-online.target" ];
176
177 path = with pkgs; [ dhcpcd nettools openresolv ];
178 unitConfig.ConditionCapability = "CAP_NET_ADMIN";
179
180 stopIfChanged = true;
181
182 preStart = ''
183 i=0
184
185 while [[ -z "$(${pkgs.iproute2}/bin/ip -6 addr show dev ${pppInterface} scope link)" ]]; do
186 ${pkgs.coreutils}/bin/sleep 0.1
187 i=$((i + 1))
188 if [[ "$i" -ge 10 ]]; then
189 exit 1
190 fi
191 done
192 '';
193
194 postStop = ''
195 for dev in lan; do
196 ${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}"
197 done
198 '';
199
200 serviceConfig = let
201 dhcpcdConf = pkgs.writeText "dhcpcd.conf" ''
202 duid
203 vendorclassid
204 ipv6only
205
206 nooption domain_name_servers, domain_name, domain_search
207 option classless_static_routes
208 option interface_mtu
209
210 option host_name
211 option rapid_commit
212 require dhcp_server_identifier
213 slaac private
214
215 nohook resolv.conf
216 ipv6ra_autoconf
217 iaid 1195061668
218 ipv6rs # enable routing solicitation for WAN adapter
219 ia_pd 1 lan/0/64/0 # request a PD and assign it to the LAN
220
221 reboot 0
222
223 waitip 6
224 '';
225 in {
226 Type = "forking";
227 PIDFile = "/var/run/dhcpcd/${pppInterface}.pid";
228 RuntimeDirectory = "dhcpcd";
229 ExecStart = "@${pkgs.dhcpcd}/sbin/dhcpcd dhcpcd -q --config ${dhcpcdConf} ${pppInterface}";
230 ExecReload = "${pkgs.dhcpcd}/sbin/dhcpcd --rebind ${pppInterface}";
231 Restart = "always";
232 RestartSec = "5";
233 };
234 };
235 systemd.services.ndppd = {
236 wantedBy = [ "dhcpcd-${pppInterface}.service" ];
237 bindsTo = [ "dhcpcd-${pppInterface}.service" ];
238 after = [ "dhcpcd-${pppInterface}.service" ];
239
240 serviceConfig = {
241 Restart = "always";
242 RestartSec = "5";
243 };
244 };
245 systemd.services.corerad = {
246 wantedBy = [ "dhcpcd-${pppInterface}.service" ];
247 bindsTo = [ "dhcpcd-${pppInterface}.service" ];
248 after = [ "dhcpcd-${pppInterface}.service" ];
249
250 serviceConfig = {
251 Restart = lib.mkForce "always";
252 RestartSec = "5";
253 };
254 };
255 users.users.dhcpcd = {
256 isSystemUser = true;
257 group = "dhcpcd";
258 };
259 users.groups.dhcpcd = {};
260
261 systemd.services.unbound = {
262 wantedBy = [ "dhcpcd-${pppInterface}.service" ];
263 bindsTo = [ "dhcpcd-${pppInterface}.service" ];
264 after = [ "dhcpcd-${pppInterface}.service" ];
265
266 serviceConfig = {
267 Restart = lib.mkForce "always";
268 };
269 };
270 };
271}