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