2020-05-29 10:53:35 +00:00
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
|
|
|
|
with lib;
|
|
|
|
let
|
|
|
|
options.nix-bitcoin.netns-isolation = {
|
|
|
|
enable = mkEnableOption "netns isolation";
|
|
|
|
|
|
|
|
addressblock = mkOption {
|
|
|
|
type = types.ints.u8;
|
2020-10-20 16:20:36 +00:00
|
|
|
default = 1;
|
2022-12-18 12:13:47 +00:00
|
|
|
description = mdDoc ''
|
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;
|
2022-12-18 12:13:47 +00:00
|
|
|
description = mdDoc ''
|
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;
|
2022-12-18 12:13:47 +00:00
|
|
|
description = mdDoc ''
|
2020-08-21 20:36:01 +00:00
|
|
|
User that is allowed to execute commands in the service network namespaces.
|
|
|
|
The user's group is also authorized.
|
|
|
|
'';
|
2020-09-28 11:09:03 +00:00
|
|
|
default = config.nix-bitcoin.operator.name;
|
2020-08-21 20:36:01 +00:00
|
|
|
};
|
2020-08-21 20:36:02 +00:00
|
|
|
|
|
|
|
netns = mkOption {
|
|
|
|
readOnly = true;
|
2021-10-30 12:55:55 +00:00
|
|
|
default = netns;
|
2022-12-18 12:13:47 +00:00
|
|
|
description = mdDoc "Exposes netns parameters.";
|
2020-08-21 20:36:02 +00:00
|
|
|
};
|
2021-10-01 09:52:01 +00:00
|
|
|
|
|
|
|
bridgeIp = mkOption {
|
|
|
|
readOnly = true;
|
2021-10-30 12:55:55 +00:00
|
|
|
default = bridgeIp;
|
2022-12-18 12:13:47 +00:00
|
|
|
description = mdDoc "IP of the netns bridge interface.";
|
2021-10-01 09:52:01 +00:00
|
|
|
};
|
2020-05-29 10:53:35 +00:00
|
|
|
};
|
|
|
|
|
2021-09-13 11:40:47 +00:00
|
|
|
cfg = config.nix-bitcoin.netns-isolation;
|
|
|
|
|
|
|
|
netns = builtins.mapAttrs (n: v: {
|
|
|
|
inherit (v) id;
|
|
|
|
address = "169.254.${toString cfg.addressblock}.${toString v.id}";
|
|
|
|
availableNetns = availableNetns.${n};
|
|
|
|
netnsName = "nb-${n}";
|
|
|
|
}) enabledServices;
|
|
|
|
|
|
|
|
# Symmetric netns connection matrix
|
|
|
|
# if clightning.connections = [ "bitcoind" ]; then
|
|
|
|
# availableNetns.bitcoind = [ "clighting" ];
|
|
|
|
# and
|
|
|
|
# availableNetns.clighting = [ "bitcoind" ];
|
|
|
|
#
|
2021-11-28 20:36:04 +00:00
|
|
|
# TODO-EXTERNAL:
|
|
|
|
# Although negligible for our purposes, this calculation's runtime
|
2021-09-13 11:40:47 +00:00
|
|
|
# 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.
|
|
|
|
#
|
|
|
|
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";
|
|
|
|
|
|
|
|
mkCliExec = service: "exec netns-exec ${netns.${service}.netnsName}";
|
|
|
|
in {
|
|
|
|
inherit options;
|
|
|
|
|
2020-08-21 20:35:59 +00:00
|
|
|
config = mkIf cfg.enable (mkMerge [
|
|
|
|
|
|
|
|
# Base infrastructure
|
|
|
|
{
|
2020-08-21 20:36:04 +00:00
|
|
|
networking.dhcpcd.denyInterfaces = [ "nb-br" "nb-veth*" ];
|
2021-08-04 22:49:00 +00:00
|
|
|
services.tor.client.socksListenAddress = {
|
|
|
|
addr = bridgeIp;
|
|
|
|
# Default NixOS values. These must be repeated when redefining this option.
|
|
|
|
port = 9050;
|
|
|
|
IsolateDestAddr = true;
|
|
|
|
};
|
2021-10-10 13:14:38 +00:00
|
|
|
services.i2pd.proto.sam.address = bridgeIp;
|
|
|
|
networking.firewall.interfaces.nb-br.allowedTCPPorts = [
|
|
|
|
config.services.tor.client.socksListenAddress.port
|
|
|
|
config.services.i2pd.proto.sam.port
|
|
|
|
];
|
2020-08-04 13:21:40 +00:00
|
|
|
boot.kernel.sysctl."net.ipv4.ip_forward" = true;
|
2020-08-21 20:36:05 +00:00
|
|
|
|
2020-08-04 13:21:40 +00:00
|
|
|
security.wrappers.netns-exec = {
|
2020-11-09 21:09:09 +00:00
|
|
|
source = config.nix-bitcoin.pkgs.netns-exec;
|
2020-08-04 13:21:40 +00:00
|
|
|
capabilities = "cap_sys_admin=ep";
|
2020-08-21 20:36:01 +00:00
|
|
|
owner = cfg.allowedUser;
|
2021-12-09 20:04:13 +00:00
|
|
|
group = ""; # Set to the group of `owner`
|
2021-02-01 21:53:09 +00:00
|
|
|
permissions = "550";
|
2020-08-04 13:21:40 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
systemd.services = {
|
2020-08-21 20:36:05 +00: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";
|
2021-02-01 21:53:05 +00:00
|
|
|
RemainAfterExit = true;
|
2020-08-21 20:36:05 +00:00
|
|
|
};
|
2020-08-04 13:21:40 +00:00
|
|
|
script = ''
|
2020-08-21 20:36:03 +00: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 13:21:40 +00: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 20:36:03 +00:00
|
|
|
${ip} link del nb-br
|
2020-08-04 13:21:40 +00:00
|
|
|
'';
|
2020-05-29 10:53:35 +00:00
|
|
|
};
|
2020-04-23 16:18:47 +00:00
|
|
|
|
2020-08-04 13:21:40 +00:00
|
|
|
} //
|
|
|
|
(let
|
|
|
|
makeNetnsServices = n: v: let
|
2020-08-21 20:36:04 +00:00
|
|
|
veth = "nb-veth-${toString v.id}";
|
|
|
|
peer = "nb-veth-br-${toString v.id}";
|
2020-08-21 20:36:00 +00:00
|
|
|
inherit (v) netnsName;
|
2021-11-08 11:45:29 +00:00
|
|
|
nsenter = "${pkgs.utillinux}/bin/nsenter";
|
2021-11-28 20:36:03 +00:00
|
|
|
allowedNetnsAddresses = map (available: netns.${available}.address) v.availableNetns;
|
|
|
|
allowedAddresses = concatStringsSep ","
|
|
|
|
([ "127.0.0.1,${bridgeIp},${v.address}" ] ++ allowedNetnsAddresses);
|
2021-11-08 11:45:29 +00:00
|
|
|
|
|
|
|
setup = ''
|
|
|
|
${ip} netns add ${netnsName}
|
|
|
|
${ip} link add ${veth} type veth peer name ${peer}
|
|
|
|
${ip} link set ${veth} netns ${netnsName}
|
|
|
|
# The peer link is never used directly, so don't auto-assign an IPv6 address
|
|
|
|
echo 1 > /proc/sys/net/ipv6/conf/${peer}/disable_ipv6
|
|
|
|
${ip} link set ${peer} up
|
|
|
|
${ip} link set ${peer} master nb-br
|
|
|
|
exec ${nsenter} --net=/run/netns/${netnsName} ${script "in-netns" setupInNetns}
|
|
|
|
'';
|
|
|
|
|
|
|
|
setupInNetns = ''
|
|
|
|
${ip} link set lo up
|
|
|
|
${ip} addr add ${v.address}/24 dev ${veth}
|
|
|
|
${ip} link set ${veth} up
|
|
|
|
${ip} route add default via ${bridgeIp}
|
|
|
|
|
|
|
|
${iptables} -w -P INPUT DROP
|
|
|
|
# allow return traffic to outgoing connections initiated by the service itself
|
|
|
|
${iptables} -w -A INPUT -m conntrack --ctstate ESTABLISHED -j ACCEPT
|
2021-11-28 20:36:03 +00:00
|
|
|
${iptables} -w -A INPUT -s ${allowedAddresses} -j ACCEPT
|
2021-11-28 20:24:49 +00:00
|
|
|
'' + optionalString (config.services.${n}.tor.enforce or false) ''
|
2021-11-08 11:45:29 +00:00
|
|
|
${iptables} -w -P OUTPUT DROP
|
|
|
|
${iptables} -w -A OUTPUT -d ${allowedAddresses} -j ACCEPT
|
|
|
|
'';
|
|
|
|
script = name: src: pkgs.writers.writeDash name ''
|
|
|
|
set -e
|
|
|
|
${src}
|
|
|
|
'';
|
2020-08-04 13:21:40 +00:00
|
|
|
in {
|
|
|
|
"${n}".serviceConfig.NetworkNamespacePath = "/var/run/netns/${netnsName}";
|
|
|
|
|
|
|
|
"netns-${n}" = rec {
|
2020-08-21 20:36:05 +00:00
|
|
|
requires = [ "nb-netns-bridge.service" ];
|
|
|
|
after = [ "nb-netns-bridge.service" ];
|
2021-11-08 11:45:28 +00:00
|
|
|
requiredBy = [ "${n}.service" ];
|
|
|
|
before = requiredBy;
|
2021-11-08 11:45:29 +00:00
|
|
|
serviceConfig = {
|
|
|
|
Type = "oneshot";
|
|
|
|
RemainAfterExit = true;
|
|
|
|
ExecStart = script "setup" setup;
|
|
|
|
};
|
2020-11-03 20:54:14 +00:00
|
|
|
# Link deletion is implicit in netns deletion, but it sometimes only happens
|
|
|
|
# after `netns delete` finishes. Add an extra `link del` to ensure that
|
|
|
|
# the link is deleted before the service stops, which is needed for service
|
|
|
|
# restart to succeed.
|
2020-05-29 10:53:35 +00:00
|
|
|
preStop = ''
|
2020-08-04 13:21:40 +00:00
|
|
|
${ip} netns delete ${netnsName}
|
2020-11-03 20:54:14 +00:00
|
|
|
${ip} link del ${peer} 2> /dev/null || true
|
2020-05-29 10:53:35 +00:00
|
|
|
'';
|
2021-11-08 11:45:29 +00:00
|
|
|
|
2020-05-29 10:53:35 +00:00
|
|
|
};
|
2020-06-10 14:56:07 +00:00
|
|
|
};
|
2023-01-20 12:45:14 +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" ];
|
|
|
|
};
|
|
|
|
nginx = {
|
|
|
|
id = 21;
|
|
|
|
};
|
|
|
|
lightning-loop = {
|
|
|
|
id = 22;
|
|
|
|
connections = [ "lnd" ];
|
|
|
|
};
|
2020-08-12 14:47:56 +00:00
|
|
|
nbxplorer = {
|
|
|
|
id = 23;
|
2021-07-13 14:03:08 +00:00
|
|
|
connections = [ "bitcoind" ]
|
|
|
|
++ optional config.services.btcpayserver.lbtc "liquidd";
|
2020-08-12 14:47:56 +00:00
|
|
|
};
|
|
|
|
btcpayserver = {
|
|
|
|
id = 24;
|
|
|
|
connections = [ "nbxplorer" ]
|
2021-07-13 14:03:08 +00:00
|
|
|
++ optional (config.services.btcpayserver.lightningBackend == "lnd") "lnd"
|
|
|
|
++ optional config.services.btcpayserver.lbtc "liquidd";
|
2020-08-12 14:47:56 +00:00
|
|
|
# communicates with clightning over rpc socket
|
|
|
|
};
|
2020-04-23 16:18:47 +00:00
|
|
|
joinmarket = {
|
|
|
|
id = 25;
|
|
|
|
connections = [ "bitcoind" ];
|
|
|
|
};
|
2021-01-17 12:24:57 +00:00
|
|
|
joinmarket-ob-watcher = {
|
|
|
|
id = 26;
|
2021-08-25 09:57:09 +00:00
|
|
|
connections = [ "bitcoind" ];
|
2021-01-17 12:24:57 +00:00
|
|
|
};
|
2021-03-01 09:59:23 +00:00
|
|
|
lightning-pool = {
|
|
|
|
id = 27;
|
|
|
|
connections = [ "lnd" ];
|
|
|
|
};
|
2021-06-02 01:11:26 +00:00
|
|
|
charge-lnd = {
|
|
|
|
id = 28;
|
|
|
|
connections = [ "lnd" "electrs" ];
|
|
|
|
};
|
2021-11-08 11:43:14 +00:00
|
|
|
rtl = {
|
|
|
|
id = 29;
|
2022-05-14 13:21:36 +00:00
|
|
|
connections = let
|
|
|
|
nodes = config.services.rtl.nodes;
|
|
|
|
in
|
|
|
|
optional nodes.lnd.enable "lnd" ++
|
|
|
|
optional (nodes.lnd.enable && nodes.lnd.loop) "lightning-loop" ++
|
|
|
|
optional nodes.clightning.enable "clightning-rest";
|
2022-05-05 19:56:16 +00:00
|
|
|
};
|
|
|
|
clightning-rest = {
|
|
|
|
id = 30;
|
2021-11-08 11:43:14 +00:00
|
|
|
};
|
2022-07-04 10:15:44 +00:00
|
|
|
fulcrum = {
|
|
|
|
id = 31;
|
|
|
|
connections = [ "bitcoind" ];
|
|
|
|
};
|
2023-08-06 19:07:51 +00:00
|
|
|
mempool = {
|
|
|
|
id = 32;
|
|
|
|
connections = [
|
|
|
|
"bitcoind"
|
|
|
|
"nginx"
|
|
|
|
(if (config.services.mempool.electrumServer == "electrs") then "electrs" else "fulcrum")
|
|
|
|
];
|
|
|
|
};
|
2020-08-21 20:35:59 +00:00
|
|
|
};
|
2020-06-10 14:56:07 +00:00
|
|
|
|
2020-08-04 13:21:40 +00:00
|
|
|
services.bitcoind = {
|
2021-01-14 12:24:01 +00:00
|
|
|
address = netns.bitcoind.address;
|
|
|
|
rpc.address = netns.bitcoind.address;
|
2021-01-14 12:24:02 +00:00
|
|
|
rpc.allowip = [
|
2020-10-29 20:20:29 +00:00
|
|
|
bridgeIp # For operator user
|
|
|
|
netns.bitcoind.address
|
|
|
|
] ++ map (n: netns.${n}.address) netns.bitcoind.availableNetns;
|
2020-08-04 13:21:40 +00:00
|
|
|
};
|
2020-06-10 14:31:38 +00:00
|
|
|
|
2021-01-14 12:24:04 +00:00
|
|
|
services.clightning.address = netns.clightning.address;
|
2020-06-10 14:34:14 +00:00
|
|
|
|
2020-08-21 20:35:58 +00:00
|
|
|
services.lnd = {
|
2021-01-14 12:24:03 +00:00
|
|
|
address = netns.lnd.address;
|
|
|
|
rpcAddress = netns.lnd.address;
|
|
|
|
restAddress = netns.lnd.address;
|
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 = {
|
2021-01-14 12:24:07 +00:00
|
|
|
address = netns.liquidd.address;
|
|
|
|
rpc.address = netns.liquidd.address;
|
2020-08-04 13:21:40 +00:00
|
|
|
rpcallowip = [
|
2020-10-29 20:20:31 +00:00
|
|
|
bridgeIp # For operator user
|
|
|
|
netns.liquidd.address
|
|
|
|
] ++ map (n: netns.${n}.address) netns.liquidd.availableNetns;
|
2020-08-04 13:21:40 +00:00
|
|
|
};
|
2020-05-29 11:13:50 +00:00
|
|
|
|
2020-09-27 10:43:15 +00:00
|
|
|
services.electrs.address = netns.electrs.address;
|
2020-06-10 14:41:13 +00:00
|
|
|
|
2022-07-04 10:15:44 +00:00
|
|
|
services.fulcrum.address = netns.fulcrum.address;
|
|
|
|
|
2020-10-29 20:20:37 +00:00
|
|
|
services.lightning-loop.rpcAddress = netns.lightning-loop.address;
|
2020-08-12 14:47:56 +00:00
|
|
|
|
2021-01-14 12:24:05 +00:00
|
|
|
services.nbxplorer.address = netns.nbxplorer.address;
|
|
|
|
services.btcpayserver.address = netns.btcpayserver.address;
|
2020-04-23 16:18:47 +00:00
|
|
|
|
2021-10-24 19:14:09 +00:00
|
|
|
services.joinmarket = {
|
|
|
|
payjoinAddress = netns.joinmarket.address;
|
2022-05-27 09:06:14 +00:00
|
|
|
messagingAddress = netns.joinmarket.address;
|
2021-10-24 19:14:09 +00:00
|
|
|
cliExec = mkCliExec "joinmarket";
|
|
|
|
};
|
2021-11-26 14:13:33 +00:00
|
|
|
systemd.services.joinmarket-yieldgenerator = mkIf config.services.joinmarket.yieldgenerator.enable {
|
|
|
|
serviceConfig.NetworkNamespacePath = "/var/run/netns/nb-joinmarket";
|
|
|
|
};
|
2021-01-17 12:24:57 +00:00
|
|
|
|
|
|
|
services.joinmarket-ob-watcher.address = netns.joinmarket-ob-watcher.address;
|
2021-03-01 09:59:23 +00:00
|
|
|
|
|
|
|
services.lightning-pool.rpcAddress = netns.lightning-pool.address;
|
2021-11-08 11:43:14 +00:00
|
|
|
|
|
|
|
services.rtl.address = netns.rtl.address;
|
2022-05-05 19:56:16 +00:00
|
|
|
|
|
|
|
services.clightning-rest.address = netns.clightning-rest.address;
|
2023-08-06 19:07:51 +00:00
|
|
|
|
|
|
|
services.mempool.address = netns.mempool.address;
|
|
|
|
services.mempool.frontend.address = netns.nginx.address;
|
2020-08-21 20:35:59 +00:00
|
|
|
}
|
|
|
|
]);
|
2020-05-29 10:53:35 +00:00
|
|
|
}
|