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 OFFSETOF(s, e) ((char *)&((s *)0)->e - (char *)((s *)0))
72 #define ERROUT(x) do { error = (x); goto done; } while (0)
74 /* Per hook private info */
75 struct ng_bpf_hookinfo
{
78 struct ng_bpf_hookprog
*prog
;
79 struct ng_bpf_hookstat stats
;
81 typedef struct ng_bpf_hookinfo
*hinfo_p
;
83 /* Netgraph methods */
84 static ng_constructor_t ng_bpf_constructor
;
85 static ng_rcvmsg_t ng_bpf_rcvmsg
;
86 static ng_shutdown_t ng_bpf_rmnode
;
87 static ng_newhook_t ng_bpf_newhook
;
88 static ng_rcvdata_t ng_bpf_rcvdata
;
89 static ng_disconnect_t ng_bpf_disconnect
;
91 /* Internal helper functions */
92 static int ng_bpf_setprog(hook_p hook
, const struct ng_bpf_hookprog
*hp
);
94 /* Parse type for one struct bfp_insn */
95 static const struct ng_parse_struct_field ng_bpf_insn_type_fields
[] = {
96 { "code", &ng_parse_hint16_type
},
97 { "jt", &ng_parse_uint8_type
},
98 { "jf", &ng_parse_uint8_type
},
99 { "k", &ng_parse_uint32_type
},
102 static const struct ng_parse_type ng_bpf_insn_type
= {
103 &ng_parse_struct_type
,
104 &ng_bpf_insn_type_fields
107 /* Parse type for the field 'bpf_prog' in struct ng_bpf_hookprog */
109 ng_bpf_hookprogary_getLength(const struct ng_parse_type
*type
,
110 const u_char
*start
, const u_char
*buf
)
112 const struct ng_bpf_hookprog
*hp
;
114 hp
= (const struct ng_bpf_hookprog
*)
115 (buf
- OFFSETOF(struct ng_bpf_hookprog
, bpf_prog
));
116 return hp
->bpf_prog_len
;
119 static const struct ng_parse_array_info ng_bpf_hookprogary_info
= {
121 &ng_bpf_hookprogary_getLength
,
124 static const struct ng_parse_type ng_bpf_hookprogary_type
= {
125 &ng_parse_array_type
,
126 &ng_bpf_hookprogary_info
129 /* Parse type for struct ng_bpf_hookprog */
130 static const struct ng_parse_struct_field ng_bpf_hookprog_type_fields
[]
131 = NG_BPF_HOOKPROG_TYPE_INFO(&ng_bpf_hookprogary_type
);
132 static const struct ng_parse_type ng_bpf_hookprog_type
= {
133 &ng_parse_struct_type
,
134 &ng_bpf_hookprog_type_fields
137 /* Parse type for struct ng_bpf_hookstat */
138 static const struct ng_parse_struct_field ng_bpf_hookstat_type_fields
[]
139 = NG_BPF_HOOKSTAT_TYPE_INFO
;
140 static const struct ng_parse_type ng_bpf_hookstat_type
= {
141 &ng_parse_struct_type
,
142 &ng_bpf_hookstat_type_fields
145 /* List of commands and how to convert arguments to/from ASCII */
146 static const struct ng_cmdlist ng_bpf_cmdlist
[] = {
151 &ng_bpf_hookprog_type
,
158 &ng_parse_hookbuf_type
,
159 &ng_bpf_hookprog_type
165 &ng_parse_hookbuf_type
,
166 &ng_bpf_hookstat_type
172 &ng_parse_hookbuf_type
,
177 NGM_BPF_GETCLR_STATS
,
179 &ng_parse_hookbuf_type
,
180 &ng_bpf_hookstat_type
185 /* Netgraph type descriptor */
186 static struct ng_type typestruct
= {
201 NETGRAPH_INIT(bpf
, &typestruct
);
203 /* Default BPF program for a hook that matches nothing */
204 static const struct ng_bpf_hookprog ng_bpf_default_prog
= {
205 { '\0' }, /* to be filled in at hook creation time */
209 { BPF_STMT(BPF_RET
+BPF_K
, 0) }
215 * We don't keep any per-node private data
218 ng_bpf_constructor(node_p
*nodep
)
222 if ((error
= ng_make_node_common(&typestruct
, nodep
)))
224 (*nodep
)->private = NULL
;
232 ng_bpf_newhook(node_p node
, hook_p hook
, const char *name
)
237 /* Create hook private structure */
238 hip
= kmalloc(sizeof(*hip
), M_NETGRAPH
, M_NOWAIT
| M_ZERO
);
245 /* Attach the default BPF program */
246 if ((error
= ng_bpf_setprog(hook
, &ng_bpf_default_prog
)) != 0) {
247 kfree(hip
, M_NETGRAPH
);
248 hook
->private = NULL
;
253 strncpy(hip
->prog
->thisHook
, name
, sizeof(hip
->prog
->thisHook
) - 1);
254 hip
->prog
->thisHook
[sizeof(hip
->prog
->thisHook
) - 1] = '\0';
259 * Receive a control message
262 ng_bpf_rcvmsg(node_p node
, struct ng_mesg
*msg
, const char *retaddr
,
263 struct ng_mesg
**rptr
)
265 struct ng_mesg
*resp
= NULL
;
268 switch (msg
->header
.typecookie
) {
270 switch (msg
->header
.cmd
) {
271 case NGM_BPF_SET_PROGRAM
:
273 struct ng_bpf_hookprog
*const
274 hp
= (struct ng_bpf_hookprog
*)msg
->data
;
278 if (msg
->header
.arglen
< sizeof(*hp
)
279 || msg
->header
.arglen
280 != NG_BPF_HOOKPROG_SIZE(hp
->bpf_prog_len
))
284 if ((hook
= ng_findhook(node
, hp
->thisHook
)) == NULL
)
287 /* Set new program */
288 if ((error
= ng_bpf_setprog(hook
, hp
)) != 0)
293 case NGM_BPF_GET_PROGRAM
:
295 struct ng_bpf_hookprog
*hp
;
299 if (msg
->header
.arglen
== 0)
301 msg
->data
[msg
->header
.arglen
- 1] = '\0';
304 if ((hook
= ng_findhook(node
, msg
->data
)) == NULL
)
308 hp
= ((hinfo_p
)hook
->private)->prog
;
309 NG_MKRESPONSE(resp
, msg
,
310 NG_BPF_HOOKPROG_SIZE(hp
->bpf_prog_len
), M_NOWAIT
);
313 bcopy(hp
, resp
->data
,
314 NG_BPF_HOOKPROG_SIZE(hp
->bpf_prog_len
));
318 case NGM_BPF_GET_STATS
:
319 case NGM_BPF_CLR_STATS
:
320 case NGM_BPF_GETCLR_STATS
:
322 struct ng_bpf_hookstat
*stats
;
326 if (msg
->header
.arglen
== 0)
328 msg
->data
[msg
->header
.arglen
- 1] = '\0';
331 if ((hook
= ng_findhook(node
, msg
->data
)) == NULL
)
333 stats
= &((hinfo_p
)hook
->private)->stats
;
335 /* Build response (if desired) */
336 if (msg
->header
.cmd
!= NGM_BPF_CLR_STATS
) {
338 msg
, sizeof(*stats
), M_NOWAIT
);
341 bcopy(stats
, resp
->data
, sizeof(*stats
));
344 /* Clear stats (if desired) */
345 if (msg
->header
.cmd
!= NGM_BPF_GET_STATS
)
346 bzero(stats
, sizeof(*stats
));
362 kfree(resp
, M_NETGRAPH
);
365 kfree(msg
, M_NETGRAPH
);
370 * Receive data on a hook
372 * Apply the filter, and then drop or forward packet as appropriate.
375 ng_bpf_rcvdata(hook_p hook
, struct mbuf
*m
, meta_p meta
)
377 const hinfo_p hip
= hook
->private;
378 int totlen
= m
->m_pkthdr
.len
;
379 int needfree
= 0, error
= 0;
380 u_char
*data
, buf
[256];
385 /* Update stats on incoming hook */
386 hip
->stats
.recvFrames
++;
387 hip
->stats
.recvOctets
+= totlen
;
389 /* Need to put packet in contiguous memory for bpf */
390 if (m
->m_next
!= NULL
) {
391 if (totlen
> sizeof(buf
)) {
392 data
= kmalloc(totlen
, M_NETGRAPH
, M_NOWAIT
);
394 NG_FREE_DATA(m
, meta
);
400 m_copydata(m
, 0, totlen
, (caddr_t
)data
);
402 data
= mtod(m
, u_char
*);
404 /* Run packet through filter */
405 len
= bpf_filter(hip
->prog
->bpf_prog
, data
, totlen
, totlen
);
407 kfree(data
, M_NETGRAPH
);
409 /* See if we got a match and find destination hook */
413 hip
->stats
.recvMatchFrames
++;
414 hip
->stats
.recvMatchOctets
+= totlen
;
416 /* Truncate packet length if required by the filter */
418 m_adj(m
, -(totlen
- len
));
421 dest
= ng_findhook(hip
->node
, hip
->prog
->ifMatch
);
423 dest
= ng_findhook(hip
->node
, hip
->prog
->ifNotMatch
);
425 NG_FREE_DATA(m
, meta
);
429 /* Deliver frame out destination hook */
430 dhip
= (hinfo_p
)dest
->private;
431 dhip
->stats
.xmitOctets
+= totlen
;
432 dhip
->stats
.xmitFrames
++;
433 NG_SEND_DATA(error
, dest
, m
, meta
);
438 * Shutdown processing
441 ng_bpf_rmnode(node_p node
)
443 node
->flags
|= NG_INVALID
;
454 ng_bpf_disconnect(hook_p hook
)
456 const hinfo_p hip
= hook
->private;
458 KASSERT(hip
!= NULL
, ("%s: null info", __func__
));
459 kfree(hip
->prog
, M_NETGRAPH
);
460 bzero(hip
, sizeof(*hip
));
461 kfree(hip
, M_NETGRAPH
);
462 hook
->private = NULL
; /* for good measure */
463 if (hook
->node
->numhooks
== 0)
464 ng_rmnode(hook
->node
);
468 /************************************************************************
470 ************************************************************************/
473 * Set the BPF program associated with a hook
476 ng_bpf_setprog(hook_p hook
, const struct ng_bpf_hookprog
*hp0
)
478 const hinfo_p hip
= hook
->private;
479 struct ng_bpf_hookprog
*hp
;
482 /* Check program for validity */
483 if (!bpf_validate(hp0
->bpf_prog
, hp0
->bpf_prog_len
))
486 /* Make a copy of the program */
487 size
= NG_BPF_HOOKPROG_SIZE(hp0
->bpf_prog_len
);
488 hp
= kmalloc(size
, M_NETGRAPH
, M_NOWAIT
);
491 bcopy(hp0
, hp
, size
);
493 /* Free previous program, if any, and assign new one */
494 if (hip
->prog
!= NULL
)
495 kfree(hip
->prog
, M_NETGRAPH
);