summaryrefslogtreecommitdiffstats
path: root/contrib/bsnmp/snmp_mibII/mibII_ip.c
blob: 11efe8274dd8679d5ffb358adddd6ffef83ee25b (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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
/*
 * Copyright (c) 2001-2003
 *	Fraunhofer Institute for Open Communication Systems (FhG Fokus).
 *	All rights reserved.
 *
 * Author: Harti 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.
 *
 * $Begemot: bsnmp/snmp_mibII/mibII_ip.c,v 1.11 2005/05/23 09:03:40 brandt_h Exp $
 *
 * ip group scalars.
 */
#include "mibII.h"
#include "mibII_oid.h"
#include <netinet/in_systm.h>
#include <netinet/ip.h>
#include <netinet/ip_var.h>
#include <netinet/ip_icmp.h>
#include <netinet/icmp_var.h>

static struct ipstat ipstat;
static u_int	ip_idrop;
static struct icmpstat icmpstat;

static int	ip_forwarding;
static int	ip_defttl;
static uint64_t ip_tick;

static uint64_t ipstat_tick;

static int
fetch_ipstat(void)
{
	size_t len;

	len = sizeof(ipstat);
	if (sysctlbyname("net.inet.ip.stats", &ipstat, &len, NULL, 0) == -1) {
		syslog(LOG_ERR, "net.inet.ip.stats: %m");
		return (-1);
	}
	if (len != sizeof(ipstat)) {
		syslog(LOG_ERR, "net.inet.ip.stats: wrong size");
		return (-1);
	}
	len = sizeof(ip_idrop);
	if (sysctlbyname("net.inet.ip.intr_queue_drops", &ip_idrop, &len, NULL, 0) == -1)
		syslog(LOG_WARNING, "net.inet.ip.intr_queue_drops: %m");
	if (len != sizeof(ip_idrop)) {
		syslog(LOG_WARNING, "net.inet.ip.intr_queue_drops: wrong size");
		ip_idrop = 0;
	}
	len = sizeof(icmpstat);
	if (sysctlbyname("net.inet.icmp.stats", &icmpstat, &len, NULL, 0) == -1) {
		syslog(LOG_ERR, "net.inet.icmp.stats: %m");
		return (-1);
	}
	if (len != sizeof(icmpstat)) {
		syslog(LOG_ERR, "net.inet.icmp.stats: wrong size");
		return (-1);
	}

	ipstat_tick = get_ticks();
	return (0);
}

static int
fetch_ip(void)
{
	size_t len;

	len = sizeof(ip_forwarding);
	if (sysctlbyname("net.inet.ip.forwarding", &ip_forwarding, &len,
	    NULL, 0) == -1) {
		syslog(LOG_ERR, "net.inet.ip.forwarding: %m");
		return (-1);
	}
	if (len != sizeof(ip_forwarding)) {
		syslog(LOG_ERR, "net.inet.ip.forwarding: wrong size");
		return (-1);
	}

	len = sizeof(ip_defttl);
	if (sysctlbyname("net.inet.ip.ttl", &ip_defttl, &len,
	    NULL, 0) == -1) {
		syslog(LOG_ERR, "net.inet.ip.ttl: %m");
		return (-1);
	}
	if (len != sizeof(ip_defttl)) {
		syslog(LOG_ERR, "net.inet.ip.ttl: wrong size");
		return (-1);
	}

	ip_tick = get_ticks();
	return (0);
}

static int
ip_forward(int forw, int *old)
{
	size_t olen;

	olen = sizeof(*old);
	if (sysctlbyname("net.inet.ip.forwarding", old, old ? &olen : NULL,
	    &forw, sizeof(forw)) == -1) {
		syslog(LOG_ERR, "set net.inet.ip.forwarding: %m");
		return (-1);
	}
	ip_forwarding = forw;
	return (0);
}

static int
ip_setttl(int ttl, int *old)
{
	size_t olen;

	olen = sizeof(*old);
	if (sysctlbyname("net.inet.ip.ttl", old, old ? &olen : NULL,
	    &ttl, sizeof(ttl)) == -1) {
		syslog(LOG_ERR, "set net.inet.ip.ttl: %m");
		return (-1);
	}
	ip_defttl = ttl;
	return (0);
}

/*
 * READ/WRITE ip group.
 */
int
op_ip(struct snmp_context *ctx, struct snmp_value *value,
    u_int sub, u_int idx __unused, enum snmp_op op)
{
	int old;

	switch (op) {

	  case SNMP_OP_GETNEXT:
		abort();

	  case SNMP_OP_GET:
		break;

	  case SNMP_OP_SET:
		if (ip_tick < this_tick)
			if (fetch_ip() == -1)
				return (SNMP_ERR_GENERR);

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

		  case LEAF_ipForwarding:
			ctx->scratch->int1 = ip_forwarding ? 1 : 2;
			ctx->scratch->int2 = value->v.integer;
			if (value->v.integer == 1) {
				if (!ip_forwarding && ip_forward(1, &old))
					return (SNMP_ERR_GENERR);
				ctx->scratch->int1 = old ? 1 : 2;
			} else if (value->v.integer == 2) {
				if (ip_forwarding && ip_forward(0, &old))
					return (SNMP_ERR_GENERR);
				ctx->scratch->int1 = old;
			} else
				return (SNMP_ERR_WRONG_VALUE);
			break;

		  case LEAF_ipDefaultTTL:
			ctx->scratch->int1 = ip_defttl;
			ctx->scratch->int2 = value->v.integer;
			if (value->v.integer < 1 || value->v.integer > 255)
				return (SNMP_ERR_WRONG_VALUE);
			if (ip_defttl != value->v.integer &&
			    ip_setttl(value->v.integer, &old))
				return (SNMP_ERR_GENERR);
			ctx->scratch->int1 = old;
			break;
		}
		return (SNMP_ERR_NOERROR);

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

		  case LEAF_ipForwarding:
			if (ctx->scratch->int1 == 1) {
				if (ctx->scratch->int2 == 2)
					(void)ip_forward(1, NULL);
			} else {
				if (ctx->scratch->int2 == 1)
					(void)ip_forward(0, NULL);
			}
			break;

		  case LEAF_ipDefaultTTL:
			if (ctx->scratch->int1 != ctx->scratch->int2)
				(void)ip_setttl(ctx->scratch->int1, NULL);
			break;
		}
		return (SNMP_ERR_NOERROR);

	  case SNMP_OP_COMMIT:
		return (SNMP_ERR_NOERROR);
	}

	if (ip_tick < this_tick)
		if (fetch_ip() == -1)
			return (SNMP_ERR_GENERR);

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

	  case LEAF_ipForwarding:
		value->v.integer = ip_forwarding ? 1 : 2;
		break;

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

