summaryrefslogtreecommitdiff
path: root/agent/mibgroup/tunnel/tunnel.c
blob: f9649ef9b3bd9981e490221634531c3d273dcfc7 (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
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
/*
 * tunnel.c --
 * 
 *      An implementation of the TUNNEL-MIB for the UCD-SNMP 4.2
 *      agent running on Linux 2.2.x.
 *      
 * Copyright (c) 2000 Frank Strauss <strauss@ibr.cs.tu-bs.de>
 *
 *                          All Rights Reserved
 * 
 * Permission to use, copy, modify and distribute this software and its
 * documentation for any purpose and without fee is hereby granted,
 * provided that the above copyright notice appears in all copies and
 * that both that copyright notice and this permission notice appear in
 * supporting documentation, and that the name of the author and CMU and
 * The Regents of the University of California not be used in advertising
 * or publicity pertaining to distribution of the software without
 * specific written permission.
 * 
 * THE AUTHOR AND CMU AND THE REGENTS OF THE UNIVERSITY OF CALIFORNIA
 * DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL
 * THE AUTHOR OR CMU OR THE REGENTS OF THE UNIVERSITY OF CALIFORNIA BE
 * LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY
 * DAMAGES WHATSOEVER RESULTING FROM THE LOSS OF USE, DATA OR PROFITS,
 * WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
 * ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
 * SOFTWARE.
 *
 */

/*
 * NOTE: This TUNNEL-MIB implementation
 *
 *       (a) DOES NOT implement write access on the tunnelConfigTable,
 *           i.e. no new tunnels can be created and no existing tunnels
 *           can be removed through SET operations.
 *
 *       (b) DOES implement write access on some tunnelIfTable objects
 *           to allow reconfiguring established tunnels. This violates
 *           RFC 2667! However, the author thinks it makes sense. ;-)
 */

#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <sys/ioctl.h>
#include <signal.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#include <linux/if.h>
#include <linux/ip.h>
#include <linux/sockios.h>
#include <linux/if_tunnel.h>
#include <linux/if_arp.h>

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

#include "tunnel.h"

#ifndef MIN
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
#endif
#ifndef MAX
#define MAX(a, b) (((a) > (b)) ? (a) : (b))
#endif



#ifdef USING_IF_MIB_IFTABLE_IFTABLE_MODULE
#include "if-mib/ifTable/ifTable.h"
#include "if-mib/ifTable/ifTable_defs.h"
#else
/*
 * This is used, because the TUNNEL-MIB augments ifTable. 
 */
extern unsigned char *var_ifEntry(struct variable *,
                                  oid *, size_t *,
                                  int, size_t *, WriteMethod **);
#endif


/*
 * tunnel_variables_oid:
 *   this is the top level oid that we want to register under.  This
 *   is essentially a prefix, with the suffix appearing in the
 *   variable below.
 */
oid             tunnel_variables_oid[] =
    { 1, 3, 6, 1, 2, 1, 10, 131, 1, 1 };
const int       tunnel_len = 10;

oid             tunnel_ifEntry_oid[] =
    { 1, 3, 6, 1, 2, 1, 10, 131, 1, 1, 1, 1 };
const int       tunnel_ifEntry_len = 12;

oid             tunnel_configEntry_oid[] =
    { 1, 3, 6, 1, 2, 1, 10, 131, 1, 1, 2, 1 };
const int       tunnel_configEntry_len = 12;



struct tunnel {
    oid             ifindex;
    int             id;
    char           *ifname;
    int             active;
    unsigned long   local;
    unsigned long   remote;
    int             encaps;
    int             hoplimit;
    int             security;
    int             tos;
    oid             config_name[MAX_OID_LEN];
    size_t          config_length;
    struct tunnel  *next;
};



/*
 * variable4 tunnel_variables:
 *   this variable defines function callbacks and type return information 
 *   for the tunnel mib section 
 */

struct variable4 tunnel_variables[] = {
    /*
     * magic number        , variable type , ro/rw , callback fn  , L, oidsuffix 
     */
#define   LOCALADDRESS          1
    {LOCALADDRESS, ASN_IPADDRESS, NETSNMP_OLDAPI_RWRITE,
     var_tunnelIfEntry, 3, {1, 1, 1}},
#define   REMOTEADDRESS         2
    {REMOTEADDRESS, ASN_IPADDRESS, NETSNMP_OLDAPI_RWRITE,
     var_tunnelIfEntry, 3, {1, 1, 2}},
#define   ENCAPSMETHOD          3
    {ENCAPSMETHOD, ASN_INTEGER, NETSNMP_OLDAPI_RONLY,
     var_tunnelIfEntry, 3, {1, 1, 3}},
#define   HOPLIMIT              4
    {HOPLIMIT, ASN_INTEGER, NETSNMP_OLDAPI_RWRITE,
     var_tunnelIfEntry, 3, {1, 1, 4}},
#define   SECURITY              5
    {SECURITY, ASN_INTEGER, NETSNMP_OLDAPI_RONLY,
     var_tunnelIfEntry, 3, {1, 1, 5}},
#define   TOS                   6
    {TOS, ASN_INTEGER, NETSNMP_OLDAPI_RWRITE,
     var_tunnelIfEntry, 3, {1, 1, 6}},

#define   IFINDEX               7
    {IFINDEX, ASN_INTEGER, NETSNMP_OLDAPI_RONLY,
     var_tunnelConfigEntry, 3, {2, 1, 5}},
#define   ROWSTATUS             8
    {ROWSTATUS, ASN_INTEGER, NETSNMP_OLDAPI_RWRITE,
     var_tunnelConfigEntry, 3, {2, 1, 6}},
};



static oid      sysORTable_reg[] = { 1, 3, 6, 1, 2, 1, 10, 131 };

static struct tunnel *tunnels;



void
deinit_tunnel(void)
{
    UNREGISTER_SYSOR_ENTRY(sysORTable_reg);
}



int
term_tunnel(int majorID, int minorID, void *serverarg, void *clientarg)
{
    deinit_tunnel();
    return 0;
}



void
init_tunnel(void)
{
    REGISTER_SYSOR_ENTRY(sysORTable_reg,
                        "RFC 2667 TUNNEL-MIB implementation for "
                        "Linux 2.2.x kernels.");

    /*
     * register ourselves with the agent to handle our mib tree 
     */
    REGISTER_MIB("tunnel", tunnel_variables, variable4,
                 tunnel_variables_oid);

    snmp_register_callback(SNMP_CALLBACK_LIBRARY,
                           SNMP_CALLBACK_SHUTDOWN, term_tunnel, NULL);

    tunnels = NULL;
}



static int
getType(int index)
{
#ifndef USING_IF_MIB_IFTABLE_IFTABLE_MODULE
    oid             name[MAX_OID_LEN] = { 1, 3, 6, 1, 2, 1, 2, 2, 1, 3 };
    size_t          length = 10;
    struct variable ifType_variable =
        { 3, ASN_INTEGER, NETSNMP_OLDAPI_RONLY,
          var_ifEntry, 10, {1, 3, 6, 1, 2, 1, 2, 2, 1, 3}
    };
    unsigned char  *p;
    size_t          var_len;
    WriteMethod    *write_method;

    name[length] = index;
    length++;

    p = var_ifEntry(&ifType_variable,
                    name, &length,
                    1 /* exact */ , &var_len, &write_method);
    if (!p)
        return 0;

    return *(int *) p;
#else
    ifTable_mib_index imi;
    ifTable_rowreq_ctx *rr;

    imi.ifIndex = index;
    rr = ifTable_row_find_by_mib_index(&imi);
    if (NULL == rr)
        return 0;

    return rr->data.ifType;
#endif
}



static const char *
getName(int index)
{
#ifndef USING_IF_MIB_IFTABLE_IFTABLE_MODULE
    oid             name[MAX_OID_LEN] = { 1, 3, 6, 1, 2, 1, 2, 2, 1, 2 };
    size_t          length = 10;
    struct variable ifName_variable =
        { 2, ASN_INTEGER, NETSNMP_OLDAPI_RONLY,
          var_ifEntry, 10, {1, 3, 6, 1, 2, 1, 2, 2, 1, 2}
    };
    unsigned char  *p;
    size_t          var_len;
    WriteMethod    *write_method;

    name[length] = index;
    length++;

    p = var_ifEntry(&ifName_variable,
                    name, &length,
                    1 /* exact */ , &var_len, &write_method);
    if (!p)
        return NULL;

    return p;
#else
    return netsnmp_access_interface_name_find(index);
#endif
}



static struct ip_tunnel_parm *
getTunnelParm(char *ifname)
{
    struct ifreq    ifrq;
    int             fd;
    static struct ip_tunnel_parm parm;

    if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
        return NULL;
    }

    memset(&parm, 0, sizeof(struct ip_tunnel_parm));
    strcpy(ifrq.ifr_name, ifname);
    ifrq.ifr_ifru.ifru_data = (void *) &parm;
    if (ioctl(fd, SIOCGETTUNNEL, &ifrq) < 0) {
        /*
         * try again with the last char of the device name cut off.
         * it might have been a zero digit appended by the agent.
         */
        ifrq.ifr_name[strlen(ifrq.ifr_name) - 1] = 0;
        if (ioctl(fd, SIOCGETTUNNEL, &ifrq) < 0) {
            close(fd);
            return NULL;
        }
        ifname[strlen(ifname) - 1] = 0;
    }

    close(fd);

    return &parm;
}



