Changeset View
Changeset View
Standalone View
Standalone View
sbin/ifconfig/ifconfig.c
Show First 20 Lines • Show All 87 Lines • ▼ Show 20 Lines | |||||
/* | /* | ||||
* Since "struct ifreq" is composed of various union members, callers | * Since "struct ifreq" is composed of various union members, callers | ||||
* should pay special attention to interpret the value. | * should pay special attention to interpret the value. | ||||
* (.e.g. little/big endian difference in the structure.) | * (.e.g. little/big endian difference in the structure.) | ||||
*/ | */ | ||||
struct ifreq ifr; | struct ifreq ifr; | ||||
char name[IFNAMSIZ]; | char name[IFNAMSIZ]; | ||||
char *descr = NULL; | #ifdef WITHOUT_NETLINK | ||||
size_t descrlen = 64; | static char *descr = NULL; | ||||
static size_t descrlen = 64; | |||||
#endif | |||||
static int setaddr; | static int setaddr; | ||||
static int setmask; | static int setmask; | ||||
static int doalias; | static int doalias; | ||||
static int clearaddr; | static int clearaddr; | ||||
int newaddr = 1; | int newaddr = 1; | ||||
int verbose; | int verbose; | ||||
int printifname = 0; | int printifname = 0; | ||||
struct ifconfig_args args; | struct ifconfig_args args; | ||||
int printkeys = 0; /* Print keying material for interfaces. */ | int printkeys = 0; /* Print keying material for interfaces. */ | ||||
int exit_code = 0; | int exit_code = 0; | ||||
/* Formatter Strings */ | /* Formatter Strings */ | ||||
char *f_inet, *f_inet6, *f_ether, *f_addr; | char *f_inet, *f_inet6, *f_ether, *f_addr; | ||||
#ifdef WITHOUT_NETLINK | |||||
static void list_interfaces_ioctl(struct ifconfig_args *args); | static void list_interfaces_ioctl(struct ifconfig_args *args); | ||||
static void status(struct ifconfig_args *args, const struct sockaddr_dl *sdl, | static void status(struct ifconfig_args *args, const struct sockaddr_dl *sdl, | ||||
struct ifaddrs *ifa); | struct ifaddrs *ifa); | ||||
#endif | |||||
static _Noreturn void usage(void); | static _Noreturn void usage(void); | ||||
static int getifflags(const char *ifname, int us, bool err_ok); | static int getifflags(const char *ifname, int us, bool err_ok); | ||||
static struct afswtch *af_getbyname(const char *name); | static struct afswtch *af_getbyname(const char *name); | ||||
void printifnamemaybe(void); | |||||
static struct option *opts = NULL; | static struct option *opts = NULL; | ||||
struct ifa_order_elt { | 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; | ||||
}; | }; | ||||
▲ Show 20 Lines • Show All 72 Lines • ▼ Show 20 Lines | if (ioctl(s, SIOCIFCREATE2, ifr) < 0) { | ||||
case EEXIST: | case EEXIST: | ||||
errx(1, "interface %s already exists", ifr->ifr_name); | errx(1, "interface %s already exists", ifr->ifr_name); | ||||
default: | default: | ||||
err(1, "SIOCIFCREATE2 (%s)", ifr->ifr_name); | err(1, "SIOCIFCREATE2 (%s)", ifr->ifr_name); | ||||
} | } | ||||
} | } | ||||
} | } | ||||
#ifdef WITHOUT_NETLINK | |||||
static int | static int | ||||
calcorders(struct ifaddrs *ifa, struct ifa_queue *q) | calcorders(struct ifaddrs *ifa, struct ifa_queue *q) | ||||
{ | { | ||||
struct ifaddrs *prev; | struct ifaddrs *prev; | ||||
struct ifa_order_elt *cur; | struct ifa_order_elt *cur; | ||||
unsigned int ord, af, ifa_ord; | unsigned int ord, af, ifa_ord; | ||||
prev = NULL; | prev = NULL; | ||||
▲ Show 20 Lines • Show All 70 Lines • ▼ Show 20 Lines | if (ret != 0) { | ||||
af2 = b->ifa_addr->sa_family; | af2 = b->ifa_addr->sa_family; | ||||
if (af1 < nitems(e1->af_orders) && af2 < nitems(e1->af_orders)) | if (af1 < nitems(e1->af_orders) && af2 < nitems(e1->af_orders)) | ||||
return (e1->af_orders[af1] - e1->af_orders[af2]); | return (e1->af_orders[af1] - e1->af_orders[af2]); | ||||
} | } | ||||
return (0); | return (0); | ||||
} | } | ||||
#endif | |||||
static void freeformat(void) | static void freeformat(void) | ||||
{ | { | ||||
if (f_inet != NULL) | if (f_inet != NULL) | ||||
free(f_inet); | free(f_inet); | ||||
if (f_inet6 != NULL) | if (f_inet6 != NULL) | ||||
free(f_inet6); | free(f_inet6); | ||||
Show All 27 Lines | while ((category = strsep(&formatstr, ",")) != NULL) { | ||||
else if (strcmp(category, "inet") == 0) | else if (strcmp(category, "inet") == 0) | ||||
f_inet = strdup(modifier); | f_inet = strdup(modifier); | ||||
else if (strcmp(category, "inet6") == 0) | else if (strcmp(category, "inet6") == 0) | ||||
f_inet6 = strdup(modifier); | f_inet6 = strdup(modifier); | ||||
} | } | ||||
free(formatstr); | free(formatstr); | ||||
} | } | ||||
#ifdef WITHOUT_NETLINK | |||||
static struct ifaddrs * | static struct ifaddrs * | ||||
sortifaddrs(struct ifaddrs *list, | sortifaddrs(struct ifaddrs *list, | ||||
int (*compare)(struct ifaddrs *, struct ifaddrs *, struct ifa_queue *), | int (*compare)(struct ifaddrs *, struct ifaddrs *, struct ifa_queue *), | ||||
struct ifa_queue *q) | struct ifa_queue *q) | ||||
{ | { | ||||
struct ifaddrs *right, *temp, *last, *result, *next, *tail; | struct ifaddrs *right, *temp, *last, *result, *next, *tail; | ||||
right = list; | right = list; | ||||
Show All 38 Lines | while (list || right) { | ||||
else | else | ||||
tail->ifa_next = next; | tail->ifa_next = next; | ||||
tail = next; | tail = next; | ||||
} | } | ||||
return (result); | return (result); | ||||
} | } | ||||
#endif | |||||
void printifnamemaybe() | static void | ||||
printifnamemaybe(void) | |||||
{ | { | ||||
if (printifname) | if (printifname) | ||||
printf("%s\n", name); | printf("%s\n", name); | ||||
} | } | ||||
static void | static void | ||||
list_interfaces(struct ifconfig_args *args) | list_interfaces(struct ifconfig_args *args) | ||||
{ | { | ||||
▲ Show 20 Lines • Show All 283 Lines • ▼ Show 20 Lines | switch (sdl->sdl_type) { | ||||
case IFT_BRIDGE: | case IFT_BRIDGE: | ||||
if (sdl->sdl_alen == ETHER_ADDR_LEN) | if (sdl->sdl_alen == ETHER_ADDR_LEN) | ||||
return (true); | return (true); | ||||
default: | default: | ||||
return (false); | return (false); | ||||
} | } | ||||
} | } | ||||
bool | |||||
match_if_flags(struct ifconfig_args *args, int if_flags) | |||||
{ | |||||
if ((if_flags & IFF_CANTCONFIG) != 0) | |||||
return (false); | |||||
if (args->downonly && (if_flags & IFF_UP) != 0) | |||||
return (false); | |||||
if (args->uponly && (if_flags & IFF_UP) == 0) | |||||
return (false); | |||||
return (true); | |||||
} | |||||
#ifdef WITHOUT_NETLINK | |||||
static bool | static bool | ||||
match_afp(const struct afswtch *afp, int sa_family, const struct sockaddr_dl *sdl) | match_afp(const struct afswtch *afp, int sa_family, const struct sockaddr_dl *sdl) | ||||
{ | { | ||||
if (afp == NULL) | if (afp == NULL) | ||||
return (true); | return (true); | ||||
/* special case for "ether" address family */ | /* special case for "ether" address family */ | ||||
if (!strcmp(afp->af_name, "ether")) { | if (!strcmp(afp->af_name, "ether")) { | ||||
if (sdl == NULL && !match_ether(sdl)) | if (sdl == NULL && !match_ether(sdl)) | ||||
return (false); | return (false); | ||||
return (true); | return (true); | ||||
} | } | ||||
return (afp->af_af == sa_family); | return (afp->af_af == sa_family); | ||||
} | } | ||||
bool | |||||
match_if_flags(struct ifconfig_args *args, int if_flags) | |||||
{ | |||||
if ((if_flags & IFF_CANTCONFIG) != 0) | |||||
return (false); | |||||
if (args->downonly && (if_flags & IFF_UP) != 0) | |||||
return (false); | |||||
if (args->uponly && (if_flags & IFF_UP) == 0) | |||||
return (false); | |||||
return (true); | |||||
} | |||||
#ifdef WITHOUT_NETLINK | |||||
static void | static void | ||||
list_interfaces_ioctl(struct ifconfig_args *args) | list_interfaces_ioctl(struct ifconfig_args *args) | ||||
{ | { | ||||
struct ifa_queue q = TAILQ_HEAD_INITIALIZER(q); | struct ifa_queue q = TAILQ_HEAD_INITIALIZER(q); | ||||
struct ifaddrs *ifap, *sifap, *ifa; | struct ifaddrs *ifap, *sifap, *ifa; | ||||
struct ifa_order_elt *cur, *tmp; | struct ifa_order_elt *cur, *tmp; | ||||
char *namecp = NULL; | char *namecp = NULL; | ||||
int ifindex; | int ifindex; | ||||
▲ Show 20 Lines • Show All 78 Lines • ▼ Show 20 Lines | |||||
*/ | */ | ||||
bool | bool | ||||
group_member(const char *ifname, const char *match, const char *nomatch) | group_member(const char *ifname, const char *match, const char *nomatch) | ||||
{ | { | ||||
static int sock = -1; | static int sock = -1; | ||||
struct ifgroupreq ifgr; | struct ifgroupreq ifgr; | ||||
struct ifg_req *ifg; | struct ifg_req *ifg; | ||||
int len; | unsigned int len; | ||||
bool matched, nomatched; | bool matched, nomatched; | ||||
/* Sanity checks. */ | /* Sanity checks. */ | ||||
if (match == NULL && nomatch == NULL) | if (match == NULL && nomatch == NULL) | ||||
return (true); | return (true); | ||||
if (ifname == NULL) | if (ifname == NULL) | ||||
return (false); | return (false); | ||||
▲ Show 20 Lines • Show All 974 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(struct ifconfig_args *args, const char *name) | ifmaybeload(struct ifconfig_args *args, const char *name) | ||||
{ | { | ||||
#define MOD_PREFIX_LEN 3 /* "if_" */ | #define MOD_PREFIX_LEN 3 /* "if_" */ | ||||
struct module_stat mstat; | struct module_stat mstat; | ||||
int i, fileid, modid; | int fileid, modid; | ||||
char ifkind[IFNAMSIZ + MOD_PREFIX_LEN], ifname[IFNAMSIZ], *dp; | char ifkind[IFNAMSIZ + MOD_PREFIX_LEN], ifname[IFNAMSIZ], *dp; | ||||
const char *cp; | const char *cp; | ||||
struct module_map_entry *mme; | struct module_map_entry *mme; | ||||
bool found; | bool found; | ||||
/* loading suppressed by the user */ | /* loading suppressed by the user */ | ||||
if (args->noload) | if (args->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)); | ||||
dp = ifname + strlen(ifname) - 1; | dp = ifname + strlen(ifname) - 1; | ||||
for (; dp > ifname; dp--) { | for (; dp > ifname; dp--) { | ||||
if (isdigit(*dp)) | if (isdigit(*dp)) | ||||
*dp = '\0'; | *dp = '\0'; | ||||
else | else | ||||
break; | break; | ||||
} | } | ||||
/* Either derive it from the map or guess otherwise */ | /* Either derive it from the map or guess otherwise */ | ||||
*ifkind = '\0'; | *ifkind = '\0'; | ||||
found = false; | found = false; | ||||
for (i = 0; i < nitems(module_map); ++i) { | for (unsigned i = 0; i < nitems(module_map); ++i) { | ||||
mme = &module_map[i]; | mme = &module_map[i]; | ||||
if (strcmp(mme->ifname, ifname) == 0) { | if (strcmp(mme->ifname, ifname) == 0) { | ||||
strlcpy(ifkind, mme->kldname, sizeof(ifkind)); | strlcpy(ifkind, mme->kldname, sizeof(ifkind)); | ||||
found = true; | found = true; | ||||
break; | break; | ||||
} | } | ||||
} | } | ||||
▲ Show 20 Lines • Show All 145 Lines • Show Last 20 Lines |