Module Name:    src
Committed By:   ozaki-r
Date:           Thu Jul  4 02:44:25 UTC 2019

Modified Files:
        src/sys/net: if.c if.h
        src/sys/sys: sockio.h

Log Message:
Add support for a network interface description.

ioctl(2):
- Add SIOCGIFDESCR/SIOCSIFDESCR commands to get/set the description.

This enables to make a memo for interface, like "Home network" or "Remote VPN".

>From t-kusaba@IIJ


To generate a diff of this commit:
cvs rdiff -u -r1.455 -r1.456 src/sys/net/if.c
cvs rdiff -u -r1.273 -r1.274 src/sys/net/if.h
cvs rdiff -u -r1.37 -r1.38 src/sys/sys/sockio.h

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/net/if.c
diff -u src/sys/net/if.c:1.455 src/sys/net/if.c:1.456
--- src/sys/net/if.c:1.455	Tue May 21 09:18:37 2019
+++ src/sys/net/if.c	Thu Jul  4 02:44:25 2019
@@ -1,4 +1,4 @@
-/*	$NetBSD: if.c,v 1.455 2019/05/21 09:18:37 msaitoh Exp $	*/
+/*	$NetBSD: if.c,v 1.456 2019/07/04 02:44:25 ozaki-r Exp $	*/
 
 /*-
  * Copyright (c) 1999, 2000, 2001, 2008 The NetBSD Foundation, Inc.
@@ -90,7 +90,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: if.c,v 1.455 2019/05/21 09:18:37 msaitoh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: if.c,v 1.456 2019/07/04 02:44:25 ozaki-r Exp $");
 
 #if defined(_KERNEL_OPT)
 #include "opt_inet.h"
@@ -3050,6 +3050,58 @@ ifioctl_common(struct ifnet *ifp, u_long
 		KERNEL_UNLOCK_UNLESS_NET_MPSAFE();
 #endif
 		return ENETRESET;
+	case SIOCSIFDESCR:
+		{
+			char *descrbuf;
+
+ 			ifr = data;
+
+ 			if (ifr->ifr_buflen > IFDESCRSIZE)
+				return ENAMETOOLONG;
+
+ 			if (ifr->ifr_buf == NULL || ifr->ifr_buflen == 0) {
+				/* unset description */
+				descrbuf = NULL;
+			} else {
+				int error;
+
+ 				descrbuf = kmem_zalloc(IFDESCRSIZE, KM_SLEEP);
+				/* copy (IFDESCRSIZE - 1) bytes to ensure terminating nul */
+				error = copyin(ifr->ifr_buf, descrbuf, IFDESCRSIZE - 1);
+				if (error) {
+					kmem_free(descrbuf, IFDESCRSIZE);
+					return error;
+				}
+			}
+
+ 			if (ifp->if_description != NULL)
+				kmem_free(ifp->if_description, IFDESCRSIZE);
+
+ 			ifp->if_description = descrbuf;
+		}
+		break;
+
+ 	case SIOCGIFDESCR:
+		{
+			char *descr;
+
+ 			ifr = data;
+			descr = ifp->if_description;
+
+ 			if (descr == NULL)
+				return ENOMSG;
+
+ 			if (ifr->ifr_buflen < IFDESCRSIZE)
+				return EINVAL;
+			else {
+				int error;
+				error = copyout(descr, ifr->ifr_buf, IFDESCRSIZE);
+				if (error)
+					return error;
+			}
+		}
+ 		break;
+
 	default:
 		return ENOTTY;
 	}

Index: src/sys/net/if.h
diff -u src/sys/net/if.h:1.273 src/sys/net/if.h:1.274
--- src/sys/net/if.h:1.273	Mon Jun 24 06:24:33 2019
+++ src/sys/net/if.h	Thu Jul  4 02:44:25 2019
@@ -1,4 +1,4 @@
-/*	$NetBSD: if.h,v 1.273 2019/06/24 06:24:33 skrll Exp $	*/
+/*	$NetBSD: if.h,v 1.274 2019/07/04 02:44:25 ozaki-r Exp $	*/
 
 /*-
  * Copyright (c) 1999, 2000, 2001 The NetBSD Foundation, Inc.
@@ -75,6 +75,11 @@
  */
 #define IF_NAMESIZE 16
 
+/*
+ * Length of interface description, including terminating '\0'.
+ */
+#define	IFDESCRSIZE	64
+
 #if defined(_NETBSD_SOURCE)
 
 #include <sys/socket.h>
@@ -365,6 +370,7 @@ typedef struct ifnet {
 	int		(*if_setflags)	/* :: */
 			    (struct ifnet *, const short);
 	kmutex_t	*if_ioctl_lock;	/* :: */
+	char		*if_description;	/* i: interface description */
 #ifdef _KERNEL /* XXX kvm(3) */
 	struct callout	*if_slowtimo_ch;/* :: */
 	struct krwlock	*if_afdata_lock;/* :: */

Index: src/sys/sys/sockio.h
diff -u src/sys/sys/sockio.h:1.37 src/sys/sys/sockio.h:1.38
--- src/sys/sys/sockio.h:1.37	Fri May 17 07:37:12 2019
+++ src/sys/sys/sockio.h	Thu Jul  4 02:44:25 2019
@@ -1,4 +1,4 @@
-/*	$NetBSD: sockio.h,v 1.37 2019/05/17 07:37:12 msaitoh Exp $	*/
+/*	$NetBSD: sockio.h,v 1.38 2019/07/04 02:44:25 ozaki-r Exp $	*/
 
 /*-
  * Copyright (c) 1982, 1986, 1990, 1993, 1994
@@ -143,6 +143,9 @@
 #define SIOCGIFINDEX  _IOWR('i', 140, struct ifreq)   /* get ifnet index */
 #define	SIOCSETHERCAP    _IOW('i', 141, struct eccapreq) /* set ethercap */
 
+#define SIOCSIFDESCR	_IOW('i', 142, struct ifreq)	/* set interface description */
+#define SIOCGIFDESCR	_IOWR('i', 143, struct ifreq)	/* get interface description */
+
 #define SIOCGUMBINFO	_IOWR('i', 190, struct ifreq)	/* get MBIM info */
 #define SIOCSUMBPARAM	_IOW('i', 191, struct ifreq)	/* set MBIM param */
 #define SIOCGUMBPARAM	_IOWR('i', 192, struct ifreq)	/* get MBIM param */

Reply via email to