npf_os.c revision 1.13 1 /*-
2 * Copyright (c) 2009-2016 The NetBSD Foundation, Inc.
3 * All rights reserved.
4 *
5 * This material is based upon work partially supported by The
6 * NetBSD Foundation under a contract with Mindaugas Rasiukevicius.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
18 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
21 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 * POSSIBILITY OF SUCH DAMAGE.
28 */
29
30 /*
31 * NPF main: dynamic load/initialisation and unload routines.
32 */
33
34 #ifdef _KERNEL
35 #include <sys/cdefs.h>
36 __KERNEL_RCSID(0, "$NetBSD: npf_os.c,v 1.13 2019/08/10 21:13:54 rmind Exp $");
37
38 #ifdef _KERNEL_OPT
39 #include "pf.h"
40 #if NPF > 0
41 #error "NPF and PF are mutually exclusive; please select one"
42 #endif
43 #endif
44
45 #include <sys/param.h>
46 #include <sys/types.h>
47
48 #include <sys/conf.h>
49 #include <sys/kauth.h>
50 #include <sys/kmem.h>
51 #include <sys/lwp.h>
52 #include <sys/module.h>
53 #include <sys/socketvar.h>
54 #include <sys/uio.h>
55
56 #include <netinet/in.h>
57 #include <netinet6/in6_var.h>
58 #endif
59
60 #include "npf_impl.h"
61 #include "npfkern.h"
62
63 #ifdef _KERNEL
64 #ifndef _MODULE
65 #include "opt_modular.h"
66 #include "opt_net_mpsafe.h"
67 #endif
68 #include "ioconf.h"
69 #endif
70
71 /*
72 * Module and device structures.
73 */
74 #ifndef _MODULE
75 /*
76 * Modular kernels load drivers too early, and we need percpu to be inited
77 * So we make this misc; a better way would be to have early boot and late
78 * boot drivers.
79 */
80 MODULE(MODULE_CLASS_MISC, npf, "bpf");
81 #else
82 /* This module autoloads via /dev/npf so it needs to be a driver */
83 MODULE(MODULE_CLASS_DRIVER, npf, "bpf");
84 #endif
85
86 static int npf_dev_open(dev_t, int, int, lwp_t *);
87 static int npf_dev_close(dev_t, int, int, lwp_t *);
88 static int npf_dev_ioctl(dev_t, u_long, void *, int, lwp_t *);
89 static int npf_dev_poll(dev_t, int, lwp_t *);
90 static int npf_dev_read(dev_t, struct uio *, int);
91
92 const struct cdevsw npf_cdevsw = {
93 .d_open = npf_dev_open,
94 .d_close = npf_dev_close,
95 .d_read = npf_dev_read,
96 .d_write = nowrite,
97 .d_ioctl = npf_dev_ioctl,
98 .d_stop = nostop,
99 .d_tty = notty,
100 .d_poll = npf_dev_poll,
101 .d_mmap = nommap,
102 .d_kqfilter = nokqfilter,
103 .d_discard = nodiscard,
104 .d_flag = D_OTHER | D_MPSAFE
105 };
106
107 static const char * npf_ifop_getname(ifnet_t *);
108 static ifnet_t * npf_ifop_lookup(const char *);
109 static void npf_ifop_flush(void *);
110 static void * npf_ifop_getmeta(const ifnet_t *);
111 static void npf_ifop_setmeta(ifnet_t *, void *);
112
113 static const unsigned nworkers = 1;
114
115 static bool pfil_registered = false;
116 static pfil_head_t * npf_ph_if = NULL;
117 static pfil_head_t * npf_ph_inet = NULL;
118 static pfil_head_t * npf_ph_inet6 = NULL;
119
120 static const npf_ifops_t kern_ifops = {
121 .getname = npf_ifop_getname,
122 .lookup = npf_ifop_lookup,
123 .flush = npf_ifop_flush,
124 .getmeta = npf_ifop_getmeta,
125 .setmeta = npf_ifop_setmeta,
126 };
127
128 static int
129 npf_fini(void)
130 {
131 npf_t *npf = npf_getkernctx();
132
133 /* At first, detach device and remove pfil hooks. */
134 #ifdef _MODULE
135 devsw_detach(NULL, &npf_cdevsw);
136 #endif
137 npf_pfil_unregister(true);
138 npf_destroy(npf);
139 npf_sysfini();
140 return 0;
141 }
142
143 static int
144 npf_init(void)
145 {
146 npf_t *npf;
147 int error = 0;
148
149 error = npf_sysinit(nworkers);
150 if (error)
151 return error;
152 npf = npf_create(0, NULL, &kern_ifops);
153 npf_setkernctx(npf);
154 npf_pfil_register(true);
155
156 #ifdef _MODULE
157 devmajor_t bmajor = NODEVMAJOR, cmajor = NODEVMAJOR;
158
159 /* Attach /dev/npf device. */
160 error = devsw_attach("npf", NULL, &bmajor, &npf_cdevsw, &cmajor);
161 if (error) {
162 /* It will call devsw_detach(), which is safe. */
163 (void)npf_fini();
164 }
165 #endif
166 return error;
167 }
168
169
170 /*
171 * Module interface.
172 */
173 static int
174 npf_modcmd(modcmd_t cmd, void *arg)
175 {
176 switch (cmd) {
177 case MODULE_CMD_INIT:
178 return npf_init();
179 case MODULE_CMD_FINI:
180 return npf_fini();
181 case MODULE_CMD_AUTOUNLOAD:
182 if (npf_autounload_p()) {
183 return EBUSY;
184 }
185 break;
186 default:
187 return ENOTTY;
188 }
189 return 0;
190 }
191
192 void
193 npfattach(int nunits)
194 {
195 /* Nothing */
196 }
197
198 static int
199 npf_dev_open(dev_t dev, int flag, int mode, lwp_t *l)
200 {
201 /* Available only for super-user. */
202 if (kauth_authorize_network(l->l_cred, KAUTH_NETWORK_FIREWALL,
203 KAUTH_REQ_NETWORK_FIREWALL_FW, NULL, NULL, NULL)) {
204 return EPERM;
205 }
206 return 0;
207 }
208
209 static int
210 npf_dev_close(dev_t dev, int flag, int mode, lwp_t *l)
211 {
212 return 0;
213 }
214
215 static int
216 npf_stats_export(npf_t *npf, void *data)
217 {
218 uint64_t *fullst, *uptr = *(uint64_t **)data;
219 int error;
220
221 fullst = kmem_alloc(NPF_STATS_SIZE, KM_SLEEP);
222 npf_stats(npf, fullst); /* will zero the buffer */
223 error = copyout(fullst, uptr, NPF_STATS_SIZE);
224 kmem_free(fullst, NPF_STATS_SIZE);
225 return error;
226 }
227
228 static int
229 npf_dev_ioctl(dev_t dev, u_long cmd, void *data, int flag, lwp_t *l)
230 {
231 npf_t *npf = npf_getkernctx();
232 int error;
233
234 /* Available only for super-user. */
235 if (kauth_authorize_network(l->l_cred, KAUTH_NETWORK_FIREWALL,
236 KAUTH_REQ_NETWORK_FIREWALL_FW, NULL, NULL, NULL)) {
237 return EPERM;
238 }
239
240 switch (cmd) {
241 case IOC_NPF_TABLE:
242 error = npfctl_table(npf, data);
243 break;
244 case IOC_NPF_RULE:
245 error = npfctl_rule(npf, cmd, data);
246 break;
247 case IOC_NPF_STATS:
248 error = npf_stats_export(npf, data);
249 break;
250 case IOC_NPF_SAVE:
251 error = npfctl_save(npf, cmd, data);
252 break;
253 case IOC_NPF_SWITCH:
254 error = npfctl_switch(data);
255 break;
256 case IOC_NPF_LOAD:
257 error = npfctl_load(npf, cmd, data);
258 break;
259 case IOC_NPF_CONN_LOOKUP:
260 error = npfctl_conn_lookup(npf, cmd, data);
261 break;
262 case IOC_NPF_VERSION:
263 *(int *)data = NPF_VERSION;
264 error = 0;
265 break;
266 default:
267 error = ENOTTY;
268 break;
269 }
270 return error;
271 }
272
273 static int
274 npf_dev_poll(dev_t dev, int events, lwp_t *l)
275 {
276 return ENOTSUP;
277 }
278
279 static int
280 npf_dev_read(dev_t dev, struct uio *uio, int flag)
281 {
282 return ENOTSUP;
283 }
284
285 bool
286 npf_autounload_p(void)
287 {
288 npf_t *npf = npf_getkernctx();
289 return !npf_pfil_registered_p() && npf_default_pass(npf);
290 }
291
292 /*
293 * Interface operations.
294 */
295
296 static const char *
297 npf_ifop_getname(ifnet_t *ifp)
298 {
299 return ifp->if_xname;
300 }
301
302 static ifnet_t *
303 npf_ifop_lookup(const char *name)
304 {
305 return ifunit(name);
306 }
307
308 static void
309 npf_ifop_flush(void *arg)
310 {
311 ifnet_t *ifp;
312
313 KERNEL_LOCK(1, NULL);
314 IFNET_GLOBAL_LOCK();
315 IFNET_WRITER_FOREACH(ifp) {
316 ifp->if_npf_private = arg;
317 }
318 IFNET_GLOBAL_UNLOCK();
319 KERNEL_UNLOCK_ONE(NULL);
320 }
321
322 static void *
323 npf_ifop_getmeta(const ifnet_t *ifp)
324 {
325 return ifp->if_npf_private;
326 }
327
328 static void
329 npf_ifop_setmeta(ifnet_t *ifp, void *arg)
330 {
331 ifp->if_npf_private = arg;
332 }
333
334 #ifdef _KERNEL
335
336 /*
337 * Wrapper of the main packet handler to pass the kernel NPF context.
338 */
339 static int
340 npfkern_packet_handler(void *arg, struct mbuf **mp, ifnet_t *ifp, int di)
341 {
342 npf_t *npf = npf_getkernctx();
343 return npf_packet_handler(npf, mp, ifp, di);
344 }
345
346 /*
347 * npf_ifhook: hook handling interface changes.
348 */
349 static void
350 npf_ifhook(void *arg, unsigned long cmd, void *arg2)
351 {
352 npf_t *npf = npf_getkernctx();
353 ifnet_t *ifp = arg2;
354
355 switch (cmd) {
356 case PFIL_IFNET_ATTACH:
357 npf_ifmap_attach(npf, ifp);
358 npf_ifaddr_sync(npf, ifp);
359 break;
360 case PFIL_IFNET_DETACH:
361 npf_ifmap_detach(npf, ifp);
362 npf_ifaddr_flush(npf, ifp);
363 break;
364 }
365 }
366
367 static void
368 npf_ifaddrhook(void *arg, u_long cmd, void *arg2)
369 {
370 npf_t *npf = npf_getkernctx();
371 struct ifaddr *ifa = arg2;
372
373 switch (cmd) {
374 case SIOCSIFADDR:
375 case SIOCAIFADDR:
376 case SIOCDIFADDR:
377 #ifdef INET6
378 case SIOCSIFADDR_IN6:
379 case SIOCAIFADDR_IN6:
380 case SIOCDIFADDR_IN6:
381 #endif
382 KASSERT(ifa != NULL);
383 break;
384 default:
385 return;
386 }
387 npf_ifaddr_sync(npf, ifa->ifa_ifp);
388 }
389
390 /*
391 * npf_pfil_register: register pfil(9) hooks.
392 */
393 int
394 npf_pfil_register(bool init)
395 {
396 npf_t *npf = npf_getkernctx();
397 int error = 0;
398
399 SOFTNET_KERNEL_LOCK_UNLESS_NET_MPSAFE();
400
401 /* Init: interface re-config and attach/detach hook. */
402 if (!npf_ph_if) {
403 npf_ph_if = pfil_head_get(PFIL_TYPE_IFNET, 0);
404 if (!npf_ph_if) {
405 error = ENOENT;
406 goto out;
407 }
408
409 error = pfil_add_ihook(npf_ifhook, NULL,
410 PFIL_IFNET, npf_ph_if);
411 KASSERT(error == 0);
412
413 error = pfil_add_ihook(npf_ifaddrhook, NULL,
414 PFIL_IFADDR, npf_ph_if);
415 KASSERT(error == 0);
416 }
417 if (init) {
418 goto out;
419 }
420
421 /* Check if pfil hooks are not already registered. */
422 if (pfil_registered) {
423 error = EEXIST;
424 goto out;
425 }
426
427 /* Capture points of the activity in the IP layer. */
428 npf_ph_inet = pfil_head_get(PFIL_TYPE_AF, (void *)AF_INET);
429 npf_ph_inet6 = pfil_head_get(PFIL_TYPE_AF, (void *)AF_INET6);
430 if (!npf_ph_inet && !npf_ph_inet6) {
431 error = ENOENT;
432 goto out;
433 }
434
435 /* Packet IN/OUT handlers for IP layer. */
436 if (npf_ph_inet) {
437 error = pfil_add_hook(npfkern_packet_handler, npf,
438 PFIL_ALL, npf_ph_inet);
439 KASSERT(error == 0);
440 }
441 if (npf_ph_inet6) {
442 error = pfil_add_hook(npfkern_packet_handler, npf,
443 PFIL_ALL, npf_ph_inet6);
444 KASSERT(error == 0);
445 }
446
447 /*
448 * It is necessary to re-sync all/any interface address tables,
449 * since we did not listen for any changes.
450 */
451 npf_ifaddr_syncall(npf);
452 pfil_registered = true;
453 out:
454 SOFTNET_KERNEL_UNLOCK_UNLESS_NET_MPSAFE();
455
456 return error;
457 }
458
459 /*
460 * npf_pfil_unregister: unregister pfil(9) hooks.
461 */
462 void
463 npf_pfil_unregister(bool fini)
464 {
465 npf_t *npf = npf_getkernctx();
466
467 SOFTNET_KERNEL_LOCK_UNLESS_NET_MPSAFE();
468
469 if (fini && npf_ph_if) {
470 (void)pfil_remove_ihook(npf_ifhook, NULL,
471 PFIL_IFNET, npf_ph_if);
472 (void)pfil_remove_ihook(npf_ifaddrhook, NULL,
473 PFIL_IFADDR, npf_ph_if);
474 }
475 if (npf_ph_inet) {
476 (void)pfil_remove_hook(npfkern_packet_handler, npf,
477 PFIL_ALL, npf_ph_inet);
478 }
479 if (npf_ph_inet6) {
480 (void)pfil_remove_hook(npfkern_packet_handler, npf,
481 PFIL_ALL, npf_ph_inet6);
482 }
483 pfil_registered = false;
484
485 SOFTNET_KERNEL_UNLOCK_UNLESS_NET_MPSAFE();
486 }
487
488 bool
489 npf_pfil_registered_p(void)
490 {
491 return pfil_registered;
492 }
493 #endif
494