From owner-svn-src-head@freebsd.org Thu May 7 08:11:37 2020 Return-Path: Delivered-To: svn-src-head@mailman.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.nyi.freebsd.org (Postfix) with ESMTP id 8088B13CC0E; Thu, 7 May 2020 08:11:37 +0000 (UTC) (envelope-from melifaro@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) server-signature RSA-PSS (4096 bits) client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "Let's Encrypt Authority X3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 49HmQ12vW2z3Ht5; Thu, 7 May 2020 08:11:37 +0000 (UTC) (envelope-from melifaro@FreeBSD.org) Received: from repo.freebsd.org (repo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 5ED20DDED; Thu, 7 May 2020 08:11:37 +0000 (UTC) (envelope-from melifaro@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id 0478Bb2B008820; Thu, 7 May 2020 08:11:37 GMT (envelope-from melifaro@FreeBSD.org) Received: (from melifaro@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id 0478BavJ008126; Thu, 7 May 2020 08:11:36 GMT (envelope-from melifaro@FreeBSD.org) Message-Id: <202005070811.0478BavJ008126@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: melifaro set sender to melifaro@FreeBSD.org using -f From: "Alexander V. Chernikov" Date: Thu, 7 May 2020 08:11:36 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r360773 - in head/sys/net: . route X-SVN-Group: head X-SVN-Commit-Author: melifaro X-SVN-Commit-Paths: in head/sys/net: . route X-SVN-Commit-Revision: 360773 X-SVN-Commit-Repository: base MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-head@freebsd.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: SVN commit messages for the src tree for head/-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 07 May 2020 08:11:37 -0000 Author: melifaro Date: Thu May 7 08:11:36 2020 New Revision: 360773 URL: https://svnweb.freebsd.org/changeset/base/360773 Log: Add rib_lookup() sockaddr lookup wrapper and make ifa_ifwithroute use it. Create rib_lookup() wrapper around per-af dataplane lookup functions. This will help in the cases of having control plane af-agnostic code. Switch ifa_ifwithroute() to use this function instead of rtalloc1(). Reviewed by: ae Differential Revision: https://reviews.freebsd.org/D24731 Modified: head/sys/net/route.c head/sys/net/route.h head/sys/net/route/route_helpers.c Modified: head/sys/net/route.c ============================================================================== --- head/sys/net/route.c Thu May 7 03:50:34 2020 (r360772) +++ head/sys/net/route.c Thu May 7 08:11:36 2020 (r360773) @@ -683,7 +683,6 @@ ifa_ifwithroute(int flags, const struct sockaddr *dst, u_int fibnum) { struct ifaddr *ifa; - int not_found = 0; NET_EPOCH_ASSERT(); if ((flags & RTF_GATEWAY) == 0) { @@ -710,34 +709,17 @@ ifa_ifwithroute(int flags, const struct sockaddr *dst, if (ifa == NULL) ifa = ifa_ifwithnet(gateway, 0, fibnum); if (ifa == NULL) { - struct rtentry *rt; + struct nhop_object *nh; - rt = rtalloc1_fib(gateway, 0, flags, fibnum); - if (rt == NULL) - goto out; + nh = rib_lookup(fibnum, gateway, NHR_NONE, 0); + /* * dismiss a gateway that is reachable only * through the default router */ - switch (gateway->sa_family) { - case AF_INET: - if (satosin(rt_key(rt))->sin_addr.s_addr == INADDR_ANY) - not_found = 1; - break; - case AF_INET6: - if (IN6_IS_ADDR_UNSPECIFIED(&satosin6(rt_key(rt))->sin6_addr)) - not_found = 1; - break; - default: - break; - } - if (!not_found && rt->rt_nhop->nh_ifa != NULL) { - ifa = rt->rt_nhop->nh_ifa; - } - RT_REMREF(rt); - RT_UNLOCK(rt); - if (not_found || ifa == NULL) - goto out; + if ((nh == NULL) || (nh->nh_flags & NHF_DEFAULT)) + return (NULL); + ifa = nh->nh_ifa; } if (ifa->ifa_addr->sa_family != dst->sa_family) { struct ifaddr *oifa = ifa; @@ -745,7 +727,7 @@ ifa_ifwithroute(int flags, const struct sockaddr *dst, if (ifa == NULL) ifa = oifa; } - out: + return (ifa); } Modified: head/sys/net/route.h ============================================================================== --- head/sys/net/route.h Thu May 7 03:50:34 2020 (r360772) +++ head/sys/net/route.h Thu May 7 08:11:36 2020 (r360773) @@ -436,6 +436,8 @@ int rib_add_redirect(u_int fibnum, struct sockaddr *ds /* New API */ void rib_walk(int af, u_int fibnum, rt_walktree_f_t *wa_f, void *arg); +struct nhop_object *rib_lookup(uint32_t fibnum, const struct sockaddr *dst, + uint32_t flags, uint32_t flowid); #endif #endif Modified: head/sys/net/route/route_helpers.c ============================================================================== --- head/sys/net/route/route_helpers.c Thu May 7 03:50:34 2020 (r360772) +++ head/sys/net/route/route_helpers.c Thu May 7 08:11:36 2020 (r360773) @@ -55,6 +55,12 @@ __FBSDID("$FreeBSD$"); #include #include #include +#ifdef INET +#include +#endif +#ifdef INET6 +#include +#endif #include /* @@ -79,5 +85,50 @@ rib_walk(int af, u_int fibnum, rt_walktree_f_t *wa_f, RIB_RLOCK(rnh); rnh->rnh_walktree(&rnh->head, (walktree_f_t *)wa_f, arg); RIB_RUNLOCK(rnh); +} + +/* + * Wrapper for the control plane functions for performing af-agnostic + * lookups. + * @fibnum: fib to perform the lookup. + * @dst: sockaddr with family and addr filled in. IPv6 addresses needs to be in + * deembedded from. + * @flags: fib(9) flags. + * @flowid: flow id for path selection in multipath use case. + * + * Returns nhop_object or NULL. + * + * Requires NET_EPOCH. + * + */ +struct nhop_object * +rib_lookup(uint32_t fibnum, const struct sockaddr *dst, uint32_t flags, + uint32_t flowid) +{ + struct nhop_object *nh; + + nh = NULL; + + switch (dst->sa_family) { +#ifdef INET + case AF_INET: + { + const struct sockaddr_in *a = (const struct sockaddr_in *)dst; + nh = fib4_lookup(fibnum, a->sin_addr, 0, flags, flowid); + break; + } +#endif +#ifdef INET6 + case AF_INET6: + { + const struct sockaddr_in6 *a = (const struct sockaddr_in6*)dst; + nh = fib6_lookup(fibnum, &a->sin6_addr, a->sin6_scope_id, + flags, flowid); + break; + } +#endif + } + + return (nh); }