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