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