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>
40 * $Whistle: ng_rfc1490.c,v 1.22 1999/11/01 09:24:52 julian Exp $
44 * This node does RFC 1490 multiplexing.
46 * NOTE: RFC 1490 is updated by RFC 2427.
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>
55 #include <sys/errno.h>
56 #include <sys/socket.h>
59 #include <netinet/in.h>
60 #include <netinet/if_ether.h>
62 #include <netgraph/ng_message.h>
63 #include <netgraph/netgraph.h>
64 #include <netgraph/ng_rfc1490.h>
70 /* Q.922 stuff -- see RFC 1490 */
74 #define NLPID_PPP 0xCF
75 #define NLPID_SNAP 0x80
76 #define NLPID_Q933 0x08
77 #define NLPID_CLNP 0x81
78 #define NLPID_ESIS 0x82
79 #define NLPID_ISIS 0x83
81 /* Node private data */
82 struct ng_rfc1490_private {
87 typedef struct ng_rfc1490_private *priv_p;
89 /* Netgraph node methods */
90 static ng_constructor_t ng_rfc1490_constructor;
91 static ng_rcvmsg_t ng_rfc1490_rcvmsg;
92 static ng_shutdown_t ng_rfc1490_shutdown;
93 static ng_newhook_t ng_rfc1490_newhook;
94 static ng_rcvdata_t ng_rfc1490_rcvdata;
95 static ng_disconnect_t ng_rfc1490_disconnect;
97 /* Node type descriptor */
98 static struct ng_type typestruct = {
100 NG_RFC1490_NODE_TYPE,
102 ng_rfc1490_constructor,
109 ng_rfc1490_disconnect,
112 NETGRAPH_INIT(rfc1490, &typestruct);
114 /************************************************************************
116 ************************************************************************/
122 ng_rfc1490_constructor(node_p node)
126 /* Allocate private structure */
127 MALLOC(priv, priv_p, sizeof(*priv), M_NETGRAPH, M_NOWAIT | M_ZERO);
131 NG_NODE_SET_PRIVATE(node, priv);
138 * Give our ok for a hook to be added
141 ng_rfc1490_newhook(node_p node, hook_p hook, const char *name)
143 const priv_p priv = NG_NODE_PRIVATE(node);
145 if (!strcmp(name, NG_RFC1490_HOOK_DOWNSTREAM)) {
148 priv->downlink = hook;
149 } else if (!strcmp(name, NG_RFC1490_HOOK_PPP)) {
153 } else if (!strcmp(name, NG_RFC1490_HOOK_INET)) {
163 * Receive a control message. We don't support any special ones.
166 ng_rfc1490_rcvmsg(node_p node, item_p item, hook_p lasthook)
173 * Receive data on a hook and encapsulate according to RFC 1490.
174 * Only those nodes marked (*) are supported by this routine so far.
179 * --------------------------------------------
183 * --------------------------------- --------------
184 * | 0x08 | 0x81 |0xCC |0xCF | 0x00 |..01.... |..10....
186 * Q.933 CLNP IP(*) PPP(*) SNAP ISO 8208 ISO 8208
187 * | (rfc1973) | Modulo 8 Modulo 128
189 * -------------------- OUI
191 * L2 ID L3 ID -------------------------
192 * | User |00-80-C2 |00-00-00
194 * | 0x70 PID Ethertype
196 * ------------------- --------------... ----------
197 * |0x51 |0x4E | |0x4C |0x1 |0xB | |0x806 |
199 * 7776 Q.922 Others 802.2 802.3 802.6 Others ARP(*) Others
204 #define MAX_ENCAPS_HDR 8
205 #define ERROUT(x) do { error = (x); goto done; } while (0)
206 #define OUICMP(P,A,B,C) ((P)[0]==(A) && (P)[1]==(B) && (P)[2]==(C))
209 ng_rfc1490_rcvdata(hook_p hook, item_p item)
211 const node_p node = NG_HOOK_NODE(hook);
212 const priv_p priv = NG_NODE_PRIVATE(node);
217 if (hook == priv->downlink) {
220 if (!m || (m->m_len < MAX_ENCAPS_HDR
221 && !(m = m_pullup(m, MAX_ENCAPS_HDR))))
223 ptr = start = mtod(m, u_char *);
225 /* Must be UI frame */
226 if (*ptr++ != HDLC_UI)
229 /* Eat optional zero pad byte */
233 /* Multiplex on NLPID */
236 if (OUICMP(ptr, 0, 0, 0)) { /* It's an ethertype */
240 etype = ntohs(*((u_int16_t *) ptr));
242 m_adj(m, ptr - start);
245 NG_FWD_NEW_DATA(error, item,
249 case ETHERTYPE_REVARP:
253 } else if (OUICMP(ptr, 0x00, 0x80, 0xc2)) /* 802.1 bridging */
255 else /* Other weird stuff... */
259 m_adj(m, ptr - start);
260 NG_FWD_NEW_DATA(error, item, priv->inet, m);
263 m_adj(m, ptr - start);
264 NG_FWD_NEW_DATA(error, item, priv->ppp, m);
271 default: /* Try PPP (see RFC 1973) */
272 ptr--; /* NLPID becomes PPP proto */
273 if ((*ptr & 0x01) == 0x01)
275 m_adj(m, ptr - start);
276 NG_FWD_NEW_DATA(error, item, priv->ppp, m);
279 } else if (hook == priv->ppp) {
280 M_PREPEND(m, 2, M_DONTWAIT); /* Prepend PPP NLPID */
283 mtod(m, u_char *)[0] = HDLC_UI;
284 mtod(m, u_char *)[1] = NLPID_PPP;
285 NG_FWD_NEW_DATA(error, item, priv->downlink, m);
286 } else if (hook == priv->inet) {
287 M_PREPEND(m, 2, M_DONTWAIT); /* Prepend IP NLPID */
290 mtod(m, u_char *)[0] = HDLC_UI;
291 mtod(m, u_char *)[1] = NLPID_IP;
292 NG_FWD_NEW_DATA(error, item, priv->downlink, m);
307 ng_rfc1490_shutdown(node_p node)
309 const priv_p priv = NG_NODE_PRIVATE(node);
311 /* Take down netgraph node */
312 bzero(priv, sizeof(*priv));
313 FREE(priv, M_NETGRAPH);
314 NG_NODE_SET_PRIVATE(node, NULL);
315 NG_NODE_UNREF(node); /* let the node escape */
323 ng_rfc1490_disconnect(hook_p hook)
325 const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
327 if ((NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0)
328 && (NG_NODE_IS_VALID(NG_HOOK_NODE(hook))))
329 ng_rmnode_self(NG_HOOK_NODE(hook));
330 else if (hook == priv->downlink)
331 priv->downlink = NULL;
332 else if (hook == priv->inet)
334 else if (hook == priv->ppp)