int
setTunnelParm(char *ifname, struct ip_tunnel_parm *parm)
{
    struct ifreq    ifrq;
    int             fd;
    int             err;

    if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
        return -1;
    }

    strcpy(ifrq.ifr_name, ifname);
    ifrq.ifr_ifru.ifru_data = (void *) parm;
    err = ioctl(fd, SIOCCHGTUNNEL, &ifrq);
    close(fd);

    return err;
}



/*
 * update a struct tunnel. its index and ifname elements have to be set.
 */
static struct tunnel *
updateTunnel(struct tunnel *tunnel)
{
    struct ip_tunnel_parm *parm;
    int             fd;
    struct ifreq    ifrq;

    /*
     * NOTE: getTunnelParm() may adjust the passed ifname. 
     */
    parm = getTunnelParm(tunnel->ifname);
    if (!parm) {
	DEBUGMSGTL(("tunnel",
		    "updateTunnel(): getTunnelParm(\"%s\") returned NULL\n",
		    tunnel->ifname));
        tunnel->active = 0;
        return NULL;
    }

    tunnel->active = 1;

    tunnel->local = parm->iph.saddr;
    tunnel->remote = parm->iph.daddr;

    if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
        DEBUGMSGTL(("snmpd", "socket open failure in updateTunnels()\n"));
        return NULL;
    } else {
        /*
         * NOTE: this ioctl does not guarantee 6 bytes of a physaddr.
         * In particular, a 'sit0' interface only appears to get back
         * 4 bytes of sa_data. We don't use sa_data here, or we'd
         * need to memset it to 0 before the ioct.
         */
        strcpy(ifrq.ifr_name, tunnel->ifname);
        if (ioctl(fd, SIOCGIFHWADDR, &ifrq) == 0)
            switch (ifrq.ifr_hwaddr.sa_family) {
            case ARPHRD_TUNNEL:
                tunnel->encaps = 2;
                break;;         /* direct */
            case ARPHRD_TUNNEL6:
                tunnel->encaps = 2;
                break;;         /* direct */
            case ARPHRD_IPGRE:
                tunnel->encaps = 3;
                break;;         /* gre */
            case ARPHRD_SIT:
                tunnel->encaps = 2;
                break;;         /* direct */
            default:
                tunnel->encaps = 1;     /* other */
            }
        close(fd);
    }

    tunnel->hoplimit = parm->iph.ttl;
    tunnel->security = 1;
    tunnel->tos = (parm->iph.tos & 1) ? -1 : parm->iph.tos;
    /*
     * XXX: adjust tos mapping (kernel <-> TUNNEL-MIB::tunnelIfTOS) 
     */

    return tunnel;
}



