#!/bin/sh
#
# wlan-address IFACE -- give a WLAN interface without an address of its own
# a stable one.  The WCN399x of hotdogb has none ("ath10k_snoc: invalid MAC
# address; choosing random"), so every boot showed up at the router as a new
# device; after a day of test boots its DHCP pool had no address left for
# the phone (2026-11-08: associated, a static address pinged the router,
# DHCP got no answer).
#
# As bt-address: the first six bytes of sha256("wlan" serialno), unicast
# and locally administered.  NetworkManager keeps it
# (conf.d/makronix-wifi-mac.conf).  Run by udev when the interface appears
# (61-makronix-wlan-address.rules).

ifc=${1:-wlan0}
serial=$(tr ' ' '\n' < /proc/cmdline | sed -n 's/^androidboot.serialno=//p' | head -n 1)
[ -n "$serial" ] || { echo "wlan-address: no androidboot.serialno" >&2; exit 1; }

h=$(printf 'wlan%s' "$serial" | sha256sum | cut -c1-12)
b0=$(( (0x$(echo "$h" | cut -c1-2) & 0xfc) | 0x02 ))
addr=$(printf '%02x' "$b0"; echo "$h" | cut -c3-12 | sed 's/../:&/g')

ip link set dev "$ifc" down 2>/dev/null
ip link set dev "$ifc" address "$addr" &&
	echo "wlan-address: $ifc $addr" > /dev/kmsg
