2015-02-22 17:58:52 -08:00
|
|
|
package bridge
|
|
|
|
|
2015-05-14 14:56:15 -07:00
|
|
|
import (
|
|
|
|
"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 {
|
2015-02-22 17:58:52 -08:00
|
|
|
// Fetch a single IPv4 and a slice of IPv6 addresses from the bridge.
|
2015-03-04 13:25:43 -08:00
|
|
|
addrv4, addrsv6, err := i.addresses()
|
2015-02-22 17:58:52 -08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2015-04-16 05:02:21 +00:00
|
|
|
// By this time we have either configured a new bridge with an IP address
|
|
|
|
// or made sure an existing bridge's IP matches the configuration
|
|
|
|
// Now is the time to cache these states in the bridgeInterface.
|
|
|
|
i.bridgeIPv4 = addrv4.IPNet
|
|
|
|
i.bridgeIPv6 = bridgeIPv6
|
|
|
|
|
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
|
|
|
}
|