service.go 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. // Copyright (C) 2015 The Syncthing Authors.
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this file,
  5. // You can obtain one at https://mozilla.org/MPL/2.0/.
  6. package nat
  7. import (
  8. "fmt"
  9. "hash/fnv"
  10. "math/rand"
  11. "net"
  12. stdsync "sync"
  13. "time"
  14. "github.com/syncthing/syncthing/lib/config"
  15. "github.com/syncthing/syncthing/lib/protocol"
  16. "github.com/syncthing/syncthing/lib/sync"
  17. )
  18. // Service runs a loop for discovery of IGDs (Internet Gateway Devices) and
  19. // setup/renewal of a port mapping.
  20. type Service struct {
  21. id protocol.DeviceID
  22. cfg config.Wrapper
  23. stop chan struct{}
  24. mappings []*Mapping
  25. timer *time.Timer
  26. mut sync.RWMutex
  27. }
  28. func NewService(id protocol.DeviceID, cfg config.Wrapper) *Service {
  29. return &Service{
  30. id: id,
  31. cfg: cfg,
  32. timer: time.NewTimer(0),
  33. mut: sync.NewRWMutex(),
  34. }
  35. }
  36. func (s *Service) Serve() {
  37. announce := stdsync.Once{}
  38. s.mut.Lock()
  39. s.timer.Reset(0)
  40. s.stop = make(chan struct{})
  41. s.mut.Unlock()
  42. for {
  43. select {
  44. case <-s.timer.C:
  45. if found := s.process(); found != -1 {
  46. announce.Do(func() {
  47. suffix := "s"
  48. if found == 1 {
  49. suffix = ""
  50. }
  51. l.Infoln("Detected", found, "NAT service"+suffix)
  52. })
  53. }
  54. case <-s.stop:
  55. s.timer.Stop()
  56. s.mut.RLock()
  57. for _, mapping := range s.mappings {
  58. mapping.clearAddresses()
  59. }
  60. s.mut.RUnlock()
  61. return
  62. }
  63. }
  64. }
  65. func (s *Service) process() int {
  66. // toRenew are mappings which are due for renewal
  67. // toUpdate are the remaining mappings, which will only be updated if one of
  68. // the old IGDs has gone away, or a new IGD has appeared, but only if we
  69. // actually need to perform a renewal.
  70. var toRenew, toUpdate []*Mapping
  71. renewIn := time.Duration(s.cfg.Options().NATRenewalM) * time.Minute
  72. if renewIn == 0 {
  73. // We always want to do renewal so lets just pick a nice sane number.
  74. renewIn = 30 * time.Minute
  75. }
  76. s.mut.RLock()
  77. for _, mapping := range s.mappings {
  78. if mapping.expires.Before(time.Now()) {
  79. toRenew = append(toRenew, mapping)
  80. } else {
  81. toUpdate = append(toUpdate, mapping)
  82. mappingRenewIn := time.Until(mapping.expires)
  83. if mappingRenewIn < renewIn {
  84. renewIn = mappingRenewIn
  85. }
  86. }
  87. }
  88. // Reset the timer while holding the lock, because of the following race:
  89. // T1: process acquires lock
  90. // T1: process checks the mappings and gets next renewal time in 30m
  91. // T2: process releases the lock
  92. // T2: NewMapping acquires the lock
  93. // T2: NewMapping adds mapping
  94. // T2: NewMapping releases the lock
  95. // T2: NewMapping resets timer to 1s
  96. // T1: process resets timer to 30
  97. s.timer.Reset(renewIn)
  98. s.mut.RUnlock()
  99. // Don't do anything, unless we really need to renew
  100. if len(toRenew) == 0 {
  101. return -1
  102. }
  103. nats := discoverAll(time.Duration(s.cfg.Options().NATRenewalM)*time.Minute, time.Duration(s.cfg.Options().NATTimeoutS)*time.Second)
  104. for _, mapping := range toRenew {
  105. s.updateMapping(mapping, nats, true)
  106. }
  107. for _, mapping := range toUpdate {
  108. s.updateMapping(mapping, nats, false)
  109. }
  110. return len(nats)
  111. }
  112. func (s *Service) Stop() {
  113. s.mut.RLock()
  114. close(s.stop)
  115. s.mut.RUnlock()
  116. }
  117. func (s *Service) NewMapping(protocol Protocol, ip net.IP, port int) *Mapping {
  118. mapping := &Mapping{
  119. protocol: protocol,
  120. address: Address{
  121. IP: ip,
  122. Port: port,
  123. },
  124. extAddresses: make(map[string]Address),
  125. mut: sync.NewRWMutex(),
  126. }
  127. s.mut.Lock()
  128. s.mappings = append(s.mappings, mapping)
  129. // Reset the timer while holding the lock, see process() for explanation
  130. s.timer.Reset(time.Second)
  131. s.mut.Unlock()
  132. return mapping
  133. }
  134. // RemoveMapping does not actually remove the mapping from the IGD, it just
  135. // internally removes it which stops renewing the mapping. Also, it clears any
  136. // existing mapped addresses from the mapping, which as a result should cause
  137. // discovery to reannounce the new addresses.
  138. func (s *Service) RemoveMapping(mapping *Mapping) {
  139. s.mut.Lock()
  140. defer s.mut.Unlock()
  141. for i, existing := range s.mappings {
  142. if existing == mapping {
  143. mapping.clearAddresses()
  144. last := len(s.mappings) - 1
  145. s.mappings[i] = s.mappings[last]
  146. s.mappings[last] = nil
  147. s.mappings = s.mappings[:last]
  148. return
  149. }
  150. }
  151. }
  152. // updateMapping compares the addresses of the existing mapping versus the natds
  153. // discovered, and removes any addresses of natds that do not exist, or tries to
  154. // acquire mappings for natds which the mapping was unaware of before.
  155. // Optionally takes renew flag which indicates whether or not we should renew
  156. // mappings with existing natds
  157. func (s *Service) updateMapping(mapping *Mapping, nats map[string]Device, renew bool) {
  158. var added, removed []Address
  159. renewalTime := time.Duration(s.cfg.Options().NATRenewalM) * time.Minute
  160. mapping.expires = time.Now().Add(renewalTime)
  161. newAdded, newRemoved := s.verifyExistingMappings(mapping, nats, renew)
  162. added = append(added, newAdded...)
  163. removed = append(removed, newRemoved...)
  164. newAdded, newRemoved = s.acquireNewMappings(mapping, nats)
  165. added = append(added, newAdded...)
  166. removed = append(removed, newRemoved...)
  167. if len(added) > 0 || len(removed) > 0 {
  168. mapping.notify(added, removed)
  169. }
  170. }
  171. func (s *Service) verifyExistingMappings(mapping *Mapping, nats map[string]Device, renew bool) ([]Address, []Address) {
  172. var added, removed []Address
  173. leaseTime := time.Duration(s.cfg.Options().NATLeaseM) * time.Minute
  174. for id, address := range mapping.addressMap() {
  175. // Delete addresses for NATDevice's that do not exist anymore
  176. nat, ok := nats[id]
  177. if !ok {
  178. mapping.removeAddress(id)
  179. removed = append(removed, address)
  180. continue
  181. } else if renew {
  182. // Only perform renewals on the nat's that have the right local IP
  183. // address
  184. localIP := nat.GetLocalIPAddress()
  185. if !mapping.validGateway(localIP) {
  186. l.Debugf("Skipping %s for %s because of IP mismatch. %s != %s", id, mapping, mapping.address.IP, localIP)
  187. continue
  188. }
  189. l.Debugf("Renewing %s -> %s mapping on %s", mapping, address, id)
  190. addr, err := s.tryNATDevice(nat, mapping.address.Port, address.Port, leaseTime)
  191. if err != nil {
  192. l.Debugf("Failed to renew %s -> mapping on %s", mapping, address, id)
  193. mapping.removeAddress(id)
  194. removed = append(removed, address)
  195. continue
  196. }
  197. l.Debugf("Renewed %s -> %s mapping on %s", mapping, address, id)
  198. if !addr.Equal(address) {
  199. mapping.removeAddress(id)
  200. mapping.setAddress(id, addr)
  201. removed = append(removed, address)
  202. added = append(added, address)
  203. }
  204. }
  205. }
  206. return added, removed
  207. }
  208. func (s *Service) acquireNewMappings(mapping *Mapping, nats map[string]Device) ([]Address, []Address) {
  209. var added, removed []Address
  210. leaseTime := time.Duration(s.cfg.Options().NATLeaseM) * time.Minute
  211. addrMap := mapping.addressMap()
  212. for id, nat := range nats {
  213. if _, ok := addrMap[id]; ok {
  214. continue
  215. }
  216. // Only perform mappings on the nat's that have the right local IP
  217. // address
  218. localIP := nat.GetLocalIPAddress()
  219. if !mapping.validGateway(localIP) {
  220. l.Debugf("Skipping %s for %s because of IP mismatch. %s != %s", id, mapping, mapping.address.IP, localIP)
  221. continue
  222. }
  223. l.Debugf("Acquiring %s mapping on %s", mapping, id)
  224. addr, err := s.tryNATDevice(nat, mapping.address.Port, 0, leaseTime)
  225. if err != nil {
  226. l.Debugf("Failed to acquire %s mapping on %s", mapping, id)
  227. continue
  228. }
  229. l.Debugf("Acquired %s -> %s mapping on %s", mapping, addr, id)
  230. mapping.setAddress(id, addr)
  231. added = append(added, addr)
  232. }
  233. return added, removed
  234. }
  235. // tryNATDevice tries to acquire a port mapping for the given internal address to
  236. // the given external port. If external port is 0, picks a pseudo-random port.
  237. func (s *Service) tryNATDevice(natd Device, intPort, extPort int, leaseTime time.Duration) (Address, error) {
  238. var err error
  239. var port int
  240. // Generate a predictable random which is based on device ID + local port + hash of the device ID
  241. // number so that the ports we'd try to acquire for the mapping would always be the same for the
  242. // same device trying to get the same internal port.
  243. predictableRand := rand.New(rand.NewSource(int64(s.id.Short()) + int64(intPort) + hash(natd.ID())))
  244. if extPort != 0 {
  245. // First try renewing our existing mapping, if we have one.
  246. name := fmt.Sprintf("syncthing-%d", extPort)
  247. port, err = natd.AddPortMapping(TCP, intPort, extPort, name, leaseTime)
  248. if err == nil {
  249. extPort = port
  250. goto findIP
  251. }
  252. l.Debugln("Error extending lease on", natd.ID(), err)
  253. }
  254. for i := 0; i < 10; i++ {
  255. // Then try up to ten random ports.
  256. extPort = 1024 + predictableRand.Intn(65535-1024)
  257. name := fmt.Sprintf("syncthing-%d", extPort)
  258. port, err = natd.AddPortMapping(TCP, intPort, extPort, name, leaseTime)
  259. if err == nil {
  260. extPort = port
  261. goto findIP
  262. }
  263. l.Debugln("Error getting new lease on", natd.ID(), err)
  264. }
  265. return Address{}, err
  266. findIP:
  267. ip, err := natd.GetExternalIPAddress()
  268. if err != nil {
  269. l.Debugln("Error getting external ip on", natd.ID(), err)
  270. ip = nil
  271. }
  272. return Address{
  273. IP: ip,
  274. Port: extPort,
  275. }, nil
  276. }
  277. func hash(input string) int64 {
  278. h := fnv.New64a()
  279. h.Write([]byte(input))
  280. return int64(h.Sum64())
  281. }