fix(libkernel): connect an AF_INET6 socket to ::1 when the address is unspecified, as FreeBSD does #14876
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/14876"
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:38Z.
What
On
e0caf13600(line numbers below refer to it) a guestconnecton anAF_INET6stream or datagram socket to[::]:portfails withEADDRNOTAVAIL(49) on Windows.connect_nid_postfixconverts 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 toin6addr_loopback.in6_pcbconnect_mbufcalls it (in6_pcb.c:407), and bothtcp6_connectandudp6_connectgo throughin6_pcbconnect, so a TCP or UDP connect to[::]:portconnects to[::1]:portandgetpeernamereports::1. Linux (tcp_v6_connect,ip6_datagram_connect) and macOS (the same KAMEin6_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.1for a socket bound to a v4-mapped address, where the change uses::1as FreeBSD does; the debt entry records this. The rule follows FreeBSD 11.4in6_pcbladdr(in6_pcb.c) and was not measured on a PS5; the replacement also assumes the console has an IPv6 address such as::1onlo0, the conditionin6_pcbladdrchecks, which was not checked either.Address()call inconnect_nid_postfix, anAF_INET6native address whose address is unspecified (IN6_IS_ADDR_UNSPECIFIED) getsin6addr_loopback, on every host. It runs before the WindowsRefuseClosedLoopbackAtOnce_nid_no_patch, which therefore sees::1and keeps its fast refusal of a closed loopback port. A connect to[::]:0still fails withEADDRNOTAVAIL, asZeroPortConnectreads the guest bytes andin6_pcbladdrchecks the port before the replacement. Asendtoorsendmsgto::is not changed, as FreeBSD'sudp6_outputdoes not replace it, and neither is anAF_INETconnect or send toINADDR_ANY, where FreeBSD 11.4'sin_pcbconnect_setupuses the first interface address, which depends on the machine; a new debt entry next to theconnectentries indocs/dev/debt/libkernel.mdrecords both and thatsceNetConnectis not changed. The existingconnectentry is 943 characters, so the gap is its own entry rather than a sentence there.guest_socket_port_zerogainsCheckUnspecifiedfor a stream and a datagram socket: a server bound to[::1]:0, a client connect to[::]:0must fail withEADDRNOTAVAIL(49), the same client then connects to the server's port with the address bytes zeroed and must succeed,getpeernameon the client must report::1with the server's port, and a byte sent by the client must arrive (on the accepted socket for TCP, on the server bound to::1for UDP). It returns early, as the existing cases do, when the host has no IPv6.pr_overlap.py --branchfinds no shared lines with the open pull requests it can compare. 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 thatmain(e0caf13600) with 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): fail bind and connect of a stream socket to a multicast address with EAFNOSUPPORT" 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 (
BUILD_TESTING=ON) on255fa04a15(based one0caf13600):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, thenctest --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.Udp.cppofe0caf13600:guest_socket_port_zero_testsfails withconnect 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_zeroandguest_socket_address_lengthpass one0caf13600without the new test.::to::1.python tools/check_conventions.py --base e0caf13600with 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:25Z.
Review: APPROVED
Windows 11, WinLibs GCC 15.2.0 r7, Release. Head
255fa04a15merged 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_port_zerofails withconnect to the unspecified AF_INET6 address must reach ::1 on the same port (errno 49); the other five pass.connect_nid_postfix): clean, and the same 6/6 pass.Doing the replacement on the native address after
Address()keepsZeroPortConnect(which reads the guest bytes) answering 49 for[::]:0, and the Windows closed-loopback refusal now sees::1. That matches the orderin6_pcbladdrchecks in.