xref: /openbsd-src/usr.sbin/unbound/util/tcp_conn_limit.c (revision 7bc20e6db500d9f6533bfbedc9830b23d8985022)
1*7bc20e6dSsthen /*
2*7bc20e6dSsthen  * daemon/tcp_conn_limit.c - client TCP connection limit storage for the server.
3*7bc20e6dSsthen  *
4*7bc20e6dSsthen  * Copyright (c) 2018, NLnet Labs. All rights reserved.
5*7bc20e6dSsthen  *
6*7bc20e6dSsthen  * This software is open source.
7*7bc20e6dSsthen  *
8*7bc20e6dSsthen  * Redistribution and use in source and binary forms, with or without
9*7bc20e6dSsthen  * modification, are permitted provided that the following conditions
10*7bc20e6dSsthen  * are met:
11*7bc20e6dSsthen  *
12*7bc20e6dSsthen  * Redistributions of source code must retain the above copyright notice,
13*7bc20e6dSsthen  * this list of conditions and the following disclaimer.
14*7bc20e6dSsthen  *
15*7bc20e6dSsthen  * Redistributions in binary form must reproduce the above copyright notice,
16*7bc20e6dSsthen  * this list of conditions and the following disclaimer in the documentation
17*7bc20e6dSsthen  * and/or other materials provided with the distribution.
18*7bc20e6dSsthen  *
19*7bc20e6dSsthen  * Neither the name of the NLNET LABS nor the names of its contributors may
20*7bc20e6dSsthen  * be used to endorse or promote products derived from this software without
21*7bc20e6dSsthen  * specific prior written permission.
22*7bc20e6dSsthen  *
23*7bc20e6dSsthen  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24*7bc20e6dSsthen  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25*7bc20e6dSsthen  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26*7bc20e6dSsthen  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27*7bc20e6dSsthen  * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28*7bc20e6dSsthen  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
29*7bc20e6dSsthen  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30*7bc20e6dSsthen  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31*7bc20e6dSsthen  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32*7bc20e6dSsthen  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33*7bc20e6dSsthen  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34*7bc20e6dSsthen  */
35*7bc20e6dSsthen 
36*7bc20e6dSsthen /**
37*7bc20e6dSsthen  * \file
38*7bc20e6dSsthen  *
39*7bc20e6dSsthen  * This file helps the server discard excess TCP connections.
40*7bc20e6dSsthen  */
41*7bc20e6dSsthen #include "config.h"
42*7bc20e6dSsthen #include "util/regional.h"
43*7bc20e6dSsthen #include "util/log.h"
44*7bc20e6dSsthen #include "util/config_file.h"
45*7bc20e6dSsthen #include "util/net_help.h"
46*7bc20e6dSsthen #include "util/tcp_conn_limit.h"
47*7bc20e6dSsthen #include "services/localzone.h"
48*7bc20e6dSsthen #include "sldns/str2wire.h"
49*7bc20e6dSsthen 
50*7bc20e6dSsthen struct tcl_list*
tcl_list_create(void)51*7bc20e6dSsthen tcl_list_create(void)
52*7bc20e6dSsthen {
53*7bc20e6dSsthen 	struct tcl_list* tcl = (struct tcl_list*)calloc(1,
54*7bc20e6dSsthen 		sizeof(struct tcl_list));
55*7bc20e6dSsthen 	if(!tcl)
56*7bc20e6dSsthen 		return NULL;
57*7bc20e6dSsthen 	tcl->region = regional_create();
58*7bc20e6dSsthen 	if(!tcl->region) {
59*7bc20e6dSsthen 		tcl_list_delete(tcl);
60*7bc20e6dSsthen 		return NULL;
61*7bc20e6dSsthen 	}
62*7bc20e6dSsthen 	return tcl;
63*7bc20e6dSsthen }
64*7bc20e6dSsthen 
65*7bc20e6dSsthen static void
tcl_list_free_node(rbnode_type * node,void * ATTR_UNUSED (arg))66*7bc20e6dSsthen tcl_list_free_node(rbnode_type* node, void* ATTR_UNUSED(arg))
67*7bc20e6dSsthen {
68*7bc20e6dSsthen 	struct tcl_addr* n = (struct tcl_addr*) node;
69*7bc20e6dSsthen 	lock_quick_destroy(&n->lock);
70*7bc20e6dSsthen #ifdef THREADS_DISABLED
71*7bc20e6dSsthen 	(void)n;
72*7bc20e6dSsthen #endif
73*7bc20e6dSsthen }
74*7bc20e6dSsthen 
75*7bc20e6dSsthen void
tcl_list_delete(struct tcl_list * tcl)76*7bc20e6dSsthen tcl_list_delete(struct tcl_list* tcl)
77*7bc20e6dSsthen {
78*7bc20e6dSsthen 	if(!tcl)
79*7bc20e6dSsthen 		return;
80*7bc20e6dSsthen 	traverse_postorder(&tcl->tree, tcl_list_free_node, NULL);
81*7bc20e6dSsthen 	regional_destroy(tcl->region);
82*7bc20e6dSsthen 	free(tcl);
83*7bc20e6dSsthen }
84*7bc20e6dSsthen 
85*7bc20e6dSsthen /** insert new address into tcl_list structure */
86*7bc20e6dSsthen static struct tcl_addr*
tcl_list_insert(struct tcl_list * tcl,struct sockaddr_storage * addr,socklen_t addrlen,int net,uint32_t limit,int complain_duplicates)87*7bc20e6dSsthen tcl_list_insert(struct tcl_list* tcl, struct sockaddr_storage* addr,
88*7bc20e6dSsthen 	socklen_t addrlen, int net, uint32_t limit,
89*7bc20e6dSsthen 	int complain_duplicates)
90*7bc20e6dSsthen {
91*7bc20e6dSsthen 	struct tcl_addr* node = regional_alloc_zero(tcl->region,
92*7bc20e6dSsthen 		sizeof(struct tcl_addr));
93*7bc20e6dSsthen 	if(!node)
94*7bc20e6dSsthen 		return NULL;
95*7bc20e6dSsthen 	lock_quick_init(&node->lock);
96*7bc20e6dSsthen 	node->limit = limit;
97*7bc20e6dSsthen 	if(!addr_tree_insert(&tcl->tree, &node->node, addr, addrlen, net)) {
98*7bc20e6dSsthen 		if(complain_duplicates)
99*7bc20e6dSsthen 			verbose(VERB_QUERY, "duplicate tcl address ignored.");
100*7bc20e6dSsthen 	}
101*7bc20e6dSsthen 	return node;
102*7bc20e6dSsthen }
103*7bc20e6dSsthen 
104*7bc20e6dSsthen /** apply tcl_list string */
105*7bc20e6dSsthen static int
tcl_list_str_cfg(struct tcl_list * tcl,const char * str,const char * s2,int complain_duplicates)106*7bc20e6dSsthen tcl_list_str_cfg(struct tcl_list* tcl, const char* str, const char* s2,
107*7bc20e6dSsthen 	int complain_duplicates)
108*7bc20e6dSsthen {
109*7bc20e6dSsthen 	struct sockaddr_storage addr;
110*7bc20e6dSsthen 	int net;
111*7bc20e6dSsthen 	socklen_t addrlen;
112*7bc20e6dSsthen 	uint32_t limit;
113*7bc20e6dSsthen 	if(atoi(s2) < 0) {
114*7bc20e6dSsthen 		log_err("bad connection limit %s", s2);
115*7bc20e6dSsthen 		return 0;
116*7bc20e6dSsthen 	}
117*7bc20e6dSsthen 	limit = (uint32_t)atoi(s2);
118*7bc20e6dSsthen 	if(!netblockstrtoaddr(str, UNBOUND_DNS_PORT, &addr, &addrlen, &net)) {
119*7bc20e6dSsthen 		log_err("cannot parse connection limit netblock: %s", str);
120*7bc20e6dSsthen 		return 0;
121*7bc20e6dSsthen 	}
122*7bc20e6dSsthen 	if(!tcl_list_insert(tcl, &addr, addrlen, net, limit,
123*7bc20e6dSsthen 		complain_duplicates)) {
124*7bc20e6dSsthen 		log_err("out of memory");
125*7bc20e6dSsthen 		return 0;
126*7bc20e6dSsthen 	}
127*7bc20e6dSsthen 	return 1;
128*7bc20e6dSsthen }
129*7bc20e6dSsthen 
130*7bc20e6dSsthen /** read tcl_list config */
131*7bc20e6dSsthen static int
read_tcl_list(struct tcl_list * tcl,struct config_file * cfg)132*7bc20e6dSsthen read_tcl_list(struct tcl_list* tcl, struct config_file* cfg)
133*7bc20e6dSsthen {
134*7bc20e6dSsthen 	struct config_str2list* p;
135*7bc20e6dSsthen 	for(p = cfg->tcp_connection_limits; p; p = p->next) {
136*7bc20e6dSsthen 		log_assert(p->str && p->str2);
137*7bc20e6dSsthen 		if(!tcl_list_str_cfg(tcl, p->str, p->str2, 1))
138*7bc20e6dSsthen 			return 0;
139*7bc20e6dSsthen 	}
140*7bc20e6dSsthen 	return 1;
141*7bc20e6dSsthen }
142*7bc20e6dSsthen 
143*7bc20e6dSsthen int
tcl_list_apply_cfg(struct tcl_list * tcl,struct config_file * cfg)144*7bc20e6dSsthen tcl_list_apply_cfg(struct tcl_list* tcl, struct config_file* cfg)
145*7bc20e6dSsthen {
146*7bc20e6dSsthen 	regional_free_all(tcl->region);
147*7bc20e6dSsthen 	addr_tree_init(&tcl->tree);
148*7bc20e6dSsthen 	if(!read_tcl_list(tcl, cfg))
149*7bc20e6dSsthen 		return 0;
150*7bc20e6dSsthen 	addr_tree_init_parents(&tcl->tree);
151*7bc20e6dSsthen 	return 1;
152*7bc20e6dSsthen }
153*7bc20e6dSsthen 
154*7bc20e6dSsthen int
tcl_new_connection(struct tcl_addr * tcl)155*7bc20e6dSsthen tcl_new_connection(struct tcl_addr* tcl)
156*7bc20e6dSsthen {
157*7bc20e6dSsthen 	if(tcl) {
158*7bc20e6dSsthen 		int res = 1;
159*7bc20e6dSsthen 		lock_quick_lock(&tcl->lock);
160*7bc20e6dSsthen 		if(tcl->count >= tcl->limit)
161*7bc20e6dSsthen 			res = 0;
162*7bc20e6dSsthen 		else
163*7bc20e6dSsthen 			tcl->count++;
164*7bc20e6dSsthen 		lock_quick_unlock(&tcl->lock);
165*7bc20e6dSsthen 		return res;
166*7bc20e6dSsthen 	}
167*7bc20e6dSsthen 	return 1;
168*7bc20e6dSsthen }
169*7bc20e6dSsthen 
170*7bc20e6dSsthen void
tcl_close_connection(struct tcl_addr * tcl)171*7bc20e6dSsthen tcl_close_connection(struct tcl_addr* tcl)
172*7bc20e6dSsthen {
173*7bc20e6dSsthen 	if(tcl) {
174*7bc20e6dSsthen 		lock_quick_lock(&tcl->lock);
175*7bc20e6dSsthen 		log_assert(tcl->count > 0);
176*7bc20e6dSsthen 		tcl->count--;
177*7bc20e6dSsthen 		lock_quick_unlock(&tcl->lock);
178*7bc20e6dSsthen 	}
179*7bc20e6dSsthen }
180*7bc20e6dSsthen 
181*7bc20e6dSsthen struct tcl_addr*
tcl_addr_lookup(struct tcl_list * tcl,struct sockaddr_storage * addr,socklen_t addrlen)182*7bc20e6dSsthen tcl_addr_lookup(struct tcl_list* tcl, struct sockaddr_storage* addr,
183*7bc20e6dSsthen         socklen_t addrlen)
184*7bc20e6dSsthen {
185*7bc20e6dSsthen 	return (struct tcl_addr*)addr_tree_lookup(&tcl->tree,
186*7bc20e6dSsthen 		addr, addrlen);
187*7bc20e6dSsthen }
188*7bc20e6dSsthen 
189*7bc20e6dSsthen size_t
tcl_list_get_mem(struct tcl_list * tcl)190*7bc20e6dSsthen tcl_list_get_mem(struct tcl_list* tcl)
191*7bc20e6dSsthen {
192*7bc20e6dSsthen 	if(!tcl) return 0;
193*7bc20e6dSsthen 	return sizeof(*tcl) + regional_get_mem(tcl->region);
194*7bc20e6dSsthen }
195