Home | History | Annotate | Line # | Download | only in net
pfil.c revision 1.24.76.1
      1 /*	$NetBSD: pfil.c,v 1.24.76.1 2008/06/23 04:31:58 wrstuden Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1996 Matthew R. Green
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     21  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     23  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     24  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26  * SUCH DAMAGE.
     27  */
     28 
     29 #include <sys/cdefs.h>
     30 __KERNEL_RCSID(0, "$NetBSD: pfil.c,v 1.24.76.1 2008/06/23 04:31:58 wrstuden Exp $");
     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 = NULL;
     62 	int rv = 0;
     63 
     64 	if ((dir & PFIL_ALL) && mp)
     65 		m = *mp;
     66 	for (pfh = pfil_hook_get(dir, ph); pfh != NULL;
     67 	     pfh = TAILQ_NEXT(pfh, pfil_link)) {
     68 		if (pfh->pfil_func != NULL) {
     69 			if (pfh->pfil_flags & PFIL_ALL) {
     70 				rv = (*pfh->pfil_func)(pfh->pfil_arg, &m, ifp,
     71 				    dir);
     72 				if (rv != 0 || m == NULL)
     73 					break;
     74 			} else {
     75 				rv = (*pfh->pfil_func)(pfh->pfil_arg, mp, ifp,
     76 				    dir);
     77 				if (rv != 0)
     78 					break;
     79 			}
     80 		}
     81 	}
     82 
     83 	if ((dir & PFIL_ALL) && mp)
     84 		*mp = m;
     85 	return (rv);
     86 }
     87 
     88 /*
     89  * pfil_head_register() registers a pfil_head with the packet filter
     90  * hook mechanism.
     91  */
     92 int
     93 pfil_head_register(struct pfil_head *ph)
     94 {
     95 	struct pfil_head *lph;
     96 
     97 	for (lph = LIST_FIRST(&pfil_head_list); lph != NULL;
     98 	     lph = LIST_NEXT(lph, ph_list)) {
     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);
    106 	TAILQ_INIT(&ph->ph_ifaddr);
    107 	TAILQ_INIT(&ph->ph_ifnetevent);
    108 
    109 	LIST_INSERT_HEAD(&pfil_head_list, ph, ph_list);
    110 
    111 	return (0);
    112 }
    113 
    114 /*
    115  * pfil_head_unregister() removes a pfil_head from the packet filter
    116  * hook mechanism.
    117  */
    118 int
    119 pfil_head_unregister(struct pfil_head *pfh)
    120 {
    121 
    122 	LIST_REMOVE(pfh, ph_list);
    123 	return (0);
    124 }
    125 
    126 /*
    127  * pfil_head_get() returns the pfil_head for a given key/dlt.
    128  */
    129 struct pfil_head *
    130 pfil_head_get(int type, u_long val)
    131 {
    132 	struct pfil_head *ph;
    133 
    134 	for (ph = LIST_FIRST(&pfil_head_list); ph != NULL;
    135 	     ph = LIST_NEXT(ph, ph_list)) {
    136 		if (ph->ph_type == type &&
    137 		    ph->ph_un.phu_val == val)
    138 			break;
    139 	}
    140 
    141 	return (ph);
    142 }
    143 
    144 /*
    145  * pfil_add_hook() adds a function to the packet filter hook.  the
    146  * flags are:
    147  *	PFIL_IN		call me on incoming packets
    148  *	PFIL_OUT	call me on outgoing packets
    149  *	PFIL_ALL	call me on all of the above
    150  *	PFIL_IFADDR  	call me on interface reconfig (mbuf ** is ioctl #)
    151  *	PFIL_IFNET  	call me on interface attach/detach
    152  *			(mbuf ** is PFIL_IFNET_*)
    153  *	PFIL_WAITOK	OK to call malloc with M_WAITOK.
    154  */
    155 int
    156 pfil_add_hook(int (*func)(void *, struct mbuf **, struct ifnet *, int),
    157     void *arg, int flags, struct pfil_head *ph)
    158 {
    159 	int err = 0;
    160 
    161 	if (flags & PFIL_IN) {
    162 		err = pfil_list_add(&ph->ph_in, func, arg, flags & ~PFIL_OUT);
    163 		if (err)
    164 			return err;
    165 	}
    166 	if (flags & PFIL_OUT) {
    167 		err = pfil_list_add(&ph->ph_out, func, arg, flags & ~PFIL_IN);
    168 		if (err) {
    169 			if (flags & PFIL_IN)
    170 				pfil_list_remove(&ph->ph_in, func, arg);
    171 			return err;
    172 		}
    173 	}
    174 	if (flags & PFIL_IFADDR) {
    175 		err = pfil_list_add(&ph->ph_ifaddr, func, arg, flags);
    176 		if (err) {
    177 			if (flags & PFIL_IN)
    178 				pfil_list_remove(&ph->ph_in, func, arg);
    179 			if (flags & PFIL_OUT)
    180 				pfil_list_remove(&ph->ph_out, func, arg);
    181 			return err;
    182 		}
    183 	}
    184 	if (flags & PFIL_IFNET) {
    185 		err = pfil_list_add(&ph->ph_ifnetevent, func, arg, flags);
    186 		if (err) {
    187 			if (flags & PFIL_IN)
    188 				pfil_list_remove(&ph->ph_in, func, arg);
    189 			if (flags & PFIL_OUT)
    190 				pfil_list_remove(&ph->ph_out, func, arg);
    191 			if (flags & PFIL_IFADDR)
    192 				pfil_list_remove(&ph->ph_ifaddr, func, arg);
    193 			return err;
    194 		}
    195 	}
    196 	return 0;
    197 }
    198 
    199 static int
    200 pfil_list_add(pfil_list_t *list,
    201     int (*func)(void *, struct mbuf **, struct ifnet *, int), void *arg,
    202     int flags)
    203 {
    204 	struct packet_filter_hook *pfh;
    205 
    206 	/*
    207 	 * First make sure the hook is not already there.
    208 	 */
    209 	for (pfh = TAILQ_FIRST(list); pfh != NULL;
    210 	     pfh = TAILQ_NEXT(pfh, pfil_link)) {
    211 		if (pfh->pfil_func == func &&
    212 		    pfh->pfil_arg == arg)
    213 			return EEXIST;
    214 	}
    215 
    216 	pfh = (struct packet_filter_hook *)malloc(sizeof(*pfh), M_IFADDR,
    217 	    (flags & PFIL_WAITOK) ? M_WAITOK : M_NOWAIT);
    218 	if (pfh == NULL)
    219 		return ENOMEM;
    220 
    221 	pfh->pfil_func = func;
    222 	pfh->pfil_arg  = arg;
    223 	pfh->pfil_flags = flags;
    224 
    225 	/*
    226 	 * insert the input list in reverse order of the output list
    227 	 * so that the same path is followed in or out of the kernel.
    228 	 */
    229 	if (flags & PFIL_IN)
    230 		TAILQ_INSERT_HEAD(list, pfh, pfil_link);
    231 	else
    232 		TAILQ_INSERT_TAIL(list, pfh, pfil_link);
    233 
    234 	return 0;
    235 }
    236 
    237 /*
    238  * pfil_remove_hook removes a specific function from the packet filter
    239  * hook list.
    240  */
    241 int
    242 pfil_remove_hook(int (*func)(void *, struct mbuf **, struct ifnet *, int),
    243     void *arg, int flags, struct pfil_head *ph)
    244 {
    245 	int err = 0;
    246 
    247 	if (flags & PFIL_IN)
    248 		err = pfil_list_remove(&ph->ph_in, func, arg);
    249 	if ((err == 0) && (flags & PFIL_OUT))
    250 		err = pfil_list_remove(&ph->ph_out, func, arg);
    251 	if ((err == 0) && (flags & PFIL_IFADDR))
    252 		err = pfil_list_remove(&ph->ph_ifaddr, func, arg);
    253 	if ((err == 0) && (flags & PFIL_IFNET))
    254 		err = pfil_list_remove(&ph->ph_ifnetevent, func, arg);
    255 	return err;
    256 }
    257 
    258 /*
    259  * pfil_list_remove is an internal function that takes a function off the
    260  * specified list.
    261  */
    262 static int
    263 pfil_list_remove(pfil_list_t *list,
    264     int (*func)(void *, struct mbuf **, struct ifnet *, int), void *arg)
    265 {
    266 	struct packet_filter_hook *pfh;
    267 
    268 	for (pfh = TAILQ_FIRST(list); pfh != NULL;
    269 	     pfh = TAILQ_NEXT(pfh, pfil_link)) {
    270 		if (pfh->pfil_func == func && pfh->pfil_arg == arg) {
    271 			TAILQ_REMOVE(list, pfh, pfil_link);
    272 			free(pfh, M_IFADDR);
    273 			return 0;
    274 		}
    275 	}
    276 	return ENOENT;
    277 }
    278