summaryrefslogtreecommitdiffstats
path: root/contrib/bsnmp/snmp_mibII/mibII_route.c
blob: e220de9af12921c4a455c2faf7d01fdd4feea22e (plain)
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
/*
 * Copyright (c) 2001-2003
 *	Fraunhofer Institute for Open Communication Systems (FhG Fokus).
 *	All rights reserved.
 *
 * Author: Harti Brandt <harti@freebsd.org>
 *
 * Redistribution of this software and documentation 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 or documentation 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.
 * 3. Neither the name of the Institute nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE AND DOCUMENTATION IS PROVIDED BY FRAUNHOFER FOKUS
 * AND ITS 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
 * FRAUNHOFER FOKUS OR ITS 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.
 *
 * $Begemot: bsnmp/snmp_mibII/mibII_route.c,v 1.4 2003/12/03 10:01:19 hbb Exp $
 *
 * Routing table
 */
#include "mibII.h"
#include "mibII_oid.h"

struct sroute {
	TAILQ_ENTRY(sroute) link;
	struct asn_oid	index;
	u_int		ifindex;
	u_int		type;
	u_int		proto;
};
static TAILQ_HEAD(, sroute) sroute_list = TAILQ_HEAD_INITIALIZER(sroute_list);

static u_int32_t route_tick;
static u_int route_total;

static int
fetch_route(void)
{
	u_char *rtab, *next;
	size_t len;
	struct sroute *r;
	struct rt_msghdr *rtm;
	struct sockaddr *addrs[RTAX_MAX];
	struct sockaddr_in *sa, *gw;
	struct in_addr mask, nhop;
	in_addr_t ha;
	struct mibif *ifp;

	while ((r = TAILQ_FIRST(&sroute_list)) != NULL) {
		TAILQ_REMOVE(&sroute_list, r, link);
		free(r);
	}
	route_total = 0;

	if ((rtab = mib_fetch_rtab(AF_INET, NET_RT_DUMP, 0, &len)) == NULL)
		return (-1);

	next = rtab;
	for (next = rtab; next < rtab + len; next += rtm->rtm_msglen) {
		rtm = (struct rt_msghdr *)(void *)next;
		if (rtm->rtm_type != RTM_GET ||
		    !(rtm->rtm_flags & RTF_UP))
			continue;
		mib_extract_addrs(rtm->rtm_addrs, (u_char *)(rtm + 1), addrs);

		if (addrs[RTAX_DST] == NULL || addrs[RTAX_GATEWAY] == NULL ||
		    addrs[RTAX_DST]->sa_family != AF_INET)
			continue;

		sa = (struct sockaddr_in *)(void *)addrs[RTAX_DST];

		if (rtm->rtm_flags & RTF_HOST) {
			mask.s_addr = 0xffffffff;
		} else {
			if (addrs[RTAX_NETMASK] == NULL ||
			    addrs[RTAX_NETMASK]->sa_len == 0)
				mask.s_addr = 0;
			else
				mask = ((struct sockaddr_in *)(void *)
				    addrs[RTAX_NETMASK])->sin_addr;
		}
		if (addrs[RTAX_GATEWAY] == NULL) {
			nhop.s_addr = 0;
		} else if (rtm->rtm_flags & RTF_LLINFO) {
			nhop = sa->sin_addr;
		} else {
			gw = (struct sockaddr_in *)(void *)addrs[RTAX_GATEWAY];
			if (gw->sin_family != AF_INET)
				continue;
			nhop = gw->sin_addr;
		}
		if ((ifp = mib_find_if_sys(rtm->rtm_index)) == NULL) {
			mib_iflist_bad = 1;
			continue;
		}

		if ((r = malloc(sizeof(*r))) == NULL) {
			syslog(LOG_ERR, "%m");
			continue;
		}

		route_total++;

		r->index.len = 13;
		ha = ntohl(sa->sin_addr.s_addr);
		r->index.subs[0] = (ha >> 24) & 0xff;
		r->index.subs[1] = (ha >> 16) & 0xff;
		r->index.subs[2] = (ha >>  8) & 0xff;
		r->index.subs[3] = (ha >>  0) & 0xff;
		ha = ntohl(mask.s_addr);
		r->index.subs[4] = (ha >> 24) & 0xff;
		r->index.subs[5] = (ha >> 16) & 0xff;
		r->index.subs[6] = (ha >>  8) & 0xff;
		r->index.subs[7] = (ha >>  0) & 0xff;

		r->index.subs[8] = 0;

		ha = ntohl(nhop.s_addr);
		r->index.subs[9] = (ha >> 24) & 0xff;
		r->index.subs[10] = (ha >> 16) & 0xff;
		r->index.subs[11] = (ha >>  8) & 0xff;
		r->index.subs[12] = (ha >>  0) & 0xff;

		r->ifindex = ifp->index;

		r->type = (rtm->rtm_flags & RTF_LLINFO) ? 3 :
		    (rtm->rtm_flags & RTF_REJECT) ? 2 : 4;

		/* cannot really know, what protocol it runs */
		r->proto = (rtm->rtm_flags & RTF_LOCAL) ? 2 :
		    (rtm->rtm_flags & RTF_STATIC) ? 3 :
		    (rtm->rtm_flags & RTF_DYNAMIC) ? 4 : 10;

		INSERT_OBJECT_OID(r, &sroute_list);
	}

	free(rtab);
	route_tick = get_ticks();

	return (0);
}

