2020-05-29 03:53:35 -07:00
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
|
|
|
|
with lib;
|
|
|
|
|
|
|
|
let
|
|
|
|
cfg = config.nix-bitcoin.netns-isolation;
|
|
|
|
|
|
|
|
netns = builtins.mapAttrs (n: v: {
|
|
|
|
inherit (v) id;
|
|
|
|
address = "169.254.${toString cfg.addressblock}.${toString v.id}";
|
2020-08-21 13:35:52 -07:00
|
|
|
availableNetns = availableNetns.${n};
|
2020-08-21 13:36:00 -07:00
|
|
|
netnsName = "nb-${n}";
|
2020-05-29 03:53:35 -07:00
|
|
|
}) enabledServices;
|
|
|
|
|
|
|
|
# Symmetric netns connection matrix
|
|
|
|
# if clightning.connections = [ "bitcoind" ]; then
|
|
|
|
# availableNetns.bitcoind = [ "clighting" ];
|
|
|
|
# and
|
|
|
|
# availableNetns.clighting = [ "bitcoind" ];
|
2020-08-21 13:35:57 -07:00
|
|
|
#
|
|
|
|
# FIXME: Although negligible for our purposes, this calculation's runtime
|
|
|
|
# is in the order of (number of connections * number of services),
|
|
|
|
# because attrsets and lists are fully copied on each update with '//' or '++'.
|
|
|
|
# This can only be improved with an update in the nix language.
|
|
|
|
#
|
2020-05-29 03:53:35 -07:00
|
|
|
availableNetns = let
|
|
|
|
# base = { clightning = [ "bitcoind" ]; ... }
|
|
|
|
base = builtins.mapAttrs (n: v:
|
|
|
|
builtins.filter isEnabled v.connections
|
|
|
|
) enabledServices;
|
|
|
|
in
|
|
|
|
foldl (xs: s1:
|
|
|
|
foldl (xs: s2:
|
|
|
|
xs // { "${s2}" = xs.${s2} ++ [ s1 ]; }
|
|
|
|
) xs cfg.services.${s1}.connections
|
|
|
|
) base (builtins.attrNames base);
|
|
|
|
|
|
|
|
enabledServices = filterAttrs (n: v: isEnabled n) cfg.services;
|
|
|
|
isEnabled = x: config.services.${x}.enable;
|
|
|
|
|
|
|
|
ip = "${pkgs.iproute}/bin/ip";
|
|
|
|
iptables = "${config.networking.firewall.package}/bin/iptables";
|
|
|
|
|
|
|
|
bridgeIp = "169.254.${toString cfg.addressblock}.10";
|
|
|
|
|
2020-08-21 13:36:00 -07:00
|
|
|
mkCliExec = service: "exec netns-exec ${netns.${service}.netnsName}";
|
2020-05-29 03:53:35 -07:00
|
|
|
in {
|
|
|
|
options.nix-bitcoin.netns-isolation = {
|
|
|
|
enable = mkEnableOption "netns isolation";
|
|
|
|
|
|
|
|
addressblock = mkOption {
|
|
|
|
type = types.ints.u8;
|
2020-10-20 09:20:36 -07:00
|
|
|
default = 1;
|
2020-05-29 03:53:35 -07:00
|
|
|
description = ''
|
2020-08-21 13:35:53 -07:00
|
|
|
The address block N in 169.254.N.0/24, used as the prefix for netns addresses.
|
2020-05-29 03:53:35 -07:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
services = mkOption {
|
|
|
|
default = {};
|
|
|
|
type = types.attrsOf (types.submodule {
|
|
|
|
options = {
|
|
|
|
id = mkOption {
|
|
|
|
# TODO: Assert uniqueness
|
2020-08-21 13:35:54 -07:00
|
|
|
type = types.ints.between 11 255;
|
2020-05-29 03:53:35 -07:00
|
|
|
description = ''
|
2020-08-21 13:35:54 -07:00
|
|
|
id for the netns, used for the IP address host part and
|
|
|
|
for naming the interfaces. Must be unique. Must be greater than 10.
|
2020-05-29 03:53:35 -07:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
connections = mkOption {
|
|
|
|
type = with types; listOf str;
|
|
|
|
default = [];
|
|
|
|
};
|
|
|
|
};
|
|
|
|
});
|
|
|
|
};
|
2020-08-21 13:36:01 -07:00
|
|
|
|
|
|
|
allowedUser = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
description = ''
|
|
|
|
User that is allowed to execute commands in the service network namespaces.
|
|
|
|
The user's group is also authorized.
|
|
|
|
'';
|
2020-09-28 04:09:03 -07:00
|
|
|
default = config.nix-bitcoin.operator.name;
|
2020-08-21 13:36:01 -07:00
|
|
|
};
|
2020-08-21 13:36:02 -07:00
|
|
|
|
|
|
|
netns = mkOption {
|
|
|
|
default = netns;
|
|
|
|
readOnly = true;
|
|
|
|
description = "Exposes netns parameters.";
|
|
|
|
};
|
2020-05-29 03:53:35 -07:00
|
|
|
};
|
|
|
|
|
2020-08-21 13:35:59 -07:00
|
|
|
config = mkIf cfg.enable (mkMerge [
|
|
|
|
|
|
|
|
# Base infrastructure
|
|
|
|
{
|
2020-08-21 13:36:04 -07:00
|
|
|
networking.dhcpcd.denyInterfaces = [ "nb-br" "nb-veth*" ];
|
2020-08-04 06:21:40 -07:00
|
|
|
services.tor.client.socksListenAddress = "${bridgeIp}:9050";
|
2020-08-21 13:36:03 -07:00
|
|
|
networking.firewall.interfaces.nb-br.allowedTCPPorts = [ 9050 ];
|
2020-08-04 06:21:40 -07:00
|
|
|
boot.kernel.sysctl."net.ipv4.ip_forward" = true;
|
2020-08-21 13:36:05 -07:00
|
|
|
|
2020-08-04 06:21:40 -07:00
|
|
|
security.wrappers.netns-exec = {
|
2020-08-21 13:36:06 -07:00
|
|
|
source = pkgs.nix-bitcoin.netns-exec;
|
2020-08-04 06:21:40 -07:00
|
|
|
capabilities = "cap_sys_admin=ep";
|
2020-08-21 13:36:01 -07:00
|
|
|
owner = cfg.allowedUser;
|
2020-08-04 06:21:40 -07:00
|
|
|
permissions = "u+rx,g+rx,o-rwx";
|
|
|
|
};
|
|
|
|
|
|
|
|
systemd.services = {
|
2020-08-21 13:36:05 -07:00
|
|
|
# Due to a NixOS bug we can't currently use option `networking.bridges` to
|
|
|
|
# setup the bridge while `networking.useDHCP` is enabled.
|
|
|
|
nb-netns-bridge = {
|
|
|
|
description = "nix-bitcoin netns bridge";
|
|
|
|
wantedBy = [ "network-setup.service" ];
|
|
|
|
partOf = [ "network-setup.service" ];
|
|
|
|
before = [ "network-setup.service" ];
|
|
|
|
after = [ "network-pre.target" ];
|
|
|
|
serviceConfig = {
|
|
|
|
Type = "oneshot";
|
|
|
|
RemainAfterExit = "yes";
|
|
|
|
};
|
2020-08-04 06:21:40 -07:00
|
|
|
script = ''
|
2020-08-21 13:36:03 -07:00
|
|
|
${ip} link add name nb-br type bridge
|
|
|
|
${ip} link set nb-br up
|
|
|
|
${ip} addr add ${bridgeIp}/24 brd + dev nb-br
|
2020-08-04 06:21:40 -07:00
|
|
|
${iptables} -w -t nat -A POSTROUTING -s 169.254.${toString cfg.addressblock}.0/24 -j MASQUERADE
|
|
|
|
'';
|
|
|
|
preStop = ''
|
|
|
|
${iptables} -w -t nat -D POSTROUTING -s 169.254.${toString cfg.addressblock}.0/24 -j MASQUERADE
|
2020-08-21 13:36:03 -07:00
|
|
|
${ip} link del nb-br
|
2020-08-04 06:21:40 -07:00
|
|
|
'';
|
2020-05-29 03:53:35 -07:00
|
|
|
};
|
2020-04-23 09:18:47 -07:00
|
|
|
|
2020-08-04 06:21:40 -07:00
|
|
|
} //
|
|
|
|
(let
|
|
|
|
makeNetnsServices = n: v: let
|
2020-08-21 13:36:04 -07:00
|
|
|
veth = "nb-veth-${toString v.id}";
|
|
|
|
peer = "nb-veth-br-${toString v.id}";
|
2020-08-21 13:36:00 -07:00
|
|
|
inherit (v) netnsName;
|
2020-08-04 06:21:40 -07:00
|
|
|
ipNetns = "${ip} -n ${netnsName}";
|
|
|
|
netnsIptables = "${ip} netns exec ${netnsName} ${config.networking.firewall.package}/bin/iptables";
|
|
|
|
in {
|
|
|
|
"${n}".serviceConfig.NetworkNamespacePath = "/var/run/netns/${netnsName}";
|
|
|
|
|
|
|
|
"netns-${n}" = rec {
|
2020-08-21 13:36:05 -07:00
|
|
|
requires = [ "nb-netns-bridge.service" ];
|
|
|
|
after = [ "nb-netns-bridge.service" ];
|
2020-08-04 06:21:40 -07:00
|
|
|
bindsTo = [ "${n}.service" ];
|
|
|
|
requiredBy = bindsTo;
|
|
|
|
before = bindsTo;
|
2020-05-29 03:53:35 -07:00
|
|
|
script = ''
|
2020-08-04 06:21:40 -07:00
|
|
|
${ip} netns add ${netnsName}
|
|
|
|
${ipNetns} link set lo up
|
2020-08-21 13:36:04 -07:00
|
|
|
${ip} link add ${veth} type veth peer name ${peer}
|
|
|
|
${ip} link set ${veth} netns ${netnsName}
|
|
|
|
${ipNetns} addr add ${v.address}/24 dev ${veth}
|
|
|
|
${ip} link set ${peer} up
|
|
|
|
${ipNetns} link set ${veth} up
|
|
|
|
${ip} link set ${peer} master nb-br
|
2020-08-04 06:21:40 -07:00
|
|
|
${ipNetns} route add default via ${bridgeIp}
|
|
|
|
${netnsIptables} -w -P INPUT DROP
|
|
|
|
${netnsIptables} -w -A INPUT -s 127.0.0.1,${bridgeIp},${v.address} -j ACCEPT
|
2020-08-12 07:46:16 -07:00
|
|
|
# allow return traffic to outgoing connections initiated by the service itself
|
|
|
|
${netnsIptables} -w -A INPUT -m conntrack --ctstate ESTABLISHED -j ACCEPT
|
2020-08-04 06:21:40 -07:00
|
|
|
'' + (optionalString (config.services.${n}.enforceTor or false)) ''
|
|
|
|
${netnsIptables} -w -P OUTPUT DROP
|
|
|
|
${netnsIptables} -w -A OUTPUT -d 127.0.0.1,${bridgeIp},${v.address} -j ACCEPT
|
|
|
|
'' + concatMapStrings (otherNetns: let
|
|
|
|
other = netns.${otherNetns};
|
|
|
|
in ''
|
|
|
|
${netnsIptables} -w -A INPUT -s ${other.address} -j ACCEPT
|
|
|
|
${netnsIptables} -w -A OUTPUT -d ${other.address} -j ACCEPT
|
|
|
|
'') v.availableNetns;
|
2020-05-29 03:53:35 -07:00
|
|
|
preStop = ''
|
2020-08-04 06:21:40 -07:00
|
|
|
${ip} netns delete ${netnsName}
|
2020-08-21 13:36:04 -07:00
|
|
|
${ip} link del ${peer}
|
2020-05-29 03:53:35 -07:00
|
|
|
'';
|
|
|
|
serviceConfig = {
|
|
|
|
Type = "oneshot";
|
|
|
|
RemainAfterExit = "yes";
|
2020-08-04 06:21:40 -07:00
|
|
|
ExecStartPre = "-${ip} netns delete ${netnsName}";
|
2020-05-29 03:53:35 -07:00
|
|
|
};
|
|
|
|
};
|
2020-06-10 07:56:07 -07:00
|
|
|
};
|
2020-08-04 06:21:40 -07:00
|
|
|
in foldl (services: n:
|
|
|
|
services // (makeNetnsServices n netns.${n})
|
|
|
|
) {} (builtins.attrNames netns));
|
2020-08-21 13:35:59 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
# Service-specific config
|
|
|
|
{
|
|
|
|
nix-bitcoin.netns-isolation.services = {
|
|
|
|
bitcoind = {
|
|
|
|
id = 12;
|
|
|
|
};
|
|
|
|
clightning = {
|
|
|
|
id = 13;
|
|
|
|
connections = [ "bitcoind" ];
|
|
|
|
};
|
|
|
|
lnd = {
|
|
|
|
id = 14;
|
|
|
|
connections = [ "bitcoind" ];
|
|
|
|
};
|
|
|
|
liquidd = {
|
|
|
|
id = 15;
|
|
|
|
connections = [ "bitcoind" ];
|
|
|
|
};
|
|
|
|
electrs = {
|
|
|
|
id = 16;
|
|
|
|
connections = [ "bitcoind" ];
|
|
|
|
};
|
|
|
|
spark-wallet = {
|
|
|
|
id = 17;
|
|
|
|
# communicates with clightning over lightning-rpc socket
|
|
|
|
};
|
|
|
|
lightning-charge = {
|
|
|
|
id = 18;
|
|
|
|
# communicates with clightning over lightning-rpc socket
|
|
|
|
};
|
|
|
|
nanopos = {
|
|
|
|
id = 19;
|
|
|
|
connections = [ "nginx" "lightning-charge" ];
|
|
|
|
};
|
|
|
|
recurring-donations = {
|
|
|
|
id = 20;
|
|
|
|
# communicates with clightning over lightning-rpc socket
|
|
|
|
};
|
|
|
|
nginx = {
|
|
|
|
id = 21;
|
|
|
|
};
|
|
|
|
lightning-loop = {
|
|
|
|
id = 22;
|
|
|
|
connections = [ "lnd" ];
|
|
|
|
};
|
2020-08-12 07:47:56 -07:00
|
|
|
nbxplorer = {
|
|
|
|
id = 23;
|
|
|
|
connections = [ "bitcoind" ];
|
|
|
|
};
|
|
|
|
btcpayserver = {
|
|
|
|
id = 24;
|
|
|
|
connections = [ "nbxplorer" ]
|
|
|
|
++ optional (config.services.btcpayserver.lightningBackend == "lnd") "lnd";
|
|
|
|
# communicates with clightning over rpc socket
|
|
|
|
};
|
2020-04-23 09:18:47 -07:00
|
|
|
joinmarket = {
|
|
|
|
id = 25;
|
|
|
|
connections = [ "bitcoind" ];
|
|
|
|
};
|
2020-08-21 13:35:59 -07:00
|
|
|
};
|
2020-06-10 07:56:07 -07:00
|
|
|
|
2020-08-04 06:21:40 -07:00
|
|
|
services.bitcoind = {
|
|
|
|
bind = netns.bitcoind.address;
|
2020-10-29 13:20:32 -07:00
|
|
|
rpcbind = netns.bitcoind.address;
|
2020-08-04 06:21:40 -07:00
|
|
|
rpcallowip = [
|
2020-10-29 13:20:29 -07:00
|
|
|
bridgeIp # For operator user
|
|
|
|
netns.bitcoind.address
|
|
|
|
] ++ map (n: netns.${n}.address) netns.bitcoind.availableNetns;
|
2020-08-04 06:21:40 -07:00
|
|
|
};
|
2020-08-21 13:35:59 -07:00
|
|
|
systemd.services.bitcoind-import-banlist.serviceConfig.NetworkNamespacePath = "/var/run/netns/nb-bitcoind";
|
2020-06-10 07:31:38 -07:00
|
|
|
|
2020-10-16 08:43:00 -07:00
|
|
|
services.clightning.bind-addr = netns.clightning.address;
|
2020-06-10 07:34:14 -07:00
|
|
|
|
2020-08-21 13:35:58 -07:00
|
|
|
services.lnd = {
|
2020-08-04 06:21:40 -07:00
|
|
|
listen = netns.lnd.address;
|
2020-10-29 13:20:30 -07:00
|
|
|
rpclisten = [ netns.lnd.address ];
|
|
|
|
restlisten = [ netns.lnd.address ];
|
2020-08-04 06:21:40 -07:00
|
|
|
};
|
2020-06-10 07:36:03 -07:00
|
|
|
|
2020-08-21 13:35:58 -07:00
|
|
|
services.liquidd = {
|
2020-08-04 06:21:40 -07:00
|
|
|
bind = netns.liquidd.address;
|
2020-10-29 13:20:31 -07:00
|
|
|
rpcbind = [ netns.liquidd.address ];
|
2020-08-04 06:21:40 -07:00
|
|
|
rpcallowip = [
|
2020-10-29 13:20:31 -07:00
|
|
|
bridgeIp # For operator user
|
|
|
|
netns.liquidd.address
|
|
|
|
] ++ map (n: netns.${n}.address) netns.liquidd.availableNetns;
|
2020-08-04 06:21:40 -07:00
|
|
|
};
|
2020-05-29 04:13:50 -07:00
|
|
|
|
2020-09-27 03:43:15 -07:00
|
|
|
services.electrs.address = netns.electrs.address;
|
2020-06-10 07:41:13 -07:00
|
|
|
|
2020-08-21 13:35:58 -07:00
|
|
|
services.spark-wallet = {
|
2020-08-04 06:21:40 -07:00
|
|
|
host = netns.spark-wallet.address;
|
|
|
|
extraArgs = "--no-tls";
|
|
|
|
};
|
2020-06-10 07:43:03 -07:00
|
|
|
|
2020-08-21 13:35:58 -07:00
|
|
|
services.lightning-charge.host = netns.lightning-charge.address;
|
2020-06-10 07:44:50 -07:00
|
|
|
|
2020-08-21 13:35:58 -07:00
|
|
|
services.nanopos = {
|
2020-08-04 06:21:40 -07:00
|
|
|
charged-url = "http://${netns.lightning-charge.address}:9112";
|
|
|
|
host = netns.nanopos.address;
|
|
|
|
};
|
2020-06-10 07:48:20 -07:00
|
|
|
|
2020-08-21 13:36:00 -07:00
|
|
|
services.lightning-loop.cliExec = mkCliExec "lightning-loop";
|
2020-08-12 07:47:56 -07:00
|
|
|
|
|
|
|
services.nbxplorer.bind = netns.nbxplorer.address;
|
|
|
|
services.btcpayserver.bind = netns.btcpayserver.address;
|
2020-04-23 09:18:47 -07:00
|
|
|
|
|
|
|
services.joinmarket.cliExec = mkCliExec "joinmarket";
|
|
|
|
systemd.services.joinmarket-yieldgenerator.serviceConfig.NetworkNamespacePath = "/var/run/netns/nb-joinmarket";
|
2020-08-21 13:35:59 -07:00
|
|
|
}
|
|
|
|
]);
|
2020-05-29 03:53:35 -07:00
|
|
|
}
|