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