2 * Copyright (c) 2002 Mark Santcroos <marks@ripe.net>
3 * Copyright (c) 2004-2005 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 int ng_device_mod_event(module_t, int, void *);
67 static ng_constructor_t ng_device_constructor;
68 static ng_rcvmsg_t ng_device_rcvmsg;
69 static ng_shutdown_t ng_device_shutdown;
70 static ng_newhook_t ng_device_newhook;
71 static ng_rcvdata_t ng_device_rcvdata;
72 static ng_disconnect_t ng_device_disconnect;
75 static struct ng_type ngd_typestruct = {
76 .version = NG_ABI_VERSION,
77 .name = NG_DEVICE_NODE_TYPE,
78 .mod_event = ng_device_mod_event,
79 .constructor = ng_device_constructor,
80 .rcvmsg = ng_device_rcvmsg,
81 .shutdown = ng_device_shutdown,
82 .newhook = ng_device_newhook,
83 .rcvdata = ng_device_rcvdata,
84 .disconnect = ng_device_disconnect,
86 NETGRAPH_INIT(device, &ngd_typestruct);
97 #define NGDF_OPEN 0x0001
98 #define NGDF_RWAIT 0x0002
100 typedef struct ngd_private *priv_p;
102 /* unit number allocator entity */
103 static struct unrhdr *ngd_unit;
105 /* Maximum number of NGD devices */
108 static d_close_t ngdclose;
109 static d_open_t ngdopen;
110 static d_read_t ngdread;
111 static d_write_t ngdwrite;
113 static d_ioctl_t ngdioctl;
115 static d_poll_t ngdpoll;
117 static struct cdevsw ngd_cdevsw = {
118 .d_version = D_VERSION,
127 .d_name = NG_DEVICE_DEVNAME,
130 /******************************************************************************
132 ******************************************************************************/
135 * Handle loading and unloading for this node type.
138 ng_device_mod_event(module_t mod, int event, void *data)
144 ngd_unit = new_unrhdr(0, MAX_NGD, NULL);
147 delete_unrhdr(ngd_unit);
160 ng_device_constructor(node_p node)
166 priv = malloc(sizeof(*priv), M_NETGRAPH, M_WAITOK | M_ZERO);
168 /* Allocate unit number */
169 priv->unit = alloc_unr(ngd_unit);
171 /* Initialize mutexes and queue */
172 mtx_init(&priv->ngd_mtx, "ng_device", NULL, MTX_DEF);
173 mtx_init(&priv->readq.ifq_mtx, "ng_device queue", NULL, MTX_DEF);
174 IFQ_SET_MAXLEN(&priv->readq, ifqmaxlen);
176 /* Link everything together */
177 NG_NODE_SET_PRIVATE(node, priv);
180 priv->ngddev = make_dev(&ngd_cdevsw, priv->unit, UID_ROOT,
181 GID_WHEEL, 0600, NG_DEVICE_DEVNAME "%d", priv->unit);
182 if(priv->ngddev == NULL) {
183 printf("%s(): make_dev() failed\n",__func__);
184 mtx_destroy(&priv->ngd_mtx);
185 mtx_destroy(&priv->readq.ifq_mtx);
186 free_unr(ngd_unit, priv->unit);
187 free(priv, M_NETGRAPH);
190 /* XXX: race here? */
191 priv->ngddev->si_drv1 = priv;
197 * Process control message.
201 ng_device_rcvmsg(node_p node, item_p item, hook_p lasthook)
203 const priv_p priv = NG_NODE_PRIVATE(node);
205 struct ng_mesg *resp = NULL;
209 NGI_GET_MSG(item, msg);
211 if (msg->header.typecookie == NGM_DEVICE_COOKIE) {
212 switch (msg->header.cmd) {
213 case NGM_DEVICE_GET_DEVNAME:
214 /* XXX: Fix when MAX_NGD us bigger */
215 NG_MKRESPONSE(resp, msg,
216 strlen(NG_DEVICE_DEVNAME) + 4, M_NOWAIT);
221 dn = devtoname(priv->ngddev);
222 strlcpy((char *)resp->data, dn, strlen(dn) + 1);
233 NG_RESPOND_MSG(error, node, item, resp);
239 * Accept incoming hook. We support only one hook per node.
242 ng_device_newhook(node_p node, hook_p hook, const char *name)
244 priv_p priv = NG_NODE_PRIVATE(node);
248 /* We have only one hook per node */
249 if (priv->hook != NULL)
258 * Receive data from hook, write it to device.
261 ng_device_rcvdata(hook_p hook, item_p item)
263 priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
271 IF_LOCK(&priv->readq);
272 if (_IF_QFULL(&priv->readq)) {
273 IF_UNLOCK(&priv->readq);
278 _IF_ENQUEUE(&priv->readq, m);
279 IF_UNLOCK(&priv->readq);
280 mtx_lock(&priv->ngd_mtx);
281 if (priv->flags & NGDF_RWAIT) {
282 priv->flags &= ~NGDF_RWAIT;
285 mtx_unlock(&priv->ngd_mtx);
291 * Removal of the hook destroys the node.
294 ng_device_disconnect(hook_p hook)
296 priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
300 destroy_dev(priv->ngddev);
301 mtx_destroy(&priv->ngd_mtx);
303 IF_DRAIN(&priv->readq);
304 mtx_destroy(&(priv)->readq.ifq_mtx);
306 free_unr(ngd_unit, priv->unit);
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 NG_MKMESSAGE(msg, NGM_DEVICE_COOKIE, cmd, sizeof(struct ngd_param_s),
387 printf("%s(): msg == NULL\n",__func__);
391 /* pass the ioctl data into the ->data area */
392 datap = (struct ngd_param_s *)msg->data;
395 NG_SEND_MSG_HOOK(error, sc->node, msg, connection->active_hook, 0);
397 printf("%s(): NG_SEND_MSG_HOOK error: %d\n",__func__,error);
406 * This function is called when a read(2) is done to our device.
407 * We process one mbuf from queue.
410 ngdread(struct cdev *dev, struct uio *uio, int flag)
412 priv_p priv = (priv_p )dev->si_drv1;
420 IF_DEQUEUE(&priv->readq, m);
422 if (flag & IO_NDELAY)
423 return (EWOULDBLOCK);
424 mtx_lock(&priv->ngd_mtx);
425 priv->flags |= NGDF_RWAIT;
426 if ((error = msleep(priv, &priv->ngd_mtx,
427 PDROP | PCATCH | (PZERO + 1),
433 while (m && uio->uio_resid > 0 && error == 0) {
434 len = MIN(uio->uio_resid, m->m_len);
436 error = uiomove(mtod(m, void *), len, uio);
448 * This function is called when our device is written to.
449 * We read the data from userland into mbuf chain and pass it to the remote hook.
453 ngdwrite(struct cdev *dev, struct uio *uio, int flag)
455 priv_p priv = (priv_p )dev->si_drv1;
461 if (uio->uio_resid == 0)
464 if (uio->uio_resid < 0 || uio->uio_resid > IP_MAXPACKET)
467 if ((m = m_uiotombuf(uio, M_NOWAIT, 0, 0, M_PKTHDR)) == NULL)
470 NG_SEND_DATA_ONLY(error, priv->hook, m);
476 * we are being polled/selected
477 * check if there is data available for read
480 ngdpoll(struct cdev *dev, int events, struct thread *td)
482 priv_p priv = (priv_p )dev->si_drv1;
485 if (events & (POLLIN | POLLRDNORM) &&
486 !IFQ_IS_EMPTY(&priv->readq))
487 revents |= events & (POLLIN | POLLRDNORM);