summaryrefslogtreecommitdiff
path: root/agent/mibgroup/mibII/kernel_linux.c
blob: ba320c7f884dc21fef5b9d44359ae264c9034891 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
/*
 *  Linux kernel interface
 *
 */

#include <net-snmp/net-snmp-config.h>
#include <net-snmp/net-snmp-features.h>
#include <net-snmp/net-snmp-includes.h>
#include <net-snmp/agent/net-snmp-agent-includes.h>

#if HAVE_STRING_H
#include <string.h>
#endif
#include <sys/types.h>
#if HAVE_SYS_PARAM_H
#include <sys/param.h>
#endif
#include <errno.h>

#include "kernel_linux.h"

struct ip_mib   cached_ip_mib;
struct ip6_mib   cached_ip6_mib;
struct icmp_mib cached_icmp_mib;
struct icmp6_mib cached_icmp6_mib;
struct icmp4_msg_mib cached_icmp4_msg_mib;
struct tcp_mib  cached_tcp_mib;
struct udp_mib  cached_udp_mib;
struct udp6_mib  cached_udp6_mib;

#define IP_STATS_LINE	"Ip: %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu"
#define ICMP_STATS_LINE	"Icmp: %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu"
#define ICMP_MSG_STATS_LINE "IcmpMsg: "
#define TCP_STATS_LINE	"Tcp: %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu"
#define UDP_STATS_LINE	"Udp: %lu %lu %lu %lu"
#define IP6_STATS_LINE   "Ip6"
#define ICMP6_STATS_LINE "Icmp6"
#define UDP6_STATS_LINE "Udp6"

#define IP_STATS_PREFIX_LEN	4
#define ICMP_STATS_PREFIX_LEN	6
#define ICMP_MSG_STATS_PREFIX_LEN 9
#define TCP_STATS_PREFIX_LEN	5
#define UDP_STATS_PREFIX_LEN	5
#define IP6_STATS_PREFIX_LEN	3
#define ICMP6_STATS_PREFIX_LEN	5
#define UDP6_STATS_PREFIX_LEN   4

netsnmp_feature_child_of(linux_ip6_stat_all, libnetsnmpmibs)

netsnmp_feature_child_of(linux_read_ip6_stat, linux_ip6_stat_all)

int
decode_icmp_msg(char *line, char *data, struct icmp4_msg_mib *msg)
{
    char *token, *saveptr, *lineptr, *saveptr1, *dataptr, *delim = NULL;
    char line_cpy[1024];
    char data_cpy[1024];
    long index;

    if(data == NULL)
        return -1;

    /*
     * Since we are using strtok, there is a possiblity of the orginal data
     * getting modified. So we take a local copy for this purpose even though
     * its expensive.
     */
    strlcpy(line_cpy, line, sizeof(line_cpy));
    strlcpy(data_cpy, data, sizeof(data_cpy));

    lineptr = line_cpy;
    dataptr = data_cpy;
    saveptr1 = NULL;
    while (1) {
        if(NULL == (token = strtok_r(lineptr, " ", &saveptr)))
            break;
        lineptr = NULL;
        errno = 0;
        if (0 == strncmp(strsep(&token, "e"), "OutTyp", 6)) {
            index = strtol(token, &delim, 0);
            if (ERANGE == errno) {
                continue;
            } else if (index > 255) {
                continue;
            } else if (index < 0) {
                continue;
            }
            if (NULL == (token = strtok_r(dataptr, " ", &saveptr1)))
                break;
            dataptr = NULL;
            msg->vals[index].OutType = atoi(token);
        } else {
            index = strtol(token, &delim, 0);
            if (ERANGE == errno) {
                continue;
            } else if (index > 255) {
                continue;
            } else if (index < 0) {
                continue;
            }
            if(NULL == (token = strtok_r(dataptr, " ", &saveptr1)))
                break;
            dataptr = NULL;
            msg->vals[index].InType = atoi(token);
        }
    }
    return 0;
}

