git: e52655a93909 - main - netinet: Promote IFP_TO_IA() from macro to function in_ifprimaryaddr().

Bruce M Simpson <[email protected]>
Newsgroups gmane.os.freebsd.devel.cvs.src
Message-ID <6a67d3ce.23104.20182398__49687.3561105289$1785189342$gmane$org@gitrepo.freebsd.org>
The branch main has been updated by bms:

URL: https://cgit.FreeBSD.org/src/commit/?id=e52655a939099402bab7e603ced58b17af106e23

commit e52655a939099402bab7e603ced58b17af106e23
Author:     Bruce M Simpson <[email protected]>
AuthorDate: 2026-02-18 08:29:58 +0000
Commit:     Bruce M Simpson <[email protected]>
CommitDate: 2026-07-27 21:55:12 +0000

    netinet: Promote IFP_TO_IA() from macro to function in_ifprimaryaddr().
    
    in_ifprimaryaddr() exists only to support IPv4 multicast usage. Since the
    adoption of epoch tracking, ifa_ref() is no longer required in its body;
    that was originally introduced by rwatson in 2009.
    
    We could not use __deprecated1() from <sys/cdefs.h> anyway, as
    IFP_TO_IA() is a macro, not a function.
    
    Approved by:    glebius (2026-02-26)
    Reviewed by:    adrian, glebius, pouria
    Differential Revision:  D55344
---
 sys/netinet/in.c     | 22 ++++++++++++++++++++++
 sys/netinet/in.h     |  1 +
 sys/netinet/in_var.h |  2 +-
 3 files changed, 24 insertions(+), 1 deletion(-)

diff --git a/sys/netinet/in.c b/sys/netinet/in.c
index 510cfa79d54b..508bc43bc63c 100644
--- a/sys/netinet/in.c
+++ b/sys/netinet/in.c
@@ -206,6 +206,28 @@ in_ifhasaddr(struct ifnet *ifp, struct in_addr in)
 	return (false);
 }
 
+/*
+ * Return the first configured IPv4 address on the given ifnet.
+ * Replaces deprecated IFP_TO_IA() from 4.3BSD.
+ * Used only by IPv4 multicast, where source-address selection
+ * has not yet been introduced.
+ *
+ * Returns ifa or NULL.
+ */
+struct in_ifaddr *
+in_ifprimaryaddr(struct ifnet *ifp)
+{
+	struct ifaddr *ifa;
+
+	NET_EPOCH_ASSERT();
+
+	CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
+		if (ifa->ifa_addr->sa_family == AF_INET)
+			break;
+
+	return ((struct in_ifaddr *)ifa);
+}
+
 /*
  * Return a reference to the interface address which is different to
  * the supplied one but with same IP address value.
diff --git a/sys/netinet/in.h b/sys/netinet/in.h
index 94f4bb682489..c4698b7056eb 100644
--- a/sys/netinet/in.h
+++ b/sys/netinet/in.h
@@ -677,6 +677,7 @@ bool	 in_localip(struct in_addr);
 bool	 in_localip_fib(struct in_addr, uint16_t);
 bool	 in_ifhasaddr(struct ifnet *, struct in_addr);
 struct in_ifaddr *in_findlocal(uint32_t, bool);
+struct in_ifaddr *in_ifprimaryaddr(struct ifnet *);
 int	 inet_aton(const char *, struct in_addr *); /* in libkern */
 char	*inet_ntoa_r(struct in_addr ina, char *buf); /* in libkern */
 char	*inet_ntop(int, const void *, char *, socklen_t); /* in libkern */
diff --git a/sys/netinet/in_var.h b/sys/netinet/in_var.h
index 6d843cf2f6ef..191f3a54dbe5 100644
--- a/sys/netinet/in_var.h
+++ b/sys/netinet/in_var.h
@@ -153,7 +153,7 @@ do {									\
 
 /*
  * Macro for finding the internet address structure (in_ifaddr) corresponding
- * to a given interface (ifnet structure).
+ * to a given interface (ifnet structure). DEPRECATED.
  */
 #define IFP_TO_IA(ifp, ia)						\
 	/* struct ifnet *ifp; */					\
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.