github.com/pwn-term/docker@v0.0.0-20210616085119-6e977cce2565/libnetwork/drivers/ipvlan/ipvlan_endpoint.go (about)

     1  package ipvlan
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/docker/libnetwork/driverapi"
     7  	"github.com/docker/libnetwork/netlabel"
     8  	"github.com/docker/libnetwork/ns"
     9  	"github.com/docker/libnetwork/osl"
    10  	"github.com/docker/libnetwork/types"
    11  	"github.com/sirupsen/logrus"
    12  )
    13  
    14  // CreateEndpoint assigns the mac, ip and endpoint id for the new container
    15  func (d *driver) CreateEndpoint(nid, eid string, ifInfo driverapi.InterfaceInfo,
    16  	epOptions map[string]interface{}) error {
    17  	defer osl.InitOSContext()()
    18  
    19  	if err := validateID(nid, eid); err != nil {
    20  		return err
    21  	}
    22  	n, err := d.getNetwork(nid)
    23  	if err != nil {
    24  		return fmt.Errorf("network id %q not found", nid)
    25  	}
    26  	if ifInfo.MacAddress() != nil {
    27  		return fmt.Errorf("%s interfaces do not support custom mac address assignment", ipvlanType)
    28  	}
    29  	ep := &endpoint{
    30  		id:     eid,
    31  		nid:    nid,
    32  		addr:   ifInfo.Address(),
    33  		addrv6: ifInfo.AddressIPv6(),
    34  	}
    35  	if ep.addr == nil {
    36  		return fmt.Errorf("create endpoint was not passed an IP address")
    37  	}
    38  	// disallow port mapping -p
    39  	if opt, ok := epOptions[netlabel.PortMap]; ok {
    40  		if _, ok := opt.([]types.PortBinding); ok {
    41  			if len(opt.([]types.PortBinding)) > 0 {
    42  				logrus.Warnf("%s driver does not support port mappings", ipvlanType)
    43  			}
    44  		}
    45  	}
    46  	// disallow port exposure --expose
    47  	if opt, ok := epOptions[netlabel.ExposedPorts]; ok {
    48  		if _, ok := opt.([]types.TransportPort); ok {
    49  			if len(opt.([]types.TransportPort)) > 0 {
    50  				logrus.Warnf("%s driver does not support port exposures", ipvlanType)
    51  			}
    52  		}
    53  	}
    54  
    55  	if err := d.storeUpdate(ep); err != nil {
    56  		return fmt.Errorf("failed to save ipvlan endpoint %.7s to store: %v", ep.id, err)
    57  	}
    58  
    59  	n.addEndpoint(ep)
    60  
    61  	return nil
    62  }
    63  
    64  // DeleteEndpoint remove the endpoint and associated netlink interface
    65  func (d *driver) DeleteEndpoint(nid, eid string) error {
    66  	defer osl.InitOSContext()()
    67  	if err := validateID(nid, eid); err != nil {
    68  		return err
    69  	}
    70  	n := d.network(nid)
    71  	if n == nil {
    72  		return fmt.Errorf("network id %q not found", nid)
    73  	}
    74  	ep := n.endpoint(eid)
    75  	if ep == nil {
    76  		return fmt.Errorf("endpoint id %q not found", eid)
    77  	}
    78  	if link, err := ns.NlHandle().LinkByName(ep.srcName); err == nil {
    79  		if err := ns.NlHandle().LinkDel(link); err != nil {
    80  			logrus.WithError(err).Warnf("Failed to delete interface (%s)'s link on endpoint (%s) delete", ep.srcName, ep.id)
    81  		}
    82  	}
    83  
    84  	if err := d.storeDelete(ep); err != nil {
    85  		logrus.Warnf("Failed to remove ipvlan endpoint %.7s from store: %v", ep.id, err)
    86  	}
    87  	n.deleteEndpoint(ep.id)
    88  	return nil
    89  }