Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,27 @@
mode. A checksum that can't be written (its `chown` or `chmod` failing) is said in the
keeper's log and in the answer to adding or forgetting a network, instead of home Wi-Fi only
staying "searching".
- **Mods' connections over home Wi-Fi are no longer refused** (Ioniq 5 US 260507). The block
that keeps mods off mobile data was in `filter OUTPUT`, where `-o` is the route a packet had
before HMG-API's mark moved it to home Wi-Fi: every connection that started on the main
table (mobile data) was refused. With home Wi-Fi on, the block now runs after the move
(`mangle POSTROUTING`, `HMGAPI_HOMECELL`: nothing marked for home Wi-Fi leaves on `rmnet`, nor
the home group's or the home apps' traffic by owner, so the block holds if the mark rule is lost),
and table 95 holds an unreachable default while home Wi-Fi is down, so marked traffic never
falls through to mobile data. IPv6, and everything with home Wi-Fi off, stays refused as before.
The keeper builds the home Wi-Fi rules (the post-reroute block, the mark, masquerade, table 95
and the fwmark rule) before the IPv4 home REJECTs leave, and fills the cellular chain before
OUTPUT jumps to it; its check also looks for the DROPs inside `HMGAPI_HOMECELL`. (Repairing
the rules still takes them out for a moment, as before.) Home Wi-Fi off
and the uninstaller take table 95's unreachable default out.
- **Mods' services can look names up on home Wi-Fi** (Ioniq 5 US 260507). Their resolver
(`ANDROID_DNS_MODE=local`) has no servers in the service's process and asks 127.0.0.1;
HMG-API's redirect to home Wi-Fi's DNS server kept that source address, so no query left the
unit. While home Wi-Fi is up, `route_localnet` is on for `wlan0` (the queries are
masqueraded) and, for 127.x on `wlan0`, only the DNS server's answers (its address, port 53)
get in (`HMGAPI_HOMELO`). The guard is checked on every pass, not only when the lease changes,
and put back first in INPUT after the stock firewall reloads; `route_localnet` is on only
while it's there.

## 1.1.0 (2026-11-28)

