mirror of
https://github.com/slackhq/nebula.git
synced 2026-02-15 17:24:23 +01:00
Switch most everything to netip in prep for ipv6 in the overlay (#1173)
This commit is contained in:
@@ -7,6 +7,7 @@ import (
|
||||
"encoding/binary"
|
||||
"fmt"
|
||||
"net"
|
||||
"net/netip"
|
||||
"syscall"
|
||||
"unsafe"
|
||||
|
||||
@@ -35,10 +36,9 @@ func maybeIPV4(ip net.IP) (net.IP, bool) {
|
||||
return ip, false
|
||||
}
|
||||
|
||||
func NewListener(l *logrus.Logger, ip net.IP, port int, multi bool, batch int) (Conn, error) {
|
||||
ipV4, isV4 := maybeIPV4(ip)
|
||||
func NewListener(l *logrus.Logger, ip netip.Addr, port int, multi bool, batch int) (Conn, error) {
|
||||
af := unix.AF_INET6
|
||||
if isV4 {
|
||||
if ip.Is4() {
|
||||
af = unix.AF_INET
|
||||
}
|
||||
syscall.ForkLock.RLock()
|
||||
@@ -61,13 +61,13 @@ func NewListener(l *logrus.Logger, ip net.IP, port int, multi bool, batch int) (
|
||||
|
||||
//TODO: support multiple listening IPs (for limiting ipv6)
|
||||
var sa unix.Sockaddr
|
||||
if isV4 {
|
||||
if ip.Is4() {
|
||||
sa4 := &unix.SockaddrInet4{Port: port}
|
||||
copy(sa4.Addr[:], ipV4)
|
||||
sa4.Addr = ip.As4()
|
||||
sa = sa4
|
||||
} else {
|
||||
sa6 := &unix.SockaddrInet6{Port: port}
|
||||
copy(sa6.Addr[:], ip.To16())
|
||||
sa6.Addr = ip.As16()
|
||||
sa = sa6
|
||||
}
|
||||
if err = unix.Bind(fd, sa); err != nil {
|
||||
@@ -79,7 +79,7 @@ func NewListener(l *logrus.Logger, ip net.IP, port int, multi bool, batch int) (
|
||||
//v, err := unix.GetsockoptInt(fd, unix.SOL_SOCKET, unix.SO_INCOMING_CPU)
|
||||
//l.Println(v, err)
|
||||
|
||||
return &StdConn{sysFd: fd, isV4: isV4, l: l, batch: batch}, err
|
||||
return &StdConn{sysFd: fd, isV4: ip.Is4(), l: l, batch: batch}, err
|
||||
}
|
||||
|
||||
func (u *StdConn) Rebind() error {
|
||||
@@ -102,30 +102,29 @@ func (u *StdConn) GetSendBuffer() (int, error) {
|
||||
return unix.GetsockoptInt(int(u.sysFd), unix.SOL_SOCKET, unix.SO_SNDBUF)
|
||||
}
|
||||
|
||||
func (u *StdConn) LocalAddr() (*Addr, error) {
|
||||
func (u *StdConn) LocalAddr() (netip.AddrPort, error) {
|
||||
sa, err := unix.Getsockname(u.sysFd)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return netip.AddrPort{}, err
|
||||
}
|
||||
|
||||
addr := &Addr{}
|
||||
switch sa := sa.(type) {
|
||||
case *unix.SockaddrInet4:
|
||||
addr.IP = net.IP{sa.Addr[0], sa.Addr[1], sa.Addr[2], sa.Addr[3]}.To16()
|
||||
addr.Port = uint16(sa.Port)
|
||||
case *unix.SockaddrInet6:
|
||||
addr.IP = sa.Addr[0:]
|
||||
addr.Port = uint16(sa.Port)
|
||||
}
|
||||
return netip.AddrPortFrom(netip.AddrFrom4(sa.Addr), uint16(sa.Port)), nil
|
||||
|
||||
return addr, nil
|
||||
case *unix.SockaddrInet6:
|
||||
return netip.AddrPortFrom(netip.AddrFrom16(sa.Addr), uint16(sa.Port)), nil
|
||||
|
||||
default:
|
||||
return netip.AddrPort{}, fmt.Errorf("unsupported sock type: %T", sa)
|
||||
}
|
||||
}
|
||||
|
||||
func (u *StdConn) ListenOut(r EncReader, lhf LightHouseHandlerFunc, cache *firewall.ConntrackCacheTicker, q int) {
|
||||
plaintext := make([]byte, MTU)
|
||||
h := &header.H{}
|
||||
fwPacket := &firewall.Packet{}
|
||||
udpAddr := &Addr{}
|
||||
var ip netip.Addr
|
||||
nb := make([]byte, 12, 12)
|
||||
|
||||
//TODO: should we track this?
|
||||
@@ -146,12 +145,23 @@ func (u *StdConn) ListenOut(r EncReader, lhf LightHouseHandlerFunc, cache *firew
|
||||
//metric.Update(int64(n))
|
||||
for i := 0; i < n; i++ {
|
||||
if u.isV4 {
|
||||
udpAddr.IP = names[i][4:8]
|
||||
ip, _ = netip.AddrFromSlice(names[i][4:8])
|
||||
//TODO: IPV6-WORK what is not ok?
|
||||
} else {
|
||||
udpAddr.IP = names[i][8:24]
|
||||
ip, _ = netip.AddrFromSlice(names[i][8:24])
|
||||
//TODO: IPV6-WORK what is not ok?
|
||||
}
|
||||
udpAddr.Port = binary.BigEndian.Uint16(names[i][2:4])
|
||||
r(udpAddr, plaintext[:0], buffers[i][:msgs[i].Len], h, fwPacket, lhf, nb, q, cache.Get(u.l))
|
||||
r(
|
||||
netip.AddrPortFrom(ip.Unmap(), binary.BigEndian.Uint16(names[i][2:4])),
|
||||
plaintext[:0],
|
||||
buffers[i][:msgs[i].Len],
|
||||
h,
|
||||
fwPacket,
|
||||
lhf,
|
||||
nb,
|
||||
q,
|
||||
cache.Get(u.l),
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -197,19 +207,20 @@ func (u *StdConn) ReadMulti(msgs []rawMessage) (int, error) {
|
||||
}
|
||||
}
|
||||
|
||||
func (u *StdConn) WriteTo(b []byte, addr *Addr) error {
|
||||
func (u *StdConn) WriteTo(b []byte, ip netip.AddrPort) error {
|
||||
if u.isV4 {
|
||||
return u.writeTo4(b, addr)
|
||||
return u.writeTo4(b, ip)
|
||||
}
|
||||
return u.writeTo6(b, addr)
|
||||
return u.writeTo6(b, ip)
|
||||
}
|
||||
|
||||
func (u *StdConn) writeTo6(b []byte, addr *Addr) error {
|
||||
func (u *StdConn) writeTo6(b []byte, ip netip.AddrPort) error {
|
||||
var rsa unix.RawSockaddrInet6
|
||||
rsa.Family = unix.AF_INET6
|
||||
rsa.Addr = ip.Addr().As16()
|
||||
port := ip.Port()
|
||||
// Little Endian -> Network Endian
|
||||
rsa.Port = (addr.Port >> 8) | ((addr.Port & 0xff) << 8)
|
||||
copy(rsa.Addr[:], addr.IP.To16())
|
||||
rsa.Port = (port >> 8) | ((port & 0xff) << 8)
|
||||
|
||||
for {
|
||||
_, _, err := unix.Syscall6(
|
||||
@@ -232,17 +243,17 @@ func (u *StdConn) writeTo6(b []byte, addr *Addr) error {
|
||||
}
|
||||
}
|
||||
|
||||
func (u *StdConn) writeTo4(b []byte, addr *Addr) error {
|
||||
addrV4, isAddrV4 := maybeIPV4(addr.IP)
|
||||
if !isAddrV4 {
|
||||
func (u *StdConn) writeTo4(b []byte, ip netip.AddrPort) error {
|
||||
if !ip.Addr().Is4() {
|
||||
return fmt.Errorf("Listener is IPv4, but writing to IPv6 remote")
|
||||
}
|
||||
|
||||
var rsa unix.RawSockaddrInet4
|
||||
rsa.Family = unix.AF_INET
|
||||
rsa.Addr = ip.Addr().As4()
|
||||
port := ip.Port()
|
||||
// Little Endian -> Network Endian
|
||||
rsa.Port = (addr.Port >> 8) | ((addr.Port & 0xff) << 8)
|
||||
copy(rsa.Addr[:], addrV4)
|
||||
rsa.Port = (port >> 8) | ((port & 0xff) << 8)
|
||||
|
||||
for {
|
||||
_, _, err := unix.Syscall6(
|
||||
|
||||
Reference in New Issue
Block a user