/*
 * Table
 */
int
op_route_table(struct snmp_context *ctx __unused, struct snmp_value *value,
    u_int sub, u_int iidx __unused, enum snmp_op op)
{
	static struct sroute *r;

	if (route_tick < this_tick)
		if (fetch_route() == -1)
			return (SNMP_ERR_GENERR);

	switch (op) {

	  case SNMP_OP_GETNEXT:
		if ((r = NEXT_OBJECT_OID(&sroute_list, &value->var, sub)) == NULL)
			return (SNMP_ERR_NOSUCHNAME);
		index_append(&value->var, sub, &r->index);
		break;

	  case SNMP_OP_GET:
		if ((r = FIND_OBJECT_OID(&sroute_list, &value->var, sub)) == NULL)
			return (SNMP_ERR_NOSUCHNAME);
		break;

	  case SNMP_OP_SET:
		if ((r = FIND_OBJECT_OID(&sroute_list, &value->var, sub)) == NULL)
			return (SNMP_ERR_NO_CREATION);
		return (SNMP_ERR_NOT_WRITEABLE);

	  case SNMP_OP_ROLLBACK:
	  case SNMP_OP_COMMIT:
		abort();

	  default:
		abort();
	}

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

	  case LEAF_ipCidrRouteDest:
		value->v.ipaddress[0] = r->index.subs[0];
		value->v.ipaddress[1] = r->index.subs[1];
		value->v.ipaddress[2] = r->index.subs[2];
		value->v.ipaddress[3] = r->index.subs[3];
		break;

	  case LEAF_ipCidrRouteMask:
		value->v.ipaddress[0] = r->index.subs[4];
		value->v.ipaddress[1] = r->index.subs[5];
		value->v.ipaddress[2] = r->index.subs[6];
		value->v.ipaddress[3] = r->index.subs[7];
		break;

	  case LEAF_ipCidrRouteTos:
		value->v.integer = r->index.subs[8];
		break;

	  case LEAF_ipCidrRouteNextHop:
		value->v.ipaddress[0] = r->index.subs[9];
		value->v.ipaddress[1] = r->index.subs[10];
		value->v.ipaddress[2] = r->index.subs[11];
		value->v.ipaddress[3] = r->index.subs[12];
		break;

	  case LEAF_ipCidrRouteIfIndex:
		value->v.integer = r->ifindex;
		break;

	  case LEAF_ipCidrRouteType:
		value->v.integer = r->type;
		break;

	  case LEAF_ipCidrRouteProto:
		value->v.integer = r->proto;
		break;

	  case LEAF_ipCidrRouteAge:
		value->v.integer = 0;
		break;

	  case LEAF_ipCidrRouteInfo:
		value->v.oid = oid_zeroDotZero;
		break;

	  case LEAF_ipCidrRouteNextHopAS:
		value->v.integer = 0;
		break;

	  case LEAF_ipCidrRouteMetric1:
	  case LEAF_ipCidrRouteMetric2:
	  case LEAF_ipCidrRouteMetric3:
	  case LEAF_ipCidrRouteMetric4:
	  case LEAF_ipCidrRouteMetric5:
		value->v.integer = -1;
		break;

	  case LEAF_ipCidrRouteStatus:
		value->v.integer = 1;
		break;
	}
	return (SNMP_ERR_NOERROR);
}

/*
 * scalars
 */
int
op_route(struct snmp_context *ctx __unused, struct snmp_value *value,
    u_int sub, u_int iidx __unused, enum snmp_op op)
{
	switch (op) {

	  case SNMP_OP_GETNEXT:
		abort();

	  case SNMP_OP_GET:
		break;

	  case SNMP_OP_SET:
		return (SNMP_ERR_NOT_WRITEABLE);

	  case SNMP_OP_ROLLBACK:
	  case SNMP_OP_COMMIT:
		abort();
	}

	if (route_tick < this_tick)
		if (fetch_route() == -1)
			return (SNMP_ERR_GENERR);

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

	  case LEAF_ipCidrRouteNumber:
		value->v.uint32 = route_total;
		break;

	}
	return (SNMP_ERR_NOERROR);
}
OpenPOWER on IntegriCloud