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