xref: /freebsd/sys/netgraph/ng_rfc1490.c (revision f8aae7776f85d2fa8aa93f73c37782cd9e1204c6)
1 
2 /*
3  * ng_rfc1490.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_rfc1490.c,v 1.22 1999/11/01 09:24:52 julian Exp $
41  */
42 
43 /*
44  * This node does RFC 1490 multiplexing.
45  *
46  * NOTE: RFC 1490 is updated by RFC 2427.
47  */
48 
49 #include <sys/param.h>
50 #include <sys/systm.h>
51 #include <sys/errno.h>
52 #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 */
82 struct ng_rfc1490_private {
83 	hook_p  downlink;
84 	hook_p  ppp;
85 	hook_p  inet;
86 	hook_p  ethernet;
87 };
88 typedef struct ng_rfc1490_private *priv_p;
89 
90 /* Netgraph node methods */
91 static ng_constructor_t	ng_rfc1490_constructor;
92 static ng_rcvmsg_t	ng_rfc1490_rcvmsg;
93 static ng_shutdown_t	ng_rfc1490_shutdown;
94 static ng_newhook_t	ng_rfc1490_newhook;
95 static ng_rcvdata_t	ng_rfc1490_rcvdata;
96 static ng_disconnect_t	ng_rfc1490_disconnect;
97 
98 /* Node type descriptor */
99 static struct ng_type typestruct = {
100 	.version =	NG_ABI_VERSION,
101 	.name =		NG_RFC1490_NODE_TYPE,
102 	.constructor =	ng_rfc1490_constructor,
103 	.rcvmsg =	ng_rfc1490_rcvmsg,
104 	.shutdown =	ng_rfc1490_shutdown,
105 	.newhook =	ng_rfc1490_newhook,
106 	.rcvdata =	ng_rfc1490_rcvdata,
107 	.disconnect =	ng_rfc1490_disconnect,
108 };
109 NETGRAPH_INIT(rfc1490, &typestruct);
110 
111 /************************************************************************
112 			NETGRAPH NODE STUFF
113  ************************************************************************/
114 
115 /*
116  * Node constructor
117  */
118 static int
119 ng_rfc1490_constructor(node_p node)
120 {
121 	priv_p priv;
122 
123 	/* Allocate private structure */
124 	MALLOC(priv, priv_p, sizeof(*priv), M_NETGRAPH, M_NOWAIT | M_ZERO);
125 	if (priv == NULL)
126 		return (ENOMEM);
127 
128 	NG_NODE_SET_PRIVATE(node, priv);
129 
130 	/* Done */
131 	return (0);
132 }
133 
134 /*
135  * Give our ok for a hook to be added
136  */
137 static int
138 ng_rfc1490_newhook(node_p node, hook_p hook, const char *name)
139 {
140 	const priv_p priv = NG_NODE_PRIVATE(node);
141 
142 	if (!strcmp(name, NG_RFC1490_HOOK_DOWNSTREAM)) {
143 		if (priv->downlink)
144 			return (EISCONN);
145 		priv->downlink = hook;
146 	} else if (!strcmp(name, NG_RFC1490_HOOK_PPP)) {
147 		if (priv->ppp)
148 			return (EISCONN);
149 		priv->ppp = hook;
150 	} else if (!strcmp(name, NG_RFC1490_HOOK_INET)) {
151 		if (priv->inet)
152 			return (EISCONN);
153 		priv->inet = hook;
154 	} else if (!strcmp(name, NG_RFC1490_HOOK_ETHERNET)) {
155 		if (priv->ethernet)
156 			return (EISCONN);
157 		priv->ethernet = hook;
158 	} else
159 		return (EINVAL);
160 	return (0);
161 }
162 
163 /*
164  * Receive a control message. We don't support any special ones.
165  */
166 static int
167 ng_rfc1490_rcvmsg(node_p node, item_p item, hook_p lasthook)
168 {
169 	NG_FREE_ITEM(item);
170 	return (EINVAL);
171 }
172 
173 /*
174  * Receive data on a hook and encapsulate according to RFC 1490.
175  * Only those nodes marked (*) are supported by this routine so far.
176  *
177  *                            Q.922 control
178  *                                 |
179  *                                 |
180  *            --------------------------------------------
181  *            | 0x03                                     |
182  *           UI                                       I Frame
183  *            |                                          |
184  *      ---------------------------------         --------------
185  *      | 0x08  | 0x81  |0xCC   |0xCF   | 0x00    |..01....    |..10....
186  *      |       |       |       |       | 0x80    |            |
187  *     Q.933   CLNP    IP(*)   PPP(*)  SNAP     ISO 8208    ISO 8208
188  *      |                    (rfc1973)  |       Modulo 8    Modulo 128
189  *      |                               |
190  *      --------------------           OUI
191  *      |                  |            |
192  *     L2 ID              L3 ID      -------------------------
193  *      |               User         |00-80-C2               |00-00-00
194  *      |               specified    |                       |
195  *      |               0x70        PID                     Ethertype
196  *      |                            |                       |
197  *      -------------------        -----------------...     ----------
198  *      |0x51 |0x4E |     |0x4C    |0x7      |0xB  |        |0x806   |
199  *      |     |     |     |        |         |     |        |        |
200  *     7776  Q.922 Others 802.2   802.3(*)  802.6 Others    ARP(*)  Others
201  *
202  *
203  */
204 
205 #define MAX_ENCAPS_HDR	8
206 #define ERROUT(x)	do { error = (x); goto done; } while (0)
207 #define OUICMP(P,A,B,C)	((P)[0]==(A) && (P)[1]==(B) && (P)[2]==(C))
208 
209 static int
210 ng_rfc1490_rcvdata(hook_p hook, item_p item)
211 {
212 	const node_p node = NG_HOOK_NODE(hook);
213 	const priv_p priv = NG_NODE_PRIVATE(node);
214 	int error = 0;
215 	struct mbuf *m;
216 
217 	NGI_GET_M(item, m);
218 	if (hook == priv->downlink) {
219 		const u_char *start;
220 		const u_char *ptr;
221 
222 		if (!m || (m->m_len < MAX_ENCAPS_HDR
223 		    && !(m = m_pullup(m, MAX_ENCAPS_HDR))))
224 			ERROUT(ENOBUFS);
225 		ptr = start = mtod(m, const u_char *);
226 
227 		/* Must be UI frame */
228 		if (*ptr++ != HDLC_UI)
229 			ERROUT(0);
230 
231 		/* Eat optional zero pad byte */
232 		if (*ptr == 0x00)
233 			ptr++;
234 
235 		/* Multiplex on NLPID */
236 		switch (*ptr++) {
237 		case NLPID_SNAP:
238 			if (OUICMP(ptr, 0, 0, 0)) {	/* It's an ethertype */
239 				u_int16_t etype;
240 
241 				ptr += 3;
242 				etype = ntohs(*((const u_int16_t *)ptr));
243 				ptr += 2;
244 				m_adj(m, ptr - start);
245 				switch (etype) {
246 				case ETHERTYPE_IP:
247 					NG_FWD_NEW_DATA(error, item,
248 					    priv->inet, m);
249 					break;
250 				case ETHERTYPE_ARP:
251 				case ETHERTYPE_REVARP:
252 				default:
253 					ERROUT(0);
254 				}
255 			} else if (OUICMP(ptr, 0x00, 0x80, 0xc2)) {
256 				/* 802.1 bridging */
257 				ptr += 3;
258 				if (*ptr++ != 0x00)
259 					ERROUT(0);     /* unknown PID octet 0 */
260 				if (*ptr++ != 0x07)
261 					ERROUT(0);	/* not FCS-less 802.3 */
262 				m_adj(m, ptr - start);
263 				NG_FWD_NEW_DATA(error, item, priv->ethernet, m);
264 			} else	/* Other weird stuff... */
265 				ERROUT(0);
266 			break;
267 		case NLPID_IP:
268 			m_adj(m, ptr - start);
269 			NG_FWD_NEW_DATA(error, item, priv->inet, m);
270 			break;
271 		case NLPID_PPP:
272 			m_adj(m, ptr - start);
273 			NG_FWD_NEW_DATA(error, item, priv->ppp, m);
274 			break;
275 		case NLPID_Q933:
276 		case NLPID_CLNP:
277 		case NLPID_ESIS:
278 		case NLPID_ISIS:
279 			ERROUT(0);
280 		default:	/* Try PPP (see RFC 1973) */
281 			ptr--;	/* NLPID becomes PPP proto */
282 			if ((*ptr & 0x01) == 0x01)
283 				ERROUT(0);
284 			m_adj(m, ptr - start);
285 			NG_FWD_NEW_DATA(error, item, priv->ppp, m);
286 			break;
287 		}
288 	} else if (hook == priv->ppp) {
289 		M_PREPEND(m, 2, M_DONTWAIT);	/* Prepend PPP NLPID */
290 		if (!m)
291 			ERROUT(ENOBUFS);
292 		mtod(m, u_char *)[0] = HDLC_UI;
293 		mtod(m, u_char *)[1] = NLPID_PPP;
294 		NG_FWD_NEW_DATA(error, item, priv->downlink, m);
295 	} else if (hook == priv->inet) {
296 		M_PREPEND(m, 2, M_DONTWAIT);	/* Prepend IP NLPID */
297 		if (!m)
298 			ERROUT(ENOBUFS);
299 		mtod(m, u_char *)[0] = HDLC_UI;
300 		mtod(m, u_char *)[1] = NLPID_IP;
301 		NG_FWD_NEW_DATA(error, item, priv->downlink, m);
302 	} else if (hook == priv->ethernet) {
303 		M_PREPEND(m, 8, M_DONTWAIT);	/* Prepend NLPID, OUI, PID */
304 		if (!m)
305 			ERROUT(ENOBUFS);
306 		mtod(m, u_char *)[0] = HDLC_UI;
307 		mtod(m, u_char *)[1] = 0x00;		/* pad */
308 		mtod(m, u_char *)[2] = NLPID_SNAP;
309 		mtod(m, u_char *)[3] = 0x00;		/* OUI */
310 		mtod(m, u_char *)[4] = 0x80;
311 		mtod(m, u_char *)[5] = 0xc2;
312 		mtod(m, u_char *)[6] = 0x00;		/* PID */
313 		mtod(m, u_char *)[7] = 0x07;
314 		NG_FWD_NEW_DATA(error, item, priv->downlink, m);
315 	} else
316 		panic(__func__);
317 
318 done:
319 	if (item)
320 		NG_FREE_ITEM(item);
321 	NG_FREE_M(m);
322 	return (error);
323 }
324 
325 /*
326  * Nuke node
327  */
328 static int
329 ng_rfc1490_shutdown(node_p node)
330 {
331 	const priv_p priv = NG_NODE_PRIVATE(node);
332 
333 	/* Take down netgraph node */
334 	bzero(priv, sizeof(*priv));
335 	FREE(priv, M_NETGRAPH);
336 	NG_NODE_SET_PRIVATE(node, NULL);
337 	NG_NODE_UNREF(node);		/* let the node escape */
338 	return (0);
339 }
340 
341 /*
342  * Hook disconnection
343  */
344 static int
345 ng_rfc1490_disconnect(hook_p hook)
346 {
347 	const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
348 
349 	if ((NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0)
350 	&& (NG_NODE_IS_VALID(NG_HOOK_NODE(hook))))
351 		ng_rmnode_self(NG_HOOK_NODE(hook));
352 	else if (hook == priv->downlink)
353 		priv->downlink = NULL;
354 	else if (hook == priv->inet)
355 		priv->inet = NULL;
356 	else if (hook == priv->ppp)
357 		priv->ppp = NULL;
358 	else if (hook == priv->ethernet)
359 		priv->ethernet = NULL;
360 	else
361 		panic(__func__);
362 	return (0);
363 }
364 
365