npf_state.c revision 1.22 1 1.1 rmind /*-
2 1.8 rmind * Copyright (c) 2010-2012 The NetBSD Foundation, Inc.
3 1.1 rmind * All rights reserved.
4 1.1 rmind *
5 1.1 rmind * This material is based upon work partially supported by The
6 1.1 rmind * NetBSD Foundation under a contract with Mindaugas Rasiukevicius.
7 1.1 rmind *
8 1.1 rmind * Redistribution and use in source and binary forms, with or without
9 1.1 rmind * modification, are permitted provided that the following conditions
10 1.1 rmind * are met:
11 1.1 rmind * 1. Redistributions of source code must retain the above copyright
12 1.1 rmind * notice, this list of conditions and the following disclaimer.
13 1.1 rmind * 2. Redistributions in binary form must reproduce the above copyright
14 1.1 rmind * notice, this list of conditions and the following disclaimer in the
15 1.1 rmind * documentation and/or other materials provided with the distribution.
16 1.1 rmind *
17 1.1 rmind * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
18 1.1 rmind * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19 1.1 rmind * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 1.1 rmind * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
21 1.1 rmind * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 1.1 rmind * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 1.1 rmind * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 1.1 rmind * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 1.1 rmind * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 1.1 rmind * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 1.1 rmind * POSSIBILITY OF SUCH DAMAGE.
28 1.1 rmind */
29 1.1 rmind
30 1.1 rmind /*
31 1.16 rmind * NPF state engine to track connection.
32 1.1 rmind */
33 1.1 rmind
34 1.18 christos #ifdef _KERNEL
35 1.1 rmind #include <sys/cdefs.h>
36 1.22 rmind __KERNEL_RCSID(0, "$NetBSD: npf_state.c,v 1.22 2019/07/23 00:52:01 rmind Exp $");
37 1.1 rmind
38 1.1 rmind #include <sys/param.h>
39 1.1 rmind #include <sys/systm.h>
40 1.1 rmind #include <sys/mutex.h>
41 1.18 christos #endif
42 1.1 rmind
43 1.1 rmind #include "npf_impl.h"
44 1.1 rmind
45 1.6 rmind /*
46 1.16 rmind * Generic connection states and timeout table.
47 1.6 rmind *
48 1.13 rmind * Note: used for connection-less protocols.
49 1.6 rmind */
50 1.6 rmind
51 1.16 rmind #define NPF_ANY_CONN_CLOSED 0
52 1.16 rmind #define NPF_ANY_CONN_NEW 1
53 1.16 rmind #define NPF_ANY_CONN_ESTABLISHED 2
54 1.16 rmind #define NPF_ANY_CONN_NSTATES 3
55 1.16 rmind
56 1.22 rmind /*
57 1.22 rmind * Parameters.
58 1.22 rmind */
59 1.22 rmind typedef struct {
60 1.22 rmind int timeouts[NPF_ANY_CONN_NSTATES];
61 1.22 rmind } npf_state_params_t;
62 1.22 rmind
63 1.22 rmind /*
64 1.22 rmind * Generic FSM.
65 1.22 rmind */
66 1.16 rmind static const uint8_t npf_generic_fsm[NPF_ANY_CONN_NSTATES][2] = {
67 1.16 rmind [NPF_ANY_CONN_CLOSED] = {
68 1.16 rmind [NPF_FLOW_FORW] = NPF_ANY_CONN_NEW,
69 1.6 rmind },
70 1.16 rmind [NPF_ANY_CONN_NEW] = {
71 1.16 rmind [NPF_FLOW_FORW] = NPF_ANY_CONN_NEW,
72 1.16 rmind [NPF_FLOW_BACK] = NPF_ANY_CONN_ESTABLISHED,
73 1.6 rmind },
74 1.16 rmind [NPF_ANY_CONN_ESTABLISHED] = {
75 1.16 rmind [NPF_FLOW_FORW] = NPF_ANY_CONN_ESTABLISHED,
76 1.16 rmind [NPF_FLOW_BACK] = NPF_ANY_CONN_ESTABLISHED,
77 1.6 rmind },
78 1.3 rmind };
79 1.1 rmind
80 1.8 rmind /*
81 1.12 rmind * State sampler for debugging.
82 1.12 rmind */
83 1.21 christos #if defined(_NPF_TESTING)
84 1.12 rmind static void (*npf_state_sample)(npf_state_t *, bool) = NULL;
85 1.12 rmind #define NPF_STATE_SAMPLE(n, r) if (npf_state_sample) (*npf_state_sample)(n, r);
86 1.12 rmind #else
87 1.12 rmind #define NPF_STATE_SAMPLE(n, r)
88 1.12 rmind #endif
89 1.12 rmind
90 1.22 rmind void
91 1.22 rmind npf_state_sysinit(npf_t *npf)
92 1.22 rmind {
93 1.22 rmind npf_state_params_t *params = npf_param_allocgroup(npf,
94 1.22 rmind NPF_PARAMS_GENERIC_STATE, sizeof(npf_state_params_t));
95 1.22 rmind npf_param_t param_map[] = {
96 1.22 rmind /*
97 1.22 rmind * Generic timeout (in seconds).
98 1.22 rmind */
99 1.22 rmind {
100 1.22 rmind "state.generic.timeout.closed",
101 1.22 rmind ¶ms->timeouts[NPF_ANY_CONN_CLOSED],
102 1.22 rmind .default_val = 0,
103 1.22 rmind .min = 0, .max = INT_MAX
104 1.22 rmind },
105 1.22 rmind {
106 1.22 rmind "state.generic.timeout.new",
107 1.22 rmind ¶ms->timeouts[NPF_ANY_CONN_NEW],
108 1.22 rmind .default_val = 30,
109 1.22 rmind .min = 0, .max = INT_MAX
110 1.22 rmind },
111 1.22 rmind {
112 1.22 rmind "state.generic.timeout.established",
113 1.22 rmind ¶ms->timeouts[NPF_ANY_CONN_ESTABLISHED],
114 1.22 rmind .default_val = 60,
115 1.22 rmind .min = 0, .max = INT_MAX
116 1.22 rmind },
117 1.22 rmind };
118 1.22 rmind npf_param_register(npf, param_map, __arraycount(param_map));
119 1.22 rmind npf_state_tcp_sysinit(npf);
120 1.22 rmind }
121 1.22 rmind
122 1.22 rmind void
123 1.22 rmind npf_state_sysfini(npf_t *npf)
124 1.22 rmind {
125 1.22 rmind const size_t len = sizeof(npf_state_params_t);
126 1.22 rmind npf_param_freegroup(npf, NPF_PARAMS_GENERIC_STATE, len);
127 1.22 rmind npf_state_tcp_sysfini(npf);
128 1.22 rmind }
129 1.22 rmind
130 1.12 rmind /*
131 1.8 rmind * npf_state_init: initialise the state structure.
132 1.8 rmind *
133 1.8 rmind * Should normally be called on a first packet, which also determines the
134 1.8 rmind * direction in a case of connection-orientated protocol. Returns true on
135 1.8 rmind * success and false otherwise (e.g. if protocol is not supported).
136 1.8 rmind */
137 1.1 rmind bool
138 1.17 rmind npf_state_init(npf_cache_t *npc, npf_state_t *nst)
139 1.1 rmind {
140 1.14 rmind const int proto = npc->npc_proto;
141 1.6 rmind bool ret;
142 1.1 rmind
143 1.5 zoltan KASSERT(npf_iscached(npc, NPC_IP46));
144 1.5 zoltan KASSERT(npf_iscached(npc, NPC_LAYER4));
145 1.2 rmind
146 1.6 rmind memset(nst, 0, sizeof(npf_state_t));
147 1.2 rmind
148 1.6 rmind switch (proto) {
149 1.6 rmind case IPPROTO_TCP:
150 1.6 rmind /* Pass to TCP state tracking engine. */
151 1.17 rmind ret = npf_state_tcp(npc, nst, NPF_FLOW_FORW);
152 1.6 rmind break;
153 1.6 rmind case IPPROTO_UDP:
154 1.6 rmind case IPPROTO_ICMP:
155 1.6 rmind /* Generic. */
156 1.6 rmind nst->nst_state = npf_generic_fsm[nst->nst_state][NPF_FLOW_FORW];
157 1.6 rmind ret = true;
158 1.6 rmind break;
159 1.6 rmind default:
160 1.6 rmind ret = false;
161 1.1 rmind }
162 1.11 rmind NPF_STATE_SAMPLE(nst, ret);
163 1.6 rmind return ret;
164 1.1 rmind }
165 1.1 rmind
166 1.1 rmind void
167 1.1 rmind npf_state_destroy(npf_state_t *nst)
168 1.1 rmind {
169 1.6 rmind nst->nst_state = 0;
170 1.1 rmind }
171 1.1 rmind
172 1.8 rmind /*
173 1.8 rmind * npf_state_inspect: inspect the packet according to the protocol state.
174 1.8 rmind *
175 1.8 rmind * Return true if packet is considered to match the state (e.g. for TCP,
176 1.8 rmind * the packet belongs to the tracked connection) and false otherwise.
177 1.8 rmind */
178 1.1 rmind bool
179 1.17 rmind npf_state_inspect(npf_cache_t *npc, npf_state_t *nst, const bool forw)
180 1.1 rmind {
181 1.14 rmind const int proto = npc->npc_proto;
182 1.6 rmind const int di = forw ? NPF_FLOW_FORW : NPF_FLOW_BACK;
183 1.1 rmind bool ret;
184 1.1 rmind
185 1.1 rmind switch (proto) {
186 1.1 rmind case IPPROTO_TCP:
187 1.6 rmind /* Pass to TCP state tracking engine. */
188 1.17 rmind ret = npf_state_tcp(npc, nst, di);
189 1.6 rmind break;
190 1.6 rmind case IPPROTO_UDP:
191 1.6 rmind case IPPROTO_ICMP:
192 1.6 rmind /* Generic. */
193 1.6 rmind nst->nst_state = npf_generic_fsm[nst->nst_state][di];
194 1.6 rmind ret = true;
195 1.1 rmind break;
196 1.1 rmind default:
197 1.6 rmind ret = false;
198 1.1 rmind }
199 1.11 rmind NPF_STATE_SAMPLE(nst, ret);
200 1.6 rmind
201 1.1 rmind return ret;
202 1.1 rmind }
203 1.1 rmind
204 1.3 rmind /*
205 1.22 rmind * npf_state_etime: return the expiration time depending on the state.
206 1.3 rmind */
207 1.1 rmind int
208 1.22 rmind npf_state_etime(npf_t *npf, const npf_state_t *nst, const int proto)
209 1.1 rmind {
210 1.22 rmind const npf_state_params_t *params;
211 1.22 rmind const unsigned state = nst->nst_state;
212 1.6 rmind int timeout = 0;
213 1.1 rmind
214 1.6 rmind switch (proto) {
215 1.6 rmind case IPPROTO_TCP:
216 1.6 rmind /* Pass to TCP state tracking engine. */
217 1.22 rmind timeout = npf_state_tcp_timeout(npf, nst);
218 1.6 rmind break;
219 1.6 rmind case IPPROTO_UDP:
220 1.6 rmind case IPPROTO_ICMP:
221 1.6 rmind /* Generic. */
222 1.22 rmind params = npf->params[NPF_PARAMS_GENERIC_STATE];
223 1.22 rmind timeout = params->timeouts[state];
224 1.6 rmind break;
225 1.6 rmind default:
226 1.6 rmind KASSERT(false);
227 1.1 rmind }
228 1.6 rmind return timeout;
229 1.1 rmind }
230 1.1 rmind
231 1.1 rmind void
232 1.9 rmind npf_state_dump(const npf_state_t *nst)
233 1.1 rmind {
234 1.4 yamt #if defined(DDB) || defined(_NPF_TESTING)
235 1.9 rmind const npf_tcpstate_t *fst = &nst->nst_tcpst[0];
236 1.9 rmind const npf_tcpstate_t *tst = &nst->nst_tcpst[1];
237 1.1 rmind
238 1.1 rmind printf("\tstate (%p) %d:\n\t\t"
239 1.6 rmind "F { end %u maxend %u mwin %u wscale %u }\n\t\t"
240 1.6 rmind "T { end %u maxend %u mwin %u wscale %u }\n",
241 1.1 rmind nst, nst->nst_state,
242 1.6 rmind fst->nst_end, fst->nst_maxend, fst->nst_maxwin, fst->nst_wscale,
243 1.6 rmind tst->nst_end, tst->nst_maxend, tst->nst_maxwin, tst->nst_wscale
244 1.1 rmind );
245 1.4 yamt #endif
246 1.1 rmind }
247 1.12 rmind
248 1.21 christos #if defined(_NPF_TESTING)
249 1.12 rmind void
250 1.12 rmind npf_state_setsampler(void (*func)(npf_state_t *, bool))
251 1.12 rmind {
252 1.12 rmind npf_state_sample = func;
253 1.12 rmind }
254 1.12 rmind #endif
255