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