/*
 * READ-ONLY statistics ip group.
 */
int
op_ipstat(struct snmp_context *ctx __unused, struct snmp_value *value,
    u_int sub, u_int idx __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 (ipstat_tick < this_tick)
		fetch_ipstat();

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

	  case LEAF_ipInReceives:
		value->v.uint32 = ipstat.ips_total;
		break;

	  case LEAF_ipInHdrErrors:
		value->v.uint32 = ipstat.ips_badsum + ipstat.ips_tooshort
		    + ipstat.ips_toosmall + ipstat.ips_badhlen
		    + ipstat.ips_badlen + ipstat.ips_badvers +
		    + ipstat.ips_toolong;
		break;

	  case LEAF_ipInAddrErrors:
		value->v.uint32 = ipstat.ips_cantforward;
		break;

	  case LEAF_ipForwDatagrams:
		value->v.uint32 = ipstat.ips_forward;
		break;

	  case LEAF_ipInUnknownProtos:
		value->v.uint32 = ipstat.ips_noproto;
		break;

	  case LEAF_ipInDiscards:
		value->v.uint32 = ip_idrop;
		break;

	  case LEAF_ipInDelivers:
		value->v.uint32 = ipstat.ips_delivered;
		break;

	  case LEAF_ipOutRequests:
		value->v.uint32 = ipstat.ips_localout;
		break;

	  case LEAF_ipOutDiscards:
		value->v.uint32 = ipstat.ips_odropped;
		break;

	  case LEAF_ipOutNoRoutes:
		value->v.uint32 = ipstat.ips_noroute;
		break;

	  case LEAF_ipReasmTimeout:
		value->v.integer = IPFRAGTTL;
		break;

	  case LEAF_ipReasmReqds:
		value->v.uint32 = ipstat.ips_fragments;
		break;

	  case LEAF_ipReasmOKs:
		value->v.uint32 = ipstat.ips_reassembled;
		break;

	  case LEAF_ipReasmFails:
		value->v.uint32 = ipstat.ips_fragdropped
		    + ipstat.ips_fragtimeout;
		break;

	  case LEAF_ipFragOKs:
		value->v.uint32 = ipstat.ips_fragmented;
		break;

	  case LEAF_ipFragFails:
		value->v.uint32 = ipstat.ips_cantfrag;
		break;

	  case LEAF_ipFragCreates:
		value->v.uint32 = ipstat.ips_ofragments;
		break;
	}
	return (SNMP_ERR_NOERROR);
}

/*
 * READ-ONLY statistics icmp group.
 */
