2020-05-29 10:53:35 +00: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 20:35:52 +00:00
|
|
|
availableNetns = availableNetns.${n};
|
2020-08-21 20:36:00 +00:00
|
|
|
netnsName = "nb-${n}";
|
2020-05-29 10:53:35 +00:00
|
|
|
}) enabledServices;
|
|
|
|
|
|
|
|
# Symmetric netns connection matrix
|
|
|
|
# if clightning.connections = [ "bitcoind" ]; then
|
|
|
|
# availableNetns.bitcoind = [ "clighting" ];
|
|
|
|
# and
|
|
|
|
# availableNetns.clighting = [ "bitcoind" ];
|
2020-08-21 20:35:57 +00: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 10:53:35 +00: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 20:36:00 +00:00
|
|
|
mkCliExec = service: "exec netns-exec ${netns.${service}.netnsName}";
|
2020-05-29 10:53:35 +00:00
|
|
|
in {
|
|
|
|
options.nix-bitcoin.netns-isolation = {
|
|
|
|
enable = mkEnableOption "netns isolation";
|
|
|
|
|
|
|
|
addressblock = mkOption {
|
|
|
|
type = types.ints.u8;
|
|
|
|
default = "1";
|
|
|
|
description = ''
|
2020-08-21 20:35:53 +00:00
|
|
|
The address block N in 169.254.N.0/24, used as the prefix for netns addresses.
|
2020-05-29 10:53:35 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
services = mkOption {
|
|
|
|
default = {};
|
|
|
|
type = types.attrsOf (types.submodule {
|
|
|
|
options = {
|
|
|
|
id = mkOption {
|
|
|
|
# TODO: Assert uniqueness
|
2020-08-21 20:35:54 +00:00
|
|
|
type = types.ints.between 11 255;
|
2020-05-29 10:53:35 +00:00
|
|
|
description = ''
|
2020-08-21 20:35:54 +00: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 10:53:35 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
connections = mkOption {
|
|
|
|
type = with types; listOf str;
|
|
|
|
default = [];
|
|
|
|
};
|
|
|
|
};
|
|
|
|
});
|
|
|
|
};
|
2020-08-21 20:36:01 +00: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-05-29 10:53:35 +00:00
|
|
|
};
|
|
|
|
|
2020-08-21 20:35:59 +00:00
|
|
|
config = mkIf cfg.enable (mkMerge [
|
|
|
|
|
|
|
|
# Base infrastructure
|
|
|
|
{
|
2020-08-04 13:21:40 +00:00
|
|
|
networking.dhcpcd.denyInterfaces = [ "br0" "br-nb*" "nb-veth*" ];
|
|
|
|
services.tor.client.socksListenAddress = "${bridgeIp}:9050";
|
|
|
|
networking.firewall.interfaces.br0.allowedTCPPorts = [ 9050 ];
|
|
|
|
boot.kernel.sysctl."net.ipv4.ip_forward" = true;
|
|
|
|
security.wrappers.netns-exec = {
|
|
|
|
source = "${pkgs.nix-bitcoin.netns-exec}/netns-exec";
|
|
|
|
capabilities = "cap_sys_admin=ep";
|
2020-08-21 20:36:01 +00:00
|
|
|
owner = cfg.allowedUser;
|
2020-08-04 13:21:40 +00:00
|
|
|
permissions = "u+rx,g+rx,o-rwx";
|
|
|
|
};
|
|
|
|
|
|
|
|
systemd.services = {
|
|
|
|
netns-bridge = {
|
|
|
|
description = "Create bridge";
|
|
|
|
requiredBy = [ "tor.service" ];
|
|
|
|
before = [ "tor.service" ];
|
|
|
|
script = ''
|
|
|
|
${ip} link add name br0 type bridge
|
|
|
|
${ip} link set br0 up
|
|
|
|
${ip} addr add ${bridgeIp}/24 brd + dev br0
|
|
|
|
${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
|
|
|
|
${ip} link del br0
|
|
|
|
'';
|
|
|
|
serviceConfig = {
|
|
|
|
Type = "oneshot";
|
|
|
|
RemainAfterExit = "yes";
|
2020-07-07 14:22:17 +00:00
|
|
|
};
|
2020-05-29 10:53:35 +00:00
|
|
|
};
|
2020-08-04 13:21:40 +00:00
|
|
|
} //
|
|
|
|
(let
|
|
|
|
makeNetnsServices = n: v: let
|
|
|
|
vethName = "nb-veth-${toString v.id}";
|
2020-08-21 20:36:00 +00:00
|
|
|
inherit (v) netnsName;
|
2020-08-04 13:21:40 +00: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 {
|
|
|
|
requires = [ "netns-bridge.service" ];
|
|
|
|
after = [ "netns-bridge.service" ];
|
|
|
|
bindsTo = [ "${n}.service" ];
|
|
|
|
requiredBy = bindsTo;
|
|
|
|
before = bindsTo;
|
2020-05-29 10:53:35 +00:00
|
|
|
script = ''
|
2020-08-04 13:21:40 +00:00
|
|
|
${ip} netns add ${netnsName}
|
|
|
|
${ipNetns} link set lo up
|
|
|
|
${ip} link add ${vethName} type veth peer name br-${vethName}
|
|
|
|
${ip} link set ${vethName} netns ${netnsName}
|
|
|
|
${ipNetns} addr add ${v.address}/24 dev ${vethName}
|
|
|
|
${ip} link set br-${vethName} up
|
|
|
|
${ipNetns} link set ${vethName} up
|
|
|
|
${ip} link set br-${vethName} master br0
|
|
|
|
${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
|
|
|
|
'' + (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 10:53:35 +00:00
|
|
|
preStop = ''
|
2020-08-04 13:21:40 +00:00
|
|
|
${ip} netns delete ${netnsName}
|
|
|
|
${ip} link del br-${vethName}
|
2020-05-29 10:53:35 +00:00
|
|
|
'';
|
|
|
|
serviceConfig = {
|
|
|
|
Type = "oneshot";
|
|
|
|
RemainAfterExit = "yes";
|
2020-08-04 13:21:40 +00:00
|
|
|
ExecStartPre = "-${ip} netns delete ${netnsName}";
|
2020-05-29 10:53:35 +00:00
|
|
|
};
|
|
|
|
};
|
2020-06-10 14:56:07 +00:00
|
|
|
};
|
2020-08-04 13:21:40 +00:00
|
|
|
in foldl (services: n:
|
|
|
|
services // (makeNetnsServices n netns.${n})
|
|
|
|
) {} (builtins.attrNames netns));
|
2020-08-21 20:35:59 +00: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-06-10 14:56:07 +00:00
|
|
|
|
2020-08-04 13:21:40 +00:00
|
|
|
services.bitcoind = {
|
|
|
|
bind = netns.bitcoind.address;
|
|
|
|
rpcbind = [
|
|
|
|
"${netns.bitcoind.address}"
|
|
|
|
"127.0.0.1"
|
|
|
|
];
|
|
|
|
rpcallowip = [
|
|
|
|
"127.0.0.1"
|
2020-08-21 20:35:56 +00:00
|
|
|
] ++ map (n: "${netns.${n}.address}") netns.bitcoind.availableNetns;
|
2020-08-21 20:36:00 +00:00
|
|
|
cli = let
|
|
|
|
inherit (config.services.bitcoind) cliBase;
|
|
|
|
in pkgs.writeScriptBin cliBase.name ''
|
|
|
|
exec netns-exec ${netns.bitcoind.netnsName} ${cliBase}/bin/${cliBase.name} "$@"
|
2020-08-04 13:21:40 +00:00
|
|
|
'';
|
|
|
|
};
|
2020-08-21 20:35:59 +00:00
|
|
|
systemd.services.bitcoind-import-banlist.serviceConfig.NetworkNamespacePath = "/var/run/netns/nb-bitcoind";
|
2020-06-10 14:31:38 +00:00
|
|
|
|
2020-08-21 20:35:58 +00:00
|
|
|
services.clightning = {
|
2020-08-04 13:21:40 +00:00
|
|
|
bitcoin-rpcconnect = netns.bitcoind.address;
|
|
|
|
bind-addr = netns.clightning.address;
|
|
|
|
};
|
2020-06-10 14:34:14 +00:00
|
|
|
|
2020-08-21 20:35:58 +00:00
|
|
|
services.lnd = {
|
2020-08-04 13:21:40 +00:00
|
|
|
listen = netns.lnd.address;
|
|
|
|
rpclisten = [
|
|
|
|
"${netns.lnd.address}"
|
|
|
|
"127.0.0.1"
|
|
|
|
];
|
|
|
|
restlisten = [
|
|
|
|
"${netns.lnd.address}"
|
|
|
|
"127.0.0.1"
|
|
|
|
];
|
|
|
|
bitcoind-host = netns.bitcoind.address;
|
2020-08-21 20:36:00 +00:00
|
|
|
cliExec = mkCliExec "lnd";
|
2020-08-04 13:21:40 +00:00
|
|
|
};
|
2020-06-10 14:36:03 +00:00
|
|
|
|
2020-08-21 20:35:58 +00:00
|
|
|
services.liquidd = {
|
2020-08-04 13:21:40 +00:00
|
|
|
bind = netns.liquidd.address;
|
|
|
|
rpcbind = [
|
|
|
|
"${netns.liquidd.address}"
|
|
|
|
"127.0.0.1"
|
|
|
|
];
|
|
|
|
rpcallowip = [
|
|
|
|
"127.0.0.1"
|
2020-08-21 20:35:56 +00:00
|
|
|
] ++ map (n: "${netns.${n}.address}") netns.liquidd.availableNetns;
|
2020-08-04 13:21:40 +00:00
|
|
|
mainchainrpchost = netns.bitcoind.address;
|
2020-08-21 20:36:00 +00:00
|
|
|
cliExec = mkCliExec "liquidd";
|
2020-08-04 13:21:40 +00:00
|
|
|
};
|
2020-05-29 11:13:50 +00:00
|
|
|
|
2020-08-21 20:35:58 +00:00
|
|
|
services.electrs = {
|
2020-08-04 13:21:40 +00:00
|
|
|
address = netns.electrs.address;
|
|
|
|
daemonrpc = "${netns.bitcoind.address}:${toString config.services.bitcoind.rpc.port}";
|
|
|
|
};
|
2020-06-10 14:41:13 +00:00
|
|
|
|
2020-08-21 20:35:58 +00:00
|
|
|
services.spark-wallet = {
|
2020-08-04 13:21:40 +00:00
|
|
|
host = netns.spark-wallet.address;
|
|
|
|
extraArgs = "--no-tls";
|
|
|
|
};
|
2020-06-10 14:43:03 +00:00
|
|
|
|
2020-08-21 20:35:58 +00:00
|
|
|
services.lightning-charge.host = netns.lightning-charge.address;
|
2020-06-10 14:44:50 +00:00
|
|
|
|
2020-08-21 20:35:58 +00:00
|
|
|
services.nanopos = {
|
2020-08-04 13:21:40 +00:00
|
|
|
charged-url = "http://${netns.lightning-charge.address}:9112";
|
|
|
|
host = netns.nanopos.address;
|
|
|
|
};
|
2020-06-10 14:48:20 +00:00
|
|
|
|
2020-08-21 20:35:58 +00:00
|
|
|
services.nix-bitcoin-webindex.host = netns.nginx.address;
|
2020-08-04 13:21:40 +00:00
|
|
|
|
2020-08-21 20:36:00 +00:00
|
|
|
services.lightning-loop.cliExec = mkCliExec "lightning-loop";
|
2020-08-21 20:35:59 +00:00
|
|
|
}
|
|
|
|
]);
|
2020-05-29 10:53:35 +00:00
|
|
|
}
|