2 * Copyright (c) 2002 Mark Santcroos <marks@ripe.net>
3 * Copyright (c) 2004-2005 Gleb Smirnoff <glebius@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 * Netgraph "device" node
27 * This node presents a /dev/ngd%d device that interfaces to an other
30 * $FreeBSD: src/sys/netgraph/ng_device.c,v 1.22 2006/11/02 17:37:21 andre Exp $
31 * $DragonFly: src/sys/netgraph7/ng_device.c,v 1.2 2008/06/26 23:05:35 dillon Exp $
36 #define DBG do { printf("ng_device: %s\n", __func__ ); } while (0)
38 #define DBG do {} while (0)
41 #include <sys/param.h>
43 #include <sys/ioccom.h>
44 #include <sys/kernel.h>
45 #include <sys/malloc.h>
48 #include <sys/queue.h>
49 #include <sys/socket.h>
50 #include <sys/systm.h>
52 #include <sys/vnode.h>
55 #include <net/if_var.h>
56 #include <netinet/in.h>
57 #include <netinet/in_systm.h>
58 #include <netinet/ip.h>
60 #include "ng_message.h"
62 #include "ng_device.h"
64 #define ERROUT(x) do { error = (x); goto done; } while (0)
66 /* Netgraph methods */
67 static int ng_device_mod_event(module_t
, int, void *);
68 static ng_constructor_t ng_device_constructor
;
69 static ng_rcvmsg_t ng_device_rcvmsg
;
70 static ng_shutdown_t ng_device_shutdown
;
71 static ng_newhook_t ng_device_newhook
;
72 static ng_rcvdata_t ng_device_rcvdata
;
73 static ng_disconnect_t ng_device_disconnect
;
76 static struct ng_type ngd_typestruct
= {
77 .version
= NG_ABI_VERSION
,
78 .name
= NG_DEVICE_NODE_TYPE
,
79 .mod_event
= ng_device_mod_event
,
80 .constructor
= ng_device_constructor
,
81 .rcvmsg
= ng_device_rcvmsg
,
82 .shutdown
= ng_device_shutdown
,
83 .newhook
= ng_device_newhook
,
84 .rcvdata
= ng_device_rcvdata
,
85 .disconnect
= ng_device_disconnect
,
87 NETGRAPH_INIT(device
, &ngd_typestruct
);
98 #define NGDF_OPEN 0x0001
99 #define NGDF_RWAIT 0x0002
101 typedef struct ngd_private
*priv_p
;
103 /* unit number allocator entity */
104 static struct unrhdr
*ngd_unit
;
106 /* Maximum number of NGD devices */
109 static d_close_t ngdclose
;
110 static d_open_t ngdopen
;
111 static d_read_t ngdread
;
112 static d_write_t ngdwrite
;
114 static d_ioctl_t ngdioctl
;
116 static d_poll_t ngdpoll
;
118 static struct cdevsw ngd_cdevsw
= {
119 .d_version
= D_VERSION
,
128 .d_name
= NG_DEVICE_DEVNAME
,
131 /******************************************************************************
133 ******************************************************************************/
136 * Handle loading and unloading for this node type.
139 ng_device_mod_event(module_t mod
, int event
, void *data
)
145 ngd_unit
= new_unrhdr(0, MAX_NGD
, NULL
);
148 delete_unrhdr(ngd_unit
);
161 ng_device_constructor(node_p node
)
167 MALLOC(priv
, priv_p
, sizeof(*priv
), M_NETGRAPH
, M_WAITOK
| M_ZERO
);
171 /* Allocate unit number */
172 priv
->unit
= alloc_unr(ngd_unit
);
174 /* Initialize mutexes and queue */
175 mtx_init(&priv
->ngd_mtx
, "ng_device", NULL
, MTX_DEF
);
176 mtx_init(&priv
->readq
.ifq_mtx
, "ng_device queue", NULL
, MTX_DEF
);
177 IFQ_SET_MAXLEN(&priv
->readq
, ifqmaxlen
);
179 /* Link everything together */
180 NG_NODE_SET_PRIVATE(node
, priv
);
183 priv
->ngddev
= make_dev(&ngd_cdevsw
, unit2minor(priv
->unit
), UID_ROOT
,
184 GID_WHEEL
, 0600, NG_DEVICE_DEVNAME
"%d", priv
->unit
);
185 if(priv
->ngddev
== NULL
) {
186 printf("%s(): make_dev() failed\n",__func__
);
187 mtx_destroy(&priv
->ngd_mtx
);
188 mtx_destroy(&priv
->readq
.ifq_mtx
);
189 free_unr(ngd_unit
, priv
->unit
);
190 FREE(priv
, M_NETGRAPH
);
193 /* XXX: race here? */
194 priv
->ngddev
->si_drv1
= priv
;
200 * Process control message.
204 ng_device_rcvmsg(node_p node
, item_p item
, hook_p lasthook
)
206 const priv_p priv
= NG_NODE_PRIVATE(node
);
208 struct ng_mesg
*resp
= NULL
;
211 NGI_GET_MSG(item
, msg
);
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_WAITOK
);
223 strlcpy((char *)resp
->data
, priv
->ngddev
->si_name
,
224 strlen(priv
->ngddev
->si_name
) + 1);
235 NG_RESPOND_MSG(error
, node
, item
, resp
);
241 * Accept incoming hook. We support only one hook per node.
244 ng_device_newhook(node_p node
, hook_p hook
, const char *name
)
246 priv_p priv
= NG_NODE_PRIVATE(node
);
250 /* We have only one hook per node */
251 if (priv
->hook
!= NULL
)
260 * Receive data from hook, write it to device.
263 ng_device_rcvdata(hook_p hook
, item_p item
)
265 priv_p priv
= NG_NODE_PRIVATE(NG_HOOK_NODE(hook
));
273 IF_LOCK(&priv
->readq
);
274 if (_IF_QFULL(&priv
->readq
)) {
275 _IF_DROP(&priv
->readq
);
276 IF_UNLOCK(&priv
->readq
);
281 _IF_ENQUEUE(&priv
->readq
, m
);
282 IF_UNLOCK(&priv
->readq
);
283 mtx_lock(&priv
->ngd_mtx
);
284 if (priv
->flags
& NGDF_RWAIT
) {
285 priv
->flags
&= ~NGDF_RWAIT
;
288 mtx_unlock(&priv
->ngd_mtx
);
294 * Removal of the hook destroys the node.
297 ng_device_disconnect(hook_p hook
)
299 priv_p priv
= NG_NODE_PRIVATE(NG_HOOK_NODE(hook
));
303 destroy_dev(priv
->ngddev
);
304 mtx_destroy(&priv
->ngd_mtx
);
306 IF_DRAIN(&priv
->readq
);
307 mtx_destroy(&(priv
)->readq
.ifq_mtx
);
309 free_unr(ngd_unit
, priv
->unit
);
311 FREE(priv
, M_NETGRAPH
);
313 ng_rmnode_self(NG_HOOK_NODE(hook
));
319 * Node shutdown. Everything is already done in disconnect method.
322 ng_device_shutdown(node_p node
)
328 /******************************************************************************
330 ******************************************************************************/
333 * the device is opened
336 ngdopen(struct cdev
*dev
, int flag
, int mode
, struct thread
*td
)
338 priv_p priv
= (priv_p
)dev
->si_drv1
;
342 mtx_lock(&priv
->ngd_mtx
);
343 priv
->flags
|= NGDF_OPEN
;
344 mtx_unlock(&priv
->ngd_mtx
);
350 * the device is closed
353 ngdclose(struct cdev
*dev
, int flag
, int mode
, struct thread
*td
)
355 priv_p priv
= (priv_p
)dev
->si_drv1
;
358 mtx_lock(&priv
->ngd_mtx
);
359 priv
->flags
&= ~NGDF_OPEN
;
360 mtx_unlock(&priv
->ngd_mtx
);
366 * The ioctl is transformed into netgraph control message.
367 * We do not process them, yet.
372 * they are translated into netgraph messages and passed on
376 ngdioctl(struct cdev
*dev
, u_long cmd
, caddr_t addr
, int flag
, struct thread
*td
)
378 struct ngd_softc
*sc
= &ngd_softc
;
379 struct ngd_connection
* connection
= NULL
;
380 struct ngd_connection
* tmp
;
383 struct ngd_param_s
* datap
;
387 NG_MKMESSAGE(msg
, NGM_DEVICE_COOKIE
, cmd
, sizeof(struct ngd_param_s
),
390 printf("%s(): msg == NULL\n",__func__
);
394 /* pass the ioctl data into the ->data area */
395 datap
= (struct ngd_param_s
*)msg
->data
;
398 NG_SEND_MSG_HOOK(error
, sc
->node
, msg
, connection
->active_hook
, 0);
400 printf("%s(): NG_SEND_MSG_HOOK error: %d\n",__func__
,error
);
409 * This function is called when a read(2) is done to our device.
410 * We process one mbuf from queue.
413 ngdread(struct cdev
*dev
, struct uio
*uio
, int flag
)
415 priv_p priv
= (priv_p
)dev
->si_drv1
;
423 IF_DEQUEUE(&priv
->readq
, m
);
425 if (flag
& IO_NDELAY
)
426 return (EWOULDBLOCK
);
427 mtx_lock(&priv
->ngd_mtx
);
428 priv
->flags
|= NGDF_RWAIT
;
429 if ((error
= msleep(priv
, &priv
->ngd_mtx
,
430 PDROP
| PCATCH
| (PZERO
+ 1),
436 while (m
&& uio
->uio_resid
> 0 && error
== 0) {
437 len
= MIN(uio
->uio_resid
, m
->m_len
);
439 error
= uiomove(mtod(m
, void *), len
, uio
);
451 * This function is called when our device is written to.
452 * We read the data from userland into mbuf chain and pass it to the remote hook.
456 ngdwrite(struct cdev
*dev
, struct uio
*uio
, int flag
)
458 priv_p priv
= (priv_p
)dev
->si_drv1
;
464 if (uio
->uio_resid
== 0)
467 if (uio
->uio_resid
< 0 || uio
->uio_resid
> IP_MAXPACKET
)
470 if ((m
= m_uiotombuf(uio
, MB_DONTWAIT
, 0, 0, M_PKTHDR
)) == NULL
)
473 NG_SEND_DATA_ONLY(error
, priv
->hook
, m
);
479 * we are being polled/selected
480 * check if there is data available for read
483 ngdpoll(struct cdev
*dev
, int events
, struct thread
*td
)
485 priv_p priv
= (priv_p
)dev
->si_drv1
;
488 if (events
& (POLLIN
| POLLRDNORM
) &&
489 !IFQ_IS_EMPTY(&priv
->readq
))
490 revents
|= events
& (POLLIN
| POLLRDNORM
);