fix(libSceNetCtl): read the default route from the routing table on macOS #6685

Merged
DavidSamir merged 7 commits from upstream-pr/6685 into main 2026-10-19 08:49:00 +00:00
DavidSamir commented 2026-10-16 04:46:15 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by DavidSamir on 2026-10-16T04:46:15Z.


What

sceNetCtlGetInfo finds the host's default route in /proc/net/route, which macOS does not have, so on macOS every IPv4 query throws:

sceNetCtlGetInfo: the host routing table is unavailable

On macOS it now reads the IPv4 routes that have a gateway with sysctl({CTL_NET, PF_ROUTE, 0, AF_INET, NET_RT_FLAGS, RTF_GATEWAY}), the table netstat -rn shows, and keeps the routes to 0.0.0.0 that are up, neither rejecting nor host routes, on an interface that still exists. macOS keeps an unscoped default route, the one it sends traffic by, beside interface-scoped ones (RTF_IFSCOPE), which rank after it. Address, netmask, MTU and DNS servers already come from getifaddrs, SIOCGIFMTU and /etc/resolv.conf, which macOS has. Linux and Windows are unchanged.

The parsing is exported as NetCtlDefaultRoutes_nid_no_patch, like NetCtlReadByteCounter_nid_no_patch, so a test can feed it a routing table built by hand: an unscoped default route, a host route, a host route to 0.0.0.0, a rejecting default route, an interface-scoped one, one that is down, one on an interface that does not exist and a truncated last message.

sceNetCtlGetIfStat keeps throwing on macOS, now with a debt entry: an unprivileged process reads the interface byte counters, through getifaddrs and sysctl(NET_RT_IFLIST2) alike, reduced to their low 32 bits (en0 here: 426753024 received against 47671393472 in netstat -ib, 11 times 2^32 less), so a total would wrap every 4 GiB.

Commits: the macOS route lookup, its test, the debt entries.

Tested

macOS 26.7.1, Apple M2 on Wi-Fi, AppleClang 21.0.0, x86-64 build under Rosetta, Release.

