summaryrefslogtreecommitdiff
path: root/lib/Vyatta/VPN/vtiIntf.pm
blob: 4e52fffb61e9bc80b9121861aa2b8860c05b84c7 (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
#!/usr/bin/perl -w
#
# module to find and store exisiting vti tunnels.

# **** License ****
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# This code was originally developed by Vyatta, Inc.
# Portions created by Vyatta are Copyright (C) 2012 Vyatta, Inc.
# All Rights Reserved.
#
# Authors: Saurabh Mohan
# Date: 2012
# Description: Find and store exisiting vti tunnels
#
# **** End License ****
#
#

package vtiIntf;

use strict;

# Collect set of existing Vti's.
my %existingVtiName = ();
my %existingVtibyName = ();
my %existingVtiMark = ();
my @VtiMarks;
my $vtiMarkBase = 0x900000;
my $maxMarks = 2048;

sub discoverVtiIntfs {
    my @currentVtis = `/sbin/ip tunnel | grep "^vti"`;
    if (@currentVtis != 0) {
        my ($remote, $local, $name, $mark);
        my $key;
        foreach my $curVti (@currentVtis) {
            ($remote, $local, $name, $mark) = parseVtiTun($curVti);
            $key = "remote $remote local $local";
            $existingVtiName{$key} = $name;
            $existingVtiMark{$key} = $mark;
            $VtiMarks[$mark-$vtiMarkBase] = 1;
            $existingVtibyName{$name} = 1;
        }
    }
}

#
# Api takes as input the o/p of 'ip tunnel show' and
#  returns a list with {remote,local,name,mark}
# Example input:
# vti2: ip/ip  remote 12.0.0.2  local 12.0.0.1  ttl inherit  nopmtudisc key 15
# 
sub parseVtiTun {
    my ($tunop) = @_;
    my ($tunName, $remote, $local, $mark);
    if ($tunop =~ m/(^vti.*): .*/) {
        $tunName = $1;
    }
    if ($tunop =~ m/remote ([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)/) {
        $remote = $1;
    }
    if ($tunop =~ m/local ([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)/) {
        $local = $1;
    }
    if ($tunop =~ m/key ([0-9\.]+)/) {
        $mark = $1;
    }
    return($remote, $local, $tunName, $mark);
}

sub extractRemoteLocal {
    my ($key) = @_;
    my ($remote, $local);
    if ($key =~ m/remote ([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)/) {
        $remote = $1;
    }
    if ($key =~ m/local ([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)/) {
        $local = $1;
    }
    return($remote, $local);
}

sub isVtinamepresent {
    my ($remote, $local) = @_;
    my $key = "remote $remote local $local";

    if (exists $existingVtiName{$key} ) {
        return $existingVtiName{$key};
    }
    return ""; 
}

#
# Pass a referenct to the existing Vti names.
#
sub getVtiNames {
    return (\%existingVtiName);
}

sub deleteVtinamepresent {
    my ($remote, $local) = @_;
    my $key = "remote $remote local $local";

    if (exists $existingVtiName{$key} ) {
        delete $existingVtiName{$key};
    }
}

sub isVtimarkpresent {
    my ($remote, $local) = @_;
    my $key = "remote $remote local $local";

    if (exists $existingVtiMark{$key}) {
        return $existingVtiMark{$key};
    }
    return ""; 
}

sub allocVtiMark {
    for my $cmark (1 .. ($maxMarks-1)) {
        if (! defined($VtiMarks[$cmark])) {
            $VtiMarks[$cmark] = 1;
            return $cmark + $vtiMarkBase ;
        }
    }
    return 0;
}

sub freeVtiMark {
    my ($freeMark) = @_;
    if ($freeMark > 0 && $freeMark < $maxMarks) {
        $VtiMarks[$freeMark] = 0;
    }
    return 0;
}

sub isVtibynamepresent {
    my ($name) = @_;
    if (exists $existingVtibyName{$name}) {
        return $existingVtibyName{$name};
    }
    return 0;
}

sub deleteVtibyname {
    my ($name) = @_;
    if (exists $existingVtibyName{$name}) {
        delete $existingVtibyName{$name};
    }
}

sub getVtibyNames {
    return (\%existingVtibyName);
}

1;