6 * Copyright (c) 1999 Whistle Communications, Inc.
9 * Subject to the following obligations and disclaimer of warranty, use and
10 * redistribution of this software, in source or object code forms, with or
11 * without modifications are expressly permitted by Whistle Communications;
12 * provided, however, that:
13 * 1. Any and all reproductions of the source or object code must include the
14 * copyright notice above and the following disclaimer of warranties; and
15 * 2. No rights are granted, in any manner or form, to use Whistle
16 * Communications, Inc. trademarks, including the mark "WHISTLE
17 * COMMUNICATIONS" on advertising, endorsements, or otherwise except as
18 * such appears in the above copyright notice or in the software.
20 * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
21 * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
22 * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
23 * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
24 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
25 * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
26 * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
27 * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
28 * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES
29 * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
30 * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
31 * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
32 * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
33 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
35 * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
38 * Author: Archie Cobbs <archie@freebsd.org>
41 * $Whistle: ng_bpf.c,v 1.3 1999/12/03 20:30:23 archie Exp $
45 * BPF NETGRAPH NODE TYPE
47 * This node type accepts any number of hook connections. With each hook
48 * is associated a bpf(4) filter program, and two hook names (each possibly
49 * the empty string). Incoming packets are compared against the filter;
50 * matching packets are delivered out the first named hook (or dropped if
51 * the empty string), and non-matching packets are delivered out the second
52 * named hook (or dropped if the empty string).
54 * Each hook also keeps statistics about how many packets have matched, etc.
59 #include <sys/param.h>
60 #include <sys/systm.h>
61 #include <sys/errno.h>
62 #include <sys/kernel.h>
63 #include <sys/malloc.h>
68 #include <net/bpf_jitter.h>
71 #include <netgraph/ng_message.h>
72 #include <netgraph/netgraph.h>
73 #include <netgraph/ng_parse.h>
74 #include <netgraph/ng_bpf.h>
76 #ifdef NG_SEPARATE_MALLOC
77 MALLOC_DEFINE(M_NETGRAPH_BPF, "netgraph_bpf", "netgraph bpf node ");
79 #define M_NETGRAPH_BPF M_NETGRAPH
82 #define OFFSETOF(s, e) ((char *)&((s *)0)->e - (char *)((s *)0))
84 #define ERROUT(x) do { error = (x); goto done; } while (0)
86 /* Per hook private info */
87 struct ng_bpf_hookinfo {
91 struct ng_bpf_hookprog *prog;
93 bpf_jit_filter *jit_prog;
95 struct ng_bpf_hookstat stats;
97 typedef struct ng_bpf_hookinfo *hinfo_p;
99 /* Netgraph methods */
100 static ng_constructor_t ng_bpf_constructor;
101 static ng_rcvmsg_t ng_bpf_rcvmsg;
102 static ng_shutdown_t ng_bpf_shutdown;
103 static ng_newhook_t ng_bpf_newhook;
104 static ng_rcvdata_t ng_bpf_rcvdata;
105 static ng_disconnect_t ng_bpf_disconnect;
107 /* Maximum bpf program instructions */
108 extern int bpf_maxinsns;
110 /* Internal helper functions */
111 static int ng_bpf_setprog(hook_p hook, const struct ng_bpf_hookprog *hp);
113 /* Parse type for one struct bfp_insn */
114 static const struct ng_parse_struct_field ng_bpf_insn_type_fields[] = {
115 { "code", &ng_parse_hint16_type },
116 { "jt", &ng_parse_uint8_type },
117 { "jf", &ng_parse_uint8_type },
118 { "k", &ng_parse_uint32_type },
121 static const struct ng_parse_type ng_bpf_insn_type = {
122 &ng_parse_struct_type,
123 &ng_bpf_insn_type_fields
126 /* Parse type for the field 'bpf_prog' in struct ng_bpf_hookprog */
128 ng_bpf_hookprogary_getLength(const struct ng_parse_type *type,
129 const u_char *start, const u_char *buf)
131 const struct ng_bpf_hookprog *hp;
133 hp = (const struct ng_bpf_hookprog *)
134 (buf - OFFSETOF(struct ng_bpf_hookprog, bpf_prog));
135 return hp->bpf_prog_len;
138 static const struct ng_parse_array_info ng_bpf_hookprogary_info = {
140 &ng_bpf_hookprogary_getLength,
143 static const struct ng_parse_type ng_bpf_hookprogary_type = {
144 &ng_parse_array_type,
145 &ng_bpf_hookprogary_info
148 /* Parse type for struct ng_bpf_hookprog */
149 static const struct ng_parse_struct_field ng_bpf_hookprog_type_fields[]
150 = NG_BPF_HOOKPROG_TYPE_INFO(&ng_bpf_hookprogary_type);
151 static const struct ng_parse_type ng_bpf_hookprog_type = {
152 &ng_parse_struct_type,
153 &ng_bpf_hookprog_type_fields
156 /* Parse type for struct ng_bpf_hookstat */
157 static const struct ng_parse_struct_field ng_bpf_hookstat_type_fields[]
158 = NG_BPF_HOOKSTAT_TYPE_INFO;
159 static const struct ng_parse_type ng_bpf_hookstat_type = {
160 &ng_parse_struct_type,
161 &ng_bpf_hookstat_type_fields
164 /* List of commands and how to convert arguments to/from ASCII */
165 static const struct ng_cmdlist ng_bpf_cmdlist[] = {
170 &ng_bpf_hookprog_type,
177 &ng_parse_hookbuf_type,
178 &ng_bpf_hookprog_type
184 &ng_parse_hookbuf_type,
185 &ng_bpf_hookstat_type
191 &ng_parse_hookbuf_type,
196 NGM_BPF_GETCLR_STATS,
198 &ng_parse_hookbuf_type,
199 &ng_bpf_hookstat_type
204 /* Netgraph type descriptor */
205 static struct ng_type typestruct = {
206 .version = NG_ABI_VERSION,
207 .name = NG_BPF_NODE_TYPE,
208 .constructor = ng_bpf_constructor,
209 .rcvmsg = ng_bpf_rcvmsg,
210 .shutdown = ng_bpf_shutdown,
211 .newhook = ng_bpf_newhook,
212 .rcvdata = ng_bpf_rcvdata,
213 .disconnect = ng_bpf_disconnect,
214 .cmdlist = ng_bpf_cmdlist,
216 NETGRAPH_INIT(bpf, &typestruct);
218 /* Default BPF program for a hook that matches nothing */
219 static const struct ng_bpf_hookprog ng_bpf_default_prog = {
220 { '\0' }, /* to be filled in at hook creation time */
224 { BPF_STMT(BPF_RET+BPF_K, 0) }
230 * We don't keep any per-node private data
231 * We go via the hooks.
234 ng_bpf_constructor(node_p node)
236 NG_NODE_SET_PRIVATE(node, NULL);
241 * Callback functions to be used by NG_NODE_FOREACH_HOOK() macro.
244 ng_bpf_addrefs(hook_p hook, void* arg)
246 hinfo_p hip = NG_HOOK_PRIVATE(hook);
247 hook_p h = (hook_p)arg;
249 if (strcmp(hip->prog->ifMatch, NG_HOOK_NAME(h)) == 0)
251 if (strcmp(hip->prog->ifNotMatch, NG_HOOK_NAME(h)) == 0)
257 ng_bpf_remrefs(hook_p hook, void* arg)
259 hinfo_p hip = NG_HOOK_PRIVATE(hook);
260 hook_p h = (hook_p)arg;
264 if (hip->nomatch == h)
273 ng_bpf_newhook(node_p node, hook_p hook, const char *name)
279 /* Create hook private structure */
280 hip = malloc(sizeof(*hip), M_NETGRAPH_BPF, M_NOWAIT | M_ZERO);
284 NG_HOOK_SET_PRIVATE(hook, hip);
286 /* Add our reference into other hooks data. */
287 NG_NODE_FOREACH_HOOK(node, ng_bpf_addrefs, hook, tmp);
289 /* Attach the default BPF program */
290 if ((error = ng_bpf_setprog(hook, &ng_bpf_default_prog)) != 0) {
291 free(hip, M_NETGRAPH_BPF);
292 NG_HOOK_SET_PRIVATE(hook, NULL);
297 strlcpy(hip->prog->thisHook, name, sizeof(hip->prog->thisHook));
302 * Receive a control message
305 ng_bpf_rcvmsg(node_p node, item_p item, hook_p lasthook)
308 struct ng_mesg *resp = NULL;
311 NGI_GET_MSG(item, msg);
312 switch (msg->header.typecookie) {
314 switch (msg->header.cmd) {
315 case NGM_BPF_SET_PROGRAM:
317 struct ng_bpf_hookprog *const
318 hp = (struct ng_bpf_hookprog *)msg->data;
322 if (msg->header.arglen < sizeof(*hp)
323 || msg->header.arglen
324 != NG_BPF_HOOKPROG_SIZE(hp->bpf_prog_len))
328 if ((hook = ng_findhook(node, hp->thisHook)) == NULL)
331 /* Set new program */
332 if ((error = ng_bpf_setprog(hook, hp)) != 0)
337 case NGM_BPF_GET_PROGRAM:
339 struct ng_bpf_hookprog *hp;
343 if (msg->header.arglen == 0)
345 msg->data[msg->header.arglen - 1] = '\0';
348 if ((hook = ng_findhook(node, msg->data)) == NULL)
352 hp = ((hinfo_p)NG_HOOK_PRIVATE(hook))->prog;
353 NG_MKRESPONSE(resp, msg,
354 NG_BPF_HOOKPROG_SIZE(hp->bpf_prog_len), M_NOWAIT);
357 bcopy(hp, resp->data,
358 NG_BPF_HOOKPROG_SIZE(hp->bpf_prog_len));
362 case NGM_BPF_GET_STATS:
363 case NGM_BPF_CLR_STATS:
364 case NGM_BPF_GETCLR_STATS:
366 struct ng_bpf_hookstat *stats;
370 if (msg->header.arglen == 0)
372 msg->data[msg->header.arglen - 1] = '\0';
375 if ((hook = ng_findhook(node, msg->data)) == NULL)
377 stats = &((hinfo_p)NG_HOOK_PRIVATE(hook))->stats;
379 /* Build response (if desired) */
380 if (msg->header.cmd != NGM_BPF_CLR_STATS) {
382 msg, sizeof(*stats), M_NOWAIT);
385 bcopy(stats, resp->data, sizeof(*stats));
388 /* Clear stats (if desired) */
389 if (msg->header.cmd != NGM_BPF_GET_STATS)
390 bzero(stats, sizeof(*stats));
404 NG_RESPOND_MSG(error, node, item, resp);
410 * Receive data on a hook
412 * Apply the filter, and then drop or forward packet as appropriate.
415 ng_bpf_rcvdata(hook_p hook, item_p item)
417 const hinfo_p hip = NG_HOOK_PRIVATE(hook);
419 int needfree = 0, error = 0, usejit = 0;
426 m = NGI_M(item); /* 'item' still owns it.. we are peeking */
427 totlen = m->m_pkthdr.len;
428 /* Update stats on incoming hook. XXX Can we do 64 bits atomically? */
429 /* atomic_add_int64(&hip->stats.recvFrames, 1); */
430 /* atomic_add_int64(&hip->stats.recvOctets, totlen); */
431 hip->stats.recvFrames++;
432 hip->stats.recvOctets += totlen;
434 /* Don't call bpf_filter() with totlen == 0! */
441 if (bpf_jitter_enable != 0 && hip->jit_prog != NULL)
445 /* Need to put packet in contiguous memory for bpf */
446 if (m->m_next != NULL && totlen > MHLEN) {
448 data = malloc(totlen, M_NETGRAPH_BPF, M_NOWAIT);
454 m_copydata(m, 0, totlen, (caddr_t)data);
457 if (m->m_next != NULL) {
458 NGI_M(item) = m = m_pullup(m, totlen);
464 data = mtod(m, u_char *);
467 /* Run packet through filter */
470 len = (*(hip->jit_prog->func))(data, totlen, totlen);
474 len = bpf_filter(hip->prog->bpf_prog, data, totlen, totlen);
476 len = bpf_filter(hip->prog->bpf_prog, (u_char *)m, totlen, 0);
478 free(data, M_NETGRAPH_BPF);
480 /* See if we got a match and find destination hook */
484 /* XXX atomically? */
485 hip->stats.recvMatchFrames++;
486 hip->stats.recvMatchOctets += totlen;
488 /* Truncate packet length if required by the filter */
489 /* Assume this never changes m */
491 m_adj(m, -(totlen - len));
502 /* Deliver frame out destination hook */
503 dhip = NG_HOOK_PRIVATE(dest);
504 dhip->stats.xmitOctets += totlen;
505 dhip->stats.xmitFrames++;
506 NG_FWD_ITEM_HOOK(error, item, dest);
511 * Shutdown processing
514 ng_bpf_shutdown(node_p node)
524 ng_bpf_disconnect(hook_p hook)
526 const node_p node = NG_HOOK_NODE(hook);
527 const hinfo_p hip = NG_HOOK_PRIVATE(hook);
530 KASSERT(hip != NULL, ("%s: null info", __func__));
532 /* Remove our reference from other hooks data. */
533 NG_NODE_FOREACH_HOOK(node, ng_bpf_remrefs, hook, tmp);
535 free(hip->prog, M_NETGRAPH_BPF);
537 if (hip->jit_prog != NULL)
538 bpf_destroy_jit_filter(hip->jit_prog);
540 free(hip, M_NETGRAPH_BPF);
541 if ((NG_NODE_NUMHOOKS(node) == 0) &&
542 (NG_NODE_IS_VALID(node))) {
543 ng_rmnode_self(node);
548 /************************************************************************
550 ************************************************************************/
553 * Set the BPF program associated with a hook
556 ng_bpf_setprog(hook_p hook, const struct ng_bpf_hookprog *hp0)
558 const hinfo_p hip = NG_HOOK_PRIVATE(hook);
559 struct ng_bpf_hookprog *hp;
561 bpf_jit_filter *jit_prog;
565 /* Check program for validity */
566 if (hp0->bpf_prog_len > bpf_maxinsns ||
567 !bpf_validate(hp0->bpf_prog, hp0->bpf_prog_len))
570 /* Make a copy of the program */
571 size = NG_BPF_HOOKPROG_SIZE(hp0->bpf_prog_len);
572 hp = malloc(size, M_NETGRAPH_BPF, M_NOWAIT);
575 bcopy(hp0, hp, size);
577 jit_prog = bpf_jitter(hp->bpf_prog, hp->bpf_prog_len);
580 /* Free previous program, if any, and assign new one */
581 if (hip->prog != NULL)
582 free(hip->prog, M_NETGRAPH_BPF);
585 if (hip->jit_prog != NULL)
586 bpf_destroy_jit_filter(hip->jit_prog);
587 hip->jit_prog = jit_prog;
590 /* Prepare direct references on target hooks. */
591 hip->match = ng_findhook(NG_HOOK_NODE(hook), hip->prog->ifMatch);
592 hip->nomatch = ng_findhook(NG_HOOK_NODE(hook), hip->prog->ifNotMatch);