diff --git a/CHANGELOG.md b/CHANGELOG.md
index 92834ae2..f6bd2ad9 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -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)
diff --git a/core/runtime/homewifi-test.sh b/core/runtime/homewifi-test.sh
index 193a545c..d6331400 100644
--- a/core/runtime/homewifi-test.sh
+++ b/core/runtime/homewifi-test.sh
@@ -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
@@ -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() { # "
" 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
@@ -144,14 +159,23 @@ 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")"
@@ -159,27 +183,37 @@ 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 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'
@@ -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)
@@ -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
@@ -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"
diff --git a/core/runtime/java/hmgapi/engine/HomeWifi.java b/core/runtime/java/hmgapi/engine/HomeWifi.java
index 4f7c33e7..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",
@@ -304,14 +308,52 @@ 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",
+ " 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\"",
" 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 > $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; 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",
@@ -445,7 +487,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 ;;",
@@ -457,6 +501,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..eeb9834d 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,28 +686,53 @@ 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, 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 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,
+ " 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",
- " 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 -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",
- " 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\"",
+ " 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 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() {",
@@ -713,12 +740,19 @@ 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 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() {",
@@ -735,7 +769,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/docs/mod-reference.md b/docs/mod-reference.md
index 663fe6cf..de43f940 100644
--- a/docs/mod-reference.md
+++ b/docs/mod-reference.md
@@ -365,6 +365,15 @@ 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; 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),
searching, nonetworks, stock (the car's own Wi-Fi settings have the Wi-Fi), off). It never
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