- Use {TAILQ,LIST}_FOREACH
[dragonfly.git] / sys / net / pfil.c
CommitLineData
9e99d0a2 1/* $NetBSD: pfil.c,v 1.20 2001/11/12 23:49:46 lukem Exp $ */
94171836 2/* $DragonFly: src/sys/net/pfil.c,v 1.7 2008/09/14 04:11:10 sephe Exp $ */
9e99d0a2
JR
3
4/*
5 * Copyright (c) 1996 Matthew R. Green
6 * All rights reserved.
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 * 3. The name of the author may not be used to endorse or promote products
17 * derived from this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
24 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
26 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32#include <sys/param.h>
33#include <sys/errno.h>
34#include <sys/malloc.h>
35#include <sys/socket.h>
36#include <sys/socketvar.h>
37#include <sys/systm.h>
38#include <sys/proc.h>
39#include <sys/queue.h>
40
41#include <net/if.h>
42#include <net/pfil.h>
43
afabe90c 44static int pfil_list_add(struct pfil_head *,
9e99d0a2
JR
45 int (*)(void *, struct mbuf **, struct ifnet *, int), void *, int);
46
afabe90c
MD
47static int pfil_list_remove(struct pfil_head *,
48 int (*)(void *, struct mbuf **, struct ifnet *, int), void *, int);
9e99d0a2
JR
49
50LIST_HEAD(, pfil_head) pfil_head_list =
51 LIST_HEAD_INITIALIZER(&pfil_head_list);
52
03b72fbe
SZ
53static __inline struct packet_filter_hook *
54pfil_hook_get(int dir, struct pfil_head *ph)
55{
56
57 if (dir == PFIL_IN)
58 return (TAILQ_FIRST(&ph->ph_in));
59 else if (dir == PFIL_OUT)
60 return (TAILQ_FIRST(&ph->ph_out));
61 else
62 return (NULL);
63}
64
9e99d0a2
JR
65/*
66 * pfil_run_hooks() runs the specified packet filter hooks.
67 */
68int
69pfil_run_hooks(struct pfil_head *ph, struct mbuf **mp, struct ifnet *ifp,
70 int dir)
71{
72 struct packet_filter_hook *pfh;
73 struct mbuf *m = *mp;
74 int rv = 0;
75
76 for (pfh = pfil_hook_get(dir, ph); pfh != NULL;
77 pfh = TAILQ_NEXT(pfh, pfil_link)) {
78 if (pfh->pfil_func != NULL) {
79 rv = (*pfh->pfil_func)(pfh->pfil_arg, &m, ifp, dir);
80 if (rv != 0 || m == NULL)
81 break;
82 }
83 }
84
85 *mp = m;
86 return (rv);
87}
88
89/*
90 * pfil_head_register() registers a pfil_head with the packet filter
91 * hook mechanism.
92 */
93int
94pfil_head_register(struct pfil_head *ph)
95{
96 struct pfil_head *lph;
97
94171836 98 LIST_FOREACH(lph, &pfil_head_list, ph_list) {
9e99d0a2
JR
99 if (ph->ph_type == lph->ph_type &&
100 ph->ph_un.phu_val == lph->ph_un.phu_val)
101 return EEXIST;
102 }
103
104 TAILQ_INIT(&ph->ph_in);
105 TAILQ_INIT(&ph->ph_out);
afabe90c 106 ph->ph_hashooks = 0;
9e99d0a2
JR
107
108 LIST_INSERT_HEAD(&pfil_head_list, ph, ph_list);
109
110 return (0);
111}
112
113/*
114 * pfil_head_unregister() removes a pfil_head from the packet filter
115 * hook mechanism.
116 */
117int
118pfil_head_unregister(struct pfil_head *pfh)
119{
9e99d0a2
JR
120 LIST_REMOVE(pfh, ph_list);
121 return (0);
122}
123
124/*
125 * pfil_head_get() returns the pfil_head for a given key/dlt.
126 */
127struct pfil_head *
128pfil_head_get(int type, u_long val)
129{
130 struct pfil_head *ph;
131
94171836
SZ
132 LIST_FOREACH(ph, &pfil_head_list, ph_list) {
133 if (ph->ph_type == type && ph->ph_un.phu_val == val)
9e99d0a2
JR
134 break;
135 }
9e99d0a2
JR
136 return (ph);
137}
138
139/*
140 * pfil_add_hook() adds a function to the packet filter hook. the
141 * flags are:
142 * PFIL_IN call me on incoming packets
143 * PFIL_OUT call me on outgoing packets
144 * PFIL_ALL call me on all of the above
702cc4db 145 * PFIL_WAITOK OK to call kmalloc with M_WAITOK.
9e99d0a2
JR
146 */
147int
148pfil_add_hook(int (*func)(void *, struct mbuf **, struct ifnet *, int),
149 void *arg, int flags, struct pfil_head *ph)
150{
151 int err = 0;
152
153 if (flags & PFIL_IN) {
afabe90c 154 err = pfil_list_add(ph, func, arg, flags & ~PFIL_OUT);
9e99d0a2
JR
155 if (err)
156 return err;
157 }
158 if (flags & PFIL_OUT) {
afabe90c 159 err = pfil_list_add(ph, func, arg, flags & ~PFIL_IN);
9e99d0a2
JR
160 if (err) {
161 if (flags & PFIL_IN)
afabe90c 162 pfil_list_remove(ph, func, arg, PFIL_IN);
9e99d0a2
JR
163 return err;
164 }
165 }
166 return 0;
167}
168
169static int
afabe90c 170pfil_list_add(struct pfil_head *ph,
9e99d0a2
JR
171 int (*func)(void *, struct mbuf **, struct ifnet *, int), void *arg,
172 int flags)
173{
174 struct packet_filter_hook *pfh;
afabe90c
MD
175 pfil_list_t *list;
176
177 list = (flags & PFIL_IN) ? &ph->ph_in : &ph->ph_out;
9e99d0a2
JR
178
179 /*
180 * First make sure the hook is not already there.
181 */
94171836
SZ
182 TAILQ_FOREACH(pfh, list, pfil_link) {
183 if (pfh->pfil_func == func && pfh->pfil_arg == arg)
9e99d0a2
JR
184 return EEXIST;
185 }
186
efda3bd0 187 pfh = (struct packet_filter_hook *)kmalloc(sizeof(*pfh), M_IFADDR,
9e99d0a2
JR
188 (flags & PFIL_WAITOK) ? M_WAITOK : M_NOWAIT);
189 if (pfh == NULL)
190 return ENOMEM;
191
192 pfh->pfil_func = func;
193 pfh->pfil_arg = arg;
194
195 /*
196 * insert the input list in reverse order of the output list
197 * so that the same path is followed in or out of the kernel.
198 */
199 if (flags & PFIL_IN)
200 TAILQ_INSERT_HEAD(list, pfh, pfil_link);
201 else
202 TAILQ_INSERT_TAIL(list, pfh, pfil_link);
afabe90c
MD
203 ph->ph_hashooks = 1;
204 return (0);
9e99d0a2
JR
205}
206
207/*
208 * pfil_remove_hook removes a specific function from the packet filter
209 * hook list.
210 */
211int
212pfil_remove_hook(int (*func)(void *, struct mbuf **, struct ifnet *, int),
213 void *arg, int flags, struct pfil_head *ph)
214{
215 int err = 0;
216
217 if (flags & PFIL_IN)
afabe90c 218 err = pfil_list_remove(ph, func, arg, PFIL_IN);
9e99d0a2 219 if ((err == 0) && (flags & PFIL_OUT))
afabe90c 220 err = pfil_list_remove(ph, func, arg, PFIL_OUT);
9e99d0a2
JR
221 return err;
222}
223
224/*
225 * pfil_list_remove is an internal function that takes a function off the
afabe90c 226 * specified list. Clear ph_hashooks if no functions remain on any list.
9e99d0a2
JR
227 */
228static int
afabe90c
MD
229pfil_list_remove(struct pfil_head *ph,
230 int (*func)(void *, struct mbuf **, struct ifnet *, int), void *arg,
231 int flags)
9e99d0a2
JR
232{
233 struct packet_filter_hook *pfh;
afabe90c
MD
234 pfil_list_t *list;
235
236 list = (flags & PFIL_IN) ? &ph->ph_in : &ph->ph_out;
9e99d0a2 237
94171836 238 TAILQ_FOREACH(pfh, list, pfil_link) {
9e99d0a2
JR
239 if (pfh->pfil_func == func && pfh->pfil_arg == arg) {
240 TAILQ_REMOVE(list, pfh, pfil_link);
efda3bd0 241 kfree(pfh, M_IFADDR);
afabe90c
MD
242 if (TAILQ_EMPTY(&ph->ph_in) && TAILQ_EMPTY(&ph->ph_out))
243 ph->ph_hashooks = 0;
9e99d0a2
JR
244 return 0;
245 }
246 }
247 return ENOENT;
248}