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, GUARANBPF, 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>
39 * $FreeBSD: src/sys/netgraph/ng_bpf.c,v 1.2.4.4 2002/07/02 23:44:02 archie Exp $
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>
70 #define ERROUT(x) do { error = (x); goto done; } while (0)
72 /* Per hook private info */
73 struct ng_bpf_hookinfo
{
76 struct ng_bpf_hookprog
*prog
;
77 struct ng_bpf_hookstat stats
;
79 typedef struct ng_bpf_hookinfo
*hinfo_p
;
81 /* Netgraph methods */
82 static ng_constructor_t ng_bpf_constructor
;
83 static ng_rcvmsg_t ng_bpf_rcvmsg
;
84 static ng_shutdown_t ng_bpf_rmnode
;
85 static ng_newhook_t ng_bpf_newhook
;
86 static ng_rcvdata_t ng_bpf_rcvdata
;
87 static ng_disconnect_t ng_bpf_disconnect
;
89 /* Internal helper functions */
90 static int ng_bpf_setprog(hook_p hook
, const struct ng_bpf_hookprog
*hp
);
92 /* Parse type for one struct bfp_insn */
93 static const struct ng_parse_struct_field ng_bpf_insn_type_fields
[] = {
94 { "code", &ng_parse_hint16_type
},
95 { "jt", &ng_parse_uint8_type
},
96 { "jf", &ng_parse_uint8_type
},
97 { "k", &ng_parse_uint32_type
},
100 static const struct ng_parse_type ng_bpf_insn_type
= {
101 &ng_parse_struct_type
,
102 &ng_bpf_insn_type_fields
105 /* Parse type for the field 'bpf_prog' in struct ng_bpf_hookprog */
107 ng_bpf_hookprogary_getLength(const struct ng_parse_type
*type
,
108 const u_char
*start
, const u_char
*buf
)
110 const struct ng_bpf_hookprog
*hp
;
112 hp
= (const struct ng_bpf_hookprog
*)
113 (buf
- __offsetof(struct ng_bpf_hookprog
, bpf_prog
));
114 return hp
->bpf_prog_len
;
117 static const struct ng_parse_array_info ng_bpf_hookprogary_info
= {
119 &ng_bpf_hookprogary_getLength
,
122 static const struct ng_parse_type ng_bpf_hookprogary_type
= {
123 &ng_parse_array_type
,
124 &ng_bpf_hookprogary_info
127 /* Parse type for struct ng_bpf_hookprog */
128 static const struct ng_parse_struct_field ng_bpf_hookprog_type_fields
[]
129 = NG_BPF_HOOKPROG_TYPE_INFO(&ng_bpf_hookprogary_type
);
130 static const struct ng_parse_type ng_bpf_hookprog_type
= {
131 &ng_parse_struct_type
,
132 &ng_bpf_hookprog_type_fields
135 /* Parse type for struct ng_bpf_hookstat */
136 static const struct ng_parse_struct_field ng_bpf_hookstat_type_fields
[]
137 = NG_BPF_HOOKSTAT_TYPE_INFO
;
138 static const struct ng_parse_type ng_bpf_hookstat_type
= {
139 &ng_parse_struct_type
,
140 &ng_bpf_hookstat_type_fields
143 /* List of commands and how to convert arguments to/from ASCII */
144 static const struct ng_cmdlist ng_bpf_cmdlist
[] = {
149 &ng_bpf_hookprog_type
,
156 &ng_parse_hookbuf_type
,
157 &ng_bpf_hookprog_type
163 &ng_parse_hookbuf_type
,
164 &ng_bpf_hookstat_type
170 &ng_parse_hookbuf_type
,
175 NGM_BPF_GETCLR_STATS
,
177 &ng_parse_hookbuf_type
,
178 &ng_bpf_hookstat_type
183 /* Netgraph type descriptor */
184 static struct ng_type typestruct
= {
199 NETGRAPH_INIT(bpf
, &typestruct
);
201 /* Default BPF program for a hook that matches nothing */
202 static const struct ng_bpf_hookprog ng_bpf_default_prog
= {
203 { '\0' }, /* to be filled in at hook creation time */
207 { BPF_STMT(BPF_RET
+BPF_K
, 0) }
213 * We don't keep any per-node private data
216 ng_bpf_constructor(node_p
*nodep
)
220 if ((error
= ng_make_node_common(&typestruct
, nodep
)))
222 (*nodep
)->private = NULL
;
230 ng_bpf_newhook(node_p node
, hook_p hook
, const char *name
)
235 /* Create hook private structure */
236 hip
= kmalloc(sizeof(*hip
), M_NETGRAPH
, M_NOWAIT
| M_ZERO
);
243 /* Attach the default BPF program */
244 if ((error
= ng_bpf_setprog(hook
, &ng_bpf_default_prog
)) != 0) {
245 kfree(hip
, M_NETGRAPH
);
246 hook
->private = NULL
;
251 strncpy(hip
->prog
->thisHook
, name
, sizeof(hip
->prog
->thisHook
) - 1);
252 hip
->prog
->thisHook
[sizeof(hip
->prog
->thisHook
) - 1] = '\0';
257 * Receive a control message
260 ng_bpf_rcvmsg(node_p node
, struct ng_mesg
*msg
, const char *retaddr
,
261 struct ng_mesg
**rptr
)
263 struct ng_mesg
*resp
= NULL
;
266 switch (msg
->header
.typecookie
) {
268 switch (msg
->header
.cmd
) {
269 case NGM_BPF_SET_PROGRAM
:
271 struct ng_bpf_hookprog
*const
272 hp
= (struct ng_bpf_hookprog
*)msg
->data
;
276 if (msg
->header
.arglen
< sizeof(*hp
)
277 || msg
->header
.arglen
278 != NG_BPF_HOOKPROG_SIZE(hp
->bpf_prog_len
))
282 if ((hook
= ng_findhook(node
, hp
->thisHook
)) == NULL
)
285 /* Set new program */
286 if ((error
= ng_bpf_setprog(hook
, hp
)) != 0)
291 case NGM_BPF_GET_PROGRAM
:
293 struct ng_bpf_hookprog
*hp
;
297 if (msg
->header
.arglen
== 0)
299 msg
->data
[msg
->header
.arglen
- 1] = '\0';
302 if ((hook
= ng_findhook(node
, msg
->data
)) == NULL
)
306 hp
= ((hinfo_p
)hook
->private)->prog
;
307 NG_MKRESPONSE(resp
, msg
,
308 NG_BPF_HOOKPROG_SIZE(hp
->bpf_prog_len
), M_NOWAIT
);
311 bcopy(hp
, resp
->data
,
312 NG_BPF_HOOKPROG_SIZE(hp
->bpf_prog_len
));
316 case NGM_BPF_GET_STATS
:
317 case NGM_BPF_CLR_STATS
:
318 case NGM_BPF_GETCLR_STATS
:
320 struct ng_bpf_hookstat
*stats
;
324 if (msg
->header
.arglen
== 0)
326 msg
->data
[msg
->header
.arglen
- 1] = '\0';
329 if ((hook
= ng_findhook(node
, msg
->data
)) == NULL
)
331 stats
= &((hinfo_p
)hook
->private)->stats
;
333 /* Build response (if desired) */
334 if (msg
->header
.cmd
!= NGM_BPF_CLR_STATS
) {
336 msg
, sizeof(*stats
), M_NOWAIT
);
339 bcopy(stats
, resp
->data
, sizeof(*stats
));
342 /* Clear stats (if desired) */
343 if (msg
->header
.cmd
!= NGM_BPF_GET_STATS
)
344 bzero(stats
, sizeof(*stats
));
360 kfree(resp
, M_NETGRAPH
);
363 kfree(msg
, M_NETGRAPH
);
368 * Receive data on a hook
370 * Apply the filter, and then drop or forward packet as appropriate.
373 ng_bpf_rcvdata(hook_p hook
, struct mbuf
*m
, meta_p meta
)
375 const hinfo_p hip
= hook
->private;
376 int totlen
= m
->m_pkthdr
.len
;
377 int needfree
= 0, error
= 0;
378 u_char
*data
, buf
[256];
383 /* Update stats on incoming hook */
384 hip
->stats
.recvFrames
++;
385 hip
->stats
.recvOctets
+= totlen
;
387 /* Need to put packet in contiguous memory for bpf */
388 if (m
->m_next
!= NULL
) {
389 if (totlen
> sizeof(buf
)) {
390 data
= kmalloc(totlen
, M_NETGRAPH
, M_NOWAIT
);
392 NG_FREE_DATA(m
, meta
);
398 m_copydata(m
, 0, totlen
, (caddr_t
)data
);
400 data
= mtod(m
, u_char
*);
402 /* Run packet through filter */
403 len
= bpf_filter(hip
->prog
->bpf_prog
, data
, totlen
, totlen
);
405 kfree(data
, M_NETGRAPH
);
407 /* See if we got a match and find destination hook */
411 hip
->stats
.recvMatchFrames
++;
412 hip
->stats
.recvMatchOctets
+= totlen
;
414 /* Truncate packet length if required by the filter */
416 m_adj(m
, -(totlen
- len
));
419 dest
= ng_findhook(hip
->node
, hip
->prog
->ifMatch
);
421 dest
= ng_findhook(hip
->node
, hip
->prog
->ifNotMatch
);
423 NG_FREE_DATA(m
, meta
);
427 /* Deliver frame out destination hook */
428 dhip
= (hinfo_p
)dest
->private;
429 dhip
->stats
.xmitOctets
+= totlen
;
430 dhip
->stats
.xmitFrames
++;
431 NG_SEND_DATA(error
, dest
, m
, meta
);
436 * Shutdown processing
439 ng_bpf_rmnode(node_p node
)
441 node
->flags
|= NG_INVALID
;
452 ng_bpf_disconnect(hook_p hook
)
454 const hinfo_p hip
= hook
->private;
456 KASSERT(hip
!= NULL
, ("%s: null info", __func__
));
457 kfree(hip
->prog
, M_NETGRAPH
);
458 bzero(hip
, sizeof(*hip
));
459 kfree(hip
, M_NETGRAPH
);
460 hook
->private = NULL
; /* for good measure */
461 if (hook
->node
->numhooks
== 0)
462 ng_rmnode(hook
->node
);
466 /************************************************************************
468 ************************************************************************/
471 * Set the BPF program associated with a hook
474 ng_bpf_setprog(hook_p hook
, const struct ng_bpf_hookprog
*hp0
)
476 const hinfo_p hip
= hook
->private;
477 struct ng_bpf_hookprog
*hp
;
480 /* Check program for validity */
481 if (!bpf_validate(hp0
->bpf_prog
, hp0
->bpf_prog_len
))
484 /* Make a copy of the program */
485 size
= NG_BPF_HOOKPROG_SIZE(hp0
->bpf_prog_len
);
486 hp
= kmalloc(size
, M_NETGRAPH
, M_NOWAIT
);
489 bcopy(hp0
, hp
, size
);
491 /* Free previous program, if any, and assign new one */
492 if (hip
->prog
!= NULL
)
493 kfree(hip
->prog
, M_NETGRAPH
);