From patchwork Wed Jun 24 16:12:08 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mark Michelson X-Patchwork-Id: 1316380 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=openvswitch.org (client-ip=140.211.166.136; helo=silver.osuosl.org; envelope-from=ovs-dev-bounces@openvswitch.org; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=redhat.com Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=hL8c/Zx9; dkim-atps=neutral Received: from silver.osuosl.org (smtp3.osuosl.org [140.211.166.136]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 49sSpx48vVz9s1x for ; Thu, 25 Jun 2020 02:12:41 +1000 (AEST) Received: from localhost (localhost [127.0.0.1]) by silver.osuosl.org (Postfix) with ESMTP id 1595020506; Wed, 24 Jun 2020 16:12:39 +0000 (UTC) X-Virus-Scanned: amavisd-new at osuosl.org Received: from silver.osuosl.org ([127.0.0.1]) by localhost (.osuosl.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id VbmQ2GWjdLv4; Wed, 24 Jun 2020 16:12:37 +0000 (UTC) Received: from lists.linuxfoundation.org (lf-lists.osuosl.org [140.211.9.56]) by silver.osuosl.org (Postfix) with ESMTP id 54F9B20446; Wed, 24 Jun 2020 16:12:29 +0000 (UTC) Received: from lf-lists.osuosl.org (localhost [127.0.0.1]) by lists.linuxfoundation.org (Postfix) with ESMTP id B61CEC016F; Wed, 24 Jun 2020 16:12:29 +0000 (UTC) X-Original-To: dev@openvswitch.org Delivered-To: ovs-dev@lists.linuxfoundation.org Received: from silver.osuosl.org (smtp3.osuosl.org [140.211.166.136]) by lists.linuxfoundation.org (Postfix) with ESMTP id C3AA3C016F for ; Wed, 24 Jun 2020 16:12:26 +0000 (UTC) Received: from localhost (localhost [127.0.0.1]) by silver.osuosl.org (Postfix) with ESMTP id 95047203DB for ; Wed, 24 Jun 2020 16:12:26 +0000 (UTC) X-Virus-Scanned: amavisd-new at osuosl.org Received: from silver.osuosl.org ([127.0.0.1]) by localhost (.osuosl.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 0eoZUFvfQqge for ; Wed, 24 Jun 2020 16:12:21 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from us-smtp-1.mimecast.com (us-smtp-delivery-1.mimecast.com [207.211.31.120]) by silver.osuosl.org (Postfix) with ESMTPS id 318031FE49 for ; Wed, 24 Jun 2020 16:12:21 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1593015140; 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: in-reply-to:in-reply-to:references:references; bh=FfC1Wi+j/2oi98YBnjkDdbvufZSzM19yrquyLq513gI=; b=hL8c/Zx9u7fAq08GTw/iLgjeClmVjqMT+DdrkRIxt9U2XRNfBGBnfHZ5wGrdFHIRFn1G5j a/9E2KssH/LRPKevfO5xGE6yNkQIztIvJqYjJZNxbjD8lkhIiaox9Xh9cD4gyXBbgVyZ7z IpKEGo+xbTA5dHsGpdT7P0SXnlx4GEU= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-367-qlEVp5DeNluCdRd2BSaaqw-1; Wed, 24 Jun 2020 12:12:18 -0400 X-MC-Unique: qlEVp5DeNluCdRd2BSaaqw-1 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 5A6E4107ACF2 for ; Wed, 24 Jun 2020 16:12:17 +0000 (UTC) Received: from monae.redhat.com (ovpn-117-53.rdu2.redhat.com [10.10.117.53]) by smtp.corp.redhat.com (Postfix) with ESMTP id 98B7E7CCD1 for ; Wed, 24 Jun 2020 16:12:16 +0000 (UTC) From: Mark Michelson To: dev@openvswitch.org Date: Wed, 24 Jun 2020 12:12:08 -0400 Message-Id: <20200624161211.1559878-3-mmichels@redhat.com> In-Reply-To: <20200624161211.1559878-1-mmichels@redhat.com> References: <20200624161211.1559878-1-mmichels@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Subject: [ovs-dev] [PATCH v3 ovn 3/6] Add more IP address normalization functions. X-BeenThere: ovs-dev@openvswitch.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: ovs-dev-bounces@openvswitch.org Sender: "dev" Normalization of IP addresses helps to fix common pitfalls when comparing addresses: * Case-sensitivity in IPv6 addresses * Different valid represntations of netmasks This commit adds new normalization functions and cleans up some of the existing ones. The new functions are not used in this commit but will be used in later commits in this series. Signed-off-by: Mark Michelson --- utilities/ovn-nbctl.c | 81 ++++++++++++++++++++++++++++++++++++------- 1 file changed, 69 insertions(+), 12 deletions(-) diff --git a/utilities/ovn-nbctl.c b/utilities/ovn-nbctl.c index 708ff2e5b..2e5253d4e 100644 --- a/utilities/ovn-nbctl.c +++ b/utilities/ovn-nbctl.c @@ -3510,28 +3510,85 @@ normalize_ipv6_prefix(struct in6_addr ipv6, unsigned int plen) } } -/* The caller must free the returned string. */ static char * -normalize_prefix_str(const char *orig_prefix) +normalize_ipv4_prefix_str(const char *orig_prefix) { unsigned int plen; ovs_be32 ipv4; char *error; error = ip_parse_cidr(orig_prefix, &ipv4, &plen); - if (!error) { - return normalize_ipv4_prefix(ipv4, plen); - } else { - struct in6_addr ipv6; + if (error) { free(error); + return NULL; + } + return normalize_ipv4_prefix(ipv4, plen); +} - error = ipv6_parse_cidr(orig_prefix, &ipv6, &plen); - if (error) { - free(error); - return NULL; - } - return normalize_ipv6_prefix(ipv6, plen); +static char * +normalize_ipv6_prefix_str(const char *orig_prefix) +{ + unsigned int plen; + struct in6_addr ipv6; + char *error; + + error = ipv6_parse_cidr(orig_prefix, &ipv6, &plen); + if (error) { + free(error); + return NULL; + } + return normalize_ipv6_prefix(ipv6, plen); +} + +/* The caller must free the returned string. */ +static char * +normalize_prefix_str(const char *orig_prefix) +{ + char *ret; + + ret = normalize_ipv4_prefix_str(orig_prefix); + if (!ret) { + ret = normalize_ipv6_prefix_str(orig_prefix); + } + return ret; +} + +static char * +normalize_ipv4_addr_str(const char *orig_addr) +{ + ovs_be32 ipv4; + + if (!ip_parse(orig_addr, &ipv4)) { + return NULL; } + + return normalize_ipv4_prefix(ipv4, 32); +} + +static char * +normalize_ipv6_addr_str(const char *orig_addr) +{ + struct in6_addr ipv6; + + if (!ipv6_parse(orig_addr, &ipv6)) { + return NULL; + } + + return normalize_ipv6_prefix(ipv6, 128); +} + +/* Similar to normalize_prefix_str but must be an un-masked address. + * The caller must free the returned string. */ +OVS_UNUSED static char * +normalize_addr_str(const char *orig_addr) +{ + char *ret; + + ret = normalize_ipv4_addr_str(orig_addr); + if (!ret) { + ret = normalize_ipv6_addr_str(orig_addr); + } + return ret; } static void