summaryrefslogtreecommitdiff
path: root/scripts/vyatta_quagga_utils.pl
blob: a4c68e680fc69a26501451cf942a98b7a8932fec (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
#!/usr/bin/perl
use strict;
use lib "/opt/vyatta/share/perl5/";
use Vyatta::Config;
use Vyatta::Misc;
use NetAddr::IP;
use Getopt::Long;

my ( $prefix, $exists, $not_exists, $area, $area6, $community, $passive );

# Allowed well-know community values (see set commuinity)
my %communities = (
    'additive'   => 1,
    'internet'   => 1,
    'local-AS'   => 1,
    'no-advertise' => 1,
    'no-export'  => 1,
    'none'       => 1,
);

GetOptions(
    "check-prefix-boundry=s" => \$prefix,
    "not-exists=s"           => \$not_exists,
    "exists=s"               => \$exists,
    "check-ospf-area=s"      => \$area,
    "check-ospfv3-area=s"    => \$area6,
    "check-community"        => \$community,
    "check-ospf-passive=s"   => \$passive,
);

check_community(@ARGV)	      if ($community);
check_prefix_boundry($prefix) if ($prefix);
check_not_exists($not_exists) if ($not_exists);
check_exists($exists)         if ($exists);
check_ospfv3_area($area6)     if ($area6);
check_ospf_area($area)        if ($area);
check_ospf_passive($passive)  if ($passive);

exit 0;

sub check_prefix_boundry {
    my $prefix = shift;
    my ( $net, $network, $cidr );

    $net     = new NetAddr::IP $prefix;
    $network = $net->network()->cidr();
    $cidr    = $net->cidr();

    die "Your prefix must fall on a natural network boundry.  ",
      "Did you mean $network?\n"
      if ( $cidr ne $network );

    exit 0;
}

sub check_exists {
    my $node   = shift;
    my $config = new Vyatta::Config;

    exit 0 if $config->exists($node);
    exit 1;
}

sub check_not_exists {
    my $node   = shift;
    my $config = new Vyatta::Config;

    exit 0 if !$config->exists($node);
    exit 1;
}

sub check_ospf_area {
    my $area = shift;

    #
    # allow both decimal or dotted decimal
    #
    if ( $area =~ m/^\d+$/ ) {
        if ( $area >= 0 && $area <= 4294967295 ) {
            exit 0;
        }
    }
    if ( $area =~ m/^(\d+)\.(\d+)\.(\d+)\.(\d+)$/ ) {
        foreach my $octet ( $1, $2, $3, $4 ) {
            if ( ( $octet < 0 ) || ( $octet > 255 ) ) { exit 1; }
        }
        exit 0;
    }

    die "Invalid OSPF area: $area\n";
}

sub check_ospfv3_area {
    my $area = shift;

    # allow both decimal or dotted decimal,
    # In FRR post PR#6700 it's no longer an issue
    #
    if ( $area =~ m/^\d+$/ ) {
        if ( $area >= 0 && $area <= 4294967295 ) {
            exit 0;
        }
    }

    if ( $area =~ m/^(\d+)\.(\d+)\.(\d+)\.(\d+)$/ ) {
        foreach my $octet ( $1, $2, $3, $4 ) {
            if ( ( $octet < 0 ) || ( $octet > 255 ) ) { exit 1; }
        }
        exit 0;
    }

    die "Invalid OSPF area: $area\n";
}

sub check_community {
    foreach my $arg (@_) {
	next if ($arg =~ /\d+:\d+/);
	next if $communities{$arg};

	die "$arg unknown community value\n"
    }
}

sub check_ospf_passive {
    my $passive = shift;

    my $config = new Vyatta::Config;
    $config->setLevel('protocols ospf passive-interface');
    my @nodesO = $config->returnOrigValues();
    my @nodes  = $config->returnValues();
    
    my %nO_hash = map { $_ => 1 } @nodesO;
    my %n_hash  = map { $_ => 1 } @nodes;

    if ($nO_hash{'default'}) {
        exit 0 if ! $n_hash{'default'};
        print "Error: can't add interface when using 'default'\n";
        exit 1;
    } else {
        if (scalar(@nodes) > 1 and $n_hash{'default'}) {
            print "Error: delete other interfaces before using 'default'\n";
            exit 1;
        }
    }

    exit 0;
}