Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1 | /* $NetBSD: res_send.c,v 1.9 2006/01/24 17:41:25 christos Exp $ */ |
| 2 | |
| 3 | /* |
| 4 | * Copyright (c) 1985, 1989, 1993 |
| 5 | * The Regents of the University of California. All rights reserved. |
| 6 | * |
| 7 | * Redistribution and use in source and binary forms, with or without |
| 8 | * modification, are permitted provided that the following conditions |
| 9 | * are met: |
| 10 | * 1. Redistributions of source code must retain the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer. |
| 12 | * 2. Redistributions in binary form must reproduce the above copyright |
| 13 | * notice, this list of conditions and the following disclaimer in the |
| 14 | * documentation and/or other materials provided with the distribution. |
| 15 | * 3. All advertising materials mentioning features or use of this software |
| 16 | * must display the following acknowledgement: |
| 17 | * This product includes software developed by the University of |
| 18 | * California, Berkeley and its contributors. |
| 19 | * 4. Neither the name of the University nor the names of its contributors |
| 20 | * may be used to endorse or promote products derived from this software |
| 21 | * without specific prior written permission. |
| 22 | * |
| 23 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
| 24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 25 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 26 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
| 27 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 28 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 29 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 30 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 31 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 32 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 33 | * SUCH DAMAGE. |
| 34 | */ |
| 35 | |
| 36 | /* |
| 37 | * Portions Copyright (c) 1993 by Digital Equipment Corporation. |
| 38 | * |
| 39 | * Permission to use, copy, modify, and distribute this software for any |
| 40 | * purpose with or without fee is hereby granted, provided that the above |
| 41 | * copyright notice and this permission notice appear in all copies, and that |
| 42 | * the name of Digital Equipment Corporation not be used in advertising or |
| 43 | * publicity pertaining to distribution of the document or software without |
| 44 | * specific, written prior permission. |
| 45 | * |
| 46 | * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL |
| 47 | * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES |
| 48 | * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT |
| 49 | * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL |
| 50 | * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR |
| 51 | * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS |
| 52 | * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS |
| 53 | * SOFTWARE. |
| 54 | */ |
| 55 | |
| 56 | /* |
| 57 | * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC") |
| 58 | * Portions Copyright (c) 1996-1999 by Internet Software Consortium. |
| 59 | * |
| 60 | * Permission to use, copy, modify, and distribute this software for any |
| 61 | * purpose with or without fee is hereby granted, provided that the above |
| 62 | * copyright notice and this permission notice appear in all copies. |
| 63 | * |
| 64 | * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES |
| 65 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 66 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR |
| 67 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 68 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 69 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT |
| 70 | * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 71 | */ |
| 72 | |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 73 | /* |
| 74 | * Send query to name server and wait for reply. |
| 75 | */ |
| 76 | |
Ken Chen | 5471dca | 2019-04-15 15:25:35 +0800 | [diff] [blame] | 77 | #define LOG_TAG "resolv" |
Bernie Innocenti | 9f05f5e | 2018-09-12 23:20:10 +0900 | [diff] [blame] | 78 | |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 79 | #include <sys/param.h> |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 80 | #include <sys/socket.h> |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 81 | #include <sys/time.h> |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 82 | #include <sys/uio.h> |
| 83 | |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 84 | #include <arpa/inet.h> |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 85 | #include <arpa/nameser.h> |
| 86 | #include <netinet/in.h> |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 87 | |
| 88 | #include <errno.h> |
| 89 | #include <fcntl.h> |
| 90 | #include <netdb.h> |
| 91 | #include <poll.h> |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 92 | #include <signal.h> |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 93 | #include <stdlib.h> |
| 94 | #include <string.h> |
| 95 | #include <time.h> |
| 96 | #include <unistd.h> |
| 97 | |
Bernie Innocenti | 9f05f5e | 2018-09-12 23:20:10 +0900 | [diff] [blame] | 98 | #include <android-base/logging.h> |
Bernie Innocenti | 758005f | 2019-02-19 18:08:36 +0900 | [diff] [blame] | 99 | #include <android/multinetwork.h> // ResNsendFlags |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 100 | |
Mike Yu | b601ff7 | 2018-11-01 20:07:00 +0800 | [diff] [blame] | 101 | #include <netdutils/Slice.h> |
lifr | 9498178 | 2019-05-17 21:15:19 +0800 | [diff] [blame] | 102 | #include <netdutils/Stopwatch.h> |
Bernie Innocenti | ec4219b | 2019-01-30 11:16:36 +0900 | [diff] [blame] | 103 | #include "DnsTlsDispatcher.h" |
| 104 | #include "DnsTlsTransport.h" |
| 105 | #include "PrivateDnsConfiguration.h" |
Bernie Innocenti | ac18b12 | 2018-10-01 23:10:18 +0900 | [diff] [blame] | 106 | #include "netd_resolv/resolv.h" |
| 107 | #include "netd_resolv/stats.h" |
Bernie Innocenti | f33a63f | 2018-08-30 12:04:03 +0900 | [diff] [blame] | 108 | #include "private/android_filesystem_config.h" |
Mike Yu | 34433c6 | 2019-08-20 20:17:36 +0800 | [diff] [blame] | 109 | #include "res_debug.h" |
Bernie Innocenti | 0848711 | 2019-10-11 21:14:13 +0900 | [diff] [blame] | 110 | #include "res_init.h" |
Bernie Innocenti | f33a63f | 2018-08-30 12:04:03 +0900 | [diff] [blame] | 111 | #include "resolv_cache.h" |
lifr | 9498178 | 2019-05-17 21:15:19 +0800 | [diff] [blame] | 112 | #include "stats.pb.h" |
Bernie Innocenti | f33a63f | 2018-08-30 12:04:03 +0900 | [diff] [blame] | 113 | |
Mike Yu | b601ff7 | 2018-11-01 20:07:00 +0800 | [diff] [blame] | 114 | // TODO: use the namespace something like android::netd_resolv for libnetd_resolv |
chenbruce | 99cbfd8 | 2019-09-05 15:08:13 +0800 | [diff] [blame] | 115 | using android::net::CacheStatus; |
| 116 | using android::net::DnsQueryEvent; |
| 117 | using android::net::DnsTlsDispatcher; |
| 118 | using android::net::DnsTlsTransport; |
| 119 | using android::net::gPrivateDnsConfiguration; |
| 120 | using android::net::IpVersion; |
| 121 | using android::net::IV_IPV4; |
| 122 | using android::net::IV_IPV6; |
| 123 | using android::net::IV_UNKNOWN; |
lifr | 9498178 | 2019-05-17 21:15:19 +0800 | [diff] [blame] | 124 | using android::net::NetworkDnsEventReported; |
chenbruce | 99cbfd8 | 2019-09-05 15:08:13 +0800 | [diff] [blame] | 125 | using android::net::NS_T_INVALID; |
| 126 | using android::net::NsRcode; |
| 127 | using android::net::NsType; |
| 128 | using android::net::PrivateDnsMode; |
| 129 | using android::net::PrivateDnsModes; |
| 130 | using android::net::PrivateDnsStatus; |
| 131 | using android::net::PROTO_TCP; |
| 132 | using android::net::PROTO_UDP; |
Bernie Innocenti | ec4219b | 2019-01-30 11:16:36 +0900 | [diff] [blame] | 133 | using android::netdutils::Slice; |
lifr | 9498178 | 2019-05-17 21:15:19 +0800 | [diff] [blame] | 134 | using android::netdutils::Stopwatch; |
Bernie Innocenti | 9f05f5e | 2018-09-12 23:20:10 +0900 | [diff] [blame] | 135 | |
Mike Yu | b601ff7 | 2018-11-01 20:07:00 +0800 | [diff] [blame] | 136 | static DnsTlsDispatcher sDnsTlsDispatcher; |
| 137 | |
Bernie Innocenti | 9c57593 | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 138 | static int get_salen(const struct sockaddr*); |
| 139 | static struct sockaddr* get_nsaddr(res_state, size_t); |
chenbruce | c51f121 | 2019-09-12 16:59:33 +0800 | [diff] [blame] | 140 | static int send_vc(res_state, res_params* params, const uint8_t*, int, uint8_t*, int, int*, int, |
Bernie Innocenti | 758005f | 2019-02-19 18:08:36 +0900 | [diff] [blame] | 141 | time_t*, int*, int*); |
chenbruce | c51f121 | 2019-09-12 16:59:33 +0800 | [diff] [blame] | 142 | static int send_dg(res_state, res_params* params, const uint8_t*, int, uint8_t*, int, int*, int, |
| 143 | int*, int*, time_t*, int*, int*); |
chenbruce | 018fdb2 | 2019-06-12 18:08:04 +0800 | [diff] [blame] | 144 | static void dump_error(const char*, const struct sockaddr*, int); |
chenbruce | acb832c | 2019-02-20 19:45:50 +0800 | [diff] [blame] | 145 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 146 | static int sock_eq(struct sockaddr*, struct sockaddr*); |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 147 | static int connect_with_timeout(int sock, const struct sockaddr* nsap, socklen_t salen, |
| 148 | const struct timespec timeout); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 149 | static int retrying_poll(const int sock, short events, const struct timespec* finish); |
Luke Huang | d7aa7b4 | 2018-11-21 20:37:50 +0800 | [diff] [blame] | 150 | static int res_tls_send(res_state, const Slice query, const Slice answer, int* rcode, |
Mike Yu | bfb1b34 | 2018-11-06 15:42:36 +0800 | [diff] [blame] | 151 | bool* fallback); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 152 | |
lifr | 9498178 | 2019-05-17 21:15:19 +0800 | [diff] [blame] | 153 | NsType getQueryType(const uint8_t* msg, size_t msgLen) { |
| 154 | ns_msg handle; |
| 155 | ns_rr rr; |
| 156 | if (ns_initparse((const uint8_t*)msg, msgLen, &handle) < 0 || |
| 157 | ns_parserr(&handle, ns_s_qd, 0, &rr) < 0) { |
| 158 | return NS_T_INVALID; |
| 159 | } |
| 160 | return static_cast<NsType>(ns_rr_type(rr)); |
| 161 | } |
| 162 | |
| 163 | IpVersion ipFamilyToIPVersion(const int ipFamily) { |
| 164 | switch (ipFamily) { |
| 165 | case AF_INET: |
| 166 | return IV_IPV4; |
| 167 | case AF_INET6: |
| 168 | return IV_IPV6; |
| 169 | default: |
| 170 | return IV_UNKNOWN; |
| 171 | } |
| 172 | } |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 173 | |
Bernie Innocenti | afaacf7 | 2018-08-30 07:34:37 +0900 | [diff] [blame] | 174 | // BEGIN: Code copied from ISC eventlib |
| 175 | // TODO: move away from this code |
Bernie Innocenti | afaacf7 | 2018-08-30 07:34:37 +0900 | [diff] [blame] | 176 | #define BILLION 1000000000 |
| 177 | |
| 178 | static struct timespec evConsTime(time_t sec, long nsec) { |
| 179 | struct timespec x; |
| 180 | |
| 181 | x.tv_sec = sec; |
| 182 | x.tv_nsec = nsec; |
| 183 | return (x); |
| 184 | } |
| 185 | |
| 186 | static struct timespec evAddTime(struct timespec addend1, struct timespec addend2) { |
| 187 | struct timespec x; |
| 188 | |
| 189 | x.tv_sec = addend1.tv_sec + addend2.tv_sec; |
| 190 | x.tv_nsec = addend1.tv_nsec + addend2.tv_nsec; |
| 191 | if (x.tv_nsec >= BILLION) { |
| 192 | x.tv_sec++; |
| 193 | x.tv_nsec -= BILLION; |
| 194 | } |
| 195 | return (x); |
| 196 | } |
| 197 | |
| 198 | static struct timespec evSubTime(struct timespec minuend, struct timespec subtrahend) { |
| 199 | struct timespec x; |
| 200 | |
| 201 | x.tv_sec = minuend.tv_sec - subtrahend.tv_sec; |
| 202 | if (minuend.tv_nsec >= subtrahend.tv_nsec) |
| 203 | x.tv_nsec = minuend.tv_nsec - subtrahend.tv_nsec; |
| 204 | else { |
| 205 | x.tv_nsec = BILLION - subtrahend.tv_nsec + minuend.tv_nsec; |
| 206 | x.tv_sec--; |
| 207 | } |
| 208 | return (x); |
| 209 | } |
| 210 | |
| 211 | static int evCmpTime(struct timespec a, struct timespec b) { |
| 212 | #define SGN(x) ((x) < 0 ? (-1) : (x) > 0 ? (1) : (0)); |
| 213 | time_t s = a.tv_sec - b.tv_sec; |
| 214 | long n; |
| 215 | |
| 216 | if (s != 0) return SGN(s); |
| 217 | |
| 218 | n = a.tv_nsec - b.tv_nsec; |
| 219 | return SGN(n); |
| 220 | } |
| 221 | |
Bernie Innocenti | afaacf7 | 2018-08-30 07:34:37 +0900 | [diff] [blame] | 222 | static struct timespec evNowTime(void) { |
Bernie Innocenti | afaacf7 | 2018-08-30 07:34:37 +0900 | [diff] [blame] | 223 | struct timespec tsnow; |
Bernie Innocenti | c183482 | 2018-08-31 16:11:41 +0900 | [diff] [blame] | 224 | clock_gettime(CLOCK_REALTIME, &tsnow); |
| 225 | return tsnow; |
Bernie Innocenti | afaacf7 | 2018-08-30 07:34:37 +0900 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | static struct iovec evConsIovec(void* buf, size_t cnt) { |
| 229 | struct iovec ret; |
| 230 | |
| 231 | memset(&ret, 0xf5, sizeof ret); |
| 232 | ret.iov_base = buf; |
| 233 | ret.iov_len = cnt; |
Bernie Innocenti | c183482 | 2018-08-31 16:11:41 +0900 | [diff] [blame] | 234 | return ret; |
Bernie Innocenti | afaacf7 | 2018-08-30 07:34:37 +0900 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | // END: Code copied from ISC eventlib |
| 238 | |
lifr | 9498178 | 2019-05-17 21:15:19 +0800 | [diff] [blame] | 239 | /* BIONIC-BEGIN: implement source port randomization */ |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 240 | static int random_bind(int s, int family) { |
nuccachen | b980f2f | 2018-10-23 17:10:58 +0800 | [diff] [blame] | 241 | sockaddr_union u; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 242 | int j; |
| 243 | socklen_t slen; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 244 | |
| 245 | /* clear all, this also sets the IP4/6 address to 'any' */ |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 246 | memset(&u, 0, sizeof u); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 247 | |
| 248 | switch (family) { |
| 249 | case AF_INET: |
| 250 | u.sin.sin_family = family; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 251 | slen = sizeof u.sin; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 252 | break; |
| 253 | case AF_INET6: |
| 254 | u.sin6.sin6_family = family; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 255 | slen = sizeof u.sin6; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 256 | break; |
| 257 | default: |
| 258 | errno = EPROTO; |
| 259 | return -1; |
| 260 | } |
| 261 | |
| 262 | /* first try to bind to a random source port a few times */ |
| 263 | for (j = 0; j < 10; j++) { |
| 264 | /* find a random port between 1025 .. 65534 */ |
Luke Huang | dda920f | 2019-02-13 14:05:02 +0800 | [diff] [blame] | 265 | int port = 1025 + (arc4random_uniform(65535 - 1025)); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 266 | if (family == AF_INET) |
| 267 | u.sin.sin_port = htons(port); |
| 268 | else |
| 269 | u.sin6.sin6_port = htons(port); |
| 270 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 271 | if (!bind(s, &u.sa, slen)) return 0; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 272 | } |
| 273 | |
Bernie Innocenti | 9c57593 | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 274 | // nothing after 10 attempts, our network table is probably busy |
| 275 | // let the system decide which port is best |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 276 | if (family == AF_INET) |
| 277 | u.sin.sin_port = 0; |
| 278 | else |
| 279 | u.sin6.sin6_port = 0; |
| 280 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 281 | return bind(s, &u.sa, slen); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 282 | } |
| 283 | /* BIONIC-END */ |
| 284 | |
Luke Huang | 70931aa | 2019-01-31 11:57:41 +0800 | [diff] [blame] | 285 | // Disables all nameservers other than selectedServer |
| 286 | static void res_set_usable_server(int selectedServer, int nscount, bool usable_servers[]) { |
| 287 | int usableIndex = 0; |
| 288 | for (int ns = 0; ns < nscount; ns++) { |
| 289 | if (usable_servers[ns]) ++usableIndex; |
| 290 | if (usableIndex != selectedServer) usable_servers[ns] = false; |
| 291 | } |
| 292 | } |
| 293 | |
Bernie Innocenti | a6f40ca | 2019-10-09 15:22:41 +0900 | [diff] [blame] | 294 | // Looks up the nameserver address in res.nsaddrs[], returns true if found, otherwise false. |
| 295 | static bool res_ourserver_p(res_state statp, const sockaddr* sa) { |
Bernie Innocenti | 2e8540b | 2018-09-26 11:52:04 +0900 | [diff] [blame] | 296 | const sockaddr_in *inp, *srv; |
| 297 | const sockaddr_in6 *in6p, *srv6; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 298 | int ns; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 299 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 300 | switch (sa->sa_family) { |
| 301 | case AF_INET: |
| 302 | inp = (const struct sockaddr_in*) (const void*) sa; |
| 303 | for (ns = 0; ns < statp->nscount; ns++) { |
| 304 | srv = (struct sockaddr_in*) (void*) get_nsaddr(statp, (size_t) ns); |
| 305 | if (srv->sin_family == inp->sin_family && srv->sin_port == inp->sin_port && |
| 306 | (srv->sin_addr.s_addr == INADDR_ANY || |
| 307 | srv->sin_addr.s_addr == inp->sin_addr.s_addr)) |
Bernie Innocenti | a6f40ca | 2019-10-09 15:22:41 +0900 | [diff] [blame] | 308 | return true; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 309 | } |
| 310 | break; |
| 311 | case AF_INET6: |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 312 | in6p = (const struct sockaddr_in6*) (const void*) sa; |
| 313 | for (ns = 0; ns < statp->nscount; ns++) { |
| 314 | srv6 = (struct sockaddr_in6*) (void*) get_nsaddr(statp, (size_t) ns); |
| 315 | if (srv6->sin6_family == in6p->sin6_family && srv6->sin6_port == in6p->sin6_port && |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 316 | #ifdef HAVE_SIN6_SCOPE_ID |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 317 | (srv6->sin6_scope_id == 0 || srv6->sin6_scope_id == in6p->sin6_scope_id) && |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 318 | #endif |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 319 | (IN6_IS_ADDR_UNSPECIFIED(&srv6->sin6_addr) || |
| 320 | IN6_ARE_ADDR_EQUAL(&srv6->sin6_addr, &in6p->sin6_addr))) |
Bernie Innocenti | a6f40ca | 2019-10-09 15:22:41 +0900 | [diff] [blame] | 321 | return true; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 322 | } |
| 323 | break; |
| 324 | default: |
| 325 | break; |
| 326 | } |
Bernie Innocenti | a6f40ca | 2019-10-09 15:22:41 +0900 | [diff] [blame] | 327 | return false; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 328 | } |
| 329 | |
| 330 | /* int |
Bernie Innocenti | 9c57593 | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 331 | * res_nameinquery(name, type, cl, buf, eom) |
| 332 | * look for (name, type, cl) in the query section of packet (buf, eom) |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 333 | * requires: |
| 334 | * buf + HFIXEDSZ <= eom |
| 335 | * returns: |
| 336 | * -1 : format error |
| 337 | * 0 : not found |
| 338 | * >0 : found |
| 339 | * author: |
| 340 | * paul vixie, 29may94 |
| 341 | */ |
chenbruce | c51f121 | 2019-09-12 16:59:33 +0800 | [diff] [blame] | 342 | int res_nameinquery(const char* name, int type, int cl, const uint8_t* buf, const uint8_t* eom) { |
| 343 | const uint8_t* cp = buf + HFIXEDSZ; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 344 | int qdcount = ntohs(((const HEADER*) (const void*) buf)->qdcount); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 345 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 346 | while (qdcount-- > 0) { |
| 347 | char tname[MAXDNAME + 1]; |
Bernie Innocenti | 9c57593 | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 348 | int n = dn_expand(buf, eom, cp, tname, sizeof tname); |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 349 | if (n < 0) return (-1); |
| 350 | cp += n; |
| 351 | if (cp + 2 * INT16SZ > eom) return (-1); |
chenbruce | 0d47042 | 2019-03-28 18:44:37 +0800 | [diff] [blame] | 352 | int ttype = ntohs(*reinterpret_cast<const uint16_t*>(cp)); |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 353 | cp += INT16SZ; |
chenbruce | 0d47042 | 2019-03-28 18:44:37 +0800 | [diff] [blame] | 354 | int tclass = ntohs(*reinterpret_cast<const uint16_t*>(cp)); |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 355 | cp += INT16SZ; |
Bernie Innocenti | 9c57593 | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 356 | if (ttype == type && tclass == cl && ns_samename(tname, name) == 1) return (1); |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 357 | } |
| 358 | return (0); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 359 | } |
| 360 | |
| 361 | /* int |
| 362 | * res_queriesmatch(buf1, eom1, buf2, eom2) |
| 363 | * is there a 1:1 mapping of (name,type,class) |
| 364 | * in (buf1,eom1) and (buf2,eom2)? |
| 365 | * returns: |
| 366 | * -1 : format error |
| 367 | * 0 : not a 1:1 mapping |
| 368 | * >0 : is a 1:1 mapping |
| 369 | * author: |
| 370 | * paul vixie, 29may94 |
| 371 | */ |
chenbruce | c51f121 | 2019-09-12 16:59:33 +0800 | [diff] [blame] | 372 | int res_queriesmatch(const uint8_t* buf1, const uint8_t* eom1, const uint8_t* buf2, |
| 373 | const uint8_t* eom2) { |
| 374 | const uint8_t* cp = buf1 + HFIXEDSZ; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 375 | int qdcount = ntohs(((const HEADER*) (const void*) buf1)->qdcount); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 376 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 377 | if (buf1 + HFIXEDSZ > eom1 || buf2 + HFIXEDSZ > eom2) return (-1); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 378 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 379 | /* |
| 380 | * Only header section present in replies to |
| 381 | * dynamic update packets. |
| 382 | */ |
| 383 | if ((((const HEADER*) (const void*) buf1)->opcode == ns_o_update) && |
| 384 | (((const HEADER*) (const void*) buf2)->opcode == ns_o_update)) |
| 385 | return (1); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 386 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 387 | if (qdcount != ntohs(((const HEADER*) (const void*) buf2)->qdcount)) return (0); |
| 388 | while (qdcount-- > 0) { |
| 389 | char tname[MAXDNAME + 1]; |
Bernie Innocenti | 9c57593 | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 390 | int n = dn_expand(buf1, eom1, cp, tname, sizeof tname); |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 391 | if (n < 0) return (-1); |
| 392 | cp += n; |
| 393 | if (cp + 2 * INT16SZ > eom1) return (-1); |
chenbruce | 0d47042 | 2019-03-28 18:44:37 +0800 | [diff] [blame] | 394 | int ttype = ntohs(*reinterpret_cast<const uint16_t*>(cp)); |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 395 | cp += INT16SZ; |
chenbruce | 0d47042 | 2019-03-28 18:44:37 +0800 | [diff] [blame] | 396 | int tclass = ntohs(*reinterpret_cast<const uint16_t*>(cp)); |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 397 | cp += INT16SZ; |
| 398 | if (!res_nameinquery(tname, ttype, tclass, buf2, eom2)) return (0); |
| 399 | } |
| 400 | return (1); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 401 | } |
| 402 | |
lifr | 9498178 | 2019-05-17 21:15:19 +0800 | [diff] [blame] | 403 | static DnsQueryEvent* addDnsQueryEvent(NetworkDnsEventReported* event) { |
| 404 | return event->mutable_dns_query_events()->add_dns_query_event(); |
| 405 | } |
| 406 | |
chenbruce | c51f121 | 2019-09-12 16:59:33 +0800 | [diff] [blame] | 407 | int res_nsend(res_state statp, const uint8_t* buf, int buflen, uint8_t* ans, int anssiz, int* rcode, |
Luke Huang | ba7bef9 | 2018-12-26 16:53:03 +0800 | [diff] [blame] | 408 | uint32_t flags) { |
Bernie Innocenti | 9c57593 | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 409 | int gotsomewhere, terrno, v_circuit, resplen, n; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 410 | ResolvCacheStatus cache_status = RESOLV_CACHE_UNSUPPORTED; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 411 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 412 | if (anssiz < HFIXEDSZ) { |
Luke Huang | aa6e5ed | 2018-12-03 15:37:28 +0800 | [diff] [blame] | 413 | // TODO: Remove errno once callers stop using it |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 414 | errno = EINVAL; |
Luke Huang | aa6e5ed | 2018-12-03 15:37:28 +0800 | [diff] [blame] | 415 | return -EINVAL; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 416 | } |
Bernie Innocenti | 3952ccc | 2019-03-03 19:39:53 +0900 | [diff] [blame] | 417 | LOG(DEBUG) << __func__; |
chenbruce | dd21072 | 2019-03-06 13:52:43 +0800 | [diff] [blame] | 418 | res_pquery(buf, buflen); |
chenbruce | acb832c | 2019-02-20 19:45:50 +0800 | [diff] [blame] | 419 | |
chenbruce | 018fdb2 | 2019-06-12 18:08:04 +0800 | [diff] [blame] | 420 | v_circuit = buflen > PACKETSZ; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 421 | gotsomewhere = 0; |
| 422 | terrno = ETIMEDOUT; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 423 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 424 | int anslen = 0; |
lifr | d4d9fbb | 2019-07-31 20:18:35 +0800 | [diff] [blame] | 425 | Stopwatch cacheStopwatch; |
Mike Yu | d1ec254 | 2019-06-06 17:17:53 +0800 | [diff] [blame] | 426 | cache_status = resolv_cache_lookup(statp->netid, buf, buflen, ans, anssiz, &anslen, flags); |
lifr | d4d9fbb | 2019-07-31 20:18:35 +0800 | [diff] [blame] | 427 | const int32_t cacheLatencyUs = saturate_cast<int32_t>(cacheStopwatch.timeTakenUs()); |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 428 | if (cache_status == RESOLV_CACHE_FOUND) { |
Luke Huang | 83c9b4b | 2019-02-27 18:18:01 +0800 | [diff] [blame] | 429 | HEADER* hp = (HEADER*)(void*)ans; |
| 430 | *rcode = hp->rcode; |
lifr | 9498178 | 2019-05-17 21:15:19 +0800 | [diff] [blame] | 431 | DnsQueryEvent* dnsQueryEvent = addDnsQueryEvent(statp->event); |
| 432 | dnsQueryEvent->set_latency_micros(cacheLatencyUs); |
| 433 | dnsQueryEvent->set_cache_hit(static_cast<CacheStatus>(cache_status)); |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 434 | return anslen; |
| 435 | } else if (cache_status != RESOLV_CACHE_UNSUPPORTED) { |
| 436 | // had a cache miss for a known network, so populate the thread private |
| 437 | // data so the normal resolve path can do its thing |
| 438 | _resolv_populate_res_for_net(statp); |
| 439 | } |
| 440 | if (statp->nscount == 0) { |
| 441 | // We have no nameservers configured, so there's no point trying. |
| 442 | // Tell the cache the query failed, or any retries and anyone else asking the same |
| 443 | // question will block for PENDING_REQUEST_TIMEOUT seconds instead of failing fast. |
Luke Huang | ba7bef9 | 2018-12-26 16:53:03 +0800 | [diff] [blame] | 444 | _resolv_cache_query_failed(statp->netid, buf, buflen, flags); |
Luke Huang | aa6e5ed | 2018-12-03 15:37:28 +0800 | [diff] [blame] | 445 | |
| 446 | // TODO: Remove errno once callers stop using it |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 447 | errno = ESRCH; |
Luke Huang | aa6e5ed | 2018-12-03 15:37:28 +0800 | [diff] [blame] | 448 | return -ESRCH; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 449 | } |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 450 | |
Bernie Innocenti | 758005f | 2019-02-19 18:08:36 +0900 | [diff] [blame] | 451 | res_stats stats[MAXNS]; |
| 452 | res_params params; |
waynema | 0161acd | 2019-02-18 17:47:59 +0800 | [diff] [blame] | 453 | int revision_id = resolv_cache_get_resolver_stats(statp->netid, ¶ms, stats); |
lifr | a1b37f1 | 2019-02-13 17:33:57 +0800 | [diff] [blame] | 454 | if (revision_id < 0) { |
| 455 | // TODO: Remove errno once callers stop using it |
| 456 | errno = ESRCH; |
| 457 | return -ESRCH; |
| 458 | } |
waynema | 0161acd | 2019-02-18 17:47:59 +0800 | [diff] [blame] | 459 | bool usable_servers[MAXNS]; |
Luke Huang | 70931aa | 2019-01-31 11:57:41 +0800 | [diff] [blame] | 460 | int usableServersCount = android_net_res_stats_get_usable_servers( |
| 461 | ¶ms, stats, statp->nscount, usable_servers); |
| 462 | |
| 463 | if ((flags & ANDROID_RESOLV_NO_RETRY) && usableServersCount > 1) { |
| 464 | auto hp = reinterpret_cast<const HEADER*>(buf); |
| 465 | |
| 466 | // Select a random server based on the query id |
| 467 | int selectedServer = (hp->id % usableServersCount) + 1; |
| 468 | res_set_usable_server(selectedServer, statp->nscount, usable_servers); |
| 469 | } |
waynema | 0161acd | 2019-02-18 17:47:59 +0800 | [diff] [blame] | 470 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 471 | /* |
| 472 | * Send request, RETRY times, or until successful. |
| 473 | */ |
waynema | a74195e | 2019-01-18 14:02:31 +0800 | [diff] [blame] | 474 | int retryTimes = (flags & ANDROID_RESOLV_NO_RETRY) ? 1 : params.retry_count; |
Luke Huang | ba7bef9 | 2018-12-26 16:53:03 +0800 | [diff] [blame] | 475 | |
| 476 | for (int attempt = 0; attempt < retryTimes; ++attempt) { |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 477 | |
Bernie Innocenti | 9c57593 | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 478 | for (int ns = 0; ns < statp->nscount; ns++) { |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 479 | if (!usable_servers[ns]) continue; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 480 | int nsaplen; |
| 481 | time_t now = 0; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 482 | int delay = 0; |
Mike Yu | bfb1b34 | 2018-11-06 15:42:36 +0800 | [diff] [blame] | 483 | *rcode = RCODE_INTERNAL_ERROR; |
lifr | 9498178 | 2019-05-17 21:15:19 +0800 | [diff] [blame] | 484 | const sockaddr* nsap = get_nsaddr(statp, ns); |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 485 | nsaplen = get_salen(nsap); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 486 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 487 | same_ns: |
Mike Yu | bfb1b34 | 2018-11-06 15:42:36 +0800 | [diff] [blame] | 488 | // TODO: Since we expect there is only one DNS server being queried here while this |
| 489 | // function tries to query all of private DNS servers. Consider moving it to other |
| 490 | // reasonable place. In addition, maybe add stats for private DNS. |
chenbruce | d8cbb9b | 2019-06-20 18:25:28 +0800 | [diff] [blame] | 491 | if (!(statp->netcontext_flags & NET_CONTEXT_FLAG_USE_LOCAL_NAMESERVERS)) { |
Mike Yu | bfb1b34 | 2018-11-06 15:42:36 +0800 | [diff] [blame] | 492 | bool fallback = false; |
chenbruce | c51f121 | 2019-09-12 16:59:33 +0800 | [diff] [blame] | 493 | resplen = res_tls_send(statp, Slice(const_cast<uint8_t*>(buf), buflen), |
Mike Yu | bfb1b34 | 2018-11-06 15:42:36 +0800 | [diff] [blame] | 494 | Slice(ans, anssiz), rcode, &fallback); |
| 495 | if (resplen > 0) { |
Mike Yu | 34433c6 | 2019-08-20 20:17:36 +0800 | [diff] [blame] | 496 | LOG(DEBUG) << __func__ << ": got answer from DoT"; |
| 497 | res_pquery(ans, resplen); |
Mike Yu | bfb1b34 | 2018-11-06 15:42:36 +0800 | [diff] [blame] | 498 | if (cache_status == RESOLV_CACHE_NOTFOUND) { |
Mike Yu | d1ec254 | 2019-06-06 17:17:53 +0800 | [diff] [blame] | 499 | resolv_cache_add(statp->netid, buf, buflen, ans, resplen); |
Mike Yu | bfb1b34 | 2018-11-06 15:42:36 +0800 | [diff] [blame] | 500 | } |
| 501 | return resplen; |
Mike Yu | b601ff7 | 2018-11-01 20:07:00 +0800 | [diff] [blame] | 502 | } |
Mike Yu | bfb1b34 | 2018-11-06 15:42:36 +0800 | [diff] [blame] | 503 | if (!fallback) { |
Luke Huang | ba7bef9 | 2018-12-26 16:53:03 +0800 | [diff] [blame] | 504 | _resolv_cache_query_failed(statp->netid, buf, buflen, flags); |
Luke Huang | aa6e5ed | 2018-12-03 15:37:28 +0800 | [diff] [blame] | 505 | res_nclose(statp); |
| 506 | return -terrno; |
Mike Yu | bfb1b34 | 2018-11-06 15:42:36 +0800 | [diff] [blame] | 507 | } |
| 508 | } |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 509 | |
lifr | 9498178 | 2019-05-17 21:15:19 +0800 | [diff] [blame] | 510 | static const int niflags = NI_NUMERICHOST | NI_NUMERICSERV; |
| 511 | char abuf[NI_MAXHOST]; |
| 512 | DnsQueryEvent* dnsQueryEvent = addDnsQueryEvent(statp->event); |
| 513 | dnsQueryEvent->set_cache_hit(static_cast<CacheStatus>(cache_status)); |
chenbruce | acb832c | 2019-02-20 19:45:50 +0800 | [diff] [blame] | 514 | |
| 515 | if (getnameinfo(nsap, (socklen_t)nsaplen, abuf, sizeof(abuf), NULL, 0, niflags) == 0) |
Ken Chen | ffc224a | 2019-03-19 17:41:28 +0800 | [diff] [blame] | 516 | LOG(DEBUG) << __func__ << ": Querying server (# " << ns + 1 |
| 517 | << ") address = " << abuf; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 518 | |
lifr | d4d9fbb | 2019-07-31 20:18:35 +0800 | [diff] [blame] | 519 | Stopwatch queryStopwatch; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 520 | if (v_circuit) { |
| 521 | /* Use VC; at most one attempt per server. */ |
Luke Huang | ba7bef9 | 2018-12-26 16:53:03 +0800 | [diff] [blame] | 522 | bool shouldRecordStats = (attempt == 0); |
| 523 | attempt = retryTimes; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 524 | |
Mike Yu | bfb1b34 | 2018-11-06 15:42:36 +0800 | [diff] [blame] | 525 | n = send_vc(statp, ¶ms, buf, buflen, ans, anssiz, &terrno, ns, &now, rcode, |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 526 | &delay); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 527 | |
lifr | 9498178 | 2019-05-17 21:15:19 +0800 | [diff] [blame] | 528 | dnsQueryEvent->set_latency_micros( |
lifr | d4d9fbb | 2019-07-31 20:18:35 +0800 | [diff] [blame] | 529 | saturate_cast<int32_t>(queryStopwatch.timeTakenUs())); |
lifr | 9498178 | 2019-05-17 21:15:19 +0800 | [diff] [blame] | 530 | dnsQueryEvent->set_dns_server_index(ns); |
| 531 | dnsQueryEvent->set_ip_version(ipFamilyToIPVersion(nsap->sa_family)); |
| 532 | dnsQueryEvent->set_retry_times(attempt); |
| 533 | dnsQueryEvent->set_rcode(static_cast<NsRcode>(*rcode)); |
| 534 | dnsQueryEvent->set_protocol(PROTO_TCP); |
| 535 | dnsQueryEvent->set_type(getQueryType(buf, buflen)); |
| 536 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 537 | /* |
| 538 | * Only record stats the first time we try a query. This ensures that |
| 539 | * queries that deterministically fail (e.g., a name that always returns |
| 540 | * SERVFAIL or times out) do not unduly affect the stats. |
| 541 | */ |
Luke Huang | ba7bef9 | 2018-12-26 16:53:03 +0800 | [diff] [blame] | 542 | if (shouldRecordStats) { |
Bernie Innocenti | ac18b12 | 2018-10-01 23:10:18 +0900 | [diff] [blame] | 543 | res_sample sample; |
Mike Yu | bfb1b34 | 2018-11-06 15:42:36 +0800 | [diff] [blame] | 544 | _res_stats_set_sample(&sample, now, *rcode, delay); |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 545 | _resolv_cache_add_resolver_stats_sample(statp->netid, revision_id, ns, &sample, |
| 546 | params.max_samples); |
| 547 | } |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 548 | |
Ken Chen | ffc224a | 2019-03-19 17:41:28 +0800 | [diff] [blame] | 549 | LOG(INFO) << __func__ << ": used send_vc " << n; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 550 | |
Luke Huang | aa6e5ed | 2018-12-03 15:37:28 +0800 | [diff] [blame] | 551 | if (n < 0) { |
Luke Huang | ba7bef9 | 2018-12-26 16:53:03 +0800 | [diff] [blame] | 552 | _resolv_cache_query_failed(statp->netid, buf, buflen, flags); |
Luke Huang | aa6e5ed | 2018-12-03 15:37:28 +0800 | [diff] [blame] | 553 | res_nclose(statp); |
| 554 | return -terrno; |
| 555 | }; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 556 | if (n == 0) goto next_ns; |
| 557 | resplen = n; |
| 558 | } else { |
| 559 | /* Use datagrams. */ |
Ken Chen | ffc224a | 2019-03-19 17:41:28 +0800 | [diff] [blame] | 560 | LOG(INFO) << __func__ << ": using send_dg"; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 561 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 562 | n = send_dg(statp, ¶ms, buf, buflen, ans, anssiz, &terrno, ns, &v_circuit, |
Mike Yu | bfb1b34 | 2018-11-06 15:42:36 +0800 | [diff] [blame] | 563 | &gotsomewhere, &now, rcode, &delay); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 564 | |
lifr | 9498178 | 2019-05-17 21:15:19 +0800 | [diff] [blame] | 565 | dnsQueryEvent->set_latency_micros( |
lifr | d4d9fbb | 2019-07-31 20:18:35 +0800 | [diff] [blame] | 566 | saturate_cast<int32_t>(queryStopwatch.timeTakenUs())); |
lifr | 9498178 | 2019-05-17 21:15:19 +0800 | [diff] [blame] | 567 | dnsQueryEvent->set_dns_server_index(ns); |
| 568 | dnsQueryEvent->set_ip_version(ipFamilyToIPVersion(nsap->sa_family)); |
| 569 | dnsQueryEvent->set_retry_times(attempt); |
| 570 | dnsQueryEvent->set_rcode(static_cast<NsRcode>(*rcode)); |
| 571 | dnsQueryEvent->set_protocol(PROTO_UDP); |
| 572 | dnsQueryEvent->set_type(getQueryType(buf, buflen)); |
| 573 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 574 | /* Only record stats the first time we try a query. See above. */ |
Bernie Innocenti | 9c57593 | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 575 | if (attempt == 0) { |
Bernie Innocenti | ac18b12 | 2018-10-01 23:10:18 +0900 | [diff] [blame] | 576 | res_sample sample; |
Mike Yu | bfb1b34 | 2018-11-06 15:42:36 +0800 | [diff] [blame] | 577 | _res_stats_set_sample(&sample, now, *rcode, delay); |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 578 | _resolv_cache_add_resolver_stats_sample(statp->netid, revision_id, ns, &sample, |
| 579 | params.max_samples); |
| 580 | } |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 581 | |
Ken Chen | ffc224a | 2019-03-19 17:41:28 +0800 | [diff] [blame] | 582 | LOG(INFO) << __func__ << ": used send_dg " << n; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 583 | |
Luke Huang | aa6e5ed | 2018-12-03 15:37:28 +0800 | [diff] [blame] | 584 | if (n < 0) { |
Luke Huang | ba7bef9 | 2018-12-26 16:53:03 +0800 | [diff] [blame] | 585 | _resolv_cache_query_failed(statp->netid, buf, buflen, flags); |
Luke Huang | aa6e5ed | 2018-12-03 15:37:28 +0800 | [diff] [blame] | 586 | res_nclose(statp); |
| 587 | return -terrno; |
| 588 | }; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 589 | if (n == 0) goto next_ns; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 590 | if (v_circuit) goto same_ns; |
| 591 | resplen = n; |
| 592 | } |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 593 | |
Ken Chen | ffc224a | 2019-03-19 17:41:28 +0800 | [diff] [blame] | 594 | LOG(DEBUG) << __func__ << ": got answer:"; |
chenbruce | dd21072 | 2019-03-06 13:52:43 +0800 | [diff] [blame] | 595 | res_pquery(ans, (resplen > anssiz) ? anssiz : resplen); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 596 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 597 | if (cache_status == RESOLV_CACHE_NOTFOUND) { |
Mike Yu | d1ec254 | 2019-06-06 17:17:53 +0800 | [diff] [blame] | 598 | resolv_cache_add(statp->netid, buf, buflen, ans, resplen); |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 599 | } |
chenbruce | 018fdb2 | 2019-06-12 18:08:04 +0800 | [diff] [blame] | 600 | res_nclose(statp); |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 601 | return (resplen); |
| 602 | next_ns:; |
Bernie Innocenti | 9c57593 | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 603 | } // for each ns |
| 604 | } // for each retry |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 605 | res_nclose(statp); |
| 606 | if (!v_circuit) { |
Luke Huang | aa6e5ed | 2018-12-03 15:37:28 +0800 | [diff] [blame] | 607 | if (!gotsomewhere) { |
| 608 | // TODO: Remove errno once callers stop using it |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 609 | errno = ECONNREFUSED; /* no nameservers found */ |
Luke Huang | aa6e5ed | 2018-12-03 15:37:28 +0800 | [diff] [blame] | 610 | terrno = ECONNREFUSED; |
| 611 | } else { |
| 612 | // TODO: Remove errno once callers stop using it |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 613 | errno = ETIMEDOUT; /* no answer obtained */ |
Luke Huang | aa6e5ed | 2018-12-03 15:37:28 +0800 | [diff] [blame] | 614 | terrno = ETIMEDOUT; |
| 615 | } |
| 616 | } else { |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 617 | errno = terrno; |
Luke Huang | aa6e5ed | 2018-12-03 15:37:28 +0800 | [diff] [blame] | 618 | } |
Luke Huang | ba7bef9 | 2018-12-26 16:53:03 +0800 | [diff] [blame] | 619 | _resolv_cache_query_failed(statp->netid, buf, buflen, flags); |
Luke Huang | aa6e5ed | 2018-12-03 15:37:28 +0800 | [diff] [blame] | 620 | return -terrno; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 621 | } |
| 622 | |
| 623 | /* Private */ |
| 624 | |
Bernie Innocenti | 9c57593 | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 625 | static int get_salen(const struct sockaddr* sa) { |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 626 | if (sa->sa_family == AF_INET) |
| 627 | return (sizeof(struct sockaddr_in)); |
| 628 | else if (sa->sa_family == AF_INET6) |
| 629 | return (sizeof(struct sockaddr_in6)); |
| 630 | else |
| 631 | return (0); /* unknown, die on connect */ |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 632 | } |
| 633 | |
Bernie Innocenti | 9c57593 | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 634 | static struct sockaddr* get_nsaddr(res_state statp, size_t n) { |
Bernie Innocenti | c977ab7 | 2019-10-09 00:34:06 +0900 | [diff] [blame] | 635 | return (struct sockaddr*)(void*)&statp->nsaddrs[n]; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 636 | } |
| 637 | |
chenbruce | 99cbfd8 | 2019-09-05 15:08:13 +0800 | [diff] [blame] | 638 | static struct timespec get_timeout(res_state statp, const res_params* params, const int ns) { |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 639 | int msec; |
waynema | a74195e | 2019-01-18 14:02:31 +0800 | [diff] [blame] | 640 | // Legacy algorithm which scales the timeout by nameserver number. |
| 641 | // For instance, with 4 nameservers: 5s, 2.5s, 5s, 10s |
| 642 | // This has no effect with 1 or 2 nameservers |
| 643 | msec = params->base_timeout_msec << ns; |
| 644 | if (ns > 0) { |
| 645 | msec /= statp->nscount; |
| 646 | } |
| 647 | // For safety, don't allow OEMs and experiments to configure a timeout shorter than 1s. |
| 648 | if (msec < 1000) { |
| 649 | msec = 1000; // Use at least 1000ms |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 650 | } |
Ken Chen | ffc224a | 2019-03-19 17:41:28 +0800 | [diff] [blame] | 651 | LOG(INFO) << __func__ << ": using timeout of " << msec << " msec"; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 652 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 653 | struct timespec result; |
| 654 | result.tv_sec = msec / 1000; |
| 655 | result.tv_nsec = (msec % 1000) * 1000000; |
| 656 | return result; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 657 | } |
| 658 | |
chenbruce | c51f121 | 2019-09-12 16:59:33 +0800 | [diff] [blame] | 659 | static int send_vc(res_state statp, res_params* params, const uint8_t* buf, int buflen, |
| 660 | uint8_t* ans, int anssiz, int* terrno, int ns, time_t* at, int* rcode, |
| 661 | int* delay) { |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 662 | *at = time(NULL); |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 663 | *delay = 0; |
| 664 | const HEADER* hp = (const HEADER*) (const void*) buf; |
| 665 | HEADER* anhp = (HEADER*) (void*) ans; |
| 666 | struct sockaddr* nsap; |
| 667 | int nsaplen; |
chenbruce | 0d47042 | 2019-03-28 18:44:37 +0800 | [diff] [blame] | 668 | int truncating, connreset, n; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 669 | struct iovec iov[2]; |
chenbruce | c51f121 | 2019-09-12 16:59:33 +0800 | [diff] [blame] | 670 | uint8_t* cp; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 671 | |
Ken Chen | ffc224a | 2019-03-19 17:41:28 +0800 | [diff] [blame] | 672 | LOG(INFO) << __func__ << ": using send_vc"; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 673 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 674 | nsap = get_nsaddr(statp, (size_t) ns); |
| 675 | nsaplen = get_salen(nsap); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 676 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 677 | connreset = 0; |
| 678 | same_ns: |
| 679 | truncating = 0; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 680 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 681 | struct timespec now = evNowTime(); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 682 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 683 | /* Are we still talking to whom we want to talk to? */ |
| 684 | if (statp->_vcsock >= 0 && (statp->_flags & RES_F_VC) != 0) { |
| 685 | struct sockaddr_storage peer; |
| 686 | socklen_t size = sizeof peer; |
| 687 | unsigned old_mark; |
| 688 | socklen_t mark_size = sizeof(old_mark); |
| 689 | if (getpeername(statp->_vcsock, (struct sockaddr*) (void*) &peer, &size) < 0 || |
| 690 | !sock_eq((struct sockaddr*) (void*) &peer, nsap) || |
| 691 | getsockopt(statp->_vcsock, SOL_SOCKET, SO_MARK, &old_mark, &mark_size) < 0 || |
| 692 | old_mark != statp->_mark) { |
| 693 | res_nclose(statp); |
| 694 | statp->_flags &= ~RES_F_VC; |
| 695 | } |
| 696 | } |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 697 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 698 | if (statp->_vcsock < 0 || (statp->_flags & RES_F_VC) == 0) { |
| 699 | if (statp->_vcsock >= 0) res_nclose(statp); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 700 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 701 | statp->_vcsock = socket(nsap->sa_family, SOCK_STREAM | SOCK_CLOEXEC, 0); |
| 702 | if (statp->_vcsock < 0) { |
| 703 | switch (errno) { |
| 704 | case EPROTONOSUPPORT: |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 705 | case EPFNOSUPPORT: |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 706 | case EAFNOSUPPORT: |
chenbruce | 018fdb2 | 2019-06-12 18:08:04 +0800 | [diff] [blame] | 707 | PLOG(DEBUG) << __func__ << ": socket(vc): "; |
Bernie Innocenti | 9f05f5e | 2018-09-12 23:20:10 +0900 | [diff] [blame] | 708 | return 0; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 709 | default: |
| 710 | *terrno = errno; |
chenbruce | 018fdb2 | 2019-06-12 18:08:04 +0800 | [diff] [blame] | 711 | PLOG(DEBUG) << __func__ << ": socket(vc): "; |
Bernie Innocenti | 9f05f5e | 2018-09-12 23:20:10 +0900 | [diff] [blame] | 712 | return -1; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 713 | } |
| 714 | } |
Praveen Moongalam Thyagarajan | 8ab18ba | 2019-09-04 14:46:50 -0700 | [diff] [blame^] | 715 | resolv_tag_socket(statp->_vcsock, statp->uid, statp->pid); |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 716 | if (statp->_mark != MARK_UNSET) { |
| 717 | if (setsockopt(statp->_vcsock, SOL_SOCKET, SO_MARK, &statp->_mark, |
| 718 | sizeof(statp->_mark)) < 0) { |
| 719 | *terrno = errno; |
chenbruce | 018fdb2 | 2019-06-12 18:08:04 +0800 | [diff] [blame] | 720 | PLOG(DEBUG) << __func__ << ": setsockopt: "; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 721 | return -1; |
| 722 | } |
| 723 | } |
| 724 | errno = 0; |
| 725 | if (random_bind(statp->_vcsock, nsap->sa_family) < 0) { |
| 726 | *terrno = errno; |
chenbruce | 018fdb2 | 2019-06-12 18:08:04 +0800 | [diff] [blame] | 727 | dump_error("bind/vc", nsap, nsaplen); |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 728 | res_nclose(statp); |
| 729 | return (0); |
| 730 | } |
| 731 | if (connect_with_timeout(statp->_vcsock, nsap, (socklen_t) nsaplen, |
| 732 | get_timeout(statp, params, ns)) < 0) { |
| 733 | *terrno = errno; |
chenbruce | 018fdb2 | 2019-06-12 18:08:04 +0800 | [diff] [blame] | 734 | dump_error("connect/vc", nsap, nsaplen); |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 735 | res_nclose(statp); |
| 736 | /* |
| 737 | * The way connect_with_timeout() is implemented prevents us from reliably |
| 738 | * determining whether this was really a timeout or e.g. ECONNREFUSED. Since |
| 739 | * currently both cases are handled in the same way, there is no need to |
| 740 | * change this (yet). If we ever need to reliably distinguish between these |
| 741 | * cases, both connect_with_timeout() and retrying_poll() need to be |
| 742 | * modified, though. |
| 743 | */ |
| 744 | *rcode = RCODE_TIMEOUT; |
| 745 | return (0); |
| 746 | } |
| 747 | statp->_flags |= RES_F_VC; |
| 748 | } |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 749 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 750 | /* |
| 751 | * Send length & message |
| 752 | */ |
chenbruce | 0d47042 | 2019-03-28 18:44:37 +0800 | [diff] [blame] | 753 | uint16_t len = htons(static_cast<uint16_t>(buflen)); |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 754 | iov[0] = evConsIovec(&len, INT16SZ); |
Bernie Innocenti | 9c57593 | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 755 | iov[1] = evConsIovec((void*) buf, (size_t) buflen); |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 756 | if (writev(statp->_vcsock, iov, 2) != (INT16SZ + buflen)) { |
| 757 | *terrno = errno; |
chenbruce | 018fdb2 | 2019-06-12 18:08:04 +0800 | [diff] [blame] | 758 | PLOG(DEBUG) << __func__ << ": write failed: "; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 759 | res_nclose(statp); |
| 760 | return (0); |
| 761 | } |
| 762 | /* |
| 763 | * Receive length & response |
| 764 | */ |
| 765 | read_len: |
| 766 | cp = ans; |
| 767 | len = INT16SZ; |
| 768 | while ((n = read(statp->_vcsock, (char*) cp, (size_t) len)) > 0) { |
| 769 | cp += n; |
| 770 | if ((len -= n) == 0) break; |
| 771 | } |
| 772 | if (n <= 0) { |
| 773 | *terrno = errno; |
chenbruce | 018fdb2 | 2019-06-12 18:08:04 +0800 | [diff] [blame] | 774 | PLOG(DEBUG) << __func__ << ": read failed: "; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 775 | res_nclose(statp); |
| 776 | /* |
| 777 | * A long running process might get its TCP |
| 778 | * connection reset if the remote server was |
| 779 | * restarted. Requery the server instead of |
| 780 | * trying a new one. When there is only one |
| 781 | * server, this means that a query might work |
| 782 | * instead of failing. We only allow one reset |
| 783 | * per query to prevent looping. |
| 784 | */ |
| 785 | if (*terrno == ECONNRESET && !connreset) { |
| 786 | connreset = 1; |
| 787 | res_nclose(statp); |
| 788 | goto same_ns; |
| 789 | } |
| 790 | res_nclose(statp); |
| 791 | return (0); |
| 792 | } |
chenbruce | 0d47042 | 2019-03-28 18:44:37 +0800 | [diff] [blame] | 793 | uint16_t resplen = ntohs(*reinterpret_cast<const uint16_t*>(ans)); |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 794 | if (resplen > anssiz) { |
Ken Chen | ffc224a | 2019-03-19 17:41:28 +0800 | [diff] [blame] | 795 | LOG(DEBUG) << __func__ << ": response truncated"; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 796 | truncating = 1; |
| 797 | len = anssiz; |
| 798 | } else |
| 799 | len = resplen; |
| 800 | if (len < HFIXEDSZ) { |
| 801 | /* |
| 802 | * Undersized message. |
| 803 | */ |
Ken Chen | ffc224a | 2019-03-19 17:41:28 +0800 | [diff] [blame] | 804 | LOG(DEBUG) << __func__ << ": undersized: " << len; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 805 | *terrno = EMSGSIZE; |
| 806 | res_nclose(statp); |
| 807 | return (0); |
| 808 | } |
| 809 | cp = ans; |
| 810 | while (len != 0 && (n = read(statp->_vcsock, (char*) cp, (size_t) len)) > 0) { |
| 811 | cp += n; |
| 812 | len -= n; |
| 813 | } |
| 814 | if (n <= 0) { |
| 815 | *terrno = errno; |
chenbruce | 018fdb2 | 2019-06-12 18:08:04 +0800 | [diff] [blame] | 816 | PLOG(DEBUG) << __func__ << ": read(vc): "; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 817 | res_nclose(statp); |
| 818 | return (0); |
| 819 | } |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 820 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 821 | if (truncating) { |
| 822 | /* |
| 823 | * Flush rest of answer so connection stays in synch. |
| 824 | */ |
| 825 | anhp->tc = 1; |
| 826 | len = resplen - anssiz; |
| 827 | while (len != 0) { |
| 828 | char junk[PACKETSZ]; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 829 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 830 | n = read(statp->_vcsock, junk, (len > sizeof junk) ? sizeof junk : len); |
| 831 | if (n > 0) |
| 832 | len -= n; |
| 833 | else |
| 834 | break; |
| 835 | } |
| 836 | } |
| 837 | /* |
Bernie Innocenti | 9f05f5e | 2018-09-12 23:20:10 +0900 | [diff] [blame] | 838 | * If the calling application has bailed out of |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 839 | * a previous call and failed to arrange to have |
| 840 | * the circuit closed or the server has got |
| 841 | * itself confused, then drop the packet and |
| 842 | * wait for the correct one. |
| 843 | */ |
| 844 | if (hp->id != anhp->id) { |
Ken Chen | ffc224a | 2019-03-19 17:41:28 +0800 | [diff] [blame] | 845 | LOG(DEBUG) << __func__ << ": ld answer (unexpected):"; |
chenbruce | dd21072 | 2019-03-06 13:52:43 +0800 | [diff] [blame] | 846 | res_pquery(ans, (resplen > anssiz) ? anssiz : resplen); |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 847 | goto read_len; |
| 848 | } |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 849 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 850 | /* |
| 851 | * All is well, or the error is fatal. Signal that the |
| 852 | * next nameserver ought not be tried. |
| 853 | */ |
| 854 | if (resplen > 0) { |
| 855 | struct timespec done = evNowTime(); |
| 856 | *delay = _res_stats_calculate_rtt(&done, &now); |
| 857 | *rcode = anhp->rcode; |
| 858 | } |
| 859 | return (resplen); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 860 | } |
| 861 | |
| 862 | /* return -1 on error (errno set), 0 on success */ |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 863 | static int connect_with_timeout(int sock, const struct sockaddr* nsap, socklen_t salen, |
| 864 | const struct timespec timeout) { |
| 865 | int res, origflags; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 866 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 867 | origflags = fcntl(sock, F_GETFL, 0); |
| 868 | fcntl(sock, F_SETFL, origflags | O_NONBLOCK); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 869 | |
Bernie Innocenti | afaacf7 | 2018-08-30 07:34:37 +0900 | [diff] [blame] | 870 | res = connect(sock, nsap, salen); |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 871 | if (res < 0 && errno != EINPROGRESS) { |
| 872 | res = -1; |
| 873 | goto done; |
| 874 | } |
| 875 | if (res != 0) { |
| 876 | struct timespec now = evNowTime(); |
| 877 | struct timespec finish = evAddTime(now, timeout); |
Ken Chen | ffc224a | 2019-03-19 17:41:28 +0800 | [diff] [blame] | 878 | LOG(INFO) << __func__ << ": " << sock << " send_vc"; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 879 | res = retrying_poll(sock, POLLIN | POLLOUT, &finish); |
| 880 | if (res <= 0) { |
| 881 | res = -1; |
| 882 | } |
| 883 | } |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 884 | done: |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 885 | fcntl(sock, F_SETFL, origflags); |
Ken Chen | ffc224a | 2019-03-19 17:41:28 +0800 | [diff] [blame] | 886 | LOG(INFO) << __func__ << ": " << sock << " connect_with_const timeout returning " << res; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 887 | return res; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 888 | } |
| 889 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 890 | static int retrying_poll(const int sock, const short events, const struct timespec* finish) { |
| 891 | struct timespec now, timeout; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 892 | |
| 893 | retry: |
Ken Chen | ffc224a | 2019-03-19 17:41:28 +0800 | [diff] [blame] | 894 | LOG(INFO) << __func__ << ": " << sock << " retrying_poll"; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 895 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 896 | now = evNowTime(); |
| 897 | if (evCmpTime(*finish, now) > 0) |
| 898 | timeout = evSubTime(*finish, now); |
| 899 | else |
| 900 | timeout = evConsTime(0L, 0L); |
| 901 | struct pollfd fds = {.fd = sock, .events = events}; |
| 902 | int n = ppoll(&fds, 1, &timeout, /*sigmask=*/NULL); |
| 903 | if (n == 0) { |
Ken Chen | ffc224a | 2019-03-19 17:41:28 +0800 | [diff] [blame] | 904 | LOG(INFO) << __func__ << ": " << sock << "retrying_poll timeout"; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 905 | errno = ETIMEDOUT; |
| 906 | return 0; |
| 907 | } |
| 908 | if (n < 0) { |
| 909 | if (errno == EINTR) goto retry; |
Ken Chen | ffc224a | 2019-03-19 17:41:28 +0800 | [diff] [blame] | 910 | PLOG(INFO) << __func__ << ": " << sock << " retrying_poll failed"; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 911 | return n; |
| 912 | } |
| 913 | if (fds.revents & (POLLIN | POLLOUT | POLLERR)) { |
| 914 | int error; |
| 915 | socklen_t len = sizeof(error); |
| 916 | if (getsockopt(sock, SOL_SOCKET, SO_ERROR, &error, &len) < 0 || error) { |
| 917 | errno = error; |
Ken Chen | ffc224a | 2019-03-19 17:41:28 +0800 | [diff] [blame] | 918 | PLOG(INFO) << __func__ << ": " << sock << " retrying_poll getsockopt failed"; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 919 | return -1; |
| 920 | } |
| 921 | } |
Ken Chen | ffc224a | 2019-03-19 17:41:28 +0800 | [diff] [blame] | 922 | LOG(INFO) << __func__ << ": " << sock << " retrying_poll returning " << n; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 923 | return n; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 924 | } |
| 925 | |
chenbruce | c51f121 | 2019-09-12 16:59:33 +0800 | [diff] [blame] | 926 | static int send_dg(res_state statp, res_params* params, const uint8_t* buf, int buflen, |
| 927 | uint8_t* ans, int anssiz, int* terrno, int ns, int* v_circuit, int* gotsomewhere, |
| 928 | time_t* at, int* rcode, int* delay) { |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 929 | *at = time(NULL); |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 930 | *delay = 0; |
| 931 | const HEADER* hp = (const HEADER*) (const void*) buf; |
| 932 | HEADER* anhp = (HEADER*) (void*) ans; |
| 933 | const struct sockaddr* nsap; |
| 934 | int nsaplen; |
| 935 | struct timespec now, timeout, finish, done; |
| 936 | struct sockaddr_storage from; |
| 937 | socklen_t fromlen; |
| 938 | int resplen, n, s; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 939 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 940 | nsap = get_nsaddr(statp, (size_t) ns); |
| 941 | nsaplen = get_salen(nsap); |
Bernie Innocenti | bb33074 | 2019-10-09 00:55:53 +0900 | [diff] [blame] | 942 | if (statp->nssocks[ns] == -1) { |
| 943 | statp->nssocks[ns] = socket(nsap->sa_family, SOCK_DGRAM | SOCK_CLOEXEC, 0); |
| 944 | if (statp->nssocks[ns] < 0) { |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 945 | switch (errno) { |
| 946 | case EPROTONOSUPPORT: |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 947 | case EPFNOSUPPORT: |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 948 | case EAFNOSUPPORT: |
chenbruce | 018fdb2 | 2019-06-12 18:08:04 +0800 | [diff] [blame] | 949 | PLOG(DEBUG) << __func__ << ": socket(dg): "; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 950 | return (0); |
| 951 | default: |
| 952 | *terrno = errno; |
chenbruce | 018fdb2 | 2019-06-12 18:08:04 +0800 | [diff] [blame] | 953 | PLOG(DEBUG) << __func__ << ": socket(dg): "; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 954 | return (-1); |
| 955 | } |
| 956 | } |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 957 | |
Praveen Moongalam Thyagarajan | 8ab18ba | 2019-09-04 14:46:50 -0700 | [diff] [blame^] | 958 | resolv_tag_socket(statp->nssocks[ns], statp->uid, statp->pid); |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 959 | if (statp->_mark != MARK_UNSET) { |
Bernie Innocenti | bb33074 | 2019-10-09 00:55:53 +0900 | [diff] [blame] | 960 | if (setsockopt(statp->nssocks[ns], SOL_SOCKET, SO_MARK, &(statp->_mark), |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 961 | sizeof(statp->_mark)) < 0) { |
| 962 | res_nclose(statp); |
| 963 | return -1; |
| 964 | } |
| 965 | } |
chenbruce | b9cce7b | 2019-07-15 16:14:53 +0800 | [diff] [blame] | 966 | // Use a "connected" datagram socket to receive an ECONNREFUSED error |
| 967 | // on the next socket operation when the server responds with an |
| 968 | // ICMP port-unreachable error. This way we can detect the absence of |
| 969 | // a nameserver without timing out. |
Bernie Innocenti | bb33074 | 2019-10-09 00:55:53 +0900 | [diff] [blame] | 970 | if (random_bind(statp->nssocks[ns], nsap->sa_family) < 0) { |
chenbruce | 018fdb2 | 2019-06-12 18:08:04 +0800 | [diff] [blame] | 971 | dump_error("bind(dg)", nsap, nsaplen); |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 972 | res_nclose(statp); |
| 973 | return (0); |
| 974 | } |
Bernie Innocenti | bb33074 | 2019-10-09 00:55:53 +0900 | [diff] [blame] | 975 | if (connect(statp->nssocks[ns], nsap, (socklen_t)nsaplen) < 0) { |
chenbruce | 018fdb2 | 2019-06-12 18:08:04 +0800 | [diff] [blame] | 976 | dump_error("connect(dg)", nsap, nsaplen); |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 977 | res_nclose(statp); |
| 978 | return (0); |
| 979 | } |
Ken Chen | ffc224a | 2019-03-19 17:41:28 +0800 | [diff] [blame] | 980 | LOG(DEBUG) << __func__ << ": new DG socket"; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 981 | } |
Bernie Innocenti | bb33074 | 2019-10-09 00:55:53 +0900 | [diff] [blame] | 982 | s = statp->nssocks[ns]; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 983 | if (send(s, (const char*) buf, (size_t) buflen, 0) != buflen) { |
chenbruce | 018fdb2 | 2019-06-12 18:08:04 +0800 | [diff] [blame] | 984 | PLOG(DEBUG) << __func__ << ": send: "; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 985 | res_nclose(statp); |
Bernie Innocenti | 3952ccc | 2019-03-03 19:39:53 +0900 | [diff] [blame] | 986 | return 0; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 987 | } |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 988 | |
Bernie Innocenti | 3952ccc | 2019-03-03 19:39:53 +0900 | [diff] [blame] | 989 | // Wait for reply. |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 990 | timeout = get_timeout(statp, params, ns); |
| 991 | now = evNowTime(); |
| 992 | finish = evAddTime(now, timeout); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 993 | retry: |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 994 | n = retrying_poll(s, POLLIN, &finish); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 995 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 996 | if (n == 0) { |
| 997 | *rcode = RCODE_TIMEOUT; |
Ken Chen | ffc224a | 2019-03-19 17:41:28 +0800 | [diff] [blame] | 998 | LOG(DEBUG) << __func__ << ": timeout"; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 999 | *gotsomewhere = 1; |
Bernie Innocenti | 3952ccc | 2019-03-03 19:39:53 +0900 | [diff] [blame] | 1000 | return 0; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1001 | } |
| 1002 | if (n < 0) { |
chenbruce | 018fdb2 | 2019-06-12 18:08:04 +0800 | [diff] [blame] | 1003 | PLOG(DEBUG) << __func__ << ": poll: "; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1004 | res_nclose(statp); |
Bernie Innocenti | 3952ccc | 2019-03-03 19:39:53 +0900 | [diff] [blame] | 1005 | return 0; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1006 | } |
| 1007 | errno = 0; |
| 1008 | fromlen = sizeof(from); |
| 1009 | resplen = recvfrom(s, (char*) ans, (size_t) anssiz, 0, (struct sockaddr*) (void*) &from, |
| 1010 | &fromlen); |
| 1011 | if (resplen <= 0) { |
chenbruce | 018fdb2 | 2019-06-12 18:08:04 +0800 | [diff] [blame] | 1012 | PLOG(DEBUG) << __func__ << ": recvfrom: "; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1013 | res_nclose(statp); |
Bernie Innocenti | 3952ccc | 2019-03-03 19:39:53 +0900 | [diff] [blame] | 1014 | return 0; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1015 | } |
| 1016 | *gotsomewhere = 1; |
| 1017 | if (resplen < HFIXEDSZ) { |
| 1018 | /* |
| 1019 | * Undersized message. |
| 1020 | */ |
Ken Chen | ffc224a | 2019-03-19 17:41:28 +0800 | [diff] [blame] | 1021 | LOG(DEBUG) << __func__ << ": undersized: " << resplen; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1022 | *terrno = EMSGSIZE; |
| 1023 | res_nclose(statp); |
Bernie Innocenti | 3952ccc | 2019-03-03 19:39:53 +0900 | [diff] [blame] | 1024 | return 0; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1025 | } |
| 1026 | if (hp->id != anhp->id) { |
| 1027 | /* |
| 1028 | * response from old query, ignore it. |
| 1029 | * XXX - potential security hazard could |
| 1030 | * be detected here. |
| 1031 | */ |
Ken Chen | ffc224a | 2019-03-19 17:41:28 +0800 | [diff] [blame] | 1032 | LOG(DEBUG) << __func__ << ": old answer:"; |
chenbruce | dd21072 | 2019-03-06 13:52:43 +0800 | [diff] [blame] | 1033 | res_pquery(ans, (resplen > anssiz) ? anssiz : resplen); |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1034 | goto retry; |
| 1035 | } |
chenbruce | 018fdb2 | 2019-06-12 18:08:04 +0800 | [diff] [blame] | 1036 | if (!res_ourserver_p(statp, (struct sockaddr*)(void*)&from)) { |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1037 | /* |
| 1038 | * response from wrong server? ignore it. |
| 1039 | * XXX - potential security hazard could |
| 1040 | * be detected here. |
| 1041 | */ |
Ken Chen | ffc224a | 2019-03-19 17:41:28 +0800 | [diff] [blame] | 1042 | LOG(DEBUG) << __func__ << ": not our server:"; |
chenbruce | dd21072 | 2019-03-06 13:52:43 +0800 | [diff] [blame] | 1043 | res_pquery(ans, (resplen > anssiz) ? anssiz : resplen); |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1044 | goto retry; |
| 1045 | } |
chenbruce | d8cbb9b | 2019-06-20 18:25:28 +0800 | [diff] [blame] | 1046 | if (anhp->rcode == FORMERR && (statp->netcontext_flags & NET_CONTEXT_FLAG_USE_EDNS)) { |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1047 | /* |
| 1048 | * Do not retry if the server do not understand EDNS0. |
| 1049 | * The case has to be captured here, as FORMERR packet do not |
| 1050 | * carry query section, hence res_queriesmatch() returns 0. |
| 1051 | */ |
Ken Chen | ffc224a | 2019-03-19 17:41:28 +0800 | [diff] [blame] | 1052 | LOG(DEBUG) << __func__ << ": server rejected query with EDNS0:"; |
chenbruce | dd21072 | 2019-03-06 13:52:43 +0800 | [diff] [blame] | 1053 | res_pquery(ans, (resplen > anssiz) ? anssiz : resplen); |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1054 | /* record the error */ |
| 1055 | statp->_flags |= RES_F_EDNS0ERR; |
| 1056 | res_nclose(statp); |
Bernie Innocenti | 3952ccc | 2019-03-03 19:39:53 +0900 | [diff] [blame] | 1057 | return 0; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1058 | } |
chenbruce | 018fdb2 | 2019-06-12 18:08:04 +0800 | [diff] [blame] | 1059 | if (!res_queriesmatch(buf, buf + buflen, ans, ans + anssiz)) { |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1060 | /* |
| 1061 | * response contains wrong query? ignore it. |
| 1062 | * XXX - potential security hazard could |
| 1063 | * be detected here. |
| 1064 | */ |
Ken Chen | ffc224a | 2019-03-19 17:41:28 +0800 | [diff] [blame] | 1065 | LOG(DEBUG) << __func__ << ": wrong query name:"; |
chenbruce | dd21072 | 2019-03-06 13:52:43 +0800 | [diff] [blame] | 1066 | res_pquery(ans, (resplen > anssiz) ? anssiz : resplen); |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1067 | goto retry; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1068 | } |
| 1069 | done = evNowTime(); |
| 1070 | *delay = _res_stats_calculate_rtt(&done, &now); |
| 1071 | if (anhp->rcode == SERVFAIL || anhp->rcode == NOTIMP || anhp->rcode == REFUSED) { |
Ken Chen | ffc224a | 2019-03-19 17:41:28 +0800 | [diff] [blame] | 1072 | LOG(DEBUG) << __func__ << ": server rejected query:"; |
chenbruce | dd21072 | 2019-03-06 13:52:43 +0800 | [diff] [blame] | 1073 | res_pquery(ans, (resplen > anssiz) ? anssiz : resplen); |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1074 | res_nclose(statp); |
chenbruce | dd21072 | 2019-03-06 13:52:43 +0800 | [diff] [blame] | 1075 | *rcode = anhp->rcode; |
| 1076 | return 0; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1077 | } |
chenbruce | 018fdb2 | 2019-06-12 18:08:04 +0800 | [diff] [blame] | 1078 | if (anhp->tc) { |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1079 | /* |
| 1080 | * To get the rest of answer, |
| 1081 | * use TCP with same server. |
| 1082 | */ |
Ken Chen | ffc224a | 2019-03-19 17:41:28 +0800 | [diff] [blame] | 1083 | LOG(DEBUG) << __func__ << ": truncated answer"; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1084 | *v_circuit = 1; |
| 1085 | res_nclose(statp); |
Bernie Innocenti | 3952ccc | 2019-03-03 19:39:53 +0900 | [diff] [blame] | 1086 | return 1; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1087 | } |
| 1088 | /* |
| 1089 | * All is well, or the error is fatal. Signal that the |
| 1090 | * next nameserver ought not be tried. |
| 1091 | */ |
| 1092 | if (resplen > 0) { |
| 1093 | *rcode = anhp->rcode; |
| 1094 | } |
Bernie Innocenti | 3952ccc | 2019-03-03 19:39:53 +0900 | [diff] [blame] | 1095 | return resplen; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1096 | } |
| 1097 | |
chenbruce | 018fdb2 | 2019-06-12 18:08:04 +0800 | [diff] [blame] | 1098 | static void dump_error(const char* str, const struct sockaddr* address, int alen) { |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1099 | char hbuf[NI_MAXHOST]; |
| 1100 | char sbuf[NI_MAXSERV]; |
Bernie Innocenti | 9f05f5e | 2018-09-12 23:20:10 +0900 | [diff] [blame] | 1101 | constexpr int niflags = NI_NUMERICHOST | NI_NUMERICSERV; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1102 | |
chenbruce | 018fdb2 | 2019-06-12 18:08:04 +0800 | [diff] [blame] | 1103 | if (!WOULD_LOG(DEBUG)) return; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1104 | |
chenbruce | 018fdb2 | 2019-06-12 18:08:04 +0800 | [diff] [blame] | 1105 | if (getnameinfo(address, (socklen_t)alen, hbuf, sizeof(hbuf), sbuf, sizeof(sbuf), niflags)) { |
| 1106 | strncpy(hbuf, "?", sizeof(hbuf) - 1); |
| 1107 | hbuf[sizeof(hbuf) - 1] = '\0'; |
| 1108 | strncpy(sbuf, "?", sizeof(sbuf) - 1); |
| 1109 | sbuf[sizeof(sbuf) - 1] = '\0'; |
Elliott Hughes | 3b8f590 | 2019-03-08 12:02:55 -0800 | [diff] [blame] | 1110 | } |
chenbruce | 018fdb2 | 2019-06-12 18:08:04 +0800 | [diff] [blame] | 1111 | PLOG(DEBUG) << __func__ << ": " << str << " ([" << hbuf << "]." << sbuf << "): "; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1112 | } |
| 1113 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1114 | static int sock_eq(struct sockaddr* a, struct sockaddr* b) { |
| 1115 | struct sockaddr_in *a4, *b4; |
| 1116 | struct sockaddr_in6 *a6, *b6; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1117 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1118 | if (a->sa_family != b->sa_family) return 0; |
| 1119 | switch (a->sa_family) { |
| 1120 | case AF_INET: |
| 1121 | a4 = (struct sockaddr_in*) (void*) a; |
| 1122 | b4 = (struct sockaddr_in*) (void*) b; |
| 1123 | return a4->sin_port == b4->sin_port && a4->sin_addr.s_addr == b4->sin_addr.s_addr; |
| 1124 | case AF_INET6: |
| 1125 | a6 = (struct sockaddr_in6*) (void*) a; |
| 1126 | b6 = (struct sockaddr_in6*) (void*) b; |
| 1127 | return a6->sin6_port == b6->sin6_port && |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1128 | #ifdef HAVE_SIN6_SCOPE_ID |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1129 | a6->sin6_scope_id == b6->sin6_scope_id && |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1130 | #endif |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1131 | IN6_ARE_ADDR_EQUAL(&a6->sin6_addr, &b6->sin6_addr); |
| 1132 | default: |
| 1133 | return 0; |
| 1134 | } |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1135 | } |
Mike Yu | b601ff7 | 2018-11-01 20:07:00 +0800 | [diff] [blame] | 1136 | |
lifr | 6b83a6f | 2019-07-26 03:17:43 +0800 | [diff] [blame] | 1137 | PrivateDnsModes convertEnumType(PrivateDnsMode privateDnsmode) { |
| 1138 | switch (privateDnsmode) { |
| 1139 | case PrivateDnsMode::OFF: |
| 1140 | return PrivateDnsModes::PDM_OFF; |
| 1141 | case PrivateDnsMode::OPPORTUNISTIC: |
| 1142 | return PrivateDnsModes::PDM_OPPORTUNISTIC; |
| 1143 | case PrivateDnsMode::STRICT: |
| 1144 | return PrivateDnsModes::PDM_STRICT; |
| 1145 | } |
| 1146 | return PrivateDnsModes::PDM_UNKNOWN; |
| 1147 | } |
| 1148 | |
Luke Huang | d7aa7b4 | 2018-11-21 20:37:50 +0800 | [diff] [blame] | 1149 | static int res_tls_send(res_state statp, const Slice query, const Slice answer, int* rcode, |
Mike Yu | bfb1b34 | 2018-11-06 15:42:36 +0800 | [diff] [blame] | 1150 | bool* fallback) { |
| 1151 | int resplen = 0; |
Mike Yu | b601ff7 | 2018-11-01 20:07:00 +0800 | [diff] [blame] | 1152 | const unsigned netId = statp->netid; |
Mike Yu | b601ff7 | 2018-11-01 20:07:00 +0800 | [diff] [blame] | 1153 | |
Mike Yu | bfb1b34 | 2018-11-06 15:42:36 +0800 | [diff] [blame] | 1154 | PrivateDnsStatus privateDnsStatus = gPrivateDnsConfiguration.getStatus(netId); |
lifr | 6b83a6f | 2019-07-26 03:17:43 +0800 | [diff] [blame] | 1155 | statp->event->set_private_dns_modes(convertEnumType(privateDnsStatus.mode)); |
Mike Yu | bfb1b34 | 2018-11-06 15:42:36 +0800 | [diff] [blame] | 1156 | |
| 1157 | if (privateDnsStatus.mode == PrivateDnsMode::OFF) { |
| 1158 | *fallback = true; |
| 1159 | return -1; |
Mike Yu | b601ff7 | 2018-11-01 20:07:00 +0800 | [diff] [blame] | 1160 | } |
| 1161 | |
Mike Yu | 3e82906 | 2019-08-07 14:01:14 +0800 | [diff] [blame] | 1162 | if (privateDnsStatus.validatedServers().empty()) { |
Mike Yu | b601ff7 | 2018-11-01 20:07:00 +0800 | [diff] [blame] | 1163 | if (privateDnsStatus.mode == PrivateDnsMode::OPPORTUNISTIC) { |
Mike Yu | bfb1b34 | 2018-11-06 15:42:36 +0800 | [diff] [blame] | 1164 | *fallback = true; |
| 1165 | return -1; |
Mike Yu | b601ff7 | 2018-11-01 20:07:00 +0800 | [diff] [blame] | 1166 | } else { |
| 1167 | // Sleep and iterate some small number of times checking for the |
| 1168 | // arrival of resolved and validated server IP addresses, instead |
| 1169 | // of returning an immediate error. |
Mike Yu | 4f3747b | 2018-12-02 17:54:29 +0900 | [diff] [blame] | 1170 | // This is needed because as soon as a network becomes the default network, apps will |
| 1171 | // send DNS queries on that network. If no servers have yet validated, and we do not |
| 1172 | // block those queries, they would immediately fail, causing application-visible errors. |
| 1173 | // Note that this can happen even before the network validates, since an unvalidated |
| 1174 | // network can become the default network if no validated networks are available. |
| 1175 | // |
| 1176 | // TODO: see if there is a better way to address this problem, such as buffering the |
| 1177 | // queries in a queue or only blocking queries for the first few seconds after a default |
| 1178 | // network change. |
Mike Yu | bfb1b34 | 2018-11-06 15:42:36 +0800 | [diff] [blame] | 1179 | for (int i = 0; i < 42; i++) { |
| 1180 | std::this_thread::sleep_for(std::chrono::milliseconds(100)); |
Mike Yu | 3e82906 | 2019-08-07 14:01:14 +0800 | [diff] [blame] | 1181 | // Calling getStatus() to merely check if there's any validated server seems |
| 1182 | // wasteful. Consider adding a new method in PrivateDnsConfiguration for speed ups. |
| 1183 | if (!gPrivateDnsConfiguration.getStatus(netId).validatedServers().empty()) { |
Mike Yu | bfb1b34 | 2018-11-06 15:42:36 +0800 | [diff] [blame] | 1184 | privateDnsStatus = gPrivateDnsConfiguration.getStatus(netId); |
| 1185 | break; |
| 1186 | } |
| 1187 | } |
Mike Yu | 3e82906 | 2019-08-07 14:01:14 +0800 | [diff] [blame] | 1188 | if (privateDnsStatus.validatedServers().empty()) { |
Mike Yu | bfb1b34 | 2018-11-06 15:42:36 +0800 | [diff] [blame] | 1189 | return -1; |
| 1190 | } |
Mike Yu | b601ff7 | 2018-11-01 20:07:00 +0800 | [diff] [blame] | 1191 | } |
| 1192 | } |
| 1193 | |
chenbruce | acb832c | 2019-02-20 19:45:50 +0800 | [diff] [blame] | 1194 | LOG(INFO) << __func__ << ": performing query over TLS"; |
Mike Yu | b601ff7 | 2018-11-01 20:07:00 +0800 | [diff] [blame] | 1195 | |
Mike Yu | 3e82906 | 2019-08-07 14:01:14 +0800 | [diff] [blame] | 1196 | const auto response = sDnsTlsDispatcher.query(privateDnsStatus.validatedServers(), statp, query, |
Mike Yu | bfb1b34 | 2018-11-06 15:42:36 +0800 | [diff] [blame] | 1197 | answer, &resplen); |
Mike Yu | b601ff7 | 2018-11-01 20:07:00 +0800 | [diff] [blame] | 1198 | |
chenbruce | acb832c | 2019-02-20 19:45:50 +0800 | [diff] [blame] | 1199 | LOG(INFO) << __func__ << ": TLS query result: " << static_cast<int>(response); |
Mike Yu | b601ff7 | 2018-11-01 20:07:00 +0800 | [diff] [blame] | 1200 | |
| 1201 | if (privateDnsStatus.mode == PrivateDnsMode::OPPORTUNISTIC) { |
| 1202 | // In opportunistic mode, handle falling back to cleartext in some |
| 1203 | // cases (DNS shouldn't fail if a validated opportunistic mode server |
| 1204 | // becomes unreachable for some reason). |
| 1205 | switch (response) { |
Mike Yu | bfb1b34 | 2018-11-06 15:42:36 +0800 | [diff] [blame] | 1206 | case DnsTlsTransport::Response::success: |
Luke Huang | d7aa7b4 | 2018-11-21 20:37:50 +0800 | [diff] [blame] | 1207 | *rcode = reinterpret_cast<HEADER*>(answer.base())->rcode; |
Mike Yu | bfb1b34 | 2018-11-06 15:42:36 +0800 | [diff] [blame] | 1208 | return resplen; |
Mike Yu | b601ff7 | 2018-11-01 20:07:00 +0800 | [diff] [blame] | 1209 | case DnsTlsTransport::Response::network_error: |
Mike Yu | bfb1b34 | 2018-11-06 15:42:36 +0800 | [diff] [blame] | 1210 | // No need to set the error timeout here since it will fallback to UDP. |
Mike Yu | b601ff7 | 2018-11-01 20:07:00 +0800 | [diff] [blame] | 1211 | case DnsTlsTransport::Response::internal_error: |
| 1212 | // Note: this will cause cleartext queries to be emitted, with |
| 1213 | // all of the EDNS0 goodness enabled. Fingers crossed. :-/ |
Mike Yu | bfb1b34 | 2018-11-06 15:42:36 +0800 | [diff] [blame] | 1214 | *fallback = true; |
| 1215 | [[fallthrough]]; |
Mike Yu | b601ff7 | 2018-11-01 20:07:00 +0800 | [diff] [blame] | 1216 | default: |
Mike Yu | bfb1b34 | 2018-11-06 15:42:36 +0800 | [diff] [blame] | 1217 | return -1; |
| 1218 | } |
| 1219 | } else { |
| 1220 | // Strict mode |
| 1221 | switch (response) { |
| 1222 | case DnsTlsTransport::Response::success: |
Luke Huang | d7aa7b4 | 2018-11-21 20:37:50 +0800 | [diff] [blame] | 1223 | *rcode = reinterpret_cast<HEADER*>(answer.base())->rcode; |
Mike Yu | bfb1b34 | 2018-11-06 15:42:36 +0800 | [diff] [blame] | 1224 | return resplen; |
| 1225 | case DnsTlsTransport::Response::network_error: |
| 1226 | // This case happens when the query stored in DnsTlsTransport is expired since |
| 1227 | // either 1) the query has been tried for 3 times but no response or 2) fail to |
| 1228 | // establish the connection with the server. |
Luke Huang | d7aa7b4 | 2018-11-21 20:37:50 +0800 | [diff] [blame] | 1229 | *rcode = RCODE_TIMEOUT; |
Mike Yu | bfb1b34 | 2018-11-06 15:42:36 +0800 | [diff] [blame] | 1230 | [[fallthrough]]; |
| 1231 | default: |
| 1232 | return -1; |
Mike Yu | b601ff7 | 2018-11-01 20:07:00 +0800 | [diff] [blame] | 1233 | } |
| 1234 | } |
Mike Yu | b601ff7 | 2018-11-01 20:07:00 +0800 | [diff] [blame] | 1235 | } |
Luke Huang | 4973a0d | 2018-11-19 20:17:26 +0800 | [diff] [blame] | 1236 | |
Luke Huang | ba7bef9 | 2018-12-26 16:53:03 +0800 | [diff] [blame] | 1237 | int resolv_res_nsend(const android_net_context* netContext, const uint8_t* msg, int msgLen, |
lifr | 9498178 | 2019-05-17 21:15:19 +0800 | [diff] [blame] | 1238 | uint8_t* ans, int ansLen, int* rcode, uint32_t flags, |
| 1239 | NetworkDnsEventReported* event) { |
| 1240 | assert(event != nullptr); |
Bernie Innocenti | 0848711 | 2019-10-11 21:14:13 +0900 | [diff] [blame] | 1241 | ResState res; |
| 1242 | res_init(&res, netContext, event); |
| 1243 | _resolv_populate_res_for_net(&res); |
Luke Huang | 4973a0d | 2018-11-19 20:17:26 +0800 | [diff] [blame] | 1244 | *rcode = NOERROR; |
Bernie Innocenti | 0848711 | 2019-10-11 21:14:13 +0900 | [diff] [blame] | 1245 | return res_nsend(&res, msg, msgLen, ans, ansLen, rcode, flags); |
Bernie Innocenti | ec4219b | 2019-01-30 11:16:36 +0900 | [diff] [blame] | 1246 | } |