aboutsummaryrefslogtreecommitdiff
path: root/usr.sbin/bsnmpd/modules/snmp_atm/atm_sys.c
blob: 20a1925d61aa8eded9d8fffb3a2b2f36651a9025 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/*
 * Copyright (c) 2001-2002
 *	Fraunhofer Institute for Open Communication Systems (FhG Fokus).
 *	All rights reserved.
 * Copyright (c) 2003-2004
 *	Hartmut Brandt.
 *	All rights reserved.
 *
 * Author: Hartmut Brandt <harti@freebsd.org>
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 
 * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * $FreeBSD: src/usr.sbin/bsnmpd/modules/snmp_atm/atm_sys.c,v 1.1.26.1.6.1 2010/12/21 17:09:25 kensmith Exp $
 *
 * SNMP module for ATM hardware interfaces - FreeBSD/Ng specific part.
 */

#include "atm.h"
#include "atm_tree.h"
#include "atm_oid.h"

#include <stdlib.h>
#include <syslog.h>
#include <string.h>

#include <net/if_atm.h>

#include <bsnmp/snmp_netgraph.h>
#include <netgraph/ng_message.h>
#include <netgraph/atm/ng_atm.h>

static const struct hwinfo {
	const char *device;
	const char *vendor;
} hwinfo[] = {
	ATM_DEVICE_NAMES
};

struct atmif_sys {
	ng_ID_t		atm_node;
	void		*regc;	/* cookie registration */
};

/*
 * Find the interface for a given node
 */
struct atmif *
atm_node2if(u_int node)
{
	struct atmif_priv *aif;

	if (node != 0)
		TAILQ_FOREACH(aif, &atmif_list, link)
			if (aif->sys->atm_node == node)
				return (&aif->pub);
	return (NULL);
}

u_int
atm_if2node(struct atmif *pub)
{
	struct atmif_priv *aif = (struct atmif_priv *)pub;

	return (aif->sys->atm_node);
}

/*
 * Destroy system dependend stuff.
 */
void
atmif_sys_destroy(struct atmif_priv *aif)
{

	ng_unregister_cookie(aif->sys->regc);
	free(aif->sys);
	free(aif->pub.mib);
}

/*
 * Handle a message from the ATM node
 */
static void
handle_atm_message(const struct ng_mesg *mesg, const char *path __unused,
    ng_ID_t node, void *uarg)
{
	struct atmif_priv *aif = uarg;
	enum atmif_carrier_state ost;

	switch (mesg->header.cmd) {

	  case NGM_ATM_IF_CHANGE:
	    {
		const struct ngm_atm_if_change *arg;

		ost = aif->pub.carrier;
		if (mesg->header.arglen != sizeof(*arg)) {
			syslog(LOG_ERR, "ATM_IF_CHANGE: wrong size");
			atmif_check_carrier(aif);
			return;
		}
		arg = (const struct ngm_atm_if_change *)
		    (const void *)mesg->data;

		if (arg->carrier)
			aif->pub.carrier = ATMIF_CARRIER_ON;
		else
			aif->pub.carrier = ATMIF_CARRIER_OFF;

		if (ost != aif->pub.carrier)
			atmif_send_notification(aif, ATMIF_NOTIFY_CARRIER,
			    (uintptr_t)ost);
		return;
	    }

	  case NGM_ATM_VCC_CHANGE:
	    {
		const struct ngm_atm_vcc_change *arg;

		if (mesg->header.arglen != sizeof(*arg)) {
			syslog(LOG_ERR, "ATM_VCC_CHANGE: wrong size");
			return;
		}
		arg = (const struct ngm_atm_vcc_change *)
		    (const void *)mesg->data;
		atmif_send_notification(aif, ATMIF_NOTIFY_VCC,
		    (uintptr_t)(((arg->vpi & 0xff) << 24) |
		    ((arg->vci & 0xffff) << 8) | (arg->state & 1)));
		return;
	    }
	}
	syslog(LOG_WARNING, "spurious message %u from node [%x]",
	    mesg->header.cmd, node);
}

/*
 * Attach to an ATM interface
 */