int
linux_read_mibII_stats(void)
{
    FILE           *in = fopen("/proc/net/snmp", "r");
    char            line[1024], data[1024];
    int ret = 0;
    if (!in) {
        DEBUGMSGTL(("mibII/kernel_linux","Unable to open /proc/net/snmp"));
        return -1;
    }


    memset(line, '\0', sizeof(line));
    memset(data, '\0', sizeof(data));
    while (line == fgets(line, sizeof(line), in)) {
        if (!strncmp(line, IP_STATS_LINE, IP_STATS_PREFIX_LEN)) {
            sscanf(line, IP_STATS_LINE,
                   &cached_ip_mib.ipForwarding,
                   &cached_ip_mib.ipDefaultTTL,
                   &cached_ip_mib.ipInReceives,
                   &cached_ip_mib.ipInHdrErrors,
                   &cached_ip_mib.ipInAddrErrors,
                   &cached_ip_mib.ipForwDatagrams,
                   &cached_ip_mib.ipInUnknownProtos,
                   &cached_ip_mib.ipInDiscards,
                   &cached_ip_mib.ipInDelivers,
                   &cached_ip_mib.ipOutRequests,
                   &cached_ip_mib.ipOutDiscards,
                   &cached_ip_mib.ipOutNoRoutes,
                   &cached_ip_mib.ipReasmTimeout,
                   &cached_ip_mib.ipReasmReqds,
                   &cached_ip_mib.ipReasmOKs,
                   &cached_ip_mib.ipReasmFails,
                   &cached_ip_mib.ipFragOKs,
                   &cached_ip_mib.ipFragFails,
                   &cached_ip_mib.ipFragCreates);
            cached_ip_mib.ipRoutingDiscards = 0;        /* XXX */
        } else if (!strncmp(line, ICMP_STATS_LINE, ICMP_STATS_PREFIX_LEN)) {
            sscanf(line, ICMP_STATS_LINE,
                   &cached_icmp_mib.icmpInMsgs,
                   &cached_icmp_mib.icmpInErrors,
                   &cached_icmp_mib.icmpInDestUnreachs,
                   &cached_icmp_mib.icmpInTimeExcds,
                   &cached_icmp_mib.icmpInParmProbs,
                   &cached_icmp_mib.icmpInSrcQuenchs,
                   &cached_icmp_mib.icmpInRedirects,
                   &cached_icmp_mib.icmpInEchos,
                   &cached_icmp_mib.icmpInEchoReps,
                   &cached_icmp_mib.icmpInTimestamps,
                   &cached_icmp_mib.icmpInTimestampReps,
                   &cached_icmp_mib.icmpInAddrMasks,
                   &cached_icmp_mib.icmpInAddrMaskReps,
                   &cached_icmp_mib.icmpOutMsgs,
                   &cached_icmp_mib.icmpOutErrors,
                   &cached_icmp_mib.icmpOutDestUnreachs,
                   &cached_icmp_mib.icmpOutTimeExcds,
                   &cached_icmp_mib.icmpOutParmProbs,
                   &cached_icmp_mib.icmpOutSrcQuenchs,
                   &cached_icmp_mib.icmpOutRedirects,
                   &cached_icmp_mib.icmpOutEchos,
                   &cached_icmp_mib.icmpOutEchoReps,
                   &cached_icmp_mib.icmpOutTimestamps,
                   &cached_icmp_mib.icmpOutTimestampReps,
                   &cached_icmp_mib.icmpOutAddrMasks,
                   &cached_icmp_mib.icmpOutAddrMaskReps);
        } else if (!strncmp(line, ICMP_MSG_STATS_LINE, ICMP_MSG_STATS_PREFIX_LEN)) {
            /*
             * Note: We have to do this differently from other stats as the
             * counters to this stats are dynamic. So we will not know the
             * number of counters at a given time.
             */
            fgets(data, sizeof(data), in);
            if(decode_icmp_msg(line + ICMP_MSG_STATS_PREFIX_LEN,
                        data + ICMP_MSG_STATS_PREFIX_LEN,
                        &cached_icmp4_msg_mib) < 0) {
                continue;
            }
            ret = 1;
        } else if (!strncmp(line, TCP_STATS_LINE, TCP_STATS_PREFIX_LEN)) {
            int             ret = sscanf(line, TCP_STATS_LINE,
                                         &cached_tcp_mib.tcpRtoAlgorithm,
                                         &cached_tcp_mib.tcpRtoMin,
                                         &cached_tcp_mib.tcpRtoMax,
                                         &cached_tcp_mib.tcpMaxConn,
                                         &cached_tcp_mib.tcpActiveOpens,
                                         &cached_tcp_mib.tcpPassiveOpens,
                                         &cached_tcp_mib.tcpAttemptFails,
                                         &cached_tcp_mib.tcpEstabResets,
                                         &cached_tcp_mib.tcpCurrEstab,
                                         &cached_tcp_mib.tcpInSegs,
                                         &cached_tcp_mib.tcpOutSegs,
                                         &cached_tcp_mib.tcpRetransSegs,
                                         &cached_tcp_mib.tcpInErrs,
                                         &cached_tcp_mib.tcpOutRsts);
            cached_tcp_mib.tcpInErrsValid = (ret > 12) ? 1 : 0;
            cached_tcp_mib.tcpOutRstsValid = (ret > 13) ? 1 : 0;
        } else if (!strncmp(line, UDP_STATS_LINE, UDP_STATS_PREFIX_LEN)) {
            sscanf(line, UDP_STATS_LINE,
                   &cached_udp_mib.udpInDatagrams,
                   &cached_udp_mib.udpNoPorts,
                   &cached_udp_mib.udpInErrors,
                   &cached_udp_mib.udpOutDatagrams);
        }
    }
    fclose(in);

    /*
     * Tweak illegal values:
     *
     * valid values for ipForwarding are 1 == yup, 2 == nope
     * a 0 is forbidden, so patch:
     */
    if (!cached_ip_mib.ipForwarding)
        cached_ip_mib.ipForwarding = 2;

    /*
     * 0 is illegal for tcpRtoAlgorithm
     * so assume `other' algorithm:
     */
    if (!cached_tcp_mib.tcpRtoAlgorithm)
        cached_tcp_mib.tcpRtoAlgorithm = 1;

    return ret;
}

