Home | History | Annotate | Line # | Download | only in net
pfil.c revision 1.24.74.1
      1  1.24.74.1     yamt /*	$NetBSD: pfil.c,v 1.24.74.1 2009/05/04 08:14:15 yamt 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  *
     16        1.1      mrg  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17        1.1      mrg  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18        1.1      mrg  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19        1.1      mrg  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20        1.1      mrg  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     21        1.1      mrg  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     22        1.1      mrg  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     23        1.1      mrg  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     24        1.1      mrg  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25        1.1      mrg  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26        1.1      mrg  * SUCH DAMAGE.
     27        1.1      mrg  */
     28       1.20    lukem 
     29       1.20    lukem #include <sys/cdefs.h>
     30  1.24.74.1     yamt __KERNEL_RCSID(0, "$NetBSD: pfil.c,v 1.24.74.1 2009/05/04 08:14:15 yamt Exp $");
     31        1.1      mrg 
     32        1.1      mrg #include <sys/param.h>
     33        1.1      mrg #include <sys/errno.h>
     34        1.1      mrg #include <sys/malloc.h>
     35        1.1      mrg #include <sys/socket.h>
     36        1.1      mrg #include <sys/socketvar.h>
     37        1.1      mrg #include <sys/systm.h>
     38        1.1      mrg #include <sys/proc.h>
     39        1.1      mrg #include <sys/queue.h>
     40        1.1      mrg 
     41        1.1      mrg #include <net/if.h>
     42        1.1      mrg #include <net/pfil.h>
     43        1.1      mrg 
     44       1.13  darrenr static int pfil_list_add(pfil_list_t *,
     45       1.16  thorpej     int (*)(void *, struct mbuf **, struct ifnet *, int), void *, int);
     46       1.16  thorpej 
     47       1.13  darrenr static int pfil_list_remove(pfil_list_t *,
     48       1.16  thorpej     int (*)(void *, struct mbuf **, struct ifnet *, int), void *);
     49       1.16  thorpej 
     50       1.16  thorpej LIST_HEAD(, pfil_head) pfil_head_list =
     51       1.16  thorpej     LIST_HEAD_INITIALIZER(&pfil_head_list);
     52       1.16  thorpej 
     53       1.16  thorpej /*
     54       1.16  thorpej  * pfil_run_hooks() runs the specified packet filter hooks.
     55       1.16  thorpej  */
     56       1.16  thorpej int
     57       1.16  thorpej pfil_run_hooks(struct pfil_head *ph, struct mbuf **mp, struct ifnet *ifp,
     58       1.16  thorpej     int dir)
     59       1.16  thorpej {
     60       1.16  thorpej 	struct packet_filter_hook *pfh;
     61       1.21   itojun 	struct mbuf *m = NULL;
     62       1.16  thorpej 	int rv = 0;
     63       1.16  thorpej 
     64       1.22     yamt 	if ((dir & PFIL_ALL) && mp)
     65       1.21   itojun 		m = *mp;
     66       1.16  thorpej 	for (pfh = pfil_hook_get(dir, ph); pfh != NULL;
     67       1.16  thorpej 	     pfh = TAILQ_NEXT(pfh, pfil_link)) {
     68       1.16  thorpej 		if (pfh->pfil_func != NULL) {
     69       1.21   itojun 			if (pfh->pfil_flags & PFIL_ALL) {
     70       1.21   itojun 				rv = (*pfh->pfil_func)(pfh->pfil_arg, &m, ifp,
     71       1.21   itojun 				    dir);
     72       1.21   itojun 				if (rv != 0 || m == NULL)
     73       1.21   itojun 					break;
     74       1.21   itojun 			} else {
     75       1.21   itojun 				rv = (*pfh->pfil_func)(pfh->pfil_arg, mp, ifp,
     76       1.21   itojun 				    dir);
     77       1.21   itojun 				if (rv != 0)
     78       1.21   itojun 					break;
     79       1.21   itojun 			}
     80       1.16  thorpej 		}
     81       1.16  thorpej 	}
     82       1.16  thorpej 
     83       1.22     yamt 	if ((dir & PFIL_ALL) && mp)
     84       1.21   itojun 		*mp = m;
     85       1.16  thorpej 	return (rv);
     86       1.16  thorpej }
     87        1.1      mrg 
     88       1.16  thorpej /*
     89       1.16  thorpej  * pfil_head_register() registers a pfil_head with the packet filter
     90       1.16  thorpej  * hook mechanism.
     91       1.16  thorpej  */
     92       1.16  thorpej int
     93       1.16  thorpej pfil_head_register(struct pfil_head *ph)
     94        1.1      mrg {
     95       1.16  thorpej 	struct pfil_head *lph;
     96       1.16  thorpej 
     97  1.24.74.1     yamt 	LIST_FOREACH(lph, &pfil_head_list, ph_list) {
     98       1.19  thorpej 		if (ph->ph_type == lph->ph_type &&
     99       1.19  thorpej 		    ph->ph_un.phu_val == lph->ph_un.phu_val)
    100       1.16  thorpej 			return EEXIST;
    101       1.16  thorpej 	}
    102        1.7      mrg 
    103       1.10  darrenr 	TAILQ_INIT(&ph->ph_in);
    104       1.10  darrenr 	TAILQ_INIT(&ph->ph_out);
    105       1.21   itojun 	TAILQ_INIT(&ph->ph_ifaddr);
    106       1.23     yamt 	TAILQ_INIT(&ph->ph_ifnetevent);
    107       1.16  thorpej 
    108       1.16  thorpej 	LIST_INSERT_HEAD(&pfil_head_list, ph, ph_list);
    109       1.16  thorpej 
    110       1.16  thorpej 	return (0);
    111       1.16  thorpej }
    112       1.16  thorpej 
    113       1.16  thorpej /*
    114       1.16  thorpej  * pfil_head_unregister() removes a pfil_head from the packet filter
    115       1.16  thorpej  * hook mechanism.
    116       1.16  thorpej  */
    117       1.16  thorpej int
    118       1.16  thorpej pfil_head_unregister(struct pfil_head *pfh)
    119       1.16  thorpej {
    120       1.16  thorpej 
    121       1.16  thorpej 	LIST_REMOVE(pfh, ph_list);
    122       1.16  thorpej 	return (0);
    123       1.16  thorpej }
    124       1.16  thorpej 
    125       1.16  thorpej /*
    126       1.16  thorpej  * pfil_head_get() returns the pfil_head for a given key/dlt.
    127       1.16  thorpej  */
    128       1.16  thorpej struct pfil_head *
    129       1.19  thorpej pfil_head_get(int type, u_long val)
    130       1.16  thorpej {
    131       1.16  thorpej 	struct pfil_head *ph;
    132       1.16  thorpej 
    133  1.24.74.1     yamt 	LIST_FOREACH(ph, &pfil_head_list, ph_list) {
    134  1.24.74.1     yamt 		if (ph->ph_type == type && ph->ph_un.phu_val == val)
    135       1.16  thorpej 			break;
    136       1.16  thorpej 	}
    137       1.16  thorpej 
    138       1.16  thorpej 	return (ph);
    139        1.1      mrg }
    140        1.1      mrg 
    141        1.1      mrg /*
    142        1.1      mrg  * pfil_add_hook() adds a function to the packet filter hook.  the
    143        1.1      mrg  * flags are:
    144        1.1      mrg  *	PFIL_IN		call me on incoming packets
    145        1.1      mrg  *	PFIL_OUT	call me on outgoing packets
    146        1.1      mrg  *	PFIL_ALL	call me on all of the above
    147       1.21   itojun  *	PFIL_IFADDR  	call me on interface reconfig (mbuf ** is ioctl #)
    148       1.23     yamt  *	PFIL_IFNET  	call me on interface attach/detach
    149       1.23     yamt  *			(mbuf ** is PFIL_IFNET_*)
    150        1.1      mrg  *	PFIL_WAITOK	OK to call malloc with M_WAITOK.
    151        1.1      mrg  */
    152       1.13  darrenr int
    153       1.16  thorpej pfil_add_hook(int (*func)(void *, struct mbuf **, struct ifnet *, int),
    154       1.16  thorpej     void *arg, int flags, struct pfil_head *ph)
    155        1.1      mrg {
    156       1.13  darrenr 	int err = 0;
    157        1.1      mrg 
    158       1.16  thorpej 	if (flags & PFIL_IN) {
    159       1.16  thorpej 		err = pfil_list_add(&ph->ph_in, func, arg, flags & ~PFIL_OUT);
    160       1.16  thorpej 		if (err)
    161       1.16  thorpej 			return err;
    162       1.16  thorpej 	}
    163       1.16  thorpej 	if (flags & PFIL_OUT) {
    164       1.16  thorpej 		err = pfil_list_add(&ph->ph_out, func, arg, flags & ~PFIL_IN);
    165       1.16  thorpej 		if (err) {
    166       1.16  thorpej 			if (flags & PFIL_IN)
    167       1.16  thorpej 				pfil_list_remove(&ph->ph_in, func, arg);
    168       1.16  thorpej 			return err;
    169       1.16  thorpej 		}
    170       1.15  mycroft 	}
    171       1.21   itojun 	if (flags & PFIL_IFADDR) {
    172       1.23     yamt 		err = pfil_list_add(&ph->ph_ifaddr, func, arg, flags);
    173       1.21   itojun 		if (err) {
    174       1.21   itojun 			if (flags & PFIL_IN)
    175       1.21   itojun 				pfil_list_remove(&ph->ph_in, func, arg);
    176       1.21   itojun 			if (flags & PFIL_OUT)
    177       1.21   itojun 				pfil_list_remove(&ph->ph_out, func, arg);
    178       1.21   itojun 			return err;
    179       1.21   itojun 		}
    180       1.21   itojun 	}
    181       1.23     yamt 	if (flags & PFIL_IFNET) {
    182       1.23     yamt 		err = pfil_list_add(&ph->ph_ifnetevent, func, arg, flags);
    183       1.21   itojun 		if (err) {
    184       1.21   itojun 			if (flags & PFIL_IN)
    185       1.21   itojun 				pfil_list_remove(&ph->ph_in, func, arg);
    186       1.21   itojun 			if (flags & PFIL_OUT)
    187       1.21   itojun 				pfil_list_remove(&ph->ph_out, func, arg);
    188       1.21   itojun 			if (flags & PFIL_IFADDR)
    189       1.21   itojun 				pfil_list_remove(&ph->ph_ifaddr, func, arg);
    190       1.21   itojun 			return err;
    191       1.21   itojun 		}
    192       1.21   itojun 	}
    193       1.15  mycroft 	return 0;
    194        1.1      mrg }
    195        1.1      mrg 
    196       1.13  darrenr static int
    197       1.16  thorpej pfil_list_add(pfil_list_t *list,
    198       1.16  thorpej     int (*func)(void *, struct mbuf **, struct ifnet *, int), void *arg,
    199       1.16  thorpej     int flags)
    200        1.1      mrg {
    201        1.1      mrg 	struct packet_filter_hook *pfh;
    202        1.1      mrg 
    203       1.16  thorpej 	/*
    204       1.16  thorpej 	 * First make sure the hook is not already there.
    205       1.16  thorpej 	 */
    206  1.24.74.1     yamt 	TAILQ_FOREACH(pfh, list, pfil_link) {
    207  1.24.74.1     yamt 		if (pfh->pfil_func == func && pfh->pfil_arg == arg)
    208       1.16  thorpej 			return EEXIST;
    209       1.16  thorpej 	}
    210       1.16  thorpej 
    211        1.1      mrg 	pfh = (struct packet_filter_hook *)malloc(sizeof(*pfh), M_IFADDR,
    212       1.16  thorpej 	    (flags & PFIL_WAITOK) ? M_WAITOK : M_NOWAIT);
    213        1.1      mrg 	if (pfh == NULL)
    214       1.13  darrenr 		return ENOMEM;
    215       1.16  thorpej 
    216        1.1      mrg 	pfh->pfil_func = func;
    217       1.16  thorpej 	pfh->pfil_arg  = arg;
    218       1.21   itojun 	pfh->pfil_flags = flags;
    219       1.16  thorpej 
    220        1.7      mrg 	/*
    221        1.7      mrg 	 * insert the input list in reverse order of the output list
    222        1.7      mrg 	 * so that the same path is followed in or out of the kernel.
    223        1.7      mrg 	 */
    224       1.12  darrenr 	if (flags & PFIL_IN)
    225       1.12  darrenr 		TAILQ_INSERT_HEAD(list, pfh, pfil_link);
    226       1.12  darrenr 	else
    227       1.12  darrenr 		TAILQ_INSERT_TAIL(list, pfh, pfil_link);
    228       1.16  thorpej 
    229       1.13  darrenr 	return 0;
    230        1.1      mrg }
    231        1.1      mrg 
    232        1.1      mrg /*
    233        1.1      mrg  * pfil_remove_hook removes a specific function from the packet filter
    234        1.1      mrg  * hook list.
    235        1.1      mrg  */
    236       1.13  darrenr int
    237       1.16  thorpej pfil_remove_hook(int (*func)(void *, struct mbuf **, struct ifnet *, int),
    238       1.16  thorpej     void *arg, int flags, struct pfil_head *ph)
    239        1.1      mrg {
    240       1.13  darrenr 	int err = 0;
    241        1.1      mrg 
    242        1.1      mrg 	if (flags & PFIL_IN)
    243       1.16  thorpej 		err = pfil_list_remove(&ph->ph_in, func, arg);
    244       1.13  darrenr 	if ((err == 0) && (flags & PFIL_OUT))
    245       1.16  thorpej 		err = pfil_list_remove(&ph->ph_out, func, arg);
    246       1.21   itojun 	if ((err == 0) && (flags & PFIL_IFADDR))
    247       1.21   itojun 		err = pfil_list_remove(&ph->ph_ifaddr, func, arg);
    248       1.23     yamt 	if ((err == 0) && (flags & PFIL_IFNET))
    249       1.23     yamt 		err = pfil_list_remove(&ph->ph_ifnetevent, func, arg);
    250       1.13  darrenr 	return err;
    251        1.1      mrg }
    252        1.1      mrg 
    253        1.1      mrg /*
    254        1.1      mrg  * pfil_list_remove is an internal function that takes a function off the
    255        1.1      mrg  * specified list.
    256        1.1      mrg  */
    257       1.13  darrenr static int
    258       1.16  thorpej pfil_list_remove(pfil_list_t *list,
    259       1.16  thorpej     int (*func)(void *, struct mbuf **, struct ifnet *, int), void *arg)
    260        1.1      mrg {
    261        1.1      mrg 	struct packet_filter_hook *pfh;
    262        1.1      mrg 
    263  1.24.74.1     yamt 	TAILQ_FOREACH(pfh, list, pfil_link) {
    264       1.16  thorpej 		if (pfh->pfil_func == func && pfh->pfil_arg == arg) {
    265        1.9      mrg 			TAILQ_REMOVE(list, pfh, pfil_link);
    266        1.1      mrg 			free(pfh, M_IFADDR);
    267       1.13  darrenr 			return 0;
    268        1.1      mrg 		}
    269       1.16  thorpej 	}
    270       1.13  darrenr 	return ENOENT;
    271        1.1      mrg }
    272