int
atmif_sys_attach_if(struct atmif_priv *aif)
{
	struct ng_mesg *resp, *resp1;
	struct namelist *list;
	u_int i;

	if ((aif->sys = malloc(sizeof(*aif->sys))) == NULL) {
		syslog(LOG_CRIT, "out of memory");
		return (-1);
	}
	memset(aif->sys, 0, sizeof(*aif->sys));

	if ((aif->pub.mib = malloc(sizeof(*aif->pub.mib))) == NULL) {
		free(aif->sys);
		syslog(LOG_CRIT, "out of memory");
		return (-1);
	}

	atmif_sys_fill_mib(aif);

	/*
	 * Get ATM node Id. Must do it the hard way by scanning all nodes
	 * because the name may be wrong.
	 */
	if ((resp = ng_dialog_id(snmp_node, NGM_GENERIC_COOKIE, NGM_LISTNODES,
	    NULL, 0)) == NULL) {
		syslog(LOG_ERR, "cannot fetch node list: %m");
		free(aif->sys);
		return (-1);
	}
	list = (struct namelist *)(void *)resp->data;

	for (i = 0; i < list->numnames; i++) {
		if (strcmp(list->nodeinfo[i].type, NG_ATM_NODE_TYPE) != 0)
			continue;
		if ((resp1 = ng_dialog_id(list->nodeinfo[i].id,
		    NGM_ATM_COOKIE, NGM_ATM_GET_IFNAME, NULL, 0)) == NULL)
			continue;
		if (strcmp(resp1->data, aif->pub.ifp->name) == 0) {
			free(resp1);
			break;
		}
		free(resp1);
	}
	if (i == list->numnames)
		aif->sys->atm_node = 0;
	else
		aif->sys->atm_node = list->nodeinfo[i].id;

	free(resp);

	if ((aif->sys->regc = ng_register_cookie(module, NGM_ATM_COOKIE,
	    aif->sys->atm_node, handle_atm_message, aif)) == NULL) {
		syslog(LOG_ERR, "cannot register cookie: %m");
		free(aif->sys);
		return (-1);
	}
	return (0);
}

/* 
 * Table of all ATM interfaces - Ng part
 */
int
op_atmif_ng(struct snmp_context *ctx __unused, struct snmp_value *value,
    u_int sub, u_int vindex __unused, enum snmp_op op)
{
	struct atmif_priv *aif;
	int err;

	if ((err = atmif_get_aif(value, sub, op, &aif)) != SNMP_ERR_NOERROR)
		return (err);

	if (op == SNMP_OP_SET) {
		switch (value->var.subs[sub - 1]) {

		  default:
			return (SNMP_ERR_NOT_WRITEABLE);
		}
	}

	switch (value->var.subs[sub - 1]) {

	  case LEAF_begemotAtmIfNodeId:
		value->v.uint32 = aif->sys->atm_node;
		return (SNMP_ERR_NOERROR);
	}
	abort();
}

/*
 * Get vendor string
 */
int
atm_sys_get_hw_vendor(struct atmif_priv *aif, struct snmp_value *value)
{

	if (aif->pub.mib->device >= sizeof(hwinfo) / sizeof(hwinfo[0]))
		return (string_get(value, "unknown", -1));
	return (string_get(value, hwinfo[aif->pub.mib->device].vendor, -1));
}

/*
 * Get device string
 */
int
atm_sys_get_hw_device(struct atmif_priv *aif, struct snmp_value *value)
{

	if (aif->pub.mib->device >= sizeof(hwinfo) / sizeof(hwinfo[0]))
		return (string_get(value, "unknown", -1));
	return (string_get(value, hwinfo[aif->pub.mib->device].device, -1));
}

/*
 * Extract the ATM MIB from the interface's private MIB
 */
void
atmif_sys_fill_mib(struct atmif_priv *aif)
{
	struct ifatm_mib *mib;

	if (aif->pub.ifp->specmiblen != sizeof(struct ifatm_mib)) {
		syslog(LOG_ERR, "atmif MIB has wrong size %zu",
		    aif->pub.ifp->specmiblen);
		memset(aif->pub.mib, 0, sizeof(*aif->pub.mib));
		aif->pub.mib->version = 0;
		return;
	}
	mib = (struct ifatm_mib *)aif->pub.ifp->specmib;

	aif->pub.mib->device = mib->device;
	aif->pub.mib->serial = mib->serial;
	aif->pub.mib->hw_version = mib->hw_version;
	aif->pub.mib->sw_version = mib->sw_version;
	aif->pub.mib->media = mib->media;

	memcpy(aif->pub.mib->esi, mib->esi, 6);
	aif->pub.mib->pcr = mib->pcr;
	aif->pub.mib->vpi_bits = mib->vpi_bits;
	aif->pub.mib->vci_bits = mib->vci_bits;
	aif->pub.mib->max_vpcs = mib->max_vpcs;
	aif->pub.mib->max_vccs = mib->max_vccs;
}