udp_generic.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. //go:build (!linux || android) && !e2e_testing && !darwin
  2. // +build !linux android
  3. // +build !e2e_testing
  4. // +build !darwin
  5. // udp_generic implements the nebula UDP interface in pure Go stdlib. This
  6. // means it can be used on platforms like Darwin and Windows.
  7. package udp
  8. import (
  9. "context"
  10. "errors"
  11. "fmt"
  12. "log/slog"
  13. "net"
  14. "net/netip"
  15. "time"
  16. "github.com/slackhq/nebula/config"
  17. )
  18. type GenericConn struct {
  19. *net.UDPConn
  20. l *slog.Logger
  21. }
  22. var _ Conn = &GenericConn{}
  23. func NewGenericListener(l *slog.Logger, ip netip.Addr, port int, multi bool, batch int) (Conn, error) {
  24. lc := NewListenConfig(multi)
  25. pc, err := lc.ListenPacket(context.TODO(), "udp", net.JoinHostPort(ip.String(), fmt.Sprintf("%v", port)))
  26. if err != nil {
  27. return nil, err
  28. }
  29. if uc, ok := pc.(*net.UDPConn); ok {
  30. return &GenericConn{UDPConn: uc, l: l}, nil
  31. }
  32. return nil, fmt.Errorf("Unexpected PacketConn: %T %#v", pc, pc)
  33. }
  34. func (u *GenericConn) WriteTo(b []byte, addr netip.AddrPort) error {
  35. _, err := u.UDPConn.WriteToUDPAddrPort(b, addr)
  36. return err
  37. }
  38. func (u *GenericConn) LocalAddr() (netip.AddrPort, error) {
  39. a := u.UDPConn.LocalAddr()
  40. switch v := a.(type) {
  41. case *net.UDPAddr:
  42. addr, ok := netip.AddrFromSlice(v.IP)
  43. if !ok {
  44. return netip.AddrPort{}, fmt.Errorf("LocalAddr returned invalid IP address: %s", v.IP)
  45. }
  46. return netip.AddrPortFrom(addr, uint16(v.Port)), nil
  47. default:
  48. return netip.AddrPort{}, fmt.Errorf("LocalAddr returned: %#v", a)
  49. }
  50. }
  51. func (u *GenericConn) ReloadConfig(c *config.C) {
  52. }
  53. func NewUDPStatsEmitter(udpConns []Conn) func() {
  54. // No UDP stats for non-linux
  55. return func() {}
  56. }
  57. type rawMessage struct {
  58. Len uint32
  59. }
  60. func (u *GenericConn) ListenOut(r EncReader) error {
  61. buffer := make([]byte, MTU)
  62. var lastRecvErr time.Time
  63. for {
  64. // Just read one packet at a time
  65. n, rua, err := u.ReadFromUDPAddrPort(buffer)
  66. if err != nil {
  67. if errors.Is(err, net.ErrClosed) {
  68. return err
  69. }
  70. // Dampen unexpected message warns to once per minute
  71. if lastRecvErr.IsZero() || time.Since(lastRecvErr) > time.Minute {
  72. lastRecvErr = time.Now()
  73. u.l.Warn("unexpected udp socket receive error", "error", err)
  74. }
  75. continue
  76. }
  77. r(netip.AddrPortFrom(rua.Addr().Unmap(), rua.Port()), buffer[:n])
  78. }
  79. }
  80. func (u *GenericConn) SupportsMultipleReaders() bool {
  81. return false
  82. }
  83. // EnablePathMTUDiscovery is implemented per-platform alongside Rebind, in
  84. // udp_android.go / udp_bsd.go / udp_netbsd.go / udp_windows.go.
  85. // controlFD invokes f with the underlying UDP socket file descriptor (or
  86. // handle, on Windows). Used by platform files for setsockopt calls that the
  87. // stdlib net.UDPConn does not expose directly.
  88. func (u *GenericConn) controlFD(f func(fd uintptr) error) error {
  89. rc, err := u.UDPConn.SyscallConn()
  90. if err != nil {
  91. return err
  92. }
  93. var sockErr error
  94. err = rc.Control(func(fd uintptr) {
  95. sockErr = f(fd)
  96. })
  97. if err != nil {
  98. return err
  99. }
  100. return sockErr
  101. }
  102. // isV4Socket reports whether the local bind address looks like an IPv4 socket.
  103. // Used by EnablePathMTUDiscovery to pick IPPROTO_IP vs IPPROTO_IPV6 socket
  104. // options. Assumes pure-v4 or pure-v6 sockets; a dual-stack v6 socket bound to
  105. // :: will be treated as v6 (correct: setting IPV6_DONTFRAG covers v4-mapped
  106. // traffic too on most stacks).
  107. func (u *GenericConn) isV4Socket() bool {
  108. la := u.UDPConn.LocalAddr()
  109. if la == nil {
  110. return false
  111. }
  112. ua, ok := la.(*net.UDPAddr)
  113. if !ok {
  114. return false
  115. }
  116. return ua.IP.To4() != nil
  117. }