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