NG_BPF(4)
π NG_BPF β Berkeley packet filter netgraph node type π
okay bestie, gather round, because today we're talking about ng_bpf β the Kernel Interfaces Manual girlie who lets you run Berkeley Packet Filter magic on data zooming through a Netgraph network. she's giving filter vibes and she's so good at her job. no cap. π¦
β¨ SYNOPSIS β¨
#include <sys/types.h>
#include <net/bpf.h>
#include <netgraph.h>
#include <netgraph/ng_bpf.h>
πΈ DESCRIPTION πΈ
so the bpf node type lets you apply Berkeley Packet Filter filters (that's bpf(4), iykyk) to data travelling through a Netgraph network. each node can have an arbitrary number of connections to arbitrarily named hooks β yes, you read that right, go wild with the names. π
with each hook you get:
- a
bpf(4)filter program, applied to incoming data only (not outgoing, she's got boundaries) - a destination hook for matching packets
- a destination hook for non-matching packets
- and a whole lil collection of statistics counters β¨
a bpf(4) program returns an unsigned integer, which normally gets interpreted as the length of the prefix of the packet to return. in this node type's context tho:
- returning zero = a non-match, and the entire packet gets delivered out the non-match destination hook
- returning anything greater than zero = the packet gets truncated to that length and delivered out the match destination hook
either or both destination hooks may be the empty string, or may just not exist at all β in which case, bestie, the packet is dropped. π
new hooks start out configured to drop all packets. a new filter program can be installed using the NGM_BPF_SET_PROGRAM control message. slay. π
πͺ HOOKS πͺ
this node type supports any number of hooks having arbitrary names. periodt.
π CONTROL MESSAGES π
this node type supports the generic control messages, plus these icons:
NGM_BPF_SET_PROGRAM(setprogram) β sets the filter program that will be applied to incoming data on a hook. you gotta supply the following structure as an argument:
struct ng_bpf_hookprog {
char thisHook[NG_HOOKSIZ]; /* name of hook */
char ifMatch[NG_HOOKSIZ]; /* match dest hook */
char ifNotMatch[NG_HOOKSIZ]; /* !match dest hook */
int32_t bpf_prog_len; /* #insns in program */
struct bpf_insn bpf_prog[]; /* bpf program */
};
the hook to be updated is specified in thisHook. the BPF program is the sequence of instructions in the bpf_prog array, and there must be bpf_prog_len of them β count them, bestie, don't be sloppy. matching incoming packets get delivered out the hook named ifMatch, and non-matching ones go out ifNotMatch. the program must be a valid bpf(4) program, or else EINVAL gets returned. π¨
NGM_BPF_GET_PROGRAM(getprogram) β this command takes an ASCII string argument (the hook name) and returns the correspondingstruct ng_bpf_hookprogas shown above.NGM_BPF_GET_STATS(getstats) β takes an ASCII string argument, the hook name, and returns the statistics associated with the hook as astruct ng_bpf_hookstat.NGM_BPF_CLR_STATS(clrstats) β takes an ASCII string argument, the hook name, and clears the statistics associated with the hook. clean slate energy. β¨NGM_BPF_GETCLR_STATS(getclrstats) β identical toNGM_BPF_GET_STATS, except the statistics are also atomically cleared. two things at once? she's multitasking, she's iconic. π
π¦ SHUTDOWN π¦
this node shuts down when it receives a NGM_SHUTDOWN control message, or when all hooks have been disconnected. bye bestie π
π EXAMPLES π
you can totally configure a node straight from the command line β use tcpdump(1) to generate raw BPF instructions, then feed those into an awk(1) script to create the ASCII form of a NGM_BPF_SET_PROGRAM control message. like this, watch:
#!/bin/sh
PATTERN="tcp dst port 80"
NODEPATH="my_node:"
INHOOK="hook1"
MATCHHOOK="hook2"
NOTMATCHHOOK="hook3"
BPFPROG=$( tcpdump -s 8192 -p -ddd ${PATTERN} | \
( read len ; \
echo -n "bpf_prog_len=$len" ; \
echo -n "bpf_prog=[" ; \
while read code jt jf k ; do \
echo -n " { code=$code jt=$jt jf=$jf k=$k }" ; \
done ; \
echo " ]" ) )
ngctl msg ${NODEPATH} setprogram { thisHook=\"${INHOOK}\" \
ifMatch=\"${MATCHHOOK}\" \
ifNotMatch=\"${NOTMATCHHOOK}\" \
${BPFPROG} }
ate that fr fr. π₯
π SEE ALSO π
β HISTORY β
the ng_bpf node type was implemented in FreeBSD 4.0. she's been thriving for a minute. π
π AUTHORS π
Archie Cobbs <archie@FreeBSD.org> β thank you for your service, king π«Ά
π BUGS π
okay so here's the tea. when built as a loadable kernel module, this module includes the file net/bpf_filter.c. loading the module should fail if net/bpf_filter.c already exists in the kernel, but currently it does not, and the duplicate copies of the file do not interfere. however, this may change in the future. so likeβ¦ noted. π
cute lil sign-off: this page came to you from Debian, dated November 13, 2012. mwah π
π¦ where this page comes from
- debian bookworm: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- debian trixie: freebsd-manpages 12.2-2 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu jammy: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu noble: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu questing: freebsd-manpages 12.2-2
- ubuntu resolute: freebsd-manpages 12.2-2build1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu stonking: freebsd-manpages 12.2-2build1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- Copyright (c) 1999 Whistle Communications, Inc
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms