pfil.h revision 248490
1120386Ssam/* $FreeBSD: head/sys/net/pfil.h 248490 2013-03-19 05:51:47Z ae $ */ 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> 39155226Scsjp#include <sys/lock.h> 40173904Smlaier#include <sys/rmlock.h> 4160317Sdarrenr 4260317Sdarrenrstruct mbuf; 4360317Sdarrenrstruct ifnet; 44135920Smlaierstruct inpcb; 4560317Sdarrenr 4660317Sdarrenr/* 4760317Sdarrenr * The packet filter hooks are designed for anything to call them to 4860317Sdarrenr * possibly intercept the packet. 4960317Sdarrenr */ 5060317Sdarrenrstruct packet_filter_hook { 5160938Sjake TAILQ_ENTRY(packet_filter_hook) pfil_link; 52186036Srwatson int (*pfil_func)(void *, struct mbuf **, struct ifnet *, int, 53198218Srwatson struct inpcb *); 54120386Ssam void *pfil_arg; 5560317Sdarrenr}; 5660317Sdarrenr 5760317Sdarrenr#define PFIL_IN 0x00000001 5860317Sdarrenr#define PFIL_OUT 0x00000002 5960317Sdarrenr#define PFIL_WAITOK 0x00000004 6060317Sdarrenr#define PFIL_ALL (PFIL_IN|PFIL_OUT) 6160317Sdarrenr 6260938Sjaketypedef TAILQ_HEAD(pfil_list, packet_filter_hook) pfil_list_t; 6360317Sdarrenr 64120386Ssam#define PFIL_TYPE_AF 1 /* key is AF_* type */ 65120386Ssam#define PFIL_TYPE_IFNET 2 /* key is ifnet pointer */ 66120386Ssam 67241888Smelifaro#define PFIL_FLAG_PRIVATE_LOCK 0x01 /* Personal lock instead of global */ 68241888Smelifaro 6960317Sdarrenrstruct pfil_head { 7060317Sdarrenr pfil_list_t ph_in; 7160317Sdarrenr pfil_list_t ph_out; 72120386Ssam int ph_type; 73155201Scsjp int ph_nhooks; 74210121Sluigi#if defined( __linux__ ) || defined( _WIN32 ) 75210121Sluigi rwlock_t ph_mtx; 76210121Sluigi#else 77241888Smelifaro struct rmlock *ph_plock; /* Pointer to the used lock */ 78241888Smelifaro struct rmlock ph_lock; /* Private lock storage */ 79241888Smelifaro int flags; 80210121Sluigi#endif 81120386Ssam union { 82120386Ssam u_long phu_val; 83120386Ssam void *phu_ptr; 84120386Ssam } ph_un; 85120386Ssam#define ph_af ph_un.phu_val 86120386Ssam#define ph_ifnet ph_un.phu_ptr 87120386Ssam LIST_ENTRY(pfil_head) ph_list; 8885305Sru}; 8960317Sdarrenr 90198218Srwatsonint pfil_add_hook(int (*func)(void *, struct mbuf **, struct ifnet *, 91198218Srwatson int, struct inpcb *), void *, int, struct pfil_head *); 92198218Srwatsonint pfil_remove_hook(int (*func)(void *, struct mbuf **, struct ifnet *, 93198218Srwatson int, struct inpcb *), void *, int, struct pfil_head *); 94120386Ssamint pfil_run_hooks(struct pfil_head *, struct mbuf **, struct ifnet *, 95135920Smlaier int, struct inpcb *inp); 9660317Sdarrenr 97241888Smelifarostruct rm_priotracker; /* Do not require including rmlock header */ 98241888Smelifaroint pfil_try_rlock(struct pfil_head *, struct rm_priotracker *); 99241888Smelifarovoid pfil_rlock(struct pfil_head *, struct rm_priotracker *); 100241888Smelifarovoid pfil_runlock(struct pfil_head *, struct rm_priotracker *); 101241888Smelifarovoid pfil_wlock(struct pfil_head *); 102241888Smelifarovoid pfil_wunlock(struct pfil_head *); 103241888Smelifaroint pfil_wowned(struct pfil_head *ph); 104241888Smelifaro 105120386Ssamint pfil_head_register(struct pfil_head *); 106120386Ssamint pfil_head_unregister(struct pfil_head *); 10760317Sdarrenr 108120386Ssamstruct pfil_head *pfil_head_get(int, u_long); 109120386Ssam 110170432Sgallatin#define PFIL_HOOKED(p) ((p)->ph_nhooks > 0) 111241888Smelifaro#define PFIL_LOCK_INIT_REAL(l, t) \ 112241888Smelifaro rm_init_flags(l, "PFil " t " rmlock", RM_RECURSE) 113241888Smelifaro#define PFIL_LOCK_DESTROY_REAL(l) \ 114241888Smelifaro rm_destroy(l) 115241888Smelifaro#define PFIL_LOCK_INIT(p) do { \ 116241888Smelifaro if ((p)->flags & PFIL_FLAG_PRIVATE_LOCK) { \ 117241888Smelifaro PFIL_LOCK_INIT_REAL(&(p)->ph_lock, "private"); \ 118241888Smelifaro (p)->ph_plock = &(p)->ph_lock; \ 119241888Smelifaro } else \ 120241888Smelifaro (p)->ph_plock = &V_pfil_lock; \ 121241888Smelifaro} while (0) 122241888Smelifaro#define PFIL_LOCK_DESTROY(p) do { \ 123241888Smelifaro if ((p)->flags & PFIL_FLAG_PRIVATE_LOCK) \ 124241888Smelifaro PFIL_LOCK_DESTROY_REAL((p)->ph_plock); \ 125241888Smelifaro} while (0) 126248490Sae#define PFIL_TRY_RLOCK(p, t) rm_try_rlock((p)->ph_plock, (t)) 127248490Sae#define PFIL_RLOCK(p, t) rm_rlock((p)->ph_plock, (t)) 128248490Sae#define PFIL_WLOCK(p) rm_wlock((p)->ph_plock) 129248490Sae#define PFIL_RUNLOCK(p, t) rm_runlock((p)->ph_plock, (t)) 130248490Sae#define PFIL_WUNLOCK(p) rm_wunlock((p)->ph_plock) 131248490Sae#define PFIL_WOWNED(p) rm_wowned((p)->ph_plock) 132248490Sae#define PFIL_LIST_LOCK() mtx_lock(&pfil_global_lock) 133248490Sae#define PFIL_LIST_UNLOCK() mtx_unlock(&pfil_global_lock) 134155201Scsjp 135120386Ssamstatic __inline struct packet_filter_hook * 136120386Ssampfil_hook_get(int dir, struct pfil_head *ph) 137120386Ssam{ 138198218Srwatson 139120386Ssam if (dir == PFIL_IN) 140120386Ssam return (TAILQ_FIRST(&ph->ph_in)); 141120386Ssam else if (dir == PFIL_OUT) 142120386Ssam return (TAILQ_FIRST(&ph->ph_out)); 143120386Ssam else 144120386Ssam return (NULL); 145120386Ssam} 146120386Ssam 14760317Sdarrenr#endif /* _NET_PFIL_H_ */ 148