Skip to content

Use all your internet connections at once on Windows, macOS, Linux and Android: per-connection load balancing over Wi-Fi, Ethernet, mobile data and phone tethers

macOS: keep the old primary's scoped default when a tether takes over (#20) (#23)

Rohit Mahto089a101bd25d
diff --git a/CHANGELOG.md b/CHANGELOG.md
index b934789..ec49b65 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -18,6 +18,10 @@ entry here before pushing a version tag.
Windows line and every manual way.
### Fixed
+- **macOS: the wired Ethernet disappeared when an Android phone was connected through TetherKit** (#20).
+ When the phone became the primary connection, macOS could not add its own route for the Ethernet next
+ to net-merge's, and net-merge then removed its route, leaving the Ethernet with none until the cable was
+ plugged in again. net-merge now leaves that route to macOS.
- **Windows: turning on merging could stop all internet, with every adapter showing "no internet"**
(#10). On an adapter with *weak host send* or *forwarding* turned on, which Mobile Hotspot, Internet
Connection Sharing, Hyper-V and some VPNs do, Windows ignores the adapter a connection is pinned to, so
diff --git a/internal/tun/tun_darwin.go b/internal/tun/tun_darwin.go
index 9d3d7fa..341d742 100644
--- a/internal/tun/tun_darwin.go
+++ b/internal/tun/tun_darwin.go
@@ -131,14 +131,25 @@ func startScopedSync() error {
}
func stopScopedSync() {
+ routes, _ := nics.DefaultRoutes()
scoped.mu.Lock()
defer scoped.mu.Unlock()
if scoped.stop != nil {
close(scoped.stop)
scoped.stop = nil
}
+ primary := map[scopedKey]bool{}
+ for _, r := range routes {
+ if !r.Scoped && r.GW != nil {
+ primary[scopedKey{r.Name, r.V6}] = true
+ }
+ }
for k, gw := range scoped.added {
- run("/sbin/route", scopedArgs("delete", k, gw)...)
+ // Only where the adapter is still primary: on one that stopped being primary since the last
+ // sync, macOS needs this route as its own scoped default (see syncScoped).
+ if routes == nil || primary[k] {
+ run("/sbin/route", scopedArgs("delete", k, gw)...)
+ }
}
scoped.added = nil
}
@@ -188,7 +199,12 @@ func syncScoped(stop chan struct{}) error {
}
for k, gw := range scoped.added {
if _, ok := want[k]; !ok {
- run("/sbin/route", scopedArgs("delete", k, gw)...)
+ // No longer the primary service (a tether took over): macOS now wants a scoped default
+ // there itself, and ours, already in its place, made its own add fail. Leave the route to
+ // macOS; deleting it would leave the adapter with no default route until it reconnects (#20).
+ if !have[k] {
+ run("/sbin/route", scopedArgs("delete", k, gw)...)
+ }
delete(scoped.added, k)
}
}
diff --git a/scripts/macos-smoke.sh b/scripts/macos-smoke.sh
index 9cdb876..8eac729 100755
--- a/scripts/macos-smoke.sh
+++ b/scripts/macos-smoke.sh
@@ -46,6 +46,22 @@ dig +short +time=3 +tries=1 @1.1.1.1 example.com | grep -qE '^[0-9.]+$' &&
relayed=$(jq '[.NICs[].Total] | add' /var/run/net-merge/status.json 2>/dev/null || jq '[.NICs[].Total] | add' "$STATE/status.json")
[ "${relayed:-0}" -ge 1 ] && pass "flows were relayed by net-merge ($relayed)" || fail "no flow went through net-merge"
+# What macOS does when a tether becomes the primary service: the old primary loses its unscoped
+# default and gets a scoped one, which ours already is. It must survive the next sync (#20).
+echo "== the primary adapter stops being primary: its scoped default must stay"
+read -r gw nic < <(netstat -rn -f inet | awk '$1 == "default" && $3 !~ /I/ {print $2, $4; exit}')
+sudo route -q -n delete default "$gw"
+sleep 5 # more than one 3 s sync
+netstat -rn -f inet | grep -E "^default .*I.* $nic" >/dev/null &&
+ pass "$nic keeps a scoped default after it stops being primary" || fail "$nic lost its default route when it stopped being primary"
+[ "$(http)" = 200 ] && pass "HTTPS still works through net-merge" || fail "HTTPS through net-merge failed after the handover"
+# and back: macOS replaces the scoped default with an unscoped one, net-merge adds its own again
+sudo route -q -n delete default "$gw" -ifscope "$nic"
+sudo route -q -n add default "$gw"
+sleep 5
+netstat -rn -f inet | grep -E "^default .*I.* $nic" >/dev/null &&
+ pass "net-merge scoped $nic again once it was primary again" || fail "no scoped default on $nic after it became primary again"
+
echo "== off: adapter, routes and scoped defaults go away, internet still works"
"$BIN" off -state-dir "$STATE"
sleep 1