2 * Copyright (c) 2001-2003
3 * Fraunhofer Institute for Open Communication Systems (FhG Fokus).
6 * Author: Harti Brandt <harti@freebsd.org>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * $Begemot: bsnmp/snmp_mibII/mibII_ip.c,v 1.11 2005/05/23 09:03:40 brandt_h Exp $
34 #include "mibII_oid.h"
35 #include <netinet/in_systm.h>
36 #include <netinet/ip.h>
37 #include <netinet/ip_var.h>
38 #include <netinet/ip_icmp.h>
39 #include <netinet/icmp_var.h>
41 static struct ipstat ipstat;
42 static u_int ip_idrop;
43 static struct icmpstat icmpstat;
45 static int ip_forwarding;
47 static uint64_t ip_tick;
49 static uint64_t ipstat_tick;
57 if (sysctlbyname("net.inet.ip.stats", &ipstat, &len, NULL, 0) == -1) {
58 syslog(LOG_ERR, "net.inet.ip.stats: %m");
61 if (len != sizeof(ipstat)) {
62 syslog(LOG_ERR, "net.inet.ip.stats: wrong size");
65 len = sizeof(ip_idrop);
66 if (sysctlbyname("net.inet.ip.intr_queue_drops", &ip_idrop, &len, NULL, 0) == -1)
67 syslog(LOG_WARNING, "net.inet.ip.intr_queue_drops: %m");
68 if (len != sizeof(ip_idrop)) {
69 syslog(LOG_WARNING, "net.inet.ip.intr_queue_drops: wrong size");
72 len = sizeof(icmpstat);
73 if (sysctlbyname("net.inet.icmp.stats", &icmpstat, &len, NULL, 0) == -1) {
74 syslog(LOG_ERR, "net.inet.icmp.stats: %m");
77 if (len != sizeof(icmpstat)) {
78 syslog(LOG_ERR, "net.inet.icmp.stats: wrong size");
82 ipstat_tick = get_ticks();
91 len = sizeof(ip_forwarding);
92 if (sysctlbyname("net.inet.ip.forwarding", &ip_forwarding, &len,
94 syslog(LOG_ERR, "net.inet.ip.forwarding: %m");
97 if (len != sizeof(ip_forwarding)) {
98 syslog(LOG_ERR, "net.inet.ip.forwarding: wrong size");
102 len = sizeof(ip_defttl);
103 if (sysctlbyname("net.inet.ip.ttl", &ip_defttl, &len,
105 syslog(LOG_ERR, "net.inet.ip.ttl: %m");
108 if (len != sizeof(ip_defttl)) {
109 syslog(LOG_ERR, "net.inet.ip.ttl: wrong size");
113 ip_tick = get_ticks();
118 ip_forward(int forw, int *old)
123 if (sysctlbyname("net.inet.ip.forwarding", old, old ? &olen : NULL,
124 &forw, sizeof(forw)) == -1) {
125 syslog(LOG_ERR, "set net.inet.ip.forwarding: %m");
128 ip_forwarding = forw;
133 ip_setttl(int ttl, int *old)
138 if (sysctlbyname("net.inet.ip.ttl", old, old ? &olen : NULL,
139 &ttl, sizeof(ttl)) == -1) {
140 syslog(LOG_ERR, "set net.inet.ip.ttl: %m");
148 * READ/WRITE ip group.
151 op_ip(struct snmp_context *ctx, struct snmp_value *value,
152 u_int sub, u_int idx __unused, enum snmp_op op)
158 case SNMP_OP_GETNEXT:
165 if (ip_tick < this_tick)
166 if (fetch_ip() == -1)
167 return (SNMP_ERR_GENERR);
169 switch (value->var.subs[sub - 1]) {
171 case LEAF_ipForwarding:
172 ctx->scratch->int1 = ip_forwarding ? 1 : 2;
173 ctx->scratch->int2 = value->v.integer;
174 if (value->v.integer == 1) {
175 if (!ip_forwarding && ip_forward(1, &old))
176 return (SNMP_ERR_GENERR);
177 ctx->scratch->int1 = old ? 1 : 2;
178 } else if (value->v.integer == 2) {
179 if (ip_forwarding && ip_forward(0, &old))
180 return (SNMP_ERR_GENERR);
181 ctx->scratch->int1 = old;
183 return (SNMP_ERR_WRONG_VALUE);
186 case LEAF_ipDefaultTTL:
187 ctx->scratch->int1 = ip_defttl;
188 ctx->scratch->int2 = value->v.integer;
189 if (value->v.integer < 1 || value->v.integer > 255)
190 return (SNMP_ERR_WRONG_VALUE);
191 if (ip_defttl != value->v.integer &&
192 ip_setttl(value->v.integer, &old))
193 return (SNMP_ERR_GENERR);
194 ctx->scratch->int1 = old;
197 return (SNMP_ERR_NOERROR);
199 case SNMP_OP_ROLLBACK:
200 switch (value->var.subs[sub - 1]) {
202 case LEAF_ipForwarding:
203 if (ctx->scratch->int1 == 1) {
204 if (ctx->scratch->int2 == 2)
205 (void)ip_forward(1, NULL);
207 if (ctx->scratch->int2 == 1)
208 (void)ip_forward(0, NULL);
212 case LEAF_ipDefaultTTL:
213 if (ctx->scratch->int1 != ctx->scratch->int2)
214 (void)ip_setttl(ctx->scratch->int1, NULL);
217 return (SNMP_ERR_NOERROR);
220 return (SNMP_ERR_NOERROR);
223 if (ip_tick < this_tick)
224 if (fetch_ip() == -1)
225 return (SNMP_ERR_GENERR);
227 switch (value->var.subs[sub - 1]) {
229 case LEAF_ipForwarding:
230 value->v.integer = ip_forwarding ? 1 : 2;
233 case LEAF_ipDefaultTTL:
234 value->v.integer = ip_defttl;
237 return (SNMP_ERR_NOERROR);
241 * READ-ONLY statistics ip group.
244 op_ipstat(struct snmp_context *ctx __unused, struct snmp_value *value,
245 u_int sub, u_int idx __unused, enum snmp_op op)
249 case SNMP_OP_GETNEXT:
256 return (SNMP_ERR_NOT_WRITEABLE);
258 case SNMP_OP_ROLLBACK:
263 if (ipstat_tick < this_tick)
266 switch (value->var.subs[sub - 1]) {
268 case LEAF_ipInReceives:
269 value->v.uint32 = ipstat.ips_total;
272 case LEAF_ipInHdrErrors:
273 value->v.uint32 = ipstat.ips_badsum + ipstat.ips_tooshort
274 + ipstat.ips_toosmall + ipstat.ips_badhlen
275 + ipstat.ips_badlen + ipstat.ips_badvers +
276 + ipstat.ips_toolong;
279 case LEAF_ipInAddrErrors:
280 value->v.uint32 = ipstat.ips_cantforward;
283 case LEAF_ipForwDatagrams:
284 value->v.uint32 = ipstat.ips_forward;
287 case LEAF_ipInUnknownProtos:
288 value->v.uint32 = ipstat.ips_noproto;
291 case LEAF_ipInDiscards:
292 value->v.uint32 = ip_idrop;
295 case LEAF_ipInDelivers:
296 value->v.uint32 = ipstat.ips_delivered;
299 case LEAF_ipOutRequests:
300 value->v.uint32 = ipstat.ips_localout;
303 case LEAF_ipOutDiscards:
304 value->v.uint32 = ipstat.ips_odropped;
307 case LEAF_ipOutNoRoutes:
308 value->v.uint32 = ipstat.ips_noroute;
311 case LEAF_ipReasmTimeout:
312 value->v.integer = IPFRAGTTL;
315 case LEAF_ipReasmReqds:
316 value->v.uint32 = ipstat.ips_fragments;
319 case LEAF_ipReasmOKs:
320 value->v.uint32 = ipstat.ips_reassembled;
323 case LEAF_ipReasmFails:
324 value->v.uint32 = ipstat.ips_fragdropped
325 + ipstat.ips_fragtimeout;
329 value->v.uint32 = ipstat.ips_fragmented;
332 case LEAF_ipFragFails:
333 value->v.uint32 = ipstat.ips_cantfrag;
336 case LEAF_ipFragCreates:
337 value->v.uint32 = ipstat.ips_ofragments;
340 return (SNMP_ERR_NOERROR);
344 * READ-ONLY statistics icmp group.
347 op_icmpstat(struct snmp_context *ctx __unused, struct snmp_value *value,
348 u_int sub, u_int idx __unused, enum snmp_op op)
354 case SNMP_OP_GETNEXT:
361 return (SNMP_ERR_NOT_WRITEABLE);
363 case SNMP_OP_ROLLBACK:
368 if (ipstat_tick < this_tick)
371 switch (value->var.subs[sub - 1]) {
373 case LEAF_icmpInMsgs:
374 value->v.integer = 0;
375 for (i = 0; i <= ICMP_MAXTYPE; i++)
376 value->v.integer += icmpstat.icps_inhist[i];
377 value->v.integer += icmpstat.icps_tooshort +
378 icmpstat.icps_checksum;
379 /* missing: bad type and packets on faith */
382 case LEAF_icmpInErrors:
383 value->v.integer = icmpstat.icps_tooshort +
384 icmpstat.icps_checksum +
385 icmpstat.icps_badlen +
386 icmpstat.icps_badcode +
387 icmpstat.icps_bmcastecho +
388 icmpstat.icps_bmcasttstamp;
391 case LEAF_icmpInDestUnreachs:
392 value->v.integer = icmpstat.icps_inhist[ICMP_UNREACH];
395 case LEAF_icmpInTimeExcds:
396 value->v.integer = icmpstat.icps_inhist[ICMP_TIMXCEED];
399 case LEAF_icmpInParmProbs:
400 value->v.integer = icmpstat.icps_inhist[ICMP_PARAMPROB];
403 case LEAF_icmpInSrcQuenchs:
404 value->v.integer = icmpstat.icps_inhist[ICMP_SOURCEQUENCH];
407 case LEAF_icmpInRedirects:
408 value->v.integer = icmpstat.icps_inhist[ICMP_REDIRECT];
411 case LEAF_icmpInEchos:
412 value->v.integer = icmpstat.icps_inhist[ICMP_ECHO];
415 case LEAF_icmpInEchoReps:
416 value->v.integer = icmpstat.icps_inhist[ICMP_ECHOREPLY];
419 case LEAF_icmpInTimestamps:
420 value->v.integer = icmpstat.icps_inhist[ICMP_TSTAMP];
423 case LEAF_icmpInTimestampReps:
424 value->v.integer = icmpstat.icps_inhist[ICMP_TSTAMPREPLY];
427 case LEAF_icmpInAddrMasks:
428 value->v.integer = icmpstat.icps_inhist[ICMP_MASKREQ];
431 case LEAF_icmpInAddrMaskReps:
432 value->v.integer = icmpstat.icps_inhist[ICMP_MASKREPLY];
435 case LEAF_icmpOutMsgs:
436 value->v.integer = 0;
437 for (i = 0; i <= ICMP_MAXTYPE; i++)
438 value->v.integer += icmpstat.icps_outhist[i];
439 value->v.integer += icmpstat.icps_badaddr +
440 icmpstat.icps_noroute;
443 case LEAF_icmpOutErrors:
444 value->v.integer = icmpstat.icps_badaddr +
445 icmpstat.icps_noroute;
448 case LEAF_icmpOutDestUnreachs:
449 value->v.integer = icmpstat.icps_outhist[ICMP_UNREACH];
452 case LEAF_icmpOutTimeExcds:
453 value->v.integer = icmpstat.icps_outhist[ICMP_TIMXCEED];
456 case LEAF_icmpOutParmProbs:
457 value->v.integer = icmpstat.icps_outhist[ICMP_PARAMPROB];
460 case LEAF_icmpOutSrcQuenchs:
461 value->v.integer = icmpstat.icps_outhist[ICMP_SOURCEQUENCH];
464 case LEAF_icmpOutRedirects:
465 value->v.integer = icmpstat.icps_outhist[ICMP_REDIRECT];
468 case LEAF_icmpOutEchos:
469 value->v.integer = icmpstat.icps_outhist[ICMP_ECHO];
472 case LEAF_icmpOutEchoReps:
473 value->v.integer = icmpstat.icps_outhist[ICMP_ECHOREPLY];
476 case LEAF_icmpOutTimestamps:
477 value->v.integer = icmpstat.icps_outhist[ICMP_TSTAMP];
480 case LEAF_icmpOutTimestampReps:
481 value->v.integer = icmpstat.icps_outhist[ICMP_TSTAMPREPLY];
484 case LEAF_icmpOutAddrMasks:
485 value->v.integer = icmpstat.icps_outhist[ICMP_MASKREQ];
488 case LEAF_icmpOutAddrMaskReps:
489 value->v.integer = icmpstat.icps_outhist[ICMP_MASKREPLY];
492 return (SNMP_ERR_NOERROR);