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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
|
/*-
* Copyright (c) 2010-2012 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This material is based upon work partially supported by The
* NetBSD Foundation under a contract with Mindaugas Rasiukevicius.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
/*
* NPF state engine to track connection.
*/
#ifdef _KERNEL
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: npf_state.c,v 1.23 2020/05/30 14:16:56 rmind Exp $");
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/mutex.h>
#endif
#include "npf_impl.h"
/*
* Generic connection states and timeout table.
*
* Note: used for connection-less protocols.
*/
#define NPF_ANY_CONN_CLOSED 0
#define NPF_ANY_CONN_NEW 1
#define NPF_ANY_CONN_ESTABLISHED 2
#define NPF_ANY_CONN_NSTATES 3
/*
* Parameters.
*/
typedef struct {
int timeouts[NPF_ANY_CONN_NSTATES];
int gre_timeout;
} npf_state_params_t;
/*
* Generic FSM.
*/
static const uint8_t npf_generic_fsm[NPF_ANY_CONN_NSTATES][2] = {
[NPF_ANY_CONN_CLOSED] = {
[NPF_FLOW_FORW] = NPF_ANY_CONN_NEW,
},
[NPF_ANY_CONN_NEW] = {
[NPF_FLOW_FORW] = NPF_ANY_CONN_NEW,
[NPF_FLOW_BACK] = NPF_ANY_CONN_ESTABLISHED,
},
[NPF_ANY_CONN_ESTABLISHED] = {
[NPF_FLOW_FORW] = NPF_ANY_CONN_ESTABLISHED,
[NPF_FLOW_BACK] = NPF_ANY_CONN_ESTABLISHED,
},
};
/*
* State sampler for debugging.
*/
#if defined(_NPF_TESTING)
static void (*npf_state_sample)(npf_state_t *, bool) = NULL;
#define NPF_STATE_SAMPLE(n, r) if (npf_state_sample) (*npf_state_sample)(n, r);
#else
#define NPF_STATE_SAMPLE(n, r)
#endif
void
npf_state_sysinit(npf_t *npf)
{
npf_state_params_t *params = npf_param_allocgroup(npf,
NPF_PARAMS_GENERIC_STATE, sizeof(npf_state_params_t));
npf_param_t param_map[] = {
/*
* Generic timeout (in seconds).
*/
{
"state.generic.timeout.closed",
¶ms->timeouts[NPF_ANY_CONN_CLOSED],
.default_val = 0,
.min = 0, .max = INT_MAX
},
{
"state.generic.timeout.new",
¶ms->timeouts[NPF_ANY_CONN_NEW],
.default_val = 30,
.min = 0, .max = INT_MAX
},
{
"state.generic.timeout.established",
¶ms->timeouts[NPF_ANY_CONN_ESTABLISHED],
.default_val = 60,
.min = 0, .max = INT_MAX
},
{
"state.generic.timeout.gre",
¶ms->gre_timeout,
.default_val = 24 * 60 * 60,
.min = 0, .max = INT_MAX
},
};
npf_param_register(npf, param_map, __arraycount(param_map));
npf_state_tcp_sysinit(npf);
}
void
npf_state_sysfini(npf_t *npf)
{
const size_t len = sizeof(npf_state_params_t);
npf_param_freegroup(npf, NPF_PARAMS_GENERIC_STATE, len);
npf_state_tcp_sysfini(npf);
}
/*
* npf_state_init: initialise the state structure.
*
* Should normally be called on a first packet, which also determines the
* direction in a case of connection-orientated protocol. Returns true on
* success and false otherwise (e.g. if protocol is not supported).
*/
bool
npf_state_init(npf_cache_t *npc, npf_state_t *nst)
{
const int proto = npc->npc_proto;
bool ret;
KASSERT(npf_iscached(npc, NPC_IP46));
KASSERT(npf_iscached(npc, NPC_LAYER4));
memset(nst, 0, sizeof(npf_state_t));
switch (proto) {
case IPPROTO_TCP:
/* Pass to TCP state tracking engine. */
ret = npf_state_tcp(npc, nst, NPF_FLOW_FORW);
break;
case IPPROTO_UDP:
case IPPROTO_ICMP:
case IPPROTO_GRE:
/* Generic. */
nst->nst_state = npf_generic_fsm[nst->nst_state][NPF_FLOW_FORW];
ret = true;
break;
default:
ret = false;
}
NPF_STATE_SAMPLE(nst, ret);
return ret;
}
void
npf_state_destroy(npf_state_t *nst)
{
nst->nst_state = 0;
}
/*
* npf_state_inspect: inspect the packet according to the protocol state.
*
* Return true if packet is considered to match the state (e.g. for TCP,
* the packet belongs to the tracked connection) and false otherwise.
*/
bool
npf_state_inspect(npf_cache_t *npc, npf_state_t *nst, const npf_flow_t flow)
{
const int proto = npc->npc_proto;
bool ret;
switch (proto) {
case IPPROTO_TCP:
/* Pass to TCP state tracking engine. */
ret = npf_state_tcp(npc, nst, flow);
break;
case IPPROTO_UDP:
case IPPROTO_ICMP:
case IPPROTO_GRE:
/* Generic. */
nst->nst_state = npf_generic_fsm[nst->nst_state][flow];
ret = true;
break;
default:
ret = false;
}
NPF_STATE_SAMPLE(nst, ret);
return ret;
}
/*
* npf_state_etime: return the expiration time depending on the state.
*/
int
npf_state_etime(npf_t *npf, const npf_state_t *nst, const int proto)
{
const npf_state_params_t *params;
const unsigned state = nst->nst_state;
int timeout = 0;
switch (proto) {
case IPPROTO_TCP:
/* Pass to TCP state tracking engine. */
timeout = npf_state_tcp_timeout(npf, nst);
break;
case IPPROTO_UDP:
case IPPROTO_ICMP:
/* Generic. */
params = npf->params[NPF_PARAMS_GENERIC_STATE];
timeout = params->timeouts[state];
break;
case IPPROTO_GRE:
params = npf->params[NPF_PARAMS_GENERIC_STATE];
timeout = params->gre_timeout;
break;
default:
KASSERT(false);
}
return timeout;
}
void
npf_state_dump(const npf_state_t *nst)
{
#if defined(DDB) || defined(_NPF_TESTING)
const npf_tcpstate_t *fst = &nst->nst_tcpst[0];
const npf_tcpstate_t *tst = &nst->nst_tcpst[1];
printf("\tstate (%p) %d:\n\t\t"
"F { end %u maxend %u mwin %u wscale %u }\n\t\t"
"T { end %u maxend %u mwin %u wscale %u }\n",
nst, nst->nst_state,
fst->nst_end, fst->nst_maxend, fst->nst_maxwin, fst->nst_wscale,
tst->nst_end, tst->nst_maxend, tst->nst_maxwin, tst->nst_wscale
);
#endif
}
#if defined(_NPF_TESTING)
void
npf_state_setsampler(void (*func)(npf_state_t *, bool))
{
npf_state_sample = func;
}
#endif
|