int
linux_read_ip_stat(struct ip_mib *ipstat)
{
    memset((char *) ipstat, (0), sizeof(*ipstat));
    if (linux_read_mibII_stats() == -1)
        return -1;
    memcpy((char *) ipstat, (char *) &cached_ip_mib, sizeof(*ipstat));
    return 0;
}

#ifndef NETSNMP_FEATURE_REMOVE_LINUX_READ_IP6_STAT
int linux_read_ip6_stat( struct ip6_mib *ip6stat)
{
#ifdef NETSNMP_ENABLE_IPV6
    FILE           *in;
    char            line[1024];
    unsigned long   stats;
    char           *endp;
    int             match;
#endif

    memset((char *) ip6stat, (0), sizeof(*ip6stat));

#ifdef NETSNMP_ENABLE_IPV6
    DEBUGMSGTL(("mibII/kernel_linux/ip6stats",
                "Reading /proc/net/snmp6 stats\n"));
    if (NULL == (in = fopen("/proc/net/snmp6", "r"))) {
        DEBUGMSGTL(("mibII/kernel_linux/ip6stats",
                    "Failed to open /proc/net/snmp6\n"));
        return -1;
    }

    while (NULL != fgets(line, sizeof(line), in)) {
        if (0 != strncmp(line, IP6_STATS_LINE, IP6_STATS_PREFIX_LEN))
            continue;

        if (1 != sscanf(line, "%*s %lu", &stats))
            continue;

        endp = strchr(line, ' ');
        *endp = '\0';
        DEBUGMSGTL(("mibII/kernel_linux/ip6stats", "Find tag: %s\n", line));

        match = 1;
        if (0 == strncmp(line + 3, "In", 2)) {  /* In */
            if (0 == strcmp(line + 5, "AddrErrors")) {
                cached_ip6_mib.ip6InAddrErrors = stats;
            } else if (0 == strcmp(line + 5, "Delivers")) {
                cached_ip6_mib.ip6InDelivers = stats;
            } else if (0 == strcmp(line + 5, "Discards")) {
                cached_ip6_mib.ip6InDiscards = stats;
            } else if (0 == strcmp(line + 5, "HdrErrors")) {
                cached_ip6_mib.ip6InHdrErrors = stats;
            } else if (0 == strcmp(line + 5, "McastPkts")) {
                cached_ip6_mib.ip6InMcastPkts = stats;
            } else if (0 == strcmp(line + 5, "NoRoutes")) {
                cached_ip6_mib.ip6InNoRoutes = stats;
            } else if (0 == strcmp(line + 5, "Receives")) {
                cached_ip6_mib.ip6InReceives = stats;
            } else if (0 == strcmp(line + 5, "TruncatedPkts")) {
                cached_ip6_mib.ip6InTruncatedPkts = stats;
            } else if (0 == strcmp(line + 5, "TooBigErrors")) {
                cached_ip6_mib.ip6InTooBigErrors = stats;
            } else if (0 == strcmp(line + 5, "UnknownProtos")) {
                cached_ip6_mib.ip6InUnknownProtos = stats;
            } else {
                match = 0;
            }
        } else if (0 == strncmp(line + 3, "Out", 3)) {  /* Out */
            if (0 == strcmp(line + 6, "Discards")) {
                cached_ip6_mib.ip6OutDiscards = stats;
            } else if (0 == strcmp(line + 6, "ForwDatagrams")) {
                cached_ip6_mib.ip6OutForwDatagrams = stats;
            } else if (0 == strcmp(line + 6, "McastPkts")) {
                cached_ip6_mib.ip6OutMcastPkts = stats;
            } else if (0 == strcmp(line + 6, "NoRoutes")) {
                cached_ip6_mib.ip6OutNoRoutes = stats;
            } else if (0 == strcmp(line + 6, "Requests")) {
                cached_ip6_mib.ip6OutRequests = stats;
            } else {
                match = 0;
            }
        } else if (0 == strncmp(line + 3, "Reasm", 5)) {  /* Reasm */
            if (0 == strcmp(line + 8, "Fails")) {
                cached_ip6_mib.ip6ReasmFails = stats;
            } else if (0 == strcmp(line + 8, "OKs")) {
                cached_ip6_mib.ip6ReasmOKs = stats;
            } else if (0 == strcmp(line + 8, "Reqds")) {
                cached_ip6_mib.ip6ReasmReqds = stats;
            } else if (0 == strcmp(line + 8, "Timeout")) {
                cached_ip6_mib.ip6ReasmTimeout = stats;
            } else {
                match = 0;
            }
        } else if (0 == strncmp(line + 3, "Frag", 4)) {  /* Frag */
            if (0 == strcmp(line + 7, "Creates")) {
                cached_ip6_mib.ip6FragCreates = stats;
            } else if (0 == strcmp(line + 7, "Fails")) {
                cached_ip6_mib.ip6FragFails = stats;
            } else if (0 == strcmp(line + 7, "OKs")) {
                cached_ip6_mib.ip6FragOKs = stats;
            } else {
                match = 0;
            }
        } else {
            match = 0;
        }

        if(!match)
            DEBUGMSGTL(("mibII/kernel_linux/ip6stats",
                        "%s is an unknown tag\n", line));
    }

    fclose(in);
#endif

    memcpy((char *) ip6stat, (char *) &cached_ip6_mib, sizeof(*ip6stat));
    return 0;
}
#endif /* NETSNMP_FEATURE_REMOVE_LINUX_READ_IP6_STAT */

