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 aidl::android::net::ResolverParamsParcel;
71 using android::net::DnsQueryEvent;
72 using android::net::DnsStats;
73 using android::net::Experiments;
74 using android::net::PROTO_TCP;
75 using android::net::PROTO_UDP;
76 using android::net::Protocol;
77 using android::netdutils::DumpWriter;
78 using android::netdutils::IPSockAddr;
79 using std::span;
80
81 /* This code implements a small and *simple* DNS resolver cache.
82 *
83 * It is only used to cache DNS answers for a time defined by the smallest TTL
84 * among the answer records in order to reduce DNS traffic. It is not supposed
85 * to be a full DNS cache, since we plan to implement that in the future in a
86 * dedicated process running on the system.
87 *
88 * Note that its design is kept simple very intentionally, i.e.:
89 *
90 * - it takes raw DNS query packet data as input, and returns raw DNS
91 * answer packet data as output
92 *
93 * (this means that two similar queries that encode the DNS name
94 * differently will be treated distinctly).
95 *
96 * the smallest TTL value among the answer records are used as the time
97 * to keep an answer in the cache.
98 *
99 * this is bad, but we absolutely want to avoid parsing the answer packets
100 * (and should be solved by the later full DNS cache process).
101 *
102 * - the implementation is just a (query-data) => (answer-data) hash table
103 * with a trivial least-recently-used expiration policy.
104 *
105 * Doing this keeps the code simple and avoids to deal with a lot of things
106 * that a full DNS cache is expected to do.
107 *
108 * The API is also very simple:
109 *
110 * - the client calls resolv_cache_lookup() before performing a query
111 *
112 * If the function returns RESOLV_CACHE_FOUND, a copy of the answer data
113 * has been copied into the client-provided answer buffer.
114 *
115 * If the function returns RESOLV_CACHE_NOTFOUND, the client should perform
116 * a request normally, *then* call resolv_cache_add() to add the received
117 * answer to the cache.
118 *
119 * If the function returns RESOLV_CACHE_UNSUPPORTED, the client should
120 * perform a request normally, and *not* call resolv_cache_add()
121 *
122 * Note that RESOLV_CACHE_UNSUPPORTED is also returned if the answer buffer
123 * is too short to accomodate the cached result.
124 */
125
126 /* Default number of entries kept in the cache. This value has been
127 * determined by browsing through various sites and counting the number
128 * of corresponding requests. Keep in mind that our framework is currently
129 * performing two requests per name lookup (one for IPv4, the other for IPv6)
130 *
131 * www.google.com 4
132 * www.ysearch.com 6
133 * www.amazon.com 8
134 * www.nytimes.com 22
135 * www.espn.com 28
136 * www.msn.com 28
137 * www.lemonde.fr 35
138 *
139 * (determined in 2009-2-17 from Paris, France, results may vary depending
140 * on location)
141 *
142 * most high-level websites use lots of media/ad servers with different names
143 * but these are generally reused when browsing through the site.
144 *
145 * As such, a value of 64 should be relatively comfortable at the moment.
146 *
147 * ******************************************
148 * * NOTE - this has changed.
149 * * 1) we've added IPv6 support so each dns query results in 2 responses
150 * * 2) we've made this a system-wide cache, so the cost is less (it's not
151 * * duplicated in each process) and the need is greater (more processes
152 * * making different requests).
153 * * Upping by 2x for IPv6
154 * * Upping by another 5x for the centralized nature
155 * *****************************************
156 */
157 const int MAX_ENTRIES_DEFAULT = 64 * 2 * 5;
158 const int MAX_ENTRIES_LOWER_BOUND = 1;
159 const int MAX_ENTRIES_UPPER_BOUND = 100 * 1000;
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(DEBUG) << __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() : max_cache_entries(get_max_cache_entries_from_flag()) {
954 entries.resize(max_cache_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 < max_cache_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
get_max_cache_entriesCache991 int get_max_cache_entries() { return max_cache_entries; }
992
993 int num_entries = 0;
994
995 // TODO: convert to std::list
996 Entry mru_list;
997 int last_id = 0;
998 std::vector<Entry> entries;
999
1000 // TODO: convert to std::vector
1001 struct pending_req_info {
1002 unsigned int hash;
1003 struct pending_req_info* next;
1004 } pending_requests{};
1005
1006 private:
get_max_cache_entries_from_flagCache1007 int get_max_cache_entries_from_flag() {
1008 int entries = android::net::Experiments::getInstance()->getFlag("max_cache_entries",
1009 MAX_ENTRIES_DEFAULT);
1010 // Check both lower and upper bounds to prevent irrational values mistakenly pushed by
1011 // server.
1012 if (entries < MAX_ENTRIES_LOWER_BOUND || entries > MAX_ENTRIES_UPPER_BOUND) {
1013 LOG(ERROR) << "Misconfiguration on max_cache_entries " << entries;
1014 entries = MAX_ENTRIES_DEFAULT;
1015 }
1016 return entries;
1017 }
1018
1019 const int max_cache_entries;
1020 };
1021
1022 struct NetConfig {
NetConfigNetConfig1023 explicit NetConfig(unsigned netId) : netid(netId) {
1024 cache = std::make_unique<Cache>();
1025 dns_event_subsampling_map = resolv_get_dns_event_subsampling_map(false);
1026 mdns_event_subsampling_map = resolv_get_dns_event_subsampling_map(true);
1027 }
nameserverCountNetConfig1028 int nameserverCount() { return nameserverSockAddrs.size(); }
setOptionsNetConfig1029 int setOptions(const ResolverOptionsParcel& resolverOptions) {
1030 customizedTable.clear();
1031 for (const auto& host : resolverOptions.hosts) {
1032 if (!host.hostName.empty() && !host.ipAddr.empty())
1033 customizedTable.emplace(host.hostName, host.ipAddr);
1034 }
1035
1036 if (resolverOptions.tcMode < aidl::android::net::IDnsResolver::TC_MODE_DEFAULT ||
1037 resolverOptions.tcMode > aidl::android::net::IDnsResolver::TC_MODE_UDP_TCP) {
1038 LOG(WARNING) << __func__ << ": netid = " << netid
1039 << ", invalid TC mode: " << resolverOptions.tcMode;
1040 return -EINVAL;
1041 }
1042 tc_mode = resolverOptions.tcMode;
1043 enforceDnsUid = resolverOptions.enforceDnsUid;
1044 return 0;
1045 }
1046 const unsigned netid;
1047 std::unique_ptr<Cache> cache;
1048 std::vector<std::string> nameservers;
1049 std::vector<IPSockAddr> nameserverSockAddrs;
1050 int revision_id = 0; // # times the nameservers have been replaced
1051 res_params params{};
1052 res_stats nsstats[MAXNS]{};
1053 std::vector<std::string> search_domains;
1054 int wait_for_pending_req_timeout_count = 0;
1055 // Map format: ReturnCode:rate_denom
1056 std::unordered_map<int, uint32_t> dns_event_subsampling_map;
1057 std::unordered_map<int, uint32_t> mdns_event_subsampling_map;
1058 DnsStats dnsStats;
1059
1060 // Customized hostname/address table will be stored in customizedTable.
1061 // If resolverParams.hosts is empty, the existing customized table will be erased.
1062 typedef std::multimap<std::string /* hostname */, std::string /* IPv4/IPv6 address */>
1063 HostMapping;
1064 HostMapping customizedTable = {};
1065
1066 int tc_mode = aidl::android::net::IDnsResolver::TC_MODE_DEFAULT;
1067 bool enforceDnsUid = false;
1068 std::vector<int32_t> transportTypes;
1069 bool metered = false;
1070 std::vector<std::string> interfaceNames;
1071 };
1072
1073 /* gets cache associated with a network, or NULL if none exists */
1074 static Cache* find_named_cache_locked(unsigned netid) REQUIRES(cache_mutex);
1075
1076 // Return true - if there is a pending request in |cache| matching |key|.
1077 // Return false - if no pending request is found matching the key. Optionally
1078 // 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)1079 static bool cache_has_pending_request_locked(Cache* cache, const Entry* key,
1080 bool append_if_not_found) {
1081 if (!cache || !key) return false;
1082
1083 Cache::pending_req_info* ri = cache->pending_requests.next;
1084 Cache::pending_req_info* prev = &cache->pending_requests;
1085 while (ri) {
1086 if (ri->hash == key->hash) {
1087 return true;
1088 }
1089 prev = ri;
1090 ri = ri->next;
1091 }
1092
1093 if (append_if_not_found) {
1094 ri = (Cache::pending_req_info*)calloc(1, sizeof(Cache::pending_req_info));
1095 if (ri) {
1096 ri->hash = key->hash;
1097 prev->next = ri;
1098 }
1099 }
1100 return false;
1101 }
1102
1103 // Notify all threads that the cache entry |key| has become available
cache_notify_waiting_tid_locked(struct Cache * cache,const Entry * key)1104 static void cache_notify_waiting_tid_locked(struct Cache* cache, const Entry* key) {
1105 if (!cache || !key) return;
1106
1107 Cache::pending_req_info* ri = cache->pending_requests.next;
1108 Cache::pending_req_info* prev = &cache->pending_requests;
1109 while (ri) {
1110 if (ri->hash == key->hash) {
1111 // remove item from list and destroy
1112 prev->next = ri->next;
1113 free(ri);
1114 cv.notify_all();
1115 return;
1116 }
1117 prev = ri;
1118 ri = ri->next;
1119 }
1120 }
1121
_resolv_cache_query_failed(unsigned netid,span<const uint8_t> query,uint32_t flags)1122 void _resolv_cache_query_failed(unsigned netid, span<const uint8_t> query, uint32_t flags) {
1123 // We should not notify with these flags.
1124 if (flags & (ANDROID_RESOLV_NO_CACHE_STORE | ANDROID_RESOLV_NO_CACHE_LOOKUP)) {
1125 return;
1126 }
1127 Entry key[1];
1128
1129 if (!entry_init_key(key, query)) return;
1130
1131 std::lock_guard guard(cache_mutex);
1132
1133 Cache* cache = find_named_cache_locked(netid);
1134
1135 if (cache) {
1136 cache_notify_waiting_tid_locked(cache, key);
1137 }
1138 }
1139
cache_dump_mru_locked(Cache * cache)1140 static void cache_dump_mru_locked(Cache* cache) {
1141 std::string buf = fmt::format("MRU LIST ({:2d}): ", cache->num_entries);
1142 for (Entry* e = cache->mru_list.mru_next; e != &cache->mru_list; e = e->mru_next) {
1143 fmt::format_to(std::back_inserter(buf), " {}", e->id);
1144 }
1145
1146 LOG(DEBUG) << __func__ << ": " << buf;
1147 }
1148
1149 /* This function tries to find a key within the hash table
1150 * In case of success, it will return a *pointer* to the hashed key.
1151 * In case of failure, it will return a *pointer* to NULL
1152 *
1153 * So, the caller must check '*result' to check for success/failure.
1154 *
1155 * The main idea is that the result can later be used directly in
1156 * calls to resolv_cache_add or _resolv_cache_remove as the 'lookup'
1157 * parameter. This makes the code simpler and avoids re-searching
1158 * for the key position in the htable.
1159 *
1160 * The result of a lookup_p is only valid until you alter the hash
1161 * table.
1162 */
_cache_lookup_p(Cache * cache,Entry * key)1163 static Entry** _cache_lookup_p(Cache* cache, Entry* key) {
1164 int index = key->hash % cache->get_max_cache_entries();
1165 Entry** pnode = (Entry**) &cache->entries[index];
1166
1167 while (*pnode != NULL) {
1168 Entry* node = *pnode;
1169
1170 if (node == NULL) break;
1171
1172 if (node->hash == key->hash && entry_equals(node, key)) break;
1173
1174 pnode = &node->hlink;
1175 }
1176 return pnode;
1177 }
1178
1179 /* Add a new entry to the hash table. 'lookup' must be the
1180 * result of an immediate previous failed _lookup_p() call
1181 * (i.e. with *lookup == NULL), and 'e' is the pointer to the
1182 * newly created entry
1183 */
_cache_add_p(Cache * cache,Entry ** lookup,Entry * e)1184 static void _cache_add_p(Cache* cache, Entry** lookup, Entry* e) {
1185 *lookup = e;
1186 e->id = ++cache->last_id;
1187 entry_mru_add(e, &cache->mru_list);
1188 cache->num_entries += 1;
1189
1190 LOG(DEBUG) << __func__ << ": entry " << e->id << " added (count=" << cache->num_entries << ")";
1191 }
1192
1193 /* Remove an existing entry from the hash table,
1194 * 'lookup' must be the result of an immediate previous
1195 * and succesful _lookup_p() call.
1196 */
_cache_remove_p(Cache * cache,Entry ** lookup)1197 static void _cache_remove_p(Cache* cache, Entry** lookup) {
1198 Entry* e = *lookup;
1199
1200 LOG(DEBUG) << __func__ << ": entry " << e->id << " removed (count=" << cache->num_entries - 1
1201 << ")";
1202
1203 entry_mru_remove(e);
1204 *lookup = e->hlink;
1205 entry_free(e);
1206 cache->num_entries -= 1;
1207 }
1208
1209 /* Remove the oldest entry from the hash table.
1210 */
_cache_remove_oldest(Cache * cache)1211 static void _cache_remove_oldest(Cache* cache) {
1212 Entry* oldest = cache->mru_list.mru_prev;
1213 Entry** lookup = _cache_lookup_p(cache, oldest);
1214
1215 if (*lookup == NULL) { /* should not happen */
1216 LOG(INFO) << __func__ << ": OLDEST NOT IN HTABLE ?";
1217 return;
1218 }
1219 LOG(DEBUG) << __func__ << ": Cache full - removing oldest";
1220 res_pquery(std::span(oldest->query, oldest->querylen));
1221 _cache_remove_p(cache, lookup);
1222 }
1223
1224 /* Remove all expired entries from the hash table.
1225 */
_cache_remove_expired(Cache * cache)1226 static void _cache_remove_expired(Cache* cache) {
1227 Entry* e;
1228 time_t now = _time_now();
1229
1230 for (e = cache->mru_list.mru_next; e != &cache->mru_list;) {
1231 // Entry is old, remove
1232 if (now >= e->expires) {
1233 Entry** lookup = _cache_lookup_p(cache, e);
1234 if (*lookup == NULL) { /* should not happen */
1235 LOG(INFO) << __func__ << ": ENTRY NOT IN HTABLE ?";
1236 return;
1237 }
1238 e = e->mru_next;
1239 _cache_remove_p(cache, lookup);
1240 } else {
1241 e = e->mru_next;
1242 }
1243 }
1244 }
1245
1246 // Get a NetConfig associated with a network, or nullptr if not found.
1247 static NetConfig* find_netconfig_locked(unsigned netid) REQUIRES(cache_mutex);
1248
resolv_cache_lookup(unsigned netid,span<const uint8_t> query,span<uint8_t> answer,int * answerlen,uint32_t flags)1249 ResolvCacheStatus resolv_cache_lookup(unsigned netid, span<const uint8_t> query,
1250 span<uint8_t> answer, int* answerlen, uint32_t flags) {
1251 // Skip cache lookup, return RESOLV_CACHE_NOTFOUND directly so that it is
1252 // possible to cache the answer of this query.
1253 // If ANDROID_RESOLV_NO_CACHE_STORE is set, return RESOLV_CACHE_SKIP to skip possible cache
1254 // storing.
1255 // (b/150371903): ANDROID_RESOLV_NO_CACHE_STORE should imply ANDROID_RESOLV_NO_CACHE_LOOKUP
1256 // to avoid side channel attack.
1257 if (flags & (ANDROID_RESOLV_NO_CACHE_LOOKUP | ANDROID_RESOLV_NO_CACHE_STORE)) {
1258 return flags & ANDROID_RESOLV_NO_CACHE_STORE ? RESOLV_CACHE_SKIP : RESOLV_CACHE_NOTFOUND;
1259 }
1260 Entry key;
1261 Entry** lookup;
1262 Entry* e;
1263 time_t now;
1264
1265 LOG(DEBUG) << __func__ << ": lookup";
1266
1267 /* we don't cache malformed queries */
1268 if (!entry_init_key(&key, query)) {
1269 LOG(INFO) << __func__ << ": unsupported query";
1270 return RESOLV_CACHE_UNSUPPORTED;
1271 }
1272 /* lookup cache */
1273 std::unique_lock lock(cache_mutex);
1274 android::base::ScopedLockAssertion assume_lock(cache_mutex);
1275 Cache* cache = find_named_cache_locked(netid);
1276 if (cache == nullptr) {
1277 return RESOLV_CACHE_UNSUPPORTED;
1278 }
1279
1280 /* see the description of _lookup_p to understand this.
1281 * the function always return a non-NULL pointer.
1282 */
1283 lookup = _cache_lookup_p(cache, &key);
1284 e = *lookup;
1285
1286 if (e == NULL) {
1287 LOG(DEBUG) << __func__ << ": NOT IN CACHE";
1288
1289 if (!cache_has_pending_request_locked(cache, &key, true)) {
1290 return RESOLV_CACHE_NOTFOUND;
1291 }
1292
1293 LOG(INFO) << __func__ << ": Waiting for previous request";
1294 // wait until (1) timeout OR
1295 // (2) cv is notified AND no pending request matching the |key|
1296 // (cv notifier should delete pending request before sending notification.)
1297 const bool ret = cv.wait_for(lock, std::chrono::seconds(PENDING_REQUEST_TIMEOUT),
1298 [netid, &cache, &key]() REQUIRES(cache_mutex) {
1299 // Must update cache as it could have been deleted
1300 cache = find_named_cache_locked(netid);
1301 return !cache_has_pending_request_locked(cache, &key, false);
1302 });
1303 if (!cache) {
1304 return RESOLV_CACHE_NOTFOUND;
1305 }
1306 if (ret == false) {
1307 NetConfig* info = find_netconfig_locked(netid);
1308 if (info != NULL) {
1309 info->wait_for_pending_req_timeout_count++;
1310 }
1311 }
1312 lookup = _cache_lookup_p(cache, &key);
1313 e = *lookup;
1314 if (e == NULL) {
1315 return RESOLV_CACHE_NOTFOUND;
1316 }
1317 }
1318
1319 now = _time_now();
1320
1321 /* remove stale entries here */
1322 if (now >= e->expires) {
1323 LOG(DEBUG) << __func__ << ": NOT IN CACHE (STALE ENTRY " << *lookup << "DISCARDED)";
1324 res_pquery(std::span(e->query, e->querylen));
1325 _cache_remove_p(cache, lookup);
1326 return RESOLV_CACHE_NOTFOUND;
1327 }
1328
1329 *answerlen = e->answerlen;
1330 if (e->answerlen > static_cast<ptrdiff_t>(answer.size())) {
1331 /* NOTE: we return UNSUPPORTED if the answer buffer is too short */
1332 LOG(INFO) << __func__ << ": ANSWER TOO LONG";
1333 return RESOLV_CACHE_UNSUPPORTED;
1334 }
1335
1336 memcpy(answer.data(), e->answer, e->answerlen);
1337
1338 /* bump up this entry to the top of the MRU list */
1339 if (e != cache->mru_list.mru_next) {
1340 entry_mru_remove(e);
1341 entry_mru_add(e, &cache->mru_list);
1342 }
1343
1344 LOG(INFO) << __func__ << ": FOUND IN CACHE entry=" << e;
1345 return RESOLV_CACHE_FOUND;
1346 }
1347
resolv_cache_add(unsigned netid,span<const uint8_t> query,span<const uint8_t> answer)1348 int resolv_cache_add(unsigned netid, span<const uint8_t> query, span<const uint8_t> answer) {
1349 Entry key[1];
1350 Entry* e;
1351 Entry** lookup;
1352 uint32_t ttl;
1353 Cache* cache = NULL;
1354
1355 /* don't assume that the query has already been cached
1356 */
1357 if (!entry_init_key(key, query)) {
1358 LOG(INFO) << __func__ << ": passed invalid query?";
1359 return -EINVAL;
1360 }
1361
1362 std::lock_guard guard(cache_mutex);
1363
1364 cache = find_named_cache_locked(netid);
1365 if (cache == nullptr) {
1366 return -ENONET;
1367 }
1368
1369 lookup = _cache_lookup_p(cache, key);
1370 e = *lookup;
1371
1372 // Should only happen on ANDROID_RESOLV_NO_CACHE_LOOKUP
1373 if (e != NULL) {
1374 LOG(INFO) << __func__ << ": ALREADY IN CACHE (" << e << ") ? IGNORING ADD";
1375 cache_notify_waiting_tid_locked(cache, key);
1376 return -EEXIST;
1377 }
1378
1379 if (cache->num_entries >= cache->get_max_cache_entries()) {
1380 _cache_remove_expired(cache);
1381 if (cache->num_entries >= cache->get_max_cache_entries()) {
1382 _cache_remove_oldest(cache);
1383 }
1384 // TODO: It looks useless, remove below code after having test to prove it.
1385 lookup = _cache_lookup_p(cache, key);
1386 e = *lookup;
1387 if (e != NULL) {
1388 LOG(INFO) << __func__ << ": ALREADY IN CACHE (" << e << ") ? IGNORING ADD";
1389 cache_notify_waiting_tid_locked(cache, key);
1390 return -EEXIST;
1391 }
1392 }
1393
1394 ttl = answer_getTTL(answer);
1395 if (ttl > 0) {
1396 e = entry_alloc(key, answer);
1397 if (e != NULL) {
1398 e->expires = ttl + _time_now();
1399 _cache_add_p(cache, lookup, e);
1400 }
1401 }
1402
1403 cache_dump_mru_locked(cache);
1404 cache_notify_waiting_tid_locked(cache, key);
1405
1406 return 0;
1407 }
1408
resolv_gethostbyaddr_from_cache(unsigned netid,char domain_name[],size_t domain_name_size,const char * ip_address,int af)1409 bool resolv_gethostbyaddr_from_cache(unsigned netid, char domain_name[], size_t domain_name_size,
1410 const char* ip_address, int af) {
1411 if (domain_name_size > NS_MAXDNAME) {
1412 LOG(WARNING) << __func__ << ": invalid domain_name_size " << domain_name_size;
1413 return false;
1414 } else if (ip_address == nullptr || ip_address[0] == '\0') {
1415 LOG(WARNING) << __func__ << ": invalid ip_address";
1416 return false;
1417 } else if (af != AF_INET && af != AF_INET6) {
1418 LOG(WARNING) << __func__ << ": unsupported AF";
1419 return false;
1420 }
1421
1422 Cache* cache = nullptr;
1423 Entry* node = nullptr;
1424
1425 ns_rr rr;
1426 ns_msg handle;
1427 ns_rr rr_query;
1428
1429 struct sockaddr_in sa;
1430 struct sockaddr_in6 sa6;
1431 char* addr_buf = nullptr;
1432
1433 std::lock_guard guard(cache_mutex);
1434
1435 cache = find_named_cache_locked(netid);
1436 if (cache == nullptr) {
1437 return false;
1438 }
1439
1440 for (node = cache->mru_list.mru_next; node != nullptr && node != &cache->mru_list;
1441 node = node->mru_next) {
1442 if (node->answer == nullptr) {
1443 continue;
1444 }
1445
1446 memset(&handle, 0, sizeof(handle));
1447
1448 if (ns_initparse(node->answer, node->answerlen, &handle) < 0) {
1449 continue;
1450 }
1451
1452 for (int n = 0; n < ns_msg_count(handle, ns_s_an); n++) {
1453 memset(&rr, 0, sizeof(rr));
1454
1455 if (ns_parserr(&handle, ns_s_an, n, &rr)) {
1456 continue;
1457 }
1458
1459 if (ns_rr_type(rr) == ns_t_a && af == AF_INET) {
1460 addr_buf = (char*)&(sa.sin_addr);
1461 } else if (ns_rr_type(rr) == ns_t_aaaa && af == AF_INET6) {
1462 addr_buf = (char*)&(sa6.sin6_addr);
1463 } else {
1464 continue;
1465 }
1466
1467 if (inet_pton(af, ip_address, addr_buf) != 1) {
1468 LOG(WARNING) << __func__ << ": inet_pton() fail";
1469 return false;
1470 }
1471
1472 if (memcmp(ns_rr_rdata(rr), addr_buf, ns_rr_rdlen(rr)) == 0) {
1473 int query_count = ns_msg_count(handle, ns_s_qd);
1474 for (int i = 0; i < query_count; i++) {
1475 memset(&rr_query, 0, sizeof(rr_query));
1476 if (ns_parserr(&handle, ns_s_qd, i, &rr_query)) {
1477 continue;
1478 }
1479 strlcpy(domain_name, ns_rr_name(rr_query), domain_name_size);
1480 if (domain_name[0] != '\0') {
1481 return true;
1482 }
1483 }
1484 }
1485 }
1486 }
1487
1488 return false;
1489 }
1490
1491 static std::unordered_map<unsigned, std::unique_ptr<NetConfig>> sNetConfigMap
1492 GUARDED_BY(cache_mutex);
1493
1494 // Clears nameservers set for |netconfig| and clears the stats
1495 static void free_nameservers_locked(NetConfig* netconfig);
1496 // Order-insensitive comparison for the two set of servers.
1497 static bool resolv_is_nameservers_equal(const std::vector<std::string>& oldServers,
1498 const std::vector<std::string>& newServers);
1499 // clears the stats samples contained withing the given netconfig.
1500 static void res_cache_clear_stats_locked(NetConfig* netconfig);
1501
1502 // public API for netd to query if name server is set on specific netid
resolv_has_nameservers(unsigned netid)1503 bool resolv_has_nameservers(unsigned netid) {
1504 std::lock_guard guard(cache_mutex);
1505 NetConfig* info = find_netconfig_locked(netid);
1506 return (info != nullptr) && (info->nameserverCount() > 0);
1507 }
1508
resolv_create_cache_for_net(unsigned netid)1509 int resolv_create_cache_for_net(unsigned netid) {
1510 std::lock_guard guard(cache_mutex);
1511 if (sNetConfigMap.find(netid) != sNetConfigMap.end()) {
1512 LOG(ERROR) << __func__ << ": Cache is already created, netId: " << netid;
1513 return -EEXIST;
1514 }
1515
1516 sNetConfigMap[netid] = std::make_unique<NetConfig>(netid);
1517
1518 return 0;
1519 }
1520
resolv_delete_cache_for_net(unsigned netid)1521 void resolv_delete_cache_for_net(unsigned netid) {
1522 std::lock_guard guard(cache_mutex);
1523 sNetConfigMap.erase(netid);
1524 }
1525
resolv_flush_cache_for_net(unsigned netid)1526 int resolv_flush_cache_for_net(unsigned netid) {
1527 std::lock_guard guard(cache_mutex);
1528
1529 NetConfig* netconfig = find_netconfig_locked(netid);
1530 if (netconfig == nullptr) {
1531 return -ENONET;
1532 }
1533 netconfig->cache->flush();
1534
1535 // Also clear the NS statistics.
1536 res_cache_clear_stats_locked(netconfig);
1537 return 0;
1538 }
1539
resolv_list_caches()1540 std::vector<unsigned> resolv_list_caches() {
1541 std::lock_guard guard(cache_mutex);
1542 std::vector<unsigned> result;
1543 result.reserve(sNetConfigMap.size());
1544 for (const auto& [netId, _] : sNetConfigMap) {
1545 result.push_back(netId);
1546 }
1547 return result;
1548 }
1549
find_named_cache_locked(unsigned netid)1550 static Cache* find_named_cache_locked(unsigned netid) {
1551 NetConfig* info = find_netconfig_locked(netid);
1552 if (info != nullptr) return info->cache.get();
1553 return nullptr;
1554 }
1555
find_netconfig_locked(unsigned netid)1556 static NetConfig* find_netconfig_locked(unsigned netid) {
1557 if (auto it = sNetConfigMap.find(netid); it != sNetConfigMap.end()) {
1558 return it->second.get();
1559 }
1560 return nullptr;
1561 }
1562
resolv_get_network_types_for_net(unsigned netid)1563 android::net::NetworkType resolv_get_network_types_for_net(unsigned netid) {
1564 std::lock_guard guard(cache_mutex);
1565 NetConfig* netconfig = find_netconfig_locked(netid);
1566 if (netconfig == nullptr) return android::net::NT_UNKNOWN;
1567 return convert_network_type(netconfig->transportTypes);
1568 }
1569
is_mdns_supported_transport_types(const std::vector<int32_t> & transportTypes)1570 bool is_mdns_supported_transport_types(const std::vector<int32_t>& transportTypes) {
1571 for (const auto& tp : transportTypes) {
1572 if (tp == IDnsResolver::TRANSPORT_CELLULAR || tp == IDnsResolver::TRANSPORT_VPN) {
1573 return false;
1574 }
1575 }
1576 return true;
1577 }
1578
is_mdns_supported_network(unsigned netid)1579 bool is_mdns_supported_network(unsigned netid) {
1580 std::lock_guard guard(cache_mutex);
1581 NetConfig* netconfig = find_netconfig_locked(netid);
1582 if (netconfig == nullptr) return false;
1583 return is_mdns_supported_transport_types(netconfig->transportTypes);
1584 }
1585
1586 namespace {
1587
1588 // Returns valid domains without duplicates which are limited to max size |MAXDNSRCH|.
filter_domains(const std::vector<std::string> & domains)1589 std::vector<std::string> filter_domains(const std::vector<std::string>& domains) {
1590 std::set<std::string> tmp_set;
1591 std::vector<std::string> res;
1592
1593 std::copy_if(domains.begin(), domains.end(), std::back_inserter(res),
1594 [&tmp_set](const std::string& str) {
1595 return !(str.size() > MAXDNSRCHPATH - 1) && (tmp_set.insert(str).second);
1596 });
1597 if (res.size() > MAXDNSRCH) {
1598 LOG(WARNING) << __func__ << ": valid domains=" << res.size()
1599 << ", but MAXDNSRCH=" << MAXDNSRCH;
1600 res.resize(MAXDNSRCH);
1601 }
1602 return res;
1603 }
1604
filter_nameservers(const std::vector<std::string> & servers)1605 std::vector<std::string> filter_nameservers(const std::vector<std::string>& servers) {
1606 std::vector<std::string> res = servers;
1607 if (res.size() > MAXNS) {
1608 LOG(WARNING) << __func__ << ": too many servers: " << res.size();
1609 res.resize(MAXNS);
1610 }
1611 return res;
1612 }
1613
isValidServer(const std::string & server)1614 bool isValidServer(const std::string& server) {
1615 const addrinfo hints = {
1616 .ai_family = AF_UNSPEC,
1617 .ai_socktype = SOCK_DGRAM,
1618 };
1619 addrinfo* result = nullptr;
1620 if (int err = getaddrinfo_numeric(server.c_str(), "53", hints, &result); err != 0) {
1621 LOG(WARNING) << __func__ << ": getaddrinfo_numeric(" << server
1622 << ") = " << gai_strerror(err);
1623 return false;
1624 }
1625 freeaddrinfo(result);
1626 return true;
1627 }
1628
1629 } // namespace
1630
getCustomizedTableByName(const size_t netid,const char * hostname)1631 std::vector<std::string> getCustomizedTableByName(const size_t netid, const char* hostname) {
1632 std::lock_guard guard(cache_mutex);
1633 NetConfig* netconfig = find_netconfig_locked(netid);
1634
1635 std::vector<std::string> result;
1636 if (netconfig != nullptr) {
1637 const auto& hosts = netconfig->customizedTable.equal_range(hostname);
1638 for (auto i = hosts.first; i != hosts.second; ++i) {
1639 result.push_back(i->second);
1640 }
1641 }
1642 return result;
1643 }
1644
resolv_get_interface_names(int netid)1645 std::vector<std::string> resolv_get_interface_names(int netid) {
1646 std::lock_guard guard(cache_mutex);
1647
1648 NetConfig* netconfig = find_netconfig_locked(netid);
1649 if (netconfig != nullptr) return netconfig->interfaceNames;
1650 return {};
1651 }
1652
resolv_set_nameservers(const ResolverParamsParcel & params)1653 int resolv_set_nameservers(const ResolverParamsParcel& params) {
1654 const unsigned netid = params.netId;
1655 std::vector<std::string> nameservers = filter_nameservers(params.servers);
1656 const int numservers = static_cast<int>(nameservers.size());
1657
1658 LOG(DEBUG) << __func__ << ": netId = " << netid << ", numservers = " << numservers;
1659
1660 // Parse the addresses before actually locking or changing any state, in case there is an error.
1661 // As a side effect this also reduces the time the lock is kept.
1662 std::vector<IPSockAddr> ipSockAddrs;
1663 ipSockAddrs.reserve(nameservers.size());
1664 for (const auto& server : nameservers) {
1665 if (!isValidServer(server)) return -EINVAL;
1666 ipSockAddrs.push_back(IPSockAddr::toIPSockAddr(server, 53));
1667 }
1668
1669 std::lock_guard guard(cache_mutex);
1670 NetConfig* netconfig = find_netconfig_locked(netid);
1671
1672 if (netconfig == nullptr) return -ENONET;
1673
1674 uint8_t old_max_samples = netconfig->params.max_samples;
1675
1676 memset(&netconfig->params, 0, sizeof(netconfig->params));
1677 netconfig->params.sample_validity = params.sampleValiditySeconds;
1678 netconfig->params.success_threshold = params.successThreshold;
1679 netconfig->params.min_samples = params.minSamples;
1680 netconfig->params.max_samples = params.maxSamples;
1681 netconfig->params.base_timeout_msec = params.baseTimeoutMsec;
1682 netconfig->params.retry_count = params.retryCount;
1683
1684 // This check must always be true, but add a protection against OEMs configure negative values
1685 // for retry_count and base_timeout_msec.
1686 if (netconfig->params.retry_count == 0) {
1687 const int retryCount = Experiments::getInstance()->getFlag("retry_count", RES_DFLRETRY);
1688 netconfig->params.retry_count = (retryCount <= 0) ? RES_DFLRETRY : retryCount;
1689 }
1690 if (netconfig->params.base_timeout_msec == 0) {
1691 const int retransmissionInterval =
1692 Experiments::getInstance()->getFlag("retransmission_time_interval", RES_TIMEOUT);
1693 netconfig->params.base_timeout_msec =
1694 (retransmissionInterval <= 0) ? RES_TIMEOUT : retransmissionInterval;
1695 }
1696
1697 if (!resolv_is_nameservers_equal(netconfig->nameservers, params.servers)) {
1698 // free current before adding new
1699 free_nameservers_locked(netconfig);
1700 netconfig->nameservers = std::move(nameservers);
1701 for (int i = 0; i < numservers; i++) {
1702 LOG(INFO) << __func__ << ": netid = " << netid
1703 << ", addr = " << netconfig->nameservers[i];
1704 }
1705 netconfig->nameserverSockAddrs = std::move(ipSockAddrs);
1706 } else {
1707 if (netconfig->params.max_samples != old_max_samples) {
1708 // If the maximum number of samples changes, the overhead of keeping the most recent
1709 // samples around is not considered worth the effort, so they are cleared instead.
1710 // All other parameters do not affect shared state: Changing these parameters does
1711 // not invalidate the samples, as they only affect aggregation and the conditions
1712 // under which servers are considered usable.
1713 res_cache_clear_stats_locked(netconfig);
1714 }
1715 }
1716
1717 // Always update the search paths. Cache-flushing however is not necessary,
1718 // since the stored cache entries do contain the domain, not just the host name.
1719 netconfig->search_domains = filter_domains(params.domains);
1720
1721 // Setup stats for cleartext dns servers.
1722 if (!netconfig->dnsStats.setAddrs(netconfig->nameserverSockAddrs, PROTO_TCP) ||
1723 !netconfig->dnsStats.setAddrs(netconfig->nameserverSockAddrs, PROTO_UDP)) {
1724 LOG(WARNING) << __func__ << ": netid = " << netid << ", failed to set dns stats";
1725 return -EINVAL;
1726 }
1727 netconfig->transportTypes = std::move(params.transportTypes);
1728 netconfig->metered = params.meteredNetwork;
1729 netconfig->interfaceNames = std::move(params.interfaceNames);
1730
1731 if (params.resolverOptions.has_value()) {
1732 return netconfig->setOptions(params.resolverOptions.value());
1733 }
1734
1735 return 0;
1736 }
1737
resolv_set_options(unsigned netid,const ResolverOptionsParcel & options)1738 int resolv_set_options(unsigned netid, const ResolverOptionsParcel& options) {
1739 std::lock_guard guard(cache_mutex);
1740 NetConfig* netconfig = find_netconfig_locked(netid);
1741
1742 if (netconfig == nullptr) return -ENONET;
1743 return netconfig->setOptions(options);
1744 }
1745
resolv_is_nameservers_equal(const std::vector<std::string> & oldServers,const std::vector<std::string> & newServers)1746 static bool resolv_is_nameservers_equal(const std::vector<std::string>& oldServers,
1747 const std::vector<std::string>& newServers) {
1748 const std::set<std::string> olds(oldServers.begin(), oldServers.end());
1749 const std::set<std::string> news(newServers.begin(), newServers.end());
1750
1751 // TODO: this is incorrect if the list of current or previous nameservers
1752 // contains duplicates. This does not really matter because the framework
1753 // filters out duplicates, but we should probably fix it. It's also
1754 // insensitive to the order of the nameservers; we should probably fix that
1755 // too.
1756 return olds == news;
1757 }
1758
free_nameservers_locked(NetConfig * netconfig)1759 static void free_nameservers_locked(NetConfig* netconfig) {
1760 netconfig->nameservers.clear();
1761 netconfig->nameserverSockAddrs.clear();
1762 res_cache_clear_stats_locked(netconfig);
1763 }
1764
resolv_populate_res_for_net(ResState * statp)1765 void resolv_populate_res_for_net(ResState* statp) {
1766 if (statp == nullptr) {
1767 return;
1768 }
1769 LOG(DEBUG) << __func__ << ": netid=" << statp->netid;
1770
1771 std::lock_guard guard(cache_mutex);
1772 NetConfig* info = find_netconfig_locked(statp->netid);
1773 if (info == nullptr) return;
1774
1775 const bool sortNameservers = Experiments::getInstance()->getFlag("sort_nameservers", 0);
1776 statp->sort_nameservers = sortNameservers;
1777 statp->nsaddrs = sortNameservers ? info->dnsStats.getSortedServers(PROTO_UDP)
1778 : info->nameserverSockAddrs;
1779 statp->search_domains = info->search_domains;
1780 statp->tc_mode = info->tc_mode;
1781 statp->enforce_dns_uid = info->enforceDnsUid;
1782 }
1783
1784 /* Resolver reachability statistics. */
1785
res_cache_add_stats_sample_locked(res_stats * stats,const res_sample & sample,int max_samples)1786 static void res_cache_add_stats_sample_locked(res_stats* stats, const res_sample& sample,
1787 int max_samples) {
1788 // Note: This function expects max_samples > 0, otherwise a (harmless) modification of the
1789 // allocated but supposedly unused memory for samples[0] will happen
1790 LOG(DEBUG) << __func__ << ": adding sample to stats, next = " << unsigned(stats->sample_next)
1791 << ", count = " << unsigned(stats->sample_count);
1792 stats->samples[stats->sample_next] = sample;
1793 if (stats->sample_count < max_samples) {
1794 ++stats->sample_count;
1795 }
1796 if (++stats->sample_next >= max_samples) {
1797 stats->sample_next = 0;
1798 }
1799 }
1800
res_cache_clear_stats_locked(NetConfig * netconfig)1801 static void res_cache_clear_stats_locked(NetConfig* netconfig) {
1802 for (int i = 0; i < MAXNS; ++i) {
1803 netconfig->nsstats[i].sample_count = 0;
1804 netconfig->nsstats[i].sample_next = 0;
1805 }
1806
1807 // Increment the revision id to ensure that sample state is not written back if the
1808 // servers change; in theory it would suffice to do so only if the servers or
1809 // max_samples actually change, in practice the overhead of checking is higher than the
1810 // cost, and overflows are unlikely.
1811 ++netconfig->revision_id;
1812 }
1813
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)1814 int android_net_res_stats_get_info_for_net(unsigned netid, int* nscount,
1815 struct sockaddr_storage servers[MAXNS], int* dcount,
1816 char domains[MAXDNSRCH][MAXDNSRCHPATH],
1817 res_params* params, struct res_stats stats[MAXNS],
1818 int* wait_for_pending_req_timeout_count) {
1819 std::lock_guard guard(cache_mutex);
1820 NetConfig* info = find_netconfig_locked(netid);
1821 if (!info) return -1;
1822
1823 const int num = info->nameserverCount();
1824 if (num > MAXNS) {
1825 LOG(INFO) << __func__ << ": nscount " << num << " > MAXNS " << MAXNS;
1826 errno = EFAULT;
1827 return -1;
1828 }
1829
1830 for (int i = 0; i < num; i++) {
1831 servers[i] = info->nameserverSockAddrs[i];
1832 stats[i] = info->nsstats[i];
1833 }
1834
1835 for (size_t i = 0; i < info->search_domains.size(); i++) {
1836 strlcpy(domains[i], info->search_domains[i].c_str(), MAXDNSRCHPATH);
1837 }
1838
1839 *nscount = num;
1840 *dcount = static_cast<int>(info->search_domains.size());
1841 *params = info->params;
1842 *wait_for_pending_req_timeout_count = info->wait_for_pending_req_timeout_count;
1843
1844 return info->revision_id;
1845 }
1846
resolv_cache_dump_subsampling_map(unsigned netid,bool is_mdns)1847 std::vector<std::string> resolv_cache_dump_subsampling_map(unsigned netid, bool is_mdns) {
1848 std::lock_guard guard(cache_mutex);
1849 NetConfig* netconfig = find_netconfig_locked(netid);
1850 if (netconfig == nullptr) return {};
1851 std::vector<std::string> result;
1852 const auto& subsampling_map = (!is_mdns) ? netconfig->dns_event_subsampling_map
1853 : netconfig->mdns_event_subsampling_map;
1854 result.reserve(subsampling_map.size());
1855 for (const auto& [return_code, rate_denom] : subsampling_map) {
1856 result.push_back(fmt::format("{}:{}",
1857 (return_code == DNSEVENT_SUBSAMPLING_MAP_DEFAULT_KEY)
1858 ? "default"
1859 : std::to_string(return_code),
1860 rate_denom));
1861 }
1862 return result;
1863 }
1864
1865 // Decides whether an event should be sampled using a random number generator and
1866 // a sampling factor derived from the netid and the return code.
1867 //
1868 // 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)1869 uint32_t resolv_cache_get_subsampling_denom(unsigned netid, int return_code, bool is_mdns) {
1870 std::lock_guard guard(cache_mutex);
1871 NetConfig* netconfig = find_netconfig_locked(netid);
1872 if (netconfig == nullptr) return 0; // Don't log anything at all.
1873 const auto& subsampling_map = (!is_mdns) ? netconfig->dns_event_subsampling_map
1874 : netconfig->mdns_event_subsampling_map;
1875 auto search_returnCode = subsampling_map.find(return_code);
1876 uint32_t denom;
1877 if (search_returnCode != subsampling_map.end()) {
1878 denom = search_returnCode->second;
1879 } else {
1880 auto search_default = subsampling_map.find(DNSEVENT_SUBSAMPLING_MAP_DEFAULT_KEY);
1881 denom = (search_default == subsampling_map.end()) ? 0 : search_default->second;
1882 }
1883 return denom;
1884 }
1885
resolv_cache_get_resolver_stats(unsigned netid,res_params * params,res_stats stats[MAXNS],const std::vector<IPSockAddr> & serverSockAddrs)1886 int resolv_cache_get_resolver_stats(unsigned netid, res_params* params, res_stats stats[MAXNS],
1887 const std::vector<IPSockAddr>& serverSockAddrs) {
1888 std::lock_guard guard(cache_mutex);
1889 NetConfig* info = find_netconfig_locked(netid);
1890 if (!info) {
1891 LOG(WARNING) << __func__ << ": NetConfig for netid " << netid << " not found";
1892 return -1;
1893 }
1894
1895 for (size_t i = 0; i < serverSockAddrs.size(); i++) {
1896 for (size_t j = 0; j < info->nameserverSockAddrs.size(); j++) {
1897 // Should never happen. Just in case because of the fix-sized array |stats|.
1898 if (j >= MAXNS) {
1899 LOG(WARNING) << __func__ << ": unexpected size " << j;
1900 return -1;
1901 }
1902
1903 // It's possible that the server is not found, e.g. when a new list of nameservers
1904 // is updated to the NetConfig just after this look up thread being populated.
1905 // Keep the server valid as-is (by means of keeping stats[i] unset), but we should
1906 // think about if there's a better way.
1907 if (info->nameserverSockAddrs[j] == serverSockAddrs[i]) {
1908 stats[i] = info->nsstats[j];
1909 break;
1910 }
1911 }
1912 }
1913
1914 *params = info->params;
1915 return info->revision_id;
1916 }
1917
resolv_cache_add_resolver_stats_sample(unsigned netid,int revision_id,const IPSockAddr & serverSockAddr,const res_sample & sample,int max_samples)1918 void resolv_cache_add_resolver_stats_sample(unsigned netid, int revision_id,
1919 const IPSockAddr& serverSockAddr,
1920 const res_sample& sample, int max_samples) {
1921 if (max_samples <= 0) return;
1922
1923 std::lock_guard guard(cache_mutex);
1924 NetConfig* info = find_netconfig_locked(netid);
1925
1926 if (info && info->revision_id == revision_id) {
1927 const int serverNum = std::min(MAXNS, static_cast<int>(info->nameserverSockAddrs.size()));
1928 for (int ns = 0; ns < serverNum; ns++) {
1929 if (serverSockAddr == info->nameserverSockAddrs[ns]) {
1930 res_cache_add_stats_sample_locked(&info->nsstats[ns], sample, max_samples);
1931 return;
1932 }
1933 }
1934 }
1935 }
1936
has_named_cache(unsigned netid)1937 bool has_named_cache(unsigned netid) {
1938 std::lock_guard guard(cache_mutex);
1939 return find_named_cache_locked(netid) != nullptr;
1940 }
1941
resolv_cache_get_expiration(unsigned netid,span<const uint8_t> query,time_t * expiration)1942 int resolv_cache_get_expiration(unsigned netid, span<const uint8_t> query, time_t* expiration) {
1943 Entry key;
1944 *expiration = -1;
1945
1946 // A malformed query is not allowed.
1947 if (!entry_init_key(&key, query)) {
1948 LOG(WARNING) << __func__ << ": unsupported query";
1949 return -EINVAL;
1950 }
1951
1952 // lookup cache.
1953 Cache* cache;
1954 std::lock_guard guard(cache_mutex);
1955 if (cache = find_named_cache_locked(netid); cache == nullptr) {
1956 LOG(WARNING) << __func__ << ": cache not created in the network " << netid;
1957 return -ENONET;
1958 }
1959 Entry** lookup = _cache_lookup_p(cache, &key);
1960 Entry* e = *lookup;
1961 if (e == NULL) {
1962 LOG(WARNING) << __func__ << ": not in cache";
1963 return -ENODATA;
1964 }
1965
1966 if (_time_now() >= e->expires) {
1967 LOG(WARNING) << __func__ << ": entry expired";
1968 return -ENODATA;
1969 }
1970
1971 *expiration = e->expires;
1972 return 0;
1973 }
1974
resolv_stats_set_addrs(unsigned netid,Protocol proto,const std::vector<std::string> & addrs,int port)1975 int resolv_stats_set_addrs(unsigned netid, Protocol proto, const std::vector<std::string>& addrs,
1976 int port) {
1977 std::lock_guard guard(cache_mutex);
1978 const auto info = find_netconfig_locked(netid);
1979
1980 if (info == nullptr) {
1981 LOG(WARNING) << __func__ << ": Network " << netid << " not found for "
1982 << Protocol_Name(proto);
1983 return -ENONET;
1984 }
1985
1986 std::vector<IPSockAddr> sockAddrs;
1987 sockAddrs.reserve(addrs.size());
1988 for (const auto& addr : addrs) {
1989 sockAddrs.push_back(IPSockAddr::toIPSockAddr(addr, port));
1990 }
1991
1992 if (!info->dnsStats.setAddrs(sockAddrs, proto)) {
1993 LOG(WARNING) << __func__ << ": Failed to set " << Protocol_Name(proto) << " on network "
1994 << netid;
1995 return -EINVAL;
1996 }
1997
1998 return 0;
1999 }
2000
resolv_stats_add(unsigned netid,const android::netdutils::IPSockAddr & server,const DnsQueryEvent * record)2001 bool resolv_stats_add(unsigned netid, const android::netdutils::IPSockAddr& server,
2002 const DnsQueryEvent* record) {
2003 if (record == nullptr) return false;
2004
2005 std::lock_guard guard(cache_mutex);
2006 if (const auto info = find_netconfig_locked(netid); info != nullptr) {
2007 return info->dnsStats.addStats(server, *record);
2008 }
2009 return false;
2010 }
2011
tc_mode_to_str(const int mode)2012 static const char* tc_mode_to_str(const int mode) {
2013 switch (mode) {
2014 case aidl::android::net::IDnsResolver::TC_MODE_DEFAULT:
2015 return "default";
2016 case aidl::android::net::IDnsResolver::TC_MODE_UDP_TCP:
2017 return "UDP_TCP";
2018 default:
2019 return "unknown";
2020 }
2021 }
2022
to_stats_network_type(int32_t mainType,bool withVpn)2023 static android::net::NetworkType to_stats_network_type(int32_t mainType, bool withVpn) {
2024 switch (mainType) {
2025 case IDnsResolver::TRANSPORT_CELLULAR:
2026 return withVpn ? android::net::NT_CELLULAR_VPN : android::net::NT_CELLULAR;
2027 case IDnsResolver::TRANSPORT_WIFI:
2028 return withVpn ? android::net::NT_WIFI_VPN : android::net::NT_WIFI;
2029 case IDnsResolver::TRANSPORT_BLUETOOTH:
2030 return withVpn ? android::net::NT_BLUETOOTH_VPN : android::net::NT_BLUETOOTH;
2031 case IDnsResolver::TRANSPORT_ETHERNET:
2032 return withVpn ? android::net::NT_ETHERNET_VPN : android::net::NT_ETHERNET;
2033 case IDnsResolver::TRANSPORT_SATELLITE:
2034 return withVpn ? android::net::NT_UNKNOWN : android::net::NT_SATELLITE;
2035 case IDnsResolver::TRANSPORT_VPN:
2036 return withVpn ? android::net::NT_UNKNOWN : android::net::NT_VPN;
2037 case IDnsResolver::TRANSPORT_WIFI_AWARE:
2038 return withVpn ? android::net::NT_UNKNOWN : android::net::NT_WIFI_AWARE;
2039 case IDnsResolver::TRANSPORT_LOWPAN:
2040 return withVpn ? android::net::NT_UNKNOWN : android::net::NT_LOWPAN;
2041 default:
2042 return android::net::NT_UNKNOWN;
2043 }
2044 }
2045
convert_network_type(const std::vector<int32_t> & transportTypes)2046 android::net::NetworkType convert_network_type(const std::vector<int32_t>& transportTypes) {
2047 // The valid transportTypes size is 1 to 3.
2048 if (transportTypes.size() > 3 || transportTypes.size() == 0) return android::net::NT_UNKNOWN;
2049 // TransportTypes size == 1, map the type to stats network type directly.
2050 if (transportTypes.size() == 1) return to_stats_network_type(transportTypes[0], false);
2051 // TransportTypes size == 3, only cellular + wifi + vpn is valid.
2052 if (transportTypes.size() == 3) {
2053 std::vector<int32_t> sortedTransTypes = transportTypes;
2054 std::sort(sortedTransTypes.begin(), sortedTransTypes.end());
2055 if (sortedTransTypes != std::vector<int32_t>{IDnsResolver::TRANSPORT_CELLULAR,
2056 IDnsResolver::TRANSPORT_WIFI,
2057 IDnsResolver::TRANSPORT_VPN}) {
2058 return android::net::NT_UNKNOWN;
2059 }
2060 return android::net::NT_WIFI_CELLULAR_VPN;
2061 }
2062 // TransportTypes size == 2, it shoud be 1 main type + vpn type.
2063 // Otherwise, consider it as UNKNOWN.
2064 bool hasVpn = false;
2065 int32_t mainType = IDnsResolver::TRANSPORT_UNKNOWN;
2066 for (const auto& transportType : transportTypes) {
2067 if (transportType == IDnsResolver::TRANSPORT_VPN) {
2068 hasVpn = true;
2069 continue;
2070 }
2071 mainType = transportType;
2072 }
2073 return hasVpn ? to_stats_network_type(mainType, true) : android::net::NT_UNKNOWN;
2074 }
2075
transport_type_to_str(const std::vector<int32_t> & transportTypes)2076 static const char* transport_type_to_str(const std::vector<int32_t>& transportTypes) {
2077 switch (convert_network_type(transportTypes)) {
2078 case android::net::NT_CELLULAR:
2079 return "CELLULAR";
2080 case android::net::NT_WIFI:
2081 return "WIFI";
2082 case android::net::NT_BLUETOOTH:
2083 return "BLUETOOTH";
2084 case android::net::NT_ETHERNET:
2085 return "ETHERNET";
2086 case android::net::NT_VPN:
2087 return "VPN";
2088 case android::net::NT_WIFI_AWARE:
2089 return "WIFI_AWARE";
2090 case android::net::NT_LOWPAN:
2091 return "LOWPAN";
2092 case android::net::NT_CELLULAR_VPN:
2093 return "CELLULAR_VPN";
2094 case android::net::NT_WIFI_VPN:
2095 return "WIFI_VPN";
2096 case android::net::NT_BLUETOOTH_VPN:
2097 return "BLUETOOTH_VPN";
2098 case android::net::NT_ETHERNET_VPN:
2099 return "ETHERNET_VPN";
2100 case android::net::NT_WIFI_CELLULAR_VPN:
2101 return "WIFI_CELLULAR_VPN";
2102 case android::net::NT_SATELLITE:
2103 return "SATELLITE";
2104 default:
2105 return "UNKNOWN";
2106 }
2107 }
2108
resolv_netconfig_dump(DumpWriter & dw,unsigned netid)2109 void resolv_netconfig_dump(DumpWriter& dw, unsigned netid) {
2110 std::lock_guard guard(cache_mutex);
2111 if (const auto info = find_netconfig_locked(netid); info != nullptr) {
2112 info->dnsStats.dump(dw);
2113 // TODO: dump info->hosts
2114 dw.println("TC mode: %s", tc_mode_to_str(info->tc_mode));
2115 dw.println("TransportType: %s", transport_type_to_str(info->transportTypes));
2116 dw.println("Metered: %s", info->metered ? "true" : "false");
2117 }
2118 }
2119
resolv_get_max_cache_entries(unsigned netid)2120 int resolv_get_max_cache_entries(unsigned netid) {
2121 std::lock_guard guard(cache_mutex);
2122 NetConfig* info = find_netconfig_locked(netid);
2123 if (!info) {
2124 LOG(WARNING) << __func__ << ": NetConfig for netid " << netid << " not found";
2125 return -1;
2126 }
2127 return info->cache->get_max_cache_entries();
2128 }
2129
resolv_is_enforceDnsUid_enabled_network(unsigned netid)2130 bool resolv_is_enforceDnsUid_enabled_network(unsigned netid) {
2131 std::lock_guard guard(cache_mutex);
2132 if (const auto info = find_netconfig_locked(netid); info != nullptr) {
2133 return info->enforceDnsUid;
2134 }
2135 return false;
2136 }
2137
resolv_is_metered_network(unsigned netid)2138 bool resolv_is_metered_network(unsigned netid) {
2139 std::lock_guard guard(cache_mutex);
2140 if (const auto info = find_netconfig_locked(netid); info != nullptr) {
2141 return info->metered;
2142 }
2143 return false;
2144 }
2145