2005-03-20 04:10:51 +03:00
|
|
|
/* $NetBSD: ifconfig.c,v 1.165 2005/03/20 01:10:51 thorpej Exp $ */
|
1997-03-17 06:08:46 +03:00
|
|
|
|
1998-08-06 03:35:17 +04:00
|
|
|
/*-
|
2000-01-25 04:03:21 +03:00
|
|
|
* Copyright (c) 1997, 1998, 2000 The NetBSD Foundation, Inc.
|
1997-03-17 06:08:46 +03:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
1998-08-06 03:35:17 +04:00
|
|
|
* 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.
|
|
|
|
*
|
1997-03-17 06:08:46 +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. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
1998-08-06 03:35:17 +04:00
|
|
|
* 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.
|
1997-03-17 06:08:46 +03:00
|
|
|
*
|
1998-08-06 03:35:17 +04:00
|
|
|
* 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.
|
1997-03-17 06:08:46 +03:00
|
|
|
*/
|
1995-03-18 17:54:19 +03:00
|
|
|
|
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.
|
2003-08-07 14:04:22 +04:00
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
1997-09-15 05:53:35 +04:00
|
|
|
#include <sys/cdefs.h>
|
1993-03-21 12:45:37 +03:00
|
|
|
#ifndef lint
|
1997-09-15 05:53:35 +04:00
|
|
|
__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
|
1995-03-18 17:54:19 +03:00
|
|
|
#if 0
|
|
|
|
static char sccsid[] = "@(#)ifconfig.c 8.2 (Berkeley) 2/16/94";
|
|
|
|
#else
|
2005-03-20 04:10:51 +03:00
|
|
|
__RCSID("$NetBSD: ifconfig.c,v 1.165 2005/03/20 01:10:51 thorpej Exp $");
|
1995-03-18 17:54:19 +03:00
|
|
|
#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>
|
1997-03-26 04:46:49 +03:00
|
|
|
#include <net/if_dl.h>
|
1997-03-17 06:08:46 +03:00
|
|
|
#include <net/if_media.h>
|
2000-01-25 02:24:16 +03:00
|
|
|
#include <net/if_ether.h>
|
1993-03-21 12:45:37 +03:00
|
|
|
#include <netinet/in.h>
|
1999-07-01 17:19:20 +04:00
|
|
|
#include <netinet/in_var.h>
|
1993-03-21 12:45:37 +03:00
|
|
|
#include <arpa/inet.h>
|
|
|
|
|
1994-09-23 05:38:45 +04:00
|
|
|
#include <netdb.h>
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
#include <sys/protosw.h>
|
|
|
|
|
|
|
|
#include <ctype.h>
|
1994-09-23 05:38:45 +04:00
|
|
|
#include <err.h>
|
|
|
|
#include <errno.h>
|
1999-04-01 12:12:23 +04:00
|
|
|
#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>
|
2000-04-13 11:39:57 +04:00
|
|
|
#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"
|
2005-03-20 02:32:55 +03:00
|
|
|
|
|
|
|
#ifndef INET_ONLY
|
|
|
|
#include "af_atalk.h"
|
2005-03-20 03:02:58 +03:00
|
|
|
#include "af_iso.h"
|
2005-03-20 02:46:03 +03:00
|
|
|
#include "af_ns.h"
|
2005-03-20 02:32:55 +03:00
|
|
|
#endif /* ! INET_ONLY */
|
2005-03-20 04:09:16 +03:00
|
|
|
#ifdef INET6
|
|
|
|
#include "af_inet6.h"
|
|
|
|
#endif /* INET6 */
|
2005-03-20 02:32:55 +03:00
|
|
|
|
2005-03-18 14:11:50 +03:00
|
|
|
#include "agr.h"
|
2005-03-20 02:13:42 +03:00
|
|
|
#include "ieee80211.h"
|
2005-03-19 20:32:26 +03:00
|
|
|
#include "tunnel.h"
|
2005-03-19 06:53:55 +03:00
|
|
|
#include "vlan.h"
|
2005-03-18 14:11:50 +03:00
|
|
|
|
2000-04-20 06:08:55 +04:00
|
|
|
struct ifreq ifr, ridreq;
|
1997-05-05 10:32:11 +04:00
|
|
|
struct ifaliasreq addreq __attribute__((aligned(4)));
|
2002-04-25 13:39:17 +04:00
|
|
|
struct in_aliasreq in_addreq;
|
2005-03-20 04:09:16 +03:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
struct sockaddr_in netmask;
|
1997-04-03 06:07:58 +04:00
|
|
|
|
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;
|
1994-12-05 23:15:31 +03:00
|
|
|
int clearaddr, s;
|
1997-05-30 09:44:11 +04:00
|
|
|
int newaddr = -1;
|
2001-08-09 01:22:35 +04:00
|
|
|
int conflicting = 0;
|
1997-03-17 06:08:46 +03:00
|
|
|
int af;
|
2003-04-12 05:07:43 +04:00
|
|
|
int aflag, bflag, Cflag, dflag, lflag, mflag, sflag, uflag, vflag, zflag;
|
1999-07-01 17:19:20 +04:00
|
|
|
#ifdef INET6
|
|
|
|
int Lflag;
|
|
|
|
#endif
|
|
|
|
int explicit_prefix = 0;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
2001-06-02 20:17:06 +04: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 setifnetmask(const char *, int);
|
|
|
|
void setifprefixlen(const char *, int);
|
|
|
|
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);
|
|
|
|
int main(int, char *[]);
|
1993-03-21 12:45:37 +03:00
|
|
|
|
1998-08-08 05:30:18 +04: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;
|
|
|
|
|
1998-08-08 22:14:20 +04:00
|
|
|
int actions; /* Actions performed */
|
1998-08-08 05:30:18 +04:00
|
|
|
|
1998-08-08 22:14:20 +04:00
|
|
|
#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)
|
1998-08-09 02:33:17 +04:00
|
|
|
#define A_MEDIAINST 0x0008 /* instance or inst command */
|
2003-10-13 12:27:35 +04:00
|
|
|
#define A_MEDIAMODE 0x0010 /* mode command */
|
1998-08-08 05:30:18 +04:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
#define NEXTARG 0xffffff
|
2000-06-30 21:45:11 +04:00
|
|
|
#define NEXTARG2 0xfffffe
|
1993-03-21 12:45:37 +03:00
|
|
|
|
2000-06-30 21:45:11 +04:00
|
|
|
const struct cmd {
|
|
|
|
const char *c_name;
|
|
|
|
int c_parameter; /* NEXTARG means next argv */
|
1998-08-08 22:14:20 +04:00
|
|
|
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[] = {
|
1998-08-08 05:30:18 +04:00
|
|
|
{ "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
|
1998-08-08 05:30:18 +04:00
|
|
|
{ "swabips", EN_SWABIPS, 0, setifflags },
|
|
|
|
{ "-swabips", -EN_SWABIPS, 0, setifflags },
|
1993-03-21 12:45:37 +03:00
|
|
|
#endif
|
1998-08-08 05:30:18 +04:00
|
|
|
{ "netmask", NEXTARG, 0, setifnetmask },
|
|
|
|
{ "metric", NEXTARG, 0, setifmetric },
|
|
|
|
{ "mtu", NEXTARG, 0, setifmtu },
|
2002-09-22 19:29:23 +04:00
|
|
|
{ "bssid", NEXTARG, 0, setifbssid },
|
|
|
|
{ "-bssid", -1, 0, setifbssid },
|
|
|
|
{ "chan", NEXTARG, 0, setifchan },
|
2002-09-30 09:38:13 +04:00
|
|
|
{ "-chan", -1, 0, setifchan },
|
2004-03-01 03:11:33 +03:00
|
|
|
{ "ssid", NEXTARG, 0, setifnwid },
|
2000-01-25 02:24:16 +03:00
|
|
|
{ "nwid", NEXTARG, 0, setifnwid },
|
2000-07-21 08:53:03 +04:00
|
|
|
{ "nwkey", NEXTARG, 0, setifnwkey },
|
|
|
|
{ "-nwkey", -1, 0, setifnwkey },
|
2000-12-12 07:08:40 +03:00
|
|
|
{ "powersave", 1, 0, setifpowersave },
|
|
|
|
{ "-powersave", 0, 0, setifpowersave },
|
|
|
|
{ "powersavesleep", NEXTARG, 0, setifpowersavesleep },
|
1998-08-08 05:30:18 +04:00
|
|
|
{ "broadcast", NEXTARG, 0, setifbroadaddr },
|
|
|
|
{ "ipdst", NEXTARG, 0, setifipdst },
|
2004-10-11 21:13:55 +04:00
|
|
|
{ "prefixlen", NEXTARG, 0, setifprefixlen},
|
1999-07-01 17:19:20 +04:00
|
|
|
#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 },
|
2001-04-27 13:08:44 +04:00
|
|
|
{ "deprecated", IN6_IFF_DEPRECATED, 0, setia6flags },
|
|
|
|
{ "-deprecated", -IN6_IFF_DEPRECATED, 0, setia6flags },
|
1999-07-01 17:19:20 +04:00
|
|
|
{ "pltime", NEXTARG, 0, setia6pltime },
|
|
|
|
{ "vltime", NEXTARG, 0, setia6vltime },
|
2002-06-14 13:01:59 +04:00
|
|
|
{ "eui64", 0, 0, setia6eui64 },
|
1999-07-01 17:19:20 +04:00
|
|
|
#endif /*INET6*/
|
1995-10-09 02:03:54 +03:00
|
|
|
#ifndef INET_ONLY
|
1998-08-08 05:30:18 +04:00
|
|
|
{ "range", NEXTARG, 0, setatrange },
|
|
|
|
{ "phase", NEXTARG, 0, setatphase },
|
|
|
|
{ "snpaoffset", NEXTARG, 0, setsnpaoffset },
|
|
|
|
{ "nsellength", NEXTARG, 0, setnsellength },
|
1995-10-09 02:03:54 +03:00
|
|
|
#endif /* INET_ONLY */
|
2000-06-30 21:45:11 +04:00
|
|
|
{ "tunnel", NEXTARG2, 0, NULL,
|
|
|
|
settunnel } ,
|
|
|
|
{ "deletetunnel", 0, 0, deletetunnel },
|
2000-09-28 03:00:24 +04:00
|
|
|
{ "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 } ,
|
1998-08-08 05:30:18 +04:00
|
|
|
{ "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 } ,
|
1998-08-08 22:14:20 +04:00
|
|
|
{ "media", NEXTARG, A_MEDIA, setmedia },
|
|
|
|
{ "mediaopt", NEXTARG, A_MEDIAOPTSET, setmediaopt },
|
|
|
|
{ "-mediaopt", NEXTARG, A_MEDIAOPTCLR, unsetmediaopt },
|
2003-10-13 12:27:35 +04:00
|
|
|
{ "mode", NEXTARG, A_MEDIAMODE, setmediamode },
|
1998-08-09 02:33:17 +04:00
|
|
|
{ "instance", NEXTARG, A_MEDIAINST, setmediainst },
|
|
|
|
{ "inst", NEXTARG, A_MEDIAINST, setmediainst },
|
2001-06-02 20:17:06 +04:00
|
|
|
{ "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 },
|
2001-09-17 21:36:06 +04:00
|
|
|
{ "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 },
|
2005-03-06 03:13:36 +03:00
|
|
|
{ "tso4", IFCAP_TSOv4, 0, setifcaps },
|
|
|
|
{ "-tso4", -IFCAP_TSOv4, 0, setifcaps },
|
2005-03-18 14:11:50 +03:00
|
|
|
{ "agrport", NEXTARG, 0, agraddport } ,
|
|
|
|
{ "-agrport", NEXTARG, 0, agrremport } ,
|
1998-08-08 05:30:18 +04:00
|
|
|
{ 0, 0, 0, setifaddr },
|
|
|
|
{ 0, 0, 0, setifdstaddr },
|
1993-03-21 12:45:37 +03:00
|
|
|
};
|
|
|
|
|
2004-10-12 00:13:37 +04:00
|
|
|
int getinfo(struct ifreq *);
|
|
|
|
int carrier(void);
|
|
|
|
void printall(const char *);
|
|
|
|
void list_cloners(void);
|
|
|
|
void status(const struct sockaddr_dl *);
|
|
|
|
void usage(void);
|
|
|
|
|
2004-11-11 23:37:18 +03:00
|
|
|
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);
|
1997-03-17 06:08:46 +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);
|
2000-06-30 21:45:11 +04:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
/* Known address families */
|
2005-03-20 02:16:55 +03:00
|
|
|
const struct afswtch afs[] = {
|
2001-01-17 09:07:32 +03:00
|
|
|
{ "inet", AF_INET, in_status, in_getaddr, in_getprefix,
|
2004-10-11 21:13:55 +04:00
|
|
|
SIOCDIFADDR, SIOCAIFADDR, SIOCGIFADDR, &ridreq, &in_addreq },
|
1999-07-01 17:19:20 +04:00
|
|
|
#ifdef INET6
|
|
|
|
{ "inet6", AF_INET6, in6_status, in6_getaddr, in6_getprefix,
|
2000-04-03 07:54:42 +04:00
|
|
|
SIOCDIFADDR_IN6, SIOCAIFADDR_IN6,
|
|
|
|
/*
|
|
|
|
* Deleting the first address before setting new one is
|
|
|
|
* not prefered way in this protocol.
|
|
|
|
*/
|
|
|
|
0,
|
2004-10-11 21:13:55 +04:00
|
|
|
&in6_ridreq, &in6_addreq },
|
1999-07-01 17:19:20 +04:00
|
|
|
#endif
|
1995-10-09 02:03:54 +03:00
|
|
|
#ifndef INET_ONLY /* small version, for boot media */
|
1999-07-01 17:19:20 +04:00
|
|
|
{ "atalk", AF_APPLETALK, at_status, at_getaddr, NULL,
|
2004-10-11 21:13:55 +04:00
|
|
|
SIOCDIFADDR, SIOCAIFADDR, SIOCGIFADDR, &addreq, &addreq },
|
1999-07-01 17:19:20 +04:00
|
|
|
{ "ns", AF_NS, xns_status, xns_getaddr, NULL,
|
2004-10-11 21:13:55 +04:00
|
|
|
SIOCDIFADDR, SIOCAIFADDR, SIOCGIFADDR, &ridreq, &addreq },
|
1999-07-01 17:19:20 +04:00
|
|
|
{ "iso", AF_ISO, iso_status, iso_getaddr, NULL,
|
2000-04-03 07:54:42 +04:00
|
|
|
SIOCDIFADDR_ISO, SIOCAIFADDR_ISO, SIOCGIFADDR_ISO,
|
2004-10-11 21:13:55 +04:00
|
|
|
&iso_ridreq, &iso_addreq },
|
1995-10-09 02:03:54 +03:00
|
|
|
#endif /* INET_ONLY */
|
1993-03-21 12:45:37 +03:00
|
|
|
{ 0, 0, 0, 0 }
|
|
|
|
};
|
|
|
|
|
2005-03-20 02:16:55 +03:00
|
|
|
const struct afswtch *afp; /*the address family being set or asked about*/
|
1993-03-21 12:45:37 +03:00
|
|
|
|
1994-12-05 23:15:31 +03:00
|
|
|
int
|
2004-10-12 00:13:37 +04:00
|
|
|
main(int argc, char *argv[])
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
1998-09-06 21:51:32 +04:00
|
|
|
int ch;
|
1997-03-17 06:08:46 +03:00
|
|
|
|
|
|
|
/* Parse command-line options */
|
2003-04-12 05:07:43 +04:00
|
|
|
aflag = mflag = vflag = zflag = 0;
|
|
|
|
while ((ch = getopt(argc, argv, "AabCdlmsuvz"
|
1999-07-01 17:19:20 +04:00
|
|
|
#ifdef INET6
|
|
|
|
"L"
|
|
|
|
#endif
|
|
|
|
)) != -1) {
|
1997-03-17 06:08:46 +03:00
|
|
|
switch (ch) {
|
1998-09-06 21:51:32 +04:00
|
|
|
case 'A':
|
2001-04-28 08:11:10 +04:00
|
|
|
warnx("-A is deprecated");
|
1998-09-06 21:51:32 +04:00
|
|
|
break;
|
|
|
|
|
1997-03-17 06:08:46 +03:00
|
|
|
case 'a':
|
|
|
|
aflag = 1;
|
|
|
|
break;
|
|
|
|
|
1999-07-03 21:31:15 +04:00
|
|
|
case 'b':
|
|
|
|
bflag = 1;
|
|
|
|
break;
|
|
|
|
|
2000-07-20 22:42:02 +04:00
|
|
|
case 'C':
|
|
|
|
Cflag = 1;
|
|
|
|
break;
|
|
|
|
|
1997-04-21 05:17:57 +04:00
|
|
|
case 'd':
|
|
|
|
dflag = 1;
|
|
|
|
break;
|
|
|
|
|
1999-07-01 17:19:20 +04:00
|
|
|
#ifdef INET6
|
|
|
|
case 'L':
|
|
|
|
Lflag = 1;
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
|
1997-03-28 01:50:09 +03:00
|
|
|
case 'l':
|
|
|
|
lflag = 1;
|
|
|
|
break;
|
|
|
|
|
1997-03-17 06:08:46 +03:00
|
|
|
case 'm':
|
|
|
|
mflag = 1;
|
|
|
|
break;
|
|
|
|
|
1999-07-03 21:31:15 +04:00
|
|
|
case 's':
|
|
|
|
sflag = 1;
|
|
|
|
break;
|
|
|
|
|
1997-04-21 05:17:57 +04:00
|
|
|
case 'u':
|
|
|
|
uflag = 1;
|
|
|
|
break;
|
|
|
|
|
2002-05-24 01:38:01 +04:00
|
|
|
case 'v':
|
|
|
|
vflag = 1;
|
|
|
|
break;
|
|
|
|
|
2003-04-12 05:07:43 +04:00
|
|
|
case 'z':
|
|
|
|
zflag = 1;
|
|
|
|
break;
|
|
|
|
|
1999-07-03 21:31:15 +04:00
|
|
|
|
1997-03-17 06:08:46 +03:00
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
/* NOTREACHED */
|
|
|
|
}
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
1997-03-17 06:08:46 +03:00
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
|
1997-03-28 01:50:09 +03:00
|
|
|
/*
|
|
|
|
* -l means "list all interfaces", and is mutally exclusive with
|
|
|
|
* all other flags/commands.
|
|
|
|
*
|
2000-07-20 22:42:02 +04:00
|
|
|
* -C means "list all names of cloners", and it mutually exclusive
|
|
|
|
* with all other flags/commands.
|
|
|
|
*
|
1997-03-28 01:50:09 +03:00
|
|
|
* -a means "print status of all interfaces".
|
|
|
|
*/
|
2003-04-12 05:07:43 +04:00
|
|
|
if ((lflag || Cflag) && (aflag || mflag || vflag || argc || zflag))
|
1999-07-03 21:31:15 +04:00
|
|
|
usage();
|
|
|
|
#ifdef INET6
|
2000-07-20 22:42:02 +04:00
|
|
|
if ((lflag || Cflag) && Lflag)
|
1997-03-28 01:50:09 +03:00
|
|
|
usage();
|
1999-07-03 21:31:15 +04:00
|
|
|
#endif
|
2000-07-20 22:42:02 +04:00
|
|
|
if (lflag && Cflag)
|
|
|
|
usage();
|
|
|
|
if (Cflag) {
|
|
|
|
if (argc)
|
|
|
|
usage();
|
|
|
|
list_cloners();
|
|
|
|
exit(0);
|
|
|
|
}
|
1997-03-28 01:50:09 +03:00
|
|
|
if (aflag || lflag) {
|
1997-03-17 06:08:46 +03:00
|
|
|
if (argc > 1)
|
1995-01-22 00:22:16 +03:00
|
|
|
usage();
|
1997-03-17 06:08:46 +03:00
|
|
|
else if (argc == 1) {
|
2005-03-20 01:57:06 +03:00
|
|
|
afp = lookup_af_byname(argv[0]);
|
1997-03-17 06:08:46 +03:00
|
|
|
if (afp == NULL)
|
|
|
|
usage();
|
1997-04-03 06:07:58 +04:00
|
|
|
}
|
|
|
|
if (afp)
|
|
|
|
af = ifr.ifr_addr.sa_family = afp->af_af;
|
|
|
|
else
|
|
|
|
af = ifr.ifr_addr.sa_family = afs[0].af_af;
|
2001-04-28 08:11:10 +04:00
|
|
|
printall(NULL);
|
1993-05-04 13:26:52 +04:00
|
|
|
exit(0);
|
|
|
|
}
|
1997-03-17 06:08:46 +03:00
|
|
|
|
|
|
|
/* Make sure there's an interface name. */
|
|
|
|
if (argc < 1)
|
|
|
|
usage();
|
2004-02-28 00:36:17 +03:00
|
|
|
if (strlcpy(name, argv[0], sizeof(name)) >= sizeof(name))
|
|
|
|
errx(1, "interface name '%s' too long", argv[0]);
|
1997-03-17 06:08:46 +03:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
1997-03-17 06:08:46 +03:00
|
|
|
/* Check for address family. */
|
|
|
|
afp = NULL;
|
|
|
|
if (argc > 0) {
|
2005-03-20 01:57:06 +03:00
|
|
|
afp = lookup_af_byname(argv[0]);
|
1997-03-17 06:08:46 +03:00
|
|
|
if (afp != NULL) {
|
|
|
|
argv++;
|
|
|
|
argc--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-05-17 20:00:05 +04:00
|
|
|
/* Initialize af, just for use in getinfo(). */
|
1997-03-17 06:08:46 +03:00
|
|
|
if (afp == NULL)
|
1999-05-17 20:00:05 +04:00
|
|
|
af = afs->af_af;
|
1999-05-18 22:01:08 +04:00
|
|
|
else
|
|
|
|
af = afp->af_af;
|
1997-03-17 06:08:46 +03:00
|
|
|
|
|
|
|
/* Get information about the interface. */
|
1997-04-03 06:07:58 +04:00
|
|
|
(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);
|
1997-03-17 06:08:46 +03:00
|
|
|
|
1999-07-03 21:31:15 +04:00
|
|
|
if (sflag) {
|
|
|
|
if (argc != 0)
|
|
|
|
usage();
|
|
|
|
else
|
|
|
|
exit(carrier());
|
|
|
|
}
|
|
|
|
|
1997-03-17 06:08:46 +03:00
|
|
|
/* No more arguments means interface status. */
|
1994-09-23 05:38:45 +04:00
|
|
|
if (argc == 0) {
|
2001-04-28 08:11:10 +04:00
|
|
|
printall(name);
|
1993-05-04 13:26:52 +04:00
|
|
|
exit(0);
|
|
|
|
}
|
1997-03-17 06:08:46 +03:00
|
|
|
|
2000-03-01 06:21:56 +03:00
|
|
|
/* The following operations assume inet family as the default. */
|
1999-05-17 20:00:05 +04:00
|
|
|
if (afp == NULL)
|
|
|
|
afp = afs;
|
|
|
|
af = ifr.ifr_addr.sa_family = afp->af_af;
|
|
|
|
|
1999-07-01 17:19:20 +04:00
|
|
|
#ifdef INET6
|
2005-03-20 04:09:16 +03:00
|
|
|
in6_init();
|
1999-07-01 17:19:20 +04:00
|
|
|
#endif
|
|
|
|
|
1997-03-17 06:08:46 +03:00
|
|
|
/* Process commands. */
|
1993-03-21 12:45:37 +03:00
|
|
|
while (argc > 0) {
|
2000-06-30 21:45:11 +04:00
|
|
|
const struct cmd *p;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
for (p = cmds; p->c_name; p++)
|
1997-03-17 06:08:46 +03:00
|
|
|
if (strcmp(argv[0], p->c_name) == 0)
|
1993-03-21 12:45:37 +03:00
|
|
|
break;
|
1999-07-26 15:56:55 +04:00
|
|
|
if (p->c_name == 0 && setaddr) {
|
1999-07-26 23:38:36 +04:00
|
|
|
if ((flags & IFF_POINTOPOINT) == 0) {
|
2002-06-14 13:01:59 +04:00
|
|
|
errx(EXIT_FAILURE,
|
|
|
|
"can't set destination address %s",
|
1999-07-26 15:56:55 +04:00
|
|
|
"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 */
|
1999-07-26 15:56:55 +04:00
|
|
|
}
|
2000-06-30 21:45:11 +04:00
|
|
|
if (p->c_func != NULL || p->c_func2 != NULL) {
|
1993-03-21 12:45:37 +03:00
|
|
|
if (p->c_parameter == NEXTARG) {
|
1997-03-17 06:08:46 +03:00
|
|
|
if (argc < 2)
|
2002-06-14 13:01:59 +04:00
|
|
|
errx(EXIT_FAILURE,
|
|
|
|
"'%s' requires argument",
|
1994-01-22 05:04:35 +03:00
|
|
|
p->c_name);
|
1997-04-03 06:07:58 +04:00
|
|
|
(*p->c_func)(argv[1], 0);
|
1993-03-21 12:45:37 +03:00
|
|
|
argc--, argv++;
|
2000-06-30 21:45:11 +04:00
|
|
|
} else if (p->c_parameter == NEXTARG2) {
|
|
|
|
if (argc < 3)
|
2002-06-14 13:01:59 +04:00
|
|
|
errx(EXIT_FAILURE,
|
|
|
|
"'%s' requires 2 arguments",
|
2000-06-30 21:45:11 +04:00
|
|
|
p->c_name);
|
|
|
|
(*p->c_func2)(argv[1], argv[2]);
|
|
|
|
argc -= 2, argv += 2;
|
1993-03-21 12:45:37 +03:00
|
|
|
} else
|
1997-03-17 06:08:46 +03:00
|
|
|
(*p->c_func)(argv[0], p->c_parameter);
|
1998-08-08 22:14:20 +04:00
|
|
|
actions |= p->c_action;
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
argc--, argv++;
|
|
|
|
}
|
1995-10-09 02:03:54 +03:00
|
|
|
|
2001-08-09 05:25:35 +04:00
|
|
|
/*
|
|
|
|
* See if multiple alias, -alias, or delete commands were
|
|
|
|
* specified. More than one constitutes an invalid command line
|
|
|
|
*/
|
2001-08-09 01:22:35 +04:00
|
|
|
|
|
|
|
if (conflicting > 1)
|
2004-07-27 18:25:19 +04:00
|
|
|
errx(EXIT_FAILURE,
|
2001-10-25 02:10:15 +04:00
|
|
|
"Only one use of alias, -alias or delete is valid.");
|
2001-08-09 01:22:35 +04:00
|
|
|
|
1998-08-08 05:30:18 +04:00
|
|
|
/* Process any media commands that may have been issued. */
|
|
|
|
process_media_commands();
|
|
|
|
|
1999-07-01 17:19:20 +04:00
|
|
|
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... */
|
|
|
|
}
|
1995-10-09 02:03:54 +03:00
|
|
|
|
1999-07-01 17:19:20 +04:00
|
|
|
#ifndef INET_ONLY
|
1993-03-21 12:45:37 +03:00
|
|
|
if (af == AF_ISO)
|
|
|
|
adjust_nsellength();
|
1997-04-03 06:07:58 +04:00
|
|
|
|
|
|
|
if (af == AF_APPLETALK)
|
2005-03-20 02:32:55 +03:00
|
|
|
checkatrange(&addreq.ifra_addr);
|
1997-04-03 06:07:58 +04:00
|
|
|
|
2005-03-20 02:46:03 +03:00
|
|
|
if (setipdst && af == AF_NS)
|
|
|
|
xns_set_nsip_route(&addreq.ifra_addr, &addreq.ifra_dstaddr);
|
1995-10-09 02:03:54 +03:00
|
|
|
#endif /* INET_ONLY */
|
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
if (clearaddr) {
|
1997-04-03 06:07:58 +04:00
|
|
|
(void) strncpy(afp->af_ridreq, name, sizeof ifr.ifr_name);
|
2001-10-25 02:10:15 +04:00
|
|
|
if (ioctl(s, afp->af_difaddr, afp->af_ridreq) == -1)
|
|
|
|
err(EXIT_FAILURE, "SIOCDIFADDR");
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
1997-05-30 09:44:11 +04:00
|
|
|
if (newaddr > 0) {
|
1997-04-03 06:07:58 +04:00
|
|
|
(void) strncpy(afp->af_addreq, name, sizeof ifr.ifr_name);
|
2001-10-25 02:10:15 +04:00
|
|
|
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
|
|
|
|
2001-06-02 20:17:06 +04:00
|
|
|
if (g_ifcr_updated) {
|
|
|
|
(void) strncpy(g_ifcr.ifcr_name, name,
|
|
|
|
sizeof(g_ifcr.ifcr_name));
|
2004-10-11 21:13:55 +04:00
|
|
|
if (ioctl(s, SIOCSIFCAP, &g_ifcr) == -1)
|
2001-10-25 02:10:15 +04:00
|
|
|
err(EXIT_FAILURE, "SIOCSIFCAP");
|
2001-06-02 20:17:06 +04:00
|
|
|
}
|
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
exit(0);
|
|
|
|
}
|
1993-05-04 13:26:52 +04:00
|
|
|
|
2005-03-20 02:16:55 +03:00
|
|
|
const struct afswtch *
|
2005-03-20 01:57:06 +03:00
|
|
|
lookup_af_byname(const char *cp)
|
1997-03-17 06:08:46 +03:00
|
|
|
{
|
2005-03-20 02:16:55 +03:00
|
|
|
const struct afswtch *a;
|
1997-03-17 06:08:46 +03:00
|
|
|
|
|
|
|
for (a = afs; a->af_name != NULL; a++)
|
|
|
|
if (strcmp(a->af_name, cp) == 0)
|
|
|
|
return (a);
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
2005-03-20 02:16:55 +03:00
|
|
|
const struct afswtch *
|
2005-03-20 01:57:06 +03:00
|
|
|
lookup_af_bynum(int afnum)
|
|
|
|
{
|
2005-03-20 02:16:55 +03:00
|
|
|
const struct afswtch *a;
|
2005-03-20 01:57:06 +03:00
|
|
|
|
|
|
|
for (a = afs; a->af_name != NULL; a++)
|
|
|
|
if (a->af_af == afnum)
|
|
|
|
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)
|
2001-10-25 02:10:15 +04:00
|
|
|
err(EXIT_FAILURE, "socket");
|
2004-10-11 21:13:55 +04:00
|
|
|
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;
|
2004-10-11 21:13:55 +04:00
|
|
|
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;
|
2004-10-11 21:13:55 +04:00
|
|
|
if (ioctl(s, SIOCGIFMTU, giifr) == -1)
|
1997-04-10 23:10:16 +04:00
|
|
|
mtu = 0;
|
|
|
|
else
|
2001-11-02 08:57:38 +03:00
|
|
|
mtu = giifr->ifr_mtu;
|
2001-06-02 20:17:06 +04:00
|
|
|
|
|
|
|
memset(&g_ifcr, 0, sizeof(g_ifcr));
|
2001-11-02 08:57:38 +03:00
|
|
|
strcpy(g_ifcr.ifcr_name, giifr->ifr_name);
|
2004-10-11 21:13:55 +04:00
|
|
|
(void) ioctl(s, SIOCGIFCAP, &g_ifcr);
|
2001-06-02 20:17:06 +04:00
|
|
|
|
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
|
|
|
{
|
2000-04-13 11:39:57 +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;
|
2000-04-13 11:39:57 +04:00
|
|
|
int idx;
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
if (getifaddrs(&ifap) != 0)
|
2001-10-25 02:10:15 +04:00
|
|
|
err(EXIT_FAILURE, "getifaddrs");
|
2000-04-13 11:39:57 +04:00
|
|
|
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,
|
2000-04-13 11:39:57 +04:00
|
|
|
ifa->ifa_addr->sa_len);
|
|
|
|
}
|
|
|
|
|
2001-04-28 08:11:10 +04:00
|
|
|
if (ifname && strcmp(ifname, ifa->ifa_name) != 0)
|
|
|
|
continue;
|
2000-04-13 11:39:57 +04:00
|
|
|
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;
|
2004-02-28 00:36:17 +03:00
|
|
|
if (strlcpy(name, ifa->ifa_name, sizeof(name)) >= sizeof(name))
|
|
|
|
continue;
|
2000-04-13 11:39:57 +04:00
|
|
|
p = ifa->ifa_name;
|
|
|
|
|
2001-11-02 08:57:38 +03:00
|
|
|
if (getinfo(&paifr) < 0)
|
2000-04-13 11:39:57 +04:00
|
|
|
continue;
|
2003-05-17 04:48:13 +04:00
|
|
|
if (bflag && (ifa->ifa_flags & IFF_BROADCAST) == 0)
|
2000-04-13 11:39:57 +04:00
|
|
|
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)
|
2004-11-11 23:37:18 +03:00
|
|
|
printf(" ");
|
2000-04-13 11:39:57 +04:00
|
|
|
fputs(name, stdout);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2001-10-06 21:05:29 +04:00
|
|
|
status(sdl);
|
|
|
|
sdl = NULL;
|
2000-04-13 11:39:57 +04:00
|
|
|
}
|
|
|
|
if (lflag)
|
2004-11-11 23:37:18 +03:00
|
|
|
printf("\n");
|
2000-04-13 11:39:57 +04:00
|
|
|
freeifaddrs(ifap);
|
1993-05-04 13:26:52 +04:00
|
|
|
}
|
|
|
|
|
2000-07-20 22:42:02 +04:00
|
|
|
void
|
|
|
|
list_cloners(void)
|
|
|
|
{
|
|
|
|
struct if_clonereq ifcr;
|
|
|
|
char *cp, *buf;
|
|
|
|
int idx;
|
|
|
|
|
|
|
|
memset(&ifcr, 0, sizeof(ifcr));
|
|
|
|
|
|
|
|
getsock(AF_INET);
|
|
|
|
|
2001-10-25 02:10:15 +04:00
|
|
|
if (ioctl(s, SIOCIFGCLONERS, &ifcr) == -1)
|
|
|
|
err(EXIT_FAILURE, "SIOCIFGCLONERS for count");
|
2000-07-20 22:42:02 +04:00
|
|
|
|
|
|
|
buf = malloc(ifcr.ifcr_total * IFNAMSIZ);
|
|
|
|
if (buf == NULL)
|
2001-10-25 02:10:15 +04:00
|
|
|
err(EXIT_FAILURE, "unable to allocate cloner name buffer");
|
2000-07-20 22:42:02 +04:00
|
|
|
|
|
|
|
ifcr.ifcr_count = ifcr.ifcr_total;
|
|
|
|
ifcr.ifcr_buffer = buf;
|
|
|
|
|
2001-10-25 02:10:15 +04:00
|
|
|
if (ioctl(s, SIOCIFGCLONERS, &ifcr) == -1)
|
|
|
|
err(EXIT_FAILURE, "SIOCIFGCLONERS for names");
|
2000-07-20 22:42:02 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 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)
|
2004-11-11 23:37:18 +03:00
|
|
|
printf(" ");
|
2000-07-20 22:42:02 +04:00
|
|
|
printf("%s", cp);
|
|
|
|
}
|
|
|
|
|
2004-11-11 23:37:18 +03:00
|
|
|
printf("\n");
|
2000-07-20 22:42:02 +04:00
|
|
|
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));
|
2001-10-25 02:10:15 +04:00
|
|
|
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));
|
2001-10-25 02:10:15 +04:00
|
|
|
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
|
|
|
/*ARGSUSED*/
|
1994-12-05 23:15:31 +03:00
|
|
|
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 */
|
2000-04-03 07:54:42 +04:00
|
|
|
|
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++;
|
1997-05-30 09:44:11 +04:00
|
|
|
if (newaddr == -1)
|
|
|
|
newaddr = 1;
|
2000-04-03 07:54:42 +04:00
|
|
|
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;
|
2000-04-03 07:54:42 +04:00
|
|
|
if (ioctl(s, afp->af_gifaddr, afp->af_ridreq) == 0)
|
|
|
|
clearaddr = 1;
|
|
|
|
else if (errno == EADDRNOTAVAIL)
|
|
|
|
/* No address was assigned yet. */
|
|
|
|
;
|
|
|
|
else
|
2001-10-25 02:10:15 +04:00
|
|
|
err(EXIT_FAILURE, "SIOCGIFADDR");
|
2000-04-03 07:54:42 +04:00
|
|
|
}
|
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
(*afp->af_getaddr)(addr, (doalias >= 0 ? ADDR : RIDADDR));
|
|
|
|
}
|
|
|
|
|
1994-12-05 23:15:31 +03:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
1994-12-05 23:15:31 +03:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
1994-12-05 23:15:31 +03:00
|
|
|
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;
|
|
|
|
}
|
1994-12-05 23:15:31 +03:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
#define rqtosa(x) (&(((struct ifreq *)(afp->x))->ifr_addr))
|
|
|
|
/*ARGSUSED*/
|
1994-12-05 23:15:31 +03:00
|
|
|
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)
|
1997-04-03 06:07:58 +04:00
|
|
|
(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;
|
2001-08-09 01:22:35 +04:00
|
|
|
conflicting++;
|
2001-08-09 05:25:35 +04:00
|
|
|
} else {
|
1993-03-21 12:45:37 +03:00
|
|
|
clearaddr = 0;
|
2001-08-09 01:22:35 +04:00
|
|
|
conflicting++;
|
2001-08-09 05:25:35 +04:00
|
|
|
}
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
|
1994-09-23 05:38:45 +04:00
|
|
|
/*ARGSUSED*/
|
1994-12-05 23:15:31 +03:00
|
|
|
void
|
2004-10-12 00:13:37 +04:00
|
|
|
notrailers(const char *vname, int value)
|
1994-09-23 05:38:45 +04:00
|
|
|
{
|
1997-04-21 05:17:57 +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*/
|
1994-12-05 23:15:31 +03:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
1994-12-05 23:15:31 +03:00
|
|
|
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));
|
2004-10-11 21:13:55 +04:00
|
|
|
if (ioctl(s, SIOCGIFFLAGS, &ifreq) == -1)
|
2001-10-25 02:10:15 +04:00
|
|
|
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;
|
2004-10-11 21:13:55 +04:00
|
|
|
if (ioctl(s, SIOCSIFFLAGS, &ifreq) == -1)
|
2001-10-25 02:10:15 +04:00
|
|
|
err(EXIT_FAILURE, "SIOCSIFFLAGS");
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
|
2001-06-02 20:17:06 +04:00
|
|
|
void
|
2004-10-12 00:13:37 +04:00
|
|
|
setifcaps(const char *vname, int value)
|
2001-06-02 20:17:06 +04:00
|
|
|
{
|
|
|
|
|
|
|
|
if (value < 0) {
|
|
|
|
value = -value;
|
|
|
|
g_ifcr.ifcr_capenable &= ~value;
|
|
|
|
} else
|
|
|
|
g_ifcr.ifcr_capenable |= value;
|
|
|
|
|
|
|
|
g_ifcr_updated = 1;
|
|
|
|
}
|
|
|
|
|
1994-12-05 23:15:31 +03:00
|
|
|
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
|
|
|
|
1997-04-03 06:07:58 +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)
|
2002-06-14 13:01:59 +04:00
|
|
|
errx(EXIT_FAILURE, "%s: invalid metric", val);
|
2004-10-11 21:13:55 +04:00
|
|
|
if (ioctl(s, SIOCSIFMETRIC, &ifr) == -1)
|
1994-09-23 05:38:45 +04:00
|
|
|
warn("SIOCSIFMETRIC");
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
|
1997-04-10 23:10:16 +04:00
|
|
|
void
|
2004-10-12 00:13:37 +04:00
|
|
|
setifmtu(const char *val, int d)
|
1997-04-10 23:10:16 +04:00
|
|
|
{
|
2002-06-14 05:07:00 +04:00
|
|
|
char *ep = NULL;
|
2002-06-14 05:04:41 +04:00
|
|
|
|
1997-04-10 23:10:16 +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)
|
2002-06-14 13:01:59 +04:00
|
|
|
errx(EXIT_FAILURE, "%s: invalid mtu", val);
|
2004-10-11 21:13:55 +04:00
|
|
|
if (ioctl(s, SIOCSIFMTU, &ifr) == -1)
|
1997-04-10 23:10:16 +04:00
|
|
|
warn("SIOCSIFMTU");
|
|
|
|
}
|
|
|
|
|
2000-07-21 08:53:03 +04:00
|
|
|
const char *
|
2004-10-12 00:13:37 +04:00
|
|
|
get_string(const char *val, const char *sep, u_int8_t *buf, int *lenp)
|
2000-01-25 02:24:16 +03:00
|
|
|
{
|
2000-07-19 08:43:34 +04:00
|
|
|
int len;
|
2000-07-21 08:53:03 +04:00
|
|
|
int hexstr;
|
2000-07-05 06:35:53 +04:00
|
|
|
u_int8_t *p;
|
2000-01-25 02:24:16 +03:00
|
|
|
|
2000-07-21 08:53:03 +04:00
|
|
|
len = *lenp;
|
|
|
|
p = buf;
|
|
|
|
hexstr = (val[0] == '0' && tolower((u_char)val[1]) == 'x');
|
|
|
|
if (hexstr)
|
2000-07-05 06:35:53 +04:00
|
|
|
val += 2;
|
2000-07-21 08:53:03 +04:00
|
|
|
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;
|
2000-07-19 08:43:34 +04:00
|
|
|
}
|
2000-07-21 08:53:03 +04:00
|
|
|
}
|
|
|
|
if (p > buf + len) {
|
|
|
|
if (hexstr)
|
|
|
|
warnx("hexadecimal digits too long");
|
|
|
|
else
|
|
|
|
warnx("strings too long");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (hexstr) {
|
2000-07-05 06:35:53 +04:00
|
|
|
#define tohex(x) (isdigit(x) ? (x) - '0' : tolower(x) - 'a' + 10)
|
2000-07-06 12:20:51 +04:00
|
|
|
*p++ = (tohex((u_char)val[0]) << 4) |
|
|
|
|
tohex((u_char)val[1]);
|
2000-07-05 06:35:53 +04:00
|
|
|
#undef tohex
|
|
|
|
val += 2;
|
2000-07-21 08:53:03 +04:00
|
|
|
} 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)
|
2000-07-21 08:53:03 +04:00
|
|
|
{
|
|
|
|
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++;
|
2000-07-05 06:35:53 +04:00
|
|
|
}
|
2000-07-21 08:53:03 +04:00
|
|
|
}
|
|
|
|
if (i == len) {
|
|
|
|
if (hasspc || len == 0)
|
|
|
|
printf("\"%.*s\"", len, buf);
|
|
|
|
else
|
|
|
|
printf("%.*s", len, buf);
|
2000-07-05 06:35:53 +04:00
|
|
|
} else {
|
2000-07-21 08:53:03 +04:00
|
|
|
printf("0x");
|
|
|
|
for (i = 0; i < len; i++)
|
|
|
|
printf("%02x", buf[i]);
|
2000-07-05 06:35:53 +04:00
|
|
|
}
|
2000-07-21 08:53:03 +04:00
|
|
|
}
|
|
|
|
|
2004-11-11 23:37:18 +03:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
1997-03-17 06:08:46 +03:00
|
|
|
void
|
2004-10-12 00:13:37 +04:00
|
|
|
init_current_media(void)
|
1997-03-17 06:08:46 +03:00
|
|
|
{
|
|
|
|
struct ifmediareq ifmr;
|
|
|
|
|
1998-08-08 05:30:18 +04:00
|
|
|
/*
|
|
|
|
* If we have not yet done so, grab the currently-selected
|
|
|
|
* media.
|
|
|
|
*/
|
2003-10-13 12:27:35 +04:00
|
|
|
if ((actions & (A_MEDIA|A_MEDIAOPT|A_MEDIAMODE)) == 0) {
|
1998-08-08 05:30:18 +04:00
|
|
|
(void) memset(&ifmr, 0, sizeof(ifmr));
|
|
|
|
(void) strncpy(ifmr.ifm_name, name, sizeof(ifmr.ifm_name));
|
|
|
|
|
2004-10-11 21:13:55 +04:00
|
|
|
if (ioctl(s, SIOCGIFMEDIA, &ifmr) == -1) {
|
1998-08-08 05:30:18 +04:00
|
|
|
/*
|
|
|
|
* If we get E2BIG, the kernel is telling us
|
|
|
|
* that there are more, so we can ignore it.
|
|
|
|
*/
|
|
|
|
if (errno != E2BIG)
|
2001-10-25 02:10:15 +04:00
|
|
|
err(EXIT_FAILURE, "SGIOCGIFMEDIA");
|
1998-08-08 05:30:18 +04:00
|
|
|
}
|
1997-03-17 06:08:46 +03:00
|
|
|
|
1998-08-08 05:30:18 +04:00
|
|
|
media_current = ifmr.ifm_current;
|
1997-03-17 06:08:46 +03:00
|
|
|
}
|
|
|
|
|
1998-08-08 05:30:18 +04:00
|
|
|
/* Sanity. */
|
|
|
|
if (IFM_TYPE(media_current) == 0)
|
2002-06-14 13:01:59 +04:00
|
|
|
errx(EXIT_FAILURE, "%s: no link type?", name);
|
1998-08-08 05:30:18 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2004-10-12 00:13:37 +04:00
|
|
|
process_media_commands(void)
|
1998-08-08 05:30:18 +04:00
|
|
|
{
|
|
|
|
|
2003-10-13 12:27:35 +04:00
|
|
|
if ((actions & (A_MEDIA|A_MEDIAOPT|A_MEDIAMODE)) == 0) {
|
1998-08-08 05:30:18 +04:00
|
|
|
/* Nothing to do. */
|
|
|
|
return;
|
|
|
|
}
|
1997-03-17 06:08:46 +03:00
|
|
|
|
|
|
|
/*
|
1998-08-08 05:30:18 +04:00
|
|
|
* Media already set up, and commands sanity-checked. Set/clear
|
|
|
|
* any options, and we're ready to go.
|
1997-03-17 06:08:46 +03:00
|
|
|
*/
|
1998-08-08 05:30:18 +04:00
|
|
|
media_current |= mediaopt_set;
|
|
|
|
media_current &= ~mediaopt_clear;
|
1997-03-17 06:08:46 +03:00
|
|
|
|
|
|
|
strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
|
1998-08-08 05:30:18 +04:00
|
|
|
ifr.ifr_media = media_current;
|
1997-03-17 06:08:46 +03:00
|
|
|
|
2004-10-11 21:13:55 +04:00
|
|
|
if (ioctl(s, SIOCSIFMEDIA, &ifr) == -1)
|
2001-10-25 02:10:15 +04:00
|
|
|
err(EXIT_FAILURE, "SIOCSIFMEDIA");
|
1997-03-17 06:08:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2004-10-12 00:13:37 +04:00
|
|
|
setmedia(const char *val, int d)
|
1997-03-17 06:08:46 +03:00
|
|
|
{
|
1998-08-08 05:30:18 +04:00
|
|
|
int type, subtype, inst;
|
1997-03-17 06:08:46 +03:00
|
|
|
|
1998-08-08 05:30:18 +04:00
|
|
|
init_current_media();
|
1997-03-17 06:08:46 +03:00
|
|
|
|
1998-08-08 05:30:18 +04:00
|
|
|
/* Only one media command may be given. */
|
1998-08-08 22:14:20 +04:00
|
|
|
if (actions & A_MEDIA)
|
2002-06-14 13:01:59 +04:00
|
|
|
errx(EXIT_FAILURE, "only one `media' command may be issued");
|
1998-08-08 05:30:18 +04:00
|
|
|
|
2003-10-13 12:27:35 +04:00
|
|
|
/* Must not come after mode commands */
|
|
|
|
if (actions & A_MEDIAMODE)
|
|
|
|
errx(EXIT_FAILURE,
|
|
|
|
"may not issue `media' after `mode' commands");
|
|
|
|
|
1998-08-08 05:30:18 +04:00
|
|
|
/* Must not come after mediaopt commands */
|
1998-08-08 22:14:20 +04:00
|
|
|
if (actions & A_MEDIAOPT)
|
2002-06-14 13:01:59 +04:00
|
|
|
errx(EXIT_FAILURE,
|
|
|
|
"may not issue `media' after `mediaopt' commands");
|
1997-03-17 06:08:46 +03:00
|
|
|
|
1998-08-09 02:33:17 +04:00
|
|
|
/*
|
|
|
|
* No need to check if `instance' has been issued; setmediainst()
|
|
|
|
* craps out if `media' has not been specified.
|
|
|
|
*/
|
|
|
|
|
1998-08-08 05:30:18 +04:00
|
|
|
type = IFM_TYPE(media_current);
|
|
|
|
inst = IFM_INST(media_current);
|
|
|
|
|
|
|
|
/* Look up the subtype. */
|
|
|
|
subtype = get_media_subtype(type, val);
|
2004-11-11 23:37:18 +03:00
|
|
|
if (subtype == -1)
|
|
|
|
media_error(type, val, "subtype");
|
1998-08-08 05:30:18 +04:00
|
|
|
|
|
|
|
/* Build the new current media word. */
|
|
|
|
media_current = IFM_MAKEWORD(type, subtype, 0, inst);
|
|
|
|
|
|
|
|
/* Media will be set after other processing is complete. */
|
1997-03-17 06:08:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2004-10-12 00:13:37 +04:00
|
|
|
setmediaopt(const char *val, int d)
|
1997-03-17 06:08:46 +03:00
|
|
|
{
|
2004-11-11 23:37:18 +03:00
|
|
|
char *invalid;
|
1997-03-17 06:08:46 +03:00
|
|
|
|
1998-08-08 05:30:18 +04:00
|
|
|
init_current_media();
|
1997-03-17 06:08:46 +03:00
|
|
|
|
1998-08-08 05:30:18 +04:00
|
|
|
/* Can only issue `mediaopt' once. */
|
1998-08-08 22:14:20 +04:00
|
|
|
if (actions & A_MEDIAOPTSET)
|
2002-06-14 13:01:59 +04:00
|
|
|
errx(EXIT_FAILURE, "only one `mediaopt' command may be issued");
|
1997-03-18 04:50:01 +03:00
|
|
|
|
1998-08-09 02:33:17 +04:00
|
|
|
/* Can't issue `mediaopt' if `instance' has already been issued. */
|
|
|
|
if (actions & A_MEDIAINST)
|
2002-06-14 13:01:59 +04:00
|
|
|
errx(EXIT_FAILURE, "may not issue `mediaopt' after `instance'");
|
1998-08-09 02:33:17 +04:00
|
|
|
|
2004-11-11 23:37:18 +03:00
|
|
|
mediaopt_set = get_media_options(media_current, val, &invalid);
|
|
|
|
if (mediaopt_set == -1)
|
|
|
|
media_error(media_current, invalid, "option");
|
1997-03-17 06:08:46 +03:00
|
|
|
|
1998-08-08 05:30:18 +04:00
|
|
|
/* Media will be set after other processing is complete. */
|
|
|
|
}
|
1997-03-17 06:08:46 +03:00
|
|
|
|
1998-08-08 05:30:18 +04:00
|
|
|
void
|
2004-10-12 00:13:37 +04:00
|
|
|
unsetmediaopt(const char *val, int d)
|
1998-08-08 05:30:18 +04:00
|
|
|
{
|
2004-11-11 23:37:18 +03:00
|
|
|
char *invalid;
|
1997-03-18 04:50:01 +03:00
|
|
|
|
1998-08-08 05:30:18 +04:00
|
|
|
init_current_media();
|
1997-03-18 04:50:01 +03:00
|
|
|
|
1998-08-08 05:30:18 +04:00
|
|
|
/* Can only issue `-mediaopt' once. */
|
1998-08-08 22:14:20 +04:00
|
|
|
if (actions & A_MEDIAOPTCLR)
|
2002-06-14 13:01:59 +04:00
|
|
|
errx(EXIT_FAILURE,
|
|
|
|
"only one `-mediaopt' command may be issued");
|
1997-03-18 04:50:01 +03:00
|
|
|
|
1998-08-08 05:30:18 +04:00
|
|
|
/* May not issue `media' and `-mediaopt'. */
|
1998-08-08 22:14:20 +04:00
|
|
|
if (actions & A_MEDIA)
|
2002-06-14 13:01:59 +04:00
|
|
|
errx(EXIT_FAILURE,
|
|
|
|
"may not issue both `media' and `-mediaopt'");
|
1997-03-17 06:08:46 +03:00
|
|
|
|
1998-08-09 02:33:17 +04:00
|
|
|
/*
|
|
|
|
* No need to check for A_MEDIAINST, since the test for A_MEDIA
|
|
|
|
* implicitly checks for A_MEDIAINST.
|
|
|
|
*/
|
|
|
|
|
2004-11-11 23:37:18 +03:00
|
|
|
mediaopt_clear = get_media_options(media_current, val, &invalid);
|
|
|
|
if (mediaopt_clear == -1)
|
|
|
|
media_error(media_current, invalid, "option");
|
1997-03-17 06:08:46 +03:00
|
|
|
|
1998-08-08 05:30:18 +04:00
|
|
|
/* Media will be set after other processing is complete. */
|
1997-03-17 06:08:46 +03:00
|
|
|
}
|
|
|
|
|
1998-08-09 02:33:17 +04:00
|
|
|
void
|
2004-10-12 00:13:37 +04:00
|
|
|
setmediainst(const char *val, int d)
|
1998-08-09 02:33:17 +04:00
|
|
|
{
|
|
|
|
int type, subtype, options, inst;
|
|
|
|
|
|
|
|
init_current_media();
|
|
|
|
|
|
|
|
/* Can only issue `instance' once. */
|
|
|
|
if (actions & A_MEDIAINST)
|
2002-06-14 13:01:59 +04:00
|
|
|
errx(EXIT_FAILURE, "only one `instance' command may be issued");
|
1998-08-09 02:33:17 +04:00
|
|
|
|
|
|
|
/* Must have already specified `media' */
|
|
|
|
if ((actions & A_MEDIA) == 0)
|
2002-06-14 13:01:59 +04:00
|
|
|
errx(EXIT_FAILURE, "must specify `media' before `instance'");
|
1998-08-09 02:33:17 +04:00
|
|
|
|
|
|
|
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)
|
2002-06-14 13:01:59 +04:00
|
|
|
errx(EXIT_FAILURE, "invalid media instance: %s", val);
|
1998-08-09 02:33:17 +04:00
|
|
|
|
|
|
|
media_current = IFM_MAKEWORD(type, subtype, options, inst);
|
|
|
|
|
|
|
|
/* Media will be set after other processing is complete. */
|
|
|
|
}
|
|
|
|
|
2003-10-13 12:27:35 +04:00
|
|
|
void
|
2004-10-12 00:13:37 +04:00
|
|
|
setmediamode(const char *val, int d)
|
2003-10-13 12:27:35 +04:00
|
|
|
{
|
|
|
|
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);
|
|
|
|
|
2004-11-11 23:37:18 +03:00
|
|
|
mode = get_media_mode(type, val);
|
|
|
|
if (mode == -1)
|
|
|
|
media_error(type, val, "mode");
|
2003-10-13 12:27:35 +04:00
|
|
|
|
|
|
|
media_current = IFM_MAKEWORD(type, subtype, options, inst) | mode;
|
|
|
|
|
|
|
|
/* Media will be set after other processing is complete. */
|
|
|
|
}
|
|
|
|
|
1997-03-17 06:08:46 +03:00
|
|
|
void
|
2004-11-11 23:37:18 +03:00
|
|
|
print_media_word(int ifmw, const char *opt_sep)
|
1997-03-17 06:08:46 +03:00
|
|
|
{
|
2004-11-11 23:37:18 +03:00
|
|
|
const char *str;
|
1997-03-17 06:08:46 +03:00
|
|
|
|
2004-11-11 23:37:18 +03:00
|
|
|
printf("%s", get_media_subtype_string(ifmw));
|
1997-03-17 06:08:46 +03:00
|
|
|
|
2003-10-13 12:27:35 +04:00
|
|
|
/* Find mode. */
|
|
|
|
if (IFM_MODE(ifmw) != 0) {
|
2004-11-11 23:37:18 +03:00
|
|
|
str = get_media_mode_string(ifmw);
|
|
|
|
if (str != NULL)
|
|
|
|
printf(" mode %s", str);
|
2003-10-13 12:27:35 +04:00
|
|
|
}
|
|
|
|
|
1997-03-17 06:08:46 +03:00
|
|
|
/* Find options. */
|
2004-11-11 23:37:18 +03:00
|
|
|
for (; (str = get_media_option_string(&ifmw)) != NULL; opt_sep = ",")
|
|
|
|
printf("%s%s", opt_sep, str);
|
|
|
|
|
1998-08-06 07:47:28 +04:00
|
|
|
if (IFM_INST(ifmw) != 0)
|
1998-08-09 02:33:17 +04:00
|
|
|
printf(" instance %d", IFM_INST(ifmw));
|
1997-03-17 06:08:46 +03:00
|
|
|
}
|
|
|
|
|
2004-10-12 00:13:37 +04:00
|
|
|
int
|
|
|
|
carrier(void)
|
1999-07-03 21:31:15 +04:00
|
|
|
{
|
|
|
|
struct ifmediareq ifmr;
|
|
|
|
|
|
|
|
(void) memset(&ifmr, 0, sizeof(ifmr));
|
|
|
|
(void) strncpy(ifmr.ifm_name, name, sizeof(ifmr.ifm_name));
|
|
|
|
|
2004-10-11 21:13:55 +04:00
|
|
|
if (ioctl(s, SIOCGIFMEDIA, &ifmr) == -1) {
|
1999-07-03 21:31:15 +04:00
|
|
|
/*
|
|
|
|
* 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
|
|
|
|
2001-06-02 20:17:06 +04:00
|
|
|
#define IFCAPBITS \
|
2005-03-06 03:13:36 +03:00
|
|
|
"\020\1IP4CSUM\2TCP4CSUM\3UDP4CSUM\4TCP6CSUM\5UDP6CSUM\6TCP4CSUM_Rx\7UDP4CSUM_Rx\10TSO4"
|
2001-06-02 20:17:06 +04:00
|
|
|
|
2000-01-25 03:59:39 +03:00
|
|
|
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.
|
|
|
|
*/
|
1994-12-05 23:15:31 +03:00
|
|
|
void
|
2004-10-12 00:13:37 +04:00
|
|
|
status(const struct sockaddr_dl *sdl)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
2005-03-20 02:16:55 +03:00
|
|
|
const struct afswtch *p = afp;
|
1997-03-17 06:08:46 +03:00
|
|
|
struct ifmediareq ifmr;
|
2002-05-24 01:38:01 +04:00
|
|
|
struct ifdatareq ifdr;
|
1997-03-17 06:08:46 +03:00
|
|
|
int *media_list, i;
|
2001-10-06 21:05:29 +04:00
|
|
|
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);
|
1997-04-10 23:10:16 +04:00
|
|
|
if (mtu)
|
2002-06-14 05:04:41 +04:00
|
|
|
printf(" mtu %lu", mtu);
|
2004-11-11 23:37:18 +03:00
|
|
|
printf("\n");
|
2000-01-25 04:46:57 +03:00
|
|
|
|
2001-06-02 20:17:06 +04:00
|
|
|
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]);
|
2001-06-02 20:17:06 +04:00
|
|
|
}
|
|
|
|
|
2000-01-25 04:46:57 +03:00
|
|
|
ieee80211_status();
|
2000-09-28 03:00:24 +04:00
|
|
|
vlan_status();
|
2000-06-30 21:45:11 +04:00
|
|
|
tunnel_status();
|
2005-03-18 14:11:50 +03:00
|
|
|
agr_status();
|
2000-01-25 04:46:57 +03:00
|
|
|
|
2001-10-06 21:05:29 +04:00
|
|
|
if (sdl != NULL &&
|
2005-01-20 19:16:10 +03:00
|
|
|
getnameinfo((const struct sockaddr *)sdl, sdl->sdl_len,
|
2001-10-06 21:05:29 +04:00
|
|
|
hbuf, sizeof(hbuf), NULL, 0, NI_NUMERICHOST) == 0 &&
|
|
|
|
hbuf[0] != '\0')
|
|
|
|
printf("\taddress: %s\n", hbuf);
|
1997-03-17 06:08:46 +03:00
|
|
|
|
1997-04-03 06:07:58 +04:00
|
|
|
(void) memset(&ifmr, 0, sizeof(ifmr));
|
|
|
|
(void) strncpy(ifmr.ifm_name, name, sizeof(ifmr.ifm_name));
|
1997-03-17 06:08:46 +03:00
|
|
|
|
2004-10-11 21:13:55 +04:00
|
|
|
if (ioctl(s, SIOCGIFMEDIA, &ifmr) == -1) {
|
1997-03-17 06:08:46 +03:00
|
|
|
/*
|
|
|
|
* Interface doesn't support SIOC{G,S}IFMEDIA.
|
|
|
|
*/
|
2002-05-24 01:38:01 +04:00
|
|
|
goto iface_stats;
|
1997-03-17 06:08:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (ifmr.ifm_count == 0) {
|
1997-03-25 04:37:11 +03:00
|
|
|
warnx("%s: no media types?", name);
|
2002-05-24 01:38:01 +04:00
|
|
|
goto iface_stats;
|
1997-03-17 06:08:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
media_list = (int *)malloc(ifmr.ifm_count * sizeof(int));
|
|
|
|
if (media_list == NULL)
|
2001-10-25 02:10:15 +04:00
|
|
|
err(EXIT_FAILURE, "malloc");
|
1997-03-17 06:08:46 +03:00
|
|
|
ifmr.ifm_ulist = media_list;
|
|
|
|
|
2004-10-11 21:13:55 +04:00
|
|
|
if (ioctl(s, SIOCGIFMEDIA, &ifmr) == -1)
|
2001-10-25 02:10:15 +04:00
|
|
|
err(EXIT_FAILURE, "SIOCGIFMEDIA");
|
1997-03-17 06:08:46 +03:00
|
|
|
|
2004-11-11 23:37:18 +03:00
|
|
|
printf("\tmedia: %s ", get_media_type_string(ifmr.ifm_current));
|
|
|
|
print_media_word(ifmr.ifm_current, " ");
|
1997-03-17 06:08:46 +03:00
|
|
|
if (ifmr.ifm_active != ifmr.ifm_current) {
|
2004-11-11 23:37:18 +03:00
|
|
|
printf(" (");
|
|
|
|
print_media_word(ifmr.ifm_active, " ");
|
|
|
|
printf(")");
|
1997-03-17 06:08:46 +03:00
|
|
|
}
|
2004-11-11 23:37:18 +03:00
|
|
|
printf("\n");
|
1997-03-17 06:08:46 +03:00
|
|
|
|
2000-01-25 03:59:39 +03:00
|
|
|
if (ifmr.ifm_status & IFM_STATUS_VALID) {
|
|
|
|
const struct ifmedia_status_description *ifms;
|
|
|
|
int bitno, found = 0;
|
|
|
|
|
1998-08-06 06:24:40 +04:00
|
|
|
printf("\tstatus: ");
|
2000-01-25 03:59:39 +03:00
|
|
|
for (bitno = 0; ifm_status_valid_list[bitno] != 0; bitno++) {
|
|
|
|
for (ifms = ifm_status_descriptions;
|
|
|
|
ifms->ifms_valid != 0; ifms++) {
|
|
|
|
if (ifms->ifms_type !=
|
2000-01-25 05:16:57 +03:00
|
|
|
IFM_TYPE(ifmr.ifm_current) ||
|
2000-01-25 03:59:39 +03:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
1997-03-17 06:08:46 +03:00
|
|
|
|
2000-01-25 03:59:39 +03:00
|
|
|
if (found == 0)
|
1998-08-06 06:24:40 +04:00
|
|
|
printf("unknown");
|
2004-11-11 23:37:18 +03:00
|
|
|
printf("\n");
|
1997-03-17 06:08:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (mflag) {
|
1998-08-06 23:22:00 +04:00
|
|
|
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++) {
|
2004-11-11 23:37:18 +03:00
|
|
|
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;
|
1998-08-06 23:22:00 +04:00
|
|
|
}
|
2004-11-11 23:37:18 +03:00
|
|
|
printf("\t\tmedia ");
|
|
|
|
print_media_word(media_list[i], " mediaopt ");
|
|
|
|
printf("\n");
|
1998-08-06 23:22:00 +04:00
|
|
|
}
|
1997-03-17 06:08:46 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
free(media_list);
|
|
|
|
|
2002-05-24 01:38:01 +04:00
|
|
|
iface_stats:
|
2003-04-12 05:07:43 +04:00
|
|
|
if (!vflag && !zflag)
|
2002-05-24 01:38:01 +04:00
|
|
|
goto proto_status;
|
|
|
|
|
|
|
|
(void) strncpy(ifdr.ifdr_name, name, sizeof(ifdr.ifdr_name));
|
|
|
|
|
2004-10-11 21:13:55 +04:00
|
|
|
if (ioctl(s, zflag ? SIOCZIFDATA:SIOCGIFDATA, &ifdr) == -1) {
|
2003-04-12 05:07:43 +04:00
|
|
|
err(EXIT_FAILURE, zflag ? "SIOCZIFDATA" : "SIOCGIFDATA");
|
|
|
|
} else {
|
2002-05-24 01:38:01 +04:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2004-12-21 02:04:55 +03:00
|
|
|
ieee80211_statistics();
|
|
|
|
|
1997-03-17 06:08:46 +03:00
|
|
|
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);
|
1998-09-06 21:51:32 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2004-10-12 00:13:37 +04:00
|
|
|
in_alias(struct ifreq *creq)
|
1998-09-06 21:51:32 +04:00
|
|
|
{
|
2001-11-02 08:57:38 +03:00
|
|
|
struct sockaddr_in *iasin;
|
2000-04-13 11:16:54 +04:00
|
|
|
int alias;
|
1998-09-06 21:51:32 +04:00
|
|
|
|
|
|
|
if (lflag)
|
|
|
|
return;
|
|
|
|
|
2000-04-13 11:16:54 +04:00
|
|
|
alias = 1;
|
|
|
|
|
1998-09-06 21:51:32 +04:00
|
|
|
/* Get the non-alias address for this interface. */
|
|
|
|
getsock(AF_INET);
|
|
|
|
if (s < 0) {
|
|
|
|
if (errno == EPROTONOSUPPORT)
|
|
|
|
return;
|
2001-10-25 02:10:15 +04:00
|
|
|
err(EXIT_FAILURE, "socket");
|
1998-09-06 21:51:32 +04:00
|
|
|
}
|
|
|
|
(void) memset(&ifr, 0, sizeof(ifr));
|
|
|
|
(void) strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
|
2004-10-11 21:13:55 +04:00
|
|
|
if (ioctl(s, SIOCGIFADDR, &ifr) == -1) {
|
1998-09-06 21:51:32 +04:00
|
|
|
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)
|
2000-04-13 11:16:54 +04:00
|
|
|
alias = 0;
|
2002-04-25 13:39:17 +04:00
|
|
|
(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));
|
2004-10-11 21:13:55 +04:00
|
|
|
if (ioctl(s, SIOCGIFALIAS, &in_addreq) == -1) {
|
1998-09-06 21:51:32 +04:00
|
|
|
if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT) {
|
|
|
|
return;
|
|
|
|
} else
|
|
|
|
warn("SIOCGIFALIAS");
|
|
|
|
}
|
|
|
|
|
2002-04-25 13:39:17 +04:00
|
|
|
iasin = &in_addreq.ifra_addr;
|
2001-11-02 08:57:38 +03:00
|
|
|
printf("\tinet %s%s", alias ? "alias " : "", inet_ntoa(iasin->sin_addr));
|
1998-09-06 21:51:32 +04:00
|
|
|
|
|
|
|
if (flags & IFF_POINTOPOINT) {
|
2002-04-25 13:39:17 +04:00
|
|
|
iasin = &in_addreq.ifra_dstaddr;
|
2001-11-02 08:57:38 +03:00
|
|
|
printf(" -> %s", inet_ntoa(iasin->sin_addr));
|
1998-09-06 21:51:32 +04:00
|
|
|
}
|
|
|
|
|
2002-04-25 13:39:17 +04:00
|
|
|
iasin = &in_addreq.ifra_mask;
|
2001-11-02 08:57:38 +03:00
|
|
|
printf(" netmask 0x%x", ntohl(iasin->sin_addr.s_addr));
|
1998-09-06 21:51:32 +04:00
|
|
|
|
|
|
|
if (flags & IFF_BROADCAST) {
|
2002-04-25 13:39:17 +04:00
|
|
|
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
|
|
|
}
|
1998-09-06 21:51:32 +04:00
|
|
|
printf("\n");
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
|
1994-12-05 23:15:31 +03:00
|
|
|
void
|
2004-10-12 00:13:37 +04:00
|
|
|
in_status(int force)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
2000-04-13 11:39:57 +04:00
|
|
|
struct ifaddrs *ifap, *ifa;
|
2001-11-02 08:57:38 +03:00
|
|
|
struct ifreq isifr;
|
2000-04-13 11:39:57 +04:00
|
|
|
|
|
|
|
if (getifaddrs(&ifap) != 0)
|
2001-10-25 02:10:15 +04:00
|
|
|
err(EXIT_FAILURE, "getifaddrs");
|
2000-04-13 11:39:57 +04:00
|
|
|
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)
|
2000-04-13 11:39:57 +04:00
|
|
|
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);
|
2000-04-13 11:39:57 +04:00
|
|
|
}
|
|
|
|
freeifaddrs(ifap);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
|
1999-07-01 17:19:20 +04:00
|
|
|
void
|
2004-10-12 00:13:37 +04:00
|
|
|
setifprefixlen(const char *addr, int d)
|
1999-07-01 17:19:20 +04:00
|
|
|
{
|
|
|
|
if (*afp->af_getprefix)
|
|
|
|
(*afp->af_getprefix)(addr, MASK);
|
|
|
|
explicit_prefix = 1;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
1994-12-05 23:15:31 +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
|
|
|
|
2000-01-12 13:29:35 +03:00
|
|
|
if (which == ADDR) {
|
|
|
|
char *p = NULL;
|
2001-11-02 08:57:38 +03:00
|
|
|
if ((p = strrchr(str, '/')) != NULL) {
|
2001-01-17 09:10:37 +03:00
|
|
|
*p = '\0';
|
|
|
|
in_getprefix(p + 1, MASK);
|
|
|
|
}
|
2000-01-12 13:29:35 +03:00
|
|
|
}
|
|
|
|
|
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);
|
1995-05-20 00:17:16 +04:00
|
|
|
else
|
2002-06-14 13:01:59 +04:00
|
|
|
errx(EXIT_FAILURE, "%s: bad value", str);
|
1995-05-20 00:17:16 +04:00
|
|
|
}
|
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
|
|
|
{
|
2005-02-06 09:13:47 +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))
|
2002-06-14 13:01:59 +04:00
|
|
|
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;
|
2001-07-25 21:29:14 +04:00
|
|
|
if (len)
|
|
|
|
*cp = 0xff << (8 - len);
|
2001-01-17 09:07:32 +03:00
|
|
|
}
|
|
|
|
|
1995-01-22 00:22:16 +03:00
|
|
|
void
|
2004-10-12 00:13:37 +04:00
|
|
|
usage(void)
|
1995-01-22 00:22:16 +03:00
|
|
|
{
|
2001-02-20 01:48:57 +03:00
|
|
|
const char *progname = getprogname();
|
2000-06-30 21:45:11 +04:00
|
|
|
|
1997-03-28 01:50:09 +03:00
|
|
|
fprintf(stderr,
|
2003-04-12 05:07:43 +04:00
|
|
|
"usage: %s [-m] [-v] [-z] "
|
1999-07-01 17:19:20 +04:00
|
|
|
#ifdef INET6
|
2003-04-12 05:07:43 +04:00
|
|
|
"[-L] "
|
1999-07-01 17:19:20 +04:00
|
|
|
#endif
|
2000-06-30 21:45:11 +04:00
|
|
|
"interface\n"
|
2001-04-27 13:10:04 +04:00
|
|
|
"\t[ af [ address [ dest_addr ] ] [ netmask mask ] [ prefixlen n ]\n"
|
|
|
|
"\t\t[ alias | -alias ] ]\n"
|
2001-04-27 13:08:44 +04:00
|
|
|
"\t[ up ] [ down ] [ metric n ] [ mtu n ]\n"
|
2000-12-12 23:27:42 +03:00
|
|
|
"\t[ nwid network_id ] [ nwkey network_key | -nwkey ]\n"
|
|
|
|
"\t[ powersave | -powersave ] [ powersavesleep duration ]\n"
|
|
|
|
"\t[ [ af ] tunnel src_addr dest_addr ] [ deletetunnel ]\n"
|
2000-06-30 21:45:11 +04:00
|
|
|
"\t[ arp | -arp ]\n"
|
2000-12-12 23:27:42 +03:00
|
|
|
"\t[ media type ] [ mediaopt opts ] [ -mediaopt opts ] "
|
|
|
|
"[ instance minst ]\n"
|
2000-09-28 03:00:24 +04:00
|
|
|
"\t[ vlan n vlanif i ]\n"
|
2005-03-18 17:30:08 +03:00
|
|
|
"\t[ agrport i ] [ -agrport i ]\n"
|
2001-04-27 13:08:44 +04:00
|
|
|
"\t[ anycast | -anycast ] [ deprecated | -deprecated ]\n"
|
2002-06-14 13:01:59 +04:00
|
|
|
"\t[ tentative | -tentative ] [ pltime n ] [ vltime n ] [ eui64 ]\n"
|
2000-06-30 21:45:11 +04:00
|
|
|
"\t[ link0 | -link0 ] [ link1 | -link1 ] [ link2 | -link2 ]\n"
|
2003-04-12 05:07:43 +04:00
|
|
|
" %s -a [-b] [-m] [-d] [-u] [-v] [-z] [ af ]\n"
|
|
|
|
" %s -l [-b] [-d] [-u] [-s]\n"
|
2001-02-23 22:49:54 +03:00
|
|
|
" %s -C\n"
|
2000-07-02 04:28:00 +04:00
|
|
|
" %s interface create\n"
|
|
|
|
" %s interface destroy\n",
|
2001-02-23 22:49:54 +03:00
|
|
|
progname, progname, progname, progname, progname, progname);
|
1995-01-22 00:22:16 +03:00
|
|
|
exit(1);
|
|
|
|
}
|