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 #include "services/rpz.h"
50 struct ub_packed_rrset_key;
63 struct auth_nextprobe;
70 * Authoritative zones, shared.
73 /** lock on the authzone trees */
75 /** rbtree of struct auth_zone */
77 /** rbtree of struct auth_xfer */
79 /** do we have downstream enabled */
81 /** number of queries upstream */
83 /** number of queries downstream */
84 size_t num_query_down;
85 /** first rpz item in linked list */
86 struct rpz* rpz_first;
87 /** rw lock for rpz linked list, needed when iterating or editing linked
89 lock_rw_type rpz_lock;
93 * Auth zone. Authoritative data, that is fetched from instead of sending
94 * packets to the internet.
97 /** rbtree node, key is name and class */
100 /** zone name, in uncompressed wireformat */
102 /** length of zone name */
104 /** number of labels in zone name */
106 /** the class of this zone, in host byteorder.
107 * uses 'dclass' to not conflict with c++ keyword class. */
110 /** lock on the data in the structure
111 * For the node, parent, name, namelen, namelabs, dclass, you
112 * need to also hold the zones_tree lock to change them (or to
113 * delete this zone) */
116 /** auth data for this zone
117 * rbtree of struct auth_data */
120 /** zonefile name (or NULL for no zonefile) */
122 /** fallback to the internet on failure or ttl-expiry of auth zone */
123 int fallback_enabled;
124 /** the zone has expired (enabled by the xfer worker), fallback
125 * happens if that option is enabled. */
127 /** zone is a slave zone (it has masters) */
129 /** for downstream: this zone answers queries towards the downstream
132 /** for upstream: this zone answers queries that unbound intends to
137 /** zone has been deleted */
139 /** deletelist pointer, unused normally except during delete */
140 struct auth_zone* delete_next;
144 * Auth data. One domain name, and the RRs to go with it.
147 /** rbtree node, key is name only */
151 /** length of name */
153 /** number of labels in name */
155 /** the data rrsets, with different types, linked list.
156 * if the list if NULL the node would be an empty non-terminal,
157 * but in this data structure such nodes that represent an empty
158 * non-terminal are not needed; they just don't exist.
160 struct auth_rrset* rrsets;
168 struct auth_rrset* next;
169 /** RR type in host byteorder */
171 /** RRset data item */
172 struct packed_rrset_data* data;
176 * Authoritative zone transfer structure.
177 * Create and destroy needs the auth_zones* biglock.
178 * The structure consists of different tasks. Each can be unowned (-1) or
179 * owner by a worker (worker-num). A worker can pick up a task and then do
180 * it. This means the events (timeouts, sockets) are for that worker.
182 * (move this to tasks).
183 * They don't have locks themselves, the worker (that owns it) uses it,
184 * also as part of callbacks, hence it has separate zonename pointers for
185 * lookup in the main zonetree. If the zone has no transfers, this
186 * structure is not created.
189 /** rbtree node, key is name and class */
192 /** lock on this structure, and on the workernum elements of the
193 * tasks. First hold the tree-lock in auth_zones, find the auth_xfer,
194 * lock this lock. Then a worker can reassign itself to fill up
196 * Once it has the task assigned to it, the worker can access the
197 * other elements of the task structure without a lock, because that
198 * is necessary for the eventloop and callbacks from that. */
199 lock_basic_type lock;
201 /** zone name, in uncompressed wireformat */
203 /** length of zone name */
205 /** number of labels in zone name */
207 /** the class of this zone, in host byteorder.
208 * uses 'dclass' to not conflict with c++ keyword class. */
211 /** task to wait for next-probe-timeout,
212 * once timeouted, see if a SOA probe is needed, or already
214 struct auth_nextprobe* task_nextprobe;
216 /** task for SOA probe. Check if the zone can be updated */
217 struct auth_probe* task_probe;
219 /** Task for transfer. Transferring and updating the zone. This
220 * includes trying (potentially) several upstream masters. Downloading
221 * and storing the zone */
222 struct auth_transfer* task_transfer;
224 /** a notify was received, but a zone transfer or probe was already
226 * However, the zone transfer could signal a newer serial number.
227 * The serial number of that notify is saved below. The transfer and
228 * probe tasks should check this once done to see if they need to
229 * restart the transfer task for the newer notify serial.
230 * Hold the lock to access this member (and the serial).
233 /** true if the notify_received has a serial number */
234 int notify_has_serial;
235 /** serial number of the notify */
236 uint32_t notify_serial;
237 /** the list of masters for checking notifies. This list is
238 * empty on start, and a copy of the list from the probe_task when
239 * it is done looking them up. */
240 struct auth_master* allow_notify_list;
242 /* protected by the lock on the structure, information about
243 * the loaded authority zone. */
244 /** is the zone currently considered expired? after expiry also older
245 * serial numbers are allowed (not just newer) */
247 /** do we have a zone (if 0, no zone data at all) */
250 /** current serial (from SOA), if we have no zone, 0 */
252 /** retry time (from SOA), time to wait with next_probe
253 * if no master responds */
255 /** refresh time (from SOA), time to wait with next_probe
256 * if everything is fine */
258 /** expiry time (from SOA), time until zone data is not considered
259 * valid any more, if no master responds within this time, either
260 * with the current zone or a new zone. */
263 /** zone lease start time (start+expiry is expiration time).
264 * this is renewed every SOA probe and transfer. On zone load
265 * from zonefile it is also set (with probe set soon to check) */
270 * The next probe task.
271 * This task consists of waiting for the probetimeout. It is a task because
272 * it needs an event in the eventtable. Once the timeout has passed, that
273 * worker can (potentially) become the auth_probe worker, or if another worker
274 * is already doing that, do nothing. Tasks becomes unowned.
275 * The probe worker, if it detects nothing has to be done picks up this task,
278 struct auth_nextprobe {
279 /* Worker pointer. NULL means unowned. */
280 struct worker* worker;
281 /* module env for this task */
282 struct module_env* env;
284 /** increasing backoff for failures */
286 /** Timeout for next probe (for SOA) */
288 /** timeout callback for next_probe or expiry(if that is sooner).
289 * it is on the worker's event_base */
290 struct comm_timer* timer;
295 * Send a SOA UDP query to see if the zone needs to be updated (or similar,
296 * potential, HTTP probe query) and check serial number.
297 * If yes, start the auth_transfer task. If no, make sure auth_nextprobe
298 * timeout wait task is running.
299 * Needs to be a task, because the UDP query needs an event entry.
300 * This task could also be started by eg. a NOTIFY being received, even though
301 * another worker is performing the nextprobe task (and that worker keeps
302 * waiting uninterrupted).
305 /* Worker pointer. NULL means unowned. */
306 struct worker* worker;
307 /* module env for this task */
308 struct module_env* env;
310 /** list of upstream masters for this zone, from config */
311 struct auth_master* masters;
313 /** for the hostname lookups, which master is current */
314 struct auth_master* lookup_target;
315 /** are we looking up A or AAAA, first A, then AAAA (if ip6 enabled) */
317 /** we only want to do lookups for making config work (for notify),
318 * don't proceed with UDP SOA probe queries */
320 /** we have seen a new lease this scan, because one of the masters
321 * replied with the current SOA serial version */
324 /** once notified, or the timeout has been reached. a scan starts. */
325 /** the scan specific target (notify source), or NULL if none */
326 struct auth_master* scan_specific;
327 /** scan tries all the upstream masters. the scan current target.
328 * or NULL if not working on sequential scan */
329 struct auth_master* scan_target;
330 /** if not NULL, the specific addr for the current master */
331 struct auth_addr* scan_addr;
333 /** dns id of packet in flight */
335 /** the SOA probe udp event.
336 * on the workers event base. */
337 struct comm_point* cp;
338 /** is the cp for ip6 or ip4 */
340 /** timeout for packets.
341 * on the workers event base. */
342 struct comm_timer* timer;
343 /** timeout in msec */
349 * Once done, make sure the nextprobe waiting task is running, whether done
350 * with failure or success. If failure, use shorter timeout for wait time.
352 struct auth_transfer {
353 /* Worker pointer. NULL means unowned. */
354 struct worker* worker;
355 /* module env for this task */
356 struct module_env* env;
358 /** xfer data that has been transferred, the data is applied
359 * once the transfer has completed correctly */
360 struct auth_chunk* chunks_first;
361 /** last element in chunks list (to append new data at the end) */
362 struct auth_chunk* chunks_last;
364 /** list of upstream masters for this zone, from config */
365 struct auth_master* masters;
367 /** for the hostname lookups, which master is current */
368 struct auth_master* lookup_target;
369 /** are we looking up A or AAAA, first A, then AAAA (if ip6 enabled) */
372 /** once notified, or the timeout has been reached. a scan starts. */
373 /** the scan specific target (notify source), or NULL if none */
374 struct auth_master* scan_specific;
375 /** scan tries all the upstream masters. the scan current target.
376 * or NULL if not working on sequential scan */
377 struct auth_master* scan_target;
378 /** what address we are scanning for the master, or NULL if the
379 * master is in IP format itself */
380 struct auth_addr* scan_addr;
381 /** the zone transfer in progress (or NULL if in scan). It is
382 * from this master */
383 struct auth_master* master;
385 /** failed ixfr transfer, retry with axfr (to the current master),
386 * the IXFR was 'REFUSED', 'SERVFAIL', 'NOTIMPL' or the contents of
387 * the IXFR did not apply cleanly (out of sync, delete of nonexistent
388 * data or add of duplicate data). Flag is cleared once the retry
389 * with axfr is done. */
391 /** we saw an ixfr-indicating timeout, count of them */
392 int ixfr_possible_timeout_count;
393 /** we are doing IXFR right now */
395 /** did we detect the current AXFR/IXFR serial number yet, 0 not yet,
396 * 1 we saw the first, 2 we saw the second, 3 must be last SOA in xfr*/
398 /** number of RRs scanned for AXFR/IXFR detection */
400 /** we are doing an IXFR but we detected an AXFR contents */
402 /** the serial number for the current AXFR/IXFR incoming reply,
403 * for IXFR, the outermost SOA records serial */
404 uint32_t incoming_xfr_serial;
406 /** dns id of AXFR query */
408 /** the transfer (TCP) to the master.
409 * on the workers event base. */
410 struct comm_point* cp;
411 /** timeout for the transfer.
412 * on the workers event base. */
413 struct comm_timer* timer;
416 /** list of addresses */
419 struct auth_addr* next;
421 struct sockaddr_storage addr;
426 /** auth zone master upstream, and the config settings for it */
428 /** next master in list */
429 struct auth_master* next;
430 /** master IP address (and port), or hostname, string */
432 /** for http, filename */
434 /** use HTTP for this master */
436 /** use IXFR for this master */
438 /** this is an allow notify member, the master can send notifies
439 * to us, but we don't send SOA probes, or zone transfer from it */
441 /** use ssl for channel */
443 /** the port number (for urls) */
445 /** if the host is a hostname, the list of resolved addrs, if any*/
446 struct auth_addr* list;
449 /** auth zone master zone transfer data chunk */
451 /** next chunk in list */
452 struct auth_chunk* next;
453 /** the data from this chunk, this is what was received.
454 * for an IXFR that means results from comm_net tcp actions,
455 * packets. also for an AXFR. For HTTP a zonefile chunk. */
457 /** length of allocated data */
462 * Create auth zones structure
464 struct auth_zones* auth_zones_create(void);
467 * Apply configuration to auth zones. Reads zonefiles.
468 * @param az: auth zones structure
469 * @param cfg: config to apply.
470 * @param setup: if true, also sets up values in the auth zones structure
471 * @param is_rpz: set to 1 if at least one RPZ zone is configured.
472 * @return false on failure.
474 int auth_zones_apply_cfg(struct auth_zones* az, struct config_file* cfg,
475 int setup, int* is_rpz);
477 /** initial pick up of worker timeouts, ties events to worker event loop
478 * @param az: auth zones structure
479 * @param env: worker env, of first worker that receives the events (if any)
482 void auth_xfer_pickup_initial(struct auth_zones* az, struct module_env* env);
485 * Cleanup auth zones. This removes all events from event bases.
486 * Stops the xfr tasks. But leaves zone data.
487 * @param az: auth zones structure.
489 void auth_zones_cleanup(struct auth_zones* az);
492 * Delete auth zones structure
494 void auth_zones_delete(struct auth_zones* az);
497 * Write auth zone data to file, in zonefile format.
499 int auth_zone_write_file(struct auth_zone* z, const char* fname);
502 * Use auth zones to lookup the answer to a query.
503 * The query is from the iterator. And the auth zones attempts to provide
504 * the answer instead of going to the internet.
506 * @param az: auth zones structure.
507 * @param qinfo: query info to lookup.
508 * @param region: region to use to allocate the reply in.
509 * @param msg: reply is stored here (if one).
510 * @param fallback: if true, fallback to making a query to the internet.
511 * @param dp_nm: name of delegation point to look for. This zone is used
512 * to answer the query.
513 * If the dp_nm is not found, fallback is set to true and false returned.
514 * @param dp_nmlen: length of dp_nm.
515 * @return 0: failure (an error of some sort, like servfail).
516 * if 0 and fallback is true, fallback to the internet.
517 * if 0 and fallback is false, like getting servfail.
518 * If true, an answer is available.
520 int auth_zones_lookup(struct auth_zones* az, struct query_info* qinfo,
521 struct regional* region, struct dns_msg** msg, int* fallback,
522 uint8_t* dp_nm, size_t dp_nmlen);
525 * Answer query from auth zone. Create authoritative answer.
526 * @param az: auth zones structure.
527 * @param env: the module environment.
528 * @param qinfo: query info (parsed).
529 * @param edns: edns info (parsed).
530 * @param buf: buffer with query ID and flags, also for reply.
531 * @param repinfo: reply information for a communication point.
532 * @param temp: temporary storage region.
533 * @return false if not answered
535 int auth_zones_answer(struct auth_zones* az, struct module_env* env,
536 struct query_info* qinfo, struct edns_data* edns,
537 struct comm_reply* repinfo, struct sldns_buffer* buf, struct regional* temp);
540 * Find the auth zone that is above the given qname.
541 * Return NULL when there is no auth_zone above the give name, otherwise
542 * returns the closest auth_zone above the qname that pertains to it.
543 * @param az: auth zones structure.
544 * @param name: query to look up for.
545 * @param name_len: length of name.
546 * @param dclass: class of zone to find.
547 * @return NULL or auth_zone that pertains to the query.
549 struct auth_zone* auth_zones_find_zone(struct auth_zones* az,
550 uint8_t* name, size_t name_len, uint16_t dclass);
552 /** find an auth zone by name (exact match by name or NULL returned) */
553 struct auth_zone* auth_zone_find(struct auth_zones* az, uint8_t* nm,
554 size_t nmlen, uint16_t dclass);
556 /** find an xfer zone by name (exact match by name or NULL returned) */
557 struct auth_xfer* auth_xfer_find(struct auth_zones* az, uint8_t* nm,
558 size_t nmlen, uint16_t dclass);
560 /** create an auth zone. returns wrlocked zone. caller must have wrlock
561 * on az. returns NULL on malloc failure */
562 struct auth_zone* auth_zone_create(struct auth_zones* az, uint8_t* nm,
563 size_t nmlen, uint16_t dclass);
565 /** set auth zone zonefile string. caller must have lock on zone */
566 int auth_zone_set_zonefile(struct auth_zone* z, char* zonefile);
568 /** set auth zone fallback. caller must have lock on zone.
569 * fallbackstr is "yes" or "no". false on parse failure. */
570 int auth_zone_set_fallback(struct auth_zone* z, char* fallbackstr);
572 /** see if the auth zone for the name can fallback
573 * @param az: auth zones
574 * @param nm: name of delegation point.
575 * @param nmlen: length of nm.
576 * @param dclass: class of zone to look for.
577 * @return true if fallback_enabled is true. false if not.
578 * if the zone does not exist, fallback is true (more lenient)
579 * also true if zone does not do upstream requests.
581 int auth_zones_can_fallback(struct auth_zones* az, uint8_t* nm, size_t nmlen,
584 /** process notify for auth zones.
585 * first checks the access list. Then processes the notify. This starts
586 * the probe sequence or it notes the serial number (if any)
587 * @param az: auth zones structure.
588 * @param env: module env of the worker that is handling the notify. it will
589 * pick up the task probe (or transfer), unless already in progress by
591 * @param nm: name of the zone. Uncompressed. from query.
592 * @param nmlen: length of name.
593 * @param dclass: class of zone.
594 * @param addr: source address of notify
595 * @param addrlen: length of addr.
596 * @param has_serial: if true, the notify has a serial attached.
597 * @param serial: the serial number, if has_serial is true.
598 * @param refused: is set to true on failure to note refused access.
599 * @return fail on failures (refused is false) and when access is
600 * denied (refused is true). True when processed.
602 int auth_zones_notify(struct auth_zones* az, struct module_env* env,
603 uint8_t* nm, size_t nmlen, uint16_t dclass,
604 struct sockaddr_storage* addr, socklen_t addrlen, int has_serial,
605 uint32_t serial, int* refused);
607 /** process notify packet and read serial number from SOA.
608 * returns 0 if no soa record in the notify */
609 int auth_zone_parse_notify_serial(struct sldns_buffer* pkt, uint32_t *serial);
611 /** for the zone and if not already going, starts the probe sequence.
612 * false if zone cannot be found. This is like a notify arrived and was
613 * accepted for that zone. */
614 int auth_zones_startprobesequence(struct auth_zones* az,
615 struct module_env* env, uint8_t* nm, size_t nmlen, uint16_t dclass);
617 /** read auth zone from zonefile. caller must lock zone. false on failure */
618 int auth_zone_read_zonefile(struct auth_zone* z, struct config_file* cfg);
620 /** find serial number of zone or false if none (no SOA record) */
621 int auth_zone_get_serial(struct auth_zone* z, uint32_t* serial);
623 /** compare auth_zones for sorted rbtree */
624 int auth_zone_cmp(const void* z1, const void* z2);
626 /** compare auth_data for sorted rbtree */
627 int auth_data_cmp(const void* z1, const void* z2);
629 /** compare auth_xfer for sorted rbtree */
630 int auth_xfer_cmp(const void* z1, const void* z2);
632 /** Create auth_xfer structure.
633 * Caller must have wrlock on az. Returns locked xfer zone.
634 * @param az: zones structure.
635 * @param z: zone with name and class
636 * @return xfer zone or NULL
638 struct auth_xfer* auth_xfer_create(struct auth_zones* az, struct auth_zone* z);
641 * Set masters in auth xfer structure from config.
642 * @param list: pointer to start of list. The malloced list is returned here.
643 * @param c: the config items to copy over.
644 * @param with_http: if true, http urls are also included, before the masters.
645 * @return false on failure.
647 int xfer_set_masters(struct auth_master** list, struct config_auth* c,
650 /** xfer nextprobe timeout callback, this is part of task_nextprobe */
651 void auth_xfer_timer(void* arg);
653 /** callback for commpoint udp replies to task_probe */
654 int auth_xfer_probe_udp_callback(struct comm_point* c, void* arg, int err,
655 struct comm_reply* repinfo);
656 /** callback for task_transfer tcp connections */
657 int auth_xfer_transfer_tcp_callback(struct comm_point* c, void* arg, int err,
658 struct comm_reply* repinfo);
659 /** callback for task_transfer http connections */
660 int auth_xfer_transfer_http_callback(struct comm_point* c, void* arg, int err,
661 struct comm_reply* repinfo);
662 /** xfer probe timeout callback, part of task_probe */
663 void auth_xfer_probe_timer_callback(void* arg);
664 /** xfer transfer timeout callback, part of task_transfer */
665 void auth_xfer_transfer_timer_callback(void* arg);
666 /** mesh callback for task_probe on lookup of host names */
667 void auth_xfer_probe_lookup_callback(void* arg, int rcode,
668 struct sldns_buffer* buf, enum sec_status sec, char* why_bogus,
669 int was_ratelimited);
670 /** mesh callback for task_transfer on lookup of host names */
671 void auth_xfer_transfer_lookup_callback(void* arg, int rcode,
672 struct sldns_buffer* buf, enum sec_status sec, char* why_bogus,
673 int was_ratelimited);
676 * Compares two 32-bit serial numbers as defined in RFC1982. Returns
677 * <0 if a < b, 0 if a == b, and >0 if a > b. The result is undefined
678 * if a != b but neither is greater or smaller (see RFC1982 section
681 int compare_serial(uint32_t a, uint32_t b);
683 #endif /* SERVICES_AUTHZONE_H */