1*9278355dSNuno Antunes /*
2*9278355dSNuno Antunes * ng_echo.c
3*9278355dSNuno Antunes */
4*9278355dSNuno Antunes
5*9278355dSNuno Antunes /*-
6*9278355dSNuno Antunes * Copyright (c) 1996-1999 Whistle Communications, Inc.
7*9278355dSNuno Antunes * All rights reserved.
8*9278355dSNuno Antunes *
9*9278355dSNuno Antunes * Subject to the following obligations and disclaimer of warranty, use and
10*9278355dSNuno Antunes * redistribution of this software, in source or object code forms, with or
11*9278355dSNuno Antunes * without modifications are expressly permitted by Whistle Communications;
12*9278355dSNuno Antunes * provided, however, that:
13*9278355dSNuno Antunes * 1. Any and all reproductions of the source or object code must include the
14*9278355dSNuno Antunes * copyright notice above and the following disclaimer of warranties; and
15*9278355dSNuno Antunes * 2. No rights are granted, in any manner or form, to use Whistle
16*9278355dSNuno Antunes * Communications, Inc. trademarks, including the mark "WHISTLE
17*9278355dSNuno Antunes * COMMUNICATIONS" on advertising, endorsements, or otherwise except as
18*9278355dSNuno Antunes * such appears in the above copyright notice or in the software.
19*9278355dSNuno Antunes *
20*9278355dSNuno Antunes * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
21*9278355dSNuno Antunes * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
22*9278355dSNuno Antunes * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
23*9278355dSNuno Antunes * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
24*9278355dSNuno Antunes * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
25*9278355dSNuno Antunes * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
26*9278355dSNuno Antunes * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
27*9278355dSNuno Antunes * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
28*9278355dSNuno Antunes * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES
29*9278355dSNuno Antunes * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
30*9278355dSNuno Antunes * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
31*9278355dSNuno Antunes * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
32*9278355dSNuno Antunes * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
33*9278355dSNuno Antunes * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34*9278355dSNuno Antunes * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
35*9278355dSNuno Antunes * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
36*9278355dSNuno Antunes * OF SUCH DAMAGE.
37*9278355dSNuno Antunes *
38*9278355dSNuno Antunes * Author: Julian Elisher <julian@freebsd.org>
39*9278355dSNuno Antunes *
40*9278355dSNuno Antunes * $FreeBSD: src/sys/netgraph/ng_echo.c,v 1.13 2005/04/15 10:14:00 glebius Exp $
41*9278355dSNuno Antunes * $DragonFly: src/sys/netgraph7/ng_echo.c,v 1.2 2008/06/26 23:05:35 dillon Exp $
42*9278355dSNuno Antunes * $Whistle: ng_echo.c,v 1.13 1999/11/01 09:24:51 julian Exp $
43*9278355dSNuno Antunes */
44*9278355dSNuno Antunes
45*9278355dSNuno Antunes /*
46*9278355dSNuno Antunes * Netgraph "echo" node
47*9278355dSNuno Antunes *
48*9278355dSNuno Antunes * This node simply bounces data and messages back to whence they came.
49*9278355dSNuno Antunes */
50*9278355dSNuno Antunes
51*9278355dSNuno Antunes #include <sys/param.h>
52*9278355dSNuno Antunes #include <sys/systm.h>
53*9278355dSNuno Antunes #include <sys/kernel.h>
54*9278355dSNuno Antunes #include <sys/malloc.h>
55*9278355dSNuno Antunes #include <sys/mbuf.h>
56*9278355dSNuno Antunes #include <netgraph7/ng_message.h>
57*9278355dSNuno Antunes #include <netgraph7/netgraph.h>
58*9278355dSNuno Antunes #include "ng_echo.h"
59*9278355dSNuno Antunes
60*9278355dSNuno Antunes /* Netgraph methods */
61*9278355dSNuno Antunes static ng_constructor_t nge_cons;
62*9278355dSNuno Antunes static ng_rcvmsg_t nge_rcvmsg;
63*9278355dSNuno Antunes static ng_rcvdata_t nge_rcvdata;
64*9278355dSNuno Antunes static ng_disconnect_t nge_disconnect;
65*9278355dSNuno Antunes
66*9278355dSNuno Antunes /* Netgraph type */
67*9278355dSNuno Antunes static struct ng_type typestruct = {
68*9278355dSNuno Antunes .version = NG_ABI_VERSION,
69*9278355dSNuno Antunes .name = NG_ECHO_NODE_TYPE,
70*9278355dSNuno Antunes .constructor = nge_cons,
71*9278355dSNuno Antunes .rcvmsg = nge_rcvmsg,
72*9278355dSNuno Antunes .rcvdata = nge_rcvdata,
73*9278355dSNuno Antunes .disconnect = nge_disconnect,
74*9278355dSNuno Antunes };
75*9278355dSNuno Antunes NETGRAPH_INIT(echo, &typestruct);
76*9278355dSNuno Antunes
77*9278355dSNuno Antunes static int
nge_cons(node_p node)78*9278355dSNuno Antunes nge_cons(node_p node)
79*9278355dSNuno Antunes {
80*9278355dSNuno Antunes return (0);
81*9278355dSNuno Antunes }
82*9278355dSNuno Antunes
83*9278355dSNuno Antunes /*
84*9278355dSNuno Antunes * Receive control message. We just bounce it back as a reply.
85*9278355dSNuno Antunes */
86*9278355dSNuno Antunes static int
nge_rcvmsg(node_p node,item_p item,hook_p lasthook)87*9278355dSNuno Antunes nge_rcvmsg(node_p node, item_p item, hook_p lasthook)
88*9278355dSNuno Antunes {
89*9278355dSNuno Antunes struct ng_mesg *msg;
90*9278355dSNuno Antunes int error = 0;
91*9278355dSNuno Antunes
92*9278355dSNuno Antunes NGI_GET_MSG(item, msg);
93*9278355dSNuno Antunes msg->header.flags |= NGF_RESP;
94*9278355dSNuno Antunes NG_RESPOND_MSG(error, node, item, msg);
95*9278355dSNuno Antunes return (error);
96*9278355dSNuno Antunes }
97*9278355dSNuno Antunes
98*9278355dSNuno Antunes /*
99*9278355dSNuno Antunes * Receive data
100*9278355dSNuno Antunes */
101*9278355dSNuno Antunes static int
nge_rcvdata(hook_p hook,item_p item)102*9278355dSNuno Antunes nge_rcvdata(hook_p hook, item_p item)
103*9278355dSNuno Antunes {
104*9278355dSNuno Antunes int error;
105*9278355dSNuno Antunes
106*9278355dSNuno Antunes NG_FWD_ITEM_HOOK(error, item, hook);
107*9278355dSNuno Antunes return (error);
108*9278355dSNuno Antunes }
109*9278355dSNuno Antunes
110*9278355dSNuno Antunes /*
111*9278355dSNuno Antunes * Removal of the last link destroys the nodeo
112*9278355dSNuno Antunes */
113*9278355dSNuno Antunes static int
nge_disconnect(hook_p hook)114*9278355dSNuno Antunes nge_disconnect(hook_p hook)
115*9278355dSNuno Antunes {
116*9278355dSNuno Antunes if ((NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0)
117*9278355dSNuno Antunes && (NG_NODE_IS_VALID(NG_HOOK_NODE(hook)))) {
118*9278355dSNuno Antunes ng_rmnode_self(NG_HOOK_NODE(hook));
119*9278355dSNuno Antunes }
120*9278355dSNuno Antunes return (0);
121*9278355dSNuno Antunes }
122*9278355dSNuno Antunes
123