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