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