From nobody Tue Apr 04 08:42:41 2023 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 4PrLqk1pkbz43D4H; Tue, 4 Apr 2023 08:42:42 +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 4PrLqk1Bncz3msM; Tue, 4 Apr 2023 08:42:42 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1680597762; 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=7/7B7qVmfzM5DbaM2kaxGovOW+62IRkJ2/0J7VxUSco=; b=w6Niksj4Ye+9nczju9HiR40+fUtPiHs40q4iY1cIhCwGmHdNGJATvbbAOkQFYcsZypw6NI 5oJmdmbhWfseE6hNbFY/ZF6AGVuW0yKzPoFmviR3FhBw0nb88jbUQw3tpkLaQ+brcu7pqr JTECXUUiyEkFyKnxSTgybTjR14kJsHQQEhqNCORLhnyiMCGfRfcu7uWPATbM88pvoqq14r MGjq86Sy2BZDxT73rawE6tNmNTWVldigrejyMseec0B0P68KSuJQJX5NGhEsE06hrVwrtS qsvuyC2pqlm5DyNLrHfifsdHiU+nIdcKLRqDhSeX/y/yQMaOuilJsPL1wTR+zw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1680597762; 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=7/7B7qVmfzM5DbaM2kaxGovOW+62IRkJ2/0J7VxUSco=; b=K9Yt7UY3GQQzMSrpcMw1vipoey2GuVzH7ZV84GHTN1Ozw6jz/t/8znIXYo+Drv4CXCE0TC ZsoGYAnQ+EH8K1Y8wPUTIlQCclQ6/Gj+f855TRVO62tG6CyyPs7KYQF3zsM4G5ZBmqmfyI cmchwKbGa7AyZXnWyHSwGNf3xwqXniJiBANTGz8ZB8XH/34+WsSzabrboHIqfl9hOvD6CF uTh8Ao1J+/Gv7KEjBcu5DZ31fyiVccmUpXwtIVvK6aKIWuKHifqLgG9c+7YEqxFcrbDZX5 brG9sLPW28lR94HyK8jOBitAkcUEOlDnVIFUVRNIdDaHOaJlClsT3EsWZwxK/A== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1680597762; a=rsa-sha256; cv=none; b=LOMSN1NZ0RkIG2/05d+hTZnxD5tEUbYBsi779FJycHT4JM2PCzYnEDhfQp0rB/OfwdXFV4 ziQPSY5Eqq/MjZkd34Els7tC0men9fKuksA4H8L7/+iNYoYSjUScZPxsvh3cAqrFyGnfAA TAsiQxpofQ0qXdw1a483z4soelnfv7U6JBkm1CiZGjFZPo5y7xkXhxxWe2H9lvbekAeNT7 fMvs2PFkzf+zrqA9bp3exedWvB9WL3pC0ODbPP0JEjP3FBY8shBGsO0Ot6eAfJbk5LGX4Z Uy5UJaviT62Qfflo3fgLWtRhxRkm1F5BmY7Exep+AIdm2wCV/6YeDkSLYQ9R9g== 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 4PrLqk0FLYzYdY; Tue, 4 Apr 2023 08:42:42 +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 3348gf55038878; Tue, 4 Apr 2023 08:42:41 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 3348gfE0038877; Tue, 4 Apr 2023 08:42:41 GMT (envelope-from git) Date: Tue, 4 Apr 2023 08:42:41 GMT Message-Id: <202304040842.3348gfE0038877@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: f262b06a57b0 - main - route: fix route get netlink translation. 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: f262b06a57b03a2025bdc0ce628514cd68af73db Auto-Submitted: auto-generated X-ThisMailContainsUnwantedMimeParts: N The branch main has been updated by melifaro: URL: https://cgit.FreeBSD.org/src/commit/?id=f262b06a57b03a2025bdc0ce628514cd68af73db commit f262b06a57b03a2025bdc0ce628514cd68af73db Author: Alexander V. Chernikov AuthorDate: 2023-04-04 08:37:14 +0000 Commit: Alexander V. Chernikov CommitDate: 2023-04-04 08:42:33 +0000 route: fix route get netlink translation. route.c uses newroute() to handle the "route get" command. The logic inside newroute() adds RTF_GATEWAY flag if "-interface" flag is not specified. That results in the inconsistent RTM_GET message with RTF_GATEWAY set but no RTAX_GATEWAY provided. Address this in the translation code by checking if the gateway is actually provided. --- sbin/route/route.c | 4 ++-- sbin/route/route_netlink.c | 48 +++++++++++++++++++++++++++++----------------- 2 files changed, 32 insertions(+), 20 deletions(-) diff --git a/sbin/route/route.c b/sbin/route/route.c index 947c97ce794a..4002cbe5867b 100644 --- a/sbin/route/route.c +++ b/sbin/route/route.c @@ -118,7 +118,7 @@ static int rtmsg_rtsock(int, int, int); static int flushroutes_fib_rtsock(int); static void monitor_rtsock(void); #else -int rtmsg_nl(int, int, int, struct sockaddr_storage *, struct rt_metrics *); +int rtmsg_nl(int, int, int, int, struct sockaddr_storage *, struct rt_metrics *); int flushroutes_fib_nl(int, int); void monitor_nl(int); #endif @@ -1524,7 +1524,7 @@ rtmsg(int cmd, int flags, int fib) #ifdef WITHOUT_NETLINK return (rtmsg_rtsock(cmd, flags, fib)); #else - errno = rtmsg_nl(cmd, flags, fib, so, &rt_metrics); + errno = rtmsg_nl(cmd, flags, fib, rtm_addrs, so, &rt_metrics); return (errno == 0 ? 0 : -1); #endif } diff --git a/sbin/route/route_netlink.c b/sbin/route/route_netlink.c index ce7da288496f..a3e9860244a9 100644 --- a/sbin/route/route_netlink.c +++ b/sbin/route/route_netlink.c @@ -33,7 +33,7 @@ void printb(int, const char *); extern const char routeflags[]; extern int verbose, debugonly; -int rtmsg_nl(int cmd, int rtm_flags, int fib, struct sockaddr_storage *so, +int rtmsg_nl(int cmd, int rtm_flags, int fib, int rtm_addrs, struct sockaddr_storage *so, struct rt_metrics *rt_metrics); int flushroutes_fib_nl(int fib, int af); void monitor_nl(int fib); @@ -125,8 +125,18 @@ nl_helper_free(struct nl_helper *h) snl_free(&h->ss_cmd); } +static struct sockaddr * +get_addr(struct sockaddr_storage *so, int rtm_addrs, int addr_type) +{ + struct sockaddr *sa = NULL; + + if (rtm_addrs & (1 << addr_type)) + sa = (struct sockaddr *)&so[addr_type]; + return (sa); +} + static int -rtmsg_nl_int(struct nl_helper *h, int cmd, int rtm_flags, int fib, +rtmsg_nl_int(struct nl_helper *h, int cmd, int rtm_flags, int fib, int rtm_addrs, struct sockaddr_storage *so, struct rt_metrics *rt_metrics) { struct snl_state *ss = &h->ss_cmd; @@ -154,9 +164,9 @@ rtmsg_nl_int(struct nl_helper *h, int cmd, int rtm_flags, int fib, exit(1); } - struct sockaddr *dst = (struct sockaddr *)&so[RTAX_DST]; - struct sockaddr *mask = (struct sockaddr *)&so[RTAX_NETMASK]; - struct sockaddr *gw = (struct sockaddr *)&so[RTAX_GATEWAY]; + struct sockaddr *dst = get_addr(so, rtm_addrs, RTAX_DST); + struct sockaddr *mask = get_addr(so, rtm_addrs, RTAX_NETMASK); + struct sockaddr *gw = get_addr(so, rtm_addrs, RTAX_GATEWAY); if (dst == NULL) return (EINVAL); @@ -210,16 +220,18 @@ rtmsg_nl_int(struct nl_helper *h, int cmd, int rtm_flags, int fib, snl_add_msg_attr_ip(&nw, RTA_DST, dst); snl_add_msg_attr_u32(&nw, RTA_TABLE, fib); - if (rtm_flags & RTF_GATEWAY) { - if (gw->sa_family == dst->sa_family) - snl_add_msg_attr_ip(&nw, RTA_GATEWAY, gw); - else - snl_add_msg_attr_ipvia(&nw, RTA_VIA, gw); - } else if (gw != NULL) { - /* Should be AF_LINK */ - struct sockaddr_dl *sdl = (struct sockaddr_dl *)gw; - if (sdl->sdl_index != 0) - snl_add_msg_attr_u32(&nw, RTA_OIF, sdl->sdl_index); + if (gw != NULL) { + if (rtm_flags & RTF_GATEWAY) { + if (gw->sa_family == dst->sa_family) + snl_add_msg_attr_ip(&nw, RTA_GATEWAY, gw); + else + snl_add_msg_attr_ipvia(&nw, RTA_VIA, gw); + } else { + /* Should be AF_LINK */ + struct sockaddr_dl *sdl = (struct sockaddr_dl *)gw; + if (sdl->sdl_index != 0) + snl_add_msg_attr_u32(&nw, RTA_OIF, sdl->sdl_index); + } } if (rtm_flags != 0) @@ -259,13 +271,13 @@ rtmsg_nl_int(struct nl_helper *h, int cmd, int rtm_flags, int fib, } int -rtmsg_nl(int cmd, int rtm_flags, int fib, struct sockaddr_storage *so, - struct rt_metrics *rt_metrics) +rtmsg_nl(int cmd, int rtm_flags, int fib, int rtm_addrs, + struct sockaddr_storage *so, struct rt_metrics *rt_metrics) { struct nl_helper h = {}; nl_helper_init(&h); - int error = rtmsg_nl_int(&h, cmd, rtm_flags, fib, so, rt_metrics); + int error = rtmsg_nl_int(&h, cmd, rtm_flags, fib, rtm_addrs, so, rt_metrics); nl_helper_free(&h); return (error);