2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 2002 Mark Santcroos <marks@ripe.net>
5 * Copyright (c) 2004-2005 Gleb Smirnoff <glebius@FreeBSD.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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.
27 * Netgraph "device" node
29 * This node presents a /dev/ngd%d device that interfaces to an other
35 #define DBG do { printf("ng_device: %s\n", __func__ ); } while (0)
37 #define DBG do {} while (0)
40 #include <sys/param.h>
42 #include <sys/ioccom.h>
43 #include <sys/kernel.h>
44 #include <sys/malloc.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>
54 #include <sys/vnode.h>
56 #include <net/ethernet.h>
58 #include <net/if_var.h>
59 #include <netinet/in.h>
60 #include <netinet/in_systm.h>
61 #include <netinet/ip.h>
63 #include <netgraph/ng_message.h>
64 #include <netgraph/netgraph.h>
65 #include <netgraph/ng_device.h>
66 #include <netgraph/ng_parse.h>
68 #define ERROUT(x) do { error = (x); goto done; } while (0)
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;
79 /* List of commands and how to convert arguments to/from ASCII. */
80 static const struct ng_cmdlist ng_device_cmds[] = {
83 NGM_DEVICE_GET_DEVNAME,
90 NGM_DEVICE_ETHERALIGN,
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,
111 NETGRAPH_INIT(device, &ngd_typestruct);
115 struct ifqueue readq;
116 struct ng_node *node;
117 struct ng_hook *hook;
123 #define NGDF_OPEN 0x0001
124 #define NGDF_RWAIT 0x0002
126 typedef struct ngd_private *priv_p;
128 /* unit number allocator entity */
129 static struct unrhdr *ngd_unit;
131 /* Maximum number of NGD devices */
134 static d_close_t ngdclose;
135 static d_open_t ngdopen;
136 static d_read_t ngdread;
137 static d_write_t ngdwrite;
139 static d_ioctl_t ngdioctl;
141 static d_poll_t ngdpoll;
143 static struct cdevsw ngd_cdevsw = {
144 .d_version = D_VERSION,
153 .d_name = NG_DEVICE_DEVNAME,
156 /******************************************************************************
158 ******************************************************************************/
161 * Handle loading and unloading for this node type.
164 ng_device_mod_event(module_t mod, int event, void *data)
170 ngd_unit = new_unrhdr(0, MAX_NGD, NULL);
173 delete_unrhdr(ngd_unit);
186 ng_device_constructor(node_p node)
192 priv = malloc(sizeof(*priv), M_NETGRAPH, M_WAITOK | M_ZERO);
194 /* Allocate unit number */
195 priv->unit = alloc_unr(ngd_unit);
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);
202 /* Link everything together */
203 NG_NODE_SET_PRIVATE(node, priv);
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);
216 /* XXX: race here? */
217 priv->ngddev->si_drv1 = priv;
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));
228 * Process control message.
232 ng_device_rcvmsg(node_p node, item_p item, hook_p lasthook)
234 const priv_p priv = NG_NODE_PRIVATE(node);
236 struct ng_mesg *resp = NULL;
240 NGI_GET_MSG(item, msg);
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);
252 dn = devtoname(priv->ngddev);
253 strlcpy((char *)resp->data, dn, strlen(dn) + 1);
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;
271 NG_RESPOND_MSG(error, node, item, resp);
277 * Accept incoming hook. We support only one hook per node.
280 ng_device_newhook(node_p node, hook_p hook, const char *name)
282 priv_p priv = NG_NODE_PRIVATE(node);
286 /* We have only one hook per node */
287 if (priv->hook != NULL)
296 * Receive data from hook, write it to device.
299 ng_device_rcvdata(hook_p hook, item_p item)
301 priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
309 IF_LOCK(&priv->readq);
310 if (_IF_QFULL(&priv->readq)) {
311 IF_UNLOCK(&priv->readq);
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;
323 mtx_unlock(&priv->ngd_mtx);
329 * Removal of the hook destroys the node.
332 ng_device_disconnect(hook_p hook)
334 priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
338 destroy_dev(priv->ngddev);
339 mtx_destroy(&priv->ngd_mtx);
341 IF_DRAIN(&priv->readq);
342 mtx_destroy(&(priv)->readq.ifq_mtx);
344 free_unr(ngd_unit, priv->unit);
346 free(priv, M_NETGRAPH);
348 ng_rmnode_self(NG_HOOK_NODE(hook));
354 * Node shutdown. Everything is already done in disconnect method.
357 ng_device_shutdown(node_p node)
363 /******************************************************************************
365 ******************************************************************************/
368 * the device is opened
371 ngdopen(struct cdev *dev, int flag, int mode, struct thread *td)
373 priv_p priv = (priv_p )dev->si_drv1;
377 mtx_lock(&priv->ngd_mtx);
378 priv->flags |= NGDF_OPEN;
379 mtx_unlock(&priv->ngd_mtx);
385 * the device is closed
388 ngdclose(struct cdev *dev, int flag, int mode, struct thread *td)
390 priv_p priv = (priv_p )dev->si_drv1;
393 mtx_lock(&priv->ngd_mtx);
394 priv->flags &= ~NGDF_OPEN;
395 mtx_unlock(&priv->ngd_mtx);
401 * The ioctl is transformed into netgraph control message.
402 * We do not process them, yet.
407 * they are translated into netgraph messages and passed on
411 ngdioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
413 struct ngd_softc *sc = &ngd_softc;
414 struct ngd_connection * connection = NULL;
415 struct ngd_connection * tmp;
418 struct ngd_param_s * datap;
422 NG_MKMESSAGE(msg, NGM_DEVICE_COOKIE, cmd, sizeof(struct ngd_param_s),
425 printf("%s(): msg == NULL\n",__func__);
429 /* pass the ioctl data into the ->data area */
430 datap = (struct ngd_param_s *)msg->data;
433 NG_SEND_MSG_HOOK(error, sc->node, msg, connection->active_hook, 0);
435 printf("%s(): NG_SEND_MSG_HOOK error: %d\n",__func__,error);
444 * This function is called when a read(2) is done to our device.
445 * We process one mbuf from queue.
448 ngdread(struct cdev *dev, struct uio *uio, int flag)
450 priv_p priv = (priv_p )dev->si_drv1;
458 IF_DEQUEUE(&priv->readq, m);
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),
471 while (m && uio->uio_resid > 0 && error == 0) {
472 len = MIN(uio->uio_resid, m->m_len);
474 error = uiomove(mtod(m, void *), len, uio);
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.
490 ngdwrite(struct cdev *dev, struct uio *uio, int flag)
492 struct epoch_tracker et;
493 priv_p priv = (priv_p )dev->si_drv1;
499 if (uio->uio_resid == 0)
502 if (uio->uio_resid < 0 || uio->uio_resid > IP_MAXPACKET)
505 m = m_uiotombuf(uio, M_NOWAIT, 0, priv->ether_align, M_PKTHDR);
510 NG_SEND_DATA_ONLY(error, priv->hook, m);
517 * we are being polled/selected
518 * check if there is data available for read
521 ngdpoll(struct cdev *dev, int events, struct thread *td)
523 priv_p priv = (priv_p )dev->si_drv1;
526 if (events & (POLLIN | POLLRDNORM) &&
527 !IFQ_IS_EMPTY(&priv->readq))
528 revents |= events & (POLLIN | POLLRDNORM);