FreeBSD kernel netgraph code
ng_hole.c
Go to the documentation of this file.
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 <julian@freebsd.org>
39 *
40 * $FreeBSD$
41 * $Whistle: ng_hole.c,v 1.10 1999/11/01 09:24:51 julian Exp $
42 */
43
44/*
45 * This node is a 'black hole' that simply discards everything it receives
46 */
47
48#include <sys/param.h>
49#include <sys/systm.h>
50#include <sys/kernel.h>
51#include <sys/malloc.h>
52#include <sys/mbuf.h>
53#include <netgraph/ng_message.h>
54#include <netgraph/netgraph.h>
55#include <netgraph/ng_parse.h>
56#include <netgraph/ng_hole.h>
57
58/* Per hook private info. */
61};
62typedef struct ng_hole_hookinfo *hinfo_p;
63
64/* Parse type for struct ng_hole_hookstat. */
67static const struct ng_parse_type ng_hole_hookstat_type = {
70};
71
72/* List of commands and how to convert arguments to/from ASCII. */
73static const struct ng_cmdlist ng_hole_cmdlist[] = {
74 {
77 "getstats",
80 },
81 {
84 "clrstats",
86 NULL
87 },
88 {
91 "getclrstats",
94 },
95 { 0 }
96};
97
98/* Netgraph methods */
104
105static struct ng_type typestruct = {
107 .name = NG_HOLE_NODE_TYPE,
108 .constructor = ngh_cons,
109 .rcvmsg = ngh_rcvmsg,
110 .newhook = ngh_newhook,
111 .rcvdata = ngh_rcvdata,
112 .disconnect = ngh_disconnect,
113 .cmdlist = ng_hole_cmdlist,
114};
116
117/*
118 * Be obliging. but no work to do.
119 */
120static int
122{
123 return(0);
124}
125
126/*
127 * Add a hook.
128 */
129static int
130ngh_newhook(node_p node, hook_p hook, const char *name)
131{
132 hinfo_p hip;
133
134 /* Create hook private structure. */
135 hip = malloc(sizeof(*hip), M_NETGRAPH, M_NOWAIT | M_ZERO);
136 if (hip == NULL)
137 return (ENOMEM);
138 NG_HOOK_SET_PRIVATE(hook, hip);
139 return (0);
140}
141
142/*
143 * Receive a control message.
144 */
145static int
146ngh_rcvmsg(node_p node, item_p item, hook_p lasthook)
147{
148 struct ng_mesg *msg;
149 struct ng_mesg *resp = NULL;
150 int error = 0;
151 struct ng_hole_hookstat *stats;
152 hook_p hook;
153
154 NGI_GET_MSG(item, msg);
155 switch (msg->header.typecookie) {
156 case NGM_HOLE_COOKIE:
157 switch (msg->header.cmd) {
161 /* Sanity check. */
162 if (msg->header.arglen != NG_HOOKSIZ) {
163 error = EINVAL;
164 break;
165 }
166 /* Find hook. */
167 hook = ng_findhook(node, (char *)msg->data);
168 if (hook == NULL) {
169 error = ENOENT;
170 break;
171 }
172 stats = &((hinfo_p)NG_HOOK_PRIVATE(hook))->stats;
173 /* Build response (if desired). */
174 if (msg->header.cmd != NGM_HOLE_CLR_STATS) {
175 NG_MKRESPONSE(resp, msg, sizeof(*stats),
176 M_NOWAIT);
177 if (resp == NULL) {
178 error = ENOMEM;
179 break;
180 }
181 bcopy(stats, resp->data, sizeof(*stats));
182 }
183 /* Clear stats (if desired). */
184 if (msg->header.cmd != NGM_HOLE_GET_STATS)
185 bzero(stats, sizeof(*stats));
186 break;
187 default: /* Unknown command. */
188 error = EINVAL;
189 break;
190 }
191 break;
192 default: /* Unknown type cookie. */
193 error = EINVAL;
194 break;
195 }
196 NG_RESPOND_MSG(error, node, item, resp);
197 NG_FREE_MSG(msg);
198 return (error);
199}
200
201/*
202 * Receive data
203 */
204static int
206{
207 const hinfo_p hip = NG_HOOK_PRIVATE(hook);
208
209 hip->stats.frames++;
210 hip->stats.octets += NGI_M(item)->m_pkthdr.len;
211 NG_FREE_ITEM(item);
212 return 0;
213}
214
215/*
216 * Hook disconnection
217 */
218static int
220{
221
222 free(NG_HOOK_PRIVATE(hook), M_NETGRAPH);
223 NG_HOOK_SET_PRIVATE(hook, NULL);
224 if (NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0)
226 return (0);
227}
#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
int ng_disconnect_t(hook_p hook)
Definition: netgraph.h:107
#define NG_RESPOND_MSG(error, here, item, resp)
Definition: netgraph.h:1025
#define NG_HOOK_SET_PRIVATE(hook, val)
Definition: netgraph.h:333
int ng_rmnode_self(node_p here)
Definition: ng_base.c:1609
int ng_rcvdata_t(hook_p hook, item_p item)
Definition: netgraph.h:106
#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
int ng_newhook_t(node_p node, hook_p hook, const char *name)
Definition: netgraph.h:102
hook_p ng_findhook(node_p node, const char *name)
Definition: ng_base.c:1129
#define NG_HOOK_PRIVATE(hook)
Definition: netgraph.h:336
static ng_disconnect_t ngh_disconnect
Definition: ng_hole.c:103
static ng_rcvmsg_t ngh_rcvmsg
Definition: ng_hole.c:100
struct ng_hole_hookinfo * hinfo_p
Definition: ng_hole.c:62
static ng_constructor_t ngh_cons
Definition: ng_hole.c:99
static const struct ng_parse_struct_field ng_hole_hookstat_type_fields[]
Definition: ng_hole.c:65
static struct ng_type typestruct
Definition: ng_hole.c:105
static const struct ng_cmdlist ng_hole_cmdlist[]
Definition: ng_hole.c:73
static const struct ng_parse_type ng_hole_hookstat_type
Definition: ng_hole.c:67
static ng_rcvdata_t ngh_rcvdata
Definition: ng_hole.c:102
static ng_newhook_t ngh_newhook
Definition: ng_hole.c:101
NETGRAPH_INIT(hole, &typestruct)
#define NG_HOLE_NODE_TYPE
Definition: ng_hole.h:48
#define NG_HOLE_HOOKSTAT_TYPE_INFO
Definition: ng_hole.h:58
#define NGM_HOLE_COOKIE
Definition: ng_hole.h:49
@ NGM_HOLE_GETCLR_STATS
Definition: ng_hole.h:68
@ NGM_HOLE_CLR_STATS
Definition: ng_hole.h:67
@ NGM_HOLE_GET_STATS
Definition: ng_hole.h:66
#define NG_MKRESPONSE(rsp, msg, len, how)
Definition: ng_message.h:396
#define NG_HOOKSIZ
Definition: ng_message.h:49
const struct ng_parse_type ng_parse_struct_type
Definition: ng_parse.c:222
const struct ng_parse_type ng_parse_hookbuf_type
Definition: ng_parse.c:851
char *const name
Definition: ng_ppp.c:261
struct ng_bpf_hookstat stats
Definition: ng_bpf.c:95
u_int32_t arglen
Definition: ng_message.h:62
u_int32_t typecookie
Definition: ng_message.h:66
struct ng_mesg::ng_msghdr header
char data[]
Definition: ng_message.h:69
u_int32_t version
Definition: netgraph.h:1077
Definition: ng_sscop.c:74