udp_tester.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. // +build e2e_testing
  2. package nebula
  3. import (
  4. "fmt"
  5. "net"
  6. "github.com/sirupsen/logrus"
  7. )
  8. type UdpPacket struct {
  9. ToIp net.IP
  10. ToPort uint16
  11. FromIp net.IP
  12. FromPort uint16
  13. Data []byte
  14. }
  15. func (u *UdpPacket) Copy() *UdpPacket {
  16. n := &UdpPacket{
  17. ToIp: make(net.IP, len(u.ToIp)),
  18. ToPort: u.ToPort,
  19. FromIp: make(net.IP, len(u.FromIp)),
  20. FromPort: u.FromPort,
  21. Data: make([]byte, len(u.Data)),
  22. }
  23. copy(n.ToIp, u.ToIp)
  24. copy(n.FromIp, u.FromIp)
  25. copy(n.Data, u.Data)
  26. return n
  27. }
  28. type udpConn struct {
  29. addr *udpAddr
  30. rxPackets chan *UdpPacket // Packets to receive into nebula
  31. txPackets chan *UdpPacket // Packets transmitted outside by nebula
  32. l *logrus.Logger
  33. }
  34. func NewListener(l *logrus.Logger, ip string, port int, _ bool) (*udpConn, error) {
  35. return &udpConn{
  36. addr: &udpAddr{net.ParseIP(ip), uint16(port)},
  37. rxPackets: make(chan *UdpPacket, 1),
  38. txPackets: make(chan *UdpPacket, 1),
  39. l: l,
  40. }, nil
  41. }
  42. // Send will place a UdpPacket onto the receive queue for nebula to consume
  43. // this is an encrypted packet or a handshake message in most cases
  44. // packets were transmitted from another nebula node, you can send them with Tun.Send
  45. func (u *udpConn) Send(packet *UdpPacket) {
  46. h := &Header{}
  47. if err := h.Parse(packet.Data); err != nil {
  48. panic(err)
  49. }
  50. u.l.WithField("header", h).
  51. WithField("udpAddr", fmt.Sprintf("%v:%v", packet.FromIp, packet.FromPort)).
  52. WithField("dataLen", len(packet.Data)).
  53. Info("UDP receiving injected packet")
  54. u.rxPackets <- packet
  55. }
  56. // Get will pull a UdpPacket from the transmit queue
  57. // nebula meant to send this message on the network, it will be encrypted
  58. // packets were ingested from the tun side (in most cases), you can send them with Tun.Send
  59. func (u *udpConn) Get(block bool) *UdpPacket {
  60. if block {
  61. return <-u.txPackets
  62. }
  63. select {
  64. case p := <-u.txPackets:
  65. return p
  66. default:
  67. return nil
  68. }
  69. }
  70. //********************************************************************************************************************//
  71. // Below this is boilerplate implementation to make nebula actually work
  72. //********************************************************************************************************************//
  73. func (u *udpConn) WriteTo(b []byte, addr *udpAddr) error {
  74. p := &UdpPacket{
  75. Data: make([]byte, len(b), len(b)),
  76. FromIp: make([]byte, 16),
  77. FromPort: u.addr.Port,
  78. ToIp: make([]byte, 16),
  79. ToPort: addr.Port,
  80. }
  81. copy(p.Data, b)
  82. copy(p.ToIp, addr.IP.To16())
  83. copy(p.FromIp, u.addr.IP.To16())
  84. u.txPackets <- p
  85. return nil
  86. }
  87. func (u *udpConn) ListenOut(f *Interface, q int) {
  88. plaintext := make([]byte, mtu)
  89. header := &Header{}
  90. fwPacket := &FirewallPacket{}
  91. ua := &udpAddr{IP: make([]byte, 16)}
  92. nb := make([]byte, 12, 12)
  93. lhh := f.lightHouse.NewRequestHandler()
  94. conntrackCache := NewConntrackCacheTicker(f.conntrackCacheTimeout)
  95. for {
  96. p := <-u.rxPackets
  97. ua.Port = p.FromPort
  98. copy(ua.IP, p.FromIp.To16())
  99. f.readOutsidePackets(ua, plaintext[:0], p.Data, header, fwPacket, lhh, nb, q, conntrackCache.Get(u.l))
  100. }
  101. }
  102. func (u *udpConn) reloadConfig(*Config) {}
  103. func NewUDPStatsEmitter(_ []*udpConn) func() {
  104. // No UDP stats for non-linux
  105. return func() {}
  106. }
  107. func (u *udpConn) LocalAddr() (*udpAddr, error) {
  108. return u.addr, nil
  109. }
  110. func (u *udpConn) Rebind() error {
  111. return nil
  112. }
  113. func hostDidRoam(addr *udpAddr, newaddr *udpAddr) bool {
  114. return !addr.Equals(newaddr)
  115. }