Netgraph port from FreeBSD - initial porting work
[dragonfly.git] / sys / netgraph7 / atm / sscfu / ng_sscfu.c
blobd433bdf39de9cfc70e17e57ea760e7317bb863de
1 /*-
2 * Copyright (c) 2001-2003
3 * Fraunhofer Institute for Open Communication Systems (FhG Fokus).
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
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
25 * SUCH DAMAGE.
27 * Author: Hartmut Brandt <harti@freebsd.org>
29 * Netgraph module for ITU-T Q.2120 UNI SSCF.
31 * $FreeBSD: src/sys/netgraph/atm/sscfu/ng_sscfu.c,v 1.4 2005/01/07 01:45:41 imp Exp $
32 * $DragonFly: src/sys/netgraph7/atm/sscfu/ng_sscfu.c,v 1.2 2008/06/26 23:05:39 dillon Exp $
33 * $DragonFly: src/sys/netgraph7/atm/sscfu/ng_sscfu.c,v 1.2 2008/06/26 23:05:39 dillon Exp $
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/kernel.h>
39 #include <sys/malloc.h>
40 #include <sys/mbuf.h>
41 #include <sys/errno.h>
42 #include <sys/syslog.h>
43 #include <sys/socket.h>
44 #include <sys/socketvar.h>
45 #include <sys/sbuf.h>
46 #include <machine/stdarg.h>
48 #include "ng_message.h"
49 #include "netgraph.h"
50 #include "ng_parse.h"
51 #include <netnatm/saal/sscopdef.h>
52 #include <netnatm/saal/sscfudef.h>
53 #include "atm/ng_sscop.h"
54 #include "atm/ng_sscfu.h"
55 #include "atm/sscfu/ng_sscfu_cust.h"
56 #include <netnatm/saal/sscfu.h>
58 MALLOC_DEFINE(M_NG_SSCFU, "netgraph_sscfu", "netgraph uni sscf node");
60 MODULE_DEPEND(ng_sscfu, ngatmbase, 1, 1, 1);
63 * Private data
65 struct priv {
66 hook_p upper; /* SAAL interface */
67 hook_p lower; /* SSCOP interface */
68 struct sscfu *sscf; /* the instance */
69 int enabled;
73 * PARSING
76 * Parse PARAM type
78 static const struct ng_parse_struct_field ng_sscop_param_type_info[] =
79 NG_SSCOP_PARAM_INFO;
81 static const struct ng_parse_type ng_sscop_param_type = {
82 &ng_parse_struct_type,
83 ng_sscop_param_type_info
86 static const struct ng_parse_struct_field ng_sscfu_getdefparam_type_info[] =
87 NG_SSCFU_GETDEFPARAM_INFO;
89 static const struct ng_parse_type ng_sscfu_getdefparam_type = {
90 &ng_parse_struct_type,
91 ng_sscfu_getdefparam_type_info
95 static const struct ng_cmdlist ng_sscfu_cmdlist[] = {
97 NGM_SSCFU_COOKIE,
98 NGM_SSCFU_GETDEFPARAM,
99 "getdefparam",
100 NULL,
101 &ng_sscfu_getdefparam_type
104 NGM_SSCFU_COOKIE,
105 NGM_SSCFU_ENABLE,
106 "enable",
107 NULL,
108 NULL
111 NGM_SSCFU_COOKIE,
112 NGM_SSCFU_DISABLE,
113 "disable",
114 NULL,
115 NULL
118 NGM_SSCFU_COOKIE,
119 NGM_SSCFU_GETDEBUG,
120 "getdebug",
121 NULL,
122 &ng_parse_hint32_type
125 NGM_SSCFU_COOKIE,
126 NGM_SSCFU_SETDEBUG,
127 "setdebug",
128 &ng_parse_hint32_type,
129 NULL
132 NGM_SSCFU_COOKIE,
133 NGM_SSCFU_GETSTATE,
134 "getstate",
135 NULL,
136 &ng_parse_uint32_type
138 { 0 }
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,
166 struct mbuf *);
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 *, ...)
171 __printflike(3, 4);
173 static const struct sscfu_funcs sscfu_funcs = {
174 sscfu_send_upper,
175 sscfu_send_lower,
176 sscfu_window,
177 sscfu_verbose
180 /************************************************************/
182 * CONTROL MESSAGES
184 static int
185 text_status(node_p node, struct priv *priv, char *arg, u_int len)
187 struct sbuf sbuf;
189 sbuf_new(&sbuf, arg, len, 0);
191 if (priv->upper)
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)));
196 else
197 sbuf_printf(&sbuf, "upper hook: <not connected>\n");
199 if (priv->lower)
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)));
204 else
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)));
211 sbuf_finish(&sbuf);
212 return (sbuf_len(&sbuf));
215 static int
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;
220 struct ng_mesg *msg;
221 int error = 0;
223 NGI_GET_MSG(item, msg);
225 switch (msg->header.typecookie) {
227 case NGM_GENERIC_COOKIE:
228 switch (msg->header.cmd) {
230 case NGM_TEXT_STATUS:
231 NG_MKRESPONSE(resp, msg, NG_TEXTRESPONSE, M_WAITOK | M_NULLOK);
232 if (resp == NULL) {
233 error = ENOMEM;
234 break;
236 resp->header.arglen = text_status(node, priv,
237 (char *)resp->data, resp->header.arglen) + 1;
238 break;
240 default:
241 error = EINVAL;
242 break;
244 break;
246 case NGM_SSCFU_COOKIE:
247 switch (msg->header.cmd) {
249 case NGM_SSCFU_GETDEFPARAM:
251 struct ng_sscfu_getdefparam *p;
253 if (msg->header.arglen != 0) {
254 error = EINVAL;
255 break;
257 NG_MKRESPONSE(resp, msg, sizeof(*p), M_WAITOK | M_NULLOK);
258 if (resp == NULL) {
259 error = ENOMEM;
260 break;
262 p = (struct ng_sscfu_getdefparam *)resp->data;
263 p->mask = sscfu_getdefparam(&p->param);
264 break;
267 case NGM_SSCFU_ENABLE:
268 if (msg->header.arglen != 0) {
269 error = EINVAL;
270 break;
272 if (priv->enabled) {
273 error = EISCONN;
274 break;
276 priv->enabled = 1;
277 break;
279 case NGM_SSCFU_DISABLE:
280 if (msg->header.arglen != 0) {
281 error = EINVAL;
282 break;
284 if (!priv->enabled) {
285 error = ENOTCONN;
286 break;
288 priv->enabled = 0;
289 sscfu_reset(priv->sscf);
290 break;
292 case NGM_SSCFU_GETSTATE:
293 if (msg->header.arglen != 0) {
294 error = EINVAL;
295 break;
297 NG_MKRESPONSE(resp, msg, sizeof(uint32_t), M_WAITOK | M_NULLOK);
298 if(resp == NULL) {
299 error = ENOMEM;
300 break;
302 *(uint32_t *)resp->data =
303 priv->enabled ? (sscfu_getstate(priv->sscf) + 1)
304 : 0;
305 break;
307 case NGM_SSCFU_GETDEBUG:
308 if (msg->header.arglen != 0) {
309 error = EINVAL;
310 break;
312 NG_MKRESPONSE(resp, msg, sizeof(uint32_t), M_WAITOK | M_NULLOK);
313 if(resp == NULL) {
314 error = ENOMEM;
315 break;
317 *(uint32_t *)resp->data = sscfu_getdebug(priv->sscf);
318 break;
320 case NGM_SSCFU_SETDEBUG:
321 if (msg->header.arglen != sizeof(uint32_t)) {
322 error = EINVAL;
323 break;
325 sscfu_setdebug(priv->sscf, *(uint32_t *)msg->data);
326 break;
328 default:
329 error = EINVAL;
330 break;
332 break;
334 default:
335 error = EINVAL;
336 break;
339 NG_RESPOND_MSG(error, node, item, resp);
340 NG_FREE_MSG(msg);
342 return (error);
345 /************************************************************/
347 * HOOK MANAGEMENT
349 static int
350 ng_sscfu_newhook(node_p node, hook_p hook, const char *name)
352 struct priv *priv = NG_NODE_PRIVATE(node);
354 if (strcmp(name, "upper") == 0)
355 priv->upper = hook;
356 else if (strcmp(name, "lower") == 0) {
357 priv->lower = hook;
358 NG_HOOK_SET_RCVDATA(hook, ng_sscfu_rcvlower);
359 } else
360 return (EINVAL);
361 return (0);
364 static int
365 ng_sscfu_disconnect(hook_p hook)
367 node_p node = NG_HOOK_NODE(hook);
368 struct priv *priv = NG_NODE_PRIVATE(node);
370 if (hook == priv->upper)
371 priv->upper = NULL;
372 else if (hook == priv->lower)
373 priv->lower = NULL;
374 else {
375 log(LOG_ERR, "bogus hook");
376 return (EINVAL);
379 if (NG_NODE_NUMHOOKS(node) == 0) {
380 if (NG_NODE_IS_VALID(node))
381 ng_rmnode_self(node);
382 } else {
384 * Because there are no timeouts reset the protocol
385 * if the lower layer is disconnected.
387 if (priv->lower == NULL &&
388 priv->enabled &&
389 sscfu_getstate(priv->sscf) != SSCFU_RELEASED)
390 sscfu_reset(priv->sscf);
392 return (0);
395 /************************************************************/
397 * DATA
399 static int
400 ng_sscfu_rcvupper(hook_p hook, item_p item)
402 node_p node = NG_HOOK_NODE(hook);
403 struct priv *priv = NG_NODE_PRIVATE(node);
404 struct mbuf *m;
405 struct sscfu_arg a;
407 if (!priv->enabled || priv->lower == NULL) {
408 NG_FREE_ITEM(item);
409 return (0);
412 NGI_GET_M(item, m);
413 NG_FREE_ITEM(item);
415 if (!(m->m_flags & M_PKTHDR)) {
416 printf("no pkthdr\n");
417 m_freem(m);
418 return (EINVAL);
420 if (m->m_len < (int)sizeof(a) && (m = m_pullup(m, sizeof(a))) == NULL)
421 return (ENOMEM);
422 bcopy((caddr_t)mtod(m, struct sscfu_arg *), &a, sizeof(a));
423 m_adj(m, sizeof(a));
425 return (sscfu_saalsig(priv->sscf, a.sig, m));
428 static void
429 sscfu_send_upper(struct sscfu *sscf, void *p, enum saal_sig sig, struct mbuf *m)
431 node_p node = (node_p)p;
432 struct priv *priv = NG_NODE_PRIVATE(node);
433 int error;
434 struct sscfu_arg *a;
436 if (priv->upper == NULL) {
437 if (m != NULL)
438 m_freem(m);
439 return;
441 if (m == NULL) {
442 MGETHDR(m, MB_DONTWAIT, MT_DATA);
443 if (m == NULL)
444 return;
445 m->m_len = sizeof(struct sscfu_arg);
446 m->m_pkthdr.len = m->m_len;
447 } else {
448 M_PREPEND(m, sizeof(struct sscfu_arg), MB_DONTWAIT);
449 if (m == NULL)
450 return;
452 a = mtod(m, struct sscfu_arg *);
453 a->sig = sig;
455 NG_SEND_DATA_ONLY(error, priv->upper, m);
458 static int
459 ng_sscfu_rcvlower(hook_p hook, item_p item)
461 node_p node = NG_HOOK_NODE(hook);
462 struct priv *priv = NG_NODE_PRIVATE(node);
463 struct mbuf *m;
464 struct sscop_arg a;
466 if (!priv->enabled || priv->upper == NULL) {
467 NG_FREE_ITEM(item);
468 return (0);
471 NGI_GET_M(item, m);
472 NG_FREE_ITEM(item);
474 if (!(m->m_flags & M_PKTHDR)) {
475 printf("no pkthdr\n");
476 m_freem(m);
477 return (EINVAL);
481 * Strip of the SSCOP header.
483 if (m->m_len < (int)sizeof(a) && (m = m_pullup(m, sizeof(a))) == NULL)
484 return (ENOMEM);
485 bcopy((caddr_t)mtod(m, struct sscop_arg *), &a, sizeof(a));
486 m_adj(m, sizeof(a));
488 sscfu_input(priv->sscf, a.sig, m, a.arg);
490 return (0);
493 static void
494 sscfu_send_lower(struct sscfu *sscf, void *p, enum sscop_aasig sig,
495 struct mbuf *m, u_int arg)
497 node_p node = (node_p)p;
498 struct priv *priv = NG_NODE_PRIVATE(node);
499 int error;
500 struct sscop_arg *a;
502 if (priv->lower == NULL) {
503 if (m != NULL)
504 m_freem(m);
505 return;
507 if (m == NULL) {
508 MGETHDR(m, MB_DONTWAIT, MT_DATA);
509 if (m == NULL)
510 return;
511 m->m_len = sizeof(struct sscop_arg);
512 m->m_pkthdr.len = m->m_len;
513 } else {
514 M_PREPEND(m, sizeof(struct sscop_arg), MB_DONTWAIT);
515 if (m == NULL)
516 return;
518 a = mtod(m, struct sscop_arg *);
519 a->sig = sig;
520 a->arg = arg;
522 NG_SEND_DATA_ONLY(error, priv->lower, m);
526 * Window is handled by ng_sscop so make this a NOP.
528 static void
529 sscfu_window(struct sscfu *sscfu, void *arg, u_int w)
533 /************************************************************/
535 * NODE MANAGEMENT
537 static int
538 ng_sscfu_constructor(node_p node)
540 struct priv *priv;
542 if ((priv = kmalloc(sizeof(*priv), M_NG_SSCFU, M_WAITOK | M_NULLOK | M_ZERO)) == NULL)
543 return (ENOMEM);
545 if ((priv->sscf = sscfu_create(node, &sscfu_funcs)) == NULL) {
546 kfree(priv, M_NG_SSCFU);
547 return (ENOMEM);
550 NG_NODE_SET_PRIVATE(node, priv);
552 return (0);
555 static int
556 ng_sscfu_shutdown(node_p node)
558 struct priv *priv = NG_NODE_PRIVATE(node);
560 sscfu_destroy(priv->sscf);
562 kfree(priv, M_NG_SSCFU);
563 NG_NODE_SET_PRIVATE(node, NULL);
565 NG_NODE_UNREF(node);
567 return (0);
570 static void
571 sscfu_verbose(struct sscfu *sscfu, void *arg, const char *fmt, ...)
573 va_list ap;
575 va_start(ap, fmt);
576 printf("sscfu(%p): ", sscfu);
577 vprintf(fmt, ap);
578 va_end(ap);
579 printf("\n");
582 /************************************************************/
584 * INITIALISATION
587 * Loading and unloading of node type
589 static int
590 ng_sscfu_mod_event(module_t mod, int event, void *data)
592 int s;
593 int error = 0;
595 s = splnet();
596 switch (event) {
598 case MOD_LOAD:
599 break;
601 case MOD_UNLOAD:
602 break;
604 default:
605 error = EOPNOTSUPP;
606 break;
608 splx(s);
609 return (error);