line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
# |
2
|
|
|
|
|
|
|
# $Id$ |
3
|
|
|
|
|
|
|
# |
4
|
|
|
|
|
|
|
# system::route Brik |
5
|
|
|
|
|
|
|
# |
6
|
|
|
|
|
|
|
package Metabrik::System::Route; |
7
|
1
|
|
|
1
|
|
826
|
use strict; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
28
|
|
8
|
1
|
|
|
1
|
|
5
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
27
|
|
9
|
|
|
|
|
|
|
|
10
|
1
|
|
|
1
|
|
4
|
use base qw(Metabrik); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
1637
|
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
sub brik_properties { |
13
|
|
|
|
|
|
|
return { |
14
|
0
|
|
|
0
|
1
|
|
revision => '$Revision$', |
15
|
|
|
|
|
|
|
tags => [ qw(unstable) ], |
16
|
|
|
|
|
|
|
author => 'GomoR ', |
17
|
|
|
|
|
|
|
license => 'http://opensource.org/licenses/BSD-3-Clause', |
18
|
|
|
|
|
|
|
attributes => { |
19
|
|
|
|
|
|
|
device => [ qw(device) ], |
20
|
|
|
|
|
|
|
}, |
21
|
|
|
|
|
|
|
commands => { |
22
|
|
|
|
|
|
|
list => [ ], |
23
|
|
|
|
|
|
|
show => [ ], |
24
|
|
|
|
|
|
|
is_router_ipv4 => [ qw(device|OPTIONAL) ], |
25
|
|
|
|
|
|
|
enable_router_ipv4 => [ qw(device|OPTIONAL) ], |
26
|
|
|
|
|
|
|
disable_router_ipv4 => [ qw(device|OPTIONAL) ], |
27
|
|
|
|
|
|
|
default_device => [ qw(ip_address|OPTIONAL) ], |
28
|
|
|
|
|
|
|
default_ipv4_gateway => [ qw(device|OPTIONAL) ], |
29
|
|
|
|
|
|
|
default_ipv6_gateway => [ qw(device|OPTIONAL) ], |
30
|
|
|
|
|
|
|
}, |
31
|
|
|
|
|
|
|
require_modules => { |
32
|
|
|
|
|
|
|
'Net::Routing' => [ ], |
33
|
|
|
|
|
|
|
'Metabrik::Network::Device' => [ ], |
34
|
|
|
|
|
|
|
'Metabrik::Shell::Command' => [ ], |
35
|
|
|
|
|
|
|
}, |
36
|
|
|
|
|
|
|
require_binaries => { |
37
|
|
|
|
|
|
|
'sysctl' => [ ], |
38
|
|
|
|
|
|
|
}, |
39
|
|
|
|
|
|
|
}; |
40
|
|
|
|
|
|
|
} |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
sub brik_use_properties { |
43
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
return { |
46
|
0
|
|
0
|
|
|
|
attributes_default => { |
47
|
|
|
|
|
|
|
device => defined($self->global) && $self->global->device || 'eth0', |
48
|
|
|
|
|
|
|
}, |
49
|
|
|
|
|
|
|
}; |
50
|
|
|
|
|
|
|
} |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
sub show { |
53
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
54
|
|
|
|
|
|
|
|
55
|
0
|
|
|
|
|
|
print "show: IPv4 network routes:\n"; |
56
|
|
|
|
|
|
|
|
57
|
0
|
0
|
|
|
|
|
my $nr4 = Net::Routing->new( |
58
|
|
|
|
|
|
|
target => Net::Routing::NR_TARGET_ALL(), |
59
|
|
|
|
|
|
|
family => Net::Routing::NR_FAMILY_INET4(), |
60
|
|
|
|
|
|
|
) or return $self->log->error("show: Net::Routing new failed: [$Net::Routing::Error]"); |
61
|
0
|
|
|
|
|
|
$nr4->list; |
62
|
|
|
|
|
|
|
|
63
|
0
|
|
|
|
|
|
print "\n"; |
64
|
|
|
|
|
|
|
|
65
|
0
|
|
|
|
|
|
print "show: IPv6 network routes:\n"; |
66
|
|
|
|
|
|
|
|
67
|
0
|
0
|
|
|
|
|
my $nr6 = Net::Routing->new( |
68
|
|
|
|
|
|
|
target => Net::Routing::NR_TARGET_ALL(), |
69
|
|
|
|
|
|
|
family => Net::Routing::NR_FAMILY_INET6(), |
70
|
|
|
|
|
|
|
) or return $self->log->error("show: Net::Routing new failed: [$Net::Routing::Error]"); |
71
|
0
|
|
|
|
|
|
$nr6->list; |
72
|
|
|
|
|
|
|
|
73
|
0
|
|
|
|
|
|
return 1; |
74
|
|
|
|
|
|
|
} |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
sub list { |
77
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
78
|
|
|
|
|
|
|
|
79
|
0
|
0
|
|
|
|
|
my $nr4 = Net::Routing->new( |
80
|
|
|
|
|
|
|
target => Net::Routing::NR_TARGET_ALL(), |
81
|
|
|
|
|
|
|
family => Net::Routing::NR_FAMILY_INET4(), |
82
|
|
|
|
|
|
|
) or return $self->log->error("list: Net::Routing new failed: [$Net::Routing::Error]"); |
83
|
0
|
|
0
|
|
|
|
my $route4 = $nr4->get || []; |
84
|
|
|
|
|
|
|
|
85
|
0
|
|
|
|
|
|
for (@$route4) { |
86
|
0
|
|
|
|
|
|
$_->{family} = 'inet4'; |
87
|
|
|
|
|
|
|
} |
88
|
|
|
|
|
|
|
|
89
|
0
|
0
|
|
|
|
|
my $nr6 = Net::Routing->new( |
90
|
|
|
|
|
|
|
target => Net::Routing::NR_TARGET_ALL(), |
91
|
|
|
|
|
|
|
family => Net::Routing::NR_FAMILY_INET6(), |
92
|
|
|
|
|
|
|
) or return $self->log->error("list: Net::Routing new failed: [$Net::Routing::Error]"); |
93
|
0
|
|
0
|
|
|
|
my $route6 = $nr6->get || []; |
94
|
|
|
|
|
|
|
|
95
|
0
|
|
|
|
|
|
for (@$route6) { |
96
|
0
|
|
|
|
|
|
$_->{family} = 'inet6'; |
97
|
|
|
|
|
|
|
} |
98
|
|
|
|
|
|
|
|
99
|
0
|
|
|
|
|
|
return [ @$route4, @$route6 ]; |
100
|
|
|
|
|
|
|
} |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
sub default_device { |
103
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
104
|
0
|
|
|
|
|
|
my ($ip_address) = @_; |
105
|
|
|
|
|
|
|
|
106
|
0
|
0
|
|
|
|
|
my $nd = Metabrik::Network::Device->new_from_brik_init($self) or return; |
107
|
0
|
|
|
|
|
|
return $nd->default($ip_address); |
108
|
|
|
|
|
|
|
} |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
sub default_ipv4_gateway { |
111
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
112
|
0
|
|
|
|
|
|
my ($device) = @_; |
113
|
|
|
|
|
|
|
|
114
|
0
|
|
0
|
|
|
|
$device ||= ''; |
115
|
|
|
|
|
|
|
|
116
|
0
|
0
|
|
|
|
|
my $routes = $self->list or return; |
117
|
0
|
|
|
|
|
|
for (@$routes) { |
118
|
0
|
0
|
0
|
|
|
|
next unless (length($device) && $_->{interface} eq $device || ! length($device)); |
|
|
|
0
|
|
|
|
|
119
|
0
|
0
|
0
|
|
|
|
if ($_->{family} eq 'inet4' && $_->{default}) { |
120
|
0
|
|
|
|
|
|
return $_->{gateway}; |
121
|
|
|
|
|
|
|
} |
122
|
|
|
|
|
|
|
} |
123
|
|
|
|
|
|
|
|
124
|
0
|
0
|
|
|
|
|
if (length($device)) { |
125
|
0
|
|
|
|
|
|
$self->log->info("default_ipv4_gateway: no default gateway found for device [$device]"); |
126
|
|
|
|
|
|
|
} |
127
|
|
|
|
|
|
|
else { |
128
|
0
|
|
|
|
|
|
$self->log->info("default_ipv4_gateway: no default gateway found"); |
129
|
|
|
|
|
|
|
} |
130
|
|
|
|
|
|
|
|
131
|
0
|
|
|
|
|
|
return 0; |
132
|
|
|
|
|
|
|
} |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
sub default_ipv6_gateway { |
135
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
136
|
0
|
|
|
|
|
|
my ($device) = @_; |
137
|
|
|
|
|
|
|
|
138
|
0
|
|
0
|
|
|
|
$device ||= ''; |
139
|
|
|
|
|
|
|
|
140
|
0
|
0
|
|
|
|
|
my $routes = $self->list or return; |
141
|
0
|
|
|
|
|
|
for (@$routes) { |
142
|
0
|
0
|
0
|
|
|
|
next unless (length($device) && $_->{interface} eq $device || ! length($device)); |
|
|
|
0
|
|
|
|
|
143
|
0
|
0
|
0
|
|
|
|
if ($_->{family} eq 'inet6' && $_->{default}) { |
144
|
0
|
|
|
|
|
|
return $_->{gateway}; |
145
|
|
|
|
|
|
|
} |
146
|
|
|
|
|
|
|
} |
147
|
|
|
|
|
|
|
|
148
|
0
|
0
|
|
|
|
|
if (length($device)) { |
149
|
0
|
|
|
|
|
|
$self->log->info("default_ipv6_gateway: no default gateway found for device [$device]"); |
150
|
|
|
|
|
|
|
} |
151
|
|
|
|
|
|
|
else { |
152
|
0
|
|
|
|
|
|
$self->log->info("default_ipv6_gateway: no default gateway found"); |
153
|
|
|
|
|
|
|
} |
154
|
|
|
|
|
|
|
|
155
|
0
|
|
|
|
|
|
return 0; |
156
|
|
|
|
|
|
|
} |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
sub is_router_ipv4 { |
159
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
160
|
0
|
|
|
|
|
|
my ($device) = @_; |
161
|
|
|
|
|
|
|
|
162
|
0
|
|
0
|
|
|
|
$device ||= $self->device; |
163
|
0
|
0
|
|
|
|
|
$self->brik_help_run_undef_arg('is_router_ipv4', $device) or return; |
164
|
|
|
|
|
|
|
|
165
|
0
|
0
|
|
|
|
|
my $sc = Metabrik::Shell::Command->new_from_brik_init($self) or return; |
166
|
0
|
|
|
|
|
|
$sc->as_matrix(0); |
167
|
0
|
|
|
|
|
|
$sc->as_array(0); |
168
|
0
|
|
|
|
|
|
$sc->capture_stderr(1); |
169
|
|
|
|
|
|
|
|
170
|
0
|
|
|
|
|
|
my $cmd = "sysctl net.ipv4.conf.$device.forwarding"; |
171
|
0
|
|
|
|
|
|
chomp(my $line = $sc->capture($cmd)); |
172
|
|
|
|
|
|
|
|
173
|
0
|
|
|
|
|
|
$self->log->verbose("is_router_ipv4: cmd [$cmd]"); |
174
|
0
|
|
|
|
|
|
$self->log->verbose("is_router_ipv4: returned [$line]"); |
175
|
|
|
|
|
|
|
|
176
|
0
|
|
|
|
|
|
my @toks = split(/\s+/, $line); |
177
|
|
|
|
|
|
|
|
178
|
0
|
|
|
|
|
|
my $is_router = $toks[-1]; |
179
|
|
|
|
|
|
|
|
180
|
0
|
0
|
|
|
|
|
$self->log->info("is_router_ipv4: ".($is_router ? "YES" : "NO")); |
181
|
|
|
|
|
|
|
|
182
|
0
|
|
|
|
|
|
return $is_router; |
183
|
|
|
|
|
|
|
} |
184
|
|
|
|
|
|
|
|
185
|
|
|
|
|
|
|
sub enable_router_ipv4 { |
186
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
187
|
0
|
|
|
|
|
|
my ($device) = @_; |
188
|
|
|
|
|
|
|
|
189
|
0
|
|
0
|
|
|
|
$device ||= $self->device; |
190
|
0
|
0
|
|
|
|
|
$self->brik_help_run_undef_arg('enable_router_ipv4', $device) or return; |
191
|
|
|
|
|
|
|
|
192
|
0
|
0
|
|
|
|
|
my $sc = Metabrik::Shell::Command->new_from_brik_init($self) or return; |
193
|
0
|
|
|
|
|
|
$sc->as_matrix(0); |
194
|
0
|
|
|
|
|
|
$sc->as_array(0); |
195
|
0
|
|
|
|
|
|
$sc->capture_stderr(1); |
196
|
|
|
|
|
|
|
|
197
|
0
|
|
|
|
|
|
my $cmd = "sudo sysctl -w net.ipv4.conf.$device.forwarding=1"; |
198
|
0
|
|
|
|
|
|
chomp(my $line = $sc->capture($cmd)); |
199
|
|
|
|
|
|
|
|
200
|
0
|
|
|
|
|
|
$self->log->verbose("enable_router_ipv4: cmd [$cmd]"); |
201
|
0
|
|
|
|
|
|
$self->log->verbose("enable_router_ipv4: returned [$line]"); |
202
|
|
|
|
|
|
|
|
203
|
0
|
|
|
|
|
|
my @toks = split(/\s+/, $line); |
204
|
|
|
|
|
|
|
|
205
|
0
|
|
|
|
|
|
my $is_router = $toks[-1]; |
206
|
|
|
|
|
|
|
|
207
|
0
|
0
|
|
|
|
|
$self->log->info("enable_router_ipv4: ".($is_router ? "YES" : "NO")); |
208
|
|
|
|
|
|
|
|
209
|
0
|
|
|
|
|
|
return $is_router; |
210
|
|
|
|
|
|
|
} |
211
|
|
|
|
|
|
|
|
212
|
|
|
|
|
|
|
sub disable_router_ipv4 { |
213
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
214
|
0
|
|
|
|
|
|
my ($device) = @_; |
215
|
|
|
|
|
|
|
|
216
|
0
|
|
0
|
|
|
|
$device ||= $self->device; |
217
|
0
|
0
|
|
|
|
|
$self->brik_help_run_undef_arg('disable_router_ipv4', $device) or return; |
218
|
|
|
|
|
|
|
|
219
|
0
|
0
|
|
|
|
|
my $sc = Metabrik::Shell::Command->new_from_brik_init($self) or return; |
220
|
0
|
|
|
|
|
|
$sc->as_matrix(0); |
221
|
0
|
|
|
|
|
|
$sc->as_array(0); |
222
|
0
|
|
|
|
|
|
$sc->capture_stderr(1); |
223
|
|
|
|
|
|
|
|
224
|
0
|
|
|
|
|
|
my $cmd = "sudo sysctl -w net.ipv4.conf.$device.forwarding=0"; |
225
|
0
|
|
|
|
|
|
chomp(my $line = $sc->capture($cmd)); |
226
|
|
|
|
|
|
|
|
227
|
0
|
|
|
|
|
|
$self->log->verbose("disable_router_ipv4: cmd [$cmd]"); |
228
|
0
|
|
|
|
|
|
$self->log->verbose("disable_router_ipv4: returned [$line]"); |
229
|
|
|
|
|
|
|
|
230
|
0
|
|
|
|
|
|
my @toks = split(/\s+/, $line); |
231
|
|
|
|
|
|
|
|
232
|
0
|
|
|
|
|
|
my $is_router = $toks[-1]; |
233
|
|
|
|
|
|
|
|
234
|
0
|
0
|
|
|
|
|
$self->log->info("disable_router_ipv4: ".($is_router ? "YES" : "NO")); |
235
|
|
|
|
|
|
|
|
236
|
0
|
|
|
|
|
|
return $is_router; |
237
|
|
|
|
|
|
|
} |
238
|
|
|
|
|
|
|
|
239
|
|
|
|
|
|
|
1; |
240
|
|
|
|
|
|
|
|
241
|
|
|
|
|
|
|
__END__ |