ng_rfc1490.c revision 109623
1276789Sdim
2276789Sdim/*
3276789Sdim * ng_rfc1490.c
4276789Sdim *
5276789Sdim * Copyright (c) 1996-1999 Whistle Communications, Inc.
6276789Sdim * All rights reserved.
7276789Sdim *
8276789Sdim * Subject to the following obligations and disclaimer of warranty, use and
9276789Sdim * redistribution of this software, in source or object code forms, with or
10276789Sdim * without modifications are expressly permitted by Whistle Communications;
11276789Sdim * provided, however, that:
12276789Sdim * 1. Any and all reproductions of the source or object code must include the
13276789Sdim *    copyright notice above and the following disclaimer of warranties; and
14276789Sdim * 2. No rights are granted, in any manner or form, to use Whistle
15276789Sdim *    Communications, Inc. trademarks, including the mark "WHISTLE
16276789Sdim *    COMMUNICATIONS" on advertising, endorsements, or otherwise except as
17276789Sdim *    such appears in the above copyright notice or in the software.
18276789Sdim *
19276789Sdim * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
20276789Sdim * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
21276789Sdim * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
22276789Sdim * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
23276789Sdim * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
24276789Sdim * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
25276789Sdim * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
26276789Sdim * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
27276789Sdim * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES
28276789Sdim * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
29276789Sdim * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
30276789Sdim * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
31276789Sdim * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
32276789Sdim * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33276789Sdim * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
34276789Sdim * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
35276789Sdim * OF SUCH DAMAGE.
36276789Sdim *
37276789Sdim * Author: Julian Elischer <julian@freebsd.org>
38276789Sdim *
39276789Sdim * $FreeBSD: head/sys/netgraph/ng_rfc1490.c 109623 2003-01-21 08:56:16Z alfred $
40276789Sdim * $Whistle: ng_rfc1490.c,v 1.22 1999/11/01 09:24:52 julian Exp $
41276789Sdim */
42276789Sdim
43276789Sdim/*
44276789Sdim * This node does RFC 1490 multiplexing.
45276789Sdim *
46276789Sdim * NOTE: RFC 1490 is updated by RFC 2427.
47276789Sdim */
48276789Sdim
49276789Sdim#include <sys/param.h>
50276789Sdim#include <sys/systm.h>
51276789Sdim#include <sys/errno.h>
52276789Sdim#include <sys/kernel.h>
53#include <sys/malloc.h>
54#include <sys/mbuf.h>
55#include <sys/errno.h>
56#include <sys/socket.h>
57
58#include <net/if.h>
59#include <netinet/in.h>
60#include <netinet/if_ether.h>
61
62#include <netgraph/ng_message.h>
63#include <netgraph/netgraph.h>
64#include <netgraph/ng_rfc1490.h>
65
66/*
67 * DEFINITIONS
68 */
69
70/* Q.922 stuff -- see RFC 1490 */
71#define HDLC_UI		0x03
72
73#define NLPID_IP	0xCC
74#define NLPID_PPP	0xCF
75#define NLPID_SNAP	0x80
76#define NLPID_Q933	0x08
77#define NLPID_CLNP	0x81
78#define NLPID_ESIS	0x82
79#define NLPID_ISIS	0x83
80
81/* Node private data */
82struct ng_rfc1490_private {
83	hook_p  downlink;
84	hook_p  ppp;
85	hook_p  inet;
86};
87typedef struct ng_rfc1490_private *priv_p;
88
89/* Netgraph node methods */
90static ng_constructor_t	ng_rfc1490_constructor;
91static ng_rcvmsg_t	ng_rfc1490_rcvmsg;
92static ng_shutdown_t	ng_rfc1490_shutdown;
93static ng_newhook_t	ng_rfc1490_newhook;
94static ng_rcvdata_t	ng_rfc1490_rcvdata;
95static ng_disconnect_t	ng_rfc1490_disconnect;
96
97/* Node type descriptor */
98static struct ng_type typestruct = {
99	NG_ABI_VERSION,
100	NG_RFC1490_NODE_TYPE,
101	NULL,
102	ng_rfc1490_constructor,
103	ng_rfc1490_rcvmsg,
104	ng_rfc1490_shutdown,
105	ng_rfc1490_newhook,
106	NULL,
107	NULL,
108	ng_rfc1490_rcvdata,
109	ng_rfc1490_disconnect,
110	NULL
111};
112NETGRAPH_INIT(rfc1490, &typestruct);
113
114/************************************************************************
115			NETGRAPH NODE STUFF
116 ************************************************************************/
117
118/*
119 * Node constructor
120 */
121static int
122ng_rfc1490_constructor(node_p node)
123{
124	priv_p priv;
125
126	/* Allocate private structure */
127	MALLOC(priv, priv_p, sizeof(*priv), M_NETGRAPH, M_NOWAIT | M_ZERO);
128	if (priv == NULL)
129		return (ENOMEM);
130
131	NG_NODE_SET_PRIVATE(node, priv);
132
133	/* Done */
134	return (0);
135}
136
137/*
138 * Give our ok for a hook to be added
139 */
140static int
141ng_rfc1490_newhook(node_p node, hook_p hook, const char *name)
142{
143	const priv_p priv = NG_NODE_PRIVATE(node);
144
145	if (!strcmp(name, NG_RFC1490_HOOK_DOWNSTREAM)) {
146		if (priv->downlink)
147			return (EISCONN);
148		priv->downlink = hook;
149	} else if (!strcmp(name, NG_RFC1490_HOOK_PPP)) {
150		if (priv->ppp)
151			return (EISCONN);
152		priv->ppp = hook;
153	} else if (!strcmp(name, NG_RFC1490_HOOK_INET)) {
154		if (priv->inet)
155			return (EISCONN);
156		priv->inet = hook;
157	} else
158		return (EINVAL);
159	return (0);
160}
161
162/*
163 * Receive a control message. We don't support any special ones.
164 */
165static int
166ng_rfc1490_rcvmsg(node_p node, item_p item, hook_p lasthook)
167{
168	NG_FREE_ITEM(item);
169	return (EINVAL);
170}
171
172/*
173 * Receive data on a hook and encapsulate according to RFC 1490.
174 * Only those nodes marked (*) are supported by this routine so far.
175 *
176 *                            Q.922 control
177 *                                 |
178 *                                 |
179 *            --------------------------------------------
180 *            | 0x03                                     |
181 *           UI                                       I Frame
182 *            |                                          |
183 *      ---------------------------------         --------------
184 *      | 0x08  | 0x81  |0xCC   |0xCF   | 0x00    |..01....    |..10....
185 *      |       |       |       |       | 0x80    |            |
186 *     Q.933   CLNP    IP(*)   PPP(*)  SNAP     ISO 8208    ISO 8208
187 *      |                    (rfc1973)  |       Modulo 8    Modulo 128
188 *      |                               |
189 *      --------------------           OUI
190 *      |                  |            |
191 *     L2 ID              L3 ID      -------------------------
192 *      |               User         |00-80-C2               |00-00-00
193 *      |               specified    |                       |
194 *      |               0x70        PID                     Ethertype
195 *      |                            |                       |
196 *      -------------------        --------------...        ----------
197 *      |0x51 |0x4E |     |0x4C    |0x1   |0xB  |           |0x806   |
198 *      |     |     |     |        |      |     |           |        |
199 *     7776  Q.922 Others 802.2   802.3  802.6 Others       ARP(*)  Others
200 *
201 *
202 */
203
204#define MAX_ENCAPS_HDR	8
205#define ERROUT(x)	do { error = (x); goto done; } while (0)
206#define OUICMP(P,A,B,C)	((P)[0]==(A) && (P)[1]==(B) && (P)[2]==(C))
207
208static int
209ng_rfc1490_rcvdata(hook_p hook, item_p item)
210{
211	const node_p node = NG_HOOK_NODE(hook);
212	const priv_p priv = NG_NODE_PRIVATE(node);
213	int error = 0;
214	struct mbuf *m;
215
216	NGI_GET_M(item, m);
217	if (hook == priv->downlink) {
218		const u_char *start;
219		const u_char *ptr;
220
221		if (!m || (m->m_len < MAX_ENCAPS_HDR
222		    && !(m = m_pullup(m, MAX_ENCAPS_HDR))))
223			ERROUT(ENOBUFS);
224		ptr = start = mtod(m, const u_char *);
225
226		/* Must be UI frame */
227		if (*ptr++ != HDLC_UI)
228			ERROUT(0);
229
230		/* Eat optional zero pad byte */
231		if (*ptr == 0x00)
232			ptr++;
233
234		/* Multiplex on NLPID */
235		switch (*ptr++) {
236		case NLPID_SNAP:
237			if (OUICMP(ptr, 0, 0, 0)) {	/* It's an ethertype */
238				u_int16_t etype;
239
240				ptr += 3;
241				etype = ntohs(*((const u_int16_t *)ptr));
242				ptr += 2;
243				m_adj(m, ptr - start);
244				switch (etype) {
245				case ETHERTYPE_IP:
246					NG_FWD_NEW_DATA(error, item,
247					    priv->inet, m);
248					break;
249				case ETHERTYPE_ARP:
250				case ETHERTYPE_REVARP:
251				default:
252					ERROUT(0);
253				}
254			} else if (OUICMP(ptr, 0x00, 0x80, 0xc2))	/* 802.1 bridging */
255				ERROUT(0);
256			else	/* Other weird stuff... */
257				ERROUT(0);
258			break;
259		case NLPID_IP:
260			m_adj(m, ptr - start);
261			NG_FWD_NEW_DATA(error, item, priv->inet, m);
262			break;
263		case NLPID_PPP:
264			m_adj(m, ptr - start);
265			NG_FWD_NEW_DATA(error, item, priv->ppp, m);
266			break;
267		case NLPID_Q933:
268		case NLPID_CLNP:
269		case NLPID_ESIS:
270		case NLPID_ISIS:
271			ERROUT(0);
272		default:	/* Try PPP (see RFC 1973) */
273			ptr--;	/* NLPID becomes PPP proto */
274			if ((*ptr & 0x01) == 0x01)
275				ERROUT(0);
276			m_adj(m, ptr - start);
277			NG_FWD_NEW_DATA(error, item, priv->ppp, m);
278			break;
279		}
280	} else if (hook == priv->ppp) {
281		M_PREPEND(m, 2, M_NOWAIT);	/* Prepend PPP NLPID */
282		if (!m)
283			ERROUT(ENOBUFS);
284		mtod(m, u_char *)[0] = HDLC_UI;
285		mtod(m, u_char *)[1] = NLPID_PPP;
286		NG_FWD_NEW_DATA(error, item, priv->downlink, m);
287	} else if (hook == priv->inet) {
288		M_PREPEND(m, 2, M_NOWAIT);	/* Prepend IP NLPID */
289		if (!m)
290			ERROUT(ENOBUFS);
291		mtod(m, u_char *)[0] = HDLC_UI;
292		mtod(m, u_char *)[1] = NLPID_IP;
293		NG_FWD_NEW_DATA(error, item, priv->downlink, m);
294	} else
295		panic(__func__);
296
297done:
298	if (item)
299		NG_FREE_ITEM(item);
300	NG_FREE_M(m);
301	return (error);
302}
303
304/*
305 * Nuke node
306 */
307static int
308ng_rfc1490_shutdown(node_p node)
309{
310	const priv_p priv = NG_NODE_PRIVATE(node);
311
312	/* Take down netgraph node */
313	bzero(priv, sizeof(*priv));
314	FREE(priv, M_NETGRAPH);
315	NG_NODE_SET_PRIVATE(node, NULL);
316	NG_NODE_UNREF(node);		/* let the node escape */
317	return (0);
318}
319
320/*
321 * Hook disconnection
322 */
323static int
324ng_rfc1490_disconnect(hook_p hook)
325{
326	const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
327
328	if ((NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0)
329	&& (NG_NODE_IS_VALID(NG_HOOK_NODE(hook))))
330		ng_rmnode_self(NG_HOOK_NODE(hook));
331	else if (hook == priv->downlink)
332		priv->downlink = NULL;
333	else if (hook == priv->inet)
334		priv->inet = NULL;
335	else if (hook == priv->ppp)
336		priv->ppp = NULL;
337	else
338		panic(__func__);
339	return (0);
340}
341
342