conn: fix sticky source address on multi-IP hosts
cover conn: fix sticky source address on multi-IP hosts
Three independent bugs in conn/bind_std.go cause the sticky source address feature to silently fail on hosts with multiple addresses on the listening interface. With UDP_GRO enabled (Linux 5.12+) the brokenness compounds: even after fixing PKTINFO delivery on the PacketConn, coalesced reads drop the cmsg on every frame past the first.
Symptom on a real-world setup: a Linux box with a static 2001:db8::100 and a DHCPv6-assigned 2001:db8::943 on enp1s0, where the default route's preferred source is ::943. A peer initiates a handshake to [::100]:port; the server responds from ::943; the peer rejects the response because the source IP does not match its configured endpoint; the session never establishes. The user has to install a route hack (ip -6 route ... src ::100) as a workaround.
After this series, the kernel's PKTINFO ancillary data correctly threads through the PacketConn, recvmmsg/UDP_GRO split path, and back into the outgoing sendmsg cmsg, so the response is emitted from the address the peer reached, regardless of route source preference.
Reproduction via netns, verified end-to-end:
sudo ip netns add srvns && sudo ip netns add cltns
sudo ip link add veth-srv type veth peer name veth-srvNS sudo ip link add veth-clt type veth peer name veth-cltNS sudo ip link set veth-srvNS netns srvns
sudo ip link set veth-cltNS netns cltns
sudo ip link add br-test type bridge
sudo ip link set veth-srv master br-test
sudo ip link set veth-clt master br-test
sudo ip link set veth-srv up
sudo ip link set veth-clt up
sudo ip link set br-test up
sudo ip netns exec srvns ip link set veth-srvNS up
sudo ip netns exec srvns ip -6 addr add fd00:1::100/64 dev veth-srvNS sudo ip netns exec srvns ip -6 addr add fd00:1::943/64 dev veth-srvNS sudo ip netns exec cltns ip link set veth-cltNS up
sudo ip netns exec cltns ip -6 addr add fd00:1::1/64 dev veth-cltNS
sleep 3 # wait for DAD
sudo ip netns exec srvns ip -6 route get fd00:1::1
# confirms: src fd00:1::943 (the preferred source from /64)
Run wireguard-go in each netns with the client's Endpoint set to [fd00:1::100]:port. Before the series, tcpdump -i br-test shows the handshake response from ::943 and the client immediately roams its endpoint to ::943. After the series, the response is from ::100 and the client's endpoint stays as configured.
Each patch is independent and reviewable on its own:
1/3 enables PKTINFO delivery on the receive path so ep.src actually
gets populated;
2/3 fixes a slice-length pool-reuse bug in Send() that truncates the
destination address after v4/v6 transitions; 3/3 restores PKTINFO on each frame produced by splitCoalescedMessages
so UDP_GRO offload no longer defeats the fix from 1/3.
conn/bind_std.go | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-)
Signature · 1 line
2.54.0
PATCH 1/3 conn: enable IP_PKTINFO ancillary data on PacketConn
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.
--- conn/bind_std.go | 2 ++ 1 file changed, 2 insertions(+)
conn/bind_std.go +2 -0
@@ -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))
Signature · 1 line
2.54.0
PATCH 2/3 conn: reset udpAddrPool slice length before copy
In reply to Artjom VejselThe pooled net.UDPAddr is allocated once with IP = make([]byte, 16) and reused indiscriminately for both IPv4 and IPv6 sends. The current code copies the destination address first and then truncates the slice to the desired length, which works in isolation but corrupts the address on pool reuse:
1. v4 send: copy(ua.IP, as4[:]) writes 4 bytes, then ua.IP = ua.IP[:4] truncates the slice.
2. ua is returned to the pool with len(IP) == 4.
3. v6 send: copy(ua.IP, as16[:]) copies only min(len(dst), len(src)) == 4 bytes (the slice header still says len == 4), then ua.IP = ua.IP[:16] re-extends the slice over the stale tail.
The result is an outgoing IPv6 datagram with the upper 4 bytes of the intended destination address followed by 12 stale bytes from prior sends. tcpdump shows truncated destinations like 2001:db8:: instead of 2001:db8::aaa, and either drops the packet or routes it to a wrong peer.
Resize the slice before copy so the destination is written into the full address space and any stale bytes are overwritten.
--- conn/bind_std.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
conn/bind_std.go +2 -2
@@ -368,13 +368,13 @@ func (s *StdNetBind) Send(bufs [][]byte, endpoint Endpoint) error { ua := s.udpAddrPool.Get().(*net.UDPAddr) defer s.udpAddrPool.Put(ua) if is6 { + ua.IP = ua.IP[:16] as16 := endpoint.DstIP().As16() copy(ua.IP, as16[:]) - ua.IP = ua.IP[:16] } else { + ua.IP = ua.IP[:4] as4 := endpoint.DstIP().As4() copy(ua.IP, as4[:]) - ua.IP = ua.IP[:4] } ua.Port = int(endpoint.(*StdNetEndpoint).Port()) var (
Signature · 1 line
2.54.0
PATCH 3/3 conn: propagate ancillary data to coalesced split frames
In reply to Artjom VejselsplitCoalescedMessages divides a single coalesced read into individual frames by copying Buffers[0][start:end], setting N, and propagating Addr to each child slot, but it does not propagate the control buffer (OOB) or its length (NN). The parent buffer holds the cmsg returned by recvmmsg for the entire coalesced datagram, including the PKTINFO that identifies the destination address of the original packet. The child slots retain whatever control content was left over from a previous recv, with NN == 0 for unused slots and stale data for reused ones.
receiveIP then calls getSrcFromControl(msg.OOB[:msg.NN], ep) on each emitted message. Because NN is zero for every split frame except the first (which still aliases the parent's storage), StdNetEndpoint.src is never populated for the rest, and setSrcControl on the corresponding send writes empty control bytes. The kernel falls back to route-based source selection, defeating the sticky source address mechanism whenever UDP_GRO coalesces packets — which is the common case on Linux 5.12+.
Copy the parent's control buffer and length into each split child. The n == i case is skipped because the destination slot is the parent itself and the slices already alias.
--- conn/bind_std.go | 5 +++++ 1 file changed, 5 insertions(+)
conn/bind_std.go +5 -0
@@ -528,6 +528,11 @@ func splitCoalescedMessages(msgs []ipv6.Message, firstMsgAt int, getGSO getGSOFu copied := copy(msgs[n].Buffers[0], msg.Buffers[0][start:end]) msgs[n].N = copied msgs[n].Addr = msg.Addr + if n != i { + oobCopied := copy(msgs[n].OOB[:cap(msgs[n].OOB)], msg.OOB[:msg.NN]) + msgs[n].OOB = msgs[n].OOB[:oobCopied] + msgs[n].NN = oobCopied + } start = end end += gsoSize if end > msg.N {
Signature · 1 line
2.54.0