kernel/netgraph7: Replace usage of MALLOC/FREE with kmalloc/kfree here too.
[dragonfly.git] / sys / netgraph7 / ng_split.c
1 /*-
2  *
3  * Copyright (c) 1999-2000, Vitaly V Belekhov
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice unmodified, this list of conditions, and the following
11  *    disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  *
28  * $FreeBSD: src/sys/netgraph/ng_split.c,v 1.7 2005/08/29 13:47:08 glebius Exp $
29  * $DragonFly: src/sys/netgraph7/ng_split.c,v 1.2 2008/06/26 23:05:35 dillon Exp $
30  *
31  */
32
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/errno.h>
36 #include <sys/kernel.h>
37 #include <sys/malloc.h>
38 #include <sys/mbuf.h>
39 #include <sys/errno.h>
40 #include <sys/sockio.h>
41 #include <sys/socket.h>
42 #include <sys/syslog.h>
43
44 #include "ng_message.h"
45 #include "netgraph.h"
46 #include "ng_split.h"
47
48 /* Netgraph methods */
49 static ng_constructor_t ng_split_constructor;
50 static ng_shutdown_t ng_split_shutdown;
51 static ng_newhook_t ng_split_newhook;
52 static ng_rcvdata_t ng_split_rcvdata;
53 static ng_disconnect_t ng_split_disconnect;
54
55 /* Node type descriptor */
56 static struct ng_type typestruct = {
57         .version =      NG_ABI_VERSION,
58         .name =         NG_SPLIT_NODE_TYPE,
59         .constructor =  ng_split_constructor,
60         .shutdown =     ng_split_shutdown,
61         .newhook =      ng_split_newhook,
62         .rcvdata =      ng_split_rcvdata,
63         .disconnect =   ng_split_disconnect,
64 };
65 NETGRAPH_INIT(ng_split, &typestruct);
66
67 /* Node private data */
68 struct ng_split_private {
69         hook_p out;
70         hook_p in;
71         hook_p mixed;
72         node_p  node;                   /* Our netgraph node */
73 };
74 typedef struct ng_split_private *priv_p;
75
76 /************************************************************************
77                         NETGRAPH NODE STUFF
78  ************************************************************************/
79
80 /*
81  * Constructor for a node
82  */
83 static int
84 ng_split_constructor(node_p node)
85 {
86         priv_p          priv;
87
88         /* Allocate node */
89         priv = kmalloc(sizeof(*priv), M_NETGRAPH,
90                        M_ZERO | M_WAITOK | M_NULLOK);
91         if (priv == NULL)
92                 return (ENOMEM);
93
94         /* Link together node and private info */
95         NG_NODE_SET_PRIVATE(node, priv);
96         priv->node = node;
97
98         /* Done */
99         return (0);
100 }
101
102 /*
103  * Give our ok for a hook to be added
104  */
105 static int
106 ng_split_newhook(node_p node, hook_p hook, const char *name)
107 {
108         priv_p          priv = NG_NODE_PRIVATE(node);
109         hook_p          *localhook;
110
111         if (strcmp(name, NG_SPLIT_HOOK_MIXED) == 0) {
112                 localhook = &priv->mixed;
113         } else if (strcmp(name, NG_SPLIT_HOOK_IN) == 0) {
114                 localhook = &priv->in;
115         } else if (strcmp(name, NG_SPLIT_HOOK_OUT) == 0) {
116                 localhook = &priv->out;
117         } else
118                 return (EINVAL);
119
120         if (*localhook != NULL)
121                 return (EISCONN);
122         *localhook = hook;
123         NG_HOOK_SET_PRIVATE(hook, localhook);
124
125         return (0);
126 }
127
128 /*
129  * Recive data from a hook.
130  */
131 static int
132 ng_split_rcvdata(hook_p hook, item_p item)
133 {
134         const priv_p    priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
135         int             error = 0;
136
137         if (hook == priv->out) {
138                 printf("ng_split: got packet from out hook!\n");
139                 NG_FREE_ITEM(item);
140                 error = EINVAL;
141         } else if ((hook == priv->in) && (priv->mixed != NULL)) {
142                 NG_FWD_ITEM_HOOK(error, item, priv->mixed);
143         } else if ((hook == priv->mixed) && (priv->out != NULL)) {
144                 NG_FWD_ITEM_HOOK(error, item, priv->out);
145         }
146
147         if (item)
148                 NG_FREE_ITEM(item);
149
150         return (error);
151 }
152
153 static int
154 ng_split_shutdown(node_p node)
155 {
156         const priv_p    priv = NG_NODE_PRIVATE(node);
157
158         NG_NODE_SET_PRIVATE(node, NULL);
159         NG_NODE_UNREF(node);
160         kfree(priv, M_NETGRAPH);
161
162         return (0);
163 }
164
165 /*
166  * Hook disconnection
167  */
168 static int
169 ng_split_disconnect(hook_p hook)
170 {
171         hook_p          *localhook = NG_HOOK_PRIVATE(hook);
172         
173         KASSERT(localhook != NULL, ("%s: null info", __func__));
174         *localhook = NULL;
175         if ((NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0)
176             && (NG_NODE_IS_VALID(NG_HOOK_NODE(hook)))) {
177                 ng_rmnode_self(NG_HOOK_NODE(hook));
178         }
179
180         return (0);
181 }