1 /*-
2 * Copyright (c) 2002 Mark Santcroos <marks@ripe.net>
3 * Copyright (c) 2004 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: releng/5.4/sys/netgraph/ng_device.c 142772 2005-02-28 09:54:28Z glebius $
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 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;
72
73 /* Netgraph type */
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,
83 };
84 NETGRAPH_INIT(device, &ngd_typestruct);
85
86 /* per node data */
87 struct ngd_private {
88 struct ifqueue readq;
89 SLIST_ENTRY(ngd_private) links;
90 struct ng_node *node;
91 struct ng_hook *hook;
92 struct cdev *ngddev;
93 struct mtx ngd_mtx;
94 int unit;
95 uint16_t flags;
96 #define NGDF_OPEN 0x0001
97 #define NGDF_RWAIT 0x0002
98 };
99 typedef struct ngd_private *priv_p;
100
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);
105
106 /* Maximum number of NGD devices */
107 #define MAX_NGD 25 /* should be more than enough for now */
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 /* Helper functions */
132 static int get_free_unit(void);
133
134 /******************************************************************************
135 * Netgraph methods
136 ******************************************************************************/
137
138 /*
139 * create new node
140 */
141 static int
142 ng_device_constructor(node_p node)
143 {
144 priv_p priv;
145
146 DBG;
147
148 MALLOC(priv, priv_p, sizeof(*priv), M_NETGRAPH, M_NOWAIT | M_ZERO);
149 if (priv == NULL)
150 return (ENOMEM);
151
152 mtx_lock(&ng_device_mtx);
153 priv->unit = get_free_unit();
154 if(priv->unit < 0) {
155 printf("%s: No free unit found by get_free_unit(), "
156 "increase MAX_NGD\n",__func__);
157 mtx_unlock(&ng_device_mtx);
158 FREE(priv, M_NETGRAPH);
159 return(EINVAL);
160 }
161 SLIST_INSERT_HEAD(&ngd_nodes, priv, links);
162 mtx_unlock(&ng_device_mtx);
163
164 /* Initialize mutexes and queue */
165 mtx_init(&priv->ngd_mtx, "ng_device", NULL, MTX_DEF);
166 mtx_init(&priv->readq.ifq_mtx, "ng_device queue", NULL, MTX_DEF);
167 IFQ_SET_MAXLEN(&priv->readq, ifqmaxlen);
168
169 /* Link everything together */
170 NG_NODE_SET_PRIVATE(node, priv);
171 priv->node = node;
172
173 priv->ngddev = make_dev(&ngd_cdevsw, unit2minor(priv->unit), UID_ROOT,
174 GID_WHEEL, 0600, NG_DEVICE_DEVNAME "%d", priv->unit);
175 if(priv->ngddev == NULL) {
176 printf("%s(): make_dev() failed\n",__func__);
177 mtx_destroy(&priv->ngd_mtx);
178 mtx_destroy(&priv->readq.ifq_mtx);
179 FREE(priv, M_NETGRAPH);
180 return(EINVAL);
181 }
182 /* XXX: race here? */
183 priv->ngddev->si_drv1 = priv;
184
185 return(0);
186 }
187
188 /*
189 * Process control message.
190 */
191
192 static int
193 ng_device_rcvmsg(node_p node, item_p item, hook_p lasthook)
194 {
195 const priv_p priv = NG_NODE_PRIVATE(node);
196 struct ng_mesg *msg;
197 struct ng_mesg *resp = NULL;
198 int error = 0;
199
200 NGI_GET_MSG(item, msg);
201
202 if (msg->header.typecookie == NGM_DEVICE_COOKIE) {
203 switch (msg->header.cmd) {
204 case NGM_DEVICE_GET_DEVNAME:
205 /* XXX: Fix when NGD_MAX us bigger */
206 NG_MKRESPONSE(resp, msg,
207 strlen(NG_DEVICE_DEVNAME) + 3, M_NOWAIT);
208
209 if (resp == NULL)
210 ERROUT(ENOMEM);
211
212 strlcpy((char *)resp->data, priv->ngddev->si_name,
213 strlen(priv->ngddev->si_name) + 1);
214 break;
215
216 default:
217 error = EINVAL;
218 break;
219 }
220 } else
221 error = EINVAL;
222
223 done:
224 NG_RESPOND_MSG(error, node, item, resp);
225 NG_FREE_MSG(msg);
226 return (error);
227 }
228
229 /*
230 * Accept incoming hook. We support only one hook per node.
231 */
232 static int
233 ng_device_newhook(node_p node, hook_p hook, const char *name)
234 {
235 priv_p priv = NG_NODE_PRIVATE(node);
236
237 DBG;
238
239 /* We have only one hook per node */
240 if (priv->hook != NULL)
241 return (EISCONN);
242
243 priv->hook = hook;
244
245 return(0);
246 }
247
248 /*
249 * Receive data from hook, write it to device.
250 */
251 static int
252 ng_device_rcvdata(hook_p hook, item_p item)
253 {
254 priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
255 struct mbuf *m;
256
257 DBG;
258
259 NGI_GET_M(item, m);
260 NG_FREE_ITEM(item);
261
262 IF_LOCK(&priv->readq);
263 if (_IF_QFULL(&priv->readq)) {
264 _IF_DROP(&priv->readq);
265 IF_UNLOCK(&priv->readq);
266 NG_FREE_M(m);
267 return (ENOBUFS);
268 }
269
270 _IF_ENQUEUE(&priv->readq, m);
271 IF_UNLOCK(&priv->readq);
272 mtx_lock(&priv->ngd_mtx);
273 if (priv->flags & NGDF_RWAIT) {
274 priv->flags &= ~NGDF_RWAIT;
275 wakeup(priv);
276 }
277 mtx_unlock(&priv->ngd_mtx);
278
279 return(0);
280 }
281
282 /*
283 * Removal of the hook destroys the node.
284 */
285 static int
286 ng_device_disconnect(hook_p hook)
287 {
288 priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
289
290 DBG;
291
292 destroy_dev(priv->ngddev);
293 mtx_destroy(&priv->ngd_mtx);
294
295 mtx_lock(&ng_device_mtx);
296 SLIST_REMOVE(&ngd_nodes, priv, ngd_private, links);
297 mtx_unlock(&ng_device_mtx);
298
299 IF_DRAIN(&priv->readq);
300 mtx_destroy(&(priv)->readq.ifq_mtx);
301
302 FREE(priv, M_NETGRAPH);
303
304 ng_rmnode_self(NG_HOOK_NODE(hook));
305
306 return(0);
307 }
308
309 /*
310 * Node shutdown. Everything is already done in disconnect method.
311 */
312 static int
313 ng_device_shutdown(node_p node)
314 {
315 NG_NODE_UNREF(node);
316 return (0);
317 }
318
319 /******************************************************************************
320 * Device methods
321 ******************************************************************************/
322
323 /*
324 * the device is opened
325 */
326 static int
327 ngdopen(struct cdev *dev, int flag, int mode, struct thread *td)
328 {
329 priv_p priv = (priv_p )dev->si_drv1;
330
331 DBG;
332
333 mtx_lock(&priv->ngd_mtx);
334 priv->flags |= NGDF_OPEN;
335 mtx_unlock(&priv->ngd_mtx);
336
337 return(0);
338 }
339
340 /*
341 * the device is closed
342 */
343 static int
344 ngdclose(struct cdev *dev, int flag, int mode, struct thread *td)
345 {
346 priv_p priv = (priv_p )dev->si_drv1;
347
348 DBG;
349 mtx_lock(&priv->ngd_mtx);
350 priv->flags &= ~NGDF_OPEN;
351 mtx_unlock(&priv->ngd_mtx);
352
353 return(0);
354 }
355
356 #if 0 /*
357 * The ioctl is transformed into netgraph control message.
358 * We do not process them, yet.
359 */
360 /*
361 * process ioctl
362 *
363 * they are translated into netgraph messages and passed on
364 *
365 */
366 static int
367 ngdioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
368 {
369 struct ngd_softc *sc = &ngd_softc;
370 struct ngd_connection * connection = NULL;
371 struct ngd_connection * tmp;
372 int error = 0;
373 struct ng_mesg *msg;
374 struct ngd_param_s * datap;
375
376 DBG;
377
378 SLIST_FOREACH(tmp,&sc->head,links) {
379 if(tmp->ngddev == dev) {
380 connection = tmp;
381 }
382 }
383 if(connection == NULL) {
384 printf("%s(): connection is still NULL, no dev found\n",__func__);
385 return(-1);
386 }
387
388 NG_MKMESSAGE(msg, NGM_DEVICE_COOKIE, cmd, sizeof(struct ngd_param_s),
389 M_NOWAIT);
390 if (msg == NULL) {
391 printf("%s(): msg == NULL\n",__func__);
392 goto nomsg;
393 }
394
395 /* pass the ioctl data into the ->data area */
396 datap = (struct ngd_param_s *)msg->data;
397 datap->p = addr;
398
399 NG_SEND_MSG_HOOK(error, sc->node, msg, connection->active_hook, 0);
400 if(error)
401 printf("%s(): NG_SEND_MSG_HOOK error: %d\n",__func__,error);
402
403 nomsg:
404
405 return(0);
406 }
407 #endif /* if 0 */
408
409 /*
410 * This function is called when a read(2) is done to our device.
411 * We process one mbuf from queue.
412 */
413 static int
414 ngdread(struct cdev *dev, struct uio *uio, int flag)
415 {
416 priv_p priv = (priv_p )dev->si_drv1;
417 struct mbuf *m;
418 int len, error = 0;
419
420 DBG;
421
422 /* get an mbuf */
423 do {
424 IF_DEQUEUE(&priv->readq, m);
425 if (m == NULL) {
426 if (flag & IO_NDELAY)
427 return (EWOULDBLOCK);
428 mtx_lock(&priv->ngd_mtx);
429 priv->flags |= NGDF_RWAIT;
430 if ((error = msleep(priv, &priv->ngd_mtx,
431 PDROP | PCATCH | (PZERO + 1),
432 "ngdread", 0)) != 0)
433 return (error);
434 }
435 } while (m == NULL);
436
437 while (m && uio->uio_resid > 0 && error == 0) {
438 len = MIN(uio->uio_resid, m->m_len);
439 if (len != 0)
440 error = uiomove(mtod(m, void *), len, uio);
441 m = m_free(m);
442 }
443
444 if (m)
445 m_freem(m);
446
447 return (error);
448 }
449
450
451 /*
452 * This function is called when our device is written to.
453 * We read the data from userland into mbuf chain and pass it to the remote hook.
454 *
455 */
456 static int
457 ngdwrite(struct cdev *dev, struct uio *uio, int flag)
458 {
459 priv_p priv = (priv_p )dev->si_drv1;
460 struct mbuf *m;
461 int error = 0;
462
463 DBG;
464
465 if (uio->uio_resid == 0)
466 return (0);
467
468 if (uio->uio_resid < 0 || uio->uio_resid > IP_MAXPACKET)
469 return (EIO);
470
471 if ((m = m_uiotombuf(uio, M_DONTWAIT, 0)) == NULL)
472 return (ENOBUFS);
473
474 NG_SEND_DATA_ONLY(error, priv->hook, m);
475
476 return (error);
477 }
478
479 /*
480 * we are being polled/selected
481 * check if there is data available for read
482 */
483 static int
484 ngdpoll(struct cdev *dev, int events, struct thread *td)
485 {
486 priv_p priv = (priv_p )dev->si_drv1;
487 int revents = 0;
488
489 if (events & (POLLIN | POLLRDNORM) &&
490 !IFQ_IS_EMPTY(&priv->readq))
491 revents |= events & (POLLIN | POLLRDNORM);
492
493 return (revents);
494 }
495
496 /******************************************************************************
497 * Helper subroutines
498 ******************************************************************************/
499
500 static int
501 get_free_unit()
502 {
503 struct ngd_private *priv = NULL;
504 int n = 0;
505 int unit = -1;
506
507 DBG;
508
509 mtx_assert(&ng_device_mtx, MA_OWNED);
510
511 /* When there is no list yet, the first device unit is always 0. */
512 if SLIST_EMPTY(&ngd_nodes)
513 return(0);
514
515 /* Just do a brute force loop to find the first free unit that is
516 * smaller than MAX_NGD.
517 * Set MAX_NGD to a large value, doesn't impact performance.
518 */
519 for(n = 0; n<MAX_NGD && unit == -1; n++) {
520 SLIST_FOREACH(priv, &ngd_nodes, links) {
521
522 if(priv->unit == n) {
523 unit = -1;
524 break;
525 }
526 unit = n;
527 }
528 }
529
530 return (unit);
531 }
Cache object: 756c3729e86e373e8bee8d556974f173
|