From 4df02ccc4835566c7c796d6a133ed328c5a69960 Mon Sep 17 00:00:00 2001 From: anthonyanderson Date: Thu, 26 Nov 2026 10:29:39 -0500 Subject: [PATCH 1/4] Home Wi-Fi: mods' connections and their services' lookups get through Both found on an Ioniq 5 US 260507 (rc.10) with a mod that downloads over home Wi-Fi. - Connections were refused. The mods' cellular block sat in filter OUTPUT, where -o is the route a packet had before HMG-API's mark (mangle OUTPUT) moved it to table 95: a connection whose first route was the main table's (mobile data) was rejected there, though it would have left on wlan0. With home Wi-Fi on, the IPv4 block for the home group and the mods that asked runs after the reroute instead (mangle POSTROUTING, HMGAPI_HOMECELL: nothing marked leaves on rmnet), and table 95 is never empty: an unreachable default while home Wi-Fi is down, so marked traffic fails rather than falling through to mobile data. IPv6, and everything with home Wi-Fi off, is refused in OUTPUT as before. - A service's resolver (ANDROID_DNS_MODE=local) has no servers in its process and asks 127.0.0.1:53. The DNAT to home Wi-Fi's server kept 127.0.0.1 as the source, which the kernel won't route out of wlan0. While home Wi-Fi is up: route_localnet on wlan0 (the queries are masqueraded by HMGAPI_HOMEOUT), and HMGAPI_HOMELO lets only answers come back in for 127.x. Co-Authored-By: Claude Opus 5.5 --- CHANGELOG.md | 12 ++++++++ core/runtime/homewifi-test.sh | 30 ++++++++++++------- core/runtime/java/hmgapi/engine/HomeWifi.java | 15 ++++++++++ core/runtime/java/hmgapi/engine/Network.java | 26 ++++++++++++---- docs/mod-reference.md | 7 +++++ 5 files changed, 74 insertions(+), 16 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5f62794b..6fd5a1b4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,18 @@ forgetting a network now updates it, and it's checked before each switch to home Wi-Fi. A first network no longer needs `printf` (not on the unit): the config starts from the car's own template. +- **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`), + 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. +- **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 only the answers may come back in for 127.x (`HMGAPI_HOMELO`). ## 1.1.0-rc.10 (2026-11-25) diff --git a/core/runtime/homewifi-test.sh b/core/runtime/homewifi-test.sh index e36e521d..940f8929 100644 --- a/core/runtime/homewifi-test.sh +++ b/core/runtime/homewifi-test.sh @@ -105,9 +105,12 @@ 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" 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.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) 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.add) n=$#; if [ "${@:$((n-1)):1}" = table ]; then tb=${!n}; set -- "${@:1:$((n-2))}"; else tb=main; fi + 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" ;; @@ -143,11 +146,11 @@ printf 'com.example.dl dl\ncom.example.quiet quiet\n' > "$D/state/apps.txt" log=$(keeper once) 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" "-o rmnet+ -m mark --mark 0x2951 -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")" @@ -160,14 +163,16 @@ 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)" keeper down >/dev/null hasnt "down: the cellular chain gone" "HMGAPI_CELL" "$(ipt -S OUTPUT)" : > "$D/settings/allow/home_wifi" @@ -175,7 +180,7 @@ 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' @@ -306,6 +311,9 @@ 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")" +has "services' lookups (their resolver asks 127.0.0.1) may come back in through wlan0, answers only" "-i wlan0 -d 127.0.0.0/8 -m state --state ESTABLISHED,RELATED -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")" 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) @@ -320,7 +328,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 diff --git a/core/runtime/java/hmgapi/engine/HomeWifi.java b/core/runtime/java/hmgapi/engine/HomeWifi.java index adbe2282..07739e15 100644 --- a/core/runtime/java/hmgapi/engine/HomeWifi.java +++ b/core/runtime/java/hmgapi/engine/HomeWifi.java @@ -303,12 +303,26 @@ static List describe(Engine e, Map> json, Se " iptables -t nat -N HMGAPI_HOMEDNS 2>/dev/null; iptables -t nat -F HMGAPI_HOMEDNS", " iptables -t nat -C OUTPUT -j HMGAPI_HOMEDNS 2>/dev/null || iptables -t nat -I OUTPUT 1 -j HMGAPI_HOMEDNS", " for p in udp tcp; do iptables -t nat -A HMGAPI_HOMEDNS -m owner --gid-owner " + HOME_GID + " -p $p --dport 53 -j DNAT --to-destination $d1; done", + " # a service's resolver (ANDROID_DNS_MODE=local) has no servers in its process on these units, so", + " # it asks 127.0.0.1:53: the DNAT above sends that on to $d1 with the source still 127.0.0.1,", + " # which the kernel only routes out of (and back in through) wlan0 with route_localnet. Masqueraded", + " # (HMGAPI_HOMEOUT: marked), so nothing leaves as 127.0.0.1; only the answers to those queries may", + " # come in for 127.x on wlan0 (HMGAPI_HOMELO), nothing a home network sends on its own", + " echo 1 > $X/proc/sys/net/ipv4/conf/$I/route_localnet 2>/dev/null", + " iptables -N HMGAPI_HOMELO 2>/dev/null; iptables -F HMGAPI_HOMELO", + " iptables -C INPUT -j HMGAPI_HOMELO 2>/dev/null || iptables -I INPUT 1 -j HMGAPI_HOMELO", + " iptables -A HMGAPI_HOMELO -i $I -d 127.0.0.0/8 -m state --state ESTABLISHED,RELATED -j RETURN", + " iptables -A HMGAPI_HOMELO -i $I -d 127.0.0.0/8 -j DROP", " echo \"$gw $d1\" > $W/lease", " say \"home Wi-Fi up: the mods that asked go through it; the car stays on mobile data\"", " fi", "}", "route_down() {", " ip route flush table $T 2>/dev/null", + " no_route", + " echo 0 > $X/proc/sys/net/ipv4/conf/$I/route_localnet 2>/dev/null", + " for _ in 1 2 3 4; do iptables -D INPUT -j HMGAPI_HOMELO 2>/dev/null || break; done", + " iptables -F HMGAPI_HOMELO 2>/dev/null; iptables -X HMGAPI_HOMELO 2>/dev/null", " iptables -t nat -F HMGAPI_HOMEDNS 2>/dev/null", " for r in $(cat $W/dnsroutes 2>/dev/null); do ip route del $r dev $I 2>/dev/null; done", " rm -f $W/dnsroutes $W/lease", @@ -454,6 +468,7 @@ static List describe(Engine e, Map> json, Se " forget) forget \"$2\"; exit $? ;;", "esac", "trap 'down; say stopped; exit 0' TERM # never left with the hotspot off", + "no_route", "tick() { t0=$(now); sleep $nap; d=$(($(now) - t0)); [ $d -le $((nap + 60)) ] || woke $d; }", "while :; do", " keep", diff --git a/core/runtime/java/hmgapi/engine/Network.java b/core/runtime/java/hmgapi/engine/Network.java index 55e80035..6598539b 100644 --- a/core/runtime/java/hmgapi/engine/Network.java +++ b/core/runtime/java/hmgapi/engine/Network.java @@ -676,6 +676,8 @@ static List describe(String id, Ask a, boolean allowed) { " for _ in 1 2 3 4 5 6 7 8; do iptables -t mangle -D OUTPUT -j HMGAPI_HOME 2>/dev/null || break; done", " iptables -t mangle -F HMGAPI_HOME 2>/dev/null; iptables -t mangle -X HMGAPI_HOME 2>/dev/null", " for _ in 1 2 3 4 5 6 7 8; do iptables -t nat -D POSTROUTING -j HMGAPI_HOMEOUT 2>/dev/null || break; done", + " for _ in 1 2 3 4 5 6 7 8; do iptables -t mangle -D POSTROUTING -j HMGAPI_HOMECELL 2>/dev/null || break; done", + " iptables -t mangle -F HMGAPI_HOMECELL 2>/dev/null; iptables -t mangle -X HMGAPI_HOMECELL 2>/dev/null", " iptables -t nat -F HMGAPI_HOMEOUT 2>/dev/null; iptables -t nat -X HMGAPI_HOMEOUT 2>/dev/null", " for _ in 1 2 3 4 5 6 7 8; do ip rule del priority " + HomeWifi.PRIORITY + " 2>/dev/null || break; done", "}", @@ -684,16 +686,24 @@ static List describe(String id, Ask a, boolean allowed) { " case $mode in block|home) ndc resolver setifaceforuidrange " + HomeWifi.IFACE + " $u $u >/dev/null 2>&1 ;; esac", " done < \"$MA\"", "}", + "# The home group and the mods that asked for home Wi-Fi: their IPv4 packets start on the main table's", + "# route (mobile data) and HMG-API's mark (mangle OUTPUT) moves them to table 95 afterwards. -o in", + "# OUTPUT is the route from before that move, so refusing them there refused every home Wi-Fi", + "# connection (seen on an Ioniq 5 US 260507). With home Wi-Fi on they're refused after the move instead", + "# (mangle POSTROUTING, HMGAPI_HOMECELL: anything marked for home Wi-Fi never leaves on rmnet), and", + "# table 95 is never empty (wifi.sh: an unreachable default while home Wi-Fi is down). IPv6 isn't", + "# routed to home Wi-Fi, so it stays refused in OUTPUT; so does everything with home Wi-Fi off.", "mods_up() {", + " home=; grep -q '^homewifi' \"$MA\" && home=1", " for v in 4 6; do", " ipt $v -N HMGAPI_CELL 2>/dev/null; ipt $v -F HMGAPI_CELL; ipt $v -I OUTPUT 1 -j HMGAPI_CELL", - " for g in " + HomeWifi.SERVICE_GID + " " + HomeWifi.HOME_GID + "; do ipt $v -A HMGAPI_CELL -o rmnet+ -m owner --gid-owner $g -j REJECT; done", + " ipt $v -A HMGAPI_CELL -o rmnet+ -m owner --gid-owner " + HomeWifi.SERVICE_GID + " -j REJECT", + " [ -n \"$home\" ] && [ $v = 4 ] || ipt $v -A HMGAPI_CELL -o rmnet+ -m owner --gid-owner " + HomeWifi.HOME_GID + " -j REJECT", " done", - " home=", " while read -r mode u; do", " case $mode in", - " block|home) for v in 4 6; do ipt $v -A HMGAPI_CELL -o rmnet+ -m owner --uid-owner $u -j REJECT; done ;;", - " homewifi) home=1 ;;", + " block) for v in 4 6; do ipt $v -A HMGAPI_CELL -o rmnet+ -m owner --uid-owner $u -j REJECT; done ;;", + " home) for v in 4 6; do [ -n \"$home\" ] && [ $v = 4 ] || ipt $v -A HMGAPI_CELL -o rmnet+ -m owner --uid-owner $u -j REJECT; done ;;", " esac", " done < \"$MA\"", " mods_dns", @@ -703,6 +713,8 @@ static List describe(String id, Ask a, boolean allowed) { " while read -r mode u; do", " [ \"$mode\" = home ] && iptables -t mangle -A HMGAPI_HOME -m owner --uid-owner $u -j MARK --set-mark " + HomeWifi.MARK, " done < \"$MA\"", + " iptables -t mangle -N HMGAPI_HOMECELL 2>/dev/null; iptables -t mangle -F HMGAPI_HOMECELL; iptables -t mangle -I POSTROUTING 1 -j HMGAPI_HOMECELL", + " iptables -t mangle -A HMGAPI_HOMECELL -o rmnet+ -m mark --mark " + HomeWifi.MARK + " -j DROP", " iptables -t nat -N HMGAPI_HOMEOUT 2>/dev/null; iptables -t nat -F HMGAPI_HOMEOUT; iptables -t nat -I POSTROUTING 1 -j HMGAPI_HOMEOUT", " iptables -t nat -A HMGAPI_HOMEOUT -o " + HomeWifi.IFACE + " -m mark --mark " + HomeWifi.MARK + " -j MASQUERADE", " ip rule add priority " + HomeWifi.PRIORITY + " fwmark " + HomeWifi.MARK + " table " + HomeWifi.TABLE, @@ -713,11 +725,13 @@ static List describe(String id, Ask a, boolean allowed) { " first=$(ipt $v -S OUTPUT 2>/dev/null | while read -r a b rest; do [ \"$a\" = -A ] && { echo \"$rest\"; break; }; done)", " [ \"$first\" = '-j HMGAPI_CELL' ] || return 1", " have=0; for l in $(ipt $v -S HMGAPI_CELL 2>/dev/null | while read -r a rest; do [ \"$a\" = -A ] && echo x; done); do have=$((have + 1)); done", - " want=2; while read -r mode u; do case $mode in block|home) want=$((want + 1)) ;; esac; done < \"$MA\"", + " want=2; grep -q '^homewifi' \"$MA\" && [ $v = 4 ] && want=1", + " while read -r mode u; do case $mode in block) want=$((want + 1)) ;; home) [ $v = 4 ] || want=$((want + 1)) ;; esac; done < \"$MA\"", " [ $have = $want ] || return 1", " done", " grep -q '^homewifi' \"$MA\" || return 0", - " iptables -t mangle -C OUTPUT -j HMGAPI_HOME 2>/dev/null && iptables -t nat -C POSTROUTING -j HMGAPI_HOMEOUT 2>/dev/null || return 1", + " iptables -t mangle -C OUTPUT -j HMGAPI_HOME 2>/dev/null && iptables -t mangle -C POSTROUTING -j HMGAPI_HOMECELL 2>/dev/null \\", + " && iptables -t nat -C POSTROUTING -j HMGAPI_HOMEOUT 2>/dev/null || return 1", " out=$(ip rule show 2>/dev/null); case $out in *\"" + HomeWifi.PRIORITY + ":\"*) ;; *) return 1 ;; esac", "}", "mods_down() { mods_fw_down; rm -f \"$MA\"; }", diff --git a/docs/mod-reference.md b/docs/mod-reference.md index 663fe6cf..c9308e9c 100644 --- a/docs/mod-reference.md +++ b/docs/mod-reference.md @@ -365,6 +365,13 @@ home Wi-Fi; the car's own stays on mobile data. A mod that asks while the switch internet (the plan says so). IPv4 only for now. This is also how HMG-API itself is meant to check for updates. +How it's routed, for a mod that wants to know: its packets are marked (`0x2951`) and go out by +table 95 (home Wi-Fi's default; an unreachable default while home Wi-Fi is down), masqueraded +to `wlan0`'s address; anything marked never leaves on `rmnet` (checked after the reroute, +`mangle POSTROUTING`). A service's own lookups (its resolver asks 127.0.0.1:53) are redirected +to home Wi-Fi's DNS server. A root service may also mark its own sockets (`SO_MARK` 0x2951) so +their route is table 95's from the start. + - Check before a download: `HmgApi.homeWifiUp()`, or `HmgApi.homeWifi()` (`{"state": "up", "rssi": -61, "freq": 2437}`; `state` up, waiting (the car is on), searching, nonetworks, stock (the car's own Wi-Fi settings have the Wi-Fi), off). It never From c44f2773b765b494f1ae78154e3313fff9ffada3 Mon Sep 17 00:00:00 2001 From: anthonyanderson Date: Fri, 27 Nov 2026 09:15:04 -0500 Subject: [PATCH 2/4] Home Wi-Fi: the 127.x guard narrowed and kept; rules built with no gap HMGAPI_HOMELO lets in, for 127.x on wlan0, only the DNS server's answers (its address, port 53, established), and it is checked on every route_up instead of only when the lease changes: the stock firewall reloading flushes INPUT and can put its own jump first. route_localnet is on only while the guard is first in INPUT; a guard that can't go in is said and route_localnet stays off. The nat OUTPUT jump to HMGAPI_HOMEDNS is checked on every pass too. The network keeper builds the post-reroute DROP, the mark, masquerade, table 95's unreachable default and the fwmark rule before the IPv4 home REJECTs leave, and fills the cellular chain before OUTPUT jumps to it. mods_ok also checks the DROP inside HMGAPI_HOMECELL. wifi.sh down, net.sh down and the uninstaller take table 95's unreachable default out. The test's fake ip route replace/add now key routes as the kernel does (table, destination, metric; not the type), so a default via the gateway replaces the unreachable one. Tested off-car only (homewifi-test.sh with bash, net-test.sh, install/common/test.sh). Not yet checked on a car with the stock whitelist firewall (US Ioniq 6). Co-Authored-By: Claude Opus 5.5 --- CHANGELOG.md | 9 ++- core/runtime/homewifi-test.sh | 59 +++++++++++++++++-- core/runtime/java/hmgapi/engine/HomeWifi.java | 53 ++++++++++++----- core/runtime/java/hmgapi/engine/Network.java | 36 +++++++---- install/common/uninstall.sh | 3 + 5 files changed, 126 insertions(+), 34 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 00d17d9b..9e52c652 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,11 +19,18 @@ (`mangle POSTROUTING`, `HMGAPI_HOMECELL`: nothing marked for home Wi-Fi leaves on `rmnet`), 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 DROP inside `HMGAPI_HOMECELL`. 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 only the answers may come back in for 127.x (`HMGAPI_HOMELO`). + 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-rc.10 (2026-11-25) diff --git a/core/runtime/homewifi-test.sh b/core/runtime/homewifi-test.sh index bf17441e..1f85ea96 100644 --- a/core/runtime/homewifi-test.sh +++ b/core/runtime/homewifi-test.sh @@ -87,6 +87,7 @@ 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 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 @@ -108,20 +109,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() { # " " 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 elif [ "$1" = table ]; then grep -- " table $2\$" "$f" | sed "s/ table $2\$//" || true; else grep -- " table 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.add) n=$#; if [ "${@:$((n-1)):1}" = table ]; then tb=${!n}; set -- "${@:1:$((n-2))}"; else tb=main; fi - echo "$* table $tb" >> "$f" ;; + 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 @@ -147,7 +158,14 @@ 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 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 @@ -162,6 +180,9 @@ has "marked traffic to table 95" "900: fwmark 0x2951 table 95" "$(cat "$F/fw/ip4 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 DROP again" "-o rmnet+ -m mark --mark 0x2951 -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')" @@ -177,8 +198,10 @@ check "home Wi-Fi off: no marks" no "$([ -f "$F/fw/4/mangle/HMGAPI_HOME" ] && ec hasnt "... no routing rule" "900:" "$(cat "$F/fw/ip4.rule")" 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 @@ -332,9 +355,32 @@ 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")" -has "services' lookups (their resolver asks 127.0.0.1) may come back in through wlan0, answers only" "-i wlan0 -d 127.0.0.0/8 -m state --state ESTABLISHED,RELATED -j RETURN +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)" +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) @@ -430,6 +476,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" diff --git a/core/runtime/java/hmgapi/engine/HomeWifi.java b/core/runtime/java/hmgapi/engine/HomeWifi.java index 5eb4dc39..408a2be5 100644 --- a/core/runtime/java/hmgapi/engine/HomeWifi.java +++ b/core/runtime/java/hmgapi/engine/HomeWifi.java @@ -308,28 +308,51 @@ static List describe(Engine e, Map> json, Se " ip route replace $d/32 via $gw dev $I metric 1000 && echo $d/32 >> $W/dnsroutes", " done", " iptables -t nat -N HMGAPI_HOMEDNS 2>/dev/null; iptables -t nat -F HMGAPI_HOMEDNS", - " iptables -t nat -C OUTPUT -j HMGAPI_HOMEDNS 2>/dev/null || iptables -t nat -I OUTPUT 1 -j HMGAPI_HOMEDNS", " for p in udp tcp; do iptables -t nat -A HMGAPI_HOMEDNS -m owner --gid-owner " + HOME_GID + " -p $p --dport 53 -j DNAT --to-destination $d1; done", - " # a service's resolver (ANDROID_DNS_MODE=local) has no servers in its process on these units, so", - " # it asks 127.0.0.1:53: the DNAT above sends that on to $d1 with the source still 127.0.0.1,", - " # which the kernel only routes out of (and back in through) wlan0 with route_localnet. Masqueraded", - " # (HMGAPI_HOMEOUT: marked), so nothing leaves as 127.0.0.1; only the answers to those queries may", - " # come in for 127.x on wlan0 (HMGAPI_HOMELO), nothing a home network sends on its own", - " echo 1 > $X/proc/sys/net/ipv4/conf/$I/route_localnet 2>/dev/null", - " iptables -N HMGAPI_HOMELO 2>/dev/null; iptables -F HMGAPI_HOMELO", - " iptables -C INPUT -j HMGAPI_HOMELO 2>/dev/null || iptables -I INPUT 1 -j HMGAPI_HOMELO", - " iptables -A HMGAPI_HOMELO -i $I -d 127.0.0.0/8 -m state --state ESTABLISHED,RELATED -j RETURN", - " iptables -A HMGAPI_HOMELO -i $I -d 127.0.0.0/8 -j DROP", + " iptables -F HMGAPI_HOMELO 2>/dev/null # the guard for the new server: lo_guard builds it", " echo \"$gw $d1\" > $W/lease", " say \"home Wi-Fi up: the mods that asked go through it; the car stays on mobile data\"", " fi", + " # checked on every pass, not only when the lease changes: the stock firewall (iptrules) restarting", + " # flushes INPUT and can put its own rules first", + " iptables -t nat -C OUTPUT -j HMGAPI_HOMEDNS 2>/dev/null || iptables -t nat -I OUTPUT 1 -j HMGAPI_HOMEDNS", + " if lo_guard $d1; then echo 1 > $LOCALNET 2>/dev/null; else echo 0 > $LOCALNET 2>/dev/null; fi", + "}", + "# A service's resolver (ANDROID_DNS_MODE=local) has no servers in its process on these units, so it", + "# asks 127.0.0.1:53: HMGAPI_HOMEDNS's DNAT sends that on to the DNS server with the source still", + "# 127.0.0.1, which the kernel only routes out of (and back in through) wlan0 with route_localnet.", + "# Masqueraded (HMGAPI_HOMEOUT: marked), so nothing leaves as 127.0.0.1. route_localnet is on only", + "# while HMGAPI_HOMELO is first in INPUT: for 127.x on wlan0, only the DNS server's answers (source", + "# $1, port 53; the DNAT is undone after filter INPUT, so they still carry its address) get past it,", + "# nothing else a home network sends. $1: the DNS server. Built again when missing or not first.", + "LOCALNET=$X/proc/sys/net/ipv4/conf/$I/route_localnet", + "lo_rules() { # $1 the server, $2 -C (check) or -A (add)", + " for p in udp tcp; do iptables $2 HMGAPI_HOMELO -i $I -s $1/32 -d 127.0.0.0/8 -p $p --sport 53 -m state --state ESTABLISHED -j RETURN 2>/dev/null || return 1; done", + " iptables $2 HMGAPI_HOMELO -i $I -d 127.0.0.0/8 -j DROP 2>/dev/null", + "}", + "lo_first() { [ \"$(iptables -S INPUT 2>/dev/null | while read -r a b rest; do [ \"$a\" = -A ] && { echo \"$rest\"; break; }; done)\" = '-j HMGAPI_HOMELO' ]; }", + "lo_guard() {", + " lo_rules $1 -C && lo_first && return 0", + " echo 0 > $LOCALNET 2>/dev/null # while it's rebuilt", + " again=; iptables -S HMGAPI_HOMELO >/dev/null 2>&1 && [ -f $W/lo.$1 ] && again=1", + " iptables -N HMGAPI_HOMELO 2>/dev/null; iptables -F HMGAPI_HOMELO", + " lo_rules $1 -A", + " for _ in 1 2 3 4; do iptables -D INPUT -j HMGAPI_HOMELO 2>/dev/null || break; done", + " iptables -I INPUT 1 -j HMGAPI_HOMELO", + " if lo_rules $1 -C && lo_first; then", + " rm -f $W/lo.*; : > $W/lo.$1", + " [ -n \"$again\" ] && say \"home Wi-Fi's 127.x guard put back (moved or removed since: the stock firewall reloaded?)\"", + " return 0", + " fi", + " say \"home Wi-Fi's 127.x guard can't be put in: mods' services' lookups stay off (route_localnet off)\"", + " return 1", "}", "route_down() {", " ip route flush table $T 2>/dev/null", " no_route", - " echo 0 > $X/proc/sys/net/ipv4/conf/$I/route_localnet 2>/dev/null", + " echo 0 > $LOCALNET 2>/dev/null", " for _ in 1 2 3 4; do iptables -D INPUT -j HMGAPI_HOMELO 2>/dev/null || break; done", - " iptables -F HMGAPI_HOMELO 2>/dev/null; iptables -X HMGAPI_HOMELO 2>/dev/null", + " iptables -F HMGAPI_HOMELO 2>/dev/null; iptables -X HMGAPI_HOMELO 2>/dev/null; rm -f $W/lo.*", " iptables -t nat -F HMGAPI_HOMEDNS 2>/dev/null", " for r in $(cat $W/dnsroutes 2>/dev/null); do ip route del $r dev $I 2>/dev/null; done", " rm -f $W/dnsroutes $W/lease", @@ -463,7 +486,9 @@ static List describe(Engine e, Map> json, Se " fi", " status waiting", "}", - "down() { client_mode && client_off \"home Wi-Fi off\"; route_down; status off; }", + "# home Wi-Fi off, the uninstaller, the service stopped: table 95's unreachable default out too (the", + "# network keeper's fwmark rule goes with home Wi-Fi; until then, marked traffic is still dropped on rmnet)", + "down() { client_mode && client_off \"home Wi-Fi off\"; route_down; ip route flush table $T 2>/dev/null; status off; }", "umask 077", "case $1 in", " down) down; exit 0 ;;", diff --git a/core/runtime/java/hmgapi/engine/Network.java b/core/runtime/java/hmgapi/engine/Network.java index 6598539b..11efe563 100644 --- a/core/runtime/java/hmgapi/engine/Network.java +++ b/core/runtime/java/hmgapi/engine/Network.java @@ -693,10 +693,29 @@ static List describe(String id, Ask a, boolean allowed) { "# (mangle POSTROUTING, HMGAPI_HOMECELL: anything marked for home Wi-Fi never leaves on rmnet), and", "# table 95 is never empty (wifi.sh: an unreachable default while home Wi-Fi is down). IPv6 isn't", "# routed to home Wi-Fi, so it stays refused in OUTPUT; so does everything with home Wi-Fi off.", + "# Built in an order with no gap: home Wi-Fi's mark, its post-reroute block, masquerade, table 95's", + "# unreachable default (if wifi.sh hasn't put one yet) and the fwmark rule first, then the cellular", + "# chain without the IPv4 home REJECTs, filled before OUTPUT jumps to it.", "mods_up() {", " home=; grep -q '^homewifi' \"$MA\" && home=1", + " if [ -n \"$home\" ]; then", + " iptables -t mangle -N HMGAPI_HOMECELL 2>/dev/null; iptables -t mangle -F HMGAPI_HOMECELL", + " iptables -t mangle -A HMGAPI_HOMECELL -o rmnet+ -m mark --mark " + HomeWifi.MARK + " -j DROP", + " iptables -t mangle -I POSTROUTING 1 -j HMGAPI_HOMECELL", + " iptables -t mangle -N HMGAPI_HOME 2>/dev/null; iptables -t mangle -F HMGAPI_HOME", + " iptables -t mangle -A HMGAPI_HOME -m owner --gid-owner " + HomeWifi.HOME_GID + " -j MARK --set-mark " + HomeWifi.MARK, + " while read -r mode u; do", + " [ \"$mode\" = home ] && iptables -t mangle -A HMGAPI_HOME -m owner --uid-owner $u -j MARK --set-mark " + HomeWifi.MARK, + " done < \"$MA\"", + " iptables -t mangle -I OUTPUT 1 -j HMGAPI_HOME", + " iptables -t nat -N HMGAPI_HOMEOUT 2>/dev/null; iptables -t nat -F HMGAPI_HOMEOUT", + " iptables -t nat -A HMGAPI_HOMEOUT -o " + HomeWifi.IFACE + " -m mark --mark " + HomeWifi.MARK + " -j MASQUERADE", + " iptables -t nat -I POSTROUTING 1 -j HMGAPI_HOMEOUT", + " [ -n \"$(ip route show table " + HomeWifi.TABLE + " 2>/dev/null)\" ] || ip route add unreachable default table " + HomeWifi.TABLE + " 2>/dev/null", + " ip rule add priority " + HomeWifi.PRIORITY + " fwmark " + HomeWifi.MARK + " table " + HomeWifi.TABLE, + " fi", " for v in 4 6; do", - " ipt $v -N HMGAPI_CELL 2>/dev/null; ipt $v -F HMGAPI_CELL; ipt $v -I OUTPUT 1 -j HMGAPI_CELL", + " ipt $v -N HMGAPI_CELL 2>/dev/null; ipt $v -F HMGAPI_CELL", " ipt $v -A HMGAPI_CELL -o rmnet+ -m owner --gid-owner " + HomeWifi.SERVICE_GID + " -j REJECT", " [ -n \"$home\" ] && [ $v = 4 ] || ipt $v -A HMGAPI_CELL -o rmnet+ -m owner --gid-owner " + HomeWifi.HOME_GID + " -j REJECT", " done", @@ -706,18 +725,8 @@ static List describe(String id, Ask a, boolean allowed) { " home) for v in 4 6; do [ -n \"$home\" ] && [ $v = 4 ] || ipt $v -A HMGAPI_CELL -o rmnet+ -m owner --uid-owner $u -j REJECT; done ;;", " esac", " done < \"$MA\"", + " for v in 4 6; do ipt $v -I OUTPUT 1 -j HMGAPI_CELL; done", " mods_dns", - " [ -n \"$home\" ] || return 0", - " iptables -t mangle -N HMGAPI_HOME 2>/dev/null; iptables -t mangle -F HMGAPI_HOME; iptables -t mangle -I OUTPUT 1 -j HMGAPI_HOME", - " iptables -t mangle -A HMGAPI_HOME -m owner --gid-owner " + HomeWifi.HOME_GID + " -j MARK --set-mark " + HomeWifi.MARK, - " while read -r mode u; do", - " [ \"$mode\" = home ] && iptables -t mangle -A HMGAPI_HOME -m owner --uid-owner $u -j MARK --set-mark " + HomeWifi.MARK, - " done < \"$MA\"", - " iptables -t mangle -N HMGAPI_HOMECELL 2>/dev/null; iptables -t mangle -F HMGAPI_HOMECELL; iptables -t mangle -I POSTROUTING 1 -j HMGAPI_HOMECELL", - " iptables -t mangle -A HMGAPI_HOMECELL -o rmnet+ -m mark --mark " + HomeWifi.MARK + " -j DROP", - " iptables -t nat -N HMGAPI_HOMEOUT 2>/dev/null; iptables -t nat -F HMGAPI_HOMEOUT; iptables -t nat -I POSTROUTING 1 -j HMGAPI_HOMEOUT", - " iptables -t nat -A HMGAPI_HOMEOUT -o " + HomeWifi.IFACE + " -m mark --mark " + HomeWifi.MARK + " -j MASQUERADE", - " ip rule add priority " + HomeWifi.PRIORITY + " fwmark " + HomeWifi.MARK + " table " + HomeWifi.TABLE, "}", "# in place, first in OUTPUT (nothing ahead of it may accept a mod's packet for cellular), all its rules there", "mods_ok() {", @@ -731,6 +740,7 @@ static List describe(String id, Ask a, boolean allowed) { " done", " grep -q '^homewifi' \"$MA\" || return 0", " iptables -t mangle -C OUTPUT -j HMGAPI_HOME 2>/dev/null && iptables -t mangle -C POSTROUTING -j HMGAPI_HOMECELL 2>/dev/null \\", + " && iptables -t mangle -C HMGAPI_HOMECELL -o rmnet+ -m mark --mark " + HomeWifi.MARK + " -j DROP 2>/dev/null \\", " && iptables -t nat -C POSTROUTING -j HMGAPI_HOMEOUT 2>/dev/null || return 1", " out=$(ip rule show 2>/dev/null); case $out in *\"" + HomeWifi.PRIORITY + ":\"*) ;; *) return 1 ;; esac", "}", @@ -749,7 +759,7 @@ static List describe(String id, Ask a, boolean allowed) { " tick=$((tick + 1)); [ $((tick % 6)) = 0 ] && mods_dns # netd forgets it when it restarts", "}", "tick=0", - "down() { fw_down; others_down; mods_down; rm -f \"$A\"; }", + "down() { fw_down; others_down; mods_down; ip route flush table " + HomeWifi.TABLE + " 2>/dev/null; rm -f \"$A\"; } # table 95: wifi.sh's, in case it couldn't", "keep() {", " case $(getprop init.svc.iptrules) in running|restarting) return ;; esac # the stock firewall loading", " mods_keep", diff --git a/install/common/uninstall.sh b/install/common/uninstall.sh index 4b9ac981..f33a5b2a 100644 --- a/install/common/uninstall.sh +++ b/install/common/uninstall.sh @@ -136,6 +136,9 @@ if [ -f "$D/state/services.txt" ]; then log "mods' services stopped" fi # mods' network rules, routes and settings out (the keeper does it when stopped; again in case it couldn't) +if [ -z "$ROOT" ] && [ -f "$D/state/wifi.sh" ]; then + sh "$D/state/wifi.sh" down >/dev/null 2>&1 && log "home Wi-Fi off, its routes out" +fi if [ -z "$ROOT" ] && [ -f "$D/state/net.sh" ]; then sh "$D/state/net.sh" down >/dev/null 2>&1 && log "mods' network rules out" fi From ee222b807a741cf36003a93981bfedaa6ba974de Mon Sep 17 00:00:00 2001 From: anthonyanderson Date: Fri, 27 Nov 2026 09:30:44 -0500 Subject: [PATCH 3/4] Docs: a root service's own sockets: the mark and wlan0, or none With SO_MARK alone a socket uses the main table (mobile data) until the network keeper's fwmark rule is in; HMG-API's own downloads bind to wlan0 too and fail if either is refused. Co-Authored-By: Claude Opus 5.5 --- docs/mod-reference.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/docs/mod-reference.md b/docs/mod-reference.md index c9308e9c..de43f940 100644 --- a/docs/mod-reference.md +++ b/docs/mod-reference.md @@ -370,7 +370,9 @@ table 95 (home Wi-Fi's default; an unreachable default while home Wi-Fi is down) to `wlan0`'s address; anything marked never leaves on `rmnet` (checked after the reroute, `mangle POSTROUTING`). A service's own lookups (its resolver asks 127.0.0.1:53) are redirected to home Wi-Fi's DNS server. A root service may also mark its own sockets (`SO_MARK` 0x2951) so -their route is table 95's from the start. +their route is table 95's from the start; it should bind them to `wlan0` too (`SO_BINDTODEVICE`) +and give up if either is refused, as HMG-API's own downloads do: with the mark alone, a socket +uses the main table (mobile data) until the network keeper's fwmark rule is in. - Check before a download: `HmgApi.homeWifiUp()`, or `HmgApi.homeWifi()` (`{"state": "up", "rssi": -61, "freq": 2437}`; `state` up, waiting (the car is on), From 32f9c50958bbce7aa7b66bebbfa3e9589cfc84af Mon Sep 17 00:00:00 2001 From: anthonyanderson Date: Mon, 30 Nov 2026 16:38:57 -0500 Subject: [PATCH 4/4] Home Wi-Fi: review fixes (route_localnet off before the guard goes, owner DROPs on rmnet, no_route here) - route_up: route_localnet is set to 0 before HMGAPI_HOMELO is flushed for a new DNS server, so 127/8 from wlan0 is never unguarded (test: every flush sees 0). - HMGAPI_HOMECELL also drops the home group's and the home apps' traffic on rmnet+ by owner, so the block holds if the mark rule is lost; mods_ok counts its rules. - no_route() moves here from #419, which never called it. - The comments and changelog no longer claim a repair has no gap. Co-Authored-By: Claude Opus 5.5 --- CHANGELOG.md | 6 ++++-- core/runtime/homewifi-test.sh | 16 ++++++++++++++-- core/runtime/java/hmgapi/engine/HomeWifi.java | 5 +++++ core/runtime/java/hmgapi/engine/Network.java | 18 ++++++++++++++---- 4 files changed, 37 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9e52c652..c069f340 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,12 +16,14 @@ 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`), + (`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 DROP inside `HMGAPI_HOMECELL`. Home Wi-Fi off + 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; diff --git a/core/runtime/homewifi-test.sh b/core/runtime/homewifi-test.sh index 1f85ea96..d6331400 100644 --- a/core/runtime/homewifi-test.sh +++ b/core/runtime/homewifi-test.sh @@ -88,6 +88,7 @@ stock() { case $1 in INPUT|OUTPUT|FORWARD|PREROUTING|POSTROUTING) return 0 ;; es 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 @@ -171,7 +172,9 @@ check "the cellular chain first in OUTPUT" "-A OUTPUT -j HMGAPI_CELL" "$(ipt -S 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: 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" "-o rmnet+ -m mark --mark 0x2951 -j DROP" "$(cat "$F/fw/4/mangle/HMGAPI_HOMECELL")" +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")" @@ -182,7 +185,10 @@ 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 DROP again" "-o rmnet+ -m mark --mark 0x2951 -j DROP" "$(cat "$F/fw/4/mangle/HMGAPI_HOMECELL")" +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')" @@ -373,6 +379,12 @@ 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) diff --git a/core/runtime/java/hmgapi/engine/HomeWifi.java b/core/runtime/java/hmgapi/engine/HomeWifi.java index 3babb101..a5bb5e37 100644 --- a/core/runtime/java/hmgapi/engine/HomeWifi.java +++ b/core/runtime/java/hmgapi/engine/HomeWifi.java @@ -282,6 +282,10 @@ static List describe(Engine e, Map> json, Se "}", "", "# ---- while joined: the car's own traffic on mobile data, the mods that asked on home Wi-Fi", + "# table 95 is never empty: while home Wi-Fi isn't up it holds an unreachable default, so traffic", + "# marked for home Wi-Fi (the mods that asked, HMG-API's own downloads) fails instead of falling", + "# through to the main table and the car's mobile data", + "no_route() { [ -n \"$(ip route show table $T 2>/dev/null)\" ] || ip route add unreachable default table $T 2>/dev/null; }", "route_up() {", " gw=", " for r in $(ip route show dev $I 2>/dev/null | while read -r a b c rest; do [ \"$a\" = default ] && [ \"$b\" = via ] && echo $c; done); do gw=$r; done", @@ -305,6 +309,7 @@ static List describe(Engine e, Map> json, Se " done", " iptables -t nat -N HMGAPI_HOMEDNS 2>/dev/null; iptables -t nat -F HMGAPI_HOMEDNS", " for p in udp tcp; do iptables -t nat -A HMGAPI_HOMEDNS -m owner --gid-owner " + HOME_GID + " -p $p --dport 53 -j DNAT --to-destination $d1; done", + " echo 0 > $LOCALNET 2>/dev/null # off before the guard goes: 127/8 from wlan0 is never unguarded", " iptables -F HMGAPI_HOMELO 2>/dev/null # the guard for the new server: lo_guard builds it", " echo \"$gw $d1\" > $W/lease", " say \"home Wi-Fi up: the mods that asked go through it; the car stays on mobile data\"", diff --git a/core/runtime/java/hmgapi/engine/Network.java b/core/runtime/java/hmgapi/engine/Network.java index 11efe563..eeb9834d 100644 --- a/core/runtime/java/hmgapi/engine/Network.java +++ b/core/runtime/java/hmgapi/engine/Network.java @@ -690,17 +690,23 @@ static List describe(String id, Ask a, boolean allowed) { "# route (mobile data) and HMG-API's mark (mangle OUTPUT) moves them to table 95 afterwards. -o in", "# OUTPUT is the route from before that move, so refusing them there refused every home Wi-Fi", "# connection (seen on an Ioniq 5 US 260507). With home Wi-Fi on they're refused after the move instead", - "# (mangle POSTROUTING, HMGAPI_HOMECELL: anything marked for home Wi-Fi never leaves on rmnet), and", + "# (mangle POSTROUTING, HMGAPI_HOMECELL: anything marked for home Wi-Fi never leaves on rmnet, nor does", + "# the home group's or the home apps' own traffic, by owner, so the block holds without the mark), and", "# table 95 is never empty (wifi.sh: an unreachable default while home Wi-Fi is down). IPv6 isn't", "# routed to home Wi-Fi, so it stays refused in OUTPUT; so does everything with home Wi-Fi off.", - "# Built in an order with no gap: home Wi-Fi's mark, its post-reroute block, masquerade, table 95's", - "# unreachable default (if wifi.sh hasn't put one yet) and the fwmark rule first, then the cellular", - "# chain without the IPv4 home REJECTs, filled before OUTPUT jumps to it.", + "# Built in an order with no gap while it goes in: home Wi-Fi's mark, its post-reroute block,", + "# masquerade, table 95's unreachable default (if wifi.sh hasn't put one yet) and the fwmark rule", + "# first, then the cellular chain without the IPv4 home REJECTs, filled before OUTPUT jumps to it.", + "# A repair (mods_keep: mods_fw_down, then mods_up) still has a moment with the chains out, as before.", "mods_up() {", " home=; grep -q '^homewifi' \"$MA\" && home=1", " if [ -n \"$home\" ]; then", " iptables -t mangle -N HMGAPI_HOMECELL 2>/dev/null; iptables -t mangle -F HMGAPI_HOMECELL", " iptables -t mangle -A HMGAPI_HOMECELL -o rmnet+ -m mark --mark " + HomeWifi.MARK + " -j DROP", + " iptables -t mangle -A HMGAPI_HOMECELL -o rmnet+ -m owner --gid-owner " + HomeWifi.HOME_GID + " -j DROP", + " while read -r mode u; do", + " [ \"$mode\" = home ] && iptables -t mangle -A HMGAPI_HOMECELL -o rmnet+ -m owner --uid-owner $u -j DROP", + " done < \"$MA\"", " iptables -t mangle -I POSTROUTING 1 -j HMGAPI_HOMECELL", " iptables -t mangle -N HMGAPI_HOME 2>/dev/null; iptables -t mangle -F HMGAPI_HOME", " iptables -t mangle -A HMGAPI_HOME -m owner --gid-owner " + HomeWifi.HOME_GID + " -j MARK --set-mark " + HomeWifi.MARK, @@ -741,8 +747,12 @@ static List describe(String id, Ask a, boolean allowed) { " grep -q '^homewifi' \"$MA\" || return 0", " iptables -t mangle -C OUTPUT -j HMGAPI_HOME 2>/dev/null && iptables -t mangle -C POSTROUTING -j HMGAPI_HOMECELL 2>/dev/null \\", " && iptables -t mangle -C HMGAPI_HOMECELL -o rmnet+ -m mark --mark " + HomeWifi.MARK + " -j DROP 2>/dev/null \\", + " && iptables -t mangle -C HMGAPI_HOMECELL -o rmnet+ -m owner --gid-owner " + HomeWifi.HOME_GID + " -j DROP 2>/dev/null \\", " && iptables -t nat -C POSTROUTING -j HMGAPI_HOMEOUT 2>/dev/null || return 1", " out=$(ip rule show 2>/dev/null); case $out in *\"" + HomeWifi.PRIORITY + ":\"*) ;; *) return 1 ;; esac", + " have=0; for l in $(iptables -t mangle -S HMGAPI_HOMECELL 2>/dev/null | while read -r a rest; do [ \"$a\" = -A ] && echo x; done); do have=$((have + 1)); done", + " want=2; while read -r mode u; do [ \"$mode\" = home ] && want=$((want + 1)); done < \"$MA\"", + " [ $have = $want ]", "}", "mods_down() { mods_fw_down; rm -f \"$MA\"; }", "mods_keep() {",