xref: /freebsd/sys/netgraph/ng_UI.c (revision c68159a6d8eede11766cf13896d0f7670dbd51aa)
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 	NG_ABI_VERSION,
81 	NG_UI_NODE_TYPE,
82 	NULL,
83 	ng_UI_constructor,
84 	ng_UI_rcvmsg,
85 	ng_UI_shutdown,
86 	ng_UI_newhook,
87 	NULL,
88 	NULL,
89 	ng_UI_rcvdata,
90 	ng_UI_disconnect,
91 	NULL
92 };
93 NETGRAPH_INIT(UI, &typestruct);
94 
95 /************************************************************************
96 			NETGRAPH NODE STUFF
97  ************************************************************************/
98 
99 /*
100  * Create a newborn node. We start with an implicit reference.
101  */
102 
103 static int
104 ng_UI_constructor(node_p nodep)
105 {
106 	priv_p  priv;
107 
108 	/* Allocate private structure */
109 	MALLOC(priv, priv_p, sizeof(*priv), M_NETGRAPH, M_NOWAIT | M_ZERO);
110 	if (priv == NULL) {
111 		return (ENOMEM);
112 	}
113 	nodep->private = priv;
114 	return (0);
115 }
116 
117 /*
118  * Give our ok for a hook to be added
119  */
120 static int
121 ng_UI_newhook(node_p node, hook_p hook, const char *name)
122 {
123 	const priv_p priv = node->private;
124 
125 	if (!strcmp(name, NG_UI_HOOK_DOWNSTREAM)) {
126 		if (priv->downlink)
127 			return (EISCONN);
128 		priv->downlink = hook;
129 	} else if (!strcmp(name, NG_UI_HOOK_UPSTREAM)) {
130 		if (priv->uplink)
131 			return (EISCONN);
132 		priv->uplink = hook;
133 	} else
134 		return (EINVAL);
135 	return (0);
136 }
137 
138 /*
139  * Receive a control message
140  */
141 static int
142 ng_UI_rcvmsg(node_p node, item_p item, hook_p lasthook)
143 {
144 	int	error;
145 	const priv_p priv = node->private;
146 	struct ng_mesg *msg;
147 
148 	msg = NGI_MSG(item); /* only peeking */
149 	if ((msg->header.typecookie == NGM_FLOW_COOKIE) && lasthook)  {
150 		if (lasthook == priv->downlink) {
151 			if (priv->uplink) {
152 				NG_FWD_MSG_HOOK(error, node, item,
153 					priv->uplink, 0);
154 				return (error);
155 			}
156 		} else {
157 			if (priv->downlink) {
158 				NG_FWD_MSG_HOOK(error, node, item,
159 					priv->downlink, 0);
160 				return (error);
161 			}
162 		}
163 	}
164 
165 	NG_FREE_MSG(msg);
166 	return (EINVAL);
167 }
168 
169 #define MAX_ENCAPS_HDR	1
170 #define ERROUT(x)	do { error = (x); goto done; } while (0)
171 
172 /*
173  * Receive a data frame
174  */
175 static int
176 ng_UI_rcvdata(hook_p hook, item_p item)
177 {
178 	const node_p node = hook->node;
179 	const priv_p priv = node->private;
180 	struct mbuf *m;
181 	int error = 0;
182 
183 	NGI_GET_M(item, m);
184 	if (hook == priv->downlink) {
185 		u_char *start, *ptr;
186 
187 		if (!m || (m->m_len < MAX_ENCAPS_HDR
188 		    && !(m = m_pullup(m, MAX_ENCAPS_HDR))))
189 			ERROUT(ENOBUFS);
190 		ptr = start = mtod(m, u_char *);
191 
192 		/* Must be UI frame */
193 		if (*ptr++ != HDLC_UI)
194 			ERROUT(0);
195 
196 		m_adj(m, ptr - start);
197 		NG_FWD_NEW_DATA(error, item, priv->uplink, m);	/* m -> NULL */
198 	} else if (hook == priv->uplink) {
199 		M_PREPEND(m, 1, M_DONTWAIT);	/* Prepend IP NLPID */
200 		if (!m)
201 			ERROUT(ENOBUFS);
202 		mtod(m, u_char *)[0] = HDLC_UI;
203 		NG_FWD_NEW_DATA(error, item, priv->downlink, m);	/* m -> NULL */
204 	} else
205 		panic(__FUNCTION__);
206 
207 done:
208 	NG_FREE_M(m);	/* does nothing if m == NULL */
209 	if (item)
210 		NG_FREE_ITEM(item);
211 	return (error);
212 }
213 
214 /*
215  * Shutdown node
216  */
217 static int
218 ng_UI_shutdown(node_p node)
219 {
220 	const priv_p priv = node->private;
221 
222 	/* Take down netgraph node */
223 	FREE(priv, M_NETGRAPH);
224 	node->private = NULL;
225 	ng_unref(node);
226 	return (0);
227 }
228 
229 /*
230  * Hook disconnection
231  */
232 static int
233 ng_UI_disconnect(hook_p hook)
234 {
235 	const priv_p priv = hook->node->private;
236 
237 	if (hook == priv->downlink)
238 		priv->downlink = NULL;
239 	else if (hook == priv->uplink)
240 		priv->uplink = NULL;
241 	else
242 		panic(__FUNCTION__);
243 	/*
244 	 * If we are not already shutting down,
245 	 * and we have no more hooks, then DO shut down.
246 	 */
247 	if ((hook->node->numhooks == 0)
248 	&& ((hook->node->flags & NG_INVALID) == 0)) {
249 			ng_rmnode_self(hook->node);
250 	}
251 	return (0);
252 }
253 
254