// sets IP4 addr on link removing any existing ones first func setAddr4(link *netlink.Vxlan, ipn *net.IPNet) error { addrs, err := netlink.AddrList(link, syscall.AF_INET) if err != nil { return err } addr := netlink.Addr{ipn, ""} existing := false for _, old := range addrs { if old.IPNet.String() == addr.IPNet.String() { existing = true continue } if err = netlink.AddrDel(link, &old); err != nil { return fmt.Errorf("failed to delete IPv4 addr %s from %s", old.String(), link.Attrs().Name) } } if !existing { if err = netlink.AddrAdd(link, &addr); err != nil { return fmt.Errorf("failed to add IP address %s to %s: %s", ipn.String(), link.Attrs().Name, err) } } return nil }
func setupBridgeIPv6(config *networkConfiguration, i *bridgeInterface) error { procFile := "/proc/sys/net/ipv6/conf/" + config.BridgeName + "/disable_ipv6" ipv6BridgeData, err := ioutil.ReadFile(procFile) if err != nil { return fmt.Errorf("Cannot read IPv6 setup for bridge %v: %v", config.BridgeName, err) } // Enable IPv6 on the bridge only if it isn't already enabled if ipv6BridgeData[0] != '0' { if err := ioutil.WriteFile(procFile, []byte{'0', '\n'}, ipv6ForwardConfPerm); err != nil { return fmt.Errorf("Unable to enable IPv6 addresses on bridge: %v", err) } } _, addrsv6, err := i.addresses() if err != nil { return err } // Add the default link local ipv6 address if it doesn't exist if !findIPv6Address(netlink.Addr{IPNet: bridgeIPv6}, addrsv6) { if err := netlink.AddrAdd(i.Link, &netlink.Addr{IPNet: bridgeIPv6}); err != nil { return &IPv6AddrAddError{IP: bridgeIPv6, Err: err} } } // Store bridge network and default gateway i.bridgeIPv6 = bridgeIPv6 i.gatewayIPv6 = i.bridgeIPv6.IP if config.AddressIPv6 == nil { return nil } // Store and program user specified bridge network and network gateway i.bridgeIPv6 = config.AddressIPv6 i.gatewayIPv6 = config.AddressIPv6.IP if err := netlink.AddrAdd(i.Link, &netlink.Addr{IPNet: i.bridgeIPv6}); err != nil { return &IPv6AddrAddError{IP: i.bridgeIPv6, Err: err} } // Setting route to global IPv6 subnet logrus.Debugf("Adding route to IPv6 network %s via device %s", config.AddressIPv6.String(), config.BridgeName) err = netlink.RouteAdd(&netlink.Route{ Scope: netlink.SCOPE_UNIVERSE, LinkIndex: i.Link.Attrs().Index, Dst: config.AddressIPv6, }) if err != nil && !os.IsExist(err) { logrus.Errorf("Could not add route to IPv6 network %s via device %s", config.AddressIPv6.String(), config.BridgeName) } return nil }
func createDummyInterface(t *testing.T) { if testutils.RunningOnCircleCI() { t.Skipf("Skipping as not supported on CIRCLE CI kernel") } dummy := &netlink.Dummy{ LinkAttrs: netlink.LinkAttrs{ Name: "dummy", }, } err := netlink.LinkAdd(dummy) require.NoError(t, err) dummyLink, err := netlink.LinkByName("dummy") require.NoError(t, err) ip, ipNet, err := net.ParseCIDR("10.1.1.1/24") require.NoError(t, err) ipNet.IP = ip ipAddr := &netlink.Addr{IPNet: ipNet, Label: ""} err = netlink.AddrAdd(dummyLink, ipAddr) require.NoError(t, err) }
// ConfigureIface takes the result of IPAM plugin and // applies to the ifName interface func configureIface(conf *NetConf) error { ifName := conf.IfName link, err := netlink.LinkByName(ifName) if err != nil { return fmt.Errorf("failed to lookup %q: %v", ifName, err) } if err := netlink.LinkSetUp(link); err != nil { return fmt.Errorf("failed to set %q UP: %v", ifName, err) } i, n, err := net.ParseCIDR(conf.IPAddr) if err != nil { return fmt.Errorf("failed to parse ip address :%s", conf.IPAddr) } addr := &net.IPNet{IP: i, Mask: n.Mask} nlAddr := &netlink.Addr{IPNet: addr, Label: ""} if err = netlink.AddrAdd(link, nlAddr); err != nil { return fmt.Errorf("failed to add IP addr to %q: %v", ifName, err) } gw := net.ParseIP(conf.Gateway) if gw == nil { return fmt.Errorf("parse gateway: %s return nil", conf.Gateway) } if err = ip.AddDefaultRoute(gw, link); err != nil { // we skip over duplicate routes as we assume the first one wins if !os.IsExist(err) { return fmt.Errorf("failed to add default route via %v dev %v: %v", gw, ifName, err) } } return nil }
/* SetupNetwork() */ func SetupNetwork(addr string) error { // Bring up loop back interface. lo, e := netlink.LinkByName("lo") if e != nil { return fmt.Errorf("Failed to find loopback interface: %v", e) } if e := netlink.LinkSetUp(lo); e != nil { return fmt.Errorf("Failed to setup loopback interface: %v", e) } if len(addr) > 0 { veth, e := netlink.LinkByName("veth0") if e != nil { return fmt.Errorf("Failed to find veth interface: %v", e) } addr, e := netlink.ParseAddr(addr) if e != nil { return fmt.Errorf("Failed to parse NetworkAddr: %v", e) } netlink.AddrAdd(veth, addr) if e := netlink.LinkSetUp(veth); e != nil { return fmt.Errorf("Network link failed to come up: %v", e) } } return nil }
func IpAddrAdd(cResp chan<- *Response, rawArgs *json.RawMessage, tag string) { args := &struct { Ifname string `json:"ifname"` IpCidr string `json:"ip"` }{} json.Unmarshal(*rawArgs, &args) iface, err := netlink.LinkByName(args.Ifname) if err != nil { cResp <- &Response{nil, tag, NewRTNetlinkError(err)} return } ip, err := netlink.ParseAddr(args.IpCidr) if err != nil { cResp <- &Response{nil, tag, NewRTNetlinkError(err)} return } if err := netlink.AddrAdd(iface, ip); err != nil { cResp <- &Response{nil, tag, NewRTNetlinkError(err)} return } cResp <- &Response{true, tag, nil} }
func setInterfaceIPv6(iface netlink.Link, settings *Interface) error { if settings.AddressIPv6 == nil { return nil } ipAddr := &netlink.Addr{IPNet: settings.AddressIPv6, Label: ""} return netlink.AddrAdd(iface, ipAddr) }
func addIPConfigToLink(ipConfig *ipam.IPConfig, link netlink.Link, ifName string) error { log.Debugf("Configuring link %+v/%s with %+v", link, ifName, ipConfig) addr := &netlink.Addr{IPNet: &ipConfig.IP} if err := netlink.AddrAdd(link, addr); err != nil { return fmt.Errorf("failed to add addr to %q: %v", ifName, err) } // Sort provided routes to make sure we apply any more specific // routes first which may be used as nexthops in wider routes sort.Sort(ipam.ByMask(ipConfig.Routes)) for _, r := range ipConfig.Routes { log.Debugf("Adding route %+v", r) rt := &netlink.Route{ LinkIndex: link.Attrs().Index, Scope: netlink.SCOPE_UNIVERSE, Dst: &r.Destination, Gw: r.NextHop, } if r.IsL2() { rt.Scope = netlink.SCOPE_LINK } if err := netlink.RouteAdd(rt); err != nil { if !os.IsExist(err) { return fmt.Errorf("failed to add route '%s via %v dev %v': %v", r.Destination.String(), r.NextHop, ifName, err) } } } return nil }
func setupHostVeth(vethName string, ipConf *types.IPConfig) error { // hostVeth moved namespaces and may have a new ifindex veth, err := netlink.LinkByName(vethName) if err != nil { return fmt.Errorf("failed to lookup %q: %v", vethName, err) } // TODO(eyakubovich): IPv6 ipn := &net.IPNet{ IP: ipConf.Gateway, Mask: net.CIDRMask(32, 32), } addr := &netlink.Addr{IPNet: ipn, Label: ""} if err = netlink.AddrAdd(veth, addr); err != nil { return fmt.Errorf("failed to add IP addr (%#v) to veth: %v", ipn, err) } ipn = &net.IPNet{ IP: ipConf.IP.IP, Mask: net.CIDRMask(32, 32), } // dst happens to be the same as IP/net of host veth if err = ip.AddHostRoute(ipn, nil, veth); err != nil && !os.IsExist(err) { return fmt.Errorf("failed to add route on host: %v", err) } return nil }
func setInterfaceIPv6(iface netlink.Link, i *nwIface) error { if i.AddressIPv6() == nil { return nil } ipAddr := &netlink.Addr{IPNet: i.AddressIPv6(), Label: "", Flags: syscall.IFA_F_NODAD} return netlink.AddrAdd(iface, ipAddr) }
func configureIface(ifname string, ipn ip.IP4Net, mtu int) error { iface, err := netlink.LinkByName(ifname) if err != nil { return fmt.Errorf("failed to lookup interface %v", ifname) } err = netlink.AddrAdd(iface, &netlink.Addr{IPNet: ipn.ToIPNet(), Label: ""}) if err != nil { return fmt.Errorf("failed to add IP address %v to %v: %v", ipn.String(), ifname, err) } err = netlink.LinkSetMTU(iface, mtu) if err != nil { return fmt.Errorf("failed to set MTU for %v: %v", ifname, err) } err = netlink.LinkSetUp(iface) if err != nil { return fmt.Errorf("failed to set interface %v to UP state: %v", ifname, err) } // explicitly add a route since there might be a route for a subnet already // installed by Docker and then it won't get auto added err = netlink.RouteAdd(&netlink.Route{ LinkIndex: iface.Attrs().Index, Scope: netlink.SCOPE_UNIVERSE, Dst: ipn.Network().ToIPNet(), }) if err != nil && err != syscall.EEXIST { return fmt.Errorf("failed to add route (%v -> %v): %v", ipn.Network().String(), ifname, err) } return nil }
// ConfigureIface takes the result of IPAM plugin and // applies to the ifName interface func ConfigureIface(ifName string, res *types.Result) error { link, err := netlink.LinkByName(ifName) if err != nil { return fmt.Errorf("failed to lookup %q: %v", ifName, err) } if err := netlink.LinkSetUp(link); err != nil { return fmt.Errorf("failed to set %q UP: %v", ifName, err) } // TODO(eyakubovich): IPv6 addr := &netlink.Addr{IPNet: &res.IP4.IP, Label: ""} if err = netlink.AddrAdd(link, addr); err != nil { return fmt.Errorf("failed to add IP addr to %q: %v", ifName, err) } for _, r := range res.IP4.Routes { gw := r.GW if gw == nil { gw = res.IP4.Gateway } if err = ip.AddRoute(&r.Dst, gw, link); err != nil { // we skip over duplicate routes as we assume the first one wins if !os.IsExist(err) { return fmt.Errorf("failed to add route '%v via %v dev %v': %v", r.Dst, gw, ifName, err) } } } return nil }
func main() { lo, _ := netlink.LinkByName("lo") fmt.Println(lo) addr, _ := netlink.ParseAddr("127.0.0.2/8") fmt.Println(addr) netlink.AddrAdd(lo, addr) fmt.Println(netlink.AddrList(lo, netlink.FAMILY_ALL)) req := nl.NewNetlinkRequest(syscall.NLMSG_MIN_TYPE, syscall.NLM_F_REQUEST) data := append([]byte("TASKSTATS"), 0) a := &Attr{ Type: 2, Data: data, } Hdr := []byte{ 3, 0, 0, 0, } m := &Message{ Type: 16, Pid: -1, Seq: -1, Flags: NLM_F_REQUEST, Payload: append(Hdr, a.Dump()...), } req.AddData(m) res, _ := req.Execute(syscall.NETLINK_GENERIC, 0) fmt.Println(res) fmt.Println(parse_attributes(res[0][4:])[1]) }
func createInterface(t *testing.T, name string, nws ...string) { // Add interface link := &netlink.Bridge{ LinkAttrs: netlink.LinkAttrs{ Name: "test", }, } bips := []*net.IPNet{} for _, nw := range nws { bip, err := types.ParseCIDR(nw) if err != nil { t.Fatal(err) } bips = append(bips, bip) } if err := netlink.LinkAdd(link); err != nil { t.Fatalf("Failed to create interface via netlink: %v", err) } for _, bip := range bips { if err := netlink.AddrAdd(link, &netlink.Addr{IPNet: bip}); err != nil { t.Fatal(err) } } if err := netlink.LinkSetUp(link); err != nil { t.Fatal(err) } }
// Create creates a bridge device and returns the interface. // If the device already exists, returns the existing interface. func (Bridge) Create(name string, ip net.IP, subnet *net.IPNet) (intf *net.Interface, err error) { netlinkMu.Lock() defer netlinkMu.Unlock() if intf, _ := net.InterfaceByName(name); intf != nil { return intf, nil } link := &netlink.Bridge{LinkAttrs: netlink.LinkAttrs{Name: name}} if err := netlink.LinkAdd(link); err != nil && err.Error() != "file exists" { return nil, fmt.Errorf("devices: create bridge: %v", err) } hAddr, _ := net.ParseMAC(randMacAddr()) err = netlink.LinkSetHardwareAddr(link, hAddr) if err != nil { return nil, fmt.Errorf("devices: set hardware address: %v", err) } if intf, err = net.InterfaceByName(name); err != nil { return nil, fmt.Errorf("devices: look up created bridge interface: %v", err) } addr := &netlink.Addr{IPNet: &net.IPNet{IP: ip, Mask: subnet.Mask}} if err = netlink.AddrAdd(link, addr); err != nil && err.Error() != "file exists" { return nil, fmt.Errorf("devices: add IP to bridge: %v", err) } return intf, nil }
func setupBridgeIPv6(config *networkConfiguration, i *bridgeInterface) error { // Enable IPv6 on the bridge procFile := "/proc/sys/net/ipv6/conf/" + config.BridgeName + "/disable_ipv6" if err := ioutil.WriteFile(procFile, []byte{'0', '\n'}, ipv6ForwardConfPerm); err != nil { return fmt.Errorf("Unable to enable IPv6 addresses on bridge: %v", err) } _, addrsv6, err := i.addresses() if err != nil { return err } // Add the default link local ipv6 address if it doesn't exist if !findIPv6Address(netlink.Addr{IPNet: bridgeIPv6}, addrsv6) { if err := netlink.AddrAdd(i.Link, &netlink.Addr{IPNet: bridgeIPv6}); err != nil { return &IPv6AddrAddError{IP: bridgeIPv6, Err: err} } } // Store bridge network and default gateway i.bridgeIPv6 = bridgeIPv6 i.gatewayIPv6 = i.bridgeIPv6.IP return nil }
func routingUp() { veth, err := netlink.LinkByName("myveth1") if err != nil { panic(err) } err = netlink.LinkSetUp(veth) if err != nil { panic(err) } addr, _ := netlink.ParseAddr("172.19.80.2/24") err = netlink.AddrAdd(veth, addr) if err != nil { panic(err) } routes := createRoutes(veth) for _, route := range routes { fmt.Println("Adding route", route) err := netlink.RouteAdd(route) if err != nil { fmt.Println(err) // panic(err) } } }
func applyNetConf(link netlink.Link, netConf config.InterfaceConfig) error { if netConf.DHCP { log.Infof("Running DHCP on %s", link.Attrs().Name) cmd := exec.Command("dhcpcd", "-A4", "-e", "force_hostname=true", link.Attrs().Name) cmd.Stdout = os.Stdout cmd.Stderr = os.Stderr if err := cmd.Run(); err != nil { log.Error(err) } } else if netConf.IPV4LL { if err := AssignLinkLocalIP(link); err != nil { log.Error("IPV4LL set failed") return err } } else if netConf.Address == "" { return nil } else { addr, err := netlink.ParseAddr(netConf.Address) if err != nil { return err } if err := netlink.AddrAdd(link, addr); err != nil { log.Error("addr add failed") return err } log.Infof("Set %s on %s", netConf.Address, link.Attrs().Name) } if netConf.MTU > 0 { if err := netlink.LinkSetMTU(link, netConf.MTU); err != nil { log.Error("set MTU Failed") return err } } if err := netlink.LinkSetUp(link); err != nil { log.Error("failed to setup link") return err } if netConf.Gateway != "" { gatewayIp := net.ParseIP(netConf.Gateway) if gatewayIp == nil { return errors.New("Invalid gateway address " + netConf.Gateway) } route := netlink.Route{ Scope: netlink.SCOPE_UNIVERSE, Gw: net.ParseIP(netConf.Gateway), } if err := netlink.RouteAdd(&route); err != nil { log.Error("gateway set failed") return err } log.Infof("Set default gateway %s", netConf.Gateway) } return nil }
// Set the IP addr of a netlink interface func (driver *driver) setInterfaceIP(name string, rawIP string) error { var netlinkRetryTimer time.Duration netlinkRetryTimer = 2 iface, err := netlink.LinkByName(name) if err != nil { log.Debugf("error retrieving new OVS bridge netlink link [ %s ] allowing another [ %d ] seconds for the host to finish creating it..", bridgeName, netlinkRetryTimer) time.Sleep(netlinkRetryTimer * time.Second) iface, err = netlink.LinkByName(name) if err != nil { log.Debugf("error retrieving new OVS bridge netlink link [ %s ] allowing another [ %d ] seconds for the host to finish creating it..", bridgeName, netlinkRetryTimer) time.Sleep(netlinkRetryTimer * time.Second) iface, err = netlink.LinkByName(name) if err != nil { log.Fatalf("Abandoning retrieving the new OVS bridge link from netlink, Run [ ip link ] to troubleshoot the error: %s", err) return err } } } ipNet, err := netlink.ParseIPNet(rawIP) if err != nil { return err } addr := &netlink.Addr{ipNet, ""} return netlink.AddrAdd(iface, addr) }
func setInterfaceIPv6(iface netlink.Link, i *nwIface) error { if i.AddressIPv6() == nil { return nil } ipAddr := &netlink.Addr{IPNet: i.AddressIPv6(), Label: ""} return netlink.AddrAdd(iface, ipAddr) }
func ensureBridgeAddr(br *netlink.Bridge, ipn *net.IPNet, forceAddress bool) error { addrs, err := netlink.AddrList(br, syscall.AF_INET) if err != nil && err != syscall.ENOENT { return fmt.Errorf("could not get list of IP addresses: %v", err) } // if there're no addresses on the bridge, it's ok -- we'll add one if len(addrs) > 0 { ipnStr := ipn.String() for _, a := range addrs { // string comp is actually easiest for doing IPNet comps if a.IPNet.String() == ipnStr { return nil } // If forceAddress is set to true then reconfigure IP address otherwise throw error if forceAddress { if err = deleteBridgeAddr(br, a.IPNet); err != nil { return err } } else { return fmt.Errorf("%q already has an IP address different from %v", br.Name, ipn.String()) } } } addr := &netlink.Addr{IPNet: ipn, Label: ""} if err := netlink.AddrAdd(br, addr); err != nil { return fmt.Errorf("could not add IP address to %q: %v", br.Name, err) } return nil }
func assignBridgeIP(bridgeName string, ipnet net.IPNet) error { link, err := netlink.LinkByName(bridgeName) if err != nil { return err } if err := netlink.AddrAdd(link, &netlink.Addr{IPNet: &ipnet}); err != nil { return fmt.Errorf("failed to add IP address to %q: %v", bridgeName, err) } return nil }
func setInterfaceIPAliases(iface netlink.Link, i *nwIface) error { if i.IPAliases() == nil { return nil } for _, ip := range i.IPAliases() { ipAddr := &netlink.Addr{IPNet: ip, Label: ""} netlink.AddrAdd(iface, ipAddr) } return nil }
func addrAdd(t *testing.T, l netlink.Link, a string) { addr, err := netlink.ParseAddr(a) if err != nil { t.Fatal(err) } err = netlink.AddrAdd(l, addr) if err != nil && err != syscall.EEXIST { t.Fatal(err) } }
func TestSetupVerifyIPv6(t *testing.T) { defer osl.SetupTestOSContext(t)() addrv4 := net.IPv4(192, 168, 1, 1) inf := setupVerifyTest(t) config := &networkConfiguration{} config.AddressIPv4 = &net.IPNet{IP: addrv4, Mask: addrv4.DefaultMask()} config.EnableIPv6 = true if err := netlink.AddrAdd(inf.Link, &netlink.Addr{IPNet: bridgeIPv6}); err != nil { t.Fatalf("Failed to assign IPv6 %s to interface: %v", bridgeIPv6, err) } if err := netlink.AddrAdd(inf.Link, &netlink.Addr{IPNet: config.AddressIPv4}); err != nil { t.Fatalf("Failed to assign IPv4 %s to interface: %v", config.AddressIPv4, err) } if err := setupVerifyAndReconcile(config, inf); err != nil { t.Fatalf("Address verification failed: %v", err) } }
func applyNetConf(link netlink.Link, netConf InterfaceConfig) error { if netConf.IPV4LL { if err := AssignLinkLocalIP(link); err != nil { log.Errorf("IPV4LL set failed: %v", err) return err } } else if netConf.Address == "" { return nil } else { addr, err := netlink.ParseAddr(netConf.Address) if err != nil { return err } if err := netlink.AddrAdd(link, addr); err != nil { //Ignore this error log.Errorf("addr add failed: %v", err) } else { log.Infof("Set %s on %s", netConf.Address, link.Attrs().Name) } } if netConf.MTU > 0 { if err := netlink.LinkSetMTU(link, netConf.MTU); err != nil { log.Errorf("set MTU Failed: %v", err) return err } } if err := netlink.LinkSetUp(link); err != nil { log.Errorf("failed to setup link: %v", err) return err } if netConf.Gateway != "" { gatewayIp := net.ParseIP(netConf.Gateway) if gatewayIp == nil { return errors.New("Invalid gateway address " + netConf.Gateway) } route := netlink.Route{ Scope: netlink.SCOPE_UNIVERSE, Gw: net.ParseIP(netConf.Gateway), } if err := netlink.RouteAdd(&route); err != nil { log.Errorf("gateway set failed: %v", err) return err } log.Infof("Set default gateway %s", netConf.Gateway) } return nil }
// SetInterfaceIP : Set IP address of an interface func SetInterfaceIP(name string, ipstr string) error { iface, err := netlink.LinkByName(name) if err != nil { return err } ipaddr, err := netlink.ParseAddr(ipstr) if err != nil { return err } netlink.LinkSetUp(iface) return netlink.AddrAdd(iface, ipaddr) }
// Set the IP addr of a link func (driver *driver) 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: ipNet, Label: ""} return netlink.AddrAdd(iface, addr) }
func (Link) AddIP(intf *net.Interface, ip net.IP, subnet *net.IPNet) error { netlinkMu.Lock() defer netlinkMu.Unlock() link, err := netlink.LinkByName(intf.Name) if err != nil { return errF(err) } addr := &netlink.Addr{IPNet: &net.IPNet{IP: ip, Mask: subnet.Mask}} return errF(netlink.AddrAdd(link, addr)) }
//AddIp it receives a CIDR Address and add it to the given interface func AddIp(ip, iface string) error { link, err := netlink.LinkByName(iface) if err != nil { return err } addr, err := netlink.ParseAddr(ip) if err != nil { return err } return netlink.AddrAdd(link, addr) }