1 /* $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
73 #define LOG_TAG "resolv"
74
75 #include <algorithm> // std::min()
76
77 #include <arpa/nameser.h>
78 #include <netdb.h>
79 #include <netinet/in.h>
80 #include <string.h>
81
82 #include <android-base/logging.h>
83 #include <netd_resolv/resolv.h> // NET_CONTEXT_FLAG_USE_DNS_OVER_TLS
84
85 #include "res_comp.h"
86 #include "res_debug.h"
87 #include "resolv_private.h" // ResState*
88
89 // Queries will be padded to a multiple of this length when EDNS0 is active.
90 constexpr uint16_t kEdns0Padding = 128;
91
92 extern 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 };
98
99 // Form all types of queries. Returns the size of the result or -1.
res_nmkquery(int op,const char * dname,int cl,int type,std::span<const uint8_t> data,std::span<uint8_t> buf,int netcontext_flags)100 int 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
105 int netcontext_flags) {
106 HEADER* hp;
107 uint8_t *cp, *ep;
108 int n;
109 uint8_t *dnptrs[20], **dpp, **lastdnptr;
110
111 LOG(INFO) << __func__ << ": (" << _res_opcodes[op] << ", " << p_class(cl) << ", "
112 << p_type(type) << ")";
113
114 /*
115 * Initialize header fields.
116 */
117 if (buf.empty() || (buf.size() < HFIXEDSZ)) return (-1);
118 memset(buf.data(), 0, HFIXEDSZ);
119 hp = (HEADER*)(void*)buf.data();
120 hp->id = htons(arc4random_uniform(65536));
121 hp->opcode = op;
122 hp->rd = true;
123 hp->ad = (netcontext_flags & NET_CONTEXT_FLAG_USE_DNS_OVER_TLS) != 0U;
124 hp->rcode = NOERROR;
125 cp = buf.data() + HFIXEDSZ;
126 ep = buf.data() + buf.size();
127 dpp = dnptrs;
128 *dpp++ = buf.data();
129 *dpp++ = NULL;
130 lastdnptr = dnptrs + sizeof dnptrs / sizeof dnptrs[0];
131 /*
132 * perform opcode specific processing
133 */
134 switch (op) {
135 case QUERY:
136 [[fallthrough]];
137 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;
141 *reinterpret_cast<uint16_t*>(cp) = htons(type);
142 cp += INT16SZ;
143 *reinterpret_cast<uint16_t*>(cp) = htons(cl);
144 cp += INT16SZ;
145 hp->qdcount = htons(1);
146 if (op == QUERY || data.empty()) break;
147 /*
148 * Make an additional record for completion domain.
149 */
150 if ((ep - cp) < RRFIXEDSZ) return (-1);
151 n = dn_comp((const char*)data.data(), cp, ep - cp - RRFIXEDSZ, dnptrs, lastdnptr);
152 if (n < 0) return (-1);
153 cp += n;
154 *reinterpret_cast<uint16_t*>(cp) = htons(ns_t_null);
155 cp += INT16SZ;
156 *reinterpret_cast<uint16_t*>(cp) = htons(cl);
157 cp += INT16SZ;
158 *reinterpret_cast<uint32_t*>(cp) = htonl(0);
159 cp += INT32SZ;
160 *reinterpret_cast<uint16_t*>(cp) = htons(0);
161 cp += INT16SZ;
162 hp->arcount = htons(1);
163 break;
164
165 case IQUERY:
166 /*
167 * Initialize answer section
168 */
169 if (ep - cp < static_cast<ptrdiff_t>(1 + RRFIXEDSZ + data.size())) return (-1);
170 *cp++ = '\0'; /* no domain name */
171 *reinterpret_cast<uint16_t*>(cp) = htons(type);
172 cp += INT16SZ;
173 *reinterpret_cast<uint16_t*>(cp) = htons(cl);
174 cp += INT16SZ;
175 *reinterpret_cast<uint32_t*>(cp) = htonl(0);
176 cp += INT32SZ;
177 *reinterpret_cast<uint16_t*>(cp) = htons(data.size());
178 cp += INT16SZ;
179 if (data.size()) {
180 memcpy(cp, data.data(), data.size());
181 cp += data.size();
182 }
183 hp->ancount = htons(1);
184 break;
185
186 default:
187 return (-1);
188 }
189 return (cp - buf.data());
190 }
191
res_nopt(ResState * statp,int n0,std::span<uint8_t> buf,int anslen)192 int res_nopt(ResState* statp, int n0, /* current offset in buffer */
193 std::span<uint8_t> buf, /* buffer to put query */
194 int anslen) /* UDP answer buffer size */
195 {
196 HEADER* hp = reinterpret_cast<HEADER*>(buf.data());
197 uint8_t *cp, *ep;
198 uint16_t flags = 0;
199
200 LOG(DEBUG) << __func__;
201
202 cp = buf.data() + n0;
203 ep = buf.data() + buf.size();
204
205 if ((ep - cp) < 1 + RRFIXEDSZ) return (-1);
206
207 *cp++ = 0; /* "." */
208
209 // Attach OPT pseudo-RR, as documented in RFC2671 (EDNS0).
210 *reinterpret_cast<uint16_t*>(cp) = htons(ns_t_opt); /* TYPE */
211 cp += INT16SZ;
212 if (anslen > 0xffff) anslen = 0xffff;
213 *reinterpret_cast<uint16_t*>(cp) = htons(anslen); /* CLASS = UDP payload size */
214 cp += INT16SZ;
215 *cp++ = NOERROR; /* extended RCODE */
216 *cp++ = 0; /* EDNS version */
217 if (statp->netcontext_flags & NET_CONTEXT_FLAG_USE_DNS_OVER_TLS) {
218 LOG(DEBUG) << __func__ << ": ENDS0 DNSSEC";
219 flags |= NS_OPT_DNSSEC_OK;
220 }
221 *reinterpret_cast<uint16_t*>(cp) = htons(flags);
222 cp += INT16SZ;
223
224 // EDNS0 padding
225 const uint16_t minlen = static_cast<uint16_t>(cp - buf.data()) + 3 * INT16SZ;
226 const uint16_t extra = minlen % kEdns0Padding;
227 uint16_t padlen = (kEdns0Padding - extra) % kEdns0Padding;
228 if (minlen > buf.size()) {
229 return -1;
230 }
231 padlen = std::min(padlen, static_cast<uint16_t>(buf.size() - minlen));
232 *reinterpret_cast<uint16_t*>(cp) = htons(padlen + 2 * INT16SZ); /* RDLEN */
233 cp += INT16SZ;
234 *reinterpret_cast<uint16_t*>(cp) = htons(NS_OPT_PADDING); /* OPTION-CODE */
235 cp += INT16SZ;
236 *reinterpret_cast<uint16_t*>(cp) = htons(padlen); /* OPTION-LENGTH */
237 cp += INT16SZ;
238 memset(cp, 0, padlen);
239 cp += padlen;
240
241 hp->arcount = htons(ntohs(hp->arcount) + 1);
242 return (cp - buf.data());
243 }
244