ng_UI.c revision 213794
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: head/sys/netgraph/ng_UI.c 213794 2010-10-13 17:21:21Z rpaulo $
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 */
65struct ng_UI_private {
66	hook_p  downlink;
67	hook_p  uplink;
68};
69typedef struct ng_UI_private *priv_p;
70
71/* Netgraph node methods */
72static ng_constructor_t	ng_UI_constructor;
73static ng_rcvmsg_t	ng_UI_rcvmsg;
74static ng_shutdown_t	ng_UI_shutdown;
75static ng_newhook_t	ng_UI_newhook;
76static ng_rcvdata_t	ng_UI_rcvdata;
77static ng_disconnect_t	ng_UI_disconnect;
78
79/* Node type descriptor */
80static 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};
90NETGRAPH_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
100static int
101ng_UI_constructor(node_p node)
102{
103	priv_p  priv;
104
105	/* Allocate private structure */
106	priv = malloc(sizeof(*priv), M_NETGRAPH, M_NOWAIT | M_ZERO);
107	if (priv == NULL) {
108		return (ENOMEM);
109	}
110	NG_NODE_SET_PRIVATE(node, priv);
111	return (0);
112}
113
114/*
115 * Give our ok for a hook to be added
116 */
117static int
118ng_UI_newhook(node_p node, hook_p hook, const char *name)
119{
120	const priv_p priv = NG_NODE_PRIVATE(node);
121
122	if (!strcmp(name, NG_UI_HOOK_DOWNSTREAM)) {
123		if (priv->downlink)
124			return (EISCONN);
125		priv->downlink = hook;
126	} else if (!strcmp(name, NG_UI_HOOK_UPSTREAM)) {
127		if (priv->uplink)
128			return (EISCONN);
129		priv->uplink = hook;
130	} else
131		return (EINVAL);
132	return (0);
133}
134
135/*
136 * Receive a control message
137 */
138static int
139ng_UI_rcvmsg(node_p node, item_p item, hook_p lasthook)
140{
141	int	error;
142	const priv_p priv = NG_NODE_PRIVATE(node);
143	struct ng_mesg *msg;
144
145	msg = NGI_MSG(item); /* only peeking */
146	if ((msg->header.typecookie == NGM_FLOW_COOKIE) && lasthook)  {
147		if (lasthook == priv->downlink) {
148			if (priv->uplink) {
149				NG_FWD_ITEM_HOOK(error, item, priv->uplink);
150				return (error);
151			}
152		} else {
153			if (priv->downlink) {
154				NG_FWD_ITEM_HOOK(error, item, priv->downlink);
155				return (error);
156			}
157		}
158	}
159
160	NG_FREE_ITEM(item);
161	return (EINVAL);
162}
163
164#define MAX_ENCAPS_HDR	1
165#define ERROUT(x)	do { error = (x); goto done; } while (0)
166
167/*
168 * Receive a data frame
169 */
170static int
171ng_UI_rcvdata(hook_p hook, item_p item)
172{
173	const node_p node = NG_HOOK_NODE(hook);
174	const priv_p priv = NG_NODE_PRIVATE(node);
175	struct mbuf *m;
176	int error = 0;
177
178	NGI_GET_M(item, m);
179	if (hook == priv->downlink) {
180		u_char *start, *ptr;
181
182		if (m->m_len < MAX_ENCAPS_HDR
183		    && !(m = m_pullup(m, MAX_ENCAPS_HDR)))
184			ERROUT(ENOBUFS);
185		ptr = start = mtod(m, u_char *);
186
187		/* Must be UI frame */
188		if (*ptr++ != HDLC_UI)
189			ERROUT(0);
190
191		m_adj(m, ptr - start);
192		NG_FWD_NEW_DATA(error, item, priv->uplink, m);	/* m -> NULL */
193	} else if (hook == priv->uplink) {
194		M_PREPEND(m, 1, M_DONTWAIT);	/* Prepend IP NLPID */
195		if (!m)
196			ERROUT(ENOBUFS);
197		mtod(m, u_char *)[0] = HDLC_UI;
198		NG_FWD_NEW_DATA(error, item, priv->downlink, m);	/* m -> NULL */
199	} else
200		panic("%s", __func__);
201
202done:
203	NG_FREE_M(m);	/* does nothing if m == NULL */
204	if (item)
205		NG_FREE_ITEM(item);
206	return (error);
207}
208
209/*
210 * Shutdown node
211 */
212static int
213ng_UI_shutdown(node_p node)
214{
215	const priv_p priv = NG_NODE_PRIVATE(node);
216
217	/* Take down netgraph node */
218	free(priv, M_NETGRAPH);
219	NG_NODE_SET_PRIVATE(node, NULL);
220	NG_NODE_UNREF(node);
221	return (0);
222}
223
224/*
225 * Hook disconnection
226 */
227static int
228ng_UI_disconnect(hook_p hook)
229{
230	const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
231
232	if (hook == priv->downlink)
233		priv->downlink = NULL;
234	else if (hook == priv->uplink)
235		priv->uplink = NULL;
236	else
237		panic("%s", __func__);
238	/*
239	 * If we are not already shutting down,
240	 * and we have no more hooks, then DO shut down.
241	 */
242	if ((NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0)
243	&& (NG_NODE_IS_VALID(NG_HOOK_NODE(hook)))) {
244			ng_rmnode_self(NG_HOOK_NODE(hook));
245	}
246	return (0);
247}
248
249