2 * Copyright (c) 2002 Mark Santcroos <marks@ripe.net>
3 * Copyright (c) 2004 Gleb Smirnoff <glebius@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 * Netgraph "device" node
27 * This node presents a /dev/ngd%d device that interfaces to an other
35 #define DBG do { printf("ng_device: %s\n", __func__ ); } while (0)
37 #define DBG do {} while (0)
40 #include <sys/param.h>
42 #include <sys/ioccom.h>
43 #include <sys/kernel.h>
44 #include <sys/malloc.h>
47 #include <sys/queue.h>
48 #include <sys/socket.h>
49 #include <sys/systm.h>
51 #include <sys/vnode.h>
54 #include <net/if_var.h>
55 #include <netinet/in.h>
56 #include <netinet/in_systm.h>
57 #include <netinet/ip.h>
59 #include <netgraph/ng_message.h>
60 #include <netgraph/netgraph.h>
61 #include <netgraph/ng_device.h>
63 #define ERROUT(x) do { error = (x); goto done; } while (0)
65 /* Netgraph methods */
66 static ng_constructor_t ng_device_constructor;
67 static ng_rcvmsg_t ng_device_rcvmsg;
68 static ng_shutdown_t ng_device_shutdown;
69 static ng_newhook_t ng_device_newhook;
70 static ng_rcvdata_t ng_device_rcvdata;
71 static ng_disconnect_t ng_device_disconnect;
74 static struct ng_type ngd_typestruct = {
75 .version = NG_ABI_VERSION,
76 .name = NG_DEVICE_NODE_TYPE,
77 .constructor = ng_device_constructor,
78 .rcvmsg = ng_device_rcvmsg,
79 .shutdown = ng_device_shutdown,
80 .newhook = ng_device_newhook,
81 .rcvdata = ng_device_rcvdata,
82 .disconnect = ng_device_disconnect,
84 NETGRAPH_INIT(device, &ngd_typestruct);
89 SLIST_ENTRY(ngd_private) links;
96 #define NGDF_OPEN 0x0001
97 #define NGDF_RWAIT 0x0002
99 typedef struct ngd_private *priv_p;
101 /* List of all active nodes and mutex to protect it */
102 static SLIST_HEAD(, ngd_private) ngd_nodes = SLIST_HEAD_INITIALIZER(ngd_nodes);
103 static struct mtx ng_device_mtx;
104 MTX_SYSINIT(ng_device, &ng_device_mtx, "ng_device", MTX_DEF);
106 /* Maximum number of NGD devices */
107 #define MAX_NGD 25 /* should be more than enough for now */
109 static d_close_t ngdclose;
110 static d_open_t ngdopen;
111 static d_read_t ngdread;
112 static d_write_t ngdwrite;
114 static d_ioctl_t ngdioctl;
116 static d_poll_t ngdpoll;
118 static struct cdevsw ngd_cdevsw = {
119 .d_version = D_VERSION,
128 .d_name = NG_DEVICE_DEVNAME,
131 /* Helper functions */
132 static int get_free_unit(void);
134 /******************************************************************************
136 ******************************************************************************/
142 ng_device_constructor(node_p node)
148 MALLOC(priv, priv_p, sizeof(*priv), M_NETGRAPH, M_NOWAIT | M_ZERO);
152 mtx_init(&priv->ngd_mtx, "ng_device", NULL, MTX_DEF);
153 mtx_lock(&priv->ngd_mtx);
155 mtx_lock(&ng_device_mtx);
157 priv->unit = get_free_unit();
159 printf("%s: No free unit found by get_free_unit(), "
160 "increase MAX_NGD\n",__func__);
161 mtx_unlock(&ng_device_mtx);
162 mtx_destroy(&priv->ngd_mtx);
163 FREE(priv, M_NETGRAPH);
167 priv->ngddev = make_dev(&ngd_cdevsw, unit2minor(priv->unit), UID_ROOT,
168 GID_WHEEL, 0600, NG_DEVICE_DEVNAME "%d", priv->unit);
169 if(priv->ngddev == NULL) {
170 printf("%s(): make_dev() failed\n",__func__);
171 mtx_unlock(&ng_device_mtx);
172 mtx_destroy(&priv->ngd_mtx);
173 FREE(priv, M_NETGRAPH);
177 SLIST_INSERT_HEAD(&ngd_nodes, priv, links);
179 mtx_unlock(&ng_device_mtx);
181 mtx_init(&priv->readq.ifq_mtx, "ng_device queue", NULL, MTX_DEF);
182 IFQ_SET_MAXLEN(&priv->readq, ifqmaxlen);
184 /* Link everything together */
185 NG_NODE_SET_PRIVATE(node, priv);
187 priv->ngddev->si_drv1 = priv;
189 mtx_unlock(&priv->ngd_mtx);
195 * Process control message.
199 ng_device_rcvmsg(node_p node, item_p item, hook_p lasthook)
201 const priv_p priv = NG_NODE_PRIVATE(node);
203 struct ng_mesg *resp = NULL;
206 NGI_GET_MSG(item, msg);
208 if (msg->header.typecookie == NGM_DEVICE_COOKIE) {
209 switch (msg->header.cmd) {
210 case NGM_DEVICE_GET_DEVNAME:
211 /* XXX: Fix when NGD_MAX us bigger */
212 NG_MKRESPONSE(resp, msg,
213 strlen(NG_DEVICE_DEVNAME) + 3, M_NOWAIT);
218 strlcpy((char *)resp->data, priv->ngddev->si_name,
219 strlen(priv->ngddev->si_name) + 1);
230 NG_RESPOND_MSG(error, node, item, resp);
236 * Accept incoming hook. We support only one hook per node.
239 ng_device_newhook(node_p node, hook_p hook, const char *name)
241 priv_p priv = NG_NODE_PRIVATE(node);
245 /* We have only one hook per node */
246 if (priv->hook != NULL)
255 * Receive data from hook, write it to device.
258 ng_device_rcvdata(hook_p hook, item_p item)
260 priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
268 IF_LOCK(&priv->readq);
269 if (_IF_QFULL(&priv->readq)) {
270 _IF_DROP(&priv->readq);
271 IF_UNLOCK(&priv->readq);
276 _IF_ENQUEUE(&priv->readq, m);
277 IF_UNLOCK(&priv->readq);
278 mtx_lock(&priv->ngd_mtx);
279 if (priv->flags & NGDF_RWAIT) {
280 priv->flags &= ~NGDF_RWAIT;
283 mtx_unlock(&priv->ngd_mtx);
289 * Removal of the hook destroys the node.
292 ng_device_disconnect(hook_p hook)
294 priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
298 destroy_dev(priv->ngddev);
299 mtx_destroy(&priv->ngd_mtx);
301 mtx_lock(&ng_device_mtx);
302 SLIST_REMOVE(&ngd_nodes, priv, ngd_private, links);
303 mtx_unlock(&ng_device_mtx);
305 IF_DRAIN(&priv->readq);
306 mtx_destroy(&(priv)->readq.ifq_mtx);
308 FREE(priv, M_NETGRAPH);
310 ng_rmnode_self(NG_HOOK_NODE(hook));
316 * Node shutdown. Everything is already done in disconnect method.
319 ng_device_shutdown(node_p node)
325 /******************************************************************************
327 ******************************************************************************/
330 * the device is opened
333 ngdopen(struct cdev *dev, int flag, int mode, struct thread *td)
335 priv_p priv = (priv_p )dev->si_drv1;
339 mtx_lock(&priv->ngd_mtx);
340 priv->flags |= NGDF_OPEN;
341 mtx_unlock(&priv->ngd_mtx);
347 * the device is closed
350 ngdclose(struct cdev *dev, int flag, int mode, struct thread *td)
352 priv_p priv = (priv_p )dev->si_drv1;
355 mtx_lock(&priv->ngd_mtx);
356 priv->flags &= ~NGDF_OPEN;
357 mtx_unlock(&priv->ngd_mtx);
363 * The ioctl is transformed into netgraph control message.
364 * We do not process them, yet.
369 * they are translated into netgraph messages and passed on
373 ngdioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
375 struct ngd_softc *sc = &ngd_softc;
376 struct ngd_connection * connection = NULL;
377 struct ngd_connection * tmp;
380 struct ngd_param_s * datap;
384 SLIST_FOREACH(tmp,&sc->head,links) {
385 if(tmp->ngddev == dev) {
389 if(connection == NULL) {
390 printf("%s(): connection is still NULL, no dev found\n",__func__);
394 NG_MKMESSAGE(msg, NGM_DEVICE_COOKIE, cmd, sizeof(struct ngd_param_s),
397 printf("%s(): msg == NULL\n",__func__);
401 /* pass the ioctl data into the ->data area */
402 datap = (struct ngd_param_s *)msg->data;
405 NG_SEND_MSG_HOOK(error, sc->node, msg, connection->active_hook, 0);
407 printf("%s(): NG_SEND_MSG_HOOK error: %d\n",__func__,error);
416 * This function is called when a read(2) is done to our device.
417 * We process one mbuf from queue.
420 ngdread(struct cdev *dev, struct uio *uio, int flag)
422 priv_p priv = (priv_p )dev->si_drv1;
430 IF_DEQUEUE(&priv->readq, m);
432 if (flag & IO_NDELAY)
433 return (EWOULDBLOCK);
434 mtx_lock(&priv->ngd_mtx);
435 priv->flags |= NGDF_RWAIT;
436 if ((error = msleep(priv, &priv->ngd_mtx,
437 PDROP | PCATCH | (PZERO + 1),
443 while (m && uio->uio_resid > 0 && error == 0) {
444 len = MIN(uio->uio_resid, m->m_len);
446 error = uiomove(mtod(m, void *), len, uio);
458 * This function is called when our device is written to.
459 * We read the data from userland into mbuf chain and pass it to the remote hook.
463 ngdwrite(struct cdev *dev, struct uio *uio, int flag)
465 priv_p priv = (priv_p )dev->si_drv1;
471 if (uio->uio_resid == 0)
474 if (uio->uio_resid < 0 || uio->uio_resid > IP_MAXPACKET)
477 if ((m = m_uiotombuf(uio, M_DONTWAIT, 0)) == NULL)
480 NG_SEND_DATA_ONLY(error, priv->hook, m);
486 * we are being polled/selected
487 * check if there is data available for read
490 ngdpoll(struct cdev *dev, int events, struct thread *td)
492 priv_p priv = (priv_p )dev->si_drv1;
495 if (events & (POLLIN | POLLRDNORM) &&
496 !IFQ_IS_EMPTY(&priv->readq))
497 revents |= events & (POLLIN | POLLRDNORM);
502 /******************************************************************************
504 ******************************************************************************/
509 struct ngd_private *priv = NULL;
515 mtx_assert(&ng_device_mtx, MA_OWNED);
517 /* When there is no list yet, the first device unit is always 0. */
518 if SLIST_EMPTY(&ngd_nodes)
521 /* Just do a brute force loop to find the first free unit that is
522 * smaller than MAX_NGD.
523 * Set MAX_NGD to a large value, doesn't impact performance.
525 for(n = 0; n<MAX_NGD && unit == -1; n++) {
526 SLIST_FOREACH(priv, &ngd_nodes, links) {
528 if(priv->unit == n) {