Skip to content

Commit

Permalink
ncm-network: nmstate - default gateway fix.
Browse files Browse the repository at this point in the history
nmstate requires next-hop-interface for every route to be added.
Create default route entry only on the interface if default_gateway falls within subnet boundary.

fixes #1655
  • Loading branch information
Abdul Karim authored and Abdul Karim committed Mar 19, 2024
1 parent a626bba commit db9dc61
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 5 deletions.
24 changes: 19 additions & 5 deletions ncm-network/src/main/perl/nmstate.pm
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,16 @@ sub find_vlan_id {
}
return $vlanid;
}
# 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.
Expand Down Expand Up @@ -414,12 +424,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
# 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;
# next-hop-interface is mandatory in nmstate therefore we need interface to create default route entry.
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
Expand Down
12 changes: 12 additions & 0 deletions ncm-network/src/test/perl/nmstate_advance.t
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -107,6 +113,9 @@ routes:
config:
- next-hop-interface: vlan0
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: vlan0
EOF
Expand Down Expand Up @@ -152,6 +161,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


Expand Down
3 changes: 3 additions & 0 deletions ncm-network/src/test/perl/nmstate_simple.t
Original file line number Diff line number Diff line change
Expand Up @@ -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 => <<EOF;
Expand Down
1 change: 1 addition & 0 deletions ncm-network/src/test/resources/nmstate_advance.pan
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ object template nmstate_advance;
include 'simple_base_profile';
include 'components/network/config-nmstate';

"/system/network/default_gateway" = "4.3.2.254";
# additional interface testing for nmstate.
"/system/network/interfaces/eth1" = create("dhcpinterface");
"/hardware/cards/nic/eth1/hwaddr" = "6e:a5:1b:55:77:0b";
Expand Down

0 comments on commit db9dc61

Please sign in to comment.