WireGuard Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: Artjom Vejsel <akawolf0@gmail.com>
To: wireguard@lists.zx2c4.com
Cc: Jason@zx2c4.com, Artjom Vejsel <akawolf0@gmail.com>
Subject: [PATCH wireguard-go 1/3] conn: enable IP_PKTINFO ancillary data on PacketConn
Date: Mon,  8 Jun 2026 03:33:05 +0300	[thread overview]
Message-ID: <20260608003307.2598372-2-akawolf0@gmail.com> (raw)
In-Reply-To: <20260608003307.2598372-1-akawolf0@gmail.com>

The IPV6_RECVPKTINFO/IP_PKTINFO setsockopts are set during socket
creation via the ListenConfig control function. However, x/net/ipv4 and
x/net/ipv6 PacketConn instances maintain their own internal control
message option tracking (rawOpt), which is consulted during recvmmsg
unpacking. Without calling SetControlMessage(FlagDst|FlagInterface,
true), the PacketConn does not advertise these flags to the unpack
path, and the resulting Message.NN remains zero on every read.

Because Message.NN is zero, getSrcFromControl never finds a PKTINFO
cmsg, StdNetEndpoint.src is never populated, and setSrcControl writes
empty control bytes on send. The kernel then selects the outgoing
source address by route lookup (RFC 6724) rather than honoring the
destination address of the incoming packet. The sticky source address
behavior is silently broken on hosts with multiple addresses on the
listening interface.

Reproduced with a userspace bind on a Linux interface holding two
global IPv6 addresses, where the route preferred source differs from
the address the peer initiated the handshake on: the server responded
from the route-preferred address, the peer rejected the response, and
the session never established.

Signed-off-by: Artjom Vejsel <akawolf0@gmail.com>
---
 conn/bind_std.go | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/conn/bind_std.go b/conn/bind_std.go
index f5c8816..c88ed4e 100644
--- a/conn/bind_std.go
+++ b/conn/bind_std.go
@@ -177,6 +177,7 @@ again:
 		s.ipv4TxOffload, s.ipv4RxOffload = supportsUDPOffload(v4conn)
 		if runtime.GOOS == "linux" || runtime.GOOS == "android" {
 			v4pc = ipv4.NewPacketConn(v4conn)
+			_ = v4pc.SetControlMessage(ipv4.FlagDst|ipv4.FlagInterface, true)
 			s.ipv4PC = v4pc
 		}
 		fns = append(fns, s.makeReceiveIPv4(v4pc, v4conn, s.ipv4RxOffload))
@@ -186,6 +187,7 @@ again:
 		s.ipv6TxOffload, s.ipv6RxOffload = supportsUDPOffload(v6conn)
 		if runtime.GOOS == "linux" || runtime.GOOS == "android" {
 			v6pc = ipv6.NewPacketConn(v6conn)
+			_ = v6pc.SetControlMessage(ipv6.FlagDst|ipv6.FlagInterface, true)
 			s.ipv6PC = v6pc
 		}
 		fns = append(fns, s.makeReceiveIPv6(v6pc, v6conn, s.ipv6RxOffload))
-- 
2.54.0


  reply	other threads:[~2026-10-05  6:55 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-06-08  0:33 [PATCH wireguard-go 0/3] conn: fix sticky source address on multi-IP hosts Artjom Vejsel
2026-06-08  0:33 ` Artjom Vejsel [this message]
2026-06-08  0:33 ` [PATCH wireguard-go 2/3] conn: reset udpAddrPool slice length before copy Artjom Vejsel
2026-06-08  0:33 ` [PATCH wireguard-go 3/3] conn: propagate ancillary data to coalesced split frames Artjom Vejsel
  -- strict thread matches above, loose matches on Subject: below --
2026-06-09 22:01 [PATCH wireguard-go 0/3] conn: fix sticky source address on multi-IP hosts Artjom Vejsel
2026-06-09 22:01 ` [PATCH wireguard-go 1/3] conn: enable IP_PKTINFO ancillary data on PacketConn Artjom Vejsel

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260608003307.2598372-2-akawolf0@gmail.com \
    --to=akawolf0@gmail.com \
    --cc=Jason@zx2c4.com \
    --cc=wireguard@lists.zx2c4.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox