bridge.go 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355
  1. package bridge
  2. import (
  3. "errors"
  4. "fmt"
  5. "io/ioutil"
  6. "net"
  7. "os"
  8. "os/exec"
  9. "path/filepath"
  10. "strconv"
  11. "strings"
  12. "sync"
  13. "syscall"
  14. "github.com/Sirupsen/logrus"
  15. "github.com/docker/libnetwork/datastore"
  16. "github.com/docker/libnetwork/driverapi"
  17. "github.com/docker/libnetwork/ipallocator"
  18. "github.com/docker/libnetwork/iptables"
  19. "github.com/docker/libnetwork/netlabel"
  20. "github.com/docker/libnetwork/netutils"
  21. "github.com/docker/libnetwork/options"
  22. "github.com/docker/libnetwork/osl"
  23. "github.com/docker/libnetwork/portmapper"
  24. "github.com/docker/libnetwork/types"
  25. "github.com/vishvananda/netlink"
  26. )
  27. const (
  28. networkType = "bridge"
  29. vethPrefix = "veth"
  30. vethLen = 7
  31. containerVethPrefix = "eth"
  32. maxAllocatePortAttempts = 10
  33. )
  34. const (
  35. // DefaultGatewayV4AuxKey represents the default-gateway configured by the user
  36. DefaultGatewayV4AuxKey = "DefaultGatewayIPv4"
  37. // DefaultGatewayV6AuxKey represents the ipv6 default-gateway configured by the user
  38. DefaultGatewayV6AuxKey = "DefaultGatewayIPv6"
  39. )
  40. var (
  41. ipAllocator *ipallocator.IPAllocator
  42. )
  43. // configuration info for the "bridge" driver.
  44. type configuration struct {
  45. EnableIPForwarding bool
  46. EnableIPTables bool
  47. EnableUserlandProxy bool
  48. }
  49. // networkConfiguration for network specific configuration
  50. type networkConfiguration struct {
  51. BridgeName string
  52. EnableIPv6 bool
  53. EnableIPMasquerade bool
  54. EnableICC bool
  55. Mtu int
  56. DefaultBindingIP net.IP
  57. DefaultBridge bool
  58. // Internal fields set after ipam data parsing
  59. AddressIPv4 *net.IPNet
  60. AddressIPv6 *net.IPNet
  61. DefaultGatewayIPv4 net.IP
  62. DefaultGatewayIPv6 net.IP
  63. }
  64. // endpointConfiguration represents the user specified configuration for the sandbox endpoint
  65. type endpointConfiguration struct {
  66. MacAddress net.HardwareAddr
  67. PortBindings []types.PortBinding
  68. ExposedPorts []types.TransportPort
  69. }
  70. // containerConfiguration represents the user specified configuration for a container
  71. type containerConfiguration struct {
  72. ParentEndpoints []string
  73. ChildEndpoints []string
  74. }
  75. type bridgeEndpoint struct {
  76. id string
  77. srcName string
  78. addr *net.IPNet
  79. addrv6 *net.IPNet
  80. macAddress net.HardwareAddr
  81. config *endpointConfiguration // User specified parameters
  82. containerConfig *containerConfiguration
  83. portMapping []types.PortBinding // Operation port bindings
  84. }
  85. type bridgeNetwork struct {
  86. id string
  87. bridge *bridgeInterface // The bridge's L3 interface
  88. config *networkConfiguration
  89. endpoints map[string]*bridgeEndpoint // key: endpoint id
  90. portMapper *portmapper.PortMapper
  91. driver *driver // The network's driver
  92. sync.Mutex
  93. }
  94. type driver struct {
  95. config *configuration
  96. network *bridgeNetwork
  97. natChain *iptables.ChainInfo
  98. filterChain *iptables.ChainInfo
  99. networks map[string]*bridgeNetwork
  100. sync.Mutex
  101. }
  102. // New constructs a new bridge driver
  103. func newDriver() *driver {
  104. ipAllocator = ipallocator.New()
  105. return &driver{networks: map[string]*bridgeNetwork{}, config: &configuration{}}
  106. }
  107. // Init registers a new instance of bridge driver
  108. func Init(dc driverapi.DriverCallback, config map[string]interface{}) error {
  109. if _, err := os.Stat("/proc/sys/net/bridge"); err != nil {
  110. if out, err := exec.Command("modprobe", "-va", "bridge", "br_netfilter").CombinedOutput(); err != nil {
  111. logrus.Warnf("Running modprobe bridge br_netfilter failed with message: %s, error: %v", out, err)
  112. }
  113. }
  114. if out, err := exec.Command("modprobe", "-va", "nf_nat").CombinedOutput(); err != nil {
  115. logrus.Warnf("Running modprobe nf_nat failed with message: `%s`, error: %v", strings.TrimSpace(string(out)), err)
  116. }
  117. if err := iptables.FirewalldInit(); err != nil {
  118. logrus.Debugf("Fail to initialize firewalld: %v, using raw iptables instead", err)
  119. }
  120. if err := iptables.RemoveExistingChain(DockerChain, iptables.Nat); err != nil {
  121. logrus.Warnf("Failed to remove existing iptables entries in %s : %v", DockerChain, err)
  122. }
  123. d := newDriver()
  124. if err := d.configure(config); err != nil {
  125. return err
  126. }
  127. c := driverapi.Capability{
  128. DataScope: datastore.LocalScope,
  129. }
  130. return dc.RegisterDriver(networkType, d, c)
  131. }
  132. // Validate performs a static validation on the network configuration parameters.
  133. // Whatever can be assessed a priori before attempting any programming.
  134. func (c *networkConfiguration) Validate() error {
  135. if c.Mtu < 0 {
  136. return ErrInvalidMtu(c.Mtu)
  137. }
  138. // If bridge v4 subnet is specified
  139. if c.AddressIPv4 != nil {
  140. // If default gw is specified, it must be part of bridge subnet
  141. if c.DefaultGatewayIPv4 != nil {
  142. if !c.AddressIPv4.Contains(c.DefaultGatewayIPv4) {
  143. return &ErrInvalidGateway{}
  144. }
  145. }
  146. }
  147. // If default v6 gw is specified, AddressIPv6 must be specified and gw must belong to AddressIPv6 subnet
  148. if c.EnableIPv6 && c.DefaultGatewayIPv6 != nil {
  149. if c.AddressIPv6 == nil || !c.AddressIPv6.Contains(c.DefaultGatewayIPv6) {
  150. return &ErrInvalidGateway{}
  151. }
  152. }
  153. return nil
  154. }
  155. // Conflicts check if two NetworkConfiguration objects overlap
  156. func (c *networkConfiguration) Conflicts(o *networkConfiguration) error {
  157. if o == nil {
  158. return fmt.Errorf("same configuration")
  159. }
  160. // Also empty, becasue only one network with empty name is allowed
  161. if c.BridgeName == o.BridgeName {
  162. return fmt.Errorf("networks have same name")
  163. }
  164. // They must be in different subnets
  165. if (c.AddressIPv4 != nil && o.AddressIPv4 != nil) &&
  166. (c.AddressIPv4.Contains(o.AddressIPv4.IP) || o.AddressIPv4.Contains(c.AddressIPv4.IP)) {
  167. return fmt.Errorf("networks have overlapping IPv4")
  168. }
  169. // They must be in different v6 subnets
  170. if (c.AddressIPv6 != nil && o.AddressIPv6 != nil) &&
  171. (c.AddressIPv6.Contains(o.AddressIPv6.IP) || o.AddressIPv6.Contains(c.AddressIPv6.IP)) {
  172. return fmt.Errorf("networks have overlapping IPv6")
  173. }
  174. return nil
  175. }
  176. // fromMap retrieve the configuration data from the map form.
  177. func (c *networkConfiguration) fromMap(data map[string]interface{}) error {
  178. var err error
  179. if i, ok := data["BridgeName"]; ok && i != nil {
  180. if c.BridgeName, ok = i.(string); !ok {
  181. return types.BadRequestErrorf("invalid type for BridgeName value")
  182. }
  183. }
  184. if i, ok := data["Mtu"]; ok && i != nil {
  185. if s, ok := i.(string); ok {
  186. if c.Mtu, err = strconv.Atoi(s); err != nil {
  187. return types.BadRequestErrorf("failed to parse Mtu value: %s", err.Error())
  188. }
  189. } else {
  190. return types.BadRequestErrorf("invalid type for Mtu value")
  191. }
  192. }
  193. if i, ok := data["EnableIPv6"]; ok && i != nil {
  194. if s, ok := i.(string); ok {
  195. if c.EnableIPv6, err = strconv.ParseBool(s); err != nil {
  196. return types.BadRequestErrorf("failed to parse EnableIPv6 value: %s", err.Error())
  197. }
  198. } else {
  199. return types.BadRequestErrorf("invalid type for EnableIPv6 value")
  200. }
  201. }
  202. if i, ok := data["EnableIPMasquerade"]; ok && i != nil {
  203. if s, ok := i.(string); ok {
  204. if c.EnableIPMasquerade, err = strconv.ParseBool(s); err != nil {
  205. return types.BadRequestErrorf("failed to parse EnableIPMasquerade value: %s", err.Error())
  206. }
  207. } else {
  208. return types.BadRequestErrorf("invalid type for EnableIPMasquerade value")
  209. }
  210. }
  211. if i, ok := data["EnableICC"]; ok && i != nil {
  212. if s, ok := i.(string); ok {
  213. if c.EnableICC, err = strconv.ParseBool(s); err != nil {
  214. return types.BadRequestErrorf("failed to parse EnableICC value: %s", err.Error())
  215. }
  216. } else {
  217. return types.BadRequestErrorf("invalid type for EnableICC value")
  218. }
  219. }
  220. if i, ok := data["DefaultBridge"]; ok && i != nil {
  221. if s, ok := i.(string); ok {
  222. if c.DefaultBridge, err = strconv.ParseBool(s); err != nil {
  223. return types.BadRequestErrorf("failed to parse DefaultBridge value: %s", err.Error())
  224. }
  225. } else {
  226. return types.BadRequestErrorf("invalid type for DefaultBridge value")
  227. }
  228. }
  229. if i, ok := data["DefaultBindingIP"]; ok && i != nil {
  230. if s, ok := i.(string); ok {
  231. if c.DefaultBindingIP = net.ParseIP(s); c.DefaultBindingIP == nil {
  232. return types.BadRequestErrorf("failed to parse DefaultBindingIP value")
  233. }
  234. } else {
  235. return types.BadRequestErrorf("invalid type for DefaultBindingIP value")
  236. }
  237. }
  238. return nil
  239. }
  240. func (n *bridgeNetwork) getDriverChains() (*iptables.ChainInfo, *iptables.ChainInfo, error) {
  241. n.Lock()
  242. defer n.Unlock()
  243. if n.driver == nil {
  244. return nil, nil, types.BadRequestErrorf("no driver found")
  245. }
  246. return n.driver.natChain, n.driver.filterChain, nil
  247. }
  248. func (n *bridgeNetwork) getNetworkBridgeName() string {
  249. n.Lock()
  250. config := n.config
  251. n.Unlock()
  252. return config.BridgeName
  253. }
  254. func (n *bridgeNetwork) getEndpoint(eid string) (*bridgeEndpoint, error) {
  255. n.Lock()
  256. defer n.Unlock()
  257. if eid == "" {
  258. return nil, InvalidEndpointIDError(eid)
  259. }
  260. if ep, ok := n.endpoints[eid]; ok {
  261. return ep, nil
  262. }
  263. return nil, nil
  264. }
  265. // Install/Removes the iptables rules needed to isolate this network
  266. // from each of the other networks
  267. func (n *bridgeNetwork) isolateNetwork(others []*bridgeNetwork, enable bool) error {
  268. n.Lock()
  269. thisV4 := n.bridge.bridgeIPv4
  270. thisV6 := getV6Network(n.config, n.bridge)
  271. n.Unlock()
  272. // Install the rules to isolate this networks against each of the other networks
  273. for _, o := range others {
  274. o.Lock()
  275. otherV4 := o.bridge.bridgeIPv4
  276. otherV6 := getV6Network(o.config, o.bridge)
  277. o.Unlock()
  278. if !types.CompareIPNet(thisV4, otherV4) {
  279. // It's ok to pass a.b.c.d/x, iptables will ignore the host subnet bits
  280. if err := setINC(thisV4.String(), otherV4.String(), enable); err != nil {
  281. return err
  282. }
  283. }
  284. if thisV6 != nil && otherV6 != nil && !types.CompareIPNet(thisV6, otherV6) {
  285. if err := setINC(thisV6.String(), otherV6.String(), enable); err != nil {
  286. return err
  287. }
  288. }
  289. }
  290. return nil
  291. }
  292. // Checks whether this network's configuration for the network with this id conflicts with any of the passed networks
  293. func (c *networkConfiguration) conflictsWithNetworks(id string, others []*bridgeNetwork) error {
  294. for _, nw := range others {
  295. nw.Lock()
  296. nwID := nw.id
  297. nwConfig := nw.config
  298. nwBridge := nw.bridge
  299. nw.Unlock()
  300. if nwID == id {
  301. continue
  302. }
  303. // Verify the name (which may have been set by newInterface()) does not conflict with
  304. // existing bridge interfaces. Ironically the system chosen name gets stored in the config...
  305. // Basically we are checking if the two original configs were both empty.
  306. if nwConfig.BridgeName == c.BridgeName {
  307. return types.ForbiddenErrorf("conflicts with network %s (%s) by bridge name", nwID, nwConfig.BridgeName)
  308. }
  309. // If this network config specifies the AddressIPv4, we need
  310. // to make sure it does not conflict with any previously allocated
  311. // bridges. This could not be completely caught by the config conflict
  312. // check, because networks which config does not specify the AddressIPv4
  313. // get their address and subnet selected by the driver (see electBridgeIPv4())
  314. if c.AddressIPv4 != nil {
  315. if nwBridge.bridgeIPv4.Contains(c.AddressIPv4.IP) ||
  316. c.AddressIPv4.Contains(nwBridge.bridgeIPv4.IP) {
  317. return types.ForbiddenErrorf("conflicts with network %s (%s) by ip network", nwID, nwConfig.BridgeName)
  318. }
  319. }
  320. }
  321. return nil
  322. }
  323. func (d *driver) configure(option map[string]interface{}) error {
  324. var config *configuration
  325. var err error
  326. d.Lock()
  327. defer d.Unlock()
  328. genericData, ok := option[netlabel.GenericData]
  329. if !ok || genericData == nil {
  330. return nil
  331. }
  332. switch opt := genericData.(type) {
  333. case options.Generic:
  334. opaqueConfig, err := options.GenerateFromModel(opt, &configuration{})
  335. if err != nil {
  336. return err
  337. }
  338. config = opaqueConfig.(*configuration)
  339. case *configuration:
  340. config = opt
  341. default:
  342. return &ErrInvalidDriverConfig{}
  343. }
  344. if config.EnableIPForwarding {
  345. err = setupIPForwarding()
  346. if err != nil {
  347. return err
  348. }
  349. }
  350. if config.EnableIPTables {
  351. d.natChain, d.filterChain, err = setupIPChains(config)
  352. if err != nil {
  353. return err
  354. }
  355. }
  356. d.config = config
  357. return nil
  358. }
  359. func (d *driver) getNetwork(id string) (*bridgeNetwork, error) {
  360. d.Lock()
  361. defer d.Unlock()
  362. if id == "" {
  363. return nil, types.BadRequestErrorf("invalid network id: %s", id)
  364. }
  365. if nw, ok := d.networks[id]; ok {
  366. return nw, nil
  367. }
  368. return nil, types.NotFoundErrorf("network not found: %s", id)
  369. }
  370. func parseNetworkGenericOptions(data interface{}) (*networkConfiguration, error) {
  371. var (
  372. err error
  373. config *networkConfiguration
  374. )
  375. switch opt := data.(type) {
  376. case *networkConfiguration:
  377. config = opt
  378. case map[string]interface{}:
  379. config = &networkConfiguration{
  380. EnableICC: true,
  381. EnableIPMasquerade: true,
  382. }
  383. err = config.fromMap(opt)
  384. case options.Generic:
  385. var opaqueConfig interface{}
  386. if opaqueConfig, err = options.GenerateFromModel(opt, config); err == nil {
  387. config = opaqueConfig.(*networkConfiguration)
  388. }
  389. default:
  390. err = types.BadRequestErrorf("do not recognize network configuration format: %T", opt)
  391. }
  392. return config, err
  393. }
  394. func (c *networkConfiguration) processIPAM(id string, ipamV4Data, ipamV6Data []driverapi.IPAMData) error {
  395. if len(ipamV4Data) > 1 || len(ipamV6Data) > 1 {
  396. return types.ForbiddenErrorf("bridge driver doesnt support multiple subnets")
  397. }
  398. if len(ipamV4Data) == 0 {
  399. return types.BadRequestErrorf("bridge network %s requires ipv4 configuration", id)
  400. }
  401. if ipamV4Data[0].Gateway != nil {
  402. c.AddressIPv4 = types.GetIPNetCopy(ipamV4Data[0].Gateway)
  403. }
  404. if gw, ok := ipamV4Data[0].AuxAddresses[DefaultGatewayV4AuxKey]; ok {
  405. c.DefaultGatewayIPv4 = gw.IP
  406. }
  407. if len(ipamV6Data) > 0 {
  408. if ipamV6Data[0].Gateway != nil {
  409. c.AddressIPv6 = types.GetIPNetCopy(ipamV6Data[0].Gateway)
  410. }
  411. if gw, ok := ipamV6Data[0].AuxAddresses[DefaultGatewayV6AuxKey]; ok {
  412. c.DefaultGatewayIPv6 = gw.IP
  413. }
  414. }
  415. return nil
  416. }
  417. func parseNetworkOptions(id string, option options.Generic) (*networkConfiguration, error) {
  418. var err error
  419. config := &networkConfiguration{}
  420. // Parse generic label first, config will be re-assigned
  421. if genData, ok := option[netlabel.GenericData]; ok && genData != nil {
  422. if config, err = parseNetworkGenericOptions(genData); err != nil {
  423. return nil, err
  424. }
  425. }
  426. // Process well-known labels next
  427. if _, ok := option[netlabel.EnableIPv6]; ok {
  428. config.EnableIPv6 = option[netlabel.EnableIPv6].(bool)
  429. }
  430. // Finally validate the configuration
  431. if err = config.Validate(); err != nil {
  432. return nil, err
  433. }
  434. if config.BridgeName == "" && config.DefaultBridge == false {
  435. config.BridgeName = "br-" + id[:12]
  436. }
  437. return config, nil
  438. }
  439. // Returns the non link-local IPv6 subnet for the containers attached to this bridge if found, nil otherwise
  440. func getV6Network(config *networkConfiguration, i *bridgeInterface) *net.IPNet {
  441. if config.AddressIPv6 != nil {
  442. return config.AddressIPv6
  443. }
  444. if i.bridgeIPv6 != nil && i.bridgeIPv6.IP != nil && !i.bridgeIPv6.IP.IsLinkLocalUnicast() {
  445. return i.bridgeIPv6
  446. }
  447. return nil
  448. }
  449. // Return a slice of networks over which caller can iterate safely
  450. func (d *driver) getNetworks() []*bridgeNetwork {
  451. d.Lock()
  452. defer d.Unlock()
  453. ls := make([]*bridgeNetwork, 0, len(d.networks))
  454. for _, nw := range d.networks {
  455. ls = append(ls, nw)
  456. }
  457. return ls
  458. }
  459. // Create a new network using bridge plugin
  460. func (d *driver) CreateNetwork(id string, option map[string]interface{}, ipV4Data, ipV6Data []driverapi.IPAMData) error {
  461. var err error
  462. defer osl.InitOSContext()()
  463. // Sanity checks
  464. d.Lock()
  465. if _, ok := d.networks[id]; ok {
  466. d.Unlock()
  467. return types.ForbiddenErrorf("network %s exists", id)
  468. }
  469. d.Unlock()
  470. // Parse and validate the config. It should not conflict with existing networks' config
  471. config, err := parseNetworkOptions(id, option)
  472. if err != nil {
  473. return err
  474. }
  475. err = config.processIPAM(id, ipV4Data, ipV6Data)
  476. if err != nil {
  477. return err
  478. }
  479. networkList := d.getNetworks()
  480. for _, nw := range networkList {
  481. nw.Lock()
  482. nwConfig := nw.config
  483. nw.Unlock()
  484. if err := nwConfig.Conflicts(config); err != nil {
  485. return types.ForbiddenErrorf("cannot create network %s (%s): conflicts with network %s (%s): %s",
  486. nwConfig.BridgeName, id, nw.id, nw.config.BridgeName, err.Error())
  487. }
  488. }
  489. // Create and set network handler in driver
  490. network := &bridgeNetwork{
  491. id: id,
  492. endpoints: make(map[string]*bridgeEndpoint),
  493. config: config,
  494. portMapper: portmapper.New(),
  495. driver: d,
  496. }
  497. d.Lock()
  498. d.networks[id] = network
  499. d.Unlock()
  500. // On failure make sure to reset driver network handler to nil
  501. defer func() {
  502. if err != nil {
  503. d.Lock()
  504. delete(d.networks, id)
  505. d.Unlock()
  506. }
  507. }()
  508. // Create or retrieve the bridge L3 interface
  509. bridgeIface := newInterface(config)
  510. network.bridge = bridgeIface
  511. // Verify the network configuration does not conflict with previously installed
  512. // networks. This step is needed now because driver might have now set the bridge
  513. // name on this config struct. And because we need to check for possible address
  514. // conflicts, so we need to check against operationa lnetworks.
  515. if err = config.conflictsWithNetworks(id, networkList); err != nil {
  516. return err
  517. }
  518. setupNetworkIsolationRules := func(config *networkConfiguration, i *bridgeInterface) error {
  519. if err := network.isolateNetwork(networkList, true); err != nil {
  520. if err := network.isolateNetwork(networkList, false); err != nil {
  521. logrus.Warnf("Failed on removing the inter-network iptables rules on cleanup: %v", err)
  522. }
  523. return err
  524. }
  525. return nil
  526. }
  527. // Prepare the bridge setup configuration
  528. bridgeSetup := newBridgeSetup(config, bridgeIface)
  529. // If the bridge interface doesn't exist, we need to start the setup steps
  530. // by creating a new device and assigning it an IPv4 address.
  531. bridgeAlreadyExists := bridgeIface.exists()
  532. if !bridgeAlreadyExists {
  533. bridgeSetup.queueStep(setupDevice)
  534. }
  535. // Even if a bridge exists try to setup IPv4.
  536. bridgeSetup.queueStep(setupBridgeIPv4)
  537. enableIPv6Forwarding := d.config.EnableIPForwarding && config.AddressIPv6 != nil
  538. // Conditionally queue setup steps depending on configuration values.
  539. for _, step := range []struct {
  540. Condition bool
  541. Fn setupStep
  542. }{
  543. // Enable IPv6 on the bridge if required. We do this even for a
  544. // previously existing bridge, as it may be here from a previous
  545. // installation where IPv6 wasn't supported yet and needs to be
  546. // assigned an IPv6 link-local address.
  547. {config.EnableIPv6, setupBridgeIPv6},
  548. // We ensure that the bridge has the expectedIPv4 and IPv6 addresses in
  549. // the case of a previously existing device.
  550. {bridgeAlreadyExists, setupVerifyAndReconcile},
  551. // Enable IPv6 Forwarding
  552. {enableIPv6Forwarding, setupIPv6Forwarding},
  553. // Setup Loopback Adresses Routing
  554. {!d.config.EnableUserlandProxy, setupLoopbackAdressesRouting},
  555. // Setup IPTables.
  556. {d.config.EnableIPTables, network.setupIPTables},
  557. //We want to track firewalld configuration so that
  558. //if it is started/reloaded, the rules can be applied correctly
  559. {d.config.EnableIPTables, network.setupFirewalld},
  560. // Setup DefaultGatewayIPv4
  561. {config.DefaultGatewayIPv4 != nil, setupGatewayIPv4},
  562. // Setup DefaultGatewayIPv6
  563. {config.DefaultGatewayIPv6 != nil, setupGatewayIPv6},
  564. // Add inter-network communication rules.
  565. {d.config.EnableIPTables, setupNetworkIsolationRules},
  566. //Configure bridge networking filtering if ICC is off and IP tables are enabled
  567. {!config.EnableICC && d.config.EnableIPTables, setupBridgeNetFiltering},
  568. } {
  569. if step.Condition {
  570. bridgeSetup.queueStep(step.Fn)
  571. }
  572. }
  573. // Apply the prepared list of steps, and abort at the first error.
  574. bridgeSetup.queueStep(setupDeviceUp)
  575. if err = bridgeSetup.apply(); err != nil {
  576. return err
  577. }
  578. return nil
  579. }
  580. func (d *driver) DeleteNetwork(nid string) error {
  581. var err error
  582. defer osl.InitOSContext()()
  583. // Get network handler and remove it from driver
  584. d.Lock()
  585. n, ok := d.networks[nid]
  586. d.Unlock()
  587. if !ok {
  588. return types.InternalMaskableErrorf("network %s does not exist", nid)
  589. }
  590. n.Lock()
  591. config := n.config
  592. n.Unlock()
  593. if config.BridgeName == DefaultBridgeName {
  594. return types.ForbiddenErrorf("default network of type \"%s\" cannot be deleted", networkType)
  595. }
  596. d.Lock()
  597. delete(d.networks, nid)
  598. d.Unlock()
  599. // On failure set network handler back in driver, but
  600. // only if is not already taken over by some other thread
  601. defer func() {
  602. if err != nil {
  603. d.Lock()
  604. if _, ok := d.networks[nid]; !ok {
  605. d.networks[nid] = n
  606. }
  607. d.Unlock()
  608. }
  609. }()
  610. // Sanity check
  611. if n == nil {
  612. err = driverapi.ErrNoNetwork(nid)
  613. return err
  614. }
  615. // Cannot remove network if endpoints are still present
  616. if len(n.endpoints) != 0 {
  617. err = ActiveEndpointsError(n.id)
  618. return err
  619. }
  620. // In case of failures after this point, restore the network isolation rules
  621. nwList := d.getNetworks()
  622. defer func() {
  623. if err != nil {
  624. if err := n.isolateNetwork(nwList, true); err != nil {
  625. logrus.Warnf("Failed on restoring the inter-network iptables rules on cleanup: %v", err)
  626. }
  627. }
  628. }()
  629. // Remove inter-network communication rules.
  630. err = n.isolateNetwork(nwList, false)
  631. if err != nil {
  632. return err
  633. }
  634. // Programming
  635. return netlink.LinkDel(n.bridge.Link)
  636. }
  637. func addToBridge(ifaceName, bridgeName string) error {
  638. link, err := netlink.LinkByName(ifaceName)
  639. if err != nil {
  640. return fmt.Errorf("could not find interface %s: %v", ifaceName, err)
  641. }
  642. if err = netlink.LinkSetMaster(link,
  643. &netlink.Bridge{LinkAttrs: netlink.LinkAttrs{Name: bridgeName}}); err != nil {
  644. logrus.Debugf("Failed to add %s to bridge via netlink.Trying ioctl: %v", ifaceName, err)
  645. iface, err := net.InterfaceByName(ifaceName)
  646. if err != nil {
  647. return fmt.Errorf("could not find network interface %s: %v", ifaceName, err)
  648. }
  649. master, err := net.InterfaceByName(bridgeName)
  650. if err != nil {
  651. return fmt.Errorf("could not find bridge %s: %v", bridgeName, err)
  652. }
  653. return ioctlAddToBridge(iface, master)
  654. }
  655. return nil
  656. }
  657. func setHairpinMode(link netlink.Link, enable bool) error {
  658. err := netlink.LinkSetHairpin(link, enable)
  659. if err != nil && err != syscall.EINVAL {
  660. // If error is not EINVAL something else went wrong, bail out right away
  661. return fmt.Errorf("unable to set hairpin mode on %s via netlink: %v",
  662. link.Attrs().Name, err)
  663. }
  664. // Hairpin mode successfully set up
  665. if err == nil {
  666. return nil
  667. }
  668. // The netlink method failed with EINVAL which is probably because of an older
  669. // kernel. Try one more time via the sysfs method.
  670. path := filepath.Join("/sys/class/net", link.Attrs().Name, "brport/hairpin_mode")
  671. var val []byte
  672. if enable {
  673. val = []byte{'1', '\n'}
  674. } else {
  675. val = []byte{'0', '\n'}
  676. }
  677. if err := ioutil.WriteFile(path, val, 0644); err != nil {
  678. return fmt.Errorf("unable to set hairpin mode on %s via sysfs: %v", link.Attrs().Name, err)
  679. }
  680. return nil
  681. }
  682. func (d *driver) CreateEndpoint(nid, eid string, ifInfo driverapi.InterfaceInfo, epOptions map[string]interface{}) error {
  683. defer osl.InitOSContext()()
  684. if ifInfo == nil {
  685. return errors.New("invalid interface info passed")
  686. }
  687. // Get the network handler and make sure it exists
  688. d.Lock()
  689. n, ok := d.networks[nid]
  690. dconfig := d.config
  691. d.Unlock()
  692. if !ok {
  693. return types.NotFoundErrorf("network %s does not exist", nid)
  694. }
  695. if n == nil {
  696. return driverapi.ErrNoNetwork(nid)
  697. }
  698. // Sanity check
  699. n.Lock()
  700. if n.id != nid {
  701. n.Unlock()
  702. return InvalidNetworkIDError(nid)
  703. }
  704. n.Unlock()
  705. // Check if endpoint id is good and retrieve correspondent endpoint
  706. ep, err := n.getEndpoint(eid)
  707. if err != nil {
  708. return err
  709. }
  710. // Endpoint with that id exists either on desired or other sandbox
  711. if ep != nil {
  712. return driverapi.ErrEndpointExists(eid)
  713. }
  714. // Try to convert the options to endpoint configuration
  715. epConfig, err := parseEndpointOptions(epOptions)
  716. if err != nil {
  717. return err
  718. }
  719. // Create and add the endpoint
  720. n.Lock()
  721. endpoint := &bridgeEndpoint{id: eid, config: epConfig}
  722. n.endpoints[eid] = endpoint
  723. n.Unlock()
  724. // On failure make sure to remove the endpoint
  725. defer func() {
  726. if err != nil {
  727. n.Lock()
  728. delete(n.endpoints, eid)
  729. n.Unlock()
  730. }
  731. }()
  732. // Generate a name for what will be the host side pipe interface
  733. hostIfName, err := netutils.GenerateIfaceName(vethPrefix, vethLen)
  734. if err != nil {
  735. return err
  736. }
  737. // Generate a name for what will be the sandbox side pipe interface
  738. containerIfName, err := netutils.GenerateIfaceName(vethPrefix, vethLen)
  739. if err != nil {
  740. return err
  741. }
  742. // Generate and add the interface pipe host <-> sandbox
  743. veth := &netlink.Veth{
  744. LinkAttrs: netlink.LinkAttrs{Name: hostIfName, TxQLen: 0},
  745. PeerName: containerIfName}
  746. if err = netlink.LinkAdd(veth); err != nil {
  747. return types.InternalErrorf("failed to add the host (%s) <=> sandbox (%s) pair interfaces: %v", hostIfName, containerIfName, err)
  748. }
  749. // Get the host side pipe interface handler
  750. host, err := netlink.LinkByName(hostIfName)
  751. if err != nil {
  752. return types.InternalErrorf("failed to find host side interface %s: %v", hostIfName, err)
  753. }
  754. defer func() {
  755. if err != nil {
  756. netlink.LinkDel(host)
  757. }
  758. }()
  759. // Get the sandbox side pipe interface handler
  760. sbox, err := netlink.LinkByName(containerIfName)
  761. if err != nil {
  762. return types.InternalErrorf("failed to find sandbox side interface %s: %v", containerIfName, err)
  763. }
  764. defer func() {
  765. if err != nil {
  766. netlink.LinkDel(sbox)
  767. }
  768. }()
  769. n.Lock()
  770. config := n.config
  771. n.Unlock()
  772. // Add bridge inherited attributes to pipe interfaces
  773. if config.Mtu != 0 {
  774. err = netlink.LinkSetMTU(host, config.Mtu)
  775. if err != nil {
  776. return types.InternalErrorf("failed to set MTU on host interface %s: %v", hostIfName, err)
  777. }
  778. err = netlink.LinkSetMTU(sbox, config.Mtu)
  779. if err != nil {
  780. return types.InternalErrorf("failed to set MTU on sandbox interface %s: %v", containerIfName, err)
  781. }
  782. }
  783. // Attach host side pipe interface into the bridge
  784. if err = addToBridge(hostIfName, config.BridgeName); err != nil {
  785. return fmt.Errorf("adding interface %s to bridge %s failed: %v", hostIfName, config.BridgeName, err)
  786. }
  787. if !dconfig.EnableUserlandProxy {
  788. err = setHairpinMode(host, true)
  789. if err != nil {
  790. return err
  791. }
  792. }
  793. // Create the sandbox side pipe interface
  794. endpoint.srcName = containerIfName
  795. endpoint.macAddress = ifInfo.MacAddress()
  796. endpoint.addr = ifInfo.Address()
  797. endpoint.addrv6 = ifInfo.AddressIPv6()
  798. // Down the interface before configuring mac address.
  799. if err = netlink.LinkSetDown(sbox); err != nil {
  800. return fmt.Errorf("could not set link down for container interface %s: %v", containerIfName, err)
  801. }
  802. // Set the sbox's MAC. If specified, use the one configured by user, otherwise generate one based on IP.
  803. if endpoint.macAddress == nil {
  804. endpoint.macAddress = electMacAddress(epConfig, endpoint.addr.IP)
  805. if err := ifInfo.SetMacAddress(endpoint.macAddress); err != nil {
  806. return err
  807. }
  808. }
  809. err = netlink.LinkSetHardwareAddr(sbox, endpoint.macAddress)
  810. if err != nil {
  811. return fmt.Errorf("could not set mac address for container interface %s: %v", containerIfName, err)
  812. }
  813. // Up the host interface after finishing all netlink configuration
  814. if err = netlink.LinkSetUp(host); err != nil {
  815. return fmt.Errorf("could not set link up for host interface %s: %v", hostIfName, err)
  816. }
  817. if endpoint.addrv6 == nil && config.EnableIPv6 {
  818. var ip6 net.IP
  819. network := n.bridge.bridgeIPv6
  820. ones, _ := network.Mask.Size()
  821. if ones <= 80 {
  822. ip6 = make(net.IP, len(network.IP))
  823. copy(ip6, network.IP)
  824. for i, h := range endpoint.macAddress {
  825. ip6[i+10] = h
  826. }
  827. }
  828. endpoint.addrv6 = &net.IPNet{IP: ip6, Mask: network.Mask}
  829. if err := ifInfo.SetIPAddress(endpoint.addrv6); err != nil {
  830. return err
  831. }
  832. }
  833. // Program any required port mapping and store them in the endpoint
  834. endpoint.portMapping, err = n.allocatePorts(epConfig, endpoint, config.DefaultBindingIP, d.config.EnableUserlandProxy)
  835. if err != nil {
  836. return err
  837. }
  838. return nil
  839. }
  840. func (d *driver) DeleteEndpoint(nid, eid string) error {
  841. var err error
  842. defer osl.InitOSContext()()
  843. // Get the network handler and make sure it exists
  844. d.Lock()
  845. n, ok := d.networks[nid]
  846. d.Unlock()
  847. if !ok {
  848. return types.NotFoundErrorf("network %s does not exist", nid)
  849. }
  850. if n == nil {
  851. return driverapi.ErrNoNetwork(nid)
  852. }
  853. // Sanity Check
  854. n.Lock()
  855. if n.id != nid {
  856. n.Unlock()
  857. return InvalidNetworkIDError(nid)
  858. }
  859. n.Unlock()
  860. // Check endpoint id and if an endpoint is actually there
  861. ep, err := n.getEndpoint(eid)
  862. if err != nil {
  863. return err
  864. }
  865. if ep == nil {
  866. return EndpointNotFoundError(eid)
  867. }
  868. // Remove it
  869. n.Lock()
  870. delete(n.endpoints, eid)
  871. n.Unlock()
  872. // On failure make sure to set back ep in n.endpoints, but only
  873. // if it hasn't been taken over already by some other thread.
  874. defer func() {
  875. if err != nil {
  876. n.Lock()
  877. if _, ok := n.endpoints[eid]; !ok {
  878. n.endpoints[eid] = ep
  879. }
  880. n.Unlock()
  881. }
  882. }()
  883. // Remove port mappings. Do not stop endpoint delete on unmap failure
  884. n.releasePorts(ep)
  885. // Release the v4 address allocated to this endpoint's sandbox interface
  886. err = ipAllocator.ReleaseIP(n.bridge.bridgeIPv4, ep.addr.IP)
  887. if err != nil {
  888. return err
  889. }
  890. // Try removal of link. Discard error: link pair might have
  891. // already been deleted by sandbox delete. Make sure defer
  892. // does not see this error either.
  893. if link, err := netlink.LinkByName(ep.srcName); err == nil {
  894. netlink.LinkDel(link)
  895. }
  896. return nil
  897. }
  898. func (d *driver) EndpointOperInfo(nid, eid string) (map[string]interface{}, error) {
  899. // Get the network handler and make sure it exists
  900. d.Lock()
  901. n, ok := d.networks[nid]
  902. d.Unlock()
  903. if !ok {
  904. return nil, types.NotFoundErrorf("network %s does not exist", nid)
  905. }
  906. if n == nil {
  907. return nil, driverapi.ErrNoNetwork(nid)
  908. }
  909. // Sanity check
  910. n.Lock()
  911. if n.id != nid {
  912. n.Unlock()
  913. return nil, InvalidNetworkIDError(nid)
  914. }
  915. n.Unlock()
  916. // Check if endpoint id is good and retrieve correspondent endpoint
  917. ep, err := n.getEndpoint(eid)
  918. if err != nil {
  919. return nil, err
  920. }
  921. if ep == nil {
  922. return nil, driverapi.ErrNoEndpoint(eid)
  923. }
  924. m := make(map[string]interface{})
  925. if ep.config.ExposedPorts != nil {
  926. // Return a copy of the config data
  927. epc := make([]types.TransportPort, 0, len(ep.config.ExposedPorts))
  928. for _, tp := range ep.config.ExposedPorts {
  929. epc = append(epc, tp.GetCopy())
  930. }
  931. m[netlabel.ExposedPorts] = epc
  932. }
  933. if ep.portMapping != nil {
  934. // Return a copy of the operational data
  935. pmc := make([]types.PortBinding, 0, len(ep.portMapping))
  936. for _, pm := range ep.portMapping {
  937. pmc = append(pmc, pm.GetCopy())
  938. }
  939. m[netlabel.PortMap] = pmc
  940. }
  941. if len(ep.macAddress) != 0 {
  942. m[netlabel.MacAddress] = ep.macAddress
  943. }
  944. return m, nil
  945. }
  946. // Join method is invoked when a Sandbox is attached to an endpoint.
  947. func (d *driver) Join(nid, eid string, sboxKey string, jinfo driverapi.JoinInfo, options map[string]interface{}) error {
  948. defer osl.InitOSContext()()
  949. network, err := d.getNetwork(nid)
  950. if err != nil {
  951. return err
  952. }
  953. endpoint, err := network.getEndpoint(eid)
  954. if err != nil {
  955. return err
  956. }
  957. if endpoint == nil {
  958. return EndpointNotFoundError(eid)
  959. }
  960. iNames := jinfo.InterfaceName()
  961. err = iNames.SetNames(endpoint.srcName, containerVethPrefix)
  962. if err != nil {
  963. return err
  964. }
  965. err = jinfo.SetGateway(network.bridge.gatewayIPv4)
  966. if err != nil {
  967. return err
  968. }
  969. err = jinfo.SetGatewayIPv6(network.bridge.gatewayIPv6)
  970. if err != nil {
  971. return err
  972. }
  973. if !network.config.EnableICC {
  974. return d.link(network, endpoint, options, true)
  975. }
  976. return nil
  977. }
  978. // Leave method is invoked when a Sandbox detaches from an endpoint.
  979. func (d *driver) Leave(nid, eid string) error {
  980. defer osl.InitOSContext()()
  981. network, err := d.getNetwork(nid)
  982. if err != nil {
  983. return err
  984. }
  985. endpoint, err := network.getEndpoint(eid)
  986. if err != nil {
  987. return err
  988. }
  989. if endpoint == nil {
  990. return EndpointNotFoundError(eid)
  991. }
  992. if !network.config.EnableICC {
  993. return d.link(network, endpoint, nil, false)
  994. }
  995. return nil
  996. }
  997. func (d *driver) link(network *bridgeNetwork, endpoint *bridgeEndpoint, options map[string]interface{}, enable bool) error {
  998. var (
  999. cc *containerConfiguration
  1000. err error
  1001. )
  1002. if enable {
  1003. cc, err = parseContainerOptions(options)
  1004. if err != nil {
  1005. return err
  1006. }
  1007. } else {
  1008. cc = endpoint.containerConfig
  1009. }
  1010. if cc == nil {
  1011. return nil
  1012. }
  1013. if endpoint.config != nil && endpoint.config.ExposedPorts != nil {
  1014. for _, p := range cc.ParentEndpoints {
  1015. var parentEndpoint *bridgeEndpoint
  1016. parentEndpoint, err = network.getEndpoint(p)
  1017. if err != nil {
  1018. return err
  1019. }
  1020. if parentEndpoint == nil {
  1021. err = InvalidEndpointIDError(p)
  1022. return err
  1023. }
  1024. l := newLink(parentEndpoint.addr.IP.String(),
  1025. endpoint.addr.IP.String(),
  1026. endpoint.config.ExposedPorts, network.config.BridgeName)
  1027. if enable {
  1028. err = l.Enable()
  1029. if err != nil {
  1030. return err
  1031. }
  1032. defer func() {
  1033. if err != nil {
  1034. l.Disable()
  1035. }
  1036. }()
  1037. } else {
  1038. l.Disable()
  1039. }
  1040. }
  1041. }
  1042. for _, c := range cc.ChildEndpoints {
  1043. var childEndpoint *bridgeEndpoint
  1044. childEndpoint, err = network.getEndpoint(c)
  1045. if err != nil {
  1046. return err
  1047. }
  1048. if childEndpoint == nil {
  1049. err = InvalidEndpointIDError(c)
  1050. return err
  1051. }
  1052. if childEndpoint.config == nil || childEndpoint.config.ExposedPorts == nil {
  1053. continue
  1054. }
  1055. l := newLink(endpoint.addr.IP.String(),
  1056. childEndpoint.addr.IP.String(),
  1057. childEndpoint.config.ExposedPorts, network.config.BridgeName)
  1058. if enable {
  1059. err = l.Enable()
  1060. if err != nil {
  1061. return err
  1062. }
  1063. defer func() {
  1064. if err != nil {
  1065. l.Disable()
  1066. }
  1067. }()
  1068. } else {
  1069. l.Disable()
  1070. }
  1071. }
  1072. if enable {
  1073. endpoint.containerConfig = cc
  1074. }
  1075. return nil
  1076. }
  1077. func (d *driver) Type() string {
  1078. return networkType
  1079. }
  1080. // DiscoverNew is a notification for a new discovery event, such as a new node joining a cluster
  1081. func (d *driver) DiscoverNew(dType driverapi.DiscoveryType, data interface{}) error {
  1082. return nil
  1083. }
  1084. // DiscoverDelete is a notification for a discovery delete event, such as a node leaving a cluster
  1085. func (d *driver) DiscoverDelete(dType driverapi.DiscoveryType, data interface{}) error {
  1086. return nil
  1087. }
  1088. func parseEndpointOptions(epOptions map[string]interface{}) (*endpointConfiguration, error) {
  1089. if epOptions == nil {
  1090. return nil, nil
  1091. }
  1092. ec := &endpointConfiguration{}
  1093. if opt, ok := epOptions[netlabel.MacAddress]; ok {
  1094. if mac, ok := opt.(net.HardwareAddr); ok {
  1095. ec.MacAddress = mac
  1096. } else {
  1097. return nil, &ErrInvalidEndpointConfig{}
  1098. }
  1099. }
  1100. if opt, ok := epOptions[netlabel.PortMap]; ok {
  1101. if bs, ok := opt.([]types.PortBinding); ok {
  1102. ec.PortBindings = bs
  1103. } else {
  1104. return nil, &ErrInvalidEndpointConfig{}
  1105. }
  1106. }
  1107. if opt, ok := epOptions[netlabel.ExposedPorts]; ok {
  1108. if ports, ok := opt.([]types.TransportPort); ok {
  1109. ec.ExposedPorts = ports
  1110. } else {
  1111. return nil, &ErrInvalidEndpointConfig{}
  1112. }
  1113. }
  1114. return ec, nil
  1115. }
  1116. func parseContainerOptions(cOptions map[string]interface{}) (*containerConfiguration, error) {
  1117. if cOptions == nil {
  1118. return nil, nil
  1119. }
  1120. genericData := cOptions[netlabel.GenericData]
  1121. if genericData == nil {
  1122. return nil, nil
  1123. }
  1124. switch opt := genericData.(type) {
  1125. case options.Generic:
  1126. opaqueConfig, err := options.GenerateFromModel(opt, &containerConfiguration{})
  1127. if err != nil {
  1128. return nil, err
  1129. }
  1130. return opaqueConfig.(*containerConfiguration), nil
  1131. case *containerConfiguration:
  1132. return opt, nil
  1133. default:
  1134. return nil, nil
  1135. }
  1136. }
  1137. func electMacAddress(epConfig *endpointConfiguration, ip net.IP) net.HardwareAddr {
  1138. if epConfig != nil && epConfig.MacAddress != nil {
  1139. return epConfig.MacAddress
  1140. }
  1141. return netutils.GenerateMACFromIP(ip)
  1142. }