Home | History | Annotate | Line # | Download | only in net
pfil.c revision 1.14
      1 /*	$NetBSD: pfil.c,v 1.14 2000/02/22 11:30:22 darrenr 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 static void pfil_init __P((struct pfil_head *));
     44 static int pfil_list_add(pfil_list_t *,
     45     int (*) __P((void *, int, struct ifnet *, int, struct mbuf **)), int);
     46 static int pfil_list_remove(pfil_list_t *,
     47     int (*) __P((void *, int, struct ifnet *, int, struct mbuf **)));
     48 
     49 static void
     50 pfil_init(ph)
     51 	 struct pfil_head *ph;
     52 {
     53 
     54 	TAILQ_INIT(&ph->ph_in);
     55 	TAILQ_INIT(&ph->ph_out);
     56 	ph->ph_init = 1;
     57 }
     58 
     59 /*
     60  * pfil_add_hook() adds a function to the packet filter hook.  the
     61  * flags are:
     62  *	PFIL_IN		call me on incoming packets
     63  *	PFIL_OUT	call me on outgoing packets
     64  *	PFIL_ALL	call me on all of the above
     65  *	PFIL_WAITOK	OK to call malloc with M_WAITOK.
     66  */
     67 int
     68 pfil_add_hook(func, flags, ph)
     69 	int	(*func) __P((void *, int, struct ifnet *, int,
     70 			     struct mbuf **));
     71 	int	flags;
     72 	struct	pfil_head	*ph;
     73 {
     74 	int err = 0;
     75 
     76 	if (ph->ph_init == 0)
     77 		pfil_init(ph);
     78 
     79 	if (flags & PFIL_IN)
     80 		err = pfil_list_add(&ph->ph_in, func, flags & ~PFIL_OUT);
     81 	if ((err == 0) && (flags & PFIL_OUT))
     82 		err = pfil_list_add(&ph->ph_out, func, flags & ~PFIL_IN);
     83 	return err;
     84 }
     85 
     86 static int
     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 		return ENOMEM;
     99 	pfh->pfil_func = func;
    100 	/*
    101 	 * insert the input list in reverse order of the output list
    102 	 * so that the same path is followed in or out of the kernel.
    103 	 */
    104 
    105 	if (flags & PFIL_IN)
    106 		TAILQ_INSERT_HEAD(list, pfh, pfil_link);
    107 	else
    108 		TAILQ_INSERT_TAIL(list, pfh, pfil_link);
    109 	return 0;
    110 }
    111 
    112 /*
    113  * pfil_remove_hook removes a specific function from the packet filter
    114  * hook list.
    115  */
    116 int
    117 pfil_remove_hook(func, flags, ph)
    118 	int	(*func) __P((void *, int, struct ifnet *, int,
    119 			     struct mbuf **));
    120 	int	flags;
    121 	struct	pfil_head	*ph;
    122 {
    123 	int err = 0;
    124 
    125 	if (ph->ph_init == 0)
    126 		pfil_init(ph);
    127 
    128 	if (flags & PFIL_IN)
    129 		err = pfil_list_remove(&ph->ph_in, func);
    130 	if ((err == 0) && (flags & PFIL_OUT))
    131 		err = pfil_list_remove(&ph->ph_out, func);
    132 	return err;
    133 }
    134 
    135 /*
    136  * pfil_list_remove is an internal function that takes a function off the
    137  * specified list.
    138  */
    139 static int
    140 pfil_list_remove(list, func)
    141 	pfil_list_t *list;
    142 	int	(*func) __P((void *, int, struct ifnet *, int,
    143 			     struct mbuf **));
    144 {
    145 	struct packet_filter_hook *pfh;
    146 
    147 	for (pfh = list->tqh_first; pfh; pfh = pfh->pfil_link.tqe_next)
    148 		if (pfh->pfil_func == func) {
    149 			TAILQ_REMOVE(list, pfh, pfil_link);
    150 			free(pfh, M_IFADDR);
    151 			return 0;
    152 		}
    153 	return ENOENT;
    154 }
    155 
    156 struct packet_filter_hook *
    157 pfil_hook_get(flag, ph)
    158 	int flag;
    159 	struct	pfil_head	*ph;
    160 {
    161 	if (ph->ph_init != 0)
    162 		switch (flag) {
    163 		case PFIL_IN:
    164 			return (ph->ph_in.tqh_first);
    165 		case PFIL_OUT:
    166 			return (ph->ph_out.tqh_first);
    167 		}
    168 	return NULL;
    169 }
    170