Put IPV6_RTHDR_TYPE_0 define back until the world fixes itself.
[dragonfly.git] / sys / net / pfil.c
CommitLineData
9e99d0a2 1/* $NetBSD: pfil.c,v 1.20 2001/11/12 23:49:46 lukem Exp $ */
3bc01464 2/* $DragonFly: src/sys/net/pfil.c,v 1.9 2008/09/14 05:22:44 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
3bc01464
SZ
44static int pfil_list_add(struct pfil_head *, pfil_func_t, void *, int);
45static int pfil_list_remove(struct pfil_head *, pfil_func_t, void *, int);
9e99d0a2
JR
46
47LIST_HEAD(, pfil_head) pfil_head_list =
48 LIST_HEAD_INITIALIZER(&pfil_head_list);
49
50/*
51 * pfil_run_hooks() runs the specified packet filter hooks.
52 */
53int
54pfil_run_hooks(struct pfil_head *ph, struct mbuf **mp, struct ifnet *ifp,
55 int dir)
56{
57 struct packet_filter_hook *pfh;
58 struct mbuf *m = *mp;
7f8565be 59 pfil_list_t *list;
9e99d0a2
JR
60 int rv = 0;
61
7f8565be
SZ
62 if (dir == PFIL_IN)
63 list = &ph->ph_in;
64 else if (dir == PFIL_OUT)
65 list = &ph->ph_out;
66 else
67 return 0; /* XXX panic? */
68
69 TAILQ_FOREACH(pfh, list, pfil_link) {
9e99d0a2
JR
70 if (pfh->pfil_func != NULL) {
71 rv = (*pfh->pfil_func)(pfh->pfil_arg, &m, ifp, dir);
72 if (rv != 0 || m == NULL)
73 break;
74 }
75 }
76
77 *mp = m;
78 return (rv);
79}
80
81/*
82 * pfil_head_register() registers a pfil_head with the packet filter
83 * hook mechanism.
84 */
85int
86pfil_head_register(struct pfil_head *ph)
87{
88 struct pfil_head *lph;
89
94171836 90 LIST_FOREACH(lph, &pfil_head_list, ph_list) {
9e99d0a2
JR
91 if (ph->ph_type == lph->ph_type &&
92 ph->ph_un.phu_val == lph->ph_un.phu_val)
93 return EEXIST;
94 }
95
96 TAILQ_INIT(&ph->ph_in);
97 TAILQ_INIT(&ph->ph_out);
afabe90c 98 ph->ph_hashooks = 0;
9e99d0a2
JR
99
100 LIST_INSERT_HEAD(&pfil_head_list, ph, ph_list);
101
102 return (0);
103}
104
105/*
106 * pfil_head_unregister() removes a pfil_head from the packet filter
107 * hook mechanism.
108 */
109int
110pfil_head_unregister(struct pfil_head *pfh)
111{
9e99d0a2
JR
112 LIST_REMOVE(pfh, ph_list);
113 return (0);
114}
115
116/*
117 * pfil_head_get() returns the pfil_head for a given key/dlt.
118 */
119struct pfil_head *
120pfil_head_get(int type, u_long val)
121{
122 struct pfil_head *ph;
123
94171836
SZ
124 LIST_FOREACH(ph, &pfil_head_list, ph_list) {
125 if (ph->ph_type == type && ph->ph_un.phu_val == val)
9e99d0a2
JR
126 break;
127 }
9e99d0a2
JR
128 return (ph);
129}
130
131/*
132 * pfil_add_hook() adds a function to the packet filter hook. the
133 * flags are:
134 * PFIL_IN call me on incoming packets
135 * PFIL_OUT call me on outgoing packets
136 * PFIL_ALL call me on all of the above
702cc4db 137 * PFIL_WAITOK OK to call kmalloc with M_WAITOK.
9e99d0a2
JR
138 */
139int
3bc01464 140pfil_add_hook(pfil_func_t func, void *arg, int flags, struct pfil_head *ph)
9e99d0a2
JR
141{
142 int err = 0;
143
144 if (flags & PFIL_IN) {
afabe90c 145 err = pfil_list_add(ph, func, arg, flags & ~PFIL_OUT);
9e99d0a2
JR
146 if (err)
147 return err;
148 }
149 if (flags & PFIL_OUT) {
afabe90c 150 err = pfil_list_add(ph, func, arg, flags & ~PFIL_IN);
9e99d0a2
JR
151 if (err) {
152 if (flags & PFIL_IN)
afabe90c 153 pfil_list_remove(ph, func, arg, PFIL_IN);
9e99d0a2
JR
154 return err;
155 }
156 }
157 return 0;
158}
159
160static int
3bc01464 161pfil_list_add(struct pfil_head *ph, pfil_func_t func, void *arg, int flags)
9e99d0a2
JR
162{
163 struct packet_filter_hook *pfh;
afabe90c
MD
164 pfil_list_t *list;
165
166 list = (flags & PFIL_IN) ? &ph->ph_in : &ph->ph_out;
9e99d0a2
JR
167
168 /*
169 * First make sure the hook is not already there.
170 */
94171836
SZ
171 TAILQ_FOREACH(pfh, list, pfil_link) {
172 if (pfh->pfil_func == func && pfh->pfil_arg == arg)
9e99d0a2
JR
173 return EEXIST;
174 }
175
efda3bd0 176 pfh = (struct packet_filter_hook *)kmalloc(sizeof(*pfh), M_IFADDR,
9e99d0a2
JR
177 (flags & PFIL_WAITOK) ? M_WAITOK : M_NOWAIT);
178 if (pfh == NULL)
179 return ENOMEM;
180
181 pfh->pfil_func = func;
182 pfh->pfil_arg = arg;
183
184 /*
185 * insert the input list in reverse order of the output list
186 * so that the same path is followed in or out of the kernel.
187 */
188 if (flags & PFIL_IN)
189 TAILQ_INSERT_HEAD(list, pfh, pfil_link);
190 else
191 TAILQ_INSERT_TAIL(list, pfh, pfil_link);
afabe90c
MD
192 ph->ph_hashooks = 1;
193 return (0);
9e99d0a2
JR
194}
195
196/*
197 * pfil_remove_hook removes a specific function from the packet filter
198 * hook list.
199 */
200int
3bc01464 201pfil_remove_hook(pfil_func_t func, void *arg, int flags, struct pfil_head *ph)
9e99d0a2
JR
202{
203 int err = 0;
204
205 if (flags & PFIL_IN)
afabe90c 206 err = pfil_list_remove(ph, func, arg, PFIL_IN);
9e99d0a2 207 if ((err == 0) && (flags & PFIL_OUT))
afabe90c 208 err = pfil_list_remove(ph, func, arg, PFIL_OUT);
9e99d0a2
JR
209 return err;
210}
211
212/*
213 * pfil_list_remove is an internal function that takes a function off the
afabe90c 214 * specified list. Clear ph_hashooks if no functions remain on any list.
9e99d0a2
JR
215 */
216static int
3bc01464 217pfil_list_remove(struct pfil_head *ph, pfil_func_t func, void *arg, int flags)
9e99d0a2
JR
218{
219 struct packet_filter_hook *pfh;
afabe90c
MD
220 pfil_list_t *list;
221
222 list = (flags & PFIL_IN) ? &ph->ph_in : &ph->ph_out;
9e99d0a2 223
94171836 224 TAILQ_FOREACH(pfh, list, pfil_link) {
9e99d0a2
JR
225 if (pfh->pfil_func == func && pfh->pfil_arg == arg) {
226 TAILQ_REMOVE(list, pfh, pfil_link);
efda3bd0 227 kfree(pfh, M_IFADDR);
afabe90c
MD
228 if (TAILQ_EMPTY(&ph->ph_in) && TAILQ_EMPTY(&ph->ph_out))
229 ph->ph_hashooks = 0;
9e99d0a2
JR
230 return 0;
231 }
232 }
233 return ENOENT;
234}