5
0
mirror of https://github.com/cwinfo/yggdrasil-go.git synced 2024-12-24 02:15:40 +00:00
yggdrasil-go/src/yggdrasil/icmpv6.go

228 lines
5.6 KiB
Go
Raw Normal View History

2018-02-12 18:19:31 +00:00
package yggdrasil
// The NDP functions are needed when you are running with a
// TAP adapter - as the operating system expects neighbor solicitations
// for on-link traffic, this goroutine provides them
import "net"
import "golang.org/x/net/ipv6"
2018-02-12 18:19:31 +00:00
import "golang.org/x/net/icmp"
import "encoding/binary"
import "errors"
2018-02-12 18:19:31 +00:00
type macAddress [6]byte
2018-05-27 22:31:34 +00:00
const len_ETHER = 14
2018-02-12 18:19:31 +00:00
type icmpv6 struct {
tun *tunDevice
peermac macAddress
peerlladdr net.IP
mylladdr net.IP
2018-02-12 18:19:31 +00:00
mymac macAddress
}
// Marshal returns the binary encoding of h.
func ipv6Header_Marshal(h *ipv6.Header) ([]byte, error) {
b := make([]byte, 40)
b[0] |= byte(h.Version) << 4
b[0] |= byte(h.TrafficClass) >> 4
b[1] |= byte(h.TrafficClass) << 4
b[1] |= byte(h.FlowLabel >> 16)
b[2] = byte(h.FlowLabel >> 8)
b[3] = byte(h.FlowLabel)
binary.BigEndian.PutUint16(b[4:6], uint16(h.PayloadLen))
b[6] = byte(h.NextHeader)
b[7] = byte(h.HopLimit)
copy(b[8:24], h.Src)
copy(b[24:40], h.Dst)
return b, nil
2018-02-12 18:19:31 +00:00
}
func (i *icmpv6) init(t *tunDevice) {
i.tun = t
// Our MAC address and link-local address
copy(i.mymac[:], []byte{
0x02, 0x00, 0x00, 0x00, 0x00, 0x02})
i.mylladdr = net.IP{
2018-02-12 18:19:31 +00:00
0xFE, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0xFE}
2018-02-12 18:19:31 +00:00
}
2018-02-12 20:00:55 +00:00
func (i *icmpv6) parse_packet(datain []byte) {
var response []byte
var err error
// Parse the frame/packet
2018-02-12 20:00:55 +00:00
if i.tun.iface.IsTAP() {
response, err = i.parse_packet_tap(datain)
} else {
response, err = i.parse_packet_tun(datain)
}
2018-02-12 20:00:55 +00:00
if err != nil {
return
}
// Write the packet to TUN/TAP
i.tun.iface.Write(response)
2018-02-12 18:19:31 +00:00
}
func (i *icmpv6) parse_packet_tap(datain []byte) ([]byte, error) {
2018-02-12 18:19:31 +00:00
// Store the peer MAC address
copy(i.peermac[:6], datain[6:12])
2018-02-12 18:19:31 +00:00
// Ignore non-IPv6 frames
if binary.BigEndian.Uint16(datain[12:14]) != uint16(0x86DD) {
return nil, nil
2018-02-12 18:19:31 +00:00
}
// Hand over to parse_packet_tun to interpret the IPv6 packet
2018-05-27 22:31:34 +00:00
ipv6packet, err := i.parse_packet_tun(datain[len_ETHER:])
if err != nil {
return nil, err
}
// Create the response buffer
2018-05-27 22:31:34 +00:00
dataout := make([]byte, len_ETHER+ipv6.HeaderLen+32)
2018-02-12 18:19:31 +00:00
// Populate the response ethernet headers
copy(dataout[:6], datain[6:12])
copy(dataout[6:12], i.mymac[:])
binary.BigEndian.PutUint16(dataout[12:14], uint16(0x86DD))
// Copy the returned packet to our response ethernet frame
2018-05-27 22:31:34 +00:00
copy(dataout[len_ETHER:], ipv6packet)
return dataout, nil
2018-02-12 18:19:31 +00:00
}
func (i *icmpv6) parse_packet_tun(datain []byte) ([]byte, error) {
// Parse the IPv6 packet headers
ipv6Header, err := ipv6.ParseHeader(datain[:ipv6.HeaderLen])
if err != nil {
return nil, err
2018-02-12 18:19:31 +00:00
}
// Check if the packet is IPv6
if ipv6Header.Version != ipv6.Version {
return nil, err
2018-02-12 18:19:31 +00:00
}
// Check if the packet is ICMPv6
if ipv6Header.NextHeader != 58 {
return nil, err
}
2018-02-12 18:19:31 +00:00
// Store the peer link local address, it will come in useful later
copy(i.peerlladdr[:], ipv6Header.Src[:])
2018-02-12 18:19:31 +00:00
// Parse the ICMPv6 message contents
icmpv6Header, err := icmp.ParseMessage(58, datain[ipv6.HeaderLen:])
if err != nil {
return nil, err
}
// Check for a supported message type
switch icmpv6Header.Type {
case ipv6.ICMPTypeNeighborSolicitation:
{
response, err := i.handle_ndp(datain[ipv6.HeaderLen:])
if err == nil {
// Create our ICMPv6 response
responsePacket, err := i.create_icmpv6_tun(
ipv6Header.Src, i.mylladdr,
ipv6.ICMPTypeNeighborAdvertisement, 0,
&icmp.DefaultMessageBody{Data: response})
if err != nil {
return nil, err
}
// Send it back
return responsePacket, nil
} else {
return nil, err
}
}
}
return nil, errors.New("ICMPv6 type not matched")
2018-02-12 18:19:31 +00:00
}
func (i *icmpv6) create_icmpv6_tap(dstmac macAddress, dst net.IP, src net.IP, mtype ipv6.ICMPType, mcode int, mbody icmp.MessageBody) ([]byte, error) {
// Pass through to create_icmpv6_tun
ipv6packet, err := i.create_icmpv6_tun(dst, src, mtype, mcode, mbody)
if err != nil {
return nil, err
}
2018-02-14 11:46:10 +00:00
// Create the response buffer
2018-05-27 22:31:34 +00:00
dataout := make([]byte, len_ETHER+len(ipv6packet))
2018-02-14 11:46:10 +00:00
// Populate the response ethernet headers
copy(dataout[:6], dstmac[:6])
copy(dataout[6:12], i.mymac[:])
binary.BigEndian.PutUint16(dataout[12:14], uint16(0x86DD))
2018-02-14 11:46:10 +00:00
// Copy the returned packet to our response ethernet frame
2018-05-27 22:31:34 +00:00
copy(dataout[len_ETHER:], ipv6packet)
2018-02-14 11:46:10 +00:00
return dataout, nil
}
func (i *icmpv6) create_icmpv6_tun(dst net.IP, src net.IP, mtype ipv6.ICMPType, mcode int, mbody icmp.MessageBody) ([]byte, error) {
// Create the ICMPv6 message
icmpMessage := icmp.Message{
Type: mtype,
Code: mcode,
Body: mbody,
}
2018-02-12 18:19:31 +00:00
// Convert the ICMPv6 message into []byte
icmpMessageBuf, err := icmpMessage.Marshal(icmp.IPv6PseudoHeader(src, dst))
2018-02-12 18:19:31 +00:00
if err != nil {
return nil, err
2018-02-12 18:19:31 +00:00
}
// Create the IPv6 header
ipv6Header := ipv6.Header{
Version: ipv6.Version,
NextHeader: 58,
PayloadLen: len(icmpMessageBuf),
HopLimit: 255,
Src: src,
Dst: dst,
}
// Convert the IPv6 header into []byte
ipv6HeaderBuf, err := ipv6Header_Marshal(&ipv6Header)
2018-02-12 18:19:31 +00:00
if err != nil {
return nil, err
2018-02-12 18:19:31 +00:00
}
// Construct the packet
responsePacket := make([]byte, ipv6.HeaderLen+ipv6Header.PayloadLen)
copy(responsePacket[:ipv6.HeaderLen], ipv6HeaderBuf)
copy(responsePacket[ipv6.HeaderLen:], icmpMessageBuf)
// Send it back
return responsePacket, nil
2018-02-12 18:19:31 +00:00
}
func (i *icmpv6) handle_ndp(in []byte) ([]byte, error) {
2018-02-12 18:19:31 +00:00
// Ignore NDP requests for anything outside of fd00::/8
if in[8] != 0xFD {
return nil, errors.New("Not an NDP for fd00::/8")
2018-02-12 18:19:31 +00:00
}
// Create our NDP message body response
body := make([]byte, 28)
binary.BigEndian.PutUint32(body[:4], uint32(0x20000000))
copy(body[4:20], in[8:24]) // Target address
body[20] = uint8(2)
body[21] = uint8(1)
copy(body[22:28], i.mymac[:6])
2018-02-12 18:19:31 +00:00
// Send it back
return body, nil
2018-02-12 18:19:31 +00:00
}