npf_state.c revision 1.19 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.19 rmind __KERNEL_RCSID(0, "$NetBSD: npf_state.c,v 1.19 2018/09/29 14:41:36 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.16 rmind static const uint8_t npf_generic_fsm[NPF_ANY_CONN_NSTATES][2] = {
57 1.16 rmind [NPF_ANY_CONN_CLOSED] = {
58 1.16 rmind [NPF_FLOW_FORW] = NPF_ANY_CONN_NEW,
59 1.6 rmind },
60 1.16 rmind [NPF_ANY_CONN_NEW] = {
61 1.16 rmind [NPF_FLOW_FORW] = NPF_ANY_CONN_NEW,
62 1.16 rmind [NPF_FLOW_BACK] = NPF_ANY_CONN_ESTABLISHED,
63 1.6 rmind },
64 1.16 rmind [NPF_ANY_CONN_ESTABLISHED] = {
65 1.16 rmind [NPF_FLOW_FORW] = NPF_ANY_CONN_ESTABLISHED,
66 1.16 rmind [NPF_FLOW_BACK] = NPF_ANY_CONN_ESTABLISHED,
67 1.6 rmind },
68 1.3 rmind };
69 1.1 rmind
70 1.8 rmind static u_int npf_generic_timeout[] __read_mostly = {
71 1.16 rmind [NPF_ANY_CONN_CLOSED] = 0,
72 1.16 rmind [NPF_ANY_CONN_NEW] = 30,
73 1.16 rmind [NPF_ANY_CONN_ESTABLISHED] = 60,
74 1.1 rmind };
75 1.1 rmind
76 1.8 rmind /*
77 1.12 rmind * State sampler for debugging.
78 1.12 rmind */
79 1.12 rmind #if defined(_NPF_TESTING)
80 1.12 rmind static void (*npf_state_sample)(npf_state_t *, bool) = NULL;
81 1.12 rmind #define NPF_STATE_SAMPLE(n, r) if (npf_state_sample) (*npf_state_sample)(n, r);
82 1.12 rmind #else
83 1.12 rmind #define NPF_STATE_SAMPLE(n, r)
84 1.12 rmind #endif
85 1.12 rmind
86 1.12 rmind /*
87 1.8 rmind * npf_state_init: initialise the state structure.
88 1.8 rmind *
89 1.8 rmind * Should normally be called on a first packet, which also determines the
90 1.8 rmind * direction in a case of connection-orientated protocol. Returns true on
91 1.8 rmind * success and false otherwise (e.g. if protocol is not supported).
92 1.8 rmind */
93 1.1 rmind bool
94 1.17 rmind npf_state_init(npf_cache_t *npc, npf_state_t *nst)
95 1.1 rmind {
96 1.14 rmind const int proto = npc->npc_proto;
97 1.6 rmind bool ret;
98 1.1 rmind
99 1.5 zoltan KASSERT(npf_iscached(npc, NPC_IP46));
100 1.5 zoltan KASSERT(npf_iscached(npc, NPC_LAYER4));
101 1.2 rmind
102 1.6 rmind memset(nst, 0, sizeof(npf_state_t));
103 1.2 rmind
104 1.6 rmind switch (proto) {
105 1.6 rmind case IPPROTO_TCP:
106 1.6 rmind /* Pass to TCP state tracking engine. */
107 1.17 rmind ret = npf_state_tcp(npc, nst, NPF_FLOW_FORW);
108 1.6 rmind break;
109 1.6 rmind case IPPROTO_UDP:
110 1.6 rmind case IPPROTO_ICMP:
111 1.6 rmind /* Generic. */
112 1.6 rmind nst->nst_state = npf_generic_fsm[nst->nst_state][NPF_FLOW_FORW];
113 1.6 rmind ret = true;
114 1.6 rmind break;
115 1.6 rmind default:
116 1.6 rmind ret = false;
117 1.1 rmind }
118 1.11 rmind NPF_STATE_SAMPLE(nst, ret);
119 1.6 rmind return ret;
120 1.1 rmind }
121 1.1 rmind
122 1.1 rmind void
123 1.1 rmind npf_state_destroy(npf_state_t *nst)
124 1.1 rmind {
125 1.6 rmind nst->nst_state = 0;
126 1.1 rmind }
127 1.1 rmind
128 1.8 rmind /*
129 1.8 rmind * npf_state_inspect: inspect the packet according to the protocol state.
130 1.8 rmind *
131 1.8 rmind * Return true if packet is considered to match the state (e.g. for TCP,
132 1.8 rmind * the packet belongs to the tracked connection) and false otherwise.
133 1.8 rmind */
134 1.1 rmind bool
135 1.17 rmind npf_state_inspect(npf_cache_t *npc, npf_state_t *nst, const bool forw)
136 1.1 rmind {
137 1.14 rmind const int proto = npc->npc_proto;
138 1.6 rmind const int di = forw ? NPF_FLOW_FORW : NPF_FLOW_BACK;
139 1.1 rmind bool ret;
140 1.1 rmind
141 1.1 rmind switch (proto) {
142 1.1 rmind case IPPROTO_TCP:
143 1.6 rmind /* Pass to TCP state tracking engine. */
144 1.17 rmind ret = npf_state_tcp(npc, nst, di);
145 1.6 rmind break;
146 1.6 rmind case IPPROTO_UDP:
147 1.6 rmind case IPPROTO_ICMP:
148 1.6 rmind /* Generic. */
149 1.6 rmind nst->nst_state = npf_generic_fsm[nst->nst_state][di];
150 1.6 rmind ret = true;
151 1.1 rmind break;
152 1.1 rmind default:
153 1.6 rmind ret = false;
154 1.1 rmind }
155 1.11 rmind NPF_STATE_SAMPLE(nst, ret);
156 1.6 rmind
157 1.1 rmind return ret;
158 1.1 rmind }
159 1.1 rmind
160 1.3 rmind /*
161 1.16 rmind * npf_state_etime: return connection expiration time according to the state.
162 1.3 rmind */
163 1.1 rmind int
164 1.1 rmind npf_state_etime(const npf_state_t *nst, const int proto)
165 1.1 rmind {
166 1.15 rmind const u_int state = nst->nst_state;
167 1.6 rmind int timeout = 0;
168 1.1 rmind
169 1.6 rmind switch (proto) {
170 1.6 rmind case IPPROTO_TCP:
171 1.6 rmind /* Pass to TCP state tracking engine. */
172 1.6 rmind timeout = npf_state_tcp_timeout(nst);
173 1.6 rmind break;
174 1.6 rmind case IPPROTO_UDP:
175 1.6 rmind case IPPROTO_ICMP:
176 1.6 rmind /* Generic. */
177 1.6 rmind timeout = npf_generic_timeout[state];
178 1.6 rmind break;
179 1.6 rmind default:
180 1.6 rmind KASSERT(false);
181 1.1 rmind }
182 1.6 rmind return timeout;
183 1.1 rmind }
184 1.1 rmind
185 1.1 rmind void
186 1.9 rmind npf_state_dump(const npf_state_t *nst)
187 1.1 rmind {
188 1.4 yamt #if defined(DDB) || defined(_NPF_TESTING)
189 1.9 rmind const npf_tcpstate_t *fst = &nst->nst_tcpst[0];
190 1.9 rmind const npf_tcpstate_t *tst = &nst->nst_tcpst[1];
191 1.1 rmind
192 1.1 rmind printf("\tstate (%p) %d:\n\t\t"
193 1.6 rmind "F { end %u maxend %u mwin %u wscale %u }\n\t\t"
194 1.6 rmind "T { end %u maxend %u mwin %u wscale %u }\n",
195 1.1 rmind nst, nst->nst_state,
196 1.6 rmind fst->nst_end, fst->nst_maxend, fst->nst_maxwin, fst->nst_wscale,
197 1.6 rmind tst->nst_end, tst->nst_maxend, tst->nst_maxwin, tst->nst_wscale
198 1.1 rmind );
199 1.4 yamt #endif
200 1.1 rmind }
201 1.12 rmind
202 1.12 rmind #if defined(_NPF_TESTING)
203 1.12 rmind void
204 1.12 rmind npf_state_setsampler(void (*func)(npf_state_t *, bool))
205 1.12 rmind {
206 1.12 rmind npf_state_sample = func;
207 1.12 rmind }
208 1.12 rmind #endif
209