xref: /freebsd/contrib/unbound/edns-subnet/subnet-whitelist.c (revision 5ca8e32633c4ffbbcd6762e5888b6a4ba0708c6c)
1 /*
2  * edns-subnet/subnet-whitelist.c - Hosts we actively try to send subnet option
3  * to.
4  *
5  * Copyright (c) 2013, NLnet Labs. All rights reserved.
6  *
7  * This software is open source.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * Redistributions of source code must retain the above copyright notice,
14  * this list of conditions and the following disclaimer.
15  *
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.
19  *
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.
23  *
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.
35  */
36 /**
37  * \file
38  *
39  * Keep track of the white listed servers for subnet option. Based
40  * on acl_list.c|h
41  */
42 
43 #include "config.h"
44 
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"
49 #include "util/log.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"
55 
56 struct ecs_whitelist*
57 ecs_whitelist_create(void)
58 {
59 	struct ecs_whitelist* whitelist =
60 		(struct ecs_whitelist*)calloc(1,
61 		sizeof(struct ecs_whitelist));
62 	if(!whitelist)
63 		return NULL;
64 	whitelist->region = regional_create();
65 	if(!whitelist->region) {
66 		ecs_whitelist_delete(whitelist);
67 		return NULL;
68 	}
69 	return whitelist;
70 }
71 
72 void
73 ecs_whitelist_delete(struct ecs_whitelist* whitelist)
74 {
75 	if(!whitelist)
76 		return;
77 	regional_destroy(whitelist->region);
78 	free(whitelist);
79 }
80 
81 /** insert new address into whitelist structure */
82 static int
83 upstream_insert(struct ecs_whitelist* whitelist,
84 	struct sockaddr_storage* addr, socklen_t addrlen, int net)
85 {
86 	struct addr_tree_node* node = (struct addr_tree_node*)regional_alloc(
87 		whitelist->region, sizeof(*node));
88 	if(!node)
89 		return 0;
90 	if(!addr_tree_insert(&whitelist->upstream, node, addr, addrlen, net)) {
91 		verbose(VERB_QUERY,
92 			"duplicate send-client-subnet address ignored.");
93 	}
94 	return 1;
95 }
96 
97 /** apply edns-subnet string */
98 static int
99 upstream_str_cfg(struct ecs_whitelist* whitelist, const char* str)
100 {
101 	struct sockaddr_storage addr;
102 	int net;
103 	socklen_t addrlen;
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);
107 		return 0;
108 	}
109 	if(!upstream_insert(whitelist, &addr, addrlen, net)) {
110 		log_err("out of memory");
111 		return 0;
112 	}
113 	return 1;
114 }
115 
116 /** read client_subnet config */
117 static int
118 read_upstream(struct ecs_whitelist* whitelist, struct config_file* cfg)
119 {
120 	struct config_strlist* p;
121 	for(p = cfg->client_subnet; p; p = p->next) {
122 		log_assert(p->str);
123 		if(!upstream_str_cfg(whitelist, p->str))
124 			return 0;
125 	}
126 	return 1;
127 }
128 
129 /** read client_subnet_zone config */
130 static int
131 read_names(struct ecs_whitelist* whitelist, struct config_file* cfg)
132 {
133 	/* parse names, report errors, insert into tree */
134 	struct config_strlist* p;
135 	struct name_tree_node* n;
136 	uint8_t* nm, *nmr;
137 	size_t nm_len;
138 	int nm_labs;
139 
140 	for(p = cfg->client_subnet_zone; p; p = p->next) {
141 		log_assert(p->str);
142 		nm = sldns_str2wire_dname(p->str, &nm_len);
143 		if(!nm) {
144 			log_err("cannot parse client-subnet-zone: %s", p->str);
145 			return 0;
146 		}
147 		nm_labs = dname_count_size_labels(nm, &nm_len);
148 		nmr = (uint8_t*)regional_alloc_init(whitelist->region, nm,
149 			nm_len);
150 		free(nm);
151 		if(!nmr) {
152 			log_err("out of memory");
153 			return 0;
154 		}
155 		n = (struct name_tree_node*)regional_alloc(whitelist->region,
156 			sizeof(*n));
157 		if(!n) {
158 			log_err("out of memory");
159 			return 0;
160 		}
161 		if(!name_tree_insert(&whitelist->dname, n, nmr, nm_len, nm_labs,
162 			LDNS_RR_CLASS_IN)) {
163 			verbose(VERB_QUERY, "ignoring duplicate "
164 				"client-subnet-zone: %s", p->str);
165 		}
166 	}
167 	return 1;
168 }
169 
170 int
171 ecs_whitelist_apply_cfg(struct ecs_whitelist* whitelist,
172 	struct config_file* cfg)
173 {
174 	regional_free_all(whitelist->region);
175 	addr_tree_init(&whitelist->upstream);
176 	name_tree_init(&whitelist->dname);
177 	if(!read_upstream(whitelist, cfg))
178 		return 0;
179 	if(!read_names(whitelist, cfg))
180 		return 0;
181 	addr_tree_init_parents(&whitelist->upstream);
182 	name_tree_init_parents(&whitelist->dname);
183 	return 1;
184 }
185 
186 int
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)
190 {
191 	int labs;
192 	if(addr_tree_lookup(&whitelist->upstream, addr, addrlen))
193 		return 1;
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,
197 		qclass) != NULL;
198 }
199 
200 size_t
201 ecs_whitelist_get_mem(struct ecs_whitelist* whitelist)
202 {
203 	if(!whitelist) return 0;
204 	return sizeof(*whitelist) + regional_get_mem(whitelist->region);
205 }
206 
207 #endif /* CLIENT_SUBNET */
208