Files
tailscale/net/sockopts/sockopts.go
Alex Valiushko e9d5cdc323 net/sockopts,net/udprelay: implement BindUDPGroup (#21783)
We're going to add a second SO_REUSEPORT use in the codebase. Extract
and generalize udprelay mechanism for future reuse. Also address the
Linux flaw of allowing same UID groups to merge if the ephemeral port
selection collides.

Exposes public SetReusePort, RawConn and ControlFunc signatures for
shared use.

Updates tailscale/corp#48168
Fixes tailscale/corp#49297

Signed-off-by: Alex Valiushko <[email protected]>
Change-Id: Iba33142877513d72123c51cf1bcc33cf6a6a6964
2026-10-15 18:04:10 -07:00

88 lines
2.3 KiB
Go

// Copyright (c) Tailscale Inc & contributors
// SPDX-License-Identifier: BSD-3-Clause
// Package sockopts contains logic for applying socket options.
package sockopts
import (
"errors"
"fmt"
"net"
"runtime"
"syscall"
"tailscale.com/types/nettype"
)
// RawConn returns the socket underlying pconn.
//
// The error wraps [ErrNoSocket] if pconn has no socket.
func RawConn(pconn nettype.PacketConn) (syscall.RawConn, error) {
sc, ok := pconn.(syscall.Conn)
if !ok {
return nil, fmt.Errorf("%T: %w", pconn, ErrNoSocket)
}
return sc.SyscallConn()
}
// ErrNoSocket reports that there is no socket to apply an option to,
// distinct from the kernel refusing the option on one.
var ErrNoSocket = errors.New("no socket to configure")
// ControlEach runs the system call fn on the socket of every conn in pconns.
// It reports [ErrNoSocket] without calling fn if pconns is empty or if any
// conn has no socket; otherwise it calls fn for each socket before returning,
// and returns the join of fn's errors.
func ControlEach(pconns []nettype.PacketConn, fn func(fd uintptr) error) error {
if len(pconns) == 0 {
return fmt.Errorf("empty group: %w", ErrNoSocket)
}
rcs := make([]syscall.RawConn, len(pconns))
for i, pconn := range pconns {
rc, err := RawConn(pconn)
if err != nil {
return fmt.Errorf("socket %d of %d: %w", i, len(pconns), err)
}
rcs[i] = rc
}
var errs []error
for _, rc := range rcs {
if err := rc.Control(func(fd uintptr) {
if err := fn(fd); err != nil {
errs = append(errs, err)
}
}); err != nil {
errs = append(errs, err)
}
}
return errors.Join(errs...)
}
// ControlFunc is the Control hook of a [net.ListenConfig].
type ControlFunc func(network, address string, c syscall.RawConn) error
// BufferDirection represents either the read/receive or write/send direction
// of a socket buffer.
type BufferDirection string
const (
ReadDirection BufferDirection = "read"
WriteDirection BufferDirection = "write"
)
func portableSetBufferSize(pconn nettype.PacketConn, direction BufferDirection, size int) error {
if runtime.GOOS == "plan9" {
// Not supported. Don't try. Avoid logspam.
return nil
}
var err error
if c, ok := pconn.(*net.UDPConn); ok {
if direction == WriteDirection {
err = c.SetWriteBuffer(size)
} else {
err = c.SetReadBuffer(size)
}
}
return err
}