mirror of
https://github.com/yggdrasil-network/yggdrasil-go
synced 2024-11-13 00:40:24 +03:00
Merge pull request #23 from neilalexander/bsd
Add support for running OpenBSD
This commit is contained in:
commit
99d0740eaa
@ -7,7 +7,7 @@ import (
|
|||||||
"os/exec"
|
"os/exec"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/songgao/water"
|
"github.com/neilalexander/water"
|
||||||
)
|
)
|
||||||
|
|
||||||
const mtu = 65535
|
const mtu = 65535
|
||||||
|
@ -6,7 +6,7 @@ import (
|
|||||||
"net"
|
"net"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
|
|
||||||
"github.com/songgao/water"
|
"github.com/neilalexander/water"
|
||||||
)
|
)
|
||||||
|
|
||||||
const mtu = 65535
|
const mtu = 65535
|
||||||
|
@ -6,7 +6,7 @@ import (
|
|||||||
"net"
|
"net"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
|
|
||||||
"github.com/songgao/water"
|
"github.com/neilalexander/water"
|
||||||
)
|
)
|
||||||
|
|
||||||
const mtu = 65535
|
const mtu = 65535
|
||||||
|
@ -159,6 +159,20 @@ func (c *Core) DEBUG_getDHTSize() int {
|
|||||||
return total
|
return total
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TUN defaults
|
||||||
|
|
||||||
|
func (c *Core) DEBUG_GetTUNDefaultIfName() string {
|
||||||
|
return getDefaults().defaultIfName
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Core) DEBUG_GetTUNDefaultIfMTU() int {
|
||||||
|
return getDefaults().defaultIfMTU
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Core) DEBUG_GetTUNDefaultIfTAPMode() bool {
|
||||||
|
return getDefaults().defaultIfTAPMode
|
||||||
|
}
|
||||||
|
|
||||||
// udpInterface
|
// udpInterface
|
||||||
// FIXME udpInterface isn't exported
|
// FIXME udpInterface isn't exported
|
||||||
// So debug functions need to work differently...
|
// So debug functions need to work differently...
|
||||||
|
@ -2,6 +2,7 @@ package yggdrasil
|
|||||||
|
|
||||||
// This manages the tun driver to send/recv packets to/from applications
|
// This manages the tun driver to send/recv packets to/from applications
|
||||||
|
|
||||||
|
import "os"
|
||||||
import ethernet "github.com/songgao/packets/ethernet"
|
import ethernet "github.com/songgao/packets/ethernet"
|
||||||
|
|
||||||
const IPv6_HEADER_LENGTH = 40
|
const IPv6_HEADER_LENGTH = 40
|
||||||
@ -14,6 +15,7 @@ type tunInterface interface {
|
|||||||
Read(to []byte) (int, error)
|
Read(to []byte) (int, error)
|
||||||
Write(from []byte) (int, error)
|
Write(from []byte) (int, error)
|
||||||
Close() error
|
Close() error
|
||||||
|
FD() *os.File
|
||||||
}
|
}
|
||||||
|
|
||||||
type tunDevice struct {
|
type tunDevice struct {
|
||||||
@ -25,6 +27,20 @@ type tunDevice struct {
|
|||||||
iface tunInterface
|
iface tunInterface
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type tunDefaultParameters struct {
|
||||||
|
maximumIfMTU int
|
||||||
|
defaultIfMTU int
|
||||||
|
defaultIfName string
|
||||||
|
defaultIfTAPMode bool
|
||||||
|
}
|
||||||
|
|
||||||
|
func getSupportedMTU(mtu int) int {
|
||||||
|
if mtu > getDefaults().maximumIfMTU {
|
||||||
|
return getDefaults().maximumIfMTU
|
||||||
|
}
|
||||||
|
return mtu
|
||||||
|
}
|
||||||
|
|
||||||
func (tun *tunDevice) init(core *Core) {
|
func (tun *tunDevice) init(core *Core) {
|
||||||
tun.core = core
|
tun.core = core
|
||||||
tun.icmpv6.init(tun)
|
tun.icmpv6.init(tun)
|
||||||
|
@ -8,7 +8,16 @@ import "strconv"
|
|||||||
import "encoding/binary"
|
import "encoding/binary"
|
||||||
import "golang.org/x/sys/unix"
|
import "golang.org/x/sys/unix"
|
||||||
|
|
||||||
import water "github.com/songgao/water"
|
import water "github.com/neilalexander/water"
|
||||||
|
|
||||||
|
func getDefaults() tunDefaultParameters {
|
||||||
|
return tunDefaultParameters{
|
||||||
|
maximumIfMTU: 65535,
|
||||||
|
defaultIfMTU: 65535,
|
||||||
|
defaultIfName: "auto",
|
||||||
|
defaultIfTAPMode: false,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func (tun *tunDevice) setup(ifname string, iftapmode bool, addr string, mtu int) error {
|
func (tun *tunDevice) setup(ifname string, iftapmode bool, addr string, mtu int) error {
|
||||||
if iftapmode {
|
if iftapmode {
|
||||||
@ -20,7 +29,7 @@ func (tun *tunDevice) setup(ifname string, iftapmode bool, addr string, mtu int)
|
|||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
tun.iface = iface
|
tun.iface = iface
|
||||||
tun.mtu = mtu
|
tun.mtu = getSupportedMTU(mtu)
|
||||||
return tun.setupAddress(addr)
|
return tun.setupAddress(addr)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -7,7 +7,16 @@ import "fmt"
|
|||||||
import "os/exec"
|
import "os/exec"
|
||||||
import "strings"
|
import "strings"
|
||||||
|
|
||||||
import water "github.com/songgao/water"
|
import water "github.com/neilalexander/water"
|
||||||
|
|
||||||
|
func getDefaults() tunDefaultParameters {
|
||||||
|
return tunDefaultParameters{
|
||||||
|
maximumIfMTU: 65535,
|
||||||
|
defaultIfMTU: 65535,
|
||||||
|
defaultIfName: "auto",
|
||||||
|
defaultIfTAPMode: false,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func (tun *tunDevice) setup(ifname string, iftapmode bool, addr string, mtu int) error {
|
func (tun *tunDevice) setup(ifname string, iftapmode bool, addr string, mtu int) error {
|
||||||
var config water.Config
|
var config water.Config
|
||||||
@ -24,7 +33,7 @@ func (tun *tunDevice) setup(ifname string, iftapmode bool, addr string, mtu int)
|
|||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
tun.iface = iface
|
tun.iface = iface
|
||||||
tun.mtu = mtu //1280 // Lets default to the smallest thing allowed for now
|
tun.mtu = getSupportedMTU(mtu)
|
||||||
return tun.setupAddress(addr)
|
return tun.setupAddress(addr)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
144
src/yggdrasil/tun_openbsd.go
Normal file
144
src/yggdrasil/tun_openbsd.go
Normal file
@ -0,0 +1,144 @@
|
|||||||
|
// +build openbsd
|
||||||
|
|
||||||
|
package yggdrasil
|
||||||
|
|
||||||
|
import "os/exec"
|
||||||
|
import "unsafe"
|
||||||
|
import "syscall"
|
||||||
|
import "golang.org/x/sys/unix"
|
||||||
|
|
||||||
|
import water "github.com/neilalexander/water"
|
||||||
|
|
||||||
|
// This is to catch OpenBSD
|
||||||
|
|
||||||
|
// TODO: Fix TUN mode for OpenBSD. It turns out that OpenBSD doesn't have a way
|
||||||
|
// to disable the PI header when in TUN mode, so we need to modify the read/
|
||||||
|
// writes to handle those first four bytes
|
||||||
|
|
||||||
|
func getDefaults() tunDefaultParameters {
|
||||||
|
return tunDefaultParameters{
|
||||||
|
maximumIfMTU: 16384,
|
||||||
|
defaultIfMTU: 16384,
|
||||||
|
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
|
||||||
|
|
||||||
|
type tuninfo struct {
|
||||||
|
tun_mtu uint16
|
||||||
|
tun_type uint32
|
||||||
|
tun_flags uint32
|
||||||
|
tun_dummy uint16
|
||||||
|
}
|
||||||
|
|
||||||
|
const TUNSIFINFO = (0x80000000) | ((12 & 0x1fff) << 16) | uint32(byte('t'))<<8 | 91
|
||||||
|
const TUNGIFINFO = (0x40000000) | ((12 & 0x1fff) << 16) | uint32(byte('t'))<<8 | 92
|
||||||
|
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 OpenBSD, 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
|
||||||
|
}
|
||||||
|
//tun.core.log.Printf("TUNGIFINFO: %+v", ti)
|
||||||
|
|
||||||
|
// Set the new MTU
|
||||||
|
ti.tun_mtu = uint16(tun.mtu)
|
||||||
|
|
||||||
|
// Set the new interface flags
|
||||||
|
ti.tun_flags |= syscall.IFF_UP
|
||||||
|
switch {
|
||||||
|
case tun.iface.IsTAP():
|
||||||
|
ti.tun_flags |= syscall.IFF_MULTICAST
|
||||||
|
ti.tun_flags |= syscall.IFF_BROADCAST
|
||||||
|
case tun.iface.IsTUN():
|
||||||
|
ti.tun_flags |= syscall.IFF_POINTOPOINT
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set the new interface flags
|
||||||
|
//tun.core.log.Printf("TUNSIFINFO: %+v", ti)
|
||||||
|
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
|
||||||
|
}
|
@ -1,14 +1,21 @@
|
|||||||
// +build !linux
|
// +build !linux,!darwin,!windows,!openbsd
|
||||||
// +build !darwin
|
|
||||||
// +build !windows
|
|
||||||
|
|
||||||
package yggdrasil
|
package yggdrasil
|
||||||
|
|
||||||
import water "github.com/songgao/water"
|
import water "github.com/neilalexander/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
|
||||||
|
|
||||||
|
func getDefaults() tunDefaultParameters {
|
||||||
|
return tunDefaultParameters{
|
||||||
|
maximumIfMTU: 65535,
|
||||||
|
defaultIfMTU: 65535,
|
||||||
|
defaultIfName: "none",
|
||||||
|
defaultIfTAPMode: false,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func (tun *tunDevice) setup(ifname string, iftapmode bool, addr string, mtu int) error {
|
func (tun *tunDevice) setup(ifname string, iftapmode bool, addr string, mtu int) error {
|
||||||
var config water.Config
|
var config water.Config
|
||||||
if iftapmode {
|
if iftapmode {
|
||||||
@ -21,7 +28,7 @@ func (tun *tunDevice) setup(ifname string, iftapmode bool, addr string, mtu int)
|
|||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
tun.iface = iface
|
tun.iface = iface
|
||||||
tun.mtu = mtu //1280 // Lets default to the smallest thing allowed for now
|
tun.mtu = getSupportedMTU(mtu)
|
||||||
return tun.setupAddress(addr)
|
return tun.setupAddress(addr)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,12 +1,21 @@
|
|||||||
package yggdrasil
|
package yggdrasil
|
||||||
|
|
||||||
import water "github.com/songgao/water"
|
import water "github.com/neilalexander/water"
|
||||||
import "os/exec"
|
import "os/exec"
|
||||||
import "strings"
|
import "strings"
|
||||||
import "fmt"
|
import "fmt"
|
||||||
|
|
||||||
// This is to catch Windows platforms
|
// This is to catch Windows platforms
|
||||||
|
|
||||||
|
func getDefaults() tunDefaultParameters {
|
||||||
|
return tunDefaultParameters{
|
||||||
|
maximumIfMTU: 65535,
|
||||||
|
defaultIfMTU: 65535,
|
||||||
|
defaultIfName: "auto",
|
||||||
|
defaultIfTAPMode: true,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func (tun *tunDevice) setup(ifname string, iftapmode bool, addr string, mtu int) error {
|
func (tun *tunDevice) setup(ifname string, iftapmode bool, addr string, mtu int) error {
|
||||||
if !iftapmode {
|
if !iftapmode {
|
||||||
tun.core.log.Printf("TUN mode is not supported on this platform, defaulting to TAP")
|
tun.core.log.Printf("TUN mode is not supported on this platform, defaulting to TAP")
|
||||||
@ -41,7 +50,7 @@ func (tun *tunDevice) setup(ifname string, iftapmode bool, addr string, mtu int)
|
|||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
tun.iface = iface
|
tun.iface = iface
|
||||||
tun.mtu = mtu
|
tun.mtu = getSupportedMTU(mtu)
|
||||||
err = tun.setupMTU(tun.mtu)
|
err = tun.setupMTU(tun.mtu)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
|
10
yggdrasil.go
10
yggdrasil.go
@ -117,13 +117,9 @@ func generateConfig() *nodeConfig {
|
|||||||
cfg.Peers = []string{}
|
cfg.Peers = []string{}
|
||||||
cfg.Multicast = true
|
cfg.Multicast = true
|
||||||
cfg.LinkLocal = ""
|
cfg.LinkLocal = ""
|
||||||
cfg.IfName = "auto"
|
cfg.IfName = core.DEBUG_GetTUNDefaultIfName()
|
||||||
cfg.IfMTU = 1280
|
cfg.IfMTU = core.DEBUG_GetTUNDefaultIfMTU()
|
||||||
if runtime.GOOS == "windows" {
|
cfg.IfTAPMode = core.DEBUG_GetTUNDefaultIfTAPMode()
|
||||||
cfg.IfTAPMode = true
|
|
||||||
} else {
|
|
||||||
cfg.IfTAPMode = false
|
|
||||||
}
|
|
||||||
return &cfg
|
return &cfg
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user