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=aSOGMU2P; 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 ED47D5A0623 for ; Fri, 30 Jan 2026 22:44:59 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1769809498; 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=n2sCxf+nadX0EPAP0x5IIwSFdF9BKAlynqu17MEDfWM=; b=aSOGMU2P+zgAtrBI5jlsA8E2hbJHFQ6W/b41rCcORtVcHtMiLUu8Ua0+uM+zD61gI12Ozs uGkG480XFqxPJN31jDURZSLsMDbhRfkEIEXFFJVoVUKhmxRMV/pJnuASEkDY2U8BSVxYAr 1/pmMIXqZ3bdHoOxYoSFbmygzzS9PD8= Received: from mx-prod-mc-06.mail-002.prod.us-west-2.aws.redhat.com (ec2-35-165-154-97.us-west-2.compute.amazonaws.com [35.165.154.97]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-474-OCY-WSpLNyat3ctAi3dIjg-1; Fri, 30 Jan 2026 16:44:55 -0500 X-MC-Unique: OCY-WSpLNyat3ctAi3dIjg-1 X-Mimecast-MFC-AGG-ID: OCY-WSpLNyat3ctAi3dIjg_1769809494 Received: from mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.111]) (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-06.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 356D918005AD; Fri, 30 Jan 2026 21:44:54 +0000 (UTC) Received: from jmaloy-thinkpadp16vgen1.rmtcaqc.csb (unknown [10.22.65.201]) by mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 29A28180097D; Fri, 30 Jan 2026 21:44:53 +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: [PATCH v3 02/11] ip: Add IN4_MASK() macro for IPv4 netmask calculation Date: Fri, 30 Jan 2026 16:44:38 -0500 Message-ID: <20260130214447.2540791-3-jmaloy@redhat.com> In-Reply-To: <20260130214447.2540791-1-jmaloy@redhat.com> References: <20260130214447.2540791-1-jmaloy@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.111 X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: tzrWDmMUZw3AZM_9HwhbpYmhCkpU7F9fythdX6J2GXM_1769809494 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: 8bit content-type: text/plain; charset="US-ASCII"; x-default=true Message-ID-Hash: HE6VECAACCJSBHVTYKSOPZ7WJJICIQYW X-Message-ID-Hash: HE6VECAACCJSBHVTYKSOPZ7WJJICIQYW 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: Add a convenience macro to compute an IPv4 netmask from a prefix length. This simplifies netmask calculations throughout the codebase. Signed-off-by: Jon Maloy --- conf.c | 2 +- dhcp.c | 2 +- ip.h | 2 ++ 3 files changed, 4 insertions(+), 2 deletions(-) diff --git a/conf.c b/conf.c index 98d5d17..5188c02 100644 --- a/conf.c +++ b/conf.c @@ -1147,7 +1147,7 @@ static void conf_print(const struct ctx *c) if (!c->no_dhcp) { uint32_t mask; - mask = htonl(0xffffffff << (32 - c->ip4.prefix_len)); + mask = IN4_MASK(c->ip4.prefix_len); info("DHCP:"); info(" assign: %s", diff --git a/dhcp.c b/dhcp.c index 6b9c2e3..c552f01 100644 --- a/dhcp.c +++ b/dhcp.c @@ -404,7 +404,7 @@ int dhcp(const struct ctx *c, struct iov_tail *data) info(" from %s", eth_ntop(m->chaddr, macstr, sizeof(macstr))); - mask.s_addr = htonl(0xffffffff << (32 - c->ip4.prefix_len)); + mask.s_addr = IN4_MASK(c->ip4.prefix_len); memcpy(opts[1].s, &mask, sizeof(mask)); memcpy(opts[3].s, &c->ip4.guest_gw, sizeof(c->ip4.guest_gw)); memcpy(opts[54].s, &c->ip4.our_tap_addr, sizeof(c->ip4.our_tap_addr)); diff --git a/ip.h b/ip.h index bd28640..c829d84 100644 --- a/ip.h +++ b/ip.h @@ -17,6 +17,8 @@ (ntohl(((struct in_addr *)(a))->s_addr) >> IN_CLASSA_NSHIFT == IN_LOOPBACKNET) #define IN4_IS_ADDR_MULTICAST(a) \ (IN_MULTICAST(ntohl(((struct in_addr *)(a))->s_addr))) +#define IN4_MASK(prefix_len) \ + (htonl(0xffffffff << (128 - (prefix_len)))) #define IN4_ARE_ADDR_EQUAL(a, b) \ (((struct in_addr *)(a))->s_addr == ((struct in_addr *)b)->s_addr) #define IN4ADDR_LOOPBACK_INIT \ -- 2.52.0