bridge.go 35 KB

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