npf_state.c revision 1.9 1 1.9 rmind /* $NetBSD: npf_state.c,v 1.9 2012/07/01 23:21:06 rmind Exp $ */
2 1.1 rmind
3 1.1 rmind /*-
4 1.8 rmind * Copyright (c) 2010-2012 The NetBSD Foundation, Inc.
5 1.1 rmind * All rights reserved.
6 1.1 rmind *
7 1.1 rmind * This material is based upon work partially supported by The
8 1.1 rmind * NetBSD Foundation under a contract with Mindaugas Rasiukevicius.
9 1.1 rmind *
10 1.1 rmind * Redistribution and use in source and binary forms, with or without
11 1.1 rmind * modification, are permitted provided that the following conditions
12 1.1 rmind * are met:
13 1.1 rmind * 1. Redistributions of source code must retain the above copyright
14 1.1 rmind * notice, this list of conditions and the following disclaimer.
15 1.1 rmind * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 rmind * notice, this list of conditions and the following disclaimer in the
17 1.1 rmind * documentation and/or other materials provided with the distribution.
18 1.1 rmind *
19 1.1 rmind * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1 rmind * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 rmind * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 rmind * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1 rmind * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1 rmind * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1 rmind * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 rmind * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 rmind * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 rmind * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 rmind * POSSIBILITY OF SUCH DAMAGE.
30 1.1 rmind */
31 1.1 rmind
32 1.1 rmind /*
33 1.6 rmind * NPF state engine to track sessions.
34 1.1 rmind */
35 1.1 rmind
36 1.1 rmind #include <sys/cdefs.h>
37 1.9 rmind __KERNEL_RCSID(0, "$NetBSD: npf_state.c,v 1.9 2012/07/01 23:21:06 rmind Exp $");
38 1.1 rmind
39 1.1 rmind #include <sys/param.h>
40 1.1 rmind #include <sys/systm.h>
41 1.1 rmind
42 1.1 rmind #include <sys/mutex.h>
43 1.1 rmind
44 1.1 rmind #include "npf_impl.h"
45 1.1 rmind
46 1.6 rmind /*
47 1.6 rmind * Generic session states and timeout table.
48 1.6 rmind *
49 1.6 rmind * Note: used for connnection-less protocols.
50 1.6 rmind */
51 1.6 rmind
52 1.6 rmind #define NPF_ANY_SESSION_CLOSED 0
53 1.6 rmind #define NPF_ANY_SESSION_NEW 1
54 1.6 rmind #define NPF_ANY_SESSION_ESTABLISHED 2
55 1.6 rmind #define NPF_ANY_SESSION_NSTATES 3
56 1.6 rmind
57 1.8 rmind static const int npf_generic_fsm[NPF_ANY_SESSION_NSTATES][2] = {
58 1.6 rmind [NPF_ANY_SESSION_CLOSED] = {
59 1.6 rmind [NPF_FLOW_FORW] = NPF_ANY_SESSION_NEW,
60 1.6 rmind },
61 1.6 rmind [NPF_ANY_SESSION_NEW] = {
62 1.6 rmind [NPF_FLOW_FORW] = NPF_ANY_SESSION_NEW,
63 1.6 rmind [NPF_FLOW_BACK] = NPF_ANY_SESSION_ESTABLISHED,
64 1.6 rmind },
65 1.6 rmind [NPF_ANY_SESSION_ESTABLISHED] = {
66 1.6 rmind [NPF_FLOW_FORW] = NPF_ANY_SESSION_ESTABLISHED,
67 1.6 rmind [NPF_FLOW_BACK] = NPF_ANY_SESSION_ESTABLISHED,
68 1.6 rmind },
69 1.3 rmind };
70 1.1 rmind
71 1.8 rmind static u_int npf_generic_timeout[] __read_mostly = {
72 1.6 rmind [NPF_ANY_SESSION_CLOSED] = 0,
73 1.6 rmind [NPF_ANY_SESSION_NEW] = 30,
74 1.6 rmind [NPF_ANY_SESSION_ESTABLISHED] = 60,
75 1.1 rmind };
76 1.1 rmind
77 1.8 rmind /*
78 1.8 rmind * npf_state_init: initialise the state structure.
79 1.8 rmind *
80 1.8 rmind * Should normally be called on a first packet, which also determines the
81 1.8 rmind * direction in a case of connection-orientated protocol. Returns true on
82 1.8 rmind * success and false otherwise (e.g. if protocol is not supported).
83 1.8 rmind */
84 1.1 rmind bool
85 1.1 rmind npf_state_init(const npf_cache_t *npc, nbuf_t *nbuf, npf_state_t *nst)
86 1.1 rmind {
87 1.1 rmind const int proto = npf_cache_ipproto(npc);
88 1.6 rmind bool ret;
89 1.1 rmind
90 1.5 zoltan KASSERT(npf_iscached(npc, NPC_IP46));
91 1.5 zoltan KASSERT(npf_iscached(npc, NPC_LAYER4));
92 1.2 rmind
93 1.6 rmind memset(nst, 0, sizeof(npf_state_t));
94 1.2 rmind mutex_init(&nst->nst_lock, MUTEX_DEFAULT, IPL_SOFTNET);
95 1.2 rmind
96 1.6 rmind switch (proto) {
97 1.6 rmind case IPPROTO_TCP:
98 1.6 rmind /* Pass to TCP state tracking engine. */
99 1.6 rmind ret = npf_state_tcp(npc, nbuf, nst, NPF_FLOW_FORW);
100 1.6 rmind break;
101 1.6 rmind case IPPROTO_UDP:
102 1.6 rmind case IPPROTO_ICMP:
103 1.6 rmind /* Generic. */
104 1.6 rmind nst->nst_state = npf_generic_fsm[nst->nst_state][NPF_FLOW_FORW];
105 1.6 rmind ret = true;
106 1.6 rmind break;
107 1.6 rmind default:
108 1.6 rmind ret = false;
109 1.1 rmind }
110 1.6 rmind return ret;
111 1.1 rmind }
112 1.1 rmind
113 1.1 rmind void
114 1.1 rmind npf_state_destroy(npf_state_t *nst)
115 1.1 rmind {
116 1.1 rmind
117 1.6 rmind nst->nst_state = 0;
118 1.1 rmind mutex_destroy(&nst->nst_lock);
119 1.1 rmind }
120 1.1 rmind
121 1.8 rmind /*
122 1.8 rmind * npf_state_inspect: inspect the packet according to the protocol state.
123 1.8 rmind *
124 1.8 rmind * Return true if packet is considered to match the state (e.g. for TCP,
125 1.8 rmind * the packet belongs to the tracked connection) and false otherwise.
126 1.8 rmind */
127 1.1 rmind bool
128 1.1 rmind npf_state_inspect(const npf_cache_t *npc, nbuf_t *nbuf,
129 1.1 rmind npf_state_t *nst, const bool forw)
130 1.1 rmind {
131 1.1 rmind const int proto = npf_cache_ipproto(npc);
132 1.6 rmind const int di = forw ? NPF_FLOW_FORW : NPF_FLOW_BACK;
133 1.1 rmind bool ret;
134 1.1 rmind
135 1.1 rmind mutex_enter(&nst->nst_lock);
136 1.1 rmind switch (proto) {
137 1.1 rmind case IPPROTO_TCP:
138 1.6 rmind /* Pass to TCP state tracking engine. */
139 1.6 rmind ret = npf_state_tcp(npc, nbuf, nst, di);
140 1.6 rmind break;
141 1.6 rmind case IPPROTO_UDP:
142 1.6 rmind case IPPROTO_ICMP:
143 1.6 rmind /* Generic. */
144 1.6 rmind nst->nst_state = npf_generic_fsm[nst->nst_state][di];
145 1.6 rmind ret = true;
146 1.1 rmind break;
147 1.1 rmind default:
148 1.6 rmind ret = false;
149 1.1 rmind }
150 1.7 rmind NPF_TCP_STATE_SAMPLE(nst, ret);
151 1.1 rmind mutex_exit(&nst->nst_lock);
152 1.6 rmind
153 1.1 rmind return ret;
154 1.1 rmind }
155 1.1 rmind
156 1.3 rmind /*
157 1.3 rmind * npf_state_etime: return session expiration time according to the state.
158 1.3 rmind */
159 1.1 rmind int
160 1.1 rmind npf_state_etime(const npf_state_t *nst, const int proto)
161 1.1 rmind {
162 1.3 rmind const int state = nst->nst_state;
163 1.6 rmind int timeout = 0;
164 1.1 rmind
165 1.6 rmind switch (proto) {
166 1.6 rmind case IPPROTO_TCP:
167 1.6 rmind /* Pass to TCP state tracking engine. */
168 1.6 rmind timeout = npf_state_tcp_timeout(nst);
169 1.6 rmind break;
170 1.6 rmind case IPPROTO_UDP:
171 1.6 rmind case IPPROTO_ICMP:
172 1.6 rmind /* Generic. */
173 1.6 rmind timeout = npf_generic_timeout[state];
174 1.6 rmind break;
175 1.6 rmind default:
176 1.6 rmind KASSERT(false);
177 1.1 rmind }
178 1.6 rmind return timeout;
179 1.1 rmind }
180 1.1 rmind
181 1.1 rmind void
182 1.9 rmind npf_state_dump(const npf_state_t *nst)
183 1.1 rmind {
184 1.4 yamt #if defined(DDB) || defined(_NPF_TESTING)
185 1.9 rmind const npf_tcpstate_t *fst = &nst->nst_tcpst[0];
186 1.9 rmind const npf_tcpstate_t *tst = &nst->nst_tcpst[1];
187 1.1 rmind
188 1.1 rmind printf("\tstate (%p) %d:\n\t\t"
189 1.6 rmind "F { end %u maxend %u mwin %u wscale %u }\n\t\t"
190 1.6 rmind "T { end %u maxend %u mwin %u wscale %u }\n",
191 1.1 rmind nst, nst->nst_state,
192 1.6 rmind fst->nst_end, fst->nst_maxend, fst->nst_maxwin, fst->nst_wscale,
193 1.6 rmind tst->nst_end, tst->nst_maxend, tst->nst_maxwin, tst->nst_wscale
194 1.1 rmind );
195 1.4 yamt #endif
196 1.1 rmind }
197