-
Notifications
You must be signed in to change notification settings - Fork 6
/
xt_sslpin_connstate.h
218 lines (175 loc) · 6.3 KB
/
xt_sslpin_connstate.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/*
* xt_sslpin_connstate.h
*
* Copyright (C) 2010-2013 fredburger (github.com/fredburger)
*
* This program is free software; you can redistribute it and/or modify it under the terms of the
* GNU General Public License as published by the Free Software Foundation; version 2 of the License.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with this program; if not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#ifndef _LINUX_NETFILTER_XT_SSLPIN_CONNSTATE_H
#define _LINUX_NETFILTER_XT_SSLPIN_CONNSTATE_H
#include "xt_sslpin_sslparser.h"
typedef enum {
SSLPIN_CONNSTATE_NEW,
SSLPIN_CONNSTATE_INVALID,
SSLPIN_CONNSTATE_GOT_SYNACK,
SSLPIN_CONNSTATE_GOT_DATA,
SSLPIN_CONNSTATE_CHECK_RULES,
SSLPIN_CONNSTATE_FINISHED
} sslpin_connstate_state_t;
struct sslpin_connstate {
struct rb_node node;
struct nf_conn *nfct;
sslpin_connstate_state_t state;
__u32 last_seq;
__u32 last_len;
__u16 last_ipid;
struct sslparser_ctx *parser_ctx;
};
/* per-connection state tracking table */
static struct rb_root sslpin_connstate_root = RB_ROOT;
static struct kmem_cache *sslpin_connstate_cache __read_mostly;
static struct kmem_cache *sslpin_parserctx_cache __read_mostly;
static __u32 sslpin_connstate_count = 0;
static __u32 sslpin_parserctx_count = 0;
/* print n.o. tracked connections */
static void sslpin_connstate_debug_count(void)
{
pr_info("xt_sslpin: %d connection%s (%d actively monitored)\n", sslpin_connstate_count,
(sslpin_connstate_count != 1) ? "s" : "",
sslpin_parserctx_count);
}
/* create parser_ctx for conn */
static struct sslparser_ctx * sslpin_connstate_bind_parser(struct sslpin_connstate *state, const bool enable_debug)
{
if (unlikely(state->parser_ctx)) {
return state->parser_ctx;
}
state->parser_ctx = kmem_cache_zalloc(sslpin_parserctx_cache, GFP_ATOMIC);
if (likely(state->parser_ctx)) {
state->parser_ctx->debug = enable_debug;
sslpin_parserctx_count++;
if (unlikely(sslpin_mt_has_debug_rules)) {
sslpin_connstate_debug_count();
}
}
return state->parser_ctx;
}
/* destroy parser_ctx for conn */
static void sslpin_connstate_unbind_parser(struct sslpin_connstate *state)
{
if (likely(state->parser_ctx)) {
kmem_cache_free(sslpin_parserctx_cache, state->parser_ctx);
state->parser_ctx = NULL;
sslpin_parserctx_count--;
if (unlikely(sslpin_mt_has_debug_rules)) {
sslpin_connstate_debug_count();
}
}
}
/* remove a conn from the tracking table */
static void sslpin_connstate_remove(struct sslpin_connstate *state)
{
sslpin_connstate_unbind_parser(state);
rb_erase(&state->node, &sslpin_connstate_root);
kmem_cache_free(sslpin_connstate_cache, state);
sslpin_connstate_count--;
}
/* init conn tracking table */
static bool sslpin_connstate_cache_init(void)
{
sslpin_connstate_cache = kmem_cache_create("xt_sslpin_connstate", sizeof(struct sslpin_connstate), 0, 0, NULL);
if (unlikely(!sslpin_connstate_cache)) {
return false;
}
sslpin_parserctx_cache = kmem_cache_create("xt_sslpin_parser", sizeof(struct sslparser_ctx), 0, 0, NULL);
if (unlikely(!sslpin_parserctx_cache)) {
kmem_cache_destroy(sslpin_connstate_cache);
return false;
}
return true;
}
/* destroy conn tracking table */
static void sslpin_connstate_cache_destroy(void)
{
struct rb_node *node;
struct sslpin_connstate *state;
if (unlikely(!sslpin_connstate_cache)) {
return;
}
node = rb_first(&sslpin_connstate_root);
while (node) {
state = rb_entry(node, struct sslpin_connstate, node);
node = rb_next(&state->node);
sslpin_connstate_remove(state);
}
if (likely(sslpin_parserctx_cache)) {
kmem_cache_destroy(sslpin_parserctx_cache);
}
kmem_cache_destroy(sslpin_connstate_cache);
}
/* find a conn in the tracking table, or return NULL */
static struct sslpin_connstate * sslpin_connstate_find(struct nf_conn *nfct,
struct sslpin_connstate **insertion_point_out)
{
struct rb_node *node = sslpin_connstate_root.rb_node;
struct sslpin_connstate *state = NULL;
struct nf_conn *node_nfct;
while (node) {
state = rb_entry(node, struct sslpin_connstate, node);
node_nfct = state->nfct;
if (nfct < node_nfct) {
node = node->rb_left;
} else if (nfct > node_nfct) {
node = node->rb_right;
} else {
return state;
}
}
if (likely(insertion_point_out)) {
*insertion_point_out = state;
}
return NULL;
}
/* find a conn in the tracking table, or add it */
static struct sslpin_connstate * sslpin_connstate_find_or_init(struct nf_conn *nfct)
{
struct sslpin_connstate *state;
struct sslpin_connstate *insertion_point;
struct rb_node *insertion_node;
struct rb_node **insertion_branch;
if (likely(state = sslpin_connstate_find(nfct, &insertion_point))) {
return state;
}
state = kmem_cache_zalloc(sslpin_connstate_cache, GFP_ATOMIC);
if (unlikely(!state)) {
return NULL;
}
state->nfct = nfct;
if (likely(insertion_point)) {
insertion_node = &insertion_point->node;
if (nfct < insertion_point->nfct) {
insertion_branch = &insertion_node->rb_left;
} else {
insertion_branch = &insertion_node->rb_right;
}
} else {
insertion_node = NULL;
insertion_branch = &sslpin_connstate_root.rb_node;
}
rb_link_node(&state->node, insertion_node, insertion_branch);
rb_insert_color(&state->node, &sslpin_connstate_root);
sslpin_connstate_count++;
if (unlikely(sslpin_mt_has_debug_rules)) {
sslpin_connstate_debug_count();
}
return state;
}
#endif /* _LINUX_NETFILTER_XT_SSLPIN_CONNSTATE_H */