Home | History | Annotate | Line # | Download | only in respip
respip.c revision 1.1.1.5
      1      1.1  christos /*
      2      1.1  christos  * respip/respip.c - filtering response IP module
      3      1.1  christos  */
      4      1.1  christos 
      5      1.1  christos /**
      6      1.1  christos  * \file
      7      1.1  christos  *
      8      1.1  christos  * This file contains a module that inspects a result of recursive resolution
      9      1.1  christos  * to see if any IP address record should trigger a special action.
     10      1.1  christos  * If applicable these actions can modify the original response.
     11      1.1  christos  */
     12      1.1  christos #include "config.h"
     13      1.1  christos 
     14      1.1  christos #include "services/localzone.h"
     15  1.1.1.4  christos #include "services/authzone.h"
     16      1.1  christos #include "services/cache/dns.h"
     17      1.1  christos #include "sldns/str2wire.h"
     18      1.1  christos #include "util/config_file.h"
     19      1.1  christos #include "util/fptr_wlist.h"
     20      1.1  christos #include "util/module.h"
     21      1.1  christos #include "util/net_help.h"
     22      1.1  christos #include "util/regional.h"
     23      1.1  christos #include "util/data/msgreply.h"
     24      1.1  christos #include "util/storage/dnstree.h"
     25      1.1  christos #include "respip/respip.h"
     26      1.1  christos #include "services/view.h"
     27      1.1  christos #include "sldns/rrdef.h"
     28  1.1.1.5  christos #include "util/data/dname.h"
     29      1.1  christos 
     30      1.1  christos 
     31      1.1  christos /** Subset of resp_addr.node, used for inform-variant logging */
     32      1.1  christos struct respip_addr_info {
     33      1.1  christos 	struct sockaddr_storage addr;
     34      1.1  christos 	socklen_t addrlen;
     35      1.1  christos 	int net;
     36      1.1  christos };
     37      1.1  christos 
     38      1.1  christos /** Query state regarding the response-ip module. */
     39      1.1  christos enum respip_state {
     40      1.1  christos 	/**
     41      1.1  christos 	 * The general state.  Unless CNAME chasing takes place, all processing
     42      1.1  christos 	 * is completed in this state without any other asynchronous event.
     43      1.1  christos 	 */
     44      1.1  christos 	RESPIP_INIT = 0,
     45      1.1  christos 
     46      1.1  christos 	/**
     47      1.1  christos 	 * A subquery for CNAME chasing is completed.
     48      1.1  christos 	 */
     49      1.1  christos 	RESPIP_SUBQUERY_FINISHED
     50      1.1  christos };
     51      1.1  christos 
     52      1.1  christos /** Per query state for the response-ip module. */
     53      1.1  christos struct respip_qstate {
     54      1.1  christos 	enum respip_state state;
     55      1.1  christos };
     56      1.1  christos 
     57      1.1  christos struct respip_set*
     58      1.1  christos respip_set_create(void)
     59      1.1  christos {
     60      1.1  christos 	struct respip_set* set = calloc(1, sizeof(*set));
     61      1.1  christos 	if(!set)
     62      1.1  christos 		return NULL;
     63      1.1  christos 	set->region = regional_create();
     64      1.1  christos 	if(!set->region) {
     65      1.1  christos 		free(set);
     66      1.1  christos 		return NULL;
     67      1.1  christos 	}
     68      1.1  christos 	addr_tree_init(&set->ip_tree);
     69  1.1.1.4  christos 	lock_rw_init(&set->lock);
     70      1.1  christos 	return set;
     71      1.1  christos }
     72      1.1  christos 
     73  1.1.1.4  christos /** helper traverse to delete resp_addr nodes */
     74  1.1.1.4  christos static void
     75  1.1.1.4  christos resp_addr_del(rbnode_type* n, void* ATTR_UNUSED(arg))
     76  1.1.1.4  christos {
     77  1.1.1.4  christos 	struct resp_addr* r = (struct resp_addr*)n->key;
     78  1.1.1.4  christos 	lock_rw_destroy(&r->lock);
     79  1.1.1.4  christos #ifdef THREADS_DISABLED
     80  1.1.1.4  christos 	(void)r;
     81  1.1.1.4  christos #endif
     82  1.1.1.4  christos }
     83  1.1.1.4  christos 
     84      1.1  christos void
     85      1.1  christos respip_set_delete(struct respip_set* set)
     86      1.1  christos {
     87      1.1  christos 	if(!set)
     88      1.1  christos 		return;
     89  1.1.1.4  christos 	lock_rw_destroy(&set->lock);
     90  1.1.1.4  christos 	traverse_postorder(&set->ip_tree, resp_addr_del, NULL);
     91      1.1  christos 	regional_destroy(set->region);
     92      1.1  christos 	free(set);
     93      1.1  christos }
     94      1.1  christos 
     95      1.1  christos struct rbtree_type*
     96      1.1  christos respip_set_get_tree(struct respip_set* set)
     97      1.1  christos {
     98      1.1  christos 	if(!set)
     99      1.1  christos 		return NULL;
    100      1.1  christos 	return &set->ip_tree;
    101      1.1  christos }
    102      1.1  christos 
    103  1.1.1.4  christos struct resp_addr*
    104  1.1.1.4  christos respip_sockaddr_find_or_create(struct respip_set* set, struct sockaddr_storage* addr,
    105  1.1.1.4  christos 		socklen_t addrlen, int net, int create, const char* ipstr)
    106      1.1  christos {
    107      1.1  christos 	struct resp_addr* node;
    108  1.1.1.4  christos 	node = (struct resp_addr*)addr_tree_find(&set->ip_tree, addr, addrlen, net);
    109      1.1  christos 	if(!node && create) {
    110      1.1  christos 		node = regional_alloc_zero(set->region, sizeof(*node));
    111      1.1  christos 		if(!node) {
    112      1.1  christos 			log_err("out of memory");
    113      1.1  christos 			return NULL;
    114      1.1  christos 		}
    115  1.1.1.4  christos 		lock_rw_init(&node->lock);
    116      1.1  christos 		node->action = respip_none;
    117  1.1.1.4  christos 		if(!addr_tree_insert(&set->ip_tree, &node->node, addr,
    118      1.1  christos 			addrlen, net)) {
    119      1.1  christos 			/* We know we didn't find it, so this should be
    120      1.1  christos 			 * impossible. */
    121      1.1  christos 			log_warn("unexpected: duplicate address: %s", ipstr);
    122      1.1  christos 		}
    123      1.1  christos 	}
    124      1.1  christos 	return node;
    125      1.1  christos }
    126      1.1  christos 
    127  1.1.1.4  christos void
    128  1.1.1.4  christos respip_sockaddr_delete(struct respip_set* set, struct resp_addr* node)
    129  1.1.1.4  christos {
    130  1.1.1.4  christos 	struct resp_addr* prev;
    131  1.1.1.4  christos 	prev = (struct resp_addr*)rbtree_previous((struct rbnode_type*)node);
    132  1.1.1.4  christos 	lock_rw_destroy(&node->lock);
    133  1.1.1.5  christos 	(void)rbtree_delete(&set->ip_tree, node);
    134  1.1.1.4  christos 	/* no free'ing, all allocated in region */
    135  1.1.1.4  christos 	if(!prev)
    136  1.1.1.4  christos 		addr_tree_init_parents((rbtree_type*)set);
    137  1.1.1.4  christos 	else
    138  1.1.1.4  christos 		addr_tree_init_parents_node(&prev->node);
    139  1.1.1.4  christos }
    140  1.1.1.4  christos 
    141  1.1.1.4  christos /** returns the node in the address tree for the specified netblock string;
    142  1.1.1.4  christos  * non-existent node will be created if 'create' is true */
    143  1.1.1.4  christos static struct resp_addr*
    144  1.1.1.4  christos respip_find_or_create(struct respip_set* set, const char* ipstr, int create)
    145  1.1.1.4  christos {
    146  1.1.1.4  christos 	struct sockaddr_storage addr;
    147  1.1.1.4  christos 	int net;
    148  1.1.1.4  christos 	socklen_t addrlen;
    149  1.1.1.4  christos 
    150  1.1.1.4  christos 	if(!netblockstrtoaddr(ipstr, 0, &addr, &addrlen, &net)) {
    151  1.1.1.4  christos 		log_err("cannot parse netblock: '%s'", ipstr);
    152  1.1.1.4  christos 		return NULL;
    153  1.1.1.4  christos 	}
    154  1.1.1.4  christos 	return respip_sockaddr_find_or_create(set, &addr, addrlen, net, create,
    155  1.1.1.4  christos 		ipstr);
    156  1.1.1.4  christos }
    157  1.1.1.4  christos 
    158      1.1  christos static int
    159      1.1  christos respip_tag_cfg(struct respip_set* set, const char* ipstr,
    160      1.1  christos 	const uint8_t* taglist, size_t taglen)
    161      1.1  christos {
    162      1.1  christos 	struct resp_addr* node;
    163      1.1  christos 
    164      1.1  christos 	if(!(node=respip_find_or_create(set, ipstr, 1)))
    165      1.1  christos 		return 0;
    166      1.1  christos 	if(node->taglist) {
    167      1.1  christos 		log_warn("duplicate response-address-tag for '%s', overridden.",
    168      1.1  christos 			ipstr);
    169      1.1  christos 	}
    170      1.1  christos 	node->taglist = regional_alloc_init(set->region, taglist, taglen);
    171      1.1  christos 	if(!node->taglist) {
    172      1.1  christos 		log_err("out of memory");
    173      1.1  christos 		return 0;
    174      1.1  christos 	}
    175      1.1  christos 	node->taglen = taglen;
    176      1.1  christos 	return 1;
    177      1.1  christos }
    178      1.1  christos 
    179      1.1  christos /** set action for the node specified by the netblock string */
    180      1.1  christos static int
    181      1.1  christos respip_action_cfg(struct respip_set* set, const char* ipstr,
    182      1.1  christos 	const char* actnstr)
    183      1.1  christos {
    184      1.1  christos 	struct resp_addr* node;
    185      1.1  christos 	enum respip_action action;
    186      1.1  christos 
    187      1.1  christos 	if(!(node=respip_find_or_create(set, ipstr, 1)))
    188      1.1  christos 		return 0;
    189      1.1  christos 	if(node->action != respip_none) {
    190  1.1.1.2  christos 		verbose(VERB_QUERY, "duplicate response-ip action for '%s', overridden.",
    191      1.1  christos 			ipstr);
    192      1.1  christos 	}
    193      1.1  christos         if(strcmp(actnstr, "deny") == 0)
    194      1.1  christos                 action = respip_deny;
    195      1.1  christos         else if(strcmp(actnstr, "redirect") == 0)
    196      1.1  christos                 action = respip_redirect;
    197      1.1  christos         else if(strcmp(actnstr, "inform") == 0)
    198      1.1  christos                 action = respip_inform;
    199      1.1  christos         else if(strcmp(actnstr, "inform_deny") == 0)
    200      1.1  christos                 action = respip_inform_deny;
    201  1.1.1.2  christos         else if(strcmp(actnstr, "inform_redirect") == 0)
    202  1.1.1.2  christos                 action = respip_inform_redirect;
    203      1.1  christos         else if(strcmp(actnstr, "always_transparent") == 0)
    204      1.1  christos                 action = respip_always_transparent;
    205      1.1  christos         else if(strcmp(actnstr, "always_refuse") == 0)
    206      1.1  christos                 action = respip_always_refuse;
    207      1.1  christos         else if(strcmp(actnstr, "always_nxdomain") == 0)
    208      1.1  christos                 action = respip_always_nxdomain;
    209  1.1.1.4  christos         else if(strcmp(actnstr, "always_nodata") == 0)
    210  1.1.1.4  christos                 action = respip_always_nodata;
    211  1.1.1.4  christos         else if(strcmp(actnstr, "always_deny") == 0)
    212  1.1.1.4  christos                 action = respip_always_deny;
    213      1.1  christos         else {
    214      1.1  christos                 log_err("unknown response-ip action %s", actnstr);
    215      1.1  christos                 return 0;
    216      1.1  christos         }
    217      1.1  christos 	node->action = action;
    218      1.1  christos 	return 1;
    219      1.1  christos }
    220      1.1  christos 
    221      1.1  christos /** allocate and initialize an rrset structure; this function is based
    222      1.1  christos  * on new_local_rrset() from the localzone.c module */
    223      1.1  christos static struct ub_packed_rrset_key*
    224      1.1  christos new_rrset(struct regional* region, uint16_t rrtype, uint16_t rrclass)
    225      1.1  christos {
    226      1.1  christos 	struct packed_rrset_data* pd;
    227      1.1  christos 	struct ub_packed_rrset_key* rrset = regional_alloc_zero(
    228      1.1  christos 		region, sizeof(*rrset));
    229      1.1  christos 	if(!rrset) {
    230      1.1  christos 		log_err("out of memory");
    231      1.1  christos 		return NULL;
    232      1.1  christos 	}
    233      1.1  christos 	rrset->entry.key = rrset;
    234      1.1  christos 	pd = regional_alloc_zero(region, sizeof(*pd));
    235      1.1  christos 	if(!pd) {
    236      1.1  christos 		log_err("out of memory");
    237      1.1  christos 		return NULL;
    238      1.1  christos 	}
    239      1.1  christos 	pd->trust = rrset_trust_prim_noglue;
    240      1.1  christos 	pd->security = sec_status_insecure;
    241      1.1  christos 	rrset->entry.data = pd;
    242      1.1  christos 	rrset->rk.dname = regional_alloc_zero(region, 1);
    243      1.1  christos 	if(!rrset->rk.dname) {
    244      1.1  christos 		log_err("out of memory");
    245      1.1  christos 		return NULL;
    246      1.1  christos 	}
    247      1.1  christos 	rrset->rk.dname_len = 1;
    248      1.1  christos 	rrset->rk.type = htons(rrtype);
    249      1.1  christos 	rrset->rk.rrset_class = htons(rrclass);
    250      1.1  christos 	return rrset;
    251      1.1  christos }
    252      1.1  christos 
    253      1.1  christos /** enter local data as resource records into a response-ip node */
    254  1.1.1.4  christos 
    255  1.1.1.4  christos int
    256      1.1  christos respip_enter_rr(struct regional* region, struct resp_addr* raddr,
    257  1.1.1.4  christos 	uint16_t rrtype, uint16_t rrclass, time_t ttl, uint8_t* rdata,
    258  1.1.1.4  christos 	size_t rdata_len, const char* rrstr, const char* netblockstr)
    259  1.1.1.4  christos {
    260  1.1.1.4  christos 	struct packed_rrset_data* pd;
    261  1.1.1.4  christos 	struct sockaddr* sa;
    262  1.1.1.4  christos 	sa = (struct sockaddr*)&raddr->node.addr;
    263  1.1.1.4  christos 	if (rrtype == LDNS_RR_TYPE_CNAME && raddr->data) {
    264  1.1.1.4  christos 		log_err("CNAME response-ip data (%s) can not co-exist with other "
    265  1.1.1.4  christos 			"response-ip data for netblock %s", rrstr, netblockstr);
    266  1.1.1.4  christos 		return 0;
    267  1.1.1.4  christos 	} else if (raddr->data &&
    268  1.1.1.4  christos 		raddr->data->rk.type == htons(LDNS_RR_TYPE_CNAME)) {
    269  1.1.1.4  christos 		log_err("response-ip data (%s) can not be added; CNAME response-ip "
    270  1.1.1.4  christos 			"data already in place for netblock %s", rrstr, netblockstr);
    271  1.1.1.4  christos 		return 0;
    272  1.1.1.4  christos 	} else if((rrtype != LDNS_RR_TYPE_CNAME) &&
    273  1.1.1.4  christos 		((sa->sa_family == AF_INET && rrtype != LDNS_RR_TYPE_A) ||
    274  1.1.1.4  christos 		(sa->sa_family == AF_INET6 && rrtype != LDNS_RR_TYPE_AAAA))) {
    275  1.1.1.4  christos 		log_err("response-ip data %s record type does not correspond "
    276  1.1.1.4  christos 			"to netblock %s address family", rrstr, netblockstr);
    277  1.1.1.4  christos 		return 0;
    278  1.1.1.4  christos 	}
    279  1.1.1.4  christos 
    280  1.1.1.4  christos 	if(!raddr->data) {
    281  1.1.1.4  christos 		raddr->data = new_rrset(region, rrtype, rrclass);
    282  1.1.1.4  christos 		if(!raddr->data)
    283  1.1.1.4  christos 			return 0;
    284  1.1.1.4  christos 	}
    285  1.1.1.4  christos 	pd = raddr->data->entry.data;
    286  1.1.1.4  christos 	return rrset_insert_rr(region, pd, rdata, rdata_len, ttl, rrstr);
    287  1.1.1.4  christos }
    288  1.1.1.4  christos 
    289  1.1.1.4  christos static int
    290  1.1.1.4  christos respip_enter_rrstr(struct regional* region, struct resp_addr* raddr,
    291      1.1  christos 		const char* rrstr, const char* netblock)
    292      1.1  christos {
    293      1.1  christos 	uint8_t* nm;
    294      1.1  christos 	uint16_t rrtype = 0, rrclass = 0;
    295      1.1  christos 	time_t ttl = 0;
    296      1.1  christos 	uint8_t rr[LDNS_RR_BUF_SIZE];
    297      1.1  christos 	uint8_t* rdata = NULL;
    298      1.1  christos 	size_t rdata_len = 0;
    299      1.1  christos 	char buf[65536];
    300      1.1  christos 	char bufshort[64];
    301      1.1  christos 	int ret;
    302  1.1.1.2  christos 	if(raddr->action != respip_redirect
    303  1.1.1.2  christos 		&& raddr->action != respip_inform_redirect) {
    304      1.1  christos 		log_err("cannot parse response-ip-data %s: response-ip "
    305      1.1  christos 			"action for %s is not redirect", rrstr, netblock);
    306      1.1  christos 		return 0;
    307      1.1  christos 	}
    308      1.1  christos 	ret = snprintf(buf, sizeof(buf), ". %s", rrstr);
    309      1.1  christos 	if(ret < 0 || ret >= (int)sizeof(buf)) {
    310      1.1  christos 		strlcpy(bufshort, rrstr, sizeof(bufshort));
    311      1.1  christos 		log_err("bad response-ip-data: %s...", bufshort);
    312      1.1  christos 		return 0;
    313      1.1  christos 	}
    314      1.1  christos 	if(!rrstr_get_rr_content(buf, &nm, &rrtype, &rrclass, &ttl, rr, sizeof(rr),
    315      1.1  christos 		&rdata, &rdata_len)) {
    316      1.1  christos 		log_err("bad response-ip-data: %s", rrstr);
    317      1.1  christos 		return 0;
    318      1.1  christos 	}
    319      1.1  christos 	free(nm);
    320  1.1.1.4  christos 	return respip_enter_rr(region, raddr, rrtype, rrclass, ttl, rdata,
    321  1.1.1.4  christos 		rdata_len, rrstr, netblock);
    322      1.1  christos }
    323      1.1  christos 
    324      1.1  christos static int
    325      1.1  christos respip_data_cfg(struct respip_set* set, const char* ipstr, const char* rrstr)
    326      1.1  christos {
    327      1.1  christos 	struct resp_addr* node;
    328      1.1  christos 
    329      1.1  christos 	node=respip_find_or_create(set, ipstr, 0);
    330      1.1  christos 	if(!node || node->action == respip_none) {
    331      1.1  christos 		log_err("cannot parse response-ip-data %s: "
    332      1.1  christos 			"response-ip node for %s not found", rrstr, ipstr);
    333      1.1  christos 		return 0;
    334      1.1  christos 	}
    335  1.1.1.4  christos 	return respip_enter_rrstr(set->region, node, rrstr, ipstr);
    336      1.1  christos }
    337      1.1  christos 
    338      1.1  christos static int
    339      1.1  christos respip_set_apply_cfg(struct respip_set* set, char* const* tagname, int num_tags,
    340      1.1  christos 	struct config_strbytelist* respip_tags,
    341      1.1  christos 	struct config_str2list* respip_actions,
    342      1.1  christos 	struct config_str2list* respip_data)
    343      1.1  christos {
    344      1.1  christos 	struct config_strbytelist* p;
    345      1.1  christos 	struct config_str2list* pa;
    346      1.1  christos 	struct config_str2list* pd;
    347      1.1  christos 
    348      1.1  christos 	set->tagname = tagname;
    349      1.1  christos 	set->num_tags = num_tags;
    350      1.1  christos 
    351      1.1  christos 	p = respip_tags;
    352      1.1  christos 	while(p) {
    353      1.1  christos 		struct config_strbytelist* np = p->next;
    354      1.1  christos 
    355      1.1  christos 		log_assert(p->str && p->str2);
    356      1.1  christos 		if(!respip_tag_cfg(set, p->str, p->str2, p->str2len)) {
    357      1.1  christos 			config_del_strbytelist(p);
    358      1.1  christos 			return 0;
    359      1.1  christos 		}
    360      1.1  christos 		free(p->str);
    361      1.1  christos 		free(p->str2);
    362      1.1  christos 		free(p);
    363      1.1  christos 		p = np;
    364      1.1  christos 	}
    365      1.1  christos 
    366      1.1  christos 	pa = respip_actions;
    367      1.1  christos 	while(pa) {
    368      1.1  christos 		struct config_str2list* np = pa->next;
    369      1.1  christos 		log_assert(pa->str && pa->str2);
    370      1.1  christos 		if(!respip_action_cfg(set, pa->str, pa->str2)) {
    371      1.1  christos 			config_deldblstrlist(pa);
    372      1.1  christos 			return 0;
    373      1.1  christos 		}
    374      1.1  christos 		free(pa->str);
    375      1.1  christos 		free(pa->str2);
    376      1.1  christos 		free(pa);
    377      1.1  christos 		pa = np;
    378      1.1  christos 	}
    379      1.1  christos 
    380      1.1  christos 	pd = respip_data;
    381      1.1  christos 	while(pd) {
    382      1.1  christos 		struct config_str2list* np = pd->next;
    383      1.1  christos 		log_assert(pd->str && pd->str2);
    384      1.1  christos 		if(!respip_data_cfg(set, pd->str, pd->str2)) {
    385      1.1  christos 			config_deldblstrlist(pd);
    386      1.1  christos 			return 0;
    387      1.1  christos 		}
    388      1.1  christos 		free(pd->str);
    389      1.1  christos 		free(pd->str2);
    390      1.1  christos 		free(pd);
    391      1.1  christos 		pd = np;
    392      1.1  christos 	}
    393  1.1.1.3  christos 	addr_tree_init_parents(&set->ip_tree);
    394      1.1  christos 
    395      1.1  christos 	return 1;
    396      1.1  christos }
    397      1.1  christos 
    398      1.1  christos int
    399      1.1  christos respip_global_apply_cfg(struct respip_set* set, struct config_file* cfg)
    400      1.1  christos {
    401      1.1  christos 	int ret = respip_set_apply_cfg(set, cfg->tagname, cfg->num_tags,
    402      1.1  christos 		cfg->respip_tags, cfg->respip_actions, cfg->respip_data);
    403      1.1  christos 	cfg->respip_data = NULL;
    404      1.1  christos 	cfg->respip_actions = NULL;
    405      1.1  christos 	cfg->respip_tags = NULL;
    406      1.1  christos 	return ret;
    407      1.1  christos }
    408      1.1  christos 
    409      1.1  christos /** Iterate through raw view data and apply the view-specific respip
    410      1.1  christos  * configuration; at this point we should have already seen all the views,
    411      1.1  christos  * so if any of the views that respip data refer to does not exist, that's
    412      1.1  christos  * an error.  This additional iteration through view configuration data
    413      1.1  christos  * is expected to not have significant performance impact (or rather, its
    414      1.1  christos  * performance impact is not expected to be prohibitive in the configuration
    415      1.1  christos  * processing phase).
    416      1.1  christos  */
    417      1.1  christos int
    418      1.1  christos respip_views_apply_cfg(struct views* vs, struct config_file* cfg,
    419      1.1  christos 	int* have_view_respip_cfg)
    420      1.1  christos {
    421      1.1  christos 	struct config_view* cv;
    422      1.1  christos 	struct view* v;
    423      1.1  christos 	int ret;
    424      1.1  christos 
    425      1.1  christos 	for(cv = cfg->views; cv; cv = cv->next) {
    426      1.1  christos 
    427      1.1  christos 		/** if no respip config for this view then there's
    428      1.1  christos 		  * nothing to do; note that even though respip data must go
    429      1.1  christos 		  * with respip action, we're checking for both here because
    430      1.1  christos 		  * we want to catch the case where the respip action is missing
    431      1.1  christos 		  * while the data is present */
    432      1.1  christos 		if(!cv->respip_actions && !cv->respip_data)
    433      1.1  christos 			continue;
    434      1.1  christos 
    435      1.1  christos 		if(!(v = views_find_view(vs, cv->name, 1))) {
    436      1.1  christos 			log_err("view '%s' unexpectedly missing", cv->name);
    437      1.1  christos 			return 0;
    438      1.1  christos 		}
    439      1.1  christos 		if(!v->respip_set) {
    440      1.1  christos 			v->respip_set = respip_set_create();
    441      1.1  christos 			if(!v->respip_set) {
    442      1.1  christos 				log_err("out of memory");
    443      1.1  christos 				lock_rw_unlock(&v->lock);
    444      1.1  christos 				return 0;
    445      1.1  christos 			}
    446      1.1  christos 		}
    447      1.1  christos 		ret = respip_set_apply_cfg(v->respip_set, NULL, 0, NULL,
    448      1.1  christos 			cv->respip_actions, cv->respip_data);
    449      1.1  christos 		lock_rw_unlock(&v->lock);
    450      1.1  christos 		if(!ret) {
    451      1.1  christos 			log_err("Error while applying respip configuration "
    452      1.1  christos 				"for view '%s'", cv->name);
    453      1.1  christos 			return 0;
    454      1.1  christos 		}
    455      1.1  christos 		*have_view_respip_cfg = (*have_view_respip_cfg ||
    456      1.1  christos 			v->respip_set->ip_tree.count);
    457      1.1  christos 		cv->respip_actions = NULL;
    458      1.1  christos 		cv->respip_data = NULL;
    459      1.1  christos 	}
    460      1.1  christos 	return 1;
    461      1.1  christos }
    462      1.1  christos 
    463      1.1  christos /**
    464      1.1  christos  * make a deep copy of 'key' in 'region'.
    465      1.1  christos  * This is largely derived from packed_rrset_copy_region() and
    466      1.1  christos  * packed_rrset_ptr_fixup(), but differs in the following points:
    467      1.1  christos  *
    468      1.1  christos  * - It doesn't assume all data in 'key' are in a contiguous memory region.
    469      1.1  christos  *   Although that would be the case in most cases, 'key' can be passed from
    470      1.1  christos  *   a lower-level module and it might not build the rrset to meet the
    471      1.1  christos  *   assumption.  In fact, an rrset specified as response-ip-data or generated
    472      1.1  christos  *   in local_data_find_tag_datas() breaks the assumption.  So it would be
    473      1.1  christos  *   safer not to naively rely on the assumption.  On the other hand, this
    474      1.1  christos  *   function ensures the copied rrset data are in a contiguous region so
    475      1.1  christos  *   that it won't cause a disruption even if an upper layer module naively
    476      1.1  christos  *   assumes the memory layout.
    477      1.1  christos  * - It doesn't copy RRSIGs (if any) in 'key'.  The rrset will be used in
    478      1.1  christos  *   a reply that was already faked, so it doesn't make much sense to provide
    479      1.1  christos  *   partial sigs even if they are valid themselves.
    480      1.1  christos  * - It doesn't adjust TTLs as it basically has to be a verbatim copy of 'key'
    481      1.1  christos  *   just allocated in 'region' (the assumption is necessary TTL adjustment
    482      1.1  christos  *   has been already done in 'key').
    483      1.1  christos  *
    484      1.1  christos  * This function returns the copied rrset key on success, and NULL on memory
    485      1.1  christos  * allocation failure.
    486      1.1  christos  */
    487  1.1.1.5  christos struct ub_packed_rrset_key*
    488  1.1.1.5  christos respip_copy_rrset(const struct ub_packed_rrset_key* key, struct regional* region)
    489      1.1  christos {
    490      1.1  christos 	struct ub_packed_rrset_key* ck = regional_alloc(region,
    491      1.1  christos 		sizeof(struct ub_packed_rrset_key));
    492      1.1  christos 	struct packed_rrset_data* d;
    493      1.1  christos 	struct packed_rrset_data* data = key->entry.data;
    494      1.1  christos 	size_t dsize, i;
    495      1.1  christos 	uint8_t* nextrdata;
    496      1.1  christos 
    497      1.1  christos 	/* derived from packed_rrset_copy_region(), but don't use
    498      1.1  christos 	 * packed_rrset_sizeof() and do exclude RRSIGs */
    499      1.1  christos 	if(!ck)
    500      1.1  christos 		return NULL;
    501      1.1  christos 	ck->id = key->id;
    502      1.1  christos 	memset(&ck->entry, 0, sizeof(ck->entry));
    503      1.1  christos 	ck->entry.hash = key->entry.hash;
    504      1.1  christos 	ck->entry.key = ck;
    505      1.1  christos 	ck->rk = key->rk;
    506  1.1.1.4  christos 	if(key->rk.dname) {
    507  1.1.1.4  christos 		ck->rk.dname = regional_alloc_init(region, key->rk.dname,
    508  1.1.1.4  christos 			key->rk.dname_len);
    509  1.1.1.4  christos 		if(!ck->rk.dname)
    510  1.1.1.4  christos 			return NULL;
    511  1.1.1.4  christos 		ck->rk.dname_len = key->rk.dname_len;
    512  1.1.1.4  christos 	} else {
    513  1.1.1.4  christos 		ck->rk.dname = NULL;
    514  1.1.1.4  christos 		ck->rk.dname_len = 0;
    515  1.1.1.4  christos 	}
    516      1.1  christos 
    517  1.1.1.3  christos 	if((unsigned)data->count >= 0xffff00U)
    518  1.1.1.3  christos 		return NULL; /* guard against integer overflow in dsize */
    519      1.1  christos 	dsize = sizeof(struct packed_rrset_data) + data->count *
    520      1.1  christos 		(sizeof(size_t)+sizeof(uint8_t*)+sizeof(time_t));
    521  1.1.1.3  christos 	for(i=0; i<data->count; i++) {
    522  1.1.1.3  christos 		if((unsigned)dsize >= 0x0fffffffU ||
    523  1.1.1.3  christos 			(unsigned)data->rr_len[i] >= 0x0fffffffU)
    524  1.1.1.3  christos 			return NULL; /* guard against integer overflow */
    525      1.1  christos 		dsize += data->rr_len[i];
    526  1.1.1.3  christos 	}
    527  1.1.1.4  christos 	d = regional_alloc_zero(region, dsize);
    528      1.1  christos 	if(!d)
    529      1.1  christos 		return NULL;
    530      1.1  christos 	*d = *data;
    531      1.1  christos 	d->rrsig_count = 0;
    532      1.1  christos 	ck->entry.data = d;
    533      1.1  christos 
    534      1.1  christos 	/* derived from packed_rrset_ptr_fixup() with copying the data */
    535      1.1  christos 	d->rr_len = (size_t*)((uint8_t*)d + sizeof(struct packed_rrset_data));
    536      1.1  christos 	d->rr_data = (uint8_t**)&(d->rr_len[d->count]);
    537      1.1  christos 	d->rr_ttl = (time_t*)&(d->rr_data[d->count]);
    538      1.1  christos 	nextrdata = (uint8_t*)&(d->rr_ttl[d->count]);
    539      1.1  christos 	for(i=0; i<d->count; i++) {
    540      1.1  christos 		d->rr_len[i] = data->rr_len[i];
    541      1.1  christos 		d->rr_ttl[i] = data->rr_ttl[i];
    542      1.1  christos 		d->rr_data[i] = nextrdata;
    543      1.1  christos 		memcpy(d->rr_data[i], data->rr_data[i], data->rr_len[i]);
    544      1.1  christos 		nextrdata += d->rr_len[i];
    545      1.1  christos 	}
    546      1.1  christos 
    547      1.1  christos 	return ck;
    548      1.1  christos }
    549      1.1  christos 
    550      1.1  christos int
    551      1.1  christos respip_init(struct module_env* env, int id)
    552      1.1  christos {
    553      1.1  christos 	(void)env;
    554      1.1  christos 	(void)id;
    555      1.1  christos 	return 1;
    556      1.1  christos }
    557      1.1  christos 
    558      1.1  christos void
    559      1.1  christos respip_deinit(struct module_env* env, int id)
    560      1.1  christos {
    561      1.1  christos 	(void)env;
    562      1.1  christos 	(void)id;
    563      1.1  christos }
    564      1.1  christos 
    565      1.1  christos /** Convert a packed AAAA or A RRset to sockaddr. */
    566      1.1  christos static int
    567      1.1  christos rdata2sockaddr(const struct packed_rrset_data* rd, uint16_t rtype, size_t i,
    568      1.1  christos 	struct sockaddr_storage* ss, socklen_t* addrlenp)
    569      1.1  christos {
    570      1.1  christos 	/* unbound can accept and cache odd-length AAAA/A records, so we have
    571      1.1  christos 	 * to validate the length. */
    572      1.1  christos 	if(rtype == LDNS_RR_TYPE_A && rd->rr_len[i] == 6) {
    573      1.1  christos 		struct sockaddr_in* sa4 = (struct sockaddr_in*)ss;
    574      1.1  christos 
    575      1.1  christos 		memset(sa4, 0, sizeof(*sa4));
    576      1.1  christos 		sa4->sin_family = AF_INET;
    577      1.1  christos 		memcpy(&sa4->sin_addr, rd->rr_data[i] + 2,
    578      1.1  christos 			sizeof(sa4->sin_addr));
    579      1.1  christos 		*addrlenp = sizeof(*sa4);
    580      1.1  christos 		return 1;
    581      1.1  christos 	} else if(rtype == LDNS_RR_TYPE_AAAA && rd->rr_len[i] == 18) {
    582      1.1  christos 		struct sockaddr_in6* sa6 = (struct sockaddr_in6*)ss;
    583      1.1  christos 
    584      1.1  christos 		memset(sa6, 0, sizeof(*sa6));
    585      1.1  christos 		sa6->sin6_family = AF_INET6;
    586      1.1  christos 		memcpy(&sa6->sin6_addr, rd->rr_data[i] + 2,
    587      1.1  christos 			sizeof(sa6->sin6_addr));
    588      1.1  christos 		*addrlenp = sizeof(*sa6);
    589      1.1  christos 		return 1;
    590      1.1  christos 	}
    591      1.1  christos 	return 0;
    592      1.1  christos }
    593      1.1  christos 
    594      1.1  christos /**
    595      1.1  christos  * Search the given 'iptree' for response address information that matches
    596      1.1  christos  * any of the IP addresses in an AAAA or A in the answer section of the
    597      1.1  christos  * response (stored in 'rep').  If found, a pointer to the matched resp_addr
    598      1.1  christos  * structure will be returned, and '*rrset_id' is set to the index in
    599      1.1  christos  * rep->rrsets for the RRset that contains the matching IP address record
    600      1.1  christos  * (the index is normally 0, but can be larger than that if this is a CNAME
    601      1.1  christos  * chain or type-ANY response).
    602  1.1.1.4  christos  * Returns resp_addr holding read lock.
    603      1.1  christos  */
    604  1.1.1.4  christos static struct resp_addr*
    605  1.1.1.4  christos respip_addr_lookup(const struct reply_info *rep, struct respip_set* rs,
    606  1.1.1.5  christos 	size_t* rrset_id, size_t* rr_id)
    607      1.1  christos {
    608      1.1  christos 	size_t i;
    609      1.1  christos 	struct resp_addr* ra;
    610      1.1  christos 	struct sockaddr_storage ss;
    611      1.1  christos 	socklen_t addrlen;
    612      1.1  christos 
    613  1.1.1.4  christos 	lock_rw_rdlock(&rs->lock);
    614      1.1  christos 	for(i=0; i<rep->an_numrrsets; i++) {
    615      1.1  christos 		size_t j;
    616      1.1  christos 		const struct packed_rrset_data* rd;
    617      1.1  christos 		uint16_t rtype = ntohs(rep->rrsets[i]->rk.type);
    618      1.1  christos 
    619      1.1  christos 		if(rtype != LDNS_RR_TYPE_A && rtype != LDNS_RR_TYPE_AAAA)
    620      1.1  christos 			continue;
    621      1.1  christos 		rd = rep->rrsets[i]->entry.data;
    622      1.1  christos 		for(j = 0; j < rd->count; j++) {
    623      1.1  christos 			if(!rdata2sockaddr(rd, rtype, j, &ss, &addrlen))
    624      1.1  christos 				continue;
    625  1.1.1.4  christos 			ra = (struct resp_addr*)addr_tree_lookup(&rs->ip_tree,
    626  1.1.1.4  christos 				&ss, addrlen);
    627      1.1  christos 			if(ra) {
    628      1.1  christos 				*rrset_id = i;
    629  1.1.1.5  christos 				*rr_id = j;
    630  1.1.1.4  christos 				lock_rw_rdlock(&ra->lock);
    631  1.1.1.4  christos 				lock_rw_unlock(&rs->lock);
    632      1.1  christos 				return ra;
    633      1.1  christos 			}
    634      1.1  christos 		}
    635      1.1  christos 	}
    636  1.1.1.4  christos 	lock_rw_unlock(&rs->lock);
    637      1.1  christos 	return NULL;
    638      1.1  christos }
    639      1.1  christos 
    640      1.1  christos /**
    641      1.1  christos  * See if response-ip or tag data should override the original answer rrset
    642      1.1  christos  * (which is rep->rrsets[rrset_id]) and if so override it.
    643      1.1  christos  * This is (mostly) equivalent to localzone.c:local_data_answer() but for
    644      1.1  christos  * response-ip actions.
    645      1.1  christos  * Note that this function distinguishes error conditions from "success but
    646      1.1  christos  * not overridden".  This is because we want to avoid accidentally applying
    647      1.1  christos  * the "no data" action in case of error.
    648      1.1  christos  * @param action: action to apply
    649  1.1.1.4  christos  * @param data: RRset to use for override
    650      1.1  christos  * @param qtype: original query type
    651      1.1  christos  * @param rep: original reply message
    652      1.1  christos  * @param rrset_id: the rrset ID in 'rep' to which the action should apply
    653      1.1  christos  * @param new_repp: see respip_rewrite_reply
    654      1.1  christos  * @param tag: if >= 0 the tag ID used to determine the action and data
    655      1.1  christos  * @param tag_datas: data corresponding to 'tag'.
    656      1.1  christos  * @param tag_datas_size: size of 'tag_datas'
    657      1.1  christos  * @param tagname: array of tag names, used for logging
    658      1.1  christos  * @param num_tags: size of 'tagname', used for logging
    659      1.1  christos  * @param redirect_rrsetp: ptr to redirect record
    660      1.1  christos  * @param region: region for building new reply
    661      1.1  christos  * @return 1 if overridden, 0 if not overridden, -1 on error.
    662      1.1  christos  */
    663      1.1  christos static int
    664  1.1.1.4  christos respip_data_answer(enum respip_action action,
    665  1.1.1.4  christos 	struct ub_packed_rrset_key* data,
    666      1.1  christos 	uint16_t qtype, const struct reply_info* rep,
    667      1.1  christos 	size_t rrset_id, struct reply_info** new_repp, int tag,
    668      1.1  christos 	struct config_strlist** tag_datas, size_t tag_datas_size,
    669      1.1  christos 	char* const* tagname, int num_tags,
    670      1.1  christos 	struct ub_packed_rrset_key** redirect_rrsetp, struct regional* region)
    671      1.1  christos {
    672  1.1.1.4  christos 	struct ub_packed_rrset_key* rp = data;
    673      1.1  christos 	struct reply_info* new_rep;
    674      1.1  christos 	*redirect_rrsetp = NULL;
    675      1.1  christos 
    676      1.1  christos 	if(action == respip_redirect && tag != -1 &&
    677      1.1  christos 		(size_t)tag<tag_datas_size && tag_datas[tag]) {
    678      1.1  christos 		struct query_info dataqinfo;
    679      1.1  christos 		struct ub_packed_rrset_key r;
    680      1.1  christos 
    681      1.1  christos 		/* Extract parameters of the original answer rrset that can be
    682      1.1  christos 		 * rewritten below, in the form of query_info.  Note that these
    683      1.1  christos 		 * can be different from the info of the original query if the
    684      1.1  christos 		 * rrset is a CNAME target.*/
    685      1.1  christos 		memset(&dataqinfo, 0, sizeof(dataqinfo));
    686      1.1  christos 		dataqinfo.qname = rep->rrsets[rrset_id]->rk.dname;
    687      1.1  christos 		dataqinfo.qname_len = rep->rrsets[rrset_id]->rk.dname_len;
    688      1.1  christos 		dataqinfo.qtype = ntohs(rep->rrsets[rrset_id]->rk.type);
    689      1.1  christos 		dataqinfo.qclass = ntohs(rep->rrsets[rrset_id]->rk.rrset_class);
    690      1.1  christos 
    691      1.1  christos 		memset(&r, 0, sizeof(r));
    692      1.1  christos 		if(local_data_find_tag_datas(&dataqinfo, tag_datas[tag], &r,
    693      1.1  christos 			region)) {
    694      1.1  christos 			verbose(VERB_ALGO,
    695      1.1  christos 				"response-ip redirect with tag data [%d] %s",
    696      1.1  christos 				tag, (tag<num_tags?tagname[tag]:"null"));
    697      1.1  christos 			/* use copy_rrset() to 'normalize' memory layout */
    698  1.1.1.5  christos 			rp = respip_copy_rrset(&r, region);
    699      1.1  christos 			if(!rp)
    700      1.1  christos 				return -1;
    701      1.1  christos 		}
    702      1.1  christos 	}
    703      1.1  christos 	if(!rp)
    704      1.1  christos 		return 0;
    705      1.1  christos 
    706      1.1  christos 	/* If we are using response-ip-data, we need to make a copy of rrset
    707      1.1  christos 	 * to replace the rrset's dname.  Note that, unlike local data, we
    708      1.1  christos 	 * rename the dname for other actions than redirect.  This is because
    709      1.1  christos 	 * response-ip-data isn't associated to any specific name. */
    710  1.1.1.4  christos 	if(rp == data) {
    711  1.1.1.5  christos 		rp = respip_copy_rrset(rp, region);
    712      1.1  christos 		if(!rp)
    713      1.1  christos 			return -1;
    714      1.1  christos 		rp->rk.dname = rep->rrsets[rrset_id]->rk.dname;
    715      1.1  christos 		rp->rk.dname_len = rep->rrsets[rrset_id]->rk.dname_len;
    716      1.1  christos 	}
    717      1.1  christos 
    718      1.1  christos 	/* Build a new reply with redirect rrset.  We keep any preceding CNAMEs
    719      1.1  christos 	 * and replace the address rrset that triggers the action.  If it's
    720      1.1  christos 	 * type ANY query, however, no other answer records should be kept
    721      1.1  christos 	 * (note that it can't be a CNAME chain in this case due to
    722      1.1  christos 	 * sanitizing). */
    723      1.1  christos 	if(qtype == LDNS_RR_TYPE_ANY)
    724      1.1  christos 		rrset_id = 0;
    725      1.1  christos 	new_rep = make_new_reply_info(rep, region, rrset_id + 1, rrset_id);
    726      1.1  christos 	if(!new_rep)
    727      1.1  christos 		return -1;
    728      1.1  christos 	rp->rk.flags |= PACKED_RRSET_FIXEDTTL; /* avoid adjusting TTL */
    729      1.1  christos 	new_rep->rrsets[rrset_id] = rp;
    730      1.1  christos 
    731      1.1  christos 	*redirect_rrsetp = rp;
    732      1.1  christos 	*new_repp = new_rep;
    733      1.1  christos 	return 1;
    734      1.1  christos }
    735      1.1  christos 
    736      1.1  christos /**
    737      1.1  christos  * apply response ip action in case where no action data is provided.
    738      1.1  christos  * this is similar to localzone.c:lz_zone_answer() but simplified due to
    739      1.1  christos  * the characteristics of response ip:
    740      1.1  christos  * - 'deny' variants will be handled at the caller side
    741      1.1  christos  * - no specific processing for 'transparent' variants: unlike local zones,
    742      1.1  christos  *   there is no such a case of 'no data but name existing'.  so all variants
    743      1.1  christos  *   just mean 'transparent if no data'.
    744      1.1  christos  * @param qtype: query type
    745      1.1  christos  * @param action: found action
    746      1.1  christos  * @param rep:
    747      1.1  christos  * @param new_repp
    748      1.1  christos  * @param rrset_id
    749      1.1  christos  * @param region: region for building new reply
    750      1.1  christos  * @return 1 on success, 0 on error.
    751      1.1  christos  */
    752      1.1  christos static int
    753      1.1  christos respip_nodata_answer(uint16_t qtype, enum respip_action action,
    754      1.1  christos 	const struct reply_info *rep, size_t rrset_id,
    755      1.1  christos 	struct reply_info** new_repp, struct regional* region)
    756      1.1  christos {
    757      1.1  christos 	struct reply_info* new_rep;
    758      1.1  christos 
    759      1.1  christos 	if(action == respip_refuse || action == respip_always_refuse) {
    760      1.1  christos 		new_rep = make_new_reply_info(rep, region, 0, 0);
    761      1.1  christos 		if(!new_rep)
    762      1.1  christos 			return 0;
    763      1.1  christos 		FLAGS_SET_RCODE(new_rep->flags, LDNS_RCODE_REFUSED);
    764      1.1  christos 		*new_repp = new_rep;
    765      1.1  christos 		return 1;
    766      1.1  christos 	} else if(action == respip_static || action == respip_redirect ||
    767  1.1.1.2  christos 		action == respip_always_nxdomain ||
    768  1.1.1.4  christos 		action == respip_always_nodata ||
    769  1.1.1.2  christos 		action == respip_inform_redirect) {
    770      1.1  christos 		/* Since we don't know about other types of the owner name,
    771      1.1  christos 		 * we generally return NOERROR/NODATA unless an NXDOMAIN action
    772      1.1  christos 		 * is explicitly specified. */
    773      1.1  christos 		int rcode = (action == respip_always_nxdomain)?
    774      1.1  christos 			LDNS_RCODE_NXDOMAIN:LDNS_RCODE_NOERROR;
    775      1.1  christos 		/* We should empty the answer section except for any preceding
    776      1.1  christos 		 * CNAMEs (in that case rrset_id > 0).  Type-ANY case is
    777      1.1  christos 		 * special as noted in respip_data_answer(). */
    778      1.1  christos 		if(qtype == LDNS_RR_TYPE_ANY)
    779      1.1  christos 			rrset_id = 0;
    780      1.1  christos 		new_rep = make_new_reply_info(rep, region, rrset_id, rrset_id);
    781      1.1  christos 		if(!new_rep)
    782      1.1  christos 			return 0;
    783      1.1  christos 		FLAGS_SET_RCODE(new_rep->flags, rcode);
    784      1.1  christos 		*new_repp = new_rep;
    785      1.1  christos 		return 1;
    786      1.1  christos 	}
    787      1.1  christos 
    788      1.1  christos 	return 1;
    789      1.1  christos }
    790      1.1  christos 
    791      1.1  christos /** Populate action info structure with the results of response-ip action
    792      1.1  christos  *  processing, iff as the result of response-ip processing we are actually
    793      1.1  christos  *  taking some action. Only action is set if action_only is true.
    794      1.1  christos  *  Returns true on success, false on failure.
    795      1.1  christos  */
    796      1.1  christos static int
    797      1.1  christos populate_action_info(struct respip_action_info* actinfo,
    798      1.1  christos 	enum respip_action action, const struct resp_addr* raddr,
    799      1.1  christos 	const struct ub_packed_rrset_key* ATTR_UNUSED(rrset),
    800      1.1  christos 	int ATTR_UNUSED(tag), const struct respip_set* ATTR_UNUSED(ipset),
    801  1.1.1.4  christos 	int ATTR_UNUSED(action_only), struct regional* region, int rpz_used,
    802  1.1.1.4  christos 	int rpz_log, char* log_name, int rpz_cname_override)
    803      1.1  christos {
    804      1.1  christos 	if(action == respip_none || !raddr)
    805      1.1  christos 		return 1;
    806      1.1  christos 	actinfo->action = action;
    807  1.1.1.4  christos 	actinfo->rpz_used = rpz_used;
    808  1.1.1.4  christos 	actinfo->rpz_log = rpz_log;
    809  1.1.1.4  christos 	actinfo->log_name = log_name;
    810  1.1.1.4  christos 	actinfo->rpz_cname_override = rpz_cname_override;
    811      1.1  christos 
    812      1.1  christos 	/* for inform variants, make a copy of the matched address block for
    813      1.1  christos 	 * later logging.  We make a copy to proactively avoid disruption if
    814      1.1  christos 	 *  and when we allow a dynamic update to the respip tree. */
    815  1.1.1.4  christos 	if(action == respip_inform || action == respip_inform_deny ||
    816  1.1.1.4  christos 		rpz_used) {
    817      1.1  christos 		struct respip_addr_info* a =
    818      1.1  christos 			regional_alloc_zero(region, sizeof(*a));
    819      1.1  christos 		if(!a) {
    820      1.1  christos 			log_err("out of memory");
    821      1.1  christos 			return 0;
    822      1.1  christos 		}
    823      1.1  christos 		a->addr = raddr->node.addr;
    824      1.1  christos 		a->addrlen = raddr->node.addrlen;
    825      1.1  christos 		a->net = raddr->node.net;
    826      1.1  christos 		actinfo->addrinfo = a;
    827      1.1  christos 	}
    828      1.1  christos 
    829      1.1  christos 	return 1;
    830      1.1  christos }
    831      1.1  christos 
    832  1.1.1.4  christos static int
    833  1.1.1.4  christos respip_use_rpz(struct resp_addr* raddr, struct rpz* r,
    834  1.1.1.4  christos 	enum respip_action* action,
    835  1.1.1.4  christos 	struct ub_packed_rrset_key** data, int* rpz_log, char** log_name,
    836  1.1.1.5  christos 	int* rpz_cname_override, struct regional* region, int* is_rpz,
    837  1.1.1.5  christos 	int* rpz_passthru)
    838  1.1.1.4  christos {
    839  1.1.1.5  christos 	if(rpz_passthru && *rpz_passthru)
    840  1.1.1.5  christos 		return 0;
    841  1.1.1.4  christos 	if(r->action_override == RPZ_DISABLED_ACTION) {
    842  1.1.1.4  christos 		*is_rpz = 0;
    843  1.1.1.4  christos 		return 1;
    844  1.1.1.4  christos 	}
    845  1.1.1.4  christos 	else if(r->action_override == RPZ_NO_OVERRIDE_ACTION)
    846  1.1.1.4  christos 		*action = raddr->action;
    847  1.1.1.4  christos 	else
    848  1.1.1.4  christos 		*action = rpz_action_to_respip_action(r->action_override);
    849  1.1.1.4  christos 	if(r->action_override == RPZ_CNAME_OVERRIDE_ACTION &&
    850  1.1.1.4  christos 		r->cname_override) {
    851  1.1.1.4  christos 		*data = r->cname_override;
    852  1.1.1.4  christos 		*rpz_cname_override = 1;
    853  1.1.1.4  christos 	}
    854  1.1.1.5  christos 	if(*action == respip_always_transparent /* RPZ_PASSTHRU_ACTION */
    855  1.1.1.5  christos 		&& rpz_passthru)
    856  1.1.1.5  christos 		*rpz_passthru = 1;
    857  1.1.1.4  christos 	*rpz_log = r->log;
    858  1.1.1.4  christos 	if(r->log_name)
    859  1.1.1.4  christos 		if(!(*log_name = regional_strdup(region, r->log_name)))
    860  1.1.1.4  christos 			return 0;
    861  1.1.1.4  christos 	*is_rpz = 1;
    862  1.1.1.4  christos 	return 1;
    863  1.1.1.4  christos }
    864  1.1.1.4  christos 
    865      1.1  christos int
    866      1.1  christos respip_rewrite_reply(const struct query_info* qinfo,
    867      1.1  christos 	const struct respip_client_info* cinfo, const struct reply_info* rep,
    868      1.1  christos 	struct reply_info** new_repp, struct respip_action_info* actinfo,
    869      1.1  christos 	struct ub_packed_rrset_key** alias_rrset, int search_only,
    870  1.1.1.5  christos 	struct regional* region, struct auth_zones* az, int* rpz_passthru)
    871      1.1  christos {
    872      1.1  christos 	const uint8_t* ctaglist;
    873      1.1  christos 	size_t ctaglen;
    874      1.1  christos 	const uint8_t* tag_actions;
    875      1.1  christos 	size_t tag_actions_size;
    876      1.1  christos 	struct config_strlist** tag_datas;
    877      1.1  christos 	size_t tag_datas_size;
    878      1.1  christos 	struct view* view = NULL;
    879      1.1  christos 	struct respip_set* ipset = NULL;
    880  1.1.1.5  christos 	size_t rrset_id = 0, rr_id = 0;
    881      1.1  christos 	enum respip_action action = respip_none;
    882      1.1  christos 	int tag = -1;
    883  1.1.1.4  christos 	struct resp_addr* raddr = NULL;
    884      1.1  christos 	int ret = 1;
    885      1.1  christos 	struct ub_packed_rrset_key* redirect_rrset = NULL;
    886  1.1.1.4  christos 	struct rpz* r;
    887  1.1.1.4  christos 	struct auth_zone* a = NULL;
    888  1.1.1.4  christos 	struct ub_packed_rrset_key* data = NULL;
    889  1.1.1.4  christos 	int rpz_used = 0;
    890  1.1.1.4  christos 	int rpz_log = 0;
    891  1.1.1.4  christos 	int rpz_cname_override = 0;
    892  1.1.1.4  christos 	char* log_name = NULL;
    893      1.1  christos 
    894      1.1  christos 	if(!cinfo)
    895      1.1  christos 		goto done;
    896      1.1  christos 	ctaglist = cinfo->taglist;
    897      1.1  christos 	ctaglen = cinfo->taglen;
    898      1.1  christos 	tag_actions = cinfo->tag_actions;
    899      1.1  christos 	tag_actions_size = cinfo->tag_actions_size;
    900      1.1  christos 	tag_datas = cinfo->tag_datas;
    901      1.1  christos 	tag_datas_size = cinfo->tag_datas_size;
    902      1.1  christos 	view = cinfo->view;
    903      1.1  christos 	ipset = cinfo->respip_set;
    904      1.1  christos 
    905  1.1.1.4  christos 	log_assert(ipset);
    906  1.1.1.4  christos 
    907      1.1  christos 	/** Try to use response-ip config from the view first; use
    908      1.1  christos 	  * global response-ip config if we don't have the view or we don't
    909      1.1  christos 	  * have the matching per-view config (and the view allows the use
    910      1.1  christos 	  * of global data in this case).
    911      1.1  christos 	  * Note that we lock the view even if we only use view members that
    912      1.1  christos 	  * currently don't change after creation.  This is for safety for
    913      1.1  christos 	  * future possible changes as the view documentation seems to expect
    914      1.1  christos 	  * any of its member can change in the view's lifetime.
    915      1.1  christos 	  * Note also that we assume 'view' is valid in this function, which
    916      1.1  christos 	  * should be safe (see unbound bug #1191) */
    917      1.1  christos 	if(view) {
    918      1.1  christos 		lock_rw_rdlock(&view->lock);
    919      1.1  christos 		if(view->respip_set) {
    920      1.1  christos 			if((raddr = respip_addr_lookup(rep,
    921  1.1.1.5  christos 				view->respip_set, &rrset_id, &rr_id))) {
    922      1.1  christos 				/** for per-view respip directives the action
    923      1.1  christos 				 * can only be direct (i.e. not tag-based) */
    924      1.1  christos 				action = raddr->action;
    925      1.1  christos 			}
    926      1.1  christos 		}
    927      1.1  christos 		if(!raddr && !view->isfirst)
    928      1.1  christos 			goto done;
    929  1.1.1.4  christos 		if(!raddr && view->isfirst) {
    930  1.1.1.4  christos 			lock_rw_unlock(&view->lock);
    931  1.1.1.4  christos 			view = NULL;
    932  1.1.1.4  christos 		}
    933      1.1  christos 	}
    934  1.1.1.4  christos 	if(!raddr && (raddr = respip_addr_lookup(rep, ipset,
    935  1.1.1.5  christos 		&rrset_id, &rr_id))) {
    936      1.1  christos 		action = (enum respip_action)local_data_find_tag_action(
    937      1.1  christos 			raddr->taglist, raddr->taglen, ctaglist, ctaglen,
    938      1.1  christos 			tag_actions, tag_actions_size,
    939      1.1  christos 			(enum localzone_type)raddr->action, &tag,
    940      1.1  christos 			ipset->tagname, ipset->num_tags);
    941      1.1  christos 	}
    942  1.1.1.4  christos 	lock_rw_rdlock(&az->rpz_lock);
    943  1.1.1.5  christos 	for(a = az->rpz_first; a && !raddr && !(rpz_passthru && *rpz_passthru); a = a->rpz_az_next) {
    944  1.1.1.4  christos 		lock_rw_rdlock(&a->lock);
    945  1.1.1.4  christos 		r = a->rpz;
    946  1.1.1.4  christos 		if(!r->taglist || taglist_intersect(r->taglist,
    947  1.1.1.4  christos 			r->taglistlen, ctaglist, ctaglen)) {
    948  1.1.1.4  christos 			if((raddr = respip_addr_lookup(rep,
    949  1.1.1.5  christos 				r->respip_set, &rrset_id, &rr_id))) {
    950  1.1.1.4  christos 				if(!respip_use_rpz(raddr, r, &action, &data,
    951  1.1.1.4  christos 					&rpz_log, &log_name, &rpz_cname_override,
    952  1.1.1.5  christos 					region, &rpz_used, rpz_passthru)) {
    953  1.1.1.4  christos 					log_err("out of memory");
    954  1.1.1.4  christos 					lock_rw_unlock(&raddr->lock);
    955  1.1.1.4  christos 					lock_rw_unlock(&a->lock);
    956  1.1.1.4  christos 					lock_rw_unlock(&az->rpz_lock);
    957  1.1.1.4  christos 					return 0;
    958  1.1.1.4  christos 				}
    959  1.1.1.4  christos 				if(rpz_used) {
    960  1.1.1.5  christos 					if(verbosity >= VERB_ALGO) {
    961  1.1.1.5  christos 						struct sockaddr_storage ss;
    962  1.1.1.5  christos 						socklen_t ss_len = 0;
    963  1.1.1.5  christos 						char nm[256], ip[256];
    964  1.1.1.5  christos 						char qn[255+1];
    965  1.1.1.5  christos 						if(!rdata2sockaddr(rep->rrsets[rrset_id]->entry.data, ntohs(rep->rrsets[rrset_id]->rk.type), rr_id, &ss, &ss_len))
    966  1.1.1.5  christos 							snprintf(ip, sizeof(ip), "invalidRRdata");
    967  1.1.1.5  christos 						else
    968  1.1.1.5  christos 							addr_to_str(&ss, ss_len, ip, sizeof(ip));
    969  1.1.1.5  christos 						dname_str(qinfo->qname, qn);
    970  1.1.1.5  christos 						addr_to_str(&raddr->node.addr,
    971  1.1.1.5  christos 							raddr->node.addrlen,
    972  1.1.1.5  christos 							nm, sizeof(nm));
    973  1.1.1.5  christos 						verbose(VERB_ALGO, "respip: rpz: response-ip trigger %s/%d on %s %s with action %s", nm, raddr->node.net, qn, ip, rpz_action_to_string(respip_action_to_rpz_action(action)));
    974  1.1.1.5  christos 					}
    975  1.1.1.4  christos 					/* break to make sure 'a' stays pointed
    976  1.1.1.4  christos 					 * to used auth_zone, and keeps lock */
    977  1.1.1.4  christos 					break;
    978  1.1.1.4  christos 				}
    979  1.1.1.4  christos 				lock_rw_unlock(&raddr->lock);
    980  1.1.1.4  christos 				raddr = NULL;
    981  1.1.1.4  christos 				actinfo->rpz_disabled++;
    982  1.1.1.4  christos 			}
    983  1.1.1.4  christos 		}
    984  1.1.1.4  christos 		lock_rw_unlock(&a->lock);
    985  1.1.1.4  christos 	}
    986  1.1.1.4  christos 	lock_rw_unlock(&az->rpz_lock);
    987      1.1  christos 	if(raddr && !search_only) {
    988      1.1  christos 		int result = 0;
    989      1.1  christos 
    990      1.1  christos 		/* first, see if we have response-ip or tag action for the
    991      1.1  christos 		 * action except for 'always' variants. */
    992      1.1  christos 		if(action != respip_always_refuse
    993      1.1  christos 			&& action != respip_always_transparent
    994      1.1  christos 			&& action != respip_always_nxdomain
    995  1.1.1.4  christos 			&& action != respip_always_nodata
    996  1.1.1.4  christos 			&& action != respip_always_deny
    997  1.1.1.4  christos 			&& (result = respip_data_answer(action,
    998  1.1.1.4  christos 			(data) ? data : raddr->data, qinfo->qtype, rep,
    999  1.1.1.4  christos 			rrset_id, new_repp, tag, tag_datas, tag_datas_size,
   1000  1.1.1.4  christos 			ipset->tagname, ipset->num_tags, &redirect_rrset,
   1001  1.1.1.4  christos 			region)) < 0) {
   1002      1.1  christos 			ret = 0;
   1003      1.1  christos 			goto done;
   1004      1.1  christos 		}
   1005      1.1  christos 
   1006      1.1  christos 		/* if no action data applied, take action specific to the
   1007      1.1  christos 		 * action without data. */
   1008      1.1  christos 		if(!result && !respip_nodata_answer(qinfo->qtype, action, rep,
   1009      1.1  christos 			rrset_id, new_repp, region)) {
   1010      1.1  christos 			ret = 0;
   1011      1.1  christos 			goto done;
   1012      1.1  christos 		}
   1013      1.1  christos 	}
   1014      1.1  christos   done:
   1015      1.1  christos 	if(view) {
   1016      1.1  christos 		lock_rw_unlock(&view->lock);
   1017      1.1  christos 	}
   1018      1.1  christos 	if(ret) {
   1019      1.1  christos 		/* If we're redirecting the original answer to a
   1020      1.1  christos 		 * CNAME, record the CNAME rrset so the caller can take
   1021      1.1  christos 		 * the appropriate action.  Note that we don't check the
   1022      1.1  christos 		 * action type; it should normally be 'redirect', but it
   1023      1.1  christos 		 * can be of other type when a data-dependent tag action
   1024      1.1  christos 		 * uses redirect response-ip data.
   1025      1.1  christos 		 */
   1026      1.1  christos 		if(redirect_rrset &&
   1027      1.1  christos 			redirect_rrset->rk.type == ntohs(LDNS_RR_TYPE_CNAME) &&
   1028      1.1  christos 			qinfo->qtype != LDNS_RR_TYPE_ANY)
   1029      1.1  christos 			*alias_rrset = redirect_rrset;
   1030      1.1  christos 		/* on success, populate respip result structure */
   1031      1.1  christos 		ret = populate_action_info(actinfo, action, raddr,
   1032  1.1.1.4  christos 			redirect_rrset, tag, ipset, search_only, region,
   1033  1.1.1.4  christos 				rpz_used, rpz_log, log_name, rpz_cname_override);
   1034  1.1.1.4  christos 	}
   1035  1.1.1.4  christos 	if(raddr) {
   1036  1.1.1.4  christos 		lock_rw_unlock(&raddr->lock);
   1037  1.1.1.4  christos 	}
   1038  1.1.1.4  christos 	if(rpz_used) {
   1039  1.1.1.4  christos 		lock_rw_unlock(&a->lock);
   1040      1.1  christos 	}
   1041      1.1  christos 	return ret;
   1042      1.1  christos }
   1043      1.1  christos 
   1044      1.1  christos static int
   1045      1.1  christos generate_cname_request(struct module_qstate* qstate,
   1046      1.1  christos 	struct ub_packed_rrset_key* alias_rrset)
   1047      1.1  christos {
   1048      1.1  christos 	struct module_qstate* subq = NULL;
   1049      1.1  christos 	struct query_info subqi;
   1050      1.1  christos 
   1051      1.1  christos 	memset(&subqi, 0, sizeof(subqi));
   1052      1.1  christos 	get_cname_target(alias_rrset, &subqi.qname, &subqi.qname_len);
   1053      1.1  christos 	if(!subqi.qname)
   1054      1.1  christos 		return 0;    /* unexpected: not a valid CNAME RDATA */
   1055      1.1  christos 	subqi.qtype = qstate->qinfo.qtype;
   1056      1.1  christos 	subqi.qclass = qstate->qinfo.qclass;
   1057      1.1  christos 	fptr_ok(fptr_whitelist_modenv_attach_sub(qstate->env->attach_sub));
   1058      1.1  christos 	return (*qstate->env->attach_sub)(qstate, &subqi, BIT_RD, 0, 0, &subq);
   1059      1.1  christos }
   1060      1.1  christos 
   1061      1.1  christos void
   1062      1.1  christos respip_operate(struct module_qstate* qstate, enum module_ev event, int id,
   1063      1.1  christos 	struct outbound_entry* outbound)
   1064      1.1  christos {
   1065      1.1  christos 	struct respip_qstate* rq = (struct respip_qstate*)qstate->minfo[id];
   1066      1.1  christos 
   1067      1.1  christos 	log_query_info(VERB_QUERY, "respip operate: query", &qstate->qinfo);
   1068      1.1  christos 	(void)outbound;
   1069      1.1  christos 
   1070      1.1  christos 	if(event == module_event_new || event == module_event_pass) {
   1071      1.1  christos 		if(!rq) {
   1072      1.1  christos 			rq = regional_alloc_zero(qstate->region, sizeof(*rq));
   1073      1.1  christos 			if(!rq)
   1074      1.1  christos 				goto servfail;
   1075      1.1  christos 			rq->state = RESPIP_INIT;
   1076      1.1  christos 			qstate->minfo[id] = rq;
   1077      1.1  christos 		}
   1078      1.1  christos 		if(rq->state == RESPIP_SUBQUERY_FINISHED) {
   1079      1.1  christos 			qstate->ext_state[id] = module_finished;
   1080      1.1  christos 			return;
   1081      1.1  christos 		}
   1082      1.1  christos 		verbose(VERB_ALGO, "respip: pass to next module");
   1083      1.1  christos 		qstate->ext_state[id] = module_wait_module;
   1084      1.1  christos 	} else if(event == module_event_moddone) {
   1085      1.1  christos 		/* If the reply may be subject to response-ip rewriting
   1086      1.1  christos 		 * according to the query type, check the actions.  If a
   1087      1.1  christos 		 * rewrite is necessary, we'll replace the reply in qstate
   1088      1.1  christos 		 * with the new one. */
   1089      1.1  christos 		enum module_ext_state next_state = module_finished;
   1090      1.1  christos 
   1091      1.1  christos 		if((qstate->qinfo.qtype == LDNS_RR_TYPE_A ||
   1092      1.1  christos 			qstate->qinfo.qtype == LDNS_RR_TYPE_AAAA ||
   1093      1.1  christos 			qstate->qinfo.qtype == LDNS_RR_TYPE_ANY) &&
   1094      1.1  christos 			qstate->return_msg && qstate->return_msg->rep) {
   1095      1.1  christos 			struct reply_info* new_rep = qstate->return_msg->rep;
   1096      1.1  christos 			struct ub_packed_rrset_key* alias_rrset = NULL;
   1097  1.1.1.4  christos 			struct respip_action_info actinfo = {0, 0, 0, 0, NULL, 0, NULL};
   1098  1.1.1.4  christos 			actinfo.action = respip_none;
   1099      1.1  christos 
   1100      1.1  christos 			if(!respip_rewrite_reply(&qstate->qinfo,
   1101      1.1  christos 				qstate->client_info, qstate->return_msg->rep,
   1102      1.1  christos 				&new_rep, &actinfo, &alias_rrset, 0,
   1103  1.1.1.5  christos 				qstate->region, qstate->env->auth_zones,
   1104  1.1.1.5  christos 				&qstate->rpz_passthru)) {
   1105      1.1  christos 				goto servfail;
   1106      1.1  christos 			}
   1107      1.1  christos 			if(actinfo.action != respip_none) {
   1108      1.1  christos 				/* save action info for logging on a
   1109      1.1  christos 				 * per-front-end-query basis */
   1110      1.1  christos 				if(!(qstate->respip_action_info =
   1111      1.1  christos 					regional_alloc_init(qstate->region,
   1112      1.1  christos 						&actinfo, sizeof(actinfo))))
   1113      1.1  christos 				{
   1114      1.1  christos 					log_err("out of memory");
   1115      1.1  christos 					goto servfail;
   1116      1.1  christos 				}
   1117      1.1  christos 			} else {
   1118      1.1  christos 				qstate->respip_action_info = NULL;
   1119      1.1  christos 			}
   1120  1.1.1.4  christos 			if (actinfo.action == respip_always_deny ||
   1121  1.1.1.4  christos 				(new_rep == qstate->return_msg->rep &&
   1122      1.1  christos 				(actinfo.action == respip_deny ||
   1123  1.1.1.4  christos 				actinfo.action == respip_inform_deny))) {
   1124      1.1  christos 				/* for deny-variant actions (unless response-ip
   1125      1.1  christos 				 * data is applied), mark the query state so
   1126      1.1  christos 				 * the response will be dropped for all
   1127      1.1  christos 				 * clients. */
   1128      1.1  christos 				qstate->is_drop = 1;
   1129      1.1  christos 			} else if(alias_rrset) {
   1130      1.1  christos 				if(!generate_cname_request(qstate, alias_rrset))
   1131      1.1  christos 					goto servfail;
   1132      1.1  christos 				next_state = module_wait_subquery;
   1133      1.1  christos 			}
   1134      1.1  christos 			qstate->return_msg->rep = new_rep;
   1135      1.1  christos 		}
   1136      1.1  christos 		qstate->ext_state[id] = next_state;
   1137      1.1  christos 	} else
   1138      1.1  christos 		qstate->ext_state[id] = module_finished;
   1139      1.1  christos 
   1140      1.1  christos 	return;
   1141      1.1  christos 
   1142      1.1  christos   servfail:
   1143      1.1  christos 	qstate->return_rcode = LDNS_RCODE_SERVFAIL;
   1144      1.1  christos 	qstate->return_msg = NULL;
   1145      1.1  christos }
   1146      1.1  christos 
   1147      1.1  christos int
   1148      1.1  christos respip_merge_cname(struct reply_info* base_rep,
   1149      1.1  christos 	const struct query_info* qinfo, const struct reply_info* tgt_rep,
   1150      1.1  christos 	const struct respip_client_info* cinfo, int must_validate,
   1151  1.1.1.4  christos 	struct reply_info** new_repp, struct regional* region,
   1152  1.1.1.4  christos 	struct auth_zones* az)
   1153      1.1  christos {
   1154      1.1  christos 	struct reply_info* new_rep;
   1155      1.1  christos 	struct reply_info* tmp_rep = NULL; /* just a placeholder */
   1156      1.1  christos 	struct ub_packed_rrset_key* alias_rrset = NULL; /* ditto */
   1157      1.1  christos 	uint16_t tgt_rcode;
   1158      1.1  christos 	size_t i, j;
   1159  1.1.1.4  christos 	struct respip_action_info actinfo = {0, 0, 0, 0, NULL, 0, NULL};
   1160  1.1.1.4  christos 	actinfo.action = respip_none;
   1161      1.1  christos 
   1162      1.1  christos 	/* If the query for the CNAME target would result in an unusual rcode,
   1163      1.1  christos 	 * we generally translate it as a failure for the base query
   1164      1.1  christos 	 * (which would then be translated into SERVFAIL).  The only exception
   1165      1.1  christos 	 * is NXDOMAIN and YXDOMAIN, which are passed to the end client(s).
   1166      1.1  christos 	 * The YXDOMAIN case would be rare but still possible (when
   1167      1.1  christos 	 * DNSSEC-validated DNAME has been cached but synthesizing CNAME
   1168      1.1  christos 	 * can't be generated due to length limitation) */
   1169      1.1  christos 	tgt_rcode = FLAGS_GET_RCODE(tgt_rep->flags);
   1170      1.1  christos 	if((tgt_rcode != LDNS_RCODE_NOERROR &&
   1171      1.1  christos 		tgt_rcode != LDNS_RCODE_NXDOMAIN &&
   1172      1.1  christos 		tgt_rcode != LDNS_RCODE_YXDOMAIN) ||
   1173      1.1  christos 		(must_validate && tgt_rep->security <= sec_status_bogus)) {
   1174      1.1  christos 		return 0;
   1175      1.1  christos 	}
   1176      1.1  christos 
   1177      1.1  christos 	/* see if the target reply would be subject to a response-ip action. */
   1178      1.1  christos 	if(!respip_rewrite_reply(qinfo, cinfo, tgt_rep, &tmp_rep, &actinfo,
   1179  1.1.1.5  christos 		&alias_rrset, 1, region, az, NULL))
   1180      1.1  christos 		return 0;
   1181      1.1  christos 	if(actinfo.action != respip_none) {
   1182      1.1  christos 		log_info("CNAME target of redirect response-ip action would "
   1183      1.1  christos 			"be subject to response-ip action, too; stripped");
   1184      1.1  christos 		*new_repp = base_rep;
   1185      1.1  christos 		return 1;
   1186      1.1  christos 	}
   1187      1.1  christos 
   1188      1.1  christos 	/* Append target reply to the base.  Since we cannot assume
   1189      1.1  christos 	 * tgt_rep->rrsets is valid throughout the lifetime of new_rep
   1190      1.1  christos 	 * or it can be safely shared by multiple threads, we need to make a
   1191      1.1  christos 	 * deep copy. */
   1192      1.1  christos 	new_rep = make_new_reply_info(base_rep, region,
   1193      1.1  christos 		base_rep->an_numrrsets + tgt_rep->an_numrrsets,
   1194      1.1  christos 		base_rep->an_numrrsets);
   1195      1.1  christos 	if(!new_rep)
   1196      1.1  christos 		return 0;
   1197      1.1  christos 	for(i=0,j=base_rep->an_numrrsets; i<tgt_rep->an_numrrsets; i++,j++) {
   1198  1.1.1.5  christos 		new_rep->rrsets[j] = respip_copy_rrset(tgt_rep->rrsets[i], region);
   1199      1.1  christos 		if(!new_rep->rrsets[j])
   1200      1.1  christos 			return 0;
   1201      1.1  christos 	}
   1202      1.1  christos 
   1203      1.1  christos 	FLAGS_SET_RCODE(new_rep->flags, tgt_rcode);
   1204      1.1  christos 	*new_repp = new_rep;
   1205      1.1  christos 	return 1;
   1206      1.1  christos }
   1207      1.1  christos 
   1208      1.1  christos void
   1209      1.1  christos respip_inform_super(struct module_qstate* qstate, int id,
   1210      1.1  christos 	struct module_qstate* super)
   1211      1.1  christos {
   1212      1.1  christos 	struct respip_qstate* rq = (struct respip_qstate*)super->minfo[id];
   1213      1.1  christos 	struct reply_info* new_rep = NULL;
   1214      1.1  christos 
   1215      1.1  christos 	rq->state = RESPIP_SUBQUERY_FINISHED;
   1216      1.1  christos 
   1217      1.1  christos 	/* respip subquery should have always been created with a valid reply
   1218      1.1  christos 	 * in super. */
   1219      1.1  christos 	log_assert(super->return_msg && super->return_msg->rep);
   1220      1.1  christos 
   1221      1.1  christos 	/* return_msg can be NULL when, e.g., the sub query resulted in
   1222      1.1  christos 	 * SERVFAIL, in which case we regard it as a failure of the original
   1223      1.1  christos 	 * query.  Other checks are probably redundant, but we check them
   1224      1.1  christos 	 * for safety. */
   1225      1.1  christos 	if(!qstate->return_msg || !qstate->return_msg->rep ||
   1226      1.1  christos 		qstate->return_rcode != LDNS_RCODE_NOERROR)
   1227      1.1  christos 		goto fail;
   1228      1.1  christos 
   1229      1.1  christos 	if(!respip_merge_cname(super->return_msg->rep, &qstate->qinfo,
   1230      1.1  christos 		qstate->return_msg->rep, super->client_info,
   1231  1.1.1.4  christos 		super->env->need_to_validate, &new_rep, super->region,
   1232  1.1.1.4  christos 		qstate->env->auth_zones))
   1233      1.1  christos 		goto fail;
   1234      1.1  christos 	super->return_msg->rep = new_rep;
   1235      1.1  christos 	return;
   1236      1.1  christos 
   1237      1.1  christos   fail:
   1238      1.1  christos 	super->return_rcode = LDNS_RCODE_SERVFAIL;
   1239      1.1  christos 	super->return_msg = NULL;
   1240      1.1  christos 	return;
   1241      1.1  christos }
   1242      1.1  christos 
   1243      1.1  christos void
   1244      1.1  christos respip_clear(struct module_qstate* qstate, int id)
   1245      1.1  christos {
   1246      1.1  christos 	qstate->minfo[id] = NULL;
   1247      1.1  christos }
   1248      1.1  christos 
   1249      1.1  christos size_t
   1250      1.1  christos respip_get_mem(struct module_env* env, int id)
   1251      1.1  christos {
   1252      1.1  christos 	(void)env;
   1253      1.1  christos 	(void)id;
   1254      1.1  christos 	return 0;
   1255      1.1  christos }
   1256      1.1  christos 
   1257      1.1  christos /**
   1258      1.1  christos  * The response-ip function block
   1259      1.1  christos  */
   1260      1.1  christos static struct module_func_block respip_block = {
   1261      1.1  christos 	"respip",
   1262      1.1  christos 	&respip_init, &respip_deinit, &respip_operate, &respip_inform_super,
   1263      1.1  christos 	&respip_clear, &respip_get_mem
   1264      1.1  christos };
   1265      1.1  christos 
   1266      1.1  christos struct module_func_block*
   1267      1.1  christos respip_get_funcblock(void)
   1268      1.1  christos {
   1269      1.1  christos 	return &respip_block;
   1270      1.1  christos }
   1271      1.1  christos 
   1272      1.1  christos enum respip_action
   1273      1.1  christos resp_addr_get_action(const struct resp_addr* addr)
   1274      1.1  christos {
   1275      1.1  christos 	return addr ? addr->action : respip_none;
   1276      1.1  christos }
   1277      1.1  christos 
   1278      1.1  christos struct ub_packed_rrset_key*
   1279      1.1  christos resp_addr_get_rrset(struct resp_addr* addr)
   1280      1.1  christos {
   1281      1.1  christos 	return addr ? addr->data : NULL;
   1282      1.1  christos }
   1283      1.1  christos 
   1284      1.1  christos int
   1285      1.1  christos respip_set_is_empty(const struct respip_set* set)
   1286      1.1  christos {
   1287      1.1  christos 	return set ? set->ip_tree.count == 0 : 1;
   1288      1.1  christos }
   1289      1.1  christos 
   1290      1.1  christos void
   1291  1.1.1.4  christos respip_inform_print(struct respip_action_info* respip_actinfo, uint8_t* qname,
   1292      1.1  christos 	uint16_t qtype, uint16_t qclass, struct local_rrset* local_alias,
   1293      1.1  christos 	struct comm_reply* repinfo)
   1294      1.1  christos {
   1295      1.1  christos 	char srcip[128], respip[128], txt[512];
   1296      1.1  christos 	unsigned port;
   1297  1.1.1.4  christos 	struct respip_addr_info* respip_addr = respip_actinfo->addrinfo;
   1298  1.1.1.4  christos 	size_t txtlen = 0;
   1299  1.1.1.4  christos 	const char* actionstr = NULL;
   1300      1.1  christos 
   1301      1.1  christos 	if(local_alias)
   1302      1.1  christos 		qname = local_alias->rrset->rk.dname;
   1303      1.1  christos 	port = (unsigned)((repinfo->addr.ss_family == AF_INET) ?
   1304      1.1  christos 		ntohs(((struct sockaddr_in*)&repinfo->addr)->sin_port) :
   1305      1.1  christos 		ntohs(((struct sockaddr_in6*)&repinfo->addr)->sin6_port));
   1306      1.1  christos 	addr_to_str(&repinfo->addr, repinfo->addrlen, srcip, sizeof(srcip));
   1307      1.1  christos 	addr_to_str(&respip_addr->addr, respip_addr->addrlen,
   1308      1.1  christos 		respip, sizeof(respip));
   1309  1.1.1.4  christos 	if(respip_actinfo->rpz_log) {
   1310  1.1.1.4  christos 		txtlen += snprintf(txt+txtlen, sizeof(txt)-txtlen, "%s",
   1311  1.1.1.5  christos 			"rpz: applied ");
   1312  1.1.1.4  christos 		if(respip_actinfo->rpz_cname_override)
   1313  1.1.1.4  christos 			actionstr = rpz_action_to_string(
   1314  1.1.1.4  christos 				RPZ_CNAME_OVERRIDE_ACTION);
   1315  1.1.1.4  christos 		else
   1316  1.1.1.4  christos 			actionstr = rpz_action_to_string(
   1317  1.1.1.4  christos 				respip_action_to_rpz_action(
   1318  1.1.1.4  christos 					respip_actinfo->action));
   1319  1.1.1.4  christos 	}
   1320  1.1.1.4  christos 	if(respip_actinfo->log_name) {
   1321  1.1.1.4  christos 		txtlen += snprintf(txt+txtlen, sizeof(txt)-txtlen,
   1322  1.1.1.4  christos 			"[%s] ", respip_actinfo->log_name);
   1323  1.1.1.4  christos 	}
   1324  1.1.1.4  christos 	snprintf(txt+txtlen, sizeof(txt)-txtlen,
   1325  1.1.1.4  christos 		"%s/%d %s %s@%u", respip, respip_addr->net,
   1326  1.1.1.4  christos 		(actionstr) ? actionstr : "inform", srcip, port);
   1327  1.1.1.3  christos 	log_nametypeclass(NO_VERBOSE, txt, qname, qtype, qclass);
   1328      1.1  christos }
   1329