xref: /dragonfly/sys/netgraph7/ng_split.c (revision a62226e46c982d037de05e1bb0894805c0b7a32f)
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  */
30 
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/errno.h>
34 #include <sys/kernel.h>
35 #include <sys/malloc.h>
36 #include <sys/mbuf.h>
37 #include <sys/sockio.h>
38 #include <sys/socket.h>
39 #include <sys/syslog.h>
40 
41 #include "ng_message.h"
42 #include "netgraph.h"
43 #include "ng_split.h"
44 
45 /* Netgraph methods */
46 static ng_constructor_t ng_split_constructor;
47 static ng_shutdown_t ng_split_shutdown;
48 static ng_newhook_t ng_split_newhook;
49 static ng_rcvdata_t ng_split_rcvdata;
50 static ng_disconnect_t ng_split_disconnect;
51 
52 /* Node type descriptor */
53 static struct ng_type typestruct = {
54           .version =          NG_ABI_VERSION,
55           .name =             NG_SPLIT_NODE_TYPE,
56           .constructor =      ng_split_constructor,
57           .shutdown =         ng_split_shutdown,
58           .newhook =          ng_split_newhook,
59           .rcvdata =          ng_split_rcvdata,
60           .disconnect =       ng_split_disconnect,
61 };
62 NETGRAPH_INIT(ng_split, &typestruct);
63 
64 /* Node private data */
65 struct ng_split_private {
66           hook_p out;
67           hook_p in;
68           hook_p mixed;
69           node_p    node;                         /* Our netgraph node */
70 };
71 typedef struct ng_split_private *priv_p;
72 
73 /************************************************************************
74                               NETGRAPH NODE STUFF
75  ************************************************************************/
76 
77 /*
78  * Constructor for a node
79  */
80 static int
ng_split_constructor(node_p node)81 ng_split_constructor(node_p node)
82 {
83           priv_p              priv;
84 
85           /* Allocate node */
86           priv = kmalloc(sizeof(*priv), M_NETGRAPH,
87                            M_ZERO | M_WAITOK | M_NULLOK);
88           if (priv == NULL)
89                     return (ENOMEM);
90 
91           /* Link together node and private info */
92           NG_NODE_SET_PRIVATE(node, priv);
93           priv->node = node;
94 
95           /* Done */
96           return (0);
97 }
98 
99 /*
100  * Give our ok for a hook to be added
101  */
102 static int
ng_split_newhook(node_p node,hook_p hook,const char * name)103 ng_split_newhook(node_p node, hook_p hook, const char *name)
104 {
105           priv_p              priv = NG_NODE_PRIVATE(node);
106           hook_p              *localhook;
107 
108           if (strcmp(name, NG_SPLIT_HOOK_MIXED) == 0) {
109                     localhook = &priv->mixed;
110           } else if (strcmp(name, NG_SPLIT_HOOK_IN) == 0) {
111                     localhook = &priv->in;
112           } else if (strcmp(name, NG_SPLIT_HOOK_OUT) == 0) {
113                     localhook = &priv->out;
114           } else
115                     return (EINVAL);
116 
117           if (*localhook != NULL)
118                     return (EISCONN);
119           *localhook = hook;
120           NG_HOOK_SET_PRIVATE(hook, localhook);
121 
122           return (0);
123 }
124 
125 /*
126  * Recive data from a hook.
127  */
128 static int
ng_split_rcvdata(hook_p hook,item_p item)129 ng_split_rcvdata(hook_p hook, item_p item)
130 {
131           const priv_p        priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
132           int                 error = 0;
133 
134           if (hook == priv->out) {
135                     kprintf("ng_split: got packet from out hook!\n");
136                     NG_FREE_ITEM(item);
137                     error = EINVAL;
138           } else if ((hook == priv->in) && (priv->mixed != NULL)) {
139                     NG_FWD_ITEM_HOOK(error, item, priv->mixed);
140           } else if ((hook == priv->mixed) && (priv->out != NULL)) {
141                     NG_FWD_ITEM_HOOK(error, item, priv->out);
142           }
143 
144           if (item)
145                     NG_FREE_ITEM(item);
146 
147           return (error);
148 }
149 
150 static int
ng_split_shutdown(node_p node)151 ng_split_shutdown(node_p node)
152 {
153           const priv_p        priv = NG_NODE_PRIVATE(node);
154 
155           NG_NODE_SET_PRIVATE(node, NULL);
156           NG_NODE_UNREF(node);
157           kfree(priv, M_NETGRAPH);
158 
159           return (0);
160 }
161 
162 /*
163  * Hook disconnection
164  */
165 static int
ng_split_disconnect(hook_p hook)166 ng_split_disconnect(hook_p hook)
167 {
168           hook_p              *localhook = NG_HOOK_PRIVATE(hook);
169 
170           KASSERT(localhook != NULL, ("%s: null info", __func__));
171           *localhook = NULL;
172           if ((NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0)
173               && (NG_NODE_IS_VALID(NG_HOOK_NODE(hook)))) {
174                     ng_rmnode_self(NG_HOOK_NODE(hook));
175           }
176 
177           return (0);
178 }
179