/src/bind9/lib/dns/rdata/generic/opt_41.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright (C) Internet Systems Consortium, Inc. ("ISC") |
3 | | * |
4 | | * SPDX-License-Identifier: MPL-2.0 |
5 | | * |
6 | | * This Source Code Form is subject to the terms of the Mozilla Public |
7 | | * License, v. 2.0. If a copy of the MPL was not distributed with this |
8 | | * file, you can obtain one at https://mozilla.org/MPL/2.0/. |
9 | | * |
10 | | * See the COPYRIGHT file distributed with this work for additional |
11 | | * information regarding copyright ownership. |
12 | | */ |
13 | | |
14 | | /* RFC2671 */ |
15 | | |
16 | | #ifndef RDATA_GENERIC_OPT_41_C |
17 | | #define RDATA_GENERIC_OPT_41_C |
18 | | |
19 | | #define RRTYPE_OPT_ATTRIBUTES \ |
20 | 28.2k | (DNS_RDATATYPEATTR_SINGLETON | DNS_RDATATYPEATTR_META | \ |
21 | 28.2k | DNS_RDATATYPEATTR_NOTQUESTION) |
22 | | |
23 | | #include <isc/utf8.h> |
24 | | |
25 | | static inline isc_result_t |
26 | 2 | fromtext_opt(ARGS_FROMTEXT) { |
27 | | /* |
28 | | * OPT records do not have a text format. |
29 | | */ |
30 | | |
31 | 2 | REQUIRE(type == dns_rdatatype_opt); |
32 | | |
33 | 2 | UNUSED(type); |
34 | 2 | UNUSED(rdclass); |
35 | 2 | UNUSED(lexer); |
36 | 2 | UNUSED(origin); |
37 | 2 | UNUSED(options); |
38 | 2 | UNUSED(target); |
39 | 2 | UNUSED(callbacks); |
40 | | |
41 | 2 | return (ISC_R_NOTIMPLEMENTED); |
42 | 2 | } |
43 | | |
44 | | static inline isc_result_t |
45 | 11.5k | totext_opt(ARGS_TOTEXT) { |
46 | 11.5k | isc_region_t r; |
47 | 11.5k | isc_region_t or ; |
48 | 11.5k | uint16_t option; |
49 | 11.5k | uint16_t length; |
50 | 11.5k | char buf[sizeof("64000 64000")]; |
51 | | |
52 | | /* |
53 | | * OPT records do not have a text format. |
54 | | */ |
55 | | |
56 | 11.5k | REQUIRE(rdata->type == dns_rdatatype_opt); |
57 | | |
58 | 0 | dns_rdata_toregion(rdata, &r); |
59 | 25.3k | while (r.length > 0) { |
60 | 13.8k | option = uint16_fromregion(&r); |
61 | 13.8k | isc_region_consume(&r, 2); |
62 | 0 | length = uint16_fromregion(&r); |
63 | 13.8k | isc_region_consume(&r, 2); |
64 | 0 | snprintf(buf, sizeof(buf), "%u %u", option, length); |
65 | 13.8k | RETERR(str_totext(buf, target)); |
66 | 13.8k | INSIST(r.length >= length); |
67 | 13.8k | if (length > 0) { |
68 | 7.79k | if ((tctx->flags & DNS_STYLEFLAG_MULTILINE) != 0) { |
69 | 0 | RETERR(str_totext(" (", target)); |
70 | 0 | } |
71 | 7.79k | RETERR(str_totext(tctx->linebreak, target)); |
72 | 7.79k | or = r; |
73 | 7.79k | or.length = length; |
74 | 7.79k | if (tctx->width == 0) { /* No splitting */ |
75 | 0 | RETERR(isc_base64_totext(& or, 60, "", target)); |
76 | 7.79k | } else { |
77 | 7.79k | RETERR(isc_base64_totext(& or, tctx->width - 2, |
78 | 7.79k | tctx->linebreak, |
79 | 7.79k | target)); |
80 | 7.79k | } |
81 | 7.79k | isc_region_consume(&r, length); |
82 | 7.79k | if ((tctx->flags & DNS_STYLEFLAG_MULTILINE) != 0) { |
83 | 0 | RETERR(str_totext(" )", target)); |
84 | 0 | } |
85 | 7.79k | } |
86 | 13.8k | if (r.length > 0) { |
87 | 9.26k | RETERR(str_totext(" ", target)); |
88 | 9.26k | } |
89 | 13.8k | } |
90 | | |
91 | 11.5k | return (ISC_R_SUCCESS); |
92 | 11.5k | } |
93 | | |
94 | | static inline isc_result_t |
95 | 17.8k | fromwire_opt(ARGS_FROMWIRE) { |
96 | 17.8k | isc_region_t sregion; |
97 | 17.8k | isc_region_t tregion; |
98 | 17.8k | uint16_t opt; |
99 | 17.8k | uint16_t length; |
100 | 17.8k | unsigned int total; |
101 | | |
102 | 17.8k | REQUIRE(type == dns_rdatatype_opt); |
103 | | |
104 | 17.8k | UNUSED(type); |
105 | 17.8k | UNUSED(rdclass); |
106 | 17.8k | UNUSED(dctx); |
107 | 17.8k | UNUSED(options); |
108 | | |
109 | 17.8k | isc_buffer_activeregion(source, &sregion); |
110 | 17.8k | if (sregion.length == 0) { |
111 | 9.54k | return (ISC_R_SUCCESS); |
112 | 9.54k | } |
113 | 8.34k | total = 0; |
114 | 361k | while (sregion.length != 0) { |
115 | 352k | if (sregion.length < 4) { |
116 | 8 | return (ISC_R_UNEXPECTEDEND); |
117 | 8 | } |
118 | 352k | opt = uint16_fromregion(&sregion); |
119 | 352k | isc_region_consume(&sregion, 2); |
120 | 0 | length = uint16_fromregion(&sregion); |
121 | 352k | isc_region_consume(&sregion, 2); |
122 | 0 | total += 4; |
123 | 352k | if (sregion.length < length) { |
124 | 21 | return (ISC_R_UNEXPECTEDEND); |
125 | 21 | } |
126 | 352k | switch (opt) { |
127 | 1.63k | case DNS_OPT_LLQ: |
128 | 1.63k | if (length != 18U) { |
129 | 6 | return (DNS_R_OPTERR); |
130 | 6 | } |
131 | 1.62k | isc_region_consume(&sregion, length); |
132 | 0 | break; |
133 | 16.6k | case DNS_OPT_CLIENT_SUBNET: { |
134 | 16.6k | uint16_t family; |
135 | 16.6k | uint8_t addrlen; |
136 | 16.6k | uint8_t scope; |
137 | 16.6k | uint8_t addrbytes; |
138 | | |
139 | 16.6k | if (length < 4) { |
140 | 2 | return (DNS_R_OPTERR); |
141 | 2 | } |
142 | 16.6k | family = uint16_fromregion(&sregion); |
143 | 16.6k | isc_region_consume(&sregion, 2); |
144 | 0 | addrlen = uint8_fromregion(&sregion); |
145 | 16.6k | isc_region_consume(&sregion, 1); |
146 | 0 | scope = uint8_fromregion(&sregion); |
147 | 16.6k | isc_region_consume(&sregion, 1); |
148 | | |
149 | 0 | switch (family) { |
150 | 9.13k | case 0: |
151 | | /* |
152 | | * XXXMUKS: In queries and replies, if |
153 | | * FAMILY is set to 0, SOURCE |
154 | | * PREFIX-LENGTH and SCOPE PREFIX-LENGTH |
155 | | * must be 0 and ADDRESS should not be |
156 | | * present as the address and prefix |
157 | | * lengths don't make sense because the |
158 | | * family is unknown. |
159 | | */ |
160 | 9.13k | if (addrlen != 0U || scope != 0U) { |
161 | 10 | return (DNS_R_OPTERR); |
162 | 10 | } |
163 | 9.12k | break; |
164 | 9.12k | case 1: |
165 | 5.01k | if (addrlen > 32U || scope > 32U) { |
166 | 2 | return (DNS_R_OPTERR); |
167 | 2 | } |
168 | 5.01k | break; |
169 | 5.01k | case 2: |
170 | 2.51k | if (addrlen > 128U || scope > 128U) { |
171 | 2 | return (DNS_R_OPTERR); |
172 | 2 | } |
173 | 2.50k | break; |
174 | 2.50k | default: |
175 | 5 | return (DNS_R_OPTERR); |
176 | 16.6k | } |
177 | 16.6k | addrbytes = (addrlen + 7) / 8; |
178 | 16.6k | if (addrbytes + 4 != length) { |
179 | 8 | return (DNS_R_OPTERR); |
180 | 8 | } |
181 | | |
182 | 16.6k | if (addrbytes != 0U && (addrlen % 8) != 0) { |
183 | 774 | uint8_t bits = ~0U << (8 - (addrlen % 8)); |
184 | 774 | bits &= sregion.base[addrbytes - 1]; |
185 | 774 | if (bits != sregion.base[addrbytes - 1]) { |
186 | 2 | return (DNS_R_OPTERR); |
187 | 2 | } |
188 | 774 | } |
189 | 16.6k | isc_region_consume(&sregion, addrbytes); |
190 | 0 | break; |
191 | 16.6k | } |
192 | 32.5k | case DNS_OPT_EXPIRE: |
193 | | /* |
194 | | * Request has zero length. Response is 32 bits. |
195 | | */ |
196 | 32.5k | if (length != 0 && length != 4) { |
197 | 1 | return (DNS_R_OPTERR); |
198 | 1 | } |
199 | 32.5k | isc_region_consume(&sregion, length); |
200 | 0 | break; |
201 | 6.07k | case DNS_OPT_COOKIE: |
202 | | /* |
203 | | * Client cookie alone has length 8. |
204 | | * Client + server cookie is 8 + [8..32]. |
205 | | */ |
206 | 6.07k | if (length != 8 && (length < 16 || length > 40)) { |
207 | 5 | return (DNS_R_OPTERR); |
208 | 5 | } |
209 | 6.07k | isc_region_consume(&sregion, length); |
210 | 0 | break; |
211 | 5.41k | case DNS_OPT_KEY_TAG: |
212 | 5.41k | if (length == 0 || (length % 2) != 0) { |
213 | 7 | return (DNS_R_OPTERR); |
214 | 7 | } |
215 | 5.40k | isc_region_consume(&sregion, length); |
216 | 0 | break; |
217 | 18.5k | case DNS_OPT_EDE: |
218 | 18.5k | if (length < 2) { |
219 | 1 | return (DNS_R_OPTERR); |
220 | 1 | } |
221 | | /* UTF-8 Byte Order Mark is not permitted. RFC 5198 */ |
222 | 18.5k | if (isc_utf8_bom(sregion.base + 2, length - 2)) { |
223 | 1 | return (DNS_R_OPTERR); |
224 | 1 | } |
225 | | /* |
226 | | * The EXTRA-TEXT field is specified as UTF-8, and |
227 | | * therefore must be validated for correctness |
228 | | * according to RFC 3269 security considerations. |
229 | | */ |
230 | 18.5k | if (!isc_utf8_valid(sregion.base + 2, length - 2)) { |
231 | 83 | return (DNS_R_OPTERR); |
232 | 83 | } |
233 | 18.4k | isc_region_consume(&sregion, length); |
234 | 0 | break; |
235 | 1.98k | case DNS_OPT_CLIENT_TAG: |
236 | | /* FALLTHROUGH */ |
237 | 4.88k | case DNS_OPT_SERVER_TAG: |
238 | 4.88k | if (length != 2) { |
239 | 3 | return (DNS_R_OPTERR); |
240 | 3 | } |
241 | 4.88k | isc_region_consume(&sregion, length); |
242 | 0 | break; |
243 | 267k | default: |
244 | 267k | isc_region_consume(&sregion, length); |
245 | 0 | break; |
246 | 352k | } |
247 | 352k | total += length; |
248 | 352k | } |
249 | | |
250 | 8.17k | isc_buffer_activeregion(source, &sregion); |
251 | 8.17k | isc_buffer_availableregion(target, &tregion); |
252 | 8.17k | if (tregion.length < total) { |
253 | 626 | return (ISC_R_NOSPACE); |
254 | 626 | } |
255 | 7.55k | memmove(tregion.base, sregion.base, total); |
256 | 7.55k | isc_buffer_forward(source, total); |
257 | 7.55k | isc_buffer_add(target, total); |
258 | | |
259 | 7.55k | return (ISC_R_SUCCESS); |
260 | 8.17k | } |
261 | | |
262 | | static inline isc_result_t |
263 | 8.57k | towire_opt(ARGS_TOWIRE) { |
264 | 8.57k | REQUIRE(rdata->type == dns_rdatatype_opt); |
265 | | |
266 | 8.57k | UNUSED(cctx); |
267 | | |
268 | 8.57k | return (mem_tobuffer(target, rdata->data, rdata->length)); |
269 | 8.57k | } |
270 | | |
271 | | static inline int |
272 | 8.05k | compare_opt(ARGS_COMPARE) { |
273 | 8.05k | isc_region_t r1; |
274 | 8.05k | isc_region_t r2; |
275 | | |
276 | 8.05k | REQUIRE(rdata1->type == rdata2->type); |
277 | 8.05k | REQUIRE(rdata1->rdclass == rdata2->rdclass); |
278 | 8.05k | REQUIRE(rdata1->type == dns_rdatatype_opt); |
279 | | |
280 | 0 | dns_rdata_toregion(rdata1, &r1); |
281 | 8.05k | dns_rdata_toregion(rdata2, &r2); |
282 | 8.05k | return (isc_region_compare(&r1, &r2)); |
283 | 8.05k | } |
284 | | |
285 | | static inline isc_result_t |
286 | 0 | fromstruct_opt(ARGS_FROMSTRUCT) { |
287 | 0 | dns_rdata_opt_t *opt = source; |
288 | 0 | isc_region_t region; |
289 | 0 | uint16_t length; |
290 | |
|
291 | 0 | REQUIRE(type == dns_rdatatype_opt); |
292 | 0 | REQUIRE(opt != NULL); |
293 | 0 | REQUIRE(opt->common.rdtype == type); |
294 | 0 | REQUIRE(opt->common.rdclass == rdclass); |
295 | 0 | REQUIRE(opt->options != NULL || opt->length == 0); |
296 | | |
297 | 0 | UNUSED(type); |
298 | 0 | UNUSED(rdclass); |
299 | |
|
300 | 0 | region.base = opt->options; |
301 | 0 | region.length = opt->length; |
302 | 0 | while (region.length >= 4) { |
303 | 0 | isc_region_consume(®ion, 2); /* opt */ |
304 | 0 | length = uint16_fromregion(®ion); |
305 | 0 | isc_region_consume(®ion, 2); |
306 | 0 | if (region.length < length) { |
307 | 0 | return (ISC_R_UNEXPECTEDEND); |
308 | 0 | } |
309 | 0 | isc_region_consume(®ion, length); |
310 | 0 | } |
311 | 0 | if (region.length != 0) { |
312 | 0 | return (ISC_R_UNEXPECTEDEND); |
313 | 0 | } |
314 | | |
315 | 0 | return (mem_tobuffer(target, opt->options, opt->length)); |
316 | 0 | } |
317 | | |
318 | | static inline isc_result_t |
319 | 0 | tostruct_opt(ARGS_TOSTRUCT) { |
320 | 0 | dns_rdata_opt_t *opt = target; |
321 | 0 | isc_region_t r; |
322 | |
|
323 | 0 | REQUIRE(rdata->type == dns_rdatatype_opt); |
324 | 0 | REQUIRE(opt != NULL); |
325 | | |
326 | 0 | opt->common.rdclass = rdata->rdclass; |
327 | 0 | opt->common.rdtype = rdata->type; |
328 | 0 | ISC_LINK_INIT(&opt->common, link); |
329 | |
|
330 | 0 | dns_rdata_toregion(rdata, &r); |
331 | 0 | opt->length = r.length; |
332 | 0 | opt->options = mem_maybedup(mctx, r.base, r.length); |
333 | 0 | opt->offset = 0; |
334 | 0 | opt->mctx = mctx; |
335 | 0 | return (ISC_R_SUCCESS); |
336 | 0 | } |
337 | | |
338 | | static inline void |
339 | 0 | freestruct_opt(ARGS_FREESTRUCT) { |
340 | 0 | dns_rdata_opt_t *opt = source; |
341 | |
|
342 | 0 | REQUIRE(opt != NULL); |
343 | 0 | REQUIRE(opt->common.rdtype == dns_rdatatype_opt); |
344 | | |
345 | 0 | if (opt->mctx == NULL) { |
346 | 0 | return; |
347 | 0 | } |
348 | | |
349 | 0 | if (opt->options != NULL) { |
350 | 0 | isc_mem_free(opt->mctx, opt->options); |
351 | 0 | } |
352 | 0 | opt->mctx = NULL; |
353 | 0 | } |
354 | | |
355 | | static inline isc_result_t |
356 | 0 | additionaldata_opt(ARGS_ADDLDATA) { |
357 | 0 | REQUIRE(rdata->type == dns_rdatatype_opt); |
358 | | |
359 | 0 | UNUSED(rdata); |
360 | 0 | UNUSED(owner); |
361 | 0 | UNUSED(add); |
362 | 0 | UNUSED(arg); |
363 | |
|
364 | 0 | return (ISC_R_SUCCESS); |
365 | 0 | } |
366 | | |
367 | | static inline isc_result_t |
368 | 0 | digest_opt(ARGS_DIGEST) { |
369 | | /* |
370 | | * OPT records are not digested. |
371 | | */ |
372 | |
|
373 | 0 | REQUIRE(rdata->type == dns_rdatatype_opt); |
374 | | |
375 | 0 | UNUSED(rdata); |
376 | 0 | UNUSED(digest); |
377 | 0 | UNUSED(arg); |
378 | |
|
379 | 0 | return (ISC_R_NOTIMPLEMENTED); |
380 | 0 | } |
381 | | |
382 | | static inline bool |
383 | 0 | checkowner_opt(ARGS_CHECKOWNER) { |
384 | 0 | REQUIRE(type == dns_rdatatype_opt); |
385 | | |
386 | 0 | UNUSED(type); |
387 | 0 | UNUSED(rdclass); |
388 | 0 | UNUSED(wildcard); |
389 | |
|
390 | 0 | return (dns_name_equal(name, dns_rootname)); |
391 | 0 | } |
392 | | |
393 | | static inline bool |
394 | 0 | checknames_opt(ARGS_CHECKNAMES) { |
395 | 0 | REQUIRE(rdata->type == dns_rdatatype_opt); |
396 | | |
397 | 0 | UNUSED(rdata); |
398 | 0 | UNUSED(owner); |
399 | 0 | UNUSED(bad); |
400 | |
|
401 | 0 | return (true); |
402 | 0 | } |
403 | | |
404 | | static inline int |
405 | 0 | casecompare_opt(ARGS_COMPARE) { |
406 | 0 | return (compare_opt(rdata1, rdata2)); |
407 | 0 | } |
408 | | |
409 | | isc_result_t |
410 | 0 | dns_rdata_opt_first(dns_rdata_opt_t *opt) { |
411 | 0 | REQUIRE(opt != NULL); |
412 | 0 | REQUIRE(opt->common.rdtype == dns_rdatatype_opt); |
413 | 0 | REQUIRE(opt->options != NULL || opt->length == 0); |
414 | | |
415 | 0 | if (opt->length == 0) { |
416 | 0 | return (ISC_R_NOMORE); |
417 | 0 | } |
418 | | |
419 | 0 | opt->offset = 0; |
420 | 0 | return (ISC_R_SUCCESS); |
421 | 0 | } |
422 | | |
423 | | isc_result_t |
424 | 0 | dns_rdata_opt_next(dns_rdata_opt_t *opt) { |
425 | 0 | isc_region_t r; |
426 | 0 | uint16_t length; |
427 | |
|
428 | 0 | REQUIRE(opt != NULL); |
429 | 0 | REQUIRE(opt->common.rdtype == dns_rdatatype_opt); |
430 | 0 | REQUIRE(opt->options != NULL && opt->length != 0); |
431 | 0 | REQUIRE(opt->offset < opt->length); |
432 | | |
433 | 0 | INSIST(opt->offset + 4 <= opt->length); |
434 | 0 | r.base = opt->options + opt->offset + 2; |
435 | 0 | r.length = opt->length - opt->offset - 2; |
436 | 0 | length = uint16_fromregion(&r); |
437 | 0 | INSIST(opt->offset + 4 + length <= opt->length); |
438 | 0 | opt->offset = opt->offset + 4 + length; |
439 | 0 | if (opt->offset == opt->length) { |
440 | 0 | return (ISC_R_NOMORE); |
441 | 0 | } |
442 | 0 | return (ISC_R_SUCCESS); |
443 | 0 | } |
444 | | |
445 | | isc_result_t |
446 | 0 | dns_rdata_opt_current(dns_rdata_opt_t *opt, dns_rdata_opt_opcode_t *opcode) { |
447 | 0 | isc_region_t r; |
448 | |
|
449 | 0 | REQUIRE(opt != NULL); |
450 | 0 | REQUIRE(opcode != NULL); |
451 | 0 | REQUIRE(opt->common.rdtype == dns_rdatatype_opt); |
452 | 0 | REQUIRE(opt->options != NULL); |
453 | 0 | REQUIRE(opt->offset < opt->length); |
454 | | |
455 | 0 | INSIST(opt->offset + 4 <= opt->length); |
456 | 0 | r.base = opt->options + opt->offset; |
457 | 0 | r.length = opt->length - opt->offset; |
458 | |
|
459 | 0 | opcode->opcode = uint16_fromregion(&r); |
460 | 0 | isc_region_consume(&r, 2); |
461 | 0 | opcode->length = uint16_fromregion(&r); |
462 | 0 | isc_region_consume(&r, 2); |
463 | 0 | opcode->data = r.base; |
464 | 0 | INSIST(opt->offset + 4 + opcode->length <= opt->length); |
465 | | |
466 | 0 | return (ISC_R_SUCCESS); |
467 | 0 | } |
468 | | |
469 | | #endif /* RDATA_GENERIC_OPT_41_C */ |