xref: /freebsd/sys/netgraph/ng_bpf.c (revision f8aae7776f85d2fa8aa93f73c37782cd9e1204c6)
192a3e552SArchie Cobbs 
292a3e552SArchie Cobbs /*
392a3e552SArchie Cobbs  * ng_bpf.c
492a3e552SArchie Cobbs  *
592a3e552SArchie Cobbs  * Copyright (c) 1999 Whistle Communications, Inc.
692a3e552SArchie Cobbs  * All rights reserved.
792a3e552SArchie Cobbs  *
892a3e552SArchie Cobbs  * Subject to the following obligations and disclaimer of warranty, use and
992a3e552SArchie Cobbs  * redistribution of this software, in source or object code forms, with or
1092a3e552SArchie Cobbs  * without modifications are expressly permitted by Whistle Communications;
1192a3e552SArchie Cobbs  * provided, however, that:
1292a3e552SArchie Cobbs  * 1. Any and all reproductions of the source or object code must include the
1392a3e552SArchie Cobbs  *    copyright notice above and the following disclaimer of warranties; and
1492a3e552SArchie Cobbs  * 2. No rights are granted, in any manner or form, to use Whistle
1592a3e552SArchie Cobbs  *    Communications, Inc. trademarks, including the mark "WHISTLE
1692a3e552SArchie Cobbs  *    COMMUNICATIONS" on advertising, endorsements, or otherwise except as
1792a3e552SArchie Cobbs  *    such appears in the above copyright notice or in the software.
1892a3e552SArchie Cobbs  *
1992a3e552SArchie Cobbs  * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
2092a3e552SArchie Cobbs  * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
2192a3e552SArchie Cobbs  * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
2292a3e552SArchie Cobbs  * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
2392a3e552SArchie Cobbs  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
24d2a57575SJulian Elischer  * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
2592a3e552SArchie Cobbs  * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
2692a3e552SArchie Cobbs  * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
2792a3e552SArchie Cobbs  * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES
2892a3e552SArchie Cobbs  * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
2992a3e552SArchie Cobbs  * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
3092a3e552SArchie Cobbs  * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
3192a3e552SArchie Cobbs  * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
3292a3e552SArchie Cobbs  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
3392a3e552SArchie Cobbs  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
3492a3e552SArchie Cobbs  * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
3592a3e552SArchie Cobbs  * OF SUCH DAMAGE.
3692a3e552SArchie Cobbs  *
37cc3bbd68SJulian Elischer  * Author: Archie Cobbs <archie@freebsd.org>
3892a3e552SArchie Cobbs  *
3992a3e552SArchie Cobbs  * $FreeBSD$
4092a3e552SArchie Cobbs  * $Whistle: ng_bpf.c,v 1.3 1999/12/03 20:30:23 archie Exp $
4192a3e552SArchie Cobbs  */
4292a3e552SArchie Cobbs 
4392a3e552SArchie Cobbs /*
4492a3e552SArchie Cobbs  * BPF NETGRAPH NODE TYPE
4592a3e552SArchie Cobbs  *
4692a3e552SArchie Cobbs  * This node type accepts any number of hook connections.  With each hook
4792a3e552SArchie Cobbs  * is associated a bpf(4) filter program, and two hook names (each possibly
4892a3e552SArchie Cobbs  * the empty string).  Incoming packets are compared against the filter;
4992a3e552SArchie Cobbs  * matching packets are delivered out the first named hook (or dropped if
5092a3e552SArchie Cobbs  * the empty string), and non-matching packets are delivered out the second
5192a3e552SArchie Cobbs  * named hook (or dropped if the empty string).
5292a3e552SArchie Cobbs  *
5392a3e552SArchie Cobbs  * Each hook also keeps statistics about how many packets have matched, etc.
5492a3e552SArchie Cobbs  */
5592a3e552SArchie Cobbs 
5692a3e552SArchie Cobbs #include <sys/param.h>
5792a3e552SArchie Cobbs #include <sys/systm.h>
5892a3e552SArchie Cobbs #include <sys/errno.h>
5992a3e552SArchie Cobbs #include <sys/kernel.h>
6092a3e552SArchie Cobbs #include <sys/malloc.h>
6192a3e552SArchie Cobbs #include <sys/mbuf.h>
6292a3e552SArchie Cobbs 
6392a3e552SArchie Cobbs #include <net/bpf.h>
6492a3e552SArchie Cobbs 
6592a3e552SArchie Cobbs #include <netgraph/ng_message.h>
6692a3e552SArchie Cobbs #include <netgraph/netgraph.h>
6792a3e552SArchie Cobbs #include <netgraph/ng_parse.h>
6892a3e552SArchie Cobbs #include <netgraph/ng_bpf.h>
6992a3e552SArchie Cobbs 
709c8c302fSJulian Elischer #ifdef NG_SEPARATE_MALLOC
719c8c302fSJulian Elischer MALLOC_DEFINE(M_NETGRAPH_BPF, "netgraph_bpf", "netgraph bpf node ");
729c8c302fSJulian Elischer #else
739c8c302fSJulian Elischer #define M_NETGRAPH_BPF M_NETGRAPH
749c8c302fSJulian Elischer #endif
759c8c302fSJulian Elischer 
7692a3e552SArchie Cobbs #define OFFSETOF(s, e) ((char *)&((s *)0)->e - (char *)((s *)0))
7792a3e552SArchie Cobbs 
7892a3e552SArchie Cobbs #define ERROUT(x)	do { error = (x); goto done; } while (0)
7992a3e552SArchie Cobbs 
8092a3e552SArchie Cobbs /* Per hook private info */
8192a3e552SArchie Cobbs struct ng_bpf_hookinfo {
8292a3e552SArchie Cobbs 	node_p			node;
8392a3e552SArchie Cobbs 	hook_p			hook;
8492a3e552SArchie Cobbs 	struct ng_bpf_hookprog	*prog;
8592a3e552SArchie Cobbs 	struct ng_bpf_hookstat	stats;
8692a3e552SArchie Cobbs };
8792a3e552SArchie Cobbs typedef struct ng_bpf_hookinfo *hinfo_p;
8892a3e552SArchie Cobbs 
8992a3e552SArchie Cobbs /* Netgraph methods */
9092a3e552SArchie Cobbs static ng_constructor_t	ng_bpf_constructor;
9192a3e552SArchie Cobbs static ng_rcvmsg_t	ng_bpf_rcvmsg;
92069154d5SJulian Elischer static ng_shutdown_t	ng_bpf_shutdown;
9392a3e552SArchie Cobbs static ng_newhook_t	ng_bpf_newhook;
9492a3e552SArchie Cobbs static ng_rcvdata_t	ng_bpf_rcvdata;
9592a3e552SArchie Cobbs static ng_disconnect_t	ng_bpf_disconnect;
9692a3e552SArchie Cobbs 
9792a3e552SArchie Cobbs /* Internal helper functions */
9892a3e552SArchie Cobbs static int	ng_bpf_setprog(hook_p hook, const struct ng_bpf_hookprog *hp);
9992a3e552SArchie Cobbs 
10092a3e552SArchie Cobbs /* Parse type for one struct bfp_insn */
101f0184ff8SArchie Cobbs static const struct ng_parse_struct_field ng_bpf_insn_type_fields[] = {
10257b57be3SArchie Cobbs 	{ "code",	&ng_parse_hint16_type	},
10357b57be3SArchie Cobbs 	{ "jt",		&ng_parse_uint8_type	},
10457b57be3SArchie Cobbs 	{ "jf",		&ng_parse_uint8_type	},
10557b57be3SArchie Cobbs 	{ "k",		&ng_parse_uint32_type	},
10692a3e552SArchie Cobbs 	{ NULL }
10792a3e552SArchie Cobbs };
10892a3e552SArchie Cobbs static const struct ng_parse_type ng_bpf_insn_type = {
10992a3e552SArchie Cobbs 	&ng_parse_struct_type,
110f0184ff8SArchie Cobbs 	&ng_bpf_insn_type_fields
11192a3e552SArchie Cobbs };
11292a3e552SArchie Cobbs 
11392a3e552SArchie Cobbs /* Parse type for the field 'bpf_prog' in struct ng_bpf_hookprog */
11492a3e552SArchie Cobbs static int
11592a3e552SArchie Cobbs ng_bpf_hookprogary_getLength(const struct ng_parse_type *type,
11692a3e552SArchie Cobbs 	const u_char *start, const u_char *buf)
11792a3e552SArchie Cobbs {
11892a3e552SArchie Cobbs 	const struct ng_bpf_hookprog *hp;
11992a3e552SArchie Cobbs 
12092a3e552SArchie Cobbs 	hp = (const struct ng_bpf_hookprog *)
12192a3e552SArchie Cobbs 	    (buf - OFFSETOF(struct ng_bpf_hookprog, bpf_prog));
12292a3e552SArchie Cobbs 	return hp->bpf_prog_len;
12392a3e552SArchie Cobbs }
12492a3e552SArchie Cobbs 
12592a3e552SArchie Cobbs static const struct ng_parse_array_info ng_bpf_hookprogary_info = {
12692a3e552SArchie Cobbs 	&ng_bpf_insn_type,
12792a3e552SArchie Cobbs 	&ng_bpf_hookprogary_getLength,
12892a3e552SArchie Cobbs 	NULL
12992a3e552SArchie Cobbs };
13092a3e552SArchie Cobbs static const struct ng_parse_type ng_bpf_hookprogary_type = {
13192a3e552SArchie Cobbs 	&ng_parse_array_type,
13292a3e552SArchie Cobbs 	&ng_bpf_hookprogary_info
13392a3e552SArchie Cobbs };
13492a3e552SArchie Cobbs 
13592a3e552SArchie Cobbs /* Parse type for struct ng_bpf_hookprog */
136f0184ff8SArchie Cobbs static const struct ng_parse_struct_field ng_bpf_hookprog_type_fields[]
13792a3e552SArchie Cobbs 	= NG_BPF_HOOKPROG_TYPE_INFO(&ng_bpf_hookprogary_type);
13892a3e552SArchie Cobbs static const struct ng_parse_type ng_bpf_hookprog_type = {
13992a3e552SArchie Cobbs 	&ng_parse_struct_type,
140f0184ff8SArchie Cobbs 	&ng_bpf_hookprog_type_fields
14192a3e552SArchie Cobbs };
14292a3e552SArchie Cobbs 
14392a3e552SArchie Cobbs /* Parse type for struct ng_bpf_hookstat */
144f0184ff8SArchie Cobbs static const struct ng_parse_struct_field ng_bpf_hookstat_type_fields[]
145f0184ff8SArchie Cobbs 	= NG_BPF_HOOKSTAT_TYPE_INFO;
14692a3e552SArchie Cobbs static const struct ng_parse_type ng_bpf_hookstat_type = {
14792a3e552SArchie Cobbs 	&ng_parse_struct_type,
148f0184ff8SArchie Cobbs 	&ng_bpf_hookstat_type_fields
14992a3e552SArchie Cobbs };
15092a3e552SArchie Cobbs 
15192a3e552SArchie Cobbs /* List of commands and how to convert arguments to/from ASCII */
15292a3e552SArchie Cobbs static const struct ng_cmdlist ng_bpf_cmdlist[] = {
15392a3e552SArchie Cobbs 	{
15492a3e552SArchie Cobbs 	  NGM_BPF_COOKIE,
15592a3e552SArchie Cobbs 	  NGM_BPF_SET_PROGRAM,
15692a3e552SArchie Cobbs 	  "setprogram",
15792a3e552SArchie Cobbs 	  &ng_bpf_hookprog_type,
15892a3e552SArchie Cobbs 	  NULL
15992a3e552SArchie Cobbs 	},
16092a3e552SArchie Cobbs 	{
16192a3e552SArchie Cobbs 	  NGM_BPF_COOKIE,
16292a3e552SArchie Cobbs 	  NGM_BPF_GET_PROGRAM,
16392a3e552SArchie Cobbs 	  "getprogram",
16492a3e552SArchie Cobbs 	  &ng_parse_hookbuf_type,
16592a3e552SArchie Cobbs 	  &ng_bpf_hookprog_type
16692a3e552SArchie Cobbs 	},
16792a3e552SArchie Cobbs 	{
16892a3e552SArchie Cobbs 	  NGM_BPF_COOKIE,
16992a3e552SArchie Cobbs 	  NGM_BPF_GET_STATS,
17092a3e552SArchie Cobbs 	  "getstats",
17192a3e552SArchie Cobbs 	  &ng_parse_hookbuf_type,
17292a3e552SArchie Cobbs 	  &ng_bpf_hookstat_type
17392a3e552SArchie Cobbs 	},
17492a3e552SArchie Cobbs 	{
17592a3e552SArchie Cobbs 	  NGM_BPF_COOKIE,
17692a3e552SArchie Cobbs 	  NGM_BPF_CLR_STATS,
17792a3e552SArchie Cobbs 	  "clrstats",
17892a3e552SArchie Cobbs 	  &ng_parse_hookbuf_type,
17992a3e552SArchie Cobbs 	  NULL
18092a3e552SArchie Cobbs 	},
18192a3e552SArchie Cobbs 	{
18292a3e552SArchie Cobbs 	  NGM_BPF_COOKIE,
18392a3e552SArchie Cobbs 	  NGM_BPF_GETCLR_STATS,
18492a3e552SArchie Cobbs 	  "getclrstats",
18592a3e552SArchie Cobbs 	  &ng_parse_hookbuf_type,
18692a3e552SArchie Cobbs 	  &ng_bpf_hookstat_type
18792a3e552SArchie Cobbs 	},
18892a3e552SArchie Cobbs 	{ 0 }
18992a3e552SArchie Cobbs };
19092a3e552SArchie Cobbs 
19192a3e552SArchie Cobbs /* Netgraph type descriptor */
19292a3e552SArchie Cobbs static struct ng_type typestruct = {
193f8aae777SJulian Elischer 	.version =	NG_ABI_VERSION,
194f8aae777SJulian Elischer 	.name =		NG_BPF_NODE_TYPE,
195f8aae777SJulian Elischer 	.constructor =	ng_bpf_constructor,
196f8aae777SJulian Elischer 	.rcvmsg =	ng_bpf_rcvmsg,
197f8aae777SJulian Elischer 	.shutdown =	ng_bpf_shutdown,
198f8aae777SJulian Elischer 	.newhook =	ng_bpf_newhook,
199f8aae777SJulian Elischer 	.rcvdata =	ng_bpf_rcvdata,
200f8aae777SJulian Elischer 	.disconnect =	ng_bpf_disconnect,
201f8aae777SJulian Elischer 	.cmdlist =	ng_bpf_cmdlist,
20292a3e552SArchie Cobbs };
20392a3e552SArchie Cobbs NETGRAPH_INIT(bpf, &typestruct);
20492a3e552SArchie Cobbs 
20592a3e552SArchie Cobbs /* Default BPF program for a hook that matches nothing */
20692a3e552SArchie Cobbs static const struct ng_bpf_hookprog ng_bpf_default_prog = {
20792a3e552SArchie Cobbs 	{ '\0' },		/* to be filled in at hook creation time */
20892a3e552SArchie Cobbs 	{ '\0' },
20992a3e552SArchie Cobbs 	{ '\0' },
21092a3e552SArchie Cobbs 	1,
21192a3e552SArchie Cobbs 	{ BPF_STMT(BPF_RET+BPF_K, 0) }
21292a3e552SArchie Cobbs };
21392a3e552SArchie Cobbs 
21492a3e552SArchie Cobbs /*
21592a3e552SArchie Cobbs  * Node constructor
21692a3e552SArchie Cobbs  *
21792a3e552SArchie Cobbs  * We don't keep any per-node private data
218069154d5SJulian Elischer  * We go via the hooks.
21992a3e552SArchie Cobbs  */
22092a3e552SArchie Cobbs static int
221069154d5SJulian Elischer ng_bpf_constructor(node_p node)
22292a3e552SArchie Cobbs {
22330400f03SJulian Elischer 	NG_NODE_SET_PRIVATE(node, NULL);
22492a3e552SArchie Cobbs 	return (0);
22592a3e552SArchie Cobbs }
22692a3e552SArchie Cobbs 
22792a3e552SArchie Cobbs /*
22892a3e552SArchie Cobbs  * Add a hook
22992a3e552SArchie Cobbs  */
23092a3e552SArchie Cobbs static int
23192a3e552SArchie Cobbs ng_bpf_newhook(node_p node, hook_p hook, const char *name)
23292a3e552SArchie Cobbs {
23392a3e552SArchie Cobbs 	hinfo_p hip;
23492a3e552SArchie Cobbs 	int error;
23592a3e552SArchie Cobbs 
23692a3e552SArchie Cobbs 	/* Create hook private structure */
2379c8c302fSJulian Elischer 	MALLOC(hip, hinfo_p, sizeof(*hip), M_NETGRAPH_BPF, M_NOWAIT | M_ZERO);
23892a3e552SArchie Cobbs 	if (hip == NULL)
23992a3e552SArchie Cobbs 		return (ENOMEM);
24092a3e552SArchie Cobbs 	hip->hook = hook;
24130400f03SJulian Elischer 	NG_HOOK_SET_PRIVATE(hook, hip);
24292a3e552SArchie Cobbs 	hip->node = node;
24392a3e552SArchie Cobbs 
24492a3e552SArchie Cobbs 	/* Attach the default BPF program */
24592a3e552SArchie Cobbs 	if ((error = ng_bpf_setprog(hook, &ng_bpf_default_prog)) != 0) {
2469c8c302fSJulian Elischer 		FREE(hip, M_NETGRAPH_BPF);
24730400f03SJulian Elischer 		NG_HOOK_SET_PRIVATE(hook, NULL);
24892a3e552SArchie Cobbs 		return (error);
24992a3e552SArchie Cobbs 	}
25092a3e552SArchie Cobbs 
25192a3e552SArchie Cobbs 	/* Set hook name */
25292a3e552SArchie Cobbs 	strncpy(hip->prog->thisHook, name, sizeof(hip->prog->thisHook) - 1);
25392a3e552SArchie Cobbs 	hip->prog->thisHook[sizeof(hip->prog->thisHook) - 1] = '\0';
25492a3e552SArchie Cobbs 	return (0);
25592a3e552SArchie Cobbs }
25692a3e552SArchie Cobbs 
25792a3e552SArchie Cobbs /*
25892a3e552SArchie Cobbs  * Receive a control message
25992a3e552SArchie Cobbs  */
26092a3e552SArchie Cobbs static int
261069154d5SJulian Elischer ng_bpf_rcvmsg(node_p node, item_p item, hook_p lasthook)
26292a3e552SArchie Cobbs {
263069154d5SJulian Elischer 	struct ng_mesg *msg;
26492a3e552SArchie Cobbs 	struct ng_mesg *resp = NULL;
26592a3e552SArchie Cobbs 	int error = 0;
26692a3e552SArchie Cobbs 
267069154d5SJulian Elischer 	NGI_GET_MSG(item, msg);
26892a3e552SArchie Cobbs 	switch (msg->header.typecookie) {
26992a3e552SArchie Cobbs 	case NGM_BPF_COOKIE:
27092a3e552SArchie Cobbs 		switch (msg->header.cmd) {
27192a3e552SArchie Cobbs 		case NGM_BPF_SET_PROGRAM:
27292a3e552SArchie Cobbs 		    {
27392a3e552SArchie Cobbs 			struct ng_bpf_hookprog *const
27492a3e552SArchie Cobbs 			    hp = (struct ng_bpf_hookprog *)msg->data;
27592a3e552SArchie Cobbs 			hook_p hook;
27692a3e552SArchie Cobbs 
27792a3e552SArchie Cobbs 			/* Sanity check */
27892a3e552SArchie Cobbs 			if (msg->header.arglen < sizeof(*hp)
279ab0d3c94SArchie Cobbs 			    || msg->header.arglen
280ab0d3c94SArchie Cobbs 			      != NG_BPF_HOOKPROG_SIZE(hp->bpf_prog_len))
28192a3e552SArchie Cobbs 				ERROUT(EINVAL);
28292a3e552SArchie Cobbs 
28392a3e552SArchie Cobbs 			/* Find hook */
28492a3e552SArchie Cobbs 			if ((hook = ng_findhook(node, hp->thisHook)) == NULL)
28592a3e552SArchie Cobbs 				ERROUT(ENOENT);
28692a3e552SArchie Cobbs 
28792a3e552SArchie Cobbs 			/* Set new program */
28892a3e552SArchie Cobbs 			if ((error = ng_bpf_setprog(hook, hp)) != 0)
28992a3e552SArchie Cobbs 				ERROUT(error);
29092a3e552SArchie Cobbs 			break;
29192a3e552SArchie Cobbs 		    }
29292a3e552SArchie Cobbs 
29392a3e552SArchie Cobbs 		case NGM_BPF_GET_PROGRAM:
29492a3e552SArchie Cobbs 		    {
295ab0d3c94SArchie Cobbs 			struct ng_bpf_hookprog *hp;
29692a3e552SArchie Cobbs 			hook_p hook;
29792a3e552SArchie Cobbs 
29892a3e552SArchie Cobbs 			/* Sanity check */
29992a3e552SArchie Cobbs 			if (msg->header.arglen == 0)
30092a3e552SArchie Cobbs 				ERROUT(EINVAL);
30192a3e552SArchie Cobbs 			msg->data[msg->header.arglen - 1] = '\0';
30292a3e552SArchie Cobbs 
30392a3e552SArchie Cobbs 			/* Find hook */
30492a3e552SArchie Cobbs 			if ((hook = ng_findhook(node, msg->data)) == NULL)
30592a3e552SArchie Cobbs 				ERROUT(ENOENT);
30692a3e552SArchie Cobbs 
30792a3e552SArchie Cobbs 			/* Build response */
30830400f03SJulian Elischer 			hp = ((hinfo_p)NG_HOOK_PRIVATE(hook))->prog;
30992a3e552SArchie Cobbs 			NG_MKRESPONSE(resp, msg,
310ab0d3c94SArchie Cobbs 			    NG_BPF_HOOKPROG_SIZE(hp->bpf_prog_len), M_NOWAIT);
31192a3e552SArchie Cobbs 			if (resp == NULL)
31292a3e552SArchie Cobbs 				ERROUT(ENOMEM);
313ab0d3c94SArchie Cobbs 			bcopy(hp, resp->data,
314ab0d3c94SArchie Cobbs 			   NG_BPF_HOOKPROG_SIZE(hp->bpf_prog_len));
31592a3e552SArchie Cobbs 			break;
31692a3e552SArchie Cobbs 		    }
31792a3e552SArchie Cobbs 
31892a3e552SArchie Cobbs 		case NGM_BPF_GET_STATS:
31992a3e552SArchie Cobbs 		case NGM_BPF_CLR_STATS:
32092a3e552SArchie Cobbs 		case NGM_BPF_GETCLR_STATS:
32192a3e552SArchie Cobbs 		    {
32292a3e552SArchie Cobbs 			struct ng_bpf_hookstat *stats;
32392a3e552SArchie Cobbs 			hook_p hook;
32492a3e552SArchie Cobbs 
32592a3e552SArchie Cobbs 			/* Sanity check */
32692a3e552SArchie Cobbs 			if (msg->header.arglen == 0)
32792a3e552SArchie Cobbs 				ERROUT(EINVAL);
32892a3e552SArchie Cobbs 			msg->data[msg->header.arglen - 1] = '\0';
32992a3e552SArchie Cobbs 
33092a3e552SArchie Cobbs 			/* Find hook */
33192a3e552SArchie Cobbs 			if ((hook = ng_findhook(node, msg->data)) == NULL)
33292a3e552SArchie Cobbs 				ERROUT(ENOENT);
33330400f03SJulian Elischer 			stats = &((hinfo_p)NG_HOOK_PRIVATE(hook))->stats;
33492a3e552SArchie Cobbs 
33592a3e552SArchie Cobbs 			/* Build response (if desired) */
33692a3e552SArchie Cobbs 			if (msg->header.cmd != NGM_BPF_CLR_STATS) {
33792a3e552SArchie Cobbs 				NG_MKRESPONSE(resp,
33892a3e552SArchie Cobbs 				    msg, sizeof(*stats), M_NOWAIT);
33992a3e552SArchie Cobbs 				if (resp == NULL)
34092a3e552SArchie Cobbs 					ERROUT(ENOMEM);
34192a3e552SArchie Cobbs 				bcopy(stats, resp->data, sizeof(*stats));
34292a3e552SArchie Cobbs 			}
34392a3e552SArchie Cobbs 
34492a3e552SArchie Cobbs 			/* Clear stats (if desired) */
34592a3e552SArchie Cobbs 			if (msg->header.cmd != NGM_BPF_GET_STATS)
34692a3e552SArchie Cobbs 				bzero(stats, sizeof(*stats));
34792a3e552SArchie Cobbs 			break;
34892a3e552SArchie Cobbs 		    }
34992a3e552SArchie Cobbs 
35092a3e552SArchie Cobbs 		default:
35192a3e552SArchie Cobbs 			error = EINVAL;
35292a3e552SArchie Cobbs 			break;
35392a3e552SArchie Cobbs 		}
35492a3e552SArchie Cobbs 		break;
35592a3e552SArchie Cobbs 	default:
35692a3e552SArchie Cobbs 		error = EINVAL;
35792a3e552SArchie Cobbs 		break;
35892a3e552SArchie Cobbs 	}
35992a3e552SArchie Cobbs done:
360cdbfe124SJulian Elischer 	NG_RESPOND_MSG(error, node, item, resp);
361069154d5SJulian Elischer 	NG_FREE_MSG(msg);
36292a3e552SArchie Cobbs 	return (error);
36392a3e552SArchie Cobbs }
36492a3e552SArchie Cobbs 
36592a3e552SArchie Cobbs /*
36692a3e552SArchie Cobbs  * Receive data on a hook
36792a3e552SArchie Cobbs  *
36892a3e552SArchie Cobbs  * Apply the filter, and then drop or forward packet as appropriate.
36992a3e552SArchie Cobbs  */
37092a3e552SArchie Cobbs static int
371069154d5SJulian Elischer ng_bpf_rcvdata(hook_p hook, item_p item)
37292a3e552SArchie Cobbs {
37330400f03SJulian Elischer 	const hinfo_p hip = NG_HOOK_PRIVATE(hook);
374069154d5SJulian Elischer 	int totlen;
37592a3e552SArchie Cobbs 	int needfree = 0, error = 0;
37692a3e552SArchie Cobbs 	u_char *data, buf[256];
37792a3e552SArchie Cobbs 	hinfo_p dhip;
37892a3e552SArchie Cobbs 	hook_p dest;
37992a3e552SArchie Cobbs 	u_int len;
380069154d5SJulian Elischer 	struct mbuf *m;
38192a3e552SArchie Cobbs 
382069154d5SJulian Elischer 	m = NGI_M(item);	/* 'item' still owns it.. we are peeking */
383069154d5SJulian Elischer 	totlen = m->m_pkthdr.len;
384069154d5SJulian Elischer 	/* Update stats on incoming hook. XXX Can we do 64 bits atomically? */
385069154d5SJulian Elischer 	/* atomic_add_int64(&hip->stats.recvFrames, 1); */
386069154d5SJulian Elischer 	/* atomic_add_int64(&hip->stats.recvOctets, totlen); */
38792a3e552SArchie Cobbs 	hip->stats.recvFrames++;
38892a3e552SArchie Cobbs 	hip->stats.recvOctets += totlen;
38992a3e552SArchie Cobbs 
39092a3e552SArchie Cobbs 	/* Need to put packet in contiguous memory for bpf */
39192a3e552SArchie Cobbs 	if (m->m_next != NULL) {
39292a3e552SArchie Cobbs 		if (totlen > sizeof(buf)) {
3939c8c302fSJulian Elischer 			MALLOC(data, u_char *, totlen, M_NETGRAPH_BPF, M_NOWAIT);
39492a3e552SArchie Cobbs 			if (data == NULL) {
395069154d5SJulian Elischer 				NG_FREE_ITEM(item);
39692a3e552SArchie Cobbs 				return (ENOMEM);
39792a3e552SArchie Cobbs 			}
39892a3e552SArchie Cobbs 			needfree = 1;
39992a3e552SArchie Cobbs 		} else
40092a3e552SArchie Cobbs 			data = buf;
40192a3e552SArchie Cobbs 		m_copydata(m, 0, totlen, (caddr_t)data);
40292a3e552SArchie Cobbs 	} else
40392a3e552SArchie Cobbs 		data = mtod(m, u_char *);
40492a3e552SArchie Cobbs 
40592a3e552SArchie Cobbs 	/* Run packet through filter */
40692a3e552SArchie Cobbs 	len = bpf_filter(hip->prog->bpf_prog, data, totlen, totlen);
40792a3e552SArchie Cobbs 	if (needfree)
4089c8c302fSJulian Elischer 		FREE(data, M_NETGRAPH_BPF);
40992a3e552SArchie Cobbs 
41092a3e552SArchie Cobbs 	/* See if we got a match and find destination hook */
41192a3e552SArchie Cobbs 	if (len > 0) {
41292a3e552SArchie Cobbs 
41392a3e552SArchie Cobbs 		/* Update stats */
414069154d5SJulian Elischer 		/* XXX atomically? */
41592a3e552SArchie Cobbs 		hip->stats.recvMatchFrames++;
41692a3e552SArchie Cobbs 		hip->stats.recvMatchOctets += totlen;
41792a3e552SArchie Cobbs 
41892a3e552SArchie Cobbs 		/* Truncate packet length if required by the filter */
419069154d5SJulian Elischer 		/* Assume this never changes m */
42092a3e552SArchie Cobbs 		if (len < totlen) {
42192a3e552SArchie Cobbs 			m_adj(m, -(totlen - len));
42292a3e552SArchie Cobbs 			totlen -= len;
42392a3e552SArchie Cobbs 		}
42492a3e552SArchie Cobbs 		dest = ng_findhook(hip->node, hip->prog->ifMatch);
42592a3e552SArchie Cobbs 	} else
42692a3e552SArchie Cobbs 		dest = ng_findhook(hip->node, hip->prog->ifNotMatch);
42792a3e552SArchie Cobbs 	if (dest == NULL) {
428069154d5SJulian Elischer 		NG_FREE_ITEM(item);
42992a3e552SArchie Cobbs 		return (0);
43092a3e552SArchie Cobbs 	}
43192a3e552SArchie Cobbs 
43292a3e552SArchie Cobbs 	/* Deliver frame out destination hook */
43330400f03SJulian Elischer 	dhip = NG_HOOK_PRIVATE(dest);
43492a3e552SArchie Cobbs 	dhip->stats.xmitOctets += totlen;
43592a3e552SArchie Cobbs 	dhip->stats.xmitFrames++;
43630400f03SJulian Elischer 	NG_FWD_ITEM_HOOK(error, item, dest);
43792a3e552SArchie Cobbs 	return (error);
43892a3e552SArchie Cobbs }
43992a3e552SArchie Cobbs 
44092a3e552SArchie Cobbs /*
44192a3e552SArchie Cobbs  * Shutdown processing
44292a3e552SArchie Cobbs  */
44392a3e552SArchie Cobbs static int
444069154d5SJulian Elischer ng_bpf_shutdown(node_p node)
44592a3e552SArchie Cobbs {
44630400f03SJulian Elischer 	NG_NODE_UNREF(node);
44792a3e552SArchie Cobbs 	return (0);
44892a3e552SArchie Cobbs }
44992a3e552SArchie Cobbs 
45092a3e552SArchie Cobbs /*
45192a3e552SArchie Cobbs  * Hook disconnection
45292a3e552SArchie Cobbs  */
45392a3e552SArchie Cobbs static int
45492a3e552SArchie Cobbs ng_bpf_disconnect(hook_p hook)
45592a3e552SArchie Cobbs {
45630400f03SJulian Elischer 	const hinfo_p hip = NG_HOOK_PRIVATE(hook);
45792a3e552SArchie Cobbs 
4586e551fb6SDavid E. O'Brien 	KASSERT(hip != NULL, ("%s: null info", __func__));
4599c8c302fSJulian Elischer 	FREE(hip->prog, M_NETGRAPH_BPF);
46092a3e552SArchie Cobbs 	bzero(hip, sizeof(*hip));
4619c8c302fSJulian Elischer 	FREE(hip, M_NETGRAPH_BPF);
46230400f03SJulian Elischer 	NG_HOOK_SET_PRIVATE(hook, NULL);			/* for good measure */
46330400f03SJulian Elischer 	if ((NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0)
46430400f03SJulian Elischer 	&& (NG_NODE_IS_VALID(NG_HOOK_NODE(hook)))) {
46530400f03SJulian Elischer 		ng_rmnode_self(NG_HOOK_NODE(hook));
466069154d5SJulian Elischer 	}
46792a3e552SArchie Cobbs 	return (0);
46892a3e552SArchie Cobbs }
46992a3e552SArchie Cobbs 
47092a3e552SArchie Cobbs /************************************************************************
47192a3e552SArchie Cobbs 			HELPER STUFF
47292a3e552SArchie Cobbs  ************************************************************************/
47392a3e552SArchie Cobbs 
47492a3e552SArchie Cobbs /*
47592a3e552SArchie Cobbs  * Set the BPF program associated with a hook
47692a3e552SArchie Cobbs  */
47792a3e552SArchie Cobbs static int
47892a3e552SArchie Cobbs ng_bpf_setprog(hook_p hook, const struct ng_bpf_hookprog *hp0)
47992a3e552SArchie Cobbs {
48030400f03SJulian Elischer 	const hinfo_p hip = NG_HOOK_PRIVATE(hook);
48192a3e552SArchie Cobbs 	struct ng_bpf_hookprog *hp;
48292a3e552SArchie Cobbs 	int size;
48392a3e552SArchie Cobbs 
48492a3e552SArchie Cobbs 	/* Check program for validity */
48592a3e552SArchie Cobbs 	if (!bpf_validate(hp0->bpf_prog, hp0->bpf_prog_len))
48692a3e552SArchie Cobbs 		return (EINVAL);
48792a3e552SArchie Cobbs 
48892a3e552SArchie Cobbs 	/* Make a copy of the program */
489ab0d3c94SArchie Cobbs 	size = NG_BPF_HOOKPROG_SIZE(hp0->bpf_prog_len);
4909c8c302fSJulian Elischer 	MALLOC(hp, struct ng_bpf_hookprog *, size, M_NETGRAPH_BPF, M_NOWAIT);
49192a3e552SArchie Cobbs 	if (hp == NULL)
49292a3e552SArchie Cobbs 		return (ENOMEM);
49392a3e552SArchie Cobbs 	bcopy(hp0, hp, size);
49492a3e552SArchie Cobbs 
49592a3e552SArchie Cobbs 	/* Free previous program, if any, and assign new one */
49692a3e552SArchie Cobbs 	if (hip->prog != NULL)
4979c8c302fSJulian Elischer 		FREE(hip->prog, M_NETGRAPH_BPF);
49892a3e552SArchie Cobbs 	hip->prog = hp;
49992a3e552SArchie Cobbs 	return (0);
50092a3e552SArchie Cobbs }
50192a3e552SArchie Cobbs 
502