6 * Copyright (c) 1996-1999 Whistle Communications, Inc.
9 * Subject to the following obligations and disclaimer of warranty, use and
10 * redistribution of this software, in source or object code forms, with or
11 * without modifications are expressly permitted by Whistle Communications;
12 * provided, however, that:
13 * 1. Any and all reproductions of the source or object code must include the
14 * copyright notice above and the following disclaimer of warranties; and
15 * 2. No rights are granted, in any manner or form, to use Whistle
16 * Communications, Inc. trademarks, including the mark "WHISTLE
17 * COMMUNICATIONS" on advertising, endorsements, or otherwise except as
18 * such appears in the above copyright notice or in the software.
20 * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
21 * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
22 * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
23 * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
24 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
25 * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
26 * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
27 * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
28 * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES
29 * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
30 * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
31 * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
32 * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
33 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
35 * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
38 * Author: Julian Elischer <julian@freebsd.org>
39 * $Whistle: ng_rfc1490.c,v 1.22 1999/11/01 09:24:52 julian Exp $
43 * This node does RFC 1490 multiplexing.
45 * NOTE: RFC 1490 is updated by RFC 2427.
48 #include <sys/param.h>
49 #include <sys/systm.h>
50 #include <sys/errno.h>
51 #include <sys/kernel.h>
52 #include <sys/malloc.h>
54 #include <sys/errno.h>
55 #include <sys/socket.h>
58 #include <netinet/in.h>
59 #include <netinet/if_ether.h>
61 #include <netgraph/ng_message.h>
62 #include <netgraph/netgraph.h>
63 #include <netgraph/ng_parse.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 #define ERROUT(x) do { error = (x); goto done; } while (0)
83 /* Encapsulation methods we understand */
85 NG_RFC1490_ENCAP_IETF_IP = 1, /* see RFC2427, chapter 7, table 1 */
86 NG_RFC1490_ENCAP_IETF_SNAP, /* see RFC2427, chapter 7, table 2 */
87 NG_RFC1490_ENCAP_CISCO, /* Cisco's proprietary encapsulation */
90 struct ng_rfc1490_encap_t {
95 static const struct ng_rfc1490_encap_t ng_rfc1490_encaps[] = {
96 { NG_RFC1490_ENCAP_IETF_IP, "ietf-ip" },
97 { NG_RFC1490_ENCAP_IETF_SNAP, "ietf-snap" },
98 { NG_RFC1490_ENCAP_CISCO, "cisco" },
102 /* Node private data */
103 struct ng_rfc1490_private {
108 const struct ng_rfc1490_encap_t *enc;
110 typedef struct ng_rfc1490_private *priv_p;
112 /* Netgraph node methods */
113 static ng_constructor_t ng_rfc1490_constructor;
114 static ng_rcvmsg_t ng_rfc1490_rcvmsg;
115 static ng_shutdown_t ng_rfc1490_shutdown;
116 static ng_newhook_t ng_rfc1490_newhook;
117 static ng_rcvdata_t ng_rfc1490_rcvdata;
118 static ng_disconnect_t ng_rfc1490_disconnect;
120 /* List of commands and how to convert arguments to/from ASCII */
121 static const struct ng_cmdlist ng_rfc1490_cmds[] = {
124 NGM_RFC1490_SET_ENCAP,
126 &ng_parse_string_type,
131 NGM_RFC1490_GET_ENCAP,
134 &ng_parse_string_type
139 /* Node type descriptor */
140 static struct ng_type typestruct = {
141 .version = NG_ABI_VERSION,
142 .name = NG_RFC1490_NODE_TYPE,
143 .constructor = ng_rfc1490_constructor,
144 .rcvmsg = ng_rfc1490_rcvmsg,
145 .shutdown = ng_rfc1490_shutdown,
146 .newhook = ng_rfc1490_newhook,
147 .rcvdata = ng_rfc1490_rcvdata,
148 .disconnect = ng_rfc1490_disconnect,
149 .cmdlist = ng_rfc1490_cmds,
151 NETGRAPH_INIT(rfc1490, &typestruct);
153 /************************************************************************
155 ************************************************************************/
161 ng_rfc1490_constructor(node_p node)
165 /* Allocate private structure */
166 priv = malloc(sizeof(*priv), M_NETGRAPH, M_WAITOK | M_ZERO);
168 /* Initialize to default encapsulation method - ietf-ip */
169 priv->enc = ng_rfc1490_encaps;
171 NG_NODE_SET_PRIVATE(node, priv);
178 * Give our ok for a hook to be added
181 ng_rfc1490_newhook(node_p node, hook_p hook, const char *name)
183 const priv_p priv = NG_NODE_PRIVATE(node);
185 if (!strcmp(name, NG_RFC1490_HOOK_DOWNSTREAM)) {
188 priv->downlink = hook;
189 } else if (!strcmp(name, NG_RFC1490_HOOK_PPP)) {
193 } else if (!strcmp(name, NG_RFC1490_HOOK_INET)) {
197 } else if (!strcmp(name, NG_RFC1490_HOOK_ETHERNET)) {
200 priv->ethernet = hook;
207 * Receive a control message.
210 ng_rfc1490_rcvmsg(node_p node, item_p item, hook_p lasthook)
212 const priv_p priv = NG_NODE_PRIVATE(node);
214 struct ng_mesg *resp = NULL;
217 NGI_GET_MSG(item, msg);
219 if (msg->header.typecookie == NGM_RFC1490_COOKIE) {
220 switch (msg->header.cmd) {
221 case NGM_RFC1490_SET_ENCAP:
223 const struct ng_rfc1490_encap_t *enc;
227 if (msg->header.arglen == 0)
230 s = (char *)msg->data;
231 len = msg->header.arglen - 1;
233 /* Search for matching encapsulation method */
234 for (enc = ng_rfc1490_encaps; enc->method != 0; enc++ )
235 if ((strlen(enc->name) == len) &&
236 !strncmp(enc->name, s, len))
239 if (enc->method != 0)
245 case NGM_RFC1490_GET_ENCAP:
247 NG_MKRESPONSE(resp, msg, strlen(priv->enc->name) + 1, M_NOWAIT);
251 strlcpy((char *)resp->data, priv->enc->name,
252 strlen(priv->enc->name) + 1);
263 NG_RESPOND_MSG(error, node, item, resp);
269 * Receive data on a hook and encapsulate according to RFC 1490.
270 * Only those nodes marked (*) are supported by this routine so far.
275 * ---------------------------------------------------------------------
279 * --------------------------------- -------------- |
280 * | 0x08 | 0x81 |0xCC |0xCF | 0x00 |..01.... |..10.... --------------
281 * | | | | | 0x80 | | |0x800 |
282 * Q.933 CLNP IP(*) PPP(*) SNAP ISO 8208 ISO 8208 | |
283 * | (rfc1973) | Modulo 8 Modulo 128 IP(*) Others
285 * -------------------- OUI
287 * L2 ID L3 ID -------------------------
288 * | User |00-80-C2 |00-00-00
290 * | 0x70 PID Ethertype
292 * ------------------- -----------------... ----------
293 * |0x51 |0x4E | |0x4C |0x7 |0xB | |0x806 |
295 * 7776 Q.922 Others 802.2 802.3(*) 802.6 Others IP(*) Others
300 #define MAX_ENCAPS_HDR 8
301 #define OUICMP(P,A,B,C) ((P)[0]==(A) && (P)[1]==(B) && (P)[2]==(C))
304 ng_rfc1490_rcvdata(hook_p hook, item_p item)
306 const node_p node = NG_HOOK_NODE(hook);
307 const priv_p priv = NG_NODE_PRIVATE(node);
312 if (hook == priv->downlink) {
316 if (m->m_len < MAX_ENCAPS_HDR
317 && !(m = m_pullup(m, MAX_ENCAPS_HDR)))
319 ptr = start = mtod(m, const u_char *);
321 if (priv->enc->method == NG_RFC1490_ENCAP_CISCO)
322 goto switch_on_etype;
324 /* Must be UI frame */
325 if (*ptr++ != HDLC_UI)
328 /* Eat optional zero pad byte */
332 /* Multiplex on NLPID */
335 if (OUICMP(ptr, 0, 0, 0)) { /* It's an ethertype */
339 switch_on_etype: etype = ntohs(*((const u_int16_t *)ptr));
341 m_adj(m, ptr - start);
344 NG_FWD_NEW_DATA(error, item,
348 case ETHERTYPE_REVARP:
352 } else if (OUICMP(ptr, 0x00, 0x80, 0xc2)) {
356 ERROUT(0); /* unknown PID octet 0 */
358 ERROUT(0); /* not FCS-less 802.3 */
359 m_adj(m, ptr - start);
360 NG_FWD_NEW_DATA(error, item, priv->ethernet, m);
361 } else /* Other weird stuff... */
365 m_adj(m, ptr - start);
366 NG_FWD_NEW_DATA(error, item, priv->inet, m);
369 m_adj(m, ptr - start);
370 NG_FWD_NEW_DATA(error, item, priv->ppp, m);
377 default: /* Try PPP (see RFC 1973) */
378 ptr--; /* NLPID becomes PPP proto */
379 if ((*ptr & 0x01) == 0x01)
381 m_adj(m, ptr - start);
382 NG_FWD_NEW_DATA(error, item, priv->ppp, m);
385 } else if (hook == priv->ppp) {
386 M_PREPEND(m, 2, M_NOWAIT); /* Prepend PPP NLPID */
389 mtod(m, u_char *)[0] = HDLC_UI;
390 mtod(m, u_char *)[1] = NLPID_PPP;
391 NG_FWD_NEW_DATA(error, item, priv->downlink, m);
392 } else if (hook == priv->inet) {
393 switch (priv->enc->method) {
394 case NG_RFC1490_ENCAP_IETF_IP:
395 M_PREPEND(m, 2, M_NOWAIT); /* Prepend IP NLPID */
398 mtod(m, u_char *)[0] = HDLC_UI;
399 mtod(m, u_char *)[1] = NLPID_IP;
401 case NG_RFC1490_ENCAP_IETF_SNAP:
403 * According to RFC2427 frame should begin with
404 * HDLC_UI PAD NLIPID OUI PID
405 * 03 00 80 00 00 00 08 00
407 M_PREPEND(m, 8, M_NOWAIT);
410 mtod(m, u_char *)[0] = HDLC_UI;
411 mtod(m, u_char *)[1] = 0x00; /* PAD */
412 mtod(m, u_char *)[2] = NLPID_SNAP;
413 bzero((char *)(mtod(m, u_char *) + 3), 3); /* OUI 0-0-0 */
414 *((u_int16_t *)mtod(m, u_int16_t *) + 6/sizeof(u_int16_t))
415 = htons(ETHERTYPE_IP); /* PID */
417 case NG_RFC1490_ENCAP_CISCO:
418 M_PREPEND(m, 2, M_NOWAIT); /* Prepend IP ethertype */
421 *((u_int16_t *)mtod(m, u_int16_t *)) = htons(ETHERTYPE_IP);
424 NG_FWD_NEW_DATA(error, item, priv->downlink, m);
425 } else if (hook == priv->ethernet) {
426 M_PREPEND(m, 8, M_NOWAIT); /* Prepend NLPID, OUI, PID */
429 mtod(m, u_char *)[0] = HDLC_UI;
430 mtod(m, u_char *)[1] = 0x00; /* pad */
431 mtod(m, u_char *)[2] = NLPID_SNAP;
432 mtod(m, u_char *)[3] = 0x00; /* OUI */
433 mtod(m, u_char *)[4] = 0x80;
434 mtod(m, u_char *)[5] = 0xc2;
435 mtod(m, u_char *)[6] = 0x00; /* PID */
436 mtod(m, u_char *)[7] = 0x07;
437 NG_FWD_NEW_DATA(error, item, priv->downlink, m);
439 panic("%s", __func__);
452 ng_rfc1490_shutdown(node_p node)
454 const priv_p priv = NG_NODE_PRIVATE(node);
456 /* Take down netgraph node */
457 bzero(priv, sizeof(*priv));
458 free(priv, M_NETGRAPH);
459 NG_NODE_SET_PRIVATE(node, NULL);
460 NG_NODE_UNREF(node); /* let the node escape */
468 ng_rfc1490_disconnect(hook_p hook)
470 const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
472 if ((NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0)
473 && (NG_NODE_IS_VALID(NG_HOOK_NODE(hook))))
474 ng_rmnode_self(NG_HOOK_NODE(hook));
475 else if (hook == priv->downlink)
476 priv->downlink = NULL;
477 else if (hook == priv->inet)
479 else if (hook == priv->ppp)
481 else if (hook == priv->ethernet)
482 priv->ethernet = NULL;
484 panic("%s", __func__);