fix(net): make new IPv6 sockets IPv6-only as FreeBSD does #4370

Merged
milescoviello merged 1 commit from upstream-pr/4370 into main 2026-10-12 18:21:35 +00:00
milescoviello commented 2026-10-12 07:54:43 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by milescoviello on 2026-10-12T07:54:43Z.


What

On Linux, socket() (libkernel) and sceNetSocket() (libSceNet) create AF_INET6 sockets in the host's default dual-stack mode (net.ipv6.bindv6only is 0), so a wildcard IPv6 bind also takes the IPv4 port. A title that keeps an IPv4 and an IPv6 socket on the same port, the usual way to serve both families, fails on main: with 0.0.0.0:P bound, binding [::]:P returns EADDRINUSE (48). An IPv6 socket bound to [::] also receives IPv4 traffic from v4-mapped peers, which the console never delivers to it.

FreeBSD makes every new IPv6 socket IPv6-only. sys/netinet6/in6_proto.c:

VNET_DEFINE(int, ip6_v6only) = 1;

sys/netinet/in_pcb.c, in_pcballoc():

if (INP_SOCKAF(so) == AF_INET6) {
	inp->inp_vflag |= INP_IPV6PROTO;
	if (V_ip6_v6only)
		inp->inp_flags |= IN6P_IPV6_V6ONLY;
}

and sys/netinet6/in6_pcb.c, in6_pcbbind(), only looks for an IPv4 owner of the port when that flag is clear:

if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0 &&
    IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
	struct sockaddr_in sin;

	in6_sin6_2_sin(&sin, sin6);
	t = in_pcblookup_local(pcbinfo, sin.sin_addr,
	    lport, lookupflags, cred);

socket() and sceNetSocket() now set IPV6_V6ONLY on every IPv6 socket they create; a failure closes the socket and returns the error. Windows already makes IPv6 sockets IPv6-only by default. No title is claimed.

Tested

