fix(libkernel): fail bind and connect of a stream socket to a multicast address with EAFNOSUPPORT #14875
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/14875"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from boykopovar/AnyPS5. Originally posted by roboterspinne on 2026-11-01T22:03:13Z.
What
On
e0caf13600(line numbers below refer to it) libkernelbindandconnectpass 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 neitherconnect_nid_postfix(Udp.cpp:1446-1469) norbind_nid_postfix(Udp.cpp:1547-1561) looks at the address class before::connectand::bind. The guest errno therefore comes from the host. On Windows, a TCPbindto239.1.2.3fails withEADDRNOTAVAIL(49), a blockingconnectto224.0.0.1fails withEADDRNOTAVAIL, a non-blocking one returnsEINPROGRESS(36), and aconnectto224.0.0.1:0reachesZeroPortConnect'sEADDRNOTAVAIL(Udp.cpp:406-419). Other hosts answer with their own errors. FreeBSD 11.4 refuses a multicast address on a TCP socket withEAFNOSUPPORTintcp_usr_bind,tcp6_usr_bind,tcp_usr_connectandtcp6_usr_connect. Each check runs right after thesa_lencheck, beforein_pcbbindandtcp_connect.soconnectstill answersEOPNOTSUPPfor a listening socket andEISCONNfor a connected one first (uipc_socket.c), because those checks come beforepru_connect. The rule follows FreeBSD 11.4'stcp_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 streamsendtoon line 269, IPv4-mapped datagram addresses on line 270).Rule. For a stream socket,
bindandconnectfail withEAFNOSUPPORT(47) when the address is IPv4 multicast (224.0.0.0/4) or IPv6 multicast (ff00::/8). The check uses a newMulticast()helper next toPeerConnectedand runs before the host is asked:bindit runs right afterAddress(). A socket that is already bound gets 47, notEINVAL, because FreeBSD checks beforein_pcbbind.connectit runs afterAddress()and the Windows loopback hook, and beforeZeroPortConnect, so224.0.0.1:0gets 47. The existing listening check (Udp.cpp:1453) still answersEOPNOTSUPPfirst. A connected socket skips the new check and still gets the host'sEISCONN.Unchanged: datagram sockets (FreeBSD lets them bind or connect to a multicast address), an IPv4-mapped multicast address on an
AF_INET6socket (FreeBSD'sIN6_IS_ADDR_MULTICASTdoes not match it, so it still goes to the host) andsceNetBind/sceNetConnect. One gap is recorded indocs/dev/debt/libkernel.md. FreeBSD'skern_connectatreturnsEALREADYfor everyconnectwhile the socket isSS_ISCONNECTING, blocking or not, beforesoconnectruns. libkernel keeps no connecting state, and the new check is skipped only oncegetpeernamesucceeds. So aconnectto a multicast address while anotherconnecton the socket is still in progress (a blocking one in another thread, or a pending non-blocking one) now gets 47 instead ofEALREADY. Before this change that call went to the host; what the host answered was not measured.Tests.
guest_socket_address_lengthgainsStreamSocketsRefuseMulticastAddresses. It expects 47 for:bindto239.1.2.3:0and to[ff0e::1]:0;connectto224.0.0.1:8080and to[ff02::1]:8080;connectto224.0.0.1:0;127.0.0.1binding239.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 UDPbindto239.1.2.3:0not to fail with 47; it does not check the exact result, which depends on the host. UDP multicastconnectstays 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 --branchfinds shared lines with one open pull request: #11002 (SIGPIPE on Linux) rewrites the twodocs/dev/debt/libkernel.mdentries right below the one this branch adds (lines 271-272 one0caf13600); 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 withmainand shareUdp.cppordocs/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 thatmainwith it already conflicts in, so this branch adds no conflict. My open #14417 also adds adocs/dev/debt/libkernel.mdentry 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 changesconnect_nid_postfixinUdp.cppanddocs/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 one0caf13600:Udp.cppofe0caf13600(cmake --build build --parallel 4 --target guest_socket_address_length_tests) andbuild/tests/guest_socket_address_length_tests.exewas run. It aborts at the first new check withtcp_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.bindandconnectwith the addresses above and was run against the libkernel built frome0caf13600and from this branch:bindto239.1.2.3:0and to[ff0e::1]:0: 49 before, 47 after;connectto224.0.0.1:8080and to[ff02::1]:8080: 36 before, 47 after;connectto224.0.0.1:8080: 49 before, 47 after;bindto239.1.2.3:0: 49 both times (unchanged).socket->type == 1guard removed from thebindcheck, the test aborts withthe 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.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 HEADwith this title and description passed.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)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
14fe943513merged into upstream/main93dbd0f82a:guest_socket_address_length,guest_socket_port_zero,guest_tcp,guest_udp,guest_socket_connect_listening,guest_socket_error: 6/6 passed.Udp.cpp,guest_socket_address_lengthaborts withtcp_usr_bind refuses an IPv4 multicast address, so bind to 239.1.2.3 must fail with EAFNOSUPPORT (47) (errno 49).I checked the v4-mapped claim against FreeBSD 11.4's
tcp_usrreq.c.tcp6_usr_bindandtcp6_usr_connectrefuse onlyIN6_IS_ADDR_MULTICAST, and the v4-mapped branch oftcp6_usr_connecthas noIN_MULTICASTcheck (that's only in the IPv4 functions). So leaving::ffff:224.xto the host matches the source. TheEALREADYgap you recorded in the debt entry is the right thing to write down.