1
0

service.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  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. "log/slog"
  12. "math/rand"
  13. "net"
  14. "slices"
  15. "sync"
  16. "time"
  17. "github.com/syncthing/syncthing/internal/slogutil"
  18. "github.com/syncthing/syncthing/lib/config"
  19. "github.com/syncthing/syncthing/lib/protocol"
  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. id protocol.DeviceID
  25. cfg config.Wrapper
  26. processScheduled chan struct{}
  27. mappings []*Mapping
  28. enabled bool
  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. processScheduled: make(chan struct{}, 1),
  36. }
  37. cfgCopy := cfg.RawCopy()
  38. s.CommitConfiguration(cfgCopy, cfgCopy)
  39. return s
  40. }
  41. func (s *Service) CommitConfiguration(_, to config.Configuration) bool {
  42. s.mut.Lock()
  43. if !s.enabled && to.Options.NATEnabled {
  44. slog.Debug("Starting NAT service")
  45. s.enabled = true
  46. s.scheduleProcess()
  47. } else if s.enabled && !to.Options.NATEnabled {
  48. slog.Debug("Stopping NAT service")
  49. s.enabled = false
  50. }
  51. s.mut.Unlock()
  52. return true
  53. }
  54. func (s *Service) Serve(ctx context.Context) error {
  55. s.cfg.Subscribe(s)
  56. defer s.cfg.Unsubscribe(s)
  57. var announce sync.Once
  58. timer := time.NewTimer(0)
  59. for {
  60. select {
  61. case <-timer.C:
  62. case <-s.processScheduled:
  63. if !timer.Stop() {
  64. select {
  65. case <-timer.C:
  66. default:
  67. }
  68. }
  69. case <-ctx.Done():
  70. timer.Stop()
  71. s.mut.RLock()
  72. for _, mapping := range s.mappings {
  73. mapping.clearAddresses()
  74. }
  75. s.mut.RUnlock()
  76. return ctx.Err()
  77. }
  78. s.mut.RLock()
  79. enabled := s.enabled
  80. s.mut.RUnlock()
  81. if !enabled {
  82. continue
  83. }
  84. found, renewIn := s.process(ctx)
  85. timer.Reset(renewIn)
  86. if found != -1 {
  87. announce.Do(func() {
  88. slog.Info("Detected NAT services", "count", found)
  89. })
  90. }
  91. }
  92. }
  93. func (s *Service) process(ctx context.Context) (int, time.Duration) {
  94. // toRenew are mappings which are due for renewal
  95. // toUpdate are the remaining mappings, which will only be updated if one of
  96. // the old IGDs has gone away, or a new IGD has appeared, but only if we
  97. // actually need to perform a renewal.
  98. var toRenew, toUpdate []*Mapping
  99. renewIn := time.Duration(s.cfg.Options().NATRenewalM) * time.Minute
  100. if renewIn == 0 {
  101. // We always want to do renewal so lets just pick a nice sane number.
  102. renewIn = 30 * time.Minute
  103. }
  104. s.mut.RLock()
  105. for _, mapping := range s.mappings {
  106. mapping.mut.RLock()
  107. expires := mapping.expires
  108. mapping.mut.RUnlock()
  109. if expires.Before(time.Now()) {
  110. toRenew = append(toRenew, mapping)
  111. } else {
  112. toUpdate = append(toUpdate, mapping)
  113. mappingRenewIn := time.Until(expires)
  114. if mappingRenewIn < renewIn {
  115. renewIn = mappingRenewIn
  116. }
  117. }
  118. }
  119. s.mut.RUnlock()
  120. // Don't do anything, unless we really need to renew
  121. if len(toRenew) == 0 {
  122. return -1, renewIn
  123. }
  124. nats := discoverAll(ctx, time.Duration(s.cfg.Options().NATRenewalM)*time.Minute, time.Duration(s.cfg.Options().NATTimeoutS)*time.Second)
  125. for _, mapping := range toRenew {
  126. s.updateMapping(ctx, mapping, nats, true)
  127. }
  128. for _, mapping := range toUpdate {
  129. s.updateMapping(ctx, mapping, nats, false)
  130. }
  131. return len(nats), renewIn
  132. }
  133. func (s *Service) scheduleProcess() {
  134. select {
  135. case s.processScheduled <- struct{}{}: // 1-buffered
  136. default:
  137. }
  138. }
  139. func (s *Service) NewMapping(protocol Protocol, ipVersion IPVersion, ip net.IP, port int) *Mapping {
  140. mapping := &Mapping{
  141. protocol: protocol,
  142. address: Address{
  143. IP: ip,
  144. Port: port,
  145. },
  146. extAddresses: make(map[string][]Address),
  147. ipVersion: ipVersion,
  148. }
  149. s.mut.Lock()
  150. s.mappings = append(s.mappings, mapping)
  151. s.mut.Unlock()
  152. s.scheduleProcess()
  153. return mapping
  154. }
  155. // RemoveMapping does not actually remove the mapping from the IGD, it just
  156. // internally removes it which stops renewing the mapping. Also, it clears any
  157. // existing mapped addresses from the mapping, which as a result should cause
  158. // discovery to reannounce the new addresses.
  159. func (s *Service) RemoveMapping(mapping *Mapping) {
  160. s.mut.Lock()
  161. defer s.mut.Unlock()
  162. for i, existing := range s.mappings {
  163. if existing == mapping {
  164. mapping.clearAddresses()
  165. last := len(s.mappings) - 1
  166. s.mappings[i] = s.mappings[last]
  167. s.mappings[last] = nil
  168. s.mappings = s.mappings[:last]
  169. return
  170. }
  171. }
  172. }
  173. // updateMapping compares the addresses of the existing mapping versus the natds
  174. // discovered, and removes any addresses of natds that do not exist, or tries to
  175. // acquire mappings for natds which the mapping was unaware of before.
  176. // Optionally takes renew flag which indicates whether or not we should renew
  177. // mappings with existing natds
  178. func (s *Service) updateMapping(ctx context.Context, mapping *Mapping, nats map[string]Device, renew bool) {
  179. renewalTime := time.Duration(s.cfg.Options().NATRenewalM) * time.Minute
  180. mapping.mut.Lock()
  181. mapping.expires = time.Now().Add(renewalTime)
  182. change := s.verifyExistingLocked(ctx, mapping, nats, renew)
  183. add := s.acquireNewLocked(ctx, mapping, nats)
  184. mapping.mut.Unlock()
  185. if change || add {
  186. mapping.notify()
  187. }
  188. }
  189. func (s *Service) verifyExistingLocked(ctx context.Context, mapping *Mapping, nats map[string]Device, renew bool) (change bool) {
  190. leaseTime := time.Duration(s.cfg.Options().NATLeaseM) * time.Minute
  191. for id, extAddrs := range mapping.extAddresses {
  192. select {
  193. case <-ctx.Done():
  194. return false
  195. default:
  196. }
  197. if nat, ok := nats[id]; !ok || len(extAddrs) == 0 {
  198. // Delete addresses for NATDevice's that do not exist anymore
  199. mapping.removeAddressLocked(id)
  200. change = true
  201. continue
  202. } else if renew {
  203. // Only perform renewals on the nat's that have the right local IP
  204. // address. For IPv6 the IP addresses are discovered by the service itself,
  205. // so this check is skipped.
  206. localIP := nat.GetLocalIPv4Address()
  207. if !mapping.validGateway(localIP) && nat.SupportsIPVersion(IPv4Only) {
  208. l.Debugf("Skipping %s for %s because of IP mismatch. %s != %s", id, mapping, mapping.address.IP, localIP)
  209. continue
  210. }
  211. if !nat.SupportsIPVersion(mapping.ipVersion) {
  212. l.Debugf("Skipping renew on gateway %s because it doesn't match the listener address family", nat.ID())
  213. continue
  214. }
  215. l.Debugf("Renewing %s -> %v open port on %s", mapping, extAddrs, id)
  216. // extAddrs either contains one IPv4 address, or possibly several
  217. // IPv6 addresses all using the same port. Therefore the first
  218. // entry always has the external port.
  219. responseAddrs, err := s.tryNATDevice(ctx, nat, mapping.address, extAddrs[0].Port, mapping.protocol, leaseTime)
  220. if err != nil {
  221. slog.WarnContext(ctx, "Failed to renew open port", slog.String("mapping", mapping.String()), slog.Any("addresses", extAddrs), slog.String("id", id), slogutil.Error(err))
  222. mapping.removeAddressLocked(id)
  223. change = true
  224. continue
  225. }
  226. l.Debugf("Renewed %s -> %v open port on %s", mapping, extAddrs, id)
  227. // We shouldn't rely on the order in which the addresses are returned.
  228. // Therefore, we test for set equality and report change if there is any difference.
  229. if !addrSetsEqual(responseAddrs, extAddrs) {
  230. mapping.setAddressLocked(id, responseAddrs)
  231. change = true
  232. }
  233. }
  234. }
  235. return change
  236. }
  237. func (s *Service) acquireNewLocked(ctx context.Context, mapping *Mapping, nats map[string]Device) (change bool) {
  238. leaseTime := time.Duration(s.cfg.Options().NATLeaseM) * time.Minute
  239. addrMap := mapping.extAddresses
  240. for id, nat := range nats {
  241. select {
  242. case <-ctx.Done():
  243. return false
  244. default:
  245. }
  246. if _, ok := addrMap[id]; ok {
  247. continue
  248. }
  249. // Only perform mappings on the nat's that have the right local IP
  250. // address
  251. localIP := nat.GetLocalIPv4Address()
  252. if !mapping.validGateway(localIP) && nat.SupportsIPVersion(IPv4Only) {
  253. l.Debugf("Skipping %s for %s because of IP mismatch. %s != %s", id, mapping, mapping.address.IP, localIP)
  254. continue
  255. }
  256. l.Debugf("Trying to open port %s on %s", mapping, id)
  257. if !nat.SupportsIPVersion(mapping.ipVersion) {
  258. l.Debugf("Skipping firewall traversal on gateway %s because it doesn't match the listener address family", nat.ID())
  259. continue
  260. }
  261. addrs, err := s.tryNATDevice(ctx, nat, mapping.address, 0, mapping.protocol, leaseTime)
  262. if err != nil {
  263. slog.WarnContext(ctx, "Failed to acquire open port", slog.String("mapping", mapping.String()), slog.String("id", id), slogutil.Error(err))
  264. continue
  265. }
  266. l.Debugf("Opened port %s -> %v on %s", mapping, addrs, id)
  267. mapping.setAddressLocked(id, addrs)
  268. change = true
  269. }
  270. return change
  271. }
  272. // tryNATDevice tries to acquire a port mapping for the given internal address to
  273. // the given external port. If external port is 0, picks a pseudo-random port.
  274. func (s *Service) tryNATDevice(ctx context.Context, natd Device, intAddr Address, extPort int, protocol Protocol, leaseTime time.Duration) ([]Address, error) {
  275. var err error
  276. var port int
  277. // For IPv6, we just try to create the pinhole. If it fails, nothing can be done (probably no IGDv2 support).
  278. // If it already exists, the relevant UPnP standard requires that the gateway recognizes this and updates the lease time.
  279. // Since we usually have a global unicast IPv6 address so no conflicting mappings, we just request the port we're running on
  280. if natd.SupportsIPVersion(IPv6Only) {
  281. ipaddrs, err := natd.AddPinhole(ctx, protocol, intAddr, leaseTime)
  282. var addrs []Address
  283. for _, ipaddr := range ipaddrs {
  284. addrs = append(addrs, Address{
  285. ipaddr,
  286. intAddr.Port,
  287. })
  288. }
  289. if err != nil {
  290. l.Debugln("Error extending lease on", natd.ID(), err)
  291. }
  292. return addrs, err
  293. }
  294. // Generate a predictable random which is based on device ID + local port + hash of the device ID
  295. // number so that the ports we'd try to acquire for the mapping would always be the same for the
  296. // same device trying to get the same internal port.
  297. predictableRand := rand.New(rand.NewSource(int64(s.id.Short()) + int64(intAddr.Port) + hash(natd.ID())))
  298. if extPort != 0 {
  299. // First try renewing our existing mapping, if we have one.
  300. name := fmt.Sprintf("syncthing-%d", extPort)
  301. port, err = natd.AddPortMapping(ctx, protocol, intAddr.Port, extPort, name, leaseTime)
  302. if err == nil {
  303. extPort = port
  304. goto findIP
  305. }
  306. l.Debugf("Error extending lease on %v (external port %d -> internal port %d): %v", natd.ID(), extPort, intAddr.Port, err)
  307. }
  308. for i := 0; i < 10; i++ {
  309. select {
  310. case <-ctx.Done():
  311. return []Address{}, ctx.Err()
  312. default:
  313. }
  314. // Then try up to ten random ports.
  315. extPort = 1024 + predictableRand.Intn(65535-1024)
  316. name := fmt.Sprintf("syncthing-%d", extPort)
  317. port, err = natd.AddPortMapping(ctx, protocol, intAddr.Port, extPort, name, leaseTime)
  318. if err == nil {
  319. extPort = port
  320. goto findIP
  321. }
  322. err = fmt.Errorf("getting new lease on %s (external port %d -> internal port %d): %w", natd.ID(), extPort, intAddr.Port, err)
  323. l.Debugf("Error %s", err)
  324. }
  325. return nil, err
  326. findIP:
  327. ip, err := natd.GetExternalIPv4Address(ctx)
  328. if err != nil {
  329. l.Debugf("Error getting external ip on %s: %s", natd.ID(), err)
  330. ip = nil
  331. }
  332. return []Address{
  333. {
  334. IP: ip,
  335. Port: extPort,
  336. },
  337. }, nil
  338. }
  339. func (s *Service) String() string {
  340. return fmt.Sprintf("nat.Service@%p", s)
  341. }
  342. func hash(input string) int64 {
  343. h := fnv.New64a()
  344. h.Write([]byte(input))
  345. return int64(h.Sum64())
  346. }
  347. func addrSetsEqual(a []Address, b []Address) bool {
  348. if len(a) != len(b) {
  349. return false
  350. }
  351. for _, v := range a {
  352. if !slices.ContainsFunc(b, v.Equal) {
  353. return false
  354. }
  355. }
  356. return true
  357. }