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
|
#
# VyOS::Nptv6Rule: Update SNPT/DNPT ip6tables rules
#
# Copyright (C) 2014 VyOS Development Group <maintainers@vyos.net>
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library 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
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
# USA
package VyOS::Nptv6Rule;
use strict;
use lib "/opt/vyatta/share/perl5";
require Vyatta::Config;
require Vyatta::IpTables::AddressFilter;
use Vyatta::Misc;
use Vyatta::TypeChecker;
require Exporter;
our @ISA = qw(Exporter);
our @EXPORT = qw(is_disabled rule_str);
my %fields = (
_rule_number => undef,
_outside_if => undef,
_inside_pfx => undef,
_outside_pfx => undef,
_disable => undef,
);
sub new {
my $that = shift;
my $class = ref ($that) || $that;
my $self = {
%fields,
};
bless $self, $class;
return $self;
}
sub setup {
my ( $self, $level ) = @_;
my $config = new Vyatta::Config;
$config->setLevel("$level");
$self->{_rule_number} = $config->returnParent("..");
$self->{_outside_if} = $config->returnValue("outbound-interface");
$self->{_inside_pfx} = $config->returnValue("source prefix");
$self->{_outside_pfx} = $config->returnValue("translation prefix");
$self->{_disable} = $config->exists("disable");
return 0;
}
# Make SNPT ip6tables string
# POSTROUTING
# ip6tables -t mangle -I VYOS_NPT_HOOK -s inside-pfx -o outside-if -j SNPT --src-pfx inside-pfx --dst-pfx outside-pfx
sub make_snpt_string {
my ($self) = @_;
my $snpt_str = "";
# Construct ip6tables string
$snpt_str .= "-I VYOS_SNPT_HOOK ";
$snpt_str .= "-s ";
$snpt_str .= $self->{_inside_pfx};
if(defined($self->{_outside_if})) {
$snpt_str .= " -o ";
$snpt_str .= $self->{_outside_if};
}
$snpt_str .= " -j SNPT --src-pfx ";
$snpt_str .= $self->{_inside_pfx};
$snpt_str .= " --dst-pfx ";
$snpt_str .= $self->{_outside_pfx};
return $snpt_str;
}
# Make DNPT ip6tables string
# PREROUTING
# ip6tables -t mangle -I VYOS_NPT_HOOK -d outside-pfx -i outside-if -j DNPT --src-pfx outside-pfx --dst-pfx inside-pfx
sub make_dnpt_string {
my ($self) = @_;
my $dnpt_str = "";
# Construct ip6tables string
$dnpt_str .= "-I VYOS_DNPT_HOOK ";
$dnpt_str .= "-d ";
$dnpt_str .= $self->{_outside_pfx};
if(defined($self->{_outside_if})) {
$dnpt_str .= " -i ";
$dnpt_str .= $self->{_outside_if};
}
$dnpt_str .= " -j DNPT --src-pfx ";
$dnpt_str .= $self->{_outside_pfx};
$dnpt_str .= " --dst-pfx ";
$dnpt_str .= $self->{_inside_pfx};
return $dnpt_str;
}
# Tests if the rule is valid, returns false if valid, returns error string if invalid
sub is_invalid {
my ($self) = @_;
# Validate prefixes
if(!defined($self->{_inside_pfx}) || $self->{_inside_pfx} eq '') {
return "inside-prefix must be set";
}
if(!defined($self->{_outside_pfx}) || $self->{_outside_pfx} eq '') {
return "outside-prefix must be set";
}
if(!Vyatta::TypeChecker::validateType('ipv6net', $self->{_inside_pfx}, 1)) {
return "inside-prefix is not a valid prefix";
}
if(!Vyatta::TypeChecker::validateType('ipv6net', $self->{_outside_pfx}, 1)) {
return "outside-prefix is not a valid prefix";
}
return 0;
}
# Returns an array of ip6tables parameters (SNPT, DNPT)
sub rule_str {
my ($self) = @_;
my $err;
$err = $self->is_invalid();
if($err ne 0) {
return ($err, undef, undef);
}
return (undef, $self->make_snpt_string(), $self->make_dnpt_string());
}
sub is_disabled {
my $self = shift;
return 1 if defined $self->{_disable};
return 0;
}
1;
|