diff --git a/ncm-network/src/main/perl/nmstate.pm b/ncm-network/src/main/perl/nmstate.pm index 0bcbd3379a..10907f9791 100644 --- a/ncm-network/src/main/perl/nmstate.pm +++ b/ncm-network/src/main/perl/nmstate.pm @@ -296,6 +296,17 @@ sub generate_vip_config { return $iface_cfg; } +# Check if given ip belongs to a network +sub ip_in_network { + my ($self, $check_ip, $ip, $netmask) = @_; + # is the given gw ip in his ip/netmask. + my $thisip = NetAddr::IP->new("$check_ip"); + if ($thisip->within(NetAddr::IP->new("$ip", "$netmask"))) { + return 1; + } + return 0; +} + # generates the hashrefs for interface in yaml file format needed by nmstate. # bulk of the config settings needed by the nmstate yml is done here. # to add additional options, it should be constructed here. @@ -388,12 +399,16 @@ sub generate_nmstate_config # create default route entry. my %default_rt; if ($default_gw) { - # create only default gw entry if gw entry match interface gateway defined + # nmstate configure requires next-hop-interface. + # create default gw entry on this interface only if it falls within the subnet boundary. # otherwise this interface is not the default gw interface. - if ((defined($iface->{gateway})) and ($iface->{gateway} eq $default_gw)) { - $default_rt{destination} = '0.0.0.0/0'; - $default_rt{'next-hop-address'} = $default_gw; - $default_rt{'next-hop-interface'} = $device; + if ((defined($iface->{ip})) and (defined($iface->{netmask}))) { + my $is_dgw_iface = $self->ip_in_network($default_gw, $iface->{ip}, $iface->{netmask}); + if ($is_dgw_iface) { + $default_rt{destination} = '0.0.0.0/0'; + $default_rt{'next-hop-address'} = $default_gw; + $default_rt{'next-hop-interface'} = $device; + } } } # combined default route with any policy routing/rule, if any diff --git a/ncm-network/src/test/perl/nmstate_advance.t b/ncm-network/src/test/perl/nmstate_advance.t index 5705775d28..d2be4f5ddc 100644 --- a/ncm-network/src/test/perl/nmstate_advance.t +++ b/ncm-network/src/test/perl/nmstate_advance.t @@ -45,6 +45,9 @@ routes: config: - next-hop-interface: eth0 state: absent + - destination: 0.0.0.0/0 + next-hop-address: 4.3.2.254 + next-hop-interface: eth0 - destination: 1.2.3.4/32 next-hop-interface: eth0 - destination: 1.2.3.5/24 @@ -82,6 +85,9 @@ routes: config: - next-hop-interface: eth0.123 state: absent + - destination: 0.0.0.0/0 + next-hop-address: 4.3.2.254 + next-hop-interface: eth0.123 - destination: 1.2.3.4/32 next-hop-interface: eth0.123 EOF @@ -126,6 +132,9 @@ routes: config: - next-hop-interface: bond0 state: absent + - destination: 0.0.0.0/0 + next-hop-address: 4.3.2.254 + next-hop-interface: bond0 EOF diff --git a/ncm-network/src/test/perl/nmstate_simple.t b/ncm-network/src/test/perl/nmstate_simple.t index e313938c4c..5ca004519c 100644 --- a/ncm-network/src/test/perl/nmstate_simple.t +++ b/ncm-network/src/test/perl/nmstate_simple.t @@ -61,6 +61,9 @@ routes: config: - next-hop-interface: eth0 state: absent + - destination: 0.0.0.0/0 + next-hop-address: 4.3.2.254 + next-hop-interface: eth0 EOF Readonly my $NOTTOREMOVE => <