From 876011d0cfeb06d33336140c14e93f8be9a88ab2 Mon Sep 17 00:00:00 2001 From: Mikael Magnusson Date: Wed, 13 Jan 2021 23:31:35 +0100 Subject: tun: implement AddAddress --- tun/netstack/tun.go | 39 +++++++++++++++++++++------------------ 1 file changed, 21 insertions(+), 18 deletions(-) diff --git a/tun/netstack/tun.go b/tun/netstack/tun.go index 6bdea23..e456bf6 100644 --- a/tun/netstack/tun.go +++ b/tun/netstack/tun.go @@ -108,26 +108,11 @@ func CreateNetTUN(localAddresses []net.IP, dnsServers []net.IP, mtu int) (tun.De return nil, nil, fmt.Errorf("CreateNIC: %v", tcpipErr) } for _, ip := range localAddresses { - if ip4 := ip.To4(); ip4 != nil { - tcpipErr = dev.stack.AddAddress(1, ipv4.ProtocolNumber, tcpip.Address(ip4)) - if tcpipErr != nil { - return nil, nil, fmt.Errorf("AddAddress(%v): %v", ip4, tcpipErr) - } - dev.hasV4 = true - } else { - tcpipErr = dev.stack.AddAddress(1, ipv6.ProtocolNumber, tcpip.Address(ip)) - if tcpipErr != nil { - return nil, nil, fmt.Errorf("AddAddress(%v): %v", ip4, tcpipErr) - } - dev.hasV6 = true + tcpipErr := (*Net)(dev).AddAddress(ip) + if tcpipErr != nil { + return nil, 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 @@ -206,6 +191,24 @@ func convertToFullAddr(ip net.IP, port int) (tcpip.FullAddress, tcpip.NetworkPro } } +func (net *Net) AddAddress(ip net.IP) *tcpip.Error { + if ip4 := ip.To4(); ip4 != nil { + tcpipErr := net.stack.AddAddress(1, ipv4.ProtocolNumber, tcpip.Address(ip4)) + if tcpipErr == nil && !net.hasV4 { + net.hasV4 = true + net.stack.AddRoute(tcpip.Route{Destination: header.IPv4EmptySubnet, NIC: 1}) + } + return tcpipErr + } else { + tcpipErr := net.stack.AddAddress(1, ipv6.ProtocolNumber, tcpip.Address(ip)) + if tcpipErr == nil && !net.hasV6{ + net.hasV6 = true + net.stack.AddRoute(tcpip.Route{Destination: header.IPv6EmptySubnet, NIC: 1}) + } + return tcpipErr + } +} + func (net *Net) DialContextTCP(ctx context.Context, addr *net.TCPAddr) (*gonet.TCPConn, error) { if addr == nil { panic("todo: deal with auto addr semantics for nil addr") -- cgit v1.2.3 From be4f4efd78ea9694a66938446ac0d7eae7280d26 Mon Sep 17 00:00:00 2001 From: Mikael Magnusson Date: Thu, 14 Jan 2021 12:05:20 +0100 Subject: tun: add CreateNetTUNWithStack --- tun/netstack/tun.go | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/tun/netstack/tun.go b/tun/netstack/tun.go index e456bf6..671e865 100644 --- a/tun/netstack/tun.go +++ b/tun/netstack/tun.go @@ -34,6 +34,7 @@ import ( type netTun struct { stack *stack.Stack + nicID tcpip.NICID dispatcher stack.NetworkDispatcher events chan tun.Event incomingPacket chan buffer.VectorisedView @@ -96,14 +97,19 @@ func CreateNetTUN(localAddresses []net.IP, dnsServers []net.IP, mtu int) (tun.De TransportProtocols: []stack.TransportProtocolFactory{tcp.NewProtocol, udp.NewProtocol}, HandleLocal: true, } + return CreateNetTUNWithStack(stack.New(opts), 1, localAddresses, dnsServers, mtu) +} + +func CreateNetTUNWithStack(stack *stack.Stack, nicID tcpip.NICID, localAddresses []net.IP, dnsServers []net.IP, mtu int) (Device, *Net, error) { dev := &netTun{ - stack: stack.New(opts), + stack: stack, + nicID: nicID, events: make(chan tun.Event, 10), incomingPacket: make(chan buffer.VectorisedView), dnsServers: dnsServers, mtu: mtu, } - tcpipErr := dev.stack.CreateNIC(1, (*endpoint)(dev)) + tcpipErr := dev.stack.CreateNIC(nicID, (*endpoint)(dev)) if tcpipErr != nil { return nil, nil, fmt.Errorf("CreateNIC: %v", tcpipErr) } @@ -160,7 +166,7 @@ func (tun *netTun) Flush() error { } func (tun *netTun) Close() error { - tun.stack.RemoveNIC(1) + tun.stack.RemoveNIC(tun.nicID) if tun.events != nil { close(tun.events) @@ -193,17 +199,17 @@ func convertToFullAddr(ip net.IP, port int) (tcpip.FullAddress, tcpip.NetworkPro func (net *Net) AddAddress(ip net.IP) *tcpip.Error { if ip4 := ip.To4(); ip4 != nil { - tcpipErr := net.stack.AddAddress(1, ipv4.ProtocolNumber, tcpip.Address(ip4)) + tcpipErr := net.stack.AddAddress(net.nicID, ipv4.ProtocolNumber, tcpip.Address(ip4)) if tcpipErr == nil && !net.hasV4 { net.hasV4 = true - net.stack.AddRoute(tcpip.Route{Destination: header.IPv4EmptySubnet, NIC: 1}) + net.stack.AddRoute(tcpip.Route{Destination: header.IPv4EmptySubnet, NIC: net.nicID}) } return tcpipErr } else { - tcpipErr := net.stack.AddAddress(1, ipv6.ProtocolNumber, tcpip.Address(ip)) + tcpipErr := net.stack.AddAddress(net.nicID, ipv6.ProtocolNumber, tcpip.Address(ip)) if tcpipErr == nil && !net.hasV6{ net.hasV6 = true - net.stack.AddRoute(tcpip.Route{Destination: header.IPv6EmptySubnet, NIC: 1}) + net.stack.AddRoute(tcpip.Route{Destination: header.IPv6EmptySubnet, NIC: net.nicID}) } return tcpipErr } -- cgit v1.2.3 From 2c8d84a3c94cbbe70f3d0443d40b5f3f95d69e54 Mon Sep 17 00:00:00 2001 From: Mikael Magnusson Date: Fri, 22 Jan 2021 12:44:18 +0100 Subject: WIP adapt to changed tunstack --- tun/netstack/tun.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tun/netstack/tun.go b/tun/netstack/tun.go index 671e865..6efa45f 100644 --- a/tun/netstack/tun.go +++ b/tun/netstack/tun.go @@ -100,7 +100,7 @@ func CreateNetTUN(localAddresses []net.IP, dnsServers []net.IP, mtu int) (tun.De return CreateNetTUNWithStack(stack.New(opts), 1, localAddresses, dnsServers, mtu) } -func CreateNetTUNWithStack(stack *stack.Stack, nicID tcpip.NICID, localAddresses []net.IP, dnsServers []net.IP, mtu int) (Device, *Net, error) { +func CreateNetTUNWithStack(stack *stack.Stack, nicID tcpip.NICID, localAddresses []net.IP, dnsServers []net.IP, mtu int) (tun.Device, *Net, error) { dev := &netTun{ stack: stack, nicID: nicID, -- cgit v1.2.3