npf_conn.c revision 1.9.2.2 1 1.9.2.2 tls /* $NetBSD: npf_conn.c,v 1.9.2.2 2014/08/10 06:56:16 tls Exp $ */
2 1.9.2.2 tls
3 1.9.2.2 tls /*-
4 1.9.2.2 tls * Copyright (c) 2014 Mindaugas Rasiukevicius <rmind at netbsd org>
5 1.9.2.2 tls * Copyright (c) 2010-2014 The NetBSD Foundation, Inc.
6 1.9.2.2 tls * All rights reserved.
7 1.9.2.2 tls *
8 1.9.2.2 tls * This material is based upon work partially supported by The
9 1.9.2.2 tls * NetBSD Foundation under a contract with Mindaugas Rasiukevicius.
10 1.9.2.2 tls *
11 1.9.2.2 tls * Redistribution and use in source and binary forms, with or without
12 1.9.2.2 tls * modification, are permitted provided that the following conditions
13 1.9.2.2 tls * are met:
14 1.9.2.2 tls * 1. Redistributions of source code must retain the above copyright
15 1.9.2.2 tls * notice, this list of conditions and the following disclaimer.
16 1.9.2.2 tls * 2. Redistributions in binary form must reproduce the above copyright
17 1.9.2.2 tls * notice, this list of conditions and the following disclaimer in the
18 1.9.2.2 tls * documentation and/or other materials provided with the distribution.
19 1.9.2.2 tls *
20 1.9.2.2 tls * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 1.9.2.2 tls * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 1.9.2.2 tls * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 1.9.2.2 tls * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 1.9.2.2 tls * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 1.9.2.2 tls * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 1.9.2.2 tls * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 1.9.2.2 tls * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 1.9.2.2 tls * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 1.9.2.2 tls * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 1.9.2.2 tls * POSSIBILITY OF SUCH DAMAGE.
31 1.9.2.2 tls */
32 1.9.2.2 tls
33 1.9.2.2 tls /*
34 1.9.2.2 tls * NPF connection tracking for stateful filtering and translation.
35 1.9.2.2 tls *
36 1.9.2.2 tls * Overview
37 1.9.2.2 tls *
38 1.9.2.2 tls * Connection direction is identified by the direction of its first
39 1.9.2.2 tls * packet. Packets can be incoming or outgoing with respect to an
40 1.9.2.2 tls * interface. To describe the packet in the context of connection
41 1.9.2.2 tls * direction we will use the terms "forwards stream" and "backwards
42 1.9.2.2 tls * stream". All connections have two keys and thus two entries:
43 1.9.2.2 tls *
44 1.9.2.2 tls * npf_conn_t::c_forw_entry for the forwards stream and
45 1.9.2.2 tls * npf_conn_t::c_back_entry for the backwards stream.
46 1.9.2.2 tls *
47 1.9.2.2 tls * The keys are formed from the 5-tuple (source/destination address,
48 1.9.2.2 tls * source/destination port and the protocol). Additional matching
49 1.9.2.2 tls * is performed for the interface (a common behaviour is equivalent
50 1.9.2.2 tls * to the 6-tuple lookup including the interface ID). Note that the
51 1.9.2.2 tls * key may be formed using translated values in a case of NAT.
52 1.9.2.2 tls *
53 1.9.2.2 tls * Connections can serve two purposes: for the implicit passing or
54 1.9.2.2 tls * to accommodate the dynamic NAT. Connections for the former purpose
55 1.9.2.2 tls * are created by the rules with "stateful" attribute and are used for
56 1.9.2.2 tls * stateful filtering. Such connections indicate that the packet of
57 1.9.2.2 tls * the backwards stream should be passed without inspection of the
58 1.9.2.2 tls * ruleset. The other purpose is to associate a dynamic NAT mechanism
59 1.9.2.2 tls * with a connection. Such connections are created by the NAT policies
60 1.9.2.2 tls * and they have a relationship with NAT translation structure via
61 1.9.2.2 tls * npf_conn_t::c_nat. A single connection can serve both purposes,
62 1.9.2.2 tls * which is a common case.
63 1.9.2.2 tls *
64 1.9.2.2 tls * Connection life-cycle
65 1.9.2.2 tls *
66 1.9.2.2 tls * Connections are established when a packet matches said rule or
67 1.9.2.2 tls * NAT policy. Both keys of the established connection are inserted
68 1.9.2.2 tls * into the connection database. A garbage collection thread
69 1.9.2.2 tls * periodically scans all connections and depending on connection
70 1.9.2.2 tls * properties (e.g. last activity time, protocol) removes connection
71 1.9.2.2 tls * entries and expires the actual connections.
72 1.9.2.2 tls *
73 1.9.2.2 tls * Each connection has a reference count. The reference is acquired
74 1.9.2.2 tls * on lookup and should be released by the caller. It guarantees that
75 1.9.2.2 tls * the connection will not be destroyed, although it may be expired.
76 1.9.2.2 tls *
77 1.9.2.2 tls * Synchronisation
78 1.9.2.2 tls *
79 1.9.2.2 tls * Connection database is accessed in a lock-less manner by the main
80 1.9.2.2 tls * routines: npf_conn_inspect() and npf_conn_establish(). Since they
81 1.9.2.2 tls * are always called from a software interrupt, the database is
82 1.9.2.2 tls * protected using passive serialisation. The main place which can
83 1.9.2.2 tls * destroy a connection is npf_conn_worker(). The database itself
84 1.9.2.2 tls * can be replaced and destroyed in npf_conn_reload().
85 1.9.2.2 tls *
86 1.9.2.2 tls * ALG support
87 1.9.2.2 tls *
88 1.9.2.2 tls * Application-level gateways (ALGs) can override generic connection
89 1.9.2.2 tls * inspection (npf_alg_conn() call in npf_conn_inspect() function) by
90 1.9.2.2 tls * performing their own lookup using different key. Recursive call
91 1.9.2.2 tls * to npf_conn_inspect() is not allowed. The ALGs ought to use the
92 1.9.2.2 tls * npf_conn_lookup() function for this purpose.
93 1.9.2.2 tls *
94 1.9.2.2 tls * Lock order
95 1.9.2.2 tls *
96 1.9.2.2 tls * npf_config_lock ->
97 1.9.2.2 tls * conn_lock ->
98 1.9.2.2 tls * npf_conn_t::c_lock
99 1.9.2.2 tls */
100 1.9.2.2 tls
101 1.9.2.2 tls #include <sys/cdefs.h>
102 1.9.2.2 tls __KERNEL_RCSID(0, "$NetBSD: npf_conn.c,v 1.9.2.2 2014/08/10 06:56:16 tls Exp $");
103 1.9.2.2 tls
104 1.9.2.2 tls #include <sys/param.h>
105 1.9.2.2 tls #include <sys/types.h>
106 1.9.2.2 tls
107 1.9.2.2 tls #include <netinet/in.h>
108 1.9.2.2 tls #include <netinet/tcp.h>
109 1.9.2.2 tls
110 1.9.2.2 tls #include <sys/atomic.h>
111 1.9.2.2 tls #include <sys/condvar.h>
112 1.9.2.2 tls #include <sys/kmem.h>
113 1.9.2.2 tls #include <sys/kthread.h>
114 1.9.2.2 tls #include <sys/mutex.h>
115 1.9.2.2 tls #include <net/pfil.h>
116 1.9.2.2 tls #include <sys/pool.h>
117 1.9.2.2 tls #include <sys/queue.h>
118 1.9.2.2 tls #include <sys/systm.h>
119 1.9.2.2 tls
120 1.9.2.2 tls #define __NPF_CONN_PRIVATE
121 1.9.2.2 tls #include "npf_conn.h"
122 1.9.2.2 tls #include "npf_impl.h"
123 1.9.2.2 tls
124 1.9.2.2 tls /*
125 1.9.2.2 tls * Connection flags: PFIL_IN and PFIL_OUT values are reserved for direction.
126 1.9.2.2 tls */
127 1.9.2.2 tls CTASSERT(PFIL_ALL == (0x001 | 0x002));
128 1.9.2.2 tls #define CONN_ACTIVE 0x004 /* visible on inspection */
129 1.9.2.2 tls #define CONN_PASS 0x008 /* perform implicit passing */
130 1.9.2.2 tls #define CONN_EXPIRE 0x010 /* explicitly expire */
131 1.9.2.2 tls #define CONN_REMOVED 0x020 /* "forw/back" entries removed */
132 1.9.2.2 tls
133 1.9.2.2 tls /*
134 1.9.2.2 tls * Connection tracking state: disabled (off) or enabled (on).
135 1.9.2.2 tls */
136 1.9.2.2 tls enum { CONN_TRACKING_OFF, CONN_TRACKING_ON };
137 1.9.2.2 tls static volatile int conn_tracking __cacheline_aligned;
138 1.9.2.2 tls
139 1.9.2.2 tls /* Connection tracking database, connection cache and the lock. */
140 1.9.2.2 tls static npf_conndb_t * conn_db __read_mostly;
141 1.9.2.2 tls static pool_cache_t conn_cache __read_mostly;
142 1.9.2.2 tls static kmutex_t conn_lock __cacheline_aligned;
143 1.9.2.2 tls
144 1.9.2.2 tls static void npf_conn_worker(void);
145 1.9.2.2 tls static void npf_conn_destroy(npf_conn_t *);
146 1.9.2.2 tls
147 1.9.2.2 tls /*
148 1.9.2.2 tls * npf_conn_sys{init,fini}: initialise/destroy connection tracking.
149 1.9.2.2 tls */
150 1.9.2.2 tls
151 1.9.2.2 tls void
152 1.9.2.2 tls npf_conn_sysinit(void)
153 1.9.2.2 tls {
154 1.9.2.2 tls conn_cache = pool_cache_init(sizeof(npf_conn_t), coherency_unit,
155 1.9.2.2 tls 0, 0, "npfconpl", NULL, IPL_NET, NULL, NULL, NULL);
156 1.9.2.2 tls mutex_init(&conn_lock, MUTEX_DEFAULT, IPL_NONE);
157 1.9.2.2 tls conn_tracking = CONN_TRACKING_OFF;
158 1.9.2.2 tls conn_db = npf_conndb_create();
159 1.9.2.2 tls
160 1.9.2.2 tls npf_worker_register(npf_conn_worker);
161 1.9.2.2 tls }
162 1.9.2.2 tls
163 1.9.2.2 tls void
164 1.9.2.2 tls npf_conn_sysfini(void)
165 1.9.2.2 tls {
166 1.9.2.2 tls /* Note: the caller should have flushed the connections. */
167 1.9.2.2 tls KASSERT(conn_tracking == CONN_TRACKING_OFF);
168 1.9.2.2 tls npf_worker_unregister(npf_conn_worker);
169 1.9.2.2 tls
170 1.9.2.2 tls npf_conndb_destroy(conn_db);
171 1.9.2.2 tls pool_cache_destroy(conn_cache);
172 1.9.2.2 tls mutex_destroy(&conn_lock);
173 1.9.2.2 tls }
174 1.9.2.2 tls
175 1.9.2.2 tls /*
176 1.9.2.2 tls * npf_conn_load: perform the load by flushing the current connection
177 1.9.2.2 tls * database and replacing it with the new one or just destroying.
178 1.9.2.2 tls *
179 1.9.2.2 tls * => The caller must disable the connection tracking and ensure that
180 1.9.2.2 tls * there are no connection database lookups or references in-flight.
181 1.9.2.2 tls */
182 1.9.2.2 tls void
183 1.9.2.2 tls npf_conn_load(npf_conndb_t *ndb, bool track)
184 1.9.2.2 tls {
185 1.9.2.2 tls npf_conndb_t *odb = NULL;
186 1.9.2.2 tls
187 1.9.2.2 tls KASSERT(npf_config_locked_p());
188 1.9.2.2 tls
189 1.9.2.2 tls /*
190 1.9.2.2 tls * The connection database is in the quiescent state.
191 1.9.2.2 tls * Prevent G/C thread from running and install a new database.
192 1.9.2.2 tls */
193 1.9.2.2 tls mutex_enter(&conn_lock);
194 1.9.2.2 tls if (ndb) {
195 1.9.2.2 tls KASSERT(conn_tracking == CONN_TRACKING_OFF);
196 1.9.2.2 tls odb = conn_db;
197 1.9.2.2 tls conn_db = ndb;
198 1.9.2.2 tls membar_sync();
199 1.9.2.2 tls }
200 1.9.2.2 tls if (track) {
201 1.9.2.2 tls /* After this point lookups start flying in. */
202 1.9.2.2 tls conn_tracking = CONN_TRACKING_ON;
203 1.9.2.2 tls }
204 1.9.2.2 tls mutex_exit(&conn_lock);
205 1.9.2.2 tls
206 1.9.2.2 tls if (odb) {
207 1.9.2.2 tls /*
208 1.9.2.2 tls * Flush all, no sync since the caller did it for us.
209 1.9.2.2 tls * Also, release the pool cache memory.
210 1.9.2.2 tls */
211 1.9.2.2 tls npf_conn_gc(odb, true, false);
212 1.9.2.2 tls npf_conndb_destroy(odb);
213 1.9.2.2 tls pool_cache_invalidate(conn_cache);
214 1.9.2.2 tls }
215 1.9.2.2 tls }
216 1.9.2.2 tls
217 1.9.2.2 tls /*
218 1.9.2.2 tls * npf_conn_tracking: enable/disable connection tracking.
219 1.9.2.2 tls */
220 1.9.2.2 tls void
221 1.9.2.2 tls npf_conn_tracking(bool track)
222 1.9.2.2 tls {
223 1.9.2.2 tls KASSERT(npf_config_locked_p());
224 1.9.2.2 tls conn_tracking = track ? CONN_TRACKING_ON : CONN_TRACKING_OFF;
225 1.9.2.2 tls }
226 1.9.2.2 tls
227 1.9.2.2 tls static inline bool
228 1.9.2.2 tls npf_conn_trackable_p(const npf_cache_t *npc)
229 1.9.2.2 tls {
230 1.9.2.2 tls /*
231 1.9.2.2 tls * Check if connection tracking is on. Also, if layer 3 and 4 are
232 1.9.2.2 tls * not cached - protocol is not supported or packet is invalid.
233 1.9.2.2 tls */
234 1.9.2.2 tls if (conn_tracking != CONN_TRACKING_ON) {
235 1.9.2.2 tls return false;
236 1.9.2.2 tls }
237 1.9.2.2 tls if (!npf_iscached(npc, NPC_IP46) || !npf_iscached(npc, NPC_LAYER4)) {
238 1.9.2.2 tls return false;
239 1.9.2.2 tls }
240 1.9.2.2 tls return true;
241 1.9.2.2 tls }
242 1.9.2.2 tls
243 1.9.2.2 tls /*
244 1.9.2.2 tls * npf_conn_conkey: construct a key for the connection lookup.
245 1.9.2.2 tls *
246 1.9.2.2 tls * => Returns the key length in bytes or zero on failure.
247 1.9.2.2 tls */
248 1.9.2.2 tls unsigned
249 1.9.2.2 tls npf_conn_conkey(const npf_cache_t *npc, npf_connkey_t *key, const bool forw)
250 1.9.2.2 tls {
251 1.9.2.2 tls const u_int alen = npc->npc_alen;
252 1.9.2.2 tls const struct tcphdr *th;
253 1.9.2.2 tls const struct udphdr *uh;
254 1.9.2.2 tls u_int keylen, isrc, idst;
255 1.9.2.2 tls uint16_t id[2];
256 1.9.2.2 tls
257 1.9.2.2 tls switch (npc->npc_proto) {
258 1.9.2.2 tls case IPPROTO_TCP:
259 1.9.2.2 tls KASSERT(npf_iscached(npc, NPC_TCP));
260 1.9.2.2 tls th = npc->npc_l4.tcp;
261 1.9.2.2 tls id[NPF_SRC] = th->th_sport;
262 1.9.2.2 tls id[NPF_DST] = th->th_dport;
263 1.9.2.2 tls break;
264 1.9.2.2 tls case IPPROTO_UDP:
265 1.9.2.2 tls KASSERT(npf_iscached(npc, NPC_UDP));
266 1.9.2.2 tls uh = npc->npc_l4.udp;
267 1.9.2.2 tls id[NPF_SRC] = uh->uh_sport;
268 1.9.2.2 tls id[NPF_DST] = uh->uh_dport;
269 1.9.2.2 tls break;
270 1.9.2.2 tls case IPPROTO_ICMP:
271 1.9.2.2 tls if (npf_iscached(npc, NPC_ICMP_ID)) {
272 1.9.2.2 tls const struct icmp *ic = npc->npc_l4.icmp;
273 1.9.2.2 tls id[NPF_SRC] = ic->icmp_id;
274 1.9.2.2 tls id[NPF_DST] = ic->icmp_id;
275 1.9.2.2 tls break;
276 1.9.2.2 tls }
277 1.9.2.2 tls return 0;
278 1.9.2.2 tls case IPPROTO_ICMPV6:
279 1.9.2.2 tls if (npf_iscached(npc, NPC_ICMP_ID)) {
280 1.9.2.2 tls const struct icmp6_hdr *ic6 = npc->npc_l4.icmp6;
281 1.9.2.2 tls id[NPF_SRC] = ic6->icmp6_id;
282 1.9.2.2 tls id[NPF_DST] = ic6->icmp6_id;
283 1.9.2.2 tls break;
284 1.9.2.2 tls }
285 1.9.2.2 tls return 0;
286 1.9.2.2 tls default:
287 1.9.2.2 tls /* Unsupported protocol. */
288 1.9.2.2 tls return 0;
289 1.9.2.2 tls }
290 1.9.2.2 tls
291 1.9.2.2 tls if (__predict_true(forw)) {
292 1.9.2.2 tls isrc = NPF_SRC, idst = NPF_DST;
293 1.9.2.2 tls } else {
294 1.9.2.2 tls isrc = NPF_DST, idst = NPF_SRC;
295 1.9.2.2 tls }
296 1.9.2.2 tls
297 1.9.2.2 tls /*
298 1.9.2.2 tls * Construct a key formed out of 32-bit integers. The key layout:
299 1.9.2.2 tls *
300 1.9.2.2 tls * Field: | proto | alen | src-id | dst-id | src-addr | dst-addr |
301 1.9.2.2 tls * +--------+--------+--------+--------+----------+----------+
302 1.9.2.2 tls * Bits: | 16 | 16 | 16 | 16 | 32-128 | 32-128 |
303 1.9.2.2 tls *
304 1.9.2.2 tls * The source and destination are inverted if they key is for the
305 1.9.2.2 tls * backwards stream (forw == false). The address length depends
306 1.9.2.2 tls * on the 'alen' field; it is a length in bytes, either 4 or 16.
307 1.9.2.2 tls */
308 1.9.2.2 tls
309 1.9.2.2 tls key->ck_key[0] = ((uint32_t)npc->npc_proto << 16) | (alen & 0xffff);
310 1.9.2.2 tls key->ck_key[1] = ((uint32_t)id[isrc] << 16) | id[idst];
311 1.9.2.2 tls
312 1.9.2.2 tls if (__predict_true(alen == sizeof(in_addr_t))) {
313 1.9.2.2 tls key->ck_key[2] = npc->npc_ips[isrc]->s6_addr32[0];
314 1.9.2.2 tls key->ck_key[3] = npc->npc_ips[idst]->s6_addr32[0];
315 1.9.2.2 tls keylen = 4 * sizeof(uint32_t);
316 1.9.2.2 tls } else {
317 1.9.2.2 tls const u_int nwords = alen >> 2;
318 1.9.2.2 tls memcpy(&key->ck_key[2], npc->npc_ips[isrc], alen);
319 1.9.2.2 tls memcpy(&key->ck_key[2 + nwords], npc->npc_ips[idst], alen);
320 1.9.2.2 tls keylen = (2 + (nwords * 2)) * sizeof(uint32_t);
321 1.9.2.2 tls }
322 1.9.2.2 tls return keylen;
323 1.9.2.2 tls }
324 1.9.2.2 tls
325 1.9.2.2 tls static __inline void
326 1.9.2.2 tls connkey_set_addr(npf_connkey_t *key, const npf_addr_t *naddr, const int di)
327 1.9.2.2 tls {
328 1.9.2.2 tls const u_int alen = key->ck_key[0] & 0xffff;
329 1.9.2.2 tls uint32_t *addr = &key->ck_key[2 + ((alen >> 2) * di)];
330 1.9.2.2 tls
331 1.9.2.2 tls KASSERT(alen > 0);
332 1.9.2.2 tls memcpy(addr, naddr, alen);
333 1.9.2.2 tls }
334 1.9.2.2 tls
335 1.9.2.2 tls static __inline void
336 1.9.2.2 tls connkey_set_id(npf_connkey_t *key, const uint16_t id, const int di)
337 1.9.2.2 tls {
338 1.9.2.2 tls const uint32_t oid = key->ck_key[1];
339 1.9.2.2 tls const u_int shift = 16 * !di;
340 1.9.2.2 tls const uint32_t mask = 0xffff0000 >> shift;
341 1.9.2.2 tls
342 1.9.2.2 tls key->ck_key[1] = ((uint32_t)id << shift) | (oid & mask);
343 1.9.2.2 tls }
344 1.9.2.2 tls
345 1.9.2.2 tls /*
346 1.9.2.2 tls * npf_conn_lookup: lookup if there is an established connection.
347 1.9.2.2 tls *
348 1.9.2.2 tls * => If found, we will hold a reference for the caller.
349 1.9.2.2 tls */
350 1.9.2.2 tls npf_conn_t *
351 1.9.2.2 tls npf_conn_lookup(const npf_cache_t *npc, const int di, bool *forw)
352 1.9.2.2 tls {
353 1.9.2.2 tls const nbuf_t *nbuf = npc->npc_nbuf;
354 1.9.2.2 tls npf_conn_t *con;
355 1.9.2.2 tls npf_connkey_t key;
356 1.9.2.2 tls u_int flags, cifid;
357 1.9.2.2 tls bool ok, pforw;
358 1.9.2.2 tls
359 1.9.2.2 tls /* Construct a key and lookup for a connection in the store. */
360 1.9.2.2 tls if (!npf_conn_conkey(npc, &key, true)) {
361 1.9.2.2 tls return NULL;
362 1.9.2.2 tls }
363 1.9.2.2 tls con = npf_conndb_lookup(conn_db, &key, forw);
364 1.9.2.2 tls if (con == NULL) {
365 1.9.2.2 tls return NULL;
366 1.9.2.2 tls }
367 1.9.2.2 tls KASSERT(npc->npc_proto == con->c_proto);
368 1.9.2.2 tls
369 1.9.2.2 tls /* Check if connection is active and not expired. */
370 1.9.2.2 tls flags = con->c_flags;
371 1.9.2.2 tls ok = (flags & (CONN_ACTIVE | CONN_EXPIRE)) == CONN_ACTIVE;
372 1.9.2.2 tls
373 1.9.2.2 tls if (__predict_false(!ok)) {
374 1.9.2.2 tls atomic_dec_uint(&con->c_refcnt);
375 1.9.2.2 tls return NULL;
376 1.9.2.2 tls }
377 1.9.2.2 tls
378 1.9.2.2 tls /*
379 1.9.2.2 tls * Match the interface and the direction of the connection entry
380 1.9.2.2 tls * and the packet.
381 1.9.2.2 tls */
382 1.9.2.2 tls cifid = con->c_ifid;
383 1.9.2.2 tls if (__predict_false(cifid && cifid != nbuf->nb_ifid)) {
384 1.9.2.2 tls atomic_dec_uint(&con->c_refcnt);
385 1.9.2.2 tls return NULL;
386 1.9.2.2 tls }
387 1.9.2.2 tls pforw = (flags & PFIL_ALL) == di;
388 1.9.2.2 tls if (__predict_false(*forw != pforw)) {
389 1.9.2.2 tls atomic_dec_uint(&con->c_refcnt);
390 1.9.2.2 tls return NULL;
391 1.9.2.2 tls }
392 1.9.2.2 tls
393 1.9.2.2 tls /* Update the last activity time. */
394 1.9.2.2 tls getnanouptime(&con->c_atime);
395 1.9.2.2 tls return con;
396 1.9.2.2 tls }
397 1.9.2.2 tls
398 1.9.2.2 tls /*
399 1.9.2.2 tls * npf_conn_inspect: lookup a connection and inspecting the protocol data.
400 1.9.2.2 tls *
401 1.9.2.2 tls * => If found, we will hold a reference for the caller.
402 1.9.2.2 tls */
403 1.9.2.2 tls npf_conn_t *
404 1.9.2.2 tls npf_conn_inspect(npf_cache_t *npc, const int di, int *error)
405 1.9.2.2 tls {
406 1.9.2.2 tls nbuf_t *nbuf = npc->npc_nbuf;
407 1.9.2.2 tls npf_conn_t *con;
408 1.9.2.2 tls bool forw, ok;
409 1.9.2.2 tls
410 1.9.2.2 tls KASSERT(!nbuf_flag_p(nbuf, NBUF_DATAREF_RESET));
411 1.9.2.2 tls if (!npf_conn_trackable_p(npc)) {
412 1.9.2.2 tls return NULL;
413 1.9.2.2 tls }
414 1.9.2.2 tls
415 1.9.2.2 tls /* Query ALG which may lookup connection for us. */
416 1.9.2.2 tls if ((con = npf_alg_conn(npc, di)) != NULL) {
417 1.9.2.2 tls /* Note: reference is held. */
418 1.9.2.2 tls return con;
419 1.9.2.2 tls }
420 1.9.2.2 tls if (nbuf_head_mbuf(nbuf) == NULL) {
421 1.9.2.2 tls *error = ENOMEM;
422 1.9.2.2 tls return NULL;
423 1.9.2.2 tls }
424 1.9.2.2 tls KASSERT(!nbuf_flag_p(nbuf, NBUF_DATAREF_RESET));
425 1.9.2.2 tls
426 1.9.2.2 tls /* Main lookup of the connection. */
427 1.9.2.2 tls if ((con = npf_conn_lookup(npc, di, &forw)) == NULL) {
428 1.9.2.2 tls return NULL;
429 1.9.2.2 tls }
430 1.9.2.2 tls
431 1.9.2.2 tls /* Inspect the protocol data and handle state changes. */
432 1.9.2.2 tls mutex_enter(&con->c_lock);
433 1.9.2.2 tls ok = npf_state_inspect(npc, &con->c_state, forw);
434 1.9.2.2 tls mutex_exit(&con->c_lock);
435 1.9.2.2 tls
436 1.9.2.2 tls if (__predict_false(!ok)) {
437 1.9.2.2 tls /* Invalid: let the rules deal with it. */
438 1.9.2.2 tls npf_conn_release(con);
439 1.9.2.2 tls npf_stats_inc(NPF_STAT_INVALID_STATE);
440 1.9.2.2 tls con = NULL;
441 1.9.2.2 tls }
442 1.9.2.2 tls return con;
443 1.9.2.2 tls }
444 1.9.2.2 tls
445 1.9.2.2 tls /*
446 1.9.2.2 tls * npf_conn_establish: create a new connection, insert into the global list.
447 1.9.2.2 tls *
448 1.9.2.2 tls * => Connection is created with the reference held for the caller.
449 1.9.2.2 tls * => Connection will be activated on the first reference release.
450 1.9.2.2 tls */
451 1.9.2.2 tls npf_conn_t *
452 1.9.2.2 tls npf_conn_establish(npf_cache_t *npc, int di, bool per_if)
453 1.9.2.2 tls {
454 1.9.2.2 tls const nbuf_t *nbuf = npc->npc_nbuf;
455 1.9.2.2 tls npf_conn_t *con;
456 1.9.2.2 tls
457 1.9.2.2 tls KASSERT(!nbuf_flag_p(nbuf, NBUF_DATAREF_RESET));
458 1.9.2.2 tls
459 1.9.2.2 tls if (!npf_conn_trackable_p(npc)) {
460 1.9.2.2 tls return NULL;
461 1.9.2.2 tls }
462 1.9.2.2 tls
463 1.9.2.2 tls /* Allocate and initialise the new connection. */
464 1.9.2.2 tls con = pool_cache_get(conn_cache, PR_NOWAIT);
465 1.9.2.2 tls if (__predict_false(!con)) {
466 1.9.2.2 tls return NULL;
467 1.9.2.2 tls }
468 1.9.2.2 tls NPF_PRINTF(("NPF: create conn %p\n", con));
469 1.9.2.2 tls npf_stats_inc(NPF_STAT_CONN_CREATE);
470 1.9.2.2 tls
471 1.9.2.2 tls /* Reference count and flags (indicate direction). */
472 1.9.2.2 tls mutex_init(&con->c_lock, MUTEX_DEFAULT, IPL_SOFTNET);
473 1.9.2.2 tls con->c_flags = (di & PFIL_ALL);
474 1.9.2.2 tls con->c_refcnt = 1;
475 1.9.2.2 tls con->c_rproc = NULL;
476 1.9.2.2 tls con->c_nat = NULL;
477 1.9.2.2 tls
478 1.9.2.2 tls /* Initialize protocol state. */
479 1.9.2.2 tls if (!npf_state_init(npc, &con->c_state)) {
480 1.9.2.2 tls goto err;
481 1.9.2.2 tls }
482 1.9.2.2 tls
483 1.9.2.2 tls KASSERT(npf_iscached(npc, NPC_IP46));
484 1.9.2.2 tls npf_connkey_t *fw = &con->c_forw_entry;
485 1.9.2.2 tls npf_connkey_t *bk = &con->c_back_entry;
486 1.9.2.2 tls
487 1.9.2.2 tls /*
488 1.9.2.2 tls * Construct "forwards" and "backwards" keys. Also, set the
489 1.9.2.2 tls * interface ID for this connection (unless it is global).
490 1.9.2.2 tls */
491 1.9.2.2 tls if (!npf_conn_conkey(npc, fw, true)) {
492 1.9.2.2 tls goto err;
493 1.9.2.2 tls }
494 1.9.2.2 tls if (!npf_conn_conkey(npc, bk, false)) {
495 1.9.2.2 tls goto err;
496 1.9.2.2 tls }
497 1.9.2.2 tls fw->ck_backptr = bk->ck_backptr = con;
498 1.9.2.2 tls con->c_ifid = per_if ? nbuf->nb_ifid : 0;
499 1.9.2.2 tls con->c_proto = npc->npc_proto;
500 1.9.2.2 tls
501 1.9.2.2 tls /* Set last activity time for a new connection. */
502 1.9.2.2 tls getnanouptime(&con->c_atime);
503 1.9.2.2 tls
504 1.9.2.2 tls /*
505 1.9.2.2 tls * Insert both keys (entries representing directions) of the
506 1.9.2.2 tls * connection. At this point, it becomes visible.
507 1.9.2.2 tls */
508 1.9.2.2 tls if (!npf_conndb_insert(conn_db, fw, con)) {
509 1.9.2.2 tls goto err;
510 1.9.2.2 tls }
511 1.9.2.2 tls if (!npf_conndb_insert(conn_db, bk, con)) {
512 1.9.2.2 tls /* We have hit the duplicate. */
513 1.9.2.2 tls npf_conndb_remove(conn_db, fw);
514 1.9.2.2 tls npf_stats_inc(NPF_STAT_RACE_CONN);
515 1.9.2.2 tls goto err;
516 1.9.2.2 tls }
517 1.9.2.2 tls
518 1.9.2.2 tls /* Finally, insert into the connection list. */
519 1.9.2.2 tls NPF_PRINTF(("NPF: establish conn %p\n", con));
520 1.9.2.2 tls npf_conndb_enqueue(conn_db, con);
521 1.9.2.2 tls return con;
522 1.9.2.2 tls err:
523 1.9.2.2 tls npf_conn_destroy(con);
524 1.9.2.2 tls return NULL;
525 1.9.2.2 tls }
526 1.9.2.2 tls
527 1.9.2.2 tls static void
528 1.9.2.2 tls npf_conn_destroy(npf_conn_t *con)
529 1.9.2.2 tls {
530 1.9.2.2 tls if (con->c_nat) {
531 1.9.2.2 tls /* Release any NAT structures. */
532 1.9.2.2 tls npf_nat_destroy(con->c_nat);
533 1.9.2.2 tls }
534 1.9.2.2 tls if (con->c_rproc) {
535 1.9.2.2 tls /* Release the rule procedure. */
536 1.9.2.2 tls npf_rproc_release(con->c_rproc);
537 1.9.2.2 tls }
538 1.9.2.2 tls
539 1.9.2.2 tls /* Destroy the state. */
540 1.9.2.2 tls npf_state_destroy(&con->c_state);
541 1.9.2.2 tls mutex_destroy(&con->c_lock);
542 1.9.2.2 tls
543 1.9.2.2 tls /* Free the structure, increase the counter. */
544 1.9.2.2 tls pool_cache_put(conn_cache, con);
545 1.9.2.2 tls npf_stats_inc(NPF_STAT_CONN_DESTROY);
546 1.9.2.2 tls NPF_PRINTF(("NPF: conn %p destroyed\n", con));
547 1.9.2.2 tls }
548 1.9.2.2 tls
549 1.9.2.2 tls /*
550 1.9.2.2 tls * npf_conn_setnat: associate NAT entry with the connection, update and
551 1.9.2.2 tls * re-insert connection entry using the translation values.
552 1.9.2.2 tls */
553 1.9.2.2 tls int
554 1.9.2.2 tls npf_conn_setnat(const npf_cache_t *npc, npf_conn_t *con,
555 1.9.2.2 tls npf_nat_t *nt, u_int ntype)
556 1.9.2.2 tls {
557 1.9.2.2 tls static const u_int nat_type_dimap[] = {
558 1.9.2.2 tls [NPF_NATOUT] = NPF_DST,
559 1.9.2.2 tls [NPF_NATIN] = NPF_SRC,
560 1.9.2.2 tls };
561 1.9.2.2 tls npf_connkey_t key, *bk;
562 1.9.2.2 tls npf_conn_t *ret __diagused;
563 1.9.2.2 tls npf_addr_t *taddr;
564 1.9.2.2 tls in_port_t tport;
565 1.9.2.2 tls u_int tidx;
566 1.9.2.2 tls
567 1.9.2.2 tls KASSERT(con->c_refcnt > 0);
568 1.9.2.2 tls
569 1.9.2.2 tls npf_nat_gettrans(nt, &taddr, &tport);
570 1.9.2.2 tls KASSERT(ntype == NPF_NATOUT || ntype == NPF_NATIN);
571 1.9.2.2 tls tidx = nat_type_dimap[ntype];
572 1.9.2.2 tls
573 1.9.2.2 tls /* Construct a "backwards" key. */
574 1.9.2.2 tls if (!npf_conn_conkey(npc, &key, false)) {
575 1.9.2.2 tls return EINVAL;
576 1.9.2.2 tls }
577 1.9.2.2 tls
578 1.9.2.2 tls /* Acquire the lock and check for the races. */
579 1.9.2.2 tls mutex_enter(&con->c_lock);
580 1.9.2.2 tls if (__predict_false(con->c_flags & CONN_EXPIRE)) {
581 1.9.2.2 tls /* The connection got expired. */
582 1.9.2.2 tls mutex_exit(&con->c_lock);
583 1.9.2.2 tls return EINVAL;
584 1.9.2.2 tls }
585 1.9.2.2 tls if (__predict_false(con->c_nat != NULL)) {
586 1.9.2.2 tls /* Race with a duplicate packet. */
587 1.9.2.2 tls mutex_exit(&con->c_lock);
588 1.9.2.2 tls npf_stats_inc(NPF_STAT_RACE_NAT);
589 1.9.2.2 tls return EISCONN;
590 1.9.2.2 tls }
591 1.9.2.2 tls
592 1.9.2.2 tls /* Remove the "backwards" entry. */
593 1.9.2.2 tls ret = npf_conndb_remove(conn_db, &key);
594 1.9.2.2 tls KASSERT(ret == con);
595 1.9.2.2 tls
596 1.9.2.2 tls /* Set the source/destination IDs to the translation values. */
597 1.9.2.2 tls bk = &con->c_back_entry;
598 1.9.2.2 tls connkey_set_addr(bk, taddr, tidx);
599 1.9.2.2 tls if (tport) {
600 1.9.2.2 tls connkey_set_id(bk, tport, tidx);
601 1.9.2.2 tls }
602 1.9.2.2 tls
603 1.9.2.2 tls /* Finally, re-insert the "backwards" entry. */
604 1.9.2.2 tls if (!npf_conndb_insert(conn_db, bk, con)) {
605 1.9.2.2 tls /*
606 1.9.2.2 tls * Race: we have hit the duplicate, remove the "forwards"
607 1.9.2.2 tls * entry and expire our connection; it is no longer valid.
608 1.9.2.2 tls */
609 1.9.2.2 tls (void)npf_conndb_remove(conn_db, &con->c_forw_entry);
610 1.9.2.2 tls atomic_or_uint(&con->c_flags, CONN_REMOVED | CONN_EXPIRE);
611 1.9.2.2 tls mutex_exit(&con->c_lock);
612 1.9.2.2 tls
613 1.9.2.2 tls npf_stats_inc(NPF_STAT_RACE_NAT);
614 1.9.2.2 tls return EISCONN;
615 1.9.2.2 tls }
616 1.9.2.2 tls
617 1.9.2.2 tls /* Associate the NAT entry and release the lock. */
618 1.9.2.2 tls con->c_nat = nt;
619 1.9.2.2 tls mutex_exit(&con->c_lock);
620 1.9.2.2 tls return 0;
621 1.9.2.2 tls }
622 1.9.2.2 tls
623 1.9.2.2 tls /*
624 1.9.2.2 tls * npf_conn_expire: explicitly mark connection as expired.
625 1.9.2.2 tls */
626 1.9.2.2 tls void
627 1.9.2.2 tls npf_conn_expire(npf_conn_t *con)
628 1.9.2.2 tls {
629 1.9.2.2 tls /* KASSERT(con->c_refcnt > 0); XXX: npf_nat_freepolicy() */
630 1.9.2.2 tls atomic_or_uint(&con->c_flags, CONN_EXPIRE);
631 1.9.2.2 tls }
632 1.9.2.2 tls
633 1.9.2.2 tls /*
634 1.9.2.2 tls * npf_conn_pass: return true if connection is "pass" one, otherwise false.
635 1.9.2.2 tls */
636 1.9.2.2 tls bool
637 1.9.2.2 tls npf_conn_pass(const npf_conn_t *con, npf_rproc_t **rp)
638 1.9.2.2 tls {
639 1.9.2.2 tls KASSERT(con->c_refcnt > 0);
640 1.9.2.2 tls if (__predict_true(con->c_flags & CONN_PASS)) {
641 1.9.2.2 tls *rp = con->c_rproc;
642 1.9.2.2 tls return true;
643 1.9.2.2 tls }
644 1.9.2.2 tls return false;
645 1.9.2.2 tls }
646 1.9.2.2 tls
647 1.9.2.2 tls /*
648 1.9.2.2 tls * npf_conn_setpass: mark connection as a "pass" one and associate the
649 1.9.2.2 tls * rule procedure with it.
650 1.9.2.2 tls */
651 1.9.2.2 tls void
652 1.9.2.2 tls npf_conn_setpass(npf_conn_t *con, npf_rproc_t *rp)
653 1.9.2.2 tls {
654 1.9.2.2 tls KASSERT((con->c_flags & CONN_ACTIVE) == 0);
655 1.9.2.2 tls KASSERT(con->c_refcnt > 0);
656 1.9.2.2 tls KASSERT(con->c_rproc == NULL);
657 1.9.2.2 tls
658 1.9.2.2 tls /*
659 1.9.2.2 tls * No need for atomic since the connection is not yet active.
660 1.9.2.2 tls * If rproc is set, the caller transfers its reference to us,
661 1.9.2.2 tls * which will be released on npf_conn_destroy().
662 1.9.2.2 tls */
663 1.9.2.2 tls con->c_flags |= CONN_PASS;
664 1.9.2.2 tls con->c_rproc = rp;
665 1.9.2.2 tls }
666 1.9.2.2 tls
667 1.9.2.2 tls /*
668 1.9.2.2 tls * npf_conn_release: release a reference, which might allow G/C thread
669 1.9.2.2 tls * to destroy this connection.
670 1.9.2.2 tls */
671 1.9.2.2 tls void
672 1.9.2.2 tls npf_conn_release(npf_conn_t *con)
673 1.9.2.2 tls {
674 1.9.2.2 tls if ((con->c_flags & (CONN_ACTIVE | CONN_EXPIRE)) == 0) {
675 1.9.2.2 tls /* Activate: after this, connection is globally visible. */
676 1.9.2.2 tls con->c_flags |= CONN_ACTIVE;
677 1.9.2.2 tls }
678 1.9.2.2 tls KASSERT(con->c_refcnt > 0);
679 1.9.2.2 tls atomic_dec_uint(&con->c_refcnt);
680 1.9.2.2 tls }
681 1.9.2.2 tls
682 1.9.2.2 tls /*
683 1.9.2.2 tls * npf_conn_retnat: return associated NAT data entry and indicate
684 1.9.2.2 tls * whether it is a "forwards" or "backwards" stream.
685 1.9.2.2 tls */
686 1.9.2.2 tls npf_nat_t *
687 1.9.2.2 tls npf_conn_retnat(npf_conn_t *con, const int di, bool *forw)
688 1.9.2.2 tls {
689 1.9.2.2 tls KASSERT(con->c_refcnt > 0);
690 1.9.2.2 tls *forw = (con->c_flags & PFIL_ALL) == di;
691 1.9.2.2 tls return con->c_nat;
692 1.9.2.2 tls }
693 1.9.2.2 tls
694 1.9.2.2 tls /*
695 1.9.2.2 tls * npf_conn_expired: criterion to check if connection is expired.
696 1.9.2.2 tls */
697 1.9.2.2 tls static inline bool
698 1.9.2.2 tls npf_conn_expired(const npf_conn_t *con, const struct timespec *tsnow)
699 1.9.2.2 tls {
700 1.9.2.2 tls const int etime = npf_state_etime(&con->c_state, con->c_proto);
701 1.9.2.2 tls struct timespec tsdiff;
702 1.9.2.2 tls
703 1.9.2.2 tls if (__predict_false(con->c_flags & CONN_EXPIRE)) {
704 1.9.2.2 tls /* Explicitly marked to be expired. */
705 1.9.2.2 tls return true;
706 1.9.2.2 tls }
707 1.9.2.2 tls timespecsub(tsnow, &con->c_atime, &tsdiff);
708 1.9.2.2 tls return tsdiff.tv_sec > etime;
709 1.9.2.2 tls }
710 1.9.2.2 tls
711 1.9.2.2 tls /*
712 1.9.2.2 tls * npf_conn_gc: garbage collect the expired connections.
713 1.9.2.2 tls *
714 1.9.2.2 tls * => Must run in a single-threaded manner.
715 1.9.2.2 tls * => If it is a flush request, then destroy all connections.
716 1.9.2.2 tls * => If 'sync' is true, then perform passive serialisation.
717 1.9.2.2 tls */
718 1.9.2.2 tls void
719 1.9.2.2 tls npf_conn_gc(npf_conndb_t *cd, bool flush, bool sync)
720 1.9.2.2 tls {
721 1.9.2.2 tls npf_conn_t *con, *prev, *gclist = NULL;
722 1.9.2.2 tls struct timespec tsnow;
723 1.9.2.2 tls
724 1.9.2.2 tls getnanouptime(&tsnow);
725 1.9.2.2 tls
726 1.9.2.2 tls /*
727 1.9.2.2 tls * Scan all connections and check them for expiration.
728 1.9.2.2 tls */
729 1.9.2.2 tls prev = NULL;
730 1.9.2.2 tls con = npf_conndb_getlist(cd);
731 1.9.2.2 tls while (con) {
732 1.9.2.2 tls npf_conn_t *next = con->c_next;
733 1.9.2.2 tls
734 1.9.2.2 tls /* Expired? Flushing all? */
735 1.9.2.2 tls if (!npf_conn_expired(con, &tsnow) && !flush) {
736 1.9.2.2 tls prev = con;
737 1.9.2.2 tls con = next;
738 1.9.2.2 tls continue;
739 1.9.2.2 tls }
740 1.9.2.2 tls
741 1.9.2.2 tls /* Remove both entries of the connection. */
742 1.9.2.2 tls mutex_enter(&con->c_lock);
743 1.9.2.2 tls if ((con->c_flags & CONN_REMOVED) == 0) {
744 1.9.2.2 tls npf_conn_t *ret __diagused;
745 1.9.2.2 tls
746 1.9.2.2 tls ret = npf_conndb_remove(cd, &con->c_forw_entry);
747 1.9.2.2 tls KASSERT(ret == con);
748 1.9.2.2 tls ret = npf_conndb_remove(cd, &con->c_back_entry);
749 1.9.2.2 tls KASSERT(ret == con);
750 1.9.2.2 tls }
751 1.9.2.2 tls
752 1.9.2.2 tls /* Flag the removal and expiration. */
753 1.9.2.2 tls atomic_or_uint(&con->c_flags, CONN_REMOVED | CONN_EXPIRE);
754 1.9.2.2 tls mutex_exit(&con->c_lock);
755 1.9.2.2 tls
756 1.9.2.2 tls /* Move to the G/C list. */
757 1.9.2.2 tls npf_conndb_dequeue(cd, con, prev);
758 1.9.2.2 tls con->c_next = gclist;
759 1.9.2.2 tls gclist = con;
760 1.9.2.2 tls
761 1.9.2.2 tls /* Next.. */
762 1.9.2.2 tls con = next;
763 1.9.2.2 tls }
764 1.9.2.2 tls npf_conndb_settail(cd, prev);
765 1.9.2.2 tls
766 1.9.2.2 tls /*
767 1.9.2.2 tls * Ensure it is safe to destroy the connections.
768 1.9.2.2 tls * Note: drop the conn_lock (see the lock order).
769 1.9.2.2 tls */
770 1.9.2.2 tls if (sync) {
771 1.9.2.2 tls mutex_exit(&conn_lock);
772 1.9.2.2 tls if (gclist) {
773 1.9.2.2 tls npf_config_enter();
774 1.9.2.2 tls npf_config_sync();
775 1.9.2.2 tls npf_config_exit();
776 1.9.2.2 tls }
777 1.9.2.2 tls }
778 1.9.2.2 tls
779 1.9.2.2 tls /*
780 1.9.2.2 tls * Garbage collect all expired connections.
781 1.9.2.2 tls * May need to wait for the references to drain.
782 1.9.2.2 tls */
783 1.9.2.2 tls con = gclist;
784 1.9.2.2 tls while (con) {
785 1.9.2.2 tls npf_conn_t *next = con->c_next;
786 1.9.2.2 tls
787 1.9.2.2 tls /*
788 1.9.2.2 tls * Destroy only if removed and no references.
789 1.9.2.2 tls * Otherwise, wait for a tiny moment.
790 1.9.2.2 tls */
791 1.9.2.2 tls if (__predict_false(con->c_refcnt)) {
792 1.9.2.2 tls kpause("npfcongc", false, 1, NULL);
793 1.9.2.2 tls continue;
794 1.9.2.2 tls }
795 1.9.2.2 tls npf_conn_destroy(con);
796 1.9.2.2 tls con = next;
797 1.9.2.2 tls }
798 1.9.2.2 tls }
799 1.9.2.2 tls
800 1.9.2.2 tls /*
801 1.9.2.2 tls * npf_conn_worker: G/C to run from a worker thread.
802 1.9.2.2 tls */
803 1.9.2.2 tls static void
804 1.9.2.2 tls npf_conn_worker(void)
805 1.9.2.2 tls {
806 1.9.2.2 tls mutex_enter(&conn_lock);
807 1.9.2.2 tls /* Note: the conn_lock will be released (sync == true). */
808 1.9.2.2 tls npf_conn_gc(conn_db, false, true);
809 1.9.2.2 tls }
810 1.9.2.2 tls
811 1.9.2.2 tls /*
812 1.9.2.2 tls * npf_conn_export: construct a list of connections prepared for saving.
813 1.9.2.2 tls * Note: this is expected to be an expensive operation.
814 1.9.2.2 tls */
815 1.9.2.2 tls int
816 1.9.2.2 tls npf_conn_export(prop_array_t conlist)
817 1.9.2.2 tls {
818 1.9.2.2 tls npf_conn_t *con, *prev;
819 1.9.2.2 tls
820 1.9.2.2 tls /*
821 1.9.2.2 tls * Note: acquire conn_lock to prevent from the database
822 1.9.2.2 tls * destruction and G/C thread.
823 1.9.2.2 tls */
824 1.9.2.2 tls mutex_enter(&conn_lock);
825 1.9.2.2 tls if (conn_tracking != CONN_TRACKING_ON) {
826 1.9.2.2 tls mutex_exit(&conn_lock);
827 1.9.2.2 tls return 0;
828 1.9.2.2 tls }
829 1.9.2.2 tls prev = NULL;
830 1.9.2.2 tls con = npf_conndb_getlist(conn_db);
831 1.9.2.2 tls while (con) {
832 1.9.2.2 tls npf_conn_t *next = con->c_next;
833 1.9.2.2 tls prop_data_t d;
834 1.9.2.2 tls
835 1.9.2.2 tls if ((con->c_flags & (CONN_ACTIVE|CONN_EXPIRE)) != CONN_ACTIVE)
836 1.9.2.2 tls goto skip;
837 1.9.2.2 tls
838 1.9.2.2 tls prop_dictionary_t cdict = prop_dictionary_create();
839 1.9.2.2 tls prop_dictionary_set_uint32(cdict, "flags", con->c_flags);
840 1.9.2.2 tls prop_dictionary_set_uint32(cdict, "proto", con->c_proto);
841 1.9.2.2 tls /* FIXME: interface-id */
842 1.9.2.2 tls
843 1.9.2.2 tls d = prop_data_create_data(&con->c_state, sizeof(npf_state_t));
844 1.9.2.2 tls prop_dictionary_set_and_rel(cdict, "state", d);
845 1.9.2.2 tls
846 1.9.2.2 tls const uint32_t *fkey = con->c_forw_entry.ck_key;
847 1.9.2.2 tls d = prop_data_create_data(fkey, NPF_CONN_MAXKEYLEN);
848 1.9.2.2 tls prop_dictionary_set_and_rel(cdict, "forw-key", d);
849 1.9.2.2 tls
850 1.9.2.2 tls const uint32_t *bkey = con->c_back_entry.ck_key;
851 1.9.2.2 tls d = prop_data_create_data(bkey, NPF_CONN_MAXKEYLEN);
852 1.9.2.2 tls prop_dictionary_set_and_rel(cdict, "back-key", d);
853 1.9.2.2 tls
854 1.9.2.2 tls if (con->c_nat) {
855 1.9.2.2 tls npf_nat_export(cdict, con->c_nat);
856 1.9.2.2 tls }
857 1.9.2.2 tls prop_array_add(conlist, cdict);
858 1.9.2.2 tls prop_object_release(cdict);
859 1.9.2.2 tls skip:
860 1.9.2.2 tls prev = con;
861 1.9.2.2 tls con = next;
862 1.9.2.2 tls }
863 1.9.2.2 tls npf_conndb_settail(conn_db, prev);
864 1.9.2.2 tls mutex_exit(&conn_lock);
865 1.9.2.2 tls return 0;
866 1.9.2.2 tls }
867 1.9.2.2 tls
868 1.9.2.2 tls /*
869 1.9.2.2 tls * npf_conn_import: fully reconstruct a single connection from a
870 1.9.2.2 tls * directory and insert into the given database.
871 1.9.2.2 tls */
872 1.9.2.2 tls int
873 1.9.2.2 tls npf_conn_import(npf_conndb_t *cd, prop_dictionary_t cdict,
874 1.9.2.2 tls npf_ruleset_t *natlist)
875 1.9.2.2 tls {
876 1.9.2.2 tls npf_conn_t *con;
877 1.9.2.2 tls npf_connkey_t *fw, *bk;
878 1.9.2.2 tls prop_object_t obj;
879 1.9.2.2 tls const void *d;
880 1.9.2.2 tls
881 1.9.2.2 tls /* Allocate a connection and initialise it (clear first). */
882 1.9.2.2 tls con = pool_cache_get(conn_cache, PR_WAITOK);
883 1.9.2.2 tls memset(con, 0, sizeof(npf_conn_t));
884 1.9.2.2 tls mutex_init(&con->c_lock, MUTEX_DEFAULT, IPL_SOFTNET);
885 1.9.2.2 tls
886 1.9.2.2 tls prop_dictionary_get_uint32(cdict, "proto", &con->c_proto);
887 1.9.2.2 tls prop_dictionary_get_uint32(cdict, "flags", &con->c_flags);
888 1.9.2.2 tls con->c_flags &= PFIL_ALL | CONN_ACTIVE | CONN_PASS;
889 1.9.2.2 tls getnanouptime(&con->c_atime);
890 1.9.2.2 tls
891 1.9.2.2 tls obj = prop_dictionary_get(cdict, "state");
892 1.9.2.2 tls if ((d = prop_data_data_nocopy(obj)) == NULL ||
893 1.9.2.2 tls prop_data_size(obj) != sizeof(npf_state_t)) {
894 1.9.2.2 tls goto err;
895 1.9.2.2 tls }
896 1.9.2.2 tls memcpy(&con->c_state, d, sizeof(npf_state_t));
897 1.9.2.2 tls
898 1.9.2.2 tls /* Reconstruct NAT association, if any, or return NULL. */
899 1.9.2.2 tls con->c_nat = npf_nat_import(cdict, natlist, con);
900 1.9.2.2 tls
901 1.9.2.2 tls /*
902 1.9.2.2 tls * Fetch and copy the keys for each direction.
903 1.9.2.2 tls */
904 1.9.2.2 tls obj = prop_dictionary_get(cdict, "forw-key");
905 1.9.2.2 tls if ((d = prop_data_data_nocopy(obj)) == NULL ||
906 1.9.2.2 tls prop_data_size(obj) != NPF_CONN_MAXKEYLEN) {
907 1.9.2.2 tls goto err;
908 1.9.2.2 tls }
909 1.9.2.2 tls fw = &con->c_forw_entry;
910 1.9.2.2 tls memcpy(&fw->ck_key, d, NPF_CONN_MAXKEYLEN);
911 1.9.2.2 tls
912 1.9.2.2 tls obj = prop_dictionary_get(cdict, "back-key");
913 1.9.2.2 tls if ((d = prop_data_data_nocopy(obj)) == NULL ||
914 1.9.2.2 tls prop_data_size(obj) != NPF_CONN_MAXKEYLEN) {
915 1.9.2.2 tls goto err;
916 1.9.2.2 tls }
917 1.9.2.2 tls bk = &con->c_back_entry;
918 1.9.2.2 tls memcpy(&bk->ck_key, d, NPF_CONN_MAXKEYLEN);
919 1.9.2.2 tls
920 1.9.2.2 tls fw->ck_backptr = bk->ck_backptr = con;
921 1.9.2.2 tls
922 1.9.2.2 tls /* Insert the entries and the connection itself. */
923 1.9.2.2 tls if (!npf_conndb_insert(cd, fw, con)) {
924 1.9.2.2 tls goto err;
925 1.9.2.2 tls }
926 1.9.2.2 tls if (!npf_conndb_insert(cd, bk, con)) {
927 1.9.2.2 tls npf_conndb_remove(cd, fw);
928 1.9.2.2 tls goto err;
929 1.9.2.2 tls }
930 1.9.2.2 tls npf_conndb_enqueue(cd, con);
931 1.9.2.2 tls return 0;
932 1.9.2.2 tls err:
933 1.9.2.2 tls npf_conn_destroy(con);
934 1.9.2.2 tls return EINVAL;
935 1.9.2.2 tls }
936 1.9.2.2 tls
937 1.9.2.2 tls #if defined(DDB) || defined(_NPF_TESTING)
938 1.9.2.2 tls
939 1.9.2.2 tls void
940 1.9.2.2 tls npf_conn_print(const npf_conn_t *con)
941 1.9.2.2 tls {
942 1.9.2.2 tls const u_int alen = NPF_CONN_GETALEN(&con->c_forw_entry);
943 1.9.2.2 tls const uint32_t *fkey = con->c_forw_entry.ck_key;
944 1.9.2.2 tls const uint32_t *bkey = con->c_back_entry.ck_key;
945 1.9.2.2 tls const u_int proto = con->c_proto;
946 1.9.2.2 tls struct timespec tsnow, tsdiff;
947 1.9.2.2 tls const void *src, *dst;
948 1.9.2.2 tls int etime;
949 1.9.2.2 tls
950 1.9.2.2 tls getnanouptime(&tsnow);
951 1.9.2.2 tls timespecsub(&tsnow, &con->c_atime, &tsdiff);
952 1.9.2.2 tls etime = npf_state_etime(&con->c_state, proto);
953 1.9.2.2 tls
954 1.9.2.2 tls printf("%p:\n\tproto %d flags 0x%x tsdiff %d etime %d\n",
955 1.9.2.2 tls con, proto, con->c_flags, (int)tsdiff.tv_sec, etime);
956 1.9.2.2 tls
957 1.9.2.2 tls src = &fkey[2], dst = &fkey[2 + (alen >> 2)];
958 1.9.2.2 tls printf("\tforw %s:%d", npf_addr_dump(src, alen), ntohs(fkey[1] >> 16));
959 1.9.2.2 tls printf("-> %s:%d\n", npf_addr_dump(dst, alen), ntohs(fkey[1] & 0xffff));
960 1.9.2.2 tls
961 1.9.2.2 tls src = &bkey[2], dst = &bkey[2 + (alen >> 2)];
962 1.9.2.2 tls printf("\tback %s:%d", npf_addr_dump(src, alen), ntohs(bkey[1] >> 16));
963 1.9.2.2 tls printf("-> %s:%d\n", npf_addr_dump(dst, alen), ntohs(bkey[1] & 0xffff));
964 1.9.2.2 tls
965 1.9.2.2 tls npf_state_dump(&con->c_state);
966 1.9.2.2 tls if (con->c_nat) {
967 1.9.2.2 tls npf_nat_dump(con->c_nat);
968 1.9.2.2 tls }
969 1.9.2.2 tls }
970 1.9.2.2 tls
971 1.9.2.2 tls #endif
972