#!/bin/sh
# Blackbox: the unlock prompt answers on IPv6 too.
#
# configure_networking (klibc ipconfig, started by the dropbear script) only
# knows the kernel's ip= parameter, which is IPv4. The measured command line
# already carries this machine's IPv6 address and gateway (bb.ip6, bb.gw6) for
# the booted system (bb-seed); the same values go on ens18 here, so a customer
# on an IPv6-only network can reach the prompt as well.
#
# The gateway is off-link from the /64 (2602:f41c:100::1 serves every
# Amsterdam /64), so it gets a host route first; BusyBox ip has no "onlink".
# Nothing to undo at the hand-off: init-bottom/dropbear and bb-seed take the
# link down, which drops IPv6 addresses and routes, and a root that uses
# netplan takes over what is there (systemd-networkd), as it does for IPv4.
PREREQ="dropbear"
prereqs() { echo "$PREREQ"; }
case "$1" in prereqs) prereqs; exit 0 ;; esac

IP6=""; GW6=""
for x in $(cat /proc/cmdline); do
    case "$x" in
        bb.ip6=*) IP6="${x#bb.ip6=}" ;;
        bb.gw6=*) GW6="${x#bb.gw6=}" ;;
    esac
done
[ -n "$IP6" ] || exit 0

i=0
while [ ! -e /sys/class/net/ens18 ] && [ $i -lt 30 ]; do sleep 1; i=$((i + 1)); done
[ -e /sys/class/net/ens18 ] || exit 0
ip link set dev ens18 up 2>/dev/null || true
ip -6 addr add "$IP6" dev ens18 2>/dev/null || true
if [ -n "$GW6" ]; then
    ip -6 route add "$GW6" dev ens18 2>/dev/null || true
    ip -6 route add default via "$GW6" dev ens18 2>/dev/null || true
fi
exit 0
