setup_verify_linux.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. package bridge
  2. import (
  3. "fmt"
  4. "strings"
  5. "github.com/docker/docker/libnetwork/ns"
  6. "github.com/vishvananda/netlink"
  7. )
  8. // setupVerifyAndReconcileIPv4 checks what IPv4 addresses the given i interface has
  9. // and ensures that they match the passed network config.
  10. func setupVerifyAndReconcileIPv4(config *networkConfiguration, i *bridgeInterface) error {
  11. // Fetch a slice of IPv4 addresses and a slice of IPv6 addresses from the bridge.
  12. addrsv4, err := i.addresses(netlink.FAMILY_V4)
  13. if err != nil {
  14. return fmt.Errorf("Failed to verify ip addresses: %v", err)
  15. }
  16. addrv4, _ := selectIPv4Address(addrsv4, config.AddressIPv4)
  17. // Verify that the bridge has an IPv4 address.
  18. if !config.Internal && addrv4.IPNet == nil {
  19. return &ErrNoIPAddr{}
  20. }
  21. // Verify that the bridge IPv4 address matches the requested configuration.
  22. if config.AddressIPv4 != nil && addrv4.IPNet != nil && !addrv4.IP.Equal(config.AddressIPv4.IP) {
  23. return &IPv4AddrNoMatchError{IP: addrv4.IP, CfgIP: config.AddressIPv4.IP}
  24. }
  25. return nil
  26. }
  27. func bridgeInterfaceExists(name string) (bool, error) {
  28. nlh := ns.NlHandle()
  29. link, err := nlh.LinkByName(name)
  30. if err != nil {
  31. if strings.Contains(err.Error(), "Link not found") {
  32. return false, nil
  33. }
  34. return false, fmt.Errorf("failed to check bridge interface existence: %v", err)
  35. }
  36. if link.Type() == "bridge" {
  37. return true, nil
  38. }
  39. return false, fmt.Errorf("existing interface %s is not a bridge", name)
  40. }