#!/bin/sh

# When changing default IPv4/6 settings, this does not apply to
# existing interfaces. This syncs the default settings to all known
# interfaces at boot, such that physical interfaces will start from
# the same point as virtual ones.
tmp=$(mktemp)
out=$(mktemp)

# Ignore unreadable entries, like net.ipv6.conf.default.stable_secret
sysctl net.ipv4.conf.default  >"$tmp" 2>/dev/null
sysctl net.ipv6.conf.default >>"$tmp" 2>/dev/null

# Build a single sysctl input with settings for all interfaces
for dir in /sys/class/net/*/; do
    iface=${dir%/}
    iface=${iface##*/}
    sed "s/.default./.${iface}./g" "$tmp" >> "$out"
done

# Apply all at once, suppress errors from read-only entries
# (e.g., net.ipv4.conf.*.mc_forwarding)
sysctl -q -p - < "$out" 2>/dev/null

rm "$tmp" "$out"
