ng_rfc1490.c revision 52752
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 *
3752419Sjulian * Author: Julian Elischer <julian@whistle.com>
3852419Sjulian *
3952419Sjulian * $FreeBSD: head/sys/netgraph/ng_rfc1490.c 52752 1999-11-01 10:00:40Z 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/conf.h>
5652419Sjulian#include <sys/errno.h>
5752419Sjulian#include <sys/socket.h>
5852419Sjulian#include <sys/syslog.h>
5952419Sjulian
6052419Sjulian#include <net/if.h>
6152419Sjulian#include <netinet/in.h>
6252419Sjulian#include <netinet/if_ether.h>
6352419Sjulian
6452419Sjulian#include <netgraph/ng_message.h>
6552419Sjulian#include <netgraph/netgraph.h>
6652419Sjulian#include <netgraph/ng_rfc1490.h>
6752419Sjulian
6852419Sjulian/*
6952419Sjulian * DEFINITIONS
7052419Sjulian */
7152419Sjulian
7252419Sjulian/* Q.922 stuff -- see RFC 1490 */
7352419Sjulian#define HDLC_UI		0x03
7452419Sjulian
7552419Sjulian#define NLPID_IP	0xCC
7652419Sjulian#define NLPID_PPP	0xCF
7752419Sjulian#define NLPID_SNAP	0x80
7852419Sjulian#define NLPID_Q933	0x08
7952419Sjulian#define NLPID_CLNP	0x81
8052419Sjulian#define NLPID_ESIS	0x82
8152419Sjulian#define NLPID_ISIS	0x83
8252419Sjulian
8352419Sjulian/* Node private data */
8452419Sjulianstruct private {
8552419Sjulian	hook_p  downlink;
8652419Sjulian	hook_p  ppp;
8752419Sjulian	hook_p  inet;
8852419Sjulian};
8952419Sjuliantypedef struct private *priv_p;
9052419Sjulian
9152419Sjulian/* Netgraph node methods */
9252752Sjulianstatic ng_constructor_t	ng_rfc1490_constructor;
9352752Sjulianstatic ng_rcvmsg_t	ng_rfc1490_rcvmsg;
9452752Sjulianstatic ng_shutdown_t	ng_rfc1490_rmnode;
9552752Sjulianstatic ng_newhook_t	ng_rfc1490_newhook;
9652752Sjulianstatic ng_rcvdata_t	ng_rfc1490_rcvdata;
9752752Sjulianstatic ng_disconnect_t	ng_rfc1490_disconnect;
9852419Sjulian
9952419Sjulian/* Node type descriptor */
10052419Sjulianstatic struct ng_type typestruct = {
10152419Sjulian	NG_VERSION,
10252419Sjulian	NG_RFC1490_NODE_TYPE,
10352419Sjulian	NULL,
10452419Sjulian	ng_rfc1490_constructor,
10552419Sjulian	ng_rfc1490_rcvmsg,
10652419Sjulian	ng_rfc1490_rmnode,
10752419Sjulian	ng_rfc1490_newhook,
10852419Sjulian	NULL,
10952419Sjulian	NULL,
11052419Sjulian	ng_rfc1490_rcvdata,
11152419Sjulian	ng_rfc1490_rcvdata,
11252419Sjulian	ng_rfc1490_disconnect
11352419Sjulian};
11452419SjulianNETGRAPH_INIT(rfc1490, &typestruct);
11552419Sjulian
11652419Sjulian/************************************************************************
11752419Sjulian			NETGRAPH NODE STUFF
11852419Sjulian ************************************************************************/
11952419Sjulian
12052419Sjulian/*
12152419Sjulian * Node constructor
12252419Sjulian */
12352419Sjulianstatic int
12452419Sjulianng_rfc1490_constructor(node_p *nodep)
12552419Sjulian{
12652419Sjulian	priv_p priv;
12752419Sjulian	int error;
12852419Sjulian
12952419Sjulian	/* Allocate private structure */
13052419Sjulian	MALLOC(priv, priv_p, sizeof(*priv), M_NETGRAPH, M_WAITOK);
13152419Sjulian	if (priv == NULL)
13252419Sjulian		return (ENOMEM);
13352419Sjulian	bzero(priv, sizeof(*priv));
13452419Sjulian
13552419Sjulian	/* Call generic node constructor */
13652419Sjulian	if ((error = ng_make_node_common(&typestruct, nodep))) {
13752419Sjulian		FREE(priv, M_NETGRAPH);
13852419Sjulian		return (error);
13952419Sjulian	}
14052419Sjulian	(*nodep)->private = priv;
14152419Sjulian
14252419Sjulian	/* Done */
14352419Sjulian	return (0);
14452419Sjulian}
14552419Sjulian
14652419Sjulian/*
14752419Sjulian * Give our ok for a hook to be added
14852419Sjulian */
14952419Sjulianstatic int
15052419Sjulianng_rfc1490_newhook(node_p node, hook_p hook, const char *name)
15152419Sjulian{
15252419Sjulian	const priv_p priv = node->private;
15352419Sjulian
15452419Sjulian	if (!strcmp(name, NG_RFC1490_HOOK_DOWNSTREAM)) {
15552419Sjulian		if (priv->downlink)
15652419Sjulian			return (EISCONN);
15752419Sjulian		priv->downlink = hook;
15852419Sjulian	} else if (!strcmp(name, NG_RFC1490_HOOK_PPP)) {
15952419Sjulian		if (priv->ppp)
16052419Sjulian			return (EISCONN);
16152419Sjulian		priv->ppp = hook;
16252419Sjulian	} else if (!strcmp(name, NG_RFC1490_HOOK_INET)) {
16352419Sjulian		if (priv->inet)
16452419Sjulian			return (EISCONN);
16552419Sjulian		priv->inet = hook;
16652419Sjulian	} else
16752419Sjulian		return (EINVAL);
16852419Sjulian	return (0);
16952419Sjulian}
17052419Sjulian
17152419Sjulian/*
17252419Sjulian * Receive a control message. We don't support any special ones.
17352419Sjulian */
17452419Sjulianstatic int
17552419Sjulianng_rfc1490_rcvmsg(node_p node, struct ng_mesg *msg,
17652419Sjulian		  const char *raddr, struct ng_mesg **rp)
17752419Sjulian{
17852419Sjulian	FREE(msg, M_NETGRAPH);
17952419Sjulian	return (EINVAL);
18052419Sjulian}
18152419Sjulian
18252419Sjulian/*
18352419Sjulian * Receive data on a hook and encapsulate according to RFC 1490.
18452419Sjulian * Only those nodes marked (*) are supported by this routine so far.
18552419Sjulian *
18652419Sjulian *                            Q.922 control
18752419Sjulian *                                 |
18852419Sjulian *                                 |
18952419Sjulian *            --------------------------------------------
19052419Sjulian *            | 0x03                                     |
19152419Sjulian *           UI                                       I Frame
19252419Sjulian *            |                                          |
19352419Sjulian *      ---------------------------------         --------------
19452419Sjulian *      | 0x08  | 0x81  |0xCC   |0xCF   | 0x00    |..01....    |..10....
19552419Sjulian *      |       |       |       |       | 0x80    |            |
19652419Sjulian *     Q.933   CLNP    IP(*)   PPP(*)  SNAP     ISO 8208    ISO 8208
19752419Sjulian *      |                    (rfc1973)  |       Modulo 8    Modulo 128
19852419Sjulian *      |                               |
19952419Sjulian *      --------------------           OUI
20052419Sjulian *      |                  |            |
20152419Sjulian *     L2 ID              L3 ID      -------------------------
20252419Sjulian *      |               User         |00-80-C2               |00-00-00
20352419Sjulian *      |               specified    |                       |
20452419Sjulian *      |               0x70        PID                     Ethertype
20552419Sjulian *      |                            |                       |
20652419Sjulian *      -------------------        --------------...        ----------
20752419Sjulian *      |0x51 |0x4E |     |0x4C    |0x1   |0xB  |           |0x806   |
20852419Sjulian *      |     |     |     |        |      |     |           |        |
20952419Sjulian *     7776  Q.922 Others 802.2   802.3  802.6 Others       ARP(*)  Others
21052419Sjulian *
21152419Sjulian *
21252419Sjulian */
21352419Sjulian
21452419Sjulian#define MAX_ENCAPS_HDR	8
21552419Sjulian#define ERROUT(x)	do { error = (x); goto done; } while (0)
21652419Sjulian#define OUICMP(P,A,B,C)	((P)[0]==(A) && (P)[1]==(B) && (P)[2]==(C))
21752419Sjulian
21852419Sjulianstatic int
21952419Sjulianng_rfc1490_rcvdata(hook_p hook, struct mbuf *m, meta_p meta)
22052419Sjulian{
22152419Sjulian	const node_p node = hook->node;
22252419Sjulian	const priv_p priv = node->private;
22352419Sjulian	int error = 0;
22452419Sjulian
22552419Sjulian	if (hook == priv->downlink) {
22652419Sjulian		u_char *start, *ptr;
22752419Sjulian
22852539Sjulian		if (!m || (m->m_len < MAX_ENCAPS_HDR
22952539Sjulian		    && !(m = m_pullup(m, MAX_ENCAPS_HDR))))
23052419Sjulian			ERROUT(ENOBUFS);
23152419Sjulian		ptr = start = mtod(m, u_char *);
23252419Sjulian
23352419Sjulian		/* Must be UI frame */
23452419Sjulian		if (*ptr++ != HDLC_UI)
23552419Sjulian			ERROUT(0);
23652419Sjulian
23752419Sjulian		/* Eat optional zero pad byte */
23852419Sjulian		if (*ptr == 0x00)
23952419Sjulian			ptr++;
24052419Sjulian
24152419Sjulian		/* Multiplex on NLPID */
24252419Sjulian		switch (*ptr++) {
24352419Sjulian		case NLPID_SNAP:
24452419Sjulian			if (OUICMP(ptr, 0, 0, 0)) {	/* It's an ethertype */
24552419Sjulian				u_int16_t etype;
24652419Sjulian
24752419Sjulian				ptr += 3;
24852419Sjulian				etype = ntohs(*((u_int16_t *) ptr));
24952419Sjulian				ptr += 2;
25052419Sjulian				m_adj(m, ptr - start);
25152419Sjulian				switch (etype) {
25252419Sjulian				case ETHERTYPE_IP:
25352419Sjulian					NG_SEND_DATA(error,
25452419Sjulian					    priv->inet, m, meta);
25552419Sjulian					break;
25652419Sjulian				case ETHERTYPE_ARP:
25752419Sjulian				case ETHERTYPE_REVARP:
25852419Sjulian				default:
25952419Sjulian					ERROUT(0);
26052419Sjulian				}
26152419Sjulian			} else if (OUICMP(ptr, 0x00, 0x80, 0xc2))	/* 802.1 bridging */
26252419Sjulian				ERROUT(0);
26352419Sjulian			else	/* Other weird stuff... */
26452419Sjulian				ERROUT(0);
26552419Sjulian			break;
26652419Sjulian		case NLPID_IP:
26752419Sjulian			m_adj(m, ptr - start);
26852419Sjulian			NG_SEND_DATA(error, priv->inet, m, meta);
26952419Sjulian			break;
27052419Sjulian		case NLPID_PPP:
27152419Sjulian			m_adj(m, ptr - start);
27252419Sjulian			NG_SEND_DATA(error, priv->ppp, m, meta);
27352419Sjulian			break;
27452419Sjulian		case NLPID_Q933:
27552419Sjulian		case NLPID_CLNP:
27652419Sjulian		case NLPID_ESIS:
27752419Sjulian		case NLPID_ISIS:
27852419Sjulian			ERROUT(0);
27952419Sjulian		default:	/* Try PPP (see RFC 1973) */
28052419Sjulian			ptr--;	/* NLPID becomes PPP proto */
28152419Sjulian			if ((*ptr & 0x01) == 0x01)
28252419Sjulian				ERROUT(0);
28352419Sjulian			m_adj(m, ptr - start);
28452419Sjulian			NG_SEND_DATA(error, priv->ppp, m, meta);
28552419Sjulian			break;
28652419Sjulian		}
28752419Sjulian	} else if (hook == priv->ppp) {
28852419Sjulian		M_PREPEND(m, 2, M_DONTWAIT);	/* Prepend PPP NLPID */
28952419Sjulian		if (!m)
29052419Sjulian			ERROUT(ENOBUFS);
29152419Sjulian		mtod(m, u_char *)[0] = HDLC_UI;
29252419Sjulian		mtod(m, u_char *)[1] = NLPID_PPP;
29352419Sjulian		NG_SEND_DATA(error, priv->downlink, m, meta);
29452419Sjulian	} else if (hook == priv->inet) {
29552419Sjulian		M_PREPEND(m, 2, M_DONTWAIT);	/* Prepend IP NLPID */
29652419Sjulian		if (!m)
29752419Sjulian			ERROUT(ENOBUFS);
29852419Sjulian		mtod(m, u_char *)[0] = HDLC_UI;
29952419Sjulian		mtod(m, u_char *)[1] = NLPID_IP;
30052419Sjulian		NG_SEND_DATA(error, priv->downlink, m, meta);
30152419Sjulian	} else
30252419Sjulian		panic(__FUNCTION__);
30352419Sjulian
30452419Sjuliandone:
30552419Sjulian	NG_FREE_DATA(m, meta);
30652419Sjulian	return (error);
30752419Sjulian}
30852419Sjulian
30952419Sjulian/*
31052419Sjulian * Nuke node
31152419Sjulian */
31252419Sjulianstatic int
31352419Sjulianng_rfc1490_rmnode(node_p node)
31452419Sjulian{
31552419Sjulian	const priv_p priv = node->private;
31652419Sjulian
31752419Sjulian	/* Take down netgraph node */
31852419Sjulian	node->flags |= NG_INVALID;
31952419Sjulian	ng_cutlinks(node);
32052419Sjulian	ng_unname(node);
32152419Sjulian	bzero(priv, sizeof(*priv));
32252419Sjulian	node->private = NULL;
32352419Sjulian	ng_unref(node);		/* let the node escape */
32452419Sjulian	return (0);
32552419Sjulian}
32652419Sjulian
32752419Sjulian/*
32852419Sjulian * Hook disconnection
32952419Sjulian */
33052419Sjulianstatic int
33152419Sjulianng_rfc1490_disconnect(hook_p hook)
33252419Sjulian{
33352419Sjulian	const priv_p priv = hook->node->private;
33452419Sjulian
33552419Sjulian	if (hook->node->numhooks == 0)
33652419Sjulian		ng_rmnode(hook->node);
33752419Sjulian	else if (hook == priv->downlink)
33852419Sjulian		priv->downlink = NULL;
33952419Sjulian	else if (hook == priv->inet)
34052419Sjulian		priv->inet = NULL;
34152419Sjulian	else if (hook == priv->ppp)
34252419Sjulian		priv->ppp = NULL;
34352419Sjulian	else
34452419Sjulian		panic(__FUNCTION__);
34552419Sjulian	return (0);
34652419Sjulian}
34752419Sjulian
348