2 * Copyright (c) 2002 Mark Santcroos <marks@ripe.net>
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * 3. The name of the author may not be used to endorse or promote products
13 * derived from this software without specific prior written permission.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 * Netgraph "device" node
28 * This node presents a /dev/ngd%d device that interfaces to an other
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/kernel.h>
40 #include <sys/queue.h>
41 #include <sys/malloc.h>
44 #include <sys/ioccom.h>
46 #include <netgraph/ng_message.h>
47 #include <netgraph/netgraph.h>
49 #include "ng_device.h"
51 /* turn this on for verbose messages */
54 /* Netgraph methods */
55 static ng_constructor_t ng_device_cons;
56 static ng_rcvmsg_t ng_device_rcvmsg;
57 static ng_newhook_t ng_device_newhook;
58 static ng_connect_t ng_device_connect;
59 static ng_rcvdata_t ng_device_rcvdata;
60 static ng_disconnect_t ng_device_disconnect;
61 static int ng_device_mod_event(module_t mod, int event, void *data);
63 static int ng_device_init(void);
64 static int get_free_unit(void);
67 static struct ng_type typestruct = {
68 NG_ABI_VERSION, /* version */
69 NG_DEVICE_NODE_TYPE, /* name */
70 ng_device_mod_event, /* modevent */
71 ng_device_cons, /* constructor */
72 ng_device_rcvmsg, /* receive msg */
74 ng_device_newhook, /* newhook */
76 ng_device_connect, /* connect */
77 ng_device_rcvdata, /* receive data */
78 ng_device_disconnect, /* disconnect */
81 NETGRAPH_INIT(device, &typestruct);
84 struct ngd_connection {
85 SLIST_ENTRY(ngd_connection) links;
88 struct ng_hook *active_hook;
96 SLIST_HEAD(, ngd_connection) head;
99 char nodename[NG_NODELEN + 1];
102 /* helper definition */
103 #define MIN(a, b) ((a) < (b) ? (a) : (b))
105 /* the per connection receiving queue maximum */
106 #define NGD_QUEUE_SIZE (1024*10)
108 /* Maximum number of NGD devices */
109 #define MAX_NGD 25 /* should be more than enough for now */
111 static d_close_t ngdclose;
112 static d_open_t ngdopen;
113 static d_read_t ngdread;
114 static d_write_t ngdwrite;
115 static d_ioctl_t ngdioctl;
116 static d_poll_t ngdpoll;
118 #define NGD_CDEV_MAJOR 20
119 static struct cdevsw ngd_cdevsw = {
121 /* close */ ngdclose,
123 /* write */ ngdwrite,
124 /* ioctl */ ngdioctl,
127 /* strategy */ nostrategy,
129 /* maj */ NGD_CDEV_MAJOR,
136 * this holds all the stuff that should be done at load time
139 ng_device_mod_event(module_t mod, int event, void *data)
144 printf("%s()\n",__func__);
145 #endif /* NGD_DEBUG */
154 /* XXX do we need to do something specific ? */
155 /* ng_device_breakdown */
170 struct ngd_softc *sc = &ngd_softc;
173 printf("%s()\n",__func__);
174 #endif /* NGD_DEBUG */
176 SLIST_INIT(&sc->head);
178 if (ng_make_node_common(&typestruct, &sc->node) != 0) {
179 printf("%s(): ng_make_node_common failed\n",__func__);
182 sprintf(sc->nodename, "%s", NG_DEVICE_NODE_TYPE);
183 if (ng_name_node(sc->node, sc->nodename)) {
184 NG_NODE_UNREF(sc->node); /* make it go away again */
185 printf("%s(): ng_name_node failed\n",__func__);
188 NG_NODE_SET_PRIVATE(sc->node, sc);
194 * don't allow to be created, only the device can do that
197 ng_device_cons(node_p node)
201 printf("%s()\n",__func__);
202 #endif /* NGD_DEBUG */
208 * Receive control message. We just bounce it back as a reply.
211 ng_device_rcvmsg(node_p node, item_p item, hook_p lasthook)
213 struct ngd_softc *sc = &ngd_softc;
216 struct ngd_connection * connection = NULL;
217 struct ngd_connection *tmp = NULL;
220 printf("%s()\n",__func__);
221 #endif /* NGD_DEBUG */
223 NGI_GET_MSG(item, msg);
225 SLIST_FOREACH(tmp,&sc->head,links) {
226 if(tmp->active_hook == lasthook) {
230 if(connection == NULL) {
231 printf("%s(): connection is still NULL, no hook found\n",__func__);
241 struct ngd_connection *tmp = NULL;
242 struct ngd_softc *sc = &ngd_softc;
247 printf("%s()\n",__func__);
248 #endif /* NGD_DEBUG */
250 /* When there is no list yet, the first device unit is always 0. */
251 if SLIST_EMPTY(&sc->head) {
256 /* Just do a brute force loop to find the first free unit that is
257 * smaller than MAX_NGD.
258 * Set MAX_NGD to a large value, doesn't impact performance.
260 for(n = 0;n<MAX_NGD && unit == -1;n++) {
261 SLIST_FOREACH(tmp,&sc->head,links) {
278 ng_device_newhook(node_p node, hook_p hook, const char *name)
280 struct ngd_softc *sc = &ngd_softc;
281 struct ngd_connection * new_connection = NULL;
284 printf("%s()\n",__func__);
285 #endif /* NGD_DEBUG */
287 new_connection = malloc(sizeof(struct ngd_connection), M_DEVBUF, M_NOWAIT);
288 if(new_connection == NULL) {
289 printf("%s(): ERROR: new_connection == NULL\n",__func__);
293 new_connection->unit = get_free_unit();
294 if(new_connection->unit<0) {
295 printf("%s: No free unit found by get_free_unit(), "
296 "increas MAX_NGD\n",__func__);
299 new_connection->ngddev = make_dev(&ngd_cdevsw, new_connection->unit, 0, 0,0600,"ngd%d",new_connection->unit);
300 if(new_connection->ngddev == NULL) {
301 printf("%s(): make_dev failed\n",__func__);
305 new_connection->readq = malloc(sizeof(char)*NGD_QUEUE_SIZE, M_DEVBUF, M_NOWAIT | M_ZERO);
306 if(new_connection->readq == NULL) {
307 printf("%s(): readq malloc failed\n",__func__);
311 /* point to begin of buffer */
312 new_connection->loc = 0;
313 new_connection->active_hook = hook;
315 SLIST_INSERT_HEAD(&sc->head, new_connection, links);
321 * we gave ok to a new hook
325 ng_device_connect(hook_p hook)
329 printf("%s()\n",__func__);
330 #endif /* NGD_DEBUG */
337 * Receive data from hook
340 ng_device_rcvdata(hook_p hook, item_p item)
343 struct ngd_softc *sc = &ngd_softc;
344 struct ngd_connection * connection = NULL;
345 struct ngd_connection * tmp;
349 printf("%s()\n",__func__);
350 #endif /* NGD_DEBUG */
352 SLIST_FOREACH(tmp,&sc->head,links) {
353 if(tmp->active_hook == hook) {
357 if(connection == NULL) {
358 printf("%s(): connection is still NULL, no hook found\n",__func__);
365 m = m_pullup(m,m->m_len);
367 printf("%s(): ERROR: m_pullup failed\n",__func__);
371 buffer = malloc(sizeof(char)*m->m_len, M_DEVBUF, M_NOWAIT | M_ZERO);
373 printf("%s(): ERROR: buffer malloc failed\n",__func__);
377 buffer = mtod(m,char *);
379 if( (connection->loc+m->m_len) < NGD_QUEUE_SIZE) {
380 memcpy(connection->readq+connection->loc, buffer, m->m_len);
381 connection->loc += m->m_len;
383 printf("%s(): queue full, first read out a bit\n",__func__);
385 free(buffer,M_DEVBUF);
391 * Removal of the last link destroys the node
394 ng_device_disconnect(hook_p hook)
396 struct ngd_softc *sc = &ngd_softc;
397 struct ngd_connection * connection = NULL;
398 struct ngd_connection * tmp;
401 printf("%s()\n",__func__);
402 #endif /* NGD_DEBUG */
404 SLIST_FOREACH(tmp,&sc->head,links) {
405 if(tmp->active_hook == hook) {
409 if(connection == NULL) {
410 printf("%s(): connection is still NULL, no hook found\n",__func__);
414 free(connection->readq,M_DEVBUF);
416 destroy_dev(connection->ngddev);
418 SLIST_REMOVE(&sc->head,connection,ngd_connection,links);
423 * the device is opened
426 ngdopen(dev_t dev, int flag, int mode, struct thread *td)
430 printf("%s()\n",__func__);
431 #endif /* NGD_DEBUG */
437 * the device is closed
440 ngdclose(dev_t dev, int flag, int mode, struct thread *td)
444 printf("%s()\n",__func__);
454 * they are translated into netgraph messages and passed on
458 ngdioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
460 struct ngd_softc *sc = &ngd_softc;
461 struct ngd_connection * connection = NULL;
462 struct ngd_connection * tmp;
465 struct ngd_param_s * datap;
468 printf("%s()\n",__func__);
469 #endif /* NGD_DEBUG */
471 SLIST_FOREACH(tmp,&sc->head,links) {
472 if(tmp->ngddev == dev) {
476 if(connection == NULL) {
477 printf("%s(): connection is still NULL, no dev found\n",__func__);
481 /* NG_MKMESSAGE(msg, cookie, cmdid, len, how) */
482 NG_MKMESSAGE(msg, NGM_DEVICE_COOKIE, cmd, sizeof(struct ngd_param_s),
485 printf("%s(): msg == NULL\n",__func__);
489 /* pass the ioctl data into the ->data area */
490 datap = (struct ngd_param_s *)msg->data;
493 /* NG_SEND_MSG_HOOK(error, here, msg, hook, retaddr) */
494 NG_SEND_MSG_HOOK(error, sc->node, msg, connection->active_hook, NULL);
496 printf("%s(): NG_SEND_MSG_HOOK error: %d\n",__func__,error);
505 * This function is called when a read(2) is done to our device.
506 * We pass the data available in kernelspace on into userland using
510 ngdread(dev_t dev, struct uio *uio, int flag)
513 char buffer[uio->uio_resid+1];
514 struct ngd_softc *sc = &ngd_softc;
515 struct ngd_connection * connection = NULL;
516 struct ngd_connection * tmp;
519 printf("%s()\n",__func__);
520 #endif /* NGD_DEBUG */
522 SLIST_FOREACH(tmp,&sc->head,links) {
523 if(tmp->ngddev == dev) {
527 if(connection == NULL) {
528 printf("%s(): connection is still NULL, no dev found\n",__func__);
532 while ( ( uio->uio_resid > 0 ) && ( connection->loc > 0 ) ) {
533 amnt = MIN(uio->uio_resid,connection->loc);
535 memcpy(buffer,connection->readq, amnt);
536 memcpy(connection->readq, connection->readq+amnt,
537 connection->loc-amnt);
538 connection->loc -= amnt;
540 ret = uiomove((caddr_t)buffer, amnt, uio);
548 printf("%s(): uiomove returns error %d\n",__func__,ret);
549 /* do error cleanup here */
555 * This function is called when our device is written to.
556 * We read the data from userland into our local buffer and pass it on
557 * into the remote hook.
561 ngdwrite(dev_t dev, struct uio *uio, int flag)
566 char buffer[uio->uio_resid];
567 int len = uio->uio_resid;
568 struct ngd_softc *sc =& ngd_softc;
569 struct ngd_connection * connection = NULL;
570 struct ngd_connection * tmp;
573 printf("%s()\n",__func__);
574 #endif /* NGD_DEBUG */
576 SLIST_FOREACH(tmp,&sc->head,links) {
577 if(tmp->ngddev == dev) {
582 if(connection == NULL) {
583 printf("%s(): connection is still NULL, no dev found\n",__func__);
588 if ((ret = uiomove((caddr_t)buffer, len, uio)) != 0)
591 printf("%s(): len <= 0 : is this supposed to happen?!\n",__func__);
593 m = m_devget(buffer,len,0,NULL,NULL);
595 NG_SEND_DATA_ONLY(error,connection->active_hook,m);
600 /* do error cleanup here */
601 printf("%s(): uiomove returned err: %d\n",__func__,ret);
607 * we are being polled/selected
608 * check if there is data available for read
611 ngdpoll(dev_t dev, int events, struct thread *td)
614 struct ngd_softc *sc = &ngd_softc;
615 struct ngd_connection * connection = NULL;
616 struct ngd_connection * tmp;
619 if (events & (POLLIN | POLLRDNORM)) {
620 /* get the connection we have to know the loc from */
621 SLIST_FOREACH(tmp,&sc->head,links) {
622 if(tmp->ngddev == dev) {
626 if(connection == NULL) {
627 printf("%s(): ERROR: connection is still NULL,"
628 "no dev found\n",__func__);
632 if (connection->loc > 0)
633 revents |= events & (POLLIN | POLLRDNORM);