2020-04-27 18:27:17 +00:00
|
|
|
{ lib, ... }:
|
2020-01-04 22:16:31 +00:00
|
|
|
|
|
|
|
let
|
2020-04-28 13:22:01 +00:00
|
|
|
vars = import ./lib/vars.nix;
|
2020-01-04 22:16:31 +00:00
|
|
|
|
2020-01-24 22:42:21 +00:00
|
|
|
# Port definitions.
|
2020-01-04 22:16:31 +00:00
|
|
|
ports = {
|
|
|
|
dns = "53";
|
|
|
|
dhcp4_server = "67";
|
|
|
|
dhcp4_client = "68";
|
|
|
|
dhcp6_client = "546";
|
|
|
|
dhcp6_server = "547";
|
|
|
|
http = "80";
|
|
|
|
https = "443";
|
|
|
|
imaps = "993";
|
2020-02-04 21:46:49 +00:00
|
|
|
mdns = "5353";
|
2020-01-04 22:16:31 +00:00
|
|
|
ntp = "123";
|
|
|
|
pop3s = "995";
|
|
|
|
plex = "32400";
|
|
|
|
smtp = "587";
|
|
|
|
ssh = "22";
|
2020-06-11 18:05:57 +00:00
|
|
|
unifi_device = "8080";
|
|
|
|
unifi_stun = "3478";
|
|
|
|
unifi_web = "8443";
|
2020-01-04 22:16:31 +00:00
|
|
|
wireguard = "51820";
|
|
|
|
};
|
|
|
|
|
2020-01-24 22:42:21 +00:00
|
|
|
# Produces a CSV list of interface names.
|
|
|
|
mkCSV = lib.concatMapStrings (ifi: "${ifi.name}, ");
|
|
|
|
|
|
|
|
# WAN interface.
|
|
|
|
wan0 = vars.interfaces.wan0.name;
|
|
|
|
|
|
|
|
# LAN interfaces, segmented into trusted, limited, and untrusted groups.
|
2020-05-24 16:30:08 +00:00
|
|
|
trusted_lans = with vars.interfaces; [ enp2s0 lan0 lab0 tengb0 wg0 ];
|
2020-01-24 22:42:21 +00:00
|
|
|
limited_lans = [ vars.interfaces.guest0 ];
|
|
|
|
untrusted_lans = [ vars.interfaces.iot0 ];
|
|
|
|
|
2020-02-05 20:07:05 +00:00
|
|
|
# ICMP filtering.
|
|
|
|
icmp_rules = ''
|
|
|
|
ip6 nexthdr icmpv6 icmpv6 type {
|
|
|
|
echo-request,
|
|
|
|
destination-unreachable,
|
|
|
|
packet-too-big,
|
|
|
|
time-exceeded,
|
|
|
|
parameter-problem,
|
|
|
|
nd-neighbor-solicit,
|
|
|
|
nd-neighbor-advert,
|
|
|
|
} counter accept
|
|
|
|
|
|
|
|
ip protocol icmp icmp type {
|
|
|
|
echo-request,
|
|
|
|
destination-unreachable,
|
|
|
|
time-exceeded,
|
|
|
|
parameter-problem,
|
|
|
|
} counter accept
|
|
|
|
'';
|
|
|
|
|
2020-01-04 22:16:31 +00:00
|
|
|
in {
|
|
|
|
networking.nftables = {
|
|
|
|
enable = true;
|
|
|
|
ruleset = ''
|
|
|
|
table inet filter {
|
|
|
|
# Incoming connections to router itself.
|
|
|
|
chain input {
|
|
|
|
type filter hook input priority 0
|
|
|
|
policy drop
|
|
|
|
|
|
|
|
ct state {established, related} counter accept
|
|
|
|
ct state invalid counter drop
|
|
|
|
|
|
|
|
# Malicious subnets.
|
|
|
|
ip saddr {
|
|
|
|
49.64.0.0/11,
|
|
|
|
218.92.0.0/16,
|
|
|
|
222.184.0.0/13,
|
|
|
|
} counter drop comment "malicious subnets"
|
|
|
|
|
2020-02-05 20:07:05 +00:00
|
|
|
# ICMPv4/6.
|
|
|
|
${icmp_rules}
|
2020-01-04 22:16:31 +00:00
|
|
|
|
|
|
|
# Allow WAN to selectively communicate with the router.
|
2020-01-24 22:42:21 +00:00
|
|
|
iifname ${wan0} jump input_wan
|
2020-01-04 22:16:31 +00:00
|
|
|
|
|
|
|
# Always allow router solicitation from any LAN.
|
|
|
|
ip6 nexthdr icmpv6 icmpv6 type nd-router-solicit counter accept
|
|
|
|
|
|
|
|
# Allow localhost and trusted LANs to communicate with router.
|
|
|
|
iifname {
|
|
|
|
lo,
|
2020-01-24 22:42:21 +00:00
|
|
|
${mkCSV trusted_lans}
|
|
|
|
} counter accept comment "localhost and trusted LANs to router"
|
2020-01-04 22:16:31 +00:00
|
|
|
|
2020-01-24 22:42:21 +00:00
|
|
|
# Limit the communication abilities of limited and untrusted LANs.
|
2020-01-04 22:16:31 +00:00
|
|
|
iifname {
|
2020-01-24 22:42:21 +00:00
|
|
|
${mkCSV limited_lans}
|
|
|
|
${mkCSV untrusted_lans}
|
|
|
|
} jump input_limited_untrusted
|
2020-01-04 22:16:31 +00:00
|
|
|
|
|
|
|
counter reject
|
|
|
|
}
|
|
|
|
|
|
|
|
chain input_wan {
|
|
|
|
# Default route via NDP.
|
|
|
|
ip6 nexthdr icmpv6 icmpv6 type nd-router-advert counter accept
|
|
|
|
|
|
|
|
# router TCP
|
|
|
|
tcp dport {
|
2020-01-23 19:47:42 +00:00
|
|
|
${ports.http},
|
|
|
|
${ports.https},
|
2020-01-04 22:16:31 +00:00
|
|
|
${ports.ssh},
|
|
|
|
} counter accept comment "router WAN TCP"
|
|
|
|
|
|
|
|
# router UDP
|
|
|
|
udp dport {
|
|
|
|
${ports.wireguard},
|
|
|
|
} counter accept comment "router WAN UDP"
|
|
|
|
|
|
|
|
# router DHCPv6 client
|
|
|
|
ip6 daddr fe80::/64 udp dport ${ports.dhcp6_client} udp sport ${ports.dhcp6_server} counter accept comment "router WAN DHCPv6"
|
|
|
|
|
|
|
|
counter reject
|
|
|
|
}
|
|
|
|
|
2020-01-24 22:42:21 +00:00
|
|
|
chain input_limited_untrusted {
|
2020-02-04 21:46:49 +00:00
|
|
|
# Handle some services early due to need for multicast/broadcast.
|
2020-01-04 22:16:31 +00:00
|
|
|
udp dport ${ports.dhcp4_server} udp sport ${ports.dhcp4_client} counter accept comment "router untrusted DHCPv4"
|
|
|
|
|
2020-02-04 21:46:49 +00:00
|
|
|
udp dport ${ports.mdns} udp sport ${ports.mdns} counter accept comment "router untrusted mDNS"
|
|
|
|
|
2020-01-04 22:16:31 +00:00
|
|
|
# Drop traffic trying to cross VLANs or broadcast.
|
2020-01-24 22:42:21 +00:00
|
|
|
${
|
|
|
|
lib.concatMapStrings (ifi: ''
|
|
|
|
iifname ${ifi.name} ip daddr != ${ifi.ipv4} counter drop comment "${ifi.name} traffic leaving IPv4 VLAN"
|
2020-01-04 22:16:31 +00:00
|
|
|
|
2020-01-24 22:42:21 +00:00
|
|
|
iifname ${ifi.name} ip6 daddr != {
|
|
|
|
${ifi.ipv6.lla},
|
|
|
|
${ifi.ipv6.ula},
|
|
|
|
} counter drop comment "${ifi.name} traffic leaving IPv6 VLAN"
|
|
|
|
'') (limited_lans ++ untrusted_lans)
|
|
|
|
}
|
2020-01-04 22:16:31 +00:00
|
|
|
|
|
|
|
# Allow only necessary router-provided services.
|
|
|
|
tcp dport {
|
|
|
|
${ports.dns},
|
|
|
|
} counter accept comment "router untrusted TCP"
|
|
|
|
|
|
|
|
udp dport {
|
|
|
|
${ports.dns},
|
|
|
|
} counter accept comment "router untrusted UDP"
|
|
|
|
|
|
|
|
counter drop
|
|
|
|
}
|
|
|
|
|
|
|
|
chain output {
|
|
|
|
type filter hook output priority 0
|
|
|
|
policy accept
|
|
|
|
|
2020-02-04 21:46:49 +00:00
|
|
|
# Generally allow all outgoing router connections, except in cases
|
|
|
|
# where software does not provide fine-grained control over traffic.
|
|
|
|
|
2020-01-04 22:16:31 +00:00
|
|
|
counter accept
|
|
|
|
}
|
|
|
|
|
|
|
|
chain forward {
|
|
|
|
type filter hook forward priority 0
|
|
|
|
policy drop
|
|
|
|
|
2020-02-04 21:46:49 +00:00
|
|
|
# Untrusted/limited LANs to trusted LANs.
|
|
|
|
iifname {
|
|
|
|
${mkCSV limited_lans}
|
|
|
|
${mkCSV untrusted_lans}
|
|
|
|
} oifname {
|
|
|
|
${mkCSV trusted_lans}
|
|
|
|
} jump forward_limited_untrusted_lan_trusted_lan
|
|
|
|
|
|
|
|
# We still want to allow limited/untrusted LANs to have working ICMP
|
|
|
|
# to the internet as a whole, just not to any trusted LANs.
|
2020-02-05 20:07:05 +00:00
|
|
|
${icmp_rules}
|
2020-01-04 22:16:31 +00:00
|
|
|
|
2020-02-04 21:46:49 +00:00
|
|
|
# Forwarding between different interface groups.
|
|
|
|
|
|
|
|
# Trusted source LANs.
|
|
|
|
iifname {
|
|
|
|
${mkCSV trusted_lans}
|
|
|
|
} oifname ${wan0} counter accept comment "Allow trusted LANs to WAN";
|
|
|
|
|
2020-01-04 22:16:31 +00:00
|
|
|
iifname {
|
2020-01-24 22:42:21 +00:00
|
|
|
${mkCSV trusted_lans}
|
2020-02-04 21:46:49 +00:00
|
|
|
} oifname {
|
|
|
|
${mkCSV trusted_lans},
|
|
|
|
${mkCSV limited_lans},
|
|
|
|
${mkCSV untrusted_lans},
|
|
|
|
} counter accept comment "Allow trusted LANs to reach all LANs";
|
2020-01-04 22:16:31 +00:00
|
|
|
|
|
|
|
# Limited/guest LANs to WAN.
|
|
|
|
iifname {
|
2020-01-24 22:42:21 +00:00
|
|
|
${mkCSV limited_lans}
|
|
|
|
} oifname ${wan0} jump forward_limited_lan_wan
|
2020-01-04 22:16:31 +00:00
|
|
|
|
|
|
|
# Untrusted LANs to WAN.
|
|
|
|
iifname {
|
2020-01-24 22:42:21 +00:00
|
|
|
${mkCSV untrusted_lans}
|
|
|
|
} oifname ${wan0} jump forward_untrusted_lan_wan
|
2020-01-04 22:16:31 +00:00
|
|
|
|
|
|
|
# WAN to trusted LANs.
|
2020-01-24 22:42:21 +00:00
|
|
|
iifname ${wan0} oifname {
|
|
|
|
${mkCSV trusted_lans}
|
2020-01-04 22:16:31 +00:00
|
|
|
} jump forward_wan_trusted_lan
|
|
|
|
|
2020-01-24 22:42:21 +00:00
|
|
|
# WAN to limited/untrusted LANs.
|
|
|
|
iifname ${wan0} oifname {
|
|
|
|
${mkCSV limited_lans}
|
|
|
|
${mkCSV untrusted_lans}
|
|
|
|
} jump forward_wan_limited_untrusted_lan
|
2020-01-04 22:16:31 +00:00
|
|
|
|
|
|
|
counter reject
|
|
|
|
}
|
|
|
|
|
|
|
|
chain forward_limited_lan_wan {
|
|
|
|
# Forward typical network services.
|
|
|
|
|
|
|
|
tcp dport {
|
|
|
|
${ports.dns},
|
|
|
|
${ports.http},
|
|
|
|
${ports.https},
|
|
|
|
${ports.imaps},
|
|
|
|
${ports.pop3s},
|
|
|
|
${ports.smtp},
|
|
|
|
${ports.ssh},
|
|
|
|
} counter accept comment "limited TCP"
|
|
|
|
|
|
|
|
udp dport {
|
|
|
|
${ports.dns},
|
|
|
|
${ports.ntp},
|
|
|
|
${ports.wireguard},
|
|
|
|
} counter accept comment "limited UDP"
|
|
|
|
|
|
|
|
counter drop
|
|
|
|
}
|
|
|
|
|
|
|
|
chain forward_untrusted_lan_wan {
|
|
|
|
# Forward only necessary internet services.
|
|
|
|
tcp dport {
|
|
|
|
${ports.http},
|
|
|
|
${ports.https},
|
|
|
|
} counter accept comment "untrusted TCP HTTP(S)"
|
|
|
|
|
|
|
|
counter drop
|
|
|
|
}
|
|
|
|
|
2020-02-04 21:46:49 +00:00
|
|
|
chain forward_limited_untrusted_lan_trusted_lan {
|
|
|
|
# Only allow established connections from trusted LANs.
|
|
|
|
ct state {established, related} counter accept
|
|
|
|
ct state invalid counter drop
|
|
|
|
|
|
|
|
counter drop
|
|
|
|
}
|
|
|
|
|
2020-01-04 22:16:31 +00:00
|
|
|
chain forward_wan_trusted_lan {
|
|
|
|
ct state {established, related} counter accept
|
|
|
|
ct state invalid counter drop
|
|
|
|
|
|
|
|
# SSH for internal machines.
|
|
|
|
ip6 daddr {
|
2020-01-24 16:36:50 +00:00
|
|
|
${
|
|
|
|
lib.concatMapStrings (host: "${host.ipv6.gua}, ")
|
|
|
|
vars.hosts.servers
|
|
|
|
}
|
2020-01-04 22:16:31 +00:00
|
|
|
} tcp dport ${ports.ssh} counter accept comment "IPv6 SSH"
|
|
|
|
|
2020-01-24 16:16:23 +00:00
|
|
|
# Plex running on server.
|
|
|
|
ip daddr ${vars.server_ipv4} tcp dport ${ports.plex} counter accept comment "server IPv4 Plex"
|
|
|
|
ip6 daddr ${vars.server_ipv6} tcp dport ${ports.plex} counter accept comment "server IPv6 Plex"
|
|
|
|
|
2020-06-11 18:05:57 +00:00
|
|
|
# Remote site access to UniFi controller running on server.
|
|
|
|
#
|
|
|
|
# TODO: template these values out.
|
|
|
|
ip saddr 98.209.181.132 tcp dport {${ports.unifi_device}, ${ports.unifi_web}} counter accept comment "server TCPv4 UniFi"
|
|
|
|
ip saddr 98.209.181.132 udp dport ${ports.unifi_stun} counter accept comment "server UDPv4 UniFi"
|
|
|
|
|
|
|
|
ip6 saddr {
|
|
|
|
2001:558:6007:72:9ee:4b74:e017:875b/128,
|
|
|
|
2601:405:8500:f600::/64,
|
|
|
|
} ip6 daddr ${vars.server_ipv6} tcp dport {${ports.unifi_device}, ${ports.unifi_web}} counter accept comment "server TCPv6 UniFi"
|
|
|
|
|
|
|
|
ip6 saddr {
|
|
|
|
2001:558:6007:72:9ee:4b74:e017:875b/128,
|
|
|
|
2601:405:8500:f600::/64,
|
|
|
|
} ip6 daddr ${vars.server_ipv6} udp dport ${ports.unifi_stun} counter accept comment "server UDPv6 UniFi"
|
|
|
|
|
2020-06-06 15:29:32 +00:00
|
|
|
# Streaming RTP6 on desktop.
|
|
|
|
ip6 daddr ${vars.desktop_ipv6} udp dport 5000-5007 counter accept comment "desktop IPv6 RTP"
|
|
|
|
|
2020-01-04 22:16:31 +00:00
|
|
|
counter reject
|
|
|
|
}
|
|
|
|
|
2020-01-24 22:42:21 +00:00
|
|
|
chain forward_wan_limited_untrusted_lan {
|
2020-01-04 22:16:31 +00:00
|
|
|
ct state {established, related} counter accept
|
|
|
|
ct state invalid counter drop
|
|
|
|
|
|
|
|
counter reject
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
table ip nat {
|
|
|
|
chain prerouting {
|
|
|
|
type nat hook prerouting priority 0
|
|
|
|
|
2020-01-24 22:42:21 +00:00
|
|
|
iifname ${wan0} jump prerouting_wan0
|
2020-01-04 22:16:31 +00:00
|
|
|
accept
|
|
|
|
}
|
|
|
|
|
|
|
|
chain prerouting_wan0 {
|
|
|
|
tcp dport {
|
|
|
|
${ports.plex},
|
2020-06-11 18:05:57 +00:00
|
|
|
${ports.unifi_device},
|
|
|
|
${ports.unifi_web},
|
2020-01-24 16:16:23 +00:00
|
|
|
} dnat ${vars.server_ipv4} comment "server TCPv4 DNAT"
|
2020-01-04 22:16:31 +00:00
|
|
|
|
2020-06-11 18:05:57 +00:00
|
|
|
udp dport {
|
|
|
|
${ports.unifi_stun},
|
|
|
|
} dnat ${vars.server_ipv4} comment "server UDPv4 DNAT"
|
|
|
|
|
2020-01-04 22:16:31 +00:00
|
|
|
udp dport {
|
|
|
|
${ports.dns},
|
|
|
|
} redirect to ${ports.wireguard} comment "router IPv4 WireGuard DNAT"
|
|
|
|
|
|
|
|
accept
|
|
|
|
}
|
|
|
|
|
|
|
|
chain postrouting {
|
|
|
|
type nat hook postrouting priority 0
|
2020-01-24 22:42:21 +00:00
|
|
|
oifname ${wan0} masquerade
|
2020-01-04 22:16:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
table ip6 nat {
|
|
|
|
chain prerouting {
|
|
|
|
type nat hook prerouting priority 0
|
|
|
|
|
2020-01-24 22:42:21 +00:00
|
|
|
iifname ${wan0} udp dport {
|
2020-01-04 22:16:31 +00:00
|
|
|
${ports.dns},
|
|
|
|
} redirect to ${ports.wireguard} comment "router IPv6 WireGuard DNAT"
|
|
|
|
|
|
|
|
accept
|
|
|
|
}
|
|
|
|
}
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
}
|