static void
updateTunnels(void)
{
    static int      max_index = 1;
    static struct tunnel *last_tunnel = NULL;
    struct tunnel  *tunnel;
    const char     *ifname;
    int             type;

    /*
     * uptime the tunnels we have so far 
     */
    for (tunnel = tunnels; tunnel; tunnel = tunnel->next) {
        DEBUGMSG(("tunnel",
                  "updateTunnels(): updating %s (index=%" NETSNMP_PRIo "u)\n",
                  tunnel->ifname, tunnel->ifindex));
        updateTunnel(tunnel);
    }

    /*
     * look for new tunnels 
     */
    for (; max_index < 256; max_index++) {
        DEBUGMSG(("tunnel",
                  "updateTunnels(): looking for new index=%d\n",
                  max_index));
        type = getType(max_index);
        if (type == 131) {
            tunnel = (struct tunnel *) malloc(sizeof(struct tunnel));
            if (!tunnel)
                continue;

            tunnel->ifindex = max_index;
            tunnel->id = 1;

            ifname = getName(max_index);
            if (!ifname) {
                free(tunnel);
                continue;
            }

            tunnel->ifname = strdup(ifname);
            if (!tunnel->ifname) {
                free(tunnel);
                continue;
            }

            if (!updateTunnel(tunnel)) {
                free(tunnel);
                continue;
            }

            if (last_tunnel)
                last_tunnel->next = tunnel;
            if (!tunnels)
                tunnels = last_tunnel = tunnel;
            tunnel->next = NULL;
            last_tunnel = tunnel;

            DEBUGMSG(("tunnel",
                      "updateTunnels(): added %s (index=%" NETSNMP_PRIo
                      "u state=%d)\n",
                      tunnel->ifname, tunnel->ifindex, tunnel->active));
        }
        if (type == 0)
            break;
    }
}



