mirror of
https://github.com/cwinfo/yggdrasil-go.git
synced 2024-11-14 03:20:28 +00:00
Merge remote-tracking branch 'origin/macos-interface'
This commit is contained in:
commit
fbe0afa625
@ -2,16 +2,25 @@ 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 water "github.com/songgao/water"
|
// import water "github.com/songgao/water"
|
||||||
|
|
||||||
const IPv6_HEADER_LENGTH = 40
|
const IPv6_HEADER_LENGTH = 40
|
||||||
|
|
||||||
|
type tunInterface interface {
|
||||||
|
IsTUN() bool
|
||||||
|
IsTAP() bool
|
||||||
|
Name() string
|
||||||
|
Read(to []byte) (int, error)
|
||||||
|
Write(from []byte) (int, error)
|
||||||
|
Close() error
|
||||||
|
}
|
||||||
|
|
||||||
type tunDevice struct {
|
type tunDevice struct {
|
||||||
core *Core
|
core *Core
|
||||||
send chan<- []byte
|
send chan<- []byte
|
||||||
recv <-chan []byte
|
recv <-chan []byte
|
||||||
mtu int
|
mtu int
|
||||||
iface *water.Interface
|
iface tunInterface
|
||||||
}
|
}
|
||||||
|
|
||||||
func (tun *tunDevice) init(core *Core) {
|
func (tun *tunDevice) init(core *Core) {
|
||||||
|
@ -1,14 +1,12 @@
|
|||||||
package yggdrasil
|
package yggdrasil
|
||||||
|
|
||||||
// The darwin platform specific tun parts
|
// The darwin platform specific tun parts
|
||||||
// macOS/Darwin is BSD-derived and doesn't have iproute2. This code instead
|
|
||||||
// uses ifconfig. There is actually code that tries to do this properly using
|
|
||||||
// syscalls in github.com/neilalexander/yggdrasil-go branch "macos-interface"
|
|
||||||
// but for some reason it doesn't work as expected!
|
|
||||||
|
|
||||||
import "fmt"
|
import "unsafe"
|
||||||
import "os/exec"
|
|
||||||
import "strings"
|
import "strings"
|
||||||
|
import "strconv"
|
||||||
|
import "encoding/binary"
|
||||||
|
import "golang.org/x/sys/unix"
|
||||||
|
|
||||||
import water "github.com/songgao/water"
|
import water "github.com/songgao/water"
|
||||||
|
|
||||||
@ -19,30 +17,89 @@ func (tun *tunDevice) setup(ifname string, addr string, mtu int) error {
|
|||||||
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 = mtu
|
||||||
return tun.setupAddress(addr)
|
return tun.setupAddress(addr)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const SIOCAIFADDR_IN6 = 2155899162
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
|
type ifreq struct {
|
||||||
|
ifr_name [16]byte
|
||||||
|
ifru_mtu uint32
|
||||||
|
}
|
||||||
|
|
||||||
func (tun *tunDevice) setupAddress(addr string) error {
|
func (tun *tunDevice) setupAddress(addr string) error {
|
||||||
// Set address
|
var fd int
|
||||||
cmd := exec.Command("ifconfig", tun.iface.Name(), "inet6",
|
var err error
|
||||||
"add", addr)
|
|
||||||
tun.core.log.Printf("ifconfig command: %v", strings.Join(cmd.Args, " "))
|
if fd, err = unix.Socket(unix.AF_INET6, unix.SOCK_DGRAM, 0); err != nil {
|
||||||
output, err := cmd.CombinedOutput()
|
tun.core.log.Printf("Create AF_SYSTEM socket failed: %v.", err)
|
||||||
if err != nil {
|
|
||||||
tun.core.log.Printf("Darwin ifconfig failed: %v.", err)
|
|
||||||
tun.core.log.Println(string(output))
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
// Set MTU and bring device up
|
|
||||||
cmd = exec.Command("ifconfig", tun.iface.Name(), "mtu",
|
var ar in6_aliasreq
|
||||||
fmt.Sprintf("%d", tun.mtu))
|
copy(ar.ifra_name[:], tun.iface.Name())
|
||||||
tun.core.log.Printf("ifconfig command: %v", strings.Join(cmd.Args, " "))
|
|
||||||
output, err = cmd.CombinedOutput()
|
ar.ifra_prefixmask.sin6_len = uint8(unsafe.Sizeof(ar.ifra_prefixmask))
|
||||||
if err != nil {
|
b := make([]byte, 16); binary.LittleEndian.PutUint16(b, uint16(0xFF00))
|
||||||
tun.core.log.Printf("Darwin ifconfig failed: %v.", err)
|
ar.ifra_prefixmask.sin6_addr[0] = uint16(binary.BigEndian.Uint16(b))
|
||||||
tun.core.log.Println(string(output))
|
|
||||||
|
ar.ifra_addr.sin6_len = uint8(unsafe.Sizeof(ar.ifra_addr))
|
||||||
|
ar.ifra_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.ifra_addr.sin6_addr[i] = uint16(binary.BigEndian.Uint16(b))
|
||||||
|
}
|
||||||
|
|
||||||
|
ar.ifra_lifetime.ia6t_vltime = 0xFFFFFFFF
|
||||||
|
ar.ifra_lifetime.ia6t_pltime = 0xFFFFFFFF
|
||||||
|
|
||||||
|
var ir ifreq
|
||||||
|
copy(ir.ifr_name[:], tun.iface.Name())
|
||||||
|
ir.ifru_mtu = uint32(tun.mtu)
|
||||||
|
|
||||||
|
tun.core.log.Printf("Interface name: %s", ar.ifra_name)
|
||||||
|
tun.core.log.Printf("Interface IPv6: %s", addr)
|
||||||
|
tun.core.log.Printf("Interface MTU: %d", ir.ifru_mtu)
|
||||||
|
|
||||||
|
if _, _, errno := unix.Syscall(unix.SYS_IOCTL, uintptr(fd), uintptr(SIOCAIFADDR_IN6), uintptr(unsafe.Pointer(&ar))); errno != 0 {
|
||||||
|
err = errno
|
||||||
|
tun.core.log.Printf("Error in SIOCAIFADDR_IN6: %v", errno)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return nil
|
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user