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

     1  package overlay
     2  
     3  import (
     4  	"fmt"
     5  	"net"
     6  
     7  	"encoding/json"
     8  
     9  	"github.com/docker/libnetwork/types"
    10  	"github.com/sirupsen/logrus"
    11  
    12  	"github.com/Microsoft/hcsshim"
    13  )
    14  
    15  const ovPeerTable = "overlay_peer_table"
    16  
    17  func (d *driver) peerAdd(nid, eid string, peerIP net.IP, peerIPMask net.IPMask,
    18  	peerMac net.HardwareAddr, vtep net.IP, updateDb bool) error {
    19  
    20  	logrus.Debugf("WINOVERLAY: Enter peerAdd for ca ip %s with ca mac %s", peerIP.String(), peerMac.String())
    21  
    22  	if err := validateID(nid, eid); err != nil {
    23  		return err
    24  	}
    25  
    26  	n := d.network(nid)
    27  	if n == nil {
    28  		return nil
    29  	}
    30  
    31  	if updateDb {
    32  		logrus.Info("WINOVERLAY: peerAdd: notifying HNS of the REMOTE endpoint")
    33  
    34  		hnsEndpoint := &hcsshim.HNSEndpoint{
    35  			Name:             eid,
    36  			VirtualNetwork:   n.hnsID,
    37  			MacAddress:       peerMac.String(),
    38  			IPAddress:        peerIP,
    39  			IsRemoteEndpoint: true,
    40  		}
    41  
    42  		paPolicy, err := json.Marshal(hcsshim.PaPolicy{
    43  			Type: "PA",
    44  			PA:   vtep.String(),
    45  		})
    46  
    47  		if err != nil {
    48  			return err
    49  		}
    50  
    51  		hnsEndpoint.Policies = append(hnsEndpoint.Policies, paPolicy)
    52  
    53  		configurationb, err := json.Marshal(hnsEndpoint)
    54  		if err != nil {
    55  			return err
    56  		}
    57  
    58  		// Temp: We have to create an endpoint object to keep track of the HNS ID for
    59  		// this endpoint so that we can retrieve it later when the endpoint is deleted.
    60  		// This seems unnecessary when we already have dockers EID. See if we can pass
    61  		// the global EID to HNS to use as it's ID, rather than having each HNS assign
    62  		// it's own local ID for the endpoint
    63  
    64  		addr, err := types.ParseCIDR(peerIP.String() + "/32")
    65  		if err != nil {
    66  			return err
    67  		}
    68  
    69  		n.removeEndpointWithAddress(addr)
    70  		hnsresponse, err := endpointRequest("POST", "", string(configurationb))
    71  		if err != nil {
    72  			return err
    73  		}
    74  
    75  		ep := &endpoint{
    76  			id:        eid,
    77  			nid:       nid,
    78  			addr:      addr,
    79  			mac:       peerMac,
    80  			profileID: hnsresponse.Id,
    81  			remote:    true,
    82  		}
    83  
    84  		n.addEndpoint(ep)
    85  	}
    86  
    87  	return nil
    88  }
    89  
    90  func (d *driver) peerDelete(nid, eid string, peerIP net.IP, peerIPMask net.IPMask,
    91  	peerMac net.HardwareAddr, vtep net.IP, updateDb bool) error {
    92  
    93  	logrus.Infof("WINOVERLAY: Enter peerDelete for endpoint %s and peer ip %s", eid, peerIP.String())
    94  
    95  	if err := validateID(nid, eid); err != nil {
    96  		return err
    97  	}
    98  
    99  	n := d.network(nid)
   100  	if n == nil {
   101  		return nil
   102  	}
   103  
   104  	ep := n.endpoint(eid)
   105  	if ep == nil {
   106  		return fmt.Errorf("could not find endpoint with id %s", eid)
   107  	}
   108  
   109  	if updateDb {
   110  		_, err := endpointRequest("DELETE", ep.profileID, "")
   111  		if err != nil {
   112  			return err
   113  		}
   114  
   115  		n.deleteEndpoint(eid)
   116  	}
   117  
   118  	return nil
   119  }