2019-01-02 18:05:54 +00:00
|
|
|
// +build !mobile
|
|
|
|
|
2019-03-28 00:30:25 +00:00
|
|
|
package tuntap
|
2018-01-06 16:39:30 +00:00
|
|
|
|
|
|
|
// The darwin platform specific tun parts
|
|
|
|
|
2018-06-12 22:50:08 +00:00
|
|
|
import (
|
|
|
|
"encoding/binary"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"unsafe"
|
2018-01-06 16:39:30 +00:00
|
|
|
|
2018-06-12 22:50:08 +00:00
|
|
|
"golang.org/x/sys/unix"
|
|
|
|
|
2019-11-22 16:43:50 +00:00
|
|
|
wgtun "golang.zx2c4.com/wireguard/tun"
|
2018-06-12 22:50:08 +00:00
|
|
|
)
|
2018-01-06 16:39:30 +00:00
|
|
|
|
2018-06-12 21:45:53 +00:00
|
|
|
// Configures the "utun" adapter with the correct IPv6 address and MTU.
|
2021-05-16 18:51:09 +00:00
|
|
|
func (tun *TunAdapter) setup(ifname string, addr string, mtu uint16) error {
|
2019-11-22 20:07:08 +00:00
|
|
|
if ifname == "auto" {
|
|
|
|
ifname = "utun"
|
|
|
|
}
|
2020-01-05 17:27:54 +00:00
|
|
|
iface, err := wgtun.CreateTUN(ifname, int(mtu))
|
2018-01-06 16:39:30 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
tun.iface = iface
|
2021-05-16 18:51:09 +00:00
|
|
|
if m, err := iface.MTU(); err == nil {
|
|
|
|
tun.mtu = getSupportedMTU(uint16(m))
|
2019-11-22 16:43:50 +00:00
|
|
|
} else {
|
|
|
|
tun.mtu = 0
|
|
|
|
}
|
2018-01-06 16:39:30 +00:00
|
|
|
return tun.setupAddress(addr)
|
|
|
|
}
|
|
|
|
|
2019-07-27 12:30:47 +00:00
|
|
|
const (
|
|
|
|
darwin_SIOCAIFADDR_IN6 = 2155899162 // netinet6/in6_var.h
|
|
|
|
darwin_IN6_IFF_NODAD = 0x0020 // netinet6/in6_var.h
|
|
|
|
darwin_IN6_IFF_SECURED = 0x0400 // netinet6/in6_var.h
|
|
|
|
darwin_ND6_INFINITE_LIFETIME = 0xFFFFFFFF // netinet6/nd6.h
|
|
|
|
)
|
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-06-12 21:45:53 +00:00
|
|
|
// Sets the IPv6 address of the utun adapter. On Darwin/macOS this is done using
|
2018-06-12 22:50:08 +00:00
|
|
|
// a system socket and making direct syscalls to the kernel.
|
2019-03-28 00:30:25 +00:00
|
|
|
func (tun *TunAdapter) setupAddress(addr string) error {
|
2018-01-06 16:39:30 +00:00
|
|
|
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 {
|
2019-04-20 15:32:27 +00:00
|
|
|
tun.log.Printf("Create AF_SYSTEM socket failed: %v.", err)
|
2018-01-06 16:39:30 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var ar in6_aliasreq
|
2019-11-22 16:43:50 +00:00
|
|
|
copy(ar.ifra_name[:], tun.Name())
|
2018-01-06 16:39:30 +00:00
|
|
|
|
|
|
|
ar.ifra_prefixmask.sin6_len = uint8(unsafe.Sizeof(ar.ifra_prefixmask))
|
2018-01-21 00:17:15 +00:00
|
|
|
b := make([]byte, 16)
|
2018-06-14 13:00:57 +00:00
|
|
|
binary.LittleEndian.PutUint16(b, uint16(0xFE00))
|
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-05-26 20:47:54 +00:00
|
|
|
parts := strings.Split(strings.Split(addr, "/")[0], ":")
|
2018-01-06 16:39:30 +00:00
|
|
|
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))
|
|
|
|
}
|
|
|
|
|
2019-07-27 12:30:47 +00:00
|
|
|
ar.ifra_flags |= darwin_IN6_IFF_NODAD
|
|
|
|
ar.ifra_flags |= darwin_IN6_IFF_SECURED
|
|
|
|
|
|
|
|
ar.ifra_lifetime.ia6t_vltime = darwin_ND6_INFINITE_LIFETIME
|
|
|
|
ar.ifra_lifetime.ia6t_pltime = darwin_ND6_INFINITE_LIFETIME
|
2018-01-06 16:39:30 +00:00
|
|
|
|
2018-01-15 14:35:37 +00:00
|
|
|
var ir ifreq
|
2019-11-22 16:43:50 +00:00
|
|
|
copy(ir.ifr_name[:], tun.Name())
|
2018-01-15 14:39:07 +00:00
|
|
|
ir.ifru_mtu = uint32(tun.mtu)
|
2018-01-15 14:35:37 +00:00
|
|
|
|
2019-04-20 15:32:27 +00:00
|
|
|
tun.log.Infof("Interface name: %s", ar.ifra_name)
|
|
|
|
tun.log.Infof("Interface IPv6: %s", addr)
|
|
|
|
tun.log.Infof("Interface MTU: %d", ir.ifru_mtu)
|
2018-01-06 16:39:30 +00:00
|
|
|
|
2018-05-27 22:31:34 +00:00
|
|
|
if _, _, errno := unix.Syscall(unix.SYS_IOCTL, uintptr(fd), uintptr(darwin_SIOCAIFADDR_IN6), uintptr(unsafe.Pointer(&ar))); errno != 0 {
|
2018-01-06 16:39:30 +00:00
|
|
|
err = errno
|
2019-04-20 15:32:27 +00:00
|
|
|
tun.log.Errorf("Error in darwin_SIOCAIFADDR_IN6: %v", errno)
|
2018-01-06 16:39:30 +00:00
|
|
|
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
|
2019-04-20 15:32:27 +00:00
|
|
|
tun.log.Errorf("Error in SIOCSIFMTU: %v", errno)
|
2018-01-15 14:35:37 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-01-06 16:39:30 +00:00
|
|
|
return err
|
|
|
|
}
|