int
linux_read_icmp_msg_stat(struct icmp_mib *icmpstat,
                         struct icmp4_msg_mib *icmpmsgstat,
                         int *flag)
{
    int ret;

    memset(icmpstat, 0, sizeof(*icmpstat));
    memset(icmpmsgstat, 0, sizeof(*icmpmsgstat));

    if ((ret = linux_read_mibII_stats()) == -1) {
        return -1;
    } else if (ret) {
       memcpy(icmpmsgstat, &cached_icmp4_msg_mib, sizeof(*icmpmsgstat));
       *flag = 1; /* We have a valid icmpmsg */
    }

    memcpy(icmpstat, &cached_icmp_mib, sizeof(*icmpstat));
    return 0;
}

int
linux_read_icmp_stat(struct icmp_mib *icmpstat)
{
    memset((char *) icmpstat, (0), sizeof(*icmpstat));
    if (linux_read_mibII_stats() == -1)
        return -1;
    memcpy((char *) icmpstat, (char *) &cached_icmp_mib,
           sizeof(*icmpstat));
    return 0;
}

int
linux_read_icmp6_parse(struct icmp6_mib *icmp6stat,
                       struct icmp6_msg_mib *icmp6msgstat,
                       int *support)
{
#ifdef NETSNMP_ENABLE_IPV6
    FILE           *in;
    char            line[1024];
    char            name[255];
    unsigned long   stats;
    char           *endp, *vals;
    int             match;
#endif

    memset(icmp6stat, 0, sizeof(*icmp6stat));
    if (NULL != icmp6msgstat)
        memset(icmp6msgstat, 0, sizeof(*icmp6msgstat));

#ifdef NETSNMP_ENABLE_IPV6
    DEBUGMSGTL(("mibII/kernel_linux/icmp6stats",
                "Reading /proc/net/snmp6 stats\n"));
    if (NULL == (in = fopen("/proc/net/snmp6", "r"))) {
        DEBUGMSGTL(("mibII/kernel_linux/icmp6stats",
                    "Failed to open /proc/net/snmp6\n"));
        return -1;
    }

    while (NULL != fgets(line, sizeof(line), in)) {
        if (0 != strncmp(line, ICMP6_STATS_LINE, ICMP6_STATS_PREFIX_LEN))
            continue;

        if (2 != sscanf(line, "%s %lu", name, &stats))
            continue;

        endp = strchr(line, ' ');
        *endp = '\0';
        DEBUGMSGTL(("mibII/kernel_linux/icmp6stats", "Find tag: %s\n", line));

        vals = name;
        if (NULL != icmp6msgstat) {
            int type;
            if (0 == strncmp(name, "Icmp6OutType", 12)) {
                strsep(&vals, "e");
                type = atoi(vals);
                if ( type < 0 || type > 255 )
                    continue;
                icmp6msgstat->vals[type].OutType = stats;
                *support = 1;
                continue;
            } else if (0 == strncmp(name, "Icmp6InType", 11)) {
                strsep(&vals, "e");
                type = atoi(vals);
                if ( type < 0 || type > 255 )
                    continue;
                icmp6msgstat->vals[type].InType = stats;
                *support = 1;
                continue;
            }
        }

        match = 1;
        if (0 == strncmp(line + 5, "In", 2)) {  /* In */
            if (0 == strcmp(line + 7, "DestUnreachs")) {
                cached_icmp6_mib.icmp6InDestUnreachs = stats;
            } else if (0 == strcmp(line + 7, "Echos")) {
                cached_icmp6_mib.icmp6InEchos = stats;
            } else if (0 == strcmp(line + 7, "EchoReplies")) {
                cached_icmp6_mib.icmp6InEchoReplies = stats;
            } else if (0 == strcmp(line + 7, "Errors")) {
                cached_icmp6_mib.icmp6InErrors = stats;
            } else if (0 == strcmp(line + 7, "GroupMembQueries")) {
                cached_icmp6_mib.icmp6InGroupMembQueries = stats;
            } else if (0 == strcmp(line + 7, "GroupMembReductions")) {
                cached_icmp6_mib.icmp6InGroupMembReductions = stats;
            } else if (0 == strcmp(line + 7, "GroupMembResponses")) {
                cached_icmp6_mib.icmp6InGroupMembResponses = stats;
            } else if (0 == strcmp(line + 7, "Msgs")) {
                cached_icmp6_mib.icmp6InMsgs = stats;
            } else if (0 == strcmp(line + 7, "NeighborAdvertisements")) {
                cached_icmp6_mib.icmp6InNeighborAdvertisements = stats;
            } else if (0 == strcmp(line + 7, "NeighborSolicits")) {
                cached_icmp6_mib.icmp6InNeighborSolicits = stats;
            } else if (0 == strcmp(line + 7, "PktTooBigs")) {
                cached_icmp6_mib.icmp6InPktTooBigs = stats;
            } else if (0 == strcmp(line + 7, "ParmProblems")) {
                cached_icmp6_mib.icmp6InParmProblems = stats;
            } else if (0 == strcmp(line + 7, "Redirects")) {
                cached_icmp6_mib.icmp6InRedirects = stats;
            } else if (0 == strcmp(line + 7, "RouterAdvertisements")) {
                cached_icmp6_mib.icmp6InRouterAdvertisements = stats;
            } else if (0 == strcmp(line + 7, "RouterSolicits")) {
                cached_icmp6_mib.icmp6InRouterSolicits = stats;
            } else if (0 == strcmp(line + 7, "TimeExcds")) {
                cached_icmp6_mib.icmp6InTimeExcds = stats;
            } else {
                match = 0;
            }
        } else if (0 == strncmp(line + 5, "Out", 3)) {  /* Out */
            if (0 == strcmp(line + 8, "DestUnreachs")) {
                cached_icmp6_mib.icmp6OutDestUnreachs = stats;
            } else if (0 == strcmp(line + 8, "EchoReplies")) {
                cached_icmp6_mib.icmp6OutEchoReplies = stats;
            } else if (0 == strcmp(line + 8, "GroupMembReductions")) {
                cached_icmp6_mib.icmp6OutGroupMembReductions = stats;
            } else if (0 == strcmp(line + 8, "GroupMembResponses")) {
                cached_icmp6_mib.icmp6OutGroupMembResponses = stats;
            } else if (0 == strcmp(line + 8, "Msgs")) {
                cached_icmp6_mib.icmp6OutMsgs = stats;
            } else if (0 == strcmp(line + 8, "NeighborAdvertisements")) {
                cached_icmp6_mib.icmp6OutNeighborAdvertisements = stats;
            } else if (0 == strcmp(line + 8, "NeighborSolicits")) {
                cached_icmp6_mib.icmp6OutNeighborSolicits = stats;
            } else if (0 == strcmp(line + 8, "PktTooBigs")) {
                cached_icmp6_mib.icmp6OutPktTooBigs = stats;
            } else if (0 == strcmp(line + 8, "ParmProblems")) {
                cached_icmp6_mib.icmp6OutParmProblems = stats;
            } else if (0 == strcmp(line + 8, "Redirects")) {
                cached_icmp6_mib.icmp6OutRedirects = stats;
            } else if (0 == strcmp(line + 8, "RouterSolicits")) {
                cached_icmp6_mib.icmp6OutRouterSolicits = stats;
            } else if (0 == strcmp(line + 8, "TimeExcds")) {
                cached_icmp6_mib.icmp6OutTimeExcds = stats;
            } else {
                match = 0;
            }
        } else {
            match = 0;
        }
        if(!match)
            DEBUGMSGTL(("mibII/kernel_linux/icmp6stats",
                        "%s is an unknown tag\n", line));
    }

    fclose(in);
#endif

    memcpy((char *) icmp6stat, (char *) &cached_icmp6_mib,
           sizeof(*icmp6stat));
    return 0;
}

int
linux_read_icmp6_msg_stat(struct icmp6_mib *icmp6stat,
                          struct icmp6_msg_mib *icmp6msgstat,
                          int *support)
{
     if (linux_read_icmp6_parse(icmp6stat, icmp6msgstat, support) < 0)
         return -1;
     else
         return 0;
}

int
linux_read_icmp6_stat(struct icmp6_mib *icmp6stat)
{
   if (linux_read_icmp6_parse(icmp6stat, NULL, NULL) < 0)
       return -1;
   else
       return 0;
}

int
linux_read_tcp_stat(struct tcp_mib *tcpstat)
{
    memset((char *) tcpstat, (0), sizeof(*tcpstat));
    if (linux_read_mibII_stats() == -1)
        return -1;
    memcpy((char *) tcpstat, (char *) &cached_tcp_mib, sizeof(*tcpstat));
    return 0;
}

int
linux_read_udp_stat(struct udp_mib *udpstat)
{
    memset((char *) udpstat, (0), sizeof(*udpstat));
    if (linux_read_mibII_stats() == -1)
        return -1;

#ifdef NETSNMP_ENABLE_IPV6
    {
        struct udp6_mib udp6stat;
        memset(&udp6stat, 0, sizeof(udp6stat));

        if (linux_read_udp6_stat(&udp6stat) == 0) {
            cached_udp_mib.udpOutDatagrams += udp6stat.udp6OutDatagrams;
            cached_udp_mib.udpNoPorts      += udp6stat.udp6NoPorts;
            cached_udp_mib.udpInDatagrams  += udp6stat.udp6InDatagrams;
            cached_udp_mib.udpInErrors     += udp6stat.udp6InErrors;
        }
    }
#endif
    memcpy((char *) udpstat, (char *) &cached_udp_mib, sizeof(*udpstat));
    return 0;
}

