2018-03-01 15:11:12 +00:00
|
|
|
// +build openbsd
|
2018-03-01 11:49:49 +00:00
|
|
|
|
|
|
|
package yggdrasil
|
|
|
|
|
2018-03-01 13:37:05 +00:00
|
|
|
import "os/exec"
|
2018-03-01 15:02:53 +00:00
|
|
|
import "unsafe"
|
|
|
|
import "syscall"
|
|
|
|
import "golang.org/x/sys/unix"
|
2018-03-01 13:37:05 +00:00
|
|
|
|
2018-03-01 11:49:49 +00:00
|
|
|
import water "github.com/neilalexander/water"
|
|
|
|
|
2018-03-01 15:11:12 +00:00
|
|
|
// This is to catch OpenBSD
|
|
|
|
|
2018-03-01 15:19:20 +00:00
|
|
|
// 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
|
|
|
|
|
2018-03-01 15:11:12 +00:00
|
|
|
// 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
|
|
|
|
}
|
2018-03-01 11:49:49 +00:00
|
|
|
|
2018-03-01 15:02:53 +00:00
|
|
|
const TUNSIFINFO = (0x80000000) | ((12 & 0x1fff) << 16) | uint32(byte('t'))<<8 | 91
|
|
|
|
const TUNGIFINFO = (0x40000000) | ((12 & 0x1fff) << 16) | uint32(byte('t'))<<8 | 92
|
2018-03-01 13:37:05 +00:00
|
|
|
const SIOCAIFADDR_IN6 = (0x80000000) | ((4 & 0x1fff) << 16) | uint32(byte('i'))<<8 | 27
|
|
|
|
|
2018-03-01 15:11:12 +00:00
|
|
|
// Below this point seems to be fairly standard at least...
|
|
|
|
|
2018-03-01 13:37:05 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2018-03-01 11:49:49 +00:00
|
|
|
func (tun *tunDevice) setup(ifname string, iftapmode bool, addr string, mtu int) error {
|
|
|
|
var config water.Config
|
2018-03-01 15:19:20 +00:00
|
|
|
if ifname[:4] == "auto" {
|
|
|
|
ifname = "/dev/tap0"
|
|
|
|
}
|
|
|
|
if len(ifname) < 9 {
|
|
|
|
panic("TUN/TAP name must be in format /dev/tunX or /dev/tapX")
|
|
|
|
}
|
2018-03-01 13:37:05 +00:00
|
|
|
switch {
|
|
|
|
case iftapmode || ifname[:8] == "/dev/tap":
|
2018-03-01 11:49:49 +00:00
|
|
|
config = water.Config{DeviceType: water.TAP}
|
2018-03-01 13:37:05 +00:00
|
|
|
case !iftapmode || ifname[:8] == "/dev/tun":
|
2018-03-01 15:19:20 +00:00
|
|
|
// config = water.Config{DeviceType: water.TUN}
|
|
|
|
panic("TUN mode is not currently supported on OpenBSD, please use TAP instead")
|
2018-03-01 13:37:05 +00:00
|
|
|
default:
|
|
|
|
panic("TUN/TAP name must be in format /dev/tunX or /dev/tapX")
|
2018-03-01 11:49:49 +00:00
|
|
|
}
|
|
|
|
config.Name = ifname
|
|
|
|
iface, err := water.New(config)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
tun.iface = iface
|
|
|
|
tun.mtu = mtu //1280 // Lets default to the smallest thing allowed for now
|
|
|
|
return tun.setupAddress(addr)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tun *tunDevice) setupAddress(addr string) error {
|
2018-03-01 15:02:53 +00:00
|
|
|
fd := tun.iface.FD().Fd()
|
|
|
|
var err error
|
|
|
|
var ti tuninfo
|
|
|
|
|
2018-03-01 15:31:49 +00:00
|
|
|
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)
|
|
|
|
|
2018-03-01 15:02:53 +00:00
|
|
|
// 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
|
|
|
|
}
|
2018-03-01 15:31:49 +00:00
|
|
|
//tun.core.log.Printf("TUNGIFINFO: %+v", ti)
|
2018-03-01 15:02:53 +00:00
|
|
|
|
|
|
|
// 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
|
2018-03-01 15:31:49 +00:00
|
|
|
//tun.core.log.Printf("TUNSIFINFO: %+v", ti)
|
2018-03-01 15:02:53 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2018-03-01 13:37:05 +00:00
|
|
|
// Set address
|
|
|
|
cmd := exec.Command("ifconfig", tun.iface.Name(), "inet6", addr)
|
2018-03-01 15:31:49 +00:00
|
|
|
//tun.core.log.Printf("ifconfig command: %v", strings.Join(cmd.Args, " "))
|
2018-03-01 13:37:05 +00:00
|
|
|
output, err := cmd.CombinedOutput()
|
|
|
|
if err != nil {
|
2018-03-01 15:02:53 +00:00
|
|
|
tun.core.log.Printf("ifconfig failed: %v.", err)
|
2018-03-01 13:37:05 +00:00
|
|
|
tun.core.log.Println(string(output))
|
|
|
|
}
|
|
|
|
|
2018-03-01 11:49:49 +00:00
|
|
|
return nil
|
|
|
|
}
|