2 * iterator/iter_priv.c - iterative resolver private address and domain store
4 * Copyright (c) 2008, 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 LIMITED
25 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
27 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 * POSSIBILITY OF SUCH DAMAGE.
39 * This file contains functions to assist the iterator module.
40 * Keep track of the private addresses and lookup fast.
44 #include <ldns/dname.h>
45 #include "iterator/iter_priv.h"
46 #include "util/regional.h"
48 #include "util/config_file.h"
49 #include "util/data/dname.h"
50 #include "util/data/msgparse.h"
51 #include "util/net_help.h"
52 #include "util/storage/dnstree.h"
54 struct iter_priv* priv_create(void)
56 struct iter_priv* priv = (struct iter_priv*)calloc(1, sizeof(*priv));
59 priv->region = regional_create();
64 addr_tree_init(&priv->a);
65 name_tree_init(&priv->n);
69 void priv_delete(struct iter_priv* priv)
72 regional_destroy(priv->region);
76 /** Read private-addr declarations from config */
77 static int read_addrs(struct iter_priv* priv, struct config_file* cfg)
79 /* parse addresses, report errors, insert into tree */
80 struct config_strlist* p;
81 struct addr_tree_node* n;
82 struct sockaddr_storage addr;
86 for(p = cfg->private_address; p; p = p->next) {
88 if(!netblockstrtoaddr(p->str, UNBOUND_DNS_PORT, &addr,
90 log_err("cannot parse private-address: %s", p->str);
93 n = (struct addr_tree_node*)regional_alloc(priv->region,
96 log_err("out of memory");
99 if(!addr_tree_insert(&priv->a, n, &addr, addrlen, net)) {
100 verbose(VERB_QUERY, "ignoring duplicate "
101 "private-address: %s", p->str);
107 /** Read private-domain declarations from config */
108 static int read_names(struct iter_priv* priv, struct config_file* cfg)
110 /* parse names, report errors, insert into tree */
111 struct config_strlist* p;
112 struct name_tree_node* n;
118 for(p = cfg->private_domain; p; p = p->next) {
120 rdf = ldns_dname_new_frm_str(p->str);
122 log_err("cannot parse private-domain: %s", p->str);
125 nm = ldns_rdf_data(rdf);
126 nm_labs = dname_count_size_labels(nm, &nm_len);
127 nm = (uint8_t*)regional_alloc_init(priv->region, nm, nm_len);
128 ldns_rdf_deep_free(rdf);
130 log_err("out of memory");
133 n = (struct name_tree_node*)regional_alloc(priv->region,
136 log_err("out of memory");
139 if(!name_tree_insert(&priv->n, n, nm, nm_len, nm_labs,
141 verbose(VERB_QUERY, "ignoring duplicate "
142 "private-domain: %s", p->str);
148 int priv_apply_cfg(struct iter_priv* priv, struct config_file* cfg)
150 /* empty the current contents */
151 regional_free_all(priv->region);
152 addr_tree_init(&priv->a);
153 name_tree_init(&priv->n);
155 /* read new contents */
156 if(!read_addrs(priv, cfg))
158 if(!read_names(priv, cfg))
161 /* prepare for lookups */
162 addr_tree_init_parents(&priv->a);
163 name_tree_init_parents(&priv->n);
168 * See if an address is blocked.
169 * @param priv: structure for address storage.
170 * @param addr: address to check
171 * @param addrlen: length of addr.
172 * @return: true if the address must not be queried. false if unlisted.
175 priv_lookup_addr(struct iter_priv* priv, struct sockaddr_storage* addr,
178 return addr_tree_lookup(&priv->a, addr, addrlen) != NULL;
182 * See if a name is whitelisted.
183 * @param priv: structure for address storage.
184 * @param pkt: the packet (for compression ptrs).
185 * @param name: name to check.
186 * @param name_len: uncompressed length of the name to check.
187 * @param dclass: class to check.
188 * @return: true if the name is OK. false if unlisted.
191 priv_lookup_name(struct iter_priv* priv, ldns_buffer* pkt,
192 uint8_t* name, size_t name_len, uint16_t dclass)
197 if(name_len >= sizeof(decomp))
199 dname_pkt_copy(pkt, decomp, name);
200 labs = dname_count_size_labels(decomp, &len);
201 log_assert(name_len == len);
202 return name_tree_lookup(&priv->n, decomp, len, labs, dclass) != NULL;
205 size_t priv_get_mem(struct iter_priv* priv)
208 return sizeof(*priv) + regional_get_mem(priv->region);
211 int priv_rrset_bad(struct iter_priv* priv, ldns_buffer* pkt,
212 struct rrset_parse* rrset)
214 if(priv->a.count == 0)
215 return 0; /* there are no blocked addresses */
217 /* see if it is a private name, that is allowed to have any */
218 if(priv_lookup_name(priv, pkt, rrset->dname, rrset->dname_len,
219 ntohs(rrset->rrset_class))) {
222 /* so its a public name, check the address */
225 if(rrset->type == LDNS_RR_TYPE_A) {
226 struct sockaddr_storage addr;
227 struct sockaddr_in sa;
229 len = (socklen_t)sizeof(sa);
231 sa.sin_family = AF_INET;
232 sa.sin_port = (in_port_t)htons(UNBOUND_DNS_PORT);
233 for(rr = rrset->rr_first; rr; rr = rr->next) {
234 if(ldns_read_uint16(rr->ttl_data+4)
237 memmove(&sa.sin_addr, rr->ttl_data+4+2,
239 memmove(&addr, &sa, len);
240 if(priv_lookup_addr(priv, &addr, len))
243 } else if(rrset->type == LDNS_RR_TYPE_AAAA) {
244 struct sockaddr_storage addr;
245 struct sockaddr_in6 sa;
246 len = (socklen_t)sizeof(sa);
248 sa.sin6_family = AF_INET6;
249 sa.sin6_port = (in_port_t)htons(UNBOUND_DNS_PORT);
250 for(rr = rrset->rr_first; rr; rr = rr->next) {
251 if(ldns_read_uint16(rr->ttl_data+4)
254 memmove(&sa.sin6_addr, rr->ttl_data+4+2,
256 memmove(&addr, &sa, len);
257 if(priv_lookup_addr(priv, &addr, len))