2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2001-2003
5 * Fraunhofer Institute for Open Communication Systems (FhG Fokus).
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * Author: Hartmut Brandt <harti@freebsd.org>
31 * Netgraph module for ITU-T Q.2120 UNI SSCF.
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/kernel.h>
40 #include <sys/malloc.h>
42 #include <sys/errno.h>
43 #include <sys/syslog.h>
44 #include <sys/socket.h>
45 #include <sys/socketvar.h>
47 #include <machine/stdarg.h>
49 #include <netgraph/ng_message.h>
50 #include <netgraph/netgraph.h>
51 #include <netgraph/ng_parse.h>
52 #include <netnatm/saal/sscopdef.h>
53 #include <netnatm/saal/sscfudef.h>
54 #include <netgraph/atm/ng_sscop.h>
55 #include <netgraph/atm/ng_sscfu.h>
56 #include <netgraph/atm/sscfu/ng_sscfu_cust.h>
57 #include <netnatm/saal/sscfu.h>
59 MALLOC_DEFINE(M_NG_SSCFU, "netgraph_sscfu", "netgraph uni sscf node");
61 MODULE_DEPEND(ng_sscfu, ngatmbase, 1, 1, 1);
67 hook_p upper; /* SAAL interface */
68 hook_p lower; /* SSCOP interface */
69 struct sscfu *sscf; /* the instance */
79 static const struct ng_parse_struct_field ng_sscop_param_type_info[] =
82 static const struct ng_parse_type ng_sscop_param_type = {
83 &ng_parse_struct_type,
84 ng_sscop_param_type_info
87 static const struct ng_parse_struct_field ng_sscfu_getdefparam_type_info[] =
88 NG_SSCFU_GETDEFPARAM_INFO;
90 static const struct ng_parse_type ng_sscfu_getdefparam_type = {
91 &ng_parse_struct_type,
92 ng_sscfu_getdefparam_type_info
95 static const struct ng_cmdlist ng_sscfu_cmdlist[] = {
98 NGM_SSCFU_GETDEFPARAM,
101 &ng_sscfu_getdefparam_type
122 &ng_parse_hint32_type
128 &ng_parse_hint32_type,
136 &ng_parse_uint32_type
141 static ng_constructor_t ng_sscfu_constructor;
142 static ng_shutdown_t ng_sscfu_shutdown;
143 static ng_rcvmsg_t ng_sscfu_rcvmsg;
144 static ng_newhook_t ng_sscfu_newhook;
145 static ng_disconnect_t ng_sscfu_disconnect;
146 static ng_rcvdata_t ng_sscfu_rcvupper;
147 static ng_rcvdata_t ng_sscfu_rcvlower;
149 static int ng_sscfu_mod_event(module_t, int, void *);
151 static struct ng_type ng_sscfu_typestruct = {
152 .version = NG_ABI_VERSION,
153 .name = NG_SSCFU_NODE_TYPE,
154 .mod_event = ng_sscfu_mod_event,
155 .constructor = ng_sscfu_constructor,
156 .rcvmsg = ng_sscfu_rcvmsg,
157 .shutdown = ng_sscfu_shutdown,
158 .newhook = ng_sscfu_newhook,
159 .rcvdata = ng_sscfu_rcvupper,
160 .disconnect = ng_sscfu_disconnect,
161 .cmdlist = ng_sscfu_cmdlist,
163 NETGRAPH_INIT(sscfu, &ng_sscfu_typestruct);
165 static void sscfu_send_upper(struct sscfu *, void *, enum saal_sig,
167 static void sscfu_send_lower(struct sscfu *, void *, enum sscop_aasig,
168 struct mbuf *, u_int);
169 static void sscfu_window(struct sscfu *, void *, u_int);
170 static void sscfu_verbose(struct sscfu *, void *, const char *, ...)
173 static const struct sscfu_funcs sscfu_funcs = {
180 /************************************************************/
185 text_status(node_p node, struct priv *priv, char *arg, u_int len)
189 sbuf_new(&sbuf, arg, len, 0);
192 sbuf_printf(&sbuf, "upper hook: %s connected to %s:%s\n",
193 NG_HOOK_NAME(priv->upper),
194 NG_NODE_NAME(NG_HOOK_NODE(NG_HOOK_PEER(priv->upper))),
195 NG_HOOK_NAME(NG_HOOK_PEER(priv->upper)));
197 sbuf_printf(&sbuf, "upper hook: <not connected>\n");
200 sbuf_printf(&sbuf, "lower hook: %s connected to %s:%s\n",
201 NG_HOOK_NAME(priv->lower),
202 NG_NODE_NAME(NG_HOOK_NODE(NG_HOOK_PEER(priv->lower))),
203 NG_HOOK_NAME(NG_HOOK_PEER(priv->lower)));
205 sbuf_printf(&sbuf, "lower hook: <not connected>\n");
207 sbuf_printf(&sbuf, "sscf state: %s\n",
208 priv->enabled == 0 ? "<disabled>" :
209 sscfu_statename(sscfu_getstate(priv->sscf)));
212 return (sbuf_len(&sbuf));
216 ng_sscfu_rcvmsg(node_p node, item_p item, hook_p lasthook)
218 struct priv *priv = NG_NODE_PRIVATE(node);
219 struct ng_mesg *resp = NULL;
223 NGI_GET_MSG(item, msg);
225 switch (msg->header.typecookie) {
226 case NGM_GENERIC_COOKIE:
227 switch (msg->header.cmd) {
228 case NGM_TEXT_STATUS:
229 NG_MKRESPONSE(resp, msg, NG_TEXTRESPONSE, M_NOWAIT);
234 resp->header.arglen = text_status(node, priv,
235 (char *)resp->data, resp->header.arglen) + 1;
244 case NGM_SSCFU_COOKIE:
245 switch (msg->header.cmd) {
246 case NGM_SSCFU_GETDEFPARAM:
248 struct ng_sscfu_getdefparam *p;
250 if (msg->header.arglen != 0) {
254 NG_MKRESPONSE(resp, msg, sizeof(*p), M_NOWAIT);
259 p = (struct ng_sscfu_getdefparam *)resp->data;
260 p->mask = sscfu_getdefparam(&p->param);
264 case NGM_SSCFU_ENABLE:
265 if (msg->header.arglen != 0) {
276 case NGM_SSCFU_DISABLE:
277 if (msg->header.arglen != 0) {
281 if (!priv->enabled) {
286 sscfu_reset(priv->sscf);
289 case NGM_SSCFU_GETSTATE:
290 if (msg->header.arglen != 0) {
294 NG_MKRESPONSE(resp, msg, sizeof(uint32_t), M_NOWAIT);
299 *(uint32_t *)resp->data =
300 priv->enabled ? (sscfu_getstate(priv->sscf) + 1)
304 case NGM_SSCFU_GETDEBUG:
305 if (msg->header.arglen != 0) {
309 NG_MKRESPONSE(resp, msg, sizeof(uint32_t), M_NOWAIT);
314 *(uint32_t *)resp->data = sscfu_getdebug(priv->sscf);
317 case NGM_SSCFU_SETDEBUG:
318 if (msg->header.arglen != sizeof(uint32_t)) {
322 sscfu_setdebug(priv->sscf, *(uint32_t *)msg->data);
336 NG_RESPOND_MSG(error, node, item, resp);
342 /************************************************************/
347 ng_sscfu_newhook(node_p node, hook_p hook, const char *name)
349 struct priv *priv = NG_NODE_PRIVATE(node);
351 if (strcmp(name, "upper") == 0)
353 else if (strcmp(name, "lower") == 0) {
355 NG_HOOK_SET_RCVDATA(hook, ng_sscfu_rcvlower);
362 ng_sscfu_disconnect(hook_p hook)
364 node_p node = NG_HOOK_NODE(hook);
365 struct priv *priv = NG_NODE_PRIVATE(node);
367 if (hook == priv->upper)
369 else if (hook == priv->lower)
372 log(LOG_ERR, "bogus hook");
376 if (NG_NODE_NUMHOOKS(node) == 0) {
377 if (NG_NODE_IS_VALID(node))
378 ng_rmnode_self(node);
381 * Because there are no timeouts reset the protocol
382 * if the lower layer is disconnected.
384 if (priv->lower == NULL &&
386 sscfu_getstate(priv->sscf) != SSCFU_RELEASED)
387 sscfu_reset(priv->sscf);
392 /************************************************************/
397 ng_sscfu_rcvupper(hook_p hook, item_p item)
399 node_p node = NG_HOOK_NODE(hook);
400 struct priv *priv = NG_NODE_PRIVATE(node);
404 if (!priv->enabled || priv->lower == NULL) {
412 if (!(m->m_flags & M_PKTHDR)) {
413 printf("no pkthdr\n");
417 if (m->m_len < (int)sizeof(a) && (m = m_pullup(m, sizeof(a))) == NULL)
419 bcopy((caddr_t)mtod(m, struct sscfu_arg *), &a, sizeof(a));
422 return (sscfu_saalsig(priv->sscf, a.sig, m));
426 sscfu_send_upper(struct sscfu *sscf, void *p, enum saal_sig sig, struct mbuf *m)
428 node_p node = (node_p)p;
429 struct priv *priv = NG_NODE_PRIVATE(node);
433 if (priv->upper == NULL) {
439 MGETHDR(m, M_NOWAIT, MT_DATA);
442 m->m_len = sizeof(struct sscfu_arg);
443 m->m_pkthdr.len = m->m_len;
445 M_PREPEND(m, sizeof(struct sscfu_arg), M_NOWAIT);
449 a = mtod(m, struct sscfu_arg *);
452 NG_SEND_DATA_ONLY(error, priv->upper, m);
456 ng_sscfu_rcvlower(hook_p hook, item_p item)
458 node_p node = NG_HOOK_NODE(hook);
459 struct priv *priv = NG_NODE_PRIVATE(node);
463 if (!priv->enabled || priv->upper == NULL) {
471 if (!(m->m_flags & M_PKTHDR)) {
472 printf("no pkthdr\n");
478 * Strip of the SSCOP header.
480 if (m->m_len < (int)sizeof(a) && (m = m_pullup(m, sizeof(a))) == NULL)
482 bcopy((caddr_t)mtod(m, struct sscop_arg *), &a, sizeof(a));
485 sscfu_input(priv->sscf, a.sig, m, a.arg);
491 sscfu_send_lower(struct sscfu *sscf, void *p, enum sscop_aasig sig,
492 struct mbuf *m, u_int arg)
494 node_p node = (node_p)p;
495 struct priv *priv = NG_NODE_PRIVATE(node);
499 if (priv->lower == NULL) {
505 MGETHDR(m, M_NOWAIT, MT_DATA);
508 m->m_len = sizeof(struct sscop_arg);
509 m->m_pkthdr.len = m->m_len;
511 M_PREPEND(m, sizeof(struct sscop_arg), M_NOWAIT);
515 a = mtod(m, struct sscop_arg *);
519 NG_SEND_DATA_ONLY(error, priv->lower, m);
523 * Window is handled by ng_sscop so make this a NOP.
526 sscfu_window(struct sscfu *sscfu, void *arg, u_int w)
530 /************************************************************/
535 ng_sscfu_constructor(node_p node)
539 priv = malloc(sizeof(*priv), M_NG_SSCFU, M_WAITOK | M_ZERO);
541 if ((priv->sscf = sscfu_create(node, &sscfu_funcs)) == NULL) {
542 free(priv, M_NG_SSCFU);
546 NG_NODE_SET_PRIVATE(node, priv);
552 ng_sscfu_shutdown(node_p node)
554 struct priv *priv = NG_NODE_PRIVATE(node);
556 sscfu_destroy(priv->sscf);
558 free(priv, M_NG_SSCFU);
559 NG_NODE_SET_PRIVATE(node, NULL);
567 sscfu_verbose(struct sscfu *sscfu, void *arg, const char *fmt, ...)
572 printf("sscfu(%p): ", sscfu);
578 /************************************************************/
583 * Loading and unloading of node type
586 ng_sscfu_mod_event(module_t mod, int event, void *data)