Home | History | Annotate | Line # | Download | only in net
pfil.c revision 1.4
      1  1.4  christos /*	$NetBSD: pfil.c,v 1.4 1996/10/13 02:11:08 christos 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.1       mrg  * 3. All advertising materials mentioning features or use of this software
     16  1.1       mrg  *    must display the following acknowledgement:
     17  1.2       mrg  *      This product includes software developed by Matthew R. Green for
     18  1.2       mrg  *      the NetBSD Project.
     19  1.1       mrg  * 4. The name of the author may not be used to endorse or promote products
     20  1.1       mrg  *    derived from this software without specific prior written permission.
     21  1.1       mrg  *
     22  1.1       mrg  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     23  1.1       mrg  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     24  1.1       mrg  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     25  1.1       mrg  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     26  1.1       mrg  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     27  1.1       mrg  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     28  1.1       mrg  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     29  1.1       mrg  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     30  1.1       mrg  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31  1.1       mrg  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32  1.1       mrg  * SUCH DAMAGE.
     33  1.1       mrg  */
     34  1.1       mrg 
     35  1.1       mrg #include <sys/param.h>
     36  1.1       mrg #include <sys/errno.h>
     37  1.1       mrg #include <sys/malloc.h>
     38  1.1       mrg #include <sys/socket.h>
     39  1.1       mrg #include <sys/socketvar.h>
     40  1.1       mrg #include <sys/systm.h>
     41  1.1       mrg #include <sys/proc.h>
     42  1.1       mrg #include <sys/queue.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.1       mrg typedef LIST_HEAD(, packet_filter_hook) pfil_list_t;
     48  1.1       mrg pfil_list_t pfil_in_list;
     49  1.1       mrg pfil_list_t pfil_out_list;
     50  1.1       mrg pfil_list_t pfil_bad_list;
     51  1.1       mrg static int done_pfil_init;
     52  1.1       mrg 
     53  1.1       mrg void pfil_init __P((void));
     54  1.1       mrg void pfil_list_add(pfil_list_t *,
     55  1.1       mrg     int (*) __P((void *, int, struct ifnet *, int, struct mbuf **)), int);
     56  1.1       mrg void pfil_list_remove(struct packet_filter_hook *,
     57  1.1       mrg     int (*) __P((void *, int, struct ifnet *, int, struct mbuf **)));
     58  1.1       mrg 
     59  1.1       mrg void
     60  1.1       mrg pfil_init()
     61  1.1       mrg {
     62  1.1       mrg 	LIST_INIT(&pfil_in_list);
     63  1.1       mrg 	LIST_INIT(&pfil_out_list);
     64  1.1       mrg 	LIST_INIT(&pfil_bad_list);
     65  1.1       mrg 	done_pfil_init = 1;
     66  1.1       mrg }
     67  1.1       mrg 
     68  1.1       mrg /*
     69  1.1       mrg  * pfil_add_hook() adds a function to the packet filter hook.  the
     70  1.1       mrg  * flags are:
     71  1.1       mrg  *	PFIL_IN		call me on incoming packets
     72  1.1       mrg  *	PFIL_OUT	call me on outgoing packets
     73  1.1       mrg  *	PFIL_BAD	call me when rejecting a packet (that was
     74  1.1       mrg  *			not already reject by in/out filters).
     75  1.1       mrg  *	PFIL_ALL	call me on all of the above
     76  1.1       mrg  *	PFIL_WAITOK	OK to call malloc with M_WAITOK.
     77  1.1       mrg  */
     78  1.1       mrg void
     79  1.1       mrg pfil_add_hook(func, flags)
     80  1.1       mrg 	int	(*func) __P((void *, int, struct ifnet *, int,
     81  1.1       mrg 			     struct mbuf **));
     82  1.1       mrg 	int	flags;
     83  1.1       mrg {
     84  1.1       mrg 
     85  1.1       mrg 	if (done_pfil_init == 0)
     86  1.1       mrg 		pfil_init();
     87  1.1       mrg 
     88  1.1       mrg 	if (flags & PFIL_IN)
     89  1.1       mrg 		pfil_list_add(&pfil_in_list, func, flags);
     90  1.1       mrg 	if (flags & PFIL_OUT)
     91  1.1       mrg 		pfil_list_add(&pfil_out_list, func, flags);
     92  1.1       mrg 	if (flags & PFIL_BAD)
     93  1.1       mrg 		pfil_list_add(&pfil_bad_list, func, flags);
     94  1.1       mrg }
     95  1.1       mrg 
     96  1.1       mrg void
     97  1.1       mrg pfil_list_add(list, func, flags)
     98  1.1       mrg 	pfil_list_t *list;
     99  1.1       mrg 	int	(*func) __P((void *, int, struct ifnet *, int,
    100  1.1       mrg 			     struct mbuf **));
    101  1.1       mrg 	int	flags;
    102  1.1       mrg {
    103  1.1       mrg 	struct packet_filter_hook *pfh;
    104  1.1       mrg 
    105  1.1       mrg 	pfh = (struct packet_filter_hook *)malloc(sizeof(*pfh), M_IFADDR,
    106  1.1       mrg 	    flags & PFIL_WAITOK ? M_WAITOK : M_NOWAIT);
    107  1.1       mrg 	if (pfh == NULL)
    108  1.1       mrg 		panic("no memory for packet filter hook");
    109  1.1       mrg 
    110  1.1       mrg 	pfh->pfil_func = func;
    111  1.1       mrg 	LIST_INSERT_HEAD(list, pfh, pfil_link);
    112  1.1       mrg }
    113  1.1       mrg 
    114  1.1       mrg /*
    115  1.1       mrg  * pfil_remove_hook removes a specific function from the packet filter
    116  1.1       mrg  * hook list.
    117  1.1       mrg  */
    118  1.1       mrg void
    119  1.1       mrg pfil_remove_hook(func, flags)
    120  1.1       mrg 	int	(*func) __P((void *, int, struct ifnet *, int,
    121  1.1       mrg 			     struct mbuf **));
    122  1.1       mrg 	int	flags;
    123  1.1       mrg {
    124  1.1       mrg 
    125  1.1       mrg 	if (done_pfil_init == 0)
    126  1.1       mrg 		pfil_init();
    127  1.1       mrg 
    128  1.1       mrg 	if (flags & PFIL_IN)
    129  1.1       mrg 		pfil_list_remove(pfil_in_list.lh_first, func);
    130  1.1       mrg 	if (flags & PFIL_OUT)
    131  1.1       mrg 		pfil_list_remove(pfil_out_list.lh_first, func);
    132  1.1       mrg 	if (flags & PFIL_BAD)
    133  1.1       mrg 		pfil_list_remove(pfil_bad_list.lh_first, func);
    134  1.1       mrg }
    135  1.1       mrg 
    136  1.1       mrg /*
    137  1.1       mrg  * pfil_list_remove is an internal function that takes a function off the
    138  1.1       mrg  * specified list.
    139  1.1       mrg  */
    140  1.1       mrg void
    141  1.1       mrg pfil_list_remove(list, func)
    142  1.1       mrg 	struct packet_filter_hook *list;
    143  1.1       mrg 	int	(*func) __P((void *, int, struct ifnet *, int,
    144  1.1       mrg 			     struct mbuf **));
    145  1.1       mrg {
    146  1.1       mrg 	struct packet_filter_hook *pfh;
    147  1.1       mrg 
    148  1.1       mrg 	for (pfh = list; pfh; pfh = pfh->pfil_link.le_next)
    149  1.1       mrg 		if (pfh->pfil_func == func) {
    150  1.1       mrg 			LIST_REMOVE(pfh, pfil_link);
    151  1.1       mrg 			free(pfh, M_IFADDR);
    152  1.1       mrg 			return;
    153  1.1       mrg 		}
    154  1.4  christos 	printf("pfil_list_remove:  no function on list\n");
    155  1.1       mrg #ifdef DIAGNOSTIC
    156  1.1       mrg 	panic("pfil_list_remove");
    157  1.1       mrg #endif
    158  1.1       mrg }
    159  1.1       mrg 
    160  1.1       mrg struct packet_filter_hook *
    161  1.1       mrg pfil_hook_get(flag)
    162  1.1       mrg 	int flag;
    163  1.1       mrg {
    164  1.1       mrg 	if (done_pfil_init)
    165  1.1       mrg 		switch (flag) {
    166  1.1       mrg 		case PFIL_IN:
    167  1.1       mrg 			return (pfil_in_list.lh_first);
    168  1.1       mrg 		case PFIL_OUT:
    169  1.1       mrg 			return (pfil_out_list.lh_first);
    170  1.1       mrg 		case PFIL_BAD:
    171  1.1       mrg 			return (pfil_bad_list.lh_first);
    172  1.1       mrg 		}
    173  1.1       mrg 	return NULL;
    174  1.1       mrg }
    175