From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: passt.top; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: passt.top; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=S5L/MYW/; dkim-atps=neutral Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by passt.top (Postfix) with ESMTPS id 03E415A0652 for ; Mon, 15 Dec 2025 02:54:50 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1765763689; 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=3uqzdH2J96F1RIDCoSvSsOC2oozf7l7xl7b0mliiArU=; b=S5L/MYW/eGpbVMK6hq0D2pwcbxtmhL7n/YDEP4HH2V7lJ5JyWOF4+Lu3IRBUklPxcXBNzM 5mM78G/6RbxKiOqpiElCnltRATycVXqmrFogHsmRAx8AR+I9kFb4l1pxR0BMcTjbVh8kF0 OpIedLwcXghcIz/0Qe4A4CDrc2RoIlg= Received: from mx-prod-mc-03.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-660-klXNPzVYNkWFBJjkGkSqBg-1; Sun, 14 Dec 2025 20:54:48 -0500 X-MC-Unique: klXNPzVYNkWFBJjkGkSqBg-1 X-Mimecast-MFC-AGG-ID: klXNPzVYNkWFBJjkGkSqBg_1765763687 Received: from mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.4]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id EBC8919560B2; Mon, 15 Dec 2025 01:54:46 +0000 (UTC) Received: from jmaloy-thinkpadp16vgen1.rmtcaqc.csb (unknown [10.22.88.123]) by mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 896263000218; Mon, 15 Dec 2025 01:54:45 +0000 (UTC) From: Jon Maloy To: sbrivio@redhat.com, dgibson@redhat.com, david@gibson.dropbear.id.au, jmaloy@redhat.com, passt-dev@passt.top Subject: [RFC 02/12] ip: Add ip4_default_prefix_len() helper function for class-based prefix Date: Sun, 14 Dec 2025 20:54:31 -0500 Message-ID: <20251215015441.887736-3-jmaloy@redhat.com> In-Reply-To: <20251215015441.887736-1-jmaloy@redhat.com> References: <20251215015441.887736-1-jmaloy@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.4 X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: lpdUFa1PNtjkca32KoS2ECp1JJYpAoWeTXVPQ3hANLc_1765763687 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: 8bit content-type: text/plain; charset="US-ASCII"; x-default=true Message-ID-Hash: 6DLJOELRXSRVFZJBUH2JUZD3GHB3BG27 X-Message-ID-Hash: 6DLJOELRXSRVFZJBUH2JUZD3GHB3BG27 X-MailFrom: jmaloy@redhat.com X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header X-Mailman-Version: 3.3.8 Precedence: list List-Id: Development discussion and patches for passt Archived-At: Archived-At: List-Archive: List-Archive: List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: We add a helper function to calculate the default IPv4 prefix length based on address class. This is used to replace the current inline calculation in conf_ip4(), and is also a preparation for more uses of this functionality in the coming commits. Signed-off-by: Jon Maloy --- conf.c | 15 +++------------ ip.c | 21 +++++++++++++++++++++ ip.h | 2 ++ 3 files changed, 26 insertions(+), 12 deletions(-) diff --git a/conf.c b/conf.c index 0e96f36..31acc20 100644 --- a/conf.c +++ b/conf.c @@ -733,18 +733,9 @@ static unsigned int conf_ip4(unsigned int ifi, struct ip4_ctx *ip4, } /* Apply default prefix_len to first address if not set */ - if (!ip4->addrs[0].prefix_len) { - in_addr_t a = ntohl(ip4->addrs[0].addr.s_addr); - - if (IN_CLASSA(a)) - ip4->addrs[0].prefix_len = 8; - else if (IN_CLASSB(a)) - ip4->addrs[0].prefix_len = 16; - else if (IN_CLASSC(a)) - ip4->addrs[0].prefix_len = 24; - else - ip4->addrs[0].prefix_len = 32; - } + if (!ip4->addrs[0].prefix_len) + ip4->addrs[0].prefix_len = + ip4_default_prefix_len(&ip4->addrs[0].addr); ip4->addr_seen = ip4->addrs[0].addr; diff --git a/ip.c b/ip.c index 9a7f4c5..2519c71 100644 --- a/ip.c +++ b/ip.c @@ -13,6 +13,8 @@ */ #include +#include + #include "util.h" #include "ip.h" @@ -67,3 +69,22 @@ found: *proto = nh; return true; } + +/** + * ip4_default_prefix_len() - Get default prefix length for IPv4 address + * @addr: IPv4 address + * + * Return: prefix length based on address class (8/16/24), or 32 for other + */ +int ip4_default_prefix_len(const struct in_addr *addr) +{ + in_addr_t a = ntohl(addr->s_addr); + + if (IN_CLASSA(a)) + return 8; + if (IN_CLASSB(a)) + return 16; + if (IN_CLASSC(a)) + return 24; + return 32; +} diff --git a/ip.h b/ip.h index 748cb1f..065b78b 100644 --- a/ip.h +++ b/ip.h @@ -139,6 +139,8 @@ static const struct in_addr in4addr_broadcast = { 0xffffffff }; #define IP4_MAX_ADDRS 8 #define IP6_MAX_ADDRS 16 +int ip4_default_prefix_len(const struct in_addr *addr); + /** * struct ip4_addr_entry - IPv4 address with prefix length * @addr: IPv4 address -- 2.51.1