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