xref: /dragonfly/sys/netgraph/ng_sample.c (revision 0db87cb7)
1 
2 /*
3  * ng_sample.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 Elischer <julian@freebsd.org>
38  *
39  * $FreeBSD: src/sys/netgraph/ng_sample.c,v 1.7.2.3 2002/07/02 23:44:03 archie Exp $
40  * $Whistle: ng_sample.c,v 1.13 1999/11/01 09:24:52 julian Exp $
41  */
42 
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/kernel.h>
46 #include <sys/mbuf.h>
47 #include <sys/malloc.h>
48 #include <sys/ctype.h>
49 #include <sys/errno.h>
50 #include <sys/syslog.h>
51 
52 #include <netgraph/ng_message.h>
53 #include <netgraph/ng_parse.h>
54 #include <netgraph/ng_sample.h>
55 #include <netgraph/netgraph.h>
56 
57 /*
58  * This section contains the netgraph method declarations for the
59  * sample node. These methods define the netgraph 'type'.
60  */
61 
62 static ng_constructor_t	ng_xxx_constructor;
63 static ng_rcvmsg_t	ng_xxx_rcvmsg;
64 static ng_shutdown_t	ng_xxx_rmnode;
65 static ng_newhook_t	ng_xxx_newhook;
66 static ng_connect_t	ng_xxx_connect;
67 static ng_rcvdata_t	ng_xxx_rcvdata;	 /* note these are both ng_rcvdata_t */
68 static ng_rcvdata_t	ng_xxx_rcvdataq; /* note these are both ng_rcvdata_t */
69 static ng_disconnect_t	ng_xxx_disconnect;
70 
71 /* Parse type for struct ngxxxstat */
72 static const struct ng_parse_struct_field ng_xxx_stat_type_fields[]
73 	= NG_XXX_STATS_TYPE_INFO;
74 static const struct ng_parse_type ng_xxx_stat_type = {
75 	&ng_parse_struct_type,
76 	&ng_xxx_stat_type_fields
77 };
78 
79 /* List of commands and how to convert arguments to/from ASCII */
80 static const struct ng_cmdlist ng_xxx_cmdlist[] = {
81 	{
82 	  NGM_XXX_COOKIE,
83 	  NGM_XXX_GET_STATUS,
84 	  "getstatus",
85 	  NULL,
86 	  &ng_xxx_stat_type,
87 	},
88 	{
89 	  NGM_XXX_COOKIE,
90 	  NGM_XXX_SET_FLAG,
91 	  "setflag",
92 	  &ng_parse_int32_type,
93 	  NULL
94 	},
95 	{ 0 }
96 };
97 
98 /* Netgraph node type descriptor */
99 static struct ng_type typestruct = {
100 	NG_VERSION,
101 	NG_XXX_NODE_TYPE,
102 	NULL,
103 	ng_xxx_constructor,
104 	ng_xxx_rcvmsg,
105 	ng_xxx_rmnode,
106 	ng_xxx_newhook,
107 	NULL,
108 	ng_xxx_connect,
109 	ng_xxx_rcvdata,
110 	ng_xxx_rcvdataq,
111 	ng_xxx_disconnect,
112 	ng_xxx_cmdlist
113 };
114 NETGRAPH_INIT(xxx, &typestruct);
115 
116 /* Information we store for each hook on each node */
117 struct XXX_hookinfo {
118 	int     dlci;		/* The DLCI it represents, -1 == downstream */
119 	int     channel;	/* The channel representing this DLCI */
120 	hook_p  hook;
121 };
122 
123 /* Information we store for each node */
124 struct XXX {
125 	struct XXX_hookinfo channel[XXX_NUM_DLCIS];
126 	struct XXX_hookinfo downstream_hook;
127 	node_p		node;		/* back pointer to node */
128 	hook_p  	debughook;
129 	u_int   	packets_in;	/* packets in from downstream */
130 	u_int   	packets_out;	/* packets out towards downstream */
131 	u_int32_t	flags;
132 };
133 typedef struct XXX *xxx_p;
134 
135 /*
136  * Allocate the private data structure and the generic node
137  * and link them together.
138  *
139  * ng_make_node_common() returns with a generic node struct
140  * with a single reference for us.. we transfer it to the
141  * private structure.. when we free the private struct we must
142  * unref the node so it gets freed too.
143  *
144  * If this were a device node than this work would be done in the attach()
145  * routine and the constructor would return EINVAL as you should not be able
146  * to creatednodes that depend on hardware (unless you can add the hardware :)
147  */
148 static int
149 ng_xxx_constructor(node_p *nodep)
150 {
151 	xxx_p privdata;
152 	int i, error;
153 
154 	/* Initialize private descriptor */
155 	privdata = kmalloc(sizeof(*privdata), M_NETGRAPH, M_NOWAIT | M_ZERO);
156 	if (privdata == NULL)
157 		return (ENOMEM);
158 	for (i = 0; i < XXX_NUM_DLCIS; i++) {
159 		privdata->channel[i].dlci = -2;
160 		privdata->channel[i].channel = i;
161 	}
162 
163 	/* Call the 'generic' (ie, superclass) node constructor */
164 	if ((error = ng_make_node_common(&typestruct, nodep))) {
165 		kfree(privdata, M_NETGRAPH);
166 		return (error);
167 	}
168 
169 	/* Link structs together; this counts as our one reference to *nodep */
170 	(*nodep)->private = privdata;
171 	privdata->node = *nodep;
172 	return (0);
173 }
174 
175 /*
176  * Give our ok for a hook to be added...
177  * If we are not running this might kick a device into life.
178  * Possibly decode information out of the hook name.
179  * Add the hook's private info to the hook structure.
180  * (if we had some). In this example, we assume that there is a
181  * an array of structs, called 'channel' in the private info,
182  * one for each active channel. The private
183  * pointer of each hook points to the appropriate XXX_hookinfo struct
184  * so that the source of an input packet is easily identified.
185  * (a dlci is a frame relay channel)
186  */
187 static int
188 ng_xxx_newhook(node_p node, hook_p hook, const char *name)
189 {
190 	const xxx_p xxxp = node->private;
191 	const char *cp;
192 	int dlci = 0;
193 	int chan;
194 
195 #if 0
196 	/* Possibly start up the device if it's not already going */
197 	if ((xxxp->flags & SCF_RUNNING) == 0) {
198 		ng_xxx_start_hardware(xxxp);
199 	}
200 #endif
201 
202 	/* Example of how one might use hooks with embedded numbers: All
203 	 * hooks start with 'dlci' and have a decimal trailing channel
204 	 * number up to 4 digits Use the leadin defined int he associated .h
205 	 * file. */
206 	if (strncmp(name,
207 	    NG_XXX_HOOK_DLCI_LEADIN, strlen(NG_XXX_HOOK_DLCI_LEADIN)) == 0) {
208 		char *eptr;
209 
210 		cp = name + sizeof(NG_XXX_HOOK_DLCI_LEADIN);
211 		if (!isdigit(*cp) || (cp[0] == '0' && cp[1] != '\0'))
212 			return (EINVAL);
213 		dlci = (int)strtoul(cp, &eptr, 10);
214 		if (*eptr != '\0' || dlci < 0 || dlci > 1023)
215 			return (EINVAL);
216 
217 		/* We have a dlci, now either find it, or allocate it */
218 		for (chan = 0; chan < XXX_NUM_DLCIS; chan++)
219 			if (xxxp->channel[chan].dlci == dlci)
220 				break;
221 		if (chan == XXX_NUM_DLCIS) {
222 			for (chan = 0; chan < XXX_NUM_DLCIS; chan++)
223 				if (xxxp->channel[chan].dlci != -2)
224 					continue;
225 			if (chan == XXX_NUM_DLCIS)
226 				return (ENOBUFS);
227 		}
228 		if (xxxp->channel[chan].hook != NULL)
229 			return (EADDRINUSE);
230 		hook->private = xxxp->channel + chan;
231 		xxxp->channel[chan].hook = hook;
232 		return (0);
233 	} else if (strcmp(name, NG_XXX_HOOK_DOWNSTREAM) == 0) {
234 		/* Example of simple predefined hooks. */
235 		/* do something specific to the downstream connection */
236 		xxxp->downstream_hook.hook = hook;
237 		hook->private = &xxxp->downstream_hook;
238 	} else if (strcmp(name, NG_XXX_HOOK_DEBUG) == 0) {
239 		/* do something specific to a debug connection */
240 		xxxp->debughook = hook;
241 		hook->private = NULL;
242 	} else
243 		return (EINVAL);	/* not a hook we know about */
244 	return(0);
245 }
246 
247 /*
248  * Get a netgraph control message.
249  * Check it is one we understand. If needed, send a response.
250  * We could save the address for an async action later, but don't here.
251  * Always free the message.
252  * The response should be in a malloc'd region that the caller can 'free'.
253  * A response is not required.
254  * Theoretically you could respond defferently to old message types if
255  * the cookie in the header didn't match what we consider to be current
256  * (so that old userland programs could continue to work).
257  */
258 static int
259 ng_xxx_rcvmsg(node_p node,
260 	   struct ng_mesg *msg, const char *retaddr, struct ng_mesg **rptr)
261 {
262 	const xxx_p xxxp = node->private;
263 	struct ng_mesg *resp = NULL;
264 	int error = 0;
265 
266 	/* Deal with message according to cookie and command */
267 	switch (msg->header.typecookie) {
268 	case NGM_XXX_COOKIE:
269 		switch (msg->header.cmd) {
270 		case NGM_XXX_GET_STATUS:
271 		    {
272 			struct ngxxxstat *stats;
273 
274 			NG_MKRESPONSE(resp, msg, sizeof(*stats), M_NOWAIT);
275 			if (!resp) {
276 				error = ENOMEM;
277 				break;
278 			}
279 			stats = (struct ngxxxstat *) resp->data;
280 			stats->packets_in = xxxp->packets_in;
281 			stats->packets_out = xxxp->packets_out;
282 			break;
283 		    }
284 		case NGM_XXX_SET_FLAG:
285 			if (msg->header.arglen != sizeof(u_int32_t)) {
286 				error = EINVAL;
287 				break;
288 			}
289 			xxxp->flags = *((u_int32_t *) msg->data);
290 			break;
291 		default:
292 			error = EINVAL;		/* unknown command */
293 			break;
294 		}
295 		break;
296 	default:
297 		error = EINVAL;			/* unknown cookie type */
298 		break;
299 	}
300 
301 	/* Take care of synchronous response, if any */
302 	if (rptr)
303 		*rptr = resp;
304 	else if (resp)
305 		kfree(resp, M_NETGRAPH);
306 
307 	/* Free the message and return */
308 	kfree(msg, M_NETGRAPH);
309 	return(error);
310 }
311 
312 /*
313  * Receive data, and do something with it.
314  * Possibly send it out on another link after processing.
315  * Possibly do something different if it comes from different
316  * hooks. the caller will never free m or meta, so
317  * if we use up this data or abort we must free BOTH of these.
318  *
319  * If we want, we may decide to force this data to be queued and reprocessed
320  * at the netgraph NETISR time. (at which time it will be entered using ng_xxx_rcvdataq().
321  */
322 static int
323 ng_xxx_rcvdata(hook_p hook, struct mbuf *m, meta_p meta)
324 {
325 	int dlci = -2;
326 
327 	if (hook->private) {
328 		/*
329 		 * If it's dlci 1023, requeue it so that it's handled
330 		 * at a lower priority. This is how a node decides to
331 		 * defer a data message.
332 		 */
333 		dlci = ((struct XXX_hookinfo *) hook->private)->dlci;
334 		if (dlci == 1023) {
335 			return(ng_queue_data(hook->peer, m, meta));
336 		}
337 	}
338 	return(ng_xxx_rcvdataq(hook, m, meta));
339 }
340 
341 /*
342  * Always accept the data. This version of rcvdata is called from the dequeueing routine.
343  */
344 static int
345 ng_xxx_rcvdataq(hook_p hook, struct mbuf *m, meta_p meta)
346 {
347 	const xxx_p xxxp = hook->node->private;
348 	int chan = -2;
349 	int dlci = -2;
350 	int error;
351 
352 	if (hook->private) {
353 		dlci = ((struct XXX_hookinfo *) hook->private)->dlci;
354 		chan = ((struct XXX_hookinfo *) hook->private)->channel;
355 		if (dlci != -1) {
356 			/* If received on a DLCI hook process for this
357 			 * channel and pass it to the downstream module.
358 			 * Normally one would add a multiplexing header at
359 			 * the front here */
360 			/* M_PREPEND(....)	; */
361 			/* mtod(m, xxxxxx)->dlci = dlci; */
362 			error = ng_send_data(xxxp->downstream_hook.hook,
363 			    m, meta);
364 			xxxp->packets_out++;
365 		} else {
366 			/* data came from the multiplexed link */
367 			dlci = 1;	/* get dlci from header */
368 			/* madjust(....) *//* chop off header */
369 			for (chan = 0; chan < XXX_NUM_DLCIS; chan++)
370 				if (xxxp->channel[chan].dlci == dlci)
371 					break;
372 			if (chan == XXX_NUM_DLCIS) {
373 				NG_FREE_DATA(m, meta);
374 				return (ENETUNREACH);
375 			}
376 			/* If we were called at splnet, use the following:
377 			 * NG_SEND_DATA(error, otherhook, m, meta); if this
378 			 * node is running at some SPL other than SPLNET
379 			 * then you should use instead: error =
380 			 * ng_queueit(otherhook, m, meta); m = NULL: meta =
381 			 * NULL; this queues the data using the standard
382 			 * NETISR system and schedules the data to be picked
383 			 * up again once the system has moved to SPLNET and
384 			 * the processing of the data can continue. after
385 			 * these are run 'm' and 'meta' should be considered
386 			 * as invalid and NG_SEND_DATA actually zaps them. */
387 			NG_SEND_DATA(error, xxxp->channel[chan].hook, m, meta);
388 			xxxp->packets_in++;
389 		}
390 	} else {
391 		/* It's the debug hook, throw it away.. */
392 		if (hook == xxxp->downstream_hook.hook)
393 			NG_FREE_DATA(m, meta);
394 	}
395 	return 0;
396 }
397 
398 #if 0
399 /*
400  * If this were a device node, the data may have been received in response
401  * to some interrupt.
402  * in which case it would probably look as follows:
403  */
404 void
405 devintr(void)
406 {
407 	meta_p  meta = NULL;	/* whatever metadata we might imagine goes
408 				 * here */
409 
410 	/* get packet from device and send on */
411 	m = MGET(blah blah)
412 	    error = ng_queueit(upstream, m, meta);	/* see note above in
413 							 * xxx_rcvdata() */
414 }
415 
416 #endif				/* 0 */
417 
418 /*
419  * Do local shutdown processing..
420  * If we are a persistant device, we might refuse to go away, and
421  * we'd only remove our links and reset ourself.
422  */
423 static int
424 ng_xxx_rmnode(node_p node)
425 {
426 	const xxx_p privdata = node->private;
427 
428 	node->flags |= NG_INVALID;
429 	ng_cutlinks(node);
430 #ifndef PERSISTANT_NODE
431 	ng_unname(node);
432 	node->private = NULL;
433 	ng_unref(privdata->node);
434 	kfree(privdata, M_NETGRAPH);
435 #else
436 	privdata->packets_in = 0;		/* reset stats */
437 	privdata->packets_out = 0;
438 	node->flags &= ~NG_INVALID;		/* reset invalid flag */
439 #endif /* PERSISTANT_NODE */
440 	return (0);
441 }
442 
443 /*
444  * This is called once we've already connected a new hook to the other node.
445  * It gives us a chance to balk at the last minute.
446  */
447 static int
448 ng_xxx_connect(hook_p hook)
449 {
450 	/* be really amiable and just say "YUP that's OK by me! " */
451 	return (0);
452 }
453 
454 /*
455  * Dook disconnection
456  *
457  * For this type, removal of the last link destroys the node
458  */
459 static int
460 ng_xxx_disconnect(hook_p hook)
461 {
462 	if (hook->private)
463 		((struct XXX_hookinfo *) (hook->private))->hook = NULL;
464 	if (hook->node->numhooks == 0)
465 		ng_rmnode(hook->node);
466 	return (0);
467 }
468 
469