1
0

policyrouting: Move ip rule magic to a dedicated module

This commit is contained in:
2022-07-17 12:24:51 +02:00
parent ee46c9855f
commit 65fac93593
3 changed files with 123 additions and 46 deletions

View File

@@ -6,35 +6,6 @@ let
cfg = config.clerie.uplink-selector;
startScript = pkgs.writeScriptBin "uplink-selector-start" ''
#! ${pkgs.runtimeShell} -e
ip46() {
ip "$@"
ip -6 "$@"
}
ip46 rule flush || true
# Route everything except default route first
ip46 rule add lookup main suppress_prefixlength 0 prio 10000
# Decide which uplink to use
${concatStrings (mapAttrsToList (iface: ifacecfg: ''
ip46 rule add iif ${iface} lookup ${cfg.uplinks.${ifacecfg.uplink}.table} prio 20000
'') cfg.interfaces)}
# Fallback to the main default table
ip46 rule add lookup main prio 32000
'';
stopScript = pkgs.writeScriptBin "uplink-selector-stop" ''
#! ${pkgs.runtimeShell} -e
ip rule flush || true
ip -6 rule flush || true
'';
in
{
@@ -90,25 +61,14 @@ in
config = mkIf cfg.enable {
environment.systemPackages = [ pkgs.iproute2 ];
clerie.policyrouting.enable = true;
systemd.services.uplink-selector = {
description = "Uplink Selector";
before = [ "network.target" ];
wantedBy = [ "network.target" ];
after = [ "network-pre.target" ];
clerie.policyrouting.rules = [
{ rule = "lookup main suppress_prefixlength 0"; prio = 10000; }
] ++ (mapAttrsToList (iface: ifacecfg: {
rule = "iif ${iface} lookup ${cfg.uplinks.${ifacecfg.uplink}.table}"; prio = 20000;
}) cfg.interfaces);
path = [ pkgs.iproute2 ];
unitConfig.ConditionCapability = "CAP_NET_ADMIN";
serviceConfig = {
Type = "oneshot";
RemainAfterExit = true;
ExecStart = "@${startScript}/bin/uplink-selector-start uplink-selector-start";
ExecStop = "@${stopScript}/bin/uplink-selector-stop uplink-selector-stop";
};
};
};
}