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