Debian Codegraph @

agent/mibgroup/ip-mib/data_access/systemstats_sysctl.c

raw
1 /*
2 * ipSystemStatsTable and ipIfStatsTable interface MIB architecture support
3 *
4 * $Id$
5 */
6 #include <net-snmp/net-snmp-config.h>
7 #include <net-snmp/net-snmp-includes.h>
8
9 #include <net-snmp/agent/net-snmp-agent-includes.h>
10 #include <net-snmp/data_access/ipstats.h>
11 #include <net-snmp/data_access/systemstats.h>
12
13 #include "../ipSystemStatsTable/ipSystemStatsTable.h"
14 #include "systemstats_private.h"
15
16 #include <sys/types.h>
17 #include <unistd.h>
18 #include <dirent.h>
19 #include <ctype.h>
20
21 #include <sys/param.h>
22 #include <sys/sysctl.h>
23 #include <sys/protosw.h>
24
25 #if defined(NETSNMP_IFNET_NEEDS_KERNEL) && !defined(_KERNEL)
26 #define _KERNEL 1
27 #define _I_DEFINED_KERNEL
28 #endif
29 #ifdef NETSNMP_IFNET_NEEDS_KERNEL_STRUCTURES
30 #define _KERNEL_STRUCTURES
31 #endif
32 #include <net/if.h>
33 #ifdef _I_DEFINED_KERNEL
34 #undef _KERNEL
35 #endif
36 #include <netinet/in.h>
37 #include <netinet/in_systm.h>
38 #include <netinet/ip.h>
39 #include <netinet/ip_icmp.h>
40 #include <netinet/ip_var.h>
41 #ifdef HAVE_NETINET_ICMP6_H
42 #include <netinet/icmp6.h>
43 #endif
44 #ifdef HAVE_NETINET6_IP6_VAR_H
45 #include <sys/queue.h>
46 #include <netinet6/ip6_var.h>
47 #endif
48 #if !defined(freebsd7) && !defined(openbsd5)
49 #include <netinet6/in6_var.h>
50 #endif
51
52 #ifdef darwin
53
54 /* This struct is in netinet6/ip6_var.h which Apple for obscure reasons
55 * do not distribute as part of /usr/include :-(
56 */
57
58 struct ip6stat {
59 u_quad_t ip6s_total; /* total packets received */
60 u_quad_t ip6s_tooshort; /* packet too short */
61 u_quad_t ip6s_toosmall; /* not enough data */
62 u_quad_t ip6s_fragments; /* fragments received */
63 u_quad_t ip6s_fragdropped; /* frags dropped(dups, out of space) */
64 u_quad_t ip6s_fragtimeout; /* fragments timed out */
65 u_quad_t ip6s_fragoverflow; /* fragments that exceeded limit */
66 u_quad_t ip6s_forward; /* packets forwarded */
67 u_quad_t ip6s_cantforward; /* packets rcvd for unreachable dest */
68 u_quad_t ip6s_redirectsent; /* packets forwarded on same net */
69 u_quad_t ip6s_delivered; /* datagrams delivered to upper level*/
70 u_quad_t ip6s_localout; /* total ip packets generated here */
71 u_quad_t ip6s_odropped; /* lost packets due to nobufs, etc. */
72 u_quad_t ip6s_reassembled; /* total packets reassembled ok */
73 u_quad_t ip6s_fragmented; /* datagrams sucessfully fragmented */
74 u_quad_t ip6s_ofragments; /* output fragments created */
75 u_quad_t ip6s_cantfrag; /* don't fragment flag was set, etc. */
76 u_quad_t ip6s_badoptions; /* error in option processing */
77 u_quad_t ip6s_noroute; /* packets discarded due to no route */
78 u_quad_t ip6s_badvers; /* ip6 version != 6 */
79 u_quad_t ip6s_rawout; /* total raw ip packets generated */
80 u_quad_t ip6s_badscope; /* scope error */
81 u_quad_t ip6s_notmember; /* don't join this multicast group */
82 u_quad_t ip6s_nxthist[256]; /* next header history */
83 u_quad_t ip6s_m1; /* one mbuf */
84 u_quad_t ip6s_m2m[32]; /* two or more mbuf */
85 u_quad_t ip6s_mext1; /* one ext mbuf */
86 u_quad_t ip6s_mext2m; /* two or more ext mbuf */
87 u_quad_t ip6s_exthdrtoolong; /* ext hdr are not continuous */
88 u_quad_t ip6s_nogif; /* no match gif found */
89 u_quad_t ip6s_toomanyhdr; /* discarded due to too many headers */
90
91 /*
92 * statistics for improvement of the source address selection
93 * algorithm:
94 * XXX: hardcoded 16 = # of ip6 multicast scope types + 1
95 */
96 /* number of times that address selection fails */
97 u_quad_t ip6s_sources_none;
98 /* number of times that an address on the outgoing I/F is chosen */
99 u_quad_t ip6s_sources_sameif[16];
100 /* number of times that an address on a non-outgoing I/F is chosen */
101 u_quad_t ip6s_sources_otherif[16];
102 /*
103 * number of times that an address that has the same scope
104 * from the destination is chosen.
105 */
106 u_quad_t ip6s_sources_samescope[16];
107 /*
108 * number of times that an address that has a different scope
109 * from the destination is chosen.
110 */
111 u_quad_t ip6s_sources_otherscope[16];
112 /* number of times that an deprecated address is chosen */
113 u_quad_t ip6s_sources_deprecated[16];
114
115 u_quad_t ip6s_forward_cachehit;
116 u_quad_t ip6s_forward_cachemiss;
117 };
118
119 #endif /* darwin*/
120
121 static int _systemstats_v4(netsnmp_container* container, u_int load_flags);
122
123 #if defined (NETSNMP_ENABLE_IPV6)
124 static int _systemstats_v6(netsnmp_container* container, u_int load_flags);
125 #endif
126
127 static int ncpus;
128
129 void
130 netsnmp_access_systemstats_arch_init(void)
131 {
132 int ncpu_mib[] = { CTL_HW, HW_NCPU };
133 size_t siz = sizeof(ncpus);
134 if (sysctl(ncpu_mib, 2, &ncpus, &siz, NULL, 0) < 0) {
135 snmp_log_perror("hw.ncpu");
136 ncpus = 1;
137 }
138 }
139
140 /*
141 *
142 * @retval 0 success
143 * @retval -1 no container specified
144 * @retval -2 could not open file
145 * @retval -3 could not create entry (probably malloc)
146 * @retval -4 file format error
147 */
148 int
149 netsnmp_access_systemstats_container_arch_load(netsnmp_container* container,
150 u_int load_flags)
151 {
152 int rc1;
153 #if defined (NETSNMP_ENABLE_IPV6)
154 int rc2;
155 #endif
156
157 if (NULL == container) {
158 snmp_log(LOG_ERR, "no container specified/found for access_systemstats_\n");
159 return -1;
160 }
161
162 /*
163 * load v4 and v6 stats. Even if one fails, try the other.
164 * If they have the same rc, return it. if the differ, return
165 * the smaller one. No log messages, since each individual function
166 * would have logged its own message.
167 */
168 rc1 = _systemstats_v4(container, load_flags);
169 #if defined (NETSNMP_ENABLE_IPV6)
170 rc2 = _systemstats_v6(container, load_flags);
171 if ((rc1 == rc2) || (rc1 < rc2))
172 return rc1;
173
174 return rc2;
175 #else
176 return rc1;
177 #endif
178 }
179
180 /*
181 * Based on load_flags, load ipSystemStatsTable or ipIfStatsTable for ipv4 entries.
182 */
183 #ifdef __NetBSD__
184
185 static int
186 _systemstats_v4(netsnmp_container* container, u_int load_flags)
187 {
188 netsnmp_systemstats_entry *entry = NULL;
189 uint64_t ipstat[IP_NSTATS];
190 size_t len = sizeof(ipstat);
191
192 if (sysctlbyname("net.inet.ip.stats", &ipstat, &len, NULL, 0) < 0) {
193 NETSNMP_LOGONCE((LOG_ERR, "Cannot sysctlbyname net.inet.ip.stats\n"));
194 return -2;
195 }
196
197 DEBUGMSGTL(("access:systemstats:container:arch", "load v4 (flags %x)\n",
198 load_flags));
199
200 netsnmp_assert(container != NULL); /* load function shoulda checked this */
201
202 if (load_flags & NETSNMP_ACCESS_SYSTEMSTATS_LOAD_IFTABLE) {
203 /* we do not support ipIfStatsTable for ipv4 */
204 return 0;
205 }
206
207 entry = netsnmp_access_systemstats_entry_create(1, 0,
208 "ipSystemStatsTable.ipv4");
209 if(NULL == entry) {
210 snmp_log(LOG_ERR, "systemstats_v4: cannot create entry\n");
211 netsnmp_access_systemstats_container_free(container,
212 NETSNMP_ACCESS_SYSTEMSTATS_FREE_NOFLAGS);
213 return -3;
214 }
215
216 /*
217 * OK - we've now got (or created) the data structure for
218 * this systemstats, including any "static" information.
219 * Now parse the rest of the line (i.e. starting from 'stats')
220 * to extract the relevant statistics, and populate
221 * data structure accordingly.
222 */
223
224 entry->stats.HCInReceives.low = ipstat[IP_STAT_TOTAL] & 0xffffffff;
225 entry->stats.HCInReceives.high = ipstat[IP_STAT_TOTAL] >> 32;
226 entry->stats.InHdrErrors = ipstat[IP_STAT_BADSUM]
227 + ipstat[IP_STAT_TOOSHORT] + ipstat[IP_STAT_TOOSMALL]
228 + ipstat[IP_STAT_BADHLEN] + ipstat[IP_STAT_BADLEN];
229 entry->stats.InAddrErrors = ipstat[IP_STAT_CANTFORWARD];
230 entry->stats.HCOutForwDatagrams.low = ipstat[IP_STAT_FORWARD] & 0xffffffff;
231 entry->stats.HCOutForwDatagrams.high = ipstat[IP_STAT_FORWARD] >> 32;
232 entry->stats.InUnknownProtos = ipstat[IP_STAT_NOPROTO];
233 entry->stats.InDiscards = ipstat[IP_STAT_FRAGDROPPED];
234 entry->stats.HCInDelivers.low = ipstat[IP_STAT_DELIVERED] & 0xffffffff;
235 entry->stats.HCInDelivers.high = ipstat[IP_STAT_DELIVERED] >> 32;
236 entry->stats.HCOutRequests.low = ipstat[IP_STAT_LOCALOUT] & 0xffffffff;
237 entry->stats.HCOutRequests.high = ipstat[IP_STAT_LOCALOUT] >> 32;
238 entry->stats.HCOutDiscards.low = ipstat[IP_STAT_ODROPPED] & 0xffffffff;
239 entry->stats.HCOutDiscards.high = ipstat[IP_STAT_ODROPPED] >> 32;
240 entry->stats.HCOutNoRoutes.low = ipstat[IP_STAT_NOGIF] & 0xffffffff;
241 entry->stats.HCOutNoRoutes.high = ipstat[IP_STAT_NOGIF] >> 32;
242 /* entry->stats. = scan_vals[12]; / * ReasmTimeout */
243 entry->stats.ReasmReqds = ipstat[IP_STAT_FRAGMENTS];
244 entry->stats.ReasmOKs = ipstat[IP_STAT_REASSEMBLED];
245 entry->stats.ReasmFails = ipstat[IP_STAT_FRAGDROPPED]
246 + ipstat[IP_STAT_FRAGTIMEOUT];
247 entry->stats.HCOutFragOKs.low = ipstat[IP_STAT_FRAGMENTS] & 0xffffffff;
248 entry->stats.HCOutFragOKs.high = ipstat[IP_STAT_FRAGMENTS] >> 32;
249 entry->stats.HCOutFragFails.low = ipstat[IP_STAT_CANTFRAG] & 0xffffffff;
250 entry->stats.HCOutFragFails.high = ipstat[IP_STAT_CANTFRAG] >> 32;
251 entry->stats.HCOutFragCreates.low = ipstat[IP_STAT_OFRAGMENTS] & 0xffffffff;
252 entry->stats.HCOutFragCreates.high = ipstat[IP_STAT_OFRAGMENTS] >> 32;
253
254 entry->stats.columnAvail[IPSYSTEMSTATSTABLE_HCINRECEIVES] = 1;
255 entry->stats.columnAvail[IPSYSTEMSTATSTABLE_INHDRERRORS] = 1;
256 entry->stats.columnAvail[IPSYSTEMSTATSTABLE_INADDRERRORS] = 1;
257 entry->stats.columnAvail[IPSYSTEMSTATSTABLE_HCOUTFORWDATAGRAMS] = 1;
258 entry->stats.columnAvail[IPSYSTEMSTATSTABLE_INUNKNOWNPROTOS] = 1;
259 entry->stats.columnAvail[IPSYSTEMSTATSTABLE_INDISCARDS] = 1;
260 entry->stats.columnAvail[IPSYSTEMSTATSTABLE_HCINDELIVERS] = 1;
261 entry->stats.columnAvail[IPSYSTEMSTATSTABLE_HCOUTREQUESTS] = 1;
262 entry->stats.columnAvail[IPSYSTEMSTATSTABLE_HCOUTDISCARDS] = 1;
263 entry->stats.columnAvail[IPSYSTEMSTATSTABLE_HCOUTNOROUTES] = 1;
264 entry->stats.columnAvail[IPSYSTEMSTATSTABLE_REASMREQDS] = 1;
265 entry->stats.columnAvail[IPSYSTEMSTATSTABLE_REASMOKS] = 1;
266 entry->stats.columnAvail[IPSYSTEMSTATSTABLE_REASMFAILS] = 1;
267 entry->stats.columnAvail[IPSYSTEMSTATSTABLE_HCOUTFRAGOKS] = 1;
268 entry->stats.columnAvail[IPSYSTEMSTATSTABLE_HCOUTFRAGFAILS] = 1;
269 entry->stats.columnAvail[IPSYSTEMSTATSTABLE_HCOUTFRAGCREATES] = 1;
270 entry->stats.columnAvail[IPSYSTEMSTATSTABLE_DISCONTINUITYTIME] = 1;
271 entry->stats.columnAvail[IPSYSTEMSTATSTABLE_REFRESHRATE] = 1;
272
273 /*
274 * add to container
275 */
276 if (CONTAINER_INSERT(container, entry) < 0)
277 {
278 snmp_log(LOG_ERR, "error with systemstats_v4: insert into container failed.\n");
279 netsnmp_access_systemstats_entry_free(entry);
280 }
281
282 return 0;
283 }
284
285
286 #if defined (NETSNMP_ENABLE_IPV6)
287
288 static int
289 _systemstats_v6_load_systemstats(netsnmp_container* container, u_int load_flags)
290 {
291 netsnmp_systemstats_entry *entry = NULL;
292 uint64_t ipstat[IP6_NSTATS];
293 size_t len = sizeof(ipstat);
294
295 if (sysctlbyname("net.inet6.ip6.stats", &ipstat, &len, NULL, 0) < 0) {
296 NETSNMP_LOGONCE((LOG_ERR, "Cannot sysctlbyname net.inet6.ip6.stats\n"));
297 return -2;
298 }
299
300 DEBUGMSGTL(("access:systemstats:container:arch", "load v6 (flags %x)\n",
301 load_flags));
302
303 netsnmp_assert(container != NULL); /* load function shoulda checked this */
304
305 entry = netsnmp_access_systemstats_entry_create(2, 0,
306 "ipSystemStatsTable.ipv6");
307 if(NULL == entry) {
308 snmp_log(LOG_ERR, "systemstats_v6_load_systemstats: cannot create entry\n");
309 netsnmp_access_systemstats_container_free(container,
310 NETSNMP_ACCESS_SYSTEMSTATS_FREE_NOFLAGS);
311 return -3;
312 }
313
314 /*
315 * OK - we've now got (or created) the data structure for
316 * this systemstats, including any "static" information.
317 */
318
319 entry->stats.HCInReceives.low = ipstat[IP6_STAT_TOTAL] & 0xffffffff;
320 entry->stats.HCInReceives.high = ipstat[IP6_STAT_TOTAL] >> 32;
321 entry->stats.InHdrErrors = ipstat[IP6_STAT_BADOPTIONS]
322 + ipstat[IP6_STAT_TOOSHORT] + ipstat[IP6_STAT_TOOSMALL]
323 + ipstat[IP6_STAT_TOOMANYHDR] + ipstat[IP6_STAT_EXTHDRTOOLONG];
324 entry->stats.InAddrErrors = ipstat[IP6_STAT_CANTFORWARD];
325 entry->stats.HCOutForwDatagrams.low = ipstat[IP6_STAT_FORWARD] & 0xffffffff;
326 entry->stats.HCOutForwDatagrams.high = ipstat[IP6_STAT_FORWARD] >> 32;
327 entry->stats.InDiscards = ipstat[IP6_STAT_FRAGDROPPED];
328 entry->stats.HCInDelivers.low = ipstat[IP6_STAT_DELIVERED] & 0xffffffff;
329 entry->stats.HCInDelivers.high = ipstat[IP6_STAT_DELIVERED] >> 32;
330 entry->stats.HCOutRequests.low = ipstat[IP6_STAT_LOCALOUT] & 0xffffffff;
331 entry->stats.HCOutRequests.high = ipstat[IP6_STAT_LOCALOUT] >> 32;
332 entry->stats.HCOutDiscards.low = ipstat[IP6_STAT_ODROPPED] & 0xffffffff;
333 entry->stats.HCOutDiscards.high = ipstat[IP6_STAT_ODROPPED] >> 32;
334 entry->stats.HCOutNoRoutes.low = ipstat[IP6_STAT_NOGIF] & 0xffffffff;
335 entry->stats.HCOutNoRoutes.high = ipstat[IP6_STAT_NOGIF] >> 32;
336 /* entry->stats. = scan_vals[12]; / * ReasmTimeout */
337 entry->stats.ReasmReqds = ipstat[IP6_STAT_FRAGMENTS];
338 entry->stats.ReasmOKs = ipstat[IP6_STAT_REASSEMBLED];
339 entry->stats.ReasmFails = ipstat[IP6_STAT_FRAGDROPPED]
340 + ipstat[IP6_STAT_FRAGTIMEOUT];
341 entry->stats.HCOutFragOKs.low = ipstat[IP6_STAT_FRAGMENTS] & 0xffffffff;
342 entry->stats.HCOutFragOKs.high = ipstat[IP6_STAT_FRAGMENTS] >> 32;
343 entry->stats.HCOutFragFails.low = ipstat[IP6_STAT_CANTFRAG] & 0xffffffff;
344 entry->stats.HCOutFragFails.high = ipstat[IP6_STAT_CANTFRAG] >> 32;
345 entry->stats.HCOutFragCreates.low = ipstat[IP6_STAT_OFRAGMENTS] & 0xffffffff;
346 entry->stats.HCOutFragCreates.high = ipstat[IP6_STAT_OFRAGMENTS] >> 32;
347
348 entry->stats.columnAvail[IPSYSTEMSTATSTABLE_HCINRECEIVES] = 1;
349 entry->stats.columnAvail[IPSYSTEMSTATSTABLE_INHDRERRORS] = 1;
350 entry->stats.columnAvail[IPSYSTEMSTATSTABLE_INADDRERRORS] = 1;
351 entry->stats.columnAvail[IPSYSTEMSTATSTABLE_HCOUTFORWDATAGRAMS] = 1;
352 entry->stats.columnAvail[IPSYSTEMSTATSTABLE_INDISCARDS] = 1;
353 entry->stats.columnAvail[IPSYSTEMSTATSTABLE_HCINDELIVERS] = 1;
354 entry->stats.columnAvail[IPSYSTEMSTATSTABLE_HCOUTREQUESTS] = 1;
355 entry->stats.columnAvail[IPSYSTEMSTATSTABLE_HCOUTDISCARDS] = 1;
356 entry->stats.columnAvail[IPSYSTEMSTATSTABLE_HCOUTNOROUTES] = 1;
357 entry->stats.columnAvail[IPSYSTEMSTATSTABLE_REASMREQDS] = 1;
358 entry->stats.columnAvail[IPSYSTEMSTATSTABLE_REASMOKS] = 1;
359 entry->stats.columnAvail[IPSYSTEMSTATSTABLE_REASMFAILS] = 1;
360 entry->stats.columnAvail[IPSYSTEMSTATSTABLE_HCOUTFRAGOKS] = 1;
361 entry->stats.columnAvail[IPSYSTEMSTATSTABLE_HCOUTFRAGFAILS] = 1;
362 entry->stats.columnAvail[IPSYSTEMSTATSTABLE_HCOUTFRAGCREATES] = 1;
363 entry->stats.columnAvail[IPSYSTEMSTATSTABLE_DISCONTINUITYTIME] = 1;
364 entry->stats.columnAvail[IPSYSTEMSTATSTABLE_REFRESHRATE] = 1;
365
366 /*
367 * add to container
368 */
369 if (CONTAINER_INSERT(container, entry) < 0)
370 {
371 snmp_log(LOG_ERR, "systemstats_v6_load_systemstats: cannot insert entry\n");
372 DEBUGMSGTL(("access:systemstats:container","error with systemstats_entry: insert into container failed.\n"));
373 netsnmp_access_systemstats_entry_free(entry);
374 }
375
376 return 0;
377 }
378
379
380 /*
381 * load ipIfStatsTable for ipv6 entries
382 */
383 static int
384 _systemstats_v6_load_ifstats(netsnmp_container* container, u_int load_flags)
385 {
386 struct if_nameindex *ifs = if_nameindex();
387 int ix;
388 int rc = 0;
389
390 for (ix = 0; ifs[ix].if_index; ix++) {
391 struct in6_ifstat *ifs6;
392 struct in6_ifreq ifr;
393 int s;
394
395 memset(&ifr, 0, sizeof(ifr));
396 strncpy(ifr.ifr_name, ifs[ix].if_name, sizeof(ifr.ifr_name)-1);
397 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
398 rc = -1;
399 break;
400 }
401 if (ioctl(s, SIOCGIFSTAT_IN6, (caddr_t)&ifr) < 0) {
402 rc = -2;
403 close(s);
404 break;
405 }
406 close(s);
407 ifs6 = &ifr.ifr_ifru.ifru_stat;
408 }
409 if_freenameindex(ifs);
410 return rc;
411 }
412 #endif
413
414 #else
415
416 static int
417 _systemstats_v4(netsnmp_container* container, u_int load_flags)
418 {
419 netsnmp_systemstats_entry *entry = NULL;
420 #ifdef __DragonFly__
421 size_t len = ncpus*sizeof(struct ip_stats);
422 struct ip_stats *ipstat = malloc(len);
423 int c;
424 #else
425 size_t len = sizeof(struct ipstat);
426 struct ipstat *ipstat = malloc(len);
427 #endif
428 int mib[] = { CTL_NET, PF_INET, IPPROTO_IP, IPCTL_STATS };
429
430 if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), ipstat, &len, NULL, 0) == -1) {
431 snmp_log_perror("Cannot sysctlbyname net.inet.ip.stats");
432 free(ipstat);
433 return -2;
434 }
435
436 DEBUGMSGTL(("access:systemstats:container:arch", "load v4 (flags %x)\n",
437 load_flags));
438
439 netsnmp_assert(container != NULL); /* load function shoulda checked this */
440
441 if (load_flags & NETSNMP_ACCESS_SYSTEMSTATS_LOAD_IFTABLE) {
442 /* we do not support ipIfStatsTable for ipv4 */
443 free(ipstat);
444 return 0;
445 }
446
447 entry = netsnmp_access_systemstats_entry_create(1, 0,
448 "ipSystemStatsTable.ipv4");
449 if(NULL == entry) {
450 netsnmp_access_systemstats_container_free(container,
451 NETSNMP_ACCESS_SYSTEMSTATS_FREE_NOFLAGS);
452 free(ipstat);
453 return -3;
454 }
455
456 /*
457 * OK - we've now got (or created) the data structure for
458 * this systemstats, including any "static" information.
459 * Now parse the rest of the line (i.e. starting from 'stats')
460 * to extract the relevant statistics, and populate
461 * data structure accordingly.
462 */
463
464 #ifdef dragonfly
465 for (c = 1; c < ncpus; c++) {
466 int i, n = sizeof(struct ip_stats)/sizeof(u_long);
467 u_long *up = (u_long *)ipstat;
468 u_long *cp = (u_long *)(ipstat+c);
469 for (i = 0; i < n; i++) {
470 *up += *cp;
471 up++;
472 cp++;
473 }
474 }
475 #endif
476 entry->stats.HCInReceives.low = ipstat->ips_total & 0xffffffff;
477 #ifndef darwin
478 entry->stats.HCInReceives.high = ipstat->ips_total >> 32;
479 #endif
480 entry->stats.InHdrErrors = ipstat->ips_badsum + ipstat->ips_tooshort
481 + ipstat->ips_toosmall + ipstat->ips_badhlen
482 + ipstat->ips_badlen;
483 entry->stats.InAddrErrors = ipstat->ips_cantforward;
484 entry->stats.HCOutForwDatagrams.low = ipstat->ips_forward & 0xffffffff;
485 #ifndef darwin
486 entry->stats.HCOutForwDatagrams.high = ipstat->ips_forward >> 32;
487 #endif
488 entry->stats.InUnknownProtos = ipstat->ips_noproto;
489 entry->stats.InDiscards = ipstat->ips_fragdropped;
490 entry->stats.HCInDelivers.low = ipstat->ips_delivered & 0xffffffff;
491 #ifndef darwin
492 entry->stats.HCInDelivers.high = ipstat->ips_delivered >> 32;
493 #endif
494 entry->stats.HCOutRequests.low = ipstat->ips_localout & 0xffffffff;
495 #ifndef darwin
496 entry->stats.HCOutRequests.high = ipstat->ips_localout >> 32;
497 #endif
498 entry->stats.HCOutDiscards.low = ipstat->ips_odropped & 0xffffffff;
499 #ifndef darwin
500 entry->stats.HCOutDiscards.high = ipstat->ips_odropped >> 32;
501 #endif
502 entry->stats.HCOutNoRoutes.low = ipstat->ips_nogif & 0xffffffff;
503 #ifndef darwin
504 entry->stats.HCOutNoRoutes.high = ipstat->ips_nogif >> 32;
505 #endif
506 /* entry->stats. = scan_vals[12]; / * ReasmTimeout */
507 entry->stats.ReasmReqds = ipstat->ips_fragments;
508 entry->stats.ReasmOKs = ipstat->ips_reassembled;
509 entry->stats.ReasmFails = ipstat->ips_fragdropped + ipstat->ips_fragtimeout;
510 entry->stats.HCOutFragOKs.low = ipstat->ips_fragments & 0xffffffff;
511 #ifndef darwin
512 entry->stats.HCOutFragOKs.high = ipstat->ips_fragments >> 32;
513 #endif
514 entry->stats.HCOutFragFails.low = ipstat->ips_cantfrag & 0xffffffff;
515 #ifndef darwin
516 entry->stats.HCOutFragFails.high = ipstat->ips_cantfrag >> 32;
517 #endif
518 entry->stats.HCOutFragCreates.low = ipstat->ips_ofragments & 0xffffffff;
519 #ifndef darwin
520 entry->stats.HCOutFragCreates.high = ipstat->ips_ofragments >> 32;
521 #endif
522
523 entry->stats.columnAvail[IPSYSTEMSTATSTABLE_HCINRECEIVES] = 1;
524 entry->stats.columnAvail[IPSYSTEMSTATSTABLE_INHDRERRORS] = 1;
525 entry->stats.columnAvail[IPSYSTEMSTATSTABLE_INADDRERRORS] = 1;
526 entry->stats.columnAvail[IPSYSTEMSTATSTABLE_HCOUTFORWDATAGRAMS] = 1;
527 entry->stats.columnAvail[IPSYSTEMSTATSTABLE_INUNKNOWNPROTOS] = 1;
528 entry->stats.columnAvail[IPSYSTEMSTATSTABLE_INDISCARDS] = 1;
529 entry->stats.columnAvail[IPSYSTEMSTATSTABLE_HCINDELIVERS] = 1;
530 entry->stats.columnAvail[IPSYSTEMSTATSTABLE_HCOUTREQUESTS] = 1;
531 entry->stats.columnAvail[IPSYSTEMSTATSTABLE_HCOUTDISCARDS] = 1;
532 entry->stats.columnAvail[IPSYSTEMSTATSTABLE_HCOUTNOROUTES] = 1;
533 entry->stats.columnAvail[IPSYSTEMSTATSTABLE_REASMREQDS] = 1;
534 entry->stats.columnAvail[IPSYSTEMSTATSTABLE_REASMOKS] = 1;
535 entry->stats.columnAvail[IPSYSTEMSTATSTABLE_REASMFAILS] = 1;
536 entry->stats.columnAvail[IPSYSTEMSTATSTABLE_HCOUTFRAGOKS] = 1;
537 entry->stats.columnAvail[IPSYSTEMSTATSTABLE_HCOUTFRAGFAILS] = 1;
538 entry->stats.columnAvail[IPSYSTEMSTATSTABLE_HCOUTFRAGCREATES] = 1;
539 entry->stats.columnAvail[IPSYSTEMSTATSTABLE_DISCONTINUITYTIME] = 1;
540 entry->stats.columnAvail[IPSYSTEMSTATSTABLE_REFRESHRATE] = 1;
541
542 /*
543 * add to container
544 */
545 if (CONTAINER_INSERT(container, entry) < 0)
546 {
547 DEBUGMSGTL(("access:systemstats:container","error with systemstats_entry: insert into container failed.\n"));
548 netsnmp_access_systemstats_entry_free(entry);
549 }
550
551 free(ipstat);
552 return 0;
553 }
554
555
556 #if defined (NETSNMP_ENABLE_IPV6)
557
558 /*
559 * load ipSystemStatsTable for ipv6 entries
560 */
561 static int
562 _systemstats_v6_load_systemstats(netsnmp_container* container, u_int load_flags)
563 {
564 struct ip6stat ip6stat;
565 int mib[] = { CTL_NET, AF_INET6, IPPROTO_IPV6, IPV6CTL_STATS };
566 size_t len = sizeof(ip6stat);
567 netsnmp_systemstats_entry *entry = NULL;
568
569 if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), &ip6stat, &len, NULL, 0) == -1) {
570 NETSNMP_LOGONCE((LOG_ERR, "Cannot sysctl(CTL_NET, AF_INET6, IPPROTO_IPV6, IPV6CTL_STATS)\n"));
571 return -1;
572 }
573
574 entry = netsnmp_access_systemstats_entry_create(2, 0,
575 "ipSystemStatsTable.ipv6");
576 if(NULL == entry)
577 return -3;
578
579 entry->stats.HCInReceives.low = ip6stat.ip6s_total & 0xffffffff;
580 entry->stats.HCInReceives.high = ip6stat.ip6s_total >> 32;
581 entry->stats.columnAvail[IPSYSTEMSTATSTABLE_HCINRECEIVES] = 1;
582 /*
583 entry->stats.HCInOctets.low = scan_val & 0xffffffff;
584 entry->stats.HCInOctets.high = scan_val >> 32;
585 entry->stats.columnAvail[IPSYSTEMSTATSTABLE_HCINOCTETS] = 1;
586 */
587 entry->stats.InHdrErrors = ip6stat.ip6s_badoptions + ip6stat.ip6s_tooshort
588 + ip6stat.ip6s_toosmall + ip6stat.ip6s_badvers
589 + ip6stat.ip6s_toomanyhdr;
590 #ifdef HAVE_STRUCT_IP6STAT_IP6S_EXTHDRTOOLONG
591 entry->stats.InHdrErrors += ip6stat.ip6s_exthdrtoolong;
592 #endif
593 entry->stats.columnAvail[IPSYSTEMSTATSTABLE_INHDRERRORS] = 1;
594 entry->stats.HCInNoRoutes.low = ip6stat.ip6s_cantforward & 0xffffffff;
595 entry->stats.HCInNoRoutes.high = ip6stat.ip6s_cantforward >> 32;
596 entry->stats.columnAvail[IPSYSTEMSTATSTABLE_HCINNOROUTES] = 1;
597 /*
598 entry->stats.inAddrErrors = 0;
599 entry->stats.columnAvail[IPSYSTEMSTATSTABLE_INADDRERRORS] = 1;
600 entry->stats.InUnknownProtos = scan_val;
601 entry->stats.columnAvail[IPSYSTEMSTATSTABLE_INUNKNOWNPROTOS] = 1;
602 entry->stats.InTruncatedPkts = scan_val & 0xffffffff;
603 entry->stats.columnAvail[IPSYSTEMSTATSTABLE_INTRUNCATEDPKTS] = 1;
604 */
605 entry->stats.HCInForwDatagrams.low = ip6stat.ip6s_forward & 0xffffffff;
606 entry->stats.HCInForwDatagrams.high = ip6stat.ip6s_forward >> 32;
607 entry->stats.columnAvail[IPSYSTEMSTATSTABLE_HCINFORWDATAGRAMS] = 1;
608 entry->stats.ReasmReqds = ip6stat.ip6s_fragments;
609 entry->stats.columnAvail[IPSYSTEMSTATSTABLE_REASMREQDS] = 1;
610 entry->stats.ReasmOKs = ip6stat.ip6s_reassembled;
611 entry->stats.columnAvail[IPSYSTEMSTATSTABLE_REASMOKS] = 1;
612 entry->stats.ReasmFails = ip6stat.ip6s_fragdropped + ip6stat.ip6s_fragtimeout;
613 entry->stats.columnAvail[IPSYSTEMSTATSTABLE_REASMFAILS] = 1;
614 entry->stats.InDiscards = ip6stat.ip6s_fragdropped;
615 entry->stats.columnAvail[IPSYSTEMSTATSTABLE_INDISCARDS] = 1;
616 entry->stats.HCInDelivers.low = ip6stat.ip6s_delivered & 0xffffffff;
617 entry->stats.HCInDelivers.high = ip6stat.ip6s_delivered >> 32;
618 entry->stats.columnAvail[IPSYSTEMSTATSTABLE_HCINDELIVERS] = 1;
619 entry->stats.HCOutRequests.low = ip6stat.ip6s_localout & 0xffffffff;
620 entry->stats.HCOutRequests.high = ip6stat.ip6s_localout >> 32;
621 entry->stats.columnAvail[IPSYSTEMSTATSTABLE_HCOUTREQUESTS] = 1;
622 entry->stats.HCOutNoRoutes.low = ip6stat.ip6s_noroute & 0xffffffff;
623 entry->stats.HCOutNoRoutes.high = ip6stat.ip6s_noroute >> 32;
624 entry->stats.columnAvail[IPSYSTEMSTATSTABLE_HCOUTNOROUTES] = 1;
625 entry->stats.HCOutForwDatagrams.low = ip6stat.ip6s_forward & 0xffffffff;
626 entry->stats.HCOutForwDatagrams.high = ip6stat.ip6s_forward >> 32;
627 entry->stats.columnAvail[IPSYSTEMSTATSTABLE_HCOUTFORWDATAGRAMS] = 1;
628 entry->stats.HCOutDiscards.low = ip6stat.ip6s_odropped & 0xffffffff;
629 entry->stats.HCOutDiscards.high = ip6stat.ip6s_odropped >> 32;
630 entry->stats.columnAvail[IPSYSTEMSTATSTABLE_HCOUTDISCARDS] = 1;
631 entry->stats.HCOutFragReqds.low = (ip6stat.ip6s_fragmented + ip6stat.ip6s_cantfrag) & 0xffffffff;
632 entry->stats.HCOutFragReqds.high = (ip6stat.ip6s_fragmented + ip6stat.ip6s_cantfrag) >> 32;
633 entry->stats.columnAvail[IPSYSTEMSTATSTABLE_HCOUTFRAGREQDS] = 1;
634 entry->stats.HCOutFragOKs.low = ip6stat.ip6s_fragmented & 0xffffffff;
635 entry->stats.HCOutFragOKs.high = ip6stat.ip6s_fragmented >> 32;
636 entry->stats.columnAvail[IPSYSTEMSTATSTABLE_HCOUTFRAGOKS] = 1;
637 entry->stats.HCOutFragFails.low = ip6stat.ip6s_cantfrag & 0xffffffff;
638 entry->stats.HCOutFragFails.high = ip6stat.ip6s_cantfrag >> 32;
639 entry->stats.columnAvail[IPSYSTEMSTATSTABLE_HCOUTFRAGFAILS] = 1;
640 entry->stats.HCOutFragCreates.low = ip6stat.ip6s_ofragments & 0xffffffff;
641 entry->stats.HCOutFragCreates.high = ip6stat.ip6s_ofragments >> 32;
642 entry->stats.columnAvail[IPSYSTEMSTATSTABLE_HCOUTFRAGCREATES] = 1;
643 /*
644 entry->stats.HCOutTransmits.low = scan_val & 0xffffffff;
645 entry->stats.HCOutTransmits.high = scan_val >> 32;
646 entry->stats.columnAvail[IPSYSTEMSTATSTABLE_HCOUTTRANSMITS] = 1;
647 entry->stats.HCOutMcastOctets.low = scan_val & 0xffffffff;
648 entry->stats.HCOutMcastOctets.high = scan_val >> 32;
649 entry->stats.columnAvail[IPSYSTEMSTATSTABLE_HCOUTMCASTOCTETS] = 1;
650 entry->stats.HCInMcastPkts.low = scan_val & 0xffffffff;
651 entry->stats.HCInMcastPkts.high = scan_val >> 32;
652 entry->stats.columnAvail[IPSYSTEMSTATSTABLE_HCINMCASTPKTS] = 1;
653 entry->stats.HCInMcastOctets.low = scan_val & 0xffffffff;
654 entry->stats.HCInMcastOctets.high = scan_val >> 32;
655 entry->stats.columnAvail[IPSYSTEMSTATSTABLE_HCINMCASTOCTETS] = 1;
656 entry->stats.HCOutMcastPkts.low = scan_val & 0xffffffff;
657 entry->stats.HCOutMcastPkts.high = scan_val >> 32;
658 entry->stats.columnAvail[IPSYSTEMSTATSTABLE_HCOUTMCASTPKTS] = 1;
659 entry->stats.HCOutOctets.low = scan_val & 0xffffffff;
660 entry->stats.HCOutOctets.high = scan_val >> 32;
661 entry->stats.columnAvail[IPSYSTEMSTATSTABLE_HCOUTOCTETS] = 1;
662 entry->stats.HCInBcastPkts.low = scan_val & 0xffffffff;
663 entry->stats.HCInBcastPkts.high = scan_val >> 32;
664 entry->stats.columnAvail[IPSYSTEMSTATSTABLE_HCINBCASTPKTS] = 1;
665 entry->stats.HCOutBcastPkts.low = scan_val & 0xffffffff;
666 entry->stats.HCOutBcastPkts.high = scan_val >> 32;
667 entry->stats.columnAvail[IPSYSTEMSTATSTABLE_HCOUTBCASTPKTS] = 1;
668 */
669
670 /*
671 * add to container
672 */
673 if (CONTAINER_INSERT(container, entry) < 0) {
674 DEBUGMSGTL(("access:systemstats:container","error with systemstats_entry: insert into container failed.\n"));
675 netsnmp_access_systemstats_entry_free(entry);
676 }
677
678 return 1;
679 }
680
681 #define DEV_SNMP6_DIRNAME "/proc/net/dev_snmp6"
682 #define IFINDEX_LINE "ifIndex"
683 #define DEV_FILENAME_LEN 64
684
685 /*
686 * load ipIfStatsTable for ipv6 entries
687 */
688 static int
689 _systemstats_v6_load_ifstats(netsnmp_container* container, u_int load_flags)
690 {
691 DIR *dev_snmp6_dir;
692 struct dirent *dev_snmp6_entry;
693 char dev_filename[DEV_FILENAME_LEN];
694 FILE *devin;
695 char line[1024];
696 char *start = line;
697 char *scan_str;
698 uintmax_t scan_val;
699 netsnmp_systemstats_entry *entry = NULL;
700
701 /*
702 * try to open /proc/net/dev_snmp6 directory. If we can't, that' ok -
703 * maybe it is not supported by the current running kernel.
704 */
705 if ((dev_snmp6_dir = opendir(DEV_SNMP6_DIRNAME)) == NULL) {
706 DEBUGMSGTL(("access:ifstats",
707 "Failed to load IPv6 IfStats Table (linux)\n"));
708 return 0;
709 }
710
711 /*
712 * Read each per interface statistics proc file
713 */
714 while ((dev_snmp6_entry = readdir(dev_snmp6_dir)) != NULL) {
715 if (dev_snmp6_entry->d_name[0] == '.')
716 continue;
717
718 if (snprintf(dev_filename, DEV_FILENAME_LEN, "%s/%s", DEV_SNMP6_DIRNAME,
719 dev_snmp6_entry->d_name) > DEV_FILENAME_LEN) {
720 snmp_log(LOG_ERR, "Interface name %s is too long\n",
721 dev_snmp6_entry->d_name);
722 continue;
723 }
724 if (NULL == (devin = fopen(dev_filename, "r"))) {
725 snmp_log(LOG_ERR, "Failed to open %s\n", dev_filename);
726 continue;
727 }
728
729 /*
730 * If a stat file name is made of digits, the name is interface index.
731 * If it is an interface name, the file includes a line labeled ifIndex.
732 */
733 if (isdigit(dev_snmp6_entry->d_name[0])) {
734 scan_val = strtoull(dev_snmp6_entry->d_name, NULL, 0);
735 } else {
736 if (NULL == (start = fgets(line, sizeof(line), devin))) {
737 snmp_log(LOG_ERR, "%s doesn't include any lines\n",
738 dev_filename);
739 fclose(devin);
740 continue;
741 }
742
743 if (0 != strncmp(start, IFINDEX_LINE, 7)) {
744 snmp_log(LOG_ERR, "%s doesn't include ifIndex line",
745 dev_filename);
746 fclose(devin);
747 continue;
748 }
749
750 scan_str = strrchr(line, ' ');
751 if (NULL == scan_str) {
752 snmp_log(LOG_ERR, "%s is wrong format", dev_filename);
753 fclose(devin);
754 continue;
755 }
756 scan_val = strtoull(scan_str, NULL, 0);
757 }
758
759 entry = netsnmp_access_systemstats_entry_create(2, scan_val,
760 "ipIfStatsTable.ipv6");
761 if(NULL == entry) {
762 fclose(devin);
763 closedir(dev_snmp6_dir);
764 return -3;
765 }
766
767 /* _systemstats_v6_load_file(entry, devin); */
768 CONTAINER_INSERT(container, entry);
769 fclose(devin);
770 }
771 closedir(dev_snmp6_dir);
772 return 0;
773 }
774 #endif /* NETSNMP_ENABLE_IPV6 */
775 #endif
776
777 #ifdef NETSNMP_ENABLE_IPV6
778 /*
779 * Based on load_flags, load ipSystemStatsTable or ipIfStatsTable for ipv6 entries.
780 */
781 static int
782 _systemstats_v6(netsnmp_container* container, u_int load_flags)
783 {
784 DEBUGMSGTL(("access:systemstats:container:arch", "load v6 (flags %u)\n",
785 load_flags));
786
787 netsnmp_assert(container != NULL); /* load function shoulda checked this */
788
789 if (load_flags & NETSNMP_ACCESS_SYSTEMSTATS_LOAD_IFTABLE) {
790 /* load ipIfStatsTable */
791 return _systemstats_v6_load_ifstats(container, load_flags);
792 } else {
793 /* load ipSystemStatsTable */
794 return _systemstats_v6_load_systemstats(container, load_flags);
795 }
796 }
797 #endif /* NETSNMP_ENABLE_IPV6 */