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