git: 7a4a122f9731 - stable/14 - netlink: fix adding an interface route

From: Zhenlei Huang <zlei_at_FreeBSD.org>
Date: Thu, 14 Mar 2024 08:29:42 UTC
The branch stable/14 has been updated by zlei:

URL: https://cgit.FreeBSD.org/src/commit/?id=7a4a122f9731c589831e1c00d85588ff9813b338

commit 7a4a122f9731c589831e1c00d85588ff9813b338
Author:     KUROSAWA Takahiro <takahiro.kurosawa@gmail.com>
AuthorDate: 2023-11-28 18:14:50 +0000
Commit:     Zhenlei Huang <zlei@FreeBSD.org>
CommitDate: 2024-03-14 08:27:42 +0000

    netlink: fix adding an interface route
    
    route add <host> -iface <netif>" for a netif without an IPv4/IPv6
    address fails with EINVAL. Need to use a link-level ifaddr for gw if
    an ifaddr for dst is not found as the rtsock-based implementation does.
    
    PR:             275341
    Reported by:    Sean Cody <sean@tinfoilhat.ca>
    Reviewed by:    rcm
    Tested by:      rcm
    Approved by:    kp (mentor)
    MFC after:      1 week
    Differential Revision:  https://reviews.freebsd.org/D41330
    
    (cherry picked from commit f818559774cb0c1516364c4beca361480fd68b5b)
---
 sys/netlink/route/rt.c | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)

diff --git a/sys/netlink/route/rt.c b/sys/netlink/route/rt.c
index cfaa2167b0d2..ed09748995dc 100644
--- a/sys/netlink/route/rt.c
+++ b/sys/netlink/route/rt.c
@@ -750,9 +750,14 @@ finalize_nhop(struct nhop_object *nh, const struct sockaddr *dst, int *perror)
 
 		struct ifaddr *ifa = ifaof_ifpforaddr(gw_sa, nh->nh_ifp);
 		if (ifa == NULL) {
-			NL_LOG(LOG_DEBUG, "Unable to determine ifa, skipping");
-			*perror = EINVAL;
-			return (NULL);
+			/* Try link-level ifa. */
+			gw_sa = &nh->gw_sa;
+			ifa = ifaof_ifpforaddr(gw_sa, nh->nh_ifp);
+			if (ifa == NULL) {
+				NL_LOG(LOG_DEBUG, "Unable to determine ifa, skipping");
+				*perror = EINVAL;
+				return (NULL);
+			}
 		}
 		nhop_set_src(nh, ifa);
 	}