xref: /freebsd/sys/netgraph/ng_rfc1490.c (revision a163d034fadcfb4a25ca34a2ba5f491c47b6ff69)
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 };
87 typedef struct ng_rfc1490_private *priv_p;
88 
89 /* Netgraph node methods */
90 static ng_constructor_t	ng_rfc1490_constructor;
91 static ng_rcvmsg_t	ng_rfc1490_rcvmsg;
92 static ng_shutdown_t	ng_rfc1490_shutdown;
93 static ng_newhook_t	ng_rfc1490_newhook;
94 static ng_rcvdata_t	ng_rfc1490_rcvdata;
95 static ng_disconnect_t	ng_rfc1490_disconnect;
96 
97 /* Node type descriptor */
98 static 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 };
112 NETGRAPH_INIT(rfc1490, &typestruct);
113 
114 /************************************************************************
115 			NETGRAPH NODE STUFF
116  ************************************************************************/
117 
118 /*
119  * Node constructor
120  */
121 static int
122 ng_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  */
140 static int
141 ng_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  */
165 static int
166 ng_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 
208 static int
209 ng_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_DONTWAIT);	/* 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_DONTWAIT);	/* 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 
297 done:
298 	if (item)
299 		NG_FREE_ITEM(item);
300 	NG_FREE_M(m);
301 	return (error);
302 }
303 
304 /*
305  * Nuke node
306  */
307 static int
308 ng_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  */
323 static int
324 ng_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