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=GAw3DFEx; 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 C63855A061D for ; Fri, 06 Mar 2026 21:09:58 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1772827797; 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=twi+bgo7xdtzaSGvM699FYEyEZSKnCXZBjBiBV3MlcA=; b=GAw3DFExo+UGIN+TthLUWJ084BsLIMhPMqcUbnO1rhHEBTXOai566fGiqdduASjqYs4Mo+ /4fnwJxOnn65ObLU0GLIfNM0b43uxeCgbXadelj+tG5HUAbq5FrBvRa6BGAK8An4Fh1n2N NstBhsr/ryAGCHRc1w3yiTx7i0BjuY8= 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-556-KH0BBn4HOmqzMzWencL5ag-1; Fri, 06 Mar 2026 15:09:56 -0500 X-MC-Unique: KH0BBn4HOmqzMzWencL5ag-1 X-Mimecast-MFC-AGG-ID: KH0BBn4HOmqzMzWencL5ag_1772827795 Received: from mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.93]) (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 E54DC1956048; Fri, 6 Mar 2026 20:09:54 +0000 (UTC) Received: from jmaloy-thinkpadp16vgen1.rmtcaqc.csb (unknown [10.22.81.106]) by mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id E3B201800761; Fri, 6 Mar 2026 20:09:52 +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 v2] netlink: Return prefix length for IPv6 addresses in nl_addr_get() Date: Fri, 6 Mar 2026 15:09:52 -0500 Message-ID: <20260306200952.1440552-1-jmaloy@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.93 X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: alQJ8seVYqhyI7Q0-lF58GUXoAQkzot8yZ7I3LEAcSE_1772827795 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: 8bit content-type: text/plain; charset="US-ASCII"; x-default=true Message-ID-Hash: 3U2WFF6JVATSZXWSLEO4NOBKLV363MUE X-Message-ID-Hash: 3U2WFF6JVATSZXWSLEO4NOBKLV363MUE 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: nl_addr_get() was not setting the prefix_len output parameter for IPv6 addresses, only for IPv4. This meant callers always got 0 for IPv6, forcing them to use a hardcoded default (64). Fix by assigning *prefix_len even in the IPv6 case. We also add another functional change. We now check for if an AF_INET address is link local, in which case we have to skip it. Although it is conventional to set the scope of such addresses to RT_SCOPE_HOST, this is not stated in any RFC, and we cannot trust it to have been set correctly by the user, just as we cannot trust it to have been set correctly for any other AF_INET address. We therefore add this check explicitly on the address itself. Signed-off-by: Jon Maloy --- v2: - Simplified according to feedback from S. Brivio - Added test fo AF_INET link local property --- netlink.c | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/netlink.c b/netlink.c index 82a2f0c..f0e8fa7 100644 --- a/netlink.c +++ b/netlink.c @@ -752,7 +752,7 @@ int nl_addr_set_ll_nodad(int s, unsigned int ifi) * @ifi: Interface index in outer network namespace * @af: Address family * @addr: Global address to fill - * @prefix_len: Mask or prefix length, to fill (for IPv4) + * @prefix_len: Mask or prefix length, to fill * @addr_l: Link-scoped address to fill (for IPv6) * * Return: 0 on success, negative error code on failure @@ -777,6 +777,7 @@ int nl_addr_get(int s, unsigned int ifi, sa_family_t af, nl_foreach_oftype(nh, status, s, buf, seq, RTM_NEWADDR) { struct ifaddrmsg *ifa = (struct ifaddrmsg *)NLMSG_DATA(nh); struct rtattr *rta; + bool link_local; size_t na; if (ifa->ifa_index != ifi || ifa->ifa_flags & IFA_F_DEPRECATED) @@ -788,18 +789,14 @@ int nl_addr_get(int s, unsigned int ifi, sa_family_t af, (af == AF_INET6 && rta->rta_type != IFA_ADDRESS)) continue; - if (af == AF_INET && ifa->ifa_prefixlen > prefix_max) { - memcpy(addr, RTA_DATA(rta), RTA_PAYLOAD(rta)); + link_local = (af == AF_INET6) ? + ifa->ifa_scope >= RT_SCOPE_LINK : + IN4_IS_ADDR_LINKLOCAL(RTA_DATA(rta)); - prefix_max = *prefix_len = ifa->ifa_prefixlen; - } else if (af == AF_INET6 && addr && - ifa->ifa_scope < RT_SCOPE_LINK && - ifa->ifa_prefixlen > prefix_max) { + if (ifa->ifa_prefixlen > prefix_max && !link_local) { memcpy(addr, RTA_DATA(rta), RTA_PAYLOAD(rta)); - - prefix_max = ifa->ifa_prefixlen; + prefix_max = *prefix_len = ifa->ifa_prefixlen; } - if (addr_l && af == AF_INET6 && ifa->ifa_scope == RT_SCOPE_LINK && ifa->ifa_prefixlen > prefix_max_ll) { -- 2.52.0