#!/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 - < /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