Don't use channels for ICMPv6 packets

This commit is contained in:
Neil Alexander 2018-02-12 20:00:55 +00:00
parent fac4bf796e
commit 6571a8c300
2 changed files with 20 additions and 29 deletions

View File

@ -6,7 +6,6 @@ package yggdrasil
import "golang.org/x/net/icmp" import "golang.org/x/net/icmp"
import "encoding/binary" import "encoding/binary"
import "errors"
import "unsafe" // TODO investigate if this can be done without resorting to unsafe import "unsafe" // TODO investigate if this can be done without resorting to unsafe
type macAddress [6]byte type macAddress [6]byte
@ -21,7 +20,6 @@ type icmpv6 struct {
peerlladdr ipv6Address peerlladdr ipv6Address
mymac macAddress mymac macAddress
mylladdr ipv6Address mylladdr ipv6Address
recv chan []byte
} }
type etherHeader struct { type etherHeader struct {
@ -76,40 +74,28 @@ type icmpv6Frame struct {
func (i *icmpv6) init(t *tunDevice) { func (i *icmpv6) init(t *tunDevice) {
i.tun = t i.tun = t
i.recv = make(chan []byte)
copy(i.mymac[:], []byte{0x02, 0x00, 0x00, 0x00, 0x00, 0x02}) copy(i.mymac[:], []byte{0x02, 0x00, 0x00, 0x00, 0x00, 0x02})
copy(i.mylladdr[:], []byte{ copy(i.mylladdr[:], []byte{
0xFE, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFE, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0xFE}) 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0xFE})
go i.listen()
} }
func (i *icmpv6) listen() { func (i *icmpv6) parse_packet(datain []byte) {
for { var response []byte
datain := <-i.recv var err error
if i.tun.iface.IsTAP() { if i.tun.iface.IsTAP() {
// TAP mode response, err = i.parse_packet_tap(datain)
response, err := i.parse_packet_tap(datain)
if err != nil {
i.tun.core.log.Printf("Error from icmpv6.parse_packet_tap: %v", err)
continue
}
if response != nil {
i.tun.iface.Write(response)
}
} else { } else {
// TUN mode response, err = i.parse_packet_tun(datain)
response, err := i.parse_packet_tun(datain) }
if err != nil { if err != nil {
i.tun.core.log.Printf("Error from icmpv6.parse_packet_tun: %v", err) i.tun.core.log.Printf("ICMPv6 error: %v", err)
continue return
} }
if response != nil { if response != nil {
i.tun.iface.Write(response) i.tun.iface.Write(response)
} }
}
}
} }
func (i *icmpv6) parse_packet_tap(datain []byte) ([]byte, error) { func (i *icmpv6) parse_packet_tap(datain []byte) ([]byte, error) {

View File

@ -79,7 +79,12 @@ func (tun *tunDevice) read() error {
// Found an ICMPv6 packet // Found an ICMPv6 packet
b := make([]byte, n) b := make([]byte, n)
copy(b, buf) copy(b, buf)
tun.icmpv6.recv <- b // tun.icmpv6.recv <- b
if tun.iface.IsTAP() {
go tun.icmpv6.parse_packet_tap(b)
} else {
go tun.icmpv6.parse_packet_tun(b)
}
} }
packet := append(util_getBytes(), buf[o:n]...) packet := append(util_getBytes(), buf[o:n]...)
tun.send <- packet tun.send <- packet