Merge branch 'vendor/OPENSSL'
[dragonfly.git] / sys / netgraph7 / ng_device.c
1 /*-
2  * Copyright (c) 2002 Mark Santcroos <marks@ripe.net>
3  * Copyright (c) 2004-2005 Gleb Smirnoff <glebius@FreeBSD.org>
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
13  *
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.
24  *
25  * Netgraph "device" node
26  *
27  * This node presents a /dev/ngd%d device that interfaces to an other
28  * netgraph node.
29  *
30  * $FreeBSD: src/sys/netgraph/ng_device.c,v 1.22 2006/11/02 17:37:21 andre Exp $
31  * $DragonFly: src/sys/netgraph7/ng_device.c,v 1.2 2008/06/26 23:05:35 dillon Exp $
32  *
33  */
34
35 #if 0
36 #define DBG do { printf("ng_device: %s\n", __func__ ); } while (0)
37 #else
38 #define DBG do {} while (0)
39 #endif
40
41 #include <sys/param.h>
42 #include <sys/conf.h>
43 #include <sys/ioccom.h>
44 #include <sys/kernel.h>
45 #include <sys/malloc.h>
46 #include <sys/mbuf.h>
47 #include <sys/poll.h>
48 #include <sys/queue.h>
49 #include <sys/socket.h>
50 #include <sys/systm.h>
51 #include <sys/uio.h>
52 #include <sys/vnode.h>
53
54 #include <net/if.h>
55 #include <net/if_var.h>
56 #include <netinet/in.h>
57 #include <netinet/in_systm.h>
58 #include <netinet/ip.h>
59
60 #include "ng_message.h"
61 #include "netgraph.h"
62 #include "ng_device.h"
63
64 #define ERROUT(x) do { error = (x); goto done; } while (0)
65
66 /* Netgraph methods */
67 static int              ng_device_mod_event(module_t, int, void *);
68 static ng_constructor_t ng_device_constructor;
69 static ng_rcvmsg_t      ng_device_rcvmsg;
70 static ng_shutdown_t    ng_device_shutdown;
71 static ng_newhook_t     ng_device_newhook;
72 static ng_rcvdata_t     ng_device_rcvdata;
73 static ng_disconnect_t  ng_device_disconnect;
74
75 /* Netgraph type */
76 static struct ng_type ngd_typestruct = {
77         .version =      NG_ABI_VERSION,
78         .name =         NG_DEVICE_NODE_TYPE,
79         .mod_event =    ng_device_mod_event,
80         .constructor =  ng_device_constructor,
81         .rcvmsg =       ng_device_rcvmsg,
82         .shutdown =     ng_device_shutdown,
83         .newhook =      ng_device_newhook,
84         .rcvdata =      ng_device_rcvdata,
85         .disconnect =   ng_device_disconnect,
86 };
87 NETGRAPH_INIT(device, &ngd_typestruct);
88
89 /* per node data */
90 struct ngd_private {
91         struct  ifqueue readq;
92         struct  ng_node *node;
93         struct  ng_hook *hook;
94         struct  cdev    *ngddev;
95         struct  mtx     ngd_mtx;
96         int             unit;
97         uint16_t        flags;
98 #define NGDF_OPEN       0x0001
99 #define NGDF_RWAIT      0x0002
100 };
101 typedef struct ngd_private *priv_p;
102
103 /* unit number allocator entity */
104 static struct unrhdr *ngd_unit;
105
106 /* Maximum number of NGD devices */
107 #define MAX_NGD 999
108
109 static d_close_t ngdclose;
110 static d_open_t ngdopen;
111 static d_read_t ngdread;
112 static d_write_t ngdwrite;
113 #if 0
114 static d_ioctl_t ngdioctl;
115 #endif
116 static d_poll_t ngdpoll;
117
118 static struct cdevsw ngd_cdevsw = {
119         .d_version =    D_VERSION,
120         .d_open =       ngdopen,
121         .d_close =      ngdclose,
122         .d_read =       ngdread,
123         .d_write =      ngdwrite,
124 #if 0
125         .d_ioctl =      ngdioctl,
126 #endif
127         .d_poll =       ngdpoll,
128         .d_name =       NG_DEVICE_DEVNAME,
129 };
130
131 /******************************************************************************
132  *  Netgraph methods
133  ******************************************************************************/
134
135 /*
136  * Handle loading and unloading for this node type.
137  */
138 static int
139 ng_device_mod_event(module_t mod, int event, void *data)
140 {
141         int error = 0;
142
143         switch (event) {
144         case MOD_LOAD:
145                 ngd_unit = new_unrhdr(0, MAX_NGD, NULL);
146                 break;
147         case MOD_UNLOAD:
148                 delete_unrhdr(ngd_unit);
149                 break;
150         default:
151                 error = EOPNOTSUPP;
152                 break;
153         }
154         return (error);
155 }
156
157 /*
158  * create new node
159  */
160 static int
161 ng_device_constructor(node_p node)
162 {
163         priv_p  priv;
164
165         DBG;
166
167         MALLOC(priv, priv_p, sizeof(*priv), M_NETGRAPH, M_WAITOK | M_ZERO);
168         if (priv == NULL)
169                 return (ENOMEM);
170
171         /* Allocate unit number */
172         priv->unit = alloc_unr(ngd_unit);
173
174         /* Initialize mutexes and queue */
175         mtx_init(&priv->ngd_mtx, "ng_device", NULL, MTX_DEF);
176         mtx_init(&priv->readq.ifq_mtx, "ng_device queue", NULL, MTX_DEF);
177         IFQ_SET_MAXLEN(&priv->readq, ifqmaxlen);
178
179         /* Link everything together */
180         NG_NODE_SET_PRIVATE(node, priv);
181         priv->node = node;
182
183         priv->ngddev = make_dev(&ngd_cdevsw, unit2minor(priv->unit), UID_ROOT,
184             GID_WHEEL, 0600, NG_DEVICE_DEVNAME "%d", priv->unit);
185         if(priv->ngddev == NULL) {
186                 printf("%s(): make_dev() failed\n",__func__);
187                 mtx_destroy(&priv->ngd_mtx);
188                 mtx_destroy(&priv->readq.ifq_mtx);
189                 free_unr(ngd_unit, priv->unit);
190                 FREE(priv, M_NETGRAPH);
191                 return(EINVAL);
192         }
193         /* XXX: race here? */
194         priv->ngddev->si_drv1 = priv;
195
196         return(0);
197 }
198
199 /*
200  * Process control message.
201  */
202
203 static int
204 ng_device_rcvmsg(node_p node, item_p item, hook_p lasthook)
205 {
206         const priv_p priv = NG_NODE_PRIVATE(node);
207         struct ng_mesg *msg;
208         struct ng_mesg *resp = NULL;
209         int error = 0;
210
211         NGI_GET_MSG(item, msg);
212
213         if (msg->header.typecookie == NGM_DEVICE_COOKIE) {
214                 switch (msg->header.cmd) {
215                 case NGM_DEVICE_GET_DEVNAME:
216                         /* XXX: Fix when MAX_NGD us bigger */
217                         NG_MKRESPONSE(resp, msg,
218                             strlen(NG_DEVICE_DEVNAME) + 4, M_WAITOK);
219
220                         if (resp == NULL)
221                                 ERROUT(ENOMEM);
222
223                         strlcpy((char *)resp->data, priv->ngddev->si_name,
224                             strlen(priv->ngddev->si_name) + 1);
225                         break;
226
227                 default:
228                         error = EINVAL;
229                         break;
230                 }
231         } else
232                 error = EINVAL;
233
234 done:
235         NG_RESPOND_MSG(error, node, item, resp);
236         NG_FREE_MSG(msg);
237         return (error);
238 }
239
240 /*
241  * Accept incoming hook. We support only one hook per node.
242  */
243 static int
244 ng_device_newhook(node_p node, hook_p hook, const char *name)
245 {
246         priv_p priv = NG_NODE_PRIVATE(node);
247
248         DBG;
249
250         /* We have only one hook per node */
251         if (priv->hook != NULL)
252                 return (EISCONN);
253
254         priv->hook = hook;
255
256         return(0);
257 }
258
259 /*
260  * Receive data from hook, write it to device.
261  */
262 static int
263 ng_device_rcvdata(hook_p hook, item_p item)
264 {
265         priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
266         struct mbuf *m;
267
268         DBG;
269
270         NGI_GET_M(item, m);
271         NG_FREE_ITEM(item);
272
273         IF_LOCK(&priv->readq);
274         if (_IF_QFULL(&priv->readq)) {
275                 _IF_DROP(&priv->readq);
276                 IF_UNLOCK(&priv->readq);
277                 NG_FREE_M(m);
278                 return (ENOBUFS);
279         }
280
281         _IF_ENQUEUE(&priv->readq, m);
282         IF_UNLOCK(&priv->readq);
283         mtx_lock(&priv->ngd_mtx);
284         if (priv->flags & NGDF_RWAIT) {
285                 priv->flags &= ~NGDF_RWAIT;
286                 wakeup(priv);
287         }
288         mtx_unlock(&priv->ngd_mtx);
289
290         return(0);
291 }
292
293 /*
294  * Removal of the hook destroys the node.
295  */
296 static int
297 ng_device_disconnect(hook_p hook)
298 {
299         priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
300
301         DBG;
302
303         destroy_dev(priv->ngddev);
304         mtx_destroy(&priv->ngd_mtx);
305
306         IF_DRAIN(&priv->readq);
307         mtx_destroy(&(priv)->readq.ifq_mtx);
308
309         free_unr(ngd_unit, priv->unit);
310
311         FREE(priv, M_NETGRAPH);
312
313         ng_rmnode_self(NG_HOOK_NODE(hook));
314
315         return(0);
316 }
317
318 /*
319  * Node shutdown. Everything is already done in disconnect method.
320  */
321 static int
322 ng_device_shutdown(node_p node)
323 {
324         NG_NODE_UNREF(node);
325         return (0);
326 }
327
328 /******************************************************************************
329  *  Device methods
330  ******************************************************************************/
331
332 /*
333  * the device is opened
334  */
335 static int
336 ngdopen(struct cdev *dev, int flag, int mode, struct thread *td)
337 {
338         priv_p  priv = (priv_p )dev->si_drv1;
339
340         DBG;
341
342         mtx_lock(&priv->ngd_mtx);
343         priv->flags |= NGDF_OPEN;
344         mtx_unlock(&priv->ngd_mtx);
345
346         return(0);
347 }
348
349 /*
350  * the device is closed
351  */
352 static int
353 ngdclose(struct cdev *dev, int flag, int mode, struct thread *td)
354 {
355         priv_p  priv = (priv_p )dev->si_drv1;
356
357         DBG;
358         mtx_lock(&priv->ngd_mtx);
359         priv->flags &= ~NGDF_OPEN;
360         mtx_unlock(&priv->ngd_mtx);
361
362         return(0);
363 }
364
365 #if 0   /*
366          * The ioctl is transformed into netgraph control message.
367          * We do not process them, yet.
368          */
369 /*
370  * process ioctl
371  *
372  * they are translated into netgraph messages and passed on
373  *
374  */
375 static int
376 ngdioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
377 {
378         struct ngd_softc *sc = &ngd_softc;
379         struct ngd_connection * connection = NULL;
380         struct ngd_connection * tmp;
381         int error = 0;
382         struct ng_mesg *msg;
383         struct ngd_param_s * datap;
384
385         DBG;
386
387         NG_MKMESSAGE(msg, NGM_DEVICE_COOKIE, cmd, sizeof(struct ngd_param_s),
388                         M_WAITOK);
389         if (msg == NULL) {
390                 printf("%s(): msg == NULL\n",__func__);
391                 goto nomsg;
392         }
393
394         /* pass the ioctl data into the ->data area */
395         datap = (struct ngd_param_s *)msg->data;
396         datap->p = addr;
397
398         NG_SEND_MSG_HOOK(error, sc->node, msg, connection->active_hook, 0);
399         if(error)
400                 printf("%s(): NG_SEND_MSG_HOOK error: %d\n",__func__,error);
401
402 nomsg:
403
404         return(0);
405 }
406 #endif /* if 0 */
407
408 /*
409  * This function is called when a read(2) is done to our device.
410  * We process one mbuf from queue.
411  */
412 static int
413 ngdread(struct cdev *dev, struct uio *uio, int flag)
414 {
415         priv_p  priv = (priv_p )dev->si_drv1;
416         struct mbuf *m;
417         int len, error = 0;
418
419         DBG;
420
421         /* get an mbuf */
422         do {
423                 IF_DEQUEUE(&priv->readq, m);
424                 if (m == NULL) {
425                         if (flag & IO_NDELAY)
426                                 return (EWOULDBLOCK);
427                         mtx_lock(&priv->ngd_mtx);
428                         priv->flags |= NGDF_RWAIT;
429                         if ((error = msleep(priv, &priv->ngd_mtx,
430                             PDROP | PCATCH | (PZERO + 1),
431                             "ngdread", 0)) != 0)
432                                 return (error);
433                 }
434         } while (m == NULL);
435
436         while (m && uio->uio_resid > 0 && error == 0) {
437                 len = MIN(uio->uio_resid, m->m_len);
438                 if (len != 0)
439                         error = uiomove(mtod(m, void *), len, uio);
440                 m = m_free(m);
441         }
442
443         if (m)
444                 m_freem(m);
445
446         return (error);
447 }
448
449
450 /*
451  * This function is called when our device is written to.
452  * We read the data from userland into mbuf chain and pass it to the remote hook.
453  *
454  */
455 static int
456 ngdwrite(struct cdev *dev, struct uio *uio, int flag)
457 {
458         priv_p  priv = (priv_p )dev->si_drv1;
459         struct mbuf *m;
460         int error = 0;
461
462         DBG;
463
464         if (uio->uio_resid == 0)
465                 return (0);
466
467         if (uio->uio_resid < 0 || uio->uio_resid > IP_MAXPACKET)
468                 return (EIO);
469
470         if ((m = m_uiotombuf(uio, MB_DONTWAIT, 0, 0, M_PKTHDR)) == NULL)
471                 return (ENOBUFS);
472
473         NG_SEND_DATA_ONLY(error, priv->hook, m);
474
475         return (error);
476 }
477
478 /*
479  * we are being polled/selected
480  * check if there is data available for read
481  */
482 static int
483 ngdpoll(struct cdev *dev, int events, struct thread *td)
484 {
485         priv_p  priv = (priv_p )dev->si_drv1;
486         int revents = 0;
487
488         if (events & (POLLIN | POLLRDNORM) &&
489             !IFQ_IS_EMPTY(&priv->readq))
490                 revents |= events & (POLLIN | POLLRDNORM);
491
492         return (revents);
493 }