xref: /freebsd/contrib/unbound/iterator/iter_utils.h (revision be771a7b7f4580a30d99e41a5bb1b93a385a119d)
1b7579f77SDag-Erling Smørgrav /*
2b7579f77SDag-Erling Smørgrav  * iterator/iter_utils.h - iterative resolver module utility functions.
3b7579f77SDag-Erling Smørgrav  *
4b7579f77SDag-Erling Smørgrav  * Copyright (c) 2007, NLnet Labs. All rights reserved.
5b7579f77SDag-Erling Smørgrav  *
6b7579f77SDag-Erling Smørgrav  * This software is open source.
7b7579f77SDag-Erling Smørgrav  *
8b7579f77SDag-Erling Smørgrav  * Redistribution and use in source and binary forms, with or without
9b7579f77SDag-Erling Smørgrav  * modification, are permitted provided that the following conditions
10b7579f77SDag-Erling Smørgrav  * are met:
11b7579f77SDag-Erling Smørgrav  *
12b7579f77SDag-Erling Smørgrav  * Redistributions of source code must retain the above copyright notice,
13b7579f77SDag-Erling Smørgrav  * this list of conditions and the following disclaimer.
14b7579f77SDag-Erling Smørgrav  *
15b7579f77SDag-Erling Smørgrav  * Redistributions in binary form must reproduce the above copyright notice,
16b7579f77SDag-Erling Smørgrav  * this list of conditions and the following disclaimer in the documentation
17b7579f77SDag-Erling Smørgrav  * and/or other materials provided with the distribution.
18b7579f77SDag-Erling Smørgrav  *
19b7579f77SDag-Erling Smørgrav  * Neither the name of the NLNET LABS nor the names of its contributors may
20b7579f77SDag-Erling Smørgrav  * be used to endorse or promote products derived from this software without
21b7579f77SDag-Erling Smørgrav  * specific prior written permission.
22b7579f77SDag-Erling Smørgrav  *
23b7579f77SDag-Erling Smørgrav  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
2417d15b25SDag-Erling Smørgrav  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
2517d15b25SDag-Erling Smørgrav  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
2617d15b25SDag-Erling Smørgrav  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
2717d15b25SDag-Erling Smørgrav  * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2817d15b25SDag-Erling Smørgrav  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
2917d15b25SDag-Erling Smørgrav  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
3017d15b25SDag-Erling Smørgrav  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
3117d15b25SDag-Erling Smørgrav  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
3217d15b25SDag-Erling Smørgrav  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
3317d15b25SDag-Erling Smørgrav  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34b7579f77SDag-Erling Smørgrav  */
35b7579f77SDag-Erling Smørgrav 
36b7579f77SDag-Erling Smørgrav /**
37b7579f77SDag-Erling Smørgrav  * \file
38b7579f77SDag-Erling Smørgrav  *
39b7579f77SDag-Erling Smørgrav  * This file contains functions to assist the iterator module.
40b7579f77SDag-Erling Smørgrav  * Configuration options. Forward zones.
41b7579f77SDag-Erling Smørgrav  */
42b7579f77SDag-Erling Smørgrav 
43b7579f77SDag-Erling Smørgrav #ifndef ITERATOR_ITER_UTILS_H
44b7579f77SDag-Erling Smørgrav #define ITERATOR_ITER_UTILS_H
45b7579f77SDag-Erling Smørgrav #include "iterator/iter_resptype.h"
4617d15b25SDag-Erling Smørgrav struct sldns_buffer;
47b7579f77SDag-Erling Smørgrav struct iter_env;
48b7579f77SDag-Erling Smørgrav struct iter_hints;
49b7579f77SDag-Erling Smørgrav struct iter_forwards;
50b7579f77SDag-Erling Smørgrav struct config_file;
51b7579f77SDag-Erling Smørgrav struct module_env;
52b7579f77SDag-Erling Smørgrav struct delegpt_addr;
53b7579f77SDag-Erling Smørgrav struct delegpt;
54b7579f77SDag-Erling Smørgrav struct regional;
55b7579f77SDag-Erling Smørgrav struct msg_parse;
56b7579f77SDag-Erling Smørgrav struct ub_randstate;
57b7579f77SDag-Erling Smørgrav struct query_info;
58b7579f77SDag-Erling Smørgrav struct reply_info;
59b7579f77SDag-Erling Smørgrav struct module_qstate;
60b7579f77SDag-Erling Smørgrav struct sock_list;
61b7579f77SDag-Erling Smørgrav struct ub_packed_rrset_key;
625469a995SCy Schubert struct module_stack;
635469a995SCy Schubert struct outside_network;
64*be771a7bSCy Schubert struct iter_nat64;
65b7579f77SDag-Erling Smørgrav 
664f5c8956SCy Schubert /* max number of lookups in the cache for target nameserver names.
674f5c8956SCy Schubert  * This stops, for large delegations, N*N lookups in the cache. */
684f5c8956SCy Schubert #define ITERATOR_NAME_CACHELOOKUP_MAX	3
694f5c8956SCy Schubert /* max number of lookups in the cache for parentside glue for nameserver names
704f5c8956SCy Schubert  * This stops, for larger delegations, N*N lookups in the cache.
714f5c8956SCy Schubert  * It is a little larger than the nonpside max, so it allows a couple extra
724f5c8956SCy Schubert  * lookups of parent side glue. */
734f5c8956SCy Schubert #define ITERATOR_NAME_CACHELOOKUP_MAX_PSIDE	5
744f5c8956SCy Schubert 
75b7579f77SDag-Erling Smørgrav /**
76b7579f77SDag-Erling Smørgrav  * Process config options and set iterator module state.
77b7579f77SDag-Erling Smørgrav  * Sets default values if no config is found.
78b7579f77SDag-Erling Smørgrav  * @param iter_env: iterator module state.
79b7579f77SDag-Erling Smørgrav  * @param cfg: config options.
80b7579f77SDag-Erling Smørgrav  * @return 0 on error.
81b7579f77SDag-Erling Smørgrav  */
82b7579f77SDag-Erling Smørgrav int iter_apply_cfg(struct iter_env* iter_env, struct config_file* cfg);
83b7579f77SDag-Erling Smørgrav 
84b7579f77SDag-Erling Smørgrav /**
85b7579f77SDag-Erling Smørgrav  * Select a valid, nice target to send query to.
86b7579f77SDag-Erling Smørgrav  * Sorting and removing unsuitable targets is combined.
87b7579f77SDag-Erling Smørgrav  *
88b7579f77SDag-Erling Smørgrav  * @param iter_env: iterator module global state, with ip6 enabled and
89b7579f77SDag-Erling Smørgrav  *	do-not-query-addresses.
90b7579f77SDag-Erling Smørgrav  * @param env: environment with infra cache (lameness, rtt info).
91b7579f77SDag-Erling Smørgrav  * @param dp: delegation point with result list.
92b7579f77SDag-Erling Smørgrav  * @param name: zone name (for lameness check).
93b7579f77SDag-Erling Smørgrav  * @param namelen: length of name.
94b7579f77SDag-Erling Smørgrav  * @param qtype: query type that we want to send.
95b7579f77SDag-Erling Smørgrav  * @param dnssec_lame: set to 1, if a known dnssec-lame server is selected
96b7579f77SDag-Erling Smørgrav  *	these are not preferred, but are used as a last resort.
97b7579f77SDag-Erling Smørgrav  * @param chase_to_rd: set to 1 if a known recursion lame server is selected
98b7579f77SDag-Erling Smørgrav  * 	these are not preferred, but are used as a last resort.
99b7579f77SDag-Erling Smørgrav  * @param open_target: number of currently outstanding target queries.
100b7579f77SDag-Erling Smørgrav  * 	If we wait for these, perhaps more server addresses become available.
101b7579f77SDag-Erling Smørgrav  * @param blacklist: the IP blacklist to use.
1020fb34990SDag-Erling Smørgrav  * @param prefetch: if not 0, prefetch is in use for this query.
1030fb34990SDag-Erling Smørgrav  * 	This means the query can have different timing, because prefetch is
1040fb34990SDag-Erling Smørgrav  * 	not waited upon by the downstream client, and thus a good time to
1050fb34990SDag-Erling Smørgrav  * 	perform exploration of other targets.
106b7579f77SDag-Erling Smørgrav  * @return best target or NULL if no target.
107b7579f77SDag-Erling Smørgrav  *	if not null, that target is removed from the result list in the dp.
108b7579f77SDag-Erling Smørgrav  */
109b7579f77SDag-Erling Smørgrav struct delegpt_addr* iter_server_selection(struct iter_env* iter_env,
110b7579f77SDag-Erling Smørgrav 	struct module_env* env, struct delegpt* dp, uint8_t* name,
111b7579f77SDag-Erling Smørgrav 	size_t namelen, uint16_t qtype, int* dnssec_lame,
1120fb34990SDag-Erling Smørgrav 	int* chase_to_rd, int open_target, struct sock_list* blacklist,
1130fb34990SDag-Erling Smørgrav 	time_t prefetch);
114b7579f77SDag-Erling Smørgrav 
115b7579f77SDag-Erling Smørgrav /**
116b7579f77SDag-Erling Smørgrav  * Allocate dns_msg from parsed msg, in regional.
117b7579f77SDag-Erling Smørgrav  * @param pkt: packet.
118b7579f77SDag-Erling Smørgrav  * @param msg: parsed message (cleaned and ready for regional allocation).
119b7579f77SDag-Erling Smørgrav  * @param regional: regional to use for allocation.
120b7579f77SDag-Erling Smørgrav  * @return newly allocated dns_msg, or NULL on memory error.
121b7579f77SDag-Erling Smørgrav  */
12217d15b25SDag-Erling Smørgrav struct dns_msg* dns_alloc_msg(struct sldns_buffer* pkt, struct msg_parse* msg,
123b7579f77SDag-Erling Smørgrav 	struct regional* regional);
124b7579f77SDag-Erling Smørgrav 
125b7579f77SDag-Erling Smørgrav /**
126b7579f77SDag-Erling Smørgrav  * Copy a dns_msg to this regional.
127b7579f77SDag-Erling Smørgrav  * @param from: dns message, also in regional.
128b7579f77SDag-Erling Smørgrav  * @param regional: regional to use for allocation.
129b7579f77SDag-Erling Smørgrav  * @return newly allocated dns_msg, or NULL on memory error.
130b7579f77SDag-Erling Smørgrav  */
131b7579f77SDag-Erling Smørgrav struct dns_msg* dns_copy_msg(struct dns_msg* from, struct regional* regional);
132b7579f77SDag-Erling Smørgrav 
133b7579f77SDag-Erling Smørgrav /**
134b7579f77SDag-Erling Smørgrav  * Allocate a dns_msg with malloc/alloc structure and store in dns cache.
135b7579f77SDag-Erling Smørgrav  * @param env: environment, with alloc structure and dns cache.
136b7579f77SDag-Erling Smørgrav  * @param qinf: query info, the query for which answer is stored.
137b7579f77SDag-Erling Smørgrav  * @param rep: reply in dns_msg from dns_alloc_msg for example.
138b7579f77SDag-Erling Smørgrav  * @param is_referral: If true, then the given message to be stored is a
139b7579f77SDag-Erling Smørgrav  *	referral. The cache implementation may use this as a hint.
140b7579f77SDag-Erling Smørgrav  * @param leeway: prefetch TTL leeway to expire old rrsets quicker.
141b7579f77SDag-Erling Smørgrav  * @param pside: true if dp is parentside, thus message is 'fresh' and NS
142b7579f77SDag-Erling Smørgrav  * 	can be prefetch-updates.
143b7579f77SDag-Erling Smørgrav  * @param region: to copy modified (cache is better) rrs back to.
144ff825849SDag-Erling Smørgrav  * @param flags: with BIT_CD for dns64 AAAA translated queries.
145790c6b24SCy Schubert  * @param qstarttime: time of query start.
146*be771a7bSCy Schubert  * @param is_valrec: if the query is validation recursion and does not get
1475469a995SCy Schubert  * return void, because we are not interested in alloc errors,
1488ed2b524SDag-Erling Smørgrav  * 	the iterator and validator can operate on the results in their
1498ed2b524SDag-Erling Smørgrav  * 	scratch space (the qstate.region) and are not dependent on the cache.
1508ed2b524SDag-Erling Smørgrav  * 	It is useful to log the alloc failure (for the server operator),
1518ed2b524SDag-Erling Smørgrav  * 	but the query resolution can continue without cache storage.
152b7579f77SDag-Erling Smørgrav  */
1538ed2b524SDag-Erling Smørgrav void iter_dns_store(struct module_env* env, struct query_info* qinf,
15417d15b25SDag-Erling Smørgrav 	struct reply_info* rep, int is_referral, time_t leeway, int pside,
155*be771a7bSCy Schubert 	struct regional* region, uint16_t flags, time_t qstarttime,
156*be771a7bSCy Schubert 	int is_valrec);
157b7579f77SDag-Erling Smørgrav 
158b7579f77SDag-Erling Smørgrav /**
159b7579f77SDag-Erling Smørgrav  * Select randomly with n/m probability.
160b7579f77SDag-Erling Smørgrav  * For shuffle NS records for address fetching.
161b7579f77SDag-Erling Smørgrav  * @param rnd: random table
162b7579f77SDag-Erling Smørgrav  * @param n: probability.
163b7579f77SDag-Erling Smørgrav  * @param m: divisor for probability.
164b7579f77SDag-Erling Smørgrav  * @return true with n/m probability.
165b7579f77SDag-Erling Smørgrav  */
166b7579f77SDag-Erling Smørgrav int iter_ns_probability(struct ub_randstate* rnd, int n, int m);
167b7579f77SDag-Erling Smørgrav 
168b7579f77SDag-Erling Smørgrav /**
169b7579f77SDag-Erling Smørgrav  * Mark targets that result in a dependency cycle as done, so they
170b7579f77SDag-Erling Smørgrav  * will not get selected as targets.
171b7579f77SDag-Erling Smørgrav  * @param qstate: query state.
172b7579f77SDag-Erling Smørgrav  * @param dp: delegpt to mark ns in.
173b7579f77SDag-Erling Smørgrav  */
174b7579f77SDag-Erling Smørgrav void iter_mark_cycle_targets(struct module_qstate* qstate, struct delegpt* dp);
175b7579f77SDag-Erling Smørgrav 
176b7579f77SDag-Erling Smørgrav /**
177b7579f77SDag-Erling Smørgrav  * Mark targets that result in a dependency cycle as done, so they
178b7579f77SDag-Erling Smørgrav  * will not get selected as targets.  For the parent-side lookups.
179b7579f77SDag-Erling Smørgrav  * @param qstate: query state.
180b7579f77SDag-Erling Smørgrav  * @param dp: delegpt to mark ns in.
181b7579f77SDag-Erling Smørgrav  */
182b7579f77SDag-Erling Smørgrav void iter_mark_pside_cycle_targets(struct module_qstate* qstate,
183b7579f77SDag-Erling Smørgrav 	struct delegpt* dp);
184b7579f77SDag-Erling Smørgrav 
185b7579f77SDag-Erling Smørgrav /**
186b7579f77SDag-Erling Smørgrav  * See if delegation is useful or offers immediately no targets for
187b7579f77SDag-Erling Smørgrav  * further recursion.
188b7579f77SDag-Erling Smørgrav  * @param qinfo: query name and type
189b7579f77SDag-Erling Smørgrav  * @param qflags: query flags with RD flag
190b7579f77SDag-Erling Smørgrav  * @param dp: delegpt to check.
191a39a5a69SCy Schubert  * @param supports_ipv4: if we support ipv4 for lookups to the target.
192a39a5a69SCy Schubert  * 	if not, then the IPv4 addresses are useless.
193a39a5a69SCy Schubert  * @param supports_ipv6: if we support ipv6 for lookups to the target.
194a39a5a69SCy Schubert  * 	if not, then the IPv6 addresses are useless.
1958f76bb7dSCy Schubert  * @param use_nat64: if we support NAT64 for lookups to the target.
1968f76bb7dSCy Schubert  *	if yes, IPv4 addresses are useful even if we don't support IPv4.
197b7579f77SDag-Erling Smørgrav  * @return true if dp is useless.
198b7579f77SDag-Erling Smørgrav  */
199b7579f77SDag-Erling Smørgrav int iter_dp_is_useless(struct query_info* qinfo, uint16_t qflags,
2008f76bb7dSCy Schubert 	struct delegpt* dp, int supports_ipv4, int supports_ipv6,
2018f76bb7dSCy Schubert 	int use_nat64);
202b7579f77SDag-Erling Smørgrav 
203b7579f77SDag-Erling Smørgrav /**
2040fb34990SDag-Erling Smørgrav  * See if qname has DNSSEC needs.  This is true if there is a trust anchor above
2050fb34990SDag-Erling Smørgrav  * it.  Whether there is an insecure delegation to the data is unknown.
206e2d15004SDag-Erling Smørgrav  * @param env: environment with anchors.
207e2d15004SDag-Erling Smørgrav  * @param qinfo: query name and class.
208e2d15004SDag-Erling Smørgrav  * @return true if trust anchor above qname, false if no anchor or insecure
209e2d15004SDag-Erling Smørgrav  * point above qname.
210e2d15004SDag-Erling Smørgrav  */
2110fb34990SDag-Erling Smørgrav int iter_qname_indicates_dnssec(struct module_env* env,
212e2d15004SDag-Erling Smørgrav 	struct query_info *qinfo);
213e2d15004SDag-Erling Smørgrav 
214e2d15004SDag-Erling Smørgrav /**
215b7579f77SDag-Erling Smørgrav  * See if delegation is expected to have DNSSEC information (RRSIGs) in
216b7579f77SDag-Erling Smørgrav  * its answers, or not. Inspects delegation point (name), trust anchors,
217b7579f77SDag-Erling Smørgrav  * and delegation message (DS RRset) to determine this.
218b7579f77SDag-Erling Smørgrav  * @param env: module env with trust anchors.
219b7579f77SDag-Erling Smørgrav  * @param dp: delegation point.
220b7579f77SDag-Erling Smørgrav  * @param msg: delegation message, with DS if a secure referral.
221b7579f77SDag-Erling Smørgrav  * @param dclass: class of query.
22257bddd21SDag-Erling Smørgrav  * @return 1 if dnssec is expected, 0 if not or insecure point above qname.
223b7579f77SDag-Erling Smørgrav  */
224b7579f77SDag-Erling Smørgrav int iter_indicates_dnssec(struct module_env* env, struct delegpt* dp,
225b7579f77SDag-Erling Smørgrav 	struct dns_msg* msg, uint16_t dclass);
226b7579f77SDag-Erling Smørgrav 
227b7579f77SDag-Erling Smørgrav /**
228b7579f77SDag-Erling Smørgrav  * See if a message contains DNSSEC.
229b7579f77SDag-Erling Smørgrav  * This is examined by looking for RRSIGs. With DNSSEC a valid answer,
230b7579f77SDag-Erling Smørgrav  * nxdomain, nodata, referral or cname reply has RRSIGs in answer or auth
231b7579f77SDag-Erling Smørgrav  * sections, sigs on answer data, SOA, DS, or NSEC/NSEC3 records.
232b7579f77SDag-Erling Smørgrav  * @param msg: message to examine.
233b7579f77SDag-Erling Smørgrav  * @return true if DNSSEC information was found.
234b7579f77SDag-Erling Smørgrav  */
235b7579f77SDag-Erling Smørgrav int iter_msg_has_dnssec(struct dns_msg* msg);
236b7579f77SDag-Erling Smørgrav 
237b7579f77SDag-Erling Smørgrav /**
238b7579f77SDag-Erling Smørgrav  * See if a message is known to be from a certain zone.
239b7579f77SDag-Erling Smørgrav  * This looks for SOA or NS rrsets, for answers.
240b7579f77SDag-Erling Smørgrav  * For referrals, when one label is delegated, the zone is detected.
241b7579f77SDag-Erling Smørgrav  * Does not look at signatures.
242b7579f77SDag-Erling Smørgrav  * @param msg: the message to inspect.
243b7579f77SDag-Erling Smørgrav  * @param dp: delegation point with zone name to look for.
244b7579f77SDag-Erling Smørgrav  * @param type: type of message.
245b7579f77SDag-Erling Smørgrav  * @param dclass: class of query.
246b7579f77SDag-Erling Smørgrav  * @return true if message is certain to be from zone in dp->name.
247b7579f77SDag-Erling Smørgrav  *	false if not sure (empty msg), or not from the zone.
248b7579f77SDag-Erling Smørgrav  */
249b7579f77SDag-Erling Smørgrav int iter_msg_from_zone(struct dns_msg* msg, struct delegpt* dp,
250b7579f77SDag-Erling Smørgrav 	enum response_type type, uint16_t dclass);
251b7579f77SDag-Erling Smørgrav 
252b7579f77SDag-Erling Smørgrav /**
253b7579f77SDag-Erling Smørgrav  * Check if two replies are equal
254b7579f77SDag-Erling Smørgrav  * For fallback procedures
255b7579f77SDag-Erling Smørgrav  * @param p: reply one. The reply has rrset data pointers in region.
256b7579f77SDag-Erling Smørgrav  * 	Does not check rrset-IDs
257b7579f77SDag-Erling Smørgrav  * @param q: reply two
25817d15b25SDag-Erling Smørgrav  * @param region: scratch buffer.
259b7579f77SDag-Erling Smørgrav  * @return if one and two are equal.
260b7579f77SDag-Erling Smørgrav  */
26117d15b25SDag-Erling Smørgrav int reply_equal(struct reply_info* p, struct reply_info* q, struct regional* region);
262b7579f77SDag-Erling Smørgrav 
263b7579f77SDag-Erling Smørgrav /**
2646480faa8SDag-Erling Smørgrav  * Remove unused bits from the reply if possible.
2656480faa8SDag-Erling Smørgrav  * So that caps-for-id (0x20) fallback is more likely to be successful.
2666480faa8SDag-Erling Smørgrav  * This removes like, the additional section, and NS record in the authority
2676480faa8SDag-Erling Smørgrav  * section if those records are gratuitous (not for a referral).
2686480faa8SDag-Erling Smørgrav  * @param rep: the reply to strip stuff out of.
2696480faa8SDag-Erling Smørgrav  */
2706480faa8SDag-Erling Smørgrav void caps_strip_reply(struct reply_info* rep);
2716480faa8SDag-Erling Smørgrav 
2726480faa8SDag-Erling Smørgrav /**
27309a3aaf3SDag-Erling Smørgrav  * see if reply has a 'useful' rcode for capsforid comparison, so
27409a3aaf3SDag-Erling Smørgrav  * not SERVFAIL or REFUSED, and thus NOERROR or NXDOMAIN.
27509a3aaf3SDag-Erling Smørgrav  * @param rep: reply to check.
27609a3aaf3SDag-Erling Smørgrav  * @return true if the rcode is a bad type of message.
27709a3aaf3SDag-Erling Smørgrav  */
27809a3aaf3SDag-Erling Smørgrav int caps_failed_rcode(struct reply_info* rep);
27909a3aaf3SDag-Erling Smørgrav 
28009a3aaf3SDag-Erling Smørgrav /**
2818a384985SDag-Erling Smørgrav  * Store parent-side rrset in separate rrset cache entries for later
282b7579f77SDag-Erling Smørgrav  * last-resort * lookups in case the child-side versions of this information
283b7579f77SDag-Erling Smørgrav  * fails.
284b7579f77SDag-Erling Smørgrav  * @param env: environment with cache, time, ...
285b7579f77SDag-Erling Smørgrav  * @param rrset: the rrset to store (copied).
286b7579f77SDag-Erling Smørgrav  * Failure to store is logged, but otherwise ignored.
287b7579f77SDag-Erling Smørgrav  */
288b7579f77SDag-Erling Smørgrav void iter_store_parentside_rrset(struct module_env* env,
289b7579f77SDag-Erling Smørgrav 	struct ub_packed_rrset_key* rrset);
290b7579f77SDag-Erling Smørgrav 
291b7579f77SDag-Erling Smørgrav /**
292b7579f77SDag-Erling Smørgrav  * Store parent-side NS records from a referral message
293b7579f77SDag-Erling Smørgrav  * @param env: environment with cache, time, ...
294b7579f77SDag-Erling Smørgrav  * @param rep: response with NS rrset.
295b7579f77SDag-Erling Smørgrav  * Failure to store is logged, but otherwise ignored.
296b7579f77SDag-Erling Smørgrav  */
297b7579f77SDag-Erling Smørgrav void iter_store_parentside_NS(struct module_env* env, struct reply_info* rep);
298b7579f77SDag-Erling Smørgrav 
299b7579f77SDag-Erling Smørgrav /**
300b7579f77SDag-Erling Smørgrav  * Store parent-side negative element, the parentside rrset does not exist,
301b7579f77SDag-Erling Smørgrav  * creates an rrset with empty rdata in the rrset cache with PARENTSIDE flag.
302b7579f77SDag-Erling Smørgrav  * @param env: environment with cache, time, ...
303b7579f77SDag-Erling Smørgrav  * @param qinfo: the identity of the rrset that is missing.
304b7579f77SDag-Erling Smørgrav  * @param rep: delegation response or answer response, to glean TTL from.
305b7579f77SDag-Erling Smørgrav  * (malloc) failure is logged but otherwise ignored.
306b7579f77SDag-Erling Smørgrav  */
307b7579f77SDag-Erling Smørgrav void iter_store_parentside_neg(struct module_env* env,
308b7579f77SDag-Erling Smørgrav 	struct query_info* qinfo, struct reply_info* rep);
309b7579f77SDag-Erling Smørgrav 
310b7579f77SDag-Erling Smørgrav /**
311b7579f77SDag-Erling Smørgrav  * Add parent NS record if that exists in the cache.  This is both new
312b7579f77SDag-Erling Smørgrav  * information and acts like a timeout throttle on retries.
313b7579f77SDag-Erling Smørgrav  * @param env: query env with rrset cache and time.
314b7579f77SDag-Erling Smørgrav  * @param dp: delegation point to store result in.  Also this dp is used to
315b7579f77SDag-Erling Smørgrav  *	see which NS name is needed.
316b7579f77SDag-Erling Smørgrav  * @param region: region to alloc result in.
317b7579f77SDag-Erling Smørgrav  * @param qinfo: pertinent information, the qclass.
318b7579f77SDag-Erling Smørgrav  * @return false on malloc failure.
319b7579f77SDag-Erling Smørgrav  *	if true, the routine worked and if such cached information
320b7579f77SDag-Erling Smørgrav  *	existed dp->has_parent_side_NS is set true.
321b7579f77SDag-Erling Smørgrav  */
322b7579f77SDag-Erling Smørgrav int iter_lookup_parent_NS_from_cache(struct module_env* env,
323b7579f77SDag-Erling Smørgrav 	struct delegpt* dp, struct regional* region, struct query_info* qinfo);
324b7579f77SDag-Erling Smørgrav 
325b7579f77SDag-Erling Smørgrav /**
326b7579f77SDag-Erling Smørgrav  * Add parent-side glue if that exists in the cache.  This is both new
327b7579f77SDag-Erling Smørgrav  * information and acts like a timeout throttle on retries to fetch them.
328b7579f77SDag-Erling Smørgrav  * @param env: query env with rrset cache and time.
329b7579f77SDag-Erling Smørgrav  * @param dp: delegation point to store result in.  Also this dp is used to
330b7579f77SDag-Erling Smørgrav  *	see which NS name is needed.
331b7579f77SDag-Erling Smørgrav  * @param region: region to alloc result in.
332b7579f77SDag-Erling Smørgrav  * @param qinfo: pertinent information, the qclass.
333b7579f77SDag-Erling Smørgrav  * @return: true, it worked, no malloc failures, and new addresses (lame)
334b7579f77SDag-Erling Smørgrav  *	have been added, giving extra options as query targets.
335b7579f77SDag-Erling Smørgrav  */
336b7579f77SDag-Erling Smørgrav int iter_lookup_parent_glue_from_cache(struct module_env* env,
337b7579f77SDag-Erling Smørgrav 	struct delegpt* dp, struct regional* region, struct query_info* qinfo);
338b7579f77SDag-Erling Smørgrav 
339b7579f77SDag-Erling Smørgrav /**
340b7579f77SDag-Erling Smørgrav  * Lookup next root-hint or root-forward entry.
341b7579f77SDag-Erling Smørgrav  * @param hints: the hints.
342b7579f77SDag-Erling Smørgrav  * @param fwd: the forwards.
343b7579f77SDag-Erling Smørgrav  * @param c: the class to start searching at. 0 means find first one.
344b7579f77SDag-Erling Smørgrav  * @return false if no classes found, true if found and returned in c.
345b7579f77SDag-Erling Smørgrav  */
346b7579f77SDag-Erling Smørgrav int iter_get_next_root(struct iter_hints* hints, struct iter_forwards* fwd,
347b7579f77SDag-Erling Smørgrav 	uint16_t* c);
348b7579f77SDag-Erling Smørgrav 
349b7579f77SDag-Erling Smørgrav /**
350b7579f77SDag-Erling Smørgrav  * Remove DS records that are inappropriate before they are cached.
351b7579f77SDag-Erling Smørgrav  * @param msg: the response to scrub.
352b7579f77SDag-Erling Smørgrav  * @param ns: RRSET that is the NS record for the referral.
353b7579f77SDag-Erling Smørgrav  * 	if NULL, then all DS records are removed from the authority section.
354b7579f77SDag-Erling Smørgrav  * @param z: zone name that the response is from.
355b7579f77SDag-Erling Smørgrav  */
356b7579f77SDag-Erling Smørgrav void iter_scrub_ds(struct dns_msg* msg, struct ub_packed_rrset_key* ns,
357b7579f77SDag-Erling Smørgrav 	uint8_t* z);
358b7579f77SDag-Erling Smørgrav 
359b7579f77SDag-Erling Smørgrav /**
360e86b9096SDag-Erling Smørgrav  * Prepare an NXDOMAIN message to be used for a subdomain answer by removing all
361e86b9096SDag-Erling Smørgrav  * RRs from the ANSWER section.
362e86b9096SDag-Erling Smørgrav  * @param msg: the response to scrub.
363e86b9096SDag-Erling Smørgrav  */
364e86b9096SDag-Erling Smørgrav void iter_scrub_nxdomain(struct dns_msg* msg);
365e86b9096SDag-Erling Smørgrav 
366e86b9096SDag-Erling Smørgrav /**
367b7579f77SDag-Erling Smørgrav  * Remove query attempts from all available ips. For 0x20.
368b7579f77SDag-Erling Smørgrav  * @param dp: delegpt.
369b7579f77SDag-Erling Smørgrav  * @param d: decrease.
37024e36522SCy Schubert  * @param outbound_msg_retry: number of retries of outgoing queries
371b7579f77SDag-Erling Smørgrav  */
37224e36522SCy Schubert void iter_dec_attempts(struct delegpt* dp, int d, int outbound_msg_retry);
373b7579f77SDag-Erling Smørgrav 
374b7579f77SDag-Erling Smørgrav /**
375b7579f77SDag-Erling Smørgrav  * Add retry counts from older delegpt to newer delegpt.
376b7579f77SDag-Erling Smørgrav  * Does not waste time on timeout'd (or other failing) addresses.
377b7579f77SDag-Erling Smørgrav  * @param dp: new delegationpoint.
378b7579f77SDag-Erling Smørgrav  * @param old: old delegationpoint.
37924e36522SCy Schubert  * @param outbound_msg_retry: number of retries of outgoing queries
380b7579f77SDag-Erling Smørgrav  */
38124e36522SCy Schubert void iter_merge_retry_counts(struct delegpt* dp, struct delegpt* old,
38224e36522SCy Schubert 	int outbound_msg_retry);
383b7579f77SDag-Erling Smørgrav 
384b7579f77SDag-Erling Smørgrav /**
385b7579f77SDag-Erling Smørgrav  * See if a DS response (type ANSWER) is too low: a nodata answer with
386b7579f77SDag-Erling Smørgrav  * a SOA record in the authority section at-or-below the qchase.qname.
387b7579f77SDag-Erling Smørgrav  * Also returns true if we are not sure (i.e. empty message, CNAME nosig).
388b7579f77SDag-Erling Smørgrav  * @param msg: the response.
389b7579f77SDag-Erling Smørgrav  * @param dp: the dp name is used to check if the RRSIG gives a clue that
390b7579f77SDag-Erling Smørgrav  * 	it was originated from the correct nameserver.
391b7579f77SDag-Erling Smørgrav  * @return true if too low.
392b7579f77SDag-Erling Smørgrav  */
393b7579f77SDag-Erling Smørgrav int iter_ds_toolow(struct dns_msg* msg, struct delegpt* dp);
394b7579f77SDag-Erling Smørgrav 
395b7579f77SDag-Erling Smørgrav /**
396b7579f77SDag-Erling Smørgrav  * See if delegpt can go down a step to the qname or not
397b7579f77SDag-Erling Smørgrav  * @param qinfo: the query name looked up.
398b7579f77SDag-Erling Smørgrav  * @param dp: checked if the name can go lower to the qname
399b7579f77SDag-Erling Smørgrav  * @return true if can go down, false if that would not be possible.
400b7579f77SDag-Erling Smørgrav  * the current response seems to be the one and only, best possible, response.
401b7579f77SDag-Erling Smørgrav  */
402b7579f77SDag-Erling Smørgrav int iter_dp_cangodown(struct query_info* qinfo, struct delegpt* dp);
403b7579f77SDag-Erling Smørgrav 
404e86b9096SDag-Erling Smørgrav /**
405e86b9096SDag-Erling Smørgrav  * Lookup if no_cache is set in stub or fwd.
406e86b9096SDag-Erling Smørgrav  * @param qstate: query state with env with hints and fwds.
407e86b9096SDag-Erling Smørgrav  * @param qinf: query name to lookup for.
4085469a995SCy Schubert  * @param retdpname: returns NULL or the deepest enclosing name of fwd or stub.
4095469a995SCy Schubert  * 	This is the name under which the closest lookup is going to happen.
4105469a995SCy Schubert  * 	Used for NXDOMAIN checks, above that it is an nxdomain from a
4115469a995SCy Schubert  * 	different server and zone. You can pass NULL to not get it.
4125469a995SCy Schubert  * @param retdpnamelen: returns the length of the dpname.
413335c7cdaSCy Schubert  * @param dpname_storage: this is where the dpname buf is stored, if any.
414335c7cdaSCy Schubert  * 	So that caller can manage the buffer.
415335c7cdaSCy Schubert  * @param dpname_storage_len: size of dpname_storage buffer.
416e86b9096SDag-Erling Smørgrav  * @return true if no_cache is set in stub or fwd.
417e86b9096SDag-Erling Smørgrav  */
418e86b9096SDag-Erling Smørgrav int iter_stub_fwd_no_cache(struct module_qstate *qstate,
419335c7cdaSCy Schubert 	struct query_info *qinf, uint8_t** retdpname, size_t* retdpnamelen,
420335c7cdaSCy Schubert 	uint8_t* dpname_storage, size_t dpname_storage_len);
4215469a995SCy Schubert 
4225469a995SCy Schubert /**
4235469a995SCy Schubert  * Set support for IP4 and IP6 depending on outgoing interfaces
4245469a995SCy Schubert  * in the outside network.  If none, no support, so no use to lookup
4255469a995SCy Schubert  * the AAAA and then attempt to use it if there is no outgoing-interface
4265469a995SCy Schubert  * for it.
4275469a995SCy Schubert  * @param mods: modstack to find iterator module in.
4285469a995SCy Schubert  * @param env: module env, find iterator module (if one) in there.
4295469a995SCy Schubert  * @param outnet: outside network structure.
4305469a995SCy Schubert  */
4315469a995SCy Schubert void iterator_set_ip46_support(struct module_stack* mods,
4325469a995SCy Schubert 	struct module_env* env, struct outside_network* outnet);
433e86b9096SDag-Erling Smørgrav 
43446d2f618SCy Schubert /**
435*be771a7bSCy Schubert  * Read config string that represents the target fetch policy.
436*be771a7bSCy Schubert  * @param target_fetch_policy: alloced on return.
437*be771a7bSCy Schubert  * @param max_dependency_depth: set on return.
438*be771a7bSCy Schubert  * @param str: the config string
439*be771a7bSCy Schubert  * @return false on failure.
440*be771a7bSCy Schubert  */
441*be771a7bSCy Schubert int read_fetch_policy(int** target_fetch_policy, int* max_dependency_depth,
442*be771a7bSCy Schubert 	const char* str);
443*be771a7bSCy Schubert 
444*be771a7bSCy Schubert /**
445*be771a7bSCy Schubert  * Create caps exempt data structure.
446*be771a7bSCy Schubert  * @return NULL on failure.
447*be771a7bSCy Schubert  */
448*be771a7bSCy Schubert struct rbtree_type* caps_white_create(void);
449*be771a7bSCy Schubert 
450*be771a7bSCy Schubert /**
451*be771a7bSCy Schubert  * Delete caps exempt data structure.
452*be771a7bSCy Schubert  * @param caps_white: caps exempt tree.
453*be771a7bSCy Schubert  */
454*be771a7bSCy Schubert void caps_white_delete(struct rbtree_type* caps_white);
455*be771a7bSCy Schubert 
456*be771a7bSCy Schubert /**
457*be771a7bSCy Schubert  * Apply config caps whitelist items to name tree
458*be771a7bSCy Schubert  * @param ntree: caps exempt tree.
459*be771a7bSCy Schubert  * @param cfg: config with options.
460*be771a7bSCy Schubert  */
461*be771a7bSCy Schubert int caps_white_apply_cfg(struct rbtree_type* ntree, struct config_file* cfg);
462*be771a7bSCy Schubert 
463*be771a7bSCy Schubert /**
464*be771a7bSCy Schubert  * Apply config for nat64
465*be771a7bSCy Schubert  * @param nat64: the nat64 state.
466*be771a7bSCy Schubert  * @param cfg: config with options.
467*be771a7bSCy Schubert  * @return false on failure.
468*be771a7bSCy Schubert  */
469*be771a7bSCy Schubert int nat64_apply_cfg(struct iter_nat64* nat64, struct config_file* cfg);
470*be771a7bSCy Schubert 
471*be771a7bSCy Schubert /**
47246d2f618SCy Schubert  * Limit NSEC and NSEC3 TTL in response, RFC9077
47346d2f618SCy Schubert  * @param msg: dns message, the SOA record ttl is used to restrict ttls
47446d2f618SCy Schubert  *	of NSEC and NSEC3 RRsets. If no SOA record, nothing happens.
47546d2f618SCy Schubert  */
47646d2f618SCy Schubert void limit_nsec_ttl(struct dns_msg* msg);
47746d2f618SCy Schubert 
478*be771a7bSCy Schubert /**
479*be771a7bSCy Schubert  * Make the response minimal. Removed authority and additional section,
480*be771a7bSCy Schubert  * that works when there is an answer in the answer section.
481*be771a7bSCy Schubert  * @param rep: reply to modify.
482*be771a7bSCy Schubert  */
483*be771a7bSCy Schubert void iter_make_minimal(struct reply_info* rep);
484*be771a7bSCy Schubert 
485b7579f77SDag-Erling Smørgrav #endif /* ITERATOR_ITER_UTILS_H */
486