2 * ng_etf.c Ethertype filter
4 * Copyright (c) 2001, FreeBSD Incorporated
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice unmodified, this list of conditions, and the following
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * Author: Julian Elischer <julian@freebsd.org>
31 * $FreeBSD: src/sys/netgraph/ng_etf.c,v 1.1.2.2 2002/07/02 23:44:02 archie Exp $
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
38 #include <sys/malloc.h>
39 #include <sys/ctype.h>
40 #include <sys/errno.h>
41 #include <sys/queue.h>
42 #include <sys/syslog.h>
44 #include <net/ethernet.h>
46 #include <netgraph/ng_message.h>
47 #include <netgraph/ng_parse.h>
49 #include <netgraph/netgraph.h>
51 /* If you do complicated mallocs you may want to do this */
52 /* and use it for your mallocs */
53 #ifdef NG_SEPARATE_MALLOC
54 MALLOC_DEFINE(M_NETGRAPH_ETF, "netgraph_etf", "netgraph etf node ");
56 #define M_NETGRAPH_ETF M_NETGRAPH
60 * This section contains the netgraph method declarations for the
61 * etf node. These methods define the netgraph 'type'.
64 static ng_constructor_t ng_etf_constructor;
65 static ng_rcvmsg_t ng_etf_rcvmsg;
66 static ng_shutdown_t ng_etf_shutdown;
67 static ng_newhook_t ng_etf_newhook;
68 static ng_connect_t ng_etf_connect;
69 static ng_rcvdata_t ng_etf_rcvdata; /* note these are both ng_rcvdata_t */
70 static ng_disconnect_t ng_etf_disconnect;
72 /* Parse type for struct ng_etfstat */
73 static const struct ng_parse_struct_field ng_etf_stat_type_fields[]
74 = NG_ETF_STATS_TYPE_INFO;
75 static const struct ng_parse_type ng_etf_stat_type = {
76 &ng_parse_struct_type,
77 &ng_etf_stat_type_fields
79 /* Parse type for struct ng_setfilter */
80 static const struct ng_parse_struct_field ng_etf_filter_type_fields[]
81 = NG_ETF_FILTER_TYPE_INFO;
82 static const struct ng_parse_type ng_etf_filter_type = {
83 &ng_parse_struct_type,
84 &ng_etf_filter_type_fields
87 /* List of commands and how to convert arguments to/from ASCII */
88 static const struct ng_cmdlist ng_etf_cmdlist[] = {
100 &ng_parse_int32_type,
113 /* Netgraph node type descriptor */
114 static struct ng_type typestruct = {
129 NETGRAPH_INIT(etf, &typestruct);
131 /* Information we store for each hook on each node */
132 struct ETF_hookinfo {
137 LIST_ENTRY(filter) next;
138 u_int16_t ethertype; /* network order ethertype */
139 hook_p match_hook; /* Hook to use on a match */
142 #define HASHSIZE 16 /* Dont change this without changing HASH() */
143 #define HASH(et) ((((et)>>12)+((et)>>8)+((et)>>4)+(et)) & 0x0f)
144 LIST_HEAD(filterhead, filter);
146 /* Information we store for each node */
148 struct ETF_hookinfo downstream_hook;
149 struct ETF_hookinfo nomatch_hook;
150 node_p node; /* back pointer to node */
151 u_int packets_in; /* packets in from downstream */
152 u_int packets_out; /* packets out towards downstream */
154 struct filterhead hashtable[HASHSIZE];
156 typedef struct ETF *etf_p;
158 static struct filter *
159 ng_etf_findentry(etf_p etfp, u_int16_t ethertype)
161 struct filterhead *chain = etfp->hashtable + HASH(ethertype);
165 LIST_FOREACH(fil, chain, next) {
166 if (fil->ethertype == ethertype) {
175 * Allocate the private data structure. The generic node has already
176 * been created. Link them together. We arrive with a reference to the node
177 * i.e. the reference count is incremented for us already.
180 ng_etf_constructor(node_p *nodep)
185 /* Initialize private descriptor */
186 privdata = kmalloc(sizeof(*privdata), M_NETGRAPH_ETF,
188 if (privdata == NULL)
190 for (i = 0; i < HASHSIZE; i++) {
191 LIST_INIT((privdata->hashtable + i));
194 /* Call the 'generic' (ie, superclass) node constructor */
195 if ((error = ng_make_node_common(&typestruct, nodep))) {
196 kfree(privdata, M_NETGRAPH);
199 /* Link structs together; this counts as our one reference to node */
200 NG_NODE_SET_PRIVATE((*nodep), privdata);
201 privdata->node = *nodep;
206 * Give our ok for a hook to be added...
207 * All names are ok. Two names are special.
210 ng_etf_newhook(node_p node, hook_p hook, const char *name)
212 const etf_p etfp = NG_NODE_PRIVATE(node);
213 struct ETF_hookinfo *hpriv;
215 if (strcmp(name, NG_ETF_HOOK_DOWNSTREAM) == 0) {
216 etfp->downstream_hook.hook = hook;
217 NG_HOOK_SET_PRIVATE(hook, &etfp->downstream_hook);
218 etfp->packets_in = 0;
219 etfp->packets_out = 0;
220 } else if (strcmp(name, NG_ETF_HOOK_NOMATCH) == 0) {
221 etfp->nomatch_hook.hook = hook;
222 NG_HOOK_SET_PRIVATE(hook, &etfp->nomatch_hook);
225 * Any other hook name is valid and can
226 * later be associated with a filter rule.
228 hpriv = kmalloc(sizeof(*hpriv), M_NETGRAPH_ETF,
234 NG_HOOK_SET_PRIVATE(hook, hpriv);
241 * Get a netgraph control message.
242 * We actually recieve a queue item that has a pointer to the message.
243 * If we free the item, the message will be freed too, unless we remove
244 * it from the item using NGI_GET_MSG();
245 * The return address is also stored in the item, as an ng_ID_t,
246 * accessible as NGI_RETADDR(item);
247 * Check it is one we understand. If needed, send a response.
248 * We could save the address for an async action later, but don't here.
249 * Always free the message.
250 * The response should be in a malloc'd region that the caller can 'free'.
251 * The NG_MKRESPONSE macro does all this for us.
252 * A response is not required.
253 * Theoretically you could respond defferently to old message types if
254 * the cookie in the header didn't match what we consider to be current
255 * (so that old userland programs could continue to work).
258 ng_etf_rcvmsg(node_p node, struct ng_mesg *msg,
259 const char *retaddr, struct ng_mesg **rptr)
261 const etf_p etfp = NG_NODE_PRIVATE(node);
262 struct ng_mesg *resp = NULL;
265 /* Deal with message according to cookie and command */
266 switch (msg->header.typecookie) {
268 switch (msg->header.cmd) {
269 case NGM_ETF_GET_STATUS:
271 struct ng_etfstat *stats;
273 NG_MKRESPONSE(resp, msg, sizeof(*stats), M_NOWAIT);
278 stats = (struct ng_etfstat *) resp->data;
279 stats->packets_in = etfp->packets_in;
280 stats->packets_out = etfp->packets_out;
283 case NGM_ETF_SET_FLAG:
284 if (msg->header.arglen != sizeof(u_int32_t)) {
288 etfp->flags = *((u_int32_t *) msg->data);
290 case NGM_ETF_SET_FILTER:
292 struct ng_etffilter *f;
296 /* Check message long enough for this command */
297 if (msg->header.arglen != sizeof(*f)) {
302 /* Make sure hook referenced exists */
303 f = (struct ng_etffilter *)msg->data;
304 hook = ng_findhook(node, f->matchhook);
310 /* and is not the downstream hook */
311 if (hook == etfp->downstream_hook.hook) {
316 /* Check we don't already trap this ethertype */
317 if (ng_etf_findentry(etfp,
318 htons(f->ethertype))) {
324 * Ok, make the filter and put it in the
325 * hashtable ready for matching.
327 fil = kmalloc(sizeof(*fil), M_NETGRAPH_ETF,
333 fil->match_hook = hook;
334 fil->ethertype = htons(f->ethertype);
335 LIST_INSERT_HEAD( etfp->hashtable
336 + HASH(fil->ethertype),
341 error = EINVAL; /* unknown command */
346 error = EINVAL; /* unknown cookie type */
350 /* Take care of synchronous response, if any */
351 NG_RESPOND_MSG(error, node, retaddr, resp, rptr);
352 /* Free the message and return */
358 * Receive data, and do something with it.
359 * Actually we receive a queue item which holds the data.
360 * If we free the item it wil also froo the data and metadata unless
361 * we have previously disassociated them using the NGI_GET_etf() macros.
362 * Possibly send it out on another link after processing.
363 * Possibly do something different if it comes from different
364 * hooks. the caller will never free m or meta, so
365 * if we use up this data or abort we must free BOTH of these.
367 * If we want, we may decide to force this data to be queued and reprocessed
368 * at the netgraph NETISR time.
369 * We would do that by setting the HK_QUEUE flag on our hook. We would do that
370 * in the connect() method.
373 ng_etf_rcvdata(hook_p hook, struct mbuf *m, meta_p meta)
375 const etf_p etfp = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
376 struct ether_header *eh;
381 if (NG_HOOK_PRIVATE(hook) == NULL) { /* Shouldn't happen but.. */
382 NG_FREE_DATA(m, meta);
386 * Everything not from the downstream hook goes to the
387 * downstream hook. But only if it matches the ethertype
388 * of the source hook. Un matching must go to/from 'nomatch'.
391 /* Make sure we have an entire header */
392 if (m->m_len < sizeof(*eh) ) {
393 m = m_pullup(m, sizeof(*eh));
400 eh = mtod(m, struct ether_header *);
401 ethertype = eh->ether_type;
402 fil = ng_etf_findentry(etfp, ethertype);
405 * if from downstream, select between a match hook or
408 if (hook == etfp->downstream_hook.hook) {
410 if (fil && fil->match_hook) {
411 NG_SEND_DATA(error, fil->match_hook, m, meta);
413 NG_SEND_DATA(error, etfp->nomatch_hook.hook, m, meta);
417 * It must be heading towards the downstream.
418 * Check that it's ethertype matches
419 * the filters for it's input hook.
420 * If it doesn't have one, check it's from nomatch.
422 if ((fil && (fil->match_hook != hook))
423 || ((fil == NULL) && (hook != etfp->nomatch_hook.hook))) {
424 NG_FREE_DATA(m, meta);
427 NG_SEND_DATA( error, etfp->downstream_hook.hook, m, meta);
436 * Do local shutdown processing..
437 * All our links and the name have already been removed.
440 ng_etf_shutdown(node_p node)
442 const etf_p privdata = NG_NODE_PRIVATE(node);
444 NG_NODE_SET_PRIVATE(node, NULL);
445 NG_NODE_UNREF(privdata->node);
446 kfree(privdata, M_NETGRAPH_ETF);
451 * This is called once we've already connected a new hook to the other node.
452 * It gives us a chance to balk at the last minute.
455 ng_etf_connect(hook_p hook)
463 * For this type, removal of the last link destroys the node
466 ng_etf_disconnect(hook_p hook)
468 const etf_p etfp = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
472 /* purge any rules that refer to this filter */
473 for (i = 0; i < HASHSIZE; i++) {
474 LIST_FOREACH(fil, (etfp->hashtable + i), next) {
475 if (fil->match_hook == hook) {
476 LIST_REMOVE(fil, next);
481 /* If it's not one of the special hooks, then free it */
482 if (hook == etfp->downstream_hook.hook) {
483 etfp->downstream_hook.hook = NULL;
484 } else if (hook == etfp->nomatch_hook.hook) {
485 etfp->nomatch_hook.hook = NULL;
487 if (NG_HOOK_PRIVATE(hook)) /* Paranoia */
488 kfree(NG_HOOK_PRIVATE(hook), M_NETGRAPH_ETF);
491 NG_HOOK_SET_PRIVATE(hook, NULL);
493 if ((NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0)
494 && (NG_NODE_IS_VALID(NG_HOOK_NODE(hook)))) /* already shutting down? */
495 ng_rmnode(NG_HOOK_NODE(hook));