xref: /freebsd/contrib/unbound/iterator/iter_delegpt.h (revision 7a789145f88a6aceacc59029a0cafe7de7aeefea)
1 /*
2  * iterator/iter_delegpt.h - delegation point with NS and address information.
3  *
4  * Copyright (c) 2007, NLnet Labs. All rights reserved.
5  *
6  * This software is open source.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * Redistributions of source code must retain the above copyright notice,
13  * this list of conditions and the following disclaimer.
14  *
15  * Redistributions in binary form must reproduce the above copyright notice,
16  * this list of conditions and the following disclaimer in the documentation
17  * and/or other materials provided with the distribution.
18  *
19  * Neither the name of the NLNET LABS nor the names of its contributors may
20  * be used to endorse or promote products derived from this software without
21  * specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27  * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
29  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34  */
35 
36 /**
37  * \file
38  *
39  * This file implements the Delegation Point. It contains a list of name servers
40  * and their addresses if known.
41  */
42 
43 #ifndef ITERATOR_ITER_DELEGPT_H
44 #define ITERATOR_ITER_DELEGPT_H
45 #include "util/log.h"
46 struct regional;
47 struct delegpt_ns;
48 struct delegpt_addr;
49 struct dns_msg;
50 struct ub_packed_rrset_key;
51 struct msgreply_entry;
52 
53 /**
54  * Delegation Point.
55  * For a domain name, the NS rrset, and the A and AAAA records for those.
56  */
57 struct delegpt {
58 	/** the domain name of the delegation point. */
59 	uint8_t* name;
60 	/** length of the delegation point name */
61 	size_t namelen;
62 	/** number of labels in delegation point */
63 	int namelabs;
64 
65 	/** the nameservers, names from the NS RRset rdata. */
66 	struct delegpt_ns* nslist;
67 	/** the target addresses for delegation */
68 	struct delegpt_addr* target_list;
69 	/** the list of usable targets; subset of target_list
70 	 * the items in this list are not part of the result list.  */
71 	struct delegpt_addr* usable_list;
72 	/** the list of returned targets; subset of target_list */
73 	struct delegpt_addr* result_list;
74 
75 	/** if true, the NS RRset was bogus. All info is bad. */
76 	int bogus;
77 	/** if true, the parent-side NS record has been applied:
78 	 * its names have been added and their addresses can follow later.
79 	 * Also true if the delegationpoint was created from a delegation
80 	 * message and thus contains the parent-side-info already. */
81 	uint8_t has_parent_side_NS;
82 	/** if true, the delegation point has reached last resort processing
83 	 *  and the parent side information has been possibly added to the
84 	 *  delegation point.
85 	 *  For now this signals that further target lookups will ignore
86 	 *  the configured target-fetch-policy and only resolve on
87 	 *  demand to try and avoid triggering limits at this stage (.i.e, it
88 	 *  is very likely that the A/AAAA queries for the newly added name
89 	 *  servers will not yield new IP addresses and trigger NXNS
90 	 *  countermeasures. */
91 	uint8_t fallback_to_parent_side_NS;
92 	/** for assertions on type of delegpt */
93 	uint8_t dp_type_mlc;
94 	/** use SSL for upstream query */
95 	uint8_t ssl_upstream;
96 	/** use TCP for upstream query */
97 	uint8_t tcp_upstream;
98 	/** delegpt from authoritative zone that is locally hosted */
99 	uint8_t auth_dp;
100 	/*** no cache */
101 	int no_cache;
102 };
103 
104 /**
105  * Nameservers for a delegation point.
106  */
107 struct delegpt_ns {
108 	/** next in list */
109 	struct delegpt_ns* next;
110 	/** name of nameserver */
111 	uint8_t* name;
112 	/** length of name */
113 	size_t namelen;
114 	/** number of cache lookups for the name */
115 	int cache_lookup_count;
116 	/**
117 	 * If the name has been resolved. false if not queried for yet.
118 	 * true if the A, AAAA queries have been generated.
119 	 * marked true if those queries fail.
120 	 * and marked true if got4 and got6 are both true.
121 	 */
122 	int resolved;
123 	/** if the ipv4 address is in the delegpt, 0=not, 1=yes 2=negative,
124 	 * negative means it was done, but no content. */
125 	uint8_t got4;
126 	/** if the ipv6 address is in the delegpt, 0=not, 1=yes 2=negative */
127 	uint8_t got6;
128 	/**
129 	 * If the name is parent-side only and thus dispreferred.
130 	 * Its addresses become dispreferred as well
131 	 */
132 	uint8_t lame;
133 	/** if the parent-side ipv4 address has been looked up (last resort).
134 	 * Also enabled if a parent-side cache entry exists, or a parent-side
135 	 * negative-cache entry exists. */
136 	uint8_t done_pside4;
137 	/** if the parent-side ipv6 address has been looked up (last resort).
138 	 * Also enabled if a parent-side cache entry exists, or a parent-side
139 	 * negative-cache entry exists. */
140 	uint8_t done_pside6;
141 	/** the TLS authentication name, (if not NULL) to use. */
142 	char* tls_auth_name;
143 	/** the port to use; it should mostly be the default 53 but configured
144 	 *  upstreams can provide nondefault ports. */
145 	int port;
146 };
147 
148 /**
149  * Address of target nameserver in delegation point.
150  */
151 struct delegpt_addr {
152 	/** next delegation point in results */
153 	struct delegpt_addr* next_result;
154 	/** next delegation point in usable list */
155 	struct delegpt_addr* next_usable;
156 	/** next delegation point in all targets list */
157 	struct delegpt_addr* next_target;
158 
159 	/** delegation point address */
160 	struct sockaddr_storage addr;
161 	/** length of addr */
162 	socklen_t addrlen;
163 	/** number of attempts for this addr */
164 	int attempts;
165 	/** rtt stored here in the selection algorithm */
166 	int sel_rtt;
167 	/** if true, the A or AAAA RR was bogus, so this address is bad.
168 	 * Also check the dp->bogus to see if everything is bogus. */
169 	uint8_t bogus;
170 	/** if true, this address is dispreferred: it is a lame IP address */
171 	uint8_t lame;
172 	/** if the address is dnsseclame, but this cannot be cached, this
173 	 * option is useful to mark the address dnsseclame.
174 	 * This value is not copied in addr-copy and dp-copy. */
175 	uint8_t dnsseclame;
176 	/** the TLS authentication name, (if not NULL) to use. */
177 	char* tls_auth_name;
178 };
179 
180 /**
181  * Create new delegation point.
182  * @param regional: where to allocate it.
183  * @return new delegation point or NULL on error.
184  */
185 struct delegpt* delegpt_create(struct regional* regional);
186 
187 /**
188  * Create a copy of a delegation point.
189  * @param dp: delegation point to copy.
190  * @param regional: where to allocate it.
191  * @return new delegation point or NULL on error.
192  */
193 struct delegpt* delegpt_copy(struct delegpt* dp, struct regional* regional);
194 
195 /**
196  * Set name of delegation point.
197  * @param dp: delegation point.
198  * @param regional: where to allocate the name copy.
199  * @param name: name to use.
200  * @return false on error.
201  */
202 int delegpt_set_name(struct delegpt* dp, struct regional* regional,
203 	uint8_t* name);
204 
205 /**
206  * Add a name to the delegation point.
207  * @param dp: delegation point.
208  * @param regional: where to allocate the info.
209  * @param name: domain name in wire format.
210  * @param lame: name is lame, disprefer it.
211  * @param tls_auth_name: TLS authentication name (or NULL).
212  * @param port: port to use for resolved addresses.
213  * @return false on error.
214  */
215 int delegpt_add_ns(struct delegpt* dp, struct regional* regional,
216 	uint8_t* name, uint8_t lame, char* tls_auth_name, int port);
217 
218 /**
219  * Add NS rrset; calls add_ns repeatedly.
220  * @param dp: delegation point.
221  * @param regional: where to allocate the info.
222  * @param ns_rrset: NS rrset.
223  * @param lame: rrset is lame, disprefer it.
224  * @param port: port or -1 if not set.
225  * @return 0 on alloc error.
226  */
227 int delegpt_rrset_add_ns(struct delegpt* dp, struct regional* regional,
228 	struct ub_packed_rrset_key* ns_rrset, uint8_t lame, int port);
229 
230 /**
231  * Add target address to the delegation point.
232  * @param dp: delegation point.
233  * @param regional: where to allocate the info.
234  * @param name: name for which target was found (must be in nslist).
235  *	This name is marked resolved.
236  * @param namelen: length of name.
237  * @param addr: the address.
238  * @param addrlen: the length of addr.
239  * @param bogus: security status for the address, pass true if bogus.
240  * @param lame: address is lame.
241  * @param additions: will be set to 1 if a new address is added
242  * @return false on error.
243  */
244 int delegpt_add_target(struct delegpt* dp, struct regional* regional,
245 	uint8_t* name, size_t namelen, struct sockaddr_storage* addr,
246 	socklen_t addrlen, uint8_t bogus, uint8_t lame, int* additions);
247 
248 /**
249  * Add A RRset to delegpt.
250  * @param dp: delegation point.
251  * @param regional: where to allocate the info.
252  * @param rrset: RRset A to add.
253  * @param lame: rrset is lame, disprefer it.
254  * @param additions: will be set to 1 if a new address is added
255  * @return 0 on alloc error.
256  */
257 int delegpt_add_rrset_A(struct delegpt* dp, struct regional* regional,
258 	struct ub_packed_rrset_key* rrset, uint8_t lame, int* additions);
259 
260 /**
261  * Add AAAA RRset to delegpt.
262  * @param dp: delegation point.
263  * @param regional: where to allocate the info.
264  * @param rrset: RRset AAAA to add.
265  * @param lame: rrset is lame, disprefer it.
266  * @param additions: will be set to 1 if a new address is added
267  * @return 0 on alloc error.
268  */
269 int delegpt_add_rrset_AAAA(struct delegpt* dp, struct regional* regional,
270 	struct ub_packed_rrset_key* rrset, uint8_t lame, int* additions);
271 
272 /**
273  * Add any RRset to delegpt.
274  * Does not check for duplicates added.
275  * @param dp: delegation point.
276  * @param regional: where to allocate the info.
277  * @param rrset: RRset to add, NS, A, AAAA.
278  * @param lame: rrset is lame, disprefer it.
279  * @param additions: will be set to 1 if a new address is added
280  * @return 0 on alloc error.
281  */
282 int delegpt_add_rrset(struct delegpt* dp, struct regional* regional,
283 	struct ub_packed_rrset_key* rrset, uint8_t lame, int* additions);
284 
285 /**
286  * Add address to the delegation point. No servername is associated or checked.
287  * @param dp: delegation point.
288  * @param regional: where to allocate the info.
289  * @param addr: the address.
290  * @param addrlen: the length of addr.
291  * @param bogus: if address is bogus.
292  * @param lame: if address is lame.
293  * @param tls_auth_name: TLS authentication name (or NULL).
294  * @param port: the port to use; if -1 the port is taken from addr.
295  * @param additions: will be set to 1 if a new address is added
296  * @return false on error.
297  */
298 int delegpt_add_addr(struct delegpt* dp, struct regional* regional,
299 	struct sockaddr_storage* addr, socklen_t addrlen,
300 	uint8_t bogus, uint8_t lame, char* tls_auth_name, int port,
301 	int* additions);
302 
303 /**
304  * Find NS record in name list of delegation point.
305  * @param dp: delegation point.
306  * @param name: name of nameserver to look for, uncompressed wireformat.
307  * @param namelen: length of name.
308  * @return the ns structure or NULL if not found.
309  */
310 struct delegpt_ns* delegpt_find_ns(struct delegpt* dp, uint8_t* name,
311 	size_t namelen);
312 
313 /**
314  * Find address record in total list of delegation point.
315  * @param dp: delegation point.
316  * @param addr: address
317  * @param addrlen: length of addr
318  * @return the addr structure or NULL if not found.
319  */
320 struct delegpt_addr* delegpt_find_addr(struct delegpt* dp,
321 	struct sockaddr_storage* addr, socklen_t addrlen);
322 
323 /**
324  * Print the delegation point to the log. For debugging.
325  * @param v: verbosity value that is needed to emit to log.
326  * @param dp: delegation point.
327  */
328 void delegpt_log(enum verbosity_value v, struct delegpt* dp);
329 
330 /** count NS and number missing for logging */
331 void delegpt_count_ns(struct delegpt* dp, size_t* numns, size_t* missing);
332 
333 /** count addresses, and number in result and available lists, for logging */
334 void delegpt_count_addr(struct delegpt* dp, size_t* numaddr, size_t* numres,
335 	size_t* numavail);
336 
337 /**
338  * Add all usable targets to the result list.
339  * @param dp: delegation point.
340  */
341 void delegpt_add_unused_targets(struct delegpt* dp);
342 
343 /**
344  * Count number of missing targets. These are ns names with no resolved flag.
345  * @param dp: delegation point.
346  * @param alllame: if set, check if all the missing targets are lame.
347  * @return number of missing targets (or 0).
348  */
349 size_t delegpt_count_missing_targets(struct delegpt* dp, int* alllame);
350 
351 /** count total number of targets in dp */
352 size_t delegpt_count_targets(struct delegpt* dp);
353 
354 /**
355  * Create new delegation point from a dns message
356  *
357  * Note that this method does not actually test to see if the message is an
358  * actual referral. It really is just checking to see if it can construct a
359  * delegation point, so the message could be of some other type (some ANSWER
360  * messages, some CNAME messages, generally.) Note that the resulting
361  * DelegationPoint will contain targets for all "relevant" glue (i.e.,
362  * address records whose ownernames match the target of one of the NS
363  * records), so if policy dictates that some glue should be discarded beyond
364  * that, discard it before calling this method. Note that this method will
365  * find "glue" in either the ADDITIONAL section or the ANSWER section.
366  *
367  * @param msg: the dns message, referral.
368  * @param regional: where to allocate delegation point.
369  * @param port: if not -1 specifies a port number.
370  * @return new delegation point or NULL on alloc error, or if the
371  *         message was not appropriate.
372  */
373 struct delegpt* delegpt_from_message(struct dns_msg* msg,
374 	struct regional* regional, int port);
375 
376 /**
377  * Mark negative return in delegation point for specific nameserver.
378  * sets the got4 or got6 to negative, updates the ns->resolved.
379  * @param ns: the nameserver in the delegpt.
380  * @param qtype: A or AAAA (host order).
381  */
382 void delegpt_mark_neg(struct delegpt_ns* ns, uint16_t qtype);
383 
384 /**
385  * Add negative message to delegation point.
386  * @param dp: delegation point.
387  * @param msg: the message added, marks off A or AAAA from an NS entry.
388  */
389 void delegpt_add_neg_msg(struct delegpt* dp, struct msgreply_entry* msg);
390 
391 /**
392  * Register the fact that there is no ipv6 and thus AAAAs are not going
393  * to be queried for or be useful.
394  * @param dp: the delegation point. Updated to reflect no ipv6.
395  */
396 void delegpt_no_ipv6(struct delegpt* dp);
397 
398 /**
399  * Register the fact that there is no ipv4 and thus As are not going
400  * to be queried for or be useful.
401  * @param dp: the delegation point. Updated to reflect no ipv4.
402  */
403 void delegpt_no_ipv4(struct delegpt* dp);
404 
405 /**
406  * create malloced delegation point, with the given name
407  * @param name: uncompressed wireformat of delegpt name.
408  * @return NULL on alloc failure
409  */
410 struct delegpt* delegpt_create_mlc(uint8_t* name);
411 
412 /**
413  * free malloced delegation point.
414  * @param dp: must have been created with delegpt_create_mlc, free'd.
415  */
416 void delegpt_free_mlc(struct delegpt* dp);
417 
418 /**
419  * Set name of delegation point.
420  * @param dp: delegation point. malloced.
421  * @param name: name to use.
422  * @return false on error.
423  */
424 int delegpt_set_name_mlc(struct delegpt* dp, uint8_t* name);
425 
426 /**
427  * add a name to malloced delegation point.
428  * @param dp: must have been created with delegpt_create_mlc.
429  * @param name: the name to add.
430  * @param lame: the name is lame, disprefer.
431  * @param tls_auth_name: TLS authentication name (or NULL).
432  * @param port: port to use for resolved addresses.
433  * @return false on error.
434  */
435 int delegpt_add_ns_mlc(struct delegpt* dp, uint8_t* name, uint8_t lame,
436 	char* tls_auth_name, int port);
437 
438 /**
439  * add an address to a malloced delegation point.
440  * @param dp: must have been created with delegpt_create_mlc.
441  * @param addr: the address.
442  * @param addrlen: the length of addr.
443  * @param bogus: if address is bogus.
444  * @param lame: if address is lame.
445  * @param tls_auth_name: TLS authentication name (or NULL).
446  * @param port: the port to use; if -1 the port is taken from addr.
447  * @return false on error.
448  */
449 int delegpt_add_addr_mlc(struct delegpt* dp, struct sockaddr_storage* addr,
450 	socklen_t addrlen, uint8_t bogus, uint8_t lame, char* tls_auth_name,
451 	int port);
452 
453 /**
454  * Add target address to the delegation point.
455  * @param dp: must have been created with delegpt_create_mlc.
456  * @param name: name for which target was found (must be in nslist).
457  *	This name is marked resolved.
458  * @param namelen: length of name.
459  * @param addr: the address.
460  * @param addrlen: the length of addr.
461  * @param bogus: security status for the address, pass true if bogus.
462  * @param lame: address is lame.
463  * @return false on error.
464  */
465 int delegpt_add_target_mlc(struct delegpt* dp, uint8_t* name, size_t namelen,
466 	struct sockaddr_storage* addr, socklen_t addrlen, uint8_t bogus,
467 	uint8_t lame);
468 
469 /** get memory in use by dp */
470 size_t delegpt_get_mem(struct delegpt* dp);
471 
472 /**
473  * See if the addr is on the result list.
474  * @param dp: delegation point.
475  * @param find: the pointer is searched for on the result list.
476  * @return 1 if found, 0 if not found.
477  */
478 int delegpt_addr_on_result_list(struct delegpt* dp, struct delegpt_addr* find);
479 
480 /**
481  * Remove the addr from the usable list.
482  * @param dp: the delegation point.
483  * @param del: the addr to remove from the list, the pointer is searched for.
484  */
485 void delegpt_usable_list_remove_addr(struct delegpt* dp,
486 	struct delegpt_addr* del);
487 
488 /**
489  * Add the delegpt_addr back to the result list, if it is not already on
490  * the result list. Also removes it from the usable list.
491  * @param dp: delegation point.
492  * @param a: addr to add, nothing happens if it is already on the result list.
493  *	It is removed from the usable list.
494  */
495 void delegpt_add_to_result_list(struct delegpt* dp, struct delegpt_addr* a);
496 
497 #endif /* ITERATOR_ITER_DELEGPT_H */
498