fix(libkernel): connect an AF_INET6 socket to ::1 when the address is unspecified, as FreeBSD does #14876

Merged
roboterspinne merged 1 commit from upstream-pr/14876 into main 2026-11-03 05:11:17 +00:00
roboterspinne commented 2026-11-01 22:03:38 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by roboterspinne on 2026-11-01T22:03:38Z.


What

On e0caf13600 (line numbers below refer to it) a guest connect on an AF_INET6 stream or datagram socket to [::]:port fails with EADDRNOTAVAIL (49) on Windows. connect_nid_postfix converts the guest address (core/libs/prx/libkernel/Socket/src/Udp.cpp:1458) and passes it unchanged to the host ::connect (Udp.cpp:1466), and Winsock does not treat :: as this host, so a guest that reaches a local server through the unspecified address cannot connect. FreeBSD 11.4 replaces the address: in6_pcbladdr (sys/netinet6/in6_pcb.c) checks the length, the family and port 0, and then, when any IPv6 interface address exists, sets an unspecified destination to in6addr_loopback. in6_pcbconnect_mbuf calls it (in6_pcb.c:407), and both tcp6_connect and udp6_connect go through in6_pcbconnect, so a TCP or UDP connect to [::]:port connects to [::1]:port and getpeername reports ::1. Linux (tcp_v6_connect, ip6_datagram_connect) and macOS (the same KAME in6_pcbladdr) do the same in the host kernel, so the change does not alter the result there, except that Linux would use ::ffff:127.0.0.1 for a socket bound to a v4-mapped address, where the change uses ::1 as FreeBSD does; the debt entry records this. The rule follows FreeBSD 11.4 in6_pcbladdr (in6_pcb.c) and was not measured on a PS5; the replacement also assumes the console has an IPv6 address such as ::1 on lo0, the condition in6_pcbladdr checks, which was not checked either.

  • Rule. Directly after the Address() call in connect_nid_postfix, an AF_INET6 native address whose address is unspecified (IN6_IS_ADDR_UNSPECIFIED) gets in6addr_loopback, on every host. It runs before the Windows RefuseClosedLoopbackAtOnce_nid_no_patch, which therefore sees ::1 and keeps its fast refusal of a closed loopback port. A connect to [::]:0 still fails with EADDRNOTAVAIL, as ZeroPortConnect reads the guest bytes and in6_pcbladdr checks the port before the replacement. A sendto or sendmsg to :: is not changed, as FreeBSD's udp6_output does not replace it, and neither is an AF_INET connect or send to INADDR_ANY, where FreeBSD 11.4's in_pcbconnect_setup uses the first interface address, which depends on the machine; a new debt entry next to the connect entries in docs/dev/debt/libkernel.md records both and that sceNetConnect is not changed. The existing connect entry is 943 characters, so the gap is its own entry rather than a sentence there.
  • Tests. guest_socket_port_zero gains CheckUnspecified for a stream and a datagram socket: a server bound to [::1]:0, a client connect to [::]:0 must fail with EADDRNOTAVAIL (49), the same client then connects to the server's port with the address bytes zeroed and must succeed, getpeername on the client must report ::1 with the server's port, and a byte sent by the client must arrive (on the accepted socket for TCP, on the server bound to ::1 for UDP). It returns early, as the existing cases do, when the host has no IPv6.
  • Overlap. pr_overlap.py --branch finds no shared lines with the open pull requests it can compare. It cannot compare 40 open pull requests that do not merge with main and share Udp.cpp or docs/dev/debt/libkernel.md (#4630, #5021, #6936, #7088, #7105, #7121, #7135, #7149, #7192, #7447, #7464, #7507, #7804, #8463, #9156, #9199, #9293, #9297, #9302, #9389, #9557, #9566, #9843, #9938, #10025, #10319, #10442, #11628, #11661, #11689, #11698, #11896, #11939, #12104, #12560, #12682, #12688, #12700, #12965, #14212). Checked by hand: a trial merge of this branch with each of them conflicts in exactly the files that main (e0caf13600) with it already conflicts in, so this branch adds no conflict. My open #14417 also adds a docs/dev/debt/libkernel.md entry and merges cleanly with this branch. My pull request "fix(libkernel): fail bind and connect of a stream socket to a multicast address with EAFNOSUPPORT" also changes connect_nid_postfix in Udp.cpp and docs/dev/debt/libkernel.md, in other lines; a trial merge of the two branches is clean.

Tested

Windows 11, WinLibs GCC 15.2.0, Ninja Release build (BUILD_TESTING=ON) on 255fa04a15 (based on e0caf13600):

  • cmake --build build --parallel 4 --target guest_socket_port_zero_tests guest_socket_address_length_tests guest_tcp_tests guest_udp_tests guest_socket_connect_listening_tests guest_socket_error_tests, then ctest --test-dir build -R '^(guest_tcp|guest_udp|guest_socket_connect_listening|guest_socket_error|guest_socket_port_zero|guest_socket_address_length)$' --output-on-failure: 6 of 6 passed.
  • Counter-check with the new test and the Udp.cpp of e0caf13600: guest_socket_port_zero_tests fails with connect to the unspecified AF_INET6 address must reach ::1 on the same port (errno 49) in the stream case; with the datagram case run first it fails with the same line, so both connects are refused by Winsock. guest_socket_port_zero and guest_socket_address_length pass on e0caf13600 without the new test.
  • Linux and macOS were not run here; the host kernels there already connect :: to ::1.
  • python tools/check_conventions.py --base e0caf13600 with this title and description passed.

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
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes (Claude Code)
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5112017114 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/14876). Originally posted by [roboterspinne](https://github.com/roboterspinne) on 2026-11-01T22:03:38Z. --- ### What On e0caf13600 (line numbers below refer to it) a guest `connect` on an `AF_INET6` stream or datagram socket to `[::]:port` fails with `EADDRNOTAVAIL` (49) on Windows. `connect_nid_postfix` converts the guest address (`core/libs/prx/libkernel/Socket/src/Udp.cpp:1458`) and passes it unchanged to the host `::connect` (`Udp.cpp:1466`), and Winsock does not treat `::` as this host, so a guest that reaches a local server through the unspecified address cannot connect. FreeBSD 11.4 replaces the address: [`in6_pcbladdr`](https://github.com/freebsd/freebsd-src/blob/releng/11.4/sys/netinet6/in6_pcb.c#L338-L356) (`sys/netinet6/in6_pcb.c`) checks the length, the family and port 0, and then, when any IPv6 interface address exists, sets an unspecified destination to `in6addr_loopback`. `in6_pcbconnect_mbuf` calls it ([`in6_pcb.c:407`](https://github.com/freebsd/freebsd-src/blob/releng/11.4/sys/netinet6/in6_pcb.c#L407)), and both [`tcp6_connect`](https://github.com/freebsd/freebsd-src/blob/releng/11.4/sys/netinet/tcp_usrreq.c#L1339) and [`udp6_connect`](https://github.com/freebsd/freebsd-src/blob/releng/11.4/sys/netinet6/udp6_usrreq.c#L1157) go through `in6_pcbconnect`, so a TCP or UDP connect to `[::]:port` connects to `[::1]:port` and `getpeername` reports `::1`. Linux (`tcp_v6_connect`, `ip6_datagram_connect`) and macOS (the same KAME `in6_pcbladdr`) do the same in the host kernel, so the change does not alter the result there, except that Linux would use `::ffff:127.0.0.1` for a socket bound to a v4-mapped address, where the change uses `::1` as FreeBSD does; the debt entry records this. The rule follows FreeBSD 11.4 `in6_pcbladdr` (`in6_pcb.c`) and was not measured on a PS5; the replacement also assumes the console has an IPv6 address such as `::1` on `lo0`, the condition `in6_pcbladdr` checks, which was not checked either. - **Rule.** Directly after the `Address()` call in `connect_nid_postfix`, an `AF_INET6` native address whose address is unspecified (`IN6_IS_ADDR_UNSPECIFIED`) gets `in6addr_loopback`, on every host. It runs before the Windows `RefuseClosedLoopbackAtOnce_nid_no_patch`, which therefore sees `::1` and keeps its fast refusal of a closed loopback port. A connect to `[::]:0` still fails with `EADDRNOTAVAIL`, as `ZeroPortConnect` reads the guest bytes and `in6_pcbladdr` checks the port before the replacement. A `sendto` or `sendmsg` to `::` is not changed, as FreeBSD's `udp6_output` does not replace it, and neither is an `AF_INET` connect or send to `INADDR_ANY`, where FreeBSD 11.4's `in_pcbconnect_setup` uses the first interface address, which depends on the machine; a new debt entry next to the `connect` entries in `docs/dev/debt/libkernel.md` records both and that `sceNetConnect` is not changed. The existing `connect` entry is 943 characters, so the gap is its own entry rather than a sentence there. - **Tests.** `guest_socket_port_zero` gains `CheckUnspecified` for a stream and a datagram socket: a server bound to `[::1]:0`, a client connect to `[::]:0` must fail with `EADDRNOTAVAIL` (49), the same client then connects to the server's port with the address bytes zeroed and must succeed, `getpeername` on the client must report `::1` with the server's port, and a byte sent by the client must arrive (on the accepted socket for TCP, on the server bound to `::1` for UDP). It returns early, as the existing cases do, when the host has no IPv6. - **Overlap.** `pr_overlap.py --branch` finds no shared lines with the open pull requests it can compare. It cannot compare 40 open pull requests that do not merge with `main` and share `Udp.cpp` or `docs/dev/debt/libkernel.md` (#4630, #5021, #6936, #7088, #7105, #7121, #7135, #7149, #7192, #7447, #7464, #7507, #7804, #8463, #9156, #9199, #9293, #9297, #9302, #9389, #9557, #9566, #9843, #9938, #10025, #10319, #10442, #11628, #11661, #11689, #11698, #11896, #11939, #12104, #12560, #12682, #12688, #12700, #12965, #14212). Checked by hand: a trial merge of this branch with each of them conflicts in exactly the files that `main` (e0caf13600) with it already conflicts in, so this branch adds no conflict. My open #14417 also adds a `docs/dev/debt/libkernel.md` entry and merges cleanly with this branch. My pull request "fix(libkernel): fail bind and connect of a stream socket to a multicast address with EAFNOSUPPORT" also changes `connect_nid_postfix` in `Udp.cpp` and `docs/dev/debt/libkernel.md`, in other lines; a trial merge of the two branches is clean. ### Tested <!-- OS, title or homebrew, what was checked. "Not tested" is a valid answer. A performance change: the summary lines of main and of this PR from the same machine and scene, with the traces attached, captured as the Performance report issue form describes. --> Windows 11, WinLibs GCC 15.2.0, Ninja Release build (`BUILD_TESTING=ON`) on 255fa04a15 (based on e0caf13600): - `cmake --build build --parallel 4 --target guest_socket_port_zero_tests guest_socket_address_length_tests guest_tcp_tests guest_udp_tests guest_socket_connect_listening_tests guest_socket_error_tests`, then `ctest --test-dir build -R '^(guest_tcp|guest_udp|guest_socket_connect_listening|guest_socket_error|guest_socket_port_zero|guest_socket_address_length)$' --output-on-failure`: 6 of 6 passed. - Counter-check with the new test and the `Udp.cpp` of e0caf13600: `guest_socket_port_zero_tests` fails with `connect to the unspecified AF_INET6 address must reach ::1 on the same port (errno 49)` in the stream case; with the datagram case run first it fails with the same line, so both connects are refused by Winsock. `guest_socket_port_zero` and `guest_socket_address_length` pass on e0caf13600 without the new test. - Linux and macOS were not run here; the host kernels there already connect `::` to `::1`. - `python tools/check_conventions.py --base e0caf13600` with this title and description passed. ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [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: <!-- #PR, or none --> none - [x] Fixes: <!-- issue it closes as Fixes #N, or none --> none - [x] Unblocks: <!-- title ID and Game bring-up discussion, e.g. PPSA01325 #3232, or none --> none - [x] GPUs tested: <!-- for AGC, shader recompiler or VideoOut changes: vendor, model, driver and OS of each GPU the GPU tests ran on; otherwise n/a --> n/a - [x] AI-assisted: <!-- yes / no --> yes (Claude Code)
BunnyFl commented 2026-11-02 01:33:25 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by BunnyFl on 2026-11-02T01:33:25Z.

Review: APPROVED


Windows 11, WinLibs GCC 15.2.0 r7, Release. Head 255fa04a15 merged into upstream/main 93dbd0f82a:

  • guest_socket_address_length, guest_socket_port_zero, guest_tcp, guest_udp, guest_socket_connect_listening, guest_socket_error: 6/6 passed.
  • Negative control: the same tests with main's Udp.cpp. guest_socket_port_zero fails with connect to the unspecified AF_INET6 address must reach ::1 on the same port (errno 49); the other five pass.
  • Merged together with #14875 (both touch connect_nid_postfix): clean, and the same 6/6 pass.

Doing the replacement on the native address after Address() keeps ZeroPortConnect (which reads the guest bytes) answering 49 for [::]:0, and the Windows closed-loopback refusal now sees ::1. That matches the order in6_pcbladdr checks in.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5731258356 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/14876#pullrequestreview-5731258356). Originally posted by [BunnyFl](https://github.com/BunnyFl) on 2026-11-02T01:33:25Z. **Review:** APPROVED --- Windows 11, WinLibs GCC 15.2.0 r7, Release. Head 255fa04a15 merged into upstream/main 93dbd0f82a: - `guest_socket_address_length`, `guest_socket_port_zero`, `guest_tcp`, `guest_udp`, `guest_socket_connect_listening`, `guest_socket_error`: 6/6 passed. - Negative control: the same tests with main's `Udp.cpp`. `guest_socket_port_zero` fails with `connect to the unspecified AF_INET6 address must reach ::1 on the same port (errno 49)`; the other five pass. - Merged together with #14875 (both touch `connect_nid_postfix`): clean, and the same 6/6 pass. Doing the replacement on the native address after `Address()` keeps `ZeroPortConnect` (which reads the guest bytes) answering 49 for `[::]:0`, and the Windows closed-loopback refusal now sees `::1`. That matches the order `in6_pcbladdr` checks in.
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!14876
No description provided.