$NetBSD: pfil.9,v 1.22 2003/07/01 13:04:06 wiz Exp $ Copyright (c) 1996 Matthew R. Green All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditio...
NAMEpfil pfil_head_register pfil_head_unregister pfil_head_get pfil_add_hook pfil_remove_hook pfil_run_hooks pfil_rlock pfil_runlock pfil_wlock pfil_wunlock - packet filter interface
SYNOPSISIn sys/param.h In sys/mbuf.h In net/if.h In net/pfil.h
typedef int (*pfil_func_t)(void *arg, struct mbuf **mp, struct ifnet *, int dir, struct inpcb); Ft int Fn pfil_head_register struct pfil_head *head Ft int Fn pfil_head_unregister struct pfil_head *head Ft struct pfil_head * Fn pfil_head_get int af u_long dlt Ft void Fn pfil_add_hook pfil_func_t void *arg int flags struct pfil_head * Ft void Fn pfil_remove_hook pfil_func_t void *arg int flags struct pfil_head * Ft int Fn pfil_run_hooks struct pfil_head *head struct mbuf **mp struct ifnet * int dir struct inpcb * Ft void Fn pfil_rlock struct pfil_head * struct rm_priotracker * Ft void Fn pfil_runlock struct pfil_head * struct rm_priotracker * Ft void Fn pfil_wlock struct pfil_head * Ft void Fn pfil_wunlock struct pfil_head *
DESCRIPTIONThe framework allows for a specified function to be invoked for every incoming or outgoing packet for a particular network I/O stream. These hooks may be used to implement a firewall or perform packet transformations.
Packet filtering points are registered with Fn pfil_head_register . Filtering points are identified by a key (Vt void * ) and a data link type (Vt int ) in the Vt pfil_head structure. Packet filters use the key and data link type to look up the filtering point with which they register themselves. The key is unique to the filtering point. The data link type is a bpf(4) DLT constant indicating what kind of header is present on the packet at the filtering point. Each filtering point uses common per-VNET rmlock by default. This can be changed by specifying Vt PFIL_FLAG_PRIVATE_LOCK as Vt flags field in the Vt pfil_head structure. Note that specifying private lock can break filters sharing the same ruleset and/or state between different data link types. Filtering points may be unregistered with the Fn pfil_head_unregister function.
Packet filters register/unregister themselves with a filtering point with the Fn pfil_add_hook and Fn pfil_remove_hook functions, respectively. The head is looked up using the Fn pfil_head_get function, which takes the key and data link type that the packet filter expects. Filters may provide an argument to be passed to the filter when invoked on a packet.
When a filter is invoked, the packet appears just as if it ``came off the wire'' That is, all protocol fields are in network byte order. The filter is called with its specified argument, the pointer to the pointer to the Vt mbuf containing the packet, the pointer to the network interface that the packet is traversing, and the direction ( PFIL_IN or PFIL_OUT that the packet is traveling. The filter may change which mbuf the Vt mbuf ** argument references. The filter returns an error (errno) if the packet processing is to stop, or 0 if the processing is to continue. If the packet processing is to stop, it is the responsibility of the filter to free the packet.
Every filter hook is called with read lock held. All heads uses the same lock within the same VNET instance. Packet filter can use this lock instead of own locking model to improve performance. Since uses rmlock(9) Fn pfil_rlock and Fn pfil_runlock require struct rm_priotracker to be passed as argument. Filter can acquire and release writer lock via Fn pfil_wlock and Fn pfil_wunlock functions. See rmlock(9) for more details.
FILTERING POINTSCurrently, filtering points are implemented for the following link types:
- IPv4 packets.
- IPv6 packets.
- Link-layer packets.
RETURN VALUESIf successful, Fn pfil_head_get returns the Vt pfil_head structure for the given key/dlt. The Fn pfil_add_hook and Fn pfil_remove_hook functions return 0 if successful. If called with flag PFIL_WAITOK Fn pfil_remove_hook is expected to always succeed.
The Fn pfil_head_unregister function might sleep!
SEE ALSObpf(4), if_bridge4, rmlock(9)
HISTORYThe interface first appeared in Nx 1.3 . The input and output lists were originally implemented as In sys/queue.h LIST structures; however this was changed in Nx 1.4 to TAILQ structures. This change was to allow the input and output filters to be processed in reverse order, to allow the same path to be taken, in or out of the kernel.
The interface was changed in 1.4T to accept a 3rd parameter to both Fn pfil_add_hook and Fn pfil_remove_hook , introducing the capability of per-protocol filtering. This was done primarily in order to support filtering of IPv6.
In 1.5K, the framework was changed to work with an arbitrary number of filtering points, as well as be less IP-centric.
Fine-grained locking was added in Fx 5.2 . lock export was added in Fx 10.0 .