Default gateway support

This commit is contained in:
Jamie Cameron
2018-07-07 12:37:09 -07:00
parent b023875d2a
commit b4dbbd9827

View File

@@ -4,9 +4,12 @@
# XXX MTU
$netplan_dir = "/etc/netplan";
$sysctl_config = "/etc/sysctl.conf";
do 'linux-lib.pl';
# boot_interfaces()
# Returns a list of all interfaces activated at boot time
sub boot_interfaces
{
my @rv;
@@ -320,6 +323,73 @@ sub routing_config_files
return ( $netplan_dir, $sysctl_config );
}
# routing_input()
# show default router and device
sub routing_input
{
my ($addr, $router) = &get_default_gateway();
my ($addr6, $router6) = &get_default_ipv6_gateway();
my @ifaces = grep { $_->{'virtual'} eq '' } &boot_interfaces();
# Show default gateway
print &ui_table_row($text{'routes_default'},
&ui_radio("gateway_def", $addr ? 0 : 1,
[ [ 1, $text{'routes_none'} ],
[ 0, $text{'routes_gateway'}." ".
&ui_textbox("gateway", $addr, 15)." ".
&ui_select("gatewaydev", $router,
[ map { $_->{'name'} } @ifaces ]) ] ]));
# Show default IPv6 gateway
print &ui_table_row($text{'routes_default6'},
&ui_radio("gateway6_def", $addr6 ? 0 : 1,
[ [ 1, $text{'routes_none'} ],
[ 0, $text{'routes_gateway'}." ".
&ui_textbox("gateway6", $addr6, 30)." ".
&ui_select("gatewaydev6", $router6,
[ map { $_->{'name'} } @ifaces ]) ] ]));
# Act as router?
local %sysctl;
&read_env_file($sysctl_config, \%sysctl);
print &ui_table_row($text{'routes_forward'},
&ui_yesno_radio("forward",
$sysctl{'net.ipv4.ip_forward'} ? 1 : 0));
}
# parse_routing()
# Save the form generated by routing_input
sub parse_routing
{
# Save IPv4 address
my ($dev, $gw);
if (!$in{'gateway_def'}) {
&check_ipaddress($in{'gateway'}) ||
&error(&text('routes_egateway', $in{'gateway'}));
$gw = $in{'gateway'};
$dev = $in{'gatewaydev'};
}
&set_default_gateway($gw, $dev);
# Save IPv6 address
my ($dev6, $gw6);
if (!$in{'gateway6_def'}) {
&check_ip6address($in{'gateway6'}) ||
&error(&text('routes_egateway6', $in{'gateway6'}));
$gw6 = $in{'gateway6'};
$dev6 = $in{'gatewaydev6'};
}
&set_default_ipv6_gateway($gw6, $dev6);
# Save routing flag
local %sysctl;
&lock_file($sysctl_config);
&read_env_file($sysctl_config, \%sysctl);
$sysctl{'net.ipv4.ip_forward'} = $in{'forward'};
&write_env_file($sysctl_config, \%sysctl);
&unlock_file($sysctl_config);
}
sub network_config_files
{
return ( "/etc/hostname", "/etc/HOSTNAME", "/etc/mailname" );
@@ -332,6 +402,72 @@ sub apply_network
&system_logged("(cd / ; netplan apply) >/dev/null 2>&1");
}
# get_default_gateway()
# Returns the default gateway IP (if one is set) and device (if set) boot time
# settings.
sub get_default_gateway
{
foreach my $iface (&boot_interfaces()) {
if ($iface->{'gateway'}) {
return ( $iface->{'gateway'}, $iface->{'fullname'} );
}
}
return ( );
}
# set_default_gateway([gateway, device])
# Sets the default gateway to the given IP accessible via the given device,
# in the boot time settings.
sub set_default_gateway
{
my ($gw, $dev) = @_;
foreach my $iface (&boot_interfaces()) {
if ($iface->{'fullname'} eq $dev && $iface->{'gateway'} ne $gw) {
# Need to add to this interface
$iface->{'gateway'} = $gw;
&save_interface($iface);
}
elsif ($iface->{'fullname'} ne $dev && $iface->{'gateway'}) {
# Need to remove from this interface
delete($iface->{'gateway'});
&save_interface($iface);
}
}
}
# get_default_ipv6_gateway()
# Returns the default gateway IPv6 address (if one is set) and device (if set)
# boot time settings.
sub get_default_ipv6_gateway
{
foreach my $iface (&boot_interfaces()) {
if ($iface->{'gateway6'}) {
return ( $iface->{'gateway6'}, $iface->{'fullname'} );
}
}
return ( );
}
# set_default_ipv6_gateway([gateway, device])
# Sets the default IPv6 gateway to the given IP accessible via the given device,
# in the boot time settings.
sub set_default_ipv6_gateway
{
my ($gw, $dev) = @_;
foreach my $iface (&boot_interfaces()) {
if ($iface->{'fullname'} eq $dev && $iface->{'gateway6'} ne $gw) {
# Need to add to this interface
$iface->{'gateway6'} = $gw;
&save_interface($iface);
}
elsif ($iface->{'fullname'} ne $dev && $iface->{'gateway6'}) {
# Need to remove from this interface
delete($iface->{'gateway6'});
&save_interface($iface);
}
}
}
# read_yaml_file(file)
# Converts a YAML file into a nested hash ref
sub read_yaml_file