2 * daemon/tcp_conn_limit.c - client TCP connection limit storage for the server.
4 * Copyright (c) 2018, 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
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.
39 * This file helps the server discard excess TCP connections.
42 #include "util/regional.h"
44 #include "util/config_file.h"
45 #include "util/net_help.h"
46 #include "util/tcp_conn_limit.h"
47 #include "services/localzone.h"
48 #include "sldns/str2wire.h"
53 struct tcl_list* tcl = (struct tcl_list*)calloc(1,
54 sizeof(struct tcl_list));
57 tcl->region = regional_create();
66 tcl_list_free_node(rbnode_type* node, void* ATTR_UNUSED(arg))
68 struct tcl_addr* n = (struct tcl_addr*) node;
69 lock_quick_destroy(&n->lock);
70 #ifdef THREADS_DISABLED
76 tcl_list_delete(struct tcl_list* tcl)
80 traverse_postorder(&tcl->tree, tcl_list_free_node, NULL);
81 regional_destroy(tcl->region);
85 /** insert new address into tcl_list structure */
86 static struct tcl_addr*
87 tcl_list_insert(struct tcl_list* tcl, struct sockaddr_storage* addr,
88 socklen_t addrlen, int net, uint32_t limit,
89 int complain_duplicates)
91 struct tcl_addr* node = regional_alloc_zero(tcl->region,
92 sizeof(struct tcl_addr));
95 lock_quick_init(&node->lock);
97 if(!addr_tree_insert(&tcl->tree, &node->node, addr, addrlen, net)) {
98 if(complain_duplicates)
99 verbose(VERB_QUERY, "duplicate tcl address ignored.");
104 /** apply tcl_list string */
106 tcl_list_str_cfg(struct tcl_list* tcl, const char* str, const char* s2,
107 int complain_duplicates)
109 struct sockaddr_storage addr;
114 log_err("bad connection limit %s", s2);
117 limit = (uint32_t)atoi(s2);
118 if(!netblockstrtoaddr(str, UNBOUND_DNS_PORT, &addr, &addrlen, &net)) {
119 log_err("cannot parse connection limit netblock: %s", str);
122 if(!tcl_list_insert(tcl, &addr, addrlen, net, limit,
123 complain_duplicates)) {
124 log_err("out of memory");
130 /** read tcl_list config */
132 read_tcl_list(struct tcl_list* tcl, struct config_file* cfg)
134 struct config_str2list* p;
135 for(p = cfg->tcp_connection_limits; p; p = p->next) {
136 log_assert(p->str && p->str2);
137 if(!tcl_list_str_cfg(tcl, p->str, p->str2, 1))
144 tcl_list_apply_cfg(struct tcl_list* tcl, struct config_file* cfg)
146 regional_free_all(tcl->region);
147 addr_tree_init(&tcl->tree);
148 if(!read_tcl_list(tcl, cfg))
150 addr_tree_init_parents(&tcl->tree);
155 tcl_new_connection(struct tcl_addr* tcl)
159 lock_quick_lock(&tcl->lock);
160 if(tcl->count >= tcl->limit)
164 lock_quick_unlock(&tcl->lock);
171 tcl_close_connection(struct tcl_addr* tcl)
174 lock_quick_lock(&tcl->lock);
175 log_assert(tcl->count > 0);
177 lock_quick_unlock(&tcl->lock);
182 tcl_addr_lookup(struct tcl_list* tcl, struct sockaddr_storage* addr,
185 return (struct tcl_addr*)addr_tree_lookup(&tcl->tree,
190 tcl_list_get_mem(struct tcl_list* tcl)
193 return sizeof(*tcl) + regional_get_mem(tcl->region);