63 lines
3.9 KiB
Bash
Executable File
63 lines
3.9 KiB
Bash
Executable File
#!/bin/sh
|
|
# Called only by mars-pcapd. Own TAP, own nft table. Never change host forwarding.
|
|
set -eu
|
|
PATH=/usr/sbin:/usr/bin:/sbin:/bin
|
|
export PATH
|
|
[ "$(id -u)" = 0 ] || exit 1
|
|
case "$1" in
|
|
down) nft delete table inet mars_ipxpkt; exit;;
|
|
up) ;;
|
|
*) exit 2;;
|
|
esac
|
|
uplink=$2; gateway=$3; dns=$4; mac=$5; web=$6; ping=$7
|
|
# Defense in depth; caller already validates full address ranges and interface names.
|
|
case "$uplink" in ''|-*|*[!a-zA-Z0-9_.-]*|lo|marsipx0) exit 2;; esac
|
|
case "$gateway:$dns" in *[!0-9.:]*) exit 2;; esac
|
|
case "$mac" in *[!0-9a-f:]*) exit 2;; esac
|
|
[ "$web" = 0 ] || [ "$web" = 1 ] || exit 2
|
|
[ "$ping" = 0 ] || [ "$ping" = 1 ] || exit 2
|
|
[ "$(cat /proc/sys/net/ipv4/ip_forward)" = 1 ] || { echo 'IPXPKT: Enable IPv4 forwarding deliberately before use' >&2; exit 1; }
|
|
# Refuse existing containing or more-specific routes in any table (except default).
|
|
subnet=${gateway%.*}.0/24
|
|
cover=$(ip -4 route show table all match "$subnet") || exit 1
|
|
inside=$(ip -4 route show table all root "$subnet") || exit 1
|
|
if [ -n "$inside" ] || printf '%s\n' "$cover" | grep -Ev '^(default([[:space:]]|$)|[[:space:]]*$)' >/dev/null; then
|
|
echo "IPXPKT: virtual network overlaps an existing route: $subnet" >&2; exit 1
|
|
fi
|
|
# Never replace an existing table (including stale table after a crash).
|
|
if nft list table inet mars_ipxpkt >/dev/null 2>&1; then echo 'IPXPKT: nft table mars_ipxpkt already exists; inspect before removing' >&2; exit 1; fi
|
|
|
|
# Forwards only IPv4 Internet traffic on the selected uplink; no access to local host.
|
|
# IPv4 non-public ranges blocked. Sites with internally routed public space must add restrictions.
|
|
web_rule=''; ping_rule=''
|
|
[ "$web" = 0 ] || web_rule='iifname "marsipx0" tcp dport { 80, 443 } ct state new,established accept'
|
|
[ "$ping" = 0 ] || ping_rule='iifname "marsipx0" icmp type echo-request ct state new,established accept'
|
|
nft -f - <<RULES
|
|
create table inet mars_ipxpkt
|
|
add chain inet mars_ipxpkt input { type filter hook input priority -10; policy accept; }
|
|
add rule inet mars_ipxpkt input iifname "marsipx0" drop
|
|
add chain inet mars_ipxpkt forward { type filter hook forward priority -10; policy accept; }
|
|
add rule inet mars_ipxpkt forward iifname "marsipx0" meta nfproto != ipv4 drop
|
|
add rule inet mars_ipxpkt forward iifname "marsipx0" oifname != "$uplink" drop
|
|
add rule inet mars_ipxpkt forward iifname "marsipx0" ip saddr != $subnet drop
|
|
add rule inet mars_ipxpkt forward iifname "marsipx0" ip daddr { 0.0.0.0/8, 10.0.0.0/8, 100.64.0.0/10, 127.0.0.0/8, 169.254.0.0/16, 172.16.0.0/12, 192.0.0.0/24, 192.0.2.0/24, 192.168.0.0/16, 198.18.0.0/15, 198.51.100.0/24, 203.0.113.0/24, 224.0.0.0/3 } drop
|
|
add rule inet mars_ipxpkt forward iifname "marsipx0" ct state invalid drop
|
|
add rule inet mars_ipxpkt forward iifname "marsipx0" ip daddr $dns udp dport 53 ct state new,established accept
|
|
add rule inet mars_ipxpkt forward iifname "marsipx0" ip daddr $dns tcp dport 53 ct state new,established accept
|
|
${web_rule:+add rule inet mars_ipxpkt forward $web_rule}
|
|
${ping_rule:+add rule inet mars_ipxpkt forward $ping_rule}
|
|
add rule inet mars_ipxpkt forward iifname "marsipx0" drop
|
|
add rule inet mars_ipxpkt forward oifname "marsipx0" iifname "$uplink" ip daddr $subnet ct state established,related accept
|
|
add rule inet mars_ipxpkt forward oifname "marsipx0" drop
|
|
add chain inet mars_ipxpkt nat { type nat hook postrouting priority srcnat; policy accept; }
|
|
add rule inet mars_ipxpkt nat iifname "marsipx0" oifname "$uplink" ip saddr $subnet masquerade
|
|
RULES
|
|
# Roll back only our freshly created table on errors, keep TAP down until rules exist.
|
|
trap 'nft delete table inet mars_ipxpkt || true' EXIT
|
|
# IPv6 traffic is not accepted by the decoder; avoid link-local address creation as well.
|
|
ip link set dev marsipx0 address "$mac" mtu 1500
|
|
if [ -e /proc/sys/net/ipv6/conf/marsipx0/disable_ipv6 ]; then echo 1 > /proc/sys/net/ipv6/conf/marsipx0/disable_ipv6; fi
|
|
ip -4 addr add "$gateway/24" dev marsipx0
|
|
ip link set dev marsipx0 up
|
|
trap - EXIT
|