2 * Copyright (c) 2001-2002
3 * Fraunhofer Institute for Open Communication Systems (FhG Fokus).
5 * Copyright (c) 2003-2004
9 * Author: Hartmut Brandt <harti@freebsd.org>
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
20 * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SNMP module for ATM hardware interfaces - FreeBSD/Ng specific part.
45 #include <net/if_atm.h>
47 #include <bsnmp/snmp_netgraph.h>
48 #include <netgraph/ng_message.h>
49 #include <netgraph/atm/ng_atm.h>
51 static const struct hwinfo {
60 void *regc; /* cookie registration */
64 * Find the interface for a given node
67 atm_node2if(u_int node)
69 struct atmif_priv *aif;
72 TAILQ_FOREACH(aif, &atmif_list, link)
73 if (aif->sys->atm_node == node)
79 atm_if2node(struct atmif *pub)
81 struct atmif_priv *aif = (struct atmif_priv *)pub;
83 return (aif->sys->atm_node);
87 * Destroy system dependend stuff.
90 atmif_sys_destroy(struct atmif_priv *aif)
93 ng_unregister_cookie(aif->sys->regc);
99 * Handle a message from the ATM node
102 handle_atm_message(const struct ng_mesg *mesg, const char *path __unused,
103 ng_ID_t node, void *uarg)
105 struct atmif_priv *aif = uarg;
106 enum atmif_carrier_state ost;
108 switch (mesg->header.cmd) {
110 case NGM_ATM_IF_CHANGE:
112 const struct ngm_atm_if_change *arg;
114 ost = aif->pub.carrier;
115 if (mesg->header.arglen != sizeof(*arg)) {
116 syslog(LOG_ERR, "ATM_IF_CHANGE: wrong size");
117 atmif_check_carrier(aif);
120 arg = (const struct ngm_atm_if_change *)
121 (const void *)mesg->data;
124 aif->pub.carrier = ATMIF_CARRIER_ON;
126 aif->pub.carrier = ATMIF_CARRIER_OFF;
128 if (ost != aif->pub.carrier)
129 atmif_send_notification(aif, ATMIF_NOTIFY_CARRIER,
134 case NGM_ATM_VCC_CHANGE:
136 const struct ngm_atm_vcc_change *arg;
138 if (mesg->header.arglen != sizeof(*arg)) {
139 syslog(LOG_ERR, "ATM_VCC_CHANGE: wrong size");
142 arg = (const struct ngm_atm_vcc_change *)
143 (const void *)mesg->data;
144 atmif_send_notification(aif, ATMIF_NOTIFY_VCC,
145 (uintptr_t)(((arg->vpi & 0xff) << 24) |
146 ((arg->vci & 0xffff) << 8) | (arg->state & 1)));
150 syslog(LOG_WARNING, "spurious message %u from node [%x]",
151 mesg->header.cmd, node);
155 * Attach to an ATM interface
158 atmif_sys_attach_if(struct atmif_priv *aif)
160 struct ng_mesg *resp, *resp1;
161 struct namelist *list;
164 if ((aif->sys = malloc(sizeof(*aif->sys))) == NULL) {
165 syslog(LOG_CRIT, "out of memory");
168 memset(aif->sys, 0, sizeof(*aif->sys));
170 if ((aif->pub.mib = malloc(sizeof(*aif->pub.mib))) == NULL) {
172 syslog(LOG_CRIT, "out of memory");
176 atmif_sys_fill_mib(aif);
179 * Get ATM node Id. Must do it the hard way by scanning all nodes
180 * because the name may be wrong.
182 if ((resp = ng_dialog_id(snmp_node, NGM_GENERIC_COOKIE, NGM_LISTNODES,
184 syslog(LOG_ERR, "cannot fetch node list: %m");
188 list = (struct namelist *)(void *)resp->data;
190 for (i = 0; i < list->numnames; i++) {
191 if (strcmp(list->nodeinfo[i].type, NG_ATM_NODE_TYPE) != 0)
193 if ((resp1 = ng_dialog_id(list->nodeinfo[i].id,
194 NGM_ATM_COOKIE, NGM_ATM_GET_IFNAME, NULL, 0)) == NULL)
196 if (strcmp(resp1->data, aif->pub.ifp->name) == 0) {
202 if (i == list->numnames)
203 aif->sys->atm_node = 0;
205 aif->sys->atm_node = list->nodeinfo[i].id;
209 if ((aif->sys->regc = ng_register_cookie(module, NGM_ATM_COOKIE,
210 aif->sys->atm_node, handle_atm_message, aif)) == NULL) {
211 syslog(LOG_ERR, "cannot register cookie: %m");
219 * Table of all ATM interfaces - Ng part
222 op_atmif_ng(struct snmp_context *ctx __unused, struct snmp_value *value,
223 u_int sub, u_int vindex __unused, enum snmp_op op)
225 struct atmif_priv *aif;
228 if ((err = atmif_get_aif(value, sub, op, &aif)) != SNMP_ERR_NOERROR)
231 if (op == SNMP_OP_SET) {
232 switch (value->var.subs[sub - 1]) {
235 return (SNMP_ERR_NOT_WRITEABLE);
239 switch (value->var.subs[sub - 1]) {
241 case LEAF_begemotAtmIfNodeId:
242 value->v.uint32 = aif->sys->atm_node;
243 return (SNMP_ERR_NOERROR);
252 atm_sys_get_hw_vendor(struct atmif_priv *aif, struct snmp_value *value)
255 if (aif->pub.mib->device >= sizeof(hwinfo) / sizeof(hwinfo[0]))
256 return (string_get(value, "unknown", -1));
257 return (string_get(value, hwinfo[aif->pub.mib->device].vendor, -1));
264 atm_sys_get_hw_device(struct atmif_priv *aif, struct snmp_value *value)
267 if (aif->pub.mib->device >= sizeof(hwinfo) / sizeof(hwinfo[0]))
268 return (string_get(value, "unknown", -1));
269 return (string_get(value, hwinfo[aif->pub.mib->device].device, -1));
273 * Extract the ATM MIB from the interface's private MIB
276 atmif_sys_fill_mib(struct atmif_priv *aif)
278 struct ifatm_mib *mib;
280 if (aif->pub.ifp->specmiblen != sizeof(struct ifatm_mib)) {
281 syslog(LOG_ERR, "atmif MIB has wrong size %zu",
282 aif->pub.ifp->specmiblen);
283 memset(aif->pub.mib, 0, sizeof(*aif->pub.mib));
284 aif->pub.mib->version = 0;
287 mib = (struct ifatm_mib *)aif->pub.ifp->specmib;
289 aif->pub.mib->device = mib->device;
290 aif->pub.mib->serial = mib->serial;
291 aif->pub.mib->hw_version = mib->hw_version;
292 aif->pub.mib->sw_version = mib->sw_version;
293 aif->pub.mib->media = mib->media;
295 memcpy(aif->pub.mib->esi, mib->esi, 6);
296 aif->pub.mib->pcr = mib->pcr;
297 aif->pub.mib->vpi_bits = mib->vpi_bits;
298 aif->pub.mib->vci_bits = mib->vci_bits;
299 aif->pub.mib->max_vpcs = mib->max_vpcs;
300 aif->pub.mib->max_vccs = mib->max_vccs;