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