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