blob: d02cdb35e568d028a8012a48a9f7b89dccd91285 [file] [log] [blame]
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001/* $NetBSD: res_mkquery.c,v 1.6 2006/01/24 17:40:32 christos Exp $ */
2
3/*
4 * Copyright (c) 1985, 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
Ken Chen5471dca2019-04-15 15:25:35 +080073#define LOG_TAG "resolv"
Bernie Innocenti3952ccc2019-03-03 19:39:53 +090074
Bernie Innocenti136af7b2018-10-01 20:46:20 +090075#include <algorithm> // std::min()
76
Bernie Innocenti318ed2d2018-08-30 04:05:20 +090077#include <arpa/nameser.h>
78#include <netdb.h>
Bernie Innocenti8ad893f2018-08-31 14:09:46 +090079#include <netinet/in.h>
Bernie Innocenti318ed2d2018-08-30 04:05:20 +090080#include <string.h>
Bernie Innocentif33a63f2018-08-30 12:04:03 +090081
chenbruceacb832c2019-02-20 19:45:50 +080082#include <android-base/logging.h>
Bernie Innocenti10a90282020-01-23 23:28:00 +090083#include <netd_resolv/resolv.h> // NET_CONTEXT_FLAG_USE_DNS_OVER_TLS
Bernie Innocenti318ed2d2018-08-30 04:05:20 +090084
Bernie Innocenti10a90282020-01-23 23:28:00 +090085#include "res_comp.h"
86#include "res_debug.h"
Bernie Innocenti983f02e2021-07-01 07:36:56 +090087#include "resolv_private.h" // ResState*
Bernie Innocenti318ed2d2018-08-30 04:05:20 +090088
Bernie Innocenti136af7b2018-10-01 20:46:20 +090089// Queries will be padded to a multiple of this length when EDNS0 is active.
90constexpr uint16_t kEdns0Padding = 128;
91
Mike Yubfb1b342018-11-06 15:42:36 +080092extern const char* const _res_opcodes[] = {
93 "QUERY", "IQUERY", "CQUERYM", "CQUERYU", /* experimental */
94 "NOTIFY", /* experimental */
95 "UPDATE", "6", "7", "8", "9", "10",
96 "11", "12", "13", "ZONEINIT", "ZONEREF",
97};
Bernie Innocenti318ed2d2018-08-30 04:05:20 +090098
Bernie Innocenti08487112019-10-11 21:14:13 +090099// Form all types of queries. Returns the size of the result or -1.
chenbrucec7d69272021-08-09 14:20:33 +0800100int res_nmkquery(int op, // opcode of query
101 const char* dname, // domain name
102 int cl, int type, // class and type of query
103 std::span<const uint8_t> data, // resource record data
104 std::span<uint8_t> buf, // buffer to put query
Bernie Innocenti08487112019-10-11 21:14:13 +0900105 int netcontext_flags) {
Bernie Innocenti9c575932018-09-07 21:10:25 +0900106 HEADER* hp;
chenbrucec51f1212019-09-12 16:59:33 +0800107 uint8_t *cp, *ep;
Bernie Innocenti9c575932018-09-07 21:10:25 +0900108 int n;
chenbrucec51f1212019-09-12 16:59:33 +0800109 uint8_t *dnptrs[20], **dpp, **lastdnptr;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900110
Ken Chenffc224a2019-03-19 17:41:28 +0800111 LOG(DEBUG) << __func__ << ": (" << _res_opcodes[op] << ", " << p_class(cl) << ", "
112 << p_type(type) << ")";
chenbruceacb832c2019-02-20 19:45:50 +0800113
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900114 /*
115 * Initialize header fields.
116 */
chenbrucec7d69272021-08-09 14:20:33 +0800117 if (buf.empty() || (buf.size() < HFIXEDSZ)) return (-1);
118 memset(buf.data(), 0, HFIXEDSZ);
119 hp = (HEADER*)(void*)buf.data();
Luke Huangdda920f2019-02-13 14:05:02 +0800120 hp->id = htons(arc4random_uniform(65536));
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900121 hp->opcode = op;
chenbruce018fdb22019-06-12 18:08:04 +0800122 hp->rd = true;
Bernie Innocenti08487112019-10-11 21:14:13 +0900123 hp->ad = (netcontext_flags & NET_CONTEXT_FLAG_USE_DNS_OVER_TLS) != 0U;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900124 hp->rcode = NOERROR;
chenbrucec7d69272021-08-09 14:20:33 +0800125 cp = buf.data() + HFIXEDSZ;
126 ep = buf.data() + buf.size();
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900127 dpp = dnptrs;
chenbrucec7d69272021-08-09 14:20:33 +0800128 *dpp++ = buf.data();
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900129 *dpp++ = NULL;
130 lastdnptr = dnptrs + sizeof dnptrs / sizeof dnptrs[0];
131 /*
132 * perform opcode specific processing
133 */
134 switch (op) {
Bernie Innocentif40b3bd2018-10-10 22:30:12 +0900135 case QUERY:
136 [[fallthrough]];
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900137 case NS_NOTIFY_OP:
138 if (ep - cp < QFIXEDSZ) return (-1);
139 if ((n = dn_comp(dname, cp, ep - cp - QFIXEDSZ, dnptrs, lastdnptr)) < 0) return (-1);
140 cp += n;
chenbruce0d470422019-03-28 18:44:37 +0800141 *reinterpret_cast<uint16_t*>(cp) = htons(type);
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900142 cp += INT16SZ;
chenbruce0d470422019-03-28 18:44:37 +0800143 *reinterpret_cast<uint16_t*>(cp) = htons(cl);
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900144 cp += INT16SZ;
145 hp->qdcount = htons(1);
chenbrucec7d69272021-08-09 14:20:33 +0800146 if (op == QUERY || data.empty()) break;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900147 /*
148 * Make an additional record for completion domain.
149 */
150 if ((ep - cp) < RRFIXEDSZ) return (-1);
chenbrucec7d69272021-08-09 14:20:33 +0800151 n = dn_comp((const char*)data.data(), cp, ep - cp - RRFIXEDSZ, dnptrs, lastdnptr);
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900152 if (n < 0) return (-1);
153 cp += n;
chenbruce0d470422019-03-28 18:44:37 +0800154 *reinterpret_cast<uint16_t*>(cp) = htons(ns_t_null);
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900155 cp += INT16SZ;
chenbruce0d470422019-03-28 18:44:37 +0800156 *reinterpret_cast<uint16_t*>(cp) = htons(cl);
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900157 cp += INT16SZ;
chenbruce0d470422019-03-28 18:44:37 +0800158 *reinterpret_cast<uint32_t*>(cp) = htonl(0);
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900159 cp += INT32SZ;
chenbruce0d470422019-03-28 18:44:37 +0800160 *reinterpret_cast<uint16_t*>(cp) = htons(0);
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900161 cp += INT16SZ;
162 hp->arcount = htons(1);
163 break;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900164
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900165 case IQUERY:
166 /*
167 * Initialize answer section
168 */
chenbrucec7d69272021-08-09 14:20:33 +0800169 if (ep - cp < 1 + RRFIXEDSZ + data.size()) return (-1);
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900170 *cp++ = '\0'; /* no domain name */
chenbruce0d470422019-03-28 18:44:37 +0800171 *reinterpret_cast<uint16_t*>(cp) = htons(type);
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900172 cp += INT16SZ;
chenbruce0d470422019-03-28 18:44:37 +0800173 *reinterpret_cast<uint16_t*>(cp) = htons(cl);
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900174 cp += INT16SZ;
chenbruce0d470422019-03-28 18:44:37 +0800175 *reinterpret_cast<uint32_t*>(cp) = htonl(0);
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900176 cp += INT32SZ;
chenbrucec7d69272021-08-09 14:20:33 +0800177 *reinterpret_cast<uint16_t*>(cp) = htons(data.size());
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900178 cp += INT16SZ;
chenbrucec7d69272021-08-09 14:20:33 +0800179 if (data.size()) {
180 memcpy(cp, data.data(), data.size());
181 cp += data.size();
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900182 }
183 hp->ancount = htons(1);
184 break;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900185
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900186 default:
187 return (-1);
188 }
chenbrucec7d69272021-08-09 14:20:33 +0800189 return (cp - buf.data());
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900190}
191
Bernie Innocenti983f02e2021-07-01 07:36:56 +0900192int res_nopt(ResState* statp, int n0, /* current offset in buffer */
chenbrucec7d69272021-08-09 14:20:33 +0800193 std::span<uint8_t> buf, /* buffer to put query */
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900194 int anslen) /* UDP answer buffer size */
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900195{
chenbrucec7d69272021-08-09 14:20:33 +0800196 HEADER* hp = reinterpret_cast<HEADER*>(buf.data());
chenbrucec51f1212019-09-12 16:59:33 +0800197 uint8_t *cp, *ep;
198 uint16_t flags = 0;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900199
Bernie Innocenti3952ccc2019-03-03 19:39:53 +0900200 LOG(DEBUG) << __func__;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900201
chenbrucec7d69272021-08-09 14:20:33 +0800202 cp = buf.data() + n0;
203 ep = buf.data() + buf.size();
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900204
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900205 if ((ep - cp) < 1 + RRFIXEDSZ) return (-1);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900206
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900207 *cp++ = 0; /* "." */
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900208
Bernie Innocenti136af7b2018-10-01 20:46:20 +0900209 // Attach OPT pseudo-RR, as documented in RFC2671 (EDNS0).
chenbruce0d470422019-03-28 18:44:37 +0800210 *reinterpret_cast<uint16_t*>(cp) = htons(ns_t_opt); /* TYPE */
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900211 cp += INT16SZ;
212 if (anslen > 0xffff) anslen = 0xffff;
chenbruce0d470422019-03-28 18:44:37 +0800213 *reinterpret_cast<uint16_t*>(cp) = htons(anslen); /* CLASS = UDP payload size */
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900214 cp += INT16SZ;
215 *cp++ = NOERROR; /* extended RCODE */
216 *cp++ = 0; /* EDNS version */
chenbruced8cbb9b2019-06-20 18:25:28 +0800217 if (statp->netcontext_flags & NET_CONTEXT_FLAG_USE_DNS_OVER_TLS) {
Bernie Innocenti3952ccc2019-03-03 19:39:53 +0900218 LOG(DEBUG) << __func__ << ": ENDS0 DNSSEC";
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900219 flags |= NS_OPT_DNSSEC_OK;
220 }
chenbruce0d470422019-03-28 18:44:37 +0800221 *reinterpret_cast<uint16_t*>(cp) = htons(flags);
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900222 cp += INT16SZ;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900223
Bernie Innocenti136af7b2018-10-01 20:46:20 +0900224 // EDNS0 padding
chenbrucec7d69272021-08-09 14:20:33 +0800225 const uint16_t minlen = static_cast<uint16_t>(cp - buf.data()) + 3 * INT16SZ;
Bernie Innocenti136af7b2018-10-01 20:46:20 +0900226 const uint16_t extra = minlen % kEdns0Padding;
227 uint16_t padlen = (kEdns0Padding - extra) % kEdns0Padding;
chenbrucec7d69272021-08-09 14:20:33 +0800228 if (minlen > buf.size()) {
Bernie Innocenti136af7b2018-10-01 20:46:20 +0900229 return -1;
230 }
chenbrucec7d69272021-08-09 14:20:33 +0800231 padlen = std::min(padlen, static_cast<uint16_t>(buf.size() - minlen));
chenbruce0d470422019-03-28 18:44:37 +0800232 *reinterpret_cast<uint16_t*>(cp) = htons(padlen + 2 * INT16SZ); /* RDLEN */
Bernie Innocenti136af7b2018-10-01 20:46:20 +0900233 cp += INT16SZ;
chenbruce0d470422019-03-28 18:44:37 +0800234 *reinterpret_cast<uint16_t*>(cp) = htons(NS_OPT_PADDING); /* OPTION-CODE */
Bernie Innocenti136af7b2018-10-01 20:46:20 +0900235 cp += INT16SZ;
chenbruce0d470422019-03-28 18:44:37 +0800236 *reinterpret_cast<uint16_t*>(cp) = htons(padlen); /* OPTION-LENGTH */
Bernie Innocenti136af7b2018-10-01 20:46:20 +0900237 cp += INT16SZ;
238 memset(cp, 0, padlen);
239 cp += padlen;
240
241 hp->arcount = htons(ntohs(hp->arcount) + 1);
chenbrucec7d69272021-08-09 14:20:33 +0800242 return (cp - buf.data());
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900243}