2017-12-29 07:16:20 +03:00
|
|
|
package yggdrasil
|
|
|
|
|
|
|
|
// This manages the tun driver to send/recv packets to/from applications
|
|
|
|
|
2018-03-04 22:57:34 +03:00
|
|
|
import "github.com/songgao/packets/ethernet"
|
|
|
|
import "github.com/yggdrasil-network/water"
|
2017-12-29 07:16:20 +03:00
|
|
|
|
2018-05-28 01:31:34 +03:00
|
|
|
const tun_IPv6_HEADER_LENGTH = 40
|
|
|
|
const tun_ETHER_HEADER_LENGTH = 14
|
2017-12-29 07:16:20 +03:00
|
|
|
|
|
|
|
type tunDevice struct {
|
2018-02-12 21:19:31 +03:00
|
|
|
core *Core
|
|
|
|
icmpv6 icmpv6
|
|
|
|
send chan<- []byte
|
|
|
|
recv <-chan []byte
|
|
|
|
mtu int
|
2018-03-04 22:57:34 +03:00
|
|
|
iface *water.Interface
|
2017-12-29 07:16:20 +03:00
|
|
|
}
|
|
|
|
|
2018-03-03 14:47:14 +03:00
|
|
|
type tunDefaultParameters struct {
|
2018-03-03 20:49:24 +03:00
|
|
|
maximumIfMTU int
|
2018-03-03 15:30:54 +03:00
|
|
|
defaultIfMTU int
|
2018-03-03 20:49:24 +03:00
|
|
|
defaultIfName string
|
2018-03-03 15:30:54 +03:00
|
|
|
defaultIfTAPMode bool
|
2018-03-03 14:47:14 +03:00
|
|
|
}
|
|
|
|
|
2018-03-03 15:30:54 +03:00
|
|
|
func getSupportedMTU(mtu int) int {
|
|
|
|
if mtu > getDefaults().maximumIfMTU {
|
|
|
|
return getDefaults().maximumIfMTU
|
2018-03-03 14:47:14 +03:00
|
|
|
}
|
|
|
|
return mtu
|
|
|
|
}
|
|
|
|
|
2017-12-29 07:16:20 +03:00
|
|
|
func (tun *tunDevice) init(core *Core) {
|
2018-01-05 01:37:51 +03:00
|
|
|
tun.core = core
|
2018-02-12 21:19:31 +03:00
|
|
|
tun.icmpv6.init(tun)
|
2017-12-29 07:16:20 +03:00
|
|
|
}
|
|
|
|
|
2018-05-28 00:35:30 +03:00
|
|
|
func (tun *tunDevice) start(ifname string, iftapmode bool, addr string, mtu int) error {
|
2018-06-03 01:29:06 +03:00
|
|
|
if ifname == "none" {
|
|
|
|
return nil
|
|
|
|
}
|
2018-05-28 00:35:30 +03:00
|
|
|
if err := tun.setup(ifname, iftapmode, addr, mtu); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
go func() { panic(tun.read()) }()
|
|
|
|
go func() { panic(tun.write()) }()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-12-29 07:16:20 +03:00
|
|
|
func (tun *tunDevice) write() error {
|
2018-01-05 01:37:51 +03:00
|
|
|
for {
|
|
|
|
data := <-tun.recv
|
2018-02-16 01:29:13 +03:00
|
|
|
if tun.iface == nil {
|
|
|
|
continue
|
|
|
|
}
|
2018-01-24 13:59:01 +03:00
|
|
|
if tun.iface.IsTAP() {
|
|
|
|
var frame ethernet.Frame
|
|
|
|
frame.Prepare(
|
2018-02-12 21:19:31 +03:00
|
|
|
tun.icmpv6.peermac[:6], // Destination MAC address
|
|
|
|
tun.icmpv6.mymac[:6], // Source MAC address
|
|
|
|
ethernet.NotTagged, // VLAN tagging
|
|
|
|
ethernet.IPv6, // Ethertype
|
|
|
|
len(data)) // Payload length
|
2018-05-28 01:31:34 +03:00
|
|
|
copy(frame[tun_ETHER_HEADER_LENGTH:], data[:])
|
2018-01-24 13:59:01 +03:00
|
|
|
if _, err := tun.iface.Write(frame); err != nil {
|
2018-01-25 20:44:56 +03:00
|
|
|
panic(err)
|
2018-01-24 13:59:01 +03:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if _, err := tun.iface.Write(data); err != nil {
|
2018-01-25 20:44:56 +03:00
|
|
|
panic(err)
|
2018-01-24 13:59:01 +03:00
|
|
|
}
|
2018-01-05 01:37:51 +03:00
|
|
|
}
|
|
|
|
util_putBytes(data)
|
|
|
|
}
|
2017-12-29 07:16:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (tun *tunDevice) read() error {
|
2018-01-25 20:44:56 +03:00
|
|
|
mtu := tun.mtu
|
|
|
|
if tun.iface.IsTAP() {
|
2018-05-28 01:31:34 +03:00
|
|
|
mtu += tun_ETHER_HEADER_LENGTH
|
2018-01-25 20:44:56 +03:00
|
|
|
}
|
|
|
|
buf := make([]byte, mtu)
|
2018-01-05 01:37:51 +03:00
|
|
|
for {
|
|
|
|
n, err := tun.iface.Read(buf)
|
|
|
|
if err != nil {
|
2018-02-28 18:15:57 +03:00
|
|
|
// panic(err)
|
|
|
|
return err
|
2018-01-05 01:37:51 +03:00
|
|
|
}
|
2018-01-24 13:59:01 +03:00
|
|
|
o := 0
|
|
|
|
if tun.iface.IsTAP() {
|
2018-05-28 01:31:34 +03:00
|
|
|
o = tun_ETHER_HEADER_LENGTH
|
2018-01-24 13:59:01 +03:00
|
|
|
}
|
|
|
|
if buf[o]&0xf0 != 0x60 ||
|
2018-05-28 01:31:34 +03:00
|
|
|
n != 256*int(buf[o+4])+int(buf[o+5])+tun_IPv6_HEADER_LENGTH+o {
|
2018-01-05 01:37:51 +03:00
|
|
|
// Either not an IPv6 packet or not the complete packet for some reason
|
|
|
|
//panic("Should not happen in testing")
|
|
|
|
continue
|
|
|
|
}
|
2018-02-12 21:19:31 +03:00
|
|
|
if buf[o+6] == 58 {
|
|
|
|
// Found an ICMPv6 packet
|
|
|
|
b := make([]byte, n)
|
|
|
|
copy(b, buf)
|
2018-02-12 23:00:55 +03:00
|
|
|
// tun.icmpv6.recv <- b
|
2018-02-14 14:21:23 +03:00
|
|
|
go tun.icmpv6.parse_packet(b)
|
2018-02-12 21:19:31 +03:00
|
|
|
}
|
2018-01-24 13:59:01 +03:00
|
|
|
packet := append(util_getBytes(), buf[o:n]...)
|
2018-01-05 01:37:51 +03:00
|
|
|
tun.send <- packet
|
|
|
|
}
|
2017-12-29 07:16:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (tun *tunDevice) close() error {
|
2018-02-16 01:29:13 +03:00
|
|
|
if tun.iface == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2018-01-05 01:37:51 +03:00
|
|
|
return tun.iface.Close()
|
2017-12-29 07:16:20 +03:00
|
|
|
}
|