2017-12-29 04:16:20 +00:00
|
|
|
package yggdrasil
|
|
|
|
|
|
|
|
// This manages the tun driver to send/recv packets to/from applications
|
|
|
|
|
2018-01-24 10:59:01 +00:00
|
|
|
import ethernet "github.com/songgao/packets/ethernet"
|
2017-12-29 04:16:20 +00:00
|
|
|
|
|
|
|
const IPv6_HEADER_LENGTH = 40
|
2018-01-25 17:44:56 +00:00
|
|
|
const ETHER_HEADER_LENGTH = 14
|
2017-12-29 04:16:20 +00:00
|
|
|
|
2018-01-05 23:35:59 +00:00
|
|
|
type tunInterface interface {
|
|
|
|
IsTUN() bool
|
|
|
|
IsTAP() bool
|
|
|
|
Name() string
|
|
|
|
Read(to []byte) (int, error)
|
|
|
|
Write(from []byte) (int, error)
|
|
|
|
Close() error
|
|
|
|
}
|
|
|
|
|
2017-12-29 04:16:20 +00:00
|
|
|
type tunDevice struct {
|
2018-01-04 22:37:51 +00:00
|
|
|
core *Core
|
2018-01-24 10:59:01 +00:00
|
|
|
ndp ndp
|
2018-01-04 22:37:51 +00:00
|
|
|
send chan<- []byte
|
|
|
|
recv <-chan []byte
|
|
|
|
mtu int
|
2018-01-05 23:35:59 +00:00
|
|
|
iface tunInterface
|
2017-12-29 04:16:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (tun *tunDevice) init(core *Core) {
|
2018-01-04 22:37:51 +00:00
|
|
|
tun.core = core
|
2018-01-24 10:59:01 +00:00
|
|
|
tun.ndp.init(tun)
|
2017-12-29 04:16:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (tun *tunDevice) write() error {
|
2018-01-04 22:37:51 +00:00
|
|
|
for {
|
|
|
|
data := <-tun.recv
|
2018-01-24 10:59:01 +00:00
|
|
|
if tun.iface.IsTAP() {
|
|
|
|
var frame ethernet.Frame
|
|
|
|
frame.Prepare(
|
|
|
|
tun.ndp.peermac[:6], // Destination MAC address
|
|
|
|
tun.ndp.mymac[:6], // Source MAC address
|
|
|
|
ethernet.NotTagged, // VLAN tagging
|
|
|
|
ethernet.IPv6, // Ethertype
|
|
|
|
len(data)) // Payload length
|
2018-01-25 17:44:56 +00:00
|
|
|
copy(frame[ETHER_HEADER_LENGTH:], data[:])
|
2018-01-24 10:59:01 +00:00
|
|
|
if _, err := tun.iface.Write(frame); err != nil {
|
2018-01-25 17:44:56 +00:00
|
|
|
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-29 04:16:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (tun *tunDevice) read() error {
|
2018-01-25 17:44:56 +00:00
|
|
|
mtu := tun.mtu
|
|
|
|
if tun.iface.IsTAP() {
|
|
|
|
mtu += ETHER_HEADER_LENGTH
|
|
|
|
}
|
|
|
|
buf := make([]byte, mtu)
|
2018-01-04 22:37:51 +00:00
|
|
|
for {
|
|
|
|
n, err := tun.iface.Read(buf)
|
|
|
|
if err != nil {
|
2018-01-25 17:44:56 +00:00
|
|
|
panic(err)
|
2018-01-04 22:37:51 +00:00
|
|
|
}
|
2018-01-24 10:59:01 +00:00
|
|
|
o := 0
|
|
|
|
if tun.iface.IsTAP() {
|
2018-01-25 17:44:56 +00:00
|
|
|
o = ETHER_HEADER_LENGTH
|
2018-01-24 10:59:01 +00:00
|
|
|
b := make([]byte, n)
|
|
|
|
copy(b, buf)
|
|
|
|
tun.ndp.recv <- b
|
|
|
|
}
|
|
|
|
if buf[o]&0xf0 != 0x60 ||
|
|
|
|
n != 256*int(buf[o+4])+int(buf[o+5])+IPv6_HEADER_LENGTH+o {
|
2018-01-04 22:37:51 +00:00
|
|
|
// Either not an IPv6 packet or not the complete packet for some reason
|
|
|
|
//panic("Should not happen in testing")
|
|
|
|
continue
|
|
|
|
}
|
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-29 04:16:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (tun *tunDevice) close() error {
|
2018-01-04 22:37:51 +00:00
|
|
|
return tun.iface.Close()
|
2017-12-29 04:16:20 +00:00
|
|
|
}
|