1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 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 42 static 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 */ 48 struct ng_hub_private { 49 int persistent; /* can exist w/o hooks */ 50 }; 51 typedef struct ng_hub_private *priv_p; 52 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; 59 60 /* List of commands and how to convert arguments to/from ASCII */ 61 static 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 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, 81 }; 82 NETGRAPH_INIT(hub, &ng_hub_typestruct); 83 84 static int 85 ng_hub_constructor(node_p node) 86 { 87 priv_p priv; 88 89 /* Allocate and initialize private info */ 90 priv = malloc(sizeof(*priv), M_NETGRAPH_HUB, M_WAITOK | M_ZERO); 91 92 NG_NODE_SET_PRIVATE(node, priv); 93 return (0); 94 } 95 96 /* 97 * Receive a control message 98 */ 99 static int 100 ng_hub_rcvmsg(node_p node, item_p item, hook_p lasthook) 101 { 102 const priv_p priv = NG_NODE_PRIVATE(node); 103 int error = 0; 104 struct ng_mesg *msg; 105 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; 110 } else { 111 error = EINVAL; 112 } 113 114 NG_FREE_MSG(msg); 115 return (error); 116 } 117 118 static int 119 ng_hub_rcvdata(hook_p hook, item_p item) 120 { 121 const node_p node = NG_HOOK_NODE(hook); 122 int error = 0; 123 hook_p hook2; 124 struct mbuf * const m = NGI_M(item), *m2; 125 int nhooks; 126 127 if ((nhooks = NG_NODE_NUMHOOKS(node)) == 1) { 128 NG_FREE_ITEM(item); 129 return (0); 130 } 131 LIST_FOREACH(hook2, &node->nd_hooks, hk_hooks) { 132 if (hook2 == hook) 133 continue; 134 if (--nhooks == 1) 135 NG_FWD_ITEM_HOOK(error, item, hook2); 136 else { 137 if ((m2 = m_dup(m, M_NOWAIT)) == NULL) { 138 NG_FREE_ITEM(item); 139 return (ENOBUFS); 140 } 141 NG_SEND_DATA_ONLY(error, hook2, m2); 142 if (error) 143 continue; /* don't give up */ 144 } 145 } 146 147 return (error); 148 } 149 150 /* 151 * Shutdown node 152 */ 153 static int 154 ng_hub_shutdown(node_p node) 155 { 156 const priv_p priv = NG_NODE_PRIVATE(node); 157 158 free(priv, M_NETGRAPH_HUB); 159 NG_NODE_SET_PRIVATE(node, NULL); 160 NG_NODE_UNREF(node); 161 return (0); 162 } 163 164 static int 165 ng_hub_disconnect(hook_p hook) 166 { 167 const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook)); 168 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)); 172 return (0); 173 } 174