xref: /freebsd/sys/netgraph/ng_device.c (revision fe267a559009cbf34f9341666fe4d88a92c02d5e)
1c398230bSWarner Losh /*-
2*fe267a55SPedro F. Giffuni  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3*fe267a55SPedro F. Giffuni  *
4a8353960SJulian Elischer  * Copyright (c) 2002 Mark Santcroos <marks@ripe.net>
55cdd064dSGleb Smirnoff  * Copyright (c) 2004-2005 Gleb Smirnoff <glebius@FreeBSD.org>
6a8353960SJulian Elischer  *
7a8353960SJulian Elischer  * Redistribution and use in source and binary forms, with or without
8a8353960SJulian Elischer  * modification, are permitted provided that the following conditions
9a8353960SJulian Elischer  * are met:
10a8353960SJulian Elischer  * 1. Redistributions of source code must retain the above copyright
11a8353960SJulian Elischer  *    notice, this list of conditions and the following disclaimer.
12a8353960SJulian Elischer  * 2. Redistributions in binary form must reproduce the above copyright
13a8353960SJulian Elischer  *    notice, this list of conditions and the following disclaimer in the
14a8353960SJulian Elischer  *    documentation and/or other materials provided with the distribution.
15a8353960SJulian Elischer  *
16a8353960SJulian Elischer  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17a8353960SJulian Elischer  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18a8353960SJulian Elischer  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19a8353960SJulian Elischer  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20a8353960SJulian Elischer  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21a8353960SJulian Elischer  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22a8353960SJulian Elischer  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23a8353960SJulian Elischer  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24a8353960SJulian Elischer  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25a8353960SJulian Elischer  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26a8353960SJulian Elischer  *
27a8353960SJulian Elischer  * Netgraph "device" node
28a8353960SJulian Elischer  *
29a8353960SJulian Elischer  * This node presents a /dev/ngd%d device that interfaces to an other
30a8353960SJulian Elischer  * netgraph node.
31a8353960SJulian Elischer  *
32a8353960SJulian Elischer  * $FreeBSD$
33a8353960SJulian Elischer  *
34a8353960SJulian Elischer  */
35a8353960SJulian Elischer 
36547d3473SGleb Smirnoff #if 0
37ec774932SGleb Smirnoff #define	DBG do { printf("ng_device: %s\n", __func__ ); } while (0)
38547d3473SGleb Smirnoff #else
39ec774932SGleb Smirnoff #define	DBG do {} while (0)
40547d3473SGleb Smirnoff #endif
41547d3473SGleb Smirnoff 
42a8353960SJulian Elischer #include <sys/param.h>
43a8353960SJulian Elischer #include <sys/conf.h>
44a8353960SJulian Elischer #include <sys/ioccom.h>
45b3e3ef98SGleb Smirnoff #include <sys/kernel.h>
46b3e3ef98SGleb Smirnoff #include <sys/malloc.h>
47b3e3ef98SGleb Smirnoff #include <sys/mbuf.h>
48b3e3ef98SGleb Smirnoff #include <sys/poll.h>
49b3e3ef98SGleb Smirnoff #include <sys/queue.h>
50547d3473SGleb Smirnoff #include <sys/socket.h>
51b3e3ef98SGleb Smirnoff #include <sys/systm.h>
52b3e3ef98SGleb Smirnoff #include <sys/uio.h>
53547d3473SGleb Smirnoff #include <sys/vnode.h>
54547d3473SGleb Smirnoff 
55547d3473SGleb Smirnoff #include <net/if.h>
56547d3473SGleb Smirnoff #include <net/if_var.h>
57547d3473SGleb Smirnoff #include <netinet/in.h>
58547d3473SGleb Smirnoff #include <netinet/in_systm.h>
59547d3473SGleb Smirnoff #include <netinet/ip.h>
60a8353960SJulian Elischer 
61a8353960SJulian Elischer #include <netgraph/ng_message.h>
62a8353960SJulian Elischer #include <netgraph/netgraph.h>
63b3e3ef98SGleb Smirnoff #include <netgraph/ng_device.h>
64a8353960SJulian Elischer 
65c1eec6c5SGleb Smirnoff #define	ERROUT(x) do { error = (x); goto done; } while (0)
66c1eec6c5SGleb Smirnoff 
67a8353960SJulian Elischer /* Netgraph methods */
6830aabc9aSRuslan Ermilov static int		ng_device_mod_event(module_t, int, void *);
69547d3473SGleb Smirnoff static ng_constructor_t	ng_device_constructor;
70a8353960SJulian Elischer static ng_rcvmsg_t	ng_device_rcvmsg;
71547d3473SGleb Smirnoff static ng_shutdown_t	ng_device_shutdown;
72a8353960SJulian Elischer static ng_newhook_t	ng_device_newhook;
73a8353960SJulian Elischer static ng_rcvdata_t	ng_device_rcvdata;
74a8353960SJulian Elischer static ng_disconnect_t	ng_device_disconnect;
75a8353960SJulian Elischer 
76a8353960SJulian Elischer /* Netgraph type */
77547d3473SGleb Smirnoff static struct ng_type ngd_typestruct = {
78f8aae777SJulian Elischer 	.version =	NG_ABI_VERSION,
79f8aae777SJulian Elischer 	.name =		NG_DEVICE_NODE_TYPE,
8030aabc9aSRuslan Ermilov 	.mod_event =	ng_device_mod_event,
81547d3473SGleb Smirnoff 	.constructor =	ng_device_constructor,
82f8aae777SJulian Elischer 	.rcvmsg	=	ng_device_rcvmsg,
83547d3473SGleb Smirnoff 	.shutdown = 	ng_device_shutdown,
84f8aae777SJulian Elischer 	.newhook =	ng_device_newhook,
85f8aae777SJulian Elischer 	.rcvdata =	ng_device_rcvdata,
86f8aae777SJulian Elischer 	.disconnect =	ng_device_disconnect,
87a8353960SJulian Elischer };
88547d3473SGleb Smirnoff NETGRAPH_INIT(device, &ngd_typestruct);
89a8353960SJulian Elischer 
90547d3473SGleb Smirnoff /* per node data */
91547d3473SGleb Smirnoff struct ngd_private {
92547d3473SGleb Smirnoff 	struct	ifqueue	readq;
93547d3473SGleb Smirnoff 	struct	ng_node	*node;
94547d3473SGleb Smirnoff 	struct	ng_hook	*hook;
9589c9c53dSPoul-Henning Kamp 	struct	cdev	*ngddev;
96547d3473SGleb Smirnoff 	struct	mtx	ngd_mtx;
97a8353960SJulian Elischer 	int 		unit;
98547d3473SGleb Smirnoff 	uint16_t	flags;
99547d3473SGleb Smirnoff #define	NGDF_OPEN	0x0001
100547d3473SGleb Smirnoff #define	NGDF_RWAIT	0x0002
101a8353960SJulian Elischer };
102547d3473SGleb Smirnoff typedef struct ngd_private *priv_p;
103a8353960SJulian Elischer 
1045cdd064dSGleb Smirnoff /* unit number allocator entity */
1055cdd064dSGleb Smirnoff static struct unrhdr *ngd_unit;
106a8353960SJulian Elischer 
107a8353960SJulian Elischer /* Maximum number of NGD devices */
1085cdd064dSGleb Smirnoff #define MAX_NGD	999
109a8353960SJulian Elischer 
110a8353960SJulian Elischer static d_close_t ngdclose;
111a8353960SJulian Elischer static d_open_t ngdopen;
112a8353960SJulian Elischer static d_read_t ngdread;
113a8353960SJulian Elischer static d_write_t ngdwrite;
114547d3473SGleb Smirnoff #if 0
115a8353960SJulian Elischer static d_ioctl_t ngdioctl;
116547d3473SGleb Smirnoff #endif
117a8353960SJulian Elischer static d_poll_t ngdpoll;
118a8353960SJulian Elischer 
119a8353960SJulian Elischer static struct cdevsw ngd_cdevsw = {
120dc08ffecSPoul-Henning Kamp 	.d_version =	D_VERSION,
1217ac40f5fSPoul-Henning Kamp 	.d_open =	ngdopen,
1227ac40f5fSPoul-Henning Kamp 	.d_close =	ngdclose,
1237ac40f5fSPoul-Henning Kamp 	.d_read =	ngdread,
1247ac40f5fSPoul-Henning Kamp 	.d_write =	ngdwrite,
125547d3473SGleb Smirnoff #if 0
1267ac40f5fSPoul-Henning Kamp 	.d_ioctl =	ngdioctl,
127547d3473SGleb Smirnoff #endif
1287ac40f5fSPoul-Henning Kamp 	.d_poll =	ngdpoll,
129547d3473SGleb Smirnoff 	.d_name =	NG_DEVICE_DEVNAME,
130a8353960SJulian Elischer };
131a8353960SJulian Elischer 
132547d3473SGleb Smirnoff /******************************************************************************
133547d3473SGleb Smirnoff  *  Netgraph methods
134547d3473SGleb Smirnoff  ******************************************************************************/
135547d3473SGleb Smirnoff 
136a8353960SJulian Elischer /*
1375cdd064dSGleb Smirnoff  * Handle loading and unloading for this node type.
1385cdd064dSGleb Smirnoff  */
1395cdd064dSGleb Smirnoff static int
1405cdd064dSGleb Smirnoff ng_device_mod_event(module_t mod, int event, void *data)
1415cdd064dSGleb Smirnoff {
1425cdd064dSGleb Smirnoff 	int error = 0;
1435cdd064dSGleb Smirnoff 
1445cdd064dSGleb Smirnoff 	switch (event) {
1455cdd064dSGleb Smirnoff 	case MOD_LOAD:
1465cdd064dSGleb Smirnoff 		ngd_unit = new_unrhdr(0, MAX_NGD, NULL);
1475cdd064dSGleb Smirnoff 		break;
1485cdd064dSGleb Smirnoff 	case MOD_UNLOAD:
1495cdd064dSGleb Smirnoff 		delete_unrhdr(ngd_unit);
1505cdd064dSGleb Smirnoff 		break;
1515cdd064dSGleb Smirnoff 	default:
1525cdd064dSGleb Smirnoff 		error = EOPNOTSUPP;
1535cdd064dSGleb Smirnoff 		break;
1545cdd064dSGleb Smirnoff 	}
1555cdd064dSGleb Smirnoff 	return (error);
1565cdd064dSGleb Smirnoff }
1575cdd064dSGleb Smirnoff 
1585cdd064dSGleb Smirnoff /*
159547d3473SGleb Smirnoff  * create new node
160a8353960SJulian Elischer  */
161a8353960SJulian Elischer static int
162547d3473SGleb Smirnoff ng_device_constructor(node_p node)
163a8353960SJulian Elischer {
164547d3473SGleb Smirnoff 	priv_p	priv;
165a8353960SJulian Elischer 
166f2b9562cSGleb Smirnoff 	DBG;
167a8353960SJulian Elischer 
168674d86bfSGleb Smirnoff 	priv = malloc(sizeof(*priv), M_NETGRAPH, M_WAITOK | M_ZERO);
169a8353960SJulian Elischer 
1705cdd064dSGleb Smirnoff 	/* Allocate unit number */
1715cdd064dSGleb Smirnoff 	priv->unit = alloc_unr(ngd_unit);
172a8353960SJulian Elischer 
173843cfd5aSGleb Smirnoff 	/* Initialize mutexes and queue */
174843cfd5aSGleb Smirnoff 	mtx_init(&priv->ngd_mtx, "ng_device", NULL, MTX_DEF);
175547d3473SGleb Smirnoff 	mtx_init(&priv->readq.ifq_mtx, "ng_device queue", NULL, MTX_DEF);
176547d3473SGleb Smirnoff 	IFQ_SET_MAXLEN(&priv->readq, ifqmaxlen);
177a8353960SJulian Elischer 
178547d3473SGleb Smirnoff 	/* Link everything together */
179547d3473SGleb Smirnoff 	NG_NODE_SET_PRIVATE(node, priv);
180547d3473SGleb Smirnoff 	priv->node = node;
181a8353960SJulian Elischer 
182d3ce8327SEd Schouten 	priv->ngddev = make_dev(&ngd_cdevsw, priv->unit, UID_ROOT,
183843cfd5aSGleb Smirnoff 	    GID_WHEEL, 0600, NG_DEVICE_DEVNAME "%d", priv->unit);
184843cfd5aSGleb Smirnoff 	if(priv->ngddev == NULL) {
185843cfd5aSGleb Smirnoff 		printf("%s(): make_dev() failed\n",__func__);
186843cfd5aSGleb Smirnoff 		mtx_destroy(&priv->ngd_mtx);
187843cfd5aSGleb Smirnoff 		mtx_destroy(&priv->readq.ifq_mtx);
1885cdd064dSGleb Smirnoff 		free_unr(ngd_unit, priv->unit);
1891ede983cSDag-Erling Smørgrav 		free(priv, M_NETGRAPH);
190843cfd5aSGleb Smirnoff 		return(EINVAL);
191843cfd5aSGleb Smirnoff 	}
192843cfd5aSGleb Smirnoff 	/* XXX: race here? */
193843cfd5aSGleb Smirnoff 	priv->ngddev->si_drv1 = priv;
194a8353960SJulian Elischer 
195a8353960SJulian Elischer 	return(0);
196a8353960SJulian Elischer }
197a8353960SJulian Elischer 
198a8353960SJulian Elischer /*
199547d3473SGleb Smirnoff  * Process control message.
200a8353960SJulian Elischer  */
201a8353960SJulian Elischer 
202a8353960SJulian Elischer static int
203a8353960SJulian Elischer ng_device_rcvmsg(node_p node, item_p item, hook_p lasthook)
204a8353960SJulian Elischer {
205547d3473SGleb Smirnoff 	const priv_p priv = NG_NODE_PRIVATE(node);
206a8353960SJulian Elischer 	struct ng_mesg *msg;
207547d3473SGleb Smirnoff 	struct ng_mesg *resp = NULL;
2087870adb6SEd Schouten 	const char *dn;
209a8353960SJulian Elischer 	int error = 0;
210a8353960SJulian Elischer 
211a8353960SJulian Elischer 	NGI_GET_MSG(item, msg);
212a8353960SJulian Elischer 
213547d3473SGleb Smirnoff 	if (msg->header.typecookie == NGM_DEVICE_COOKIE) {
214547d3473SGleb Smirnoff 		switch (msg->header.cmd) {
215547d3473SGleb Smirnoff 		case NGM_DEVICE_GET_DEVNAME:
2165cdd064dSGleb Smirnoff 			/* XXX: Fix when MAX_NGD us bigger */
217547d3473SGleb Smirnoff 			NG_MKRESPONSE(resp, msg,
2185cdd064dSGleb Smirnoff 			    strlen(NG_DEVICE_DEVNAME) + 4, M_NOWAIT);
219a8353960SJulian Elischer 
220547d3473SGleb Smirnoff 			if (resp == NULL)
221547d3473SGleb Smirnoff 				ERROUT(ENOMEM);
222547d3473SGleb Smirnoff 
2237870adb6SEd Schouten 			dn = devtoname(priv->ngddev);
2247870adb6SEd Schouten 			strlcpy((char *)resp->data, dn, strlen(dn) + 1);
225547d3473SGleb Smirnoff 			break;
226547d3473SGleb Smirnoff 
227547d3473SGleb Smirnoff 		default:
228547d3473SGleb Smirnoff 			error = EINVAL;
229547d3473SGleb Smirnoff 			break;
230547d3473SGleb Smirnoff 		}
231547d3473SGleb Smirnoff 	} else
232547d3473SGleb Smirnoff 		error = EINVAL;
233547d3473SGleb Smirnoff 
234547d3473SGleb Smirnoff done:
235547d3473SGleb Smirnoff 	NG_RESPOND_MSG(error, node, item, resp);
236547d3473SGleb Smirnoff 	NG_FREE_MSG(msg);
237a8353960SJulian Elischer 	return (error);
238a8353960SJulian Elischer }
239a8353960SJulian Elischer 
240a8353960SJulian Elischer /*
241547d3473SGleb Smirnoff  * Accept incoming hook. We support only one hook per node.
242a8353960SJulian Elischer  */
243a8353960SJulian Elischer static int
244a8353960SJulian Elischer ng_device_newhook(node_p node, hook_p hook, const char *name)
245a8353960SJulian Elischer {
246547d3473SGleb Smirnoff 	priv_p priv = NG_NODE_PRIVATE(node);
247a8353960SJulian Elischer 
248f2b9562cSGleb Smirnoff 	DBG;
249a8353960SJulian Elischer 
250547d3473SGleb Smirnoff 	/* We have only one hook per node */
251547d3473SGleb Smirnoff 	if (priv->hook != NULL)
252547d3473SGleb Smirnoff 		return (EISCONN);
253a8353960SJulian Elischer 
254547d3473SGleb Smirnoff 	priv->hook = hook;
255a8353960SJulian Elischer 
256a8353960SJulian Elischer 	return(0);
257a8353960SJulian Elischer }
258a8353960SJulian Elischer 
259a8353960SJulian Elischer /*
260547d3473SGleb Smirnoff  * Receive data from hook, write it to device.
261a8353960SJulian Elischer  */
262a8353960SJulian Elischer static int
263a8353960SJulian Elischer ng_device_rcvdata(hook_p hook, item_p item)
264a8353960SJulian Elischer {
265547d3473SGleb Smirnoff 	priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
266a8353960SJulian Elischer 	struct mbuf *m;
267a8353960SJulian Elischer 
268f2b9562cSGleb Smirnoff 	DBG;
269a8353960SJulian Elischer 
270a8353960SJulian Elischer 	NGI_GET_M(item, m);
271a8353960SJulian Elischer 	NG_FREE_ITEM(item);
272a8353960SJulian Elischer 
273547d3473SGleb Smirnoff 	IF_LOCK(&priv->readq);
274547d3473SGleb Smirnoff 	if (_IF_QFULL(&priv->readq)) {
275547d3473SGleb Smirnoff 		IF_UNLOCK(&priv->readq);
276c1eec6c5SGleb Smirnoff 		NG_FREE_M(m);
277547d3473SGleb Smirnoff 		return (ENOBUFS);
278547d3473SGleb Smirnoff 	}
279547d3473SGleb Smirnoff 
280547d3473SGleb Smirnoff 	_IF_ENQUEUE(&priv->readq, m);
281547d3473SGleb Smirnoff 	IF_UNLOCK(&priv->readq);
282547d3473SGleb Smirnoff 	mtx_lock(&priv->ngd_mtx);
283547d3473SGleb Smirnoff 	if (priv->flags & NGDF_RWAIT) {
284547d3473SGleb Smirnoff 		priv->flags &= ~NGDF_RWAIT;
285547d3473SGleb Smirnoff 		wakeup(priv);
286547d3473SGleb Smirnoff 	}
287547d3473SGleb Smirnoff 	mtx_unlock(&priv->ngd_mtx);
288547d3473SGleb Smirnoff 
289547d3473SGleb Smirnoff 	return(0);
290a8353960SJulian Elischer }
291a8353960SJulian Elischer 
292a8353960SJulian Elischer /*
293547d3473SGleb Smirnoff  * Removal of the hook destroys the node.
294a8353960SJulian Elischer  */
295a8353960SJulian Elischer static int
296a8353960SJulian Elischer ng_device_disconnect(hook_p hook)
297a8353960SJulian Elischer {
298547d3473SGleb Smirnoff 	priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
299a8353960SJulian Elischer 
300f2b9562cSGleb Smirnoff 	DBG;
301a8353960SJulian Elischer 
302547d3473SGleb Smirnoff 	destroy_dev(priv->ngddev);
303547d3473SGleb Smirnoff 	mtx_destroy(&priv->ngd_mtx);
304c1eec6c5SGleb Smirnoff 
305547d3473SGleb Smirnoff 	IF_DRAIN(&priv->readq);
306547d3473SGleb Smirnoff 	mtx_destroy(&(priv)->readq.ifq_mtx);
307a8353960SJulian Elischer 
3085cdd064dSGleb Smirnoff 	free_unr(ngd_unit, priv->unit);
3095cdd064dSGleb Smirnoff 
3101ede983cSDag-Erling Smørgrav 	free(priv, M_NETGRAPH);
311a8353960SJulian Elischer 
312547d3473SGleb Smirnoff 	ng_rmnode_self(NG_HOOK_NODE(hook));
313a8353960SJulian Elischer 
314a8353960SJulian Elischer 	return(0);
315a8353960SJulian Elischer }
316547d3473SGleb Smirnoff 
317547d3473SGleb Smirnoff /*
318547d3473SGleb Smirnoff  * Node shutdown. Everything is already done in disconnect method.
319547d3473SGleb Smirnoff  */
320547d3473SGleb Smirnoff static int
321547d3473SGleb Smirnoff ng_device_shutdown(node_p node)
322547d3473SGleb Smirnoff {
323547d3473SGleb Smirnoff 	NG_NODE_UNREF(node);
324547d3473SGleb Smirnoff 	return (0);
325547d3473SGleb Smirnoff }
326547d3473SGleb Smirnoff 
327547d3473SGleb Smirnoff /******************************************************************************
328547d3473SGleb Smirnoff  *  Device methods
329547d3473SGleb Smirnoff  ******************************************************************************/
330547d3473SGleb Smirnoff 
331a8353960SJulian Elischer /*
332a8353960SJulian Elischer  * the device is opened
333a8353960SJulian Elischer  */
334a8353960SJulian Elischer static int
33589c9c53dSPoul-Henning Kamp ngdopen(struct cdev *dev, int flag, int mode, struct thread *td)
336a8353960SJulian Elischer {
337547d3473SGleb Smirnoff 	priv_p	priv = (priv_p )dev->si_drv1;
338a8353960SJulian Elischer 
339f2b9562cSGleb Smirnoff 	DBG;
340f2b9562cSGleb Smirnoff 
341547d3473SGleb Smirnoff 	mtx_lock(&priv->ngd_mtx);
342547d3473SGleb Smirnoff 	priv->flags |= NGDF_OPEN;
343547d3473SGleb Smirnoff 	mtx_unlock(&priv->ngd_mtx);
344a8353960SJulian Elischer 
345a8353960SJulian Elischer 	return(0);
346a8353960SJulian Elischer }
347a8353960SJulian Elischer 
348a8353960SJulian Elischer /*
349a8353960SJulian Elischer  * the device is closed
350a8353960SJulian Elischer  */
351a8353960SJulian Elischer static int
35289c9c53dSPoul-Henning Kamp ngdclose(struct cdev *dev, int flag, int mode, struct thread *td)
353a8353960SJulian Elischer {
354547d3473SGleb Smirnoff 	priv_p	priv = (priv_p )dev->si_drv1;
355a8353960SJulian Elischer 
356f2b9562cSGleb Smirnoff 	DBG;
357547d3473SGleb Smirnoff 	mtx_lock(&priv->ngd_mtx);
358547d3473SGleb Smirnoff 	priv->flags &= ~NGDF_OPEN;
359547d3473SGleb Smirnoff 	mtx_unlock(&priv->ngd_mtx);
360a8353960SJulian Elischer 
361a8353960SJulian Elischer 	return(0);
362a8353960SJulian Elischer }
363a8353960SJulian Elischer 
364547d3473SGleb Smirnoff #if 0	/*
365547d3473SGleb Smirnoff 	 * The ioctl is transformed into netgraph control message.
366547d3473SGleb Smirnoff 	 * We do not process them, yet.
367547d3473SGleb Smirnoff 	 */
368a8353960SJulian Elischer /*
369a8353960SJulian Elischer  * process ioctl
370a8353960SJulian Elischer  *
371a8353960SJulian Elischer  * they are translated into netgraph messages and passed on
372a8353960SJulian Elischer  *
373a8353960SJulian Elischer  */
374a8353960SJulian Elischer static int
37589c9c53dSPoul-Henning Kamp ngdioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
376a8353960SJulian Elischer {
377a8353960SJulian Elischer 	struct ngd_softc *sc = &ngd_softc;
378a8353960SJulian Elischer 	struct ngd_connection * connection = NULL;
379a8353960SJulian Elischer 	struct ngd_connection * tmp;
380a8353960SJulian Elischer 	int error = 0;
381a8353960SJulian Elischer 	struct ng_mesg *msg;
382a8353960SJulian Elischer 	struct ngd_param_s * datap;
383a8353960SJulian Elischer 
384f2b9562cSGleb Smirnoff 	DBG;
385a8353960SJulian Elischer 
386a8353960SJulian Elischer 	NG_MKMESSAGE(msg, NGM_DEVICE_COOKIE, cmd, sizeof(struct ngd_param_s),
387a8353960SJulian Elischer 			M_NOWAIT);
388a8353960SJulian Elischer 	if (msg == NULL) {
389a8353960SJulian Elischer 		printf("%s(): msg == NULL\n",__func__);
390a8353960SJulian Elischer 		goto nomsg;
391a8353960SJulian Elischer 	}
392a8353960SJulian Elischer 
393a8353960SJulian Elischer 	/* pass the ioctl data into the ->data area */
394a8353960SJulian Elischer 	datap = (struct ngd_param_s *)msg->data;
395a8353960SJulian Elischer 	datap->p = addr;
396a8353960SJulian Elischer 
397b3e3ef98SGleb Smirnoff 	NG_SEND_MSG_HOOK(error, sc->node, msg, connection->active_hook, 0);
398a8353960SJulian Elischer 	if(error)
399a8353960SJulian Elischer 		printf("%s(): NG_SEND_MSG_HOOK error: %d\n",__func__,error);
400a8353960SJulian Elischer 
401a8353960SJulian Elischer nomsg:
402a8353960SJulian Elischer 
403a8353960SJulian Elischer 	return(0);
404a8353960SJulian Elischer }
405547d3473SGleb Smirnoff #endif /* if 0 */
406a8353960SJulian Elischer 
407a8353960SJulian Elischer /*
408a8353960SJulian Elischer  * This function is called when a read(2) is done to our device.
409547d3473SGleb Smirnoff  * We process one mbuf from queue.
410a8353960SJulian Elischer  */
411a8353960SJulian Elischer static int
41289c9c53dSPoul-Henning Kamp ngdread(struct cdev *dev, struct uio *uio, int flag)
413a8353960SJulian Elischer {
414547d3473SGleb Smirnoff 	priv_p	priv = (priv_p )dev->si_drv1;
415547d3473SGleb Smirnoff 	struct mbuf *m;
416547d3473SGleb Smirnoff 	int len, error = 0;
417a8353960SJulian Elischer 
418f2b9562cSGleb Smirnoff 	DBG;
419a8353960SJulian Elischer 
420547d3473SGleb Smirnoff 	/* get an mbuf */
421547d3473SGleb Smirnoff 	do {
422547d3473SGleb Smirnoff 		IF_DEQUEUE(&priv->readq, m);
423547d3473SGleb Smirnoff 		if (m == NULL) {
424547d3473SGleb Smirnoff 			if (flag & IO_NDELAY)
425547d3473SGleb Smirnoff 				return (EWOULDBLOCK);
426547d3473SGleb Smirnoff 			mtx_lock(&priv->ngd_mtx);
427547d3473SGleb Smirnoff 			priv->flags |= NGDF_RWAIT;
42847095f77SRoman Kurakin 			if ((error = msleep(priv, &priv->ngd_mtx,
42947095f77SRoman Kurakin 			    PDROP | PCATCH | (PZERO + 1),
430547d3473SGleb Smirnoff 			    "ngdread", 0)) != 0)
431547d3473SGleb Smirnoff 				return (error);
432a8353960SJulian Elischer 		}
433547d3473SGleb Smirnoff 	} while (m == NULL);
434547d3473SGleb Smirnoff 
435547d3473SGleb Smirnoff 	while (m && uio->uio_resid > 0 && error == 0) {
436547d3473SGleb Smirnoff 		len = MIN(uio->uio_resid, m->m_len);
437547d3473SGleb Smirnoff 		if (len != 0)
438547d3473SGleb Smirnoff 			error = uiomove(mtod(m, void *), len, uio);
439547d3473SGleb Smirnoff 		m = m_free(m);
440a8353960SJulian Elischer 	}
441a8353960SJulian Elischer 
442547d3473SGleb Smirnoff 	if (m)
443547d3473SGleb Smirnoff 		m_freem(m);
444a8353960SJulian Elischer 
445547d3473SGleb Smirnoff 	return (error);
446a8353960SJulian Elischer }
447a8353960SJulian Elischer 
448a8353960SJulian Elischer 
449a8353960SJulian Elischer /*
450a8353960SJulian Elischer  * This function is called when our device is written to.
451547d3473SGleb Smirnoff  * We read the data from userland into mbuf chain and pass it to the remote hook.
452a8353960SJulian Elischer  *
453a8353960SJulian Elischer  */
454a8353960SJulian Elischer static int
45589c9c53dSPoul-Henning Kamp ngdwrite(struct cdev *dev, struct uio *uio, int flag)
456a8353960SJulian Elischer {
457547d3473SGleb Smirnoff 	priv_p	priv = (priv_p )dev->si_drv1;
458a8353960SJulian Elischer 	struct mbuf *m;
459547d3473SGleb Smirnoff 	int error = 0;
460a8353960SJulian Elischer 
461f2b9562cSGleb Smirnoff 	DBG;
462a8353960SJulian Elischer 
463547d3473SGleb Smirnoff 	if (uio->uio_resid == 0)
464a8353960SJulian Elischer 		return (0);
465a8353960SJulian Elischer 
466547d3473SGleb Smirnoff 	if (uio->uio_resid < 0 || uio->uio_resid > IP_MAXPACKET)
467547d3473SGleb Smirnoff 		return (EIO);
468a8353960SJulian Elischer 
469eb1b1807SGleb Smirnoff 	if ((m = m_uiotombuf(uio, M_NOWAIT, 0, 0, M_PKTHDR)) == NULL)
470547d3473SGleb Smirnoff 		return (ENOBUFS);
471547d3473SGleb Smirnoff 
472547d3473SGleb Smirnoff 	NG_SEND_DATA_ONLY(error, priv->hook, m);
473547d3473SGleb Smirnoff 
474547d3473SGleb Smirnoff 	return (error);
475a8353960SJulian Elischer }
476a8353960SJulian Elischer 
477a8353960SJulian Elischer /*
478a8353960SJulian Elischer  * we are being polled/selected
479a8353960SJulian Elischer  * check if there is data available for read
480a8353960SJulian Elischer  */
481a8353960SJulian Elischer static int
48289c9c53dSPoul-Henning Kamp ngdpoll(struct cdev *dev, int events, struct thread *td)
483a8353960SJulian Elischer {
484547d3473SGleb Smirnoff 	priv_p	priv = (priv_p )dev->si_drv1;
485a8353960SJulian Elischer 	int revents = 0;
486a8353960SJulian Elischer 
487547d3473SGleb Smirnoff 	if (events & (POLLIN | POLLRDNORM) &&
488547d3473SGleb Smirnoff 	    !IFQ_IS_EMPTY(&priv->readq))
489a8353960SJulian Elischer 		revents |= events & (POLLIN | POLLRDNORM);
490a8353960SJulian Elischer 
491a8353960SJulian Elischer 	return (revents);
492a8353960SJulian Elischer }
493