2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2004 Ruslan Ermilov
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
31 #include <sys/param.h>
32 #include <sys/errno.h>
33 #include <sys/kernel.h>
35 #include <sys/systm.h>
37 #include <netgraph/ng_message.h>
38 #include <netgraph/ng_hub.h>
39 #include <netgraph/netgraph.h>
41 #ifdef NG_SEPARATE_MALLOC
42 static MALLOC_DEFINE(M_NETGRAPH_HUB, "netgraph_hub", "netgraph hub node");
44 #define M_NETGRAPH_HUB M_NETGRAPH
47 /* Per-node private data */
48 struct ng_hub_private {
49 int persistent; /* can exist w/o hooks */
51 typedef struct ng_hub_private *priv_p;
53 /* Netgraph node methods */
54 static ng_constructor_t ng_hub_constructor;
55 static ng_rcvmsg_t ng_hub_rcvmsg;
56 static ng_shutdown_t ng_hub_shutdown;
57 static ng_rcvdata_t ng_hub_rcvdata;
58 static ng_disconnect_t ng_hub_disconnect;
60 /* List of commands and how to convert arguments to/from ASCII */
61 static const struct ng_cmdlist ng_hub_cmdlist[] = {
64 NGM_HUB_SET_PERSISTENT,
72 static 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,
82 NETGRAPH_INIT(hub, &ng_hub_typestruct);
86 ng_hub_constructor(node_p node)
90 /* Allocate and initialize private info */
91 priv = malloc(sizeof(*priv), M_NETGRAPH_HUB, M_WAITOK | M_ZERO);
93 NG_NODE_SET_PRIVATE(node, priv);
98 * Receive a control message
101 ng_hub_rcvmsg(node_p node, item_p item, hook_p lasthook)
103 const priv_p priv = NG_NODE_PRIVATE(node);
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;
120 ng_hub_rcvdata(hook_p hook, item_p item)
122 const node_p node = NG_HOOK_NODE(hook);
125 struct mbuf * const m = NGI_M(item), *m2;
128 if ((nhooks = NG_NODE_NUMHOOKS(node)) == 1) {
132 LIST_FOREACH(hook2, &node->nd_hooks, hk_hooks) {
136 NG_FWD_ITEM_HOOK(error, item, hook2);
138 if ((m2 = m_dup(m, M_NOWAIT)) == NULL) {
142 NG_SEND_DATA_ONLY(error, hook2, m2);
144 continue; /* don't give up */
155 ng_hub_shutdown(node_p node)
157 const priv_p priv = NG_NODE_PRIVATE(node);
159 free(priv, M_NETGRAPH_HUB);
160 NG_NODE_SET_PRIVATE(node, NULL);
166 ng_hub_disconnect(hook_p hook)
168 const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
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));