| 1 |
//go:build (!linux || android) && !e2e_testing && !darwin |
| 2 |
// +build !linux android |
| 3 |
// +build !e2e_testing |
| 4 |
// +build !darwin |
| 5 |
|
| 6 |
// udp_generic implements the nebula UDP interface in pure Go stdlib. This |
| 7 |
// means it can be used on platforms like Darwin and Windows. |
| 8 |
|
| 9 |
package udp |
| 10 |
|
| 11 |
import ( |
| 12 |
"context" |
| 13 |
"errors" |
| 14 |
"fmt" |
| 15 |
"log/slog" |
| 16 |
"net" |
| 17 |
"net/netip" |
| 18 |
"time" |
| 19 |
|
| 20 |
"github.com/slackhq/nebula/config" |
| 21 |
) |
| 22 |
|
| 23 |
type GenericConn struct { |
| 24 |
*net.UDPConn |
| 25 |
l *slog.Logger |
| 26 |
} |
| 27 |
|
| 28 |
var _ Conn = &GenericConn{} |
| 29 |
|
| 30 |
func NewGenericListener(l *slog.Logger, ip netip.Addr, port int, multi bool, batch int) (Conn, error) { |
| 31 |
lc := NewListenConfig(multi) |
| 32 |
pc, err := lc.ListenPacket(context.TODO(), "udp", net.JoinHostPort(ip.String(), fmt.Sprintf("%v", port))) |
| 33 |
if err != nil { |
| 34 |
return nil, err |
| 35 |
} |
| 36 |
if uc, ok := pc.(*net.UDPConn); ok { |
| 37 |
return &GenericConn{UDPConn: uc, l: l}, nil |
| 38 |
} |
| 39 |
return nil, fmt.Errorf("Unexpected PacketConn: %T %#v", pc, pc) |
| 40 |
} |
| 41 |
|
| 42 |
func (u *GenericConn) WriteTo(b []byte, addr netip.AddrPort) error { |
| 43 |
_, err := u.UDPConn.WriteToUDPAddrPort(b, addr) |
| 44 |
return err |
| 45 |
} |
| 46 |
|
| 47 |
func (u *GenericConn) LocalAddr() (netip.AddrPort, error) { |
| 48 |
a := u.UDPConn.LocalAddr() |
| 49 |
|
| 50 |
switch v := a.(type) { |
| 51 |
case *net.UDPAddr: |
| 52 |
addr, ok := netip.AddrFromSlice(v.IP) |
| 53 |
if !ok { |
| 54 |
return netip.AddrPort{}, fmt.Errorf("LocalAddr returned invalid IP address: %s", v.IP) |
| 55 |
} |
| 56 |
return netip.AddrPortFrom(addr, uint16(v.Port)), nil |
| 57 |
|
| 58 |
default: |
| 59 |
return netip.AddrPort{}, fmt.Errorf("LocalAddr returned: %#v", a) |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
func (u *GenericConn) ReloadConfig(c *config.C) { |
| 64 |
|
| 65 |
} |
| 66 |
|
| 67 |
func NewUDPStatsEmitter(udpConns []Conn) func() { |
| 68 |
// No UDP stats for non-linux |
| 69 |
return func() {} |
| 70 |
} |
| 71 |
|
| 72 |
type rawMessage struct { |
| 73 |
Len uint32 |
| 74 |
} |
| 75 |
|
| 76 |
func (u *GenericConn) ListenOut(r EncReader) error { |
| 77 |
buffer := make([]byte, MTU) |
| 78 |
|
| 79 |
var lastRecvErr time.Time |
| 80 |
|
| 81 |
for { |
| 82 |
// Just read one packet at a time |
| 83 |
n, rua, err := u.ReadFromUDPAddrPort(buffer) |
| 84 |
if err != nil { |
| 85 |
if errors.Is(err, net.ErrClosed) { |
| 86 |
return err |
| 87 |
} |
| 88 |
// Dampen unexpected message warns to once per minute |
| 89 |
if lastRecvErr.IsZero() || time.Since(lastRecvErr) > time.Minute { |
| 90 |
lastRecvErr = time.Now() |
| 91 |
u.l.Warn("unexpected udp socket receive error", "error", err) |
| 92 |
} |
| 93 |
continue |
| 94 |
} |
| 95 |
|
| 96 |
r(netip.AddrPortFrom(rua.Addr().Unmap(), rua.Port()), buffer[:n]) |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
func (u *GenericConn) SupportsMultipleReaders() bool { |
| 101 |
return false |
| 102 |
} |