1*a1fd9364Slogwang /*
2*a1fd9364Slogwang * ng_sample.c
3*a1fd9364Slogwang */
4*a1fd9364Slogwang
5*a1fd9364Slogwang /*-
6*a1fd9364Slogwang * Copyright (c) 1996-1999 Whistle Communications, Inc.
7*a1fd9364Slogwang * All rights reserved.
8*a1fd9364Slogwang *
9*a1fd9364Slogwang * Subject to the following obligations and disclaimer of warranty, use and
10*a1fd9364Slogwang * redistribution of this software, in source or object code forms, with or
11*a1fd9364Slogwang * without modifications are expressly permitted by Whistle Communications;
12*a1fd9364Slogwang * provided, however, that:
13*a1fd9364Slogwang * 1. Any and all reproductions of the source or object code must include the
14*a1fd9364Slogwang * copyright notice above and the following disclaimer of warranties; and
15*a1fd9364Slogwang * 2. No rights are granted, in any manner or form, to use Whistle
16*a1fd9364Slogwang * Communications, Inc. trademarks, including the mark "WHISTLE
17*a1fd9364Slogwang * COMMUNICATIONS" on advertising, endorsements, or otherwise except as
18*a1fd9364Slogwang * such appears in the above copyright notice or in the software.
19*a1fd9364Slogwang *
20*a1fd9364Slogwang * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
21*a1fd9364Slogwang * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
22*a1fd9364Slogwang * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
23*a1fd9364Slogwang * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
24*a1fd9364Slogwang * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
25*a1fd9364Slogwang * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
26*a1fd9364Slogwang * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
27*a1fd9364Slogwang * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
28*a1fd9364Slogwang * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES
29*a1fd9364Slogwang * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
30*a1fd9364Slogwang * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
31*a1fd9364Slogwang * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
32*a1fd9364Slogwang * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
33*a1fd9364Slogwang * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34*a1fd9364Slogwang * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
35*a1fd9364Slogwang * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
36*a1fd9364Slogwang * OF SUCH DAMAGE.
37*a1fd9364Slogwang *
38*a1fd9364Slogwang * Author: Julian Elischer <[email protected]>
39*a1fd9364Slogwang *
40*a1fd9364Slogwang * $FreeBSD$
41*a1fd9364Slogwang * $Whistle: ng_sample.c,v 1.13 1999/11/01 09:24:52 julian Exp $
42*a1fd9364Slogwang */
43*a1fd9364Slogwang
44*a1fd9364Slogwang #include <sys/param.h>
45*a1fd9364Slogwang #include <sys/systm.h>
46*a1fd9364Slogwang #include <sys/kernel.h>
47*a1fd9364Slogwang #include <sys/mbuf.h>
48*a1fd9364Slogwang #include <sys/malloc.h>
49*a1fd9364Slogwang #include <sys/ctype.h>
50*a1fd9364Slogwang #include <sys/errno.h>
51*a1fd9364Slogwang #include <sys/syslog.h>
52*a1fd9364Slogwang
53*a1fd9364Slogwang #include <netgraph/ng_message.h>
54*a1fd9364Slogwang #include <netgraph/ng_parse.h>
55*a1fd9364Slogwang #include <netgraph/ng_sample.h>
56*a1fd9364Slogwang #include <netgraph/netgraph.h>
57*a1fd9364Slogwang
58*a1fd9364Slogwang /* If you do complicated mallocs you may want to do this */
59*a1fd9364Slogwang /* and use it for your mallocs */
60*a1fd9364Slogwang #ifdef NG_SEPARATE_MALLOC
61*a1fd9364Slogwang static MALLOC_DEFINE(M_NETGRAPH_XXX, "netgraph_xxx", "netgraph xxx node");
62*a1fd9364Slogwang #else
63*a1fd9364Slogwang #define M_NETGRAPH_XXX M_NETGRAPH
64*a1fd9364Slogwang #endif
65*a1fd9364Slogwang
66*a1fd9364Slogwang /*
67*a1fd9364Slogwang * This section contains the netgraph method declarations for the
68*a1fd9364Slogwang * sample node. These methods define the netgraph 'type'.
69*a1fd9364Slogwang */
70*a1fd9364Slogwang
71*a1fd9364Slogwang static ng_constructor_t ng_xxx_constructor;
72*a1fd9364Slogwang static ng_rcvmsg_t ng_xxx_rcvmsg;
73*a1fd9364Slogwang static ng_shutdown_t ng_xxx_shutdown;
74*a1fd9364Slogwang static ng_newhook_t ng_xxx_newhook;
75*a1fd9364Slogwang static ng_connect_t ng_xxx_connect;
76*a1fd9364Slogwang static ng_rcvdata_t ng_xxx_rcvdata;
77*a1fd9364Slogwang static ng_disconnect_t ng_xxx_disconnect;
78*a1fd9364Slogwang
79*a1fd9364Slogwang /* Parse type for struct ngxxxstat */
80*a1fd9364Slogwang static const struct ng_parse_struct_field ng_xxx_stat_type_fields[]
81*a1fd9364Slogwang = NG_XXX_STATS_TYPE_INFO;
82*a1fd9364Slogwang static const struct ng_parse_type ng_xxx_stat_type = {
83*a1fd9364Slogwang &ng_parse_struct_type,
84*a1fd9364Slogwang &ng_xxx_stat_type_fields
85*a1fd9364Slogwang };
86*a1fd9364Slogwang
87*a1fd9364Slogwang /* List of commands and how to convert arguments to/from ASCII */
88*a1fd9364Slogwang static const struct ng_cmdlist ng_xxx_cmdlist[] = {
89*a1fd9364Slogwang {
90*a1fd9364Slogwang NGM_XXX_COOKIE,
91*a1fd9364Slogwang NGM_XXX_GET_STATUS,
92*a1fd9364Slogwang "getstatus",
93*a1fd9364Slogwang NULL,
94*a1fd9364Slogwang &ng_xxx_stat_type,
95*a1fd9364Slogwang },
96*a1fd9364Slogwang {
97*a1fd9364Slogwang NGM_XXX_COOKIE,
98*a1fd9364Slogwang NGM_XXX_SET_FLAG,
99*a1fd9364Slogwang "setflag",
100*a1fd9364Slogwang &ng_parse_int32_type,
101*a1fd9364Slogwang NULL
102*a1fd9364Slogwang },
103*a1fd9364Slogwang { 0 }
104*a1fd9364Slogwang };
105*a1fd9364Slogwang
106*a1fd9364Slogwang /* Netgraph node type descriptor */
107*a1fd9364Slogwang static struct ng_type typestruct = {
108*a1fd9364Slogwang .version = NG_ABI_VERSION,
109*a1fd9364Slogwang .name = NG_XXX_NODE_TYPE,
110*a1fd9364Slogwang .constructor = ng_xxx_constructor,
111*a1fd9364Slogwang .rcvmsg = ng_xxx_rcvmsg,
112*a1fd9364Slogwang .shutdown = ng_xxx_shutdown,
113*a1fd9364Slogwang .newhook = ng_xxx_newhook,
114*a1fd9364Slogwang /* .findhook = ng_xxx_findhook, */
115*a1fd9364Slogwang .connect = ng_xxx_connect,
116*a1fd9364Slogwang .rcvdata = ng_xxx_rcvdata,
117*a1fd9364Slogwang .disconnect = ng_xxx_disconnect,
118*a1fd9364Slogwang .cmdlist = ng_xxx_cmdlist,
119*a1fd9364Slogwang };
120*a1fd9364Slogwang NETGRAPH_INIT(xxx, &typestruct);
121*a1fd9364Slogwang
122*a1fd9364Slogwang /* Information we store for each hook on each node */
123*a1fd9364Slogwang struct XXX_hookinfo {
124*a1fd9364Slogwang int dlci; /* The DLCI it represents, -1 == downstream */
125*a1fd9364Slogwang int channel; /* The channel representing this DLCI */
126*a1fd9364Slogwang hook_p hook;
127*a1fd9364Slogwang };
128*a1fd9364Slogwang
129*a1fd9364Slogwang /* Information we store for each node */
130*a1fd9364Slogwang struct XXX {
131*a1fd9364Slogwang struct XXX_hookinfo channel[XXX_NUM_DLCIS];
132*a1fd9364Slogwang struct XXX_hookinfo downstream_hook;
133*a1fd9364Slogwang node_p node; /* back pointer to node */
134*a1fd9364Slogwang hook_p debughook;
135*a1fd9364Slogwang u_int packets_in; /* packets in from downstream */
136*a1fd9364Slogwang u_int packets_out; /* packets out towards downstream */
137*a1fd9364Slogwang u_int32_t flags;
138*a1fd9364Slogwang };
139*a1fd9364Slogwang typedef struct XXX *xxx_p;
140*a1fd9364Slogwang
141*a1fd9364Slogwang /*
142*a1fd9364Slogwang * Allocate the private data structure. The generic node has already
143*a1fd9364Slogwang * been created. Link them together. We arrive with a reference to the node
144*a1fd9364Slogwang * i.e. the reference count is incremented for us already.
145*a1fd9364Slogwang *
146*a1fd9364Slogwang * If this were a device node than this work would be done in the attach()
147*a1fd9364Slogwang * routine and the constructor would return EINVAL as you should not be able
148*a1fd9364Slogwang * to creatednodes that depend on hardware (unless you can add the hardware :)
149*a1fd9364Slogwang */
150*a1fd9364Slogwang static int
ng_xxx_constructor(node_p node)151*a1fd9364Slogwang ng_xxx_constructor(node_p node)
152*a1fd9364Slogwang {
153*a1fd9364Slogwang xxx_p privdata;
154*a1fd9364Slogwang int i;
155*a1fd9364Slogwang
156*a1fd9364Slogwang /* Initialize private descriptor */
157*a1fd9364Slogwang privdata = malloc(sizeof(*privdata), M_NETGRAPH, M_WAITOK | M_ZERO);
158*a1fd9364Slogwang for (i = 0; i < XXX_NUM_DLCIS; i++) {
159*a1fd9364Slogwang privdata->channel[i].dlci = -2;
160*a1fd9364Slogwang privdata->channel[i].channel = i;
161*a1fd9364Slogwang }
162*a1fd9364Slogwang
163*a1fd9364Slogwang /* Link structs together; this counts as our one reference to *nodep */
164*a1fd9364Slogwang NG_NODE_SET_PRIVATE(node, privdata);
165*a1fd9364Slogwang privdata->node = node;
166*a1fd9364Slogwang return (0);
167*a1fd9364Slogwang }
168*a1fd9364Slogwang
169*a1fd9364Slogwang /*
170*a1fd9364Slogwang * Give our ok for a hook to be added...
171*a1fd9364Slogwang * If we are not running this might kick a device into life.
172*a1fd9364Slogwang * Possibly decode information out of the hook name.
173*a1fd9364Slogwang * Add the hook's private info to the hook structure.
174*a1fd9364Slogwang * (if we had some). In this example, we assume that there is a
175*a1fd9364Slogwang * an array of structs, called 'channel' in the private info,
176*a1fd9364Slogwang * one for each active channel. The private
177*a1fd9364Slogwang * pointer of each hook points to the appropriate XXX_hookinfo struct
178*a1fd9364Slogwang * so that the source of an input packet is easily identified.
179*a1fd9364Slogwang * (a dlci is a frame relay channel)
180*a1fd9364Slogwang */
181*a1fd9364Slogwang static int
ng_xxx_newhook(node_p node,hook_p hook,const char * name)182*a1fd9364Slogwang ng_xxx_newhook(node_p node, hook_p hook, const char *name)
183*a1fd9364Slogwang {
184*a1fd9364Slogwang const xxx_p xxxp = NG_NODE_PRIVATE(node);
185*a1fd9364Slogwang const char *cp;
186*a1fd9364Slogwang int dlci = 0;
187*a1fd9364Slogwang int chan;
188*a1fd9364Slogwang
189*a1fd9364Slogwang #if 0
190*a1fd9364Slogwang /* Possibly start up the device if it's not already going */
191*a1fd9364Slogwang if ((xxxp->flags & SCF_RUNNING) == 0) {
192*a1fd9364Slogwang ng_xxx_start_hardware(xxxp);
193*a1fd9364Slogwang }
194*a1fd9364Slogwang #endif
195*a1fd9364Slogwang
196*a1fd9364Slogwang /* Example of how one might use hooks with embedded numbers: All
197*a1fd9364Slogwang * hooks start with 'dlci' and have a decimal trailing channel
198*a1fd9364Slogwang * number up to 4 digits Use the leadin defined int he associated .h
199*a1fd9364Slogwang * file. */
200*a1fd9364Slogwang if (strncmp(name,
201*a1fd9364Slogwang NG_XXX_HOOK_DLCI_LEADIN, strlen(NG_XXX_HOOK_DLCI_LEADIN)) == 0) {
202*a1fd9364Slogwang char *eptr;
203*a1fd9364Slogwang
204*a1fd9364Slogwang cp = name + strlen(NG_XXX_HOOK_DLCI_LEADIN);
205*a1fd9364Slogwang if (!isdigit(*cp) || (cp[0] == '0' && cp[1] != '\0'))
206*a1fd9364Slogwang return (EINVAL);
207*a1fd9364Slogwang dlci = (int)strtoul(cp, &eptr, 10);
208*a1fd9364Slogwang if (*eptr != '\0' || dlci < 0 || dlci > 1023)
209*a1fd9364Slogwang return (EINVAL);
210*a1fd9364Slogwang
211*a1fd9364Slogwang /* We have a dlci, now either find it, or allocate it */
212*a1fd9364Slogwang for (chan = 0; chan < XXX_NUM_DLCIS; chan++)
213*a1fd9364Slogwang if (xxxp->channel[chan].dlci == dlci)
214*a1fd9364Slogwang break;
215*a1fd9364Slogwang if (chan == XXX_NUM_DLCIS) {
216*a1fd9364Slogwang for (chan = 0; chan < XXX_NUM_DLCIS; chan++)
217*a1fd9364Slogwang if (xxxp->channel[chan].dlci == -2)
218*a1fd9364Slogwang break;
219*a1fd9364Slogwang if (chan == XXX_NUM_DLCIS)
220*a1fd9364Slogwang return (ENOBUFS);
221*a1fd9364Slogwang xxxp->channel[chan].dlci = dlci;
222*a1fd9364Slogwang }
223*a1fd9364Slogwang if (xxxp->channel[chan].hook != NULL)
224*a1fd9364Slogwang return (EADDRINUSE);
225*a1fd9364Slogwang NG_HOOK_SET_PRIVATE(hook, xxxp->channel + chan);
226*a1fd9364Slogwang xxxp->channel[chan].hook = hook;
227*a1fd9364Slogwang return (0);
228*a1fd9364Slogwang } else if (strcmp(name, NG_XXX_HOOK_DOWNSTREAM) == 0) {
229*a1fd9364Slogwang /* Example of simple predefined hooks. */
230*a1fd9364Slogwang /* do something specific to the downstream connection */
231*a1fd9364Slogwang xxxp->downstream_hook.hook = hook;
232*a1fd9364Slogwang NG_HOOK_SET_PRIVATE(hook, &xxxp->downstream_hook);
233*a1fd9364Slogwang } else if (strcmp(name, NG_XXX_HOOK_DEBUG) == 0) {
234*a1fd9364Slogwang /* do something specific to a debug connection */
235*a1fd9364Slogwang xxxp->debughook = hook;
236*a1fd9364Slogwang NG_HOOK_SET_PRIVATE(hook, NULL);
237*a1fd9364Slogwang } else
238*a1fd9364Slogwang return (EINVAL); /* not a hook we know about */
239*a1fd9364Slogwang return(0);
240*a1fd9364Slogwang }
241*a1fd9364Slogwang
242*a1fd9364Slogwang /*
243*a1fd9364Slogwang * Get a netgraph control message.
244*a1fd9364Slogwang * We actually receive a queue item that has a pointer to the message.
245*a1fd9364Slogwang * If we free the item, the message will be freed too, unless we remove
246*a1fd9364Slogwang * it from the item using NGI_GET_MSG();
247*a1fd9364Slogwang * The return address is also stored in the item, as an ng_ID_t,
248*a1fd9364Slogwang * accessible as NGI_RETADDR(item);
249*a1fd9364Slogwang * Check it is one we understand. If needed, send a response.
250*a1fd9364Slogwang * We could save the address for an async action later, but don't here.
251*a1fd9364Slogwang * Always free the message.
252*a1fd9364Slogwang * The response should be in a malloc'd region that the caller can 'free'.
253*a1fd9364Slogwang * A response is not required.
254*a1fd9364Slogwang * Theoretically you could respond defferently to old message types if
255*a1fd9364Slogwang * the cookie in the header didn't match what we consider to be current
256*a1fd9364Slogwang * (so that old userland programs could continue to work).
257*a1fd9364Slogwang */
258*a1fd9364Slogwang static int
ng_xxx_rcvmsg(node_p node,item_p item,hook_p lasthook)259*a1fd9364Slogwang ng_xxx_rcvmsg(node_p node, item_p item, hook_p lasthook)
260*a1fd9364Slogwang {
261*a1fd9364Slogwang const xxx_p xxxp = NG_NODE_PRIVATE(node);
262*a1fd9364Slogwang struct ng_mesg *resp = NULL;
263*a1fd9364Slogwang int error = 0;
264*a1fd9364Slogwang struct ng_mesg *msg;
265*a1fd9364Slogwang
266*a1fd9364Slogwang NGI_GET_MSG(item, msg);
267*a1fd9364Slogwang /* Deal with message according to cookie and command */
268*a1fd9364Slogwang switch (msg->header.typecookie) {
269*a1fd9364Slogwang case NGM_XXX_COOKIE:
270*a1fd9364Slogwang switch (msg->header.cmd) {
271*a1fd9364Slogwang case NGM_XXX_GET_STATUS:
272*a1fd9364Slogwang {
273*a1fd9364Slogwang struct ngxxxstat *stats;
274*a1fd9364Slogwang
275*a1fd9364Slogwang NG_MKRESPONSE(resp, msg, sizeof(*stats), M_NOWAIT);
276*a1fd9364Slogwang if (!resp) {
277*a1fd9364Slogwang error = ENOMEM;
278*a1fd9364Slogwang break;
279*a1fd9364Slogwang }
280*a1fd9364Slogwang stats = (struct ngxxxstat *) resp->data;
281*a1fd9364Slogwang stats->packets_in = xxxp->packets_in;
282*a1fd9364Slogwang stats->packets_out = xxxp->packets_out;
283*a1fd9364Slogwang break;
284*a1fd9364Slogwang }
285*a1fd9364Slogwang case NGM_XXX_SET_FLAG:
286*a1fd9364Slogwang if (msg->header.arglen != sizeof(u_int32_t)) {
287*a1fd9364Slogwang error = EINVAL;
288*a1fd9364Slogwang break;
289*a1fd9364Slogwang }
290*a1fd9364Slogwang xxxp->flags = *((u_int32_t *) msg->data);
291*a1fd9364Slogwang break;
292*a1fd9364Slogwang default:
293*a1fd9364Slogwang error = EINVAL; /* unknown command */
294*a1fd9364Slogwang break;
295*a1fd9364Slogwang }
296*a1fd9364Slogwang break;
297*a1fd9364Slogwang default:
298*a1fd9364Slogwang error = EINVAL; /* unknown cookie type */
299*a1fd9364Slogwang break;
300*a1fd9364Slogwang }
301*a1fd9364Slogwang
302*a1fd9364Slogwang /* Take care of synchronous response, if any */
303*a1fd9364Slogwang NG_RESPOND_MSG(error, node, item, resp);
304*a1fd9364Slogwang /* Free the message and return */
305*a1fd9364Slogwang NG_FREE_MSG(msg);
306*a1fd9364Slogwang return(error);
307*a1fd9364Slogwang }
308*a1fd9364Slogwang
309*a1fd9364Slogwang /*
310*a1fd9364Slogwang * Receive data, and do something with it.
311*a1fd9364Slogwang * Actually we receive a queue item which holds the data.
312*a1fd9364Slogwang * If we free the item it will also free the data unless we have
313*a1fd9364Slogwang * previously disassociated it using the NGI_GET_M() macro.
314*a1fd9364Slogwang * Possibly send it out on another link after processing.
315*a1fd9364Slogwang * Possibly do something different if it comes from different
316*a1fd9364Slogwang * hooks. The caller will never free m, so if we use up this data or
317*a1fd9364Slogwang * abort we must free it.
318*a1fd9364Slogwang *
319*a1fd9364Slogwang * If we want, we may decide to force this data to be queued and reprocessed
320*a1fd9364Slogwang * at the netgraph NETISR time.
321*a1fd9364Slogwang * We would do that by setting the HK_QUEUE flag on our hook. We would do that
322*a1fd9364Slogwang * in the connect() method.
323*a1fd9364Slogwang */
324*a1fd9364Slogwang static int
ng_xxx_rcvdata(hook_p hook,item_p item)325*a1fd9364Slogwang ng_xxx_rcvdata(hook_p hook, item_p item )
326*a1fd9364Slogwang {
327*a1fd9364Slogwang const xxx_p xxxp = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
328*a1fd9364Slogwang int chan = -2;
329*a1fd9364Slogwang int dlci = -2;
330*a1fd9364Slogwang int error;
331*a1fd9364Slogwang struct mbuf *m;
332*a1fd9364Slogwang
333*a1fd9364Slogwang NGI_GET_M(item, m);
334*a1fd9364Slogwang if (NG_HOOK_PRIVATE(hook)) {
335*a1fd9364Slogwang dlci = ((struct XXX_hookinfo *) NG_HOOK_PRIVATE(hook))->dlci;
336*a1fd9364Slogwang chan = ((struct XXX_hookinfo *) NG_HOOK_PRIVATE(hook))->channel;
337*a1fd9364Slogwang if (dlci != -1) {
338*a1fd9364Slogwang /* If received on a DLCI hook process for this
339*a1fd9364Slogwang * channel and pass it to the downstream module.
340*a1fd9364Slogwang * Normally one would add a multiplexing header at
341*a1fd9364Slogwang * the front here */
342*a1fd9364Slogwang /* M_PREPEND(....) ; */
343*a1fd9364Slogwang /* mtod(m, xxxxxx)->dlci = dlci; */
344*a1fd9364Slogwang NG_FWD_NEW_DATA(error, item,
345*a1fd9364Slogwang xxxp->downstream_hook.hook, m);
346*a1fd9364Slogwang xxxp->packets_out++;
347*a1fd9364Slogwang } else {
348*a1fd9364Slogwang /* data came from the multiplexed link */
349*a1fd9364Slogwang dlci = 1; /* get dlci from header */
350*a1fd9364Slogwang /* madjust(....) *//* chop off header */
351*a1fd9364Slogwang for (chan = 0; chan < XXX_NUM_DLCIS; chan++)
352*a1fd9364Slogwang if (xxxp->channel[chan].dlci == dlci)
353*a1fd9364Slogwang break;
354*a1fd9364Slogwang if (chan == XXX_NUM_DLCIS) {
355*a1fd9364Slogwang NG_FREE_ITEM(item);
356*a1fd9364Slogwang NG_FREE_M(m);
357*a1fd9364Slogwang return (ENETUNREACH);
358*a1fd9364Slogwang }
359*a1fd9364Slogwang /* If we were called at splnet, use the following:
360*a1fd9364Slogwang * NG_SEND_DATA_ONLY(error, otherhook, m); if this
361*a1fd9364Slogwang * node is running at some SPL other than SPLNET
362*a1fd9364Slogwang * then you should use instead: error =
363*a1fd9364Slogwang * ng_queueit(otherhook, m, NULL); m = NULL;
364*a1fd9364Slogwang * This queues the data using the standard NETISR
365*a1fd9364Slogwang * system and schedules the data to be picked
366*a1fd9364Slogwang * up again once the system has moved to SPLNET and
367*a1fd9364Slogwang * the processing of the data can continue. After
368*a1fd9364Slogwang * these are run 'm' should be considered
369*a1fd9364Slogwang * as invalid and NG_SEND_DATA actually zaps them. */
370*a1fd9364Slogwang NG_FWD_NEW_DATA(error, item,
371*a1fd9364Slogwang xxxp->channel[chan].hook, m);
372*a1fd9364Slogwang xxxp->packets_in++;
373*a1fd9364Slogwang }
374*a1fd9364Slogwang } else {
375*a1fd9364Slogwang /* It's the debug hook, throw it away.. */
376*a1fd9364Slogwang if (hook == xxxp->downstream_hook.hook) {
377*a1fd9364Slogwang NG_FREE_ITEM(item);
378*a1fd9364Slogwang NG_FREE_M(m);
379*a1fd9364Slogwang }
380*a1fd9364Slogwang }
381*a1fd9364Slogwang return 0;
382*a1fd9364Slogwang }
383*a1fd9364Slogwang
384*a1fd9364Slogwang #if 0
385*a1fd9364Slogwang /*
386*a1fd9364Slogwang * If this were a device node, the data may have been received in response
387*a1fd9364Slogwang * to some interrupt.
388*a1fd9364Slogwang * in which case it would probably look as follows:
389*a1fd9364Slogwang */
390*a1fd9364Slogwang devintr()
391*a1fd9364Slogwang {
392*a1fd9364Slogwang int error;
393*a1fd9364Slogwang
394*a1fd9364Slogwang /* get packet from device and send on */
395*a1fd9364Slogwang m = MGET(blah blah)
396*a1fd9364Slogwang
397*a1fd9364Slogwang NG_SEND_DATA_ONLY(error, xxxp->upstream_hook.hook, m);
398*a1fd9364Slogwang /* see note above in xxx_rcvdata() */
399*a1fd9364Slogwang /* and ng_xxx_connect() */
400*a1fd9364Slogwang }
401*a1fd9364Slogwang
402*a1fd9364Slogwang #endif /* 0 */
403*a1fd9364Slogwang
404*a1fd9364Slogwang /*
405*a1fd9364Slogwang * Do local shutdown processing..
406*a1fd9364Slogwang * All our links and the name have already been removed.
407*a1fd9364Slogwang * If we are a persistent device, we might refuse to go away.
408*a1fd9364Slogwang * In the case of a persistent node we signal the framework that we
409*a1fd9364Slogwang * are still in business by clearing the NGF_INVALID bit. However
410*a1fd9364Slogwang * If we find the NGF_REALLY_DIE bit set, this means that
411*a1fd9364Slogwang * we REALLY need to die (e.g. hardware removed).
412*a1fd9364Slogwang * This would have been set using the NG_NODE_REALLY_DIE(node)
413*a1fd9364Slogwang * macro in some device dependent function (not shown here) before
414*a1fd9364Slogwang * calling ng_rmnode_self().
415*a1fd9364Slogwang */
416*a1fd9364Slogwang static int
ng_xxx_shutdown(node_p node)417*a1fd9364Slogwang ng_xxx_shutdown(node_p node)
418*a1fd9364Slogwang {
419*a1fd9364Slogwang const xxx_p privdata = NG_NODE_PRIVATE(node);
420*a1fd9364Slogwang
421*a1fd9364Slogwang #ifndef PERSISTANT_NODE
422*a1fd9364Slogwang NG_NODE_SET_PRIVATE(node, NULL);
423*a1fd9364Slogwang NG_NODE_UNREF(node);
424*a1fd9364Slogwang free(privdata, M_NETGRAPH);
425*a1fd9364Slogwang #else
426*a1fd9364Slogwang if (node->nd_flags & NGF_REALLY_DIE) {
427*a1fd9364Slogwang /*
428*a1fd9364Slogwang * WE came here because the widget card is being unloaded,
429*a1fd9364Slogwang * so stop being persistent.
430*a1fd9364Slogwang * Actually undo all the things we did on creation.
431*a1fd9364Slogwang */
432*a1fd9364Slogwang NG_NODE_SET_PRIVATE(node, NULL);
433*a1fd9364Slogwang NG_NODE_UNREF(privdata->node);
434*a1fd9364Slogwang free(privdata, M_NETGRAPH);
435*a1fd9364Slogwang return (0);
436*a1fd9364Slogwang }
437*a1fd9364Slogwang NG_NODE_REVIVE(node); /* tell ng_rmnode() we will persist */
438*a1fd9364Slogwang #endif /* PERSISTANT_NODE */
439*a1fd9364Slogwang return (0);
440*a1fd9364Slogwang }
441*a1fd9364Slogwang
442*a1fd9364Slogwang /*
443*a1fd9364Slogwang * This is called once we've already connected a new hook to the other node.
444*a1fd9364Slogwang * It gives us a chance to balk at the last minute.
445*a1fd9364Slogwang */
446*a1fd9364Slogwang static int
ng_xxx_connect(hook_p hook)447*a1fd9364Slogwang ng_xxx_connect(hook_p hook)
448*a1fd9364Slogwang {
449*a1fd9364Slogwang #if 0
450*a1fd9364Slogwang /*
451*a1fd9364Slogwang * If we were a driver running at other than splnet then
452*a1fd9364Slogwang * we should set the QUEUE bit on the edge so that we
453*a1fd9364Slogwang * will deliver by queing.
454*a1fd9364Slogwang */
455*a1fd9364Slogwang if /*it is the upstream hook */
456*a1fd9364Slogwang NG_HOOK_FORCE_QUEUE(NG_HOOK_PEER(hook));
457*a1fd9364Slogwang #endif
458*a1fd9364Slogwang #if 0
459*a1fd9364Slogwang /*
460*a1fd9364Slogwang * If for some reason we want incoming date to be queued
461*a1fd9364Slogwang * by the NETISR system and delivered later we can set the same bit on
462*a1fd9364Slogwang * OUR hook. (maybe to allow unwinding of the stack)
463*a1fd9364Slogwang */
464*a1fd9364Slogwang
465*a1fd9364Slogwang if (NG_HOOK_PRIVATE(hook)) {
466*a1fd9364Slogwang int dlci;
467*a1fd9364Slogwang /*
468*a1fd9364Slogwang * If it's dlci 1023, requeue it so that it's handled
469*a1fd9364Slogwang * at a lower priority. This is how a node decides to
470*a1fd9364Slogwang * defer a data message.
471*a1fd9364Slogwang */
472*a1fd9364Slogwang dlci = ((struct XXX_hookinfo *) NG_HOOK_PRIVATE(hook))->dlci;
473*a1fd9364Slogwang if (dlci == 1023) {
474*a1fd9364Slogwang NG_HOOK_FORCE_QUEUE(hook);
475*a1fd9364Slogwang }
476*a1fd9364Slogwang #endif
477*a1fd9364Slogwang /* otherwise be really amiable and just say "YUP that's OK by me! " */
478*a1fd9364Slogwang return (0);
479*a1fd9364Slogwang }
480*a1fd9364Slogwang
481*a1fd9364Slogwang /*
482*a1fd9364Slogwang * Hook disconnection
483*a1fd9364Slogwang *
484*a1fd9364Slogwang * For this type, removal of the last link destroys the node
485*a1fd9364Slogwang */
486*a1fd9364Slogwang static int
487*a1fd9364Slogwang ng_xxx_disconnect(hook_p hook)
488*a1fd9364Slogwang {
489*a1fd9364Slogwang if (NG_HOOK_PRIVATE(hook))
490*a1fd9364Slogwang ((struct XXX_hookinfo *) (NG_HOOK_PRIVATE(hook)))->hook = NULL;
491*a1fd9364Slogwang if ((NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0)
492*a1fd9364Slogwang && (NG_NODE_IS_VALID(NG_HOOK_NODE(hook)))) /* already shutting down? */
493*a1fd9364Slogwang ng_rmnode_self(NG_HOOK_NODE(hook));
494*a1fd9364Slogwang return (0);
495*a1fd9364Slogwang }
496