Home | History | Annotate | Line # | Download | only in net
      1  1.42  knakahar /*	$NetBSD: pfil.c,v 1.42 2022/08/16 04:35:57 knakahara 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.42  knakahar __KERNEL_RCSID(0, "$NetBSD: pfil.c,v 1.42 2022/08/16 04:35:57 knakahara Exp $");
     32  1.34     ozaki 
     33  1.34     ozaki #if defined(_KERNEL_OPT)
     34  1.34     ozaki #include "opt_net_mpsafe.h"
     35  1.34     ozaki #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.32       ryo #include <sys/psref.h>
     42  1.41  riastrad #include <sys/cpu.h>
     43   1.1       mrg 
     44   1.1       mrg #include <net/if.h>
     45   1.1       mrg #include <net/pfil.h>
     46   1.1       mrg 
     47  1.28     rmind #define	MAX_HOOKS	8
     48  1.16   thorpej 
     49  1.29  christos /* Func is either pfil_func_t or pfil_ifunc_t. */
     50  1.29  christos typedef void		(*pfil_polyfunc_t)(void);
     51  1.29  christos 
     52  1.28     rmind typedef struct {
     53  1.29  christos 	pfil_polyfunc_t pfil_func;
     54  1.28     rmind 	void *		pfil_arg;
     55  1.28     rmind } pfil_hook_t;
     56  1.28     rmind 
     57  1.28     rmind typedef struct {
     58  1.28     rmind 	pfil_hook_t	hooks[MAX_HOOKS];
     59  1.28     rmind 	u_int		nhooks;
     60  1.32       ryo 	struct psref_target psref;
     61  1.28     rmind } pfil_list_t;
     62  1.28     rmind 
     63  1.32       ryo typedef struct {
     64  1.32       ryo 	pfil_list_t	*active;	/* lists[0] or lists[1] */
     65  1.32       ryo 	pfil_list_t	lists[2];
     66  1.32       ryo } pfil_listset_t;
     67  1.32       ryo 
     68  1.29  christos CTASSERT(PFIL_IN == 1);
     69  1.29  christos CTASSERT(PFIL_OUT == 2);
     70  1.29  christos 
     71  1.28     rmind struct pfil_head {
     72  1.32       ryo 	pfil_listset_t	ph_in;
     73  1.32       ryo 	pfil_listset_t	ph_out;
     74  1.32       ryo 	pfil_listset_t	ph_ifaddr;
     75  1.32       ryo 	pfil_listset_t	ph_ifevent;
     76  1.28     rmind 	int		ph_type;
     77  1.28     rmind 	void *		ph_key;
     78  1.28     rmind 	LIST_ENTRY(pfil_head) ph_list;
     79  1.28     rmind };
     80  1.28     rmind 
     81  1.28     rmind static const int pfil_flag_cases[] = {
     82  1.31       ryo 	PFIL_IN, PFIL_OUT
     83  1.28     rmind };
     84  1.16   thorpej 
     85  1.28     rmind static LIST_HEAD(, pfil_head) pfil_head_list __read_mostly =
     86  1.16   thorpej     LIST_HEAD_INITIALIZER(&pfil_head_list);
     87  1.16   thorpej 
     88  1.32       ryo static kmutex_t pfil_mtx __cacheline_aligned;
     89  1.32       ryo static struct psref_class *pfil_psref_class __read_mostly;
     90  1.39      maxv #ifdef NET_MPSAFE
     91  1.32       ryo static pserialize_t pfil_psz;
     92  1.39      maxv #endif
     93  1.32       ryo 
     94  1.32       ryo void
     95  1.32       ryo pfil_init(void)
     96  1.32       ryo {
     97  1.32       ryo 	mutex_init(&pfil_mtx, MUTEX_DEFAULT, IPL_NONE);
     98  1.39      maxv #ifdef NET_MPSAFE
     99  1.32       ryo 	pfil_psz = pserialize_create();
    100  1.39      maxv #endif
    101  1.32       ryo 	pfil_psref_class = psref_class_create("pfil", IPL_SOFTNET);
    102  1.32       ryo }
    103  1.32       ryo 
    104  1.32       ryo static inline void
    105  1.32       ryo pfil_listset_init(pfil_listset_t *pflistset)
    106  1.32       ryo {
    107  1.32       ryo 	pflistset->active = &pflistset->lists[0];
    108  1.32       ryo 	psref_target_init(&pflistset->active->psref, pfil_psref_class);
    109  1.32       ryo }
    110  1.32       ryo 
    111  1.16   thorpej /*
    112  1.28     rmind  * pfil_head_create: create and register a packet filter head.
    113  1.16   thorpej  */
    114  1.28     rmind pfil_head_t *
    115  1.28     rmind pfil_head_create(int type, void *key)
    116  1.16   thorpej {
    117  1.28     rmind 	pfil_head_t *ph;
    118   1.1       mrg 
    119  1.28     rmind 	if (pfil_head_get(type, key)) {
    120  1.28     rmind 		return NULL;
    121  1.16   thorpej 	}
    122  1.28     rmind 	ph = kmem_zalloc(sizeof(pfil_head_t), KM_SLEEP);
    123  1.28     rmind 	ph->ph_type = type;
    124  1.28     rmind 	ph->ph_key = key;
    125  1.16   thorpej 
    126  1.32       ryo 	pfil_listset_init(&ph->ph_in);
    127  1.32       ryo 	pfil_listset_init(&ph->ph_out);
    128  1.32       ryo 	pfil_listset_init(&ph->ph_ifaddr);
    129  1.32       ryo 	pfil_listset_init(&ph->ph_ifevent);
    130  1.32       ryo 
    131  1.16   thorpej 	LIST_INSERT_HEAD(&pfil_head_list, ph, ph_list);
    132  1.28     rmind 	return ph;
    133  1.16   thorpej }
    134  1.16   thorpej 
    135  1.16   thorpej /*
    136  1.28     rmind  * pfil_head_destroy: remove and destroy a packet filter head.
    137  1.16   thorpej  */
    138  1.28     rmind void
    139  1.28     rmind pfil_head_destroy(pfil_head_t *pfh)
    140  1.16   thorpej {
    141  1.16   thorpej 	LIST_REMOVE(pfh, ph_list);
    142  1.32       ryo 
    143  1.32       ryo 	psref_target_destroy(&pfh->ph_in.active->psref, pfil_psref_class);
    144  1.32       ryo 	psref_target_destroy(&pfh->ph_out.active->psref, pfil_psref_class);
    145  1.32       ryo 	psref_target_destroy(&pfh->ph_ifaddr.active->psref, pfil_psref_class);
    146  1.32       ryo 	psref_target_destroy(&pfh->ph_ifevent.active->psref, pfil_psref_class);
    147  1.32       ryo 
    148  1.28     rmind 	kmem_free(pfh, sizeof(pfil_head_t));
    149  1.16   thorpej }
    150  1.16   thorpej 
    151  1.16   thorpej /*
    152  1.28     rmind  * pfil_head_get: returns the packer filter head for a given key.
    153  1.16   thorpej  */
    154  1.28     rmind pfil_head_t *
    155  1.28     rmind pfil_head_get(int type, void *key)
    156  1.16   thorpej {
    157  1.28     rmind 	pfil_head_t *ph;
    158  1.16   thorpej 
    159  1.27    dyoung 	LIST_FOREACH(ph, &pfil_head_list, ph_list) {
    160  1.28     rmind 		if (ph->ph_type == type && ph->ph_key == key)
    161  1.16   thorpej 			break;
    162  1.16   thorpej 	}
    163  1.28     rmind 	return ph;
    164   1.1       mrg }
    165   1.1       mrg 
    166  1.32       ryo static pfil_listset_t *
    167  1.28     rmind pfil_hook_get(int dir, pfil_head_t *ph)
    168   1.1       mrg {
    169  1.28     rmind 	switch (dir) {
    170  1.28     rmind 	case PFIL_IN:
    171  1.30       ryo 		return &ph->ph_in;
    172  1.28     rmind 	case PFIL_OUT:
    173  1.30       ryo 		return &ph->ph_out;
    174  1.28     rmind 	case PFIL_IFADDR:
    175  1.28     rmind 		return &ph->ph_ifaddr;
    176  1.28     rmind 	case PFIL_IFNET:
    177  1.28     rmind 		return &ph->ph_ifevent;
    178  1.21    itojun 	}
    179  1.28     rmind 	return NULL;
    180   1.1       mrg }
    181   1.1       mrg 
    182  1.13   darrenr static int
    183  1.32       ryo pfil_list_add(pfil_listset_t *phlistset, pfil_polyfunc_t func, void *arg,
    184  1.32       ryo               int flags)
    185   1.1       mrg {
    186  1.32       ryo 	u_int nhooks;
    187  1.32       ryo 	pfil_list_t *newlist, *oldlist;
    188  1.28     rmind 	pfil_hook_t *pfh;
    189   1.1       mrg 
    190  1.32       ryo 	mutex_enter(&pfil_mtx);
    191  1.32       ryo 
    192  1.28     rmind 	/* Check if we have a free slot. */
    193  1.32       ryo 	nhooks = phlistset->active->nhooks;
    194  1.28     rmind 	if (nhooks == MAX_HOOKS) {
    195  1.32       ryo 		mutex_exit(&pfil_mtx);
    196  1.28     rmind 		return ENOSPC;
    197  1.28     rmind 	}
    198  1.28     rmind 	KASSERT(nhooks < MAX_HOOKS);
    199  1.28     rmind 
    200  1.32       ryo 	if (phlistset->active == &phlistset->lists[0]) {
    201  1.32       ryo 		oldlist = &phlistset->lists[0];
    202  1.32       ryo 		newlist = &phlistset->lists[1];
    203  1.32       ryo 	} else{
    204  1.32       ryo 		oldlist = &phlistset->lists[1];
    205  1.32       ryo 		newlist = &phlistset->lists[0];
    206  1.32       ryo 	}
    207  1.32       ryo 
    208  1.28     rmind 	/* Make sure the hook is not already added. */
    209  1.28     rmind 	for (u_int i = 0; i < nhooks; i++) {
    210  1.32       ryo 		pfh = &oldlist->hooks[i];
    211  1.32       ryo 		if (pfh->pfil_func == func && pfh->pfil_arg == arg) {
    212  1.32       ryo 			mutex_exit(&pfil_mtx);
    213  1.16   thorpej 			return EEXIST;
    214  1.32       ryo 		}
    215  1.16   thorpej 	}
    216  1.16   thorpej 
    217  1.32       ryo 	/* create new pfil_list_t copied from old */
    218  1.32       ryo 	memcpy(newlist, oldlist, sizeof(pfil_list_t));
    219  1.32       ryo 	psref_target_init(&newlist->psref, pfil_psref_class);
    220  1.32       ryo 
    221  1.28     rmind 	/*
    222  1.28     rmind 	 * Finally, add the hook.  Note: for PFIL_IN we insert the hooks in
    223  1.28     rmind 	 * reverse order of the PFIL_OUT so that the same path is followed
    224  1.28     rmind 	 * in or out of the kernel.
    225  1.28     rmind 	 */
    226  1.28     rmind 	if (flags & PFIL_IN) {
    227  1.28     rmind 		/* XXX: May want to revisit this later; */
    228  1.28     rmind 		size_t len = sizeof(pfil_hook_t) * nhooks;
    229  1.32       ryo 		pfh = &newlist->hooks[0];
    230  1.32       ryo 		memmove(&newlist->hooks[1], pfh, len);
    231  1.28     rmind 	} else {
    232  1.32       ryo 		pfh = &newlist->hooks[nhooks];
    233  1.28     rmind 	}
    234  1.32       ryo 	newlist->nhooks++;
    235  1.16   thorpej 
    236   1.1       mrg 	pfh->pfil_func = func;
    237  1.16   thorpej 	pfh->pfil_arg  = arg;
    238  1.32       ryo 
    239  1.32       ryo 	/* switch from oldlist to newlist */
    240  1.36  riastrad 	atomic_store_release(&phlistset->active, newlist);
    241  1.34     ozaki #ifdef NET_MPSAFE
    242  1.32       ryo 	pserialize_perform(pfil_psz);
    243  1.34     ozaki #endif
    244  1.32       ryo 	mutex_exit(&pfil_mtx);
    245  1.32       ryo 
    246  1.32       ryo 	/* Wait for all readers */
    247  1.34     ozaki #ifdef NET_MPSAFE
    248  1.32       ryo 	psref_target_destroy(&oldlist->psref, pfil_psref_class);
    249  1.34     ozaki #endif
    250  1.32       ryo 
    251  1.13   darrenr 	return 0;
    252   1.1       mrg }
    253   1.1       mrg 
    254   1.1       mrg /*
    255  1.28     rmind  * pfil_add_hook: add a function (hook) to the packet filter head.
    256  1.28     rmind  * The possible flags are:
    257  1.28     rmind  *
    258  1.28     rmind  *	PFIL_IN		call on incoming packets
    259  1.28     rmind  *	PFIL_OUT	call on outgoing packets
    260  1.28     rmind  *	PFIL_ALL	call on all of the above
    261   1.1       mrg  */
    262  1.13   darrenr int
    263  1.28     rmind pfil_add_hook(pfil_func_t func, void *arg, int flags, pfil_head_t *ph)
    264   1.1       mrg {
    265  1.28     rmind 	int error = 0;
    266  1.28     rmind 
    267  1.28     rmind 	KASSERT(func != NULL);
    268  1.29  christos 	KASSERT((flags & ~PFIL_ALL) == 0);
    269  1.28     rmind 
    270  1.40  riastrad 	ASSERT_SLEEPABLE();
    271  1.40  riastrad 
    272  1.28     rmind 	for (u_int i = 0; i < __arraycount(pfil_flag_cases); i++) {
    273  1.28     rmind 		const int fcase = pfil_flag_cases[i];
    274  1.32       ryo 		pfil_listset_t *phlistset;
    275   1.1       mrg 
    276  1.28     rmind 		if ((flags & fcase) == 0) {
    277  1.28     rmind 			continue;
    278  1.28     rmind 		}
    279  1.32       ryo 		phlistset = pfil_hook_get(fcase, ph);
    280  1.32       ryo 		error = pfil_list_add(phlistset, (pfil_polyfunc_t)func, arg,
    281  1.32       ryo 		    flags);
    282  1.32       ryo 		if (error && (error != EEXIST))
    283  1.28     rmind 			break;
    284  1.28     rmind 	}
    285  1.32       ryo 	if (error && (error != EEXIST)) {
    286  1.28     rmind 		pfil_remove_hook(func, arg, flags, ph);
    287  1.28     rmind 	}
    288  1.28     rmind 	return error;
    289   1.1       mrg }
    290   1.1       mrg 
    291   1.1       mrg /*
    292  1.31       ryo  * pfil_add_ihook: add an interface-event function (hook) to the packet
    293  1.29  christos  * filter head.  The possible flags are:
    294  1.29  christos  *
    295  1.31       ryo  *	PFIL_IFADDR	call on interface reconfig (cmd is ioctl #)
    296  1.31       ryo  *	PFIL_IFNET	call on interface attach/detach (cmd is PFIL_IFNET_*)
    297  1.29  christos  */
    298  1.29  christos int
    299  1.29  christos pfil_add_ihook(pfil_ifunc_t func, void *arg, int flags, pfil_head_t *ph)
    300  1.29  christos {
    301  1.32       ryo 	pfil_listset_t *phlistset;
    302  1.29  christos 
    303  1.31       ryo 	KASSERT(func != NULL);
    304  1.29  christos 	KASSERT(flags == PFIL_IFADDR || flags == PFIL_IFNET);
    305  1.31       ryo 
    306  1.40  riastrad 	ASSERT_SLEEPABLE();
    307  1.40  riastrad 
    308  1.32       ryo 	phlistset = pfil_hook_get(flags, ph);
    309  1.32       ryo 	return pfil_list_add(phlistset, (pfil_polyfunc_t)func, arg, flags);
    310  1.29  christos }
    311  1.29  christos 
    312  1.29  christos /*
    313  1.28     rmind  * pfil_list_remove: remove the hook from a specified list.
    314   1.1       mrg  */
    315  1.13   darrenr static int
    316  1.32       ryo pfil_list_remove(pfil_listset_t *phlistset, pfil_polyfunc_t func, void *arg)
    317   1.1       mrg {
    318  1.32       ryo 	u_int nhooks;
    319  1.32       ryo 	pfil_list_t *oldlist, *newlist;
    320  1.32       ryo 
    321  1.32       ryo 	mutex_enter(&pfil_mtx);
    322   1.1       mrg 
    323  1.32       ryo 	/* create new pfil_list_t copied from old */
    324  1.32       ryo 	if (phlistset->active == &phlistset->lists[0]) {
    325  1.32       ryo 		oldlist = &phlistset->lists[0];
    326  1.32       ryo 		newlist = &phlistset->lists[1];
    327  1.32       ryo 	} else{
    328  1.32       ryo 		oldlist = &phlistset->lists[1];
    329  1.32       ryo 		newlist = &phlistset->lists[0];
    330  1.32       ryo 	}
    331  1.32       ryo 	memcpy(newlist, oldlist, sizeof(*newlist));
    332  1.32       ryo 	psref_target_init(&newlist->psref, pfil_psref_class);
    333  1.32       ryo 
    334  1.32       ryo 	nhooks = newlist->nhooks;
    335  1.28     rmind 	for (u_int i = 0; i < nhooks; i++) {
    336  1.32       ryo 		pfil_hook_t *last, *pfh = &newlist->hooks[i];
    337  1.28     rmind 
    338  1.28     rmind 		if (pfh->pfil_func != func || pfh->pfil_arg != arg) {
    339  1.28     rmind 			continue;
    340  1.28     rmind 		}
    341  1.32       ryo 		if ((last = &newlist->hooks[nhooks - 1]) != pfh) {
    342  1.28     rmind 			memcpy(pfh, last, sizeof(pfil_hook_t));
    343   1.1       mrg 		}
    344  1.32       ryo 		newlist->nhooks--;
    345  1.32       ryo 
    346  1.32       ryo 		/* switch from oldlist to newlist */
    347  1.36  riastrad 		atomic_store_release(&phlistset->active, newlist);
    348  1.34     ozaki #ifdef NET_MPSAFE
    349  1.32       ryo 		pserialize_perform(pfil_psz);
    350  1.34     ozaki #endif
    351  1.32       ryo 		mutex_exit(&pfil_mtx);
    352  1.32       ryo 
    353  1.32       ryo 		/* Wait for all readers */
    354  1.34     ozaki #ifdef NET_MPSAFE
    355  1.32       ryo 		psref_target_destroy(&oldlist->psref, pfil_psref_class);
    356  1.34     ozaki #endif
    357  1.32       ryo 
    358  1.28     rmind 		return 0;
    359  1.16   thorpej 	}
    360  1.32       ryo 	mutex_exit(&pfil_mtx);
    361  1.13   darrenr 	return ENOENT;
    362   1.1       mrg }
    363  1.28     rmind 
    364  1.28     rmind /*
    365  1.28     rmind  * pfil_remove_hook: remove the hook from the packet filter head.
    366  1.28     rmind  */
    367  1.28     rmind int
    368  1.28     rmind pfil_remove_hook(pfil_func_t func, void *arg, int flags, pfil_head_t *ph)
    369  1.28     rmind {
    370  1.31       ryo 	KASSERT((flags & ~PFIL_ALL) == 0);
    371  1.31       ryo 
    372  1.40  riastrad 	ASSERT_SLEEPABLE();
    373  1.40  riastrad 
    374  1.28     rmind 	for (u_int i = 0; i < __arraycount(pfil_flag_cases); i++) {
    375  1.28     rmind 		const int fcase = pfil_flag_cases[i];
    376  1.32       ryo 		pfil_listset_t *pflistset;
    377  1.28     rmind 
    378  1.28     rmind 		if ((flags & fcase) == 0) {
    379  1.28     rmind 			continue;
    380  1.28     rmind 		}
    381  1.32       ryo 		pflistset = pfil_hook_get(fcase, ph);
    382  1.32       ryo 		(void)pfil_list_remove(pflistset, (pfil_polyfunc_t)func, arg);
    383  1.28     rmind 	}
    384  1.28     rmind 	return 0;
    385  1.28     rmind }
    386  1.28     rmind 
    387  1.29  christos int
    388  1.29  christos pfil_remove_ihook(pfil_ifunc_t func, void *arg, int flags, pfil_head_t *ph)
    389  1.29  christos {
    390  1.32       ryo 	pfil_listset_t *pflistset;
    391  1.29  christos 
    392  1.29  christos 	KASSERT(flags == PFIL_IFADDR || flags == PFIL_IFNET);
    393  1.40  riastrad 
    394  1.40  riastrad 	ASSERT_SLEEPABLE();
    395  1.40  riastrad 
    396  1.32       ryo 	pflistset = pfil_hook_get(flags, ph);
    397  1.32       ryo 	(void)pfil_list_remove(pflistset, (pfil_polyfunc_t)func, arg);
    398  1.29  christos 	return 0;
    399  1.29  christos }
    400  1.29  christos 
    401  1.28     rmind /*
    402  1.28     rmind  * pfil_run_hooks: run the specified packet filter hooks.
    403  1.28     rmind  */
    404  1.28     rmind int
    405  1.28     rmind pfil_run_hooks(pfil_head_t *ph, struct mbuf **mp, ifnet_t *ifp, int dir)
    406  1.28     rmind {
    407  1.29  christos 	struct mbuf *m = mp ? *mp : NULL;
    408  1.32       ryo 	pfil_listset_t *phlistset;
    409  1.28     rmind 	pfil_list_t *phlist;
    410  1.32       ryo 	struct psref psref;
    411  1.33     ozaki 	int s, bound;
    412  1.28     rmind 	int ret = 0;
    413  1.28     rmind 
    414  1.31       ryo 	KASSERT(dir == PFIL_IN || dir == PFIL_OUT);
    415  1.41  riastrad 	KASSERT(!cpu_intr_p());
    416  1.37       nat 
    417  1.38       nat 	if (ph == NULL) {
    418  1.37       nat 		return ret;
    419  1.37       nat 	}
    420  1.37       nat 
    421  1.32       ryo 	if (__predict_false((phlistset = pfil_hook_get(dir, ph)) == NULL)) {
    422  1.28     rmind 		return ret;
    423  1.28     rmind 	}
    424  1.28     rmind 
    425  1.33     ozaki 	bound = curlwp_bind();
    426  1.32       ryo 	s = pserialize_read_enter();
    427  1.36  riastrad 	phlist = atomic_load_consume(&phlistset->active);
    428  1.42  knakahar 	if (phlist->nhooks == 0) {
    429  1.42  knakahar 		pserialize_read_exit(s);
    430  1.42  knakahar 		curlwp_bindx(bound);
    431  1.42  knakahar 		return ret;
    432  1.42  knakahar 	}
    433  1.32       ryo 	psref_acquire(&psref, &phlist->psref, pfil_psref_class);
    434  1.32       ryo 	pserialize_read_exit(s);
    435  1.28     rmind 	for (u_int i = 0; i < phlist->nhooks; i++) {
    436  1.28     rmind 		pfil_hook_t *pfh = &phlist->hooks[i];
    437  1.29  christos 		pfil_func_t func = (pfil_func_t)pfh->pfil_func;
    438  1.28     rmind 
    439  1.29  christos 		ret = (*func)(pfh->pfil_arg, &m, ifp, dir);
    440  1.29  christos 		if (m == NULL || ret)
    441  1.28     rmind 			break;
    442  1.28     rmind 	}
    443  1.32       ryo 	psref_release(&psref, &phlist->psref, pfil_psref_class);
    444  1.33     ozaki 	curlwp_bindx(bound);
    445  1.28     rmind 
    446  1.29  christos 	if (mp) {
    447  1.28     rmind 		*mp = m;
    448  1.28     rmind 	}
    449  1.28     rmind 	return ret;
    450  1.28     rmind }
    451  1.29  christos 
    452  1.32       ryo static void
    453  1.32       ryo pfil_run_arg(pfil_listset_t *phlistset, u_long cmd, void *arg)
    454  1.29  christos {
    455  1.32       ryo 	pfil_list_t *phlist;
    456  1.32       ryo 	struct psref psref;
    457  1.33     ozaki 	int s, bound;
    458  1.29  christos 
    459  1.41  riastrad 	KASSERT(!cpu_intr_p());
    460  1.41  riastrad 
    461  1.33     ozaki 	bound = curlwp_bind();
    462  1.32       ryo 	s = pserialize_read_enter();
    463  1.36  riastrad 	phlist = atomic_load_consume(&phlistset->active);
    464  1.32       ryo 	psref_acquire(&psref, &phlist->psref, pfil_psref_class);
    465  1.32       ryo 	pserialize_read_exit(s);
    466  1.29  christos 	for (u_int i = 0; i < phlist->nhooks; i++) {
    467  1.29  christos 		pfil_hook_t *pfh = &phlist->hooks[i];
    468  1.29  christos 		pfil_ifunc_t func = (pfil_ifunc_t)pfh->pfil_func;
    469  1.32       ryo 		(*func)(pfh->pfil_arg, cmd, arg);
    470  1.29  christos 	}
    471  1.32       ryo 	psref_release(&psref, &phlist->psref, pfil_psref_class);
    472  1.33     ozaki 	curlwp_bindx(bound);
    473  1.32       ryo }
    474  1.32       ryo 
    475  1.32       ryo void
    476  1.32       ryo pfil_run_addrhooks(pfil_head_t *ph, u_long cmd, struct ifaddr *ifa)
    477  1.32       ryo {
    478  1.32       ryo 	pfil_run_arg(&ph->ph_ifaddr, cmd, ifa);
    479  1.29  christos }
    480  1.29  christos 
    481  1.29  christos void
    482  1.29  christos pfil_run_ifhooks(pfil_head_t *ph, u_long cmd, struct ifnet *ifp)
    483  1.29  christos {
    484  1.32       ryo 	pfil_run_arg(&ph->ph_ifevent, cmd, ifp);
    485  1.29  christos }
    486