Example #1
0
func (i *IPAllocator) getIPMap() (map[string]struct{}, error) {
	// get the neighbors
	var (
		list []netlink.Neigh
		err  error
	)
	if i.IPNet.IP.To4() == nil {
		list, err = netlink.NeighList(i.Bridge.Index, netlink.FAMILY_V6)
		if err != nil {
			return nil, fmt.Errorf("Cannot retrieve IPv6 neighbor information for interface %s: %v", i.Bridge.Name, err)
		}
	} else {
		list, err = netlink.NeighList(i.Bridge.Index, netlink.FAMILY_V4)
		if err != nil {
			return nil, fmt.Errorf("Cannot retrieve IPv4 neighbor information for interface %s: %v", i.Bridge.Name, err)
		}
	}

	ipMap := map[string]struct{}{}
	for _, entry := range list {
		ipMap[entry.String()] = struct{}{}
	}

	return ipMap, nil
}
Example #2
0
func GetIPv6LinkLocalNeighborAddress(ifname string) (string, error) {
	ifi, err := net.InterfaceByName(ifname)
	if err != nil {
		return "", err
	}
	neighs, err := netlink.NeighList(ifi.Index, netlink.FAMILY_V6)
	if err != nil {
		return "", err
	}
	cnt := 0
	var addr net.IP
	for _, neigh := range neighs {
		local, err := isLocalLinkLocalAddress(ifi.Index, neigh.IP)
		if err != nil {
			return "", err
		}
		if neigh.IP.IsLinkLocalUnicast() && !local {
			addr = neigh.IP
			cnt += 1
		}
	}

	if cnt == 0 {
		return "", fmt.Errorf("no ipv6 link-local neighbor found")
	} else if cnt > 1 {
		return "", fmt.Errorf("found %d link-local neighbors. only support p2p link", cnt)
	}

	return fmt.Sprintf("%s%%%s", addr, ifname), nil
}
Example #3
0
func getMacAddresses(iface string) ([]string, error) {
	br0, err := netlink.LinkByName(iface)
	if err != nil {
		return nil, fmt.Errorf("%s is not configured on this host", iface)
	}
	neighbors, err := netlink.NeighList(br0.Attrs().Index, 0)
	if err != nil {
		return nil, fmt.Errorf("neighbor list could not be retrieved")
	}
	hwAddrs := []string{}
	for _, n := range neighbors {
		hwAddrs = append(hwAddrs, n.HardwareAddr.String())
	}
	return hwAddrs, nil
}
Example #4
0
func getIPAddressFromMac(macAddr, iface string) net.IP {
	br0, err := netlink.LinkByName(iface)
	if err != nil {
		return nil
	}
	neighbors, err := netlink.NeighList(br0.Attrs().Index, 0)
	if err != nil {
		return nil
	}
	for _, n := range neighbors {
		if n.HardwareAddr.String() == macAddr {
			return n.IP
		}
	}
	return nil
}
Example #5
0
func (dev *vxlanDevice) GetL2List() ([]netlink.Neigh, error) {
	log.Infof("calling GetL2List() dev.link.Index: %d ", dev.link.Index)
	return netlink.NeighList(dev.link.Index, syscall.AF_BRIDGE)
}