Home | History | Annotate | Line # | Download | only in net
pfil.c revision 1.13
      1  1.13  darrenr /*	$NetBSD: pfil.c,v 1.13 2000/02/22 10:45:47 darrenr Exp $	*/
      2   1.1      mrg 
      3   1.1      mrg /*
      4   1.1      mrg  * Copyright (c) 1996 Matthew R. Green
      5   1.1      mrg  * All rights reserved.
      6   1.1      mrg  *
      7   1.1      mrg  * Redistribution and use in source and binary forms, with or without
      8   1.1      mrg  * modification, are permitted provided that the following conditions
      9   1.1      mrg  * are met:
     10   1.1      mrg  * 1. Redistributions of source code must retain the above copyright
     11   1.1      mrg  *    notice, this list of conditions and the following disclaimer.
     12   1.1      mrg  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1      mrg  *    notice, this list of conditions and the following disclaimer in the
     14   1.1      mrg  *    documentation and/or other materials provided with the distribution.
     15   1.6      mrg  * 3. The name of the author may not be used to endorse or promote products
     16   1.1      mrg  *    derived from this software without specific prior written permission.
     17   1.1      mrg  *
     18   1.1      mrg  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     19   1.1      mrg  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     20   1.1      mrg  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     21   1.1      mrg  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     22   1.1      mrg  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     23   1.1      mrg  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     24   1.1      mrg  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     25   1.1      mrg  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     26   1.1      mrg  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     27   1.1      mrg  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     28   1.1      mrg  * SUCH DAMAGE.
     29   1.1      mrg  */
     30   1.1      mrg 
     31   1.1      mrg #include <sys/param.h>
     32   1.1      mrg #include <sys/errno.h>
     33   1.1      mrg #include <sys/malloc.h>
     34   1.1      mrg #include <sys/socket.h>
     35   1.1      mrg #include <sys/socketvar.h>
     36   1.1      mrg #include <sys/systm.h>
     37   1.1      mrg #include <sys/proc.h>
     38   1.1      mrg #include <sys/queue.h>
     39   1.1      mrg 
     40   1.1      mrg #include <net/if.h>
     41   1.1      mrg #include <net/pfil.h>
     42   1.1      mrg 
     43  1.10  darrenr static void pfil_init __P((struct pfil_head *));
     44  1.13  darrenr static int pfil_list_add(pfil_list_t *,
     45   1.1      mrg     int (*) __P((void *, int, struct ifnet *, int, struct mbuf **)), int);
     46  1.13  darrenr static int pfil_list_remove(pfil_list_t *,
     47   1.1      mrg     int (*) __P((void *, int, struct ifnet *, int, struct mbuf **)));
     48   1.1      mrg 
     49   1.7      mrg static void
     50  1.10  darrenr pfil_init(ph)
     51  1.10  darrenr 	 struct pfil_head *ph;
     52   1.1      mrg {
     53   1.7      mrg 
     54  1.10  darrenr 	TAILQ_INIT(&ph->ph_in);
     55  1.10  darrenr 	TAILQ_INIT(&ph->ph_out);
     56  1.10  darrenr 	ph->ph_init = 1;
     57   1.1      mrg }
     58   1.1      mrg 
     59   1.1      mrg /*
     60   1.1      mrg  * pfil_add_hook() adds a function to the packet filter hook.  the
     61   1.1      mrg  * flags are:
     62   1.1      mrg  *	PFIL_IN		call me on incoming packets
     63   1.1      mrg  *	PFIL_OUT	call me on outgoing packets
     64   1.1      mrg  *	PFIL_ALL	call me on all of the above
     65   1.1      mrg  *	PFIL_WAITOK	OK to call malloc with M_WAITOK.
     66   1.1      mrg  */
     67  1.13  darrenr int
     68  1.11  darrenr pfil_add_hook(func, flags, ph)
     69   1.1      mrg 	int	(*func) __P((void *, int, struct ifnet *, int,
     70   1.1      mrg 			     struct mbuf **));
     71   1.1      mrg 	int	flags;
     72  1.11  darrenr 	struct	pfil_head	*ph;
     73   1.1      mrg {
     74  1.13  darrenr 	int err = 0;
     75   1.1      mrg 
     76  1.10  darrenr 	if (ph->ph_init == 0)
     77  1.10  darrenr 		pfil_init(ph);
     78   1.1      mrg 
     79   1.1      mrg 	if (flags & PFIL_IN)
     80  1.13  darrenr 		err = pfil_list_add(&ph->ph_in, func, flags);
     81  1.13  darrenr 	if ((err == 0) && (flags & PFIL_OUT))
     82  1.13  darrenr 		err = pfil_list_add(&ph->ph_out, func, flags);
     83  1.13  darrenr 	return err;
     84   1.1      mrg }
     85   1.1      mrg 
     86  1.13  darrenr static int
     87   1.1      mrg pfil_list_add(list, func, flags)
     88   1.1      mrg 	pfil_list_t *list;
     89   1.1      mrg 	int	(*func) __P((void *, int, struct ifnet *, int,
     90   1.1      mrg 			     struct mbuf **));
     91  1.10  darrenr 	int flags;
     92   1.1      mrg {
     93   1.1      mrg 	struct packet_filter_hook *pfh;
     94   1.1      mrg 
     95   1.1      mrg 	pfh = (struct packet_filter_hook *)malloc(sizeof(*pfh), M_IFADDR,
     96   1.1      mrg 	    flags & PFIL_WAITOK ? M_WAITOK : M_NOWAIT);
     97   1.1      mrg 	if (pfh == NULL)
     98  1.13  darrenr 		return ENOMEM;
     99   1.1      mrg 	pfh->pfil_func = func;
    100   1.7      mrg 	/*
    101   1.7      mrg 	 * insert the input list in reverse order of the output list
    102   1.7      mrg 	 * so that the same path is followed in or out of the kernel.
    103   1.7      mrg 	 */
    104  1.12  darrenr 
    105  1.12  darrenr 	if (flags & PFIL_IN)
    106  1.12  darrenr 		TAILQ_INSERT_HEAD(list, pfh, pfil_link);
    107  1.12  darrenr 	else
    108  1.12  darrenr 		TAILQ_INSERT_TAIL(list, pfh, pfil_link);
    109  1.13  darrenr 	return 0;
    110   1.1      mrg }
    111   1.1      mrg 
    112   1.1      mrg /*
    113   1.1      mrg  * pfil_remove_hook removes a specific function from the packet filter
    114   1.1      mrg  * hook list.
    115   1.1      mrg  */
    116  1.13  darrenr int
    117  1.11  darrenr pfil_remove_hook(func, flags, ph)
    118   1.1      mrg 	int	(*func) __P((void *, int, struct ifnet *, int,
    119   1.1      mrg 			     struct mbuf **));
    120   1.1      mrg 	int	flags;
    121  1.11  darrenr 	struct	pfil_head	*ph;
    122   1.1      mrg {
    123  1.13  darrenr 	int err = 0;
    124   1.1      mrg 
    125  1.10  darrenr 	if (ph->ph_init == 0)
    126  1.10  darrenr 		pfil_init(ph);
    127   1.1      mrg 
    128   1.1      mrg 	if (flags & PFIL_IN)
    129  1.13  darrenr 		err = pfil_list_remove(&ph->ph_in, func);
    130  1.13  darrenr 	if ((err == 0) && (flags & PFIL_OUT))
    131  1.13  darrenr 		err = pfil_list_remove(&ph->ph_out, func);
    132  1.13  darrenr 	return err;
    133   1.1      mrg }
    134   1.1      mrg 
    135   1.1      mrg /*
    136   1.1      mrg  * pfil_list_remove is an internal function that takes a function off the
    137   1.1      mrg  * specified list.
    138   1.1      mrg  */
    139  1.13  darrenr static int
    140   1.1      mrg pfil_list_remove(list, func)
    141   1.9      mrg 	pfil_list_t *list;
    142   1.1      mrg 	int	(*func) __P((void *, int, struct ifnet *, int,
    143   1.1      mrg 			     struct mbuf **));
    144   1.1      mrg {
    145   1.1      mrg 	struct packet_filter_hook *pfh;
    146   1.1      mrg 
    147   1.9      mrg 	for (pfh = list->tqh_first; pfh; pfh = pfh->pfil_link.tqe_next)
    148   1.1      mrg 		if (pfh->pfil_func == func) {
    149   1.9      mrg 			TAILQ_REMOVE(list, pfh, pfil_link);
    150   1.1      mrg 			free(pfh, M_IFADDR);
    151  1.13  darrenr 			return 0;
    152   1.1      mrg 		}
    153  1.13  darrenr 	return ENOENT;
    154   1.1      mrg }
    155   1.1      mrg 
    156   1.1      mrg struct packet_filter_hook *
    157  1.11  darrenr pfil_hook_get(flag, ph)
    158   1.1      mrg 	int flag;
    159  1.11  darrenr 	struct	pfil_head	*ph;
    160   1.1      mrg {
    161  1.10  darrenr 	if (ph->ph_init != 0)
    162   1.1      mrg 		switch (flag) {
    163   1.1      mrg 		case PFIL_IN:
    164  1.10  darrenr 			return (ph->ph_in.tqh_first);
    165   1.1      mrg 		case PFIL_OUT:
    166  1.10  darrenr 			return (ph->ph_out.tqh_first);
    167   1.1      mrg 		}
    168   1.1      mrg 	return NULL;
    169   1.1      mrg }
    170