xref: /freebsd/sys/netgraph/ng_UI.c (revision 37a60314613bc8baf1a46d9981e7c2f674dbe1cd)
1 /*
2  * ng_UI.c
3  */
4 
5 /*-
6  * Copyright (c) 1996-1999 Whistle Communications, Inc.
7  * All rights reserved.
8  *
9  * Subject to the following obligations and disclaimer of warranty, use and
10  * redistribution of this software, in source or object code forms, with or
11  * without modifications are expressly permitted by Whistle Communications;
12  * provided, however, that:
13  * 1. Any and all reproductions of the source or object code must include the
14  *    copyright notice above and the following disclaimer of warranties; and
15  * 2. No rights are granted, in any manner or form, to use Whistle
16  *    Communications, Inc. trademarks, including the mark "WHISTLE
17  *    COMMUNICATIONS" on advertising, endorsements, or otherwise except as
18  *    such appears in the above copyright notice or in the software.
19  *
20  * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
21  * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
22  * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
23  * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
24  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
25  * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
26  * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
27  * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
28  * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES
29  * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
30  * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
31  * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
32  * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
33  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
35  * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
36  * OF SUCH DAMAGE.
37  *
38  * Author: Julian Elischer <julian@freebsd.org>
39  *
40  * $FreeBSD$
41  * $Whistle: ng_UI.c,v 1.14 1999/11/01 09:24:51 julian Exp $
42  */
43 
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/errno.h>
47 #include <sys/kernel.h>
48 #include <sys/malloc.h>
49 #include <sys/mbuf.h>
50 #include <sys/errno.h>
51 
52 
53 #include <netgraph/ng_message.h>
54 #include <netgraph/netgraph.h>
55 #include <netgraph/ng_UI.h>
56 
57 /*
58  * DEFINITIONS
59  */
60 
61 /* Everything, starting with sdlc on has defined UI as 0x03 */
62 #define HDLC_UI	0x03
63 
64 /* Node private data */
65 struct ng_UI_private {
66 	hook_p  downlink;
67 	hook_p  uplink;
68 };
69 typedef struct ng_UI_private *priv_p;
70 
71 /* Netgraph node methods */
72 static ng_constructor_t	ng_UI_constructor;
73 static ng_rcvmsg_t	ng_UI_rcvmsg;
74 static ng_shutdown_t	ng_UI_shutdown;
75 static ng_newhook_t	ng_UI_newhook;
76 static ng_rcvdata_t	ng_UI_rcvdata;
77 static ng_disconnect_t	ng_UI_disconnect;
78 
79 /* Node type descriptor */
80 static struct ng_type typestruct = {
81 	.version =	NG_ABI_VERSION,
82 	.name =		NG_UI_NODE_TYPE,
83 	.constructor =	ng_UI_constructor,
84 	.rcvmsg =	ng_UI_rcvmsg,
85 	.shutdown =	ng_UI_shutdown,
86 	.newhook =	ng_UI_newhook,
87 	.rcvdata =	ng_UI_rcvdata,
88 	.disconnect =	ng_UI_disconnect,
89 };
90 NETGRAPH_INIT(UI, &typestruct);
91 
92 /************************************************************************
93 			NETGRAPH NODE STUFF
94  ************************************************************************/
95 
96 /*
97  * Create a newborn node. We start with an implicit reference.
98  */
99 
100 static int
101 ng_UI_constructor(node_p node)
102 {
103 	priv_p  priv;
104 
105 	/* Allocate private structure */
106 	priv = malloc(sizeof(*priv), M_NETGRAPH, M_WAITOK | M_ZERO);
107 	NG_NODE_SET_PRIVATE(node, priv);
108 	return (0);
109 }
110 
111 /*
112  * Give our ok for a hook to be added
113  */
114 static int
115 ng_UI_newhook(node_p node, hook_p hook, const char *name)
116 {
117 	const priv_p priv = NG_NODE_PRIVATE(node);
118 
119 	if (!strcmp(name, NG_UI_HOOK_DOWNSTREAM)) {
120 		if (priv->downlink)
121 			return (EISCONN);
122 		priv->downlink = hook;
123 	} else if (!strcmp(name, NG_UI_HOOK_UPSTREAM)) {
124 		if (priv->uplink)
125 			return (EISCONN);
126 		priv->uplink = hook;
127 	} else
128 		return (EINVAL);
129 	return (0);
130 }
131 
132 /*
133  * Receive a control message
134  */
135 static int
136 ng_UI_rcvmsg(node_p node, item_p item, hook_p lasthook)
137 {
138 	int	error;
139 	const priv_p priv = NG_NODE_PRIVATE(node);
140 	struct ng_mesg *msg;
141 
142 	msg = NGI_MSG(item); /* only peeking */
143 	if ((msg->header.typecookie == NGM_FLOW_COOKIE) && lasthook)  {
144 		if (lasthook == priv->downlink) {
145 			if (priv->uplink) {
146 				NG_FWD_ITEM_HOOK(error, item, priv->uplink);
147 				return (error);
148 			}
149 		} else {
150 			if (priv->downlink) {
151 				NG_FWD_ITEM_HOOK(error, item, priv->downlink);
152 				return (error);
153 			}
154 		}
155 	}
156 
157 	NG_FREE_ITEM(item);
158 	return (EINVAL);
159 }
160 
161 #define MAX_ENCAPS_HDR	1
162 #define ERROUT(x)	do { error = (x); goto done; } while (0)
163 
164 /*
165  * Receive a data frame
166  */
167 static int
168 ng_UI_rcvdata(hook_p hook, item_p item)
169 {
170 	const node_p node = NG_HOOK_NODE(hook);
171 	const priv_p priv = NG_NODE_PRIVATE(node);
172 	struct mbuf *m;
173 	int error = 0;
174 
175 	NGI_GET_M(item, m);
176 	if (hook == priv->downlink) {
177 		u_char *start, *ptr;
178 
179 		if (m->m_len < MAX_ENCAPS_HDR
180 		    && !(m = m_pullup(m, MAX_ENCAPS_HDR)))
181 			ERROUT(ENOBUFS);
182 		ptr = start = mtod(m, u_char *);
183 
184 		/* Must be UI frame */
185 		if (*ptr++ != HDLC_UI)
186 			ERROUT(0);
187 
188 		m_adj(m, ptr - start);
189 		NG_FWD_NEW_DATA(error, item, priv->uplink, m);	/* m -> NULL */
190 	} else if (hook == priv->uplink) {
191 		M_PREPEND(m, 1, M_NOWAIT);	/* Prepend IP NLPID */
192 		if (!m)
193 			ERROUT(ENOBUFS);
194 		mtod(m, u_char *)[0] = HDLC_UI;
195 		NG_FWD_NEW_DATA(error, item, priv->downlink, m);	/* m -> NULL */
196 	} else
197 		panic("%s", __func__);
198 
199 done:
200 	NG_FREE_M(m);	/* does nothing if m == NULL */
201 	if (item)
202 		NG_FREE_ITEM(item);
203 	return (error);
204 }
205 
206 /*
207  * Shutdown node
208  */
209 static int
210 ng_UI_shutdown(node_p node)
211 {
212 	const priv_p priv = NG_NODE_PRIVATE(node);
213 
214 	/* Take down netgraph node */
215 	free(priv, M_NETGRAPH);
216 	NG_NODE_SET_PRIVATE(node, NULL);
217 	NG_NODE_UNREF(node);
218 	return (0);
219 }
220 
221 /*
222  * Hook disconnection
223  */
224 static int
225 ng_UI_disconnect(hook_p hook)
226 {
227 	const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
228 
229 	if (hook == priv->downlink)
230 		priv->downlink = NULL;
231 	else if (hook == priv->uplink)
232 		priv->uplink = NULL;
233 	else
234 		panic("%s", __func__);
235 	/*
236 	 * If we are not already shutting down,
237 	 * and we have no more hooks, then DO shut down.
238 	 */
239 	if ((NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0)
240 	&& (NG_NODE_IS_VALID(NG_HOOK_NODE(hook)))) {
241 			ng_rmnode_self(NG_HOOK_NODE(hook));
242 	}
243 	return (0);
244 }
245 
246