static struct tunnel *
getTunnelByIfIndex(int index)
{
    struct tunnel  *tunnel;

    DEBUGMSG(("tunnel", "getTunnelByIfIndex(%d): ", index));

    for (tunnel = tunnels; tunnel; tunnel = tunnel->next) {
        if (tunnel->ifindex == index) {
            if (!tunnel->active)
                break;
            DEBUGMSG(("tunnel", "%s (index=%" NETSNMP_PRIo "u)\n",
                     tunnel->ifname, tunnel->ifindex));
            return tunnel;
        }
    }
    DEBUGMSG(("tunnel", "NONE\n"));
    return NULL;
}



static struct tunnel *
getNextTunnelByIfIndex(int index)
{
    struct tunnel  *tunnel;

    DEBUGMSG(("tunnel", "getNextTunnelByIfIndex(%d): ", index));

    for (tunnel = tunnels; tunnel; tunnel = tunnel->next) {
        if (tunnel->ifindex > index) {
            if (!tunnel->active)
                continue;
            DEBUGMSG(("tunnel", "%s (index=%" NETSNMP_PRIo "u)\n",
                      tunnel->ifname, tunnel->ifindex));
            return tunnel;
        }
    }
    DEBUGMSG(("tunnel", "NONE\n"));
    return NULL;
}



static void
fillConfigOid(oid * name, struct tunnel *tunnel)
{
    name[0] = ((unsigned char *) &tunnel->local)[0];
    name[1] = ((unsigned char *) &tunnel->local)[1];
    name[2] = ((unsigned char *) &tunnel->local)[2];
    name[3] = ((unsigned char *) &tunnel->local)[3];
    name[4] = ((unsigned char *) &tunnel->remote)[0];
    name[5] = ((unsigned char *) &tunnel->remote)[1];
    name[6] = ((unsigned char *) &tunnel->remote)[2];
    name[7] = ((unsigned char *) &tunnel->remote)[3];
    name[8] = tunnel->encaps;
    name[9] = tunnel->id;
    DEBUGMSGOID(("tunnel", name, 10));
}



static struct tunnel *
getTunnelByConfigOid(oid * name, size_t * length)
{
    struct tunnel  *tunnel;
    oid             tname[4 + 4 + 1 + 1];

    DEBUGMSG(("tunnel", "getTunnelByConfigOid(): "));

    for (tunnel = tunnels; tunnel; tunnel = tunnel->next) {
        fillConfigOid(tname, tunnel);
        if (!snmp_oid_compare(tname, 4 + 4 + 1 + 1,
                              &name[tunnel_len + 3],
                              (*length) - tunnel_len - 3)) {
            if (!tunnel->active)
                break;
            DEBUGMSG(("tunnel", "%s (index=%" NETSNMP_PRIo "u)\n",
                      tunnel->ifname, tunnel->ifindex));
            return tunnel;
        }
    }
    DEBUGMSG(("tunnel", "NONE\n"));
    return NULL;
}



