mirror of
				https://github.com/yggdrasil-network/yggdrasil-go.git
				synced 2025-11-04 03:05:07 +03:00 
			
		
		
		
	Update water go.mod references, fix some bugs in TAP mode (which should hopefully fix Windows support too)
This commit is contained in:
		
							parent
							
								
									36201895e7
								
							
						
					
					
						commit
						48ad3c5d7f
					
				
					 5 changed files with 33 additions and 18 deletions
				
			
		| 
						 | 
				
			
			@ -78,8 +78,9 @@ func (i *ICMPv6) Init(t *TunAdapter) {
 | 
			
		|||
// Parses an incoming ICMPv6 packet. The packet provided may be either an
 | 
			
		||||
// ethernet frame containing an IP packet, or the IP packet alone. This is
 | 
			
		||||
// determined by whether the TUN/TAP adapter is running in TUN (layer 3) or
 | 
			
		||||
// TAP (layer 2) mode.
 | 
			
		||||
func (i *ICMPv6) ParsePacket(datain []byte) {
 | 
			
		||||
// TAP (layer 2) mode. Returns an error condition which is nil if the ICMPv6
 | 
			
		||||
// module handled the packet or contains the error if not.
 | 
			
		||||
func (i *ICMPv6) ParsePacket(datain []byte) error {
 | 
			
		||||
	var response []byte
 | 
			
		||||
	var err error
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -91,11 +92,12 @@ func (i *ICMPv6) ParsePacket(datain []byte) {
 | 
			
		|||
	}
 | 
			
		||||
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// Write the packet to TUN/TAP
 | 
			
		||||
	i.tun.iface.Write(response)
 | 
			
		||||
	return nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Unwraps the ethernet headers of an incoming ICMPv6 packet and hands off
 | 
			
		||||
| 
						 | 
				
			
			@ -105,7 +107,7 @@ func (i *ICMPv6) ParsePacket(datain []byte) {
 | 
			
		|||
func (i *ICMPv6) UnmarshalPacketL2(datain []byte) ([]byte, error) {
 | 
			
		||||
	// Ignore non-IPv6 frames
 | 
			
		||||
	if binary.BigEndian.Uint16(datain[12:14]) != uint16(0x86DD) {
 | 
			
		||||
		return nil, nil
 | 
			
		||||
		return nil, errors.New("Ignoring non-IPv6 frame")
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// Hand over to ParsePacket to interpret the IPv6 packet
 | 
			
		||||
| 
						 | 
				
			
			@ -141,12 +143,12 @@ func (i *ICMPv6) UnmarshalPacket(datain []byte, datamac *[]byte) ([]byte, error)
 | 
			
		|||
 | 
			
		||||
	// Check if the packet is IPv6
 | 
			
		||||
	if ipv6Header.Version != ipv6.Version {
 | 
			
		||||
		return nil, err
 | 
			
		||||
		return nil, errors.New("Ignoring non-IPv6 packet")
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// Check if the packet is ICMPv6
 | 
			
		||||
	if ipv6Header.NextHeader != 58 {
 | 
			
		||||
		return nil, err
 | 
			
		||||
		return nil, errors.New("Ignoring non-ICMPv6 packet")
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// Parse the ICMPv6 message contents
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -111,7 +111,7 @@ func (tun *TunAdapter) writer() error {
 | 
			
		|||
}
 | 
			
		||||
 | 
			
		||||
func (tun *TunAdapter) reader() error {
 | 
			
		||||
	recvd := make([]byte, 65535)
 | 
			
		||||
	recvd := make([]byte, 65535+tun_ETHER_HEADER_LENGTH)
 | 
			
		||||
	for {
 | 
			
		||||
		// Wait for a packet to be delivered to us through the TUN/TAP adapter
 | 
			
		||||
		n, err := tun.iface.Read(recvd)
 | 
			
		||||
| 
						 | 
				
			
			@ -134,17 +134,22 @@ func (tun *TunAdapter) reader() error {
 | 
			
		|||
			if len(recvd) <= offset {
 | 
			
		||||
				continue
 | 
			
		||||
			}
 | 
			
		||||
			// If we detect an ICMP packet then hand it to the ICMPv6 module
 | 
			
		||||
			if recvd[offset+6] == 58 {
 | 
			
		||||
				// Found an ICMPv6 packet
 | 
			
		||||
				b := make([]byte, n)
 | 
			
		||||
				copy(b, recvd)
 | 
			
		||||
				go tun.icmpv6.ParsePacket(b)
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
		// Offset the buffer from now on so that we can ignore ethernet frames if
 | 
			
		||||
		// they are present
 | 
			
		||||
		bs := recvd[offset:]
 | 
			
		||||
		bs := recvd[offset : offset+n]
 | 
			
		||||
		n -= offset
 | 
			
		||||
		// If we detect an ICMP packet then hand it to the ICMPv6 module
 | 
			
		||||
		if bs[6] == 58 {
 | 
			
		||||
			// Found an ICMPv6 packet - we need to make sure to give ICMPv6 the full
 | 
			
		||||
			// Ethernet frame rather than just the IPv6 packet as this is needed for
 | 
			
		||||
			// NDP to work correctly
 | 
			
		||||
			if err := tun.icmpv6.ParsePacket(recvd); err == nil {
 | 
			
		||||
				// We acted on the packet in the ICMPv6 module so don't forward or do
 | 
			
		||||
				// anything else with it
 | 
			
		||||
				continue
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
		// From the IP header, work out what our source and destination addresses
 | 
			
		||||
		// and node IDs are. We will need these in order to work out where to send
 | 
			
		||||
		// the packet
 | 
			
		||||
| 
						 | 
				
			
			@ -162,7 +167,7 @@ func (tun *TunAdapter) reader() error {
 | 
			
		|||
				continue
 | 
			
		||||
			}
 | 
			
		||||
			// Check the packet size
 | 
			
		||||
			if n != 256*int(bs[4])+int(bs[5])+offset+tun_IPv6_HEADER_LENGTH {
 | 
			
		||||
			if n-tun_IPv6_HEADER_LENGTH != 256*int(bs[4])+int(bs[5]) {
 | 
			
		||||
				continue
 | 
			
		||||
			}
 | 
			
		||||
			// IPv6 address
 | 
			
		||||
| 
						 | 
				
			
			@ -176,7 +181,7 @@ func (tun *TunAdapter) reader() error {
 | 
			
		|||
				continue
 | 
			
		||||
			}
 | 
			
		||||
			// Check the packet size
 | 
			
		||||
			if n != 256*int(bs[2])+int(bs[3])+offset {
 | 
			
		||||
			if n != 256*int(bs[2])+int(bs[3]) {
 | 
			
		||||
				continue
 | 
			
		||||
			}
 | 
			
		||||
			// IPv4 address
 | 
			
		||||
| 
						 | 
				
			
			@ -185,6 +190,7 @@ func (tun *TunAdapter) reader() error {
 | 
			
		|||
			copy(dstAddr[:addrlen], bs[16:])
 | 
			
		||||
		} else {
 | 
			
		||||
			// Unknown address length or protocol, so drop the packet and ignore it
 | 
			
		||||
			tun.log.Traceln("Unknown packet type, dropping")
 | 
			
		||||
			continue
 | 
			
		||||
		}
 | 
			
		||||
		if tun.ckr.isEnabled() && !tun.ckr.isValidSource(srcAddr, addrlen) {
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -71,6 +71,9 @@ func (tun *TunAdapter) setup(ifname string, iftapmode bool, addr string, mtu int
 | 
			
		|||
 | 
			
		||||
// Sets the MTU of the TAP adapter.
 | 
			
		||||
func (tun *TunAdapter) setupMTU(mtu int) error {
 | 
			
		||||
	if tun.iface == nil || tun.iface.Name() == "" {
 | 
			
		||||
		return errors.New("Can't configure MTU as TAP adapter is not present")
 | 
			
		||||
	}
 | 
			
		||||
	// Set MTU
 | 
			
		||||
	cmd := exec.Command("netsh", "interface", "ipv6", "set", "subinterface",
 | 
			
		||||
		fmt.Sprintf("interface=%s", tun.iface.Name()),
 | 
			
		||||
| 
						 | 
				
			
			@ -88,6 +91,9 @@ func (tun *TunAdapter) setupMTU(mtu int) error {
 | 
			
		|||
 | 
			
		||||
// Sets the IPv6 address of the TAP adapter.
 | 
			
		||||
func (tun *TunAdapter) setupAddress(addr string) error {
 | 
			
		||||
	if tun.iface == nil || tun.iface.Name() == "" {
 | 
			
		||||
		return errors.New("Can't configure IPv6 address as TAP adapter is not present")
 | 
			
		||||
	}
 | 
			
		||||
	// Set address
 | 
			
		||||
	cmd := exec.Command("netsh", "interface", "ipv6", "add", "address",
 | 
			
		||||
		fmt.Sprintf("interface=%s", tun.iface.Name()),
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue