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