pfil.h revision 139823
1120386Ssam/*	$FreeBSD: head/sys/net/pfil.h 139823 2005-01-07 01:45:51Z imp $ */
2120386Ssam/*	$NetBSD: pfil.h,v 1.22 2003/06/23 12:57:08 martin Exp $	*/
360317Sdarrenr
4139823Simp/*-
560317Sdarrenr * Copyright (c) 1996 Matthew R. Green
660317Sdarrenr * All rights reserved.
760317Sdarrenr *
860317Sdarrenr * Redistribution and use in source and binary forms, with or without
960317Sdarrenr * modification, are permitted provided that the following conditions
1060317Sdarrenr * are met:
1160317Sdarrenr * 1. Redistributions of source code must retain the above copyright
1260317Sdarrenr *    notice, this list of conditions and the following disclaimer.
1360317Sdarrenr * 2. Redistributions in binary form must reproduce the above copyright
1460317Sdarrenr *    notice, this list of conditions and the following disclaimer in the
1560317Sdarrenr *    documentation and/or other materials provided with the distribution.
1660317Sdarrenr * 3. The name of the author may not be used to endorse or promote products
1760317Sdarrenr *    derived from this software without specific prior written permission.
1860317Sdarrenr *
1960317Sdarrenr * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
2060317Sdarrenr * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
2160317Sdarrenr * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
2260317Sdarrenr * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
2360317Sdarrenr * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
2460317Sdarrenr * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
2560317Sdarrenr * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
2660317Sdarrenr * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
2760317Sdarrenr * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2860317Sdarrenr * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2960317Sdarrenr * SUCH DAMAGE.
3060317Sdarrenr */
3160317Sdarrenr
3260317Sdarrenr#ifndef _NET_PFIL_H_
3360317Sdarrenr#define _NET_PFIL_H_
3460317Sdarrenr
35120386Ssam#include <sys/systm.h>
36130731Sbde#include <sys/queue.h>
37120386Ssam#include <sys/_lock.h>
38120386Ssam#include <sys/_mutex.h>
39120386Ssam#include <sys/condvar.h>	/* XXX */
4060317Sdarrenr
4160317Sdarrenrstruct mbuf;
4260317Sdarrenrstruct ifnet;
43135920Smlaierstruct inpcb;
4460317Sdarrenr
4560317Sdarrenr/*
4660317Sdarrenr * The packet filter hooks are designed for anything to call them to
4760317Sdarrenr * possibly intercept the packet.
4860317Sdarrenr */
4960317Sdarrenrstruct packet_filter_hook {
5060938Sjake        TAILQ_ENTRY(packet_filter_hook) pfil_link;
51135920Smlaier	int	(*pfil_func)(void *, struct mbuf **, struct ifnet *, int, struct inpcb *);
52120386Ssam	void	*pfil_arg;
5360317Sdarrenr	int	pfil_flags;
5460317Sdarrenr};
5560317Sdarrenr
5660317Sdarrenr#define PFIL_IN		0x00000001
5760317Sdarrenr#define PFIL_OUT	0x00000002
5860317Sdarrenr#define PFIL_WAITOK	0x00000004
5960317Sdarrenr#define PFIL_ALL	(PFIL_IN|PFIL_OUT)
6060317Sdarrenr
6160938Sjaketypedef	TAILQ_HEAD(pfil_list, packet_filter_hook) pfil_list_t;
6260317Sdarrenr
63120386Ssam#define	PFIL_TYPE_AF		1	/* key is AF_* type */
64120386Ssam#define	PFIL_TYPE_IFNET		2	/* key is ifnet pointer */
65120386Ssam
6660317Sdarrenrstruct pfil_head {
6760317Sdarrenr	pfil_list_t	ph_in;
6860317Sdarrenr	pfil_list_t	ph_out;
69120386Ssam	int		ph_type;
70120386Ssam	/*
71120386Ssam	 * Locking: use a busycounter per pfil_head.
72120386Ssam	 * Use ph_busy_count = -1 to indicate pfil_head is empty.
73120386Ssam	 */
74120386Ssam	int		ph_busy_count;	/* count of threads with read lock */
75120386Ssam	int		ph_want_write;	/* want write lock flag */
76120386Ssam	struct cv	ph_cv;		/* for waking up writers */
77120386Ssam	struct mtx	ph_mtx;		/* mutex on locking state */
78120386Ssam	union {
79120386Ssam		u_long		phu_val;
80120386Ssam		void		*phu_ptr;
81120386Ssam	} ph_un;
82120386Ssam#define	ph_af		ph_un.phu_val
83120386Ssam#define	ph_ifnet	ph_un.phu_ptr
84120386Ssam	LIST_ENTRY(pfil_head) ph_list;
8585305Sru};
8660317Sdarrenr
87120386Ssamint	pfil_run_hooks(struct pfil_head *, struct mbuf **, struct ifnet *,
88135920Smlaier	    int, struct inpcb *inp);
8960317Sdarrenr
90120386Ssamint	pfil_add_hook(int (*func)(void *, struct mbuf **,
91135920Smlaier	    struct ifnet *, int, struct inpcb *), void *, int, struct pfil_head *);
92120386Ssamint	pfil_remove_hook(int (*func)(void *, struct mbuf **,
93135920Smlaier	    struct ifnet *, int, struct inpcb *), void *, int, struct pfil_head *);
9460317Sdarrenr
95120386Ssamint	pfil_head_register(struct pfil_head *);
96120386Ssamint	pfil_head_unregister(struct pfil_head *);
9760317Sdarrenr
98120386Ssamstruct pfil_head *pfil_head_get(int, u_long);
99120386Ssam
100120386Ssamstatic __inline struct packet_filter_hook *
101120386Ssampfil_hook_get(int dir, struct pfil_head *ph)
102120386Ssam{
103120386Ssam	KASSERT(ph->ph_busy_count > 0,
104120386Ssam	    ("pfil_hook_get: called on unbusy pfil_head"));
105120386Ssam	if (dir == PFIL_IN)
106120386Ssam		return (TAILQ_FIRST(&ph->ph_in));
107120386Ssam	else if (dir == PFIL_OUT)
108120386Ssam		return (TAILQ_FIRST(&ph->ph_out));
109120386Ssam	else
110120386Ssam		return (NULL);
111120386Ssam}
112120386Ssam
11360317Sdarrenr#endif /* _NET_PFIL_H_ */
114