int
op_icmpstat(struct snmp_context *ctx __unused, struct snmp_value *value,
    u_int sub, u_int idx __unused, enum snmp_op op)
{
	u_int i;

	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 (ipstat_tick < this_tick)
		fetch_ipstat();

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

	  case LEAF_icmpInMsgs:
		value->v.integer = 0;
		for (i = 0; i <= ICMP_MAXTYPE; i++)
			value->v.integer += icmpstat.icps_inhist[i];
		value->v.integer += icmpstat.icps_tooshort +
		    icmpstat.icps_checksum;
		/* missing: bad type and packets on faith */
		break;

	  case LEAF_icmpInErrors:
		value->v.integer = icmpstat.icps_tooshort +
		    icmpstat.icps_checksum +
		    icmpstat.icps_badlen +
		    icmpstat.icps_badcode +
		    icmpstat.icps_bmcastecho +
		    icmpstat.icps_bmcasttstamp;
		break;

	  case LEAF_icmpInDestUnreachs:
		value->v.integer = icmpstat.icps_inhist[ICMP_UNREACH];
		break;

	  case LEAF_icmpInTimeExcds:
		value->v.integer = icmpstat.icps_inhist[ICMP_TIMXCEED];
		break;

	  case LEAF_icmpInParmProbs:
		value->v.integer = icmpstat.icps_inhist[ICMP_PARAMPROB];
		break;

	  case LEAF_icmpInSrcQuenchs:
		value->v.integer = icmpstat.icps_inhist[ICMP_SOURCEQUENCH];
		break;

	  case LEAF_icmpInRedirects:
		value->v.integer = icmpstat.icps_inhist[ICMP_REDIRECT];
		break;

	  case LEAF_icmpInEchos:
		value->v.integer = icmpstat.icps_inhist[ICMP_ECHO];
		break;

	  case LEAF_icmpInEchoReps:
		value->v.integer = icmpstat.icps_inhist[ICMP_ECHOREPLY];
		break;

	  case LEAF_icmpInTimestamps:
		value->v.integer = icmpstat.icps_inhist[ICMP_TSTAMP];
		break;

	  case LEAF_icmpInTimestampReps:
		value->v.integer = icmpstat.icps_inhist[ICMP_TSTAMPREPLY];
		break;

	  case LEAF_icmpInAddrMasks:
		value->v.integer = icmpstat.icps_inhist[ICMP_MASKREQ];
		break;

	  case LEAF_icmpInAddrMaskReps:
		value->v.integer = icmpstat.icps_inhist[ICMP_MASKREPLY];
		break;

	  case LEAF_icmpOutMsgs:
		value->v.integer = 0;
		for (i = 0; i <= ICMP_MAXTYPE; i++)
			value->v.integer += icmpstat.icps_outhist[i];
		value->v.integer += icmpstat.icps_badaddr +
		    icmpstat.icps_noroute;
		break;

	  case LEAF_icmpOutErrors:
		value->v.integer = icmpstat.icps_badaddr +
		    icmpstat.icps_noroute;
		break;

	  case LEAF_icmpOutDestUnreachs:
		value->v.integer = icmpstat.icps_outhist[ICMP_UNREACH];
		break;

	  case LEAF_icmpOutTimeExcds:
		value->v.integer = icmpstat.icps_outhist[ICMP_TIMXCEED];
		break;

	  case LEAF_icmpOutParmProbs:
		value->v.integer = icmpstat.icps_outhist[ICMP_PARAMPROB];
		break;

	  case LEAF_icmpOutSrcQuenchs:
		value->v.integer = icmpstat.icps_outhist[ICMP_SOURCEQUENCH];
		break;

	  case LEAF_icmpOutRedirects:
		value->v.integer = icmpstat.icps_outhist[ICMP_REDIRECT];
		break;

	  case LEAF_icmpOutEchos:
		value->v.integer = icmpstat.icps_outhist[ICMP_ECHO];
		break;

	  case LEAF_icmpOutEchoReps:
		value->v.integer = icmpstat.icps_outhist[ICMP_ECHOREPLY];
		break;

	  case LEAF_icmpOutTimestamps:
		value->v.integer = icmpstat.icps_outhist[ICMP_TSTAMP];
		break;

	  case LEAF_icmpOutTimestampReps:
		value->v.integer = icmpstat.icps_outhist[ICMP_TSTAMPREPLY];
		break;

	  case LEAF_icmpOutAddrMasks:
		value->v.integer = icmpstat.icps_outhist[ICMP_MASKREQ];
		break;

	  case LEAF_icmpOutAddrMaskReps:
		value->v.integer = icmpstat.icps_outhist[ICMP_MASKREPLY];
		break;
	}
	return (SNMP_ERR_NOERROR);
}
OpenPOWER on IntegriCloud