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