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