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