FreeBSD/Linux Kernel Cross Reference
sys/netgraph/ng_etf.c
1 /*-
2 * ng_etf.c Ethertype filter
3 *
4 * Copyright (c) 2001, FreeBSD Incorporated
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice unmodified, this list of conditions, and the following
12 * disclaimer.
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.
16 *
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
27 * SUCH DAMAGE.
28 *
29 * Author: Julian Elischer <julian@freebsd.org>
30 *
31 * $FreeBSD: releng/5.1/sys/netgraph/ng_etf.c 97685 2002-05-31 23:48:03Z archie $
32 */
33
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/mbuf.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>
43
44 #include <net/ethernet.h>
45
46 #include <netgraph/ng_message.h>
47 #include <netgraph/ng_parse.h>
48 #include <netgraph/ng_etf.h>
49 #include <netgraph/netgraph.h>
50
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 ");
55 #else
56 #define M_NETGRAPH_ETF M_NETGRAPH
57 #endif
58
59 /*
60 * This section contains the netgraph method declarations for the
61 * etf node. These methods define the netgraph 'type'.
62 */
63
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;
71
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
78 };
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
85 };
86
87 /* List of commands and how to convert arguments to/from ASCII */
88 static const struct ng_cmdlist ng_etf_cmdlist[] = {
89 {
90 NGM_ETF_COOKIE,
91 NGM_ETF_GET_STATUS,
92 "getstatus",
93 NULL,
94 &ng_etf_stat_type,
95 },
96 {
97 NGM_ETF_COOKIE,
98 NGM_ETF_SET_FLAG,
99 "setflag",
100 &ng_parse_int32_type,
101 NULL
102 },
103 {
104 NGM_ETF_COOKIE,
105 NGM_ETF_SET_FILTER,
106 "setfilter",
107 &ng_etf_filter_type,
108 NULL
109 },
110 { 0 }
111 };
112
113 /* Netgraph node type descriptor */
114 static struct ng_type typestruct = {
115 NG_ABI_VERSION,
116 NG_ETF_NODE_TYPE,
117 NULL,
118 ng_etf_constructor,
119 ng_etf_rcvmsg,
120 ng_etf_shutdown,
121 ng_etf_newhook,
122 NULL,
123 ng_etf_connect,
124 ng_etf_rcvdata,
125 ng_etf_disconnect,
126 ng_etf_cmdlist
127 };
128 NETGRAPH_INIT(etf, &typestruct);
129
130 /* Information we store for each hook on each node */
131 struct ETF_hookinfo {
132 hook_p hook;
133 };
134
135 struct filter {
136 LIST_ENTRY(filter) next;
137 u_int16_t ethertype; /* network order ethertype */
138 hook_p match_hook; /* Hook to use on a match */
139 };
140
141 #define HASHSIZE 16 /* Dont change this without changing HASH() */
142 #define HASH(et) ((((et)>>12)+((et)>>8)+((et)>>4)+(et)) & 0x0f)
143 LIST_HEAD(filterhead, filter);
144
145 /* Information we store for each node */
146 struct ETF {
147 struct ETF_hookinfo downstream_hook;
148 struct ETF_hookinfo nomatch_hook;
149 node_p node; /* back pointer to node */
150 u_int packets_in; /* packets in from downstream */
151 u_int packets_out; /* packets out towards downstream */
152 u_int32_t flags;
153 struct filterhead hashtable[HASHSIZE];
154 };
155 typedef struct ETF *etf_p;
156
157 static struct filter *
158 ng_etf_findentry(etf_p etfp, u_int16_t ethertype)
159 {
160 struct filterhead *chain = etfp->hashtable + HASH(ethertype);
161 struct filter *fil;
162
163
164 LIST_FOREACH(fil, chain, next) {
165 if (fil->ethertype == ethertype) {
166 return (fil);
167 }
168 }
169 return (NULL);
170 }
171
172
173 /*
174 * Allocate the private data structure. The generic node has already
175 * been created. Link them together. We arrive with a reference to the node
176 * i.e. the reference count is incremented for us already.
177 */
178 static int
179 ng_etf_constructor(node_p node)
180 {
181 etf_p privdata;
182 int i;
183
184 /* Initialize private descriptor */
185 MALLOC(privdata, etf_p, sizeof(*privdata), M_NETGRAPH_ETF,
186 M_NOWAIT | M_ZERO);
187 if (privdata == NULL)
188 return (ENOMEM);
189 for (i = 0; i < HASHSIZE; i++) {
190 LIST_INIT((privdata->hashtable + i));
191 }
192
193 /* Link structs together; this counts as our one reference to node */
194 NG_NODE_SET_PRIVATE(node, privdata);
195 privdata->node = node;
196 return (0);
197 }
198
199 /*
200 * Give our ok for a hook to be added...
201 * All names are ok. Two names are special.
202 */
203 static int
204 ng_etf_newhook(node_p node, hook_p hook, const char *name)
205 {
206 const etf_p etfp = NG_NODE_PRIVATE(node);
207 struct ETF_hookinfo *hpriv;
208
209 if (strcmp(name, NG_ETF_HOOK_DOWNSTREAM) == 0) {
210 etfp->downstream_hook.hook = hook;
211 NG_HOOK_SET_PRIVATE(hook, &etfp->downstream_hook);
212 etfp->packets_in = 0;
213 etfp->packets_out = 0;
214 } else if (strcmp(name, NG_ETF_HOOK_NOMATCH) == 0) {
215 etfp->nomatch_hook.hook = hook;
216 NG_HOOK_SET_PRIVATE(hook, &etfp->nomatch_hook);
217 } else {
218 /*
219 * Any other hook name is valid and can
220 * later be associated with a filter rule.
221 */
222 MALLOC(hpriv, struct ETF_hookinfo *, sizeof(*hpriv),
223 M_NETGRAPH_ETF, M_NOWAIT | M_ZERO);
224 if (hpriv == NULL) {
225 return (ENOMEM);
226 }
227
228 NG_HOOK_SET_PRIVATE(hook, hpriv);
229 hpriv->hook = hook;
230 }
231 return(0);
232 }
233
234 /*
235 * Get a netgraph control message.
236 * We actually recieve a queue item that has a pointer to the message.
237 * If we free the item, the message will be freed too, unless we remove
238 * it from the item using NGI_GET_MSG();
239 * The return address is also stored in the item, as an ng_ID_t,
240 * accessible as NGI_RETADDR(item);
241 * Check it is one we understand. If needed, send a response.
242 * We could save the address for an async action later, but don't here.
243 * Always free the message.
244 * The response should be in a malloc'd region that the caller can 'free'.
245 * The NG_MKRESPONSE macro does all this for us.
246 * A response is not required.
247 * Theoretically you could respond defferently to old message types if
248 * the cookie in the header didn't match what we consider to be current
249 * (so that old userland programs could continue to work).
250 */
251 static int
252 ng_etf_rcvmsg(node_p node, item_p item, hook_p lasthook)
253 {
254 const etf_p etfp = NG_NODE_PRIVATE(node);
255 struct ng_mesg *resp = NULL;
256 int error = 0;
257 struct ng_mesg *msg;
258
259 NGI_GET_MSG(item, msg);
260 /* Deal with message according to cookie and command */
261 switch (msg->header.typecookie) {
262 case NGM_ETF_COOKIE:
263 switch (msg->header.cmd) {
264 case NGM_ETF_GET_STATUS:
265 {
266 struct ng_etfstat *stats;
267
268 NG_MKRESPONSE(resp, msg, sizeof(*stats), M_NOWAIT);
269 if (!resp) {
270 error = ENOMEM;
271 break;
272 }
273 stats = (struct ng_etfstat *) resp->data;
274 stats->packets_in = etfp->packets_in;
275 stats->packets_out = etfp->packets_out;
276 break;
277 }
278 case NGM_ETF_SET_FLAG:
279 if (msg->header.arglen != sizeof(u_int32_t)) {
280 error = EINVAL;
281 break;
282 }
283 etfp->flags = *((u_int32_t *) msg->data);
284 break;
285 case NGM_ETF_SET_FILTER:
286 {
287 struct ng_etffilter *f;
288 struct filter *fil;
289 hook_p hook;
290
291 /* Check message long enough for this command */
292 if (msg->header.arglen != sizeof(*f)) {
293 error = EINVAL;
294 break;
295 }
296
297 /* Make sure hook referenced exists */
298 f = (struct ng_etffilter *)msg->data;
299 hook = ng_findhook(node, f->matchhook);
300 if (hook == NULL) {
301 error = ENOENT;
302 break;
303 }
304
305 /* and is not the downstream hook */
306 if (hook == etfp->downstream_hook.hook) {
307 error = EINVAL;
308 break;
309 }
310
311 /* Check we don't already trap this ethertype */
312 if (ng_etf_findentry(etfp,
313 htons(f->ethertype))) {
314 error = EEXIST;
315 break;
316 }
317
318 /*
319 * Ok, make the filter and put it in the
320 * hashtable ready for matching.
321 */
322 MALLOC(fil, struct filter *, sizeof(*fil),
323 M_NETGRAPH_ETF, M_NOWAIT | M_ZERO);
324 if (fil == NULL) {
325 return (ENOMEM);
326 }
327
328 fil->match_hook = hook;
329 fil->ethertype = htons(f->ethertype);
330 LIST_INSERT_HEAD( etfp->hashtable
331 + HASH(fil->ethertype),
332 fil, next);
333 }
334 break;
335 default:
336 error = EINVAL; /* unknown command */
337 break;
338 }
339 break;
340 default:
341 error = EINVAL; /* unknown cookie type */
342 break;
343 }
344
345 /* Take care of synchronous response, if any */
346 NG_RESPOND_MSG(error, node, item, resp);
347 /* Free the message and return */
348 NG_FREE_MSG(msg);
349 return(error);
350 }
351
352 /*
353 * Receive data, and do something with it.
354 * Actually we receive a queue item which holds the data.
355 * If we free the item it wil also froo the data and metadata unless
356 * we have previously disassociated them using the NGI_GET_etf() macros.
357 * Possibly send it out on another link after processing.
358 * Possibly do something different if it comes from different
359 * hooks. the caller will never free m or meta, so
360 * if we use up this data or abort we must free BOTH of these.
361 *
362 * If we want, we may decide to force this data to be queued and reprocessed
363 * at the netgraph NETISR time.
364 * We would do that by setting the HK_QUEUE flag on our hook. We would do that
365 * in the connect() method.
366 */
367 static int
368 ng_etf_rcvdata(hook_p hook, item_p item )
369 {
370 const etf_p etfp = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
371 struct ether_header *eh;
372 int error = 0;
373 struct mbuf *m;
374 u_int16_t ethertype;
375 struct filter *fil;
376
377 if (NG_HOOK_PRIVATE(hook) == NULL) { /* Shouldn't happen but.. */
378 NG_FREE_ITEM(item);
379 }
380
381 /*
382 * Everything not from the downstream hook goes to the
383 * downstream hook. But only if it matches the ethertype
384 * of the source hook. Un matching must go to/from 'nomatch'.
385 */
386
387 /* Make sure we have an entire header */
388 NGI_GET_M(item, m);
389 if (m->m_len < sizeof(*eh) ) {
390 m = m_pullup(m, sizeof(*eh));
391 if (m == NULL) {
392 NG_FREE_ITEM(item);
393 return(EINVAL);
394 }
395 }
396
397 eh = mtod(m, struct ether_header *);
398 ethertype = eh->ether_type;
399 fil = ng_etf_findentry(etfp, ethertype);
400
401 /*
402 * if from downstream, select between a match hook or
403 * the nomatch hook
404 */
405 if (hook == etfp->downstream_hook.hook) {
406 etfp->packets_in++;
407 if (fil && fil->match_hook) {
408 NG_FWD_NEW_DATA(error, item, fil->match_hook, m);
409 } else {
410 NG_FWD_NEW_DATA(error, item,etfp->nomatch_hook.hook, m);
411 }
412 } else {
413 /*
414 * It must be heading towards the downstream.
415 * Check that it's ethertype matches
416 * the filters for it's input hook.
417 * If it doesn't have one, check it's from nomatch.
418 */
419 if ((fil && (fil->match_hook != hook))
420 || ((fil == NULL) && (hook != etfp->nomatch_hook.hook))) {
421 NG_FREE_ITEM(item);
422 NG_FREE_M(m);
423 return (EPROTOTYPE);
424 }
425 NG_FWD_NEW_DATA( error, item, etfp->downstream_hook.hook, m);
426 if (error == 0) {
427 etfp->packets_out++;
428 }
429 }
430 return (error);
431 }
432
433 /*
434 * Do local shutdown processing..
435 * All our links and the name have already been removed.
436 */
437 static int
438 ng_etf_shutdown(node_p node)
439 {
440 const etf_p privdata = NG_NODE_PRIVATE(node);
441
442 NG_NODE_SET_PRIVATE(node, NULL);
443 NG_NODE_UNREF(privdata->node);
444 FREE(privdata, M_NETGRAPH_ETF);
445 return (0);
446 }
447
448 /*
449 * This is called once we've already connected a new hook to the other node.
450 * It gives us a chance to balk at the last minute.
451 */
452 static int
453 ng_etf_connect(hook_p hook)
454 {
455 return (0);
456 }
457
458 /*
459 * Hook disconnection
460 *
461 * For this type, removal of the last link destroys the node
462 */
463 static int
464 ng_etf_disconnect(hook_p hook)
465 {
466 const etf_p etfp = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
467 int i;
468 struct filter *fil;
469
470 /* purge any rules that refer to this filter */
471 for (i = 0; i < HASHSIZE; i++) {
472 LIST_FOREACH(fil, (etfp->hashtable + i), next) {
473 if (fil->match_hook == hook) {
474 LIST_REMOVE(fil, next);
475 }
476 }
477 }
478
479 /* If it's not one of the special hooks, then free it */
480 if (hook == etfp->downstream_hook.hook) {
481 etfp->downstream_hook.hook = NULL;
482 } else if (hook == etfp->nomatch_hook.hook) {
483 etfp->nomatch_hook.hook = NULL;
484 } else {
485 if (NG_HOOK_PRIVATE(hook)) /* Paranoia */
486 FREE(NG_HOOK_PRIVATE(hook), M_NETGRAPH_ETF);
487 }
488
489 NG_HOOK_SET_PRIVATE(hook, NULL);
490
491 if ((NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0)
492 && (NG_NODE_IS_VALID(NG_HOOK_NODE(hook)))) /* already shutting down? */
493 ng_rmnode_self(NG_HOOK_NODE(hook));
494 return (0);
495 }
496
Cache object: 605d48e73bb6da515a8d3d7fe71b0f9f
|