- NetBSD Manual Pages
PFIL(9) NetBSD Kernel Developer's Manual PFIL(9)
Powered by man-cgi (2021-06-01).
Maintained for NetBSD
by Kimmo Suominen.
Based on man-cgi by Panagiotis Christias.
pfil, pfil_head_register, pfil_head_unregister, pfil_head_get,
pfil_hook_get, pfil_add_hook, pfil_remove_hook, pfil_run_hooks -- packet
pfil_head_register(struct pfil_head *ph);
pfil_head_unregister(struct pfil_head *ph);
struct pfil_head *
pfil_head_get(int af, u_long dlt);
struct packet_filter_hook *
pfil_hook_get(int dir, struct pfil_head *ph);
pfil_add_hook(int (*func)(), void *arg, int flags, struct pfil_head *ph);
pfil_remove_hook(int (*func)(), void *arg, int flags,
struct pfil_head *ph);
(*func)(void *arg, struct mbuf **mp, struct ifnet *, int dir);
pfil_run_hooks(struct pfil_head *ph, struct mbuf **mp, struct ifnet *ifp,
The pfil 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 trans-
Packet filtering points are registered with pfil_head_register(). Fil-
tering points are identified by a key (void *) and a data link type (int)
in the 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. Filtering points may be unregistered with the
Packet filters register/unregister themselves with a filtering point with
the pfil_add_hook() and pfil_remove_hook() functions, respectively. The
head is looked up using the 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 mbuf containing the packet, the pointer to the network interface
that the packet is traversing, and the direction (PFIL_IN or PFIL_OUT,
see also below) that the packet is traveling. The filter may change
which mbuf the mbuf ** argument references. The filter returns an errno
if the packet processing is to stop, or 0 if the processing is to con-
tinue. If the packet processing is to stop, it is the responsibility of
the filter to free the packet.
The flags parameter, used in the pfil_add_hook() and pfil_remove_hook()
functions, indicates when the filter should be called. The flags are:
PFIL_IN call me on incoming packets
PFIL_OUT call me on outgoing packets
PFIL_ALL call me on all of the above
PFIL_IFADDR call me on interface reconfig (mbuf ** is ioctl #)
PFIL_IFNET call me on interface attach/detach (mbuf ** is either
PFIL_IFNET_ATTACH or PFIL_IFNET_DETACH)
PFIL_WAITOK OK to call malloc with M_WAITOK.
The pfil interface is enabled in the kernel via the PFIL_HOOKS option.
The pfil interface first appeared in NetBSD 1.3. The pfil input and out-
put lists were originally implemented as <sys/queue.h> LIST structures;
however this was changed in NetBSD 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 pfil interface was changed in 1.4T to accept a 3rd parameter to both
pfil_add_hook() and pfil_remove_hook(), introducing the capability of
per-protocol filtering. This was done primarily in order to support fil-
tering of IPv6.
In 1.5K, the pfil framework was changed to work with an arbitrary number
of filtering points, as well as be less IP-centric.
The pfil interface was designed and implemented by Matthew R. Green, with
help from Darren Reed, Jason R. Thorpe and Charles M. Hannum. Darren
Reed added support for IPv6 in addition to IPv4. Jason R. Thorpe added
support for multiple hooks and other clean up.
The current pfil implementation will need changes to suit a threaded ker-
NetBSD 6.0 January 8, 2006 NetBSD 6.0