udp_darwin.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. //go:build !e2e_testing
  2. // +build !e2e_testing
  3. package udp
  4. // Darwin support is primarily implemented in udp_generic, besides NewListenConfig
  5. import (
  6. "context"
  7. "encoding/binary"
  8. "errors"
  9. "fmt"
  10. "net"
  11. "net/netip"
  12. "syscall"
  13. "unsafe"
  14. "github.com/sirupsen/logrus"
  15. "github.com/slackhq/nebula/config"
  16. "github.com/slackhq/nebula/firewall"
  17. "github.com/slackhq/nebula/header"
  18. "golang.org/x/sys/unix"
  19. )
  20. type StdConn struct {
  21. *net.UDPConn
  22. isV4 bool
  23. sysFd uintptr
  24. l *logrus.Logger
  25. }
  26. var _ Conn = &StdConn{}
  27. func NewListener(l *logrus.Logger, ip netip.Addr, port int, multi bool, batch int) (Conn, error) {
  28. lc := NewListenConfig(multi)
  29. pc, err := lc.ListenPacket(context.TODO(), "udp", net.JoinHostPort(ip.String(), fmt.Sprintf("%v", port)))
  30. if err != nil {
  31. return nil, err
  32. }
  33. if uc, ok := pc.(*net.UDPConn); ok {
  34. c := &StdConn{UDPConn: uc, l: l}
  35. rc, err := uc.SyscallConn()
  36. if err != nil {
  37. return nil, fmt.Errorf("failed to open udp socket: %w", err)
  38. }
  39. err = rc.Control(func(fd uintptr) {
  40. c.sysFd = fd
  41. })
  42. if err != nil {
  43. return nil, fmt.Errorf("failed to get udp fd: %w", err)
  44. }
  45. la, err := c.LocalAddr()
  46. if err != nil {
  47. return nil, err
  48. }
  49. c.isV4 = la.Addr().Is4()
  50. return c, nil
  51. }
  52. return nil, fmt.Errorf("unexpected PacketConn: %T %#v", pc, pc)
  53. }
  54. func NewListenConfig(multi bool) net.ListenConfig {
  55. return net.ListenConfig{
  56. Control: func(network, address string, c syscall.RawConn) error {
  57. if multi {
  58. var controlErr error
  59. err := c.Control(func(fd uintptr) {
  60. if err := syscall.SetsockoptInt(int(fd), syscall.SOL_SOCKET, unix.SO_REUSEPORT, 1); err != nil {
  61. controlErr = fmt.Errorf("SO_REUSEPORT failed: %v", err)
  62. return
  63. }
  64. })
  65. if err != nil {
  66. return err
  67. }
  68. if controlErr != nil {
  69. return controlErr
  70. }
  71. }
  72. return nil
  73. },
  74. }
  75. }
  76. //go:linkname sendto golang.org/x/sys/unix.sendto
  77. //go:noescape
  78. func sendto(s int, buf []byte, flags int, to unsafe.Pointer, addrlen int32) (err error)
  79. func (u *StdConn) WriteTo(b []byte, ap netip.AddrPort) error {
  80. var sa unsafe.Pointer
  81. var addrLen int32
  82. if u.isV4 {
  83. if ap.Addr().Is6() {
  84. return ErrInvalidIPv6RemoteForSocket
  85. }
  86. var rsa unix.RawSockaddrInet6
  87. rsa.Family = unix.AF_INET6
  88. rsa.Addr = ap.Addr().As16()
  89. binary.BigEndian.PutUint16((*[2]byte)(unsafe.Pointer(&rsa.Port))[:], ap.Port())
  90. sa = unsafe.Pointer(&rsa)
  91. addrLen = syscall.SizeofSockaddrInet4
  92. } else {
  93. var rsa unix.RawSockaddrInet6
  94. rsa.Family = unix.AF_INET6
  95. rsa.Addr = ap.Addr().As16()
  96. binary.BigEndian.PutUint16((*[2]byte)(unsafe.Pointer(&rsa.Port))[:], ap.Port())
  97. sa = unsafe.Pointer(&rsa)
  98. addrLen = syscall.SizeofSockaddrInet6
  99. }
  100. // Golang stdlib doesn't handle EAGAIN correctly in some situations so we do writes ourselves
  101. // See https://github.com/golang/go/issues/73919
  102. for {
  103. //_, _, err := unix.Syscall6(unix.SYS_SENDTO, u.sysFd, uintptr(unsafe.Pointer(&b[0])), uintptr(len(b)), 0, sa, addrLen)
  104. err := sendto(int(u.sysFd), b, 0, sa, addrLen)
  105. if err == nil {
  106. // Written, get out before the error handling
  107. return nil
  108. }
  109. if errors.Is(err, syscall.EINTR) {
  110. // Write was interrupted, retry
  111. continue
  112. }
  113. if errors.Is(err, syscall.EAGAIN) {
  114. return &net.OpError{Op: "sendto", Err: unix.EWOULDBLOCK}
  115. }
  116. if errors.Is(err, syscall.EBADF) {
  117. return net.ErrClosed
  118. }
  119. return &net.OpError{Op: "sendto", Err: err}
  120. }
  121. }
  122. func (u *StdConn) LocalAddr() (netip.AddrPort, error) {
  123. a := u.UDPConn.LocalAddr()
  124. switch v := a.(type) {
  125. case *net.UDPAddr:
  126. addr, ok := netip.AddrFromSlice(v.IP)
  127. if !ok {
  128. return netip.AddrPort{}, fmt.Errorf("LocalAddr returned invalid IP address: %s", v.IP)
  129. }
  130. return netip.AddrPortFrom(addr, uint16(v.Port)), nil
  131. default:
  132. return netip.AddrPort{}, fmt.Errorf("LocalAddr returned: %#v", a)
  133. }
  134. }
  135. func (u *StdConn) ReloadConfig(c *config.C) {
  136. // TODO
  137. }
  138. func NewUDPStatsEmitter(udpConns []Conn) func() {
  139. // No UDP stats for non-linux
  140. return func() {}
  141. }
  142. func (u *StdConn) ListenOut(r EncReader, lhf LightHouseHandlerFunc, cache *firewall.ConntrackCacheTicker, q int) {
  143. plaintext := make([]byte, MTU)
  144. buffer := make([]byte, MTU)
  145. h := &header.H{}
  146. fwPacket := &firewall.Packet{}
  147. nb := make([]byte, 12, 12)
  148. for {
  149. // Just read one packet at a time
  150. n, rua, err := u.ReadFromUDPAddrPort(buffer)
  151. if err != nil {
  152. if errors.Is(err, net.ErrClosed) {
  153. u.l.WithError(err).Debug("udp socket is closed, exiting read loop")
  154. return
  155. }
  156. u.l.WithError(err).Error("unexpected udp socket receive error")
  157. }
  158. r(
  159. netip.AddrPortFrom(rua.Addr().Unmap(), rua.Port()),
  160. plaintext[:0],
  161. buffer[:n],
  162. h,
  163. fwPacket,
  164. lhf,
  165. nb,
  166. q,
  167. cache.Get(u.l),
  168. )
  169. }
  170. }
  171. func (u *StdConn) Rebind() error {
  172. var err error
  173. if u.isV4 {
  174. err = syscall.SetsockoptInt(int(u.sysFd), syscall.IPPROTO_IP, syscall.IP_BOUND_IF, 0)
  175. } else {
  176. err = syscall.SetsockoptInt(int(u.sysFd), syscall.IPPROTO_IPV6, syscall.IPV6_BOUND_IF, 0)
  177. }
  178. if err != nil {
  179. u.l.WithError(err).Error("Failed to rebind udp socket")
  180. }
  181. return nil
  182. }