Merge branch 'vendor/NCURSES'
[dragonfly.git] / sys / netgraph / UI / ng_UI.c
1
2 /*
3  * ng_UI.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_UI.c,v 1.6.2.2 2000/10/24 18:36:44 julian Exp $
40  * $Whistle: ng_UI.c,v 1.14 1999/11/01 09:24:51 julian Exp $
41  */
42
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/errno.h>
46 #include <sys/kernel.h>
47 #include <sys/malloc.h>
48 #include <sys/mbuf.h>
49 #include <sys/errno.h>
50
51
52 #include <netgraph/ng_message.h>
53 #include <netgraph/netgraph.h>
54 #include "ng_UI.h"
55
56 /*
57  * DEFINITIONS
58  */
59
60 /* Everything, starting with sdlc on has defined UI as 0x03 */
61 #define HDLC_UI 0x03
62
63 /* Node private data */
64 struct ng_UI_private {
65         hook_p  downlink;
66         hook_p  uplink;
67 };
68 typedef struct ng_UI_private *priv_p;
69
70 /* Netgraph node methods */
71 static ng_constructor_t ng_UI_constructor;
72 static ng_rcvmsg_t      ng_UI_rcvmsg;
73 static ng_shutdown_t    ng_UI_rmnode;
74 static ng_newhook_t     ng_UI_newhook;
75 static ng_rcvdata_t     ng_UI_rcvdata;
76 static ng_disconnect_t  ng_UI_disconnect;
77
78 /* Node type descriptor */
79 static struct ng_type typestruct = {
80         NG_VERSION,
81         NG_UI_NODE_TYPE,
82         NULL,
83         ng_UI_constructor,
84         ng_UI_rcvmsg,
85         ng_UI_rmnode,
86         ng_UI_newhook,
87         NULL,
88         NULL,
89         ng_UI_rcvdata,
90         ng_UI_rcvdata,
91         ng_UI_disconnect,
92         NULL
93 };
94 NETGRAPH_INIT(UI, &typestruct);
95
96 /************************************************************************
97                         NETGRAPH NODE STUFF
98  ************************************************************************/
99
100 /*
101  * Create a newborn node. We start with an implicit reference.
102  */
103
104 static int
105 ng_UI_constructor(node_p *nodep)
106 {
107         priv_p  priv;
108         int     error;
109
110         /* Allocate private structure */
111         priv = kmalloc(sizeof(*priv), M_NETGRAPH, M_NOWAIT | M_ZERO);
112         if (priv == NULL)
113                 return (ENOMEM);
114
115         /* Call generic node constructor */
116         if ((error = ng_make_node_common(&typestruct, nodep))) {
117                 kfree(priv, M_NETGRAPH);
118                 return (error);
119         }
120         (*nodep)->private = priv;
121
122         /* Done */
123         return (0);
124 }
125
126 /*
127  * Give our ok for a hook to be added
128  */
129 static int
130 ng_UI_newhook(node_p node, hook_p hook, const char *name)
131 {
132         const priv_p priv = node->private;
133
134         if (!strcmp(name, NG_UI_HOOK_DOWNSTREAM)) {
135                 if (priv->downlink)
136                         return (EISCONN);
137                 priv->downlink = hook;
138         } else if (!strcmp(name, NG_UI_HOOK_UPSTREAM)) {
139                 if (priv->uplink)
140                         return (EISCONN);
141                 priv->uplink = hook;
142         } else
143                 return (EINVAL);
144         return (0);
145 }
146
147 /*
148  * Receive a control message
149  */
150 static int
151 ng_UI_rcvmsg(node_p node, struct ng_mesg *msg,
152              const char *raddr, struct ng_mesg **rp)
153 {
154         kfree(msg, M_NETGRAPH);
155         return (EINVAL);
156 }
157
158 #define MAX_ENCAPS_HDR  1
159 #define ERROUT(x)       do { error = (x); goto done; } while (0)
160
161 /*
162  * Receive a data frame
163  */
164 static int
165 ng_UI_rcvdata(hook_p hook, struct mbuf *m, meta_p meta)
166 {
167         const node_p node = hook->node;
168         const priv_p priv = node->private;
169         int error = 0;
170
171         if (hook == priv->downlink) {
172                 u_char *start, *ptr;
173
174                 if (!m || (m->m_len < MAX_ENCAPS_HDR
175                     && !(m = m_pullup(m, MAX_ENCAPS_HDR))))
176                         ERROUT(ENOBUFS);
177                 ptr = start = mtod(m, u_char *);
178
179                 /* Must be UI frame */
180                 if (*ptr++ != HDLC_UI)
181                         ERROUT(0);
182
183                 m_adj(m, ptr - start);
184                 NG_SEND_DATA(error, priv->uplink, m, meta);     /* m -> NULL */
185         } else if (hook == priv->uplink) {
186                 M_PREPEND(m, 1, MB_DONTWAIT);   /* Prepend IP NLPID */
187                 if (!m)
188                         ERROUT(ENOBUFS);
189                 mtod(m, u_char *)[0] = HDLC_UI;
190                 NG_SEND_DATA(error, priv->downlink, m, meta);   /* m -> NULL */
191         } else
192                 panic(__func__);
193
194 done:
195         NG_FREE_DATA(m, meta);  /* does nothing if m == NULL */
196         return (error);
197 }
198
199 /*
200  * Shutdown node
201  */
202 static int
203 ng_UI_rmnode(node_p node)
204 {
205         const priv_p priv = node->private;
206
207         /* Take down netgraph node */
208         node->flags |= NG_INVALID;
209         ng_cutlinks(node);
210         ng_unname(node);
211         bzero(priv, sizeof(*priv));
212         kfree(priv, M_NETGRAPH);
213         node->private = NULL;
214         ng_unref(node);
215         return (0);
216 }
217
218 /*
219  * Hook disconnection
220  */
221 static int
222 ng_UI_disconnect(hook_p hook)
223 {
224         const priv_p priv = hook->node->private;
225
226         if (hook->node->numhooks == 0)
227                 ng_rmnode(hook->node);
228         else if (hook == priv->downlink)
229                 priv->downlink = NULL;
230         else if (hook == priv->uplink)
231                 priv->uplink = NULL;
232         else
233                 panic(__func__);
234         return (0);
235 }
236