Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/freebsd-src
Path: blob/main/sys/netgraph/ng_hole.c
34372 views
1
/*
2
* ng_hole.c
3
*/
4
5
/*-
6
* Copyright (c) 1996-1999 Whistle Communications, Inc.
7
* All rights reserved.
8
*
9
* Subject to the following obligations and disclaimer of warranty, use and
10
* redistribution of this software, in source or object code forms, with or
11
* without modifications are expressly permitted by Whistle Communications;
12
* provided, however, that:
13
* 1. Any and all reproductions of the source or object code must include the
14
* copyright notice above and the following disclaimer of warranties; and
15
* 2. No rights are granted, in any manner or form, to use Whistle
16
* Communications, Inc. trademarks, including the mark "WHISTLE
17
* COMMUNICATIONS" on advertising, endorsements, or otherwise except as
18
* such appears in the above copyright notice or in the software.
19
*
20
* THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
21
* TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
22
* REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
23
* INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
24
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
25
* WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
26
* REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
27
* SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
28
* IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES
29
* RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
30
* WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
31
* PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
32
* SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
33
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
35
* THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
36
* OF SUCH DAMAGE.
37
*
38
* Author: Julian Elisher <[email protected]>
39
* $Whistle: ng_hole.c,v 1.10 1999/11/01 09:24:51 julian Exp $
40
*/
41
42
/*
43
* This node is a 'black hole' that simply discards everything it receives
44
*/
45
46
#include <sys/param.h>
47
#include <sys/systm.h>
48
#include <sys/kernel.h>
49
#include <sys/malloc.h>
50
#include <sys/mbuf.h>
51
#include <netgraph/ng_message.h>
52
#include <netgraph/netgraph.h>
53
#include <netgraph/ng_parse.h>
54
#include <netgraph/ng_hole.h>
55
56
/* Per hook private info. */
57
struct ng_hole_hookinfo {
58
struct ng_hole_hookstat stats;
59
};
60
typedef struct ng_hole_hookinfo *hinfo_p;
61
62
/* Parse type for struct ng_hole_hookstat. */
63
static const struct ng_parse_struct_field ng_hole_hookstat_type_fields[] =
64
NG_HOLE_HOOKSTAT_TYPE_INFO;
65
static const struct ng_parse_type ng_hole_hookstat_type = {
66
&ng_parse_struct_type,
67
&ng_hole_hookstat_type_fields
68
};
69
70
/* List of commands and how to convert arguments to/from ASCII. */
71
static const struct ng_cmdlist ng_hole_cmdlist[] = {
72
{
73
NGM_HOLE_COOKIE,
74
NGM_HOLE_GET_STATS,
75
"getstats",
76
&ng_parse_hookbuf_type,
77
&ng_hole_hookstat_type
78
},
79
{
80
NGM_HOLE_COOKIE,
81
NGM_HOLE_CLR_STATS,
82
"clrstats",
83
&ng_parse_hookbuf_type,
84
NULL
85
},
86
{
87
NGM_HOLE_COOKIE,
88
NGM_HOLE_GETCLR_STATS,
89
"getclrstats",
90
&ng_parse_hookbuf_type,
91
&ng_hole_hookstat_type
92
},
93
{ 0 }
94
};
95
96
/* Netgraph methods */
97
static ng_constructor_t ngh_cons;
98
static ng_rcvmsg_t ngh_rcvmsg;
99
static ng_newhook_t ngh_newhook;
100
static ng_rcvdata_t ngh_rcvdata;
101
static ng_disconnect_t ngh_disconnect;
102
103
static struct ng_type typestruct = {
104
.version = NG_ABI_VERSION,
105
.name = NG_HOLE_NODE_TYPE,
106
.constructor = ngh_cons,
107
.rcvmsg = ngh_rcvmsg,
108
.newhook = ngh_newhook,
109
.rcvdata = ngh_rcvdata,
110
.disconnect = ngh_disconnect,
111
.cmdlist = ng_hole_cmdlist,
112
};
113
NETGRAPH_INIT(hole, &typestruct);
114
115
/*
116
* Be obliging. but no work to do.
117
*/
118
static int
119
ngh_cons(node_p node)
120
{
121
return(0);
122
}
123
124
/*
125
* Add a hook.
126
*/
127
static int
128
ngh_newhook(node_p node, hook_p hook, const char *name)
129
{
130
hinfo_p hip;
131
132
/* Create hook private structure. */
133
hip = malloc(sizeof(*hip), M_NETGRAPH, M_NOWAIT | M_ZERO);
134
if (hip == NULL)
135
return (ENOMEM);
136
NG_HOOK_SET_PRIVATE(hook, hip);
137
return (0);
138
}
139
140
/*
141
* Receive a control message.
142
*/
143
static int
144
ngh_rcvmsg(node_p node, item_p item, hook_p lasthook)
145
{
146
struct ng_mesg *msg;
147
struct ng_mesg *resp = NULL;
148
int error = 0;
149
struct ng_hole_hookstat *stats;
150
hook_p hook;
151
152
NGI_GET_MSG(item, msg);
153
switch (msg->header.typecookie) {
154
case NGM_HOLE_COOKIE:
155
switch (msg->header.cmd) {
156
case NGM_HOLE_GET_STATS:
157
case NGM_HOLE_CLR_STATS:
158
case NGM_HOLE_GETCLR_STATS:
159
/* Sanity check. */
160
if (msg->header.arglen != NG_HOOKSIZ) {
161
error = EINVAL;
162
break;
163
}
164
/* Find hook. */
165
hook = ng_findhook(node, (char *)msg->data);
166
if (hook == NULL) {
167
error = ENOENT;
168
break;
169
}
170
stats = &((hinfo_p)NG_HOOK_PRIVATE(hook))->stats;
171
/* Build response (if desired). */
172
if (msg->header.cmd != NGM_HOLE_CLR_STATS) {
173
NG_MKRESPONSE(resp, msg, sizeof(*stats),
174
M_NOWAIT);
175
if (resp == NULL) {
176
error = ENOMEM;
177
break;
178
}
179
bcopy(stats, resp->data, sizeof(*stats));
180
}
181
/* Clear stats (if desired). */
182
if (msg->header.cmd != NGM_HOLE_GET_STATS)
183
bzero(stats, sizeof(*stats));
184
break;
185
default: /* Unknown command. */
186
error = EINVAL;
187
break;
188
}
189
break;
190
default: /* Unknown type cookie. */
191
error = EINVAL;
192
break;
193
}
194
NG_RESPOND_MSG(error, node, item, resp);
195
NG_FREE_MSG(msg);
196
return (error);
197
}
198
199
/*
200
* Receive data
201
*/
202
static int
203
ngh_rcvdata(hook_p hook, item_p item)
204
{
205
const hinfo_p hip = NG_HOOK_PRIVATE(hook);
206
207
hip->stats.frames++;
208
hip->stats.octets += NGI_M(item)->m_pkthdr.len;
209
NG_FREE_ITEM(item);
210
return 0;
211
}
212
213
/*
214
* Hook disconnection
215
*/
216
static int
217
ngh_disconnect(hook_p hook)
218
{
219
220
free(NG_HOOK_PRIVATE(hook), M_NETGRAPH);
221
NG_HOOK_SET_PRIVATE(hook, NULL);
222
if (NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0)
223
ng_rmnode_self(NG_HOOK_NODE(hook));
224
return (0);
225
}
226
227