2014-11-20 10:37:48 +00:00
|
|
|
|
{ config, lib, pkgs, utils, ... }:
|
|
|
|
|
|
|
|
|
|
with utils;
|
2014-12-11 00:28:45 +00:00
|
|
|
|
with lib;
|
2014-11-20 10:37:48 +00:00
|
|
|
|
|
|
|
|
|
let
|
|
|
|
|
|
|
|
|
|
cfg = config.networking;
|
|
|
|
|
interfaces = attrValues cfg.interfaces;
|
|
|
|
|
hasVirtuals = any (i: i.virtual) interfaces;
|
|
|
|
|
|
|
|
|
|
# We must escape interfaces due to the systemd interpretation
|
|
|
|
|
subsystemDevice = interface:
|
|
|
|
|
"sys-subsystem-net-devices-${escapeSystemdPath interface}.device";
|
|
|
|
|
|
|
|
|
|
interfaceIps = i:
|
|
|
|
|
i.ip4 ++ optionals cfg.enableIPv6 i.ip6
|
|
|
|
|
++ optional (i.ipAddress != null) {
|
|
|
|
|
address = i.ipAddress;
|
|
|
|
|
prefixLength = i.prefixLength;
|
|
|
|
|
} ++ optional (cfg.enableIPv6 && i.ipv6Address != null) {
|
|
|
|
|
address = i.ipv6Address;
|
|
|
|
|
prefixLength = i.ipv6PrefixLength;
|
|
|
|
|
};
|
|
|
|
|
|
2014-11-25 09:53:37 +00: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
|
|
|
|
|
ip link set "$I" nomaster
|
|
|
|
|
done
|
|
|
|
|
[ "$UPDATED" -eq "1" ] && break
|
|
|
|
|
done
|
2014-11-30 06:34:50 +00:00
|
|
|
|
ip link set "${i}" down 2>/dev/null || true
|
|
|
|
|
ip link del "${i}" 2>/dev/null || true
|
2014-11-25 09:53:37 +00:00
|
|
|
|
'';
|
|
|
|
|
|
2014-11-20 10:37:48 +00:00
|
|
|
|
in
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
config = mkIf (!cfg.useNetworkd) {
|
|
|
|
|
|
|
|
|
|
systemd.services =
|
|
|
|
|
let
|
|
|
|
|
|
2016-09-30 08:45:48 +00:00
|
|
|
|
deviceDependency = dev:
|
|
|
|
|
if (config.boot.isContainer == false)
|
|
|
|
|
then
|
|
|
|
|
# Trust udev when not in the container
|
|
|
|
|
[ (subsystemDevice dev) ]
|
|
|
|
|
else
|
|
|
|
|
# When in the container, check whether the interface is built from other definitions
|
|
|
|
|
if (hasAttr dev cfg.bridges) ||
|
|
|
|
|
(hasAttr dev cfg.bonds) ||
|
|
|
|
|
(hasAttr dev cfg.macvlans) ||
|
|
|
|
|
(hasAttr dev cfg.sits) ||
|
|
|
|
|
(hasAttr dev cfg.vlans) ||
|
|
|
|
|
(hasAttr dev cfg.vswitches) ||
|
|
|
|
|
(hasAttr dev cfg.wlanInterfaces)
|
|
|
|
|
then [ "${dev}-netdev.service" ]
|
|
|
|
|
else [];
|
|
|
|
|
|
2014-11-20 10:37:48 +00:00
|
|
|
|
networkLocalCommands = {
|
|
|
|
|
after = [ "network-setup.service" ];
|
|
|
|
|
bindsTo = [ "network-setup.service" ];
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
networkSetup =
|
|
|
|
|
{ description = "Networking Setup";
|
|
|
|
|
|
2016-09-09 03:25:56 +00:00
|
|
|
|
after = [ "network-pre.target" "systemd-udevd.service" "systemd-sysctl.service" ];
|
|
|
|
|
before = [ "network.target" "shutdown.target" ];
|
|
|
|
|
wants = [ "network.target" ];
|
|
|
|
|
conflicts = [ "shutdown.target" ];
|
|
|
|
|
wantedBy = [ "multi-user.target" ];
|
2014-11-20 10:37:48 +00:00
|
|
|
|
|
|
|
|
|
unitConfig.ConditionCapability = "CAP_NET_ADMIN";
|
|
|
|
|
|
|
|
|
|
path = [ pkgs.iproute ];
|
|
|
|
|
|
2016-09-09 03:25:56 +00:00
|
|
|
|
serviceConfig = {
|
|
|
|
|
Type = "oneshot";
|
|
|
|
|
RemainAfterExit = true;
|
|
|
|
|
};
|
2014-11-20 10:37:48 +00:00
|
|
|
|
|
2016-10-02 12:00:12 +00:00
|
|
|
|
unitConfig.DefaultDependencies = false;
|
|
|
|
|
|
2014-11-20 10:37:48 +00:00
|
|
|
|
script =
|
2014-12-01 09:18:26 +00:00
|
|
|
|
''
|
2014-11-20 10:37:48 +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}
|
|
|
|
|
''}
|
|
|
|
|
${optionalString (cfg.search != []) ("search " + concatStringsSep " " cfg.search)}
|
|
|
|
|
${flip concatMapStrings cfg.nameservers (ns: ''
|
|
|
|
|
nameserver ${ns}
|
|
|
|
|
'')}
|
|
|
|
|
EOF
|
2014-12-01 09:18:26 +00:00
|
|
|
|
|
2014-11-20 10:37:48 +00:00
|
|
|
|
# Set the default gateway.
|
2014-12-01 09:18:26 +00:00
|
|
|
|
${optionalString (cfg.defaultGateway != null && cfg.defaultGateway != "") ''
|
2014-11-20 10:37:48 +00:00
|
|
|
|
# FIXME: get rid of "|| true" (necessary to make it idempotent).
|
|
|
|
|
ip route add default via "${cfg.defaultGateway}" ${
|
|
|
|
|
optionalString (cfg.defaultGatewayWindowSize != null)
|
2015-12-25 00:43:49 +00:00
|
|
|
|
"window ${toString cfg.defaultGatewayWindowSize}"} || true
|
2014-11-20 10:37:48 +00:00
|
|
|
|
''}
|
2014-12-18 01:54:06 +00:00
|
|
|
|
${optionalString (cfg.defaultGateway6 != null && cfg.defaultGateway6 != "") ''
|
|
|
|
|
# FIXME: get rid of "|| true" (necessary to make it idempotent).
|
|
|
|
|
ip -6 route add ::/0 via "${cfg.defaultGateway6}" ${
|
|
|
|
|
optionalString (cfg.defaultGatewayWindowSize != null)
|
2015-12-25 00:43:49 +00:00
|
|
|
|
"window ${toString cfg.defaultGatewayWindowSize}"} || true
|
2014-12-18 01:54:06 +00:00
|
|
|
|
''}
|
2014-11-20 10:37:48 +00:00
|
|
|
|
'';
|
|
|
|
|
};
|
|
|
|
|
|
2014-11-24 20:06:07 +00:00
|
|
|
|
# For each interface <foo>, create a job ‘network-addresses-<foo>.service"
|
|
|
|
|
# that performs static address configuration. It has a "wants"
|
2014-11-20 10:37:48 +00: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 20:06:07 +00:00
|
|
|
|
configureAddrs = i:
|
2014-11-20 10:37:48 +00:00
|
|
|
|
let
|
|
|
|
|
ips = interfaceIps i;
|
|
|
|
|
in
|
2014-11-24 20:06:07 +00:00
|
|
|
|
nameValuePair "network-addresses-${i.name}"
|
2015-04-20 14:12:19 +00:00
|
|
|
|
{ description = "Address configuration of ${i.name}";
|
2016-09-09 03:25:56 +00:00
|
|
|
|
wantedBy = [ "network-setup.service" ];
|
|
|
|
|
# propagate stop and reload from network-setup
|
|
|
|
|
partOf = [ "network-setup.service" ];
|
|
|
|
|
# order before network-setup because the routes that are configured
|
|
|
|
|
# there may need ip addresses configured
|
|
|
|
|
before = [ "network-setup.service" ];
|
2016-09-30 08:45:48 +00:00
|
|
|
|
bindsTo = deviceDependency i.name;
|
|
|
|
|
after = [ "network-pre.target" ] ++ (deviceDependency i.name);
|
2014-11-20 10:37:48 +00:00
|
|
|
|
serviceConfig.Type = "oneshot";
|
|
|
|
|
serviceConfig.RemainAfterExit = true;
|
2014-11-26 19:19:31 +00:00
|
|
|
|
path = [ pkgs.iproute ];
|
2014-11-20 10:37:48 +00:00
|
|
|
|
script =
|
|
|
|
|
''
|
|
|
|
|
echo "bringing up interface..."
|
|
|
|
|
ip link set "${i.name}" up
|
|
|
|
|
|
2014-11-24 20:06:07 +00:00
|
|
|
|
restart_network_interfaces=false
|
|
|
|
|
'' + flip concatMapStrings (ips) (ip:
|
2014-11-20 10:37:48 +00:00
|
|
|
|
let
|
|
|
|
|
address = "${ip.address}/${toString ip.prefixLength}";
|
|
|
|
|
in
|
|
|
|
|
''
|
|
|
|
|
echo "checking ip ${address}..."
|
2014-11-24 20:06:07 +00:00
|
|
|
|
if out=$(ip addr add "${address}" dev "${i.name}" 2>&1); then
|
|
|
|
|
echo "added ip ${address}..."
|
|
|
|
|
elif ! echo "$out" | grep "File exists" >/dev/null 2>&1; then
|
|
|
|
|
echo "failed to add ${address}"
|
|
|
|
|
exit 1
|
2014-11-20 10:37:48 +00:00
|
|
|
|
fi
|
2016-09-09 03:25:56 +00:00
|
|
|
|
'');
|
2016-02-12 13:31:06 +00:00
|
|
|
|
preStop = flip concatMapStrings (ips) (ip:
|
2014-11-20 10:37:48 +00:00
|
|
|
|
let
|
|
|
|
|
address = "${ip.address}/${toString ip.prefixLength}";
|
|
|
|
|
in
|
|
|
|
|
''
|
2016-02-12 13:31:06 +00:00
|
|
|
|
echo -n "deleting ${address}..."
|
2014-11-20 10:37:48 +00:00
|
|
|
|
ip addr del "${address}" dev "${i.name}" >/dev/null 2>&1 || echo -n " Failed"
|
|
|
|
|
echo ""
|
|
|
|
|
'');
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
createTunDevice = i: nameValuePair "${i.name}-netdev"
|
|
|
|
|
{ description = "Virtual Network Interface ${i.name}";
|
2016-09-09 03:25:56 +00:00
|
|
|
|
bindsTo = [ "dev-net-tun.device" ];
|
2014-12-02 01:19:06 +00:00
|
|
|
|
after = [ "dev-net-tun.device" "network-pre.target" ];
|
2016-09-09 03:25:56 +00:00
|
|
|
|
wantedBy = [ "network-setup.service" (subsystemDevice i.name) ];
|
|
|
|
|
partOf = [ "network-setup.service" ];
|
|
|
|
|
before = [ "network-setup.service" (subsystemDevice i.name) ];
|
2014-11-20 10:37:48 +00:00
|
|
|
|
path = [ pkgs.iproute ];
|
|
|
|
|
serviceConfig = {
|
|
|
|
|
Type = "oneshot";
|
|
|
|
|
RemainAfterExit = true;
|
|
|
|
|
};
|
|
|
|
|
script = ''
|
|
|
|
|
ip tuntap add dev "${i.name}" \
|
|
|
|
|
${optionalString (i.virtualType != null) "mode ${i.virtualType}"} \
|
|
|
|
|
user "${i.virtualOwner}"
|
|
|
|
|
'';
|
|
|
|
|
postStop = ''
|
|
|
|
|
ip link del ${i.name}
|
|
|
|
|
'';
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
createBridgeDevice = n: v: nameValuePair "${n}-netdev"
|
|
|
|
|
(let
|
2016-09-30 12:52:46 +00:00
|
|
|
|
deps = concatLists (map deviceDependency v.interfaces);
|
2014-11-20 10:37:48 +00:00
|
|
|
|
in
|
|
|
|
|
{ description = "Bridge Interface ${n}";
|
2016-09-09 03:25:56 +00:00
|
|
|
|
wantedBy = [ "network-setup.service" (subsystemDevice n) ];
|
2015-01-07 22:34:15 +00:00
|
|
|
|
bindsTo = deps ++ optional v.rstp "mstpd.service";
|
2016-09-09 03:25:56 +00:00
|
|
|
|
partOf = [ "network-setup.service" ] ++ optional v.rstp "mstpd.service";
|
2015-01-07 22:34:15 +00:00
|
|
|
|
after = [ "network-pre.target" "mstpd.service" ] ++ deps
|
2014-12-02 01:19:06 +00:00
|
|
|
|
++ concatMap (i: [ "network-addresses-${i}.service" "network-link-${i}.service" ]) v.interfaces;
|
2016-09-09 03:25:56 +00:00
|
|
|
|
before = [ "network-setup.service" (subsystemDevice n) ];
|
2014-11-20 10:37:48 +00:00
|
|
|
|
serviceConfig.Type = "oneshot";
|
|
|
|
|
serviceConfig.RemainAfterExit = true;
|
2015-01-14 00:10:39 +00:00
|
|
|
|
path = [ pkgs.iproute ];
|
2014-11-25 09:53:37 +00:00
|
|
|
|
script = ''
|
|
|
|
|
# Remove Dead Interfaces
|
|
|
|
|
echo "Removing old bridge ${n}..."
|
|
|
|
|
ip link show "${n}" >/dev/null 2>&1 && ip link del "${n}"
|
2014-11-20 10:37:48 +00:00
|
|
|
|
|
2014-11-25 09:53:37 +00:00
|
|
|
|
echo "Adding bridge ${n}..."
|
|
|
|
|
ip link add name "${n}" type bridge
|
2014-11-20 10:37:48 +00:00
|
|
|
|
|
2014-11-25 09:53:37 +00:00
|
|
|
|
# Enslave child interfaces
|
|
|
|
|
${flip concatMapStrings v.interfaces (i: ''
|
|
|
|
|
ip link set "${i}" master "${n}"
|
|
|
|
|
ip link set "${i}" up
|
|
|
|
|
'')}
|
2014-11-20 10:37:48 +00:00
|
|
|
|
|
2015-01-14 00:10:39 +00:00
|
|
|
|
# Enable stp on the interface
|
2015-01-07 22:34:15 +00:00
|
|
|
|
${optionalString v.rstp ''
|
2015-01-14 00:10:39 +00:00
|
|
|
|
echo 2 >/sys/class/net/${n}/bridge/stp_state
|
2015-01-07 22:34:15 +00:00
|
|
|
|
''}
|
|
|
|
|
|
2014-11-25 09:53:37 +00:00
|
|
|
|
ip link set "${n}" up
|
|
|
|
|
'';
|
|
|
|
|
postStop = ''
|
|
|
|
|
ip link set "${n}" down || true
|
|
|
|
|
ip link del "${n}" || true
|
|
|
|
|
'';
|
2014-11-20 10:37:48 +00:00
|
|
|
|
});
|
|
|
|
|
|
2015-09-22 13:49:17 +00:00
|
|
|
|
createVswitchDevice = n: v: nameValuePair "${n}-netdev"
|
|
|
|
|
(let
|
2016-09-30 12:52:46 +00:00
|
|
|
|
deps = concatLists (map deviceDependency v.interfaces);
|
2015-09-22 13:49:17 +00:00
|
|
|
|
ofRules = pkgs.writeText "vswitch-${n}-openFlowRules" v.openFlowRules;
|
|
|
|
|
in
|
|
|
|
|
{ description = "Open vSwitch Interface ${n}";
|
2016-09-09 03:25:56 +00:00
|
|
|
|
wantedBy = [ "network-setup.service" "vswitchd.service" ] ++ deps;
|
2015-10-05 11:23:58 +00:00
|
|
|
|
bindsTo = [ "vswitchd.service" (subsystemDevice n) ] ++ deps;
|
2016-09-09 03:25:56 +00:00
|
|
|
|
partOf = [ "network-setup.service" "vswitchd.service" ];
|
2015-10-05 11:23:58 +00:00
|
|
|
|
after = [ "network-pre.target" "vswitchd.service" ] ++ deps;
|
2016-09-09 03:25:56 +00:00
|
|
|
|
before = [ "network-setup.service" ];
|
2015-09-22 13:49:17 +00:00
|
|
|
|
serviceConfig.Type = "oneshot";
|
|
|
|
|
serviceConfig.RemainAfterExit = true;
|
|
|
|
|
path = [ pkgs.iproute config.virtualisation.vswitch.package ];
|
|
|
|
|
script = ''
|
|
|
|
|
echo "Removing old Open vSwitch ${n}..."
|
|
|
|
|
ovs-vsctl --if-exists del-br ${n}
|
|
|
|
|
|
|
|
|
|
echo "Adding Open vSwitch ${n}..."
|
|
|
|
|
ovs-vsctl -- add-br ${n} ${concatMapStrings (i: " -- add-port ${n} ${i}") v.interfaces} \
|
|
|
|
|
${concatMapStrings (x: " -- set-controller ${n} " + x) v.controllers} \
|
|
|
|
|
${concatMapStrings (x: " -- " + x) (splitString "\n" v.extraOvsctlCmds)}
|
|
|
|
|
|
|
|
|
|
echo "Adding OpenFlow rules for Open vSwitch ${n}..."
|
|
|
|
|
ovs-ofctl add-flows ${n} ${ofRules}
|
|
|
|
|
'';
|
|
|
|
|
postStop = ''
|
|
|
|
|
ip link set ${n} down || true
|
|
|
|
|
ovs-ofctl del-flows ${n} || true
|
|
|
|
|
ovs-vsctl --if-exists del-br ${n}
|
|
|
|
|
'';
|
|
|
|
|
});
|
|
|
|
|
|
2014-11-20 10:37:48 +00:00
|
|
|
|
createBondDevice = n: v: nameValuePair "${n}-netdev"
|
|
|
|
|
(let
|
2016-09-30 12:52:46 +00:00
|
|
|
|
deps = concatLists (map deviceDependency v.interfaces);
|
2014-11-20 10:37:48 +00:00
|
|
|
|
in
|
|
|
|
|
{ description = "Bond Interface ${n}";
|
2016-09-09 03:25:56 +00:00
|
|
|
|
wantedBy = [ "network-setup.service" (subsystemDevice n) ];
|
2014-11-20 10:37:48 +00:00
|
|
|
|
bindsTo = deps;
|
2016-09-09 03:25:56 +00:00
|
|
|
|
partOf = [ "network-setup.service" ];
|
2014-12-02 01:19:06 +00:00
|
|
|
|
after = [ "network-pre.target" ] ++ deps
|
|
|
|
|
++ concatMap (i: [ "network-addresses-${i}.service" "network-link-${i}.service" ]) v.interfaces;
|
2016-09-09 03:25:56 +00:00
|
|
|
|
before = [ "network-setup.service" (subsystemDevice n) ];
|
2014-11-20 10:37:48 +00:00
|
|
|
|
serviceConfig.Type = "oneshot";
|
|
|
|
|
serviceConfig.RemainAfterExit = true;
|
2014-11-28 06:53:50 +00:00
|
|
|
|
path = [ pkgs.iproute pkgs.gawk ];
|
2014-11-20 10:37:48 +00:00
|
|
|
|
script = ''
|
2014-11-25 09:53:37 +00:00
|
|
|
|
echo "Destroying old bond ${n}..."
|
|
|
|
|
${destroyBond n}
|
2014-11-20 10:37:48 +00:00
|
|
|
|
|
2014-11-25 09:53:37 +00:00
|
|
|
|
echo "Creating new bond ${n}..."
|
|
|
|
|
ip link add name "${n}" type bond \
|
|
|
|
|
${optionalString (v.mode != null) "mode ${toString v.mode}"} \
|
|
|
|
|
${optionalString (v.miimon != null) "miimon ${toString v.miimon}"} \
|
|
|
|
|
${optionalString (v.xmit_hash_policy != null) "xmit_hash_policy ${toString v.xmit_hash_policy}"} \
|
|
|
|
|
${optionalString (v.lacp_rate != null) "lacp_rate ${toString v.lacp_rate}"}
|
2014-11-20 10:37:48 +00:00
|
|
|
|
|
2014-11-25 09:53:37 +00:00
|
|
|
|
# !!! There must be a better way to wait for the interface
|
|
|
|
|
while [ ! -d "/sys/class/net/${n}" ]; do sleep 0.1; done;
|
2014-11-20 10:37:48 +00:00
|
|
|
|
|
|
|
|
|
# Bring up the bond and enslave the specified interfaces
|
|
|
|
|
ip link set "${n}" up
|
|
|
|
|
${flip concatMapStrings v.interfaces (i: ''
|
2014-11-30 06:34:50 +00:00
|
|
|
|
ip link set "${i}" down
|
2014-11-25 09:53:37 +00:00
|
|
|
|
ip link set "${i}" master "${n}"
|
2014-11-20 10:37:48 +00:00
|
|
|
|
'')}
|
|
|
|
|
'';
|
2014-11-25 09:53:37 +00:00
|
|
|
|
postStop = destroyBond n;
|
2014-11-20 10:37:48 +00:00
|
|
|
|
});
|
|
|
|
|
|
2014-11-26 23:42:32 +00:00
|
|
|
|
createMacvlanDevice = n: v: nameValuePair "${n}-netdev"
|
|
|
|
|
(let
|
2016-09-30 08:45:48 +00:00
|
|
|
|
deps = deviceDependency v.interface;
|
2014-11-26 23:42:32 +00:00
|
|
|
|
in
|
|
|
|
|
{ description = "Vlan Interface ${n}";
|
2016-09-09 03:25:56 +00:00
|
|
|
|
wantedBy = [ "network-setup.service" (subsystemDevice n) ];
|
2014-11-26 23:42:32 +00:00
|
|
|
|
bindsTo = deps;
|
2016-09-09 03:25:56 +00:00
|
|
|
|
partOf = [ "network-setup.service" ];
|
2014-12-02 01:19:06 +00:00
|
|
|
|
after = [ "network-pre.target" ] ++ deps;
|
2016-09-09 03:25:56 +00:00
|
|
|
|
before = [ "network-setup.service" (subsystemDevice n) ];
|
2014-11-26 23:42:32 +00:00
|
|
|
|
serviceConfig.Type = "oneshot";
|
|
|
|
|
serviceConfig.RemainAfterExit = true;
|
|
|
|
|
path = [ pkgs.iproute ];
|
|
|
|
|
script = ''
|
|
|
|
|
# Remove Dead Interfaces
|
|
|
|
|
ip link show "${n}" >/dev/null 2>&1 && ip link delete "${n}"
|
|
|
|
|
ip link add link "${v.interface}" name "${n}" type macvlan \
|
|
|
|
|
${optionalString (v.mode != null) "mode ${v.mode}"}
|
|
|
|
|
ip link set "${n}" up
|
|
|
|
|
'';
|
|
|
|
|
postStop = ''
|
|
|
|
|
ip link delete "${n}"
|
|
|
|
|
'';
|
|
|
|
|
});
|
|
|
|
|
|
2014-11-20 10:37:48 +00:00
|
|
|
|
createSitDevice = n: v: nameValuePair "${n}-netdev"
|
|
|
|
|
(let
|
2016-10-11 21:55:22 +00:00
|
|
|
|
deps = optionals (v.dev != null) (deviceDependency v.dev);
|
2014-11-20 10:37:48 +00:00
|
|
|
|
in
|
|
|
|
|
{ description = "6-to-4 Tunnel Interface ${n}";
|
2016-09-09 03:25:56 +00:00
|
|
|
|
wantedBy = [ "network-setup.service" (subsystemDevice n) ];
|
2014-11-20 10:37:48 +00:00
|
|
|
|
bindsTo = deps;
|
2016-09-09 03:25:56 +00:00
|
|
|
|
partOf = [ "network-setup.service" ];
|
2014-12-02 01:19:06 +00:00
|
|
|
|
after = [ "network-pre.target" ] ++ deps;
|
2016-09-09 03:25:56 +00:00
|
|
|
|
before = [ "network-setup.service" (subsystemDevice n) ];
|
2014-11-20 10:37:48 +00:00
|
|
|
|
serviceConfig.Type = "oneshot";
|
|
|
|
|
serviceConfig.RemainAfterExit = true;
|
|
|
|
|
path = [ pkgs.iproute ];
|
|
|
|
|
script = ''
|
|
|
|
|
# Remove Dead Interfaces
|
|
|
|
|
ip link show "${n}" >/dev/null 2>&1 && ip link delete "${n}"
|
|
|
|
|
ip link add name "${n}" type sit \
|
|
|
|
|
${optionalString (v.remote != null) "remote \"${v.remote}\""} \
|
|
|
|
|
${optionalString (v.local != null) "local \"${v.local}\""} \
|
|
|
|
|
${optionalString (v.ttl != null) "ttl ${toString v.ttl}"} \
|
|
|
|
|
${optionalString (v.dev != null) "dev \"${v.dev}\""}
|
|
|
|
|
ip link set "${n}" up
|
|
|
|
|
'';
|
|
|
|
|
postStop = ''
|
|
|
|
|
ip link delete "${n}"
|
|
|
|
|
'';
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
createVlanDevice = n: v: nameValuePair "${n}-netdev"
|
|
|
|
|
(let
|
2016-09-30 08:45:48 +00:00
|
|
|
|
deps = deviceDependency v.interface;
|
2014-11-20 10:37:48 +00:00
|
|
|
|
in
|
|
|
|
|
{ description = "Vlan Interface ${n}";
|
2016-09-09 03:25:56 +00:00
|
|
|
|
wantedBy = [ "network-setup.service" (subsystemDevice n) ];
|
2014-11-20 10:37:48 +00:00
|
|
|
|
bindsTo = deps;
|
2016-09-09 03:25:56 +00:00
|
|
|
|
partOf = [ "network-setup.service" ];
|
2014-12-02 01:19:06 +00:00
|
|
|
|
after = [ "network-pre.target" ] ++ deps;
|
2016-09-09 03:25:56 +00:00
|
|
|
|
before = [ "network-setup.service" (subsystemDevice n) ];
|
2014-11-20 10:37:48 +00:00
|
|
|
|
serviceConfig.Type = "oneshot";
|
|
|
|
|
serviceConfig.RemainAfterExit = true;
|
|
|
|
|
path = [ pkgs.iproute ];
|
|
|
|
|
script = ''
|
|
|
|
|
# Remove Dead Interfaces
|
|
|
|
|
ip link show "${n}" >/dev/null 2>&1 && ip link delete "${n}"
|
|
|
|
|
ip link add link "${v.interface}" name "${n}" type vlan id "${toString v.id}"
|
|
|
|
|
ip link set "${n}" up
|
|
|
|
|
'';
|
|
|
|
|
postStop = ''
|
|
|
|
|
ip link delete "${n}"
|
|
|
|
|
'';
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
in listToAttrs (
|
2014-11-24 20:06:07 +00:00
|
|
|
|
map configureAddrs interfaces ++
|
2014-11-20 10:37:48 +00:00
|
|
|
|
map createTunDevice (filter (i: i.virtual) interfaces))
|
|
|
|
|
// mapAttrs' createBridgeDevice cfg.bridges
|
2015-09-22 13:49:17 +00:00
|
|
|
|
// mapAttrs' createVswitchDevice cfg.vswitches
|
2014-11-20 10:37:48 +00:00
|
|
|
|
// mapAttrs' createBondDevice cfg.bonds
|
2014-11-26 23:42:32 +00:00
|
|
|
|
// mapAttrs' createMacvlanDevice cfg.macvlans
|
2014-11-20 10:37:48 +00:00
|
|
|
|
// mapAttrs' createSitDevice cfg.sits
|
|
|
|
|
// mapAttrs' createVlanDevice cfg.vlans
|
|
|
|
|
// {
|
|
|
|
|
"network-setup" = networkSetup;
|
|
|
|
|
"network-local-commands" = networkLocalCommands;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
services.udev.extraRules =
|
|
|
|
|
''
|
|
|
|
|
KERNEL=="tun", TAG+="systemd"
|
|
|
|
|
'';
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
}
|