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