fix(libkernel): fail bind and connect of a stream socket to a multicast address with EAFNOSUPPORT #14875

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

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


What

On e0caf13600 (line numbers below refer to it) libkernel bind and connect pass a stream socket's multicast address to the host. Address() (core/libs/prx/libkernel/Socket/src/Udp.cpp:285-311) checks only the length and the family, and neither connect_nid_postfix (Udp.cpp:1446-1469) nor bind_nid_postfix (Udp.cpp:1547-1561) looks at the address class before ::connect and ::bind. The guest errno therefore comes from the host. On Windows, a TCP bind to 239.1.2.3 fails with EADDRNOTAVAIL (49), a blocking connect to 224.0.0.1 fails with EADDRNOTAVAIL, a non-blocking one returns EINPROGRESS (36), and a connect to 224.0.0.1:0 reaches ZeroPortConnect's EADDRNOTAVAIL (Udp.cpp:406-419). Other hosts answer with their own errors. FreeBSD 11.4 refuses a multicast address on a TCP socket with EAFNOSUPPORT in tcp_usr_bind, tcp6_usr_bind, tcp_usr_connect and tcp6_usr_connect. Each check runs right after the sa_len check, before in_pcbbind and tcp_connect. soconnect still answers EOPNOTSUPP for a listening socket and EISCONN for a connected one first (uipc_socket.c), because those checks come before pru_connect. The rule follows FreeBSD 11.4's tcp_usrreq.c. It was not measured on a PS5. The file's other address checks were measured on a PS5 (docs/dev/debt/libkernel.md:269); where the console was not measured, it follows FreeBSD 11.4 (connected stream sendto on line 269, IPv4-mapped datagram addresses on line 270).

  • Rule. For a stream socket, bind and connect fail with EAFNOSUPPORT (47) when the address is IPv4 multicast (224.0.0.0/4) or IPv6 multicast (ff00::/8). The check uses a new Multicast() helper next to PeerConnected and runs before the host is asked:

    • In bind it runs right after Address(). A socket that is already bound gets 47, not EINVAL, because FreeBSD checks before in_pcbbind.
    • In connect it runs after Address() and the Windows loopback hook, and before ZeroPortConnect, so 224.0.0.1:0 gets 47. The existing listening check (Udp.cpp:1453) still answers EOPNOTSUPP first. A connected socket skips the new check and still gets the host's EISCONN.

    Unchanged: datagram sockets (FreeBSD lets them bind or connect to a multicast address), an IPv4-mapped multicast address on an AF_INET6 socket (FreeBSD's IN6_IS_ADDR_MULTICAST does not match it, so it still goes to the host) and sceNetBind/sceNetConnect. One gap is recorded in docs/dev/debt/libkernel.md. FreeBSD's kern_connectat returns EALREADY for every connect while the socket is SS_ISCONNECTING, blocking or not, before soconnect runs. libkernel keeps no connecting state, and the new check is skipped only once getpeername succeeds. So a connect to a multicast address while another connect on the socket is still in progress (a blocking one in another thread, or a pending non-blocking one) now gets 47 instead of EALREADY. Before this change that call went to the host; what the host answered was not measured.

  • Tests. guest_socket_address_length gains StreamSocketsRefuseMulticastAddresses. It expects 47 for:

    • TCP bind to 239.1.2.3:0 and to [ff0e::1]:0;
    • blocking and non-blocking TCP connect to 224.0.0.1:8080 and to [ff02::1]:8080;
    • connect to 224.0.0.1:0;
    • a socket bound to 127.0.0.1 binding 239.1.2.3.

    It also expects 45 for a listening socket and 56 for a loopback-connected socket that connects to 224.0.0.1:8080. It also expects a UDP bind to 239.1.2.3:0 not to fail with 47; it does not check the exact result, which depends on the host. UDP multicast connect stays out of the test for the same reason. The test needs no network: every refusal happens before a host call, and the connected case uses loopback.

  • Overlap. pr_overlap.py --branch finds shared lines with one open pull request: #11002 (SIGPIPE on Linux) rewrites the two docs/dev/debt/libkernel.md entries right below the one this branch adds (lines 271-272 on e0caf13600); the entries cover different functions, so whichever merges second only needs that file rebased. 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 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): connect an AF_INET6 socket to ::1 when the address is unspecified, as FreeBSD does" 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 with BUILD_TESTING=ON, on this branch based on e0caf13600:

  • Counter-check: the new test was built against the untouched Udp.cpp of e0caf13600 (cmake --build build --parallel 4 --target guest_socket_address_length_tests) and build/tests/guest_socket_address_length_tests.exe was run. It aborts at the first new check with tcp_usr_bind refuses an IPv4 multicast address, so bind to 239.1.2.3 must fail with EAFNOSUPPORT (47) (errno 49). The existing checks pass before it.
  • The test stops at its first failure, so the connect cases were checked separately with a small probe linked against libkernel and libc. It calls bind and connect with the addresses above and was run against the libkernel built from e0caf13600 and from this branch:
    • bind to 239.1.2.3:0 and to [ff0e::1]:0: 49 before, 47 after;
    • non-blocking connect to 224.0.0.1:8080 and to [ff02::1]:8080: 36 before, 47 after;
    • blocking connect to 224.0.0.1:8080: 49 before, 47 after;
    • UDP bind to 239.1.2.3:0: 49 both times (unchanged).
  • Counter-check of the UDP case: with the socket->type == 1 guard removed from the bind check, the test aborts with the multicast check is in tcp_usr_bind only, so a datagram bind to 239.1.2.3 must not fail with EAFNOSUPPORT (47) (errno 47); with the guard restored it passes.
  • With the change: the same test build and run exits 0.
  • ctest --test-dir build -R '^(guest_socket_address_length|guest_socket_port_zero|guest_tcp|guest_poll)$' --output-on-failure: 4 of 4 passed (0.02 s, 0.05 s, 0.21 s, 0.40 s).
  • python tools/check_conventions.py --base e0caf13600 --head HEAD 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:5112014004 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/14875). Originally posted by [roboterspinne](https://github.com/roboterspinne) on 2026-11-01T22:03:13Z. --- ### What On e0caf13600 (line numbers below refer to it) libkernel `bind` and `connect` pass a stream socket's multicast address to the host. `Address()` (`core/libs/prx/libkernel/Socket/src/Udp.cpp:285-311`) checks only the length and the family, and neither `connect_nid_postfix` (`Udp.cpp:1446-1469`) nor `bind_nid_postfix` (`Udp.cpp:1547-1561`) looks at the address class before `::connect` and `::bind`. The guest errno therefore comes from the host. On Windows, a TCP `bind` to `239.1.2.3` fails with `EADDRNOTAVAIL` (49), a blocking `connect` to `224.0.0.1` fails with `EADDRNOTAVAIL`, a non-blocking one returns `EINPROGRESS` (36), and a `connect` to `224.0.0.1:0` reaches `ZeroPortConnect`'s `EADDRNOTAVAIL` (`Udp.cpp:406-419`). Other hosts answer with their own errors. FreeBSD 11.4 refuses a multicast address on a TCP socket with `EAFNOSUPPORT` in [`tcp_usr_bind`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/sys/netinet/tcp_usrreq.c#L308-L310), [`tcp6_usr_bind`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/sys/netinet/tcp_usrreq.c#L351-L353), [`tcp_usr_connect`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/sys/netinet/tcp_usrreq.c#L524-L526) and [`tcp6_usr_connect`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/sys/netinet/tcp_usrreq.c#L581-L583). Each check runs right after the `sa_len` check, before `in_pcbbind` and `tcp_connect`. `soconnect` still answers `EOPNOTSUPP` for a listening socket and `EISCONN` for a connected one first ([`uipc_socket.c`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/sys/kern/uipc_socket.c#L993-L1006)), because those checks come before `pru_connect`. The rule follows FreeBSD 11.4's `tcp_usrreq.c`. It was not measured on a PS5. The file's other address checks were measured on a PS5 (`docs/dev/debt/libkernel.md:269`); where the console was not measured, it follows FreeBSD 11.4 (connected stream `sendto` on line 269, IPv4-mapped datagram addresses on line 270). - **Rule.** For a stream socket, `bind` and `connect` fail with `EAFNOSUPPORT` (47) when the address is IPv4 multicast (`224.0.0.0/4`) or IPv6 multicast (`ff00::/8`). The check uses a new `Multicast()` helper next to `PeerConnected` and runs before the host is asked: - In `bind` it runs right after `Address()`. A socket that is already bound gets 47, not `EINVAL`, because FreeBSD checks before `in_pcbbind`. - In `connect` it runs after `Address()` and the Windows loopback hook, and before `ZeroPortConnect`, so `224.0.0.1:0` gets 47. The existing listening check (`Udp.cpp:1453`) still answers `EOPNOTSUPP` first. A connected socket skips the new check and still gets the host's `EISCONN`. Unchanged: datagram sockets (FreeBSD lets them bind or connect to a multicast address), an IPv4-mapped multicast address on an `AF_INET6` socket (FreeBSD's `IN6_IS_ADDR_MULTICAST` does not match it, so it still goes to the host) and `sceNetBind`/`sceNetConnect`. One gap is recorded in `docs/dev/debt/libkernel.md`. FreeBSD's [`kern_connectat`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/sys/kern/uipc_syscalls.c#L572-L575) returns `EALREADY` for every `connect` while the socket is `SS_ISCONNECTING`, blocking or not, before `soconnect` runs. libkernel keeps no connecting state, and the new check is skipped only once `getpeername` succeeds. So a `connect` to a multicast address while another `connect` on the socket is still in progress (a blocking one in another thread, or a pending non-blocking one) now gets 47 instead of `EALREADY`. Before this change that call went to the host; what the host answered was not measured. - **Tests.** `guest_socket_address_length` gains `StreamSocketsRefuseMulticastAddresses`. It expects 47 for: - TCP `bind` to `239.1.2.3:0` and to `[ff0e::1]:0`; - blocking and non-blocking TCP `connect` to `224.0.0.1:8080` and to `[ff02::1]:8080`; - `connect` to `224.0.0.1:0`; - a socket bound to `127.0.0.1` binding `239.1.2.3`. It also expects 45 for a listening socket and 56 for a loopback-connected socket that connects to `224.0.0.1:8080`. It also expects a UDP `bind` to `239.1.2.3:0` not to fail with 47; it does not check the exact result, which depends on the host. UDP multicast `connect` stays out of the test for the same reason. The test needs no network: every refusal happens before a host call, and the connected case uses loopback. - **Overlap.** `pr_overlap.py --branch` finds shared lines with one open pull request: #11002 (SIGPIPE on Linux) rewrites the two `docs/dev/debt/libkernel.md` entries right below the one this branch adds (lines 271-272 on e0caf13600); the entries cover different functions, so whichever merges second only needs that file rebased. 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` 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): connect an AF_INET6 socket to ::1 when the address is unspecified, as FreeBSD does" 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 with `BUILD_TESTING=ON`, on this branch based on e0caf13600: - Counter-check: the new test was built against the untouched `Udp.cpp` of e0caf13600 (`cmake --build build --parallel 4 --target guest_socket_address_length_tests`) and `build/tests/guest_socket_address_length_tests.exe` was run. It aborts at the first new check with `tcp_usr_bind refuses an IPv4 multicast address, so bind to 239.1.2.3 must fail with EAFNOSUPPORT (47) (errno 49)`. The existing checks pass before it. - The test stops at its first failure, so the connect cases were checked separately with a small probe linked against libkernel and libc. It calls `bind` and `connect` with the addresses above and was run against the libkernel built from e0caf13600 and from this branch: - `bind` to `239.1.2.3:0` and to `[ff0e::1]:0`: 49 before, 47 after; - non-blocking `connect` to `224.0.0.1:8080` and to `[ff02::1]:8080`: 36 before, 47 after; - blocking `connect` to `224.0.0.1:8080`: 49 before, 47 after; - UDP `bind` to `239.1.2.3:0`: 49 both times (unchanged). - Counter-check of the UDP case: with the `socket->type == 1` guard removed from the `bind` check, the test aborts with `the multicast check is in tcp_usr_bind only, so a datagram bind to 239.1.2.3 must not fail with EAFNOSUPPORT (47) (errno 47)`; with the guard restored it passes. - With the change: the same test build and run exits 0. - `ctest --test-dir build -R '^(guest_socket_address_length|guest_socket_port_zero|guest_tcp|guest_poll)$' --output-on-failure`: 4 of 4 passed (0.02 s, 0.05 s, 0.21 s, 0.40 s). - `python tools/check_conventions.py --base e0caf13600 --head HEAD` 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:27 +00:00 (Migrated from github.com)

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

Review: APPROVED


Windows 11, WinLibs GCC 15.2.0 r7, Release. Head 14fe943513 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: with main's Udp.cpp, guest_socket_address_length aborts with tcp_usr_bind refuses an IPv4 multicast address, so bind to 239.1.2.3 must fail with EAFNOSUPPORT (47) (errno 49).
  • Merged together with #14876: clean, 6/6 pass.

I checked the v4-mapped claim against FreeBSD 11.4's tcp_usrreq.c. tcp6_usr_bind and tcp6_usr_connect refuse only IN6_IS_ADDR_MULTICAST, and the v4-mapped branch of tcp6_usr_connect has no IN_MULTICAST check (that's only in the IPv4 functions). So leaving ::ffff:224.x to the host matches the source. The EALREADY gap you recorded in the debt entry is the right thing to write down.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5731258498 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/14875#pullrequestreview-5731258498). Originally posted by [BunnyFl](https://github.com/BunnyFl) on 2026-11-02T01:33:27Z. **Review:** APPROVED --- Windows 11, WinLibs GCC 15.2.0 r7, Release. Head 14fe943513 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: with main's `Udp.cpp`, `guest_socket_address_length` aborts with `tcp_usr_bind refuses an IPv4 multicast address, so bind to 239.1.2.3 must fail with EAFNOSUPPORT (47) (errno 49)`. - Merged together with #14876: clean, 6/6 pass. I checked the v4-mapped claim against FreeBSD 11.4's `tcp_usrreq.c`. `tcp6_usr_bind` and `tcp6_usr_connect` refuse only `IN6_IS_ADDR_MULTICAST`, and the v4-mapped branch of `tcp6_usr_connect` has no `IN_MULTICAST` check (that's only in the IPv4 functions). So leaving `::ffff:224.x` to the host matches the source. The `EALREADY` gap you recorded in the debt entry is the right thing to write down.
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!14875
No description provided.