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