blob: 2371c7e9df88aa91a6789560e6c403818846b2ef [file] [log] [blame]
Bernie Innocenti55864192018-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
Bernie Innocentid017e972019-03-03 19:39:53 +090073#define LOG_TAG "res_mkquery"
74
Bernie Innocenti1fbca5c2018-10-01 20:46:20 +090075#include <algorithm> // std::min()
76
Bernie Innocenti55864192018-08-30 04:05:20 +090077#include <arpa/nameser.h>
78#include <netdb.h>
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090079#include <netinet/in.h>
Bernie Innocenti55864192018-08-30 04:05:20 +090080#include <stdio.h>
81#include <string.h>
Bernie Innocenti2fd418e2018-08-30 12:04:03 +090082#include <sys/types.h>
83
chenbruce16adee42019-02-20 19:45:50 +080084#include <android-base/logging.h>
Bernie Innocenti55864192018-08-30 04:05:20 +090085
chenbruce16adee42019-02-20 19:45:50 +080086#include "resolv_private.h"
Bernie Innocenti55864192018-08-30 04:05:20 +090087
Bernie Innocenti1fbca5c2018-10-01 20:46:20 +090088// Queries will be padded to a multiple of this length when EDNS0 is active.
89constexpr uint16_t kEdns0Padding = 128;
90
Mike Yu69615f62018-11-06 15:42:36 +080091extern const char* const _res_opcodes[] = {
92 "QUERY", "IQUERY", "CQUERYM", "CQUERYU", /* experimental */
93 "NOTIFY", /* experimental */
94 "UPDATE", "6", "7", "8", "9", "10",
95 "11", "12", "13", "ZONEINIT", "ZONEREF",
96};
Bernie Innocenti55864192018-08-30 04:05:20 +090097
98/*
99 * Form all types of queries.
100 * Returns the size of the result or -1.
101 */
Bernie Innocentiee1b85b2018-09-25 14:23:19 +0900102int res_nmkquery(res_state statp, int op, /* opcode of query */
103 const char* dname, /* domain name */
104 int cl, int type, /* class and type of query */
105 const u_char* data, /* resource record data */
106 int datalen, /* length of data */
107 const u_char* /*newrr_in*/, /* new rr for modify or append */
108 u_char* buf, /* buffer to put query */
109 int buflen) /* size of buffer */
Bernie Innocenti55864192018-08-30 04:05:20 +0900110{
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900111 HEADER* hp;
112 u_char *cp, *ep;
113 int n;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900114 u_char *dnptrs[20], **dpp, **lastdnptr;
Bernie Innocenti55864192018-08-30 04:05:20 +0900115
Ken Chenbab50142019-03-19 17:41:28 +0800116 LOG(DEBUG) << __func__ << ": (" << _res_opcodes[op] << ", " << p_class(cl) << ", "
117 << p_type(type) << ")";
chenbruce16adee42019-02-20 19:45:50 +0800118
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900119 /*
120 * Initialize header fields.
121 */
122 if ((buf == NULL) || (buflen < HFIXEDSZ)) return (-1);
123 memset(buf, 0, HFIXEDSZ);
124 hp = (HEADER*) (void*) buf;
Luke Huang042e9422019-02-13 14:05:02 +0800125 hp->id = htons(arc4random_uniform(65536));
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900126 hp->opcode = op;
127 hp->rd = (statp->options & RES_RECURSE) != 0U;
128 hp->ad = (statp->options & RES_USE_DNSSEC) != 0U;
129 hp->rcode = NOERROR;
130 cp = buf + HFIXEDSZ;
131 ep = buf + buflen;
132 dpp = dnptrs;
133 *dpp++ = buf;
134 *dpp++ = NULL;
135 lastdnptr = dnptrs + sizeof dnptrs / sizeof dnptrs[0];
136 /*
137 * perform opcode specific processing
138 */
139 switch (op) {
Bernie Innocenti8bb94ba2018-10-10 22:30:12 +0900140 case QUERY:
141 [[fallthrough]];
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900142 case NS_NOTIFY_OP:
143 if (ep - cp < QFIXEDSZ) return (-1);
144 if ((n = dn_comp(dname, cp, ep - cp - QFIXEDSZ, dnptrs, lastdnptr)) < 0) return (-1);
145 cp += n;
chenbruce86a50bb2019-03-28 18:44:37 +0800146 *reinterpret_cast<uint16_t*>(cp) = htons(type);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900147 cp += INT16SZ;
chenbruce86a50bb2019-03-28 18:44:37 +0800148 *reinterpret_cast<uint16_t*>(cp) = htons(cl);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900149 cp += INT16SZ;
150 hp->qdcount = htons(1);
151 if (op == QUERY || data == NULL) break;
152 /*
153 * Make an additional record for completion domain.
154 */
155 if ((ep - cp) < RRFIXEDSZ) return (-1);
156 n = dn_comp((const char*) data, cp, ep - cp - RRFIXEDSZ, dnptrs, lastdnptr);
157 if (n < 0) return (-1);
158 cp += n;
chenbruce86a50bb2019-03-28 18:44:37 +0800159 *reinterpret_cast<uint16_t*>(cp) = htons(ns_t_null);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900160 cp += INT16SZ;
chenbruce86a50bb2019-03-28 18:44:37 +0800161 *reinterpret_cast<uint16_t*>(cp) = htons(cl);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900162 cp += INT16SZ;
chenbruce86a50bb2019-03-28 18:44:37 +0800163 *reinterpret_cast<uint32_t*>(cp) = htonl(0);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900164 cp += INT32SZ;
chenbruce86a50bb2019-03-28 18:44:37 +0800165 *reinterpret_cast<uint16_t*>(cp) = htons(0);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900166 cp += INT16SZ;
167 hp->arcount = htons(1);
168 break;
Bernie Innocenti55864192018-08-30 04:05:20 +0900169
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900170 case IQUERY:
171 /*
172 * Initialize answer section
173 */
174 if (ep - cp < 1 + RRFIXEDSZ + datalen) return (-1);
175 *cp++ = '\0'; /* no domain name */
chenbruce86a50bb2019-03-28 18:44:37 +0800176 *reinterpret_cast<uint16_t*>(cp) = htons(type);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900177 cp += INT16SZ;
chenbruce86a50bb2019-03-28 18:44:37 +0800178 *reinterpret_cast<uint16_t*>(cp) = htons(cl);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900179 cp += INT16SZ;
chenbruce86a50bb2019-03-28 18:44:37 +0800180 *reinterpret_cast<uint32_t*>(cp) = htonl(0);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900181 cp += INT32SZ;
chenbruce86a50bb2019-03-28 18:44:37 +0800182 *reinterpret_cast<uint16_t*>(cp) = htons(datalen);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900183 cp += INT16SZ;
184 if (datalen) {
185 memcpy(cp, data, (size_t) datalen);
186 cp += datalen;
187 }
188 hp->ancount = htons(1);
189 break;
Bernie Innocenti55864192018-08-30 04:05:20 +0900190
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900191 default:
192 return (-1);
193 }
194 return (cp - buf);
Bernie Innocenti55864192018-08-30 04:05:20 +0900195}
196
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900197int res_nopt(res_state statp, int n0, /* current offset in buffer */
198 u_char* buf, /* buffer to put query */
199 int buflen, /* size of buffer */
200 int anslen) /* UDP answer buffer size */
Bernie Innocenti55864192018-08-30 04:05:20 +0900201{
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900202 HEADER* hp;
203 u_char *cp, *ep;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900204 u_int16_t flags = 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900205
Bernie Innocentid017e972019-03-03 19:39:53 +0900206 LOG(DEBUG) << __func__;
Bernie Innocenti55864192018-08-30 04:05:20 +0900207
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900208 hp = (HEADER*) (void*) buf;
209 cp = buf + n0;
210 ep = buf + buflen;
Bernie Innocenti55864192018-08-30 04:05:20 +0900211
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900212 if ((ep - cp) < 1 + RRFIXEDSZ) return (-1);
Bernie Innocenti55864192018-08-30 04:05:20 +0900213
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900214 *cp++ = 0; /* "." */
Bernie Innocenti55864192018-08-30 04:05:20 +0900215
Bernie Innocenti1fbca5c2018-10-01 20:46:20 +0900216 // Attach OPT pseudo-RR, as documented in RFC2671 (EDNS0).
chenbruce86a50bb2019-03-28 18:44:37 +0800217 *reinterpret_cast<uint16_t*>(cp) = htons(ns_t_opt); /* TYPE */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900218 cp += INT16SZ;
219 if (anslen > 0xffff) anslen = 0xffff;
chenbruce86a50bb2019-03-28 18:44:37 +0800220 *reinterpret_cast<uint16_t*>(cp) = htons(anslen); /* CLASS = UDP payload size */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900221 cp += INT16SZ;
222 *cp++ = NOERROR; /* extended RCODE */
223 *cp++ = 0; /* EDNS version */
224 if (statp->options & RES_USE_DNSSEC) {
Bernie Innocentid017e972019-03-03 19:39:53 +0900225 LOG(DEBUG) << __func__ << ": ENDS0 DNSSEC";
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900226 flags |= NS_OPT_DNSSEC_OK;
227 }
chenbruce86a50bb2019-03-28 18:44:37 +0800228 *reinterpret_cast<uint16_t*>(cp) = htons(flags);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900229 cp += INT16SZ;
Bernie Innocenti55864192018-08-30 04:05:20 +0900230
Bernie Innocenti1fbca5c2018-10-01 20:46:20 +0900231 // EDNS0 padding
232 const uint16_t minlen = static_cast<uint16_t>(cp - buf) + 3 * INT16SZ;
233 const uint16_t extra = minlen % kEdns0Padding;
234 uint16_t padlen = (kEdns0Padding - extra) % kEdns0Padding;
235 if (minlen > buflen) {
236 return -1;
237 }
238 padlen = std::min(padlen, static_cast<uint16_t>(buflen - minlen));
chenbruce86a50bb2019-03-28 18:44:37 +0800239 *reinterpret_cast<uint16_t*>(cp) = htons(padlen + 2 * INT16SZ); /* RDLEN */
Bernie Innocenti1fbca5c2018-10-01 20:46:20 +0900240 cp += INT16SZ;
chenbruce86a50bb2019-03-28 18:44:37 +0800241 *reinterpret_cast<uint16_t*>(cp) = htons(NS_OPT_PADDING); /* OPTION-CODE */
Bernie Innocenti1fbca5c2018-10-01 20:46:20 +0900242 cp += INT16SZ;
chenbruce86a50bb2019-03-28 18:44:37 +0800243 *reinterpret_cast<uint16_t*>(cp) = htons(padlen); /* OPTION-LENGTH */
Bernie Innocenti1fbca5c2018-10-01 20:46:20 +0900244 cp += INT16SZ;
245 memset(cp, 0, padlen);
246 cp += padlen;
247
248 hp->arcount = htons(ntohs(hp->arcount) + 1);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900249 return (cp - buf);
Bernie Innocenti55864192018-08-30 04:05:20 +0900250}