From owner-dev-commits-src-branches@freebsd.org Tue Sep 7 21:30:47 2021 Return-Path: Delivered-To: dev-commits-src-branches@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 2BC0866D448; Tue, 7 Sep 2021 21:30:47 +0000 (UTC) (envelope-from git@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) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4H3z2t3Rdmz4ff1; Tue, 7 Sep 2021 21:30:46 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 3F93C19D30; Tue, 7 Sep 2021 21:30:46 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.16.1/8.16.1) with ESMTP id 187LUktM046293; Tue, 7 Sep 2021 21:30:46 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 187LUkIq046292; Tue, 7 Sep 2021 21:30:46 GMT (envelope-from git) Date: Tue, 7 Sep 2021 21:30:46 GMT Message-Id: <202109072130.187LUkIq046292@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: "Alexander V. Chernikov" Subject: git: f3d69003374a - stable/13 - routing: Bring back the ability to specify transmit interface via its name. MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: melifaro X-Git-Repository: src X-Git-Refname: refs/heads/stable/13 X-Git-Reftype: branch X-Git-Commit: f3d69003374a291e2ccb4a1ecc318e90827bac09 Auto-Submitted: auto-generated X-BeenThere: dev-commits-src-branches@freebsd.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: Commits to the stable branches of the FreeBSD src repository List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 07 Sep 2021 21:30:47 -0000 The branch stable/13 has been updated by melifaro: URL: https://cgit.FreeBSD.org/src/commit/?id=f3d69003374a291e2ccb4a1ecc318e90827bac09 commit f3d69003374a291e2ccb4a1ecc318e90827bac09 Author: Alexander V. Chernikov AuthorDate: 2021-08-29 19:51:28 +0000 Commit: Alexander V. Chernikov CommitDate: 2021-09-07 21:25:24 +0000 routing: Bring back the ability to specify transmit interface via its name. Some software references outgoing interfaces by specifying name instead of index. Use rti_ifp from rt_addrinfo if provided instead of always using address interface when constructing nexthop. PR: 255678 Reported by: martin.larsson2 at gmail.com (cherry picked from commit d98954e229812eee2fa6bf97714fecbbdcc56e4c) --- sys/net/route.c | 45 ++++++++++++++++++++++++++++++++++++--------- sys/net/route/nhop_ctl.c | 2 +- 2 files changed, 37 insertions(+), 10 deletions(-) diff --git a/sys/net/route.c b/sys/net/route.c index 2416aa9a983f..a24438563f50 100644 --- a/sys/net/route.c +++ b/sys/net/route.c @@ -500,6 +500,38 @@ rt_flushifroutes(struct ifnet *ifp) rib_foreach_table_walk_del(AF_UNSPEC, rt_ifdelroute, ifp); } +/* + * Tries to extract interface from RTAX_IFP passed in rt_addrinfo. + * Interface can be specified ether as interface index (sdl_index) or + * the interface name (sdl_data). + * + * Returns found ifp or NULL + */ +static struct ifnet * +info_get_ifp(struct rt_addrinfo *info) +{ + const struct sockaddr_dl *sdl; + + sdl = (const struct sockaddr_dl *)info->rti_info[RTAX_IFP]; + if (sdl->sdl_family != AF_LINK) + return (NULL); + + if (sdl->sdl_index != 0) + return (ifnet_byindex(sdl->sdl_index)); + if (sdl->sdl_nlen > 0) { + char if_name[IF_NAMESIZE]; + if (sdl->sdl_nlen + offsetof(struct sockaddr_dl, sdl_data) > sdl->sdl_len) + return (NULL); + if (sdl->sdl_nlen >= IF_NAMESIZE) + return (NULL); + bzero(if_name, sizeof(if_name)); + memcpy(if_name, sdl->sdl_data, sdl->sdl_nlen); + return (ifunit(if_name)); + } + + return (NULL); +} + /* * Look up rt_addrinfo for a specific fib. * @@ -509,12 +541,11 @@ rt_flushifroutes(struct ifnet *ifp) int rt_getifa_fib(struct rt_addrinfo *info, u_int fibnum) { - const struct sockaddr *dst, *gateway, *ifpaddr, *ifaaddr; + const struct sockaddr *dst, *gateway, *ifaaddr; int error, flags; dst = info->rti_info[RTAX_DST]; gateway = info->rti_info[RTAX_GATEWAY]; - ifpaddr = info->rti_info[RTAX_IFP]; ifaaddr = info->rti_info[RTAX_IFA]; flags = info->rti_flags; @@ -524,13 +555,9 @@ rt_getifa_fib(struct rt_addrinfo *info, u_int fibnum) */ error = 0; - /* If we have interface specified by the ifindex in the address, use it */ - if (info->rti_ifp == NULL && ifpaddr != NULL && - ifpaddr->sa_family == AF_LINK) { - const struct sockaddr_dl *sdl = (const struct sockaddr_dl *)ifpaddr; - if (sdl->sdl_index != 0) - info->rti_ifp = ifnet_byindex(sdl->sdl_index); - } + /* If we have interface specified by RTAX_IFP address, try to use it */ + if ((info->rti_ifp == NULL) && (info->rti_info[RTAX_IFP] != NULL)) + info->rti_ifp = info_get_ifp(info); /* * If we have source address specified, try to find it * TODO: avoid enumerating all ifas on all interfaces. diff --git a/sys/net/route/nhop_ctl.c b/sys/net/route/nhop_ctl.c index 92b43871d604..21aefcc7a83b 100644 --- a/sys/net/route/nhop_ctl.c +++ b/sys/net/route/nhop_ctl.c @@ -286,7 +286,7 @@ fill_nhop_from_info(struct nhop_priv *nh_priv, struct rt_addrinfo *info) if ((error = set_nhop_gw_from_info(nh, info)) != 0) return (error); - nh->nh_ifp = info->rti_ifa->ifa_ifp; + nh->nh_ifp = (info->rti_ifp != NULL) ? info->rti_ifp : info->rti_ifa->ifa_ifp; nh->nh_ifa = info->rti_ifa; /* depends on the gateway */ nh->nh_aifp = get_aifp(nh);