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