1 /*
2  * edns-subnet/subnetmod.c - edns subnet module. Must be called before validator
3  * and iterator.
4  *
5  * Copyright (c) 2013, NLnet Labs. All rights reserved.
6  *
7  * This software is open source.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * Redistributions of source code must retain the above copyright notice,
14  * this list of conditions and the following disclaimer.
15  *
16  * Redistributions in binary form must reproduce the above copyright notice,
17  * this list of conditions and the following disclaimer in the documentation
18  * and/or other materials provided with the distribution.
19  *
20  * Neither the name of the NLNET LABS nor the names of its contributors may
21  * be used to endorse or promote products derived from this software without
22  * specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
27  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
28  * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
30  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
31  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
32  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
33  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
34  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35  */
36  /**
37  * \file
38  * subnet module for unbound.
39  */
40 
41 #include "config.h"
42 
43 #ifdef CLIENT_SUBNET /* keeps splint happy */
44 
45 #include "edns-subnet/subnetmod.h"
46 #include "edns-subnet/edns-subnet.h"
47 #include "edns-subnet/addrtree.h"
48 #include "edns-subnet/subnet-whitelist.h"
49 
50 #include "services/mesh.h"
51 #include "services/cache/dns.h"
52 #include "util/module.h"
53 #include "util/regional.h"
54 #include "util/storage/slabhash.h"
55 #include "util/config_file.h"
56 #include "util/data/msgreply.h"
57 #include "sldns/sbuffer.h"
58 #include "sldns/wire2str.h"
59 #include "iterator/iter_utils.h"
60 
61 /** externally called */
62 void
63 subnet_data_delete(void *d, void *ATTR_UNUSED(arg))
64 {
65 	struct subnet_msg_cache_data *r;
66 	r = (struct subnet_msg_cache_data*)d;
67 	addrtree_delete(r->tree4);
68 	addrtree_delete(r->tree6);
69 	free(r);
70 }
71 
72 /** externally called */
73 size_t
74 msg_cache_sizefunc(void *k, void *d)
75 {
76 	struct msgreply_entry *q = (struct msgreply_entry*)k;
77 	struct subnet_msg_cache_data *r = (struct subnet_msg_cache_data*)d;
78 	size_t s = sizeof(struct msgreply_entry)
79 		+ sizeof(struct subnet_msg_cache_data)
80 		+ q->key.qname_len + lock_get_mem(&q->entry.lock);
81 	s += addrtree_size(r->tree4);
82 	s += addrtree_size(r->tree6);
83 	return s;
84 }
85 
86 /** new query for ecs module */
87 static int
88 subnet_new_qstate(struct module_qstate *qstate, int id)
89 {
90 	struct subnet_qstate *sq = (struct subnet_qstate*)regional_alloc(
91 		qstate->region, sizeof(struct subnet_qstate));
92 	if(!sq)
93 		return 0;
94 	qstate->minfo[id] = sq;
95 	memset(sq, 0, sizeof(*sq));
96 	sq->started_no_cache_store = qstate->no_cache_store;
97 	sq->started_no_cache_lookup = qstate->no_cache_lookup;
98 	return 1;
99 }
100 
101 /** Add ecs struct to edns list, after parsing it to wire format. */
102 void
103 subnet_ecs_opt_list_append(struct ecs_data* ecs, struct edns_option** list,
104 	struct module_qstate *qstate, struct regional *region)
105 {
106 	size_t sn_octs, sn_octs_remainder;
107 	sldns_buffer* buf = qstate->env->scratch_buffer;
108 
109 	if(ecs->subnet_validdata) {
110 		log_assert(ecs->subnet_addr_fam == EDNSSUBNET_ADDRFAM_IP4 ||
111 			ecs->subnet_addr_fam == EDNSSUBNET_ADDRFAM_IP6);
112 		log_assert(ecs->subnet_addr_fam != EDNSSUBNET_ADDRFAM_IP4 ||
113 			ecs->subnet_source_mask <=  INET_SIZE*8);
114 		log_assert(ecs->subnet_addr_fam != EDNSSUBNET_ADDRFAM_IP6 ||
115 			ecs->subnet_source_mask <= INET6_SIZE*8);
116 
117 		sn_octs = ecs->subnet_source_mask / 8;
118 		sn_octs_remainder =
119 			(size_t)((ecs->subnet_source_mask % 8)>0?1:0);
120 
121 		log_assert(sn_octs + sn_octs_remainder <= INET6_SIZE);
122 
123 		sldns_buffer_clear(buf);
124 		sldns_buffer_write_u16(buf, ecs->subnet_addr_fam);
125 		sldns_buffer_write_u8(buf, ecs->subnet_source_mask);
126 		sldns_buffer_write_u8(buf, ecs->subnet_scope_mask);
127 		sldns_buffer_write(buf, ecs->subnet_addr, sn_octs);
128 		if(sn_octs_remainder)
129 			sldns_buffer_write_u8(buf, ecs->subnet_addr[sn_octs] &
130 				~(0xFF >> (ecs->subnet_source_mask % 8)));
131 		sldns_buffer_flip(buf);
132 
133 		edns_opt_list_append(list,
134 				qstate->env->cfg->client_subnet_opcode,
135 				sn_octs + sn_octs_remainder + 4,
136 				sldns_buffer_begin(buf), region);
137 	}
138 }
139 
140 int ecs_whitelist_check(struct query_info* qinfo,
141 	uint16_t ATTR_UNUSED(flags), struct module_qstate* qstate,
142 	struct sockaddr_storage* addr, socklen_t addrlen,
143 	uint8_t* ATTR_UNUSED(zone), size_t ATTR_UNUSED(zonelen),
144 	struct regional *region, int id, void* ATTR_UNUSED(cbargs))
145 {
146 	struct subnet_qstate *sq;
147 	struct subnet_env *sn_env;
148 
149 	if(!(sq=(struct subnet_qstate*)qstate->minfo[id]))
150 		return 1;
151 	sn_env = (struct subnet_env*)qstate->env->modinfo[id];
152 
153 	/* Cache by default, might be disabled after parsing EDNS option
154 	 * received from nameserver. */
155 	if(!iter_stub_fwd_no_cache(qstate, &qstate->qinfo, NULL, NULL)) {
156 		qstate->no_cache_store = 0;
157 	}
158 
159 	if(sq->ecs_server_out.subnet_validdata && ((sq->subnet_downstream &&
160 		qstate->env->cfg->client_subnet_always_forward) ||
161 		ecs_is_whitelisted(sn_env->whitelist,
162 		addr, addrlen, qinfo->qname, qinfo->qname_len,
163 		qinfo->qclass))) {
164 		/* Address on whitelist or client query contains ECS option, we
165 		 * want to sent out ECS. Only add option if it is not already
166 		 * set. */
167 		if(!edns_opt_list_find(qstate->edns_opts_back_out,
168 			qstate->env->cfg->client_subnet_opcode)) {
169 			subnet_ecs_opt_list_append(&sq->ecs_server_out,
170 				&qstate->edns_opts_back_out, qstate, region);
171 		}
172 		sq->subnet_sent = 1;
173 	}
174 	else {
175 		/* Outgoing ECS option is set, but we don't want to sent it to
176 		 * this address, remove option. */
177 		if(edns_opt_list_find(qstate->edns_opts_back_out,
178 			qstate->env->cfg->client_subnet_opcode)) {
179 			edns_opt_list_remove(&qstate->edns_opts_back_out,
180 				qstate->env->cfg->client_subnet_opcode);
181 		}
182 		sq->subnet_sent = 0;
183 	}
184 	return 1;
185 }
186 
187 
188 void
189 subnet_markdel(void* key)
190 {
191 	struct msgreply_entry *e = (struct msgreply_entry*)key;
192 	e->key.qtype = 0;
193 	e->key.qclass = 0;
194 }
195 
196 int
197 subnetmod_init(struct module_env *env, int id)
198 {
199 	struct subnet_env *sn_env = (struct subnet_env*)calloc(1,
200 		sizeof(struct subnet_env));
201 	if(!sn_env) {
202 		log_err("malloc failure");
203 		return 0;
204 	}
205 	alloc_init(&sn_env->alloc, NULL, 0);
206 	env->modinfo[id] = (void*)sn_env;
207 
208 	/* Warn that serve-expired and prefetch do not work with the subnet
209 	 * module cache. */
210 	if(env->cfg->serve_expired)
211 		log_warn(
212 			"subnetcache: serve-expired is set but not working "
213 			"for data originating from the subnet module cache.");
214 	if(env->cfg->prefetch)
215 		log_warn(
216 			"subnetcache: prefetch is set but not working "
217 			"for data originating from the subnet module cache.");
218 	/* Copy msg_cache settings */
219 	sn_env->subnet_msg_cache = slabhash_create(env->cfg->msg_cache_slabs,
220 		HASH_DEFAULT_STARTARRAY, env->cfg->msg_cache_size,
221 		msg_cache_sizefunc, query_info_compare, query_entry_delete,
222 		subnet_data_delete, NULL);
223 	slabhash_setmarkdel(sn_env->subnet_msg_cache, &subnet_markdel);
224 	if(!sn_env->subnet_msg_cache) {
225 		log_err("subnetcache: could not create cache");
226 		free(sn_env);
227 		env->modinfo[id] = NULL;
228 		return 0;
229 	}
230 	/* whitelist for edns subnet capable servers */
231 	sn_env->whitelist = ecs_whitelist_create();
232 	if(!sn_env->whitelist ||
233 		!ecs_whitelist_apply_cfg(sn_env->whitelist, env->cfg)) {
234 		log_err("subnetcache: could not create ECS whitelist");
235 		slabhash_delete(sn_env->subnet_msg_cache);
236 		free(sn_env);
237 		env->modinfo[id] = NULL;
238 		return 0;
239 	}
240 
241 	verbose(VERB_QUERY, "subnetcache: option registered (%d)",
242 		env->cfg->client_subnet_opcode);
243 	/* Create new mesh state for all queries. */
244 	env->unique_mesh = 1;
245 	if(!edns_register_option(env->cfg->client_subnet_opcode,
246 		env->cfg->client_subnet_always_forward /* bypass cache */,
247 		1 /* no aggregation */, env)) {
248 		log_err("subnetcache: could not register opcode");
249 		ecs_whitelist_delete(sn_env->whitelist);
250 		slabhash_delete(sn_env->subnet_msg_cache);
251 		free(sn_env);
252 		env->modinfo[id] = NULL;
253 		return 0;
254 	}
255 	inplace_cb_register((void*)ecs_whitelist_check, inplace_cb_query, NULL,
256 		env, id);
257 	inplace_cb_register((void*)ecs_edns_back_parsed,
258 		inplace_cb_edns_back_parsed, NULL, env, id);
259 	inplace_cb_register((void*)ecs_query_response,
260 		inplace_cb_query_response, NULL, env, id);
261 	lock_rw_init(&sn_env->biglock);
262 	return 1;
263 }
264 
265 void
266 subnetmod_deinit(struct module_env *env, int id)
267 {
268 	struct subnet_env *sn_env;
269 	if(!env || !env->modinfo[id])
270 		return;
271 	sn_env = (struct subnet_env*)env->modinfo[id];
272 	lock_rw_destroy(&sn_env->biglock);
273 	inplace_cb_delete(env, inplace_cb_edns_back_parsed, id);
274 	inplace_cb_delete(env, inplace_cb_query, id);
275 	inplace_cb_delete(env, inplace_cb_query_response, id);
276 	ecs_whitelist_delete(sn_env->whitelist);
277 	slabhash_delete(sn_env->subnet_msg_cache);
278 	alloc_clear(&sn_env->alloc);
279 	free(sn_env);
280 	env->modinfo[id] = NULL;
281 }
282 
283 /** Tells client that upstream has no/improper support */
284 static void
285 cp_edns_bad_response(struct ecs_data *target, struct ecs_data *source)
286 {
287 	target->subnet_scope_mask  = 0;
288 	target->subnet_source_mask = source->subnet_source_mask;
289 	target->subnet_addr_fam    = source->subnet_addr_fam;
290 	memcpy(target->subnet_addr, source->subnet_addr, INET6_SIZE);
291 	target->subnet_validdata = 1;
292 }
293 
294 static void
295 delfunc(void *envptr, void *elemptr) {
296 	struct reply_info *elem = (struct reply_info *)elemptr;
297 	struct subnet_env *env = (struct subnet_env *)envptr;
298 	reply_info_parsedelete(elem, &env->alloc);
299 }
300 
301 static size_t
302 sizefunc(void *elemptr) {
303 	struct reply_info *elem  = (struct reply_info *)elemptr;
304 	return sizeof (struct reply_info) - sizeof (struct rrset_ref)
305 		+ elem->rrset_count * sizeof (struct rrset_ref)
306 		+ elem->rrset_count * sizeof (struct ub_packed_rrset_key *);
307 }
308 
309 /**
310  * Select tree from cache entry based on edns data.
311  * If for address family not present it will create a new one.
312  * NULL on failure to create. */
313 static struct addrtree*
314 get_tree(struct subnet_msg_cache_data *data, struct ecs_data *edns,
315 	struct subnet_env *env, struct config_file* cfg)
316 {
317 	struct addrtree *tree;
318 	if (edns->subnet_addr_fam == EDNSSUBNET_ADDRFAM_IP4) {
319 		if (!data->tree4)
320 			data->tree4 = addrtree_create(
321 				cfg->max_client_subnet_ipv4, &delfunc,
322 				&sizefunc, env, cfg->max_ecs_tree_size_ipv4);
323 		tree = data->tree4;
324 	} else {
325 		if (!data->tree6)
326 			data->tree6 = addrtree_create(
327 				cfg->max_client_subnet_ipv6, &delfunc,
328 				&sizefunc, env, cfg->max_ecs_tree_size_ipv6);
329 		tree = data->tree6;
330 	}
331 	return tree;
332 }
333 
334 static void
335 update_cache(struct module_qstate *qstate, int id)
336 {
337 	struct msgreply_entry *mrep_entry;
338 	struct addrtree *tree;
339 	struct reply_info *rep;
340 	struct query_info qinf;
341 	struct subnet_env *sne = qstate->env->modinfo[id];
342 	struct subnet_qstate *sq = (struct subnet_qstate*)qstate->minfo[id];
343 	struct slabhash *subnet_msg_cache = sne->subnet_msg_cache;
344 	struct ecs_data *edns = &sq->ecs_client_in;
345 	size_t i;
346 	int only_match_scope_zero;
347 
348 	/* We already calculated hash upon lookup (lookup_and_reply) if we were
349 	 * allowed to look in the ECS cache */
350 	hashvalue_type h = qstate->minfo[id] &&
351 		((struct subnet_qstate*)qstate->minfo[id])->qinfo_hash_calculated?
352 		((struct subnet_qstate*)qstate->minfo[id])->qinfo_hash :
353 		query_info_hash(&qstate->qinfo, qstate->query_flags);
354 	/* Step 1, general qinfo lookup */
355 	struct lruhash_entry *lru_entry = slabhash_lookup(subnet_msg_cache, h,
356 		&qstate->qinfo, 1);
357 	int need_to_insert = (lru_entry == NULL);
358 	if (!lru_entry) {
359 		void* data = calloc(1,
360 			sizeof(struct subnet_msg_cache_data));
361 		if(!data) {
362 			log_err("malloc failed");
363 			return;
364 		}
365 		qinf = qstate->qinfo;
366 		qinf.qname = memdup(qstate->qinfo.qname,
367 			qstate->qinfo.qname_len);
368 		if(!qinf.qname) {
369 			free(data);
370 			log_err("memdup failed");
371 			return;
372 		}
373 		mrep_entry = query_info_entrysetup(&qinf, data, h);
374 		free(qinf.qname); /* if qname 'consumed', it is set to NULL */
375 		if (!mrep_entry) {
376 			free(data);
377 			log_err("query_info_entrysetup failed");
378 			return;
379 		}
380 		lru_entry = &mrep_entry->entry;
381 		lock_rw_wrlock(&lru_entry->lock);
382 	}
383 	/* lru_entry->lock is locked regardless of how we got here,
384 	 * either from the slabhash_lookup, or above in the new allocated */
385 	/* Step 2, find the correct tree */
386 	if (!(tree = get_tree(lru_entry->data, edns, sne, qstate->env->cfg))) {
387 		lock_rw_unlock(&lru_entry->lock);
388 		log_err("subnetcache: cache insertion failed");
389 		return;
390 	}
391 	lock_quick_lock(&sne->alloc.lock);
392 	rep = reply_info_copy(qstate->return_msg->rep, &sne->alloc, NULL);
393 	lock_quick_unlock(&sne->alloc.lock);
394 	if (!rep) {
395 		lock_rw_unlock(&lru_entry->lock);
396 		log_err("subnetcache: cache insertion failed");
397 		return;
398 	}
399 
400 	/* store RRsets */
401 	for(i=0; i<rep->rrset_count; i++) {
402 		rep->ref[i].key = rep->rrsets[i];
403 		rep->ref[i].id = rep->rrsets[i]->id;
404 	}
405 	reply_info_set_ttls(rep, *qstate->env->now);
406 	rep->flags |= (BIT_RA | BIT_QR); /* fix flags to be sensible for */
407 	rep->flags &= ~(BIT_AA | BIT_CD);/* a reply based on the cache   */
408 	if(edns->subnet_source_mask == 0 && edns->subnet_scope_mask == 0)
409 		only_match_scope_zero = 1;
410 	else only_match_scope_zero = 0;
411 	addrtree_insert(tree, (addrkey_t*)edns->subnet_addr,
412 		edns->subnet_source_mask, sq->max_scope, rep,
413 		rep->ttl, *qstate->env->now, only_match_scope_zero);
414 
415 	lock_rw_unlock(&lru_entry->lock);
416 	if (need_to_insert) {
417 		slabhash_insert(subnet_msg_cache, h, lru_entry, lru_entry->data,
418 			NULL);
419 	}
420 }
421 
422 /** Lookup in cache and reply true iff reply is sent. */
423 static int
424 lookup_and_reply(struct module_qstate *qstate, int id, struct subnet_qstate *sq)
425 {
426 	struct lruhash_entry *e;
427 	struct module_env *env = qstate->env;
428 	struct subnet_env *sne = (struct subnet_env*)env->modinfo[id];
429 	hashvalue_type h = query_info_hash(&qstate->qinfo, qstate->query_flags);
430 	struct subnet_msg_cache_data *data;
431 	struct ecs_data *ecs = &sq->ecs_client_in;
432 	struct addrtree *tree;
433 	struct addrnode *node;
434 	uint8_t scope;
435 
436 	memset(&sq->ecs_client_out, 0, sizeof(sq->ecs_client_out));
437 
438 	if (sq) {
439 		sq->qinfo_hash = h; /* Might be useful on cache miss */
440 		sq->qinfo_hash_calculated = 1;
441 	}
442 	e = slabhash_lookup(sne->subnet_msg_cache, h, &qstate->qinfo, 1);
443 	if (!e) return 0; /* qinfo not in cache */
444 	data = e->data;
445 	tree = (ecs->subnet_addr_fam == EDNSSUBNET_ADDRFAM_IP4)?
446 		data->tree4 : data->tree6;
447 	if (!tree) { /* qinfo in cache but not for this family */
448 		lock_rw_unlock(&e->lock);
449 		return 0;
450 	}
451 	node = addrtree_find(tree, (addrkey_t*)ecs->subnet_addr,
452 		ecs->subnet_source_mask, *env->now);
453 	if (!node) { /* plain old cache miss */
454 		lock_rw_unlock(&e->lock);
455 		return 0;
456 	}
457 
458 	qstate->return_msg = tomsg(NULL, &qstate->qinfo,
459 		(struct reply_info *)node->elem, qstate->region, *env->now, 0,
460 		env->scratch);
461 	scope = (uint8_t)node->scope;
462 	lock_rw_unlock(&e->lock);
463 
464 	if (!qstate->return_msg) { /* Failed allocation or expired TTL */
465 		return 0;
466 	}
467 
468 	if (sq->subnet_downstream) { /* relay to interested client */
469 		sq->ecs_client_out.subnet_scope_mask = scope;
470 		sq->ecs_client_out.subnet_addr_fam = ecs->subnet_addr_fam;
471 		sq->ecs_client_out.subnet_source_mask = ecs->subnet_source_mask;
472 		memcpy(&sq->ecs_client_out.subnet_addr, &ecs->subnet_addr,
473 			INET6_SIZE);
474 		sq->ecs_client_out.subnet_validdata = 1;
475 	}
476 	return 1;
477 }
478 
479 /**
480  * Test first bits of addresses for equality. Caller is responsible
481  * for making sure that both a and b are at least net/8 octets long.
482  * @param a: first address.
483  * @param a: seconds address.
484  * @param net: Number of bits to test.
485  * @return: 1 if equal, 0 otherwise.
486  */
487 static int
488 common_prefix(uint8_t *a, uint8_t *b, uint8_t net)
489 {
490 	size_t n = (size_t)net / 8;
491 	return !memcmp(a, b, n) && ((net % 8) == 0 || a[n] == b[n]);
492 }
493 
494 static enum module_ext_state
495 eval_response(struct module_qstate *qstate, int id, struct subnet_qstate *sq)
496 {
497 	struct subnet_env *sne = qstate->env->modinfo[id];
498 
499 	struct ecs_data *c_in  = &sq->ecs_client_in; /* rcvd from client */
500 	struct ecs_data *c_out = &sq->ecs_client_out;/* will send to client */
501 	struct ecs_data *s_in  = &sq->ecs_server_in; /* rcvd from auth */
502 	struct ecs_data *s_out = &sq->ecs_server_out;/* sent to auth */
503 
504 	memset(c_out, 0, sizeof(*c_out));
505 
506 	if (!qstate->return_msg) {
507 		/* already an answer and its not a message, but retain
508 		 * the actual rcode, instead of module_error, so send
509 		 * module_finished */
510 		return module_finished;
511 	}
512 
513 	/* We have not asked for subnet data */
514 	if (!sq->subnet_sent) {
515 		if (s_in->subnet_validdata)
516 			verbose(VERB_QUERY, "subnetcache: received spurious data");
517 		if (sq->subnet_downstream) /* Copy back to client */
518 			cp_edns_bad_response(c_out, c_in);
519 		return module_finished;
520 	}
521 
522 	/* subnet sent but nothing came back */
523 	if (!s_in->subnet_validdata) {
524 		/* The authority indicated no support for edns subnet. As a
525 		 * consequence the answer ended up in the regular cache. It
526 		 * is still useful to put it in the edns subnet cache for
527 		 * when a client explicitly asks for subnet specific answer. */
528 		verbose(VERB_QUERY, "subnetcache: Authority indicates no support");
529 		if(!sq->started_no_cache_store) {
530 			lock_rw_wrlock(&sne->biglock);
531 			update_cache(qstate, id);
532 			lock_rw_unlock(&sne->biglock);
533 		}
534 		if (sq->subnet_downstream)
535 			cp_edns_bad_response(c_out, c_in);
536 		return module_finished;
537 	}
538 
539 	/* Being here means we have asked for and got a subnet specific
540 	 * answer. Also, the answer from the authority is not yet cached
541 	 * anywhere. */
542 
543 	/* can we accept response? */
544 	if(s_out->subnet_addr_fam != s_in->subnet_addr_fam ||
545 		s_out->subnet_source_mask != s_in->subnet_source_mask ||
546 		!common_prefix(s_out->subnet_addr, s_in->subnet_addr,
547 			s_out->subnet_source_mask))
548 	{
549 		/* we can not accept, restart query without option */
550 		verbose(VERB_QUERY, "subnetcache: forged data");
551 		s_out->subnet_validdata = 0;
552 		(void)edns_opt_list_remove(&qstate->edns_opts_back_out,
553 			qstate->env->cfg->client_subnet_opcode);
554 		sq->subnet_sent = 0;
555 		return module_restart_next;
556 	}
557 
558 	lock_rw_wrlock(&sne->biglock);
559 	if(!sq->started_no_cache_store) {
560 		update_cache(qstate, id);
561 	}
562 	sne->num_msg_nocache++;
563 	lock_rw_unlock(&sne->biglock);
564 
565 	if (sq->subnet_downstream) {
566 		/* Client wants to see the answer, echo option back
567 		 * and adjust the scope. */
568 		c_out->subnet_addr_fam = c_in->subnet_addr_fam;
569 		c_out->subnet_source_mask = c_in->subnet_source_mask;
570 		memcpy(&c_out->subnet_addr, &c_in->subnet_addr, INET6_SIZE);
571 		c_out->subnet_scope_mask = sq->max_scope;
572 		/* Limit scope returned to client to scope used for caching. */
573 		if(c_out->subnet_addr_fam == EDNSSUBNET_ADDRFAM_IP4) {
574 			if(c_out->subnet_scope_mask >
575 				qstate->env->cfg->max_client_subnet_ipv4) {
576 				c_out->subnet_scope_mask =
577 					qstate->env->cfg->max_client_subnet_ipv4;
578 			}
579 		}
580 		else if(c_out->subnet_scope_mask >
581 				qstate->env->cfg->max_client_subnet_ipv6) {
582 				c_out->subnet_scope_mask =
583 					qstate->env->cfg->max_client_subnet_ipv6;
584 		}
585 		c_out->subnet_validdata = 1;
586 	}
587 	return module_finished;
588 }
589 
590 /** Parse EDNS opt data containing ECS */
591 static int
592 parse_subnet_option(struct edns_option* ecs_option, struct ecs_data* ecs)
593 {
594 	memset(ecs, 0, sizeof(*ecs));
595 	if (ecs_option->opt_len < 4)
596 		return 0;
597 
598 	ecs->subnet_addr_fam = sldns_read_uint16(ecs_option->opt_data);
599 	ecs->subnet_source_mask = ecs_option->opt_data[2];
600 	ecs->subnet_scope_mask = ecs_option->opt_data[3];
601 	/* remaining bytes indicate address */
602 
603 	/* validate input*/
604 	/* option length matches calculated length? */
605 	if (ecs_option->opt_len != (size_t)((ecs->subnet_source_mask+7)/8 + 4))
606 		return 0;
607 	if (ecs_option->opt_len - 4 > INET6_SIZE || ecs_option->opt_len == 0)
608 		return 0;
609 	if (ecs->subnet_addr_fam == EDNSSUBNET_ADDRFAM_IP4) {
610 		if (ecs->subnet_source_mask > 32 || ecs->subnet_scope_mask > 32)
611 			return 0;
612 	} else if (ecs->subnet_addr_fam == EDNSSUBNET_ADDRFAM_IP6) {
613 		if (ecs->subnet_source_mask > 128 ||
614 			ecs->subnet_scope_mask > 128)
615 			return 0;
616 	} else
617 		return 0;
618 
619 	/* valid ECS data, write to ecs_data */
620 	if (copy_clear(ecs->subnet_addr, INET6_SIZE, ecs_option->opt_data + 4,
621 		ecs_option->opt_len - 4, ecs->subnet_source_mask))
622 		return 0;
623 	ecs->subnet_validdata = 1;
624 	return 1;
625 }
626 
627 void
628 subnet_option_from_ss(struct sockaddr_storage *ss, struct ecs_data* ecs,
629 	struct config_file* cfg)
630 {
631 	void* sinaddr;
632 
633 	/* Construct subnet option from original query */
634 	if(((struct sockaddr_in*)ss)->sin_family == AF_INET) {
635 		ecs->subnet_source_mask = cfg->max_client_subnet_ipv4;
636 		ecs->subnet_addr_fam = EDNSSUBNET_ADDRFAM_IP4;
637 		sinaddr = &((struct sockaddr_in*)ss)->sin_addr;
638 		if (!copy_clear( ecs->subnet_addr, INET6_SIZE,
639 			(uint8_t *)sinaddr, INET_SIZE,
640 			ecs->subnet_source_mask)) {
641 			ecs->subnet_validdata = 1;
642 		}
643 	}
644 #ifdef INET6
645 	else {
646 		ecs->subnet_source_mask = cfg->max_client_subnet_ipv6;
647 		ecs->subnet_addr_fam = EDNSSUBNET_ADDRFAM_IP6;
648 		sinaddr = &((struct sockaddr_in6*)ss)->sin6_addr;
649 		if (!copy_clear( ecs->subnet_addr, INET6_SIZE,
650 			(uint8_t *)sinaddr, INET6_SIZE,
651 			ecs->subnet_source_mask)) {
652 			ecs->subnet_validdata = 1;
653 		}
654 	}
655 #else
656 			/* We don't know how to handle ip6, just pass */
657 #endif /* INET6 */
658 }
659 
660 int
661 ecs_query_response(struct module_qstate* qstate, struct dns_msg* response,
662 	int id, void* ATTR_UNUSED(cbargs))
663 {
664 	struct subnet_qstate *sq;
665 
666 	if(!response || !(sq=(struct subnet_qstate*)qstate->minfo[id]))
667 		return 1;
668 
669 	if(sq->subnet_sent &&
670 		FLAGS_GET_RCODE(response->rep->flags) == LDNS_RCODE_REFUSED) {
671 		/* REFUSED response to ECS query, remove ECS option. */
672 		edns_opt_list_remove(&qstate->edns_opts_back_out,
673 			qstate->env->cfg->client_subnet_opcode);
674 		sq->subnet_sent = 0;
675 		memset(&sq->ecs_server_out, 0, sizeof(sq->ecs_server_out));
676 	} else if (!sq->track_max_scope &&
677 		FLAGS_GET_RCODE(response->rep->flags) == LDNS_RCODE_NOERROR &&
678 		response->rep->an_numrrsets > 0
679 		) {
680 		struct ub_packed_rrset_key* s = response->rep->rrsets[0];
681 		if(ntohs(s->rk.type) == LDNS_RR_TYPE_CNAME &&
682 			query_dname_compare(qstate->qinfo.qname,
683 			s->rk.dname) == 0) {
684 			/* CNAME response for QNAME. From now on keep track of
685 			 * longest received ECS prefix for all queries on this
686 			 * qstate. */
687 			sq->track_max_scope = 1;
688 		}
689 	}
690 	return 1;
691 }
692 
693 /** verbose print edns subnet option in pretty print */
694 static void
695 subnet_log_print(const char* s, struct edns_option* ecs_opt)
696 {
697 	if(verbosity >= VERB_ALGO) {
698 		char buf[256];
699 		char* str = buf;
700 		size_t str_len = sizeof(buf);
701 		if(!ecs_opt) {
702 			verbose(VERB_ALGO, "%s (null)", s);
703 			return;
704 		}
705 		(void)sldns_wire2str_edns_subnet_print(&str, &str_len,
706 			ecs_opt->opt_data, ecs_opt->opt_len);
707 		verbose(VERB_ALGO, "%s %s", s, buf);
708 	}
709 }
710 
711 int
712 ecs_edns_back_parsed(struct module_qstate* qstate, int id,
713 	void* ATTR_UNUSED(cbargs))
714 {
715 	struct subnet_qstate *sq;
716 	struct edns_option* ecs_opt;
717 
718 	if(!(sq=(struct subnet_qstate*)qstate->minfo[id]))
719 		return 1;
720 	if((ecs_opt = edns_opt_list_find(
721 		qstate->edns_opts_back_in,
722 		qstate->env->cfg->client_subnet_opcode)) &&
723 		parse_subnet_option(ecs_opt, &sq->ecs_server_in) &&
724 		sq->subnet_sent && sq->ecs_server_in.subnet_validdata) {
725 			subnet_log_print("answer has edns subnet", ecs_opt);
726 			/* Only skip global cache store if we sent an ECS option
727 			 * and received one back. Answers from non-whitelisted
728 			 * servers will end up in global cache. Answers for
729 			 * queries with 0 source will not (unless nameserver
730 			 * does not support ECS). */
731 			qstate->no_cache_store = 1;
732 			if(!sq->track_max_scope || (sq->track_max_scope &&
733 				sq->ecs_server_in.subnet_scope_mask >
734 				sq->max_scope))
735 				sq->max_scope = sq->ecs_server_in.subnet_scope_mask;
736 	}
737 
738 	return 1;
739 }
740 
741 void
742 subnetmod_operate(struct module_qstate *qstate, enum module_ev event,
743 	int id, struct outbound_entry* outbound)
744 {
745 	struct subnet_env *sne = qstate->env->modinfo[id];
746 	struct subnet_qstate *sq = (struct subnet_qstate*)qstate->minfo[id];
747 
748 	verbose(VERB_QUERY, "subnetcache[module %d] operate: extstate:%s "
749 		"event:%s", id, strextstate(qstate->ext_state[id]),
750 		strmodulevent(event));
751 	log_query_info(VERB_QUERY, "subnetcache operate: query", &qstate->qinfo);
752 
753 	if((event == module_event_new || event == module_event_pass) &&
754 		sq == NULL) {
755 		struct edns_option* ecs_opt;
756 		if(!subnet_new_qstate(qstate, id)) {
757 			qstate->return_msg = NULL;
758 			qstate->ext_state[id] = module_finished;
759 			return;
760 		}
761 
762 		sq = (struct subnet_qstate*)qstate->minfo[id];
763 
764 		if((ecs_opt = edns_opt_list_find(
765 			qstate->edns_opts_front_in,
766 			qstate->env->cfg->client_subnet_opcode))) {
767 			if(!parse_subnet_option(ecs_opt, &sq->ecs_client_in)) {
768 				/* Wrongly formatted ECS option. RFC mandates to
769 				 * return FORMERROR. */
770 				qstate->return_rcode = LDNS_RCODE_FORMERR;
771 				qstate->ext_state[id] = module_finished;
772 				return;
773 			}
774 			subnet_log_print("query has edns subnet", ecs_opt);
775 			sq->subnet_downstream = 1;
776 		}
777 		else if(qstate->mesh_info->reply_list) {
778 			subnet_option_from_ss(
779 				&qstate->mesh_info->reply_list->query_reply.client_addr,
780 				&sq->ecs_client_in, qstate->env->cfg);
781 		}
782 
783 		if(sq->ecs_client_in.subnet_validdata == 0) {
784 			/* No clients are interested in result or we could not
785 			 * parse it, we don't do client subnet */
786 			sq->ecs_server_out.subnet_validdata = 0;
787 			verbose(VERB_ALGO, "subnetcache: pass to next module");
788 			qstate->ext_state[id] = module_wait_module;
789 			return;
790 		}
791 
792 		/* Limit to minimum allowed source mask */
793 		if(sq->ecs_client_in.subnet_source_mask != 0 && (
794 			(sq->ecs_client_in.subnet_addr_fam == EDNSSUBNET_ADDRFAM_IP4 &&
795 			 sq->ecs_client_in.subnet_source_mask < qstate->env->cfg->min_client_subnet_ipv4) ||
796 			(sq->ecs_client_in.subnet_addr_fam == EDNSSUBNET_ADDRFAM_IP6 &&
797 			 sq->ecs_client_in.subnet_source_mask < qstate->env->cfg->min_client_subnet_ipv6))) {
798 				qstate->return_rcode = LDNS_RCODE_REFUSED;
799 				qstate->ext_state[id] = module_finished;
800 				return;
801 		}
802 
803 		if(!sq->started_no_cache_lookup && !qstate->blacklist) {
804 			lock_rw_wrlock(&sne->biglock);
805 			if(lookup_and_reply(qstate, id, sq)) {
806 				sne->num_msg_cache++;
807 				lock_rw_unlock(&sne->biglock);
808 				verbose(VERB_QUERY, "subnetcache: answered from cache");
809 				qstate->ext_state[id] = module_finished;
810 
811 				subnet_ecs_opt_list_append(&sq->ecs_client_out,
812 					&qstate->edns_opts_front_out, qstate,
813 					qstate->region);
814 				if(verbosity >= VERB_ALGO) {
815 					subnet_log_print("reply has edns subnet",
816 						edns_opt_list_find(
817 						qstate->edns_opts_front_out,
818 						qstate->env->cfg->
819 						client_subnet_opcode));
820 				}
821 				return;
822 			}
823 			lock_rw_unlock(&sne->biglock);
824 		}
825 
826 		sq->ecs_server_out.subnet_addr_fam =
827 			sq->ecs_client_in.subnet_addr_fam;
828 		sq->ecs_server_out.subnet_source_mask =
829 			sq->ecs_client_in.subnet_source_mask;
830 		/* Limit source prefix to configured maximum */
831 		if(sq->ecs_server_out.subnet_addr_fam == EDNSSUBNET_ADDRFAM_IP4
832 			&& sq->ecs_server_out.subnet_source_mask >
833 			qstate->env->cfg->max_client_subnet_ipv4)
834 			sq->ecs_server_out.subnet_source_mask =
835 				qstate->env->cfg->max_client_subnet_ipv4;
836 		else if(sq->ecs_server_out.subnet_addr_fam == EDNSSUBNET_ADDRFAM_IP6
837 			&& sq->ecs_server_out.subnet_source_mask >
838 			qstate->env->cfg->max_client_subnet_ipv6)
839 			sq->ecs_server_out.subnet_source_mask =
840 				qstate->env->cfg->max_client_subnet_ipv6;
841 		/* Safe to copy completely, even if the source is limited by the
842 		 * configuration. subnet_ecs_opt_list_append() will limit the address.
843 		 * */
844 		memcpy(&sq->ecs_server_out.subnet_addr,
845 			sq->ecs_client_in.subnet_addr, INET6_SIZE);
846 		sq->ecs_server_out.subnet_scope_mask = 0;
847 		sq->ecs_server_out.subnet_validdata = 1;
848 		if(sq->ecs_server_out.subnet_source_mask != 0 &&
849 			qstate->env->cfg->client_subnet_always_forward &&
850 			sq->subnet_downstream)
851 			/* ECS specific data required, do not look at the global
852 			 * cache in other modules. */
853 			qstate->no_cache_lookup = 1;
854 
855 		/* pass request to next module */
856 		verbose(VERB_ALGO,
857 			"subnetcache: not found in cache. pass to next module");
858 		qstate->ext_state[id] = module_wait_module;
859 		return;
860 	}
861 	/* Query handed back by next module, we have a 'final' answer */
862 	if(sq && event == module_event_moddone) {
863 		qstate->ext_state[id] = eval_response(qstate, id, sq);
864 		if(qstate->ext_state[id] == module_finished &&
865 			qstate->return_msg) {
866 			subnet_ecs_opt_list_append(&sq->ecs_client_out,
867 				&qstate->edns_opts_front_out, qstate,
868 				qstate->region);
869 			if(verbosity >= VERB_ALGO) {
870 				subnet_log_print("reply has edns subnet",
871 					edns_opt_list_find(
872 					qstate->edns_opts_front_out,
873 					qstate->env->cfg->
874 					client_subnet_opcode));
875 			}
876 		}
877 		qstate->no_cache_store = sq->started_no_cache_store;
878 		qstate->no_cache_lookup = sq->started_no_cache_lookup;
879 		return;
880 	}
881 	if(sq && outbound) {
882 		return;
883 	}
884 	/* We are being revisited */
885 	if(event == module_event_pass || event == module_event_new) {
886 		/* Just pass it on, we already did the work */
887 		verbose(VERB_ALGO, "subnetcache: pass to next module");
888 		qstate->ext_state[id] = module_wait_module;
889 		return;
890 	}
891 	if(!sq && (event == module_event_moddone)) {
892 		/* during priming, module done but we never started */
893 		qstate->ext_state[id] = module_finished;
894 		return;
895 	}
896 	log_err("subnetcache: bad event %s", strmodulevent(event));
897 	qstate->ext_state[id] = module_error;
898 	return;
899 }
900 
901 void
902 subnetmod_clear(struct module_qstate *ATTR_UNUSED(qstate),
903 	int ATTR_UNUSED(id))
904 {
905 	/* qstate has no data outside region */
906 }
907 
908 void
909 subnetmod_inform_super(struct module_qstate *ATTR_UNUSED(qstate),
910 	int ATTR_UNUSED(id), struct module_qstate *ATTR_UNUSED(super))
911 {
912 	/* Not used */
913 }
914 
915 size_t
916 subnetmod_get_mem(struct module_env *env, int id)
917 {
918 	struct subnet_env *sn_env = env->modinfo[id];
919 	if (!sn_env) return 0;
920 	return sizeof(*sn_env) +
921 		slabhash_get_mem(sn_env->subnet_msg_cache) +
922 		ecs_whitelist_get_mem(sn_env->whitelist);
923 }
924 
925 /**
926  * The module function block
927  */
928 static struct module_func_block subnetmod_block = {
929 	"subnetcache", &subnetmod_init, &subnetmod_deinit, &subnetmod_operate,
930 	&subnetmod_inform_super, &subnetmod_clear, &subnetmod_get_mem
931 };
932 
933 struct module_func_block*
934 subnetmod_get_funcblock(void)
935 {
936 	return &subnetmod_block;
937 }
938 
939 /** Wrappers for static functions to unit test */
940 size_t
941 unittest_wrapper_subnetmod_sizefunc(void *elemptr)
942 {
943 	return sizefunc(elemptr);
944 }
945 
946 #endif  /* CLIENT_SUBNET */
947