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);
85 ng_hub_constructor(node_p node)
89 /* Allocate and initialize private info */
90 priv = malloc(sizeof(*priv), M_NETGRAPH_HUB, M_WAITOK | M_ZERO);
92 NG_NODE_SET_PRIVATE(node, priv);
97 * Receive a control message
100 ng_hub_rcvmsg(node_p node, item_p item, hook_p lasthook)
102 const priv_p priv = NG_NODE_PRIVATE(node);
106 NGI_GET_MSG(item, msg);
107 if (msg->header.typecookie == NGM_HUB_COOKIE &&
108 msg->header.cmd == NGM_HUB_SET_PERSISTENT) {
109 priv->persistent = 1;
119 ng_hub_rcvdata(hook_p hook, item_p item)
121 const node_p node = NG_HOOK_NODE(hook);
124 struct mbuf * const m = NGI_M(item), *m2;
127 if ((nhooks = NG_NODE_NUMHOOKS(node)) == 1) {
131 LIST_FOREACH(hook2, &node->nd_hooks, hk_hooks) {
135 NG_FWD_ITEM_HOOK(error, item, hook2);
137 if ((m2 = m_dup(m, M_NOWAIT)) == NULL) {
141 NG_SEND_DATA_ONLY(error, hook2, m2);
143 continue; /* don't give up */
154 ng_hub_shutdown(node_p node)
156 const priv_p priv = NG_NODE_PRIVATE(node);
158 free(priv, M_NETGRAPH_HUB);
159 NG_NODE_SET_PRIVATE(node, NULL);
165 ng_hub_disconnect(hook_p hook)
167 const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
169 if (NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0 &&
170 NG_NODE_IS_VALID(NG_HOOK_NODE(hook)) && !priv->persistent)
171 ng_rmnode_self(NG_HOOK_NODE(hook));