2021-03-29 13:29:20 -06:00
|
|
|
// +build e2e_testing
|
|
|
|
|
|
|
|
package nebula
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
|
|
|
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
|
|
|
type UdpPacket struct {
|
|
|
|
ToIp net.IP
|
|
|
|
ToPort uint16
|
|
|
|
FromIp net.IP
|
|
|
|
FromPort uint16
|
|
|
|
Data []byte
|
|
|
|
}
|
|
|
|
|
2021-03-31 12:36:10 -06:00
|
|
|
func (u *UdpPacket) Copy() *UdpPacket {
|
|
|
|
n := &UdpPacket{
|
|
|
|
ToIp: make(net.IP, len(u.ToIp)),
|
|
|
|
ToPort: u.ToPort,
|
|
|
|
FromIp: make(net.IP, len(u.FromIp)),
|
|
|
|
FromPort: u.FromPort,
|
|
|
|
Data: make([]byte, len(u.Data)),
|
|
|
|
}
|
|
|
|
|
|
|
|
copy(n.ToIp, u.ToIp)
|
|
|
|
copy(n.FromIp, u.FromIp)
|
|
|
|
copy(n.Data, u.Data)
|
|
|
|
return n
|
|
|
|
}
|
|
|
|
|
2021-03-29 13:29:20 -06:00
|
|
|
type udpConn struct {
|
|
|
|
addr *udpAddr
|
|
|
|
|
|
|
|
rxPackets chan *UdpPacket // Packets to receive into nebula
|
|
|
|
txPackets chan *UdpPacket // Packets transmitted outside by nebula
|
|
|
|
|
|
|
|
l *logrus.Logger
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewListener(l *logrus.Logger, ip string, port int, _ bool) (*udpConn, error) {
|
|
|
|
return &udpConn{
|
|
|
|
addr: &udpAddr{net.ParseIP(ip), uint16(port)},
|
|
|
|
rxPackets: make(chan *UdpPacket, 1),
|
|
|
|
txPackets: make(chan *UdpPacket, 1),
|
|
|
|
l: l,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Send will place a UdpPacket onto the receive queue for nebula to consume
|
|
|
|
// this is an encrypted packet or a handshake message in most cases
|
|
|
|
// packets were transmitted from another nebula node, you can send them with Tun.Send
|
|
|
|
func (u *udpConn) Send(packet *UdpPacket) {
|
2021-03-31 09:26:35 -06:00
|
|
|
u.l.Infof("UDP injecting packet %+v", packet)
|
2021-03-29 13:29:20 -06:00
|
|
|
u.rxPackets <- packet
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get will pull a UdpPacket from the transmit queue
|
|
|
|
// nebula meant to send this message on the network, it will be encrypted
|
|
|
|
// packets were ingested from the tun side (in most cases), you can send them with Tun.Send
|
|
|
|
func (u *udpConn) Get(block bool) *UdpPacket {
|
|
|
|
if block {
|
|
|
|
return <-u.txPackets
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case p := <-u.txPackets:
|
|
|
|
return p
|
|
|
|
default:
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//********************************************************************************************************************//
|
|
|
|
// Below this is boilerplate implementation to make nebula actually work
|
|
|
|
//********************************************************************************************************************//
|
|
|
|
|
|
|
|
func (u *udpConn) WriteTo(b []byte, addr *udpAddr) error {
|
|
|
|
p := &UdpPacket{
|
|
|
|
Data: make([]byte, len(b), len(b)),
|
|
|
|
FromIp: make([]byte, 16),
|
|
|
|
FromPort: u.addr.Port,
|
|
|
|
ToIp: make([]byte, 16),
|
|
|
|
ToPort: addr.Port,
|
|
|
|
}
|
|
|
|
|
|
|
|
copy(p.Data, b)
|
2021-03-31 09:26:35 -06:00
|
|
|
copy(p.ToIp, addr.IP.To16())
|
|
|
|
copy(p.FromIp, u.addr.IP.To16())
|
2021-03-29 13:29:20 -06:00
|
|
|
|
|
|
|
u.txPackets <- p
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *udpConn) ListenOut(f *Interface, q int) {
|
|
|
|
plaintext := make([]byte, mtu)
|
|
|
|
header := &Header{}
|
|
|
|
fwPacket := &FirewallPacket{}
|
|
|
|
ua := &udpAddr{IP: make([]byte, 16)}
|
|
|
|
nb := make([]byte, 12, 12)
|
|
|
|
|
|
|
|
lhh := f.lightHouse.NewRequestHandler()
|
|
|
|
conntrackCache := NewConntrackCacheTicker(f.conntrackCacheTimeout)
|
|
|
|
|
|
|
|
for {
|
|
|
|
p := <-u.rxPackets
|
|
|
|
ua.Port = p.FromPort
|
|
|
|
copy(ua.IP, p.FromIp.To16())
|
|
|
|
f.readOutsidePackets(ua, plaintext[:0], p.Data, header, fwPacket, lhh, nb, q, conntrackCache.Get(u.l))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *udpConn) reloadConfig(*Config) {}
|
|
|
|
|
|
|
|
func NewUDPStatsEmitter(_ []*udpConn) func() {
|
|
|
|
// No UDP stats for non-linux
|
|
|
|
return func() {}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *udpConn) LocalAddr() (*udpAddr, error) {
|
|
|
|
return u.addr, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *udpConn) Rebind() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func hostDidRoam(addr *udpAddr, newaddr *udpAddr) bool {
|
|
|
|
return !addr.Equals(newaddr)
|
|
|
|
}
|