From nobody Thu Sep 14 10:14:24 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 4RmY8J51dyz4tPnk; Thu, 14 Sep 2023 10:14:24 +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 4RmY8J4c15z4mtd; Thu, 14 Sep 2023 10:14:24 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1694686464; 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=TY6PxVNoLXLl9L4f/iGuPvsc44hODobi1l+sMt0W0kw=; b=yg9emgu2woAuFnNChM9yLsi5d0uyPNG6Nd6c7JHoKooiHKiPKEc9S03cv/7ABLyxx2qgIF JLDdOieEtAoWTRXd0ckiG3NmNsBSEPsM2mUe2HHMcZl1B7HrxEo2ipq+yb4jKAItAwvnac 8sjVx/YD9kcCI228AwkUBfJW8yAHjFDVdzkK/yfqEymnn9CgSeD6+MLMPGuNTfChPVrsmr h8t8d+kwEAprP4a1vMETWu/hzAWO2ujdPkB1wf4aB7LkrRiFI+JP2rauQKidD9y3Oius2w MK1B54ROR5wHZZPz2duApHbisSWzilOA6b0/85FOD707l1U05/466WqnuhNEAg== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1694686464; a=rsa-sha256; cv=none; b=OmSjyiakW822urNO6KGWwceuvf03Z1izP9kIA72zdrTNm81r+6PPm2fDMRHiLRywigrzYx 7eQHURmVAhcnz+ubBfU3BA1u5QDAq7lGl+0fH0h/ZrW4v+Kzb9fPwYo5Bxi4ph5shlyjQX J14LTIWm8XVLpwL/ZTMB5CaBXRDj0KeCnj58/AlgD0BPJNj4ZNE8riDHer3hxpvmUUJOSh Ypwl1EUHEYQT044pX6bUWLfnQEBNkEDNHm9kedapdkIqG5ohlbcIf7qBSLAtDSC7OQaR27 U3e+52NgYY/O8CQFZ7lrtD6uwqkdbZErjRoG46K+jVAbkc+WX5wUE87Sceq9yg== 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=1694686464; 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=TY6PxVNoLXLl9L4f/iGuPvsc44hODobi1l+sMt0W0kw=; b=HnmacDYNpSMiy2A4J3m3KiAe3VPd+Y7DXH17bchAehqzqYsVy8ol/d/T9FTmYNsG2bobnm 7dUT63bcUPqKvWbe+DKNz/SXswISWUozGd0P04m+xAthY609k0pXmCBcnnYu92piQA/ivA r2xV5M3URlQBXw9VPQEjwlrMi6DsFZ9L2DKAFrTrqTSD45LIns05ybVd1ECE30cbtWCZY6 q4Xhww+SbEDKzPK2ag0Yrd+0jWeyOCcmy9u0VwtWY6e/uv8ZxbXYYG8hPS+4+v+GTrwRz2 mRNk8N5xKjm4H2DFjuGpmBhaRIKV4ohyNgagmE9B+PeRxDz8fd/NKA84+HTXqA== 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 4RmY8J3g9dzsgn; Thu, 14 Sep 2023 10:14:24 +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 38EAEOpj011003; Thu, 14 Sep 2023 10:14:24 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.17.1/8.17.1/Submit) id 38EAEOjG011000; Thu, 14 Sep 2023 10:14:24 GMT (envelope-from git) Date: Thu, 14 Sep 2023 10:14:24 GMT Message-Id: <202309141014.38EAEOjG011000@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Kristof Provost Subject: git: 79278872ad96 - main - 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: kp X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 79278872ad966e5f54805efbeb692c8cbc0306c8 Auto-Submitted: auto-generated The branch main has been updated by kp: URL: https://cgit.FreeBSD.org/src/commit/?id=79278872ad966e5f54805efbeb692c8cbc0306c8 commit 79278872ad966e5f54805efbeb692c8cbc0306c8 Author: R. Christian McDonald AuthorDate: 2023-09-14 07:07:24 +0000 Commit: Kristof Provost CommitDate: 2023-09-14 08:14:10 +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") --- 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);