2 * services/authzone.h - authoritative zone that is locally hosted.
4 * Copyright (c) 2017, NLnet Labs. All rights reserved.
6 * This software is open source.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * Redistributions of source code must retain the above copyright notice,
13 * this list of conditions and the following disclaimer.
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.
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.
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.
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.
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;
62 struct auth_nextprobe;
69 * Authoritative zones, shared.
72 /** lock on the authzone trees */
74 /** rbtree of struct auth_zone */
76 /** rbtree of struct auth_xfer */
78 /** do we have downstream enabled */
83 * Auth zone. Authoritative data, that is fetched from instead of sending
84 * packets to the internet.
87 /** rbtree node, key is name and class */
90 /** zone name, in uncompressed wireformat */
92 /** length of zone name */
94 /** number of labels in zone name */
96 /** the class of this zone, in host byteorder.
97 * uses 'dclass' to not conflict with c++ keyword class. */
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) */
106 /** auth data for this zone
107 * rbtree of struct auth_data */
110 /** zonefile name (or NULL for no 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. */
117 /** zone is a slave zone (it has masters) */
119 /** for downstream: this zone answers queries towards the downstream
122 /** for upstream: this zone answers queries that unbound intends to
128 * Auth data. One domain name, and the RRs to go with it.
131 /** rbtree node, key is name only */
135 /** length of name */
137 /** number of labels in name */
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.
144 struct auth_rrset* rrsets;
152 struct auth_rrset* next;
153 /** RR type in host byteorder */
155 /** RRset data item */
156 struct packed_rrset_data* data;
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.
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.
173 /** rbtree node, key is name and class */
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
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;
185 /** zone name, in uncompressed wireformat */
187 /** length of zone name */
189 /** number of labels in zone name */
191 /** the class of this zone, in host byteorder.
192 * uses 'dclass' to not conflict with c++ keyword class. */
195 /** task to wait for next-probe-timeout,
196 * once timeouted, see if a SOA probe is needed, or already
198 struct auth_nextprobe* task_nextprobe;
200 /** task for SOA probe. Check if the zone can be updated */
201 struct auth_probe* task_probe;
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;
208 /** a notify was received, but a zone transfer or probe was already
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).
217 /** serial number of the notify */
218 uint32_t notify_serial;
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) */
225 /** do we have a zone (if 0, no zone data at all) */
228 /** current serial (from SOA), if we have no zone, 0 */
230 /** retry time (from SOA), time to wait with next_probe
231 * if no master responds */
233 /** refresh time (from SOA), time to wait with next_probe
234 * if everything is fine */
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. */
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) */
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,
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;
262 /** increasing backoff for failures */
264 /** Timeout for next probe (for SOA) */
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;
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).
283 /* Worker pointer. NULL means unowned. */
284 struct worker* worker;
285 /* module env for this task */
286 struct module_env* env;
288 /** list of upstream masters for this zone, from config */
289 struct auth_master* masters;
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) */
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;
305 /** dns id of packet in flight */
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 */
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.
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;
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;
334 /** list of upstream masters for this zone, from config */
335 struct auth_master* masters;
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) */
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;
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. */
361 /** we are doing IXFR right now */
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*/
366 /** number of RRs scanned for AXFR/IXFR detection */
368 /** we are doing an IXFR but we detected an AXFR contents */
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;
374 /** dns id of AXFR query */
376 /** the transfer (TCP) to the master.
377 * on the workers event base. */
378 struct comm_point* cp;
381 /** list of addresses */
384 struct auth_addr* next;
386 struct sockaddr_storage addr;
391 /** auth zone master upstream, and the config settings for it */
393 /** next master in list */
394 struct auth_master* next;
395 /** master IP address (and port), or hostname, string */
397 /** for http, filename */
399 /** use HTTP for this master */
401 /** use IXFR for this master */
403 /** use ssl for channel */
405 /** the port number (for urls) */
407 /** if the host is a hostname, the list of resolved addrs, if any*/
408 struct auth_addr* list;
411 /** auth zone master zone transfer data 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. */
419 /** length of allocated data */
424 * Create auth zones structure
426 struct auth_zones* auth_zones_create(void);
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.
435 int auth_zones_apply_cfg(struct auth_zones* az, struct config_file* cfg,
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)
443 void auth_xfer_pickup_initial(struct auth_zones* az, struct module_env* env);
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.
450 void auth_zones_cleanup(struct auth_zones* az);
453 * Delete auth zones structure
455 void auth_zones_delete(struct auth_zones* az);
458 * Write auth zone data to file, in zonefile format.
460 int auth_zone_write_file(struct auth_zone* z, const char* fname);
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.
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.
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);
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
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);
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.
509 struct auth_zone* auth_zones_find_zone(struct auth_zones* az,
510 uint8_t* name, size_t name_len, uint16_t dclass);
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);
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);
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);
525 /** set auth zone zonefile string. caller must have lock on zone */
526 int auth_zone_set_zonefile(struct auth_zone* z, char* zonefile);
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);
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.
541 int auth_zones_can_fallback(struct auth_zones* az, uint8_t* nm, size_t nmlen,
544 /** read auth zone from zonefile. caller must lock zone. false on failure */
545 int auth_zone_read_zonefile(struct auth_zone* z);
547 /** compare auth_zones for sorted rbtree */
548 int auth_zone_cmp(const void* z1, const void* z2);
550 /** compare auth_data for sorted rbtree */
551 int auth_data_cmp(const void* z1, const void* z2);
553 /** compare auth_xfer for sorted rbtree */
554 int auth_xfer_cmp(const void* z1, const void* z2);
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
562 struct auth_xfer* auth_xfer_create(struct auth_zones* az, struct auth_zone* z);
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.
571 int xfer_set_masters(struct auth_master** list, struct config_auth* c,
574 /** xfer nextprobe timeout callback, this is part of task_nextprobe */
575 void auth_xfer_timer(void* arg);
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);
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
601 int compare_serial(uint32_t a, uint32_t b);
603 #endif /* SERVICES_AUTHZONE_H */