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