summaryrefslogtreecommitdiffhomepage
path: root/tun
diff options
context:
space:
mode:
Diffstat (limited to 'tun')
-rw-r--r--tun/netstack/tun.go55
1 files changed, 29 insertions, 26 deletions
diff --git a/tun/netstack/tun.go b/tun/netstack/tun.go
index 44b5f04..f61cc2a 100644
--- a/tun/netstack/tun.go
+++ b/tun/netstack/tun.go
@@ -94,6 +94,32 @@ func (*endpoint) ARPHardwareType() header.ARPHardwareType {
func (e *endpoint) AddHeader(tcpip.LinkAddress, tcpip.LinkAddress, tcpip.NetworkProtocolNumber, *stack.PacketBuffer) {
}
+func (net *Net) addAddress(ip net.IP) tcpip.Error {
+ if ip4 := ip.To4(); ip4 != nil {
+ protoAddr := tcpip.ProtocolAddress{
+ Protocol: ipv4.ProtocolNumber,
+ AddressWithPrefix: tcpip.Address(ip4).WithPrefix(),
+ }
+ tcpipErr := net.stack.AddProtocolAddress(1, protoAddr, stack.AddressProperties{})
+ if tcpipErr == nil && !net.hasV4 {
+ net.hasV4 = true
+ net.stack.AddRoute(tcpip.Route{Destination: header.IPv4EmptySubnet, NIC: 1})
+ }
+ return tcpipErr
+ } else {
+ protoAddr := tcpip.ProtocolAddress{
+ Protocol: ipv6.ProtocolNumber,
+ AddressWithPrefix: tcpip.Address(ip).WithPrefix(),
+ }
+ tcpipErr := net.stack.AddProtocolAddress(1, protoAddr, stack.AddressProperties{})
+ if tcpipErr == nil && !net.hasV6{
+ net.hasV6 = true
+ net.stack.AddRoute(tcpip.Route{Destination: header.IPv6EmptySubnet, NIC: 1})
+ }
+ return tcpipErr
+ }
+}
+
func CreateNetTUN(localAddresses, dnsServers []net.IP, mtu int) (tun.Device, *Net, error) {
opts := stack.Options{
NetworkProtocols: []stack.NetworkProtocolFactory{ipv4.NewProtocol, ipv6.NewProtocol},
@@ -112,34 +138,11 @@ func CreateNetTUN(localAddresses, dnsServers []net.IP, mtu int) (tun.Device, *Ne
return nil, nil, fmt.Errorf("CreateNIC: %v", tcpipErr)
}
for _, ip := range localAddresses {
- if ip4 := ip.To4(); ip4 != nil {
- protoAddr := tcpip.ProtocolAddress{
- Protocol: ipv4.ProtocolNumber,
- AddressWithPrefix: tcpip.Address(ip4).WithPrefix(),
- }
- tcpipErr := dev.stack.AddProtocolAddress(1, protoAddr, stack.AddressProperties{})
- if tcpipErr != nil {
- return nil, nil, fmt.Errorf("AddProtocolAddress(%v): %v", ip4, tcpipErr)
- }
- dev.hasV4 = true
- } else {
- protoAddr := tcpip.ProtocolAddress{
- Protocol: ipv6.ProtocolNumber,
- AddressWithPrefix: tcpip.Address(ip).WithPrefix(),
- }
- tcpipErr := dev.stack.AddProtocolAddress(1, protoAddr, stack.AddressProperties{})
- if tcpipErr != nil {
- return nil, nil, fmt.Errorf("AddProtocolAddress(%v): %v", ip, tcpipErr)
- }
- dev.hasV6 = true
+ tcpipErr := tnet.addAddress(ip)
+ if tcpipErr != nil {
+ return nil, fmt.Errorf("addAddress(%v): %w", ip, tcpipErr)
}
}
- if dev.hasV4 {
- dev.stack.AddRoute(tcpip.Route{Destination: header.IPv4EmptySubnet, NIC: 1})
- }
- if dev.hasV6 {
- dev.stack.AddRoute(tcpip.Route{Destination: header.IPv6EmptySubnet, NIC: 1})
- }
dev.events <- tun.EventUp
return dev, (*Net)(dev), nil