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
96 static const struct ng_cmdlist ng_sscfu_cmdlist[] = {
99 NGM_SSCFU_GETDEFPARAM,
102 &ng_sscfu_getdefparam_type
123 &ng_parse_hint32_type
129 &ng_parse_hint32_type,
137 &ng_parse_uint32_type
142 static ng_constructor_t ng_sscfu_constructor;
143 static ng_shutdown_t ng_sscfu_shutdown;
144 static ng_rcvmsg_t ng_sscfu_rcvmsg;
145 static ng_newhook_t ng_sscfu_newhook;
146 static ng_disconnect_t ng_sscfu_disconnect;
147 static ng_rcvdata_t ng_sscfu_rcvupper;
148 static ng_rcvdata_t ng_sscfu_rcvlower;
150 static int ng_sscfu_mod_event(module_t, int, void *);
152 static struct ng_type ng_sscfu_typestruct = {
153 .version = NG_ABI_VERSION,
154 .name = NG_SSCFU_NODE_TYPE,
155 .mod_event = ng_sscfu_mod_event,
156 .constructor = ng_sscfu_constructor,
157 .rcvmsg = ng_sscfu_rcvmsg,
158 .shutdown = ng_sscfu_shutdown,
159 .newhook = ng_sscfu_newhook,
160 .rcvdata = ng_sscfu_rcvupper,
161 .disconnect = ng_sscfu_disconnect,
162 .cmdlist = ng_sscfu_cmdlist,
164 NETGRAPH_INIT(sscfu, &ng_sscfu_typestruct);
166 static void sscfu_send_upper(struct sscfu *, void *, enum saal_sig,
168 static void sscfu_send_lower(struct sscfu *, void *, enum sscop_aasig,
169 struct mbuf *, u_int);
170 static void sscfu_window(struct sscfu *, void *, u_int);
171 static void sscfu_verbose(struct sscfu *, void *, const char *, ...)
174 static const struct sscfu_funcs sscfu_funcs = {
181 /************************************************************/
186 text_status(node_p node, struct priv *priv, char *arg, u_int len)
190 sbuf_new(&sbuf, arg, len, 0);
193 sbuf_printf(&sbuf, "upper hook: %s connected to %s:%s\n",
194 NG_HOOK_NAME(priv->upper),
195 NG_NODE_NAME(NG_HOOK_NODE(NG_HOOK_PEER(priv->upper))),
196 NG_HOOK_NAME(NG_HOOK_PEER(priv->upper)));
198 sbuf_printf(&sbuf, "upper hook: <not connected>\n");
201 sbuf_printf(&sbuf, "lower hook: %s connected to %s:%s\n",
202 NG_HOOK_NAME(priv->lower),
203 NG_NODE_NAME(NG_HOOK_NODE(NG_HOOK_PEER(priv->lower))),
204 NG_HOOK_NAME(NG_HOOK_PEER(priv->lower)));
206 sbuf_printf(&sbuf, "lower hook: <not connected>\n");
208 sbuf_printf(&sbuf, "sscf state: %s\n",
209 priv->enabled == 0 ? "<disabled>" :
210 sscfu_statename(sscfu_getstate(priv->sscf)));
213 return (sbuf_len(&sbuf));
217 ng_sscfu_rcvmsg(node_p node, item_p item, hook_p lasthook)
219 struct priv *priv = NG_NODE_PRIVATE(node);
220 struct ng_mesg *resp = NULL;
224 NGI_GET_MSG(item, msg);
226 switch (msg->header.typecookie) {
228 case NGM_GENERIC_COOKIE:
229 switch (msg->header.cmd) {
231 case NGM_TEXT_STATUS:
232 NG_MKRESPONSE(resp, msg, NG_TEXTRESPONSE, M_NOWAIT);
237 resp->header.arglen = text_status(node, priv,
238 (char *)resp->data, resp->header.arglen) + 1;
247 case NGM_SSCFU_COOKIE:
248 switch (msg->header.cmd) {
250 case NGM_SSCFU_GETDEFPARAM:
252 struct ng_sscfu_getdefparam *p;
254 if (msg->header.arglen != 0) {
258 NG_MKRESPONSE(resp, msg, sizeof(*p), M_NOWAIT);
263 p = (struct ng_sscfu_getdefparam *)resp->data;
264 p->mask = sscfu_getdefparam(&p->param);
268 case NGM_SSCFU_ENABLE:
269 if (msg->header.arglen != 0) {
280 case NGM_SSCFU_DISABLE:
281 if (msg->header.arglen != 0) {
285 if (!priv->enabled) {
290 sscfu_reset(priv->sscf);
293 case NGM_SSCFU_GETSTATE:
294 if (msg->header.arglen != 0) {
298 NG_MKRESPONSE(resp, msg, sizeof(uint32_t), M_NOWAIT);
303 *(uint32_t *)resp->data =
304 priv->enabled ? (sscfu_getstate(priv->sscf) + 1)
308 case NGM_SSCFU_GETDEBUG:
309 if (msg->header.arglen != 0) {
313 NG_MKRESPONSE(resp, msg, sizeof(uint32_t), M_NOWAIT);
318 *(uint32_t *)resp->data = sscfu_getdebug(priv->sscf);
321 case NGM_SSCFU_SETDEBUG:
322 if (msg->header.arglen != sizeof(uint32_t)) {
326 sscfu_setdebug(priv->sscf, *(uint32_t *)msg->data);
340 NG_RESPOND_MSG(error, node, item, resp);
346 /************************************************************/
351 ng_sscfu_newhook(node_p node, hook_p hook, const char *name)
353 struct priv *priv = NG_NODE_PRIVATE(node);
355 if (strcmp(name, "upper") == 0)
357 else if (strcmp(name, "lower") == 0) {
359 NG_HOOK_SET_RCVDATA(hook, ng_sscfu_rcvlower);
366 ng_sscfu_disconnect(hook_p hook)
368 node_p node = NG_HOOK_NODE(hook);
369 struct priv *priv = NG_NODE_PRIVATE(node);
371 if (hook == priv->upper)
373 else if (hook == priv->lower)
376 log(LOG_ERR, "bogus hook");
380 if (NG_NODE_NUMHOOKS(node) == 0) {
381 if (NG_NODE_IS_VALID(node))
382 ng_rmnode_self(node);
385 * Because there are no timeouts reset the protocol
386 * if the lower layer is disconnected.
388 if (priv->lower == NULL &&
390 sscfu_getstate(priv->sscf) != SSCFU_RELEASED)
391 sscfu_reset(priv->sscf);
396 /************************************************************/
401 ng_sscfu_rcvupper(hook_p hook, item_p item)
403 node_p node = NG_HOOK_NODE(hook);
404 struct priv *priv = NG_NODE_PRIVATE(node);
408 if (!priv->enabled || priv->lower == NULL) {
416 if (!(m->m_flags & M_PKTHDR)) {
417 printf("no pkthdr\n");
421 if (m->m_len < (int)sizeof(a) && (m = m_pullup(m, sizeof(a))) == NULL)
423 bcopy((caddr_t)mtod(m, struct sscfu_arg *), &a, sizeof(a));
426 return (sscfu_saalsig(priv->sscf, a.sig, m));
430 sscfu_send_upper(struct sscfu *sscf, void *p, enum saal_sig sig, struct mbuf *m)
432 node_p node = (node_p)p;
433 struct priv *priv = NG_NODE_PRIVATE(node);
437 if (priv->upper == NULL) {
443 MGETHDR(m, M_NOWAIT, MT_DATA);
446 m->m_len = sizeof(struct sscfu_arg);
447 m->m_pkthdr.len = m->m_len;
449 M_PREPEND(m, sizeof(struct sscfu_arg), M_NOWAIT);
453 a = mtod(m, struct sscfu_arg *);
456 NG_SEND_DATA_ONLY(error, priv->upper, m);
460 ng_sscfu_rcvlower(hook_p hook, item_p item)
462 node_p node = NG_HOOK_NODE(hook);
463 struct priv *priv = NG_NODE_PRIVATE(node);
467 if (!priv->enabled || priv->upper == NULL) {
475 if (!(m->m_flags & M_PKTHDR)) {
476 printf("no pkthdr\n");
482 * Strip of the SSCOP header.
484 if (m->m_len < (int)sizeof(a) && (m = m_pullup(m, sizeof(a))) == NULL)
486 bcopy((caddr_t)mtod(m, struct sscop_arg *), &a, sizeof(a));
489 sscfu_input(priv->sscf, a.sig, m, a.arg);
495 sscfu_send_lower(struct sscfu *sscf, void *p, enum sscop_aasig sig,
496 struct mbuf *m, u_int arg)
498 node_p node = (node_p)p;
499 struct priv *priv = NG_NODE_PRIVATE(node);
503 if (priv->lower == NULL) {
509 MGETHDR(m, M_NOWAIT, MT_DATA);
512 m->m_len = sizeof(struct sscop_arg);
513 m->m_pkthdr.len = m->m_len;
515 M_PREPEND(m, sizeof(struct sscop_arg), M_NOWAIT);
519 a = mtod(m, struct sscop_arg *);
523 NG_SEND_DATA_ONLY(error, priv->lower, m);
527 * Window is handled by ng_sscop so make this a NOP.
530 sscfu_window(struct sscfu *sscfu, void *arg, u_int w)
534 /************************************************************/
539 ng_sscfu_constructor(node_p node)
543 priv = malloc(sizeof(*priv), M_NG_SSCFU, M_WAITOK | M_ZERO);
545 if ((priv->sscf = sscfu_create(node, &sscfu_funcs)) == NULL) {
546 free(priv, M_NG_SSCFU);
550 NG_NODE_SET_PRIVATE(node, priv);
556 ng_sscfu_shutdown(node_p node)
558 struct priv *priv = NG_NODE_PRIVATE(node);
560 sscfu_destroy(priv->sscf);
562 free(priv, M_NG_SSCFU);
563 NG_NODE_SET_PRIVATE(node, NULL);
571 sscfu_verbose(struct sscfu *sscfu, void *arg, const char *fmt, ...)
576 printf("sscfu(%p): ", sscfu);
582 /************************************************************/
587 * Loading and unloading of node type
590 ng_sscfu_mod_event(module_t mod, int event, void *data)