git: faa8d81cf7a2 - stable/12 - add SIOCGIFDATA ioctl

From: Ed Maste <emaste_at_FreeBSD.org>
Date: Mon, 13 Dec 2021 00:29:11 UTC
The branch stable/12 has been updated by emaste:

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

commit faa8d81cf7a25db3b7ea45acbef0cb28d80eefe0
Author:     Ed Maste <emaste@FreeBSD.org>
AuthorDate: 2020-09-28 16:54:39 +0000
Commit:     Ed Maste <emaste@FreeBSD.org>
CommitDate: 2021-12-12 22:42:08 +0000

    add SIOCGIFDATA ioctl
    
    For interfaces that do not support SIOCGIFMEDIA (for which there are
    quite a few) the only fallback is to query the interface for
    if_data->ifi_link_state.  While it's possible to get at if_data for an
    interface via getifaddrs(3) or sysctl, both are heavy weight mechanisms.
    
    SIOCGIFDATA is a simple ioctl to retrieve this fast with very little
    resource use in comparison.  This implementation mirrors that of other
    similar ioctls in FreeBSD.
    
    Submitted by:   Roy Marples <roy@marples.name>
    Reviewed by:    markj
    MFC after:      1 month
    Differential Revision:  https://reviews.freebsd.org/D26538
    
    (cherry picked from commit c1aedfcbd9896401f637bc815ba4e51dca107f6f)
---
 share/man/man9/ifnet.9 |  5 +++--
 sys/net/if.c           | 12 ++++++++++++
 sys/sys/sockio.h       |  1 +
 3 files changed, 16 insertions(+), 2 deletions(-)

diff --git a/share/man/man9/ifnet.9 b/share/man/man9/ifnet.9
index ebe202079588..eb12a91eb603 100644
--- a/share/man/man9/ifnet.9
+++ b/share/man/man9/ifnet.9
@@ -28,7 +28,7 @@
 .\"
 .\" $FreeBSD$
 .\"
-.Dd November 14, 2018
+.Dd September 28, 2020
 .Dt IFNET 9
 .Os
 .Sh NAME
@@ -1276,12 +1276,13 @@ list.
 Caller must have appropriate privilege.
 (No call-down to driver.)
 .It Dv SIOCGIFCAP
+.It Dv SIOCGIFDATA
 .It Dv SIOCGIFFIB
 .It Dv SIOCGIFFLAGS
 .It Dv SIOCGIFMETRIC
 .It Dv SIOCGIFMTU
 .It Dv SIOCGIFPHYS
-Get interface capabilities, FIB, flags, metric, MTU, medium selection.
+Get interface capabilities, data, FIB, flags, metric, MTU, medium selection.
 (No call-down to driver.)
 .Pp
 .It Dv SIOCSIFCAP
diff --git a/sys/net/if.c b/sys/net/if.c
index f2ef88d3f28e..d93d63f82f23 100644
--- a/sys/net/if.c
+++ b/sys/net/if.c
@@ -2633,6 +2633,18 @@ ifhwioctl(u_long cmd, struct ifnet *ifp, caddr_t data, struct thread *td)
 		ifr->ifr_curcap = ifp->if_capenable;
 		break;
 
+	case SIOCGIFDATA:
+	{
+		struct if_data ifd;
+
+		/* Ensure uninitialised padding is not leaked. */
+		memset(&ifd, 0, sizeof(ifd));
+
+		if_data_copy(ifp, &ifd);
+		error = copyout(&ifd, ifr_data_get_ptr(ifr), sizeof(ifd));
+		break;
+	}
+
 #ifdef MAC
 	case SIOCGIFMAC:
 		error = mac_ifnet_ioctl_get(td->td_ucred, ifr, ifp);
diff --git a/sys/sys/sockio.h b/sys/sys/sockio.h
index 2e5a56200e1b..93b8af28e171 100644
--- a/sys/sys/sockio.h
+++ b/sys/sys/sockio.h
@@ -83,6 +83,7 @@
 #define	SIOCSIFDESCR	 _IOW('i', 41, struct ifreq)	/* set ifnet descr */ 
 #define	SIOCGIFDESCR	_IOWR('i', 42, struct ifreq)	/* get ifnet descr */ 
 #define	SIOCAIFADDR	 _IOW('i', 43, struct ifaliasreq)/* add/chg IF alias */
+#define	SIOCGIFDATA	 _IOW('i', 44, struct ifreq)	/* get if_data */
 #define	SIOCGIFALIAS	_IOWR('i', 45, struct ifaliasreq)/* get IF alias */
 
 #define	SIOCADDMULTI	 _IOW('i', 49, struct ifreq)	/* add m'cast addr */