fix(libkernel): fail sendto to port 0 from an unbound IPv4 datagram socket with EADDRNOTAVAIL #8499

Merged
xXPBSXx merged 2 commits from upstream-pr/8499 into main 2026-10-22 15:54:00 +00:00
xXPBSXx commented 2026-10-20 13:57:16 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by xXPBSXx on 2026-10-20T13:57:16Z.


What

udp_output hands the destination to in_pcbconnect_setup unless the socket is bound to a specific address and port and the destination is neither INADDR_ANY nor INADDR_BROADCAST (in_pcbconnect_setup fails port 0 with EADDRNOTAVAIL). sendto, and sendmsg through it, sent such a datagram to the host. For an AF_INET datagram socket that is not connected they now fail with 49 in those cases; a socket bound to a specific address and port can still send to port 0, as FreeBSD allows. This closes the sendto/sendmsg part of the known gap in the connect Technical debt entry (#6884). AF_INET6 is unchanged.
Fixes #8498
Overlap: git merge-tree against #7475 (sendto on a connected socket, open) has one conflict in Udp.cpp; against #7090, #8392, #8448, #8463, #8474, #8483, #8492, #7511, #7850, #7490 and #8312 it is clean. Whichever lands second rebases. pr_overlap was not run.

Tested

Windows 10 (19045), MinGW-w64 GCC 15.2.0, Release, on upstream/main d0eb3b64c9 + this commit:

  • guest_udp: the new case aborts without the change to Udp.cpp and passes with it. It checks port 0 from an unbound socket (49, nothing queued), a datagram to the real port, port 0 from a socket bound to 127.0.0.1 (sent) and destination 0.0.0.0 port 0 from that socket (49).
  • guest_datagram_recv, guest_sce_net, guest_socket_error, guest_socket_address_length, guest_poll and guest_tcp pass.
  • python tools/check_conventions.py --base upstream/main: passes.
  • Not tested: Linux/macOS, a console, a title run, sendmsg directly, AF_INET6.

Checklist

  • Based on current main; no other open PR implements the same functions (#7475 changes sendto for a connected socket)
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw; no silent stubs added
  • No notes, investigation .md files or images added to the repository
  • New third-party code is a submodule built from source (none added)
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Fixes: #8498
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes

AI-assisted (Claude Code).

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4936848118 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8499). Originally posted by [xXPBSXx](https://github.com/xXPBSXx) on 2026-10-20T13:57:16Z. --- ### What `udp_output` hands the destination to `in_pcbconnect_setup` unless the socket is bound to a specific address and port and the destination is neither `INADDR_ANY` nor `INADDR_BROADCAST` ([`in_pcbconnect_setup`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/sys/netinet/in_pcb.c#L1034-L1035) fails port 0 with `EADDRNOTAVAIL`). `sendto`, and `sendmsg` through it, sent such a datagram to the host. For an `AF_INET` datagram socket that is not connected they now fail with 49 in those cases; a socket bound to a specific address and port can still send to port 0, as FreeBSD allows. This closes the `sendto`/`sendmsg` part of the known gap in the `connect` Technical debt entry (#6884). `AF_INET6` is unchanged. Fixes #8498 Overlap: `git merge-tree` against #7475 (sendto on a connected socket, open) has one conflict in `Udp.cpp`; against #7090, #8392, #8448, #8463, #8474, #8483, #8492, #7511, #7850, #7490 and #8312 it is clean. Whichever lands second rebases. `pr_overlap` was not run. ### Tested Windows 10 (19045), MinGW-w64 GCC 15.2.0, Release, on upstream/main d0eb3b64c9 + this commit: - `guest_udp`: the new case aborts without the change to `Udp.cpp` and passes with it. It checks port 0 from an unbound socket (49, nothing queued), a datagram to the real port, port 0 from a socket bound to 127.0.0.1 (sent) and destination 0.0.0.0 port 0 from that socket (49). - `guest_datagram_recv`, `guest_sce_net`, `guest_socket_error`, `guest_socket_address_length`, `guest_poll` and `guest_tcp` pass. - `python tools/check_conventions.py --base upstream/main`: passes. - Not tested: Linux/macOS, a console, a title run, `sendmsg` directly, `AF_INET6`. ### Checklist - [x] Based on current `main`; no other open PR implements the same functions (#7475 changes `sendto` for a connected socket) - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except technical debt - [x] Unimplemented paths throw; no silent stubs added - [x] No notes, investigation `.md` files or images added to the repository - [x] New third-party code is a submodule built from source (none added) - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] Fixes: #8498 - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes AI-assisted (Claude Code). 🤖 Generated with [Claude Code](https://claude.com/claude-code)
oneandonlydean commented 2026-10-20 14:26:21 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-20T14:26:21Z.


Thanks, the rule matches udp_output and in_pcbconnect_setup. One problem on Linux: the new test expects a socket bound to 127.0.0.1 to send to port 0 successfully, and that send goes to the host. Linux's udp_sendmsg rejects destination port 0 with EINVAL (bound or not; checked on a 7.2 kernel and in an Ubuntu 24.04 container), so guest_udp will fail on the Linux CI job. Could you either answer that case without the host on Linux (FreeBSD sends the datagram, and nothing receives on port 0, so returning the length matches what the guest sees), or keep EINVAL on Linux, note it in the technical debt entry, and make that check platform-specific?

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6293184808 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8499#issuecomment-6293184808). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-20T14:26:21Z. --- Thanks, the rule matches `udp_output` and `in_pcbconnect_setup`. One problem on Linux: the new test expects a socket bound to 127.0.0.1 to send to port 0 successfully, and that send goes to the host. Linux's `udp_sendmsg` rejects destination port 0 with `EINVAL` (bound or not; checked on a 7.2 kernel and in an Ubuntu 24.04 container), so `guest_udp` will fail on the Linux CI job. Could you either answer that case without the host on Linux (FreeBSD sends the datagram, and nothing receives on port 0, so returning the length matches what the guest sees), or keep `EINVAL` on Linux, note it in the technical debt entry, and make that check platform-specific?
xXPBSXx commented 2026-10-20 14:59:44 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by xXPBSXx on 2026-10-20T14:59:44Z.


Thanks, you're right: Linux's udp_sendmsg refuses port 0 with EINVAL. The send that FreeBSD accepts (socket bound to a specific address and port, destination not INADDR_ANY/INADDR_BROADCAST) is now answered in Udp.cpp without the host in 0bb7b02e9f: nothing can receive on port 0, so it returns the length on every platform. The Technical debt entry says so; guest_udp now also checks that nothing is queued afterwards. Re-tested on Windows 10 (guest_udp, guest_sce_net, guest_socket_address_length pass); I could not run Linux here, so the Linux CI is the check.

AI-assisted (Claude Code).

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6293915182 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8499#issuecomment-6293915182). Originally posted by [xXPBSXx](https://github.com/xXPBSXx) on 2026-10-20T14:59:44Z. --- Thanks, you're right: Linux's udp_sendmsg refuses port 0 with EINVAL. The send that FreeBSD accepts (socket bound to a specific address and port, destination not INADDR_ANY/INADDR_BROADCAST) is now answered in Udp.cpp without the host in 0bb7b02e9f: nothing can receive on port 0, so it returns the length on every platform. The Technical debt entry says so; guest_udp now also checks that nothing is queued afterwards. Re-tested on Windows 10 (guest_udp, guest_sce_net, guest_socket_address_length pass); I could not run Linux here, so the Linux CI is the check. 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!8499
No description provided.