mirror of
https://github.com/yggdrasil-network/yggdrasil-go.git
synced 2025-04-28 22:25:07 +03:00
TAP support added
- Supports Windows using OpenVPN NDIS 6 TAP driver - Supports NDP Neighbor Solicitation and Advertisements in ndp.go - Supports TAP encapsulation and decapsulation in tun.go
This commit is contained in:
parent
2b7c6eafcd
commit
ff55070458
4 changed files with 232 additions and 6 deletions
|
@ -2,7 +2,7 @@ package yggdrasil
|
|||
|
||||
// This manages the tun driver to send/recv packets to/from applications
|
||||
|
||||
// import water "github.com/songgao/water"
|
||||
import ethernet "github.com/songgao/packets/ethernet"
|
||||
|
||||
const IPv6_HEADER_LENGTH = 40
|
||||
|
||||
|
@ -17,6 +17,7 @@ type tunInterface interface {
|
|||
|
||||
type tunDevice struct {
|
||||
core *Core
|
||||
ndp ndp
|
||||
send chan<- []byte
|
||||
recv <-chan []byte
|
||||
mtu int
|
||||
|
@ -25,13 +26,28 @@ type tunDevice struct {
|
|||
|
||||
func (tun *tunDevice) init(core *Core) {
|
||||
tun.core = core
|
||||
tun.ndp.init(tun)
|
||||
}
|
||||
|
||||
func (tun *tunDevice) write() error {
|
||||
for {
|
||||
data := <-tun.recv
|
||||
if _, err := tun.iface.Write(data); err != nil {
|
||||
return err
|
||||
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
|
||||
copy(frame[14:], data[:])
|
||||
if _, err := tun.iface.Write(frame); err != nil {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
if _, err := tun.iface.Write(data); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
util_putBytes(data)
|
||||
}
|
||||
|
@ -44,13 +60,20 @@ func (tun *tunDevice) read() error {
|
|||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if buf[0]&0xf0 != 0x60 ||
|
||||
n != 256*int(buf[4])+int(buf[5])+IPv6_HEADER_LENGTH {
|
||||
o := 0
|
||||
if tun.iface.IsTAP() {
|
||||
o = 14
|
||||
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 {
|
||||
// Either not an IPv6 packet or not the complete packet for some reason
|
||||
//panic("Should not happen in testing")
|
||||
continue
|
||||
}
|
||||
packet := append(util_getBytes(), buf[:n]...)
|
||||
packet := append(util_getBytes(), buf[o:n]...)
|
||||
tun.send <- packet
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue