2 * Copyright (c) 2001-2003
3 * Fraunhofer Institute for Open Communication Systems (FhG Fokus).
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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
27 * Author: Hartmut Brandt <harti@freebsd.org>
31 * Netgraph module to connect NATM interfaces to netgraph.
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 <sys/ioccom.h>
48 #include <sys/sysctl.h>
51 #include <net/if_types.h>
52 #include <net/if_arp.h>
53 #include <net/if_var.h>
54 #include <net/if_media.h>
55 #include <net/if_atm.h>
58 #include <netgraph/ng_message.h>
59 #include <netgraph/netgraph.h>
60 #include <netgraph/ng_parse.h>
61 #include <netgraph/atm/ng_atm.h>
64 * Hooks in the NATM code
66 extern void (*ng_atm_attach_p)(struct ifnet *);
67 extern void (*ng_atm_detach_p)(struct ifnet *);
68 extern int (*ng_atm_output_p)(struct ifnet *, struct mbuf **);
69 extern void (*ng_atm_input_p)(struct ifnet *, struct mbuf **,
70 struct atm_pseudohdr *, void *);
71 extern void (*ng_atm_input_orphan_p)(struct ifnet *, struct mbuf *,
72 struct atm_pseudohdr *, void *);
73 extern void (*ng_atm_event_p)(struct ifnet *, uint32_t, void *);
78 static SYSCTL_NODE(_net_graph, OID_AUTO, atm, CTLFLAG_RW, 0,
82 static int allow_shutdown;
84 SYSCTL_INT(_net_graph_atm, OID_AUTO, allow_shutdown, CTLFLAG_RW,
85 &allow_shutdown, 0, "allow ng_atm nodes to shutdown");
92 uint16_t vpi; /* VPI of this hook */
93 uint16_t vci; /* VCI of this hook, 0 if none */
94 uint32_t flags; /* private flags */
95 hook_p hook; /* the connected hook */
97 LIST_ENTRY(ngvcc) link;
99 #define VCC_OPEN 0x0001 /* open */
105 struct ifnet *ifp; /* the ATM interface */
106 hook_p input; /* raw input hook */
107 hook_p orphans; /* packets to nowhere */
108 hook_p output; /* catch output packets */
109 hook_p manage; /* has also entry in vccs */
112 uint64_t out_packets;
115 LIST_HEAD(, ngvcc) vccs;
119 * Parse ifstate state
121 static const struct ng_parse_struct_field ng_atm_if_change_info[] =
122 NGM_ATM_IF_CHANGE_INFO;
123 static const struct ng_parse_type ng_atm_if_change_type = {
124 &ng_parse_struct_type,
125 &ng_atm_if_change_info
129 * Parse vcc state change
131 static const struct ng_parse_struct_field ng_atm_vcc_change_info[] =
132 NGM_ATM_VCC_CHANGE_INFO;
133 static const struct ng_parse_type ng_atm_vcc_change_type = {
134 &ng_parse_struct_type,
135 &ng_atm_vcc_change_info
141 static const struct ng_parse_struct_field ng_atm_acr_change_info[] =
142 NGM_ATM_ACR_CHANGE_INFO;
143 static const struct ng_parse_type ng_atm_acr_change_type = {
144 &ng_parse_struct_type,
145 &ng_atm_acr_change_info
149 * Parse the configuration structure ng_atm_config
151 static const struct ng_parse_struct_field ng_atm_config_type_info[] =
154 static const struct ng_parse_type ng_atm_config_type = {
155 &ng_parse_struct_type,
156 &ng_atm_config_type_info
160 * Parse a single vcc structure and a variable array of these ng_atm_vccs
162 static const struct ng_parse_struct_field ng_atm_tparam_type_info[] =
164 static const struct ng_parse_type ng_atm_tparam_type = {
165 &ng_parse_struct_type,
166 &ng_atm_tparam_type_info
168 static const struct ng_parse_struct_field ng_atm_vcc_type_info[] =
170 static const struct ng_parse_type ng_atm_vcc_type = {
171 &ng_parse_struct_type,
172 &ng_atm_vcc_type_info
177 ng_atm_vccarray_getlen(const struct ng_parse_type *type,
178 const u_char *start, const u_char *buf)
180 const struct atmio_vcctable *vp;
182 vp = (const struct atmio_vcctable *)
183 (buf - offsetof(struct atmio_vcctable, vccs));
187 static const struct ng_parse_array_info ng_atm_vccarray_info =
188 NGM_ATM_VCCARRAY_INFO;
189 static const struct ng_parse_type ng_atm_vccarray_type = {
190 &ng_parse_array_type,
191 &ng_atm_vccarray_info
195 static const struct ng_parse_struct_field ng_atm_vcctable_type_info[] =
196 NGM_ATM_VCCTABLE_INFO;
198 static const struct ng_parse_type ng_atm_vcctable_type = {
199 &ng_parse_struct_type,
200 &ng_atm_vcctable_type_info
204 * Parse CPCS INIT structure ng_atm_cpcs_init
206 static const struct ng_parse_struct_field ng_atm_cpcs_init_type_info[] =
207 NGM_ATM_CPCS_INIT_INFO;
209 static const struct ng_parse_type ng_atm_cpcs_init_type = {
210 &ng_parse_struct_type,
211 &ng_atm_cpcs_init_type_info
215 * Parse CPCS TERM structure ng_atm_cpcs_term
217 static const struct ng_parse_struct_field ng_atm_cpcs_term_type_info[] =
218 NGM_ATM_CPCS_TERM_INFO;
220 static const struct ng_parse_type ng_atm_cpcs_term_type = {
221 &ng_parse_struct_type,
222 &ng_atm_cpcs_term_type_info
226 * Parse statistic struct
228 static const struct ng_parse_struct_field ng_atm_stats_type_info[] =
231 static const struct ng_parse_type ng_atm_stats_type = {
232 &ng_parse_struct_type,
233 &ng_atm_stats_type_info
236 static const struct ng_cmdlist ng_atm_cmdlist[] = {
242 &ng_parse_string_type
256 &ng_atm_vcctable_type
262 &ng_atm_cpcs_init_type,
269 &ng_atm_cpcs_term_type,
276 &ng_parse_hookbuf_type,
299 &ng_atm_if_change_type,
300 &ng_atm_if_change_type,
306 &ng_atm_vcc_change_type,
307 &ng_atm_vcc_change_type,
313 &ng_atm_acr_change_type,
314 &ng_atm_acr_change_type,
319 static int ng_atm_mod_event(module_t, int, void *);
321 static ng_constructor_t ng_atm_constructor;
322 static ng_shutdown_t ng_atm_shutdown;
323 static ng_rcvmsg_t ng_atm_rcvmsg;
324 static ng_newhook_t ng_atm_newhook;
325 static ng_connect_t ng_atm_connect;
326 static ng_disconnect_t ng_atm_disconnect;
327 static ng_rcvdata_t ng_atm_rcvdata;
328 static ng_rcvdata_t ng_atm_rcvdrop;
330 static struct ng_type ng_atm_typestruct = {
331 .version = NG_ABI_VERSION,
332 .name = NG_ATM_NODE_TYPE,
333 .mod_event = ng_atm_mod_event,
334 .constructor = ng_atm_constructor,
335 .rcvmsg = ng_atm_rcvmsg,
336 .shutdown = ng_atm_shutdown,
337 .newhook = ng_atm_newhook,
338 .connect = ng_atm_connect,
339 .rcvdata = ng_atm_rcvdata,
340 .disconnect = ng_atm_disconnect,
341 .cmdlist = ng_atm_cmdlist,
343 NETGRAPH_INIT(atm, &ng_atm_typestruct);
345 static const struct {
348 } atmmedia[] = IFM_SUBTYPE_ATM_DESCRIPTIONS;
351 #define IFP2NG(IFP) ((node_p)((struct ifatm *)(IFP)->if_softc)->ngpriv)
352 #define IFP2NG_SET(IFP, val) (((struct ifatm *)(IFP)->if_softc)->ngpriv = (val))
354 #define IFFLAGS "\020\001UP\002BROADCAST\003DEBUG\004LOOPBACK" \
355 "\005POINTOPOINT\006SMART\007RUNNING\010NOARP" \
356 "\011PROMISC\012ALLMULTI\013OACTIVE\014SIMPLEX" \
357 "\015LINK0\016LINK1\017LINK2\020MULTICAST"
360 /************************************************************/
365 * A packet is received from an interface.
366 * If we have an input hook, prepend the pseudoheader to the data and
367 * deliver it out to that hook. If not, look whether it is destined for
368 * use. If so locate the appropriate hook, deliver the packet without the
369 * header and we are done. If it is not for us, leave it alone.
372 ng_atm_input(struct ifnet *ifp, struct mbuf **mp,
373 struct atm_pseudohdr *ah, void *rxhand)
375 node_p node = IFP2NG(ifp);
377 const struct ngvcc *vcc;
382 priv = NG_NODE_PRIVATE(node);
383 if (priv->input != NULL) {
385 * Prepend the atm_pseudoheader.
387 M_PREPEND(*mp, sizeof(*ah), M_NOWAIT);
390 memcpy(mtod(*mp, struct atm_pseudohdr *), ah, sizeof(*ah));
391 NG_SEND_DATA_ONLY(error, priv->input, *mp);
397 printf("%s: error=%d\n", __func__, error);
403 if ((ATM_PH_FLAGS(ah) & ATMIO_FLAG_NG) == 0)
406 vcc = (struct ngvcc *)rxhand;
408 NG_SEND_DATA_ONLY(error, vcc->hook, *mp);
414 printf("%s: error=%d\n", __func__, error);
421 * ATM packet is about to be output. The atm_pseudohdr is already prepended.
422 * If the hook is set, reroute the packet to the hook.
425 ng_atm_output(struct ifnet *ifp, struct mbuf **mp)
427 const node_p node = IFP2NG(ifp);
428 const struct priv *priv;
433 priv = NG_NODE_PRIVATE(node);
435 NG_SEND_DATA_ONLY(error, priv->output, *mp);
443 * Well, this doesn't make much sense for ATM.
446 ng_atm_input_orphans(struct ifnet *ifp, struct mbuf *m,
447 struct atm_pseudohdr *ah, void *rxhand)
449 node_p node = IFP2NG(ifp);
457 priv = NG_NODE_PRIVATE(node);
458 if (priv->orphans == NULL) {
463 * Prepend the atm_pseudoheader.
465 M_PREPEND(m, sizeof(*ah), M_NOWAIT);
468 memcpy(mtod(m, struct atm_pseudohdr *), ah, sizeof(*ah));
469 NG_SEND_DATA_ONLY(error, priv->orphans, m);
475 printf("%s: error=%d\n", __func__, error);
480 /************************************************************/
485 ng_atm_rcvdata(hook_p hook, item_p item)
487 node_p node = NG_HOOK_NODE(hook);
488 struct priv *priv = NG_NODE_PRIVATE(node);
489 const struct ngvcc *vcc = NG_HOOK_PRIVATE(hook);
491 struct atm_pseudohdr *aph;
503 * Prepend pseudo-hdr. Drivers don't care about the flags.
505 M_PREPEND(m, sizeof(*aph), M_NOWAIT);
510 aph = mtod(m, struct atm_pseudohdr *);
511 ATM_PH_VPI(aph) = vcc->vpi;
512 ATM_PH_SETVCI(aph, vcc->vci);
513 ATM_PH_FLAGS(aph) = 0;
515 if ((error = atm_output(priv->ifp, m, NULL, NULL)) == 0)
523 ng_atm_rcvdrop(hook_p hook, item_p item)
530 /************************************************************
535 ng_atm_event_func(node_p node, hook_p hook, void *arg, int event)
537 const struct priv *priv = NG_NODE_PRIVATE(node);
539 struct ng_mesg *mesg;
544 case ATMEV_FLOW_CONTROL:
546 struct atmev_flow_control *ev = arg;
547 struct ngm_queue_state *qstate;
549 /* find the connection */
550 LIST_FOREACH(vcc, &priv->vccs, link)
551 if (vcc->vci == ev->vci && vcc->vpi == ev->vpi)
556 /* convert into a flow control message */
557 NG_MKMESSAGE(mesg, NGM_FLOW_COOKIE,
558 ev->busy ? NGM_HIGH_WATER_PASSED : NGM_LOW_WATER_PASSED,
559 sizeof(struct ngm_queue_state), M_NOWAIT);
562 qstate = (struct ngm_queue_state *)mesg->data;
564 /* XXX have to figure out how to get that info */
566 NG_SEND_MSG_HOOK(error, node, mesg, vcc->hook, 0);
570 case ATMEV_VCC_CHANGED:
572 struct atmev_vcc_changed *ev = arg;
573 struct ngm_atm_vcc_change *chg;
575 if (priv->manage == NULL)
577 NG_MKMESSAGE(mesg, NGM_ATM_COOKIE, NGM_ATM_VCC_CHANGE,
578 sizeof(struct ngm_atm_vcc_change), M_NOWAIT);
581 chg = (struct ngm_atm_vcc_change *)mesg->data;
584 chg->state = (ev->up != 0);
585 chg->node = NG_NODE_ID(node);
586 NG_SEND_MSG_HOOK(error, node, mesg, priv->manage, 0);
590 case ATMEV_IFSTATE_CHANGED:
592 struct atmev_ifstate_changed *ev = arg;
593 struct ngm_atm_if_change *chg;
595 if (priv->manage == NULL)
597 NG_MKMESSAGE(mesg, NGM_ATM_COOKIE, NGM_ATM_IF_CHANGE,
598 sizeof(struct ngm_atm_if_change), M_NOWAIT);
601 chg = (struct ngm_atm_if_change *)mesg->data;
602 chg->carrier = (ev->carrier != 0);
603 chg->running = (ev->running != 0);
604 chg->node = NG_NODE_ID(node);
605 NG_SEND_MSG_HOOK(error, node, mesg, priv->manage, 0);
609 case ATMEV_ACR_CHANGED:
611 struct atmev_acr_changed *ev = arg;
612 struct ngm_atm_acr_change *acr;
614 /* find the connection */
615 LIST_FOREACH(vcc, &priv->vccs, link)
616 if (vcc->vci == ev->vci && vcc->vpi == ev->vpi)
621 /* convert into a flow control message */
622 NG_MKMESSAGE(mesg, NGM_ATM_COOKIE, NGM_ATM_ACR_CHANGE,
623 sizeof(struct ngm_atm_acr_change), M_NOWAIT);
626 acr = (struct ngm_atm_acr_change *)mesg->data;
627 acr->node = NG_NODE_ID(node);
632 NG_SEND_MSG_HOOK(error, node, mesg, vcc->hook, 0);
639 * Use send_fn to get the right lock
642 ng_atm_event(struct ifnet *ifp, uint32_t event, void *arg)
644 const node_p node = IFP2NG(ifp);
647 /* may happen during attach/detach */
648 (void)ng_send_fn(node, NULL, ng_atm_event_func, arg, event);
651 /************************************************************
656 * Open a channel for the user
659 ng_atm_cpcs_init(node_p node, const struct ngm_atm_cpcs_init *arg)
661 struct priv *priv = NG_NODE_PRIVATE(node);
662 const struct ifatm_mib *mib;
664 struct atmio_openvcc data;
667 if(priv->ifp->if_ioctl == NULL)
670 mib = (const struct ifatm_mib *)(priv->ifp->if_linkmib);
672 LIST_FOREACH(vcc, &priv->vccs, link)
673 if (strcmp(arg->name, NG_HOOK_NAME(vcc->hook)) == 0)
677 if (vcc->flags & VCC_OPEN)
681 * Check user arguments and construct ioctl argument
683 memset(&data, 0, sizeof(data));
687 switch (data.param.aal = arg->aal) {
701 data.param.vpi = arg->vpi;
703 /* allow 0.0 as catch all receive channel */
704 if (arg->vci == 0 && (arg->vpi != 0 || !(arg->flags & ATMIO_FLAG_NOTX)))
706 data.param.vci = arg->vci;
708 data.param.tparam.pcr = arg->pcr;
710 if (arg->mcr > arg->pcr)
712 data.param.tparam.mcr = arg->mcr;
714 if (!(arg->flags & ATMIO_FLAG_NOTX)) {
716 data.param.tmtu = priv->ifp->if_mtu;
718 data.param.tmtu = arg->tmtu;
721 if (!(arg->flags & ATMIO_FLAG_NORX)) {
723 data.param.rmtu = priv->ifp->if_mtu;
725 data.param.rmtu = arg->rmtu;
729 switch (data.param.traffic = arg->traffic) {
731 case ATMIO_TRAFFIC_UBR:
732 case ATMIO_TRAFFIC_CBR:
735 case ATMIO_TRAFFIC_VBR:
736 if (arg->scr > arg->pcr)
738 data.param.tparam.scr = arg->scr;
740 if (arg->mbs > (1 << 24))
742 data.param.tparam.mbs = arg->mbs;
745 case ATMIO_TRAFFIC_ABR:
746 if (arg->icr > arg->pcr || arg->icr < arg->mcr)
748 data.param.tparam.icr = arg->icr;
750 if (arg->tbe == 0 || arg->tbe > (1 << 24))
752 data.param.tparam.tbe = arg->tbe;
756 data.param.tparam.nrm = arg->nrm;
760 data.param.tparam.trm = arg->trm;
762 if (arg->adtf > 0x3ff)
764 data.param.tparam.adtf = arg->adtf;
768 data.param.tparam.rif = arg->rif;
772 data.param.tparam.rdf = arg->rdf;
776 data.param.tparam.cdf = arg->cdf;
784 if ((arg->flags & ATMIO_FLAG_NORX) && (arg->flags & ATMIO_FLAG_NOTX))
787 data.param.flags = arg->flags & ~(ATM_PH_AAL5 | ATM_PH_LLCSNAP);
788 data.param.flags |= ATMIO_FLAG_NG;
790 err = (*priv->ifp->if_ioctl)(priv->ifp, SIOCATMOPENVCC, (caddr_t)&data);
793 vcc->vci = data.param.vci;
794 vcc->vpi = data.param.vpi;
795 vcc->flags = VCC_OPEN;
802 * Issue the close command to the driver
805 cpcs_term(const struct priv *priv, u_int vpi, u_int vci)
807 struct atmio_closevcc data;
809 if (priv->ifp->if_ioctl == NULL)
815 return ((*priv->ifp->if_ioctl)(priv->ifp,
816 SIOCATMCLOSEVCC, (caddr_t)&data));
821 * Close a channel by request of the user
824 ng_atm_cpcs_term(node_p node, const struct ngm_atm_cpcs_term *arg)
826 struct priv *priv = NG_NODE_PRIVATE(node);
830 LIST_FOREACH(vcc, &priv->vccs, link)
831 if(strcmp(arg->name, NG_HOOK_NAME(vcc->hook)) == 0)
835 if (!(vcc->flags & VCC_OPEN))
838 error = cpcs_term(priv, vcc->vpi, vcc->vci);
847 /************************************************************/
853 * Produce a textual description of the current status
856 text_status(node_p node, char *arg, u_int len)
858 const struct priv *priv = NG_NODE_PRIVATE(node);
859 const struct ifatm_mib *mib;
863 static const struct {
870 mib = (const struct ifatm_mib *)(priv->ifp->if_linkmib);
872 sbuf_new(&sbuf, arg, len, SBUF_FIXEDLEN);
873 sbuf_printf(&sbuf, "interface: %s\n", priv->ifp->if_xname);
875 if (mib->device >= nitems(devices))
876 sbuf_printf(&sbuf, "device=unknown\nvendor=unknown\n");
878 sbuf_printf(&sbuf, "device=%s\nvendor=%s\n",
879 devices[mib->device].name, devices[mib->device].vendor);
881 for (i = 0; atmmedia[i].name; i++)
882 if(mib->media == atmmedia[i].media) {
883 sbuf_printf(&sbuf, "media=%s\n", atmmedia[i].name);
886 if(atmmedia[i].name == NULL)
887 sbuf_printf(&sbuf, "media=unknown\n");
889 sbuf_printf(&sbuf, "serial=%u esi=%6D hardware=%u software=%u\n",
890 mib->serial, mib->esi, ":", mib->hw_version, mib->sw_version);
891 sbuf_printf(&sbuf, "pcr=%u vpi_bits=%u vci_bits=%u max_vpcs=%u "
892 "max_vccs=%u\n", mib->pcr, mib->vpi_bits, mib->vci_bits,
893 mib->max_vpcs, mib->max_vccs);
894 sbuf_printf(&sbuf, "ifflags=%b\n", priv->ifp->if_flags, IFFLAGS);
898 return (sbuf_len(&sbuf));
902 * Get control message
905 ng_atm_rcvmsg(node_p node, item_p item, hook_p lasthook)
907 const struct priv *priv = NG_NODE_PRIVATE(node);
908 struct ng_mesg *resp = NULL;
910 struct ifatm_mib *mib = (struct ifatm_mib *)(priv->ifp->if_linkmib);
913 NGI_GET_MSG(item, msg);
915 switch (msg->header.typecookie) {
917 case NGM_GENERIC_COOKIE:
918 switch (msg->header.cmd) {
920 case NGM_TEXT_STATUS:
921 NG_MKRESPONSE(resp, msg, NG_TEXTRESPONSE, M_NOWAIT);
927 resp->header.arglen = text_status(node,
928 (char *)resp->data, resp->header.arglen) + 1;
938 switch (msg->header.cmd) {
940 case NGM_ATM_GET_IFNAME:
941 NG_MKRESPONSE(resp, msg, IFNAMSIZ, M_NOWAIT);
946 strlcpy(resp->data, priv->ifp->if_xname, IFNAMSIZ);
949 case NGM_ATM_GET_CONFIG:
951 struct ngm_atm_config *config;
953 NG_MKRESPONSE(resp, msg, sizeof(*config), M_NOWAIT);
958 config = (struct ngm_atm_config *)resp->data;
959 config->pcr = mib->pcr;
960 config->vpi_bits = mib->vpi_bits;
961 config->vci_bits = mib->vci_bits;
962 config->max_vpcs = mib->max_vpcs;
963 config->max_vccs = mib->max_vccs;
967 case NGM_ATM_GET_VCCS:
969 struct atmio_vcctable *vccs;
972 if (priv->ifp->if_ioctl == NULL) {
976 error = (*priv->ifp->if_ioctl)(priv->ifp,
977 SIOCATMGETVCCS, (caddr_t)&vccs);
981 len = sizeof(*vccs) +
982 vccs->count * sizeof(vccs->vccs[0]);
983 NG_MKRESPONSE(resp, msg, len, M_NOWAIT);
986 free(vccs, M_DEVBUF);
990 (void)memcpy(resp->data, vccs, len);
991 free(vccs, M_DEVBUF);
996 case NGM_ATM_GET_VCC:
998 char hook[NG_HOOKSIZ];
999 struct atmio_vcctable *vccs;
1003 if (priv->ifp->if_ioctl == NULL) {
1007 if (msg->header.arglen != NG_HOOKSIZ) {
1011 strncpy(hook, msg->data, NG_HOOKSIZ);
1012 hook[NG_HOOKSIZ - 1] = '\0';
1013 LIST_FOREACH(vcc, &priv->vccs, link)
1014 if (strcmp(NG_HOOK_NAME(vcc->hook), hook) == 0)
1020 error = (*priv->ifp->if_ioctl)(priv->ifp,
1021 SIOCATMGETVCCS, (caddr_t)&vccs);
1025 for (i = 0; i < vccs->count; i++)
1026 if (vccs->vccs[i].vpi == vcc->vpi &&
1027 vccs->vccs[i].vci == vcc->vci)
1029 if (i == vccs->count) {
1031 free(vccs, M_DEVBUF);
1035 NG_MKRESPONSE(resp, msg, sizeof(vccs->vccs[0]),
1039 free(vccs, M_DEVBUF);
1043 *(struct atmio_vcc *)resp->data = vccs->vccs[i];
1044 free(vccs, M_DEVBUF);
1048 case NGM_ATM_GET_VCCID:
1050 struct atmio_vcc *arg;
1051 struct atmio_vcctable *vccs;
1054 if (priv->ifp->if_ioctl == NULL) {
1058 if (msg->header.arglen != sizeof(*arg)) {
1062 arg = (struct atmio_vcc *)msg->data;
1064 error = (*priv->ifp->if_ioctl)(priv->ifp,
1065 SIOCATMGETVCCS, (caddr_t)&vccs);
1069 for (i = 0; i < vccs->count; i++)
1070 if (vccs->vccs[i].vpi == arg->vpi &&
1071 vccs->vccs[i].vci == arg->vci)
1073 if (i == vccs->count) {
1075 free(vccs, M_DEVBUF);
1079 NG_MKRESPONSE(resp, msg, sizeof(vccs->vccs[0]),
1083 free(vccs, M_DEVBUF);
1087 *(struct atmio_vcc *)resp->data = vccs->vccs[i];
1088 free(vccs, M_DEVBUF);
1092 case NGM_ATM_CPCS_INIT:
1093 if (msg->header.arglen !=
1094 sizeof(struct ngm_atm_cpcs_init)) {
1098 error = ng_atm_cpcs_init(node,
1099 (struct ngm_atm_cpcs_init *)msg->data);
1102 case NGM_ATM_CPCS_TERM:
1103 if (msg->header.arglen !=
1104 sizeof(struct ngm_atm_cpcs_term)) {
1108 error = ng_atm_cpcs_term(node,
1109 (struct ngm_atm_cpcs_term *)msg->data);
1112 case NGM_ATM_GET_STATS:
1114 struct ngm_atm_stats *p;
1116 if (msg->header.arglen != 0) {
1120 NG_MKRESPONSE(resp, msg, sizeof(*p), M_NOWAIT);
1125 p = (struct ngm_atm_stats *)resp->data;
1126 p->in_packets = priv->in_packets;
1127 p->out_packets = priv->out_packets;
1128 p->in_errors = priv->in_errors;
1129 p->out_errors = priv->out_errors;
1145 NG_RESPOND_MSG(error, node, item, resp);
1150 /************************************************************/
1156 * A new hook is create that will be connected to the node.
1157 * Check, whether the name is one of the predefined ones.
1158 * If not, create a new entry into the vcc list.
1161 ng_atm_newhook(node_p node, hook_p hook, const char *name)
1163 struct priv *priv = NG_NODE_PRIVATE(node);
1166 if (strcmp(name, "input") == 0) {
1168 NG_HOOK_SET_RCVDATA(hook, ng_atm_rcvdrop);
1171 if (strcmp(name, "output") == 0) {
1172 priv->output = hook;
1173 NG_HOOK_SET_RCVDATA(hook, ng_atm_rcvdrop);
1176 if (strcmp(name, "orphans") == 0) {
1177 priv->orphans = hook;
1178 NG_HOOK_SET_RCVDATA(hook, ng_atm_rcvdrop);
1183 * Allocate a new entry
1185 vcc = malloc(sizeof(*vcc), M_NETGRAPH, M_NOWAIT | M_ZERO);
1190 NG_HOOK_SET_PRIVATE(hook, vcc);
1192 LIST_INSERT_HEAD(&priv->vccs, vcc, link);
1194 if (strcmp(name, "manage") == 0)
1195 priv->manage = hook;
1201 * Connect. Set the peer to queuing.
1204 ng_atm_connect(hook_p hook)
1206 if (NG_HOOK_PRIVATE(hook) != NULL)
1207 NG_HOOK_FORCE_QUEUE(NG_HOOK_PEER(hook));
1216 ng_atm_disconnect(hook_p hook)
1218 struct priv *priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
1219 struct ngvcc *vcc = NG_HOOK_PRIVATE(hook);
1222 if (hook == priv->output) {
1223 priv->output = NULL;
1226 if (hook == priv->input) {
1230 if (hook == priv->orphans) {
1231 priv->orphans = NULL;
1234 log(LOG_ERR, "ng_atm: bad hook '%s'", NG_HOOK_NAME(hook));
1238 /* don't terminate if we are detaching from the interface */
1239 if ((vcc->flags & VCC_OPEN) && priv->ifp != NULL)
1240 (void)cpcs_term(priv, vcc->vpi, vcc->vci);
1242 NG_HOOK_SET_PRIVATE(hook, NULL);
1244 LIST_REMOVE(vcc, link);
1245 free(vcc, M_NETGRAPH);
1247 if (hook == priv->manage)
1248 priv->manage = NULL;
1253 /************************************************************/
1259 * ATM interface attached - create a node and name it like the interface.
1262 ng_atm_attach(struct ifnet *ifp)
1267 KASSERT(IFP2NG(ifp) == 0, ("%s: node alreay exists?", __func__));
1269 if (ng_make_node_common(&ng_atm_typestruct, &node) != 0) {
1270 log(LOG_ERR, "%s: can't create node for %s\n",
1271 __func__, ifp->if_xname);
1275 priv = malloc(sizeof(*priv), M_NETGRAPH, M_NOWAIT | M_ZERO);
1277 log(LOG_ERR, "%s: can't allocate memory for %s\n",
1278 __func__, ifp->if_xname);
1279 NG_NODE_UNREF(node);
1282 NG_NODE_SET_PRIVATE(node, priv);
1284 LIST_INIT(&priv->vccs);
1285 IFP2NG_SET(ifp, node);
1287 if (ng_name_node(node, ifp->if_xname) != 0) {
1288 log(LOG_WARNING, "%s: can't name node %s\n",
1289 __func__, ifp->if_xname);
1294 * ATM interface detached - destroy node.
1297 ng_atm_detach(struct ifnet *ifp)
1299 const node_p node = IFP2NG(ifp);
1305 NG_NODE_REALLY_DIE(node);
1307 priv = NG_NODE_PRIVATE(node);
1308 IFP2NG_SET(priv->ifp, NULL);
1311 ng_rmnode_self(node);
1315 * Shutdown the node. This is called from the shutdown message processing.
1318 ng_atm_shutdown(node_p node)
1320 struct priv *priv = NG_NODE_PRIVATE(node);
1322 if (node->nd_flags & NGF_REALLY_DIE) {
1324 * We are called from unloading the ATM driver. Really,
1325 * really need to shutdown this node. The ifp was
1326 * already handled in the detach routine.
1328 NG_NODE_SET_PRIVATE(node, NULL);
1329 free(priv, M_NETGRAPH);
1331 NG_NODE_UNREF(node);
1336 if (!allow_shutdown)
1337 NG_NODE_REVIVE(node); /* we persist */
1339 IFP2NG_SET(priv->ifp, NULL);
1340 NG_NODE_SET_PRIVATE(node, NULL);
1341 free(priv, M_NETGRAPH);
1342 NG_NODE_UNREF(node);
1346 * We are persistent - reinitialize.
1348 NG_NODE_REVIVE(node);
1354 * Nodes are constructed only via interface attaches.
1357 ng_atm_constructor(node_p nodep)
1362 /************************************************************/
1367 * Loading and unloading of node type
1369 * The assignments to the globals for the hooks should be ok without
1370 * a special hook. The use pattern is generally: check that the pointer
1371 * is not NULL, call the function. In the attach case this is no problem.
1372 * In the detach case we can detach only when no ATM node exists. That
1373 * means that there is no ATM interface anymore. So we are sure that
1374 * we are not in the code path in if_atmsubr.c. To prevent someone
1375 * from adding an interface after we have started to unload the node, we
1376 * take the iflist lock so an if_attach will be blocked until we are done.
1377 * XXX: perhaps the function pointers should be 'volatile' for this to work
1381 ng_atm_mod_event(module_t mod, int event, void *data)
1383 VNET_ITERATOR_DECL(vnet_iter);
1391 * Register function hooks
1393 if (ng_atm_attach_p != NULL) {
1399 ng_atm_attach_p = ng_atm_attach;
1400 ng_atm_detach_p = ng_atm_detach;
1401 ng_atm_output_p = ng_atm_output;
1402 ng_atm_input_p = ng_atm_input;
1403 ng_atm_input_orphan_p = ng_atm_input_orphans;
1404 ng_atm_event_p = ng_atm_event;
1406 /* Create nodes for existing ATM interfaces */
1408 VNET_FOREACH(vnet_iter) {
1409 CURVNET_SET_QUIET(vnet_iter);
1410 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1411 if (ifp->if_type == IFT_ATM)
1416 VNET_LIST_RUNLOCK();
1423 ng_atm_attach_p = NULL;
1424 ng_atm_detach_p = NULL;
1425 ng_atm_output_p = NULL;
1426 ng_atm_input_p = NULL;
1427 ng_atm_input_orphan_p = NULL;
1428 ng_atm_event_p = NULL;
1431 VNET_FOREACH(vnet_iter) {
1432 CURVNET_SET_QUIET(vnet_iter);
1433 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1434 if (ifp->if_type == IFT_ATM)
1439 VNET_LIST_RUNLOCK();