2 * Copyright (c) 2001-2003
3 * Fraunhofer Institute for Open Communication Systems (FhG Fokus).
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * Author: Hartmut Brandt <harti@freebsd.org>
29 * Netgraph module for ITU-T Q.2120 UNI SSCF.
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/kernel.h>
38 #include <sys/malloc.h>
40 #include <sys/errno.h>
41 #include <sys/syslog.h>
42 #include <sys/socket.h>
43 #include <sys/socketvar.h>
45 #include <machine/stdarg.h>
47 #include <netgraph/ng_message.h>
48 #include <netgraph/netgraph.h>
49 #include <netgraph/ng_parse.h>
50 #include <netnatm/saal/sscopdef.h>
51 #include <netnatm/saal/sscfudef.h>
52 #include <netgraph/atm/ng_sscop.h>
53 #include <netgraph/atm/ng_sscfu.h>
54 #include <netgraph/atm/sscfu/ng_sscfu_cust.h>
55 #include <netnatm/saal/sscfu.h>
57 MALLOC_DEFINE(M_NG_SSCFU, "netgraph_sscfu", "netgraph uni sscf node");
59 MODULE_DEPEND(ng_sscfu, ngatmbase, 1, 1, 1);
65 hook_p upper; /* SAAL interface */
66 hook_p lower; /* SSCOP interface */
67 struct sscfu *sscf; /* the instance */
77 static const struct ng_parse_struct_field ng_sscop_param_type_info[] =
80 static const struct ng_parse_type ng_sscop_param_type = {
81 &ng_parse_struct_type,
82 ng_sscop_param_type_info
85 static const struct ng_parse_struct_field ng_sscfu_getdefparam_type_info[] =
86 NG_SSCFU_GETDEFPARAM_INFO;
88 static const struct ng_parse_type ng_sscfu_getdefparam_type = {
89 &ng_parse_struct_type,
90 ng_sscfu_getdefparam_type_info
94 static const struct ng_cmdlist ng_sscfu_cmdlist[] = {
97 NGM_SSCFU_GETDEFPARAM,
100 &ng_sscfu_getdefparam_type
121 &ng_parse_hint32_type
127 &ng_parse_hint32_type,
135 &ng_parse_uint32_type
140 static ng_constructor_t ng_sscfu_constructor;
141 static ng_shutdown_t ng_sscfu_shutdown;
142 static ng_rcvmsg_t ng_sscfu_rcvmsg;
143 static ng_newhook_t ng_sscfu_newhook;
144 static ng_disconnect_t ng_sscfu_disconnect;
145 static ng_rcvdata_t ng_sscfu_rcvupper;
146 static ng_rcvdata_t ng_sscfu_rcvlower;
148 static int ng_sscfu_mod_event(module_t, int, void *);
150 static struct ng_type ng_sscfu_typestruct = {
151 .version = NG_ABI_VERSION,
152 .name = NG_SSCFU_NODE_TYPE,
153 .mod_event = ng_sscfu_mod_event,
154 .constructor = ng_sscfu_constructor,
155 .rcvmsg = ng_sscfu_rcvmsg,
156 .shutdown = ng_sscfu_shutdown,
157 .newhook = ng_sscfu_newhook,
158 .rcvdata = ng_sscfu_rcvupper,
159 .disconnect = ng_sscfu_disconnect,
160 .cmdlist = ng_sscfu_cmdlist,
162 NETGRAPH_INIT(sscfu, &ng_sscfu_typestruct);
164 static void sscfu_send_upper(struct sscfu *, void *, enum saal_sig,
166 static void sscfu_send_lower(struct sscfu *, void *, enum sscop_aasig,
167 struct mbuf *, u_int);
168 static void sscfu_window(struct sscfu *, void *, u_int);
169 static void sscfu_verbose(struct sscfu *, void *, const char *, ...)
172 static const struct sscfu_funcs sscfu_funcs = {
179 /************************************************************/
184 text_status(node_p node, struct priv *priv, char *arg, u_int len)
188 sbuf_new(&sbuf, arg, len, 0);
191 sbuf_printf(&sbuf, "upper hook: %s connected to %s:%s\n",
192 NG_HOOK_NAME(priv->upper),
193 NG_NODE_NAME(NG_HOOK_NODE(NG_HOOK_PEER(priv->upper))),
194 NG_HOOK_NAME(NG_HOOK_PEER(priv->upper)));
196 sbuf_printf(&sbuf, "upper hook: <not connected>\n");
199 sbuf_printf(&sbuf, "lower hook: %s connected to %s:%s\n",
200 NG_HOOK_NAME(priv->lower),
201 NG_NODE_NAME(NG_HOOK_NODE(NG_HOOK_PEER(priv->lower))),
202 NG_HOOK_NAME(NG_HOOK_PEER(priv->lower)));
204 sbuf_printf(&sbuf, "lower hook: <not connected>\n");
206 sbuf_printf(&sbuf, "sscf state: %s\n",
207 priv->enabled == 0 ? "<disabled>" :
208 sscfu_statename(sscfu_getstate(priv->sscf)));
211 return (sbuf_len(&sbuf));
215 ng_sscfu_rcvmsg(node_p node, item_p item, hook_p lasthook)
217 struct priv *priv = NG_NODE_PRIVATE(node);
218 struct ng_mesg *resp = NULL;
222 NGI_GET_MSG(item, msg);
224 switch (msg->header.typecookie) {
226 case NGM_GENERIC_COOKIE:
227 switch (msg->header.cmd) {
229 case NGM_TEXT_STATUS:
230 NG_MKRESPONSE(resp, msg, NG_TEXTRESPONSE, M_NOWAIT);
235 resp->header.arglen = text_status(node, priv,
236 (char *)resp->data, resp->header.arglen) + 1;
245 case NGM_SSCFU_COOKIE:
246 switch (msg->header.cmd) {
248 case NGM_SSCFU_GETDEFPARAM:
250 struct ng_sscfu_getdefparam *p;
252 if (msg->header.arglen != 0) {
256 NG_MKRESPONSE(resp, msg, sizeof(*p), M_NOWAIT);
261 p = (struct ng_sscfu_getdefparam *)resp->data;
262 p->mask = sscfu_getdefparam(&p->param);
266 case NGM_SSCFU_ENABLE:
267 if (msg->header.arglen != 0) {
278 case NGM_SSCFU_DISABLE:
279 if (msg->header.arglen != 0) {
283 if (!priv->enabled) {
288 sscfu_reset(priv->sscf);
291 case NGM_SSCFU_GETSTATE:
292 if (msg->header.arglen != 0) {
296 NG_MKRESPONSE(resp, msg, sizeof(uint32_t), M_NOWAIT);
301 *(uint32_t *)resp->data =
302 priv->enabled ? (sscfu_getstate(priv->sscf) + 1)
306 case NGM_SSCFU_GETDEBUG:
307 if (msg->header.arglen != 0) {
311 NG_MKRESPONSE(resp, msg, sizeof(uint32_t), M_NOWAIT);
316 *(uint32_t *)resp->data = sscfu_getdebug(priv->sscf);
319 case NGM_SSCFU_SETDEBUG:
320 if (msg->header.arglen != sizeof(uint32_t)) {
324 sscfu_setdebug(priv->sscf, *(uint32_t *)msg->data);
338 NG_RESPOND_MSG(error, node, item, resp);
344 /************************************************************/
349 ng_sscfu_newhook(node_p node, hook_p hook, const char *name)
351 struct priv *priv = NG_NODE_PRIVATE(node);
353 if (strcmp(name, "upper") == 0)
355 else if (strcmp(name, "lower") == 0) {
357 NG_HOOK_SET_RCVDATA(hook, ng_sscfu_rcvlower);
364 ng_sscfu_disconnect(hook_p hook)
366 node_p node = NG_HOOK_NODE(hook);
367 struct priv *priv = NG_NODE_PRIVATE(node);
369 if (hook == priv->upper)
371 else if (hook == priv->lower)
374 log(LOG_ERR, "bogus hook");
378 if (NG_NODE_NUMHOOKS(node) == 0) {
379 if (NG_NODE_IS_VALID(node))
380 ng_rmnode_self(node);
383 * Because there are no timeouts reset the protocol
384 * if the lower layer is disconnected.
386 if (priv->lower == NULL &&
388 sscfu_getstate(priv->sscf) != SSCFU_RELEASED)
389 sscfu_reset(priv->sscf);
394 /************************************************************/
399 ng_sscfu_rcvupper(hook_p hook, item_p item)
401 node_p node = NG_HOOK_NODE(hook);
402 struct priv *priv = NG_NODE_PRIVATE(node);
406 if (!priv->enabled || priv->lower == NULL) {
414 if (!(m->m_flags & M_PKTHDR)) {
415 printf("no pkthdr\n");
419 if (m->m_len < (int)sizeof(a) && (m = m_pullup(m, sizeof(a))) == NULL)
421 bcopy((caddr_t)mtod(m, struct sscfu_arg *), &a, sizeof(a));
424 return (sscfu_saalsig(priv->sscf, a.sig, m));
428 sscfu_send_upper(struct sscfu *sscf, void *p, enum saal_sig sig, struct mbuf *m)
430 node_p node = (node_p)p;
431 struct priv *priv = NG_NODE_PRIVATE(node);
435 if (priv->upper == NULL) {
441 MGETHDR(m, M_NOWAIT, MT_DATA);
444 m->m_len = sizeof(struct sscfu_arg);
445 m->m_pkthdr.len = m->m_len;
447 M_PREPEND(m, sizeof(struct sscfu_arg), M_NOWAIT);
451 a = mtod(m, struct sscfu_arg *);
454 NG_SEND_DATA_ONLY(error, priv->upper, m);
458 ng_sscfu_rcvlower(hook_p hook, item_p item)
460 node_p node = NG_HOOK_NODE(hook);
461 struct priv *priv = NG_NODE_PRIVATE(node);
465 if (!priv->enabled || priv->upper == NULL) {
473 if (!(m->m_flags & M_PKTHDR)) {
474 printf("no pkthdr\n");
480 * Strip of the SSCOP header.
482 if (m->m_len < (int)sizeof(a) && (m = m_pullup(m, sizeof(a))) == NULL)
484 bcopy((caddr_t)mtod(m, struct sscop_arg *), &a, sizeof(a));
487 sscfu_input(priv->sscf, a.sig, m, a.arg);
493 sscfu_send_lower(struct sscfu *sscf, void *p, enum sscop_aasig sig,
494 struct mbuf *m, u_int arg)
496 node_p node = (node_p)p;
497 struct priv *priv = NG_NODE_PRIVATE(node);
501 if (priv->lower == NULL) {
507 MGETHDR(m, M_NOWAIT, MT_DATA);
510 m->m_len = sizeof(struct sscop_arg);
511 m->m_pkthdr.len = m->m_len;
513 M_PREPEND(m, sizeof(struct sscop_arg), M_NOWAIT);
517 a = mtod(m, struct sscop_arg *);
521 NG_SEND_DATA_ONLY(error, priv->lower, m);
525 * Window is handled by ng_sscop so make this a NOP.
528 sscfu_window(struct sscfu *sscfu, void *arg, u_int w)
532 /************************************************************/
537 ng_sscfu_constructor(node_p node)
541 if ((priv = malloc(sizeof(*priv), M_NG_SSCFU, M_NOWAIT|M_ZERO)) == NULL)
544 if ((priv->sscf = sscfu_create(node, &sscfu_funcs)) == NULL) {
545 free(priv, M_NG_SSCFU);
549 NG_NODE_SET_PRIVATE(node, priv);
555 ng_sscfu_shutdown(node_p node)
557 struct priv *priv = NG_NODE_PRIVATE(node);
559 sscfu_destroy(priv->sscf);
561 free(priv, M_NG_SSCFU);
562 NG_NODE_SET_PRIVATE(node, NULL);
570 sscfu_verbose(struct sscfu *sscfu, void *arg, const char *fmt, ...)
575 printf("sscfu(%p): ", sscfu);
581 /************************************************************/
586 * Loading and unloading of node type
589 ng_sscfu_mod_event(module_t mod, int event, void *data)