5
0
mirror of https://github.com/cwinfo/yggdrasil-go.git synced 2024-11-22 22:20:27 +00:00

use netlink instead of ip commands to set address/mtu and bring up the tuntap device on linux

This commit is contained in:
Arceliar 2018-03-03 16:41:36 -06:00
parent 56fd7bd4d4
commit 5ec6265a70

View File

@ -3,12 +3,14 @@ package yggdrasil
// The linux platform specific tun parts // The linux platform specific tun parts
// It depends on iproute2 being installed to set things on the tun device // It depends on iproute2 being installed to set things on the tun device
import "errors"
import "fmt" import "fmt"
import "os/exec" import "net"
import "strings"
import water "github.com/neilalexander/water" import water "github.com/neilalexander/water"
import "github.com/docker/libcontainer/netlink"
func getDefaults() tunDefaultParameters { func getDefaults() tunDefaultParameters {
return tunDefaultParameters{ return tunDefaultParameters{
maximumIfMTU: 65535, maximumIfMTU: 65535,
@ -39,26 +41,33 @@ func (tun *tunDevice) setup(ifname string, iftapmode bool, addr string, mtu int)
func (tun *tunDevice) setupAddress(addr string) error { func (tun *tunDevice) setupAddress(addr string) error {
// Set address // Set address
cmd := exec.Command("ip", "-f", "inet6", var netIF *net.Interface
"addr", "add", addr, ifces, err := net.Interfaces()
"dev", tun.iface.Name())
tun.core.log.Printf("ip command: %v", strings.Join(cmd.Args, " "))
output, err := cmd.CombinedOutput()
if err != nil { if err != nil {
tun.core.log.Printf("Linux ip failed: %v.", err)
tun.core.log.Println(string(output))
return err return err
} }
// Set MTU and bring device up for _, ifce := range ifces {
cmd = exec.Command("ip", "link", "set", if ifce.Name == tun.iface.Name() {
"dev", tun.iface.Name(), netIF = &ifce
"mtu", fmt.Sprintf("%d", tun.mtu), }
"up") }
tun.core.log.Printf("ip command: %v", strings.Join(cmd.Args, " ")) if netIF == nil {
output, err = cmd.CombinedOutput() return errors.New(fmt.Sprintf("Failed to find interface: %s", tun.iface.Name()))
}
ip, ipNet, err := net.ParseCIDR(addr)
if err != nil {
return err
}
err = netlink.NetworkLinkAddIp(netIF, ip, ipNet)
if err != nil {
return err
}
err = netlink.NetworkSetMTU(netIF, tun.mtu)
if err != nil {
return err
}
netlink.NetworkLinkUp(netIF)
if err != nil { if err != nil {
tun.core.log.Printf("Linux ip failed: %v.", err)
tun.core.log.Println(string(output))
return err return err
} }
return nil return nil