github.com/rish1988/moby@v25.0.2+incompatible/libnetwork/drivers/bridge/setup_verify_linux.go (about)

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