Exemplo n.º 1
0
func SetInterfaceInNamespaceFd(name string, fd uintptr) error {
	iface, err := netlink.LinkByName(name)
	if err != nil {
		return err
	}
	return netlink.LinkSetNsFd(iface, int(fd))
}
Exemplo n.º 2
0
func SetMtu(name string, mtu int) error {
	iface, err := netlink.LinkByName(name)
	if err != nil {
		return err
	}
	return netlink.LinkSetMTU(iface, mtu)
}
Exemplo n.º 3
0
func SetInterfaceInNamespacePid(name string, nsPid int) error {
	iface, err := netlink.LinkByName(name)
	if err != nil {
		return err
	}
	return netlink.LinkSetNsPid(iface, nsPid)
}
Exemplo n.º 4
0
func ChangeInterfaceName(old, newName string) error {
	iface, err := netlink.LinkByName(old)
	if err != nil {
		return err
	}
	return netlink.LinkSetName(iface, newName)
}
Exemplo n.º 5
0
func InterfaceDown(name string) error {
	iface, err := netlink.LinkByName(name)
	if err != nil {
		return err
	}
	return netlink.LinkSetDown(iface)
}
Exemplo n.º 6
0
func SetInterfaceMac(name string, macaddr string) error {
	iface, err := netlink.LinkByName(name)
	if err != nil {
		return err
	}
	hwaddr, err := net.ParseMAC(macaddr)
	if err != nil {
		return err
	}
	return netlink.LinkSetHardwareAddr(iface, hwaddr)
}
Exemplo n.º 7
0
func SetInterfaceIp(name string, rawIp string) error {
	iface, err := netlink.LinkByName(name)
	if err != nil {
		return err
	}

	ipNet, err := netlink.ParseIPNet(rawIp)
	if err != nil {
		return err
	}
	addr := &netlink.Addr{ipNet, ""}
	return netlink.AddrAdd(iface, addr)
}
Exemplo n.º 8
0
// Return the IPv4 address of a network interface
func GetIfaceAddr(name string) (*net.IPNet, error) {
	iface, err := netlink.LinkByName(name)
	if err != nil {
		return nil, err
	}

	addrs, err := netlink.AddrList(iface, netlink.FAMILY_V4)
	if err != nil {
		return nil, err
	}

	if len(addrs) == 0 {
		return nil, fmt.Errorf("Interface %v has no IP addresses", name)
	}

	if len(addrs) > 1 {
		log.Info("Interface %v has more than 1 IPv4 address. Defaulting to using %v\n", name, addrs[0].IP)
	}

	return addrs[0].IPNet, nil
}
Exemplo n.º 9
0
func SetDefaultGateway(ip, ifaceName string) error {
	iface, err := netlink.LinkByName(ifaceName)
	if err != nil {
		return err
	}
	gw := net.ParseIP(ip)
	if gw == nil {
		return errors.New("Invalid gateway address")
	}

	_, dst, err := net.ParseCIDR("0.0.0.0/0")
	if err != nil {
		return err
	}
	defaultRoute := &netlink.Route{
		LinkIndex: iface.Attrs().Index,
		Dst:       dst,
		Gw:        gw,
	}
	return netlink.RouteAdd(defaultRoute)
}
Exemplo n.º 10
0
func TestSetInterfaceInNamespaceFd(t *testing.T) {
	teardown := setUp(t)
	defer teardown()

	newns, err := netns.New()
	if err != nil {
		t.Fatal("Failed to create newns")
	}
	defer newns.Close()

	netlink.LinkAdd(&netlink.Dummy{netlink.LinkAttrs{Name: "foo2"}})

	err = SetInterfaceInNamespaceFd("foo2", uintptr(newns))
	if err != nil {
		t.Fatal(err)
	}

	_, err = netlink.LinkByName("bar")
	if err == nil {
		t.Fatal("Link foo2 is still in newns")
	}
}