Skip site navigation (1)Skip section navigation (2)
Date:      Tue, 14 May 2019 02:00:13 +0000 (UTC)
From:      Kyle Evans <kevans@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-stable@freebsd.org, svn-src-stable-11@freebsd.org
Subject:   svn commit: r347557 - in stable: 11/sbin/ifconfig 12/sbin/ifconfig
Message-ID:  <201905140200.x4E20DjS083820@repo.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: kevans
Date: Tue May 14 02:00:12 2019
New Revision: 347557
URL: https://svnweb.freebsd.org/changeset/base/347557

Log:
  MFC r347241 (partial), r347392, r347429: ifconfig(8) ifname <-> kld mapping
  
  MFC r347241 (partial): Initial mechanism for mapping ifname <-> kld
  
  if_tun/if_tap mappings have been removed and the vmnet mapping has been
  updated to the if_tap module.
  
  MFC r347392: ifconfig(8): Partial revert of r347241
  
  r347241 introduced an ifname <-> kld mapping table, mostly so tun/tap/vmnet
  can autoload the correct module on use. It also inadvertently made bogus
  some previously valid uses of sizeof().
  
  Revert back to ifkind on the stack for simplicity sake. This reduces the
  diff from the previous version of ifmaybeload for easiser auditing.
  
  MFC r347429: ifconfig(8): Add kld mappings for ipsec/enc
  
  Additionally, providing mappings makes the comparison for already loaded
  modules a little more strict. This should have been done at initial
  introduction, but there was no real reason- however, it proves necessary for
  enc which has a standard enc -> if_enc mapping but there also exists an
  'enc' module that's actually CAM. The mapping lets us unambiguously
  determine the correct module.

Modified:
  stable/11/sbin/ifconfig/ifconfig.c
Directory Properties:
  stable/11/   (props changed)

Changes in other areas also in this revision:
Modified:
  stable/12/sbin/ifconfig/ifconfig.c
Directory Properties:
  stable/12/   (props changed)

Modified: stable/11/sbin/ifconfig/ifconfig.c
==============================================================================
--- stable/11/sbin/ifconfig/ifconfig.c	Tue May 14 00:34:08 2019	(r347556)
+++ stable/11/sbin/ifconfig/ifconfig.c	Tue May 14 02:00:12 2019	(r347557)
@@ -69,6 +69,7 @@ static const char rcsid[] =
 #ifdef JAIL
 #include <jail.h>
 #endif
+#include <stdbool.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -125,6 +126,31 @@ struct ifa_order_elt {
 
 TAILQ_HEAD(ifa_queue, ifa_order_elt);
 
+static struct module_map_entry {
+	const char *ifname;
+	const char *kldname;
+} module_map[] = {
+	{
+		.ifname = "vmnet",
+		.kldname = "if_tap",
+	},
+	{
+		.ifname = "ipsec",
+		.kldname = "ipsec",
+	},
+	{
+		/*
+		 * This mapping exists because there is a conflicting enc module
+		 * in CAM.  ifconfig's guessing behavior will attempt to match
+		 * the ifname to a module as well as if_${ifname} and clash with
+		 * CAM enc.  This is an assertion of the correct module to load.
+		 */
+		.ifname = "enc",
+		.kldname = "if_enc",
+	},
+};
+
+
 void
 opt_register(struct option *p)
 {
@@ -1371,9 +1397,11 @@ ifmaybeload(const char *name)
 {
 #define MOD_PREFIX_LEN		3	/* "if_" */
 	struct module_stat mstat;
-	int fileid, modid;
+	int i, fileid, modid;
 	char ifkind[IFNAMSIZ + MOD_PREFIX_LEN], ifname[IFNAMSIZ], *dp;
 	const char *cp;
+	struct module_map_entry *mme;
+	bool found;
 
 	/* loading suppressed by the user */
 	if (noload)
@@ -1387,10 +1415,25 @@ ifmaybeload(const char *name)
 			break;
 		}
 
-	/* turn interface and unit into module name */
-	strlcpy(ifkind, "if_", sizeof(ifkind));
-	strlcat(ifkind, ifname, sizeof(ifkind));
+	/* Either derive it from the map or guess otherwise */
+	*ifkind = '\0';
+	found = false;
+	for (i = 0; i < nitems(module_map); ++i) {
+		mme = &module_map[i];
+		if (strcmp(mme->ifname, ifname) == 0) {
+			strlcpy(ifkind, mme->kldname, sizeof(ifkind));
+			found = true;
+			break;
+		}
+	}
 
+	/* We didn't have an alias for it... we'll guess. */
+	if (!found) {
+	    /* turn interface and unit into module name */
+	    strlcpy(ifkind, "if_", sizeof(ifkind));
+	    strlcat(ifkind, ifname, sizeof(ifkind));
+	}
+
 	/* scan files in kernel */
 	mstat.version = sizeof(struct module_stat);
 	for (fileid = kldnext(0); fileid > 0; fileid = kldnext(fileid)) {
@@ -1405,8 +1448,12 @@ ifmaybeload(const char *name)
 			} else {
 				cp = mstat.name;
 			}
-			/* already loaded? */
-			if (strcmp(ifname, cp) == 0 ||
+			/*
+			 * Is it already loaded?  Don't compare with ifname if
+			 * we were specifically told which kld to use.  Doing
+			 * so could lead to conflicts not trivially solved.
+			 */
+			if ((!found && strcmp(ifname, cp) == 0) ||
 			    strcmp(ifkind, cp) == 0)
 				return;
 		}



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201905140200.x4E20DjS083820>