|
@@ -35,6 +35,13 @@ const (
|
|
|
maxAllocatePortAttempts = 10
|
|
|
)
|
|
|
|
|
|
+const (
|
|
|
+ // DefaultGatewayV4AuxKey represents the default-gateway configured by the user
|
|
|
+ DefaultGatewayV4AuxKey = "DefaultGatewayIPv4"
|
|
|
+ // DefaultGatewayV6AuxKey represents the ipv6 default-gateway configured by the user
|
|
|
+ DefaultGatewayV6AuxKey = "DefaultGatewayIPv6"
|
|
|
+)
|
|
|
+
|
|
|
var (
|
|
|
ipAllocator *ipallocator.IPAllocator
|
|
|
)
|
|
@@ -50,8 +57,6 @@ type configuration struct {
|
|
|
type networkConfiguration struct {
|
|
|
BridgeName string
|
|
|
AddressIPv4 *net.IPNet
|
|
|
- FixedCIDR *net.IPNet
|
|
|
- FixedCIDRv6 *net.IPNet
|
|
|
EnableIPv6 bool
|
|
|
EnableIPMasquerade bool
|
|
|
EnableICC bool
|
|
@@ -148,19 +153,6 @@ func (c *networkConfiguration) Validate() error {
|
|
|
|
|
|
// If bridge v4 subnet is specified
|
|
|
if c.AddressIPv4 != nil {
|
|
|
- // If Container restricted subnet is specified, it must be a subset of bridge subnet
|
|
|
- if c.FixedCIDR != nil {
|
|
|
- // Check Network address
|
|
|
- if !c.AddressIPv4.Contains(c.FixedCIDR.IP) {
|
|
|
- return &ErrInvalidContainerSubnet{}
|
|
|
- }
|
|
|
- // Check it is effectively a subset
|
|
|
- brNetLen, _ := c.AddressIPv4.Mask.Size()
|
|
|
- cnNetLen, _ := c.FixedCIDR.Mask.Size()
|
|
|
- if brNetLen > cnNetLen {
|
|
|
- return &ErrInvalidContainerSubnet{}
|
|
|
- }
|
|
|
- }
|
|
|
// If default gw is specified, it must be part of bridge subnet
|
|
|
if c.DefaultGatewayIPv4 != nil {
|
|
|
if !c.AddressIPv4.Contains(c.DefaultGatewayIPv4) {
|
|
@@ -169,13 +161,6 @@ func (c *networkConfiguration) Validate() error {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- // If default v6 gw is specified, FixedCIDRv6 must be specified and gw must belong to FixedCIDRv6 subnet
|
|
|
- if c.EnableIPv6 && c.DefaultGatewayIPv6 != nil {
|
|
|
- if c.FixedCIDRv6 == nil || !c.FixedCIDRv6.Contains(c.DefaultGatewayIPv6) {
|
|
|
- return &ErrInvalidGateway{}
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
return nil
|
|
|
}
|
|
|
|
|
@@ -259,65 +244,6 @@ func (c *networkConfiguration) fromMap(data map[string]interface{}) error {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- if i, ok := data["AddressIPv4"]; ok && i != nil {
|
|
|
- if s, ok := i.(string); ok {
|
|
|
- if ip, nw, e := net.ParseCIDR(s); e == nil {
|
|
|
- nw.IP = ip
|
|
|
- c.AddressIPv4 = nw
|
|
|
- } else {
|
|
|
- return types.BadRequestErrorf("failed to parse AddressIPv4 value")
|
|
|
- }
|
|
|
- } else {
|
|
|
- return types.BadRequestErrorf("invalid type for AddressIPv4 value")
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- if i, ok := data["FixedCIDR"]; ok && i != nil {
|
|
|
- if s, ok := i.(string); ok {
|
|
|
- if ip, nw, e := net.ParseCIDR(s); e == nil {
|
|
|
- nw.IP = ip
|
|
|
- c.FixedCIDR = nw
|
|
|
- } else {
|
|
|
- return types.BadRequestErrorf("failed to parse FixedCIDR value")
|
|
|
- }
|
|
|
- } else {
|
|
|
- return types.BadRequestErrorf("invalid type for FixedCIDR value")
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- if i, ok := data["FixedCIDRv6"]; ok && i != nil {
|
|
|
- if s, ok := i.(string); ok {
|
|
|
- if ip, nw, e := net.ParseCIDR(s); e == nil {
|
|
|
- nw.IP = ip
|
|
|
- c.FixedCIDRv6 = nw
|
|
|
- } else {
|
|
|
- return types.BadRequestErrorf("failed to parse FixedCIDRv6 value")
|
|
|
- }
|
|
|
- } else {
|
|
|
- return types.BadRequestErrorf("invalid type for FixedCIDRv6 value")
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- if i, ok := data["DefaultGatewayIPv4"]; ok && i != nil {
|
|
|
- if s, ok := i.(string); ok {
|
|
|
- if c.DefaultGatewayIPv4 = net.ParseIP(s); c.DefaultGatewayIPv4 == nil {
|
|
|
- return types.BadRequestErrorf("failed to parse DefaultGatewayIPv4 value")
|
|
|
- }
|
|
|
- } else {
|
|
|
- return types.BadRequestErrorf("invalid type for DefaultGatewayIPv4 value")
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- if i, ok := data["DefaultGatewayIPv6"]; ok && i != nil {
|
|
|
- if s, ok := i.(string); ok {
|
|
|
- if c.DefaultGatewayIPv6 = net.ParseIP(s); c.DefaultGatewayIPv6 == nil {
|
|
|
- return types.BadRequestErrorf("failed to parse DefaultGatewayIPv6 value")
|
|
|
- }
|
|
|
- } else {
|
|
|
- return types.BadRequestErrorf("invalid type for DefaultGatewayIPv6 value")
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
if i, ok := data["DefaultBindingIP"]; ok && i != nil {
|
|
|
if s, ok := i.(string); ok {
|
|
|
if c.DefaultBindingIP = net.ParseIP(s); c.DefaultBindingIP == nil {
|
|
@@ -327,6 +253,7 @@ func (c *networkConfiguration) fromMap(data map[string]interface{}) error {
|
|
|
return types.BadRequestErrorf("invalid type for DefaultBindingIP value")
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
return nil
|
|
|
}
|
|
|
|
|
@@ -516,6 +443,36 @@ func parseNetworkGenericOptions(data interface{}) (*networkConfiguration, error)
|
|
|
return config, err
|
|
|
}
|
|
|
|
|
|
+func (c *networkConfiguration) processIPAM(id string, ipamV4Data, ipamV6Data []driverapi.IPAMData) error {
|
|
|
+ if len(ipamV4Data) > 1 || len(ipamV6Data) > 1 {
|
|
|
+ return types.ForbiddenErrorf("bridge driver doesnt 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 c.EnableIPv6 && len(ipamV6Data) == 0 {
|
|
|
+ return types.BadRequestErrorf("bridge network %s requires ipv6 configuration", id)
|
|
|
+ }
|
|
|
+
|
|
|
+ gw, ok := ipamV4Data[0].AuxAddresses[DefaultGatewayV4AuxKey]
|
|
|
+ if ok {
|
|
|
+ c.DefaultGatewayIPv4 = gw.IP
|
|
|
+ }
|
|
|
+
|
|
|
+ gw, ok = ipamV4Data[0].AuxAddresses[DefaultGatewayV6AuxKey]
|
|
|
+ if ok {
|
|
|
+ c.DefaultGatewayIPv6 = gw.IP
|
|
|
+ }
|
|
|
+
|
|
|
+ return nil
|
|
|
+}
|
|
|
+
|
|
|
func parseNetworkOptions(id string, option options.Generic) (*networkConfiguration, error) {
|
|
|
var err error
|
|
|
config := &networkConfiguration{}
|
|
@@ -545,10 +502,6 @@ func parseNetworkOptions(id string, option options.Generic) (*networkConfigurati
|
|
|
|
|
|
// Returns the non link-local IPv6 subnet for the containers attached to this bridge if found, nil otherwise
|
|
|
func getV6Network(config *networkConfiguration, i *bridgeInterface) *net.IPNet {
|
|
|
- if config.FixedCIDRv6 != nil {
|
|
|
- return config.FixedCIDRv6
|
|
|
- }
|
|
|
-
|
|
|
if i.bridgeIPv6 != nil && i.bridgeIPv6.IP != nil && !i.bridgeIPv6.IP.IsLinkLocalUnicast() {
|
|
|
return i.bridgeIPv6
|
|
|
}
|
|
@@ -587,6 +540,12 @@ func (d *driver) CreateNetwork(id string, option map[string]interface{}, ipV4Dat
|
|
|
if err != nil {
|
|
|
return err
|
|
|
}
|
|
|
+
|
|
|
+ err = config.processIPAM(id, ipV4Data, ipV6Data)
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+
|
|
|
networkList := d.getNetworks()
|
|
|
for _, nw := range networkList {
|
|
|
nw.Lock()
|
|
@@ -655,7 +614,7 @@ func (d *driver) CreateNetwork(id string, option map[string]interface{}, ipV4Dat
|
|
|
bridgeSetup.queueStep(setupBridgeIPv4)
|
|
|
|
|
|
enableIPv6Forwarding := false
|
|
|
- if d.config.EnableIPForwarding && config.FixedCIDRv6 != nil {
|
|
|
+ if d.config.EnableIPForwarding {
|
|
|
enableIPv6Forwarding = true
|
|
|
}
|
|
|
|
|
@@ -674,14 +633,6 @@ func (d *driver) CreateNetwork(id string, option map[string]interface{}, ipV4Dat
|
|
|
// the case of a previously existing device.
|
|
|
{bridgeAlreadyExists, setupVerifyAndReconcile},
|
|
|
|
|
|
- // Setup the bridge to allocate containers IPv4 addresses in the
|
|
|
- // specified subnet.
|
|
|
- {config.FixedCIDR != nil, setupFixedCIDRv4},
|
|
|
-
|
|
|
- // Setup the bridge to allocate containers global IPv6 addresses in the
|
|
|
- // specified subnet.
|
|
|
- {config.FixedCIDRv6 != nil, setupFixedCIDRv6},
|
|
|
-
|
|
|
// Enable IPv6 Forwarding
|
|
|
{enableIPv6Forwarding, setupIPv6Forwarding},
|
|
|
|
|
@@ -712,8 +663,6 @@ func (d *driver) CreateNetwork(id string, option map[string]interface{}, ipV4Dat
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- // Block bridge IP from being allocated.
|
|
|
- bridgeSetup.queueStep(allocateBridgeIP)
|
|
|
// Apply the prepared list of steps, and abort at the first error.
|
|
|
bridgeSetup.queueStep(setupDeviceUp)
|
|
|
if err = bridgeSetup.apply(); err != nil {
|
|
@@ -790,21 +739,7 @@ func (d *driver) DeleteNetwork(nid string) error {
|
|
|
}
|
|
|
|
|
|
// Programming
|
|
|
- err = netlink.LinkDel(n.bridge.Link)
|
|
|
-
|
|
|
- // Release ip addresses (ignore errors)
|
|
|
- if config.FixedCIDR == nil || config.FixedCIDR.Contains(config.DefaultGatewayIPv4) {
|
|
|
- if e := ipAllocator.ReleaseIP(n.bridge.bridgeIPv4, n.bridge.gatewayIPv4); e != nil {
|
|
|
- logrus.Warnf("Failed to release default gateway address %s: %v", n.bridge.gatewayIPv4.String(), e)
|
|
|
- }
|
|
|
- }
|
|
|
- if config.FixedCIDR == nil || config.FixedCIDR.Contains(n.bridge.bridgeIPv4.IP) {
|
|
|
- if e := ipAllocator.ReleaseIP(n.bridge.bridgeIPv4, n.bridge.bridgeIPv4.IP); e != nil {
|
|
|
- logrus.Warnf("Failed to release bridge IP %s: %v", n.bridge.bridgeIPv4.IP.String(), e)
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- return err
|
|
|
+ return netlink.LinkDel(n.bridge.Link)
|
|
|
}
|
|
|
|
|
|
func addToBridge(ifaceName, bridgeName string) error {
|
|
@@ -996,12 +931,7 @@ func (d *driver) CreateEndpoint(nid, eid string, ifInfo driverapi.InterfaceInfo,
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- // v4 address for the sandbox side pipe interface
|
|
|
- ip4, err := ipAllocator.RequestIP(n.bridge.bridgeIPv4, nil)
|
|
|
- if err != nil {
|
|
|
- return err
|
|
|
- }
|
|
|
- ipv4Addr := &net.IPNet{IP: ip4, Mask: n.bridge.bridgeIPv4.Mask}
|
|
|
+ ipv4Addr := ifInfo.Address()
|
|
|
|
|
|
// Down the interface before configuring mac address.
|
|
|
if err = netlink.LinkSetDown(sbox); err != nil {
|
|
@@ -1009,7 +939,10 @@ func (d *driver) CreateEndpoint(nid, eid string, ifInfo driverapi.InterfaceInfo,
|
|
|
}
|
|
|
|
|
|
// Set the sbox's MAC. If specified, use the one configured by user, otherwise generate one based on IP.
|
|
|
- mac := electMacAddress(epConfig, ip4)
|
|
|
+ mac := ifInfo.MacAddress()
|
|
|
+ if mac == nil {
|
|
|
+ mac = electMacAddress(epConfig, ipv4Addr.IP)
|
|
|
+ }
|
|
|
err = netlink.LinkSetHardwareAddr(sbox, mac)
|
|
|
if err != nil {
|
|
|
return fmt.Errorf("could not set mac address for container interface %s: %v", containerIfName, err)
|
|
@@ -1021,33 +954,7 @@ func (d *driver) CreateEndpoint(nid, eid string, ifInfo driverapi.InterfaceInfo,
|
|
|
return fmt.Errorf("could not set link up for host interface %s: %v", hostIfName, err)
|
|
|
}
|
|
|
|
|
|
- // v6 address for the sandbox side pipe interface
|
|
|
- ipv6Addr = &net.IPNet{}
|
|
|
- if config.EnableIPv6 {
|
|
|
- var ip6 net.IP
|
|
|
-
|
|
|
- network := n.bridge.bridgeIPv6
|
|
|
- if config.FixedCIDRv6 != nil {
|
|
|
- network = config.FixedCIDRv6
|
|
|
- }
|
|
|
-
|
|
|
- ones, _ := network.Mask.Size()
|
|
|
- if ones <= 80 {
|
|
|
- ip6 = make(net.IP, len(network.IP))
|
|
|
- copy(ip6, network.IP)
|
|
|
- for i, h := range mac {
|
|
|
- ip6[i+10] = h
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- ip6, err := ipAllocator.RequestIP(network, ip6)
|
|
|
- if err != nil {
|
|
|
- return err
|
|
|
- }
|
|
|
-
|
|
|
- ipv6Addr = &net.IPNet{IP: ip6, Mask: network.Mask}
|
|
|
- }
|
|
|
-
|
|
|
+ ipv6Addr = ifInfo.AddressIPv6()
|
|
|
// Create the sandbox side pipe interface
|
|
|
endpoint.srcName = containerIfName
|
|
|
endpoint.addr = ipv4Addr
|
|
@@ -1062,16 +969,8 @@ func (d *driver) CreateEndpoint(nid, eid string, ifInfo driverapi.InterfaceInfo,
|
|
|
return err
|
|
|
}
|
|
|
|
|
|
- err = ifInfo.SetMacAddress(endpoint.macAddress)
|
|
|
- if err != nil {
|
|
|
- return err
|
|
|
- }
|
|
|
- err = ifInfo.SetIPAddress(ipv4Addr)
|
|
|
- if err != nil {
|
|
|
- return err
|
|
|
- }
|
|
|
- if config.EnableIPv6 {
|
|
|
- err = ifInfo.SetIPAddress(ipv6Addr)
|
|
|
+ if ifInfo.MacAddress() == nil {
|
|
|
+ err = ifInfo.SetMacAddress(endpoint.macAddress)
|
|
|
if err != nil {
|
|
|
return err
|
|
|
}
|
|
@@ -1140,22 +1039,6 @@ func (d *driver) DeleteEndpoint(nid, eid string) error {
|
|
|
return err
|
|
|
}
|
|
|
|
|
|
- n.Lock()
|
|
|
- config := n.config
|
|
|
- n.Unlock()
|
|
|
-
|
|
|
- // Release the v6 address allocated to this endpoint's sandbox interface
|
|
|
- if config.EnableIPv6 {
|
|
|
- network := n.bridge.bridgeIPv6
|
|
|
- if config.FixedCIDRv6 != nil {
|
|
|
- network = config.FixedCIDRv6
|
|
|
- }
|
|
|
- err := ipAllocator.ReleaseIP(network, ep.addrv6.IP)
|
|
|
- if err != nil {
|
|
|
- return err
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
// Try removal of link. Discard error: link pair might have
|
|
|
// already been deleted by sandbox delete. Make sure defer
|
|
|
// does not see this error either.
|