FreeBSD kernel netgraph code
ng_hub.c
Go to the documentation of this file.
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 */
49 int persistent; /* can exist w/o hooks */
50};
51typedef struct ng_hub_private *priv_p;
52
53/* Netgraph node methods */
59
60/* List of commands and how to convert arguments to/from ASCII */
61static const struct ng_cmdlist ng_hub_cmdlist[] = {
62 {
65 "setpersistent",
66 NULL,
67 NULL
68 },
69 { 0 }
70};
71
72static struct ng_type ng_hub_typestruct = {
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};
83
84static int
86{
88
89 /* Allocate and initialize private info */
90 priv = malloc(sizeof(*priv), M_NETGRAPH_HUB, M_WAITOK | M_ZERO);
91
93 return (0);
94}
95
96/*
97 * Receive a control message
98 */
99static int
100ng_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 &&
109 priv->persistent = 1;
110 } else {
111 error = EINVAL;
112 }
113
114 NG_FREE_MSG(msg);
115 return (error);
116}
117
118static int
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 */
153static int
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
164static int
166{
168
169 if (NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0 &&
170 NG_NODE_IS_VALID(NG_HOOK_NODE(hook)) && !priv->persistent)
172 return (0);
173}
#define NGI_M(i)
Definition: netgraph.h:833
#define NG_HOOK_NODE(hook)
Definition: netgraph.h:339
int ng_rcvmsg_t(node_p node, item_p item, hook_p lasthook)
Definition: netgraph.h:105
#define NG_FWD_ITEM_HOOK(error, item, hook)
Definition: netgraph.h:898
int ng_disconnect_t(hook_p hook)
Definition: netgraph.h:107
#define NG_NODE_SET_PRIVATE(node, val)
Definition: netgraph.h:608
#define NG_NODE_IS_VALID(node)
Definition: netgraph.h:610
#define NG_NODE_UNREF(node)
Definition: netgraph.h:607
int ng_rmnode_self(node_p here)
Definition: ng_base.c:1609
#define NG_SEND_DATA_ONLY(error, hook, m)
Definition: netgraph.h:932
int ng_rcvdata_t(hook_p hook, item_p item)
Definition: netgraph.h:106
int ng_shutdown_t(node_p node)
Definition: netgraph.h:101
#define NG_FREE_ITEM(item)
Definition: netgraph.h:847
int ng_constructor_t(node_p node)
Definition: netgraph.h:99
#define NG_FREE_MSG(msg)
Definition: netgraph.h:938
#define NG_ABI_VERSION
Definition: netgraph.h:77
#define NG_NODE_NUMHOOKS(node)
Definition: netgraph.h:615
#define NGI_GET_MSG(i, m)
Definition: netgraph.h:858
#define NG_NODE_PRIVATE(node)
Definition: netgraph.h:609
MALLOC_DEFINE(M_NG_CCATM, "ng_ccatm", "netgraph uni api node")
static ng_shutdown_t ng_hub_shutdown
Definition: ng_hub.c:56
static const struct ng_cmdlist ng_hub_cmdlist[]
Definition: ng_hub.c:61
static ng_rcvmsg_t ng_hub_rcvmsg
Definition: ng_hub.c:55
static ng_constructor_t ng_hub_constructor
Definition: ng_hub.c:54
static struct ng_type ng_hub_typestruct
Definition: ng_hub.c:72
static ng_rcvdata_t ng_hub_rcvdata
Definition: ng_hub.c:57
#define M_NETGRAPH_HUB
Definition: ng_hub.c:44
static ng_disconnect_t ng_hub_disconnect
Definition: ng_hub.c:58
struct ng_hub_private * priv_p
Definition: ng_hub.c:51
NETGRAPH_INIT(hub, &ng_hub_typestruct)
#define NGM_HUB_COOKIE
Definition: ng_hub.h:36
@ NGM_HUB_SET_PERSISTENT
Definition: ng_hub.h:40
#define NG_HUB_NODE_TYPE
Definition: ng_hub.h:35
int persistent
Definition: ng_hub.c:49
u_int32_t typecookie
Definition: ng_message.h:66
struct ng_mesg::ng_msghdr header
u_int32_t version
Definition: netgraph.h:1077
Definition: ng_sscfu.c:66