main does not build libc and libkernel on macOS for other reasons as well (#4627, #4630, #6124 and mugurc's macOS branches address them), so the tests ran in a local tree of main ce991f1ec with those merged.

  • main: guest_net_ctl aborts with the error above.
  • This branch: guest_net_ctl passes 5 runs of 5. The values it reads match the host: address 192.168.0.102, gateway 192.168.0.1 (route -n get default), MTU 1500, first IPv4 DNS server 192.168.0.1 (/etc/resolv.conf).
  • The new case fails when host routes are kept (aborts) and when scoped routes are not ranked after the unscoped one (aborts).
  • tools/check_conventions.py --base origin/main and git diff --check pass.

Windows 11 (WinLibs GCC 15.2.0 r7, UCRT, Release) and Ubuntu 24.04 under WSL2 (GCC 13.3.0, Release), head 80a9ef9e3 merged onto main 5d9dcc42d: guest_net_ctl pass 3 runs of 3 with the change, and 3 runs of 3 with libSceNetCtl/Export.cpp as on main and the tests kept. It passes either way there: their code paths are unchanged and the new test case is compiled on macOS only.

Overlap: #6138 (callbacks) edits the declarations and the start of main in GuestNetCtl.cpp, and #6039, #6138 and #6227 edit the sceNetCtlGetInfo entry next to the new debt entries; none of them changes how the route is found. #6044 moves GuestNetCtl.cpp. Whichever lands second rebases.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Unblocks: none named; ProsperoLichess (PPSA99009) and Prospero Explorer (PPSA99109) import sceNetCtlGetInfo, but on macOS neither reached it in a 45 s run
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4883613331 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6685). Originally posted by [DavidSamir](https://github.com/DavidSamir) on 2026-10-16T04:46:15Z. --- ### What `sceNetCtlGetInfo` finds the host's default route in `/proc/net/route`, which macOS does not have, so on macOS every IPv4 query throws: ``` sceNetCtlGetInfo: the host routing table is unavailable ``` On macOS it now reads the IPv4 routes that have a gateway with `sysctl({CTL_NET, PF_ROUTE, 0, AF_INET, NET_RT_FLAGS, RTF_GATEWAY})`, the table `netstat -rn` shows, and keeps the routes to 0.0.0.0 that are up, neither rejecting nor host routes, on an interface that still exists. macOS keeps an unscoped default route, the one it sends traffic by, beside interface-scoped ones (`RTF_IFSCOPE`), which rank after it. Address, netmask, MTU and DNS servers already come from `getifaddrs`, `SIOCGIFMTU` and `/etc/resolv.conf`, which macOS has. Linux and Windows are unchanged. The parsing is exported as `NetCtlDefaultRoutes_nid_no_patch`, like `NetCtlReadByteCounter_nid_no_patch`, so a test can feed it a routing table built by hand: an unscoped default route, a host route, a host route to 0.0.0.0, a rejecting default route, an interface-scoped one, one that is down, one on an interface that does not exist and a truncated last message. `sceNetCtlGetIfStat` keeps throwing on macOS, now with a debt entry: an unprivileged process reads the interface byte counters, through `getifaddrs` and `sysctl(NET_RT_IFLIST2)` alike, reduced to their low 32 bits (`en0` here: 426753024 received against 47671393472 in `netstat -ib`, 11 times 2^32 less), so a total would wrap every 4 GiB. Commits: the macOS route lookup, its test, the debt entries. ### Tested macOS 26.7.1, Apple M2 on Wi-Fi, AppleClang 21.0.0, x86-64 build under Rosetta, Release. `main` does not build libc and libkernel on macOS for other reasons as well (#4627, #4630, #6124 and mugurc's macOS branches address them), so the tests ran in a local tree of `main` ce991f1ec with those merged. - `main`: `guest_net_ctl` aborts with the error above. - This branch: `guest_net_ctl` passes 5 runs of 5. The values it reads match the host: address 192.168.0.102, gateway 192.168.0.1 (`route -n get default`), MTU 1500, first IPv4 DNS server 192.168.0.1 (`/etc/resolv.conf`). - The new case fails when host routes are kept (aborts) and when scoped routes are not ranked after the unscoped one (aborts). - `tools/check_conventions.py --base origin/main` and `git diff --check` pass. Windows 11 (WinLibs GCC 15.2.0 r7, UCRT, Release) and Ubuntu 24.04 under WSL2 (GCC 13.3.0, Release), head `80a9ef9e3` merged onto `main` 5d9dcc42d: `guest_net_ctl` pass 3 runs of 3 with the change, and 3 runs of 3 with `libSceNetCtl/Export.cpp` as on `main` and the tests kept. It passes either way there: their code paths are unchanged and the new test case is compiled on macOS only. Overlap: #6138 (callbacks) edits the declarations and the start of `main` in `GuestNetCtl.cpp`, and #6039, #6138 and #6227 edit the `sceNetCtlGetInfo` entry next to the new debt entries; none of them changes how the route is found. #6044 moves `GuestNetCtl.cpp`. Whichever lands second rebases. ### Checklist - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] Unblocks: none named; ProsperoLichess (PPSA99009) and Prospero Explorer (PPSA99109) import `sceNetCtlGetInfo`, but on macOS neither reached it in a 45 s run - [x] GPUs tested: n/a - [x] AI-assisted: yes
github-actions[bot] commented 2026-10-16 05:32:20 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-16T05:32:20Z.


Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.

Pull request Shared files not compared
PR 6039 docs/dev/debt/libSceNetCtl.md
PR 6044 core/libs/tests/GuestNetCtl.cpp
PR 7201 core/libs/prx/libSceNetCtl/Export.cpp
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6208825518 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6685#issuecomment-6208825518). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-16T05:32:20Z. --- <!-- pr-overlap --> Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap. | Pull request | Shared files not compared | |---|---| | [PR 6039](https://www.github.com/boykopovar/AnyPS5/pull/6039) | `docs/dev/debt/libSceNetCtl.md` | | [PR 6044](https://www.github.com/boykopovar/AnyPS5/pull/6044) | `core/libs/tests/GuestNetCtl.cpp` | | [PR 7201](https://www.github.com/boykopovar/AnyPS5/pull/7201) | `core/libs/prx/libSceNetCtl/Export.cpp` |
DavidSamir commented 2026-10-16 07:33:43 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by DavidSamir on 2026-10-16T07:33:43Z.


Windows and Linux results for 80a9ef9e3b, merged onto main 5d9dcc42d2:

Host Toolchain With the change Code as on main, new tests kept Restored
Windows 11 WinLibs GCC 15.2.0 r7 (UCRT), Release guest_net_ctl 3/3 3/3 1/1
Ubuntu 24.04 (WSL2) GCC 13.3.0, Release guest_net_ctl 3/3 3/3 1/1

Reverted for the middle column: libSceNetCtl/Export.cpp. The test passes either way on both hosts, as expected for a macOS-only routing-table path. Windows and Linux still build and pass with it.

AI-assisted: yes

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6210485055 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6685#issuecomment-6210485055). Originally posted by [DavidSamir](https://github.com/DavidSamir) on 2026-10-16T07:33:43Z. --- Windows and Linux results for 80a9ef9e3b, merged onto `main` 5d9dcc42d2: | Host | Toolchain | With the change | Code as on `main`, new tests kept | Restored | |---|---|---|---|---| | Windows 11 | WinLibs GCC 15.2.0 r7 (UCRT), Release | `guest_net_ctl` 3/3 | 3/3 | 1/1 | | Ubuntu 24.04 (WSL2) | GCC 13.3.0, Release | `guest_net_ctl` 3/3 | 3/3 | 1/1 | Reverted for the middle column: `libSceNetCtl/Export.cpp`. The test passes either way on both hosts, as expected for a macOS-only routing-table path. Windows and Linux still build and pass with it. AI-assisted: yes
blawesom commented 2026-10-17 10:52:28 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by blawesom on 2026-10-17T10:52:28Z.


Rebase handoff — rebased onto current main (authorship unchanged). One docs-debt conflict, unioned.

Verified on Linux (CachyOS, kernel 7.2.9, GCC 16.2.1, Ninja, Release, BUILD_TESTING=ON, AMD RX 9070 XT / RADV):

  • builds; tools/check_conventions.py --base origin/main passes
  • full suite: 1338/1339 — the only failure is the known main regression (agc_driver_tessellation segfault, #4727), not this PR

To pick it up:

git fetch https://github.com/blawesom/AnyPS5 rebase/6685
git checkout <your branch>
git merge --ff-only FETCH_HEAD

Head is 90d1f458a.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6233039231 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6685#issuecomment-6233039231). Originally posted by [blawesom](https://github.com/blawesom) on 2026-10-17T10:52:28Z. --- **Rebase handoff** — rebased onto current `main` (authorship unchanged). One docs-debt conflict, unioned. Verified on Linux (CachyOS, kernel 7.2.9, GCC 16.2.1, Ninja, Release, `BUILD_TESTING=ON`, AMD RX 9070 XT / RADV): - builds; `tools/check_conventions.py --base origin/main` passes - **full suite: 1338/1339** — the only failure is the known `main` regression (`agc_driver_tessellation` segfault, #4727), not this PR **To pick it up:** ``` git fetch https://github.com/blawesom/AnyPS5 rebase/6685 git checkout <your branch> git merge --ff-only FETCH_HEAD ``` Head is `90d1f458a`.
DavidSamir commented 2026-10-17 15:34:33 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by DavidSamir on 2026-10-17T15:34:33Z.


Thanks, @blawesom. I checked your rebase/6685 against this branch: the code commits are identical, and the debt-file commit differs only by the conflict resolution. Rather than force-pushing, I merged current main into the branch with the same resolution, so the history stays as reviewed. The PR's diff against main now matches your rebase. Head is 26bc5ce87, and it merges cleanly with main.

AI-assisted: yes

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6235844341 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6685#issuecomment-6235844341). Originally posted by [DavidSamir](https://github.com/DavidSamir) on 2026-10-17T15:34:33Z. --- Thanks, @blawesom. I checked your `rebase/6685` against this branch: the code commits are identical, and the debt-file commit differs only by the conflict resolution. Rather than force-pushing, I merged current `main` into the branch with the same resolution, so the history stays as reviewed. The PR's diff against `main` now matches your rebase. Head is `26bc5ce87`, and it merges cleanly with `main`. AI-assisted: yes
adriadam10 commented 2026-10-19 08:50:46 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-19T08:50:46Z.


Checked the diff: the macOS path is confined to __APPLE__, the routing-message parser stays inside rtm_msglen/sa_len and the buffer size, the filters (up, not reject/host, destination 0.0.0.0, existing interface) and the scoped-route ranking match the description, the test pins values on a hand-built table including the truncated last message, and the debt entries are in place. Linux/Windows code paths are unchanged. macOS runtime behaviour relies on the author's run; merged.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6263392533 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6685#issuecomment-6263392533). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-19T08:50:46Z. --- Checked the diff: the macOS path is confined to `__APPLE__`, the routing-message parser stays inside `rtm_msglen`/`sa_len` and the buffer size, the filters (up, not reject/host, destination 0.0.0.0, existing interface) and the scoped-route ranking match the description, the test pins values on a hand-built table including the truncated last message, and the debt entries are in place. Linux/Windows code paths are unchanged. macOS runtime behaviour relies on the author's run; merged.
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5!6685
No description provided.