Expand Down
95 changes: 82 additions & 13 deletions core/runtime/homewifi-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ d=$FW/$v/$t; mkdir -p "$d"
stock() { case $1 in INPUT|OUTPUT|FORWARD|PREROUTING|POSTROUTING) return 0 ;; esac; return 1; }
op=$1 c=$2; shift 2
f=$d/$c
case $op in -A|-I|-D|-F|-N|-X) echo "ipt$v $t $op $c $*" >> "$FW/order.log" ;; esac
[ "$op $c" = "-F HMGAPI_HOMELO" ] && [ -n "$LOCALNET_FILE" ] && { cat "$LOCALNET_FILE" 2>/dev/null || echo none; } >> "$FW/lo-at-flush"
if ! stock "$c" && [ "$op" != -N ] && [ ! -f "$f" ]; then echo "iptables: No chain/target/match by that name." >&2; exit 1; fi
case $op in
-S) if stock "$c"; then echo "-P $c ACCEPT"; else echo "-N $c"; fi
Expand All @@ -108,17 +110,30 @@ cat > "$F/bin/ip" <<'S'
v=4; case $1 in -4) shift ;; -6) v=6; shift ;; esac
o=$1 c=$2; shift 2
f=$FW/ip$v.$o; touch "$f"
rkey() { # "<table> <destination> <metric>" of a route line
set -- $1; case $1 in unicast|unreachable|blackhole|prohibit|throw|local) shift ;; esac
dst=$1; [ "$dst" = default ] && dst=0.0.0.0/0; case $dst in */*) ;; *) dst=$dst/32 ;; esac
m=0 tb=main; while [ $# -gt 0 ]; do case $1 in metric|priority) m=$2 ;; table) tb=$2 ;; esac; shift; done
echo "$tb $dst $m"
}
case $o.$c in
route.show) if [ "$1" = dev ]; then grep -- " dev $2 .*table main\$" "$f" | sed 's/ table main$//' || true; else grep -- " table ${3:-main}\$" "$f" || true; fi ;;
route.replace) n=$#; if [ "${@:$((n-1)):1}" = table ]; then tb=${!n}; set -- "${@:1:$((n-2))}"; else tb=main; fi
grep -v -- "^$1 .*table $tb\$" "$f" > "$f.new"; mv "$f.new" "$f"; echo "$* table $tb" >> "$f" ;;
route.show) if [ "$1" = dev ]; then grep -- " dev $2 .*table main\$" "$f" | sed 's/ table main$//' || true
elif [ "$1" = table ]; then grep -- " table $2\$" "$f" | sed "s/ table $2\$//" || true; else grep -- " table main\$" "$f" || true; fi ;;
route.replace|route.add) n=$#; if [ "${@:$((n-1)):1}" = table ]; then tb=${!n}; set -- "${@:1:$((n-2))}"; else tb=main; fi
# as the kernel: a route is its table, destination and metric; its type (unreachable, ...) and
# the rest don't count. replace swaps the one with that key; add fails if there is one
k=$(rkey "$* table $tb")
while IFS= read -r l; do [ "$(rkey "$l")" = "$k" ] && found=1 || echo "$l"; done < "$f" > "$f.new"
if [ $c = add ] && [ -n "$found" ]; then rm "$f.new"; echo "RTNETLINK answers: File exists" >&2; exit 2; fi
echo "ip $c $* table $tb" >> "$FW/order.log"
mv "$f.new" "$f"; echo "$* table $tb" >> "$f" ;;
route.del) grep -q -e "^$1 .*dev $3 .*table main\$" -e "^$1 dev $3 .*table main\$" "$f" || exit 2
awk -v r="$1" -v d="$3" '!x && $1 == r && index($0, " dev " d " ") { x = 1; next } { print }' "$f" > "$f.new"; mv "$f.new" "$f" ;;
route.flush) grep -v -- " table $2\$" "$f" > "$f.new"; mv "$f.new" "$f" ;;
route.get) [ "$2" = oif ] && grep -q "^10\.1\." <<< "$1" && echo "$1 dev $3" ;;
addr.flush) : ;;
rule.show) echo "0: from all lookup local"; cat "$f" ;;
rule.add) p=$2; shift 2; printf '%s:\t%s\n' "$p" "$*" >> "$f" ;;
rule.add) p=$2; shift 2; printf '%s:\t%s\n' "$p" "$*" >> "$f"; echo "ip rule add $p $*" >> "$FW/order.log" ;;
rule.del) grep -q "^$2:" "$f" || exit 2; grep -v "^$2:" "$f" > "$f.new"; mv "$f.new" "$f" ;;
esac
S
Expand All @@ -144,42 +159,61 @@ ipt() { PATH=$F/bin:$PATH iptables "$@"; }
printf 'com.example.dl 10071 0 /data/data/com.example.dl default 3003\ncom.example.quiet 10072 0 /data/data/com.example.quiet default 3003\n' > "$F/packages.list"
printf 'com.example.dl dl\ncom.example.quiet quiet\n' > "$D/state/apps.txt"

: > "$F/fw/order.log"
log=$(keeper once)
# the order it's built in: no moment with the IPv4 home REJECTs gone and nothing else in their place
at() { grep -n -m1 -F -- "$1" "$F/fw/order.log" | cut -d: -f1; }
cell=$(at "ipt4 filter -I OUTPUT 1 -j HMGAPI_CELL")
check "built first: the post-reroute block, the mark, masquerade, table 95, the fwmark rule" yes "$(for l in "ipt4 mangle -A HMGAPI_HOMECELL -o rmnet+" "ipt4 mangle -I POSTROUTING 1 -j HMGAPI_HOMECELL" "ipt4 mangle -I OUTPUT 1 -j HMGAPI_HOME" "ipt4 nat -I POSTROUTING 1 -j HMGAPI_HOMEOUT" "ip add unreachable default table 95" "ip rule add 900 fwmark"; do a=$(at "$l"); [ -n "$a" ] && [ "$a" -lt "$cell" ] || { echo "no: $l"; exit; }; done; echo yes)"
check "... the cellular chain filled before OUTPUT jumps to it" yes "$([ "$(at "ipt4 filter -A HMGAPI_CELL -o rmnet+ -m owner --uid-owner 10072")" -lt "$cell" ] && echo yes || echo no)"
check "... marks before the jump to them" yes "$([ "$(at "ipt4 mangle -A HMGAPI_HOME -m owner --uid-owner 10071")" -lt "$(at "ipt4 mangle -I OUTPUT 1 -j HMGAPI_HOME")" ] && echo yes || echo no)"
has "mods kept off cellular" "mods kept off cellular: 2 app(s) and every mod service, home Wi-Fi routing in" "$log"
check "the cellular chain first in OUTPUT" "-A OUTPUT -j HMGAPI_CELL" "$(ipt -S OUTPUT | grep -m1 '^-A')"
check "its rules: both services' groups and both apps' uids" "-o rmnet+ -m owner --gid-owner 2950 -j REJECT
-o rmnet+ -m owner --gid-owner 2951 -j REJECT
-o rmnet+ -m owner --uid-owner 10071 -j REJECT
check "its IPv4 rules: the plain group and the app that didn't ask (home ones: after the reroute)" "-o rmnet+ -m owner --gid-owner 2950 -j REJECT
-o rmnet+ -m owner --uid-owner 10072 -j REJECT" "$(cat "$F/fw/4/filter/HMGAPI_CELL")"
check "IPv6 too" 4 "$(grep -c REJECT "$F/fw/6/filter/HMGAPI_CELL")"
check "IPv6: all four (never routed to home Wi-Fi)" 4 "$(grep -c REJECT "$F/fw/6/filter/HMGAPI_CELL")"
check "home Wi-Fi traffic never out on mobile data, checked after the reroute: by the mark, and by owner without it" "-o rmnet+ -m mark --mark 0x2951 -j DROP
-o rmnet+ -m owner --gid-owner 2951 -j DROP
-o rmnet+ -m owner --uid-owner 10071 -j DROP" "$(cat "$F/fw/4/mangle/HMGAPI_HOMECELL")"
check "... first in POSTROUTING" "-j HMGAPI_HOMECELL" "$(head -1 "$F/fw/4/mangle/POSTROUTING")"
check "only the mod that asked marked for home Wi-Fi (and the home group)" "-m owner --gid-owner 2951 -j MARK --set-mark 0x2951
-m owner --uid-owner 10071 -j MARK --set-mark 0x2951" "$(cat "$F/fw/4/mangle/HMGAPI_HOME")"
check "masqueraded on wlan0 (home Wi-Fi: the car's own client)" "-o wlan0 -m mark --mark 0x2951 -j MASQUERADE" "$(cat "$F/fw/4/nat/HMGAPI_HOMEOUT")"
has "marked traffic to table 95" "900: fwmark 0x2951 table 95" "$(cat "$F/fw/ip4.rule")"
check "both mods' apps look names up through wlan0, never cellular DNS" "resolver setifaceforuidrange wlan0 10071 10071
resolver setifaceforuidrange wlan0 10072 10072" "$(cat "$F/fw/ndc.log")"
check "all there: a check is quiet" "" "$(keeper once)"
ipt -t mangle -F HMGAPI_HOMECELL # the jump left, the DROP in it gone
has "the post-reroute DROP gone (its jump still there): put back" "mods' cellular block put back" "$(keeper once)"
check "... the DROPs again" 3 "$(grep -c DROP "$F/fw/4/mangle/HMGAPI_HOMECELL")"
ipt -t mangle -D HMGAPI_HOMECELL -o rmnet+ -m owner --uid-owner 10071 -j DROP # one owner DROP gone
has "an owner DROP gone: put back" "mods' cellular block put back" "$(keeper once)"
has "... it's there again" "--uid-owner 10071 -j DROP" "$(cat "$F/fw/4/mangle/HMGAPI_HOMECELL")"
ipt -I OUTPUT 1 -o rmnet+ -j ACCEPT # the stock firewall puts an accept first
has "moved: put back first" "mods' cellular block put back" "$(keeper once)"
check "... first again" "-A OUTPUT -j HMGAPI_CELL" "$(ipt -S OUTPUT | grep -m1 '^-A')"
printf 'com.example.dl 10091 0 /data/data/com.example.dl default 3003\ncom.example.quiet 10072 0 /data/data/com.example.quiet default 3003\n' > "$F/packages.list"
keeper once >/dev/null
has "a reinstalled app's new uid" "--uid-owner 10091 -j REJECT" "$(cat "$F/fw/4/filter/HMGAPI_CELL")"
hasnt "... the old one gone" "10071" "$(cat "$F/fw/4/filter/HMGAPI_CELL")"
has "a reinstalled app's new uid" "--uid-owner 10091 -j REJECT" "$(cat "$F/fw/6/filter/HMGAPI_CELL")"
has "... marked with it" "--uid-owner 10091 -j MARK" "$(cat "$F/fw/4/mangle/HMGAPI_HOME")"
hasnt "... the old one gone" "10071" "$(cat "$F/fw/6/filter/HMGAPI_CELL" "$F/fw/4/mangle/HMGAPI_HOME")"
rm "$D/settings/allow/home_wifi"
eng publish-state
keeper once >/dev/null
check "home Wi-Fi off: no marks" no "$([ -f "$F/fw/4/mangle/HMGAPI_HOME" ] && echo yes || echo no)"
hasnt "... no routing rule" "900:" "$(cat "$F/fw/ip4.rule")"
check "... still off cellular" 4 "$(grep -c REJECT "$F/fw/4/filter/HMGAPI_CELL")"
check "... still off cellular: all four refused in OUTPUT again" 4 "$(grep -c REJECT "$F/fw/4/filter/HMGAPI_CELL")"
check "... no post-reroute block (nothing is marked)" no "$([ -f "$F/fw/4/mangle/HMGAPI_HOMECELL" ] && echo yes || echo no)"
FW=$F/fw PATH=$F/bin:$PATH ip route replace unreachable default table 95
keeper down >/dev/null
hasnt "down: the cellular chain gone" "HMGAPI_CELL" "$(ipt -S OUTPUT)"
check "... table 95 empty (the uninstaller; wifi.sh's, in case it couldn't)" "" "$(grep 'table 95' "$F/fw/ip4.route" || true)"
: > "$D/settings/allow/home_wifi"
eng publish-state

# ---- the home Wi-Fi keeper, against fake tools
X=$T/unit
mkdir -p "$X/data/misc/wifi/sockets" "$X/data/hmgapi/public" "$X/proc/sys/kernel/random"
mkdir -p "$X/data/misc/wifi/sockets" "$X/data/hmgapi/public" "$X/proc/sys/kernel/random" "$X/proc/sys/net/ipv4/conf/wlan0"
echo boot-1 > "$X/proc/sys/kernel/random/boot_id"
echo "1000.00 2000.00" > "$X/proc/uptime"
cat > "$F/bin/hmgapid" <<'S'
Expand Down Expand Up @@ -327,6 +361,38 @@ has "wlan0's DNS servers for the mods" "resolver setifdns wlan0 10.10.10.100" "
has "a DNS server off the subnet: through the gateway" "10.10.10.100/32 via 10.4.0.1 dev wlan0 metric 1000" "$(cat "$F/fw/ip4.route")"
check "mods' services' DNS to the home router's" "-m owner --gid-owner 2951 -p udp --dport 53 -j DNAT --to-destination 10.10.10.100
-m owner --gid-owner 2951 -p tcp --dport 53 -j DNAT --to-destination 10.10.10.100" "$(cat "$F/fw/4/nat/HMGAPI_HOMEDNS")"
check "services' lookups (their resolver asks 127.0.0.1) may come back in through wlan0: the DNS server's answers only" "-i wlan0 -s 10.10.10.100/32 -d 127.0.0.0/8 -p udp --sport 53 -m state --state ESTABLISHED -j RETURN
-i wlan0 -s 10.10.10.100/32 -d 127.0.0.0/8 -p tcp --sport 53 -m state --state ESTABLISHED -j RETURN
-i wlan0 -d 127.0.0.0/8 -j DROP" "$(cat "$F/fw/4/filter/HMGAPI_HOMELO")"
check "... first in INPUT" "-j HMGAPI_HOMELO" "$(head -1 "$F/fw/4/filter/INPUT")"
check "... route_localnet on, behind it" 1 "$(cat "$X/proc/sys/net/ipv4/conf/wlan0/route_localnet")"
check "table 95: Wi-Fi's default replaced the unreachable one (one default, as the kernel keeps)" "default via 10.4.0.1 dev wlan0" "$(FW=$F/fw PATH=$F/bin:$PATH ip route show table 95 | grep default)"
# the stock firewall restarts (iptrules: INPUT flushed, its whitelist jump back), the lease the same
: > "$F/fw/4/filter/INPUT"; ipt -N WLIST_IN 2>/dev/null || true; ipt -A INPUT -j WLIST_IN
log=$(wk)
has "the stock firewall reloaded, the lease unchanged: the guard put back" "home Wi-Fi's 127.x guard put back" "$log"
check "... first in INPUT again" "-j HMGAPI_HOMELO" "$(head -1 "$F/fw/4/filter/INPUT")"
check "... the stock jump kept, after it" "-j WLIST_IN" "$(sed -n 2p "$F/fw/4/filter/INPUT")"
check "... route_localnet still on" 1 "$(cat "$X/proc/sys/net/ipv4/conf/wlan0/route_localnet")"
ipt -I INPUT 1 -j WLIST_IN # the stock jump put first, ours still there
wk >/dev/null
check "not first any more: moved back first" "-j HMGAPI_HOMELO" "$(head -1 "$F/fw/4/filter/INPUT")"
check "... once" 1 "$(grep -c HMGAPI_HOMELO "$F/fw/4/filter/INPUT")"
check "all in place: the next pass is quiet" "" "$(wk)"
export LOCALNET_FILE=$X/proc/sys/net/ipv4/conf/wlan0/route_localnet; : > "$F/fw/lo-at-flush"
was=$(cat "$F/fw/props/dhcp.wlan0.dns1"); printf 10.10.10.101 > "$F/fw/props/dhcp.wlan0.dns1"
wk >/dev/null
check "a new DNS server: route_localnet off before the old guard is flushed (each flush)" "0 0" "$(echo $(cat "$F/fw/lo-at-flush"))"
check "... the guard for the new server, and route_localnet on behind it" "1 -s 10.10.10.101/32" "$(cat "$X/proc/sys/net/ipv4/conf/wlan0/route_localnet") $(grep -o -m1 -- '-s 10.10.10.101/32' "$F/fw/4/filter/HMGAPI_HOMELO")"
printf '%s' "$was" > "$F/fw/props/dhcp.wlan0.dns1"; wk >/dev/null; unset LOCALNET_FILE
ipt -F INPUT; ipt -N HMGAPI_BROKEN 2>/dev/null; printf '#!/bin/bash\ncase "$*" in *"-I INPUT 1 -j HMGAPI_HOMELO"*) exit 1 ;; esac\nexec "$(dirname "$0")/iptables.real" "$@"\n' > "$F/bin/iptables.wrap"
mv "$F/bin/iptables" "$F/bin/iptables.real"; mv "$F/bin/iptables.wrap" "$F/bin/iptables"; chmod +x "$F/bin/iptables"
log=$(wk)
has "the guard can't go in: said" "home Wi-Fi's 127.x guard can't be put in" "$log"
check "... route_localnet off" 0 "$(cat "$X/proc/sys/net/ipv4/conf/wlan0/route_localnet")"
mv "$F/bin/iptables.real" "$F/bin/iptables"
wk >/dev/null
check "... and on once it can" 1 "$(cat "$X/proc/sys/net/ipv4/conf/wlan0/route_localnet")"
has "what it saw, for the app" "2437 -61 [WPA2-PSK-CCMP][ESS] Willows" "$(verb seen)"
hasnt "the key never in the log" "secret" "$log"
FW=$F/fw PATH=$F/bin:$PATH ip route replace default via 10.4.0.1 dev wlan0 # Android puts it back (a renewal)
Expand All @@ -341,7 +407,9 @@ has "the car on: back" "back to the car's Wi-Fi (the car is on): the car's Wi-Fi
check "... the projection hotspot, in its mode" 2 "$(cat "$F/fw/ap")"
check "... client off, then the hotspot" "client off
start 2" "$(grep -o 'client off\|start [12]' "$F/fw/apps.log")"
check "... table 95 empty" "" "$(grep 'table 95' "$F/fw/ip4.route" || true)"
check "... table 95: unreachable, never the main table's mobile data" "unreachable default table 95" "$(grep 'table 95' "$F/fw/ip4.route" || true)"
check "... route_localnet off again" 0 "$(cat "$X/proc/sys/net/ipv4/conf/wlan0/route_localnet")"
check "... the 127.x guard gone" no "$([ -f "$F/fw/4/filter/HMGAPI_HOMELO" ] && echo yes || echo no)"
check "... waiting again" waiting "$(state)"

# the car off without parked (the screen goes dark with it): 30 s, then home Wi-Fi
Expand Down Expand Up @@ -420,6 +488,7 @@ wk >/dev/null
verb down >/dev/null
check "down while parked: the hotspot back" 2 "$(cat "$F/fw/ap")"
check "... the client off" no "$([ -f "$F/fw/client" ] && echo yes || echo no)"
check "... table 95's unreachable default out too (home Wi-Fi off, the uninstaller)" "" "$(grep 'table 95' "$F/fw/ip4.route" || true)"

# ---- the projection AP ("wifi": {"projection": true}): the engine
rm -f "$D/settings/allow/home_wifi" "$D/settings/hmgapi.json"
Expand Down
Loading
Loading