Skip to content

Tunnel inbound: SO_REUSEPORT fix and IPv6 support for the OpenBSD transparent proxy - #6624

Merged
RPRX merged 2 commits into
XTLS:mainfrom
adsdevdoo:openbsd-udp-reuseport
Aug 26, 2026
Merged

RPRX merged 2 commits into
XTLS:mainfrom
adsdevdoo:openbsd-udp-reuseport

Conversation

@adsdevdoo

Copy link
Copy Markdown
Contributor

Follow-up to #6546.

On OpenBSD every UDP reply to a client is sent from a socket bound to the
original destination with SO_BINDANY, and FakeUDP creates one such socket
per session. Two clients talking to the same destination at the same time, two
phones resolving over the same DNS server for instance, therefore bind the same
address and port twice. The second bind fails with EADDRINUSE, surfacing as
fake: bind ...: address already in use, and that session never gets an answer.

SO_REUSEADDR alone does not cover this. In in_pcbaddrisavail_lock() the
reuse flag starts as so->so_options & SO_REUSEPORT and is only widened to
accept SO_REUSEADDR for multicast addresses, so a duplicate binding on a
unicast address needs SO_REUSEPORT on both sockets:

int reuseport = (so->so_options & SO_REUSEPORT);

if (IN_MULTICAST(sin->sin_addr.s_addr)) {
	if (so->so_options & (SO_REUSEADDR|SO_REUSEPORT))
		reuseport = SO_REUSEADDR|SO_REUSEPORT;
}
...
if (t && (reuseport & t->inp_socket->so_options) == 0)
	error = EADDRINUSE;

proxy/dokodemo/fakeudp_linux.go already sets both options, this brings the
OpenBSD implementation in line.

Tested on OpenBSD 7.7/7.8/7.9 amd64 on a live router with several LAN clients.

Every reply to a client is sent from a socket bound to the original
destination with SO_BINDANY, and one such socket is created per session.
Two clients talking to the same destination, two phones resolving over
the same DNS server for instance, therefore bind the same address and
port twice, and the second bind fails with EADDRINUSE, leaving that
session without answers.

SO_REUSEADDR does not help here: on OpenBSD it is only promoted to allow
a duplicate binding for multicast addresses, so a unicast address needs
SO_REUSEPORT on both sockets. The Linux implementation of FakeUDP already
sets both options.
@Fangliding

Copy link
Copy Markdown
Member

6546晾了三个星期为什么刚好合并两天才发现。。

@adsdevdoo

Copy link
Copy Markdown
Contributor Author

Fair point. It only shows up when two sessions reach the same destination
at the same time, and my first round of testing was with a single client.
I hit it later, while running IPv6 on a live router with several devices
behind it.

The fix was already sitting in my IPv6 branch. I split it out into its own
PR so that a one-line fix to code that is already merged would not have to
wait for review of a whole feature.

@RPRX

RPRX commented Aug 25, 2026

Copy link
Copy Markdown
Member

直接把 IPv6 的代码合并到这个 PR 吧,这 OpenBSD 专属代码没人有能力深入 review

pf divert-to works the same way for inet6, but the original destination
arrives in different control messages: IPV6_PKTINFO carries the address
and IPV6_RECVDSTPORT the port, instead of IP_RECVDSTADDR and
IP_RECVDSTPORT. The reply socket has to be created in the matching
address family as well, because OpenBSD always forces IPV6_V6ONLY and a
single socket never serves both.
@adsdevdoo

Copy link
Copy Markdown
Contributor Author

Done, added the IPv6 commit here. The PR is now two commits:

  • the SO_REUSEPORT fix, unchanged;
  • IPv6 support for the transparent proxy. The original destination of an
    inet6 flow arrives in different control messages, IPV6_PKTINFO for the
    address and IPV6_RECVDSTPORT for the port, and the reply socket has to be
    created in the matching family, because OpenBSD always forces IPV6_V6ONLY
    and a single socket never serves both. The inbound sockopt setter now
    tries both option pairs and keeps whichever the socket's family accepts.

Tested on OpenBSD 7.7/7.8/7.9 amd64 on live routers, IPv6 via an HE tunnel:
TCP and UDP (DNS, QUIC) work in both families.

One piece is left out on purpose: outbound UDP to an IPv6 destination also
needs a small fix in the system dialer, but that one is not OpenBSD-specific,
so I will send it as a separate PR.

@adsdevdoo adsdevdoo changed the title Tunnel inbound: Share the fake UDP reply socket address on OpenBSD Tunnel inbound: SO_REUSEPORT fix and IPv6 support for the OpenBSD transparent proxy Aug 26, 2026
@RPRX

RPRX commented Aug 26, 2026

Copy link
Copy Markdown
Member

One piece is left out on purpose: outbound UDP to an IPv6 destination also needs a small fix in the system dialer, but that one is not OpenBSD-specific, so I will send it as a separate PR.

这个策略是对的,这个 PR 只包含 OpenBSD 平台特定代码,所以将会得到快速合并

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants