From nobody Fri Jul 08 11:36:03 2022 X-Original-To: dev-commits-src-main@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 4B67417FB46D; Fri, 8 Jul 2022 11:36:04 +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 4LfWSN1hhDz3Myk; Fri, 8 Jul 2022 11:36:04 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1657280164; 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=Cgyf2d3UF99WuhPAMBF5a9NoyZcj+j7tGjpSOl70G4E=; b=eZDfyfdliBSQB6m5FOCGGMVJdlImV+KiGmViO7ZpIaoffaQoOsFoka3VZlQEyrwmRt8hyL Y7qaPDj0b3U7W7WXIdTuAS5QSc4EQMwA0k9DFjzadtq0S+PR9q77lPW2sBLd3EIwujdwIB g2lkZT2hrye9l8VgI7BcfrtPfiTJUJlxcskypZHIHjc5j1daCOD/wtbTZ0tjP3tI2Karqt J/GZIeVvY8vJIerVoSq652fSDtMijukEUNE+4kii4QREJlcet4JSUWIKHZ/WNkMensw/s0 PKMxGujv5ypK4yk9U0Vj8IpY5KmcvjGjP0cZwmEW5c1WGEnbYqGu7p0RpfAd3Q== 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 4LfWSN0L6zz1KZx; Fri, 8 Jul 2022 11:36:04 +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 268Ba3XM063426; Fri, 8 Jul 2022 11:36:03 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 268Ba3hZ063425; Fri, 8 Jul 2022 11:36:03 GMT (envelope-from git) Date: Fri, 8 Jul 2022 11:36:03 GMT Message-Id: <202207081136.268Ba3hZ063425@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: "Alexander V. Chernikov" Subject: git: 2756774c3f53 - main - netinet6: simplify selectroute() List-Id: Commit messages for the main branch of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-main List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-main@freebsd.org X-BeenThere: dev-commits-src-main@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/main X-Git-Reftype: branch X-Git-Commit: 2756774c3f537bc566362ef424d992092fb6fc87 Auto-Submitted: auto-generated ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1657280164; 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=Cgyf2d3UF99WuhPAMBF5a9NoyZcj+j7tGjpSOl70G4E=; b=EVdwGf5QDbl6KHPOeKc7NJMEooUoEidpeczZdmh74TvE1bcTA3CjTI7So4M3ixPwqiLngL tB9GpVzT8Ztt3L2TzeICT/RHcUKoUSc2Y5OS94Ib8S8dsKTS+dZYIgBTF/FBGIzhoyKAEN SnaILx1f0vw525iNjnqsoUskkU+bYKMl+u+/9yW9Y0PnWFQaamH7y/1M8MI5mCPvvtp9km 5n1aLXwLkbxsPey1lERJbIcYT9uNoANqTxT4aG1R1ETcHwTDygC6uVlttneqiWN1W0OIaI RPe79HGgpOZkeX0618XD6rKdN2qG37bV0FgLUl/Q6VNzinCKi/F5qT74RP+9Vw== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1657280164; a=rsa-sha256; cv=none; b=Ye9e3Mm2r1N4oW5qrndhKmXFOIaFNHluPIAOQ2tx6a76TIuYQKeIXGHMFYQyPBGVKkrRua k9785zrEdtDMIxdc8Zjmmz8GeJtZiyhe7OUGi32PZQm2dqS8+M7+K6eTqR3+akiXJk5U0f WOImenh0zRmLkNndK8nh6ZdRlf8dsGLUmkou+ZfexkcbOPklARpzUJ1Jox/exQImMpoKwy SWQi/2dF6oeacg9kSlMqAY2YFLXSrQLpMIT4Sk4zzgamhJvlHVeXRoRpr6lhobFTDP6GRL f3SlIxeeEUcwQw3RQehgEMl/symFpajUo1aiUrxn/K2E5cUH+NoqV5A+nZbjDw== ARC-Authentication-Results: i=1; mx1.freebsd.org; none X-ThisMailContainsUnwantedMimeParts: N The branch main has been updated by melifaro: URL: https://cgit.FreeBSD.org/src/commit/?id=2756774c3f537bc566362ef424d992092fb6fc87 commit 2756774c3f537bc566362ef424d992092fb6fc87 Author: Alexander V. Chernikov AuthorDate: 2022-07-04 18:05:38 +0000 Commit: Alexander V. Chernikov CommitDate: 2022-07-08 11:27:16 +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 --- 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 e44a1b0873b4..7188282013cc 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); }