mirror of
https://github.com/yggdrasil-network/yggdrasil-go.git
synced 2025-04-28 22:25:07 +03:00
Attempt to support NetBSD
This code actually consolidates a lot of the BSD code together, and even setting the interface MTU with SIOCSIFMTU seems to work fine. What doesn't work though is setting the interface address using SIOCSIFADDR_IN6, which I attempted to plagiarise from the Darwin code. As a fallback, ifconfig is used, which solves the problem enough to get it working.
This commit is contained in:
parent
b30b6022a8
commit
166d25619d
5 changed files with 106 additions and 111 deletions
|
@ -1,15 +1,20 @@
|
|||
// +build openbsd freebsd
|
||||
// +build openbsd freebsd netbsd
|
||||
|
||||
package yggdrasil
|
||||
|
||||
import "os"
|
||||
import "os/exec"
|
||||
import "unsafe"
|
||||
import "syscall"
|
||||
import "strings"
|
||||
import "strconv"
|
||||
import "encoding/binary"
|
||||
import "os/exec"
|
||||
|
||||
import "golang.org/x/sys/unix"
|
||||
|
||||
import "github.com/yggdrasil-network/water"
|
||||
|
||||
const SIOCSIFADDR_IN6 = (0x80000000) | ((288 & 0x1fff) << 16) | uint32(byte('i'))<<8 | 12
|
||||
|
||||
type in6_addrlifetime struct {
|
||||
ia6t_expire float64
|
||||
ia6t_preferred float64
|
||||
|
@ -26,13 +31,43 @@ type sockaddr_in6 struct {
|
|||
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
|
||||
/*
|
||||
from <netinet6/in6_var.h>
|
||||
struct in6_ifreq {
|
||||
277 char ifr_name[IFNAMSIZ];
|
||||
278 union {
|
||||
279 struct sockaddr_in6 ifru_addr;
|
||||
280 struct sockaddr_in6 ifru_dstaddr;
|
||||
281 int ifru_flags;
|
||||
282 int ifru_flags6;
|
||||
283 int ifru_metric;
|
||||
284 caddr_t ifru_data;
|
||||
285 struct in6_addrlifetime ifru_lifetime;
|
||||
286 struct in6_ifstat ifru_stat;
|
||||
287 struct icmp6_ifstat ifru_icmp6stat;
|
||||
288 u_int32_t ifru_scope_id[16];
|
||||
289 } ifr_ifru;
|
||||
290 };
|
||||
*/
|
||||
|
||||
type in6_ifreq_mtu struct {
|
||||
ifr_name [syscall.IFNAMSIZ]byte
|
||||
ifru_mtu int
|
||||
}
|
||||
|
||||
type in6_ifreq_addr struct {
|
||||
ifr_name [syscall.IFNAMSIZ]byte
|
||||
ifru_addr sockaddr_in6
|
||||
}
|
||||
|
||||
type in6_ifreq_flags struct {
|
||||
ifr_name [syscall.IFNAMSIZ]byte
|
||||
flags int
|
||||
}
|
||||
|
||||
type in6_ifreq_lifetime struct {
|
||||
ifr_name [syscall.IFNAMSIZ]byte
|
||||
ifru_addrlifetime in6_addrlifetime
|
||||
}
|
||||
|
||||
func (tun *tunDevice) setup(ifname string, iftapmode bool, addr string, mtu int) error {
|
||||
|
@ -62,38 +97,67 @@ func (tun *tunDevice) setup(ifname string, iftapmode bool, addr string, mtu int)
|
|||
}
|
||||
|
||||
func (tun *tunDevice) setupAddress(addr string) error {
|
||||
fd := tun.iface.ReadWriteCloser.(*os.File).Fd()
|
||||
var sfd int
|
||||
var err error
|
||||
var ti tuninfo
|
||||
|
||||
// Create system socket
|
||||
if sfd, err = unix.Socket(unix.AF_INET, unix.SOCK_DGRAM, 0); err != nil {
|
||||
tun.core.log.Printf("Create AF_INET socket failed: %v.", err)
|
||||
return err
|
||||
}
|
||||
|
||||
// Friendly output
|
||||
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 {
|
||||
// Create the MTU request
|
||||
var ir in6_ifreq_mtu
|
||||
copy(ir.ifr_name[:], tun.iface.Name())
|
||||
ir.ifru_mtu = int(tun.mtu)
|
||||
|
||||
// Set the MTU
|
||||
if _, _, errno := unix.Syscall(unix.SYS_IOCTL, uintptr(sfd), uintptr(syscall.SIOCSIFMTU), uintptr(unsafe.Pointer(&ir))); errno != 0 {
|
||||
err = errno
|
||||
tun.core.log.Printf("Error in TUNGIFINFO: %v", errno)
|
||||
return err
|
||||
tun.core.log.Printf("Error in SIOCSIFMTU: %v", errno)
|
||||
|
||||
// Fall back to ifconfig to set the MTU
|
||||
cmd := exec.Command("ifconfig", tun.iface.Name(), "mtu", string(tun.mtu))
|
||||
tun.core.log.Printf("Using ifconfig as fallback: %v", strings.Join(cmd.Args, " "))
|
||||
output, err := cmd.CombinedOutput()
|
||||
if err != nil {
|
||||
tun.core.log.Printf("SIOCSIFMTU fallback failed: %v.", err)
|
||||
tun.core.log.Println(string(output))
|
||||
}
|
||||
}
|
||||
|
||||
// Update with any OS-specific flags, MTU, etc.
|
||||
ti.setInfo(tun)
|
||||
|
||||
// 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
|
||||
// Create the address request
|
||||
// FIXME: I don't work!
|
||||
var ar in6_ifreq_addr
|
||||
copy(ar.ifr_name[:], tun.iface.Name())
|
||||
ar.ifru_addr.sin6_len = uint8(unsafe.Sizeof(ar.ifru_addr))
|
||||
ar.ifru_addr.sin6_family = unix.AF_INET6
|
||||
parts := strings.Split(strings.TrimRight(addr, "/8"), ":")
|
||||
for i := 0; i < 8; i++ {
|
||||
addr, _ := strconv.ParseUint(parts[i], 16, 16)
|
||||
b := make([]byte, 16)
|
||||
binary.LittleEndian.PutUint16(b, uint16(addr))
|
||||
ar.ifru_addr.sin6_addr[i] = uint16(binary.BigEndian.Uint16(b))
|
||||
}
|
||||
|
||||
// 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))
|
||||
// Set the interface address
|
||||
if _, _, errno := unix.Syscall(unix.SYS_IOCTL, uintptr(sfd), uintptr(SIOCSIFADDR_IN6), uintptr(unsafe.Pointer(&ar))); errno != 0 {
|
||||
err = errno
|
||||
tun.core.log.Printf("Error in SIOCSIFADDR_IN6: %v", errno)
|
||||
|
||||
// Fall back to ifconfig to set the address
|
||||
cmd := exec.Command("ifconfig", tun.iface.Name(), "inet6", addr)
|
||||
tun.core.log.Printf("Using ifconfig as fallback: %v", strings.Join(cmd.Args, " "))
|
||||
output, err := cmd.CombinedOutput()
|
||||
if err != nil {
|
||||
tun.core.log.Printf("SIOCSIFADDR_IN6 fallback failed: %v.", err)
|
||||
tun.core.log.Println(string(output))
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue