| 1 |
/* |
| 2 |
* Linux kernel interface |
| 3 |
* |
| 4 |
*/ |
| 5 |
|
| 6 |
#include <net-snmp/net-snmp-config.h> |
| 7 |
#include <net-snmp/net-snmp-features.h> |
| 8 |
#include <net-snmp/net-snmp-includes.h> |
| 9 |
#include <net-snmp/agent/net-snmp-agent-includes.h> |
| 10 |
|
| 11 |
#ifdef HAVE_STRING_H |
| 12 |
#include <string.h> |
| 13 |
#endif |
| 14 |
#include <sys/types.h> |
| 15 |
#ifdef HAVE_SYS_PARAM_H |
| 16 |
#include <sys/param.h> |
| 17 |
#endif |
| 18 |
#include <errno.h> |
| 19 |
#include <stddef.h> |
| 20 |
|
| 21 |
#include "kernel_linux.h" |
| 22 |
|
| 23 |
struct ip_mib cached_ip_mib; |
| 24 |
struct ip6_mib cached_ip6_mib; |
| 25 |
struct icmp_mib cached_icmp_mib; |
| 26 |
struct icmp6_mib cached_icmp6_mib; |
| 27 |
struct icmp4_msg_mib cached_icmp4_msg_mib; |
| 28 |
struct tcp_mib cached_tcp_mib; |
| 29 |
struct udp_mib cached_udp_mib; |
| 30 |
struct udp6_mib cached_udp6_mib; |
| 31 |
|
| 32 |
struct stats_descr { |
| 33 |
const char *prefix; |
| 34 |
const char *col_name; |
| 35 |
void *var; |
| 36 |
unsigned int offset; |
| 37 |
unsigned int validity_offset; |
| 38 |
}; |
| 39 |
|
| 40 |
static const struct stats_descr ipv4_snmp_stats[] = { |
| 41 |
{ "Ip:", "Forwarding", &cached_ip_mib, |
| 42 |
offsetof(struct ip_mib, ipForwarding) }, |
| 43 |
{ "Ip:", "DefaultTTL", &cached_ip_mib, |
| 44 |
offsetof(struct ip_mib, ipDefaultTTL) }, |
| 45 |
{ "Ip:", "InReceives", &cached_ip_mib, |
| 46 |
offsetof(struct ip_mib, ipInReceives) }, |
| 47 |
{ "Ip:", "InHdrErrors", &cached_ip_mib, |
| 48 |
offsetof(struct ip_mib, ipInHdrErrors) }, |
| 49 |
{ "Ip:", "InAddrErrors", &cached_ip_mib, |
| 50 |
offsetof(struct ip_mib, ipInAddrErrors) }, |
| 51 |
{ "Ip:", "ForwDatagrams", &cached_ip_mib, |
| 52 |
offsetof(struct ip_mib, ipForwDatagrams) }, |
| 53 |
{ "Ip:", "InUnknownProtos", &cached_ip_mib, |
| 54 |
offsetof(struct ip_mib, ipInUnknownProtos) }, |
| 55 |
{ "Ip:", "InDiscards", &cached_ip_mib, |
| 56 |
offsetof(struct ip_mib, ipInDiscards) }, |
| 57 |
{ "Ip:", "InDelivers", &cached_ip_mib, |
| 58 |
offsetof(struct ip_mib, ipInDelivers) }, |
| 59 |
{ "Ip:", "OutRequests", &cached_ip_mib, |
| 60 |
offsetof(struct ip_mib, ipOutRequests) }, |
| 61 |
{ "Ip:", "OutDiscards", &cached_ip_mib, |
| 62 |
offsetof(struct ip_mib, ipOutDiscards) }, |
| 63 |
{ "Ip:", "OutNoRoutes", &cached_ip_mib, |
| 64 |
offsetof(struct ip_mib, ipOutNoRoutes) }, |
| 65 |
{ "Ip:", "ReasmTimeout", &cached_ip_mib, |
| 66 |
offsetof(struct ip_mib, ipReasmTimeout) }, |
| 67 |
{ "Ip:", "ReasmReqds", &cached_ip_mib, |
| 68 |
offsetof(struct ip_mib, ipReasmReqds) }, |
| 69 |
{ "Ip:", "ReasmOKs", &cached_ip_mib, |
| 70 |
offsetof(struct ip_mib, ipReasmOKs) }, |
| 71 |
{ "Ip:", "ReasmFails", &cached_ip_mib, |
| 72 |
offsetof(struct ip_mib, ipReasmFails) }, |
| 73 |
{ "Ip:", "FragOKs", &cached_ip_mib, |
| 74 |
offsetof(struct ip_mib, ipFragOKs) }, |
| 75 |
{ "Ip:", "FragFails", &cached_ip_mib, |
| 76 |
offsetof(struct ip_mib, ipFragFails) }, |
| 77 |
{ "Ip:", "FragCreates", &cached_ip_mib, |
| 78 |
offsetof(struct ip_mib, ipFragCreates) }, |
| 79 |
|
| 80 |
{ "Icmp:", "InMsgs", &cached_icmp_mib, |
| 81 |
offsetof(struct icmp_mib, icmpInMsgs) }, |
| 82 |
{ "Icmp:", "InErrors", &cached_icmp_mib, |
| 83 |
offsetof(struct icmp_mib, icmpInErrors) }, |
| 84 |
{ "Icmp:", "InDestUnreachs", &cached_icmp_mib, |
| 85 |
offsetof(struct icmp_mib, icmpInDestUnreachs) }, |
| 86 |
{ "Icmp:", "InTimeExcds", &cached_icmp_mib, |
| 87 |
offsetof(struct icmp_mib, icmpInTimeExcds) }, |
| 88 |
{ "Icmp:", "InParmProbs", &cached_icmp_mib, |
| 89 |
offsetof(struct icmp_mib, icmpInParmProbs) }, |
| 90 |
{ "Icmp:", "InSrcQuenchs", &cached_icmp_mib, |
| 91 |
offsetof(struct icmp_mib, icmpInSrcQuenchs) }, |
| 92 |
{ "Icmp:", "InRedirects", &cached_icmp_mib, |
| 93 |
offsetof(struct icmp_mib, icmpInRedirects) }, |
| 94 |
{ "Icmp:", "InEchos", &cached_icmp_mib, |
| 95 |
offsetof(struct icmp_mib, icmpInEchos) }, |
| 96 |
{ "Icmp:", "InEchoReps", &cached_icmp_mib, |
| 97 |
offsetof(struct icmp_mib, icmpInEchoReps) }, |
| 98 |
{ "Icmp:", "InTimestamps", &cached_icmp_mib, |
| 99 |
offsetof(struct icmp_mib, icmpInTimestamps) }, |
| 100 |
{ "Icmp:", "InTimestampReps", &cached_icmp_mib, |
| 101 |
offsetof(struct icmp_mib, icmpInTimestampReps) }, |
| 102 |
{ "Icmp:", "InAddrMasks", &cached_icmp_mib, |
| 103 |
offsetof(struct icmp_mib, icmpInAddrMasks) }, |
| 104 |
{ "Icmp:", "InAddrMaskReps", &cached_icmp_mib, |
| 105 |
offsetof(struct icmp_mib, icmpInAddrMaskReps) }, |
| 106 |
{ "Icmp:", "OutMsgs", &cached_icmp_mib, |
| 107 |
offsetof(struct icmp_mib, icmpOutMsgs) }, |
| 108 |
{ "Icmp:", "OutErrors", &cached_icmp_mib, |
| 109 |
offsetof(struct icmp_mib, icmpOutErrors) }, |
| 110 |
{ "Icmp:", "OutDestUnreachs", &cached_icmp_mib, |
| 111 |
offsetof(struct icmp_mib, icmpOutDestUnreachs) }, |
| 112 |
{ "Icmp:", "OutTimeExcds", &cached_icmp_mib, |
| 113 |
offsetof(struct icmp_mib, icmpOutTimeExcds) }, |
| 114 |
{ "Icmp:", "OutParmProbs", &cached_icmp_mib, |
| 115 |
offsetof(struct icmp_mib, icmpOutParmProbs) }, |
| 116 |
{ "Icmp:", "OutSrcQuenchs", &cached_icmp_mib, |
| 117 |
offsetof(struct icmp_mib, icmpOutSrcQuenchs) }, |
| 118 |
{ "Icmp:", "OutRedirects", &cached_icmp_mib, |
| 119 |
offsetof(struct icmp_mib, icmpOutRedirects) }, |
| 120 |
{ "Icmp:", "OutEchos", &cached_icmp_mib, |
| 121 |
offsetof(struct icmp_mib, icmpOutEchos) }, |
| 122 |
{ "Icmp:", "OutEchoReps", &cached_icmp_mib, |
| 123 |
offsetof(struct icmp_mib, icmpOutEchoReps) }, |
| 124 |
{ "Icmp:", "OutTimestamps", &cached_icmp_mib, |
| 125 |
offsetof(struct icmp_mib, icmpOutTimestamps) }, |
| 126 |
{ "Icmp:", "OutTimestampReps", &cached_icmp_mib, |
| 127 |
offsetof(struct icmp_mib, icmpOutTimestampReps) }, |
| 128 |
{ "Icmp:", "OutAddrMasks", &cached_icmp_mib, |
| 129 |
offsetof(struct icmp_mib, icmpOutAddrMasks) }, |
| 130 |
{ "Icmp:", "OutAddrMaskReps", &cached_icmp_mib, |
| 131 |
offsetof(struct icmp_mib, icmpOutAddrMaskReps) }, |
| 132 |
|
| 133 |
{ "Tcp:", "RtoAlgorithm", &cached_tcp_mib, |
| 134 |
offsetof(struct tcp_mib, tcpRtoAlgorithm) }, |
| 135 |
{ "Tcp:", "RtoMin", &cached_tcp_mib, |
| 136 |
offsetof(struct tcp_mib, tcpRtoMin) }, |
| 137 |
{ "Tcp:", "RtoMax", &cached_tcp_mib, |
| 138 |
offsetof(struct tcp_mib, tcpRtoMax) }, |
| 139 |
{ "Tcp:", "MaxConn", &cached_tcp_mib, |
| 140 |
offsetof(struct tcp_mib, tcpMaxConn) }, |
| 141 |
{ "Tcp:", "ActiveOpens", &cached_tcp_mib, |
| 142 |
offsetof(struct tcp_mib, tcpActiveOpens) }, |
| 143 |
{ "Tcp:", "PassiveOpens", &cached_tcp_mib, |
| 144 |
offsetof(struct tcp_mib, tcpPassiveOpens) }, |
| 145 |
{ "Tcp:", "AttemptFails", &cached_tcp_mib, |
| 146 |
offsetof(struct tcp_mib, tcpAttemptFails) }, |
| 147 |
{ "Tcp:", "EstabResets", &cached_tcp_mib, |
| 148 |
offsetof(struct tcp_mib, tcpEstabResets) }, |
| 149 |
{ "Tcp:", "CurrEstab", &cached_tcp_mib, |
| 150 |
offsetof(struct tcp_mib, tcpCurrEstab) }, |
| 151 |
{ "Tcp:", "InSegs", &cached_tcp_mib, |
| 152 |
offsetof(struct tcp_mib, tcpInSegs) }, |
| 153 |
{ "Tcp:", "OutSegs", &cached_tcp_mib, |
| 154 |
offsetof(struct tcp_mib, tcpOutSegs) }, |
| 155 |
{ "Tcp:", "RetransSegs", &cached_tcp_mib, |
| 156 |
offsetof(struct tcp_mib, tcpRetransSegs) }, |
| 157 |
{ "Tcp:", "InErrs", &cached_tcp_mib, |
| 158 |
offsetof(struct tcp_mib, tcpInErrs), |
| 159 |
offsetof(struct tcp_mib, tcpInErrsValid) }, |
| 160 |
{ "Tcp:", "OutRsts", &cached_tcp_mib, |
| 161 |
offsetof(struct tcp_mib, tcpOutRsts), |
| 162 |
offsetof(struct tcp_mib, tcpOutRstsValid) }, |
| 163 |
|
| 164 |
{ "Udp:", "InDatagrams", &cached_udp_mib, |
| 165 |
offsetof(struct udp_mib, udpInDatagrams) }, |
| 166 |
{ "Udp:", "NoPorts", &cached_udp_mib, |
| 167 |
offsetof(struct udp_mib, udpNoPorts) }, |
| 168 |
{ "Udp:", "InErrors", &cached_udp_mib, |
| 169 |
offsetof(struct udp_mib, udpInErrors) }, |
| 170 |
{ "Udp:", "OutDatagrams", &cached_udp_mib, |
| 171 |
offsetof(struct udp_mib, udpOutDatagrams) }, |
| 172 |
}; |
| 173 |
|
| 174 |
#define IP6_STATS_LINE "Ip6" |
| 175 |
#define ICMP6_STATS_LINE "Icmp6" |
| 176 |
#define UDP6_STATS_LINE "Udp6" |
| 177 |
|
| 178 |
#define IP6_STATS_PREFIX_LEN 3 |
| 179 |
#define ICMP6_STATS_PREFIX_LEN 5 |
| 180 |
#define UDP6_STATS_PREFIX_LEN 4 |
| 181 |
|
| 182 |
netsnmp_feature_child_of(linux_ip6_stat_all, libnetsnmpmibs); |
| 183 |
|
| 184 |
netsnmp_feature_child_of(linux_read_ip6_stat, linux_ip6_stat_all); |
| 185 |
|
| 186 |
static int |
| 187 |
decode_icmp_msg(char *line, char *data, struct icmp4_msg_mib *msg) |
| 188 |
{ |
| 189 |
char *token, *saveptr, *lineptr, *saveptr1, *dataptr, *delim = NULL; |
| 190 |
char line_cpy[1024]; |
| 191 |
char data_cpy[1024]; |
| 192 |
long index; |
| 193 |
|
| 194 |
if(data == NULL) |
| 195 |
return -1; |
| 196 |
|
| 197 |
/* |
| 198 |
* Since we are using strtok, there is a possiblity of the orginal data |
| 199 |
* getting modified. So we take a local copy for this purpose even though |
| 200 |
* its expensive. |
| 201 |
*/ |
| 202 |
strlcpy(line_cpy, line, sizeof(line_cpy)); |
| 203 |
strlcpy(data_cpy, data, sizeof(data_cpy)); |
| 204 |
|
| 205 |
lineptr = line_cpy; |
| 206 |
dataptr = data_cpy; |
| 207 |
saveptr1 = saveptr = NULL; |
| 208 |
while (1) { |
| 209 |
if(NULL == (token = strtok_r(lineptr, " ", &saveptr))) |
| 210 |
break; |
| 211 |
lineptr = NULL; |
| 212 |
errno = 0; |
| 213 |
if (0 == strncmp(strsep(&token, "e"), "OutTyp", 6)) { |
| 214 |
index = strtol(token, &delim, 0); |
| 215 |
if (ERANGE == errno) { |
| 216 |
continue; |
| 217 |
} else if (index > 255) { |
| 218 |
continue; |
| 219 |
} else if (index < 0) { |
| 220 |
continue; |
| 221 |
} |
| 222 |
if (NULL == (token = strtok_r(dataptr, " ", &saveptr1))) |
| 223 |
break; |
| 224 |
dataptr = NULL; |
| 225 |
msg->vals[index].OutType = atoi(token); |
| 226 |
} else { |
| 227 |
index = strtol(token, &delim, 0); |
| 228 |
if (ERANGE == errno) { |
| 229 |
continue; |
| 230 |
} else if (index > 255) { |
| 231 |
continue; |
| 232 |
} else if (index < 0) { |
| 233 |
continue; |
| 234 |
} |
| 235 |
if(NULL == (token = strtok_r(dataptr, " ", &saveptr1))) |
| 236 |
break; |
| 237 |
dataptr = NULL; |
| 238 |
msg->vals[index].InType = atoi(token); |
| 239 |
} |
| 240 |
} |
| 241 |
return 0; |
| 242 |
} |
| 243 |
|
| 244 |
static int |
| 245 |
linux_read_mibII_stats(void) |
| 246 |
{ |
| 247 |
FILE *in; |
| 248 |
char line[1024], data[1024], *linepos, *datapos, *pfx, *hdr, *v; |
| 249 |
const struct stats_descr *d; |
| 250 |
const struct stats_descr *const end = ipv4_snmp_stats + |
| 251 |
sizeof(ipv4_snmp_stats) / sizeof(ipv4_snmp_stats[0]); |
| 252 |
int ret = 0; |
| 253 |
|
| 254 |
in = fopen("/proc/net/snmp", "r"); |
| 255 |
if (!in) { |
| 256 |
DEBUGMSGTL(("mibII/kernel_linux","Unable to open /proc/net/snmp")); |
| 257 |
return -1; |
| 258 |
} |
| 259 |
|
| 260 |
while (fgets(line, sizeof(line), in) && fgets(data, sizeof(data), in)) { |
| 261 |
if (!(pfx = strtok_r(line, " ", &linepos))) |
| 262 |
continue; |
| 263 |
if (!strtok_r(data, " ", &datapos)) |
| 264 |
continue; |
| 265 |
if (strcmp(pfx, "IcmpMsg:") == 0) { |
| 266 |
decode_icmp_msg(linepos, datapos, &cached_icmp4_msg_mib); |
| 267 |
} else { |
| 268 |
while ((hdr = strtok_r(linepos, " \n", &linepos)) && |
| 269 |
(v = strtok_r(datapos, " \n", &datapos))) { |
| 270 |
for (d = ipv4_snmp_stats; d < end; d++) { |
| 271 |
if (strcmp(d->prefix, pfx) == 0 && |
| 272 |
strcmp(d->col_name, hdr) == 0) { |
| 273 |
*(unsigned long *)(d->var + d->offset) = atol(v); |
| 274 |
if (d->validity_offset != 0) { |
| 275 |
*(short *)(d->var + d->validity_offset) = 1; |
| 276 |
} |
| 277 |
break; |
| 278 |
} |
| 279 |
} |
| 280 |
if (d == end) |
| 281 |
DEBUGMSGTL(("mibII/kernel_linux", "Skipped %s %s %s\n", pfx, |
| 282 |
hdr, v)); |
| 283 |
} |
| 284 |
} |
| 285 |
} |
| 286 |
fclose(in); |
| 287 |
|
| 288 |
/* |
| 289 |
* Tweak illegal values: |
| 290 |
* |
| 291 |
* valid values for ipForwarding are 1 == yup, 2 == nope |
| 292 |
* a 0 is forbidden, so patch: |
| 293 |
*/ |
| 294 |
if (!cached_ip_mib.ipForwarding) |
| 295 |
cached_ip_mib.ipForwarding = 2; |
| 296 |
|
| 297 |
/* |
| 298 |
* 0 is illegal for tcpRtoAlgorithm |
| 299 |
* so assume `other' algorithm: |
| 300 |
*/ |
| 301 |
if (!cached_tcp_mib.tcpRtoAlgorithm) |
| 302 |
cached_tcp_mib.tcpRtoAlgorithm = 1; |
| 303 |
|
| 304 |
return ret; |
| 305 |
} |
| 306 |
|
| 307 |
int |
| 308 |
linux_read_ip_stat(struct ip_mib *ipstat) |
| 309 |
{ |
| 310 |
memset((char *) ipstat, (0), sizeof(*ipstat)); |
| 311 |
if (linux_read_mibII_stats() == -1) |
| 312 |
return -1; |
| 313 |
memcpy((char *) ipstat, (char *) &cached_ip_mib, sizeof(*ipstat)); |
| 314 |
return 0; |
| 315 |
} |
| 316 |
|
| 317 |
#ifndef NETSNMP_FEATURE_REMOVE_LINUX_READ_IP6_STAT |
| 318 |
int linux_read_ip6_stat( struct ip6_mib *ip6stat) |
| 319 |
{ |
| 320 |
#ifdef NETSNMP_ENABLE_IPV6 |
| 321 |
FILE *in; |
| 322 |
char line[1024]; |
| 323 |
unsigned long stats; |
| 324 |
char *endp; |
| 325 |
int match; |
| 326 |
#endif |
| 327 |
|
| 328 |
memset((char *) ip6stat, (0), sizeof(*ip6stat)); |
| 329 |
|
| 330 |
#ifdef NETSNMP_ENABLE_IPV6 |
| 331 |
DEBUGMSGTL(("mibII/kernel_linux/ip6stats", |
| 332 |
"Reading /proc/net/snmp6 stats\n")); |
| 333 |
if (NULL == (in = fopen("/proc/net/snmp6", "r"))) { |
| 334 |
DEBUGMSGTL(("mibII/kernel_linux/ip6stats", |
| 335 |
"Failed to open /proc/net/snmp6\n")); |
| 336 |
return -1; |
| 337 |
} |
| 338 |
|
| 339 |
while (NULL != fgets(line, sizeof(line), in)) { |
| 340 |
if (0 != strncmp(line, IP6_STATS_LINE, IP6_STATS_PREFIX_LEN)) |
| 341 |
continue; |
| 342 |
|
| 343 |
if (1 != sscanf(line, "%*s %lu", &stats)) |
| 344 |
continue; |
| 345 |
|
| 346 |
endp = strchr(line, ' '); |
| 347 |
if (endp) |
| 348 |
*endp = '\0'; |
| 349 |
DEBUGMSGTL(("mibII/kernel_linux/ip6stats", "Find tag: %s\n", line)); |
| 350 |
|
| 351 |
match = 1; |
| 352 |
if (0 == strncmp(line + 3, "In", 2)) { /* In */ |
| 353 |
if (0 == strcmp(line + 5, "AddrErrors")) { |
| 354 |
cached_ip6_mib.ip6InAddrErrors = stats; |
| 355 |
} else if (0 == strcmp(line + 5, "Delivers")) { |
| 356 |
cached_ip6_mib.ip6InDelivers = stats; |
| 357 |
} else if (0 == strcmp(line + 5, "Discards")) { |
| 358 |
cached_ip6_mib.ip6InDiscards = stats; |
| 359 |
} else if (0 == strcmp(line + 5, "HdrErrors")) { |
| 360 |
cached_ip6_mib.ip6InHdrErrors = stats; |
| 361 |
} else if (0 == strcmp(line + 5, "McastPkts")) { |
| 362 |
cached_ip6_mib.ip6InMcastPkts = stats; |
| 363 |
} else if (0 == strcmp(line + 5, "NoRoutes")) { |
| 364 |
cached_ip6_mib.ip6InNoRoutes = stats; |
| 365 |
} else if (0 == strcmp(line + 5, "Receives")) { |
| 366 |
cached_ip6_mib.ip6InReceives = stats; |
| 367 |
} else if (0 == strcmp(line + 5, "TruncatedPkts")) { |
| 368 |
cached_ip6_mib.ip6InTruncatedPkts = stats; |
| 369 |
} else if (0 == strcmp(line + 5, "TooBigErrors")) { |
| 370 |
cached_ip6_mib.ip6InTooBigErrors = stats; |
| 371 |
} else if (0 == strcmp(line + 5, "UnknownProtos")) { |
| 372 |
cached_ip6_mib.ip6InUnknownProtos = stats; |
| 373 |
} else { |
| 374 |
match = 0; |
| 375 |
} |
| 376 |
} else if (0 == strncmp(line + 3, "Out", 3)) { /* Out */ |
| 377 |
if (0 == strcmp(line + 6, "Discards")) { |
| 378 |
cached_ip6_mib.ip6OutDiscards = stats; |
| 379 |
} else if (0 == strcmp(line + 6, "ForwDatagrams")) { |
| 380 |
cached_ip6_mib.ip6OutForwDatagrams = stats; |
| 381 |
} else if (0 == strcmp(line + 6, "McastPkts")) { |
| 382 |
cached_ip6_mib.ip6OutMcastPkts = stats; |
| 383 |
} else if (0 == strcmp(line + 6, "NoRoutes")) { |
| 384 |
cached_ip6_mib.ip6OutNoRoutes = stats; |
| 385 |
} else if (0 == strcmp(line + 6, "Requests")) { |
| 386 |
cached_ip6_mib.ip6OutRequests = stats; |
| 387 |
} else { |
| 388 |
match = 0; |
| 389 |
} |
| 390 |
} else if (0 == strncmp(line + 3, "Reasm", 5)) { /* Reasm */ |
| 391 |
if (0 == strcmp(line + 8, "Fails")) { |
| 392 |
cached_ip6_mib.ip6ReasmFails = stats; |
| 393 |
} else if (0 == strcmp(line + 8, "OKs")) { |
| 394 |
cached_ip6_mib.ip6ReasmOKs = stats; |
| 395 |
} else if (0 == strcmp(line + 8, "Reqds")) { |
| 396 |
cached_ip6_mib.ip6ReasmReqds = stats; |
| 397 |
} else if (0 == strcmp(line + 8, "Timeout")) { |
| 398 |
cached_ip6_mib.ip6ReasmTimeout = stats; |
| 399 |
} else { |
| 400 |
match = 0; |
| 401 |
} |
| 402 |
} else if (0 == strncmp(line + 3, "Frag", 4)) { /* Frag */ |
| 403 |
if (0 == strcmp(line + 7, "Creates")) { |
| 404 |
cached_ip6_mib.ip6FragCreates = stats; |
| 405 |
} else if (0 == strcmp(line + 7, "Fails")) { |
| 406 |
cached_ip6_mib.ip6FragFails = stats; |
| 407 |
} else if (0 == strcmp(line + 7, "OKs")) { |
| 408 |
cached_ip6_mib.ip6FragOKs = stats; |
| 409 |
} else { |
| 410 |
match = 0; |
| 411 |
} |
| 412 |
} else { |
| 413 |
match = 0; |
| 414 |
} |
| 415 |
|
| 416 |
if(!match) |
| 417 |
DEBUGMSGTL(("mibII/kernel_linux/ip6stats", |
| 418 |
"%s is an unknown tag\n", line)); |
| 419 |
} |
| 420 |
|
| 421 |
fclose(in); |
| 422 |
#endif |
| 423 |
|
| 424 |
memcpy((char *) ip6stat, (char *) &cached_ip6_mib, sizeof(*ip6stat)); |
| 425 |
return 0; |
| 426 |
} |
| 427 |
#endif /* NETSNMP_FEATURE_REMOVE_LINUX_READ_IP6_STAT */ |
| 428 |
|
| 429 |
int |
| 430 |
linux_read_icmp_msg_stat(struct icmp_mib *icmpstat, |
| 431 |
struct icmp4_msg_mib *icmpmsgstat, |
| 432 |
int *flag) |
| 433 |
{ |
| 434 |
int ret; |
| 435 |
|
| 436 |
memset(icmpstat, 0, sizeof(*icmpstat)); |
| 437 |
memset(icmpmsgstat, 0, sizeof(*icmpmsgstat)); |
| 438 |
|
| 439 |
if ((ret = linux_read_mibII_stats()) == -1) { |
| 440 |
return -1; |
| 441 |
} else if (ret) { |
| 442 |
memcpy(icmpmsgstat, &cached_icmp4_msg_mib, sizeof(*icmpmsgstat)); |
| 443 |
*flag = 1; /* We have a valid icmpmsg */ |
| 444 |
} |
| 445 |
|
| 446 |
memcpy(icmpstat, &cached_icmp_mib, sizeof(*icmpstat)); |
| 447 |
return 0; |
| 448 |
} |
| 449 |
|
| 450 |
int |
| 451 |
linux_read_icmp_stat(struct icmp_mib *icmpstat) |
| 452 |
{ |
| 453 |
memset((char *) icmpstat, (0), sizeof(*icmpstat)); |
| 454 |
if (linux_read_mibII_stats() == -1) |
| 455 |
return -1; |
| 456 |
memcpy((char *) icmpstat, (char *) &cached_icmp_mib, |
| 457 |
sizeof(*icmpstat)); |
| 458 |
return 0; |
| 459 |
} |
| 460 |
|
| 461 |
static int |
| 462 |
linux_read_icmp6_parse(struct icmp6_mib *icmp6stat, |
| 463 |
struct icmp6_msg_mib *icmp6msgstat, |
| 464 |
int *support) |
| 465 |
{ |
| 466 |
#ifdef NETSNMP_ENABLE_IPV6 |
| 467 |
FILE *in; |
| 468 |
char line[1024]; |
| 469 |
char name[255]; |
| 470 |
unsigned long stats; |
| 471 |
char *endp, *vals; |
| 472 |
int match; |
| 473 |
#endif |
| 474 |
|
| 475 |
memset(icmp6stat, 0, sizeof(*icmp6stat)); |
| 476 |
if (NULL != icmp6msgstat) |
| 477 |
memset(icmp6msgstat, 0, sizeof(*icmp6msgstat)); |
| 478 |
|
| 479 |
#ifdef NETSNMP_ENABLE_IPV6 |
| 480 |
DEBUGMSGTL(("mibII/kernel_linux/icmp6stats", |
| 481 |
"Reading /proc/net/snmp6 stats\n")); |
| 482 |
if (NULL == (in = fopen("/proc/net/snmp6", "r"))) { |
| 483 |
DEBUGMSGTL(("mibII/kernel_linux/icmp6stats", |
| 484 |
"Failed to open /proc/net/snmp6\n")); |
| 485 |
return -1; |
| 486 |
} |
| 487 |
|
| 488 |
while (NULL != fgets(line, sizeof(line), in)) { |
| 489 |
if (0 != strncmp(line, ICMP6_STATS_LINE, ICMP6_STATS_PREFIX_LEN)) |
| 490 |
continue; |
| 491 |
|
| 492 |
if (2 != sscanf(line, "%s %lu", name, &stats)) |
| 493 |
continue; |
| 494 |
|
| 495 |
endp = strchr(line, ' '); |
| 496 |
if (endp) |
| 497 |
*endp = '\0'; |
| 498 |
DEBUGMSGTL(("mibII/kernel_linux/icmp6stats", "Find tag: %s\n", line)); |
| 499 |
|
| 500 |
vals = name; |
| 501 |
if (NULL != icmp6msgstat) { |
| 502 |
int type; |
| 503 |
if (0 == strncmp(name, "Icmp6OutType", 12)) { |
| 504 |
strsep(&vals, "e"); |
| 505 |
type = atoi(vals); |
| 506 |
if ( type < 0 || type > 255 ) |
| 507 |
continue; |
| 508 |
icmp6msgstat->vals[type].OutType = stats; |
| 509 |
*support = 1; |
| 510 |
continue; |
| 511 |
} else if (0 == strncmp(name, "Icmp6InType", 11)) { |
| 512 |
strsep(&vals, "e"); |
| 513 |
type = atoi(vals); |
| 514 |
if ( type < 0 || type > 255 ) |
| 515 |
continue; |
| 516 |
icmp6msgstat->vals[type].InType = stats; |
| 517 |
*support = 1; |
| 518 |
continue; |
| 519 |
} |
| 520 |
} |
| 521 |
|
| 522 |
match = 1; |
| 523 |
if (0 == strncmp(line + 5, "In", 2)) { /* In */ |
| 524 |
if (0 == strcmp(line + 7, "DestUnreachs")) { |
| 525 |
cached_icmp6_mib.icmp6InDestUnreachs = stats; |
| 526 |
} else if (0 == strcmp(line + 7, "Echos")) { |
| 527 |
cached_icmp6_mib.icmp6InEchos = stats; |
| 528 |
} else if (0 == strcmp(line + 7, "EchoReplies")) { |
| 529 |
cached_icmp6_mib.icmp6InEchoReplies = stats; |
| 530 |
} else if (0 == strcmp(line + 7, "Errors")) { |
| 531 |
cached_icmp6_mib.icmp6InErrors = stats; |
| 532 |
} else if (0 == strcmp(line + 7, "GroupMembQueries")) { |
| 533 |
cached_icmp6_mib.icmp6InGroupMembQueries = stats; |
| 534 |
} else if (0 == strcmp(line + 7, "GroupMembReductions")) { |
| 535 |
cached_icmp6_mib.icmp6InGroupMembReductions = stats; |
| 536 |
} else if (0 == strcmp(line + 7, "GroupMembResponses")) { |
| 537 |
cached_icmp6_mib.icmp6InGroupMembResponses = stats; |
| 538 |
} else if (0 == strcmp(line + 7, "Msgs")) { |
| 539 |
cached_icmp6_mib.icmp6InMsgs = stats; |
| 540 |
} else if (0 == strcmp(line + 7, "NeighborAdvertisements")) { |
| 541 |
cached_icmp6_mib.icmp6InNeighborAdvertisements = stats; |
| 542 |
} else if (0 == strcmp(line + 7, "NeighborSolicits")) { |
| 543 |
cached_icmp6_mib.icmp6InNeighborSolicits = stats; |
| 544 |
} else if (0 == strcmp(line + 7, "PktTooBigs")) { |
| 545 |
cached_icmp6_mib.icmp6InPktTooBigs = stats; |
| 546 |
} else if (0 == strcmp(line + 7, "ParmProblems")) { |
| 547 |
cached_icmp6_mib.icmp6InParmProblems = stats; |
| 548 |
} else if (0 == strcmp(line + 7, "Redirects")) { |
| 549 |
cached_icmp6_mib.icmp6InRedirects = stats; |
| 550 |
} else if (0 == strcmp(line + 7, "RouterAdvertisements")) { |
| 551 |
cached_icmp6_mib.icmp6InRouterAdvertisements = stats; |
| 552 |
} else if (0 == strcmp(line + 7, "RouterSolicits")) { |
| 553 |
cached_icmp6_mib.icmp6InRouterSolicits = stats; |
| 554 |
} else if (0 == strcmp(line + 7, "TimeExcds")) { |
| 555 |
cached_icmp6_mib.icmp6InTimeExcds = stats; |
| 556 |
} else { |
| 557 |
match = 0; |
| 558 |
} |
| 559 |
} else if (0 == strncmp(line + 5, "Out", 3)) { /* Out */ |
| 560 |
if (0 == strcmp(line + 8, "DestUnreachs")) { |
| 561 |
cached_icmp6_mib.icmp6OutDestUnreachs = stats; |
| 562 |
} else if (0 == strcmp(line + 8, "Echos")) { |
| 563 |
cached_icmp6_mib.icmp6OutEchos = stats; |
| 564 |
} else if (0 == strcmp(line + 8, "EchoReplies")) { |
| 565 |
cached_icmp6_mib.icmp6OutEchoReplies = stats; |
| 566 |
} else if (0 == strcmp(line + 8, "GroupMembReductions")) { |
| 567 |
cached_icmp6_mib.icmp6OutGroupMembReductions = stats; |
| 568 |
} else if (0 == strcmp(line + 8, "GroupMembResponses")) { |
| 569 |
cached_icmp6_mib.icmp6OutGroupMembResponses = stats; |
| 570 |
} else if (0 == strcmp(line + 8, "Msgs")) { |
| 571 |
cached_icmp6_mib.icmp6OutMsgs = stats; |
| 572 |
} else if (0 == strcmp(line + 8, "NeighborAdvertisements")) { |
| 573 |
cached_icmp6_mib.icmp6OutNeighborAdvertisements = stats; |
| 574 |
} else if (0 == strcmp(line + 8, "NeighborSolicits")) { |
| 575 |
cached_icmp6_mib.icmp6OutNeighborSolicits = stats; |
| 576 |
} else if (0 == strcmp(line + 8, "PktTooBigs")) { |
| 577 |
cached_icmp6_mib.icmp6OutPktTooBigs = stats; |
| 578 |
} else if (0 == strcmp(line + 8, "ParmProblems")) { |
| 579 |
cached_icmp6_mib.icmp6OutParmProblems = stats; |
| 580 |
} else if (0 == strcmp(line + 8, "Redirects")) { |
| 581 |
cached_icmp6_mib.icmp6OutRedirects = stats; |
| 582 |
} else if (0 == strcmp(line + 8, "RouterSolicits")) { |
| 583 |
cached_icmp6_mib.icmp6OutRouterSolicits = stats; |
| 584 |
} else if (0 == strcmp(line + 8, "TimeExcds")) { |
| 585 |
cached_icmp6_mib.icmp6OutTimeExcds = stats; |
| 586 |
} else { |
| 587 |
match = 0; |
| 588 |
} |
| 589 |
} else { |
| 590 |
match = 0; |
| 591 |
} |
| 592 |
if(!match) |
| 593 |
DEBUGMSGTL(("mibII/kernel_linux/icmp6stats", |
| 594 |
"%s is an unknown tag\n", line)); |
| 595 |
} |
| 596 |
|
| 597 |
fclose(in); |
| 598 |
#endif |
| 599 |
|
| 600 |
memcpy((char *) icmp6stat, (char *) &cached_icmp6_mib, |
| 601 |
sizeof(*icmp6stat)); |
| 602 |
return 0; |
| 603 |
} |
| 604 |
|
| 605 |
int |
| 606 |
linux_read_icmp6_msg_stat(struct icmp6_mib *icmp6stat, |
| 607 |
struct icmp6_msg_mib *icmp6msgstat, |
| 608 |
int *support) |
| 609 |
{ |
| 610 |
if (linux_read_icmp6_parse(icmp6stat, icmp6msgstat, support) < 0) |
| 611 |
return -1; |
| 612 |
else |
| 613 |
return 0; |
| 614 |
} |
| 615 |
|
| 616 |
int |
| 617 |
linux_read_icmp6_stat(struct icmp6_mib *icmp6stat) |
| 618 |
{ |
| 619 |
if (linux_read_icmp6_parse(icmp6stat, NULL, NULL) < 0) |
| 620 |
return -1; |
| 621 |
else |
| 622 |
return 0; |
| 623 |
} |
| 624 |
|
| 625 |
int |
| 626 |
linux_read_tcp_stat(struct tcp_mib *tcpstat) |
| 627 |
{ |
| 628 |
memset((char *) tcpstat, (0), sizeof(*tcpstat)); |
| 629 |
if (linux_read_mibII_stats() == -1) |
| 630 |
return -1; |
| 631 |
memcpy((char *) tcpstat, (char *) &cached_tcp_mib, sizeof(*tcpstat)); |
| 632 |
return 0; |
| 633 |
} |
| 634 |
|
| 635 |
int |
| 636 |
linux_read_udp_stat(struct udp_mib *udpstat) |
| 637 |
{ |
| 638 |
memset((char *) udpstat, (0), sizeof(*udpstat)); |
| 639 |
if (linux_read_mibII_stats() == -1) |
| 640 |
return -1; |
| 641 |
|
| 642 |
#ifdef NETSNMP_ENABLE_IPV6 |
| 643 |
{ |
| 644 |
struct udp6_mib udp6stat; |
| 645 |
memset(&udp6stat, 0, sizeof(udp6stat)); |
| 646 |
|
| 647 |
if (linux_read_udp6_stat(&udp6stat) == 0) { |
| 648 |
cached_udp_mib.udpOutDatagrams += udp6stat.udp6OutDatagrams; |
| 649 |
cached_udp_mib.udpNoPorts += udp6stat.udp6NoPorts; |
| 650 |
cached_udp_mib.udpInDatagrams += udp6stat.udp6InDatagrams; |
| 651 |
cached_udp_mib.udpInErrors += udp6stat.udp6InErrors; |
| 652 |
} |
| 653 |
} |
| 654 |
#endif |
| 655 |
memcpy((char *) udpstat, (char *) &cached_udp_mib, sizeof(*udpstat)); |
| 656 |
return 0; |
| 657 |
} |
| 658 |
|
| 659 |
int |
| 660 |
linux_read_udp6_stat(struct udp6_mib *udp6stat) |
| 661 |
{ |
| 662 |
#ifdef NETSNMP_ENABLE_IPV6 |
| 663 |
FILE *in; |
| 664 |
char line[1024]; |
| 665 |
unsigned long stats; |
| 666 |
char *endp; |
| 667 |
#endif |
| 668 |
|
| 669 |
memset(udp6stat, 0, sizeof(*udp6stat)); |
| 670 |
|
| 671 |
#ifdef NETSNMP_ENABLE_IPV6 |
| 672 |
DEBUGMSGTL(("mibII/kernel_linux/udp6stats", |
| 673 |
"Reading /proc/net/snmp6 stats\n")); |
| 674 |
if (NULL == (in = fopen("/proc/net/snmp6", "r"))) { |
| 675 |
DEBUGMSGTL(("mibII/kernel_linux/udp6stats", |
| 676 |
"Failed to open /proc/net/snmp6\n")); |
| 677 |
return -1; |
| 678 |
} |
| 679 |
|
| 680 |
while (NULL != fgets(line, sizeof(line), in)) { |
| 681 |
if (0 != strncmp(line, UDP6_STATS_LINE, UDP6_STATS_PREFIX_LEN)) |
| 682 |
continue; |
| 683 |
|
| 684 |
if (1 != sscanf(line, "%*s %lu", &stats)) |
| 685 |
continue; |
| 686 |
|
| 687 |
endp = strchr(line, ' '); |
| 688 |
if (endp) |
| 689 |
*endp = '\0'; |
| 690 |
DEBUGMSGTL(("mibII/kernel_linux/udp6stats", "Find tag: %s\n", line)); |
| 691 |
|
| 692 |
if (0 == strcmp(line + 4, "OutDatagrams")) { |
| 693 |
cached_udp6_mib.udp6OutDatagrams = stats; |
| 694 |
} else if (0 == strcmp(line + 4, "NoPorts")) { |
| 695 |
cached_udp6_mib.udp6NoPorts = stats; |
| 696 |
} else if (0 == strcmp(line + 4, "InDatagrams")) { |
| 697 |
cached_udp6_mib.udp6InDatagrams = stats; |
| 698 |
} else if (0 == strcmp(line + 4, "InErrors")) { |
| 699 |
cached_udp6_mib.udp6InErrors = stats; |
| 700 |
} else { |
| 701 |
DEBUGMSGTL(("mibII/kernel_linux/udp6stats", |
| 702 |
"%s is an unknown tag\n", line)); |
| 703 |
} |
| 704 |
} |
| 705 |
|
| 706 |
fclose(in); |
| 707 |
#endif |
| 708 |
|
| 709 |
memcpy((char *) udp6stat, (char *) &cached_udp6_mib, sizeof(*udp6stat)); |
| 710 |
return 0; |
| 711 |
} |