From nobody Tue Jul 05 03:56:58 2022 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 0AC371D06335; Tue, 5 Jul 2022 03:56:59 +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 4LcTQ26j61z4vhV; Tue, 5 Jul 2022 03:56:58 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1656993419; 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=LkxrQ+/d1K4hwUpMsERf2BzebY6IMozdfZFVNth+D/I=; b=axJfv5Cyo7XAT/5WdAu+mx53348kVlwaauWN4E7J6UH2CW7+Dfiqjbsu2BdUK4NzmJx3LV A4uW1mEmkwBZbWJeAued1nLm6g54uYaOCgW2I4u6U1FuHF1vIIfSK/9d7khqbJRcVqDBF5 +Q60v3w1XvDjrx110Y1z4w5gFsyp5kxisMhEWEAvtnJ6n6DduoF3zcBZ/FnJOaXnb9lrY8 bUu+3gpfnGBXNHgwYgNuB0b5AFrGi7MfMu0fXLaJ8IGqs49bSohlzQCPl3pusZh6koR1+5 dxu6HEX5V/whLaV4aLkV67Xz8ew+It4loMS/qRPeAlX5J6nZch3j8CgtrXIM/w== 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 C764B1E807; Tue, 5 Jul 2022 03:56:58 +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 2653uwRR062759; Tue, 5 Jul 2022 03:56:58 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 2653uwfF062758; Tue, 5 Jul 2022 03:56:58 GMT (envelope-from git) Date: Tue, 5 Jul 2022 03:56:58 GMT Message-Id: <202207050356.2653uwfF062758@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Gleb Smirnoff Subject: git: d80d73493767 - main - arp(8): use getifaddrs(3) instead of ioctl(SIOCGIFCONF) 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: glebius X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: d80d73493767111b7569e831a93061014c682274 Auto-Submitted: auto-generated ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1656993419; 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=LkxrQ+/d1K4hwUpMsERf2BzebY6IMozdfZFVNth+D/I=; b=TSDX/eVxej1SANeJC89ekabgPxnfAXrEMWq79yp9VPRHec+8sBEsJsU7YojhwfSjfcVjNW 6hFj4TSKSQo/GdHSFcDfPLrsT9nif4Bdos4hf8HfwgIMSLwv1PWwbBQ2EmrTxlnYhoQslc vwyBBYK6xy+K0N2Bak5hzvLQbVPJd9KIe0BAE1YzJteCk5h3yXOccSZnvyHtcr/GZ7JQXv FcQsqPBjDQxGUu+hno/OFgQzt0pwLRZ/v+6o5cSs8iYvvrYMaqBCjkEtSDOI3pHMLNZEcm pAK2eVzjK2BTGhVQ8aVPhXv92EM/0QDQylekxn/N94mNdaq4gHv6FviCEZIJIA== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1656993419; a=rsa-sha256; cv=none; b=CJWhKWN84wu4p3o8HeUicz9IZcq/SKHi4YXvuhR30akeDSDbJU8wMo2iJFv3iseHe/KbAe ohMQYOR1jDt1sCa/r/pr8qODN9DF+HBTnbD8bxz+45sl1Ds9Lz4S+Q+aj5Yt0dRbI5zgST nzD6qJfgTH3bh50TKm7yOCqlbovn773xyttZA8k3G8slnjiUvFQALukh2dNiA6dwS7R4kK 6T++dtTwbeD5Rn5xsZVySCHhTPTt1Zq1S0Zk0k0mtMYbeL8hqLW16cTFTrcDhUd2bVgCQ4 wCZdq+x0UF7MJ2/GSGZFNexPOYSGdhPgPHlb/1tgVZYVGxC+25uPVt86oAHVbA== ARC-Authentication-Results: i=1; mx1.freebsd.org; none X-ThisMailContainsUnwantedMimeParts: N The branch main has been updated by glebius: URL: https://cgit.FreeBSD.org/src/commit/?id=d80d73493767111b7569e831a93061014c682274 commit d80d73493767111b7569e831a93061014c682274 Author: KUROSAWA Takahiro AuthorDate: 2022-07-05 03:56:29 +0000 Commit: Gleb Smirnoff CommitDate: 2022-07-05 03:56:29 +0000 arp(8): use getifaddrs(3) instead of ioctl(SIOCGIFCONF) The original code had used a fixed-size buffer for ioctl(SIOCGIFCONF), that might cause the target ifreq spilled from the buffer. Use the handy getifaddrs(3) to fix the problem. Reviewed by: glebius Differential revision: https://reviews.freebsd.org/D35536 --- usr.sbin/arp/arp.c | 71 ++++++++++++++++++++---------------------------------- 1 file changed, 26 insertions(+), 45 deletions(-) diff --git a/usr.sbin/arp/arp.c b/usr.sbin/arp/arp.c index f018baa2679e..e7c18b0f323f 100644 --- a/usr.sbin/arp/arp.c +++ b/usr.sbin/arp/arp.c @@ -79,6 +79,7 @@ __FBSDID("$FreeBSD$"); #include #include #include +#include #include typedef void (action_fn)(struct sockaddr_dl *sdl, struct sockaddr_in *s_in, @@ -794,90 +795,70 @@ doit: * get_ether_addr - get the hardware address of an interface on the * the same subnet as ipaddr. */ -#define MAX_IFS 32 - static int get_ether_addr(in_addr_t ipaddr, struct ether_addr *hwaddr) { - struct ifreq *ifr, *ifend, *ifp; + struct ifaddrs *ifa, *ifd, *ifas = NULL; in_addr_t ina, mask; struct sockaddr_dl *dla; - struct ifreq ifreq; - struct ifconf ifc; - struct ifreq ifs[MAX_IFS]; - int sock; int retval = 0; - sock = socket(AF_INET, SOCK_DGRAM, 0); - if (sock < 0) - xo_err(1, "socket"); - - ifc.ifc_len = sizeof(ifs); - ifc.ifc_req = ifs; - if (ioctl(sock, SIOCGIFCONF, &ifc) < 0) { - xo_warnx("ioctl(SIOCGIFCONF)"); - goto done; - } - -#define NEXTIFR(i) \ - ((struct ifreq *)((char *)&(i)->ifr_addr \ - + MAX((i)->ifr_addr.sa_len, sizeof((i)->ifr_addr))) ) - /* * Scan through looking for an interface with an Internet * address on the same subnet as `ipaddr'. */ - ifend = (struct ifreq *)(ifc.ifc_buf + ifc.ifc_len); - for (ifr = ifc.ifc_req; ifr < ifend; ifr = NEXTIFR(ifr) ) { - if (ifr->ifr_addr.sa_family != AF_INET) + if (getifaddrs(&ifas) < 0) { + xo_warnx("getifaddrs"); + goto done; + } + + for (ifa = ifas; ifa != NULL; ifa = ifa->ifa_next) { + if (ifa->ifa_addr == NULL || ifa->ifa_netmask == NULL) + continue; + if (ifa->ifa_addr->sa_family != AF_INET) continue; - strncpy(ifreq.ifr_name, ifr->ifr_name, - sizeof(ifreq.ifr_name)); - ifreq.ifr_addr = ifr->ifr_addr; /* * Check that the interface is up, * and not point-to-point or loopback. */ - if (ioctl(sock, SIOCGIFFLAGS, &ifreq) < 0) - continue; - if ((ifreq.ifr_flags & + if ((ifa->ifa_flags & (IFF_UP|IFF_BROADCAST|IFF_POINTOPOINT| IFF_LOOPBACK|IFF_NOARP)) != (IFF_UP|IFF_BROADCAST)) continue; /* Get its netmask and check that it's on the right subnet. */ - if (ioctl(sock, SIOCGIFNETMASK, &ifreq) < 0) - continue; mask = ((struct sockaddr_in *) - &ifreq.ifr_addr)->sin_addr.s_addr; + ifa->ifa_netmask)->sin_addr.s_addr; ina = ((struct sockaddr_in *) - &ifr->ifr_addr)->sin_addr.s_addr; + ifa->ifa_addr)->sin_addr.s_addr; if ((ipaddr & mask) == (ina & mask)) break; /* ok, we got it! */ } - - if (ifr >= ifend) + if (ifa == NULL) goto done; /* * Now scan through again looking for a link-level address * for this interface. */ - ifp = ifr; - for (ifr = ifc.ifc_req; ifr < ifend; ifr = NEXTIFR(ifr)) - if (strcmp(ifp->ifr_name, ifr->ifr_name) == 0 && - ifr->ifr_addr.sa_family == AF_LINK) + for (ifd = ifas; ifd != NULL; ifd = ifd->ifa_next) { + if (ifd->ifa_addr == NULL) + continue; + if (strcmp(ifa->ifa_name, ifd->ifa_name) == 0 && + ifd->ifa_addr->sa_family == AF_LINK) break; - if (ifr >= ifend) + } + if (ifd == NULL) goto done; /* * Found the link-level address - copy it out */ - dla = (struct sockaddr_dl *) &ifr->ifr_addr; + dla = (struct sockaddr_dl *)ifd->ifa_addr; memcpy(hwaddr, LLADDR(dla), dla->sdl_alen); - printf("using interface %s for proxy with address %s\n", ifp->ifr_name, + printf("using interface %s for proxy with address %s\n", ifa->ifa_name, ether_ntoa(hwaddr)); retval = dla->sdl_alen; done: - close(sock); + if (ifas != NULL) + freeifaddrs(ifas); return (retval); }