2018-01-06 16:39:30 +00:00
|
|
|
package yggdrasil
|
|
|
|
|
|
|
|
// The darwin platform specific tun parts
|
|
|
|
|
|
|
|
import "unsafe"
|
|
|
|
import "strings"
|
|
|
|
import "strconv"
|
|
|
|
import "encoding/binary"
|
2018-01-15 14:35:37 +00:00
|
|
|
import "golang.org/x/sys/unix"
|
2018-01-06 16:39:30 +00:00
|
|
|
|
|
|
|
import water "github.com/songgao/water"
|
|
|
|
|
2018-02-11 21:45:44 +00:00
|
|
|
func (tun *tunDevice) setup(ifname string, iftapmode bool, addr string, mtu int) error {
|
|
|
|
if iftapmode {
|
|
|
|
tun.core.log.Printf("TAP mode is not supported on this platform, defaulting to TUN")
|
|
|
|
}
|
2018-01-06 16:39:30 +00:00
|
|
|
config := water.Config{DeviceType: water.TUN}
|
|
|
|
iface, err := water.New(config)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
tun.iface = iface
|
|
|
|
tun.mtu = mtu
|
|
|
|
return tun.setupAddress(addr)
|
|
|
|
}
|
|
|
|
|
2018-01-15 14:14:18 +00:00
|
|
|
const SIOCAIFADDR_IN6 = 2155899162
|
2018-01-06 16:39:30 +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
|
2018-01-21 00:17:15 +00:00
|
|
|
ifra_flags uint32
|
2018-01-06 16:39:30 +00:00
|
|
|
ifra_lifetime in6_addrlifetime
|
|
|
|
}
|
|
|
|
|
2018-01-15 14:35:37 +00:00
|
|
|
type ifreq struct {
|
2018-01-21 00:17:15 +00:00
|
|
|
ifr_name [16]byte
|
|
|
|
ifru_mtu uint32
|
2018-01-15 14:35:37 +00:00
|
|
|
}
|
|
|
|
|
2018-01-06 16:39:30 +00:00
|
|
|
func (tun *tunDevice) setupAddress(addr string) error {
|
|
|
|
var fd int
|
|
|
|
var err error
|
|
|
|
|
2018-01-15 14:35:37 +00:00
|
|
|
if fd, err = unix.Socket(unix.AF_INET6, unix.SOCK_DGRAM, 0); err != nil {
|
2018-01-06 16:39:30 +00:00
|
|
|
tun.core.log.Printf("Create AF_SYSTEM socket failed: %v.", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var ar in6_aliasreq
|
|
|
|
copy(ar.ifra_name[:], tun.iface.Name())
|
|
|
|
|
|
|
|
ar.ifra_prefixmask.sin6_len = uint8(unsafe.Sizeof(ar.ifra_prefixmask))
|
2018-01-21 00:17:15 +00:00
|
|
|
b := make([]byte, 16)
|
|
|
|
binary.LittleEndian.PutUint16(b, uint16(0xFF00))
|
2018-01-06 16:39:30 +00:00
|
|
|
ar.ifra_prefixmask.sin6_addr[0] = uint16(binary.BigEndian.Uint16(b))
|
|
|
|
|
|
|
|
ar.ifra_addr.sin6_len = uint8(unsafe.Sizeof(ar.ifra_addr))
|
2018-01-15 14:35:37 +00:00
|
|
|
ar.ifra_addr.sin6_family = unix.AF_INET6
|
2018-01-06 16:39:30 +00:00
|
|
|
parts := strings.Split(strings.TrimRight(addr, "/8"), ":")
|
|
|
|
for i := 0; i < 8; i++ {
|
|
|
|
addr, _ := strconv.ParseUint(parts[i], 16, 16)
|
2018-01-21 00:17:15 +00:00
|
|
|
b := make([]byte, 16)
|
|
|
|
binary.LittleEndian.PutUint16(b, uint16(addr))
|
2018-01-06 16:39:30 +00:00
|
|
|
ar.ifra_addr.sin6_addr[i] = uint16(binary.BigEndian.Uint16(b))
|
|
|
|
}
|
|
|
|
|
|
|
|
ar.ifra_lifetime.ia6t_vltime = 0xFFFFFFFF
|
|
|
|
ar.ifra_lifetime.ia6t_pltime = 0xFFFFFFFF
|
|
|
|
|
2018-01-15 14:35:37 +00:00
|
|
|
var ir ifreq
|
|
|
|
copy(ir.ifr_name[:], tun.iface.Name())
|
2018-01-15 14:39:07 +00:00
|
|
|
ir.ifru_mtu = uint32(tun.mtu)
|
2018-01-15 14:35:37 +00:00
|
|
|
|
2018-01-06 16:39:30 +00:00
|
|
|
tun.core.log.Printf("Interface name: %s", ar.ifra_name)
|
|
|
|
tun.core.log.Printf("Interface IPv6: %s", addr)
|
2018-01-15 14:35:37 +00:00
|
|
|
tun.core.log.Printf("Interface MTU: %d", ir.ifru_mtu)
|
2018-01-06 16:39:30 +00:00
|
|
|
|
2018-01-15 14:35:37 +00:00
|
|
|
if _, _, errno := unix.Syscall(unix.SYS_IOCTL, uintptr(fd), uintptr(SIOCAIFADDR_IN6), uintptr(unsafe.Pointer(&ar))); errno != 0 {
|
2018-01-06 16:39:30 +00:00
|
|
|
err = errno
|
|
|
|
tun.core.log.Printf("Error in SIOCAIFADDR_IN6: %v", errno)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-01-15 14:35:37 +00:00
|
|
|
if _, _, errno := unix.Syscall(unix.SYS_IOCTL, uintptr(fd), uintptr(unix.SIOCSIFMTU), uintptr(unsafe.Pointer(&ir))); errno != 0 {
|
|
|
|
err = errno
|
|
|
|
tun.core.log.Printf("Error in SIOCSIFMTU: %v", errno)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-01-06 16:39:30 +00:00
|
|
|
return err
|
|
|
|
}
|