5 * Copyright (c) 1999 Whistle Communications, Inc.
8 * Subject to the following obligations and disclaimer of warranty, use and
9 * redistribution of this software, in source or object code forms, with or
10 * without modifications are expressly permitted by Whistle Communications;
11 * provided, however, that:
12 * 1. Any and all reproductions of the source or object code must include the
13 * copyright notice above and the following disclaimer of warranties; and
14 * 2. No rights are granted, in any manner or form, to use Whistle
15 * Communications, Inc. trademarks, including the mark "WHISTLE
16 * COMMUNICATIONS" on advertising, endorsements, or otherwise except as
17 * such appears in the above copyright notice or in the software.
19 * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
20 * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
21 * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
22 * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
23 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
24 * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
25 * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
26 * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
27 * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES
28 * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
29 * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
30 * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
31 * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
32 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
34 * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
37 * Author: Archie Cobbs <archie@freebsd.org>
40 * $Whistle: ng_bpf.c,v 1.3 1999/12/03 20:30:23 archie Exp $
44 * BPF NETGRAPH NODE TYPE
46 * This node type accepts any number of hook connections. With each hook
47 * is associated a bpf(4) filter program, and two hook names (each possibly
48 * the empty string). Incoming packets are compared against the filter;
49 * matching packets are delivered out the first named hook (or dropped if
50 * the empty string), and non-matching packets are delivered out the second
51 * named hook (or dropped if the empty string).
53 * Each hook also keeps statistics about how many packets have matched, etc.
56 #include <sys/param.h>
57 #include <sys/systm.h>
58 #include <sys/errno.h>
59 #include <sys/kernel.h>
60 #include <sys/malloc.h>
65 #include <netgraph/ng_message.h>
66 #include <netgraph/netgraph.h>
67 #include <netgraph/ng_parse.h>
68 #include <netgraph/ng_bpf.h>
70 #ifdef NG_SEPARATE_MALLOC
71 MALLOC_DEFINE(M_NETGRAPH_BPF, "netgraph_bpf", "netgraph bpf node ");
73 #define M_NETGRAPH_BPF M_NETGRAPH
76 #define OFFSETOF(s, e) ((char *)&((s *)0)->e - (char *)((s *)0))
78 #define ERROUT(x) do { error = (x); goto done; } while (0)
80 /* Per hook private info */
81 struct ng_bpf_hookinfo {
84 struct ng_bpf_hookprog *prog;
85 struct ng_bpf_hookstat stats;
87 typedef struct ng_bpf_hookinfo *hinfo_p;
89 /* Netgraph methods */
90 static ng_constructor_t ng_bpf_constructor;
91 static ng_rcvmsg_t ng_bpf_rcvmsg;
92 static ng_shutdown_t ng_bpf_shutdown;
93 static ng_newhook_t ng_bpf_newhook;
94 static ng_rcvdata_t ng_bpf_rcvdata;
95 static ng_disconnect_t ng_bpf_disconnect;
97 /* Internal helper functions */
98 static int ng_bpf_setprog(hook_p hook, const struct ng_bpf_hookprog *hp);
100 /* Parse type for one struct bfp_insn */
101 static const struct ng_parse_struct_info ng_bpf_insn_type_info = {
103 { "code", &ng_parse_hint16_type },
104 { "jt", &ng_parse_uint8_type },
105 { "jf", &ng_parse_uint8_type },
106 { "k", &ng_parse_uint32_type },
110 static const struct ng_parse_type ng_bpf_insn_type = {
111 &ng_parse_struct_type,
112 &ng_bpf_insn_type_info
115 /* Parse type for the field 'bpf_prog' in struct ng_bpf_hookprog */
117 ng_bpf_hookprogary_getLength(const struct ng_parse_type *type,
118 const u_char *start, const u_char *buf)
120 const struct ng_bpf_hookprog *hp;
122 hp = (const struct ng_bpf_hookprog *)
123 (buf - OFFSETOF(struct ng_bpf_hookprog, bpf_prog));
124 return hp->bpf_prog_len;
127 static const struct ng_parse_array_info ng_bpf_hookprogary_info = {
129 &ng_bpf_hookprogary_getLength,
132 static const struct ng_parse_type ng_bpf_hookprogary_type = {
133 &ng_parse_array_type,
134 &ng_bpf_hookprogary_info
137 /* Parse type for struct ng_bpf_hookprog */
138 static const struct ng_parse_struct_info ng_bpf_hookprog_type_info
139 = NG_BPF_HOOKPROG_TYPE_INFO(&ng_bpf_hookprogary_type);
140 static const struct ng_parse_type ng_bpf_hookprog_type = {
141 &ng_parse_struct_type,
142 &ng_bpf_hookprog_type_info
145 /* Parse type for struct ng_bpf_hookstat */
146 static const struct ng_parse_struct_info
147 ng_bpf_hookstat_type_info = NG_BPF_HOOKSTAT_TYPE_INFO;
148 static const struct ng_parse_type ng_bpf_hookstat_type = {
149 &ng_parse_struct_type,
150 &ng_bpf_hookstat_type_info
153 /* List of commands and how to convert arguments to/from ASCII */
154 static const struct ng_cmdlist ng_bpf_cmdlist[] = {
159 &ng_bpf_hookprog_type,
166 &ng_parse_hookbuf_type,
167 &ng_bpf_hookprog_type
173 &ng_parse_hookbuf_type,
174 &ng_bpf_hookstat_type
180 &ng_parse_hookbuf_type,
185 NGM_BPF_GETCLR_STATS,
187 &ng_parse_hookbuf_type,
188 &ng_bpf_hookstat_type
193 /* Netgraph type descriptor */
194 static struct ng_type typestruct = {
208 NETGRAPH_INIT(bpf, &typestruct);
210 /* Default BPF program for a hook that matches nothing */
211 static const struct ng_bpf_hookprog ng_bpf_default_prog = {
212 { '\0' }, /* to be filled in at hook creation time */
216 { BPF_STMT(BPF_RET+BPF_K, 0) }
222 * We don't keep any per-node private data
223 * We go via the hooks.
226 ng_bpf_constructor(node_p node)
228 NG_NODE_SET_PRIVATE(node, NULL);
236 ng_bpf_newhook(node_p node, hook_p hook, const char *name)
241 /* Create hook private structure */
242 MALLOC(hip, hinfo_p, sizeof(*hip), M_NETGRAPH_BPF, M_NOWAIT | M_ZERO);
246 NG_HOOK_SET_PRIVATE(hook, hip);
249 /* Attach the default BPF program */
250 if ((error = ng_bpf_setprog(hook, &ng_bpf_default_prog)) != 0) {
251 FREE(hip, M_NETGRAPH_BPF);
252 NG_HOOK_SET_PRIVATE(hook, NULL);
257 strncpy(hip->prog->thisHook, name, sizeof(hip->prog->thisHook) - 1);
258 hip->prog->thisHook[sizeof(hip->prog->thisHook) - 1] = '\0';
263 * Receive a control message
266 ng_bpf_rcvmsg(node_p node, item_p item, hook_p lasthook)
269 struct ng_mesg *resp = NULL;
272 NGI_GET_MSG(item, msg);
273 switch (msg->header.typecookie) {
275 switch (msg->header.cmd) {
276 case NGM_BPF_SET_PROGRAM:
278 struct ng_bpf_hookprog *const
279 hp = (struct ng_bpf_hookprog *)msg->data;
283 if (msg->header.arglen < sizeof(*hp)
284 || msg->header.arglen
285 != NG_BPF_HOOKPROG_SIZE(hp->bpf_prog_len))
289 if ((hook = ng_findhook(node, hp->thisHook)) == NULL)
292 /* Set new program */
293 if ((error = ng_bpf_setprog(hook, hp)) != 0)
298 case NGM_BPF_GET_PROGRAM:
300 struct ng_bpf_hookprog *hp;
304 if (msg->header.arglen == 0)
306 msg->data[msg->header.arglen - 1] = '\0';
309 if ((hook = ng_findhook(node, msg->data)) == NULL)
313 hp = ((hinfo_p)NG_HOOK_PRIVATE(hook))->prog;
314 NG_MKRESPONSE(resp, msg,
315 NG_BPF_HOOKPROG_SIZE(hp->bpf_prog_len), M_NOWAIT);
318 bcopy(hp, resp->data,
319 NG_BPF_HOOKPROG_SIZE(hp->bpf_prog_len));
323 case NGM_BPF_GET_STATS:
324 case NGM_BPF_CLR_STATS:
325 case NGM_BPF_GETCLR_STATS:
327 struct ng_bpf_hookstat *stats;
331 if (msg->header.arglen == 0)
333 msg->data[msg->header.arglen - 1] = '\0';
336 if ((hook = ng_findhook(node, msg->data)) == NULL)
338 stats = &((hinfo_p)NG_HOOK_PRIVATE(hook))->stats;
340 /* Build response (if desired) */
341 if (msg->header.cmd != NGM_BPF_CLR_STATS) {
343 msg, sizeof(*stats), M_NOWAIT);
346 bcopy(stats, resp->data, sizeof(*stats));
349 /* Clear stats (if desired) */
350 if (msg->header.cmd != NGM_BPF_GET_STATS)
351 bzero(stats, sizeof(*stats));
365 NG_RESPOND_MSG(error, node, item, resp);
371 * Receive data on a hook
373 * Apply the filter, and then drop or forward packet as appropriate.
376 ng_bpf_rcvdata(hook_p hook, item_p item)
378 const hinfo_p hip = NG_HOOK_PRIVATE(hook);
380 int needfree = 0, error = 0;
381 u_char *data, buf[256];
387 m = NGI_M(item); /* 'item' still owns it.. we are peeking */
388 totlen = m->m_pkthdr.len;
389 /* Update stats on incoming hook. XXX Can we do 64 bits atomically? */
390 /* atomic_add_int64(&hip->stats.recvFrames, 1); */
391 /* atomic_add_int64(&hip->stats.recvOctets, totlen); */
392 hip->stats.recvFrames++;
393 hip->stats.recvOctets += totlen;
395 /* Need to put packet in contiguous memory for bpf */
396 if (m->m_next != NULL) {
397 if (totlen > sizeof(buf)) {
398 MALLOC(data, u_char *, totlen, M_NETGRAPH_BPF, M_NOWAIT);
406 m_copydata(m, 0, totlen, (caddr_t)data);
408 data = mtod(m, u_char *);
410 /* Run packet through filter */
411 len = bpf_filter(hip->prog->bpf_prog, data, totlen, totlen);
413 FREE(data, M_NETGRAPH_BPF);
415 /* See if we got a match and find destination hook */
419 /* XXX atomically? */
420 hip->stats.recvMatchFrames++;
421 hip->stats.recvMatchOctets += totlen;
423 /* Truncate packet length if required by the filter */
424 /* Assume this never changes m */
426 m_adj(m, -(totlen - len));
429 dest = ng_findhook(hip->node, hip->prog->ifMatch);
431 dest = ng_findhook(hip->node, hip->prog->ifNotMatch);
437 /* Deliver frame out destination hook */
438 dhip = NG_HOOK_PRIVATE(dest);
439 dhip->stats.xmitOctets += totlen;
440 dhip->stats.xmitFrames++;
441 NG_FWD_ITEM_HOOK(error, item, dest);
446 * Shutdown processing
449 ng_bpf_shutdown(node_p node)
459 ng_bpf_disconnect(hook_p hook)
461 const hinfo_p hip = NG_HOOK_PRIVATE(hook);
463 KASSERT(hip != NULL, ("%s: null info", __func__));
464 FREE(hip->prog, M_NETGRAPH_BPF);
465 bzero(hip, sizeof(*hip));
466 FREE(hip, M_NETGRAPH_BPF);
467 NG_HOOK_SET_PRIVATE(hook, NULL); /* for good measure */
468 if ((NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0)
469 && (NG_NODE_IS_VALID(NG_HOOK_NODE(hook)))) {
470 ng_rmnode_self(NG_HOOK_NODE(hook));
475 /************************************************************************
477 ************************************************************************/
480 * Set the BPF program associated with a hook
483 ng_bpf_setprog(hook_p hook, const struct ng_bpf_hookprog *hp0)
485 const hinfo_p hip = NG_HOOK_PRIVATE(hook);
486 struct ng_bpf_hookprog *hp;
489 /* Check program for validity */
490 if (!bpf_validate(hp0->bpf_prog, hp0->bpf_prog_len))
493 /* Make a copy of the program */
494 size = NG_BPF_HOOKPROG_SIZE(hp0->bpf_prog_len);
495 MALLOC(hp, struct ng_bpf_hookprog *, size, M_NETGRAPH_BPF, M_NOWAIT);
498 bcopy(hp0, hp, size);
500 /* Free previous program, if any, and assign new one */
501 if (hip->prog != NULL)
502 FREE(hip->prog, M_NETGRAPH_BPF);