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