2 * services/localzone.h - local zones authority service.
4 * Copyright (c) 2007, 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 functions to enable local zone authority service.
42 #ifndef SERVICES_LOCALZONE_H
43 #define SERVICES_LOCALZONE_H
44 #include "util/rbtree.h"
45 #include "util/locks.h"
46 #include "util/storage/dnstree.h"
47 #include "util/module.h"
48 #include "services/view.h"
49 #include "sldns/sbuffer.h"
50 struct packed_rrset_data;
51 struct ub_packed_rrset_key;
58 struct config_strlist;
62 * This type determines processing for queries that did not match
63 * local-data directly.
66 /** unset type, used for unset tag_action elements */
70 /** answer with error */
72 /** answer nxdomain or nodata */
74 /** resolve normally */
75 local_zone_transparent,
76 /** do not block types at localdata names */
77 local_zone_typetransparent,
78 /** answer with data at zone apex */
80 /** remove default AS112 blocking contents for zone
81 * nodefault is used in config not during service. */
83 /** log client address, but no block (transparent) */
85 /** log client address, and block (drop) */
86 local_zone_inform_deny,
87 /** log client address, and direct */
88 local_zone_inform_redirect,
89 /** resolve normally, even when there is local data */
90 local_zone_always_transparent,
91 /** answer with error, even when there is local data */
92 local_zone_always_refuse,
93 /** answer with nxdomain, even when there is local data */
94 local_zone_always_nxdomain,
95 /** answer with noerror/nodata, even when there is local data */
96 local_zone_always_nodata,
97 /** drop query, even when there is local data */
98 local_zone_always_deny,
99 /** answer not from the view, but global or no-answer */
101 /** Invalid type, cannot be used to generate answer */
106 * Authoritative local zones storage, shared.
109 /** lock on the localzone tree */
111 /** rbtree of struct local_zone */
116 * Local zone. A locally served authoritative zone.
119 /** rbtree node, key is name and class */
121 /** parent zone, if any. */
122 struct local_zone* parent;
124 /** zone name, in uncompressed wireformat */
126 /** length of zone name */
128 /** number of labels in zone name */
130 /** the class of this zone.
131 * uses 'dclass' to not conflict with c++ keyword class. */
134 /** lock on the data in the structure
135 * For the node, parent, name, namelen, namelabs, dclass, you
136 * need to also hold the zones_tree lock to change them (or to
137 * delete this zone) */
140 /** how to process zone */
141 enum localzone_type type;
144 /** length of the taglist (in bytes) */
146 /** netblock addr_tree with struct local_zone_override information
147 * or NULL if there are no override elements */
148 struct rbtree_type* override_tree;
150 /** in this region the zone's data is allocated.
151 * the struct local_zone itself is malloced. */
152 struct regional* region;
153 /** local data for this zone
154 * rbtree of struct local_data */
156 /** if data contains zone apex SOA data, this is a ptr to it. */
157 struct ub_packed_rrset_key* soa;
161 * Local data. One domain name, and the RRs to go with it.
164 /** rbtree node, key is name only */
168 /** length of name */
170 /** number of labels in name */
172 /** the data rrsets, with different types, linked list.
173 * If this list is NULL, the node is an empty non-terminal. */
174 struct local_rrset* rrsets;
182 struct local_rrset* next;
183 /** RRset data item */
184 struct ub_packed_rrset_key* rrset;
188 * Local zone override information
190 struct local_zone_override {
191 /** node in addrtree */
192 struct addr_tree_node node;
193 /** override for local zone type */
194 enum localzone_type type;
198 * Create local zones storage
199 * @return new struct or NULL on error.
201 struct local_zones* local_zones_create(void);
204 * Delete local zones storage
205 * @param zones: to delete.
207 void local_zones_delete(struct local_zones* zones);
210 * Apply config settings; setup the local authoritative data.
211 * Takes care of locking.
212 * @param zones: is set up.
213 * @param cfg: config data.
214 * @return false on error.
216 int local_zones_apply_cfg(struct local_zones* zones, struct config_file* cfg);
219 * Compare two local_zone entries in rbtree. Sort hierarchical but not
223 * @return: -1, 0, +1 comparison value.
225 int local_zone_cmp(const void* z1, const void* z2);
228 * Compare two local_data entries in rbtree. Sort canonical.
231 * @return: -1, 0, +1 comparison value.
233 int local_data_cmp(const void* d1, const void* d2);
237 * @param z: to delete.
239 void local_zone_delete(struct local_zone* z);
242 * Lookup zone that contains the given name, class and taglist.
243 * User must lock the tree or result zone.
244 * @param zones: the zones tree
245 * @param name: dname to lookup
246 * @param len: length of name.
247 * @param labs: labelcount of name.
248 * @param dclass: class to lookup.
249 * @param dtype: type to lookup, if type DS a zone higher is used for zonecuts.
250 * @param taglist: taglist to lookup.
251 * @param taglen: lenth of taglist.
252 * @param ignoretags: lookup zone by name and class, regardless the
254 * @return closest local_zone or NULL if no covering zone is found.
256 struct local_zone* local_zones_tags_lookup(struct local_zones* zones,
257 uint8_t* name, size_t len, int labs, uint16_t dclass, uint16_t dtype,
258 uint8_t* taglist, size_t taglen, int ignoretags);
261 * Lookup zone that contains the given name, class.
262 * User must lock the tree or result zone.
263 * @param zones: the zones tree
264 * @param name: dname to lookup
265 * @param len: length of name.
266 * @param labs: labelcount of name.
267 * @param dclass: class to lookup.
268 * @param dtype: type of the record, if type DS then a zone higher up is found
269 * pass 0 to just plain find a zone for a name.
270 * @return closest local_zone or NULL if no covering zone is found.
272 struct local_zone* local_zones_lookup(struct local_zones* zones,
273 uint8_t* name, size_t len, int labs, uint16_t dclass, uint16_t dtype);
276 * Debug helper. Print all zones
277 * Takes care of locking.
278 * @param zones: the zones tree
280 void local_zones_print(struct local_zones* zones);
283 * Answer authoritatively for local zones.
284 * Takes care of locking.
285 * @param zones: the stored zones (shared, read only).
286 * @param env: the module environment.
287 * @param qinfo: query info (parsed).
288 * @param edns: edns info (parsed).
289 * @param buf: buffer with query ID and flags, also for reply.
290 * @param temp: temporary storage region.
291 * @param repinfo: source address for checks. may be NULL.
292 * @param taglist: taglist for checks. May be NULL.
293 * @param taglen: length of the taglist.
294 * @param tagactions: local zone actions for tags. May be NULL.
295 * @param tagactionssize: length of the tagactions.
296 * @param tag_datas: array per tag of strlist with rdata strings. or NULL.
297 * @param tag_datas_size: size of tag_datas array.
298 * @param tagname: array of tag name strings (for debug output).
299 * @param num_tags: number of items in tagname array.
300 * @param view: answer using this view. May be NULL.
301 * @return true if answer is in buffer. false if query is not answered
302 * by authority data. If the reply should be dropped altogether, the return
303 * value is true, but the buffer is cleared (empty).
304 * It can also return true if a non-exact alias answer is found. In this
305 * case qinfo->local_alias points to the corresponding alias RRset but the
306 * answer is NOT encoded in buffer. It's the caller's responsibility to
307 * complete the alias chain (if needed) and encode the final set of answer.
308 * Data pointed to by qinfo->local_alias is allocated in 'temp' or refers to
309 * configuration data. So the caller will need to make a deep copy of it
310 * if it needs to keep it beyond the lifetime of 'temp' or a dynamic update
311 * to local zone data.
313 int local_zones_answer(struct local_zones* zones, struct module_env* env,
314 struct query_info* qinfo, struct edns_data* edns, struct sldns_buffer* buf,
315 struct regional* temp, struct comm_reply* repinfo, uint8_t* taglist,
316 size_t taglen, uint8_t* tagactions, size_t tagactionssize,
317 struct config_strlist** tag_datas, size_t tag_datas_size,
318 char** tagname, int num_tags, struct view* view);
321 * Answer using the local zone only (not local data used).
322 * @param z: zone for query.
323 * @param env: module environment.
324 * @param qinfo: query.
325 * @param edns: edns from query.
326 * @param repinfo: source address for checks. may be NULL.
327 * @param buf: buffer for answer.
328 * @param temp: temp region for encoding.
329 * @param ld: local data, if NULL, no such name exists in localdata.
330 * @param lz_type: type of the local zone.
331 * @return 1 if a reply is to be sent, 0 if not.
334 local_zones_zone_answer(struct local_zone* z, struct module_env* env,
335 struct query_info* qinfo, struct edns_data* edns,
336 struct comm_reply* repinfo, sldns_buffer* buf, struct regional* temp,
337 struct local_data* ld, enum localzone_type lz_type);
340 * Parse the string into localzone type.
342 * @param str: string to parse
343 * @param t: local zone type returned here.
344 * @return 0 on parse error.
346 int local_zone_str2type(const char* str, enum localzone_type* t);
349 * Print localzone type to a string. Pointer to a constant string.
351 * @param t: local zone type.
352 * @return constant string that describes type.
354 const char* local_zone_type2str(enum localzone_type t);
357 * Find zone that with exactly given name, class.
358 * User must lock the tree or result zone.
359 * @param zones: the zones tree
360 * @param name: dname to lookup
361 * @param len: length of name.
362 * @param labs: labelcount of name.
363 * @param dclass: class to lookup.
364 * @return the exact local_zone or NULL.
366 struct local_zone* local_zones_find(struct local_zones* zones,
367 uint8_t* name, size_t len, int labs, uint16_t dclass);
370 * Find zone that with exactly or smaller name/class
371 * User must lock the tree or result zone.
372 * @param zones: the zones tree
373 * @param name: dname to lookup
374 * @param len: length of name.
375 * @param labs: labelcount of name.
376 * @param dclass: class to lookup.
377 * @param exact: 1 on return is this is an exact match.
378 * @return the exact or smaller local_zone or NULL.
381 local_zones_find_le(struct local_zones* zones,
382 uint8_t* name, size_t len, int labs, uint16_t dclass,
386 * Add a new zone. Caller must hold the zones lock.
387 * Adjusts the other zones as well (parent pointers) after insertion.
388 * The zone must NOT exist (returns NULL and logs error).
389 * @param zones: the zones tree
390 * @param name: dname to add
391 * @param len: length of name.
392 * @param labs: labelcount of name.
393 * @param dclass: class to add.
395 * @return local_zone or NULL on error, caller must printout memory error.
397 struct local_zone* local_zones_add_zone(struct local_zones* zones,
398 uint8_t* name, size_t len, int labs, uint16_t dclass,
399 enum localzone_type tp);
402 * Delete a zone. Caller must hold the zones lock.
403 * Adjusts the other zones as well (parent pointers) after insertion.
404 * @param zones: the zones tree
405 * @param zone: the zone to delete from tree. Also deletes zone from memory.
407 void local_zones_del_zone(struct local_zones* zones, struct local_zone* zone);
410 * Add RR data into the localzone data.
411 * Looks up the zone, if no covering zone, a transparent zone with the
412 * name of the RR is created.
413 * @param zones: the zones tree. Not locked by caller.
414 * @param rr: string with on RR.
415 * @return false on failure.
417 int local_zones_add_RR(struct local_zones* zones, const char* rr);
420 * Remove data from domain name in the tree.
421 * All types are removed. No effect if zone or name does not exist.
422 * @param zones: zones tree.
423 * @param name: dname to remove
424 * @param len: length of name.
425 * @param labs: labelcount of name.
426 * @param dclass: class to remove.
428 void local_zones_del_data(struct local_zones* zones,
429 uint8_t* name, size_t len, int labs, uint16_t dclass);
433 * Form wireformat from text format domain name.
434 * @param str: the domain name in text "www.example.com"
435 * @param res: resulting wireformat is stored here with malloc.
436 * @param len: length of resulting wireformat.
437 * @param labs: number of labels in resulting wireformat.
438 * @return false on error, syntax or memory. Also logged.
440 int parse_dname(const char* str, uint8_t** res, size_t* len, int* labs);
443 * Find local data tag string match for the given type (in qinfo) in the list.
444 * If found, 'r' will be filled with corresponding rrset information.
445 * @param qinfo: contains name, type, and class for the data
446 * @param list: stores local tag data to be searched
447 * @param r: rrset key to be filled for matched data
448 * @param temp: region to allocate rrset in 'r'
449 * @return 1 if a match is found and rrset is built; otherwise 0 including
452 int local_data_find_tag_datas(const struct query_info* qinfo,
453 struct config_strlist* list, struct ub_packed_rrset_key* r,
454 struct regional* temp);
457 * See if two sets of tag lists (in the form of bitmap) have the same tag that
458 * has an action. If so, '*tag' will be set to the found tag index, and the
459 * corresponding action will be returned in the form of local zone type.
460 * Otherwise the passed type (lzt) will be returned as the default action.
461 * Pointers except tagactions must not be NULL.
462 * @param taglist: 1st list of tags
463 * @param taglen: size of taglist in bytes
464 * @param taglist2: 2nd list of tags
465 * @param taglen2: size of taglist2 in bytes
466 * @param tagactions: local data actions for tags. May be NULL.
467 * @param tagactionssize: length of the tagactions.
468 * @param lzt: default action (local zone type) if no tag action is found.
469 * @param tag: see above.
470 * @param tagname: array of tag name strings (for debug output).
471 * @param num_tags: number of items in tagname array.
472 * @return found tag action or the default action.
474 enum localzone_type local_data_find_tag_action(const uint8_t* taglist,
475 size_t taglen, const uint8_t* taglist2, size_t taglen2,
476 const uint8_t* tagactions, size_t tagactionssize,
477 enum localzone_type lzt, int* tag, char* const* tagname, int num_tags);
480 * Enter defaults to local zone.
481 * @param zones: to add defaults to
482 * @param cfg: containing list of zones to exclude from default set.
483 * @return 1 on success; 0 otherwise.
485 int local_zone_enter_defaults(struct local_zones* zones,
486 struct config_file* cfg);
489 * Parses resource record string into wire format, also returning its field values.
490 * @param str: input resource record
491 * @param nm: domain name field
492 * @param type: record type field
493 * @param dclass: record class field
494 * @param ttl: ttl field
495 * @param rr: buffer for the parsed rr in wire format
496 * @param len: buffer length
497 * @param rdata: rdata field
498 * @param rdata_len: rdata field length
499 * @return 1 on success; 0 otherwise.
501 int rrstr_get_rr_content(const char* str, uint8_t** nm, uint16_t* type,
502 uint16_t* dclass, time_t* ttl, uint8_t* rr, size_t len,
503 uint8_t** rdata, size_t* rdata_len);
506 * Insert specified rdata into the specified resource record.
507 * @param region: allocator
508 * @param pd: data portion of the destination resource record
509 * @param rdata: source rdata
510 * @param rdata_len: source rdata length
511 * @param ttl: time to live
512 * @param rrstr: resource record in text form (for logging)
513 * @return 1 on success; 0 otherwise.
515 int rrset_insert_rr(struct regional* region, struct packed_rrset_data* pd,
516 uint8_t* rdata, size_t rdata_len, time_t ttl, const char* rrstr);
519 * Remove RR from rrset that is created using localzone's rrset_insert_rr.
520 * @param pd: the RRset containing the RR to remove
521 * @param index: index of RR to remove
522 * @return: 1 on success; 0 otherwise.
525 local_rrset_remove_rr(struct packed_rrset_data* pd, size_t index);
528 * Valid response ip actions for the IP-response-driven-action feature;
529 * defined here instead of in the respip module to enable sharing of enum
530 * values with the localzone_type enum.
531 * Note that these values except 'none' are the same as localzone types of
532 * the 'same semantics'. It's intentional as we use these values via
533 * access-control-tags, which can be shared for both response ip actions and
537 /** no respip action */
538 respip_none = local_zone_unset,
540 respip_deny = local_zone_deny,
541 /** redirect as per provided data */
542 respip_redirect = local_zone_redirect,
543 /** log query source and answer query */
544 respip_inform = local_zone_inform,
545 /** log query source and don't answer query */
546 respip_inform_deny = local_zone_inform_deny,
547 /** log query source and redirect */
548 respip_inform_redirect = local_zone_inform_redirect,
549 /** resolve normally, even when there is response-ip data */
550 respip_always_transparent = local_zone_always_transparent,
551 /** answer with 'refused' response */
552 respip_always_refuse = local_zone_always_refuse,
553 /** answer with 'no such domain' response */
554 respip_always_nxdomain = local_zone_always_nxdomain,
555 /** answer with nodata response */
556 respip_always_nodata = local_zone_always_nodata,
557 /** answer with nodata response */
558 respip_always_deny = local_zone_always_deny,
560 /* The rest of the values are only possible as
561 * access-control-tag-action */
563 /** serves response data (if any), else, drops queries. */
564 respip_refuse = local_zone_refuse,
565 /** serves response data, else, nodata answer. */
566 respip_static = local_zone_static,
567 /** gives response data (if any), else nodata answer. */
568 respip_transparent = local_zone_transparent,
569 /** gives response data (if any), else nodata answer. */
570 respip_typetransparent = local_zone_typetransparent,
572 respip_invalid = local_zone_invalid,
576 * Get local data from local zone and encode answer.
577 * @param z: local zone to use
578 * @param env: module env
579 * @param qinfo: qinfo
580 * @param edns: edns data, for message encoding
581 * @param repinfo: reply info, for message encoding
582 * @param buf: commpoint buffer
583 * @param temp: scratchpad region
584 * @param labs: number of labels in qname
585 * @param ldp: where to store local data
586 * @param lz_type: type of local zone
587 * @param tag: matching tag index
588 * @param tag_datas: alc specific tag data list
589 * @param tag_datas_size: size of tag_datas
590 * @param tagname: list of names of tags, for logging purpose
591 * @param num_tags: number of tags
592 * @return 1 on success
595 local_data_answer(struct local_zone* z, struct module_env* env,
596 struct query_info* qinfo, struct edns_data* edns,
597 struct comm_reply* repinfo, sldns_buffer* buf,
598 struct regional* temp, int labs, struct local_data** ldp,
599 enum localzone_type lz_type, int tag, struct config_strlist** tag_datas,
600 size_t tag_datas_size, char** tagname, int num_tags);
603 * Add RR to local zone.
604 * @param z: local zone to add RR to
605 * @param nm: dname of RR
606 * @param nmlen: length of nm
607 * @param nmlabs: number of labels of nm
608 * @param rrtype: RR type
609 * @param rrclass: RR class
610 * @param ttl: TTL of RR to add
611 * @param rdata: RDATA of RR to add
612 * @param rdata_len: length of rdata
613 * @param rrstr: RR in string format, for logging
614 * @return: 1 on success
617 local_zone_enter_rr(struct local_zone* z, uint8_t* nm, size_t nmlen,
618 int nmlabs, uint16_t rrtype, uint16_t rrclass, time_t ttl,
619 uint8_t* rdata, size_t rdata_len, const char* rrstr);
622 * Find a data node by exact name for a local zone
623 * @param z: local_zone containing data tree
624 * @param nm: name of local-data element to find
625 * @param nmlen: length of nm
626 * @param nmlabs: labs of nm
627 * @return local_data on exact match, NULL otherwise.
630 local_zone_find_data(struct local_zone* z, uint8_t* nm, size_t nmlen, int nmlabs);
631 #endif /* SERVICES_LOCALZONE_H */