]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/unbound/services/authzone.h
Upgrade Unbound to 1.7.0. More to follow.
[FreeBSD/FreeBSD.git] / contrib / unbound / services / authzone.h
1 /*
2  * services/authzone.h - authoritative zone that is locally hosted.
3  *
4  * Copyright (c) 2017, 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
25  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27  * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
29  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34  */
35
36 /**
37  * \file
38  *
39  * This file contains the functions for an authority zone.  This zone
40  * is queried by the iterator, just like a stub or forward zone, but then
41  * the data is locally held.
42  */
43
44 #ifndef SERVICES_AUTHZONE_H
45 #define SERVICES_AUTHZONE_H
46 #include "util/rbtree.h"
47 #include "util/locks.h"
48 #include "services/mesh.h"
49 struct ub_packed_rrset_key;
50 struct regional;
51 struct config_file;
52 struct config_auth;
53 struct query_info;
54 struct dns_msg;
55 struct edns_data;
56 struct module_env;
57 struct worker;
58 struct comm_point;
59 struct comm_timer;
60 struct comm_reply;
61 struct auth_rrset;
62 struct auth_nextprobe;
63 struct auth_probe;
64 struct auth_transfer;
65 struct auth_master;
66 struct auth_chunk;
67
68 /**
69  * Authoritative zones, shared.
70  */
71 struct auth_zones {
72         /** lock on the authzone trees */
73         lock_rw_type lock;
74         /** rbtree of struct auth_zone */
75         rbtree_type ztree;
76         /** rbtree of struct auth_xfer */
77         rbtree_type xtree;
78         /** do we have downstream enabled */
79         int have_downstream;
80 };
81
82 /**
83  * Auth zone.  Authoritative data, that is fetched from instead of sending
84  * packets to the internet.
85  */
86 struct auth_zone {
87         /** rbtree node, key is name and class */
88         rbnode_type node;
89
90         /** zone name, in uncompressed wireformat */
91         uint8_t* name;
92         /** length of zone name */
93         size_t namelen;
94         /** number of labels in zone name */
95         int namelabs;
96         /** the class of this zone, in host byteorder.
97          * uses 'dclass' to not conflict with c++ keyword class. */
98         uint16_t dclass;
99
100         /** lock on the data in the structure
101          * For the node, parent, name, namelen, namelabs, dclass, you
102          * need to also hold the zones_tree lock to change them (or to
103          * delete this zone) */
104         lock_rw_type lock;
105
106         /** auth data for this zone
107          * rbtree of struct auth_data */
108         rbtree_type data;
109
110         /** zonefile name (or NULL for no zonefile) */
111         char* zonefile;
112         /** fallback to the internet on failure or ttl-expiry of auth zone */
113         int fallback_enabled;
114         /** the zone has expired (enabled by the xfer worker), fallback
115          * happens if that option is enabled. */
116         int zone_expired;
117         /** zone is a slave zone (it has masters) */
118         int zone_is_slave;
119         /** for downstream: this zone answers queries towards the downstream
120          * clients */
121         int for_downstream;
122         /** for upstream: this zone answers queries that unbound intends to
123          * send upstream. */
124         int for_upstream;
125 };
126
127 /**
128  * Auth data. One domain name, and the RRs to go with it.
129  */
130 struct auth_data {
131         /** rbtree node, key is name only */
132         rbnode_type node;
133         /** domain name */
134         uint8_t* name;
135         /** length of name */
136         size_t namelen;
137         /** number of labels in name */
138         int namelabs;
139         /** the data rrsets, with different types, linked list.
140          * if the list if NULL the node would be an empty non-terminal,
141          * but in this data structure such nodes that represent an empty
142          * non-terminal are not needed; they just don't exist.
143          */
144         struct auth_rrset* rrsets;
145 };
146
147 /**
148  * A auth data RRset
149  */
150 struct auth_rrset {
151         /** next in list */
152         struct auth_rrset* next;
153         /** RR type in host byteorder */
154         uint16_t type;
155         /** RRset data item */
156         struct packed_rrset_data* data;
157 };
158
159 /**
160  * Authoritative zone transfer structure.
161  * Create and destroy needs the auth_zones* biglock.
162  * The structure consists of different tasks.  Each can be unowned (-1) or
163  * owner by a worker (worker-num).  A worker can pick up a task and then do
164  * it.  This means the events (timeouts, sockets) are for that worker.
165  * 
166  * (move this to tasks).
167  * They don't have locks themselves, the worker (that owns it) uses it,
168  * also as part of callbacks, hence it has separate zonename pointers for
169  * lookup in the main zonetree.  If the zone has no transfers, this
170  * structure is not created.
171  */
172 struct auth_xfer {
173         /** rbtree node, key is name and class */
174         rbnode_type node;
175
176         /** lock on this structure, and on the workernum elements of the
177          * tasks.  First hold the tree-lock in auth_zones, find the auth_xfer,
178          * lock this lock.  Then a worker can reassign itself to fill up
179          * one of the tasks. 
180          * Once it has the task assigned to it, the worker can access the
181          * other elements of the task structure without a lock, because that
182          * is necessary for the eventloop and callbacks from that. */
183         lock_basic_type lock;
184
185         /** zone name, in uncompressed wireformat */
186         uint8_t* name;
187         /** length of zone name */
188         size_t namelen;
189         /** number of labels in zone name */
190         int namelabs;
191         /** the class of this zone, in host byteorder.
192          * uses 'dclass' to not conflict with c++ keyword class. */
193         uint16_t dclass;
194
195         /** task to wait for next-probe-timeout,
196          * once timeouted, see if a SOA probe is needed, or already
197          * in progress */
198         struct auth_nextprobe* task_nextprobe;
199
200         /** task for SOA probe.  Check if the zone can be updated */
201         struct auth_probe* task_probe;
202
203         /** Task for transfer.  Transferring and updating the zone.  This
204          * includes trying (potentially) several upstream masters.  Downloading
205          * and storing the zone */
206         struct auth_transfer* task_transfer;
207
208         /** a notify was received, but a zone transfer or probe was already
209          * acted on.
210          * However, the zone transfer could signal a newer serial number.
211          * The serial number of that notify is saved below.  The transfer and
212          * probe tasks should check this once done to see if they need to
213          * restart the transfer task for the newer notify serial.
214          * Hold the lock to access this member (and the serial).
215          */
216         int notify_received;
217         /** serial number of the notify */
218         uint32_t notify_serial;
219
220         /* protected by the lock on the structure, information about
221          * the loaded authority zone. */
222         /** is the zone currently considered expired? after expiry also older
223          * serial numbers are allowed (not just newer) */
224         int zone_expired;
225         /** do we have a zone (if 0, no zone data at all) */
226         int have_zone;
227
228         /** current serial (from SOA), if we have no zone, 0 */
229         uint32_t serial;
230         /** retry time (from SOA), time to wait with next_probe
231          * if no master responds */
232         time_t retry;
233         /** refresh time (from SOA), time to wait with next_probe
234          * if everything is fine */
235         time_t refresh;
236         /** expiry time (from SOA), time until zone data is not considered
237          * valid any more, if no master responds within this time, either
238          * with the current zone or a new zone. */
239         time_t expiry;
240
241         /** zone lease start time (start+expiry is expiration time).
242          * this is renewed every SOA probe and transfer.  On zone load
243          * from zonefile it is also set (with probe set soon to check) */
244         time_t lease_time;
245 };
246
247 /**
248  * The next probe task.
249  * This task consists of waiting for the probetimeout.  It is a task because
250  * it needs an event in the eventtable.  Once the timeout has passed, that
251  * worker can (potentially) become the auth_probe worker, or if another worker
252  * is already doing that, do nothing.  Tasks becomes unowned.
253  * The probe worker, if it detects nothing has to be done picks up this task,
254  * if unowned.
255  */
256 struct auth_nextprobe {
257         /* Worker pointer. NULL means unowned. */
258         struct worker* worker;
259         /* module env for this task */
260         struct module_env* env;
261
262         /** increasing backoff for failures */
263         time_t backoff;
264         /** Timeout for next probe (for SOA) */
265         time_t next_probe;
266         /** timeout callback for next_probe or expiry(if that is sooner).
267          * it is on the worker's event_base */
268         struct comm_timer* timer;
269 };
270
271 /**
272  * The probe task.
273  * Send a SOA UDP query to see if the zone needs to be updated (or similar,
274  * potential, HTTP probe query) and check serial number.
275  * If yes, start the auth_transfer task.  If no, make sure auth_nextprobe
276  * timeout wait task is running.
277  * Needs to be a task, because the UDP query needs an event entry.
278  * This task could also be started by eg. a NOTIFY being received, even though
279  * another worker is performing the nextprobe task (and that worker keeps
280  * waiting uninterrupted).
281  */
282 struct auth_probe {
283         /* Worker pointer. NULL means unowned. */
284         struct worker* worker;
285         /* module env for this task */
286         struct module_env* env;
287
288         /** list of upstream masters for this zone, from config */
289         struct auth_master* masters;
290
291         /** for the hostname lookups, which master is current */
292         struct auth_master* lookup_target;
293         /** are we looking up A or AAAA, first A, then AAAA (if ip6 enabled) */
294         int lookup_aaaa;
295
296         /** once notified, or the timeout has been reached. a scan starts. */
297         /** the scan specific target (notify source), or NULL if none */
298         struct auth_master* scan_specific;
299         /** scan tries all the upstream masters. the scan current target. 
300          * or NULL if not working on sequential scan */
301         struct auth_master* scan_target;
302         /** if not NULL, the specific addr for the current master */
303         struct auth_addr* scan_addr;
304
305         /** dns id of packet in flight */
306         uint16_t id;
307         /** the SOA probe udp event.
308          * on the workers event base. */
309         struct comm_point* cp;
310         /** timeout for packets.
311          * on the workers event base. */
312         struct comm_timer* timer;
313         /** timeout in msec */
314         int timeout;
315 };
316
317 /**
318  * The transfer task.
319  * Once done, make sure the nextprobe waiting task is running, whether done
320  * with failure or success.  If failure, use shorter timeout for wait time.
321  */
322 struct auth_transfer {
323         /* Worker pointer. NULL means unowned. */
324         struct worker* worker;
325         /* module env for this task */
326         struct module_env* env;
327
328         /** xfer data that has been transferred, the data is applied
329          * once the transfer has completed correctly */
330         struct auth_chunk* chunks_first;
331         /** last element in chunks list (to append new data at the end) */
332         struct auth_chunk* chunks_last;
333
334         /** list of upstream masters for this zone, from config */
335         struct auth_master* masters;
336
337         /** for the hostname lookups, which master is current */
338         struct auth_master* lookup_target;
339         /** are we looking up A or AAAA, first A, then AAAA (if ip6 enabled) */
340         int lookup_aaaa;
341
342         /** once notified, or the timeout has been reached. a scan starts. */
343         /** the scan specific target (notify source), or NULL if none */
344         struct auth_master* scan_specific;
345         /** scan tries all the upstream masters. the scan current target. 
346          * or NULL if not working on sequential scan */
347         struct auth_master* scan_target;
348         /** what address we are scanning for the master, or NULL if the
349          * master is in IP format itself */
350         struct auth_addr* scan_addr;
351         /** the zone transfer in progress (or NULL if in scan).  It is
352          * from this master */
353         struct auth_master* master;
354
355         /** failed ixfr transfer, retry with axfr (to the current master),
356          * the IXFR was 'REFUSED', 'SERVFAIL', 'NOTIMPL' or the contents of
357          * the IXFR did not apply cleanly (out of sync, delete of nonexistent
358          * data or add of duplicate data).  Flag is cleared once the retry
359          * with axfr is done. */
360         int ixfr_fail;
361         /** we are doing IXFR right now */
362         int on_ixfr;
363         /** did we detect the current AXFR/IXFR serial number yet, 0 not yet,
364          * 1 we saw the first, 2 we saw the second, 3 must be last SOA in xfr*/
365         int got_xfr_serial;
366         /** number of RRs scanned for AXFR/IXFR detection */
367         size_t rr_scan_num;
368         /** we are doing an IXFR but we detected an AXFR contents */
369         int on_ixfr_is_axfr;
370         /** the serial number for the current AXFR/IXFR incoming reply,
371          * for IXFR, the outermost SOA records serial */
372         uint32_t incoming_xfr_serial;
373
374         /** dns id of AXFR query */
375         uint16_t id;
376         /** the transfer (TCP) to the master.
377          * on the workers event base. */
378         struct comm_point* cp;
379 };
380
381 /** list of addresses */
382 struct auth_addr {
383         /** next in list */
384         struct auth_addr* next;
385         /** IP address */
386         struct sockaddr_storage addr;
387         /** addr length */
388         socklen_t addrlen;
389 };
390
391 /** auth zone master upstream, and the config settings for it */
392 struct auth_master {
393         /** next master in list */
394         struct auth_master* next;
395         /** master IP address (and port), or hostname, string */
396         char* host;
397         /** for http, filename */
398         char* file;
399         /** use HTTP for this master */
400         int http;
401         /** use IXFR for this master */
402         int ixfr;
403         /** use ssl for channel */
404         int ssl;
405         /** the port number (for urls) */
406         int port;
407         /** if the host is a hostname, the list of resolved addrs, if any*/
408         struct auth_addr* list;
409 };
410
411 /** auth zone master zone transfer data chunk */
412 struct auth_chunk {
413         /** next chunk in list */
414         struct auth_chunk* next;
415         /** the data from this chunk, this is what was received.
416          * for an IXFR that means results from comm_net tcp actions,
417          * packets. also for an AXFR. For HTTP a zonefile chunk. */
418         uint8_t* data;
419         /** length of allocated data */
420         size_t len;
421 };
422
423 /**
424  * Create auth zones structure
425  */
426 struct auth_zones* auth_zones_create(void);
427
428 /**
429  * Apply configuration to auth zones.  Reads zonefiles.
430  * @param az: auth zones structure
431  * @param cfg: config to apply.
432  * @param setup: if true, also sets up values in the auth zones structure
433  * @return false on failure.
434  */
435 int auth_zones_apply_cfg(struct auth_zones* az, struct config_file* cfg,
436         int setup);
437
438 /** initial pick up of worker timeouts, ties events to worker event loop
439  * @param az: auth zones structure
440  * @param env: worker env, of first worker that receives the events (if any)
441  *      in its eventloop.
442  */
443 void auth_xfer_pickup_initial(struct auth_zones* az, struct module_env* env);
444
445 /**
446  * Cleanup auth zones.  This removes all events from event bases.
447  * Stops the xfr tasks.  But leaves zone data.
448  * @param az: auth zones structure.
449  */
450 void auth_zones_cleanup(struct auth_zones* az);
451
452 /**
453  * Delete auth zones structure
454  */
455 void auth_zones_delete(struct auth_zones* az);
456
457 /**
458  * Write auth zone data to file, in zonefile format.
459  */
460 int auth_zone_write_file(struct auth_zone* z, const char* fname);
461
462 /**
463  * Use auth zones to lookup the answer to a query.
464  * The query is from the iterator.  And the auth zones attempts to provide
465  * the answer instead of going to the internet.
466  *
467  * @param az: auth zones structure.
468  * @param qinfo: query info to lookup.
469  * @param region: region to use to allocate the reply in.
470  * @param msg: reply is stored here (if one).
471  * @param fallback: if true, fallback to making a query to the internet.
472  * @param dp_nm: name of delegation point to look for.  This zone is used
473  *      to answer the query.
474  *      If the dp_nm is not found, fallback is set to true and false returned.
475  * @param dp_nmlen: length of dp_nm.
476  * @return 0: failure (an error of some sort, like servfail).
477  *         if 0 and fallback is true, fallback to the internet.
478  *         if 0 and fallback is false, like getting servfail.
479  *         If true, an answer is available.
480  */
481 int auth_zones_lookup(struct auth_zones* az, struct query_info* qinfo,
482         struct regional* region, struct dns_msg** msg, int* fallback,
483         uint8_t* dp_nm, size_t dp_nmlen);
484
485 /**
486  * Answer query from auth zone.  Create authoritative answer.
487  * @param az: auth zones structure.
488  * @param env: the module environment.
489  * @param qinfo: query info (parsed).
490  * @param edns: edns info (parsed).
491  * @param buf: buffer with query ID and flags, also for reply.
492  * @param temp: temporary storage region.
493  * @return false if not answered
494  */
495 int auth_zones_answer(struct auth_zones* az, struct module_env* env,
496         struct query_info* qinfo, struct edns_data* edns, struct sldns_buffer* buf,
497         struct regional* temp);
498
499 /** 
500  * Find the auth zone that is above the given qname.
501  * Return NULL when there is no auth_zone above the give name, otherwise
502  * returns the closest auth_zone above the qname that pertains to it.
503  * @param az: auth zones structure.
504  * @param name: query to look up for.
505  * @param name_len: length of name.
506  * @param dclass: class of zone to find.
507  * @return NULL or auth_zone that pertains to the query.
508  */
509 struct auth_zone* auth_zones_find_zone(struct auth_zones* az,
510         uint8_t* name, size_t name_len, uint16_t dclass);
511
512 /** find an auth zone by name (exact match by name or NULL returned) */
513 struct auth_zone* auth_zone_find(struct auth_zones* az, uint8_t* nm,
514         size_t nmlen, uint16_t dclass);
515
516 /** find an xfer zone by name (exact match by name or NULL returned) */
517 struct auth_xfer* auth_xfer_find(struct auth_zones* az, uint8_t* nm,
518         size_t nmlen, uint16_t dclass);
519
520 /** create an auth zone. returns wrlocked zone. caller must have wrlock
521  * on az. returns NULL on malloc failure */
522 struct auth_zone* auth_zone_create(struct auth_zones* az, uint8_t* nm,
523         size_t nmlen, uint16_t dclass);
524
525 /** set auth zone zonefile string. caller must have lock on zone */
526 int auth_zone_set_zonefile(struct auth_zone* z, char* zonefile);
527
528 /** set auth zone fallback. caller must have lock on zone.
529  * fallbackstr is "yes" or "no". false on parse failure. */
530 int auth_zone_set_fallback(struct auth_zone* z, char* fallbackstr);
531
532 /** see if the auth zone for the name can fallback
533  * @param az: auth zones
534  * @param nm: name of delegation point.
535  * @param nmlen: length of nm.
536  * @param dclass: class of zone to look for.
537  * @return true if fallback_enabled is true. false if not.
538  * if the zone does not exist, fallback is true (more lenient)
539  * also true if zone does not do upstream requests.
540  */
541 int auth_zones_can_fallback(struct auth_zones* az, uint8_t* nm, size_t nmlen,
542         uint16_t dclass);
543
544 /** read auth zone from zonefile. caller must lock zone. false on failure */
545 int auth_zone_read_zonefile(struct auth_zone* z);
546
547 /** compare auth_zones for sorted rbtree */
548 int auth_zone_cmp(const void* z1, const void* z2);
549
550 /** compare auth_data for sorted rbtree */
551 int auth_data_cmp(const void* z1, const void* z2);
552
553 /** compare auth_xfer for sorted rbtree */
554 int auth_xfer_cmp(const void* z1, const void* z2);
555
556 /** Create auth_xfer structure.
557  * Caller must have wrlock on az. Returns locked xfer zone.
558  * @param az: zones structure.
559  * @param z: zone with name and class
560  * @return xfer zone or NULL
561  */
562 struct auth_xfer* auth_xfer_create(struct auth_zones* az, struct auth_zone* z);
563
564 /**
565  * Set masters in auth xfer structure from config.
566  * @param list: pointer to start of list.  The malloced list is returned here.
567  * @param c: the config items to copy over.
568  * @param with_http: if true, http urls are also included, before the masters.
569  * @return false on failure.
570  */
571 int xfer_set_masters(struct auth_master** list, struct config_auth* c,
572         int with_http);
573
574 /** xfer nextprobe timeout callback, this is part of task_nextprobe */
575 void auth_xfer_timer(void* arg);
576
577 /** callback for commpoint udp replies to task_probe */
578 int auth_xfer_probe_udp_callback(struct comm_point* c, void* arg, int err,
579         struct comm_reply* repinfo);
580 /** callback for task_transfer tcp connections */
581 int auth_xfer_transfer_tcp_callback(struct comm_point* c, void* arg, int err,
582         struct comm_reply* repinfo);
583 /** callback for task_transfer http connections */
584 int auth_xfer_transfer_http_callback(struct comm_point* c, void* arg, int err,
585         struct comm_reply* repinfo);
586 /** xfer probe timeout callback, part of task_probe */
587 void auth_xfer_probe_timer_callback(void* arg);
588 /** mesh callback for task_probe on lookup of host names */
589 void auth_xfer_probe_lookup_callback(void* arg, int rcode,
590         struct sldns_buffer* buf, enum sec_status sec, char* why_bogus);
591 /** mesh callback for task_transfer on lookup of host names */
592 void auth_xfer_transfer_lookup_callback(void* arg, int rcode,
593         struct sldns_buffer* buf, enum sec_status sec, char* why_bogus);
594
595 /*
596  * Compares two 32-bit serial numbers as defined in RFC1982.  Returns
597  * <0 if a < b, 0 if a == b, and >0 if a > b.  The result is undefined
598  * if a != b but neither is greater or smaller (see RFC1982 section
599  * 3.2.).
600  */
601 int compare_serial(uint32_t a, uint32_t b);
602
603 #endif /* SERVICES_AUTHZONE_H */