2015-02-22 17:58:52 -08:00
|
|
|
package bridge
|
|
|
|
|
2015-05-14 14:56:15 -07:00
|
|
|
import (
|
2016-05-16 11:51:40 -07:00
|
|
|
"fmt"
|
2016-06-28 11:42:50 +08:00
|
|
|
"strings"
|
2016-05-16 11:51:40 -07:00
|
|
|
|
2016-06-28 11:42:50 +08:00
|
|
|
"github.com/docker/libnetwork/ns"
|
2016-01-06 18:13:08 -08:00
|
|
|
"github.com/docker/libnetwork/types"
|
2017-07-26 14:18:31 -07:00
|
|
|
"github.com/sirupsen/logrus"
|
2015-05-14 14:56:15 -07:00
|
|
|
"github.com/vishvananda/netlink"
|
|
|
|
)
|
2015-02-22 17:58:52 -08:00
|
|
|
|
2015-05-22 10:56:36 -07:00
|
|
|
func setupVerifyAndReconcile(config *networkConfiguration, i *bridgeInterface) error {
|
2016-09-16 22:40:44 -07:00
|
|
|
// Fetch a slice of IPv4 addresses and a slice of IPv6 addresses from the bridge.
|
|
|
|
addrsv4, addrsv6, err := i.addresses()
|
2015-02-22 17:58:52 -08:00
|
|
|
if err != nil {
|
2016-05-16 11:51:40 -07:00
|
|
|
return fmt.Errorf("Failed to verify ip addresses: %v", err)
|
2015-02-22 17:58:52 -08:00
|
|
|
}
|
|
|
|
|
2016-09-16 22:40:44 -07:00
|
|
|
addrv4, _ := selectIPv4Address(addrsv4, config.AddressIPv4)
|
|
|
|
|
2015-02-24 18:41:17 -08:00
|
|
|
// Verify that the bridge does have an IPv4 address.
|
|
|
|
if addrv4.IPNet == nil {
|
2015-05-14 14:56:15 -07:00
|
|
|
return &ErrNoIPAddr{}
|
2015-02-24 18:41:17 -08:00
|
|
|
}
|
|
|
|
|
2015-02-22 17:58:52 -08: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 14:56:15 -07:00
|
|
|
return &IPv4AddrNoMatchError{IP: addrv4.IP, CfgIP: config.AddressIPv4.IP}
|
2015-02-22 17:58:52 -08: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-22 17:58:52 -08:00
|
|
|
}
|
|
|
|
|
2016-01-06 18:13:08 -08: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 11:51:40 -07:00
|
|
|
if err := i.nlh.AddrDel(i.Link, &addrv6); err != nil {
|
2016-10-31 22:26:14 -06:00
|
|
|
logrus.Warnf("Failed to remove residual IPv6 address %s from bridge: %v", addrv6.IPNet, err)
|
2016-01-06 18:13:08 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-04-16 05:02:21 +00:00
|
|
|
|
2015-02-24 18:41:17 -08: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-22 17:58:52 -08:00
|
|
|
}
|
2016-06-28 11:42:50 +08: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)
|
|
|
|
}
|