mirror of
				https://github.com/yggdrasil-network/yggdrasil-go.git
				synced 2025-11-04 03:05:07 +03:00 
			
		
		
		
	
						commit
						6b6a5a2906
					
				
					 6 changed files with 32 additions and 16 deletions
				
			
		| 
						 | 
					@ -63,9 +63,12 @@ type TunOptions struct {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// Gets the maximum supported MTU for the platform based on the defaults in
 | 
					// Gets the maximum supported MTU for the platform based on the defaults in
 | 
				
			||||||
// defaults.GetDefaults().
 | 
					// defaults.GetDefaults().
 | 
				
			||||||
func getSupportedMTU(mtu int) int {
 | 
					func getSupportedMTU(mtu int, istapmode bool) int {
 | 
				
			||||||
	if mtu > defaults.GetDefaults().MaximumIfMTU {
 | 
						if mtu < 1280 {
 | 
				
			||||||
		return defaults.GetDefaults().MaximumIfMTU
 | 
							return 1280
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						if mtu > MaximumMTU(istapmode) {
 | 
				
			||||||
 | 
							return MaximumMTU(istapmode)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	return mtu
 | 
						return mtu
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					@ -80,7 +83,7 @@ func (tun *TunAdapter) Name() string {
 | 
				
			||||||
// the maximum value is determined by your platform. The returned value will
 | 
					// the maximum value is determined by your platform. The returned value will
 | 
				
			||||||
// never exceed that of MaximumMTU().
 | 
					// never exceed that of MaximumMTU().
 | 
				
			||||||
func (tun *TunAdapter) MTU() int {
 | 
					func (tun *TunAdapter) MTU() int {
 | 
				
			||||||
	return getSupportedMTU(tun.mtu)
 | 
						return getSupportedMTU(tun.mtu, tun.IsTAP())
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// IsTAP returns true if the adapter is a TAP adapter (Layer 2) or false if it
 | 
					// IsTAP returns true if the adapter is a TAP adapter (Layer 2) or false if it
 | 
				
			||||||
| 
						 | 
					@ -97,7 +100,11 @@ func DefaultName() string {
 | 
				
			||||||
// DefaultMTU gets the default TUN/TAP interface MTU for your platform. This can
 | 
					// DefaultMTU gets the default TUN/TAP interface MTU for your platform. This can
 | 
				
			||||||
// be as high as MaximumMTU(), depending on platform, but is never lower than 1280.
 | 
					// be as high as MaximumMTU(), depending on platform, but is never lower than 1280.
 | 
				
			||||||
func DefaultMTU() int {
 | 
					func DefaultMTU() int {
 | 
				
			||||||
	return defaults.GetDefaults().DefaultIfMTU
 | 
						ehbytes := 0
 | 
				
			||||||
 | 
						if DefaultIsTAP() {
 | 
				
			||||||
 | 
							ehbytes = 14
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						return defaults.GetDefaults().DefaultIfMTU - ehbytes
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// DefaultIsTAP returns true if the default adapter mode for the current
 | 
					// DefaultIsTAP returns true if the default adapter mode for the current
 | 
				
			||||||
| 
						 | 
					@ -109,8 +116,12 @@ func DefaultIsTAP() bool {
 | 
				
			||||||
// MaximumMTU returns the maximum supported TUN/TAP interface MTU for your
 | 
					// MaximumMTU returns the maximum supported TUN/TAP interface MTU for your
 | 
				
			||||||
// platform. This can be as high as 65535, depending on platform, but is never
 | 
					// platform. This can be as high as 65535, depending on platform, but is never
 | 
				
			||||||
// lower than 1280.
 | 
					// lower than 1280.
 | 
				
			||||||
func MaximumMTU() int {
 | 
					func MaximumMTU(iftapmode bool) int {
 | 
				
			||||||
	return defaults.GetDefaults().MaximumIfMTU
 | 
						ehbytes := 0
 | 
				
			||||||
 | 
						if iftapmode {
 | 
				
			||||||
 | 
							ehbytes = 14
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						return defaults.GetDefaults().MaximumIfMTU - ehbytes
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// Init initialises the TUN/TAP module. You must have acquired a Listener from
 | 
					// Init initialises the TUN/TAP module. You must have acquired a Listener from
 | 
				
			||||||
| 
						 | 
					@ -167,6 +178,9 @@ func (tun *TunAdapter) _start() error {
 | 
				
			||||||
		if err := tun.setup(ifname, iftapmode, addr, tun.mtu); err != nil {
 | 
							if err := tun.setup(ifname, iftapmode, addr, tun.mtu); err != nil {
 | 
				
			||||||
			return err
 | 
								return err
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
							if tun.MTU() != current.IfMTU {
 | 
				
			||||||
 | 
								tun.log.Warnf("Warning: Interface MTU %d automatically adjusted to %d (supported range is 1280-%d)", current.IfMTU, tun.MTU(), MaximumMTU(tun.IsTAP()))
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	if ifname == "none" || ifname == "dummy" {
 | 
						if ifname == "none" || ifname == "dummy" {
 | 
				
			||||||
		tun.log.Debugln("Not starting TUN/TAP as ifname is none or dummy")
 | 
							tun.log.Debugln("Not starting TUN/TAP as ifname is none or dummy")
 | 
				
			||||||
| 
						 | 
					@ -176,7 +190,7 @@ func (tun *TunAdapter) _start() error {
 | 
				
			||||||
	go tun.handler()
 | 
						go tun.handler()
 | 
				
			||||||
	tun.reader.Act(nil, tun.reader._read) // Start the reader
 | 
						tun.reader.Act(nil, tun.reader._read) // Start the reader
 | 
				
			||||||
	tun.icmpv6.Init(tun)
 | 
						tun.icmpv6.Init(tun)
 | 
				
			||||||
	if iftapmode {
 | 
						if tun.IsTAP() {
 | 
				
			||||||
		go tun.icmpv6.Solicit(tun.addr)
 | 
							go tun.icmpv6.Solicit(tun.addr)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	tun.ckr.init(tun)
 | 
						tun.ckr.init(tun)
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -99,7 +99,7 @@ func (tun *TunAdapter) setup(ifname string, iftapmode bool, addr string, mtu int
 | 
				
			||||||
		panic(err)
 | 
							panic(err)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	tun.iface = iface
 | 
						tun.iface = iface
 | 
				
			||||||
	tun.mtu = getSupportedMTU(mtu)
 | 
						tun.mtu = getSupportedMTU(mtu, iftapmode)
 | 
				
			||||||
	return tun.setupAddress(addr)
 | 
						return tun.setupAddress(addr)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -18,7 +18,8 @@ import (
 | 
				
			||||||
// Configures the "utun" adapter with the correct IPv6 address and MTU.
 | 
					// Configures the "utun" adapter with the correct IPv6 address and MTU.
 | 
				
			||||||
func (tun *TunAdapter) setup(ifname string, iftapmode bool, addr string, mtu int) error {
 | 
					func (tun *TunAdapter) setup(ifname string, iftapmode bool, addr string, mtu int) error {
 | 
				
			||||||
	if iftapmode {
 | 
						if iftapmode {
 | 
				
			||||||
		tun.log.Warnln("TAP mode is not supported on this platform, defaulting to TUN")
 | 
							tun.log.Warnln("Warning: TAP mode is not supported on this platform, defaulting to TUN")
 | 
				
			||||||
 | 
							iftapmode = false
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	config := water.Config{DeviceType: water.TUN}
 | 
						config := water.Config{DeviceType: water.TUN}
 | 
				
			||||||
	iface, err := water.New(config)
 | 
						iface, err := water.New(config)
 | 
				
			||||||
| 
						 | 
					@ -26,7 +27,7 @@ func (tun *TunAdapter) setup(ifname string, iftapmode bool, addr string, mtu int
 | 
				
			||||||
		panic(err)
 | 
							panic(err)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	tun.iface = iface
 | 
						tun.iface = iface
 | 
				
			||||||
	tun.mtu = getSupportedMTU(mtu)
 | 
						tun.mtu = getSupportedMTU(mtu, iftapmode)
 | 
				
			||||||
	return tun.setupAddress(addr)
 | 
						return tun.setupAddress(addr)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -26,7 +26,7 @@ func (tun *TunAdapter) setup(ifname string, iftapmode bool, addr string, mtu int
 | 
				
			||||||
		panic(err)
 | 
							panic(err)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	tun.iface = iface
 | 
						tun.iface = iface
 | 
				
			||||||
	tun.mtu = getSupportedMTU(mtu)
 | 
						tun.mtu = getSupportedMTU(mtu, iftapmode)
 | 
				
			||||||
	// The following check is specific to Linux, as the TAP driver only supports
 | 
						// The following check is specific to Linux, as the TAP driver only supports
 | 
				
			||||||
	// an MTU of 65535-14 to make room for the ethernet headers. This makes sure
 | 
						// an MTU of 65535-14 to make room for the ethernet headers. This makes sure
 | 
				
			||||||
	// that the MTU gets rounded down to 65521 instead of causing a panic.
 | 
						// that the MTU gets rounded down to 65521 instead of causing a panic.
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -21,13 +21,13 @@ func (tun *TunAdapter) setup(ifname string, iftapmode bool, addr string, mtu int
 | 
				
			||||||
		panic(err)
 | 
							panic(err)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	tun.iface = iface
 | 
						tun.iface = iface
 | 
				
			||||||
	tun.mtu = getSupportedMTU(mtu)
 | 
						tun.mtu = getSupportedMTU(mtu, iftapmode)
 | 
				
			||||||
	return tun.setupAddress(addr)
 | 
						return tun.setupAddress(addr)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// We don't know how to set the IPv6 address on an unknown platform, therefore
 | 
					// We don't know how to set the IPv6 address on an unknown platform, therefore
 | 
				
			||||||
// write about it to stdout and don't try to do anything further.
 | 
					// write about it to stdout and don't try to do anything further.
 | 
				
			||||||
func (tun *TunAdapter) setupAddress(addr string) error {
 | 
					func (tun *TunAdapter) setupAddress(addr string) error {
 | 
				
			||||||
	tun.log.Warnln("Platform not supported, you must set the address of", tun.iface.Name(), "to", addr)
 | 
						tun.log.Warnln("Warning: Platform not supported, you must set the address of", tun.iface.Name(), "to", addr)
 | 
				
			||||||
	return nil
 | 
						return nil
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -17,7 +17,8 @@ import (
 | 
				
			||||||
// delegate the hard work to "netsh".
 | 
					// delegate the hard work to "netsh".
 | 
				
			||||||
func (tun *TunAdapter) setup(ifname string, iftapmode bool, addr string, mtu int) error {
 | 
					func (tun *TunAdapter) setup(ifname string, iftapmode bool, addr string, mtu int) error {
 | 
				
			||||||
	if !iftapmode {
 | 
						if !iftapmode {
 | 
				
			||||||
		tun.log.Warnln("TUN mode is not supported on this platform, defaulting to TAP")
 | 
							tun.log.Warnln("Warning: TUN mode is not supported on this platform, defaulting to TAP")
 | 
				
			||||||
 | 
							iftapmode = true
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	config := water.Config{DeviceType: water.TAP}
 | 
						config := water.Config{DeviceType: water.TAP}
 | 
				
			||||||
	config.PlatformSpecificParams.ComponentID = "tap0901"
 | 
						config.PlatformSpecificParams.ComponentID = "tap0901"
 | 
				
			||||||
| 
						 | 
					@ -60,7 +61,7 @@ func (tun *TunAdapter) setup(ifname string, iftapmode bool, addr string, mtu int
 | 
				
			||||||
		panic(err)
 | 
							panic(err)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	tun.iface = iface
 | 
						tun.iface = iface
 | 
				
			||||||
	tun.mtu = getSupportedMTU(mtu)
 | 
						tun.mtu = getSupportedMTU(mtu, iftapmode)
 | 
				
			||||||
	err = tun.setupMTU(tun.mtu)
 | 
						err = tun.setupMTU(tun.mtu)
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		panic(err)
 | 
							panic(err)
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue