ng_rfc1490.c revision 129823
152419Sjulian
252419Sjulian/*
352419Sjulian * ng_rfc1490.c
452419Sjulian *
552419Sjulian * Copyright (c) 1996-1999 Whistle Communications, Inc.
652419Sjulian * All rights reserved.
752419Sjulian *
852419Sjulian * Subject to the following obligations and disclaimer of warranty, use and
952419Sjulian * redistribution of this software, in source or object code forms, with or
1052419Sjulian * without modifications are expressly permitted by Whistle Communications;
1152419Sjulian * provided, however, that:
1252419Sjulian * 1. Any and all reproductions of the source or object code must include the
1352419Sjulian *    copyright notice above and the following disclaimer of warranties; and
1452419Sjulian * 2. No rights are granted, in any manner or form, to use Whistle
1552419Sjulian *    Communications, Inc. trademarks, including the mark "WHISTLE
1652419Sjulian *    COMMUNICATIONS" on advertising, endorsements, or otherwise except as
1752419Sjulian *    such appears in the above copyright notice or in the software.
1852419Sjulian *
1952419Sjulian * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
2052419Sjulian * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
2152419Sjulian * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
2252419Sjulian * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
2352419Sjulian * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
2452419Sjulian * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
2552419Sjulian * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
2652419Sjulian * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
2752419Sjulian * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES
2852419Sjulian * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
2952419Sjulian * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
3052419Sjulian * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
3152419Sjulian * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
3252419Sjulian * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
3352419Sjulian * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
3452419Sjulian * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
3552419Sjulian * OF SUCH DAMAGE.
3652419Sjulian *
3767506Sjulian * Author: Julian Elischer <julian@freebsd.org>
3852419Sjulian *
3952419Sjulian * $FreeBSD: head/sys/netgraph/ng_rfc1490.c 129823 2004-05-29 00:51:19Z julian $
4052752Sjulian * $Whistle: ng_rfc1490.c,v 1.22 1999/11/01 09:24:52 julian Exp $
4152419Sjulian */
4252419Sjulian
4352419Sjulian/*
4452419Sjulian * This node does RFC 1490 multiplexing.
4552419Sjulian *
4652419Sjulian * NOTE: RFC 1490 is updated by RFC 2427.
4752419Sjulian */
4852419Sjulian
4952419Sjulian#include <sys/param.h>
5052419Sjulian#include <sys/systm.h>
5152419Sjulian#include <sys/errno.h>
5252419Sjulian#include <sys/kernel.h>
5352419Sjulian#include <sys/malloc.h>
5452419Sjulian#include <sys/mbuf.h>
5552419Sjulian#include <sys/errno.h>
5652419Sjulian#include <sys/socket.h>
5752419Sjulian
5852419Sjulian#include <net/if.h>
5952419Sjulian#include <netinet/in.h>
6052419Sjulian#include <netinet/if_ether.h>
6152419Sjulian
6252419Sjulian#include <netgraph/ng_message.h>
6352419Sjulian#include <netgraph/netgraph.h>
6452419Sjulian#include <netgraph/ng_rfc1490.h>
6552419Sjulian
6652419Sjulian/*
6752419Sjulian * DEFINITIONS
6852419Sjulian */
6952419Sjulian
7052419Sjulian/* Q.922 stuff -- see RFC 1490 */
7152419Sjulian#define HDLC_UI		0x03
7252419Sjulian
7352419Sjulian#define NLPID_IP	0xCC
7452419Sjulian#define NLPID_PPP	0xCF
7552419Sjulian#define NLPID_SNAP	0x80
7652419Sjulian#define NLPID_Q933	0x08
7752419Sjulian#define NLPID_CLNP	0x81
7852419Sjulian#define NLPID_ESIS	0x82
7952419Sjulian#define NLPID_ISIS	0x83
8052419Sjulian
8152419Sjulian/* Node private data */
8253407Sarchiestruct ng_rfc1490_private {
8352419Sjulian	hook_p  downlink;
8452419Sjulian	hook_p  ppp;
8552419Sjulian	hook_p  inet;
86124506Sgreen	hook_p  ethernet;
8752419Sjulian};
8853407Sarchietypedef struct ng_rfc1490_private *priv_p;
8952419Sjulian
9052419Sjulian/* Netgraph node methods */
9152752Sjulianstatic ng_constructor_t	ng_rfc1490_constructor;
9252752Sjulianstatic ng_rcvmsg_t	ng_rfc1490_rcvmsg;
9370700Sjulianstatic ng_shutdown_t	ng_rfc1490_shutdown;
9452752Sjulianstatic ng_newhook_t	ng_rfc1490_newhook;
9552752Sjulianstatic ng_rcvdata_t	ng_rfc1490_rcvdata;
9652752Sjulianstatic ng_disconnect_t	ng_rfc1490_disconnect;
9752419Sjulian
9852419Sjulian/* Node type descriptor */
9952419Sjulianstatic struct ng_type typestruct = {
100129823Sjulian	.version =	NG_ABI_VERSION,
101129823Sjulian	.name =		NG_RFC1490_NODE_TYPE,
102129823Sjulian	.constructor =	ng_rfc1490_constructor,
103129823Sjulian	.rcvmsg =	ng_rfc1490_rcvmsg,
104129823Sjulian	.shutdown =	ng_rfc1490_shutdown,
105129823Sjulian	.newhook =	ng_rfc1490_newhook,
106129823Sjulian	.rcvdata =	ng_rfc1490_rcvdata,
107129823Sjulian	.disconnect =	ng_rfc1490_disconnect,
10852419Sjulian};
10952419SjulianNETGRAPH_INIT(rfc1490, &typestruct);
11052419Sjulian
11152419Sjulian/************************************************************************
11252419Sjulian			NETGRAPH NODE STUFF
11352419Sjulian ************************************************************************/
11452419Sjulian
11552419Sjulian/*
11652419Sjulian * Node constructor
11752419Sjulian */
11852419Sjulianstatic int
11970700Sjulianng_rfc1490_constructor(node_p node)
12052419Sjulian{
12152419Sjulian	priv_p priv;
12252419Sjulian
12352419Sjulian	/* Allocate private structure */
12468876Sdwmalone	MALLOC(priv, priv_p, sizeof(*priv), M_NETGRAPH, M_NOWAIT | M_ZERO);
12552419Sjulian	if (priv == NULL)
12652419Sjulian		return (ENOMEM);
12752419Sjulian
12870784Sjulian	NG_NODE_SET_PRIVATE(node, priv);
12952419Sjulian
13052419Sjulian	/* Done */
13152419Sjulian	return (0);
13252419Sjulian}
13352419Sjulian
13452419Sjulian/*
13552419Sjulian * Give our ok for a hook to be added
13652419Sjulian */
13752419Sjulianstatic int
13852419Sjulianng_rfc1490_newhook(node_p node, hook_p hook, const char *name)
13952419Sjulian{
14070784Sjulian	const priv_p priv = NG_NODE_PRIVATE(node);
14152419Sjulian
14252419Sjulian	if (!strcmp(name, NG_RFC1490_HOOK_DOWNSTREAM)) {
14352419Sjulian		if (priv->downlink)
14452419Sjulian			return (EISCONN);
14552419Sjulian		priv->downlink = hook;
14652419Sjulian	} else if (!strcmp(name, NG_RFC1490_HOOK_PPP)) {
14752419Sjulian		if (priv->ppp)
14852419Sjulian			return (EISCONN);
14952419Sjulian		priv->ppp = hook;
15052419Sjulian	} else if (!strcmp(name, NG_RFC1490_HOOK_INET)) {
15152419Sjulian		if (priv->inet)
15252419Sjulian			return (EISCONN);
15352419Sjulian		priv->inet = hook;
154124506Sgreen	} else if (!strcmp(name, NG_RFC1490_HOOK_ETHERNET)) {
155124506Sgreen		if (priv->ethernet)
156124506Sgreen			return (EISCONN);
157124506Sgreen		priv->ethernet = hook;
15852419Sjulian	} else
15952419Sjulian		return (EINVAL);
16052419Sjulian	return (0);
16152419Sjulian}
16252419Sjulian
16352419Sjulian/*
16452419Sjulian * Receive a control message. We don't support any special ones.
16552419Sjulian */
16652419Sjulianstatic int
16770700Sjulianng_rfc1490_rcvmsg(node_p node, item_p item, hook_p lasthook)
16852419Sjulian{
16970700Sjulian	NG_FREE_ITEM(item);
17052419Sjulian	return (EINVAL);
17152419Sjulian}
17252419Sjulian
17352419Sjulian/*
17452419Sjulian * Receive data on a hook and encapsulate according to RFC 1490.
17552419Sjulian * Only those nodes marked (*) are supported by this routine so far.
17652419Sjulian *
17752419Sjulian *                            Q.922 control
17852419Sjulian *                                 |
17952419Sjulian *                                 |
18052419Sjulian *            --------------------------------------------
18152419Sjulian *            | 0x03                                     |
18252419Sjulian *           UI                                       I Frame
18352419Sjulian *            |                                          |
18452419Sjulian *      ---------------------------------         --------------
18552419Sjulian *      | 0x08  | 0x81  |0xCC   |0xCF   | 0x00    |..01....    |..10....
18652419Sjulian *      |       |       |       |       | 0x80    |            |
18752419Sjulian *     Q.933   CLNP    IP(*)   PPP(*)  SNAP     ISO 8208    ISO 8208
18852419Sjulian *      |                    (rfc1973)  |       Modulo 8    Modulo 128
18952419Sjulian *      |                               |
19052419Sjulian *      --------------------           OUI
19152419Sjulian *      |                  |            |
19252419Sjulian *     L2 ID              L3 ID      -------------------------
19352419Sjulian *      |               User         |00-80-C2               |00-00-00
19452419Sjulian *      |               specified    |                       |
19552419Sjulian *      |               0x70        PID                     Ethertype
19652419Sjulian *      |                            |                       |
197124506Sgreen *      -------------------        -----------------...     ----------
198124506Sgreen *      |0x51 |0x4E |     |0x4C    |0x7      |0xB  |        |0x806   |
199124506Sgreen *      |     |     |     |        |         |     |        |        |
200124506Sgreen *     7776  Q.922 Others 802.2   802.3(*)  802.6 Others    ARP(*)  Others
20152419Sjulian *
20252419Sjulian *
20352419Sjulian */
20452419Sjulian
20552419Sjulian#define MAX_ENCAPS_HDR	8
20652419Sjulian#define ERROUT(x)	do { error = (x); goto done; } while (0)
20752419Sjulian#define OUICMP(P,A,B,C)	((P)[0]==(A) && (P)[1]==(B) && (P)[2]==(C))
20852419Sjulian
20952419Sjulianstatic int
21070700Sjulianng_rfc1490_rcvdata(hook_p hook, item_p item)
21152419Sjulian{
21270784Sjulian	const node_p node = NG_HOOK_NODE(hook);
21370784Sjulian	const priv_p priv = NG_NODE_PRIVATE(node);
21452419Sjulian	int error = 0;
21570700Sjulian	struct mbuf *m;
21652419Sjulian
21770700Sjulian	NGI_GET_M(item, m);
21852419Sjulian	if (hook == priv->downlink) {
21997897Sarchie		const u_char *start;
22097897Sarchie		const u_char *ptr;
22152419Sjulian
22252539Sjulian		if (!m || (m->m_len < MAX_ENCAPS_HDR
22352539Sjulian		    && !(m = m_pullup(m, MAX_ENCAPS_HDR))))
22452419Sjulian			ERROUT(ENOBUFS);
22597897Sarchie		ptr = start = mtod(m, const u_char *);
22652419Sjulian
22752419Sjulian		/* Must be UI frame */
22852419Sjulian		if (*ptr++ != HDLC_UI)
22952419Sjulian			ERROUT(0);
23052419Sjulian
23152419Sjulian		/* Eat optional zero pad byte */
23252419Sjulian		if (*ptr == 0x00)
23352419Sjulian			ptr++;
23452419Sjulian
23552419Sjulian		/* Multiplex on NLPID */
23652419Sjulian		switch (*ptr++) {
23752419Sjulian		case NLPID_SNAP:
23852419Sjulian			if (OUICMP(ptr, 0, 0, 0)) {	/* It's an ethertype */
23952419Sjulian				u_int16_t etype;
24052419Sjulian
24152419Sjulian				ptr += 3;
24297897Sarchie				etype = ntohs(*((const u_int16_t *)ptr));
24352419Sjulian				ptr += 2;
24452419Sjulian				m_adj(m, ptr - start);
24552419Sjulian				switch (etype) {
24652419Sjulian				case ETHERTYPE_IP:
24770700Sjulian					NG_FWD_NEW_DATA(error, item,
24870700Sjulian					    priv->inet, m);
24952419Sjulian					break;
25052419Sjulian				case ETHERTYPE_ARP:
25152419Sjulian				case ETHERTYPE_REVARP:
25252419Sjulian				default:
25352419Sjulian					ERROUT(0);
25452419Sjulian				}
255124506Sgreen			} else if (OUICMP(ptr, 0x00, 0x80, 0xc2)) {
256124506Sgreen				/* 802.1 bridging */
257124506Sgreen				ptr += 3;
258124506Sgreen				if (*ptr++ != 0x00)
259124506Sgreen					ERROUT(0);     /* unknown PID octet 0 */
260124506Sgreen				if (*ptr++ != 0x07)
261124506Sgreen					ERROUT(0);	/* not FCS-less 802.3 */
262124506Sgreen				m_adj(m, ptr - start);
263124506Sgreen				NG_FWD_NEW_DATA(error, item, priv->ethernet, m);
264124506Sgreen			} else	/* Other weird stuff... */
26552419Sjulian				ERROUT(0);
26652419Sjulian			break;
26752419Sjulian		case NLPID_IP:
26852419Sjulian			m_adj(m, ptr - start);
26970700Sjulian			NG_FWD_NEW_DATA(error, item, priv->inet, m);
27052419Sjulian			break;
27152419Sjulian		case NLPID_PPP:
27252419Sjulian			m_adj(m, ptr - start);
27370700Sjulian			NG_FWD_NEW_DATA(error, item, priv->ppp, m);
27452419Sjulian			break;
27552419Sjulian		case NLPID_Q933:
27652419Sjulian		case NLPID_CLNP:
27752419Sjulian		case NLPID_ESIS:
27852419Sjulian		case NLPID_ISIS:
27952419Sjulian			ERROUT(0);
28052419Sjulian		default:	/* Try PPP (see RFC 1973) */
28152419Sjulian			ptr--;	/* NLPID becomes PPP proto */
28252419Sjulian			if ((*ptr & 0x01) == 0x01)
28352419Sjulian				ERROUT(0);
28452419Sjulian			m_adj(m, ptr - start);
28570700Sjulian			NG_FWD_NEW_DATA(error, item, priv->ppp, m);
28652419Sjulian			break;
28752419Sjulian		}
28852419Sjulian	} else if (hook == priv->ppp) {
289111119Simp		M_PREPEND(m, 2, M_DONTWAIT);	/* Prepend PPP NLPID */
29052419Sjulian		if (!m)
29152419Sjulian			ERROUT(ENOBUFS);
29252419Sjulian		mtod(m, u_char *)[0] = HDLC_UI;
29352419Sjulian		mtod(m, u_char *)[1] = NLPID_PPP;
29470700Sjulian		NG_FWD_NEW_DATA(error, item, priv->downlink, m);
29552419Sjulian	} else if (hook == priv->inet) {
296111119Simp		M_PREPEND(m, 2, M_DONTWAIT);	/* Prepend IP NLPID */
29752419Sjulian		if (!m)
29852419Sjulian			ERROUT(ENOBUFS);
29952419Sjulian		mtod(m, u_char *)[0] = HDLC_UI;
30052419Sjulian		mtod(m, u_char *)[1] = NLPID_IP;
30170700Sjulian		NG_FWD_NEW_DATA(error, item, priv->downlink, m);
302124506Sgreen	} else if (hook == priv->ethernet) {
303124506Sgreen		M_PREPEND(m, 8, M_DONTWAIT);	/* Prepend NLPID, OUI, PID */
304124506Sgreen		if (!m)
305124506Sgreen			ERROUT(ENOBUFS);
306124506Sgreen		mtod(m, u_char *)[0] = HDLC_UI;
307124506Sgreen		mtod(m, u_char *)[1] = 0x00;		/* pad */
308124506Sgreen		mtod(m, u_char *)[2] = NLPID_SNAP;
309124506Sgreen		mtod(m, u_char *)[3] = 0x00;		/* OUI */
310124506Sgreen		mtod(m, u_char *)[4] = 0x80;
311124506Sgreen		mtod(m, u_char *)[5] = 0xc2;
312124506Sgreen		mtod(m, u_char *)[6] = 0x00;		/* PID */
313124506Sgreen		mtod(m, u_char *)[7] = 0x07;
314124506Sgreen		NG_FWD_NEW_DATA(error, item, priv->downlink, m);
31552419Sjulian	} else
31687599Sobrien		panic(__func__);
31752419Sjulian
31852419Sjuliandone:
31970700Sjulian	if (item)
32070700Sjulian		NG_FREE_ITEM(item);
32170700Sjulian	NG_FREE_M(m);
32252419Sjulian	return (error);
32352419Sjulian}
32452419Sjulian
32552419Sjulian/*
32652419Sjulian * Nuke node
32752419Sjulian */
32852419Sjulianstatic int
32970700Sjulianng_rfc1490_shutdown(node_p node)
33052419Sjulian{
33170784Sjulian	const priv_p priv = NG_NODE_PRIVATE(node);
33252419Sjulian
33352419Sjulian	/* Take down netgraph node */
33452419Sjulian	bzero(priv, sizeof(*priv));
33570810Sjulian	FREE(priv, M_NETGRAPH);
33670784Sjulian	NG_NODE_SET_PRIVATE(node, NULL);
33770784Sjulian	NG_NODE_UNREF(node);		/* let the node escape */
33852419Sjulian	return (0);
33952419Sjulian}
34052419Sjulian
34152419Sjulian/*
34252419Sjulian * Hook disconnection
34352419Sjulian */
34452419Sjulianstatic int
34552419Sjulianng_rfc1490_disconnect(hook_p hook)
34652419Sjulian{
34770784Sjulian	const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
34852419Sjulian
34970784Sjulian	if ((NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0)
35070784Sjulian	&& (NG_NODE_IS_VALID(NG_HOOK_NODE(hook))))
35170784Sjulian		ng_rmnode_self(NG_HOOK_NODE(hook));
35252419Sjulian	else if (hook == priv->downlink)
35352419Sjulian		priv->downlink = NULL;
35452419Sjulian	else if (hook == priv->inet)
35552419Sjulian		priv->inet = NULL;
35652419Sjulian	else if (hook == priv->ppp)
35752419Sjulian		priv->ppp = NULL;
358124506Sgreen	else if (hook == priv->ethernet)
359124506Sgreen		priv->ethernet = NULL;
36052419Sjulian	else
36187599Sobrien		panic(__func__);
36252419Sjulian	return (0);
36352419Sjulian}
36452419Sjulian
365