xref: /openbsd-src/usr.sbin/unbound/util/module.h (revision 4c1e55dc91edd6e69ccc60ce855900fbc12cf34f)
1 /*
2  * util/module.h - DNS handling module interface
3  *
4  * Copyright (c) 2007, NLnet Labs. All rights reserved.
5  *
6  * This software is open source.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * Redistributions of source code must retain the above copyright notice,
13  * this list of conditions and the following disclaimer.
14  *
15  * Redistributions in binary form must reproduce the above copyright notice,
16  * this list of conditions and the following disclaimer in the documentation
17  * and/or other materials provided with the distribution.
18  *
19  * Neither the name of the NLNET LABS nor the names of its contributors may
20  * be used to endorse or promote products derived from this software without
21  * specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
25  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
27  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33  * POSSIBILITY OF SUCH DAMAGE.
34  */
35 
36 /**
37  * \file
38  *
39  * This file contains the interface for DNS handling modules.
40  */
41 
42 #ifndef UTIL_MODULE_H
43 #define UTIL_MODULE_H
44 #include "util/storage/lruhash.h"
45 #include "util/data/msgreply.h"
46 #include "util/data/msgparse.h"
47 struct alloc_cache;
48 struct rrset_cache;
49 struct key_cache;
50 struct config_file;
51 struct slabhash;
52 struct query_info;
53 struct edns_data;
54 struct regional;
55 struct worker;
56 struct module_qstate;
57 struct ub_randstate;
58 struct mesh_area;
59 struct mesh_state;
60 struct val_anchors;
61 struct val_neg_cache;
62 struct iter_forwards;
63 
64 /** Maximum number of modules in operation */
65 #define MAX_MODULE 5
66 
67 /**
68  * Module environment.
69  * Services and data provided to the module.
70  */
71 struct module_env {
72 	/* --- data --- */
73 	/** config file with config options */
74 	struct config_file* cfg;
75 	/** shared message cache */
76 	struct slabhash* msg_cache;
77 	/** shared rrset cache */
78 	struct rrset_cache* rrset_cache;
79 	/** shared infrastructure cache (edns, lameness) */
80 	struct infra_cache* infra_cache;
81 	/** shared key cache */
82 	struct key_cache* key_cache;
83 
84 	/* --- services --- */
85 	/**
86 	 * Send serviced DNS query to server. UDP/TCP and EDNS is handled.
87 	 * operate() should return with wait_reply. Later on a callback
88 	 * will cause operate() to be called with event timeout or reply.
89 	 * The time until a timeout is calculated from roundtrip timing,
90 	 * several UDP retries are attempted.
91 	 * @param qname: query name. (host order)
92 	 * @param qnamelen: length in bytes of qname, including trailing 0.
93 	 * @param qtype: query type. (host order)
94 	 * @param qclass: query class. (host order)
95 	 * @param flags: host order flags word, with opcode and CD bit.
96 	 * @param dnssec: if set, EDNS record will have bits set.
97 	 *	If EDNS_DO bit is set, DO bit is set in EDNS records.
98 	 *	If BIT_CD is set, CD bit is set in queries with EDNS records.
99 	 * @param want_dnssec: if set, the validator wants DNSSEC.  Without
100 	 * 	EDNS, the answer is likely to be useless for this domain.
101 	 * @param addr: where to.
102 	 * @param addrlen: length of addr.
103 	 * @param zone: delegation point name.
104 	 * @param zonelen: length of zone name.
105 	 * @param q: wich query state to reactivate upon return.
106 	 * @return: false on failure (memory or socket related). no query was
107 	 *	sent. Or returns an outbound entry with qsent and qstate set.
108 	 *	This outbound_entry will be used on later module invocations
109 	 *	that involve this query (timeout, error or reply).
110 	 */
111 	struct outbound_entry* (*send_query)(uint8_t* qname, size_t qnamelen,
112 		uint16_t qtype, uint16_t qclass, uint16_t flags, int dnssec,
113 		int want_dnssec, struct sockaddr_storage* addr,
114 		socklen_t addrlen, uint8_t* zone, size_t zonelen,
115 		struct module_qstate* q);
116 
117 	/**
118 	 * Detach-subqueries.
119 	 * Remove all sub-query references from this query state.
120 	 * Keeps super-references of those sub-queries correct.
121 	 * Updates stat items in mesh_area structure.
122 	 * @param qstate: used to find mesh state.
123 	 */
124 	void (*detach_subs)(struct module_qstate* qstate);
125 
126 	/**
127 	 * Attach subquery.
128 	 * Creates it if it does not exist already.
129 	 * Keeps sub and super references correct.
130 	 * Updates stat items in mesh_area structure.
131 	 * Pass if it is priming query or not.
132 	 * return:
133 	 * o if error (malloc) happened.
134 	 * o need to initialise the new state (module init; it is a new state).
135 	 *   so that the next run of the query with this module is successful.
136 	 * o no init needed, attachment successful.
137 	 *
138 	 * @param qstate: the state to find mesh state, and that wants to
139 	 * 	receive the results from the new subquery.
140 	 * @param qinfo: what to query for (copied).
141 	 * @param qflags: what flags to use (RD, CD flag or not).
142 	 * @param prime: if it is a (stub) priming query.
143 	 * @param newq: If the new subquery needs initialisation, it is
144 	 * 	returned, otherwise NULL is returned.
145 	 * @return: false on error, true if success (and init may be needed).
146 	 */
147 	int (*attach_sub)(struct module_qstate* qstate,
148 		struct query_info* qinfo, uint16_t qflags, int prime,
149 		struct module_qstate** newq);
150 
151 	/**
152 	 * Kill newly attached sub. If attach_sub returns newq for
153 	 * initialisation, but that fails, then this routine will cleanup and
154 	 * delete the fresly created sub.
155 	 * @param newq: the new subquery that is no longer needed.
156 	 * 	It is removed.
157 	 */
158 	void (*kill_sub)(struct module_qstate* newq);
159 
160 	/**
161 	 * Detect if adding a dependency for qstate on name,type,class will
162 	 * create a dependency cycle.
163 	 * @param qstate: given mesh querystate.
164 	 * @param qinfo: query info for dependency.
165 	 * @param flags: query flags of dependency, RD/CD flags.
166 	 * @param prime: if dependency is a priming query or not.
167 	 * @return true if the name,type,class exists and the given
168 	 * 	qstate mesh exists as a dependency of that name. Thus
169 	 * 	if qstate becomes dependent on name,type,class then a
170 	 * 	cycle is created.
171 	 */
172 	int (*detect_cycle)(struct module_qstate* qstate,
173 		struct query_info* qinfo, uint16_t flags, int prime);
174 
175 	/** region for temporary usage. May be cleared after operate() call. */
176 	struct regional* scratch;
177 	/** buffer for temporary usage. May be cleared after operate() call. */
178 	ldns_buffer* scratch_buffer;
179 	/** internal data for daemon - worker thread. */
180 	struct worker* worker;
181 	/** mesh area with query state dependencies */
182 	struct mesh_area* mesh;
183 	/** allocation service */
184 	struct alloc_cache* alloc;
185 	/** random table to generate random numbers */
186 	struct ub_randstate* rnd;
187 	/** time in seconds, converted to integer */
188 	uint32_t* now;
189 	/** time in microseconds. Relatively recent. */
190 	struct timeval* now_tv;
191 	/** is validation required for messages, controls client-facing
192 	 * validation status (AD bits) and servfails */
193 	int need_to_validate;
194 	/** trusted key storage; these are the configured keys, if not NULL,
195 	 * otherwise configured by validator. These are the trust anchors,
196 	 * and are not primed and ready for validation, but on the bright
197 	 * side, they are read only memory, thus no locks and fast. */
198 	struct val_anchors* anchors;
199 	/** negative cache, configured by the validator. if not NULL,
200 	 * contains NSEC record lookup trees. */
201 	struct val_neg_cache* neg_cache;
202 	/** the 5011-probe timer (if any) */
203 	struct comm_timer* probe_timer;
204 	/** Mapping of forwarding zones to targets.
205 	 * iterator forwarder information. per-thread, created by worker */
206 	struct iter_forwards* fwds;
207 	/** module specific data. indexed by module id. */
208 	void* modinfo[MAX_MODULE];
209 };
210 
211 /**
212  * External visible states of the module state machine
213  * Modules may also have an internal state.
214  * Modules are supposed to run to completion or until blocked.
215  */
216 enum module_ext_state {
217 	/** initial state - new query */
218 	module_state_initial = 0,
219 	/** waiting for reply to outgoing network query */
220 	module_wait_reply,
221 	/** module is waiting for another module */
222 	module_wait_module,
223 	/** module is waiting for another module; that other is restarted */
224 	module_restart_next,
225 	/** module is waiting for sub-query */
226 	module_wait_subquery,
227 	/** module could not finish the query */
228 	module_error,
229 	/** module is finished with query */
230 	module_finished
231 };
232 
233 /**
234  * Events that happen to modules, that start or wakeup modules.
235  */
236 enum module_ev {
237 	/** new query */
238 	module_event_new = 0,
239 	/** query passed by other module */
240 	module_event_pass,
241 	/** reply inbound from server */
242 	module_event_reply,
243 	/** no reply, timeout or other error */
244 	module_event_noreply,
245 	/** reply is there, but capitalisation check failed */
246 	module_event_capsfail,
247 	/** next module is done, and its reply is awaiting you */
248 	module_event_moddone,
249 	/** error */
250 	module_event_error
251 };
252 
253 /**
254  * Linked list of sockaddrs
255  * May be allocated such that only 'len' bytes of addr exist for the structure.
256  */
257 struct sock_list {
258 	/** next in list */
259 	struct sock_list* next;
260 	/** length of addr */
261 	socklen_t len;
262 	/** sockaddr */
263 	struct sockaddr_storage addr;
264 };
265 
266 /**
267  * Module state, per query.
268  */
269 struct module_qstate {
270 	/** which query is being answered: name, type, class */
271 	struct query_info qinfo;
272 	/** flags uint16 from query */
273 	uint16_t query_flags;
274 	/** if this is a (stub or root) priming query (with hints) */
275 	int is_priming;
276 
277 	/** comm_reply contains server replies */
278 	struct comm_reply* reply;
279 	/** the reply message, with message for client and calling module */
280 	struct dns_msg* return_msg;
281 	/** the rcode, in case of error, instead of a reply message */
282 	int return_rcode;
283 	/** origin of the reply (can be NULL from cache, list for cnames) */
284 	struct sock_list* reply_origin;
285 	/** IP blacklist for queries */
286 	struct sock_list* blacklist;
287 	/** region for this query. Cleared when query process finishes. */
288 	struct regional* region;
289 	/** failure reason information if val-log-level is high */
290 	struct config_strlist* errinf;
291 
292 	/** which module is executing */
293 	int curmod;
294 	/** module states */
295 	enum module_ext_state ext_state[MAX_MODULE];
296 	/** module specific data for query. indexed by module id. */
297 	void* minfo[MAX_MODULE];
298 	/** environment for this query */
299 	struct module_env* env;
300 	/** mesh related information for this query */
301 	struct mesh_state* mesh_info;
302 	/** how many seconds before expiry is this prefetched (0 if not) */
303 	uint32_t prefetch_leeway;
304 };
305 
306 /**
307  * Module functionality block
308  */
309 struct module_func_block {
310 	/** text string name of module */
311 	const char* name;
312 
313 	/**
314 	 * init the module. Called once for the global state.
315 	 * This is the place to apply settings from the config file.
316 	 * @param env: module environment.
317 	 * @param id: module id number.
318 	 * return: 0 on error
319 	 */
320 	int (*init)(struct module_env* env, int id);
321 
322 	/**
323 	 * de-init, delete, the module. Called once for the global state.
324 	 * @param env: module environment.
325 	 * @param id: module id number.
326 	 */
327 	void (*deinit)(struct module_env* env, int id);
328 
329 	/**
330 	 * accept a new query, or work further on existing query.
331 	 * Changes the qstate->ext_state to be correct on exit.
332 	 * @param ev: event that causes the module state machine to
333 	 *	(re-)activate.
334 	 * @param qstate: the query state.
335 	 *	Note that this method is not allowed to change the
336 	 *	query state 'identity', that is query info, qflags,
337 	 *	and priming status.
338 	 *	Attach a subquery to get results to a different query.
339 	 * @param id: module id number that operate() is called on.
340 	 * @param outbound: if not NULL this event is due to the reply/timeout
341 	 *	or error on this outbound query.
342 	 * @return: if at exit the ext_state is:
343 	 *	o wait_module: next module is started. (with pass event).
344 	 *	o error or finished: previous module is resumed.
345 	 *	o otherwise it waits until that event happens (assumes
346 	 *	  the service routine to make subrequest or send message
347 	 *	  have been called.
348 	 */
349 	void (*operate)(struct module_qstate* qstate, enum module_ev event,
350 		int id, struct outbound_entry* outbound);
351 
352 	/**
353 	 * inform super querystate about the results from this subquerystate.
354 	 * Is called when the querystate is finished.  The method invoked is
355 	 * the one from the current module active in the super querystate.
356 	 * @param qstate: the query state that is finished.
357 	 *	Examine return_rcode and return_reply in the qstate.
358 	 * @param id: module id for this module.
359 	 *	This coincides with the current module for the super qstate.
360 	 * @param super: the super querystate that needs to be informed.
361 	 */
362 	void (*inform_super)(struct module_qstate* qstate, int id,
363 		struct module_qstate* super);
364 
365 	/**
366 	 * clear module specific data
367 	 */
368 	void (*clear)(struct module_qstate* qstate, int id);
369 
370 	/**
371 	 * How much memory is the module specific data using.
372 	 * @param env: module environment.
373 	 * @param id: the module id.
374 	 * @return the number of bytes that are alloced.
375 	 */
376 	size_t (*get_mem)(struct module_env* env, int id);
377 };
378 
379 /**
380  * Debug utility: module external qstate to string
381  * @param s: the state value.
382  * @return descriptive string.
383  */
384 const char* strextstate(enum module_ext_state s);
385 
386 /**
387  * Debug utility: module event to string
388  * @param e: the module event value.
389  * @return descriptive string.
390  */
391 const char* strmodulevent(enum module_ev e);
392 
393 #endif /* UTIL_MODULE_H */
394