2 * Copyright (c) 2001-2003
3 * Fraunhofer Institute for Open Communication Systems (FhG Fokus).
6 * Author: Hartmut Brandt <harti@freebsd.org>
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 * Netgraph module for ATM-Forum UNI 4.0 signalling
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>
44 #include <sys/callout.h>
46 #include <machine/stdarg.h>
48 #include <netgraph/ng_message.h>
49 #include <netgraph/netgraph.h>
50 #include <netgraph/ng_parse.h>
51 #include <netnatm/unimsg.h>
52 #include <netnatm/msg/unistruct.h>
53 #include <netgraph/atm/ngatmbase.h>
54 #include <netnatm/saal/sscopdef.h>
55 #include <netnatm/saal/sscfudef.h>
56 #include <netgraph/atm/uni/ng_uni_cust.h>
57 #include <netnatm/sig/uni.h>
58 #include <netnatm/sig/unisig.h>
59 #include <netgraph/atm/ng_sscop.h>
60 #include <netgraph/atm/ng_sscfu.h>
61 #include <netgraph/atm/ng_uni.h>
63 static MALLOC_DEFINE(M_NG_UNI, "netgraph_uni_node", "netgraph uni node");
64 static MALLOC_DEFINE(M_UNI, "netgraph_uni_data", "uni protocol data");
66 MODULE_DEPEND(ng_uni, ngatmbase, 1, 1, 1);
79 static const struct ng_parse_struct_field ng_uni_config_mask_type_info[] =
80 NGM_UNI_CONFIG_MASK_INFO;
81 static const struct ng_parse_type ng_uni_config_mask_type = {
82 &ng_parse_struct_type,
83 ng_uni_config_mask_type_info
87 static const struct ng_parse_struct_field ng_uni_config_type_info[] =
89 static const struct ng_parse_type ng_uni_config_type = {
90 &ng_parse_struct_type,
91 ng_uni_config_type_info
95 static const struct ng_parse_struct_field ng_uni_set_config_type_info[] =
96 NGM_UNI_SET_CONFIG_INFO;
97 static const struct ng_parse_type ng_uni_set_config_type = {
98 &ng_parse_struct_type,
99 ng_uni_set_config_type_info
105 static const struct ng_parse_fixedarray_info ng_uni_debuglevel_type_info =
106 NGM_UNI_DEBUGLEVEL_INFO;
107 static const struct ng_parse_type ng_uni_debuglevel_type = {
108 &ng_parse_fixedarray_type,
109 &ng_uni_debuglevel_type_info
111 static const struct ng_parse_struct_field ng_uni_debug_type_info[] =
113 static const struct ng_parse_type ng_uni_debug_type = {
114 &ng_parse_struct_type,
115 ng_uni_debug_type_info
121 static const struct ng_cmdlist ng_uni_cmdlist[] = {
147 &ng_uni_set_config_type,
148 &ng_uni_config_mask_type,
169 &ng_parse_uint32_type
175 * Netgraph module data
177 static ng_constructor_t ng_uni_constructor;
178 static ng_shutdown_t ng_uni_shutdown;
179 static ng_rcvmsg_t ng_uni_rcvmsg;
180 static ng_newhook_t ng_uni_newhook;
181 static ng_disconnect_t ng_uni_disconnect;
182 static ng_rcvdata_t ng_uni_rcvlower;
183 static ng_rcvdata_t ng_uni_rcvupper;
185 static int ng_uni_mod_event(module_t, int, void *);
187 static struct ng_type ng_uni_typestruct = {
188 .version = NG_ABI_VERSION,
189 .name = NG_UNI_NODE_TYPE,
190 .mod_event = ng_uni_mod_event,
191 .constructor = ng_uni_constructor,
192 .rcvmsg = ng_uni_rcvmsg,
193 .shutdown = ng_uni_shutdown,
194 .newhook = ng_uni_newhook,
195 .rcvdata = ng_uni_rcvlower,
196 .disconnect = ng_uni_disconnect,
197 .cmdlist = ng_uni_cmdlist,
199 NETGRAPH_INIT(uni, &ng_uni_typestruct);
201 static void uni_uni_output(struct uni *, void *, enum uni_sig, u_int32_t,
203 static void uni_saal_output(struct uni *, void *, enum saal_sig,
205 static void uni_verbose(struct uni *, void *, u_int, const char *, ...)
207 static void uni_do_status(struct uni *, void *, void *, const char *, ...)
210 static const struct uni_funcs uni_funcs = {
217 /************************************************************/
222 ng_uni_constructor(node_p node)
226 priv = malloc(sizeof(*priv), M_NG_UNI, M_WAITOK | M_ZERO);
228 if ((priv->uni = uni_create(node, &uni_funcs)) == NULL) {
229 free(priv, M_NG_UNI);
233 NG_NODE_SET_PRIVATE(node, priv);
234 NG_NODE_FORCE_WRITER(node);
240 ng_uni_shutdown(node_p node)
242 struct priv *priv = NG_NODE_PRIVATE(node);
244 uni_destroy(priv->uni);
246 free(priv, M_NG_UNI);
247 NG_NODE_SET_PRIVATE(node, NULL);
254 /************************************************************/
259 uni_do_status(struct uni *uni, void *uarg, void *sbuf, const char *fmt, ...)
264 sbuf_printf(sbuf, fmt, ap);
269 text_status(node_p node, struct priv *priv, char *buf, u_int len)
274 sbuf_new(&sbuf, buf, len, 0);
276 if (priv->lower != NULL)
277 sbuf_printf(&sbuf, "lower hook: connected to %s:%s\n",
278 NG_NODE_NAME(NG_HOOK_NODE(NG_HOOK_PEER(priv->lower))),
279 NG_HOOK_NAME(NG_HOOK_PEER(priv->lower)));
281 sbuf_printf(&sbuf, "lower hook: <not connected>\n");
283 if (priv->upper != NULL)
284 sbuf_printf(&sbuf, "upper hook: connected to %s:%s\n",
285 NG_NODE_NAME(NG_HOOK_NODE(NG_HOOK_PEER(priv->upper))),
286 NG_HOOK_NAME(NG_HOOK_PEER(priv->upper)));
288 sbuf_printf(&sbuf, "upper hook: <not connected>\n");
290 sbuf_printf(&sbuf, "debugging:");
291 for (f = 0; f < UNI_MAXFACILITY; f++)
292 if (uni_get_debug(priv->uni, f) != 0)
293 sbuf_printf(&sbuf, " %s=%u", uni_facname(f),
294 uni_get_debug(priv->uni, f));
295 sbuf_printf(&sbuf, "\n");
298 uni_status(priv->uni, &sbuf);
301 return (sbuf_len(&sbuf));
305 ng_uni_rcvmsg(node_p node, item_p item, hook_p lasthook)
307 struct priv *priv = NG_NODE_PRIVATE(node);
308 struct ng_mesg *resp = NULL;
313 NGI_GET_MSG(item, msg);
315 switch (msg->header.typecookie) {
317 case NGM_GENERIC_COOKIE:
318 switch (msg->header.cmd) {
320 case NGM_TEXT_STATUS:
321 NG_MKRESPONSE(resp, msg, NG_TEXTRESPONSE, M_NOWAIT);
327 resp->header.arglen = text_status(node, priv,
328 (char *)resp->data, resp->header.arglen) + 1;
338 switch (msg->header.cmd) {
340 case NGM_UNI_SETDEBUG:
342 struct ngm_uni_debug *arg;
344 if (msg->header.arglen > sizeof(*arg)) {
348 arg = (struct ngm_uni_debug *)msg->data;
349 for (i = 0; i < UNI_MAXFACILITY; i++)
350 uni_set_debug(priv->uni, i, arg->level[i]);
354 case NGM_UNI_GETDEBUG:
356 struct ngm_uni_debug *arg;
358 NG_MKRESPONSE(resp, msg, sizeof(*arg), M_NOWAIT);
363 arg = (struct ngm_uni_debug *)resp->data;
364 for (i = 0; i < UNI_MAXFACILITY; i++)
365 arg->level[i] = uni_get_debug(priv->uni, i);
369 case NGM_UNI_GET_CONFIG:
371 struct uni_config *config;
373 if (msg->header.arglen != 0) {
377 NG_MKRESPONSE(resp, msg, sizeof(*config), M_NOWAIT);
382 config = (struct uni_config *)resp->data;
383 uni_get_config(priv->uni, config);
388 case NGM_UNI_SET_CONFIG:
390 struct ngm_uni_set_config *arg;
391 struct ngm_uni_config_mask *mask;
393 if (msg->header.arglen != sizeof(*arg)) {
397 arg = (struct ngm_uni_set_config *)msg->data;
399 NG_MKRESPONSE(resp, msg, sizeof(*mask), M_NOWAIT);
404 mask = (struct ngm_uni_config_mask *)resp->data;
408 uni_set_config(priv->uni, &arg->config,
409 &mask->mask, &mask->popt_mask, &mask->option_mask);
415 if (msg->header.arglen != 0) {
426 case NGM_UNI_DISABLE:
427 if (msg->header.arglen != 0) {
431 if (!priv->enabled) {
436 uni_reset(priv->uni);
439 case NGM_UNI_GETSTATE:
440 if (msg->header.arglen != 0) {
444 NG_MKRESPONSE(resp, msg, sizeof(u_int32_t), M_NOWAIT);
449 *(u_int32_t *)resp->data =
450 priv->enabled ? (uni_getcustate(priv->uni) + 1)
465 NG_RESPOND_MSG(error, node, item, resp);
470 /************************************************************/
475 ng_uni_newhook(node_p node, hook_p hook, const char *name)
477 struct priv *priv = NG_NODE_PRIVATE(node);
479 if (strcmp(name, "lower") == 0) {
481 } else if(strcmp(name, "upper") == 0) {
483 NG_HOOK_SET_RCVDATA(hook, ng_uni_rcvupper);
491 ng_uni_disconnect(hook_p hook)
493 node_p node = NG_HOOK_NODE(hook);
494 struct priv *priv = NG_NODE_PRIVATE(node);
496 if(hook == priv->lower)
498 else if(hook == priv->upper)
501 printf("%s: bogus hook %s\n", __func__, NG_HOOK_NAME(hook));
503 if (NG_NODE_NUMHOOKS(node) == 0) {
504 if (NG_NODE_IS_VALID(node))
505 ng_rmnode_self(node);
511 /************************************************************/
516 * Receive signal from USER.
518 * Repackage the data into one large buffer.
521 ng_uni_rcvupper(hook_p hook, item_p item)
523 node_p node = NG_HOOK_NODE(hook);
524 struct priv *priv = NG_NODE_PRIVATE(node);
530 if (!priv->enabled) {
538 if ((error = uni_msg_unpack_mbuf(m, &msg)) != 0) {
544 if (uni_msg_len(msg) < sizeof(arg)) {
545 printf("%s: packet too short\n", __func__);
546 uni_msg_destroy(msg);
550 bcopy(msg->b_rptr, &arg, sizeof(arg));
551 msg->b_rptr += sizeof(arg);
553 if (arg.sig >= UNIAPI_MAXSIG) {
554 printf("%s: bogus signal\n", __func__);
555 uni_msg_destroy(msg);
558 uni_uni_input(priv->uni, arg.sig, arg.cookie, msg);
566 * Upper layer signal from UNI
569 uni_uni_output(struct uni *uni, void *varg, enum uni_sig sig, u_int32_t cookie,
572 node_p node = (node_p)varg;
573 struct priv *priv = NG_NODE_PRIVATE(node);
578 if (priv->upper == NULL) {
580 uni_msg_destroy(msg);
586 m = uni_msg_pack_mbuf(msg, &arg, sizeof(arg));
588 uni_msg_destroy(msg);
592 NG_SEND_DATA_ONLY(error, priv->upper, m);
597 dump_uni_msg(struct uni_msg *msg)
601 for (pos = 0; pos < uni_msg_len(msg); pos++) {
603 printf("%06o ", pos);
606 printf(" %02x", msg->b_rptr[pos]);
616 * Dump a SAAL signal in either direction
619 dump_saal_signal(node_p node, enum saal_sig sig, struct uni_msg *msg, int to)
621 struct priv *priv = NG_NODE_PRIVATE(node);
623 printf("signal %s SAAL: ", to ? "to" : "from");
627 #define D(S) case S: printf("%s", #S); break
629 D(SAAL_ESTABLISH_request);
630 D(SAAL_ESTABLISH_indication);
631 D(SAAL_ESTABLISH_confirm);
632 D(SAAL_RELEASE_request);
633 D(SAAL_RELEASE_confirm);
634 D(SAAL_RELEASE_indication);
635 D(SAAL_DATA_request);
636 D(SAAL_DATA_indication);
637 D(SAAL_UDATA_request);
638 D(SAAL_UDATA_indication);
642 printf("sig=%d", sig); break;
645 printf(" data=%zu\n", uni_msg_len(msg));
646 if (uni_get_debug(priv->uni, UNI_FAC_SAAL) > 1)
653 * Receive signal from SSCOP.
655 * If this is a data signal, repackage the data into one large buffer.
656 * UNI shouldn't be the bottleneck in a system and this greatly simplifies
660 ng_uni_rcvlower(hook_p hook __unused, item_p item)
662 node_p node = NG_HOOK_NODE(hook);
663 struct priv *priv = NG_NODE_PRIVATE(node);
665 struct sscfu_arg arg;
669 if (!priv->enabled) {
677 if ((error = uni_msg_unpack_mbuf(m, &msg)) != 0) {
683 if (uni_msg_len(msg) < sizeof(arg)) {
684 uni_msg_destroy(msg);
685 printf("%s: packet too short\n", __func__);
688 bcopy(msg->b_rptr, &arg, sizeof(arg));
689 msg->b_rptr += sizeof(arg);
691 if (arg.sig > SAAL_UDATA_indication) {
692 uni_msg_destroy(msg);
693 printf("%s: bogus signal\n", __func__);
697 if (uni_get_debug(priv->uni, UNI_FAC_SAAL) > 0)
698 dump_saal_signal(node, arg.sig, msg, 0);
700 uni_saal_input(priv->uni, arg.sig, msg);
707 * Send signal to sscop.
708 * Pack the message into an mbuf chain.
711 uni_saal_output(struct uni *uni, void *varg, enum saal_sig sig, struct uni_msg *msg)
713 node_p node = (node_p)varg;
714 struct priv *priv = NG_NODE_PRIVATE(node);
716 struct sscfu_arg arg;
719 if (uni_get_debug(priv->uni, UNI_FAC_SAAL) > 0)
720 dump_saal_signal(node, sig, msg, 1);
722 if (priv->lower == NULL) {
724 uni_msg_destroy(msg);
730 m = uni_msg_pack_mbuf(msg, &arg, sizeof(arg));
732 uni_msg_destroy(msg);
736 NG_SEND_DATA_ONLY(error, priv->lower, m);
740 uni_verbose(struct uni *uni, void *varg, u_int fac, const char *fmt, ...)
744 static char *facnames[] = {
745 #define UNI_DEBUG_DEFINE(D) [UNI_FAC_##D] = #D,
747 #undef UNI_DEBUG_DEFINE
750 printf("%s: ", facnames[fac]);
760 /************************************************************/
764 struct unimem_debug {
767 LIST_ENTRY(unimem_debug) link;
770 LIST_HEAD(unimem_debug_list, unimem_debug);
772 static struct unimem_debug_list nguni_freemem[UNIMEM_TYPES] = {
773 LIST_HEAD_INITIALIZER(nguni_freemem[0]),
774 LIST_HEAD_INITIALIZER(nguni_freemem[1]),
775 LIST_HEAD_INITIALIZER(nguni_freemem[2]),
776 LIST_HEAD_INITIALIZER(nguni_freemem[3]),
777 LIST_HEAD_INITIALIZER(nguni_freemem[4]),
779 static struct unimem_debug_list nguni_usedmem[UNIMEM_TYPES] = {
780 LIST_HEAD_INITIALIZER(nguni_usedmem[0]),
781 LIST_HEAD_INITIALIZER(nguni_usedmem[1]),
782 LIST_HEAD_INITIALIZER(nguni_usedmem[2]),
783 LIST_HEAD_INITIALIZER(nguni_usedmem[3]),
784 LIST_HEAD_INITIALIZER(nguni_usedmem[4]),
787 static struct mtx nguni_unilist_mtx;
789 static const char *unimem_names[UNIMEM_TYPES] = {
800 mtx_init(&nguni_unilist_mtx, "netgraph UNI structure lists", NULL,
808 struct unimem_debug *h;
810 for (type = 0; type < UNIMEM_TYPES; type++) {
811 while ((h = LIST_FIRST(&nguni_freemem[type])) != NULL) {
812 LIST_REMOVE(h, link);
816 while ((h = LIST_FIRST(&nguni_usedmem[type])) != NULL) {
817 LIST_REMOVE(h, link);
818 printf("ng_uni: %s in use: %p (%s,%u)\n",
819 unimem_names[type], (caddr_t)h->data,
825 mtx_destroy(&nguni_unilist_mtx);
829 * Allocate a chunk of memory from a given type.
832 ng_uni_malloc(enum unimem type, const char *file, u_int lno)
834 struct unimem_debug *d;
840 mtx_lock(&nguni_unilist_mtx);
841 if ((d = LIST_FIRST(&nguni_freemem[type])) != NULL)
842 LIST_REMOVE(d, link);
843 mtx_unlock(&nguni_unilist_mtx);
849 full = unimem_sizes[type] + offsetof(struct unimem_debug, data);
850 if ((d = malloc(full, M_UNI, M_NOWAIT | M_ZERO)) == NULL)
853 bzero(d->data, unimem_sizes[type]);
858 mtx_lock(&nguni_unilist_mtx);
859 LIST_INSERT_HEAD(&nguni_usedmem[type], d, link);
860 mtx_unlock(&nguni_unilist_mtx);
865 ng_uni_free(enum unimem type, void *ptr, const char *file, u_int lno)
867 struct unimem_debug *d, *h;
869 d = (struct unimem_debug *)
870 ((char *)ptr - offsetof(struct unimem_debug, data));
872 mtx_lock(&nguni_unilist_mtx);
874 LIST_FOREACH(h, &nguni_usedmem[type], link)
879 LIST_REMOVE(d, link);
880 LIST_INSERT_HEAD(&nguni_freemem[type], d, link);
883 * Not on used list - try free list.
885 LIST_FOREACH(h, &nguni_freemem[type], link)
889 printf("ng_uni: %s,%u: %p(%s) was never allocated\n",
890 file, lno, ptr, unimem_names[type]);
892 printf("ng_uni: %s,%u: %p(%s) was already destroyed "
894 file, lno, ptr, unimem_names[type],
897 mtx_unlock(&nguni_unilist_mtx);
899 /************************************************************/
905 * Loading and unloading of node type
908 ng_uni_mod_event(module_t mod, int event, void *data)