165b390aaSDag-Erling Smørgrav /*
265b390aaSDag-Erling Smørgrav * respip/respip.c - filtering response IP module
365b390aaSDag-Erling Smørgrav */
465b390aaSDag-Erling Smørgrav
565b390aaSDag-Erling Smørgrav /**
665b390aaSDag-Erling Smørgrav * \file
765b390aaSDag-Erling Smørgrav *
865b390aaSDag-Erling Smørgrav * This file contains a module that inspects a result of recursive resolution
965b390aaSDag-Erling Smørgrav * to see if any IP address record should trigger a special action.
1065b390aaSDag-Erling Smørgrav * If applicable these actions can modify the original response.
1165b390aaSDag-Erling Smørgrav */
1265b390aaSDag-Erling Smørgrav #include "config.h"
1365b390aaSDag-Erling Smørgrav
1465b390aaSDag-Erling Smørgrav #include "services/localzone.h"
15091e9e46SCy Schubert #include "services/authzone.h"
1665b390aaSDag-Erling Smørgrav #include "services/cache/dns.h"
1765b390aaSDag-Erling Smørgrav #include "sldns/str2wire.h"
1865b390aaSDag-Erling Smørgrav #include "util/config_file.h"
1965b390aaSDag-Erling Smørgrav #include "util/fptr_wlist.h"
2065b390aaSDag-Erling Smørgrav #include "util/module.h"
2165b390aaSDag-Erling Smørgrav #include "util/net_help.h"
2265b390aaSDag-Erling Smørgrav #include "util/regional.h"
2365b390aaSDag-Erling Smørgrav #include "util/data/msgreply.h"
2465b390aaSDag-Erling Smørgrav #include "util/storage/dnstree.h"
2565b390aaSDag-Erling Smørgrav #include "respip/respip.h"
2665b390aaSDag-Erling Smørgrav #include "services/view.h"
2765b390aaSDag-Erling Smørgrav #include "sldns/rrdef.h"
2824e36522SCy Schubert #include "util/data/dname.h"
2965b390aaSDag-Erling Smørgrav
3065b390aaSDag-Erling Smørgrav
3165b390aaSDag-Erling Smørgrav /** Subset of resp_addr.node, used for inform-variant logging */
3265b390aaSDag-Erling Smørgrav struct respip_addr_info {
3365b390aaSDag-Erling Smørgrav struct sockaddr_storage addr;
3465b390aaSDag-Erling Smørgrav socklen_t addrlen;
3565b390aaSDag-Erling Smørgrav int net;
3665b390aaSDag-Erling Smørgrav };
3765b390aaSDag-Erling Smørgrav
3865b390aaSDag-Erling Smørgrav /** Query state regarding the response-ip module. */
3965b390aaSDag-Erling Smørgrav enum respip_state {
4065b390aaSDag-Erling Smørgrav /**
4165b390aaSDag-Erling Smørgrav * The general state. Unless CNAME chasing takes place, all processing
4265b390aaSDag-Erling Smørgrav * is completed in this state without any other asynchronous event.
4365b390aaSDag-Erling Smørgrav */
4465b390aaSDag-Erling Smørgrav RESPIP_INIT = 0,
4565b390aaSDag-Erling Smørgrav
4665b390aaSDag-Erling Smørgrav /**
4765b390aaSDag-Erling Smørgrav * A subquery for CNAME chasing is completed.
4865b390aaSDag-Erling Smørgrav */
4965b390aaSDag-Erling Smørgrav RESPIP_SUBQUERY_FINISHED
5065b390aaSDag-Erling Smørgrav };
5165b390aaSDag-Erling Smørgrav
5265b390aaSDag-Erling Smørgrav /** Per query state for the response-ip module. */
5365b390aaSDag-Erling Smørgrav struct respip_qstate {
5465b390aaSDag-Erling Smørgrav enum respip_state state;
5565b390aaSDag-Erling Smørgrav };
5665b390aaSDag-Erling Smørgrav
5765b390aaSDag-Erling Smørgrav struct respip_set*
respip_set_create(void)5865b390aaSDag-Erling Smørgrav respip_set_create(void)
5965b390aaSDag-Erling Smørgrav {
6065b390aaSDag-Erling Smørgrav struct respip_set* set = calloc(1, sizeof(*set));
6165b390aaSDag-Erling Smørgrav if(!set)
6265b390aaSDag-Erling Smørgrav return NULL;
6365b390aaSDag-Erling Smørgrav set->region = regional_create();
6465b390aaSDag-Erling Smørgrav if(!set->region) {
6565b390aaSDag-Erling Smørgrav free(set);
6665b390aaSDag-Erling Smørgrav return NULL;
6765b390aaSDag-Erling Smørgrav }
6865b390aaSDag-Erling Smørgrav addr_tree_init(&set->ip_tree);
69091e9e46SCy Schubert lock_rw_init(&set->lock);
7065b390aaSDag-Erling Smørgrav return set;
7165b390aaSDag-Erling Smørgrav }
7265b390aaSDag-Erling Smørgrav
73091e9e46SCy Schubert /** helper traverse to delete resp_addr nodes */
74091e9e46SCy Schubert static void
resp_addr_del(rbnode_type * n,void * ATTR_UNUSED (arg))75091e9e46SCy Schubert resp_addr_del(rbnode_type* n, void* ATTR_UNUSED(arg))
76091e9e46SCy Schubert {
77091e9e46SCy Schubert struct resp_addr* r = (struct resp_addr*)n->key;
78091e9e46SCy Schubert lock_rw_destroy(&r->lock);
79091e9e46SCy Schubert #ifdef THREADS_DISABLED
80091e9e46SCy Schubert (void)r;
81091e9e46SCy Schubert #endif
82091e9e46SCy Schubert }
83091e9e46SCy Schubert
8465b390aaSDag-Erling Smørgrav void
respip_set_delete(struct respip_set * set)8565b390aaSDag-Erling Smørgrav respip_set_delete(struct respip_set* set)
8665b390aaSDag-Erling Smørgrav {
8765b390aaSDag-Erling Smørgrav if(!set)
8865b390aaSDag-Erling Smørgrav return;
89091e9e46SCy Schubert lock_rw_destroy(&set->lock);
90091e9e46SCy Schubert traverse_postorder(&set->ip_tree, resp_addr_del, NULL);
9165b390aaSDag-Erling Smørgrav regional_destroy(set->region);
9265b390aaSDag-Erling Smørgrav free(set);
9365b390aaSDag-Erling Smørgrav }
9465b390aaSDag-Erling Smørgrav
9565b390aaSDag-Erling Smørgrav struct rbtree_type*
respip_set_get_tree(struct respip_set * set)9665b390aaSDag-Erling Smørgrav respip_set_get_tree(struct respip_set* set)
9765b390aaSDag-Erling Smørgrav {
9865b390aaSDag-Erling Smørgrav if(!set)
9965b390aaSDag-Erling Smørgrav return NULL;
10065b390aaSDag-Erling Smørgrav return &set->ip_tree;
10165b390aaSDag-Erling Smørgrav }
10265b390aaSDag-Erling Smørgrav
103091e9e46SCy Schubert struct resp_addr*
respip_sockaddr_find_or_create(struct respip_set * set,struct sockaddr_storage * addr,socklen_t addrlen,int net,int create,const char * ipstr)104091e9e46SCy Schubert respip_sockaddr_find_or_create(struct respip_set* set, struct sockaddr_storage* addr,
105091e9e46SCy Schubert socklen_t addrlen, int net, int create, const char* ipstr)
106091e9e46SCy Schubert {
107091e9e46SCy Schubert struct resp_addr* node;
108091e9e46SCy Schubert node = (struct resp_addr*)addr_tree_find(&set->ip_tree, addr, addrlen, net);
109091e9e46SCy Schubert if(!node && create) {
110091e9e46SCy Schubert node = regional_alloc_zero(set->region, sizeof(*node));
111091e9e46SCy Schubert if(!node) {
112091e9e46SCy Schubert log_err("out of memory");
113091e9e46SCy Schubert return NULL;
114091e9e46SCy Schubert }
115091e9e46SCy Schubert lock_rw_init(&node->lock);
116091e9e46SCy Schubert node->action = respip_none;
117091e9e46SCy Schubert if(!addr_tree_insert(&set->ip_tree, &node->node, addr,
118091e9e46SCy Schubert addrlen, net)) {
119091e9e46SCy Schubert /* We know we didn't find it, so this should be
120091e9e46SCy Schubert * impossible. */
121091e9e46SCy Schubert log_warn("unexpected: duplicate address: %s", ipstr);
122091e9e46SCy Schubert }
123091e9e46SCy Schubert }
124091e9e46SCy Schubert return node;
125091e9e46SCy Schubert }
126091e9e46SCy Schubert
127091e9e46SCy Schubert void
respip_sockaddr_delete(struct respip_set * set,struct resp_addr * node)128091e9e46SCy Schubert respip_sockaddr_delete(struct respip_set* set, struct resp_addr* node)
129091e9e46SCy Schubert {
130091e9e46SCy Schubert struct resp_addr* prev;
131091e9e46SCy Schubert prev = (struct resp_addr*)rbtree_previous((struct rbnode_type*)node);
132091e9e46SCy Schubert lock_rw_destroy(&node->lock);
1335469a995SCy Schubert (void)rbtree_delete(&set->ip_tree, node);
134091e9e46SCy Schubert /* no free'ing, all allocated in region */
135091e9e46SCy Schubert if(!prev)
136091e9e46SCy Schubert addr_tree_init_parents((rbtree_type*)set);
137091e9e46SCy Schubert else
138091e9e46SCy Schubert addr_tree_init_parents_node(&prev->node);
139091e9e46SCy Schubert }
140091e9e46SCy Schubert
14165b390aaSDag-Erling Smørgrav /** returns the node in the address tree for the specified netblock string;
14265b390aaSDag-Erling Smørgrav * non-existent node will be created if 'create' is true */
14365b390aaSDag-Erling Smørgrav static struct resp_addr*
respip_find_or_create(struct respip_set * set,const char * ipstr,int create)14465b390aaSDag-Erling Smørgrav respip_find_or_create(struct respip_set* set, const char* ipstr, int create)
14565b390aaSDag-Erling Smørgrav {
14665b390aaSDag-Erling Smørgrav struct sockaddr_storage addr;
14765b390aaSDag-Erling Smørgrav int net;
14865b390aaSDag-Erling Smørgrav socklen_t addrlen;
14965b390aaSDag-Erling Smørgrav
15065b390aaSDag-Erling Smørgrav if(!netblockstrtoaddr(ipstr, 0, &addr, &addrlen, &net)) {
15165b390aaSDag-Erling Smørgrav log_err("cannot parse netblock: '%s'", ipstr);
15265b390aaSDag-Erling Smørgrav return NULL;
15365b390aaSDag-Erling Smørgrav }
154091e9e46SCy Schubert return respip_sockaddr_find_or_create(set, &addr, addrlen, net, create,
155091e9e46SCy Schubert ipstr);
15665b390aaSDag-Erling Smørgrav }
15765b390aaSDag-Erling Smørgrav
15865b390aaSDag-Erling Smørgrav static int
respip_tag_cfg(struct respip_set * set,const char * ipstr,const uint8_t * taglist,size_t taglen)15965b390aaSDag-Erling Smørgrav respip_tag_cfg(struct respip_set* set, const char* ipstr,
16065b390aaSDag-Erling Smørgrav const uint8_t* taglist, size_t taglen)
16165b390aaSDag-Erling Smørgrav {
16265b390aaSDag-Erling Smørgrav struct resp_addr* node;
16365b390aaSDag-Erling Smørgrav
16465b390aaSDag-Erling Smørgrav if(!(node=respip_find_or_create(set, ipstr, 1)))
16565b390aaSDag-Erling Smørgrav return 0;
16665b390aaSDag-Erling Smørgrav if(node->taglist) {
16765b390aaSDag-Erling Smørgrav log_warn("duplicate response-address-tag for '%s', overridden.",
16865b390aaSDag-Erling Smørgrav ipstr);
16965b390aaSDag-Erling Smørgrav }
17065b390aaSDag-Erling Smørgrav node->taglist = regional_alloc_init(set->region, taglist, taglen);
17165b390aaSDag-Erling Smørgrav if(!node->taglist) {
17265b390aaSDag-Erling Smørgrav log_err("out of memory");
17365b390aaSDag-Erling Smørgrav return 0;
17465b390aaSDag-Erling Smørgrav }
17565b390aaSDag-Erling Smørgrav node->taglen = taglen;
17665b390aaSDag-Erling Smørgrav return 1;
17765b390aaSDag-Erling Smørgrav }
17865b390aaSDag-Erling Smørgrav
17965b390aaSDag-Erling Smørgrav /** set action for the node specified by the netblock string */
18065b390aaSDag-Erling Smørgrav static int
respip_action_cfg(struct respip_set * set,const char * ipstr,const char * actnstr)18165b390aaSDag-Erling Smørgrav respip_action_cfg(struct respip_set* set, const char* ipstr,
18265b390aaSDag-Erling Smørgrav const char* actnstr)
18365b390aaSDag-Erling Smørgrav {
18465b390aaSDag-Erling Smørgrav struct resp_addr* node;
18565b390aaSDag-Erling Smørgrav enum respip_action action;
18665b390aaSDag-Erling Smørgrav
18765b390aaSDag-Erling Smørgrav if(!(node=respip_find_or_create(set, ipstr, 1)))
18865b390aaSDag-Erling Smørgrav return 0;
18965b390aaSDag-Erling Smørgrav if(node->action != respip_none) {
190a755b6f6SDag-Erling Smørgrav verbose(VERB_QUERY, "duplicate response-ip action for '%s', overridden.",
19165b390aaSDag-Erling Smørgrav ipstr);
19265b390aaSDag-Erling Smørgrav }
19365b390aaSDag-Erling Smørgrav if(strcmp(actnstr, "deny") == 0)
19465b390aaSDag-Erling Smørgrav action = respip_deny;
19565b390aaSDag-Erling Smørgrav else if(strcmp(actnstr, "redirect") == 0)
19665b390aaSDag-Erling Smørgrav action = respip_redirect;
19765b390aaSDag-Erling Smørgrav else if(strcmp(actnstr, "inform") == 0)
19865b390aaSDag-Erling Smørgrav action = respip_inform;
19965b390aaSDag-Erling Smørgrav else if(strcmp(actnstr, "inform_deny") == 0)
20065b390aaSDag-Erling Smørgrav action = respip_inform_deny;
201e86b9096SDag-Erling Smørgrav else if(strcmp(actnstr, "inform_redirect") == 0)
202e86b9096SDag-Erling Smørgrav action = respip_inform_redirect;
20365b390aaSDag-Erling Smørgrav else if(strcmp(actnstr, "always_transparent") == 0)
20465b390aaSDag-Erling Smørgrav action = respip_always_transparent;
20565b390aaSDag-Erling Smørgrav else if(strcmp(actnstr, "always_refuse") == 0)
20665b390aaSDag-Erling Smørgrav action = respip_always_refuse;
20765b390aaSDag-Erling Smørgrav else if(strcmp(actnstr, "always_nxdomain") == 0)
20865b390aaSDag-Erling Smørgrav action = respip_always_nxdomain;
209091e9e46SCy Schubert else if(strcmp(actnstr, "always_nodata") == 0)
210091e9e46SCy Schubert action = respip_always_nodata;
211091e9e46SCy Schubert else if(strcmp(actnstr, "always_deny") == 0)
212091e9e46SCy Schubert action = respip_always_deny;
21365b390aaSDag-Erling Smørgrav else {
21465b390aaSDag-Erling Smørgrav log_err("unknown response-ip action %s", actnstr);
21565b390aaSDag-Erling Smørgrav return 0;
21665b390aaSDag-Erling Smørgrav }
21765b390aaSDag-Erling Smørgrav node->action = action;
21865b390aaSDag-Erling Smørgrav return 1;
21965b390aaSDag-Erling Smørgrav }
22065b390aaSDag-Erling Smørgrav
22165b390aaSDag-Erling Smørgrav /** allocate and initialize an rrset structure; this function is based
22265b390aaSDag-Erling Smørgrav * on new_local_rrset() from the localzone.c module */
22365b390aaSDag-Erling Smørgrav static struct ub_packed_rrset_key*
new_rrset(struct regional * region,uint16_t rrtype,uint16_t rrclass)22465b390aaSDag-Erling Smørgrav new_rrset(struct regional* region, uint16_t rrtype, uint16_t rrclass)
22565b390aaSDag-Erling Smørgrav {
22665b390aaSDag-Erling Smørgrav struct packed_rrset_data* pd;
22765b390aaSDag-Erling Smørgrav struct ub_packed_rrset_key* rrset = regional_alloc_zero(
22865b390aaSDag-Erling Smørgrav region, sizeof(*rrset));
22965b390aaSDag-Erling Smørgrav if(!rrset) {
23065b390aaSDag-Erling Smørgrav log_err("out of memory");
23165b390aaSDag-Erling Smørgrav return NULL;
23265b390aaSDag-Erling Smørgrav }
23365b390aaSDag-Erling Smørgrav rrset->entry.key = rrset;
23465b390aaSDag-Erling Smørgrav pd = regional_alloc_zero(region, sizeof(*pd));
23565b390aaSDag-Erling Smørgrav if(!pd) {
23665b390aaSDag-Erling Smørgrav log_err("out of memory");
23765b390aaSDag-Erling Smørgrav return NULL;
23865b390aaSDag-Erling Smørgrav }
23965b390aaSDag-Erling Smørgrav pd->trust = rrset_trust_prim_noglue;
24065b390aaSDag-Erling Smørgrav pd->security = sec_status_insecure;
24165b390aaSDag-Erling Smørgrav rrset->entry.data = pd;
24265b390aaSDag-Erling Smørgrav rrset->rk.dname = regional_alloc_zero(region, 1);
24365b390aaSDag-Erling Smørgrav if(!rrset->rk.dname) {
24465b390aaSDag-Erling Smørgrav log_err("out of memory");
24565b390aaSDag-Erling Smørgrav return NULL;
24665b390aaSDag-Erling Smørgrav }
24765b390aaSDag-Erling Smørgrav rrset->rk.dname_len = 1;
24865b390aaSDag-Erling Smørgrav rrset->rk.type = htons(rrtype);
24965b390aaSDag-Erling Smørgrav rrset->rk.rrset_class = htons(rrclass);
25065b390aaSDag-Erling Smørgrav return rrset;
25165b390aaSDag-Erling Smørgrav }
25265b390aaSDag-Erling Smørgrav
25365b390aaSDag-Erling Smørgrav /** enter local data as resource records into a response-ip node */
254091e9e46SCy Schubert
255091e9e46SCy Schubert int
respip_enter_rr(struct regional * region,struct resp_addr * raddr,uint16_t rrtype,uint16_t rrclass,time_t ttl,uint8_t * rdata,size_t rdata_len,const char * rrstr,const char * netblockstr)25665b390aaSDag-Erling Smørgrav respip_enter_rr(struct regional* region, struct resp_addr* raddr,
257091e9e46SCy Schubert uint16_t rrtype, uint16_t rrclass, time_t ttl, uint8_t* rdata,
258091e9e46SCy Schubert size_t rdata_len, const char* rrstr, const char* netblockstr)
259091e9e46SCy Schubert {
260091e9e46SCy Schubert struct packed_rrset_data* pd;
261091e9e46SCy Schubert struct sockaddr* sa;
262091e9e46SCy Schubert sa = (struct sockaddr*)&raddr->node.addr;
263091e9e46SCy Schubert if (rrtype == LDNS_RR_TYPE_CNAME && raddr->data) {
264091e9e46SCy Schubert log_err("CNAME response-ip data (%s) can not co-exist with other "
265091e9e46SCy Schubert "response-ip data for netblock %s", rrstr, netblockstr);
266091e9e46SCy Schubert return 0;
267091e9e46SCy Schubert } else if (raddr->data &&
268091e9e46SCy Schubert raddr->data->rk.type == htons(LDNS_RR_TYPE_CNAME)) {
269091e9e46SCy Schubert log_err("response-ip data (%s) can not be added; CNAME response-ip "
270091e9e46SCy Schubert "data already in place for netblock %s", rrstr, netblockstr);
271091e9e46SCy Schubert return 0;
272091e9e46SCy Schubert } else if((rrtype != LDNS_RR_TYPE_CNAME) &&
273091e9e46SCy Schubert ((sa->sa_family == AF_INET && rrtype != LDNS_RR_TYPE_A) ||
274091e9e46SCy Schubert (sa->sa_family == AF_INET6 && rrtype != LDNS_RR_TYPE_AAAA))) {
275091e9e46SCy Schubert log_err("response-ip data %s record type does not correspond "
276091e9e46SCy Schubert "to netblock %s address family", rrstr, netblockstr);
277091e9e46SCy Schubert return 0;
278091e9e46SCy Schubert }
279091e9e46SCy Schubert
280091e9e46SCy Schubert if(!raddr->data) {
281091e9e46SCy Schubert raddr->data = new_rrset(region, rrtype, rrclass);
282091e9e46SCy Schubert if(!raddr->data)
283091e9e46SCy Schubert return 0;
284091e9e46SCy Schubert }
285091e9e46SCy Schubert pd = raddr->data->entry.data;
286091e9e46SCy Schubert return rrset_insert_rr(region, pd, rdata, rdata_len, ttl, rrstr);
287091e9e46SCy Schubert }
288091e9e46SCy Schubert
289091e9e46SCy Schubert static int
respip_enter_rrstr(struct regional * region,struct resp_addr * raddr,const char * rrstr,const char * netblock)290091e9e46SCy Schubert respip_enter_rrstr(struct regional* region, struct resp_addr* raddr,
29165b390aaSDag-Erling Smørgrav const char* rrstr, const char* netblock)
29265b390aaSDag-Erling Smørgrav {
29365b390aaSDag-Erling Smørgrav uint8_t* nm;
29465b390aaSDag-Erling Smørgrav uint16_t rrtype = 0, rrclass = 0;
29565b390aaSDag-Erling Smørgrav time_t ttl = 0;
29665b390aaSDag-Erling Smørgrav uint8_t rr[LDNS_RR_BUF_SIZE];
29765b390aaSDag-Erling Smørgrav uint8_t* rdata = NULL;
29865b390aaSDag-Erling Smørgrav size_t rdata_len = 0;
29965b390aaSDag-Erling Smørgrav char buf[65536];
30065b390aaSDag-Erling Smørgrav char bufshort[64];
30165b390aaSDag-Erling Smørgrav int ret;
302e86b9096SDag-Erling Smørgrav if(raddr->action != respip_redirect
303e86b9096SDag-Erling Smørgrav && raddr->action != respip_inform_redirect) {
30465b390aaSDag-Erling Smørgrav log_err("cannot parse response-ip-data %s: response-ip "
30565b390aaSDag-Erling Smørgrav "action for %s is not redirect", rrstr, netblock);
30665b390aaSDag-Erling Smørgrav return 0;
30765b390aaSDag-Erling Smørgrav }
30865b390aaSDag-Erling Smørgrav ret = snprintf(buf, sizeof(buf), ". %s", rrstr);
30965b390aaSDag-Erling Smørgrav if(ret < 0 || ret >= (int)sizeof(buf)) {
31065b390aaSDag-Erling Smørgrav strlcpy(bufshort, rrstr, sizeof(bufshort));
31165b390aaSDag-Erling Smørgrav log_err("bad response-ip-data: %s...", bufshort);
31265b390aaSDag-Erling Smørgrav return 0;
31365b390aaSDag-Erling Smørgrav }
31465b390aaSDag-Erling Smørgrav if(!rrstr_get_rr_content(buf, &nm, &rrtype, &rrclass, &ttl, rr, sizeof(rr),
31565b390aaSDag-Erling Smørgrav &rdata, &rdata_len)) {
31665b390aaSDag-Erling Smørgrav log_err("bad response-ip-data: %s", rrstr);
31765b390aaSDag-Erling Smørgrav return 0;
31865b390aaSDag-Erling Smørgrav }
319c7f4d7adSDag-Erling Smørgrav free(nm);
320091e9e46SCy Schubert return respip_enter_rr(region, raddr, rrtype, rrclass, ttl, rdata,
321091e9e46SCy Schubert rdata_len, rrstr, netblock);
32265b390aaSDag-Erling Smørgrav }
32365b390aaSDag-Erling Smørgrav
32465b390aaSDag-Erling Smørgrav static int
respip_data_cfg(struct respip_set * set,const char * ipstr,const char * rrstr)32565b390aaSDag-Erling Smørgrav respip_data_cfg(struct respip_set* set, const char* ipstr, const char* rrstr)
32665b390aaSDag-Erling Smørgrav {
32765b390aaSDag-Erling Smørgrav struct resp_addr* node;
32865b390aaSDag-Erling Smørgrav
32965b390aaSDag-Erling Smørgrav node=respip_find_or_create(set, ipstr, 0);
33065b390aaSDag-Erling Smørgrav if(!node || node->action == respip_none) {
33165b390aaSDag-Erling Smørgrav log_err("cannot parse response-ip-data %s: "
33265b390aaSDag-Erling Smørgrav "response-ip node for %s not found", rrstr, ipstr);
33365b390aaSDag-Erling Smørgrav return 0;
33465b390aaSDag-Erling Smørgrav }
335091e9e46SCy Schubert return respip_enter_rrstr(set->region, node, rrstr, ipstr);
33665b390aaSDag-Erling Smørgrav }
33765b390aaSDag-Erling Smørgrav
33865b390aaSDag-Erling Smørgrav static int
respip_set_apply_cfg(struct respip_set * set,char * const * tagname,int num_tags,struct config_strbytelist * respip_tags,struct config_str2list * respip_actions,struct config_str2list * respip_data)33965b390aaSDag-Erling Smørgrav respip_set_apply_cfg(struct respip_set* set, char* const* tagname, int num_tags,
34065b390aaSDag-Erling Smørgrav struct config_strbytelist* respip_tags,
34165b390aaSDag-Erling Smørgrav struct config_str2list* respip_actions,
34265b390aaSDag-Erling Smørgrav struct config_str2list* respip_data)
34365b390aaSDag-Erling Smørgrav {
34465b390aaSDag-Erling Smørgrav struct config_strbytelist* p;
34565b390aaSDag-Erling Smørgrav struct config_str2list* pa;
34665b390aaSDag-Erling Smørgrav struct config_str2list* pd;
34765b390aaSDag-Erling Smørgrav
34865b390aaSDag-Erling Smørgrav set->tagname = tagname;
34965b390aaSDag-Erling Smørgrav set->num_tags = num_tags;
35065b390aaSDag-Erling Smørgrav
35165b390aaSDag-Erling Smørgrav p = respip_tags;
35265b390aaSDag-Erling Smørgrav while(p) {
35365b390aaSDag-Erling Smørgrav struct config_strbytelist* np = p->next;
35465b390aaSDag-Erling Smørgrav
35565b390aaSDag-Erling Smørgrav log_assert(p->str && p->str2);
35665b390aaSDag-Erling Smørgrav if(!respip_tag_cfg(set, p->str, p->str2, p->str2len)) {
35765b390aaSDag-Erling Smørgrav config_del_strbytelist(p);
35865b390aaSDag-Erling Smørgrav return 0;
35965b390aaSDag-Erling Smørgrav }
36065b390aaSDag-Erling Smørgrav free(p->str);
36165b390aaSDag-Erling Smørgrav free(p->str2);
36265b390aaSDag-Erling Smørgrav free(p);
36365b390aaSDag-Erling Smørgrav p = np;
36465b390aaSDag-Erling Smørgrav }
36565b390aaSDag-Erling Smørgrav
36665b390aaSDag-Erling Smørgrav pa = respip_actions;
36765b390aaSDag-Erling Smørgrav while(pa) {
36865b390aaSDag-Erling Smørgrav struct config_str2list* np = pa->next;
36965b390aaSDag-Erling Smørgrav log_assert(pa->str && pa->str2);
37065b390aaSDag-Erling Smørgrav if(!respip_action_cfg(set, pa->str, pa->str2)) {
37165b390aaSDag-Erling Smørgrav config_deldblstrlist(pa);
37265b390aaSDag-Erling Smørgrav return 0;
37365b390aaSDag-Erling Smørgrav }
37465b390aaSDag-Erling Smørgrav free(pa->str);
37565b390aaSDag-Erling Smørgrav free(pa->str2);
37665b390aaSDag-Erling Smørgrav free(pa);
37765b390aaSDag-Erling Smørgrav pa = np;
37865b390aaSDag-Erling Smørgrav }
37965b390aaSDag-Erling Smørgrav
38065b390aaSDag-Erling Smørgrav pd = respip_data;
38165b390aaSDag-Erling Smørgrav while(pd) {
38265b390aaSDag-Erling Smørgrav struct config_str2list* np = pd->next;
38365b390aaSDag-Erling Smørgrav log_assert(pd->str && pd->str2);
38465b390aaSDag-Erling Smørgrav if(!respip_data_cfg(set, pd->str, pd->str2)) {
38565b390aaSDag-Erling Smørgrav config_deldblstrlist(pd);
38665b390aaSDag-Erling Smørgrav return 0;
38765b390aaSDag-Erling Smørgrav }
38865b390aaSDag-Erling Smørgrav free(pd->str);
38965b390aaSDag-Erling Smørgrav free(pd->str2);
39065b390aaSDag-Erling Smørgrav free(pd);
39165b390aaSDag-Erling Smørgrav pd = np;
39265b390aaSDag-Erling Smørgrav }
3930eefd307SCy Schubert addr_tree_init_parents(&set->ip_tree);
39465b390aaSDag-Erling Smørgrav
39565b390aaSDag-Erling Smørgrav return 1;
39665b390aaSDag-Erling Smørgrav }
39765b390aaSDag-Erling Smørgrav
39865b390aaSDag-Erling Smørgrav int
respip_global_apply_cfg(struct respip_set * set,struct config_file * cfg)39965b390aaSDag-Erling Smørgrav respip_global_apply_cfg(struct respip_set* set, struct config_file* cfg)
40065b390aaSDag-Erling Smørgrav {
40165b390aaSDag-Erling Smørgrav int ret = respip_set_apply_cfg(set, cfg->tagname, cfg->num_tags,
40265b390aaSDag-Erling Smørgrav cfg->respip_tags, cfg->respip_actions, cfg->respip_data);
40365b390aaSDag-Erling Smørgrav cfg->respip_data = NULL;
40465b390aaSDag-Erling Smørgrav cfg->respip_actions = NULL;
40565b390aaSDag-Erling Smørgrav cfg->respip_tags = NULL;
40665b390aaSDag-Erling Smørgrav return ret;
40765b390aaSDag-Erling Smørgrav }
40865b390aaSDag-Erling Smørgrav
40965b390aaSDag-Erling Smørgrav /** Iterate through raw view data and apply the view-specific respip
41065b390aaSDag-Erling Smørgrav * configuration; at this point we should have already seen all the views,
41165b390aaSDag-Erling Smørgrav * so if any of the views that respip data refer to does not exist, that's
41265b390aaSDag-Erling Smørgrav * an error. This additional iteration through view configuration data
41365b390aaSDag-Erling Smørgrav * is expected to not have significant performance impact (or rather, its
41465b390aaSDag-Erling Smørgrav * performance impact is not expected to be prohibitive in the configuration
41565b390aaSDag-Erling Smørgrav * processing phase).
41665b390aaSDag-Erling Smørgrav */
41765b390aaSDag-Erling Smørgrav int
respip_views_apply_cfg(struct views * vs,struct config_file * cfg,int * have_view_respip_cfg)41865b390aaSDag-Erling Smørgrav respip_views_apply_cfg(struct views* vs, struct config_file* cfg,
41965b390aaSDag-Erling Smørgrav int* have_view_respip_cfg)
42065b390aaSDag-Erling Smørgrav {
42165b390aaSDag-Erling Smørgrav struct config_view* cv;
42265b390aaSDag-Erling Smørgrav struct view* v;
42365b390aaSDag-Erling Smørgrav int ret;
42465b390aaSDag-Erling Smørgrav
42565b390aaSDag-Erling Smørgrav for(cv = cfg->views; cv; cv = cv->next) {
42665b390aaSDag-Erling Smørgrav
42765b390aaSDag-Erling Smørgrav /** if no respip config for this view then there's
42865b390aaSDag-Erling Smørgrav * nothing to do; note that even though respip data must go
42965b390aaSDag-Erling Smørgrav * with respip action, we're checking for both here because
43065b390aaSDag-Erling Smørgrav * we want to catch the case where the respip action is missing
43165b390aaSDag-Erling Smørgrav * while the data is present */
43265b390aaSDag-Erling Smørgrav if(!cv->respip_actions && !cv->respip_data)
43365b390aaSDag-Erling Smørgrav continue;
43465b390aaSDag-Erling Smørgrav
43565b390aaSDag-Erling Smørgrav if(!(v = views_find_view(vs, cv->name, 1))) {
43665b390aaSDag-Erling Smørgrav log_err("view '%s' unexpectedly missing", cv->name);
43765b390aaSDag-Erling Smørgrav return 0;
43865b390aaSDag-Erling Smørgrav }
43965b390aaSDag-Erling Smørgrav if(!v->respip_set) {
44065b390aaSDag-Erling Smørgrav v->respip_set = respip_set_create();
44165b390aaSDag-Erling Smørgrav if(!v->respip_set) {
44265b390aaSDag-Erling Smørgrav log_err("out of memory");
44365b390aaSDag-Erling Smørgrav lock_rw_unlock(&v->lock);
44465b390aaSDag-Erling Smørgrav return 0;
44565b390aaSDag-Erling Smørgrav }
44665b390aaSDag-Erling Smørgrav }
44765b390aaSDag-Erling Smørgrav ret = respip_set_apply_cfg(v->respip_set, NULL, 0, NULL,
44865b390aaSDag-Erling Smørgrav cv->respip_actions, cv->respip_data);
44965b390aaSDag-Erling Smørgrav lock_rw_unlock(&v->lock);
45065b390aaSDag-Erling Smørgrav if(!ret) {
45165b390aaSDag-Erling Smørgrav log_err("Error while applying respip configuration "
45265b390aaSDag-Erling Smørgrav "for view '%s'", cv->name);
45365b390aaSDag-Erling Smørgrav return 0;
45465b390aaSDag-Erling Smørgrav }
45565b390aaSDag-Erling Smørgrav *have_view_respip_cfg = (*have_view_respip_cfg ||
45665b390aaSDag-Erling Smørgrav v->respip_set->ip_tree.count);
45765b390aaSDag-Erling Smørgrav cv->respip_actions = NULL;
45865b390aaSDag-Erling Smørgrav cv->respip_data = NULL;
45965b390aaSDag-Erling Smørgrav }
46065b390aaSDag-Erling Smørgrav return 1;
46165b390aaSDag-Erling Smørgrav }
46265b390aaSDag-Erling Smørgrav
46365b390aaSDag-Erling Smørgrav /**
46465b390aaSDag-Erling Smørgrav * make a deep copy of 'key' in 'region'.
46565b390aaSDag-Erling Smørgrav * This is largely derived from packed_rrset_copy_region() and
46665b390aaSDag-Erling Smørgrav * packed_rrset_ptr_fixup(), but differs in the following points:
46765b390aaSDag-Erling Smørgrav *
46865b390aaSDag-Erling Smørgrav * - It doesn't assume all data in 'key' are in a contiguous memory region.
46965b390aaSDag-Erling Smørgrav * Although that would be the case in most cases, 'key' can be passed from
47065b390aaSDag-Erling Smørgrav * a lower-level module and it might not build the rrset to meet the
47165b390aaSDag-Erling Smørgrav * assumption. In fact, an rrset specified as response-ip-data or generated
47265b390aaSDag-Erling Smørgrav * in local_data_find_tag_datas() breaks the assumption. So it would be
47365b390aaSDag-Erling Smørgrav * safer not to naively rely on the assumption. On the other hand, this
47465b390aaSDag-Erling Smørgrav * function ensures the copied rrset data are in a contiguous region so
47565b390aaSDag-Erling Smørgrav * that it won't cause a disruption even if an upper layer module naively
47665b390aaSDag-Erling Smørgrav * assumes the memory layout.
47765b390aaSDag-Erling Smørgrav * - It doesn't copy RRSIGs (if any) in 'key'. The rrset will be used in
47865b390aaSDag-Erling Smørgrav * a reply that was already faked, so it doesn't make much sense to provide
47965b390aaSDag-Erling Smørgrav * partial sigs even if they are valid themselves.
48065b390aaSDag-Erling Smørgrav * - It doesn't adjust TTLs as it basically has to be a verbatim copy of 'key'
48165b390aaSDag-Erling Smørgrav * just allocated in 'region' (the assumption is necessary TTL adjustment
48265b390aaSDag-Erling Smørgrav * has been already done in 'key').
48365b390aaSDag-Erling Smørgrav *
48465b390aaSDag-Erling Smørgrav * This function returns the copied rrset key on success, and NULL on memory
48565b390aaSDag-Erling Smørgrav * allocation failure.
48665b390aaSDag-Erling Smørgrav */
48724e36522SCy Schubert struct ub_packed_rrset_key*
respip_copy_rrset(const struct ub_packed_rrset_key * key,struct regional * region)48824e36522SCy Schubert respip_copy_rrset(const struct ub_packed_rrset_key* key, struct regional* region)
48965b390aaSDag-Erling Smørgrav {
49065b390aaSDag-Erling Smørgrav struct ub_packed_rrset_key* ck = regional_alloc(region,
49165b390aaSDag-Erling Smørgrav sizeof(struct ub_packed_rrset_key));
49265b390aaSDag-Erling Smørgrav struct packed_rrset_data* d;
49365b390aaSDag-Erling Smørgrav struct packed_rrset_data* data = key->entry.data;
49465b390aaSDag-Erling Smørgrav size_t dsize, i;
49565b390aaSDag-Erling Smørgrav uint8_t* nextrdata;
49665b390aaSDag-Erling Smørgrav
49765b390aaSDag-Erling Smørgrav /* derived from packed_rrset_copy_region(), but don't use
49865b390aaSDag-Erling Smørgrav * packed_rrset_sizeof() and do exclude RRSIGs */
49965b390aaSDag-Erling Smørgrav if(!ck)
50065b390aaSDag-Erling Smørgrav return NULL;
50165b390aaSDag-Erling Smørgrav ck->id = key->id;
50265b390aaSDag-Erling Smørgrav memset(&ck->entry, 0, sizeof(ck->entry));
50365b390aaSDag-Erling Smørgrav ck->entry.hash = key->entry.hash;
50465b390aaSDag-Erling Smørgrav ck->entry.key = ck;
50565b390aaSDag-Erling Smørgrav ck->rk = key->rk;
50625039b37SCy Schubert if(key->rk.dname) {
50765b390aaSDag-Erling Smørgrav ck->rk.dname = regional_alloc_init(region, key->rk.dname,
50865b390aaSDag-Erling Smørgrav key->rk.dname_len);
50965b390aaSDag-Erling Smørgrav if(!ck->rk.dname)
51065b390aaSDag-Erling Smørgrav return NULL;
51125039b37SCy Schubert ck->rk.dname_len = key->rk.dname_len;
51225039b37SCy Schubert } else {
51325039b37SCy Schubert ck->rk.dname = NULL;
51425039b37SCy Schubert ck->rk.dname_len = 0;
51525039b37SCy Schubert }
51665b390aaSDag-Erling Smørgrav
5170eefd307SCy Schubert if((unsigned)data->count >= 0xffff00U)
5180eefd307SCy Schubert return NULL; /* guard against integer overflow in dsize */
51965b390aaSDag-Erling Smørgrav dsize = sizeof(struct packed_rrset_data) + data->count *
52065b390aaSDag-Erling Smørgrav (sizeof(size_t)+sizeof(uint8_t*)+sizeof(time_t));
5210eefd307SCy Schubert for(i=0; i<data->count; i++) {
5220eefd307SCy Schubert if((unsigned)dsize >= 0x0fffffffU ||
5230eefd307SCy Schubert (unsigned)data->rr_len[i] >= 0x0fffffffU)
5240eefd307SCy Schubert return NULL; /* guard against integer overflow */
52565b390aaSDag-Erling Smørgrav dsize += data->rr_len[i];
5260eefd307SCy Schubert }
527f44e67d1SCy Schubert d = regional_alloc_zero(region, dsize);
52865b390aaSDag-Erling Smørgrav if(!d)
52965b390aaSDag-Erling Smørgrav return NULL;
53065b390aaSDag-Erling Smørgrav *d = *data;
53165b390aaSDag-Erling Smørgrav d->rrsig_count = 0;
53265b390aaSDag-Erling Smørgrav ck->entry.data = d;
53365b390aaSDag-Erling Smørgrav
53465b390aaSDag-Erling Smørgrav /* derived from packed_rrset_ptr_fixup() with copying the data */
53565b390aaSDag-Erling Smørgrav d->rr_len = (size_t*)((uint8_t*)d + sizeof(struct packed_rrset_data));
53665b390aaSDag-Erling Smørgrav d->rr_data = (uint8_t**)&(d->rr_len[d->count]);
53765b390aaSDag-Erling Smørgrav d->rr_ttl = (time_t*)&(d->rr_data[d->count]);
53865b390aaSDag-Erling Smørgrav nextrdata = (uint8_t*)&(d->rr_ttl[d->count]);
53965b390aaSDag-Erling Smørgrav for(i=0; i<d->count; i++) {
54065b390aaSDag-Erling Smørgrav d->rr_len[i] = data->rr_len[i];
54165b390aaSDag-Erling Smørgrav d->rr_ttl[i] = data->rr_ttl[i];
54265b390aaSDag-Erling Smørgrav d->rr_data[i] = nextrdata;
54365b390aaSDag-Erling Smørgrav memcpy(d->rr_data[i], data->rr_data[i], data->rr_len[i]);
54465b390aaSDag-Erling Smørgrav nextrdata += d->rr_len[i];
54565b390aaSDag-Erling Smørgrav }
54665b390aaSDag-Erling Smørgrav
54765b390aaSDag-Erling Smørgrav return ck;
54865b390aaSDag-Erling Smørgrav }
54965b390aaSDag-Erling Smørgrav
55065b390aaSDag-Erling Smørgrav int
respip_init(struct module_env * env,int id)55165b390aaSDag-Erling Smørgrav respip_init(struct module_env* env, int id)
55265b390aaSDag-Erling Smørgrav {
55365b390aaSDag-Erling Smørgrav (void)env;
55465b390aaSDag-Erling Smørgrav (void)id;
55565b390aaSDag-Erling Smørgrav return 1;
55665b390aaSDag-Erling Smørgrav }
55765b390aaSDag-Erling Smørgrav
55865b390aaSDag-Erling Smørgrav void
respip_deinit(struct module_env * env,int id)55965b390aaSDag-Erling Smørgrav respip_deinit(struct module_env* env, int id)
56065b390aaSDag-Erling Smørgrav {
56165b390aaSDag-Erling Smørgrav (void)env;
56265b390aaSDag-Erling Smørgrav (void)id;
56365b390aaSDag-Erling Smørgrav }
56465b390aaSDag-Erling Smørgrav
56565b390aaSDag-Erling Smørgrav /** Convert a packed AAAA or A RRset to sockaddr. */
56665b390aaSDag-Erling Smørgrav static int
rdata2sockaddr(const struct packed_rrset_data * rd,uint16_t rtype,size_t i,struct sockaddr_storage * ss,socklen_t * addrlenp)56765b390aaSDag-Erling Smørgrav rdata2sockaddr(const struct packed_rrset_data* rd, uint16_t rtype, size_t i,
56865b390aaSDag-Erling Smørgrav struct sockaddr_storage* ss, socklen_t* addrlenp)
56965b390aaSDag-Erling Smørgrav {
57065b390aaSDag-Erling Smørgrav /* unbound can accept and cache odd-length AAAA/A records, so we have
57165b390aaSDag-Erling Smørgrav * to validate the length. */
57265b390aaSDag-Erling Smørgrav if(rtype == LDNS_RR_TYPE_A && rd->rr_len[i] == 6) {
57365b390aaSDag-Erling Smørgrav struct sockaddr_in* sa4 = (struct sockaddr_in*)ss;
57465b390aaSDag-Erling Smørgrav
57565b390aaSDag-Erling Smørgrav memset(sa4, 0, sizeof(*sa4));
57665b390aaSDag-Erling Smørgrav sa4->sin_family = AF_INET;
57765b390aaSDag-Erling Smørgrav memcpy(&sa4->sin_addr, rd->rr_data[i] + 2,
57865b390aaSDag-Erling Smørgrav sizeof(sa4->sin_addr));
57965b390aaSDag-Erling Smørgrav *addrlenp = sizeof(*sa4);
58065b390aaSDag-Erling Smørgrav return 1;
58165b390aaSDag-Erling Smørgrav } else if(rtype == LDNS_RR_TYPE_AAAA && rd->rr_len[i] == 18) {
58265b390aaSDag-Erling Smørgrav struct sockaddr_in6* sa6 = (struct sockaddr_in6*)ss;
58365b390aaSDag-Erling Smørgrav
58465b390aaSDag-Erling Smørgrav memset(sa6, 0, sizeof(*sa6));
58565b390aaSDag-Erling Smørgrav sa6->sin6_family = AF_INET6;
58665b390aaSDag-Erling Smørgrav memcpy(&sa6->sin6_addr, rd->rr_data[i] + 2,
58765b390aaSDag-Erling Smørgrav sizeof(sa6->sin6_addr));
58865b390aaSDag-Erling Smørgrav *addrlenp = sizeof(*sa6);
58965b390aaSDag-Erling Smørgrav return 1;
59065b390aaSDag-Erling Smørgrav }
59165b390aaSDag-Erling Smørgrav return 0;
59265b390aaSDag-Erling Smørgrav }
59365b390aaSDag-Erling Smørgrav
59465b390aaSDag-Erling Smørgrav /**
59565b390aaSDag-Erling Smørgrav * Search the given 'iptree' for response address information that matches
59665b390aaSDag-Erling Smørgrav * any of the IP addresses in an AAAA or A in the answer section of the
59765b390aaSDag-Erling Smørgrav * response (stored in 'rep'). If found, a pointer to the matched resp_addr
59865b390aaSDag-Erling Smørgrav * structure will be returned, and '*rrset_id' is set to the index in
59965b390aaSDag-Erling Smørgrav * rep->rrsets for the RRset that contains the matching IP address record
60065b390aaSDag-Erling Smørgrav * (the index is normally 0, but can be larger than that if this is a CNAME
60165b390aaSDag-Erling Smørgrav * chain or type-ANY response).
602091e9e46SCy Schubert * Returns resp_addr holding read lock.
60365b390aaSDag-Erling Smørgrav */
604091e9e46SCy Schubert static struct resp_addr*
respip_addr_lookup(const struct reply_info * rep,struct respip_set * rs,size_t * rrset_id,size_t * rr_id)605091e9e46SCy Schubert respip_addr_lookup(const struct reply_info *rep, struct respip_set* rs,
60624e36522SCy Schubert size_t* rrset_id, size_t* rr_id)
60765b390aaSDag-Erling Smørgrav {
60865b390aaSDag-Erling Smørgrav size_t i;
60965b390aaSDag-Erling Smørgrav struct resp_addr* ra;
61065b390aaSDag-Erling Smørgrav struct sockaddr_storage ss;
61165b390aaSDag-Erling Smørgrav socklen_t addrlen;
61265b390aaSDag-Erling Smørgrav
613091e9e46SCy Schubert lock_rw_rdlock(&rs->lock);
61465b390aaSDag-Erling Smørgrav for(i=0; i<rep->an_numrrsets; i++) {
61565b390aaSDag-Erling Smørgrav size_t j;
61665b390aaSDag-Erling Smørgrav const struct packed_rrset_data* rd;
61765b390aaSDag-Erling Smørgrav uint16_t rtype = ntohs(rep->rrsets[i]->rk.type);
61865b390aaSDag-Erling Smørgrav
61965b390aaSDag-Erling Smørgrav if(rtype != LDNS_RR_TYPE_A && rtype != LDNS_RR_TYPE_AAAA)
62065b390aaSDag-Erling Smørgrav continue;
62165b390aaSDag-Erling Smørgrav rd = rep->rrsets[i]->entry.data;
62265b390aaSDag-Erling Smørgrav for(j = 0; j < rd->count; j++) {
62365b390aaSDag-Erling Smørgrav if(!rdata2sockaddr(rd, rtype, j, &ss, &addrlen))
62465b390aaSDag-Erling Smørgrav continue;
625091e9e46SCy Schubert ra = (struct resp_addr*)addr_tree_lookup(&rs->ip_tree,
626091e9e46SCy Schubert &ss, addrlen);
62765b390aaSDag-Erling Smørgrav if(ra) {
62865b390aaSDag-Erling Smørgrav *rrset_id = i;
62924e36522SCy Schubert *rr_id = j;
630091e9e46SCy Schubert lock_rw_rdlock(&ra->lock);
631091e9e46SCy Schubert lock_rw_unlock(&rs->lock);
63265b390aaSDag-Erling Smørgrav return ra;
63365b390aaSDag-Erling Smørgrav }
63465b390aaSDag-Erling Smørgrav }
63565b390aaSDag-Erling Smørgrav }
636091e9e46SCy Schubert lock_rw_unlock(&rs->lock);
63765b390aaSDag-Erling Smørgrav return NULL;
63865b390aaSDag-Erling Smørgrav }
63965b390aaSDag-Erling Smørgrav
64065b390aaSDag-Erling Smørgrav /**
64165b390aaSDag-Erling Smørgrav * See if response-ip or tag data should override the original answer rrset
64265b390aaSDag-Erling Smørgrav * (which is rep->rrsets[rrset_id]) and if so override it.
64365b390aaSDag-Erling Smørgrav * This is (mostly) equivalent to localzone.c:local_data_answer() but for
64465b390aaSDag-Erling Smørgrav * response-ip actions.
64565b390aaSDag-Erling Smørgrav * Note that this function distinguishes error conditions from "success but
64665b390aaSDag-Erling Smørgrav * not overridden". This is because we want to avoid accidentally applying
64765b390aaSDag-Erling Smørgrav * the "no data" action in case of error.
64865b390aaSDag-Erling Smørgrav * @param action: action to apply
649091e9e46SCy Schubert * @param data: RRset to use for override
65065b390aaSDag-Erling Smørgrav * @param qtype: original query type
65165b390aaSDag-Erling Smørgrav * @param rep: original reply message
65265b390aaSDag-Erling Smørgrav * @param rrset_id: the rrset ID in 'rep' to which the action should apply
65365b390aaSDag-Erling Smørgrav * @param new_repp: see respip_rewrite_reply
65465b390aaSDag-Erling Smørgrav * @param tag: if >= 0 the tag ID used to determine the action and data
65565b390aaSDag-Erling Smørgrav * @param tag_datas: data corresponding to 'tag'.
65665b390aaSDag-Erling Smørgrav * @param tag_datas_size: size of 'tag_datas'
65765b390aaSDag-Erling Smørgrav * @param tagname: array of tag names, used for logging
65865b390aaSDag-Erling Smørgrav * @param num_tags: size of 'tagname', used for logging
65965b390aaSDag-Erling Smørgrav * @param redirect_rrsetp: ptr to redirect record
66065b390aaSDag-Erling Smørgrav * @param region: region for building new reply
66165b390aaSDag-Erling Smørgrav * @return 1 if overridden, 0 if not overridden, -1 on error.
66265b390aaSDag-Erling Smørgrav */
66365b390aaSDag-Erling Smørgrav static int
respip_data_answer(enum respip_action action,struct ub_packed_rrset_key * data,uint16_t qtype,const struct reply_info * rep,size_t rrset_id,struct reply_info ** new_repp,int tag,struct config_strlist ** tag_datas,size_t tag_datas_size,char * const * tagname,int num_tags,struct ub_packed_rrset_key ** redirect_rrsetp,struct regional * region)664091e9e46SCy Schubert respip_data_answer(enum respip_action action,
665091e9e46SCy Schubert struct ub_packed_rrset_key* data,
66665b390aaSDag-Erling Smørgrav uint16_t qtype, const struct reply_info* rep,
66765b390aaSDag-Erling Smørgrav size_t rrset_id, struct reply_info** new_repp, int tag,
66865b390aaSDag-Erling Smørgrav struct config_strlist** tag_datas, size_t tag_datas_size,
66965b390aaSDag-Erling Smørgrav char* const* tagname, int num_tags,
67065b390aaSDag-Erling Smørgrav struct ub_packed_rrset_key** redirect_rrsetp, struct regional* region)
67165b390aaSDag-Erling Smørgrav {
672091e9e46SCy Schubert struct ub_packed_rrset_key* rp = data;
67365b390aaSDag-Erling Smørgrav struct reply_info* new_rep;
67465b390aaSDag-Erling Smørgrav *redirect_rrsetp = NULL;
67565b390aaSDag-Erling Smørgrav
67665b390aaSDag-Erling Smørgrav if(action == respip_redirect && tag != -1 &&
67765b390aaSDag-Erling Smørgrav (size_t)tag<tag_datas_size && tag_datas[tag]) {
67865b390aaSDag-Erling Smørgrav struct query_info dataqinfo;
67965b390aaSDag-Erling Smørgrav struct ub_packed_rrset_key r;
68065b390aaSDag-Erling Smørgrav
68165b390aaSDag-Erling Smørgrav /* Extract parameters of the original answer rrset that can be
68265b390aaSDag-Erling Smørgrav * rewritten below, in the form of query_info. Note that these
68365b390aaSDag-Erling Smørgrav * can be different from the info of the original query if the
68465b390aaSDag-Erling Smørgrav * rrset is a CNAME target.*/
68565b390aaSDag-Erling Smørgrav memset(&dataqinfo, 0, sizeof(dataqinfo));
68665b390aaSDag-Erling Smørgrav dataqinfo.qname = rep->rrsets[rrset_id]->rk.dname;
68765b390aaSDag-Erling Smørgrav dataqinfo.qname_len = rep->rrsets[rrset_id]->rk.dname_len;
68865b390aaSDag-Erling Smørgrav dataqinfo.qtype = ntohs(rep->rrsets[rrset_id]->rk.type);
68965b390aaSDag-Erling Smørgrav dataqinfo.qclass = ntohs(rep->rrsets[rrset_id]->rk.rrset_class);
69065b390aaSDag-Erling Smørgrav
69165b390aaSDag-Erling Smørgrav memset(&r, 0, sizeof(r));
69265b390aaSDag-Erling Smørgrav if(local_data_find_tag_datas(&dataqinfo, tag_datas[tag], &r,
69365b390aaSDag-Erling Smørgrav region)) {
69465b390aaSDag-Erling Smørgrav verbose(VERB_ALGO,
69565b390aaSDag-Erling Smørgrav "response-ip redirect with tag data [%d] %s",
69665b390aaSDag-Erling Smørgrav tag, (tag<num_tags?tagname[tag]:"null"));
69765b390aaSDag-Erling Smørgrav /* use copy_rrset() to 'normalize' memory layout */
69824e36522SCy Schubert rp = respip_copy_rrset(&r, region);
69965b390aaSDag-Erling Smørgrav if(!rp)
70065b390aaSDag-Erling Smørgrav return -1;
70165b390aaSDag-Erling Smørgrav }
70265b390aaSDag-Erling Smørgrav }
70365b390aaSDag-Erling Smørgrav if(!rp)
70465b390aaSDag-Erling Smørgrav return 0;
70565b390aaSDag-Erling Smørgrav
70665b390aaSDag-Erling Smørgrav /* If we are using response-ip-data, we need to make a copy of rrset
70765b390aaSDag-Erling Smørgrav * to replace the rrset's dname. Note that, unlike local data, we
70865b390aaSDag-Erling Smørgrav * rename the dname for other actions than redirect. This is because
70965b390aaSDag-Erling Smørgrav * response-ip-data isn't associated to any specific name. */
710091e9e46SCy Schubert if(rp == data) {
71124e36522SCy Schubert rp = respip_copy_rrset(rp, region);
71265b390aaSDag-Erling Smørgrav if(!rp)
71365b390aaSDag-Erling Smørgrav return -1;
71465b390aaSDag-Erling Smørgrav rp->rk.dname = rep->rrsets[rrset_id]->rk.dname;
71565b390aaSDag-Erling Smørgrav rp->rk.dname_len = rep->rrsets[rrset_id]->rk.dname_len;
71665b390aaSDag-Erling Smørgrav }
71765b390aaSDag-Erling Smørgrav
71865b390aaSDag-Erling Smørgrav /* Build a new reply with redirect rrset. We keep any preceding CNAMEs
71965b390aaSDag-Erling Smørgrav * and replace the address rrset that triggers the action. If it's
72065b390aaSDag-Erling Smørgrav * type ANY query, however, no other answer records should be kept
72165b390aaSDag-Erling Smørgrav * (note that it can't be a CNAME chain in this case due to
72265b390aaSDag-Erling Smørgrav * sanitizing). */
72365b390aaSDag-Erling Smørgrav if(qtype == LDNS_RR_TYPE_ANY)
72465b390aaSDag-Erling Smørgrav rrset_id = 0;
72565b390aaSDag-Erling Smørgrav new_rep = make_new_reply_info(rep, region, rrset_id + 1, rrset_id);
72665b390aaSDag-Erling Smørgrav if(!new_rep)
72765b390aaSDag-Erling Smørgrav return -1;
72865b390aaSDag-Erling Smørgrav rp->rk.flags |= PACKED_RRSET_FIXEDTTL; /* avoid adjusting TTL */
72965b390aaSDag-Erling Smørgrav new_rep->rrsets[rrset_id] = rp;
73065b390aaSDag-Erling Smørgrav
73165b390aaSDag-Erling Smørgrav *redirect_rrsetp = rp;
73265b390aaSDag-Erling Smørgrav *new_repp = new_rep;
73365b390aaSDag-Erling Smørgrav return 1;
73465b390aaSDag-Erling Smørgrav }
73565b390aaSDag-Erling Smørgrav
73665b390aaSDag-Erling Smørgrav /**
73765b390aaSDag-Erling Smørgrav * apply response ip action in case where no action data is provided.
73865b390aaSDag-Erling Smørgrav * this is similar to localzone.c:lz_zone_answer() but simplified due to
73965b390aaSDag-Erling Smørgrav * the characteristics of response ip:
74065b390aaSDag-Erling Smørgrav * - 'deny' variants will be handled at the caller side
74165b390aaSDag-Erling Smørgrav * - no specific processing for 'transparent' variants: unlike local zones,
74265b390aaSDag-Erling Smørgrav * there is no such a case of 'no data but name existing'. so all variants
74365b390aaSDag-Erling Smørgrav * just mean 'transparent if no data'.
74465b390aaSDag-Erling Smørgrav * @param qtype: query type
74565b390aaSDag-Erling Smørgrav * @param action: found action
74665b390aaSDag-Erling Smørgrav * @param rep:
74765b390aaSDag-Erling Smørgrav * @param new_repp
74865b390aaSDag-Erling Smørgrav * @param rrset_id
74965b390aaSDag-Erling Smørgrav * @param region: region for building new reply
75065b390aaSDag-Erling Smørgrav * @return 1 on success, 0 on error.
75165b390aaSDag-Erling Smørgrav */
75265b390aaSDag-Erling Smørgrav static int
respip_nodata_answer(uint16_t qtype,enum respip_action action,const struct reply_info * rep,size_t rrset_id,struct reply_info ** new_repp,struct regional * region)75365b390aaSDag-Erling Smørgrav respip_nodata_answer(uint16_t qtype, enum respip_action action,
75465b390aaSDag-Erling Smørgrav const struct reply_info *rep, size_t rrset_id,
75565b390aaSDag-Erling Smørgrav struct reply_info** new_repp, struct regional* region)
75665b390aaSDag-Erling Smørgrav {
75765b390aaSDag-Erling Smørgrav struct reply_info* new_rep;
75865b390aaSDag-Erling Smørgrav
75965b390aaSDag-Erling Smørgrav if(action == respip_refuse || action == respip_always_refuse) {
76065b390aaSDag-Erling Smørgrav new_rep = make_new_reply_info(rep, region, 0, 0);
76165b390aaSDag-Erling Smørgrav if(!new_rep)
76265b390aaSDag-Erling Smørgrav return 0;
76365b390aaSDag-Erling Smørgrav FLAGS_SET_RCODE(new_rep->flags, LDNS_RCODE_REFUSED);
76465b390aaSDag-Erling Smørgrav *new_repp = new_rep;
76565b390aaSDag-Erling Smørgrav return 1;
76665b390aaSDag-Erling Smørgrav } else if(action == respip_static || action == respip_redirect ||
767e86b9096SDag-Erling Smørgrav action == respip_always_nxdomain ||
768091e9e46SCy Schubert action == respip_always_nodata ||
769e86b9096SDag-Erling Smørgrav action == respip_inform_redirect) {
77065b390aaSDag-Erling Smørgrav /* Since we don't know about other types of the owner name,
77165b390aaSDag-Erling Smørgrav * we generally return NOERROR/NODATA unless an NXDOMAIN action
77265b390aaSDag-Erling Smørgrav * is explicitly specified. */
77365b390aaSDag-Erling Smørgrav int rcode = (action == respip_always_nxdomain)?
77465b390aaSDag-Erling Smørgrav LDNS_RCODE_NXDOMAIN:LDNS_RCODE_NOERROR;
77565b390aaSDag-Erling Smørgrav /* We should empty the answer section except for any preceding
77665b390aaSDag-Erling Smørgrav * CNAMEs (in that case rrset_id > 0). Type-ANY case is
77765b390aaSDag-Erling Smørgrav * special as noted in respip_data_answer(). */
77865b390aaSDag-Erling Smørgrav if(qtype == LDNS_RR_TYPE_ANY)
77965b390aaSDag-Erling Smørgrav rrset_id = 0;
78065b390aaSDag-Erling Smørgrav new_rep = make_new_reply_info(rep, region, rrset_id, rrset_id);
78165b390aaSDag-Erling Smørgrav if(!new_rep)
78265b390aaSDag-Erling Smørgrav return 0;
78365b390aaSDag-Erling Smørgrav FLAGS_SET_RCODE(new_rep->flags, rcode);
78465b390aaSDag-Erling Smørgrav *new_repp = new_rep;
78565b390aaSDag-Erling Smørgrav return 1;
78665b390aaSDag-Erling Smørgrav }
78765b390aaSDag-Erling Smørgrav
78865b390aaSDag-Erling Smørgrav return 1;
78965b390aaSDag-Erling Smørgrav }
79065b390aaSDag-Erling Smørgrav
79165b390aaSDag-Erling Smørgrav /** Populate action info structure with the results of response-ip action
79265b390aaSDag-Erling Smørgrav * processing, iff as the result of response-ip processing we are actually
79365b390aaSDag-Erling Smørgrav * taking some action. Only action is set if action_only is true.
79465b390aaSDag-Erling Smørgrav * Returns true on success, false on failure.
79565b390aaSDag-Erling Smørgrav */
79665b390aaSDag-Erling Smørgrav static int
populate_action_info(struct respip_action_info * actinfo,enum respip_action action,const struct resp_addr * raddr,const struct ub_packed_rrset_key * ATTR_UNUSED (rrset),int ATTR_UNUSED (tag),const struct respip_set * ATTR_UNUSED (ipset),int ATTR_UNUSED (action_only),struct regional * region,int rpz_used,int rpz_log,char * log_name,int rpz_cname_override)79765b390aaSDag-Erling Smørgrav populate_action_info(struct respip_action_info* actinfo,
79865b390aaSDag-Erling Smørgrav enum respip_action action, const struct resp_addr* raddr,
79965b390aaSDag-Erling Smørgrav const struct ub_packed_rrset_key* ATTR_UNUSED(rrset),
80065b390aaSDag-Erling Smørgrav int ATTR_UNUSED(tag), const struct respip_set* ATTR_UNUSED(ipset),
801091e9e46SCy Schubert int ATTR_UNUSED(action_only), struct regional* region, int rpz_used,
802091e9e46SCy Schubert int rpz_log, char* log_name, int rpz_cname_override)
80365b390aaSDag-Erling Smørgrav {
80465b390aaSDag-Erling Smørgrav if(action == respip_none || !raddr)
80565b390aaSDag-Erling Smørgrav return 1;
80665b390aaSDag-Erling Smørgrav actinfo->action = action;
807091e9e46SCy Schubert actinfo->rpz_used = rpz_used;
808091e9e46SCy Schubert actinfo->rpz_log = rpz_log;
809091e9e46SCy Schubert actinfo->log_name = log_name;
810091e9e46SCy Schubert actinfo->rpz_cname_override = rpz_cname_override;
81165b390aaSDag-Erling Smørgrav
81265b390aaSDag-Erling Smørgrav /* for inform variants, make a copy of the matched address block for
81365b390aaSDag-Erling Smørgrav * later logging. We make a copy to proactively avoid disruption if
81465b390aaSDag-Erling Smørgrav * and when we allow a dynamic update to the respip tree. */
815091e9e46SCy Schubert if(action == respip_inform || action == respip_inform_deny ||
816091e9e46SCy Schubert rpz_used) {
81765b390aaSDag-Erling Smørgrav struct respip_addr_info* a =
81865b390aaSDag-Erling Smørgrav regional_alloc_zero(region, sizeof(*a));
81965b390aaSDag-Erling Smørgrav if(!a) {
82065b390aaSDag-Erling Smørgrav log_err("out of memory");
82165b390aaSDag-Erling Smørgrav return 0;
82265b390aaSDag-Erling Smørgrav }
82365b390aaSDag-Erling Smørgrav a->addr = raddr->node.addr;
82465b390aaSDag-Erling Smørgrav a->addrlen = raddr->node.addrlen;
82565b390aaSDag-Erling Smørgrav a->net = raddr->node.net;
82665b390aaSDag-Erling Smørgrav actinfo->addrinfo = a;
82765b390aaSDag-Erling Smørgrav }
82865b390aaSDag-Erling Smørgrav
82965b390aaSDag-Erling Smørgrav return 1;
83065b390aaSDag-Erling Smørgrav }
83165b390aaSDag-Erling Smørgrav
832091e9e46SCy Schubert static int
respip_use_rpz(struct resp_addr * raddr,struct rpz * r,enum respip_action * action,struct ub_packed_rrset_key ** data,int * rpz_log,char ** log_name,int * rpz_cname_override,struct regional * region,int * is_rpz,int * rpz_passthru)833091e9e46SCy Schubert respip_use_rpz(struct resp_addr* raddr, struct rpz* r,
834091e9e46SCy Schubert enum respip_action* action,
835091e9e46SCy Schubert struct ub_packed_rrset_key** data, int* rpz_log, char** log_name,
836a39a5a69SCy Schubert int* rpz_cname_override, struct regional* region, int* is_rpz,
837a39a5a69SCy Schubert int* rpz_passthru)
838091e9e46SCy Schubert {
839a39a5a69SCy Schubert if(rpz_passthru && *rpz_passthru)
840a39a5a69SCy Schubert return 0;
841091e9e46SCy Schubert if(r->action_override == RPZ_DISABLED_ACTION) {
842091e9e46SCy Schubert *is_rpz = 0;
843091e9e46SCy Schubert return 1;
844091e9e46SCy Schubert }
845091e9e46SCy Schubert else if(r->action_override == RPZ_NO_OVERRIDE_ACTION)
846091e9e46SCy Schubert *action = raddr->action;
847091e9e46SCy Schubert else
848091e9e46SCy Schubert *action = rpz_action_to_respip_action(r->action_override);
849091e9e46SCy Schubert if(r->action_override == RPZ_CNAME_OVERRIDE_ACTION &&
850091e9e46SCy Schubert r->cname_override) {
851091e9e46SCy Schubert *data = r->cname_override;
852091e9e46SCy Schubert *rpz_cname_override = 1;
853091e9e46SCy Schubert }
854a39a5a69SCy Schubert if(*action == respip_always_transparent /* RPZ_PASSTHRU_ACTION */
855a39a5a69SCy Schubert && rpz_passthru)
856a39a5a69SCy Schubert *rpz_passthru = 1;
857091e9e46SCy Schubert *rpz_log = r->log;
858091e9e46SCy Schubert if(r->log_name)
859091e9e46SCy Schubert if(!(*log_name = regional_strdup(region, r->log_name)))
860091e9e46SCy Schubert return 0;
861091e9e46SCy Schubert *is_rpz = 1;
862091e9e46SCy Schubert return 1;
863091e9e46SCy Schubert }
864091e9e46SCy Schubert
86565b390aaSDag-Erling Smørgrav int
respip_rewrite_reply(const struct query_info * qinfo,const struct respip_client_info * cinfo,const struct reply_info * rep,struct reply_info ** new_repp,struct respip_action_info * actinfo,struct ub_packed_rrset_key ** alias_rrset,int search_only,struct regional * region,struct auth_zones * az,int * rpz_passthru)86665b390aaSDag-Erling Smørgrav respip_rewrite_reply(const struct query_info* qinfo,
86765b390aaSDag-Erling Smørgrav const struct respip_client_info* cinfo, const struct reply_info* rep,
86865b390aaSDag-Erling Smørgrav struct reply_info** new_repp, struct respip_action_info* actinfo,
86965b390aaSDag-Erling Smørgrav struct ub_packed_rrset_key** alias_rrset, int search_only,
870a39a5a69SCy Schubert struct regional* region, struct auth_zones* az, int* rpz_passthru)
87165b390aaSDag-Erling Smørgrav {
87265b390aaSDag-Erling Smørgrav const uint8_t* ctaglist;
87365b390aaSDag-Erling Smørgrav size_t ctaglen;
87465b390aaSDag-Erling Smørgrav const uint8_t* tag_actions;
87565b390aaSDag-Erling Smørgrav size_t tag_actions_size;
87665b390aaSDag-Erling Smørgrav struct config_strlist** tag_datas;
87765b390aaSDag-Erling Smørgrav size_t tag_datas_size;
87865b390aaSDag-Erling Smørgrav struct view* view = NULL;
87965b390aaSDag-Erling Smørgrav struct respip_set* ipset = NULL;
88024e36522SCy Schubert size_t rrset_id = 0, rr_id = 0;
88165b390aaSDag-Erling Smørgrav enum respip_action action = respip_none;
88265b390aaSDag-Erling Smørgrav int tag = -1;
883091e9e46SCy Schubert struct resp_addr* raddr = NULL;
88465b390aaSDag-Erling Smørgrav int ret = 1;
88565b390aaSDag-Erling Smørgrav struct ub_packed_rrset_key* redirect_rrset = NULL;
886091e9e46SCy Schubert struct rpz* r;
887369c6923SCy Schubert struct auth_zone* a = NULL;
888091e9e46SCy Schubert struct ub_packed_rrset_key* data = NULL;
889091e9e46SCy Schubert int rpz_used = 0;
890091e9e46SCy Schubert int rpz_log = 0;
891091e9e46SCy Schubert int rpz_cname_override = 0;
892091e9e46SCy Schubert char* log_name = NULL;
89365b390aaSDag-Erling Smørgrav
89465b390aaSDag-Erling Smørgrav if(!cinfo)
89565b390aaSDag-Erling Smørgrav goto done;
89665b390aaSDag-Erling Smørgrav ctaglist = cinfo->taglist;
89765b390aaSDag-Erling Smørgrav ctaglen = cinfo->taglen;
89865b390aaSDag-Erling Smørgrav tag_actions = cinfo->tag_actions;
89965b390aaSDag-Erling Smørgrav tag_actions_size = cinfo->tag_actions_size;
90065b390aaSDag-Erling Smørgrav tag_datas = cinfo->tag_datas;
90165b390aaSDag-Erling Smørgrav tag_datas_size = cinfo->tag_datas_size;
90265b390aaSDag-Erling Smørgrav view = cinfo->view;
90365b390aaSDag-Erling Smørgrav ipset = cinfo->respip_set;
90465b390aaSDag-Erling Smørgrav
905091e9e46SCy Schubert log_assert(ipset);
906091e9e46SCy Schubert
90765b390aaSDag-Erling Smørgrav /** Try to use response-ip config from the view first; use
90865b390aaSDag-Erling Smørgrav * global response-ip config if we don't have the view or we don't
90965b390aaSDag-Erling Smørgrav * have the matching per-view config (and the view allows the use
91065b390aaSDag-Erling Smørgrav * of global data in this case).
91165b390aaSDag-Erling Smørgrav * Note that we lock the view even if we only use view members that
91265b390aaSDag-Erling Smørgrav * currently don't change after creation. This is for safety for
91365b390aaSDag-Erling Smørgrav * future possible changes as the view documentation seems to expect
91465b390aaSDag-Erling Smørgrav * any of its member can change in the view's lifetime.
91565b390aaSDag-Erling Smørgrav * Note also that we assume 'view' is valid in this function, which
91665b390aaSDag-Erling Smørgrav * should be safe (see unbound bug #1191) */
91765b390aaSDag-Erling Smørgrav if(view) {
91865b390aaSDag-Erling Smørgrav lock_rw_rdlock(&view->lock);
91965b390aaSDag-Erling Smørgrav if(view->respip_set) {
92065b390aaSDag-Erling Smørgrav if((raddr = respip_addr_lookup(rep,
92124e36522SCy Schubert view->respip_set, &rrset_id, &rr_id))) {
92265b390aaSDag-Erling Smørgrav /** for per-view respip directives the action
92365b390aaSDag-Erling Smørgrav * can only be direct (i.e. not tag-based) */
92465b390aaSDag-Erling Smørgrav action = raddr->action;
92565b390aaSDag-Erling Smørgrav }
92665b390aaSDag-Erling Smørgrav }
92765b390aaSDag-Erling Smørgrav if(!raddr && !view->isfirst)
92865b390aaSDag-Erling Smørgrav goto done;
92925039b37SCy Schubert if(!raddr && view->isfirst) {
93025039b37SCy Schubert lock_rw_unlock(&view->lock);
93125039b37SCy Schubert view = NULL;
93225039b37SCy Schubert }
93365b390aaSDag-Erling Smørgrav }
934091e9e46SCy Schubert if(!raddr && (raddr = respip_addr_lookup(rep, ipset,
93524e36522SCy Schubert &rrset_id, &rr_id))) {
93665b390aaSDag-Erling Smørgrav action = (enum respip_action)local_data_find_tag_action(
93765b390aaSDag-Erling Smørgrav raddr->taglist, raddr->taglen, ctaglist, ctaglen,
93865b390aaSDag-Erling Smørgrav tag_actions, tag_actions_size,
93965b390aaSDag-Erling Smørgrav (enum localzone_type)raddr->action, &tag,
94065b390aaSDag-Erling Smørgrav ipset->tagname, ipset->num_tags);
94165b390aaSDag-Erling Smørgrav }
942091e9e46SCy Schubert lock_rw_rdlock(&az->rpz_lock);
943a39a5a69SCy Schubert for(a = az->rpz_first; a && !raddr && !(rpz_passthru && *rpz_passthru); a = a->rpz_az_next) {
94425039b37SCy Schubert lock_rw_rdlock(&a->lock);
94525039b37SCy Schubert r = a->rpz;
946091e9e46SCy Schubert if(!r->taglist || taglist_intersect(r->taglist,
947091e9e46SCy Schubert r->taglistlen, ctaglist, ctaglen)) {
948091e9e46SCy Schubert if((raddr = respip_addr_lookup(rep,
94924e36522SCy Schubert r->respip_set, &rrset_id, &rr_id))) {
950091e9e46SCy Schubert if(!respip_use_rpz(raddr, r, &action, &data,
951091e9e46SCy Schubert &rpz_log, &log_name, &rpz_cname_override,
952a39a5a69SCy Schubert region, &rpz_used, rpz_passthru)) {
953091e9e46SCy Schubert log_err("out of memory");
954091e9e46SCy Schubert lock_rw_unlock(&raddr->lock);
95525039b37SCy Schubert lock_rw_unlock(&a->lock);
956091e9e46SCy Schubert lock_rw_unlock(&az->rpz_lock);
957091e9e46SCy Schubert return 0;
958091e9e46SCy Schubert }
95925039b37SCy Schubert if(rpz_used) {
96024e36522SCy Schubert if(verbosity >= VERB_ALGO) {
96124e36522SCy Schubert struct sockaddr_storage ss;
96224e36522SCy Schubert socklen_t ss_len = 0;
96324e36522SCy Schubert char nm[256], ip[256];
96424e36522SCy Schubert char qn[255+1];
96524e36522SCy Schubert if(!rdata2sockaddr(rep->rrsets[rrset_id]->entry.data, ntohs(rep->rrsets[rrset_id]->rk.type), rr_id, &ss, &ss_len))
96624e36522SCy Schubert snprintf(ip, sizeof(ip), "invalidRRdata");
96724e36522SCy Schubert else
96824e36522SCy Schubert addr_to_str(&ss, ss_len, ip, sizeof(ip));
96924e36522SCy Schubert dname_str(qinfo->qname, qn);
97024e36522SCy Schubert addr_to_str(&raddr->node.addr,
97124e36522SCy Schubert raddr->node.addrlen,
97224e36522SCy Schubert nm, sizeof(nm));
973a39a5a69SCy Schubert 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)));
97424e36522SCy Schubert }
97525039b37SCy Schubert /* break to make sure 'a' stays pointed
97625039b37SCy Schubert * to used auth_zone, and keeps lock */
97725039b37SCy Schubert break;
97825039b37SCy Schubert }
979091e9e46SCy Schubert lock_rw_unlock(&raddr->lock);
980091e9e46SCy Schubert raddr = NULL;
981091e9e46SCy Schubert actinfo->rpz_disabled++;
982091e9e46SCy Schubert }
983091e9e46SCy Schubert }
98425039b37SCy Schubert lock_rw_unlock(&a->lock);
985091e9e46SCy Schubert }
986091e9e46SCy Schubert lock_rw_unlock(&az->rpz_lock);
98765b390aaSDag-Erling Smørgrav if(raddr && !search_only) {
98865b390aaSDag-Erling Smørgrav int result = 0;
98965b390aaSDag-Erling Smørgrav
99065b390aaSDag-Erling Smørgrav /* first, see if we have response-ip or tag action for the
99165b390aaSDag-Erling Smørgrav * action except for 'always' variants. */
99265b390aaSDag-Erling Smørgrav if(action != respip_always_refuse
99365b390aaSDag-Erling Smørgrav && action != respip_always_transparent
99465b390aaSDag-Erling Smørgrav && action != respip_always_nxdomain
995091e9e46SCy Schubert && action != respip_always_nodata
996091e9e46SCy Schubert && action != respip_always_deny
997091e9e46SCy Schubert && (result = respip_data_answer(action,
998091e9e46SCy Schubert (data) ? data : raddr->data, qinfo->qtype, rep,
999091e9e46SCy Schubert rrset_id, new_repp, tag, tag_datas, tag_datas_size,
1000091e9e46SCy Schubert ipset->tagname, ipset->num_tags, &redirect_rrset,
1001091e9e46SCy Schubert region)) < 0) {
100265b390aaSDag-Erling Smørgrav ret = 0;
100365b390aaSDag-Erling Smørgrav goto done;
100465b390aaSDag-Erling Smørgrav }
100565b390aaSDag-Erling Smørgrav
100665b390aaSDag-Erling Smørgrav /* if no action data applied, take action specific to the
100765b390aaSDag-Erling Smørgrav * action without data. */
100865b390aaSDag-Erling Smørgrav if(!result && !respip_nodata_answer(qinfo->qtype, action, rep,
100965b390aaSDag-Erling Smørgrav rrset_id, new_repp, region)) {
101065b390aaSDag-Erling Smørgrav ret = 0;
101165b390aaSDag-Erling Smørgrav goto done;
101265b390aaSDag-Erling Smørgrav }
101365b390aaSDag-Erling Smørgrav }
101465b390aaSDag-Erling Smørgrav done:
101565b390aaSDag-Erling Smørgrav if(view) {
101665b390aaSDag-Erling Smørgrav lock_rw_unlock(&view->lock);
101765b390aaSDag-Erling Smørgrav }
101865b390aaSDag-Erling Smørgrav if(ret) {
101965b390aaSDag-Erling Smørgrav /* If we're redirecting the original answer to a
102065b390aaSDag-Erling Smørgrav * CNAME, record the CNAME rrset so the caller can take
102165b390aaSDag-Erling Smørgrav * the appropriate action. Note that we don't check the
102265b390aaSDag-Erling Smørgrav * action type; it should normally be 'redirect', but it
102365b390aaSDag-Erling Smørgrav * can be of other type when a data-dependent tag action
102465b390aaSDag-Erling Smørgrav * uses redirect response-ip data.
102565b390aaSDag-Erling Smørgrav */
102665b390aaSDag-Erling Smørgrav if(redirect_rrset &&
102765b390aaSDag-Erling Smørgrav redirect_rrset->rk.type == ntohs(LDNS_RR_TYPE_CNAME) &&
102865b390aaSDag-Erling Smørgrav qinfo->qtype != LDNS_RR_TYPE_ANY)
102965b390aaSDag-Erling Smørgrav *alias_rrset = redirect_rrset;
103065b390aaSDag-Erling Smørgrav /* on success, populate respip result structure */
103165b390aaSDag-Erling Smørgrav ret = populate_action_info(actinfo, action, raddr,
1032091e9e46SCy Schubert redirect_rrset, tag, ipset, search_only, region,
1033091e9e46SCy Schubert rpz_used, rpz_log, log_name, rpz_cname_override);
1034091e9e46SCy Schubert }
1035091e9e46SCy Schubert if(raddr) {
1036091e9e46SCy Schubert lock_rw_unlock(&raddr->lock);
103765b390aaSDag-Erling Smørgrav }
103825039b37SCy Schubert if(rpz_used) {
103925039b37SCy Schubert lock_rw_unlock(&a->lock);
104025039b37SCy Schubert }
104165b390aaSDag-Erling Smørgrav return ret;
104265b390aaSDag-Erling Smørgrav }
104365b390aaSDag-Erling Smørgrav
104465b390aaSDag-Erling Smørgrav static int
generate_cname_request(struct module_qstate * qstate,struct ub_packed_rrset_key * alias_rrset)104565b390aaSDag-Erling Smørgrav generate_cname_request(struct module_qstate* qstate,
104665b390aaSDag-Erling Smørgrav struct ub_packed_rrset_key* alias_rrset)
104765b390aaSDag-Erling Smørgrav {
104865b390aaSDag-Erling Smørgrav struct module_qstate* subq = NULL;
104965b390aaSDag-Erling Smørgrav struct query_info subqi;
105065b390aaSDag-Erling Smørgrav
105165b390aaSDag-Erling Smørgrav memset(&subqi, 0, sizeof(subqi));
105265b390aaSDag-Erling Smørgrav get_cname_target(alias_rrset, &subqi.qname, &subqi.qname_len);
105365b390aaSDag-Erling Smørgrav if(!subqi.qname)
105465b390aaSDag-Erling Smørgrav return 0; /* unexpected: not a valid CNAME RDATA */
105565b390aaSDag-Erling Smørgrav subqi.qtype = qstate->qinfo.qtype;
105665b390aaSDag-Erling Smørgrav subqi.qclass = qstate->qinfo.qclass;
105765b390aaSDag-Erling Smørgrav fptr_ok(fptr_whitelist_modenv_attach_sub(qstate->env->attach_sub));
105865b390aaSDag-Erling Smørgrav return (*qstate->env->attach_sub)(qstate, &subqi, BIT_RD, 0, 0, &subq);
105965b390aaSDag-Erling Smørgrav }
106065b390aaSDag-Erling Smørgrav
106165b390aaSDag-Erling Smørgrav void
respip_operate(struct module_qstate * qstate,enum module_ev event,int id,struct outbound_entry * outbound)106265b390aaSDag-Erling Smørgrav respip_operate(struct module_qstate* qstate, enum module_ev event, int id,
106365b390aaSDag-Erling Smørgrav struct outbound_entry* outbound)
106465b390aaSDag-Erling Smørgrav {
106565b390aaSDag-Erling Smørgrav struct respip_qstate* rq = (struct respip_qstate*)qstate->minfo[id];
106665b390aaSDag-Erling Smørgrav
106765b390aaSDag-Erling Smørgrav log_query_info(VERB_QUERY, "respip operate: query", &qstate->qinfo);
106865b390aaSDag-Erling Smørgrav (void)outbound;
106965b390aaSDag-Erling Smørgrav
107065b390aaSDag-Erling Smørgrav if(event == module_event_new || event == module_event_pass) {
107165b390aaSDag-Erling Smørgrav if(!rq) {
107265b390aaSDag-Erling Smørgrav rq = regional_alloc_zero(qstate->region, sizeof(*rq));
107365b390aaSDag-Erling Smørgrav if(!rq)
107465b390aaSDag-Erling Smørgrav goto servfail;
107565b390aaSDag-Erling Smørgrav rq->state = RESPIP_INIT;
107665b390aaSDag-Erling Smørgrav qstate->minfo[id] = rq;
107765b390aaSDag-Erling Smørgrav }
107865b390aaSDag-Erling Smørgrav if(rq->state == RESPIP_SUBQUERY_FINISHED) {
107965b390aaSDag-Erling Smørgrav qstate->ext_state[id] = module_finished;
108065b390aaSDag-Erling Smørgrav return;
108165b390aaSDag-Erling Smørgrav }
108265b390aaSDag-Erling Smørgrav verbose(VERB_ALGO, "respip: pass to next module");
108365b390aaSDag-Erling Smørgrav qstate->ext_state[id] = module_wait_module;
108465b390aaSDag-Erling Smørgrav } else if(event == module_event_moddone) {
108565b390aaSDag-Erling Smørgrav /* If the reply may be subject to response-ip rewriting
108665b390aaSDag-Erling Smørgrav * according to the query type, check the actions. If a
108765b390aaSDag-Erling Smørgrav * rewrite is necessary, we'll replace the reply in qstate
108865b390aaSDag-Erling Smørgrav * with the new one. */
108965b390aaSDag-Erling Smørgrav enum module_ext_state next_state = module_finished;
109065b390aaSDag-Erling Smørgrav
109165b390aaSDag-Erling Smørgrav if((qstate->qinfo.qtype == LDNS_RR_TYPE_A ||
109265b390aaSDag-Erling Smørgrav qstate->qinfo.qtype == LDNS_RR_TYPE_AAAA ||
109365b390aaSDag-Erling Smørgrav qstate->qinfo.qtype == LDNS_RR_TYPE_ANY) &&
109465b390aaSDag-Erling Smørgrav qstate->return_msg && qstate->return_msg->rep) {
109565b390aaSDag-Erling Smørgrav struct reply_info* new_rep = qstate->return_msg->rep;
109665b390aaSDag-Erling Smørgrav struct ub_packed_rrset_key* alias_rrset = NULL;
1097369c6923SCy Schubert struct respip_action_info actinfo = {0, 0, 0, 0, NULL, 0, NULL};
1098091e9e46SCy Schubert actinfo.action = respip_none;
109965b390aaSDag-Erling Smørgrav
110065b390aaSDag-Erling Smørgrav if(!respip_rewrite_reply(&qstate->qinfo,
110165b390aaSDag-Erling Smørgrav qstate->client_info, qstate->return_msg->rep,
110265b390aaSDag-Erling Smørgrav &new_rep, &actinfo, &alias_rrset, 0,
1103a39a5a69SCy Schubert qstate->region, qstate->env->auth_zones,
1104a39a5a69SCy Schubert &qstate->rpz_passthru)) {
110565b390aaSDag-Erling Smørgrav goto servfail;
110665b390aaSDag-Erling Smørgrav }
110765b390aaSDag-Erling Smørgrav if(actinfo.action != respip_none) {
110865b390aaSDag-Erling Smørgrav /* save action info for logging on a
110965b390aaSDag-Erling Smørgrav * per-front-end-query basis */
111065b390aaSDag-Erling Smørgrav if(!(qstate->respip_action_info =
111165b390aaSDag-Erling Smørgrav regional_alloc_init(qstate->region,
111265b390aaSDag-Erling Smørgrav &actinfo, sizeof(actinfo))))
111365b390aaSDag-Erling Smørgrav {
111465b390aaSDag-Erling Smørgrav log_err("out of memory");
111565b390aaSDag-Erling Smørgrav goto servfail;
111665b390aaSDag-Erling Smørgrav }
111765b390aaSDag-Erling Smørgrav } else {
111865b390aaSDag-Erling Smørgrav qstate->respip_action_info = NULL;
111965b390aaSDag-Erling Smørgrav }
1120091e9e46SCy Schubert if (actinfo.action == respip_always_deny ||
1121091e9e46SCy Schubert (new_rep == qstate->return_msg->rep &&
112265b390aaSDag-Erling Smørgrav (actinfo.action == respip_deny ||
1123091e9e46SCy Schubert actinfo.action == respip_inform_deny))) {
112465b390aaSDag-Erling Smørgrav /* for deny-variant actions (unless response-ip
112565b390aaSDag-Erling Smørgrav * data is applied), mark the query state so
112665b390aaSDag-Erling Smørgrav * the response will be dropped for all
112765b390aaSDag-Erling Smørgrav * clients. */
112865b390aaSDag-Erling Smørgrav qstate->is_drop = 1;
112965b390aaSDag-Erling Smørgrav } else if(alias_rrset) {
113065b390aaSDag-Erling Smørgrav if(!generate_cname_request(qstate, alias_rrset))
113165b390aaSDag-Erling Smørgrav goto servfail;
113265b390aaSDag-Erling Smørgrav next_state = module_wait_subquery;
113365b390aaSDag-Erling Smørgrav }
113465b390aaSDag-Erling Smørgrav qstate->return_msg->rep = new_rep;
113565b390aaSDag-Erling Smørgrav }
113665b390aaSDag-Erling Smørgrav qstate->ext_state[id] = next_state;
113765b390aaSDag-Erling Smørgrav } else
113865b390aaSDag-Erling Smørgrav qstate->ext_state[id] = module_finished;
113965b390aaSDag-Erling Smørgrav
114065b390aaSDag-Erling Smørgrav return;
114165b390aaSDag-Erling Smørgrav
114265b390aaSDag-Erling Smørgrav servfail:
114365b390aaSDag-Erling Smørgrav qstate->return_rcode = LDNS_RCODE_SERVFAIL;
114465b390aaSDag-Erling Smørgrav qstate->return_msg = NULL;
114565b390aaSDag-Erling Smørgrav }
114665b390aaSDag-Erling Smørgrav
114765b390aaSDag-Erling Smørgrav int
respip_merge_cname(struct reply_info * base_rep,const struct query_info * qinfo,const struct reply_info * tgt_rep,const struct respip_client_info * cinfo,int must_validate,struct reply_info ** new_repp,struct regional * region,struct auth_zones * az)114865b390aaSDag-Erling Smørgrav respip_merge_cname(struct reply_info* base_rep,
114965b390aaSDag-Erling Smørgrav const struct query_info* qinfo, const struct reply_info* tgt_rep,
115065b390aaSDag-Erling Smørgrav const struct respip_client_info* cinfo, int must_validate,
1151091e9e46SCy Schubert struct reply_info** new_repp, struct regional* region,
1152091e9e46SCy Schubert struct auth_zones* az)
115365b390aaSDag-Erling Smørgrav {
115465b390aaSDag-Erling Smørgrav struct reply_info* new_rep;
115565b390aaSDag-Erling Smørgrav struct reply_info* tmp_rep = NULL; /* just a placeholder */
115665b390aaSDag-Erling Smørgrav struct ub_packed_rrset_key* alias_rrset = NULL; /* ditto */
115765b390aaSDag-Erling Smørgrav uint16_t tgt_rcode;
115865b390aaSDag-Erling Smørgrav size_t i, j;
1159369c6923SCy Schubert struct respip_action_info actinfo = {0, 0, 0, 0, NULL, 0, NULL};
1160091e9e46SCy Schubert actinfo.action = respip_none;
116165b390aaSDag-Erling Smørgrav
116265b390aaSDag-Erling Smørgrav /* If the query for the CNAME target would result in an unusual rcode,
116365b390aaSDag-Erling Smørgrav * we generally translate it as a failure for the base query
116465b390aaSDag-Erling Smørgrav * (which would then be translated into SERVFAIL). The only exception
116565b390aaSDag-Erling Smørgrav * is NXDOMAIN and YXDOMAIN, which are passed to the end client(s).
116665b390aaSDag-Erling Smørgrav * The YXDOMAIN case would be rare but still possible (when
116765b390aaSDag-Erling Smørgrav * DNSSEC-validated DNAME has been cached but synthesizing CNAME
116865b390aaSDag-Erling Smørgrav * can't be generated due to length limitation) */
116965b390aaSDag-Erling Smørgrav tgt_rcode = FLAGS_GET_RCODE(tgt_rep->flags);
117065b390aaSDag-Erling Smørgrav if((tgt_rcode != LDNS_RCODE_NOERROR &&
117165b390aaSDag-Erling Smørgrav tgt_rcode != LDNS_RCODE_NXDOMAIN &&
117265b390aaSDag-Erling Smørgrav tgt_rcode != LDNS_RCODE_YXDOMAIN) ||
117365b390aaSDag-Erling Smørgrav (must_validate && tgt_rep->security <= sec_status_bogus)) {
117465b390aaSDag-Erling Smørgrav return 0;
117565b390aaSDag-Erling Smørgrav }
117665b390aaSDag-Erling Smørgrav
117765b390aaSDag-Erling Smørgrav /* see if the target reply would be subject to a response-ip action. */
117865b390aaSDag-Erling Smørgrav if(!respip_rewrite_reply(qinfo, cinfo, tgt_rep, &tmp_rep, &actinfo,
1179a39a5a69SCy Schubert &alias_rrset, 1, region, az, NULL))
118065b390aaSDag-Erling Smørgrav return 0;
118165b390aaSDag-Erling Smørgrav if(actinfo.action != respip_none) {
118265b390aaSDag-Erling Smørgrav log_info("CNAME target of redirect response-ip action would "
118365b390aaSDag-Erling Smørgrav "be subject to response-ip action, too; stripped");
118465b390aaSDag-Erling Smørgrav *new_repp = base_rep;
118565b390aaSDag-Erling Smørgrav return 1;
118665b390aaSDag-Erling Smørgrav }
118765b390aaSDag-Erling Smørgrav
118865b390aaSDag-Erling Smørgrav /* Append target reply to the base. Since we cannot assume
118965b390aaSDag-Erling Smørgrav * tgt_rep->rrsets is valid throughout the lifetime of new_rep
119065b390aaSDag-Erling Smørgrav * or it can be safely shared by multiple threads, we need to make a
119165b390aaSDag-Erling Smørgrav * deep copy. */
119265b390aaSDag-Erling Smørgrav new_rep = make_new_reply_info(base_rep, region,
119365b390aaSDag-Erling Smørgrav base_rep->an_numrrsets + tgt_rep->an_numrrsets,
119465b390aaSDag-Erling Smørgrav base_rep->an_numrrsets);
119565b390aaSDag-Erling Smørgrav if(!new_rep)
119665b390aaSDag-Erling Smørgrav return 0;
119765b390aaSDag-Erling Smørgrav for(i=0,j=base_rep->an_numrrsets; i<tgt_rep->an_numrrsets; i++,j++) {
119824e36522SCy Schubert new_rep->rrsets[j] = respip_copy_rrset(tgt_rep->rrsets[i], region);
119965b390aaSDag-Erling Smørgrav if(!new_rep->rrsets[j])
120065b390aaSDag-Erling Smørgrav return 0;
120165b390aaSDag-Erling Smørgrav }
120265b390aaSDag-Erling Smørgrav
120365b390aaSDag-Erling Smørgrav FLAGS_SET_RCODE(new_rep->flags, tgt_rcode);
120465b390aaSDag-Erling Smørgrav *new_repp = new_rep;
120565b390aaSDag-Erling Smørgrav return 1;
120665b390aaSDag-Erling Smørgrav }
120765b390aaSDag-Erling Smørgrav
120865b390aaSDag-Erling Smørgrav void
respip_inform_super(struct module_qstate * qstate,int id,struct module_qstate * super)120965b390aaSDag-Erling Smørgrav respip_inform_super(struct module_qstate* qstate, int id,
121065b390aaSDag-Erling Smørgrav struct module_qstate* super)
121165b390aaSDag-Erling Smørgrav {
121265b390aaSDag-Erling Smørgrav struct respip_qstate* rq = (struct respip_qstate*)super->minfo[id];
121365b390aaSDag-Erling Smørgrav struct reply_info* new_rep = NULL;
121465b390aaSDag-Erling Smørgrav
121565b390aaSDag-Erling Smørgrav rq->state = RESPIP_SUBQUERY_FINISHED;
121665b390aaSDag-Erling Smørgrav
121765b390aaSDag-Erling Smørgrav /* respip subquery should have always been created with a valid reply
121865b390aaSDag-Erling Smørgrav * in super. */
121965b390aaSDag-Erling Smørgrav log_assert(super->return_msg && super->return_msg->rep);
122065b390aaSDag-Erling Smørgrav
122165b390aaSDag-Erling Smørgrav /* return_msg can be NULL when, e.g., the sub query resulted in
122265b390aaSDag-Erling Smørgrav * SERVFAIL, in which case we regard it as a failure of the original
122365b390aaSDag-Erling Smørgrav * query. Other checks are probably redundant, but we check them
122465b390aaSDag-Erling Smørgrav * for safety. */
122565b390aaSDag-Erling Smørgrav if(!qstate->return_msg || !qstate->return_msg->rep ||
122665b390aaSDag-Erling Smørgrav qstate->return_rcode != LDNS_RCODE_NOERROR)
122765b390aaSDag-Erling Smørgrav goto fail;
122865b390aaSDag-Erling Smørgrav
122965b390aaSDag-Erling Smørgrav if(!respip_merge_cname(super->return_msg->rep, &qstate->qinfo,
123065b390aaSDag-Erling Smørgrav qstate->return_msg->rep, super->client_info,
1231091e9e46SCy Schubert super->env->need_to_validate, &new_rep, super->region,
1232091e9e46SCy Schubert qstate->env->auth_zones))
123365b390aaSDag-Erling Smørgrav goto fail;
123465b390aaSDag-Erling Smørgrav super->return_msg->rep = new_rep;
123565b390aaSDag-Erling Smørgrav return;
123665b390aaSDag-Erling Smørgrav
123765b390aaSDag-Erling Smørgrav fail:
123865b390aaSDag-Erling Smørgrav super->return_rcode = LDNS_RCODE_SERVFAIL;
123965b390aaSDag-Erling Smørgrav super->return_msg = NULL;
124065b390aaSDag-Erling Smørgrav return;
124165b390aaSDag-Erling Smørgrav }
124265b390aaSDag-Erling Smørgrav
124365b390aaSDag-Erling Smørgrav void
respip_clear(struct module_qstate * qstate,int id)124465b390aaSDag-Erling Smørgrav respip_clear(struct module_qstate* qstate, int id)
124565b390aaSDag-Erling Smørgrav {
124665b390aaSDag-Erling Smørgrav qstate->minfo[id] = NULL;
124765b390aaSDag-Erling Smørgrav }
124865b390aaSDag-Erling Smørgrav
124965b390aaSDag-Erling Smørgrav size_t
respip_get_mem(struct module_env * env,int id)125065b390aaSDag-Erling Smørgrav respip_get_mem(struct module_env* env, int id)
125165b390aaSDag-Erling Smørgrav {
125265b390aaSDag-Erling Smørgrav (void)env;
125365b390aaSDag-Erling Smørgrav (void)id;
125465b390aaSDag-Erling Smørgrav return 0;
125565b390aaSDag-Erling Smørgrav }
125665b390aaSDag-Erling Smørgrav
125765b390aaSDag-Erling Smørgrav /**
125865b390aaSDag-Erling Smørgrav * The response-ip function block
125965b390aaSDag-Erling Smørgrav */
126065b390aaSDag-Erling Smørgrav static struct module_func_block respip_block = {
126165b390aaSDag-Erling Smørgrav "respip",
1262*56850988SCy Schubert NULL, NULL, &respip_init, &respip_deinit, &respip_operate,
1263*56850988SCy Schubert &respip_inform_super, &respip_clear, &respip_get_mem
126465b390aaSDag-Erling Smørgrav };
126565b390aaSDag-Erling Smørgrav
126665b390aaSDag-Erling Smørgrav struct module_func_block*
respip_get_funcblock(void)126765b390aaSDag-Erling Smørgrav respip_get_funcblock(void)
126865b390aaSDag-Erling Smørgrav {
126965b390aaSDag-Erling Smørgrav return &respip_block;
127065b390aaSDag-Erling Smørgrav }
127165b390aaSDag-Erling Smørgrav
127265b390aaSDag-Erling Smørgrav enum respip_action
resp_addr_get_action(const struct resp_addr * addr)127365b390aaSDag-Erling Smørgrav resp_addr_get_action(const struct resp_addr* addr)
127465b390aaSDag-Erling Smørgrav {
127565b390aaSDag-Erling Smørgrav return addr ? addr->action : respip_none;
127665b390aaSDag-Erling Smørgrav }
127765b390aaSDag-Erling Smørgrav
127865b390aaSDag-Erling Smørgrav struct ub_packed_rrset_key*
resp_addr_get_rrset(struct resp_addr * addr)127965b390aaSDag-Erling Smørgrav resp_addr_get_rrset(struct resp_addr* addr)
128065b390aaSDag-Erling Smørgrav {
128165b390aaSDag-Erling Smørgrav return addr ? addr->data : NULL;
128265b390aaSDag-Erling Smørgrav }
128365b390aaSDag-Erling Smørgrav
128465b390aaSDag-Erling Smørgrav int
respip_set_is_empty(const struct respip_set * set)128565b390aaSDag-Erling Smørgrav respip_set_is_empty(const struct respip_set* set)
128665b390aaSDag-Erling Smørgrav {
128765b390aaSDag-Erling Smørgrav return set ? set->ip_tree.count == 0 : 1;
128865b390aaSDag-Erling Smørgrav }
128965b390aaSDag-Erling Smørgrav
129065b390aaSDag-Erling Smørgrav void
respip_inform_print(struct respip_action_info * respip_actinfo,uint8_t * qname,uint16_t qtype,uint16_t qclass,struct local_rrset * local_alias,struct sockaddr_storage * addr,socklen_t addrlen)1291091e9e46SCy Schubert respip_inform_print(struct respip_action_info* respip_actinfo, uint8_t* qname,
129265b390aaSDag-Erling Smørgrav uint16_t qtype, uint16_t qclass, struct local_rrset* local_alias,
1293865f46b2SCy Schubert struct sockaddr_storage* addr, socklen_t addrlen)
129465b390aaSDag-Erling Smørgrav {
129565b390aaSDag-Erling Smørgrav char srcip[128], respip[128], txt[512];
129665b390aaSDag-Erling Smørgrav unsigned port;
1297091e9e46SCy Schubert struct respip_addr_info* respip_addr = respip_actinfo->addrinfo;
1298091e9e46SCy Schubert size_t txtlen = 0;
1299091e9e46SCy Schubert const char* actionstr = NULL;
130065b390aaSDag-Erling Smørgrav
130165b390aaSDag-Erling Smørgrav if(local_alias)
130265b390aaSDag-Erling Smørgrav qname = local_alias->rrset->rk.dname;
1303865f46b2SCy Schubert port = (unsigned)((addr->ss_family == AF_INET) ?
1304865f46b2SCy Schubert ntohs(((struct sockaddr_in*)addr)->sin_port) :
1305865f46b2SCy Schubert ntohs(((struct sockaddr_in6*)addr)->sin6_port));
1306865f46b2SCy Schubert addr_to_str(addr, addrlen, srcip, sizeof(srcip));
130765b390aaSDag-Erling Smørgrav addr_to_str(&respip_addr->addr, respip_addr->addrlen,
130865b390aaSDag-Erling Smørgrav respip, sizeof(respip));
1309091e9e46SCy Schubert if(respip_actinfo->rpz_log) {
1310091e9e46SCy Schubert txtlen += snprintf(txt+txtlen, sizeof(txt)-txtlen, "%s",
1311a39a5a69SCy Schubert "rpz: applied ");
1312091e9e46SCy Schubert if(respip_actinfo->rpz_cname_override)
1313091e9e46SCy Schubert actionstr = rpz_action_to_string(
1314091e9e46SCy Schubert RPZ_CNAME_OVERRIDE_ACTION);
1315091e9e46SCy Schubert else
1316091e9e46SCy Schubert actionstr = rpz_action_to_string(
1317091e9e46SCy Schubert respip_action_to_rpz_action(
1318091e9e46SCy Schubert respip_actinfo->action));
1319091e9e46SCy Schubert }
1320091e9e46SCy Schubert if(respip_actinfo->log_name) {
1321091e9e46SCy Schubert txtlen += snprintf(txt+txtlen, sizeof(txt)-txtlen,
1322091e9e46SCy Schubert "[%s] ", respip_actinfo->log_name);
1323091e9e46SCy Schubert }
1324091e9e46SCy Schubert snprintf(txt+txtlen, sizeof(txt)-txtlen,
1325091e9e46SCy Schubert "%s/%d %s %s@%u", respip, respip_addr->net,
1326091e9e46SCy Schubert (actionstr) ? actionstr : "inform", srcip, port);
13270eefd307SCy Schubert log_nametypeclass(NO_VERBOSE, txt, qname, qtype, qclass);
132865b390aaSDag-Erling Smørgrav }
1329