npf.c revision 1.1 1 1.1 rmind /* $NetBSD: npf.c,v 1.1 2010/08/22 18:56:22 rmind Exp $ */
2 1.1 rmind
3 1.1 rmind /*-
4 1.1 rmind * Copyright (c) 2009-2010 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.1 rmind * NPF main: dynamic load/initialisation and unload routines.
34 1.1 rmind */
35 1.1 rmind
36 1.1 rmind #include <sys/cdefs.h>
37 1.1 rmind __KERNEL_RCSID(0, "$NetBSD: npf.c,v 1.1 2010/08/22 18:56:22 rmind Exp $");
38 1.1 rmind
39 1.1 rmind #include <sys/param.h>
40 1.1 rmind #include <sys/types.h>
41 1.1 rmind
42 1.1 rmind #include <sys/conf.h>
43 1.1 rmind #include <sys/kauth.h>
44 1.1 rmind #include <sys/lwp.h>
45 1.1 rmind #include <sys/module.h>
46 1.1 rmind #include <sys/socketvar.h>
47 1.1 rmind #include <sys/uio.h>
48 1.1 rmind
49 1.1 rmind #include "npf_impl.h"
50 1.1 rmind
51 1.1 rmind /*
52 1.1 rmind * Module and device structures.
53 1.1 rmind */
54 1.1 rmind MODULE(MODULE_CLASS_MISC, npf, NULL);
55 1.1 rmind
56 1.1 rmind void npfattach(int);
57 1.1 rmind
58 1.1 rmind static int npf_dev_open(dev_t, int, int, lwp_t *);
59 1.1 rmind static int npf_dev_close(dev_t, int, int, lwp_t *);
60 1.1 rmind static int npf_dev_ioctl(dev_t, u_long, void *, int, lwp_t *);
61 1.1 rmind static int npf_dev_poll(dev_t, int, lwp_t *);
62 1.1 rmind static int npf_dev_read(dev_t, struct uio *, int);
63 1.1 rmind
64 1.1 rmind const struct cdevsw npf_cdevsw = {
65 1.1 rmind npf_dev_open, npf_dev_close, npf_dev_read, nowrite, npf_dev_ioctl,
66 1.1 rmind nostop, notty, npf_dev_poll, nommap, nokqfilter, D_OTHER | D_MPSAFE
67 1.1 rmind };
68 1.1 rmind
69 1.1 rmind static int
70 1.1 rmind npf_init(void)
71 1.1 rmind {
72 1.1 rmind #ifdef _MODULE
73 1.1 rmind devmajor_t bmajor = NODEVMAJOR, cmajor = NODEVMAJOR;
74 1.1 rmind #endif
75 1.1 rmind int error;
76 1.1 rmind
77 1.1 rmind /*
78 1.1 rmind * Initialise ruleset, tables and session structures.
79 1.1 rmind */
80 1.1 rmind
81 1.1 rmind error = npf_ruleset_sysinit();
82 1.1 rmind if (error)
83 1.1 rmind return error;
84 1.1 rmind
85 1.1 rmind error = npf_tableset_sysinit();
86 1.1 rmind if (error) {
87 1.1 rmind npf_ruleset_sysfini();
88 1.1 rmind return error;
89 1.1 rmind }
90 1.1 rmind
91 1.1 rmind error = npf_session_sysinit();
92 1.1 rmind if (error) {
93 1.1 rmind npf_tableset_sysfini();
94 1.1 rmind npf_ruleset_sysfini();
95 1.1 rmind return error;
96 1.1 rmind }
97 1.1 rmind npf_nat_sysinit();
98 1.1 rmind npf_alg_sysinit();
99 1.1 rmind
100 1.1 rmind #ifdef _MODULE
101 1.1 rmind /* Attach /dev/npf device. */
102 1.1 rmind error = devsw_attach("npf", NULL, &bmajor, &npf_cdevsw, &cmajor);
103 1.1 rmind if (error) {
104 1.1 rmind npf_nat_sysfini();
105 1.1 rmind npf_session_sysfini();
106 1.1 rmind npf_tableset_sysfini();
107 1.1 rmind npf_ruleset_sysfini();
108 1.1 rmind }
109 1.1 rmind #endif
110 1.1 rmind return error;
111 1.1 rmind }
112 1.1 rmind
113 1.1 rmind static int
114 1.1 rmind npf_fini(void)
115 1.1 rmind {
116 1.1 rmind
117 1.1 rmind #ifdef _MODULE
118 1.1 rmind /* At first, detach device and remove pfil hooks. */
119 1.1 rmind devsw_detach(NULL, &npf_cdevsw);
120 1.1 rmind #endif
121 1.1 rmind npf_nat_sysfini();
122 1.1 rmind npf_alg_sysfini();
123 1.1 rmind npf_session_sysfini();
124 1.1 rmind npf_tableset_sysfini();
125 1.1 rmind npf_ruleset_sysfini();
126 1.1 rmind
127 1.1 rmind return 0;
128 1.1 rmind }
129 1.1 rmind
130 1.1 rmind /*
131 1.1 rmind * Module interface.
132 1.1 rmind */
133 1.1 rmind static int
134 1.1 rmind npf_modcmd(modcmd_t cmd, void *arg)
135 1.1 rmind {
136 1.1 rmind
137 1.1 rmind switch (cmd) {
138 1.1 rmind case MODULE_CMD_INIT:
139 1.1 rmind return npf_init();
140 1.1 rmind case MODULE_CMD_FINI:
141 1.1 rmind return npf_fini();
142 1.1 rmind default:
143 1.1 rmind return ENOTTY;
144 1.1 rmind }
145 1.1 rmind return 0;
146 1.1 rmind }
147 1.1 rmind
148 1.1 rmind void
149 1.1 rmind npfattach(int nunits)
150 1.1 rmind {
151 1.1 rmind
152 1.1 rmind /* Void. */
153 1.1 rmind }
154 1.1 rmind
155 1.1 rmind static int
156 1.1 rmind npf_dev_open(dev_t dev, int flag, int mode, lwp_t *l)
157 1.1 rmind {
158 1.1 rmind
159 1.1 rmind /* Available only for super-user. */
160 1.1 rmind if (kauth_authorize_generic(l->l_cred, KAUTH_GENERIC_ISSUSER, NULL)) {
161 1.1 rmind return EPERM;
162 1.1 rmind }
163 1.1 rmind return 0;
164 1.1 rmind }
165 1.1 rmind
166 1.1 rmind static int
167 1.1 rmind npf_dev_close(dev_t dev, int flag, int mode, lwp_t *l)
168 1.1 rmind {
169 1.1 rmind
170 1.1 rmind return 0;
171 1.1 rmind }
172 1.1 rmind
173 1.1 rmind static int
174 1.1 rmind npf_dev_ioctl(dev_t dev, u_long cmd, void *data, int flag, lwp_t *l)
175 1.1 rmind {
176 1.1 rmind int error;
177 1.1 rmind
178 1.1 rmind /* Available only for super-user. */
179 1.1 rmind if (kauth_authorize_generic(l->l_cred, KAUTH_GENERIC_ISSUSER, NULL)) {
180 1.1 rmind return EPERM;
181 1.1 rmind }
182 1.1 rmind
183 1.1 rmind switch (cmd) {
184 1.1 rmind case IOC_NPF_VERSION:
185 1.1 rmind *(int *)data = NPF_VERSION;
186 1.1 rmind error = 0;
187 1.1 rmind break;
188 1.1 rmind case IOC_NPF_SWITCH:
189 1.1 rmind error = npfctl_switch(data);
190 1.1 rmind break;
191 1.1 rmind case IOC_NPF_RELOAD:
192 1.1 rmind error = npfctl_reload(cmd, data);
193 1.1 rmind break;
194 1.1 rmind case IOC_NPF_TABLE:
195 1.1 rmind error = npfctl_table(data);
196 1.1 rmind break;
197 1.1 rmind default:
198 1.1 rmind error = ENOTTY;
199 1.1 rmind break;
200 1.1 rmind }
201 1.1 rmind return error;
202 1.1 rmind }
203 1.1 rmind
204 1.1 rmind static int
205 1.1 rmind npf_dev_poll(dev_t dev, int events, lwp_t *l)
206 1.1 rmind {
207 1.1 rmind
208 1.1 rmind return ENOTSUP;
209 1.1 rmind }
210 1.1 rmind
211 1.1 rmind static int
212 1.1 rmind npf_dev_read(dev_t dev, struct uio *uio, int flag)
213 1.1 rmind {
214 1.1 rmind
215 1.1 rmind return ENOTSUP;
216 1.1 rmind }
217