1/*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3 *
4 * Copyright (c) 2002 Mark Santcroos <marks@ripe.net>
5 * Copyright (c) 2004-2005 Gleb Smirnoff <glebius@FreeBSD.org>
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 *
27 * Netgraph "device" node
28 *
29 * This node presents a /dev/ngd%d device that interfaces to an other
30 * netgraph node.
31 *
32 * $FreeBSD$
33 *
34 */
35
36#if 0
37#define	DBG do { printf("ng_device: %s\n", __func__ ); } while (0)
38#else
39#define	DBG do {} while (0)
40#endif
41
42#include <sys/param.h>
43#include <sys/conf.h>
44#include <sys/ioccom.h>
45#include <sys/kernel.h>
46#include <sys/malloc.h>
47#include <sys/mbuf.h>
48#include <sys/poll.h>
49#include <sys/queue.h>
50#include <sys/socket.h>
51#include <sys/systm.h>
52#include <sys/uio.h>
53#include <sys/vnode.h>
54
55#include <net/if.h>
56#include <net/if_var.h>
57#include <netinet/in.h>
58#include <netinet/in_systm.h>
59#include <netinet/ip.h>
60
61#include <netgraph/ng_message.h>
62#include <netgraph/netgraph.h>
63#include <netgraph/ng_device.h>
64
65#define	ERROUT(x) do { error = (x); goto done; } while (0)
66
67/* Netgraph methods */
68static int		ng_device_mod_event(module_t, int, void *);
69static ng_constructor_t	ng_device_constructor;
70static ng_rcvmsg_t	ng_device_rcvmsg;
71static ng_shutdown_t	ng_device_shutdown;
72static ng_newhook_t	ng_device_newhook;
73static ng_rcvdata_t	ng_device_rcvdata;
74static ng_disconnect_t	ng_device_disconnect;
75
76/* Netgraph type */
77static struct ng_type ngd_typestruct = {
78	.version =	NG_ABI_VERSION,
79	.name =		NG_DEVICE_NODE_TYPE,
80	.mod_event =	ng_device_mod_event,
81	.constructor =	ng_device_constructor,
82	.rcvmsg	=	ng_device_rcvmsg,
83	.shutdown = 	ng_device_shutdown,
84	.newhook =	ng_device_newhook,
85	.rcvdata =	ng_device_rcvdata,
86	.disconnect =	ng_device_disconnect,
87};
88NETGRAPH_INIT(device, &ngd_typestruct);
89
90/* per node data */
91struct ngd_private {
92	struct	ifqueue	readq;
93	struct	ng_node	*node;
94	struct	ng_hook	*hook;
95	struct	cdev	*ngddev;
96	struct	mtx	ngd_mtx;
97	int 		unit;
98	uint16_t	flags;
99#define	NGDF_OPEN	0x0001
100#define	NGDF_RWAIT	0x0002
101};
102typedef struct ngd_private *priv_p;
103
104/* unit number allocator entity */
105static struct unrhdr *ngd_unit;
106
107/* Maximum number of NGD devices */
108#define MAX_NGD	999
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/******************************************************************************
133 *  Netgraph methods
134 ******************************************************************************/
135
136/*
137 * Handle loading and unloading for this node type.
138 */
139static int
140ng_device_mod_event(module_t mod, int event, void *data)
141{
142	int error = 0;
143
144	switch (event) {
145	case MOD_LOAD:
146		ngd_unit = new_unrhdr(0, MAX_NGD, NULL);
147		break;
148	case MOD_UNLOAD:
149		delete_unrhdr(ngd_unit);
150		break;
151	default:
152		error = EOPNOTSUPP;
153		break;
154	}
155	return (error);
156}
157
158/*
159 * create new node
160 */
161static int
162ng_device_constructor(node_p node)
163{
164	priv_p	priv;
165
166	DBG;
167
168	priv = malloc(sizeof(*priv), M_NETGRAPH, M_WAITOK | M_ZERO);
169
170	/* Allocate unit number */
171	priv->unit = alloc_unr(ngd_unit);
172
173	/* Initialize mutexes and queue */
174	mtx_init(&priv->ngd_mtx, "ng_device", NULL, MTX_DEF);
175	mtx_init(&priv->readq.ifq_mtx, "ng_device queue", NULL, MTX_DEF);
176	IFQ_SET_MAXLEN(&priv->readq, ifqmaxlen);
177
178	/* Link everything together */
179	NG_NODE_SET_PRIVATE(node, priv);
180	priv->node = node;
181
182	priv->ngddev = make_dev(&ngd_cdevsw, priv->unit, UID_ROOT,
183	    GID_WHEEL, 0600, NG_DEVICE_DEVNAME "%d", priv->unit);
184	if(priv->ngddev == NULL) {
185		printf("%s(): make_dev() failed\n",__func__);
186		mtx_destroy(&priv->ngd_mtx);
187		mtx_destroy(&priv->readq.ifq_mtx);
188		free_unr(ngd_unit, priv->unit);
189		free(priv, M_NETGRAPH);
190		return(EINVAL);
191	}
192	/* XXX: race here? */
193	priv->ngddev->si_drv1 = priv;
194
195	return(0);
196}
197
198/*
199 * Process control message.
200 */
201
202static int
203ng_device_rcvmsg(node_p node, item_p item, hook_p lasthook)
204{
205	const priv_p priv = NG_NODE_PRIVATE(node);
206	struct ng_mesg *msg;
207	struct ng_mesg *resp = NULL;
208	const char *dn;
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_NOWAIT);
219
220			if (resp == NULL)
221				ERROUT(ENOMEM);
222
223			dn = devtoname(priv->ngddev);
224			strlcpy((char *)resp->data, dn, strlen(dn) + 1);
225			break;
226
227		default:
228			error = EINVAL;
229			break;
230		}
231	} else
232		error = EINVAL;
233
234done:
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 */
243static int
244ng_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 */
262static int
263ng_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_UNLOCK(&priv->readq);
276		NG_FREE_M(m);
277		return (ENOBUFS);
278	}
279
280	_IF_ENQUEUE(&priv->readq, m);
281	IF_UNLOCK(&priv->readq);
282	mtx_lock(&priv->ngd_mtx);
283	if (priv->flags & NGDF_RWAIT) {
284		priv->flags &= ~NGDF_RWAIT;
285		wakeup(priv);
286	}
287	mtx_unlock(&priv->ngd_mtx);
288
289	return(0);
290}
291
292/*
293 * Removal of the hook destroys the node.
294 */
295static int
296ng_device_disconnect(hook_p hook)
297{
298	priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
299
300	DBG;
301
302	destroy_dev(priv->ngddev);
303	mtx_destroy(&priv->ngd_mtx);
304
305	IF_DRAIN(&priv->readq);
306	mtx_destroy(&(priv)->readq.ifq_mtx);
307
308	free_unr(ngd_unit, priv->unit);
309
310	free(priv, M_NETGRAPH);
311
312	ng_rmnode_self(NG_HOOK_NODE(hook));
313
314	return(0);
315}
316
317/*
318 * Node shutdown. Everything is already done in disconnect method.
319 */
320static int
321ng_device_shutdown(node_p node)
322{
323	NG_NODE_UNREF(node);
324	return (0);
325}
326
327/******************************************************************************
328 *  Device methods
329 ******************************************************************************/
330
331/*
332 * the device is opened
333 */
334static int
335ngdopen(struct cdev *dev, int flag, int mode, struct thread *td)
336{
337	priv_p	priv = (priv_p )dev->si_drv1;
338
339	DBG;
340
341	mtx_lock(&priv->ngd_mtx);
342	priv->flags |= NGDF_OPEN;
343	mtx_unlock(&priv->ngd_mtx);
344
345	return(0);
346}
347
348/*
349 * the device is closed
350 */
351static int
352ngdclose(struct cdev *dev, int flag, int mode, struct thread *td)
353{
354	priv_p	priv = (priv_p )dev->si_drv1;
355
356	DBG;
357	mtx_lock(&priv->ngd_mtx);
358	priv->flags &= ~NGDF_OPEN;
359	mtx_unlock(&priv->ngd_mtx);
360
361	return(0);
362}
363
364#if 0	/*
365	 * The ioctl is transformed into netgraph control message.
366	 * We do not process them, yet.
367	 */
368/*
369 * process ioctl
370 *
371 * they are translated into netgraph messages and passed on
372 *
373 */
374static int
375ngdioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
376{
377	struct ngd_softc *sc = &ngd_softc;
378	struct ngd_connection * connection = NULL;
379	struct ngd_connection * tmp;
380	int error = 0;
381	struct ng_mesg *msg;
382	struct ngd_param_s * datap;
383
384	DBG;
385
386	NG_MKMESSAGE(msg, NGM_DEVICE_COOKIE, cmd, sizeof(struct ngd_param_s),
387			M_NOWAIT);
388	if (msg == NULL) {
389		printf("%s(): msg == NULL\n",__func__);
390		goto nomsg;
391	}
392
393	/* pass the ioctl data into the ->data area */
394	datap = (struct ngd_param_s *)msg->data;
395	datap->p = addr;
396
397	NG_SEND_MSG_HOOK(error, sc->node, msg, connection->active_hook, 0);
398	if(error)
399		printf("%s(): NG_SEND_MSG_HOOK error: %d\n",__func__,error);
400
401nomsg:
402
403	return(0);
404}
405#endif /* if 0 */
406
407/*
408 * This function is called when a read(2) is done to our device.
409 * We process one mbuf from queue.
410 */
411static int
412ngdread(struct cdev *dev, struct uio *uio, int flag)
413{
414	priv_p	priv = (priv_p )dev->si_drv1;
415	struct mbuf *m;
416	int len, error = 0;
417
418	DBG;
419
420	/* get an mbuf */
421	do {
422		IF_DEQUEUE(&priv->readq, m);
423		if (m == NULL) {
424			if (flag & IO_NDELAY)
425				return (EWOULDBLOCK);
426			mtx_lock(&priv->ngd_mtx);
427			priv->flags |= NGDF_RWAIT;
428			if ((error = msleep(priv, &priv->ngd_mtx,
429			    PDROP | PCATCH | (PZERO + 1),
430			    "ngdread", 0)) != 0)
431				return (error);
432		}
433	} while (m == NULL);
434
435	while (m && uio->uio_resid > 0 && error == 0) {
436		len = MIN(uio->uio_resid, m->m_len);
437		if (len != 0)
438			error = uiomove(mtod(m, void *), len, uio);
439		m = m_free(m);
440	}
441
442	if (m)
443		m_freem(m);
444
445	return (error);
446}
447
448
449/*
450 * This function is called when our device is written to.
451 * We read the data from userland into mbuf chain and pass it to the remote hook.
452 *
453 */
454static int
455ngdwrite(struct cdev *dev, struct uio *uio, int flag)
456{
457	priv_p	priv = (priv_p )dev->si_drv1;
458	struct mbuf *m;
459	int error = 0;
460
461	DBG;
462
463	if (uio->uio_resid == 0)
464		return (0);
465
466	if (uio->uio_resid < 0 || uio->uio_resid > IP_MAXPACKET)
467		return (EIO);
468
469	if ((m = m_uiotombuf(uio, M_NOWAIT, 0, 0, M_PKTHDR)) == NULL)
470		return (ENOBUFS);
471
472	NG_SEND_DATA_ONLY(error, priv->hook, m);
473
474	return (error);
475}
476
477/*
478 * we are being polled/selected
479 * check if there is data available for read
480 */
481static int
482ngdpoll(struct cdev *dev, int events, struct thread *td)
483{
484	priv_p	priv = (priv_p )dev->si_drv1;
485	int revents = 0;
486
487	if (events & (POLLIN | POLLRDNORM) &&
488	    !IFQ_IS_EMPTY(&priv->readq))
489		revents |= events & (POLLIN | POLLRDNORM);
490
491	return (revents);
492}
493