mirror of
https://github.com/yggdrasil-network/yggdrasil-go.git
synced 2024-11-24 18:45:21 +00:00
Add FreeBSD support in TAP mode
This commit is contained in:
parent
57777b6152
commit
c30792245a
@ -7,7 +7,7 @@ import (
|
|||||||
"os/exec"
|
"os/exec"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/neilalexander/water"
|
"github.com/yggdrasil-network/water"
|
||||||
)
|
)
|
||||||
|
|
||||||
const mtu = 65535
|
const mtu = 65535
|
||||||
|
@ -6,7 +6,7 @@ import (
|
|||||||
"net"
|
"net"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
|
|
||||||
"github.com/neilalexander/water"
|
"github.com/yggdrasil-network/water"
|
||||||
)
|
)
|
||||||
|
|
||||||
const mtu = 65535
|
const mtu = 65535
|
||||||
|
@ -6,7 +6,7 @@ import (
|
|||||||
"net"
|
"net"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
|
|
||||||
"github.com/neilalexander/water"
|
"github.com/yggdrasil-network/water"
|
||||||
)
|
)
|
||||||
|
|
||||||
const mtu = 65535
|
const mtu = 65535
|
||||||
|
@ -8,7 +8,7 @@ import "strconv"
|
|||||||
import "encoding/binary"
|
import "encoding/binary"
|
||||||
import "golang.org/x/sys/unix"
|
import "golang.org/x/sys/unix"
|
||||||
|
|
||||||
import water "github.com/neilalexander/water"
|
import water "github.com/yggdrasil-network/water"
|
||||||
|
|
||||||
func getDefaults() tunDefaultParameters {
|
func getDefaults() tunDefaultParameters {
|
||||||
return tunDefaultParameters{
|
return tunDefaultParameters{
|
||||||
|
138
src/yggdrasil/tun_freebsd.go
Normal file
138
src/yggdrasil/tun_freebsd.go
Normal file
@ -0,0 +1,138 @@
|
|||||||
|
package yggdrasil
|
||||||
|
|
||||||
|
import "os/exec"
|
||||||
|
import "unsafe"
|
||||||
|
//import "syscall"
|
||||||
|
import "golang.org/x/sys/unix"
|
||||||
|
|
||||||
|
import water "github.com/yggdrasil-network/water"
|
||||||
|
|
||||||
|
// This is to catch FreeBSD and NetBSD
|
||||||
|
|
||||||
|
func getDefaults() tunDefaultParameters {
|
||||||
|
return tunDefaultParameters{
|
||||||
|
maximumIfMTU: 32767,
|
||||||
|
defaultIfMTU: 32767,
|
||||||
|
defaultIfName: "/dev/tap0",
|
||||||
|
defaultIfTAPMode: true,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Warning! When porting this to other BSDs, the tuninfo struct can appear with
|
||||||
|
// the fields in a different order, and the consts below might also have
|
||||||
|
// different values
|
||||||
|
|
||||||
|
/*
|
||||||
|
FreeBSD/NetBSD, net/if_tun.h:
|
||||||
|
|
||||||
|
struct tuninfo {
|
||||||
|
int baudrate;
|
||||||
|
short mtu;
|
||||||
|
u_char type;
|
||||||
|
u_char dummy;
|
||||||
|
};
|
||||||
|
*/
|
||||||
|
|
||||||
|
type tuninfo struct {
|
||||||
|
tun_baudrate int32
|
||||||
|
tun_mtu int16
|
||||||
|
tun_type uint8
|
||||||
|
tun_dummy uint8
|
||||||
|
}
|
||||||
|
|
||||||
|
const TUNSIFINFO = (0x80000000) | ((8 & 0x1fff) << 16) | uint32(byte('t'))<<8 | 91
|
||||||
|
const TUNGIFINFO = (0x40000000) | ((8 & 0x1fff) << 16) | uint32(byte('t'))<<8 | 92
|
||||||
|
const TUNSIFHEAD = (0x80000000) | ((4 & 0x1fff) << 16) | uint32(byte('t'))<<8 | 96
|
||||||
|
const SIOCAIFADDR_IN6 = (0x80000000) | ((4 & 0x1fff) << 16) | uint32(byte('i'))<<8 | 27
|
||||||
|
|
||||||
|
// Below this point seems to be fairly standard at least...
|
||||||
|
|
||||||
|
type in6_addrlifetime struct {
|
||||||
|
ia6t_expire float64
|
||||||
|
ia6t_preferred float64
|
||||||
|
ia6t_vltime uint32
|
||||||
|
ia6t_pltime uint32
|
||||||
|
}
|
||||||
|
|
||||||
|
type sockaddr_in6 struct {
|
||||||
|
sin6_len uint8
|
||||||
|
sin6_family uint8
|
||||||
|
sin6_port uint8
|
||||||
|
sin6_flowinfo uint32
|
||||||
|
sin6_addr [8]uint16
|
||||||
|
sin6_scope_id uint32
|
||||||
|
}
|
||||||
|
|
||||||
|
type in6_aliasreq struct {
|
||||||
|
ifra_name [16]byte
|
||||||
|
ifra_addr sockaddr_in6
|
||||||
|
ifra_dstaddr sockaddr_in6
|
||||||
|
ifra_prefixmask sockaddr_in6
|
||||||
|
ifra_flags uint32
|
||||||
|
ifra_lifetime in6_addrlifetime
|
||||||
|
}
|
||||||
|
|
||||||
|
func (tun *tunDevice) setup(ifname string, iftapmode bool, addr string, mtu int) error {
|
||||||
|
var config water.Config
|
||||||
|
if ifname[:4] == "auto" {
|
||||||
|
ifname = "/dev/tap0"
|
||||||
|
}
|
||||||
|
if len(ifname) < 9 {
|
||||||
|
panic("TUN/TAP name must be in format /dev/tunX or /dev/tapX")
|
||||||
|
}
|
||||||
|
switch {
|
||||||
|
case iftapmode || ifname[:8] == "/dev/tap":
|
||||||
|
config = water.Config{DeviceType: water.TAP}
|
||||||
|
case !iftapmode || ifname[:8] == "/dev/tun":
|
||||||
|
//config = water.Config{DeviceType: water.TUN}
|
||||||
|
panic("TUN mode is not currently supported on this platform, please use TAP instead")
|
||||||
|
default:
|
||||||
|
panic("TUN/TAP name must be in format /dev/tunX or /dev/tapX")
|
||||||
|
}
|
||||||
|
config.Name = ifname
|
||||||
|
iface, err := water.New(config)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
tun.iface = iface
|
||||||
|
tun.mtu = getSupportedMTU(mtu)
|
||||||
|
return tun.setupAddress(addr)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (tun *tunDevice) setupAddress(addr string) error {
|
||||||
|
fd := tun.iface.FD().Fd()
|
||||||
|
var err error
|
||||||
|
var ti tuninfo
|
||||||
|
|
||||||
|
tun.core.log.Printf("Interface name: %s", tun.iface.Name())
|
||||||
|
tun.core.log.Printf("Interface IPv6: %s", addr)
|
||||||
|
tun.core.log.Printf("Interface MTU: %d", tun.mtu)
|
||||||
|
|
||||||
|
// Get the existing interface flags
|
||||||
|
if _, _, errno := unix.Syscall(unix.SYS_IOCTL, uintptr(fd), uintptr(TUNGIFINFO), uintptr(unsafe.Pointer(&ti))); errno != 0 {
|
||||||
|
err = errno
|
||||||
|
tun.core.log.Printf("Error in TUNGIFINFO: %v", errno)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set the new MTU
|
||||||
|
ti.tun_mtu = int16(tun.mtu)
|
||||||
|
|
||||||
|
// Set the new interface flags
|
||||||
|
if _, _, errno := unix.Syscall(unix.SYS_IOCTL, uintptr(fd), uintptr(TUNSIFINFO), uintptr(unsafe.Pointer(&ti))); errno != 0 {
|
||||||
|
err = errno
|
||||||
|
tun.core.log.Printf("Error in TUNSIFINFO: %v", errno)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set address
|
||||||
|
cmd := exec.Command("ifconfig", tun.iface.Name(), "inet6", addr)
|
||||||
|
//tun.core.log.Printf("ifconfig command: %v", strings.Join(cmd.Args, " "))
|
||||||
|
output, err := cmd.CombinedOutput()
|
||||||
|
if err != nil {
|
||||||
|
tun.core.log.Printf("ifconfig failed: %v.", err)
|
||||||
|
tun.core.log.Println(string(output))
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
@ -7,7 +7,7 @@ import "errors"
|
|||||||
import "fmt"
|
import "fmt"
|
||||||
import "net"
|
import "net"
|
||||||
|
|
||||||
import water "github.com/neilalexander/water"
|
import water "github.com/yggdrasil-network/water"
|
||||||
|
|
||||||
import "github.com/docker/libcontainer/netlink"
|
import "github.com/docker/libcontainer/netlink"
|
||||||
|
|
||||||
|
@ -1,5 +1,3 @@
|
|||||||
// +build openbsd
|
|
||||||
|
|
||||||
package yggdrasil
|
package yggdrasil
|
||||||
|
|
||||||
import "os/exec"
|
import "os/exec"
|
||||||
@ -7,7 +5,7 @@ import "unsafe"
|
|||||||
import "syscall"
|
import "syscall"
|
||||||
import "golang.org/x/sys/unix"
|
import "golang.org/x/sys/unix"
|
||||||
|
|
||||||
import water "github.com/neilalexander/water"
|
import water "github.com/yggdrasil-network/water"
|
||||||
|
|
||||||
// This is to catch OpenBSD
|
// This is to catch OpenBSD
|
||||||
|
|
||||||
@ -28,11 +26,22 @@ func getDefaults() tunDefaultParameters {
|
|||||||
// the fields in a different order, and the consts below might also have
|
// the fields in a different order, and the consts below might also have
|
||||||
// different values
|
// different values
|
||||||
|
|
||||||
|
/*
|
||||||
|
OpenBSD, net/if_tun.h:
|
||||||
|
|
||||||
|
struct tuninfo {
|
||||||
|
u_int mtu;
|
||||||
|
u_short type;
|
||||||
|
u_short flags;
|
||||||
|
u_int baudrate;
|
||||||
|
};
|
||||||
|
*/
|
||||||
|
|
||||||
type tuninfo struct {
|
type tuninfo struct {
|
||||||
tun_mtu uint16
|
tun_mtu uint32
|
||||||
tun_type uint32
|
tun_type uint16
|
||||||
tun_flags uint32
|
tun_flags uint16
|
||||||
tun_dummy uint16
|
tun_baudrate uint32
|
||||||
}
|
}
|
||||||
|
|
||||||
const TUNSIFINFO = (0x80000000) | ((12 & 0x1fff) << 16) | uint32(byte('t'))<<8 | 91
|
const TUNSIFINFO = (0x80000000) | ((12 & 0x1fff) << 16) | uint32(byte('t'))<<8 | 91
|
||||||
@ -79,7 +88,7 @@ func (tun *tunDevice) setup(ifname string, iftapmode bool, addr string, mtu int)
|
|||||||
config = water.Config{DeviceType: water.TAP}
|
config = water.Config{DeviceType: water.TAP}
|
||||||
case !iftapmode || ifname[:8] == "/dev/tun":
|
case !iftapmode || ifname[:8] == "/dev/tun":
|
||||||
// config = water.Config{DeviceType: water.TUN}
|
// config = water.Config{DeviceType: water.TUN}
|
||||||
panic("TUN mode is not currently supported on OpenBSD, please use TAP instead")
|
panic("TUN mode is not currently supported on this platform, please use TAP instead")
|
||||||
default:
|
default:
|
||||||
panic("TUN/TAP name must be in format /dev/tunX or /dev/tapX")
|
panic("TUN/TAP name must be in format /dev/tunX or /dev/tapX")
|
||||||
}
|
}
|
||||||
@ -111,7 +120,7 @@ func (tun *tunDevice) setupAddress(addr string) error {
|
|||||||
//tun.core.log.Printf("TUNGIFINFO: %+v", ti)
|
//tun.core.log.Printf("TUNGIFINFO: %+v", ti)
|
||||||
|
|
||||||
// Set the new MTU
|
// Set the new MTU
|
||||||
ti.tun_mtu = uint16(tun.mtu)
|
ti.tun_mtu = uint32(tun.mtu)
|
||||||
|
|
||||||
// Set the new interface flags
|
// Set the new interface flags
|
||||||
ti.tun_flags |= syscall.IFF_UP
|
ti.tun_flags |= syscall.IFF_UP
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
// +build !linux,!darwin,!windows,!openbsd
|
// +build !linux,!darwin,!windows,!openbsd,!freebsd
|
||||||
|
|
||||||
package yggdrasil
|
package yggdrasil
|
||||||
|
|
||||||
import water "github.com/neilalexander/water"
|
import water "github.com/yggdrasil-network/water"
|
||||||
|
|
||||||
// This is to catch unsupported platforms
|
// This is to catch unsupported platforms
|
||||||
// If your platform supports tun devices, you could try configuring it manually
|
// If your platform supports tun devices, you could try configuring it manually
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
package yggdrasil
|
package yggdrasil
|
||||||
|
|
||||||
import water "github.com/neilalexander/water"
|
import water "github.com/yggdrasil-network/water"
|
||||||
import "os/exec"
|
import "os/exec"
|
||||||
import "strings"
|
import "strings"
|
||||||
import "fmt"
|
import "fmt"
|
||||||
|
Loading…
Reference in New Issue
Block a user