Home | History | Annotate | Line # | Download | only in net
pfil.c revision 1.9
      1 /*	$NetBSD: pfil.c,v 1.9 1999/10/10 09:07:32 mrg 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 TAILQ_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 static void pfil_init __P((void));
     49 static void pfil_list_add(pfil_list_t *,
     50     int (*) __P((void *, int, struct ifnet *, int, struct mbuf **)), int);
     51 static void pfil_list_remove(pfil_list_t *,
     52     int (*) __P((void *, int, struct ifnet *, int, struct mbuf **)));
     53 
     54 static void
     55 pfil_init()
     56 {
     57 
     58 	TAILQ_INIT(&pfil_in_list);
     59 	TAILQ_INIT(&pfil_out_list);
     60 	done_pfil_init = 1;
     61 }
     62 
     63 /*
     64  * pfil_add_hook() adds a function to the packet filter hook.  the
     65  * flags are:
     66  *	PFIL_IN		call me on incoming packets
     67  *	PFIL_OUT	call me on outgoing packets
     68  *	PFIL_ALL	call me on all of the above
     69  *	PFIL_WAITOK	OK to call malloc with M_WAITOK.
     70  */
     71 void
     72 pfil_add_hook(func, flags)
     73 	int	(*func) __P((void *, int, struct ifnet *, int,
     74 			     struct mbuf **));
     75 	int	flags;
     76 {
     77 
     78 	if (done_pfil_init == 0)
     79 		pfil_init();
     80 
     81 	if (flags & PFIL_IN)
     82 		pfil_list_add(&pfil_in_list, func, PFIL_IN |
     83 		    (flags & PFIL_WAITOK));
     84 	if (flags & PFIL_OUT)
     85 		pfil_list_add(&pfil_out_list, func, PFIL_OUT |
     86 		    (flags & PFIL_WAITOK));
     87 }
     88 
     89 static void
     90 pfil_list_add(list, func, flags)
     91 	pfil_list_t *list;
     92 	int	(*func) __P((void *, int, struct ifnet *, int,
     93 			     struct mbuf **));
     94 	int	flags;
     95 {
     96 	struct packet_filter_hook *pfh;
     97 
     98 	pfh = (struct packet_filter_hook *)malloc(sizeof(*pfh), M_IFADDR,
     99 	    flags & PFIL_WAITOK ? M_WAITOK : M_NOWAIT);
    100 	if (pfh == NULL)
    101 		panic("no memory for packet filter hook");
    102 
    103 	pfh->pfil_func = func;
    104 	/*
    105 	 * insert the input list in reverse order of the output list
    106 	 * so that the same path is followed in or out of the kernel.
    107 	 */
    108 	if (flags & PFIL_IN)
    109 		TAILQ_INSERT_HEAD(list, pfh, pfil_link);
    110 	else
    111 		TAILQ_INSERT_TAIL(list, pfh, pfil_link);
    112 }
    113 
    114 /*
    115  * pfil_remove_hook removes a specific function from the packet filter
    116  * hook list.
    117  */
    118 void
    119 pfil_remove_hook(func, flags)
    120 	int	(*func) __P((void *, int, struct ifnet *, int,
    121 			     struct mbuf **));
    122 	int	flags;
    123 {
    124 
    125 	if (done_pfil_init == 0)
    126 		pfil_init();
    127 
    128 	if (flags & PFIL_IN)
    129 		pfil_list_remove(&pfil_in_list, func);
    130 	if (flags & PFIL_OUT)
    131 		pfil_list_remove(&pfil_out_list, func);
    132 }
    133 
    134 /*
    135  * pfil_list_remove is an internal function that takes a function off the
    136  * specified list.
    137  */
    138 static void
    139 pfil_list_remove(list, func)
    140 	pfil_list_t *list;
    141 	int	(*func) __P((void *, int, struct ifnet *, int,
    142 			     struct mbuf **));
    143 {
    144 	struct packet_filter_hook *pfh;
    145 
    146 	for (pfh = list->tqh_first; pfh; pfh = pfh->pfil_link.tqe_next)
    147 		if (pfh->pfil_func == func) {
    148 			TAILQ_REMOVE(list, pfh, pfil_link);
    149 			free(pfh, M_IFADDR);
    150 			return;
    151 		}
    152 	printf("pfil_list_remove:  no function on list\n");
    153 #ifdef DIAGNOSTIC
    154 	panic("pfil_list_remove");
    155 #endif
    156 }
    157 
    158 struct packet_filter_hook *
    159 pfil_hook_get(flag)
    160 	int flag;
    161 {
    162 
    163 	if (done_pfil_init)
    164 		switch (flag) {
    165 		case PFIL_IN:
    166 			return (pfil_in_list.tqh_first);
    167 		case PFIL_OUT:
    168 			return (pfil_out_list.tqh_first);
    169 		}
    170 	return NULL;
    171 }
    172