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