network.go 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820
  1. package docker
  2. import (
  3. "encoding/binary"
  4. "errors"
  5. "fmt"
  6. "github.com/dotcloud/docker/pkg/iptables"
  7. "github.com/dotcloud/docker/pkg/netlink"
  8. "github.com/dotcloud/docker/proxy"
  9. "github.com/dotcloud/docker/utils"
  10. "log"
  11. "net"
  12. "strconv"
  13. "sync"
  14. "syscall"
  15. "unsafe"
  16. )
  17. const (
  18. DefaultNetworkBridge = "docker0"
  19. DisableNetworkBridge = "none"
  20. DefaultNetworkMtu = 1500
  21. portRangeStart = 49153
  22. portRangeEnd = 65535
  23. siocBRADDBR = 0x89a0
  24. )
  25. // Calculates the first and last IP addresses in an IPNet
  26. func networkRange(network *net.IPNet) (net.IP, net.IP) {
  27. netIP := network.IP.To4()
  28. firstIP := netIP.Mask(network.Mask)
  29. lastIP := net.IPv4(0, 0, 0, 0).To4()
  30. for i := 0; i < len(lastIP); i++ {
  31. lastIP[i] = netIP[i] | ^network.Mask[i]
  32. }
  33. return firstIP, lastIP
  34. }
  35. // Detects overlap between one IPNet and another
  36. func networkOverlaps(netX *net.IPNet, netY *net.IPNet) bool {
  37. firstIP, _ := networkRange(netX)
  38. if netY.Contains(firstIP) {
  39. return true
  40. }
  41. firstIP, _ = networkRange(netY)
  42. if netX.Contains(firstIP) {
  43. return true
  44. }
  45. return false
  46. }
  47. // Converts a 4 bytes IP into a 32 bit integer
  48. func ipToInt(ip net.IP) int32 {
  49. return int32(binary.BigEndian.Uint32(ip.To4()))
  50. }
  51. // Converts 32 bit integer into a 4 bytes IP address
  52. func intToIP(n int32) net.IP {
  53. b := make([]byte, 4)
  54. binary.BigEndian.PutUint32(b, uint32(n))
  55. return net.IP(b)
  56. }
  57. // Given a netmask, calculates the number of available hosts
  58. func networkSize(mask net.IPMask) int32 {
  59. m := net.IPv4Mask(0, 0, 0, 0)
  60. for i := 0; i < net.IPv4len; i++ {
  61. m[i] = ^mask[i]
  62. }
  63. return int32(binary.BigEndian.Uint32(m)) + 1
  64. }
  65. func checkRouteOverlaps(networks []*net.IPNet, dockerNetwork *net.IPNet) error {
  66. for _, network := range networks {
  67. if networkOverlaps(dockerNetwork, network) {
  68. return fmt.Errorf("Network %s is already routed: '%s'", dockerNetwork, network)
  69. }
  70. }
  71. return nil
  72. }
  73. func checkNameserverOverlaps(nameservers []string, dockerNetwork *net.IPNet) error {
  74. if len(nameservers) > 0 {
  75. for _, ns := range nameservers {
  76. _, nsNetwork, err := net.ParseCIDR(ns)
  77. if err != nil {
  78. return err
  79. }
  80. if networkOverlaps(dockerNetwork, nsNetwork) {
  81. return fmt.Errorf("%s overlaps nameserver %s", dockerNetwork, nsNetwork)
  82. }
  83. }
  84. }
  85. return nil
  86. }
  87. // CreateBridgeIface creates a network bridge interface on the host system with the name `ifaceName`,
  88. // and attempts to configure it with an address which doesn't conflict with any other interface on the host.
  89. // If it can't find an address which doesn't conflict, it will return an error.
  90. func CreateBridgeIface(config *DaemonConfig) error {
  91. addrs := []string{
  92. // Here we don't follow the convention of using the 1st IP of the range for the gateway.
  93. // This is to use the same gateway IPs as the /24 ranges, which predate the /16 ranges.
  94. // In theory this shouldn't matter - in practice there's bound to be a few scripts relying
  95. // on the internal addressing or other stupid things like that.
  96. // The shouldn't, but hey, let's not break them unless we really have to.
  97. "172.17.42.1/16", // Don't use 172.16.0.0/16, it conflicts with EC2 DNS 172.16.0.23
  98. "10.0.42.1/16", // Don't even try using the entire /8, that's too intrusive
  99. "10.1.42.1/16",
  100. "10.42.42.1/16",
  101. "172.16.42.1/24",
  102. "172.16.43.1/24",
  103. "172.16.44.1/24",
  104. "10.0.42.1/24",
  105. "10.0.43.1/24",
  106. "192.168.42.1/24",
  107. "192.168.43.1/24",
  108. "192.168.44.1/24",
  109. }
  110. nameservers := []string{}
  111. resolvConf, _ := utils.GetResolvConf()
  112. // we don't check for an error here, because we don't really care
  113. // if we can't read /etc/resolv.conf. So instead we skip the append
  114. // if resolvConf is nil. It either doesn't exist, or we can't read it
  115. // for some reason.
  116. if resolvConf != nil {
  117. nameservers = append(nameservers, utils.GetNameserversAsCIDR(resolvConf)...)
  118. }
  119. var ifaceAddr string
  120. if len(config.BridgeIp) != 0 {
  121. _, _, err := net.ParseCIDR(config.BridgeIp)
  122. if err != nil {
  123. return err
  124. }
  125. ifaceAddr = config.BridgeIp
  126. } else {
  127. for _, addr := range addrs {
  128. _, dockerNetwork, err := net.ParseCIDR(addr)
  129. if err != nil {
  130. return err
  131. }
  132. routes, err := netlink.NetworkGetRoutes()
  133. if err != nil {
  134. return err
  135. }
  136. if err := checkRouteOverlaps(routes, dockerNetwork); err == nil {
  137. if err := checkNameserverOverlaps(nameservers, dockerNetwork); err == nil {
  138. ifaceAddr = addr
  139. break
  140. }
  141. } else {
  142. utils.Debugf("%s: %s", addr, err)
  143. }
  144. }
  145. }
  146. if ifaceAddr == "" {
  147. return fmt.Errorf("Could not find a free IP address range for interface '%s'. Please configure its address manually and run 'docker -b %s'", config.BridgeIface, config.BridgeIface)
  148. }
  149. utils.Debugf("Creating bridge %s with network %s", config.BridgeIface, ifaceAddr)
  150. if err := createBridgeIface(config.BridgeIface); err != nil {
  151. return err
  152. }
  153. iface, err := net.InterfaceByName(config.BridgeIface)
  154. if err != nil {
  155. return err
  156. }
  157. ipAddr, ipNet, err := net.ParseCIDR(ifaceAddr)
  158. if err != nil {
  159. return err
  160. }
  161. if netlink.NetworkLinkAddIp(iface, ipAddr, ipNet); err != nil {
  162. return fmt.Errorf("Unable to add private network: %s", err)
  163. }
  164. if err := netlink.NetworkLinkUp(iface); err != nil {
  165. return fmt.Errorf("Unable to start network bridge: %s", err)
  166. }
  167. return nil
  168. }
  169. // Create the actual bridge device. This is more backward-compatible than
  170. // netlink.NetworkLinkAdd and works on RHEL 6.
  171. func createBridgeIface(name string) error {
  172. s, err := syscall.Socket(syscall.AF_INET6, syscall.SOCK_STREAM, syscall.IPPROTO_IP)
  173. if err != nil {
  174. utils.Debugf("Bridge socket creation failed IPv6 probably not enabled: %v", err)
  175. s, err = syscall.Socket(syscall.AF_INET, syscall.SOCK_STREAM, syscall.IPPROTO_IP)
  176. if err != nil {
  177. return fmt.Errorf("Error creating bridge creation socket: %s", err)
  178. }
  179. }
  180. defer syscall.Close(s)
  181. nameBytePtr, err := syscall.BytePtrFromString(name)
  182. if err != nil {
  183. return fmt.Errorf("Error converting bridge name %s to byte array: %s", name, err)
  184. }
  185. if _, _, err := syscall.Syscall(syscall.SYS_IOCTL, uintptr(s), siocBRADDBR, uintptr(unsafe.Pointer(nameBytePtr))); err != 0 {
  186. return fmt.Errorf("Error creating bridge: %s", err)
  187. }
  188. return nil
  189. }
  190. // Return the IPv4 address of a network interface
  191. func getIfaceAddr(name string) (net.Addr, error) {
  192. iface, err := net.InterfaceByName(name)
  193. if err != nil {
  194. return nil, err
  195. }
  196. addrs, err := iface.Addrs()
  197. if err != nil {
  198. return nil, err
  199. }
  200. var addrs4 []net.Addr
  201. for _, addr := range addrs {
  202. ip := (addr.(*net.IPNet)).IP
  203. if ip4 := ip.To4(); len(ip4) == net.IPv4len {
  204. addrs4 = append(addrs4, addr)
  205. }
  206. }
  207. switch {
  208. case len(addrs4) == 0:
  209. return nil, fmt.Errorf("Interface %v has no IP addresses", name)
  210. case len(addrs4) > 1:
  211. fmt.Printf("Interface %v has more than 1 IPv4 address. Defaulting to using %v\n",
  212. name, (addrs4[0].(*net.IPNet)).IP)
  213. }
  214. return addrs4[0], nil
  215. }
  216. // Port mapper takes care of mapping external ports to containers by setting
  217. // up iptables rules.
  218. // It keeps track of all mappings and is able to unmap at will
  219. type PortMapper struct {
  220. tcpMapping map[string]*net.TCPAddr
  221. tcpProxies map[string]proxy.Proxy
  222. udpMapping map[string]*net.UDPAddr
  223. udpProxies map[string]proxy.Proxy
  224. iptables *iptables.Chain
  225. defaultIp net.IP
  226. proxyFactoryFunc func(net.Addr, net.Addr) (proxy.Proxy, error)
  227. }
  228. func (mapper *PortMapper) Map(ip net.IP, port int, backendAddr net.Addr) error {
  229. if _, isTCP := backendAddr.(*net.TCPAddr); isTCP {
  230. mapKey := (&net.TCPAddr{Port: port, IP: ip}).String()
  231. if _, exists := mapper.tcpProxies[mapKey]; exists {
  232. return fmt.Errorf("TCP Port %s is already in use", mapKey)
  233. }
  234. backendPort := backendAddr.(*net.TCPAddr).Port
  235. backendIP := backendAddr.(*net.TCPAddr).IP
  236. if mapper.iptables != nil {
  237. if err := mapper.iptables.Forward(iptables.Add, ip, port, "tcp", backendIP.String(), backendPort); err != nil {
  238. return err
  239. }
  240. }
  241. mapper.tcpMapping[mapKey] = backendAddr.(*net.TCPAddr)
  242. proxy, err := mapper.proxyFactoryFunc(&net.TCPAddr{IP: ip, Port: port}, backendAddr)
  243. if err != nil {
  244. mapper.Unmap(ip, port, "tcp")
  245. return err
  246. }
  247. mapper.tcpProxies[mapKey] = proxy
  248. go proxy.Run()
  249. } else {
  250. mapKey := (&net.UDPAddr{Port: port, IP: ip}).String()
  251. if _, exists := mapper.udpProxies[mapKey]; exists {
  252. return fmt.Errorf("UDP: Port %s is already in use", mapKey)
  253. }
  254. backendPort := backendAddr.(*net.UDPAddr).Port
  255. backendIP := backendAddr.(*net.UDPAddr).IP
  256. if mapper.iptables != nil {
  257. if err := mapper.iptables.Forward(iptables.Add, ip, port, "udp", backendIP.String(), backendPort); err != nil {
  258. return err
  259. }
  260. }
  261. mapper.udpMapping[mapKey] = backendAddr.(*net.UDPAddr)
  262. proxy, err := mapper.proxyFactoryFunc(&net.UDPAddr{IP: ip, Port: port}, backendAddr)
  263. if err != nil {
  264. mapper.Unmap(ip, port, "udp")
  265. return err
  266. }
  267. mapper.udpProxies[mapKey] = proxy
  268. go proxy.Run()
  269. }
  270. return nil
  271. }
  272. func (mapper *PortMapper) Unmap(ip net.IP, port int, proto string) error {
  273. if proto == "tcp" {
  274. mapKey := (&net.TCPAddr{Port: port, IP: ip}).String()
  275. backendAddr, ok := mapper.tcpMapping[mapKey]
  276. if !ok {
  277. return fmt.Errorf("Port tcp/%s is not mapped", mapKey)
  278. }
  279. if proxy, exists := mapper.tcpProxies[mapKey]; exists {
  280. proxy.Close()
  281. delete(mapper.tcpProxies, mapKey)
  282. }
  283. if mapper.iptables != nil {
  284. if err := mapper.iptables.Forward(iptables.Delete, ip, port, proto, backendAddr.IP.String(), backendAddr.Port); err != nil {
  285. return err
  286. }
  287. }
  288. delete(mapper.tcpMapping, mapKey)
  289. } else {
  290. mapKey := (&net.UDPAddr{Port: port, IP: ip}).String()
  291. backendAddr, ok := mapper.udpMapping[mapKey]
  292. if !ok {
  293. return fmt.Errorf("Port udp/%s is not mapped", mapKey)
  294. }
  295. if proxy, exists := mapper.udpProxies[mapKey]; exists {
  296. proxy.Close()
  297. delete(mapper.udpProxies, mapKey)
  298. }
  299. if mapper.iptables != nil {
  300. if err := mapper.iptables.Forward(iptables.Delete, ip, port, proto, backendAddr.IP.String(), backendAddr.Port); err != nil {
  301. return err
  302. }
  303. }
  304. delete(mapper.udpMapping, mapKey)
  305. }
  306. return nil
  307. }
  308. func newPortMapper(config *DaemonConfig) (*PortMapper, error) {
  309. // We can always try removing the iptables
  310. if err := iptables.RemoveExistingChain("DOCKER"); err != nil {
  311. return nil, err
  312. }
  313. var chain *iptables.Chain
  314. if config.EnableIptables {
  315. var err error
  316. chain, err = iptables.NewChain("DOCKER", config.BridgeIface)
  317. if err != nil {
  318. return nil, fmt.Errorf("Failed to create DOCKER chain: %s", err)
  319. }
  320. }
  321. mapper := &PortMapper{
  322. tcpMapping: make(map[string]*net.TCPAddr),
  323. tcpProxies: make(map[string]proxy.Proxy),
  324. udpMapping: make(map[string]*net.UDPAddr),
  325. udpProxies: make(map[string]proxy.Proxy),
  326. iptables: chain,
  327. defaultIp: config.DefaultIp,
  328. proxyFactoryFunc: proxy.NewProxy,
  329. }
  330. return mapper, nil
  331. }
  332. // Port allocator: Automatically allocate and release networking ports
  333. type PortAllocator struct {
  334. sync.Mutex
  335. inUse map[string]struct{}
  336. fountain chan int
  337. quit chan bool
  338. }
  339. func (alloc *PortAllocator) runFountain() {
  340. for {
  341. for port := portRangeStart; port < portRangeEnd; port++ {
  342. select {
  343. case alloc.fountain <- port:
  344. case quit := <-alloc.quit:
  345. if quit {
  346. return
  347. }
  348. }
  349. }
  350. }
  351. }
  352. // FIXME: Release can no longer fail, change its prototype to reflect that.
  353. func (alloc *PortAllocator) Release(addr net.IP, port int) error {
  354. mapKey := (&net.TCPAddr{Port: port, IP: addr}).String()
  355. utils.Debugf("Releasing %d", port)
  356. alloc.Lock()
  357. delete(alloc.inUse, mapKey)
  358. alloc.Unlock()
  359. return nil
  360. }
  361. func (alloc *PortAllocator) Acquire(addr net.IP, port int) (int, error) {
  362. mapKey := (&net.TCPAddr{Port: port, IP: addr}).String()
  363. utils.Debugf("Acquiring %s", mapKey)
  364. if port == 0 {
  365. // Allocate a port from the fountain
  366. for port := range alloc.fountain {
  367. if _, err := alloc.Acquire(addr, port); err == nil {
  368. return port, nil
  369. }
  370. }
  371. return -1, fmt.Errorf("Port generator ended unexpectedly")
  372. }
  373. alloc.Lock()
  374. defer alloc.Unlock()
  375. if _, inUse := alloc.inUse[mapKey]; inUse {
  376. return -1, fmt.Errorf("Port already in use: %d", port)
  377. }
  378. alloc.inUse[mapKey] = struct{}{}
  379. return port, nil
  380. }
  381. func (alloc *PortAllocator) Close() error {
  382. alloc.quit <- true
  383. close(alloc.quit)
  384. close(alloc.fountain)
  385. return nil
  386. }
  387. func newPortAllocator() (*PortAllocator, error) {
  388. allocator := &PortAllocator{
  389. inUse: make(map[string]struct{}),
  390. fountain: make(chan int),
  391. quit: make(chan bool),
  392. }
  393. go allocator.runFountain()
  394. return allocator, nil
  395. }
  396. // IP allocator: Automatically allocate and release networking ports
  397. type IPAllocator struct {
  398. network *net.IPNet
  399. queueAlloc chan allocatedIP
  400. queueReleased chan net.IP
  401. inUse map[int32]struct{}
  402. quit chan bool
  403. }
  404. type allocatedIP struct {
  405. ip net.IP
  406. err error
  407. }
  408. func (alloc *IPAllocator) run() {
  409. firstIP, _ := networkRange(alloc.network)
  410. ipNum := ipToInt(firstIP)
  411. ownIP := ipToInt(alloc.network.IP)
  412. size := networkSize(alloc.network.Mask)
  413. pos := int32(1)
  414. max := size - 2 // -1 for the broadcast address, -1 for the gateway address
  415. for {
  416. var (
  417. newNum int32
  418. inUse bool
  419. )
  420. // Find first unused IP, give up after one whole round
  421. for attempt := int32(0); attempt < max; attempt++ {
  422. newNum = ipNum + pos
  423. pos = pos%max + 1
  424. // The network's IP is never okay to use
  425. if newNum == ownIP {
  426. continue
  427. }
  428. if _, inUse = alloc.inUse[newNum]; !inUse {
  429. // We found an unused IP
  430. break
  431. }
  432. }
  433. ip := allocatedIP{ip: intToIP(newNum)}
  434. if inUse {
  435. ip.err = errors.New("No unallocated IP available")
  436. }
  437. select {
  438. case quit := <-alloc.quit:
  439. if quit {
  440. return
  441. }
  442. case alloc.queueAlloc <- ip:
  443. alloc.inUse[newNum] = struct{}{}
  444. case released := <-alloc.queueReleased:
  445. r := ipToInt(released)
  446. delete(alloc.inUse, r)
  447. if inUse {
  448. // If we couldn't allocate a new IP, the released one
  449. // will be the only free one now, so instantly use it
  450. // next time
  451. pos = r - ipNum
  452. } else {
  453. // Use same IP as last time
  454. if pos == 1 {
  455. pos = max
  456. } else {
  457. pos--
  458. }
  459. }
  460. }
  461. }
  462. }
  463. func (alloc *IPAllocator) Acquire() (net.IP, error) {
  464. ip := <-alloc.queueAlloc
  465. return ip.ip, ip.err
  466. }
  467. func (alloc *IPAllocator) Release(ip net.IP) {
  468. alloc.queueReleased <- ip
  469. }
  470. func (alloc *IPAllocator) Close() error {
  471. alloc.quit <- true
  472. close(alloc.quit)
  473. close(alloc.queueAlloc)
  474. close(alloc.queueReleased)
  475. return nil
  476. }
  477. func newIPAllocator(network *net.IPNet) *IPAllocator {
  478. alloc := &IPAllocator{
  479. network: network,
  480. queueAlloc: make(chan allocatedIP),
  481. queueReleased: make(chan net.IP),
  482. inUse: make(map[int32]struct{}),
  483. quit: make(chan bool),
  484. }
  485. go alloc.run()
  486. return alloc
  487. }
  488. // Network interface represents the networking stack of a container
  489. type NetworkInterface struct {
  490. IPNet net.IPNet
  491. Gateway net.IP
  492. manager *NetworkManager
  493. extPorts []*Nat
  494. disabled bool
  495. }
  496. // Allocate an external port and map it to the interface
  497. func (iface *NetworkInterface) AllocatePort(port Port, binding PortBinding) (*Nat, error) {
  498. if iface.disabled {
  499. return nil, fmt.Errorf("Trying to allocate port for interface %v, which is disabled", iface) // FIXME
  500. }
  501. ip := iface.manager.portMapper.defaultIp
  502. if binding.HostIp != "" {
  503. ip = net.ParseIP(binding.HostIp)
  504. } else {
  505. binding.HostIp = ip.String()
  506. }
  507. nat := &Nat{
  508. Port: port,
  509. Binding: binding,
  510. }
  511. containerPort, err := parsePort(port.Port())
  512. if err != nil {
  513. return nil, err
  514. }
  515. hostPort, _ := parsePort(nat.Binding.HostPort)
  516. if nat.Port.Proto() == "tcp" {
  517. extPort, err := iface.manager.tcpPortAllocator.Acquire(ip, hostPort)
  518. if err != nil {
  519. return nil, err
  520. }
  521. backend := &net.TCPAddr{IP: iface.IPNet.IP, Port: containerPort}
  522. if err := iface.manager.portMapper.Map(ip, extPort, backend); err != nil {
  523. iface.manager.tcpPortAllocator.Release(ip, extPort)
  524. return nil, err
  525. }
  526. nat.Binding.HostPort = strconv.Itoa(extPort)
  527. } else {
  528. extPort, err := iface.manager.udpPortAllocator.Acquire(ip, hostPort)
  529. if err != nil {
  530. return nil, err
  531. }
  532. backend := &net.UDPAddr{IP: iface.IPNet.IP, Port: containerPort}
  533. if err := iface.manager.portMapper.Map(ip, extPort, backend); err != nil {
  534. iface.manager.udpPortAllocator.Release(ip, extPort)
  535. return nil, err
  536. }
  537. nat.Binding.HostPort = strconv.Itoa(extPort)
  538. }
  539. iface.extPorts = append(iface.extPorts, nat)
  540. return nat, nil
  541. }
  542. type Nat struct {
  543. Port Port
  544. Binding PortBinding
  545. }
  546. func (n *Nat) String() string {
  547. return fmt.Sprintf("%s:%s:%s/%s", n.Binding.HostIp, n.Binding.HostPort, n.Port.Port(), n.Port.Proto())
  548. }
  549. // Release: Network cleanup - release all resources
  550. func (iface *NetworkInterface) Release() {
  551. if iface.disabled {
  552. return
  553. }
  554. for _, nat := range iface.extPorts {
  555. hostPort, err := parsePort(nat.Binding.HostPort)
  556. if err != nil {
  557. log.Printf("Unable to get host port: %s", err)
  558. continue
  559. }
  560. ip := net.ParseIP(nat.Binding.HostIp)
  561. utils.Debugf("Unmaping %s/%s:%s", nat.Port.Proto, ip.String(), nat.Binding.HostPort)
  562. if err := iface.manager.portMapper.Unmap(ip, hostPort, nat.Port.Proto()); err != nil {
  563. log.Printf("Unable to unmap port %s: %s", nat, err)
  564. }
  565. if nat.Port.Proto() == "tcp" {
  566. if err := iface.manager.tcpPortAllocator.Release(ip, hostPort); err != nil {
  567. log.Printf("Unable to release port %s", nat)
  568. }
  569. } else if nat.Port.Proto() == "udp" {
  570. if err := iface.manager.udpPortAllocator.Release(ip, hostPort); err != nil {
  571. log.Printf("Unable to release port %s: %s", nat, err)
  572. }
  573. }
  574. }
  575. iface.manager.ipAllocator.Release(iface.IPNet.IP)
  576. }
  577. // Network Manager manages a set of network interfaces
  578. // Only *one* manager per host machine should be used
  579. type NetworkManager struct {
  580. bridgeIface string
  581. bridgeNetwork *net.IPNet
  582. ipAllocator *IPAllocator
  583. tcpPortAllocator *PortAllocator
  584. udpPortAllocator *PortAllocator
  585. portMapper *PortMapper
  586. disabled bool
  587. }
  588. // Allocate a network interface
  589. func (manager *NetworkManager) Allocate() (*NetworkInterface, error) {
  590. if manager.disabled {
  591. return &NetworkInterface{disabled: true}, nil
  592. }
  593. var ip net.IP
  594. var err error
  595. ip, err = manager.ipAllocator.Acquire()
  596. if err != nil {
  597. return nil, err
  598. }
  599. // avoid duplicate IP
  600. ipNum := ipToInt(ip)
  601. firstIP := manager.ipAllocator.network.IP.To4().Mask(manager.ipAllocator.network.Mask)
  602. firstIPNum := ipToInt(firstIP) + 1
  603. if firstIPNum == ipNum {
  604. ip, err = manager.ipAllocator.Acquire()
  605. if err != nil {
  606. return nil, err
  607. }
  608. }
  609. iface := &NetworkInterface{
  610. IPNet: net.IPNet{IP: ip, Mask: manager.bridgeNetwork.Mask},
  611. Gateway: manager.bridgeNetwork.IP,
  612. manager: manager,
  613. }
  614. return iface, nil
  615. }
  616. func (manager *NetworkManager) Close() error {
  617. if manager.disabled {
  618. return nil
  619. }
  620. err1 := manager.tcpPortAllocator.Close()
  621. err2 := manager.udpPortAllocator.Close()
  622. err3 := manager.ipAllocator.Close()
  623. if err1 != nil {
  624. return err1
  625. }
  626. if err2 != nil {
  627. return err2
  628. }
  629. return err3
  630. }
  631. func newNetworkManager(config *DaemonConfig) (*NetworkManager, error) {
  632. if config.BridgeIface == DisableNetworkBridge {
  633. manager := &NetworkManager{
  634. disabled: true,
  635. }
  636. return manager, nil
  637. }
  638. addr, err := getIfaceAddr(config.BridgeIface)
  639. if err != nil {
  640. // If the iface is not found, try to create it
  641. if err := CreateBridgeIface(config); err != nil {
  642. return nil, err
  643. }
  644. addr, err = getIfaceAddr(config.BridgeIface)
  645. if err != nil {
  646. return nil, err
  647. }
  648. }
  649. network := addr.(*net.IPNet)
  650. // Configure iptables for link support
  651. if config.EnableIptables {
  652. // Enable NAT
  653. natArgs := []string{"POSTROUTING", "-t", "nat", "-s", addr.String(), "!", "-d", addr.String(), "-j", "MASQUERADE"}
  654. if !iptables.Exists(natArgs...) {
  655. if output, err := iptables.Raw(append([]string{"-A"}, natArgs...)...); err != nil {
  656. return nil, fmt.Errorf("Unable to enable network bridge NAT: %s", err)
  657. } else if len(output) != 0 {
  658. return nil, fmt.Errorf("Error iptables postrouting: %s", output)
  659. }
  660. }
  661. // Accept incoming packets for existing connections
  662. existingArgs := []string{"FORWARD", "-o", config.BridgeIface, "-m", "conntrack", "--ctstate", "RELATED,ESTABLISHED", "-j", "ACCEPT"}
  663. if !iptables.Exists(existingArgs...) {
  664. if output, err := iptables.Raw(append([]string{"-I"}, existingArgs...)...); err != nil {
  665. return nil, fmt.Errorf("Unable to allow incoming packets: %s", err)
  666. } else if len(output) != 0 {
  667. return nil, fmt.Errorf("Error iptables allow incoming: %s", output)
  668. }
  669. }
  670. // Accept all non-intercontainer outgoing packets
  671. outgoingArgs := []string{"FORWARD", "-i", config.BridgeIface, "!", "-o", config.BridgeIface, "-j", "ACCEPT"}
  672. if !iptables.Exists(outgoingArgs...) {
  673. if output, err := iptables.Raw(append([]string{"-I"}, outgoingArgs...)...); err != nil {
  674. return nil, fmt.Errorf("Unable to allow outgoing packets: %s", err)
  675. } else if len(output) != 0 {
  676. return nil, fmt.Errorf("Error iptables allow outgoing: %s", output)
  677. }
  678. }
  679. args := []string{"FORWARD", "-i", config.BridgeIface, "-o", config.BridgeIface, "-j"}
  680. acceptArgs := append(args, "ACCEPT")
  681. dropArgs := append(args, "DROP")
  682. if !config.InterContainerCommunication {
  683. iptables.Raw(append([]string{"-D"}, acceptArgs...)...)
  684. if !iptables.Exists(dropArgs...) {
  685. utils.Debugf("Disable inter-container communication")
  686. if output, err := iptables.Raw(append([]string{"-I"}, dropArgs...)...); err != nil {
  687. return nil, fmt.Errorf("Unable to prevent intercontainer communication: %s", err)
  688. } else if len(output) != 0 {
  689. return nil, fmt.Errorf("Error disabling intercontainer communication: %s", output)
  690. }
  691. }
  692. } else {
  693. iptables.Raw(append([]string{"-D"}, dropArgs...)...)
  694. if !iptables.Exists(acceptArgs...) {
  695. utils.Debugf("Enable inter-container communication")
  696. if output, err := iptables.Raw(append([]string{"-I"}, acceptArgs...)...); err != nil {
  697. return nil, fmt.Errorf("Unable to allow intercontainer communication: %s", err)
  698. } else if len(output) != 0 {
  699. return nil, fmt.Errorf("Error enabling intercontainer communication: %s", output)
  700. }
  701. }
  702. }
  703. }
  704. ipAllocator := newIPAllocator(network)
  705. tcpPortAllocator, err := newPortAllocator()
  706. if err != nil {
  707. return nil, err
  708. }
  709. udpPortAllocator, err := newPortAllocator()
  710. if err != nil {
  711. return nil, err
  712. }
  713. portMapper, err := newPortMapper(config)
  714. if err != nil {
  715. return nil, err
  716. }
  717. manager := &NetworkManager{
  718. bridgeIface: config.BridgeIface,
  719. bridgeNetwork: network,
  720. ipAllocator: ipAllocator,
  721. tcpPortAllocator: tcpPortAllocator,
  722. udpPortAllocator: udpPortAllocator,
  723. portMapper: portMapper,
  724. }
  725. return manager, nil
  726. }