summaryrefslogtreecommitdiffstats
path: root/ebtables-save.in
blob: 443805024cd5a891d61f95bd2d94aaf10400258f (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
#!/usr/bin/perl -w
#
#
# A script that generates text output of the ebtables rules.
# Similar to iptables-save.
#
# It can be used to store active configuration to /etc/sysconfig/ebtables

use strict;
use POSIX qw(strftime);
my $table;
my $ebtables = "@sbindir@/ebtables";
my $cnt = "";
my $version = "1.1";
my $table_name;
my $modulesfh;
my @tables;

# ========================================================
# Process filter table
# ========================================================
sub process_table {
    my $chain = "";
    my $rules = "";
    my $chains = "";
    my $line = "";

    foreach $line (split("\n",$_[0])) {
        if ($line =~ m/Bridge table: (.*)/) {
            print "*$1\n";
            next;
        }
        if ($line =~ m/Bridge chain: (.*?), entries:.* policy: (.*)/) {
            $chains = $chains . ":$1 $2\n";
            $chain = $1;
            next;
        }
        if ($line =~ m/^$/) {
            next;
        }
        if ($cnt eq "--Lc") {
            $line =~ s/, pcnt = (.*) -- bcnt = (.*)/-c $1 $2/;
        } else {
            $line =~ s/ $//;
        }
        $rules = $rules . "-A $chain $line\n";
    }

    print $chains;
    print $rules;
    print "\n";
}
# ========================================================

unless (-x $ebtables) { exit -1 };

open $modulesfh, '<', '/proc/modules' or die "Failed to open /proc/modules: $!";
while( my $line = <$modulesfh>) {
    if($line =~ /^ebtable_([^ ]+)/) {
        push @tables, $1;
    }
}
close $modulesfh;

printf("# Generated by ebtables-save v$version (legacy) on %s\n", strftime("%a %FT%TZ", gmtime));
if (defined($ENV{'EBTABLES_SAVE_COUNTER'}) && $ENV{'EBTABLES_SAVE_COUNTER'} eq "yes") {
    $cnt = "--Lc";
}

foreach $table_name (@tables) {
    $table =`$ebtables -t $table_name -L $cnt`;
    unless ($? == 0) { print $table; exit -1 };
    &process_table($table);
}