2 * edns-subnet/subnet-whitelist.c - Hosts we actively try to send subnet option
5 * Copyright (c) 2013, NLnet Labs. All rights reserved.
7 * This software is open source.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
13 * Redistributions of source code must retain the above copyright notice,
14 * this list of conditions and the following disclaimer.
16 * Redistributions in binary form must reproduce the above copyright notice,
17 * this list of conditions and the following disclaimer in the documentation
18 * and/or other materials provided with the distribution.
20 * Neither the name of the NLNET LABS nor the names of its contributors may
21 * be used to endorse or promote products derived from this software without
22 * specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
27 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
28 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
30 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
31 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
32 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
33 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
34 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 * Keep track of the white listed servers for subnet option. Based
45 #ifdef CLIENT_SUBNET /* keeps splint happy */
46 #include "edns-subnet/edns-subnet.h"
47 #include "edns-subnet/subnet-whitelist.h"
48 #include "util/regional.h"
50 #include "util/config_file.h"
51 #include "util/net_help.h"
52 #include "util/storage/dnstree.h"
53 #include "sldns/str2wire.h"
54 #include "util/data/dname.h"
57 ecs_whitelist_create(void)
59 struct ecs_whitelist* whitelist =
60 (struct ecs_whitelist*)calloc(1,
61 sizeof(struct ecs_whitelist));
64 whitelist->region = regional_create();
65 if(!whitelist->region) {
66 ecs_whitelist_delete(whitelist);
73 ecs_whitelist_delete(struct ecs_whitelist* whitelist)
77 regional_destroy(whitelist->region);
81 /** insert new address into whitelist structure */
83 upstream_insert(struct ecs_whitelist* whitelist,
84 struct sockaddr_storage* addr, socklen_t addrlen, int net)
86 struct addr_tree_node* node = (struct addr_tree_node*)regional_alloc(
87 whitelist->region, sizeof(*node));
90 if(!addr_tree_insert(&whitelist->upstream, node, addr, addrlen, net)) {
92 "duplicate send-client-subnet address ignored.");
97 /** apply edns-subnet string */
99 upstream_str_cfg(struct ecs_whitelist* whitelist, const char* str)
101 struct sockaddr_storage addr;
104 verbose(VERB_ALGO, "send-client-subnet: %s", str);
105 if(!netblockstrtoaddr(str, UNBOUND_DNS_PORT, &addr, &addrlen, &net)) {
106 log_err("cannot parse send-client-subnet netblock: %s", str);
109 if(!upstream_insert(whitelist, &addr, addrlen, net)) {
110 log_err("out of memory");
116 /** read client_subnet config */
118 read_upstream(struct ecs_whitelist* whitelist, struct config_file* cfg)
120 struct config_strlist* p;
121 for(p = cfg->client_subnet; p; p = p->next) {
123 if(!upstream_str_cfg(whitelist, p->str))
129 /** read client_subnet_zone config */
131 read_names(struct ecs_whitelist* whitelist, struct config_file* cfg)
133 /* parse names, report errors, insert into tree */
134 struct config_strlist* p;
135 struct name_tree_node* n;
140 for(p = cfg->client_subnet_zone; p; p = p->next) {
142 nm = sldns_str2wire_dname(p->str, &nm_len);
144 log_err("cannot parse client-subnet-zone: %s", p->str);
147 nm_labs = dname_count_size_labels(nm, &nm_len);
148 nmr = (uint8_t*)regional_alloc_init(whitelist->region, nm,
152 log_err("out of memory");
155 n = (struct name_tree_node*)regional_alloc(whitelist->region,
158 log_err("out of memory");
161 if(!name_tree_insert(&whitelist->dname, n, nmr, nm_len, nm_labs,
163 verbose(VERB_QUERY, "ignoring duplicate "
164 "client-subnet-zone: %s", p->str);
171 ecs_whitelist_apply_cfg(struct ecs_whitelist* whitelist,
172 struct config_file* cfg)
174 regional_free_all(whitelist->region);
175 addr_tree_init(&whitelist->upstream);
176 name_tree_init(&whitelist->dname);
177 if(!read_upstream(whitelist, cfg))
179 if(!read_names(whitelist, cfg))
181 addr_tree_init_parents(&whitelist->upstream);
182 name_tree_init_parents(&whitelist->dname);
187 ecs_is_whitelisted(struct ecs_whitelist* whitelist,
188 struct sockaddr_storage* addr, socklen_t addrlen, uint8_t* qname,
189 size_t qname_len, uint16_t qclass)
192 if(addr_tree_lookup(&whitelist->upstream, addr, addrlen))
194 /* Not in upstream whitelist, check dname whitelist. */
195 labs = dname_count_labels(qname);
196 return name_tree_lookup(&whitelist->dname, qname, qname_len, labs,
201 ecs_whitelist_get_mem(struct ecs_whitelist* whitelist)
203 if(!whitelist) return 0;
204 return sizeof(*whitelist) + regional_get_mem(whitelist->region);
207 #endif /* CLIENT_SUBNET */