e954806ba78d6663bb5c38b9bee965ac77f123fa
[dragonfly.git] / sys / netgraph7 / ng_rfc1490.c
1 /*
2  * ng_rfc1490.c
3  */
4
5 /*-
6  * Copyright (c) 1996-1999 Whistle Communications, Inc.
7  * All rights reserved.
8  * 
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.
19  * 
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
36  * OF SUCH DAMAGE.
37  *
38  * Author: Julian Elischer <julian@freebsd.org>
39  *
40  * $FreeBSD: src/sys/netgraph/ng_rfc1490.c,v 1.24 2005/01/07 01:45:39 imp Exp $
41  * $DragonFly: src/sys/netgraph7/ng_rfc1490.c,v 1.2 2008/06/26 23:05:35 dillon Exp $
42  * $Whistle: ng_rfc1490.c,v 1.22 1999/11/01 09:24:52 julian Exp $
43  */
44
45 /*
46  * This node does RFC 1490 multiplexing.
47  *
48  * NOTE: RFC 1490 is updated by RFC 2427.
49  */
50
51 #include <sys/param.h>
52 #include <sys/systm.h>
53 #include <sys/errno.h>
54 #include <sys/kernel.h>
55 #include <sys/malloc.h>
56 #include <sys/mbuf.h>
57 #include <sys/errno.h>
58 #include <sys/socket.h>
59
60 #include <net/if.h>
61 #include <netinet/in.h>
62 #include <netinet/if_ether.h>
63
64 #include "ng_message.h"
65 #include "netgraph.h"
66 #include "ng_parse.h"
67 #include "ng_rfc1490.h"
68
69 /*
70  * DEFINITIONS
71  */
72
73 /* Q.922 stuff -- see RFC 1490 */
74 #define HDLC_UI         0x03
75
76 #define NLPID_IP        0xCC
77 #define NLPID_PPP       0xCF
78 #define NLPID_SNAP      0x80
79 #define NLPID_Q933      0x08
80 #define NLPID_CLNP      0x81
81 #define NLPID_ESIS      0x82
82 #define NLPID_ISIS      0x83
83
84 #define ERROUT(x)       do { error = (x); goto done; } while (0)
85
86 /* Encapsulation methods we understand */
87 enum {
88         NG_RFC1490_ENCAP_IETF_IP = 1,   /* see RFC2427, chapter 7, table 1 */
89         NG_RFC1490_ENCAP_IETF_SNAP,     /* see RFC2427, chapter 7, table 2 */
90         NG_RFC1490_ENCAP_CISCO,         /* Cisco's proprietary encapsulation */
91 };
92
93 struct ng_rfc1490_encap_t {
94         u_int8_t        method;
95         const char      *name;
96 };
97
98 static const struct ng_rfc1490_encap_t ng_rfc1490_encaps[] = {
99         { NG_RFC1490_ENCAP_IETF_IP,     "ietf-ip" },
100         { NG_RFC1490_ENCAP_IETF_SNAP,   "ietf-snap" },
101         { NG_RFC1490_ENCAP_CISCO,       "cisco" },
102         { 0, NULL},
103 };
104
105 /* Node private data */
106 struct ng_rfc1490_private {
107         hook_p  downlink;
108         hook_p  ppp;
109         hook_p  inet;
110         hook_p  ethernet;
111         const struct ng_rfc1490_encap_t *enc;
112 };
113 typedef struct ng_rfc1490_private *priv_p;
114
115 /* Netgraph node methods */
116 static ng_constructor_t ng_rfc1490_constructor;
117 static ng_rcvmsg_t      ng_rfc1490_rcvmsg;
118 static ng_shutdown_t    ng_rfc1490_shutdown;
119 static ng_newhook_t     ng_rfc1490_newhook;
120 static ng_rcvdata_t     ng_rfc1490_rcvdata;
121 static ng_disconnect_t  ng_rfc1490_disconnect;
122
123 /* List of commands and how to convert arguments to/from ASCII */
124 static const struct ng_cmdlist ng_rfc1490_cmds[] = {
125         {
126           NGM_RFC1490_COOKIE,
127           NGM_RFC1490_SET_ENCAP,
128           "setencap",
129           &ng_parse_string_type,
130           NULL
131         },
132         {
133           NGM_RFC1490_COOKIE,
134           NGM_RFC1490_GET_ENCAP,
135           "getencap",
136           NULL,
137           &ng_parse_string_type
138         },
139         { 0 }
140 };
141
142 /* Node type descriptor */
143 static struct ng_type typestruct = {
144         .version =      NG_ABI_VERSION,
145         .name =         NG_RFC1490_NODE_TYPE,
146         .constructor =  ng_rfc1490_constructor,
147         .rcvmsg =       ng_rfc1490_rcvmsg,
148         .shutdown =     ng_rfc1490_shutdown,
149         .newhook =      ng_rfc1490_newhook,
150         .rcvdata =      ng_rfc1490_rcvdata,
151         .disconnect =   ng_rfc1490_disconnect,
152         .cmdlist =      ng_rfc1490_cmds,
153 };
154 NETGRAPH_INIT(rfc1490, &typestruct);
155
156 /************************************************************************
157                         NETGRAPH NODE STUFF
158  ************************************************************************/
159
160 /*
161  * Node constructor
162  */
163 static int
164 ng_rfc1490_constructor(node_p node)
165 {
166         priv_p priv;
167
168         /* Allocate private structure */
169         MALLOC(priv, priv_p, sizeof(*priv), M_NETGRAPH, M_WAITOK | M_NULLOK | M_ZERO);
170         if (priv == NULL)
171                 return (ENOMEM);
172
173         /* Initialize to default encapsulation method - ietf-ip */
174         priv->enc = ng_rfc1490_encaps;
175
176         NG_NODE_SET_PRIVATE(node, priv);
177
178         /* Done */
179         return (0);
180 }
181
182 /*
183  * Give our ok for a hook to be added
184  */
185 static int
186 ng_rfc1490_newhook(node_p node, hook_p hook, const char *name)
187 {
188         const priv_p priv = NG_NODE_PRIVATE(node);
189
190         if (!strcmp(name, NG_RFC1490_HOOK_DOWNSTREAM)) {
191                 if (priv->downlink)
192                         return (EISCONN);
193                 priv->downlink = hook;
194         } else if (!strcmp(name, NG_RFC1490_HOOK_PPP)) {
195                 if (priv->ppp)
196                         return (EISCONN);
197                 priv->ppp = hook;
198         } else if (!strcmp(name, NG_RFC1490_HOOK_INET)) {
199                 if (priv->inet)
200                         return (EISCONN);
201                 priv->inet = hook;
202         } else if (!strcmp(name, NG_RFC1490_HOOK_ETHERNET)) {
203                 if (priv->ethernet)
204                         return (EISCONN);
205                 priv->ethernet = hook;
206         } else
207                 return (EINVAL);
208         return (0);
209 }
210
211 /*
212  * Receive a control message.
213  */
214 static int
215 ng_rfc1490_rcvmsg(node_p node, item_p item, hook_p lasthook)
216 {
217         const priv_p priv = NG_NODE_PRIVATE(node);
218         struct ng_mesg *msg;
219         struct ng_mesg *resp = NULL;
220         int error = 0;
221
222         NGI_GET_MSG(item, msg);
223
224         if (msg->header.typecookie == NGM_RFC1490_COOKIE) {
225                 switch (msg->header.cmd) {
226                 case NGM_RFC1490_SET_ENCAP:
227                 {
228                         const struct ng_rfc1490_encap_t *enc;
229                         char *s;
230                         size_t len;
231
232                         if (msg->header.arglen == 0)
233                                 ERROUT(EINVAL);
234
235                         s = (char *)msg->data;
236                         len = msg->header.arglen - 1;
237
238                         /* Search for matching encapsulation method */
239                         for (enc = ng_rfc1490_encaps; enc->method != 0; enc++ )
240                                 if ((strlen(enc->name) == len) &&
241                                     !strncmp(enc->name, s, len))
242                                         break;  /* found */
243
244                         if (enc->method != 0)
245                                 priv->enc = enc;
246                         else
247                                 error = EINVAL;
248                         break;
249                 }
250                 case NGM_RFC1490_GET_ENCAP:
251
252                         NG_MKRESPONSE(resp, msg, strlen(priv->enc->name) + 1, M_WAITOK | M_NULLOK);
253                         if (resp == NULL)
254                                 ERROUT(ENOMEM);
255
256                         strlcpy((char *)resp->data, priv->enc->name,
257                             strlen(priv->enc->name) + 1);
258                         break;
259
260                 default:
261                         error = EINVAL;
262                         break;
263                 }
264         } else
265                 error = EINVAL;
266
267 done:
268         NG_RESPOND_MSG(error, node, item, resp);
269         NG_FREE_MSG(msg);
270         return (error);
271 }
272
273 /*
274  * Receive data on a hook and encapsulate according to RFC 1490.
275  * Only those nodes marked (*) are supported by this routine so far.
276  *
277  *                            Q.922 control
278  *                                 |
279  *                                 |
280  *            ---------------------------------------------------------------------
281  *            | 0x03                                     |                        |
282  *           UI                                       I Frame                   Cisco
283  *            |                                          |                  Encapsulation
284  *      ---------------------------------         --------------                  |
285  *      | 0x08  | 0x81  |0xCC   |0xCF   | 0x00    |..01....    |..10....   --------------
286  *      |       |       |       |       | 0x80    |            |           |0x800       |
287  *     Q.933   CLNP    IP(*)   PPP(*)  SNAP     ISO 8208    ISO 8208       |            |
288  *      |                    (rfc1973)  |       Modulo 8    Modulo 128     IP(*)     Others
289  *      |                               |
290  *      --------------------           OUI
291  *      |                  |            |
292  *     L2 ID              L3 ID      -------------------------
293  *      |               User         |00-80-C2               |00-00-00
294  *      |               specified    |                       |
295  *      |               0x70        PID                     Ethertype
296  *      |                            |                       |
297  *      -------------------        -----------------...     ----------
298  *      |0x51 |0x4E |     |0x4C    |0x7      |0xB  |        |0x806   |
299  *      |     |     |     |        |         |     |        |        |
300  *     7776  Q.922 Others 802.2   802.3(*)  802.6 Others    IP(*)   Others
301  *
302  *
303  */
304
305 #define MAX_ENCAPS_HDR  8
306 #define OUICMP(P,A,B,C) ((P)[0]==(A) && (P)[1]==(B) && (P)[2]==(C))
307
308 static int
309 ng_rfc1490_rcvdata(hook_p hook, item_p item)
310 {
311         const node_p node = NG_HOOK_NODE(hook);
312         const priv_p priv = NG_NODE_PRIVATE(node);
313         int error = 0;
314         struct mbuf *m;
315
316         NGI_GET_M(item, m);
317         if (hook == priv->downlink) {
318                 const u_char *start;
319                 const u_char *ptr;
320
321                 if (m->m_len < MAX_ENCAPS_HDR
322                     && !(m = m_pullup(m, MAX_ENCAPS_HDR)))
323                         ERROUT(ENOBUFS);
324                 ptr = start = mtod(m, const u_char *);
325
326                 if (priv->enc->method == NG_RFC1490_ENCAP_CISCO)
327                         goto switch_on_etype;
328
329                 /* Must be UI frame */
330                 if (*ptr++ != HDLC_UI)
331                         ERROUT(0);
332
333                 /* Eat optional zero pad byte */
334                 if (*ptr == 0x00)
335                         ptr++;
336
337                 /* Multiplex on NLPID */
338                 switch (*ptr++) {
339                 case NLPID_SNAP:
340                         if (OUICMP(ptr, 0, 0, 0)) {     /* It's an ethertype */
341                                 u_int16_t etype;
342
343                                 ptr += 3;
344 switch_on_etype:                etype = ntohs(*((const u_int16_t *)ptr));
345                                 ptr += 2;
346                                 m_adj(m, ptr - start);
347                                 switch (etype) {
348                                 case ETHERTYPE_IP:
349                                         NG_FWD_NEW_DATA(error, item,
350                                             priv->inet, m);
351                                         break;
352                                 case ETHERTYPE_ARP:
353                                 case ETHERTYPE_REVARP:
354                                 default:
355                                         ERROUT(0);
356                                 }
357                         } else if (OUICMP(ptr, 0x00, 0x80, 0xc2)) {
358                                 /* 802.1 bridging */
359                                 ptr += 3;
360                                 if (*ptr++ != 0x00)
361                                         ERROUT(0);      /* unknown PID octet 0 */
362                                 if (*ptr++ != 0x07)
363                                         ERROUT(0);      /* not FCS-less 802.3 */
364                                 m_adj(m, ptr - start);
365                                 NG_FWD_NEW_DATA(error, item, priv->ethernet, m);
366                         } else  /* Other weird stuff... */
367                                 ERROUT(0);
368                         break;
369                 case NLPID_IP:
370                         m_adj(m, ptr - start);
371                         NG_FWD_NEW_DATA(error, item, priv->inet, m);
372                         break;
373                 case NLPID_PPP:
374                         m_adj(m, ptr - start);
375                         NG_FWD_NEW_DATA(error, item, priv->ppp, m);
376                         break;
377                 case NLPID_Q933:
378                 case NLPID_CLNP:
379                 case NLPID_ESIS:
380                 case NLPID_ISIS:
381                         ERROUT(0);
382                 default:        /* Try PPP (see RFC 1973) */
383                         ptr--;  /* NLPID becomes PPP proto */
384                         if ((*ptr & 0x01) == 0x01)
385                                 ERROUT(0);
386                         m_adj(m, ptr - start);
387                         NG_FWD_NEW_DATA(error, item, priv->ppp, m);
388                         break;
389                 }
390         } else if (hook == priv->ppp) {
391                 M_PREPEND(m, 2, MB_DONTWAIT);   /* Prepend PPP NLPID */
392                 if (!m)
393                         ERROUT(ENOBUFS);
394                 mtod(m, u_char *)[0] = HDLC_UI;
395                 mtod(m, u_char *)[1] = NLPID_PPP;
396                 NG_FWD_NEW_DATA(error, item, priv->downlink, m);
397         } else if (hook == priv->inet) {
398                 switch (priv->enc->method) {
399                 case NG_RFC1490_ENCAP_IETF_IP:
400                         M_PREPEND(m, 2, MB_DONTWAIT);   /* Prepend IP NLPID */
401                         if (!m)
402                                 ERROUT(ENOBUFS);
403                         mtod(m, u_char *)[0] = HDLC_UI;
404                         mtod(m, u_char *)[1] = NLPID_IP;
405                         break;
406                 case NG_RFC1490_ENCAP_IETF_SNAP:
407                         /*
408                          *  According to RFC2427 frame should begin with
409                          *  HDLC_UI  PAD  NLIPID  OUI      PID
410                          *  03      00   80      00 00 00  08 00
411                          */
412                         M_PREPEND(m, 8, MB_DONTWAIT);
413                         if (!m)
414                                 ERROUT(ENOBUFS);
415                         mtod(m, u_char *)[0] = HDLC_UI;
416                         mtod(m, u_char *)[1] = 0x00;                    /* PAD */
417                         mtod(m, u_char *)[2] = NLPID_SNAP;
418                         bzero((char *)(mtod(m, u_char *) + 3), 3);      /* OUI 0-0-0 */
419                         *((u_int16_t *)mtod(m, u_int16_t *) + 6/sizeof(u_int16_t))
420                             = htons(ETHERTYPE_IP);  /* PID */
421                         break;
422                 case NG_RFC1490_ENCAP_CISCO:
423                         M_PREPEND(m, 2, MB_DONTWAIT);   /* Prepend IP ethertype */
424                         if (!m)
425                                 ERROUT(ENOBUFS);
426                         *((u_int16_t *)mtod(m, u_int16_t *)) = htons(ETHERTYPE_IP);
427                         break;
428                 }
429                 NG_FWD_NEW_DATA(error, item, priv->downlink, m);
430         } else if (hook == priv->ethernet) {
431                 M_PREPEND(m, 8, MB_DONTWAIT);   /* Prepend NLPID, OUI, PID */
432                 if (!m)
433                         ERROUT(ENOBUFS);
434                 mtod(m, u_char *)[0] = HDLC_UI;
435                 mtod(m, u_char *)[1] = 0x00;            /* pad */
436                 mtod(m, u_char *)[2] = NLPID_SNAP;
437                 mtod(m, u_char *)[3] = 0x00;            /* OUI */
438                 mtod(m, u_char *)[4] = 0x80;
439                 mtod(m, u_char *)[5] = 0xc2;
440                 mtod(m, u_char *)[6] = 0x00;            /* PID */
441                 mtod(m, u_char *)[7] = 0x07;
442                 NG_FWD_NEW_DATA(error, item, priv->downlink, m);
443         } else
444                 panic(__func__);
445
446 done:
447         if (item)
448                 NG_FREE_ITEM(item);
449         NG_FREE_M(m);
450         return (error);
451 }
452
453 /*
454  * Nuke node
455  */
456 static int
457 ng_rfc1490_shutdown(node_p node)
458 {
459         const priv_p priv = NG_NODE_PRIVATE(node);
460
461         /* Take down netgraph node */
462         bzero(priv, sizeof(*priv));
463         FREE(priv, M_NETGRAPH);
464         NG_NODE_SET_PRIVATE(node, NULL);
465         NG_NODE_UNREF(node);            /* let the node escape */
466         return (0);
467 }
468
469 /*
470  * Hook disconnection
471  */
472 static int
473 ng_rfc1490_disconnect(hook_p hook)
474 {
475         const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
476
477         if ((NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0)
478         && (NG_NODE_IS_VALID(NG_HOOK_NODE(hook))))
479                 ng_rmnode_self(NG_HOOK_NODE(hook));
480         else if (hook == priv->downlink)
481                 priv->downlink = NULL;
482         else if (hook == priv->inet)
483                 priv->inet = NULL;
484         else if (hook == priv->ppp)
485                 priv->ppp = NULL;
486         else if (hook == priv->ethernet)
487                 priv->ethernet = NULL;
488         else
489                 panic(__func__);
490         return (0);
491 }
492