1994-05-26 06:35:07 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 1983, 1993
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 4. Neither the name of the University nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef lint
|
1996-11-21 19:36:09 +00:00
|
|
|
static const char copyright[] =
|
1994-05-26 06:35:07 +00:00
|
|
|
"@(#) Copyright (c) 1983, 1993\n\
|
|
|
|
The Regents of the University of California. All rights reserved.\n";
|
|
|
|
#endif /* not lint */
|
|
|
|
|
|
|
|
#ifndef lint
|
1998-07-06 06:53:43 +00:00
|
|
|
#if 0
|
1994-05-26 06:35:07 +00:00
|
|
|
static char sccsid[] = "@(#)ifconfig.c 8.2 (Berkeley) 2/16/94";
|
1998-07-06 06:53:43 +00:00
|
|
|
#endif
|
1996-11-21 19:36:09 +00:00
|
|
|
static const char rcsid[] =
|
1999-08-28 00:22:10 +00:00
|
|
|
"$FreeBSD$";
|
1994-05-26 06:35:07 +00:00
|
|
|
#endif /* not lint */
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/ioctl.h>
|
1996-11-21 19:36:09 +00:00
|
|
|
#include <sys/socket.h>
|
1996-01-01 09:05:22 +00:00
|
|
|
#include <sys/sysctl.h>
|
1996-12-10 17:11:53 +00:00
|
|
|
#include <sys/time.h>
|
1999-09-20 07:58:08 +00:00
|
|
|
#include <sys/module.h>
|
|
|
|
#include <sys/linker.h>
|
1994-05-26 06:35:07 +00:00
|
|
|
|
2000-06-16 20:14:43 +00:00
|
|
|
#include <net/ethernet.h>
|
1994-05-26 06:35:07 +00:00
|
|
|
#include <net/if.h>
|
1997-01-03 20:19:50 +00:00
|
|
|
#include <net/if_var.h>
|
1994-12-30 22:42:41 +00:00
|
|
|
#include <net/if_dl.h>
|
1996-01-01 09:05:22 +00:00
|
|
|
#include <net/if_types.h>
|
|
|
|
#include <net/route.h>
|
1996-11-21 19:36:09 +00:00
|
|
|
|
|
|
|
/* IP */
|
1994-05-26 06:35:07 +00:00
|
|
|
#include <netinet/in.h>
|
1994-12-30 22:42:41 +00:00
|
|
|
#include <netinet/in_var.h>
|
1994-05-26 06:35:07 +00:00
|
|
|
#include <arpa/inet.h>
|
1995-10-26 20:31:59 +00:00
|
|
|
#include <netdb.h>
|
|
|
|
|
1994-05-26 06:35:07 +00:00
|
|
|
#include <ctype.h>
|
|
|
|
#include <err.h>
|
|
|
|
#include <errno.h>
|
1996-11-21 19:36:09 +00:00
|
|
|
#include <fcntl.h>
|
1994-05-26 06:35:07 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
1997-05-04 06:27:45 +00:00
|
|
|
#include "ifconfig.h"
|
|
|
|
|
2004-08-09 03:13:57 +00:00
|
|
|
/*
|
|
|
|
* Since "struct ifreq" is composed of various union members, callers
|
|
|
|
* should pay special attention to interprete the value.
|
|
|
|
* (.e.g. little/big endian difference in the structure.)
|
|
|
|
*/
|
2004-12-08 19:18:07 +00:00
|
|
|
struct ifreq ifr;
|
1994-05-26 06:35:07 +00:00
|
|
|
|
2004-01-27 01:43:14 +00:00
|
|
|
char name[IFNAMSIZ];
|
1994-05-26 06:35:07 +00:00
|
|
|
int flags;
|
|
|
|
int setaddr;
|
|
|
|
int setipdst;
|
2001-07-26 16:44:19 +00:00
|
|
|
int setmask;
|
1994-05-26 06:35:07 +00:00
|
|
|
int doalias;
|
|
|
|
int clearaddr;
|
|
|
|
int newaddr = 1;
|
2004-12-08 19:18:07 +00:00
|
|
|
int verbose;
|
1994-05-26 06:35:07 +00:00
|
|
|
|
2004-12-08 19:18:07 +00:00
|
|
|
int supmedia = 0;
|
|
|
|
int printname = 0; /* Print the name of the created interface. */
|
1996-11-21 19:36:09 +00:00
|
|
|
|
2004-12-08 19:18:07 +00:00
|
|
|
static int ifconfig(int argc, char *const *argv, const struct afswtch *afp);
|
|
|
|
static void status(const struct afswtch *afp, int addrcount,
|
1997-05-10 17:14:53 +00:00
|
|
|
struct sockaddr_dl *sdl, struct if_msghdr *ifm,
|
2002-04-18 17:14:09 +00:00
|
|
|
struct ifa_msghdr *ifam);
|
2004-12-08 19:18:07 +00:00
|
|
|
static void tunnel_status(int s);
|
|
|
|
static void usage(void);
|
1999-12-07 17:39:16 +00:00
|
|
|
|
2004-12-08 19:18:07 +00:00
|
|
|
static struct afswtch *af_getbyname(const char *name);
|
|
|
|
static struct afswtch *af_getbyfamily(int af);
|
2004-12-31 19:46:27 +00:00
|
|
|
static void af_other_status(int);
|
1994-05-26 06:35:07 +00:00
|
|
|
|
2004-12-08 19:18:07 +00:00
|
|
|
static struct option *opts = NULL;
|
1996-01-01 09:05:22 +00:00
|
|
|
|
|
|
|
void
|
2004-12-08 19:18:07 +00:00
|
|
|
opt_register(struct option *p)
|
1996-01-01 09:05:22 +00:00
|
|
|
{
|
2004-12-08 19:18:07 +00:00
|
|
|
p->next = opts;
|
|
|
|
opts = p;
|
1996-01-01 09:05:22 +00:00
|
|
|
}
|
|
|
|
|
2004-12-08 19:18:07 +00:00
|
|
|
static void
|
2002-04-18 17:14:09 +00:00
|
|
|
usage(void)
|
1997-05-04 06:14:47 +00:00
|
|
|
{
|
2004-12-08 19:18:07 +00:00
|
|
|
char options[1024];
|
|
|
|
struct option *p;
|
|
|
|
|
|
|
|
/* XXX not right but close enough for now */
|
|
|
|
options[0] = '\0';
|
|
|
|
for (p = opts; p != NULL; p = p->next) {
|
|
|
|
strlcat(options, p->opt_usage, sizeof(options));
|
|
|
|
strlcat(options, " ", sizeof(options));
|
|
|
|
}
|
|
|
|
|
|
|
|
fprintf(stderr,
|
|
|
|
"usage: ifconfig %sinterface address_family [address [dest_address]]\n"
|
|
|
|
" [parameters]\n"
|
|
|
|
" ifconfig interface create\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",
|
|
|
|
options, options, options);
|
1997-05-04 06:14:47 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
1996-01-01 09:05:22 +00:00
|
|
|
|
1996-11-21 19:36:09 +00:00
|
|
|
int
|
2002-04-18 17:14:09 +00:00
|
|
|
main(int argc, char *argv[])
|
1994-05-26 06:35:07 +00:00
|
|
|
{
|
2004-12-08 19:18:07 +00:00
|
|
|
int c, all, namesonly, downonly, uponly;
|
2004-04-30 22:34:12 +00:00
|
|
|
int need_nl = 0, count = 0;
|
2004-12-08 19:18:07 +00:00
|
|
|
const struct afswtch *afp = NULL;
|
2001-10-17 18:30:13 +00:00
|
|
|
int addrcount, ifindex;
|
2004-12-08 19:18:07 +00:00
|
|
|
struct if_msghdr *ifm, *nextifm;
|
|
|
|
struct ifa_msghdr *ifam;
|
|
|
|
struct sockaddr_dl *sdl;
|
|
|
|
char *buf, *lim, *next;
|
1996-01-01 09:05:22 +00:00
|
|
|
size_t needed;
|
1996-11-21 19:36:09 +00:00
|
|
|
int mib[6];
|
2004-12-08 19:18:07 +00:00
|
|
|
char options[1024];
|
|
|
|
struct option *p;
|
|
|
|
|
|
|
|
all = downonly = uponly = namesonly = verbose = 0;
|
1997-05-04 06:14:47 +00:00
|
|
|
|
|
|
|
/* Parse leading line options */
|
2004-12-08 19:18:07 +00:00
|
|
|
strlcpy(options, "adlmuv", sizeof(options));
|
|
|
|
for (p = opts; p != NULL; p = p->next)
|
|
|
|
strlcat(options, p->opt, sizeof(options));
|
|
|
|
while ((c = getopt(argc, argv, options)) != -1) {
|
1997-05-04 06:14:47 +00:00
|
|
|
switch (c) {
|
|
|
|
case 'a': /* scan all interfaces */
|
|
|
|
all++;
|
|
|
|
break;
|
2001-07-02 20:52:34 +00:00
|
|
|
case 'd': /* restrict scan to "down" interfaces */
|
|
|
|
downonly++;
|
1999-12-07 17:39:16 +00:00
|
|
|
break;
|
1997-05-04 06:14:47 +00:00
|
|
|
case 'l': /* scan interface names only */
|
|
|
|
namesonly++;
|
|
|
|
break;
|
2001-07-02 20:52:34 +00:00
|
|
|
case 'm': /* show media choices in status */
|
|
|
|
supmedia = 1;
|
1997-05-04 06:14:47 +00:00
|
|
|
break;
|
1997-10-27 03:28:44 +00:00
|
|
|
case 'u': /* restrict scan to "up" interfaces */
|
1997-05-04 06:14:47 +00:00
|
|
|
uponly++;
|
|
|
|
break;
|
2004-12-08 19:18:07 +00:00
|
|
|
case 'v':
|
|
|
|
verbose++;
|
1997-05-04 06:27:45 +00:00
|
|
|
break;
|
1997-05-04 06:14:47 +00:00
|
|
|
default:
|
2004-12-08 19:18:07 +00:00
|
|
|
for (p = opts; p != NULL; p = p->next)
|
|
|
|
if (p->opt[0] == c) {
|
|
|
|
p->cb(optarg);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (p == NULL)
|
|
|
|
usage();
|
1997-05-04 06:14:47 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
|
|
|
|
/* -l cannot be used with -a or -m */
|
2001-05-29 09:13:44 +00:00
|
|
|
if (namesonly && (all || supmedia))
|
1997-05-04 06:14:47 +00:00
|
|
|
usage();
|
|
|
|
|
|
|
|
/* nonsense.. */
|
|
|
|
if (uponly && downonly)
|
|
|
|
usage();
|
|
|
|
|
2000-07-18 22:07:31 +00:00
|
|
|
/* no arguments is equivalent to '-a' */
|
|
|
|
if (!namesonly && argc < 1)
|
|
|
|
all = 1;
|
|
|
|
|
1997-05-04 06:14:47 +00:00
|
|
|
/* -a and -l allow an address family arg to limit the output */
|
|
|
|
if (all || namesonly) {
|
|
|
|
if (argc > 1)
|
|
|
|
usage();
|
|
|
|
|
2001-10-17 18:30:13 +00:00
|
|
|
ifindex = 0;
|
1997-05-04 06:14:47 +00:00
|
|
|
if (argc == 1) {
|
2004-12-08 19:18:07 +00:00
|
|
|
afp = af_getbyname(*argv);
|
|
|
|
if (afp == NULL)
|
1997-05-04 06:14:47 +00:00
|
|
|
usage();
|
2004-12-08 19:18:07 +00:00
|
|
|
if (afp->af_name != NULL)
|
|
|
|
argc--, argv++;
|
1997-05-04 06:14:47 +00:00
|
|
|
/* leave with afp non-zero */
|
|
|
|
}
|
|
|
|
} else {
|
1998-07-06 06:53:43 +00:00
|
|
|
/* not listing, need an argument */
|
1997-05-04 06:14:47 +00:00
|
|
|
if (argc < 1)
|
|
|
|
usage();
|
|
|
|
|
|
|
|
strncpy(name, *argv, sizeof(name));
|
|
|
|
argc--, argv++;
|
1999-09-20 07:58:08 +00:00
|
|
|
|
|
|
|
/* check and maybe load support for this interface */
|
|
|
|
ifmaybeload(name);
|
2001-07-02 20:52:34 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* NOTE: We must special-case the `create' command right
|
|
|
|
* here as we would otherwise fail when trying to find
|
|
|
|
* the interface.
|
|
|
|
*/
|
2001-08-17 22:16:11 +00:00
|
|
|
if (argc > 0 && (strcmp(argv[0], "create") == 0 ||
|
|
|
|
strcmp(argv[0], "plumb") == 0)) {
|
2001-07-02 20:52:34 +00:00
|
|
|
clone_create();
|
|
|
|
argc--, argv++;
|
|
|
|
if (argc == 0)
|
2004-02-04 02:55:46 +00:00
|
|
|
goto end;
|
2001-07-02 20:52:34 +00:00
|
|
|
}
|
2001-10-17 18:30:13 +00:00
|
|
|
ifindex = if_nametoindex(name);
|
|
|
|
if (ifindex == 0)
|
|
|
|
errx(1, "interface %s does not exist", name);
|
1994-05-26 06:35:07 +00:00
|
|
|
}
|
1997-05-04 06:14:47 +00:00
|
|
|
|
|
|
|
/* Check for address family */
|
1994-05-26 06:35:07 +00:00
|
|
|
if (argc > 0) {
|
2004-12-08 19:18:07 +00:00
|
|
|
afp = af_getbyname(*argv);
|
|
|
|
if (afp != NULL)
|
|
|
|
argc--, argv++;
|
1994-05-26 06:35:07 +00:00
|
|
|
}
|
1996-01-01 09:05:22 +00:00
|
|
|
|
2004-04-30 22:34:12 +00:00
|
|
|
retry:
|
1996-01-01 09:05:22 +00:00
|
|
|
mib[0] = CTL_NET;
|
|
|
|
mib[1] = PF_ROUTE;
|
|
|
|
mib[2] = 0;
|
2001-10-17 18:30:13 +00:00
|
|
|
mib[3] = 0; /* address family */
|
1996-01-01 09:05:22 +00:00
|
|
|
mib[4] = NET_RT_IFLIST;
|
2001-10-17 18:30:13 +00:00
|
|
|
mib[5] = ifindex; /* interface index */
|
1996-01-01 09:05:22 +00:00
|
|
|
|
1996-01-08 10:23:25 +00:00
|
|
|
/* if particular family specified, only ask about it */
|
2004-12-08 19:18:07 +00:00
|
|
|
if (afp != NULL)
|
1996-01-08 10:23:25 +00:00
|
|
|
mib[3] = afp->af_af;
|
|
|
|
|
1996-01-01 09:05:22 +00:00
|
|
|
if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
|
1996-01-08 10:23:25 +00:00
|
|
|
errx(1, "iflist-sysctl-estimate");
|
1996-01-01 09:05:22 +00:00
|
|
|
if ((buf = malloc(needed)) == NULL)
|
|
|
|
errx(1, "malloc");
|
2004-04-30 22:34:12 +00:00
|
|
|
if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0) {
|
|
|
|
if (errno == ENOMEM && count++ < 10) {
|
|
|
|
warnx("Routing table grew, retrying");
|
|
|
|
free(buf);
|
|
|
|
sleep(1);
|
|
|
|
goto retry;
|
|
|
|
}
|
1996-01-01 09:05:22 +00:00
|
|
|
errx(1, "actual retrieval of interface table");
|
2004-04-30 22:34:12 +00:00
|
|
|
}
|
1996-01-01 09:05:22 +00:00
|
|
|
lim = buf + needed;
|
|
|
|
|
1997-05-10 17:14:53 +00:00
|
|
|
next = buf;
|
|
|
|
while (next < lim) {
|
1996-01-01 09:05:22 +00:00
|
|
|
|
|
|
|
ifm = (struct if_msghdr *)next;
|
1997-05-10 17:14:53 +00:00
|
|
|
|
1996-01-01 09:05:22 +00:00
|
|
|
if (ifm->ifm_type == RTM_IFINFO) {
|
2004-09-01 18:22:14 +00:00
|
|
|
if (ifm->ifm_data.ifi_datalen == 0)
|
|
|
|
ifm->ifm_data.ifi_datalen = sizeof(struct if_data);
|
|
|
|
sdl = (struct sockaddr_dl *)((char *)ifm + sizeof(struct if_msghdr) -
|
|
|
|
sizeof(struct if_data) + ifm->ifm_data.ifi_datalen);
|
1996-01-01 09:05:22 +00:00
|
|
|
flags = ifm->ifm_flags;
|
|
|
|
} else {
|
1997-05-10 17:14:53 +00:00
|
|
|
fprintf(stderr, "out of sync parsing NET_RT_IFLIST\n");
|
|
|
|
fprintf(stderr, "expected %d, got %d\n", RTM_IFINFO,
|
|
|
|
ifm->ifm_type);
|
|
|
|
fprintf(stderr, "msglen = %d\n", ifm->ifm_msglen);
|
|
|
|
fprintf(stderr, "buf:%p, next:%p, lim:%p\n", buf, next,
|
|
|
|
lim);
|
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
next += ifm->ifm_msglen;
|
|
|
|
ifam = NULL;
|
|
|
|
addrcount = 0;
|
|
|
|
while (next < lim) {
|
|
|
|
|
|
|
|
nextifm = (struct if_msghdr *)next;
|
|
|
|
|
|
|
|
if (nextifm->ifm_type != RTM_NEWADDR)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (ifam == NULL)
|
|
|
|
ifam = (struct ifa_msghdr *)nextifm;
|
|
|
|
|
|
|
|
addrcount++;
|
|
|
|
next += nextifm->ifm_msglen;
|
1996-01-01 09:05:22 +00:00
|
|
|
}
|
2004-01-31 22:59:03 +00:00
|
|
|
memcpy(name, sdl->sdl_data,
|
|
|
|
sizeof(name) < sdl->sdl_nlen ?
|
|
|
|
sizeof(name)-1 : sdl->sdl_nlen);
|
|
|
|
name[sizeof(name) < sdl->sdl_nlen ?
|
|
|
|
sizeof(name)-1 : sdl->sdl_nlen] = '\0';
|
1996-01-01 09:05:22 +00:00
|
|
|
|
1997-05-04 06:14:47 +00:00
|
|
|
if (all || namesonly) {
|
|
|
|
if (uponly)
|
|
|
|
if ((flags & IFF_UP) == 0)
|
|
|
|
continue; /* not up */
|
|
|
|
if (downonly)
|
|
|
|
if (flags & IFF_UP)
|
|
|
|
continue; /* not down */
|
|
|
|
if (namesonly) {
|
2004-12-08 19:18:07 +00:00
|
|
|
if (afp == NULL || afp->af_af != AF_LINK ||
|
|
|
|
sdl->sdl_type == IFT_ETHER) {
|
1998-03-19 20:58:45 +00:00
|
|
|
if (need_nl)
|
|
|
|
putchar(' ');
|
|
|
|
fputs(name, stdout);
|
|
|
|
need_nl++;
|
|
|
|
}
|
1997-05-04 06:14:47 +00:00
|
|
|
continue;
|
|
|
|
}
|
1996-01-01 09:05:22 +00:00
|
|
|
}
|
|
|
|
|
1997-05-10 17:14:53 +00:00
|
|
|
if (argc > 0)
|
|
|
|
ifconfig(argc, argv, afp);
|
|
|
|
else
|
|
|
|
status(afp, addrcount, sdl, ifm, ifam);
|
1996-01-01 09:05:22 +00:00
|
|
|
}
|
|
|
|
free(buf);
|
|
|
|
|
1997-05-04 06:14:47 +00:00
|
|
|
if (namesonly && need_nl > 0)
|
|
|
|
putchar('\n');
|
2004-02-04 02:55:46 +00:00
|
|
|
end:
|
|
|
|
if (printname)
|
|
|
|
printf("%s\n", name);
|
1997-05-04 06:14:47 +00:00
|
|
|
|
1994-12-30 22:42:41 +00:00
|
|
|
exit (0);
|
|
|
|
}
|
|
|
|
|
2004-12-08 19:18:07 +00:00
|
|
|
static struct afswtch *afs = NULL;
|
|
|
|
|
|
|
|
void
|
|
|
|
af_register(struct afswtch *p)
|
|
|
|
{
|
|
|
|
p->af_next = afs;
|
|
|
|
afs = p;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct afswtch *
|
|
|
|
af_getbyname(const char *name)
|
|
|
|
{
|
|
|
|
struct afswtch *afp;
|
|
|
|
|
|
|
|
for (afp = afs; afp != NULL; afp = afp->af_next)
|
|
|
|
if (strcmp(afp->af_name, name) == 0)
|
|
|
|
return afp;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct afswtch *
|
|
|
|
af_getbyfamily(int af)
|
|
|
|
{
|
|
|
|
struct afswtch *afp;
|
|
|
|
|
|
|
|
for (afp = afs; afp != NULL; afp = afp->af_next)
|
|
|
|
if (afp->af_af == af)
|
|
|
|
return afp;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2004-12-31 19:46:27 +00:00
|
|
|
af_other_status(int s)
|
2004-12-08 19:18:07 +00:00
|
|
|
{
|
|
|
|
struct afswtch *afp;
|
|
|
|
uint8_t afmask[howmany(AF_MAX, NBBY)];
|
|
|
|
|
|
|
|
memset(afmask, 0, sizeof(afmask));
|
|
|
|
for (afp = afs; afp != NULL; afp = afp->af_next) {
|
2004-12-31 19:46:27 +00:00
|
|
|
if (afp->af_other_status == NULL)
|
2004-12-08 19:18:07 +00:00
|
|
|
continue;
|
|
|
|
if (afp->af_af != AF_UNSPEC && isset(afmask, afp->af_af))
|
|
|
|
continue;
|
2004-12-31 19:46:27 +00:00
|
|
|
afp->af_other_status(s);
|
2004-12-08 19:18:07 +00:00
|
|
|
setbit(afmask, afp->af_af);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
af_all_tunnel_status(int s)
|
|
|
|
{
|
|
|
|
struct afswtch *afp;
|
|
|
|
uint8_t afmask[howmany(AF_MAX, NBBY)];
|
|
|
|
|
|
|
|
memset(afmask, 0, sizeof(afmask));
|
|
|
|
for (afp = afs; afp != NULL; afp = afp->af_next) {
|
|
|
|
if (afp->af_status_tunnel == NULL)
|
|
|
|
continue;
|
|
|
|
if (afp->af_af != AF_UNSPEC && isset(afmask, afp->af_af))
|
|
|
|
continue;
|
|
|
|
afp->af_status_tunnel(s);
|
|
|
|
setbit(afmask, afp->af_af);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct cmd *cmds = NULL;
|
|
|
|
|
|
|
|
void
|
|
|
|
cmd_register(struct cmd *p)
|
|
|
|
{
|
|
|
|
p->c_next = cmds;
|
|
|
|
cmds = p;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct cmd *
|
|
|
|
cmd_lookup(const char *name)
|
|
|
|
{
|
|
|
|
#define N(a) (sizeof(a)/sizeof(a[0]))
|
|
|
|
const struct cmd *p;
|
|
|
|
|
|
|
|
for (p = cmds; p != NULL; p = p->c_next)
|
|
|
|
if (strcmp(name, p->c_name) == 0)
|
|
|
|
return p;
|
|
|
|
return NULL;
|
|
|
|
#undef N
|
|
|
|
}
|
|
|
|
|
2004-12-11 02:33:33 +00:00
|
|
|
struct callback {
|
|
|
|
callback_func *cb_func;
|
|
|
|
void *cb_arg;
|
|
|
|
struct callback *cb_next;
|
|
|
|
};
|
|
|
|
static struct callback *callbacks = NULL;
|
|
|
|
|
|
|
|
void
|
|
|
|
callback_register(callback_func *func, void *arg)
|
|
|
|
{
|
|
|
|
struct callback *cb;
|
|
|
|
|
|
|
|
cb = malloc(sizeof(struct callback));
|
|
|
|
if (cb == NULL)
|
|
|
|
errx(1, "unable to allocate memory for callback");
|
|
|
|
cb->cb_func = func;
|
|
|
|
cb->cb_arg = arg;
|
|
|
|
cb->cb_next = callbacks;
|
|
|
|
callbacks = cb;
|
|
|
|
}
|
|
|
|
|
2005-04-08 21:31:20 +00:00
|
|
|
/* specially-handled commands */
|
2004-12-08 19:18:07 +00:00
|
|
|
static void setifaddr(const char *, int, int, const struct afswtch *);
|
|
|
|
static const struct cmd setifaddr_cmd = DEF_CMD("ifaddr", 0, setifaddr);
|
|
|
|
|
|
|
|
static void setifdstaddr(const char *, int, int, const struct afswtch *);
|
|
|
|
static const struct cmd setifdstaddr_cmd =
|
|
|
|
DEF_CMD("ifdstaddr", 0, setifdstaddr);
|
|
|
|
|
|
|
|
static int
|
2002-04-18 17:14:09 +00:00
|
|
|
ifconfig(int argc, char *const *argv, const struct afswtch *afp)
|
1994-12-30 22:42:41 +00:00
|
|
|
{
|
2004-12-11 02:33:33 +00:00
|
|
|
struct callback *cb;
|
2004-12-08 19:18:07 +00:00
|
|
|
int s;
|
1995-03-12 19:05:03 +00:00
|
|
|
|
1997-05-10 17:14:53 +00:00
|
|
|
if (afp == NULL)
|
2004-12-08 19:18:07 +00:00
|
|
|
afp = af_getbyname("inet");
|
|
|
|
ifr.ifr_addr.sa_family =
|
|
|
|
afp->af_af == AF_LINK || afp->af_af == AF_UNSPEC ?
|
|
|
|
AF_INET : afp->af_af;
|
1994-05-26 06:35:07 +00:00
|
|
|
strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
|
1997-05-04 06:14:47 +00:00
|
|
|
|
2004-12-08 19:18:07 +00:00
|
|
|
if ((s = socket(ifr.ifr_addr.sa_family, SOCK_DGRAM, 0)) < 0)
|
|
|
|
err(1, "socket(family %u,SOCK_DGRAM", ifr.ifr_addr.sa_family);
|
1996-01-01 09:05:22 +00:00
|
|
|
|
1994-05-26 06:35:07 +00:00
|
|
|
while (argc > 0) {
|
2002-03-20 17:55:10 +00:00
|
|
|
const struct cmd *p;
|
1994-05-26 06:35:07 +00:00
|
|
|
|
2004-12-08 19:18:07 +00:00
|
|
|
p = cmd_lookup(*argv);
|
|
|
|
if (p == NULL) {
|
|
|
|
/*
|
|
|
|
* Not a recognized command, choose between setting
|
|
|
|
* the interface address and the dst address.
|
|
|
|
*/
|
|
|
|
p = (setaddr ? &setifdstaddr_cmd : &setifaddr_cmd);
|
|
|
|
}
|
2005-04-08 21:37:41 +00:00
|
|
|
if (p->c_u.c_func || p->c_u.c_func2) {
|
1994-05-26 06:35:07 +00:00
|
|
|
if (p->c_parameter == NEXTARG) {
|
|
|
|
if (argv[1] == NULL)
|
|
|
|
errx(1, "'%s' requires argument",
|
|
|
|
p->c_name);
|
2005-04-08 21:37:41 +00:00
|
|
|
p->c_u.c_func(argv[1], 0, s, afp);
|
1994-05-26 06:35:07 +00:00
|
|
|
argc--, argv++;
|
2004-12-08 19:18:07 +00:00
|
|
|
} else if (p->c_parameter == OPTARG) {
|
2005-04-08 21:37:41 +00:00
|
|
|
p->c_u.c_func(argv[1], 0, s, afp);
|
2004-12-08 19:18:07 +00:00
|
|
|
if (argv[1] != NULL)
|
|
|
|
argc--, argv++;
|
2001-06-11 12:39:29 +00:00
|
|
|
} else if (p->c_parameter == NEXTARG2) {
|
|
|
|
if (argc < 3)
|
|
|
|
errx(1, "'%s' requires 2 arguments",
|
|
|
|
p->c_name);
|
2005-04-08 21:37:41 +00:00
|
|
|
p->c_u.c_func2(argv[1], argv[2], s, afp);
|
2001-06-11 12:39:29 +00:00
|
|
|
argc -= 2, argv += 2;
|
1994-05-26 06:35:07 +00:00
|
|
|
} else
|
2005-04-08 21:37:41 +00:00
|
|
|
p->c_u.c_func(*argv, p->c_parameter, s, afp);
|
1994-05-26 06:35:07 +00:00
|
|
|
}
|
|
|
|
argc--, argv++;
|
|
|
|
}
|
1995-10-26 20:31:59 +00:00
|
|
|
|
2004-12-08 19:18:07 +00:00
|
|
|
/*
|
|
|
|
* Do any post argument processing required by the address family.
|
|
|
|
*/
|
|
|
|
if (afp->af_postproc != NULL)
|
|
|
|
afp->af_postproc(s, afp);
|
|
|
|
/*
|
|
|
|
* Do deferred operations.
|
|
|
|
*/
|
1996-01-08 03:46:27 +00:00
|
|
|
if (clearaddr) {
|
1997-05-10 17:14:53 +00:00
|
|
|
if (afp->af_ridreq == NULL || afp->af_difaddr == 0) {
|
1996-01-08 03:46:27 +00:00
|
|
|
warnx("interface %s cannot change %s addresses!",
|
1997-05-10 17:14:53 +00:00
|
|
|
name, afp->af_name);
|
2004-03-10 08:46:39 +00:00
|
|
|
clearaddr = 0;
|
1996-01-08 03:46:27 +00:00
|
|
|
}
|
|
|
|
}
|
1994-05-26 06:35:07 +00:00
|
|
|
if (clearaddr) {
|
|
|
|
int ret;
|
1997-05-10 17:14:53 +00:00
|
|
|
strncpy(afp->af_ridreq, name, sizeof ifr.ifr_name);
|
2004-12-08 19:18:07 +00:00
|
|
|
ret = ioctl(s, afp->af_difaddr, afp->af_ridreq);
|
|
|
|
if (ret < 0) {
|
1994-05-26 06:35:07 +00:00
|
|
|
if (errno == EADDRNOTAVAIL && (doalias >= 0)) {
|
|
|
|
/* means no previous address for interface */
|
|
|
|
} else
|
|
|
|
Perror("ioctl (SIOCDIFADDR)");
|
|
|
|
}
|
|
|
|
}
|
1996-01-08 03:46:27 +00:00
|
|
|
if (newaddr) {
|
1999-12-29 13:53:13 +00:00
|
|
|
if (afp->af_addreq == NULL || afp->af_aifaddr == 0) {
|
1996-01-08 03:46:27 +00:00
|
|
|
warnx("interface %s cannot change %s addresses!",
|
1997-05-10 17:14:53 +00:00
|
|
|
name, afp->af_name);
|
1999-12-29 13:53:13 +00:00
|
|
|
newaddr = 0;
|
1996-01-08 03:46:27 +00:00
|
|
|
}
|
|
|
|
}
|
2001-07-26 16:44:19 +00:00
|
|
|
if (newaddr && (setaddr || setmask)) {
|
1997-05-10 17:14:53 +00:00
|
|
|
strncpy(afp->af_addreq, name, sizeof ifr.ifr_name);
|
|
|
|
if (ioctl(s, afp->af_aifaddr, afp->af_addreq) < 0)
|
1994-05-26 06:35:07 +00:00
|
|
|
Perror("ioctl (SIOCAIFADDR)");
|
|
|
|
}
|
2004-12-11 02:33:33 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Do deferred callbacks registered while processing
|
|
|
|
* command-line arguments.
|
|
|
|
*/
|
|
|
|
for (cb = callbacks; cb != NULL; cb = cb->cb_next)
|
|
|
|
cb->cb_func(s, cb->cb_arg);
|
|
|
|
|
1997-05-04 06:14:47 +00:00
|
|
|
close(s);
|
1994-12-30 22:42:41 +00:00
|
|
|
return(0);
|
1994-05-26 06:35:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*ARGSUSED*/
|
2004-12-08 19:18:07 +00:00
|
|
|
static void
|
2002-04-18 17:14:09 +00:00
|
|
|
setifaddr(const char *addr, int param, int s, const struct afswtch *afp)
|
1994-05-26 06:35:07 +00:00
|
|
|
{
|
2004-12-08 19:18:07 +00:00
|
|
|
if (afp->af_getaddr == NULL)
|
1999-12-29 13:53:13 +00:00
|
|
|
return;
|
1994-05-26 06:35:07 +00:00
|
|
|
/*
|
|
|
|
* Delay the ioctl to set the interface addr until flags are all set.
|
|
|
|
* The address interpretation may depend on the flags,
|
|
|
|
* and the flags may change when the address is set.
|
|
|
|
*/
|
|
|
|
setaddr++;
|
2000-07-12 16:41:47 +00:00
|
|
|
if (doalias == 0 && afp->af_af != AF_LINK)
|
1994-05-26 06:35:07 +00:00
|
|
|
clearaddr = 1;
|
2004-12-08 19:18:07 +00:00
|
|
|
afp->af_getaddr(addr, (doalias >= 0 ? ADDR : RIDADDR));
|
1994-05-26 06:35:07 +00:00
|
|
|
}
|
|
|
|
|
2004-12-08 19:18:07 +00:00
|
|
|
static void
|
2002-04-18 17:14:09 +00:00
|
|
|
settunnel(const char *src, const char *dst, int s, const struct afswtch *afp)
|
2001-06-11 12:39:29 +00:00
|
|
|
{
|
2004-12-08 19:18:07 +00:00
|
|
|
struct addrinfo *srcres, *dstres;
|
2001-06-11 12:39:29 +00:00
|
|
|
int ecode;
|
|
|
|
|
2004-12-08 19:18:07 +00:00
|
|
|
if (afp->af_settunnel == NULL) {
|
|
|
|
warn("address family %s does not support tunnel setup",
|
|
|
|
afp->af_name);
|
|
|
|
return;
|
|
|
|
}
|
2001-06-11 12:39:29 +00:00
|
|
|
|
|
|
|
if ((ecode = getaddrinfo(src, NULL, NULL, &srcres)) != 0)
|
|
|
|
errx(1, "error in parsing address string: %s",
|
|
|
|
gai_strerror(ecode));
|
|
|
|
|
|
|
|
if ((ecode = getaddrinfo(dst, NULL, NULL, &dstres)) != 0)
|
|
|
|
errx(1, "error in parsing address string: %s",
|
|
|
|
gai_strerror(ecode));
|
|
|
|
|
|
|
|
if (srcres->ai_addr->sa_family != dstres->ai_addr->sa_family)
|
|
|
|
errx(1,
|
|
|
|
"source and destination address families do not match");
|
|
|
|
|
2004-12-08 19:18:07 +00:00
|
|
|
afp->af_settunnel(s, srcres, dstres);
|
2001-06-11 12:39:29 +00:00
|
|
|
|
|
|
|
freeaddrinfo(srcres);
|
|
|
|
freeaddrinfo(dstres);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ARGSUSED */
|
2004-12-08 19:18:07 +00:00
|
|
|
static void
|
2002-04-18 17:14:09 +00:00
|
|
|
deletetunnel(const char *vname, int param, int s, const struct afswtch *afp)
|
2001-06-11 12:39:29 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
if (ioctl(s, SIOCDIFPHYADDR, &ifr) < 0)
|
|
|
|
err(1, "SIOCDIFPHYADDR");
|
|
|
|
}
|
|
|
|
|
2004-12-08 19:18:07 +00:00
|
|
|
static void
|
2002-04-18 17:14:09 +00:00
|
|
|
setifnetmask(const char *addr, int dummy __unused, int s,
|
|
|
|
const struct afswtch *afp)
|
1994-05-26 06:35:07 +00:00
|
|
|
{
|
2004-12-08 19:18:07 +00:00
|
|
|
if (afp->af_getaddr != NULL) {
|
|
|
|
setmask++;
|
|
|
|
afp->af_getaddr(addr, MASK);
|
2002-06-15 15:35:24 +00:00
|
|
|
}
|
|
|
|
}
|
1999-12-07 17:39:16 +00:00
|
|
|
|
2004-12-08 19:18:07 +00:00
|
|
|
static void
|
2002-04-18 17:14:09 +00:00
|
|
|
setifbroadaddr(const char *addr, int dummy __unused, int s,
|
|
|
|
const struct afswtch *afp)
|
1994-05-26 06:35:07 +00:00
|
|
|
{
|
2004-12-08 19:18:07 +00:00
|
|
|
if (afp->af_getaddr != NULL)
|
|
|
|
afp->af_getaddr(addr, DSTADDR);
|
1994-05-26 06:35:07 +00:00
|
|
|
}
|
|
|
|
|
2004-12-08 19:18:07 +00:00
|
|
|
static void
|
2002-04-18 17:14:09 +00:00
|
|
|
setifipdst(const char *addr, int dummy __unused, int s,
|
|
|
|
const struct afswtch *afp)
|
1994-05-26 06:35:07 +00:00
|
|
|
{
|
2004-12-08 19:18:07 +00:00
|
|
|
const struct afswtch *inet;
|
|
|
|
|
|
|
|
inet = af_getbyname("inet");
|
|
|
|
if (inet == NULL)
|
|
|
|
return;
|
|
|
|
inet->af_getaddr(addr, DSTADDR);
|
1994-05-26 06:35:07 +00:00
|
|
|
setipdst++;
|
|
|
|
clearaddr = 0;
|
|
|
|
newaddr = 0;
|
|
|
|
}
|
1996-11-21 19:36:09 +00:00
|
|
|
|
2004-12-08 19:18:07 +00:00
|
|
|
static void
|
2002-04-18 17:14:09 +00:00
|
|
|
notealias(const char *addr, int param, int s, const struct afswtch *afp)
|
1994-05-26 06:35:07 +00:00
|
|
|
{
|
2004-12-08 19:18:07 +00:00
|
|
|
#define rqtosa(x) (&(((struct ifreq *)(afp->x))->ifr_addr))
|
1994-05-26 06:35:07 +00:00
|
|
|
if (setaddr && doalias == 0 && param < 0)
|
2002-10-02 02:17:59 +00:00
|
|
|
if (afp->af_addreq != NULL && afp->af_ridreq != NULL)
|
|
|
|
bcopy((caddr_t)rqtosa(af_addreq),
|
|
|
|
(caddr_t)rqtosa(af_ridreq),
|
|
|
|
rqtosa(af_addreq)->sa_len);
|
1994-05-26 06:35:07 +00:00
|
|
|
doalias = param;
|
|
|
|
if (param < 0) {
|
|
|
|
clearaddr = 1;
|
|
|
|
newaddr = 0;
|
|
|
|
} else
|
|
|
|
clearaddr = 0;
|
2004-12-08 19:18:07 +00:00
|
|
|
#undef rqtosa
|
1994-05-26 06:35:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*ARGSUSED*/
|
2004-12-08 19:18:07 +00:00
|
|
|
static void
|
2002-04-18 17:14:09 +00:00
|
|
|
setifdstaddr(const char *addr, int param __unused, int s,
|
|
|
|
const struct afswtch *afp)
|
1994-05-26 06:35:07 +00:00
|
|
|
{
|
2004-12-08 19:18:07 +00:00
|
|
|
if (afp->af_getaddr != NULL)
|
|
|
|
afp->af_getaddr(addr, DSTADDR);
|
1994-05-26 06:35:07 +00:00
|
|
|
}
|
|
|
|
|
1999-03-15 01:22:01 +00:00
|
|
|
/*
|
|
|
|
* Note: doing an SIOCIGIFFLAGS scribbles on the union portion
|
|
|
|
* of the ifreq structure, which may confuse other parts of ifconfig.
|
|
|
|
* Make a private copy so we can avoid that.
|
|
|
|
*/
|
2004-12-08 19:18:07 +00:00
|
|
|
static void
|
2002-04-18 17:14:09 +00:00
|
|
|
setifflags(const char *vname, int value, int s, const struct afswtch *afp)
|
1994-05-26 06:35:07 +00:00
|
|
|
{
|
1999-03-15 01:22:01 +00:00
|
|
|
struct ifreq my_ifr;
|
|
|
|
|
|
|
|
bcopy((char *)&ifr, (char *)&my_ifr, sizeof(struct ifreq));
|
|
|
|
|
|
|
|
if (ioctl(s, SIOCGIFFLAGS, (caddr_t)&my_ifr) < 0) {
|
1994-05-26 06:35:07 +00:00
|
|
|
Perror("ioctl (SIOCGIFFLAGS)");
|
|
|
|
exit(1);
|
|
|
|
}
|
1999-03-15 01:22:01 +00:00
|
|
|
strncpy(my_ifr.ifr_name, name, sizeof (my_ifr.ifr_name));
|
2002-08-19 15:16:38 +00:00
|
|
|
flags = (my_ifr.ifr_flags & 0xffff) | (my_ifr.ifr_flagshigh << 16);
|
1994-05-26 06:35:07 +00:00
|
|
|
|
|
|
|
if (value < 0) {
|
|
|
|
value = -value;
|
|
|
|
flags &= ~value;
|
|
|
|
} else
|
|
|
|
flags |= value;
|
2002-08-18 07:05:00 +00:00
|
|
|
my_ifr.ifr_flags = flags & 0xffff;
|
|
|
|
my_ifr.ifr_flagshigh = flags >> 16;
|
1999-03-15 01:22:01 +00:00
|
|
|
if (ioctl(s, SIOCSIFFLAGS, (caddr_t)&my_ifr) < 0)
|
1994-05-26 06:35:07 +00:00
|
|
|
Perror(vname);
|
|
|
|
}
|
|
|
|
|
2001-09-18 17:43:30 +00:00
|
|
|
void
|
2002-04-18 17:14:09 +00:00
|
|
|
setifcap(const char *vname, int value, int s, const struct afswtch *afp)
|
2001-09-18 17:43:30 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
if (ioctl(s, SIOCGIFCAP, (caddr_t)&ifr) < 0) {
|
|
|
|
Perror("ioctl (SIOCGIFCAP)");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
flags = ifr.ifr_curcap;
|
|
|
|
if (value < 0) {
|
|
|
|
value = -value;
|
|
|
|
flags &= ~value;
|
|
|
|
} else
|
|
|
|
flags |= value;
|
|
|
|
ifr.ifr_reqcap = flags;
|
|
|
|
if (ioctl(s, SIOCSIFCAP, (caddr_t)&ifr) < 0)
|
|
|
|
Perror(vname);
|
|
|
|
}
|
|
|
|
|
2004-12-08 19:18:07 +00:00
|
|
|
static void
|
2002-04-18 17:14:09 +00:00
|
|
|
setifmetric(const char *val, int dummy __unused, int s,
|
|
|
|
const struct afswtch *afp)
|
1994-05-26 06:35:07 +00:00
|
|
|
{
|
|
|
|
strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
|
|
|
|
ifr.ifr_metric = atoi(val);
|
|
|
|
if (ioctl(s, SIOCSIFMETRIC, (caddr_t)&ifr) < 0)
|
1998-07-06 06:53:43 +00:00
|
|
|
warn("ioctl (set metric)");
|
1994-05-26 06:35:07 +00:00
|
|
|
}
|
|
|
|
|
2004-12-08 19:18:07 +00:00
|
|
|
static void
|
2002-04-18 17:14:09 +00:00
|
|
|
setifmtu(const char *val, int dummy __unused, int s,
|
|
|
|
const struct afswtch *afp)
|
1994-08-08 10:45:06 +00:00
|
|
|
{
|
|
|
|
strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
|
|
|
|
ifr.ifr_mtu = atoi(val);
|
|
|
|
if (ioctl(s, SIOCSIFMTU, (caddr_t)&ifr) < 0)
|
1998-07-06 06:53:43 +00:00
|
|
|
warn("ioctl (set mtu)");
|
1994-08-08 10:45:06 +00:00
|
|
|
}
|
|
|
|
|
2004-12-08 19:18:07 +00:00
|
|
|
static void
|
2004-02-04 02:55:46 +00:00
|
|
|
setifname(const char *val, int dummy __unused, int s,
|
|
|
|
const struct afswtch *afp)
|
|
|
|
{
|
2004-12-08 19:18:07 +00:00
|
|
|
char *newname;
|
2004-02-04 02:55:46 +00:00
|
|
|
|
|
|
|
newname = strdup(val);
|
2004-12-08 19:18:07 +00:00
|
|
|
if (newname == NULL) {
|
|
|
|
warn("no memory to set ifname");
|
|
|
|
return;
|
|
|
|
}
|
2004-02-04 02:55:46 +00:00
|
|
|
ifr.ifr_data = newname;
|
|
|
|
if (ioctl(s, SIOCSIFNAME, (caddr_t)&ifr) < 0) {
|
|
|
|
warn("ioctl (set name)");
|
|
|
|
free(newname);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
strlcpy(name, newname, sizeof(name));
|
|
|
|
free(newname);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Even if we just created the interface, we don't need to print
|
|
|
|
* its name because we just nailed it down separately.
|
|
|
|
*/
|
|
|
|
printname = 0;
|
|
|
|
}
|
|
|
|
|
2004-12-08 19:18:07 +00:00
|
|
|
/*
|
|
|
|
* Expand the compacted form of addresses as returned via the
|
|
|
|
* configuration read via sysctl().
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
rt_xaddrs(caddr_t cp, caddr_t cplim, struct rt_addrinfo *rtinfo)
|
|
|
|
{
|
|
|
|
struct sockaddr *sa;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
memset(rtinfo->rti_info, 0, sizeof(rtinfo->rti_info));
|
|
|
|
for (i = 0; (i < RTAX_MAX) && (cp < cplim); i++) {
|
|
|
|
if ((rtinfo->rti_addrs & (1 << i)) == 0)
|
|
|
|
continue;
|
|
|
|
rtinfo->rti_info[i] = sa = (struct sockaddr *)cp;
|
|
|
|
cp += SA_SIZE(sa);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1994-05-26 06:35:07 +00:00
|
|
|
#define IFFBITS \
|
1999-06-06 09:17:51 +00:00
|
|
|
"\020\1UP\2BROADCAST\3DEBUG\4LOOPBACK\5POINTOPOINT\6SMART\7RUNNING" \
|
1996-11-21 19:36:09 +00:00
|
|
|
"\10NOARP\11PROMISC\12ALLMULTI\13OACTIVE\14SIMPLEX\15LINK0\16LINK1\17LINK2" \
|
2004-03-12 23:43:40 +00:00
|
|
|
"\20MULTICAST\21POLLING\23MONITOR\24STATICARP"
|
1994-05-26 06:35:07 +00:00
|
|
|
|
2001-09-18 17:43:30 +00:00
|
|
|
#define IFCAPBITS \
|
2004-04-11 13:36:52 +00:00
|
|
|
"\020\1RXCSUM\2TXCSUM\3NETCONS\4VLAN_MTU\5VLAN_HWTAGGING\6JUMBO_MTU\7POLLING"
|
2001-09-18 17:43:30 +00:00
|
|
|
|
1994-05-26 06:35:07 +00:00
|
|
|
/*
|
|
|
|
* Print the status of the interface. If an address family was
|
2004-12-08 19:18:07 +00:00
|
|
|
* specified, show only it; otherwise, show them all.
|
1994-05-26 06:35:07 +00:00
|
|
|
*/
|
2004-12-08 19:18:07 +00:00
|
|
|
static void
|
2002-04-18 17:14:09 +00:00
|
|
|
status(const struct afswtch *afp, int addrcount, struct sockaddr_dl *sdl,
|
|
|
|
struct if_msghdr *ifm, struct ifa_msghdr *ifam)
|
1994-05-26 06:35:07 +00:00
|
|
|
{
|
1997-05-10 17:14:53 +00:00
|
|
|
struct rt_addrinfo info;
|
|
|
|
int allfamilies, s;
|
1999-06-19 18:42:31 +00:00
|
|
|
struct ifstat ifs;
|
1997-05-10 17:14:53 +00:00
|
|
|
|
|
|
|
if (afp == NULL) {
|
|
|
|
allfamilies = 1;
|
2004-12-08 19:18:07 +00:00
|
|
|
afp = af_getbyname("inet");
|
1997-05-10 17:14:53 +00:00
|
|
|
} else
|
|
|
|
allfamilies = 0;
|
|
|
|
|
2000-07-12 16:41:47 +00:00
|
|
|
ifr.ifr_addr.sa_family = afp->af_af == AF_LINK ? AF_INET : afp->af_af;
|
2001-10-17 18:30:13 +00:00
|
|
|
strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
|
1997-05-10 17:14:53 +00:00
|
|
|
|
2004-12-08 19:18:07 +00:00
|
|
|
s = socket(ifr.ifr_addr.sa_family, SOCK_DGRAM, 0);
|
|
|
|
if (s < 0)
|
|
|
|
err(1, "socket(family %u,SOCK_DGRAM)", ifr.ifr_addr.sa_family);
|
1997-05-10 17:14:53 +00:00
|
|
|
|
1994-05-26 06:35:07 +00:00
|
|
|
printf("%s: ", name);
|
|
|
|
printb("flags", flags, IFFBITS);
|
2001-12-03 15:12:45 +00:00
|
|
|
if (ifm->ifm_data.ifi_metric)
|
|
|
|
printf(" metric %ld", ifm->ifm_data.ifi_metric);
|
|
|
|
if (ifm->ifm_data.ifi_mtu)
|
|
|
|
printf(" mtu %ld", ifm->ifm_data.ifi_mtu);
|
1994-05-26 06:35:07 +00:00
|
|
|
putchar('\n');
|
1996-01-01 09:05:22 +00:00
|
|
|
|
2001-09-18 17:43:30 +00:00
|
|
|
if (ioctl(s, SIOCGIFCAP, (caddr_t)&ifr) == 0) {
|
|
|
|
if (ifr.ifr_curcap != 0) {
|
|
|
|
printb("\toptions", ifr.ifr_curcap, IFCAPBITS);
|
|
|
|
putchar('\n');
|
|
|
|
}
|
|
|
|
if (supmedia && ifr.ifr_reqcap != 0) {
|
2004-12-08 19:18:07 +00:00
|
|
|
printb("\tcapabilities", ifr.ifr_reqcap, IFCAPBITS);
|
2001-09-18 17:43:30 +00:00
|
|
|
putchar('\n');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-06-11 12:39:29 +00:00
|
|
|
tunnel_status(s);
|
|
|
|
|
1997-05-10 17:14:53 +00:00
|
|
|
while (addrcount > 0) {
|
1996-01-01 09:05:22 +00:00
|
|
|
info.rti_addrs = ifam->ifam_addrs;
|
|
|
|
/* Expand the compacted addresses */
|
|
|
|
rt_xaddrs((char *)(ifam + 1), ifam->ifam_msglen + (char *)ifam,
|
|
|
|
&info);
|
|
|
|
|
2004-12-08 19:18:07 +00:00
|
|
|
if (allfamilies) {
|
|
|
|
const struct afswtch *p;
|
|
|
|
p = af_getbyfamily(info.rti_info[RTAX_IFA]->sa_family);
|
|
|
|
if (p != NULL)
|
|
|
|
p->af_status(s, &info);
|
|
|
|
} else if (afp->af_af == info.rti_info[RTAX_IFA]->sa_family)
|
|
|
|
afp->af_status(s, &info);
|
1997-05-10 17:14:53 +00:00
|
|
|
addrcount--;
|
|
|
|
ifam = (struct ifa_msghdr *)((char *)ifam + ifam->ifam_msglen);
|
1994-05-26 06:35:07 +00:00
|
|
|
}
|
2004-12-31 19:46:27 +00:00
|
|
|
if (allfamilies || afp->af_af == AF_LINK) {
|
|
|
|
const struct afswtch *lafp;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Hack; the link level address is received separately
|
|
|
|
* from the routing information so any address is not
|
|
|
|
* handled above. Cobble together an entry and invoke
|
|
|
|
* the status method specially.
|
|
|
|
*/
|
|
|
|
lafp = af_getbyname("lladdr");
|
|
|
|
if (lafp != NULL) {
|
|
|
|
info.rti_info[RTAX_IFA] = (struct sockaddr *)sdl;
|
|
|
|
lafp->af_status(s, &info);
|
|
|
|
}
|
|
|
|
}
|
2004-12-08 19:18:07 +00:00
|
|
|
if (allfamilies)
|
2004-12-31 19:46:27 +00:00
|
|
|
af_other_status(s);
|
|
|
|
else if (afp->af_other_status != NULL)
|
|
|
|
afp->af_other_status(s);
|
2004-12-08 19:18:07 +00:00
|
|
|
|
1999-06-19 20:07:24 +00:00
|
|
|
strncpy(ifs.ifs_name, name, sizeof ifs.ifs_name);
|
|
|
|
if (ioctl(s, SIOCGIFSTATUS, &ifs) == 0)
|
|
|
|
printf("%s", ifs.ascii);
|
|
|
|
|
1997-05-10 17:14:53 +00:00
|
|
|
close(s);
|
|
|
|
return;
|
1994-05-26 06:35:07 +00:00
|
|
|
}
|
|
|
|
|
2004-12-08 19:18:07 +00:00
|
|
|
static void
|
2002-04-18 17:14:09 +00:00
|
|
|
tunnel_status(int s)
|
2001-06-11 12:39:29 +00:00
|
|
|
{
|
2004-12-08 19:18:07 +00:00
|
|
|
af_all_tunnel_status(s);
|
1994-12-30 22:42:41 +00:00
|
|
|
}
|
1995-05-30 06:12:45 +00:00
|
|
|
|
1996-11-21 19:36:09 +00:00
|
|
|
void
|
2002-04-18 17:14:09 +00:00
|
|
|
Perror(const char *cmd)
|
1994-05-26 06:35:07 +00:00
|
|
|
{
|
|
|
|
switch (errno) {
|
|
|
|
|
|
|
|
case ENXIO:
|
|
|
|
errx(1, "%s: no such interface", cmd);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case EPERM:
|
|
|
|
errx(1, "%s: permission denied", cmd);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
err(1, "%s", cmd);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Print a value a la the %b format of the kernel's printf
|
|
|
|
*/
|
1996-11-21 19:36:09 +00:00
|
|
|
void
|
2002-04-18 17:14:09 +00:00
|
|
|
printb(const char *s, unsigned v, const char *bits)
|
1994-05-26 06:35:07 +00:00
|
|
|
{
|
2002-03-20 17:55:10 +00:00
|
|
|
int i, any = 0;
|
|
|
|
char c;
|
1994-05-26 06:35:07 +00:00
|
|
|
|
|
|
|
if (bits && *bits == 8)
|
|
|
|
printf("%s=%o", s, v);
|
|
|
|
else
|
|
|
|
printf("%s=%x", s, v);
|
|
|
|
bits++;
|
|
|
|
if (bits) {
|
|
|
|
putchar('<');
|
1996-11-21 19:36:09 +00:00
|
|
|
while ((i = *bits++) != '\0') {
|
1994-05-26 06:35:07 +00:00
|
|
|
if (v & (1 << (i-1))) {
|
|
|
|
if (any)
|
|
|
|
putchar(',');
|
|
|
|
any = 1;
|
|
|
|
for (; (c = *bits) > 32; bits++)
|
|
|
|
putchar(c);
|
|
|
|
} else
|
|
|
|
for (; *bits > 32; bits++)
|
|
|
|
;
|
|
|
|
}
|
|
|
|
putchar('>');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-09-20 07:58:08 +00:00
|
|
|
void
|
2002-04-18 17:14:09 +00:00
|
|
|
ifmaybeload(char *name)
|
1999-09-20 07:58:08 +00:00
|
|
|
{
|
|
|
|
struct module_stat mstat;
|
|
|
|
int fileid, modid;
|
|
|
|
char ifkind[35], *cp, *dp;
|
|
|
|
|
|
|
|
/* turn interface and unit into module name */
|
|
|
|
strcpy(ifkind, "if_");
|
2001-07-02 20:52:34 +00:00
|
|
|
for (cp = name, dp = ifkind + 3;
|
|
|
|
(*cp != 0) && !isdigit(*cp); cp++, dp++)
|
1999-09-20 07:58:08 +00:00
|
|
|
*dp = *cp;
|
|
|
|
*dp = 0;
|
|
|
|
|
|
|
|
/* scan files in kernel */
|
|
|
|
mstat.version = sizeof(struct module_stat);
|
|
|
|
for (fileid = kldnext(0); fileid > 0; fileid = kldnext(fileid)) {
|
|
|
|
/* scan modules in file */
|
|
|
|
for (modid = kldfirstmod(fileid); modid > 0;
|
|
|
|
modid = modfnext(modid)) {
|
|
|
|
if (modstat(modid, &mstat) < 0)
|
|
|
|
continue;
|
|
|
|
/* strip bus name if present */
|
|
|
|
if ((cp = strchr(mstat.name, '/')) != NULL) {
|
|
|
|
cp++;
|
|
|
|
} else {
|
|
|
|
cp = mstat.name;
|
|
|
|
}
|
|
|
|
/* already loaded? */
|
2004-02-27 06:43:14 +00:00
|
|
|
if (strncmp(name, cp, strlen(cp)) == 0 ||
|
|
|
|
strncmp(ifkind, cp, strlen(cp)) == 0)
|
1999-09-20 07:58:08 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* not present, we should try to load it */
|
|
|
|
kldload(ifkind);
|
|
|
|
}
|
2001-07-02 20:52:34 +00:00
|
|
|
|
2004-12-08 19:18:07 +00:00
|
|
|
static struct cmd basic_cmds[] = {
|
|
|
|
DEF_CMD("up", IFF_UP, setifflags),
|
|
|
|
DEF_CMD("down", -IFF_UP, 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),
|
|
|
|
DEF_CMD("promisc", IFF_PPROMISC, setifflags),
|
|
|
|
DEF_CMD("-promisc", -IFF_PPROMISC, setifflags),
|
|
|
|
DEF_CMD("add", IFF_UP, notealias),
|
|
|
|
DEF_CMD("alias", IFF_UP, notealias),
|
|
|
|
DEF_CMD("-alias", -IFF_UP, notealias),
|
|
|
|
DEF_CMD("delete", -IFF_UP, notealias),
|
|
|
|
DEF_CMD("remove", -IFF_UP, notealias),
|
|
|
|
#ifdef notdef
|
|
|
|
#define EN_SWABIPS 0x1000
|
|
|
|
DEF_CMD("swabips", EN_SWABIPS, setifflags),
|
|
|
|
DEF_CMD("-swabips", -EN_SWABIPS, setifflags),
|
|
|
|
#endif
|
|
|
|
DEF_CMD_ARG("netmask", setifnetmask),
|
|
|
|
DEF_CMD_ARG("metric", setifmetric),
|
|
|
|
DEF_CMD_ARG("broadcast", setifbroadaddr),
|
|
|
|
DEF_CMD_ARG("ipdst", setifipdst),
|
|
|
|
DEF_CMD_ARG2("tunnel", settunnel),
|
|
|
|
DEF_CMD("deletetunnel", 0, deletetunnel),
|
|
|
|
DEF_CMD("link0", IFF_LINK0, setifflags),
|
|
|
|
DEF_CMD("-link0", -IFF_LINK0, setifflags),
|
|
|
|
DEF_CMD("link1", IFF_LINK1, setifflags),
|
|
|
|
DEF_CMD("-link1", -IFF_LINK1, setifflags),
|
|
|
|
DEF_CMD("link2", IFF_LINK2, setifflags),
|
|
|
|
DEF_CMD("-link2", -IFF_LINK2, setifflags),
|
|
|
|
DEF_CMD("monitor", IFF_MONITOR, setifflags),
|
|
|
|
DEF_CMD("-monitor", -IFF_MONITOR, setifflags),
|
|
|
|
DEF_CMD("staticarp", IFF_STATICARP, setifflags),
|
|
|
|
DEF_CMD("-staticarp", -IFF_STATICARP, setifflags),
|
|
|
|
DEF_CMD("rxcsum", IFCAP_RXCSUM, setifcap),
|
|
|
|
DEF_CMD("-rxcsum", -IFCAP_RXCSUM, setifcap),
|
|
|
|
DEF_CMD("txcsum", IFCAP_TXCSUM, setifcap),
|
|
|
|
DEF_CMD("-txcsum", -IFCAP_TXCSUM, setifcap),
|
|
|
|
DEF_CMD("netcons", IFCAP_NETCONS, setifcap),
|
|
|
|
DEF_CMD("-netcons", -IFCAP_NETCONS, setifcap),
|
|
|
|
DEF_CMD("polling", IFCAP_POLLING, setifcap),
|
|
|
|
DEF_CMD("-polling", -IFCAP_POLLING, setifcap),
|
|
|
|
DEF_CMD("normal", -IFF_LINK0, setifflags),
|
|
|
|
DEF_CMD("compress", IFF_LINK0, setifflags),
|
|
|
|
DEF_CMD("noicmp", IFF_LINK1, setifflags),
|
|
|
|
DEF_CMD_ARG("mtu", setifmtu),
|
|
|
|
DEF_CMD_ARG("name", setifname),
|
|
|
|
};
|
2001-07-02 20:52:34 +00:00
|
|
|
|
2004-12-08 19:18:07 +00:00
|
|
|
static __constructor void
|
|
|
|
ifconfig_ctor(void)
|
2001-07-02 20:52:34 +00:00
|
|
|
{
|
2004-12-08 19:18:07 +00:00
|
|
|
#define N(a) (sizeof(a) / sizeof(a[0]))
|
|
|
|
int i;
|
2001-07-02 20:52:34 +00:00
|
|
|
|
2004-12-08 19:18:07 +00:00
|
|
|
for (i = 0; i < N(basic_cmds); i++)
|
|
|
|
cmd_register(&basic_cmds[i]);
|
|
|
|
#undef N
|
2001-07-02 20:52:34 +00:00
|
|
|
}
|