xref: /openbsd-src/usr.sbin/unbound/util/tcp_conn_limit.h (revision 7bc20e6db500d9f6533bfbedc9830b23d8985022)
1*7bc20e6dSsthen /*
2*7bc20e6dSsthen  * daemon/tcp_conn_limit.h - 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 keeps track of the limit on the number of TCP connections
40*7bc20e6dSsthen  * each client makes the server.
41*7bc20e6dSsthen  */
42*7bc20e6dSsthen 
43*7bc20e6dSsthen #ifndef DAEMON_TCP_CONN_LIMIT_H
44*7bc20e6dSsthen #define DAEMON_TCP_CONN_LIMIT_H
45*7bc20e6dSsthen #include "util/storage/dnstree.h"
46*7bc20e6dSsthen #include "util/locks.h"
47*7bc20e6dSsthen struct config_file;
48*7bc20e6dSsthen struct regional;
49*7bc20e6dSsthen 
50*7bc20e6dSsthen /**
51*7bc20e6dSsthen  * TCP connection limit storage structure
52*7bc20e6dSsthen  */
53*7bc20e6dSsthen struct tcl_list {
54*7bc20e6dSsthen 	/** regional for allocation */
55*7bc20e6dSsthen 	struct regional* region;
56*7bc20e6dSsthen 	/**
57*7bc20e6dSsthen 	 * Tree of the addresses that are TCP connection limited.
58*7bc20e6dSsthen 	 * contents of type tcl_addr.
59*7bc20e6dSsthen 	 */
60*7bc20e6dSsthen 	rbtree_type tree;
61*7bc20e6dSsthen };
62*7bc20e6dSsthen 
63*7bc20e6dSsthen /**
64*7bc20e6dSsthen  *
65*7bc20e6dSsthen  * An address span with connection limit information
66*7bc20e6dSsthen  */
67*7bc20e6dSsthen struct tcl_addr {
68*7bc20e6dSsthen 	/** node in address tree */
69*7bc20e6dSsthen 	struct addr_tree_node node;
70*7bc20e6dSsthen 	/** lock on structure data */
71*7bc20e6dSsthen 	lock_quick_type lock;
72*7bc20e6dSsthen 	/** connection limit on this netblock */
73*7bc20e6dSsthen 	uint32_t limit;
74*7bc20e6dSsthen 	/** current connection count on this netblock */
75*7bc20e6dSsthen 	uint32_t count;
76*7bc20e6dSsthen };
77*7bc20e6dSsthen 
78*7bc20e6dSsthen /**
79*7bc20e6dSsthen  * Create TCP connection limit structure
80*7bc20e6dSsthen  * @return new structure or NULL on error.
81*7bc20e6dSsthen  */
82*7bc20e6dSsthen struct tcl_list* tcl_list_create(void);
83*7bc20e6dSsthen 
84*7bc20e6dSsthen /**
85*7bc20e6dSsthen  * Delete TCP connection limit structure.
86*7bc20e6dSsthen  * @param tcl: to delete.
87*7bc20e6dSsthen  */
88*7bc20e6dSsthen void tcl_list_delete(struct tcl_list* tcl);
89*7bc20e6dSsthen 
90*7bc20e6dSsthen /**
91*7bc20e6dSsthen  * Process TCP connection limit config.
92*7bc20e6dSsthen  * @param tcl: where to store.
93*7bc20e6dSsthen  * @param cfg: config options.
94*7bc20e6dSsthen  * @return 0 on error.
95*7bc20e6dSsthen  */
96*7bc20e6dSsthen int tcl_list_apply_cfg(struct tcl_list* tcl, struct config_file* cfg);
97*7bc20e6dSsthen 
98*7bc20e6dSsthen /**
99*7bc20e6dSsthen  * Increment TCP connection count if found, provided the
100*7bc20e6dSsthen  * count was below the limit.
101*7bc20e6dSsthen  * @param tcl: structure for tcl storage, or NULL.
102*7bc20e6dSsthen  * @return: 0 if limit reached, 1 if tcl was NULL or limit not reached.
103*7bc20e6dSsthen  */
104*7bc20e6dSsthen int tcl_new_connection(struct tcl_addr* tcl);
105*7bc20e6dSsthen 
106*7bc20e6dSsthen /**
107*7bc20e6dSsthen  * Decrement TCP connection count if found.
108*7bc20e6dSsthen  * @param tcl: structure for tcl storage, or NULL.
109*7bc20e6dSsthen  */
110*7bc20e6dSsthen void tcl_close_connection(struct tcl_addr* tcl);
111*7bc20e6dSsthen 
112*7bc20e6dSsthen /**
113*7bc20e6dSsthen  * Lookup address to see its TCP connection limit structure
114*7bc20e6dSsthen  * @param tcl: structure for address storage.
115*7bc20e6dSsthen  * @param addr: address to check
116*7bc20e6dSsthen  * @param addrlen: length of addr.
117*7bc20e6dSsthen  * @return: tcl structure from this address.
118*7bc20e6dSsthen  */
119*7bc20e6dSsthen struct tcl_addr*
120*7bc20e6dSsthen tcl_addr_lookup(struct tcl_list* tcl, struct sockaddr_storage* addr,
121*7bc20e6dSsthen         socklen_t addrlen);
122*7bc20e6dSsthen 
123*7bc20e6dSsthen /**
124*7bc20e6dSsthen  * Get memory used by TCP connection limit structure.
125*7bc20e6dSsthen  * @param tcl: structure for address storage.
126*7bc20e6dSsthen  * @return bytes in use.
127*7bc20e6dSsthen  */
128*7bc20e6dSsthen size_t tcl_list_get_mem(struct tcl_list* tcl);
129*7bc20e6dSsthen 
130*7bc20e6dSsthen #endif /* DAEMON_TCP_CONN_LIMIT_H */
131