• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2008 The Android Open Source Project
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *  * Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  *  * Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in
12  *    the documentation and/or other materials provided with the
13  *    distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #define LOG_TAG "resolv"
30 
31 #include "resolv_cache.h"
32 
33 #include <resolv.h>
34 #include <stdarg.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <time.h>
38 #include <algorithm>
39 #include <mutex>
40 #include <set>
41 #include <string>
42 #include <unordered_map>
43 #include <vector>
44 
45 #include <arpa/inet.h>
46 #include <arpa/nameser.h>
47 #include <errno.h>
48 #include <linux/if.h>
49 #include <net/if.h>
50 #include <netdb.h>
51 
52 #include <aidl/android/net/IDnsResolver.h>
53 #include <android-base/logging.h>
54 #include <android-base/parseint.h>
55 #include <android-base/strings.h>
56 #include <android-base/thread_annotations.h>
57 #include <android/multinetwork.h>  // ResNsendFlags
58 
59 #include <server_configurable_flags/get_flags.h>
60 
61 #include "DnsStats.h"
62 #include "Experiments.h"
63 #include "res_comp.h"
64 #include "res_debug.h"
65 #include "resolv_private.h"
66 #include "util.h"
67 
68 using aidl::android::net::IDnsResolver;
69 using aidl::android::net::ResolverOptionsParcel;
70 using android::net::DnsQueryEvent;
71 using android::net::DnsStats;
72 using android::net::Experiments;
73 using android::net::PROTO_DOH;
74 using android::net::PROTO_DOT;
75 using android::net::PROTO_MDNS;
76 using android::net::PROTO_TCP;
77 using android::net::PROTO_UDP;
78 using android::net::Protocol;
79 using android::netdutils::DumpWriter;
80 using android::netdutils::IPSockAddr;
81 using std::span;
82 
83 /* This code implements a small and *simple* DNS resolver cache.
84  *
85  * It is only used to cache DNS answers for a time defined by the smallest TTL
86  * among the answer records in order to reduce DNS traffic. It is not supposed
87  * to be a full DNS cache, since we plan to implement that in the future in a
88  * dedicated process running on the system.
89  *
90  * Note that its design is kept simple very intentionally, i.e.:
91  *
92  *  - it takes raw DNS query packet data as input, and returns raw DNS
93  *    answer packet data as output
94  *
95  *    (this means that two similar queries that encode the DNS name
96  *     differently will be treated distinctly).
97  *
98  *    the smallest TTL value among the answer records are used as the time
99  *    to keep an answer in the cache.
100  *
101  *    this is bad, but we absolutely want to avoid parsing the answer packets
102  *    (and should be solved by the later full DNS cache process).
103  *
104  *  - the implementation is just a (query-data) => (answer-data) hash table
105  *    with a trivial least-recently-used expiration policy.
106  *
107  * Doing this keeps the code simple and avoids to deal with a lot of things
108  * that a full DNS cache is expected to do.
109  *
110  * The API is also very simple:
111  *
112  *   - the client calls resolv_cache_lookup() before performing a query
113  *
114  *     If the function returns RESOLV_CACHE_FOUND, a copy of the answer data
115  *     has been copied into the client-provided answer buffer.
116  *
117  *     If the function returns RESOLV_CACHE_NOTFOUND, the client should perform
118  *     a request normally, *then* call resolv_cache_add() to add the received
119  *     answer to the cache.
120  *
121  *     If the function returns RESOLV_CACHE_UNSUPPORTED, the client should
122  *     perform a request normally, and *not* call resolv_cache_add()
123  *
124  *     Note that RESOLV_CACHE_UNSUPPORTED is also returned if the answer buffer
125  *     is too short to accomodate the cached result.
126  */
127 
128 /* Default number of entries kept in the cache. This value has been
129  * determined by browsing through various sites and counting the number
130  * of corresponding requests. Keep in mind that our framework is currently
131  * performing two requests per name lookup (one for IPv4, the other for IPv6)
132  *
133  *    www.google.com      4
134  *    www.ysearch.com     6
135  *    www.amazon.com      8
136  *    www.nytimes.com     22
137  *    www.espn.com        28
138  *    www.msn.com         28
139  *    www.lemonde.fr      35
140  *
141  * (determined in 2009-2-17 from Paris, France, results may vary depending
142  *  on location)
143  *
144  * most high-level websites use lots of media/ad servers with different names
145  * but these are generally reused when browsing through the site.
146  *
147  * As such, a value of 64 should be relatively comfortable at the moment.
148  *
149  * ******************************************
150  * * NOTE - this has changed.
151  * * 1) we've added IPv6 support so each dns query results in 2 responses
152  * * 2) we've made this a system-wide cache, so the cost is less (it's not
153  * *    duplicated in each process) and the need is greater (more processes
154  * *    making different requests).
155  * * Upping by 2x for IPv6
156  * * Upping by another 5x for the centralized nature
157  * *****************************************
158  */
159 const int CONFIG_MAX_ENTRIES = 64 * 2 * 5;
160 constexpr int DNSEVENT_SUBSAMPLING_MAP_DEFAULT_KEY = -1;
161 
_time_now(void)162 static time_t _time_now(void) {
163     struct timeval tv;
164 
165     gettimeofday(&tv, NULL);
166     return tv.tv_sec;
167 }
168 
169 /* reminder: the general format of a DNS packet is the following:
170  *
171  *    HEADER  (12 bytes)
172  *    QUESTION  (variable)
173  *    ANSWER (variable)
174  *    AUTHORITY (variable)
175  *    ADDITIONNAL (variable)
176  *
177  * the HEADER is made of:
178  *
179  *   ID     : 16 : 16-bit unique query identification field
180  *
181  *   QR     :  1 : set to 0 for queries, and 1 for responses
182  *   Opcode :  4 : set to 0 for queries
183  *   AA     :  1 : set to 0 for queries
184  *   TC     :  1 : truncation flag, will be set to 0 in queries
185  *   RD     :  1 : recursion desired
186  *
187  *   RA     :  1 : recursion available (0 in queries)
188  *   Z      :  3 : three reserved zero bits
189  *   RCODE  :  4 : response code (always 0=NOERROR in queries)
190  *
191  *   QDCount: 16 : question count
192  *   ANCount: 16 : Answer count (0 in queries)
193  *   NSCount: 16: Authority Record count (0 in queries)
194  *   ARCount: 16: Additionnal Record count (0 in queries)
195  *
196  * the QUESTION is made of QDCount Question Record (QRs)
197  * the ANSWER is made of ANCount RRs
198  * the AUTHORITY is made of NSCount RRs
199  * the ADDITIONNAL is made of ARCount RRs
200  *
201  * Each Question Record (QR) is made of:
202  *
203  *   QNAME   : variable : Query DNS NAME
204  *   TYPE    : 16       : type of query (A=1, PTR=12, MX=15, AAAA=28, ALL=255)
205  *   CLASS   : 16       : class of query (IN=1)
206  *
207  * Each Resource Record (RR) is made of:
208  *
209  *   NAME    : variable : DNS NAME
210  *   TYPE    : 16       : type of query (A=1, PTR=12, MX=15, AAAA=28, ALL=255)
211  *   CLASS   : 16       : class of query (IN=1)
212  *   TTL     : 32       : seconds to cache this RR (0=none)
213  *   RDLENGTH: 16       : size of RDDATA in bytes
214  *   RDDATA  : variable : RR data (depends on TYPE)
215  *
216  * Each QNAME contains a domain name encoded as a sequence of 'labels'
217  * terminated by a zero. Each label has the following format:
218  *
219  *    LEN  : 8     : lenght of label (MUST be < 64)
220  *    NAME : 8*LEN : label length (must exclude dots)
221  *
222  * A value of 0 in the encoding is interpreted as the 'root' domain and
223  * terminates the encoding. So 'www.android.com' will be encoded as:
224  *
225  *   <3>www<7>android<3>com<0>
226  *
227  * Where <n> represents the byte with value 'n'
228  *
229  * Each NAME reflects the QNAME of the question, but has a slightly more
230  * complex encoding in order to provide message compression. This is achieved
231  * by using a 2-byte pointer, with format:
232  *
233  *    TYPE   : 2  : 0b11 to indicate a pointer, 0b01 and 0b10 are reserved
234  *    OFFSET : 14 : offset to another part of the DNS packet
235  *
236  * The offset is relative to the start of the DNS packet and must point
237  * A pointer terminates the encoding.
238  *
239  * The NAME can be encoded in one of the following formats:
240  *
241  *   - a sequence of simple labels terminated by 0 (like QNAMEs)
242  *   - a single pointer
243  *   - a sequence of simple labels terminated by a pointer
244  *
245  * A pointer shall always point to either a pointer of a sequence of
246  * labels (which can themselves be terminated by either a 0 or a pointer)
247  *
248  * The expanded length of a given domain name should not exceed 255 bytes.
249  *
250  * NOTE: we don't parse the answer packets, so don't need to deal with NAME
251  *       records, only QNAMEs.
252  */
253 
254 #define DNS_HEADER_SIZE 12
255 
256 #define DNS_TYPE_A "\00\01"     /* big-endian decimal 1 */
257 #define DNS_TYPE_PTR "\00\014"  /* big-endian decimal 12 */
258 #define DNS_TYPE_MX "\00\017"   /* big-endian decimal 15 */
259 #define DNS_TYPE_AAAA "\00\034" /* big-endian decimal 28 */
260 #define DNS_TYPE_ALL "\00\0377" /* big-endian decimal 255 */
261 
262 #define DNS_CLASS_IN "\00\01" /* big-endian decimal 1 */
263 
264 struct DnsPacket {
265     const uint8_t* base;
266     const uint8_t* end;
267     const uint8_t* cursor;
268 };
269 
res_tolower(uint8_t c)270 static uint8_t res_tolower(uint8_t c) {
271     return (c >= 'A' && c <= 'Z') ? (c | 0x20) : c;
272 }
273 
res_memcasecmp(const unsigned char * s1,const unsigned char * s2,size_t len)274 static int res_memcasecmp(const unsigned char *s1, const unsigned char *s2, size_t len) {
275     for (size_t i = 0; i < len; i++) {
276         int ch1 = *s1++;
277         int ch2 = *s2++;
278         int d = res_tolower(ch1) - res_tolower(ch2);
279         if (d != 0) {
280             return d;
281         }
282     }
283     return 0;
284 }
285 
_dnsPacket_init(DnsPacket * packet,const uint8_t * buff,int bufflen)286 static void _dnsPacket_init(DnsPacket* packet, const uint8_t* buff, int bufflen) {
287     packet->base = buff;
288     packet->end = buff + bufflen;
289     packet->cursor = buff;
290 }
291 
_dnsPacket_rewind(DnsPacket * packet)292 static void _dnsPacket_rewind(DnsPacket* packet) {
293     packet->cursor = packet->base;
294 }
295 
_dnsPacket_skip(DnsPacket * packet,int count)296 static void _dnsPacket_skip(DnsPacket* packet, int count) {
297     const uint8_t* p = packet->cursor + count;
298 
299     if (p > packet->end) p = packet->end;
300 
301     packet->cursor = p;
302 }
303 
_dnsPacket_readInt16(DnsPacket * packet)304 static int _dnsPacket_readInt16(DnsPacket* packet) {
305     const uint8_t* p = packet->cursor;
306 
307     if (p + 2 > packet->end) return -1;
308 
309     packet->cursor = p + 2;
310     return (p[0] << 8) | p[1];
311 }
312 
313 /** QUERY CHECKING **/
314 
315 /* check bytes in a dns packet. returns 1 on success, 0 on failure.
316  * the cursor is only advanced in the case of success
317  */
_dnsPacket_checkBytes(DnsPacket * packet,int numBytes,const void * bytes)318 static int _dnsPacket_checkBytes(DnsPacket* packet, int numBytes, const void* bytes) {
319     const uint8_t* p = packet->cursor;
320 
321     if (p + numBytes > packet->end) return 0;
322 
323     if (memcmp(p, bytes, numBytes) != 0) return 0;
324 
325     packet->cursor = p + numBytes;
326     return 1;
327 }
328 
329 /* parse and skip a given QNAME stored in a query packet,
330  * from the current cursor position. returns 1 on success,
331  * or 0 for malformed data.
332  */
_dnsPacket_checkQName(DnsPacket * packet)333 static int _dnsPacket_checkQName(DnsPacket* packet) {
334     const uint8_t* p = packet->cursor;
335     const uint8_t* end = packet->end;
336 
337     for (;;) {
338         int c;
339 
340         if (p >= end) break;
341 
342         c = *p++;
343 
344         if (c == 0) {
345             packet->cursor = p;
346             return 1;
347         }
348 
349         /* we don't expect label compression in QNAMEs */
350         if (c >= 64) break;
351 
352         p += c;
353         /* we rely on the bound check at the start
354          * of the loop here */
355     }
356     /* malformed data */
357     LOG(INFO) << __func__ << ": malformed QNAME";
358     return 0;
359 }
360 
361 /* parse and skip a given QR stored in a packet.
362  * returns 1 on success, and 0 on failure
363  */
_dnsPacket_checkQR(DnsPacket * packet)364 static int _dnsPacket_checkQR(DnsPacket* packet) {
365     if (!_dnsPacket_checkQName(packet)) return 0;
366 
367     /* TYPE must be one of the things we support */
368     if (!_dnsPacket_checkBytes(packet, 2, DNS_TYPE_A) &&
369         !_dnsPacket_checkBytes(packet, 2, DNS_TYPE_PTR) &&
370         !_dnsPacket_checkBytes(packet, 2, DNS_TYPE_MX) &&
371         !_dnsPacket_checkBytes(packet, 2, DNS_TYPE_AAAA) &&
372         !_dnsPacket_checkBytes(packet, 2, DNS_TYPE_ALL)) {
373         LOG(INFO) << __func__ << ": unsupported TYPE";
374         return 0;
375     }
376     /* CLASS must be IN */
377     if (!_dnsPacket_checkBytes(packet, 2, DNS_CLASS_IN)) {
378         LOG(INFO) << __func__ << ": unsupported CLASS";
379         return 0;
380     }
381 
382     return 1;
383 }
384 
385 /* check the header of a DNS Query packet, return 1 if it is one
386  * type of query we can cache, or 0 otherwise
387  */
_dnsPacket_checkQuery(DnsPacket * packet)388 static int _dnsPacket_checkQuery(DnsPacket* packet) {
389     const uint8_t* p = packet->base;
390     int qdCount, anCount, dnCount, arCount;
391 
392     if (p + DNS_HEADER_SIZE > packet->end) {
393         LOG(INFO) << __func__ << ": query packet too small";
394         return 0;
395     }
396 
397     /* QR must be set to 0, opcode must be 0 and AA must be 0 */
398     /* RA, Z, and RCODE must be 0 */
399     if ((p[2] & 0xFC) != 0 || (p[3] & 0xCF) != 0) {
400         LOG(INFO) << __func__ << ": query packet flags unsupported";
401         return 0;
402     }
403 
404     /* Note that we ignore the TC, RD, CD, and AD bits here for the
405      * following reasons:
406      *
407      * - there is no point for a query packet sent to a server
408      *   to have the TC bit set, but the implementation might
409      *   set the bit in the query buffer for its own needs
410      *   between a resolv_cache_lookup and a resolv_cache_add.
411      *   We should not freak out if this is the case.
412      *
413      * - we consider that the result from a query might depend on
414      *   the RD, AD, and CD bits, so these bits
415      *   should be used to differentiate cached result.
416      *
417      *   this implies that these bits are checked when hashing or
418      *   comparing query packets, but not TC
419      */
420 
421     /* ANCOUNT, DNCOUNT and ARCOUNT must be 0 */
422     qdCount = (p[4] << 8) | p[5];
423     anCount = (p[6] << 8) | p[7];
424     dnCount = (p[8] << 8) | p[9];
425     arCount = (p[10] << 8) | p[11];
426 
427     if (anCount != 0 || dnCount != 0 || arCount > 1) {
428         LOG(INFO) << __func__ << ": query packet contains non-query records";
429         return 0;
430     }
431 
432     if (qdCount == 0) {
433         LOG(INFO) << __func__ << ": query packet doesn't contain query record";
434         return 0;
435     }
436 
437     /* Check QDCOUNT QRs */
438     packet->cursor = p + DNS_HEADER_SIZE;
439 
440     for (; qdCount > 0; qdCount--)
441         if (!_dnsPacket_checkQR(packet)) return 0;
442 
443     return 1;
444 }
445 
446 /** QUERY HASHING SUPPORT
447  **
448  ** THE FOLLOWING CODE ASSUMES THAT THE INPUT PACKET HAS ALREADY
449  ** BEEN SUCCESFULLY CHECKED.
450  **/
451 
452 /* use 32-bit FNV hash function */
453 #define FNV_MULT 16777619U
454 #define FNV_BASIS 2166136261U
455 
_dnsPacket_hashBytes(DnsPacket * packet,int numBytes,unsigned hash)456 static unsigned _dnsPacket_hashBytes(DnsPacket* packet, int numBytes, unsigned hash) {
457     const uint8_t* p = packet->cursor;
458     const uint8_t* end = packet->end;
459 
460     while (numBytes > 0 && p < end) {
461         hash = hash * FNV_MULT ^ *p++;
462         numBytes--;
463     }
464     packet->cursor = p;
465     return hash;
466 }
467 
_dnsPacket_hashQName(DnsPacket * packet,unsigned hash)468 static unsigned _dnsPacket_hashQName(DnsPacket* packet, unsigned hash) {
469     const uint8_t* p = packet->cursor;
470     const uint8_t* end = packet->end;
471 
472     for (;;) {
473         if (p >= end) { /* should not happen */
474             LOG(INFO) << __func__ << ": INTERNAL_ERROR: read-overflow";
475             break;
476         }
477 
478         int c = *p++;
479 
480         if (c == 0) break;
481 
482         if (c >= 64) {
483             LOG(INFO) << __func__ << ": INTERNAL_ERROR: malformed domain";
484             break;
485         }
486         if (p + c >= end) {
487             LOG(INFO) << __func__ << ": INTERNAL_ERROR: simple label read-overflow";
488             break;
489         }
490 
491         while (c > 0) {
492             uint8_t ch = *p++;
493             ch = res_tolower(ch);
494             hash = hash * FNV_MULT ^ ch;
495             c--;
496         }
497     }
498     packet->cursor = p;
499     return hash;
500 }
501 
_dnsPacket_hashQR(DnsPacket * packet,unsigned hash)502 static unsigned _dnsPacket_hashQR(DnsPacket* packet, unsigned hash) {
503     hash = _dnsPacket_hashQName(packet, hash);
504     hash = _dnsPacket_hashBytes(packet, 4, hash); /* TYPE and CLASS */
505     return hash;
506 }
507 
_dnsPacket_hashRR(DnsPacket * packet,unsigned hash)508 static unsigned _dnsPacket_hashRR(DnsPacket* packet, unsigned hash) {
509     int rdlength;
510     hash = _dnsPacket_hashQR(packet, hash);
511     hash = _dnsPacket_hashBytes(packet, 4, hash); /* TTL */
512     rdlength = _dnsPacket_readInt16(packet);
513     hash = _dnsPacket_hashBytes(packet, rdlength, hash); /* RDATA */
514     return hash;
515 }
516 
_dnsPacket_hashQuery(DnsPacket * packet)517 static unsigned _dnsPacket_hashQuery(DnsPacket* packet) {
518     unsigned hash = FNV_BASIS;
519     int count, arcount;
520     _dnsPacket_rewind(packet);
521 
522     /* ignore the ID */
523     _dnsPacket_skip(packet, 2);
524 
525     /* we ignore the TC bit for reasons explained in
526      * _dnsPacket_checkQuery().
527      *
528      * however we hash the RD bit to differentiate
529      * between answers for recursive and non-recursive
530      * queries.
531      */
532     hash = hash * FNV_MULT ^ (packet->base[2] & 1);
533 
534     /* mark the first header byte as processed */
535     _dnsPacket_skip(packet, 1);
536 
537     /* process the second header byte */
538     hash = _dnsPacket_hashBytes(packet, 1, hash);
539 
540     /* read QDCOUNT */
541     count = _dnsPacket_readInt16(packet);
542 
543     /* assume: ANcount and NScount are 0 */
544     _dnsPacket_skip(packet, 4);
545 
546     /* read ARCOUNT */
547     arcount = _dnsPacket_readInt16(packet);
548 
549     /* hash QDCOUNT QRs */
550     for (; count > 0; count--) hash = _dnsPacket_hashQR(packet, hash);
551 
552     /* hash ARCOUNT RRs */
553     for (; arcount > 0; arcount--) hash = _dnsPacket_hashRR(packet, hash);
554 
555     return hash;
556 }
557 
558 /** QUERY COMPARISON
559  **
560  ** THE FOLLOWING CODE ASSUMES THAT THE INPUT PACKETS HAVE ALREADY
561  ** BEEN SUCCESSFULLY CHECKED.
562  **/
563 
_dnsPacket_isEqualDomainName(DnsPacket * pack1,DnsPacket * pack2)564 static int _dnsPacket_isEqualDomainName(DnsPacket* pack1, DnsPacket* pack2) {
565     const uint8_t* p1 = pack1->cursor;
566     const uint8_t* end1 = pack1->end;
567     const uint8_t* p2 = pack2->cursor;
568     const uint8_t* end2 = pack2->end;
569 
570     for (;;) {
571         if (p1 >= end1 || p2 >= end2) {
572             LOG(INFO) << __func__ << ": INTERNAL_ERROR: read-overflow";
573             break;
574         }
575         int c1 = *p1++;
576         int c2 = *p2++;
577         if (c1 != c2) break;
578 
579         if (c1 == 0) {
580             pack1->cursor = p1;
581             pack2->cursor = p2;
582             return 1;
583         }
584         if (c1 >= 64) {
585             LOG(INFO) << __func__ << ": INTERNAL_ERROR: malformed domain";
586             break;
587         }
588         if ((p1 + c1 > end1) || (p2 + c1 > end2)) {
589             LOG(INFO) << __func__ << ": INTERNAL_ERROR: simple label read-overflow";
590             break;
591         }
592         if (res_memcasecmp(p1, p2, c1) != 0) break;
593         p1 += c1;
594         p2 += c1;
595         /* we rely on the bound checks at the start of the loop */
596     }
597     /* not the same, or one is malformed */
598     LOG(INFO) << __func__ << ": different DN";
599     return 0;
600 }
601 
_dnsPacket_isEqualBytes(DnsPacket * pack1,DnsPacket * pack2,int numBytes)602 static int _dnsPacket_isEqualBytes(DnsPacket* pack1, DnsPacket* pack2, int numBytes) {
603     const uint8_t* p1 = pack1->cursor;
604     const uint8_t* p2 = pack2->cursor;
605 
606     if (p1 + numBytes > pack1->end || p2 + numBytes > pack2->end) return 0;
607 
608     if (memcmp(p1, p2, numBytes) != 0) return 0;
609 
610     pack1->cursor += numBytes;
611     pack2->cursor += numBytes;
612     return 1;
613 }
614 
_dnsPacket_isEqualQR(DnsPacket * pack1,DnsPacket * pack2)615 static int _dnsPacket_isEqualQR(DnsPacket* pack1, DnsPacket* pack2) {
616     /* compare domain name encoding + TYPE + CLASS */
617     if (!_dnsPacket_isEqualDomainName(pack1, pack2) ||
618         !_dnsPacket_isEqualBytes(pack1, pack2, 2 + 2))
619         return 0;
620 
621     return 1;
622 }
623 
_dnsPacket_isEqualRR(DnsPacket * pack1,DnsPacket * pack2)624 static int _dnsPacket_isEqualRR(DnsPacket* pack1, DnsPacket* pack2) {
625     int rdlength1, rdlength2;
626     /* compare query + TTL */
627     if (!_dnsPacket_isEqualQR(pack1, pack2) || !_dnsPacket_isEqualBytes(pack1, pack2, 4)) return 0;
628 
629     /* compare RDATA */
630     rdlength1 = _dnsPacket_readInt16(pack1);
631     rdlength2 = _dnsPacket_readInt16(pack2);
632     if (rdlength1 != rdlength2 || !_dnsPacket_isEqualBytes(pack1, pack2, rdlength1)) return 0;
633 
634     return 1;
635 }
636 
_dnsPacket_isEqualQuery(DnsPacket * pack1,DnsPacket * pack2)637 static int _dnsPacket_isEqualQuery(DnsPacket* pack1, DnsPacket* pack2) {
638     int count1, count2, arcount1, arcount2;
639 
640     /* compare the headers, ignore most fields */
641     _dnsPacket_rewind(pack1);
642     _dnsPacket_rewind(pack2);
643 
644     /* compare RD, ignore TC, see comment in _dnsPacket_checkQuery */
645     if ((pack1->base[2] & 1) != (pack2->base[2] & 1)) {
646         LOG(INFO) << __func__ << ": different RD";
647         return 0;
648     }
649 
650     if (pack1->base[3] != pack2->base[3]) {
651         LOG(INFO) << __func__ << ": different CD or AD";
652         return 0;
653     }
654 
655     /* mark ID and header bytes as compared */
656     _dnsPacket_skip(pack1, 4);
657     _dnsPacket_skip(pack2, 4);
658 
659     /* compare QDCOUNT */
660     count1 = _dnsPacket_readInt16(pack1);
661     count2 = _dnsPacket_readInt16(pack2);
662     if (count1 != count2 || count1 < 0) {
663         LOG(INFO) << __func__ << ": different QDCOUNT";
664         return 0;
665     }
666 
667     /* assume: ANcount and NScount are 0 */
668     _dnsPacket_skip(pack1, 4);
669     _dnsPacket_skip(pack2, 4);
670 
671     /* compare ARCOUNT */
672     arcount1 = _dnsPacket_readInt16(pack1);
673     arcount2 = _dnsPacket_readInt16(pack2);
674     if (arcount1 != arcount2 || arcount1 < 0) {
675         LOG(INFO) << __func__ << ": different ARCOUNT";
676         return 0;
677     }
678 
679     /* compare the QDCOUNT QRs */
680     for (; count1 > 0; count1--) {
681         if (!_dnsPacket_isEqualQR(pack1, pack2)) {
682             LOG(INFO) << __func__ << ": different QR";
683             return 0;
684         }
685     }
686 
687     /* compare the ARCOUNT RRs */
688     for (; arcount1 > 0; arcount1--) {
689         if (!_dnsPacket_isEqualRR(pack1, pack2)) {
690             LOG(INFO) << __func__ << ": different additional RR";
691             return 0;
692         }
693     }
694     return 1;
695 }
696 
697 /* cache entry. for simplicity, 'hash' and 'hlink' are inlined in this
698  * structure though they are conceptually part of the hash table.
699  *
700  * similarly, mru_next and mru_prev are part of the global MRU list
701  */
702 struct Entry {
703     unsigned int hash;   /* hash value */
704     struct Entry* hlink; /* next in collision chain */
705     struct Entry* mru_prev;
706     struct Entry* mru_next;
707 
708     const uint8_t* query;
709     int querylen;
710     const uint8_t* answer;
711     int answerlen;
712     time_t expires; /* time_t when the entry isn't valid any more */
713     int id;         /* for debugging purpose */
714 };
715 
716 /*
717  * Find the TTL for a negative DNS result.  This is defined as the minimum
718  * of the SOA records TTL and the MINIMUM-TTL field (RFC-2308).
719  *
720  * Return 0 if not found.
721  */
answer_getNegativeTTL(ns_msg handle)722 static uint32_t answer_getNegativeTTL(ns_msg handle) {
723     int n, nscount;
724     uint32_t result = 0;
725     ns_rr rr;
726 
727     nscount = ns_msg_count(handle, ns_s_ns);
728     for (n = 0; n < nscount; n++) {
729         if ((ns_parserr(&handle, ns_s_ns, n, &rr) == 0) && (ns_rr_type(rr) == ns_t_soa)) {
730             const uint8_t* rdata = ns_rr_rdata(rr);          // find the data
731             const uint8_t* edata = rdata + ns_rr_rdlen(rr);  // add the len to find the end
732             int len;
733             uint32_t ttl, rec_result = rr.ttl;
734 
735             // find the MINIMUM-TTL field from the blob of binary data for this record
736             // skip the server name
737             len = dn_skipname(rdata, edata);
738             if (len == -1) continue;  // error skipping
739             rdata += len;
740 
741             // skip the admin name
742             len = dn_skipname(rdata, edata);
743             if (len == -1) continue;  // error skipping
744             rdata += len;
745 
746             if (edata - rdata != 5 * NS_INT32SZ) continue;
747             // skip: serial number + refresh interval + retry interval + expiry
748             rdata += NS_INT32SZ * 4;
749             // finally read the MINIMUM TTL
750             ttl = ntohl(*reinterpret_cast<const uint32_t*>(rdata));
751             if (ttl < rec_result) {
752                 rec_result = ttl;
753             }
754             // Now that the record is read successfully, apply the new min TTL
755             if (n == 0 || rec_result < result) {
756                 result = rec_result;
757             }
758         }
759     }
760     return result;
761 }
762 
763 /*
764  * Parse the answer records and find the appropriate
765  * smallest TTL among the records.  This might be from
766  * the answer records if found or from the SOA record
767  * if it's a negative result.
768  *
769  * The returned TTL is the number of seconds to
770  * keep the answer in the cache.
771  *
772  * In case of parse error zero (0) is returned which
773  * indicates that the answer shall not be cached.
774  */
answer_getTTL(span<const uint8_t> answer)775 static uint32_t answer_getTTL(span<const uint8_t> answer) {
776     ns_msg handle;
777     int ancount, n;
778     uint32_t result, ttl;
779     ns_rr rr;
780 
781     result = 0;
782     if (ns_initparse(answer.data(), answer.size(), &handle) >= 0) {
783         // get number of answer records
784         ancount = ns_msg_count(handle, ns_s_an);
785 
786         if (ancount == 0) {
787             // a response with no answers?  Cache this negative result.
788             result = answer_getNegativeTTL(handle);
789         } else {
790             for (n = 0; n < ancount; n++) {
791                 if (ns_parserr(&handle, ns_s_an, n, &rr) == 0) {
792                     ttl = rr.ttl;
793                     if (n == 0 || ttl < result) {
794                         result = ttl;
795                     }
796                 } else {
797                     PLOG(INFO) << __func__ << ": ns_parserr failed ancount no = " << n;
798                 }
799             }
800         }
801     } else {
802         PLOG(INFO) << __func__ << ": ns_initparse failed";
803     }
804 
805     LOG(INFO) << __func__ << ": TTL = " << result;
806     return result;
807 }
808 
entry_free(Entry * e)809 static void entry_free(Entry* e) {
810     /* everything is allocated in a single memory block */
811     if (e) {
812         free(e);
813     }
814 }
815 
entry_mru_remove(Entry * e)816 static void entry_mru_remove(Entry* e) {
817     e->mru_prev->mru_next = e->mru_next;
818     e->mru_next->mru_prev = e->mru_prev;
819 }
820 
entry_mru_add(Entry * e,Entry * list)821 static void entry_mru_add(Entry* e, Entry* list) {
822     Entry* first = list->mru_next;
823 
824     e->mru_next = first;
825     e->mru_prev = list;
826 
827     list->mru_next = e;
828     first->mru_prev = e;
829 }
830 
831 /* compute the hash of a given entry, this is a hash of most
832  * data in the query (key) */
entry_hash(const Entry * e)833 static unsigned entry_hash(const Entry* e) {
834     DnsPacket pack[1];
835 
836     _dnsPacket_init(pack, e->query, e->querylen);
837     return _dnsPacket_hashQuery(pack);
838 }
839 
840 /* initialize an Entry as a search key, this also checks the input query packet
841  * returns 1 on success, or 0 in case of unsupported/malformed data */
entry_init_key(Entry * e,span<const uint8_t> query)842 static int entry_init_key(Entry* e, span<const uint8_t> query) {
843     DnsPacket pack[1];
844 
845     memset(e, 0, sizeof(*e));
846 
847     e->query = query.data();
848     e->querylen = query.size();
849     e->hash = entry_hash(e);
850 
851     _dnsPacket_init(pack, e->query, e->querylen);
852 
853     return _dnsPacket_checkQuery(pack);
854 }
855 
856 /* allocate a new entry as a cache node */
entry_alloc(const Entry * init,span<const uint8_t> answer)857 static Entry* entry_alloc(const Entry* init, span<const uint8_t> answer) {
858     Entry* e;
859     int size;
860 
861     size = sizeof(*e) + init->querylen + answer.size();
862     e = (Entry*) calloc(size, 1);
863     if (e == NULL) return e;
864 
865     e->hash = init->hash;
866     e->query = (const uint8_t*) (e + 1);
867     e->querylen = init->querylen;
868 
869     memcpy((char*) e->query, init->query, e->querylen);
870 
871     e->answer = e->query + e->querylen;
872     e->answerlen = answer.size();
873 
874     memcpy((char*)e->answer, answer.data(), e->answerlen);
875 
876     return e;
877 }
878 
entry_equals(const Entry * e1,const Entry * e2)879 static int entry_equals(const Entry* e1, const Entry* e2) {
880     DnsPacket pack1[1], pack2[1];
881 
882     if (e1->querylen != e2->querylen) {
883         return 0;
884     }
885     _dnsPacket_init(pack1, e1->query, e1->querylen);
886     _dnsPacket_init(pack2, e2->query, e2->querylen);
887 
888     return _dnsPacket_isEqualQuery(pack1, pack2);
889 }
890 
891 /* We use a simple hash table with external collision lists
892  * for simplicity, the hash-table fields 'hash' and 'hlink' are
893  * inlined in the Entry structure.
894  */
895 
896 /* Maximum time for a thread to wait for an pending request */
897 constexpr int PENDING_REQUEST_TIMEOUT = 20;
898 
899 // lock protecting everything in NetConfig.
900 static std::mutex cache_mutex;
901 static std::condition_variable cv;
902 
903 namespace {
904 
905 // Map format: ReturnCode:rate_denom
906 // if the ReturnCode is not associated with any rate_denom, use default
907 // Sampling rate varies by return code; events to log are chosen randomly, with a
908 // probability proportional to the sampling rate.
909 constexpr const char DEFAULT_SUBSAMPLING_MAP[] = "default:8 0:400 2:110 7:110";
910 constexpr const char DEFAULT_MDNS_SUBSAMPLING_MAP[] = "default:1";
911 
resolv_get_dns_event_subsampling_map(bool isMdns)912 std::unordered_map<int, uint32_t> resolv_get_dns_event_subsampling_map(bool isMdns) {
913     using android::base::ParseInt;
914     using android::base::ParseUint;
915     using android::base::Split;
916     using server_configurable_flags::GetServerConfigurableFlag;
917     std::unordered_map<int, uint32_t> sampling_rate_map{};
918     const char* flag = isMdns ? "mdns_event_subsample_map" : "dns_event_subsample_map";
919     const char* defaultMap = isMdns ? DEFAULT_MDNS_SUBSAMPLING_MAP : DEFAULT_SUBSAMPLING_MAP;
920     const std::vector<std::string> subsampling_vector =
921             Split(GetServerConfigurableFlag("netd_native", flag, defaultMap), " ");
922 
923     for (const auto& pair : subsampling_vector) {
924         std::vector<std::string> rate_denom = Split(pair, ":");
925         int return_code;
926         uint32_t denom;
927         if (rate_denom.size() != 2) {
928             LOG(ERROR) << __func__ << ": invalid subsampling_pair = " << pair;
929             continue;
930         }
931         if (rate_denom[0] == "default") {
932             return_code = DNSEVENT_SUBSAMPLING_MAP_DEFAULT_KEY;
933         } else if (!ParseInt(rate_denom[0], &return_code)) {
934             LOG(ERROR) << __func__ << ": parse subsampling_pair failed = " << pair;
935             continue;
936         }
937         if (!ParseUint(rate_denom[1], &denom)) {
938             LOG(ERROR) << __func__ << ": parse subsampling_pair failed = " << pair;
939             continue;
940         }
941         sampling_rate_map[return_code] = denom;
942     }
943     return sampling_rate_map;
944 }
945 
946 }  // namespace
947 
948 // Note that Cache is not thread-safe per se, access to its members must be protected
949 // by an external mutex.
950 //
951 // TODO: move all cache manipulation code here and make data members private.
952 struct Cache {
CacheCache953     Cache() {
954         entries.resize(CONFIG_MAX_ENTRIES);
955         mru_list.mru_prev = mru_list.mru_next = &mru_list;
956     }
~CacheCache957     ~Cache() { flush(); }
958 
flushCache959     void flush() {
960         for (int nn = 0; nn < CONFIG_MAX_ENTRIES; nn++) {
961             Entry** pnode = (Entry**)&entries[nn];
962 
963             while (*pnode) {
964                 Entry* node = *pnode;
965                 *pnode = node->hlink;
966                 entry_free(node);
967             }
968         }
969 
970         flushPendingRequests();
971 
972         mru_list.mru_next = mru_list.mru_prev = &mru_list;
973         num_entries = 0;
974         last_id = 0;
975 
976         LOG(INFO) << "DNS cache flushed";
977     }
978 
flushPendingRequestsCache979     void flushPendingRequests() {
980         pending_req_info* ri = pending_requests.next;
981         while (ri) {
982             pending_req_info* tmp = ri;
983             ri = ri->next;
984             free(tmp);
985         }
986 
987         pending_requests.next = nullptr;
988         cv.notify_all();
989     }
990 
991     int num_entries = 0;
992 
993     // TODO: convert to std::list
994     Entry mru_list;
995     int last_id = 0;
996     std::vector<Entry> entries;
997 
998     // TODO: convert to std::vector
999     struct pending_req_info {
1000         unsigned int hash;
1001         struct pending_req_info* next;
1002     } pending_requests{};
1003 };
1004 
1005 struct NetConfig {
NetConfigNetConfig1006     explicit NetConfig(unsigned netId) : netid(netId) {
1007         cache = std::make_unique<Cache>();
1008         dns_event_subsampling_map = resolv_get_dns_event_subsampling_map(false);
1009         mdns_event_subsampling_map = resolv_get_dns_event_subsampling_map(true);
1010     }
nameserverCountNetConfig1011     int nameserverCount() { return nameserverSockAddrs.size(); }
setOptionsNetConfig1012     int setOptions(const ResolverOptionsParcel& resolverOptions) {
1013         customizedTable.clear();
1014         for (const auto& host : resolverOptions.hosts) {
1015             if (!host.hostName.empty() && !host.ipAddr.empty())
1016                 customizedTable.emplace(host.hostName, host.ipAddr);
1017         }
1018 
1019         if (resolverOptions.tcMode < aidl::android::net::IDnsResolver::TC_MODE_DEFAULT ||
1020             resolverOptions.tcMode > aidl::android::net::IDnsResolver::TC_MODE_UDP_TCP) {
1021             LOG(WARNING) << __func__ << ": netid = " << netid
1022                          << ", invalid TC mode: " << resolverOptions.tcMode;
1023             return -EINVAL;
1024         }
1025         tc_mode = resolverOptions.tcMode;
1026         enforceDnsUid = resolverOptions.enforceDnsUid;
1027         return 0;
1028     }
1029     const unsigned netid;
1030     std::unique_ptr<Cache> cache;
1031     std::vector<std::string> nameservers;
1032     std::vector<IPSockAddr> nameserverSockAddrs;
1033     int revision_id = 0;  // # times the nameservers have been replaced
1034     res_params params{};
1035     res_stats nsstats[MAXNS]{};
1036     std::vector<std::string> search_domains;
1037     int wait_for_pending_req_timeout_count = 0;
1038     // Map format: ReturnCode:rate_denom
1039     std::unordered_map<int, uint32_t> dns_event_subsampling_map;
1040     std::unordered_map<int, uint32_t> mdns_event_subsampling_map;
1041     DnsStats dnsStats;
1042 
1043     // Customized hostname/address table will be stored in customizedTable.
1044     // If resolverParams.hosts is empty, the existing customized table will be erased.
1045     typedef std::multimap<std::string /* hostname */, std::string /* IPv4/IPv6 address */>
1046             HostMapping;
1047     HostMapping customizedTable = {};
1048 
1049     int tc_mode = aidl::android::net::IDnsResolver::TC_MODE_DEFAULT;
1050     bool enforceDnsUid = false;
1051     std::vector<int32_t> transportTypes;
1052 };
1053 
1054 /* gets cache associated with a network, or NULL if none exists */
1055 static Cache* find_named_cache_locked(unsigned netid) REQUIRES(cache_mutex);
1056 
1057 // Return true - if there is a pending request in |cache| matching |key|.
1058 // Return false - if no pending request is found matching the key. Optionally
1059 //                link a new one if parameter append_if_not_found is true.
cache_has_pending_request_locked(Cache * cache,const Entry * key,bool append_if_not_found)1060 static bool cache_has_pending_request_locked(Cache* cache, const Entry* key,
1061                                              bool append_if_not_found) {
1062     if (!cache || !key) return false;
1063 
1064     Cache::pending_req_info* ri = cache->pending_requests.next;
1065     Cache::pending_req_info* prev = &cache->pending_requests;
1066     while (ri) {
1067         if (ri->hash == key->hash) {
1068             return true;
1069         }
1070         prev = ri;
1071         ri = ri->next;
1072     }
1073 
1074     if (append_if_not_found) {
1075         ri = (Cache::pending_req_info*)calloc(1, sizeof(Cache::pending_req_info));
1076         if (ri) {
1077             ri->hash = key->hash;
1078             prev->next = ri;
1079         }
1080     }
1081     return false;
1082 }
1083 
1084 // Notify all threads that the cache entry |key| has become available
cache_notify_waiting_tid_locked(struct Cache * cache,const Entry * key)1085 static void cache_notify_waiting_tid_locked(struct Cache* cache, const Entry* key) {
1086     if (!cache || !key) return;
1087 
1088     Cache::pending_req_info* ri = cache->pending_requests.next;
1089     Cache::pending_req_info* prev = &cache->pending_requests;
1090     while (ri) {
1091         if (ri->hash == key->hash) {
1092             // remove item from list and destroy
1093             prev->next = ri->next;
1094             free(ri);
1095             cv.notify_all();
1096             return;
1097         }
1098         prev = ri;
1099         ri = ri->next;
1100     }
1101 }
1102 
_resolv_cache_query_failed(unsigned netid,span<const uint8_t> query,uint32_t flags)1103 void _resolv_cache_query_failed(unsigned netid, span<const uint8_t> query, uint32_t flags) {
1104     // We should not notify with these flags.
1105     if (flags & (ANDROID_RESOLV_NO_CACHE_STORE | ANDROID_RESOLV_NO_CACHE_LOOKUP)) {
1106         return;
1107     }
1108     Entry key[1];
1109 
1110     if (!entry_init_key(key, query)) return;
1111 
1112     std::lock_guard guard(cache_mutex);
1113 
1114     Cache* cache = find_named_cache_locked(netid);
1115 
1116     if (cache) {
1117         cache_notify_waiting_tid_locked(cache, key);
1118     }
1119 }
1120 
cache_dump_mru_locked(Cache * cache)1121 static void cache_dump_mru_locked(Cache* cache) {
1122     std::string buf = fmt::format("MRU LIST ({:2d}): ", cache->num_entries);
1123     for (Entry* e = cache->mru_list.mru_next; e != &cache->mru_list; e = e->mru_next) {
1124         fmt::format_to(std::back_inserter(buf), " {}", e->id);
1125     }
1126 
1127     LOG(INFO) << __func__ << ": " << buf;
1128 }
1129 
1130 /* This function tries to find a key within the hash table
1131  * In case of success, it will return a *pointer* to the hashed key.
1132  * In case of failure, it will return a *pointer* to NULL
1133  *
1134  * So, the caller must check '*result' to check for success/failure.
1135  *
1136  * The main idea is that the result can later be used directly in
1137  * calls to resolv_cache_add or _resolv_cache_remove as the 'lookup'
1138  * parameter. This makes the code simpler and avoids re-searching
1139  * for the key position in the htable.
1140  *
1141  * The result of a lookup_p is only valid until you alter the hash
1142  * table.
1143  */
_cache_lookup_p(Cache * cache,Entry * key)1144 static Entry** _cache_lookup_p(Cache* cache, Entry* key) {
1145     int index = key->hash % CONFIG_MAX_ENTRIES;
1146     Entry** pnode = (Entry**) &cache->entries[index];
1147 
1148     while (*pnode != NULL) {
1149         Entry* node = *pnode;
1150 
1151         if (node == NULL) break;
1152 
1153         if (node->hash == key->hash && entry_equals(node, key)) break;
1154 
1155         pnode = &node->hlink;
1156     }
1157     return pnode;
1158 }
1159 
1160 /* Add a new entry to the hash table. 'lookup' must be the
1161  * result of an immediate previous failed _lookup_p() call
1162  * (i.e. with *lookup == NULL), and 'e' is the pointer to the
1163  * newly created entry
1164  */
_cache_add_p(Cache * cache,Entry ** lookup,Entry * e)1165 static void _cache_add_p(Cache* cache, Entry** lookup, Entry* e) {
1166     *lookup = e;
1167     e->id = ++cache->last_id;
1168     entry_mru_add(e, &cache->mru_list);
1169     cache->num_entries += 1;
1170 
1171     LOG(INFO) << __func__ << ": entry " << e->id << " added (count=" << cache->num_entries << ")";
1172 }
1173 
1174 /* Remove an existing entry from the hash table,
1175  * 'lookup' must be the result of an immediate previous
1176  * and succesful _lookup_p() call.
1177  */
_cache_remove_p(Cache * cache,Entry ** lookup)1178 static void _cache_remove_p(Cache* cache, Entry** lookup) {
1179     Entry* e = *lookup;
1180 
1181     LOG(INFO) << __func__ << ": entry " << e->id << " removed (count=" << cache->num_entries - 1
1182               << ")";
1183 
1184     entry_mru_remove(e);
1185     *lookup = e->hlink;
1186     entry_free(e);
1187     cache->num_entries -= 1;
1188 }
1189 
1190 /* Remove the oldest entry from the hash table.
1191  */
_cache_remove_oldest(Cache * cache)1192 static void _cache_remove_oldest(Cache* cache) {
1193     Entry* oldest = cache->mru_list.mru_prev;
1194     Entry** lookup = _cache_lookup_p(cache, oldest);
1195 
1196     if (*lookup == NULL) { /* should not happen */
1197         LOG(INFO) << __func__ << ": OLDEST NOT IN HTABLE ?";
1198         return;
1199     }
1200     LOG(INFO) << __func__ << ": Cache full - removing oldest";
1201     res_pquery({oldest->query, oldest->querylen});
1202     _cache_remove_p(cache, lookup);
1203 }
1204 
1205 /* Remove all expired entries from the hash table.
1206  */
_cache_remove_expired(Cache * cache)1207 static void _cache_remove_expired(Cache* cache) {
1208     Entry* e;
1209     time_t now = _time_now();
1210 
1211     for (e = cache->mru_list.mru_next; e != &cache->mru_list;) {
1212         // Entry is old, remove
1213         if (now >= e->expires) {
1214             Entry** lookup = _cache_lookup_p(cache, e);
1215             if (*lookup == NULL) { /* should not happen */
1216                 LOG(INFO) << __func__ << ": ENTRY NOT IN HTABLE ?";
1217                 return;
1218             }
1219             e = e->mru_next;
1220             _cache_remove_p(cache, lookup);
1221         } else {
1222             e = e->mru_next;
1223         }
1224     }
1225 }
1226 
1227 // Get a NetConfig associated with a network, or nullptr if not found.
1228 static NetConfig* find_netconfig_locked(unsigned netid) REQUIRES(cache_mutex);
1229 
resolv_cache_lookup(unsigned netid,span<const uint8_t> query,span<uint8_t> answer,int * answerlen,uint32_t flags)1230 ResolvCacheStatus resolv_cache_lookup(unsigned netid, span<const uint8_t> query,
1231                                       span<uint8_t> answer, int* answerlen, uint32_t flags) {
1232     // Skip cache lookup, return RESOLV_CACHE_NOTFOUND directly so that it is
1233     // possible to cache the answer of this query.
1234     // If ANDROID_RESOLV_NO_CACHE_STORE is set, return RESOLV_CACHE_SKIP to skip possible cache
1235     // storing.
1236     // (b/150371903): ANDROID_RESOLV_NO_CACHE_STORE should imply ANDROID_RESOLV_NO_CACHE_LOOKUP
1237     // to avoid side channel attack.
1238     if (flags & (ANDROID_RESOLV_NO_CACHE_LOOKUP | ANDROID_RESOLV_NO_CACHE_STORE)) {
1239         return flags & ANDROID_RESOLV_NO_CACHE_STORE ? RESOLV_CACHE_SKIP : RESOLV_CACHE_NOTFOUND;
1240     }
1241     Entry key;
1242     Entry** lookup;
1243     Entry* e;
1244     time_t now;
1245 
1246     LOG(INFO) << __func__ << ": lookup";
1247 
1248     /* we don't cache malformed queries */
1249     if (!entry_init_key(&key, query)) {
1250         LOG(INFO) << __func__ << ": unsupported query";
1251         return RESOLV_CACHE_UNSUPPORTED;
1252     }
1253     /* lookup cache */
1254     std::unique_lock lock(cache_mutex);
1255     android::base::ScopedLockAssertion assume_lock(cache_mutex);
1256     Cache* cache = find_named_cache_locked(netid);
1257     if (cache == nullptr) {
1258         return RESOLV_CACHE_UNSUPPORTED;
1259     }
1260 
1261     /* see the description of _lookup_p to understand this.
1262      * the function always return a non-NULL pointer.
1263      */
1264     lookup = _cache_lookup_p(cache, &key);
1265     e = *lookup;
1266 
1267     if (e == NULL) {
1268         LOG(INFO) << __func__ << ": NOT IN CACHE";
1269 
1270         if (!cache_has_pending_request_locked(cache, &key, true)) {
1271             return RESOLV_CACHE_NOTFOUND;
1272 
1273         } else {
1274             LOG(INFO) << __func__ << ": Waiting for previous request";
1275             // wait until (1) timeout OR
1276             //            (2) cv is notified AND no pending request matching the |key|
1277             // (cv notifier should delete pending request before sending notification.)
1278             bool ret = cv.wait_for(lock, std::chrono::seconds(PENDING_REQUEST_TIMEOUT),
1279                                    [netid, &cache, &key]() REQUIRES(cache_mutex) {
1280                                        // Must update cache as it could have been deleted
1281                                        cache = find_named_cache_locked(netid);
1282                                        return !cache_has_pending_request_locked(cache, &key, false);
1283                                    });
1284             if (!cache) {
1285                 return RESOLV_CACHE_NOTFOUND;
1286             }
1287             if (ret == false) {
1288                 NetConfig* info = find_netconfig_locked(netid);
1289                 if (info != NULL) {
1290                     info->wait_for_pending_req_timeout_count++;
1291                 }
1292             }
1293             lookup = _cache_lookup_p(cache, &key);
1294             e = *lookup;
1295             if (e == NULL) {
1296                 return RESOLV_CACHE_NOTFOUND;
1297             }
1298         }
1299     }
1300 
1301     now = _time_now();
1302 
1303     /* remove stale entries here */
1304     if (now >= e->expires) {
1305         LOG(INFO) << __func__ << ": NOT IN CACHE (STALE ENTRY " << *lookup << "DISCARDED)";
1306         res_pquery({e->query, e->querylen});
1307         _cache_remove_p(cache, lookup);
1308         return RESOLV_CACHE_NOTFOUND;
1309     }
1310 
1311     *answerlen = e->answerlen;
1312     if (e->answerlen > answer.size()) {
1313         /* NOTE: we return UNSUPPORTED if the answer buffer is too short */
1314         LOG(INFO) << __func__ << ": ANSWER TOO LONG";
1315         return RESOLV_CACHE_UNSUPPORTED;
1316     }
1317 
1318     memcpy(answer.data(), e->answer, e->answerlen);
1319 
1320     /* bump up this entry to the top of the MRU list */
1321     if (e != cache->mru_list.mru_next) {
1322         entry_mru_remove(e);
1323         entry_mru_add(e, &cache->mru_list);
1324     }
1325 
1326     LOG(INFO) << __func__ << ": FOUND IN CACHE entry=" << e;
1327     return RESOLV_CACHE_FOUND;
1328 }
1329 
resolv_cache_add(unsigned netid,span<const uint8_t> query,span<const uint8_t> answer)1330 int resolv_cache_add(unsigned netid, span<const uint8_t> query, span<const uint8_t> answer) {
1331     Entry key[1];
1332     Entry* e;
1333     Entry** lookup;
1334     uint32_t ttl;
1335     Cache* cache = NULL;
1336 
1337     /* don't assume that the query has already been cached
1338      */
1339     if (!entry_init_key(key, query)) {
1340         LOG(INFO) << __func__ << ": passed invalid query?";
1341         return -EINVAL;
1342     }
1343 
1344     std::lock_guard guard(cache_mutex);
1345 
1346     cache = find_named_cache_locked(netid);
1347     if (cache == nullptr) {
1348         return -ENONET;
1349     }
1350 
1351     lookup = _cache_lookup_p(cache, key);
1352     e = *lookup;
1353 
1354     // Should only happen on ANDROID_RESOLV_NO_CACHE_LOOKUP
1355     if (e != NULL) {
1356         LOG(INFO) << __func__ << ": ALREADY IN CACHE (" << e << ") ? IGNORING ADD";
1357         cache_notify_waiting_tid_locked(cache, key);
1358         return -EEXIST;
1359     }
1360 
1361     if (cache->num_entries >= CONFIG_MAX_ENTRIES) {
1362         _cache_remove_expired(cache);
1363         if (cache->num_entries >= CONFIG_MAX_ENTRIES) {
1364             _cache_remove_oldest(cache);
1365         }
1366         // TODO: It looks useless, remove below code after having test to prove it.
1367         lookup = _cache_lookup_p(cache, key);
1368         e = *lookup;
1369         if (e != NULL) {
1370             LOG(INFO) << __func__ << ": ALREADY IN CACHE (" << e << ") ? IGNORING ADD";
1371             cache_notify_waiting_tid_locked(cache, key);
1372             return -EEXIST;
1373         }
1374     }
1375 
1376     ttl = answer_getTTL(answer);
1377     if (ttl > 0) {
1378         e = entry_alloc(key, answer);
1379         if (e != NULL) {
1380             e->expires = ttl + _time_now();
1381             _cache_add_p(cache, lookup, e);
1382         }
1383     }
1384 
1385     cache_dump_mru_locked(cache);
1386     cache_notify_waiting_tid_locked(cache, key);
1387 
1388     return 0;
1389 }
1390 
resolv_gethostbyaddr_from_cache(unsigned netid,char domain_name[],size_t domain_name_size,const char * ip_address,int af)1391 bool resolv_gethostbyaddr_from_cache(unsigned netid, char domain_name[], size_t domain_name_size,
1392                                      const char* ip_address, int af) {
1393     if (domain_name_size > NS_MAXDNAME) {
1394         LOG(WARNING) << __func__ << ": invalid domain_name_size " << domain_name_size;
1395         return false;
1396     } else if (ip_address == nullptr || ip_address[0] == '\0') {
1397         LOG(WARNING) << __func__ << ": invalid ip_address";
1398         return false;
1399     } else if (af != AF_INET && af != AF_INET6) {
1400         LOG(WARNING) << __func__ << ": unsupported AF";
1401         return false;
1402     }
1403 
1404     Cache* cache = nullptr;
1405     Entry* node = nullptr;
1406 
1407     ns_rr rr;
1408     ns_msg handle;
1409     ns_rr rr_query;
1410 
1411     struct sockaddr_in sa;
1412     struct sockaddr_in6 sa6;
1413     char* addr_buf = nullptr;
1414 
1415     std::lock_guard guard(cache_mutex);
1416 
1417     cache = find_named_cache_locked(netid);
1418     if (cache == nullptr) {
1419         return false;
1420     }
1421 
1422     for (node = cache->mru_list.mru_next; node != nullptr && node != &cache->mru_list;
1423          node = node->mru_next) {
1424         if (node->answer == nullptr) {
1425             continue;
1426         }
1427 
1428         memset(&handle, 0, sizeof(handle));
1429 
1430         if (ns_initparse(node->answer, node->answerlen, &handle) < 0) {
1431             continue;
1432         }
1433 
1434         for (int n = 0; n < ns_msg_count(handle, ns_s_an); n++) {
1435             memset(&rr, 0, sizeof(rr));
1436 
1437             if (ns_parserr(&handle, ns_s_an, n, &rr)) {
1438                 continue;
1439             }
1440 
1441             if (ns_rr_type(rr) == ns_t_a && af == AF_INET) {
1442                 addr_buf = (char*)&(sa.sin_addr);
1443             } else if (ns_rr_type(rr) == ns_t_aaaa && af == AF_INET6) {
1444                 addr_buf = (char*)&(sa6.sin6_addr);
1445             } else {
1446                 continue;
1447             }
1448 
1449             if (inet_pton(af, ip_address, addr_buf) != 1) {
1450                 LOG(WARNING) << __func__ << ": inet_pton() fail";
1451                 return false;
1452             }
1453 
1454             if (memcmp(ns_rr_rdata(rr), addr_buf, ns_rr_rdlen(rr)) == 0) {
1455                 int query_count = ns_msg_count(handle, ns_s_qd);
1456                 for (int i = 0; i < query_count; i++) {
1457                     memset(&rr_query, 0, sizeof(rr_query));
1458                     if (ns_parserr(&handle, ns_s_qd, i, &rr_query)) {
1459                         continue;
1460                     }
1461                     strlcpy(domain_name, ns_rr_name(rr_query), domain_name_size);
1462                     if (domain_name[0] != '\0') {
1463                         return true;
1464                     }
1465                 }
1466             }
1467         }
1468     }
1469 
1470     return false;
1471 }
1472 
1473 static std::unordered_map<unsigned, std::unique_ptr<NetConfig>> sNetConfigMap
1474         GUARDED_BY(cache_mutex);
1475 
1476 // Clears nameservers set for |netconfig| and clears the stats
1477 static void free_nameservers_locked(NetConfig* netconfig);
1478 // Order-insensitive comparison for the two set of servers.
1479 static bool resolv_is_nameservers_equal(const std::vector<std::string>& oldServers,
1480                                         const std::vector<std::string>& newServers);
1481 // clears the stats samples contained withing the given netconfig.
1482 static void res_cache_clear_stats_locked(NetConfig* netconfig);
1483 
1484 // public API for netd to query if name server is set on specific netid
resolv_has_nameservers(unsigned netid)1485 bool resolv_has_nameservers(unsigned netid) {
1486     std::lock_guard guard(cache_mutex);
1487     NetConfig* info = find_netconfig_locked(netid);
1488     return (info != nullptr) && (info->nameserverCount() > 0);
1489 }
1490 
resolv_create_cache_for_net(unsigned netid)1491 int resolv_create_cache_for_net(unsigned netid) {
1492     std::lock_guard guard(cache_mutex);
1493     if (sNetConfigMap.find(netid) != sNetConfigMap.end()) {
1494         LOG(ERROR) << __func__ << ": Cache is already created, netId: " << netid;
1495         return -EEXIST;
1496     }
1497 
1498     sNetConfigMap[netid] = std::make_unique<NetConfig>(netid);
1499 
1500     return 0;
1501 }
1502 
resolv_delete_cache_for_net(unsigned netid)1503 void resolv_delete_cache_for_net(unsigned netid) {
1504     std::lock_guard guard(cache_mutex);
1505     sNetConfigMap.erase(netid);
1506 }
1507 
resolv_flush_cache_for_net(unsigned netid)1508 int resolv_flush_cache_for_net(unsigned netid) {
1509     std::lock_guard guard(cache_mutex);
1510 
1511     NetConfig* netconfig = find_netconfig_locked(netid);
1512     if (netconfig == nullptr) {
1513         return -ENONET;
1514     }
1515     netconfig->cache->flush();
1516 
1517     // Also clear the NS statistics.
1518     res_cache_clear_stats_locked(netconfig);
1519     return 0;
1520 }
1521 
resolv_list_caches()1522 std::vector<unsigned> resolv_list_caches() {
1523     std::lock_guard guard(cache_mutex);
1524     std::vector<unsigned> result;
1525     result.reserve(sNetConfigMap.size());
1526     for (const auto& [netId, _] : sNetConfigMap) {
1527         result.push_back(netId);
1528     }
1529     return result;
1530 }
1531 
find_named_cache_locked(unsigned netid)1532 static Cache* find_named_cache_locked(unsigned netid) {
1533     NetConfig* info = find_netconfig_locked(netid);
1534     if (info != nullptr) return info->cache.get();
1535     return nullptr;
1536 }
1537 
find_netconfig_locked(unsigned netid)1538 static NetConfig* find_netconfig_locked(unsigned netid) {
1539     if (auto it = sNetConfigMap.find(netid); it != sNetConfigMap.end()) {
1540         return it->second.get();
1541     }
1542     return nullptr;
1543 }
1544 
resolv_set_experiment_params(res_params * params)1545 static void resolv_set_experiment_params(res_params* params) {
1546     if (params->retry_count == 0) {
1547         params->retry_count = getExperimentFlagInt("retry_count", RES_DFLRETRY);
1548     }
1549 
1550     if (params->base_timeout_msec == 0) {
1551         params->base_timeout_msec =
1552                 getExperimentFlagInt("retransmission_time_interval", RES_TIMEOUT);
1553     }
1554 }
1555 
resolv_get_network_types_for_net(unsigned netid)1556 android::net::NetworkType resolv_get_network_types_for_net(unsigned netid) {
1557     std::lock_guard guard(cache_mutex);
1558     NetConfig* netconfig = find_netconfig_locked(netid);
1559     if (netconfig == nullptr) return android::net::NT_UNKNOWN;
1560     return convert_network_type(netconfig->transportTypes);
1561 }
1562 
is_mdns_supported_transport_types(const std::vector<int32_t> & transportTypes)1563 bool is_mdns_supported_transport_types(const std::vector<int32_t>& transportTypes) {
1564     for (const auto& tp : transportTypes) {
1565         if (tp == IDnsResolver::TRANSPORT_CELLULAR || tp == IDnsResolver::TRANSPORT_VPN) {
1566             return false;
1567         }
1568     }
1569     return true;
1570 }
1571 
is_mdns_supported_network(unsigned netid)1572 bool is_mdns_supported_network(unsigned netid) {
1573     std::lock_guard guard(cache_mutex);
1574     NetConfig* netconfig = find_netconfig_locked(netid);
1575     if (netconfig == nullptr) return false;
1576     return is_mdns_supported_transport_types(netconfig->transportTypes);
1577 }
1578 
1579 namespace {
1580 
1581 // Returns valid domains without duplicates which are limited to max size |MAXDNSRCH|.
filter_domains(const std::vector<std::string> & domains)1582 std::vector<std::string> filter_domains(const std::vector<std::string>& domains) {
1583     std::set<std::string> tmp_set;
1584     std::vector<std::string> res;
1585 
1586     std::copy_if(domains.begin(), domains.end(), std::back_inserter(res),
1587                  [&tmp_set](const std::string& str) {
1588                      return !(str.size() > MAXDNSRCHPATH - 1) && (tmp_set.insert(str).second);
1589                  });
1590     if (res.size() > MAXDNSRCH) {
1591         LOG(WARNING) << __func__ << ": valid domains=" << res.size()
1592                      << ", but MAXDNSRCH=" << MAXDNSRCH;
1593         res.resize(MAXDNSRCH);
1594     }
1595     return res;
1596 }
1597 
filter_nameservers(const std::vector<std::string> & servers)1598 std::vector<std::string> filter_nameservers(const std::vector<std::string>& servers) {
1599     std::vector<std::string> res = servers;
1600     if (res.size() > MAXNS) {
1601         LOG(WARNING) << __func__ << ": too many servers: " << res.size();
1602         res.resize(MAXNS);
1603     }
1604     return res;
1605 }
1606 
isValidServer(const std::string & server)1607 bool isValidServer(const std::string& server) {
1608     const addrinfo hints = {
1609             .ai_family = AF_UNSPEC,
1610             .ai_socktype = SOCK_DGRAM,
1611     };
1612     addrinfo* result = nullptr;
1613     if (int err = getaddrinfo_numeric(server.c_str(), "53", hints, &result); err != 0) {
1614         LOG(WARNING) << __func__ << ": getaddrinfo_numeric(" << server
1615                      << ") = " << gai_strerror(err);
1616         return false;
1617     }
1618     freeaddrinfo(result);
1619     return true;
1620 }
1621 
1622 }  // namespace
1623 
getCustomizedTableByName(const size_t netid,const char * hostname)1624 std::vector<std::string> getCustomizedTableByName(const size_t netid, const char* hostname) {
1625     std::lock_guard guard(cache_mutex);
1626     NetConfig* netconfig = find_netconfig_locked(netid);
1627 
1628     std::vector<std::string> result;
1629     if (netconfig != nullptr) {
1630         const auto& hosts = netconfig->customizedTable.equal_range(hostname);
1631         for (auto i = hosts.first; i != hosts.second; ++i) {
1632             result.push_back(i->second);
1633         }
1634     }
1635     return result;
1636 }
1637 
resolv_set_nameservers(unsigned netid,const std::vector<std::string> & servers,const std::vector<std::string> & domains,const res_params & params,const std::optional<ResolverOptionsParcel> optionalResolverOptions,const std::vector<int32_t> & transportTypes)1638 int resolv_set_nameservers(unsigned netid, const std::vector<std::string>& servers,
1639                            const std::vector<std::string>& domains, const res_params& params,
1640                            const std::optional<ResolverOptionsParcel> optionalResolverOptions,
1641                            const std::vector<int32_t>& transportTypes) {
1642     std::vector<std::string> nameservers = filter_nameservers(servers);
1643     const int numservers = static_cast<int>(nameservers.size());
1644 
1645     LOG(INFO) << __func__ << ": netId = " << netid << ", numservers = " << numservers;
1646 
1647     // Parse the addresses before actually locking or changing any state, in case there is an error.
1648     // As a side effect this also reduces the time the lock is kept.
1649     std::vector<IPSockAddr> ipSockAddrs;
1650     ipSockAddrs.reserve(nameservers.size());
1651     for (const auto& server : nameservers) {
1652         if (!isValidServer(server)) return -EINVAL;
1653         ipSockAddrs.push_back(IPSockAddr::toIPSockAddr(server, 53));
1654     }
1655 
1656     std::lock_guard guard(cache_mutex);
1657     NetConfig* netconfig = find_netconfig_locked(netid);
1658 
1659     if (netconfig == nullptr) return -ENONET;
1660 
1661     uint8_t old_max_samples = netconfig->params.max_samples;
1662     netconfig->params = params;
1663     resolv_set_experiment_params(&netconfig->params);
1664     if (!resolv_is_nameservers_equal(netconfig->nameservers, nameservers)) {
1665         // free current before adding new
1666         free_nameservers_locked(netconfig);
1667         netconfig->nameservers = std::move(nameservers);
1668         for (int i = 0; i < numservers; i++) {
1669             LOG(INFO) << __func__ << ": netid = " << netid
1670                       << ", addr = " << netconfig->nameservers[i];
1671         }
1672         netconfig->nameserverSockAddrs = std::move(ipSockAddrs);
1673     } else {
1674         if (netconfig->params.max_samples != old_max_samples) {
1675             // If the maximum number of samples changes, the overhead of keeping the most recent
1676             // samples around is not considered worth the effort, so they are cleared instead.
1677             // All other parameters do not affect shared state: Changing these parameters does
1678             // not invalidate the samples, as they only affect aggregation and the conditions
1679             // under which servers are considered usable.
1680             res_cache_clear_stats_locked(netconfig);
1681         }
1682     }
1683 
1684     // Always update the search paths. Cache-flushing however is not necessary,
1685     // since the stored cache entries do contain the domain, not just the host name.
1686     netconfig->search_domains = filter_domains(domains);
1687 
1688     // Setup stats for cleartext dns servers.
1689     if (!netconfig->dnsStats.setAddrs(netconfig->nameserverSockAddrs, PROTO_TCP) ||
1690         !netconfig->dnsStats.setAddrs(netconfig->nameserverSockAddrs, PROTO_UDP)) {
1691         LOG(WARNING) << __func__ << ": netid = " << netid << ", failed to set dns stats";
1692         return -EINVAL;
1693     }
1694     netconfig->transportTypes = transportTypes;
1695     if (optionalResolverOptions.has_value()) {
1696         const ResolverOptionsParcel& resolverOptions = optionalResolverOptions.value();
1697         return netconfig->setOptions(resolverOptions);
1698     }
1699     return 0;
1700 }
1701 
resolv_set_options(unsigned netid,const ResolverOptionsParcel & options)1702 int resolv_set_options(unsigned netid, const ResolverOptionsParcel& options) {
1703     std::lock_guard guard(cache_mutex);
1704     NetConfig* netconfig = find_netconfig_locked(netid);
1705 
1706     if (netconfig == nullptr) return -ENONET;
1707     return netconfig->setOptions(options);
1708 }
1709 
resolv_is_nameservers_equal(const std::vector<std::string> & oldServers,const std::vector<std::string> & newServers)1710 static bool resolv_is_nameservers_equal(const std::vector<std::string>& oldServers,
1711                                         const std::vector<std::string>& newServers) {
1712     const std::set<std::string> olds(oldServers.begin(), oldServers.end());
1713     const std::set<std::string> news(newServers.begin(), newServers.end());
1714 
1715     // TODO: this is incorrect if the list of current or previous nameservers
1716     // contains duplicates. This does not really matter because the framework
1717     // filters out duplicates, but we should probably fix it. It's also
1718     // insensitive to the order of the nameservers; we should probably fix that
1719     // too.
1720     return olds == news;
1721 }
1722 
free_nameservers_locked(NetConfig * netconfig)1723 static void free_nameservers_locked(NetConfig* netconfig) {
1724     netconfig->nameservers.clear();
1725     netconfig->nameserverSockAddrs.clear();
1726     res_cache_clear_stats_locked(netconfig);
1727 }
1728 
resolv_populate_res_for_net(ResState * statp)1729 void resolv_populate_res_for_net(ResState* statp) {
1730     if (statp == nullptr) {
1731         return;
1732     }
1733     LOG(INFO) << __func__ << ": netid=" << statp->netid;
1734 
1735     std::lock_guard guard(cache_mutex);
1736     NetConfig* info = find_netconfig_locked(statp->netid);
1737     if (info == nullptr) return;
1738 
1739     const bool sortNameservers = Experiments::getInstance()->getFlag("sort_nameservers", 0);
1740     statp->sort_nameservers = sortNameservers;
1741     statp->nsaddrs = sortNameservers ? info->dnsStats.getSortedServers(PROTO_UDP)
1742                                      : info->nameserverSockAddrs;
1743     statp->search_domains = info->search_domains;
1744     statp->tc_mode = info->tc_mode;
1745     statp->enforce_dns_uid = info->enforceDnsUid;
1746 }
1747 
1748 /* Resolver reachability statistics. */
1749 
res_cache_add_stats_sample_locked(res_stats * stats,const res_sample & sample,int max_samples)1750 static void res_cache_add_stats_sample_locked(res_stats* stats, const res_sample& sample,
1751                                               int max_samples) {
1752     // Note: This function expects max_samples > 0, otherwise a (harmless) modification of the
1753     // allocated but supposedly unused memory for samples[0] will happen
1754     LOG(INFO) << __func__ << ": adding sample to stats, next = " << unsigned(stats->sample_next)
1755               << ", count = " << unsigned(stats->sample_count);
1756     stats->samples[stats->sample_next] = sample;
1757     if (stats->sample_count < max_samples) {
1758         ++stats->sample_count;
1759     }
1760     if (++stats->sample_next >= max_samples) {
1761         stats->sample_next = 0;
1762     }
1763 }
1764 
res_cache_clear_stats_locked(NetConfig * netconfig)1765 static void res_cache_clear_stats_locked(NetConfig* netconfig) {
1766     for (int i = 0; i < MAXNS; ++i) {
1767         netconfig->nsstats[i].sample_count = 0;
1768         netconfig->nsstats[i].sample_next = 0;
1769     }
1770 
1771     // Increment the revision id to ensure that sample state is not written back if the
1772     // servers change; in theory it would suffice to do so only if the servers or
1773     // max_samples actually change, in practice the overhead of checking is higher than the
1774     // cost, and overflows are unlikely.
1775     ++netconfig->revision_id;
1776 }
1777 
android_net_res_stats_get_info_for_net(unsigned netid,int * nscount,struct sockaddr_storage servers[MAXNS],int * dcount,char domains[MAXDNSRCH][MAXDNSRCHPATH],res_params * params,struct res_stats stats[MAXNS],int * wait_for_pending_req_timeout_count)1778 int android_net_res_stats_get_info_for_net(unsigned netid, int* nscount,
1779                                            struct sockaddr_storage servers[MAXNS], int* dcount,
1780                                            char domains[MAXDNSRCH][MAXDNSRCHPATH],
1781                                            res_params* params, struct res_stats stats[MAXNS],
1782                                            int* wait_for_pending_req_timeout_count) {
1783     std::lock_guard guard(cache_mutex);
1784     NetConfig* info = find_netconfig_locked(netid);
1785     if (!info) return -1;
1786 
1787     const int num = info->nameserverCount();
1788     if (num > MAXNS) {
1789         LOG(INFO) << __func__ << ": nscount " << num << " > MAXNS " << MAXNS;
1790         errno = EFAULT;
1791         return -1;
1792     }
1793 
1794     for (int i = 0; i < num; i++) {
1795         servers[i] = info->nameserverSockAddrs[i];
1796         stats[i] = info->nsstats[i];
1797     }
1798 
1799     for (size_t i = 0; i < info->search_domains.size(); i++) {
1800         strlcpy(domains[i], info->search_domains[i].c_str(), MAXDNSRCHPATH);
1801     }
1802 
1803     *nscount = num;
1804     *dcount = static_cast<int>(info->search_domains.size());
1805     *params = info->params;
1806     *wait_for_pending_req_timeout_count = info->wait_for_pending_req_timeout_count;
1807 
1808     return info->revision_id;
1809 }
1810 
resolv_cache_dump_subsampling_map(unsigned netid,bool is_mdns)1811 std::vector<std::string> resolv_cache_dump_subsampling_map(unsigned netid, bool is_mdns) {
1812     std::lock_guard guard(cache_mutex);
1813     NetConfig* netconfig = find_netconfig_locked(netid);
1814     if (netconfig == nullptr) return {};
1815     std::vector<std::string> result;
1816     const auto& subsampling_map = (!is_mdns) ? netconfig->dns_event_subsampling_map
1817                                              : netconfig->mdns_event_subsampling_map;
1818     result.reserve(subsampling_map.size());
1819     for (const auto& [return_code, rate_denom] : subsampling_map) {
1820         result.push_back(fmt::format("{}:{}",
1821                                      (return_code == DNSEVENT_SUBSAMPLING_MAP_DEFAULT_KEY)
1822                                              ? "default"
1823                                              : std::to_string(return_code),
1824                                      rate_denom));
1825     }
1826     return result;
1827 }
1828 
1829 // Decides whether an event should be sampled using a random number generator and
1830 // a sampling factor derived from the netid and the return code.
1831 //
1832 // Returns the subsampling rate if the event should be sampled, or 0 if it should be discarded.
resolv_cache_get_subsampling_denom(unsigned netid,int return_code,bool is_mdns)1833 uint32_t resolv_cache_get_subsampling_denom(unsigned netid, int return_code, bool is_mdns) {
1834     std::lock_guard guard(cache_mutex);
1835     NetConfig* netconfig = find_netconfig_locked(netid);
1836     if (netconfig == nullptr) return 0;  // Don't log anything at all.
1837     const auto& subsampling_map = (!is_mdns) ? netconfig->dns_event_subsampling_map
1838                                              : netconfig->mdns_event_subsampling_map;
1839     auto search_returnCode = subsampling_map.find(return_code);
1840     uint32_t denom;
1841     if (search_returnCode != subsampling_map.end()) {
1842         denom = search_returnCode->second;
1843     } else {
1844         auto search_default = subsampling_map.find(DNSEVENT_SUBSAMPLING_MAP_DEFAULT_KEY);
1845         denom = (search_default == subsampling_map.end()) ? 0 : search_default->second;
1846     }
1847     return denom;
1848 }
1849 
resolv_cache_get_resolver_stats(unsigned netid,res_params * params,res_stats stats[MAXNS],const std::vector<IPSockAddr> & serverSockAddrs)1850 int resolv_cache_get_resolver_stats(unsigned netid, res_params* params, res_stats stats[MAXNS],
1851                                     const std::vector<IPSockAddr>& serverSockAddrs) {
1852     std::lock_guard guard(cache_mutex);
1853     NetConfig* info = find_netconfig_locked(netid);
1854     if (!info) return -1;
1855 
1856     for (size_t i = 0; i < serverSockAddrs.size(); i++) {
1857         for (size_t j = 0; j < info->nameserverSockAddrs.size(); j++) {
1858             // Should never happen. Just in case because of the fix-sized array |stats|.
1859             if (j >= MAXNS) {
1860                 LOG(WARNING) << __func__ << ": unexpected size " << j;
1861                 return -1;
1862             }
1863 
1864             // It's possible that the server is not found, e.g. when a new list of nameservers
1865             // is updated to the NetConfig just after this look up thread being populated.
1866             // Keep the server valid as-is (by means of keeping stats[i] unset), but we should
1867             // think about if there's a better way.
1868             if (info->nameserverSockAddrs[j] == serverSockAddrs[i]) {
1869                 stats[i] = info->nsstats[j];
1870                 break;
1871             }
1872         }
1873     }
1874 
1875     *params = info->params;
1876     return info->revision_id;
1877 }
1878 
resolv_cache_add_resolver_stats_sample(unsigned netid,int revision_id,const IPSockAddr & serverSockAddr,const res_sample & sample,int max_samples)1879 void resolv_cache_add_resolver_stats_sample(unsigned netid, int revision_id,
1880                                             const IPSockAddr& serverSockAddr,
1881                                             const res_sample& sample, int max_samples) {
1882     if (max_samples <= 0) return;
1883 
1884     std::lock_guard guard(cache_mutex);
1885     NetConfig* info = find_netconfig_locked(netid);
1886 
1887     if (info && info->revision_id == revision_id) {
1888         const int serverNum = std::min(MAXNS, static_cast<int>(info->nameserverSockAddrs.size()));
1889         for (int ns = 0; ns < serverNum; ns++) {
1890             if (serverSockAddr == info->nameserverSockAddrs[ns]) {
1891                 res_cache_add_stats_sample_locked(&info->nsstats[ns], sample, max_samples);
1892                 return;
1893             }
1894         }
1895     }
1896 }
1897 
has_named_cache(unsigned netid)1898 bool has_named_cache(unsigned netid) {
1899     std::lock_guard guard(cache_mutex);
1900     return find_named_cache_locked(netid) != nullptr;
1901 }
1902 
resolv_cache_get_expiration(unsigned netid,span<const uint8_t> query,time_t * expiration)1903 int resolv_cache_get_expiration(unsigned netid, span<const uint8_t> query, time_t* expiration) {
1904     Entry key;
1905     *expiration = -1;
1906 
1907     // A malformed query is not allowed.
1908     if (!entry_init_key(&key, query)) {
1909         LOG(WARNING) << __func__ << ": unsupported query";
1910         return -EINVAL;
1911     }
1912 
1913     // lookup cache.
1914     Cache* cache;
1915     std::lock_guard guard(cache_mutex);
1916     if (cache = find_named_cache_locked(netid); cache == nullptr) {
1917         LOG(WARNING) << __func__ << ": cache not created in the network " << netid;
1918         return -ENONET;
1919     }
1920     Entry** lookup = _cache_lookup_p(cache, &key);
1921     Entry* e = *lookup;
1922     if (e == NULL) {
1923         LOG(WARNING) << __func__ << ": not in cache";
1924         return -ENODATA;
1925     }
1926 
1927     if (_time_now() >= e->expires) {
1928         LOG(WARNING) << __func__ << ": entry expired";
1929         return -ENODATA;
1930     }
1931 
1932     *expiration = e->expires;
1933     return 0;
1934 }
1935 
protocol_to_str(const Protocol proto)1936 static const char* protocol_to_str(const Protocol proto) {
1937     switch (proto) {
1938         case PROTO_UDP:
1939             return "UDP";
1940         case PROTO_TCP:
1941             return "TCP";
1942         case PROTO_DOT:
1943             return "DOT";
1944         case PROTO_DOH:
1945             return "DOH";
1946         case PROTO_MDNS:
1947             return "MDNS";
1948         default:
1949             return "UNKNOWN";
1950     }
1951 }
1952 
resolv_stats_set_addrs(unsigned netid,Protocol proto,const std::vector<std::string> & addrs,int port)1953 int resolv_stats_set_addrs(unsigned netid, Protocol proto, const std::vector<std::string>& addrs,
1954                            int port) {
1955     std::lock_guard guard(cache_mutex);
1956     const auto info = find_netconfig_locked(netid);
1957 
1958     if (info == nullptr) return -ENONET;
1959 
1960     std::vector<IPSockAddr> sockAddrs;
1961     sockAddrs.reserve(addrs.size());
1962     for (const auto& addr : addrs) {
1963         sockAddrs.push_back(IPSockAddr::toIPSockAddr(addr, port));
1964     }
1965 
1966     if (!info->dnsStats.setAddrs(sockAddrs, proto)) {
1967         LOG(WARNING) << __func__ << ": netid = " << netid << ", failed to set "
1968                      << protocol_to_str(proto) << " stats";
1969         return -EINVAL;
1970     }
1971 
1972     return 0;
1973 }
1974 
resolv_stats_add(unsigned netid,const android::netdutils::IPSockAddr & server,const DnsQueryEvent * record)1975 bool resolv_stats_add(unsigned netid, const android::netdutils::IPSockAddr& server,
1976                       const DnsQueryEvent* record) {
1977     if (record == nullptr) return false;
1978 
1979     std::lock_guard guard(cache_mutex);
1980     if (const auto info = find_netconfig_locked(netid); info != nullptr) {
1981         return info->dnsStats.addStats(server, *record);
1982     }
1983     return false;
1984 }
1985 
tc_mode_to_str(const int mode)1986 static const char* tc_mode_to_str(const int mode) {
1987     switch (mode) {
1988         case aidl::android::net::IDnsResolver::TC_MODE_DEFAULT:
1989             return "default";
1990         case aidl::android::net::IDnsResolver::TC_MODE_UDP_TCP:
1991             return "UDP_TCP";
1992         default:
1993             return "unknown";
1994     }
1995 }
1996 
to_stats_network_type(int32_t mainType,bool withVpn)1997 static android::net::NetworkType to_stats_network_type(int32_t mainType, bool withVpn) {
1998     switch (mainType) {
1999         case IDnsResolver::TRANSPORT_CELLULAR:
2000             return withVpn ? android::net::NT_CELLULAR_VPN : android::net::NT_CELLULAR;
2001         case IDnsResolver::TRANSPORT_WIFI:
2002             return withVpn ? android::net::NT_WIFI_VPN : android::net::NT_WIFI;
2003         case IDnsResolver::TRANSPORT_BLUETOOTH:
2004             return withVpn ? android::net::NT_BLUETOOTH_VPN : android::net::NT_BLUETOOTH;
2005         case IDnsResolver::TRANSPORT_ETHERNET:
2006             return withVpn ? android::net::NT_ETHERNET_VPN : android::net::NT_ETHERNET;
2007         case IDnsResolver::TRANSPORT_VPN:
2008             return withVpn ? android::net::NT_UNKNOWN : android::net::NT_VPN;
2009         case IDnsResolver::TRANSPORT_WIFI_AWARE:
2010             return withVpn ? android::net::NT_UNKNOWN : android::net::NT_WIFI_AWARE;
2011         case IDnsResolver::TRANSPORT_LOWPAN:
2012             return withVpn ? android::net::NT_UNKNOWN : android::net::NT_LOWPAN;
2013         default:
2014             return android::net::NT_UNKNOWN;
2015     }
2016 }
2017 
convert_network_type(const std::vector<int32_t> & transportTypes)2018 android::net::NetworkType convert_network_type(const std::vector<int32_t>& transportTypes) {
2019     // The valid transportTypes size is 1 to 3.
2020     if (transportTypes.size() > 3 || transportTypes.size() == 0) return android::net::NT_UNKNOWN;
2021     // TransportTypes size == 1, map the type to stats network type directly.
2022     if (transportTypes.size() == 1) return to_stats_network_type(transportTypes[0], false);
2023     // TransportTypes size == 3, only cellular + wifi + vpn is valid.
2024     if (transportTypes.size() == 3) {
2025         std::vector<int32_t> sortedTransTypes = transportTypes;
2026         std::sort(sortedTransTypes.begin(), sortedTransTypes.end());
2027         if (sortedTransTypes != std::vector<int32_t>{IDnsResolver::TRANSPORT_CELLULAR,
2028                                                      IDnsResolver::TRANSPORT_WIFI,
2029                                                      IDnsResolver::TRANSPORT_VPN}) {
2030             return android::net::NT_UNKNOWN;
2031         }
2032         return android::net::NT_WIFI_CELLULAR_VPN;
2033     }
2034     // TransportTypes size == 2, it shoud be 1 main type + vpn type.
2035     // Otherwise, consider it as UNKNOWN.
2036     bool hasVpn = false;
2037     int32_t mainType = IDnsResolver::TRANSPORT_UNKNOWN;
2038     for (const auto& transportType : transportTypes) {
2039         if (transportType == IDnsResolver::TRANSPORT_VPN) {
2040             hasVpn = true;
2041             continue;
2042         }
2043         mainType = transportType;
2044     }
2045     return hasVpn ? to_stats_network_type(mainType, true) : android::net::NT_UNKNOWN;
2046 }
2047 
transport_type_to_str(const std::vector<int32_t> & transportTypes)2048 static const char* transport_type_to_str(const std::vector<int32_t>& transportTypes) {
2049     switch (convert_network_type(transportTypes)) {
2050         case android::net::NT_CELLULAR:
2051             return "CELLULAR";
2052         case android::net::NT_WIFI:
2053             return "WIFI";
2054         case android::net::NT_BLUETOOTH:
2055             return "BLUETOOTH";
2056         case android::net::NT_ETHERNET:
2057             return "ETHERNET";
2058         case android::net::NT_VPN:
2059             return "VPN";
2060         case android::net::NT_WIFI_AWARE:
2061             return "WIFI_AWARE";
2062         case android::net::NT_LOWPAN:
2063             return "LOWPAN";
2064         case android::net::NT_CELLULAR_VPN:
2065             return "CELLULAR_VPN";
2066         case android::net::NT_WIFI_VPN:
2067             return "WIFI_VPN";
2068         case android::net::NT_BLUETOOTH_VPN:
2069             return "BLUETOOTH_VPN";
2070         case android::net::NT_ETHERNET_VPN:
2071             return "ETHERNET_VPN";
2072         case android::net::NT_WIFI_CELLULAR_VPN:
2073             return "WIFI_CELLULAR_VPN";
2074         default:
2075             return "UNKNOWN";
2076     }
2077 }
2078 
resolv_netconfig_dump(DumpWriter & dw,unsigned netid)2079 void resolv_netconfig_dump(DumpWriter& dw, unsigned netid) {
2080     std::lock_guard guard(cache_mutex);
2081     if (const auto info = find_netconfig_locked(netid); info != nullptr) {
2082         info->dnsStats.dump(dw);
2083         // TODO: dump info->hosts
2084         dw.println("TC mode: %s", tc_mode_to_str(info->tc_mode));
2085         dw.println("TransportType: %s", transport_type_to_str(info->transportTypes));
2086     }
2087 }
2088