xref: /freebsd/sys/netgraph/ng_hub.c (revision 95ee2897e98f5d444f26ed2334cc7c439f9c16c6)
1b462702fSRuslan Ermilov /*-
2*4d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
3fe267a55SPedro F. Giffuni  *
4b462702fSRuslan Ermilov  * Copyright (c) 2004 Ruslan Ermilov
5b462702fSRuslan Ermilov  * All rights reserved.
6b462702fSRuslan Ermilov  *
7b462702fSRuslan Ermilov  * Redistribution and use in source and binary forms, with or without
8b462702fSRuslan Ermilov  * modification, are permitted provided that the following conditions
9b462702fSRuslan Ermilov  * are met:
10b462702fSRuslan Ermilov  * 1. Redistributions of source code must retain the above copyright
11b462702fSRuslan Ermilov  *    notice, this list of conditions and the following disclaimer.
12b462702fSRuslan Ermilov  * 2. Redistributions in binary form must reproduce the above copyright
13b462702fSRuslan Ermilov  *    notice, this list of conditions and the following disclaimer in the
14b462702fSRuslan Ermilov  *    documentation and/or other materials provided with the distribution.
15b462702fSRuslan Ermilov  *
16b462702fSRuslan Ermilov  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17b462702fSRuslan Ermilov  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18b462702fSRuslan Ermilov  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19b462702fSRuslan Ermilov  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20b462702fSRuslan Ermilov  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21b462702fSRuslan Ermilov  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22b462702fSRuslan Ermilov  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23b462702fSRuslan Ermilov  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24b462702fSRuslan Ermilov  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25b462702fSRuslan Ermilov  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26b462702fSRuslan Ermilov  * SUCH DAMAGE.
27b462702fSRuslan Ermilov  */
28b462702fSRuslan Ermilov 
29b462702fSRuslan Ermilov #include <sys/param.h>
30b462702fSRuslan Ermilov #include <sys/errno.h>
31b462702fSRuslan Ermilov #include <sys/kernel.h>
32b462702fSRuslan Ermilov #include <sys/mbuf.h>
33b462702fSRuslan Ermilov #include <sys/systm.h>
34b462702fSRuslan Ermilov 
35b462702fSRuslan Ermilov #include <netgraph/ng_message.h>
36b462702fSRuslan Ermilov #include <netgraph/ng_hub.h>
37b462702fSRuslan Ermilov #include <netgraph/netgraph.h>
38b462702fSRuslan Ermilov 
39f8aab721SMarko Zec #ifdef NG_SEPARATE_MALLOC
40d745c852SEd Schouten static MALLOC_DEFINE(M_NETGRAPH_HUB, "netgraph_hub", "netgraph hub node");
41f8aab721SMarko Zec #else
42f8aab721SMarko Zec #define M_NETGRAPH_HUB M_NETGRAPH
43f8aab721SMarko Zec #endif
44f8aab721SMarko Zec 
45f8aab721SMarko Zec /* Per-node private data */
46f8aab721SMarko Zec struct ng_hub_private {
47f8aab721SMarko Zec 	int		persistent;	/* can exist w/o hooks */
48f8aab721SMarko Zec };
49f8aab721SMarko Zec typedef struct ng_hub_private *priv_p;
50f8aab721SMarko Zec 
51f8aab721SMarko Zec /* Netgraph node methods */
52b462702fSRuslan Ermilov static ng_constructor_t	ng_hub_constructor;
53f8aab721SMarko Zec static ng_rcvmsg_t	ng_hub_rcvmsg;
54f8aab721SMarko Zec static ng_shutdown_t	ng_hub_shutdown;
55b462702fSRuslan Ermilov static ng_rcvdata_t	ng_hub_rcvdata;
56b462702fSRuslan Ermilov static ng_disconnect_t	ng_hub_disconnect;
57b462702fSRuslan Ermilov 
58f8aab721SMarko Zec /* List of commands and how to convert arguments to/from ASCII */
59f8aab721SMarko Zec static const struct ng_cmdlist ng_hub_cmdlist[] = {
60f8aab721SMarko Zec 	{
61f8aab721SMarko Zec 		NGM_HUB_COOKIE,
62f8aab721SMarko Zec 		NGM_HUB_SET_PERSISTENT,
63f8aab721SMarko Zec 		"setpersistent",
64f8aab721SMarko Zec 		NULL,
65f8aab721SMarko Zec 		NULL
66f8aab721SMarko Zec 	},
67f8aab721SMarko Zec 	{ 0 }
68f8aab721SMarko Zec };
69f8aab721SMarko Zec 
70b462702fSRuslan Ermilov static struct ng_type ng_hub_typestruct = {
71b462702fSRuslan Ermilov 	.version =	NG_ABI_VERSION,
72b462702fSRuslan Ermilov 	.name =		NG_HUB_NODE_TYPE,
73b462702fSRuslan Ermilov 	.constructor =	ng_hub_constructor,
74f8aab721SMarko Zec 	.rcvmsg =	ng_hub_rcvmsg,
75f8aab721SMarko Zec 	.shutdown =	ng_hub_shutdown,
76b462702fSRuslan Ermilov 	.rcvdata =	ng_hub_rcvdata,
77b462702fSRuslan Ermilov 	.disconnect =	ng_hub_disconnect,
78f8aab721SMarko Zec 	.cmdlist =	ng_hub_cmdlist,
79b462702fSRuslan Ermilov };
80b462702fSRuslan Ermilov NETGRAPH_INIT(hub, &ng_hub_typestruct);
81b462702fSRuslan Ermilov 
82b462702fSRuslan Ermilov static int
ng_hub_constructor(node_p node)83b462702fSRuslan Ermilov ng_hub_constructor(node_p node)
84b462702fSRuslan Ermilov {
85f8aab721SMarko Zec 	priv_p priv;
86b462702fSRuslan Ermilov 
87f8aab721SMarko Zec 	/* Allocate and initialize private info */
88674d86bfSGleb Smirnoff 	priv = malloc(sizeof(*priv), M_NETGRAPH_HUB, M_WAITOK | M_ZERO);
89f8aab721SMarko Zec 
90f8aab721SMarko Zec 	NG_NODE_SET_PRIVATE(node, priv);
91b462702fSRuslan Ermilov 	return (0);
92b462702fSRuslan Ermilov }
93b462702fSRuslan Ermilov 
94f8aab721SMarko Zec /*
95f8aab721SMarko Zec  * Receive a control message
96f8aab721SMarko Zec  */
97f8aab721SMarko Zec static int
ng_hub_rcvmsg(node_p node,item_p item,hook_p lasthook)98f8aab721SMarko Zec ng_hub_rcvmsg(node_p node, item_p item, hook_p lasthook)
99f8aab721SMarko Zec {
100f8aab721SMarko Zec 	const priv_p priv = NG_NODE_PRIVATE(node);
101f8aab721SMarko Zec 	int error = 0;
102f8aab721SMarko Zec 	struct ng_mesg *msg;
103f8aab721SMarko Zec 
104f8aab721SMarko Zec 	NGI_GET_MSG(item, msg);
105f8aab721SMarko Zec 	if (msg->header.typecookie == NGM_HUB_COOKIE &&
106f8aab721SMarko Zec 	    msg->header.cmd == NGM_HUB_SET_PERSISTENT) {
107f8aab721SMarko Zec 		priv->persistent = 1;
108f8aab721SMarko Zec 	} else {
109f8aab721SMarko Zec 		error = EINVAL;
110f8aab721SMarko Zec 	}
111f8aab721SMarko Zec 
112f8aab721SMarko Zec 	NG_FREE_MSG(msg);
113f8aab721SMarko Zec 	return (error);
114f8aab721SMarko Zec }
115f8aab721SMarko Zec 
116b462702fSRuslan Ermilov static int
ng_hub_rcvdata(hook_p hook,item_p item)117b462702fSRuslan Ermilov ng_hub_rcvdata(hook_p hook, item_p item)
118b462702fSRuslan Ermilov {
119b462702fSRuslan Ermilov 	const node_p node = NG_HOOK_NODE(hook);
120b462702fSRuslan Ermilov 	int error = 0;
121b462702fSRuslan Ermilov 	hook_p hook2;
122b462702fSRuslan Ermilov 	struct mbuf * const m = NGI_M(item), *m2;
123b462702fSRuslan Ermilov 	int nhooks;
124b462702fSRuslan Ermilov 
125b462702fSRuslan Ermilov 	if ((nhooks = NG_NODE_NUMHOOKS(node)) == 1) {
126b462702fSRuslan Ermilov 		NG_FREE_ITEM(item);
127b462702fSRuslan Ermilov 		return (0);
128b462702fSRuslan Ermilov 	}
129b462702fSRuslan Ermilov 	LIST_FOREACH(hook2, &node->nd_hooks, hk_hooks) {
130b462702fSRuslan Ermilov 		if (hook2 == hook)
131b462702fSRuslan Ermilov 			continue;
132b462702fSRuslan Ermilov 		if (--nhooks == 1)
133b462702fSRuslan Ermilov 			NG_FWD_ITEM_HOOK(error, item, hook2);
134b462702fSRuslan Ermilov 		else {
135eb1b1807SGleb Smirnoff 			if ((m2 = m_dup(m, M_NOWAIT)) == NULL) {
136b462702fSRuslan Ermilov 				NG_FREE_ITEM(item);
137b462702fSRuslan Ermilov 				return (ENOBUFS);
138b462702fSRuslan Ermilov 			}
1393ca24c28SJulian Elischer 			NG_SEND_DATA_ONLY(error, hook2, m2);
140b462702fSRuslan Ermilov 			if (error)
141dfa515f2SRuslan Ermilov 				continue;	/* don't give up */
142b462702fSRuslan Ermilov 		}
143b462702fSRuslan Ermilov 	}
144b462702fSRuslan Ermilov 
145b462702fSRuslan Ermilov 	return (error);
146b462702fSRuslan Ermilov }
147b462702fSRuslan Ermilov 
148f8aab721SMarko Zec /*
149f8aab721SMarko Zec  * Shutdown node
150f8aab721SMarko Zec  */
151f8aab721SMarko Zec static int
ng_hub_shutdown(node_p node)152f8aab721SMarko Zec ng_hub_shutdown(node_p node)
153f8aab721SMarko Zec {
154f8aab721SMarko Zec 	const priv_p priv = NG_NODE_PRIVATE(node);
155f8aab721SMarko Zec 
156f8aab721SMarko Zec 	free(priv, M_NETGRAPH_HUB);
157fae147aaSMarko Zec 	NG_NODE_SET_PRIVATE(node, NULL);
158fae147aaSMarko Zec 	NG_NODE_UNREF(node);
159f8aab721SMarko Zec 	return (0);
160f8aab721SMarko Zec }
161f8aab721SMarko Zec 
162b462702fSRuslan Ermilov static int
ng_hub_disconnect(hook_p hook)163b462702fSRuslan Ermilov ng_hub_disconnect(hook_p hook)
164b462702fSRuslan Ermilov {
165f8aab721SMarko Zec 	const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
166b462702fSRuslan Ermilov 
167b462702fSRuslan Ermilov 	if (NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0 &&
168f8aab721SMarko Zec 	    NG_NODE_IS_VALID(NG_HOOK_NODE(hook)) && !priv->persistent)
169b462702fSRuslan Ermilov 		ng_rmnode_self(NG_HOOK_NODE(hook));
170b462702fSRuslan Ermilov 	return (0);
171b462702fSRuslan Ermilov }
172