int
linux_read_udp6_stat(struct udp6_mib *udp6stat)
{
#ifdef NETSNMP_ENABLE_IPV6
    FILE           *in;
    char            line[1024];
    unsigned long   stats;
    char           *endp;
#endif

    memset(udp6stat, 0, sizeof(*udp6stat));

#ifdef NETSNMP_ENABLE_IPV6
    DEBUGMSGTL(("mibII/kernel_linux/udp6stats",
                "Reading /proc/net/snmp6 stats\n"));
    if (NULL == (in = fopen("/proc/net/snmp6", "r"))) {
        DEBUGMSGTL(("mibII/kernel_linux/udp6stats",
                    "Failed to open /proc/net/snmp6\n"));
       return -1;
    }

    while (NULL != fgets(line, sizeof(line), in)) {
        if (0 != strncmp(line, UDP6_STATS_LINE, UDP6_STATS_PREFIX_LEN))
            continue;

        if (1 != sscanf(line, "%*s %lu", &stats))
            continue;

        endp = strchr(line, ' ');
        *endp = '\0';
        DEBUGMSGTL(("mibII/kernel_linux/udp6stats", "Find tag: %s\n", line));

        if (0 == strcmp(line + 4, "OutDatagrams")) {
            cached_udp6_mib.udp6OutDatagrams = stats;
        } else if (0 == strcmp(line + 4, "NoPorts")) {
            cached_udp6_mib.udp6NoPorts = stats;
        } else if (0 == strcmp(line + 4, "InDatagrams")) {
            cached_udp6_mib.udp6InDatagrams = stats;
        } else if (0 == strcmp(line + 4, "InErrors")) {
            cached_udp6_mib.udp6InErrors = stats;
        } else {
            DEBUGMSGTL(("mibII/kernel_linux/udp6stats",
                        "%s is an unknown tag\n", line));
        }
    }

    fclose(in);
#endif

    memcpy((char *) udp6stat, (char *) &cached_udp6_mib, sizeof(*udp6stat));
    return 0;
}