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"
11 systemd.services."openvpn-${ns}" = {
12 bindsTo = [ "netns-${ns}.service" ];
13 requires = [ "netns-${ns}.service" ];
15 services.netns.namespaces.riseup = {
16 nftables = lib.mkBefore ''
18 include "${../../../../var/nftables/filter.txt}"
20 type filter hook input priority filter
24 ct state { established, related } accept
25 jump accept-connectivity-input
30 type filter hook forward priority filter
32 jump accept-connectivity-forward
35 type filter hook output priority filter
38 ct state { related, established } accept
39 jump accept-connectivity-output
44 services.openvpn.servers = {
48 ca ${riseup/cacert.pem}
49 cert ${riseup/client.pem}
55 # Useless to setup the interface
56 # because moving it to ${ns} will reset it
60 key ${riseup/client.pem}
62 remote-cert-tls server
63 remote 37.218.241.7 1194 tcp4
64 remote 37.218.241.106 443 tcp4
65 remote 163.172.126.44 443 tcp4
66 remote 198.252.153.28 443 tcp4
67 remote 199.58.81.143 443 tcp4
68 remote 199.58.81.145 443 tcp4
69 remote 212.83.143.67 443 tcp4
70 remote 212.83.144.12 443 tcp4
71 remote 212.83.146.228 443 tcp4
72 remote 212.83.165.160 443 tcp4
73 remote 212.83.182.127 443 tcp4
74 remote 212.129.62.247 443 tcp4
79 up = let dev = "ov-${ns}"; in ''
81 PATH=${lib.makeBinPath [pkgs.iproute]}
82 ip link set dev "${dev}" up netns "${ns}" mtu "$tun_mtu"
83 ip netns exec "${ns}" ${pkgs.writeShellScript "route-up.sh" ''
85 PATH=${lib.makeBinPath [pkgs.iproute pkgs.coreutils]}
89 mkdir -p /etc/netns/"${ns}"
91 process_foreign_option () {
93 dhcp-option:DNS) echo "nameserver $3" >>/etc/netns/"${ns}"/resolv.conf ;;
94 dhcp-option:DOMAIN) foreign_opt_domains="$foreign_opt_domains $3" ;;
97 if test ! -e /etc/netns/"${ns}"/resolv.conf; then
98 # add DNS settings if given in foreign options
101 eval opt=\"\''${foreign_option_$i-}\"
104 process_foreign_option $opt
107 for d in $foreign_opt_domains; do
108 printf '%s\n' "domain $1" "search $*" \
109 >>/etc/netns/"${ns}"/resolv.conf
113 netmask4="''${ifconfig_netmask:-30}"
114 netbits6="''${ifconfig_ipv6_netbits:-112}"
115 if [ -n "''${ifconfig_local-}" ]; then
116 if [ -n "''${ifconfig_remote-}" ]; then
118 local "$ifconfig_local" \
119 peer "$ifconfig_remote/$netmask4" \
120 ''${ifconfig_broadcast:+broadcast "$ifconfig_broadcast"} \
124 local "$ifconfig_local/$netmask4" \
125 ''${ifconfig_broadcast:+broadcast "$ifconfig_broadcast"} \
129 if [ -n "''${ifconfig_ipv6_local-}" ]; then
130 if [ -n "''${ifconfig_ipv6_remote-}" ]; then
132 local "$ifconfig_ipv6_local" \
133 peer "$ifconfig_ipv6_remote/$netbits6" \
137 local "$ifconfig_ipv6_local/$netbits6" \
145 PATH=${lib.makeBinPath [pkgs.iproute]}
146 ${pkgs.coreutils}/bin/env
147 ip netns exec "${ns}" ${pkgs.writeShellScript "route-up.sh" ''
149 PATH=${lib.makeBinPath [pkgs.iproute]}
152 eval net=\"\''${route_network_$i-}\"
153 eval mask=\"\''${route_netmask_$i-}\"
154 eval gw=\"\''${route_gateway_$i-}\"
155 eval mtr=\"\''${route_metric_$i-}\"
158 ip -4 route replace "$net/$mask" via "$gw" ''${mtr:+metric "$mtr"}
162 if [ -n "''${route_vpn_gateway-}" ]; then
163 ip -4 route replace default via "$route_vpn_gateway"
168 # There doesn't seem to be $route_ipv6_metric_<n>
169 # according to the manpage.
170 eval net=\"\''${route_ipv6_network_$i-}\"
171 eval gw=\"\''${route_ipv6_gateway_$i-}\"
174 ip -6 route replace "$net" via "$gw" metric 100
178 # There's no $route_vpn_gateway for IPv6. It's not
179 # documented if OpenVPN includes default route in
180 # $route_ipv6_*. Set default route to remote VPN
181 # endpoint address if there is one. Use higher metric
182 # than $route_ipv6_* routes to give preference to a
183 # possible default route in them.
184 if [ -n "''${ifconfig_ipv6_remote-}" ]; then
185 ip -6 route replace default \
186 via "$ifconfig_ipv6_remote" metric 200