1c398230bSWarner Losh /*- 2a8353960SJulian Elischer * Copyright (c) 2002 Mark Santcroos <marks@ripe.net> 35cdd064dSGleb Smirnoff * Copyright (c) 2004-2005 Gleb Smirnoff <glebius@FreeBSD.org> 4a8353960SJulian Elischer * 5a8353960SJulian Elischer * Redistribution and use in source and binary forms, with or without 6a8353960SJulian Elischer * modification, are permitted provided that the following conditions 7a8353960SJulian Elischer * are met: 8a8353960SJulian Elischer * 1. Redistributions of source code must retain the above copyright 9a8353960SJulian Elischer * notice, this list of conditions and the following disclaimer. 10a8353960SJulian Elischer * 2. Redistributions in binary form must reproduce the above copyright 11a8353960SJulian Elischer * notice, this list of conditions and the following disclaimer in the 12a8353960SJulian Elischer * documentation and/or other materials provided with the distribution. 13a8353960SJulian Elischer * 14a8353960SJulian Elischer * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 15a8353960SJulian Elischer * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 16a8353960SJulian Elischer * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 17a8353960SJulian Elischer * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 18a8353960SJulian Elischer * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 19a8353960SJulian Elischer * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 20a8353960SJulian Elischer * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 21a8353960SJulian Elischer * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22a8353960SJulian Elischer * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 23a8353960SJulian Elischer * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24a8353960SJulian Elischer * 25a8353960SJulian Elischer * Netgraph "device" node 26a8353960SJulian Elischer * 27a8353960SJulian Elischer * This node presents a /dev/ngd%d device that interfaces to an other 28a8353960SJulian Elischer * netgraph node. 29a8353960SJulian Elischer * 30a8353960SJulian Elischer * $FreeBSD$ 31a8353960SJulian Elischer * 32a8353960SJulian Elischer */ 33a8353960SJulian Elischer 34547d3473SGleb Smirnoff #if 0 35ec774932SGleb Smirnoff #define DBG do { printf("ng_device: %s\n", __func__ ); } while (0) 36547d3473SGleb Smirnoff #else 37ec774932SGleb Smirnoff #define DBG do {} while (0) 38547d3473SGleb Smirnoff #endif 39547d3473SGleb Smirnoff 40a8353960SJulian Elischer #include <sys/param.h> 41a8353960SJulian Elischer #include <sys/conf.h> 42a8353960SJulian Elischer #include <sys/ioccom.h> 43b3e3ef98SGleb Smirnoff #include <sys/kernel.h> 44b3e3ef98SGleb Smirnoff #include <sys/malloc.h> 45b3e3ef98SGleb Smirnoff #include <sys/mbuf.h> 46b3e3ef98SGleb Smirnoff #include <sys/poll.h> 47b3e3ef98SGleb Smirnoff #include <sys/queue.h> 48547d3473SGleb Smirnoff #include <sys/socket.h> 49b3e3ef98SGleb Smirnoff #include <sys/systm.h> 50b3e3ef98SGleb Smirnoff #include <sys/uio.h> 51547d3473SGleb Smirnoff #include <sys/vnode.h> 52547d3473SGleb Smirnoff 53547d3473SGleb Smirnoff #include <net/if.h> 54547d3473SGleb Smirnoff #include <net/if_var.h> 55547d3473SGleb Smirnoff #include <netinet/in.h> 56547d3473SGleb Smirnoff #include <netinet/in_systm.h> 57547d3473SGleb Smirnoff #include <netinet/ip.h> 58a8353960SJulian Elischer 59a8353960SJulian Elischer #include <netgraph/ng_message.h> 60a8353960SJulian Elischer #include <netgraph/netgraph.h> 61b3e3ef98SGleb Smirnoff #include <netgraph/ng_device.h> 62a8353960SJulian Elischer 63c1eec6c5SGleb Smirnoff #define ERROUT(x) do { error = (x); goto done; } while (0) 64c1eec6c5SGleb Smirnoff 65a8353960SJulian Elischer /* Netgraph methods */ 6630aabc9aSRuslan Ermilov static int ng_device_mod_event(module_t, int, void *); 67547d3473SGleb Smirnoff static ng_constructor_t ng_device_constructor; 68a8353960SJulian Elischer static ng_rcvmsg_t ng_device_rcvmsg; 69547d3473SGleb Smirnoff static ng_shutdown_t ng_device_shutdown; 70a8353960SJulian Elischer static ng_newhook_t ng_device_newhook; 71a8353960SJulian Elischer static ng_rcvdata_t ng_device_rcvdata; 72a8353960SJulian Elischer static ng_disconnect_t ng_device_disconnect; 73a8353960SJulian Elischer 74a8353960SJulian Elischer /* Netgraph type */ 75547d3473SGleb Smirnoff static struct ng_type ngd_typestruct = { 76f8aae777SJulian Elischer .version = NG_ABI_VERSION, 77f8aae777SJulian Elischer .name = NG_DEVICE_NODE_TYPE, 7830aabc9aSRuslan Ermilov .mod_event = ng_device_mod_event, 79547d3473SGleb Smirnoff .constructor = ng_device_constructor, 80f8aae777SJulian Elischer .rcvmsg = ng_device_rcvmsg, 81547d3473SGleb Smirnoff .shutdown = ng_device_shutdown, 82f8aae777SJulian Elischer .newhook = ng_device_newhook, 83f8aae777SJulian Elischer .rcvdata = ng_device_rcvdata, 84f8aae777SJulian Elischer .disconnect = ng_device_disconnect, 85a8353960SJulian Elischer }; 86547d3473SGleb Smirnoff NETGRAPH_INIT(device, &ngd_typestruct); 87a8353960SJulian Elischer 88547d3473SGleb Smirnoff /* per node data */ 89547d3473SGleb Smirnoff struct ngd_private { 90547d3473SGleb Smirnoff struct ifqueue readq; 91547d3473SGleb Smirnoff struct ng_node *node; 92547d3473SGleb Smirnoff struct ng_hook *hook; 9389c9c53dSPoul-Henning Kamp struct cdev *ngddev; 94547d3473SGleb Smirnoff struct mtx ngd_mtx; 95a8353960SJulian Elischer int unit; 96547d3473SGleb Smirnoff uint16_t flags; 97547d3473SGleb Smirnoff #define NGDF_OPEN 0x0001 98547d3473SGleb Smirnoff #define NGDF_RWAIT 0x0002 99a8353960SJulian Elischer }; 100547d3473SGleb Smirnoff typedef struct ngd_private *priv_p; 101a8353960SJulian Elischer 1025cdd064dSGleb Smirnoff /* unit number allocator entity */ 1035cdd064dSGleb Smirnoff static struct unrhdr *ngd_unit; 104a8353960SJulian Elischer 105a8353960SJulian Elischer /* Maximum number of NGD devices */ 1065cdd064dSGleb Smirnoff #define MAX_NGD 999 107a8353960SJulian Elischer 108a8353960SJulian Elischer static d_close_t ngdclose; 109a8353960SJulian Elischer static d_open_t ngdopen; 110a8353960SJulian Elischer static d_read_t ngdread; 111a8353960SJulian Elischer static d_write_t ngdwrite; 112547d3473SGleb Smirnoff #if 0 113a8353960SJulian Elischer static d_ioctl_t ngdioctl; 114547d3473SGleb Smirnoff #endif 115a8353960SJulian Elischer static d_poll_t ngdpoll; 116a8353960SJulian Elischer 117a8353960SJulian Elischer static struct cdevsw ngd_cdevsw = { 118dc08ffecSPoul-Henning Kamp .d_version = D_VERSION, 1197ac40f5fSPoul-Henning Kamp .d_open = ngdopen, 1207ac40f5fSPoul-Henning Kamp .d_close = ngdclose, 1217ac40f5fSPoul-Henning Kamp .d_read = ngdread, 1227ac40f5fSPoul-Henning Kamp .d_write = ngdwrite, 123547d3473SGleb Smirnoff #if 0 1247ac40f5fSPoul-Henning Kamp .d_ioctl = ngdioctl, 125547d3473SGleb Smirnoff #endif 1267ac40f5fSPoul-Henning Kamp .d_poll = ngdpoll, 127547d3473SGleb Smirnoff .d_name = NG_DEVICE_DEVNAME, 128a8353960SJulian Elischer }; 129a8353960SJulian Elischer 130547d3473SGleb Smirnoff /****************************************************************************** 131547d3473SGleb Smirnoff * Netgraph methods 132547d3473SGleb Smirnoff ******************************************************************************/ 133547d3473SGleb Smirnoff 134a8353960SJulian Elischer /* 1355cdd064dSGleb Smirnoff * Handle loading and unloading for this node type. 1365cdd064dSGleb Smirnoff */ 1375cdd064dSGleb Smirnoff static int 1385cdd064dSGleb Smirnoff ng_device_mod_event(module_t mod, int event, void *data) 1395cdd064dSGleb Smirnoff { 1405cdd064dSGleb Smirnoff int error = 0; 1415cdd064dSGleb Smirnoff 1425cdd064dSGleb Smirnoff switch (event) { 1435cdd064dSGleb Smirnoff case MOD_LOAD: 1445cdd064dSGleb Smirnoff ngd_unit = new_unrhdr(0, MAX_NGD, NULL); 1455cdd064dSGleb Smirnoff break; 1465cdd064dSGleb Smirnoff case MOD_UNLOAD: 1475cdd064dSGleb Smirnoff delete_unrhdr(ngd_unit); 1485cdd064dSGleb Smirnoff break; 1495cdd064dSGleb Smirnoff default: 1505cdd064dSGleb Smirnoff error = EOPNOTSUPP; 1515cdd064dSGleb Smirnoff break; 1525cdd064dSGleb Smirnoff } 1535cdd064dSGleb Smirnoff return (error); 1545cdd064dSGleb Smirnoff } 1555cdd064dSGleb Smirnoff 1565cdd064dSGleb Smirnoff /* 157547d3473SGleb Smirnoff * create new node 158a8353960SJulian Elischer */ 159a8353960SJulian Elischer static int 160547d3473SGleb Smirnoff ng_device_constructor(node_p node) 161a8353960SJulian Elischer { 162547d3473SGleb Smirnoff priv_p priv; 163a8353960SJulian Elischer 164f2b9562cSGleb Smirnoff DBG; 165a8353960SJulian Elischer 166547d3473SGleb Smirnoff MALLOC(priv, priv_p, sizeof(*priv), M_NETGRAPH, M_NOWAIT | M_ZERO); 167547d3473SGleb Smirnoff if (priv == NULL) 168547d3473SGleb Smirnoff return (ENOMEM); 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 182843cfd5aSGleb Smirnoff priv->ngddev = make_dev(&ngd_cdevsw, unit2minor(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); 189843cfd5aSGleb Smirnoff 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; 208a8353960SJulian Elischer int error = 0; 209a8353960SJulian Elischer 210a8353960SJulian Elischer NGI_GET_MSG(item, msg); 211a8353960SJulian Elischer 212547d3473SGleb Smirnoff if (msg->header.typecookie == NGM_DEVICE_COOKIE) { 213547d3473SGleb Smirnoff switch (msg->header.cmd) { 214547d3473SGleb Smirnoff case NGM_DEVICE_GET_DEVNAME: 2155cdd064dSGleb Smirnoff /* XXX: Fix when MAX_NGD us bigger */ 216547d3473SGleb Smirnoff NG_MKRESPONSE(resp, msg, 2175cdd064dSGleb Smirnoff strlen(NG_DEVICE_DEVNAME) + 4, M_NOWAIT); 218a8353960SJulian Elischer 219547d3473SGleb Smirnoff if (resp == NULL) 220547d3473SGleb Smirnoff ERROUT(ENOMEM); 221547d3473SGleb Smirnoff 222547d3473SGleb Smirnoff strlcpy((char *)resp->data, priv->ngddev->si_name, 223547d3473SGleb Smirnoff strlen(priv->ngddev->si_name) + 1); 224547d3473SGleb Smirnoff break; 225547d3473SGleb Smirnoff 226547d3473SGleb Smirnoff default: 227547d3473SGleb Smirnoff error = EINVAL; 228547d3473SGleb Smirnoff break; 229547d3473SGleb Smirnoff } 230547d3473SGleb Smirnoff } else 231547d3473SGleb Smirnoff error = EINVAL; 232547d3473SGleb Smirnoff 233547d3473SGleb Smirnoff done: 234547d3473SGleb Smirnoff NG_RESPOND_MSG(error, node, item, resp); 235547d3473SGleb Smirnoff NG_FREE_MSG(msg); 236a8353960SJulian Elischer return (error); 237a8353960SJulian Elischer } 238a8353960SJulian Elischer 239a8353960SJulian Elischer /* 240547d3473SGleb Smirnoff * Accept incoming hook. We support only one hook per node. 241a8353960SJulian Elischer */ 242a8353960SJulian Elischer static int 243a8353960SJulian Elischer ng_device_newhook(node_p node, hook_p hook, const char *name) 244a8353960SJulian Elischer { 245547d3473SGleb Smirnoff priv_p priv = NG_NODE_PRIVATE(node); 246a8353960SJulian Elischer 247f2b9562cSGleb Smirnoff DBG; 248a8353960SJulian Elischer 249547d3473SGleb Smirnoff /* We have only one hook per node */ 250547d3473SGleb Smirnoff if (priv->hook != NULL) 251547d3473SGleb Smirnoff return (EISCONN); 252a8353960SJulian Elischer 253547d3473SGleb Smirnoff priv->hook = hook; 254a8353960SJulian Elischer 255a8353960SJulian Elischer return(0); 256a8353960SJulian Elischer } 257a8353960SJulian Elischer 258a8353960SJulian Elischer /* 259547d3473SGleb Smirnoff * Receive data from hook, write it to device. 260a8353960SJulian Elischer */ 261a8353960SJulian Elischer static int 262a8353960SJulian Elischer ng_device_rcvdata(hook_p hook, item_p item) 263a8353960SJulian Elischer { 264547d3473SGleb Smirnoff priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook)); 265a8353960SJulian Elischer struct mbuf *m; 266a8353960SJulian Elischer 267f2b9562cSGleb Smirnoff DBG; 268a8353960SJulian Elischer 269a8353960SJulian Elischer NGI_GET_M(item, m); 270a8353960SJulian Elischer NG_FREE_ITEM(item); 271a8353960SJulian Elischer 272547d3473SGleb Smirnoff IF_LOCK(&priv->readq); 273547d3473SGleb Smirnoff if (_IF_QFULL(&priv->readq)) { 274547d3473SGleb Smirnoff _IF_DROP(&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 310547d3473SGleb Smirnoff 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 469547d3473SGleb Smirnoff if ((m = m_uiotombuf(uio, M_DONTWAIT, 0)) == 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