ng_rfc1490.c revision 111119
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 111119 2003-02-19 05:47:46Z imp $ 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; 8652419Sjulian}; 8753407Sarchietypedef struct ng_rfc1490_private *priv_p; 8852419Sjulian 8952419Sjulian/* Netgraph node methods */ 9052752Sjulianstatic ng_constructor_t ng_rfc1490_constructor; 9152752Sjulianstatic ng_rcvmsg_t ng_rfc1490_rcvmsg; 9270700Sjulianstatic ng_shutdown_t ng_rfc1490_shutdown; 9352752Sjulianstatic ng_newhook_t ng_rfc1490_newhook; 9452752Sjulianstatic ng_rcvdata_t ng_rfc1490_rcvdata; 9552752Sjulianstatic ng_disconnect_t ng_rfc1490_disconnect; 9652419Sjulian 9752419Sjulian/* Node type descriptor */ 9852419Sjulianstatic struct ng_type typestruct = { 9970159Sjulian NG_ABI_VERSION, 10052419Sjulian NG_RFC1490_NODE_TYPE, 10152419Sjulian NULL, 10252419Sjulian ng_rfc1490_constructor, 10352419Sjulian ng_rfc1490_rcvmsg, 10470700Sjulian ng_rfc1490_shutdown, 10552419Sjulian ng_rfc1490_newhook, 10652419Sjulian NULL, 10752419Sjulian NULL, 10852419Sjulian ng_rfc1490_rcvdata, 10953913Sarchie ng_rfc1490_disconnect, 11053913Sarchie NULL 11152419Sjulian}; 11252419SjulianNETGRAPH_INIT(rfc1490, &typestruct); 11352419Sjulian 11452419Sjulian/************************************************************************ 11552419Sjulian NETGRAPH NODE STUFF 11652419Sjulian ************************************************************************/ 11752419Sjulian 11852419Sjulian/* 11952419Sjulian * Node constructor 12052419Sjulian */ 12152419Sjulianstatic int 12270700Sjulianng_rfc1490_constructor(node_p node) 12352419Sjulian{ 12452419Sjulian priv_p priv; 12552419Sjulian 12652419Sjulian /* Allocate private structure */ 12768876Sdwmalone MALLOC(priv, priv_p, sizeof(*priv), M_NETGRAPH, M_NOWAIT | M_ZERO); 12852419Sjulian if (priv == NULL) 12952419Sjulian return (ENOMEM); 13052419Sjulian 13170784Sjulian NG_NODE_SET_PRIVATE(node, priv); 13252419Sjulian 13352419Sjulian /* Done */ 13452419Sjulian return (0); 13552419Sjulian} 13652419Sjulian 13752419Sjulian/* 13852419Sjulian * Give our ok for a hook to be added 13952419Sjulian */ 14052419Sjulianstatic int 14152419Sjulianng_rfc1490_newhook(node_p node, hook_p hook, const char *name) 14252419Sjulian{ 14370784Sjulian const priv_p priv = NG_NODE_PRIVATE(node); 14452419Sjulian 14552419Sjulian if (!strcmp(name, NG_RFC1490_HOOK_DOWNSTREAM)) { 14652419Sjulian if (priv->downlink) 14752419Sjulian return (EISCONN); 14852419Sjulian priv->downlink = hook; 14952419Sjulian } else if (!strcmp(name, NG_RFC1490_HOOK_PPP)) { 15052419Sjulian if (priv->ppp) 15152419Sjulian return (EISCONN); 15252419Sjulian priv->ppp = hook; 15352419Sjulian } else if (!strcmp(name, NG_RFC1490_HOOK_INET)) { 15452419Sjulian if (priv->inet) 15552419Sjulian return (EISCONN); 15652419Sjulian priv->inet = hook; 15752419Sjulian } else 15852419Sjulian return (EINVAL); 15952419Sjulian return (0); 16052419Sjulian} 16152419Sjulian 16252419Sjulian/* 16352419Sjulian * Receive a control message. We don't support any special ones. 16452419Sjulian */ 16552419Sjulianstatic int 16670700Sjulianng_rfc1490_rcvmsg(node_p node, item_p item, hook_p lasthook) 16752419Sjulian{ 16870700Sjulian NG_FREE_ITEM(item); 16952419Sjulian return (EINVAL); 17052419Sjulian} 17152419Sjulian 17252419Sjulian/* 17352419Sjulian * Receive data on a hook and encapsulate according to RFC 1490. 17452419Sjulian * Only those nodes marked (*) are supported by this routine so far. 17552419Sjulian * 17652419Sjulian * Q.922 control 17752419Sjulian * | 17852419Sjulian * | 17952419Sjulian * -------------------------------------------- 18052419Sjulian * | 0x03 | 18152419Sjulian * UI I Frame 18252419Sjulian * | | 18352419Sjulian * --------------------------------- -------------- 18452419Sjulian * | 0x08 | 0x81 |0xCC |0xCF | 0x00 |..01.... |..10.... 18552419Sjulian * | | | | | 0x80 | | 18652419Sjulian * Q.933 CLNP IP(*) PPP(*) SNAP ISO 8208 ISO 8208 18752419Sjulian * | (rfc1973) | Modulo 8 Modulo 128 18852419Sjulian * | | 18952419Sjulian * -------------------- OUI 19052419Sjulian * | | | 19152419Sjulian * L2 ID L3 ID ------------------------- 19252419Sjulian * | User |00-80-C2 |00-00-00 19352419Sjulian * | specified | | 19452419Sjulian * | 0x70 PID Ethertype 19552419Sjulian * | | | 19652419Sjulian * ------------------- --------------... ---------- 19752419Sjulian * |0x51 |0x4E | |0x4C |0x1 |0xB | |0x806 | 19852419Sjulian * | | | | | | | | | 19952419Sjulian * 7776 Q.922 Others 802.2 802.3 802.6 Others ARP(*) Others 20052419Sjulian * 20152419Sjulian * 20252419Sjulian */ 20352419Sjulian 20452419Sjulian#define MAX_ENCAPS_HDR 8 20552419Sjulian#define ERROUT(x) do { error = (x); goto done; } while (0) 20652419Sjulian#define OUICMP(P,A,B,C) ((P)[0]==(A) && (P)[1]==(B) && (P)[2]==(C)) 20752419Sjulian 20852419Sjulianstatic int 20970700Sjulianng_rfc1490_rcvdata(hook_p hook, item_p item) 21052419Sjulian{ 21170784Sjulian const node_p node = NG_HOOK_NODE(hook); 21270784Sjulian const priv_p priv = NG_NODE_PRIVATE(node); 21352419Sjulian int error = 0; 21470700Sjulian struct mbuf *m; 21552419Sjulian 21670700Sjulian NGI_GET_M(item, m); 21752419Sjulian if (hook == priv->downlink) { 21897897Sarchie const u_char *start; 21997897Sarchie const u_char *ptr; 22052419Sjulian 22152539Sjulian if (!m || (m->m_len < MAX_ENCAPS_HDR 22252539Sjulian && !(m = m_pullup(m, MAX_ENCAPS_HDR)))) 22352419Sjulian ERROUT(ENOBUFS); 22497897Sarchie ptr = start = mtod(m, const u_char *); 22552419Sjulian 22652419Sjulian /* Must be UI frame */ 22752419Sjulian if (*ptr++ != HDLC_UI) 22852419Sjulian ERROUT(0); 22952419Sjulian 23052419Sjulian /* Eat optional zero pad byte */ 23152419Sjulian if (*ptr == 0x00) 23252419Sjulian ptr++; 23352419Sjulian 23452419Sjulian /* Multiplex on NLPID */ 23552419Sjulian switch (*ptr++) { 23652419Sjulian case NLPID_SNAP: 23752419Sjulian if (OUICMP(ptr, 0, 0, 0)) { /* It's an ethertype */ 23852419Sjulian u_int16_t etype; 23952419Sjulian 24052419Sjulian ptr += 3; 24197897Sarchie etype = ntohs(*((const u_int16_t *)ptr)); 24252419Sjulian ptr += 2; 24352419Sjulian m_adj(m, ptr - start); 24452419Sjulian switch (etype) { 24552419Sjulian case ETHERTYPE_IP: 24670700Sjulian NG_FWD_NEW_DATA(error, item, 24770700Sjulian priv->inet, m); 24852419Sjulian break; 24952419Sjulian case ETHERTYPE_ARP: 25052419Sjulian case ETHERTYPE_REVARP: 25152419Sjulian default: 25252419Sjulian ERROUT(0); 25352419Sjulian } 25452419Sjulian } else if (OUICMP(ptr, 0x00, 0x80, 0xc2)) /* 802.1 bridging */ 25552419Sjulian ERROUT(0); 25652419Sjulian else /* Other weird stuff... */ 25752419Sjulian ERROUT(0); 25852419Sjulian break; 25952419Sjulian case NLPID_IP: 26052419Sjulian m_adj(m, ptr - start); 26170700Sjulian NG_FWD_NEW_DATA(error, item, priv->inet, m); 26252419Sjulian break; 26352419Sjulian case NLPID_PPP: 26452419Sjulian m_adj(m, ptr - start); 26570700Sjulian NG_FWD_NEW_DATA(error, item, priv->ppp, m); 26652419Sjulian break; 26752419Sjulian case NLPID_Q933: 26852419Sjulian case NLPID_CLNP: 26952419Sjulian case NLPID_ESIS: 27052419Sjulian case NLPID_ISIS: 27152419Sjulian ERROUT(0); 27252419Sjulian default: /* Try PPP (see RFC 1973) */ 27352419Sjulian ptr--; /* NLPID becomes PPP proto */ 27452419Sjulian if ((*ptr & 0x01) == 0x01) 27552419Sjulian ERROUT(0); 27652419Sjulian m_adj(m, ptr - start); 27770700Sjulian NG_FWD_NEW_DATA(error, item, priv->ppp, m); 27852419Sjulian break; 27952419Sjulian } 28052419Sjulian } else if (hook == priv->ppp) { 281111119Simp M_PREPEND(m, 2, M_DONTWAIT); /* Prepend PPP NLPID */ 28252419Sjulian if (!m) 28352419Sjulian ERROUT(ENOBUFS); 28452419Sjulian mtod(m, u_char *)[0] = HDLC_UI; 28552419Sjulian mtod(m, u_char *)[1] = NLPID_PPP; 28670700Sjulian NG_FWD_NEW_DATA(error, item, priv->downlink, m); 28752419Sjulian } else if (hook == priv->inet) { 288111119Simp M_PREPEND(m, 2, M_DONTWAIT); /* Prepend IP NLPID */ 28952419Sjulian if (!m) 29052419Sjulian ERROUT(ENOBUFS); 29152419Sjulian mtod(m, u_char *)[0] = HDLC_UI; 29252419Sjulian mtod(m, u_char *)[1] = NLPID_IP; 29370700Sjulian NG_FWD_NEW_DATA(error, item, priv->downlink, m); 29452419Sjulian } else 29587599Sobrien panic(__func__); 29652419Sjulian 29752419Sjuliandone: 29870700Sjulian if (item) 29970700Sjulian NG_FREE_ITEM(item); 30070700Sjulian NG_FREE_M(m); 30152419Sjulian return (error); 30252419Sjulian} 30352419Sjulian 30452419Sjulian/* 30552419Sjulian * Nuke node 30652419Sjulian */ 30752419Sjulianstatic int 30870700Sjulianng_rfc1490_shutdown(node_p node) 30952419Sjulian{ 31070784Sjulian const priv_p priv = NG_NODE_PRIVATE(node); 31152419Sjulian 31252419Sjulian /* Take down netgraph node */ 31352419Sjulian bzero(priv, sizeof(*priv)); 31470810Sjulian FREE(priv, M_NETGRAPH); 31570784Sjulian NG_NODE_SET_PRIVATE(node, NULL); 31670784Sjulian NG_NODE_UNREF(node); /* let the node escape */ 31752419Sjulian return (0); 31852419Sjulian} 31952419Sjulian 32052419Sjulian/* 32152419Sjulian * Hook disconnection 32252419Sjulian */ 32352419Sjulianstatic int 32452419Sjulianng_rfc1490_disconnect(hook_p hook) 32552419Sjulian{ 32670784Sjulian const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook)); 32752419Sjulian 32870784Sjulian if ((NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0) 32970784Sjulian && (NG_NODE_IS_VALID(NG_HOOK_NODE(hook)))) 33070784Sjulian ng_rmnode_self(NG_HOOK_NODE(hook)); 33152419Sjulian else if (hook == priv->downlink) 33252419Sjulian priv->downlink = NULL; 33352419Sjulian else if (hook == priv->inet) 33452419Sjulian priv->inet = NULL; 33552419Sjulian else if (hook == priv->ppp) 33652419Sjulian priv->ppp = NULL; 33752419Sjulian else 33887599Sobrien panic(__func__); 33952419Sjulian return (0); 34052419Sjulian} 34152419Sjulian 342