Home | History | Annotate | Line # | Download | only in net
pfil.c revision 1.28.12.2
      1  1.28.12.2  pgoyette /*	$NetBSD: pfil.c,v 1.28.12.2 2017/03/20 06:57:50 pgoyette Exp $	*/
      2        1.1       mrg 
      3        1.1       mrg /*
      4       1.28     rmind  * Copyright (c) 2013 Mindaugas Rasiukevicius <rmind at NetBSD org>
      5        1.1       mrg  * Copyright (c) 1996 Matthew R. Green
      6        1.1       mrg  * All rights reserved.
      7        1.1       mrg  *
      8        1.1       mrg  * Redistribution and use in source and binary forms, with or without
      9        1.1       mrg  * modification, are permitted provided that the following conditions
     10        1.1       mrg  * are met:
     11        1.1       mrg  * 1. Redistributions of source code must retain the above copyright
     12        1.1       mrg  *    notice, this list of conditions and the following disclaimer.
     13        1.1       mrg  * 2. Redistributions in binary form must reproduce the above copyright
     14        1.1       mrg  *    notice, this list of conditions and the following disclaimer in the
     15        1.1       mrg  *    documentation and/or other materials provided with the distribution.
     16        1.1       mrg  *
     17        1.1       mrg  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     18        1.1       mrg  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     19        1.1       mrg  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     20        1.1       mrg  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     21        1.1       mrg  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     22        1.1       mrg  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     23        1.1       mrg  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     24        1.1       mrg  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     25        1.1       mrg  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     26        1.1       mrg  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     27        1.1       mrg  * SUCH DAMAGE.
     28        1.1       mrg  */
     29       1.20     lukem 
     30       1.20     lukem #include <sys/cdefs.h>
     31  1.28.12.2  pgoyette __KERNEL_RCSID(0, "$NetBSD: pfil.c,v 1.28.12.2 2017/03/20 06:57:50 pgoyette Exp $");
     32  1.28.12.2  pgoyette 
     33  1.28.12.2  pgoyette #if defined(_KERNEL_OPT)
     34  1.28.12.2  pgoyette #include "opt_net_mpsafe.h"
     35  1.28.12.2  pgoyette #endif
     36        1.1       mrg 
     37        1.1       mrg #include <sys/param.h>
     38        1.1       mrg #include <sys/systm.h>
     39        1.1       mrg #include <sys/queue.h>
     40       1.28     rmind #include <sys/kmem.h>
     41  1.28.12.2  pgoyette #include <sys/psref.h>
     42        1.1       mrg 
     43        1.1       mrg #include <net/if.h>
     44        1.1       mrg #include <net/pfil.h>
     45        1.1       mrg 
     46       1.28     rmind #define	MAX_HOOKS	8
     47       1.16   thorpej 
     48  1.28.12.1  pgoyette /* Func is either pfil_func_t or pfil_ifunc_t. */
     49  1.28.12.1  pgoyette typedef void		(*pfil_polyfunc_t)(void);
     50  1.28.12.1  pgoyette 
     51       1.28     rmind typedef struct {
     52  1.28.12.1  pgoyette 	pfil_polyfunc_t pfil_func;
     53       1.28     rmind 	void *		pfil_arg;
     54       1.28     rmind } pfil_hook_t;
     55       1.28     rmind 
     56       1.28     rmind typedef struct {
     57       1.28     rmind 	pfil_hook_t	hooks[MAX_HOOKS];
     58       1.28     rmind 	u_int		nhooks;
     59  1.28.12.2  pgoyette 	struct psref_target psref;
     60       1.28     rmind } pfil_list_t;
     61       1.28     rmind 
     62  1.28.12.2  pgoyette typedef struct {
     63  1.28.12.2  pgoyette 	pfil_list_t	*active;	/* lists[0] or lists[1] */
     64  1.28.12.2  pgoyette 	pfil_list_t	lists[2];
     65  1.28.12.2  pgoyette } pfil_listset_t;
     66  1.28.12.2  pgoyette 
     67  1.28.12.1  pgoyette CTASSERT(PFIL_IN == 1);
     68  1.28.12.1  pgoyette CTASSERT(PFIL_OUT == 2);
     69  1.28.12.1  pgoyette 
     70       1.28     rmind struct pfil_head {
     71  1.28.12.2  pgoyette 	pfil_listset_t	ph_in;
     72  1.28.12.2  pgoyette 	pfil_listset_t	ph_out;
     73  1.28.12.2  pgoyette 	pfil_listset_t	ph_ifaddr;
     74  1.28.12.2  pgoyette 	pfil_listset_t	ph_ifevent;
     75       1.28     rmind 	int		ph_type;
     76       1.28     rmind 	void *		ph_key;
     77       1.28     rmind 	LIST_ENTRY(pfil_head) ph_list;
     78       1.28     rmind };
     79       1.28     rmind 
     80       1.28     rmind static const int pfil_flag_cases[] = {
     81  1.28.12.2  pgoyette 	PFIL_IN, PFIL_OUT
     82       1.28     rmind };
     83       1.16   thorpej 
     84       1.28     rmind static LIST_HEAD(, pfil_head) pfil_head_list __read_mostly =
     85       1.16   thorpej     LIST_HEAD_INITIALIZER(&pfil_head_list);
     86       1.16   thorpej 
     87  1.28.12.2  pgoyette static kmutex_t pfil_mtx __cacheline_aligned;
     88  1.28.12.2  pgoyette static struct psref_class *pfil_psref_class __read_mostly;
     89  1.28.12.2  pgoyette static pserialize_t pfil_psz;
     90  1.28.12.2  pgoyette 
     91  1.28.12.2  pgoyette void
     92  1.28.12.2  pgoyette pfil_init(void)
     93  1.28.12.2  pgoyette {
     94  1.28.12.2  pgoyette 	mutex_init(&pfil_mtx, MUTEX_DEFAULT, IPL_NONE);
     95  1.28.12.2  pgoyette 	pfil_psz = pserialize_create();
     96  1.28.12.2  pgoyette 	pfil_psref_class = psref_class_create("pfil", IPL_SOFTNET);
     97  1.28.12.2  pgoyette }
     98  1.28.12.2  pgoyette 
     99  1.28.12.2  pgoyette static inline void
    100  1.28.12.2  pgoyette pfil_listset_init(pfil_listset_t *pflistset)
    101  1.28.12.2  pgoyette {
    102  1.28.12.2  pgoyette 	pflistset->active = &pflistset->lists[0];
    103  1.28.12.2  pgoyette 	psref_target_init(&pflistset->active->psref, pfil_psref_class);
    104  1.28.12.2  pgoyette }
    105  1.28.12.2  pgoyette 
    106       1.16   thorpej /*
    107       1.28     rmind  * pfil_head_create: create and register a packet filter head.
    108       1.16   thorpej  */
    109       1.28     rmind pfil_head_t *
    110       1.28     rmind pfil_head_create(int type, void *key)
    111       1.16   thorpej {
    112       1.28     rmind 	pfil_head_t *ph;
    113        1.1       mrg 
    114       1.28     rmind 	if (pfil_head_get(type, key)) {
    115       1.28     rmind 		return NULL;
    116       1.16   thorpej 	}
    117       1.28     rmind 	ph = kmem_zalloc(sizeof(pfil_head_t), KM_SLEEP);
    118       1.28     rmind 	ph->ph_type = type;
    119       1.28     rmind 	ph->ph_key = key;
    120       1.16   thorpej 
    121  1.28.12.2  pgoyette 	pfil_listset_init(&ph->ph_in);
    122  1.28.12.2  pgoyette 	pfil_listset_init(&ph->ph_out);
    123  1.28.12.2  pgoyette 	pfil_listset_init(&ph->ph_ifaddr);
    124  1.28.12.2  pgoyette 	pfil_listset_init(&ph->ph_ifevent);
    125  1.28.12.2  pgoyette 
    126       1.16   thorpej 	LIST_INSERT_HEAD(&pfil_head_list, ph, ph_list);
    127       1.28     rmind 	return ph;
    128       1.16   thorpej }
    129       1.16   thorpej 
    130       1.16   thorpej /*
    131       1.28     rmind  * pfil_head_destroy: remove and destroy a packet filter head.
    132       1.16   thorpej  */
    133       1.28     rmind void
    134       1.28     rmind pfil_head_destroy(pfil_head_t *pfh)
    135       1.16   thorpej {
    136       1.16   thorpej 	LIST_REMOVE(pfh, ph_list);
    137  1.28.12.2  pgoyette 
    138  1.28.12.2  pgoyette 	psref_target_destroy(&pfh->ph_in.active->psref, pfil_psref_class);
    139  1.28.12.2  pgoyette 	psref_target_destroy(&pfh->ph_out.active->psref, pfil_psref_class);
    140  1.28.12.2  pgoyette 	psref_target_destroy(&pfh->ph_ifaddr.active->psref, pfil_psref_class);
    141  1.28.12.2  pgoyette 	psref_target_destroy(&pfh->ph_ifevent.active->psref, pfil_psref_class);
    142  1.28.12.2  pgoyette 
    143       1.28     rmind 	kmem_free(pfh, sizeof(pfil_head_t));
    144       1.16   thorpej }
    145       1.16   thorpej 
    146       1.16   thorpej /*
    147       1.28     rmind  * pfil_head_get: returns the packer filter head for a given key.
    148       1.16   thorpej  */
    149       1.28     rmind pfil_head_t *
    150       1.28     rmind pfil_head_get(int type, void *key)
    151       1.16   thorpej {
    152       1.28     rmind 	pfil_head_t *ph;
    153       1.16   thorpej 
    154       1.27    dyoung 	LIST_FOREACH(ph, &pfil_head_list, ph_list) {
    155       1.28     rmind 		if (ph->ph_type == type && ph->ph_key == key)
    156       1.16   thorpej 			break;
    157       1.16   thorpej 	}
    158       1.28     rmind 	return ph;
    159        1.1       mrg }
    160        1.1       mrg 
    161  1.28.12.2  pgoyette static pfil_listset_t *
    162       1.28     rmind pfil_hook_get(int dir, pfil_head_t *ph)
    163        1.1       mrg {
    164       1.28     rmind 	switch (dir) {
    165       1.28     rmind 	case PFIL_IN:
    166       1.28     rmind 		return &ph->ph_in;
    167       1.28     rmind 	case PFIL_OUT:
    168       1.28     rmind 		return &ph->ph_out;
    169       1.28     rmind 	case PFIL_IFADDR:
    170       1.28     rmind 		return &ph->ph_ifaddr;
    171       1.28     rmind 	case PFIL_IFNET:
    172       1.28     rmind 		return &ph->ph_ifevent;
    173       1.21    itojun 	}
    174       1.28     rmind 	return NULL;
    175        1.1       mrg }
    176        1.1       mrg 
    177       1.13   darrenr static int
    178  1.28.12.2  pgoyette pfil_list_add(pfil_listset_t *phlistset, pfil_polyfunc_t func, void *arg,
    179  1.28.12.2  pgoyette               int flags)
    180        1.1       mrg {
    181  1.28.12.2  pgoyette 	u_int nhooks;
    182  1.28.12.2  pgoyette 	pfil_list_t *newlist, *oldlist;
    183       1.28     rmind 	pfil_hook_t *pfh;
    184        1.1       mrg 
    185  1.28.12.2  pgoyette 	mutex_enter(&pfil_mtx);
    186  1.28.12.2  pgoyette 
    187       1.28     rmind 	/* Check if we have a free slot. */
    188  1.28.12.2  pgoyette 	nhooks = phlistset->active->nhooks;
    189       1.28     rmind 	if (nhooks == MAX_HOOKS) {
    190  1.28.12.2  pgoyette 		mutex_exit(&pfil_mtx);
    191       1.28     rmind 		return ENOSPC;
    192       1.28     rmind 	}
    193       1.28     rmind 	KASSERT(nhooks < MAX_HOOKS);
    194       1.28     rmind 
    195  1.28.12.2  pgoyette 	if (phlistset->active == &phlistset->lists[0]) {
    196  1.28.12.2  pgoyette 		oldlist = &phlistset->lists[0];
    197  1.28.12.2  pgoyette 		newlist = &phlistset->lists[1];
    198  1.28.12.2  pgoyette 	} else{
    199  1.28.12.2  pgoyette 		oldlist = &phlistset->lists[1];
    200  1.28.12.2  pgoyette 		newlist = &phlistset->lists[0];
    201  1.28.12.2  pgoyette 	}
    202  1.28.12.2  pgoyette 
    203       1.28     rmind 	/* Make sure the hook is not already added. */
    204       1.28     rmind 	for (u_int i = 0; i < nhooks; i++) {
    205  1.28.12.2  pgoyette 		pfh = &oldlist->hooks[i];
    206  1.28.12.2  pgoyette 		if (pfh->pfil_func == func && pfh->pfil_arg == arg) {
    207  1.28.12.2  pgoyette 			mutex_exit(&pfil_mtx);
    208       1.16   thorpej 			return EEXIST;
    209  1.28.12.2  pgoyette 		}
    210       1.16   thorpej 	}
    211       1.16   thorpej 
    212  1.28.12.2  pgoyette 	/* create new pfil_list_t copied from old */
    213  1.28.12.2  pgoyette 	memcpy(newlist, oldlist, sizeof(pfil_list_t));
    214  1.28.12.2  pgoyette 	psref_target_init(&newlist->psref, pfil_psref_class);
    215  1.28.12.2  pgoyette 
    216       1.28     rmind 	/*
    217       1.28     rmind 	 * Finally, add the hook.  Note: for PFIL_IN we insert the hooks in
    218       1.28     rmind 	 * reverse order of the PFIL_OUT so that the same path is followed
    219       1.28     rmind 	 * in or out of the kernel.
    220       1.28     rmind 	 */
    221       1.28     rmind 	if (flags & PFIL_IN) {
    222       1.28     rmind 		/* XXX: May want to revisit this later; */
    223       1.28     rmind 		size_t len = sizeof(pfil_hook_t) * nhooks;
    224  1.28.12.2  pgoyette 		pfh = &newlist->hooks[0];
    225  1.28.12.2  pgoyette 		memmove(&newlist->hooks[1], pfh, len);
    226       1.28     rmind 	} else {
    227  1.28.12.2  pgoyette 		pfh = &newlist->hooks[nhooks];
    228       1.28     rmind 	}
    229  1.28.12.2  pgoyette 	newlist->nhooks++;
    230       1.16   thorpej 
    231        1.1       mrg 	pfh->pfil_func = func;
    232       1.16   thorpej 	pfh->pfil_arg  = arg;
    233  1.28.12.2  pgoyette 
    234  1.28.12.2  pgoyette 	/* switch from oldlist to newlist */
    235  1.28.12.2  pgoyette 	membar_producer();
    236  1.28.12.2  pgoyette 	phlistset->active = newlist;
    237  1.28.12.2  pgoyette #ifdef NET_MPSAFE
    238  1.28.12.2  pgoyette 	pserialize_perform(pfil_psz);
    239  1.28.12.2  pgoyette #endif
    240  1.28.12.2  pgoyette 	mutex_exit(&pfil_mtx);
    241  1.28.12.2  pgoyette 
    242  1.28.12.2  pgoyette 	/* Wait for all readers */
    243  1.28.12.2  pgoyette #ifdef NET_MPSAFE
    244  1.28.12.2  pgoyette 	psref_target_destroy(&oldlist->psref, pfil_psref_class);
    245  1.28.12.2  pgoyette #endif
    246  1.28.12.2  pgoyette 
    247       1.13   darrenr 	return 0;
    248        1.1       mrg }
    249        1.1       mrg 
    250        1.1       mrg /*
    251       1.28     rmind  * pfil_add_hook: add a function (hook) to the packet filter head.
    252       1.28     rmind  * The possible flags are:
    253       1.28     rmind  *
    254       1.28     rmind  *	PFIL_IN		call on incoming packets
    255       1.28     rmind  *	PFIL_OUT	call on outgoing packets
    256       1.28     rmind  *	PFIL_ALL	call on all of the above
    257        1.1       mrg  */
    258       1.13   darrenr int
    259       1.28     rmind pfil_add_hook(pfil_func_t func, void *arg, int flags, pfil_head_t *ph)
    260        1.1       mrg {
    261       1.28     rmind 	int error = 0;
    262       1.28     rmind 
    263       1.28     rmind 	KASSERT(func != NULL);
    264  1.28.12.1  pgoyette 	KASSERT((flags & ~PFIL_ALL) == 0);
    265       1.28     rmind 
    266       1.28     rmind 	for (u_int i = 0; i < __arraycount(pfil_flag_cases); i++) {
    267       1.28     rmind 		const int fcase = pfil_flag_cases[i];
    268  1.28.12.2  pgoyette 		pfil_listset_t *phlistset;
    269        1.1       mrg 
    270       1.28     rmind 		if ((flags & fcase) == 0) {
    271       1.28     rmind 			continue;
    272       1.28     rmind 		}
    273  1.28.12.2  pgoyette 		phlistset = pfil_hook_get(fcase, ph);
    274  1.28.12.2  pgoyette 		error = pfil_list_add(phlistset, (pfil_polyfunc_t)func, arg,
    275  1.28.12.2  pgoyette 		    flags);
    276  1.28.12.2  pgoyette 		if (error && (error != EEXIST))
    277       1.28     rmind 			break;
    278       1.28     rmind 	}
    279  1.28.12.2  pgoyette 	if (error && (error != EEXIST)) {
    280       1.28     rmind 		pfil_remove_hook(func, arg, flags, ph);
    281       1.28     rmind 	}
    282       1.28     rmind 	return error;
    283        1.1       mrg }
    284        1.1       mrg 
    285        1.1       mrg /*
    286  1.28.12.2  pgoyette  * pfil_add_ihook: add an interface-event function (hook) to the packet
    287  1.28.12.1  pgoyette  * filter head.  The possible flags are:
    288  1.28.12.1  pgoyette  *
    289  1.28.12.2  pgoyette  *	PFIL_IFADDR	call on interface reconfig (cmd is ioctl #)
    290  1.28.12.2  pgoyette  *	PFIL_IFNET	call on interface attach/detach (cmd is PFIL_IFNET_*)
    291  1.28.12.1  pgoyette  */
    292  1.28.12.1  pgoyette int
    293  1.28.12.1  pgoyette pfil_add_ihook(pfil_ifunc_t func, void *arg, int flags, pfil_head_t *ph)
    294  1.28.12.1  pgoyette {
    295  1.28.12.2  pgoyette 	pfil_listset_t *phlistset;
    296  1.28.12.1  pgoyette 
    297  1.28.12.2  pgoyette 	KASSERT(func != NULL);
    298  1.28.12.1  pgoyette 	KASSERT(flags == PFIL_IFADDR || flags == PFIL_IFNET);
    299  1.28.12.2  pgoyette 
    300  1.28.12.2  pgoyette 	phlistset = pfil_hook_get(flags, ph);
    301  1.28.12.2  pgoyette 	return pfil_list_add(phlistset, (pfil_polyfunc_t)func, arg, flags);
    302  1.28.12.1  pgoyette }
    303  1.28.12.1  pgoyette 
    304  1.28.12.1  pgoyette /*
    305       1.28     rmind  * pfil_list_remove: remove the hook from a specified list.
    306        1.1       mrg  */
    307       1.13   darrenr static int
    308  1.28.12.2  pgoyette pfil_list_remove(pfil_listset_t *phlistset, pfil_polyfunc_t func, void *arg)
    309        1.1       mrg {
    310  1.28.12.2  pgoyette 	u_int nhooks;
    311  1.28.12.2  pgoyette 	pfil_list_t *oldlist, *newlist;
    312  1.28.12.2  pgoyette 
    313  1.28.12.2  pgoyette 	mutex_enter(&pfil_mtx);
    314        1.1       mrg 
    315  1.28.12.2  pgoyette 	/* create new pfil_list_t copied from old */
    316  1.28.12.2  pgoyette 	if (phlistset->active == &phlistset->lists[0]) {
    317  1.28.12.2  pgoyette 		oldlist = &phlistset->lists[0];
    318  1.28.12.2  pgoyette 		newlist = &phlistset->lists[1];
    319  1.28.12.2  pgoyette 	} else{
    320  1.28.12.2  pgoyette 		oldlist = &phlistset->lists[1];
    321  1.28.12.2  pgoyette 		newlist = &phlistset->lists[0];
    322  1.28.12.2  pgoyette 	}
    323  1.28.12.2  pgoyette 	memcpy(newlist, oldlist, sizeof(*newlist));
    324  1.28.12.2  pgoyette 	psref_target_init(&newlist->psref, pfil_psref_class);
    325  1.28.12.2  pgoyette 
    326  1.28.12.2  pgoyette 	nhooks = newlist->nhooks;
    327       1.28     rmind 	for (u_int i = 0; i < nhooks; i++) {
    328  1.28.12.2  pgoyette 		pfil_hook_t *last, *pfh = &newlist->hooks[i];
    329       1.28     rmind 
    330       1.28     rmind 		if (pfh->pfil_func != func || pfh->pfil_arg != arg) {
    331       1.28     rmind 			continue;
    332       1.28     rmind 		}
    333  1.28.12.2  pgoyette 		if ((last = &newlist->hooks[nhooks - 1]) != pfh) {
    334       1.28     rmind 			memcpy(pfh, last, sizeof(pfil_hook_t));
    335        1.1       mrg 		}
    336  1.28.12.2  pgoyette 		newlist->nhooks--;
    337  1.28.12.2  pgoyette 
    338  1.28.12.2  pgoyette 		/* switch from oldlist to newlist */
    339  1.28.12.2  pgoyette 		phlistset->active = newlist;
    340  1.28.12.2  pgoyette 		membar_producer();
    341  1.28.12.2  pgoyette #ifdef NET_MPSAFE
    342  1.28.12.2  pgoyette 		pserialize_perform(pfil_psz);
    343  1.28.12.2  pgoyette #endif
    344  1.28.12.2  pgoyette 		mutex_exit(&pfil_mtx);
    345  1.28.12.2  pgoyette 
    346  1.28.12.2  pgoyette 		/* Wait for all readers */
    347  1.28.12.2  pgoyette #ifdef NET_MPSAFE
    348  1.28.12.2  pgoyette 		psref_target_destroy(&oldlist->psref, pfil_psref_class);
    349  1.28.12.2  pgoyette #endif
    350  1.28.12.2  pgoyette 
    351       1.28     rmind 		return 0;
    352       1.16   thorpej 	}
    353  1.28.12.2  pgoyette 	mutex_exit(&pfil_mtx);
    354       1.13   darrenr 	return ENOENT;
    355        1.1       mrg }
    356       1.28     rmind 
    357       1.28     rmind /*
    358       1.28     rmind  * pfil_remove_hook: remove the hook from the packet filter head.
    359       1.28     rmind  */
    360       1.28     rmind int
    361       1.28     rmind pfil_remove_hook(pfil_func_t func, void *arg, int flags, pfil_head_t *ph)
    362       1.28     rmind {
    363  1.28.12.2  pgoyette 	KASSERT((flags & ~PFIL_ALL) == 0);
    364  1.28.12.2  pgoyette 
    365       1.28     rmind 	for (u_int i = 0; i < __arraycount(pfil_flag_cases); i++) {
    366       1.28     rmind 		const int fcase = pfil_flag_cases[i];
    367  1.28.12.2  pgoyette 		pfil_listset_t *pflistset;
    368       1.28     rmind 
    369       1.28     rmind 		if ((flags & fcase) == 0) {
    370       1.28     rmind 			continue;
    371       1.28     rmind 		}
    372  1.28.12.2  pgoyette 		pflistset = pfil_hook_get(fcase, ph);
    373  1.28.12.2  pgoyette 		(void)pfil_list_remove(pflistset, (pfil_polyfunc_t)func, arg);
    374       1.28     rmind 	}
    375       1.28     rmind 	return 0;
    376       1.28     rmind }
    377       1.28     rmind 
    378  1.28.12.1  pgoyette int
    379  1.28.12.1  pgoyette pfil_remove_ihook(pfil_ifunc_t func, void *arg, int flags, pfil_head_t *ph)
    380  1.28.12.1  pgoyette {
    381  1.28.12.2  pgoyette 	pfil_listset_t *pflistset;
    382  1.28.12.1  pgoyette 
    383  1.28.12.1  pgoyette 	KASSERT(flags == PFIL_IFADDR || flags == PFIL_IFNET);
    384  1.28.12.2  pgoyette 	pflistset = pfil_hook_get(flags, ph);
    385  1.28.12.2  pgoyette 	(void)pfil_list_remove(pflistset, (pfil_polyfunc_t)func, arg);
    386  1.28.12.1  pgoyette 	return 0;
    387  1.28.12.1  pgoyette }
    388  1.28.12.1  pgoyette 
    389       1.28     rmind /*
    390       1.28     rmind  * pfil_run_hooks: run the specified packet filter hooks.
    391       1.28     rmind  */
    392       1.28     rmind int
    393       1.28     rmind pfil_run_hooks(pfil_head_t *ph, struct mbuf **mp, ifnet_t *ifp, int dir)
    394       1.28     rmind {
    395  1.28.12.1  pgoyette 	struct mbuf *m = mp ? *mp : NULL;
    396  1.28.12.2  pgoyette 	pfil_listset_t *phlistset;
    397       1.28     rmind 	pfil_list_t *phlist;
    398  1.28.12.2  pgoyette 	struct psref psref;
    399  1.28.12.2  pgoyette 	int s, bound;
    400       1.28     rmind 	int ret = 0;
    401       1.28     rmind 
    402  1.28.12.2  pgoyette 	KASSERT(dir == PFIL_IN || dir == PFIL_OUT);
    403  1.28.12.2  pgoyette 	if (__predict_false((phlistset = pfil_hook_get(dir, ph)) == NULL)) {
    404       1.28     rmind 		return ret;
    405       1.28     rmind 	}
    406       1.28     rmind 
    407  1.28.12.2  pgoyette 	bound = curlwp_bind();
    408  1.28.12.2  pgoyette 	s = pserialize_read_enter();
    409  1.28.12.2  pgoyette 	phlist = phlistset->active;
    410  1.28.12.2  pgoyette 	membar_datadep_consumer();
    411  1.28.12.2  pgoyette 	psref_acquire(&psref, &phlist->psref, pfil_psref_class);
    412  1.28.12.2  pgoyette 	pserialize_read_exit(s);
    413       1.28     rmind 	for (u_int i = 0; i < phlist->nhooks; i++) {
    414       1.28     rmind 		pfil_hook_t *pfh = &phlist->hooks[i];
    415  1.28.12.1  pgoyette 		pfil_func_t func = (pfil_func_t)pfh->pfil_func;
    416       1.28     rmind 
    417  1.28.12.1  pgoyette 		ret = (*func)(pfh->pfil_arg, &m, ifp, dir);
    418  1.28.12.1  pgoyette 		if (m == NULL || ret)
    419       1.28     rmind 			break;
    420       1.28     rmind 	}
    421  1.28.12.2  pgoyette 	psref_release(&psref, &phlist->psref, pfil_psref_class);
    422  1.28.12.2  pgoyette 	curlwp_bindx(bound);
    423       1.28     rmind 
    424  1.28.12.1  pgoyette 	if (mp) {
    425       1.28     rmind 		*mp = m;
    426       1.28     rmind 	}
    427       1.28     rmind 	return ret;
    428       1.28     rmind }
    429  1.28.12.1  pgoyette 
    430  1.28.12.2  pgoyette static void
    431  1.28.12.2  pgoyette pfil_run_arg(pfil_listset_t *phlistset, u_long cmd, void *arg)
    432  1.28.12.1  pgoyette {
    433  1.28.12.2  pgoyette 	pfil_list_t *phlist;
    434  1.28.12.2  pgoyette 	struct psref psref;
    435  1.28.12.2  pgoyette 	int s, bound;
    436  1.28.12.1  pgoyette 
    437  1.28.12.2  pgoyette 	bound = curlwp_bind();
    438  1.28.12.2  pgoyette 	s = pserialize_read_enter();
    439  1.28.12.2  pgoyette 	phlist = phlistset->active;
    440  1.28.12.2  pgoyette 	membar_datadep_consumer();
    441  1.28.12.2  pgoyette 	psref_acquire(&psref, &phlist->psref, pfil_psref_class);
    442  1.28.12.2  pgoyette 	pserialize_read_exit(s);
    443  1.28.12.1  pgoyette 	for (u_int i = 0; i < phlist->nhooks; i++) {
    444  1.28.12.1  pgoyette 		pfil_hook_t *pfh = &phlist->hooks[i];
    445  1.28.12.1  pgoyette 		pfil_ifunc_t func = (pfil_ifunc_t)pfh->pfil_func;
    446  1.28.12.2  pgoyette 		(*func)(pfh->pfil_arg, cmd, arg);
    447  1.28.12.1  pgoyette 	}
    448  1.28.12.2  pgoyette 	psref_release(&psref, &phlist->psref, pfil_psref_class);
    449  1.28.12.2  pgoyette 	curlwp_bindx(bound);
    450  1.28.12.1  pgoyette }
    451  1.28.12.1  pgoyette 
    452  1.28.12.1  pgoyette void
    453  1.28.12.2  pgoyette pfil_run_addrhooks(pfil_head_t *ph, u_long cmd, struct ifaddr *ifa)
    454  1.28.12.1  pgoyette {
    455  1.28.12.2  pgoyette 	pfil_run_arg(&ph->ph_ifaddr, cmd, ifa);
    456  1.28.12.2  pgoyette }
    457  1.28.12.1  pgoyette 
    458  1.28.12.2  pgoyette void
    459  1.28.12.2  pgoyette pfil_run_ifhooks(pfil_head_t *ph, u_long cmd, struct ifnet *ifp)
    460  1.28.12.2  pgoyette {
    461  1.28.12.2  pgoyette 	pfil_run_arg(&ph->ph_ifevent, cmd, ifp);
    462  1.28.12.1  pgoyette }
    463