npf_rproc.c revision 1.1.4.2 1 /* $NetBSD: npf_rproc.c,v 1.1.4.2 2012/02/18 07:35:38 mrg Exp $ */
2
3 /*-
4 * Copyright (c) 2009-2012 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This material is based upon work partially supported by The
8 * NetBSD Foundation under a contract with Mindaugas Rasiukevicius.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 /*
33 * NPF rule procedure interface.
34 */
35
36 #include <sys/cdefs.h>
37 __KERNEL_RCSID(0, "$NetBSD");
38
39 #include <sys/param.h>
40 #include <sys/kernel.h>
41
42 #include <sys/atomic.h>
43 #include <sys/kmem.h>
44 #include <sys/types.h>
45
46 #include "npf_impl.h"
47
48 #define NPF_RNAME_LEN 16
49
50 /* Rule procedure structure. */
51 struct npf_rproc {
52 /* Name. */
53 char rp_name[NPF_RNAME_LEN];
54 /* Reference count. */
55 u_int rp_refcnt;
56 uint32_t rp_flags;
57 /* Normalisation options. */
58 bool rp_rnd_ipid;
59 bool rp_no_df;
60 u_int rp_minttl;
61 u_int rp_maxmss;
62 /* Logging interface. */
63 u_int rp_log_ifid;
64 };
65
66 npf_rproc_t *
67 npf_rproc_create(prop_dictionary_t rpdict)
68 {
69 npf_rproc_t *rp;
70 const char *rname;
71
72 rp = kmem_zalloc(sizeof(npf_rproc_t), KM_SLEEP);
73 rp->rp_refcnt = 1;
74
75 /* Name and flags. */
76 prop_dictionary_get_cstring_nocopy(rpdict, "name", &rname);
77 strlcpy(rp->rp_name, rname, NPF_RNAME_LEN);
78 prop_dictionary_get_uint32(rpdict, "flags", &rp->rp_flags);
79
80 /* Logging interface ID (integer). */
81 prop_dictionary_get_uint32(rpdict, "log-interface", &rp->rp_log_ifid);
82
83 /* IP ID randomisation and IP_DF flag cleansing. */
84 prop_dictionary_get_bool(rpdict, "randomize-id", &rp->rp_rnd_ipid);
85 prop_dictionary_get_bool(rpdict, "no-df", &rp->rp_no_df);
86
87 /* Minimum IP TTL and maximum TCP MSS. */
88 prop_dictionary_get_uint32(rpdict, "min-ttl", &rp->rp_minttl);
89 prop_dictionary_get_uint32(rpdict, "max-mss", &rp->rp_maxmss);
90
91 return rp;
92 }
93
94 void
95 npf_rproc_acquire(npf_rproc_t *rp)
96 {
97
98 atomic_inc_uint(&rp->rp_refcnt);
99 }
100
101 void
102 npf_rproc_release(npf_rproc_t *rp)
103 {
104
105 /* Destroy on last reference. */
106 KASSERT(rp->rp_refcnt > 0);
107 if (atomic_dec_uint_nv(&rp->rp_refcnt) != 0) {
108 return;
109 }
110 kmem_free(rp, sizeof(npf_rproc_t));
111 }
112
113 void
114 npf_rproc_run(npf_cache_t *npc, nbuf_t *nbuf, npf_rproc_t *rp, int error)
115 {
116 const uint32_t flags = rp->rp_flags;
117
118 KASSERT(rp->rp_refcnt > 0);
119
120 /* Normalise the packet, if required. */
121 if ((flags & NPF_RPROC_NORMALIZE) != 0 && !error) {
122 (void)npf_normalize(npc, nbuf,
123 rp->rp_rnd_ipid, rp->rp_no_df,
124 rp->rp_minttl, rp->rp_maxmss);
125 npf_stats_inc(NPF_STAT_RPROC_NORM);
126 }
127
128 /* Log packet, if required. */
129 if ((flags & NPF_RPROC_LOG) != 0) {
130 npf_log_packet(npc, nbuf, rp->rp_log_ifid);
131 npf_stats_inc(NPF_STAT_RPROC_LOG);
132 }
133 }
134