static struct tunnel *
getNextTunnelByConfigOid(oid * name, size_t * length)
{
    struct tunnel  *tunnel, *last_tunnel;
    oid             tname[10], last_tname[10];

    DEBUGMSG(("tunnel", "getNextTunnelByConfigOid("));
    DEBUGMSGOID(("tunnel", name, *length));
    DEBUGMSG(("tunnel", "): "));

    last_tunnel = NULL;
    for (tunnel = tunnels; tunnel; tunnel = tunnel->next) {
        if (!tunnel->active)
            continue;
        fillConfigOid(tname, tunnel);
        if (snmp_oid_compare(tname, 10,
                             &name[tunnel_len + 3],
                             (*length) - tunnel_len - 3) > 0) {
            if (!last_tunnel) {
                last_tunnel = tunnel;
                memcpy((char *) last_tname, (char *) tname,
                       10 * sizeof(oid));
            } else {
                if (snmp_oid_compare(tname, 10, last_tname, 10) < 0) {
                    last_tunnel = tunnel;
                    memcpy((char *) last_tname, (char *) tname,
                           10 * sizeof(oid));
                }
            }
        }
    }

    if (last_tunnel) {
        DEBUGMSG(("tunnel", "%s (index=%" NETSNMP_PRIo "u)\n",
                  last_tunnel->ifname, last_tunnel->ifindex));
    } else {
        DEBUGMSG(("tunnel", "NONE\n"));
    }

    return last_tunnel;
}



static int
writeLocalAddress(int action, unsigned char *var_val,
                  unsigned char var_val_type, size_t var_val_len,
                  unsigned char *statP, oid * name, size_t name_len)
{
    static struct tunnel *tunnel;
    struct ip_tunnel_parm *parm;

    switch (action) {
    case RESERVE1:
        if (var_val_type != ASN_IPADDRESS) {
            return SNMP_ERR_WRONGTYPE;
        }
        if (var_val_len != 4) {
            return SNMP_ERR_WRONGLENGTH;
        }
    case RESERVE2:
        tunnel = getTunnelByIfIndex((int) name[name_len - 1]);
        if (!tunnel) {
            return SNMP_ERR_NOSUCHNAME;
        }
    case FREE:
        break;
    case ACTION:
        break;
    case UNDO:
        break;
    case COMMIT:
        if (!tunnel) {
            return SNMP_ERR_NOSUCHNAME;
        }
        parm = getTunnelParm(tunnel->ifname);
        if (!parm) {
            return SNMP_ERR_NOSUCHNAME;
        }
        parm->iph.saddr = *(unsigned long *) var_val;
        setTunnelParm(tunnel->ifname, parm);
        break;
    }

    return SNMP_ERR_NOERROR;
}



static int
writeRemoteAddress(int action, unsigned char *var_val,
                   unsigned char var_val_type, size_t var_val_len,
                   unsigned char *statP, oid * name, size_t name_len)
{
    static struct tunnel *tunnel;
    struct ip_tunnel_parm *parm;

    switch (action) {
    case RESERVE1:
        if (var_val_type != ASN_IPADDRESS) {
            return SNMP_ERR_WRONGTYPE;
        }
        if (var_val_len != 4) {
            return SNMP_ERR_WRONGLENGTH;
        }
    case RESERVE2:
        tunnel = getTunnelByIfIndex((int) name[name_len - 1]);
        if (!tunnel) {
            return SNMP_ERR_NOSUCHNAME;
        }
    case FREE:
        break;
    case ACTION:
        break;
    case UNDO:
        break;
    case COMMIT:
        if (!tunnel) {
            return SNMP_ERR_NOSUCHNAME;
        }
        parm = getTunnelParm(tunnel->ifname);
        if (!parm) {
            return SNMP_ERR_NOSUCHNAME;
        }
        parm->iph.daddr = *(unsigned long *) var_val;
        setTunnelParm(tunnel->ifname, parm);
        break;
    }

    return SNMP_ERR_NOERROR;
}



