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