summaryrefslogtreecommitdiffhomepage
path: root/tunnel/tools
diff options
context:
space:
mode:
Diffstat (limited to 'tunnel/tools')
-rw-r--r--tunnel/tools/libwg-go/api-android.go53
-rw-r--r--tunnel/tools/libwg-go/go.mod2
-rw-r--r--tunnel/tools/libwg-go/go.sum6
3 files changed, 46 insertions, 15 deletions
diff --git a/tunnel/tools/libwg-go/api-android.go b/tunnel/tools/libwg-go/api-android.go
index 986bc177..2a4e68b3 100644
--- a/tunnel/tools/libwg-go/api-android.go
+++ b/tunnel/tools/libwg-go/api-android.go
@@ -71,8 +71,14 @@ type TunnelHandle struct {
logger *device.Logger
interfaceName string
tunFd int
+ linkEP stack.LinkEndpoint
+ wgNic tcpip.NICID
+ tunNic tcpip.NICID
}
+var tcpipStack *stack.Stack
+var nextNic tcpip.NICID = 1
+
var tunnelHandles map[int32]TunnelHandle
func init() {
@@ -183,11 +189,17 @@ func wgTurnOff(tunnelHandle int32) {
if !ok {
return
}
+ handle.logger.Verbosef("wgTurnOff enter")
delete(tunnelHandles, tunnelHandle)
if handle.uapi != nil {
handle.uapi.Close()
}
handle.device.Close()
+ handle.logger.Verbosef("wgTurnOff before Close")
+ handle.stack.RemoveNIC(handle.wgNic)
+ handle.stack.RemoveNIC(handle.tunNic)
+ unix.Close(handle.tunFd)
+ handle.logger.Verbosef("wgTurnOff exit")
}
//export wgGetSocketV4
@@ -295,26 +307,30 @@ func newTunnel(logger *device.Logger) (TunnelHandle, tun.Device, error) {
dnsServers := []net.IP{net.ParseIP("8.8.8.8"), net.ParseIP("8.8.4.4")}
mtu := 1420
- opts := stack.Options{
- NetworkProtocols: []stack.NetworkProtocolFactory{ipv4.NewProtocol, ipv6.NewProtocol},
- TransportProtocols: []stack.TransportProtocolFactory{tcp.NewProtocol, udp.NewProtocol},
- HandleLocal: true,
+ if tcpipStack == nil {
+ opts := stack.Options{
+ NetworkProtocols: []stack.NetworkProtocolFactory{ipv4.NewProtocol, ipv6.NewProtocol},
+ TransportProtocols: []stack.TransportProtocolFactory{tcp.NewProtocol, udp.NewProtocol},
+ HandleLocal: true,
+ }
+ tcpipStack = stack.New(opts)
}
- stack := stack.New(opts)
- tun, tnet, err := netstack.CreateNetTUNWithStack(stack, 1, localAddresses, dnsServers, mtu)
+ nic := nextNic; nextNic = nextNic + 1
+ tun, tnet, err := netstack.CreateNetTUNWithStack(tcpipStack, nic, localAddresses, dnsServers, mtu)
if err != nil {
// TODO: Release stack?
return TunnelHandle{}, nil, err
}
- handle := TunnelHandle{stack: stack, tnet: tnet, logger: logger}
+ handle := TunnelHandle{stack: tcpipStack, tnet: tnet, logger: logger, wgNic: nic}
return handle, tun, nil
}
func (handle *TunnelHandle) startDHCPv6() {
// TODO: Configurable address
- src, err := net.ResolveUDPAddr("udp6", "[fe80::104%1]:546"); if err != nil {
+ address := fmt.Sprintf("[fe80::104%%%d]:546", handle.wgNic)
+ src, err := net.ResolveUDPAddr("udp6", address); if err != nil {
handle.logger.Errorf("ResolveUDPAddr: %v", err)
return
}
@@ -323,7 +339,7 @@ func (handle *TunnelHandle) startDHCPv6() {
dst = nil
conn, err := handle.tnet.DialUDP(src, dst); if err != nil {
- handle.logger.Errorf("DialUDP: %v (%v -> %v)", src, dst, err)
+ handle.logger.Errorf("DialUDP: (%v -> %v) %v", src, dst, err)
return
}
@@ -333,6 +349,14 @@ func (handle *TunnelHandle) startDHCPv6() {
return
}
+ defer func() {
+ err := client.Close()
+ if err != nil {
+ handle.logger.Errorf("Close: %v", err)
+ }
+ handle.logger.Verbosef("Closed")
+ }()
+
duid := dhcpv6.Duid{
Type: dhcpv6.DUID_LL,
HwType: iana.HWTypeEthernet,
@@ -418,7 +442,7 @@ func (handle *TunnelHandle) startDHCPv6() {
handle.logger.Verbosef("DHCP done")
- var nic tcpip.NICID = 2
+ var nic tcpip.NICID = nextNic; nextNic = nextNic + 1
handle.createNetTUNFromFD(nic)
// TODO: Close clients?
@@ -450,7 +474,7 @@ func (handle *TunnelHandle) startDHCPv6() {
})
handle.stack.AddRoute(tcpip.Route{
Destination: header.IPv4EmptySubnet,
- NIC: 1,
+ NIC: handle.wgNic,
})
}
@@ -469,7 +493,7 @@ func (handle *TunnelHandle) startDHCPv6() {
handle.stack.AddRoute(tcpip.Route{
Destination: header.IPv6EmptySubnet,
- NIC: 1,
+ NIC: handle.wgNic,
})
iana := msg.GetOneOption(dhcpv6.OptionIANA).(*dhcpv6.OptIANA)
@@ -513,17 +537,18 @@ func (handle *TunnelHandle) newTun(nic tcpip.NICID) error {
}
var mtu uint32 = 1420
- linkEP, err := fdbased.New(&fdbased.Options{FDs: []int{handle.tunFd}, MTU: mtu})
+ handle.linkEP, err = fdbased.New(&fdbased.Options{FDs: []int{handle.tunFd}, MTU: mtu})
if err != nil {
handle.logger.Errorf("fdbased.New: %v", err)
return err
}
- if err := handle.stack.CreateNIC(nic, sniffer.New(linkEP)); err != nil {
+ if err := handle.stack.CreateNIC(nic, sniffer.New(handle.linkEP)); err != nil {
handle.logger.Errorf("CreateNIC: %v", err)
// TODO: Return error
return nil
}
+ handle.tunNic = nic
return nil
}
diff --git a/tunnel/tools/libwg-go/go.mod b/tunnel/tools/libwg-go/go.mod
index e2212070..8065de3f 100644
--- a/tunnel/tools/libwg-go/go.mod
+++ b/tunnel/tools/libwg-go/go.mod
@@ -16,4 +16,4 @@ replace gvisor.dev/gvisor => golang.m7n.se/gvisor v0.0.0-20210124001233-ea4772ea
replace golang.zx2c4.com/wireguard => golang.m7n.se/wireguard v0.0.0-20210202225823-3f068cc9cec2
-replace golang.zx2c4.com/wireguard/tun/netstack => golang.m7n.se/wireguard/tun/netstack v0.0.0-20210202225823-3f068cc9cec2
+replace golang.zx2c4.com/wireguard/tun/netstack => golang.m7n.se/wireguard/tun/netstack v0.0.0-20210203220917-09e5183c2ea7
diff --git a/tunnel/tools/libwg-go/go.sum b/tunnel/tools/libwg-go/go.sum
index 00bd3946..b8cf49d2 100644
--- a/tunnel/tools/libwg-go/go.sum
+++ b/tunnel/tools/libwg-go/go.sum
@@ -233,6 +233,12 @@ golang.m7n.se/wireguard v0.0.0-20210202225823-3f068cc9cec2 h1:bPbTX9a5oyjW2E1NYA
golang.m7n.se/wireguard v0.0.0-20210202225823-3f068cc9cec2/go.mod h1:r0ExowOoGFfDoLDxx+M9SYbNVsoZ0xviLL+K4f2mt+A=
golang.m7n.se/wireguard/tun/netstack v0.0.0-20210202225823-3f068cc9cec2 h1:qzbGRlqeuQd7QXdIzV60k3k1IhWEFc6M5RgzoOPFXqo=
golang.m7n.se/wireguard/tun/netstack v0.0.0-20210202225823-3f068cc9cec2/go.mod h1:e6oylkegAl6XhFmlXpz8AhyBtXfLdgVeGB2OjT0+3WQ=
+golang.m7n.se/wireguard/tun/netstack v0.0.0-20210203220050-069c5ec96ad7 h1:bVJNfPkhAfHDtJ6bhbiwU3ChKw+lOfTlDjRCI633O0U=
+golang.m7n.se/wireguard/tun/netstack v0.0.0-20210203220050-069c5ec96ad7/go.mod h1:e6oylkegAl6XhFmlXpz8AhyBtXfLdgVeGB2OjT0+3WQ=
+golang.m7n.se/wireguard/tun/netstack v0.0.0-20210203220611-7a7f89b37634 h1:g3rFSvzMRJL3JF8fwXyCt1VBX21BQO5xQ9Utl/oQWfE=
+golang.m7n.se/wireguard/tun/netstack v0.0.0-20210203220611-7a7f89b37634/go.mod h1:e6oylkegAl6XhFmlXpz8AhyBtXfLdgVeGB2OjT0+3WQ=
+golang.m7n.se/wireguard/tun/netstack v0.0.0-20210203220917-09e5183c2ea7 h1:e9EGt0VgOCCf1NYaWy+fyT4/0ueaTjZemzV8bYsIsgQ=
+golang.m7n.se/wireguard/tun/netstack v0.0.0-20210203220917-09e5183c2ea7/go.mod h1:e6oylkegAl6XhFmlXpz8AhyBtXfLdgVeGB2OjT0+3WQ=
golang.org/x/crypto v0.0.0-20190211182817-74369b46fc67/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=