xref: /freebsd/contrib/unbound/iterator/iter_delegpt.h (revision 5f4c09dd85bff675e0ca63c55ea3c517e0fddfcc)
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 	/** for assertions on type of delegpt */
83 	uint8_t dp_type_mlc;
84 	/** use SSL for upstream query */
85 	uint8_t ssl_upstream;
86 	/** use TCP for upstream query */
87 	uint8_t tcp_upstream;
88 	/** delegpt from authoritative zone that is locally hosted */
89 	uint8_t auth_dp;
90 	/*** no cache */
91 	int no_cache;
92 };
93 
94 /**
95  * Nameservers for a delegation point.
96  */
97 struct delegpt_ns {
98 	/** next in list */
99 	struct delegpt_ns* next;
100 	/** name of nameserver */
101 	uint8_t* name;
102 	/** length of name */
103 	size_t namelen;
104 	/** number of cache lookups for the name */
105 	int cache_lookup_count;
106 	/**
107 	 * If the name has been resolved. false if not queried for yet.
108 	 * true if the A, AAAA queries have been generated.
109 	 * marked true if those queries fail.
110 	 * and marked true if got4 and got6 are both true.
111 	 */
112 	int resolved;
113 	/** if the ipv4 address is in the delegpt, 0=not, 1=yes 2=negative,
114 	 * negative means it was done, but no content. */
115 	uint8_t got4;
116 	/** if the ipv6 address is in the delegpt, 0=not, 1=yes 2=negative */
117 	uint8_t got6;
118 	/**
119 	 * If the name is parent-side only and thus dispreferred.
120 	 * Its addresses become dispreferred as well
121 	 */
122 	uint8_t lame;
123 	/** if the parent-side ipv4 address has been looked up (last resort).
124 	 * Also enabled if a parent-side cache entry exists, or a parent-side
125 	 * negative-cache entry exists. */
126 	uint8_t done_pside4;
127 	/** if the parent-side ipv6 address has been looked up (last resort).
128 	 * Also enabled if a parent-side cache entry exists, or a parent-side
129 	 * negative-cache entry exists. */
130 	uint8_t done_pside6;
131 	/** the TLS authentication name, (if not NULL) to use. */
132 	char* tls_auth_name;
133 	/** the port to use; it should mostly be the default 53 but configured
134 	 *  upstreams can provide nondefault ports. */
135 	int port;
136 };
137 
138 /**
139  * Address of target nameserver in delegation point.
140  */
141 struct delegpt_addr {
142 	/** next delegation point in results */
143 	struct delegpt_addr* next_result;
144 	/** next delegation point in usable list */
145 	struct delegpt_addr* next_usable;
146 	/** next delegation point in all targets list */
147 	struct delegpt_addr* next_target;
148 
149 	/** delegation point address */
150 	struct sockaddr_storage addr;
151 	/** length of addr */
152 	socklen_t addrlen;
153 	/** number of attempts for this addr */
154 	int attempts;
155 	/** rtt stored here in the selection algorithm */
156 	int sel_rtt;
157 	/** if true, the A or AAAA RR was bogus, so this address is bad.
158 	 * Also check the dp->bogus to see if everything is bogus. */
159 	uint8_t bogus;
160 	/** if true, this address is dispreferred: it is a lame IP address */
161 	uint8_t lame;
162 	/** if the address is dnsseclame, but this cannot be cached, this
163 	 * option is useful to mark the address dnsseclame.
164 	 * This value is not copied in addr-copy and dp-copy. */
165 	uint8_t dnsseclame;
166 	/** the TLS authentication name, (if not NULL) to use. */
167 	char* tls_auth_name;
168 };
169 
170 /**
171  * Create new delegation point.
172  * @param regional: where to allocate it.
173  * @return new delegation point or NULL on error.
174  */
175 struct delegpt* delegpt_create(struct regional* regional);
176 
177 /**
178  * Create a copy of a delegation point.
179  * @param dp: delegation point to copy.
180  * @param regional: where to allocate it.
181  * @return new delegation point or NULL on error.
182  */
183 struct delegpt* delegpt_copy(struct delegpt* dp, struct regional* regional);
184 
185 /**
186  * Set name of delegation point.
187  * @param dp: delegation point.
188  * @param regional: where to allocate the name copy.
189  * @param name: name to use.
190  * @return false on error.
191  */
192 int delegpt_set_name(struct delegpt* dp, struct regional* regional,
193 	uint8_t* name);
194 
195 /**
196  * Add a name to the delegation point.
197  * @param dp: delegation point.
198  * @param regional: where to allocate the info.
199  * @param name: domain name in wire format.
200  * @param lame: name is lame, disprefer it.
201  * @param tls_auth_name: TLS authentication name (or NULL).
202  * @param port: port to use for resolved addresses.
203  * @return false on error.
204  */
205 int delegpt_add_ns(struct delegpt* dp, struct regional* regional,
206 	uint8_t* name, uint8_t lame, char* tls_auth_name, int port);
207 
208 /**
209  * Add NS rrset; calls add_ns repeatedly.
210  * @param dp: delegation point.
211  * @param regional: where to allocate the info.
212  * @param ns_rrset: NS rrset.
213  * @param lame: rrset is lame, disprefer it.
214  * @return 0 on alloc error.
215  */
216 int delegpt_rrset_add_ns(struct delegpt* dp, struct regional* regional,
217 	struct ub_packed_rrset_key* ns_rrset, uint8_t lame);
218 
219 /**
220  * Add target address to the delegation point.
221  * @param dp: delegation point.
222  * @param regional: where to allocate the info.
223  * @param name: name for which target was found (must be in nslist).
224  *	This name is marked resolved.
225  * @param namelen: length of name.
226  * @param addr: the address.
227  * @param addrlen: the length of addr.
228  * @param bogus: security status for the address, pass true if bogus.
229  * @param lame: address is lame.
230  * @param additions: will be set to 1 if a new address is added
231  * @return false on error.
232  */
233 int delegpt_add_target(struct delegpt* dp, struct regional* regional,
234 	uint8_t* name, size_t namelen, struct sockaddr_storage* addr,
235 	socklen_t addrlen, uint8_t bogus, uint8_t lame, int* additions);
236 
237 /**
238  * Add A RRset to delegpt.
239  * @param dp: delegation point.
240  * @param regional: where to allocate the info.
241  * @param rrset: RRset A to add.
242  * @param lame: rrset is lame, disprefer it.
243  * @param additions: will be set to 1 if a new address is added
244  * @return 0 on alloc error.
245  */
246 int delegpt_add_rrset_A(struct delegpt* dp, struct regional* regional,
247 	struct ub_packed_rrset_key* rrset, uint8_t lame, int* additions);
248 
249 /**
250  * Add AAAA RRset to delegpt.
251  * @param dp: delegation point.
252  * @param regional: where to allocate the info.
253  * @param rrset: RRset AAAA to add.
254  * @param lame: rrset is lame, disprefer it.
255  * @param additions: will be set to 1 if a new address is added
256  * @return 0 on alloc error.
257  */
258 int delegpt_add_rrset_AAAA(struct delegpt* dp, struct regional* regional,
259 	struct ub_packed_rrset_key* rrset, uint8_t lame, int* additions);
260 
261 /**
262  * Add any RRset to delegpt.
263  * Does not check for duplicates added.
264  * @param dp: delegation point.
265  * @param regional: where to allocate the info.
266  * @param rrset: RRset to add, NS, A, AAAA.
267  * @param lame: rrset is lame, disprefer it.
268  * @param additions: will be set to 1 if a new address is added
269  * @return 0 on alloc error.
270  */
271 int delegpt_add_rrset(struct delegpt* dp, struct regional* regional,
272 	struct ub_packed_rrset_key* rrset, uint8_t lame, int* additions);
273 
274 /**
275  * Add address to the delegation point. No servername is associated or checked.
276  * @param dp: delegation point.
277  * @param regional: where to allocate the info.
278  * @param addr: the address.
279  * @param addrlen: the length of addr.
280  * @param bogus: if address is bogus.
281  * @param lame: if address is lame.
282  * @param tls_auth_name: TLS authentication name (or NULL).
283  * @param port: the port to use; if -1 the port is taken from addr.
284  * @param additions: will be set to 1 if a new address is added
285  * @return false on error.
286  */
287 int delegpt_add_addr(struct delegpt* dp, struct regional* regional,
288 	struct sockaddr_storage* addr, socklen_t addrlen,
289 	uint8_t bogus, uint8_t lame, char* tls_auth_name, int port,
290 	int* additions);
291 
292 /**
293  * Find NS record in name list of delegation point.
294  * @param dp: delegation point.
295  * @param name: name of nameserver to look for, uncompressed wireformat.
296  * @param namelen: length of name.
297  * @return the ns structure or NULL if not found.
298  */
299 struct delegpt_ns* delegpt_find_ns(struct delegpt* dp, uint8_t* name,
300 	size_t namelen);
301 
302 /**
303  * Find address record in total list of delegation point.
304  * @param dp: delegation point.
305  * @param addr: address
306  * @param addrlen: length of addr
307  * @return the addr structure or NULL if not found.
308  */
309 struct delegpt_addr* delegpt_find_addr(struct delegpt* dp,
310 	struct sockaddr_storage* addr, socklen_t addrlen);
311 
312 /**
313  * Print the delegation point to the log. For debugging.
314  * @param v: verbosity value that is needed to emit to log.
315  * @param dp: delegation point.
316  */
317 void delegpt_log(enum verbosity_value v, struct delegpt* dp);
318 
319 /** count NS and number missing for logging */
320 void delegpt_count_ns(struct delegpt* dp, size_t* numns, size_t* missing);
321 
322 /** count addresses, and number in result and available lists, for logging */
323 void delegpt_count_addr(struct delegpt* dp, size_t* numaddr, size_t* numres,
324 	size_t* numavail);
325 
326 /**
327  * Add all usable targets to the result list.
328  * @param dp: delegation point.
329  */
330 void delegpt_add_unused_targets(struct delegpt* dp);
331 
332 /**
333  * Count number of missing targets. These are ns names with no resolved flag.
334  * @param dp: delegation point.
335  * @param alllame: if set, check if all the missing targets are lame.
336  * @return number of missing targets (or 0).
337  */
338 size_t delegpt_count_missing_targets(struct delegpt* dp, int* alllame);
339 
340 /** count total number of targets in dp */
341 size_t delegpt_count_targets(struct delegpt* dp);
342 
343 /**
344  * Create new delegation point from a dns message
345  *
346  * Note that this method does not actually test to see if the message is an
347  * actual referral. It really is just checking to see if it can construct a
348  * delegation point, so the message could be of some other type (some ANSWER
349  * messages, some CNAME messages, generally.) Note that the resulting
350  * DelegationPoint will contain targets for all "relevant" glue (i.e.,
351  * address records whose ownernames match the target of one of the NS
352  * records), so if policy dictates that some glue should be discarded beyond
353  * that, discard it before calling this method. Note that this method will
354  * find "glue" in either the ADDITIONAL section or the ANSWER section.
355  *
356  * @param msg: the dns message, referral.
357  * @param regional: where to allocate delegation point.
358  * @return new delegation point or NULL on alloc error, or if the
359  *         message was not appropriate.
360  */
361 struct delegpt* delegpt_from_message(struct dns_msg* msg,
362 	struct regional* regional);
363 
364 /**
365  * Mark negative return in delegation point for specific nameserver.
366  * sets the got4 or got6 to negative, updates the ns->resolved.
367  * @param ns: the nameserver in the delegpt.
368  * @param qtype: A or AAAA (host order).
369  */
370 void delegpt_mark_neg(struct delegpt_ns* ns, uint16_t qtype);
371 
372 /**
373  * Add negative message to delegation point.
374  * @param dp: delegation point.
375  * @param msg: the message added, marks off A or AAAA from an NS entry.
376  */
377 void delegpt_add_neg_msg(struct delegpt* dp, struct msgreply_entry* msg);
378 
379 /**
380  * Register the fact that there is no ipv6 and thus AAAAs are not going
381  * to be queried for or be useful.
382  * @param dp: the delegation point. Updated to reflect no ipv6.
383  */
384 void delegpt_no_ipv6(struct delegpt* dp);
385 
386 /**
387  * Register the fact that there is no ipv4 and thus As are not going
388  * to be queried for or be useful.
389  * @param dp: the delegation point. Updated to reflect no ipv4.
390  */
391 void delegpt_no_ipv4(struct delegpt* dp);
392 
393 /**
394  * create malloced delegation point, with the given name
395  * @param name: uncompressed wireformat of delegpt name.
396  * @return NULL on alloc failure
397  */
398 struct delegpt* delegpt_create_mlc(uint8_t* name);
399 
400 /**
401  * free malloced delegation point.
402  * @param dp: must have been created with delegpt_create_mlc, free'd.
403  */
404 void delegpt_free_mlc(struct delegpt* dp);
405 
406 /**
407  * Set name of delegation point.
408  * @param dp: delegation point. malloced.
409  * @param name: name to use.
410  * @return false on error.
411  */
412 int delegpt_set_name_mlc(struct delegpt* dp, uint8_t* name);
413 
414 /**
415  * add a name to malloced delegation point.
416  * @param dp: must have been created with delegpt_create_mlc.
417  * @param name: the name to add.
418  * @param lame: the name is lame, disprefer.
419  * @param tls_auth_name: TLS authentication name (or NULL).
420  * @param port: port to use for resolved addresses.
421  * @return false on error.
422  */
423 int delegpt_add_ns_mlc(struct delegpt* dp, uint8_t* name, uint8_t lame,
424 	char* tls_auth_name, int port);
425 
426 /**
427  * add an address to a malloced delegation point.
428  * @param dp: must have been created with delegpt_create_mlc.
429  * @param addr: the address.
430  * @param addrlen: the length of addr.
431  * @param bogus: if address is bogus.
432  * @param lame: if address is lame.
433  * @param tls_auth_name: TLS authentication name (or NULL).
434  * @param port: the port to use; if -1 the port is taken from addr.
435  * @return false on error.
436  */
437 int delegpt_add_addr_mlc(struct delegpt* dp, struct sockaddr_storage* addr,
438 	socklen_t addrlen, uint8_t bogus, uint8_t lame, char* tls_auth_name,
439 	int port);
440 
441 /**
442  * Add target address to the delegation point.
443  * @param dp: must have been created with delegpt_create_mlc.
444  * @param name: name for which target was found (must be in nslist).
445  *	This name is marked resolved.
446  * @param namelen: length of name.
447  * @param addr: the address.
448  * @param addrlen: the length of addr.
449  * @param bogus: security status for the address, pass true if bogus.
450  * @param lame: address is lame.
451  * @return false on error.
452  */
453 int delegpt_add_target_mlc(struct delegpt* dp, uint8_t* name, size_t namelen,
454 	struct sockaddr_storage* addr, socklen_t addrlen, uint8_t bogus,
455 	uint8_t lame);
456 
457 /** get memory in use by dp */
458 size_t delegpt_get_mem(struct delegpt* dp);
459 
460 /**
461  * See if the addr is on the result list.
462  * @param dp: delegation point.
463  * @param find: the pointer is searched for on the result list.
464  * @return 1 if found, 0 if not found.
465  */
466 int delegpt_addr_on_result_list(struct delegpt* dp, struct delegpt_addr* find);
467 
468 /**
469  * Remove the addr from the usable list.
470  * @param dp: the delegation point.
471  * @param del: the addr to remove from the list, the pointer is searched for.
472  */
473 void delegpt_usable_list_remove_addr(struct delegpt* dp,
474 	struct delegpt_addr* del);
475 
476 /**
477  * Add the delegpt_addr back to the result list, if it is not already on
478  * the result list. Also removes it from the usable list.
479  * @param dp: delegation point.
480  * @param a: addr to add, nothing happens if it is already on the result list.
481  *	It is removed from the usable list.
482  */
483 void delegpt_add_to_result_list(struct delegpt* dp, struct delegpt_addr* a);
484 
485 #endif /* ITERATOR_ITER_DELEGPT_H */
486