kernel: Replace all usage of MALLOC()/FREE() with kmalloc()/kfree().
[dragonfly.git] / sys / netgraph / rfc1490 / ng_rfc1490.c
1
2 /*
3  * ng_rfc1490.c
4  *
5  * Copyright (c) 1996-1999 Whistle Communications, Inc.
6  * All rights reserved.
7  * 
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.
18  * 
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
35  * OF SUCH DAMAGE.
36  *
37  * Author: Julian Elischer <julian@freebsd.org>
38  *
39  * $FreeBSD: src/sys/netgraph/ng_rfc1490.c,v 1.6.2.4 2002/07/02 22:17:18 archie Exp $
40  * $Whistle: ng_rfc1490.c,v 1.22 1999/11/01 09:24:52 julian Exp $
41  */
42
43 /*
44  * This node does RFC 1490 multiplexing.
45  *
46  * NOTE: RFC 1490 is updated by RFC 2427.
47  */
48
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>
54 #include <sys/mbuf.h>
55 #include <sys/errno.h>
56 #include <sys/socket.h>
57
58 #include <net/if.h>
59 #include <netinet/in.h>
60 #include <netinet/if_ether.h>
61
62 #include <netgraph/ng_message.h>
63 #include <netgraph/netgraph.h>
64 #include "ng_rfc1490.h"
65
66 /*
67  * DEFINITIONS
68  */
69
70 /* Q.922 stuff -- see RFC 1490 */
71 #define HDLC_UI         0x03
72
73 #define NLPID_IP        0xCC
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
80
81 /* Node private data */
82 struct ng_rfc1490_private {
83         hook_p  downlink;
84         hook_p  ppp;
85         hook_p  inet;
86 };
87 typedef struct ng_rfc1490_private *priv_p;
88
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_rmnode;
93 static ng_newhook_t     ng_rfc1490_newhook;
94 static ng_rcvdata_t     ng_rfc1490_rcvdata;
95 static ng_disconnect_t  ng_rfc1490_disconnect;
96
97 /* Node type descriptor */
98 static struct ng_type typestruct = {
99         NG_VERSION,
100         NG_RFC1490_NODE_TYPE,
101         NULL,
102         ng_rfc1490_constructor,
103         ng_rfc1490_rcvmsg,
104         ng_rfc1490_rmnode,
105         ng_rfc1490_newhook,
106         NULL,
107         NULL,
108         ng_rfc1490_rcvdata,
109         ng_rfc1490_rcvdata,
110         ng_rfc1490_disconnect,
111         NULL
112 };
113 NETGRAPH_INIT(rfc1490, &typestruct);
114
115 /************************************************************************
116                         NETGRAPH NODE STUFF
117  ************************************************************************/
118
119 /*
120  * Node constructor
121  */
122 static int
123 ng_rfc1490_constructor(node_p *nodep)
124 {
125         priv_p priv;
126         int error;
127
128         /* Allocate private structure */
129         priv = kmalloc(sizeof(*priv), M_NETGRAPH, M_NOWAIT | M_ZERO);
130         if (priv == NULL)
131                 return (ENOMEM);
132
133         /* Call generic node constructor */
134         if ((error = ng_make_node_common(&typestruct, nodep))) {
135                 kfree(priv, M_NETGRAPH);
136                 return (error);
137         }
138         (*nodep)->private = priv;
139
140         /* Done */
141         return (0);
142 }
143
144 /*
145  * Give our ok for a hook to be added
146  */
147 static int
148 ng_rfc1490_newhook(node_p node, hook_p hook, const char *name)
149 {
150         const priv_p priv = node->private;
151
152         if (!strcmp(name, NG_RFC1490_HOOK_DOWNSTREAM)) {
153                 if (priv->downlink)
154                         return (EISCONN);
155                 priv->downlink = hook;
156         } else if (!strcmp(name, NG_RFC1490_HOOK_PPP)) {
157                 if (priv->ppp)
158                         return (EISCONN);
159                 priv->ppp = hook;
160         } else if (!strcmp(name, NG_RFC1490_HOOK_INET)) {
161                 if (priv->inet)
162                         return (EISCONN);
163                 priv->inet = hook;
164         } else
165                 return (EINVAL);
166         return (0);
167 }
168
169 /*
170  * Receive a control message. We don't support any special ones.
171  */
172 static int
173 ng_rfc1490_rcvmsg(node_p node, struct ng_mesg *msg,
174                   const char *raddr, struct ng_mesg **rp)
175 {
176         kfree(msg, M_NETGRAPH);
177         return (EINVAL);
178 }
179
180 /*
181  * Receive data on a hook and encapsulate according to RFC 1490.
182  * Only those nodes marked (*) are supported by this routine so far.
183  *
184  *                            Q.922 control
185  *                                 |
186  *                                 |
187  *            --------------------------------------------
188  *            | 0x03                                     |
189  *           UI                                       I Frame
190  *            |                                          |
191  *      ---------------------------------         --------------
192  *      | 0x08  | 0x81  |0xCC   |0xCF   | 0x00    |..01....    |..10....
193  *      |       |       |       |       | 0x80    |            |
194  *     Q.933   CLNP    IP(*)   PPP(*)  SNAP     ISO 8208    ISO 8208
195  *      |                    (rfc1973)  |       Modulo 8    Modulo 128
196  *      |                               |
197  *      --------------------           OUI
198  *      |                  |            |
199  *     L2 ID              L3 ID      -------------------------
200  *      |               User         |00-80-C2               |00-00-00
201  *      |               specified    |                       |
202  *      |               0x70        PID                     Ethertype
203  *      |                            |                       |
204  *      -------------------        --------------...        ----------
205  *      |0x51 |0x4E |     |0x4C    |0x1   |0xB  |           |0x806   |
206  *      |     |     |     |        |      |     |           |        |
207  *     7776  Q.922 Others 802.2   802.3  802.6 Others       ARP(*)  Others
208  *
209  *
210  */
211
212 #define MAX_ENCAPS_HDR  8
213 #define ERROUT(x)       do { error = (x); goto done; } while (0)
214 #define OUICMP(P,A,B,C) ((P)[0]==(A) && (P)[1]==(B) && (P)[2]==(C))
215
216 static int
217 ng_rfc1490_rcvdata(hook_p hook, struct mbuf *m, meta_p meta)
218 {
219         const node_p node = hook->node;
220         const priv_p priv = node->private;
221         int error = 0;
222
223         if (hook == priv->downlink) {
224                 const u_char *start;
225                 const u_char *ptr;
226
227                 if (!m || (m->m_len < MAX_ENCAPS_HDR
228                     && !(m = m_pullup(m, MAX_ENCAPS_HDR))))
229                         ERROUT(ENOBUFS);
230                 ptr = start = mtod(m, const u_char *);
231
232                 /* Must be UI frame */
233                 if (*ptr++ != HDLC_UI)
234                         ERROUT(0);
235
236                 /* Eat optional zero pad byte */
237                 if (*ptr == 0x00)
238                         ptr++;
239
240                 /* Multiplex on NLPID */
241                 switch (*ptr++) {
242                 case NLPID_SNAP:
243                         if (OUICMP(ptr, 0, 0, 0)) {     /* It's an ethertype */
244                                 u_int16_t etype;
245
246                                 ptr += 3;
247                                 etype = ntohs(*((const u_int16_t *)ptr));
248                                 ptr += 2;
249                                 m_adj(m, ptr - start);
250                                 switch (etype) {
251                                 case ETHERTYPE_IP:
252                                         NG_SEND_DATA(error,
253                                             priv->inet, m, meta);
254                                         break;
255                                 case ETHERTYPE_ARP:
256                                 case ETHERTYPE_REVARP:
257                                 default:
258                                         ERROUT(0);
259                                 }
260                         } else if (OUICMP(ptr, 0x00, 0x80, 0xc2))       /* 802.1 bridging */
261                                 ERROUT(0);
262                         else    /* Other weird stuff... */
263                                 ERROUT(0);
264                         break;
265                 case NLPID_IP:
266                         m_adj(m, ptr - start);
267                         NG_SEND_DATA(error, priv->inet, m, meta);
268                         break;
269                 case NLPID_PPP:
270                         m_adj(m, ptr - start);
271                         NG_SEND_DATA(error, priv->ppp, m, meta);
272                         break;
273                 case NLPID_Q933:
274                 case NLPID_CLNP:
275                 case NLPID_ESIS:
276                 case NLPID_ISIS:
277                         ERROUT(0);
278                 default:        /* Try PPP (see RFC 1973) */
279                         ptr--;  /* NLPID becomes PPP proto */
280                         if ((*ptr & 0x01) == 0x01)
281                                 ERROUT(0);
282                         m_adj(m, ptr - start);
283                         NG_SEND_DATA(error, priv->ppp, m, meta);
284                         break;
285                 }
286         } else if (hook == priv->ppp) {
287                 M_PREPEND(m, 2, MB_DONTWAIT);   /* Prepend PPP NLPID */
288                 if (!m)
289                         ERROUT(ENOBUFS);
290                 mtod(m, u_char *)[0] = HDLC_UI;
291                 mtod(m, u_char *)[1] = NLPID_PPP;
292                 NG_SEND_DATA(error, priv->downlink, m, meta);
293         } else if (hook == priv->inet) {
294                 M_PREPEND(m, 2, MB_DONTWAIT);   /* Prepend IP NLPID */
295                 if (!m)
296                         ERROUT(ENOBUFS);
297                 mtod(m, u_char *)[0] = HDLC_UI;
298                 mtod(m, u_char *)[1] = NLPID_IP;
299                 NG_SEND_DATA(error, priv->downlink, m, meta);
300         } else
301                 panic(__func__);
302
303 done:
304         NG_FREE_DATA(m, meta);
305         return (error);
306 }
307
308 /*
309  * Nuke node
310  */
311 static int
312 ng_rfc1490_rmnode(node_p node)
313 {
314         const priv_p priv = node->private;
315
316         /* Take down netgraph node */
317         node->flags |= NG_INVALID;
318         ng_cutlinks(node);
319         ng_unname(node);
320         bzero(priv, sizeof(*priv));
321         node->private = NULL;
322         kfree(priv, M_NETGRAPH);
323         ng_unref(node);         /* let the node escape */
324         return (0);
325 }
326
327 /*
328  * Hook disconnection
329  */
330 static int
331 ng_rfc1490_disconnect(hook_p hook)
332 {
333         const priv_p priv = hook->node->private;
334
335         if (hook->node->numhooks == 0)
336                 ng_rmnode(hook->node);
337         else if (hook == priv->downlink)
338                 priv->downlink = NULL;
339         else if (hook == priv->inet)
340                 priv->inet = NULL;
341         else if (hook == priv->ppp)
342                 priv->ppp = NULL;
343         else
344                 panic(__func__);
345         return (0);
346 }
347