Ubuntu 24.04, GCC 13.3 (CI's compiler), Release with CI's configure flags.

  • guest_tcp (libkernel) binds a TCP listener to 0.0.0.0:0, reads its port P and binds and listens on an IPv6 TCP socket at [::]:P; guest_sce_net (libSceNet) does the same with UDP sockets. Both binds must succeed. The libkernel check is skipped when the host has no IPv6 (EAFNOSUPPORT from socket() or EADDRNOTAVAIL from bind()); the libSceNet one runs only when the existing IPv6 checks found a usable IPv6 loopback.
  • With only the tests applied to main's library sources both fail:
    guest_tcp:     bind of [::]:59479 failed with errno 48 while an IPv4 listener holds 0.0.0.0:59479
    guest_sce_net: sceNetBind of [::]:37416 failed with errno 48 while an IPv4 socket holds 0.0.0.0:37416
    
  • With the fix both pass, as do guest_udp, guest_socket_pair, guest_socket_read_write and guest_socket_events, and all 299 guest_* tests pass (-E '^guest_video_out_').

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 in TechnicalDebt
  • 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
  • Unblocks: none
  • AI-assisted: yes

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4826510652 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4370). Originally posted by [milescoviello](https://github.com/milescoviello) on 2026-10-12T07:54:43Z. --- ### What On Linux, `socket()` (libkernel) and `sceNetSocket()` (libSceNet) create `AF_INET6` sockets in the host's default dual-stack mode (`net.ipv6.bindv6only` is 0), so a wildcard IPv6 bind also takes the IPv4 port. A title that keeps an IPv4 and an IPv6 socket on the same port, the usual way to serve both families, fails on main: with `0.0.0.0:P` bound, binding `[::]:P` returns `EADDRINUSE` (48). An IPv6 socket bound to `[::]` also receives IPv4 traffic from v4-mapped peers, which the console never delivers to it. FreeBSD makes every new IPv6 socket IPv6-only. [`sys/netinet6/in6_proto.c`](https://github.com/freebsd/freebsd-src/blob/releng/11.4/sys/netinet6/in6_proto.c#L399): ```c VNET_DEFINE(int, ip6_v6only) = 1; ``` [`sys/netinet/in_pcb.c`](https://github.com/freebsd/freebsd-src/blob/releng/11.4/sys/netinet/in_pcb.c#L312-L316), `in_pcballoc()`: ```c if (INP_SOCKAF(so) == AF_INET6) { inp->inp_vflag |= INP_IPV6PROTO; if (V_ip6_v6only) inp->inp_flags |= IN6P_IPV6_V6ONLY; } ``` and [`sys/netinet6/in6_pcb.c`](https://github.com/freebsd/freebsd-src/blob/releng/11.4/sys/netinet6/in6_pcb.c#L271-L277), `in6_pcbbind()`, only looks for an IPv4 owner of the port when that flag is clear: ```c if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0 && IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) { struct sockaddr_in sin; in6_sin6_2_sin(&sin, sin6); t = in_pcblookup_local(pcbinfo, sin.sin_addr, lport, lookupflags, cred); ``` `socket()` and `sceNetSocket()` now set `IPV6_V6ONLY` on every IPv6 socket they create; a failure closes the socket and returns the error. Windows already makes IPv6 sockets IPv6-only by default. No title is claimed. ### Tested Ubuntu 24.04, GCC 13.3 (CI's compiler), Release with CI's configure flags. - `guest_tcp` (libkernel) binds a TCP listener to `0.0.0.0:0`, reads its port P and binds and listens on an IPv6 TCP socket at `[::]:P`; `guest_sce_net` (libSceNet) does the same with UDP sockets. Both binds must succeed. The libkernel check is skipped when the host has no IPv6 (`EAFNOSUPPORT` from `socket()` or `EADDRNOTAVAIL` from `bind()`); the libSceNet one runs only when the existing IPv6 checks found a usable IPv6 loopback. - With only the tests applied to main's library sources both fail: ```text guest_tcp: bind of [::]:59479 failed with errno 48 while an IPv4 listener holds 0.0.0.0:59479 guest_sce_net: sceNetBind of [::]:37416 failed with errno 48 while an IPv4 socket holds 0.0.0.0:37416 ``` - With the fix both pass, as do `guest_udp`, `guest_socket_pair`, `guest_socket_read_write` and `guest_socket_events`, and all 299 `guest_*` tests pass (`-E '^guest_video_out_'`). ### Checklist - [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 in [TechnicalDebt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/TechnicalDebt.md#silent-stubs) - [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] Unblocks: none - [x] AI-assisted: yes 🤖 Generated with [Claude Code](https://claude.com/claude-code)
xXPBSXx commented 2026-10-12 12:20:23 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by xXPBSXx on 2026-10-12T12:20:23Z.


Windows test: head 98f6639e9 merged onto current main ffa198407 (clean merge, no conflicts). Windows 10, MinGW GCC (WinLibs), Ninja Release, -j4: full build 0 errors; ctest with the filter guest_sce_net|guest_tcp: guest_sce_net passed, guest_tcp passed (2 test(s), 0 failed); python tools/check_conventions.py --base upstream/main passed. I did not run the new tests against main without the change.

AI-assisted (Claude Code).

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6124145952 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4370#issuecomment-6124145952). Originally posted by [xXPBSXx](https://github.com/xXPBSXx) on 2026-10-12T12:20:23Z. --- Windows test: head `98f6639e9` merged onto current `main` `ffa198407` (clean merge, no conflicts). Windows 10, MinGW GCC (WinLibs), Ninja Release, `-j4`: full build 0 errors; ctest with the filter `guest_sce_net|guest_tcp`: guest_sce_net passed, guest_tcp passed (2 test(s), 0 failed); `python tools/check_conventions.py --base upstream/main` passed. I did not run the new tests against `main` without the change. AI-assisted (Claude Code).
github-actions[bot] commented 2026-10-12 17:49:26 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-12T17:49:26Z.


Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.

Pull request Conflicting files (lines on main) Same exports Same new files
#3947 core/libs/tests/GuestTcp.cpp L50, L52
#3958 core/libs/tests/GuestTcp.cpp L3-4, L50, L52
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6129585048 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4370#issuecomment-6129585048). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-12T17:49:26Z. --- <!-- pr-overlap --> Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in **Depends on**. | Pull request | Conflicting files (lines on `main`) | Same exports | Same new files | |---|---|---|---| | #3947 | `core/libs/tests/GuestTcp.cpp` [L50](https://github.com/boykopovar/AnyPS5/blob/bde332a59ce98ce8aa776b26a4ed04e53417946d/core/libs/tests/GuestTcp.cpp#L50), [L52](https://github.com/boykopovar/AnyPS5/blob/bde332a59ce98ce8aa776b26a4ed04e53417946d/core/libs/tests/GuestTcp.cpp#L52) | | | | #3958 | `core/libs/tests/GuestTcp.cpp` [L3-4](https://github.com/boykopovar/AnyPS5/blob/bde332a59ce98ce8aa776b26a4ed04e53417946d/core/libs/tests/GuestTcp.cpp#L3-L4), [L50](https://github.com/boykopovar/AnyPS5/blob/bde332a59ce98ce8aa776b26a4ed04e53417946d/core/libs/tests/GuestTcp.cpp#L50), [L52](https://github.com/boykopovar/AnyPS5/blob/bde332a59ce98ce8aa776b26a4ed04e53417946d/core/libs/tests/GuestTcp.cpp#L52) | | |
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!4370
No description provided.