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