Debian Codegraph @

agent/mibgroup/mibII/ip.c

raw
1 /*
2 * IP MIB group implementation - ip.c
3 *
4 */
5
6 #include <net-snmp/net-snmp-config.h>
7 #include "mibII_common.h"
8
9 #ifdef HAVE_SYS_HASHING_H
10 #include <sys/hashing.h>
11 #endif
12 #ifdef HAVE_NETINET_IN_VAR_H
13 #include <netinet/in_var.h>
14 #endif
15 #ifdef HAVE_SYSLOG_H
16 #include <syslog.h>
17 #endif
18
19 #include <net-snmp/net-snmp-includes.h>
20 #include <net-snmp/agent/net-snmp-agent-includes.h>
21 #include <net-snmp/agent/auto_nlist.h>
22 #include <net-snmp/agent/sysORTable.h>
23 #include <net-snmp/data_access/ip_scalars.h>
24
25 #include "util_funcs/MIB_STATS_CACHE_TIMEOUT.h"
26 #include "ip.h"
27 #include "ipAddr.h"
28 #include "interfaces.h"
29
30 #ifndef MIB_STATS_CACHE_TIMEOUT
31 #define MIB_STATS_CACHE_TIMEOUT 5
32 #endif
33 #ifndef IP_STATS_CACHE_TIMEOUT
34 #define IP_STATS_CACHE_TIMEOUT MIB_STATS_CACHE_TIMEOUT
35 #endif
36
37 #if defined(HAVE_LIBPERFSTAT_H) && (defined(aix4) || defined(aix5) || defined(aix6) || defined(aix7)) && !defined(FIRST_PROTOCOL)
38 #ifdef HAVE_SYS_PROTOSW_H
39 #include <sys/protosw.h>
40 #endif
41 #include <libperfstat.h>
42 #ifdef FIRST_PROTOCOL
43 perfstat_protocol_t ps_proto;
44 perfstat_id_t ps_name;
45 #define _USE_PERFSTAT_PROTOCOL 1
46 #endif
47 #endif
48
49 /*********************
50 *
51 * Kernel & interface information,
52 * and internal forward declarations
53 *
54 *********************/
55
56
57 /*********************
58 *
59 * Initialisation & common implementation functions
60 *
61 *********************/
62
63 /*
64 * define the structure we're going to ask the agent to register our
65 * information at
66 */
67 struct variable1 ipaddr_variables[] = {
68 {IPADADDR, ASN_IPADDRESS, NETSNMP_OLDAPI_RONLY,
69 var_ipAddrEntry, 1, {1}},
70 {IPADIFINDEX, ASN_INTEGER, NETSNMP_OLDAPI_RONLY,
71 var_ipAddrEntry, 1, {2}},
72 #ifndef sunV3
73 {IPADNETMASK, ASN_IPADDRESS, NETSNMP_OLDAPI_RONLY,
74 var_ipAddrEntry, 1, {3}},
75 #endif
76 {IPADBCASTADDR, ASN_INTEGER, NETSNMP_OLDAPI_RONLY,
77 var_ipAddrEntry, 1, {4}},
78 {IPADREASMMAX, ASN_INTEGER, NETSNMP_OLDAPI_RONLY,
79 var_ipAddrEntry, 1, {5}}
80 };
81
82 struct variable1 iproute_variables[] = {
83 {IPROUTEDEST, ASN_IPADDRESS, NETSNMP_OLDAPI_RWRITE,
84 var_ipRouteEntry, 1, {1}},
85 {IPROUTEIFINDEX, ASN_INTEGER, NETSNMP_OLDAPI_RWRITE,
86 var_ipRouteEntry, 1, {2}},
87 {IPROUTEMETRIC1, ASN_INTEGER, NETSNMP_OLDAPI_RWRITE,
88 var_ipRouteEntry, 1, {3}},
89 {IPROUTEMETRIC2, ASN_INTEGER, NETSNMP_OLDAPI_RWRITE,
90 var_ipRouteEntry, 1, {4}},
91 {IPROUTEMETRIC3, ASN_INTEGER, NETSNMP_OLDAPI_RWRITE,
92 var_ipRouteEntry, 1, {5}},
93 {IPROUTEMETRIC4, ASN_INTEGER, NETSNMP_OLDAPI_RWRITE,
94 var_ipRouteEntry, 1, {6}},
95 {IPROUTENEXTHOP, ASN_IPADDRESS, NETSNMP_OLDAPI_RWRITE,
96 var_ipRouteEntry, 1, {7}},
97 {IPROUTETYPE, ASN_INTEGER, NETSNMP_OLDAPI_RWRITE,
98 var_ipRouteEntry, 1, {8}},
99 {IPROUTEPROTO, ASN_INTEGER, NETSNMP_OLDAPI_RONLY,
100 var_ipRouteEntry, 1, {9}},
101 {IPROUTEAGE, ASN_INTEGER, NETSNMP_OLDAPI_RWRITE,
102 var_ipRouteEntry, 1, {10}},
103 {IPROUTEMASK, ASN_IPADDRESS, NETSNMP_OLDAPI_RWRITE,
104 var_ipRouteEntry, 1, {11}},
105 {IPROUTEMETRIC5, ASN_INTEGER, NETSNMP_OLDAPI_RWRITE,
106 var_ipRouteEntry, 1, {12}},
107 {IPROUTEINFO, ASN_OBJECT_ID, NETSNMP_OLDAPI_RONLY,
108 var_ipRouteEntry, 1, {13}}
109 };
110
111 struct variable1 ipmedia_variables[] = {
112 #ifdef USING_MIBII_AT_MODULE
113 #if defined (WIN32) || defined (cygwin)
114 {IPMEDIAIFINDEX, ASN_INTEGER, NETSNMP_OLDAPI_RWRITE,
115 var_atEntry, 1, {1}},
116 {IPMEDIAPHYSADDRESS, ASN_OCTET_STR, NETSNMP_OLDAPI_RWRITE,
117 var_atEntry, 1, {2}},
118 {IPMEDIANETADDRESS, ASN_IPADDRESS, NETSNMP_OLDAPI_RWRITE,
119 var_atEntry, 1, {3}},
120 {IPMEDIATYPE, ASN_INTEGER, NETSNMP_OLDAPI_RWRITE,
121 var_atEntry, 1, {4}}
122 #else
123 {IPMEDIAIFINDEX, ASN_INTEGER, NETSNMP_OLDAPI_RONLY,
124 var_atEntry, 1, {1}},
125 {IPMEDIAPHYSADDRESS, ASN_OCTET_STR, NETSNMP_OLDAPI_RONLY,
126 var_atEntry, 1, {2}},
127 {IPMEDIANETADDRESS, ASN_IPADDRESS, NETSNMP_OLDAPI_RONLY,
128 var_atEntry, 1, {3}},
129 {IPMEDIATYPE, ASN_INTEGER, NETSNMP_OLDAPI_RONLY,
130 var_atEntry, 1, {4}}
131 #endif
132 #endif
133 };
134
135 /*
136 * Define the OID pointer to the top of the mib tree that we're
137 * registering underneath, and the OID of the MIB module
138 */
139 oid ip_oid[] = { SNMP_OID_MIB2, 4 };
140
141 oid ipaddr_variables_oid[] = { SNMP_OID_MIB2, 4, 20, 1 };
142 oid iproute_variables_oid[] = { SNMP_OID_MIB2, 4, 21, 1 };
143 oid ipmedia_variables_oid[] = { SNMP_OID_MIB2, 4, 22, 1 };
144 oid ip_module_oid[] = { SNMP_OID_MIB2, 4 };
145 int ip_module_oid_len = sizeof(ip_module_oid) / sizeof(oid);
146 int ip_module_count = 0; /* Need to liaise with icmp.c */
147
148 void
149 init_ip(void)
150 {
151 netsnmp_handler_registration *reginfo;
152 int rc;
153
154 /*
155 * register ourselves with the agent as a group of scalars...
156 */
157 DEBUGMSGTL(("mibII/ip", "Initialising IP group\n"));
158 reginfo = netsnmp_create_handler_registration("ip", ip_handler,
159 ip_oid, OID_LENGTH(ip_oid), HANDLER_CAN_RONLY);
160 rc = netsnmp_register_scalar_group(reginfo, IPFORWARDING, IPROUTEDISCARDS);
161 if (rc != SNMPERR_SUCCESS)
162 return;
163
164 /*
165 * .... with a local cache
166 * (except for HP-UX 11, which extracts objects individually)
167 */
168 #ifndef hpux11
169 netsnmp_inject_handler( reginfo,
170 netsnmp_get_cache_handler(IP_STATS_CACHE_TIMEOUT,
171 ip_load, ip_free,
172 ip_oid, OID_LENGTH(ip_oid)));
173 #endif
174
175 /*
176 * register (using the old-style API) to handle the IP tables
177 */
178 REGISTER_MIB("mibII/ipaddr", ipaddr_variables,
179 variable1, ipaddr_variables_oid);
180 REGISTER_MIB("mibII/iproute", iproute_variables,
181 variable1, iproute_variables_oid);
182 REGISTER_MIB("mibII/ipmedia", ipmedia_variables,
183 variable1, ipmedia_variables_oid);
184 if (++ip_module_count == 2)
185 REGISTER_SYSOR_ENTRY(ip_module_oid,
186 "The MIB module for managing IP and ICMP implementations");
187
188
189 /*
190 * for speed optimization, we call this now to do the lookup
191 */
192 #ifndef _USE_PERFSTAT_PROTOCOL
193 #ifdef IPSTAT_SYMBOL
194 auto_nlist(IPSTAT_SYMBOL, 0, 0);
195 #endif
196 #ifdef IP_FORWARDING_SYMBOL
197 auto_nlist(IP_FORWARDING_SYMBOL, 0, 0);
198 #endif
199 #ifdef TCP_TTL_SYMBOL
200 auto_nlist(TCP_TTL_SYMBOL, 0, 0);
201 #endif
202 #ifdef MIB_IPCOUNTER_SYMBOL
203 auto_nlist(MIB_IPCOUNTER_SYMBOL, 0, 0);
204 #endif
205 #ifdef solaris2
206 init_kernel_sunos5();
207 #endif
208 #endif
209 }
210
211
212 /*********************
213 *
214 * System specific data formats
215 *
216 *********************/
217
218 #ifdef hpux11
219 #define IP_STAT_STRUCTURE int
220 #endif
221
222 #ifdef linux
223 #define IP_STAT_STRUCTURE struct ip_mib
224 #define USES_SNMP_DESIGNED_IPSTAT
225 #endif
226
227 #ifdef solaris2
228 #define IP_STAT_STRUCTURE mib2_ip_t
229 #define USES_SNMP_DESIGNED_IPSTAT
230 #endif
231
232 #ifdef NETBSD_STATS_VIA_SYSCTL
233 #define IP_STAT_STRUCTURE struct ip_mib
234 #define USES_SNMP_DESIGNED_IPSTAT
235 #undef IP_NSTATS
236 #endif
237
238 #ifdef HAVE_IPHLPAPI_H
239 #include <iphlpapi.h>
240 #define IP_STAT_STRUCTURE MIB_IPSTATS
241 long ipForwarding;
242 long oldipForwarding;
243 long ipTTL, oldipTTL;
244 #endif /* WIN32 cygwin */
245
246 #ifdef HAVE_SYS_TCPIPSTATS_H
247 #define IP_STAT_STRUCTURE struct kna
248 #define USES_TRADITIONAL_IPSTAT
249 #endif
250
251 #ifdef IP_NSTATS
252 typedef struct ipstat {
253 uint64_t st[IP_NSTATS];
254 };
255 #define IP_STAT_STRUCTURE struct ipstat
256 #endif
257
258 #ifdef dragonfly
259 #define IP_STAT_STRUCTURE struct ip_stats
260 #define USES_TRADITIONAL_IPSTAT
261 #endif
262
263 #if !defined(IP_STAT_STRUCTURE)
264 #define IP_STAT_STRUCTURE struct ipstat
265 #define USES_TRADITIONAL_IPSTAT
266 #endif
267
268 IP_STAT_STRUCTURE ipstat;
269
270
271
272 /*********************
273 *
274 * System independent handler
275 * (mostly)
276 *
277 *********************/
278
279
280 int
281 ip_handler(netsnmp_mib_handler *handler,
282 netsnmp_handler_registration *reginfo,
283 netsnmp_agent_request_info *reqinfo,
284 netsnmp_request_info *requests)
285 {
286 netsnmp_request_info *request;
287 netsnmp_variable_list *requestvb;
288 long ret_value = 0;
289 oid subid;
290 int type = ASN_COUNTER;
291
292 /*
293 * The cached data should already have been loaded by the
294 * cache handler, higher up the handler chain.
295 */
296 #ifdef _USE_PERFSTAT_PROTOCOL
297 ip_load(NULL, NULL);
298 #endif
299
300
301 /*
302 *
303 *
304 */
305 DEBUGMSGTL(("mibII/ip", "Handler - mode %s\n",
306 se_find_label_in_slist("agent_mode", reqinfo->mode)));
307 switch (reqinfo->mode) {
308 case MODE_GET:
309 for (request=requests; request; request=request->next) {
310 requestvb = request->requestvb;
311 subid = requestvb->name[OID_LENGTH(ip_oid)]; /* XXX */
312 DEBUGMSGTL(( "mibII/ip", "oid: "));
313 DEBUGMSGOID(("mibII/ip", requestvb->name,
314 requestvb->name_length));
315 DEBUGMSG(( "mibII/ip", "\n"));
316
317 switch (subid) {
318 #ifdef USES_SNMP_DESIGNED_IPSTAT
319 case IPFORWARDING:
320 ret_value = ipstat.ipForwarding;
321 type = ASN_INTEGER;
322 break;
323 case IPDEFAULTTTL:
324 ret_value = ipstat.ipDefaultTTL;
325 type = ASN_INTEGER;
326 break;
327 case IPINRECEIVES:
328 ret_value = ipstat.ipInReceives & 0xffffffff;
329 break;
330 case IPINHDRERRORS:
331 ret_value = ipstat.ipInHdrErrors;
332 break;
333 case IPINADDRERRORS:
334 ret_value = ipstat.ipInAddrErrors;
335 break;
336 case IPFORWDATAGRAMS:
337 ret_value = ipstat.ipForwDatagrams;
338 break;
339 case IPINUNKNOWNPROTOS:
340 ret_value = ipstat.ipInUnknownProtos;
341 break;
342 case IPINDISCARDS:
343 ret_value = ipstat.ipInDiscards;
344 break;
345 case IPINDELIVERS:
346 ret_value = ipstat.ipInDelivers & 0xffffffff;
347 break;
348 case IPOUTREQUESTS:
349 ret_value = ipstat.ipOutRequests & 0xffffffff;
350 break;
351 case IPOUTDISCARDS:
352 ret_value = ipstat.ipOutDiscards;
353 break;
354 case IPOUTNOROUTES:
355 ret_value = ipstat.ipOutNoRoutes;
356 break;
357 case IPREASMTIMEOUT:
358 ret_value = ipstat.ipReasmTimeout;
359 type = ASN_INTEGER;
360 break;
361 case IPREASMREQDS:
362 ret_value = ipstat.ipReasmReqds;
363 break;
364 case IPREASMOKS:
365 ret_value = ipstat.ipReasmOKs;
366 break;
367 case IPREASMFAILS:
368 ret_value = ipstat.ipReasmFails;
369 break;
370 case IPFRAGOKS:
371 ret_value = ipstat.ipFragOKs;
372 break;
373 case IPFRAGFAILS:
374 ret_value = ipstat.ipFragFails;
375 break;
376 case IPFRAGCREATES:
377 ret_value = ipstat.ipFragCreates;
378 break;
379 case IPROUTEDISCARDS:
380 ret_value = ipstat.ipRoutingDiscards;
381 break;
382
383 #elif defined(USES_TRADITIONAL_IPSTAT) && !defined(_USE_PERFSTAT_PROTOCOL)
384 #ifdef HAVE_SYS_TCPIPSTATS_H
385 /*
386 * This actually reads statistics for *all* the groups together,
387 * so we need to isolate the IP-specific bits.
388 */
389 #define ipstat ipstat.ipstat
390 #endif
391 case IPFORWARDING:
392 case IPDEFAULTTTL:
393 /*
394 * Query these two individually
395 */
396 ret_value = ip_load(NULL, (void *)subid);
397 if (ret_value == -1 ) {
398 netsnmp_set_request_error(reqinfo, request, SNMP_NOSUCHOBJECT);
399 continue;
400 }
401 type = ASN_INTEGER;
402 break;
403 case IPINRECEIVES:
404 ret_value = ipstat.ips_total & 0xffffffff;
405 break;
406 case IPINHDRERRORS:
407 ret_value = ipstat.ips_badsum
408 + ipstat.ips_tooshort
409 + ipstat.ips_toosmall + ipstat.ips_badhlen + ipstat.ips_badlen;
410 break;
411 case IPINADDRERRORS:
412 ret_value = ipstat.ips_cantforward;
413 break;
414 case IPFORWDATAGRAMS:
415 ret_value = ipstat.ips_forward;
416 break;
417 case IPINUNKNOWNPROTOS:
418 #ifdef HAVE_STRUCT_IPSTAT_IPS_NOPROTO
419 ret_value = ipstat.ips_noproto;
420 break;
421 #else
422 netsnmp_set_request_error(reqinfo, request, SNMP_NOSUCHOBJECT);
423 continue;
424 #endif
425 case IPINDISCARDS:
426 #ifdef HAVE_STRUCT_IPSTAT_IPS_FRAGDROPPED
427 ret_value = ipstat.ips_fragdropped; /* ?? */
428 break;
429 #else
430 netsnmp_set_request_error(reqinfo, request, SNMP_NOSUCHOBJECT);
431 continue;
432 #endif
433 case IPINDELIVERS:
434 #ifdef HAVE_STRUCT_IPSTAT_IPS_DELIVERED
435 ret_value = ipstat.ips_delivered & 0xffffffff;
436 break;
437 #else
438 netsnmp_set_request_error(reqinfo, request, SNMP_NOSUCHOBJECT);
439 continue;
440 #endif
441 case IPOUTREQUESTS:
442 #ifdef HAVE_STRUCT_IPSTAT_IPS_LOCALOUT
443 ret_value = ipstat.ips_localout & 0xffffffff;
444 break;
445 #else
446 netsnmp_set_request_error(reqinfo, request, SNMP_NOSUCHOBJECT);
447 continue;
448 #endif
449 case IPOUTDISCARDS:
450 #ifdef HAVE_STRUCT_IPSTAT_IPS_ODROPPED
451 ret_value = ipstat.ips_odropped;
452 break;
453 #else
454 netsnmp_set_request_error(reqinfo, request, SNMP_NOSUCHOBJECT);
455 continue;
456 #endif
457 case IPOUTNOROUTES:
458 /*
459 * XXX: how to calculate this (counts dropped routes, not packets)?
460 * ipstat.ips_cantforward isn't right, as it counts packets.
461 * ipstat.ips_noroute is also incorrect.
462 */
463 netsnmp_set_request_error(reqinfo, request, SNMP_NOSUCHOBJECT);
464 continue;
465 case IPREASMTIMEOUT:
466 ret_value = netsnmp_arch_ip_scalars_ipReasmTimeout_get();
467 type = ASN_INTEGER;
468 break;
469 case IPREASMREQDS:
470 ret_value = ipstat.ips_fragments;
471 break;
472 case IPREASMOKS:
473 #ifdef HAVE_STRUCT_IPSTAT_IPS_REASSEMBLED
474 ret_value = ipstat.ips_reassembled;
475 break;
476 #else
477 netsnmp_set_request_error(reqinfo, request, SNMP_NOSUCHOBJECT);
478 continue;
479 #endif
480 case IPREASMFAILS:
481 ret_value = ipstat.ips_fragdropped + ipstat.ips_fragtimeout;
482 break;
483 case IPFRAGOKS:
484 #ifdef HAVE_STRUCT_IPSTAT_IPS_FRAGMENTED
485 ret_value = ipstat.ips_fragments;
486 break;
487 #else /* XXX */
488 ret_value = ipstat.ips_fragments
489 - (ipstat.ips_fragdropped + ipstat.ips_fragtimeout);
490 break;
491 #endif
492 case IPFRAGFAILS:
493 #ifdef HAVE_STRUCT_IPSTAT_IPS_CANTFRAG
494 ret_value = ipstat.ips_cantfrag;
495 break;
496 #else
497 netsnmp_set_request_error(reqinfo, request, SNMP_NOSUCHOBJECT);
498 continue;
499 #endif
500 case IPFRAGCREATES:
501 #ifdef HAVE_STRUCT_IPSTAT_IPS_OFRAGMENTS
502 ret_value = ipstat.ips_ofragments;
503 break;
504 #else
505 netsnmp_set_request_error(reqinfo, request, SNMP_NOSUCHOBJECT);
506 continue;
507 #endif
508 case IPROUTEDISCARDS:
509 #ifdef HAVE_STRUCT_IPSTAT_IPS_NOROUTE
510 ret_value = ipstat.ips_noroute;
511 break;
512 #else
513 netsnmp_set_request_error(reqinfo, request, SNMP_NOSUCHOBJECT);
514 continue;
515 #endif
516 #ifdef HAVE_SYS_TCPIPSTATS_H
517 #undef ipstat
518 #endif
519 #elif defined(hpux11)
520 case IPFORWARDING:
521 case IPDEFAULTTTL:
522 case IPREASMTIMEOUT:
523 type = ASN_INTEGER;
524 case IPINRECEIVES:
525 case IPINHDRERRORS:
526 case IPINADDRERRORS:
527 case IPFORWDATAGRAMS:
528 case IPINUNKNOWNPROTOS:
529 case IPINDISCARDS:
530 case IPINDELIVERS:
531 case IPOUTREQUESTS:
532 case IPOUTDISCARDS:
533 case IPOUTNOROUTES:
534 case IPREASMREQDS:
535 case IPREASMOKS:
536 case IPREASMFAILS:
537 case IPFRAGOKS:
538 case IPFRAGFAILS:
539 case IPFRAGCREATES:
540 case IPROUTEDISCARDS:
541 /*
542 * This is a bit of a hack, to shoehorn the HP-UX 11
543 * single-object retrieval approach into the caching
544 * architecture.
545 */
546 if (ip_load(NULL, (void*)subid) == -1 ) {
547 netsnmp_set_request_error(reqinfo, request, SNMP_NOSUCHOBJECT);
548 continue;
549 }
550 ret_value = ipstat;
551 break;
552 #elif defined (WIN32) || defined (cygwin)
553 case IPFORWARDING:
554 ipForwarding = ipstat.dwForwarding;
555 ret_value = ipstat.dwForwarding;
556 type = ASN_INTEGER;
557 break;
558 case IPDEFAULTTTL:
559 ipTTL = ipstat.dwDefaultTTL;
560 ret_value = ipstat.dwDefaultTTL;
561 type = ASN_INTEGER;
562 break;
563 case IPINRECEIVES:
564 ret_value = ipstat.dwInReceives;
565 break;
566 case IPINHDRERRORS:
567 ret_value = ipstat.dwInHdrErrors;
568 break;
569 case IPINADDRERRORS:
570 ret_value = ipstat.dwInAddrErrors;
571 break;
572 case IPFORWDATAGRAMS:
573 ret_value = ipstat.dwForwDatagrams;
574 break;
575 case IPINUNKNOWNPROTOS:
576 ret_value = ipstat.dwInUnknownProtos;
577 break;
578 case IPINDISCARDS:
579 ret_value = ipstat.dwInDiscards;
580 break;
581 case IPINDELIVERS:
582 ret_value = ipstat.dwInDelivers;
583 break;
584 case IPOUTREQUESTS:
585 ret_value = ipstat.dwOutRequests;
586 break;
587 case IPOUTDISCARDS:
588 ret_value = ipstat.dwOutDiscards;
589 break;
590 case IPOUTNOROUTES:
591 ret_value = ipstat.dwOutNoRoutes;
592 break;
593 case IPREASMTIMEOUT:
594 ret_value = ipstat.dwReasmTimeout;
595 type = ASN_INTEGER;
596 break;
597 case IPREASMREQDS:
598 ret_value = ipstat.dwReasmReqds;
599 break;
600 case IPREASMOKS:
601 ret_value = ipstat.dwReasmOks;
602 break;
603 case IPREASMFAILS:
604 ret_value = ipstat.dwReasmFails;
605 break;
606 case IPFRAGOKS:
607 ret_value = ipstat.dwFragOks;
608 break;
609 case IPFRAGFAILS:
610 ret_value = ipstat.dwFragFails;
611 break;
612 case IPFRAGCREATES:
613 ret_value = ipstat.dwFragCreates;
614 break;
615 case IPROUTEDISCARDS:
616 ret_value = ipstat.dwRoutingDiscards;
617 break;
618 #elif defined(_USE_PERFSTAT_PROTOCOL)
619 case IPFORWARDING:
620 ret_value = 0;
621 type = ASN_INTEGER;
622 break;
623 case IPDEFAULTTTL:
624 ret_value = 0;
625 type = ASN_INTEGER;
626 break;
627 case IPINRECEIVES:
628 ret_value = ps_proto.u.ip.ipackets;
629 break;
630 case IPINHDRERRORS:
631 case IPINADDRERRORS:
632 case IPFORWDATAGRAMS:
633 ret_value = 0;
634 break;
635 case IPINUNKNOWNPROTOS:
636 ret_value = ps_proto.u.ip.ierrors;
637 break;
638 case IPINDISCARDS:
639 ret_value = 0;
640 break;
641 case IPINDELIVERS:
642 case IPOUTREQUESTS:
643 ret_value = ps_proto.u.ip.opackets;
644 break;
645 case IPOUTDISCARDS:
646 case IPOUTNOROUTES:
647 ret_value = 0;
648 break;
649 case IPREASMTIMEOUT:
650 ret_value = 0;
651 type = ASN_INTEGER;
652 break;
653 case IPREASMREQDS:
654 case IPREASMOKS:
655 case IPREASMFAILS:
656 case IPFRAGOKS:
657 case IPFRAGFAILS:
658 case IPFRAGCREATES:
659 ret_value = 0;
660 break;
661 case IPROUTEDISCARDS:
662 ret_value = ps_proto.u.ip.oerrors;
663 break;
664 #endif /* USES_SNMP_DESIGNED_IPSTAT */
665
666 case IPADDRTABLE:
667 case IPROUTETABLE:
668 case IPMEDIATABLE:
669 /*
670 * These are not actually valid scalar objects.
671 * The relevant table registrations should take precedence,
672 * so skip these three subtrees, regardless of architecture.
673 */
674 netsnmp_set_request_error(reqinfo, request, SNMP_NOSUCHOBJECT);
675 continue;
676
677 }
678 snmp_set_var_typed_value(request->requestvb, (u_char)type,
679 (u_char *)&ret_value, sizeof(ret_value));
680 }
681 break;
682
683 case MODE_GETNEXT:
684 case MODE_GETBULK:
685 #ifndef NETSNMP_NO_WRITE_SUPPORT
686 case MODE_SET_RESERVE1:
687 /* XXX - Windows currently supports setting this */
688 case MODE_SET_RESERVE2:
689 case MODE_SET_ACTION:
690 case MODE_SET_COMMIT:
691 case MODE_SET_FREE:
692 case MODE_SET_UNDO:
693 snmp_log(LOG_WARNING, "mibII/ip: Unsupported mode (%d)\n",
694 reqinfo->mode);
695 break;
696 #endif /* !NETSNMP_NO_WRITE_SUPPORT */
697 default:
698 snmp_log(LOG_WARNING, "mibII/ip: Unrecognised mode (%d)\n",
699 reqinfo->mode);
700 break;
701 }
702
703 return SNMP_ERR_NOERROR;
704 }
705
706
707
708 /*********************
709 *
710 * Internal implementation functions
711 *
712 *********************/
713
714
715 #ifdef hpux11
716 int
717 ip_load(netsnmp_cache *cache, void *vmagic)
718 {
719 int fd;
720 struct nmparms p;
721 unsigned int ulen;
722 int ret;
723 int magic = (uintptr_t)vmagic;
724
725 if ((fd = open_mib("/dev/ip", O_RDONLY, 0, NM_ASYNC_OFF)) < 0) {
726 DEBUGMSGTL(("mibII/ip", "Failed to load IP object %d (hpux11)\n", magic));
727 return (-1); /* error */
728 }
729
730 switch (magic) {
731 case IPFORWARDING:
732 p.objid = ID_ipForwarding;
733 break;
734 case IPDEFAULTTTL:
735 p.objid = ID_ipDefaultTTL;
736 break;
737 case IPINRECEIVES:
738 p.objid = ID_ipInReceives;
739 break;
740 case IPINHDRERRORS:
741 p.objid = ID_ipInHdrErrors;
742 break;
743 case IPINADDRERRORS:
744 p.objid = ID_ipInAddrErrors;
745 break;
746 case IPFORWDATAGRAMS:
747 p.objid = ID_ipForwDatagrams;
748 break;
749 case IPINUNKNOWNPROTOS:
750 p.objid = ID_ipInUnknownProtos;
751 break;
752 case IPINDISCARDS:
753 p.objid = ID_ipInDiscards;
754 break;
755 case IPINDELIVERS:
756 p.objid = ID_ipInDelivers;
757 break;
758 case IPOUTREQUESTS:
759 p.objid = ID_ipOutRequests;
760 break;
761 case IPOUTDISCARDS:
762 p.objid = ID_ipOutDiscards;
763 break;
764 case IPOUTNOROUTES:
765 p.objid = ID_ipOutNoRoutes;
766 break;
767 case IPREASMTIMEOUT:
768 p.objid = ID_ipReasmTimeout;
769 break;
770 case IPREASMREQDS:
771 p.objid = ID_ipReasmReqds;
772 break;
773 case IPREASMOKS:
774 p.objid = ID_ipReasmOKs;
775 break;
776 case IPREASMFAILS:
777 p.objid = ID_ipReasmFails;
778 break;
779 case IPFRAGOKS:
780 p.objid = ID_ipFragOKs;
781 break;
782 case IPFRAGFAILS:
783 p.objid = ID_ipFragFails;
784 break;
785 case IPFRAGCREATES:
786 p.objid = ID_ipFragCreates;
787 break;
788 case IPROUTEDISCARDS:
789 p.objid = ID_ipRoutingDiscards;
790 break;
791 default:
792 ipstat = 0;
793 close_mib(fd);
794 return (0);
795 }
796
797 p.buffer = (void *)&ipstat;
798 ulen = sizeof(IP_STAT_STRUCTURE);
799 p.len = &ulen;
800 ret = get_mib_info(fd, &p);
801 close_mib(fd);
802
803 DEBUGMSGTL(("mibII/ip", "%s IP object %d (hpux11)\n",
804 (ret < 0 ? "Failed to load" : "Loaded"), magic));
805 return (ret); /* 0: ok, < 0: error */
806 }
807 #elif defined(linux)
808 int
809 ip_load(netsnmp_cache *cache, void *vmagic)
810 {
811 long ret_value = -1;
812
813 ret_value = linux_read_ip_stat(&ipstat);
814
815 if ( ret_value < 0 ) {
816 DEBUGMSGTL(("mibII/ip", "Failed to load IP Group (linux)\n"));
817 } else {
818 DEBUGMSGTL(("mibII/ip", "Loaded IP Group (linux)\n"));
819 }
820 return ret_value;
821 }
822 #elif defined(solaris2)
823 int
824 ip_load(netsnmp_cache *cache, void *vmagic)
825 {
826 long ret_value = -1;
827
828 ret_value =
829 getMibstat(MIB_IP, &ipstat, sizeof(mib2_ip_t), GET_FIRST,
830 &Get_everything, NULL);
831
832 if ( ret_value < 0 ) {
833 DEBUGMSGTL(("mibII/ip", "Failed to load IP Group (solaris)\n"));
834 } else {
835 DEBUGMSGTL(("mibII/ip", "Loaded IP Group (solaris)\n"));
836 }
837 return ret_value;
838 }
839 #elif defined (NETBSD_STATS_VIA_SYSCTL)
840 int
841 ip_load(netsnmp_cache *cache, void *vmagic)
842 {
843 long ret_value = -1;
844
845 ret_value = netbsd_read_ip_stat(&ipstat);
846
847 if ( ret_value < 0) {
848 DEBUGMSGTL(("mibII/ip", "Failed to load IP Group (netbsd)\n"));
849 } else {
850 DEBUGMSGTL(("mibII/ip", "Loaded IP Group (netbsd)\n"));
851 }
852 return ret_value;
853 }
854 #elif defined (WIN32) || defined (cygwin)
855 int
856 ip_load(netsnmp_cache *cache, void *vmagic)
857 {
858 long ret_value = -1;
859
860 ret_value = GetIpStatistics(&ipstat);
861
862 if ( ret_value < 0 ) {
863 DEBUGMSGTL(("mibII/ip", "Failed to load IP Group (win32)\n"));
864 } else {
865 DEBUGMSGTL(("mibII/ip", "Loaded IP Group (win32)\n"));
866 }
867 return ret_value;
868 }
869 #elif defined(_USE_PERFSTAT_PROTOCOL)
870 int
871 ip_load(netsnmp_cache *cache, void *vmagic)
872 {
873 long ret_value = -1;
874
875 strcpy(ps_name.name, "ip");
876 ret_value = perfstat_protocol(&ps_name, &ps_proto, sizeof(ps_proto), 1);
877
878 if ( ret_value < 0 ) {
879 DEBUGMSGTL(("mibII/ip", "Failed to load IP Group (AIX)\n"));
880 } else {
881 ret_value = 0;
882 DEBUGMSGTL(("mibII/ip", "Loaded IP Group (AIX)\n"));
883 }
884 return ret_value;
885 }
886 #elif defined(NETSNMP_CAN_USE_SYSCTL) && defined(IPCTL_STATS)
887 int
888 ip_load(netsnmp_cache *cache, void *vmagic)
889 {
890 long ret_value = 0;
891 int i;
892 static int sname[4] = { CTL_NET, PF_INET, IPPROTO_IP, 0 };
893 size_t len;
894 int magic = (uintptr_t) vmagic;
895
896 switch (magic) {
897 case IPFORWARDING:
898 len = sizeof i;
899 sname[3] = IPCTL_FORWARDING;
900 if (sysctl(sname, 4, &i, &len, 0, 0) < 0)
901 return -1;
902 else
903 return (i ? 1 /* GATEWAY */
904 : 2 /* HOST */ );
905
906 case IPDEFAULTTTL:
907 len = sizeof i;
908 sname[3] = IPCTL_DEFTTL;
909 if (sysctl(sname, 4, &i, &len, 0, 0) < 0)
910 return -1;
911 else
912 return i;
913
914 default:
915 len = sizeof(ipstat);
916 sname[3] = IPCTL_STATS;
917 ret_value = sysctl(sname, 4, &ipstat, &len, 0, 0);
918
919 if ( ret_value < 0 ) {
920 DEBUGMSGTL(("mibII/ip", "Failed to load IP Group (sysctl)\n"));
921 } else {
922 DEBUGMSGTL(("mibII/ip", "Loaded IP Group (sysctl)\n"));
923 }
924 return ret_value;
925 }
926 }
927 #elif defined(HAVE_SYS_TCPIPSTATS_H)
928 int
929 ip_load(netsnmp_cache *cache, void *vmagic)
930 {
931 long ret_value = -1;
932 int magic = (uintptr_t)vmagic;
933
934 switch (magic) {
935 case IPFORWARDING:
936 if (!auto_nlist
937 (IP_FORWARDING_SYMBOL, (char *) &ret_value, sizeof(ret_value)))
938 return -1;
939 else
940 return (ret_value ? 1 /* GATEWAY */
941 : 2 /* HOST */ );
942
943 case IPDEFAULTTTL:
944 if (!auto_nlist
945 (TCP_TTL_SYMBOL, (char *) &ret_value, sizeof(ret_value)))
946 return -1;
947 else
948 return ret_value;
949
950 default:
951 ret_value = sysmp(MP_SAGET, MPSA_TCPIPSTATS, &ipstat, sizeof ipstat);
952
953 if ( ret_value < 0 ) {
954 DEBUGMSGTL(("mibII/ip", "Failed to load IP Group (tcpipstats)\n"));
955 } else {
956 DEBUGMSGTL(("mibII/ip", "Loaded IP Group (tcpipstats)\n"));
957 }
958 return ret_value;
959 }
960 }
961 #elif defined(IPSTAT_SYMBOL)
962 int
963 ip_load(netsnmp_cache *cache, void *vmagic)
964 {
965 long ret_value = -1;
966 int magic = (uintptr_t)vmagic;
967
968 switch (magic) {
969 case IPFORWARDING:
970 if (!auto_nlist
971 (IP_FORWARDING_SYMBOL, (char *) &ret_value, sizeof(ret_value)))
972 return -1;
973 else
974 return (ret_value ? 1 /* GATEWAY */
975 : 2 /* HOST */ );
976
977 case IPDEFAULTTTL:
978 if (!auto_nlist
979 (TCP_TTL_SYMBOL, (char *) &ret_value, sizeof(ret_value)))
980 return -1;
981 else
982 return ret_value;
983
984 default:
985 if (auto_nlist(IPSTAT_SYMBOL, (char *)&ipstat, sizeof(ipstat)))
986 ret_value = 0;
987
988 if ( ret_value < 0 ) {
989 DEBUGMSGTL(("mibII/ip", "Failed to load IP Group (ipstat)\n"));
990 } else {
991 DEBUGMSGTL(("mibII/ip", "Loaded IP Group (ipstat)\n"));
992 }
993 return ret_value;
994 }
995 }
996 #else /* IPSTAT_SYMBOL */
997 int
998 ip_load(netsnmp_cache *cache, void *vmagic)
999 {
1000 long ret_value = -1;
1001
1002 DEBUGMSGTL(("mibII/ip", "Failed to load IP Group (null)\n"));
1003 return ret_value;
1004 }
1005 #endif /* hpux11 */
1006
1007 void
1008 ip_free(netsnmp_cache *cache, void *magic)
1009 {
1010 #if defined(_USE_PERFSTAT_PROTOCOL)
1011 memset(&ps_proto, 0, sizeof(ps_proto));
1012 #else
1013 memset(&ipstat, 0, sizeof(ipstat));
1014 #endif
1015 }
1016