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