From nobody Sun Oct 01 00:22:10 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 4RylC631CPz4w2gV; Sun, 1 Oct 2023 00:22:10 +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 4RylC62TkWz4LGc; Sun, 1 Oct 2023 00:22:10 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1696119730; 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=DDfLT55oIesf1Nu30UAME1FOmoNkYK78z7QUyE0Ohd0=; b=urhTioD+2xWLlXOgiS4kxbN4geI3ihV3pPcn13sTQsVkQ3uE2INKfK2qiXWS8TyOPetk24 tfo9ZytEx4hxl3dWNYEzVavpdVhzjcFlMYeBYLgiglNOz1ApSHU8T5W3+honzUCAVmII9e sg+yBfeZ+YSulsAydLdfAKxg4p8uvnNmwKNA34L8lvB4QyY9q619AMMyl9oKHdB2bJ2lC1 +vgRi8qm0X1XkAk8lVGvRwD2a5AagVBd3y1oWvUHM95uIuLFFUkD40nd3s2OuXreFUgEo1 P3XW5u9DKkhKK1euRdCNVJlfnXFVMPz4Xbqawe9q+WL5gQjWYhZNRVrgH9nIZw== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1696119730; a=rsa-sha256; cv=none; b=KURGI3le8EJwFdh4tfF5OCnh0jCksE5vDzqS6pOabLQGXDaLG8CXn56HvDJplXBdsWINxi 42iXLuVl4BtAvBk2NPsoLE/K14XBsBvqqvMjurpA9fFfn42GCXHhmQvS7fVJA0gNSqoi6j /hdw2bPovLdNB/60Ao8jStkUmAuGq6JiKuIWwuv/tsqdjx1Br+NKc8rCD/0UrONcHfGkgs spnGEwh323n81S2lVPPbPEmSfgJ1Xvu7/TBt7ryNWIzO3BCVKesPAgNLdLyU8Ive4WjJOd PA4yJ/IBGhx593xjC7v5eyZP42o76Rr+uoby772Pu16dA8F5xzspdz/+hvTeVQ== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1696119730; 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=DDfLT55oIesf1Nu30UAME1FOmoNkYK78z7QUyE0Ohd0=; b=gK9M5yX69P2rzZ/dNU2Rgqk6Yte5PwJ+eMZbrW2mVbtIu0XdWNlp35mhANt/BWieDGJq78 PlK0eFW2QQlj+tDRPp0EQqUAc5MmGjrNS4swFFW5rZztnJR7KWvA+aPkoixURbjngG5O/c 5jCWTOnfIjM94il9aXq/IWatpUXFnDez/h3yEPi1n5lzyGex34sqp3Z0YJjPCz6j4UvdGm pweuXEPEOY4V6fkZSrIxVAt+DR83BClGpFn9/QQ83CFThmY6UWO7+e2DEd86bVeWKYuAXB KIoxQ8IqS5k85vQTTfeafw4t7PSACIS51L376wgCGBCOxpFY/7DtSF3Psiq+MA== 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 4RylC61XrJz17Y4; Sun, 1 Oct 2023 00:22:10 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.17.1/8.17.1) with ESMTP id 3910MA4r093440; Sun, 1 Oct 2023 00:22:10 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.17.1/8.17.1/Submit) id 3910MAlp093437; Sun, 1 Oct 2023 00:22:10 GMT (envelope-from git) Date: Sun, 1 Oct 2023 00:22:10 GMT Message-Id: <202310010022.3910MAlp093437@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Dag-Erling =?utf-8?Q?Sm=C3=B8rgrav?= Subject: git: f21f0d2e16af - stable/14 - arp(8): fix by-interface and by-host filtering when using netlink 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: des X-Git-Repository: src X-Git-Refname: refs/heads/stable/14 X-Git-Reftype: branch X-Git-Commit: f21f0d2e16af702c53bc150c6c23d1bc99399bcd Auto-Submitted: auto-generated The branch stable/14 has been updated by des: URL: https://cgit.FreeBSD.org/src/commit/?id=f21f0d2e16af702c53bc150c6c23d1bc99399bcd commit f21f0d2e16af702c53bc150c6c23d1bc99399bcd Author: R. Christian McDonald AuthorDate: 2023-09-14 07:07:24 +0000 Commit: Dag-Erling Smørgrav CommitDate: 2023-09-30 13:26:12 +0000 arp(8): fix by-interface and by-host filtering when using netlink arp(8) has traditionally supported filtering by interface via -i and by hostname. However, this functionality was omitted from the initial netlink-ification of arp. This patch re-introduces this filtering functionality. This patch also improves by-interface filtering by storing and using the ifindex of the requested interface for filtering instead of comparing interface name strings Reviewed by: melifaro Sponsored by: Rubicon Communications, LLC ("Netgate") (cherry picked from commit 79278872ad966e5f54805efbeb692c8cbc0306c8) --- usr.sbin/arp/arp.c | 34 +++++++++++++++------------------- usr.sbin/arp/arp.h | 2 ++ usr.sbin/arp/arp_netlink.c | 8 ++++++++ 3 files changed, 25 insertions(+), 19 deletions(-) diff --git a/usr.sbin/arp/arp.c b/usr.sbin/arp/arp.c index 02b2bb1ac4f8..9a19d792f788 100644 --- a/usr.sbin/arp/arp.c +++ b/usr.sbin/arp/arp.c @@ -98,8 +98,6 @@ static int get_ether_addr(in_addr_t ipaddr, struct ether_addr *hwaddr); static int set_rtsock(struct sockaddr_in *dst, struct sockaddr_dl *sdl_m, char *host); -static char *rifname; - struct if_nameindex *ifnameindex; struct arp_opts opts = {}; @@ -146,7 +144,7 @@ main(int argc, char *argv[]) SETFUNC(F_FILESET); break; case 'i': - rifname = optarg; + opts.rifname = optarg; break; case '?': default: @@ -157,15 +155,15 @@ main(int argc, char *argv[]) if (!func) func = F_GET; - if (rifname) { + if (opts.rifname) { if (func != F_GET && !(func == F_DELETE && opts.aflag)) xo_errx(1, "-i not applicable to this operation"); - if (if_nametoindex(rifname) == 0) { + if ((opts.rifindex = if_nametoindex(opts.rifname)) == 0) { if (errno == ENXIO) xo_errx(1, "interface %s does not exist", - rifname); + opts.rifname); else - xo_err(1, "if_nametoindex(%s)", rifname); + xo_err(1, "if_nametoindex(%s)", opts.rifname); } } switch (func) { @@ -179,7 +177,7 @@ main(int argc, char *argv[]) xo_open_list("arp-cache"); struct in_addr all_addrs = {}; - print_entries(0, all_addrs); + print_entries(opts.rifindex, all_addrs); xo_close_list("arp-cache"); xo_close_container("arp"); @@ -448,13 +446,13 @@ get(char *host) xo_open_container("arp"); xo_open_list("arp-cache"); - found = print_entries(0, addr->sin_addr); + found = print_entries(opts.rifindex, addr->sin_addr); if (found == 0) { xo_emit("{d:hostname/%s} ({d:ip-address/%s}) -- no entry", host, inet_ntoa(addr->sin_addr)); - if (rifname) - xo_emit(" on {d:interface/%s}", rifname); + if (opts.rifname) + xo_emit(" on {d:interface/%s}", opts.rifname); xo_emit("\n"); } @@ -552,7 +550,6 @@ search(u_long addr, action_fn *action) struct rt_msghdr *rtm; struct sockaddr_in *sin2; struct sockaddr_dl *sdl; - char ifname[IF_NAMESIZE]; int st, found_entry = 0; mib[0] = CTL_NET; @@ -586,14 +583,13 @@ search(u_long addr, action_fn *action) rtm = (struct rt_msghdr *)next; sin2 = (struct sockaddr_in *)(rtm + 1); sdl = (struct sockaddr_dl *)((char *)sin2 + SA_SIZE(sin2)); - if (rifname && if_indextoname(sdl->sdl_index, ifname) && - strcmp(ifname, rifname)) + if (opts.rifindex && + (opts.rifindex != sdl->sdl_index)) continue; - if (addr) { - if (addr != sin2->sin_addr.s_addr) - continue; - found_entry = 1; - } + if (addr && + (addr != sin2->sin_addr.s_addr)) + continue; + found_entry = 1; (*action)(sdl, sin2, rtm); } free(buf); diff --git a/usr.sbin/arp/arp.h b/usr.sbin/arp/arp.h index a7de3a1a3024..487863be43e7 100644 --- a/usr.sbin/arp/arp.h +++ b/usr.sbin/arp/arp.h @@ -10,6 +10,8 @@ struct arp_opts { bool nflag; time_t expire_time; int flags; + char *rifname; + unsigned int rifindex; }; extern struct arp_opts opts; diff --git a/usr.sbin/arp/arp_netlink.c b/usr.sbin/arp/arp_netlink.c index 4e5c8f3d9940..40b5367f330d 100644 --- a/usr.sbin/arp/arp_netlink.c +++ b/usr.sbin/arp/arp_netlink.c @@ -281,6 +281,7 @@ print_entries_nl(uint32_t ifindex, struct in_addr addr) struct ndmsg *ndmsg = snl_reserve_msg_object(&nw, struct ndmsg); if (ndmsg != NULL) { ndmsg->ndm_family = AF_INET; + /* let kernel filter results by interface if provided */ ndmsg->ndm_ifindex = ifindex; } @@ -296,6 +297,7 @@ print_entries_nl(uint32_t ifindex, struct in_addr addr) while ((hdr = snl_read_reply_multi(&ss_req, nlmsg_seq, &e)) != NULL) { struct snl_parsed_neigh neigh = {}; + struct sockaddr_in *neighaddr; if (!snl_parse_nlmsg(&ss_req, hdr, &snl_rtm_neigh_parser, &neigh)) continue; @@ -307,6 +309,12 @@ print_entries_nl(uint32_t ifindex, struct in_addr addr) continue; } + /* filter results based on host if provided */ + neighaddr = (struct sockaddr_in *)neigh.nda_dst; + if (addr.s_addr && + (addr.s_addr != neighaddr->sin_addr.s_addr)) + continue; + print_entry(&neigh, &link); count++; snl_clear_lb(&ss_req);