Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 The Android Open Source Project |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions |
| 7 | * are met: |
| 8 | * * Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * * Redistributions in binary form must reproduce the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer in |
| 12 | * the documentation and/or other materials provided with the |
| 13 | * distribution. |
| 14 | * |
| 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 16 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 17 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| 18 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 19 | * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 20 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
| 21 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS |
| 22 | * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
| 23 | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 24 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT |
| 25 | * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 26 | * SUCH DAMAGE. |
| 27 | */ |
| 28 | |
Bernie Innocenti | e9ba09c | 2018-09-12 23:20:10 +0900 | [diff] [blame] | 29 | // NOTE: verbose logging MUST NOT be left enabled in production binaries. |
| 30 | // It floods logs at high rate, and can leak privacy-sensitive information. |
| 31 | constexpr bool kVerboseLogging = false; |
| 32 | constexpr bool kDumpData = false; |
| 33 | #define LOG_TAG "res_cache" |
| 34 | |
Bernie Innocenti | f89b351 | 2018-08-30 07:34:37 +0900 | [diff] [blame] | 35 | #include <pthread.h> |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 36 | #include <resolv.h> |
| 37 | #include <stdarg.h> |
| 38 | #include <stdio.h> |
| 39 | #include <stdlib.h> |
| 40 | #include <string.h> |
| 41 | #include <time.h> |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 42 | |
Bernie Innocenti | e9ba09c | 2018-09-12 23:20:10 +0900 | [diff] [blame] | 43 | #include <arpa/inet.h> |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 44 | #include <arpa/nameser.h> |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 45 | #include <errno.h> |
| 46 | #include <linux/if.h> |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 47 | #include <net/if.h> |
| 48 | #include <netdb.h> |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 49 | |
Bernie Innocenti | e9ba09c | 2018-09-12 23:20:10 +0900 | [diff] [blame] | 50 | #include <android-base/logging.h> |
Bernie Innocenti | f89b351 | 2018-08-30 07:34:37 +0900 | [diff] [blame] | 51 | |
Bernie Innocenti | 189eb50 | 2018-10-01 23:10:18 +0900 | [diff] [blame] | 52 | #include "netd_resolv/resolv.h" |
| 53 | #include "res_state_ext.h" |
Bernie Innocenti | f89b351 | 2018-08-30 07:34:37 +0900 | [diff] [blame] | 54 | #include "resolv_cache.h" |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 55 | #include "resolv_private.h" |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 56 | |
Bernie Innocenti | e9ba09c | 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 | 5586419 | 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 | e9ba09c | 2018-09-12 23:20:10 +0900 | [diff] [blame] | 145 | #define CONFIG_MAX_ENTRIES (64 * 2 * 5) |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 146 | |
Bernie Innocenti | 1f4a9fd | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 147 | /** BOUNDED BUFFER FORMATTING **/ |
Bernie Innocenti | 5586419 | 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 | e9ba09c | 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 | 5586419 | 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 | e9ba09c | 2018-09-12 23:20:10 +0900 | [diff] [blame] | 168 | * - a return value equal to 'end' means that truncation occurred |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 169 | * (in which case, end[-1] will be set to 0) |
| 170 | * |
Bernie Innocenti | e9ba09c | 2018-09-12 23:20:10 +0900 | [diff] [blame] | 171 | * - after returning from a bprint_x() function, the content of the buffer |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 172 | * is always 0-terminated, even in the event of truncation. |
| 173 | * |
Bernie Innocenti | e9ba09c | 2018-09-12 23:20:10 +0900 | [diff] [blame] | 174 | * these conventions allow you to call bprint_x() functions multiple times and |
Bernie Innocenti | 5586419 | 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 | e9ba09c | 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 | 5586419 | 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 | 1fbca5c | 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 | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 197 | /* add a char to a bounded buffer */ |
Bernie Innocenti | e9ba09c | 2018-09-12 23:20:10 +0900 | [diff] [blame] | 198 | static char* bprint_c(char* p, char* end, int c) { |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 199 | if (p < end) { |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 200 | if (p + 1 == end) |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 201 | *p++ = 0; |
| 202 | else { |
| 203 | *p++ = (char) c; |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 204 | *p = 0; |
Bernie Innocenti | 5586419 | 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 | e9ba09c | 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 | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 212 | int avail = end - p; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 213 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 214 | if (avail <= 0 || len <= 0) return p; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 215 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 216 | if (avail > len) avail = len; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 217 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 218 | memcpy(p, buf, avail); |
Bernie Innocenti | 5586419 | 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 | e9ba09c | 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 | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | /* add a formatted string to a bounded buffer */ |
Bernie Innocenti | e9ba09c | 2018-09-12 23:20:10 +0900 | [diff] [blame] | 235 | static char* bprint(char* p, char* end, const char* format, ...) { |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 236 | int avail, n; |
| 237 | va_list args; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 238 | |
| 239 | avail = end - p; |
| 240 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 241 | if (avail <= 0) return p; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 242 | |
| 243 | va_start(args, format); |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 244 | n = vsnprintf(p, avail, format, args); |
Bernie Innocenti | 5586419 | 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 | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 248 | if (n < 0 || n > avail) n = avail; |
Bernie Innocenti | 5586419 | 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 | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 252 | if (p == end) p[-1] = 0; |
Bernie Innocenti | 5586419 | 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 | e9ba09c | 2018-09-12 23:20:10 +0900 | [diff] [blame] | 258 | static char* bprint_hex(char* p, char* end, unsigned value, int numDigits) { |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 259 | char text[sizeof(unsigned) * 2]; |
| 260 | int nn = 0; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 261 | |
| 262 | while (numDigits-- > 0) { |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 263 | text[nn++] = "0123456789abcdef"[(value >> (numDigits * 4)) & 15]; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 264 | } |
Bernie Innocenti | e9ba09c | 2018-09-12 23:20:10 +0900 | [diff] [blame] | 265 | return bprint_b(p, end, text, nn); |
Bernie Innocenti | 5586419 | 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 | e9ba09c | 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 | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 270 | int lineSize = 16; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 271 | |
| 272 | while (datalen > 0) { |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 273 | int avail = datalen; |
| 274 | int nn; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 275 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 276 | if (avail > lineSize) avail = lineSize; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 277 | |
| 278 | for (nn = 0; nn < avail; nn++) { |
Bernie Innocenti | e9ba09c | 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 | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 281 | } |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 282 | for (; nn < lineSize; nn++) { |
Bernie Innocenti | e9ba09c | 2018-09-12 23:20:10 +0900 | [diff] [blame] | 283 | p = bprint_s(p, end, " "); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 284 | } |
Bernie Innocenti | e9ba09c | 2018-09-12 23:20:10 +0900 | [diff] [blame] | 285 | p = bprint_s(p, end, " "); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 286 | |
| 287 | for (nn = 0; nn < avail; nn++) { |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 288 | int c = data[nn]; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 289 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 290 | if (c < 32 || c > 127) c = '.'; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 291 | |
Bernie Innocenti | e9ba09c | 2018-09-12 23:20:10 +0900 | [diff] [blame] | 292 | p = bprint_c(p, end, c); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 293 | } |
Bernie Innocenti | e9ba09c | 2018-09-12 23:20:10 +0900 | [diff] [blame] | 294 | p = bprint_c(p, end, '\n'); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 295 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 296 | data += avail; |
Bernie Innocenti | 5586419 | 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 | e9ba09c | 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 | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 305 | |
Bernie Innocenti | e9ba09c | 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 | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 311 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 312 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 313 | static time_t _time_now(void) { |
| 314 | struct timeval tv; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 315 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 316 | gettimeofday(&tv, NULL); |
Bernie Innocenti | 5586419 | 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 | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 405 | #define DNS_HEADER_SIZE 12 |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 406 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 407 | #define DNS_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 | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 412 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 413 | #define DNS_CLASS_IN "\00\01" /* big-endian decimal 1 */ |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 414 | |
| 415 | typedef struct { |
Bernie Innocenti | f12d5bb | 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 | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 419 | } DnsPacket; |
| 420 | |
Bernie Innocenti | f12d5bb | 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 | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 424 | packet->cursor = buff; |
| 425 | } |
| 426 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 427 | static void _dnsPacket_rewind(DnsPacket* packet) { |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 428 | packet->cursor = packet->base; |
| 429 | } |
| 430 | |
Bernie Innocenti | f12d5bb | 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 | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 433 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 434 | if (p > packet->end) p = packet->end; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 435 | |
| 436 | packet->cursor = p; |
| 437 | } |
| 438 | |
Bernie Innocenti | f12d5bb | 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 | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 441 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 442 | if (p + 2 > packet->end) return -1; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 443 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 444 | packet->cursor = p + 2; |
| 445 | return (p[0] << 8) | p[1]; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 446 | } |
| 447 | |
Bernie Innocenti | 1f4a9fd | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 448 | /** QUERY CHECKING **/ |
Bernie Innocenti | 5586419 | 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 | f12d5bb | 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 | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 455 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 456 | if (p + numBytes > packet->end) return 0; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 457 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 458 | if (memcmp(p, bytes, numBytes) != 0) return 0; |
Bernie Innocenti | 5586419 | 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 | f12d5bb | 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 | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 471 | |
| 472 | for (;;) { |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 473 | int c; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 474 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 475 | if (p >= end) break; |
Bernie Innocenti | 5586419 | 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 | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 485 | if (c >= 64) break; |
Bernie Innocenti | 5586419 | 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 | e9ba09c | 2018-09-12 23:20:10 +0900 | [diff] [blame] | 492 | VLOG << "malformed QNAME"; |
Bernie Innocenti | 5586419 | 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 | f12d5bb | 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 | 5586419 | 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 | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 507 | !_dnsPacket_checkBytes(packet, 2, DNS_TYPE_ALL)) { |
Bernie Innocenti | e9ba09c | 2018-09-12 23:20:10 +0900 | [diff] [blame] | 508 | VLOG << "unsupported TYPE"; |
Bernie Innocenti | 5586419 | 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 | e9ba09c | 2018-09-12 23:20:10 +0900 | [diff] [blame] | 513 | VLOG << "unsupported CLASS"; |
Bernie Innocenti | 5586419 | 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 | f12d5bb | 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 | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 526 | |
| 527 | if (p + DNS_HEADER_SIZE > packet->end) { |
Bernie Innocenti | e9ba09c | 2018-09-12 23:20:10 +0900 | [diff] [blame] | 528 | VLOG << "query packet too small"; |
Bernie Innocenti | 5586419 | 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 | e9ba09c | 2018-09-12 23:20:10 +0900 | [diff] [blame] | 535 | VLOG << "query packet flags unsupported"; |
Bernie Innocenti | 5586419 | 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 | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 561 | arCount = (p[10] << 8) | p[11]; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 562 | |
| 563 | if (anCount != 0 || dnCount != 0 || arCount > 1) { |
Bernie Innocenti | e9ba09c | 2018-09-12 23:20:10 +0900 | [diff] [blame] | 564 | VLOG << "query packet contains non-query records"; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 565 | return 0; |
| 566 | } |
| 567 | |
| 568 | if (qdCount == 0) { |
Bernie Innocenti | e9ba09c | 2018-09-12 23:20:10 +0900 | [diff] [blame] | 569 | VLOG << "query packet doesn't contain query record"; |
Bernie Innocenti | 5586419 | 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 | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 576 | for (; qdCount > 0; qdCount--) |
| 577 | if (!_dnsPacket_checkQR(packet)) return 0; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 578 | |
| 579 | return 1; |
| 580 | } |
| 581 | |
Bernie Innocenti | 1f4a9fd | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 582 | /** QUERY DEBUGGING **/ |
Bernie Innocenti | e9ba09c | 2018-09-12 23:20:10 +0900 | [diff] [blame] | 583 | static char* dnsPacket_bprintQName(DnsPacket* packet, char* bp, char* bend) { |
Bernie Innocenti | f12d5bb | 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 | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 587 | |
| 588 | for (;;) { |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 589 | int c; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 590 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 591 | if (p >= end) break; |
Bernie Innocenti | 5586419 | 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 | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 601 | if (c >= 64) break; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 602 | |
| 603 | if (first) |
| 604 | first = 0; |
| 605 | else |
Bernie Innocenti | e9ba09c | 2018-09-12 23:20:10 +0900 | [diff] [blame] | 606 | bp = bprint_c(bp, bend, '.'); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 607 | |
Bernie Innocenti | e9ba09c | 2018-09-12 23:20:10 +0900 | [diff] [blame] | 608 | bp = bprint_b(bp, bend, (const char*) p, c); |
Bernie Innocenti | 5586419 | 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 | e9ba09c | 2018-09-12 23:20:10 +0900 | [diff] [blame] | 615 | bp = bprint_s(bp, bend, "<MALFORMED>"); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 616 | return bp; |
| 617 | } |
| 618 | |
Bernie Innocenti | e9ba09c | 2018-09-12 23:20:10 +0900 | [diff] [blame] | 619 | static char* dnsPacket_bprintQR(DnsPacket* packet, char* p, char* end) { |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 620 | #define QQ(x) \ |
| 621 | { DNS_TYPE_##x, #x } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 622 | static const struct { |
Bernie Innocenti | f12d5bb | 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 | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 628 | |
| 629 | /* dump QNAME */ |
Bernie Innocenti | e9ba09c | 2018-09-12 23:20:10 +0900 | [diff] [blame] | 630 | p = dnsPacket_bprintQName(packet, p, end); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 631 | |
| 632 | /* dump TYPE */ |
Bernie Innocenti | e9ba09c | 2018-09-12 23:20:10 +0900 | [diff] [blame] | 633 | p = bprint_s(p, end, " ("); |
Bernie Innocenti | 5586419 | 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 | e9ba09c | 2018-09-12 23:20:10 +0900 | [diff] [blame] | 643 | p = bprint_s(p, end, typeString); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 644 | else { |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 645 | int typeCode = _dnsPacket_readInt16(packet); |
Bernie Innocenti | e9ba09c | 2018-09-12 23:20:10 +0900 | [diff] [blame] | 646 | p = bprint(p, end, "UNKNOWN-%d", typeCode); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 647 | } |
| 648 | |
Bernie Innocenti | e9ba09c | 2018-09-12 23:20:10 +0900 | [diff] [blame] | 649 | p = bprint_c(p, end, ')'); |
Bernie Innocenti | 5586419 | 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 | e9ba09c | 2018-09-12 23:20:10 +0900 | [diff] [blame] | 657 | static char* dnsPacket_bprintQuery(DnsPacket* packet, char* p, char* end) { |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 658 | int qdCount; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 659 | |
| 660 | if (packet->base[2] & 0x1) { |
Bernie Innocenti | e9ba09c | 2018-09-12 23:20:10 +0900 | [diff] [blame] | 661 | p = bprint_s(p, end, "RECURSIVE "); |
Bernie Innocenti | 5586419 | 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 | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 668 | for (; qdCount > 0; qdCount--) { |
Bernie Innocenti | e9ba09c | 2018-09-12 23:20:10 +0900 | [diff] [blame] | 669 | p = dnsPacket_bprintQR(packet, p, end); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 670 | } |
| 671 | return p; |
| 672 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 673 | |
Bernie Innocenti | 5586419 | 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 | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 681 | #define FNV_MULT 16777619U |
| 682 | #define FNV_BASIS 2166136261U |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 683 | |
Bernie Innocenti | f12d5bb | 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 | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 687 | |
| 688 | while (numBytes > 0 && p < end) { |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 689 | hash = hash * FNV_MULT ^ *p++; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 690 | } |
| 691 | packet->cursor = p; |
| 692 | return hash; |
| 693 | } |
| 694 | |
Bernie Innocenti | f12d5bb | 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 | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 698 | |
| 699 | for (;;) { |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 700 | int c; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 701 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 702 | if (p >= end) { /* should not happen */ |
Bernie Innocenti | e9ba09c | 2018-09-12 23:20:10 +0900 | [diff] [blame] | 703 | VLOG << __func__ << ": INTERNAL_ERROR: read-overflow"; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 704 | break; |
| 705 | } |
| 706 | |
| 707 | c = *p++; |
| 708 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 709 | if (c == 0) break; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 710 | |
| 711 | if (c >= 64) { |
Bernie Innocenti | e9ba09c | 2018-09-12 23:20:10 +0900 | [diff] [blame] | 712 | VLOG << __func__ << ": INTERNAL_ERROR: malformed domain"; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 713 | break; |
| 714 | } |
| 715 | if (p + c >= end) { |
Bernie Innocenti | e9ba09c | 2018-09-12 23:20:10 +0900 | [diff] [blame] | 716 | VLOG << __func__ << ": INTERNAL_ERROR: simple label read-overflow"; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 717 | break; |
| 718 | } |
| 719 | while (c > 0) { |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 720 | hash = hash * FNV_MULT ^ *p++; |
| 721 | c -= 1; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 722 | } |
| 723 | } |
| 724 | packet->cursor = p; |
| 725 | return hash; |
| 726 | } |
| 727 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 728 | static unsigned _dnsPacket_hashQR(DnsPacket* packet, unsigned hash) { |
Bernie Innocenti | 5586419 | 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 | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 734 | static unsigned _dnsPacket_hashRR(DnsPacket* packet, unsigned hash) { |
Bernie Innocenti | 5586419 | 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 | f12d5bb | 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 | 5586419 | 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 | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 758 | hash = hash * FNV_MULT ^ (packet->base[2] & 1); |
Bernie Innocenti | 5586419 | 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 | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 776 | for (; count > 0; count--) hash = _dnsPacket_hashQR(packet, hash); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 777 | |
| 778 | /* hash ARCOUNT RRs */ |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 779 | for (; arcount > 0; arcount--) hash = _dnsPacket_hashRR(packet, hash); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 780 | |
| 781 | return hash; |
| 782 | } |
| 783 | |
Bernie Innocenti | 5586419 | 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 | e9ba09c | 2018-09-12 23:20:10 +0900 | [diff] [blame] | 787 | ** BEEN SUCCESSFULLY CHECKED. |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 788 | **/ |
| 789 | |
Bernie Innocenti | f12d5bb | 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 | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 795 | |
| 796 | for (;;) { |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 797 | int c1, c2; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 798 | |
| 799 | if (p1 >= end1 || p2 >= end2) { |
Bernie Innocenti | e9ba09c | 2018-09-12 23:20:10 +0900 | [diff] [blame] | 800 | VLOG << __func__ << ": INTERNAL_ERROR: read-overflow"; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 801 | break; |
| 802 | } |
| 803 | c1 = *p1++; |
| 804 | c2 = *p2++; |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 805 | if (c1 != c2) break; |
Bernie Innocenti | 5586419 | 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 | e9ba09c | 2018-09-12 23:20:10 +0900 | [diff] [blame] | 813 | VLOG << __func__ << ": INTERNAL_ERROR: malformed domain"; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 814 | break; |
| 815 | } |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 816 | if ((p1 + c1 > end1) || (p2 + c1 > end2)) { |
Bernie Innocenti | e9ba09c | 2018-09-12 23:20:10 +0900 | [diff] [blame] | 817 | VLOG << __func__ << ": INTERNAL_ERROR: simple label read-overflow"; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 818 | break; |
| 819 | } |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 820 | if (memcmp(p1, p2, c1) != 0) break; |
Bernie Innocenti | 5586419 | 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 | e9ba09c | 2018-09-12 23:20:10 +0900 | [diff] [blame] | 826 | VLOG << "different DN"; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 827 | return 0; |
| 828 | } |
| 829 | |
Bernie Innocenti | f12d5bb | 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 | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 833 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 834 | if (p1 + numBytes > pack1->end || p2 + numBytes > pack2->end) return 0; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 835 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 836 | if (memcmp(p1, p2, numBytes) != 0) return 0; |
Bernie Innocenti | 5586419 | 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 | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 843 | static int _dnsPacket_isEqualQR(DnsPacket* pack1, DnsPacket* pack2) { |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 844 | /* compare domain name encoding + TYPE + CLASS */ |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 845 | if (!_dnsPacket_isEqualDomainName(pack1, pack2) || |
| 846 | !_dnsPacket_isEqualBytes(pack1, pack2, 2 + 2)) |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 847 | return 0; |
| 848 | |
| 849 | return 1; |
| 850 | } |
| 851 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 852 | static int _dnsPacket_isEqualRR(DnsPacket* pack1, DnsPacket* pack2) { |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 853 | int rdlength1, rdlength2; |
| 854 | /* compare query + TTL */ |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 855 | if (!_dnsPacket_isEqualQR(pack1, pack2) || !_dnsPacket_isEqualBytes(pack1, pack2, 4)) return 0; |
Bernie Innocenti | 5586419 | 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 | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 860 | if (rdlength1 != rdlength2 || !_dnsPacket_isEqualBytes(pack1, pack2, rdlength1)) return 0; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 861 | |
| 862 | return 1; |
| 863 | } |
| 864 | |
Bernie Innocenti | f12d5bb | 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 | 5586419 | 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 | e9ba09c | 2018-09-12 23:20:10 +0900 | [diff] [blame] | 874 | VLOG << "different RD"; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 875 | return 0; |
| 876 | } |
| 877 | |
| 878 | if (pack1->base[3] != pack2->base[3]) { |
Bernie Innocenti | e9ba09c | 2018-09-12 23:20:10 +0900 | [diff] [blame] | 879 | VLOG << "different CD or AD"; |
Bernie Innocenti | 5586419 | 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 | e9ba09c | 2018-09-12 23:20:10 +0900 | [diff] [blame] | 891 | VLOG << "different QDCOUNT"; |
Bernie Innocenti | 5586419 | 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 | e9ba09c | 2018-09-12 23:20:10 +0900 | [diff] [blame] | 903 | VLOG << "different ARCOUNT"; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 904 | return 0; |
| 905 | } |
| 906 | |
| 907 | /* compare the QDCOUNT QRs */ |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 908 | for (; count1 > 0; count1--) { |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 909 | if (!_dnsPacket_isEqualQR(pack1, pack2)) { |
Bernie Innocenti | e9ba09c | 2018-09-12 23:20:10 +0900 | [diff] [blame] | 910 | VLOG << "different QR"; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 911 | return 0; |
| 912 | } |
| 913 | } |
| 914 | |
| 915 | /* compare the ARCOUNT RRs */ |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 916 | for (; arcount1 > 0; arcount1--) { |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 917 | if (!_dnsPacket_isEqualRR(pack1, pack2)) { |
Bernie Innocenti | e9ba09c | 2018-09-12 23:20:10 +0900 | [diff] [blame] | 918 | VLOG << "different additional RR"; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 919 | return 0; |
| 920 | } |
| 921 | } |
| 922 | return 1; |
| 923 | } |
| 924 | |
Bernie Innocenti | 5586419 | 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 | f12d5bb | 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 | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 935 | |
Bernie Innocenti | f12d5bb | 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 | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 942 | } Entry; |
| 943 | |
Bernie Innocenti | 1f4a9fd | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 944 | /* |
Bernie Innocenti | 5586419 | 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 | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 950 | static u_long answer_getNegativeTTL(ns_msg handle) { |
Bernie Innocenti | 5586419 | 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 | f12d5bb | 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 | 5586419 | 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 | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 966 | if (len == -1) continue; // error skipping |
Bernie Innocenti | 5586419 | 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 | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 971 | if (len == -1) continue; // error skipping |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 972 | rdata += len; |
| 973 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 974 | if (edata - rdata != 5 * NS_INT32SZ) continue; |
Bernie Innocenti | 5586419 | 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 | 1f4a9fd | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 991 | /* |
Bernie Innocenti | 5586419 | 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 | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1003 | static u_long answer_getTTL(const void* answer, int answerlen) { |
Bernie Innocenti | 5586419 | 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 | 1f4a9fd | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 1010 | if (ns_initparse((const uint8_t*) answer, answerlen, &handle) >= 0) { |
Bernie Innocenti | 5586419 | 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 | e9ba09c | 2018-09-12 23:20:10 +0900 | [diff] [blame] | 1025 | VLOG << "ns_parserr failed ancount no = " |
| 1026 | << n << ". errno = " << strerror(errno); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1027 | } |
| 1028 | } |
| 1029 | } |
| 1030 | } else { |
Bernie Innocenti | e9ba09c | 2018-09-12 23:20:10 +0900 | [diff] [blame] | 1031 | VLOG << "ns_initparse failed: " << strerror(errno); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1032 | } |
| 1033 | |
Bernie Innocenti | e9ba09c | 2018-09-12 23:20:10 +0900 | [diff] [blame] | 1034 | VLOG << "TTL = " << result; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1035 | return result; |
| 1036 | } |
| 1037 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1038 | static void entry_free(Entry* e) { |
Bernie Innocenti | 5586419 | 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 | 84ec88d | 2018-09-27 13:44:29 +0900 | [diff] [blame] | 1045 | static void entry_mru_remove(Entry* e) { |
Bernie Innocenti | 5586419 | 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 | 84ec88d | 2018-09-27 13:44:29 +0900 | [diff] [blame] | 1050 | static void entry_mru_add(Entry* e, Entry* list) { |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1051 | Entry* first = list->mru_next; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1052 | |
| 1053 | e->mru_next = first; |
| 1054 | e->mru_prev = list; |
| 1055 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1056 | list->mru_next = e; |
Bernie Innocenti | 5586419 | 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 | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1062 | static unsigned entry_hash(const Entry* e) { |
| 1063 | DnsPacket pack[1]; |
Bernie Innocenti | 5586419 | 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 | f12d5bb | 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 | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1073 | |
| 1074 | memset(e, 0, sizeof(*e)); |
| 1075 | |
Bernie Innocenti | 1f4a9fd | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 1076 | e->query = (const uint8_t*) query; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1077 | e->querylen = querylen; |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1078 | e->hash = entry_hash(e); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1079 | |
Bernie Innocenti | 1f4a9fd | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 1080 | _dnsPacket_init(pack, e->query, e->querylen); |
Bernie Innocenti | 5586419 | 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 | f12d5bb | 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 | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1089 | |
| 1090 | size = sizeof(*e) + init->querylen + answerlen; |
Bernie Innocenti | 1f4a9fd | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 1091 | e = (Entry*) calloc(size, 1); |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1092 | if (e == NULL) return e; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1093 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1094 | e->hash = init->hash; |
| 1095 | e->query = (const uint8_t*) (e + 1); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1096 | e->querylen = init->querylen; |
| 1097 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1098 | memcpy((char*) e->query, init->query, e->querylen); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1099 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1100 | e->answer = e->query + e->querylen; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1101 | e->answerlen = answerlen; |
| 1102 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1103 | memcpy((char*) e->answer, answer, e->answerlen); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1104 | |
| 1105 | return e; |
| 1106 | } |
| 1107 | |
Bernie Innocenti | f12d5bb | 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 | 5586419 | 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 | 5586419 | 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 */ |
| 1126 | #define PENDING_REQUEST_TIMEOUT 20; |
| 1127 | |
| 1128 | typedef struct pending_req_info { |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1129 | unsigned int hash; |
| 1130 | pthread_cond_t cond; |
| 1131 | struct pending_req_info* next; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1132 | } PendingReqInfo; |
| 1133 | |
| 1134 | typedef struct resolv_cache { |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1135 | int max_entries; |
| 1136 | int num_entries; |
| 1137 | Entry mru_list; |
| 1138 | int last_id; |
| 1139 | Entry* entries; |
| 1140 | PendingReqInfo pending_requests; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1141 | } Cache; |
| 1142 | |
| 1143 | struct resolv_cache_info { |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1144 | unsigned netid; |
| 1145 | Cache* cache; |
| 1146 | struct resolv_cache_info* next; |
| 1147 | int nscount; |
| 1148 | char* nameservers[MAXNS]; |
| 1149 | struct addrinfo* nsaddrinfo[MAXNS]; |
| 1150 | int revision_id; // # times the nameservers have been replaced |
| 1151 | struct __res_params params; |
Bernie Innocenti | 189eb50 | 2018-10-01 23:10:18 +0900 | [diff] [blame] | 1152 | struct res_stats nsstats[MAXNS]; |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1153 | char defdname[MAXDNSRCHPATH]; |
| 1154 | int dnsrch_offset[MAXDNSRCH + 1]; // offsets into defdname |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1155 | }; |
| 1156 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1157 | static pthread_once_t _res_cache_once = PTHREAD_ONCE_INIT; |
Bernie Innocenti | 84ec88d | 2018-09-27 13:44:29 +0900 | [diff] [blame] | 1158 | static void res_cache_init(void); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1159 | |
| 1160 | // lock protecting everything in the _resolve_cache_info structs (next ptr, etc) |
Bernie Innocenti | 84ec88d | 2018-09-27 13:44:29 +0900 | [diff] [blame] | 1161 | static pthread_mutex_t res_cache_list_lock; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1162 | |
| 1163 | /* gets cache associated with a network, or NULL if none exists */ |
Bernie Innocenti | 84ec88d | 2018-09-27 13:44:29 +0900 | [diff] [blame] | 1164 | static struct resolv_cache* find_named_cache_locked(unsigned netid); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1165 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1166 | static void _cache_flush_pending_requests_locked(struct resolv_cache* cache) { |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1167 | struct pending_req_info *ri, *tmp; |
| 1168 | if (cache) { |
| 1169 | ri = cache->pending_requests.next; |
| 1170 | |
| 1171 | while (ri) { |
| 1172 | tmp = ri; |
| 1173 | ri = ri->next; |
| 1174 | pthread_cond_broadcast(&tmp->cond); |
| 1175 | |
| 1176 | pthread_cond_destroy(&tmp->cond); |
| 1177 | free(tmp); |
| 1178 | } |
| 1179 | |
| 1180 | cache->pending_requests.next = NULL; |
| 1181 | } |
| 1182 | } |
| 1183 | |
| 1184 | /* Return 0 if no pending request is found matching the key. |
| 1185 | * If a matching request is found the calling thread will wait until |
| 1186 | * the matching request completes, then update *cache and return 1. */ |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1187 | static int _cache_check_pending_request_locked(struct resolv_cache** cache, Entry* key, |
| 1188 | unsigned netid) { |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1189 | struct pending_req_info *ri, *prev; |
| 1190 | int exist = 0; |
| 1191 | |
| 1192 | if (*cache && key) { |
| 1193 | ri = (*cache)->pending_requests.next; |
| 1194 | prev = &(*cache)->pending_requests; |
| 1195 | while (ri) { |
| 1196 | if (ri->hash == key->hash) { |
| 1197 | exist = 1; |
| 1198 | break; |
| 1199 | } |
| 1200 | prev = ri; |
| 1201 | ri = ri->next; |
| 1202 | } |
| 1203 | |
| 1204 | if (!exist) { |
Bernie Innocenti | 1f4a9fd | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 1205 | ri = (struct pending_req_info*) calloc(1, sizeof(struct pending_req_info)); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1206 | if (ri) { |
| 1207 | ri->hash = key->hash; |
| 1208 | pthread_cond_init(&ri->cond, NULL); |
| 1209 | prev->next = ri; |
| 1210 | } |
| 1211 | } else { |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1212 | struct timespec ts = {0, 0}; |
Bernie Innocenti | e9ba09c | 2018-09-12 23:20:10 +0900 | [diff] [blame] | 1213 | VLOG << "Waiting for previous request"; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1214 | ts.tv_sec = _time_now() + PENDING_REQUEST_TIMEOUT; |
Bernie Innocenti | 84ec88d | 2018-09-27 13:44:29 +0900 | [diff] [blame] | 1215 | pthread_cond_timedwait(&ri->cond, &res_cache_list_lock, &ts); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1216 | /* Must update *cache as it could have been deleted. */ |
Bernie Innocenti | 84ec88d | 2018-09-27 13:44:29 +0900 | [diff] [blame] | 1217 | *cache = find_named_cache_locked(netid); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1218 | } |
| 1219 | } |
| 1220 | |
| 1221 | return exist; |
| 1222 | } |
| 1223 | |
| 1224 | /* notify any waiting thread that waiting on a request |
| 1225 | * matching the key has been added to the cache */ |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1226 | static void _cache_notify_waiting_tid_locked(struct resolv_cache* cache, Entry* key) { |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1227 | struct pending_req_info *ri, *prev; |
| 1228 | |
| 1229 | if (cache && key) { |
| 1230 | ri = cache->pending_requests.next; |
| 1231 | prev = &cache->pending_requests; |
| 1232 | while (ri) { |
| 1233 | if (ri->hash == key->hash) { |
| 1234 | pthread_cond_broadcast(&ri->cond); |
| 1235 | break; |
| 1236 | } |
| 1237 | prev = ri; |
| 1238 | ri = ri->next; |
| 1239 | } |
| 1240 | |
| 1241 | // remove item from list and destroy |
| 1242 | if (ri) { |
| 1243 | prev->next = ri->next; |
| 1244 | pthread_cond_destroy(&ri->cond); |
| 1245 | free(ri); |
| 1246 | } |
| 1247 | } |
| 1248 | } |
| 1249 | |
| 1250 | /* notify the cache that the query failed */ |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1251 | void _resolv_cache_query_failed(unsigned netid, const void* query, int querylen) { |
| 1252 | Entry key[1]; |
| 1253 | Cache* cache; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1254 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1255 | if (!entry_init_key(key, query, querylen)) return; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1256 | |
Bernie Innocenti | 84ec88d | 2018-09-27 13:44:29 +0900 | [diff] [blame] | 1257 | pthread_mutex_lock(&res_cache_list_lock); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1258 | |
Bernie Innocenti | 84ec88d | 2018-09-27 13:44:29 +0900 | [diff] [blame] | 1259 | cache = find_named_cache_locked(netid); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1260 | |
| 1261 | if (cache) { |
| 1262 | _cache_notify_waiting_tid_locked(cache, key); |
| 1263 | } |
| 1264 | |
Bernie Innocenti | 84ec88d | 2018-09-27 13:44:29 +0900 | [diff] [blame] | 1265 | pthread_mutex_unlock(&res_cache_list_lock); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1266 | } |
| 1267 | |
cken | b1a69a4 | 2018-12-01 17:45:18 +0900 | [diff] [blame] | 1268 | static resolv_cache_info* find_cache_info_locked(unsigned netid); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1269 | |
Bernie Innocenti | d2b2703 | 2018-12-18 19:53:42 +0900 | [diff] [blame] | 1270 | static void cache_flush_locked(Cache* cache) { |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1271 | int nn; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1272 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1273 | for (nn = 0; nn < cache->max_entries; nn++) { |
| 1274 | Entry** pnode = (Entry**) &cache->entries[nn]; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1275 | |
| 1276 | while (*pnode != NULL) { |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1277 | Entry* node = *pnode; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1278 | *pnode = node->hlink; |
| 1279 | entry_free(node); |
| 1280 | } |
| 1281 | } |
| 1282 | |
| 1283 | // flush pending request |
| 1284 | _cache_flush_pending_requests_locked(cache); |
| 1285 | |
| 1286 | cache->mru_list.mru_next = cache->mru_list.mru_prev = &cache->mru_list; |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1287 | cache->num_entries = 0; |
| 1288 | cache->last_id = 0; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1289 | |
Bernie Innocenti | e9ba09c | 2018-09-12 23:20:10 +0900 | [diff] [blame] | 1290 | VLOG << "*** DNS CACHE FLUSHED ***"; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1291 | } |
| 1292 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1293 | static struct resolv_cache* _resolv_cache_create(void) { |
| 1294 | struct resolv_cache* cache; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1295 | |
Bernie Innocenti | 1f4a9fd | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 1296 | cache = (struct resolv_cache*) calloc(sizeof(*cache), 1); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1297 | if (cache) { |
nuccachen | 989d223 | 2018-11-29 17:41:12 +0800 | [diff] [blame] | 1298 | cache->max_entries = CONFIG_MAX_ENTRIES; |
Bernie Innocenti | 1f4a9fd | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 1299 | cache->entries = (Entry*) calloc(sizeof(*cache->entries), cache->max_entries); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1300 | if (cache->entries) { |
| 1301 | cache->mru_list.mru_prev = cache->mru_list.mru_next = &cache->mru_list; |
Bernie Innocenti | e9ba09c | 2018-09-12 23:20:10 +0900 | [diff] [blame] | 1302 | VLOG << __func__ << ": cache created"; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1303 | } else { |
| 1304 | free(cache); |
| 1305 | cache = NULL; |
| 1306 | } |
| 1307 | } |
| 1308 | return cache; |
| 1309 | } |
| 1310 | |
Bernie Innocenti | e9ba09c | 2018-09-12 23:20:10 +0900 | [diff] [blame] | 1311 | static void dump_query(const uint8_t* query, int querylen) { |
| 1312 | if (!kVerboseLogging) return; |
| 1313 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1314 | char temp[256], *p = temp, *end = p + sizeof(temp); |
| 1315 | DnsPacket pack[1]; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1316 | |
| 1317 | _dnsPacket_init(pack, query, querylen); |
Bernie Innocenti | e9ba09c | 2018-09-12 23:20:10 +0900 | [diff] [blame] | 1318 | p = dnsPacket_bprintQuery(pack, p, end); |
| 1319 | VLOG << temp; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1320 | } |
| 1321 | |
Bernie Innocenti | e9ba09c | 2018-09-12 23:20:10 +0900 | [diff] [blame] | 1322 | static void cache_dump_mru(Cache* cache) { |
| 1323 | if (!kVerboseLogging) return; |
| 1324 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1325 | char temp[512], *p = temp, *end = p + sizeof(temp); |
| 1326 | Entry* e; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1327 | |
Bernie Innocenti | e9ba09c | 2018-09-12 23:20:10 +0900 | [diff] [blame] | 1328 | p = bprint(temp, end, "MRU LIST (%2d): ", cache->num_entries); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1329 | for (e = cache->mru_list.mru_next; e != &cache->mru_list; e = e->mru_next) |
Bernie Innocenti | e9ba09c | 2018-09-12 23:20:10 +0900 | [diff] [blame] | 1330 | p = bprint(p, end, " %d", e->id); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1331 | |
Bernie Innocenti | e9ba09c | 2018-09-12 23:20:10 +0900 | [diff] [blame] | 1332 | VLOG << temp; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1333 | } |
| 1334 | |
Bernie Innocenti | e9ba09c | 2018-09-12 23:20:10 +0900 | [diff] [blame] | 1335 | // TODO: Rewrite to avoid creating a file in /data as temporary buffer (WAT). |
| 1336 | static void dump_answer(const u_char* answer, int answerlen) { |
| 1337 | if (!kVerboseLogging) return; |
| 1338 | |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1339 | res_state statep; |
| 1340 | FILE* fp; |
| 1341 | char* buf; |
| 1342 | int fileLen; |
| 1343 | |
| 1344 | fp = fopen("/data/reslog.txt", "w+e"); |
| 1345 | if (fp != NULL) { |
Bernie Innocenti | 4acba1a | 2018-09-26 11:52:04 +0900 | [diff] [blame] | 1346 | statep = res_get_state(); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1347 | |
| 1348 | res_pquery(statep, answer, answerlen, fp); |
| 1349 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1350 | // Get file length |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1351 | fseek(fp, 0, SEEK_END); |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1352 | fileLen = ftell(fp); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1353 | fseek(fp, 0, SEEK_SET); |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1354 | buf = (char*) malloc(fileLen + 1); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1355 | if (buf != NULL) { |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1356 | // Read file contents into buffer |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1357 | fread(buf, fileLen, 1, fp); |
Bernie Innocenti | e9ba09c | 2018-09-12 23:20:10 +0900 | [diff] [blame] | 1358 | VLOG << buf; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1359 | free(buf); |
| 1360 | } |
| 1361 | fclose(fp); |
| 1362 | remove("/data/reslog.txt"); |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1363 | } else { |
| 1364 | errno = 0; // else debug is introducing error signals |
Bernie Innocenti | e9ba09c | 2018-09-12 23:20:10 +0900 | [diff] [blame] | 1365 | VLOG << __func__ << ": can't open file"; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1366 | } |
| 1367 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1368 | |
| 1369 | /* This function tries to find a key within the hash table |
| 1370 | * In case of success, it will return a *pointer* to the hashed key. |
| 1371 | * In case of failure, it will return a *pointer* to NULL |
| 1372 | * |
| 1373 | * So, the caller must check '*result' to check for success/failure. |
| 1374 | * |
| 1375 | * The main idea is that the result can later be used directly in |
| 1376 | * calls to _resolv_cache_add or _resolv_cache_remove as the 'lookup' |
| 1377 | * parameter. This makes the code simpler and avoids re-searching |
| 1378 | * for the key position in the htable. |
| 1379 | * |
| 1380 | * The result of a lookup_p is only valid until you alter the hash |
| 1381 | * table. |
| 1382 | */ |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1383 | static Entry** _cache_lookup_p(Cache* cache, Entry* key) { |
| 1384 | int index = key->hash % cache->max_entries; |
| 1385 | Entry** pnode = (Entry**) &cache->entries[index]; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1386 | |
| 1387 | while (*pnode != NULL) { |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1388 | Entry* node = *pnode; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1389 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1390 | if (node == NULL) break; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1391 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1392 | if (node->hash == key->hash && entry_equals(node, key)) break; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1393 | |
| 1394 | pnode = &node->hlink; |
| 1395 | } |
| 1396 | return pnode; |
| 1397 | } |
| 1398 | |
| 1399 | /* Add a new entry to the hash table. 'lookup' must be the |
| 1400 | * result of an immediate previous failed _lookup_p() call |
| 1401 | * (i.e. with *lookup == NULL), and 'e' is the pointer to the |
| 1402 | * newly created entry |
| 1403 | */ |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1404 | static void _cache_add_p(Cache* cache, Entry** lookup, Entry* e) { |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1405 | *lookup = e; |
| 1406 | e->id = ++cache->last_id; |
| 1407 | entry_mru_add(e, &cache->mru_list); |
| 1408 | cache->num_entries += 1; |
| 1409 | |
Bernie Innocenti | e9ba09c | 2018-09-12 23:20:10 +0900 | [diff] [blame] | 1410 | VLOG << __func__ << ": entry " << e->id << " added (count=" << cache->num_entries << ")"; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1411 | } |
| 1412 | |
| 1413 | /* Remove an existing entry from the hash table, |
| 1414 | * 'lookup' must be the result of an immediate previous |
| 1415 | * and succesful _lookup_p() call. |
| 1416 | */ |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1417 | static void _cache_remove_p(Cache* cache, Entry** lookup) { |
| 1418 | Entry* e = *lookup; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1419 | |
Bernie Innocenti | e9ba09c | 2018-09-12 23:20:10 +0900 | [diff] [blame] | 1420 | VLOG << __func__ << ": entry " << e->id << " removed (count=" << cache->num_entries - 1 << ")"; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1421 | |
| 1422 | entry_mru_remove(e); |
| 1423 | *lookup = e->hlink; |
| 1424 | entry_free(e); |
| 1425 | cache->num_entries -= 1; |
| 1426 | } |
| 1427 | |
| 1428 | /* Remove the oldest entry from the hash table. |
| 1429 | */ |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1430 | static void _cache_remove_oldest(Cache* cache) { |
| 1431 | Entry* oldest = cache->mru_list.mru_prev; |
| 1432 | Entry** lookup = _cache_lookup_p(cache, oldest); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1433 | |
| 1434 | if (*lookup == NULL) { /* should not happen */ |
Bernie Innocenti | e9ba09c | 2018-09-12 23:20:10 +0900 | [diff] [blame] | 1435 | VLOG << __func__ << ": OLDEST NOT IN HTABLE ?"; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1436 | return; |
| 1437 | } |
Bernie Innocenti | e9ba09c | 2018-09-12 23:20:10 +0900 | [diff] [blame] | 1438 | VLOG << "Cache full - removing oldest"; |
| 1439 | dump_query(oldest->query, oldest->querylen); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1440 | _cache_remove_p(cache, lookup); |
| 1441 | } |
| 1442 | |
| 1443 | /* Remove all expired entries from the hash table. |
| 1444 | */ |
| 1445 | static void _cache_remove_expired(Cache* cache) { |
| 1446 | Entry* e; |
| 1447 | time_t now = _time_now(); |
| 1448 | |
| 1449 | for (e = cache->mru_list.mru_next; e != &cache->mru_list;) { |
| 1450 | // Entry is old, remove |
| 1451 | if (now >= e->expires) { |
| 1452 | Entry** lookup = _cache_lookup_p(cache, e); |
| 1453 | if (*lookup == NULL) { /* should not happen */ |
Bernie Innocenti | e9ba09c | 2018-09-12 23:20:10 +0900 | [diff] [blame] | 1454 | VLOG << __func__ << ": ENTRY NOT IN HTABLE ?"; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1455 | return; |
| 1456 | } |
| 1457 | e = e->mru_next; |
| 1458 | _cache_remove_p(cache, lookup); |
| 1459 | } else { |
| 1460 | e = e->mru_next; |
| 1461 | } |
| 1462 | } |
| 1463 | } |
| 1464 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1465 | ResolvCacheStatus _resolv_cache_lookup(unsigned netid, const void* query, int querylen, |
| 1466 | void* answer, int answersize, int* answerlen) { |
| 1467 | Entry key[1]; |
| 1468 | Entry** lookup; |
| 1469 | Entry* e; |
| 1470 | time_t now; |
| 1471 | Cache* cache; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1472 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1473 | ResolvCacheStatus result = RESOLV_CACHE_NOTFOUND; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1474 | |
Bernie Innocenti | e9ba09c | 2018-09-12 23:20:10 +0900 | [diff] [blame] | 1475 | VLOG << __func__ << ": lookup"; |
| 1476 | dump_query((u_char*) query, querylen); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1477 | |
| 1478 | /* we don't cache malformed queries */ |
| 1479 | if (!entry_init_key(key, query, querylen)) { |
Bernie Innocenti | e9ba09c | 2018-09-12 23:20:10 +0900 | [diff] [blame] | 1480 | VLOG << __func__ << ": unsupported query"; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1481 | return RESOLV_CACHE_UNSUPPORTED; |
| 1482 | } |
| 1483 | /* lookup cache */ |
Bernie Innocenti | 84ec88d | 2018-09-27 13:44:29 +0900 | [diff] [blame] | 1484 | pthread_once(&_res_cache_once, res_cache_init); |
| 1485 | pthread_mutex_lock(&res_cache_list_lock); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1486 | |
Bernie Innocenti | 84ec88d | 2018-09-27 13:44:29 +0900 | [diff] [blame] | 1487 | cache = find_named_cache_locked(netid); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1488 | if (cache == NULL) { |
| 1489 | result = RESOLV_CACHE_UNSUPPORTED; |
| 1490 | goto Exit; |
| 1491 | } |
| 1492 | |
| 1493 | /* see the description of _lookup_p to understand this. |
| 1494 | * the function always return a non-NULL pointer. |
| 1495 | */ |
| 1496 | lookup = _cache_lookup_p(cache, key); |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1497 | e = *lookup; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1498 | |
| 1499 | if (e == NULL) { |
Bernie Innocenti | e9ba09c | 2018-09-12 23:20:10 +0900 | [diff] [blame] | 1500 | VLOG << "NOT IN CACHE"; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1501 | // calling thread will wait if an outstanding request is found |
| 1502 | // that matching this query |
| 1503 | if (!_cache_check_pending_request_locked(&cache, key, netid) || cache == NULL) { |
| 1504 | goto Exit; |
| 1505 | } else { |
| 1506 | lookup = _cache_lookup_p(cache, key); |
| 1507 | e = *lookup; |
| 1508 | if (e == NULL) { |
| 1509 | goto Exit; |
| 1510 | } |
| 1511 | } |
| 1512 | } |
| 1513 | |
| 1514 | now = _time_now(); |
| 1515 | |
| 1516 | /* remove stale entries here */ |
| 1517 | if (now >= e->expires) { |
Bernie Innocenti | e9ba09c | 2018-09-12 23:20:10 +0900 | [diff] [blame] | 1518 | VLOG << " NOT IN CACHE (STALE ENTRY " << *lookup << "DISCARDED)"; |
| 1519 | dump_query(e->query, e->querylen); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1520 | _cache_remove_p(cache, lookup); |
| 1521 | goto Exit; |
| 1522 | } |
| 1523 | |
| 1524 | *answerlen = e->answerlen; |
| 1525 | if (e->answerlen > answersize) { |
| 1526 | /* NOTE: we return UNSUPPORTED if the answer buffer is too short */ |
| 1527 | result = RESOLV_CACHE_UNSUPPORTED; |
Bernie Innocenti | e9ba09c | 2018-09-12 23:20:10 +0900 | [diff] [blame] | 1528 | VLOG << " ANSWER TOO LONG"; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1529 | goto Exit; |
| 1530 | } |
| 1531 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1532 | memcpy(answer, e->answer, e->answerlen); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1533 | |
| 1534 | /* bump up this entry to the top of the MRU list */ |
| 1535 | if (e != cache->mru_list.mru_next) { |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1536 | entry_mru_remove(e); |
| 1537 | entry_mru_add(e, &cache->mru_list); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1538 | } |
| 1539 | |
Bernie Innocenti | e9ba09c | 2018-09-12 23:20:10 +0900 | [diff] [blame] | 1540 | VLOG << "FOUND IN CACHE entry=" << e; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1541 | result = RESOLV_CACHE_FOUND; |
| 1542 | |
| 1543 | Exit: |
Bernie Innocenti | 84ec88d | 2018-09-27 13:44:29 +0900 | [diff] [blame] | 1544 | pthread_mutex_unlock(&res_cache_list_lock); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1545 | return result; |
| 1546 | } |
| 1547 | |
Bernie Innocenti | f12d5bb | 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 | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1555 | |
| 1556 | /* don't assume that the query has already been cached |
| 1557 | */ |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1558 | if (!entry_init_key(key, query, querylen)) { |
Bernie Innocenti | e9ba09c | 2018-09-12 23:20:10 +0900 | [diff] [blame] | 1559 | VLOG << __func__ << ": passed invalid query?"; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1560 | return; |
| 1561 | } |
| 1562 | |
Bernie Innocenti | 84ec88d | 2018-09-27 13:44:29 +0900 | [diff] [blame] | 1563 | pthread_mutex_lock(&res_cache_list_lock); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1564 | |
Bernie Innocenti | 84ec88d | 2018-09-27 13:44:29 +0900 | [diff] [blame] | 1565 | cache = find_named_cache_locked(netid); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1566 | if (cache == NULL) { |
| 1567 | goto Exit; |
| 1568 | } |
| 1569 | |
Bernie Innocenti | e9ba09c | 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 | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1577 | |
| 1578 | lookup = _cache_lookup_p(cache, key); |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1579 | e = *lookup; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1580 | |
| 1581 | if (e != NULL) { /* should not happen */ |
Bernie Innocenti | e9ba09c | 2018-09-12 23:20:10 +0900 | [diff] [blame] | 1582 | VLOG << __func__ << ": ALREADY IN CACHE (" << e << ") ? IGNORING ADD"; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1583 | goto Exit; |
| 1584 | } |
| 1585 | |
| 1586 | if (cache->num_entries >= cache->max_entries) { |
| 1587 | _cache_remove_expired(cache); |
| 1588 | if (cache->num_entries >= cache->max_entries) { |
| 1589 | _cache_remove_oldest(cache); |
| 1590 | } |
| 1591 | /* need to lookup again */ |
| 1592 | lookup = _cache_lookup_p(cache, key); |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1593 | e = *lookup; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1594 | if (e != NULL) { |
Bernie Innocenti | e9ba09c | 2018-09-12 23:20:10 +0900 | [diff] [blame] | 1595 | VLOG << __func__ << ": ALREADY IN CACHE (" << e << ") ? IGNORING ADD"; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1596 | goto Exit; |
| 1597 | } |
| 1598 | } |
| 1599 | |
| 1600 | ttl = answer_getTTL(answer, answerlen); |
| 1601 | if (ttl > 0) { |
| 1602 | e = entry_alloc(key, answer, answerlen); |
| 1603 | if (e != NULL) { |
| 1604 | e->expires = ttl + _time_now(); |
| 1605 | _cache_add_p(cache, lookup, e); |
| 1606 | } |
| 1607 | } |
Bernie Innocenti | e9ba09c | 2018-09-12 23:20:10 +0900 | [diff] [blame] | 1608 | cache_dump_mru(cache); |
| 1609 | |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1610 | Exit: |
| 1611 | if (cache != NULL) { |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1612 | _cache_notify_waiting_tid_locked(cache, key); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1613 | } |
Bernie Innocenti | 84ec88d | 2018-09-27 13:44:29 +0900 | [diff] [blame] | 1614 | pthread_mutex_unlock(&res_cache_list_lock); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1615 | } |
| 1616 | |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1617 | // Head of the list of caches. Protected by _res_cache_list_lock. |
Bernie Innocenti | 84ec88d | 2018-09-27 13:44:29 +0900 | [diff] [blame] | 1618 | static struct resolv_cache_info res_cache_list; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1619 | |
cken | b1a69a4 | 2018-12-01 17:45:18 +0900 | [diff] [blame] | 1620 | // insert resolv_cache_info into the list of resolv_cache_infos |
Bernie Innocenti | 84ec88d | 2018-09-27 13:44:29 +0900 | [diff] [blame] | 1621 | static void insert_cache_info_locked(resolv_cache_info* cache_info); |
cken | b1a69a4 | 2018-12-01 17:45:18 +0900 | [diff] [blame] | 1622 | // creates a resolv_cache_info |
Bernie Innocenti | 84ec88d | 2018-09-27 13:44:29 +0900 | [diff] [blame] | 1623 | static resolv_cache_info* create_cache_info(); |
cken | b1a69a4 | 2018-12-01 17:45:18 +0900 | [diff] [blame] | 1624 | // gets a resolv_cache_info associated with a network, or NULL if not found |
| 1625 | static resolv_cache_info* find_cache_info_locked(unsigned netid); |
cken | b1a69a4 | 2018-12-01 17:45:18 +0900 | [diff] [blame] | 1626 | // empty the nameservers set for the named cache |
Bernie Innocenti | 84ec88d | 2018-09-27 13:44:29 +0900 | [diff] [blame] | 1627 | static void free_nameservers_locked(resolv_cache_info* cache_info); |
| 1628 | // return 1 if the provided list of name servers differs from the list of name servers |
| 1629 | // currently attached to the provided cache_info |
| 1630 | static int resolv_is_nameservers_equal_locked(resolv_cache_info* cache_info, const char** servers, |
| 1631 | int numservers); |
cken | b1a69a4 | 2018-12-01 17:45:18 +0900 | [diff] [blame] | 1632 | // clears the stats samples contained withing the given cache_info |
Bernie Innocenti | 84ec88d | 2018-09-27 13:44:29 +0900 | [diff] [blame] | 1633 | static void res_cache_clear_stats_locked(resolv_cache_info* cache_info); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1634 | |
Bernie Innocenti | 84ec88d | 2018-09-27 13:44:29 +0900 | [diff] [blame] | 1635 | static void res_cache_init(void) { |
| 1636 | memset(&res_cache_list, 0, sizeof(res_cache_list)); |
| 1637 | pthread_mutex_init(&res_cache_list_lock, NULL); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1638 | } |
| 1639 | |
cken | b1a69a4 | 2018-12-01 17:45:18 +0900 | [diff] [blame] | 1640 | // public API for netd to query if name server is set on specific netid |
| 1641 | bool resolv_has_nameservers(unsigned netid) { |
| 1642 | pthread_once(&_res_cache_once, res_cache_init); |
| 1643 | pthread_mutex_lock(&res_cache_list_lock); |
| 1644 | resolv_cache_info* info = find_cache_info_locked(netid); |
| 1645 | const bool ret = (info != nullptr) && (info->nscount > 0); |
| 1646 | pthread_mutex_unlock(&res_cache_list_lock); |
| 1647 | |
| 1648 | return ret; |
| 1649 | } |
| 1650 | |
| 1651 | // look up the named cache, and creates one if needed |
Bernie Innocenti | 84ec88d | 2018-09-27 13:44:29 +0900 | [diff] [blame] | 1652 | static resolv_cache* get_res_cache_for_net_locked(unsigned netid) { |
| 1653 | resolv_cache* cache = find_named_cache_locked(netid); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1654 | if (!cache) { |
Bernie Innocenti | 84ec88d | 2018-09-27 13:44:29 +0900 | [diff] [blame] | 1655 | resolv_cache_info* cache_info = create_cache_info(); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1656 | if (cache_info) { |
| 1657 | cache = _resolv_cache_create(); |
| 1658 | if (cache) { |
| 1659 | cache_info->cache = cache; |
| 1660 | cache_info->netid = netid; |
Bernie Innocenti | 84ec88d | 2018-09-27 13:44:29 +0900 | [diff] [blame] | 1661 | insert_cache_info_locked(cache_info); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1662 | } else { |
| 1663 | free(cache_info); |
| 1664 | } |
| 1665 | } |
| 1666 | } |
| 1667 | return cache; |
| 1668 | } |
| 1669 | |
Bernie Innocenti | 189eb50 | 2018-10-01 23:10:18 +0900 | [diff] [blame] | 1670 | void resolv_delete_cache_for_net(unsigned netid) { |
Bernie Innocenti | 84ec88d | 2018-09-27 13:44:29 +0900 | [diff] [blame] | 1671 | pthread_once(&_res_cache_once, res_cache_init); |
| 1672 | pthread_mutex_lock(&res_cache_list_lock); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1673 | |
Bernie Innocenti | 84ec88d | 2018-09-27 13:44:29 +0900 | [diff] [blame] | 1674 | struct resolv_cache_info* prev_cache_info = &res_cache_list; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1675 | |
| 1676 | while (prev_cache_info->next) { |
| 1677 | struct resolv_cache_info* cache_info = prev_cache_info->next; |
| 1678 | |
| 1679 | if (cache_info->netid == netid) { |
| 1680 | prev_cache_info->next = cache_info->next; |
Bernie Innocenti | d2b2703 | 2018-12-18 19:53:42 +0900 | [diff] [blame] | 1681 | cache_flush_locked(cache_info->cache); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1682 | free(cache_info->cache->entries); |
| 1683 | free(cache_info->cache); |
Bernie Innocenti | 84ec88d | 2018-09-27 13:44:29 +0900 | [diff] [blame] | 1684 | free_nameservers_locked(cache_info); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1685 | free(cache_info); |
| 1686 | break; |
| 1687 | } |
| 1688 | |
| 1689 | prev_cache_info = prev_cache_info->next; |
| 1690 | } |
| 1691 | |
Bernie Innocenti | 84ec88d | 2018-09-27 13:44:29 +0900 | [diff] [blame] | 1692 | pthread_mutex_unlock(&res_cache_list_lock); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1693 | } |
| 1694 | |
Bernie Innocenti | 84ec88d | 2018-09-27 13:44:29 +0900 | [diff] [blame] | 1695 | static resolv_cache_info* create_cache_info() { |
Bernie Innocenti | 1f4a9fd | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 1696 | return (struct resolv_cache_info*) calloc(sizeof(struct resolv_cache_info), 1); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1697 | } |
| 1698 | |
Bernie Innocenti | 84ec88d | 2018-09-27 13:44:29 +0900 | [diff] [blame] | 1699 | static void insert_cache_info_locked(struct resolv_cache_info* cache_info) { |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1700 | struct resolv_cache_info* last; |
Bernie Innocenti | 84ec88d | 2018-09-27 13:44:29 +0900 | [diff] [blame] | 1701 | for (last = &res_cache_list; last->next; last = last->next) {} |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1702 | last->next = cache_info; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1703 | } |
| 1704 | |
Bernie Innocenti | 84ec88d | 2018-09-27 13:44:29 +0900 | [diff] [blame] | 1705 | static resolv_cache* find_named_cache_locked(unsigned netid) { |
cken | b1a69a4 | 2018-12-01 17:45:18 +0900 | [diff] [blame] | 1706 | resolv_cache_info* info = find_cache_info_locked(netid); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1707 | if (info != NULL) return info->cache; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1708 | return NULL; |
| 1709 | } |
| 1710 | |
cken | b1a69a4 | 2018-12-01 17:45:18 +0900 | [diff] [blame] | 1711 | static resolv_cache_info* find_cache_info_locked(unsigned netid) { |
Bernie Innocenti | 84ec88d | 2018-09-27 13:44:29 +0900 | [diff] [blame] | 1712 | struct resolv_cache_info* cache_info = res_cache_list.next; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1713 | |
| 1714 | while (cache_info) { |
| 1715 | if (cache_info->netid == netid) { |
| 1716 | break; |
| 1717 | } |
| 1718 | |
| 1719 | cache_info = cache_info->next; |
| 1720 | } |
| 1721 | return cache_info; |
| 1722 | } |
| 1723 | |
Bernie Innocenti | 1fbca5c | 2018-10-01 20:46:20 +0900 | [diff] [blame] | 1724 | static void resolv_set_default_params(struct __res_params* params) { |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1725 | params->sample_validity = NSSAMPLE_VALIDITY; |
| 1726 | params->success_threshold = SUCCESS_THRESHOLD; |
| 1727 | params->min_samples = 0; |
| 1728 | params->max_samples = 0; |
| 1729 | params->base_timeout_msec = 0; // 0 = legacy algorithm |
| 1730 | } |
| 1731 | |
Bernie Innocenti | 45238a1 | 2018-12-04 14:57:48 +0900 | [diff] [blame^] | 1732 | int resolv_set_nameservers_for_net(unsigned netid, const char** servers, const int numservers, |
Bernie Innocenti | 189eb50 | 2018-10-01 23:10:18 +0900 | [diff] [blame] | 1733 | const char* domains, const __res_params* params) { |
Bernie Innocenti | 1f4a9fd | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 1734 | char* cp; |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1735 | int* offset; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1736 | struct addrinfo* nsaddrinfo[MAXNS]; |
| 1737 | |
| 1738 | if (numservers > MAXNS) { |
Bernie Innocenti | e9ba09c | 2018-09-12 23:20:10 +0900 | [diff] [blame] | 1739 | VLOG << __func__ << ": numservers=" << numservers << ", MAXNS=" << MAXNS; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1740 | return E2BIG; |
| 1741 | } |
| 1742 | |
| 1743 | // Parse the addresses before actually locking or changing any state, in case there is an error. |
| 1744 | // As a side effect this also reduces the time the lock is kept. |
Bernie Innocenti | c165ce8 | 2018-10-16 23:35:28 +0900 | [diff] [blame] | 1745 | char sbuf[NI_MAXSERV]; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1746 | snprintf(sbuf, sizeof(sbuf), "%u", NAMESERVER_PORT); |
Bernie Innocenti | 45238a1 | 2018-12-04 14:57:48 +0900 | [diff] [blame^] | 1747 | for (int i = 0; i < numservers; i++) { |
Bernie Innocenti | 84ec88d | 2018-09-27 13:44:29 +0900 | [diff] [blame] | 1748 | // The addrinfo structures allocated here are freed in free_nameservers_locked(). |
Bernie Innocenti | c165ce8 | 2018-10-16 23:35:28 +0900 | [diff] [blame] | 1749 | const addrinfo hints = { |
| 1750 | .ai_family = AF_UNSPEC, .ai_socktype = SOCK_DGRAM, .ai_flags = AI_NUMERICHOST}; |
| 1751 | int rt = getaddrinfo_numeric(servers[i], sbuf, hints, &nsaddrinfo[i]); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1752 | if (rt != 0) { |
Bernie Innocenti | 45238a1 | 2018-12-04 14:57:48 +0900 | [diff] [blame^] | 1753 | for (int j = 0; j < i; j++) { |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1754 | freeaddrinfo(nsaddrinfo[j]); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1755 | } |
Bernie Innocenti | c165ce8 | 2018-10-16 23:35:28 +0900 | [diff] [blame] | 1756 | VLOG << __func__ << ": getaddrinfo_numeric(" << servers[i] |
| 1757 | << ") = " << gai_strerror(rt); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1758 | return EINVAL; |
| 1759 | } |
| 1760 | } |
| 1761 | |
Bernie Innocenti | 84ec88d | 2018-09-27 13:44:29 +0900 | [diff] [blame] | 1762 | pthread_once(&_res_cache_once, res_cache_init); |
| 1763 | pthread_mutex_lock(&res_cache_list_lock); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1764 | |
| 1765 | // creates the cache if not created |
Bernie Innocenti | 84ec88d | 2018-09-27 13:44:29 +0900 | [diff] [blame] | 1766 | get_res_cache_for_net_locked(netid); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1767 | |
cken | b1a69a4 | 2018-12-01 17:45:18 +0900 | [diff] [blame] | 1768 | resolv_cache_info* cache_info = find_cache_info_locked(netid); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1769 | |
| 1770 | if (cache_info != NULL) { |
| 1771 | uint8_t old_max_samples = cache_info->params.max_samples; |
| 1772 | if (params != NULL) { |
| 1773 | cache_info->params = *params; |
| 1774 | } else { |
Bernie Innocenti | 1fbca5c | 2018-10-01 20:46:20 +0900 | [diff] [blame] | 1775 | resolv_set_default_params(&cache_info->params); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1776 | } |
| 1777 | |
Bernie Innocenti | 84ec88d | 2018-09-27 13:44:29 +0900 | [diff] [blame] | 1778 | if (!resolv_is_nameservers_equal_locked(cache_info, servers, numservers)) { |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1779 | // free current before adding new |
Bernie Innocenti | 84ec88d | 2018-09-27 13:44:29 +0900 | [diff] [blame] | 1780 | free_nameservers_locked(cache_info); |
Bernie Innocenti | 45238a1 | 2018-12-04 14:57:48 +0900 | [diff] [blame^] | 1781 | for (int i = 0; i < numservers; i++) { |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1782 | cache_info->nsaddrinfo[i] = nsaddrinfo[i]; |
| 1783 | cache_info->nameservers[i] = strdup(servers[i]); |
Bernie Innocenti | e9ba09c | 2018-09-12 23:20:10 +0900 | [diff] [blame] | 1784 | VLOG << __func__ << ": netid = " << netid << ", addr = " << servers[i]; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1785 | } |
| 1786 | cache_info->nscount = numservers; |
| 1787 | |
| 1788 | // Clear the NS statistics because the mapping to nameservers might have changed. |
Bernie Innocenti | 84ec88d | 2018-09-27 13:44:29 +0900 | [diff] [blame] | 1789 | res_cache_clear_stats_locked(cache_info); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1790 | |
| 1791 | // increment the revision id to ensure that sample state is not written back if the |
| 1792 | // servers change; in theory it would suffice to do so only if the servers or |
| 1793 | // max_samples actually change, in practice the overhead of checking is higher than the |
| 1794 | // cost, and overflows are unlikely |
| 1795 | ++cache_info->revision_id; |
Ken Chen | 26f01e4 | 2018-11-02 13:18:40 +0800 | [diff] [blame] | 1796 | } else { |
| 1797 | if (cache_info->params.max_samples != old_max_samples) { |
| 1798 | // If the maximum number of samples changes, the overhead of keeping the most recent |
| 1799 | // samples around is not considered worth the effort, so they are cleared instead. |
| 1800 | // All other parameters do not affect shared state: Changing these parameters does |
| 1801 | // not invalidate the samples, as they only affect aggregation and the conditions |
| 1802 | // under which servers are considered usable. |
| 1803 | res_cache_clear_stats_locked(cache_info); |
| 1804 | ++cache_info->revision_id; |
| 1805 | } |
Bernie Innocenti | 45238a1 | 2018-12-04 14:57:48 +0900 | [diff] [blame^] | 1806 | for (int j = 0; j < numservers; j++) { |
Ken Chen | 26f01e4 | 2018-11-02 13:18:40 +0800 | [diff] [blame] | 1807 | freeaddrinfo(nsaddrinfo[j]); |
| 1808 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1809 | } |
| 1810 | |
| 1811 | // Always update the search paths, since determining whether they actually changed is |
| 1812 | // complex due to the zero-padding, and probably not worth the effort. Cache-flushing |
| 1813 | // however is not // necessary, since the stored cache entries do contain the domain, not |
| 1814 | // just the host name. |
| 1815 | // code moved from res_init.c, load_domain_search_list |
| 1816 | strlcpy(cache_info->defdname, domains, sizeof(cache_info->defdname)); |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1817 | if ((cp = strchr(cache_info->defdname, '\n')) != NULL) *cp = '\0'; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1818 | |
| 1819 | cp = cache_info->defdname; |
| 1820 | offset = cache_info->dnsrch_offset; |
| 1821 | while (offset < cache_info->dnsrch_offset + MAXDNSRCH) { |
| 1822 | while (*cp == ' ' || *cp == '\t') /* skip leading white space */ |
| 1823 | cp++; |
| 1824 | if (*cp == '\0') /* stop if nothing more to do */ |
| 1825 | break; |
| 1826 | *offset++ = cp - cache_info->defdname; /* record this search domain */ |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1827 | while (*cp) { /* zero-terminate it */ |
| 1828 | if (*cp == ' ' || *cp == '\t') { |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1829 | *cp++ = '\0'; |
| 1830 | break; |
| 1831 | } |
| 1832 | cp++; |
| 1833 | } |
| 1834 | } |
| 1835 | *offset = -1; /* cache_info->dnsrch_offset has MAXDNSRCH+1 items */ |
| 1836 | } |
| 1837 | |
Bernie Innocenti | 84ec88d | 2018-09-27 13:44:29 +0900 | [diff] [blame] | 1838 | pthread_mutex_unlock(&res_cache_list_lock); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1839 | return 0; |
| 1840 | } |
| 1841 | |
Bernie Innocenti | 84ec88d | 2018-09-27 13:44:29 +0900 | [diff] [blame] | 1842 | static int resolv_is_nameservers_equal_locked(resolv_cache_info* cache_info, const char** servers, |
| 1843 | int numservers) { |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1844 | if (cache_info->nscount != numservers) { |
| 1845 | return 0; |
| 1846 | } |
| 1847 | |
| 1848 | // Compare each name server against current name servers. |
| 1849 | // TODO: this is incorrect if the list of current or previous nameservers |
| 1850 | // contains duplicates. This does not really matter because the framework |
| 1851 | // filters out duplicates, but we should probably fix it. It's also |
| 1852 | // insensitive to the order of the nameservers; we should probably fix that |
| 1853 | // too. |
| 1854 | for (int i = 0; i < numservers; i++) { |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1855 | for (int j = 0;; j++) { |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1856 | if (j >= numservers) { |
| 1857 | return 0; |
| 1858 | } |
| 1859 | if (strcmp(cache_info->nameservers[i], servers[j]) == 0) { |
| 1860 | break; |
| 1861 | } |
| 1862 | } |
| 1863 | } |
| 1864 | |
| 1865 | return 1; |
| 1866 | } |
| 1867 | |
Bernie Innocenti | 84ec88d | 2018-09-27 13:44:29 +0900 | [diff] [blame] | 1868 | static void free_nameservers_locked(resolv_cache_info* cache_info) { |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1869 | int i; |
| 1870 | for (i = 0; i < cache_info->nscount; i++) { |
| 1871 | free(cache_info->nameservers[i]); |
| 1872 | cache_info->nameservers[i] = NULL; |
| 1873 | if (cache_info->nsaddrinfo[i] != NULL) { |
| 1874 | freeaddrinfo(cache_info->nsaddrinfo[i]); |
| 1875 | cache_info->nsaddrinfo[i] = NULL; |
| 1876 | } |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1877 | cache_info->nsstats[i].sample_count = cache_info->nsstats[i].sample_next = 0; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1878 | } |
| 1879 | cache_info->nscount = 0; |
Bernie Innocenti | 84ec88d | 2018-09-27 13:44:29 +0900 | [diff] [blame] | 1880 | res_cache_clear_stats_locked(cache_info); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1881 | ++cache_info->revision_id; |
| 1882 | } |
| 1883 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1884 | void _resolv_populate_res_for_net(res_state statp) { |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1885 | if (statp == NULL) { |
| 1886 | return; |
| 1887 | } |
| 1888 | |
Bernie Innocenti | 84ec88d | 2018-09-27 13:44:29 +0900 | [diff] [blame] | 1889 | pthread_once(&_res_cache_once, res_cache_init); |
| 1890 | pthread_mutex_lock(&res_cache_list_lock); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1891 | |
cken | b1a69a4 | 2018-12-01 17:45:18 +0900 | [diff] [blame] | 1892 | resolv_cache_info* info = find_cache_info_locked(statp->netid); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1893 | if (info != NULL) { |
| 1894 | int nserv; |
| 1895 | struct addrinfo* ai; |
Bernie Innocenti | e9ba09c | 2018-09-12 23:20:10 +0900 | [diff] [blame] | 1896 | VLOG << __func__ << ": " << statp->netid; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1897 | for (nserv = 0; nserv < MAXNS; nserv++) { |
| 1898 | ai = info->nsaddrinfo[nserv]; |
| 1899 | if (ai == NULL) { |
| 1900 | break; |
| 1901 | } |
| 1902 | |
| 1903 | if ((size_t) ai->ai_addrlen <= sizeof(statp->_u._ext.ext->nsaddrs[0])) { |
| 1904 | if (statp->_u._ext.ext != NULL) { |
| 1905 | memcpy(&statp->_u._ext.ext->nsaddrs[nserv], ai->ai_addr, ai->ai_addrlen); |
| 1906 | statp->nsaddr_list[nserv].sin_family = AF_UNSPEC; |
| 1907 | } else { |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1908 | if ((size_t) ai->ai_addrlen <= sizeof(statp->nsaddr_list[0])) { |
| 1909 | memcpy(&statp->nsaddr_list[nserv], ai->ai_addr, ai->ai_addrlen); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1910 | } else { |
| 1911 | statp->nsaddr_list[nserv].sin_family = AF_UNSPEC; |
| 1912 | } |
| 1913 | } |
| 1914 | } else { |
Bernie Innocenti | e9ba09c | 2018-09-12 23:20:10 +0900 | [diff] [blame] | 1915 | VLOG << __func__ << ": found too long addrlen"; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1916 | } |
| 1917 | } |
| 1918 | statp->nscount = nserv; |
| 1919 | // now do search domains. Note that we cache the offsets as this code runs alot |
| 1920 | // but the setting/offset-computer only runs when set/changed |
| 1921 | // WARNING: Don't use str*cpy() here, this string contains zeroes. |
| 1922 | memcpy(statp->defdname, info->defdname, sizeof(statp->defdname)); |
Bernie Innocenti | 1f4a9fd | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 1923 | char** pp = statp->dnsrch; |
| 1924 | int* p = info->dnsrch_offset; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1925 | while (pp < statp->dnsrch + MAXDNSRCH && *p != -1) { |
| 1926 | *pp++ = &statp->defdname[0] + *p++; |
| 1927 | } |
| 1928 | } |
Bernie Innocenti | 84ec88d | 2018-09-27 13:44:29 +0900 | [diff] [blame] | 1929 | pthread_mutex_unlock(&res_cache_list_lock); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1930 | } |
| 1931 | |
| 1932 | /* Resolver reachability statistics. */ |
| 1933 | |
Bernie Innocenti | 189eb50 | 2018-10-01 23:10:18 +0900 | [diff] [blame] | 1934 | static void _res_cache_add_stats_sample_locked(res_stats* stats, const res_sample* sample, |
| 1935 | int max_samples) { |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1936 | // Note: This function expects max_samples > 0, otherwise a (harmless) modification of the |
| 1937 | // allocated but supposedly unused memory for samples[0] will happen |
Bernie Innocenti | e9ba09c | 2018-09-12 23:20:10 +0900 | [diff] [blame] | 1938 | VLOG << __func__ << ": adding sample to stats, next = " << stats->sample_next |
| 1939 | << ", count = " << stats->sample_count; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1940 | stats->samples[stats->sample_next] = *sample; |
| 1941 | if (stats->sample_count < max_samples) { |
| 1942 | ++stats->sample_count; |
| 1943 | } |
| 1944 | if (++stats->sample_next >= max_samples) { |
| 1945 | stats->sample_next = 0; |
| 1946 | } |
| 1947 | } |
| 1948 | |
Bernie Innocenti | 84ec88d | 2018-09-27 13:44:29 +0900 | [diff] [blame] | 1949 | static void res_cache_clear_stats_locked(resolv_cache_info* cache_info) { |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1950 | if (cache_info) { |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1951 | for (int i = 0; i < MAXNS; ++i) { |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1952 | cache_info->nsstats->sample_count = cache_info->nsstats->sample_next = 0; |
| 1953 | } |
| 1954 | } |
| 1955 | } |
| 1956 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1957 | int android_net_res_stats_get_info_for_net(unsigned netid, int* nscount, |
| 1958 | struct sockaddr_storage servers[MAXNS], int* dcount, |
| 1959 | char domains[MAXDNSRCH][MAXDNSRCHPATH], |
| 1960 | struct __res_params* params, |
Bernie Innocenti | 189eb50 | 2018-10-01 23:10:18 +0900 | [diff] [blame] | 1961 | struct res_stats stats[MAXNS]) { |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1962 | int revision_id = -1; |
Bernie Innocenti | 84ec88d | 2018-09-27 13:44:29 +0900 | [diff] [blame] | 1963 | pthread_mutex_lock(&res_cache_list_lock); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1964 | |
cken | b1a69a4 | 2018-12-01 17:45:18 +0900 | [diff] [blame] | 1965 | resolv_cache_info* info = find_cache_info_locked(netid); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1966 | if (info) { |
| 1967 | if (info->nscount > MAXNS) { |
Bernie Innocenti | 84ec88d | 2018-09-27 13:44:29 +0900 | [diff] [blame] | 1968 | pthread_mutex_unlock(&res_cache_list_lock); |
Bernie Innocenti | e9ba09c | 2018-09-12 23:20:10 +0900 | [diff] [blame] | 1969 | VLOG << __func__ << ": nscount " << info->nscount << " > MAXNS " << MAXNS; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1970 | errno = EFAULT; |
| 1971 | return -1; |
| 1972 | } |
| 1973 | int i; |
| 1974 | for (i = 0; i < info->nscount; i++) { |
| 1975 | // Verify that the following assumptions are held, failure indicates corruption: |
| 1976 | // - getaddrinfo() may never return a sockaddr > sockaddr_storage |
| 1977 | // - all addresses are valid |
| 1978 | // - there is only one address per addrinfo thanks to numeric resolution |
| 1979 | int addrlen = info->nsaddrinfo[i]->ai_addrlen; |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1980 | if (addrlen < (int) sizeof(struct sockaddr) || addrlen > (int) sizeof(servers[0])) { |
Bernie Innocenti | 84ec88d | 2018-09-27 13:44:29 +0900 | [diff] [blame] | 1981 | pthread_mutex_unlock(&res_cache_list_lock); |
Bernie Innocenti | e9ba09c | 2018-09-12 23:20:10 +0900 | [diff] [blame] | 1982 | VLOG << __func__ << ": nsaddrinfo[" << i << "].ai_addrlen == " << addrlen; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1983 | errno = EMSGSIZE; |
| 1984 | return -1; |
| 1985 | } |
| 1986 | if (info->nsaddrinfo[i]->ai_addr == NULL) { |
Bernie Innocenti | 84ec88d | 2018-09-27 13:44:29 +0900 | [diff] [blame] | 1987 | pthread_mutex_unlock(&res_cache_list_lock); |
Bernie Innocenti | e9ba09c | 2018-09-12 23:20:10 +0900 | [diff] [blame] | 1988 | VLOG << __func__ << ": nsaddrinfo[" << i << "].ai_addr == NULL"; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1989 | errno = ENOENT; |
| 1990 | return -1; |
| 1991 | } |
| 1992 | if (info->nsaddrinfo[i]->ai_next != NULL) { |
Bernie Innocenti | 84ec88d | 2018-09-27 13:44:29 +0900 | [diff] [blame] | 1993 | pthread_mutex_unlock(&res_cache_list_lock); |
Bernie Innocenti | e9ba09c | 2018-09-12 23:20:10 +0900 | [diff] [blame] | 1994 | VLOG << __func__ << ": nsaddrinfo[" << i << "].ai_next != NULL"; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1995 | errno = ENOTUNIQ; |
| 1996 | return -1; |
| 1997 | } |
| 1998 | } |
| 1999 | *nscount = info->nscount; |
| 2000 | for (i = 0; i < info->nscount; i++) { |
| 2001 | memcpy(&servers[i], info->nsaddrinfo[i]->ai_addr, info->nsaddrinfo[i]->ai_addrlen); |
| 2002 | stats[i] = info->nsstats[i]; |
| 2003 | } |
| 2004 | for (i = 0; i < MAXDNSRCH; i++) { |
| 2005 | const char* cur_domain = info->defdname + info->dnsrch_offset[i]; |
| 2006 | // dnsrch_offset[i] can either be -1 or point to an empty string to indicate the end |
| 2007 | // of the search offsets. Checking for < 0 is not strictly necessary, but safer. |
| 2008 | // TODO: Pass in a search domain array instead of a string to |
Bernie Innocenti | 189eb50 | 2018-10-01 23:10:18 +0900 | [diff] [blame] | 2009 | // resolv_set_nameservers_for_net() and make this double check unnecessary. |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 2010 | if (info->dnsrch_offset[i] < 0 || |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 2011 | ((size_t) info->dnsrch_offset[i]) >= sizeof(info->defdname) || !cur_domain[0]) { |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 2012 | break; |
| 2013 | } |
| 2014 | strlcpy(domains[i], cur_domain, MAXDNSRCHPATH); |
| 2015 | } |
| 2016 | *dcount = i; |
| 2017 | *params = info->params; |
| 2018 | revision_id = info->revision_id; |
| 2019 | } |
| 2020 | |
Bernie Innocenti | 84ec88d | 2018-09-27 13:44:29 +0900 | [diff] [blame] | 2021 | pthread_mutex_unlock(&res_cache_list_lock); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 2022 | return revision_id; |
| 2023 | } |
| 2024 | |
Bernie Innocenti | 189eb50 | 2018-10-01 23:10:18 +0900 | [diff] [blame] | 2025 | int resolv_cache_get_resolver_stats(unsigned netid, __res_params* params, res_stats stats[MAXNS]) { |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 2026 | int revision_id = -1; |
Bernie Innocenti | 84ec88d | 2018-09-27 13:44:29 +0900 | [diff] [blame] | 2027 | pthread_mutex_lock(&res_cache_list_lock); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 2028 | |
cken | b1a69a4 | 2018-12-01 17:45:18 +0900 | [diff] [blame] | 2029 | resolv_cache_info* info = find_cache_info_locked(netid); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 2030 | if (info) { |
| 2031 | memcpy(stats, info->nsstats, sizeof(info->nsstats)); |
| 2032 | *params = info->params; |
| 2033 | revision_id = info->revision_id; |
| 2034 | } |
| 2035 | |
Bernie Innocenti | 84ec88d | 2018-09-27 13:44:29 +0900 | [diff] [blame] | 2036 | pthread_mutex_unlock(&res_cache_list_lock); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 2037 | return revision_id; |
| 2038 | } |
| 2039 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 2040 | void _resolv_cache_add_resolver_stats_sample(unsigned netid, int revision_id, int ns, |
Bernie Innocenti | 189eb50 | 2018-10-01 23:10:18 +0900 | [diff] [blame] | 2041 | const res_sample* sample, int max_samples) { |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 2042 | if (max_samples <= 0) return; |
| 2043 | |
Bernie Innocenti | 84ec88d | 2018-09-27 13:44:29 +0900 | [diff] [blame] | 2044 | pthread_mutex_lock(&res_cache_list_lock); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 2045 | |
cken | b1a69a4 | 2018-12-01 17:45:18 +0900 | [diff] [blame] | 2046 | resolv_cache_info* info = find_cache_info_locked(netid); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 2047 | |
| 2048 | if (info && info->revision_id == revision_id) { |
| 2049 | _res_cache_add_stats_sample_locked(&info->nsstats[ns], sample, max_samples); |
| 2050 | } |
| 2051 | |
Bernie Innocenti | 84ec88d | 2018-09-27 13:44:29 +0900 | [diff] [blame] | 2052 | pthread_mutex_unlock(&res_cache_list_lock); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 2053 | } |