xref: /freebsd/sys/netgraph/ng_device.c (revision edf8578117e8844e02c0121147f45e4609b30680)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
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  */
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/proc.h>
48 #include <sys/epoch.h>
49 #include <sys/queue.h>
50 #include <sys/socket.h>
51 #include <sys/syslog.h>
52 #include <sys/systm.h>
53 #include <sys/uio.h>
54 #include <sys/vnode.h>
55 
56 #include <net/ethernet.h>
57 #include <net/if.h>
58 #include <net/if_var.h>
59 #include <netinet/in.h>
60 #include <netinet/in_systm.h>
61 #include <netinet/ip.h>
62 
63 #include <netgraph/ng_message.h>
64 #include <netgraph/netgraph.h>
65 #include <netgraph/ng_device.h>
66 #include <netgraph/ng_parse.h>
67 
68 #define	ERROUT(x) do { error = (x); goto done; } while (0)
69 
70 /* Netgraph methods */
71 static int		ng_device_mod_event(module_t, int, void *);
72 static ng_constructor_t	ng_device_constructor;
73 static ng_rcvmsg_t	ng_device_rcvmsg;
74 static ng_shutdown_t	ng_device_shutdown;
75 static ng_newhook_t	ng_device_newhook;
76 static ng_rcvdata_t	ng_device_rcvdata;
77 static ng_disconnect_t	ng_device_disconnect;
78 
79 /* List of commands and how to convert arguments to/from ASCII. */
80 static const struct ng_cmdlist ng_device_cmds[] = {
81 	{
82 	  NGM_DEVICE_COOKIE,
83 	  NGM_DEVICE_GET_DEVNAME,
84 	  "getdevname",
85 	  NULL,
86 	  &ng_parse_string_type
87 	},
88 	{
89 	  NGM_DEVICE_COOKIE,
90 	  NGM_DEVICE_ETHERALIGN,
91 	  "etheralign",
92 	  NULL,
93 	  NULL
94 	},
95 	{ 0 }
96 };
97 
98 /* Netgraph type */
99 static struct ng_type ngd_typestruct = {
100 	.version =	NG_ABI_VERSION,
101 	.name =		NG_DEVICE_NODE_TYPE,
102 	.mod_event =	ng_device_mod_event,
103 	.constructor =	ng_device_constructor,
104 	.rcvmsg	=	ng_device_rcvmsg,
105 	.shutdown = 	ng_device_shutdown,
106 	.newhook =	ng_device_newhook,
107 	.rcvdata =	ng_device_rcvdata,
108 	.disconnect =	ng_device_disconnect,
109 	.cmdlist =	ng_device_cmds,
110 };
111 NETGRAPH_INIT(device, &ngd_typestruct);
112 
113 /* per node data */
114 struct ngd_private {
115 	struct	ifqueue	readq;
116 	struct	ng_node	*node;
117 	struct	ng_hook	*hook;
118 	struct	cdev	*ngddev;
119 	struct	mtx	ngd_mtx;
120 	int 		unit;
121 	int		ether_align;
122 	uint16_t	flags;
123 #define	NGDF_OPEN	0x0001
124 #define	NGDF_RWAIT	0x0002
125 };
126 typedef struct ngd_private *priv_p;
127 
128 /* unit number allocator entity */
129 static struct unrhdr *ngd_unit;
130 
131 /* Maximum number of NGD devices */
132 #define MAX_NGD	999
133 
134 static d_close_t ngdclose;
135 static d_open_t ngdopen;
136 static d_read_t ngdread;
137 static d_write_t ngdwrite;
138 #if 0
139 static d_ioctl_t ngdioctl;
140 #endif
141 static d_poll_t ngdpoll;
142 
143 static struct cdevsw ngd_cdevsw = {
144 	.d_version =	D_VERSION,
145 	.d_open =	ngdopen,
146 	.d_close =	ngdclose,
147 	.d_read =	ngdread,
148 	.d_write =	ngdwrite,
149 #if 0
150 	.d_ioctl =	ngdioctl,
151 #endif
152 	.d_poll =	ngdpoll,
153 	.d_name =	NG_DEVICE_DEVNAME,
154 };
155 
156 /******************************************************************************
157  *  Netgraph methods
158  ******************************************************************************/
159 
160 /*
161  * Handle loading and unloading for this node type.
162  */
163 static int
164 ng_device_mod_event(module_t mod, int event, void *data)
165 {
166 	int error = 0;
167 
168 	switch (event) {
169 	case MOD_LOAD:
170 		ngd_unit = new_unrhdr(0, MAX_NGD, NULL);
171 		break;
172 	case MOD_UNLOAD:
173 		delete_unrhdr(ngd_unit);
174 		break;
175 	default:
176 		error = EOPNOTSUPP;
177 		break;
178 	}
179 	return (error);
180 }
181 
182 /*
183  * create new node
184  */
185 static int
186 ng_device_constructor(node_p node)
187 {
188 	priv_p	priv;
189 
190 	DBG;
191 
192 	priv = malloc(sizeof(*priv), M_NETGRAPH, M_WAITOK | M_ZERO);
193 
194 	/* Allocate unit number */
195 	priv->unit = alloc_unr(ngd_unit);
196 
197 	/* Initialize mutexes and queue */
198 	mtx_init(&priv->ngd_mtx, "ng_device", NULL, MTX_DEF);
199 	mtx_init(&priv->readq.ifq_mtx, "ng_device queue", NULL, MTX_DEF);
200 	IFQ_SET_MAXLEN(&priv->readq, ifqmaxlen);
201 
202 	/* Link everything together */
203 	NG_NODE_SET_PRIVATE(node, priv);
204 	priv->node = node;
205 
206 	priv->ngddev = make_dev(&ngd_cdevsw, priv->unit, UID_ROOT,
207 	    GID_WHEEL, 0600, NG_DEVICE_DEVNAME "%d", priv->unit);
208 	if(priv->ngddev == NULL) {
209 		printf("%s(): make_dev() failed\n",__func__);
210 		mtx_destroy(&priv->ngd_mtx);
211 		mtx_destroy(&priv->readq.ifq_mtx);
212 		free_unr(ngd_unit, priv->unit);
213 		free(priv, M_NETGRAPH);
214 		return(EINVAL);
215 	}
216 	/* XXX: race here? */
217 	priv->ngddev->si_drv1 = priv;
218 
219 	/* Give this node the same name as the device (if possible). */
220 	if (ng_name_node(node, devtoname(priv->ngddev)) != 0)
221 		log(LOG_WARNING, "%s: can't acquire netgraph name\n",
222 		    devtoname(priv->ngddev));
223 
224 	return(0);
225 }
226 
227 /*
228  * Process control message.
229  */
230 
231 static int
232 ng_device_rcvmsg(node_p node, item_p item, hook_p lasthook)
233 {
234 	const priv_p priv = NG_NODE_PRIVATE(node);
235 	struct ng_mesg *msg;
236 	struct ng_mesg *resp = NULL;
237 	const char *dn;
238 	int error = 0;
239 
240 	NGI_GET_MSG(item, msg);
241 
242 	if (msg->header.typecookie == NGM_DEVICE_COOKIE) {
243 		switch (msg->header.cmd) {
244 		case NGM_DEVICE_GET_DEVNAME:
245 			/* XXX: Fix when MAX_NGD us bigger */
246 			NG_MKRESPONSE(resp, msg,
247 			    strlen(NG_DEVICE_DEVNAME) + 4, M_NOWAIT);
248 
249 			if (resp == NULL)
250 				ERROUT(ENOMEM);
251 
252 			dn = devtoname(priv->ngddev);
253 			strlcpy((char *)resp->data, dn, strlen(dn) + 1);
254 			break;
255 
256 		case NGM_DEVICE_ETHERALIGN:
257 			/* Use ETHER_ALIGN on arches that require it. */
258 #ifndef __NO_STRICT_ALIGNMENT
259 			priv->ether_align = ETHER_ALIGN;
260 #endif
261 			break;
262 
263 		default:
264 			error = EINVAL;
265 			break;
266 		}
267 	} else
268 		error = EINVAL;
269 
270 done:
271 	NG_RESPOND_MSG(error, node, item, resp);
272 	NG_FREE_MSG(msg);
273 	return (error);
274 }
275 
276 /*
277  * Accept incoming hook. We support only one hook per node.
278  */
279 static int
280 ng_device_newhook(node_p node, hook_p hook, const char *name)
281 {
282 	priv_p priv = NG_NODE_PRIVATE(node);
283 
284 	DBG;
285 
286 	/* We have only one hook per node */
287 	if (priv->hook != NULL)
288 		return (EISCONN);
289 
290 	priv->hook = hook;
291 
292 	return(0);
293 }
294 
295 /*
296  * Receive data from hook, write it to device.
297  */
298 static int
299 ng_device_rcvdata(hook_p hook, item_p item)
300 {
301 	priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
302 	struct mbuf *m;
303 
304 	DBG;
305 
306 	NGI_GET_M(item, m);
307 	NG_FREE_ITEM(item);
308 
309 	IF_LOCK(&priv->readq);
310 	if (_IF_QFULL(&priv->readq)) {
311 		IF_UNLOCK(&priv->readq);
312 		NG_FREE_M(m);
313 		return (ENOBUFS);
314 	}
315 
316 	_IF_ENQUEUE(&priv->readq, m);
317 	IF_UNLOCK(&priv->readq);
318 	mtx_lock(&priv->ngd_mtx);
319 	if (priv->flags & NGDF_RWAIT) {
320 		priv->flags &= ~NGDF_RWAIT;
321 		wakeup(priv);
322 	}
323 	mtx_unlock(&priv->ngd_mtx);
324 
325 	return(0);
326 }
327 
328 /*
329  * Removal of the hook destroys the node.
330  */
331 static int
332 ng_device_disconnect(hook_p hook)
333 {
334 	priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
335 
336 	DBG;
337 
338 	destroy_dev(priv->ngddev);
339 	mtx_destroy(&priv->ngd_mtx);
340 
341 	IF_DRAIN(&priv->readq);
342 	mtx_destroy(&(priv)->readq.ifq_mtx);
343 
344 	free_unr(ngd_unit, priv->unit);
345 
346 	free(priv, M_NETGRAPH);
347 
348 	ng_rmnode_self(NG_HOOK_NODE(hook));
349 
350 	return(0);
351 }
352 
353 /*
354  * Node shutdown. Everything is already done in disconnect method.
355  */
356 static int
357 ng_device_shutdown(node_p node)
358 {
359 	NG_NODE_UNREF(node);
360 	return (0);
361 }
362 
363 /******************************************************************************
364  *  Device methods
365  ******************************************************************************/
366 
367 /*
368  * the device is opened
369  */
370 static int
371 ngdopen(struct cdev *dev, int flag, int mode, struct thread *td)
372 {
373 	priv_p	priv = (priv_p )dev->si_drv1;
374 
375 	DBG;
376 
377 	mtx_lock(&priv->ngd_mtx);
378 	priv->flags |= NGDF_OPEN;
379 	mtx_unlock(&priv->ngd_mtx);
380 
381 	return(0);
382 }
383 
384 /*
385  * the device is closed
386  */
387 static int
388 ngdclose(struct cdev *dev, int flag, int mode, struct thread *td)
389 {
390 	priv_p	priv = (priv_p )dev->si_drv1;
391 
392 	DBG;
393 	mtx_lock(&priv->ngd_mtx);
394 	priv->flags &= ~NGDF_OPEN;
395 	mtx_unlock(&priv->ngd_mtx);
396 
397 	return(0);
398 }
399 
400 #if 0	/*
401 	 * The ioctl is transformed into netgraph control message.
402 	 * We do not process them, yet.
403 	 */
404 /*
405  * process ioctl
406  *
407  * they are translated into netgraph messages and passed on
408  *
409  */
410 static int
411 ngdioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
412 {
413 	struct ngd_softc *sc = &ngd_softc;
414 	struct ngd_connection * connection = NULL;
415 	struct ngd_connection * tmp;
416 	int error = 0;
417 	struct ng_mesg *msg;
418 	struct ngd_param_s * datap;
419 
420 	DBG;
421 
422 	NG_MKMESSAGE(msg, NGM_DEVICE_COOKIE, cmd, sizeof(struct ngd_param_s),
423 			M_NOWAIT);
424 	if (msg == NULL) {
425 		printf("%s(): msg == NULL\n",__func__);
426 		goto nomsg;
427 	}
428 
429 	/* pass the ioctl data into the ->data area */
430 	datap = (struct ngd_param_s *)msg->data;
431 	datap->p = addr;
432 
433 	NG_SEND_MSG_HOOK(error, sc->node, msg, connection->active_hook, 0);
434 	if(error)
435 		printf("%s(): NG_SEND_MSG_HOOK error: %d\n",__func__,error);
436 
437 nomsg:
438 
439 	return(0);
440 }
441 #endif /* if 0 */
442 
443 /*
444  * This function is called when a read(2) is done to our device.
445  * We process one mbuf from queue.
446  */
447 static int
448 ngdread(struct cdev *dev, struct uio *uio, int flag)
449 {
450 	priv_p	priv = (priv_p )dev->si_drv1;
451 	struct mbuf *m;
452 	int len, error = 0;
453 
454 	DBG;
455 
456 	/* get an mbuf */
457 	do {
458 		IF_DEQUEUE(&priv->readq, m);
459 		if (m == NULL) {
460 			if (flag & IO_NDELAY)
461 				return (EWOULDBLOCK);
462 			mtx_lock(&priv->ngd_mtx);
463 			priv->flags |= NGDF_RWAIT;
464 			if ((error = msleep(priv, &priv->ngd_mtx,
465 			    PDROP | PCATCH | (PZERO + 1),
466 			    "ngdread", 0)) != 0)
467 				return (error);
468 		}
469 	} while (m == NULL);
470 
471 	while (m && uio->uio_resid > 0 && error == 0) {
472 		len = MIN(uio->uio_resid, m->m_len);
473 		if (len != 0)
474 			error = uiomove(mtod(m, void *), len, uio);
475 		m = m_free(m);
476 	}
477 
478 	if (m)
479 		m_freem(m);
480 
481 	return (error);
482 }
483 
484 /*
485  * This function is called when our device is written to.
486  * We read the data from userland into mbuf chain and pass it to the remote hook.
487  *
488  */
489 static int
490 ngdwrite(struct cdev *dev, struct uio *uio, int flag)
491 {
492 	struct epoch_tracker et;
493 	priv_p	priv = (priv_p )dev->si_drv1;
494 	struct mbuf *m;
495 	int error = 0;
496 
497 	DBG;
498 
499 	if (uio->uio_resid == 0)
500 		return (0);
501 
502 	if (uio->uio_resid < 0 || uio->uio_resid > IP_MAXPACKET)
503 		return (EIO);
504 
505 	m = m_uiotombuf(uio, M_NOWAIT, 0, priv->ether_align, M_PKTHDR);
506 	if (m == NULL)
507 		return (ENOBUFS);
508 
509 	NET_EPOCH_ENTER(et);
510 	NG_SEND_DATA_ONLY(error, priv->hook, m);
511 	NET_EPOCH_EXIT(et);
512 
513 	return (error);
514 }
515 
516 /*
517  * we are being polled/selected
518  * check if there is data available for read
519  */
520 static int
521 ngdpoll(struct cdev *dev, int events, struct thread *td)
522 {
523 	priv_p	priv = (priv_p )dev->si_drv1;
524 	int revents = 0;
525 
526 	if (events & (POLLIN | POLLRDNORM) &&
527 	    !IFQ_IS_EMPTY(&priv->readq))
528 		revents |= events & (POLLIN | POLLRDNORM);
529 
530 	return (revents);
531 }
532