nebula/overlay/tun_linux.go

321 lines
7.0 KiB
Go
Raw Normal View History

2021-10-21 23:24:11 +02:00
//go:build !android && !e2e_testing
// +build !android,!e2e_testing
2020-07-01 17:20:52 +02:00
2021-11-11 23:37:29 +01:00
package overlay
2019-11-19 18:00:20 +01:00
import (
"fmt"
"io"
"net"
"os"
"strings"
"unsafe"
2021-03-26 15:46:30 +01:00
"github.com/sirupsen/logrus"
2019-11-19 18:00:20 +01:00
"github.com/vishvananda/netlink"
"golang.org/x/sys/unix"
)
type tun struct {
2019-11-19 18:00:20 +01:00
io.ReadWriteCloser
2019-12-12 17:34:17 +01:00
fd int
Device string
Cidr *net.IPNet
MaxMTU int
DefaultMTU int
TXQueueLen int
2021-11-11 23:37:29 +01:00
Routes []Route
UnsafeRoutes []Route
2021-03-26 15:46:30 +01:00
l *logrus.Logger
2019-11-19 18:00:20 +01:00
}
type ifReq struct {
Name [16]byte
Flags uint16
pad [8]byte
}
func ioctl(a1, a2, a3 uintptr) error {
_, _, errno := unix.Syscall(unix.SYS_IOCTL, a1, a2, a3)
2019-11-19 18:00:20 +01:00
if errno != 0 {
return errno
}
return nil
}
type ifreqAddr struct {
Name [16]byte
Addr unix.RawSockaddrInet4
2019-11-19 18:00:20 +01:00
pad [8]byte
}
type ifreqMTU struct {
Name [16]byte
2019-11-25 20:21:48 +01:00
MTU int32
2019-11-19 18:00:20 +01:00
pad [8]byte
}
type ifreqQLEN struct {
Name [16]byte
2019-12-11 21:02:57 +01:00
Value int32
2019-11-19 18:00:20 +01:00
pad [8]byte
}
func newTunFromFd(l *logrus.Logger, deviceFd int, cidr *net.IPNet, defaultMTU int, routes []Route, unsafeRoutes []Route, txQueueLen int) (*tun, error) {
file := os.NewFile(uintptr(deviceFd), "/dev/net/tun")
return &tun{
ReadWriteCloser: file,
fd: int(file.Fd()),
Device: "tun0",
Cidr: cidr,
DefaultMTU: defaultMTU,
TXQueueLen: txQueueLen,
Routes: routes,
UnsafeRoutes: unsafeRoutes,
2021-03-26 15:46:30 +01:00
l: l,
}, nil
}
func newTun(l *logrus.Logger, deviceName string, cidr *net.IPNet, defaultMTU int, routes []Route, unsafeRoutes []Route, txQueueLen int, multiqueue bool) (*tun, error) {
2019-11-19 18:00:20 +01:00
fd, err := unix.Open("/dev/net/tun", os.O_RDWR, 0)
if err != nil {
return nil, err
}
var req ifReq
req.Flags = uint16(unix.IFF_TUN | unix.IFF_NO_PI)
if multiqueue {
req.Flags |= unix.IFF_MULTI_QUEUE
}
2019-11-19 18:00:20 +01:00
copy(req.Name[:], deviceName)
if err = ioctl(uintptr(fd), uintptr(unix.TUNSETIFF), uintptr(unsafe.Pointer(&req))); err != nil {
return nil, err
2019-11-19 18:00:20 +01:00
}
name := strings.Trim(string(req.Name[:]), "\x00")
file := os.NewFile(uintptr(fd), "/dev/net/tun")
maxMTU := defaultMTU
for _, r := range routes {
2021-11-11 23:37:29 +01:00
if r.MTU > maxMTU {
maxMTU = r.MTU
2019-11-19 18:00:20 +01:00
}
}
return &tun{
2019-11-19 18:00:20 +01:00
ReadWriteCloser: file,
fd: int(file.Fd()),
Device: name,
Cidr: cidr,
MaxMTU: maxMTU,
DefaultMTU: defaultMTU,
TXQueueLen: txQueueLen,
Routes: routes,
2019-12-12 17:34:17 +01:00
UnsafeRoutes: unsafeRoutes,
2021-03-26 15:46:30 +01:00
l: l,
}, nil
2019-11-19 18:00:20 +01:00
}
func (t *tun) NewMultiQueueReader() (io.ReadWriteCloser, error) {
fd, err := unix.Open("/dev/net/tun", os.O_RDWR, 0)
if err != nil {
return nil, err
}
var req ifReq
req.Flags = uint16(unix.IFF_TUN | unix.IFF_NO_PI | unix.IFF_MULTI_QUEUE)
copy(req.Name[:], t.Device)
if err = ioctl(uintptr(fd), uintptr(unix.TUNSETIFF), uintptr(unsafe.Pointer(&req))); err != nil {
return nil, err
}
file := os.NewFile(uintptr(fd), "/dev/net/tun")
return file, nil
}
func (t *tun) WriteRaw(b []byte) error {
2019-11-19 18:00:20 +01:00
var nn int
for {
max := len(b)
n, err := unix.Write(t.fd, b[nn:max])
2019-11-19 18:00:20 +01:00
if n > 0 {
nn += n
}
if nn == len(b) {
return err
}
if err != nil {
return err
}
if n == 0 {
return io.ErrUnexpectedEOF
}
}
}
func (t *tun) Write(b []byte) (int, error) {
return len(b), t.WriteRaw(b)
}
func (t tun) deviceBytes() (o [16]byte) {
for i, c := range t.Device {
2019-11-19 18:00:20 +01:00
o[i] = byte(c)
}
return
}
func (t tun) Activate() error {
devName := t.deviceBytes()
2019-11-19 18:00:20 +01:00
var addr, mask [4]byte
copy(addr[:], t.Cidr.IP.To4())
copy(mask[:], t.Cidr.Mask)
2019-11-19 18:00:20 +01:00
s, err := unix.Socket(
unix.AF_INET,
unix.SOCK_DGRAM,
unix.IPPROTO_IP,
2019-11-19 18:00:20 +01:00
)
if err != nil {
return err
}
fd := uintptr(s)
ifra := ifreqAddr{
Name: devName,
Addr: unix.RawSockaddrInet4{
Family: unix.AF_INET,
2019-11-19 18:00:20 +01:00
Addr: addr,
},
}
// Set the device ip address
if err = ioctl(fd, unix.SIOCSIFADDR, uintptr(unsafe.Pointer(&ifra))); err != nil {
2019-11-24 08:18:03 +01:00
return fmt.Errorf("failed to set tun address: %s", err)
2019-11-19 18:00:20 +01:00
}
// Set the device network
ifra.Addr.Addr = mask
if err = ioctl(fd, unix.SIOCSIFNETMASK, uintptr(unsafe.Pointer(&ifra))); err != nil {
2019-11-24 08:18:03 +01:00
return fmt.Errorf("failed to set tun netmask: %s", err)
2019-11-19 18:00:20 +01:00
}
// Set the device name
ifrf := ifReq{Name: devName}
if err = ioctl(fd, unix.SIOCGIFFLAGS, uintptr(unsafe.Pointer(&ifrf))); err != nil {
2019-11-24 08:18:03 +01:00
return fmt.Errorf("failed to set tun device name: %s", err)
2019-11-19 18:00:20 +01:00
}
// Set the MTU on the device
ifm := ifreqMTU{Name: devName, MTU: int32(t.MaxMTU)}
if err = ioctl(fd, unix.SIOCSIFMTU, uintptr(unsafe.Pointer(&ifm))); err != nil {
// This is currently a non fatal condition because the route table must have the MTU set appropriately as well
t.l.WithError(err).Error("Failed to set tun mtu")
2019-11-19 18:00:20 +01:00
}
// Set the transmit queue length
ifrq := ifreqQLEN{Name: devName, Value: int32(t.TXQueueLen)}
if err = ioctl(fd, unix.SIOCSIFTXQLEN, uintptr(unsafe.Pointer(&ifrq))); err != nil {
// If we can't set the queue length nebula will still work but it may lead to packet loss
t.l.WithError(err).Error("Failed to set tun tx queue length")
2019-11-19 18:00:20 +01:00
}
// Bring up the interface
ifrf.Flags = ifrf.Flags | unix.IFF_UP
if err = ioctl(fd, unix.SIOCSIFFLAGS, uintptr(unsafe.Pointer(&ifrf))); err != nil {
2019-11-24 08:18:03 +01:00
return fmt.Errorf("failed to bring the tun device up: %s", err)
2019-11-19 18:00:20 +01:00
}
// Set the routes
link, err := netlink.LinkByName(t.Device)
2019-11-19 18:00:20 +01:00
if err != nil {
2019-11-24 08:18:03 +01:00
return fmt.Errorf("failed to get tun device link: %s", err)
2019-11-19 18:00:20 +01:00
}
// Default route
dr := &net.IPNet{IP: t.Cidr.IP.Mask(t.Cidr.Mask), Mask: t.Cidr.Mask}
2019-11-19 18:00:20 +01:00
nr := netlink.Route{
LinkIndex: link.Attrs().Index,
Dst: dr,
MTU: t.DefaultMTU,
AdvMSS: t.advMSS(Route{}),
2019-11-19 18:00:20 +01:00
Scope: unix.RT_SCOPE_LINK,
Src: t.Cidr.IP,
2019-11-19 18:00:20 +01:00
Protocol: unix.RTPROT_KERNEL,
Table: unix.RT_TABLE_MAIN,
Type: unix.RTN_UNICAST,
}
err = netlink.RouteReplace(&nr)
if err != nil {
return fmt.Errorf("failed to set mtu %v on the default route %v; %v", t.DefaultMTU, dr, err)
2019-11-19 18:00:20 +01:00
}
// Path routes
for _, r := range t.Routes {
2019-11-19 18:00:20 +01:00
nr := netlink.Route{
LinkIndex: link.Attrs().Index,
2021-11-11 23:37:29 +01:00
Dst: r.Cidr,
MTU: r.MTU,
AdvMSS: t.advMSS(r),
2019-11-19 18:00:20 +01:00
Scope: unix.RT_SCOPE_LINK,
}
err = netlink.RouteAdd(&nr)
if err != nil {
2021-11-11 23:37:29 +01:00
return fmt.Errorf("failed to set mtu %v on route %v; %v", r.MTU, r.Cidr, err)
2019-11-19 18:00:20 +01:00
}
}
2019-12-12 17:34:17 +01:00
// Unsafe path routes
for _, r := range t.UnsafeRoutes {
2019-12-12 17:34:17 +01:00
nr := netlink.Route{
LinkIndex: link.Attrs().Index,
2021-11-11 23:37:29 +01:00
Dst: r.Cidr,
MTU: r.MTU,
Priority: r.Metric,
AdvMSS: t.advMSS(r),
2019-12-12 17:34:17 +01:00
Scope: unix.RT_SCOPE_LINK,
}
err = netlink.RouteAdd(&nr)
if err != nil {
2021-11-11 23:37:29 +01:00
return fmt.Errorf("failed to set mtu %v on route %v; %v", r.MTU, r.Cidr, err)
2019-12-12 17:34:17 +01:00
}
}
2019-11-19 18:00:20 +01:00
// Run the interface
ifrf.Flags = ifrf.Flags | unix.IFF_UP | unix.IFF_RUNNING
if err = ioctl(fd, unix.SIOCSIFFLAGS, uintptr(unsafe.Pointer(&ifrf))); err != nil {
2019-11-24 08:18:03 +01:00
return fmt.Errorf("failed to run tun device: %s", err)
2019-11-19 18:00:20 +01:00
}
return nil
}
func (t *tun) CidrNet() *net.IPNet {
return t.Cidr
}
func (t *tun) DeviceName() string {
return t.Device
}
func (t tun) advMSS(r Route) int {
2021-11-11 23:37:29 +01:00
mtu := r.MTU
if r.MTU == 0 {
mtu = t.DefaultMTU
}
// We only need to set advmss if the route MTU does not match the device MTU
if mtu != t.MaxMTU {
return mtu - 40
}
return 0
}