From patchwork Thu Feb 15 18:34:00 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Serhey Popovych X-Patchwork-Id: 874043 X-Patchwork-Delegate: dsahern@gmail.com Return-Path: X-Original-To: patchwork-incoming@ozlabs.org Delivered-To: patchwork-incoming@ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=vger.kernel.org (client-ip=209.132.180.67; helo=vger.kernel.org; envelope-from=netdev-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="nWuFyZc0"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 3zj4gL5fZLz9t5R for ; Fri, 16 Feb 2018 05:34:22 +1100 (AEDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1166631AbeBOSeT (ORCPT ); Thu, 15 Feb 2018 13:34:19 -0500 Received: from mail-lf0-f67.google.com ([209.85.215.67]:37781 "EHLO mail-lf0-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1166573AbeBOSeQ (ORCPT ); Thu, 15 Feb 2018 13:34:16 -0500 Received: by mail-lf0-f67.google.com with SMTP id f137so857495lfe.4 for ; Thu, 15 Feb 2018 10:34:15 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=bQjabgc7kctEWzZsO0ACgyvlIB032ljVW3jxbLVdVdI=; b=nWuFyZc0OglSGgNx/VUy5pPYSl0v+qtcb22wfO6S8jXQ6BtSih5SLPv5BTawVilptQ SoRIpmQCMhmo4MZ19psKb1Vd2AoriKNFwjFd3HsEVVPClEzgz09sol5UcLNVMjNkHU1j SE+6CpX9BQ4Bin852RtybsfUjOgaC/e05r3AkNcmfJ90sNsAa2Q9Q27y+ychxZMOL6UP hNidDjgy0VLBuNwPS/z5QA9zA2SwMTTBNUXdGjXtDmTKORvydf7YDARE5C2JWONazsRU LZn9WmL+u/nxy3CPlJTGGNr4awGA6L+uXFM9MknDRDqCf/YAE3JffJ5juQMRwCYBRSO4 bTtA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=bQjabgc7kctEWzZsO0ACgyvlIB032ljVW3jxbLVdVdI=; b=f4M3jJRQPrYf1MpWRk5ovIgz257enrQ98E1A4xOC5OLD2uFHIOVbUVFzuNSZ4n5MeV 8ObDr9pLgYGolwKejkclQTbdmhFdjZkebZDluo0UR/VwoNa7f9uuu/X8jvGR2MddldJw QXMhL74n9bXSzOdX0Dg3Nj8C0Ont38UDFFmb1LwQVT/SnT0ZGktZ7ryFbMnrQ2fEk2Sb UIS2XheBEhPwkxkJ1OCuSMo716N83Yo6WfU7CLFezTGZMm7wyY4Ht0e/PJBz5Vz86DTd nbYFHe6dzzcB9tkQDlfpaDOfUbJMlcH2aon7qRA4GJadm9Igr2nYboq9y0cFfTvRjh+9 JYrg== X-Gm-Message-State: APf1xPBVaB4NAtwJICwcwbMBoF89nrhFF+nfKzSHveN/owrQNKuGL/zU TkK6K8E8JpYQjEJvNAh0BW6T1w== X-Google-Smtp-Source: AH8x225yFoKw9+ADdYXFAjiIQTLuai1BKArpum3mzvuQuze37cnGSGPoZ2egVDyrTWj8y+J5XX6O+w== X-Received: by 10.25.141.212 with SMTP id p203mr2402979lfd.24.1518719655005; Thu, 15 Feb 2018 10:34:15 -0800 (PST) Received: from tuxracer.localdomain ([2a01:6d80::195:20:96:53]) by smtp.gmail.com with ESMTPSA id x66sm251119lff.80.2018.02.15.10.34.13 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 15 Feb 2018 10:34:14 -0800 (PST) From: Serhey Popovych To: netdev@vger.kernel.org Cc: dsahern@gmail.com Subject: [PATCH iproute2-next v4 1/9] ipaddress: Abstract IFA_LABEL matching code Date: Thu, 15 Feb 2018 20:34:00 +0200 Message-Id: <1518719648-30134-2-git-send-email-serhe.popovych@gmail.com> X-Mailer: git-send-email 1.7.10.4 In-Reply-To: <1518719648-30134-1-git-send-email-serhe.popovych@gmail.com> References: <1518719648-30134-1-git-send-email-serhe.popovych@gmail.com> Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org There at least two places in ip/ipaddress.c where we match IFA_LABEL against filter.label if that is given. Get rid of "common" if () statement for inet_addr_match_rta() and ifa_label_match_rta(): it is not common because first will check for filter.pfx.family != AF_UNSPEC inside and second for filter.label being non NULL. This allows us to further simplify down code and prepare for ll_idx_n2a() replacement with ll_index_to_name() without 80 columns checkpatch notice. Signed-off-by: Serhey Popovych --- ip/ipaddress.c | 57 +++++++++++++++++++++++++++----------------------------- 1 file changed, 27 insertions(+), 30 deletions(-) diff --git a/ip/ipaddress.c b/ip/ipaddress.c index 6990b81..ad69d09 100644 --- a/ip/ipaddress.c +++ b/ip/ipaddress.c @@ -1305,6 +1305,22 @@ static int get_filter(const char *arg) return 0; } +static int ifa_label_match_rta(int ifindex, const struct rtattr *rta) +{ + const char *label; + SPRINT_BUF(b1); + + if (!filter.label) + return 0; + + if (rta) + label = RTA_DATA(rta); + else + label = ll_idx_n2a(ifindex, b1); + + return fnmatch(filter.label, label, 0); +} + int print_addrinfo(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg) { @@ -1343,21 +1359,13 @@ int print_addrinfo(const struct sockaddr_nl *who, struct nlmsghdr *n, return 0; if ((filter.flags ^ ifa_flags) & filter.flagmask) return 0; - if (filter.label) { - SPRINT_BUF(b1); - const char *label; - - if (rta_tb[IFA_LABEL]) - label = RTA_DATA(rta_tb[IFA_LABEL]); - else - label = ll_idx_n2a(ifa->ifa_index, b1); - if (fnmatch(filter.label, label, 0) != 0) - return 0; - } if (filter.family && filter.family != ifa->ifa_family) return 0; + if (ifa_label_match_rta(ifa->ifa_index, rta_tb[IFA_LABEL])) + return 0; + if (inet_addr_match_rta(&filter.pfx, rta_tb[IFA_LOCAL])) return 0; @@ -1713,25 +1721,14 @@ static void ipaddr_filter(struct nlmsg_chain *linfo, struct nlmsg_chain *ainfo) if ((filter.flags ^ ifa_flags) & filter.flagmask) continue; - if (filter.pfx.family || filter.label) { - struct rtattr *rta = - tb[IFA_LOCAL] ? : tb[IFA_ADDRESS]; - - if (inet_addr_match_rta(&filter.pfx, rta)) - continue; - - if (filter.label) { - SPRINT_BUF(b1); - const char *label; - - if (tb[IFA_LABEL]) - label = RTA_DATA(tb[IFA_LABEL]); - else - label = ll_idx_n2a(ifa->ifa_index, b1); - if (fnmatch(filter.label, label, 0) != 0) - continue; - } - } + + if (ifa_label_match_rta(ifa->ifa_index, tb[IFA_LABEL])) + continue; + + if (!tb[IFA_LOCAL]) + tb[IFA_LOCAL] = tb[IFA_ADDRESS]; + if (inet_addr_match_rta(&filter.pfx, tb[IFA_LOCAL])) + continue; ok = 1; break;