• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2020 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef NET_DNS_DNS_UDP_TRACKER_H_
6 #define NET_DNS_DNS_UDP_TRACKER_H_
7 
8 #include <stdint.h>
9 
10 #include "base/containers/circular_deque.h"
11 #include "base/memory/raw_ptr.h"
12 #include "base/time/default_tick_clock.h"
13 #include "base/time/time.h"
14 #include "net/base/net_export.h"
15 
16 namespace base {
17 class TickClock;
18 }  // namespace base
19 
20 namespace net {
21 
22 // Data tracker for DNS UDP and its usage of local ports. Intended to be owned
23 // by a DnsSession and thus keep track of the data session-wide. Responsible for
24 // related metrics and used to inform behavior based on the stored data.
25 //
26 // TODO(ericorth@chromium.org): Add methods to access the stored data or
27 // conclusions about it.
28 class NET_EXPORT_PRIVATE DnsUdpTracker {
29  public:
30   static constexpr base::TimeDelta kMaxAge = base::Minutes(10);
31   static constexpr size_t kMaxRecordedQueries = 256;
32 
33   // How recently an ID needs to be recorded in a recent query to be considered
34   // "recognized".
35   static constexpr base::TimeDelta kMaxRecognizedIdAge = base::Seconds(15);
36 
37   // Numbers of ID mismatches required to set the |low_entropy_| flag. Also
38   // serves as the max number of mismatches to be recorded, as no more entries
39   // are recorded after setting the flag.
40   static constexpr size_t kUnrecognizedIdMismatchThreshold = 8;
41   static constexpr size_t kRecognizedIdMismatchThreshold = 128;
42 
43   // Number of reuses of the same port required to set the |low_entropy_| flag.
44   // Note: The original value of this parameter was 2, but it caused a problem
45   // on Windows (crbug.com/1413620). The low entropy checker in DnsUdpTracker
46   // was too sensitive and caused many TCP fallbacks. This happened because the
47   // dynamic port range for UDP on Windows is too small (only 16384 ports). This
48   // meant that there was a high probability (about 1%) of reusing the same port
49   // number three or more times out of 256 records. To avoid these unnecessary
50   // TCP fallbacks, the value was changed to 3. The probability of reusing the
51   // same port number to 4 or more times out of 256 records is 3.92566e-05. And
52   // if the available port count is 2048, the probability: 0.0182851. So it is
53   // likely to activate when getting into the low entropy.
54   // (See crrev.com/c/4374511 for the calculation).
55   static constexpr int kPortReuseThreshold = 3;
56 
57   DnsUdpTracker();
58   ~DnsUdpTracker();
59 
60   DnsUdpTracker(DnsUdpTracker&&);
61   DnsUdpTracker& operator=(DnsUdpTracker&&);
62 
63   void RecordQuery(uint16_t port, uint16_t query_id);
64   void RecordResponseId(uint16_t query_id, uint16_t response_id);
65   void RecordConnectionError(int connection_error);
66 
67   // If true, the entropy from random UDP port and DNS ID has been detected to
68   // potentially be low, e.g. due to exhaustion of the port pool or mismatches
69   // on IDs.
low_entropy()70   bool low_entropy() const { return low_entropy_; }
71 
set_tick_clock_for_testing(base::TickClock * tick_clock)72   void set_tick_clock_for_testing(base::TickClock* tick_clock) {
73     tick_clock_ = tick_clock;
74   }
75 
76  private:
77   struct QueryData;
78 
79   void PurgeOldRecords();
80   void SaveQuery(QueryData query);
81   void SaveIdMismatch(uint16_t it);
82 
83   bool low_entropy_ = false;
84   base::circular_deque<QueryData> recent_queries_;
85 
86   // Times of recent ID mismatches, separated by whether or not the ID was
87   // recognized from recent queries.
88   base::circular_deque<base::TimeTicks> recent_unrecognized_id_hits_;
89   base::circular_deque<base::TimeTicks> recent_recognized_id_hits_;
90 
91   raw_ptr<const base::TickClock> tick_clock_ =
92       base::DefaultTickClock::GetInstance();
93 };
94 
95 }  // namespace net
96 
97 #endif  // NET_DNS_DNS_UDP_TRACKER_H_
98