static int
writeHopLimit(int action, unsigned char *var_val,
              unsigned char var_val_type, size_t var_val_len,
              unsigned char *statP, oid * name, size_t name_len)
{
    static struct tunnel *tunnel;
    struct ip_tunnel_parm *parm;

    switch (action) {
    case RESERVE1:
        if (var_val_type != ASN_INTEGER) {
            return SNMP_ERR_WRONGTYPE;
        }
        if (var_val_len > sizeof(long)) {
            return SNMP_ERR_WRONGLENGTH;
        }
    case RESERVE2:
        tunnel = getTunnelByIfIndex((int) name[name_len - 1]);
        if (!tunnel) {
            return SNMP_ERR_NOSUCHNAME;
        }
    case FREE:
        break;
    case ACTION:
        break;
    case UNDO:
        break;
    case COMMIT:
        if (!tunnel) {
            return SNMP_ERR_NOSUCHNAME;
        }
        parm = getTunnelParm(tunnel->ifname);
        if (!parm) {
            return SNMP_ERR_NOSUCHNAME;
        }
        parm->iph.ttl = *(long *) var_val;
        setTunnelParm(tunnel->ifname, parm);
        break;
    }

    return SNMP_ERR_NOERROR;
}



static int
writeTOS(int action, unsigned char *var_val,
         unsigned char var_val_type, size_t var_val_len,
         unsigned char *statP, oid * name, size_t name_len)
{
    static struct tunnel *tunnel;
    struct ip_tunnel_parm *parm;

    switch (action) {
    case RESERVE1:
        if (var_val_type != ASN_INTEGER) {
            return SNMP_ERR_WRONGTYPE;
        }
        if (var_val_len > sizeof(long)) {
            return SNMP_ERR_WRONGLENGTH;
        }
    case RESERVE2:
        tunnel = getTunnelByIfIndex((int) name[name_len - 1]);
        if (!tunnel) {
            return SNMP_ERR_NOSUCHNAME;
        }
    case FREE:
        break;
    case ACTION:
        break;
    case UNDO:
        break;
    case COMMIT:
        if (!tunnel) {
            return SNMP_ERR_NOSUCHNAME;
        }
        parm = getTunnelParm(tunnel->ifname);
        if (!parm) {
            return SNMP_ERR_NOSUCHNAME;
        }
        /*
         * this does not cover all meaningful values: 
         */
        parm->iph.tos = (*(long *) var_val == -1) ? 1 : *(long *) var_val;
        setTunnelParm(tunnel->ifname, parm);
        break;
    }

    return SNMP_ERR_NOERROR;
}



unsigned char  *
var_tunnelIfEntry(struct variable *vp,
                  oid * name, size_t * length,
                  int exact, size_t * var_len, WriteMethod ** write_method)
{
    static unsigned long ret_addr;
    static long     ret_int;
    struct tunnel  *tunnel;

    DEBUGMSGTL(("tunnel", "var_tunnelIfEntry: "));
    DEBUGMSGOID(("tunnel", name, *length));
    DEBUGMSG(("tunnel", " %d\n", exact));

    updateTunnels();

    if (exact) {
        if (*length != tunnel_len + 3 + 1) {
            return NULL;
        }
        tunnel = getTunnelByIfIndex((int) name[*length - 1]);
    } else {
        if ((*length) < tunnel_len) {
            memcpy((char *) name, (char *) tunnel_variables_oid,
                   tunnel_len * sizeof(oid));
        }
        if ((*length) < tunnel_len + 1) {
            name[tunnel_len] = 1;
        }
        if ((*length) < tunnel_len + 2) {
            name[tunnel_len + 1] = 1;
        }
        if ((*length) < tunnel_len + 3) {
            name[tunnel_len + 2] = 1;
        }
        if ((*length) < tunnel_len + 4) {
            name[tunnel_len + 3] = 0;
        }
        *length = tunnel_len + 4;

        tunnel = getNextTunnelByIfIndex(name[*length - 1]);
        if (!tunnel) {
            /*
             * end of column, continue with first row of next column 
             */
            tunnel = tunnels;
            name[tunnel_len + 2]++;
            if (name[tunnel_len + 2] > 6) {
                /*
                 * there is no next column 
                 */
                return NULL;
            }
            if (!tunnel) {
                /*
                 * there is no (next) row 
                 */
                return NULL;
            }
        }
    }

    if (!tunnel) {
        return NULL;
    }

    name[*length - 1] = tunnel->ifindex;

    DEBUGMSGTL(("tunnel", "var_tunnelIfEntry: using"));
    DEBUGMSGOID(("tunnel", name, *length));
    DEBUGMSG(("tunnel", "\n"));

    switch (name[tunnel_len + 2]) {
    case 1:                    /* tunnelIfLocalAddress */
        ret_addr = tunnel->local;
        *var_len = 4;
        vp->type = ASN_IPADDRESS;
        *write_method = writeLocalAddress;
        return (u_char *) & ret_addr;
    case 2:                    /* tunnelIfRemoteAddress */
        ret_addr = tunnel->remote;
        *var_len = 4;
        vp->type = ASN_IPADDRESS;
        *write_method = writeRemoteAddress;
        return (u_char *) & ret_addr;
    case 3:                    /* tunnelIfEncapsMethod */
        ret_int = tunnel->encaps;
        *var_len = sizeof(ret_int);
        vp->type = ASN_INTEGER;
        return (u_char *) & ret_int;
    case 4:                    /* tunnelIfHopLimit */
        ret_int = tunnel->hoplimit;
        *var_len = sizeof(ret_int);
        vp->type = ASN_INTEGER;
        *write_method = writeHopLimit;
        return (u_char *) & ret_int;
    case 5:                    /* tunnelIfSecurity */
        ret_int = tunnel->security;
        *var_len = sizeof(ret_int);
        vp->type = ASN_INTEGER;
        return (u_char *) & ret_int;
    case 6:                    /* tunnelIfTOS */
        ret_int = tunnel->tos;
        *var_len = sizeof(ret_int);
        vp->type = ASN_INTEGER;
        *write_method = writeTOS;
        return (u_char *) & ret_int;
    default:
        return 0;
    }

    return NULL;
}



