From nobody Fri Jan 13 21:25:45 2023 X-Original-To: dev-commits-src-all@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 4NtvbY2bb5z2ql1D; Fri, 13 Jan 2023 21:25:45 +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 4NtvbY25hPz43kM; Fri, 13 Jan 2023 21:25:45 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1673645145; 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=QudeiZWcFezAE8YCLDZ+lAKww7lKm3wTLPkSiMJxkl8=; b=W/oSA7Bdm5II0zcAtw3yXpcL3n8Qsaq//IG1iBfcqITe0nqmghVsHs+HBPKTGlhTeipeU8 rxhG2LN5Umu1Aux+YmE1LxauatodM/zHbaArNOvez1vbqLC6TbLOuogNo9Vg2oeDSbDPbV hZVwldcKHTZ2S5lHmeJiuurMa44Kw2/CtoXVvuv7uxFlFnOTr7Nuz1ojBJckNBfQYIdJ+x xqSxlOaMM9tM6/5SyQuSxHIBZ5lGhivo4X2WJr5aYq/UWlcvAFcWXoh+FdwIYsyej4LUap Cxhrdp1rICORILuqLj6TGthgLpJ/99cwPiMsOleB6PIzEHHNobNuiIRVErT92A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1673645145; 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=QudeiZWcFezAE8YCLDZ+lAKww7lKm3wTLPkSiMJxkl8=; b=ikomahFZXbuEykXHtIRAp2z2IaDZZPu6n6vqh2j4TsH7q8inKFbBvIrNN7g7JhEeS+2fqz 0ejBWJ3abtMRTAHkleox9mLuHZ8RGSjazu79sRSX1a1X396mJ41wOisplNq/cqxTY569FR LrvoMY6e0oMKVGW34PptccYraQ0WYz0nAXVHCm4Z2AJKwzCZbAceMzBa++9AXhISIbO9eg a3yg1f5llvClNsZ6H+qEzVWgt++FjG6O8O1G5WWtRbKI6gdb/RYlXrA0Y4Sw6H4H6V7lfP vz4q1FCFUVUWQchItyojhL7bIznxhQZrtl1VrFxUbAHjFeYtii10A+UVCtNrOA== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1673645145; a=rsa-sha256; cv=none; b=NLF0dFG820MfjgWp5vMAJJ+4/gmntdVIjfcqITnw1AhmvXpA8V72JSOIMLg3XIQg/o+7eg 2SQQ4pKxNdrofrfkaRoxFqJx0gCy4wNU1+icm9Hrt+UKTqz1H/qmRqJCUxx5OQ38RWgMUs NAbXH5ifC0JrmHd0+dE36xS6lPRybKWniPXuNlNkkEz88scSFNrHwsmhpFVJppco+gMcY3 59SP7Emu/97nZ5516doC0pwKvKlRGIWnpnPl7SWyEw2FY2lr/pqIaE7CAm/PKgjSCPjQnW 6yOoKXsnbJJgKRIxGgnXWsx5zhW79eqih7AcvAm7sN0AJkYrQp4AXknd9g+vXA== 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 4NtvbY15pBzNDP; Fri, 13 Jan 2023 21:25:45 +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 30DLPjLa042280; Fri, 13 Jan 2023 21:25:45 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 30DLPjHA042279; Fri, 13 Jan 2023 21:25:45 GMT (envelope-from git) Date: Fri, 13 Jan 2023 21:25:45 GMT Message-Id: <202301132125.30DLPjHA042279@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: 0b381e393d40 - stable/13 - routing: add rib_add_default_route() wrapper List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-all@freebsd.org X-BeenThere: dev-commits-src-all@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: 0b381e393d40db2254ea2212c86ad02aeb2d4406 Auto-Submitted: auto-generated X-ThisMailContainsUnwantedMimeParts: N The branch stable/13 has been updated by melifaro: URL: https://cgit.FreeBSD.org/src/commit/?id=0b381e393d40db2254ea2212c86ad02aeb2d4406 commit 0b381e393d40db2254ea2212c86ad02aeb2d4406 Author: Alexander V. Chernikov AuthorDate: 2022-08-09 13:34:28 +0000 Commit: Alexander V. Chernikov CommitDate: 2023-01-13 21:24:11 +0000 routing: add rib_add_default_route() wrapper Multiple consumers in the kernel space want to install IPv4 or IPv6 default route. Provide convenient wrapper to simplify the code inside the customers. MFC after: 1 month Differential Revision: https://reviews.freebsd.org/D36167 (cherry picked from commit d8b2693414ae5cebdbd4c64c9559275c4baff11e) --- sys/net/route/route_ctl.h | 4 ++-- sys/net/route/route_helpers.c | 56 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+), 2 deletions(-) diff --git a/sys/net/route/route_ctl.h b/sys/net/route/route_ctl.h index 5cb7e67120ae..bfd769c3d524 100644 --- a/sys/net/route/route_ctl.h +++ b/sys/net/route/route_ctl.h @@ -79,8 +79,8 @@ int rib_match_gw(const struct rtentry *rt, const struct nhop_object *nh, void *_data); int rib_handle_ifaddr_info(uint32_t fibnum, int cmd, struct rt_addrinfo *info); -int rib_match_gw(const struct rtentry *rt, const struct nhop_object *nh, - void *gw_sa); +int rib_add_default_route(uint32_t fibnum, int family, struct ifnet *ifp, + struct sockaddr *gw, struct rib_cmd_info *rc); typedef void route_notification_t(struct rib_cmd_info *rc, void *); void rib_decompose_notification(struct rib_cmd_info *rc, diff --git a/sys/net/route/route_helpers.c b/sys/net/route/route_helpers.c index 9f18b9bfcc63..a9d21ebfb507 100644 --- a/sys/net/route/route_helpers.c +++ b/sys/net/route/route_helpers.c @@ -409,6 +409,62 @@ rib_decompose_notification(struct rib_cmd_info *rc, route_notification_t *cb, } #endif +union sockaddr_union { + struct sockaddr sa; + struct sockaddr_in sin; + struct sockaddr_in6 sin6; + char _buf[32]; +}; + +/* + * Creates nexhops suitable for using as a default route nhop. + * Helper for the various kernel subsystems adding/changing default route. + */ +int +rib_add_default_route(uint32_t fibnum, int family, struct ifnet *ifp, + struct sockaddr *gw, struct rib_cmd_info *rc) +{ + struct route_nhop_data rnd = { .rnd_weight = RT_DEFAULT_WEIGHT }; + union sockaddr_union saun = {}; + struct sockaddr *dst = &saun.sa; + int error; + + switch (family) { +#ifdef INET + case AF_INET: + saun.sin.sin_family = AF_INET; + saun.sin.sin_len = sizeof(struct sockaddr_in); + break; +#endif +#ifdef INET6 + case AF_INET6: + saun.sin6.sin6_family = AF_INET6; + saun.sin6.sin6_len = sizeof(struct sockaddr_in6); + break; +#endif + default: + return (EAFNOSUPPORT); + } + + struct ifaddr *ifa = ifaof_ifpforaddr(gw, ifp); + if (ifa == NULL) + return (ENOENT); + + struct nhop_object *nh = nhop_alloc(fibnum, family); + if (nh == NULL) + return (ENOMEM); + + nhop_set_gw(nh, gw, true); + nhop_set_transmit_ifp(nh, ifp); + nhop_set_src(nh, ifa); + nhop_set_pxtype_flag(nh, NHF_DEFAULT); + rnd.rnd_nhop = nhop_get_nhop(nh, &error); + + if (error == 0) + error = rib_add_route_px(fibnum, dst, 0, &rnd, RTM_F_CREATE, rc); + return (error); +} + #ifdef INET /* * Checks if the found key in the trie contains (<=) a prefix covering