fix(libkernel): disconnect a connected datagram socket on connect to port 0 #9105

Merged
Retr0G28 merged 1 commit from upstream-pr/9105 into main 2026-10-24 03:54:25 +00:00
Retr0G28 commented 2026-10-21 12:30:02 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Retr0G28 on 2026-10-21T12:30:02Z.


What

connect to port 0 on a datagram socket that was already connected kept the old peer. FreeBSD 11.4's soconnectat() (sys/kern/uipc_socket.c) disconnects a connected socket of a protocol without PR_CONNREQUIRED before it calls pru_connect. udp_disconnect() (sys/netinet/udp_usrreq.c) and udp6_disconnect() (sys/netinet6/udp6_usrreq.c) drop the peer, reset the local address to the wildcard address and clear SS_ISCONNECTED. Then udp_connect() reaches in_pcbconnect_setup() (sys/netinet/in_pcb.c), and udp6_connect() reaches in6_pcbladdr() (sys/netinet6/in6_pcb.c). Both fail port 0 with EADDRNOTAVAIL (49) before anything is bound, so the socket ends up unconnected: getpeername gives ENOTCONN (57) and send without an address gives EDESTADDRREQ (39) from sosend_dgram().

ZeroPortConnect in Udp.cpp now dissolves the association of a connected AF_INET or AF_INET6 datagram socket through Disconnect, the helper that connect to AF_UNSPEC already uses, and then fails with 49. A failed host disconnect returns that error. Stream sockets are unchanged: a connected one still gets the host's EISCONN. The gap is cut from the connect entry in docs/dev/debt/libkernel.md.

Test change: guest_socket_port_zero (CheckDatagram) now connects a UDP socket to a bound loopback peer, for AF_INET and AF_INET6. It then expects connect to port 0 to give -1/49, getpeername to give -1/57 and send to give -1/39. If the host has no IPv6 loopback, the IPv6 case is skipped the same way CheckStream skips it.

Tested

Windows 11, WinLibs MinGW-w64 GCC 15.2.0 r7, CMake + Ninja, Release, -DBUILD_TESTING=ON, rebased on main 11f976b27c.

  • Before the fix, guest_socket_port_zero with the new checks fails with connect to port 0 on a connected datagram socket must fail with EADDRNOTAVAIL (49) (errno 57). With this PR it passes, and IPv6 loopback is available on this machine, so the IPv6 case ran.
  • Socket tests run serially (guest_inet_address, guest_refused_connect, guest_sce_net, guest_socket_* (13 tests), guest_tcp, guest_udp): 18 of 18 pass. guest_refused_connect timed out once in the full run and passed twice when run alone.
  • After the rebase onto #8499: guest_socket_* (12 tests, guest_socket_port_zero among them) pass 12 of 12.
  • python tools/check_conventions.py --base origin/main reports no errors. Its one warning is the length of the connect debt entry, 1141 characters on main, which this PR cuts to 1086.
  • Second Windows machine: @xXPBSXx ran the PR head on Windows 10 (same toolchain): guest_socket_port_zero passes 3 of 3, and fails 3 of 3 with main's Udp.cpp (comment below).
  • Not tested on Linux. The new path only calls Disconnect, which connect to AF_UNSPEC already runs on Linux (host AF_UNSPEC connect, then a rebind when the port was dropped). Linux CI must confirm that the test passes there.
  • No title or homebrew run.

