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