xref: /dragonfly/sys/netgraph/rfc1490/ng_rfc1490.c (revision cfd1aba3)
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: src/sys/netgraph/ng_rfc1490.c,v 1.6.2.4 2002/07/02 22:17:18 archie Exp $
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/socket.h>
56 
57 #include <net/if.h>
58 #include <netinet/in.h>
59 #include <netinet/if_ether.h>
60 
61 #include <netgraph/ng_message.h>
62 #include <netgraph/netgraph.h>
63 #include "ng_rfc1490.h"
64 
65 /*
66  * DEFINITIONS
67  */
68 
69 /* Q.922 stuff -- see RFC 1490 */
70 #define HDLC_UI		0x03
71 
72 #define NLPID_IP	0xCC
73 #define NLPID_PPP	0xCF
74 #define NLPID_SNAP	0x80
75 #define NLPID_Q933	0x08
76 #define NLPID_CLNP	0x81
77 #define NLPID_ESIS	0x82
78 #define NLPID_ISIS	0x83
79 
80 /* Node private data */
81 struct ng_rfc1490_private {
82 	hook_p  downlink;
83 	hook_p  ppp;
84 	hook_p  inet;
85 };
86 typedef struct ng_rfc1490_private *priv_p;
87 
88 /* Netgraph node methods */
89 static ng_constructor_t	ng_rfc1490_constructor;
90 static ng_rcvmsg_t	ng_rfc1490_rcvmsg;
91 static ng_shutdown_t	ng_rfc1490_rmnode;
92 static ng_newhook_t	ng_rfc1490_newhook;
93 static ng_rcvdata_t	ng_rfc1490_rcvdata;
94 static ng_disconnect_t	ng_rfc1490_disconnect;
95 
96 /* Node type descriptor */
97 static struct ng_type typestruct = {
98 	NG_VERSION,
99 	NG_RFC1490_NODE_TYPE,
100 	NULL,
101 	ng_rfc1490_constructor,
102 	ng_rfc1490_rcvmsg,
103 	ng_rfc1490_rmnode,
104 	ng_rfc1490_newhook,
105 	NULL,
106 	NULL,
107 	ng_rfc1490_rcvdata,
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 *nodep)
123 {
124 	priv_p priv;
125 	int error;
126 
127 	/* Allocate private structure */
128 	priv = kmalloc(sizeof(*priv), M_NETGRAPH, M_NOWAIT | M_ZERO);
129 	if (priv == NULL)
130 		return (ENOMEM);
131 
132 	/* Call generic node constructor */
133 	if ((error = ng_make_node_common(&typestruct, nodep))) {
134 		kfree(priv, M_NETGRAPH);
135 		return (error);
136 	}
137 	(*nodep)->private = priv;
138 
139 	/* Done */
140 	return (0);
141 }
142 
143 /*
144  * Give our ok for a hook to be added
145  */
146 static int
147 ng_rfc1490_newhook(node_p node, hook_p hook, const char *name)
148 {
149 	const priv_p priv = node->private;
150 
151 	if (!strcmp(name, NG_RFC1490_HOOK_DOWNSTREAM)) {
152 		if (priv->downlink)
153 			return (EISCONN);
154 		priv->downlink = hook;
155 	} else if (!strcmp(name, NG_RFC1490_HOOK_PPP)) {
156 		if (priv->ppp)
157 			return (EISCONN);
158 		priv->ppp = hook;
159 	} else if (!strcmp(name, NG_RFC1490_HOOK_INET)) {
160 		if (priv->inet)
161 			return (EISCONN);
162 		priv->inet = hook;
163 	} else
164 		return (EINVAL);
165 	return (0);
166 }
167 
168 /*
169  * Receive a control message. We don't support any special ones.
170  */
171 static int
172 ng_rfc1490_rcvmsg(node_p node, struct ng_mesg *msg,
173 		  const char *raddr, struct ng_mesg **rp)
174 {
175 	kfree(msg, M_NETGRAPH);
176 	return (EINVAL);
177 }
178 
179 /*
180  * Receive data on a hook and encapsulate according to RFC 1490.
181  * Only those nodes marked (*) are supported by this routine so far.
182  *
183  *                            Q.922 control
184  *                                 |
185  *                                 |
186  *            --------------------------------------------
187  *            | 0x03                                     |
188  *           UI                                       I Frame
189  *            |                                          |
190  *      ---------------------------------         --------------
191  *      | 0x08  | 0x81  |0xCC   |0xCF   | 0x00    |..01....    |..10....
192  *      |       |       |       |       | 0x80    |            |
193  *     Q.933   CLNP    IP(*)   PPP(*)  SNAP     ISO 8208    ISO 8208
194  *      |                    (rfc1973)  |       Modulo 8    Modulo 128
195  *      |                               |
196  *      --------------------           OUI
197  *      |                  |            |
198  *     L2 ID              L3 ID      -------------------------
199  *      |               User         |00-80-C2               |00-00-00
200  *      |               specified    |                       |
201  *      |               0x70        PID                     Ethertype
202  *      |                            |                       |
203  *      -------------------        --------------...        ----------
204  *      |0x51 |0x4E |     |0x4C    |0x1   |0xB  |           |0x806   |
205  *      |     |     |     |        |      |     |           |        |
206  *     7776  Q.922 Others 802.2   802.3  802.6 Others       ARP(*)  Others
207  *
208  *
209  */
210 
211 #define MAX_ENCAPS_HDR	8
212 #define ERROUT(x)	do { error = (x); goto done; } while (0)
213 #define OUICMP(P,A,B,C)	((P)[0]==(A) && (P)[1]==(B) && (P)[2]==(C))
214 
215 static int
216 ng_rfc1490_rcvdata(hook_p hook, struct mbuf *m, meta_p meta)
217 {
218 	const node_p node = hook->node;
219 	const priv_p priv = node->private;
220 	int error = 0;
221 
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_SEND_DATA(error,
252 					    priv->inet, m, meta);
253 					break;
254 				case ETHERTYPE_ARP:
255 				case ETHERTYPE_REVARP:
256 				default:
257 					ERROUT(0);
258 				}
259 			} else if (OUICMP(ptr, 0x00, 0x80, 0xc2))	/* 802.1 bridging */
260 				ERROUT(0);
261 			else	/* Other weird stuff... */
262 				ERROUT(0);
263 			break;
264 		case NLPID_IP:
265 			m_adj(m, ptr - start);
266 			NG_SEND_DATA(error, priv->inet, m, meta);
267 			break;
268 		case NLPID_PPP:
269 			m_adj(m, ptr - start);
270 			NG_SEND_DATA(error, priv->ppp, m, meta);
271 			break;
272 		case NLPID_Q933:
273 		case NLPID_CLNP:
274 		case NLPID_ESIS:
275 		case NLPID_ISIS:
276 			ERROUT(0);
277 		default:	/* Try PPP (see RFC 1973) */
278 			ptr--;	/* NLPID becomes PPP proto */
279 			if ((*ptr & 0x01) == 0x01)
280 				ERROUT(0);
281 			m_adj(m, ptr - start);
282 			NG_SEND_DATA(error, priv->ppp, m, meta);
283 			break;
284 		}
285 	} else if (hook == priv->ppp) {
286 		M_PREPEND(m, 2, MB_DONTWAIT);	/* Prepend PPP NLPID */
287 		if (!m)
288 			ERROUT(ENOBUFS);
289 		mtod(m, u_char *)[0] = HDLC_UI;
290 		mtod(m, u_char *)[1] = NLPID_PPP;
291 		NG_SEND_DATA(error, priv->downlink, m, meta);
292 	} else if (hook == priv->inet) {
293 		M_PREPEND(m, 2, MB_DONTWAIT);	/* Prepend IP NLPID */
294 		if (!m)
295 			ERROUT(ENOBUFS);
296 		mtod(m, u_char *)[0] = HDLC_UI;
297 		mtod(m, u_char *)[1] = NLPID_IP;
298 		NG_SEND_DATA(error, priv->downlink, m, meta);
299 	} else
300 		panic(__func__);
301 
302 done:
303 	NG_FREE_DATA(m, meta);
304 	return (error);
305 }
306 
307 /*
308  * Nuke node
309  */
310 static int
311 ng_rfc1490_rmnode(node_p node)
312 {
313 	const priv_p priv = node->private;
314 
315 	/* Take down netgraph node */
316 	node->flags |= NG_INVALID;
317 	ng_cutlinks(node);
318 	ng_unname(node);
319 	bzero(priv, sizeof(*priv));
320 	node->private = NULL;
321 	kfree(priv, M_NETGRAPH);
322 	ng_unref(node);		/* let the node escape */
323 	return (0);
324 }
325 
326 /*
327  * Hook disconnection
328  */
329 static int
330 ng_rfc1490_disconnect(hook_p hook)
331 {
332 	const priv_p priv = hook->node->private;
333 
334 	if (hook->node->numhooks == 0)
335 		ng_rmnode(hook->node);
336 	else if (hook == priv->downlink)
337 		priv->downlink = NULL;
338 	else if (hook == priv->inet)
339 		priv->inet = NULL;
340 	else if (hook == priv->ppp)
341 		priv->ppp = NULL;
342 	else
343 		panic(__func__);
344 	return (0);
345 }
346 
347