Overlap: tools/pr_overlap.py lists #8499, #8682, #8728 and #8921, and none of them implements this. #8499 (now merged) added a sendto port-0 check right next to ZeroPortConnect and rewrote the sendto half of the same debt sentence; this PR is rebased onto it, keeps ZeroPortSend where it is and keeps its sendto sentence word for word. #8682 changes Disconnect to take a non-const Socket&. If it merges first, ZeroPortConnect has to drop its const too, and the textual conflict around the moved function will show this. #8728 and #8921 only touch neighbouring debt lines. The tool could not compare some open PRs. I checked the ones that touch Udp.cpp (#5021, #6977, #7088, #7201, #8344, #8483, #8658, #8670), and none changes connect to port 0 or Disconnect.

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
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4951557053 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9105). Originally posted by [Retr0G28](https://github.com/Retr0G28) on 2026-10-21T12:30:02Z. --- ### What `connect` to port 0 on a datagram socket that was already connected kept the old peer. FreeBSD 11.4's `soconnectat()` (`sys/kern/uipc_socket.c`) disconnects a connected socket of a protocol without `PR_CONNREQUIRED` before it calls `pru_connect`. `udp_disconnect()` (`sys/netinet/udp_usrreq.c`) and `udp6_disconnect()` (`sys/netinet6/udp6_usrreq.c`) drop the peer, reset the local address to the wildcard address and clear `SS_ISCONNECTED`. Then `udp_connect()` reaches `in_pcbconnect_setup()` (`sys/netinet/in_pcb.c`), and `udp6_connect()` reaches `in6_pcbladdr()` (`sys/netinet6/in6_pcb.c`). Both fail port 0 with `EADDRNOTAVAIL` (49) before anything is bound, so the socket ends up unconnected: `getpeername` gives `ENOTCONN` (57) and `send` without an address gives `EDESTADDRREQ` (39) from `sosend_dgram()`. `ZeroPortConnect` in `Udp.cpp` now dissolves the association of a connected `AF_INET` or `AF_INET6` datagram socket through `Disconnect`, the helper that `connect` to `AF_UNSPEC` already uses, and then fails with 49. A failed host disconnect returns that error. Stream sockets are unchanged: a connected one still gets the host's `EISCONN`. The gap is cut from the `connect` entry in `docs/dev/debt/libkernel.md`. Test change: `guest_socket_port_zero` (`CheckDatagram`) now connects a UDP socket to a bound loopback peer, for `AF_INET` and `AF_INET6`. It then expects `connect` to port 0 to give -1/49, `getpeername` to give -1/57 and `send` to give -1/39. If the host has no IPv6 loopback, the IPv6 case is skipped the same way `CheckStream` skips it. ### Tested Windows 11, WinLibs MinGW-w64 GCC 15.2.0 r7, CMake + Ninja, Release, `-DBUILD_TESTING=ON`, rebased on `main` 11f976b27c. - Before the fix, `guest_socket_port_zero` with the new checks fails with `connect to port 0 on a connected datagram socket must fail with EADDRNOTAVAIL (49) (errno 57)`. With this PR it passes, and IPv6 loopback is available on this machine, so the IPv6 case ran. - Socket tests run serially (`guest_inet_address`, `guest_refused_connect`, `guest_sce_net`, `guest_socket_*` (13 tests), `guest_tcp`, `guest_udp`): 18 of 18 pass. `guest_refused_connect` timed out once in the full run and passed twice when run alone. - After the rebase onto #8499: `guest_socket_*` (12 tests, `guest_socket_port_zero` among them) pass 12 of 12. - `python tools/check_conventions.py --base origin/main` reports no errors. Its one warning is the length of the `connect` debt entry, 1141 characters on `main`, which this PR cuts to 1086. - Second Windows machine: @xXPBSXx ran the PR head on Windows 10 (same toolchain): `guest_socket_port_zero` passes 3 of 3, and fails 3 of 3 with `main`'s `Udp.cpp` (comment below). - Not tested on Linux. The new path only calls `Disconnect`, which `connect` to `AF_UNSPEC` already runs on Linux (host `AF_UNSPEC` connect, then a rebind when the port was dropped). Linux CI must confirm that the test passes there. - No title or homebrew run. Overlap: `tools/pr_overlap.py` lists #8499, #8682, #8728 and #8921, and none of them implements this. #8499 (now merged) added a `sendto` port-0 check right next to `ZeroPortConnect` and rewrote the `sendto` half of the same debt sentence; this PR is rebased onto it, keeps `ZeroPortSend` where it is and keeps its `sendto` sentence word for word. #8682 changes `Disconnect` to take a non-const `Socket&`. If it merges first, `ZeroPortConnect` has to drop its `const` too, and the textual conflict around the moved function will show this. #8728 and #8921 only touch neighbouring debt lines. The tool could not compare some open PRs. I checked the ones that touch `Udp.cpp` (#5021, #6977, #7088, #7201, #8344, #8483, #8658, #8670), and none changes `connect` to port 0 or `Disconnect`. ### 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: none - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes
xXPBSXx commented 2026-10-21 16:14:48 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by xXPBSXx on 2026-10-21T16:14:48Z.


Independent Windows run of this PR (data only).

Environment: Windows 10 Pro 10.0.19045, Ryzen 7 5800X3D, WinLibs GCC 15.2.0 r7 (MinGW-w64 UCRT), Release, BUILD_TESTING=ON. Merge-tree of the PR head against main is clean. I built only guest_socket_port_zero_tests; ctest -N -R guest_socket_port_zero lists exactly one test.

PR head tested: 144470f04d3797be44fd0ce00fe8767c797ef5d9.

Results (3 runs each):

  • PR head: guest_socket_port_zero passes 3/3.
  • Baseline: I kept the PR's test and replaced only core/libs/prx/libkernel/Socket/src/Udp.cpp with the version from the PR's merge base (11bb53a347), then rebuilt. The test fails 3/3 (ctest reports Failed). I restored the PR's files afterwards.

GPU: the run output has no Selected GPU line; no GPU claim is made.

Not run: other platforms, the other tests in the same family, the full repository CTest, and any title or console run.

AI-assisted (Claude Code).

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6320725501 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9105#issuecomment-6320725501). Originally posted by [xXPBSXx](https://github.com/xXPBSXx) on 2026-10-21T16:14:48Z. --- Independent Windows run of this PR (data only). **Environment:** Windows 10 Pro 10.0.19045, Ryzen 7 5800X3D, WinLibs GCC 15.2.0 r7 (MinGW-w64 UCRT), Release, BUILD_TESTING=ON. Merge-tree of the PR head against `main` is clean. I built only `guest_socket_port_zero_tests`; `ctest -N -R guest_socket_port_zero` lists exactly one test. **PR head tested:** `144470f04d3797be44fd0ce00fe8767c797ef5d9`. **Results (3 runs each):** - PR head: `guest_socket_port_zero` passes 3/3. - Baseline: I kept the PR's test and replaced only `core/libs/prx/libkernel/Socket/src/Udp.cpp` with the version from the PR's merge base (`11bb53a347`), then rebuilt. The test fails 3/3 (ctest reports Failed). I restored the PR's files afterwards. **GPU:** the run output has no `Selected GPU` line; no GPU claim is made. **Not run:** other platforms, the other tests in the same family, the full repository CTest, and any title or console run. AI-assisted (Claude Code).
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!9105
No description provided.