| 1 | # | ||
| 2 | # $Id$ | ||
| 3 | # | ||
| 4 | # system::route Brik | ||
| 5 | # | ||
| 6 | package Metabrik::System::Route; | ||
| 7 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | |||
| 10 | 1 | ▶ | use base qw(Metabrik); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 11 | |||
| 12 | sub brik_properties { | ||
| 13 | return { | ||
| 14 | revision => '$Revision$', | ||
| 15 | tags => [ qw(unstable) ], | ||
| 16 | author => 'GomoR <GomoR[at]metabrik.org>', | ||
| 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 | my $self = shift; | ||
| 44 | |||
| 45 | return { | ||
| 46 | attributes_default => { | ||
| 47 | device => defined($self->global) && $self->global->device || 'eth0', | ||
| 48 | }, | ||
| 49 | }; | ||
| 50 | } | ||
| 51 | |||
| 52 | sub show { | ||
| 53 | my $self = shift; | ||
| 54 | |||
| 55 | print "show: IPv4 network routes:\n"; | ||
| 56 | |||
| 57 | 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 | $nr4->list; | ||
| 62 | |||
| 63 | print "\n"; | ||
| 64 | |||
| 65 | print "show: IPv6 network routes:\n"; | ||
| 66 | |||
| 67 | 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 | $nr6->list; | ||
| 72 | |||
| 73 | return 1; | ||
| 74 | } | ||
| 75 | |||
| 76 | sub list { | ||
| 77 | my $self = shift; | ||
| 78 | |||
| 79 | 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 | my $route4 = $nr4->get || []; | ||
| 84 | |||
| 85 | for (@$route4) { | ||
| 86 | $_->{family} = 'inet4'; | ||
| 87 | } | ||
| 88 | |||
| 89 | 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 | my $route6 = $nr6->get || []; | ||
| 94 | |||
| 95 | for (@$route6) { | ||
| 96 | $_->{family} = 'inet6'; | ||
| 97 | } | ||
| 98 | |||
| 99 | return [ @$route4, @$route6 ]; | ||
| 100 | } | ||
| 101 | |||
| 102 | sub default_device { | ||
| 103 | my $self = shift; | ||
| 104 | my ($ip_address) = @_; | ||
| 105 | |||
| 106 | my $nd = Metabrik::Network::Device->new_from_brik_init($self) or return; | ||
| 107 | return $nd->default($ip_address); | ||
| 108 | } | ||
| 109 | |||
| 110 | sub default_ipv4_gateway { | ||
| 111 | my $self = shift; | ||
| 112 | my ($device) = @_; | ||
| 113 | |||
| 114 | $device ||= ''; | ||
| 115 | |||
| 116 | my $routes = $self->list or return; | ||
| 117 | for (@$routes) { | ||
| 118 | next unless (length($device) && $_->{interface} eq $device || ! length($device)); | ||
| 119 | if ($_->{family} eq 'inet4' && $_->{default}) { | ||
| 120 | return $_->{gateway}; | ||
| 121 | } | ||
| 122 | } | ||
| 123 | |||
| 124 | if (length($device)) { | ||
| 125 | $self->log->info("default_ipv4_gateway: no default gateway found for device [$device]"); | ||
| 126 | } | ||
| 127 | else { | ||
| 128 | $self->log->info("default_ipv4_gateway: no default gateway found"); | ||
| 129 | } | ||
| 130 | |||
| 131 | return 0; | ||
| 132 | } | ||
| 133 | |||
| 134 | sub default_ipv6_gateway { | ||
| 135 | my $self = shift; | ||
| 136 | my ($device) = @_; | ||
| 137 | |||
| 138 | $device ||= ''; | ||
| 139 | |||
| 140 | my $routes = $self->list or return; | ||
| 141 | for (@$routes) { | ||
| 142 | next unless (length($device) && $_->{interface} eq $device || ! length($device)); | ||
| 143 | if ($_->{family} eq 'inet6' && $_->{default}) { | ||
| 144 | return $_->{gateway}; | ||
| 145 | } | ||
| 146 | } | ||
| 147 | |||
| 148 | if (length($device)) { | ||
| 149 | $self->log->info("default_ipv6_gateway: no default gateway found for device [$device]"); | ||
| 150 | } | ||
| 151 | else { | ||
| 152 | $self->log->info("default_ipv6_gateway: no default gateway found"); | ||
| 153 | } | ||
| 154 | |||
| 155 | return 0; | ||
| 156 | } | ||
| 157 | |||
| 158 | sub is_router_ipv4 { | ||
| 159 | my $self = shift; | ||
| 160 | my ($device) = @_; | ||
| 161 | |||
| 162 | $device ||= $self->device; | ||
| 163 | $self->brik_help_run_undef_arg('is_router_ipv4', $device) or return; | ||
| 164 | |||
| 165 | my $sc = Metabrik::Shell::Command->new_from_brik_init($self) or return; | ||
| 166 | $sc->as_matrix(0); | ||
| 167 | $sc->as_array(0); | ||
| 168 | $sc->capture_stderr(1); | ||
| 169 | |||
| 170 | my $cmd = "sysctl net.ipv4.conf.$device.forwarding"; | ||
| 171 | chomp(my $line = $sc->capture($cmd)); | ||
| 172 | |||
| 173 | $self->log->verbose("is_router_ipv4: cmd [$cmd]"); | ||
| 174 | $self->log->verbose("is_router_ipv4: returned [$line]"); | ||
| 175 | |||
| 176 | my @toks = split(/\s+/, $line); | ||
| 177 | |||
| 178 | my $is_router = $toks[-1]; | ||
| 179 | |||
| 180 | $self->log->info("is_router_ipv4: ".($is_router ? "YES" : "NO")); | ||
| 181 | |||
| 182 | return $is_router; | ||
| 183 | } | ||
| 184 | |||
| 185 | sub enable_router_ipv4 { | ||
| 186 | my $self = shift; | ||
| 187 | my ($device) = @_; | ||
| 188 | |||
| 189 | $device ||= $self->device; | ||
| 190 | $self->brik_help_run_undef_arg('enable_router_ipv4', $device) or return; | ||
| 191 | |||
| 192 | my $sc = Metabrik::Shell::Command->new_from_brik_init($self) or return; | ||
| 193 | $sc->as_matrix(0); | ||
| 194 | $sc->as_array(0); | ||
| 195 | $sc->capture_stderr(1); | ||
| 196 | |||
| 197 | my $cmd = "sudo sysctl -w net.ipv4.conf.$device.forwarding=1"; | ||
| 198 | chomp(my $line = $sc->capture($cmd)); | ||
| 199 | |||
| 200 | $self->log->verbose("enable_router_ipv4: cmd [$cmd]"); | ||
| 201 | $self->log->verbose("enable_router_ipv4: returned [$line]"); | ||
| 202 | |||
| 203 | my @toks = split(/\s+/, $line); | ||
| 204 | |||
| 205 | my $is_router = $toks[-1]; | ||
| 206 | |||
| 207 | $self->log->info("enable_router_ipv4: ".($is_router ? "YES" : "NO")); | ||
| 208 | |||
| 209 | return $is_router; | ||
| 210 | } | ||
| 211 | |||
| 212 | sub disable_router_ipv4 { | ||
| 213 | my $self = shift; | ||
| 214 | my ($device) = @_; | ||
| 215 | |||
| 216 | $device ||= $self->device; | ||
| 217 | $self->brik_help_run_undef_arg('disable_router_ipv4', $device) or return; | ||
| 218 | |||
| 219 | my $sc = Metabrik::Shell::Command->new_from_brik_init($self) or return; | ||
| 220 | $sc->as_matrix(0); | ||
| 221 | $sc->as_array(0); | ||
| 222 | $sc->capture_stderr(1); | ||
| 223 | |||
| 224 | my $cmd = "sudo sysctl -w net.ipv4.conf.$device.forwarding=0"; | ||
| 225 | chomp(my $line = $sc->capture($cmd)); | ||
| 226 | |||
| 227 | $self->log->verbose("disable_router_ipv4: cmd [$cmd]"); | ||
| 228 | $self->log->verbose("disable_router_ipv4: returned [$line]"); | ||
| 229 | |||
| 230 | my @toks = split(/\s+/, $line); | ||
| 231 | |||
| 232 | my $is_router = $toks[-1]; | ||
| 233 | |||
| 234 | $self->log->info("disable_router_ipv4: ".($is_router ? "YES" : "NO")); | ||
| 235 | |||
| 236 | return $is_router; | ||
| 237 | } | ||
| 238 | |||
| 239 | 1; | ||
| 240 | |||
| 241 | __END__ | ||