FreeBSD kernel netgraph code
ng_UI.c
Go to the documentation of this file.
1/*
2 * ng_UI.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$
41 * $Whistle: ng_UI.c,v 1.14 1999/11/01 09:24:51 julian Exp $
42 */
43
44#include <sys/param.h>
45#include <sys/systm.h>
46#include <sys/errno.h>
47#include <sys/kernel.h>
48#include <sys/malloc.h>
49#include <sys/mbuf.h>
50#include <sys/errno.h>
51
52#include <netgraph/ng_message.h>
53#include <netgraph/netgraph.h>
54#include <netgraph/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 */
67};
68typedef struct ng_UI_private *priv_p;
69
70/* Netgraph node methods */
77
78/* Node type descriptor */
79static struct ng_type typestruct = {
81 .name = NG_UI_NODE_TYPE,
82 .constructor = ng_UI_constructor,
83 .rcvmsg = ng_UI_rcvmsg,
84 .shutdown = ng_UI_shutdown,
85 .newhook = ng_UI_newhook,
86 .rcvdata = ng_UI_rcvdata,
87 .disconnect = ng_UI_disconnect,
88};
90
91/************************************************************************
92 NETGRAPH NODE STUFF
93 ************************************************************************/
94
95/*
96 * Create a newborn node. We start with an implicit reference.
97 */
98
99static int
101{
102 priv_p priv;
103
104 /* Allocate private structure */
105 priv = malloc(sizeof(*priv), M_NETGRAPH, M_WAITOK | M_ZERO);
107 return (0);
108}
109
110/*
111 * Give our ok for a hook to be added
112 */
113static int
114ng_UI_newhook(node_p node, hook_p hook, const char *name)
115{
116 const priv_p priv = NG_NODE_PRIVATE(node);
117
118 if (!strcmp(name, NG_UI_HOOK_DOWNSTREAM)) {
119 if (priv->downlink)
120 return (EISCONN);
121 priv->downlink = hook;
122 } else if (!strcmp(name, NG_UI_HOOK_UPSTREAM)) {
123 if (priv->uplink)
124 return (EISCONN);
125 priv->uplink = hook;
126 } else
127 return (EINVAL);
128 return (0);
129}
130
131/*
132 * Receive a control message
133 */
134static int
135ng_UI_rcvmsg(node_p node, item_p item, hook_p lasthook)
136{
137 int error;
138 const priv_p priv = NG_NODE_PRIVATE(node);
139 struct ng_mesg *msg;
140
141 msg = NGI_MSG(item); /* only peeking */
142 if ((msg->header.typecookie == NGM_FLOW_COOKIE) && lasthook) {
143 if (lasthook == priv->downlink) {
144 if (priv->uplink) {
145 NG_FWD_ITEM_HOOK(error, item, priv->uplink);
146 return (error);
147 }
148 } else {
149 if (priv->downlink) {
150 NG_FWD_ITEM_HOOK(error, item, priv->downlink);
151 return (error);
152 }
153 }
154 }
155
156 NG_FREE_ITEM(item);
157 return (EINVAL);
158}
159
160#define MAX_ENCAPS_HDR 1
161#define ERROUT(x) do { error = (x); goto done; } while (0)
162
163/*
164 * Receive a data frame
165 */
166static int
168{
169 const node_p node = NG_HOOK_NODE(hook);
170 const priv_p priv = NG_NODE_PRIVATE(node);
171 struct mbuf *m;
172 int error = 0;
173
174 NGI_GET_M(item, m);
175 if (hook == priv->downlink) {
176 u_char *start, *ptr;
177
178 if (m->m_len < MAX_ENCAPS_HDR
179 && !(m = m_pullup(m, MAX_ENCAPS_HDR)))
180 ERROUT(ENOBUFS);
181 ptr = start = mtod(m, u_char *);
182
183 /* Must be UI frame */
184 if (*ptr++ != HDLC_UI)
185 ERROUT(0);
186
187 m_adj(m, ptr - start);
188 NG_FWD_NEW_DATA(error, item, priv->uplink, m); /* m -> NULL */
189 } else if (hook == priv->uplink) {
190 M_PREPEND(m, 1, M_NOWAIT); /* Prepend IP NLPID */
191 if (!m)
192 ERROUT(ENOBUFS);
193 mtod(m, u_char *)[0] = HDLC_UI;
194 NG_FWD_NEW_DATA(error, item, priv->downlink, m); /* m -> NULL */
195 } else
196 panic("%s", __func__);
197
198done:
199 NG_FREE_M(m); /* does nothing if m == NULL */
200 if (item)
201 NG_FREE_ITEM(item);
202 return (error);
203}
204
205/*
206 * Shutdown node
207 */
208static int
210{
211 const priv_p priv = NG_NODE_PRIVATE(node);
212
213 /* Take down netgraph node */
214 free(priv, M_NETGRAPH);
215 NG_NODE_SET_PRIVATE(node, NULL);
216 NG_NODE_UNREF(node);
217 return (0);
218}
219
220/*
221 * Hook disconnection
222 */
223static int
225{
227
228 if (hook == priv->downlink)
229 priv->downlink = NULL;
230 else if (hook == priv->uplink)
231 priv->uplink = NULL;
232 else
233 panic("%s", __func__);
234 /*
235 * If we are not already shutting down,
236 * and we have no more hooks, then DO shut down.
237 */
238 if ((NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0)
239 && (NG_NODE_IS_VALID(NG_HOOK_NODE(hook)))) {
241 }
242 return (0);
243}
#define NGI_MSG(i)
Definition: netgraph.h:834
#define NG_HOOK_NODE(hook)
Definition: netgraph.h:339
#define NG_FREE_M(m)
Definition: netgraph.h:946
int ng_rcvmsg_t(node_p node, item_p item, hook_p lasthook)
Definition: netgraph.h:105
#define NG_FWD_ITEM_HOOK(error, item, hook)
Definition: netgraph.h:898
int ng_disconnect_t(hook_p hook)
Definition: netgraph.h:107
#define NG_NODE_SET_PRIVATE(node, val)
Definition: netgraph.h:608
#define NG_NODE_IS_VALID(node)
Definition: netgraph.h:610
#define NG_NODE_UNREF(node)
Definition: netgraph.h:607
int ng_rmnode_self(node_p here)
Definition: ng_base.c:1609
#define NG_FWD_NEW_DATA(error, item, hook, m)
Definition: netgraph.h:914
int ng_rcvdata_t(hook_p hook, item_p item)
Definition: netgraph.h:106
int ng_shutdown_t(node_p node)
Definition: netgraph.h:101
#define NG_FREE_ITEM(item)
Definition: netgraph.h:847
int ng_constructor_t(node_p node)
Definition: netgraph.h:99
#define NGI_GET_M(i, m)
Definition: netgraph.h:852
#define NG_ABI_VERSION
Definition: netgraph.h:77
#define NG_NODE_NUMHOOKS(node)
Definition: netgraph.h:615
#define NG_NODE_PRIVATE(node)
Definition: netgraph.h:609
int ng_newhook_t(node_p node, hook_p hook, const char *name)
Definition: netgraph.h:102
static ng_constructor_t ng_UI_constructor
Definition: ng_UI.c:71
static ng_disconnect_t ng_UI_disconnect
Definition: ng_UI.c:76
#define HDLC_UI
Definition: ng_UI.c:61
struct ng_UI_private * priv_p
Definition: ng_UI.c:68
NETGRAPH_INIT(UI, &typestruct)
#define ERROUT(x)
Definition: ng_UI.c:161
static ng_newhook_t ng_UI_newhook
Definition: ng_UI.c:74
static ng_rcvmsg_t ng_UI_rcvmsg
Definition: ng_UI.c:72
static struct ng_type typestruct
Definition: ng_UI.c:79
static ng_rcvdata_t ng_UI_rcvdata
Definition: ng_UI.c:75
#define MAX_ENCAPS_HDR
Definition: ng_UI.c:160
static ng_shutdown_t ng_UI_shutdown
Definition: ng_UI.c:73
#define NG_UI_NODE_TYPE
Definition: ng_UI.h:48
#define NG_UI_HOOK_DOWNSTREAM
Definition: ng_UI.h:52
#define NG_UI_HOOK_UPSTREAM
Definition: ng_UI.h:53
#define NGM_FLOW_COOKIE
Definition: ng_message.h:152
char *const name
Definition: ng_ppp.c:261
hook_p uplink
Definition: ng_UI.c:66
hook_p downlink
Definition: ng_UI.c:65
u_int32_t typecookie
Definition: ng_message.h:66
struct ng_mesg::ng_msghdr header
u_int32_t version
Definition: netgraph.h:1077
Definition: ng_sscfu.c:66