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