Home | History | Annotate | Line # | Download | only in net
pfil.c revision 1.30
      1  1.30       ryo /*	$NetBSD: pfil.c,v 1.30 2017/01/04 13:03:41 ryo 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.30       ryo __KERNEL_RCSID(0, "$NetBSD: pfil.c,v 1.30 2017/01/04 13:03:41 ryo Exp $");
     32   1.1       mrg 
     33   1.1       mrg #include <sys/param.h>
     34   1.1       mrg #include <sys/systm.h>
     35   1.1       mrg #include <sys/queue.h>
     36  1.28     rmind #include <sys/kmem.h>
     37   1.1       mrg 
     38   1.1       mrg #include <net/if.h>
     39   1.1       mrg #include <net/pfil.h>
     40   1.1       mrg 
     41  1.28     rmind #define	MAX_HOOKS	8
     42  1.16   thorpej 
     43  1.29  christos /* Func is either pfil_func_t or pfil_ifunc_t. */
     44  1.29  christos typedef void		(*pfil_polyfunc_t)(void);
     45  1.29  christos 
     46  1.28     rmind typedef struct {
     47  1.29  christos 	pfil_polyfunc_t pfil_func;
     48  1.28     rmind 	void *		pfil_arg;
     49  1.28     rmind } pfil_hook_t;
     50  1.28     rmind 
     51  1.28     rmind typedef struct {
     52  1.28     rmind 	pfil_hook_t	hooks[MAX_HOOKS];
     53  1.28     rmind 	u_int		nhooks;
     54  1.28     rmind } pfil_list_t;
     55  1.28     rmind 
     56  1.29  christos CTASSERT(PFIL_IN == 1);
     57  1.29  christos CTASSERT(PFIL_OUT == 2);
     58  1.29  christos 
     59  1.28     rmind struct pfil_head {
     60  1.30       ryo 	pfil_list_t	ph_in;
     61  1.30       ryo 	pfil_list_t	ph_out;
     62  1.28     rmind 	pfil_list_t	ph_ifaddr;
     63  1.28     rmind 	pfil_list_t	ph_ifevent;
     64  1.28     rmind 	int		ph_type;
     65  1.28     rmind 	void *		ph_key;
     66  1.28     rmind 	LIST_ENTRY(pfil_head) ph_list;
     67  1.28     rmind };
     68  1.28     rmind 
     69  1.28     rmind static const int pfil_flag_cases[] = {
     70  1.28     rmind 	PFIL_IN, PFIL_OUT, PFIL_IFADDR, PFIL_IFNET
     71  1.28     rmind };
     72  1.16   thorpej 
     73  1.28     rmind static LIST_HEAD(, pfil_head) pfil_head_list __read_mostly =
     74  1.16   thorpej     LIST_HEAD_INITIALIZER(&pfil_head_list);
     75  1.16   thorpej 
     76  1.16   thorpej /*
     77  1.28     rmind  * pfil_head_create: create and register a packet filter head.
     78  1.16   thorpej  */
     79  1.28     rmind pfil_head_t *
     80  1.28     rmind pfil_head_create(int type, void *key)
     81  1.16   thorpej {
     82  1.28     rmind 	pfil_head_t *ph;
     83   1.1       mrg 
     84  1.28     rmind 	if (pfil_head_get(type, key)) {
     85  1.28     rmind 		return NULL;
     86  1.16   thorpej 	}
     87  1.28     rmind 	ph = kmem_zalloc(sizeof(pfil_head_t), KM_SLEEP);
     88  1.28     rmind 	ph->ph_type = type;
     89  1.28     rmind 	ph->ph_key = key;
     90  1.16   thorpej 
     91  1.16   thorpej 	LIST_INSERT_HEAD(&pfil_head_list, ph, ph_list);
     92  1.28     rmind 	return ph;
     93  1.16   thorpej }
     94  1.16   thorpej 
     95  1.16   thorpej /*
     96  1.28     rmind  * pfil_head_destroy: remove and destroy a packet filter head.
     97  1.16   thorpej  */
     98  1.28     rmind void
     99  1.28     rmind pfil_head_destroy(pfil_head_t *pfh)
    100  1.16   thorpej {
    101  1.16   thorpej 	LIST_REMOVE(pfh, ph_list);
    102  1.28     rmind 	kmem_free(pfh, sizeof(pfil_head_t));
    103  1.16   thorpej }
    104  1.16   thorpej 
    105  1.16   thorpej /*
    106  1.28     rmind  * pfil_head_get: returns the packer filter head for a given key.
    107  1.16   thorpej  */
    108  1.28     rmind pfil_head_t *
    109  1.28     rmind pfil_head_get(int type, void *key)
    110  1.16   thorpej {
    111  1.28     rmind 	pfil_head_t *ph;
    112  1.16   thorpej 
    113  1.27    dyoung 	LIST_FOREACH(ph, &pfil_head_list, ph_list) {
    114  1.28     rmind 		if (ph->ph_type == type && ph->ph_key == key)
    115  1.16   thorpej 			break;
    116  1.16   thorpej 	}
    117  1.28     rmind 	return ph;
    118   1.1       mrg }
    119   1.1       mrg 
    120  1.28     rmind static pfil_list_t *
    121  1.28     rmind pfil_hook_get(int dir, pfil_head_t *ph)
    122   1.1       mrg {
    123  1.28     rmind 	switch (dir) {
    124  1.28     rmind 	case PFIL_IN:
    125  1.30       ryo 		return &ph->ph_in;
    126  1.28     rmind 	case PFIL_OUT:
    127  1.30       ryo 		return &ph->ph_out;
    128  1.28     rmind 	case PFIL_IFADDR:
    129  1.28     rmind 		return &ph->ph_ifaddr;
    130  1.28     rmind 	case PFIL_IFNET:
    131  1.28     rmind 		return &ph->ph_ifevent;
    132  1.21    itojun 	}
    133  1.28     rmind 	return NULL;
    134   1.1       mrg }
    135   1.1       mrg 
    136  1.13   darrenr static int
    137  1.29  christos pfil_list_add(pfil_list_t *phlist, pfil_polyfunc_t func, void *arg, int flags)
    138   1.1       mrg {
    139  1.28     rmind 	const u_int nhooks = phlist->nhooks;
    140  1.28     rmind 	pfil_hook_t *pfh;
    141   1.1       mrg 
    142  1.28     rmind 	/* Check if we have a free slot. */
    143  1.28     rmind 	if (nhooks == MAX_HOOKS) {
    144  1.28     rmind 		return ENOSPC;
    145  1.28     rmind 	}
    146  1.28     rmind 	KASSERT(nhooks < MAX_HOOKS);
    147  1.28     rmind 
    148  1.28     rmind 	/* Make sure the hook is not already added. */
    149  1.28     rmind 	for (u_int i = 0; i < nhooks; i++) {
    150  1.28     rmind 		pfh = &phlist->hooks[i];
    151  1.26    dyoung 		if (pfh->pfil_func == func && pfh->pfil_arg == arg)
    152  1.16   thorpej 			return EEXIST;
    153  1.16   thorpej 	}
    154  1.16   thorpej 
    155  1.28     rmind 	/*
    156  1.28     rmind 	 * Finally, add the hook.  Note: for PFIL_IN we insert the hooks in
    157  1.28     rmind 	 * reverse order of the PFIL_OUT so that the same path is followed
    158  1.28     rmind 	 * in or out of the kernel.
    159  1.28     rmind 	 */
    160  1.28     rmind 	if (flags & PFIL_IN) {
    161  1.28     rmind 		/* XXX: May want to revisit this later; */
    162  1.28     rmind 		size_t len = sizeof(pfil_hook_t) * nhooks;
    163  1.28     rmind 		pfh = &phlist->hooks[0];
    164  1.28     rmind 		memmove(&phlist->hooks[1], pfh, len);
    165  1.28     rmind 	} else {
    166  1.28     rmind 		pfh = &phlist->hooks[nhooks];
    167  1.28     rmind 	}
    168  1.28     rmind 	phlist->nhooks++;
    169  1.16   thorpej 
    170   1.1       mrg 	pfh->pfil_func = func;
    171  1.16   thorpej 	pfh->pfil_arg  = arg;
    172  1.13   darrenr 	return 0;
    173   1.1       mrg }
    174   1.1       mrg 
    175   1.1       mrg /*
    176  1.28     rmind  * pfil_add_hook: add a function (hook) to the packet filter head.
    177  1.28     rmind  * The possible flags are:
    178  1.28     rmind  *
    179  1.28     rmind  *	PFIL_IN		call on incoming packets
    180  1.28     rmind  *	PFIL_OUT	call on outgoing packets
    181  1.28     rmind  *	PFIL_ALL	call on all of the above
    182   1.1       mrg  */
    183  1.13   darrenr int
    184  1.28     rmind pfil_add_hook(pfil_func_t func, void *arg, int flags, pfil_head_t *ph)
    185   1.1       mrg {
    186  1.28     rmind 	int error = 0;
    187  1.28     rmind 
    188  1.28     rmind 	KASSERT(func != NULL);
    189  1.29  christos 	KASSERT((flags & ~PFIL_ALL) == 0);
    190  1.28     rmind 
    191  1.28     rmind 	for (u_int i = 0; i < __arraycount(pfil_flag_cases); i++) {
    192  1.28     rmind 		const int fcase = pfil_flag_cases[i];
    193  1.28     rmind 		pfil_list_t *phlist;
    194   1.1       mrg 
    195  1.28     rmind 		if ((flags & fcase) == 0) {
    196  1.28     rmind 			continue;
    197  1.28     rmind 		}
    198  1.28     rmind 		phlist = pfil_hook_get(fcase, ph);
    199  1.29  christos 		error = pfil_list_add(phlist, (pfil_polyfunc_t)func, arg, flags);
    200  1.29  christos 		if (error) {
    201  1.28     rmind 			break;
    202  1.28     rmind 		}
    203  1.28     rmind 	}
    204  1.28     rmind 	if (error) {
    205  1.28     rmind 		pfil_remove_hook(func, arg, flags, ph);
    206  1.28     rmind 	}
    207  1.28     rmind 	return error;
    208   1.1       mrg }
    209   1.1       mrg 
    210   1.1       mrg /*
    211  1.29  christos  * pfil_add_hook: add an interface-event function (hook) to the packet
    212  1.29  christos  * filter head.  The possible flags are:
    213  1.29  christos  *
    214  1.29  christos  *	PFIL_IFADDR	call on interface reconfig (mbuf is ioctl #)
    215  1.29  christos  *	PFIL_IFNET	call on interface attach/detach (mbuf is PFIL_IFNET_*)
    216  1.29  christos  */
    217  1.29  christos int
    218  1.29  christos pfil_add_ihook(pfil_ifunc_t func, void *arg, int flags, pfil_head_t *ph)
    219  1.29  christos {
    220  1.29  christos 	pfil_list_t *phlist;
    221  1.29  christos 
    222  1.29  christos 	KASSERT(flags == PFIL_IFADDR || flags == PFIL_IFNET);
    223  1.29  christos 	phlist = pfil_hook_get(flags, ph);
    224  1.29  christos 	return pfil_list_add(phlist, (pfil_polyfunc_t)func, arg, flags);
    225  1.29  christos }
    226  1.29  christos 
    227  1.29  christos /*
    228  1.28     rmind  * pfil_list_remove: remove the hook from a specified list.
    229   1.1       mrg  */
    230  1.13   darrenr static int
    231  1.29  christos pfil_list_remove(pfil_list_t *phlist, pfil_polyfunc_t func, void *arg)
    232   1.1       mrg {
    233  1.28     rmind 	const u_int nhooks = phlist->nhooks;
    234   1.1       mrg 
    235  1.28     rmind 	for (u_int i = 0; i < nhooks; i++) {
    236  1.28     rmind 		pfil_hook_t *last, *pfh = &phlist->hooks[i];
    237  1.28     rmind 
    238  1.28     rmind 		if (pfh->pfil_func != func || pfh->pfil_arg != arg) {
    239  1.28     rmind 			continue;
    240  1.28     rmind 		}
    241  1.28     rmind 		if ((last = &phlist->hooks[nhooks - 1]) != pfh) {
    242  1.28     rmind 			memcpy(pfh, last, sizeof(pfil_hook_t));
    243   1.1       mrg 		}
    244  1.28     rmind 		phlist->nhooks--;
    245  1.28     rmind 		return 0;
    246  1.16   thorpej 	}
    247  1.13   darrenr 	return ENOENT;
    248   1.1       mrg }
    249  1.28     rmind 
    250  1.28     rmind /*
    251  1.28     rmind  * pfil_remove_hook: remove the hook from the packet filter head.
    252  1.28     rmind  */
    253  1.28     rmind int
    254  1.28     rmind pfil_remove_hook(pfil_func_t func, void *arg, int flags, pfil_head_t *ph)
    255  1.28     rmind {
    256  1.28     rmind 	for (u_int i = 0; i < __arraycount(pfil_flag_cases); i++) {
    257  1.28     rmind 		const int fcase = pfil_flag_cases[i];
    258  1.28     rmind 		pfil_list_t *pflist;
    259  1.28     rmind 
    260  1.28     rmind 		if ((flags & fcase) == 0) {
    261  1.28     rmind 			continue;
    262  1.28     rmind 		}
    263  1.28     rmind 		pflist = pfil_hook_get(fcase, ph);
    264  1.29  christos 		(void)pfil_list_remove(pflist, (pfil_polyfunc_t)func, arg);
    265  1.28     rmind 	}
    266  1.28     rmind 	return 0;
    267  1.28     rmind }
    268  1.28     rmind 
    269  1.29  christos int
    270  1.29  christos pfil_remove_ihook(pfil_ifunc_t func, void *arg, int flags, pfil_head_t *ph)
    271  1.29  christos {
    272  1.29  christos 	pfil_list_t *pflist;
    273  1.29  christos 
    274  1.29  christos 	KASSERT(flags == PFIL_IFADDR || flags == PFIL_IFNET);
    275  1.29  christos 	pflist = pfil_hook_get(flags, ph);
    276  1.29  christos 	(void)pfil_list_remove(pflist, (pfil_polyfunc_t)func, arg);
    277  1.29  christos 	return 0;
    278  1.29  christos }
    279  1.29  christos 
    280  1.28     rmind /*
    281  1.28     rmind  * pfil_run_hooks: run the specified packet filter hooks.
    282  1.28     rmind  */
    283  1.28     rmind int
    284  1.28     rmind pfil_run_hooks(pfil_head_t *ph, struct mbuf **mp, ifnet_t *ifp, int dir)
    285  1.28     rmind {
    286  1.29  christos 	struct mbuf *m = mp ? *mp : NULL;
    287  1.28     rmind 	pfil_list_t *phlist;
    288  1.28     rmind 	int ret = 0;
    289  1.28     rmind 
    290  1.29  christos 	KASSERT((dir & ~PFIL_ALL) == 0);
    291  1.30       ryo 	if (__predict_false((phlist = pfil_hook_get(dir, ph)) == NULL)) {
    292  1.28     rmind 		return ret;
    293  1.28     rmind 	}
    294  1.28     rmind 
    295  1.28     rmind 	for (u_int i = 0; i < phlist->nhooks; i++) {
    296  1.28     rmind 		pfil_hook_t *pfh = &phlist->hooks[i];
    297  1.29  christos 		pfil_func_t func = (pfil_func_t)pfh->pfil_func;
    298  1.28     rmind 
    299  1.29  christos 		ret = (*func)(pfh->pfil_arg, &m, ifp, dir);
    300  1.29  christos 		if (m == NULL || ret)
    301  1.28     rmind 			break;
    302  1.28     rmind 	}
    303  1.28     rmind 
    304  1.29  christos 	if (mp) {
    305  1.28     rmind 		*mp = m;
    306  1.28     rmind 	}
    307  1.28     rmind 	return ret;
    308  1.28     rmind }
    309  1.29  christos 
    310  1.29  christos void
    311  1.29  christos pfil_run_addrhooks(pfil_head_t *ph, u_long cmd, struct ifaddr *ifa)
    312  1.29  christos {
    313  1.29  christos 	pfil_list_t *phlist = &ph->ph_ifaddr;
    314  1.29  christos 
    315  1.29  christos 	for (u_int i = 0; i < phlist->nhooks; i++) {
    316  1.29  christos 		pfil_hook_t *pfh = &phlist->hooks[i];
    317  1.29  christos 		pfil_ifunc_t func = (pfil_ifunc_t)pfh->pfil_func;
    318  1.29  christos 		(*func)(pfh->pfil_arg, cmd, (void *)ifa);
    319  1.29  christos 	}
    320  1.29  christos }
    321  1.29  christos 
    322  1.29  christos void
    323  1.29  christos pfil_run_ifhooks(pfil_head_t *ph, u_long cmd, struct ifnet *ifp)
    324  1.29  christos {
    325  1.29  christos 	pfil_list_t *phlist = &ph->ph_ifevent;
    326  1.29  christos 
    327  1.29  christos 	for (u_int i = 0; i < phlist->nhooks; i++) {
    328  1.29  christos 		pfil_hook_t *pfh = &phlist->hooks[i];
    329  1.29  christos 		pfil_ifunc_t func = (pfil_ifunc_t)pfh->pfil_func;
    330  1.29  christos 		(*func)(pfh->pfil_arg, cmd, (void *)ifp);
    331  1.29  christos 	}
    332  1.29  christos }
    333