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>
49 #include <sys/vimage.h>
52 #include <net/if_types.h>
53 #include <net/if_arp.h>
54 #include <net/if_var.h>
55 #include <net/if_media.h>
56 #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 SYSCTL_NODE(_net_graph, OID_AUTO, atm, CTLFLAG_RW, 0, "atm related stuff");
81 static int allow_shutdown;
83 SYSCTL_INT(_net_graph_atm, OID_AUTO, allow_shutdown, CTLFLAG_RW,
84 &allow_shutdown, 0, "allow ng_atm nodes to shutdown");
91 uint16_t vpi; /* VPI of this hook */
92 uint16_t vci; /* VCI of this hook, 0 if none */
93 uint32_t flags; /* private flags */
94 hook_p hook; /* the connected hook */
96 LIST_ENTRY(ngvcc) link;
98 #define VCC_OPEN 0x0001 /* open */
104 struct ifnet *ifp; /* the ATM interface */
105 hook_p input; /* raw input hook */
106 hook_p orphans; /* packets to nowhere */
107 hook_p output; /* catch output packets */
108 hook_p manage; /* has also entry in vccs */
111 uint64_t out_packets;
114 LIST_HEAD(, ngvcc) vccs;
118 * Parse ifstate state
120 static const struct ng_parse_struct_field ng_atm_if_change_info[] =
121 NGM_ATM_IF_CHANGE_INFO;
122 static const struct ng_parse_type ng_atm_if_change_type = {
123 &ng_parse_struct_type,
124 &ng_atm_if_change_info
128 * Parse vcc state change
130 static const struct ng_parse_struct_field ng_atm_vcc_change_info[] =
131 NGM_ATM_VCC_CHANGE_INFO;
132 static const struct ng_parse_type ng_atm_vcc_change_type = {
133 &ng_parse_struct_type,
134 &ng_atm_vcc_change_info
140 static const struct ng_parse_struct_field ng_atm_acr_change_info[] =
141 NGM_ATM_ACR_CHANGE_INFO;
142 static const struct ng_parse_type ng_atm_acr_change_type = {
143 &ng_parse_struct_type,
144 &ng_atm_acr_change_info
148 * Parse the configuration structure ng_atm_config
150 static const struct ng_parse_struct_field ng_atm_config_type_info[] =
153 static const struct ng_parse_type ng_atm_config_type = {
154 &ng_parse_struct_type,
155 &ng_atm_config_type_info
159 * Parse a single vcc structure and a variable array of these ng_atm_vccs
161 static const struct ng_parse_struct_field ng_atm_tparam_type_info[] =
163 static const struct ng_parse_type ng_atm_tparam_type = {
164 &ng_parse_struct_type,
165 &ng_atm_tparam_type_info
167 static const struct ng_parse_struct_field ng_atm_vcc_type_info[] =
169 static const struct ng_parse_type ng_atm_vcc_type = {
170 &ng_parse_struct_type,
171 &ng_atm_vcc_type_info
176 ng_atm_vccarray_getlen(const struct ng_parse_type *type,
177 const u_char *start, const u_char *buf)
179 const struct atmio_vcctable *vp;
181 vp = (const struct atmio_vcctable *)
182 (buf - offsetof(struct atmio_vcctable, vccs));
186 static const struct ng_parse_array_info ng_atm_vccarray_info =
187 NGM_ATM_VCCARRAY_INFO;
188 static const struct ng_parse_type ng_atm_vccarray_type = {
189 &ng_parse_array_type,
190 &ng_atm_vccarray_info
194 static const struct ng_parse_struct_field ng_atm_vcctable_type_info[] =
195 NGM_ATM_VCCTABLE_INFO;
197 static const struct ng_parse_type ng_atm_vcctable_type = {
198 &ng_parse_struct_type,
199 &ng_atm_vcctable_type_info
203 * Parse CPCS INIT structure ng_atm_cpcs_init
205 static const struct ng_parse_struct_field ng_atm_cpcs_init_type_info[] =
206 NGM_ATM_CPCS_INIT_INFO;
208 static const struct ng_parse_type ng_atm_cpcs_init_type = {
209 &ng_parse_struct_type,
210 &ng_atm_cpcs_init_type_info
214 * Parse CPCS TERM structure ng_atm_cpcs_term
216 static const struct ng_parse_struct_field ng_atm_cpcs_term_type_info[] =
217 NGM_ATM_CPCS_TERM_INFO;
219 static const struct ng_parse_type ng_atm_cpcs_term_type = {
220 &ng_parse_struct_type,
221 &ng_atm_cpcs_term_type_info
225 * Parse statistic struct
227 static const struct ng_parse_struct_field ng_atm_stats_type_info[] =
230 static const struct ng_parse_type ng_atm_stats_type = {
231 &ng_parse_struct_type,
232 &ng_atm_stats_type_info
235 static const struct ng_cmdlist ng_atm_cmdlist[] = {
241 &ng_parse_string_type
255 &ng_atm_vcctable_type
261 &ng_atm_cpcs_init_type,
268 &ng_atm_cpcs_term_type,
275 &ng_parse_hookbuf_type,
298 &ng_atm_if_change_type,
299 &ng_atm_if_change_type,
305 &ng_atm_vcc_change_type,
306 &ng_atm_vcc_change_type,
312 &ng_atm_acr_change_type,
313 &ng_atm_acr_change_type,
318 static int ng_atm_mod_event(module_t, int, void *);
320 static ng_constructor_t ng_atm_constructor;
321 static ng_shutdown_t ng_atm_shutdown;
322 static ng_rcvmsg_t ng_atm_rcvmsg;
323 static ng_newhook_t ng_atm_newhook;
324 static ng_connect_t ng_atm_connect;
325 static ng_disconnect_t ng_atm_disconnect;
326 static ng_rcvdata_t ng_atm_rcvdata;
327 static ng_rcvdata_t ng_atm_rcvdrop;
329 static struct ng_type ng_atm_typestruct = {
330 .version = NG_ABI_VERSION,
331 .name = NG_ATM_NODE_TYPE,
332 .mod_event = ng_atm_mod_event,
333 .constructor = ng_atm_constructor,
334 .rcvmsg = ng_atm_rcvmsg,
335 .shutdown = ng_atm_shutdown,
336 .newhook = ng_atm_newhook,
337 .connect = ng_atm_connect,
338 .rcvdata = ng_atm_rcvdata,
339 .disconnect = ng_atm_disconnect,
340 .cmdlist = ng_atm_cmdlist,
342 NETGRAPH_INIT(atm, &ng_atm_typestruct);
344 static const struct {
347 } atmmedia[] = IFM_SUBTYPE_ATM_DESCRIPTIONS;
350 #define IFP2NG(IFP) ((node_p)((struct ifatm *)(IFP)->if_softc)->ngpriv)
351 #define IFP2NG_SET(IFP, val) (((struct ifatm *)(IFP)->if_softc)->ngpriv = (val))
353 #define IFFLAGS "\020\001UP\002BROADCAST\003DEBUG\004LOOPBACK" \
354 "\005POINTOPOINT\006SMART\007RUNNING\010NOARP" \
355 "\011PROMISC\012ALLMULTI\013OACTIVE\014SIMPLEX" \
356 "\015LINK0\016LINK1\017LINK2\020MULTICAST"
359 /************************************************************/
364 * A packet is received from an interface.
365 * If we have an input hook, prepend the pseudoheader to the data and
366 * deliver it out to that hook. If not, look whether it is destined for
367 * use. If so locate the appropriate hook, deliver the packet without the
368 * header and we are done. If it is not for us, leave it alone.
371 ng_atm_input(struct ifnet *ifp, struct mbuf **mp,
372 struct atm_pseudohdr *ah, void *rxhand)
374 node_p node = IFP2NG(ifp);
376 const struct ngvcc *vcc;
381 priv = NG_NODE_PRIVATE(node);
382 if (priv->input != NULL) {
384 * Prepend the atm_pseudoheader.
386 M_PREPEND(*mp, sizeof(*ah), M_DONTWAIT);
389 memcpy(mtod(*mp, struct atm_pseudohdr *), ah, sizeof(*ah));
390 NG_SEND_DATA_ONLY(error, priv->input, *mp);
396 printf("%s: error=%d\n", __func__, error);
402 if ((ATM_PH_FLAGS(ah) & ATMIO_FLAG_NG) == 0)
405 vcc = (struct ngvcc *)rxhand;
407 NG_SEND_DATA_ONLY(error, vcc->hook, *mp);
413 printf("%s: error=%d\n", __func__, error);
420 * ATM packet is about to be output. The atm_pseudohdr is already prepended.
421 * If the hook is set, reroute the packet to the hook.
424 ng_atm_output(struct ifnet *ifp, struct mbuf **mp)
426 const node_p node = IFP2NG(ifp);
427 const struct priv *priv;
432 priv = NG_NODE_PRIVATE(node);
434 NG_SEND_DATA_ONLY(error, priv->output, *mp);
442 * Well, this doesn't make much sense for ATM.
445 ng_atm_input_orphans(struct ifnet *ifp, struct mbuf *m,
446 struct atm_pseudohdr *ah, void *rxhand)
448 node_p node = IFP2NG(ifp);
456 priv = NG_NODE_PRIVATE(node);
457 if (priv->orphans == NULL) {
462 * Prepend the atm_pseudoheader.
464 M_PREPEND(m, sizeof(*ah), M_DONTWAIT);
467 memcpy(mtod(m, struct atm_pseudohdr *), ah, sizeof(*ah));
468 NG_SEND_DATA_ONLY(error, priv->orphans, m);
474 printf("%s: error=%d\n", __func__, error);
479 /************************************************************/
484 ng_atm_rcvdata(hook_p hook, item_p item)
486 node_p node = NG_HOOK_NODE(hook);
487 struct priv *priv = NG_NODE_PRIVATE(node);
488 const struct ngvcc *vcc = NG_HOOK_PRIVATE(hook);
490 struct atm_pseudohdr *aph;
502 * Prepend pseudo-hdr. Drivers don't care about the flags.
504 M_PREPEND(m, sizeof(*aph), M_DONTWAIT);
509 aph = mtod(m, struct atm_pseudohdr *);
510 ATM_PH_VPI(aph) = vcc->vpi;
511 ATM_PH_SETVCI(aph, vcc->vci);
512 ATM_PH_FLAGS(aph) = 0;
514 if ((error = atm_output(priv->ifp, m, NULL, NULL)) == 0)
522 ng_atm_rcvdrop(hook_p hook, item_p item)
529 /************************************************************
534 ng_atm_event_func(node_p node, hook_p hook, void *arg, int event)
536 const struct priv *priv = NG_NODE_PRIVATE(node);
538 struct ng_mesg *mesg;
543 case ATMEV_FLOW_CONTROL:
545 struct atmev_flow_control *ev = arg;
546 struct ngm_queue_state *qstate;
548 /* find the connection */
549 LIST_FOREACH(vcc, &priv->vccs, link)
550 if (vcc->vci == ev->vci && vcc->vpi == ev->vpi)
555 /* convert into a flow control message */
556 NG_MKMESSAGE(mesg, NGM_FLOW_COOKIE,
557 ev->busy ? NGM_HIGH_WATER_PASSED : NGM_LOW_WATER_PASSED,
558 sizeof(struct ngm_queue_state), M_NOWAIT);
561 qstate = (struct ngm_queue_state *)mesg->data;
563 /* XXX have to figure out how to get that info */
565 NG_SEND_MSG_HOOK(error, node, mesg, vcc->hook, 0);
569 case ATMEV_VCC_CHANGED:
571 struct atmev_vcc_changed *ev = arg;
572 struct ngm_atm_vcc_change *chg;
574 if (priv->manage == NULL)
576 NG_MKMESSAGE(mesg, NGM_ATM_COOKIE, NGM_ATM_VCC_CHANGE,
577 sizeof(struct ngm_atm_vcc_change), M_NOWAIT);
580 chg = (struct ngm_atm_vcc_change *)mesg->data;
583 chg->state = (ev->up != 0);
584 chg->node = NG_NODE_ID(node);
585 NG_SEND_MSG_HOOK(error, node, mesg, priv->manage, 0);
589 case ATMEV_IFSTATE_CHANGED:
591 struct atmev_ifstate_changed *ev = arg;
592 struct ngm_atm_if_change *chg;
594 if (priv->manage == NULL)
596 NG_MKMESSAGE(mesg, NGM_ATM_COOKIE, NGM_ATM_IF_CHANGE,
597 sizeof(struct ngm_atm_if_change), M_NOWAIT);
600 chg = (struct ngm_atm_if_change *)mesg->data;
601 chg->carrier = (ev->carrier != 0);
602 chg->running = (ev->running != 0);
603 chg->node = NG_NODE_ID(node);
604 NG_SEND_MSG_HOOK(error, node, mesg, priv->manage, 0);
608 case ATMEV_ACR_CHANGED:
610 struct atmev_acr_changed *ev = arg;
611 struct ngm_atm_acr_change *acr;
613 /* find the connection */
614 LIST_FOREACH(vcc, &priv->vccs, link)
615 if (vcc->vci == ev->vci && vcc->vpi == ev->vpi)
620 /* convert into a flow control message */
621 NG_MKMESSAGE(mesg, NGM_ATM_COOKIE, NGM_ATM_ACR_CHANGE,
622 sizeof(struct ngm_atm_acr_change), M_NOWAIT);
625 acr = (struct ngm_atm_acr_change *)mesg->data;
626 acr->node = NG_NODE_ID(node);
631 NG_SEND_MSG_HOOK(error, node, mesg, vcc->hook, 0);
638 * Use send_fn to get the right lock
641 ng_atm_event(struct ifnet *ifp, uint32_t event, void *arg)
643 const node_p node = IFP2NG(ifp);
646 /* may happen during attach/detach */
647 (void)ng_send_fn(node, NULL, ng_atm_event_func, arg, event);
650 /************************************************************
655 * Open a channel for the user
658 ng_atm_cpcs_init(node_p node, const struct ngm_atm_cpcs_init *arg)
660 struct priv *priv = NG_NODE_PRIVATE(node);
661 const struct ifatm_mib *mib;
663 struct atmio_openvcc data;
666 if(priv->ifp->if_ioctl == NULL)
669 mib = (const struct ifatm_mib *)(priv->ifp->if_linkmib);
671 LIST_FOREACH(vcc, &priv->vccs, link)
672 if (strcmp(arg->name, NG_HOOK_NAME(vcc->hook)) == 0)
676 if (vcc->flags & VCC_OPEN)
680 * Check user arguments and construct ioctl argument
682 memset(&data, 0, sizeof(data));
686 switch (data.param.aal = arg->aal) {
700 data.param.vpi = arg->vpi;
702 /* allow 0.0 as catch all receive channel */
703 if (arg->vci == 0 && (arg->vpi != 0 || !(arg->flags & ATMIO_FLAG_NOTX)))
705 data.param.vci = arg->vci;
707 data.param.tparam.pcr = arg->pcr;
709 if (arg->mcr > arg->pcr)
711 data.param.tparam.mcr = arg->mcr;
713 if (!(arg->flags & ATMIO_FLAG_NOTX)) {
715 data.param.tmtu = priv->ifp->if_mtu;
717 data.param.tmtu = arg->tmtu;
720 if (!(arg->flags & ATMIO_FLAG_NORX)) {
722 data.param.rmtu = priv->ifp->if_mtu;
724 data.param.rmtu = arg->rmtu;
728 switch (data.param.traffic = arg->traffic) {
730 case ATMIO_TRAFFIC_UBR:
731 case ATMIO_TRAFFIC_CBR:
734 case ATMIO_TRAFFIC_VBR:
735 if (arg->scr > arg->pcr)
737 data.param.tparam.scr = arg->scr;
739 if (arg->mbs > (1 << 24))
741 data.param.tparam.mbs = arg->mbs;
744 case ATMIO_TRAFFIC_ABR:
745 if (arg->icr > arg->pcr || arg->icr < arg->mcr)
747 data.param.tparam.icr = arg->icr;
749 if (arg->tbe == 0 || arg->tbe > (1 << 24))
751 data.param.tparam.tbe = arg->tbe;
755 data.param.tparam.nrm = arg->nrm;
759 data.param.tparam.trm = arg->trm;
761 if (arg->adtf > 0x3ff)
763 data.param.tparam.adtf = arg->adtf;
767 data.param.tparam.rif = arg->rif;
771 data.param.tparam.rdf = arg->rdf;
775 data.param.tparam.cdf = arg->cdf;
783 if ((arg->flags & ATMIO_FLAG_NORX) && (arg->flags & ATMIO_FLAG_NOTX))
786 data.param.flags = arg->flags & ~(ATM_PH_AAL5 | ATM_PH_LLCSNAP);
787 data.param.flags |= ATMIO_FLAG_NG;
789 err = (*priv->ifp->if_ioctl)(priv->ifp, SIOCATMOPENVCC, (caddr_t)&data);
792 vcc->vci = data.param.vci;
793 vcc->vpi = data.param.vpi;
794 vcc->flags = VCC_OPEN;
801 * Issue the close command to the driver
804 cpcs_term(const struct priv *priv, u_int vpi, u_int vci)
806 struct atmio_closevcc data;
808 if (priv->ifp->if_ioctl == NULL)
814 return ((*priv->ifp->if_ioctl)(priv->ifp,
815 SIOCATMCLOSEVCC, (caddr_t)&data));
820 * Close a channel by request of the user
823 ng_atm_cpcs_term(node_p node, const struct ngm_atm_cpcs_term *arg)
825 struct priv *priv = NG_NODE_PRIVATE(node);
829 LIST_FOREACH(vcc, &priv->vccs, link)
830 if(strcmp(arg->name, NG_HOOK_NAME(vcc->hook)) == 0)
834 if (!(vcc->flags & VCC_OPEN))
837 error = cpcs_term(priv, vcc->vpi, vcc->vci);
846 /************************************************************/
852 * Produce a textual description of the current status
855 text_status(node_p node, char *arg, u_int len)
857 const struct priv *priv = NG_NODE_PRIVATE(node);
858 const struct ifatm_mib *mib;
862 static const struct {
869 mib = (const struct ifatm_mib *)(priv->ifp->if_linkmib);
871 sbuf_new(&sbuf, arg, len, SBUF_FIXEDLEN);
872 sbuf_printf(&sbuf, "interface: %s\n", priv->ifp->if_xname);
874 if (mib->device >= sizeof(devices) / sizeof(devices[0]))
875 sbuf_printf(&sbuf, "device=unknown\nvendor=unknown\n");
877 sbuf_printf(&sbuf, "device=%s\nvendor=%s\n",
878 devices[mib->device].name, devices[mib->device].vendor);
880 for (i = 0; atmmedia[i].name; i++)
881 if(mib->media == atmmedia[i].media) {
882 sbuf_printf(&sbuf, "media=%s\n", atmmedia[i].name);
885 if(atmmedia[i].name == NULL)
886 sbuf_printf(&sbuf, "media=unknown\n");
888 sbuf_printf(&sbuf, "serial=%u esi=%6D hardware=%u software=%u\n",
889 mib->serial, mib->esi, ":", mib->hw_version, mib->sw_version);
890 sbuf_printf(&sbuf, "pcr=%u vpi_bits=%u vci_bits=%u max_vpcs=%u "
891 "max_vccs=%u\n", mib->pcr, mib->vpi_bits, mib->vci_bits,
892 mib->max_vpcs, mib->max_vccs);
893 sbuf_printf(&sbuf, "ifflags=%b\n", priv->ifp->if_flags, IFFLAGS);
897 return (sbuf_len(&sbuf));
901 * Get control message
904 ng_atm_rcvmsg(node_p node, item_p item, hook_p lasthook)
906 const struct priv *priv = NG_NODE_PRIVATE(node);
907 struct ng_mesg *resp = NULL;
909 struct ifatm_mib *mib = (struct ifatm_mib *)(priv->ifp->if_linkmib);
912 NGI_GET_MSG(item, msg);
914 switch (msg->header.typecookie) {
916 case NGM_GENERIC_COOKIE:
917 switch (msg->header.cmd) {
919 case NGM_TEXT_STATUS:
920 NG_MKRESPONSE(resp, msg, NG_TEXTRESPONSE, M_NOWAIT);
926 resp->header.arglen = text_status(node,
927 (char *)resp->data, resp->header.arglen) + 1;
937 switch (msg->header.cmd) {
939 case NGM_ATM_GET_IFNAME:
940 NG_MKRESPONSE(resp, msg, IFNAMSIZ, M_NOWAIT);
945 strlcpy(resp->data, priv->ifp->if_xname, IFNAMSIZ);
948 case NGM_ATM_GET_CONFIG:
950 struct ngm_atm_config *config;
952 NG_MKRESPONSE(resp, msg, sizeof(*config), M_NOWAIT);
957 config = (struct ngm_atm_config *)resp->data;
958 config->pcr = mib->pcr;
959 config->vpi_bits = mib->vpi_bits;
960 config->vci_bits = mib->vci_bits;
961 config->max_vpcs = mib->max_vpcs;
962 config->max_vccs = mib->max_vccs;
966 case NGM_ATM_GET_VCCS:
968 struct atmio_vcctable *vccs;
971 if (priv->ifp->if_ioctl == NULL) {
975 error = (*priv->ifp->if_ioctl)(priv->ifp,
976 SIOCATMGETVCCS, (caddr_t)&vccs);
980 len = sizeof(*vccs) +
981 vccs->count * sizeof(vccs->vccs[0]);
982 NG_MKRESPONSE(resp, msg, len, M_NOWAIT);
985 free(vccs, M_DEVBUF);
989 (void)memcpy(resp->data, vccs, len);
990 free(vccs, M_DEVBUF);
995 case NGM_ATM_GET_VCC:
997 char hook[NG_HOOKSIZ];
998 struct atmio_vcctable *vccs;
1002 if (priv->ifp->if_ioctl == NULL) {
1006 if (msg->header.arglen != NG_HOOKSIZ) {
1010 strncpy(hook, msg->data, NG_HOOKSIZ);
1011 hook[NG_HOOKSIZ - 1] = '\0';
1012 LIST_FOREACH(vcc, &priv->vccs, link)
1013 if (strcmp(NG_HOOK_NAME(vcc->hook), hook) == 0)
1019 error = (*priv->ifp->if_ioctl)(priv->ifp,
1020 SIOCATMGETVCCS, (caddr_t)&vccs);
1024 for (i = 0; i < vccs->count; i++)
1025 if (vccs->vccs[i].vpi == vcc->vpi &&
1026 vccs->vccs[i].vci == vcc->vci)
1028 if (i == vccs->count) {
1030 free(vccs, M_DEVBUF);
1034 NG_MKRESPONSE(resp, msg, sizeof(vccs->vccs[0]),
1038 free(vccs, M_DEVBUF);
1042 *(struct atmio_vcc *)resp->data = vccs->vccs[i];
1043 free(vccs, M_DEVBUF);
1047 case NGM_ATM_GET_VCCID:
1049 struct atmio_vcc *arg;
1050 struct atmio_vcctable *vccs;
1053 if (priv->ifp->if_ioctl == NULL) {
1057 if (msg->header.arglen != sizeof(*arg)) {
1061 arg = (struct atmio_vcc *)msg->data;
1063 error = (*priv->ifp->if_ioctl)(priv->ifp,
1064 SIOCATMGETVCCS, (caddr_t)&vccs);
1068 for (i = 0; i < vccs->count; i++)
1069 if (vccs->vccs[i].vpi == arg->vpi &&
1070 vccs->vccs[i].vci == arg->vci)
1072 if (i == vccs->count) {
1074 free(vccs, M_DEVBUF);
1078 NG_MKRESPONSE(resp, msg, sizeof(vccs->vccs[0]),
1082 free(vccs, M_DEVBUF);
1086 *(struct atmio_vcc *)resp->data = vccs->vccs[i];
1087 free(vccs, M_DEVBUF);
1091 case NGM_ATM_CPCS_INIT:
1092 if (msg->header.arglen !=
1093 sizeof(struct ngm_atm_cpcs_init)) {
1097 error = ng_atm_cpcs_init(node,
1098 (struct ngm_atm_cpcs_init *)msg->data);
1101 case NGM_ATM_CPCS_TERM:
1102 if (msg->header.arglen !=
1103 sizeof(struct ngm_atm_cpcs_term)) {
1107 error = ng_atm_cpcs_term(node,
1108 (struct ngm_atm_cpcs_term *)msg->data);
1111 case NGM_ATM_GET_STATS:
1113 struct ngm_atm_stats *p;
1115 if (msg->header.arglen != 0) {
1119 NG_MKRESPONSE(resp, msg, sizeof(*p), M_NOWAIT);
1124 p = (struct ngm_atm_stats *)resp->data;
1125 p->in_packets = priv->in_packets;
1126 p->out_packets = priv->out_packets;
1127 p->in_errors = priv->in_errors;
1128 p->out_errors = priv->out_errors;
1144 NG_RESPOND_MSG(error, node, item, resp);
1149 /************************************************************/
1155 * A new hook is create that will be connected to the node.
1156 * Check, whether the name is one of the predefined ones.
1157 * If not, create a new entry into the vcc list.
1160 ng_atm_newhook(node_p node, hook_p hook, const char *name)
1162 struct priv *priv = NG_NODE_PRIVATE(node);
1165 if (strcmp(name, "input") == 0) {
1167 NG_HOOK_SET_RCVDATA(hook, ng_atm_rcvdrop);
1170 if (strcmp(name, "output") == 0) {
1171 priv->output = hook;
1172 NG_HOOK_SET_RCVDATA(hook, ng_atm_rcvdrop);
1175 if (strcmp(name, "orphans") == 0) {
1176 priv->orphans = hook;
1177 NG_HOOK_SET_RCVDATA(hook, ng_atm_rcvdrop);
1182 * Allocate a new entry
1184 vcc = malloc(sizeof(*vcc), M_NETGRAPH, M_NOWAIT | M_ZERO);
1189 NG_HOOK_SET_PRIVATE(hook, vcc);
1191 LIST_INSERT_HEAD(&priv->vccs, vcc, link);
1193 if (strcmp(name, "manage") == 0)
1194 priv->manage = hook;
1200 * Connect. Set the peer to queuing.
1203 ng_atm_connect(hook_p hook)
1205 if (NG_HOOK_PRIVATE(hook) != NULL)
1206 NG_HOOK_FORCE_QUEUE(NG_HOOK_PEER(hook));
1215 ng_atm_disconnect(hook_p hook)
1217 struct priv *priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
1218 struct ngvcc *vcc = NG_HOOK_PRIVATE(hook);
1221 if (hook == priv->output) {
1222 priv->output = NULL;
1225 if (hook == priv->input) {
1229 if (hook == priv->orphans) {
1230 priv->orphans = NULL;
1233 log(LOG_ERR, "ng_atm: bad hook '%s'", NG_HOOK_NAME(hook));
1237 /* don't terminate if we are detaching from the interface */
1238 if ((vcc->flags & VCC_OPEN) && priv->ifp != NULL)
1239 (void)cpcs_term(priv, vcc->vpi, vcc->vci);
1241 NG_HOOK_SET_PRIVATE(hook, NULL);
1243 LIST_REMOVE(vcc, link);
1244 free(vcc, M_NETGRAPH);
1246 if (hook == priv->manage)
1247 priv->manage = NULL;
1252 /************************************************************/
1258 * ATM interface attached - create a node and name it like the interface.
1261 ng_atm_attach(struct ifnet *ifp)
1266 KASSERT(IFP2NG(ifp) == 0, ("%s: node alreay exists?", __func__));
1268 if (ng_make_node_common(&ng_atm_typestruct, &node) != 0) {
1269 log(LOG_ERR, "%s: can't create node for %s\n",
1270 __func__, ifp->if_xname);
1274 priv = malloc(sizeof(*priv), M_NETGRAPH, M_NOWAIT | M_ZERO);
1276 log(LOG_ERR, "%s: can't allocate memory for %s\n",
1277 __func__, ifp->if_xname);
1278 NG_NODE_UNREF(node);
1281 NG_NODE_SET_PRIVATE(node, priv);
1283 LIST_INIT(&priv->vccs);
1284 IFP2NG_SET(ifp, node);
1286 if (ng_name_node(node, ifp->if_xname) != 0) {
1287 log(LOG_WARNING, "%s: can't name node %s\n",
1288 __func__, ifp->if_xname);
1293 * ATM interface detached - destroy node.
1296 ng_atm_detach(struct ifnet *ifp)
1298 const node_p node = IFP2NG(ifp);
1304 NG_NODE_REALLY_DIE(node);
1306 priv = NG_NODE_PRIVATE(node);
1307 IFP2NG_SET(priv->ifp, NULL);
1310 ng_rmnode_self(node);
1314 * Shutdown the node. This is called from the shutdown message processing.
1317 ng_atm_shutdown(node_p node)
1319 struct priv *priv = NG_NODE_PRIVATE(node);
1321 if (node->nd_flags & NGF_REALLY_DIE) {
1323 * We are called from unloading the ATM driver. Really,
1324 * really need to shutdown this node. The ifp was
1325 * already handled in the detach routine.
1327 NG_NODE_SET_PRIVATE(node, NULL);
1328 free(priv, M_NETGRAPH);
1330 NG_NODE_UNREF(node);
1335 if (!allow_shutdown)
1336 NG_NODE_REVIVE(node); /* we persist */
1338 IFP2NG_SET(priv->ifp, NULL);
1339 NG_NODE_SET_PRIVATE(node, NULL);
1340 free(priv, M_NETGRAPH);
1341 NG_NODE_UNREF(node);
1345 * We are persistant - reinitialize
1347 NG_NODE_REVIVE(node);
1353 * Nodes are constructed only via interface attaches.
1356 ng_atm_constructor(node_p nodep)
1361 /************************************************************/
1366 * Loading and unloading of node type
1368 * The assignments to the globals for the hooks should be ok without
1369 * a special hook. The use pattern is generally: check that the pointer
1370 * is not NULL, call the function. In the attach case this is no problem.
1371 * In the detach case we can detach only when no ATM node exists. That
1372 * means that there is no ATM interface anymore. So we are sure that
1373 * we are not in the code path in if_atmsubr.c. To prevent someone
1374 * from adding an interface after we have started to unload the node, we
1375 * take the iflist lock so an if_attach will be blocked until we are done.
1376 * XXX: perhaps the function pointers should be 'volatile' for this to work
1380 ng_atm_mod_event(module_t mod, int event, void *data)
1382 VNET_ITERATOR_DECL(vnet_iter);
1390 * Register function hooks
1392 if (ng_atm_attach_p != NULL) {
1398 ng_atm_attach_p = ng_atm_attach;
1399 ng_atm_detach_p = ng_atm_detach;
1400 ng_atm_output_p = ng_atm_output;
1401 ng_atm_input_p = ng_atm_input;
1402 ng_atm_input_orphan_p = ng_atm_input_orphans;
1403 ng_atm_event_p = ng_atm_event;
1405 /* Create nodes for existing ATM interfaces */
1407 VNET_FOREACH(vnet_iter) {
1408 CURVNET_SET_QUIET(vnet_iter);
1409 INIT_VNET_NET(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 INIT_VNET_NET(vnet_iter);
1434 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1435 if (ifp->if_type == IFT_ATM)
1440 VNET_LIST_RUNLOCK();