kernel/netgraph7: Replace usage of MALLOC/FREE with kmalloc/kfree here too.
[dragonfly.git] / sys / netgraph7 / ng_etf.c
CommitLineData
b06ebda0
MD
1/*-
2 * ng_etf.c Ethertype filter
3 */
4
5/*-
6 * Copyright (c) 2001, FreeBSD Incorporated
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice unmodified, this list of conditions, and the following
14 * disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 *
31 * Author: Julian Elischer <julian@freebsd.org>
32 *
33 * $FreeBSD: src/sys/netgraph/ng_etf.c,v 1.9 2005/03/14 20:49:48 glebius Exp $
5a975a3d 34 * $DragonFly: src/sys/netgraph7/ng_etf.c,v 1.2 2008/06/26 23:05:35 dillon Exp $
b06ebda0
MD
35 */
36
37#include <sys/param.h>
38#include <sys/systm.h>
39#include <sys/kernel.h>
40#include <sys/mbuf.h>
41#include <sys/malloc.h>
42#include <sys/ctype.h>
43#include <sys/errno.h>
44#include <sys/queue.h>
45#include <sys/syslog.h>
46
47#include <net/ethernet.h>
48
5a975a3d
MD
49#include "ng_message.h"
50#include "ng_parse.h"
51#include "ng_etf.h"
52#include "netgraph.h"
b06ebda0
MD
53
54/* If you do complicated mallocs you may want to do this */
55/* and use it for your mallocs */
56#ifdef NG_SEPARATE_MALLOC
57static MALLOC_DEFINE(M_NETGRAPH_ETF, "netgraph_etf", "netgraph etf node ");
58#else
59#define M_NETGRAPH_ETF M_NETGRAPH
60#endif
61
62/*
63 * This section contains the netgraph method declarations for the
64 * etf node. These methods define the netgraph 'type'.
65 */
66
67static ng_constructor_t ng_etf_constructor;
68static ng_rcvmsg_t ng_etf_rcvmsg;
69static ng_shutdown_t ng_etf_shutdown;
70static ng_newhook_t ng_etf_newhook;
71static ng_rcvdata_t ng_etf_rcvdata; /* note these are both ng_rcvdata_t */
72static ng_disconnect_t ng_etf_disconnect;
73
74/* Parse type for struct ng_etfstat */
75static const struct ng_parse_struct_field ng_etf_stat_type_fields[]
76 = NG_ETF_STATS_TYPE_INFO;
77static const struct ng_parse_type ng_etf_stat_type = {
78 &ng_parse_struct_type,
79 &ng_etf_stat_type_fields
80};
81/* Parse type for struct ng_setfilter */
82static const struct ng_parse_struct_field ng_etf_filter_type_fields[]
83 = NG_ETF_FILTER_TYPE_INFO;
84static const struct ng_parse_type ng_etf_filter_type = {
85 &ng_parse_struct_type,
86 &ng_etf_filter_type_fields
87};
88
89/* List of commands and how to convert arguments to/from ASCII */
90static const struct ng_cmdlist ng_etf_cmdlist[] = {
91 {
92 NGM_ETF_COOKIE,
93 NGM_ETF_GET_STATUS,
94 "getstatus",
95 NULL,
96 &ng_etf_stat_type,
97 },
98 {
99 NGM_ETF_COOKIE,
100 NGM_ETF_SET_FLAG,
101 "setflag",
102 &ng_parse_int32_type,
103 NULL
104 },
105 {
106 NGM_ETF_COOKIE,
107 NGM_ETF_SET_FILTER,
108 "setfilter",
109 &ng_etf_filter_type,
110 NULL
111 },
112 { 0 }
113};
114
115/* Netgraph node type descriptor */
116static struct ng_type typestruct = {
117 .version = NG_ABI_VERSION,
118 .name = NG_ETF_NODE_TYPE,
119 .constructor = ng_etf_constructor,
120 .rcvmsg = ng_etf_rcvmsg,
121 .shutdown = ng_etf_shutdown,
122 .newhook = ng_etf_newhook,
123 .rcvdata = ng_etf_rcvdata,
124 .disconnect = ng_etf_disconnect,
125 .cmdlist = ng_etf_cmdlist,
126};
127NETGRAPH_INIT(etf, &typestruct);
128
129/* Information we store for each hook on each node */
130struct ETF_hookinfo {
131 hook_p hook;
132};
133
134struct filter {
135 LIST_ENTRY(filter) next;
136 u_int16_t ethertype; /* network order ethertype */
137 hook_p match_hook; /* Hook to use on a match */
138};
139
140#define HASHSIZE 16 /* Dont change this without changing HASH() */
141#define HASH(et) ((((et)>>12)+((et)>>8)+((et)>>4)+(et)) & 0x0f)
142LIST_HEAD(filterhead, filter);
143
144/* Information we store for each node */
145struct ETF {
146 struct ETF_hookinfo downstream_hook;
147 struct ETF_hookinfo nomatch_hook;
148 node_p node; /* back pointer to node */
149 u_int packets_in; /* packets in from downstream */
150 u_int packets_out; /* packets out towards downstream */
151 u_int32_t flags;
152 struct filterhead hashtable[HASHSIZE];
153};
154typedef struct ETF *etf_p;
155
156static struct filter *
157ng_etf_findentry(etf_p etfp, u_int16_t ethertype)
158{
159 struct filterhead *chain = etfp->hashtable + HASH(ethertype);
160 struct filter *fil;
161
162
163 LIST_FOREACH(fil, chain, next) {
164 if (fil->ethertype == ethertype) {
165 return (fil);
166 }
167 }
168 return (NULL);
169}
170
171
172/*
173 * Allocate the private data structure. The generic node has already
174 * been created. Link them together. We arrive with a reference to the node
175 * i.e. the reference count is incremented for us already.
176 */
177static int
178ng_etf_constructor(node_p node)
179{
180 etf_p privdata;
181 int i;
182
183 /* Initialize private descriptor */
fc025606
SW
184 privdata = kmalloc(sizeof(*privdata), M_NETGRAPH_ETF,
185 M_WAITOK | M_NULLOK | M_ZERO);
b06ebda0
MD
186 if (privdata == NULL)
187 return (ENOMEM);
188 for (i = 0; i < HASHSIZE; i++) {
189 LIST_INIT((privdata->hashtable + i));
190 }
191
192 /* Link structs together; this counts as our one reference to node */
193 NG_NODE_SET_PRIVATE(node, privdata);
194 privdata->node = node;
195 return (0);
196}
197
198/*
199 * Give our ok for a hook to be added...
200 * All names are ok. Two names are special.
201 */
202static int
203ng_etf_newhook(node_p node, hook_p hook, const char *name)
204{
205 const etf_p etfp = NG_NODE_PRIVATE(node);
206 struct ETF_hookinfo *hpriv;
207
208 if (strcmp(name, NG_ETF_HOOK_DOWNSTREAM) == 0) {
209 etfp->downstream_hook.hook = hook;
210 NG_HOOK_SET_PRIVATE(hook, &etfp->downstream_hook);
211 etfp->packets_in = 0;
212 etfp->packets_out = 0;
213 } else if (strcmp(name, NG_ETF_HOOK_NOMATCH) == 0) {
214 etfp->nomatch_hook.hook = hook;
215 NG_HOOK_SET_PRIVATE(hook, &etfp->nomatch_hook);
216 } else {
217 /*
218 * Any other hook name is valid and can
219 * later be associated with a filter rule.
220 */
fc025606
SW
221 hpriv = kmalloc(sizeof(*hpriv), M_NETGRAPH_ETF,
222 M_WAITOK | M_NULLOK | M_ZERO);
b06ebda0
MD
223 if (hpriv == NULL) {
224 return (ENOMEM);
225 }
226
227 NG_HOOK_SET_PRIVATE(hook, hpriv);
228 hpriv->hook = hook;
229 }
230 return(0);
231}
232
233/*
234 * Get a netgraph control message.
235 * We actually recieve a queue item that has a pointer to the message.
236 * If we free the item, the message will be freed too, unless we remove
237 * it from the item using NGI_GET_MSG();
238 * The return address is also stored in the item, as an ng_ID_t,
239 * accessible as NGI_RETADDR(item);
240 * Check it is one we understand. If needed, send a response.
241 * We could save the address for an async action later, but don't here.
242 * Always free the message.
243 * The response should be in a malloc'd region that the caller can 'free'.
244 * The NG_MKRESPONSE macro does all this for us.
245 * A response is not required.
246 * Theoretically you could respond defferently to old message types if
247 * the cookie in the header didn't match what we consider to be current
248 * (so that old userland programs could continue to work).
249 */
250static int
251ng_etf_rcvmsg(node_p node, item_p item, hook_p lasthook)
252{
253 const etf_p etfp = NG_NODE_PRIVATE(node);
254 struct ng_mesg *resp = NULL;
255 int error = 0;
256 struct ng_mesg *msg;
257
258 NGI_GET_MSG(item, msg);
259 /* Deal with message according to cookie and command */
260 switch (msg->header.typecookie) {
261 case NGM_ETF_COOKIE:
262 switch (msg->header.cmd) {
263 case NGM_ETF_GET_STATUS:
264 {
265 struct ng_etfstat *stats;
266
5a975a3d 267 NG_MKRESPONSE(resp, msg, sizeof(*stats), M_WAITOK | M_NULLOK);
b06ebda0
MD
268 if (!resp) {
269 error = ENOMEM;
270 break;
271 }
272 stats = (struct ng_etfstat *) resp->data;
273 stats->packets_in = etfp->packets_in;
274 stats->packets_out = etfp->packets_out;
275 break;
276 }
277 case NGM_ETF_SET_FLAG:
278 if (msg->header.arglen != sizeof(u_int32_t)) {
279 error = EINVAL;
280 break;
281 }
282 etfp->flags = *((u_int32_t *) msg->data);
283 break;
284 case NGM_ETF_SET_FILTER:
285 {
286 struct ng_etffilter *f;
287 struct filter *fil;
288 hook_p hook;
289
290 /* Check message long enough for this command */
291 if (msg->header.arglen != sizeof(*f)) {
292 error = EINVAL;
293 break;
294 }
295
296 /* Make sure hook referenced exists */
297 f = (struct ng_etffilter *)msg->data;
298 hook = ng_findhook(node, f->matchhook);
299 if (hook == NULL) {
300 error = ENOENT;
301 break;
302 }
303
304 /* and is not the downstream hook */
305 if (hook == etfp->downstream_hook.hook) {
306 error = EINVAL;
307 break;
308 }
309
310 /* Check we don't already trap this ethertype */
311 if (ng_etf_findentry(etfp,
312 htons(f->ethertype))) {
313 error = EEXIST;
314 break;
315 }
316
317 /*
318 * Ok, make the filter and put it in the
319 * hashtable ready for matching.
320 */
fc025606
SW
321 fil = kmalloc(sizeof(*fil), M_NETGRAPH_ETF,
322 M_WAITOK | M_NULLOK | M_ZERO);
b06ebda0
MD
323 if (fil == NULL) {
324 error = ENOMEM;
325 break;
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 will also free the data unless we have previously
356 * disassociated it using the NGI_GET_etf() macro.
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 , so if we use up this data
360 * or abort we must free it.
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 */
367static int
368ng_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 */
437static int
438ng_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);
fc025606 444 kfree(privdata, M_NETGRAPH_ETF);
b06ebda0
MD
445 return (0);
446}
447
448/*
449 * Hook disconnection
450 *
451 * For this type, removal of the last link destroys the node
452 */
453static int
454ng_etf_disconnect(hook_p hook)
455{
456 const etf_p etfp = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
457 int i;
458 struct filter *fil1, *fil2;
459
460 /* purge any rules that refer to this filter */
461 for (i = 0; i < HASHSIZE; i++) {
462 fil1 = LIST_FIRST(&etfp->hashtable[i]);
463 while (fil1 != NULL) {
464 fil2 = LIST_NEXT(fil1, next);
465 if (fil1->match_hook == hook) {
466 LIST_REMOVE(fil1, next);
fc025606 467 kfree(fil1, M_NETGRAPH_ETF);
b06ebda0
MD
468 }
469 fil1 = fil2;
470 }
471 }
472
473 /* If it's not one of the special hooks, then free it */
474 if (hook == etfp->downstream_hook.hook) {
475 etfp->downstream_hook.hook = NULL;
476 } else if (hook == etfp->nomatch_hook.hook) {
477 etfp->nomatch_hook.hook = NULL;
478 } else {
479 if (NG_HOOK_PRIVATE(hook)) /* Paranoia */
fc025606 480 kfree(NG_HOOK_PRIVATE(hook), M_NETGRAPH_ETF);
b06ebda0
MD
481 }
482
483 NG_HOOK_SET_PRIVATE(hook, NULL);
484
485 if ((NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0)
486 && (NG_NODE_IS_VALID(NG_HOOK_NODE(hook)))) /* already shutting down? */
487 ng_rmnode_self(NG_HOOK_NODE(hook));
488 return (0);
489}
490