func (c *networkConfiguration) processIPAM(id string, ipamV4Data, ipamV6Data []driverapi.IPAMData) error { if len(ipamV4Data) > 1 || len(ipamV6Data) > 1 { return types.ForbiddenErrorf("bridge driver doesn't support multiple subnets") } if len(ipamV4Data) == 0 { return types.BadRequestErrorf("bridge network %s requires ipv4 configuration", id) } if ipamV4Data[0].Gateway != nil { c.AddressIPv4 = types.GetIPNetCopy(ipamV4Data[0].Gateway) } if gw, ok := ipamV4Data[0].AuxAddresses[DefaultGatewayV4AuxKey]; ok { c.DefaultGatewayIPv4 = gw.IP } if len(ipamV6Data) > 0 { c.AddressIPv6 = ipamV6Data[0].Pool if ipamV6Data[0].Gateway != nil { c.AddressIPv6 = types.GetIPNetCopy(ipamV6Data[0].Gateway) } if gw, ok := ipamV6Data[0].AuxAddresses[DefaultGatewayV6AuxKey]; ok { c.DefaultGatewayIPv6 = gw.IP } } return nil }
// GetCopy returns a copy of this Interface structure func (i *Interface) GetCopy() *Interface { return &Interface{ SrcName: i.SrcName, DstName: i.DstName, Address: types.GetIPNetCopy(i.Address), AddressIPv6: types.GetIPNetCopy(i.AddressIPv6), } }
// CopyTo deep copies the pool data to the destination pooldata func (p *PoolData) CopyTo(dstP *PoolData) error { dstP.ParentKey = p.ParentKey dstP.Pool = types.GetIPNetCopy(p.Pool) if p.Range != nil { dstP.Range = &AddressRange{} dstP.Range.Sub = types.GetIPNetCopy(p.Range.Sub) dstP.Range.Start = p.Range.Start dstP.Range.End = p.Range.End } dstP.RefCount = p.RefCount return nil }
func (epi *endpointInterface) CopyTo(dstEpi *endpointInterface) error { dstEpi.mac = types.GetMacCopy(epi.mac) dstEpi.addr = types.GetIPNetCopy(epi.addr) dstEpi.addrv6 = types.GetIPNetCopy(epi.addrv6) dstEpi.srcName = epi.srcName dstEpi.dstPrefix = epi.dstPrefix dstEpi.poolID = epi.poolID for _, route := range epi.routes { dstEpi.routes = append(dstEpi.routes, types.GetIPNetCopy(route)) } return nil }
func setAddress(ifaceAddr **net.IPNet, address *net.IPNet) error { if *ifaceAddr != nil { return types.ForbiddenErrorf("endpoint interface IP present (%s). Cannot be modified with (%s).", *ifaceAddr, address) } *ifaceAddr = types.GetIPNetCopy(address) return nil }
func (a *Allocator) getAddress(nw *net.IPNet, bitmask *bitseq.Handle, prefAddress net.IP, ipr *AddressRange) (net.IP, error) { var ( ordinal uint64 err error base *net.IPNet ) base = types.GetIPNetCopy(nw) if bitmask.Unselected() <= 0 { return nil, ipamapi.ErrNoAvailableIPs } if ipr == nil && prefAddress == nil { ordinal, err = bitmask.SetAny() } else if prefAddress != nil { hostPart, e := types.GetHostPartIP(prefAddress, base.Mask) if e != nil { return nil, fmt.Errorf("failed to allocate preferred address %s: %v", prefAddress.String(), e) } ordinal = ipToUint64(types.GetMinimalIP(hostPart)) err = bitmask.Set(ordinal) } else { base.IP = ipr.Sub.IP ordinal, err = bitmask.SetAnyInRange(ipr.Start, ipr.End) } if err != nil { return nil, ipamapi.ErrNoAvailableIPs } // Convert IP ordinal for this subnet into IP address return generateAddress(ordinal, base), nil }
func (epi *endpointInterface) IPAliases() []*net.IPNet { var aliases []*net.IPNet for _, alias := range epi.ipAliases { aliases = append(aliases, types.GetIPNetCopy(alias)) } return aliases }
func (epi *endpointInterface) SetIPAliases(aliases []*net.IPNet) error { if epi.ipAliases != nil { return types.ForbiddenErrorf("endpoint aliases present. Cannot be modified with (%s).", aliases) } epi.ipAliases = make([]*net.IPNet, 0) for _, alias := range aliases { epi.ipAliases = append(epi.ipAliases, types.GetIPNetCopy(alias)) } return nil }
func (i *nwIface) IPAliases() []*net.IPNet { i.Lock() defer i.Unlock() aliases := make([]*net.IPNet, len(i.aliases)) for index, alias := range i.aliases { ipa := types.GetIPNetCopy(alias) aliases[index] = ipa } return aliases }
func getIPv4Data(t *testing.T) []driverapi.IPAMData { ipd := driverapi.IPAMData{AddressSpace: "full"} nw, _, err := ipamutils.ElectInterfaceAddresses("") if err != nil { t.Fatal(err) } ipd.Pool = nw // Set network gateway to X.X.X.1 ipd.Gateway = types.GetIPNetCopy(nw) ipd.Gateway.IP[len(ipd.Gateway.IP)-1] = 1 return []driverapi.IPAMData{ipd} }
func (epi *endpointInterface) CopyTo(dstEpi *endpointInterface) error { dstEpi.mac = types.GetMacCopy(epi.mac) dstEpi.addr = types.GetIPNetCopy(epi.addr) dstEpi.addrv6 = types.GetIPNetCopy(epi.addrv6) dstEpi.srcName = epi.srcName dstEpi.dstPrefix = epi.dstPrefix dstEpi.v4PoolID = epi.v4PoolID dstEpi.v6PoolID = epi.v6PoolID if len(epi.llAddrs) != 0 { dstEpi.llAddrs = make([]*net.IPNet, 0, len(epi.llAddrs)) for _, ll := range epi.llAddrs { dstEpi.llAddrs = append(dstEpi.llAddrs, ll) } } for _, route := range epi.routes { dstEpi.routes = append(dstEpi.routes, types.GetIPNetCopy(route)) } return nil }
func (i *nwIface) Routes() []*net.IPNet { i.Lock() defer i.Unlock() routes := make([]*net.IPNet, len(i.routes)) for index, route := range i.routes { r := types.GetIPNetCopy(route) routes[index] = r } return routes }
// CopyTo deep copies to the destination IpamInfo func (i *IpamInfo) CopyTo(dstI *IpamInfo) error { dstI.PoolID = i.PoolID if i.Meta != nil { dstI.Meta = make(map[string]string) for k, v := range i.Meta { dstI.Meta[k] = v } } dstI.AddressSpace = i.AddressSpace dstI.Pool = types.GetIPNetCopy(i.Pool) dstI.Gateway = types.GetIPNetCopy(i.Gateway) if i.AuxAddresses != nil { dstI.AuxAddresses = make(map[string]*net.IPNet) for k, v := range i.AuxAddresses { dstI.AuxAddresses[k] = types.GetIPNetCopy(v) } } return nil }
func (ep *endpoint) SetIPAddress(address *net.IPNet) error { if address.IP == nil { return types.BadRequestErrorf("tried to set nil IP address to endpoint interface") } if address.IP.To4() == nil { return types.NotImplementedErrorf("do not support ipv6 yet") } if ep.addr != nil { return types.ForbiddenErrorf("endpoint interface IP present (%s). Cannot be modified with %s.", ep.addr, address) } ep.addr = types.GetIPNetCopy(address) return nil }
func verifyV4INCEntries(networks map[string]*bridgeNetwork, numEntries int, t *testing.T) { out, err := iptables.Raw("-L", "FORWARD") if err != nil { t.Fatal(err) } for _, nw := range networks { nt := types.GetIPNetCopy(nw.bridge.bridgeIPv4) nt.IP = nt.IP.Mask(nt.Mask) re := regexp.MustCompile(nt.String()) matches := re.FindAllString(string(out[:]), -1) if len(matches) != numEntries { t.Fatalf("Cannot find expected inter-network isolation rules in IP Tables:\n%s", string(out[:])) } } }
func (a *Allocator) getAddress(nw *net.IPNet, bitmask *bitseq.Handle, prefAddress net.IP, ipr *AddressRange) (net.IP, error) { var ( ordinal uint64 err error base *net.IPNet ) base = types.GetIPNetCopy(nw) if bitmask.Unselected() <= 0 { return nil, ipamapi.ErrNoAvailableIPs } if ipr == nil && prefAddress == nil { ordinal, err = bitmask.SetAny() } else if prefAddress != nil { hostPart, e := types.GetHostPartIP(prefAddress, base.Mask) if e != nil { return nil, types.InternalErrorf("failed to allocate requested address %s: %v", prefAddress.String(), e) } ordinal = ipToUint64(types.GetMinimalIP(hostPart)) err = bitmask.Set(ordinal) } else { ordinal, err = bitmask.SetAnyInRange(ipr.Start, ipr.End) } switch err { case nil: // Convert IP ordinal for this subnet into IP address return generateAddress(ordinal, base), nil case bitseq.ErrBitAllocated: return nil, ipamapi.ErrIPAlreadyAllocated case bitseq.ErrNoBitAvailable: return nil, ipamapi.ErrNoAvailableIPs default: return nil, err } }
func newTestEndpoint(nw *net.IPNet, ordinal byte) *testEndpoint { addr := types.GetIPNetCopy(nw) addr.IP[len(addr.IP)-1] = ordinal return &testEndpoint{iface: &testInterface{addr: addr}} }
func (epi *endpointInterface) AddressIPv6() *net.IPNet { return types.GetIPNetCopy(epi.addrv6) }
func (ep *endpoint) Address() *net.IPNet { return types.GetIPNetCopy(ep.addr) }
func (i *nwIface) AddressIPv6() *net.IPNet { i.Lock() defer i.Unlock() return types.GetIPNetCopy(i.addressIPv6) }