From ce27a13e90885de3a830e4ec78afc9efeadae42e Mon Sep 17 00:00:00 2001 From: Igor Ryzhov Date: Sat, 6 Nov 2021 01:22:07 +0300 Subject: lib: fix vrf deletion when the last interface is deleted Currently, we automatically delete an inactive VRF when its last interface is deleted. This code introduces a couple of crashes because of the following problems: - vrf_delete is called before calling if_del hook, so daemons may try to dereference an ifp->vrf pointer which is freed - in if_terminate, we continue to use the VRF in the loop condition after the last interface is deleted This check is needed only when the interface is deleted by the user, because if the interface is deleted by the system, VRF must still exist in the system. Move the check to appropriate places to fix crashes. Signed-off-by: Igor Ryzhov --- lib/if.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) (limited to 'lib/if.c') diff --git a/lib/if.c b/lib/if.c index 71d2f5d9cc..a40f04f5a2 100644 --- a/lib/if.c +++ b/lib/if.c @@ -303,9 +303,6 @@ void if_delete(struct interface **ifp) if (ptr->ifindex != IFINDEX_INTERNAL) IFINDEX_RB_REMOVE(vrf, ptr); - if (!vrf_is_enabled(vrf)) - vrf_delete(vrf); - if_delete_retain(ptr); list_delete(&ptr->connected); @@ -1420,7 +1417,7 @@ static int lib_interface_create(struct nb_cb_create_args *args) static int lib_interface_destroy(struct nb_cb_destroy_args *args) { struct interface *ifp; - + struct vrf *vrf; switch (args->event) { case NB_EV_VALIDATE: @@ -1436,9 +1433,13 @@ static int lib_interface_destroy(struct nb_cb_destroy_args *args) break; case NB_EV_APPLY: ifp = nb_running_unset_entry(args->dnode); + vrf = ifp->vrf; ifp->configured = false; if_delete(&ifp); + + if (!vrf_is_enabled(vrf)) + vrf_delete(vrf); break; } -- cgit v1.2.3