xref: /freebsd/contrib/unbound/edns-subnet/subnetmod.h (revision 36712a94975f5bd0d26c85377283b49a2369c82f)
1 /*
2  * edns-subnet/subnetmod.h - 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 #ifndef SUBNETMOD_H
42 #define SUBNETMOD_H
43 #include "util/module.h"
44 #include "services/outbound_list.h"
45 #include "util/alloc.h"
46 #include "util/net_help.h"
47 #include "util/storage/slabhash.h"
48 #include "edns-subnet/addrtree.h"
49 #include "edns-subnet/edns-subnet.h"
50 
51 /**
52  * Global state for the subnet module.
53  */
54 struct subnet_env {
55 	/** shared message cache
56 	 * key: struct query_info*
57 	 * data: struct subnet_msg_cache_data* */
58 	struct slabhash* subnet_msg_cache;
59 	/** access control, which upstream servers we send client address */
60 	struct ecs_whitelist* whitelist;
61 	/** allocation service */
62 	struct alloc_cache alloc;
63 	lock_rw_type biglock;
64 	/** number of messages from cache */
65 	size_t num_msg_cache;
66 	/** number of messages not from cache */
67 	size_t num_msg_nocache;
68 };
69 
70 struct subnet_msg_cache_data {
71 	struct addrtree* tree4;
72 	struct addrtree* tree6;
73 };
74 
75 struct subnet_qstate {
76 	/** We need the hash for both cache lookup and insert */
77 	hashvalue_type qinfo_hash;
78 	/** ecs_data for client communication */
79 	struct ecs_data	ecs_client_in;
80 	struct ecs_data	ecs_client_out;
81 	/** ecss data for server communication */
82 	struct ecs_data	ecs_server_in;
83 	struct ecs_data	ecs_server_out;
84 	int subnet_downstream;
85 	int subnet_sent;
86 	/** has the subnet module been started with no_cache_store? */
87 	int started_no_cache_store;
88 };
89 
90 void subnet_data_delete(void* d, void* ATTR_UNUSED(arg));
91 size_t msg_cache_sizefunc(void* k, void* d);
92 
93 /**
94  * Get the module function block.
95  * @return: function block with function pointers to module methods.
96  */
97 struct module_func_block* subnetmod_get_funcblock(void);
98 
99 /** subnet module init */
100 int subnetmod_init(struct module_env* env, int id);
101 
102 /** subnet module deinit */
103 void subnetmod_deinit(struct module_env* env, int id);
104 
105 /** subnet module operate on a query */
106 void subnetmod_operate(struct module_qstate* qstate, enum module_ev event,
107 	int id, struct outbound_entry* outbound);
108 
109 /** subnet module  */
110 void subnetmod_inform_super(struct module_qstate* qstate, int id,
111 	struct module_qstate* super);
112 
113 /** subnet module cleanup query state */
114 void subnetmod_clear(struct module_qstate* qstate, int id);
115 
116 /** subnet module alloc size routine */
117 size_t subnetmod_get_mem(struct module_env* env, int id);
118 
119 /** Wrappers for static functions to unit test */
120 size_t unittest_wrapper_subnetmod_sizefunc(void *elemptr);
121 
122 /** Whitelist check, called just before query is sent upstream. */
123 int ecs_whitelist_check(struct query_info* qinfo, uint16_t flags,
124 	struct module_qstate* qstate, struct sockaddr_storage* addr,
125 	socklen_t addrlen, uint8_t* zone, size_t zonelen,
126 	struct regional* region, int id, void* cbargs);
127 
128 /** Check whether response from server contains ECS record, if so, skip cache
129  * store. Called just after parsing EDNS data from server. */
130 int ecs_edns_back_parsed(struct module_qstate* qstate, int id, void* cbargs);
131 
132 /** Remove ECS record from back_out when query resulted in REFUSED response. */
133 int ecs_query_response(struct module_qstate* qstate, struct dns_msg* response,
134 	int id, void* cbargs);
135 
136 /** mark subnet msg to be deleted */
137 void subnet_markdel(void* key);
138 
139 #endif /* SUBNETMOD_H */
140