2021-10-21 15:24:11 -06:00
|
|
|
//go:build (!linux || android) && !e2e_testing
|
2020-06-30 12:48:58 -06:00
|
|
|
// +build !linux android
|
2021-03-26 08:46:30 -06:00
|
|
|
// +build !e2e_testing
|
2019-11-19 10:00:20 -07:00
|
|
|
|
|
|
|
// udp_generic implements the nebula UDP interface in pure Go stdlib. This
|
|
|
|
// means it can be used on platforms like Darwin and Windows.
|
|
|
|
|
2021-11-03 19:54:04 -06:00
|
|
|
package udp
|
2019-11-19 10:00:20 -07:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"net"
|
2021-03-26 08:46:30 -06:00
|
|
|
|
|
|
|
"github.com/sirupsen/logrus"
|
2021-11-03 19:54:04 -06:00
|
|
|
"github.com/slackhq/nebula/config"
|
|
|
|
"github.com/slackhq/nebula/firewall"
|
|
|
|
"github.com/slackhq/nebula/header"
|
2019-11-19 10:00:20 -07:00
|
|
|
)
|
|
|
|
|
2021-11-03 19:54:04 -06:00
|
|
|
type Conn struct {
|
2019-11-19 10:00:20 -07:00
|
|
|
*net.UDPConn
|
2021-03-26 08:46:30 -06:00
|
|
|
l *logrus.Logger
|
2019-11-19 10:00:20 -07:00
|
|
|
}
|
|
|
|
|
2021-11-03 19:54:04 -06:00
|
|
|
func NewListener(l *logrus.Logger, ip string, port int, multi bool, batch int) (*Conn, error) {
|
2019-11-19 10:00:20 -07:00
|
|
|
lc := NewListenConfig(multi)
|
2021-03-18 19:37:24 -06:00
|
|
|
pc, err := lc.ListenPacket(context.TODO(), "udp", fmt.Sprintf("%s:%d", ip, port))
|
2019-11-19 10:00:20 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if uc, ok := pc.(*net.UDPConn); ok {
|
2021-11-03 19:54:04 -06:00
|
|
|
return &Conn{UDPConn: uc, l: l}, nil
|
2019-11-19 10:00:20 -07:00
|
|
|
}
|
|
|
|
return nil, fmt.Errorf("Unexpected PacketConn: %T %#v", pc, pc)
|
|
|
|
}
|
|
|
|
|
2021-11-03 19:54:04 -06:00
|
|
|
func (uc *Conn) WriteTo(b []byte, addr *Addr) error {
|
2021-03-18 19:37:24 -06:00
|
|
|
_, err := uc.UDPConn.WriteToUDP(b, &net.UDPAddr{IP: addr.IP, Port: int(addr.Port)})
|
2019-11-19 10:00:20 -07:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-11-03 19:54:04 -06:00
|
|
|
func (uc *Conn) LocalAddr() (*Addr, error) {
|
2019-11-19 10:00:20 -07:00
|
|
|
a := uc.UDPConn.LocalAddr()
|
|
|
|
|
|
|
|
switch v := a.(type) {
|
|
|
|
case *net.UDPAddr:
|
2021-11-03 19:54:04 -06:00
|
|
|
addr := &Addr{IP: make([]byte, len(v.IP))}
|
2021-03-18 19:37:24 -06:00
|
|
|
copy(addr.IP, v.IP)
|
|
|
|
addr.Port = uint16(v.Port)
|
|
|
|
return addr, nil
|
|
|
|
|
2019-11-19 10:00:20 -07:00
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("LocalAddr returned: %#v", a)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-03 19:54:04 -06:00
|
|
|
func (u *Conn) ReloadConfig(c *config.C) {
|
2019-11-19 10:00:20 -07:00
|
|
|
// TODO
|
|
|
|
}
|
|
|
|
|
2021-11-03 19:54:04 -06:00
|
|
|
func NewUDPStatsEmitter(udpConns []*Conn) func() {
|
2021-03-01 17:51:33 -07:00
|
|
|
// No UDP stats for non-linux
|
|
|
|
return func() {}
|
|
|
|
}
|
|
|
|
|
2019-11-19 10:00:20 -07:00
|
|
|
type rawMessage struct {
|
|
|
|
Len uint32
|
|
|
|
}
|
|
|
|
|
2021-11-03 19:54:04 -06:00
|
|
|
func (u *Conn) ListenOut(r EncReader, lhf LightHouseHandlerFunc, cache *firewall.ConntrackCacheTicker, q int) {
|
|
|
|
plaintext := make([]byte, MTU)
|
|
|
|
buffer := make([]byte, MTU)
|
|
|
|
h := &header.H{}
|
|
|
|
fwPacket := &firewall.Packet{}
|
|
|
|
udpAddr := &Addr{IP: make([]byte, 16)}
|
2019-11-19 10:00:20 -07:00
|
|
|
nb := make([]byte, 12, 12)
|
|
|
|
|
|
|
|
for {
|
|
|
|
// Just read one packet at a time
|
|
|
|
n, rua, err := u.ReadFromUDP(buffer)
|
|
|
|
if err != nil {
|
2021-11-03 19:54:04 -06:00
|
|
|
u.l.WithError(err).Error("Failed to read packets")
|
2019-11-19 10:00:20 -07:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-03-18 19:37:24 -06:00
|
|
|
udpAddr.IP = rua.IP
|
|
|
|
udpAddr.Port = uint16(rua.Port)
|
2022-06-21 12:35:23 -06:00
|
|
|
r(udpAddr, nil, plaintext[:0], buffer[:n], h, fwPacket, lhf, nb, q, cache.Get(u.l))
|
2019-11-19 10:00:20 -07:00
|
|
|
}
|
|
|
|
}
|