func electBridgeIPv4(config *NetworkConfiguration) (*net.IPNet, error) { // Use the requested IPv4 CIDR when available. if config.AddressIPv4 != nil { return config.AddressIPv4, nil } // We don't check for an error here, because we don't really care if we // can't read /etc/resolv.conf. So instead we skip the append if resolvConf // is nil. It either doesn't exist, or we can't read it for some reason. nameservers := []string{} if resolvConf, _ := readResolvConf(); resolvConf != nil { nameservers = append(nameservers, getNameserversAsCIDR(resolvConf)...) } // Try to automatically elect appropriate bridge IPv4 settings. for _, n := range bridgeNetworks { if err := netutils.CheckNameserverOverlaps(nameservers, n); err == nil { if err := netutils.CheckRouteOverlaps(n); err == nil { return n, nil } } } return nil, IPv4AddrRangeError(config.BridgeName) }
func TestSetDefaultGw(t *testing.T) { defer testutils.SetupTestOSContext(t)() d := newDriver() if err := d.configure(nil); err != nil { t.Fatalf("Failed to setup driver config: %v", err) } _, subnetv6, _ := net.ParseCIDR("2001:db8:ea9:9abc:b0c4::/80") var nw *net.IPNet for _, n := range bridgeNetworks { if err := netutils.CheckRouteOverlaps(n); err == nil { nw = n break } } if nw == nil { t.Skipf("Skip as no more automatic networks available") } gw4 := types.GetIPCopy(nw.IP).To4() gw4[3] = 254 gw6 := net.ParseIP("2001:db8:ea9:9abc:b0c4::254") config := &networkConfiguration{ BridgeName: DefaultBridgeName, EnableIPv6: true, FixedCIDRv6: subnetv6, DefaultGatewayIPv4: gw4, DefaultGatewayIPv6: gw6, } genericOption := make(map[string]interface{}) genericOption[netlabel.GenericData] = config err := d.CreateNetwork("dummy", genericOption) if err != nil { t.Fatalf("Failed to create bridge: %v", err) } te := &testEndpoint{} err = d.CreateEndpoint("dummy", "ep", te, nil) if err != nil { t.Fatalf("Failed to create endpoint: %v", err) } err = d.Join("dummy", "ep", "sbox", te, nil) if err != nil { t.Fatalf("Failed to join endpoint: %v", err) } if !gw4.Equal(te.gw) { t.Fatalf("Failed to configure default gateway. Expected %v. Found %v", gw4, te.gw) } if !gw6.Equal(te.gw6) { t.Fatalf("Failed to configure default gateway. Expected %v. Found %v", gw6, te.gw6) } }
func getFirstAvailablePool(a *Allocator, as string, atLeast int) (int, *net.IPNet, error) { i := 0 for i < len(a.predefined[as])-1 { if err := netutils.CheckRouteOverlaps(a.predefined[as][i]); err == nil { break } i++ } if i > len(a.predefined[as])-1-atLeast { return 0, nil, fmt.Errorf("Not enough non-overlapping networks to run the test") } return i, a.predefined[as][i], nil }
// FindAvailableNetwork returns a network from the passed list which does not // overlap with existing interfaces in the system func FindAvailableNetwork(list []*net.IPNet) (*net.IPNet, error) { // We don't check for an error here, because we don't really care if we // can't read /etc/resolv.conf. So instead we skip the append if resolvConf // is nil. It either doesn't exist, or we can't read it for some reason. var nameservers []string if rc, err := resolvconf.Get(); err == nil { nameservers = resolvconf.GetNameserversAsCIDR(rc.Content) } for _, nw := range list { if err := netutils.CheckNameserverOverlaps(nameservers, nw); err == nil { if err := netutils.CheckRouteOverlaps(nw); err == nil { return nw, nil } } } return nil, fmt.Errorf("no available network") }
func isOverlap(nw *net.IPNet) bool { var nameservers []string if rc, err := resolvconf.Get(); err == nil { nameservers = resolvconf.GetNameserversAsCIDR(rc.Content) } if err := netutils.CheckNameserverOverlaps(nameservers, nw); err != nil { return true } if err := netutils.CheckRouteOverlaps(nw); err != nil { return true } return false }
func checkOverlap(nw *net.IPNet) error { var nameservers []string if rc, err := resolvconf.Get(); err == nil { nameservers = resolvconf.GetNameserversAsCIDR(rc.Content) } if err := netutils.CheckNameserverOverlaps(nameservers, nw); err != nil { return fmt.Errorf("overlay subnet %s failed check with nameserver: %v: %v", nw.String(), nameservers, err) } if err := netutils.CheckRouteOverlaps(nw); err != nil { return fmt.Errorf("overlay subnet %s failed check with host route table: %v", nw.String(), err) } return nil }
func (a *Allocator) getPredefinedPool(as string, ipV6 bool) (*net.IPNet, error) { var v ipVersion v = v4 if ipV6 { v = v6 } if as != localAddressSpace && as != globalAddressSpace { return nil, fmt.Errorf("no default pool availbale for non-default addresss spaces") } cfg, err := a.getPoolsConfig(as) if err != nil { return nil, err } for _, nw := range a.getPredefineds(as) { if v != getAddressVersion(nw.IP) { continue } cfg.Lock() _, ok := cfg.subnets[SubnetKey{AddressSpace: as, Subnet: nw.String()}] cfg.Unlock() if ok { continue } if !cfg.contains(as, nw) { if as == localAddressSpace { if err := netutils.CheckRouteOverlaps(nw); err == nil { return nw, nil } continue } return nw, nil } } return nil, types.NotFoundErrorf("could not find an available predefined network") }
func TestSetupBridgeIPv4Auto(t *testing.T) { defer osl.SetupTestOSContext(t)() var toBeChosen *net.IPNet for _, n := range bridgeNetworks { if err := netutils.CheckRouteOverlaps(n); err == nil { toBeChosen = n break } } if toBeChosen == nil { t.Skipf("Skip as no more automatic networks available") } config, br := setupTestInterface(t) if err := setupBridgeIPv4(config, br); err != nil { t.Fatalf("Failed to setup bridge IPv4: %v", err) } addrsv4, err := netlink.AddrList(br.Link, netlink.FAMILY_V4) if err != nil { t.Fatalf("Failed to list device IPv4 addresses: %v", err) } var found bool for _, addr := range addrsv4 { if toBeChosen.String() == addr.IPNet.String() { found = true break } } if !found { t.Fatalf("Bridge device does not have the automatic IPv4 address %s", toBeChosen.String()) } }