From 13d73250676eae52cdf84768cfcf9c792b81d01b Mon Sep 17 00:00:00 2001 From: Aaron LI Date: Tue, 27 Nov 2018 12:05:58 +0800 Subject: [PATCH] ifconfig(8): Add option -n to disable auto module loading This option may be used by rc scripts to disable the auto loading of network modules when try to configure/stop a non-existent interface. e.g., `ifconfig tap0` will auto load `if_tap.ko` when `tap0` doesn't exist yet. Taken-from: FreeBSD (r169873) --- sbin/ifconfig/ifconfig.8 | 11 ++++++++++- sbin/ifconfig/ifconfig.c | 17 +++++++++++++---- 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/sbin/ifconfig/ifconfig.8 b/sbin/ifconfig/ifconfig.8 index 09c1a7118d..dc5df9c141 100644 --- a/sbin/ifconfig/ifconfig.8 +++ b/sbin/ifconfig/ifconfig.8 @@ -28,7 +28,7 @@ .\" From: @(#)ifconfig.8 8.3 (Berkeley) 1/5/94 .\" $FreeBSD: src/sbin/ifconfig/ifconfig.8,v 1.124 2006/10/10 09:44:08 ru Exp $ .\" -.Dd June 16, 2018 +.Dd November 27, 2018 .Dt IFCONFIG 8 .Os .Sh NAME @@ -39,6 +39,7 @@ .Op Fl L .Op Fl k .Op Fl m +.Op Fl n .Ar interface .Op Cm create .Op Ar address_family @@ -48,6 +49,7 @@ .Oc .Op Ar parameters .Nm +.Op Fl n .Ar interface .Cm destroy .Nm @@ -2319,6 +2321,13 @@ the current user. This information is not printed by default, as it may be considered sensitive. .Pp +If the network interface driver is not present in the kernel then +.Nm +will attempt to load it. +The +.Fl n +flag disables this behavior. +.Pp Only the super-user may modify the configuration of a network interface. .Sh DIAGNOSTICS Messages indicating the specified interface does not exist, the diff --git a/sbin/ifconfig/ifconfig.c b/sbin/ifconfig/ifconfig.c index 4d2562d44b..b7f475cc01 100644 --- a/sbin/ifconfig/ifconfig.c +++ b/sbin/ifconfig/ifconfig.c @@ -77,6 +77,7 @@ int doalias; int clearaddr; int newaddr = 1; int verbose; +int noload; int supmedia = 0; int printkeys = 0; /* Print keying material for interfaces. */ @@ -118,9 +119,10 @@ usage(void) } fprintf(stderr, - "usage: ifconfig %sinterface address_family [address [dest_address]]\n" + "usage: ifconfig %s[-n] interface address_family [address [dest_address]]\n" " [parameters]\n" - " ifconfig interface create\n" + " ifconfig [-n] interface create\n" + " ifconfig [-n] interface destroy\n" " ifconfig -a %s[-d] [-m] [-u] [-v] [address_family]\n" " ifconfig -l [-d] [-u] [address_family]\n" " ifconfig %s[-d] [-m] [-u] [-v]\n", @@ -152,7 +154,7 @@ main(int argc, char *argv[]) struct option *p; size_t iflen; - all = downonly = uponly = namesonly = verbose = 0; + all = downonly = uponly = namesonly = verbose = noload = 0; /* * Ensure we print interface name when expected to, @@ -161,7 +163,7 @@ main(int argc, char *argv[]) atexit(printifnamemaybe); /* Parse leading line options */ - strlcpy(options, "adklmuv", sizeof(options)); + strlcpy(options, "adklmnuv", sizeof(options)); for (p = opts; p != NULL; p = p->next) strlcat(options, p->opt, sizeof(options)); while ((c = getopt(argc, argv, options)) != -1) { @@ -181,6 +183,9 @@ main(int argc, char *argv[]) case 'm': /* show media choices in status */ supmedia = 1; break; + case 'n': /* suppress module loading */ + noload++; + break; case 'u': /* restrict scan to "up" interfaces */ uponly++; break; @@ -1035,6 +1040,10 @@ ifmaybeload(const char *name) char ifkind[IFNAMSIZ + MOD_PREFIX_LEN], ifname[IFNAMSIZ], *dp; const char *cp; + /* loading suppressed by the user */ + if (noload) + return; + /* trim the interface number off the end */ strlcpy(ifname, name, sizeof(ifname)); for (dp = ifname; *dp != 0; dp++) -- 2.41.0