summaryrefslogtreecommitdiff
path: root/scripts/vyatta-tunnel-cleanup
blob: af73bc0461ec9f4e4d0e7ec2a609726bc6b97408 (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
#! /usr/bin/perl

# This script gets run after tunnels have been deleted, it removes
# the underlying tunnel link, if all tunnels of that type are gone.

use strict;
use warnings;
use lib "/opt/vyatta/share/perl5";
use Vyatta::Config;

# encapsulations and module names
my %encapsulation = (
    'gre'  => 'ip_gre',
    'ipip' => 'ipip',
    'sit'  => 'sit',
);

# interface being deleted
my $interface = $ARGV[0];

# Create map of encapsulations still in use
my $config = new Vyatta::Config;
$config->setLevel("interfaces tunnel");

my %tunnel;
foreach my $tun ( $config->listEffectiveNodes() ) {
    my $mode = $config->returnEffectiveValue("$tun encapsulation");
    next if ($interface eq $tun); # don't count the one being deleted
    if ($mode eq "gre-multipoint") {
       $mode = "gre";
    }
    $tunnel{$mode} = 1;
}

foreach my $type ( keys %encapsulation ) {
    # skip if tunnel is still in use
    next if $tunnel{$type};

    my $module = $encapsulation{$type};
    next unless ( -d "/sys/module/$module" );
    system("sudo /sbin/rmmod $module") == 0
	or warn "rmmod $module failed\n";
}