1 { pkgs, lib, config, ... }:
5 inherit (config.services) openvpn;
8 networking.nftables.ruleset = ''
9 #add rule inet filter fw2net tcp dport {443,1194} counter accept comment "OpenVPN"
10 add rule inet filter fw2net udp dport 1194 counter accept comment "OpenVPN"
12 systemd.services."openvpn-${ns}" = {
13 bindsTo = [ "netns-${ns}.service" ];
14 requires = [ "netns-${ns}.service" ];
16 services.netns.namespaces.riseup = {
17 nftables = lib.mkBefore ''
19 include "${../../../../var/nftables/filter.txt}"
21 type filter hook input priority filter
25 ct state { established, related } accept
26 jump accept-connectivity-input
31 type filter hook forward priority filter
33 jump accept-connectivity-forward
36 type filter hook output priority filter
39 ct state { related, established } accept
40 jump accept-connectivity-output
45 services.openvpn.servers = {
48 cert ${riseup/client.pem}
49 key ${riseup/client.pem}
50 remote 37.218.241.7 1194 tcp4
51 remote 37.218.241.106 443 tcp4
52 remote 163.172.126.44 443 tcp4
53 remote 198.252.153.28 443 tcp4
54 remote 199.58.81.143 443 tcp4
55 remote 199.58.81.145 443 tcp4
56 remote 212.83.143.67 443 tcp4
57 remote 212.83.144.12 443 tcp4
58 remote 212.83.146.228 443 tcp4
59 remote 212.83.165.160 443 tcp4
60 remote 212.83.182.127 443 tcp4
61 remote 212.129.62.247 443 tcp4
62 ca ${riseup/cacert.pem}
66 ca ${riseup/RiseupCA.pem}
72 # Useless to setup the interface
73 # because moving it to ${ns} will reset it
77 auth-user-pass /root/riseup.auth
79 remote-cert-tls server
80 remote 198.252.153.226 1194 udp
85 up = let dev = "ov-${ns}"; in ''
87 PATH=${lib.makeBinPath [pkgs.iproute]}
88 ip link set dev "${dev}" up netns "${ns}" mtu "$tun_mtu"
89 ip netns exec "${ns}" ${pkgs.writeShellScript "route-up.sh" ''
91 PATH=${lib.makeBinPath [pkgs.iproute pkgs.coreutils]}
95 mkdir -p /etc/netns/"${ns}"
97 process_foreign_option () {
99 dhcp-option:DNS) echo "nameserver $3" >>/etc/netns/"${ns}"/resolv.conf ;;
100 dhcp-option:DOMAIN) foreign_opt_domains="$foreign_opt_domains $3" ;;
103 if test ! -e /etc/netns/"${ns}"/resolv.conf; then
104 # add DNS settings if given in foreign options
107 eval opt=\"\''${foreign_option_$i-}\"
110 process_foreign_option $opt
113 for d in $foreign_opt_domains; do
114 printf '%s\n' "domain $1" "search $*" \
115 >>/etc/netns/"${ns}"/resolv.conf
119 netmask4="''${ifconfig_netmask:-30}"
120 netbits6="''${ifconfig_ipv6_netbits:-112}"
121 if [ -n "''${ifconfig_local-}" ]; then
122 if [ -n "''${ifconfig_remote-}" ]; then
124 local "$ifconfig_local" \
125 peer "$ifconfig_remote/$netmask4" \
126 ''${ifconfig_broadcast:+broadcast "$ifconfig_broadcast"} \
130 local "$ifconfig_local/$netmask4" \
131 ''${ifconfig_broadcast:+broadcast "$ifconfig_broadcast"} \
135 if [ -n "''${ifconfig_ipv6_local-}" ]; then
136 if [ -n "''${ifconfig_ipv6_remote-}" ]; then
138 local "$ifconfig_ipv6_local" \
139 peer "$ifconfig_ipv6_remote/$netbits6" \
143 local "$ifconfig_ipv6_local/$netbits6" \
151 PATH=${lib.makeBinPath [pkgs.iproute]}
152 ${pkgs.coreutils}/bin/env
153 ip netns exec "${ns}" ${pkgs.writeShellScript "route-up.sh" ''
155 PATH=${lib.makeBinPath [pkgs.iproute]}
158 eval net=\"\''${route_network_$i-}\"
159 eval mask=\"\''${route_netmask_$i-}\"
160 eval gw=\"\''${route_gateway_$i-}\"
161 eval mtr=\"\''${route_metric_$i-}\"
164 ip -4 route replace "$net/$mask" via "$gw" ''${mtr:+metric "$mtr"}
168 if [ -n "''${route_vpn_gateway-}" ]; then
169 ip -4 route replace default via "$route_vpn_gateway"
174 # There doesn't seem to be $route_ipv6_metric_<n>
175 # according to the manpage.
176 eval net=\"\''${route_ipv6_network_$i-}\"
177 eval gw=\"\''${route_ipv6_gateway_$i-}\"
180 ip -6 route replace "$net" via "$gw" metric 100
184 # There's no $route_vpn_gateway for IPv6. It's not
185 # documented if OpenVPN includes default route in
186 # $route_ipv6_*. Set default route to remote VPN
187 # endpoint address if there is one. Use higher metric
188 # than $route_ipv6_* routes to give preference to a
189 # possible default route in them.
190 if [ -n "''${ifconfig_ipv6_remote-}" ]; then
191 ip -6 route replace default \
192 via "$ifconfig_ipv6_remote" metric 200