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$
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. */
58 struct ng_hole_hookinfo {
59 struct ng_hole_hookstat stats;
60 };
61 typedef struct ng_hole_hookinfo *hinfo_p;
62
63 /* Parse type for struct ng_hole_hookstat. */
64 static const struct ng_parse_struct_field ng_hole_hookstat_type_fields[] =
65 NG_HOLE_HOOKSTAT_TYPE_INFO;
66 static 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. */
72 static 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 */
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 NG_VERSION,
105 NG_HOLE_NODE_TYPE,
106 NULL,
107 NULL,
108 ngh_rcvmsg,
109 NULL,
110 ngh_newhook,
111 NULL,
112 NULL,
113 ngh_rcvdata,
114 ngh_rcvdata,
115 ngh_disconnect,
116 ng_hole_cmdlist
117 };
118 NETGRAPH_INIT(hole, &typestruct);
119
120 /*
121 * Add a hook.
122 */
123 static int
124 ngh_newhook(node_p node, hook_p hook, const char *name)
125 {
126 hinfo_p hip;
127
128 /* Create hook private structure. */
129 MALLOC(hip, hinfo_p, sizeof(*hip), M_NETGRAPH, M_NOWAIT | M_ZERO);
130 if (hip == NULL)
131 return (ENOMEM);
132 NG_HOOK_SET_PRIVATE(hook, hip);
133 return (0);
134 }
135
136 /*
137 * Receive a control message.
138 */
139 static int
140 ngh_rcvmsg(node_p node, struct ng_mesg *msg, const char *retaddr,
141 struct ng_mesg **rptr)
142 {
143 struct ng_mesg *resp = NULL;
144 int error = 0;
145 struct ng_hole_hookstat *stats;
146 hook_p hook;
147
148 switch (msg->header.typecookie) {
149 case NGM_HOLE_COOKIE:
150 switch (msg->header.cmd) {
151 case NGM_HOLE_GET_STATS:
152 case NGM_HOLE_CLR_STATS:
153 case NGM_HOLE_GETCLR_STATS:
154 /* Sanity check. */
155 if (msg->header.arglen != NG_HOOKLEN + 1) {
156 error = EINVAL;
157 break;
158 }
159 /* Find hook. */
160 hook = ng_findhook(node, (char *)msg->data);
161 if (hook == NULL) {
162 error = ENOENT;
163 break;
164 }
165 stats = &((hinfo_p)NG_HOOK_PRIVATE(hook))->stats;
166 /* Build response (if desired). */
167 if (msg->header.cmd != NGM_HOLE_CLR_STATS) {
168 NG_MKRESPONSE(resp, msg, sizeof(*stats),
169 M_NOWAIT);
170 if (resp == NULL) {
171 error = ENOMEM;
172 break;
173 }
174 bcopy(stats, resp->data, sizeof(*stats));
175 }
176 /* Clear stats (if desired). */
177 if (msg->header.cmd != NGM_HOLE_GET_STATS)
178 bzero(stats, sizeof(*stats));
179 break;
180 default: /* Unknown command. */
181 error = EINVAL;
182 break;
183 }
184 break;
185 default: /* Unknown type cookie. */
186 error = EINVAL;
187 break;
188 }
189 NG_RESPOND_MSG(error, node, retaddr, resp, rptr);
190 NG_FREE_MSG(msg);
191 return (error);
192 }
193
194 /*
195 * Receive data
196 */
197 static int
198 ngh_rcvdata(hook_p hook, struct mbuf *m, meta_p meta)
199 {
200 const hinfo_p hip = NG_HOOK_PRIVATE(hook);
201
202 hip->stats.frames++;
203 hip->stats.octets += m->m_pkthdr.len;
204 NG_FREE_DATA(m, meta);
205 return 0;
206 }
207
208 /*
209 * Hook disconnection
210 */
211 static int
212 ngh_disconnect(hook_p hook)
213 {
214
215 FREE(NG_HOOK_PRIVATE(hook), M_NETGRAPH);
216 NG_HOOK_SET_PRIVATE(hook, NULL);
217 if (hook->node->numhooks == 0)
218 ng_rmnode(hook->node);
219 return (0);
220 }
Cache object: b52ec5fbaad59f214642ecec9960f819
|