1 /* $FreeBSD$ */
2 /* $NetBSD: pfil.c,v 1.20 2001/11/12 23:49:46 lukem Exp $ */
3
4 /*-
5 * SPDX-License-Identifier: BSD-3-Clause
6 *
7 * Copyright (c) 2019 Gleb Smirnoff <[email protected]>
8 * Copyright (c) 1996 Matthew R. Green
9 * All rights reserved.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. The name of the author may not be used to endorse or promote products
20 * derived from this software without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 */
34
35 #include <sys/param.h>
36 #include <sys/conf.h>
37 #include <sys/kernel.h>
38 #include <sys/epoch.h>
39 #include <sys/errno.h>
40 #include <sys/lock.h>
41 #include <sys/malloc.h>
42 #include <sys/socket.h>
43 #include <sys/socketvar.h>
44 #include <sys/systm.h>
45 #include <sys/lock.h>
46 #include <sys/mutex.h>
47 #include <sys/proc.h>
48 #include <sys/queue.h>
49 #include <sys/ucred.h>
50 #include <sys/jail.h>
51
52 #include <net/if.h>
53 #include <net/if_var.h>
54 #include <net/pfil.h>
55
56 static MALLOC_DEFINE(M_PFIL, "pfil", "pfil(9) packet filter hooks");
57
58 static int pfil_ioctl(struct cdev *, u_long, caddr_t, int, struct thread *);
59 static struct cdevsw pfil_cdevsw = {
60 .d_ioctl = pfil_ioctl,
61 .d_name = PFILDEV,
62 .d_version = D_VERSION,
63 };
64 static struct cdev *pfil_dev;
65
66 static struct mtx pfil_lock;
67 MTX_SYSINIT(pfil_mtxinit, &pfil_lock, "pfil(9) lock", MTX_DEF);
68 #define PFIL_LOCK() mtx_lock(&pfil_lock)
69 #define PFIL_UNLOCK() mtx_unlock(&pfil_lock)
70 #define PFIL_LOCK_ASSERT() mtx_assert(&pfil_lock, MA_OWNED)
71
72 struct pfil_hook {
73 pfil_func_t hook_func;
74 void *hook_ruleset;
75 int hook_flags;
76 int hook_links;
77 enum pfil_types hook_type;
78 const char *hook_modname;
79 const char *hook_rulname;
80 LIST_ENTRY(pfil_hook) hook_list;
81 };
82
83 struct pfil_link {
84 CK_STAILQ_ENTRY(pfil_link) link_chain;
85 pfil_func_t link_func;
86 void *link_ruleset;
87 int link_flags;
88 struct pfil_hook *link_hook;
89 struct epoch_context link_epoch_ctx;
90 };
91
92 typedef CK_STAILQ_HEAD(pfil_chain, pfil_link) pfil_chain_t;
93 struct pfil_head {
94 int head_nhooksin;
95 int head_nhooksout;
96 pfil_chain_t head_in;
97 pfil_chain_t head_out;
98 int head_flags;
99 enum pfil_types head_type;
100 LIST_ENTRY(pfil_head) head_list;
101 const char *head_name;
102 };
103
104 LIST_HEAD(pfilheadhead, pfil_head);
105 VNET_DEFINE_STATIC(struct pfilheadhead, pfil_head_list) =
106 LIST_HEAD_INITIALIZER(pfil_head_list);
107 #define V_pfil_head_list VNET(pfil_head_list)
108
109 LIST_HEAD(pfilhookhead, pfil_hook);
110 VNET_DEFINE_STATIC(struct pfilhookhead, pfil_hook_list) =
111 LIST_HEAD_INITIALIZER(pfil_hook_list);
112 #define V_pfil_hook_list VNET(pfil_hook_list)
113
114 static struct pfil_link *pfil_link_remove(pfil_chain_t *, pfil_hook_t );
115 static void pfil_link_free(epoch_context_t);
116
117 int
pfil_realloc(pfil_packet_t * p,int flags,struct ifnet * ifp)118 pfil_realloc(pfil_packet_t *p, int flags, struct ifnet *ifp)
119 {
120 struct mbuf *m;
121
122 MPASS(flags & PFIL_MEMPTR);
123
124 if ((m = m_devget(p->mem, PFIL_LENGTH(flags), 0, ifp, NULL)) == NULL)
125 return (ENOMEM);
126 *p = pfil_packet_align(*p);
127 *p->m = m;
128
129 return (0);
130 }
131
132 static __noinline int
pfil_fake_mbuf(pfil_func_t func,pfil_packet_t * p,struct ifnet * ifp,int flags,void * ruleset,struct inpcb * inp)133 pfil_fake_mbuf(pfil_func_t func, pfil_packet_t *p, struct ifnet *ifp, int flags,
134 void *ruleset, struct inpcb *inp)
135 {
136 struct mbuf m, *mp;
137 pfil_return_t rv;
138
139 (void)m_init(&m, M_NOWAIT, MT_DATA, M_NOFREE | M_PKTHDR);
140 m_extadd(&m, p->mem, PFIL_LENGTH(flags), NULL, NULL, NULL, 0,
141 EXT_RXRING);
142 m.m_len = m.m_pkthdr.len = PFIL_LENGTH(flags);
143 mp = &m;
144 flags &= ~(PFIL_MEMPTR | PFIL_LENMASK);
145
146 rv = func(&mp, ifp, flags, ruleset, inp);
147 if (rv == PFIL_PASS && mp != &m) {
148 /*
149 * Firewalls that need pfil_fake_mbuf() most likely don't
150 * know they need return PFIL_REALLOCED.
151 */
152 rv = PFIL_REALLOCED;
153 *p = pfil_packet_align(*p);
154 *p->m = mp;
155 }
156
157 return (rv);
158 }
159
160 /*
161 * pfil_run_hooks() runs the specified packet filter hook chain.
162 */
163 int
pfil_run_hooks(struct pfil_head * head,pfil_packet_t p,struct ifnet * ifp,int flags,struct inpcb * inp)164 pfil_run_hooks(struct pfil_head *head, pfil_packet_t p, struct ifnet *ifp,
165 int flags, struct inpcb *inp)
166 {
167 pfil_chain_t *pch;
168 struct pfil_link *link;
169 pfil_return_t rv;
170 bool realloc = false;
171
172 NET_EPOCH_ASSERT();
173
174 if (PFIL_DIR(flags) == PFIL_IN)
175 pch = &head->head_in;
176 else if (__predict_true(PFIL_DIR(flags) == PFIL_OUT))
177 pch = &head->head_out;
178 else
179 panic("%s: bogus flags %d", __func__, flags);
180
181 rv = PFIL_PASS;
182 CK_STAILQ_FOREACH(link, pch, link_chain) {
183 if ((flags & PFIL_MEMPTR) && !(link->link_flags & PFIL_MEMPTR))
184 rv = pfil_fake_mbuf(link->link_func, &p, ifp, flags,
185 link->link_ruleset, inp);
186 else
187 rv = (*link->link_func)(p, ifp, flags,
188 link->link_ruleset, inp);
189 if (rv == PFIL_DROPPED || rv == PFIL_CONSUMED)
190 break;
191 else if (rv == PFIL_REALLOCED) {
192 flags &= ~(PFIL_MEMPTR | PFIL_LENMASK);
193 realloc = true;
194 }
195 }
196 if (realloc && rv == PFIL_PASS)
197 rv = PFIL_REALLOCED;
198 return (rv);
199 }
200
201 /*
202 * pfil_head_register() registers a pfil_head with the packet filter hook
203 * mechanism.
204 */
205 pfil_head_t
pfil_head_register(struct pfil_head_args * pa)206 pfil_head_register(struct pfil_head_args *pa)
207 {
208 struct pfil_head *head, *list;
209
210 MPASS(pa->pa_version == PFIL_VERSION);
211
212 head = malloc(sizeof(struct pfil_head), M_PFIL, M_WAITOK);
213
214 head->head_nhooksin = head->head_nhooksout = 0;
215 head->head_flags = pa->pa_flags;
216 head->head_type = pa->pa_type;
217 head->head_name = pa->pa_headname;
218 CK_STAILQ_INIT(&head->head_in);
219 CK_STAILQ_INIT(&head->head_out);
220
221 PFIL_LOCK();
222 LIST_FOREACH(list, &V_pfil_head_list, head_list)
223 if (strcmp(pa->pa_headname, list->head_name) == 0) {
224 printf("pfil: duplicate head \"%s\"\n",
225 pa->pa_headname);
226 }
227 LIST_INSERT_HEAD(&V_pfil_head_list, head, head_list);
228 PFIL_UNLOCK();
229
230 return (head);
231 }
232
233 /*
234 * pfil_head_unregister() removes a pfil_head from the packet filter hook
235 * mechanism. The producer of the hook promises that all outstanding
236 * invocations of the hook have completed before it unregisters the hook.
237 */
238 void
pfil_head_unregister(pfil_head_t ph)239 pfil_head_unregister(pfil_head_t ph)
240 {
241 struct pfil_link *link, *next;
242
243 PFIL_LOCK();
244 LIST_REMOVE(ph, head_list);
245
246 CK_STAILQ_FOREACH_SAFE(link, &ph->head_in, link_chain, next) {
247 link->link_hook->hook_links--;
248 free(link, M_PFIL);
249 }
250 CK_STAILQ_FOREACH_SAFE(link, &ph->head_out, link_chain, next) {
251 link->link_hook->hook_links--;
252 free(link, M_PFIL);
253 }
254 PFIL_UNLOCK();
255 }
256
257 pfil_hook_t
pfil_add_hook(struct pfil_hook_args * pa)258 pfil_add_hook(struct pfil_hook_args *pa)
259 {
260 struct pfil_hook *hook, *list;
261
262 MPASS(pa->pa_version == PFIL_VERSION);
263
264 hook = malloc(sizeof(struct pfil_hook), M_PFIL, M_WAITOK | M_ZERO);
265 hook->hook_func = pa->pa_func;
266 hook->hook_ruleset = pa->pa_ruleset;
267 hook->hook_flags = pa->pa_flags;
268 hook->hook_type = pa->pa_type;
269 hook->hook_modname = pa->pa_modname;
270 hook->hook_rulname = pa->pa_rulname;
271
272 PFIL_LOCK();
273 LIST_FOREACH(list, &V_pfil_hook_list, hook_list)
274 if (strcmp(pa->pa_modname, list->hook_modname) == 0 &&
275 strcmp(pa->pa_rulname, list->hook_rulname) == 0) {
276 printf("pfil: duplicate hook \"%s:%s\"\n",
277 pa->pa_modname, pa->pa_rulname);
278 }
279 LIST_INSERT_HEAD(&V_pfil_hook_list, hook, hook_list);
280 PFIL_UNLOCK();
281
282 return (hook);
283 }
284
285 static int
pfil_unlink(struct pfil_link_args * pa,pfil_head_t head,pfil_hook_t hook)286 pfil_unlink(struct pfil_link_args *pa, pfil_head_t head, pfil_hook_t hook)
287 {
288 struct pfil_link *in, *out;
289
290 PFIL_LOCK_ASSERT();
291
292 if (pa->pa_flags & PFIL_IN) {
293 in = pfil_link_remove(&head->head_in, hook);
294 if (in != NULL) {
295 head->head_nhooksin--;
296 hook->hook_links--;
297 }
298 } else
299 in = NULL;
300 if (pa->pa_flags & PFIL_OUT) {
301 out = pfil_link_remove(&head->head_out, hook);
302 if (out != NULL) {
303 head->head_nhooksout--;
304 hook->hook_links--;
305 }
306 } else
307 out = NULL;
308 PFIL_UNLOCK();
309
310 if (in != NULL)
311 NET_EPOCH_CALL(pfil_link_free, &in->link_epoch_ctx);
312 if (out != NULL)
313 NET_EPOCH_CALL(pfil_link_free, &out->link_epoch_ctx);
314
315 if (in == NULL && out == NULL)
316 return (ENOENT);
317 else
318 return (0);
319 }
320
321 int
pfil_link(struct pfil_link_args * pa)322 pfil_link(struct pfil_link_args *pa)
323 {
324 struct pfil_link *in, *out, *link;
325 struct pfil_head *head;
326 struct pfil_hook *hook;
327 int error;
328
329 MPASS(pa->pa_version == PFIL_VERSION);
330
331 if ((pa->pa_flags & (PFIL_IN | PFIL_UNLINK)) == PFIL_IN)
332 in = malloc(sizeof(*in), M_PFIL, M_WAITOK | M_ZERO);
333 else
334 in = NULL;
335 if ((pa->pa_flags & (PFIL_OUT | PFIL_UNLINK)) == PFIL_OUT)
336 out = malloc(sizeof(*out), M_PFIL, M_WAITOK | M_ZERO);
337 else
338 out = NULL;
339
340 PFIL_LOCK();
341 if (pa->pa_flags & PFIL_HEADPTR)
342 head = pa->pa_head;
343 else
344 LIST_FOREACH(head, &V_pfil_head_list, head_list)
345 if (strcmp(pa->pa_headname, head->head_name) == 0)
346 break;
347 if (pa->pa_flags & PFIL_HOOKPTR)
348 hook = pa->pa_hook;
349 else
350 LIST_FOREACH(hook, &V_pfil_hook_list, hook_list)
351 if (strcmp(pa->pa_modname, hook->hook_modname) == 0 &&
352 strcmp(pa->pa_rulname, hook->hook_rulname) == 0)
353 break;
354 if (head == NULL || hook == NULL) {
355 error = ENOENT;
356 goto fail;
357 }
358
359 if (pa->pa_flags & PFIL_UNLINK)
360 return (pfil_unlink(pa, head, hook));
361
362 if (head->head_type != hook->hook_type ||
363 ((hook->hook_flags & pa->pa_flags) & ~head->head_flags)) {
364 error = EINVAL;
365 goto fail;
366 }
367
368 if (pa->pa_flags & PFIL_IN)
369 CK_STAILQ_FOREACH(link, &head->head_in, link_chain)
370 if (link->link_hook == hook) {
371 error = EEXIST;
372 goto fail;
373 }
374 if (pa->pa_flags & PFIL_OUT)
375 CK_STAILQ_FOREACH(link, &head->head_out, link_chain)
376 if (link->link_hook == hook) {
377 error = EEXIST;
378 goto fail;
379 }
380
381 if (pa->pa_flags & PFIL_IN) {
382 in->link_hook = hook;
383 in->link_func = hook->hook_func;
384 in->link_flags = hook->hook_flags;
385 in->link_ruleset = hook->hook_ruleset;
386 if (pa->pa_flags & PFIL_APPEND)
387 CK_STAILQ_INSERT_TAIL(&head->head_in, in, link_chain);
388 else
389 CK_STAILQ_INSERT_HEAD(&head->head_in, in, link_chain);
390 hook->hook_links++;
391 head->head_nhooksin++;
392 }
393 if (pa->pa_flags & PFIL_OUT) {
394 out->link_hook = hook;
395 out->link_func = hook->hook_func;
396 out->link_flags = hook->hook_flags;
397 out->link_ruleset = hook->hook_ruleset;
398 if (pa->pa_flags & PFIL_APPEND)
399 CK_STAILQ_INSERT_HEAD(&head->head_out, out, link_chain);
400 else
401 CK_STAILQ_INSERT_TAIL(&head->head_out, out, link_chain);
402 hook->hook_links++;
403 head->head_nhooksout++;
404 }
405 PFIL_UNLOCK();
406
407 return (0);
408
409 fail:
410 PFIL_UNLOCK();
411 free(in, M_PFIL);
412 free(out, M_PFIL);
413 return (error);
414 }
415
416 static void
pfil_link_free(epoch_context_t ctx)417 pfil_link_free(epoch_context_t ctx)
418 {
419 struct pfil_link *link;
420
421 link = __containerof(ctx, struct pfil_link, link_epoch_ctx);
422 free(link, M_PFIL);
423 }
424
425 /*
426 * pfil_remove_hook removes a filter from all filtering points.
427 */
428 void
pfil_remove_hook(pfil_hook_t hook)429 pfil_remove_hook(pfil_hook_t hook)
430 {
431 struct pfil_head *head;
432 struct pfil_link *in, *out;
433
434 PFIL_LOCK();
435 LIST_FOREACH(head, &V_pfil_head_list, head_list) {
436 retry:
437 in = pfil_link_remove(&head->head_in, hook);
438 if (in != NULL) {
439 head->head_nhooksin--;
440 hook->hook_links--;
441 NET_EPOCH_CALL(pfil_link_free, &in->link_epoch_ctx);
442 }
443 out = pfil_link_remove(&head->head_out, hook);
444 if (out != NULL) {
445 head->head_nhooksout--;
446 hook->hook_links--;
447 NET_EPOCH_CALL(pfil_link_free, &out->link_epoch_ctx);
448 }
449 if (in != NULL || out != NULL)
450 /* What if some stupid admin put same filter twice? */
451 goto retry;
452 }
453 LIST_REMOVE(hook, hook_list);
454 PFIL_UNLOCK();
455 MPASS(hook->hook_links == 0);
456 free(hook, M_PFIL);
457 }
458
459 /*
460 * Internal: Remove a pfil hook from a hook chain.
461 */
462 static struct pfil_link *
pfil_link_remove(pfil_chain_t * chain,pfil_hook_t hook)463 pfil_link_remove(pfil_chain_t *chain, pfil_hook_t hook)
464 {
465 struct pfil_link *link;
466
467 PFIL_LOCK_ASSERT();
468
469 CK_STAILQ_FOREACH(link, chain, link_chain)
470 if (link->link_hook == hook) {
471 CK_STAILQ_REMOVE(chain, link, pfil_link, link_chain);
472 return (link);
473 }
474
475 return (NULL);
476 }
477
478 #ifndef FSTACK
479 static void
pfil_init(const void * unused __unused)480 pfil_init(const void *unused __unused)
481 {
482 struct make_dev_args args;
483 int error;
484
485 make_dev_args_init(&args);
486 args.mda_flags = MAKEDEV_WAITOK | MAKEDEV_CHECKNAME;
487 args.mda_devsw = &pfil_cdevsw;
488 args.mda_uid = UID_ROOT;
489 args.mda_gid = GID_WHEEL;
490 args.mda_mode = 0600;
491 error = make_dev_s(&args, &pfil_dev, PFILDEV);
492 KASSERT(error == 0, ("%s: failed to create dev: %d", __func__, error));
493 }
494 /*
495 * Make sure the pfil bits are first before any possible subsystem which
496 * might piggyback on the SI_SUB_PROTO_PFIL.
497 */
498 SYSINIT(pfil_init, SI_SUB_PROTO_PFIL, SI_ORDER_FIRST, pfil_init, NULL);
499 #endif
500
501 /*
502 * User control interface.
503 */
504 static int pfilioc_listheads(struct pfilioc_list *);
505 static int pfilioc_listhooks(struct pfilioc_list *);
506 static int pfilioc_link(struct pfilioc_link *);
507
508 static int
pfil_ioctl(struct cdev * dev,u_long cmd,caddr_t addr,int flags,struct thread * td)509 pfil_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flags,
510 struct thread *td)
511 {
512 int error;
513
514 CURVNET_SET(TD_TO_VNET(td));
515 error = 0;
516 switch (cmd) {
517 case PFILIOC_LISTHEADS:
518 error = pfilioc_listheads((struct pfilioc_list *)addr);
519 break;
520 case PFILIOC_LISTHOOKS:
521 error = pfilioc_listhooks((struct pfilioc_list *)addr);
522 break;
523 case PFILIOC_LINK:
524 error = pfilioc_link((struct pfilioc_link *)addr);
525 break;
526 default:
527 error = EINVAL;
528 break;
529 }
530 CURVNET_RESTORE();
531 return (error);
532 }
533
534 static int
pfilioc_listheads(struct pfilioc_list * req)535 pfilioc_listheads(struct pfilioc_list *req)
536 {
537 struct pfil_head *head;
538 struct pfil_link *link;
539 struct pfilioc_head *iohead;
540 struct pfilioc_hook *iohook;
541 u_int nheads, nhooks, hd, hk;
542 int error;
543
544 PFIL_LOCK();
545 restart:
546 nheads = nhooks = 0;
547 LIST_FOREACH(head, &V_pfil_head_list, head_list) {
548 nheads++;
549 nhooks += head->head_nhooksin + head->head_nhooksout;
550 }
551 PFIL_UNLOCK();
552
553 if (req->pio_nheads < nheads || req->pio_nhooks < nhooks) {
554 req->pio_nheads = nheads;
555 req->pio_nhooks = nhooks;
556 return (0);
557 }
558
559 iohead = malloc(sizeof(*iohead) * nheads, M_TEMP, M_WAITOK);
560 iohook = malloc(sizeof(*iohook) * nhooks, M_TEMP, M_WAITOK);
561
562 hd = hk = 0;
563 PFIL_LOCK();
564 LIST_FOREACH(head, &V_pfil_head_list, head_list) {
565 if (hd + 1 > nheads ||
566 hk + head->head_nhooksin + head->head_nhooksout > nhooks) {
567 /* Configuration changed during malloc(). */
568 free(iohead, M_TEMP);
569 free(iohook, M_TEMP);
570 goto restart;
571 }
572 strlcpy(iohead[hd].pio_name, head->head_name,
573 sizeof(iohead[0].pio_name));
574 iohead[hd].pio_nhooksin = head->head_nhooksin;
575 iohead[hd].pio_nhooksout = head->head_nhooksout;
576 iohead[hd].pio_type = head->head_type;
577 CK_STAILQ_FOREACH(link, &head->head_in, link_chain) {
578 strlcpy(iohook[hk].pio_module,
579 link->link_hook->hook_modname,
580 sizeof(iohook[0].pio_module));
581 strlcpy(iohook[hk].pio_ruleset,
582 link->link_hook->hook_rulname,
583 sizeof(iohook[0].pio_ruleset));
584 hk++;
585 }
586 CK_STAILQ_FOREACH(link, &head->head_out, link_chain) {
587 strlcpy(iohook[hk].pio_module,
588 link->link_hook->hook_modname,
589 sizeof(iohook[0].pio_module));
590 strlcpy(iohook[hk].pio_ruleset,
591 link->link_hook->hook_rulname,
592 sizeof(iohook[0].pio_ruleset));
593 hk++;
594 }
595 hd++;
596 }
597 PFIL_UNLOCK();
598
599 error = copyout(iohead, req->pio_heads,
600 sizeof(*iohead) * min(hd, req->pio_nheads));
601 if (error == 0)
602 error = copyout(iohook, req->pio_hooks,
603 sizeof(*iohook) * min(req->pio_nhooks, hk));
604
605 req->pio_nheads = hd;
606 req->pio_nhooks = hk;
607
608 free(iohead, M_TEMP);
609 free(iohook, M_TEMP);
610
611 return (error);
612 }
613
614 static int
pfilioc_listhooks(struct pfilioc_list * req)615 pfilioc_listhooks(struct pfilioc_list *req)
616 {
617 struct pfil_hook *hook;
618 struct pfilioc_hook *iohook;
619 u_int nhooks, hk;
620 int error;
621
622 PFIL_LOCK();
623 restart:
624 nhooks = 0;
625 LIST_FOREACH(hook, &V_pfil_hook_list, hook_list)
626 nhooks++;
627 PFIL_UNLOCK();
628
629 if (req->pio_nhooks < nhooks) {
630 req->pio_nhooks = nhooks;
631 return (0);
632 }
633
634 iohook = malloc(sizeof(*iohook) * nhooks, M_TEMP, M_WAITOK);
635
636 hk = 0;
637 PFIL_LOCK();
638 LIST_FOREACH(hook, &V_pfil_hook_list, hook_list) {
639 if (hk + 1 > nhooks) {
640 /* Configuration changed during malloc(). */
641 free(iohook, M_TEMP);
642 goto restart;
643 }
644 strlcpy(iohook[hk].pio_module, hook->hook_modname,
645 sizeof(iohook[0].pio_module));
646 strlcpy(iohook[hk].pio_ruleset, hook->hook_rulname,
647 sizeof(iohook[0].pio_ruleset));
648 iohook[hk].pio_type = hook->hook_type;
649 iohook[hk].pio_flags = hook->hook_flags;
650 hk++;
651 }
652 PFIL_UNLOCK();
653
654 error = copyout(iohook, req->pio_hooks,
655 sizeof(*iohook) * min(req->pio_nhooks, hk));
656 req->pio_nhooks = hk;
657 free(iohook, M_TEMP);
658
659 return (error);
660 }
661
662 static int
pfilioc_link(struct pfilioc_link * req)663 pfilioc_link(struct pfilioc_link *req)
664 {
665 struct pfil_link_args args;
666
667 if (req->pio_flags & ~(PFIL_IN | PFIL_OUT | PFIL_UNLINK | PFIL_APPEND))
668 return (EINVAL);
669
670 args.pa_version = PFIL_VERSION;
671 args.pa_flags = req->pio_flags;
672 args.pa_headname = req->pio_name;
673 args.pa_modname = req->pio_module;
674 args.pa_rulname = req->pio_ruleset;
675
676 return (pfil_link(&args));
677 }
678