2014-11-20 02:37:48 -08:00
|
|
|
|
{
|
|
|
|
|
config,
|
|
|
|
|
lib,
|
|
|
|
|
pkgs,
|
|
|
|
|
utils,
|
|
|
|
|
...
|
|
|
|
|
}:
|
|
|
|
|
|
|
|
|
|
with utils;
|
2014-12-10 19:28:45 -05:00
|
|
|
|
with lib;
|
2014-11-20 02:37:48 -08:00
|
|
|
|
|
|
|
|
|
let
|
|
|
|
|
|
|
|
|
|
cfg = config.networking;
|
|
|
|
|
interfaces = attrValues cfg.interfaces;
|
|
|
|
|
|
2017-09-24 16:54:48 +02:00
|
|
|
|
slaves =
|
|
|
|
|
concatMap (i: i.interfaces) (attrValues cfg.bonds)
|
|
|
|
|
++ concatMap (i: i.interfaces) (attrValues cfg.bridges)
|
2018-12-29 11:16:51 +01:00
|
|
|
|
++ concatMap (i: attrNames (filterAttrs (_: config: config.type != "internal") i.interfaces)) (
|
|
|
|
|
attrValues cfg.vswitches
|
|
|
|
|
)
|
2017-09-24 16:54:48 +02:00
|
|
|
|
++ concatMap (i: [ i.interface ]) (attrValues cfg.macvlans)
|
|
|
|
|
++ concatMap (i: [ i.interface ]) (attrValues cfg.vlans);
|
|
|
|
|
|
2014-11-20 02:37:48 -08:00
|
|
|
|
# We must escape interfaces due to the systemd interpretation
|
|
|
|
|
subsystemDevice = interface: "sys-subsystem-net-devices-${escapeSystemdPath interface}.device";
|
|
|
|
|
|
2017-12-03 05:13:14 +01:00
|
|
|
|
interfaceIps = i: i.ipv4.addresses ++ optionals cfg.enableIPv6 i.ipv6.addresses;
|
2014-11-20 02:37:48 -08:00
|
|
|
|
|
2014-11-25 01:53:37 -08:00
|
|
|
|
destroyBond = i: ''
|
|
|
|
|
while true; do
|
|
|
|
|
UPDATED=1
|
|
|
|
|
SLAVES=$(ip link | grep 'master ${i}' | awk -F: '{print $2}')
|
|
|
|
|
for I in $SLAVES; do
|
|
|
|
|
UPDATED=0
|
2023-10-29 18:27:16 +01:00
|
|
|
|
ip link set dev "$I" nomaster
|
2014-11-25 01:53:37 -08:00
|
|
|
|
done
|
|
|
|
|
[ "$UPDATED" -eq "1" ] && break
|
|
|
|
|
done
|
2023-10-29 18:27:16 +01:00
|
|
|
|
ip link set dev "${i}" down 2>/dev/null || true
|
|
|
|
|
ip link del dev "${i}" 2>/dev/null || true
|
2014-11-25 01:53:37 -08:00
|
|
|
|
'';
|
|
|
|
|
|
2017-02-02 22:21:03 +01:00
|
|
|
|
# warn that these attributes are deprecated (2017-2-2)
|
|
|
|
|
# Should be removed in the release after next
|
|
|
|
|
bondDeprecation = rec {
|
|
|
|
|
deprecated = [
|
|
|
|
|
"lacp_rate"
|
|
|
|
|
"miimon"
|
|
|
|
|
"mode"
|
|
|
|
|
"xmit_hash_policy"
|
|
|
|
|
];
|
|
|
|
|
filterDeprecated =
|
|
|
|
|
bond: (filterAttrs (attrName: attr: elem attrName deprecated && attr != null) bond);
|
|
|
|
|
};
|
2014-11-20 02:37:48 -08:00
|
|
|
|
|
2017-02-02 22:21:03 +01:00
|
|
|
|
bondWarnings =
|
|
|
|
|
let
|
|
|
|
|
oneBondWarnings =
|
|
|
|
|
bondName: bond: mapAttrsToList (bondText bondName) (bondDeprecation.filterDeprecated bond);
|
|
|
|
|
bondText =
|
|
|
|
|
bondName: optName: _:
|
|
|
|
|
"${bondName}.${optName} is deprecated, use ${bondName}.driverOptions";
|
|
|
|
|
in
|
|
|
|
|
{
|
|
|
|
|
warnings = flatten (mapAttrsToList oneBondWarnings cfg.bonds);
|
|
|
|
|
};
|
2014-11-20 02:37:48 -08:00
|
|
|
|
|
2017-02-02 22:21:03 +01:00
|
|
|
|
normalConfig = {
|
2020-05-17 21:57:01 +02:00
|
|
|
|
systemd.network.links =
|
|
|
|
|
let
|
|
|
|
|
createNetworkLink =
|
|
|
|
|
i:
|
|
|
|
|
nameValuePair "40-${i.name}" {
|
|
|
|
|
matchConfig.OriginalName = i.name;
|
|
|
|
|
linkConfig =
|
|
|
|
|
optionalAttrs (i.macAddress != null) {
|
|
|
|
|
MACAddress = i.macAddress;
|
|
|
|
|
}
|
|
|
|
|
// optionalAttrs (i.mtu != null) {
|
|
|
|
|
MTUBytes = toString i.mtu;
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
in
|
|
|
|
|
listToAttrs (map createNetworkLink interfaces);
|
2014-11-20 02:37:48 -08:00
|
|
|
|
systemd.services =
|
|
|
|
|
let
|
|
|
|
|
|
2016-09-30 10:45:48 +02:00
|
|
|
|
deviceDependency =
|
|
|
|
|
dev:
|
2017-02-09 23:08:48 +01:00
|
|
|
|
# Use systemd service if we manage device creation, else
|
|
|
|
|
# trust udev when not in a container
|
2024-01-02 08:02:50 +05:30
|
|
|
|
if (dev == null || dev == "lo") then
|
|
|
|
|
[ ]
|
|
|
|
|
else if
|
|
|
|
|
(hasAttr dev (filterAttrs (k: v: v.virtual) cfg.interfaces))
|
2017-02-18 04:54:15 +01:00
|
|
|
|
|| (hasAttr dev cfg.bridges)
|
2017-02-09 23:08:48 +01:00
|
|
|
|
|| (hasAttr dev cfg.bonds)
|
|
|
|
|
|| (hasAttr dev cfg.macvlans)
|
|
|
|
|
|| (hasAttr dev cfg.sits)
|
|
|
|
|
|| (hasAttr dev cfg.vlans)
|
2025-01-15 10:32:39 +08:00
|
|
|
|
|| (hasAttr dev cfg.greTunnels)
|
2018-04-03 20:00:12 +00:00
|
|
|
|
|| (hasAttr dev cfg.vswitches)
|
2017-02-09 23:08:48 +01:00
|
|
|
|
then
|
|
|
|
|
[ "${dev}-netdev.service" ]
|
2024-01-02 08:02:50 +05:30
|
|
|
|
else
|
|
|
|
|
optional (!config.boot.isContainer) (subsystemDevice dev);
|
2025-04-01 20:10:43 +02:00
|
|
|
|
|
2017-09-18 14:46:37 +02:00
|
|
|
|
hasDefaultGatewaySet =
|
|
|
|
|
(cfg.defaultGateway != null && cfg.defaultGateway.address != "")
|
2018-02-19 23:30:49 +03:00
|
|
|
|
|| (cfg.enableIPv6 && cfg.defaultGateway6 != null && cfg.defaultGateway6.address != "");
|
2025-04-01 20:10:43 +02:00
|
|
|
|
|
2022-06-24 20:43:40 +03:00
|
|
|
|
needNetworkSetup =
|
|
|
|
|
cfg.resolvconf.enable || cfg.defaultGateway != null || cfg.defaultGateway6 != null;
|
|
|
|
|
|
|
|
|
|
networkLocalCommands = lib.mkIf needNetworkSetup {
|
2014-11-20 02:37:48 -08:00
|
|
|
|
after = [ "network-setup.service" ];
|
|
|
|
|
bindsTo = [ "network-setup.service" ];
|
|
|
|
|
};
|
|
|
|
|
|
2022-06-24 20:43:40 +03:00
|
|
|
|
networkSetup = lib.mkIf needNetworkSetup {
|
2014-11-20 02:37:48 -08:00
|
|
|
|
description = "Networking Setup";
|
2025-04-01 20:10:43 +02:00
|
|
|
|
|
2025-01-15 10:32:39 +08:00
|
|
|
|
after = [ "network-pre.target" ];
|
2016-09-09 05:25:56 +02:00
|
|
|
|
before = [
|
|
|
|
|
"network.target"
|
|
|
|
|
"shutdown.target"
|
|
|
|
|
];
|
|
|
|
|
wants = [ "network.target" ];
|
2018-09-23 22:55:59 +02:00
|
|
|
|
# exclude bridges from the partOf relationship to fix container networking bug #47210
|
|
|
|
|
partOf = map (i: "network-addresses-${i.name}.service") (
|
|
|
|
|
filter (i: !(hasAttr i.name cfg.bridges)) interfaces
|
|
|
|
|
);
|
2016-09-09 05:25:56 +02:00
|
|
|
|
conflicts = [ "shutdown.target" ];
|
2017-09-18 14:46:37 +02:00
|
|
|
|
wantedBy = [ "multi-user.target" ] ++ optional hasDefaultGatewaySet "network-online.target";
|
2025-04-01 20:10:43 +02:00
|
|
|
|
|
2014-11-20 02:37:48 -08:00
|
|
|
|
unitConfig.ConditionCapability = "CAP_NET_ADMIN";
|
2025-04-01 20:10:43 +02:00
|
|
|
|
|
2021-03-14 17:05:16 +01:00
|
|
|
|
path = [ pkgs.iproute2 ];
|
2025-04-01 20:10:43 +02:00
|
|
|
|
|
2016-09-09 05:25:56 +02:00
|
|
|
|
serviceConfig = {
|
|
|
|
|
Type = "oneshot";
|
|
|
|
|
RemainAfterExit = true;
|
|
|
|
|
};
|
2014-11-20 02:37:48 -08:00
|
|
|
|
|
2016-10-02 14:00:12 +02:00
|
|
|
|
unitConfig.DefaultDependencies = false;
|
2014-12-01 01:18:26 -08:00
|
|
|
|
|
2014-11-20 02:37:48 -08:00
|
|
|
|
script = ''
|
2019-07-15 20:18:49 +03:00
|
|
|
|
${optionalString config.networking.resolvconf.enable ''
|
2019-03-09 11:42:14 +00:00
|
|
|
|
# Set the static DNS configuration, if given.
|
|
|
|
|
${pkgs.openresolv}/sbin/resolvconf -m 1 -a static <<EOF
|
|
|
|
|
${optionalString (cfg.nameservers != [ ] && cfg.domain != null) ''
|
|
|
|
|
domain ${cfg.domain}
|
2025-04-01 20:10:43 +02:00
|
|
|
|
''}
|
2019-03-09 11:42:14 +00:00
|
|
|
|
${optionalString (cfg.search != [ ]) ("search " + concatStringsSep " " cfg.search)}
|
|
|
|
|
${flip concatMapStrings cfg.nameservers (ns: ''
|
|
|
|
|
nameserver ${ns}
|
2025-04-01 20:10:43 +02:00
|
|
|
|
'')}
|
|
|
|
|
EOF
|
|
|
|
|
''}
|
|
|
|
|
|
2014-11-20 02:37:48 -08:00
|
|
|
|
# Set the default gateway.
|
2017-01-14 04:14:16 +03:00
|
|
|
|
${optionalString (cfg.defaultGateway != null && cfg.defaultGateway.address != "") ''
|
2017-09-01 06:24:37 +02:00
|
|
|
|
${optionalString (cfg.defaultGateway.interface != null) ''
|
|
|
|
|
ip route replace ${cfg.defaultGateway.address} dev ${cfg.defaultGateway.interface} ${
|
|
|
|
|
optionalString (cfg.defaultGateway.metric != null) "metric ${toString cfg.defaultGateway.metric}"
|
|
|
|
|
} proto static
|
|
|
|
|
''}
|
|
|
|
|
ip route replace default ${
|
|
|
|
|
optionalString (cfg.defaultGateway.metric != null) "metric ${toString cfg.defaultGateway.metric}"
|
2017-02-09 23:09:07 +01:00
|
|
|
|
} via "${cfg.defaultGateway.address}" ${
|
2014-11-20 02:37:48 -08:00
|
|
|
|
optionalString (
|
|
|
|
|
cfg.defaultGatewayWindowSize != null
|
2017-01-14 04:14:16 +03:00
|
|
|
|
) "window ${toString cfg.defaultGatewayWindowSize}"
|
|
|
|
|
} ${
|
|
|
|
|
optionalString (cfg.defaultGateway.interface != null) "dev ${cfg.defaultGateway.interface}"
|
2017-09-01 06:24:37 +02:00
|
|
|
|
} proto static
|
2014-11-20 02:37:48 -08:00
|
|
|
|
''}
|
2017-01-14 04:14:16 +03:00
|
|
|
|
${optionalString (cfg.defaultGateway6 != null && cfg.defaultGateway6.address != "") ''
|
2017-09-01 06:24:37 +02:00
|
|
|
|
${optionalString (cfg.defaultGateway6.interface != null) ''
|
|
|
|
|
ip -6 route replace ${cfg.defaultGateway6.address} dev ${cfg.defaultGateway6.interface} ${
|
|
|
|
|
optionalString (cfg.defaultGateway6.metric != null) "metric ${toString cfg.defaultGateway6.metric}"
|
|
|
|
|
} proto static
|
2025-04-01 20:10:43 +02:00
|
|
|
|
''}
|
2017-09-01 06:24:37 +02:00
|
|
|
|
ip -6 route replace default ${
|
2017-02-09 23:09:07 +01:00
|
|
|
|
optionalString (cfg.defaultGateway6.metric != null) "metric ${toString cfg.defaultGateway6.metric}"
|
|
|
|
|
} via "${cfg.defaultGateway6.address}" ${
|
2014-12-17 17:54:06 -08:00
|
|
|
|
optionalString (
|
|
|
|
|
cfg.defaultGatewayWindowSize != null
|
2017-01-14 04:14:16 +03:00
|
|
|
|
) "window ${toString cfg.defaultGatewayWindowSize}"
|
|
|
|
|
} ${
|
|
|
|
|
optionalString (cfg.defaultGateway6.interface != null) "dev ${cfg.defaultGateway6.interface}"
|
2017-09-01 06:24:37 +02:00
|
|
|
|
} proto static
|
2014-12-17 17:54:06 -08:00
|
|
|
|
''}
|
2014-11-20 02:37:48 -08:00
|
|
|
|
'';
|
|
|
|
|
};
|
|
|
|
|
|
2014-11-24 12:06:07 -08:00
|
|
|
|
# For each interface <foo>, create a job ‘network-addresses-<foo>.service"
|
|
|
|
|
# that performs static address configuration. It has a "wants"
|
2014-11-20 02:37:48 -08:00
|
|
|
|
# dependency on ‘<foo>.service’, which is supposed to create
|
|
|
|
|
# the interface and need not exist (i.e. for hardware
|
|
|
|
|
# interfaces). It has a binds-to dependency on the actual
|
|
|
|
|
# network device, so it only gets started after the interface
|
|
|
|
|
# has appeared, and it's stopped when the interface
|
|
|
|
|
# disappears.
|
2014-11-24 12:06:07 -08:00
|
|
|
|
configureAddrs =
|
|
|
|
|
i:
|
2014-11-20 02:37:48 -08:00
|
|
|
|
let
|
|
|
|
|
ips = interfaceIps i;
|
|
|
|
|
in
|
2014-11-24 12:06:07 -08:00
|
|
|
|
nameValuePair "network-addresses-${i.name}" {
|
2015-04-20 16:12:19 +02:00
|
|
|
|
description = "Address configuration of ${i.name}";
|
2017-09-24 15:55:02 +02:00
|
|
|
|
wantedBy = [
|
|
|
|
|
"network-setup.service"
|
|
|
|
|
"network.target"
|
|
|
|
|
];
|
2016-09-09 05:25:56 +02:00
|
|
|
|
# order before network-setup because the routes that are configured
|
|
|
|
|
# there may need ip addresses configured
|
|
|
|
|
before = [ "network-setup.service" ];
|
2016-09-30 10:45:48 +02:00
|
|
|
|
bindsTo = deviceDependency i.name;
|
|
|
|
|
after = [ "network-pre.target" ] ++ (deviceDependency i.name);
|
2014-11-20 02:37:48 -08:00
|
|
|
|
serviceConfig.Type = "oneshot";
|
|
|
|
|
serviceConfig.RemainAfterExit = true;
|
2017-04-04 14:18:49 +02:00
|
|
|
|
# Restart rather than stop+start this unit to prevent the
|
|
|
|
|
# network from dying during switch-to-configuration.
|
|
|
|
|
stopIfChanged = false;
|
2021-03-14 17:05:16 +01:00
|
|
|
|
path = [ pkgs.iproute2 ];
|
2014-11-20 02:37:48 -08:00
|
|
|
|
script = ''
|
2017-04-04 14:18:49 +02:00
|
|
|
|
state="/run/nixos/network/addresses/${i.name}"
|
2024-10-18 18:23:05 +02:00
|
|
|
|
mkdir -p "$(dirname "$state")"
|
2025-04-01 20:10:43 +02:00
|
|
|
|
|
2023-10-29 18:27:16 +01:00
|
|
|
|
ip link set dev "${i.name}" up
|
2025-04-01 20:10:43 +02:00
|
|
|
|
|
2017-07-27 16:23:20 +02:00
|
|
|
|
${flip concatMapStrings ips (
|
2025-04-01 20:10:43 +02:00
|
|
|
|
ip:
|
|
|
|
|
let
|
2017-07-27 16:23:20 +02:00
|
|
|
|
cidr = "${ip.address}/${toString ip.prefixLength}";
|
2025-04-01 20:10:43 +02:00
|
|
|
|
in
|
|
|
|
|
''
|
2017-07-27 16:23:20 +02:00
|
|
|
|
echo "${cidr}" >> $state
|
|
|
|
|
echo -n "adding address ${cidr}... "
|
2024-05-17 01:02:29 +02:00
|
|
|
|
if out=$(ip addr replace "${cidr}" dev "${i.name}" 2>&1); then
|
2020-05-29 12:20:06 +02:00
|
|
|
|
echo "done"
|
2025-04-01 20:10:43 +02:00
|
|
|
|
else
|
2024-10-18 18:23:05 +02:00
|
|
|
|
echo "'ip addr replace \"${cidr}\" dev \"${i.name}\"' failed: $out"
|
2025-04-01 20:10:43 +02:00
|
|
|
|
exit 1
|
|
|
|
|
fi
|
|
|
|
|
''
|
|
|
|
|
)}
|
2020-05-17 21:57:01 +02:00
|
|
|
|
|
2017-07-27 16:23:20 +02:00
|
|
|
|
state="/run/nixos/network/routes/${i.name}"
|
2024-10-18 18:23:05 +02:00
|
|
|
|
mkdir -p "$(dirname "$state")"
|
2025-04-01 20:10:43 +02:00
|
|
|
|
|
2017-07-27 16:23:20 +02:00
|
|
|
|
${flip concatMapStrings (i.ipv4.routes ++ i.ipv6.routes) (
|
|
|
|
|
route:
|
|
|
|
|
let
|
|
|
|
|
cidr = "${route.address}/${toString route.prefixLength}";
|
2017-07-28 16:45:58 +02:00
|
|
|
|
via = optionalString (route.via != null) ''via "${route.via}"'';
|
|
|
|
|
options = concatStrings (mapAttrsToList (name: val: "${name} ${val} ") route.options);
|
2022-06-02 19:16:25 +01:00
|
|
|
|
type = toString route.type;
|
2017-07-27 16:23:20 +02:00
|
|
|
|
in
|
|
|
|
|
''
|
|
|
|
|
echo "${cidr}" >> $state
|
|
|
|
|
echo -n "adding route ${cidr}... "
|
2022-06-02 19:16:25 +01:00
|
|
|
|
if out=$(ip route add ${type} "${cidr}" ${options} ${via} dev "${i.name}" proto static 2>&1); then
|
2017-07-27 16:23:20 +02:00
|
|
|
|
echo "done"
|
|
|
|
|
elif ! echo "$out" | grep "File exists" >/dev/null 2>&1; then
|
2024-10-18 18:23:05 +02:00
|
|
|
|
echo "'ip route add ${type} \"${cidr}\" ${options} ${via} dev \"${i.name}\"' failed: $out"
|
2017-07-27 16:23:20 +02:00
|
|
|
|
exit 1
|
|
|
|
|
fi
|
|
|
|
|
''
|
|
|
|
|
)}
|
|
|
|
|
'';
|
2017-04-04 14:18:49 +02:00
|
|
|
|
preStop = ''
|
2017-07-27 16:23:20 +02:00
|
|
|
|
state="/run/nixos/network/routes/${i.name}"
|
2020-05-29 12:20:06 +02:00
|
|
|
|
if [ -e "$state" ]; then
|
2024-12-09 01:28:54 +00:00
|
|
|
|
while read -r cidr; do
|
2020-05-29 12:20:06 +02:00
|
|
|
|
echo -n "deleting route $cidr... "
|
|
|
|
|
ip route del "$cidr" dev "${i.name}" >/dev/null 2>&1 && echo "done" || echo "failed"
|
|
|
|
|
done < "$state"
|
|
|
|
|
rm -f "$state"
|
|
|
|
|
fi
|
2017-07-27 16:23:20 +02:00
|
|
|
|
|
2017-04-04 14:18:49 +02:00
|
|
|
|
state="/run/nixos/network/addresses/${i.name}"
|
2020-05-29 12:20:06 +02:00
|
|
|
|
if [ -e "$state" ]; then
|
2024-12-09 01:28:54 +00:00
|
|
|
|
while read -r cidr; do
|
2020-05-29 12:20:06 +02:00
|
|
|
|
echo -n "deleting address $cidr... "
|
|
|
|
|
ip addr del "$cidr" dev "${i.name}" >/dev/null 2>&1 && echo "done" || echo "failed"
|
|
|
|
|
done < "$state"
|
|
|
|
|
rm -f "$state"
|
|
|
|
|
fi
|
2017-04-04 14:18:49 +02:00
|
|
|
|
'';
|
2014-11-20 02:37:48 -08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
createTunDevice =
|
|
|
|
|
i:
|
|
|
|
|
nameValuePair "${i.name}-netdev" {
|
|
|
|
|
description = "Virtual Network Interface ${i.name}";
|
2020-08-06 02:12:57 +00:00
|
|
|
|
bindsTo = optional (!config.boot.isContainer) "dev-net-tun.device";
|
|
|
|
|
after = optional (!config.boot.isContainer) "dev-net-tun.device" ++ [ "network-pre.target" ];
|
2016-09-09 05:25:56 +02:00
|
|
|
|
wantedBy = [
|
|
|
|
|
"network-setup.service"
|
|
|
|
|
(subsystemDevice i.name)
|
|
|
|
|
];
|
2017-09-24 16:01:07 +02:00
|
|
|
|
before = [ "network-setup.service" ];
|
2021-03-14 17:05:16 +01:00
|
|
|
|
path = [ pkgs.iproute2 ];
|
2014-11-20 02:37:48 -08:00
|
|
|
|
serviceConfig = {
|
|
|
|
|
Type = "oneshot";
|
|
|
|
|
RemainAfterExit = true;
|
|
|
|
|
};
|
|
|
|
|
script = ''
|
2018-01-31 05:51:09 +01:00
|
|
|
|
ip tuntap add dev "${i.name}" mode "${i.virtualType}" user "${i.virtualOwner}"
|
2014-11-20 02:37:48 -08:00
|
|
|
|
'';
|
|
|
|
|
postStop = ''
|
2023-10-29 18:27:16 +01:00
|
|
|
|
ip link del dev ${i.name} || true
|
2014-11-20 02:37:48 -08:00
|
|
|
|
'';
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
createBridgeDevice =
|
|
|
|
|
n: v:
|
|
|
|
|
nameValuePair "${n}-netdev" (
|
|
|
|
|
let
|
2016-09-30 14:52:46 +02:00
|
|
|
|
deps = concatLists (map deviceDependency v.interfaces);
|
2014-11-20 02:37:48 -08:00
|
|
|
|
in
|
|
|
|
|
{
|
|
|
|
|
description = "Bridge Interface ${n}";
|
2016-09-09 05:25:56 +02:00
|
|
|
|
wantedBy = [
|
|
|
|
|
"network-setup.service"
|
|
|
|
|
(subsystemDevice n)
|
|
|
|
|
];
|
2015-01-07 14:34:15 -08:00
|
|
|
|
bindsTo = deps ++ optional v.rstp "mstpd.service";
|
2016-09-09 05:25:56 +02:00
|
|
|
|
partOf = [ "network-setup.service" ] ++ optional v.rstp "mstpd.service";
|
2016-09-07 10:09:20 +00:00
|
|
|
|
after =
|
|
|
|
|
[ "network-pre.target" ]
|
|
|
|
|
++ deps
|
|
|
|
|
++ optional v.rstp "mstpd.service"
|
2020-05-17 21:57:01 +02:00
|
|
|
|
++ map (i: "network-addresses-${i}.service") v.interfaces;
|
2017-09-24 16:01:07 +02:00
|
|
|
|
before = [ "network-setup.service" ];
|
2014-11-20 02:37:48 -08:00
|
|
|
|
serviceConfig.Type = "oneshot";
|
|
|
|
|
serviceConfig.RemainAfterExit = true;
|
2021-03-14 17:05:16 +01:00
|
|
|
|
path = [ pkgs.iproute2 ];
|
2014-11-25 01:53:37 -08:00
|
|
|
|
script = ''
|
|
|
|
|
# Remove Dead Interfaces
|
|
|
|
|
echo "Removing old bridge ${n}..."
|
2023-10-29 18:27:16 +01:00
|
|
|
|
ip link show dev "${n}" >/dev/null 2>&1 && ip link del dev "${n}"
|
2025-04-01 20:10:43 +02:00
|
|
|
|
|
2014-11-25 01:53:37 -08:00
|
|
|
|
echo "Adding bridge ${n}..."
|
|
|
|
|
ip link add name "${n}" type bridge
|
2025-04-01 20:10:43 +02:00
|
|
|
|
|
2014-11-25 01:53:37 -08:00
|
|
|
|
# Enslave child interfaces
|
|
|
|
|
${flip concatMapStrings v.interfaces (i: ''
|
2023-10-29 18:27:16 +01:00
|
|
|
|
ip link set dev "${i}" master "${n}"
|
|
|
|
|
ip link set dev "${i}" up
|
2014-11-25 01:53:37 -08:00
|
|
|
|
'')}
|
2017-02-11 16:46:55 +01:00
|
|
|
|
# Save list of enslaved interfaces
|
|
|
|
|
echo "${
|
|
|
|
|
flip concatMapStrings v.interfaces (i: ''
|
|
|
|
|
${i}
|
|
|
|
|
'')
|
|
|
|
|
}" > /run/${n}.interfaces
|
2025-04-01 20:10:43 +02:00
|
|
|
|
|
2017-07-27 05:49:18 +00:00
|
|
|
|
${optionalString config.virtualisation.libvirtd.enable ''
|
|
|
|
|
# Enslave dynamically added interfaces which may be lost on nixos-rebuild
|
2020-03-09 09:05:13 +00:00
|
|
|
|
#
|
|
|
|
|
# if `libvirtd.service` is not running, do not use `virsh` which would try activate it via 'libvirtd.socket' and thus start it out-of-order.
|
|
|
|
|
# `libvirtd.service` will set up bridge interfaces when it will start normally.
|
|
|
|
|
#
|
2020-05-21 10:33:51 +02:00
|
|
|
|
if /run/current-system/systemd/bin/systemctl --quiet is-active 'libvirtd.service'; then
|
2020-03-09 09:05:13 +00:00
|
|
|
|
for uri in qemu:///system lxc:///; do
|
|
|
|
|
for dom in $(${pkgs.libvirt}/bin/virsh -c $uri list --name); do
|
|
|
|
|
${pkgs.libvirt}/bin/virsh -c $uri dumpxml "$dom" | \
|
2023-10-29 18:27:16 +01:00
|
|
|
|
${pkgs.xmlstarlet}/bin/xmlstarlet sel -t -m "//domain/devices/interface[@type='bridge'][source/@bridge='${n}'][target/@dev]" -v "concat('ip link set dev ',target/@dev,' master ',source/@bridge,';')" | \
|
2020-03-09 09:05:13 +00:00
|
|
|
|
${pkgs.bash}/bin/bash
|
|
|
|
|
done
|
2017-08-01 10:30:58 +00:00
|
|
|
|
done
|
2020-03-09 09:05:13 +00:00
|
|
|
|
fi
|
2017-07-27 05:49:18 +00:00
|
|
|
|
''}
|
|
|
|
|
|
2017-02-11 16:46:55 +01:00
|
|
|
|
# Enable stp on the interface
|
2014-11-20 02:37:48 -08:00
|
|
|
|
${optionalString v.rstp ''
|
2021-08-11 11:02:47 +02:00
|
|
|
|
echo 2 >/sys/class/net/${n}/bridge/stp_state
|
2014-11-20 02:37:48 -08:00
|
|
|
|
''}
|
2021-12-07 15:44:00 +00:00
|
|
|
|
|
2023-10-29 18:27:16 +01:00
|
|
|
|
ip link set dev "${n}" up
|
2025-04-01 20:10:43 +02:00
|
|
|
|
'';
|
2015-09-22 15:49:17 +02:00
|
|
|
|
postStop = ''
|
2023-10-29 18:27:16 +01:00
|
|
|
|
ip link set dev "${n}" down || true
|
|
|
|
|
ip link del dev "${n}" || true
|
2017-02-11 16:46:55 +01:00
|
|
|
|
rm -f /run/${n}.interfaces
|
2025-04-01 20:10:43 +02:00
|
|
|
|
'';
|
2017-02-11 16:46:55 +01:00
|
|
|
|
reload = ''
|
2014-11-20 02:37:48 -08:00
|
|
|
|
# Un-enslave child interfaces (old list of interfaces)
|
2017-02-11 16:46:55 +01:00
|
|
|
|
for interface in `cat /run/${n}.interfaces`; do
|
2014-11-20 02:37:48 -08:00
|
|
|
|
ip link set dev "$interface" nomaster up
|
2025-04-01 20:10:43 +02:00
|
|
|
|
done
|
|
|
|
|
|
2017-02-11 16:46:55 +01:00
|
|
|
|
# Enslave child interfaces (new list of interfaces)
|
2014-11-20 02:37:48 -08:00
|
|
|
|
${flip concatMapStrings v.interfaces (i: ''
|
2023-10-29 18:27:16 +01:00
|
|
|
|
ip link set dev "${i}" master "${n}"
|
|
|
|
|
ip link set dev "${i}" up
|
2025-04-01 20:10:43 +02:00
|
|
|
|
'')}
|
2017-02-11 16:46:55 +01:00
|
|
|
|
# Save list of enslaved interfaces
|
2025-04-01 20:10:43 +02:00
|
|
|
|
echo "${
|
2014-11-20 02:37:48 -08:00
|
|
|
|
flip concatMapStrings v.interfaces (i: ''
|
2025-04-01 20:10:43 +02:00
|
|
|
|
${i}
|
|
|
|
|
'')
|
2017-02-11 16:46:55 +01:00
|
|
|
|
}" > /run/${n}.interfaces
|
2025-04-01 20:10:43 +02:00
|
|
|
|
|
2017-02-11 16:46:55 +01:00
|
|
|
|
# (Un-)set stp on the bridge
|
|
|
|
|
echo ${if v.rstp then "2" else "0"} > /sys/class/net/${n}/bridge/stp_state
|
2025-04-01 20:10:43 +02:00
|
|
|
|
'';
|
2014-11-20 02:37:48 -08:00
|
|
|
|
reloadIfChanged = true;
|
2025-04-01 20:10:43 +02:00
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
2014-11-20 02:37:48 -08:00
|
|
|
|
createVswitchDevice =
|
|
|
|
|
n: v:
|
|
|
|
|
nameValuePair "${n}-netdev" (
|
|
|
|
|
let
|
2016-09-30 10:45:48 +02:00
|
|
|
|
deps = concatLists (
|
|
|
|
|
map deviceDependency (attrNames (filterAttrs (_: config: config.type != "internal") v.interfaces))
|
|
|
|
|
);
|
2020-05-17 21:57:01 +02:00
|
|
|
|
internalConfigs = map (i: "network-addresses-${i}.service") (
|
|
|
|
|
attrNames (filterAttrs (_: config: config.type == "internal") v.interfaces)
|
2025-04-01 20:10:43 +02:00
|
|
|
|
);
|
2015-09-22 15:49:17 +02:00
|
|
|
|
ofRules = pkgs.writeText "vswitch-${n}-openFlowRules" v.openFlowRules;
|
2014-11-20 02:37:48 -08:00
|
|
|
|
in
|
2024-10-04 22:07:18 +02:00
|
|
|
|
{
|
|
|
|
|
description = "Open vSwitch Interface ${n}";
|
2016-09-09 05:25:56 +02:00
|
|
|
|
wantedBy = [
|
|
|
|
|
"network-setup.service"
|
|
|
|
|
(subsystemDevice n)
|
2014-12-01 17:19:06 -08:00
|
|
|
|
] ++ internalConfigs;
|
2017-09-24 16:01:07 +02:00
|
|
|
|
# before = [ "network-setup.service" ];
|
2018-12-29 11:16:51 +01:00
|
|
|
|
# should work without internalConfigs dependencies because address/link configuration depends
|
|
|
|
|
# on the device, which is created by ovs-vswitchd with type=internal, but it does not...
|
|
|
|
|
before = [ "network-setup.service" ] ++ internalConfigs;
|
|
|
|
|
partOf = [ "network-setup.service" ]; # shutdown the bridge when network is shutdown
|
|
|
|
|
bindsTo = [ "ovs-vswitchd.service" ]; # requires ovs-vswitchd to be alive at all times
|
2014-12-01 17:19:06 -08:00
|
|
|
|
after = [
|
|
|
|
|
"network-pre.target"
|
2018-12-29 11:16:51 +01:00
|
|
|
|
"ovs-vswitchd.service"
|
|
|
|
|
] ++ deps; # start switch after physical interfaces and vswitch daemon
|
|
|
|
|
wants = deps; # if one or more interface fails, the switch should continue to run
|
2014-11-20 02:37:48 -08:00
|
|
|
|
serviceConfig.Type = "oneshot";
|
|
|
|
|
serviceConfig.RemainAfterExit = true;
|
2021-03-14 17:05:16 +01:00
|
|
|
|
path = [
|
|
|
|
|
pkgs.iproute2
|
|
|
|
|
config.virtualisation.vswitch.package
|
|
|
|
|
];
|
2018-12-29 11:16:51 +01:00
|
|
|
|
preStart = ''
|
|
|
|
|
echo "Resetting Open vSwitch ${n}..."
|
|
|
|
|
ovs-vsctl --if-exists del-br ${n} -- add-br ${n} \
|
|
|
|
|
-- set bridge ${n} protocols=${concatStringsSep "," v.supportedOpenFlowVersions}
|
2025-04-01 20:10:43 +02:00
|
|
|
|
'';
|
2014-11-20 02:37:48 -08:00
|
|
|
|
script = ''
|
2018-12-29 11:16:51 +01:00
|
|
|
|
echo "Configuring Open vSwitch ${n}..."
|
|
|
|
|
ovs-vsctl ${
|
|
|
|
|
concatStrings (
|
2017-02-02 22:21:03 +01:00
|
|
|
|
mapAttrsToList (
|
2018-12-29 11:16:51 +01:00
|
|
|
|
name: config:
|
|
|
|
|
" -- add-port ${n} ${name}" + optionalString (config.vlan != null) " tag=${toString config.vlan}"
|
|
|
|
|
) v.interfaces
|
2025-04-01 20:10:43 +02:00
|
|
|
|
)
|
|
|
|
|
} \
|
|
|
|
|
${
|
2018-12-29 11:16:51 +01:00
|
|
|
|
concatStrings (
|
2017-02-02 22:21:03 +01:00
|
|
|
|
mapAttrsToList (
|
2018-12-29 11:16:51 +01:00
|
|
|
|
name: config: optionalString (config.type != null) " -- set interface ${name} type=${config.type}"
|
|
|
|
|
) v.interfaces
|
2025-04-01 20:10:43 +02:00
|
|
|
|
)
|
|
|
|
|
} \
|
2015-09-22 15:49:17 +02:00
|
|
|
|
${concatMapStrings (x: " -- set-controller ${n} " + x) v.controllers} \
|
|
|
|
|
${concatMapStrings (x: " -- " + x) (splitString "\n" v.extraOvsctlCmds)}
|
2025-04-01 20:10:43 +02:00
|
|
|
|
|
|
|
|
|
|
2015-09-22 15:49:17 +02:00
|
|
|
|
echo "Adding OpenFlow rules for Open vSwitch ${n}..."
|
2018-12-29 11:16:51 +01:00
|
|
|
|
ovs-ofctl --protocols=${v.openFlowVersion} add-flows ${n} ${ofRules}
|
2025-04-01 20:10:43 +02:00
|
|
|
|
'';
|
2021-08-11 11:02:47 +02:00
|
|
|
|
postStop = ''
|
2018-12-29 11:16:51 +01:00
|
|
|
|
echo "Cleaning Open vSwitch ${n}"
|
2023-05-19 22:11:38 -04:00
|
|
|
|
echo "Shutting down internal ${n} interface"
|
2023-10-29 18:27:16 +01:00
|
|
|
|
ip link set dev ${n} down || true
|
2018-12-29 11:16:51 +01:00
|
|
|
|
echo "Deleting flows for ${n}"
|
|
|
|
|
ovs-ofctl --protocols=${v.openFlowVersion} del-flows ${n} || true
|
|
|
|
|
echo "Deleting Open vSwitch ${n}"
|
|
|
|
|
ovs-vsctl --if-exists del-br ${n} || true
|
2025-04-01 20:10:43 +02:00
|
|
|
|
'';
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
2014-11-20 02:37:48 -08:00
|
|
|
|
createBondDevice =
|
2025-04-01 20:10:43 +02:00
|
|
|
|
n: v:
|
2014-11-20 02:37:48 -08:00
|
|
|
|
nameValuePair "${n}-netdev" (
|
2025-04-01 20:10:43 +02:00
|
|
|
|
let
|
2016-09-30 14:52:46 +02:00
|
|
|
|
deps = concatLists (map deviceDependency v.interfaces);
|
2025-04-01 20:10:43 +02:00
|
|
|
|
in
|
|
|
|
|
{
|
2014-11-20 02:37:48 -08:00
|
|
|
|
description = "Bond Interface ${n}";
|
2016-09-09 05:25:56 +02:00
|
|
|
|
wantedBy = [
|
|
|
|
|
"network-setup.service"
|
|
|
|
|
(subsystemDevice n)
|
2025-04-01 20:10:43 +02:00
|
|
|
|
];
|
2014-11-20 02:37:48 -08:00
|
|
|
|
bindsTo = deps;
|
2018-12-29 11:16:51 +01:00
|
|
|
|
after = [ "network-pre.target" ] ++ deps ++ map (i: "network-addresses-${i}.service") v.interfaces;
|
2017-09-24 16:01:07 +02:00
|
|
|
|
before = [ "network-setup.service" ];
|
2014-11-20 02:37:48 -08:00
|
|
|
|
serviceConfig.Type = "oneshot";
|
|
|
|
|
serviceConfig.RemainAfterExit = true;
|
2025-04-01 20:10:43 +02:00
|
|
|
|
path = [
|
2021-03-14 17:05:16 +01:00
|
|
|
|
pkgs.iproute2
|
|
|
|
|
pkgs.gawk
|
2025-04-01 20:10:43 +02:00
|
|
|
|
];
|
2014-11-20 02:37:48 -08:00
|
|
|
|
script = ''
|
2014-11-25 01:53:37 -08:00
|
|
|
|
echo "Destroying old bond ${n}..."
|
|
|
|
|
${destroyBond n}
|
2025-04-01 20:10:43 +02:00
|
|
|
|
|
2014-11-25 01:53:37 -08:00
|
|
|
|
echo "Creating new bond ${n}..."
|
|
|
|
|
ip link add name "${n}" type bond \
|
2025-04-01 20:10:43 +02:00
|
|
|
|
${
|
|
|
|
|
let
|
2018-12-29 11:16:51 +01:00
|
|
|
|
opts = (mapAttrs (const toString) (bondDeprecation.filterDeprecated v)) // v.driverOptions;
|
2025-04-01 20:10:43 +02:00
|
|
|
|
in
|
2017-02-02 22:21:03 +01:00
|
|
|
|
concatStringsSep "\n" (mapAttrsToList (set: val: " ${set} ${val} \\") opts)
|
2025-04-01 20:10:43 +02:00
|
|
|
|
}
|
|
|
|
|
|
2014-11-25 01:53:37 -08:00
|
|
|
|
# !!! There must be a better way to wait for the interface
|
|
|
|
|
while [ ! -d "/sys/class/net/${n}" ]; do sleep 0.1; done;
|
2025-04-01 20:10:43 +02:00
|
|
|
|
|
2014-11-20 02:37:48 -08:00
|
|
|
|
# Bring up the bond and enslave the specified interfaces
|
2023-10-29 18:27:16 +01:00
|
|
|
|
ip link set dev "${n}" up
|
2014-11-20 02:37:48 -08:00
|
|
|
|
${flip concatMapStrings v.interfaces (i: ''
|
2023-10-29 18:27:16 +01:00
|
|
|
|
ip link set dev "${i}" down
|
|
|
|
|
ip link set dev "${i}" master "${n}"
|
2025-04-01 20:10:43 +02:00
|
|
|
|
'')}
|
|
|
|
|
'';
|
2014-11-25 01:53:37 -08:00
|
|
|
|
postStop = destroyBond n;
|
2025-04-01 20:10:43 +02:00
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
2014-11-26 15:42:32 -08:00
|
|
|
|
createMacvlanDevice =
|
2025-04-01 20:10:43 +02:00
|
|
|
|
n: v:
|
2021-12-07 15:44:00 +00:00
|
|
|
|
nameValuePair "${n}-netdev" (
|
2025-04-01 20:10:43 +02:00
|
|
|
|
let
|
2016-09-30 10:45:48 +02:00
|
|
|
|
deps = deviceDependency v.interface;
|
2025-04-01 20:10:43 +02:00
|
|
|
|
in
|
|
|
|
|
{
|
2024-10-04 22:05:42 +02:00
|
|
|
|
description = "MACVLAN Interface ${n}";
|
2021-12-07 15:44:00 +00:00
|
|
|
|
wantedBy = [
|
|
|
|
|
"network-setup.service"
|
|
|
|
|
(subsystemDevice n)
|
2025-04-01 20:10:43 +02:00
|
|
|
|
];
|
2021-12-07 15:44:00 +00:00
|
|
|
|
bindsTo = deps;
|
|
|
|
|
after = [ "network-pre.target" ] ++ deps;
|
|
|
|
|
before = [ "network-setup.service" ];
|
|
|
|
|
serviceConfig.Type = "oneshot";
|
|
|
|
|
serviceConfig.RemainAfterExit = true;
|
|
|
|
|
path = [ pkgs.iproute2 ];
|
|
|
|
|
script = ''
|
2014-11-20 02:37:48 -08:00
|
|
|
|
# Remove Dead Interfaces
|
2023-10-29 18:27:16 +01:00
|
|
|
|
ip link show dev "${n}" >/dev/null 2>&1 && ip link delete dev "${n}"
|
2014-11-20 02:37:48 -08:00
|
|
|
|
ip link add link "${v.interface}" name "${n}" type macvlan \
|
|
|
|
|
${optionalString (v.mode != null) "mode ${v.mode}"}
|
2018-12-29 11:16:51 +01:00
|
|
|
|
ip link set dev "${n}" up
|
2025-04-01 20:10:43 +02:00
|
|
|
|
'';
|
2018-08-13 20:38:32 +00:00
|
|
|
|
postStop = ''
|
2023-10-29 18:27:16 +01:00
|
|
|
|
ip link delete dev "${n}" || true
|
2014-11-20 02:37:48 -08:00
|
|
|
|
'';
|
2025-04-01 20:10:43 +02:00
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
2021-08-11 11:02:47 +02:00
|
|
|
|
createFouEncapsulation =
|
2025-04-01 20:10:43 +02:00
|
|
|
|
n: v:
|
2021-08-11 11:02:47 +02:00
|
|
|
|
nameValuePair "${n}-fou-encap" (
|
2025-04-01 20:10:43 +02:00
|
|
|
|
let
|
2021-08-11 11:02:47 +02:00
|
|
|
|
# if we have a device to bind to we can wait for its addresses to be
|
|
|
|
|
# configured, otherwise external sequencing is required.
|
|
|
|
|
deps = optionals (v.local != null && v.local.dev != null) (
|
|
|
|
|
deviceDependency v.local.dev ++ [ "network-addresses-${v.local.dev}.service" ]
|
2025-04-01 20:10:43 +02:00
|
|
|
|
);
|
2021-08-11 11:02:47 +02:00
|
|
|
|
fouSpec = "port ${toString v.port} ${
|
|
|
|
|
if v.protocol != null then "ipproto ${toString v.protocol}" else "gue"
|
2025-04-01 20:10:43 +02:00
|
|
|
|
} ${
|
2021-08-11 11:02:47 +02:00
|
|
|
|
optionalString (v.local != null)
|
|
|
|
|
"local ${escapeShellArg v.local.address} ${
|
|
|
|
|
optionalString (v.local.dev != null) "dev ${escapeShellArg v.local.dev}"
|
2025-04-01 20:10:43 +02:00
|
|
|
|
}"
|
|
|
|
|
}";
|
|
|
|
|
in
|
|
|
|
|
{
|
2021-08-11 11:02:47 +02:00
|
|
|
|
description = "FOU endpoint ${n}";
|
2021-12-07 15:44:00 +00:00
|
|
|
|
wantedBy = [
|
2016-09-09 05:25:56 +02:00
|
|
|
|
"network-setup.service"
|
2021-12-07 15:44:00 +00:00
|
|
|
|
(subsystemDevice n)
|
2025-04-01 20:10:43 +02:00
|
|
|
|
];
|
2014-11-20 02:37:48 -08:00
|
|
|
|
bindsTo = deps;
|
2021-12-07 15:44:00 +00:00
|
|
|
|
after = [ "network-pre.target" ] ++ deps;
|
|
|
|
|
before = [ "network-setup.service" ];
|
|
|
|
|
serviceConfig.Type = "oneshot";
|
|
|
|
|
serviceConfig.RemainAfterExit = true;
|
|
|
|
|
path = [ pkgs.iproute2 ];
|
|
|
|
|
script = ''
|
2021-08-11 11:02:47 +02:00
|
|
|
|
# always remove previous incarnation since show can't filter
|
|
|
|
|
ip fou del ${fouSpec} >/dev/null 2>&1 || true
|
|
|
|
|
ip fou add ${fouSpec}
|
2025-04-01 20:10:43 +02:00
|
|
|
|
'';
|
2014-11-20 02:37:48 -08:00
|
|
|
|
postStop = ''
|
2023-10-29 18:27:16 +01:00
|
|
|
|
ip fou del ${fouSpec} || true
|
2025-04-01 20:10:43 +02:00
|
|
|
|
'';
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
2014-11-20 02:37:48 -08:00
|
|
|
|
createSitDevice =
|
2025-04-01 20:10:43 +02:00
|
|
|
|
n: v:
|
2021-12-07 15:44:00 +00:00
|
|
|
|
nameValuePair "${n}-netdev" (
|
2025-04-01 20:10:43 +02:00
|
|
|
|
let
|
2021-12-07 15:44:00 +00:00
|
|
|
|
deps = deviceDependency v.dev;
|
2025-04-01 20:10:43 +02:00
|
|
|
|
in
|
|
|
|
|
{
|
2014-11-20 02:37:48 -08:00
|
|
|
|
description = "6-to-4 Tunnel Interface ${n}";
|
2021-12-07 15:44:00 +00:00
|
|
|
|
wantedBy = [
|
2016-09-09 05:25:56 +02:00
|
|
|
|
"network-setup.service"
|
2021-12-07 15:44:00 +00:00
|
|
|
|
(subsystemDevice n)
|
2025-04-01 20:10:43 +02:00
|
|
|
|
];
|
2014-11-20 02:37:48 -08:00
|
|
|
|
bindsTo = deps;
|
2021-12-07 15:44:00 +00:00
|
|
|
|
after = [ "network-pre.target" ] ++ deps;
|
|
|
|
|
before = [ "network-setup.service" ];
|
|
|
|
|
serviceConfig.Type = "oneshot";
|
|
|
|
|
serviceConfig.RemainAfterExit = true;
|
|
|
|
|
path = [ pkgs.iproute2 ];
|
|
|
|
|
script = ''
|
|
|
|
|
# Remove Dead Interfaces
|
2023-10-29 18:27:16 +01:00
|
|
|
|
ip link show dev "${n}" >/dev/null 2>&1 && ip link delete dev "${n}"
|
2014-11-24 12:06:07 -08:00
|
|
|
|
ip link add name "${n}" type sit \
|
2014-11-20 02:37:48 -08:00
|
|
|
|
${optionalString (v.remote != null) "remote \"${v.remote}\""} \
|
|
|
|
|
${optionalString (v.local != null) "local \"${v.local}\""} \
|
|
|
|
|
${optionalString (v.ttl != null) "ttl ${toString v.ttl}"} \
|
2015-09-22 15:49:17 +02:00
|
|
|
|
${optionalString (v.dev != null) "dev \"${v.dev}\""} \
|
2014-11-20 02:37:48 -08:00
|
|
|
|
${optionalString (v.encapsulation != null)
|
2021-08-11 11:02:47 +02:00
|
|
|
|
"encap ${v.encapsulation.type} encap-dport ${toString v.encapsulation.port} ${
|
2021-08-11 12:48:43 +02:00
|
|
|
|
optionalString (
|
2021-08-11 11:02:47 +02:00
|
|
|
|
v.encapsulation.sourcePort != null
|
2021-08-11 12:48:43 +02:00
|
|
|
|
) "encap-sport ${toString v.encapsulation.sourcePort}"
|
2025-04-01 20:10:43 +02:00
|
|
|
|
}"
|
|
|
|
|
}
|
2023-10-29 18:27:16 +01:00
|
|
|
|
ip link set dev "${n}" up
|
2025-04-01 20:10:43 +02:00
|
|
|
|
'';
|
2021-08-11 11:02:47 +02:00
|
|
|
|
postStop = ''
|
2023-10-29 18:27:16 +01:00
|
|
|
|
ip link delete dev "${n}" || true
|
2025-04-01 20:10:43 +02:00
|
|
|
|
'';
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
2021-08-11 11:02:47 +02:00
|
|
|
|
createGreDevice =
|
2025-04-01 20:10:43 +02:00
|
|
|
|
n: v:
|
2021-08-11 11:02:47 +02:00
|
|
|
|
nameValuePair "${n}-netdev" (
|
2025-04-01 20:10:43 +02:00
|
|
|
|
let
|
2021-12-07 15:44:00 +00:00
|
|
|
|
deps = deviceDependency v.dev;
|
2021-08-11 11:02:47 +02:00
|
|
|
|
ttlarg = if lib.hasPrefix "ip6" v.type then "hoplimit" else "ttl";
|
2025-04-01 20:10:43 +02:00
|
|
|
|
in
|
|
|
|
|
{
|
2021-12-07 15:44:00 +00:00
|
|
|
|
description = "GRE Tunnel Interface ${n}";
|
|
|
|
|
wantedBy = [
|
2016-09-09 05:25:56 +02:00
|
|
|
|
"network-setup.service"
|
2021-12-07 15:44:00 +00:00
|
|
|
|
(subsystemDevice n)
|
2025-04-01 20:10:43 +02:00
|
|
|
|
];
|
2014-11-20 02:37:48 -08:00
|
|
|
|
bindsTo = deps;
|
2021-12-07 15:44:00 +00:00
|
|
|
|
after = [ "network-pre.target" ] ++ deps;
|
2021-08-11 11:02:47 +02:00
|
|
|
|
before = [ "network-setup.service" ];
|
2021-12-07 15:44:00 +00:00
|
|
|
|
serviceConfig.Type = "oneshot";
|
2021-08-11 11:02:47 +02:00
|
|
|
|
serviceConfig.RemainAfterExit = true;
|
2021-12-07 15:44:00 +00:00
|
|
|
|
path = [ pkgs.iproute2 ];
|
|
|
|
|
script = ''
|
2021-08-11 11:02:47 +02:00
|
|
|
|
# Remove Dead Interfaces
|
2023-10-29 18:27:16 +01:00
|
|
|
|
ip link show dev "${n}" >/dev/null 2>&1 && ip link delete dev "${n}"
|
2021-12-07 15:44:00 +00:00
|
|
|
|
ip link add name "${n}" type ${v.type} \
|
|
|
|
|
${optionalString (v.remote != null) "remote \"${v.remote}\""} \
|
|
|
|
|
${optionalString (v.local != null) "local \"${v.local}\""} \
|
2022-03-22 20:24:25 +05:30
|
|
|
|
${optionalString (v.ttl != null) "${ttlarg} ${toString v.ttl}"} \
|
2021-12-07 15:44:00 +00:00
|
|
|
|
${optionalString (v.dev != null) "dev \"${v.dev}\""}
|
2023-10-29 18:27:16 +01:00
|
|
|
|
ip link set dev "${n}" up
|
2025-04-01 20:10:43 +02:00
|
|
|
|
'';
|
2014-11-20 02:37:48 -08:00
|
|
|
|
postStop = ''
|
2023-10-29 18:27:16 +01:00
|
|
|
|
ip link delete dev "${n}" || true
|
2025-04-01 20:10:43 +02:00
|
|
|
|
'';
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
2014-11-26 15:42:32 -08:00
|
|
|
|
createVlanDevice =
|
2025-04-01 20:10:43 +02:00
|
|
|
|
n: v:
|
2014-11-20 02:37:48 -08:00
|
|
|
|
nameValuePair "${n}-netdev" (
|
2025-04-01 20:10:43 +02:00
|
|
|
|
let
|
2016-09-30 10:45:48 +02:00
|
|
|
|
deps = deviceDependency v.interface;
|
2025-04-01 20:10:43 +02:00
|
|
|
|
in
|
|
|
|
|
{
|
2024-10-04 22:05:42 +02:00
|
|
|
|
description = "VLAN Interface ${n}";
|
2021-12-07 15:44:00 +00:00
|
|
|
|
wantedBy = [
|
2014-11-20 02:37:48 -08:00
|
|
|
|
"network-setup.service"
|
2021-12-07 15:44:00 +00:00
|
|
|
|
(subsystemDevice n)
|
2025-04-01 20:10:43 +02:00
|
|
|
|
];
|
2014-11-20 02:37:48 -08:00
|
|
|
|
bindsTo = deps;
|
2016-09-09 05:25:56 +02:00
|
|
|
|
partOf = [ "network-setup.service" ];
|
2021-12-07 15:44:00 +00:00
|
|
|
|
after = [ "network-pre.target" ] ++ deps;
|
|
|
|
|
before = [ "network-setup.service" ];
|
|
|
|
|
serviceConfig.Type = "oneshot";
|
|
|
|
|
serviceConfig.RemainAfterExit = true;
|
|
|
|
|
path = [ pkgs.iproute2 ];
|
|
|
|
|
script = ''
|
|
|
|
|
# Remove Dead Interfaces
|
2023-10-29 18:27:16 +01:00
|
|
|
|
ip link show dev "${n}" >/dev/null 2>&1 && ip link delete dev "${n}"
|
2014-11-26 15:42:32 -08:00
|
|
|
|
ip link add link "${v.interface}" name "${n}" type vlan id "${toString v.id}"
|
2025-04-01 20:10:43 +02:00
|
|
|
|
|
2018-12-29 11:16:51 +01:00
|
|
|
|
# We try to bring up the logical VLAN interface. If the master
|
|
|
|
|
# interface the logical interface is dependent upon is not up yet we will
|
2018-08-13 20:38:32 +00:00
|
|
|
|
# fail to immediately bring up the logical interface. The resulting logical
|
|
|
|
|
# interface will brought up later when the master interface is up.
|
2023-10-29 18:27:16 +01:00
|
|
|
|
ip link set dev "${n}" up || true
|
2025-04-01 20:10:43 +02:00
|
|
|
|
'';
|
2021-12-07 15:44:00 +00:00
|
|
|
|
postStop = ''
|
2023-10-29 18:27:16 +01:00
|
|
|
|
ip link delete dev "${n}" || true
|
2025-04-01 20:10:43 +02:00
|
|
|
|
'';
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
in
|
2014-11-20 02:37:48 -08:00
|
|
|
|
listToAttrs (
|
2020-05-17 21:57:01 +02:00
|
|
|
|
map configureAddrs interfaces ++ map createTunDevice (filter (i: i.virtual) interfaces)
|
2025-04-01 20:10:43 +02:00
|
|
|
|
)
|
2014-11-20 02:37:48 -08:00
|
|
|
|
// mapAttrs' createBridgeDevice cfg.bridges
|
|
|
|
|
// mapAttrs' createVswitchDevice cfg.vswitches
|
|
|
|
|
// mapAttrs' createBondDevice cfg.bonds
|
2014-11-26 15:42:32 -08:00
|
|
|
|
// mapAttrs' createMacvlanDevice cfg.macvlans
|
|
|
|
|
// mapAttrs' createFouEncapsulation cfg.fooOverUDP
|
|
|
|
|
// mapAttrs' createSitDevice cfg.sits
|
2021-12-07 15:44:00 +00:00
|
|
|
|
// mapAttrs' createGreDevice cfg.greTunnels
|
2014-11-20 02:37:48 -08:00
|
|
|
|
// mapAttrs' createVlanDevice cfg.vlans
|
|
|
|
|
// {
|
2019-08-13 21:52:01 +00:00
|
|
|
|
network-setup = networkSetup;
|
|
|
|
|
network-local-commands = networkLocalCommands;
|
2014-11-20 02:37:48 -08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
services.udev.extraRules = ''
|
|
|
|
|
KERNEL=="tun", TAG+="systemd"
|
2025-04-01 20:10:43 +02:00
|
|
|
|
'';
|
2017-02-02 22:21:03 +01:00
|
|
|
|
|
2014-11-20 02:37:48 -08:00
|
|
|
|
};
|
|
|
|
|
|
2017-02-02 22:21:03 +01:00
|
|
|
|
in
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
config = mkMerge [
|
|
|
|
|
bondWarnings
|
|
|
|
|
(mkIf (!cfg.useNetworkd) normalConfig)
|
2017-09-24 16:54:48 +02:00
|
|
|
|
{
|
|
|
|
|
# Ensure slave interfaces are brought up
|
|
|
|
|
networking.interfaces = genAttrs slaves (i: { });
|
|
|
|
|
}
|
2017-02-02 22:21:03 +01:00
|
|
|
|
];
|
2014-11-20 02:37:48 -08:00
|
|
|
|
}
|