Changeset View
Changeset View
Standalone View
Standalone View
head/sbin/ifconfig/ifconfig.c
Show First 20 Lines • Show All 124 Lines • ▼ Show 20 Lines | struct ifa_order_elt { | ||||
int if_order; | int if_order; | ||||
int af_orders[255]; | int af_orders[255]; | ||||
struct ifaddrs *ifa; | struct ifaddrs *ifa; | ||||
TAILQ_ENTRY(ifa_order_elt) link; | TAILQ_ENTRY(ifa_order_elt) link; | ||||
}; | }; | ||||
TAILQ_HEAD(ifa_queue, ifa_order_elt); | TAILQ_HEAD(ifa_queue, ifa_order_elt); | ||||
static struct module_map_entry { | |||||
const char *ifname; | |||||
const char *kldname; | |||||
} module_map[] = { | |||||
{ | |||||
.ifname = "tun", | |||||
.kldname = "if_tuntap", | |||||
}, | |||||
{ | |||||
.ifname = "tap", | |||||
.kldname = "if_tuntap", | |||||
}, | |||||
{ | |||||
.ifname = "vmnet", | |||||
.kldname = "if_tuntap", | |||||
}, | |||||
}; | |||||
void | void | ||||
opt_register(struct option *p) | opt_register(struct option *p) | ||||
{ | { | ||||
p->next = opts; | p->next = opts; | ||||
opts = p; | opts = p; | ||||
} | } | ||||
static void | static void | ||||
▲ Show 20 Lines • Show All 1,267 Lines • ▼ Show 20 Lines | print_vhid(const struct ifaddrs *ifa, const char *s) | ||||
printf(" vhid %d", ifd->ifi_vhid); | printf(" vhid %d", ifd->ifi_vhid); | ||||
} | } | ||||
void | void | ||||
ifmaybeload(const char *name) | ifmaybeload(const char *name) | ||||
{ | { | ||||
#define MOD_PREFIX_LEN 3 /* "if_" */ | #define MOD_PREFIX_LEN 3 /* "if_" */ | ||||
struct module_stat mstat; | struct module_stat mstat; | ||||
int fileid, modid; | int i, fileid, modid; | ||||
char ifkind[IFNAMSIZ + MOD_PREFIX_LEN], ifname[IFNAMSIZ], *dp; | char ifname[IFNAMSIZ], *ifkind, *dp; | ||||
const char *cp; | const char *cp; | ||||
struct module_map_entry *mme; | |||||
/* loading suppressed by the user */ | /* loading suppressed by the user */ | ||||
if (noload) | if (noload) | ||||
return; | return; | ||||
/* trim the interface number off the end */ | /* trim the interface number off the end */ | ||||
strlcpy(ifname, name, sizeof(ifname)); | strlcpy(ifname, name, sizeof(ifname)); | ||||
for (dp = ifname; *dp != 0; dp++) | for (dp = ifname; *dp != 0; dp++) | ||||
if (isdigit(*dp)) { | if (isdigit(*dp)) { | ||||
*dp = 0; | *dp = 0; | ||||
break; | break; | ||||
} | } | ||||
/* Either derive it from the map or guess otherwise */ | |||||
ifkind = NULL; | |||||
for (i = 0; i < nitems(module_map); ++i) { | |||||
mme = &module_map[i]; | |||||
if (strcmp(mme->ifname, ifname) == 0) { | |||||
ifkind = strdup(mme->kldname); | |||||
if (ifkind == NULL) | |||||
err(EXIT_FAILURE, "ifmaybeload"); | |||||
break; | |||||
} | |||||
} | |||||
/* We didn't have an alias for it... we'll guess. */ | |||||
if (ifkind == NULL) { | |||||
ifkind = malloc(IFNAMSIZ + MOD_PREFIX_LEN); | |||||
/* turn interface and unit into module name */ | /* turn interface and unit into module name */ | ||||
strlcpy(ifkind, "if_", sizeof(ifkind)); | strlcpy(ifkind, "if_", sizeof(ifkind)); | ||||
strlcat(ifkind, ifname, sizeof(ifkind)); | strlcat(ifkind, ifname, sizeof(ifkind)); | ||||
} | |||||
/* scan files in kernel */ | /* scan files in kernel */ | ||||
mstat.version = sizeof(struct module_stat); | mstat.version = sizeof(struct module_stat); | ||||
for (fileid = kldnext(0); fileid > 0; fileid = kldnext(fileid)) { | for (fileid = kldnext(0); fileid > 0; fileid = kldnext(fileid)) { | ||||
/* scan modules in file */ | /* scan modules in file */ | ||||
for (modid = kldfirstmod(fileid); modid > 0; | for (modid = kldfirstmod(fileid); modid > 0; | ||||
modid = modfnext(modid)) { | modid = modfnext(modid)) { | ||||
if (modstat(modid, &mstat) < 0) | if (modstat(modid, &mstat) < 0) | ||||
continue; | continue; | ||||
/* strip bus name if present */ | /* strip bus name if present */ | ||||
if ((cp = strchr(mstat.name, '/')) != NULL) { | if ((cp = strchr(mstat.name, '/')) != NULL) { | ||||
cp++; | cp++; | ||||
} else { | } else { | ||||
cp = mstat.name; | cp = mstat.name; | ||||
} | } | ||||
/* already loaded? */ | /* already loaded? */ | ||||
if (strcmp(ifname, cp) == 0 || | if (strcmp(ifname, cp) == 0 || | ||||
strcmp(ifkind, cp) == 0) | strcmp(ifkind, cp) == 0) | ||||
return; | goto out; | ||||
} | } | ||||
} | } | ||||
/* | /* | ||||
* Try to load the module. But ignore failures, because ifconfig can't | * Try to load the module. But ignore failures, because ifconfig can't | ||||
* infer the names of all drivers (eg mlx4en(4)). | * infer the names of all drivers (eg mlx4en(4)). | ||||
*/ | */ | ||||
(void) kldload(ifkind); | (void) kldload(ifkind); | ||||
out: | |||||
free(ifkind); | |||||
} | } | ||||
static struct cmd basic_cmds[] = { | static struct cmd basic_cmds[] = { | ||||
DEF_CMD("up", IFF_UP, setifflags), | DEF_CMD("up", IFF_UP, setifflags), | ||||
DEF_CMD("down", -IFF_UP, setifflags), | DEF_CMD("down", -IFF_UP, setifflags), | ||||
DEF_CMD("arp", -IFF_NOARP, setifflags), | DEF_CMD("arp", -IFF_NOARP, setifflags), | ||||
DEF_CMD("-arp", IFF_NOARP, setifflags), | DEF_CMD("-arp", IFF_NOARP, setifflags), | ||||
DEF_CMD("debug", IFF_DEBUG, setifflags), | DEF_CMD("debug", IFF_DEBUG, setifflags), | ||||
▲ Show 20 Lines • Show All 88 Lines • Show Last 20 Lines |