rpz.c revision 1.17 1 1.16 christos /* $NetBSD: rpz.c,v 1.17 2026/01/29 18:37:50 christos Exp $ */
2 1.1 christos
3 1.1 christos /*
4 1.1 christos * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
5 1.1 christos *
6 1.11 christos * SPDX-License-Identifier: MPL-2.0
7 1.11 christos *
8 1.1 christos * This Source Code Form is subject to the terms of the Mozilla Public
9 1.1 christos * License, v. 2.0. If a copy of the MPL was not distributed with this
10 1.9 christos * file, you can obtain one at https://mozilla.org/MPL/2.0/.
11 1.1 christos *
12 1.1 christos * See the COPYRIGHT file distributed with this work for additional
13 1.1 christos * information regarding copyright ownership.
14 1.1 christos */
15 1.1 christos
16 1.1 christos /*! \file */
17 1.1 christos
18 1.3 christos #include <inttypes.h>
19 1.3 christos #include <stdbool.h>
20 1.16 christos #include <stdint.h>
21 1.3 christos #include <stdlib.h>
22 1.3 christos
23 1.16 christos #include <isc/async.h>
24 1.1 christos #include <isc/buffer.h>
25 1.16 christos #include <isc/loop.h>
26 1.15 christos #include <isc/magic.h>
27 1.1 christos #include <isc/mem.h>
28 1.1 christos #include <isc/net.h>
29 1.1 christos #include <isc/netaddr.h>
30 1.16 christos #include <isc/refcount.h>
31 1.15 christos #include <isc/result.h>
32 1.1 christos #include <isc/rwlock.h>
33 1.1 christos #include <isc/string.h>
34 1.1 christos #include <isc/util.h>
35 1.17 christos #include <isc/uv.h>
36 1.16 christos #include <isc/work.h>
37 1.1 christos
38 1.1 christos #include <dns/db.h>
39 1.1 christos #include <dns/dbiterator.h>
40 1.1 christos #include <dns/dnsrps.h>
41 1.1 christos #include <dns/fixedname.h>
42 1.1 christos #include <dns/log.h>
43 1.16 christos #include <dns/qp.h>
44 1.1 christos #include <dns/rdata.h>
45 1.1 christos #include <dns/rdataset.h>
46 1.7 christos #include <dns/rdatasetiter.h>
47 1.1 christos #include <dns/rdatastruct.h>
48 1.1 christos #include <dns/rpz.h>
49 1.1 christos #include <dns/view.h>
50 1.1 christos
51 1.15 christos #define DNS_RPZ_ZONE_MAGIC ISC_MAGIC('r', 'p', 'z', ' ')
52 1.15 christos #define DNS_RPZ_ZONES_MAGIC ISC_MAGIC('r', 'p', 'z', 's')
53 1.15 christos
54 1.15 christos #define DNS_RPZ_ZONE_VALID(rpz) ISC_MAGIC_VALID(rpz, DNS_RPZ_ZONE_MAGIC)
55 1.15 christos #define DNS_RPZ_ZONES_VALID(rpzs) ISC_MAGIC_VALID(rpzs, DNS_RPZ_ZONES_MAGIC)
56 1.15 christos
57 1.1 christos /*
58 1.1 christos * Parallel radix trees for databases of response policy IP addresses
59 1.1 christos *
60 1.1 christos * The radix or patricia trees are somewhat specialized to handle response
61 1.1 christos * policy addresses by representing the two sets of IP addresses and name
62 1.1 christos * server IP addresses in a single tree. One set of IP addresses is
63 1.1 christos * for rpz-ip policies or policies triggered by addresses in A or
64 1.1 christos * AAAA records in responses.
65 1.1 christos * The second set is for rpz-nsip policies or policies triggered by addresses
66 1.1 christos * in A or AAAA records for NS records that are authorities for responses.
67 1.1 christos *
68 1.1 christos * Each leaf indicates that an IP address is listed in the IP address or the
69 1.1 christos * name server IP address policy sub-zone (or both) of the corresponding
70 1.1 christos * response policy zone. The policy data such as a CNAME or an A record
71 1.1 christos * is kept in the policy zone. After an IP address has been found in a radix
72 1.1 christos * tree, the node in the policy zone's database is found by converting
73 1.1 christos * the IP address to a domain name in a canonical form.
74 1.1 christos *
75 1.1 christos *
76 1.1 christos * The response policy zone canonical form of an IPv6 address is one of:
77 1.1 christos * prefix.W.W.W.W.W.W.W.W
78 1.1 christos * prefix.WORDS.zz
79 1.1 christos * prefix.WORDS.zz.WORDS
80 1.1 christos * prefix.zz.WORDS
81 1.1 christos * where
82 1.1 christos * prefix is the prefix length of the IPv6 address between 1 and 128
83 1.1 christos * W is a number between 0 and 65535
84 1.1 christos * WORDS is one or more numbers W separated with "."
85 1.1 christos * zz corresponds to :: in the standard IPv6 text representation
86 1.1 christos *
87 1.1 christos * The canonical form of IPv4 addresses is:
88 1.1 christos * prefix.B.B.B.B
89 1.1 christos * where
90 1.1 christos * prefix is the prefix length of the address between 1 and 32
91 1.1 christos * B is a number between 0 and 255
92 1.1 christos *
93 1.1 christos * Names for IPv4 addresses are distinguished from IPv6 addresses by having
94 1.1 christos * 5 labels all of which are numbers, and a prefix between 1 and 32.
95 1.1 christos */
96 1.1 christos
97 1.1 christos /*
98 1.1 christos * Nodes hashtable calculation parameters
99 1.1 christos */
100 1.7 christos #define DNS_RPZ_HTSIZE_MAX 24
101 1.7 christos #define DNS_RPZ_HTSIZE_DIV 3
102 1.1 christos
103 1.15 christos static isc_result_t
104 1.15 christos dns__rpz_shuttingdown(dns_rpz_zones_t *rpzs);
105 1.1 christos static void
106 1.16 christos dns__rpz_timer_cb(void *);
107 1.16 christos static void
108 1.16 christos dns__rpz_timer_start(dns_rpz_zone_t *rpz);
109 1.1 christos
110 1.1 christos /*
111 1.1 christos * Use a private definition of IPv6 addresses because s6_addr32 is not
112 1.1 christos * always defined and our IPv6 addresses are in non-standard byte order
113 1.1 christos */
114 1.7 christos typedef uint32_t dns_rpz_cidr_word_t;
115 1.7 christos #define DNS_RPZ_CIDR_WORD_BITS ((int)sizeof(dns_rpz_cidr_word_t) * 8)
116 1.7 christos #define DNS_RPZ_CIDR_KEY_BITS ((int)sizeof(dns_rpz_cidr_key_t) * 8)
117 1.7 christos #define DNS_RPZ_CIDR_WORDS (128 / DNS_RPZ_CIDR_WORD_BITS)
118 1.1 christos typedef struct {
119 1.7 christos dns_rpz_cidr_word_t w[DNS_RPZ_CIDR_WORDS];
120 1.1 christos } dns_rpz_cidr_key_t;
121 1.1 christos
122 1.7 christos #define ADDR_V4MAPPED 0xffff
123 1.7 christos #define KEY_IS_IPV4(prefix, ip) \
124 1.7 christos ((prefix) >= 96 && (ip)->w[0] == 0 && (ip)->w[1] == 0 && \
125 1.7 christos (ip)->w[2] == ADDR_V4MAPPED)
126 1.7 christos
127 1.7 christos #define DNS_RPZ_WORD_MASK(b) \
128 1.7 christos ((b) == 0 ? (dns_rpz_cidr_word_t)(-1) \
129 1.7 christos : ((dns_rpz_cidr_word_t)(-1) \
130 1.7 christos << (DNS_RPZ_CIDR_WORD_BITS - (b))))
131 1.1 christos
132 1.1 christos /*
133 1.1 christos * Get bit #n from the array of words of an IP address.
134 1.1 christos */
135 1.7 christos #define DNS_RPZ_IP_BIT(ip, n) \
136 1.7 christos (1 & ((ip)->w[(n) / DNS_RPZ_CIDR_WORD_BITS] >> \
137 1.7 christos (DNS_RPZ_CIDR_WORD_BITS - 1 - ((n) % DNS_RPZ_CIDR_WORD_BITS))))
138 1.1 christos
139 1.1 christos /*
140 1.1 christos * A triplet of arrays of bits flagging the existence of
141 1.1 christos * client-IP, IP, and NSIP policy triggers.
142 1.1 christos */
143 1.1 christos typedef struct dns_rpz_addr_zbits dns_rpz_addr_zbits_t;
144 1.1 christos struct dns_rpz_addr_zbits {
145 1.7 christos dns_rpz_zbits_t client_ip;
146 1.7 christos dns_rpz_zbits_t ip;
147 1.7 christos dns_rpz_zbits_t nsip;
148 1.1 christos };
149 1.1 christos
150 1.1 christos /*
151 1.1 christos * A CIDR or radix tree node.
152 1.1 christos */
153 1.1 christos struct dns_rpz_cidr_node {
154 1.7 christos dns_rpz_cidr_node_t *parent;
155 1.7 christos dns_rpz_cidr_node_t *child[2];
156 1.7 christos dns_rpz_cidr_key_t ip;
157 1.7 christos dns_rpz_prefix_t prefix;
158 1.7 christos dns_rpz_addr_zbits_t set;
159 1.7 christos dns_rpz_addr_zbits_t sum;
160 1.1 christos };
161 1.1 christos
162 1.1 christos /*
163 1.1 christos * A pair of arrays of bits flagging the existence of
164 1.1 christos * QNAME and NSDNAME policy triggers.
165 1.1 christos */
166 1.1 christos typedef struct dns_rpz_nm_zbits dns_rpz_nm_zbits_t;
167 1.1 christos struct dns_rpz_nm_zbits {
168 1.7 christos dns_rpz_zbits_t qname;
169 1.7 christos dns_rpz_zbits_t ns;
170 1.1 christos };
171 1.1 christos
172 1.1 christos /*
173 1.16 christos * The data for a name in the summary database. This has two pairs of bits
174 1.16 christos * for policy zones: one pair is for the exact name of the node, such as
175 1.16 christos * example.com, and the other pair is for a wildcard child such as
176 1.16 christos * *.example.com.
177 1.16 christos */
178 1.16 christos typedef struct nmdata nmdata_t;
179 1.16 christos struct nmdata {
180 1.16 christos dns_name_t name;
181 1.16 christos isc_mem_t *mctx;
182 1.16 christos isc_refcount_t references;
183 1.7 christos dns_rpz_nm_zbits_t set;
184 1.7 christos dns_rpz_nm_zbits_t wild;
185 1.1 christos };
186 1.1 christos
187 1.16 christos #ifdef DNS_RPZ_TRACE
188 1.16 christos #define nmdata_ref(ptr) nmdata__ref(ptr, __func__, __FILE__, __LINE__)
189 1.16 christos #define nmdata_unref(ptr) nmdata__unref(ptr, __func__, __FILE__, __LINE__)
190 1.16 christos #define nmdata_attach(ptr, ptrp) \
191 1.16 christos nmdata__attach(ptr, ptrp, __func__, __FILE__, __LINE__)
192 1.16 christos #define nmdata_detach(ptrp) nmdata__detach(ptrp, __func__, __FILE__, __LINE__)
193 1.16 christos ISC_REFCOUNT_TRACE_DECL(nmdata);
194 1.16 christos #else
195 1.16 christos ISC_REFCOUNT_DECL(nmdata);
196 1.16 christos #endif
197 1.16 christos
198 1.15 christos static isc_result_t
199 1.15 christos rpz_add(dns_rpz_zone_t *rpz, const dns_name_t *src_name);
200 1.5 christos static void
201 1.15 christos rpz_del(dns_rpz_zone_t *rpz, const dns_name_t *src_name);
202 1.1 christos
203 1.16 christos static nmdata_t *
204 1.16 christos new_nmdata(isc_mem_t *mctx, const dns_name_t *name, const nmdata_t *data);
205 1.16 christos
206 1.16 christos /* QP trie methods */
207 1.16 christos static void
208 1.16 christos qp_attach(void *uctx, void *pval, uint32_t ival);
209 1.16 christos static void
210 1.16 christos qp_detach(void *uctx, void *pval, uint32_t ival);
211 1.16 christos static size_t
212 1.16 christos qp_makekey(dns_qpkey_t key, void *uctx, void *pval, uint32_t ival);
213 1.16 christos static void
214 1.16 christos qp_triename(void *uctx, char *buf, size_t size);
215 1.16 christos
216 1.16 christos static dns_qpmethods_t qpmethods = {
217 1.16 christos qp_attach,
218 1.16 christos qp_detach,
219 1.16 christos qp_makekey,
220 1.16 christos qp_triename,
221 1.16 christos };
222 1.16 christos
223 1.1 christos const char *
224 1.1 christos dns_rpz_type2str(dns_rpz_type_t type) {
225 1.1 christos switch (type) {
226 1.1 christos case DNS_RPZ_TYPE_CLIENT_IP:
227 1.16 christos return "CLIENT-IP";
228 1.1 christos case DNS_RPZ_TYPE_QNAME:
229 1.16 christos return "QNAME";
230 1.1 christos case DNS_RPZ_TYPE_IP:
231 1.16 christos return "IP";
232 1.1 christos case DNS_RPZ_TYPE_NSIP:
233 1.16 christos return "NSIP";
234 1.1 christos case DNS_RPZ_TYPE_NSDNAME:
235 1.16 christos return "NSDNAME";
236 1.1 christos case DNS_RPZ_TYPE_BAD:
237 1.1 christos break;
238 1.1 christos }
239 1.15 christos FATAL_ERROR("impossible rpz type %d", type);
240 1.16 christos return "impossible";
241 1.1 christos }
242 1.1 christos
243 1.1 christos dns_rpz_policy_t
244 1.1 christos dns_rpz_str2policy(const char *str) {
245 1.1 christos static struct {
246 1.1 christos const char *str;
247 1.1 christos dns_rpz_policy_t policy;
248 1.1 christos } tbl[] = {
249 1.7 christos { "given", DNS_RPZ_POLICY_GIVEN },
250 1.7 christos { "disabled", DNS_RPZ_POLICY_DISABLED },
251 1.7 christos { "passthru", DNS_RPZ_POLICY_PASSTHRU },
252 1.7 christos { "drop", DNS_RPZ_POLICY_DROP },
253 1.7 christos { "tcp-only", DNS_RPZ_POLICY_TCP_ONLY },
254 1.7 christos { "nxdomain", DNS_RPZ_POLICY_NXDOMAIN },
255 1.7 christos { "nodata", DNS_RPZ_POLICY_NODATA },
256 1.7 christos { "cname", DNS_RPZ_POLICY_CNAME },
257 1.7 christos { "no-op", DNS_RPZ_POLICY_PASSTHRU }, /* old passthru */
258 1.1 christos };
259 1.1 christos unsigned int n;
260 1.1 christos
261 1.7 christos if (str == NULL) {
262 1.16 christos return DNS_RPZ_POLICY_ERROR;
263 1.7 christos }
264 1.7 christos for (n = 0; n < sizeof(tbl) / sizeof(tbl[0]); ++n) {
265 1.7 christos if (!strcasecmp(tbl[n].str, str)) {
266 1.16 christos return tbl[n].policy;
267 1.7 christos }
268 1.1 christos }
269 1.16 christos return DNS_RPZ_POLICY_ERROR;
270 1.1 christos }
271 1.1 christos
272 1.1 christos const char *
273 1.1 christos dns_rpz_policy2str(dns_rpz_policy_t policy) {
274 1.15 christos const char *str = NULL;
275 1.1 christos
276 1.1 christos switch (policy) {
277 1.1 christos case DNS_RPZ_POLICY_PASSTHRU:
278 1.1 christos str = "PASSTHRU";
279 1.1 christos break;
280 1.1 christos case DNS_RPZ_POLICY_DROP:
281 1.1 christos str = "DROP";
282 1.1 christos break;
283 1.1 christos case DNS_RPZ_POLICY_TCP_ONLY:
284 1.1 christos str = "TCP-ONLY";
285 1.1 christos break;
286 1.1 christos case DNS_RPZ_POLICY_NXDOMAIN:
287 1.1 christos str = "NXDOMAIN";
288 1.1 christos break;
289 1.1 christos case DNS_RPZ_POLICY_NODATA:
290 1.1 christos str = "NODATA";
291 1.1 christos break;
292 1.1 christos case DNS_RPZ_POLICY_RECORD:
293 1.1 christos str = "Local-Data";
294 1.1 christos break;
295 1.1 christos case DNS_RPZ_POLICY_CNAME:
296 1.1 christos case DNS_RPZ_POLICY_WILDCNAME:
297 1.1 christos str = "CNAME";
298 1.1 christos break;
299 1.1 christos case DNS_RPZ_POLICY_MISS:
300 1.1 christos str = "MISS";
301 1.1 christos break;
302 1.5 christos case DNS_RPZ_POLICY_DNS64:
303 1.5 christos str = "DNS64";
304 1.5 christos break;
305 1.11 christos case DNS_RPZ_POLICY_ERROR:
306 1.11 christos str = "ERROR";
307 1.11 christos break;
308 1.1 christos default:
309 1.11 christos UNREACHABLE();
310 1.1 christos }
311 1.16 christos return str;
312 1.16 christos }
313 1.16 christos
314 1.16 christos uint16_t
315 1.16 christos dns_rpz_str2ede(const char *str) {
316 1.16 christos static struct {
317 1.16 christos const char *str;
318 1.16 christos uint16_t ede;
319 1.16 christos } tbl[] = {
320 1.16 christos { "none", 0 },
321 1.16 christos { "forged", DNS_EDE_FORGEDANSWER },
322 1.16 christos { "blocked", DNS_EDE_BLOCKED },
323 1.16 christos { "censored", DNS_EDE_CENSORED },
324 1.16 christos { "filtered", DNS_EDE_FILTERED },
325 1.16 christos { "prohibited", DNS_EDE_PROHIBITED },
326 1.16 christos };
327 1.16 christos unsigned int n;
328 1.16 christos
329 1.16 christos if (str == NULL) {
330 1.16 christos return UINT16_MAX;
331 1.16 christos }
332 1.16 christos for (n = 0; n < sizeof(tbl) / sizeof(tbl[0]); ++n) {
333 1.16 christos if (!strcasecmp(tbl[n].str, str)) {
334 1.16 christos return tbl[n].ede;
335 1.16 christos }
336 1.16 christos }
337 1.16 christos return UINT16_MAX;
338 1.1 christos }
339 1.1 christos
340 1.1 christos /*
341 1.1 christos * Return the bit number of the highest set bit in 'zbit'.
342 1.1 christos * (for example, 0x01 returns 0, 0xFF returns 7, etc.)
343 1.1 christos */
344 1.1 christos static int
345 1.1 christos zbit_to_num(dns_rpz_zbits_t zbit) {
346 1.1 christos dns_rpz_num_t rpz_num;
347 1.1 christos
348 1.1 christos REQUIRE(zbit != 0);
349 1.1 christos rpz_num = 0;
350 1.3 christos if ((zbit & 0xffffffff00000000ULL) != 0) {
351 1.1 christos zbit >>= 32;
352 1.1 christos rpz_num += 32;
353 1.1 christos }
354 1.1 christos if ((zbit & 0xffff0000) != 0) {
355 1.1 christos zbit >>= 16;
356 1.1 christos rpz_num += 16;
357 1.1 christos }
358 1.1 christos if ((zbit & 0xff00) != 0) {
359 1.1 christos zbit >>= 8;
360 1.1 christos rpz_num += 8;
361 1.1 christos }
362 1.1 christos if ((zbit & 0xf0) != 0) {
363 1.1 christos zbit >>= 4;
364 1.1 christos rpz_num += 4;
365 1.1 christos }
366 1.1 christos if ((zbit & 0xc) != 0) {
367 1.1 christos zbit >>= 2;
368 1.1 christos rpz_num += 2;
369 1.1 christos }
370 1.7 christos if ((zbit & 2) != 0) {
371 1.1 christos ++rpz_num;
372 1.7 christos }
373 1.16 christos return rpz_num;
374 1.1 christos }
375 1.1 christos
376 1.1 christos /*
377 1.1 christos * Make a set of bit masks given one or more bits and their type.
378 1.1 christos */
379 1.1 christos static void
380 1.1 christos make_addr_set(dns_rpz_addr_zbits_t *tgt_set, dns_rpz_zbits_t zbits,
381 1.7 christos dns_rpz_type_t type) {
382 1.1 christos switch (type) {
383 1.1 christos case DNS_RPZ_TYPE_CLIENT_IP:
384 1.1 christos tgt_set->client_ip = zbits;
385 1.1 christos tgt_set->ip = 0;
386 1.1 christos tgt_set->nsip = 0;
387 1.1 christos break;
388 1.1 christos case DNS_RPZ_TYPE_IP:
389 1.1 christos tgt_set->client_ip = 0;
390 1.1 christos tgt_set->ip = zbits;
391 1.1 christos tgt_set->nsip = 0;
392 1.1 christos break;
393 1.1 christos case DNS_RPZ_TYPE_NSIP:
394 1.1 christos tgt_set->client_ip = 0;
395 1.1 christos tgt_set->ip = 0;
396 1.1 christos tgt_set->nsip = zbits;
397 1.1 christos break;
398 1.1 christos default:
399 1.11 christos UNREACHABLE();
400 1.1 christos }
401 1.1 christos }
402 1.1 christos
403 1.1 christos static void
404 1.7 christos make_nm_set(dns_rpz_nm_zbits_t *tgt_set, dns_rpz_num_t rpz_num,
405 1.7 christos dns_rpz_type_t type) {
406 1.1 christos switch (type) {
407 1.1 christos case DNS_RPZ_TYPE_QNAME:
408 1.1 christos tgt_set->qname = DNS_RPZ_ZBIT(rpz_num);
409 1.1 christos tgt_set->ns = 0;
410 1.1 christos break;
411 1.1 christos case DNS_RPZ_TYPE_NSDNAME:
412 1.1 christos tgt_set->qname = 0;
413 1.1 christos tgt_set->ns = DNS_RPZ_ZBIT(rpz_num);
414 1.1 christos break;
415 1.1 christos default:
416 1.11 christos UNREACHABLE();
417 1.1 christos }
418 1.1 christos }
419 1.1 christos
420 1.1 christos /*
421 1.1 christos * Mark a node and all of its parents as having client-IP, IP, or NSIP data
422 1.1 christos */
423 1.1 christos static void
424 1.1 christos set_sum_pair(dns_rpz_cidr_node_t *cnode) {
425 1.1 christos dns_rpz_addr_zbits_t sum;
426 1.1 christos
427 1.1 christos do {
428 1.15 christos dns_rpz_cidr_node_t *child = cnode->child[0];
429 1.1 christos sum = cnode->set;
430 1.1 christos
431 1.1 christos if (child != NULL) {
432 1.1 christos sum.client_ip |= child->sum.client_ip;
433 1.1 christos sum.ip |= child->sum.ip;
434 1.1 christos sum.nsip |= child->sum.nsip;
435 1.1 christos }
436 1.1 christos
437 1.1 christos child = cnode->child[1];
438 1.1 christos if (child != NULL) {
439 1.1 christos sum.client_ip |= child->sum.client_ip;
440 1.1 christos sum.ip |= child->sum.ip;
441 1.1 christos sum.nsip |= child->sum.nsip;
442 1.1 christos }
443 1.1 christos
444 1.1 christos if (cnode->sum.client_ip == sum.client_ip &&
445 1.7 christos cnode->sum.ip == sum.ip && cnode->sum.nsip == sum.nsip)
446 1.7 christos {
447 1.1 christos break;
448 1.7 christos }
449 1.1 christos cnode->sum = sum;
450 1.1 christos cnode = cnode->parent;
451 1.1 christos } while (cnode != NULL);
452 1.1 christos }
453 1.1 christos
454 1.1 christos /* Caller must hold rpzs->maint_lock */
455 1.1 christos static void
456 1.1 christos fix_qname_skip_recurse(dns_rpz_zones_t *rpzs) {
457 1.1 christos dns_rpz_zbits_t mask;
458 1.1 christos
459 1.1 christos /*
460 1.1 christos * qname_wait_recurse and qname_skip_recurse are used to
461 1.1 christos * implement the "qname-wait-recurse" config option.
462 1.1 christos *
463 1.1 christos * When "qname-wait-recurse" is yes, no processing happens without
464 1.1 christos * recursion. In this case, qname_wait_recurse is true, and
465 1.1 christos * qname_skip_recurse (a bit field indicating which policy zones
466 1.1 christos * can be processed without recursion) is set to all 0's by
467 1.1 christos * fix_qname_skip_recurse().
468 1.1 christos *
469 1.1 christos * When "qname-wait-recurse" is no, qname_skip_recurse may be
470 1.1 christos * set to a non-zero value by fix_qname_skip_recurse(). The mask
471 1.1 christos * has to have bits set for the policy zones for which
472 1.1 christos * processing may continue without recursion, and bits cleared
473 1.1 christos * for the rest.
474 1.1 christos *
475 1.1 christos * (1) The ARM says:
476 1.1 christos *
477 1.1 christos * The "qname-wait-recurse no" option overrides that default
478 1.1 christos * behavior when recursion cannot change a non-error
479 1.1 christos * response. The option does not affect QNAME or client-IP
480 1.1 christos * triggers in policy zones listed after other zones
481 1.1 christos * containing IP, NSIP and NSDNAME triggers, because those may
482 1.1 christos * depend on the A, AAAA, and NS records that would be found
483 1.1 christos * during recursive resolution.
484 1.1 christos *
485 1.1 christos * Let's consider the following:
486 1.1 christos *
487 1.1 christos * zbits_req = (rpzs->have.ipv4 | rpzs->have.ipv6 |
488 1.1 christos * rpzs->have.nsdname |
489 1.1 christos * rpzs->have.nsipv4 | rpzs->have.nsipv6);
490 1.1 christos *
491 1.1 christos * zbits_req now contains bits set for zones which require
492 1.1 christos * recursion.
493 1.1 christos *
494 1.1 christos * But going by the description in the ARM, if the first policy
495 1.1 christos * zone requires recursion, then all zones after that (higher
496 1.1 christos * order bits) have to wait as well. If the Nth zone requires
497 1.1 christos * recursion, then (N+1)th zone onwards all need to wait.
498 1.1 christos *
499 1.1 christos * So mapping this, examples:
500 1.1 christos *
501 1.1 christos * zbits_req = 0b000 mask = 0xffffffff (no zones have to wait for
502 1.1 christos * recursion)
503 1.1 christos * zbits_req = 0b001 mask = 0x00000000 (all zones have to wait)
504 1.1 christos * zbits_req = 0b010 mask = 0x00000001 (the first zone doesn't have to
505 1.1 christos * wait, second zone onwards need
506 1.1 christos * to wait)
507 1.1 christos * zbits_req = 0b011 mask = 0x00000000 (all zones have to wait)
508 1.1 christos * zbits_req = 0b100 mask = 0x00000011 (the 1st and 2nd zones don't
509 1.1 christos * have to wait, third zone
510 1.1 christos * onwards need to wait)
511 1.1 christos *
512 1.1 christos * More generally, we have to count the number of trailing 0
513 1.1 christos * bits in zbits_req and only these can be processed without
514 1.1 christos * recursion. All the rest need to wait.
515 1.1 christos *
516 1.1 christos * (2) The ARM says that "qname-wait-recurse no" option
517 1.1 christos * overrides the default behavior when recursion cannot change a
518 1.1 christos * non-error response. So, in the order of listing of policy
519 1.1 christos * zones, within the first policy zone where recursion may be
520 1.1 christos * required, we should first allow CLIENT-IP and QNAME policy
521 1.1 christos * records to be attempted without recursion.
522 1.1 christos */
523 1.1 christos
524 1.1 christos /*
525 1.1 christos * Get a mask covering all policy zones that are not subordinate to
526 1.1 christos * other policy zones containing triggers that require that the
527 1.1 christos * qname be resolved before they can be checked.
528 1.1 christos */
529 1.1 christos rpzs->have.client_ip = rpzs->have.client_ipv4 | rpzs->have.client_ipv6;
530 1.1 christos rpzs->have.ip = rpzs->have.ipv4 | rpzs->have.ipv6;
531 1.1 christos rpzs->have.nsip = rpzs->have.nsipv4 | rpzs->have.nsipv6;
532 1.1 christos
533 1.1 christos if (rpzs->p.qname_wait_recurse) {
534 1.1 christos mask = 0;
535 1.1 christos } else {
536 1.1 christos dns_rpz_zbits_t zbits_req;
537 1.1 christos dns_rpz_zbits_t zbits_notreq;
538 1.1 christos dns_rpz_zbits_t mask2;
539 1.1 christos dns_rpz_zbits_t req_mask;
540 1.1 christos
541 1.1 christos /*
542 1.1 christos * Get the masks of zones with policies that
543 1.1 christos * do/don't require recursion
544 1.1 christos */
545 1.1 christos
546 1.1 christos zbits_req = (rpzs->have.ipv4 | rpzs->have.ipv6 |
547 1.7 christos rpzs->have.nsdname | rpzs->have.nsipv4 |
548 1.7 christos rpzs->have.nsipv6);
549 1.1 christos zbits_notreq = (rpzs->have.client_ip | rpzs->have.qname);
550 1.1 christos
551 1.1 christos if (zbits_req == 0) {
552 1.1 christos mask = DNS_RPZ_ALL_ZBITS;
553 1.1 christos goto set;
554 1.1 christos }
555 1.1 christos
556 1.1 christos /*
557 1.1 christos * req_mask is a mask covering used bits in
558 1.1 christos * zbits_req. (For instance, 0b1 => 0b1, 0b101 => 0b111,
559 1.1 christos * 0b11010101 => 0b11111111).
560 1.1 christos */
561 1.1 christos req_mask = zbits_req;
562 1.1 christos req_mask |= req_mask >> 1;
563 1.1 christos req_mask |= req_mask >> 2;
564 1.1 christos req_mask |= req_mask >> 4;
565 1.1 christos req_mask |= req_mask >> 8;
566 1.1 christos req_mask |= req_mask >> 16;
567 1.1 christos req_mask |= req_mask >> 32;
568 1.1 christos
569 1.1 christos /*
570 1.1 christos * There's no point in skipping recursion for a later
571 1.1 christos * zone if it is required in a previous zone.
572 1.1 christos */
573 1.1 christos if ((zbits_notreq & req_mask) == 0) {
574 1.1 christos mask = 0;
575 1.1 christos goto set;
576 1.1 christos }
577 1.1 christos
578 1.1 christos /*
579 1.1 christos * This bit arithmetic creates a mask of zones in which
580 1.1 christos * it is okay to skip recursion. After the first zone
581 1.1 christos * that has to wait for recursion, all the others have
582 1.1 christos * to wait as well, so we want to create a mask in which
583 1.1 christos * all the trailing zeroes in zbits_req are are 1, and
584 1.1 christos * more significant bits are 0. (For instance,
585 1.1 christos * 0x0700 => 0x00ff, 0x0007 => 0x0000)
586 1.1 christos */
587 1.1 christos mask = ~(zbits_req | ((~zbits_req) + 1));
588 1.1 christos
589 1.1 christos /*
590 1.1 christos * As mentioned in (2) above, the zone corresponding to
591 1.1 christos * the least significant zero could have its CLIENT-IP
592 1.1 christos * and QNAME policies checked before recursion, if it
593 1.1 christos * has any of those policies. So if it does, we
594 1.1 christos * can set its 0 to 1.
595 1.1 christos *
596 1.1 christos * Locate the least significant 0 bit in the mask (for
597 1.1 christos * instance, 0xff => 0x100)...
598 1.1 christos */
599 1.1 christos mask2 = (mask << 1) & ~mask;
600 1.1 christos
601 1.1 christos /*
602 1.1 christos * Also set the bit for zone 0, because if it's in
603 1.1 christos * zbits_notreq then it's definitely okay to attempt to
604 1.1 christos * skip recursion for zone 0...
605 1.1 christos */
606 1.1 christos mask2 |= 1;
607 1.1 christos
608 1.1 christos /* Clear any bits *not* in zbits_notreq... */
609 1.1 christos mask2 &= zbits_notreq;
610 1.1 christos
611 1.1 christos /* And merge the result into the skip-recursion mask */
612 1.1 christos mask |= mask2;
613 1.1 christos }
614 1.1 christos
615 1.7 christos set:
616 1.1 christos isc_log_write(dns_lctx, DNS_LOGCATEGORY_RPZ, DNS_LOGMODULE_RBTDB,
617 1.1 christos DNS_RPZ_DEBUG_QUIET,
618 1.3 christos "computed RPZ qname_skip_recurse mask=0x%" PRIx64,
619 1.7 christos (uint64_t)mask);
620 1.1 christos rpzs->have.qname_skip_recurse = mask;
621 1.1 christos }
622 1.1 christos
623 1.1 christos static void
624 1.15 christos adj_trigger_cnt(dns_rpz_zone_t *rpz, dns_rpz_type_t rpz_type,
625 1.15 christos const dns_rpz_cidr_key_t *tgt_ip, dns_rpz_prefix_t tgt_prefix,
626 1.15 christos bool inc) {
627 1.3 christos dns_rpz_trigger_counter_t *cnt = NULL;
628 1.3 christos dns_rpz_zbits_t *have = NULL;
629 1.1 christos
630 1.1 christos switch (rpz_type) {
631 1.1 christos case DNS_RPZ_TYPE_CLIENT_IP:
632 1.1 christos REQUIRE(tgt_ip != NULL);
633 1.1 christos if (KEY_IS_IPV4(tgt_prefix, tgt_ip)) {
634 1.15 christos cnt = &rpz->rpzs->triggers[rpz->num].client_ipv4;
635 1.15 christos have = &rpz->rpzs->have.client_ipv4;
636 1.1 christos } else {
637 1.15 christos cnt = &rpz->rpzs->triggers[rpz->num].client_ipv6;
638 1.15 christos have = &rpz->rpzs->have.client_ipv6;
639 1.1 christos }
640 1.1 christos break;
641 1.1 christos case DNS_RPZ_TYPE_QNAME:
642 1.15 christos cnt = &rpz->rpzs->triggers[rpz->num].qname;
643 1.15 christos have = &rpz->rpzs->have.qname;
644 1.1 christos break;
645 1.1 christos case DNS_RPZ_TYPE_IP:
646 1.1 christos REQUIRE(tgt_ip != NULL);
647 1.1 christos if (KEY_IS_IPV4(tgt_prefix, tgt_ip)) {
648 1.15 christos cnt = &rpz->rpzs->triggers[rpz->num].ipv4;
649 1.15 christos have = &rpz->rpzs->have.ipv4;
650 1.1 christos } else {
651 1.15 christos cnt = &rpz->rpzs->triggers[rpz->num].ipv6;
652 1.15 christos have = &rpz->rpzs->have.ipv6;
653 1.1 christos }
654 1.1 christos break;
655 1.1 christos case DNS_RPZ_TYPE_NSDNAME:
656 1.15 christos cnt = &rpz->rpzs->triggers[rpz->num].nsdname;
657 1.15 christos have = &rpz->rpzs->have.nsdname;
658 1.1 christos break;
659 1.1 christos case DNS_RPZ_TYPE_NSIP:
660 1.1 christos REQUIRE(tgt_ip != NULL);
661 1.1 christos if (KEY_IS_IPV4(tgt_prefix, tgt_ip)) {
662 1.15 christos cnt = &rpz->rpzs->triggers[rpz->num].nsipv4;
663 1.15 christos have = &rpz->rpzs->have.nsipv4;
664 1.1 christos } else {
665 1.15 christos cnt = &rpz->rpzs->triggers[rpz->num].nsipv6;
666 1.15 christos have = &rpz->rpzs->have.nsipv6;
667 1.1 christos }
668 1.1 christos break;
669 1.1 christos default:
670 1.11 christos UNREACHABLE();
671 1.1 christos }
672 1.1 christos
673 1.1 christos if (inc) {
674 1.1 christos if (++*cnt == 1U) {
675 1.15 christos *have |= DNS_RPZ_ZBIT(rpz->num);
676 1.15 christos fix_qname_skip_recurse(rpz->rpzs);
677 1.1 christos }
678 1.1 christos } else {
679 1.1 christos REQUIRE(*cnt != 0U);
680 1.1 christos if (--*cnt == 0U) {
681 1.15 christos *have &= ~DNS_RPZ_ZBIT(rpz->num);
682 1.15 christos fix_qname_skip_recurse(rpz->rpzs);
683 1.1 christos }
684 1.1 christos }
685 1.1 christos }
686 1.1 christos
687 1.1 christos static dns_rpz_cidr_node_t *
688 1.7 christos new_node(dns_rpz_zones_t *rpzs, const dns_rpz_cidr_key_t *ip,
689 1.7 christos dns_rpz_prefix_t prefix, const dns_rpz_cidr_node_t *child) {
690 1.15 christos dns_rpz_cidr_node_t *node = NULL;
691 1.1 christos int i, words, wlen;
692 1.1 christos
693 1.1 christos node = isc_mem_get(rpzs->mctx, sizeof(*node));
694 1.15 christos *node = (dns_rpz_cidr_node_t){
695 1.15 christos .prefix = prefix,
696 1.15 christos };
697 1.1 christos
698 1.7 christos if (child != NULL) {
699 1.1 christos node->sum = child->sum;
700 1.7 christos }
701 1.1 christos
702 1.1 christos words = prefix / DNS_RPZ_CIDR_WORD_BITS;
703 1.1 christos wlen = prefix % DNS_RPZ_CIDR_WORD_BITS;
704 1.1 christos i = 0;
705 1.1 christos while (i < words) {
706 1.1 christos node->ip.w[i] = ip->w[i];
707 1.1 christos ++i;
708 1.1 christos }
709 1.1 christos if (wlen != 0) {
710 1.1 christos node->ip.w[i] = ip->w[i] & DNS_RPZ_WORD_MASK(wlen);
711 1.1 christos ++i;
712 1.1 christos }
713 1.7 christos while (i < DNS_RPZ_CIDR_WORDS) {
714 1.1 christos node->ip.w[i++] = 0;
715 1.7 christos }
716 1.1 christos
717 1.16 christos return node;
718 1.1 christos }
719 1.1 christos
720 1.1 christos static void
721 1.1 christos badname(int level, const dns_name_t *name, const char *str1, const char *str2) {
722 1.1 christos /*
723 1.1 christos * bin/tests/system/rpz/tests.sh looks for "invalid rpz".
724 1.1 christos */
725 1.7 christos if (level < DNS_RPZ_DEBUG_QUIET && isc_log_wouldlog(dns_lctx, level)) {
726 1.15 christos char namebuf[DNS_NAME_FORMATSIZE];
727 1.1 christos dns_name_format(name, namebuf, sizeof(namebuf));
728 1.1 christos isc_log_write(dns_lctx, DNS_LOGCATEGORY_RPZ,
729 1.1 christos DNS_LOGMODULE_RBTDB, level,
730 1.7 christos "invalid rpz IP address \"%s\"%s%s", namebuf,
731 1.7 christos str1, str2);
732 1.1 christos }
733 1.1 christos }
734 1.1 christos
735 1.1 christos /*
736 1.1 christos * Convert an IP address from radix tree binary (host byte order) to
737 1.1 christos * to its canonical response policy domain name without the origin of the
738 1.1 christos * policy zone.
739 1.1 christos *
740 1.1 christos * Generate a name for an IPv6 address that fits RFC 5952, except that our
741 1.1 christos * reversed format requires that when the length of the consecutive 16-bit
742 1.1 christos * 0 fields are equal (e.g., 1.0.0.1.0.0.db8.2001 corresponding to
743 1.1 christos * 2001:db8:0:0:1:0:0:1), we shorted the last instead of the first
744 1.1 christos * (e.g., 1.0.0.1.zz.db8.2001 corresponding to 2001:db8::1:0:0:1).
745 1.1 christos */
746 1.1 christos static isc_result_t
747 1.1 christos ip2name(const dns_rpz_cidr_key_t *tgt_ip, dns_rpz_prefix_t tgt_prefix,
748 1.7 christos const dns_name_t *base_name, dns_name_t *ip_name) {
749 1.1 christos #ifndef INET6_ADDRSTRLEN
750 1.1 christos #define INET6_ADDRSTRLEN 46
751 1.7 christos #endif /* ifndef INET6_ADDRSTRLEN */
752 1.7 christos char str[1 + 8 + 1 + INET6_ADDRSTRLEN + 1];
753 1.1 christos isc_buffer_t buffer;
754 1.1 christos isc_result_t result;
755 1.15 christos int len;
756 1.1 christos
757 1.1 christos if (KEY_IS_IPV4(tgt_prefix, tgt_ip)) {
758 1.1 christos len = snprintf(str, sizeof(str), "%u.%u.%u.%u.%u",
759 1.7 christos tgt_prefix - 96U, tgt_ip->w[3] & 0xffU,
760 1.7 christos (tgt_ip->w[3] >> 8) & 0xffU,
761 1.7 christos (tgt_ip->w[3] >> 16) & 0xffU,
762 1.7 christos (tgt_ip->w[3] >> 24) & 0xffU);
763 1.7 christos if (len < 0 || (size_t)len >= sizeof(str)) {
764 1.16 christos return ISC_R_FAILURE;
765 1.1 christos }
766 1.1 christos } else {
767 1.15 christos int w[DNS_RPZ_CIDR_WORDS * 2];
768 1.15 christos int best_first, best_len, cur_first, cur_len;
769 1.15 christos
770 1.1 christos len = snprintf(str, sizeof(str), "%d", tgt_prefix);
771 1.7 christos if (len < 0 || (size_t)len >= sizeof(str)) {
772 1.16 christos return ISC_R_FAILURE;
773 1.1 christos }
774 1.1 christos
775 1.15 christos for (int n = 0; n < DNS_RPZ_CIDR_WORDS; n++) {
776 1.15 christos w[n * 2 + 1] =
777 1.15 christos ((tgt_ip->w[DNS_RPZ_CIDR_WORDS - 1 - n] >> 16) &
778 1.7 christos 0xffff);
779 1.15 christos w[n * 2] = tgt_ip->w[DNS_RPZ_CIDR_WORDS - 1 - n] &
780 1.7 christos 0xffff;
781 1.1 christos }
782 1.1 christos /*
783 1.1 christos * Find the start and length of the first longest sequence
784 1.1 christos * of zeros in the address.
785 1.1 christos */
786 1.1 christos best_first = -1;
787 1.1 christos best_len = 0;
788 1.1 christos cur_first = -1;
789 1.1 christos cur_len = 0;
790 1.15 christos for (int n = 0; n <= 7; ++n) {
791 1.1 christos if (w[n] != 0) {
792 1.1 christos cur_len = 0;
793 1.1 christos cur_first = -1;
794 1.1 christos } else {
795 1.1 christos ++cur_len;
796 1.1 christos if (cur_first < 0) {
797 1.1 christos cur_first = n;
798 1.1 christos } else if (cur_len >= best_len) {
799 1.1 christos best_first = cur_first;
800 1.1 christos best_len = cur_len;
801 1.1 christos }
802 1.1 christos }
803 1.1 christos }
804 1.1 christos
805 1.15 christos for (int n = 0; n <= 7; ++n) {
806 1.15 christos int i;
807 1.15 christos
808 1.7 christos INSIST(len > 0 && (size_t)len < sizeof(str));
809 1.1 christos if (n == best_first) {
810 1.7 christos i = snprintf(str + len, sizeof(str) - len,
811 1.7 christos ".zz");
812 1.1 christos n += best_len - 1;
813 1.1 christos } else {
814 1.7 christos i = snprintf(str + len, sizeof(str) - len,
815 1.7 christos ".%x", w[n]);
816 1.7 christos }
817 1.7 christos if (i < 0 || (size_t)i >= (size_t)(sizeof(str) - len)) {
818 1.16 christos return ISC_R_FAILURE;
819 1.1 christos }
820 1.7 christos len += i;
821 1.1 christos }
822 1.1 christos }
823 1.1 christos
824 1.1 christos isc_buffer_init(&buffer, str, sizeof(str));
825 1.1 christos isc_buffer_add(&buffer, len);
826 1.1 christos result = dns_name_fromtext(ip_name, &buffer, base_name, 0, NULL);
827 1.16 christos return result;
828 1.1 christos }
829 1.1 christos
830 1.1 christos /*
831 1.1 christos * Determine the type of a name in a response policy zone.
832 1.1 christos */
833 1.1 christos static dns_rpz_type_t
834 1.7 christos type_from_name(const dns_rpz_zones_t *rpzs, dns_rpz_zone_t *rpz,
835 1.7 christos const dns_name_t *name) {
836 1.1 christos if (dns_name_issubdomain(name, &rpz->ip)) {
837 1.16 christos return DNS_RPZ_TYPE_IP;
838 1.1 christos }
839 1.1 christos
840 1.1 christos if (dns_name_issubdomain(name, &rpz->client_ip)) {
841 1.16 christos return DNS_RPZ_TYPE_CLIENT_IP;
842 1.1 christos }
843 1.1 christos
844 1.1 christos if ((rpzs->p.nsip_on & DNS_RPZ_ZBIT(rpz->num)) != 0 &&
845 1.1 christos dns_name_issubdomain(name, &rpz->nsip))
846 1.1 christos {
847 1.16 christos return DNS_RPZ_TYPE_NSIP;
848 1.1 christos }
849 1.1 christos
850 1.1 christos if ((rpzs->p.nsdname_on & DNS_RPZ_ZBIT(rpz->num)) != 0 &&
851 1.1 christos dns_name_issubdomain(name, &rpz->nsdname))
852 1.1 christos {
853 1.16 christos return DNS_RPZ_TYPE_NSDNAME;
854 1.1 christos }
855 1.1 christos
856 1.16 christos return DNS_RPZ_TYPE_QNAME;
857 1.1 christos }
858 1.1 christos
859 1.1 christos /*
860 1.1 christos * Convert an IP address from canonical response policy domain name form
861 1.1 christos * to radix tree binary (host byte order) for adding or deleting IP or NSIP
862 1.1 christos * data.
863 1.1 christos */
864 1.1 christos static isc_result_t
865 1.15 christos name2ipkey(int log_level, dns_rpz_zone_t *rpz, dns_rpz_type_t rpz_type,
866 1.15 christos const dns_name_t *src_name, dns_rpz_cidr_key_t *tgt_ip,
867 1.15 christos dns_rpz_prefix_t *tgt_prefix, dns_rpz_addr_zbits_t *new_set) {
868 1.15 christos char ip_str[DNS_NAME_FORMATSIZE];
869 1.1 christos dns_offsets_t ip_name_offsets;
870 1.1 christos dns_fixedname_t ip_name2f;
871 1.15 christos dns_name_t ip_name;
872 1.15 christos const char *prefix_str = NULL, *cp = NULL, *end = NULL;
873 1.17 christos char *prefix_end, *cp2;
874 1.1 christos int ip_labels;
875 1.1 christos dns_rpz_prefix_t prefix;
876 1.1 christos unsigned long prefix_num, l;
877 1.1 christos isc_result_t result;
878 1.1 christos int i;
879 1.1 christos
880 1.1 christos REQUIRE(rpz != NULL);
881 1.15 christos REQUIRE(rpz->rpzs != NULL && rpz->num < rpz->rpzs->p.num_zones);
882 1.1 christos
883 1.15 christos make_addr_set(new_set, DNS_RPZ_ZBIT(rpz->num), rpz_type);
884 1.1 christos
885 1.1 christos ip_labels = dns_name_countlabels(src_name);
886 1.7 christos if (rpz_type == DNS_RPZ_TYPE_QNAME) {
887 1.1 christos ip_labels -= dns_name_countlabels(&rpz->origin);
888 1.7 christos } else {
889 1.1 christos ip_labels -= dns_name_countlabels(&rpz->nsdname);
890 1.7 christos }
891 1.1 christos if (ip_labels < 2) {
892 1.1 christos badname(log_level, src_name, "; too short", "");
893 1.16 christos return ISC_R_FAILURE;
894 1.1 christos }
895 1.1 christos dns_name_init(&ip_name, ip_name_offsets);
896 1.1 christos dns_name_getlabelsequence(src_name, 0, ip_labels, &ip_name);
897 1.1 christos
898 1.1 christos /*
899 1.1 christos * Get text for the IP address
900 1.1 christos */
901 1.1 christos dns_name_format(&ip_name, ip_str, sizeof(ip_str));
902 1.7 christos end = &ip_str[strlen(ip_str) + 1];
903 1.1 christos prefix_str = ip_str;
904 1.1 christos
905 1.1 christos prefix_num = strtoul(prefix_str, &cp2, 10);
906 1.1 christos if (*cp2 != '.') {
907 1.7 christos badname(log_level, src_name, "; invalid leading prefix length",
908 1.7 christos "");
909 1.16 christos return ISC_R_FAILURE;
910 1.1 christos }
911 1.17 christos prefix_end = cp2;
912 1.1 christos if (prefix_num < 1U || prefix_num > 128U) {
913 1.17 christos *prefix_end = '\0';
914 1.7 christos badname(log_level, src_name, "; invalid prefix length of ",
915 1.7 christos prefix_str);
916 1.16 christos return ISC_R_FAILURE;
917 1.1 christos }
918 1.7 christos cp = cp2 + 1;
919 1.1 christos
920 1.1 christos if (--ip_labels == 4 && !strchr(cp, 'z')) {
921 1.1 christos /*
922 1.1 christos * Convert an IPv4 address
923 1.1 christos * from the form "prefix.z.y.x.w"
924 1.1 christos */
925 1.1 christos if (prefix_num > 32U) {
926 1.17 christos *prefix_end = '\0';
927 1.1 christos badname(log_level, src_name,
928 1.1 christos "; invalid IPv4 prefix length of ", prefix_str);
929 1.16 christos return ISC_R_FAILURE;
930 1.1 christos }
931 1.1 christos prefix_num += 96;
932 1.1 christos *tgt_prefix = (dns_rpz_prefix_t)prefix_num;
933 1.1 christos tgt_ip->w[0] = 0;
934 1.1 christos tgt_ip->w[1] = 0;
935 1.1 christos tgt_ip->w[2] = ADDR_V4MAPPED;
936 1.1 christos tgt_ip->w[3] = 0;
937 1.1 christos for (i = 0; i < 32; i += 8) {
938 1.1 christos l = strtoul(cp, &cp2, 10);
939 1.1 christos if (l > 255U || (*cp2 != '.' && *cp2 != '\0')) {
940 1.7 christos if (*cp2 == '.') {
941 1.1 christos *cp2 = '\0';
942 1.7 christos }
943 1.1 christos badname(log_level, src_name,
944 1.1 christos "; invalid IPv4 octet ", cp);
945 1.16 christos return ISC_R_FAILURE;
946 1.1 christos }
947 1.1 christos tgt_ip->w[3] |= l << i;
948 1.1 christos cp = cp2 + 1;
949 1.1 christos }
950 1.1 christos } else {
951 1.1 christos /*
952 1.1 christos * Convert a text IPv6 address.
953 1.1 christos */
954 1.1 christos *tgt_prefix = (dns_rpz_prefix_t)prefix_num;
955 1.7 christos for (i = 0; ip_labels > 0 && i < DNS_RPZ_CIDR_WORDS * 2;
956 1.12 christos ip_labels--)
957 1.12 christos {
958 1.1 christos if (cp[0] == 'z' && cp[1] == 'z' &&
959 1.12 christos (cp[2] == '.' || cp[2] == '\0') && i <= 6)
960 1.12 christos {
961 1.1 christos do {
962 1.7 christos if ((i & 1) == 0) {
963 1.7 christos tgt_ip->w[3 - i / 2] = 0;
964 1.7 christos }
965 1.1 christos ++i;
966 1.1 christos } while (ip_labels + i <= 8);
967 1.1 christos cp += 3;
968 1.1 christos } else {
969 1.1 christos l = strtoul(cp, &cp2, 16);
970 1.1 christos if (l > 0xffffu ||
971 1.12 christos (*cp2 != '.' && *cp2 != '\0'))
972 1.12 christos {
973 1.7 christos if (*cp2 == '.') {
974 1.7 christos *cp2 = '\0';
975 1.7 christos }
976 1.1 christos badname(log_level, src_name,
977 1.1 christos "; invalid IPv6 word ", cp);
978 1.16 christos return ISC_R_FAILURE;
979 1.1 christos }
980 1.7 christos if ((i & 1) == 0) {
981 1.7 christos tgt_ip->w[3 - i / 2] = l;
982 1.7 christos } else {
983 1.7 christos tgt_ip->w[3 - i / 2] |= l << 16;
984 1.7 christos }
985 1.1 christos i++;
986 1.1 christos cp = cp2 + 1;
987 1.1 christos }
988 1.1 christos }
989 1.1 christos }
990 1.1 christos if (cp != end) {
991 1.1 christos badname(log_level, src_name, "", "");
992 1.16 christos return ISC_R_FAILURE;
993 1.1 christos }
994 1.1 christos
995 1.1 christos /*
996 1.1 christos * Check for 1s after the prefix length.
997 1.1 christos */
998 1.1 christos prefix = (dns_rpz_prefix_t)prefix_num;
999 1.1 christos while (prefix < DNS_RPZ_CIDR_KEY_BITS) {
1000 1.1 christos dns_rpz_cidr_word_t aword;
1001 1.1 christos
1002 1.1 christos i = prefix % DNS_RPZ_CIDR_WORD_BITS;
1003 1.1 christos aword = tgt_ip->w[prefix / DNS_RPZ_CIDR_WORD_BITS];
1004 1.1 christos if ((aword & ~DNS_RPZ_WORD_MASK(i)) != 0) {
1005 1.17 christos *prefix_end = '\0';
1006 1.1 christos badname(log_level, src_name,
1007 1.1 christos "; too small prefix length of ", prefix_str);
1008 1.16 christos return ISC_R_FAILURE;
1009 1.1 christos }
1010 1.1 christos prefix -= i;
1011 1.1 christos prefix += DNS_RPZ_CIDR_WORD_BITS;
1012 1.1 christos }
1013 1.1 christos
1014 1.1 christos /*
1015 1.1 christos * Complain about bad names but be generous and accept them.
1016 1.1 christos */
1017 1.1 christos if (log_level < DNS_RPZ_DEBUG_QUIET &&
1018 1.12 christos isc_log_wouldlog(dns_lctx, log_level))
1019 1.12 christos {
1020 1.1 christos /*
1021 1.1 christos * Convert the address back to a canonical domain name
1022 1.1 christos * to ensure that the original name is in canonical form.
1023 1.1 christos */
1024 1.15 christos dns_name_t *ip_name2 = dns_fixedname_initname(&ip_name2f);
1025 1.7 christos result = ip2name(tgt_ip, (dns_rpz_prefix_t)prefix_num, NULL,
1026 1.7 christos ip_name2);
1027 1.1 christos if (result != ISC_R_SUCCESS ||
1028 1.12 christos !dns_name_equal(&ip_name, ip_name2))
1029 1.12 christos {
1030 1.15 christos char ip2_str[DNS_NAME_FORMATSIZE];
1031 1.1 christos dns_name_format(ip_name2, ip2_str, sizeof(ip2_str));
1032 1.1 christos isc_log_write(dns_lctx, DNS_LOGCATEGORY_RPZ,
1033 1.1 christos DNS_LOGMODULE_RBTDB, log_level,
1034 1.1 christos "rpz IP address \"%s\""
1035 1.1 christos " is not the canonical \"%s\"",
1036 1.1 christos ip_str, ip2_str);
1037 1.1 christos }
1038 1.1 christos }
1039 1.1 christos
1040 1.16 christos return ISC_R_SUCCESS;
1041 1.1 christos }
1042 1.1 christos
1043 1.1 christos /*
1044 1.1 christos * Get trigger name and data bits for adding or deleting summary NSDNAME
1045 1.1 christos * or QNAME data.
1046 1.1 christos */
1047 1.1 christos static void
1048 1.15 christos name2data(dns_rpz_zone_t *rpz, dns_rpz_type_t rpz_type,
1049 1.7 christos const dns_name_t *src_name, dns_name_t *trig_name,
1050 1.16 christos nmdata_t *new_data) {
1051 1.1 christos dns_offsets_t tmp_name_offsets;
1052 1.1 christos dns_name_t tmp_name;
1053 1.1 christos unsigned int prefix_len, n;
1054 1.1 christos
1055 1.1 christos REQUIRE(rpz != NULL);
1056 1.15 christos REQUIRE(rpz->rpzs != NULL && rpz->num < rpz->rpzs->p.num_zones);
1057 1.1 christos
1058 1.1 christos /*
1059 1.1 christos * Handle wildcards by putting only the parent into the
1060 1.16 christos * summary database. The database only causes a check of the
1061 1.1 christos * real policy zone where wildcards will be handled.
1062 1.1 christos */
1063 1.1 christos if (dns_name_iswildcard(src_name)) {
1064 1.1 christos prefix_len = 1;
1065 1.1 christos memset(&new_data->set, 0, sizeof(new_data->set));
1066 1.15 christos make_nm_set(&new_data->wild, rpz->num, rpz_type);
1067 1.1 christos } else {
1068 1.1 christos prefix_len = 0;
1069 1.15 christos make_nm_set(&new_data->set, rpz->num, rpz_type);
1070 1.1 christos memset(&new_data->wild, 0, sizeof(new_data->wild));
1071 1.1 christos }
1072 1.1 christos
1073 1.1 christos dns_name_init(&tmp_name, tmp_name_offsets);
1074 1.1 christos n = dns_name_countlabels(src_name);
1075 1.1 christos n -= prefix_len;
1076 1.7 christos if (rpz_type == DNS_RPZ_TYPE_QNAME) {
1077 1.1 christos n -= dns_name_countlabels(&rpz->origin);
1078 1.7 christos } else {
1079 1.1 christos n -= dns_name_countlabels(&rpz->nsdname);
1080 1.7 christos }
1081 1.1 christos dns_name_getlabelsequence(src_name, prefix_len, n, &tmp_name);
1082 1.1 christos (void)dns_name_concatenate(&tmp_name, dns_rootname, trig_name, NULL);
1083 1.1 christos }
1084 1.1 christos
1085 1.1 christos #ifndef HAVE_BUILTIN_CLZ
1086 1.1 christos /**
1087 1.1 christos * \brief Count Leading Zeros: Find the location of the left-most set
1088 1.1 christos * bit.
1089 1.1 christos */
1090 1.11 christos static unsigned int
1091 1.1 christos clz(dns_rpz_cidr_word_t w) {
1092 1.1 christos unsigned int bit;
1093 1.1 christos
1094 1.7 christos bit = DNS_RPZ_CIDR_WORD_BITS - 1;
1095 1.1 christos
1096 1.1 christos if ((w & 0xffff0000) != 0) {
1097 1.1 christos w >>= 16;
1098 1.1 christos bit -= 16;
1099 1.1 christos }
1100 1.1 christos
1101 1.1 christos if ((w & 0xff00) != 0) {
1102 1.1 christos w >>= 8;
1103 1.1 christos bit -= 8;
1104 1.1 christos }
1105 1.1 christos
1106 1.1 christos if ((w & 0xf0) != 0) {
1107 1.1 christos w >>= 4;
1108 1.1 christos bit -= 4;
1109 1.1 christos }
1110 1.1 christos
1111 1.1 christos if ((w & 0xc) != 0) {
1112 1.1 christos w >>= 2;
1113 1.1 christos bit -= 2;
1114 1.1 christos }
1115 1.1 christos
1116 1.7 christos if ((w & 2) != 0) {
1117 1.1 christos --bit;
1118 1.7 christos }
1119 1.1 christos
1120 1.16 christos return bit;
1121 1.1 christos }
1122 1.7 christos #endif /* ifndef HAVE_BUILTIN_CLZ */
1123 1.1 christos
1124 1.1 christos /*
1125 1.1 christos * Find the first differing bit in two keys (IP addresses).
1126 1.1 christos */
1127 1.1 christos static int
1128 1.1 christos diff_keys(const dns_rpz_cidr_key_t *key1, dns_rpz_prefix_t prefix1,
1129 1.7 christos const dns_rpz_cidr_key_t *key2, dns_rpz_prefix_t prefix2) {
1130 1.1 christos dns_rpz_cidr_word_t delta;
1131 1.1 christos dns_rpz_prefix_t maxbit, bit;
1132 1.1 christos int i;
1133 1.1 christos
1134 1.1 christos bit = 0;
1135 1.1 christos maxbit = ISC_MIN(prefix1, prefix2);
1136 1.1 christos
1137 1.1 christos /*
1138 1.1 christos * find the first differing words
1139 1.1 christos */
1140 1.1 christos for (i = 0; bit < maxbit; i++, bit += DNS_RPZ_CIDR_WORD_BITS) {
1141 1.1 christos delta = key1->w[i] ^ key2->w[i];
1142 1.15 christos if (delta != 0) {
1143 1.1 christos #ifdef HAVE_BUILTIN_CLZ
1144 1.1 christos bit += __builtin_clz(delta);
1145 1.7 christos #else /* ifdef HAVE_BUILTIN_CLZ */
1146 1.1 christos bit += clz(delta);
1147 1.7 christos #endif /* ifdef HAVE_BUILTIN_CLZ */
1148 1.1 christos break;
1149 1.1 christos }
1150 1.1 christos }
1151 1.16 christos return ISC_MIN(bit, maxbit);
1152 1.1 christos }
1153 1.1 christos
1154 1.1 christos /*
1155 1.1 christos * Given a hit while searching the radix trees,
1156 1.1 christos * clear all bits for higher numbered zones.
1157 1.1 christos */
1158 1.11 christos static dns_rpz_zbits_t
1159 1.1 christos trim_zbits(dns_rpz_zbits_t zbits, dns_rpz_zbits_t found) {
1160 1.1 christos dns_rpz_zbits_t x;
1161 1.1 christos
1162 1.1 christos /*
1163 1.1 christos * Isolate the first or smallest numbered hit bit.
1164 1.1 christos * Make a mask of that bit and all smaller numbered bits.
1165 1.1 christos */
1166 1.1 christos x = zbits & found;
1167 1.1 christos x &= (~x + 1);
1168 1.1 christos x = (x << 1) - 1;
1169 1.7 christos zbits &= x;
1170 1.16 christos return zbits;
1171 1.1 christos }
1172 1.1 christos
1173 1.1 christos /*
1174 1.1 christos * Search a radix tree for an IP address for ordinary lookup
1175 1.1 christos * or for a CIDR block adding or deleting an entry
1176 1.1 christos *
1177 1.1 christos * Return ISC_R_SUCCESS, DNS_R_PARTIALMATCH, ISC_R_NOTFOUND,
1178 1.1 christos * and *found=longest match node
1179 1.16 christos * or with create==true, ISC_R_EXISTS
1180 1.1 christos */
1181 1.1 christos static isc_result_t
1182 1.7 christos search(dns_rpz_zones_t *rpzs, const dns_rpz_cidr_key_t *tgt_ip,
1183 1.7 christos dns_rpz_prefix_t tgt_prefix, const dns_rpz_addr_zbits_t *tgt_set,
1184 1.7 christos bool create, dns_rpz_cidr_node_t **found) {
1185 1.16 christos dns_rpz_cidr_node_t *cur = rpzs->cidr;
1186 1.16 christos dns_rpz_cidr_node_t *parent = NULL, *child = NULL;
1187 1.15 christos dns_rpz_cidr_node_t *new_parent = NULL, *sibling = NULL;
1188 1.16 christos dns_rpz_addr_zbits_t set = *tgt_set;
1189 1.16 christos int cur_num = 0, child_num;
1190 1.16 christos isc_result_t find_result = ISC_R_NOTFOUND;
1191 1.1 christos
1192 1.1 christos *found = NULL;
1193 1.1 christos for (;;) {
1194 1.15 christos dns_rpz_prefix_t dbit;
1195 1.1 christos if (cur == NULL) {
1196 1.1 christos /*
1197 1.1 christos * No child so we cannot go down.
1198 1.1 christos * Quit with whatever we already found
1199 1.1 christos * or add the target as a child of the current parent.
1200 1.1 christos */
1201 1.7 christos if (!create) {
1202 1.16 christos return find_result;
1203 1.7 christos }
1204 1.1 christos child = new_node(rpzs, tgt_ip, tgt_prefix, NULL);
1205 1.7 christos if (parent == NULL) {
1206 1.1 christos rpzs->cidr = child;
1207 1.7 christos } else {
1208 1.1 christos parent->child[cur_num] = child;
1209 1.7 christos }
1210 1.1 christos child->parent = parent;
1211 1.1 christos child->set.client_ip |= tgt_set->client_ip;
1212 1.1 christos child->set.ip |= tgt_set->ip;
1213 1.1 christos child->set.nsip |= tgt_set->nsip;
1214 1.1 christos set_sum_pair(child);
1215 1.1 christos *found = child;
1216 1.16 christos return ISC_R_SUCCESS;
1217 1.1 christos }
1218 1.1 christos
1219 1.1 christos if ((cur->sum.client_ip & set.client_ip) == 0 &&
1220 1.1 christos (cur->sum.ip & set.ip) == 0 &&
1221 1.7 christos (cur->sum.nsip & set.nsip) == 0)
1222 1.7 christos {
1223 1.1 christos /*
1224 1.1 christos * This node has no relevant data
1225 1.1 christos * and is in none of the target trees.
1226 1.1 christos * Pretend it does not exist if we are not adding.
1227 1.1 christos *
1228 1.1 christos * If we are adding, continue down to eventually add
1229 1.1 christos * a node and mark/put this node in the correct tree.
1230 1.1 christos */
1231 1.7 christos if (!create) {
1232 1.16 christos return find_result;
1233 1.7 christos }
1234 1.1 christos }
1235 1.1 christos
1236 1.1 christos dbit = diff_keys(tgt_ip, tgt_prefix, &cur->ip, cur->prefix);
1237 1.1 christos /*
1238 1.1 christos * dbit <= tgt_prefix and dbit <= cur->prefix always.
1239 1.1 christos * We are finished searching if we matched all of the target.
1240 1.1 christos */
1241 1.1 christos if (dbit == tgt_prefix) {
1242 1.1 christos if (tgt_prefix == cur->prefix) {
1243 1.1 christos /*
1244 1.1 christos * The node's key matches the target exactly.
1245 1.1 christos */
1246 1.1 christos if ((cur->set.client_ip & set.client_ip) != 0 ||
1247 1.1 christos (cur->set.ip & set.ip) != 0 ||
1248 1.7 christos (cur->set.nsip & set.nsip) != 0)
1249 1.7 christos {
1250 1.1 christos /*
1251 1.1 christos * It is the answer if it has data.
1252 1.1 christos */
1253 1.1 christos *found = cur;
1254 1.1 christos if (create) {
1255 1.7 christos find_result = ISC_R_EXISTS;
1256 1.1 christos } else {
1257 1.7 christos find_result = ISC_R_SUCCESS;
1258 1.1 christos }
1259 1.1 christos } else if (create) {
1260 1.1 christos /*
1261 1.1 christos * The node lacked relevant data,
1262 1.1 christos * but will have it now.
1263 1.1 christos */
1264 1.1 christos cur->set.client_ip |=
1265 1.1 christos tgt_set->client_ip;
1266 1.1 christos cur->set.ip |= tgt_set->ip;
1267 1.1 christos cur->set.nsip |= tgt_set->nsip;
1268 1.1 christos set_sum_pair(cur);
1269 1.1 christos *found = cur;
1270 1.1 christos find_result = ISC_R_SUCCESS;
1271 1.1 christos }
1272 1.16 christos return find_result;
1273 1.1 christos }
1274 1.1 christos
1275 1.1 christos /*
1276 1.1 christos * We know tgt_prefix < cur->prefix which means that
1277 1.1 christos * the target is shorter than the current node.
1278 1.1 christos * Add the target as the current node's parent.
1279 1.1 christos */
1280 1.7 christos if (!create) {
1281 1.16 christos return find_result;
1282 1.7 christos }
1283 1.1 christos
1284 1.1 christos new_parent = new_node(rpzs, tgt_ip, tgt_prefix, cur);
1285 1.1 christos new_parent->parent = parent;
1286 1.7 christos if (parent == NULL) {
1287 1.1 christos rpzs->cidr = new_parent;
1288 1.7 christos } else {
1289 1.1 christos parent->child[cur_num] = new_parent;
1290 1.7 christos }
1291 1.1 christos child_num = DNS_RPZ_IP_BIT(&cur->ip, tgt_prefix);
1292 1.1 christos new_parent->child[child_num] = cur;
1293 1.1 christos cur->parent = new_parent;
1294 1.1 christos new_parent->set = *tgt_set;
1295 1.1 christos set_sum_pair(new_parent);
1296 1.1 christos *found = new_parent;
1297 1.16 christos return ISC_R_SUCCESS;
1298 1.1 christos }
1299 1.1 christos
1300 1.1 christos if (dbit == cur->prefix) {
1301 1.1 christos if ((cur->set.client_ip & set.client_ip) != 0 ||
1302 1.1 christos (cur->set.ip & set.ip) != 0 ||
1303 1.7 christos (cur->set.nsip & set.nsip) != 0)
1304 1.7 christos {
1305 1.1 christos /*
1306 1.1 christos * We have a partial match between of all of the
1307 1.1 christos * current node but only part of the target.
1308 1.1 christos * Continue searching for other hits in the
1309 1.1 christos * same or lower numbered trees.
1310 1.1 christos */
1311 1.1 christos find_result = DNS_R_PARTIALMATCH;
1312 1.1 christos *found = cur;
1313 1.1 christos set.client_ip = trim_zbits(set.client_ip,
1314 1.1 christos cur->set.client_ip);
1315 1.7 christos set.ip = trim_zbits(set.ip, cur->set.ip);
1316 1.7 christos set.nsip = trim_zbits(set.nsip, cur->set.nsip);
1317 1.1 christos }
1318 1.1 christos parent = cur;
1319 1.1 christos cur_num = DNS_RPZ_IP_BIT(tgt_ip, dbit);
1320 1.1 christos cur = cur->child[cur_num];
1321 1.1 christos continue;
1322 1.1 christos }
1323 1.1 christos
1324 1.1 christos /*
1325 1.1 christos * dbit < tgt_prefix and dbit < cur->prefix,
1326 1.1 christos * so we failed to match both the target and the current node.
1327 1.1 christos * Insert a fork of a parent above the current node and
1328 1.1 christos * add the target as a sibling of the current node
1329 1.1 christos */
1330 1.7 christos if (!create) {
1331 1.16 christos return find_result;
1332 1.7 christos }
1333 1.1 christos
1334 1.1 christos sibling = new_node(rpzs, tgt_ip, tgt_prefix, NULL);
1335 1.1 christos new_parent = new_node(rpzs, tgt_ip, dbit, cur);
1336 1.1 christos new_parent->parent = parent;
1337 1.7 christos if (parent == NULL) {
1338 1.1 christos rpzs->cidr = new_parent;
1339 1.7 christos } else {
1340 1.1 christos parent->child[cur_num] = new_parent;
1341 1.7 christos }
1342 1.1 christos child_num = DNS_RPZ_IP_BIT(tgt_ip, dbit);
1343 1.1 christos new_parent->child[child_num] = sibling;
1344 1.7 christos new_parent->child[1 - child_num] = cur;
1345 1.1 christos cur->parent = new_parent;
1346 1.1 christos sibling->parent = new_parent;
1347 1.1 christos sibling->set = *tgt_set;
1348 1.1 christos set_sum_pair(sibling);
1349 1.1 christos *found = sibling;
1350 1.16 christos return ISC_R_SUCCESS;
1351 1.1 christos }
1352 1.1 christos }
1353 1.1 christos
1354 1.1 christos /*
1355 1.1 christos * Add an IP address to the radix tree.
1356 1.1 christos */
1357 1.1 christos static isc_result_t
1358 1.15 christos add_cidr(dns_rpz_zone_t *rpz, dns_rpz_type_t rpz_type,
1359 1.7 christos const dns_name_t *src_name) {
1360 1.1 christos dns_rpz_cidr_key_t tgt_ip;
1361 1.1 christos dns_rpz_prefix_t tgt_prefix;
1362 1.1 christos dns_rpz_addr_zbits_t set;
1363 1.15 christos dns_rpz_cidr_node_t *found = NULL;
1364 1.1 christos isc_result_t result;
1365 1.1 christos
1366 1.15 christos result = name2ipkey(DNS_RPZ_ERROR_LEVEL, rpz, rpz_type, src_name,
1367 1.15 christos &tgt_ip, &tgt_prefix, &set);
1368 1.1 christos /*
1369 1.1 christos * Log complaints about bad owner names but let the zone load.
1370 1.1 christos */
1371 1.7 christos if (result != ISC_R_SUCCESS) {
1372 1.16 christos return ISC_R_SUCCESS;
1373 1.7 christos }
1374 1.1 christos
1375 1.16 christos RWLOCK(&rpz->rpzs->search_lock, isc_rwlocktype_write);
1376 1.15 christos result = search(rpz->rpzs, &tgt_ip, tgt_prefix, &set, true, &found);
1377 1.1 christos if (result != ISC_R_SUCCESS) {
1378 1.1 christos char namebuf[DNS_NAME_FORMATSIZE];
1379 1.1 christos
1380 1.1 christos /*
1381 1.1 christos * Do not worry if the radix tree already exists,
1382 1.1 christos * because diff_apply() likes to add nodes before deleting.
1383 1.1 christos */
1384 1.7 christos if (result == ISC_R_EXISTS) {
1385 1.16 christos result = ISC_R_SUCCESS;
1386 1.16 christos goto done;
1387 1.7 christos }
1388 1.1 christos
1389 1.1 christos /*
1390 1.1 christos * bin/tests/system/rpz/tests.sh looks for "rpz.*failed".
1391 1.1 christos */
1392 1.1 christos dns_name_format(src_name, namebuf, sizeof(namebuf));
1393 1.1 christos isc_log_write(dns_lctx, DNS_LOGCATEGORY_RPZ,
1394 1.1 christos DNS_LOGMODULE_RBTDB, DNS_RPZ_ERROR_LEVEL,
1395 1.7 christos "rpz add_cidr(%s) failed: %s", namebuf,
1396 1.7 christos isc_result_totext(result));
1397 1.16 christos goto done;
1398 1.1 christos }
1399 1.1 christos
1400 1.15 christos adj_trigger_cnt(rpz, rpz_type, &tgt_ip, tgt_prefix, true);
1401 1.16 christos done:
1402 1.16 christos RWUNLOCK(&rpz->rpzs->search_lock, isc_rwlocktype_write);
1403 1.16 christos return result;
1404 1.16 christos }
1405 1.16 christos
1406 1.16 christos static nmdata_t *
1407 1.16 christos new_nmdata(isc_mem_t *mctx, const dns_name_t *name, const nmdata_t *data) {
1408 1.16 christos nmdata_t *newdata = isc_mem_get(mctx, sizeof(*newdata));
1409 1.16 christos *newdata = (nmdata_t){
1410 1.16 christos .set = data->set,
1411 1.16 christos .wild = data->wild,
1412 1.16 christos .name = DNS_NAME_INITEMPTY,
1413 1.16 christos .references = ISC_REFCOUNT_INITIALIZER(1),
1414 1.16 christos };
1415 1.16 christos dns_name_dupwithoffsets(name, mctx, &newdata->name);
1416 1.16 christos isc_mem_attach(mctx, &newdata->mctx);
1417 1.16 christos
1418 1.16 christos #ifdef DNS_RPZ_TRACE
1419 1.16 christos fprintf(stderr, "new_nmdata:%s:%s:%d:%p->references = 1\n", __func__,
1420 1.16 christos __FILE__, __LINE__ + 1, name);
1421 1.16 christos #endif
1422 1.16 christos
1423 1.16 christos return newdata;
1424 1.1 christos }
1425 1.1 christos
1426 1.1 christos static isc_result_t
1427 1.16 christos add_nm(dns_rpz_zones_t *rpzs, dns_name_t *trig_name, const nmdata_t *new_data) {
1428 1.1 christos isc_result_t result;
1429 1.16 christos nmdata_t *data = NULL;
1430 1.16 christos dns_qp_t *qp = NULL;
1431 1.1 christos
1432 1.16 christos dns_qpmulti_write(rpzs->table, &qp);
1433 1.16 christos result = dns_qp_getname(qp, trig_name, (void **)&data, NULL);
1434 1.16 christos if (result != ISC_R_SUCCESS) {
1435 1.16 christos INSIST(data == NULL);
1436 1.16 christos data = new_nmdata(rpzs->mctx, trig_name, new_data);
1437 1.16 christos result = dns_qp_insert(qp, data, 0);
1438 1.16 christos nmdata_detach(&data);
1439 1.16 christos goto done;
1440 1.1 christos }
1441 1.1 christos
1442 1.1 christos /*
1443 1.1 christos * Do not count bits that are already present
1444 1.1 christos */
1445 1.16 christos if ((data->set.qname & new_data->set.qname) != 0 ||
1446 1.16 christos (data->set.ns & new_data->set.ns) != 0 ||
1447 1.16 christos (data->wild.qname & new_data->wild.qname) != 0 ||
1448 1.16 christos (data->wild.ns & new_data->wild.ns) != 0)
1449 1.7 christos {
1450 1.16 christos result = ISC_R_EXISTS;
1451 1.7 christos }
1452 1.1 christos
1453 1.16 christos /* copy in the bits from the new data */
1454 1.16 christos data->set.qname |= new_data->set.qname;
1455 1.16 christos data->set.ns |= new_data->set.ns;
1456 1.16 christos data->wild.qname |= new_data->wild.qname;
1457 1.16 christos data->wild.ns |= new_data->wild.ns;
1458 1.16 christos
1459 1.16 christos done:
1460 1.16 christos dns_qp_compact(qp, DNS_QPGC_MAYBE);
1461 1.16 christos dns_qpmulti_commit(rpzs->table, &qp);
1462 1.16 christos
1463 1.16 christos return result;
1464 1.1 christos }
1465 1.1 christos
1466 1.1 christos static isc_result_t
1467 1.15 christos add_name(dns_rpz_zone_t *rpz, dns_rpz_type_t rpz_type,
1468 1.7 christos const dns_name_t *src_name) {
1469 1.16 christos nmdata_t new_data;
1470 1.1 christos dns_fixedname_t trig_namef;
1471 1.15 christos dns_name_t *trig_name = NULL;
1472 1.1 christos isc_result_t result;
1473 1.1 christos
1474 1.1 christos /*
1475 1.1 christos * We need a summary database of names even with 1 policy zone,
1476 1.1 christos * because wildcard triggers are handled differently.
1477 1.1 christos */
1478 1.1 christos
1479 1.1 christos trig_name = dns_fixedname_initname(&trig_namef);
1480 1.15 christos name2data(rpz, rpz_type, src_name, trig_name, &new_data);
1481 1.1 christos
1482 1.15 christos result = add_nm(rpz->rpzs, trig_name, &new_data);
1483 1.1 christos
1484 1.1 christos /*
1485 1.1 christos * Do not worry if the node already exists,
1486 1.1 christos * because diff_apply() likes to add nodes before deleting.
1487 1.1 christos */
1488 1.7 christos if (result == ISC_R_EXISTS) {
1489 1.16 christos return ISC_R_SUCCESS;
1490 1.7 christos }
1491 1.7 christos if (result == ISC_R_SUCCESS) {
1492 1.16 christos RWLOCK(&rpz->rpzs->search_lock, isc_rwlocktype_write);
1493 1.15 christos adj_trigger_cnt(rpz, rpz_type, NULL, 0, true);
1494 1.16 christos RWUNLOCK(&rpz->rpzs->search_lock, isc_rwlocktype_write);
1495 1.7 christos }
1496 1.16 christos return result;
1497 1.1 christos }
1498 1.1 christos
1499 1.1 christos /*
1500 1.1 christos * Get ready for a new set of policy zones for a view.
1501 1.1 christos */
1502 1.1 christos isc_result_t
1503 1.16 christos dns_rpz_new_zones(dns_view_t *view, isc_loopmgr_t *loopmgr, char *rps_cstr,
1504 1.17 christos size_t rps_cstr_size, dns_rpz_zones_t **rpzsp,
1505 1.17 christos bool first_time) {
1506 1.15 christos dns_rpz_zones_t *rpzs = NULL;
1507 1.16 christos isc_mem_t *mctx = NULL;
1508 1.16 christos #ifdef USE_DNSRPS
1509 1.10 christos isc_result_t result = ISC_R_SUCCESS;
1510 1.16 christos #endif
1511 1.1 christos
1512 1.1 christos REQUIRE(rpzsp != NULL && *rpzsp == NULL);
1513 1.16 christos REQUIRE(view != NULL);
1514 1.16 christos
1515 1.16 christos mctx = view->mctx;
1516 1.1 christos
1517 1.15 christos rpzs = isc_mem_get(mctx, sizeof(*rpzs));
1518 1.15 christos *rpzs = (dns_rpz_zones_t){
1519 1.15 christos .rps_cstr = rps_cstr,
1520 1.15 christos .rps_cstr_size = rps_cstr_size,
1521 1.16 christos .loopmgr = loopmgr,
1522 1.15 christos .magic = DNS_RPZ_ZONES_MAGIC,
1523 1.17 christos .first_time = first_time,
1524 1.15 christos };
1525 1.1 christos
1526 1.16 christos isc_rwlock_init(&rpzs->search_lock);
1527 1.15 christos isc_mutex_init(&rpzs->maint_lock);
1528 1.15 christos isc_refcount_init(&rpzs->references, 1);
1529 1.1 christos
1530 1.1 christos #ifdef USE_DNSRPS
1531 1.1 christos if (rps_cstr != NULL) {
1532 1.15 christos result = dns_dnsrps_view_init(rpzs, rps_cstr);
1533 1.15 christos if (result != ISC_R_SUCCESS) {
1534 1.16 christos goto cleanup;
1535 1.15 christos }
1536 1.1 christos }
1537 1.7 christos #else /* ifdef USE_DNSRPS */
1538 1.15 christos INSIST(!rpzs->p.dnsrps_enabled);
1539 1.7 christos #endif /* ifdef USE_DNSRPS */
1540 1.15 christos if (!rpzs->p.dnsrps_enabled) {
1541 1.16 christos dns_qpmulti_create(mctx, &qpmethods, view, &rpzs->table);
1542 1.7 christos }
1543 1.1 christos
1544 1.15 christos isc_mem_attach(mctx, &rpzs->mctx);
1545 1.1 christos
1546 1.15 christos *rpzsp = rpzs;
1547 1.16 christos return ISC_R_SUCCESS;
1548 1.1 christos
1549 1.16 christos #ifdef USE_DNSRPS
1550 1.16 christos /* Only if DNSRPS is in use can this function fail */
1551 1.16 christos cleanup:
1552 1.15 christos isc_refcount_decrementz(&rpzs->references);
1553 1.15 christos isc_refcount_destroy(&rpzs->references);
1554 1.15 christos isc_mutex_destroy(&rpzs->maint_lock);
1555 1.15 christos isc_rwlock_destroy(&rpzs->search_lock);
1556 1.15 christos isc_mem_put(mctx, rpzs, sizeof(*rpzs));
1557 1.1 christos
1558 1.16 christos return result;
1559 1.16 christos #endif /* ifdef USE_DNSRPS */
1560 1.1 christos }
1561 1.1 christos
1562 1.1 christos isc_result_t
1563 1.1 christos dns_rpz_new_zone(dns_rpz_zones_t *rpzs, dns_rpz_zone_t **rpzp) {
1564 1.1 christos isc_result_t result;
1565 1.15 christos dns_rpz_zone_t *rpz = NULL;
1566 1.1 christos
1567 1.15 christos REQUIRE(DNS_RPZ_ZONES_VALID(rpzs));
1568 1.1 christos REQUIRE(rpzp != NULL && *rpzp == NULL);
1569 1.15 christos
1570 1.1 christos if (rpzs->p.num_zones >= DNS_RPZ_MAX_ZONES) {
1571 1.16 christos return ISC_R_NOSPACE;
1572 1.1 christos }
1573 1.1 christos
1574 1.15 christos result = dns__rpz_shuttingdown(rpzs);
1575 1.15 christos if (result != ISC_R_SUCCESS) {
1576 1.16 christos return result;
1577 1.15 christos }
1578 1.1 christos
1579 1.15 christos rpz = isc_mem_get(rpzs->mctx, sizeof(*rpz));
1580 1.15 christos *rpz = (dns_rpz_zone_t){
1581 1.15 christos .addsoa = true,
1582 1.15 christos .magic = DNS_RPZ_ZONE_MAGIC,
1583 1.15 christos .rpzs = rpzs,
1584 1.15 christos };
1585 1.1 christos
1586 1.1 christos /*
1587 1.1 christos * This will never be used, but costs us nothing and
1588 1.16 christos * simplifies update_from_db().
1589 1.1 christos */
1590 1.1 christos
1591 1.15 christos isc_ht_init(&rpz->nodes, rpzs->mctx, 1, ISC_HT_CASE_SENSITIVE);
1592 1.15 christos
1593 1.15 christos dns_name_init(&rpz->origin, NULL);
1594 1.15 christos dns_name_init(&rpz->client_ip, NULL);
1595 1.15 christos dns_name_init(&rpz->ip, NULL);
1596 1.15 christos dns_name_init(&rpz->nsdname, NULL);
1597 1.15 christos dns_name_init(&rpz->nsip, NULL);
1598 1.15 christos dns_name_init(&rpz->passthru, NULL);
1599 1.15 christos dns_name_init(&rpz->drop, NULL);
1600 1.15 christos dns_name_init(&rpz->tcp_only, NULL);
1601 1.15 christos dns_name_init(&rpz->cname, NULL);
1602 1.1 christos
1603 1.15 christos isc_time_settoepoch(&rpz->lastupdated);
1604 1.1 christos
1605 1.15 christos rpz->num = rpzs->p.num_zones++;
1606 1.15 christos rpzs->zones[rpz->num] = rpz;
1607 1.15 christos
1608 1.15 christos *rpzp = rpz;
1609 1.1 christos
1610 1.16 christos return ISC_R_SUCCESS;
1611 1.1 christos }
1612 1.1 christos
1613 1.1 christos isc_result_t
1614 1.1 christos dns_rpz_dbupdate_callback(dns_db_t *db, void *fn_arg) {
1615 1.15 christos dns_rpz_zone_t *rpz = (dns_rpz_zone_t *)fn_arg;
1616 1.1 christos isc_result_t result = ISC_R_SUCCESS;
1617 1.1 christos
1618 1.1 christos REQUIRE(DNS_DB_VALID(db));
1619 1.15 christos REQUIRE(DNS_RPZ_ZONE_VALID(rpz));
1620 1.1 christos
1621 1.15 christos LOCK(&rpz->rpzs->maint_lock);
1622 1.15 christos
1623 1.15 christos if (rpz->rpzs->shuttingdown) {
1624 1.15 christos result = ISC_R_SHUTTINGDOWN;
1625 1.15 christos goto unlock;
1626 1.15 christos }
1627 1.1 christos
1628 1.1 christos /* New zone came as AXFR */
1629 1.15 christos if (rpz->db != NULL && rpz->db != db) {
1630 1.1 christos /* We need to clean up the old DB */
1631 1.15 christos if (rpz->dbversion != NULL) {
1632 1.15 christos dns_db_closeversion(rpz->db, &rpz->dbversion, false);
1633 1.7 christos }
1634 1.15 christos dns_db_updatenotify_unregister(rpz->db,
1635 1.15 christos dns_rpz_dbupdate_callback, rpz);
1636 1.15 christos dns_db_detach(&rpz->db);
1637 1.1 christos }
1638 1.1 christos
1639 1.15 christos if (rpz->db == NULL) {
1640 1.15 christos RUNTIME_CHECK(rpz->dbversion == NULL);
1641 1.15 christos dns_db_attach(db, &rpz->db);
1642 1.1 christos }
1643 1.1 christos
1644 1.15 christos if (!rpz->updatepending && !rpz->updaterunning) {
1645 1.15 christos rpz->updatepending = true;
1646 1.15 christos
1647 1.16 christos dns_db_currentversion(rpz->db, &rpz->dbversion);
1648 1.16 christos dns__rpz_timer_start(rpz);
1649 1.1 christos } else {
1650 1.16 christos char dname[DNS_NAME_FORMATSIZE];
1651 1.15 christos rpz->updatepending = true;
1652 1.16 christos
1653 1.16 christos dns_name_format(&rpz->origin, dname, DNS_NAME_FORMATSIZE);
1654 1.1 christos isc_log_write(dns_lctx, DNS_LOGCATEGORY_GENERAL,
1655 1.1 christos DNS_LOGMODULE_MASTER, ISC_LOG_DEBUG(3),
1656 1.6 christos "rpz: %s: update already queued or running",
1657 1.6 christos dname);
1658 1.15 christos if (rpz->dbversion != NULL) {
1659 1.15 christos dns_db_closeversion(rpz->db, &rpz->dbversion, false);
1660 1.6 christos }
1661 1.15 christos dns_db_currentversion(rpz->db, &rpz->dbversion);
1662 1.1 christos }
1663 1.1 christos
1664 1.15 christos unlock:
1665 1.15 christos UNLOCK(&rpz->rpzs->maint_lock);
1666 1.1 christos
1667 1.16 christos return result;
1668 1.1 christos }
1669 1.1 christos
1670 1.16 christos void
1671 1.16 christos dns_rpz_dbupdate_unregister(dns_db_t *db, dns_rpz_zone_t *rpz) {
1672 1.16 christos REQUIRE(DNS_DB_VALID(db));
1673 1.15 christos REQUIRE(DNS_RPZ_ZONE_VALID(rpz));
1674 1.1 christos
1675 1.17 christos LOCK(&rpz->rpzs->maint_lock);
1676 1.16 christos dns_db_updatenotify_unregister(db, dns_rpz_dbupdate_callback, rpz);
1677 1.17 christos if (rpz->processed) {
1678 1.17 christos rpz->processed = false;
1679 1.17 christos INSIST(atomic_fetch_sub_acq_rel(&rpz->rpzs->zones_processed,
1680 1.17 christos 1) > 0);
1681 1.17 christos }
1682 1.17 christos if (rpz->dbregistered) {
1683 1.17 christos rpz->dbregistered = false;
1684 1.17 christos INSIST(atomic_fetch_sub_acq_rel(&rpz->rpzs->zones_registered,
1685 1.17 christos 1) > 0);
1686 1.17 christos }
1687 1.17 christos UNLOCK(&rpz->rpzs->maint_lock);
1688 1.16 christos }
1689 1.1 christos
1690 1.16 christos void
1691 1.16 christos dns_rpz_dbupdate_register(dns_db_t *db, dns_rpz_zone_t *rpz) {
1692 1.16 christos REQUIRE(DNS_DB_VALID(db));
1693 1.16 christos REQUIRE(DNS_RPZ_ZONE_VALID(rpz));
1694 1.7 christos
1695 1.17 christos LOCK(&rpz->rpzs->maint_lock);
1696 1.17 christos if (!rpz->dbregistered) {
1697 1.17 christos rpz->dbregistered = true;
1698 1.17 christos atomic_fetch_add_acq_rel(&rpz->rpzs->zones_registered, 1);
1699 1.17 christos }
1700 1.16 christos dns_db_updatenotify_register(db, dns_rpz_dbupdate_callback, rpz);
1701 1.17 christos UNLOCK(&rpz->rpzs->maint_lock);
1702 1.16 christos }
1703 1.17 christos
1704 1.16 christos static void
1705 1.16 christos dns__rpz_timer_start(dns_rpz_zone_t *rpz) {
1706 1.16 christos uint64_t tdiff;
1707 1.16 christos isc_interval_t interval;
1708 1.16 christos isc_time_t now;
1709 1.7 christos
1710 1.16 christos REQUIRE(DNS_RPZ_ZONE_VALID(rpz));
1711 1.7 christos
1712 1.16 christos now = isc_time_now();
1713 1.16 christos tdiff = isc_time_microdiff(&now, &rpz->lastupdated) / 1000000;
1714 1.16 christos if (tdiff < rpz->min_update_interval) {
1715 1.16 christos uint64_t defer = rpz->min_update_interval - tdiff;
1716 1.16 christos char dname[DNS_NAME_FORMATSIZE];
1717 1.7 christos
1718 1.16 christos dns_name_format(&rpz->origin, dname, DNS_NAME_FORMATSIZE);
1719 1.7 christos isc_log_write(dns_lctx, DNS_LOGCATEGORY_GENERAL,
1720 1.7 christos DNS_LOGMODULE_MASTER, ISC_LOG_INFO,
1721 1.15 christos "rpz: %s: new zone version came "
1722 1.15 christos "too soon, deferring update for "
1723 1.15 christos "%" PRIu64 " seconds",
1724 1.15 christos dname, defer);
1725 1.15 christos isc_interval_set(&interval, (unsigned int)defer, 0);
1726 1.7 christos } else {
1727 1.16 christos isc_interval_set(&interval, 0, 0);
1728 1.16 christos }
1729 1.16 christos
1730 1.16 christos rpz->loop = isc_loop();
1731 1.16 christos
1732 1.16 christos isc_timer_create(rpz->loop, dns__rpz_timer_cb, rpz, &rpz->updatetimer);
1733 1.16 christos isc_timer_start(rpz->updatetimer, isc_timertype_once, &interval);
1734 1.16 christos }
1735 1.16 christos
1736 1.16 christos static void
1737 1.16 christos dns__rpz_timer_stop(void *arg) {
1738 1.16 christos dns_rpz_zone_t *rpz = arg;
1739 1.16 christos REQUIRE(DNS_RPZ_ZONE_VALID(rpz));
1740 1.16 christos
1741 1.16 christos isc_timer_stop(rpz->updatetimer);
1742 1.16 christos isc_timer_destroy(&rpz->updatetimer);
1743 1.16 christos rpz->loop = NULL;
1744 1.16 christos
1745 1.16 christos dns_rpz_zones_unref(rpz->rpzs);
1746 1.16 christos }
1747 1.16 christos
1748 1.16 christos static void
1749 1.16 christos update_rpz_done_cb(void *data) {
1750 1.16 christos dns_rpz_zone_t *rpz = (dns_rpz_zone_t *)data;
1751 1.16 christos char dname[DNS_NAME_FORMATSIZE];
1752 1.16 christos
1753 1.16 christos REQUIRE(DNS_RPZ_ZONE_VALID(rpz));
1754 1.16 christos
1755 1.16 christos LOCK(&rpz->rpzs->maint_lock);
1756 1.16 christos rpz->updaterunning = false;
1757 1.16 christos
1758 1.16 christos dns_name_format(&rpz->origin, dname, DNS_NAME_FORMATSIZE);
1759 1.16 christos
1760 1.16 christos if (rpz->updatepending && !rpz->rpzs->shuttingdown) {
1761 1.16 christos /* Restart the timer */
1762 1.16 christos dns__rpz_timer_start(rpz);
1763 1.7 christos }
1764 1.7 christos
1765 1.7 christos dns_db_closeversion(rpz->updb, &rpz->updbversion, false);
1766 1.7 christos dns_db_detach(&rpz->updb);
1767 1.1 christos
1768 1.17 christos if (rpz->dbregistered && !rpz->processed) {
1769 1.17 christos rpz->processed = true;
1770 1.17 christos atomic_fetch_add_acq_rel(&rpz->rpzs->zones_processed, 1);
1771 1.17 christos }
1772 1.17 christos
1773 1.15 christos UNLOCK(&rpz->rpzs->maint_lock);
1774 1.1 christos
1775 1.15 christos isc_log_write(dns_lctx, DNS_LOGCATEGORY_GENERAL, DNS_LOGMODULE_MASTER,
1776 1.15 christos ISC_LOG_INFO, "rpz: %s: reload done: %s", dname,
1777 1.16 christos isc_result_totext(rpz->updateresult));
1778 1.1 christos
1779 1.16 christos dns_rpz_zones_unref(rpz->rpzs);
1780 1.15 christos }
1781 1.1 christos
1782 1.15 christos static isc_result_t
1783 1.15 christos update_nodes(dns_rpz_zone_t *rpz, isc_ht_t *newnodes) {
1784 1.15 christos isc_result_t result;
1785 1.15 christos dns_dbiterator_t *updbit = NULL;
1786 1.15 christos dns_name_t *name = NULL;
1787 1.15 christos dns_fixedname_t fixname;
1788 1.15 christos char domain[DNS_NAME_FORMATSIZE];
1789 1.17 christos bool slow_mode;
1790 1.1 christos
1791 1.15 christos dns_name_format(&rpz->origin, domain, DNS_NAME_FORMATSIZE);
1792 1.1 christos
1793 1.1 christos name = dns_fixedname_initname(&fixname);
1794 1.1 christos
1795 1.15 christos result = dns_db_createiterator(rpz->updb, DNS_DB_NONSEC3, &updbit);
1796 1.15 christos if (result != ISC_R_SUCCESS) {
1797 1.15 christos isc_log_write(dns_lctx, DNS_LOGCATEGORY_GENERAL,
1798 1.15 christos DNS_LOGMODULE_MASTER, ISC_LOG_ERROR,
1799 1.15 christos "rpz: %s: failed to create DB iterator - %s",
1800 1.15 christos domain, isc_result_totext(result));
1801 1.16 christos return result;
1802 1.15 christos }
1803 1.1 christos
1804 1.15 christos result = dns_dbiterator_first(updbit);
1805 1.15 christos if (result != ISC_R_SUCCESS && result != ISC_R_NOMORE) {
1806 1.15 christos isc_log_write(dns_lctx, DNS_LOGCATEGORY_GENERAL,
1807 1.15 christos DNS_LOGMODULE_MASTER, ISC_LOG_ERROR,
1808 1.15 christos "rpz: %s: failed to get db iterator - %s", domain,
1809 1.15 christos isc_result_totext(result));
1810 1.7 christos goto cleanup;
1811 1.7 christos }
1812 1.7 christos
1813 1.17 christos LOCK(&rpz->rpzs->maint_lock);
1814 1.17 christos slow_mode = rpz->rpzs->p.slow_mode;
1815 1.17 christos UNLOCK(&rpz->rpzs->maint_lock);
1816 1.17 christos
1817 1.15 christos while (result == ISC_R_SUCCESS) {
1818 1.1 christos char namebuf[DNS_NAME_FORMATSIZE];
1819 1.1 christos dns_rdatasetiter_t *rdsiter = NULL;
1820 1.15 christos dns_dbnode_t *node = NULL;
1821 1.15 christos
1822 1.15 christos result = dns__rpz_shuttingdown(rpz->rpzs);
1823 1.15 christos if (result != ISC_R_SUCCESS) {
1824 1.15 christos goto cleanup;
1825 1.15 christos }
1826 1.1 christos
1827 1.15 christos result = dns_dbiterator_current(updbit, &node, name);
1828 1.1 christos if (result != ISC_R_SUCCESS) {
1829 1.1 christos isc_log_write(dns_lctx, DNS_LOGCATEGORY_GENERAL,
1830 1.1 christos DNS_LOGMODULE_MASTER, ISC_LOG_ERROR,
1831 1.1 christos "rpz: %s: failed to get dbiterator - %s",
1832 1.1 christos domain, isc_result_totext(result));
1833 1.15 christos goto cleanup;
1834 1.1 christos }
1835 1.1 christos
1836 1.15 christos result = dns_dbiterator_pause(updbit);
1837 1.15 christos RUNTIME_CHECK(result == ISC_R_SUCCESS);
1838 1.15 christos
1839 1.1 christos result = dns_db_allrdatasets(rpz->updb, node, rpz->updbversion,
1840 1.12 christos 0, 0, &rdsiter);
1841 1.1 christos if (result != ISC_R_SUCCESS) {
1842 1.1 christos isc_log_write(dns_lctx, DNS_LOGCATEGORY_GENERAL,
1843 1.1 christos DNS_LOGMODULE_MASTER, ISC_LOG_ERROR,
1844 1.1 christos "rpz: %s: failed to fetch "
1845 1.1 christos "rrdatasets - %s",
1846 1.1 christos domain, isc_result_totext(result));
1847 1.1 christos dns_db_detachnode(rpz->updb, &node);
1848 1.15 christos goto cleanup;
1849 1.1 christos }
1850 1.1 christos
1851 1.1 christos result = dns_rdatasetiter_first(rdsiter);
1852 1.15 christos
1853 1.1 christos dns_rdatasetiter_destroy(&rdsiter);
1854 1.15 christos dns_db_detachnode(rpz->updb, &node);
1855 1.15 christos
1856 1.15 christos if (result != ISC_R_SUCCESS) { /* skip empty non-terminal */
1857 1.7 christos if (result != ISC_R_NOMORE) {
1858 1.7 christos isc_log_write(
1859 1.7 christos dns_lctx, DNS_LOGCATEGORY_GENERAL,
1860 1.7 christos DNS_LOGMODULE_MASTER, ISC_LOG_ERROR,
1861 1.7 christos "rpz: %s: error %s while creating "
1862 1.7 christos "rdatasetiter",
1863 1.7 christos domain, isc_result_totext(result));
1864 1.7 christos }
1865 1.15 christos goto next;
1866 1.1 christos }
1867 1.1 christos
1868 1.9 christos dns_name_downcase(name, name, NULL);
1869 1.15 christos
1870 1.15 christos /* Add entry to the new nodes table */
1871 1.15 christos result = isc_ht_add(newnodes, name->ndata, name->length, rpz);
1872 1.1 christos if (result != ISC_R_SUCCESS) {
1873 1.1 christos dns_name_format(name, namebuf, sizeof(namebuf));
1874 1.1 christos isc_log_write(dns_lctx, DNS_LOGCATEGORY_GENERAL,
1875 1.1 christos DNS_LOGMODULE_MASTER, ISC_LOG_ERROR,
1876 1.1 christos "rpz: %s, adding node %s to HT error %s",
1877 1.1 christos domain, namebuf,
1878 1.1 christos isc_result_totext(result));
1879 1.15 christos goto next;
1880 1.1 christos }
1881 1.1 christos
1882 1.15 christos /* Does the entry exist in the old nodes table? */
1883 1.7 christos result = isc_ht_find(rpz->nodes, name->ndata, name->length,
1884 1.7 christos NULL);
1885 1.15 christos if (result == ISC_R_SUCCESS) { /* found */
1886 1.1 christos isc_ht_delete(rpz->nodes, name->ndata, name->length);
1887 1.15 christos goto next;
1888 1.15 christos }
1889 1.15 christos
1890 1.15 christos /*
1891 1.15 christos * Only the single rpz updates are serialized, so we need to
1892 1.15 christos * lock here because we can be processing more updates to
1893 1.15 christos * different rpz zones at the same time
1894 1.15 christos */
1895 1.15 christos LOCK(&rpz->rpzs->maint_lock);
1896 1.15 christos result = rpz_add(rpz, name);
1897 1.15 christos UNLOCK(&rpz->rpzs->maint_lock);
1898 1.15 christos
1899 1.15 christos if (result != ISC_R_SUCCESS) {
1900 1.15 christos dns_name_format(name, namebuf, sizeof(namebuf));
1901 1.15 christos isc_log_write(dns_lctx, DNS_LOGCATEGORY_GENERAL,
1902 1.15 christos DNS_LOGMODULE_MASTER, ISC_LOG_ERROR,
1903 1.15 christos "rpz: %s: adding node %s "
1904 1.15 christos "to RPZ error %s",
1905 1.15 christos domain, namebuf,
1906 1.15 christos isc_result_totext(result));
1907 1.15 christos } else if (isc_log_wouldlog(dns_lctx, ISC_LOG_DEBUG(3))) {
1908 1.15 christos dns_name_format(name, namebuf, sizeof(namebuf));
1909 1.15 christos isc_log_write(dns_lctx, DNS_LOGCATEGORY_GENERAL,
1910 1.15 christos DNS_LOGMODULE_MASTER, ISC_LOG_DEBUG(3),
1911 1.15 christos "rpz: %s: adding node %s", domain,
1912 1.15 christos namebuf);
1913 1.1 christos }
1914 1.1 christos
1915 1.15 christos next:
1916 1.15 christos result = dns_dbiterator_next(updbit);
1917 1.17 christos
1918 1.17 christos if (slow_mode) {
1919 1.17 christos uv_sleep(100);
1920 1.17 christos }
1921 1.15 christos }
1922 1.15 christos INSIST(result != ISC_R_SUCCESS);
1923 1.15 christos if (result == ISC_R_NOMORE) {
1924 1.15 christos result = ISC_R_SUCCESS;
1925 1.1 christos }
1926 1.1 christos
1927 1.15 christos cleanup:
1928 1.15 christos dns_dbiterator_destroy(&updbit);
1929 1.15 christos
1930 1.16 christos return result;
1931 1.15 christos }
1932 1.15 christos
1933 1.15 christos static isc_result_t
1934 1.15 christos cleanup_nodes(dns_rpz_zone_t *rpz) {
1935 1.15 christos isc_result_t result;
1936 1.15 christos isc_ht_iter_t *iter = NULL;
1937 1.15 christos dns_name_t *name = NULL;
1938 1.15 christos dns_fixedname_t fixname;
1939 1.15 christos
1940 1.15 christos name = dns_fixedname_initname(&fixname);
1941 1.15 christos
1942 1.15 christos isc_ht_iter_create(rpz->nodes, &iter);
1943 1.15 christos
1944 1.15 christos for (result = isc_ht_iter_first(iter); result == ISC_R_SUCCESS;
1945 1.15 christos result = isc_ht_iter_delcurrent_next(iter))
1946 1.15 christos {
1947 1.15 christos isc_region_t region;
1948 1.15 christos unsigned char *key = NULL;
1949 1.15 christos size_t keysize;
1950 1.15 christos
1951 1.15 christos result = dns__rpz_shuttingdown(rpz->rpzs);
1952 1.15 christos if (result != ISC_R_SUCCESS) {
1953 1.15 christos break;
1954 1.15 christos }
1955 1.15 christos
1956 1.15 christos isc_ht_iter_currentkey(iter, &key, &keysize);
1957 1.15 christos region.base = key;
1958 1.15 christos region.length = (unsigned int)keysize;
1959 1.15 christos dns_name_fromregion(name, ®ion);
1960 1.7 christos
1961 1.15 christos LOCK(&rpz->rpzs->maint_lock);
1962 1.15 christos rpz_del(rpz, name);
1963 1.7 christos UNLOCK(&rpz->rpzs->maint_lock);
1964 1.15 christos }
1965 1.15 christos INSIST(result != ISC_R_SUCCESS);
1966 1.15 christos if (result == ISC_R_NOMORE) {
1967 1.15 christos result = ISC_R_SUCCESS;
1968 1.15 christos }
1969 1.15 christos
1970 1.15 christos isc_ht_iter_destroy(&iter);
1971 1.15 christos
1972 1.16 christos return result;
1973 1.15 christos }
1974 1.15 christos
1975 1.15 christos static isc_result_t
1976 1.15 christos dns__rpz_shuttingdown(dns_rpz_zones_t *rpzs) {
1977 1.15 christos bool shuttingdown = false;
1978 1.15 christos
1979 1.15 christos LOCK(&rpzs->maint_lock);
1980 1.15 christos shuttingdown = rpzs->shuttingdown;
1981 1.15 christos UNLOCK(&rpzs->maint_lock);
1982 1.15 christos
1983 1.15 christos if (shuttingdown) {
1984 1.16 christos return ISC_R_SHUTTINGDOWN;
1985 1.15 christos }
1986 1.15 christos
1987 1.16 christos return ISC_R_SUCCESS;
1988 1.15 christos }
1989 1.15 christos
1990 1.15 christos static void
1991 1.15 christos update_rpz_cb(void *data) {
1992 1.15 christos dns_rpz_zone_t *rpz = (dns_rpz_zone_t *)data;
1993 1.15 christos isc_result_t result = ISC_R_SUCCESS;
1994 1.15 christos isc_ht_t *newnodes = NULL;
1995 1.15 christos
1996 1.15 christos REQUIRE(rpz->nodes != NULL);
1997 1.7 christos
1998 1.15 christos result = dns__rpz_shuttingdown(rpz->rpzs);
1999 1.15 christos if (result != ISC_R_SUCCESS) {
2000 1.15 christos goto shuttingdown;
2001 1.1 christos }
2002 1.1 christos
2003 1.15 christos isc_ht_init(&newnodes, rpz->rpzs->mctx, 1, ISC_HT_CASE_SENSITIVE);
2004 1.7 christos
2005 1.15 christos result = update_nodes(rpz, newnodes);
2006 1.15 christos if (result != ISC_R_SUCCESS) {
2007 1.15 christos goto cleanup;
2008 1.7 christos }
2009 1.15 christos
2010 1.15 christos result = cleanup_nodes(rpz);
2011 1.15 christos if (result != ISC_R_SUCCESS) {
2012 1.15 christos goto cleanup;
2013 1.7 christos }
2014 1.15 christos
2015 1.15 christos /* Finalize the update */
2016 1.15 christos ISC_SWAP(rpz->nodes, newnodes);
2017 1.15 christos
2018 1.15 christos cleanup:
2019 1.15 christos isc_ht_destroy(&newnodes);
2020 1.15 christos
2021 1.15 christos shuttingdown:
2022 1.15 christos rpz->updateresult = result;
2023 1.1 christos }
2024 1.1 christos
2025 1.1 christos static void
2026 1.16 christos dns__rpz_timer_cb(void *arg) {
2027 1.15 christos char domain[DNS_NAME_FORMATSIZE];
2028 1.16 christos dns_rpz_zone_t *rpz = (dns_rpz_zone_t *)arg;
2029 1.15 christos
2030 1.15 christos REQUIRE(DNS_RPZ_ZONE_VALID(rpz));
2031 1.16 christos REQUIRE(DNS_DB_VALID(rpz->db));
2032 1.16 christos REQUIRE(rpz->updb == NULL);
2033 1.16 christos REQUIRE(rpz->updbversion == NULL);
2034 1.15 christos
2035 1.15 christos LOCK(&rpz->rpzs->maint_lock);
2036 1.1 christos
2037 1.15 christos if (rpz->rpzs->shuttingdown) {
2038 1.15 christos goto unlock;
2039 1.15 christos }
2040 1.1 christos
2041 1.15 christos rpz->updatepending = false;
2042 1.15 christos rpz->updaterunning = true;
2043 1.15 christos rpz->updateresult = ISC_R_UNSET;
2044 1.15 christos
2045 1.16 christos dns_db_attach(rpz->db, &rpz->updb);
2046 1.15 christos INSIST(rpz->dbversion != NULL);
2047 1.1 christos rpz->updbversion = rpz->dbversion;
2048 1.1 christos rpz->dbversion = NULL;
2049 1.1 christos
2050 1.15 christos dns_name_format(&rpz->origin, domain, DNS_NAME_FORMATSIZE);
2051 1.15 christos isc_log_write(dns_lctx, DNS_LOGCATEGORY_GENERAL, DNS_LOGMODULE_MASTER,
2052 1.15 christos ISC_LOG_INFO, "rpz: %s: reload start", domain);
2053 1.15 christos
2054 1.16 christos dns_rpz_zones_ref(rpz->rpzs);
2055 1.16 christos isc_work_enqueue(rpz->loop, update_rpz_cb, update_rpz_done_cb, rpz);
2056 1.1 christos
2057 1.16 christos isc_timer_destroy(&rpz->updatetimer);
2058 1.16 christos rpz->loop = NULL;
2059 1.1 christos
2060 1.16 christos rpz->lastupdated = isc_time_now();
2061 1.15 christos unlock:
2062 1.15 christos UNLOCK(&rpz->rpzs->maint_lock);
2063 1.1 christos }
2064 1.1 christos
2065 1.1 christos /*
2066 1.1 christos * Free the radix tree of a response policy database.
2067 1.1 christos */
2068 1.1 christos static void
2069 1.1 christos cidr_free(dns_rpz_zones_t *rpzs) {
2070 1.15 christos dns_rpz_cidr_node_t *cur = NULL, *child = NULL, *parent = NULL;
2071 1.1 christos
2072 1.1 christos cur = rpzs->cidr;
2073 1.1 christos while (cur != NULL) {
2074 1.1 christos /* Depth first. */
2075 1.1 christos child = cur->child[0];
2076 1.1 christos if (child != NULL) {
2077 1.1 christos cur = child;
2078 1.1 christos continue;
2079 1.1 christos }
2080 1.1 christos child = cur->child[1];
2081 1.1 christos if (child != NULL) {
2082 1.1 christos cur = child;
2083 1.1 christos continue;
2084 1.1 christos }
2085 1.1 christos
2086 1.1 christos /* Delete this leaf and go up. */
2087 1.1 christos parent = cur->parent;
2088 1.7 christos if (parent == NULL) {
2089 1.1 christos rpzs->cidr = NULL;
2090 1.7 christos } else {
2091 1.1 christos parent->child[parent->child[1] == cur] = NULL;
2092 1.7 christos }
2093 1.1 christos isc_mem_put(rpzs->mctx, cur, sizeof(*cur));
2094 1.1 christos cur = parent;
2095 1.1 christos }
2096 1.1 christos }
2097 1.1 christos
2098 1.1 christos static void
2099 1.15 christos dns__rpz_shutdown(dns_rpz_zone_t *rpz) {
2100 1.15 christos /* maint_lock must be locked */
2101 1.15 christos if (rpz->updatetimer != NULL) {
2102 1.16 christos /* Don't wait for timer to trigger for shutdown */
2103 1.16 christos INSIST(rpz->loop != NULL);
2104 1.15 christos
2105 1.16 christos dns_rpz_zones_ref(rpz->rpzs);
2106 1.16 christos isc_async_run(rpz->loop, dns__rpz_timer_stop, rpz);
2107 1.15 christos }
2108 1.15 christos }
2109 1.15 christos
2110 1.15 christos static void
2111 1.15 christos dns_rpz_zone_destroy(dns_rpz_zone_t **rpzp) {
2112 1.15 christos dns_rpz_zone_t *rpz = NULL;
2113 1.5 christos dns_rpz_zones_t *rpzs;
2114 1.3 christos
2115 1.1 christos rpz = *rpzp;
2116 1.1 christos *rpzp = NULL;
2117 1.3 christos
2118 1.15 christos rpzs = rpz->rpzs;
2119 1.15 christos rpz->rpzs = NULL;
2120 1.3 christos
2121 1.15 christos if (dns_name_dynamic(&rpz->origin)) {
2122 1.15 christos dns_name_free(&rpz->origin, rpzs->mctx);
2123 1.15 christos }
2124 1.15 christos if (dns_name_dynamic(&rpz->client_ip)) {
2125 1.15 christos dns_name_free(&rpz->client_ip, rpzs->mctx);
2126 1.15 christos }
2127 1.15 christos if (dns_name_dynamic(&rpz->ip)) {
2128 1.15 christos dns_name_free(&rpz->ip, rpzs->mctx);
2129 1.15 christos }
2130 1.15 christos if (dns_name_dynamic(&rpz->nsdname)) {
2131 1.15 christos dns_name_free(&rpz->nsdname, rpzs->mctx);
2132 1.15 christos }
2133 1.15 christos if (dns_name_dynamic(&rpz->nsip)) {
2134 1.15 christos dns_name_free(&rpz->nsip, rpzs->mctx);
2135 1.15 christos }
2136 1.15 christos if (dns_name_dynamic(&rpz->passthru)) {
2137 1.15 christos dns_name_free(&rpz->passthru, rpzs->mctx);
2138 1.15 christos }
2139 1.15 christos if (dns_name_dynamic(&rpz->drop)) {
2140 1.15 christos dns_name_free(&rpz->drop, rpzs->mctx);
2141 1.15 christos }
2142 1.15 christos if (dns_name_dynamic(&rpz->tcp_only)) {
2143 1.15 christos dns_name_free(&rpz->tcp_only, rpzs->mctx);
2144 1.15 christos }
2145 1.15 christos if (dns_name_dynamic(&rpz->cname)) {
2146 1.15 christos dns_name_free(&rpz->cname, rpzs->mctx);
2147 1.15 christos }
2148 1.15 christos if (rpz->db != NULL) {
2149 1.15 christos if (rpz->dbversion != NULL) {
2150 1.15 christos dns_db_closeversion(rpz->db, &rpz->dbversion, false);
2151 1.3 christos }
2152 1.15 christos dns_db_updatenotify_unregister(rpz->db,
2153 1.15 christos dns_rpz_dbupdate_callback, rpz);
2154 1.15 christos dns_db_detach(&rpz->db);
2155 1.15 christos }
2156 1.15 christos INSIST(!rpz->updaterunning);
2157 1.3 christos
2158 1.15 christos isc_ht_destroy(&rpz->nodes);
2159 1.5 christos
2160 1.15 christos isc_mem_put(rpzs->mctx, rpz, sizeof(*rpz));
2161 1.1 christos }
2162 1.1 christos
2163 1.15 christos static void
2164 1.15 christos dns__rpz_zones_destroy(dns_rpz_zones_t *rpzs) {
2165 1.15 christos REQUIRE(rpzs->shuttingdown);
2166 1.1 christos
2167 1.15 christos for (dns_rpz_num_t rpz_num = 0; rpz_num < DNS_RPZ_MAX_ZONES; ++rpz_num)
2168 1.15 christos {
2169 1.15 christos if (rpzs->zones[rpz_num] == NULL) {
2170 1.15 christos continue;
2171 1.1 christos }
2172 1.15 christos
2173 1.15 christos dns_rpz_zone_destroy(&rpzs->zones[rpz_num]);
2174 1.5 christos }
2175 1.5 christos
2176 1.15 christos if (rpzs->rps_cstr_size != 0) {
2177 1.1 christos #ifdef USE_DNSRPS
2178 1.15 christos librpz->client_detach(&rpzs->rps_client);
2179 1.7 christos #endif /* ifdef USE_DNSRPS */
2180 1.15 christos isc_mem_put(rpzs->mctx, rpzs->rps_cstr, rpzs->rps_cstr_size);
2181 1.15 christos }
2182 1.3 christos
2183 1.15 christos cidr_free(rpzs);
2184 1.16 christos if (rpzs->table != NULL) {
2185 1.16 christos dns_qpmulti_destroy(&rpzs->table);
2186 1.1 christos }
2187 1.16 christos
2188 1.15 christos isc_mutex_destroy(&rpzs->maint_lock);
2189 1.15 christos isc_rwlock_destroy(&rpzs->search_lock);
2190 1.15 christos isc_mem_putanddetach(&rpzs->mctx, rpzs, sizeof(*rpzs));
2191 1.1 christos }
2192 1.1 christos
2193 1.15 christos void
2194 1.15 christos dns_rpz_zones_shutdown(dns_rpz_zones_t *rpzs) {
2195 1.15 christos REQUIRE(DNS_RPZ_ZONES_VALID(rpzs));
2196 1.15 christos /*
2197 1.15 christos * Forget the last of the view's rpz machinery when shutting down.
2198 1.15 christos */
2199 1.15 christos
2200 1.15 christos LOCK(&rpzs->maint_lock);
2201 1.15 christos if (rpzs->shuttingdown) {
2202 1.15 christos UNLOCK(&rpzs->maint_lock);
2203 1.15 christos return;
2204 1.15 christos }
2205 1.1 christos
2206 1.15 christos rpzs->shuttingdown = true;
2207 1.1 christos
2208 1.15 christos for (dns_rpz_num_t rpz_num = 0; rpz_num < DNS_RPZ_MAX_ZONES; ++rpz_num)
2209 1.15 christos {
2210 1.15 christos if (rpzs->zones[rpz_num] == NULL) {
2211 1.15 christos continue;
2212 1.15 christos }
2213 1.1 christos
2214 1.15 christos dns__rpz_shutdown(rpzs->zones[rpz_num]);
2215 1.15 christos }
2216 1.15 christos UNLOCK(&rpzs->maint_lock);
2217 1.1 christos }
2218 1.1 christos
2219 1.15 christos #ifdef DNS_RPZ_TRACE
2220 1.15 christos ISC_REFCOUNT_TRACE_IMPL(dns_rpz_zones, dns__rpz_zones_destroy);
2221 1.15 christos #else
2222 1.15 christos ISC_REFCOUNT_IMPL(dns_rpz_zones, dns__rpz_zones_destroy);
2223 1.15 christos #endif
2224 1.15 christos
2225 1.1 christos /*
2226 1.1 christos * Add an IP address to the radix tree or a name to the summary database.
2227 1.1 christos */
2228 1.15 christos static isc_result_t
2229 1.15 christos rpz_add(dns_rpz_zone_t *rpz, const dns_name_t *src_name) {
2230 1.1 christos dns_rpz_type_t rpz_type;
2231 1.1 christos isc_result_t result = ISC_R_FAILURE;
2232 1.15 christos dns_rpz_zones_t *rpzs = NULL;
2233 1.15 christos dns_rpz_num_t rpz_num;
2234 1.15 christos
2235 1.15 christos REQUIRE(rpz != NULL);
2236 1.15 christos
2237 1.15 christos rpzs = rpz->rpzs;
2238 1.15 christos rpz_num = rpz->num;
2239 1.1 christos
2240 1.1 christos REQUIRE(rpzs != NULL && rpz_num < rpzs->p.num_zones);
2241 1.15 christos
2242 1.1 christos rpz_type = type_from_name(rpzs, rpz, src_name);
2243 1.1 christos switch (rpz_type) {
2244 1.1 christos case DNS_RPZ_TYPE_QNAME:
2245 1.1 christos case DNS_RPZ_TYPE_NSDNAME:
2246 1.15 christos result = add_name(rpz, rpz_type, src_name);
2247 1.1 christos break;
2248 1.1 christos case DNS_RPZ_TYPE_CLIENT_IP:
2249 1.1 christos case DNS_RPZ_TYPE_IP:
2250 1.1 christos case DNS_RPZ_TYPE_NSIP:
2251 1.15 christos result = add_cidr(rpz, rpz_type, src_name);
2252 1.1 christos break;
2253 1.1 christos case DNS_RPZ_TYPE_BAD:
2254 1.1 christos break;
2255 1.1 christos }
2256 1.1 christos
2257 1.16 christos return result;
2258 1.1 christos }
2259 1.1 christos
2260 1.1 christos /*
2261 1.1 christos * Remove an IP address from the radix tree.
2262 1.1 christos */
2263 1.1 christos static void
2264 1.15 christos del_cidr(dns_rpz_zone_t *rpz, dns_rpz_type_t rpz_type,
2265 1.7 christos const dns_name_t *src_name) {
2266 1.1 christos isc_result_t result;
2267 1.1 christos dns_rpz_cidr_key_t tgt_ip;
2268 1.1 christos dns_rpz_prefix_t tgt_prefix;
2269 1.1 christos dns_rpz_addr_zbits_t tgt_set;
2270 1.15 christos dns_rpz_cidr_node_t *tgt = NULL, *parent = NULL, *child = NULL;
2271 1.1 christos
2272 1.1 christos /*
2273 1.1 christos * Do not worry about invalid rpz IP address names. If we
2274 1.1 christos * are here, then something relevant was added and so was
2275 1.16 christos * valid.
2276 1.1 christos */
2277 1.15 christos result = name2ipkey(DNS_RPZ_DEBUG_QUIET, rpz, rpz_type, src_name,
2278 1.15 christos &tgt_ip, &tgt_prefix, &tgt_set);
2279 1.7 christos if (result != ISC_R_SUCCESS) {
2280 1.1 christos return;
2281 1.7 christos }
2282 1.1 christos
2283 1.16 christos RWLOCK(&rpz->rpzs->search_lock, isc_rwlocktype_write);
2284 1.15 christos result = search(rpz->rpzs, &tgt_ip, tgt_prefix, &tgt_set, false, &tgt);
2285 1.1 christos if (result != ISC_R_SUCCESS) {
2286 1.16 christos goto done;
2287 1.1 christos }
2288 1.1 christos
2289 1.1 christos /*
2290 1.1 christos * Mark the node and its parents to reflect the deleted IP address.
2291 1.1 christos */
2292 1.1 christos tgt_set.client_ip &= tgt->set.client_ip;
2293 1.1 christos tgt_set.ip &= tgt->set.ip;
2294 1.1 christos tgt_set.nsip &= tgt->set.nsip;
2295 1.1 christos tgt->set.client_ip &= ~tgt_set.client_ip;
2296 1.1 christos tgt->set.ip &= ~tgt_set.ip;
2297 1.1 christos tgt->set.nsip &= ~tgt_set.nsip;
2298 1.1 christos set_sum_pair(tgt);
2299 1.1 christos
2300 1.15 christos adj_trigger_cnt(rpz, rpz_type, &tgt_ip, tgt_prefix, false);
2301 1.1 christos
2302 1.1 christos /*
2303 1.1 christos * We might need to delete 2 nodes.
2304 1.1 christos */
2305 1.1 christos do {
2306 1.1 christos /*
2307 1.1 christos * The node is now useless if it has no data of its own
2308 1.15 christos * and 0 or 1 children. We are finished if it is not
2309 1.15 christos * useless.
2310 1.1 christos */
2311 1.1 christos if ((child = tgt->child[0]) != NULL) {
2312 1.7 christos if (tgt->child[1] != NULL) {
2313 1.1 christos break;
2314 1.7 christos }
2315 1.1 christos } else {
2316 1.1 christos child = tgt->child[1];
2317 1.1 christos }
2318 1.7 christos if (tgt->set.client_ip != 0 || tgt->set.ip != 0 ||
2319 1.12 christos tgt->set.nsip != 0)
2320 1.12 christos {
2321 1.1 christos break;
2322 1.7 christos }
2323 1.1 christos
2324 1.1 christos /*
2325 1.1 christos * Replace the pointer to this node in the parent with
2326 1.1 christos * the remaining child or NULL.
2327 1.1 christos */
2328 1.1 christos parent = tgt->parent;
2329 1.1 christos if (parent == NULL) {
2330 1.15 christos rpz->rpzs->cidr = child;
2331 1.1 christos } else {
2332 1.1 christos parent->child[parent->child[1] == tgt] = child;
2333 1.1 christos }
2334 1.15 christos
2335 1.1 christos /*
2336 1.1 christos * If the child exists fix up its parent pointer.
2337 1.1 christos */
2338 1.7 christos if (child != NULL) {
2339 1.1 christos child->parent = parent;
2340 1.7 christos }
2341 1.15 christos isc_mem_put(rpz->rpzs->mctx, tgt, sizeof(*tgt));
2342 1.1 christos
2343 1.1 christos tgt = parent;
2344 1.1 christos } while (tgt != NULL);
2345 1.16 christos
2346 1.16 christos done:
2347 1.16 christos RWUNLOCK(&rpz->rpzs->search_lock, isc_rwlocktype_write);
2348 1.1 christos }
2349 1.1 christos
2350 1.1 christos static void
2351 1.15 christos del_name(dns_rpz_zone_t *rpz, dns_rpz_type_t rpz_type,
2352 1.7 christos const dns_name_t *src_name) {
2353 1.16 christos isc_result_t result;
2354 1.1 christos char namebuf[DNS_NAME_FORMATSIZE];
2355 1.1 christos dns_fixedname_t trig_namef;
2356 1.15 christos dns_name_t *trig_name = NULL;
2357 1.16 christos dns_rpz_zones_t *rpzs = rpz->rpzs;
2358 1.16 christos nmdata_t *data = NULL;
2359 1.16 christos nmdata_t del_data;
2360 1.16 christos dns_qp_t *qp = NULL;
2361 1.3 christos bool exists;
2362 1.1 christos
2363 1.16 christos dns_qpmulti_write(rpzs->table, &qp);
2364 1.16 christos
2365 1.1 christos /*
2366 1.1 christos * We need a summary database of names even with 1 policy zone,
2367 1.1 christos * because wildcard triggers are handled differently.
2368 1.1 christos */
2369 1.1 christos
2370 1.1 christos trig_name = dns_fixedname_initname(&trig_namef);
2371 1.15 christos name2data(rpz, rpz_type, src_name, trig_name, &del_data);
2372 1.1 christos
2373 1.16 christos result = dns_qp_getname(qp, trig_name, (void **)&data, NULL);
2374 1.1 christos if (result != ISC_R_SUCCESS) {
2375 1.1 christos return;
2376 1.1 christos }
2377 1.1 christos
2378 1.16 christos INSIST(data != NULL);
2379 1.1 christos
2380 1.16 christos del_data.set.qname &= data->set.qname;
2381 1.16 christos del_data.set.ns &= data->set.ns;
2382 1.16 christos del_data.wild.qname &= data->wild.qname;
2383 1.16 christos del_data.wild.ns &= data->wild.ns;
2384 1.1 christos
2385 1.3 christos exists = (del_data.set.qname != 0 || del_data.set.ns != 0 ||
2386 1.3 christos del_data.wild.qname != 0 || del_data.wild.ns != 0);
2387 1.1 christos
2388 1.16 christos data->set.qname &= ~del_data.set.qname;
2389 1.16 christos data->set.ns &= ~del_data.set.ns;
2390 1.16 christos data->wild.qname &= ~del_data.wild.qname;
2391 1.16 christos data->wild.ns &= ~del_data.wild.ns;
2392 1.1 christos
2393 1.16 christos if (data->set.qname == 0 && data->set.ns == 0 &&
2394 1.16 christos data->wild.qname == 0 && data->wild.ns == 0)
2395 1.7 christos {
2396 1.16 christos result = dns_qp_deletename(qp, trig_name, NULL, NULL);
2397 1.1 christos if (result != ISC_R_SUCCESS) {
2398 1.1 christos /*
2399 1.1 christos * bin/tests/system/rpz/tests.sh looks for
2400 1.1 christos * "rpz.*failed".
2401 1.1 christos */
2402 1.1 christos dns_name_format(src_name, namebuf, sizeof(namebuf));
2403 1.1 christos isc_log_write(dns_lctx, DNS_LOGCATEGORY_RPZ,
2404 1.1 christos DNS_LOGMODULE_RBTDB, DNS_RPZ_ERROR_LEVEL,
2405 1.15 christos "rpz del_name(%s) node delete "
2406 1.15 christos "failed: %s",
2407 1.1 christos namebuf, isc_result_totext(result));
2408 1.1 christos }
2409 1.1 christos }
2410 1.1 christos
2411 1.7 christos if (exists) {
2412 1.16 christos RWLOCK(&rpz->rpzs->search_lock, isc_rwlocktype_write);
2413 1.15 christos adj_trigger_cnt(rpz, rpz_type, NULL, 0, false);
2414 1.16 christos RWUNLOCK(&rpz->rpzs->search_lock, isc_rwlocktype_write);
2415 1.7 christos }
2416 1.16 christos
2417 1.16 christos dns_qp_compact(qp, DNS_QPGC_MAYBE);
2418 1.16 christos dns_qpmulti_commit(rpzs->table, &qp);
2419 1.1 christos }
2420 1.1 christos
2421 1.1 christos /*
2422 1.1 christos * Remove an IP address from the radix tree or a name from the summary database.
2423 1.1 christos */
2424 1.15 christos static void
2425 1.15 christos rpz_del(dns_rpz_zone_t *rpz, const dns_name_t *src_name) {
2426 1.1 christos dns_rpz_type_t rpz_type;
2427 1.15 christos dns_rpz_zones_t *rpzs = NULL;
2428 1.15 christos dns_rpz_num_t rpz_num;
2429 1.15 christos
2430 1.15 christos REQUIRE(rpz != NULL);
2431 1.15 christos
2432 1.15 christos rpzs = rpz->rpzs;
2433 1.15 christos rpz_num = rpz->num;
2434 1.1 christos
2435 1.1 christos REQUIRE(rpzs != NULL && rpz_num < rpzs->p.num_zones);
2436 1.1 christos
2437 1.1 christos rpz_type = type_from_name(rpzs, rpz, src_name);
2438 1.1 christos switch (rpz_type) {
2439 1.1 christos case DNS_RPZ_TYPE_QNAME:
2440 1.1 christos case DNS_RPZ_TYPE_NSDNAME:
2441 1.15 christos del_name(rpz, rpz_type, src_name);
2442 1.1 christos break;
2443 1.1 christos case DNS_RPZ_TYPE_CLIENT_IP:
2444 1.1 christos case DNS_RPZ_TYPE_IP:
2445 1.1 christos case DNS_RPZ_TYPE_NSIP:
2446 1.15 christos del_cidr(rpz, rpz_type, src_name);
2447 1.1 christos break;
2448 1.1 christos case DNS_RPZ_TYPE_BAD:
2449 1.1 christos break;
2450 1.1 christos }
2451 1.1 christos }
2452 1.1 christos
2453 1.1 christos /*
2454 1.1 christos * Search the summary radix tree to get a relative owner name in a
2455 1.1 christos * policy zone relevant to a triggering IP address.
2456 1.1 christos * rpz_type and zbits limit the search for IP address netaddr
2457 1.1 christos * return the policy zone's number or DNS_RPZ_INVALID_NUM
2458 1.1 christos * ip_name is the relative owner name found and
2459 1.1 christos * *prefixp is its prefix length.
2460 1.1 christos */
2461 1.1 christos dns_rpz_num_t
2462 1.1 christos dns_rpz_find_ip(dns_rpz_zones_t *rpzs, dns_rpz_type_t rpz_type,
2463 1.1 christos dns_rpz_zbits_t zbits, const isc_netaddr_t *netaddr,
2464 1.7 christos dns_name_t *ip_name, dns_rpz_prefix_t *prefixp) {
2465 1.1 christos dns_rpz_cidr_key_t tgt_ip;
2466 1.1 christos dns_rpz_addr_zbits_t tgt_set;
2467 1.15 christos dns_rpz_cidr_node_t *found = NULL;
2468 1.1 christos isc_result_t result;
2469 1.3 christos dns_rpz_num_t rpz_num = 0;
2470 1.1 christos dns_rpz_have_t have;
2471 1.1 christos int i;
2472 1.1 christos
2473 1.1 christos RWLOCK(&rpzs->search_lock, isc_rwlocktype_read);
2474 1.1 christos have = rpzs->have;
2475 1.1 christos RWUNLOCK(&rpzs->search_lock, isc_rwlocktype_read);
2476 1.1 christos
2477 1.1 christos /*
2478 1.1 christos * Convert IP address to CIDR tree key.
2479 1.1 christos */
2480 1.1 christos if (netaddr->family == AF_INET) {
2481 1.1 christos tgt_ip.w[0] = 0;
2482 1.1 christos tgt_ip.w[1] = 0;
2483 1.1 christos tgt_ip.w[2] = ADDR_V4MAPPED;
2484 1.1 christos tgt_ip.w[3] = ntohl(netaddr->type.in.s_addr);
2485 1.1 christos switch (rpz_type) {
2486 1.1 christos case DNS_RPZ_TYPE_CLIENT_IP:
2487 1.1 christos zbits &= have.client_ipv4;
2488 1.1 christos break;
2489 1.1 christos case DNS_RPZ_TYPE_IP:
2490 1.1 christos zbits &= have.ipv4;
2491 1.1 christos break;
2492 1.1 christos case DNS_RPZ_TYPE_NSIP:
2493 1.1 christos zbits &= have.nsipv4;
2494 1.1 christos break;
2495 1.1 christos default:
2496 1.11 christos UNREACHABLE();
2497 1.15 christos break;
2498 1.1 christos }
2499 1.1 christos } else if (netaddr->family == AF_INET6) {
2500 1.1 christos dns_rpz_cidr_key_t src_ip6;
2501 1.1 christos
2502 1.1 christos /*
2503 1.1 christos * Given the int aligned struct in_addr member of netaddr->type
2504 1.1 christos * one could cast netaddr->type.in6 to dns_rpz_cidr_key_t *,
2505 1.1 christos * but some people object.
2506 1.1 christos */
2507 1.1 christos memmove(src_ip6.w, &netaddr->type.in6, sizeof(src_ip6.w));
2508 1.1 christos for (i = 0; i < 4; i++) {
2509 1.1 christos tgt_ip.w[i] = ntohl(src_ip6.w[i]);
2510 1.1 christos }
2511 1.1 christos switch (rpz_type) {
2512 1.1 christos case DNS_RPZ_TYPE_CLIENT_IP:
2513 1.1 christos zbits &= have.client_ipv6;
2514 1.1 christos break;
2515 1.1 christos case DNS_RPZ_TYPE_IP:
2516 1.1 christos zbits &= have.ipv6;
2517 1.1 christos break;
2518 1.1 christos case DNS_RPZ_TYPE_NSIP:
2519 1.1 christos zbits &= have.nsipv6;
2520 1.1 christos break;
2521 1.1 christos default:
2522 1.11 christos UNREACHABLE();
2523 1.15 christos break;
2524 1.1 christos }
2525 1.1 christos } else {
2526 1.16 christos return DNS_RPZ_INVALID_NUM;
2527 1.1 christos }
2528 1.1 christos
2529 1.7 christos if (zbits == 0) {
2530 1.16 christos return DNS_RPZ_INVALID_NUM;
2531 1.7 christos }
2532 1.1 christos make_addr_set(&tgt_set, zbits, rpz_type);
2533 1.1 christos
2534 1.1 christos RWLOCK(&rpzs->search_lock, isc_rwlocktype_read);
2535 1.3 christos result = search(rpzs, &tgt_ip, 128, &tgt_set, false, &found);
2536 1.1 christos if (result == ISC_R_NOTFOUND) {
2537 1.1 christos /*
2538 1.1 christos * There are no eligible zones for this IP address.
2539 1.1 christos */
2540 1.1 christos RWUNLOCK(&rpzs->search_lock, isc_rwlocktype_read);
2541 1.16 christos return DNS_RPZ_INVALID_NUM;
2542 1.1 christos }
2543 1.1 christos
2544 1.1 christos /*
2545 1.1 christos * Construct the trigger name for the longest matching trigger
2546 1.1 christos * in the first eligible zone with a match.
2547 1.1 christos */
2548 1.1 christos *prefixp = found->prefix;
2549 1.1 christos switch (rpz_type) {
2550 1.1 christos case DNS_RPZ_TYPE_CLIENT_IP:
2551 1.1 christos rpz_num = zbit_to_num(found->set.client_ip & tgt_set.client_ip);
2552 1.1 christos break;
2553 1.1 christos case DNS_RPZ_TYPE_IP:
2554 1.1 christos rpz_num = zbit_to_num(found->set.ip & tgt_set.ip);
2555 1.1 christos break;
2556 1.1 christos case DNS_RPZ_TYPE_NSIP:
2557 1.1 christos rpz_num = zbit_to_num(found->set.nsip & tgt_set.nsip);
2558 1.1 christos break;
2559 1.1 christos default:
2560 1.11 christos UNREACHABLE();
2561 1.1 christos }
2562 1.1 christos result = ip2name(&found->ip, found->prefix, dns_rootname, ip_name);
2563 1.1 christos RWUNLOCK(&rpzs->search_lock, isc_rwlocktype_read);
2564 1.1 christos if (result != ISC_R_SUCCESS) {
2565 1.1 christos /*
2566 1.1 christos * bin/tests/system/rpz/tests.sh looks for "rpz.*failed".
2567 1.1 christos */
2568 1.1 christos isc_log_write(dns_lctx, DNS_LOGCATEGORY_RPZ,
2569 1.1 christos DNS_LOGMODULE_RBTDB, DNS_RPZ_ERROR_LEVEL,
2570 1.1 christos "rpz ip2name() failed: %s",
2571 1.1 christos isc_result_totext(result));
2572 1.16 christos return DNS_RPZ_INVALID_NUM;
2573 1.1 christos }
2574 1.16 christos return rpz_num;
2575 1.1 christos }
2576 1.1 christos
2577 1.1 christos /*
2578 1.1 christos * Search the summary radix tree for policy zones with triggers matching
2579 1.1 christos * a name.
2580 1.1 christos */
2581 1.1 christos dns_rpz_zbits_t
2582 1.1 christos dns_rpz_find_name(dns_rpz_zones_t *rpzs, dns_rpz_type_t rpz_type,
2583 1.7 christos dns_rpz_zbits_t zbits, dns_name_t *trig_name) {
2584 1.16 christos isc_result_t result;
2585 1.1 christos char namebuf[DNS_NAME_FORMATSIZE];
2586 1.16 christos nmdata_t *data = NULL;
2587 1.16 christos dns_rpz_zbits_t found_zbits = 0;
2588 1.16 christos dns_qpchain_t chain;
2589 1.16 christos dns_qpread_t qpr;
2590 1.6 christos int i;
2591 1.1 christos
2592 1.6 christos if (zbits == 0) {
2593 1.16 christos return 0;
2594 1.6 christos }
2595 1.1 christos
2596 1.16 christos dns_qpmulti_query(rpzs->table, &qpr);
2597 1.16 christos dns_qpchain_init(&qpr, &chain);
2598 1.9 christos
2599 1.16 christos result = dns_qp_lookup(&qpr, trig_name, NULL, NULL, &chain,
2600 1.16 christos (void **)&data, NULL);
2601 1.1 christos switch (result) {
2602 1.1 christos case ISC_R_SUCCESS:
2603 1.16 christos INSIST(data != NULL);
2604 1.16 christos if (rpz_type == DNS_RPZ_TYPE_QNAME) {
2605 1.16 christos found_zbits = data->set.qname;
2606 1.16 christos } else {
2607 1.16 christos found_zbits = data->set.ns;
2608 1.1 christos }
2609 1.11 christos FALLTHROUGH;
2610 1.6 christos
2611 1.1 christos case DNS_R_PARTIALMATCH:
2612 1.16 christos i = dns_qpchain_length(&chain);
2613 1.16 christos while (i-- > 0) {
2614 1.16 christos dns_qpchain_node(&chain, i, NULL, (void **)&data, NULL);
2615 1.16 christos INSIST(data != NULL);
2616 1.16 christos if (rpz_type == DNS_RPZ_TYPE_QNAME) {
2617 1.16 christos found_zbits |= data->wild.qname;
2618 1.9 christos } else {
2619 1.16 christos found_zbits |= data->wild.ns;
2620 1.9 christos }
2621 1.1 christos }
2622 1.1 christos break;
2623 1.1 christos
2624 1.1 christos case ISC_R_NOTFOUND:
2625 1.1 christos break;
2626 1.1 christos
2627 1.1 christos default:
2628 1.1 christos /*
2629 1.1 christos * bin/tests/system/rpz/tests.sh looks for "rpz.*failed".
2630 1.1 christos */
2631 1.1 christos dns_name_format(trig_name, namebuf, sizeof(namebuf));
2632 1.1 christos isc_log_write(dns_lctx, DNS_LOGCATEGORY_RPZ,
2633 1.1 christos DNS_LOGMODULE_RBTDB, DNS_RPZ_ERROR_LEVEL,
2634 1.7 christos "dns_rpz_find_name(%s) failed: %s", namebuf,
2635 1.7 christos isc_result_totext(result));
2636 1.1 christos break;
2637 1.1 christos }
2638 1.1 christos
2639 1.16 christos dns_qpread_destroy(rpzs->table, &qpr);
2640 1.16 christos return zbits & found_zbits;
2641 1.1 christos }
2642 1.1 christos
2643 1.1 christos /*
2644 1.1 christos * Translate CNAME rdata to a QNAME response policy action.
2645 1.1 christos */
2646 1.1 christos dns_rpz_policy_t
2647 1.1 christos dns_rpz_decode_cname(dns_rpz_zone_t *rpz, dns_rdataset_t *rdataset,
2648 1.7 christos dns_name_t *selfname) {
2649 1.1 christos dns_rdata_t rdata = DNS_RDATA_INIT;
2650 1.1 christos dns_rdata_cname_t cname;
2651 1.1 christos isc_result_t result;
2652 1.1 christos
2653 1.1 christos result = dns_rdataset_first(rdataset);
2654 1.1 christos INSIST(result == ISC_R_SUCCESS);
2655 1.1 christos dns_rdataset_current(rdataset, &rdata);
2656 1.1 christos result = dns_rdata_tostruct(&rdata, &cname, NULL);
2657 1.1 christos INSIST(result == ISC_R_SUCCESS);
2658 1.1 christos dns_rdata_reset(&rdata);
2659 1.1 christos
2660 1.1 christos /*
2661 1.1 christos * CNAME . means NXDOMAIN
2662 1.1 christos */
2663 1.7 christos if (dns_name_equal(&cname.cname, dns_rootname)) {
2664 1.16 christos return DNS_RPZ_POLICY_NXDOMAIN;
2665 1.7 christos }
2666 1.1 christos
2667 1.1 christos if (dns_name_iswildcard(&cname.cname)) {
2668 1.1 christos /*
2669 1.1 christos * CNAME *. means NODATA
2670 1.1 christos */
2671 1.7 christos if (dns_name_countlabels(&cname.cname) == 2) {
2672 1.16 christos return DNS_RPZ_POLICY_NODATA;
2673 1.7 christos }
2674 1.1 christos
2675 1.1 christos /*
2676 1.1 christos * A qname of www.evil.com and a policy of
2677 1.1 christos * *.evil.com CNAME *.garden.net
2678 1.1 christos * gives a result of
2679 1.1 christos * evil.com CNAME evil.com.garden.net
2680 1.1 christos */
2681 1.7 christos if (dns_name_countlabels(&cname.cname) > 2) {
2682 1.16 christos return DNS_RPZ_POLICY_WILDCNAME;
2683 1.7 christos }
2684 1.1 christos }
2685 1.1 christos
2686 1.1 christos /*
2687 1.1 christos * CNAME rpz-tcp-only. means "send truncated UDP responses."
2688 1.1 christos */
2689 1.7 christos if (dns_name_equal(&cname.cname, &rpz->tcp_only)) {
2690 1.16 christos return DNS_RPZ_POLICY_TCP_ONLY;
2691 1.7 christos }
2692 1.1 christos
2693 1.1 christos /*
2694 1.1 christos * CNAME rpz-drop. means "do not respond."
2695 1.1 christos */
2696 1.7 christos if (dns_name_equal(&cname.cname, &rpz->drop)) {
2697 1.16 christos return DNS_RPZ_POLICY_DROP;
2698 1.7 christos }
2699 1.1 christos
2700 1.1 christos /*
2701 1.1 christos * CNAME rpz-passthru. means "do not rewrite."
2702 1.1 christos */
2703 1.7 christos if (dns_name_equal(&cname.cname, &rpz->passthru)) {
2704 1.16 christos return DNS_RPZ_POLICY_PASSTHRU;
2705 1.7 christos }
2706 1.1 christos
2707 1.1 christos /*
2708 1.1 christos * 128.1.0.127.rpz-ip CNAME 128.1.0.0.127. is obsolete PASSTHRU
2709 1.1 christos */
2710 1.7 christos if (selfname != NULL && dns_name_equal(&cname.cname, selfname)) {
2711 1.16 christos return DNS_RPZ_POLICY_PASSTHRU;
2712 1.7 christos }
2713 1.1 christos
2714 1.1 christos /*
2715 1.1 christos * Any other rdata gives a response consisting of the rdata.
2716 1.1 christos */
2717 1.16 christos return DNS_RPZ_POLICY_RECORD;
2718 1.16 christos }
2719 1.16 christos
2720 1.16 christos static void
2721 1.16 christos destroy_nmdata(nmdata_t *data) {
2722 1.16 christos dns_name_free(&data->name, data->mctx);
2723 1.16 christos isc_mem_putanddetach(&data->mctx, data, sizeof(nmdata_t));
2724 1.16 christos }
2725 1.16 christos
2726 1.16 christos #ifdef DNS_RPZ_TRACE
2727 1.16 christos ISC_REFCOUNT_TRACE_IMPL(nmdata, destroy_nmdata);
2728 1.16 christos #else
2729 1.16 christos ISC_REFCOUNT_IMPL(nmdata, destroy_nmdata);
2730 1.16 christos #endif
2731 1.16 christos
2732 1.16 christos static void
2733 1.16 christos qp_attach(void *uctx ISC_ATTR_UNUSED, void *pval,
2734 1.16 christos uint32_t ival ISC_ATTR_UNUSED) {
2735 1.16 christos nmdata_t *data = pval;
2736 1.16 christos nmdata_ref(data);
2737 1.16 christos }
2738 1.16 christos
2739 1.16 christos static void
2740 1.16 christos qp_detach(void *uctx ISC_ATTR_UNUSED, void *pval,
2741 1.16 christos uint32_t ival ISC_ATTR_UNUSED) {
2742 1.16 christos nmdata_t *data = pval;
2743 1.16 christos nmdata_detach(&data);
2744 1.16 christos }
2745 1.16 christos
2746 1.16 christos static size_t
2747 1.16 christos qp_makekey(dns_qpkey_t key, void *uctx ISC_ATTR_UNUSED, void *pval,
2748 1.16 christos uint32_t ival ISC_ATTR_UNUSED) {
2749 1.16 christos nmdata_t *data = pval;
2750 1.16 christos return dns_qpkey_fromname(key, &data->name);
2751 1.16 christos }
2752 1.16 christos
2753 1.16 christos static void
2754 1.16 christos qp_triename(void *uctx, char *buf, size_t size) {
2755 1.16 christos dns_view_t *view = uctx;
2756 1.16 christos snprintf(buf, size, "view %s RPZs", view->name);
2757 1.1 christos }
2758