From nobody Fri Jan 13 21:25:36 2023 X-Original-To: dev-commits-src-branches@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4NtvbN3T19z2qlBK; Fri, 13 Jan 2023 21:25:36 +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 4NtvbN2PgXz436P; Fri, 13 Jan 2023 21:25:36 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1673645136; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=mJuTmIYDGkMEBOgytWFsusNTmx4P29U07IvyEsVSGME=; b=Q5oU5oFeu1nEeB15urJGkxVfk8ZnhNOF0hwDkh+iW3bY+y9JuzBJIo+UPHJ9hEn5FMqb2+ foFyAL5Urpc6cw6fRvPnkM3FTgWnzwU3xwLfP9ICcZHOx2yTKq+/R2ikP8bxQhBCfm2JkH Ym7YbIbmo+xmHRYUs7GwKpsMZXVpPjtTfxx3ptr+UoUTZdUs7uQnftZO+vDSfc9mKHTf+y yIZ9eO5EwVdTyOlokq+V2hRyZ+Dq6/8P5SiLvFjPt6l4QzGo8Oe+Z7mx754mIXLIuZGwJ/ TJNcQkYi4jmzNo8dLVS6sBBDA1X5gkCAMjPlMYfu0ozQiuDp6SkvKTcfVXtZgA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1673645136; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=mJuTmIYDGkMEBOgytWFsusNTmx4P29U07IvyEsVSGME=; b=rGIkQwDg53mrmgqQf7c/x31FcQW0ObFUDQl+z16AKjFUoDiNHYfIVbkn5w1tr4+fNErBWH vNpg+UK8eY7WnIzCB50paQY0PXUBnYoVblcC27Tozr0zjNRa9y23QQetsZXgs01hiCMzN7 q4eI8paNePLUT7ofeywqRBZGA+R1klW80myw6UtDR0Yk1DRtHnoTq2sJGpuieBe0jVQ8gu U/FkTC18+xKxtZDzs/gXZHg1jf6hwSM/sXie6LfsNauetRborngLjggGG6lW5T60NwdPiS rPELQj6JlZUlgRjlZ96Bzaf1yTP+fyOgpfAJjJMJbX7EteJJ3fxGLLGx7y1ckQ== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1673645136; a=rsa-sha256; cv=none; b=Vqq63AJviWjuFnxG5ujFd/wW3J36SiWUakyyvrnLF5WP36slGendoDOSRqIbfJ1qLG+ToT rpSWy/UGWe/pq1G6OVxwKYpM9ER/TIoyunjPnODm3AQLZZJsMxcPSLWlfnSz0BrBtOwwl8 V/SKWBEDXTcNZfMT/tz3dupEbfcQKbHq74ivR/Cp8PnB97dBWeY6WbSL8EmjhERpb+y5+0 YBzRIA7qFoD1KC5lIbwB+4lIvJRjK7Tk3NDjrhHzX7bzvQPDM2YFtVFpDi53U1TKQM15xS Uv/O8bnJyYJcp4eiuah/n5nQGIjxkjXG6EzPpDd38o3Gk6NFJ0XoG+1S000jqw== 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 4NtvbN1Wp2zNJY; Fri, 13 Jan 2023 21:25:36 +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 30DLPapK042070; Fri, 13 Jan 2023 21:25:36 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 30DLPaPW042069; Fri, 13 Jan 2023 21:25:36 GMT (envelope-from git) Date: Fri, 13 Jan 2023 21:25:36 GMT Message-Id: <202301132125.30DLPaPW042069@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: 9ecae0139331 - stable/13 - netinet6: simplify selectroute() List-Id: Commits to the stable branches of the FreeBSD src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-branches List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-branches@freebsd.org X-BeenThere: dev-commits-src-branches@freebsd.org 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: 9ecae0139331b96005930c96e7cb7a35ad90d249 Auto-Submitted: auto-generated X-ThisMailContainsUnwantedMimeParts: N The branch stable/13 has been updated by melifaro: URL: https://cgit.FreeBSD.org/src/commit/?id=9ecae0139331b96005930c96e7cb7a35ad90d249 commit 9ecae0139331b96005930c96e7cb7a35ad90d249 Author: Alexander V. Chernikov AuthorDate: 2022-07-04 18:05:38 +0000 Commit: Alexander V. Chernikov CommitDate: 2023-01-13 21:24:11 +0000 netinet6: simplify selectroute() Effectively selectroute() addresses two different cases: providing interface info for multicast destinations and providing nexthop data for unicast ones. Current implementation intertwines handling of both cases, especially in the error handling part. Factor out all route lookup logic in a separate function, lookup_route() to simplify the code. Ensure consistent KPI: no error means *retifp is set and otherwise. Differential Revision: https://reviews.freebsd.org/D35711 MFC after: 2 weeks (cherry picked from commit 2756774c3f537bc566362ef424d992092fb6fc87) --- sys/netinet6/in6_src.c | 115 ++++++++++++++++++++++++------------------------- 1 file changed, 56 insertions(+), 59 deletions(-) diff --git a/sys/netinet6/in6_src.c b/sys/netinet6/in6_src.c index 472e16d318ad..da536a3424b9 100644 --- a/sys/netinet6/in6_src.c +++ b/sys/netinet6/in6_src.c @@ -643,6 +643,47 @@ cache_route(uint32_t fibnum, const struct sockaddr_in6 *dst, struct route_in6 *r return (ro->ro_nh); } +static struct nhop_object * +lookup_route(uint32_t fibnum, struct sockaddr_in6 *dst, struct route_in6 *ro, + struct ip6_pktopts *opts, uint32_t flowid) +{ + struct nhop_object *nh = NULL; + + /* + * If the next hop address for the packet is specified by the caller, + * use it as the gateway. + */ + if (opts && opts->ip6po_nexthop) { + struct route_in6 *ron = &opts->ip6po_nextroute; + struct sockaddr_in6 *sin6_next = satosin6(opts->ip6po_nexthop); + + nh = cache_route(fibnum, sin6_next, ron, flowid); + + /* + * The node identified by that address must be a + * neighbor of the sending host. + */ + if (nh != NULL && (nh->nh_flags & NHF_GATEWAY) != 0) + nh = NULL; + } else if (ro != NULL) { + nh = cache_route(fibnum, dst, ro, flowid); + if (nh == NULL) + return (NULL); + + /* + * Check if the outgoing interface conflicts with + * the interface specified by ipi6_ifindex (if specified). + */ + struct in6_pktinfo *pi; + if (opts && (pi = opts->ip6po_pktinfo) != NULL && pi->ipi6_ifindex) { + if (nh->nh_aifp->if_index != pi->ipi6_ifindex) + nh = NULL; + } + } + + return (nh); +} + /* * Finds outgoing nexthop or the outgoing interface for the * @dstsock. @@ -656,11 +697,8 @@ selectroute(struct sockaddr_in6 *dstsock, struct ip6_pktopts *opts, { int error = 0; struct ifnet *ifp = NULL; - struct nhop_object *nh = NULL; - struct sockaddr_in6 *sin6_next; struct in6_pktinfo *pi = NULL; struct in6_addr *dst = &dstsock->sin6_addr; - uint32_t zoneid; /* If the caller specify the outgoing interface explicitly, use it. */ if (opts && (pi = opts->ip6po_pktinfo) != NULL && pi->ipi6_ifindex) { @@ -689,66 +727,28 @@ selectroute(struct sockaddr_in6 *dstsock, struct ip6_pktopts *opts, */ if (IN6_IS_ADDR_MC_LINKLOCAL(dst) || IN6_IS_ADDR_MC_NODELOCAL(dst)) { - zoneid = ntohs(in6_getscope(dst)); + uint32_t zoneid = ntohs(in6_getscope(dst)); if (zoneid > 0) { ifp = in6_getlinkifnet(zoneid); goto done; } } - getroute: - /* - * If the next hop address for the packet is specified by the caller, - * use it as the gateway. - */ - if (opts && opts->ip6po_nexthop) { - struct route_in6 *ron = &opts->ip6po_nextroute; - sin6_next = satosin6(opts->ip6po_nexthop); - - nh = cache_route(fibnum, sin6_next, ron, flowid); - - /* - * The node identified by that address must be a - * neighbor of the sending host. - */ - if (nh != NULL && (nh->nh_flags & NHF_GATEWAY) == 0) - ifp = nh->nh_ifp; - else { - nh = NULL; // cached nh is still stored in @opts - error = EHOSTUNREACH; - } - } else if (ro != NULL) { - nh = cache_route(fibnum, dstsock, ro, flowid); - if (nh != NULL) - ifp = nh->nh_ifp; - else - error = EHOSTUNREACH; - - /* - * Check if the outgoing interface conflicts with - * the interface specified by ipi6_ifindex (if specified). - * Note that loopback interface is always okay. - * (this may happen when we are sending a packet to one of - * our own addresses.) - */ - if (ifp && opts && opts->ip6po_pktinfo && - opts->ip6po_pktinfo->ipi6_ifindex) { - if (!(ifp->if_flags & IFF_LOOPBACK) && - ifp->if_index != - opts->ip6po_pktinfo->ipi6_ifindex) { - error = EHOSTUNREACH; - ifp = NULL; - nh = NULL; - } - } + getroute:; + struct nhop_object *nh = lookup_route(fibnum, dstsock, ro, opts, flowid); + if (nh != NULL) { + *retifp = nh->nh_aifp; + error = 0; + } else { + *retifp = NULL; + IP6STAT_INC(ip6s_noroute); + error = EHOSTUNREACH; } - /* - * Output must be consistent: no error -> both ifp and nh != NULL, - * otherwise both NULL - */ + *retnh = nh; + return (error); done: - if (ifp == NULL && nh == NULL) { + if (ifp == NULL) { /* * This can happen if the caller did not pass a cached route * nor any other hints. We treat this case an error. @@ -758,11 +758,8 @@ selectroute(struct sockaddr_in6 *dstsock, struct ip6_pktopts *opts, if (error == EHOSTUNREACH) IP6STAT_INC(ip6s_noroute); - if (nh != NULL) - *retifp = nh->nh_aifp; - else - *retifp = ifp; - *retnh = nh; /* nh may be NULL */ + *retifp = ifp; + *retnh = NULL; return (error); }