unsigned char  *
var_tunnelConfigEntry(struct variable *vp,
                      oid * name, size_t * length,
                      int exact, size_t * var_len,
                      WriteMethod ** write_method)
{
    static long     ret_int;
    struct tunnel  *tunnel;
    int             i;

    DEBUGMSGTL(("tunnel", "var_tunnelConfigEntry: "));
    DEBUGMSGOID(("tunnel", name, *length));
    DEBUGMSG(("tunnel", " %d\n", exact));

    updateTunnels();

    if (exact) {
        if (*length != tunnel_len + 3 + 4 + 4 + 1 + 1) {
            return NULL;
        }
        tunnel = getTunnelByConfigOid(name, length);
    } else {
        if (snmp_oid_compare(name, *length,
                             tunnel_configEntry_oid,
                             tunnel_configEntry_len) < 0) {
            *length = 0;
        }
        if ((*length) < tunnel_len) {
            memcpy((char *) name, (char *) tunnel_variables_oid,
                   tunnel_len * sizeof(oid));
        }
        if ((*length) < tunnel_len + 1) {
            name[tunnel_len] = 2;
        }
        if ((*length) < tunnel_len + 2) {
            name[tunnel_len + 1] = 1;
        }
        if ((*length) < tunnel_len + 3) {
            name[tunnel_len + 2] = 5;
        }
        for (i = MAX(*length, tunnel_len + 3);
             i < tunnel_len + 3 + 4 + 4 + 1 + 1; i++) {
            name[i] = 0;
        }
        *length = tunnel_len + 3 + 4 + 4 + 1 + 1;
        tunnel = getNextTunnelByConfigOid(name, length);
        if (!tunnel) {
            /*
             * end of column, continue with first row of next column 
             */
            tunnel = tunnels;
            name[tunnel_len + 2]++;
            if (name[tunnel_len + 2] > 6) {
                /*
                 * there is no next column 
                 */
                return NULL;
            }
            if (!tunnel) {
                /*
                 * there is no (next) row 
                 */
                return NULL;
            }
        }
    }

    if (!tunnel) {
        return NULL;
    }

    fillConfigOid(&name[tunnel_len + 3], tunnel);

    DEBUGMSGTL(("tunnel", "var_tunnelConfigEntry: using "));
    DEBUGMSGOID(("tunnel", name, *length));
    DEBUGMSG(("tunnel", "\n"));

    switch (name[tunnel_len + 2]) {
    case 5:                    /* tunnelConfigIfIndex */
        ret_int = tunnel->ifindex;
        *var_len = sizeof(ret_int);
        vp->type = ASN_INTEGER;
        return (u_char *) & ret_int;
    case 6:                    /* tunnelConfigStatus */
        ret_int = 1;            /* active */
        *var_len = sizeof(ret_int);
        vp->type = ASN_INTEGER;
        return (u_char *) & ret_int;
    default:
        return 0;
    }

    return NULL;
}