1994-05-26 06:35:07 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 1983, 1989, 1991, 1993
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 4. Neither the name of the University nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef lint
|
1996-01-02 20:09:22 +00:00
|
|
|
static const char copyright[] =
|
1994-05-26 06:35:07 +00:00
|
|
|
"@(#) Copyright (c) 1983, 1989, 1991, 1993\n\
|
|
|
|
The Regents of the University of California. All rights reserved.\n";
|
|
|
|
#endif /* not lint */
|
|
|
|
|
|
|
|
#ifndef lint
|
1998-07-28 06:25:35 +00:00
|
|
|
#if 0
|
2001-10-17 09:11:23 +00:00
|
|
|
static char sccsid[] = "@(#)route.c 8.6 (Berkeley) 4/28/95";
|
1998-07-28 06:25:35 +00:00
|
|
|
#endif
|
1996-01-02 20:09:22 +00:00
|
|
|
static const char rcsid[] =
|
1999-08-28 00:22:10 +00:00
|
|
|
"$FreeBSD$";
|
1994-05-26 06:35:07 +00:00
|
|
|
#endif /* not lint */
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/file.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/sysctl.h>
|
1999-09-25 03:20:29 +00:00
|
|
|
#include <sys/types.h>
|
1994-05-26 06:35:07 +00:00
|
|
|
|
|
|
|
#include <net/if.h>
|
|
|
|
#include <net/route.h>
|
|
|
|
#include <net/if_dl.h>
|
|
|
|
#include <netinet/in.h>
|
2001-06-12 13:31:53 +00:00
|
|
|
#include <netinet/if_ether.h>
|
1996-07-09 19:02:28 +00:00
|
|
|
#include <netatalk/at.h>
|
1994-05-26 06:35:07 +00:00
|
|
|
#include <arpa/inet.h>
|
|
|
|
#include <netdb.h>
|
|
|
|
|
1996-12-10 17:11:53 +00:00
|
|
|
#include <ctype.h>
|
|
|
|
#include <err.h>
|
1994-05-26 06:35:07 +00:00
|
|
|
#include <errno.h>
|
2000-12-09 09:35:55 +00:00
|
|
|
#include <paths.h>
|
1994-05-26 06:35:07 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
1996-01-02 20:09:22 +00:00
|
|
|
#include <sysexits.h>
|
1996-12-10 17:11:53 +00:00
|
|
|
#include <unistd.h>
|
2001-06-11 12:39:29 +00:00
|
|
|
#include <ifaddrs.h>
|
2000-02-10 03:03:09 +00:00
|
|
|
|
1994-05-26 06:35:07 +00:00
|
|
|
struct keytab {
|
|
|
|
char *kt_cp;
|
|
|
|
int kt_i;
|
|
|
|
} keywords[] = {
|
|
|
|
#include "keywords.h"
|
|
|
|
{0, 0}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct ortentry route;
|
|
|
|
union sockunion {
|
|
|
|
struct sockaddr sa;
|
|
|
|
struct sockaddr_in sin;
|
1999-12-07 17:39:16 +00:00
|
|
|
#ifdef INET6
|
|
|
|
struct sockaddr_in6 sin6;
|
|
|
|
#endif
|
1996-07-09 19:02:28 +00:00
|
|
|
struct sockaddr_at sat;
|
1994-05-26 06:35:07 +00:00
|
|
|
struct sockaddr_dl sdl;
|
2001-06-12 13:31:53 +00:00
|
|
|
struct sockaddr_inarp sinarp;
|
2001-06-11 12:39:29 +00:00
|
|
|
struct sockaddr_storage ss; /* added to avoid memory overrun */
|
1994-05-26 06:35:07 +00:00
|
|
|
} so_dst, so_gate, so_mask, so_genmask, so_ifa, so_ifp;
|
|
|
|
|
|
|
|
typedef union sockunion *sup;
|
2001-08-31 12:31:09 +00:00
|
|
|
int pid, rtm_addrs;
|
1994-05-26 06:35:07 +00:00
|
|
|
int s;
|
|
|
|
int forcehost, forcenet, doflush, nflag, af, qflag, tflag, keyword();
|
|
|
|
int iflag, verbose, aflen = sizeof (struct sockaddr_in);
|
|
|
|
int locking, lockrest, debugonly;
|
|
|
|
struct rt_metrics rt_metrics;
|
|
|
|
u_long rtm_inits;
|
2001-08-31 12:31:09 +00:00
|
|
|
uid_t uid;
|
2002-03-21 13:20:49 +00:00
|
|
|
int atalk_aton(const char *, struct at_addr *);
|
|
|
|
char *atalk_ntoa(struct at_addr);
|
2001-06-11 12:39:29 +00:00
|
|
|
const char *routename(), *netname();
|
1994-05-26 06:35:07 +00:00
|
|
|
void flushroutes(), newroute(), monitor(), sockaddr(), sodump(), bprintf();
|
|
|
|
void print_getmsg(), print_rtmsg(), pmsg_common(), pmsg_addrs(), mask_addr();
|
2002-05-21 11:43:53 +00:00
|
|
|
#ifdef INET6
|
2002-05-21 18:11:31 +00:00
|
|
|
static int inet6_makenetandmask(struct sockaddr_in6 *, char *);
|
2002-05-21 11:43:53 +00:00
|
|
|
#endif
|
1994-05-26 06:35:07 +00:00
|
|
|
int getaddr(), rtmsg(), x25_makemask();
|
1999-12-07 17:39:16 +00:00
|
|
|
int prefixlen();
|
1999-09-25 03:20:29 +00:00
|
|
|
extern char *iso_ntoa();
|
1996-01-02 20:09:22 +00:00
|
|
|
|
2002-03-21 13:20:49 +00:00
|
|
|
void usage(const char *) __dead2;
|
1994-05-26 06:35:07 +00:00
|
|
|
|
1996-09-14 03:00:32 +00:00
|
|
|
void
|
1994-05-26 06:35:07 +00:00
|
|
|
usage(cp)
|
1996-01-02 20:09:22 +00:00
|
|
|
const char *cp;
|
1994-05-26 06:35:07 +00:00
|
|
|
{
|
|
|
|
if (cp)
|
1996-01-02 20:09:22 +00:00
|
|
|
warnx("bad keyword: %s", cp);
|
1994-05-26 06:35:07 +00:00
|
|
|
(void) fprintf(stderr,
|
1998-07-28 06:25:35 +00:00
|
|
|
"usage: route [-dnqtv] command [[modifiers] args]\n");
|
1996-01-02 20:09:22 +00:00
|
|
|
exit(EX_USAGE);
|
1994-05-26 06:35:07 +00:00
|
|
|
/* NOTREACHED */
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main(argc, argv)
|
|
|
|
int argc;
|
|
|
|
char **argv;
|
|
|
|
{
|
|
|
|
int ch;
|
|
|
|
|
|
|
|
if (argc < 2)
|
|
|
|
usage((char *)NULL);
|
|
|
|
|
1998-07-28 06:25:35 +00:00
|
|
|
while ((ch = getopt(argc, argv, "nqdtv")) != -1)
|
1994-05-26 06:35:07 +00:00
|
|
|
switch(ch) {
|
|
|
|
case 'n':
|
|
|
|
nflag = 1;
|
|
|
|
break;
|
|
|
|
case 'q':
|
|
|
|
qflag = 1;
|
|
|
|
break;
|
|
|
|
case 'v':
|
|
|
|
verbose = 1;
|
|
|
|
break;
|
|
|
|
case 't':
|
|
|
|
tflag = 1;
|
|
|
|
break;
|
|
|
|
case 'd':
|
|
|
|
debugonly = 1;
|
|
|
|
break;
|
|
|
|
case '?':
|
|
|
|
default:
|
|
|
|
usage((char *)NULL);
|
|
|
|
}
|
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
|
|
|
|
pid = getpid();
|
2003-01-25 01:28:11 +00:00
|
|
|
uid = geteuid();
|
1994-05-26 06:35:07 +00:00
|
|
|
if (tflag)
|
2000-12-09 09:35:55 +00:00
|
|
|
s = open(_PATH_DEVNULL, O_WRONLY, 0);
|
1994-05-26 06:35:07 +00:00
|
|
|
else
|
|
|
|
s = socket(PF_ROUTE, SOCK_RAW, 0);
|
|
|
|
if (s < 0)
|
1996-01-02 20:09:22 +00:00
|
|
|
err(EX_OSERR, "socket");
|
1994-05-26 06:35:07 +00:00
|
|
|
if (*argv)
|
|
|
|
switch (keyword(*argv)) {
|
|
|
|
case K_GET:
|
|
|
|
uid = 0;
|
|
|
|
/* FALLTHROUGH */
|
|
|
|
|
|
|
|
case K_CHANGE:
|
|
|
|
case K_ADD:
|
2005-09-28 12:12:15 +00:00
|
|
|
case K_DEL:
|
1994-05-26 06:35:07 +00:00
|
|
|
case K_DELETE:
|
|
|
|
newroute(argc, argv);
|
|
|
|
/* NOTREACHED */
|
|
|
|
|
|
|
|
case K_MONITOR:
|
|
|
|
monitor();
|
|
|
|
/* NOTREACHED */
|
|
|
|
|
|
|
|
case K_FLUSH:
|
|
|
|
flushroutes(argc, argv);
|
|
|
|
exit(0);
|
|
|
|
/* NOTREACHED */
|
|
|
|
}
|
|
|
|
usage(*argv);
|
|
|
|
/* NOTREACHED */
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Purge all entries in the routing tables not
|
|
|
|
* associated with network interfaces.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
flushroutes(argc, argv)
|
|
|
|
int argc;
|
|
|
|
char *argv[];
|
|
|
|
{
|
|
|
|
size_t needed;
|
2004-04-30 22:34:12 +00:00
|
|
|
int mib[6], rlen, seqno, count = 0;
|
1994-05-26 06:35:07 +00:00
|
|
|
char *buf, *next, *lim;
|
2002-03-20 17:55:10 +00:00
|
|
|
struct rt_msghdr *rtm;
|
1994-05-26 06:35:07 +00:00
|
|
|
|
2003-04-16 12:06:53 +00:00
|
|
|
if (uid && !debugonly) {
|
1996-01-02 20:09:22 +00:00
|
|
|
errx(EX_NOPERM, "must be root to alter routing table");
|
1994-05-26 06:35:07 +00:00
|
|
|
}
|
2005-05-11 02:50:41 +00:00
|
|
|
shutdown(s, SHUT_RD); /* Don't want to read back our messages */
|
1994-05-26 06:35:07 +00:00
|
|
|
if (argc > 1) {
|
|
|
|
argv++;
|
|
|
|
if (argc == 2 && **argv == '-')
|
|
|
|
switch (keyword(*argv + 1)) {
|
|
|
|
case K_INET:
|
|
|
|
af = AF_INET;
|
|
|
|
break;
|
1999-12-07 17:39:16 +00:00
|
|
|
#ifdef INET6
|
|
|
|
case K_INET6:
|
|
|
|
af = AF_INET6;
|
|
|
|
break;
|
|
|
|
#endif
|
1996-07-09 19:02:28 +00:00
|
|
|
case K_ATALK:
|
|
|
|
af = AF_APPLETALK;
|
|
|
|
break;
|
1994-05-26 06:35:07 +00:00
|
|
|
case K_LINK:
|
|
|
|
af = AF_LINK;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
goto bad;
|
|
|
|
} else
|
|
|
|
bad: usage(*argv);
|
|
|
|
}
|
2004-04-30 22:34:12 +00:00
|
|
|
retry:
|
1994-05-26 06:35:07 +00:00
|
|
|
mib[0] = CTL_NET;
|
|
|
|
mib[1] = PF_ROUTE;
|
|
|
|
mib[2] = 0; /* protocol */
|
|
|
|
mib[3] = 0; /* wildcard address family */
|
|
|
|
mib[4] = NET_RT_DUMP;
|
|
|
|
mib[5] = 0; /* no flags */
|
|
|
|
if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
|
1996-01-02 20:09:22 +00:00
|
|
|
err(EX_OSERR, "route-sysctl-estimate");
|
1994-05-26 06:35:07 +00:00
|
|
|
if ((buf = malloc(needed)) == NULL)
|
1998-07-28 06:25:35 +00:00
|
|
|
errx(EX_OSERR, "malloc failed");
|
2004-04-30 22:34:12 +00:00
|
|
|
if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0) {
|
|
|
|
if (errno == ENOMEM && count++ < 10) {
|
|
|
|
warnx("Routing table grew, retrying");
|
|
|
|
sleep(1);
|
|
|
|
free(buf);
|
|
|
|
goto retry;
|
|
|
|
}
|
1996-01-02 20:09:22 +00:00
|
|
|
err(EX_OSERR, "route-sysctl-get");
|
2004-04-30 22:34:12 +00:00
|
|
|
}
|
1994-05-26 06:35:07 +00:00
|
|
|
lim = buf + needed;
|
|
|
|
if (verbose)
|
|
|
|
(void) printf("Examining routing table from sysctl\n");
|
|
|
|
seqno = 0; /* ??? */
|
|
|
|
for (next = buf; next < lim; next += rtm->rtm_msglen) {
|
|
|
|
rtm = (struct rt_msghdr *)next;
|
|
|
|
if (verbose)
|
|
|
|
print_rtmsg(rtm, rtm->rtm_msglen);
|
|
|
|
if ((rtm->rtm_flags & RTF_GATEWAY) == 0)
|
|
|
|
continue;
|
|
|
|
if (af) {
|
|
|
|
struct sockaddr *sa = (struct sockaddr *)(rtm + 1);
|
|
|
|
|
|
|
|
if (sa->sa_family != af)
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (debugonly)
|
|
|
|
continue;
|
|
|
|
rtm->rtm_type = RTM_DELETE;
|
|
|
|
rtm->rtm_seq = seqno;
|
|
|
|
rlen = write(s, next, rtm->rtm_msglen);
|
2004-05-07 15:33:17 +00:00
|
|
|
if (rlen < 0 && errno == EPERM)
|
|
|
|
err(1, "write to routing socket");
|
1994-05-26 06:35:07 +00:00
|
|
|
if (rlen < (int)rtm->rtm_msglen) {
|
1996-01-02 20:09:22 +00:00
|
|
|
warn("write to routing socket");
|
1994-05-26 06:35:07 +00:00
|
|
|
(void) printf("got only %d for rlen\n", rlen);
|
2004-04-30 22:34:12 +00:00
|
|
|
free(buf);
|
|
|
|
goto retry;
|
1994-05-26 06:35:07 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
seqno++;
|
|
|
|
if (qflag)
|
|
|
|
continue;
|
|
|
|
if (verbose)
|
|
|
|
print_rtmsg(rtm, rlen);
|
|
|
|
else {
|
|
|
|
struct sockaddr *sa = (struct sockaddr *)(rtm + 1);
|
|
|
|
(void) printf("%-20.20s ", rtm->rtm_flags & RTF_HOST ?
|
|
|
|
routename(sa) : netname(sa));
|
2004-04-13 11:24:43 +00:00
|
|
|
sa = (struct sockaddr *)(SA_SIZE(sa) + (char *)sa);
|
1994-05-26 06:35:07 +00:00
|
|
|
(void) printf("%-20.20s ", routename(sa));
|
|
|
|
(void) printf("done\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-06-11 12:39:29 +00:00
|
|
|
const char *
|
1994-05-26 06:35:07 +00:00
|
|
|
routename(sa)
|
|
|
|
struct sockaddr *sa;
|
|
|
|
{
|
2002-03-20 17:55:10 +00:00
|
|
|
char *cp;
|
1996-10-27 17:42:14 +00:00
|
|
|
static char line[MAXHOSTNAMELEN + 1];
|
1994-05-26 06:35:07 +00:00
|
|
|
struct hostent *hp;
|
|
|
|
static char domain[MAXHOSTNAMELEN + 1];
|
2001-08-20 12:56:45 +00:00
|
|
|
static int first = 1, n;
|
1994-05-26 06:35:07 +00:00
|
|
|
|
|
|
|
if (first) {
|
|
|
|
first = 0;
|
|
|
|
if (gethostname(domain, MAXHOSTNAMELEN) == 0 &&
|
2001-10-17 09:11:23 +00:00
|
|
|
(cp = strchr(domain, '.'))) {
|
1996-10-27 17:42:14 +00:00
|
|
|
domain[MAXHOSTNAMELEN] = '\0';
|
1994-05-26 06:35:07 +00:00
|
|
|
(void) strcpy(domain, cp + 1);
|
1996-10-27 17:42:14 +00:00
|
|
|
} else
|
1994-05-26 06:35:07 +00:00
|
|
|
domain[0] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sa->sa_len == 0)
|
|
|
|
strcpy(line, "default");
|
|
|
|
else switch (sa->sa_family) {
|
|
|
|
|
|
|
|
case AF_INET:
|
|
|
|
{ struct in_addr in;
|
|
|
|
in = ((struct sockaddr_in *)sa)->sin_addr;
|
|
|
|
|
|
|
|
cp = 0;
|
|
|
|
if (in.s_addr == INADDR_ANY || sa->sa_len < 4)
|
|
|
|
cp = "default";
|
|
|
|
if (cp == 0 && !nflag) {
|
|
|
|
hp = gethostbyaddr((char *)&in, sizeof (struct in_addr),
|
|
|
|
AF_INET);
|
|
|
|
if (hp) {
|
2001-10-17 09:11:23 +00:00
|
|
|
if ((cp = strchr(hp->h_name, '.')) &&
|
1994-05-26 06:35:07 +00:00
|
|
|
!strcmp(cp + 1, domain))
|
|
|
|
*cp = 0;
|
|
|
|
cp = hp->h_name;
|
|
|
|
}
|
|
|
|
}
|
1997-12-24 00:59:49 +00:00
|
|
|
if (cp) {
|
|
|
|
strncpy(line, cp, sizeof(line) - 1);
|
|
|
|
line[sizeof(line) - 1] = '\0';
|
2001-06-07 13:50:24 +00:00
|
|
|
} else
|
|
|
|
(void) sprintf(line, "%s", inet_ntoa(in));
|
1994-05-26 06:35:07 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
1999-12-07 17:39:16 +00:00
|
|
|
#ifdef INET6
|
|
|
|
case AF_INET6:
|
2001-06-11 12:39:29 +00:00
|
|
|
{
|
|
|
|
struct sockaddr_in6 sin6; /* use static var for safety */
|
|
|
|
int niflags = 0;
|
|
|
|
|
|
|
|
memset(&sin6, 0, sizeof(sin6));
|
|
|
|
memcpy(&sin6, sa, sa->sa_len);
|
|
|
|
sin6.sin6_len = sizeof(struct sockaddr_in6);
|
|
|
|
sin6.sin6_family = AF_INET6;
|
|
|
|
#ifdef __KAME__
|
|
|
|
if (sa->sa_len == sizeof(struct sockaddr_in6) &&
|
|
|
|
(IN6_IS_ADDR_LINKLOCAL(&sin6.sin6_addr) ||
|
|
|
|
IN6_IS_ADDR_MC_LINKLOCAL(&sin6.sin6_addr)) &&
|
|
|
|
sin6.sin6_scope_id == 0) {
|
|
|
|
sin6.sin6_scope_id =
|
|
|
|
ntohs(*(u_int16_t *)&sin6.sin6_addr.s6_addr[2]);
|
|
|
|
sin6.sin6_addr.s6_addr[2] = 0;
|
|
|
|
sin6.sin6_addr.s6_addr[3] = 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
if (nflag)
|
|
|
|
niflags |= NI_NUMERICHOST;
|
|
|
|
if (getnameinfo((struct sockaddr *)&sin6, sin6.sin6_len,
|
|
|
|
line, sizeof(line), NULL, 0, niflags) != 0)
|
|
|
|
strncpy(line, "invalid", sizeof(line));
|
|
|
|
|
|
|
|
return(line);
|
|
|
|
}
|
1999-12-07 17:39:16 +00:00
|
|
|
#endif
|
|
|
|
|
1996-07-09 19:02:28 +00:00
|
|
|
case AF_APPLETALK:
|
|
|
|
(void) snprintf(line, sizeof(line), "atalk %s",
|
|
|
|
atalk_ntoa(((struct sockaddr_at *)sa)->sat_addr));
|
|
|
|
break;
|
|
|
|
|
1994-05-26 06:35:07 +00:00
|
|
|
case AF_LINK:
|
|
|
|
return (link_ntoa((struct sockaddr_dl *)sa));
|
|
|
|
|
|
|
|
default:
|
|
|
|
{ u_short *s = (u_short *)sa;
|
|
|
|
u_short *slim = s + ((sa->sa_len + 1) >> 1);
|
|
|
|
char *cp = line + sprintf(line, "(%d)", sa->sa_family);
|
1996-10-27 17:42:14 +00:00
|
|
|
char *cpe = line + sizeof(line);
|
1994-05-26 06:35:07 +00:00
|
|
|
|
1996-10-27 17:42:14 +00:00
|
|
|
while (++s < slim && cp < cpe) /* start with sa->sa_data */
|
2001-08-20 12:56:45 +00:00
|
|
|
if ((n = snprintf(cp, cpe - cp, " %x", *s)) > 0)
|
|
|
|
cp += n;
|
2001-08-20 14:53:05 +00:00
|
|
|
else
|
|
|
|
*cp = '\0';
|
1994-05-26 06:35:07 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return (line);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Return the name of the network whose address is given.
|
|
|
|
* The address is assumed to be that of a net or subnet, not a host.
|
|
|
|
*/
|
2001-06-11 12:39:29 +00:00
|
|
|
const char *
|
1994-05-26 06:35:07 +00:00
|
|
|
netname(sa)
|
|
|
|
struct sockaddr *sa;
|
|
|
|
{
|
|
|
|
char *cp = 0;
|
1996-10-27 17:42:14 +00:00
|
|
|
static char line[MAXHOSTNAMELEN + 1];
|
1994-05-26 06:35:07 +00:00
|
|
|
struct netent *np = 0;
|
|
|
|
u_long net, mask;
|
2002-03-20 17:55:10 +00:00
|
|
|
u_long i;
|
2001-08-20 12:56:45 +00:00
|
|
|
int n, subnetshift;
|
1994-05-26 06:35:07 +00:00
|
|
|
|
|
|
|
switch (sa->sa_family) {
|
|
|
|
|
|
|
|
case AF_INET:
|
|
|
|
{ struct in_addr in;
|
|
|
|
in = ((struct sockaddr_in *)sa)->sin_addr;
|
|
|
|
|
|
|
|
i = in.s_addr = ntohl(in.s_addr);
|
|
|
|
if (in.s_addr == 0)
|
|
|
|
cp = "default";
|
|
|
|
else if (!nflag) {
|
|
|
|
if (IN_CLASSA(i)) {
|
|
|
|
mask = IN_CLASSA_NET;
|
|
|
|
subnetshift = 8;
|
|
|
|
} else if (IN_CLASSB(i)) {
|
|
|
|
mask = IN_CLASSB_NET;
|
|
|
|
subnetshift = 8;
|
|
|
|
} else {
|
|
|
|
mask = IN_CLASSC_NET;
|
|
|
|
subnetshift = 4;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* If there are more bits than the standard mask
|
|
|
|
* would suggest, subnets must be in use.
|
|
|
|
* Guess at the subnet mask, assuming reasonable
|
|
|
|
* width subnet fields.
|
|
|
|
*/
|
|
|
|
while (in.s_addr &~ mask)
|
|
|
|
mask = (long)mask >> subnetshift;
|
|
|
|
net = in.s_addr & mask;
|
|
|
|
while ((mask & 1) == 0)
|
|
|
|
mask >>= 1, net >>= 1;
|
|
|
|
np = getnetbyaddr(net, AF_INET);
|
|
|
|
if (np)
|
|
|
|
cp = np->n_name;
|
|
|
|
}
|
2001-06-07 13:50:24 +00:00
|
|
|
#define C(x) (unsigned)((x) & 0xff)
|
1994-05-26 06:35:07 +00:00
|
|
|
if (cp)
|
1996-10-27 17:42:14 +00:00
|
|
|
strncpy(line, cp, sizeof(line));
|
1994-05-26 06:35:07 +00:00
|
|
|
else if ((in.s_addr & 0xffffff) == 0)
|
|
|
|
(void) sprintf(line, "%u", C(in.s_addr >> 24));
|
|
|
|
else if ((in.s_addr & 0xffff) == 0)
|
|
|
|
(void) sprintf(line, "%u.%u", C(in.s_addr >> 24),
|
|
|
|
C(in.s_addr >> 16));
|
|
|
|
else if ((in.s_addr & 0xff) == 0)
|
|
|
|
(void) sprintf(line, "%u.%u.%u", C(in.s_addr >> 24),
|
|
|
|
C(in.s_addr >> 16), C(in.s_addr >> 8));
|
|
|
|
else
|
|
|
|
(void) sprintf(line, "%u.%u.%u.%u", C(in.s_addr >> 24),
|
|
|
|
C(in.s_addr >> 16), C(in.s_addr >> 8),
|
|
|
|
C(in.s_addr));
|
2001-06-07 13:50:24 +00:00
|
|
|
#undef C
|
1994-05-26 06:35:07 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
1999-12-07 17:39:16 +00:00
|
|
|
#ifdef INET6
|
|
|
|
case AF_INET6:
|
2001-06-11 12:39:29 +00:00
|
|
|
{
|
|
|
|
struct sockaddr_in6 sin6; /* use static var for safety */
|
|
|
|
int niflags = 0;
|
|
|
|
|
|
|
|
memset(&sin6, 0, sizeof(sin6));
|
|
|
|
memcpy(&sin6, sa, sa->sa_len);
|
|
|
|
sin6.sin6_len = sizeof(struct sockaddr_in6);
|
|
|
|
sin6.sin6_family = AF_INET6;
|
|
|
|
#ifdef __KAME__
|
|
|
|
if (sa->sa_len == sizeof(struct sockaddr_in6) &&
|
|
|
|
(IN6_IS_ADDR_LINKLOCAL(&sin6.sin6_addr) ||
|
|
|
|
IN6_IS_ADDR_MC_LINKLOCAL(&sin6.sin6_addr)) &&
|
|
|
|
sin6.sin6_scope_id == 0) {
|
|
|
|
sin6.sin6_scope_id =
|
|
|
|
ntohs(*(u_int16_t *)&sin6.sin6_addr.s6_addr[2]);
|
|
|
|
sin6.sin6_addr.s6_addr[2] = 0;
|
|
|
|
sin6.sin6_addr.s6_addr[3] = 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
if (nflag)
|
|
|
|
niflags |= NI_NUMERICHOST;
|
|
|
|
if (getnameinfo((struct sockaddr *)&sin6, sin6.sin6_len,
|
|
|
|
line, sizeof(line), NULL, 0, niflags) != 0)
|
|
|
|
strncpy(line, "invalid", sizeof(line));
|
|
|
|
|
|
|
|
return(line);
|
|
|
|
}
|
1999-12-07 17:39:16 +00:00
|
|
|
#endif
|
|
|
|
|
1996-07-09 19:02:28 +00:00
|
|
|
case AF_APPLETALK:
|
|
|
|
(void) snprintf(line, sizeof(line), "atalk %s",
|
|
|
|
atalk_ntoa(((struct sockaddr_at *)sa)->sat_addr));
|
|
|
|
break;
|
|
|
|
|
1994-05-26 06:35:07 +00:00
|
|
|
case AF_LINK:
|
|
|
|
return (link_ntoa((struct sockaddr_dl *)sa));
|
|
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
{ u_short *s = (u_short *)sa->sa_data;
|
|
|
|
u_short *slim = s + ((sa->sa_len + 1)>>1);
|
|
|
|
char *cp = line + sprintf(line, "af %d:", sa->sa_family);
|
1996-10-27 17:42:14 +00:00
|
|
|
char *cpe = line + sizeof(line);
|
1994-05-26 06:35:07 +00:00
|
|
|
|
1996-10-27 17:42:14 +00:00
|
|
|
while (s < slim && cp < cpe)
|
2001-08-20 12:56:45 +00:00
|
|
|
if ((n = snprintf(cp, cpe - cp, " %x", *s++)) > 0)
|
|
|
|
cp += n;
|
2001-08-20 14:53:05 +00:00
|
|
|
else
|
|
|
|
*cp = '\0';
|
1994-05-26 06:35:07 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return (line);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
set_metric(value, key)
|
|
|
|
char *value;
|
|
|
|
int key;
|
|
|
|
{
|
|
|
|
int flag = 0;
|
|
|
|
u_long noval, *valp = &noval;
|
|
|
|
|
|
|
|
switch (key) {
|
|
|
|
#define caseof(x, y, z) case x: valp = &rt_metrics.z; flag = y; break
|
|
|
|
caseof(K_MTU, RTV_MTU, rmx_mtu);
|
|
|
|
caseof(K_HOPCOUNT, RTV_HOPCOUNT, rmx_hopcount);
|
|
|
|
caseof(K_EXPIRE, RTV_EXPIRE, rmx_expire);
|
|
|
|
caseof(K_RECVPIPE, RTV_RPIPE, rmx_recvpipe);
|
|
|
|
caseof(K_SENDPIPE, RTV_SPIPE, rmx_sendpipe);
|
|
|
|
caseof(K_SSTHRESH, RTV_SSTHRESH, rmx_ssthresh);
|
|
|
|
caseof(K_RTT, RTV_RTT, rmx_rtt);
|
|
|
|
caseof(K_RTTVAR, RTV_RTTVAR, rmx_rttvar);
|
|
|
|
}
|
|
|
|
rtm_inits |= flag;
|
|
|
|
if (lockrest || locking)
|
|
|
|
rt_metrics.rmx_locks |= flag;
|
|
|
|
if (locking)
|
|
|
|
locking = 0;
|
|
|
|
*valp = atoi(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
newroute(argc, argv)
|
|
|
|
int argc;
|
2002-03-20 17:55:10 +00:00
|
|
|
char **argv;
|
1994-05-26 06:35:07 +00:00
|
|
|
{
|
|
|
|
char *cmd, *dest = "", *gateway = "", *err;
|
2001-06-12 13:31:53 +00:00
|
|
|
int ishost = 0, proxy = 0, ret, attempts, oerrno, flags = RTF_STATIC;
|
1994-05-26 06:35:07 +00:00
|
|
|
int key;
|
|
|
|
struct hostent *hp = 0;
|
|
|
|
|
|
|
|
if (uid) {
|
1996-01-02 20:09:22 +00:00
|
|
|
errx(EX_NOPERM, "must be root to alter routing table");
|
1994-05-26 06:35:07 +00:00
|
|
|
}
|
|
|
|
cmd = argv[0];
|
|
|
|
if (*cmd != 'g')
|
2005-05-11 02:50:41 +00:00
|
|
|
shutdown(s, SHUT_RD); /* Don't want to read back our messages */
|
1994-05-26 06:35:07 +00:00
|
|
|
while (--argc > 0) {
|
|
|
|
if (**(++argv)== '-') {
|
|
|
|
switch (key = keyword(1 + *argv)) {
|
|
|
|
case K_LINK:
|
|
|
|
af = AF_LINK;
|
|
|
|
aflen = sizeof(struct sockaddr_dl);
|
|
|
|
break;
|
|
|
|
case K_INET:
|
|
|
|
af = AF_INET;
|
|
|
|
aflen = sizeof(struct sockaddr_in);
|
|
|
|
break;
|
1999-12-07 17:39:16 +00:00
|
|
|
#ifdef INET6
|
|
|
|
case K_INET6:
|
|
|
|
af = AF_INET6;
|
|
|
|
aflen = sizeof(struct sockaddr_in6);
|
|
|
|
break;
|
|
|
|
#endif
|
1996-07-09 19:02:28 +00:00
|
|
|
case K_ATALK:
|
|
|
|
af = AF_APPLETALK;
|
|
|
|
aflen = sizeof(struct sockaddr_at);
|
|
|
|
break;
|
1994-05-26 06:35:07 +00:00
|
|
|
case K_SA:
|
|
|
|
af = PF_ROUTE;
|
|
|
|
aflen = sizeof(union sockunion);
|
|
|
|
break;
|
|
|
|
case K_IFACE:
|
|
|
|
case K_INTERFACE:
|
|
|
|
iflag++;
|
1994-09-15 08:35:37 +00:00
|
|
|
break;
|
1994-05-26 06:35:07 +00:00
|
|
|
case K_NOSTATIC:
|
|
|
|
flags &= ~RTF_STATIC;
|
|
|
|
break;
|
|
|
|
case K_LOCK:
|
|
|
|
locking = 1;
|
|
|
|
break;
|
|
|
|
case K_LOCKREST:
|
|
|
|
lockrest = 1;
|
|
|
|
break;
|
|
|
|
case K_HOST:
|
|
|
|
forcehost++;
|
|
|
|
break;
|
|
|
|
case K_REJECT:
|
|
|
|
flags |= RTF_REJECT;
|
|
|
|
break;
|
|
|
|
case K_BLACKHOLE:
|
|
|
|
flags |= RTF_BLACKHOLE;
|
|
|
|
break;
|
|
|
|
case K_PROTO1:
|
|
|
|
flags |= RTF_PROTO1;
|
|
|
|
break;
|
|
|
|
case K_PROTO2:
|
|
|
|
flags |= RTF_PROTO2;
|
|
|
|
break;
|
2001-06-12 13:31:53 +00:00
|
|
|
case K_PROXY:
|
|
|
|
proxy = 1;
|
|
|
|
break;
|
1994-05-26 06:35:07 +00:00
|
|
|
case K_XRESOLVE:
|
|
|
|
flags |= RTF_XRESOLVE;
|
|
|
|
break;
|
|
|
|
case K_STATIC:
|
|
|
|
flags |= RTF_STATIC;
|
|
|
|
break;
|
|
|
|
case K_IFA:
|
1999-06-01 13:14:07 +00:00
|
|
|
if (!--argc)
|
|
|
|
usage((char *)NULL);
|
1994-05-26 06:35:07 +00:00
|
|
|
(void) getaddr(RTA_IFA, *++argv, 0);
|
|
|
|
break;
|
|
|
|
case K_IFP:
|
1999-06-01 13:14:07 +00:00
|
|
|
if (!--argc)
|
|
|
|
usage((char *)NULL);
|
1994-05-26 06:35:07 +00:00
|
|
|
(void) getaddr(RTA_IFP, *++argv, 0);
|
|
|
|
break;
|
|
|
|
case K_GENMASK:
|
1999-06-01 13:14:07 +00:00
|
|
|
if (!--argc)
|
|
|
|
usage((char *)NULL);
|
1994-05-26 06:35:07 +00:00
|
|
|
(void) getaddr(RTA_GENMASK, *++argv, 0);
|
|
|
|
break;
|
|
|
|
case K_GATEWAY:
|
1999-06-01 13:14:07 +00:00
|
|
|
if (!--argc)
|
|
|
|
usage((char *)NULL);
|
1994-05-26 06:35:07 +00:00
|
|
|
(void) getaddr(RTA_GATEWAY, *++argv, 0);
|
|
|
|
break;
|
|
|
|
case K_DST:
|
1999-06-01 13:14:07 +00:00
|
|
|
if (!--argc)
|
|
|
|
usage((char *)NULL);
|
1994-05-26 06:35:07 +00:00
|
|
|
ishost = getaddr(RTA_DST, *++argv, &hp);
|
|
|
|
dest = *argv;
|
|
|
|
break;
|
|
|
|
case K_NETMASK:
|
1999-06-01 13:14:07 +00:00
|
|
|
if (!--argc)
|
|
|
|
usage((char *)NULL);
|
1994-05-26 06:35:07 +00:00
|
|
|
(void) getaddr(RTA_NETMASK, *++argv, 0);
|
|
|
|
/* FALLTHROUGH */
|
|
|
|
case K_NET:
|
|
|
|
forcenet++;
|
|
|
|
break;
|
1999-12-07 17:39:16 +00:00
|
|
|
case K_PREFIXLEN:
|
|
|
|
if (!--argc)
|
|
|
|
usage((char *)NULL);
|
|
|
|
if (prefixlen(*++argv) == -1) {
|
|
|
|
forcenet = 0;
|
|
|
|
ishost = 1;
|
|
|
|
} else {
|
|
|
|
forcenet = 1;
|
|
|
|
ishost = 0;
|
|
|
|
}
|
|
|
|
break;
|
1994-05-26 06:35:07 +00:00
|
|
|
case K_MTU:
|
|
|
|
case K_HOPCOUNT:
|
|
|
|
case K_EXPIRE:
|
|
|
|
case K_RECVPIPE:
|
|
|
|
case K_SENDPIPE:
|
|
|
|
case K_SSTHRESH:
|
|
|
|
case K_RTT:
|
|
|
|
case K_RTTVAR:
|
1999-06-01 13:14:07 +00:00
|
|
|
if (!--argc)
|
|
|
|
usage((char *)NULL);
|
1994-05-26 06:35:07 +00:00
|
|
|
set_metric(*++argv, key);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
usage(1+*argv);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if ((rtm_addrs & RTA_DST) == 0) {
|
|
|
|
dest = *argv;
|
|
|
|
ishost = getaddr(RTA_DST, *argv, &hp);
|
|
|
|
} else if ((rtm_addrs & RTA_GATEWAY) == 0) {
|
|
|
|
gateway = *argv;
|
|
|
|
(void) getaddr(RTA_GATEWAY, *argv, &hp);
|
|
|
|
} else {
|
|
|
|
(void) getaddr(RTA_NETMASK, *argv, 0);
|
2001-07-11 16:11:15 +00:00
|
|
|
forcenet = 1;
|
1994-05-26 06:35:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
1999-12-07 17:39:16 +00:00
|
|
|
if (forcehost) {
|
1994-05-26 06:35:07 +00:00
|
|
|
ishost = 1;
|
1999-12-07 17:39:16 +00:00
|
|
|
#ifdef INET6
|
|
|
|
if (af == AF_INET6) {
|
|
|
|
rtm_addrs &= ~RTA_NETMASK;
|
2009-04-06 10:09:20 +00:00
|
|
|
memset((void *)&so_mask, 0, sizeof(so_mask));
|
1999-12-07 17:39:16 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
1994-05-26 06:35:07 +00:00
|
|
|
if (forcenet)
|
|
|
|
ishost = 0;
|
|
|
|
flags |= RTF_UP;
|
|
|
|
if (ishost)
|
|
|
|
flags |= RTF_HOST;
|
|
|
|
if (iflag == 0)
|
|
|
|
flags |= RTF_GATEWAY;
|
2001-06-12 13:31:53 +00:00
|
|
|
if (proxy) {
|
|
|
|
so_dst.sinarp.sin_other = SIN_PROXY;
|
|
|
|
flags |= RTF_ANNOUNCE;
|
|
|
|
}
|
1994-05-26 06:35:07 +00:00
|
|
|
for (attempts = 1; ; attempts++) {
|
|
|
|
errno = 0;
|
|
|
|
if ((ret = rtmsg(*cmd, flags)) == 0)
|
|
|
|
break;
|
|
|
|
if (errno != ENETUNREACH && errno != ESRCH)
|
|
|
|
break;
|
|
|
|
if (af == AF_INET && *gateway && hp && hp->h_addr_list[1]) {
|
|
|
|
hp->h_addr_list++;
|
2001-10-17 09:11:23 +00:00
|
|
|
memmove(&so_gate.sin.sin_addr, hp->h_addr_list[0],
|
1997-12-24 00:59:49 +00:00
|
|
|
MIN(hp->h_length, sizeof(so_gate.sin.sin_addr)));
|
1994-05-26 06:35:07 +00:00
|
|
|
} else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (*cmd == 'g')
|
2008-03-22 12:50:43 +00:00
|
|
|
exit(ret != 0);
|
2002-05-25 12:51:34 +00:00
|
|
|
if (!qflag) {
|
|
|
|
oerrno = errno;
|
|
|
|
(void) printf("%s %s %s", cmd, ishost? "host" : "net", dest);
|
|
|
|
if (*gateway) {
|
|
|
|
(void) printf(": gateway %s", gateway);
|
|
|
|
if (attempts > 1 && ret == 0 && af == AF_INET)
|
|
|
|
(void) printf(" (%s)",
|
|
|
|
inet_ntoa(((struct sockaddr_in *)&route.rt_gateway)->sin_addr));
|
|
|
|
}
|
|
|
|
if (ret == 0) {
|
|
|
|
(void) printf("\n");
|
|
|
|
} else {
|
|
|
|
switch (oerrno) {
|
|
|
|
case ESRCH:
|
|
|
|
err = "not in table";
|
|
|
|
break;
|
|
|
|
case EBUSY:
|
|
|
|
err = "entry in use";
|
|
|
|
break;
|
|
|
|
case ENOBUFS:
|
2005-09-21 12:24:46 +00:00
|
|
|
err = "not enough memory";
|
2002-05-25 12:51:34 +00:00
|
|
|
break;
|
2005-09-21 12:24:46 +00:00
|
|
|
case EADDRINUSE:
|
|
|
|
/* handle recursion avoidance in rt_setgate() */
|
2002-05-25 12:51:34 +00:00
|
|
|
err = "gateway uses the same route";
|
|
|
|
break;
|
2005-09-21 12:24:46 +00:00
|
|
|
case EEXIST:
|
|
|
|
err = "route already in table";
|
|
|
|
break;
|
2002-05-25 12:51:34 +00:00
|
|
|
default:
|
|
|
|
err = strerror(oerrno);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
(void) printf(": %s\n", err);
|
1994-05-26 06:35:07 +00:00
|
|
|
}
|
|
|
|
}
|
2002-05-25 12:51:34 +00:00
|
|
|
exit(ret != 0);
|
1994-05-26 06:35:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
1997-04-02 17:05:30 +00:00
|
|
|
inet_makenetandmask(net, sin, bits)
|
|
|
|
u_long net, bits;
|
2002-03-20 17:55:10 +00:00
|
|
|
struct sockaddr_in *sin;
|
1994-05-26 06:35:07 +00:00
|
|
|
{
|
|
|
|
u_long addr, mask = 0;
|
2002-03-20 17:55:10 +00:00
|
|
|
char *cp;
|
1994-05-26 06:35:07 +00:00
|
|
|
|
|
|
|
rtm_addrs |= RTA_NETMASK;
|
2009-01-18 11:38:35 +00:00
|
|
|
/*
|
|
|
|
* XXX: This approach unable to handle 0.0.0.1/32 correctly
|
|
|
|
* as inet_network() converts 0.0.0.1 and 1 equally.
|
|
|
|
*/
|
|
|
|
if (net <= 0xff)
|
|
|
|
addr = net << IN_CLASSA_NSHIFT;
|
|
|
|
else if (net <= 0xffff)
|
|
|
|
addr = net << IN_CLASSB_NSHIFT;
|
|
|
|
else if (net <= 0xffffff)
|
|
|
|
addr = net << IN_CLASSC_NSHIFT;
|
|
|
|
else
|
|
|
|
addr = net;
|
2009-04-06 10:09:20 +00:00
|
|
|
/*
|
|
|
|
* If no /xx was specified we must cacluate the
|
|
|
|
* CIDR address.
|
|
|
|
*/
|
|
|
|
if ((bits == 0) && (addr != 0)) {
|
|
|
|
int i, j;
|
|
|
|
for(i=0,j=1; i<32; i++) {
|
|
|
|
if (addr & j) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
j <<= 1;
|
|
|
|
}
|
|
|
|
/* i holds the first non zero bit */
|
|
|
|
bits = 32 - i;
|
|
|
|
}
|
|
|
|
mask = 0xffffffff << (32 - bits);
|
2007-10-29 00:08:24 +00:00
|
|
|
|
1994-05-26 06:35:07 +00:00
|
|
|
sin->sin_addr.s_addr = htonl(addr);
|
|
|
|
sin = &so_mask.sin;
|
|
|
|
sin->sin_addr.s_addr = htonl(mask);
|
|
|
|
sin->sin_len = 0;
|
|
|
|
sin->sin_family = 0;
|
|
|
|
cp = (char *)(&sin->sin_addr + 1);
|
|
|
|
while (*--cp == 0 && cp > (char *)sin)
|
|
|
|
;
|
|
|
|
sin->sin_len = 1 + cp - (char *)sin;
|
|
|
|
}
|
|
|
|
|
2002-05-20 15:01:19 +00:00
|
|
|
#ifdef INET6
|
|
|
|
/*
|
|
|
|
* XXX the function may need more improvement...
|
|
|
|
*/
|
2002-05-21 11:43:53 +00:00
|
|
|
static int
|
2002-05-21 18:11:31 +00:00
|
|
|
inet6_makenetandmask(sin6, plen)
|
2002-05-20 15:01:19 +00:00
|
|
|
struct sockaddr_in6 *sin6;
|
|
|
|
char *plen;
|
2002-05-21 18:11:31 +00:00
|
|
|
{
|
2002-05-20 15:01:19 +00:00
|
|
|
struct in6_addr in6;
|
|
|
|
|
2002-05-21 18:11:31 +00:00
|
|
|
if (!plen) {
|
|
|
|
if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr) &&
|
|
|
|
sin6->sin6_scope_id == 0) {
|
|
|
|
plen = "0";
|
|
|
|
} else if ((sin6->sin6_addr.s6_addr[0] & 0xe0) == 0x20) {
|
|
|
|
/* aggregatable global unicast - RFC2374 */
|
|
|
|
memset(&in6, 0, sizeof(in6));
|
|
|
|
if (!memcmp(&sin6->sin6_addr.s6_addr[8],
|
|
|
|
&in6.s6_addr[8], 8))
|
|
|
|
plen = "64";
|
|
|
|
}
|
2002-05-20 15:01:19 +00:00
|
|
|
}
|
|
|
|
|
2002-06-08 19:49:22 +00:00
|
|
|
if (!plen || strcmp(plen, "128") == 0)
|
|
|
|
return 1;
|
|
|
|
rtm_addrs |= RTA_NETMASK;
|
|
|
|
(void)prefixlen(plen);
|
2002-05-21 11:43:53 +00:00
|
|
|
return 0;
|
2002-05-20 15:01:19 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
1994-05-26 06:35:07 +00:00
|
|
|
/*
|
|
|
|
* Interpret an argument as a network address of some kind,
|
|
|
|
* returning 1 if a host address, 0 if a network address.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
getaddr(which, s, hpp)
|
|
|
|
int which;
|
|
|
|
char *s;
|
|
|
|
struct hostent **hpp;
|
|
|
|
{
|
2002-03-20 17:55:10 +00:00
|
|
|
sup su;
|
1994-05-26 06:35:07 +00:00
|
|
|
struct hostent *hp;
|
|
|
|
struct netent *np;
|
|
|
|
u_long val;
|
2000-09-29 10:50:11 +00:00
|
|
|
char *q;
|
1999-12-07 17:39:16 +00:00
|
|
|
int afamily; /* local copy of af so we can change it */
|
1994-05-26 06:35:07 +00:00
|
|
|
|
|
|
|
if (af == 0) {
|
|
|
|
af = AF_INET;
|
|
|
|
aflen = sizeof(struct sockaddr_in);
|
|
|
|
}
|
1999-12-07 17:39:16 +00:00
|
|
|
afamily = af;
|
1994-05-26 06:35:07 +00:00
|
|
|
rtm_addrs |= which;
|
|
|
|
switch (which) {
|
|
|
|
case RTA_DST:
|
|
|
|
su = &so_dst;
|
|
|
|
break;
|
|
|
|
case RTA_GATEWAY:
|
|
|
|
su = &so_gate;
|
1996-08-09 22:52:02 +00:00
|
|
|
if (iflag) {
|
2001-06-11 12:39:29 +00:00
|
|
|
struct ifaddrs *ifap, *ifa;
|
|
|
|
struct sockaddr_dl *sdl = NULL;
|
|
|
|
|
|
|
|
if (getifaddrs(&ifap))
|
|
|
|
err(1, "getifaddrs");
|
|
|
|
|
|
|
|
for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
|
|
|
|
if (ifa->ifa_addr->sa_family != AF_LINK)
|
|
|
|
continue;
|
|
|
|
|
2001-06-12 13:23:43 +00:00
|
|
|
if (strcmp(s, ifa->ifa_name))
|
2001-06-11 12:39:29 +00:00
|
|
|
continue;
|
1996-08-09 22:52:02 +00:00
|
|
|
|
2001-06-11 12:39:29 +00:00
|
|
|
sdl = (struct sockaddr_dl *)ifa->ifa_addr;
|
|
|
|
}
|
1996-08-09 22:52:02 +00:00
|
|
|
/* If we found it, then use it */
|
|
|
|
if (sdl) {
|
2001-06-11 12:39:29 +00:00
|
|
|
/*
|
|
|
|
* Copy is safe since we have a
|
|
|
|
* sockaddr_storage member in sockunion{}.
|
|
|
|
* Note that we need to copy before calling
|
|
|
|
* freeifaddrs().
|
|
|
|
*/
|
|
|
|
memcpy(&su->sdl, sdl, sdl->sdl_len);
|
1996-08-09 22:52:02 +00:00
|
|
|
}
|
2001-06-11 12:39:29 +00:00
|
|
|
freeifaddrs(ifap);
|
|
|
|
if (sdl)
|
|
|
|
return(1);
|
1996-08-09 22:52:02 +00:00
|
|
|
}
|
1994-05-26 06:35:07 +00:00
|
|
|
break;
|
|
|
|
case RTA_NETMASK:
|
|
|
|
su = &so_mask;
|
|
|
|
break;
|
|
|
|
case RTA_GENMASK:
|
|
|
|
su = &so_genmask;
|
|
|
|
break;
|
|
|
|
case RTA_IFP:
|
|
|
|
su = &so_ifp;
|
1999-12-07 17:39:16 +00:00
|
|
|
afamily = AF_LINK;
|
1994-05-26 06:35:07 +00:00
|
|
|
break;
|
|
|
|
case RTA_IFA:
|
|
|
|
su = &so_ifa;
|
|
|
|
break;
|
|
|
|
default:
|
1998-07-28 06:25:35 +00:00
|
|
|
usage("internal error");
|
1994-05-26 06:35:07 +00:00
|
|
|
/*NOTREACHED*/
|
|
|
|
}
|
|
|
|
su->sa.sa_len = aflen;
|
1999-12-07 17:39:16 +00:00
|
|
|
su->sa.sa_family = afamily; /* cases that don't want it have left already */
|
1994-05-26 06:35:07 +00:00
|
|
|
if (strcmp(s, "default") == 0) {
|
1997-07-18 09:05:12 +00:00
|
|
|
/*
|
|
|
|
* Default is net 0.0.0.0/0
|
|
|
|
*/
|
1994-05-26 06:35:07 +00:00
|
|
|
switch (which) {
|
|
|
|
case RTA_DST:
|
|
|
|
forcenet++;
|
2002-05-30 21:41:42 +00:00
|
|
|
#if 0
|
|
|
|
bzero(su, sizeof(*su)); /* for readability */
|
|
|
|
#endif
|
1994-05-26 06:35:07 +00:00
|
|
|
(void) getaddr(RTA_NETMASK, s, 0);
|
|
|
|
break;
|
2002-05-30 21:41:42 +00:00
|
|
|
#if 0
|
1994-05-26 06:35:07 +00:00
|
|
|
case RTA_NETMASK:
|
|
|
|
case RTA_GENMASK:
|
2002-05-30 21:41:42 +00:00
|
|
|
bzero(su, sizeof(*su)); /* for readability */
|
|
|
|
#endif
|
1994-05-26 06:35:07 +00:00
|
|
|
}
|
|
|
|
return (0);
|
|
|
|
}
|
1999-12-07 17:39:16 +00:00
|
|
|
switch (afamily) {
|
|
|
|
#ifdef INET6
|
|
|
|
case AF_INET6:
|
2001-06-11 12:39:29 +00:00
|
|
|
{
|
2000-02-10 03:03:09 +00:00
|
|
|
struct addrinfo hints, *res;
|
2005-05-23 14:12:32 +00:00
|
|
|
int ecode;
|
2001-06-11 12:39:29 +00:00
|
|
|
|
2002-05-21 18:11:31 +00:00
|
|
|
q = NULL;
|
|
|
|
if (which == RTA_DST && (q = strchr(s, '/')) != NULL)
|
|
|
|
*q = '\0';
|
2001-06-11 12:39:29 +00:00
|
|
|
memset(&hints, 0, sizeof(hints));
|
|
|
|
hints.ai_family = afamily; /*AF_INET6*/
|
|
|
|
hints.ai_socktype = SOCK_DGRAM; /*dummy*/
|
2005-05-23 14:12:32 +00:00
|
|
|
ecode = getaddrinfo(s, NULL, &hints, &res);
|
|
|
|
if (ecode != 0 || res->ai_family != AF_INET6 ||
|
2001-06-11 12:39:29 +00:00
|
|
|
res->ai_addrlen != sizeof(su->sin6)) {
|
2005-05-23 14:12:32 +00:00
|
|
|
(void) fprintf(stderr, "%s: %s\n", s,
|
|
|
|
gai_strerror(ecode));
|
1999-12-07 17:39:16 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
2001-06-11 12:39:29 +00:00
|
|
|
memcpy(&su->sin6, res->ai_addr, sizeof(su->sin6));
|
|
|
|
#ifdef __KAME__
|
|
|
|
if ((IN6_IS_ADDR_LINKLOCAL(&su->sin6.sin6_addr) ||
|
2003-12-07 11:11:26 +00:00
|
|
|
IN6_IS_ADDR_MC_LINKLOCAL(&su->sin6.sin6_addr)) &&
|
2001-06-11 12:39:29 +00:00
|
|
|
su->sin6.sin6_scope_id) {
|
|
|
|
*(u_int16_t *)&su->sin6.sin6_addr.s6_addr[2] =
|
|
|
|
htons(su->sin6.sin6_scope_id);
|
|
|
|
su->sin6.sin6_scope_id = 0;
|
|
|
|
}
|
1999-12-07 17:39:16 +00:00
|
|
|
#endif
|
2001-06-11 12:39:29 +00:00
|
|
|
freeaddrinfo(res);
|
2002-05-21 18:11:31 +00:00
|
|
|
if (q != NULL)
|
|
|
|
*q++ = '/';
|
2002-05-21 11:43:53 +00:00
|
|
|
if (which == RTA_DST)
|
2002-06-08 19:49:22 +00:00
|
|
|
return (inet6_makenetandmask(&su->sin6, q));
|
2001-06-11 12:39:29 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
#endif /* INET6 */
|
1999-12-07 17:39:16 +00:00
|
|
|
|
1996-07-09 19:02:28 +00:00
|
|
|
case AF_APPLETALK:
|
|
|
|
if (!atalk_aton(s, &su->sat.sat_addr))
|
|
|
|
errx(EX_NOHOST, "bad address: %s", s);
|
1996-07-23 22:00:14 +00:00
|
|
|
rtm_addrs |= RTA_NETMASK;
|
|
|
|
return(forcehost || su->sat.sat_addr.s_node != 0);
|
1996-07-09 19:02:28 +00:00
|
|
|
|
1994-05-26 06:35:07 +00:00
|
|
|
case AF_LINK:
|
|
|
|
link_addr(s, &su->sdl);
|
|
|
|
return (1);
|
|
|
|
|
|
|
|
|
|
|
|
case PF_ROUTE:
|
|
|
|
su->sa.sa_len = sizeof(*su);
|
|
|
|
sockaddr(s, &su->sa);
|
|
|
|
return (1);
|
|
|
|
|
|
|
|
case AF_INET:
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (hpp == NULL)
|
|
|
|
hpp = &hp;
|
|
|
|
*hpp = NULL;
|
1997-04-02 17:05:30 +00:00
|
|
|
|
|
|
|
q = strchr(s,'/');
|
|
|
|
if (q && which == RTA_DST) {
|
|
|
|
*q = '\0';
|
2001-06-08 09:07:34 +00:00
|
|
|
if ((val = inet_network(s)) != INADDR_NONE) {
|
1997-04-02 17:05:30 +00:00
|
|
|
inet_makenetandmask(
|
2001-06-08 09:07:34 +00:00
|
|
|
val, &su->sin, strtoul(q+1, 0, 0));
|
1997-04-02 17:05:30 +00:00
|
|
|
return (0);
|
|
|
|
}
|
2000-09-29 10:50:11 +00:00
|
|
|
*q = '/';
|
1997-04-02 17:05:30 +00:00
|
|
|
}
|
2000-09-29 10:50:11 +00:00
|
|
|
if ((which != RTA_DST || forcenet == 0) &&
|
2001-07-11 16:11:15 +00:00
|
|
|
inet_aton(s, &su->sin.sin_addr)) {
|
|
|
|
val = su->sin.sin_addr.s_addr;
|
2004-06-16 06:29:41 +00:00
|
|
|
if (which != RTA_DST || forcehost ||
|
2000-09-29 10:50:11 +00:00
|
|
|
inet_lnaof(su->sin.sin_addr) != INADDR_ANY)
|
1994-05-26 06:35:07 +00:00
|
|
|
return (1);
|
|
|
|
else {
|
|
|
|
val = ntohl(val);
|
|
|
|
goto netdone;
|
|
|
|
}
|
|
|
|
}
|
2000-09-29 10:50:11 +00:00
|
|
|
if (which == RTA_DST && forcehost == 0 &&
|
|
|
|
((val = inet_network(s)) != INADDR_NONE ||
|
|
|
|
((np = getnetbyname(s)) != NULL && (val = np->n_net) != 0))) {
|
1994-05-26 06:35:07 +00:00
|
|
|
netdone:
|
2000-09-29 10:50:11 +00:00
|
|
|
inet_makenetandmask(val, &su->sin, 0);
|
1994-05-26 06:35:07 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
hp = gethostbyname(s);
|
|
|
|
if (hp) {
|
|
|
|
*hpp = hp;
|
|
|
|
su->sin.sin_family = hp->h_addrtype;
|
2001-10-17 09:11:23 +00:00
|
|
|
memmove((char *)&su->sin.sin_addr, hp->h_addr,
|
1997-12-26 23:28:17 +00:00
|
|
|
MIN(hp->h_length, sizeof(su->sin.sin_addr)));
|
1994-05-26 06:35:07 +00:00
|
|
|
return (1);
|
|
|
|
}
|
1996-01-20 12:56:57 +00:00
|
|
|
errx(EX_NOHOST, "bad address: %s", s);
|
1994-05-26 06:35:07 +00:00
|
|
|
}
|
|
|
|
|
1999-12-07 17:39:16 +00:00
|
|
|
int
|
|
|
|
prefixlen(s)
|
|
|
|
char *s;
|
|
|
|
{
|
|
|
|
int len = atoi(s), q, r;
|
|
|
|
int max;
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
rtm_addrs |= RTA_NETMASK;
|
|
|
|
switch (af) {
|
|
|
|
#ifdef INET6
|
|
|
|
case AF_INET6:
|
|
|
|
max = 128;
|
|
|
|
p = (char *)&so_mask.sin6.sin6_addr;
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
case AF_INET:
|
|
|
|
max = 32;
|
|
|
|
p = (char *)&so_mask.sin.sin_addr;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
(void) fprintf(stderr, "prefixlen not supported in this af\n");
|
|
|
|
exit(1);
|
|
|
|
/*NOTREACHED*/
|
|
|
|
}
|
|
|
|
|
|
|
|
if (len < 0 || max < len) {
|
|
|
|
(void) fprintf(stderr, "%s: bad value\n", s);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
q = len >> 3;
|
|
|
|
r = len & 7;
|
|
|
|
so_mask.sa.sa_family = af;
|
|
|
|
so_mask.sa.sa_len = aflen;
|
|
|
|
memset((void *)p, 0, max / 8);
|
|
|
|
if (q > 0)
|
|
|
|
memset((void *)p, 0xff, q);
|
|
|
|
if (r > 0)
|
|
|
|
*((u_char *)p + q) = (0xff00 >> r) & 0xff;
|
|
|
|
if (len == max)
|
|
|
|
return -1;
|
|
|
|
else
|
|
|
|
return len;
|
|
|
|
}
|
1994-05-26 06:35:07 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
interfaces()
|
|
|
|
{
|
|
|
|
size_t needed;
|
|
|
|
int mib[6];
|
2004-04-30 22:34:12 +00:00
|
|
|
char *buf, *lim, *next, count = 0;
|
2002-03-20 17:55:10 +00:00
|
|
|
struct rt_msghdr *rtm;
|
1994-05-26 06:35:07 +00:00
|
|
|
|
2004-04-30 22:34:12 +00:00
|
|
|
retry2:
|
1994-05-26 06:35:07 +00:00
|
|
|
mib[0] = CTL_NET;
|
|
|
|
mib[1] = PF_ROUTE;
|
|
|
|
mib[2] = 0; /* protocol */
|
|
|
|
mib[3] = 0; /* wildcard address family */
|
|
|
|
mib[4] = NET_RT_IFLIST;
|
|
|
|
mib[5] = 0; /* no flags */
|
|
|
|
if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
|
1996-01-02 20:09:22 +00:00
|
|
|
err(EX_OSERR, "route-sysctl-estimate");
|
1994-05-26 06:35:07 +00:00
|
|
|
if ((buf = malloc(needed)) == NULL)
|
1998-07-28 06:25:35 +00:00
|
|
|
errx(EX_OSERR, "malloc failed");
|
2004-04-30 22:34:12 +00:00
|
|
|
if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0) {
|
|
|
|
if (errno == ENOMEM && count++ < 10) {
|
|
|
|
warnx("Routing table grew, retrying");
|
|
|
|
sleep(1);
|
|
|
|
free(buf);
|
|
|
|
goto retry2;
|
|
|
|
}
|
1996-01-02 20:09:22 +00:00
|
|
|
err(EX_OSERR, "actual retrieval of interface table");
|
2004-04-30 22:34:12 +00:00
|
|
|
}
|
1994-05-26 06:35:07 +00:00
|
|
|
lim = buf + needed;
|
|
|
|
for (next = buf; next < lim; next += rtm->rtm_msglen) {
|
|
|
|
rtm = (struct rt_msghdr *)next;
|
|
|
|
print_rtmsg(rtm, rtm->rtm_msglen);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
monitor()
|
|
|
|
{
|
|
|
|
int n;
|
|
|
|
char msg[2048];
|
|
|
|
|
|
|
|
verbose = 1;
|
|
|
|
if (debugonly) {
|
|
|
|
interfaces();
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
for(;;) {
|
1999-12-07 17:39:16 +00:00
|
|
|
time_t now;
|
1994-05-26 06:35:07 +00:00
|
|
|
n = read(s, msg, 2048);
|
1999-12-07 17:39:16 +00:00
|
|
|
now = time(NULL);
|
2001-01-15 12:28:48 +00:00
|
|
|
(void) printf("\ngot message of size %d on %s", n, ctime(&now));
|
1994-05-26 06:35:07 +00:00
|
|
|
print_rtmsg((struct rt_msghdr *)msg, n);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct {
|
|
|
|
struct rt_msghdr m_rtm;
|
|
|
|
char m_space[512];
|
|
|
|
} m_rtmsg;
|
|
|
|
|
|
|
|
int
|
|
|
|
rtmsg(cmd, flags)
|
|
|
|
int cmd, flags;
|
|
|
|
{
|
|
|
|
static int seq;
|
|
|
|
int rlen;
|
2002-03-20 17:55:10 +00:00
|
|
|
char *cp = m_rtmsg.m_space;
|
|
|
|
int l;
|
1994-05-26 06:35:07 +00:00
|
|
|
|
|
|
|
#define NEXTADDR(w, u) \
|
|
|
|
if (rtm_addrs & (w)) {\
|
2004-04-13 11:24:43 +00:00
|
|
|
l = SA_SIZE(&(u.sa)); memmove(cp, &(u), l); cp += l;\
|
2008-04-10 12:16:20 +00:00
|
|
|
if (verbose) sodump(&(u),#u);\
|
1994-05-26 06:35:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
errno = 0;
|
2001-10-17 09:11:23 +00:00
|
|
|
memset(&m_rtmsg, 0, sizeof(m_rtmsg));
|
1994-05-26 06:35:07 +00:00
|
|
|
if (cmd == 'a')
|
|
|
|
cmd = RTM_ADD;
|
|
|
|
else if (cmd == 'c')
|
|
|
|
cmd = RTM_CHANGE;
|
|
|
|
else if (cmd == 'g') {
|
|
|
|
cmd = RTM_GET;
|
|
|
|
if (so_ifp.sa.sa_family == 0) {
|
1996-01-02 20:09:22 +00:00
|
|
|
so_ifp.sa.sa_family = AF_LINK;
|
|
|
|
so_ifp.sa.sa_len = sizeof(struct sockaddr_dl);
|
1994-05-26 06:35:07 +00:00
|
|
|
rtm_addrs |= RTA_IFP;
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
cmd = RTM_DELETE;
|
|
|
|
#define rtm m_rtmsg.m_rtm
|
|
|
|
rtm.rtm_type = cmd;
|
|
|
|
rtm.rtm_flags = flags;
|
|
|
|
rtm.rtm_version = RTM_VERSION;
|
|
|
|
rtm.rtm_seq = ++seq;
|
|
|
|
rtm.rtm_addrs = rtm_addrs;
|
|
|
|
rtm.rtm_rmx = rt_metrics;
|
|
|
|
rtm.rtm_inits = rtm_inits;
|
|
|
|
|
|
|
|
if (rtm_addrs & RTA_NETMASK)
|
|
|
|
mask_addr();
|
|
|
|
NEXTADDR(RTA_DST, so_dst);
|
|
|
|
NEXTADDR(RTA_GATEWAY, so_gate);
|
|
|
|
NEXTADDR(RTA_NETMASK, so_mask);
|
|
|
|
NEXTADDR(RTA_GENMASK, so_genmask);
|
|
|
|
NEXTADDR(RTA_IFP, so_ifp);
|
|
|
|
NEXTADDR(RTA_IFA, so_ifa);
|
|
|
|
rtm.rtm_msglen = l = cp - (char *)&m_rtmsg;
|
|
|
|
if (verbose)
|
|
|
|
print_rtmsg(&rtm, l);
|
|
|
|
if (debugonly)
|
|
|
|
return (0);
|
|
|
|
if ((rlen = write(s, (char *)&m_rtmsg, l)) < 0) {
|
2004-05-07 15:33:17 +00:00
|
|
|
if (errno == EPERM)
|
|
|
|
err(1, "writing to routing socket");
|
1998-07-28 06:25:35 +00:00
|
|
|
warn("writing to routing socket");
|
1994-05-26 06:35:07 +00:00
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
if (cmd == RTM_GET) {
|
|
|
|
do {
|
|
|
|
l = read(s, (char *)&m_rtmsg, sizeof(m_rtmsg));
|
|
|
|
} while (l > 0 && (rtm.rtm_seq != seq || rtm.rtm_pid != pid));
|
|
|
|
if (l < 0)
|
1996-01-02 20:09:22 +00:00
|
|
|
warn("read from routing socket");
|
1994-05-26 06:35:07 +00:00
|
|
|
else
|
|
|
|
print_getmsg(&rtm, l);
|
|
|
|
}
|
|
|
|
#undef rtm
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
mask_addr()
|
|
|
|
{
|
|
|
|
int olen = so_mask.sa.sa_len;
|
2002-03-20 17:55:10 +00:00
|
|
|
char *cp1 = olen + (char *)&so_mask, *cp2;
|
1994-05-26 06:35:07 +00:00
|
|
|
|
|
|
|
for (so_mask.sa.sa_len = 0; cp1 > (char *)&so_mask; )
|
|
|
|
if (*--cp1 != 0) {
|
|
|
|
so_mask.sa.sa_len = 1 + cp1 - (char *)&so_mask;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if ((rtm_addrs & RTA_DST) == 0)
|
|
|
|
return;
|
|
|
|
switch (so_dst.sa.sa_family) {
|
|
|
|
case AF_INET:
|
1999-12-07 17:39:16 +00:00
|
|
|
#ifdef INET6
|
|
|
|
case AF_INET6:
|
|
|
|
#endif
|
1996-07-09 19:02:28 +00:00
|
|
|
case AF_APPLETALK:
|
1994-05-26 06:35:07 +00:00
|
|
|
case 0:
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
cp1 = so_mask.sa.sa_len + 1 + (char *)&so_dst;
|
|
|
|
cp2 = so_dst.sa.sa_len + 1 + (char *)&so_dst;
|
|
|
|
while (cp2 > cp1)
|
|
|
|
*--cp2 = 0;
|
|
|
|
cp2 = so_mask.sa.sa_len + 1 + (char *)&so_mask;
|
|
|
|
while (cp1 > so_dst.sa.sa_data)
|
|
|
|
*--cp1 &= *--cp2;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *msgtypes[] = {
|
|
|
|
"",
|
|
|
|
"RTM_ADD: Add Route",
|
|
|
|
"RTM_DELETE: Delete Route",
|
|
|
|
"RTM_CHANGE: Change Metrics or flags",
|
|
|
|
"RTM_GET: Report Metrics",
|
|
|
|
"RTM_LOSING: Kernel Suspects Partitioning",
|
|
|
|
"RTM_REDIRECT: Told to use different route",
|
|
|
|
"RTM_MISS: Lookup failed on this address",
|
|
|
|
"RTM_LOCK: fix specified metrics",
|
|
|
|
"RTM_OLDADD: caused by SIOCADDRT",
|
|
|
|
"RTM_OLDDEL: caused by SIOCDELRT",
|
|
|
|
"RTM_RESOLVE: Route created by cloning",
|
|
|
|
"RTM_NEWADDR: address being added to iface",
|
|
|
|
"RTM_DELADDR: address being removed from iface",
|
|
|
|
"RTM_IFINFO: iface status change",
|
1997-01-09 21:34:03 +00:00
|
|
|
"RTM_NEWMADDR: new multicast group membership on iface",
|
|
|
|
"RTM_DELMADDR: multicast group membership removed from iface",
|
2002-01-18 14:33:04 +00:00
|
|
|
"RTM_IFANNOUNCE: interface arrival/departure",
|
1994-05-26 06:35:07 +00:00
|
|
|
0,
|
|
|
|
};
|
|
|
|
|
|
|
|
char metricnames[] =
|
1996-05-08 20:48:59 +00:00
|
|
|
"\011pksent\010rttvar\7rtt\6ssthresh\5sendpipe\4recvpipe\3expire\2hopcount"
|
|
|
|
"\1mtu";
|
1994-05-26 06:35:07 +00:00
|
|
|
char routeflags[] =
|
1996-05-08 20:48:59 +00:00
|
|
|
"\1UP\2GATEWAY\3HOST\4REJECT\5DYNAMIC\6MODIFIED\7DONE\010MASK_PRESENT"
|
|
|
|
"\011CLONING\012XRESOLVE\013LLINFO\014STATIC\015BLACKHOLE\016b016"
|
|
|
|
"\017PROTO2\020PROTO1\021PRCLONING\022WASCLONED\023PROTO3\024CHAINDELETE"
|
|
|
|
"\025PINNED\026LOCAL\027BROADCAST\030MULTICAST";
|
1994-05-26 06:35:07 +00:00
|
|
|
char ifnetflags[] =
|
1996-05-08 20:48:59 +00:00
|
|
|
"\1UP\2BROADCAST\3DEBUG\4LOOPBACK\5PTP\6b6\7RUNNING\010NOARP"
|
|
|
|
"\011PPROMISC\012ALLMULTI\013OACTIVE\014SIMPLEX\015LINK0\016LINK1"
|
|
|
|
"\017LINK2\020MULTICAST";
|
1994-05-26 06:35:07 +00:00
|
|
|
char addrnames[] =
|
|
|
|
"\1DST\2GATEWAY\3NETMASK\4GENMASK\5IFP\6IFA\7AUTHOR\010BRD";
|
|
|
|
|
|
|
|
void
|
|
|
|
print_rtmsg(rtm, msglen)
|
2002-03-20 17:55:10 +00:00
|
|
|
struct rt_msghdr *rtm;
|
1994-05-26 06:35:07 +00:00
|
|
|
int msglen;
|
|
|
|
{
|
|
|
|
struct if_msghdr *ifm;
|
|
|
|
struct ifa_msghdr *ifam;
|
1997-01-09 21:34:03 +00:00
|
|
|
#ifdef RTM_NEWMADDR
|
|
|
|
struct ifma_msghdr *ifmam;
|
|
|
|
#endif
|
2002-01-18 14:33:04 +00:00
|
|
|
struct if_announcemsghdr *ifan;
|
2004-05-03 16:30:32 +00:00
|
|
|
char *state;
|
1994-05-26 06:35:07 +00:00
|
|
|
|
|
|
|
if (verbose == 0)
|
|
|
|
return;
|
|
|
|
if (rtm->rtm_version != RTM_VERSION) {
|
|
|
|
(void) printf("routing message version %d not understood\n",
|
|
|
|
rtm->rtm_version);
|
|
|
|
return;
|
|
|
|
}
|
2002-01-18 14:33:04 +00:00
|
|
|
if (msgtypes[rtm->rtm_type] != NULL)
|
|
|
|
(void)printf("%s: ", msgtypes[rtm->rtm_type]);
|
|
|
|
else
|
|
|
|
(void)printf("#%d: ", rtm->rtm_type);
|
|
|
|
(void)printf("len %d, ", rtm->rtm_msglen);
|
1994-05-26 06:35:07 +00:00
|
|
|
switch (rtm->rtm_type) {
|
|
|
|
case RTM_IFINFO:
|
|
|
|
ifm = (struct if_msghdr *)rtm;
|
2004-05-03 16:30:32 +00:00
|
|
|
(void) printf("if# %d, ", ifm->ifm_index);
|
|
|
|
switch (ifm->ifm_data.ifi_link_state) {
|
|
|
|
case LINK_STATE_DOWN:
|
|
|
|
state = "down";
|
|
|
|
break;
|
|
|
|
case LINK_STATE_UP:
|
|
|
|
state = "up";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
state = "unknown";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
(void) printf("link: %s, flags:", state);
|
1994-05-26 06:35:07 +00:00
|
|
|
bprintf(stdout, ifm->ifm_flags, ifnetflags);
|
|
|
|
pmsg_addrs((char *)(ifm + 1), ifm->ifm_addrs);
|
|
|
|
break;
|
|
|
|
case RTM_NEWADDR:
|
|
|
|
case RTM_DELADDR:
|
|
|
|
ifam = (struct ifa_msghdr *)rtm;
|
|
|
|
(void) printf("metric %d, flags:", ifam->ifam_metric);
|
|
|
|
bprintf(stdout, ifam->ifam_flags, routeflags);
|
|
|
|
pmsg_addrs((char *)(ifam + 1), ifam->ifam_addrs);
|
|
|
|
break;
|
1997-01-09 21:34:03 +00:00
|
|
|
#ifdef RTM_NEWMADDR
|
|
|
|
case RTM_NEWMADDR:
|
|
|
|
case RTM_DELMADDR:
|
|
|
|
ifmam = (struct ifma_msghdr *)rtm;
|
|
|
|
pmsg_addrs((char *)(ifmam + 1), ifmam->ifmam_addrs);
|
|
|
|
break;
|
|
|
|
#endif
|
2002-01-18 14:33:04 +00:00
|
|
|
case RTM_IFANNOUNCE:
|
|
|
|
ifan = (struct if_announcemsghdr *)rtm;
|
|
|
|
(void) printf("if# %d, what: ", ifan->ifan_index);
|
|
|
|
switch (ifan->ifan_what) {
|
|
|
|
case IFAN_ARRIVAL:
|
|
|
|
printf("arrival");
|
|
|
|
break;
|
|
|
|
case IFAN_DEPARTURE:
|
|
|
|
printf("departure");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
printf("#%d", ifan->ifan_what);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
printf("\n");
|
|
|
|
break;
|
|
|
|
|
1994-05-26 06:35:07 +00:00
|
|
|
default:
|
1996-01-02 20:09:22 +00:00
|
|
|
(void) printf("pid: %ld, seq %d, errno %d, flags:",
|
|
|
|
(long)rtm->rtm_pid, rtm->rtm_seq, rtm->rtm_errno);
|
1994-05-26 06:35:07 +00:00
|
|
|
bprintf(stdout, rtm->rtm_flags, routeflags);
|
|
|
|
pmsg_common(rtm);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
print_getmsg(rtm, msglen)
|
2002-03-20 17:55:10 +00:00
|
|
|
struct rt_msghdr *rtm;
|
1994-05-26 06:35:07 +00:00
|
|
|
int msglen;
|
|
|
|
{
|
|
|
|
struct sockaddr *dst = NULL, *gate = NULL, *mask = NULL;
|
|
|
|
struct sockaddr_dl *ifp = NULL;
|
2002-03-20 17:55:10 +00:00
|
|
|
struct sockaddr *sa;
|
|
|
|
char *cp;
|
|
|
|
int i;
|
1994-05-26 06:35:07 +00:00
|
|
|
|
|
|
|
(void) printf(" route to: %s\n", routename(&so_dst));
|
|
|
|
if (rtm->rtm_version != RTM_VERSION) {
|
1996-01-02 20:09:22 +00:00
|
|
|
warnx("routing message version %d not understood",
|
|
|
|
rtm->rtm_version);
|
1994-05-26 06:35:07 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (rtm->rtm_msglen > msglen) {
|
1998-07-28 06:25:35 +00:00
|
|
|
warnx("message length mismatch, in packet %d, returned %d",
|
1996-01-02 20:09:22 +00:00
|
|
|
rtm->rtm_msglen, msglen);
|
1994-05-26 06:35:07 +00:00
|
|
|
}
|
|
|
|
if (rtm->rtm_errno) {
|
1996-01-02 20:09:22 +00:00
|
|
|
errno = rtm->rtm_errno;
|
|
|
|
warn("message indicates error %d", errno);
|
1994-05-26 06:35:07 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
cp = ((char *)(rtm + 1));
|
|
|
|
if (rtm->rtm_addrs)
|
|
|
|
for (i = 1; i; i <<= 1)
|
|
|
|
if (i & rtm->rtm_addrs) {
|
|
|
|
sa = (struct sockaddr *)cp;
|
|
|
|
switch (i) {
|
|
|
|
case RTA_DST:
|
|
|
|
dst = sa;
|
|
|
|
break;
|
|
|
|
case RTA_GATEWAY:
|
|
|
|
gate = sa;
|
|
|
|
break;
|
|
|
|
case RTA_NETMASK:
|
|
|
|
mask = sa;
|
|
|
|
break;
|
|
|
|
case RTA_IFP:
|
|
|
|
if (sa->sa_family == AF_LINK &&
|
|
|
|
((struct sockaddr_dl *)sa)->sdl_nlen)
|
|
|
|
ifp = (struct sockaddr_dl *)sa;
|
|
|
|
break;
|
|
|
|
}
|
2004-04-13 11:24:43 +00:00
|
|
|
cp += SA_SIZE(sa);
|
1994-05-26 06:35:07 +00:00
|
|
|
}
|
|
|
|
if (dst && mask)
|
|
|
|
mask->sa_family = dst->sa_family; /* XXX */
|
|
|
|
if (dst)
|
|
|
|
(void)printf("destination: %s\n", routename(dst));
|
|
|
|
if (mask) {
|
|
|
|
int savenflag = nflag;
|
|
|
|
|
|
|
|
nflag = 1;
|
|
|
|
(void)printf(" mask: %s\n", routename(mask));
|
|
|
|
nflag = savenflag;
|
|
|
|
}
|
|
|
|
if (gate && rtm->rtm_flags & RTF_GATEWAY)
|
|
|
|
(void)printf(" gateway: %s\n", routename(gate));
|
|
|
|
if (ifp)
|
|
|
|
(void)printf(" interface: %.*s\n",
|
|
|
|
ifp->sdl_nlen, ifp->sdl_data);
|
|
|
|
(void)printf(" flags: ");
|
|
|
|
bprintf(stdout, rtm->rtm_flags, routeflags);
|
|
|
|
|
|
|
|
#define lock(f) ((rtm->rtm_rmx.rmx_locks & __CONCAT(RTV_,f)) ? 'L' : ' ')
|
|
|
|
#define msec(u) (((u) + 500) / 1000) /* usec to msec */
|
|
|
|
|
|
|
|
(void) printf("\n%s\n", "\
|
|
|
|
recvpipe sendpipe ssthresh rtt,msec rttvar hopcount mtu expire");
|
1996-01-02 20:09:22 +00:00
|
|
|
printf("%8ld%c ", rtm->rtm_rmx.rmx_recvpipe, lock(RPIPE));
|
|
|
|
printf("%8ld%c ", rtm->rtm_rmx.rmx_sendpipe, lock(SPIPE));
|
|
|
|
printf("%8ld%c ", rtm->rtm_rmx.rmx_ssthresh, lock(SSTHRESH));
|
|
|
|
printf("%8ld%c ", msec(rtm->rtm_rmx.rmx_rtt), lock(RTT));
|
|
|
|
printf("%8ld%c ", msec(rtm->rtm_rmx.rmx_rttvar), lock(RTTVAR));
|
|
|
|
printf("%8ld%c ", rtm->rtm_rmx.rmx_hopcount, lock(HOPCOUNT));
|
|
|
|
printf("%8ld%c ", rtm->rtm_rmx.rmx_mtu, lock(MTU));
|
1994-05-26 06:35:07 +00:00
|
|
|
if (rtm->rtm_rmx.rmx_expire)
|
|
|
|
rtm->rtm_rmx.rmx_expire -= time(0);
|
1996-01-02 20:09:22 +00:00
|
|
|
printf("%8ld%c\n", rtm->rtm_rmx.rmx_expire, lock(EXPIRE));
|
1994-05-26 06:35:07 +00:00
|
|
|
#undef lock
|
|
|
|
#undef msec
|
|
|
|
#define RTA_IGN (RTA_DST|RTA_GATEWAY|RTA_NETMASK|RTA_IFP|RTA_IFA|RTA_BRD)
|
|
|
|
if (verbose)
|
|
|
|
pmsg_common(rtm);
|
|
|
|
else if (rtm->rtm_addrs &~ RTA_IGN) {
|
|
|
|
(void) printf("sockaddrs: ");
|
|
|
|
bprintf(stdout, rtm->rtm_addrs, addrnames);
|
|
|
|
putchar('\n');
|
|
|
|
}
|
|
|
|
#undef RTA_IGN
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
pmsg_common(rtm)
|
2002-03-20 17:55:10 +00:00
|
|
|
struct rt_msghdr *rtm;
|
1994-05-26 06:35:07 +00:00
|
|
|
{
|
|
|
|
(void) printf("\nlocks: ");
|
|
|
|
bprintf(stdout, rtm->rtm_rmx.rmx_locks, metricnames);
|
|
|
|
(void) printf(" inits: ");
|
|
|
|
bprintf(stdout, rtm->rtm_inits, metricnames);
|
|
|
|
pmsg_addrs(((char *)(rtm + 1)), rtm->rtm_addrs);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
pmsg_addrs(cp, addrs)
|
|
|
|
char *cp;
|
|
|
|
int addrs;
|
|
|
|
{
|
2002-03-20 17:55:10 +00:00
|
|
|
struct sockaddr *sa;
|
1994-05-26 06:35:07 +00:00
|
|
|
int i;
|
|
|
|
|
2001-01-15 12:28:48 +00:00
|
|
|
if (addrs == 0) {
|
|
|
|
(void) putchar('\n');
|
1994-05-26 06:35:07 +00:00
|
|
|
return;
|
2001-01-15 12:28:48 +00:00
|
|
|
}
|
1994-05-26 06:35:07 +00:00
|
|
|
(void) printf("\nsockaddrs: ");
|
|
|
|
bprintf(stdout, addrs, addrnames);
|
|
|
|
(void) putchar('\n');
|
|
|
|
for (i = 1; i; i <<= 1)
|
|
|
|
if (i & addrs) {
|
|
|
|
sa = (struct sockaddr *)cp;
|
|
|
|
(void) printf(" %s", routename(sa));
|
2004-04-13 11:24:43 +00:00
|
|
|
cp += SA_SIZE(sa);
|
1994-05-26 06:35:07 +00:00
|
|
|
}
|
|
|
|
(void) putchar('\n');
|
|
|
|
(void) fflush(stdout);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
bprintf(fp, b, s)
|
2002-03-20 17:55:10 +00:00
|
|
|
FILE *fp;
|
|
|
|
int b;
|
|
|
|
u_char *s;
|
1994-05-26 06:35:07 +00:00
|
|
|
{
|
2002-03-20 17:55:10 +00:00
|
|
|
int i;
|
1994-05-26 06:35:07 +00:00
|
|
|
int gotsome = 0;
|
|
|
|
|
|
|
|
if (b == 0)
|
|
|
|
return;
|
1996-01-02 20:09:22 +00:00
|
|
|
while ((i = *s++) != 0) {
|
1994-05-26 06:35:07 +00:00
|
|
|
if (b & (1 << (i-1))) {
|
|
|
|
if (gotsome == 0)
|
|
|
|
i = '<';
|
|
|
|
else
|
|
|
|
i = ',';
|
|
|
|
(void) putc(i, fp);
|
|
|
|
gotsome = 1;
|
|
|
|
for (; (i = *s) > 32; s++)
|
|
|
|
(void) putc(i, fp);
|
|
|
|
} else
|
|
|
|
while (*s > 32)
|
|
|
|
s++;
|
|
|
|
}
|
|
|
|
if (gotsome)
|
|
|
|
(void) putc('>', fp);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
keyword(cp)
|
|
|
|
char *cp;
|
|
|
|
{
|
2002-03-20 17:55:10 +00:00
|
|
|
struct keytab *kt = keywords;
|
1994-05-26 06:35:07 +00:00
|
|
|
|
|
|
|
while (kt->kt_cp && strcmp(kt->kt_cp, cp))
|
|
|
|
kt++;
|
|
|
|
return kt->kt_i;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
sodump(su, which)
|
2002-03-20 17:55:10 +00:00
|
|
|
sup su;
|
1994-05-26 06:35:07 +00:00
|
|
|
char *which;
|
|
|
|
{
|
|
|
|
switch (su->sa.sa_family) {
|
|
|
|
case AF_LINK:
|
|
|
|
(void) printf("%s: link %s; ",
|
|
|
|
which, link_ntoa(&su->sdl));
|
|
|
|
break;
|
|
|
|
case AF_INET:
|
|
|
|
(void) printf("%s: inet %s; ",
|
|
|
|
which, inet_ntoa(su->sin.sin_addr));
|
|
|
|
break;
|
1996-07-09 19:02:28 +00:00
|
|
|
case AF_APPLETALK:
|
|
|
|
(void) printf("%s: atalk %s; ",
|
|
|
|
which, atalk_ntoa(su->sat.sat_addr));
|
|
|
|
break;
|
1994-05-26 06:35:07 +00:00
|
|
|
}
|
|
|
|
(void) fflush(stdout);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* States*/
|
|
|
|
#define VIRGIN 0
|
|
|
|
#define GOTONE 1
|
|
|
|
#define GOTTWO 2
|
|
|
|
/* Inputs */
|
|
|
|
#define DIGIT (4*0)
|
|
|
|
#define END (4*1)
|
|
|
|
#define DELIM (4*2)
|
|
|
|
|
|
|
|
void
|
|
|
|
sockaddr(addr, sa)
|
2002-03-20 17:55:10 +00:00
|
|
|
char *addr;
|
|
|
|
struct sockaddr *sa;
|
1994-05-26 06:35:07 +00:00
|
|
|
{
|
2002-03-20 17:55:10 +00:00
|
|
|
char *cp = (char *)sa;
|
1994-05-26 06:35:07 +00:00
|
|
|
int size = sa->sa_len;
|
|
|
|
char *cplim = cp + size;
|
2002-03-20 17:55:10 +00:00
|
|
|
int byte = 0, state = VIRGIN, new = 0 /* foil gcc */;
|
1994-05-26 06:35:07 +00:00
|
|
|
|
2001-10-17 09:11:23 +00:00
|
|
|
memset(cp, 0, size);
|
1994-05-26 06:35:07 +00:00
|
|
|
cp++;
|
|
|
|
do {
|
|
|
|
if ((*addr >= '0') && (*addr <= '9')) {
|
|
|
|
new = *addr - '0';
|
|
|
|
} else if ((*addr >= 'a') && (*addr <= 'f')) {
|
|
|
|
new = *addr - 'a' + 10;
|
|
|
|
} else if ((*addr >= 'A') && (*addr <= 'F')) {
|
|
|
|
new = *addr - 'A' + 10;
|
|
|
|
} else if (*addr == 0)
|
|
|
|
state |= END;
|
|
|
|
else
|
|
|
|
state |= DELIM;
|
|
|
|
addr++;
|
|
|
|
switch (state /* | INPUT */) {
|
|
|
|
case GOTTWO | DIGIT:
|
|
|
|
*cp++ = byte; /*FALLTHROUGH*/
|
|
|
|
case VIRGIN | DIGIT:
|
|
|
|
state = GOTONE; byte = new; continue;
|
|
|
|
case GOTONE | DIGIT:
|
|
|
|
state = GOTTWO; byte = new + (byte << 4); continue;
|
|
|
|
default: /* | DELIM */
|
|
|
|
state = VIRGIN; *cp++ = byte; byte = 0; continue;
|
|
|
|
case GOTONE | END:
|
|
|
|
case GOTTWO | END:
|
|
|
|
*cp++ = byte; /* FALLTHROUGH */
|
|
|
|
case VIRGIN | END:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
} while (cp < cplim);
|
|
|
|
sa->sa_len = cp - (char *)sa;
|
|
|
|
}
|
1996-07-09 19:02:28 +00:00
|
|
|
|
|
|
|
int
|
|
|
|
atalk_aton(const char *text, struct at_addr *addr)
|
|
|
|
{
|
|
|
|
u_int net, node;
|
|
|
|
|
|
|
|
if (sscanf(text, "%u.%u", &net, &node) != 2
|
|
|
|
|| net > 0xffff || node > 0xff)
|
|
|
|
return(0);
|
1996-07-23 01:18:47 +00:00
|
|
|
addr->s_net = htons(net);
|
1996-07-09 19:02:28 +00:00
|
|
|
addr->s_node = node;
|
|
|
|
return(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
char *
|
|
|
|
atalk_ntoa(struct at_addr at)
|
|
|
|
{
|
|
|
|
static char buf[20];
|
|
|
|
|
1996-07-23 01:18:47 +00:00
|
|
|
(void) snprintf(buf, sizeof(buf), "%u.%u", ntohs(at.s_net), at.s_node);
|
1996-07-09 19:02:28 +00:00
|
|
|
return(buf);
|
|
|
|
}
|