service.go 9.4 KB

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