2017-12-28 22:16:20 -06:00
|
|
|
package yggdrasil
|
|
|
|
|
|
|
|
// This manages the tun driver to send/recv packets to/from applications
|
|
|
|
|
2018-06-12 17:50:08 -05:00
|
|
|
import (
|
2018-11-10 18:33:52 +00:00
|
|
|
"bytes"
|
2018-11-10 15:46:10 +00:00
|
|
|
"errors"
|
2018-11-10 17:32:03 +00:00
|
|
|
"time"
|
2018-07-07 12:08:52 +01:00
|
|
|
"yggdrasil/defaults"
|
|
|
|
|
2018-06-12 17:50:08 -05:00
|
|
|
"github.com/songgao/packets/ethernet"
|
|
|
|
"github.com/yggdrasil-network/water"
|
|
|
|
)
|
2017-12-28 22:16:20 -06:00
|
|
|
|
2018-05-27 23:31:34 +01:00
|
|
|
const tun_IPv6_HEADER_LENGTH = 40
|
|
|
|
const tun_ETHER_HEADER_LENGTH = 14
|
2017-12-28 22:16:20 -06:00
|
|
|
|
2018-06-12 22:45:53 +01:00
|
|
|
// Represents a running TUN/TAP interface.
|
2017-12-28 22:16:20 -06:00
|
|
|
type tunDevice struct {
|
2018-02-12 18:19:31 +00:00
|
|
|
core *Core
|
|
|
|
icmpv6 icmpv6
|
|
|
|
send chan<- []byte
|
|
|
|
recv <-chan []byte
|
|
|
|
mtu int
|
2018-03-04 13:57:34 -06:00
|
|
|
iface *water.Interface
|
2017-12-28 22:16:20 -06:00
|
|
|
}
|
|
|
|
|
2018-06-12 22:45:53 +01:00
|
|
|
// Gets the maximum supported MTU for the platform based on the defaults in
|
2018-07-07 12:08:52 +01:00
|
|
|
// defaults.GetDefaults().
|
2018-03-03 12:30:54 +00:00
|
|
|
func getSupportedMTU(mtu int) int {
|
2018-07-07 12:08:52 +01:00
|
|
|
if mtu > defaults.GetDefaults().MaximumIfMTU {
|
|
|
|
return defaults.GetDefaults().MaximumIfMTU
|
2018-03-03 11:47:14 +00:00
|
|
|
}
|
|
|
|
return mtu
|
|
|
|
}
|
|
|
|
|
2018-06-12 22:45:53 +01:00
|
|
|
// Initialises the TUN/TAP adapter.
|
2017-12-28 22:16:20 -06:00
|
|
|
func (tun *tunDevice) init(core *Core) {
|
2018-01-04 22:37:51 +00:00
|
|
|
tun.core = core
|
2018-02-12 18:19:31 +00:00
|
|
|
tun.icmpv6.init(tun)
|
2017-12-28 22:16:20 -06:00
|
|
|
}
|
|
|
|
|
2018-06-12 22:45:53 +01:00
|
|
|
// Starts the setup process for the TUN/TAP adapter, and if successful, starts
|
|
|
|
// the read/write goroutines to handle packets on that interface.
|
2018-05-27 22:35:30 +01:00
|
|
|
func (tun *tunDevice) start(ifname string, iftapmode bool, addr string, mtu int) error {
|
2018-06-02 23:29:06 +01:00
|
|
|
if ifname == "none" {
|
|
|
|
return nil
|
|
|
|
}
|
2018-05-27 22:35:30 +01:00
|
|
|
if err := tun.setup(ifname, iftapmode, addr, mtu); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
go func() { panic(tun.read()) }()
|
|
|
|
go func() { panic(tun.write()) }()
|
2018-11-10 22:39:15 -06:00
|
|
|
if iftapmode {
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
if _, ok := tun.icmpv6.peermacs[tun.core.router.addr]; ok {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
request, err := tun.icmpv6.create_ndp_tap(tun.core.router.addr)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
if _, err := tun.iface.Write(request); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
time.Sleep(time.Second)
|
2018-11-10 17:32:03 +00:00
|
|
|
}
|
2018-11-10 22:39:15 -06:00
|
|
|
}()
|
|
|
|
}
|
2018-05-27 22:35:30 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-06-12 22:45:53 +01:00
|
|
|
// Writes a packet to the TUN/TAP adapter. If the adapter is running in TAP
|
|
|
|
// mode then additional ethernet encapsulation is added for the benefit of the
|
|
|
|
// host operating system.
|
2017-12-28 22:16:20 -06:00
|
|
|
func (tun *tunDevice) write() error {
|
2018-01-04 22:37:51 +00:00
|
|
|
for {
|
|
|
|
data := <-tun.recv
|
2018-02-15 22:29:13 +00:00
|
|
|
if tun.iface == nil {
|
|
|
|
continue
|
|
|
|
}
|
2018-01-24 10:59:01 +00:00
|
|
|
if tun.iface.IsTAP() {
|
2018-11-10 15:46:10 +00:00
|
|
|
var destAddr address
|
|
|
|
if data[0]&0xf0 == 0x60 {
|
|
|
|
if len(data) < 40 {
|
|
|
|
panic("Tried to send a packet shorter than an IPv6 header...")
|
|
|
|
}
|
|
|
|
copy(destAddr[:16], data[24:])
|
|
|
|
} else if data[0]&0xf0 == 0x40 {
|
|
|
|
if len(data) < 20 {
|
|
|
|
panic("Tried to send a packet shorter than an IPv4 header...")
|
|
|
|
}
|
|
|
|
copy(destAddr[:4], data[16:])
|
|
|
|
} else {
|
|
|
|
return errors.New("Invalid address family")
|
|
|
|
}
|
2018-11-10 17:32:03 +00:00
|
|
|
sendndp := func(destAddr address) {
|
|
|
|
neigh, known := tun.icmpv6.peermacs[destAddr]
|
|
|
|
known = known && (time.Since(neigh.lastsolicitation).Seconds() < 30)
|
|
|
|
if !known {
|
|
|
|
request, err := tun.icmpv6.create_ndp_tap(destAddr)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
if _, err := tun.iface.Write(request); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
tun.icmpv6.peermacs[destAddr] = neighbor{
|
|
|
|
lastsolicitation: time.Now(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
var peermac macAddress
|
|
|
|
var peerknown bool
|
2018-11-10 18:33:52 +00:00
|
|
|
if data[0]&0xf0 == 0x40 {
|
|
|
|
destAddr = tun.core.router.addr
|
|
|
|
} else if data[0]&0xf0 == 0x60 {
|
|
|
|
if !bytes.Equal(tun.core.router.addr[:16], destAddr[:16]) && !bytes.Equal(tun.core.router.subnet[:8], destAddr[:8]) {
|
|
|
|
destAddr = tun.core.router.addr
|
|
|
|
}
|
|
|
|
}
|
2018-11-10 17:32:03 +00:00
|
|
|
if neighbor, ok := tun.icmpv6.peermacs[destAddr]; ok && neighbor.learned {
|
|
|
|
peermac = neighbor.mac
|
|
|
|
peerknown = true
|
|
|
|
} else if neighbor, ok := tun.icmpv6.peermacs[tun.core.router.addr]; ok && neighbor.learned {
|
|
|
|
peermac = neighbor.mac
|
|
|
|
peerknown = true
|
|
|
|
sendndp(destAddr)
|
|
|
|
} else {
|
|
|
|
sendndp(tun.core.router.addr)
|
|
|
|
}
|
|
|
|
if peerknown {
|
2018-11-10 18:33:52 +00:00
|
|
|
var proto ethernet.Ethertype
|
|
|
|
switch {
|
|
|
|
case data[0]&0xf0 == 0x60:
|
|
|
|
proto = ethernet.IPv6
|
|
|
|
case data[0]&0xf0 == 0x40:
|
|
|
|
proto = ethernet.IPv4
|
|
|
|
}
|
2018-11-10 15:46:10 +00:00
|
|
|
var frame ethernet.Frame
|
|
|
|
frame.Prepare(
|
|
|
|
peermac[:6], // Destination MAC address
|
|
|
|
tun.icmpv6.mymac[:6], // Source MAC address
|
|
|
|
ethernet.NotTagged, // VLAN tagging
|
2018-11-10 18:33:52 +00:00
|
|
|
proto, // Ethertype
|
2018-11-10 15:46:10 +00:00
|
|
|
len(data)) // Payload length
|
|
|
|
copy(frame[tun_ETHER_HEADER_LENGTH:], data[:])
|
|
|
|
if _, err := tun.iface.Write(frame); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2018-01-24 10:59:01 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if _, err := tun.iface.Write(data); err != nil {
|
2018-01-25 17:44:56 +00:00
|
|
|
panic(err)
|
2018-01-24 10:59:01 +00:00
|
|
|
}
|
2018-01-04 22:37:51 +00:00
|
|
|
}
|
|
|
|
util_putBytes(data)
|
|
|
|
}
|
2017-12-28 22:16:20 -06:00
|
|
|
}
|
|
|
|
|
2018-06-12 22:45:53 +01:00
|
|
|
// Reads any packets that are waiting on the TUN/TAP adapter. If the adapter
|
|
|
|
// is running in TAP mode then the ethernet headers will automatically be
|
|
|
|
// processed and stripped if necessary. If an ICMPv6 packet is found, then
|
|
|
|
// the relevant helper functions in icmpv6.go are called.
|
2017-12-28 22:16:20 -06:00
|
|
|
func (tun *tunDevice) read() error {
|
2018-01-25 17:44:56 +00:00
|
|
|
mtu := tun.mtu
|
|
|
|
if tun.iface.IsTAP() {
|
2018-05-27 23:31:34 +01:00
|
|
|
mtu += tun_ETHER_HEADER_LENGTH
|
2018-01-25 17:44:56 +00:00
|
|
|
}
|
|
|
|
buf := make([]byte, mtu)
|
2018-01-04 22:37:51 +00:00
|
|
|
for {
|
|
|
|
n, err := tun.iface.Read(buf)
|
|
|
|
if err != nil {
|
2018-02-28 15:15:57 +00:00
|
|
|
// panic(err)
|
|
|
|
return err
|
2018-01-04 22:37:51 +00:00
|
|
|
}
|
2018-01-24 10:59:01 +00:00
|
|
|
o := 0
|
|
|
|
if tun.iface.IsTAP() {
|
2018-05-27 23:31:34 +01:00
|
|
|
o = tun_ETHER_HEADER_LENGTH
|
2018-01-24 10:59:01 +00:00
|
|
|
}
|
2018-11-06 22:35:28 +00:00
|
|
|
switch {
|
|
|
|
case buf[o]&0xf0 == 0x60 && n == 256*int(buf[o+4])+int(buf[o+5])+tun_IPv6_HEADER_LENGTH+o:
|
|
|
|
case buf[o]&0xf0 == 0x40 && n == 256*int(buf[o+2])+int(buf[o+3])+o:
|
|
|
|
default:
|
|
|
|
continue
|
2018-01-04 22:37:51 +00:00
|
|
|
}
|
2018-02-12 18:19:31 +00:00
|
|
|
if buf[o+6] == 58 {
|
|
|
|
// Found an ICMPv6 packet
|
|
|
|
b := make([]byte, n)
|
|
|
|
copy(b, buf)
|
2018-02-12 20:00:55 +00:00
|
|
|
// tun.icmpv6.recv <- b
|
2018-02-14 11:21:23 +00:00
|
|
|
go tun.icmpv6.parse_packet(b)
|
2018-02-12 18:19:31 +00:00
|
|
|
}
|
2018-01-24 10:59:01 +00:00
|
|
|
packet := append(util_getBytes(), buf[o:n]...)
|
2018-01-04 22:37:51 +00:00
|
|
|
tun.send <- packet
|
|
|
|
}
|
2017-12-28 22:16:20 -06:00
|
|
|
}
|
|
|
|
|
2018-06-12 22:45:53 +01:00
|
|
|
// Closes the TUN/TAP adapter. This is only usually called when the Yggdrasil
|
|
|
|
// process stops. Typically this operation will happen quickly, but on macOS
|
2018-06-12 17:50:08 -05:00
|
|
|
// it can block until a read operation is completed.
|
2017-12-28 22:16:20 -06:00
|
|
|
func (tun *tunDevice) close() error {
|
2018-02-15 22:29:13 +00:00
|
|
|
if tun.iface == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2018-01-04 22:37:51 +00:00
|
|
|
return tun.iface.Close()
|
2017-12-28 22:16:20 -06:00
|
|
|
}
|