1 From 14ef78b30a3100dc5e4621bb8c3601a7604b730a Mon Sep 17 00:00:00 2001
2 From: Julien Moutinho <julm+nixpkgs@sourcephile.fr>
3 Date: Sun, 17 Jan 2021 15:28:25 +0100
4 Subject: [PATCH] nixos/openvpn: add netns support
7 nixos/modules/services/networking/openvpn.nix | 459 ++++++++++++++++++++--------
8 1 file changed, 342 insertions(+), 117 deletions(-)
10 diff --git a/nixos/modules/services/networking/openvpn.nix b/nixos/modules/services/networking/openvpn.nix
11 index cea5b180a3..0145fdd9fe 100644
12 --- a/nixos/modules/services/networking/openvpn.nix
13 +++ b/nixos/modules/services/networking/openvpn.nix
17 cfg = config.services.openvpn;
21 + enabledServers = filterAttrs (name: srv: srv.enable) cfg.servers;
23 + inherit (pkgs) openvpn;
25 + PATH = name: makeBinPath config.systemd.services."openvpn-${name}".path;
27 + makeOpenVPNJob = cfg: name:
30 - path = makeBinPath (getAttr "openvpn-${name}" config.systemd.services).path;
35 - # For convenience in client scripts, extract the remote domain
36 - # name and name server.
37 - for var in ''${!foreign_option_*}; do
39 - if [ "''${x[0]}" = dhcp-option ]; then
40 - if [ "''${x[1]}" = DOMAIN ]; then domain="''${x[2]}"
41 - elif [ "''${x[1]}" = DNS ]; then nameserver="''${x[2]}"
47 - ${optionalString cfg.updateResolvConf "${pkgs.update-resolv-conf}/libexec/openvpn/update-resolv-conf"}
52 - ${optionalString cfg.updateResolvConf "${pkgs.update-resolv-conf}/libexec/openvpn/update-resolv-conf"}
56 - configFile = pkgs.writeText "openvpn-config-${name}" ''
58 - ${optionalString (cfg.up != "" || cfg.down != "" || cfg.updateResolvConf) "script-security 2"}
61 - cfg.up != "" || cfg.updateResolvConf
62 - ) "up ${pkgs.writeShellScript "openvpn-${name}-up" upScript}"}
64 - cfg.down != "" || cfg.updateResolvConf
65 - ) "down ${pkgs.writeShellScript "openvpn-${name}-down" downScript}"}
66 - ${optionalString (cfg.authUserPass != null) (
67 - if isAttrs cfg.authUserPass then
68 - "auth-user-pass ${pkgs.writeText "openvpn-credentials-${name}" ''
69 - ${cfg.authUserPass.username}
70 - ${cfg.authUserPass.password}
73 - "auth-user-pass ${cfg.authUserPass}"
76 + configFile = pkgs.writeText "openvpn-config-${name}" (
77 + generators.toKeyValue
79 + mkKeyValue = key: value:
80 + if hasAttr key scripts
81 + then "${key} " + pkgs.writeShellScript "openvpn-${name}-${key}" (scripts.${key} value)
82 + else if builtins.isBool value
83 + then optionalString value key
84 + else if builtins.isPath value
85 + then "${key} ${value}"
86 + else if builtins.isList value
87 + then concatMapStringsSep "\n" (v: "${key} ${generators.mkValueStringDefault {} v}") value
88 + else "${key} ${generators.mkValueStringDefault {} value}";
97 + export PATH=${PATH name}
99 + # For convenience in client scripts, extract the remote domain
100 + # name and name server.
101 + for var in ''${!foreign_option_*}; do
103 + if [ "''${x[0]}" = dhcp-option ]; then
104 + if [ "''${x[1]}" = DOMAIN ]; then domain="''${x[2]}"
105 + elif [ "''${x[1]}" = DNS ]; then nameserver="''${x[2]}"
110 + ${optionalString cfg.updateResolvConf
111 + "${pkgs.update-resolv-conf}/libexec/openvpn/update-resolv-conf"}
113 + # Add DNS settings given in foreign DHCP options to the resolv.conf of the netns.
114 + # Note that JoinsNamespaceOf="netns-${cfg.netns}.service" will not
115 + # BindReadOnlyPaths=["/etc/netns/${cfg.netns}/resolv.conf:/etc/resolv.conf"];
116 + # this will have to be added in each service joining the namespace.
117 + setNetNSResolvConf = ''
118 + mkdir -p /etc/netns/${cfg.netns}
119 + # This file is normally created by netns-${cfg.netns}.service,
120 + # care must be taken to not delete it but to truncate it
121 + # in order to propagate the changes to bind-mounted versions.
122 + : > /etc/netns/${cfg.netns}/resolv.conf
123 + chmod 644 /etc/netns/${cfg.netns}/resolv.conf
124 + foreign_opt_domains=
125 + process_foreign_option () {
127 + dhcp-option:DNS) echo "nameserver $3" >>/etc/netns/'${cfg.netns}'/resolv.conf ;;
128 + dhcp-option:DOMAIN) foreign_opt_domains="$foreign_opt_domains $3" ;;
133 + eval opt=\"\''${foreign_option_$i-}\"
136 + process_foreign_option $opt
139 + for d in $foreign_opt_domains; do
140 + printf '%s\n' "domain $1" "search $*" \
141 + >>/etc/netns/'${cfg.netns}'/resolv.conf
145 + if cfg.netns == null
151 + export PATH=${PATH name}
153 + ${setNetNSResolvConf}
154 + ip link set dev '${cfg.settings.dev}' up netns '${cfg.netns}' mtu "$tun_mtu"
155 + ip netns exec '${cfg.netns}' ${pkgs.writeShellScript "openvpn-${name}-up-netns.sh" ''
158 + export PATH=${PATH name}
160 + ip link set dev lo up
162 + netmask4="''${ifconfig_netmask:-30}"
163 + netbits6="''${ifconfig_ipv6_netbits:-112}"
164 + if [ -n "''${ifconfig_local-}" ]; then
165 + if [ -n "''${ifconfig_remote-}" ]; then
166 + ip -4 addr replace \
167 + local "$ifconfig_local" \
168 + peer "$ifconfig_remote/$netmask4" \
169 + ''${ifconfig_broadcast:+broadcast "$ifconfig_broadcast"} \
170 + dev '${cfg.settings.dev}'
172 + ip -4 addr replace \
173 + local "$ifconfig_local/$netmask4" \
174 + ''${ifconfig_broadcast:+broadcast "$ifconfig_broadcast"} \
175 + dev '${cfg.settings.dev}'
178 + if [ -n "''${ifconfig_ipv6_local-}" ]; then
179 + if [ -n "''${ifconfig_ipv6_remote-}" ]; then
180 + ip -6 addr replace \
181 + local "$ifconfig_ipv6_local" \
182 + peer "$ifconfig_ipv6_remote/$netbits6" \
183 + dev '${cfg.settings.dev}'
185 + ip -6 addr replace \
186 + local "$ifconfig_ipv6_local/$netbits6" \
187 + dev '${cfg.settings.dev}'
195 + if cfg.netns == null
198 + export PATH=${PATH name}
200 + ip netns exec '${cfg.netns}' ${pkgs.writeShellScript "openvpn-${name}-route-up-netns" ''
201 + export PATH=${PATH name}
205 + eval net=\"\''${route_network_$i-}\"
206 + eval mask=\"\''${route_netmask_$i-}\"
207 + eval gw=\"\''${route_gateway_$i-}\"
208 + eval mtr=\"\''${route_metric_$i-}\"
211 + ip -4 route replace "$net/$mask" via "$gw" ''${mtr:+metric "$mtr"}
215 + if [ -n "''${route_vpn_gateway-}" ]; then
216 + ip -4 route replace default via "$route_vpn_gateway"
221 + # There doesn't seem to be $route_ipv6_metric_<n>
222 + # according to the manpage.
223 + eval net=\"\''${route_ipv6_network_$i-}\"
224 + eval gw=\"\''${route_ipv6_gateway_$i-}\"
227 + ip -6 route replace "$net" via "$gw" metric 100
231 + # There's no $route_vpn_gateway for IPv6. It's not
232 + # documented if OpenVPN includes default route in
233 + # $route_ipv6_*. Set default route to remote VPN
234 + # endpoint address if there is one. Use higher metric
235 + # than $route_ipv6_* routes to give preference to a
236 + # possible default route in them.
237 + if [ -n "''${ifconfig_ipv6_remote-}" ]; then
238 + ip -6 route replace default \
239 + via "$ifconfig_ipv6_remote" metric 200
247 + export PATH=${PATH name}
248 + ${optionalString cfg.updateResolvConf
249 + "${pkgs.update-resolv-conf}/libexec/openvpn/update-resolv-conf"}
252 + if cfg.netns == null
258 + export PATH=${PATH name}
259 + ip netns exec '${cfg.netns}' ${pkgs.writeShellScript "openvpn-${name}-down-netns.sh" ''
263 + rm -f /etc/netns/'${cfg.netns}'/resolv.conf
271 wantedBy = optional cfg.autoStart "multi-user.target";
272 after = [ "network.target" ];
273 + bindsTo = optional (cfg.netns != null) "netns-${cfg.netns}.service";
274 + requires = optional (cfg.netns != null) "netns-${cfg.netns}.service";
280 serviceConfig.ExecStart = "@${config.services.openvpn.package}/sbin/openvpn openvpn --suppress-timestamps --config ${configFile}";
281 serviceConfig.Restart = "always";
282 + serviceConfig.RestartSec = "5s";
283 serviceConfig.Type = "notify";
287 unitNames = map (n: "openvpn-${n}.service") (builtins.attrNames cfg.servers);
289 "systemctl try-restart ${lib.escapeShellArgs unitNames}";
290 - description = "Restart system OpenVPN connections when returning from sleep";
291 + description = "Sends a signal to OpenVPN process to trigger a restart after return from sleep";
295 @@ -110,30 +261,30 @@
296 example = literalExpression ''
301 # Simplest server configuration: https://community.openvpn.net/openvpn/wiki/StaticKeyMiniHowto
304 - ifconfig 10.8.0.1 10.8.0.2
305 - secret /root/static.key
307 - up = "ip route add ...";
308 - down = "ip route del ...";
310 + ifconfig = "10.8.0.1 10.8.0.2";
311 + secret = "/root/static.key";
312 + up = "ip route add ...";
313 + down = "ip route del ...";
320 - remote vpn.example.org
324 - ca /root/.vpn/ca.crt
325 - cert /root/.vpn/alice.crt
326 - key /root/.vpn/alice.key
328 - up = "echo nameserver $nameserver | ''${pkgs.openresolv}/sbin/resolvconf -m 0 -a $dev";
329 - down = "''${pkgs.openresolv}/sbin/resolvconf -d $dev";
332 + remote = "vpn.example.org";
334 + proto = "tcp-client";
336 + ca = "/root/.vpn/ca.crt";
337 + cert = "/root/.vpn/alice.crt";
338 + key = "/root/.vpn/alice.key";
339 + up = "echo nameserver $nameserver | ''${pkgs.openresolv}/sbin/resolvconf -m 0 -a $dev";
340 + down = "''${pkgs.openresolv}/sbin/resolvconf -d $dev";
345 @@ -147,55 +298,102 @@
351 - attrsOf (submodule {
352 + type = with types; attrsOf (submodule ({ name, config, options, ... }: {
356 + enable = mkEnableOption "OpenVPN server" // { default = true; };
358 - config = mkOption {
359 - type = types.lines;
361 - Configuration of this OpenVPN instance. See
362 - {manpage}`openvpn(8)`
364 + settings = mkOption {
366 + Configuration of this OpenVPN instance. See
367 + {manpage}`openvpn(8)`
370 To import an external config file, use the following definition:
371 `config = "config /path/to/config.ovpn"`
377 - type = types.lines;
379 - Shell commands executed when the instance is starting.
382 + type = types.submodule {
383 + freeformType = with types;
391 + (listOf (oneOf [ bool int str path ]))
395 + options.dev = mkOption {
399 + Shell commands executed when the instance is starting.
402 + options.down = mkOption {
404 + type = types.lines;
406 + Shell commands executed when the instance is shutting down.
409 + options.errors-to-stderr = mkOption {
413 + Output errors to stderr instead of stdout
414 + unless log output is redirected by one of the `--log` options.
417 + options.route-up = mkOption {
419 + type = types.lines;
421 + Run command after routes are added.
424 + options.up = mkOption {
426 + type = types.lines;
428 + Shell commands executed when the instance is starting.
431 + options.script-security = mkOption {
433 + type = types.enum [ 1 2 3 ];
435 + - 1 — (Default) Only call built-in executables such as ifconfig, ip, route, or netsh.
436 + - 2 — Allow calling of built-in executables and user-defined scripts.
437 + - 3 — Allow passwords to be passed to scripts via environmental variables (potentially unsafe).
445 - type = types.lines;
447 - Shell commands executed when the instance is shutting down.
450 + autoStart = mkOption {
453 + description = "Whether this OpenVPN instance should be started automatically.";
456 - autoStart = mkOption {
459 - description = "Whether this OpenVPN instance should be started automatically.";
462 + down = (elemAt settings.type.functor.payload.modules 0).options.down;
463 + up = (elemAt settings.type.functor.payload.modules 0).options.up;
465 - updateResolvConf = mkOption {
469 - Use the script from the update-resolv-conf package to automatically
470 - update resolv.conf with the DNS information provided by openvpn. The
471 - script will be run after the "up" commands and before the "down" commands.
474 + updateResolvConf = mkOption {
478 + Use the script from the update-resolv-conf package to automatically
479 + update resolv.conf with the DNS information provided by openvpn. The
480 + script will be run after the "up" commands and before the "down" commands.
484 authUserPass = mkOption {
496 + type = with types; nullOr str;
497 + description = "Network namespace.";
501 + config.settings = mkMerge
503 + (mkIf (config.netns != null) {
504 + # Useless to setup the interface
505 + # because moving it to the netns will reset it
506 + ifconfig-noexec = true;
507 + route-noexec = true;
508 + script-security = 2;
510 + (mkIf (config.authUserPass != null) {
511 + auth-user-pass = pkgs.writeText "openvpn-auth-user-pass-${name}" ''
512 + ${config.authUserPass.username}
513 + ${config.authUserPass.password}
516 + (mkIf config.updateResolvConf {
517 + script-security = 2;
520 + # Aliases legacy options
521 + down = modules.mkAliasAndWrapDefsWithPriority id (options.down or { });
522 + up = modules.mkAliasAndWrapDefsWithPriority id (options.up or { });
529 @@ -243,14 +471,11 @@
531 ###### implementation
533 - config = mkIf (cfg.servers != { }) {
534 + config = mkIf (enabledServers != { }) {
539 - name: value: nameValuePair "openvpn-${name}" (makeOpenVPNJob value name)
542 + systemd.services = (listToAttrs (mapAttrsToList
543 + (name: value: nameValuePair "openvpn-${name}"
544 + (makeOpenVPNJob value name)) enabledServers))
547 environment.systemPackages = [ cfg.package ];