1134ea224SSam Leffler /* $NetBSD: pfil.h,v 1.22 2003/06/23 12:57:08 martin Exp $ */ 2e9c7bebfSDarren Reed 3c398230bSWarner Losh /*- 4fe267a55SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause 5fe267a55SPedro F. Giffuni * 6b252313fSGleb Smirnoff * Copyright (c) 2019 Gleb Smirnoff <glebius@FreeBSD.org> 7e9c7bebfSDarren Reed * Copyright (c) 1996 Matthew R. Green 8e9c7bebfSDarren Reed * All rights reserved. 9e9c7bebfSDarren Reed * 10e9c7bebfSDarren Reed * Redistribution and use in source and binary forms, with or without 11e9c7bebfSDarren Reed * modification, are permitted provided that the following conditions 12e9c7bebfSDarren Reed * are met: 13e9c7bebfSDarren Reed * 1. Redistributions of source code must retain the above copyright 14e9c7bebfSDarren Reed * notice, this list of conditions and the following disclaimer. 15e9c7bebfSDarren Reed * 2. Redistributions in binary form must reproduce the above copyright 16e9c7bebfSDarren Reed * notice, this list of conditions and the following disclaimer in the 17e9c7bebfSDarren Reed * documentation and/or other materials provided with the distribution. 18e9c7bebfSDarren Reed * 3. The name of the author may not be used to endorse or promote products 19e9c7bebfSDarren Reed * derived from this software without specific prior written permission. 20e9c7bebfSDarren Reed * 21e9c7bebfSDarren Reed * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 22e9c7bebfSDarren Reed * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 23e9c7bebfSDarren Reed * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 24e9c7bebfSDarren Reed * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 25e9c7bebfSDarren Reed * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 26e9c7bebfSDarren Reed * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 27e9c7bebfSDarren Reed * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 28e9c7bebfSDarren Reed * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 29e9c7bebfSDarren Reed * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30e9c7bebfSDarren Reed * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31e9c7bebfSDarren Reed * SUCH DAMAGE. 32e9c7bebfSDarren Reed */ 33e9c7bebfSDarren Reed 34e9c7bebfSDarren Reed #ifndef _NET_PFIL_H_ 35e9c7bebfSDarren Reed #define _NET_PFIL_H_ 36e9c7bebfSDarren Reed 37b252313fSGleb Smirnoff #include <sys/ioccom.h> 38e9c7bebfSDarren Reed 39b252313fSGleb Smirnoff enum pfil_types { 40b252313fSGleb Smirnoff PFIL_TYPE_IP4, 41b252313fSGleb Smirnoff PFIL_TYPE_IP6, 42b252313fSGleb Smirnoff PFIL_TYPE_ETHERNET, 43b252313fSGleb Smirnoff }; 44b252313fSGleb Smirnoff 45b252313fSGleb Smirnoff #define MAXPFILNAME 64 46b252313fSGleb Smirnoff 47b252313fSGleb Smirnoff struct pfilioc_head { 48b252313fSGleb Smirnoff char pio_name[MAXPFILNAME]; 49b252313fSGleb Smirnoff int pio_nhooksin; 50b252313fSGleb Smirnoff int pio_nhooksout; 51b252313fSGleb Smirnoff enum pfil_types pio_type; 52b252313fSGleb Smirnoff }; 53b252313fSGleb Smirnoff 54b252313fSGleb Smirnoff struct pfilioc_hook { 55b252313fSGleb Smirnoff char pio_module[MAXPFILNAME]; 56b252313fSGleb Smirnoff char pio_ruleset[MAXPFILNAME]; 57b252313fSGleb Smirnoff int pio_flags; 58b252313fSGleb Smirnoff enum pfil_types pio_type; 59b252313fSGleb Smirnoff }; 60b252313fSGleb Smirnoff 61b252313fSGleb Smirnoff struct pfilioc_list { 62b252313fSGleb Smirnoff u_int pio_nheads; 63b252313fSGleb Smirnoff u_int pio_nhooks; 64b252313fSGleb Smirnoff struct pfilioc_head *pio_heads; 65b252313fSGleb Smirnoff struct pfilioc_hook *pio_hooks; 66b252313fSGleb Smirnoff }; 67b252313fSGleb Smirnoff 68b252313fSGleb Smirnoff struct pfilioc_link { 69b252313fSGleb Smirnoff char pio_name[MAXPFILNAME]; 70b252313fSGleb Smirnoff char pio_module[MAXPFILNAME]; 71b252313fSGleb Smirnoff char pio_ruleset[MAXPFILNAME]; 72b252313fSGleb Smirnoff int pio_flags; 73b252313fSGleb Smirnoff }; 74b252313fSGleb Smirnoff 75b252313fSGleb Smirnoff #define PFILDEV "pfil" 76b252313fSGleb Smirnoff #define PFILIOC_LISTHEADS _IOWR('P', 1, struct pfilioc_list) 77b252313fSGleb Smirnoff #define PFILIOC_LISTHOOKS _IOWR('P', 2, struct pfilioc_list) 78b252313fSGleb Smirnoff #define PFILIOC_LINK _IOW('P', 3, struct pfilioc_link) 79b252313fSGleb Smirnoff 80b252313fSGleb Smirnoff #define PFIL_IN 0x00010000 81b252313fSGleb Smirnoff #define PFIL_OUT 0x00020000 82*b52b61c0SKristof Provost #define PFIL_FWD 0x00040000 83b252313fSGleb Smirnoff #define PFIL_DIR(f) ((f) & (PFIL_IN|PFIL_OUT)) 84b252313fSGleb Smirnoff #define PFIL_HEADPTR 0x00100000 85b252313fSGleb Smirnoff #define PFIL_HOOKPTR 0x00200000 86b252313fSGleb Smirnoff #define PFIL_APPEND 0x00400000 87b252313fSGleb Smirnoff #define PFIL_UNLINK 0x00800000 88b252313fSGleb Smirnoff 89b252313fSGleb Smirnoff #ifdef _KERNEL 90e9c7bebfSDarren Reed struct mbuf; 91e9c7bebfSDarren Reed struct ifnet; 92d6a8d588SMax Laier struct inpcb; 93e9c7bebfSDarren Reed 94b252313fSGleb Smirnoff typedef enum { 95b252313fSGleb Smirnoff PFIL_PASS = 0, 96b252313fSGleb Smirnoff PFIL_DROPPED, 97b252313fSGleb Smirnoff PFIL_CONSUMED, 98b252313fSGleb Smirnoff PFIL_REALLOCED, 99b252313fSGleb Smirnoff } pfil_return_t; 100b252313fSGleb Smirnoff 101caf32b26SGleb Smirnoff typedef pfil_return_t (*pfil_mbuf_chk_t)(struct mbuf **, struct ifnet *, int, 102b252313fSGleb Smirnoff void *, struct inpcb *); 103caf32b26SGleb Smirnoff typedef pfil_return_t (*pfil_mem_chk_t)(void *, u_int, int, struct ifnet *, 104caf32b26SGleb Smirnoff void *, struct mbuf **); 105caf32b26SGleb Smirnoff 106b252313fSGleb Smirnoff /* 107b252313fSGleb Smirnoff * A pfil head is created by a packet intercept point. 108b252313fSGleb Smirnoff * 109b252313fSGleb Smirnoff * A pfil hook is created by a packet filter. 110b252313fSGleb Smirnoff * 111b252313fSGleb Smirnoff * Hooks are chained on heads. Historically some hooking happens 112b252313fSGleb Smirnoff * automatically, e.g. ipfw(4), pf(4) and ipfilter(4) would register 113b252313fSGleb Smirnoff * theirselves on IPv4 and IPv6 input/output. 114b252313fSGleb Smirnoff */ 115b252313fSGleb Smirnoff 116b252313fSGleb Smirnoff typedef struct pfil_hook * pfil_hook_t; 117b252313fSGleb Smirnoff typedef struct pfil_head * pfil_head_t; 11825da5060SAndre Oppermann 119e9c7bebfSDarren Reed /* 120b252313fSGleb Smirnoff * Give us a chance to modify pfil_xxx_args structures in future. 121e9c7bebfSDarren Reed */ 122caf32b26SGleb Smirnoff #define PFIL_VERSION 2 123e9c7bebfSDarren Reed 124b252313fSGleb Smirnoff /* Argument structure used by packet filters to register themselves. */ 125b252313fSGleb Smirnoff struct pfil_hook_args { 126b252313fSGleb Smirnoff int pa_version; 127b252313fSGleb Smirnoff int pa_flags; 128b252313fSGleb Smirnoff enum pfil_types pa_type; 129caf32b26SGleb Smirnoff pfil_mbuf_chk_t pa_mbuf_chk; 130caf32b26SGleb Smirnoff pfil_mem_chk_t pa_mem_chk; 131b252313fSGleb Smirnoff void *pa_ruleset; 132b252313fSGleb Smirnoff const char *pa_modname; 133b252313fSGleb Smirnoff const char *pa_rulname; 134f1eafd6dSRuslan Ermilov }; 135e9c7bebfSDarren Reed 1368da01399SAndre Oppermann /* Public functions for pfil hook management by packet filters. */ 137b252313fSGleb Smirnoff pfil_hook_t pfil_add_hook(struct pfil_hook_args *); 138b252313fSGleb Smirnoff void pfil_remove_hook(pfil_hook_t); 1398da01399SAndre Oppermann 140b252313fSGleb Smirnoff /* Argument structure used by ioctl() and packet filters to set filters. */ 141b252313fSGleb Smirnoff struct pfil_link_args { 142b252313fSGleb Smirnoff int pa_version; 143b252313fSGleb Smirnoff int pa_flags; 144b252313fSGleb Smirnoff union { 145b252313fSGleb Smirnoff const char *pa_headname; 146b252313fSGleb Smirnoff pfil_head_t pa_head; 147b252313fSGleb Smirnoff }; 148b252313fSGleb Smirnoff union { 149b252313fSGleb Smirnoff struct { 150b252313fSGleb Smirnoff const char *pa_modname; 151b252313fSGleb Smirnoff const char *pa_rulname; 152b252313fSGleb Smirnoff }; 153b252313fSGleb Smirnoff pfil_hook_t pa_hook; 154b252313fSGleb Smirnoff }; 155b252313fSGleb Smirnoff }; 156e9c7bebfSDarren Reed 157b252313fSGleb Smirnoff /* Public function to configure filter chains. Used by ioctl() and filters. */ 158b252313fSGleb Smirnoff int pfil_link(struct pfil_link_args *); 1598da01399SAndre Oppermann 160b252313fSGleb Smirnoff /* Argument structure used by inspection points to register themselves. */ 161b252313fSGleb Smirnoff struct pfil_head_args { 162b252313fSGleb Smirnoff int pa_version; 163b252313fSGleb Smirnoff int pa_flags; 164b252313fSGleb Smirnoff enum pfil_types pa_type; 165b252313fSGleb Smirnoff const char *pa_headname; 166b252313fSGleb Smirnoff }; 1674dab1a18SAlexander V. Chernikov 168b252313fSGleb Smirnoff /* Public functions for pfil head management by inspection points. */ 169b252313fSGleb Smirnoff pfil_head_t pfil_head_register(struct pfil_head_args *); 170b252313fSGleb Smirnoff void pfil_head_unregister(pfil_head_t); 171b252313fSGleb Smirnoff 172b252313fSGleb Smirnoff /* Public functions to run the packet inspection by inspection points. */ 173caf32b26SGleb Smirnoff int pfil_mem_in(struct pfil_head *, void *, u_int, struct ifnet *, 174caf32b26SGleb Smirnoff struct mbuf **); 175caf32b26SGleb Smirnoff int pfil_mem_out(struct pfil_head *, void *, u_int, struct ifnet *, 176caf32b26SGleb Smirnoff struct mbuf **); 177caf32b26SGleb Smirnoff int pfil_mbuf_in(struct pfil_head *, struct mbuf **, struct ifnet *, 178b252313fSGleb Smirnoff struct inpcb *inp); 179caf32b26SGleb Smirnoff int pfil_mbuf_out(struct pfil_head *, struct mbuf **, struct ifnet *, 1800b70e3e7SMateusz Guzik struct inpcb *inp); 181*b52b61c0SKristof Provost int pfil_mbuf_fwd(struct pfil_head *, struct mbuf **, struct ifnet *, 182*b52b61c0SKristof Provost struct inpcb *); 183caf32b26SGleb Smirnoff 184b252313fSGleb Smirnoff /* 185b252313fSGleb Smirnoff * Minimally exposed structure to avoid function call in case of absence 186b252313fSGleb Smirnoff * of any filters by protocols and macros to do the check. 187b252313fSGleb Smirnoff */ 188b252313fSGleb Smirnoff struct _pfil_head { 189b252313fSGleb Smirnoff int head_nhooksin; 190b252313fSGleb Smirnoff int head_nhooksout; 191b252313fSGleb Smirnoff }; 192b252313fSGleb Smirnoff #define PFIL_HOOKED_IN(p) (((struct _pfil_head *)(p))->head_nhooksin > 0) 193b252313fSGleb Smirnoff #define PFIL_HOOKED_OUT(p) (((struct _pfil_head *)(p))->head_nhooksout > 0) 194b252313fSGleb Smirnoff 195b252313fSGleb Smirnoff #endif /* _KERNEL */ 196e9c7bebfSDarren Reed #endif /* _NET_PFIL_H_ */ 197