setup_verify.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. package bridge
  2. import (
  3. "github.com/vishvananda/netlink"
  4. )
  5. func setupVerifyAndReconcile(config *NetworkConfiguration, i *bridgeInterface) error {
  6. // Fetch a single IPv4 and a slice of IPv6 addresses from the bridge.
  7. addrv4, addrsv6, err := i.addresses()
  8. if err != nil {
  9. return err
  10. }
  11. // Verify that the bridge does have an IPv4 address.
  12. if addrv4.IPNet == nil {
  13. return &ErrNoIPAddr{}
  14. }
  15. // Verify that the bridge IPv4 address matches the requested configuration.
  16. if config.AddressIPv4 != nil && !addrv4.IP.Equal(config.AddressIPv4.IP) {
  17. return &IPv4AddrNoMatchError{IP: addrv4.IP, CfgIP: config.AddressIPv4.IP}
  18. }
  19. // Verify that one of the bridge IPv6 addresses matches the requested
  20. // configuration.
  21. if config.EnableIPv6 && !findIPv6Address(netlink.Addr{IPNet: bridgeIPv6}, addrsv6) {
  22. return (*IPv6AddrNoMatchError)(bridgeIPv6)
  23. }
  24. // By this time we have either configured a new bridge with an IP address
  25. // or made sure an existing bridge's IP matches the configuration
  26. // Now is the time to cache these states in the bridgeInterface.
  27. i.bridgeIPv4 = addrv4.IPNet
  28. i.bridgeIPv6 = bridgeIPv6
  29. return nil
  30. }
  31. func findIPv6Address(addr netlink.Addr, addresses []netlink.Addr) bool {
  32. for _, addrv6 := range addresses {
  33. if addrv6.String() == addr.String() {
  34. return true
  35. }
  36. }
  37. return false
  38. }