1/*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3 *
4 * Copyright (c) 2004 Ruslan Ermilov
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, this list of conditions and the following 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$
29 */
30
31#include <sys/param.h>
32#include <sys/errno.h>
33#include <sys/kernel.h>
34#include <sys/mbuf.h>
35#include <sys/systm.h>
36
37#include <netgraph/ng_message.h>
38#include <netgraph/ng_hub.h>
39#include <netgraph/netgraph.h>
40
41#ifdef NG_SEPARATE_MALLOC
42static MALLOC_DEFINE(M_NETGRAPH_HUB, "netgraph_hub", "netgraph hub node");
43#else
44#define M_NETGRAPH_HUB M_NETGRAPH
45#endif
46
47/* Per-node private data */
48struct ng_hub_private {
49	int		persistent;	/* can exist w/o hooks */
50};
51typedef struct ng_hub_private *priv_p;
52
53/* Netgraph node methods */
54static ng_constructor_t	ng_hub_constructor;
55static ng_rcvmsg_t	ng_hub_rcvmsg;
56static ng_shutdown_t	ng_hub_shutdown;
57static ng_rcvdata_t	ng_hub_rcvdata;
58static ng_disconnect_t	ng_hub_disconnect;
59
60/* List of commands and how to convert arguments to/from ASCII */
61static const struct ng_cmdlist ng_hub_cmdlist[] = {
62	{
63		NGM_HUB_COOKIE,
64		NGM_HUB_SET_PERSISTENT,
65		"setpersistent",
66		NULL,
67		NULL
68	},
69	{ 0 }
70};
71
72static struct ng_type ng_hub_typestruct = {
73	.version =	NG_ABI_VERSION,
74	.name =		NG_HUB_NODE_TYPE,
75	.constructor =	ng_hub_constructor,
76	.rcvmsg =	ng_hub_rcvmsg,
77	.shutdown =	ng_hub_shutdown,
78	.rcvdata =	ng_hub_rcvdata,
79	.disconnect =	ng_hub_disconnect,
80	.cmdlist =	ng_hub_cmdlist,
81};
82NETGRAPH_INIT(hub, &ng_hub_typestruct);
83
84
85static int
86ng_hub_constructor(node_p node)
87{
88	priv_p priv;
89
90	/* Allocate and initialize private info */
91	priv = malloc(sizeof(*priv), M_NETGRAPH_HUB, M_WAITOK | M_ZERO);
92
93	NG_NODE_SET_PRIVATE(node, priv);
94	return (0);
95}
96
97/*
98 * Receive a control message
99 */
100static int
101ng_hub_rcvmsg(node_p node, item_p item, hook_p lasthook)
102{
103	const priv_p priv = NG_NODE_PRIVATE(node);
104	int error = 0;
105	struct ng_mesg *msg;
106
107	NGI_GET_MSG(item, msg);
108	if (msg->header.typecookie == NGM_HUB_COOKIE &&
109	    msg->header.cmd == NGM_HUB_SET_PERSISTENT) {
110		priv->persistent = 1;
111	} else {
112		error = EINVAL;
113	}
114
115	NG_FREE_MSG(msg);
116	return (error);
117}
118
119static int
120ng_hub_rcvdata(hook_p hook, item_p item)
121{
122	const node_p node = NG_HOOK_NODE(hook);
123	int error = 0;
124	hook_p hook2;
125	struct mbuf * const m = NGI_M(item), *m2;
126	int nhooks;
127
128	if ((nhooks = NG_NODE_NUMHOOKS(node)) == 1) {
129		NG_FREE_ITEM(item);
130		return (0);
131	}
132	LIST_FOREACH(hook2, &node->nd_hooks, hk_hooks) {
133		if (hook2 == hook)
134			continue;
135		if (--nhooks == 1)
136			NG_FWD_ITEM_HOOK(error, item, hook2);
137		else {
138			if ((m2 = m_dup(m, M_NOWAIT)) == NULL) {
139				NG_FREE_ITEM(item);
140				return (ENOBUFS);
141			}
142			NG_SEND_DATA_ONLY(error, hook2, m2);
143			if (error)
144				continue;	/* don't give up */
145		}
146	}
147
148	return (error);
149}
150
151/*
152 * Shutdown node
153 */
154static int
155ng_hub_shutdown(node_p node)
156{
157	const priv_p priv = NG_NODE_PRIVATE(node);
158
159	free(priv, M_NETGRAPH_HUB);
160	NG_NODE_SET_PRIVATE(node, NULL);
161	NG_NODE_UNREF(node);
162	return (0);
163}
164
165static int
166ng_hub_disconnect(hook_p hook)
167{
168	const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
169
170	if (NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0 &&
171	    NG_NODE_IS_VALID(NG_HOOK_NODE(hook)) && !priv->persistent)
172		ng_rmnode_self(NG_HOOK_NODE(hook));
173	return (0);
174}
175