NetBSD/sbin/ifconfig/ifconfig.c

2843 lines
72 KiB
C
Raw Normal View History

2005-03-19 06:56:06 +03:00
/* $NetBSD: ifconfig.c,v 1.156 2005/03/19 03:56:06 thorpej Exp $ */
/*-
2000-01-25 04:03:21 +03:00
* Copyright (c) 1997, 1998, 2000 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
* NASA Ames Research Center.
*
* 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.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the NetBSD
* Foundation, Inc. and its contributors.
* 4. Neither the name of The NetBSD Foundation 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 NETBSD FOUNDATION, INC. 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 FOUNDATION 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.
*/
1993-03-21 12:45:37 +03:00
/*
1994-09-23 05:38:45 +04:00
* Copyright (c) 1983, 1993
* The Regents of the University of California. All rights reserved.
1993-03-21 12:45:37 +03:00
*
* 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.
* 3. Neither the name of the University nor the names of its contributors
1993-03-21 12:45:37 +03:00
* 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.
*/
#include <sys/cdefs.h>
1993-03-21 12:45:37 +03:00
#ifndef lint
__COPYRIGHT("@(#) Copyright (c) 1983, 1993\n\
The Regents of the University of California. All rights reserved.\n");
1993-03-21 12:45:37 +03:00
#endif /* not lint */
#ifndef lint
#if 0
static char sccsid[] = "@(#)ifconfig.c 8.2 (Berkeley) 2/16/94";
#else
2005-03-19 06:56:06 +03:00
__RCSID("$NetBSD: ifconfig.c,v 1.156 2005/03/19 03:56:06 thorpej Exp $");
#endif
1993-03-21 12:45:37 +03:00
#endif /* not lint */
#include <sys/param.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <net/if_dl.h>
#include <net/if_media.h>
#include <net/if_ether.h>
#include <net80211/ieee80211.h>
#include <net80211/ieee80211_ioctl.h>
1993-03-21 12:45:37 +03:00
#include <netinet/in.h>
#include <netinet/in_var.h>
#ifdef INET6
#include <netinet6/nd6.h>
#endif
1993-03-21 12:45:37 +03:00
#include <arpa/inet.h>
#include <netatalk/at.h>
1993-03-21 12:45:37 +03:00
#define NSIP
#include <netns/ns.h>
#include <netns/ns_if.h>
1994-09-23 05:38:45 +04:00
#include <netdb.h>
1993-03-21 12:45:37 +03:00
#define EON
#include <netiso/iso.h>
#include <netiso/iso_var.h>
#include <sys/protosw.h>
#include <ctype.h>
1994-09-23 05:38:45 +04:00
#include <err.h>
#include <errno.h>
#include <stddef.h>
1994-09-23 05:38:45 +04:00
#include <stdio.h>
1993-03-21 12:45:37 +03:00
#include <stdlib.h>
#include <string.h>
1994-09-23 05:38:45 +04:00
#include <unistd.h>
#include <ifaddrs.h>
2002-09-21 01:21:53 +04:00
#include <util.h>
1993-03-21 12:45:37 +03:00
2005-03-19 06:56:06 +03:00
#include "extern.h"
#include "agr.h"
#include "vlan.h"
2000-04-20 06:08:55 +04:00
struct ifreq ifr, ridreq;
struct ifaliasreq addreq __attribute__((aligned(4)));
struct in_aliasreq in_addreq;
#ifdef INET6
struct in6_ifreq ifr6;
struct in6_ifreq in6_ridreq;
struct in6_aliasreq in6_addreq;
#endif
struct iso_ifreq iso_ridreq;
1993-03-21 12:45:37 +03:00
struct iso_aliasreq iso_addreq;
struct sockaddr_in netmask;
struct netrange at_nr; /* AppleTalk net range */
1993-03-21 12:45:37 +03:00
char name[30];
2002-09-21 01:21:53 +04:00
u_short flags;
int setaddr, setipdst, doalias;
2002-06-14 05:04:41 +04:00
u_long metric, mtu;
int clearaddr, s;
int newaddr = -1;
int conflicting = 0;
int nsellength = 1;
int af;
int aflag, bflag, Cflag, dflag, lflag, mflag, sflag, uflag, vflag, zflag;
#ifdef INET6
int Lflag;
#endif
int explicit_prefix = 0;
1993-03-21 12:45:37 +03:00
struct ifcapreq g_ifcr;
int g_ifcr_updated;
2004-10-12 00:13:37 +04:00
void notealias(const char *, int);
void notrailers(const char *, int);
void setifaddr(const char *, int);
void setifdstaddr(const char *, int);
void setifflags(const char *, int);
void setifcaps(const char *, int);
void setifbroadaddr(const char *, int);
void setifipdst(const char *, int);
void setifmetric(const char *, int);
void setifmtu(const char *, int);
void setifnwid(const char *, int);
void setifnwkey(const char *, int);
void setifbssid(const char *, int);
void setifchan(const char *, int);
void setifpowersave(const char *, int);
void setifpowersavesleep(const char *, int);
void setifnetmask(const char *, int);
void setifprefixlen(const char *, int);
void setnsellength(const char *, int);
void setsnpaoffset(const char *, int);
void setatrange(const char *, int);
void setatphase(const char *, int);
void settunnel(const char *, const char *);
void deletetunnel(const char *, int);
#ifdef INET6
2004-10-12 00:13:37 +04:00
void setia6flags(const char *, int);
void setia6pltime(const char *, int);
void setia6vltime(const char *, int);
void setia6lifetime(const char *, const char *);
void setia6eui64(const char *, int);
#endif
2004-10-12 00:13:37 +04:00
void checkatrange(struct sockaddr_at *);
void setmedia(const char *, int);
void setmediamode(const char *, int);
void setmediaopt(const char *, int);
void unsetmediaopt(const char *, int);
void setmediainst(const char *, int);
void clone_create(const char *, int);
void clone_destroy(const char *, int);
void fixnsel(struct sockaddr_iso *);
int main(int, char *[]);
1993-03-21 12:45:37 +03:00
/*
* Media stuff. Whenever a media command is first performed, the
* currently select media is grabbed for this interface. If `media'
* is given, the current media word is modifed. `mediaopt' commands
* only modify the set and clear words. They then operate on the
* current media word later.
*/
int media_current;
int mediaopt_set;
int mediaopt_clear;
int actions; /* Actions performed */
#define A_MEDIA 0x0001 /* media command */
#define A_MEDIAOPTSET 0x0002 /* mediaopt command */
#define A_MEDIAOPTCLR 0x0004 /* -mediaopt command */
#define A_MEDIAOPT (A_MEDIAOPTSET|A_MEDIAOPTCLR)
#define A_MEDIAINST 0x0008 /* instance or inst command */
#define A_MEDIAMODE 0x0010 /* mode command */
1993-03-21 12:45:37 +03:00
#define NEXTARG 0xffffff
#define NEXTARG2 0xfffffe
1993-03-21 12:45:37 +03:00
const struct cmd {
const char *c_name;
int c_parameter; /* NEXTARG means next argv */
int c_action; /* defered action */
2004-10-12 00:13:37 +04:00
void (*c_func)(const char *, int);
void (*c_func2)(const char *, const char *);
1993-03-21 12:45:37 +03:00
} cmds[] = {
{ "up", IFF_UP, 0, setifflags } ,
{ "down", -IFF_UP, 0, setifflags },
{ "trailers", -1, 0, notrailers },
{ "-trailers", 1, 0, notrailers },
{ "arp", -IFF_NOARP, 0, setifflags },
{ "-arp", IFF_NOARP, 0, setifflags },
{ "debug", IFF_DEBUG, 0, setifflags },
{ "-debug", -IFF_DEBUG, 0, setifflags },
{ "alias", IFF_UP, 0, notealias },
{ "-alias", -IFF_UP, 0, notealias },
{ "delete", -IFF_UP, 0, notealias },
1993-03-21 12:45:37 +03:00
#ifdef notdef
#define EN_SWABIPS 0x1000
{ "swabips", EN_SWABIPS, 0, setifflags },
{ "-swabips", -EN_SWABIPS, 0, setifflags },
1993-03-21 12:45:37 +03:00
#endif
{ "netmask", NEXTARG, 0, setifnetmask },
{ "metric", NEXTARG, 0, setifmetric },
{ "mtu", NEXTARG, 0, setifmtu },
{ "bssid", NEXTARG, 0, setifbssid },
{ "-bssid", -1, 0, setifbssid },
{ "chan", NEXTARG, 0, setifchan },
{ "-chan", -1, 0, setifchan },
{ "ssid", NEXTARG, 0, setifnwid },
{ "nwid", NEXTARG, 0, setifnwid },
{ "nwkey", NEXTARG, 0, setifnwkey },
{ "-nwkey", -1, 0, setifnwkey },
{ "powersave", 1, 0, setifpowersave },
{ "-powersave", 0, 0, setifpowersave },
{ "powersavesleep", NEXTARG, 0, setifpowersavesleep },
{ "broadcast", NEXTARG, 0, setifbroadaddr },
{ "ipdst", NEXTARG, 0, setifipdst },
{ "prefixlen", NEXTARG, 0, setifprefixlen},
#ifdef INET6
{ "anycast", IN6_IFF_ANYCAST, 0, setia6flags },
{ "-anycast", -IN6_IFF_ANYCAST, 0, setia6flags },
{ "tentative", IN6_IFF_TENTATIVE, 0, setia6flags },
{ "-tentative", -IN6_IFF_TENTATIVE, 0, setia6flags },
{ "deprecated", IN6_IFF_DEPRECATED, 0, setia6flags },
{ "-deprecated", -IN6_IFF_DEPRECATED, 0, setia6flags },
{ "pltime", NEXTARG, 0, setia6pltime },
{ "vltime", NEXTARG, 0, setia6vltime },
{ "eui64", 0, 0, setia6eui64 },
#endif /*INET6*/
#ifndef INET_ONLY
{ "range", NEXTARG, 0, setatrange },
{ "phase", NEXTARG, 0, setatphase },
{ "snpaoffset", NEXTARG, 0, setsnpaoffset },
{ "nsellength", NEXTARG, 0, setnsellength },
#endif /* INET_ONLY */
{ "tunnel", NEXTARG2, 0, NULL,
settunnel } ,
{ "deletetunnel", 0, 0, deletetunnel },
{ "vlan", NEXTARG, 0, setvlan } ,
{ "vlanif", NEXTARG, 0, setvlanif } ,
{ "-vlanif", 0, 0, unsetvlanif } ,
2000-07-02 04:28:00 +04:00
#if 0
/* XXX `create' special-cased below */
{ "create", 0, 0, clone_create } ,
#endif
{ "destroy", 0, 0, clone_destroy } ,
{ "link0", IFF_LINK0, 0, setifflags } ,
{ "-link0", -IFF_LINK0, 0, setifflags } ,
{ "link1", IFF_LINK1, 0, setifflags } ,
{ "-link1", -IFF_LINK1, 0, setifflags } ,
{ "link2", IFF_LINK2, 0, setifflags } ,
{ "-link2", -IFF_LINK2, 0, setifflags } ,
{ "media", NEXTARG, A_MEDIA, setmedia },
{ "mediaopt", NEXTARG, A_MEDIAOPTSET, setmediaopt },
{ "-mediaopt", NEXTARG, A_MEDIAOPTCLR, unsetmediaopt },
{ "mode", NEXTARG, A_MEDIAMODE, setmediamode },
{ "instance", NEXTARG, A_MEDIAINST, setmediainst },
{ "inst", NEXTARG, A_MEDIAINST, setmediainst },
{ "ip4csum", IFCAP_CSUM_IPv4,0, setifcaps },
{ "-ip4csum", -IFCAP_CSUM_IPv4,0, setifcaps },
{ "tcp4csum", IFCAP_CSUM_TCPv4,0, setifcaps },
{ "-tcp4csum", -IFCAP_CSUM_TCPv4,0, setifcaps },
{ "udp4csum", IFCAP_CSUM_UDPv4,0, setifcaps },
{ "-udp4csum", -IFCAP_CSUM_UDPv4,0, setifcaps },
{ "tcp6csum", IFCAP_CSUM_TCPv6,0, setifcaps },
{ "-tcp6csum", -IFCAP_CSUM_TCPv6,0, setifcaps },
{ "udp6csum", IFCAP_CSUM_UDPv6,0, setifcaps },
{ "-udp6csum", -IFCAP_CSUM_UDPv6,0, setifcaps },
{ "tcp4csum-rx",IFCAP_CSUM_TCPv4_Rx,0, setifcaps },
{ "-tcp4csum-rx",-IFCAP_CSUM_TCPv4_Rx,0, setifcaps },
{ "udp4csum-rx",IFCAP_CSUM_UDPv4_Rx,0, setifcaps },
{ "-udp4csum-rx",-IFCAP_CSUM_UDPv4_Rx,0, setifcaps },
{ "tso4", IFCAP_TSOv4, 0, setifcaps },
{ "-tso4", -IFCAP_TSOv4, 0, setifcaps },
{ "agrport", NEXTARG, 0, agraddport } ,
{ "-agrport", NEXTARG, 0, agrremport } ,
{ 0, 0, 0, setifaddr },
{ 0, 0, 0, setifdstaddr },
1993-03-21 12:45:37 +03:00
};
2004-10-12 00:13:37 +04:00
void adjust_nsellength(void);
int getinfo(struct ifreq *);
int carrier(void);
void getsock(int);
void printall(const char *);
void list_cloners(void);
int prefix(void *, int);
void status(const struct sockaddr_dl *);
void usage(void);
const char *get_string(const char *, const char *, u_int8_t *, int *);
void print_string(const u_int8_t *, int);
char *sec2str(time_t);
void print_media_word(int, const char *);
2004-10-12 00:13:37 +04:00
void process_media_commands(void);
void init_current_media(void);
1993-03-21 12:45:37 +03:00
/*
1994-09-23 05:38:45 +04:00
* XNS support liberally adapted from code written at the University of
* Maryland principally by James O'Toole and Chris Torek.
1993-03-21 12:45:37 +03:00
*/
2004-10-12 00:13:37 +04:00
void in_alias(struct ifreq *);
void in_status(int);
void in_getaddr(const char *, int);
void in_getprefix(const char *, int);
#ifdef INET6
2004-10-12 00:13:37 +04:00
void in6_fillscopeid(struct sockaddr_in6 *sin6);
void in6_alias(struct in6_ifreq *);
void in6_status(int);
void in6_getaddr(const char *, int);
void in6_getprefix(const char *, int);
#endif
2004-10-12 00:13:37 +04:00
void at_status(int);
void at_getaddr(const char *, int);
void xns_status(int);
void xns_getaddr(const char *, int);
void iso_status(int);
void iso_getaddr(const char *, int);
void ieee80211_statistics(void);
2004-10-12 00:13:37 +04:00
void ieee80211_status(void);
void tunnel_status(void);
1993-03-21 12:45:37 +03:00
/* Known address families */
struct afswtch {
const char *af_name;
1993-03-21 12:45:37 +03:00
short af_af;
2004-10-12 00:13:37 +04:00
void (*af_status)(int);
void (*af_getaddr)(const char *, int);
void (*af_getprefix)(const char *, int);
1994-10-31 07:24:09 +03:00
u_long af_difaddr;
u_long af_aifaddr;
u_long af_gifaddr;
void *af_ridreq;
void *af_addreq;
1993-03-21 12:45:37 +03:00
} afs[] = {
2001-01-17 09:07:32 +03:00
{ "inet", AF_INET, in_status, in_getaddr, in_getprefix,
SIOCDIFADDR, SIOCAIFADDR, SIOCGIFADDR, &ridreq, &in_addreq },
#ifdef INET6
{ "inet6", AF_INET6, in6_status, in6_getaddr, in6_getprefix,
SIOCDIFADDR_IN6, SIOCAIFADDR_IN6,
/*
* Deleting the first address before setting new one is
* not prefered way in this protocol.
*/
0,
&in6_ridreq, &in6_addreq },
#endif
#ifndef INET_ONLY /* small version, for boot media */
{ "atalk", AF_APPLETALK, at_status, at_getaddr, NULL,
SIOCDIFADDR, SIOCAIFADDR, SIOCGIFADDR, &addreq, &addreq },
{ "ns", AF_NS, xns_status, xns_getaddr, NULL,
SIOCDIFADDR, SIOCAIFADDR, SIOCGIFADDR, &ridreq, &addreq },
{ "iso", AF_ISO, iso_status, iso_getaddr, NULL,
SIOCDIFADDR_ISO, SIOCAIFADDR_ISO, SIOCGIFADDR_ISO,
&iso_ridreq, &iso_addreq },
#endif /* INET_ONLY */
1993-03-21 12:45:37 +03:00
{ 0, 0, 0, 0 }
};
struct afswtch *afp; /*the address family being set or asked about*/
2004-10-12 00:13:37 +04:00
struct afswtch *lookup_af(const char *);
int
2004-10-12 00:13:37 +04:00
main(int argc, char *argv[])
1993-03-21 12:45:37 +03:00
{
int ch;
/* Parse command-line options */
aflag = mflag = vflag = zflag = 0;
while ((ch = getopt(argc, argv, "AabCdlmsuvz"
#ifdef INET6
"L"
#endif
)) != -1) {
switch (ch) {
case 'A':
warnx("-A is deprecated");
break;
case 'a':
aflag = 1;
break;
case 'b':
bflag = 1;
break;
case 'C':
Cflag = 1;
break;
case 'd':
dflag = 1;
break;
#ifdef INET6
case 'L':
Lflag = 1;
break;
#endif
case 'l':
lflag = 1;
break;
case 'm':
mflag = 1;
break;
case 's':
sflag = 1;
break;
case 'u':
uflag = 1;
break;
case 'v':
vflag = 1;
break;
case 'z':
zflag = 1;
break;
default:
usage();
/* NOTREACHED */
}
1993-03-21 12:45:37 +03:00
}
argc -= optind;
argv += optind;
/*
* -l means "list all interfaces", and is mutally exclusive with
* all other flags/commands.
*
* -C means "list all names of cloners", and it mutually exclusive
* with all other flags/commands.
*
* -a means "print status of all interfaces".
*/
if ((lflag || Cflag) && (aflag || mflag || vflag || argc || zflag))
usage();
#ifdef INET6
if ((lflag || Cflag) && Lflag)
usage();
#endif
if (lflag && Cflag)
usage();
if (Cflag) {
if (argc)
usage();
list_cloners();
exit(0);
}
if (aflag || lflag) {
if (argc > 1)
usage();
else if (argc == 1) {
afp = lookup_af(argv[0]);
if (afp == NULL)
usage();
}
if (afp)
af = ifr.ifr_addr.sa_family = afp->af_af;
else
af = ifr.ifr_addr.sa_family = afs[0].af_af;
printall(NULL);
1993-05-04 13:26:52 +04:00
exit(0);
}
/* Make sure there's an interface name. */
if (argc < 1)
usage();
if (strlcpy(name, argv[0], sizeof(name)) >= sizeof(name))
errx(1, "interface name '%s' too long", argv[0]);
argc--; argv++;
2000-07-02 04:28:00 +04:00
/*
* NOTE: We must special-case the `create' command right
* here as we would otherwise fail in getinfo().
*/
if (argc > 0 && strcmp(argv[0], "create") == 0) {
clone_create(argv[0], 0);
argc--, argv++;
if (argc == 0)
exit(0);
}
/* Check for address family. */
afp = NULL;
if (argc > 0) {
afp = lookup_af(argv[0]);
if (afp != NULL) {
argv++;
argc--;
}
}
/* Initialize af, just for use in getinfo(). */
if (afp == NULL)
af = afs->af_af;
1999-05-18 22:01:08 +04:00
else
af = afp->af_af;
/* Get information about the interface. */
(void) strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
1994-09-23 05:38:45 +04:00
if (getinfo(&ifr) < 0)
1993-03-21 12:45:37 +03:00
exit(1);
if (sflag) {
if (argc != 0)
usage();
else
exit(carrier());
}
/* No more arguments means interface status. */
1994-09-23 05:38:45 +04:00
if (argc == 0) {
printall(name);
1993-05-04 13:26:52 +04:00
exit(0);
}
/* The following operations assume inet family as the default. */
if (afp == NULL)
afp = afs;
af = ifr.ifr_addr.sa_family = afp->af_af;
#ifdef INET6
/* initialization */
in6_addreq.ifra_lifetime.ia6t_pltime = ND6_INFINITE_LIFETIME;
in6_addreq.ifra_lifetime.ia6t_vltime = ND6_INFINITE_LIFETIME;
#endif
/* Process commands. */
1993-03-21 12:45:37 +03:00
while (argc > 0) {
const struct cmd *p;
1993-03-21 12:45:37 +03:00
for (p = cmds; p->c_name; p++)
if (strcmp(argv[0], p->c_name) == 0)
1993-03-21 12:45:37 +03:00
break;
if (p->c_name == 0 && setaddr) {
1999-07-26 23:38:36 +04:00
if ((flags & IFF_POINTOPOINT) == 0) {
errx(EXIT_FAILURE,
"can't set destination address %s",
"on non-point-to-point link");
1999-07-26 23:38:36 +04:00
}
1993-03-21 12:45:37 +03:00
p++; /* got src, do dst */
}
if (p->c_func != NULL || p->c_func2 != NULL) {
1993-03-21 12:45:37 +03:00
if (p->c_parameter == NEXTARG) {
if (argc < 2)
errx(EXIT_FAILURE,
"'%s' requires argument",
p->c_name);
(*p->c_func)(argv[1], 0);
1993-03-21 12:45:37 +03:00
argc--, argv++;
} else if (p->c_parameter == NEXTARG2) {
if (argc < 3)
errx(EXIT_FAILURE,
"'%s' requires 2 arguments",
p->c_name);
(*p->c_func2)(argv[1], argv[2]);
argc -= 2, argv += 2;
1993-03-21 12:45:37 +03:00
} else
(*p->c_func)(argv[0], p->c_parameter);
actions |= p->c_action;
1993-03-21 12:45:37 +03:00
}
argc--, argv++;
}
/*
* See if multiple alias, -alias, or delete commands were
* specified. More than one constitutes an invalid command line
*/
if (conflicting > 1)
2004-07-27 18:25:19 +04:00
errx(EXIT_FAILURE,
"Only one use of alias, -alias or delete is valid.");
/* Process any media commands that may have been issued. */
process_media_commands();
if (af == AF_INET6 && explicit_prefix == 0) {
/*
* Aggregatable address architecture defines all prefixes
* are 64. So, it is convenient to set prefixlen to 64 if
* it is not specified.
*/
setifprefixlen("64", 0);
/* in6_getprefix("64", MASK) if MASK is available here... */
}
#ifndef INET_ONLY
1993-03-21 12:45:37 +03:00
if (af == AF_ISO)
adjust_nsellength();
if (af == AF_APPLETALK)
checkatrange((struct sockaddr_at *) &addreq.ifra_addr);
1993-03-21 12:45:37 +03:00
if (setipdst && af==AF_NS) {
struct nsip_req rq;
int size = sizeof(rq);
rq.rq_ns = addreq.ifra_addr;
rq.rq_ip = addreq.ifra_dstaddr;
if (setsockopt(s, 0, SO_NSIP_ROUTE, &rq, size) < 0)
1994-09-23 05:38:45 +04:00
warn("encapsulation routing");
1993-03-21 12:45:37 +03:00
}
#endif /* INET_ONLY */
1993-03-21 12:45:37 +03:00
if (clearaddr) {
(void) strncpy(afp->af_ridreq, name, sizeof ifr.ifr_name);
if (ioctl(s, afp->af_difaddr, afp->af_ridreq) == -1)
err(EXIT_FAILURE, "SIOCDIFADDR");
1993-03-21 12:45:37 +03:00
}
if (newaddr > 0) {
(void) strncpy(afp->af_addreq, name, sizeof ifr.ifr_name);
if (ioctl(s, afp->af_aifaddr, afp->af_addreq) == -1)
1994-09-23 05:38:45 +04:00
warn("SIOCAIFADDR");
1993-03-21 12:45:37 +03:00
}
2000-04-20 06:08:55 +04:00
if (g_ifcr_updated) {
(void) strncpy(g_ifcr.ifcr_name, name,
sizeof(g_ifcr.ifcr_name));
if (ioctl(s, SIOCSIFCAP, &g_ifcr) == -1)
err(EXIT_FAILURE, "SIOCSIFCAP");
}
1993-03-21 12:45:37 +03:00
exit(0);
}
1993-05-04 13:26:52 +04:00
struct afswtch *
2004-10-12 00:13:37 +04:00
lookup_af(const char *cp)
{
struct afswtch *a;
for (a = afs; a->af_name != NULL; a++)
if (strcmp(a->af_name, cp) == 0)
return (a);
return (NULL);
}
1994-09-23 05:38:45 +04:00
void
2004-10-12 00:13:37 +04:00
getsock(int naf)
1994-09-23 05:38:45 +04:00
{
static int oaf = -1;
if (oaf == naf)
return;
if (oaf != -1)
close(s);
s = socket(naf, SOCK_DGRAM, 0);
if (s < 0)
oaf = -1;
else
oaf = naf;
}
int
2004-10-12 00:13:37 +04:00
getinfo(struct ifreq *giifr)
1994-09-23 05:38:45 +04:00
{
getsock(af);
if (s < 0)
err(EXIT_FAILURE, "socket");
if (ioctl(s, SIOCGIFFLAGS, giifr) == -1) {
2001-11-02 08:57:38 +03:00
warn("SIOCGIFFLAGS %s", giifr->ifr_name);
1994-09-23 05:38:45 +04:00
return (-1);
}
2001-11-02 08:57:38 +03:00
flags = giifr->ifr_flags;
if (ioctl(s, SIOCGIFMETRIC, giifr) == -1) {
2001-11-02 08:57:38 +03:00
warn("SIOCGIFMETRIC %s", giifr->ifr_name);
1994-09-23 05:38:45 +04:00
metric = 0;
} else
2001-11-02 08:57:38 +03:00
metric = giifr->ifr_metric;
if (ioctl(s, SIOCGIFMTU, giifr) == -1)
mtu = 0;
else
2001-11-02 08:57:38 +03:00
mtu = giifr->ifr_mtu;
memset(&g_ifcr, 0, sizeof(g_ifcr));
2001-11-02 08:57:38 +03:00
strcpy(g_ifcr.ifcr_name, giifr->ifr_name);
(void) ioctl(s, SIOCGIFCAP, &g_ifcr);
1994-09-23 05:38:45 +04:00
return (0);
}
void
2004-10-12 00:13:37 +04:00
printall(const char *ifname)
1993-05-04 13:26:52 +04:00
{
struct ifaddrs *ifap, *ifa;
2001-11-02 08:57:38 +03:00
struct ifreq paifr;
2001-01-08 17:06:12 +03:00
const struct sockaddr_dl *sdl = NULL;
int idx;
char *p;
if (getifaddrs(&ifap) != 0)
err(EXIT_FAILURE, "getifaddrs");
p = NULL;
idx = 0;
for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
2001-11-02 08:57:38 +03:00
memset(&paifr, 0, sizeof(paifr));
strncpy(paifr.ifr_name, ifa->ifa_name, sizeof(paifr.ifr_name));
if (sizeof(paifr.ifr_addr) >= ifa->ifa_addr->sa_len) {
memcpy(&paifr.ifr_addr, ifa->ifa_addr,
ifa->ifa_addr->sa_len);
}
if (ifname && strcmp(ifname, ifa->ifa_name) != 0)
continue;
if (ifa->ifa_addr->sa_family == AF_LINK)
sdl = (const struct sockaddr_dl *) ifa->ifa_addr;
if (p && strcmp(p, ifa->ifa_name) == 0)
continue;
if (strlcpy(name, ifa->ifa_name, sizeof(name)) >= sizeof(name))
continue;
p = ifa->ifa_name;
2001-11-02 08:57:38 +03:00
if (getinfo(&paifr) < 0)
continue;
if (bflag && (ifa->ifa_flags & IFF_BROADCAST) == 0)
continue;
if (dflag && (ifa->ifa_flags & IFF_UP) != 0)
continue;
if (uflag && (ifa->ifa_flags & IFF_UP) == 0)
continue;
if (sflag && carrier())
continue;
idx++;
/*
* Are we just listing the interfaces?
*/
if (lflag) {
if (idx > 1)
printf(" ");
fputs(name, stdout);
continue;
}
status(sdl);
sdl = NULL;
}
if (lflag)
printf("\n");
freeifaddrs(ifap);
1993-05-04 13:26:52 +04:00
}
void
list_cloners(void)
{
struct if_clonereq ifcr;
char *cp, *buf;
int idx;
memset(&ifcr, 0, sizeof(ifcr));
getsock(AF_INET);
if (ioctl(s, SIOCIFGCLONERS, &ifcr) == -1)
err(EXIT_FAILURE, "SIOCIFGCLONERS for count");
buf = malloc(ifcr.ifcr_total * IFNAMSIZ);
if (buf == NULL)
err(EXIT_FAILURE, "unable to allocate cloner name buffer");
ifcr.ifcr_count = ifcr.ifcr_total;
ifcr.ifcr_buffer = buf;
if (ioctl(s, SIOCIFGCLONERS, &ifcr) == -1)
err(EXIT_FAILURE, "SIOCIFGCLONERS for names");
/*
* In case some disappeared in the mean time, clamp it down.
*/
if (ifcr.ifcr_count > ifcr.ifcr_total)
ifcr.ifcr_count = ifcr.ifcr_total;
for (cp = buf, idx = 0; idx < ifcr.ifcr_count; idx++, cp += IFNAMSIZ) {
if (idx > 0)
printf(" ");
printf("%s", cp);
}
printf("\n");
free(buf);
return;
}
2000-07-02 04:28:00 +04:00
/*ARGSUSED*/
void
2004-10-12 00:13:37 +04:00
clone_create(const char *addr, int param)
2000-07-02 04:28:00 +04:00
{
/* We're called early... */
getsock(AF_INET);
(void) strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
if (ioctl(s, SIOCIFCREATE, &ifr) == -1)
err(EXIT_FAILURE, "SIOCIFCREATE");
2000-07-02 04:28:00 +04:00
}
/*ARGSUSED*/
void
2004-10-12 00:13:37 +04:00
clone_destroy(const char *addr, int param)
2000-07-02 04:28:00 +04:00
{
(void) strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
if (ioctl(s, SIOCIFDESTROY, &ifr) == -1)
err(EXIT_FAILURE, "SIOCIFDESTROY");
2000-07-02 04:28:00 +04:00
}
1993-03-21 12:45:37 +03:00
#define RIDADDR 0
#define ADDR 1
#define MASK 2
#define DSTADDR 3
/*ARGSUSED*/
void
2004-10-12 00:13:37 +04:00
setifaddr(const char *addr, int param)
1993-03-21 12:45:37 +03:00
{
2001-11-02 08:57:38 +03:00
struct ifreq *siifr; /* XXX */
1993-03-21 12:45:37 +03: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++;
if (newaddr == -1)
newaddr = 1;
if (doalias == 0 && afp->af_gifaddr != 0) {
2001-11-02 08:57:38 +03:00
siifr = (struct ifreq *)afp->af_ridreq;
(void) strncpy(siifr->ifr_name, name, sizeof(siifr->ifr_name));
siifr->ifr_addr.sa_family = afp->af_af;
if (ioctl(s, afp->af_gifaddr, afp->af_ridreq) == 0)
clearaddr = 1;
else if (errno == EADDRNOTAVAIL)
/* No address was assigned yet. */
;
else
err(EXIT_FAILURE, "SIOCGIFADDR");
}
1993-03-21 12:45:37 +03:00
(*afp->af_getaddr)(addr, (doalias >= 0 ? ADDR : RIDADDR));
}
void
2004-10-12 00:13:37 +04:00
settunnel(const char *src, const char *dst)
{
struct addrinfo hints, *srcres, *dstres;
int ecode;
struct if_laddrreq req;
memset(&hints, 0, sizeof(hints));
hints.ai_family = afp->af_af;
hints.ai_socktype = SOCK_DGRAM; /*dummy*/
if ((ecode = getaddrinfo(src, NULL, &hints, &srcres)) != 0)
errx(EXIT_FAILURE, "error in parsing address string: %s",
gai_strerror(ecode));
2001-03-01 11:35:16 +03:00
if ((ecode = getaddrinfo(dst, NULL, &hints, &dstres)) != 0)
errx(EXIT_FAILURE, "error in parsing address string: %s",
gai_strerror(ecode));
if (srcres->ai_addr->sa_family != dstres->ai_addr->sa_family)
errx(EXIT_FAILURE,
"source and destination address families do not match");
if (srcres->ai_addrlen > sizeof(req.addr) ||
dstres->ai_addrlen > sizeof(req.dstaddr))
errx(EXIT_FAILURE, "invalid sockaddr");
memset(&req, 0, sizeof(req));
strncpy(req.iflr_name, name, sizeof(req.iflr_name));
memcpy(&req.addr, srcres->ai_addr, srcres->ai_addrlen);
memcpy(&req.dstaddr, dstres->ai_addr, dstres->ai_addrlen);
#ifdef INET6
if (req.addr.ss_family == AF_INET6) {
2001-11-02 08:57:38 +03:00
struct sockaddr_in6 *s6, *d;
2001-11-02 08:57:38 +03:00
s6 = (struct sockaddr_in6 *)&req.addr;
d = (struct sockaddr_in6 *)&req.dstaddr;
2001-11-02 08:57:38 +03:00
if (s6->sin6_scope_id != d->sin6_scope_id) {
errx(EXIT_FAILURE, "scope mismatch");
/* NOTREACHED */
}
#ifdef __KAME__
/* embed scopeid */
2001-11-02 08:57:38 +03:00
if (s6->sin6_scope_id &&
(IN6_IS_ADDR_LINKLOCAL(&s6->sin6_addr) ||
IN6_IS_ADDR_MC_LINKLOCAL(&s6->sin6_addr))) {
*(u_int16_t *)&s6->sin6_addr.s6_addr[2] =
htons(s6->sin6_scope_id);
}
if (d->sin6_scope_id &&
(IN6_IS_ADDR_LINKLOCAL(&d->sin6_addr) ||
IN6_IS_ADDR_MC_LINKLOCAL(&d->sin6_addr))) {
*(u_int16_t *)&d->sin6_addr.s6_addr[2] =
htons(d->sin6_scope_id);
}
#endif
}
#endif
if (ioctl(s, SIOCSLIFPHYADDR, &req) == -1)
warn("SIOCSLIFPHYADDR");
freeaddrinfo(srcres);
freeaddrinfo(dstres);
}
/* ARGSUSED */
void
2004-10-12 00:13:37 +04:00
deletetunnel(const char *vname, int param)
{
if (ioctl(s, SIOCDIFPHYADDR, &ifr) == -1)
err(EXIT_FAILURE, "SIOCDIFPHYADDR");
}
void
2004-10-12 00:13:37 +04:00
setifnetmask(const char *addr, int d)
1993-03-21 12:45:37 +03:00
{
(*afp->af_getaddr)(addr, MASK);
}
void
2004-10-12 00:13:37 +04:00
setifbroadaddr(const char *addr, int d)
1993-03-21 12:45:37 +03:00
{
(*afp->af_getaddr)(addr, DSTADDR);
}
void
2004-10-12 00:13:37 +04:00
setifipdst(const char *addr, int d)
1993-03-21 12:45:37 +03:00
{
in_getaddr(addr, DSTADDR);
setipdst++;
clearaddr = 0;
newaddr = 0;
}
1993-03-21 12:45:37 +03:00
#define rqtosa(x) (&(((struct ifreq *)(afp->x))->ifr_addr))
/*ARGSUSED*/
void
2004-10-12 00:13:37 +04:00
notealias(const char *addr, int param)
1993-03-21 12:45:37 +03:00
{
if (setaddr && doalias == 0 && param < 0)
(void) memcpy(rqtosa(af_ridreq), rqtosa(af_addreq),
rqtosa(af_addreq)->sa_len);
1993-03-21 12:45:37 +03:00
doalias = param;
if (param < 0) {
clearaddr = 1;
newaddr = 0;
conflicting++;
} else {
1993-03-21 12:45:37 +03:00
clearaddr = 0;
conflicting++;
}
1993-03-21 12:45:37 +03:00
}
1994-09-23 05:38:45 +04:00
/*ARGSUSED*/
void
2004-10-12 00:13:37 +04:00
notrailers(const char *vname, int value)
1994-09-23 05:38:45 +04:00
{
puts("Note: trailers are no longer sent, but always received");
1994-09-23 05:38:45 +04:00
}
1993-03-21 12:45:37 +03:00
/*ARGSUSED*/
void
2004-10-12 00:13:37 +04:00
setifdstaddr(const char *addr, int param)
1993-03-21 12:45:37 +03:00
{
(*afp->af_getaddr)(addr, DSTADDR);
}
void
2004-10-12 00:13:37 +04:00
setifflags(const char *vname, int value)
1993-03-21 12:45:37 +03:00
{
2000-04-20 06:08:55 +04:00
struct ifreq ifreq;
(void) strncpy(ifreq.ifr_name, name, sizeof(ifreq.ifr_name));
if (ioctl(s, SIOCGIFFLAGS, &ifreq) == -1)
err(EXIT_FAILURE, "SIOCGIFFLAGS");
2000-04-20 06:08:55 +04:00
flags = ifreq.ifr_flags;
1993-03-21 12:45:37 +03:00
if (value < 0) {
value = -value;
flags &= ~value;
} else
flags |= value;
2000-04-20 06:08:55 +04:00
ifreq.ifr_flags = flags;
if (ioctl(s, SIOCSIFFLAGS, &ifreq) == -1)
err(EXIT_FAILURE, "SIOCSIFFLAGS");
1993-03-21 12:45:37 +03:00
}
void
2004-10-12 00:13:37 +04:00
setifcaps(const char *vname, int value)
{
if (value < 0) {
value = -value;
g_ifcr.ifcr_capenable &= ~value;
} else
g_ifcr.ifcr_capenable |= value;
g_ifcr_updated = 1;
}
#ifdef INET6
void
2004-10-12 00:13:37 +04:00
setia6flags(const char *vname, int value)
{
2002-06-14 05:04:41 +04:00
if (value < 0) {
value = -value;
in6_addreq.ifra_flags &= ~value;
} else
in6_addreq.ifra_flags |= value;
}
void
2004-10-12 00:13:37 +04:00
setia6pltime(const char *val, int d)
{
2002-06-14 05:04:41 +04:00
setia6lifetime("pltime", val);
}
void
2004-10-12 00:13:37 +04:00
setia6vltime(const char *val, int d)
{
2002-06-14 05:04:41 +04:00
setia6lifetime("vltime", val);
}
void
2004-10-12 00:13:37 +04:00
setia6lifetime(const char *cmd, const char *val)
{
time_t newval, t;
char *ep;
t = time(NULL);
newval = (time_t)strtoul(val, &ep, 0);
if (val == ep)
errx(EXIT_FAILURE, "invalid %s", cmd);
if (afp->af_af != AF_INET6)
errx(EXIT_FAILURE, "%s not allowed for the AF", cmd);
if (strcmp(cmd, "vltime") == 0) {
in6_addreq.ifra_lifetime.ia6t_expire = t + newval;
in6_addreq.ifra_lifetime.ia6t_vltime = newval;
} else if (strcmp(cmd, "pltime") == 0) {
in6_addreq.ifra_lifetime.ia6t_preferred = t + newval;
in6_addreq.ifra_lifetime.ia6t_pltime = newval;
}
}
void
2004-10-12 00:13:37 +04:00
setia6eui64(const char *cmd, int val)
{
struct ifaddrs *ifap, *ifa;
const struct sockaddr_in6 *sin6 = NULL;
const struct in6_addr *lladdr = NULL;
struct in6_addr *in6;
if (afp->af_af != AF_INET6)
errx(EXIT_FAILURE, "%s not allowed for the AF", cmd);
in6 = (struct in6_addr *)&in6_addreq.ifra_addr.sin6_addr;
if (memcmp(&in6addr_any.s6_addr[8], &in6->s6_addr[8], 8) != 0)
errx(EXIT_FAILURE, "interface index is already filled");
if (getifaddrs(&ifap) != 0)
err(EXIT_FAILURE, "getifaddrs");
for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
if (ifa->ifa_addr->sa_family == AF_INET6 &&
strcmp(ifa->ifa_name, name) == 0) {
sin6 = (const struct sockaddr_in6 *)ifa->ifa_addr;
if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
lladdr = &sin6->sin6_addr;
break;
}
}
}
if (!lladdr)
errx(EXIT_FAILURE, "could not determine link local address");
memcpy(&in6->s6_addr[8], &lladdr->s6_addr[8], 8);
freeifaddrs(ifap);
}
#endif
void
2004-10-12 00:13:37 +04:00
setifmetric(const char *val, int d)
1993-03-21 12:45:37 +03:00
{
2002-06-14 05:07:00 +04:00
char *ep = NULL;
2002-06-14 05:04:41 +04:00
(void) strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
2002-06-14 05:04:41 +04:00
ifr.ifr_metric = strtoul(val, &ep, 10);
if (!ep || *ep)
errx(EXIT_FAILURE, "%s: invalid metric", val);
if (ioctl(s, SIOCSIFMETRIC, &ifr) == -1)
1994-09-23 05:38:45 +04:00
warn("SIOCSIFMETRIC");
1993-03-21 12:45:37 +03:00
}
void
2004-10-12 00:13:37 +04:00
setifmtu(const char *val, int d)
{
2002-06-14 05:07:00 +04:00
char *ep = NULL;
2002-06-14 05:04:41 +04:00
(void)strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
2002-06-14 05:04:41 +04:00
ifr.ifr_mtu = strtoul(val, &ep, 10);
if (!ep || *ep)
errx(EXIT_FAILURE, "%s: invalid mtu", val);
if (ioctl(s, SIOCSIFMTU, &ifr) == -1)
warn("SIOCSIFMTU");
}
const char *
2004-10-12 00:13:37 +04:00
get_string(const char *val, const char *sep, u_int8_t *buf, int *lenp)
{
int len;
int hexstr;
u_int8_t *p;
len = *lenp;
p = buf;
hexstr = (val[0] == '0' && tolower((u_char)val[1]) == 'x');
if (hexstr)
val += 2;
for (;;) {
if (*val == '\0')
break;
if (sep != NULL && strchr(sep, *val) != NULL) {
val++;
break;
}
if (hexstr) {
if (!isxdigit((u_char)val[0]) ||
!isxdigit((u_char)val[1])) {
warnx("bad hexadecimal digits");
return NULL;
}
}
if (p > buf + len) {
if (hexstr)
warnx("hexadecimal digits too long");
else
warnx("strings too long");
return NULL;
}
if (hexstr) {
#define tohex(x) (isdigit(x) ? (x) - '0' : tolower(x) - 'a' + 10)
*p++ = (tohex((u_char)val[0]) << 4) |
tohex((u_char)val[1]);
#undef tohex
val += 2;
} else
*p++ = *val++;
}
len = p - buf;
if (len < *lenp)
memset(p, 0, *lenp - len);
*lenp = len;
return val;
}
void
2004-10-12 00:13:37 +04:00
print_string(const u_int8_t *buf, int len)
{
int i;
int hasspc;
i = 0;
hasspc = 0;
if (len < 2 || buf[0] != '0' || tolower(buf[1]) != 'x') {
for (; i < len; i++) {
if (!isprint(buf[i]))
break;
if (isspace(buf[i]))
hasspc++;
}
}
if (i == len) {
if (hasspc || len == 0)
printf("\"%.*s\"", len, buf);
else
printf("%.*s", len, buf);
} else {
printf("0x");
for (i = 0; i < len; i++)
printf("%02x", buf[i]);
}
}
void
2004-10-12 00:13:37 +04:00
setifnwid(const char *val, int d)
{
struct ieee80211_nwid nwid;
int len;
len = sizeof(nwid.i_nwid);
if (get_string(val, NULL, nwid.i_nwid, &len) == NULL)
return;
nwid.i_len = len;
(void)strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
ifr.ifr_data = (void *)&nwid;
if (ioctl(s, SIOCS80211NWID, &ifr) == -1)
warn("SIOCS80211NWID");
}
void
2004-10-12 00:13:37 +04:00
setifbssid(const char *val, int d)
{
struct ieee80211_bssid bssid;
struct ether_addr *ea;
if (d != 0) {
/* no BSSID is especially desired */
memset(&bssid.i_bssid, 0, sizeof(bssid.i_bssid));
} else {
ea = ether_aton(val);
if (ea == NULL) {
warnx("malformed BSSID: %s", val);
return;
}
memcpy(&bssid.i_bssid, ea->ether_addr_octet,
sizeof(bssid.i_bssid));
}
(void)strncpy(bssid.i_name, name, sizeof(bssid.i_name));
if (ioctl(s, SIOCS80211BSSID, &bssid) == -1)
warn("SIOCS80211BSSID");
}
void
2004-10-12 00:13:37 +04:00
setifchan(const char *val, int d)
{
struct ieee80211chanreq channel;
int chan;
if (d != 0)
chan = IEEE80211_CHAN_ANY;
else {
chan = atoi(val);
if (chan < 0 || chan > 0xffff) {
warnx("invalid channel: %s", val);
return;
}
}
(void)strncpy(channel.i_name, name, sizeof(channel.i_name));
channel.i_channel = (u_int16_t) chan;
if (ioctl(s, SIOCS80211CHANNEL, &channel) == -1)
warn("SIOCS80211CHANNEL");
}
void
2004-10-12 00:13:37 +04:00
setifnwkey(const char *val, int d)
{
struct ieee80211_nwkey nwkey;
int i;
u_int8_t keybuf[IEEE80211_WEP_NKID][16];
nwkey.i_wepon = IEEE80211_NWKEY_WEP;
nwkey.i_defkid = 1;
for (i = 0; i < IEEE80211_WEP_NKID; i++) {
nwkey.i_key[i].i_keylen = sizeof(keybuf[i]);
nwkey.i_key[i].i_keydat = keybuf[i];
}
if (d != 0) {
/* disable WEP encryption */
nwkey.i_wepon = 0;
i = 0;
} else if (strcasecmp("persist", val) == 0) {
/* use all values from persistent memory */
nwkey.i_wepon |= IEEE80211_NWKEY_PERSIST;
nwkey.i_defkid = 0;
for (i = 0; i < IEEE80211_WEP_NKID; i++)
nwkey.i_key[i].i_keylen = -1;
} else if (strncasecmp("persist:", val, 8) == 0) {
val += 8;
/* program keys in persistent memory */
nwkey.i_wepon |= IEEE80211_NWKEY_PERSIST;
goto set_nwkey;
} else {
set_nwkey:
if (isdigit((unsigned char)val[0]) && val[1] == ':') {
/* specifying a full set of four keys */
nwkey.i_defkid = val[0] - '0';
val += 2;
for (i = 0; i < IEEE80211_WEP_NKID; i++) {
val = get_string(val, ",", keybuf[i],
&nwkey.i_key[i].i_keylen);
if (val == NULL)
return;
}
if (*val != '\0') {
warnx("SIOCS80211NWKEY: too many keys.");
return;
}
} else {
val = get_string(val, NULL, keybuf[0],
&nwkey.i_key[0].i_keylen);
if (val == NULL)
return;
i = 1;
}
}
for (; i < IEEE80211_WEP_NKID; i++)
nwkey.i_key[i].i_keylen = 0;
(void)strncpy(nwkey.i_name, name, sizeof(nwkey.i_name));
if (ioctl(s, SIOCS80211NWKEY, &nwkey) == -1)
warn("SIOCS80211NWKEY");
}
void
2004-10-12 00:13:37 +04:00
setifpowersave(const char *val, int d)
{
struct ieee80211_power power;
(void)strncpy(power.i_name, name, sizeof(power.i_name));
if (ioctl(s, SIOCG80211POWER, &power) == -1) {
warn("SIOCG80211POWER");
return;
}
power.i_enabled = d;
if (ioctl(s, SIOCS80211POWER, &power) == -1)
warn("SIOCS80211POWER");
}
void
2004-10-12 00:13:37 +04:00
setifpowersavesleep(const char *val, int d)
{
struct ieee80211_power power;
(void)strncpy(power.i_name, name, sizeof(power.i_name));
if (ioctl(s, SIOCG80211POWER, &power) == -1) {
warn("SIOCG80211POWER");
return;
}
power.i_maxsleep = atoi(val);
if (ioctl(s, SIOCS80211POWER, &power) == -1)
warn("SIOCS80211POWER");
}
void
ieee80211_statistics(void)
{
struct ieee80211_stats stats;
memset(&ifr, 0, sizeof(ifr));
ifr.ifr_data = (caddr_t)&stats;
(void)strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
if (ioctl(s, (zflag) ? SIOCG80211ZSTATS : SIOCG80211STATS,
(caddr_t)&ifr) == -1)
return;
#define RX_PRINT(desc, member) printf("\trx " desc ": %u\n", stats.member)
#define TX_PRINT(desc, member) printf("\ttx " desc ": %u\n", stats.member)
RX_PRINT("too short", is_rx_tooshort);
RX_PRINT("bad version", is_rx_badversion);
RX_PRINT("wrong bss", is_rx_wrongbss);
RX_PRINT("duplicate", is_rx_dup);
RX_PRINT("wrong direction", is_rx_wrongdir);
RX_PRINT("multicast echo", is_rx_mcastecho);
RX_PRINT("STA not associated", is_rx_notassoc);
RX_PRINT("WEP-encrypted but WEP not configured", is_rx_nowep);
RX_PRINT("WEP processing failed", is_rx_wepfail);
#if 0
RX_PRINT("single (M)MSDU, both WEP/non-WEP fragments", is_rx_wepmix);
RX_PRINT("non-consecutive fragments", is_rx_fragorder);
#endif
RX_PRINT("decapsulation failed", is_rx_decap);
RX_PRINT("management-type discarded", is_rx_mgtdiscard);
RX_PRINT("control-type discarded", is_rx_ctl);
RX_PRINT("truncated rate set", is_rx_rstoobig);
RX_PRINT("beacon/prresp element missing", is_rx_elem_missing);
RX_PRINT("beacon/prresp element too big", is_rx_elem_toobig);
RX_PRINT("beacon/prresp element too small", is_rx_elem_toosmall);
RX_PRINT("beacon/prresp element unknown", is_rx_elem_unknown);
RX_PRINT("invalid channel", is_rx_badchan);
RX_PRINT("channel mismatch", is_rx_chanmismatch);
RX_PRINT("failed node allocation", is_rx_nodealloc);
RX_PRINT("SSID mismatch", is_rx_ssidmismatch);
RX_PRINT("unsupported authentication algor.", is_rx_auth_unsupported);
RX_PRINT("STA authentication failure", is_rx_auth_fail);
RX_PRINT("association for wrong bss", is_rx_assoc_bss);
RX_PRINT("association without authenication", is_rx_assoc_notauth);
RX_PRINT("association capability mismatch", is_rx_assoc_capmismatch);
RX_PRINT("association without rate match", is_rx_assoc_norate);
RX_PRINT("deauthentication", is_rx_deauth);
RX_PRINT("disassocation", is_rx_disassoc);
RX_PRINT("unknown subtype", is_rx_badsubtype);
RX_PRINT("failed, mbuf unavailable", is_rx_nombuf);
RX_PRINT("failed, bad ICV", is_rx_decryptcrc);
RX_PRINT("discard mgmt frame in ad-hoc demo mode", is_rx_ahdemo_mgt);
RX_PRINT("bad authentication", is_rx_bad_auth);
TX_PRINT("failed, mbuf unavailable", is_tx_nombuf);
TX_PRINT("failed, no node", is_tx_nonode);
TX_PRINT("unknown mgmt frame", is_tx_unknownmgt);
printf("\tactive scans: %u\n", stats.is_scan_active);
printf("\tpassive scans: %u\n", stats.is_scan_passive);
printf("\tnodes timed-out for inactivity: %u\n",
stats.is_node_timeout);
printf("\tcrypto context memory unavailable: %u\n",
stats.is_crypto_nomem);
}
void
2004-10-12 00:13:37 +04:00
ieee80211_status(void)
{
int i, nwkey_verbose;
struct ieee80211_nwid nwid;
struct ieee80211_nwkey nwkey;
struct ieee80211_power power;
u_int8_t keybuf[IEEE80211_WEP_NKID][16];
struct ieee80211_bssid bssid;
struct ieee80211chanreq channel;
struct ether_addr ea;
static const u_int8_t zero_macaddr[IEEE80211_ADDR_LEN];
memset(&ifr, 0, sizeof(ifr));
ifr.ifr_data = (void *)&nwid;
(void)strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
if (ioctl(s, SIOCG80211NWID, &ifr) == -1)
return;
if (nwid.i_len > IEEE80211_NWID_LEN) {
warnx("SIOCG80211NWID: wrong length of nwid (%d)", nwid.i_len);
return;
}
printf("\tssid ");
print_string(nwid.i_nwid, nwid.i_len);
memset(&nwkey, 0, sizeof(nwkey));
(void)strncpy(nwkey.i_name, name, sizeof(nwkey.i_name));
/* show nwkey only when WEP is enabled */
if (ioctl(s, SIOCG80211NWKEY, &nwkey) == -1 ||
nwkey.i_wepon == 0) {
printf("\n");
goto skip_wep;
}
printf(" nwkey ");
/* try to retrieve WEP keys */
for (i = 0; i < IEEE80211_WEP_NKID; i++) {
nwkey.i_key[i].i_keydat = keybuf[i];
nwkey.i_key[i].i_keylen = sizeof(keybuf[i]);
}
if (ioctl(s, SIOCG80211NWKEY, &nwkey) == -1) {
printf("*****");
} else {
nwkey_verbose = 0;
/* check to see non default key or multiple keys defined */
if (nwkey.i_defkid != 1) {
nwkey_verbose = 1;
} else {
for (i = 1; i < IEEE80211_WEP_NKID; i++) {
if (nwkey.i_key[i].i_keylen != 0) {
nwkey_verbose = 1;
break;
}
}
}
/* check extra ambiguity with keywords */
if (!nwkey_verbose) {
if (nwkey.i_key[0].i_keylen >= 2 &&
isdigit(nwkey.i_key[0].i_keydat[0]) &&
nwkey.i_key[0].i_keydat[1] == ':')
nwkey_verbose = 1;
else if (nwkey.i_key[0].i_keylen >= 7 &&
strncasecmp("persist", nwkey.i_key[0].i_keydat, 7)
== 0)
nwkey_verbose = 1;
}
if (nwkey_verbose)
printf("%d:", nwkey.i_defkid);
for (i = 0; i < IEEE80211_WEP_NKID; i++) {
if (i > 0)
printf(",");
if (nwkey.i_key[i].i_keylen < 0)
printf("persist");
else
print_string(nwkey.i_key[i].i_keydat,
nwkey.i_key[i].i_keylen);
if (!nwkey_verbose)
break;
}
}
printf("\n");
skip_wep:
(void)strncpy(power.i_name, name, sizeof(power.i_name));
if (ioctl(s, SIOCG80211POWER, &power) == -1)
goto skip_power;
printf("\tpowersave ");
if (power.i_enabled)
printf("on (%dms sleep)", power.i_maxsleep);
else
printf("off");
printf("\n");
skip_power:
(void)strncpy(bssid.i_name, name, sizeof(bssid.i_name));
if (ioctl(s, SIOCG80211BSSID, &bssid) == -1)
return;
(void)strncpy(channel.i_name, name, sizeof(channel.i_name));
if (ioctl(s, SIOCG80211CHANNEL, &channel) == -1)
return;
if (memcmp(bssid.i_bssid, zero_macaddr, IEEE80211_ADDR_LEN) == 0) {
if (channel.i_channel != (u_int16_t)-1)
printf("\tchan %d\n", channel.i_channel);
} else {
memcpy(ea.ether_addr_octet, bssid.i_bssid,
sizeof(ea.ether_addr_octet));
printf("\tbssid %s", ether_ntoa(&ea));
if (channel.i_channel != IEEE80211_CHAN_ANY)
printf(" chan %d", channel.i_channel);
printf("\n");
}
}
static void
media_error(int type, const char *val, const char *opt)
{
errx(EXIT_FAILURE, "unknown %s media %s: %s",
get_media_type_string(type), opt, val);
}
void
2004-10-12 00:13:37 +04:00
init_current_media(void)
{
struct ifmediareq ifmr;
/*
* If we have not yet done so, grab the currently-selected
* media.
*/
if ((actions & (A_MEDIA|A_MEDIAOPT|A_MEDIAMODE)) == 0) {
(void) memset(&ifmr, 0, sizeof(ifmr));
(void) strncpy(ifmr.ifm_name, name, sizeof(ifmr.ifm_name));
if (ioctl(s, SIOCGIFMEDIA, &ifmr) == -1) {
/*
* If we get E2BIG, the kernel is telling us
* that there are more, so we can ignore it.
*/
if (errno != E2BIG)
err(EXIT_FAILURE, "SGIOCGIFMEDIA");
}
media_current = ifmr.ifm_current;
}
/* Sanity. */
if (IFM_TYPE(media_current) == 0)
errx(EXIT_FAILURE, "%s: no link type?", name);
}
void
2004-10-12 00:13:37 +04:00
process_media_commands(void)
{
if ((actions & (A_MEDIA|A_MEDIAOPT|A_MEDIAMODE)) == 0) {
/* Nothing to do. */
return;
}
/*
* Media already set up, and commands sanity-checked. Set/clear
* any options, and we're ready to go.
*/
media_current |= mediaopt_set;
media_current &= ~mediaopt_clear;
strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
ifr.ifr_media = media_current;
if (ioctl(s, SIOCSIFMEDIA, &ifr) == -1)
err(EXIT_FAILURE, "SIOCSIFMEDIA");
}
void
2004-10-12 00:13:37 +04:00
setmedia(const char *val, int d)
{
int type, subtype, inst;
init_current_media();
/* Only one media command may be given. */
if (actions & A_MEDIA)
errx(EXIT_FAILURE, "only one `media' command may be issued");
/* Must not come after mode commands */
if (actions & A_MEDIAMODE)
errx(EXIT_FAILURE,
"may not issue `media' after `mode' commands");
/* Must not come after mediaopt commands */
if (actions & A_MEDIAOPT)
errx(EXIT_FAILURE,
"may not issue `media' after `mediaopt' commands");
/*
* No need to check if `instance' has been issued; setmediainst()
* craps out if `media' has not been specified.
*/
type = IFM_TYPE(media_current);
inst = IFM_INST(media_current);
/* Look up the subtype. */
subtype = get_media_subtype(type, val);
if (subtype == -1)
media_error(type, val, "subtype");
/* Build the new current media word. */
media_current = IFM_MAKEWORD(type, subtype, 0, inst);
/* Media will be set after other processing is complete. */
}
void
2004-10-12 00:13:37 +04:00
setmediaopt(const char *val, int d)
{
char *invalid;
init_current_media();
/* Can only issue `mediaopt' once. */
if (actions & A_MEDIAOPTSET)
errx(EXIT_FAILURE, "only one `mediaopt' command may be issued");
/* Can't issue `mediaopt' if `instance' has already been issued. */
if (actions & A_MEDIAINST)
errx(EXIT_FAILURE, "may not issue `mediaopt' after `instance'");
mediaopt_set = get_media_options(media_current, val, &invalid);
if (mediaopt_set == -1)
media_error(media_current, invalid, "option");
/* Media will be set after other processing is complete. */
}
void
2004-10-12 00:13:37 +04:00
unsetmediaopt(const char *val, int d)
{
char *invalid;
init_current_media();
/* Can only issue `-mediaopt' once. */
if (actions & A_MEDIAOPTCLR)
errx(EXIT_FAILURE,
"only one `-mediaopt' command may be issued");
/* May not issue `media' and `-mediaopt'. */
if (actions & A_MEDIA)
errx(EXIT_FAILURE,
"may not issue both `media' and `-mediaopt'");
/*
* No need to check for A_MEDIAINST, since the test for A_MEDIA
* implicitly checks for A_MEDIAINST.
*/
mediaopt_clear = get_media_options(media_current, val, &invalid);
if (mediaopt_clear == -1)
media_error(media_current, invalid, "option");
/* Media will be set after other processing is complete. */
}
void
2004-10-12 00:13:37 +04:00
setmediainst(const char *val, int d)
{
int type, subtype, options, inst;
init_current_media();
/* Can only issue `instance' once. */
if (actions & A_MEDIAINST)
errx(EXIT_FAILURE, "only one `instance' command may be issued");
/* Must have already specified `media' */
if ((actions & A_MEDIA) == 0)
errx(EXIT_FAILURE, "must specify `media' before `instance'");
type = IFM_TYPE(media_current);
subtype = IFM_SUBTYPE(media_current);
options = IFM_OPTIONS(media_current);
inst = atoi(val);
if (inst < 0 || inst > IFM_INST_MAX)
errx(EXIT_FAILURE, "invalid media instance: %s", val);
media_current = IFM_MAKEWORD(type, subtype, options, inst);
/* Media will be set after other processing is complete. */
}
void
2004-10-12 00:13:37 +04:00
setmediamode(const char *val, int d)
{
int type, subtype, options, inst, mode;
init_current_media();
/* Can only issue `mode' once. */
if (actions & A_MEDIAMODE)
errx(EXIT_FAILURE, "only one `mode' command may be issued");
type = IFM_TYPE(media_current);
subtype = IFM_SUBTYPE(media_current);
options = IFM_OPTIONS(media_current);
inst = IFM_INST(media_current);
mode = get_media_mode(type, val);
if (mode == -1)
media_error(type, val, "mode");
media_current = IFM_MAKEWORD(type, subtype, options, inst) | mode;
/* Media will be set after other processing is complete. */
}
void
print_media_word(int ifmw, const char *opt_sep)
{
const char *str;
printf("%s", get_media_subtype_string(ifmw));
/* Find mode. */
if (IFM_MODE(ifmw) != 0) {
str = get_media_mode_string(ifmw);
if (str != NULL)
printf(" mode %s", str);
}
/* Find options. */
for (; (str = get_media_option_string(&ifmw)) != NULL; opt_sep = ",")
printf("%s%s", opt_sep, str);
if (IFM_INST(ifmw) != 0)
printf(" instance %d", IFM_INST(ifmw));
}
2004-10-12 00:13:37 +04:00
int
carrier(void)
{
struct ifmediareq ifmr;
(void) memset(&ifmr, 0, sizeof(ifmr));
(void) strncpy(ifmr.ifm_name, name, sizeof(ifmr.ifm_name));
if (ioctl(s, SIOCGIFMEDIA, &ifmr) == -1) {
/*
* Interface doesn't support SIOC{G,S}IFMEDIA;
* assume ok.
*/
return 0;
}
if ((ifmr.ifm_status & IFM_AVALID) == 0) {
/*
* Interface doesn't report media-valid status.
* assume ok.
*/
return 0;
}
/* otherwise, return ok for active, not-ok if not active. */
return !(ifmr.ifm_status & IFM_ACTIVE);
}
1993-03-21 12:45:37 +03:00
#define IFFBITS \
1994-09-23 05:38:45 +04:00
"\020\1UP\2BROADCAST\3DEBUG\4LOOPBACK\5POINTOPOINT\6NOTRAILERS\7RUNNING\10NOARP\
\11PROMISC\12ALLMULTI\13OACTIVE\14SIMPLEX\15LINK0\16LINK1\17LINK2\20MULTICAST"
1993-03-21 12:45:37 +03:00
#define IFCAPBITS \
"\020\1IP4CSUM\2TCP4CSUM\3UDP4CSUM\4TCP6CSUM\5UDP6CSUM\6TCP4CSUM_Rx\7UDP4CSUM_Rx\10TSO4"
const int ifm_status_valid_list[] = IFM_STATUS_VALID_LIST;
const struct ifmedia_status_description ifm_status_descriptions[] =
IFM_STATUS_DESCRIPTIONS;
1993-03-21 12:45:37 +03:00
/*
* Print the status of the interface. If an address family was
* specified, show it and it only; otherwise, show them all.
*/
void
2004-10-12 00:13:37 +04:00
status(const struct sockaddr_dl *sdl)
1993-03-21 12:45:37 +03:00
{
struct afswtch *p = afp;
struct ifmediareq ifmr;
struct ifdatareq ifdr;
int *media_list, i;
char hbuf[NI_MAXHOST];
2002-09-21 01:21:53 +04:00
char fbuf[BUFSIZ];
1993-03-21 12:45:37 +03:00
2002-09-21 01:21:53 +04:00
(void)snprintb(fbuf, sizeof(fbuf), IFFBITS, flags);
printf("%s: flags=%s", name, &fbuf[2]);
1993-03-21 12:45:37 +03:00
if (metric)
2002-06-14 05:04:41 +04:00
printf(" metric %lu", metric);
if (mtu)
2002-06-14 05:04:41 +04:00
printf(" mtu %lu", mtu);
printf("\n");
if (g_ifcr.ifcr_capabilities) {
2002-09-21 01:21:53 +04:00
(void)snprintb(fbuf, sizeof(fbuf), IFCAPBITS,
g_ifcr.ifcr_capabilities);
printf("\tcapabilities=%s\n", &fbuf[2]);
(void)snprintb(fbuf, sizeof(fbuf), IFCAPBITS,
g_ifcr.ifcr_capenable);
printf("\tenabled=%s\n", &fbuf[2]);
}
ieee80211_status();
vlan_status();
tunnel_status();
agr_status();
if (sdl != NULL &&
getnameinfo((const struct sockaddr *)sdl, sdl->sdl_len,
hbuf, sizeof(hbuf), NULL, 0, NI_NUMERICHOST) == 0 &&
hbuf[0] != '\0')
printf("\taddress: %s\n", hbuf);
(void) memset(&ifmr, 0, sizeof(ifmr));
(void) strncpy(ifmr.ifm_name, name, sizeof(ifmr.ifm_name));
if (ioctl(s, SIOCGIFMEDIA, &ifmr) == -1) {
/*
* Interface doesn't support SIOC{G,S}IFMEDIA.
*/
goto iface_stats;
}
if (ifmr.ifm_count == 0) {
warnx("%s: no media types?", name);
goto iface_stats;
}
media_list = (int *)malloc(ifmr.ifm_count * sizeof(int));
if (media_list == NULL)
err(EXIT_FAILURE, "malloc");
ifmr.ifm_ulist = media_list;
if (ioctl(s, SIOCGIFMEDIA, &ifmr) == -1)
err(EXIT_FAILURE, "SIOCGIFMEDIA");
printf("\tmedia: %s ", get_media_type_string(ifmr.ifm_current));
print_media_word(ifmr.ifm_current, " ");
if (ifmr.ifm_active != ifmr.ifm_current) {
printf(" (");
print_media_word(ifmr.ifm_active, " ");
printf(")");
}
printf("\n");
if (ifmr.ifm_status & IFM_STATUS_VALID) {
const struct ifmedia_status_description *ifms;
int bitno, found = 0;
printf("\tstatus: ");
for (bitno = 0; ifm_status_valid_list[bitno] != 0; bitno++) {
for (ifms = ifm_status_descriptions;
ifms->ifms_valid != 0; ifms++) {
if (ifms->ifms_type !=
IFM_TYPE(ifmr.ifm_current) ||
ifms->ifms_valid !=
ifm_status_valid_list[bitno])
continue;
printf("%s%s", found ? ", " : "",
IFM_STATUS_DESC(ifms, ifmr.ifm_status));
found = 1;
/*
* For each valid indicator bit, there's
* only one entry for each media type, so
* terminate the inner loop now.
*/
break;
}
}
if (found == 0)
printf("unknown");
printf("\n");
}
if (mflag) {
int type, printed_type;
for (type = IFM_NMIN; type <= IFM_NMAX; type += IFM_NMIN) {
for (i = 0, printed_type = 0; i < ifmr.ifm_count; i++) {
if (IFM_TYPE(media_list[i]) != type)
continue;
if (printed_type == 0) {
printf("\tsupported %s media:\n",
get_media_type_string(type));
printed_type = 1;
}
printf("\t\tmedia ");
print_media_word(media_list[i], " mediaopt ");
printf("\n");
}
}
}
free(media_list);
iface_stats:
if (!vflag && !zflag)
goto proto_status;
(void) strncpy(ifdr.ifdr_name, name, sizeof(ifdr.ifdr_name));
if (ioctl(s, zflag ? SIOCZIFDATA:SIOCGIFDATA, &ifdr) == -1) {
err(EXIT_FAILURE, zflag ? "SIOCZIFDATA" : "SIOCGIFDATA");
} else {
struct if_data * const ifi = &ifdr.ifdr_data;
#define PLURAL(n) ((n) == 1 ? "" : "s")
printf("\tinput: %llu packet%s, %llu byte%s",
(unsigned long long) ifi->ifi_ipackets,
PLURAL(ifi->ifi_ipackets),
(unsigned long long) ifi->ifi_ibytes,
PLURAL(ifi->ifi_ibytes));
if (ifi->ifi_imcasts)
printf(", %llu multicast%s",
(unsigned long long) ifi->ifi_imcasts,
PLURAL(ifi->ifi_imcasts));
if (ifi->ifi_ierrors)
printf(", %llu error%s",
(unsigned long long) ifi->ifi_ierrors,
PLURAL(ifi->ifi_ierrors));
if (ifi->ifi_iqdrops)
printf(", %llu queue drop%s",
(unsigned long long) ifi->ifi_iqdrops,
PLURAL(ifi->ifi_iqdrops));
if (ifi->ifi_noproto)
printf(", %llu unknown protocol",
(unsigned long long) ifi->ifi_noproto);
printf("\n\toutput: %llu packet%s, %llu byte%s",
(unsigned long long) ifi->ifi_opackets,
PLURAL(ifi->ifi_opackets),
(unsigned long long) ifi->ifi_obytes,
PLURAL(ifi->ifi_obytes));
if (ifi->ifi_omcasts)
printf(", %llu multicast%s",
(unsigned long long) ifi->ifi_omcasts,
PLURAL(ifi->ifi_omcasts));
if (ifi->ifi_oerrors)
printf(", %llu error%s",
(unsigned long long) ifi->ifi_oerrors,
PLURAL(ifi->ifi_oerrors));
if (ifi->ifi_collisions)
printf(", %llu collision%s",
(unsigned long long) ifi->ifi_collisions,
PLURAL(ifi->ifi_collisions));
printf("\n");
#undef PLURAL
}
ieee80211_statistics();
proto_status:
1993-03-21 12:45:37 +03:00
if ((p = afp) != NULL) {
(*p->af_status)(1);
} else for (p = afs; p->af_name; p++) {
ifr.ifr_addr.sa_family = p->af_af;
(*p->af_status)(0);
}
}
void
2004-10-12 00:13:37 +04:00
tunnel_status(void)
{
char psrcaddr[NI_MAXHOST];
char pdstaddr[NI_MAXHOST];
const char *ver = "";
const int niflag = NI_NUMERICHOST;
struct if_laddrreq req;
psrcaddr[0] = pdstaddr[0] = '\0';
memset(&req, 0, sizeof(req));
strncpy(req.iflr_name, name, IFNAMSIZ);
if (ioctl(s, SIOCGLIFPHYADDR, &req) == -1)
return;
#ifdef INET6
if (req.addr.ss_family == AF_INET6)
in6_fillscopeid((struct sockaddr_in6 *)&req.addr);
#endif
getnameinfo((struct sockaddr *)&req.addr, req.addr.ss_len,
psrcaddr, sizeof(psrcaddr), 0, 0, niflag);
#ifdef INET6
if (req.addr.ss_family == AF_INET6)
ver = "6";
#endif
#ifdef INET6
if (req.dstaddr.ss_family == AF_INET6)
in6_fillscopeid((struct sockaddr_in6 *)&req.dstaddr);
#endif
getnameinfo((struct sockaddr *)&req.dstaddr, req.dstaddr.ss_len,
pdstaddr, sizeof(pdstaddr), 0, 0, niflag);
printf("\ttunnel inet%s %s --> %s\n", ver, psrcaddr, pdstaddr);
}
void
2004-10-12 00:13:37 +04:00
in_alias(struct ifreq *creq)
{
2001-11-02 08:57:38 +03:00
struct sockaddr_in *iasin;
int alias;
if (lflag)
return;
alias = 1;
/* Get the non-alias address for this interface. */
getsock(AF_INET);
if (s < 0) {
if (errno == EPROTONOSUPPORT)
return;
err(EXIT_FAILURE, "socket");
}
(void) memset(&ifr, 0, sizeof(ifr));
(void) strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
if (ioctl(s, SIOCGIFADDR, &ifr) == -1) {
if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT) {
return;
} else
warn("SIOCGIFADDR");
}
/* If creq and ifr are the same address, this is not an alias. */
if (memcmp(&ifr.ifr_addr, &creq->ifr_addr,
sizeof(creq->ifr_addr)) == 0)
alias = 0;
(void) memset(&in_addreq, 0, sizeof(in_addreq));
(void) strncpy(in_addreq.ifra_name, name, sizeof(in_addreq.ifra_name));
memcpy(&in_addreq.ifra_addr, &creq->ifr_addr,
sizeof(in_addreq.ifra_addr));
if (ioctl(s, SIOCGIFALIAS, &in_addreq) == -1) {
if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT) {
return;
} else
warn("SIOCGIFALIAS");
}
iasin = &in_addreq.ifra_addr;
2001-11-02 08:57:38 +03:00
printf("\tinet %s%s", alias ? "alias " : "", inet_ntoa(iasin->sin_addr));
if (flags & IFF_POINTOPOINT) {
iasin = &in_addreq.ifra_dstaddr;
2001-11-02 08:57:38 +03:00
printf(" -> %s", inet_ntoa(iasin->sin_addr));
}
iasin = &in_addreq.ifra_mask;
2001-11-02 08:57:38 +03:00
printf(" netmask 0x%x", ntohl(iasin->sin_addr.s_addr));
if (flags & IFF_BROADCAST) {
iasin = &in_addreq.ifra_broadaddr;
2001-11-02 08:57:38 +03:00
printf(" broadcast %s", inet_ntoa(iasin->sin_addr));
1993-03-21 12:45:37 +03:00
}
printf("\n");
1993-03-21 12:45:37 +03:00
}
void
2004-10-12 00:13:37 +04:00
in_status(int force)
1993-03-21 12:45:37 +03:00
{
struct ifaddrs *ifap, *ifa;
2001-11-02 08:57:38 +03:00
struct ifreq isifr;
if (getifaddrs(&ifap) != 0)
err(EXIT_FAILURE, "getifaddrs");
for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
if (strcmp(name, ifa->ifa_name) != 0)
continue;
if (ifa->ifa_addr->sa_family != AF_INET)
continue;
2001-11-02 08:57:38 +03:00
if (sizeof(isifr.ifr_addr) < ifa->ifa_addr->sa_len)
continue;
2001-11-02 08:57:38 +03:00
memset(&isifr, 0, sizeof(isifr));
strncpy(isifr.ifr_name, ifa->ifa_name, sizeof(isifr.ifr_name));
memcpy(&isifr.ifr_addr, ifa->ifa_addr, ifa->ifa_addr->sa_len);
in_alias(&isifr);
}
freeifaddrs(ifap);
1993-03-21 12:45:37 +03:00
}
void
2004-10-12 00:13:37 +04:00
setifprefixlen(const char *addr, int d)
{
if (*afp->af_getprefix)
(*afp->af_getprefix)(addr, MASK);
explicit_prefix = 1;
}
#ifdef INET6
void
2004-10-12 00:13:37 +04:00
in6_fillscopeid(struct sockaddr_in6 *sin6)
{
#if defined(__KAME__) && defined(KAME_SCOPEID)
if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
sin6->sin6_scope_id =
ntohs(*(u_int16_t *)&sin6->sin6_addr.s6_addr[2]);
sin6->sin6_addr.s6_addr[2] = sin6->sin6_addr.s6_addr[3] = 0;
}
#endif
}
/* XXX not really an alias */
void
2004-10-12 00:13:37 +04:00
in6_alias(struct in6_ifreq *creq)
{
struct sockaddr_in6 *sin6;
char hbuf[NI_MAXHOST];
u_int32_t scopeid;
const int niflag = NI_NUMERICHOST;
/* Get the non-alias address for this interface. */
getsock(AF_INET6);
if (s < 0) {
if (errno == EPROTONOSUPPORT)
return;
err(EXIT_FAILURE, "socket");
}
sin6 = (struct sockaddr_in6 *)&creq->ifr_addr;
in6_fillscopeid(sin6);
scopeid = sin6->sin6_scope_id;
if (getnameinfo((struct sockaddr *)sin6, sin6->sin6_len,
hbuf, sizeof(hbuf), NULL, 0, niflag))
2003-05-18 03:03:28 +04:00
strlcpy(hbuf, "", sizeof(hbuf)); /* some message? */
printf("\tinet6 %s", hbuf);
if (flags & IFF_POINTOPOINT) {
(void) memset(&ifr6, 0, sizeof(ifr6));
(void) strncpy(ifr6.ifr_name, name, sizeof(ifr6.ifr_name));
ifr6.ifr_addr = creq->ifr_addr;
if (ioctl(s, SIOCGIFDSTADDR_IN6, &ifr6) == -1) {
if (errno != EADDRNOTAVAIL)
warn("SIOCGIFDSTADDR_IN6");
(void) memset(&ifr6.ifr_addr, 0, sizeof(ifr6.ifr_addr));
ifr6.ifr_addr.sin6_family = AF_INET6;
ifr6.ifr_addr.sin6_len = sizeof(struct sockaddr_in6);
}
sin6 = (struct sockaddr_in6 *)&ifr6.ifr_addr;
in6_fillscopeid(sin6);
hbuf[0] = '\0';
if (getnameinfo((struct sockaddr *)sin6, sin6->sin6_len,
hbuf, sizeof(hbuf), NULL, 0, niflag))
2003-05-18 03:03:28 +04:00
strlcpy(hbuf, "", sizeof(hbuf)); /* some message? */
printf(" -> %s", hbuf);
}
(void) memset(&ifr6, 0, sizeof(ifr6));
(void) strncpy(ifr6.ifr_name, name, sizeof(ifr6.ifr_name));
ifr6.ifr_addr = creq->ifr_addr;
if (ioctl(s, SIOCGIFNETMASK_IN6, &ifr6) == -1) {
if (errno != EADDRNOTAVAIL)
warn("SIOCGIFNETMASK_IN6");
} else {
sin6 = (struct sockaddr_in6 *)&ifr6.ifr_addr;
printf(" prefixlen %d", prefix(&sin6->sin6_addr,
sizeof(struct in6_addr)));
}
(void) memset(&ifr6, 0, sizeof(ifr6));
(void) strncpy(ifr6.ifr_name, name, sizeof(ifr6.ifr_name));
ifr6.ifr_addr = creq->ifr_addr;
if (ioctl(s, SIOCGIFAFLAG_IN6, &ifr6) == -1) {
if (errno != EADDRNOTAVAIL)
warn("SIOCGIFAFLAG_IN6");
} else {
if (ifr6.ifr_ifru.ifru_flags6 & IN6_IFF_ANYCAST)
printf(" anycast");
if (ifr6.ifr_ifru.ifru_flags6 & IN6_IFF_TENTATIVE)
printf(" tentative");
if (ifr6.ifr_ifru.ifru_flags6 & IN6_IFF_DUPLICATED)
printf(" duplicated");
if (ifr6.ifr_ifru.ifru_flags6 & IN6_IFF_DETACHED)
printf(" detached");
if (ifr6.ifr_ifru.ifru_flags6 & IN6_IFF_DEPRECATED)
printf(" deprecated");
}
if (scopeid)
printf(" scopeid 0x%x", scopeid);
if (Lflag) {
struct in6_addrlifetime *lifetime;
(void) memset(&ifr6, 0, sizeof(ifr6));
(void) strncpy(ifr6.ifr_name, name, sizeof(ifr6.ifr_name));
ifr6.ifr_addr = creq->ifr_addr;
lifetime = &ifr6.ifr_ifru.ifru_lifetime;
if (ioctl(s, SIOCGIFALIFETIME_IN6, &ifr6) == -1) {
if (errno != EADDRNOTAVAIL)
warn("SIOCGIFALIFETIME_IN6");
} else if (lifetime->ia6t_preferred || lifetime->ia6t_expire) {
time_t t = time(NULL);
printf(" pltime ");
if (lifetime->ia6t_preferred) {
printf("%s", lifetime->ia6t_preferred < t
? "0"
: sec2str(lifetime->ia6t_preferred - t));
} else
printf("infty");
printf(" vltime ");
if (lifetime->ia6t_expire) {
printf("%s", lifetime->ia6t_expire < t
? "0"
: sec2str(lifetime->ia6t_expire - t));
} else
printf("infty");
}
}
printf("\n");
}
void
2004-10-12 00:13:37 +04:00
in6_status(int force)
{
struct ifaddrs *ifap, *ifa;
2001-11-02 08:57:38 +03:00
struct in6_ifreq isifr;
if (getifaddrs(&ifap) != 0)
err(EXIT_FAILURE, "getifaddrs");
for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
if (strcmp(name, ifa->ifa_name) != 0)
continue;
if (ifa->ifa_addr->sa_family != AF_INET6)
continue;
2001-11-02 08:57:38 +03:00
if (sizeof(isifr.ifr_addr) < ifa->ifa_addr->sa_len)
continue;
2001-11-02 08:57:38 +03:00
memset(&isifr, 0, sizeof(isifr));
strncpy(isifr.ifr_name, ifa->ifa_name, sizeof(isifr.ifr_name));
memcpy(&isifr.ifr_addr, ifa->ifa_addr, ifa->ifa_addr->sa_len);
in6_alias(&isifr);
}
freeifaddrs(ifap);
}
#endif /*INET6*/
#ifndef INET_ONLY
void
2004-10-12 00:13:37 +04:00
at_status(int force)
{
struct sockaddr_at *sat, null_sat;
struct netrange *nr;
getsock(AF_APPLETALK);
if (s < 0) {
if (errno == EPROTONOSUPPORT)
return;
err(EXIT_FAILURE, "socket");
}
(void) memset(&ifr, 0, sizeof(ifr));
(void) strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
if (ioctl(s, SIOCGIFADDR, &ifr) == -1) {
if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT) {
if (!force)
return;
(void) memset(&ifr.ifr_addr, 0, sizeof(ifr.ifr_addr));
} else
warn("SIOCGIFADDR");
}
(void) strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
sat = (struct sockaddr_at *)&ifr.ifr_addr;
(void) memset(&null_sat, 0, sizeof(null_sat));
nr = (struct netrange *) &sat->sat_zero;
printf("\tatalk %d.%d range %d-%d phase %d",
ntohs(sat->sat_addr.s_net), sat->sat_addr.s_node,
ntohs(nr->nr_firstnet), ntohs(nr->nr_lastnet), nr->nr_phase);
if (flags & IFF_POINTOPOINT) {
if (ioctl(s, SIOCGIFDSTADDR, &ifr) == -1) {
if (errno == EADDRNOTAVAIL)
(void) memset(&ifr.ifr_addr, 0,
sizeof(ifr.ifr_addr));
else
warn("SIOCGIFDSTADDR");
}
(void) strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
sat = (struct sockaddr_at *)&ifr.ifr_dstaddr;
if (!sat)
sat = &null_sat;
printf("--> %d.%d",
ntohs(sat->sat_addr.s_net), sat->sat_addr.s_node);
}
if (flags & IFF_BROADCAST) {
/* note RTAX_BRD overlap with IFF_POINTOPOINT */
sat = (struct sockaddr_at *)&ifr.ifr_broadaddr;
if (sat)
printf(" broadcast %d.%d", ntohs(sat->sat_addr.s_net),
sat->sat_addr.s_node);
}
printf("\n");
}
void
2004-10-12 00:13:37 +04:00
xns_status(int force)
1993-03-21 12:45:37 +03:00
{
struct sockaddr_ns *sns;
1994-09-23 05:38:45 +04:00
getsock(AF_NS);
if (s < 0) {
if (errno == EPROTONOSUPPORT)
1993-03-21 12:45:37 +03:00
return;
err(EXIT_FAILURE, "socket");
1994-09-23 05:38:45 +04:00
}
(void) memset(&ifr, 0, sizeof(ifr));
(void) strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
if (ioctl(s, SIOCGIFADDR, &ifr) == -1) {
1993-03-21 12:45:37 +03:00
if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT) {
if (!force)
return;
memset(&ifr.ifr_addr, 0, sizeof(ifr.ifr_addr));
1993-03-21 12:45:37 +03:00
} else
1994-09-23 05:38:45 +04:00
warn("SIOCGIFADDR");
1993-03-21 12:45:37 +03:00
}
(void) strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
1993-03-21 12:45:37 +03:00
sns = (struct sockaddr_ns *)&ifr.ifr_addr;
printf("\tns %s ", ns_ntoa(sns->sns_addr));
if (flags & IFF_POINTOPOINT) { /* by W. Nesheim@Cornell */
if (ioctl(s, SIOCGIFDSTADDR, &ifr) == -1) {
1993-03-21 12:45:37 +03:00
if (errno == EADDRNOTAVAIL)
memset(&ifr.ifr_addr, 0, sizeof(ifr.ifr_addr));
1993-03-21 12:45:37 +03:00
else
1994-09-23 05:38:45 +04:00
warn("SIOCGIFDSTADDR");
1993-03-21 12:45:37 +03:00
}
(void) strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
1993-03-21 12:45:37 +03:00
sns = (struct sockaddr_ns *)&ifr.ifr_dstaddr;
printf("--> %s ", ns_ntoa(sns->sns_addr));
}
printf("\n");
1993-03-21 12:45:37 +03:00
}
void
2004-10-12 00:13:37 +04:00
iso_status(int force)
1993-03-21 12:45:37 +03:00
{
struct sockaddr_iso *siso;
2001-11-02 08:57:38 +03:00
struct iso_ifreq isoifr;
1993-03-21 12:45:37 +03:00
1994-09-23 05:38:45 +04:00
getsock(AF_ISO);
if (s < 0) {
if (errno == EPROTONOSUPPORT)
1993-03-21 12:45:37 +03:00
return;
err(EXIT_FAILURE, "socket");
1994-09-23 05:38:45 +04:00
}
2001-11-02 08:57:38 +03:00
(void) memset(&isoifr, 0, sizeof(isoifr));
(void) strncpy(isoifr.ifr_name, name, sizeof(isoifr.ifr_name));
if (ioctl(s, SIOCGIFADDR_ISO, &isoifr) == -1) {
1993-03-21 12:45:37 +03:00
if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT) {
if (!force)
return;
2001-11-02 08:57:38 +03:00
(void) memset(&isoifr.ifr_Addr, 0,
sizeof(isoifr.ifr_Addr));
1994-09-23 05:38:45 +04:00
} else
warn("SIOCGIFADDR_ISO");
1993-03-21 12:45:37 +03:00
}
2001-11-02 08:57:38 +03:00
(void) strncpy(isoifr.ifr_name, name, sizeof isoifr.ifr_name);
siso = &isoifr.ifr_Addr;
1993-03-21 12:45:37 +03:00
printf("\tiso %s ", iso_ntoa(&siso->siso_addr));
if (ioctl(s, SIOCGIFNETMASK_ISO, &isoifr) == -1) {
1994-09-23 05:38:45 +04:00
if (errno == EADDRNOTAVAIL)
2001-11-02 08:57:38 +03:00
memset(&isoifr.ifr_Addr, 0, sizeof(isoifr.ifr_Addr));
1994-09-23 05:38:45 +04:00
else
warn("SIOCGIFNETMASK_ISO");
1993-03-21 12:45:37 +03:00
} else {
if (siso->siso_len > offsetof(struct sockaddr_iso, siso_addr))
siso->siso_addr.isoa_len = siso->siso_len
- offsetof(struct sockaddr_iso, siso_addr);
printf("\n\t\tnetmask %s ", iso_ntoa(&siso->siso_addr));
1993-03-21 12:45:37 +03:00
}
if (flags & IFF_POINTOPOINT) {
if (ioctl(s, SIOCGIFDSTADDR_ISO, &isoifr) == -1) {
1993-03-21 12:45:37 +03:00
if (errno == EADDRNOTAVAIL)
2001-11-02 08:57:38 +03:00
memset(&isoifr.ifr_Addr, 0,
sizeof(isoifr.ifr_Addr));
1993-03-21 12:45:37 +03:00
else
warn("SIOCGIFDSTADDR_ISO");
1993-03-21 12:45:37 +03:00
}
2001-11-02 08:57:38 +03:00
(void) strncpy(isoifr.ifr_name, name, sizeof (isoifr.ifr_name));
siso = &isoifr.ifr_Addr;
1993-03-21 12:45:37 +03:00
printf("--> %s ", iso_ntoa(&siso->siso_addr));
}
printf("\n");
1993-03-21 12:45:37 +03:00
}
#endif /* INET_ONLY */
1993-03-21 12:45:37 +03:00
#define SIN(x) ((struct sockaddr_in *) &(x))
struct sockaddr_in *sintab[] = {
2004-10-12 00:13:37 +04:00
SIN(ridreq.ifr_addr), SIN(in_addreq.ifra_addr),
SIN(in_addreq.ifra_mask), SIN(in_addreq.ifra_broadaddr)};
1993-03-21 12:45:37 +03:00
void
2004-10-12 00:13:37 +04:00
in_getaddr(const char *str, int which)
1993-03-21 12:45:37 +03:00
{
2001-11-02 08:57:38 +03:00
struct sockaddr_in *gasin = sintab[which];
1993-03-21 12:45:37 +03:00
struct hostent *hp;
struct netent *np;
2001-11-02 08:57:38 +03:00
gasin->sin_len = sizeof(*gasin);
1993-03-21 12:45:37 +03:00
if (which != MASK)
2001-11-02 08:57:38 +03:00
gasin->sin_family = AF_INET;
1993-03-21 12:45:37 +03:00
if (which == ADDR) {
char *p = NULL;
2001-11-02 08:57:38 +03:00
if ((p = strrchr(str, '/')) != NULL) {
*p = '\0';
in_getprefix(p + 1, MASK);
}
}
2001-11-02 08:57:38 +03:00
if (inet_aton(str, &gasin->sin_addr) == 0) {
if ((hp = gethostbyname(str)) != NULL)
(void) memcpy(&gasin->sin_addr, hp->h_addr, hp->h_length);
else if ((np = getnetbyname(str)) != NULL)
gasin->sin_addr = inet_makeaddr(np->n_net, INADDR_ANY);
else
errx(EXIT_FAILURE, "%s: bad value", str);
}
1993-03-21 12:45:37 +03:00
}
2001-01-17 09:07:32 +03:00
void
2004-10-12 00:13:37 +04:00
in_getprefix(const char *plen, int which)
2001-01-17 09:07:32 +03:00
{
struct sockaddr_in *igsin = sintab[which];
u_char *cp;
2001-01-17 09:07:32 +03:00
int len = strtol(plen, (char **)NULL, 10);
if ((len < 0) || (len > 32))
errx(EXIT_FAILURE, "%s: bad value", plen);
2001-11-02 08:57:38 +03:00
igsin->sin_len = sizeof(*igsin);
2001-01-17 09:07:32 +03:00
if (which != MASK)
2001-11-02 08:57:38 +03:00
igsin->sin_family = AF_INET;
2001-01-17 09:07:32 +03:00
if ((len == 0) || (len == 32)) {
2001-11-02 08:57:38 +03:00
memset(&igsin->sin_addr, 0xff, sizeof(struct in_addr));
2001-01-17 09:07:32 +03:00
return;
}
2001-11-02 08:57:38 +03:00
memset((void *)&igsin->sin_addr, 0x00, sizeof(igsin->sin_addr));
for (cp = (u_char *)&igsin->sin_addr; len > 7; len -= 8)
2001-01-17 09:07:32 +03:00
*cp++ = 0xff;
if (len)
*cp = 0xff << (8 - len);
2001-01-17 09:07:32 +03:00
}
#ifdef INET6
#define SIN6(x) ((struct sockaddr_in6 *) &(x))
struct sockaddr_in6 *sin6tab[] = {
2004-10-12 00:13:37 +04:00
SIN6(in6_ridreq.ifr_addr), SIN6(in6_addreq.ifra_addr),
SIN6(in6_addreq.ifra_prefixmask), SIN6(in6_addreq.ifra_dstaddr)};
void
2004-10-12 00:13:37 +04:00
in6_getaddr(const char *str, int which)
{
#if defined(__KAME__) && defined(KAME_SCOPEID)
struct sockaddr_in6 *sin6 = sin6tab[which];
struct addrinfo hints, *res;
int error;
char *slash = NULL;
if (which == ADDR) {
if ((slash = strrchr(str, '/')) != NULL)
*slash = '\0';
}
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_INET6;
hints.ai_socktype = SOCK_DGRAM;
#if 0 /* in_getaddr() allows FQDN */
hints.ai_flags = AI_NUMERICHOST;
#endif
2001-11-02 08:57:38 +03:00
error = getaddrinfo(str, "0", &hints, &res);
if (error && slash) {
/* try again treating the '/' as part of the name */
*slash = '/';
slash = NULL;
error = getaddrinfo(str, "0", &hints, &res);
}
if (error)
errx(EXIT_FAILURE, "%s: %s", str, gai_strerror(error));
if (res->ai_next)
errx(EXIT_FAILURE, "%s: resolved to multiple addresses", str);
if (res->ai_addrlen != sizeof(struct sockaddr_in6))
errx(EXIT_FAILURE, "%s: bad value", str);
memcpy(sin6, res->ai_addr, res->ai_addrlen);
freeaddrinfo(res);
if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr) && sin6->sin6_scope_id) {
*(u_int16_t *)&sin6->sin6_addr.s6_addr[2] =
htons(sin6->sin6_scope_id);
sin6->sin6_scope_id = 0;
}
if (slash) {
in6_getprefix(slash + 1, MASK);
explicit_prefix = 1;
}
#else
2001-11-02 08:57:38 +03:00
struct sockaddr_in6 *gasin = sin6tab[which];
2001-11-02 08:57:38 +03:00
gasin->sin6_len = sizeof(*gasin);
if (which != MASK)
2001-11-02 08:57:38 +03:00
gasin->sin6_family = AF_INET6;
if (which == ADDR) {
char *p = NULL;
2001-11-02 08:57:38 +03:00
if((p = strrchr(str, '/')) != NULL) {
*p = '\0';
in6_getprefix(p + 1, MASK);
explicit_prefix = 1;
}
}
2001-11-02 08:57:38 +03:00
if (inet_pton(AF_INET6, str, &gasin->sin6_addr) != 1)
errx(EXIT_FAILURE, "%s: bad value", str);
#endif
}
void
2004-10-12 00:13:37 +04:00
in6_getprefix(const char *plen, int which)
{
struct sockaddr_in6 *gpsin = sin6tab[which];
u_char *cp;
int len = strtol(plen, (char **)NULL, 10);
if ((len < 0) || (len > 128))
errx(EXIT_FAILURE, "%s: bad value", plen);
2001-11-02 08:57:38 +03:00
gpsin->sin6_len = sizeof(*gpsin);
if (which != MASK)
2001-11-02 08:57:38 +03:00
gpsin->sin6_family = AF_INET6;
if ((len == 0) || (len == 128)) {
2001-11-02 08:57:38 +03:00
memset(&gpsin->sin6_addr, 0xff, sizeof(struct in6_addr));
return;
}
2001-11-02 08:57:38 +03:00
memset((void *)&gpsin->sin6_addr, 0x00, sizeof(gpsin->sin6_addr));
for (cp = (u_char *)&gpsin->sin6_addr; len > 7; len -= 8)
*cp++ = 0xff;
if (len)
*cp = 0xff << (8 - len);
}
int
2004-10-12 00:13:37 +04:00
prefix(void *val, int size)
{
u_char *pname = (u_char *)val;
int byte, bit, plen = 0;
for (byte = 0; byte < size; byte++, plen += 8)
2001-11-02 08:57:38 +03:00
if (pname[byte] != 0xff)
break;
if (byte == size)
return (plen);
for (bit = 7; bit != 0; bit--, plen++)
2001-11-02 08:57:38 +03:00
if (!(pname[byte] & (1 << bit)))
break;
for (; bit != 0; bit--)
2001-11-02 08:57:38 +03:00
if (pname[byte] & (1 << bit))
return(0);
byte++;
for (; byte < size; byte++)
2001-11-02 08:57:38 +03:00
if (pname[byte])
return(0);
return (plen);
}
#endif /*INET6*/
#ifndef INET_ONLY
void
2004-10-12 00:13:37 +04:00
at_getaddr(const char *addr, int which)
{
struct sockaddr_at *sat = (struct sockaddr_at *) &addreq.ifra_addr;
u_int net, node;
sat->sat_family = AF_APPLETALK;
sat->sat_len = sizeof(*sat);
if (which == MASK)
errx(EXIT_FAILURE, "AppleTalk does not use netmasks");
if (sscanf(addr, "%u.%u", &net, &node) != 2
|| net == 0 || net > 0xffff || node == 0 || node > 0xfe)
errx(EXIT_FAILURE, "%s: illegal address", addr);
sat->sat_addr.s_net = htons(net);
sat->sat_addr.s_node = node;
}
void
2004-10-12 00:13:37 +04:00
setatrange(const char *range, int d)
{
u_short first = 123, last = 123;
if (sscanf(range, "%hu-%hu", &first, &last) != 2
|| first == 0 /* || first > 0xffff */
|| last == 0 /* || last > 0xffff */ || first > last)
errx(EXIT_FAILURE, "%s: illegal net range: %u-%u", range,
first, last);
at_nr.nr_firstnet = htons(first);
at_nr.nr_lastnet = htons(last);
}
void
2004-10-12 00:13:37 +04:00
setatphase(const char *phase, int d)
{
if (!strcmp(phase, "1"))
at_nr.nr_phase = 1;
else if (!strcmp(phase, "2"))
at_nr.nr_phase = 2;
else
errx(EXIT_FAILURE, "%s: illegal phase", phase);
}
void
2004-10-12 00:13:37 +04:00
checkatrange(struct sockaddr_at *sat)
{
if (at_nr.nr_phase == 0)
at_nr.nr_phase = 2; /* Default phase 2 */
if (at_nr.nr_firstnet == 0)
at_nr.nr_firstnet = /* Default range of one */
at_nr.nr_lastnet = sat->sat_addr.s_net;
printf("\tatalk %d.%d range %d-%d phase %d\n",
ntohs(sat->sat_addr.s_net), sat->sat_addr.s_node,
ntohs(at_nr.nr_firstnet), ntohs(at_nr.nr_lastnet), at_nr.nr_phase);
if ((u_short) ntohs(at_nr.nr_firstnet) >
(u_short) ntohs(sat->sat_addr.s_net)
|| (u_short) ntohs(at_nr.nr_lastnet) <
(u_short) ntohs(sat->sat_addr.s_net))
errx(EXIT_FAILURE, "AppleTalk address is not in range");
*((struct netrange *) &sat->sat_zero) = at_nr;
}
1993-03-21 12:45:37 +03:00
#define SNS(x) ((struct sockaddr_ns *) &(x))
struct sockaddr_ns *snstab[] = {
2004-10-12 00:13:37 +04:00
SNS(ridreq.ifr_addr), SNS(addreq.ifra_addr),
SNS(addreq.ifra_mask), SNS(addreq.ifra_broadaddr)};
1993-03-21 12:45:37 +03:00
void
2004-10-12 00:13:37 +04:00
xns_getaddr(const char *addr, int which)
1993-03-21 12:45:37 +03:00
{
struct sockaddr_ns *sns = snstab[which];
sns->sns_family = AF_NS;
sns->sns_len = sizeof(*sns);
sns->sns_addr = ns_addr(addr);
if (which == MASK)
puts("Attempt to set XNS netmask will be ineffectual");
1993-03-21 12:45:37 +03:00
}
#define SISO(x) ((struct sockaddr_iso *) &(x))
struct sockaddr_iso *sisotab[] = {
SISO(iso_ridreq.ifr_Addr), SISO(iso_addreq.ifra_addr),
SISO(iso_addreq.ifra_mask), SISO(iso_addreq.ifra_dstaddr)};
1993-03-21 12:45:37 +03:00
void
2004-10-12 00:13:37 +04:00
iso_getaddr(const char *addr, int which)
1993-03-21 12:45:37 +03:00
{
struct sockaddr_iso *siso = sisotab[which];
1993-03-21 12:45:37 +03:00
siso->siso_addr = *iso_addr(addr);
if (which == MASK) {
siso->siso_len = TSEL(siso) - (char *)(siso);
1993-03-21 12:45:37 +03:00
siso->siso_nlen = 0;
} else {
siso->siso_len = sizeof(*siso);
siso->siso_family = AF_ISO;
}
}
void
2004-10-12 00:13:37 +04:00
setsnpaoffset(const char *val, int d)
{
iso_addreq.ifra_snpaoffset = atoi(val);
}
void
2004-10-12 00:13:37 +04:00
setnsellength(const char *val, int d)
1993-03-21 12:45:37 +03:00
{
nsellength = atoi(val);
if (nsellength < 0)
errx(EXIT_FAILURE, "Negative NSEL length is absurd");
if (afp == 0 || afp->af_af != AF_ISO)
errx(EXIT_FAILURE, "Setting NSEL length valid only for iso");
1993-03-21 12:45:37 +03:00
}
void
2004-10-12 00:13:37 +04:00
fixnsel(struct sockaddr_iso *siso)
1993-03-21 12:45:37 +03:00
{
2001-11-02 08:57:38 +03:00
if (siso->siso_family == 0)
1993-03-21 12:45:37 +03:00
return;
2001-11-02 08:57:38 +03:00
siso->siso_tlen = nsellength;
1993-03-21 12:45:37 +03:00
}
void
2004-10-12 00:13:37 +04:00
adjust_nsellength(void)
1993-03-21 12:45:37 +03:00
{
fixnsel(sisotab[RIDADDR]);
fixnsel(sisotab[ADDR]);
fixnsel(sisotab[DSTADDR]);
}
#endif /* INET_ONLY */
void
2004-10-12 00:13:37 +04:00
usage(void)
{
2001-02-20 01:48:57 +03:00
const char *progname = getprogname();
fprintf(stderr,
"usage: %s [-m] [-v] [-z] "
#ifdef INET6
"[-L] "
#endif
"interface\n"
"\t[ af [ address [ dest_addr ] ] [ netmask mask ] [ prefixlen n ]\n"
"\t\t[ alias | -alias ] ]\n"
"\t[ up ] [ down ] [ metric n ] [ mtu n ]\n"
"\t[ nwid network_id ] [ nwkey network_key | -nwkey ]\n"
"\t[ powersave | -powersave ] [ powersavesleep duration ]\n"
"\t[ [ af ] tunnel src_addr dest_addr ] [ deletetunnel ]\n"
"\t[ arp | -arp ]\n"
"\t[ media type ] [ mediaopt opts ] [ -mediaopt opts ] "
"[ instance minst ]\n"
"\t[ vlan n vlanif i ]\n"
"\t[ agrport i ] [ -agrport i ]\n"
"\t[ anycast | -anycast ] [ deprecated | -deprecated ]\n"
"\t[ tentative | -tentative ] [ pltime n ] [ vltime n ] [ eui64 ]\n"
"\t[ link0 | -link0 ] [ link1 | -link1 ] [ link2 | -link2 ]\n"
" %s -a [-b] [-m] [-d] [-u] [-v] [-z] [ af ]\n"
" %s -l [-b] [-d] [-u] [-s]\n"
" %s -C\n"
2000-07-02 04:28:00 +04:00
" %s interface create\n"
" %s interface destroy\n",
progname, progname, progname, progname, progname, progname);
exit(1);
}
#ifdef INET6
char *
sec2str(total)
time_t total;
{
static char result[256];
int days, hours, mins, secs;
int first = 1;
char *p = result;
char *end = &result[sizeof(result)];
int n;
if (0) { /*XXX*/
days = total / 3600 / 24;
hours = (total / 3600) % 24;
mins = (total / 60) % 60;
secs = total % 60;
if (days) {
first = 0;
n = snprintf(p, end - p, "%dd", days);
if (n < 0 || n >= end - p)
return(result);
p += n;
}
if (!first || hours) {
first = 0;
n = snprintf(p, end - p, "%dh", hours);
if (n < 0 || n >= end - p)
return(result);
p += n;
}
if (!first || mins) {
first = 0;
n = snprintf(p, end - p, "%dm", mins);
if (n < 0 || n >= end - p)
return(result);
p += n;
}
snprintf(p, end - p, "%ds", secs);
} else
snprintf(p, end - p, "%lu", (u_long)total);
return(result);
}
#endif