nebula/tun_darwin.go

90 lines
2.4 KiB
Go
Raw Normal View History

// +build !ios
2019-11-19 18:00:20 +01:00
package nebula
import (
"fmt"
"io"
2019-11-19 18:00:20 +01:00
"net"
"os/exec"
"strconv"
2021-03-26 15:46:30 +01:00
"github.com/sirupsen/logrus"
"github.com/songgao/water"
2019-11-19 18:00:20 +01:00
)
type Tun struct {
Device string
Cidr *net.IPNet
MTU int
UnsafeRoutes []route
2021-03-26 15:46:30 +01:00
l *logrus.Logger
2019-11-19 18:00:20 +01:00
*water.Interface
}
2021-03-26 15:46:30 +01:00
func newTun(l *logrus.Logger, deviceName string, cidr *net.IPNet, defaultMTU int, routes []route, unsafeRoutes []route, txQueueLen int, multiqueue bool) (ifce *Tun, err error) {
2019-11-19 18:00:20 +01:00
if len(routes) > 0 {
return nil, fmt.Errorf("route MTU not supported in Darwin")
2019-12-12 17:34:17 +01:00
}
2019-11-19 18:00:20 +01:00
// NOTE: You cannot set the deviceName under Darwin, so you must check tun.Device after calling .Activate()
return &Tun{
Cidr: cidr,
MTU: defaultMTU,
UnsafeRoutes: unsafeRoutes,
2021-03-26 15:46:30 +01:00
l: l,
2019-11-19 18:00:20 +01:00
}, nil
}
2021-03-26 15:46:30 +01:00
func newTunFromFd(l *logrus.Logger, deviceFd int, cidr *net.IPNet, defaultMTU int, routes []route, unsafeRoutes []route, txQueueLen int) (ifce *Tun, err error) {
return nil, fmt.Errorf("newTunFromFd not supported in Darwin")
}
2019-11-19 18:00:20 +01:00
func (c *Tun) Activate() error {
var err error
c.Interface, err = water.New(water.Config{
DeviceType: water.TUN,
})
if err != nil {
return fmt.Errorf("activate failed: %v", err)
2019-11-19 18:00:20 +01:00
}
c.Device = c.Interface.Name()
// TODO use syscalls instead of exec.Command
if err = exec.Command("/sbin/ifconfig", c.Device, c.Cidr.String(), c.Cidr.IP.String()).Run(); err != nil {
2019-11-19 18:00:20 +01:00
return fmt.Errorf("failed to run 'ifconfig': %s", err)
}
if err = exec.Command("/sbin/route", "-n", "add", "-net", c.Cidr.String(), "-interface", c.Device).Run(); err != nil {
2019-11-19 18:00:20 +01:00
return fmt.Errorf("failed to run 'route add': %s", err)
}
if err = exec.Command("/sbin/ifconfig", c.Device, "mtu", strconv.Itoa(c.MTU)).Run(); err != nil {
2019-11-19 18:00:20 +01:00
return fmt.Errorf("failed to run 'ifconfig': %s", err)
}
// Unsafe path routes
for _, r := range c.UnsafeRoutes {
if err = exec.Command("/sbin/route", "-n", "add", "-net", r.route.String(), "-interface", c.Device).Run(); err != nil {
return fmt.Errorf("failed to run 'route add' for unsafe_route %s: %s", r.route.String(), err)
}
}
2019-11-19 18:00:20 +01:00
return nil
}
func (c *Tun) CidrNet() *net.IPNet {
return c.Cidr
}
func (c *Tun) DeviceName() string {
return c.Device
}
2019-11-19 18:00:20 +01:00
func (c *Tun) WriteRaw(b []byte) error {
_, err := c.Write(b)
return err
}
func (t *Tun) NewMultiQueueReader() (io.ReadWriteCloser, error) {
return nil, fmt.Errorf("TODO: multiqueue not implemented for darwin")
}