nixos/hosts/sigma/network.nix

266 lines
8.5 KiB
Nix
Raw Normal View History

2024-03-28 16:35:03 +01:00
{
config,
2024-03-29 20:38:51 +01:00
lib,
2024-03-28 16:35:03 +01:00
secrets,
...
}: {
2024-02-24 19:36:29 +01:00
systemd.network = {
config = {
routeTables = {
2024-03-29 20:36:24 +01:00
"wg-sigma-public" = 42;
"wg-sigma-p2p" = 6881;
2024-02-24 19:36:29 +01:00
};
};
2024-02-28 00:49:48 +01:00
2024-06-15 01:49:08 +02:00
# Rename network interfaces. The PermanentMACAddress is found using
# `ethtool -P enp5s0`.
links."10-wan0" = {
# Realtek motherboard port
matchConfig.PermanentMACAddress = "9c:6b:00:27:00:89";
linkConfig.Name = "wan0";
};
links."11-lan0" = {
# Intel pci port (right)
matchConfig.PermanentMACAddress = "00:15:17:a6:ee:a0";
linkConfig.Name = "lan0";
};
2024-06-12 03:12:04 +02:00
# The following configures the server as a typical "home router" with a
# DHCP server to hand out client addresses and NATing. The server's own
# address is requested from the ISP through DHCP.
2024-06-15 01:49:08 +02:00
networks."20-wan" = {
matchConfig.Name = "wan0";
2024-06-12 03:12:04 +02:00
networkConfig = {
# Enable DHCP *client* to request an IP address from the ISP. Denmark
# does not use IPv6.
DHCP = "ipv4";
};
# Ignore ISP DNS server(s) received from the DHCP server
dhcpV4Config.UseDNS = false;
dhcpV6Config.UseDNS = false;
2024-06-12 03:12:04 +02:00
};
2024-06-15 01:49:08 +02:00
networks."21-lan" = {
matchConfig.Name = "lan0";
2024-06-12 03:12:04 +02:00
address = [
"192.168.0.1/24"
];
networkConfig = {
# Enable DHCP *server*. By default, the DHCP leases handed out to
# clients contain DNS information from our own uplink interface and
# specify our own address as the router. See DHCP leases with
2024-06-15 01:49:08 +02:00
# `networkctl status lan0` and `dhcpdump -i lan0`.
2024-06-12 03:12:04 +02:00
DHCPServer = true;
# Enable IP masquerading (NAT) to rewrite the address on packets
# forwarded from this interface so as to appear as coming from this
# host. Required to share a single external IP address and act as a
# "router" since each lan host does not get its own public IP address.
IPMasquerade = "ipv4";
};
};
2024-02-24 19:36:29 +01:00
# The following establishes a wireguard tunnel to alpha and configures
# receiving traffic destined for 49.13.33.75. This allows us to have a
# public address even though we are behind NAT.
netdevs."50-wg-sigma-public" = {
netdevConfig = {
Name = "wg-sigma-public";
Kind = "wireguard";
};
wireguardConfig = {
2024-03-29 20:38:51 +01:00
PrivateKeyFile = config.age.secrets.wireguard-private-key-file-sigma.path;
2024-02-24 19:36:29 +01:00
};
wireguardPeers = [
{
wireguardPeerConfig = {
PublicKey = "AlphazUR/z+1DRCFSvxTeKPIJnyPQvYsDoSgESvqJhM=";
PresharedKeyFile = config.age.secrets.wireguard-preshared-key-file.path;
2024-09-07 18:49:55 +02:00
# Explicit IPv4 address of alpha.caspervk.net to avoid attempting
# to (re)connect through IPv6(??).
Endpoint = "116.203.179.206:51820";
2024-02-24 19:36:29 +01:00
# Keep NAT mappings and stateful firewalls open at the ISP
PersistentKeepalive = 25;
# AllowedIPs is both an ACL for incoming traffic, as well as a
# routing table specifying to which peer outgoing traffic should be
# sent. We want to allow incoming traffic from any address on the
# internet (routed through alpha), but only replies to this should
# be routed back over wireguard. Unlike if we had used NAT, IP
# routes are stateless, so we have no notion of "replies". Instead,
# we add these routes to a specific routing table and configure a
# routing policy rule to only use it for packets being sent as the
# public IP.
2024-03-05 22:57:41 +01:00
AllowedIPs = ["0.0.0.0/0"];
2024-02-24 19:36:29 +01:00
RouteTable = "wg-sigma-public";
};
}
];
};
networks."50-wg-sigma-public" = {
matchConfig.Name = "wg-sigma-public";
2024-03-05 22:57:41 +01:00
address = ["49.13.33.75/32"];
2024-02-24 19:36:29 +01:00
routingPolicyRules = [
{
# Allow hosts on the local network to contact us directly on the
# public address instead of routing the packet through Wireguard and
# back again.
routingPolicyRuleConfig = {
2024-06-15 00:47:03 +02:00
Priority = 10;
To = "192.168.0.0/24";
Table = "main";
};
}
2024-06-15 00:47:03 +02:00
{
# The postfix systemd service has
# RestrictNetworkInterfaces=wg-sigma-public, but that does not tell
2024-06-15 01:49:08 +02:00
# it to use the correct routing table. You can check that this works
# as expected using `sudo -u postfix curl ip.caspervk.net`.
2024-06-15 00:47:03 +02:00
routingPolicyRuleConfig = {
Priority = 100;
User = config.services.postfix.user;
Table = "wg-sigma-public";
};
}
2024-04-26 01:25:50 +02:00
{
# See the AllowedIPs comment above for why this is necessary
2024-04-26 01:25:50 +02:00
routingPolicyRuleConfig = {
Priority = 1000;
From = "49.13.33.75/32";
2024-04-26 01:25:50 +02:00
Table = "wg-sigma-public";
};
}
2024-02-24 19:36:29 +01:00
];
};
2024-02-28 00:49:48 +01:00
# The following establishes a wireguard tunnel to alpha and configures
2024-03-29 20:36:24 +01:00
# receiving traffic destined for the sigma-p2p address. This allows the
# server to have a public address and help others sail the high seas even
# though it is behind NAT.
netdevs."50-wg-sigma-p2p" = {
2024-02-28 00:49:48 +01:00
netdevConfig = {
Name = "wg-sigma-p2p";
Kind = "wireguard";
};
wireguardConfig = {
2024-03-29 20:38:51 +01:00
PrivateKeyFile = config.age.secrets.wireguard-private-key-file-sigma.path;
2024-02-28 00:49:48 +01:00
};
wireguardPeers = [
{
wireguardPeerConfig = {
PublicKey = "AlphazUR/z+1DRCFSvxTeKPIJnyPQvYsDoSgESvqJhM=";
PresharedKeyFile = config.age.secrets.wireguard-preshared-key-file.path;
2024-09-07 18:49:55 +02:00
# Explicit IPv4 address of alpha.caspervk.net to avoid attempting
# to (re)connect through IPv6(??).
Endpoint = "116.203.179.206:51821";
2024-02-28 00:49:48 +01:00
PersistentKeepalive = 25;
2024-03-05 22:57:41 +01:00
AllowedIPs = ["0.0.0.0/0"];
2024-02-28 00:49:48 +01:00
RouteTable = "wg-sigma-p2p";
};
}
];
};
networks."50-wg-sigma-p2p" = {
matchConfig.Name = "wg-sigma-p2p";
2024-05-10 18:50:22 +02:00
address = ["${secrets.hosts.sigma.sigma-p2p-ip-address}/32"];
2024-02-28 00:49:48 +01:00
routingPolicyRules = [
{
# The deluge systemd service has
# RestrictNetworkInterfaces=wg-sigma-p2p, but that does not tell it
2024-06-15 01:49:08 +02:00
# to use the correct routing table. You can check that this works as
# expected using `sudo -u deluge curl ip.caspervk.net`.
2024-02-28 00:49:48 +01:00
routingPolicyRuleConfig = {
2024-06-15 00:47:03 +02:00
Priority = 100;
User = config.services.deluge.user;
2024-02-28 00:49:48 +01:00
Table = "wg-sigma-p2p";
};
}
2024-03-29 20:38:51 +01:00
{
routingPolicyRuleConfig = {
Priority = 1000;
2024-05-10 18:50:22 +02:00
From = "${secrets.hosts.sigma.sigma-p2p-ip-address}/32";
2024-03-29 20:38:51 +01:00
Table = "wg-sigma-p2p";
};
}
2024-02-28 00:49:48 +01:00
];
};
};
2024-03-29 20:38:51 +01:00
# Force explicit firewall configuration to ensure we allow the right services
# on the right interfaces.
networking.firewall = {
allowedTCPPorts = lib.mkForce [];
allowedUDPPorts = lib.mkForce [];
allowedTCPPortRanges = lib.mkForce [];
allowedUDPPortRanges = lib.mkForce [];
interfaces = {
2024-06-15 01:49:08 +02:00
"lan0" = {
2024-04-16 01:49:39 +02:00
allowedTCPPorts = [
22 # SSH
2024-04-26 01:25:50 +02:00
25 # Mail SMTP
2024-06-11 01:13:12 +02:00
80 # Caddy
139 # Samba
2024-04-22 23:59:18 +02:00
443 # Caddy
2024-05-09 17:24:46 +02:00
445 # Samba
2024-04-26 01:25:50 +02:00
465 # Mail ESMTP
2024-06-11 01:13:12 +02:00
993 # Mail IMAPS
1234 # ad hoc
1337 # ad hoc
2024-05-10 16:36:10 +02:00
8000 # ad hoc
8080 # ad hoc
2024-06-11 01:13:12 +02:00
22000 # syncthing
2024-04-16 01:49:39 +02:00
];
2024-05-10 16:36:10 +02:00
allowedUDPPorts = [
2024-06-12 03:12:04 +02:00
67 # DHCP server
2024-06-11 01:13:12 +02:00
445 # Samba
2024-05-10 20:34:38 +02:00
21027 # syncthing
22000 # syncthing
2024-05-10 16:36:10 +02:00
];
2024-03-29 20:38:51 +01:00
};
"wg-sigma-public" = {
2024-04-16 01:49:39 +02:00
allowedTCPPorts = [
22 # SSH
2024-04-26 01:25:50 +02:00
25 # Mail SMTP
2024-06-11 01:13:12 +02:00
80 # Caddy
2024-04-16 01:49:39 +02:00
443 # Caddy
2024-04-26 01:25:50 +02:00
465 # Mail ESMTP
2024-06-11 01:13:12 +02:00
993 # Mail IMAPS
1234 # ad hoc
1337 # ad hoc
2024-05-10 16:36:10 +02:00
8000 # ad hoc
8080 # ad hoc
2024-06-11 01:13:12 +02:00
22000 # syncthing
2024-04-16 01:49:39 +02:00
];
2024-05-10 20:34:38 +02:00
allowedUDPPorts = [
21027 # syncthing
22000 # syncthing
];
2024-03-29 20:38:51 +01:00
};
"wg-sigma-p2p" = {
2024-04-16 01:49:39 +02:00
allowedTCPPorts = [
2024-04-22 23:59:18 +02:00
60881 # Deluge
];
allowedUDPPorts = [
60881 # Deluge
2024-04-16 01:49:39 +02:00
];
2024-03-29 20:38:51 +01:00
};
};
};
2024-06-12 03:12:04 +02:00
# Enable forwarding of packets
boot.kernel.sysctl = {
"net.ipv4.conf.all.forwarding" = true;
};
2024-02-24 19:36:29 +01:00
age.secrets.wireguard-preshared-key-file = {
2024-03-28 16:35:03 +01:00
file = "${secrets}/secrets/wireguard-preshared-key-file.age";
2024-05-09 17:26:55 +02:00
mode = "440";
2024-02-24 19:36:29 +01:00
owner = "root";
group = "systemd-network";
};
2024-03-29 20:38:51 +01:00
age.secrets.wireguard-private-key-file-sigma = {
file = "${secrets}/secrets/wireguard-private-key-file-sigma.age";
2024-05-09 17:26:55 +02:00
mode = "440";
2024-02-24 19:36:29 +01:00
owner = "root";
group = "systemd-network";
};
}