12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355 |
- package bridge
- import (
- "errors"
- "fmt"
- "io/ioutil"
- "net"
- "os"
- "os/exec"
- "path/filepath"
- "strconv"
- "strings"
- "sync"
- "syscall"
- "github.com/Sirupsen/logrus"
- "github.com/docker/libnetwork/datastore"
- "github.com/docker/libnetwork/driverapi"
- "github.com/docker/libnetwork/ipallocator"
- "github.com/docker/libnetwork/iptables"
- "github.com/docker/libnetwork/netlabel"
- "github.com/docker/libnetwork/netutils"
- "github.com/docker/libnetwork/options"
- "github.com/docker/libnetwork/osl"
- "github.com/docker/libnetwork/portmapper"
- "github.com/docker/libnetwork/types"
- "github.com/vishvananda/netlink"
- )
- const (
- networkType = "bridge"
- vethPrefix = "veth"
- vethLen = 7
- containerVethPrefix = "eth"
- maxAllocatePortAttempts = 10
- )
- const (
- // DefaultGatewayV4AuxKey represents the default-gateway configured by the user
- DefaultGatewayV4AuxKey = "DefaultGatewayIPv4"
- // DefaultGatewayV6AuxKey represents the ipv6 default-gateway configured by the user
- DefaultGatewayV6AuxKey = "DefaultGatewayIPv6"
- )
- var (
- ipAllocator *ipallocator.IPAllocator
- )
- // configuration info for the "bridge" driver.
- type configuration struct {
- EnableIPForwarding bool
- EnableIPTables bool
- EnableUserlandProxy bool
- }
- // networkConfiguration for network specific configuration
- type networkConfiguration struct {
- BridgeName string
- EnableIPv6 bool
- EnableIPMasquerade bool
- EnableICC bool
- Mtu int
- DefaultBindingIP net.IP
- DefaultBridge bool
- // Internal fields set after ipam data parsing
- AddressIPv4 *net.IPNet
- AddressIPv6 *net.IPNet
- DefaultGatewayIPv4 net.IP
- DefaultGatewayIPv6 net.IP
- }
- // endpointConfiguration represents the user specified configuration for the sandbox endpoint
- type endpointConfiguration struct {
- MacAddress net.HardwareAddr
- PortBindings []types.PortBinding
- ExposedPorts []types.TransportPort
- }
- // containerConfiguration represents the user specified configuration for a container
- type containerConfiguration struct {
- ParentEndpoints []string
- ChildEndpoints []string
- }
- type bridgeEndpoint struct {
- id string
- srcName string
- addr *net.IPNet
- addrv6 *net.IPNet
- macAddress net.HardwareAddr
- config *endpointConfiguration // User specified parameters
- containerConfig *containerConfiguration
- portMapping []types.PortBinding // Operation port bindings
- }
- type bridgeNetwork struct {
- id string
- bridge *bridgeInterface // The bridge's L3 interface
- config *networkConfiguration
- endpoints map[string]*bridgeEndpoint // key: endpoint id
- portMapper *portmapper.PortMapper
- driver *driver // The network's driver
- sync.Mutex
- }
- type driver struct {
- config *configuration
- network *bridgeNetwork
- natChain *iptables.ChainInfo
- filterChain *iptables.ChainInfo
- networks map[string]*bridgeNetwork
- sync.Mutex
- }
- // New constructs a new bridge driver
- func newDriver() *driver {
- ipAllocator = ipallocator.New()
- return &driver{networks: map[string]*bridgeNetwork{}, config: &configuration{}}
- }
- // Init registers a new instance of bridge driver
- func Init(dc driverapi.DriverCallback, config map[string]interface{}) error {
- if _, err := os.Stat("/proc/sys/net/bridge"); err != nil {
- if out, err := exec.Command("modprobe", "-va", "bridge", "br_netfilter").CombinedOutput(); err != nil {
- logrus.Warnf("Running modprobe bridge br_netfilter failed with message: %s, error: %v", out, err)
- }
- }
- if out, err := exec.Command("modprobe", "-va", "nf_nat").CombinedOutput(); err != nil {
- logrus.Warnf("Running modprobe nf_nat failed with message: `%s`, error: %v", strings.TrimSpace(string(out)), err)
- }
- if err := iptables.FirewalldInit(); err != nil {
- logrus.Debugf("Fail to initialize firewalld: %v, using raw iptables instead", err)
- }
- if err := iptables.RemoveExistingChain(DockerChain, iptables.Nat); err != nil {
- logrus.Warnf("Failed to remove existing iptables entries in %s : %v", DockerChain, err)
- }
- d := newDriver()
- if err := d.configure(config); err != nil {
- return err
- }
- c := driverapi.Capability{
- DataScope: datastore.LocalScope,
- }
- return dc.RegisterDriver(networkType, d, c)
- }
- // Validate performs a static validation on the network configuration parameters.
- // Whatever can be assessed a priori before attempting any programming.
- func (c *networkConfiguration) Validate() error {
- if c.Mtu < 0 {
- return ErrInvalidMtu(c.Mtu)
- }
- // If bridge v4 subnet is specified
- if c.AddressIPv4 != nil {
- // If default gw is specified, it must be part of bridge subnet
- if c.DefaultGatewayIPv4 != nil {
- if !c.AddressIPv4.Contains(c.DefaultGatewayIPv4) {
- return &ErrInvalidGateway{}
- }
- }
- }
- // If default v6 gw is specified, AddressIPv6 must be specified and gw must belong to AddressIPv6 subnet
- if c.EnableIPv6 && c.DefaultGatewayIPv6 != nil {
- if c.AddressIPv6 == nil || !c.AddressIPv6.Contains(c.DefaultGatewayIPv6) {
- return &ErrInvalidGateway{}
- }
- }
- return nil
- }
- // Conflicts check if two NetworkConfiguration objects overlap
- func (c *networkConfiguration) Conflicts(o *networkConfiguration) error {
- if o == nil {
- return fmt.Errorf("same configuration")
- }
- // Also empty, becasue only one network with empty name is allowed
- if c.BridgeName == o.BridgeName {
- return fmt.Errorf("networks have same name")
- }
- // They must be in different subnets
- if (c.AddressIPv4 != nil && o.AddressIPv4 != nil) &&
- (c.AddressIPv4.Contains(o.AddressIPv4.IP) || o.AddressIPv4.Contains(c.AddressIPv4.IP)) {
- return fmt.Errorf("networks have overlapping IPv4")
- }
- // They must be in different v6 subnets
- if (c.AddressIPv6 != nil && o.AddressIPv6 != nil) &&
- (c.AddressIPv6.Contains(o.AddressIPv6.IP) || o.AddressIPv6.Contains(c.AddressIPv6.IP)) {
- return fmt.Errorf("networks have overlapping IPv6")
- }
- return nil
- }
- // fromMap retrieve the configuration data from the map form.
- func (c *networkConfiguration) fromMap(data map[string]interface{}) error {
- var err error
- if i, ok := data["BridgeName"]; ok && i != nil {
- if c.BridgeName, ok = i.(string); !ok {
- return types.BadRequestErrorf("invalid type for BridgeName value")
- }
- }
- if i, ok := data["Mtu"]; ok && i != nil {
- if s, ok := i.(string); ok {
- if c.Mtu, err = strconv.Atoi(s); err != nil {
- return types.BadRequestErrorf("failed to parse Mtu value: %s", err.Error())
- }
- } else {
- return types.BadRequestErrorf("invalid type for Mtu value")
- }
- }
- if i, ok := data["EnableIPv6"]; ok && i != nil {
- if s, ok := i.(string); ok {
- if c.EnableIPv6, err = strconv.ParseBool(s); err != nil {
- return types.BadRequestErrorf("failed to parse EnableIPv6 value: %s", err.Error())
- }
- } else {
- return types.BadRequestErrorf("invalid type for EnableIPv6 value")
- }
- }
- if i, ok := data["EnableIPMasquerade"]; ok && i != nil {
- if s, ok := i.(string); ok {
- if c.EnableIPMasquerade, err = strconv.ParseBool(s); err != nil {
- return types.BadRequestErrorf("failed to parse EnableIPMasquerade value: %s", err.Error())
- }
- } else {
- return types.BadRequestErrorf("invalid type for EnableIPMasquerade value")
- }
- }
- if i, ok := data["EnableICC"]; ok && i != nil {
- if s, ok := i.(string); ok {
- if c.EnableICC, err = strconv.ParseBool(s); err != nil {
- return types.BadRequestErrorf("failed to parse EnableICC value: %s", err.Error())
- }
- } else {
- return types.BadRequestErrorf("invalid type for EnableICC value")
- }
- }
- if i, ok := data["DefaultBridge"]; ok && i != nil {
- if s, ok := i.(string); ok {
- if c.DefaultBridge, err = strconv.ParseBool(s); err != nil {
- return types.BadRequestErrorf("failed to parse DefaultBridge value: %s", err.Error())
- }
- } else {
- return types.BadRequestErrorf("invalid type for DefaultBridge value")
- }
- }
- if i, ok := data["DefaultBindingIP"]; ok && i != nil {
- if s, ok := i.(string); ok {
- if c.DefaultBindingIP = net.ParseIP(s); c.DefaultBindingIP == nil {
- return types.BadRequestErrorf("failed to parse DefaultBindingIP value")
- }
- } else {
- return types.BadRequestErrorf("invalid type for DefaultBindingIP value")
- }
- }
- return nil
- }
- func (n *bridgeNetwork) getDriverChains() (*iptables.ChainInfo, *iptables.ChainInfo, error) {
- n.Lock()
- defer n.Unlock()
- if n.driver == nil {
- return nil, nil, types.BadRequestErrorf("no driver found")
- }
- return n.driver.natChain, n.driver.filterChain, nil
- }
- func (n *bridgeNetwork) getNetworkBridgeName() string {
- n.Lock()
- config := n.config
- n.Unlock()
- return config.BridgeName
- }
- func (n *bridgeNetwork) getEndpoint(eid string) (*bridgeEndpoint, error) {
- n.Lock()
- defer n.Unlock()
- if eid == "" {
- return nil, InvalidEndpointIDError(eid)
- }
- if ep, ok := n.endpoints[eid]; ok {
- return ep, nil
- }
- return nil, nil
- }
- // Install/Removes the iptables rules needed to isolate this network
- // from each of the other networks
- func (n *bridgeNetwork) isolateNetwork(others []*bridgeNetwork, enable bool) error {
- n.Lock()
- thisV4 := n.bridge.bridgeIPv4
- thisV6 := getV6Network(n.config, n.bridge)
- n.Unlock()
- // Install the rules to isolate this networks against each of the other networks
- for _, o := range others {
- o.Lock()
- otherV4 := o.bridge.bridgeIPv4
- otherV6 := getV6Network(o.config, o.bridge)
- o.Unlock()
- if !types.CompareIPNet(thisV4, otherV4) {
- // It's ok to pass a.b.c.d/x, iptables will ignore the host subnet bits
- if err := setINC(thisV4.String(), otherV4.String(), enable); err != nil {
- return err
- }
- }
- if thisV6 != nil && otherV6 != nil && !types.CompareIPNet(thisV6, otherV6) {
- if err := setINC(thisV6.String(), otherV6.String(), enable); err != nil {
- return err
- }
- }
- }
- return nil
- }
- // Checks whether this network's configuration for the network with this id conflicts with any of the passed networks
- func (c *networkConfiguration) conflictsWithNetworks(id string, others []*bridgeNetwork) error {
- for _, nw := range others {
- nw.Lock()
- nwID := nw.id
- nwConfig := nw.config
- nwBridge := nw.bridge
- nw.Unlock()
- if nwID == id {
- continue
- }
- // Verify the name (which may have been set by newInterface()) does not conflict with
- // existing bridge interfaces. Ironically the system chosen name gets stored in the config...
- // Basically we are checking if the two original configs were both empty.
- if nwConfig.BridgeName == c.BridgeName {
- return types.ForbiddenErrorf("conflicts with network %s (%s) by bridge name", nwID, nwConfig.BridgeName)
- }
- // If this network config specifies the AddressIPv4, we need
- // to make sure it does not conflict with any previously allocated
- // bridges. This could not be completely caught by the config conflict
- // check, because networks which config does not specify the AddressIPv4
- // get their address and subnet selected by the driver (see electBridgeIPv4())
- if c.AddressIPv4 != nil {
- if nwBridge.bridgeIPv4.Contains(c.AddressIPv4.IP) ||
- c.AddressIPv4.Contains(nwBridge.bridgeIPv4.IP) {
- return types.ForbiddenErrorf("conflicts with network %s (%s) by ip network", nwID, nwConfig.BridgeName)
- }
- }
- }
- return nil
- }
- func (d *driver) configure(option map[string]interface{}) error {
- var config *configuration
- var err error
- d.Lock()
- defer d.Unlock()
- genericData, ok := option[netlabel.GenericData]
- if !ok || genericData == nil {
- return nil
- }
- switch opt := genericData.(type) {
- case options.Generic:
- opaqueConfig, err := options.GenerateFromModel(opt, &configuration{})
- if err != nil {
- return err
- }
- config = opaqueConfig.(*configuration)
- case *configuration:
- config = opt
- default:
- return &ErrInvalidDriverConfig{}
- }
- if config.EnableIPForwarding {
- err = setupIPForwarding()
- if err != nil {
- return err
- }
- }
- if config.EnableIPTables {
- d.natChain, d.filterChain, err = setupIPChains(config)
- if err != nil {
- return err
- }
- }
- d.config = config
- return nil
- }
- func (d *driver) getNetwork(id string) (*bridgeNetwork, error) {
- d.Lock()
- defer d.Unlock()
- if id == "" {
- return nil, types.BadRequestErrorf("invalid network id: %s", id)
- }
- if nw, ok := d.networks[id]; ok {
- return nw, nil
- }
- return nil, types.NotFoundErrorf("network not found: %s", id)
- }
- func parseNetworkGenericOptions(data interface{}) (*networkConfiguration, error) {
- var (
- err error
- config *networkConfiguration
- )
- switch opt := data.(type) {
- case *networkConfiguration:
- config = opt
- case map[string]interface{}:
- config = &networkConfiguration{
- EnableICC: true,
- EnableIPMasquerade: true,
- }
- err = config.fromMap(opt)
- case options.Generic:
- var opaqueConfig interface{}
- if opaqueConfig, err = options.GenerateFromModel(opt, config); err == nil {
- config = opaqueConfig.(*networkConfiguration)
- }
- default:
- err = types.BadRequestErrorf("do not recognize network configuration format: %T", opt)
- }
- return config, err
- }
- func (c *networkConfiguration) processIPAM(id string, ipamV4Data, ipamV6Data []driverapi.IPAMData) error {
- if len(ipamV4Data) > 1 || len(ipamV6Data) > 1 {
- return types.ForbiddenErrorf("bridge driver doesnt support multiple subnets")
- }
- if len(ipamV4Data) == 0 {
- return types.BadRequestErrorf("bridge network %s requires ipv4 configuration", id)
- }
- if ipamV4Data[0].Gateway != nil {
- c.AddressIPv4 = types.GetIPNetCopy(ipamV4Data[0].Gateway)
- }
- if gw, ok := ipamV4Data[0].AuxAddresses[DefaultGatewayV4AuxKey]; ok {
- c.DefaultGatewayIPv4 = gw.IP
- }
- if len(ipamV6Data) > 0 {
- if ipamV6Data[0].Gateway != nil {
- c.AddressIPv6 = types.GetIPNetCopy(ipamV6Data[0].Gateway)
- }
- if gw, ok := ipamV6Data[0].AuxAddresses[DefaultGatewayV6AuxKey]; ok {
- c.DefaultGatewayIPv6 = gw.IP
- }
- }
- return nil
- }
- func parseNetworkOptions(id string, option options.Generic) (*networkConfiguration, error) {
- var err error
- config := &networkConfiguration{}
- // Parse generic label first, config will be re-assigned
- if genData, ok := option[netlabel.GenericData]; ok && genData != nil {
- if config, err = parseNetworkGenericOptions(genData); err != nil {
- return nil, err
- }
- }
- // Process well-known labels next
- if _, ok := option[netlabel.EnableIPv6]; ok {
- config.EnableIPv6 = option[netlabel.EnableIPv6].(bool)
- }
- // Finally validate the configuration
- if err = config.Validate(); err != nil {
- return nil, err
- }
- if config.BridgeName == "" && config.DefaultBridge == false {
- config.BridgeName = "br-" + id[:12]
- }
- return config, nil
- }
- // Returns the non link-local IPv6 subnet for the containers attached to this bridge if found, nil otherwise
- func getV6Network(config *networkConfiguration, i *bridgeInterface) *net.IPNet {
- if config.AddressIPv6 != nil {
- return config.AddressIPv6
- }
- if i.bridgeIPv6 != nil && i.bridgeIPv6.IP != nil && !i.bridgeIPv6.IP.IsLinkLocalUnicast() {
- return i.bridgeIPv6
- }
- return nil
- }
- // Return a slice of networks over which caller can iterate safely
- func (d *driver) getNetworks() []*bridgeNetwork {
- d.Lock()
- defer d.Unlock()
- ls := make([]*bridgeNetwork, 0, len(d.networks))
- for _, nw := range d.networks {
- ls = append(ls, nw)
- }
- return ls
- }
- // Create a new network using bridge plugin
- func (d *driver) CreateNetwork(id string, option map[string]interface{}, ipV4Data, ipV6Data []driverapi.IPAMData) error {
- var err error
- defer osl.InitOSContext()()
- // Sanity checks
- d.Lock()
- if _, ok := d.networks[id]; ok {
- d.Unlock()
- return types.ForbiddenErrorf("network %s exists", id)
- }
- d.Unlock()
- // Parse and validate the config. It should not conflict with existing networks' config
- config, err := parseNetworkOptions(id, option)
- if err != nil {
- return err
- }
- err = config.processIPAM(id, ipV4Data, ipV6Data)
- if err != nil {
- return err
- }
- networkList := d.getNetworks()
- for _, nw := range networkList {
- nw.Lock()
- nwConfig := nw.config
- nw.Unlock()
- if err := nwConfig.Conflicts(config); err != nil {
- return types.ForbiddenErrorf("cannot create network %s (%s): conflicts with network %s (%s): %s",
- nwConfig.BridgeName, id, nw.id, nw.config.BridgeName, err.Error())
- }
- }
- // Create and set network handler in driver
- network := &bridgeNetwork{
- id: id,
- endpoints: make(map[string]*bridgeEndpoint),
- config: config,
- portMapper: portmapper.New(),
- driver: d,
- }
- d.Lock()
- d.networks[id] = network
- d.Unlock()
- // On failure make sure to reset driver network handler to nil
- defer func() {
- if err != nil {
- d.Lock()
- delete(d.networks, id)
- d.Unlock()
- }
- }()
- // Create or retrieve the bridge L3 interface
- bridgeIface := newInterface(config)
- network.bridge = bridgeIface
- // Verify the network configuration does not conflict with previously installed
- // networks. This step is needed now because driver might have now set the bridge
- // name on this config struct. And because we need to check for possible address
- // conflicts, so we need to check against operationa lnetworks.
- if err = config.conflictsWithNetworks(id, networkList); err != nil {
- return err
- }
- setupNetworkIsolationRules := func(config *networkConfiguration, i *bridgeInterface) error {
- if err := network.isolateNetwork(networkList, true); err != nil {
- if err := network.isolateNetwork(networkList, false); err != nil {
- logrus.Warnf("Failed on removing the inter-network iptables rules on cleanup: %v", err)
- }
- return err
- }
- return nil
- }
- // Prepare the bridge setup configuration
- bridgeSetup := newBridgeSetup(config, bridgeIface)
- // If the bridge interface doesn't exist, we need to start the setup steps
- // by creating a new device and assigning it an IPv4 address.
- bridgeAlreadyExists := bridgeIface.exists()
- if !bridgeAlreadyExists {
- bridgeSetup.queueStep(setupDevice)
- }
- // Even if a bridge exists try to setup IPv4.
- bridgeSetup.queueStep(setupBridgeIPv4)
- enableIPv6Forwarding := d.config.EnableIPForwarding && config.AddressIPv6 != nil
- // Conditionally queue setup steps depending on configuration values.
- for _, step := range []struct {
- Condition bool
- Fn setupStep
- }{
- // Enable IPv6 on the bridge if required. We do this even for a
- // previously existing bridge, as it may be here from a previous
- // installation where IPv6 wasn't supported yet and needs to be
- // assigned an IPv6 link-local address.
- {config.EnableIPv6, setupBridgeIPv6},
- // We ensure that the bridge has the expectedIPv4 and IPv6 addresses in
- // the case of a previously existing device.
- {bridgeAlreadyExists, setupVerifyAndReconcile},
- // Enable IPv6 Forwarding
- {enableIPv6Forwarding, setupIPv6Forwarding},
- // Setup Loopback Adresses Routing
- {!d.config.EnableUserlandProxy, setupLoopbackAdressesRouting},
- // Setup IPTables.
- {d.config.EnableIPTables, network.setupIPTables},
- //We want to track firewalld configuration so that
- //if it is started/reloaded, the rules can be applied correctly
- {d.config.EnableIPTables, network.setupFirewalld},
- // Setup DefaultGatewayIPv4
- {config.DefaultGatewayIPv4 != nil, setupGatewayIPv4},
- // Setup DefaultGatewayIPv6
- {config.DefaultGatewayIPv6 != nil, setupGatewayIPv6},
- // Add inter-network communication rules.
- {d.config.EnableIPTables, setupNetworkIsolationRules},
- //Configure bridge networking filtering if ICC is off and IP tables are enabled
- {!config.EnableICC && d.config.EnableIPTables, setupBridgeNetFiltering},
- } {
- if step.Condition {
- bridgeSetup.queueStep(step.Fn)
- }
- }
- // Apply the prepared list of steps, and abort at the first error.
- bridgeSetup.queueStep(setupDeviceUp)
- if err = bridgeSetup.apply(); err != nil {
- return err
- }
- return nil
- }
- func (d *driver) DeleteNetwork(nid string) error {
- var err error
- defer osl.InitOSContext()()
- // Get network handler and remove it from driver
- d.Lock()
- n, ok := d.networks[nid]
- d.Unlock()
- if !ok {
- return types.InternalMaskableErrorf("network %s does not exist", nid)
- }
- n.Lock()
- config := n.config
- n.Unlock()
- if config.BridgeName == DefaultBridgeName {
- return types.ForbiddenErrorf("default network of type \"%s\" cannot be deleted", networkType)
- }
- d.Lock()
- delete(d.networks, nid)
- d.Unlock()
- // On failure set network handler back in driver, but
- // only if is not already taken over by some other thread
- defer func() {
- if err != nil {
- d.Lock()
- if _, ok := d.networks[nid]; !ok {
- d.networks[nid] = n
- }
- d.Unlock()
- }
- }()
- // Sanity check
- if n == nil {
- err = driverapi.ErrNoNetwork(nid)
- return err
- }
- // Cannot remove network if endpoints are still present
- if len(n.endpoints) != 0 {
- err = ActiveEndpointsError(n.id)
- return err
- }
- // In case of failures after this point, restore the network isolation rules
- nwList := d.getNetworks()
- defer func() {
- if err != nil {
- if err := n.isolateNetwork(nwList, true); err != nil {
- logrus.Warnf("Failed on restoring the inter-network iptables rules on cleanup: %v", err)
- }
- }
- }()
- // Remove inter-network communication rules.
- err = n.isolateNetwork(nwList, false)
- if err != nil {
- return err
- }
- // Programming
- return netlink.LinkDel(n.bridge.Link)
- }
- func addToBridge(ifaceName, bridgeName string) error {
- link, err := netlink.LinkByName(ifaceName)
- if err != nil {
- return fmt.Errorf("could not find interface %s: %v", ifaceName, err)
- }
- if err = netlink.LinkSetMaster(link,
- &netlink.Bridge{LinkAttrs: netlink.LinkAttrs{Name: bridgeName}}); err != nil {
- logrus.Debugf("Failed to add %s to bridge via netlink.Trying ioctl: %v", ifaceName, err)
- iface, err := net.InterfaceByName(ifaceName)
- if err != nil {
- return fmt.Errorf("could not find network interface %s: %v", ifaceName, err)
- }
- master, err := net.InterfaceByName(bridgeName)
- if err != nil {
- return fmt.Errorf("could not find bridge %s: %v", bridgeName, err)
- }
- return ioctlAddToBridge(iface, master)
- }
- return nil
- }
- func setHairpinMode(link netlink.Link, enable bool) error {
- err := netlink.LinkSetHairpin(link, enable)
- if err != nil && err != syscall.EINVAL {
- // If error is not EINVAL something else went wrong, bail out right away
- return fmt.Errorf("unable to set hairpin mode on %s via netlink: %v",
- link.Attrs().Name, err)
- }
- // Hairpin mode successfully set up
- if err == nil {
- return nil
- }
- // The netlink method failed with EINVAL which is probably because of an older
- // kernel. Try one more time via the sysfs method.
- path := filepath.Join("/sys/class/net", link.Attrs().Name, "brport/hairpin_mode")
- var val []byte
- if enable {
- val = []byte{'1', '\n'}
- } else {
- val = []byte{'0', '\n'}
- }
- if err := ioutil.WriteFile(path, val, 0644); err != nil {
- return fmt.Errorf("unable to set hairpin mode on %s via sysfs: %v", link.Attrs().Name, err)
- }
- return nil
- }
- func (d *driver) CreateEndpoint(nid, eid string, ifInfo driverapi.InterfaceInfo, epOptions map[string]interface{}) error {
- defer osl.InitOSContext()()
- if ifInfo == nil {
- return errors.New("invalid interface info passed")
- }
- // Get the network handler and make sure it exists
- d.Lock()
- n, ok := d.networks[nid]
- dconfig := d.config
- d.Unlock()
- if !ok {
- return types.NotFoundErrorf("network %s does not exist", nid)
- }
- if n == nil {
- return driverapi.ErrNoNetwork(nid)
- }
- // Sanity check
- n.Lock()
- if n.id != nid {
- n.Unlock()
- return InvalidNetworkIDError(nid)
- }
- n.Unlock()
- // Check if endpoint id is good and retrieve correspondent endpoint
- ep, err := n.getEndpoint(eid)
- if err != nil {
- return err
- }
- // Endpoint with that id exists either on desired or other sandbox
- if ep != nil {
- return driverapi.ErrEndpointExists(eid)
- }
- // Try to convert the options to endpoint configuration
- epConfig, err := parseEndpointOptions(epOptions)
- if err != nil {
- return err
- }
- // Create and add the endpoint
- n.Lock()
- endpoint := &bridgeEndpoint{id: eid, config: epConfig}
- n.endpoints[eid] = endpoint
- n.Unlock()
- // On failure make sure to remove the endpoint
- defer func() {
- if err != nil {
- n.Lock()
- delete(n.endpoints, eid)
- n.Unlock()
- }
- }()
- // Generate a name for what will be the host side pipe interface
- hostIfName, err := netutils.GenerateIfaceName(vethPrefix, vethLen)
- if err != nil {
- return err
- }
- // Generate a name for what will be the sandbox side pipe interface
- containerIfName, err := netutils.GenerateIfaceName(vethPrefix, vethLen)
- if err != nil {
- return err
- }
- // Generate and add the interface pipe host <-> sandbox
- veth := &netlink.Veth{
- LinkAttrs: netlink.LinkAttrs{Name: hostIfName, TxQLen: 0},
- PeerName: containerIfName}
- if err = netlink.LinkAdd(veth); err != nil {
- return types.InternalErrorf("failed to add the host (%s) <=> sandbox (%s) pair interfaces: %v", hostIfName, containerIfName, err)
- }
- // Get the host side pipe interface handler
- host, err := netlink.LinkByName(hostIfName)
- if err != nil {
- return types.InternalErrorf("failed to find host side interface %s: %v", hostIfName, err)
- }
- defer func() {
- if err != nil {
- netlink.LinkDel(host)
- }
- }()
- // Get the sandbox side pipe interface handler
- sbox, err := netlink.LinkByName(containerIfName)
- if err != nil {
- return types.InternalErrorf("failed to find sandbox side interface %s: %v", containerIfName, err)
- }
- defer func() {
- if err != nil {
- netlink.LinkDel(sbox)
- }
- }()
- n.Lock()
- config := n.config
- n.Unlock()
- // Add bridge inherited attributes to pipe interfaces
- if config.Mtu != 0 {
- err = netlink.LinkSetMTU(host, config.Mtu)
- if err != nil {
- return types.InternalErrorf("failed to set MTU on host interface %s: %v", hostIfName, err)
- }
- err = netlink.LinkSetMTU(sbox, config.Mtu)
- if err != nil {
- return types.InternalErrorf("failed to set MTU on sandbox interface %s: %v", containerIfName, err)
- }
- }
- // Attach host side pipe interface into the bridge
- if err = addToBridge(hostIfName, config.BridgeName); err != nil {
- return fmt.Errorf("adding interface %s to bridge %s failed: %v", hostIfName, config.BridgeName, err)
- }
- if !dconfig.EnableUserlandProxy {
- err = setHairpinMode(host, true)
- if err != nil {
- return err
- }
- }
- // Create the sandbox side pipe interface
- endpoint.srcName = containerIfName
- endpoint.macAddress = ifInfo.MacAddress()
- endpoint.addr = ifInfo.Address()
- endpoint.addrv6 = ifInfo.AddressIPv6()
- // Down the interface before configuring mac address.
- if err = netlink.LinkSetDown(sbox); err != nil {
- return fmt.Errorf("could not set link down for container interface %s: %v", containerIfName, err)
- }
- // Set the sbox's MAC. If specified, use the one configured by user, otherwise generate one based on IP.
- if endpoint.macAddress == nil {
- endpoint.macAddress = electMacAddress(epConfig, endpoint.addr.IP)
- if err := ifInfo.SetMacAddress(endpoint.macAddress); err != nil {
- return err
- }
- }
- err = netlink.LinkSetHardwareAddr(sbox, endpoint.macAddress)
- if err != nil {
- return fmt.Errorf("could not set mac address for container interface %s: %v", containerIfName, err)
- }
- // Up the host interface after finishing all netlink configuration
- if err = netlink.LinkSetUp(host); err != nil {
- return fmt.Errorf("could not set link up for host interface %s: %v", hostIfName, err)
- }
- if endpoint.addrv6 == nil && config.EnableIPv6 {
- var ip6 net.IP
- network := n.bridge.bridgeIPv6
- ones, _ := network.Mask.Size()
- if ones <= 80 {
- ip6 = make(net.IP, len(network.IP))
- copy(ip6, network.IP)
- for i, h := range endpoint.macAddress {
- ip6[i+10] = h
- }
- }
- endpoint.addrv6 = &net.IPNet{IP: ip6, Mask: network.Mask}
- if err := ifInfo.SetIPAddress(endpoint.addrv6); err != nil {
- return err
- }
- }
- // Program any required port mapping and store them in the endpoint
- endpoint.portMapping, err = n.allocatePorts(epConfig, endpoint, config.DefaultBindingIP, d.config.EnableUserlandProxy)
- if err != nil {
- return err
- }
- return nil
- }
- func (d *driver) DeleteEndpoint(nid, eid string) error {
- var err error
- defer osl.InitOSContext()()
- // Get the network handler and make sure it exists
- d.Lock()
- n, ok := d.networks[nid]
- d.Unlock()
- if !ok {
- return types.NotFoundErrorf("network %s does not exist", nid)
- }
- if n == nil {
- return driverapi.ErrNoNetwork(nid)
- }
- // Sanity Check
- n.Lock()
- if n.id != nid {
- n.Unlock()
- return InvalidNetworkIDError(nid)
- }
- n.Unlock()
- // Check endpoint id and if an endpoint is actually there
- ep, err := n.getEndpoint(eid)
- if err != nil {
- return err
- }
- if ep == nil {
- return EndpointNotFoundError(eid)
- }
- // Remove it
- n.Lock()
- delete(n.endpoints, eid)
- n.Unlock()
- // On failure make sure to set back ep in n.endpoints, but only
- // if it hasn't been taken over already by some other thread.
- defer func() {
- if err != nil {
- n.Lock()
- if _, ok := n.endpoints[eid]; !ok {
- n.endpoints[eid] = ep
- }
- n.Unlock()
- }
- }()
- // Remove port mappings. Do not stop endpoint delete on unmap failure
- n.releasePorts(ep)
- // Release the v4 address allocated to this endpoint's sandbox interface
- err = ipAllocator.ReleaseIP(n.bridge.bridgeIPv4, ep.addr.IP)
- if err != nil {
- return err
- }
- // Try removal of link. Discard error: link pair might have
- // already been deleted by sandbox delete. Make sure defer
- // does not see this error either.
- if link, err := netlink.LinkByName(ep.srcName); err == nil {
- netlink.LinkDel(link)
- }
- return nil
- }
- func (d *driver) EndpointOperInfo(nid, eid string) (map[string]interface{}, error) {
- // Get the network handler and make sure it exists
- d.Lock()
- n, ok := d.networks[nid]
- d.Unlock()
- if !ok {
- return nil, types.NotFoundErrorf("network %s does not exist", nid)
- }
- if n == nil {
- return nil, driverapi.ErrNoNetwork(nid)
- }
- // Sanity check
- n.Lock()
- if n.id != nid {
- n.Unlock()
- return nil, InvalidNetworkIDError(nid)
- }
- n.Unlock()
- // Check if endpoint id is good and retrieve correspondent endpoint
- ep, err := n.getEndpoint(eid)
- if err != nil {
- return nil, err
- }
- if ep == nil {
- return nil, driverapi.ErrNoEndpoint(eid)
- }
- m := make(map[string]interface{})
- if ep.config.ExposedPorts != nil {
- // Return a copy of the config data
- epc := make([]types.TransportPort, 0, len(ep.config.ExposedPorts))
- for _, tp := range ep.config.ExposedPorts {
- epc = append(epc, tp.GetCopy())
- }
- m[netlabel.ExposedPorts] = epc
- }
- if ep.portMapping != nil {
- // Return a copy of the operational data
- pmc := make([]types.PortBinding, 0, len(ep.portMapping))
- for _, pm := range ep.portMapping {
- pmc = append(pmc, pm.GetCopy())
- }
- m[netlabel.PortMap] = pmc
- }
- if len(ep.macAddress) != 0 {
- m[netlabel.MacAddress] = ep.macAddress
- }
- return m, nil
- }
- // Join method is invoked when a Sandbox is attached to an endpoint.
- func (d *driver) Join(nid, eid string, sboxKey string, jinfo driverapi.JoinInfo, options map[string]interface{}) error {
- defer osl.InitOSContext()()
- network, err := d.getNetwork(nid)
- if err != nil {
- return err
- }
- endpoint, err := network.getEndpoint(eid)
- if err != nil {
- return err
- }
- if endpoint == nil {
- return EndpointNotFoundError(eid)
- }
- iNames := jinfo.InterfaceName()
- err = iNames.SetNames(endpoint.srcName, containerVethPrefix)
- if err != nil {
- return err
- }
- err = jinfo.SetGateway(network.bridge.gatewayIPv4)
- if err != nil {
- return err
- }
- err = jinfo.SetGatewayIPv6(network.bridge.gatewayIPv6)
- if err != nil {
- return err
- }
- if !network.config.EnableICC {
- return d.link(network, endpoint, options, true)
- }
- return nil
- }
- // Leave method is invoked when a Sandbox detaches from an endpoint.
- func (d *driver) Leave(nid, eid string) error {
- defer osl.InitOSContext()()
- network, err := d.getNetwork(nid)
- if err != nil {
- return err
- }
- endpoint, err := network.getEndpoint(eid)
- if err != nil {
- return err
- }
- if endpoint == nil {
- return EndpointNotFoundError(eid)
- }
- if !network.config.EnableICC {
- return d.link(network, endpoint, nil, false)
- }
- return nil
- }
- func (d *driver) link(network *bridgeNetwork, endpoint *bridgeEndpoint, options map[string]interface{}, enable bool) error {
- var (
- cc *containerConfiguration
- err error
- )
- if enable {
- cc, err = parseContainerOptions(options)
- if err != nil {
- return err
- }
- } else {
- cc = endpoint.containerConfig
- }
- if cc == nil {
- return nil
- }
- if endpoint.config != nil && endpoint.config.ExposedPorts != nil {
- for _, p := range cc.ParentEndpoints {
- var parentEndpoint *bridgeEndpoint
- parentEndpoint, err = network.getEndpoint(p)
- if err != nil {
- return err
- }
- if parentEndpoint == nil {
- err = InvalidEndpointIDError(p)
- return err
- }
- l := newLink(parentEndpoint.addr.IP.String(),
- endpoint.addr.IP.String(),
- endpoint.config.ExposedPorts, network.config.BridgeName)
- if enable {
- err = l.Enable()
- if err != nil {
- return err
- }
- defer func() {
- if err != nil {
- l.Disable()
- }
- }()
- } else {
- l.Disable()
- }
- }
- }
- for _, c := range cc.ChildEndpoints {
- var childEndpoint *bridgeEndpoint
- childEndpoint, err = network.getEndpoint(c)
- if err != nil {
- return err
- }
- if childEndpoint == nil {
- err = InvalidEndpointIDError(c)
- return err
- }
- if childEndpoint.config == nil || childEndpoint.config.ExposedPorts == nil {
- continue
- }
- l := newLink(endpoint.addr.IP.String(),
- childEndpoint.addr.IP.String(),
- childEndpoint.config.ExposedPorts, network.config.BridgeName)
- if enable {
- err = l.Enable()
- if err != nil {
- return err
- }
- defer func() {
- if err != nil {
- l.Disable()
- }
- }()
- } else {
- l.Disable()
- }
- }
- if enable {
- endpoint.containerConfig = cc
- }
- return nil
- }
- func (d *driver) Type() string {
- return networkType
- }
- // DiscoverNew is a notification for a new discovery event, such as a new node joining a cluster
- func (d *driver) DiscoverNew(dType driverapi.DiscoveryType, data interface{}) error {
- return nil
- }
- // DiscoverDelete is a notification for a discovery delete event, such as a node leaving a cluster
- func (d *driver) DiscoverDelete(dType driverapi.DiscoveryType, data interface{}) error {
- return nil
- }
- func parseEndpointOptions(epOptions map[string]interface{}) (*endpointConfiguration, error) {
- if epOptions == nil {
- return nil, nil
- }
- ec := &endpointConfiguration{}
- if opt, ok := epOptions[netlabel.MacAddress]; ok {
- if mac, ok := opt.(net.HardwareAddr); ok {
- ec.MacAddress = mac
- } else {
- return nil, &ErrInvalidEndpointConfig{}
- }
- }
- if opt, ok := epOptions[netlabel.PortMap]; ok {
- if bs, ok := opt.([]types.PortBinding); ok {
- ec.PortBindings = bs
- } else {
- return nil, &ErrInvalidEndpointConfig{}
- }
- }
- if opt, ok := epOptions[netlabel.ExposedPorts]; ok {
- if ports, ok := opt.([]types.TransportPort); ok {
- ec.ExposedPorts = ports
- } else {
- return nil, &ErrInvalidEndpointConfig{}
- }
- }
- return ec, nil
- }
- func parseContainerOptions(cOptions map[string]interface{}) (*containerConfiguration, error) {
- if cOptions == nil {
- return nil, nil
- }
- genericData := cOptions[netlabel.GenericData]
- if genericData == nil {
- return nil, nil
- }
- switch opt := genericData.(type) {
- case options.Generic:
- opaqueConfig, err := options.GenerateFromModel(opt, &containerConfiguration{})
- if err != nil {
- return nil, err
- }
- return opaqueConfig.(*containerConfiguration), nil
- case *containerConfiguration:
- return opt, nil
- default:
- return nil, nil
- }
- }
- func electMacAddress(epConfig *endpointConfiguration, ip net.IP) net.HardwareAddr {
- if epConfig != nil && epConfig.MacAddress != nil {
- return epConfig.MacAddress
- }
- return netutils.GenerateMACFromIP(ip)
- }
|