2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2001-2003
5 * Fraunhofer Institute for Open Communication Systems (FhG Fokus).
8 * Author: Hartmut Brandt <harti@freebsd.org>
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * Netgraph module for ATM-Forum UNI 4.0 signalling
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>
46 #include <sys/callout.h>
48 #include <machine/stdarg.h>
50 #include <netgraph/ng_message.h>
51 #include <netgraph/netgraph.h>
52 #include <netgraph/ng_parse.h>
53 #include <netnatm/unimsg.h>
54 #include <netnatm/msg/unistruct.h>
55 #include <netgraph/atm/ngatmbase.h>
56 #include <netnatm/saal/sscopdef.h>
57 #include <netnatm/saal/sscfudef.h>
58 #include <netgraph/atm/uni/ng_uni_cust.h>
59 #include <netnatm/sig/uni.h>
60 #include <netnatm/sig/unisig.h>
61 #include <netgraph/atm/ng_sscop.h>
62 #include <netgraph/atm/ng_sscfu.h>
63 #include <netgraph/atm/ng_uni.h>
65 static MALLOC_DEFINE(M_NG_UNI, "netgraph_uni_node", "netgraph uni node");
66 static MALLOC_DEFINE(M_UNI, "netgraph_uni_data", "uni protocol data");
68 MODULE_DEPEND(ng_uni, ngatmbase, 1, 1, 1);
81 static const struct ng_parse_struct_field ng_uni_config_mask_type_info[] =
82 NGM_UNI_CONFIG_MASK_INFO;
83 static const struct ng_parse_type ng_uni_config_mask_type = {
84 &ng_parse_struct_type,
85 ng_uni_config_mask_type_info
89 static const struct ng_parse_struct_field ng_uni_config_type_info[] =
91 static const struct ng_parse_type ng_uni_config_type = {
92 &ng_parse_struct_type,
93 ng_uni_config_type_info
97 static const struct ng_parse_struct_field ng_uni_set_config_type_info[] =
98 NGM_UNI_SET_CONFIG_INFO;
99 static const struct ng_parse_type ng_uni_set_config_type = {
100 &ng_parse_struct_type,
101 ng_uni_set_config_type_info
107 static const struct ng_parse_fixedarray_info ng_uni_debuglevel_type_info =
108 NGM_UNI_DEBUGLEVEL_INFO;
109 static const struct ng_parse_type ng_uni_debuglevel_type = {
110 &ng_parse_fixedarray_type,
111 &ng_uni_debuglevel_type_info
113 static const struct ng_parse_struct_field ng_uni_debug_type_info[] =
115 static const struct ng_parse_type ng_uni_debug_type = {
116 &ng_parse_struct_type,
117 ng_uni_debug_type_info
123 static const struct ng_cmdlist ng_uni_cmdlist[] = {
149 &ng_uni_set_config_type,
150 &ng_uni_config_mask_type,
171 &ng_parse_uint32_type
177 * Netgraph module data
179 static ng_constructor_t ng_uni_constructor;
180 static ng_shutdown_t ng_uni_shutdown;
181 static ng_rcvmsg_t ng_uni_rcvmsg;
182 static ng_newhook_t ng_uni_newhook;
183 static ng_disconnect_t ng_uni_disconnect;
184 static ng_rcvdata_t ng_uni_rcvlower;
185 static ng_rcvdata_t ng_uni_rcvupper;
187 static int ng_uni_mod_event(module_t, int, void *);
189 static struct ng_type ng_uni_typestruct = {
190 .version = NG_ABI_VERSION,
191 .name = NG_UNI_NODE_TYPE,
192 .mod_event = ng_uni_mod_event,
193 .constructor = ng_uni_constructor,
194 .rcvmsg = ng_uni_rcvmsg,
195 .shutdown = ng_uni_shutdown,
196 .newhook = ng_uni_newhook,
197 .rcvdata = ng_uni_rcvlower,
198 .disconnect = ng_uni_disconnect,
199 .cmdlist = ng_uni_cmdlist,
201 NETGRAPH_INIT(uni, &ng_uni_typestruct);
203 static void uni_uni_output(struct uni *, void *, enum uni_sig, u_int32_t,
205 static void uni_saal_output(struct uni *, void *, enum saal_sig,
207 static void uni_verbose(struct uni *, void *, u_int, const char *, ...)
209 static void uni_do_status(struct uni *, void *, void *, const char *, ...)
212 static const struct uni_funcs uni_funcs = {
219 /************************************************************/
224 ng_uni_constructor(node_p node)
228 priv = malloc(sizeof(*priv), M_NG_UNI, M_WAITOK | M_ZERO);
230 if ((priv->uni = uni_create(node, &uni_funcs)) == NULL) {
231 free(priv, M_NG_UNI);
235 NG_NODE_SET_PRIVATE(node, priv);
236 NG_NODE_FORCE_WRITER(node);
242 ng_uni_shutdown(node_p node)
244 struct priv *priv = NG_NODE_PRIVATE(node);
246 uni_destroy(priv->uni);
248 free(priv, M_NG_UNI);
249 NG_NODE_SET_PRIVATE(node, NULL);
256 /************************************************************/
261 uni_do_status(struct uni *uni, void *uarg, void *sbuf, const char *fmt, ...)
266 sbuf_printf(sbuf, fmt, ap);
271 text_status(node_p node, struct priv *priv, char *buf, u_int len)
276 sbuf_new(&sbuf, buf, len, 0);
278 if (priv->lower != NULL)
279 sbuf_printf(&sbuf, "lower hook: connected to %s:%s\n",
280 NG_NODE_NAME(NG_HOOK_NODE(NG_HOOK_PEER(priv->lower))),
281 NG_HOOK_NAME(NG_HOOK_PEER(priv->lower)));
283 sbuf_printf(&sbuf, "lower hook: <not connected>\n");
285 if (priv->upper != NULL)
286 sbuf_printf(&sbuf, "upper hook: connected to %s:%s\n",
287 NG_NODE_NAME(NG_HOOK_NODE(NG_HOOK_PEER(priv->upper))),
288 NG_HOOK_NAME(NG_HOOK_PEER(priv->upper)));
290 sbuf_printf(&sbuf, "upper hook: <not connected>\n");
292 sbuf_printf(&sbuf, "debugging:");
293 for (f = 0; f < UNI_MAXFACILITY; f++)
294 if (uni_get_debug(priv->uni, f) != 0)
295 sbuf_printf(&sbuf, " %s=%u", uni_facname(f),
296 uni_get_debug(priv->uni, f));
297 sbuf_printf(&sbuf, "\n");
300 uni_status(priv->uni, &sbuf);
303 return (sbuf_len(&sbuf));
307 ng_uni_rcvmsg(node_p node, item_p item, hook_p lasthook)
309 struct priv *priv = NG_NODE_PRIVATE(node);
310 struct ng_mesg *resp = NULL;
315 NGI_GET_MSG(item, msg);
317 switch (msg->header.typecookie) {
319 case NGM_GENERIC_COOKIE:
320 switch (msg->header.cmd) {
322 case NGM_TEXT_STATUS:
323 NG_MKRESPONSE(resp, msg, NG_TEXTRESPONSE, M_NOWAIT);
329 resp->header.arglen = text_status(node, priv,
330 (char *)resp->data, resp->header.arglen) + 1;
340 switch (msg->header.cmd) {
342 case NGM_UNI_SETDEBUG:
344 struct ngm_uni_debug *arg;
346 if (msg->header.arglen > sizeof(*arg)) {
350 arg = (struct ngm_uni_debug *)msg->data;
351 for (i = 0; i < UNI_MAXFACILITY; i++)
352 uni_set_debug(priv->uni, i, arg->level[i]);
356 case NGM_UNI_GETDEBUG:
358 struct ngm_uni_debug *arg;
360 NG_MKRESPONSE(resp, msg, sizeof(*arg), M_NOWAIT);
365 arg = (struct ngm_uni_debug *)resp->data;
366 for (i = 0; i < UNI_MAXFACILITY; i++)
367 arg->level[i] = uni_get_debug(priv->uni, i);
371 case NGM_UNI_GET_CONFIG:
373 struct uni_config *config;
375 if (msg->header.arglen != 0) {
379 NG_MKRESPONSE(resp, msg, sizeof(*config), M_NOWAIT);
384 config = (struct uni_config *)resp->data;
385 uni_get_config(priv->uni, config);
390 case NGM_UNI_SET_CONFIG:
392 struct ngm_uni_set_config *arg;
393 struct ngm_uni_config_mask *mask;
395 if (msg->header.arglen != sizeof(*arg)) {
399 arg = (struct ngm_uni_set_config *)msg->data;
401 NG_MKRESPONSE(resp, msg, sizeof(*mask), M_NOWAIT);
406 mask = (struct ngm_uni_config_mask *)resp->data;
410 uni_set_config(priv->uni, &arg->config,
411 &mask->mask, &mask->popt_mask, &mask->option_mask);
417 if (msg->header.arglen != 0) {
428 case NGM_UNI_DISABLE:
429 if (msg->header.arglen != 0) {
433 if (!priv->enabled) {
438 uni_reset(priv->uni);
441 case NGM_UNI_GETSTATE:
442 if (msg->header.arglen != 0) {
446 NG_MKRESPONSE(resp, msg, sizeof(u_int32_t), M_NOWAIT);
451 *(u_int32_t *)resp->data =
452 priv->enabled ? (uni_getcustate(priv->uni) + 1)
467 NG_RESPOND_MSG(error, node, item, resp);
472 /************************************************************/
477 ng_uni_newhook(node_p node, hook_p hook, const char *name)
479 struct priv *priv = NG_NODE_PRIVATE(node);
481 if (strcmp(name, "lower") == 0) {
483 } else if(strcmp(name, "upper") == 0) {
485 NG_HOOK_SET_RCVDATA(hook, ng_uni_rcvupper);
493 ng_uni_disconnect(hook_p hook)
495 node_p node = NG_HOOK_NODE(hook);
496 struct priv *priv = NG_NODE_PRIVATE(node);
498 if(hook == priv->lower)
500 else if(hook == priv->upper)
503 printf("%s: bogus hook %s\n", __func__, NG_HOOK_NAME(hook));
505 if (NG_NODE_NUMHOOKS(node) == 0) {
506 if (NG_NODE_IS_VALID(node))
507 ng_rmnode_self(node);
513 /************************************************************/
518 * Receive signal from USER.
520 * Repackage the data into one large buffer.
523 ng_uni_rcvupper(hook_p hook, item_p item)
525 node_p node = NG_HOOK_NODE(hook);
526 struct priv *priv = NG_NODE_PRIVATE(node);
532 if (!priv->enabled) {
540 if ((error = uni_msg_unpack_mbuf(m, &msg)) != 0) {
546 if (uni_msg_len(msg) < sizeof(arg)) {
547 printf("%s: packet too short\n", __func__);
548 uni_msg_destroy(msg);
552 bcopy(msg->b_rptr, &arg, sizeof(arg));
553 msg->b_rptr += sizeof(arg);
555 if (arg.sig >= UNIAPI_MAXSIG) {
556 printf("%s: bogus signal\n", __func__);
557 uni_msg_destroy(msg);
560 uni_uni_input(priv->uni, arg.sig, arg.cookie, msg);
568 * Upper layer signal from UNI
571 uni_uni_output(struct uni *uni, void *varg, enum uni_sig sig, u_int32_t cookie,
574 node_p node = (node_p)varg;
575 struct priv *priv = NG_NODE_PRIVATE(node);
580 if (priv->upper == NULL) {
582 uni_msg_destroy(msg);
588 m = uni_msg_pack_mbuf(msg, &arg, sizeof(arg));
590 uni_msg_destroy(msg);
594 NG_SEND_DATA_ONLY(error, priv->upper, m);
599 dump_uni_msg(struct uni_msg *msg)
603 for (pos = 0; pos < uni_msg_len(msg); pos++) {
605 printf("%06o ", pos);
608 printf(" %02x", msg->b_rptr[pos]);
618 * Dump a SAAL signal in either direction
621 dump_saal_signal(node_p node, enum saal_sig sig, struct uni_msg *msg, int to)
623 struct priv *priv = NG_NODE_PRIVATE(node);
625 printf("signal %s SAAL: ", to ? "to" : "from");
629 #define D(S) case S: printf("%s", #S); break
631 D(SAAL_ESTABLISH_request);
632 D(SAAL_ESTABLISH_indication);
633 D(SAAL_ESTABLISH_confirm);
634 D(SAAL_RELEASE_request);
635 D(SAAL_RELEASE_confirm);
636 D(SAAL_RELEASE_indication);
637 D(SAAL_DATA_request);
638 D(SAAL_DATA_indication);
639 D(SAAL_UDATA_request);
640 D(SAAL_UDATA_indication);
644 printf("sig=%d", sig); break;
647 printf(" data=%zu\n", uni_msg_len(msg));
648 if (uni_get_debug(priv->uni, UNI_FAC_SAAL) > 1)
655 * Receive signal from SSCOP.
657 * If this is a data signal, repackage the data into one large buffer.
658 * UNI shouldn't be the bottleneck in a system and this greatly simplifies
662 ng_uni_rcvlower(hook_p hook __unused, item_p item)
664 node_p node = NG_HOOK_NODE(hook);
665 struct priv *priv = NG_NODE_PRIVATE(node);
667 struct sscfu_arg arg;
671 if (!priv->enabled) {
679 if ((error = uni_msg_unpack_mbuf(m, &msg)) != 0) {
685 if (uni_msg_len(msg) < sizeof(arg)) {
686 uni_msg_destroy(msg);
687 printf("%s: packet too short\n", __func__);
690 bcopy(msg->b_rptr, &arg, sizeof(arg));
691 msg->b_rptr += sizeof(arg);
693 if (arg.sig > SAAL_UDATA_indication) {
694 uni_msg_destroy(msg);
695 printf("%s: bogus signal\n", __func__);
699 if (uni_get_debug(priv->uni, UNI_FAC_SAAL) > 0)
700 dump_saal_signal(node, arg.sig, msg, 0);
702 uni_saal_input(priv->uni, arg.sig, msg);
709 * Send signal to sscop.
710 * Pack the message into an mbuf chain.
713 uni_saal_output(struct uni *uni, void *varg, enum saal_sig sig, struct uni_msg *msg)
715 node_p node = (node_p)varg;
716 struct priv *priv = NG_NODE_PRIVATE(node);
718 struct sscfu_arg arg;
721 if (uni_get_debug(priv->uni, UNI_FAC_SAAL) > 0)
722 dump_saal_signal(node, sig, msg, 1);
724 if (priv->lower == NULL) {
726 uni_msg_destroy(msg);
732 m = uni_msg_pack_mbuf(msg, &arg, sizeof(arg));
734 uni_msg_destroy(msg);
738 NG_SEND_DATA_ONLY(error, priv->lower, m);
742 uni_verbose(struct uni *uni, void *varg, u_int fac, const char *fmt, ...)
746 static char *facnames[] = {
747 #define UNI_DEBUG_DEFINE(D) [UNI_FAC_##D] = #D,
749 #undef UNI_DEBUG_DEFINE
752 printf("%s: ", facnames[fac]);
762 /************************************************************/
766 struct unimem_debug {
769 LIST_ENTRY(unimem_debug) link;
772 LIST_HEAD(unimem_debug_list, unimem_debug);
774 static struct unimem_debug_list nguni_freemem[UNIMEM_TYPES] = {
775 LIST_HEAD_INITIALIZER(nguni_freemem[0]),
776 LIST_HEAD_INITIALIZER(nguni_freemem[1]),
777 LIST_HEAD_INITIALIZER(nguni_freemem[2]),
778 LIST_HEAD_INITIALIZER(nguni_freemem[3]),
779 LIST_HEAD_INITIALIZER(nguni_freemem[4]),
781 static struct unimem_debug_list nguni_usedmem[UNIMEM_TYPES] = {
782 LIST_HEAD_INITIALIZER(nguni_usedmem[0]),
783 LIST_HEAD_INITIALIZER(nguni_usedmem[1]),
784 LIST_HEAD_INITIALIZER(nguni_usedmem[2]),
785 LIST_HEAD_INITIALIZER(nguni_usedmem[3]),
786 LIST_HEAD_INITIALIZER(nguni_usedmem[4]),
789 static struct mtx nguni_unilist_mtx;
791 static const char *unimem_names[UNIMEM_TYPES] = {
802 mtx_init(&nguni_unilist_mtx, "netgraph UNI structure lists", NULL,
810 struct unimem_debug *h;
812 for (type = 0; type < UNIMEM_TYPES; type++) {
813 while ((h = LIST_FIRST(&nguni_freemem[type])) != NULL) {
814 LIST_REMOVE(h, link);
818 while ((h = LIST_FIRST(&nguni_usedmem[type])) != NULL) {
819 LIST_REMOVE(h, link);
820 printf("ng_uni: %s in use: %p (%s,%u)\n",
821 unimem_names[type], (caddr_t)h->data,
827 mtx_destroy(&nguni_unilist_mtx);
831 * Allocate a chunk of memory from a given type.
834 ng_uni_malloc(enum unimem type, const char *file, u_int lno)
836 struct unimem_debug *d;
842 mtx_lock(&nguni_unilist_mtx);
843 if ((d = LIST_FIRST(&nguni_freemem[type])) != NULL)
844 LIST_REMOVE(d, link);
845 mtx_unlock(&nguni_unilist_mtx);
851 full = unimem_sizes[type] + offsetof(struct unimem_debug, data);
852 if ((d = malloc(full, M_UNI, M_NOWAIT | M_ZERO)) == NULL)
855 bzero(d->data, unimem_sizes[type]);
860 mtx_lock(&nguni_unilist_mtx);
861 LIST_INSERT_HEAD(&nguni_usedmem[type], d, link);
862 mtx_unlock(&nguni_unilist_mtx);
867 ng_uni_free(enum unimem type, void *ptr, const char *file, u_int lno)
869 struct unimem_debug *d, *h;
871 d = (struct unimem_debug *)
872 ((char *)ptr - offsetof(struct unimem_debug, data));
874 mtx_lock(&nguni_unilist_mtx);
876 LIST_FOREACH(h, &nguni_usedmem[type], link)
881 LIST_REMOVE(d, link);
882 LIST_INSERT_HEAD(&nguni_freemem[type], d, link);
885 * Not on used list - try free list.
887 LIST_FOREACH(h, &nguni_freemem[type], link)
891 printf("ng_uni: %s,%u: %p(%s) was never allocated\n",
892 file, lno, ptr, unimem_names[type]);
894 printf("ng_uni: %s,%u: %p(%s) was already destroyed "
896 file, lno, ptr, unimem_names[type],
899 mtx_unlock(&nguni_unilist_mtx);
901 /************************************************************/
907 * Loading and unloading of node type
910 ng_uni_mod_event(module_t mod, int event, void *data)