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