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