5 * Copyright (c) 1996-1999 Whistle Communications, Inc.
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.
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
37 * Author: Julian Elischer <julian@freebsd.org>
39 * $FreeBSD: src/sys/netgraph/ng_rfc1490.c,v 1.6.2.4 2002/07/02 22:17:18 archie Exp $
40 * $DragonFly: src/sys/netgraph/rfc1490/ng_rfc1490.c,v 1.2 2003/06/17 04:28:51 dillon Exp $
41 * $Whistle: ng_rfc1490.c,v 1.22 1999/11/01 09:24:52 julian Exp $
45 * This node does RFC 1490 multiplexing.
47 * NOTE: RFC 1490 is updated by RFC 2427.
50 #include <sys/param.h>
51 #include <sys/systm.h>
52 #include <sys/errno.h>
53 #include <sys/kernel.h>
54 #include <sys/malloc.h>
56 #include <sys/errno.h>
57 #include <sys/socket.h>
60 #include <netinet/in.h>
61 #include <netinet/if_ether.h>
63 #include <netgraph/ng_message.h>
64 #include <netgraph/netgraph.h>
65 #include <netgraph/ng_rfc1490.h>
71 /* Q.922 stuff -- see RFC 1490 */
75 #define NLPID_PPP 0xCF
76 #define NLPID_SNAP 0x80
77 #define NLPID_Q933 0x08
78 #define NLPID_CLNP 0x81
79 #define NLPID_ESIS 0x82
80 #define NLPID_ISIS 0x83
82 /* Node private data */
83 struct ng_rfc1490_private {
88 typedef struct ng_rfc1490_private *priv_p;
90 /* Netgraph node methods */
91 static ng_constructor_t ng_rfc1490_constructor;
92 static ng_rcvmsg_t ng_rfc1490_rcvmsg;
93 static ng_shutdown_t ng_rfc1490_rmnode;
94 static ng_newhook_t ng_rfc1490_newhook;
95 static ng_rcvdata_t ng_rfc1490_rcvdata;
96 static ng_disconnect_t ng_rfc1490_disconnect;
98 /* Node type descriptor */
99 static struct ng_type typestruct = {
101 NG_RFC1490_NODE_TYPE,
103 ng_rfc1490_constructor,
111 ng_rfc1490_disconnect,
114 NETGRAPH_INIT(rfc1490, &typestruct);
116 /************************************************************************
118 ************************************************************************/
124 ng_rfc1490_constructor(node_p *nodep)
129 /* Allocate private structure */
130 MALLOC(priv, priv_p, sizeof(*priv), M_NETGRAPH, M_NOWAIT);
133 bzero(priv, sizeof(*priv));
135 /* Call generic node constructor */
136 if ((error = ng_make_node_common(&typestruct, nodep))) {
137 FREE(priv, M_NETGRAPH);
140 (*nodep)->private = priv;
147 * Give our ok for a hook to be added
150 ng_rfc1490_newhook(node_p node, hook_p hook, const char *name)
152 const priv_p priv = node->private;
154 if (!strcmp(name, NG_RFC1490_HOOK_DOWNSTREAM)) {
157 priv->downlink = hook;
158 } else if (!strcmp(name, NG_RFC1490_HOOK_PPP)) {
162 } else if (!strcmp(name, NG_RFC1490_HOOK_INET)) {
172 * Receive a control message. We don't support any special ones.
175 ng_rfc1490_rcvmsg(node_p node, struct ng_mesg *msg,
176 const char *raddr, struct ng_mesg **rp)
178 FREE(msg, M_NETGRAPH);
183 * Receive data on a hook and encapsulate according to RFC 1490.
184 * Only those nodes marked (*) are supported by this routine so far.
189 * --------------------------------------------
193 * --------------------------------- --------------
194 * | 0x08 | 0x81 |0xCC |0xCF | 0x00 |..01.... |..10....
196 * Q.933 CLNP IP(*) PPP(*) SNAP ISO 8208 ISO 8208
197 * | (rfc1973) | Modulo 8 Modulo 128
199 * -------------------- OUI
201 * L2 ID L3 ID -------------------------
202 * | User |00-80-C2 |00-00-00
204 * | 0x70 PID Ethertype
206 * ------------------- --------------... ----------
207 * |0x51 |0x4E | |0x4C |0x1 |0xB | |0x806 |
209 * 7776 Q.922 Others 802.2 802.3 802.6 Others ARP(*) Others
214 #define MAX_ENCAPS_HDR 8
215 #define ERROUT(x) do { error = (x); goto done; } while (0)
216 #define OUICMP(P,A,B,C) ((P)[0]==(A) && (P)[1]==(B) && (P)[2]==(C))
219 ng_rfc1490_rcvdata(hook_p hook, struct mbuf *m, meta_p meta)
221 const node_p node = hook->node;
222 const priv_p priv = node->private;
225 if (hook == priv->downlink) {
229 if (!m || (m->m_len < MAX_ENCAPS_HDR
230 && !(m = m_pullup(m, MAX_ENCAPS_HDR))))
232 ptr = start = mtod(m, const u_char *);
234 /* Must be UI frame */
235 if (*ptr++ != HDLC_UI)
238 /* Eat optional zero pad byte */
242 /* Multiplex on NLPID */
245 if (OUICMP(ptr, 0, 0, 0)) { /* It's an ethertype */
249 etype = ntohs(*((const u_int16_t *)ptr));
251 m_adj(m, ptr - start);
255 priv->inet, m, meta);
258 case ETHERTYPE_REVARP:
262 } else if (OUICMP(ptr, 0x00, 0x80, 0xc2)) /* 802.1 bridging */
264 else /* Other weird stuff... */
268 m_adj(m, ptr - start);
269 NG_SEND_DATA(error, priv->inet, m, meta);
272 m_adj(m, ptr - start);
273 NG_SEND_DATA(error, priv->ppp, m, meta);
280 default: /* Try PPP (see RFC 1973) */
281 ptr--; /* NLPID becomes PPP proto */
282 if ((*ptr & 0x01) == 0x01)
284 m_adj(m, ptr - start);
285 NG_SEND_DATA(error, priv->ppp, m, meta);
288 } else if (hook == priv->ppp) {
289 M_PREPEND(m, 2, M_DONTWAIT); /* Prepend PPP NLPID */
292 mtod(m, u_char *)[0] = HDLC_UI;
293 mtod(m, u_char *)[1] = NLPID_PPP;
294 NG_SEND_DATA(error, priv->downlink, m, meta);
295 } else if (hook == priv->inet) {
296 M_PREPEND(m, 2, M_DONTWAIT); /* Prepend IP NLPID */
299 mtod(m, u_char *)[0] = HDLC_UI;
300 mtod(m, u_char *)[1] = NLPID_IP;
301 NG_SEND_DATA(error, priv->downlink, m, meta);
306 NG_FREE_DATA(m, meta);
314 ng_rfc1490_rmnode(node_p node)
316 const priv_p priv = node->private;
318 /* Take down netgraph node */
319 node->flags |= NG_INVALID;
322 bzero(priv, sizeof(*priv));
323 node->private = NULL;
324 FREE(priv, M_NETGRAPH);
325 ng_unref(node); /* let the node escape */
333 ng_rfc1490_disconnect(hook_p hook)
335 const priv_p priv = hook->node->private;
337 if (hook->node->numhooks == 0)
338 ng_rmnode(hook->node);
339 else if (hook == priv->downlink)
340 priv->downlink = NULL;
341 else if (hook == priv->inet)
343 else if (hook == priv->ppp)