From 4702da2bcbb7b50d1fb1bb1535045a0df6908a44 Mon Sep 17 00:00:00 2001 From: Neil Alexander Date: Wed, 14 Aug 2019 19:32:40 +0100 Subject: [PATCH] Use new netlink library (fixes #493) --- go.mod | 2 ++ go.sum | 4 ++++ src/tuntap/tun_linux.go | 30 ++++++------------------------ 3 files changed, 12 insertions(+), 24 deletions(-) diff --git a/go.mod b/go.mod index c7773cc..be3c302 100644 --- a/go.mod +++ b/go.mod @@ -8,6 +8,8 @@ require ( github.com/kardianos/minwinsvc v0.0.0-20151122163309-cad6b2b879b0 github.com/mitchellh/mapstructure v1.1.2 github.com/songgao/packets v0.0.0-20160404182456-549a10cd4091 + github.com/vishvananda/netlink v1.0.0 + github.com/vishvananda/netns v0.0.0-20190625233234-7109fa855b0f // indirect github.com/yggdrasil-network/water v0.0.0-20190812103929-c83fe40250f8 golang.org/x/crypto v0.0.0-20190701094942-4def268fd1a4 golang.org/x/net v0.0.0-20190724013045-ca1201d0de80 diff --git a/go.sum b/go.sum index c786f5e..2fde954 100644 --- a/go.sum +++ b/go.sum @@ -12,6 +12,10 @@ github.com/mitchellh/mapstructure v1.1.2 h1:fmNYVwqnSfB9mZU6OS2O6GsXM+wcskZDuKQz github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y= github.com/songgao/packets v0.0.0-20160404182456-549a10cd4091 h1:1zN6ImoqhSJhN8hGXFaJlSC8msLmIbX8bFqOfWLKw0w= github.com/songgao/packets v0.0.0-20160404182456-549a10cd4091/go.mod h1:N20Z5Y8oye9a7HmytmZ+tr8Q2vlP0tAHP13kTHzwvQY= +github.com/vishvananda/netlink v1.0.0 h1:bqNY2lgheFIu1meHUFSH3d7vG93AFyqg3oGbJCOJgSM= +github.com/vishvananda/netlink v1.0.0/go.mod h1:+SR5DhBJrl6ZM7CoCKvpw5BKroDKQ+PJqOg65H/2ktk= +github.com/vishvananda/netns v0.0.0-20190625233234-7109fa855b0f h1:nBX3nTcmxEtHSERBJaIo1Qa26VwRaopnZmfDQUXsF4I= +github.com/vishvananda/netns v0.0.0-20190625233234-7109fa855b0f/go.mod h1:ZjcWmFBXmLKZu9Nxj3WKYEafiSqer2rnvPr0en9UNpI= github.com/yggdrasil-network/water v0.0.0-20180615095340-f732c88f34ae h1:MYCANF1kehCG6x6G+/9txLfq6n3lS5Vp0Mxn1hdiBAc= github.com/yggdrasil-network/water v0.0.0-20180615095340-f732c88f34ae/go.mod h1:R0SBCsugm+Sf1katgTb2t7GXMm+nRIv43tM4VDZbaOs= github.com/yggdrasil-network/water v0.0.0-20190719211521-a76871ea954b/go.mod h1:R0SBCsugm+Sf1katgTb2t7GXMm+nRIv43tM4VDZbaOs= diff --git a/src/tuntap/tun_linux.go b/src/tuntap/tun_linux.go index c9c03c0..764e56b 100644 --- a/src/tuntap/tun_linux.go +++ b/src/tuntap/tun_linux.go @@ -5,11 +5,7 @@ package tuntap // The linux platform specific tun parts import ( - "errors" - "fmt" - "net" - - "github.com/docker/libcontainer/netlink" + "github.com/vishvananda/netlink" water "github.com/yggdrasil-network/water" ) @@ -51,35 +47,21 @@ func (tun *TunAdapter) setup(ifname string, iftapmode bool, addr string, mtu int // to exist on the system, but this will fail if Netlink is not present in the // kernel (it nearly always is). func (tun *TunAdapter) setupAddress(addr string) error { - // Set address - var netIF *net.Interface - ifces, err := net.Interfaces() + nladdr, err := netlink.ParseAddr(addr) if err != nil { return err } - for _, ifce := range ifces { - if ifce.Name == tun.iface.Name() { - var newIF = ifce - netIF = &newIF // Don't point inside ifces, it's apparently unsafe?... - } - } - if netIF == nil { - return errors.New(fmt.Sprintf("Failed to find interface: %s", tun.iface.Name())) - } - ip, ipNet, err := net.ParseCIDR(addr) + nlintf, err := netlink.LinkByName(tun.iface.Name()) if err != nil { return err } - err = netlink.NetworkLinkAddIp(netIF, ip, ipNet) - if err != nil { + if err := netlink.AddrAdd(nlintf, nladdr); err != nil { return err } - err = netlink.NetworkSetMTU(netIF, tun.mtu) - if err != nil { + if err := netlink.LinkSetMTU(nlintf, tun.mtu); err != nil { return err } - netlink.NetworkLinkUp(netIF) - if err != nil { + if err := netlink.LinkSetUp(nlintf); err != nil { return err } return nil