xref: /freebsd/contrib/unbound/edns-subnet/subnetmod.c (revision 24e365220007c415f495cf8dcb228ece6002b8b7)
165b390aaSDag-Erling Smørgrav /*
265b390aaSDag-Erling Smørgrav  * edns-subnet/subnetmod.c - edns subnet module. Must be called before validator
365b390aaSDag-Erling Smørgrav  * and iterator.
465b390aaSDag-Erling Smørgrav  *
565b390aaSDag-Erling Smørgrav  * Copyright (c) 2013, NLnet Labs. All rights reserved.
665b390aaSDag-Erling Smørgrav  *
765b390aaSDag-Erling Smørgrav  * This software is open source.
865b390aaSDag-Erling Smørgrav  *
965b390aaSDag-Erling Smørgrav  * Redistribution and use in source and binary forms, with or without
1065b390aaSDag-Erling Smørgrav  * modification, are permitted provided that the following conditions
1165b390aaSDag-Erling Smørgrav  * are met:
1265b390aaSDag-Erling Smørgrav  *
1365b390aaSDag-Erling Smørgrav  * Redistributions of source code must retain the above copyright notice,
1465b390aaSDag-Erling Smørgrav  * this list of conditions and the following disclaimer.
1565b390aaSDag-Erling Smørgrav  *
1665b390aaSDag-Erling Smørgrav  * Redistributions in binary form must reproduce the above copyright notice,
1765b390aaSDag-Erling Smørgrav  * this list of conditions and the following disclaimer in the documentation
1865b390aaSDag-Erling Smørgrav  * and/or other materials provided with the distribution.
1965b390aaSDag-Erling Smørgrav  *
2065b390aaSDag-Erling Smørgrav  * Neither the name of the NLNET LABS nor the names of its contributors may
2165b390aaSDag-Erling Smørgrav  * be used to endorse or promote products derived from this software without
2265b390aaSDag-Erling Smørgrav  * specific prior written permission.
2365b390aaSDag-Erling Smørgrav  *
2465b390aaSDag-Erling Smørgrav  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
2565b390aaSDag-Erling Smørgrav  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
2665b390aaSDag-Erling Smørgrav  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
2765b390aaSDag-Erling Smørgrav  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
2865b390aaSDag-Erling Smørgrav  * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2965b390aaSDag-Erling Smørgrav  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
3065b390aaSDag-Erling Smørgrav  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
3165b390aaSDag-Erling Smørgrav  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
3265b390aaSDag-Erling Smørgrav  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
3365b390aaSDag-Erling Smørgrav  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
3465b390aaSDag-Erling Smørgrav  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3565b390aaSDag-Erling Smørgrav  */
3665b390aaSDag-Erling Smørgrav  /**
3765b390aaSDag-Erling Smørgrav  * \file
3865b390aaSDag-Erling Smørgrav  * subnet module for unbound.
3965b390aaSDag-Erling Smørgrav  */
4065b390aaSDag-Erling Smørgrav 
4165b390aaSDag-Erling Smørgrav #include "config.h"
4265b390aaSDag-Erling Smørgrav 
4365b390aaSDag-Erling Smørgrav #ifdef CLIENT_SUBNET /* keeps splint happy */
4465b390aaSDag-Erling Smørgrav 
4565b390aaSDag-Erling Smørgrav #include "edns-subnet/subnetmod.h"
4665b390aaSDag-Erling Smørgrav #include "edns-subnet/edns-subnet.h"
4765b390aaSDag-Erling Smørgrav #include "edns-subnet/addrtree.h"
4865b390aaSDag-Erling Smørgrav #include "edns-subnet/subnet-whitelist.h"
4965b390aaSDag-Erling Smørgrav 
5065b390aaSDag-Erling Smørgrav #include "services/mesh.h"
5165b390aaSDag-Erling Smørgrav #include "services/cache/dns.h"
5265b390aaSDag-Erling Smørgrav #include "util/module.h"
5365b390aaSDag-Erling Smørgrav #include "util/regional.h"
5465b390aaSDag-Erling Smørgrav #include "util/storage/slabhash.h"
5565b390aaSDag-Erling Smørgrav #include "util/config_file.h"
5665b390aaSDag-Erling Smørgrav #include "util/data/msgreply.h"
5765b390aaSDag-Erling Smørgrav #include "sldns/sbuffer.h"
58e86b9096SDag-Erling Smørgrav #include "iterator/iter_utils.h"
5965b390aaSDag-Erling Smørgrav 
6065b390aaSDag-Erling Smørgrav /** externally called */
6165b390aaSDag-Erling Smørgrav void
6265b390aaSDag-Erling Smørgrav subnet_data_delete(void *d, void *ATTR_UNUSED(arg))
6365b390aaSDag-Erling Smørgrav {
6465b390aaSDag-Erling Smørgrav 	struct subnet_msg_cache_data *r;
6565b390aaSDag-Erling Smørgrav 	r = (struct subnet_msg_cache_data*)d;
6665b390aaSDag-Erling Smørgrav 	addrtree_delete(r->tree4);
6765b390aaSDag-Erling Smørgrav 	addrtree_delete(r->tree6);
6865b390aaSDag-Erling Smørgrav 	free(r);
6965b390aaSDag-Erling Smørgrav }
7065b390aaSDag-Erling Smørgrav 
7165b390aaSDag-Erling Smørgrav /** externally called */
7265b390aaSDag-Erling Smørgrav size_t
7365b390aaSDag-Erling Smørgrav msg_cache_sizefunc(void *k, void *d)
7465b390aaSDag-Erling Smørgrav {
7565b390aaSDag-Erling Smørgrav 	struct msgreply_entry *q = (struct msgreply_entry*)k;
7665b390aaSDag-Erling Smørgrav 	struct subnet_msg_cache_data *r = (struct subnet_msg_cache_data*)d;
7765b390aaSDag-Erling Smørgrav 	size_t s = sizeof(struct msgreply_entry)
7865b390aaSDag-Erling Smørgrav 		+ sizeof(struct subnet_msg_cache_data)
7965b390aaSDag-Erling Smørgrav 		+ q->key.qname_len + lock_get_mem(&q->entry.lock);
8065b390aaSDag-Erling Smørgrav 	s += addrtree_size(r->tree4);
8165b390aaSDag-Erling Smørgrav 	s += addrtree_size(r->tree6);
8265b390aaSDag-Erling Smørgrav 	return s;
8365b390aaSDag-Erling Smørgrav }
8465b390aaSDag-Erling Smørgrav 
8565b390aaSDag-Erling Smørgrav /** new query for ecs module */
8665b390aaSDag-Erling Smørgrav static int
8765b390aaSDag-Erling Smørgrav subnet_new_qstate(struct module_qstate *qstate, int id)
8865b390aaSDag-Erling Smørgrav {
8965b390aaSDag-Erling Smørgrav 	struct subnet_qstate *sq = (struct subnet_qstate*)regional_alloc(
9065b390aaSDag-Erling Smørgrav 		qstate->region, sizeof(struct subnet_qstate));
9165b390aaSDag-Erling Smørgrav 	if(!sq)
9265b390aaSDag-Erling Smørgrav 		return 0;
9365b390aaSDag-Erling Smørgrav 	qstate->minfo[id] = sq;
9465b390aaSDag-Erling Smørgrav 	memset(sq, 0, sizeof(*sq));
95e86b9096SDag-Erling Smørgrav 	sq->started_no_cache_store = qstate->no_cache_store;
9665b390aaSDag-Erling Smørgrav 	return 1;
9765b390aaSDag-Erling Smørgrav }
9865b390aaSDag-Erling Smørgrav 
9965b390aaSDag-Erling Smørgrav /** Add ecs struct to edns list, after parsing it to wire format. */
10065b390aaSDag-Erling Smørgrav static void
10165b390aaSDag-Erling Smørgrav ecs_opt_list_append(struct ecs_data* ecs, struct edns_option** list,
10265b390aaSDag-Erling Smørgrav 	struct module_qstate *qstate)
10365b390aaSDag-Erling Smørgrav {
10465b390aaSDag-Erling Smørgrav 	size_t sn_octs, sn_octs_remainder;
10565b390aaSDag-Erling Smørgrav 	sldns_buffer* buf = qstate->env->scratch_buffer;
10665b390aaSDag-Erling Smørgrav 
10765b390aaSDag-Erling Smørgrav 	if(ecs->subnet_validdata) {
10865b390aaSDag-Erling Smørgrav 		log_assert(ecs->subnet_addr_fam == EDNSSUBNET_ADDRFAM_IP4 ||
10965b390aaSDag-Erling Smørgrav 			ecs->subnet_addr_fam == EDNSSUBNET_ADDRFAM_IP6);
11065b390aaSDag-Erling Smørgrav 		log_assert(ecs->subnet_addr_fam != EDNSSUBNET_ADDRFAM_IP4 ||
11165b390aaSDag-Erling Smørgrav 			ecs->subnet_source_mask <=  INET_SIZE*8);
11265b390aaSDag-Erling Smørgrav 		log_assert(ecs->subnet_addr_fam != EDNSSUBNET_ADDRFAM_IP6 ||
11365b390aaSDag-Erling Smørgrav 			ecs->subnet_source_mask <= INET6_SIZE*8);
11465b390aaSDag-Erling Smørgrav 
11565b390aaSDag-Erling Smørgrav 		sn_octs = ecs->subnet_source_mask / 8;
11665b390aaSDag-Erling Smørgrav 		sn_octs_remainder =
11765b390aaSDag-Erling Smørgrav 			(size_t)((ecs->subnet_source_mask % 8)>0?1:0);
11865b390aaSDag-Erling Smørgrav 
11965b390aaSDag-Erling Smørgrav 		log_assert(sn_octs + sn_octs_remainder <= INET6_SIZE);
12065b390aaSDag-Erling Smørgrav 
12165b390aaSDag-Erling Smørgrav 		sldns_buffer_clear(buf);
12265b390aaSDag-Erling Smørgrav 		sldns_buffer_write_u16(buf, ecs->subnet_addr_fam);
12365b390aaSDag-Erling Smørgrav 		sldns_buffer_write_u8(buf, ecs->subnet_source_mask);
12465b390aaSDag-Erling Smørgrav 		sldns_buffer_write_u8(buf, ecs->subnet_scope_mask);
12565b390aaSDag-Erling Smørgrav 		sldns_buffer_write(buf, ecs->subnet_addr, sn_octs);
12665b390aaSDag-Erling Smørgrav 		if(sn_octs_remainder)
12765b390aaSDag-Erling Smørgrav 			sldns_buffer_write_u8(buf, ecs->subnet_addr[sn_octs] &
12865b390aaSDag-Erling Smørgrav 				~(0xFF >> (ecs->subnet_source_mask % 8)));
12965b390aaSDag-Erling Smørgrav 		sldns_buffer_flip(buf);
13065b390aaSDag-Erling Smørgrav 
13165b390aaSDag-Erling Smørgrav 		edns_opt_list_append(list,
13265b390aaSDag-Erling Smørgrav 				qstate->env->cfg->client_subnet_opcode,
13365b390aaSDag-Erling Smørgrav 				sn_octs + sn_octs_remainder + 4,
13465b390aaSDag-Erling Smørgrav 				sldns_buffer_begin(buf), qstate->region);
13565b390aaSDag-Erling Smørgrav 	}
13665b390aaSDag-Erling Smørgrav }
13765b390aaSDag-Erling Smørgrav 
138c7f4d7adSDag-Erling Smørgrav int ecs_whitelist_check(struct query_info* qinfo,
13965b390aaSDag-Erling Smørgrav 	uint16_t ATTR_UNUSED(flags), struct module_qstate* qstate,
14065b390aaSDag-Erling Smørgrav 	struct sockaddr_storage* addr, socklen_t addrlen,
14165b390aaSDag-Erling Smørgrav 	uint8_t* ATTR_UNUSED(zone), size_t ATTR_UNUSED(zonelen),
14265b390aaSDag-Erling Smørgrav 	struct regional* ATTR_UNUSED(region), int id, void* ATTR_UNUSED(cbargs))
14365b390aaSDag-Erling Smørgrav {
14465b390aaSDag-Erling Smørgrav 	struct subnet_qstate *sq;
14565b390aaSDag-Erling Smørgrav 	struct subnet_env *sn_env;
14665b390aaSDag-Erling Smørgrav 
14765b390aaSDag-Erling Smørgrav 	if(!(sq=(struct subnet_qstate*)qstate->minfo[id]))
14865b390aaSDag-Erling Smørgrav 		return 1;
14965b390aaSDag-Erling Smørgrav 	sn_env = (struct subnet_env*)qstate->env->modinfo[id];
15065b390aaSDag-Erling Smørgrav 
15165b390aaSDag-Erling Smørgrav 	/* Cache by default, might be disabled after parsing EDNS option
15265b390aaSDag-Erling Smørgrav 	 * received from nameserver. */
1535469a995SCy Schubert 	if(!iter_stub_fwd_no_cache(qstate, &qstate->qinfo, NULL, NULL)) {
15465b390aaSDag-Erling Smørgrav 		qstate->no_cache_store = 0;
155e86b9096SDag-Erling Smørgrav 	}
15665b390aaSDag-Erling Smørgrav 
15765b390aaSDag-Erling Smørgrav 	if(sq->ecs_server_out.subnet_validdata && ((sq->subnet_downstream &&
15865b390aaSDag-Erling Smørgrav 		qstate->env->cfg->client_subnet_always_forward) ||
159c7f4d7adSDag-Erling Smørgrav 		ecs_is_whitelisted(sn_env->whitelist,
160c7f4d7adSDag-Erling Smørgrav 		addr, addrlen, qinfo->qname, qinfo->qname_len,
161c7f4d7adSDag-Erling Smørgrav 		qinfo->qclass))) {
16265b390aaSDag-Erling Smørgrav 		/* Address on whitelist or client query contains ECS option, we
16365b390aaSDag-Erling Smørgrav 		 * want to sent out ECS. Only add option if it is not already
16465b390aaSDag-Erling Smørgrav 		 * set. */
16565b390aaSDag-Erling Smørgrav 		if(!(sq->subnet_sent)) {
16665b390aaSDag-Erling Smørgrav 			ecs_opt_list_append(&sq->ecs_server_out,
16765b390aaSDag-Erling Smørgrav 				&qstate->edns_opts_back_out, qstate);
16865b390aaSDag-Erling Smørgrav 			sq->subnet_sent = 1;
16965b390aaSDag-Erling Smørgrav 		}
17065b390aaSDag-Erling Smørgrav 	}
17165b390aaSDag-Erling Smørgrav 	else if(sq->subnet_sent) {
17265b390aaSDag-Erling Smørgrav 		/* Outgoing ECS option is set, but we don't want to sent it to
17365b390aaSDag-Erling Smørgrav 		 * this address, remove option. */
17465b390aaSDag-Erling Smørgrav 		edns_opt_list_remove(&qstate->edns_opts_back_out,
17565b390aaSDag-Erling Smørgrav 			qstate->env->cfg->client_subnet_opcode);
17665b390aaSDag-Erling Smørgrav 		sq->subnet_sent = 0;
17765b390aaSDag-Erling Smørgrav 	}
17865b390aaSDag-Erling Smørgrav 	return 1;
17965b390aaSDag-Erling Smørgrav }
18065b390aaSDag-Erling Smørgrav 
18165b390aaSDag-Erling Smørgrav 
182e86b9096SDag-Erling Smørgrav void
183e86b9096SDag-Erling Smørgrav subnet_markdel(void* key)
184e86b9096SDag-Erling Smørgrav {
185e86b9096SDag-Erling Smørgrav 	struct msgreply_entry *e = (struct msgreply_entry*)key;
186e86b9096SDag-Erling Smørgrav 	e->key.qtype = 0;
187e86b9096SDag-Erling Smørgrav 	e->key.qclass = 0;
188e86b9096SDag-Erling Smørgrav }
189e86b9096SDag-Erling Smørgrav 
19065b390aaSDag-Erling Smørgrav int
19165b390aaSDag-Erling Smørgrav subnetmod_init(struct module_env *env, int id)
19265b390aaSDag-Erling Smørgrav {
19365b390aaSDag-Erling Smørgrav 	struct subnet_env *sn_env = (struct subnet_env*)calloc(1,
19465b390aaSDag-Erling Smørgrav 		sizeof(struct subnet_env));
19565b390aaSDag-Erling Smørgrav 	if(!sn_env) {
19665b390aaSDag-Erling Smørgrav 		log_err("malloc failure");
19765b390aaSDag-Erling Smørgrav 		return 0;
19865b390aaSDag-Erling Smørgrav 	}
19965b390aaSDag-Erling Smørgrav 	alloc_init(&sn_env->alloc, NULL, 0);
20065b390aaSDag-Erling Smørgrav 	env->modinfo[id] = (void*)sn_env;
20165b390aaSDag-Erling Smørgrav 	/* Copy msg_cache settings */
20265b390aaSDag-Erling Smørgrav 	sn_env->subnet_msg_cache = slabhash_create(env->cfg->msg_cache_slabs,
20365b390aaSDag-Erling Smørgrav 		HASH_DEFAULT_STARTARRAY, env->cfg->msg_cache_size,
20465b390aaSDag-Erling Smørgrav 		msg_cache_sizefunc, query_info_compare, query_entry_delete,
20565b390aaSDag-Erling Smørgrav 		subnet_data_delete, NULL);
206e86b9096SDag-Erling Smørgrav 	slabhash_setmarkdel(sn_env->subnet_msg_cache, &subnet_markdel);
20765b390aaSDag-Erling Smørgrav 	if(!sn_env->subnet_msg_cache) {
2085469a995SCy Schubert 		log_err("subnetcache: could not create cache");
20965b390aaSDag-Erling Smørgrav 		free(sn_env);
21065b390aaSDag-Erling Smørgrav 		env->modinfo[id] = NULL;
21165b390aaSDag-Erling Smørgrav 		return 0;
21265b390aaSDag-Erling Smørgrav 	}
21365b390aaSDag-Erling Smørgrav 	/* whitelist for edns subnet capable servers */
214c7f4d7adSDag-Erling Smørgrav 	sn_env->whitelist = ecs_whitelist_create();
215c7f4d7adSDag-Erling Smørgrav 	if(!sn_env->whitelist ||
216c7f4d7adSDag-Erling Smørgrav 		!ecs_whitelist_apply_cfg(sn_env->whitelist, env->cfg)) {
2175469a995SCy Schubert 		log_err("subnetcache: could not create ECS whitelist");
21865b390aaSDag-Erling Smørgrav 		slabhash_delete(sn_env->subnet_msg_cache);
21965b390aaSDag-Erling Smørgrav 		free(sn_env);
22065b390aaSDag-Erling Smørgrav 		env->modinfo[id] = NULL;
22165b390aaSDag-Erling Smørgrav 		return 0;
22265b390aaSDag-Erling Smørgrav 	}
22365b390aaSDag-Erling Smørgrav 
2245469a995SCy Schubert 	verbose(VERB_QUERY, "subnetcache: option registered (%d)",
22565b390aaSDag-Erling Smørgrav 		env->cfg->client_subnet_opcode);
22665b390aaSDag-Erling Smørgrav 	/* Create new mesh state for all queries. */
22765b390aaSDag-Erling Smørgrav 	env->unique_mesh = 1;
22865b390aaSDag-Erling Smørgrav 	if(!edns_register_option(env->cfg->client_subnet_opcode,
22965b390aaSDag-Erling Smørgrav 		env->cfg->client_subnet_always_forward /* bypass cache */,
23065b390aaSDag-Erling Smørgrav 		0 /* no aggregation */, env)) {
2315469a995SCy Schubert 		log_err("subnetcache: could not register opcode");
232c7f4d7adSDag-Erling Smørgrav 		ecs_whitelist_delete(sn_env->whitelist);
23365b390aaSDag-Erling Smørgrav 		slabhash_delete(sn_env->subnet_msg_cache);
23465b390aaSDag-Erling Smørgrav 		free(sn_env);
23565b390aaSDag-Erling Smørgrav 		env->modinfo[id] = NULL;
23665b390aaSDag-Erling Smørgrav 		return 0;
23765b390aaSDag-Erling Smørgrav 	}
23865b390aaSDag-Erling Smørgrav 	inplace_cb_register((void*)ecs_whitelist_check, inplace_cb_query, NULL,
23965b390aaSDag-Erling Smørgrav 		env, id);
24065b390aaSDag-Erling Smørgrav 	inplace_cb_register((void*)ecs_edns_back_parsed,
24165b390aaSDag-Erling Smørgrav 		inplace_cb_edns_back_parsed, NULL, env, id);
24265b390aaSDag-Erling Smørgrav 	inplace_cb_register((void*)ecs_query_response,
24365b390aaSDag-Erling Smørgrav 		inplace_cb_query_response, NULL, env, id);
24465b390aaSDag-Erling Smørgrav 	lock_rw_init(&sn_env->biglock);
24565b390aaSDag-Erling Smørgrav 	return 1;
24665b390aaSDag-Erling Smørgrav }
24765b390aaSDag-Erling Smørgrav 
24865b390aaSDag-Erling Smørgrav void
24965b390aaSDag-Erling Smørgrav subnetmod_deinit(struct module_env *env, int id)
25065b390aaSDag-Erling Smørgrav {
25165b390aaSDag-Erling Smørgrav 	struct subnet_env *sn_env;
25265b390aaSDag-Erling Smørgrav 	if(!env || !env->modinfo[id])
25365b390aaSDag-Erling Smørgrav 		return;
25465b390aaSDag-Erling Smørgrav 	sn_env = (struct subnet_env*)env->modinfo[id];
25565b390aaSDag-Erling Smørgrav 	lock_rw_destroy(&sn_env->biglock);
25665b390aaSDag-Erling Smørgrav 	inplace_cb_delete(env, inplace_cb_edns_back_parsed, id);
25765b390aaSDag-Erling Smørgrav 	inplace_cb_delete(env, inplace_cb_query, id);
258c7f4d7adSDag-Erling Smørgrav 	inplace_cb_delete(env, inplace_cb_query_response, id);
259c7f4d7adSDag-Erling Smørgrav 	ecs_whitelist_delete(sn_env->whitelist);
26065b390aaSDag-Erling Smørgrav 	slabhash_delete(sn_env->subnet_msg_cache);
26165b390aaSDag-Erling Smørgrav 	alloc_clear(&sn_env->alloc);
26265b390aaSDag-Erling Smørgrav 	free(sn_env);
26365b390aaSDag-Erling Smørgrav 	env->modinfo[id] = NULL;
26465b390aaSDag-Erling Smørgrav }
26565b390aaSDag-Erling Smørgrav 
26665b390aaSDag-Erling Smørgrav /** Tells client that upstream has no/improper support */
26765b390aaSDag-Erling Smørgrav static void
26865b390aaSDag-Erling Smørgrav cp_edns_bad_response(struct ecs_data *target, struct ecs_data *source)
26965b390aaSDag-Erling Smørgrav {
27065b390aaSDag-Erling Smørgrav 	target->subnet_scope_mask  = 0;
27165b390aaSDag-Erling Smørgrav 	target->subnet_source_mask = source->subnet_source_mask;
27265b390aaSDag-Erling Smørgrav 	target->subnet_addr_fam    = source->subnet_addr_fam;
27365b390aaSDag-Erling Smørgrav 	memcpy(target->subnet_addr, source->subnet_addr, INET6_SIZE);
27465b390aaSDag-Erling Smørgrav 	target->subnet_validdata = 1;
27565b390aaSDag-Erling Smørgrav }
27665b390aaSDag-Erling Smørgrav 
27765b390aaSDag-Erling Smørgrav static void
27865b390aaSDag-Erling Smørgrav delfunc(void *envptr, void *elemptr) {
27965b390aaSDag-Erling Smørgrav 	struct reply_info *elem = (struct reply_info *)elemptr;
28065b390aaSDag-Erling Smørgrav 	struct subnet_env *env = (struct subnet_env *)envptr;
28165b390aaSDag-Erling Smørgrav 	reply_info_parsedelete(elem, &env->alloc);
28265b390aaSDag-Erling Smørgrav }
28365b390aaSDag-Erling Smørgrav 
28465b390aaSDag-Erling Smørgrav static size_t
28565b390aaSDag-Erling Smørgrav sizefunc(void *elemptr) {
28665b390aaSDag-Erling Smørgrav 	struct reply_info *elem  = (struct reply_info *)elemptr;
28765b390aaSDag-Erling Smørgrav 	return sizeof (struct reply_info) - sizeof (struct rrset_ref)
28865b390aaSDag-Erling Smørgrav 		+ elem->rrset_count * sizeof (struct rrset_ref)
28965b390aaSDag-Erling Smørgrav 		+ elem->rrset_count * sizeof (struct ub_packed_rrset_key *);
29065b390aaSDag-Erling Smørgrav }
29165b390aaSDag-Erling Smørgrav 
29265b390aaSDag-Erling Smørgrav /**
29365b390aaSDag-Erling Smørgrav  * Select tree from cache entry based on edns data.
29465b390aaSDag-Erling Smørgrav  * If for address family not present it will create a new one.
29565b390aaSDag-Erling Smørgrav  * NULL on failure to create. */
29665b390aaSDag-Erling Smørgrav static struct addrtree*
29765b390aaSDag-Erling Smørgrav get_tree(struct subnet_msg_cache_data *data, struct ecs_data *edns,
29865b390aaSDag-Erling Smørgrav 	struct subnet_env *env, struct config_file* cfg)
29965b390aaSDag-Erling Smørgrav {
30065b390aaSDag-Erling Smørgrav 	struct addrtree *tree;
30165b390aaSDag-Erling Smørgrav 	if (edns->subnet_addr_fam == EDNSSUBNET_ADDRFAM_IP4) {
30265b390aaSDag-Erling Smørgrav 		if (!data->tree4)
30365b390aaSDag-Erling Smørgrav 			data->tree4 = addrtree_create(
30465b390aaSDag-Erling Smørgrav 				cfg->max_client_subnet_ipv4, &delfunc,
305e86b9096SDag-Erling Smørgrav 				&sizefunc, env, cfg->max_ecs_tree_size_ipv4);
30665b390aaSDag-Erling Smørgrav 		tree = data->tree4;
30765b390aaSDag-Erling Smørgrav 	} else {
30865b390aaSDag-Erling Smørgrav 		if (!data->tree6)
30965b390aaSDag-Erling Smørgrav 			data->tree6 = addrtree_create(
31065b390aaSDag-Erling Smørgrav 				cfg->max_client_subnet_ipv6, &delfunc,
311e86b9096SDag-Erling Smørgrav 				&sizefunc, env, cfg->max_ecs_tree_size_ipv6);
31265b390aaSDag-Erling Smørgrav 		tree = data->tree6;
31365b390aaSDag-Erling Smørgrav 	}
31465b390aaSDag-Erling Smørgrav 	return tree;
31565b390aaSDag-Erling Smørgrav }
31665b390aaSDag-Erling Smørgrav 
31765b390aaSDag-Erling Smørgrav static void
31865b390aaSDag-Erling Smørgrav update_cache(struct module_qstate *qstate, int id)
31965b390aaSDag-Erling Smørgrav {
32065b390aaSDag-Erling Smørgrav 	struct msgreply_entry *mrep_entry;
32165b390aaSDag-Erling Smørgrav 	struct addrtree *tree;
32265b390aaSDag-Erling Smørgrav 	struct reply_info *rep;
32365b390aaSDag-Erling Smørgrav 	struct query_info qinf;
32465b390aaSDag-Erling Smørgrav 	struct subnet_env *sne = qstate->env->modinfo[id];
32565b390aaSDag-Erling Smørgrav 	struct subnet_qstate *sq = (struct subnet_qstate*)qstate->minfo[id];
32665b390aaSDag-Erling Smørgrav 	struct slabhash *subnet_msg_cache = sne->subnet_msg_cache;
32765b390aaSDag-Erling Smørgrav 	struct ecs_data *edns = &sq->ecs_client_in;
32865b390aaSDag-Erling Smørgrav 	size_t i;
32965b390aaSDag-Erling Smørgrav 
33065b390aaSDag-Erling Smørgrav 	/* We already calculated hash upon lookup */
33165b390aaSDag-Erling Smørgrav 	hashvalue_type h = qstate->minfo[id] ?
33265b390aaSDag-Erling Smørgrav 		((struct subnet_qstate*)qstate->minfo[id])->qinfo_hash :
33365b390aaSDag-Erling Smørgrav 		query_info_hash(&qstate->qinfo, qstate->query_flags);
33465b390aaSDag-Erling Smørgrav 	/* Step 1, general qinfo lookup */
33565b390aaSDag-Erling Smørgrav 	struct lruhash_entry *lru_entry = slabhash_lookup(subnet_msg_cache, h,
33665b390aaSDag-Erling Smørgrav 		&qstate->qinfo, 1);
337e86b9096SDag-Erling Smørgrav 	int need_to_insert = (lru_entry == NULL);
33865b390aaSDag-Erling Smørgrav 	if (!lru_entry) {
339e86b9096SDag-Erling Smørgrav 		void* data = calloc(1,
340e86b9096SDag-Erling Smørgrav 			sizeof(struct subnet_msg_cache_data));
341e86b9096SDag-Erling Smørgrav 		if(!data) {
342e86b9096SDag-Erling Smørgrav 			log_err("malloc failed");
343e86b9096SDag-Erling Smørgrav 			return;
344e86b9096SDag-Erling Smørgrav 		}
34565b390aaSDag-Erling Smørgrav 		qinf = qstate->qinfo;
34665b390aaSDag-Erling Smørgrav 		qinf.qname = memdup(qstate->qinfo.qname,
34765b390aaSDag-Erling Smørgrav 			qstate->qinfo.qname_len);
34865b390aaSDag-Erling Smørgrav 		if(!qinf.qname) {
349e86b9096SDag-Erling Smørgrav 			free(data);
35065b390aaSDag-Erling Smørgrav 			log_err("memdup failed");
35165b390aaSDag-Erling Smørgrav 			return;
35265b390aaSDag-Erling Smørgrav 		}
353e86b9096SDag-Erling Smørgrav 		mrep_entry = query_info_entrysetup(&qinf, data, h);
35465b390aaSDag-Erling Smørgrav 		free(qinf.qname); /* if qname 'consumed', it is set to NULL */
35565b390aaSDag-Erling Smørgrav 		if (!mrep_entry) {
356e86b9096SDag-Erling Smørgrav 			free(data);
35765b390aaSDag-Erling Smørgrav 			log_err("query_info_entrysetup failed");
35865b390aaSDag-Erling Smørgrav 			return;
35965b390aaSDag-Erling Smørgrav 		}
36065b390aaSDag-Erling Smørgrav 		lru_entry = &mrep_entry->entry;
36157bddd21SDag-Erling Smørgrav 		lock_rw_wrlock(&lru_entry->lock);
36265b390aaSDag-Erling Smørgrav 	}
363e86b9096SDag-Erling Smørgrav 	/* lru_entry->lock is locked regardless of how we got here,
364e86b9096SDag-Erling Smørgrav 	 * either from the slabhash_lookup, or above in the new allocated */
36565b390aaSDag-Erling Smørgrav 	/* Step 2, find the correct tree */
36665b390aaSDag-Erling Smørgrav 	if (!(tree = get_tree(lru_entry->data, edns, sne, qstate->env->cfg))) {
367e86b9096SDag-Erling Smørgrav 		lock_rw_unlock(&lru_entry->lock);
3685469a995SCy Schubert 		log_err("subnetcache: cache insertion failed");
36965b390aaSDag-Erling Smørgrav 		return;
37065b390aaSDag-Erling Smørgrav 	}
37157bddd21SDag-Erling Smørgrav 	lock_quick_lock(&sne->alloc.lock);
37265b390aaSDag-Erling Smørgrav 	rep = reply_info_copy(qstate->return_msg->rep, &sne->alloc, NULL);
37357bddd21SDag-Erling Smørgrav 	lock_quick_unlock(&sne->alloc.lock);
37465b390aaSDag-Erling Smørgrav 	if (!rep) {
375e86b9096SDag-Erling Smørgrav 		lock_rw_unlock(&lru_entry->lock);
3765469a995SCy Schubert 		log_err("subnetcache: cache insertion failed");
37765b390aaSDag-Erling Smørgrav 		return;
37865b390aaSDag-Erling Smørgrav 	}
37965b390aaSDag-Erling Smørgrav 
38065b390aaSDag-Erling Smørgrav 	/* store RRsets */
38165b390aaSDag-Erling Smørgrav 	for(i=0; i<rep->rrset_count; i++) {
38265b390aaSDag-Erling Smørgrav 		rep->ref[i].key = rep->rrsets[i];
38365b390aaSDag-Erling Smørgrav 		rep->ref[i].id = rep->rrsets[i]->id;
38465b390aaSDag-Erling Smørgrav 	}
38565b390aaSDag-Erling Smørgrav 	reply_info_set_ttls(rep, *qstate->env->now);
38665b390aaSDag-Erling Smørgrav 	rep->flags |= (BIT_RA | BIT_QR); /* fix flags to be sensible for */
38765b390aaSDag-Erling Smørgrav 	rep->flags &= ~(BIT_AA | BIT_CD);/* a reply based on the cache   */
38865b390aaSDag-Erling Smørgrav 	addrtree_insert(tree, (addrkey_t*)edns->subnet_addr,
38925039b37SCy Schubert 		edns->subnet_source_mask, sq->max_scope, rep,
39065b390aaSDag-Erling Smørgrav 		rep->ttl, *qstate->env->now);
391e86b9096SDag-Erling Smørgrav 
39265b390aaSDag-Erling Smørgrav 	lock_rw_unlock(&lru_entry->lock);
393e86b9096SDag-Erling Smørgrav 	if (need_to_insert) {
39465b390aaSDag-Erling Smørgrav 		slabhash_insert(subnet_msg_cache, h, lru_entry, lru_entry->data,
39565b390aaSDag-Erling Smørgrav 			NULL);
39665b390aaSDag-Erling Smørgrav 	}
39765b390aaSDag-Erling Smørgrav }
39865b390aaSDag-Erling Smørgrav 
39965b390aaSDag-Erling Smørgrav /** Lookup in cache and reply true iff reply is sent. */
40065b390aaSDag-Erling Smørgrav static int
40165b390aaSDag-Erling Smørgrav lookup_and_reply(struct module_qstate *qstate, int id, struct subnet_qstate *sq)
40265b390aaSDag-Erling Smørgrav {
40365b390aaSDag-Erling Smørgrav 	struct lruhash_entry *e;
40465b390aaSDag-Erling Smørgrav 	struct module_env *env = qstate->env;
40565b390aaSDag-Erling Smørgrav 	struct subnet_env *sne = (struct subnet_env*)env->modinfo[id];
40665b390aaSDag-Erling Smørgrav 	hashvalue_type h = query_info_hash(&qstate->qinfo, qstate->query_flags);
40765b390aaSDag-Erling Smørgrav 	struct subnet_msg_cache_data *data;
40865b390aaSDag-Erling Smørgrav 	struct ecs_data *ecs = &sq->ecs_client_in;
40965b390aaSDag-Erling Smørgrav 	struct addrtree *tree;
41065b390aaSDag-Erling Smørgrav 	struct addrnode *node;
41165b390aaSDag-Erling Smørgrav 	uint8_t scope;
41265b390aaSDag-Erling Smørgrav 
41365b390aaSDag-Erling Smørgrav 	memset(&sq->ecs_client_out, 0, sizeof(sq->ecs_client_out));
41465b390aaSDag-Erling Smørgrav 
41565b390aaSDag-Erling Smørgrav 	if (sq) sq->qinfo_hash = h; /* Might be useful on cache miss */
41665b390aaSDag-Erling Smørgrav 	e = slabhash_lookup(sne->subnet_msg_cache, h, &qstate->qinfo, 1);
41765b390aaSDag-Erling Smørgrav 	if (!e) return 0; /* qinfo not in cache */
41865b390aaSDag-Erling Smørgrav 	data = e->data;
41965b390aaSDag-Erling Smørgrav 	tree = (ecs->subnet_addr_fam == EDNSSUBNET_ADDRFAM_IP4)?
42065b390aaSDag-Erling Smørgrav 		data->tree4 : data->tree6;
42165b390aaSDag-Erling Smørgrav 	if (!tree) { /* qinfo in cache but not for this family */
42265b390aaSDag-Erling Smørgrav 		lock_rw_unlock(&e->lock);
42365b390aaSDag-Erling Smørgrav 		return 0;
42465b390aaSDag-Erling Smørgrav 	}
42565b390aaSDag-Erling Smørgrav 	node = addrtree_find(tree, (addrkey_t*)ecs->subnet_addr,
42665b390aaSDag-Erling Smørgrav 		ecs->subnet_source_mask, *env->now);
42765b390aaSDag-Erling Smørgrav 	if (!node) { /* plain old cache miss */
42865b390aaSDag-Erling Smørgrav 		lock_rw_unlock(&e->lock);
42965b390aaSDag-Erling Smørgrav 		return 0;
43065b390aaSDag-Erling Smørgrav 	}
43165b390aaSDag-Erling Smørgrav 
43265b390aaSDag-Erling Smørgrav 	qstate->return_msg = tomsg(NULL, &qstate->qinfo,
433091e9e46SCy Schubert 		(struct reply_info *)node->elem, qstate->region, *env->now, 0,
43465b390aaSDag-Erling Smørgrav 		env->scratch);
43565b390aaSDag-Erling Smørgrav 	scope = (uint8_t)node->scope;
43665b390aaSDag-Erling Smørgrav 	lock_rw_unlock(&e->lock);
43765b390aaSDag-Erling Smørgrav 
43865b390aaSDag-Erling Smørgrav 	if (!qstate->return_msg) { /* Failed allocation or expired TTL */
43965b390aaSDag-Erling Smørgrav 		return 0;
44065b390aaSDag-Erling Smørgrav 	}
44165b390aaSDag-Erling Smørgrav 
44265b390aaSDag-Erling Smørgrav 	if (sq->subnet_downstream) { /* relay to interested client */
44365b390aaSDag-Erling Smørgrav 		sq->ecs_client_out.subnet_scope_mask = scope;
44465b390aaSDag-Erling Smørgrav 		sq->ecs_client_out.subnet_addr_fam = ecs->subnet_addr_fam;
44565b390aaSDag-Erling Smørgrav 		sq->ecs_client_out.subnet_source_mask = ecs->subnet_source_mask;
44665b390aaSDag-Erling Smørgrav 		memcpy(&sq->ecs_client_out.subnet_addr, &ecs->subnet_addr,
44765b390aaSDag-Erling Smørgrav 			INET6_SIZE);
44865b390aaSDag-Erling Smørgrav 		sq->ecs_client_out.subnet_validdata = 1;
44965b390aaSDag-Erling Smørgrav 	}
45065b390aaSDag-Erling Smørgrav 	return 1;
45165b390aaSDag-Erling Smørgrav }
45265b390aaSDag-Erling Smørgrav 
45365b390aaSDag-Erling Smørgrav /**
45465b390aaSDag-Erling Smørgrav  * Test first bits of addresses for equality. Caller is responsible
45565b390aaSDag-Erling Smørgrav  * for making sure that both a and b are at least net/8 octets long.
45665b390aaSDag-Erling Smørgrav  * @param a: first address.
45765b390aaSDag-Erling Smørgrav  * @param a: seconds address.
45865b390aaSDag-Erling Smørgrav  * @param net: Number of bits to test.
45965b390aaSDag-Erling Smørgrav  * @return: 1 if equal, 0 otherwise.
46065b390aaSDag-Erling Smørgrav  */
46165b390aaSDag-Erling Smørgrav static int
46265b390aaSDag-Erling Smørgrav common_prefix(uint8_t *a, uint8_t *b, uint8_t net)
46365b390aaSDag-Erling Smørgrav {
46465b390aaSDag-Erling Smørgrav 	size_t n = (size_t)net / 8;
46565b390aaSDag-Erling Smørgrav 	return !memcmp(a, b, n) && ((net % 8) == 0 || a[n] == b[n]);
46665b390aaSDag-Erling Smørgrav }
46765b390aaSDag-Erling Smørgrav 
46865b390aaSDag-Erling Smørgrav static enum module_ext_state
46965b390aaSDag-Erling Smørgrav eval_response(struct module_qstate *qstate, int id, struct subnet_qstate *sq)
47065b390aaSDag-Erling Smørgrav {
47165b390aaSDag-Erling Smørgrav 	struct subnet_env *sne = qstate->env->modinfo[id];
47265b390aaSDag-Erling Smørgrav 
47365b390aaSDag-Erling Smørgrav 	struct ecs_data *c_in  = &sq->ecs_client_in; /* rcvd from client */
47465b390aaSDag-Erling Smørgrav 	struct ecs_data *c_out = &sq->ecs_client_out;/* will send to client */
47565b390aaSDag-Erling Smørgrav 	struct ecs_data *s_in  = &sq->ecs_server_in; /* rcvd from auth */
47665b390aaSDag-Erling Smørgrav 	struct ecs_data *s_out = &sq->ecs_server_out;/* sent to auth */
47765b390aaSDag-Erling Smørgrav 
47865b390aaSDag-Erling Smørgrav 	memset(c_out, 0, sizeof(*c_out));
47965b390aaSDag-Erling Smørgrav 
4804c75e3aaSDag-Erling Smørgrav 	if (!qstate->return_msg) {
4814c75e3aaSDag-Erling Smørgrav 		/* already an answer and its not a message, but retain
4824c75e3aaSDag-Erling Smørgrav 		 * the actual rcode, instead of module_error, so send
4834c75e3aaSDag-Erling Smørgrav 		 * module_finished */
4844c75e3aaSDag-Erling Smørgrav 		return module_finished;
4854c75e3aaSDag-Erling Smørgrav 	}
48665b390aaSDag-Erling Smørgrav 
48765b390aaSDag-Erling Smørgrav 	/* We have not asked for subnet data */
48865b390aaSDag-Erling Smørgrav 	if (!sq->subnet_sent) {
48965b390aaSDag-Erling Smørgrav 		if (s_in->subnet_validdata)
4905469a995SCy Schubert 			verbose(VERB_QUERY, "subnetcache: received spurious data");
49165b390aaSDag-Erling Smørgrav 		if (sq->subnet_downstream) /* Copy back to client */
49265b390aaSDag-Erling Smørgrav 			cp_edns_bad_response(c_out, c_in);
49365b390aaSDag-Erling Smørgrav 		return module_finished;
49465b390aaSDag-Erling Smørgrav 	}
49565b390aaSDag-Erling Smørgrav 
49665b390aaSDag-Erling Smørgrav 	/* subnet sent but nothing came back */
49765b390aaSDag-Erling Smørgrav 	if (!s_in->subnet_validdata) {
49865b390aaSDag-Erling Smørgrav 		/* The authority indicated no support for edns subnet. As a
49965b390aaSDag-Erling Smørgrav 		 * consequence the answer ended up in the regular cache. It
500*24e36522SCy Schubert 		 * is still useful to put it in the edns subnet cache for
50165b390aaSDag-Erling Smørgrav 		 * when a client explicitly asks for subnet specific answer. */
5025469a995SCy Schubert 		verbose(VERB_QUERY, "subnetcache: Authority indicates no support");
503e86b9096SDag-Erling Smørgrav 		if(!sq->started_no_cache_store) {
50465b390aaSDag-Erling Smørgrav 			lock_rw_wrlock(&sne->biglock);
50565b390aaSDag-Erling Smørgrav 			update_cache(qstate, id);
50665b390aaSDag-Erling Smørgrav 			lock_rw_unlock(&sne->biglock);
507e86b9096SDag-Erling Smørgrav 		}
50865b390aaSDag-Erling Smørgrav 		if (sq->subnet_downstream)
50965b390aaSDag-Erling Smørgrav 			cp_edns_bad_response(c_out, c_in);
51065b390aaSDag-Erling Smørgrav 		return module_finished;
51165b390aaSDag-Erling Smørgrav 	}
51265b390aaSDag-Erling Smørgrav 
51365b390aaSDag-Erling Smørgrav 	/* Being here means we have asked for and got a subnet specific
51465b390aaSDag-Erling Smørgrav 	 * answer. Also, the answer from the authority is not yet cached
51565b390aaSDag-Erling Smørgrav 	 * anywhere. */
51665b390aaSDag-Erling Smørgrav 
51765b390aaSDag-Erling Smørgrav 	/* can we accept response? */
51865b390aaSDag-Erling Smørgrav 	if(s_out->subnet_addr_fam != s_in->subnet_addr_fam ||
51965b390aaSDag-Erling Smørgrav 		s_out->subnet_source_mask != s_in->subnet_source_mask ||
52065b390aaSDag-Erling Smørgrav 		!common_prefix(s_out->subnet_addr, s_in->subnet_addr,
52165b390aaSDag-Erling Smørgrav 			s_out->subnet_source_mask))
52265b390aaSDag-Erling Smørgrav 	{
52365b390aaSDag-Erling Smørgrav 		/* we can not accept, restart query without option */
5245469a995SCy Schubert 		verbose(VERB_QUERY, "subnetcache: forged data");
52565b390aaSDag-Erling Smørgrav 		s_out->subnet_validdata = 0;
52665b390aaSDag-Erling Smørgrav 		(void)edns_opt_list_remove(&qstate->edns_opts_back_out,
52765b390aaSDag-Erling Smørgrav 			qstate->env->cfg->client_subnet_opcode);
52865b390aaSDag-Erling Smørgrav 		sq->subnet_sent = 0;
52965b390aaSDag-Erling Smørgrav 		return module_restart_next;
53065b390aaSDag-Erling Smørgrav 	}
53165b390aaSDag-Erling Smørgrav 
53265b390aaSDag-Erling Smørgrav 	lock_rw_wrlock(&sne->biglock);
533e86b9096SDag-Erling Smørgrav 	if(!sq->started_no_cache_store) {
53465b390aaSDag-Erling Smørgrav 		update_cache(qstate, id);
535e86b9096SDag-Erling Smørgrav 	}
5364c75e3aaSDag-Erling Smørgrav 	sne->num_msg_nocache++;
53765b390aaSDag-Erling Smørgrav 	lock_rw_unlock(&sne->biglock);
53865b390aaSDag-Erling Smørgrav 
53965b390aaSDag-Erling Smørgrav 	if (sq->subnet_downstream) {
54065b390aaSDag-Erling Smørgrav 		/* Client wants to see the answer, echo option back
54165b390aaSDag-Erling Smørgrav 		 * and adjust the scope. */
54265b390aaSDag-Erling Smørgrav 		c_out->subnet_addr_fam = c_in->subnet_addr_fam;
54365b390aaSDag-Erling Smørgrav 		c_out->subnet_source_mask = c_in->subnet_source_mask;
54465b390aaSDag-Erling Smørgrav 		memcpy(&c_out->subnet_addr, &c_in->subnet_addr, INET6_SIZE);
54525039b37SCy Schubert 		c_out->subnet_scope_mask = sq->max_scope;
546e86b9096SDag-Erling Smørgrav 		/* Limit scope returned to client to scope used for caching. */
547e86b9096SDag-Erling Smørgrav 		if(c_out->subnet_addr_fam == EDNSSUBNET_ADDRFAM_IP4) {
548e86b9096SDag-Erling Smørgrav 			if(c_out->subnet_scope_mask >
549e86b9096SDag-Erling Smørgrav 				qstate->env->cfg->max_client_subnet_ipv4) {
550e86b9096SDag-Erling Smørgrav 				c_out->subnet_scope_mask =
551e86b9096SDag-Erling Smørgrav 					qstate->env->cfg->max_client_subnet_ipv4;
552e86b9096SDag-Erling Smørgrav 			}
553e86b9096SDag-Erling Smørgrav 		}
554e86b9096SDag-Erling Smørgrav 		else if(c_out->subnet_scope_mask >
555e86b9096SDag-Erling Smørgrav 				qstate->env->cfg->max_client_subnet_ipv6) {
556e86b9096SDag-Erling Smørgrav 				c_out->subnet_scope_mask =
557e86b9096SDag-Erling Smørgrav 					qstate->env->cfg->max_client_subnet_ipv6;
558e86b9096SDag-Erling Smørgrav 		}
55965b390aaSDag-Erling Smørgrav 		c_out->subnet_validdata = 1;
56065b390aaSDag-Erling Smørgrav 	}
56165b390aaSDag-Erling Smørgrav 	return module_finished;
56265b390aaSDag-Erling Smørgrav }
56365b390aaSDag-Erling Smørgrav 
56465b390aaSDag-Erling Smørgrav /** Parse EDNS opt data containing ECS */
56565b390aaSDag-Erling Smørgrav static int
56665b390aaSDag-Erling Smørgrav parse_subnet_option(struct edns_option* ecs_option, struct ecs_data* ecs)
56765b390aaSDag-Erling Smørgrav {
56865b390aaSDag-Erling Smørgrav 	memset(ecs, 0, sizeof(*ecs));
56965b390aaSDag-Erling Smørgrav 	if (ecs_option->opt_len < 4)
57065b390aaSDag-Erling Smørgrav 		return 0;
57165b390aaSDag-Erling Smørgrav 
57265b390aaSDag-Erling Smørgrav 	ecs->subnet_addr_fam = sldns_read_uint16(ecs_option->opt_data);
57365b390aaSDag-Erling Smørgrav 	ecs->subnet_source_mask = ecs_option->opt_data[2];
57465b390aaSDag-Erling Smørgrav 	ecs->subnet_scope_mask = ecs_option->opt_data[3];
5758a384985SDag-Erling Smørgrav 	/* remaining bytes indicate address */
57665b390aaSDag-Erling Smørgrav 
57765b390aaSDag-Erling Smørgrav 	/* validate input*/
57865b390aaSDag-Erling Smørgrav 	/* option length matches calculated length? */
57965b390aaSDag-Erling Smørgrav 	if (ecs_option->opt_len != (size_t)((ecs->subnet_source_mask+7)/8 + 4))
58065b390aaSDag-Erling Smørgrav 		return 0;
58165b390aaSDag-Erling Smørgrav 	if (ecs_option->opt_len - 4 > INET6_SIZE || ecs_option->opt_len == 0)
58265b390aaSDag-Erling Smørgrav 		return 0;
58365b390aaSDag-Erling Smørgrav 	if (ecs->subnet_addr_fam == EDNSSUBNET_ADDRFAM_IP4) {
58465b390aaSDag-Erling Smørgrav 		if (ecs->subnet_source_mask > 32 || ecs->subnet_scope_mask > 32)
58565b390aaSDag-Erling Smørgrav 			return 0;
58665b390aaSDag-Erling Smørgrav 	} else if (ecs->subnet_addr_fam == EDNSSUBNET_ADDRFAM_IP6) {
58765b390aaSDag-Erling Smørgrav 		if (ecs->subnet_source_mask > 128 ||
58865b390aaSDag-Erling Smørgrav 			ecs->subnet_scope_mask > 128)
58965b390aaSDag-Erling Smørgrav 			return 0;
59065b390aaSDag-Erling Smørgrav 	} else
59165b390aaSDag-Erling Smørgrav 		return 0;
59265b390aaSDag-Erling Smørgrav 
59365b390aaSDag-Erling Smørgrav 	/* valid ECS data, write to ecs_data */
59465b390aaSDag-Erling Smørgrav 	if (copy_clear(ecs->subnet_addr, INET6_SIZE, ecs_option->opt_data + 4,
59565b390aaSDag-Erling Smørgrav 		ecs_option->opt_len - 4, ecs->subnet_source_mask))
59665b390aaSDag-Erling Smørgrav 		return 0;
59765b390aaSDag-Erling Smørgrav 	ecs->subnet_validdata = 1;
59865b390aaSDag-Erling Smørgrav 	return 1;
59965b390aaSDag-Erling Smørgrav }
60065b390aaSDag-Erling Smørgrav 
60165b390aaSDag-Erling Smørgrav static void
60265b390aaSDag-Erling Smørgrav subnet_option_from_ss(struct sockaddr_storage *ss, struct ecs_data* ecs,
60365b390aaSDag-Erling Smørgrav 	struct config_file* cfg)
60465b390aaSDag-Erling Smørgrav {
60565b390aaSDag-Erling Smørgrav 	void* sinaddr;
60665b390aaSDag-Erling Smørgrav 
60765b390aaSDag-Erling Smørgrav 	/* Construct subnet option from original query */
60865b390aaSDag-Erling Smørgrav 	if(((struct sockaddr_in*)ss)->sin_family == AF_INET) {
60965b390aaSDag-Erling Smørgrav 		ecs->subnet_source_mask = cfg->max_client_subnet_ipv4;
61065b390aaSDag-Erling Smørgrav 		ecs->subnet_addr_fam = EDNSSUBNET_ADDRFAM_IP4;
61165b390aaSDag-Erling Smørgrav 		sinaddr = &((struct sockaddr_in*)ss)->sin_addr;
61265b390aaSDag-Erling Smørgrav 		if (!copy_clear( ecs->subnet_addr, INET6_SIZE,
61365b390aaSDag-Erling Smørgrav 			(uint8_t *)sinaddr, INET_SIZE,
61465b390aaSDag-Erling Smørgrav 			ecs->subnet_source_mask)) {
61565b390aaSDag-Erling Smørgrav 			ecs->subnet_validdata = 1;
61665b390aaSDag-Erling Smørgrav 		}
61765b390aaSDag-Erling Smørgrav 	}
61865b390aaSDag-Erling Smørgrav #ifdef INET6
61965b390aaSDag-Erling Smørgrav 	else {
62065b390aaSDag-Erling Smørgrav 		ecs->subnet_source_mask = cfg->max_client_subnet_ipv6;
62165b390aaSDag-Erling Smørgrav 		ecs->subnet_addr_fam = EDNSSUBNET_ADDRFAM_IP6;
62265b390aaSDag-Erling Smørgrav 		sinaddr = &((struct sockaddr_in6*)ss)->sin6_addr;
62365b390aaSDag-Erling Smørgrav 		if (!copy_clear( ecs->subnet_addr, INET6_SIZE,
62465b390aaSDag-Erling Smørgrav 			(uint8_t *)sinaddr, INET6_SIZE,
62565b390aaSDag-Erling Smørgrav 			ecs->subnet_source_mask)) {
62665b390aaSDag-Erling Smørgrav 			ecs->subnet_validdata = 1;
62765b390aaSDag-Erling Smørgrav 		}
62865b390aaSDag-Erling Smørgrav 	}
62965b390aaSDag-Erling Smørgrav #else
63065b390aaSDag-Erling Smørgrav 			/* We don't know how to handle ip6, just pass */
63165b390aaSDag-Erling Smørgrav #endif /* INET6 */
63265b390aaSDag-Erling Smørgrav }
63365b390aaSDag-Erling Smørgrav 
63465b390aaSDag-Erling Smørgrav int
63565b390aaSDag-Erling Smørgrav ecs_query_response(struct module_qstate* qstate, struct dns_msg* response,
63665b390aaSDag-Erling Smørgrav 	int id, void* ATTR_UNUSED(cbargs))
63765b390aaSDag-Erling Smørgrav {
63865b390aaSDag-Erling Smørgrav 	struct subnet_qstate *sq;
63965b390aaSDag-Erling Smørgrav 
64065b390aaSDag-Erling Smørgrav 	if(!response || !(sq=(struct subnet_qstate*)qstate->minfo[id]))
64165b390aaSDag-Erling Smørgrav 		return 1;
64265b390aaSDag-Erling Smørgrav 
64365b390aaSDag-Erling Smørgrav 	if(sq->subnet_sent &&
64465b390aaSDag-Erling Smørgrav 		FLAGS_GET_RCODE(response->rep->flags) == LDNS_RCODE_REFUSED) {
6458a384985SDag-Erling Smørgrav 		/* REFUSED response to ECS query, remove ECS option. */
64665b390aaSDag-Erling Smørgrav 		edns_opt_list_remove(&qstate->edns_opts_back_out,
64765b390aaSDag-Erling Smørgrav 			qstate->env->cfg->client_subnet_opcode);
64865b390aaSDag-Erling Smørgrav 		sq->subnet_sent = 0;
64965b390aaSDag-Erling Smørgrav 		memset(&sq->ecs_server_out, 0, sizeof(sq->ecs_server_out));
65025039b37SCy Schubert 	} else if (!sq->track_max_scope &&
65125039b37SCy Schubert 		FLAGS_GET_RCODE(response->rep->flags) == LDNS_RCODE_NOERROR &&
65225039b37SCy Schubert 		response->rep->an_numrrsets > 0
65325039b37SCy Schubert 		) {
65425039b37SCy Schubert 		struct ub_packed_rrset_key* s = response->rep->rrsets[0];
65525039b37SCy Schubert 		if(ntohs(s->rk.type) == LDNS_RR_TYPE_CNAME &&
65625039b37SCy Schubert 			query_dname_compare(qstate->qinfo.qname,
65725039b37SCy Schubert 			s->rk.dname) == 0) {
65825039b37SCy Schubert 			/* CNAME response for QNAME. From now on keep track of
65925039b37SCy Schubert 			 * longest received ECS prefix for all queries on this
66025039b37SCy Schubert 			 * qstate. */
66125039b37SCy Schubert 			sq->track_max_scope = 1;
66225039b37SCy Schubert 		}
66365b390aaSDag-Erling Smørgrav 	}
66465b390aaSDag-Erling Smørgrav 	return 1;
66565b390aaSDag-Erling Smørgrav }
66665b390aaSDag-Erling Smørgrav 
66765b390aaSDag-Erling Smørgrav int
66865b390aaSDag-Erling Smørgrav ecs_edns_back_parsed(struct module_qstate* qstate, int id,
66965b390aaSDag-Erling Smørgrav 	void* ATTR_UNUSED(cbargs))
67065b390aaSDag-Erling Smørgrav {
67165b390aaSDag-Erling Smørgrav 	struct subnet_qstate *sq;
67265b390aaSDag-Erling Smørgrav 	struct edns_option* ecs_opt;
67365b390aaSDag-Erling Smørgrav 
67465b390aaSDag-Erling Smørgrav 	if(!(sq=(struct subnet_qstate*)qstate->minfo[id]))
67565b390aaSDag-Erling Smørgrav 		return 1;
67665b390aaSDag-Erling Smørgrav 	if((ecs_opt = edns_opt_list_find(
67765b390aaSDag-Erling Smørgrav 		qstate->edns_opts_back_in,
67825039b37SCy Schubert 		qstate->env->cfg->client_subnet_opcode)) &&
67925039b37SCy Schubert 		parse_subnet_option(ecs_opt, &sq->ecs_server_in) &&
68025039b37SCy Schubert 		sq->subnet_sent && sq->ecs_server_in.subnet_validdata) {
68165b390aaSDag-Erling Smørgrav 			/* Only skip global cache store if we sent an ECS option
68265b390aaSDag-Erling Smørgrav 			 * and received one back. Answers from non-whitelisted
6838a384985SDag-Erling Smørgrav 			 * servers will end up in global cache. Answers for
68465b390aaSDag-Erling Smørgrav 			 * queries with 0 source will not (unless nameserver
68565b390aaSDag-Erling Smørgrav 			 * does not support ECS). */
68665b390aaSDag-Erling Smørgrav 			qstate->no_cache_store = 1;
68725039b37SCy Schubert 			if(!sq->track_max_scope || (sq->track_max_scope &&
68825039b37SCy Schubert 				sq->ecs_server_in.subnet_scope_mask >
68925039b37SCy Schubert 				sq->max_scope))
69025039b37SCy Schubert 				sq->max_scope = sq->ecs_server_in.subnet_scope_mask;
69165b390aaSDag-Erling Smørgrav 	}
69265b390aaSDag-Erling Smørgrav 
69365b390aaSDag-Erling Smørgrav 	return 1;
69465b390aaSDag-Erling Smørgrav }
69565b390aaSDag-Erling Smørgrav 
69665b390aaSDag-Erling Smørgrav void
69765b390aaSDag-Erling Smørgrav subnetmod_operate(struct module_qstate *qstate, enum module_ev event,
69865b390aaSDag-Erling Smørgrav 	int id, struct outbound_entry* outbound)
69965b390aaSDag-Erling Smørgrav {
70065b390aaSDag-Erling Smørgrav 	struct subnet_env *sne = qstate->env->modinfo[id];
70165b390aaSDag-Erling Smørgrav 	struct subnet_qstate *sq = (struct subnet_qstate*)qstate->minfo[id];
70265b390aaSDag-Erling Smørgrav 
7035469a995SCy Schubert 	verbose(VERB_QUERY, "subnetcache[module %d] operate: extstate:%s "
70465b390aaSDag-Erling Smørgrav 		"event:%s", id, strextstate(qstate->ext_state[id]),
70565b390aaSDag-Erling Smørgrav 		strmodulevent(event));
7065469a995SCy Schubert 	log_query_info(VERB_QUERY, "subnetcache operate: query", &qstate->qinfo);
70765b390aaSDag-Erling Smørgrav 
70865b390aaSDag-Erling Smørgrav 	if((event == module_event_new || event == module_event_pass) &&
70965b390aaSDag-Erling Smørgrav 		sq == NULL) {
71065b390aaSDag-Erling Smørgrav 		struct edns_option* ecs_opt;
71165b390aaSDag-Erling Smørgrav 		if(!subnet_new_qstate(qstate, id)) {
71265b390aaSDag-Erling Smørgrav 			qstate->return_msg = NULL;
71365b390aaSDag-Erling Smørgrav 			qstate->ext_state[id] = module_finished;
71465b390aaSDag-Erling Smørgrav 			return;
71565b390aaSDag-Erling Smørgrav 		}
71665b390aaSDag-Erling Smørgrav 
71765b390aaSDag-Erling Smørgrav 		sq = (struct subnet_qstate*)qstate->minfo[id];
71865b390aaSDag-Erling Smørgrav 
71965b390aaSDag-Erling Smørgrav 		if((ecs_opt = edns_opt_list_find(
72065b390aaSDag-Erling Smørgrav 			qstate->edns_opts_front_in,
72165b390aaSDag-Erling Smørgrav 			qstate->env->cfg->client_subnet_opcode))) {
72265b390aaSDag-Erling Smørgrav 			if(!parse_subnet_option(ecs_opt, &sq->ecs_client_in)) {
72365b390aaSDag-Erling Smørgrav 				/* Wrongly formatted ECS option. RFC mandates to
72465b390aaSDag-Erling Smørgrav 				 * return FORMERROR. */
72565b390aaSDag-Erling Smørgrav 				qstate->return_rcode = LDNS_RCODE_FORMERR;
72665b390aaSDag-Erling Smørgrav 				qstate->ext_state[id] = module_finished;
72765b390aaSDag-Erling Smørgrav 				return;
72865b390aaSDag-Erling Smørgrav 			}
72965b390aaSDag-Erling Smørgrav 			sq->subnet_downstream = 1;
73065b390aaSDag-Erling Smørgrav 		}
73165b390aaSDag-Erling Smørgrav 		else if(qstate->mesh_info->reply_list) {
73265b390aaSDag-Erling Smørgrav 			subnet_option_from_ss(
73365b390aaSDag-Erling Smørgrav 				&qstate->mesh_info->reply_list->query_reply.addr,
73465b390aaSDag-Erling Smørgrav 				&sq->ecs_client_in, qstate->env->cfg);
73565b390aaSDag-Erling Smørgrav 		}
73665b390aaSDag-Erling Smørgrav 
73765b390aaSDag-Erling Smørgrav 		if(sq->ecs_client_in.subnet_validdata == 0) {
73865b390aaSDag-Erling Smørgrav 			/* No clients are interested in result or we could not
73965b390aaSDag-Erling Smørgrav 			 * parse it, we don't do client subnet */
74065b390aaSDag-Erling Smørgrav 			sq->ecs_server_out.subnet_validdata = 0;
7415469a995SCy Schubert 			verbose(VERB_ALGO, "subnetcache: pass to next module");
74265b390aaSDag-Erling Smørgrav 			qstate->ext_state[id] = module_wait_module;
74365b390aaSDag-Erling Smørgrav 			return;
74465b390aaSDag-Erling Smørgrav 		}
74565b390aaSDag-Erling Smørgrav 
746e86b9096SDag-Erling Smørgrav 		/* Limit to minimum allowed source mask */
747e86b9096SDag-Erling Smørgrav 		if(sq->ecs_client_in.subnet_source_mask != 0 && (
748e86b9096SDag-Erling Smørgrav 			(sq->ecs_client_in.subnet_addr_fam == EDNSSUBNET_ADDRFAM_IP4 &&
749e86b9096SDag-Erling Smørgrav 			 sq->ecs_client_in.subnet_source_mask < qstate->env->cfg->min_client_subnet_ipv4) ||
750e86b9096SDag-Erling Smørgrav 			(sq->ecs_client_in.subnet_addr_fam == EDNSSUBNET_ADDRFAM_IP6 &&
751e86b9096SDag-Erling Smørgrav 			 sq->ecs_client_in.subnet_source_mask < qstate->env->cfg->min_client_subnet_ipv6))) {
752e86b9096SDag-Erling Smørgrav 				qstate->return_rcode = LDNS_RCODE_REFUSED;
753e86b9096SDag-Erling Smørgrav 				qstate->ext_state[id] = module_finished;
754e86b9096SDag-Erling Smørgrav 				return;
755e86b9096SDag-Erling Smørgrav 		}
756e86b9096SDag-Erling Smørgrav 
75765b390aaSDag-Erling Smørgrav 		lock_rw_wrlock(&sne->biglock);
75865b390aaSDag-Erling Smørgrav 		if (lookup_and_reply(qstate, id, sq)) {
7594c75e3aaSDag-Erling Smørgrav 			sne->num_msg_cache++;
76065b390aaSDag-Erling Smørgrav 			lock_rw_unlock(&sne->biglock);
7615469a995SCy Schubert 			verbose(VERB_QUERY, "subnetcache: answered from cache");
76265b390aaSDag-Erling Smørgrav 			qstate->ext_state[id] = module_finished;
76365b390aaSDag-Erling Smørgrav 
76465b390aaSDag-Erling Smørgrav 			ecs_opt_list_append(&sq->ecs_client_out,
76565b390aaSDag-Erling Smørgrav 				&qstate->edns_opts_front_out, qstate);
76665b390aaSDag-Erling Smørgrav 			return;
76765b390aaSDag-Erling Smørgrav 		}
76865b390aaSDag-Erling Smørgrav 		lock_rw_unlock(&sne->biglock);
76965b390aaSDag-Erling Smørgrav 
77065b390aaSDag-Erling Smørgrav 		sq->ecs_server_out.subnet_addr_fam =
77165b390aaSDag-Erling Smørgrav 			sq->ecs_client_in.subnet_addr_fam;
77265b390aaSDag-Erling Smørgrav 		sq->ecs_server_out.subnet_source_mask =
77365b390aaSDag-Erling Smørgrav 			sq->ecs_client_in.subnet_source_mask;
77465b390aaSDag-Erling Smørgrav 		/* Limit source prefix to configured maximum */
77565b390aaSDag-Erling Smørgrav 		if(sq->ecs_server_out.subnet_addr_fam == EDNSSUBNET_ADDRFAM_IP4
77665b390aaSDag-Erling Smørgrav 			&& sq->ecs_server_out.subnet_source_mask >
77765b390aaSDag-Erling Smørgrav 			qstate->env->cfg->max_client_subnet_ipv4)
77865b390aaSDag-Erling Smørgrav 			sq->ecs_server_out.subnet_source_mask =
77965b390aaSDag-Erling Smørgrav 				qstate->env->cfg->max_client_subnet_ipv4;
78065b390aaSDag-Erling Smørgrav 		else if(sq->ecs_server_out.subnet_addr_fam == EDNSSUBNET_ADDRFAM_IP6
78165b390aaSDag-Erling Smørgrav 			&& sq->ecs_server_out.subnet_source_mask >
78265b390aaSDag-Erling Smørgrav 			qstate->env->cfg->max_client_subnet_ipv6)
78365b390aaSDag-Erling Smørgrav 			sq->ecs_server_out.subnet_source_mask =
78465b390aaSDag-Erling Smørgrav 				qstate->env->cfg->max_client_subnet_ipv6;
78565b390aaSDag-Erling Smørgrav 		/* Safe to copy completely, even if the source is limited by the
78665b390aaSDag-Erling Smørgrav 		 * configuration. ecs_opt_list_append() will limit the address.
78765b390aaSDag-Erling Smørgrav 		 * */
78865b390aaSDag-Erling Smørgrav 		memcpy(&sq->ecs_server_out.subnet_addr,
78965b390aaSDag-Erling Smørgrav 			sq->ecs_client_in.subnet_addr, INET6_SIZE);
79065b390aaSDag-Erling Smørgrav 		sq->ecs_server_out.subnet_scope_mask = 0;
79165b390aaSDag-Erling Smørgrav 		sq->ecs_server_out.subnet_validdata = 1;
79265b390aaSDag-Erling Smørgrav 		if(sq->ecs_server_out.subnet_source_mask != 0 &&
7938a384985SDag-Erling Smørgrav 			qstate->env->cfg->client_subnet_always_forward &&
79465b390aaSDag-Erling Smørgrav 			sq->subnet_downstream)
79565b390aaSDag-Erling Smørgrav 			/* ECS specific data required, do not look at the global
79665b390aaSDag-Erling Smørgrav 			 * cache in other modules. */
79765b390aaSDag-Erling Smørgrav 			qstate->no_cache_lookup = 1;
79865b390aaSDag-Erling Smørgrav 
79965b390aaSDag-Erling Smørgrav 		/* pass request to next module */
80065b390aaSDag-Erling Smørgrav 		verbose(VERB_ALGO,
8015469a995SCy Schubert 			"subnetcache: not found in cache. pass to next module");
80265b390aaSDag-Erling Smørgrav 		qstate->ext_state[id] = module_wait_module;
80365b390aaSDag-Erling Smørgrav 		return;
80465b390aaSDag-Erling Smørgrav 	}
80565b390aaSDag-Erling Smørgrav 	/* Query handed back by next module, we have a 'final' answer */
80665b390aaSDag-Erling Smørgrav 	if(sq && event == module_event_moddone) {
80765b390aaSDag-Erling Smørgrav 		qstate->ext_state[id] = eval_response(qstate, id, sq);
8084c75e3aaSDag-Erling Smørgrav 		if(qstate->ext_state[id] == module_finished &&
8094c75e3aaSDag-Erling Smørgrav 			qstate->return_msg) {
81065b390aaSDag-Erling Smørgrav 			ecs_opt_list_append(&sq->ecs_client_out,
81165b390aaSDag-Erling Smørgrav 				&qstate->edns_opts_front_out, qstate);
81265b390aaSDag-Erling Smørgrav 		}
813e86b9096SDag-Erling Smørgrav 		qstate->no_cache_store = sq->started_no_cache_store;
81465b390aaSDag-Erling Smørgrav 		return;
81565b390aaSDag-Erling Smørgrav 	}
81665b390aaSDag-Erling Smørgrav 	if(sq && outbound) {
81765b390aaSDag-Erling Smørgrav 		return;
81865b390aaSDag-Erling Smørgrav 	}
81965b390aaSDag-Erling Smørgrav 	/* We are being revisited */
82065b390aaSDag-Erling Smørgrav 	if(event == module_event_pass || event == module_event_new) {
82165b390aaSDag-Erling Smørgrav 		/* Just pass it on, we already did the work */
8225469a995SCy Schubert 		verbose(VERB_ALGO, "subnetcache: pass to next module");
82365b390aaSDag-Erling Smørgrav 		qstate->ext_state[id] = module_wait_module;
82465b390aaSDag-Erling Smørgrav 		return;
82565b390aaSDag-Erling Smørgrav 	}
82665b390aaSDag-Erling Smørgrav 	if(!sq && (event == module_event_moddone)) {
82765b390aaSDag-Erling Smørgrav 		/* during priming, module done but we never started */
82865b390aaSDag-Erling Smørgrav 		qstate->ext_state[id] = module_finished;
82965b390aaSDag-Erling Smørgrav 		return;
83065b390aaSDag-Erling Smørgrav 	}
8315469a995SCy Schubert 	log_err("subnetcache: bad event %s", strmodulevent(event));
83265b390aaSDag-Erling Smørgrav 	qstate->ext_state[id] = module_error;
83365b390aaSDag-Erling Smørgrav 	return;
83465b390aaSDag-Erling Smørgrav }
83565b390aaSDag-Erling Smørgrav 
83665b390aaSDag-Erling Smørgrav void
83765b390aaSDag-Erling Smørgrav subnetmod_clear(struct module_qstate *ATTR_UNUSED(qstate),
83865b390aaSDag-Erling Smørgrav 	int ATTR_UNUSED(id))
83965b390aaSDag-Erling Smørgrav {
84065b390aaSDag-Erling Smørgrav 	/* qstate has no data outside region */
84165b390aaSDag-Erling Smørgrav }
84265b390aaSDag-Erling Smørgrav 
84365b390aaSDag-Erling Smørgrav void
84465b390aaSDag-Erling Smørgrav subnetmod_inform_super(struct module_qstate *ATTR_UNUSED(qstate),
84565b390aaSDag-Erling Smørgrav 	int ATTR_UNUSED(id), struct module_qstate *ATTR_UNUSED(super))
84665b390aaSDag-Erling Smørgrav {
84765b390aaSDag-Erling Smørgrav 	/* Not used */
84865b390aaSDag-Erling Smørgrav }
84965b390aaSDag-Erling Smørgrav 
85065b390aaSDag-Erling Smørgrav size_t
85165b390aaSDag-Erling Smørgrav subnetmod_get_mem(struct module_env *env, int id)
85265b390aaSDag-Erling Smørgrav {
85365b390aaSDag-Erling Smørgrav 	struct subnet_env *sn_env = env->modinfo[id];
85465b390aaSDag-Erling Smørgrav 	if (!sn_env) return 0;
85565b390aaSDag-Erling Smørgrav 	return sizeof(*sn_env) +
85665b390aaSDag-Erling Smørgrav 		slabhash_get_mem(sn_env->subnet_msg_cache) +
857c7f4d7adSDag-Erling Smørgrav 		ecs_whitelist_get_mem(sn_env->whitelist);
85865b390aaSDag-Erling Smørgrav }
85965b390aaSDag-Erling Smørgrav 
86065b390aaSDag-Erling Smørgrav /**
86165b390aaSDag-Erling Smørgrav  * The module function block
86265b390aaSDag-Erling Smørgrav  */
86365b390aaSDag-Erling Smørgrav static struct module_func_block subnetmod_block = {
8645469a995SCy Schubert 	"subnetcache", &subnetmod_init, &subnetmod_deinit, &subnetmod_operate,
86565b390aaSDag-Erling Smørgrav 	&subnetmod_inform_super, &subnetmod_clear, &subnetmod_get_mem
86665b390aaSDag-Erling Smørgrav };
86765b390aaSDag-Erling Smørgrav 
86865b390aaSDag-Erling Smørgrav struct module_func_block*
86965b390aaSDag-Erling Smørgrav subnetmod_get_funcblock(void)
87065b390aaSDag-Erling Smørgrav {
87165b390aaSDag-Erling Smørgrav 	return &subnetmod_block;
87265b390aaSDag-Erling Smørgrav }
87365b390aaSDag-Erling Smørgrav 
87465b390aaSDag-Erling Smørgrav /** Wrappers for static functions to unit test */
87565b390aaSDag-Erling Smørgrav size_t
87665b390aaSDag-Erling Smørgrav unittest_wrapper_subnetmod_sizefunc(void *elemptr)
87765b390aaSDag-Erling Smørgrav {
87865b390aaSDag-Erling Smørgrav 	return sizefunc(elemptr);
87965b390aaSDag-Erling Smørgrav }
88065b390aaSDag-Erling Smørgrav 
88165b390aaSDag-Erling Smørgrav #endif  /* CLIENT_SUBNET */
882