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