2015-02-23 01:58:52 +00:00
|
|
|
package bridge
|
|
|
|
|
2015-05-14 21:56:15 +00:00
|
|
|
import (
|
2016-05-16 18:51:40 +00:00
|
|
|
"fmt"
|
2016-06-28 03:42:50 +00:00
|
|
|
"strings"
|
2016-05-16 18:51:40 +00:00
|
|
|
|
2016-11-01 04:26:14 +00:00
|
|
|
"github.com/Sirupsen/logrus"
|
2016-06-28 03:42:50 +00:00
|
|
|
"github.com/docker/libnetwork/ns"
|
2016-01-07 02:13:08 +00:00
|
|
|
"github.com/docker/libnetwork/types"
|
2015-05-14 21:56:15 +00:00
|
|
|
"github.com/vishvananda/netlink"
|
|
|
|
)
|
2015-02-23 01:58:52 +00:00
|
|
|
|
2015-05-22 17:56:36 +00:00
|
|
|
func setupVerifyAndReconcile(config *networkConfiguration, i *bridgeInterface) error {
|
2016-09-17 05:40:44 +00:00
|
|
|
// Fetch a slice of IPv4 addresses and a slice of IPv6 addresses from the bridge.
|
|
|
|
addrsv4, addrsv6, err := i.addresses()
|
2015-02-23 01:58:52 +00:00
|
|
|
if err != nil {
|
2016-05-16 18:51:40 +00:00
|
|
|
return fmt.Errorf("Failed to verify ip addresses: %v", err)
|
2015-02-23 01:58:52 +00:00
|
|
|
}
|
|
|
|
|
2016-09-17 05:40:44 +00:00
|
|
|
addrv4, _ := selectIPv4Address(addrsv4, config.AddressIPv4)
|
|
|
|
|
2015-02-25 02:41:17 +00:00
|
|
|
// Verify that the bridge does have an IPv4 address.
|
|
|
|
if addrv4.IPNet == nil {
|
2015-05-14 21:56:15 +00:00
|
|
|
return &ErrNoIPAddr{}
|
2015-02-25 02:41:17 +00:00
|
|
|
}
|
|
|
|
|
2015-02-23 01:58:52 +00:00
|
|
|
// Verify that the bridge IPv4 address matches the requested configuration.
|
2015-04-15 05:25:42 +00:00
|
|
|
if config.AddressIPv4 != nil && !addrv4.IP.Equal(config.AddressIPv4.IP) {
|
2015-05-14 21:56:15 +00:00
|
|
|
return &IPv4AddrNoMatchError{IP: addrv4.IP, CfgIP: config.AddressIPv4.IP}
|
2015-02-23 01:58:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Verify that one of the bridge IPv6 addresses matches the requested
|
|
|
|
// configuration.
|
2015-04-15 05:25:42 +00:00
|
|
|
if config.EnableIPv6 && !findIPv6Address(netlink.Addr{IPNet: bridgeIPv6}, addrsv6) {
|
2015-04-17 02:47:12 +00:00
|
|
|
return (*IPv6AddrNoMatchError)(bridgeIPv6)
|
2015-02-23 01:58:52 +00:00
|
|
|
}
|
|
|
|
|
2016-01-07 02:13:08 +00:00
|
|
|
// Release any residual IPv6 address that might be there because of older daemon instances
|
|
|
|
for _, addrv6 := range addrsv6 {
|
|
|
|
if addrv6.IP.IsGlobalUnicast() && !types.CompareIPNet(addrv6.IPNet, i.bridgeIPv6) {
|
2016-05-16 18:51:40 +00:00
|
|
|
if err := i.nlh.AddrDel(i.Link, &addrv6); err != nil {
|
2016-11-01 04:26:14 +00:00
|
|
|
logrus.Warnf("Failed to remove residual IPv6 address %s from bridge: %v", addrv6.IPNet, err)
|
2016-01-07 02:13:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-04-16 05:02:21 +00:00
|
|
|
|
2015-02-25 02:41:17 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func findIPv6Address(addr netlink.Addr, addresses []netlink.Addr) bool {
|
|
|
|
for _, addrv6 := range addresses {
|
|
|
|
if addrv6.String() == addr.String() {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
2015-02-23 01:58:52 +00:00
|
|
|
}
|
2016-06-28 03:42:50 +00:00
|
|
|
|
|
|
|
func bridgeInterfaceExists(name string) (bool, error) {
|
|
|
|
nlh := ns.NlHandle()
|
|
|
|
link, err := nlh.LinkByName(name)
|
|
|
|
if err != nil {
|
|
|
|
if strings.Contains(err.Error(), "Link not found") {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
return false, fmt.Errorf("failed to check bridge interface existence: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if link.Type() == "bridge" {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
return false, fmt.Errorf("existing interface %s is not a bridge", name)
|
|
|
|
}
|