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