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
|
1994-05-26 06:35:07 +00:00
|
|
|
#endif /* not lint */
|
|
|
|
|
2009-08-25 04:09:09 +00:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
1994-05-26 06:35:07 +00:00
|
|
|
#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>
|
2012-11-17 19:54:23 +00:00
|
|
|
#include <sys/queue.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
|
|
|
|
2013-07-17 14:05:20 +00:00
|
|
|
static struct keytab {
|
2010-02-27 10:18:33 +00:00
|
|
|
const char *kt_cp;
|
1994-05-26 06:35:07 +00:00
|
|
|
int kt_i;
|
|
|
|
} keywords[] = {
|
|
|
|
#include "keywords.h"
|
|
|
|
{0, 0}
|
|
|
|
};
|
|
|
|
|
2013-07-17 14:05:20 +00:00
|
|
|
static struct sockaddr_storage so[RTAX_MAX];
|
|
|
|
static int pid, rtm_addrs;
|
|
|
|
static int s;
|
|
|
|
static int forcehost, forcenet, nflag, af, qflag, tflag;
|
|
|
|
static int verbose, aflen;
|
|
|
|
static int locking, lockrest, debugonly;
|
|
|
|
static struct rt_metrics rt_metrics;
|
|
|
|
static u_long rtm_inits;
|
|
|
|
static uid_t uid;
|
2012-11-17 19:54:23 +00:00
|
|
|
static int defaultfib;
|
|
|
|
static int numfibs;
|
2009-08-25 04:09:09 +00:00
|
|
|
|
2010-02-27 10:18:33 +00:00
|
|
|
static int atalk_aton(const char *, struct at_addr *);
|
|
|
|
static char *atalk_ntoa(struct at_addr);
|
2013-07-17 14:05:20 +00:00
|
|
|
static void printb(int, const char *);
|
2010-02-27 10:18:33 +00:00
|
|
|
static void flushroutes(int argc, char *argv[]);
|
2012-11-17 19:54:23 +00:00
|
|
|
static int flushroutes_fib(int);
|
2013-01-08 17:24:43 +00:00
|
|
|
static int getaddr(int, char *, struct hostent **, int);
|
2010-02-27 10:18:33 +00:00
|
|
|
static int keyword(const char *);
|
2013-07-17 14:05:20 +00:00
|
|
|
#ifdef INET
|
|
|
|
static void inet_makenetandmask(u_long, struct sockaddr_in *,
|
|
|
|
struct sockaddr_in *, u_long);
|
|
|
|
#endif
|
2002-05-21 11:43:53 +00:00
|
|
|
#ifdef INET6
|
2013-07-17 14:05:20 +00:00
|
|
|
static int inet6_makenetandmask(struct sockaddr_in6 *, const char *);
|
2002-05-21 11:43:53 +00:00
|
|
|
#endif
|
2010-02-27 10:18:33 +00:00
|
|
|
static void interfaces(void);
|
2012-11-17 19:54:23 +00:00
|
|
|
static void monitor(int, char*[]);
|
2010-02-27 10:18:33 +00:00
|
|
|
static const char *netname(struct sockaddr *);
|
|
|
|
static void newroute(int, char **);
|
2012-11-17 19:54:23 +00:00
|
|
|
static int newroute_fib(int, char *, int);
|
2010-12-08 15:12:37 +00:00
|
|
|
static void pmsg_addrs(char *, int, size_t);
|
|
|
|
static void pmsg_common(struct rt_msghdr *, size_t);
|
2010-02-27 10:18:33 +00:00
|
|
|
static int prefixlen(const char *);
|
2012-11-17 19:54:23 +00:00
|
|
|
static void print_getmsg(struct rt_msghdr *, int, int);
|
2010-12-08 15:12:37 +00:00
|
|
|
static void print_rtmsg(struct rt_msghdr *, size_t);
|
2010-02-27 10:18:33 +00:00
|
|
|
static const char *routename(struct sockaddr *);
|
2012-11-17 19:54:23 +00:00
|
|
|
static int rtmsg(int, int, int);
|
2010-02-27 10:18:33 +00:00
|
|
|
static void set_metric(char *, int);
|
2012-11-17 19:54:23 +00:00
|
|
|
static int set_sofib(int);
|
2013-07-17 14:05:20 +00:00
|
|
|
static void sockaddr(char *, struct sockaddr *, size_t);
|
|
|
|
static void sodump(struct sockaddr *, const char *);
|
2010-02-27 10:18:33 +00:00
|
|
|
|
2012-11-17 19:54:23 +00:00
|
|
|
struct fibl {
|
|
|
|
TAILQ_ENTRY(fibl) fl_next;
|
|
|
|
|
|
|
|
int fl_num;
|
|
|
|
int fl_error;
|
|
|
|
int fl_errno;
|
|
|
|
};
|
2013-07-17 14:05:20 +00:00
|
|
|
static TAILQ_HEAD(fibl_head_t, fibl) fibl_head;
|
2012-11-17 19:54:23 +00:00
|
|
|
|
|
|
|
static int fiboptlist_csv(const char *, struct fibl_head_t *);
|
|
|
|
static int fiboptlist_range(const char *, struct fibl_head_t *);
|
|
|
|
|
2010-02-27 10:18:33 +00:00
|
|
|
static void usage(const char *) __dead2;
|
1994-05-26 06:35:07 +00:00
|
|
|
|
2013-07-21 14:27:07 +00:00
|
|
|
static void
|
2009-08-25 04:09:09 +00:00
|
|
|
usage(const char *cp)
|
1994-05-26 06:35:07 +00:00
|
|
|
{
|
2010-02-27 10:18:33 +00:00
|
|
|
if (cp != NULL)
|
1996-01-02 20:09:22 +00:00
|
|
|
warnx("bad keyword: %s", cp);
|
2013-07-17 14:05:20 +00:00
|
|
|
errx(EX_USAGE, "usage: route [-dnqtv] command [[modifiers] args]");
|
1994-05-26 06:35:07 +00:00
|
|
|
/* NOTREACHED */
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2009-08-25 04:09:09 +00:00
|
|
|
main(int argc, char **argv)
|
1994-05-26 06:35:07 +00:00
|
|
|
{
|
|
|
|
int ch;
|
2012-11-17 19:54:23 +00:00
|
|
|
size_t len;
|
1994-05-26 06:35:07 +00:00
|
|
|
|
|
|
|
if (argc < 2)
|
2010-02-27 10:18:33 +00:00
|
|
|
usage(NULL);
|
1994-05-26 06:35:07 +00:00
|
|
|
|
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:
|
2010-02-27 10:18:33 +00:00
|
|
|
usage(NULL);
|
1994-05-26 06:35:07 +00:00
|
|
|
}
|
|
|
|
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");
|
2012-11-17 19:54:23 +00:00
|
|
|
|
|
|
|
len = sizeof(numfibs);
|
|
|
|
if (sysctlbyname("net.fibs", (void *)&numfibs, &len, NULL, 0) == -1)
|
|
|
|
numfibs = -1;
|
|
|
|
|
|
|
|
len = sizeof(defaultfib);
|
|
|
|
if (numfibs != -1 &&
|
|
|
|
sysctlbyname("net.my_fibnum", (void *)&defaultfib, &len, NULL,
|
|
|
|
0) == -1)
|
|
|
|
defaultfib = -1;
|
|
|
|
|
2010-02-27 10:18:33 +00:00
|
|
|
if (*argv != NULL)
|
1994-05-26 06:35:07 +00:00
|
|
|
switch (keyword(*argv)) {
|
|
|
|
case K_GET:
|
2009-04-14 23:05:36 +00:00
|
|
|
case K_SHOW:
|
1994-05-26 06:35:07 +00:00
|
|
|
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:
|
2012-11-17 19:54:23 +00:00
|
|
|
monitor(argc, argv);
|
1994-05-26 06:35:07 +00:00
|
|
|
/* NOTREACHED */
|
|
|
|
|
|
|
|
case K_FLUSH:
|
|
|
|
flushroutes(argc, argv);
|
|
|
|
exit(0);
|
|
|
|
/* NOTREACHED */
|
|
|
|
}
|
|
|
|
usage(*argv);
|
|
|
|
/* NOTREACHED */
|
|
|
|
}
|
|
|
|
|
2012-11-17 19:54:23 +00:00
|
|
|
static int
|
|
|
|
set_sofib(int fib)
|
|
|
|
{
|
|
|
|
|
|
|
|
if (fib < 0)
|
|
|
|
return (0);
|
|
|
|
return (setsockopt(s, SOL_SOCKET, SO_SETFIB, (void *)&fib,
|
|
|
|
sizeof(fib)));
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
fiboptlist_range(const char *arg, struct fibl_head_t *flh)
|
|
|
|
{
|
|
|
|
struct fibl *fl;
|
2012-12-16 23:46:59 +00:00
|
|
|
char *str0, *str, *token, *endptr;
|
2012-11-17 19:54:23 +00:00
|
|
|
int fib[2], i, error;
|
|
|
|
|
2012-12-16 23:46:59 +00:00
|
|
|
str0 = str = strdup(arg);
|
2012-11-17 19:54:23 +00:00
|
|
|
error = 0;
|
|
|
|
i = 0;
|
|
|
|
while ((token = strsep(&str, "-")) != NULL) {
|
|
|
|
switch (i) {
|
|
|
|
case 0:
|
|
|
|
case 1:
|
2012-12-16 23:46:59 +00:00
|
|
|
errno = 0;
|
2012-11-17 19:54:23 +00:00
|
|
|
fib[i] = strtol(token, &endptr, 0);
|
2012-12-16 23:46:59 +00:00
|
|
|
if (errno == 0) {
|
|
|
|
if (*endptr != '\0' ||
|
|
|
|
fib[i] < 0 ||
|
|
|
|
(numfibs != -1 && fib[i] > numfibs - 1))
|
|
|
|
errno = EINVAL;
|
|
|
|
}
|
|
|
|
if (errno)
|
2012-11-17 19:54:23 +00:00
|
|
|
error = 1;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
error = 1;
|
|
|
|
}
|
|
|
|
if (error)
|
|
|
|
goto fiboptlist_range_ret;
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
if (fib[0] >= fib[1]) {
|
|
|
|
error = 1;
|
|
|
|
goto fiboptlist_range_ret;
|
|
|
|
}
|
|
|
|
for (i = fib[0]; i <= fib[1]; i++) {
|
|
|
|
fl = calloc(1, sizeof(*fl));
|
|
|
|
if (fl == NULL) {
|
|
|
|
error = 1;
|
|
|
|
goto fiboptlist_range_ret;
|
|
|
|
}
|
|
|
|
fl->fl_num = i;
|
|
|
|
TAILQ_INSERT_TAIL(flh, fl, fl_next);
|
|
|
|
}
|
|
|
|
fiboptlist_range_ret:
|
2012-12-16 23:46:59 +00:00
|
|
|
free(str0);
|
2012-11-17 19:54:23 +00:00
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
|
|
|
#define ALLSTRLEN 64
|
|
|
|
static int
|
|
|
|
fiboptlist_csv(const char *arg, struct fibl_head_t *flh)
|
|
|
|
{
|
|
|
|
struct fibl *fl;
|
2012-12-16 23:46:59 +00:00
|
|
|
char *str0, *str, *token, *endptr;
|
2012-11-17 19:54:23 +00:00
|
|
|
int fib, error;
|
|
|
|
|
2013-07-17 14:05:20 +00:00
|
|
|
str0 = str = NULL;
|
2012-11-17 19:54:23 +00:00
|
|
|
if (strcmp("all", arg) == 0) {
|
|
|
|
str = calloc(1, ALLSTRLEN);
|
|
|
|
if (str == NULL) {
|
|
|
|
error = 1;
|
|
|
|
goto fiboptlist_csv_ret;
|
|
|
|
}
|
|
|
|
if (numfibs > 1)
|
|
|
|
snprintf(str, ALLSTRLEN - 1, "%d-%d", 0, numfibs - 1);
|
|
|
|
else
|
|
|
|
snprintf(str, ALLSTRLEN - 1, "%d", 0);
|
|
|
|
} else if (strcmp("default", arg) == 0) {
|
2012-12-16 23:46:59 +00:00
|
|
|
str0 = str = calloc(1, ALLSTRLEN);
|
2012-11-17 19:54:23 +00:00
|
|
|
if (str == NULL) {
|
|
|
|
error = 1;
|
|
|
|
goto fiboptlist_csv_ret;
|
|
|
|
}
|
|
|
|
snprintf(str, ALLSTRLEN - 1, "%d", defaultfib);
|
|
|
|
} else
|
2012-12-16 23:46:59 +00:00
|
|
|
str0 = str = strdup(arg);
|
2012-11-17 19:54:23 +00:00
|
|
|
|
|
|
|
error = 0;
|
|
|
|
while ((token = strsep(&str, ",")) != NULL) {
|
|
|
|
if (*token != '-' && strchr(token, '-') != NULL) {
|
|
|
|
error = fiboptlist_range(token, flh);
|
|
|
|
if (error)
|
|
|
|
goto fiboptlist_csv_ret;
|
|
|
|
} else {
|
2012-12-16 23:46:59 +00:00
|
|
|
errno = 0;
|
2012-11-17 19:54:23 +00:00
|
|
|
fib = strtol(token, &endptr, 0);
|
2012-12-16 23:46:59 +00:00
|
|
|
if (errno == 0) {
|
|
|
|
if (*endptr != '\0' ||
|
|
|
|
fib < 0 ||
|
|
|
|
(numfibs != -1 && fib > numfibs - 1))
|
|
|
|
errno = EINVAL;
|
|
|
|
}
|
|
|
|
if (errno) {
|
2012-11-17 19:54:23 +00:00
|
|
|
error = 1;
|
|
|
|
goto fiboptlist_csv_ret;
|
|
|
|
}
|
|
|
|
fl = calloc(1, sizeof(*fl));
|
|
|
|
if (fl == NULL) {
|
|
|
|
error = 1;
|
|
|
|
goto fiboptlist_csv_ret;
|
|
|
|
}
|
|
|
|
fl->fl_num = fib;
|
|
|
|
TAILQ_INSERT_TAIL(flh, fl, fl_next);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fiboptlist_csv_ret:
|
2013-07-17 14:05:20 +00:00
|
|
|
if (str0 != NULL)
|
|
|
|
free(str0);
|
2012-11-17 19:54:23 +00:00
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
1994-05-26 06:35:07 +00:00
|
|
|
/*
|
|
|
|
* Purge all entries in the routing tables not
|
|
|
|
* associated with network interfaces.
|
|
|
|
*/
|
2010-02-27 10:18:33 +00:00
|
|
|
static void
|
2009-08-25 04:09:09 +00:00
|
|
|
flushroutes(int argc, char *argv[])
|
1994-05-26 06:35:07 +00:00
|
|
|
{
|
2012-11-17 19:54:23 +00:00
|
|
|
struct fibl *fl;
|
|
|
|
int error;
|
1994-05-26 06:35:07 +00:00
|
|
|
|
2013-07-17 14:05:20 +00:00
|
|
|
if (uid != 0 && !debugonly && !tflag)
|
1996-01-02 20:09:22 +00:00
|
|
|
errx(EX_NOPERM, "must be root to alter routing table");
|
2005-05-11 02:50:41 +00:00
|
|
|
shutdown(s, SHUT_RD); /* Don't want to read back our messages */
|
2012-11-17 19:54:23 +00:00
|
|
|
|
|
|
|
TAILQ_INIT(&fibl_head);
|
|
|
|
while (argc > 1) {
|
|
|
|
argc--;
|
1994-05-26 06:35:07 +00:00
|
|
|
argv++;
|
2012-11-17 19:54:23 +00:00
|
|
|
if (**argv != '-')
|
|
|
|
usage(*argv);
|
|
|
|
switch (keyword(*argv + 1)) {
|
2013-07-17 14:05:20 +00:00
|
|
|
#ifdef INET
|
2012-11-17 19:54:23 +00:00
|
|
|
case K_INET:
|
|
|
|
af = AF_INET;
|
|
|
|
break;
|
2013-07-17 14:05:20 +00:00
|
|
|
#endif
|
1999-12-07 17:39:16 +00:00
|
|
|
#ifdef INET6
|
2012-11-17 19:54:23 +00:00
|
|
|
case K_INET6:
|
|
|
|
af = AF_INET6;
|
|
|
|
break;
|
1999-12-07 17:39:16 +00:00
|
|
|
#endif
|
2012-11-17 19:54:23 +00:00
|
|
|
case K_ATALK:
|
|
|
|
af = AF_APPLETALK;
|
|
|
|
break;
|
|
|
|
case K_LINK:
|
|
|
|
af = AF_LINK;
|
|
|
|
break;
|
|
|
|
case K_FIB:
|
|
|
|
if (!--argc)
|
|
|
|
usage(*argv);
|
|
|
|
error = fiboptlist_csv(*++argv, &fibl_head);
|
|
|
|
if (error)
|
2012-12-16 23:46:59 +00:00
|
|
|
errx(EX_USAGE, "invalid fib number: %s", *argv);
|
2012-11-17 19:54:23 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
usage(*argv);
|
|
|
|
}
|
1994-05-26 06:35:07 +00:00
|
|
|
}
|
2012-11-17 19:54:23 +00:00
|
|
|
if (TAILQ_EMPTY(&fibl_head)) {
|
|
|
|
error = fiboptlist_csv("default", &fibl_head);
|
|
|
|
if (error)
|
|
|
|
errx(EX_OSERR, "fiboptlist_csv failed.");
|
|
|
|
}
|
|
|
|
TAILQ_FOREACH(fl, &fibl_head, fl_next)
|
|
|
|
flushroutes_fib(fl->fl_num);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
flushroutes_fib(int fib)
|
|
|
|
{
|
|
|
|
struct rt_msghdr *rtm;
|
|
|
|
size_t needed;
|
|
|
|
char *buf, *next, *lim;
|
2013-07-17 14:15:00 +00:00
|
|
|
int mib[7], rlen, seqno, count = 0;
|
2012-11-17 19:54:23 +00:00
|
|
|
int error;
|
|
|
|
|
|
|
|
error = set_sofib(fib);
|
|
|
|
if (error) {
|
|
|
|
warn("fib number %d is ignored", fib);
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
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 */
|
2013-07-17 14:05:20 +00:00
|
|
|
mib[3] = AF_UNSPEC;
|
1994-05-26 06:35:07 +00:00
|
|
|
mib[4] = NET_RT_DUMP;
|
|
|
|
mib[5] = 0; /* no flags */
|
2013-07-17 14:15:00 +00:00
|
|
|
mib[6] = fib;
|
2013-07-17 14:05:20 +00:00
|
|
|
if (sysctl(mib, nitems(mib), 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");
|
2013-07-17 14:05:20 +00:00
|
|
|
if (sysctl(mib, nitems(mib), buf, &needed, NULL, 0) < 0) {
|
2004-04-30 22:34:12 +00:00
|
|
|
if (errno == ENOMEM && count++ < 10) {
|
2010-02-27 10:18:33 +00:00
|
|
|
warnx("Routing table grew, retrying");
|
2004-04-30 22:34:12 +00:00
|
|
|
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)
|
2013-07-17 14:05:20 +00:00
|
|
|
(void)printf("Examining routing table from sysctl\n");
|
1994-05-26 06:35:07 +00:00
|
|
|
seqno = 0; /* ??? */
|
|
|
|
for (next = buf; next < lim; next += rtm->rtm_msglen) {
|
2013-07-20 15:23:42 +00:00
|
|
|
rtm = (struct rt_msghdr *)(void *)next;
|
1994-05-26 06:35:07 +00:00
|
|
|
if (verbose)
|
|
|
|
print_rtmsg(rtm, rtm->rtm_msglen);
|
|
|
|
if ((rtm->rtm_flags & RTF_GATEWAY) == 0)
|
|
|
|
continue;
|
2010-02-27 10:18:33 +00:00
|
|
|
if (af != 0) {
|
1994-05-26 06:35:07 +00:00
|
|
|
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");
|
2013-07-17 14:05:20 +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);
|
2012-11-17 19:54:23 +00:00
|
|
|
|
|
|
|
printf("%-20.20s ", rtm->rtm_flags & RTF_HOST ?
|
1994-05-26 06:35:07 +00:00
|
|
|
routename(sa) : netname(sa));
|
2004-04-13 11:24:43 +00:00
|
|
|
sa = (struct sockaddr *)(SA_SIZE(sa) + (char *)sa);
|
2012-11-17 19:54:23 +00:00
|
|
|
printf("%-20.20s ", routename(sa));
|
|
|
|
if (fib >= 0)
|
|
|
|
printf("-fib %-3d ", fib);
|
|
|
|
printf("done\n");
|
1994-05-26 06:35:07 +00:00
|
|
|
}
|
|
|
|
}
|
2012-11-17 19:54:23 +00:00
|
|
|
return (error);
|
1994-05-26 06:35:07 +00:00
|
|
|
}
|
|
|
|
|
2013-07-21 14:27:07 +00:00
|
|
|
static const char *
|
2009-08-25 04:09:09 +00:00
|
|
|
routename(struct sockaddr *sa)
|
1994-05-26 06:35:07 +00:00
|
|
|
{
|
2013-07-21 11:59:41 +00:00
|
|
|
struct sockaddr_dl *sdl;
|
2010-02-27 10:18:33 +00:00
|
|
|
const char *cp;
|
2013-07-21 14:27:07 +00:00
|
|
|
static char line[NI_MAXHOST];
|
1994-05-26 06:35:07 +00:00
|
|
|
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';
|
2013-07-17 14:05:20 +00:00
|
|
|
(void)strcpy(domain, cp + 1);
|
1996-10-27 17:42:14 +00:00
|
|
|
} else
|
2013-07-17 14:05:20 +00:00
|
|
|
domain[0] = '\0';
|
1994-05-26 06:35:07 +00:00
|
|
|
}
|
|
|
|
|
2013-07-21 14:27:07 +00:00
|
|
|
/* If the address is zero-filled, use "default". */
|
2013-07-20 15:58:43 +00:00
|
|
|
if (sa->sa_len == 0 && nflag == 0)
|
|
|
|
return ("default");
|
2013-07-21 14:27:07 +00:00
|
|
|
#if defined(INET) || defined(INET6)
|
2013-07-17 14:05:20 +00:00
|
|
|
switch (sa->sa_family) {
|
|
|
|
#ifdef INET
|
1994-05-26 06:35:07 +00:00
|
|
|
case AF_INET:
|
2013-07-21 14:27:07 +00:00
|
|
|
/* If the address is zero-filled, use "default". */
|
|
|
|
if (nflag == 0 &&
|
|
|
|
((struct sockaddr_in *)(void *)sa)->sin_addr.s_addr ==
|
|
|
|
INADDR_ANY)
|
|
|
|
return("default");
|
1994-05-26 06:35:07 +00:00
|
|
|
break;
|
2013-07-21 14:27:07 +00:00
|
|
|
#endif
|
|
|
|
#ifdef INET6
|
|
|
|
case AF_INET6:
|
|
|
|
/* If the address is zero-filled, use "default". */
|
|
|
|
if (nflag == 0 &&
|
|
|
|
IN6_IS_ADDR_UNSPECIFIED(&((struct sockaddr_in6 *)(void *)sa)->sin6_addr))
|
|
|
|
return("default");
|
|
|
|
break;
|
|
|
|
#endif
|
2013-07-17 14:05:20 +00:00
|
|
|
}
|
2013-07-21 14:27:07 +00:00
|
|
|
#endif
|
1994-05-26 06:35:07 +00:00
|
|
|
|
2013-07-21 14:27:07 +00:00
|
|
|
switch (sa->sa_family) {
|
|
|
|
#if defined(INET) || defined(INET6)
|
|
|
|
#ifdef INET
|
|
|
|
case AF_INET:
|
2013-07-17 14:05:20 +00:00
|
|
|
#endif
|
1999-12-07 17:39:16 +00:00
|
|
|
#ifdef INET6
|
|
|
|
case AF_INET6:
|
2013-07-21 14:27:07 +00:00
|
|
|
#endif
|
2001-06-11 12:39:29 +00:00
|
|
|
{
|
2013-07-21 14:27:07 +00:00
|
|
|
struct sockaddr_storage ss;
|
|
|
|
int error;
|
|
|
|
char *p;
|
2001-06-11 12:39:29 +00:00
|
|
|
|
2013-07-21 14:27:07 +00:00
|
|
|
memset(&ss, 0, sizeof(ss));
|
|
|
|
if (sa->sa_len == 0)
|
|
|
|
ss.ss_family = sa->sa_family;
|
|
|
|
else
|
|
|
|
memcpy(&ss, sa, sa->sa_len);
|
|
|
|
/* Expand sa->sa_len because it could be shortened. */
|
|
|
|
if (sa->sa_family == AF_INET)
|
|
|
|
ss.ss_len = sizeof(struct sockaddr_in);
|
|
|
|
else if (sa->sa_family == AF_INET6)
|
|
|
|
ss.ss_len = sizeof(struct sockaddr_in6);
|
|
|
|
error = getnameinfo((struct sockaddr *)&ss, ss.ss_len,
|
|
|
|
line, sizeof(line), NULL, 0,
|
|
|
|
(nflag == 0) ? 0 : NI_NUMERICHOST);
|
|
|
|
if (error) {
|
|
|
|
warnx("getnameinfo(): %s", gai_strerror(error));
|
2001-06-11 12:39:29 +00:00
|
|
|
strncpy(line, "invalid", sizeof(line));
|
2013-07-21 14:27:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Remove the domain part if any. */
|
|
|
|
p = strchr(line, '.');
|
|
|
|
if (p != NULL && strcmp(p + 1, domain) == 0)
|
|
|
|
*p = '\0';
|
2001-06-11 12:39:29 +00:00
|
|
|
|
2013-07-17 14:05:20 +00:00
|
|
|
return (line);
|
2013-07-21 14:27:07 +00:00
|
|
|
break;
|
2001-06-11 12:39:29 +00:00
|
|
|
}
|
1999-12-07 17:39:16 +00:00
|
|
|
#endif
|
1996-07-09 19:02:28 +00:00
|
|
|
case AF_APPLETALK:
|
2013-07-17 14:05:20 +00:00
|
|
|
(void)snprintf(line, sizeof(line), "atalk %s",
|
2013-07-20 15:23:42 +00:00
|
|
|
atalk_ntoa(((struct sockaddr_at *)(void *)sa)->sat_addr));
|
1996-07-09 19:02:28 +00:00
|
|
|
break;
|
|
|
|
|
1994-05-26 06:35:07 +00:00
|
|
|
case AF_LINK:
|
2013-07-21 11:59:41 +00:00
|
|
|
sdl = (struct sockaddr_dl *)(void *)sa;
|
|
|
|
|
|
|
|
if (sdl->sdl_nlen == 0 &&
|
|
|
|
sdl->sdl_alen == 0 &&
|
|
|
|
sdl->sdl_slen == 0) {
|
|
|
|
n = snprintf(line, sizeof(line), "link#%d",
|
|
|
|
sdl->sdl_index);
|
|
|
|
if (n > (int)sizeof(line))
|
|
|
|
line[0] = '\0';
|
|
|
|
return (line);
|
|
|
|
} else
|
|
|
|
return (link_ntoa(sdl));
|
2013-07-17 14:05:20 +00:00
|
|
|
break;
|
1994-05-26 06:35:07 +00:00
|
|
|
|
|
|
|
default:
|
2010-02-27 10:18:33 +00:00
|
|
|
{
|
2013-07-20 15:23:42 +00:00
|
|
|
u_short *sp = (u_short *)(void *)sa;
|
2010-02-27 10:18:33 +00:00
|
|
|
u_short *splim = sp + ((sa->sa_len + 1) >> 1);
|
|
|
|
char *cps = 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
|
|
|
|
2010-02-27 10:18:33 +00:00
|
|
|
while (++sp < splim && cps < cpe) /* start with sa->sa_data */
|
|
|
|
if ((n = snprintf(cps, cpe - cps, " %x", *sp)) > 0)
|
|
|
|
cps += n;
|
2001-08-20 14:53:05 +00:00
|
|
|
else
|
2010-02-27 10:18:33 +00:00
|
|
|
*cps = '\0';
|
1994-05-26 06:35:07 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return (line);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Return the name of the network whose address is given.
|
2012-11-14 08:05:21 +00:00
|
|
|
* The address is assumed to be that of a net, not a host.
|
1994-05-26 06:35:07 +00:00
|
|
|
*/
|
2013-07-21 14:27:07 +00:00
|
|
|
static const char *
|
2009-08-25 04:09:09 +00:00
|
|
|
netname(struct sockaddr *sa)
|
1994-05-26 06:35:07 +00:00
|
|
|
{
|
2013-07-21 11:59:41 +00:00
|
|
|
struct sockaddr_dl *sdl;
|
1996-10-27 17:42:14 +00:00
|
|
|
static char line[MAXHOSTNAMELEN + 1];
|
2013-07-17 14:05:20 +00:00
|
|
|
int n;
|
|
|
|
#ifdef INET
|
2010-02-27 10:18:33 +00:00
|
|
|
struct netent *np = NULL;
|
2013-07-17 14:05:20 +00:00
|
|
|
const char *cp = NULL;
|
2002-03-20 17:55:10 +00:00
|
|
|
u_long i;
|
2013-07-17 14:05:20 +00:00
|
|
|
#endif
|
1994-05-26 06:35:07 +00:00
|
|
|
|
|
|
|
switch (sa->sa_family) {
|
2013-07-17 14:05:20 +00:00
|
|
|
#ifdef INET
|
1994-05-26 06:35:07 +00:00
|
|
|
case AF_INET:
|
2013-07-17 14:05:20 +00:00
|
|
|
{
|
|
|
|
struct in_addr in;
|
1994-05-26 06:35:07 +00:00
|
|
|
|
2013-07-20 15:23:42 +00:00
|
|
|
in = ((struct sockaddr_in *)(void *)sa)->sin_addr;
|
1994-05-26 06:35:07 +00:00
|
|
|
i = in.s_addr = ntohl(in.s_addr);
|
|
|
|
if (in.s_addr == 0)
|
|
|
|
cp = "default";
|
|
|
|
else if (!nflag) {
|
2012-11-14 08:05:21 +00:00
|
|
|
np = getnetbyaddr(i, AF_INET);
|
2010-02-27 10:18:33 +00:00
|
|
|
if (np != NULL)
|
1994-05-26 06:35:07 +00:00
|
|
|
cp = np->n_name;
|
|
|
|
}
|
2001-06-07 13:50:24 +00:00
|
|
|
#define C(x) (unsigned)((x) & 0xff)
|
2010-02-27 10:18:33 +00:00
|
|
|
if (cp != NULL)
|
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)
|
2013-07-17 14:05:20 +00:00
|
|
|
(void)sprintf(line, "%u", C(in.s_addr >> 24));
|
1994-05-26 06:35:07 +00:00
|
|
|
else if ((in.s_addr & 0xffff) == 0)
|
2013-07-17 14:05:20 +00:00
|
|
|
(void)sprintf(line, "%u.%u", C(in.s_addr >> 24),
|
1994-05-26 06:35:07 +00:00
|
|
|
C(in.s_addr >> 16));
|
|
|
|
else if ((in.s_addr & 0xff) == 0)
|
2013-07-17 14:05:20 +00:00
|
|
|
(void)sprintf(line, "%u.%u.%u", C(in.s_addr >> 24),
|
1994-05-26 06:35:07 +00:00
|
|
|
C(in.s_addr >> 16), C(in.s_addr >> 8));
|
|
|
|
else
|
2013-07-17 14:05:20 +00:00
|
|
|
(void)sprintf(line, "%u.%u.%u.%u", C(in.s_addr >> 24),
|
1994-05-26 06:35:07 +00:00
|
|
|
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;
|
2013-07-17 14:05:20 +00:00
|
|
|
}
|
|
|
|
#endif
|
1999-12-07 17:39:16 +00:00
|
|
|
#ifdef INET6
|
|
|
|
case AF_INET6:
|
2001-06-11 12:39:29 +00:00
|
|
|
{
|
2013-07-17 14:05:20 +00:00
|
|
|
struct sockaddr_in6 sin6;
|
2001-06-11 12:39:29 +00:00
|
|
|
int niflags = 0;
|
|
|
|
|
|
|
|
memset(&sin6, 0, sizeof(sin6));
|
|
|
|
memcpy(&sin6, sa, sa->sa_len);
|
2013-07-17 14:05:20 +00:00
|
|
|
sin6.sin6_len = sizeof(sin6);
|
2001-06-11 12:39:29 +00:00
|
|
|
sin6.sin6_family = AF_INET6;
|
|
|
|
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:
|
2013-07-17 14:05:20 +00:00
|
|
|
(void)snprintf(line, sizeof(line), "atalk %s",
|
2013-07-20 15:23:42 +00:00
|
|
|
atalk_ntoa(((struct sockaddr_at *)(void *)sa)->sat_addr));
|
1996-07-09 19:02:28 +00:00
|
|
|
break;
|
|
|
|
|
1994-05-26 06:35:07 +00:00
|
|
|
case AF_LINK:
|
2013-07-21 11:59:41 +00:00
|
|
|
sdl = (struct sockaddr_dl *)(void *)sa;
|
|
|
|
|
|
|
|
if (sdl->sdl_nlen == 0 &&
|
|
|
|
sdl->sdl_alen == 0 &&
|
|
|
|
sdl->sdl_slen == 0) {
|
|
|
|
n = snprintf(line, sizeof(line), "link#%d",
|
|
|
|
sdl->sdl_index);
|
|
|
|
if (n > (int)sizeof(line))
|
|
|
|
line[0] = '\0';
|
|
|
|
return (line);
|
|
|
|
} else
|
|
|
|
return (link_ntoa(sdl));
|
2013-07-17 14:05:20 +00:00
|
|
|
break;
|
1994-05-26 06:35:07 +00:00
|
|
|
|
|
|
|
default:
|
2010-02-27 10:18:33 +00:00
|
|
|
{
|
2013-07-20 15:23:42 +00:00
|
|
|
u_short *sp = (u_short *)(void *)sa->sa_data;
|
2010-02-27 10:18:33 +00:00
|
|
|
u_short *splim = sp + ((sa->sa_len + 1)>>1);
|
|
|
|
char *cps = 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
|
|
|
|
2010-02-27 10:18:33 +00:00
|
|
|
while (sp < splim && cps < cpe)
|
|
|
|
if ((n = snprintf(cps, cpe - cps, " %x", *sp++)) > 0)
|
|
|
|
cps += n;
|
2001-08-20 14:53:05 +00:00
|
|
|
else
|
2010-02-27 10:18:33 +00:00
|
|
|
*cps = '\0';
|
1994-05-26 06:35:07 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return (line);
|
|
|
|
}
|
|
|
|
|
2010-02-27 10:18:33 +00:00
|
|
|
static void
|
2009-08-25 04:09:09 +00:00
|
|
|
set_metric(char *value, int key)
|
1994-05-26 06:35:07 +00:00
|
|
|
{
|
|
|
|
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);
|
2009-04-14 23:05:36 +00:00
|
|
|
caseof(K_WEIGHT, RTV_WEIGHT, rmx_weight);
|
1994-05-26 06:35:07 +00:00
|
|
|
}
|
|
|
|
rtm_inits |= flag;
|
|
|
|
if (lockrest || locking)
|
|
|
|
rt_metrics.rmx_locks |= flag;
|
|
|
|
if (locking)
|
|
|
|
locking = 0;
|
|
|
|
*valp = atoi(value);
|
|
|
|
}
|
|
|
|
|
2012-11-17 19:54:23 +00:00
|
|
|
#define F_ISHOST 0x01
|
|
|
|
#define F_FORCENET 0x02
|
|
|
|
#define F_FORCEHOST 0x04
|
|
|
|
#define F_PROXY 0x08
|
|
|
|
#define F_INTERFACE 0x10
|
|
|
|
|
2010-02-27 10:18:33 +00:00
|
|
|
static void
|
2009-08-25 04:09:09 +00:00
|
|
|
newroute(int argc, char **argv)
|
1994-05-26 06:35:07 +00:00
|
|
|
{
|
2012-11-17 19:54:23 +00:00
|
|
|
struct hostent *hp;
|
|
|
|
struct fibl *fl;
|
2010-02-27 10:18:33 +00:00
|
|
|
char *cmd;
|
2012-11-17 19:54:23 +00:00
|
|
|
const char *dest, *gateway, *errmsg;
|
|
|
|
int key, error, flags, nrflags, fibnum;
|
1994-05-26 06:35:07 +00:00
|
|
|
|
2013-07-17 14:05:20 +00:00
|
|
|
if (uid != 0 && !debugonly && !tflag)
|
1996-01-02 20:09:22 +00:00
|
|
|
errx(EX_NOPERM, "must be root to alter routing table");
|
2012-11-17 19:54:23 +00:00
|
|
|
dest = NULL;
|
|
|
|
gateway = NULL;
|
|
|
|
flags = RTF_STATIC;
|
|
|
|
nrflags = 0;
|
|
|
|
hp = NULL;
|
|
|
|
TAILQ_INIT(&fibl_head);
|
|
|
|
|
1994-05-26 06:35:07 +00:00
|
|
|
cmd = argv[0];
|
2009-04-14 23:05:36 +00:00
|
|
|
if (*cmd != 'g' && *cmd != 's')
|
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;
|
2013-07-17 14:05:20 +00:00
|
|
|
#ifdef INET
|
1994-05-26 06:35:07 +00:00
|
|
|
case K_INET:
|
|
|
|
af = AF_INET;
|
|
|
|
aflen = sizeof(struct sockaddr_in);
|
|
|
|
break;
|
2013-07-17 14:05:20 +00:00
|
|
|
#endif
|
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;
|
2013-07-17 14:05:20 +00:00
|
|
|
aflen = sizeof(struct sockaddr_storage);
|
1994-05-26 06:35:07 +00:00
|
|
|
break;
|
|
|
|
case K_IFACE:
|
|
|
|
case K_INTERFACE:
|
2012-11-17 19:54:23 +00:00
|
|
|
nrflags |= F_INTERFACE;
|
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:
|
2012-11-17 19:54:23 +00:00
|
|
|
nrflags |= F_FORCEHOST;
|
1994-05-26 06:35:07 +00:00
|
|
|
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:
|
2012-11-17 19:54:23 +00:00
|
|
|
nrflags |= F_PROXY;
|
2001-06-12 13:31:53 +00:00
|
|
|
break;
|
1994-05-26 06:35:07 +00:00
|
|
|
case K_XRESOLVE:
|
|
|
|
flags |= RTF_XRESOLVE;
|
|
|
|
break;
|
|
|
|
case K_STATIC:
|
|
|
|
flags |= RTF_STATIC;
|
|
|
|
break;
|
2009-04-14 23:05:36 +00:00
|
|
|
case K_STICKY:
|
|
|
|
flags |= RTF_STICKY;
|
|
|
|
break;
|
|
|
|
case K_NOSTICK:
|
|
|
|
flags &= ~RTF_STICKY;
|
|
|
|
break;
|
2012-11-17 19:54:23 +00:00
|
|
|
case K_FIB:
|
|
|
|
if (!--argc)
|
|
|
|
usage(NULL);
|
|
|
|
error = fiboptlist_csv(*++argv, &fibl_head);
|
|
|
|
if (error)
|
2012-12-16 23:46:59 +00:00
|
|
|
errx(EX_USAGE,
|
|
|
|
"invalid fib number: %s", *argv);
|
2012-11-17 19:54:23 +00:00
|
|
|
break;
|
1994-05-26 06:35:07 +00:00
|
|
|
case K_IFA:
|
1999-06-01 13:14:07 +00:00
|
|
|
if (!--argc)
|
2010-02-27 10:18:33 +00:00
|
|
|
usage(NULL);
|
2013-07-20 16:46:51 +00:00
|
|
|
getaddr(RTAX_IFA, *++argv, 0, nrflags);
|
1994-05-26 06:35:07 +00:00
|
|
|
break;
|
|
|
|
case K_IFP:
|
1999-06-01 13:14:07 +00:00
|
|
|
if (!--argc)
|
2010-02-27 10:18:33 +00:00
|
|
|
usage(NULL);
|
2013-07-20 16:46:51 +00:00
|
|
|
getaddr(RTAX_IFP, *++argv, 0, nrflags);
|
1994-05-26 06:35:07 +00:00
|
|
|
break;
|
|
|
|
case K_GENMASK:
|
1999-06-01 13:14:07 +00:00
|
|
|
if (!--argc)
|
2010-02-27 10:18:33 +00:00
|
|
|
usage(NULL);
|
2013-07-20 16:46:51 +00:00
|
|
|
getaddr(RTAX_GENMASK, *++argv, 0, nrflags);
|
1994-05-26 06:35:07 +00:00
|
|
|
break;
|
|
|
|
case K_GATEWAY:
|
1999-06-01 13:14:07 +00:00
|
|
|
if (!--argc)
|
2010-02-27 10:18:33 +00:00
|
|
|
usage(NULL);
|
2013-07-20 16:46:51 +00:00
|
|
|
getaddr(RTAX_GATEWAY, *++argv, 0, nrflags);
|
2013-06-09 17:40:06 +00:00
|
|
|
gateway = *argv;
|
1994-05-26 06:35:07 +00:00
|
|
|
break;
|
|
|
|
case K_DST:
|
1999-06-01 13:14:07 +00:00
|
|
|
if (!--argc)
|
2010-02-27 10:18:33 +00:00
|
|
|
usage(NULL);
|
2013-07-20 16:46:51 +00:00
|
|
|
if (getaddr(RTAX_DST, *++argv, &hp, nrflags))
|
2012-11-17 19:54:23 +00:00
|
|
|
nrflags |= F_ISHOST;
|
1994-05-26 06:35:07 +00:00
|
|
|
dest = *argv;
|
|
|
|
break;
|
|
|
|
case K_NETMASK:
|
1999-06-01 13:14:07 +00:00
|
|
|
if (!--argc)
|
2010-02-27 10:18:33 +00:00
|
|
|
usage(NULL);
|
2013-07-20 16:46:51 +00:00
|
|
|
getaddr(RTAX_NETMASK, *++argv, 0, nrflags);
|
1994-05-26 06:35:07 +00:00
|
|
|
/* FALLTHROUGH */
|
|
|
|
case K_NET:
|
2012-11-17 19:54:23 +00:00
|
|
|
nrflags |= F_FORCENET;
|
1994-05-26 06:35:07 +00:00
|
|
|
break;
|
1999-12-07 17:39:16 +00:00
|
|
|
case K_PREFIXLEN:
|
|
|
|
if (!--argc)
|
2010-02-27 10:18:33 +00:00
|
|
|
usage(NULL);
|
1999-12-07 17:39:16 +00:00
|
|
|
if (prefixlen(*++argv) == -1) {
|
2012-11-17 19:54:23 +00:00
|
|
|
nrflags &= ~F_FORCENET;
|
|
|
|
nrflags |= F_ISHOST;
|
1999-12-07 17:39:16 +00:00
|
|
|
} else {
|
2012-11-17 19:54:23 +00:00
|
|
|
nrflags |= F_FORCENET;
|
|
|
|
nrflags &= ~F_ISHOST;
|
1999-12-07 17:39:16 +00:00
|
|
|
}
|
|
|
|
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:
|
2009-04-14 23:05:36 +00:00
|
|
|
case K_WEIGHT:
|
1999-06-01 13:14:07 +00:00
|
|
|
if (!--argc)
|
2010-02-27 10:18:33 +00:00
|
|
|
usage(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;
|
2013-07-20 16:46:51 +00:00
|
|
|
if (getaddr(RTAX_DST, *argv, &hp, nrflags))
|
2012-11-17 19:54:23 +00:00
|
|
|
nrflags |= F_ISHOST;
|
1994-05-26 06:35:07 +00:00
|
|
|
} else if ((rtm_addrs & RTA_GATEWAY) == 0) {
|
|
|
|
gateway = *argv;
|
2013-07-20 16:46:51 +00:00
|
|
|
getaddr(RTAX_GATEWAY, *argv, &hp, nrflags);
|
1994-05-26 06:35:07 +00:00
|
|
|
} else {
|
2013-07-20 16:46:51 +00:00
|
|
|
getaddr(RTAX_NETMASK, *argv, 0, nrflags);
|
2012-11-17 19:54:23 +00:00
|
|
|
nrflags |= F_FORCENET;
|
1994-05-26 06:35:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-11-17 19:54:23 +00:00
|
|
|
|
2013-10-08 08:16:17 +00:00
|
|
|
if (so[RTAX_DST].ss_len == 0) {
|
|
|
|
warnx("destination parameter required");
|
|
|
|
usage(NULL);
|
|
|
|
}
|
|
|
|
|
2012-11-17 19:54:23 +00:00
|
|
|
if (nrflags & F_FORCEHOST) {
|
|
|
|
nrflags |= F_ISHOST;
|
1999-12-07 17:39:16 +00:00
|
|
|
#ifdef INET6
|
|
|
|
if (af == AF_INET6) {
|
|
|
|
rtm_addrs &= ~RTA_NETMASK;
|
2013-07-17 14:05:20 +00:00
|
|
|
memset(&so[RTAX_NETMASK], 0, sizeof(so[RTAX_NETMASK]));
|
1999-12-07 17:39:16 +00:00
|
|
|
}
|
2010-02-27 10:18:33 +00:00
|
|
|
#endif
|
1999-12-07 17:39:16 +00:00
|
|
|
}
|
2012-11-17 19:54:23 +00:00
|
|
|
if (nrflags & F_FORCENET)
|
|
|
|
nrflags &= ~F_ISHOST;
|
1994-05-26 06:35:07 +00:00
|
|
|
flags |= RTF_UP;
|
2012-11-17 19:54:23 +00:00
|
|
|
if (nrflags & F_ISHOST)
|
1994-05-26 06:35:07 +00:00
|
|
|
flags |= RTF_HOST;
|
2012-11-17 19:54:23 +00:00
|
|
|
if ((nrflags & F_INTERFACE) == 0)
|
1994-05-26 06:35:07 +00:00
|
|
|
flags |= RTF_GATEWAY;
|
2013-01-31 08:55:21 +00:00
|
|
|
if (nrflags & F_PROXY)
|
2001-06-12 13:31:53 +00:00
|
|
|
flags |= RTF_ANNOUNCE;
|
2012-11-17 19:54:23 +00:00
|
|
|
if (dest == NULL)
|
|
|
|
dest = "";
|
|
|
|
if (gateway == NULL)
|
|
|
|
gateway = "";
|
|
|
|
|
|
|
|
if (TAILQ_EMPTY(&fibl_head)) {
|
|
|
|
error = fiboptlist_csv("default", &fibl_head);
|
|
|
|
if (error)
|
|
|
|
errx(EX_OSERR, "fiboptlist_csv failed.");
|
|
|
|
}
|
|
|
|
error = 0;
|
|
|
|
TAILQ_FOREACH(fl, &fibl_head, fl_next) {
|
|
|
|
fl->fl_error = newroute_fib(fl->fl_num, cmd, flags);
|
|
|
|
if (fl->fl_error)
|
|
|
|
fl->fl_errno = errno;
|
|
|
|
error += fl->fl_error;
|
1994-05-26 06:35:07 +00:00
|
|
|
}
|
2009-04-14 23:05:36 +00:00
|
|
|
if (*cmd == 'g' || *cmd == 's')
|
2012-11-17 19:54:23 +00:00
|
|
|
exit(error);
|
|
|
|
|
|
|
|
error = 0;
|
2002-05-25 12:51:34 +00:00
|
|
|
if (!qflag) {
|
2012-11-17 19:54:23 +00:00
|
|
|
fibnum = 0;
|
|
|
|
TAILQ_FOREACH(fl, &fibl_head, fl_next) {
|
|
|
|
if (fl->fl_error == 0)
|
|
|
|
fibnum++;
|
2002-05-25 12:51:34 +00:00
|
|
|
}
|
2012-11-17 19:54:23 +00:00
|
|
|
if (fibnum > 0) {
|
|
|
|
int firstfib = 1;
|
|
|
|
|
|
|
|
printf("%s %s %s", cmd,
|
|
|
|
(nrflags & F_ISHOST) ? "host" : "net", dest);
|
|
|
|
if (*gateway)
|
|
|
|
printf(": gateway %s", gateway);
|
|
|
|
|
|
|
|
if (numfibs > 1) {
|
|
|
|
TAILQ_FOREACH(fl, &fibl_head, fl_next) {
|
|
|
|
if (fl->fl_error == 0
|
|
|
|
&& fl->fl_num >= 0) {
|
|
|
|
if (firstfib) {
|
|
|
|
printf(" fib ");
|
|
|
|
firstfib = 0;
|
|
|
|
}
|
|
|
|
printf("%d", fl->fl_num);
|
|
|
|
if (fibnum-- > 1)
|
|
|
|
printf(",");
|
|
|
|
}
|
|
|
|
}
|
2002-05-25 12:51:34 +00:00
|
|
|
}
|
2012-11-17 19:54:23 +00:00
|
|
|
printf("\n");
|
1994-05-26 06:35:07 +00:00
|
|
|
}
|
2012-11-17 19:54:23 +00:00
|
|
|
|
|
|
|
fibnum = 0;
|
|
|
|
TAILQ_FOREACH(fl, &fibl_head, fl_next) {
|
|
|
|
if (fl->fl_error != 0) {
|
|
|
|
printf("%s %s %s", cmd, (nrflags & F_ISHOST)
|
|
|
|
? "host" : "net", dest);
|
|
|
|
if (*gateway)
|
|
|
|
printf(": gateway %s", gateway);
|
|
|
|
|
|
|
|
if (fl->fl_num >= 0)
|
|
|
|
printf(" fib %d", fl->fl_num);
|
|
|
|
|
|
|
|
switch (fl->fl_errno) {
|
|
|
|
case ESRCH:
|
|
|
|
errmsg = "not in table";
|
|
|
|
break;
|
|
|
|
case EBUSY:
|
|
|
|
errmsg = "entry in use";
|
|
|
|
break;
|
|
|
|
case ENOBUFS:
|
|
|
|
errmsg = "not enough memory";
|
|
|
|
break;
|
|
|
|
case EADDRINUSE:
|
|
|
|
/*
|
|
|
|
* handle recursion avoidance
|
|
|
|
* in rt_setgate()
|
|
|
|
*/
|
|
|
|
errmsg = "gateway uses the same route";
|
|
|
|
break;
|
|
|
|
case EEXIST:
|
|
|
|
errmsg = "route already in table";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
errmsg = strerror(fl->fl_errno);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
printf(": %s\n", errmsg);
|
|
|
|
error = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
exit(error);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
newroute_fib(int fib, char *cmd, int flags)
|
|
|
|
{
|
|
|
|
int error;
|
|
|
|
|
|
|
|
error = set_sofib(fib);
|
|
|
|
if (error) {
|
|
|
|
warn("fib number %d is ignored", fib);
|
|
|
|
return (error);
|
1994-05-26 06:35:07 +00:00
|
|
|
}
|
2012-11-17 19:54:23 +00:00
|
|
|
|
|
|
|
error = rtmsg(*cmd, flags, fib);
|
|
|
|
return (error);
|
1994-05-26 06:35:07 +00:00
|
|
|
}
|
|
|
|
|
2013-07-17 14:05:20 +00:00
|
|
|
#ifdef INET
|
2010-02-27 10:18:33 +00:00
|
|
|
static void
|
2013-07-17 14:05:20 +00:00
|
|
|
inet_makenetandmask(u_long net, struct sockaddr_in *sin,
|
|
|
|
struct sockaddr_in *sin_mask, u_long bits)
|
1994-05-26 06:35:07 +00:00
|
|
|
{
|
2012-11-14 08:05:21 +00:00
|
|
|
u_long mask = 0;
|
1994-05-26 06:35:07 +00:00
|
|
|
|
|
|
|
rtm_addrs |= RTA_NETMASK;
|
2012-11-14 08:05:21 +00:00
|
|
|
|
2012-12-04 20:05:01 +00:00
|
|
|
/*
|
|
|
|
* MSB of net should be meaningful. 0/0 is exception.
|
|
|
|
*/
|
|
|
|
if (net > 0)
|
|
|
|
while ((net & 0xff000000) == 0)
|
|
|
|
net <<= 8;
|
|
|
|
|
2009-04-06 10:09:20 +00:00
|
|
|
/*
|
2010-02-27 10:18:33 +00:00
|
|
|
* If no /xx was specified we must calculate the
|
2009-04-06 10:09:20 +00:00
|
|
|
* CIDR address.
|
|
|
|
*/
|
2012-11-14 08:05:21 +00:00
|
|
|
if ((bits == 0) && (net != 0)) {
|
2009-04-06 14:27:28 +00:00
|
|
|
u_long i, j;
|
2013-07-17 14:05:20 +00:00
|
|
|
|
|
|
|
for(i = 0, j = 0xff; i < 4; i++) {
|
2012-11-14 08:05:21 +00:00
|
|
|
if (net & j) {
|
2009-04-06 10:09:20 +00:00
|
|
|
break;
|
|
|
|
}
|
2009-04-06 14:27:28 +00:00
|
|
|
j <<= 8;
|
2009-04-06 10:09:20 +00:00
|
|
|
}
|
|
|
|
/* i holds the first non zero bit */
|
2009-04-06 14:27:28 +00:00
|
|
|
bits = 32 - (i*8);
|
2009-04-06 10:09:20 +00:00
|
|
|
}
|
2009-04-11 10:08:26 +00:00
|
|
|
if (bits != 0)
|
|
|
|
mask = 0xffffffff << (32 - bits);
|
2007-10-29 00:08:24 +00:00
|
|
|
|
2012-11-14 08:05:21 +00:00
|
|
|
sin->sin_addr.s_addr = htonl(net);
|
2013-07-17 14:05:20 +00:00
|
|
|
sin_mask->sin_addr.s_addr = htonl(mask);
|
|
|
|
sin_mask->sin_len = sizeof(struct sockaddr_in);
|
|
|
|
sin_mask->sin_family = AF_INET;
|
1994-05-26 06:35:07 +00:00
|
|
|
}
|
2013-07-17 14:05:20 +00:00
|
|
|
#endif
|
1994-05-26 06:35:07 +00:00
|
|
|
|
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
|
2010-02-27 10:18:33 +00:00
|
|
|
inet6_makenetandmask(struct sockaddr_in6 *sin6, const char *plen)
|
2002-05-21 18:11:31 +00:00
|
|
|
{
|
2002-05-20 15:01:19 +00:00
|
|
|
struct in6_addr in6;
|
|
|
|
|
2010-02-27 10:18:33 +00:00
|
|
|
if (plen == NULL) {
|
2002-05-21 18:11:31 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2010-02-27 10:18:33 +00:00
|
|
|
if (plen == NULL || strcmp(plen, "128") == 0)
|
|
|
|
return (1);
|
2002-06-08 19:49:22 +00:00
|
|
|
rtm_addrs |= RTA_NETMASK;
|
2010-02-27 10:18:33 +00:00
|
|
|
prefixlen(plen);
|
|
|
|
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.
|
|
|
|
*/
|
2010-02-27 10:18:33 +00:00
|
|
|
static int
|
2013-07-20 16:46:51 +00:00
|
|
|
getaddr(int idx, char *str, struct hostent **hpp, int nrflags)
|
1994-05-26 06:35:07 +00:00
|
|
|
{
|
2013-07-17 14:05:20 +00:00
|
|
|
struct sockaddr *sa;
|
|
|
|
#if defined(INET)
|
|
|
|
struct sockaddr_in *sin;
|
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;
|
2013-07-17 14:05:20 +00:00
|
|
|
#elif defined(INET6)
|
|
|
|
char *q;
|
|
|
|
#endif
|
1994-05-26 06:35:07 +00:00
|
|
|
|
2013-08-01 04:50:46 +00:00
|
|
|
if (idx < 0 || idx >= RTAX_MAX)
|
|
|
|
usage("internal error");
|
1994-05-26 06:35:07 +00:00
|
|
|
if (af == 0) {
|
2013-07-17 14:05:20 +00:00
|
|
|
#if defined(INET)
|
1994-05-26 06:35:07 +00:00
|
|
|
af = AF_INET;
|
|
|
|
aflen = sizeof(struct sockaddr_in);
|
2013-07-17 14:05:20 +00:00
|
|
|
#elif defined(INET6)
|
|
|
|
af = AF_INET6;
|
|
|
|
aflen = sizeof(struct sockaddr_in6);
|
|
|
|
#else
|
|
|
|
af = AF_LINK;
|
|
|
|
aflen = sizeof(struct sockaddr_dl);
|
|
|
|
#endif
|
1994-05-26 06:35:07 +00:00
|
|
|
}
|
2013-07-21 14:27:07 +00:00
|
|
|
#ifndef INET
|
|
|
|
hpp = NULL;
|
|
|
|
#endif
|
2013-07-20 16:46:51 +00:00
|
|
|
rtm_addrs |= (1 << idx);
|
|
|
|
sa = (struct sockaddr *)&so[idx];
|
2013-07-17 14:05:20 +00:00
|
|
|
sa->sa_family = af;
|
|
|
|
sa->sa_len = aflen;
|
|
|
|
|
2013-07-20 16:46:51 +00:00
|
|
|
switch (idx) {
|
|
|
|
case RTAX_GATEWAY:
|
2013-01-08 17:24:43 +00:00
|
|
|
if (nrflags & F_INTERFACE) {
|
2001-06-11 12:39:29 +00:00
|
|
|
struct ifaddrs *ifap, *ifa;
|
2013-07-20 15:23:42 +00:00
|
|
|
struct sockaddr_dl *sdl0 = (struct sockaddr_dl *)(void *)sa;
|
2001-06-11 12:39:29 +00:00
|
|
|
struct sockaddr_dl *sdl = NULL;
|
|
|
|
|
|
|
|
if (getifaddrs(&ifap))
|
2013-07-17 14:05:20 +00:00
|
|
|
err(EX_OSERR, "getifaddrs");
|
2001-06-11 12:39:29 +00:00
|
|
|
|
2010-02-27 10:18:33 +00:00
|
|
|
for (ifa = ifap; ifa != NULL; ifa = ifa->ifa_next) {
|
2001-06-11 12:39:29 +00:00
|
|
|
if (ifa->ifa_addr->sa_family != AF_LINK)
|
|
|
|
continue;
|
|
|
|
|
2010-02-27 10:18:33 +00:00
|
|
|
if (strcmp(str, ifa->ifa_name) != 0)
|
2001-06-11 12:39:29 +00:00
|
|
|
continue;
|
1996-08-09 22:52:02 +00:00
|
|
|
|
2013-07-20 15:23:42 +00:00
|
|
|
sdl = (struct sockaddr_dl *)(void *)ifa->ifa_addr;
|
2001-06-11 12:39:29 +00:00
|
|
|
}
|
1996-08-09 22:52:02 +00:00
|
|
|
/* If we found it, then use it */
|
2010-02-27 10:18:33 +00:00
|
|
|
if (sdl != NULL) {
|
2001-06-11 12:39:29 +00:00
|
|
|
/*
|
|
|
|
* Note that we need to copy before calling
|
|
|
|
* freeifaddrs().
|
|
|
|
*/
|
2013-07-17 14:05:20 +00:00
|
|
|
memcpy(sdl0, sdl, sdl->sdl_len);
|
1996-08-09 22:52:02 +00:00
|
|
|
}
|
2001-06-11 12:39:29 +00:00
|
|
|
freeifaddrs(ifap);
|
2010-02-27 10:18:33 +00:00
|
|
|
if (sdl != NULL)
|
2001-06-11 12:39:29 +00:00
|
|
|
return(1);
|
1996-08-09 22:52:02 +00:00
|
|
|
}
|
1994-05-26 06:35:07 +00:00
|
|
|
break;
|
2013-07-20 16:46:51 +00:00
|
|
|
case RTAX_IFP:
|
2013-07-17 14:05:20 +00:00
|
|
|
sa->sa_family = AF_LINK;
|
1994-05-26 06:35:07 +00:00
|
|
|
break;
|
|
|
|
}
|
2010-02-27 10:18:33 +00:00
|
|
|
if (strcmp(str, "default") == 0) {
|
1997-07-18 09:05:12 +00:00
|
|
|
/*
|
2010-02-27 10:18:33 +00:00
|
|
|
* Default is net 0.0.0.0/0
|
1997-07-18 09:05:12 +00:00
|
|
|
*/
|
2013-07-20 16:46:51 +00:00
|
|
|
switch (idx) {
|
|
|
|
case RTAX_DST:
|
1994-05-26 06:35:07 +00:00
|
|
|
forcenet++;
|
2013-07-20 16:46:51 +00:00
|
|
|
getaddr(RTAX_NETMASK, str, 0, nrflags);
|
1994-05-26 06:35:07 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
return (0);
|
|
|
|
}
|
2013-07-17 14:05:20 +00:00
|
|
|
switch (sa->sa_family) {
|
1999-12-07 17:39:16 +00:00
|
|
|
#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;
|
2013-07-20 16:46:51 +00:00
|
|
|
if (idx == RTAX_DST && (q = strchr(str, '/')) != NULL)
|
2002-05-21 18:11:31 +00:00
|
|
|
*q = '\0';
|
2001-06-11 12:39:29 +00:00
|
|
|
memset(&hints, 0, sizeof(hints));
|
2013-07-17 14:05:20 +00:00
|
|
|
hints.ai_family = sa->sa_family;
|
|
|
|
hints.ai_socktype = SOCK_DGRAM;
|
2010-02-27 10:18:33 +00:00
|
|
|
ecode = getaddrinfo(str, NULL, &hints, &res);
|
2005-05-23 14:12:32 +00:00
|
|
|
if (ecode != 0 || res->ai_family != AF_INET6 ||
|
2013-07-17 14:05:20 +00:00
|
|
|
res->ai_addrlen != sizeof(struct sockaddr_in6))
|
|
|
|
errx(EX_OSERR, "%s: %s", str, gai_strerror(ecode));
|
|
|
|
memcpy(sa, res->ai_addr, res->ai_addrlen);
|
2001-06-11 12:39:29 +00:00
|
|
|
freeaddrinfo(res);
|
2002-05-21 18:11:31 +00:00
|
|
|
if (q != NULL)
|
|
|
|
*q++ = '/';
|
2013-07-20 16:46:51 +00:00
|
|
|
if (idx == RTAX_DST)
|
2013-07-20 15:23:42 +00:00
|
|
|
return (inet6_makenetandmask((struct sockaddr_in6 *)(void *)sa, 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:
|
2013-07-17 14:05:20 +00:00
|
|
|
{
|
2013-07-20 15:23:42 +00:00
|
|
|
struct sockaddr_at *sat = (struct sockaddr_at *)(void *)sa;
|
2013-07-17 14:05:20 +00:00
|
|
|
|
|
|
|
if (!atalk_aton(str, &sat->sat_addr))
|
2010-02-27 10:18:33 +00:00
|
|
|
errx(EX_NOHOST, "bad address: %s", str);
|
1996-07-23 22:00:14 +00:00
|
|
|
rtm_addrs |= RTA_NETMASK;
|
2013-07-17 14:05:20 +00:00
|
|
|
return(forcehost || sat->sat_addr.s_node != 0);
|
|
|
|
}
|
1994-05-26 06:35:07 +00:00
|
|
|
case AF_LINK:
|
2013-07-20 15:23:42 +00:00
|
|
|
link_addr(str, (struct sockaddr_dl *)(void *)sa);
|
1994-05-26 06:35:07 +00:00
|
|
|
return (1);
|
|
|
|
|
|
|
|
case PF_ROUTE:
|
2013-07-17 14:05:20 +00:00
|
|
|
sockaddr(str, sa, sizeof(struct sockaddr_storage));
|
1994-05-26 06:35:07 +00:00
|
|
|
return (1);
|
2013-07-17 14:05:20 +00:00
|
|
|
#ifdef INET
|
1994-05-26 06:35:07 +00:00
|
|
|
case AF_INET:
|
2013-07-17 14:05:20 +00:00
|
|
|
#endif
|
1994-05-26 06:35:07 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2013-07-17 14:05:20 +00:00
|
|
|
#ifdef INET
|
2013-07-20 15:23:42 +00:00
|
|
|
sin = (struct sockaddr_in *)(void *)sa;
|
1994-05-26 06:35:07 +00:00
|
|
|
if (hpp == NULL)
|
|
|
|
hpp = &hp;
|
|
|
|
*hpp = NULL;
|
1997-04-02 17:05:30 +00:00
|
|
|
|
2010-02-27 10:18:33 +00:00
|
|
|
q = strchr(str,'/');
|
2013-07-20 16:46:51 +00:00
|
|
|
if (q != NULL && idx == RTAX_DST) {
|
1997-04-02 17:05:30 +00:00
|
|
|
*q = '\0';
|
2010-02-27 10:18:33 +00:00
|
|
|
if ((val = inet_network(str)) != INADDR_NONE) {
|
2013-07-17 14:05:20 +00:00
|
|
|
inet_makenetandmask(val, sin,
|
|
|
|
(struct sockaddr_in *)&so[RTAX_NETMASK],
|
|
|
|
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
|
|
|
}
|
2013-07-20 16:46:51 +00:00
|
|
|
if ((idx != RTAX_DST || forcenet == 0) &&
|
2013-07-17 14:05:20 +00:00
|
|
|
inet_aton(str, &sin->sin_addr)) {
|
|
|
|
val = sin->sin_addr.s_addr;
|
2013-07-20 16:46:51 +00:00
|
|
|
if (idx != RTAX_DST || forcehost ||
|
2013-07-17 14:05:20 +00:00
|
|
|
inet_lnaof(sin->sin_addr) != INADDR_ANY)
|
1994-05-26 06:35:07 +00:00
|
|
|
return (1);
|
|
|
|
else {
|
|
|
|
val = ntohl(val);
|
|
|
|
goto netdone;
|
|
|
|
}
|
|
|
|
}
|
2013-07-20 16:46:51 +00:00
|
|
|
if (idx == RTAX_DST && forcehost == 0 &&
|
2010-02-27 10:18:33 +00:00
|
|
|
((val = inet_network(str)) != INADDR_NONE ||
|
|
|
|
((np = getnetbyname(str)) != NULL && (val = np->n_net) != 0))) {
|
1994-05-26 06:35:07 +00:00
|
|
|
netdone:
|
2013-07-17 14:05:20 +00:00
|
|
|
inet_makenetandmask(val, sin,
|
|
|
|
(struct sockaddr_in *)&so[RTAX_NETMASK], 0);
|
1994-05-26 06:35:07 +00:00
|
|
|
return (0);
|
|
|
|
}
|
2010-02-27 10:18:33 +00:00
|
|
|
hp = gethostbyname(str);
|
|
|
|
if (hp != NULL) {
|
1994-05-26 06:35:07 +00:00
|
|
|
*hpp = hp;
|
2013-07-17 14:05:20 +00:00
|
|
|
sin->sin_family = hp->h_addrtype;
|
|
|
|
memmove((char *)&sin->sin_addr, hp->h_addr,
|
|
|
|
MIN((size_t)hp->h_length, sizeof(sin->sin_addr)));
|
1994-05-26 06:35:07 +00:00
|
|
|
return (1);
|
|
|
|
}
|
2013-07-17 14:05:20 +00:00
|
|
|
#endif
|
2010-02-27 10:18:33 +00:00
|
|
|
errx(EX_NOHOST, "bad address: %s", str);
|
1994-05-26 06:35:07 +00:00
|
|
|
}
|
|
|
|
|
2010-02-27 10:18:33 +00:00
|
|
|
static int
|
|
|
|
prefixlen(const char *str)
|
1999-12-07 17:39:16 +00:00
|
|
|
{
|
2010-02-27 10:18:33 +00:00
|
|
|
int len = atoi(str), q, r;
|
1999-12-07 17:39:16 +00:00
|
|
|
int max;
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
rtm_addrs |= RTA_NETMASK;
|
|
|
|
switch (af) {
|
|
|
|
#ifdef INET6
|
|
|
|
case AF_INET6:
|
2013-07-17 14:05:20 +00:00
|
|
|
{
|
|
|
|
struct sockaddr_in6 *sin6 =
|
|
|
|
(struct sockaddr_in6 *)&so[RTAX_NETMASK];
|
|
|
|
|
1999-12-07 17:39:16 +00:00
|
|
|
max = 128;
|
2013-07-17 14:05:20 +00:00
|
|
|
p = (char *)&sin6->sin6_addr;
|
|
|
|
sin6->sin6_family = AF_INET6;
|
|
|
|
sin6->sin6_len = sizeof(*sin6);
|
1999-12-07 17:39:16 +00:00
|
|
|
break;
|
2013-07-17 14:05:20 +00:00
|
|
|
}
|
1999-12-07 17:39:16 +00:00
|
|
|
#endif
|
2013-07-17 14:05:20 +00:00
|
|
|
#ifdef INET
|
1999-12-07 17:39:16 +00:00
|
|
|
case AF_INET:
|
2013-07-17 14:05:20 +00:00
|
|
|
{
|
|
|
|
struct sockaddr_in *sin =
|
|
|
|
(struct sockaddr_in *)&so[RTAX_NETMASK];
|
|
|
|
|
1999-12-07 17:39:16 +00:00
|
|
|
max = 32;
|
2013-07-17 14:05:20 +00:00
|
|
|
p = (char *)&sin->sin_addr;
|
|
|
|
sin->sin_family = AF_INET;
|
|
|
|
sin->sin_len = sizeof(*sin);
|
1999-12-07 17:39:16 +00:00
|
|
|
break;
|
2013-07-17 14:05:20 +00:00
|
|
|
}
|
|
|
|
#endif
|
1999-12-07 17:39:16 +00:00
|
|
|
default:
|
2013-07-17 14:05:20 +00:00
|
|
|
errx(EX_OSERR, "prefixlen not supported in this af");
|
1999-12-07 17:39:16 +00:00
|
|
|
}
|
|
|
|
|
2013-07-17 14:05:20 +00:00
|
|
|
if (len < 0 || max < len)
|
|
|
|
errx(EX_USAGE, "%s: invalid prefixlen", str);
|
1999-12-07 17:39:16 +00:00
|
|
|
|
|
|
|
q = len >> 3;
|
|
|
|
r = len & 7;
|
|
|
|
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)
|
2010-02-27 10:18:33 +00:00
|
|
|
return (-1);
|
1999-12-07 17:39:16 +00:00
|
|
|
else
|
2010-02-27 10:18:33 +00:00
|
|
|
return (len);
|
1999-12-07 17:39:16 +00:00
|
|
|
}
|
1994-05-26 06:35:07 +00:00
|
|
|
|
2010-02-27 10:18:33 +00:00
|
|
|
static void
|
2009-08-25 04:09:09 +00:00
|
|
|
interfaces(void)
|
1994-05-26 06:35:07 +00:00
|
|
|
{
|
|
|
|
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 */
|
2013-07-17 14:05:20 +00:00
|
|
|
mib[3] = AF_UNSPEC;
|
1994-05-26 06:35:07 +00:00
|
|
|
mib[4] = NET_RT_IFLIST;
|
|
|
|
mib[5] = 0; /* no flags */
|
2013-07-17 14:05:20 +00:00
|
|
|
if (sysctl(mib, nitems(mib), 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");
|
2013-07-17 14:05:20 +00:00
|
|
|
if (sysctl(mib, nitems(mib), buf, &needed, NULL, 0) < 0) {
|
2004-04-30 22:34:12 +00:00
|
|
|
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) {
|
2013-07-20 15:23:42 +00:00
|
|
|
rtm = (struct rt_msghdr *)(void *)next;
|
1994-05-26 06:35:07 +00:00
|
|
|
print_rtmsg(rtm, rtm->rtm_msglen);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-02-27 10:18:33 +00:00
|
|
|
static void
|
2012-11-17 19:54:23 +00:00
|
|
|
monitor(int argc, char *argv[])
|
1994-05-26 06:35:07 +00:00
|
|
|
{
|
2012-11-17 19:54:23 +00:00
|
|
|
int n, fib, error;
|
|
|
|
char msg[2048], *endptr;
|
|
|
|
|
|
|
|
fib = defaultfib;
|
|
|
|
while (argc > 1) {
|
|
|
|
argc--;
|
|
|
|
argv++;
|
|
|
|
if (**argv != '-')
|
|
|
|
usage(*argv);
|
|
|
|
switch (keyword(*argv + 1)) {
|
|
|
|
case K_FIB:
|
|
|
|
if (!--argc)
|
|
|
|
usage(*argv);
|
2012-12-16 23:46:59 +00:00
|
|
|
errno = 0;
|
2012-11-17 19:54:23 +00:00
|
|
|
fib = strtol(*++argv, &endptr, 0);
|
2012-12-16 23:46:59 +00:00
|
|
|
if (errno == 0) {
|
|
|
|
if (*endptr != '\0' ||
|
|
|
|
fib < 0 ||
|
|
|
|
(numfibs != -1 && fib > numfibs - 1))
|
|
|
|
errno = EINVAL;
|
|
|
|
}
|
|
|
|
if (errno)
|
|
|
|
errx(EX_USAGE, "invalid fib number: %s", *argv);
|
2012-11-17 19:54:23 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
usage(*argv);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
error = set_sofib(fib);
|
|
|
|
if (error)
|
|
|
|
errx(EX_USAGE, "invalid fib number: %d", fib);
|
1994-05-26 06:35:07 +00:00
|
|
|
|
|
|
|
verbose = 1;
|
|
|
|
if (debugonly) {
|
|
|
|
interfaces();
|
|
|
|
exit(0);
|
|
|
|
}
|
2010-02-27 10:18:33 +00:00
|
|
|
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);
|
2013-07-17 14:05:20 +00:00
|
|
|
(void)printf("\ngot message of size %d on %s", n, ctime(&now));
|
2013-07-20 15:23:42 +00:00
|
|
|
print_rtmsg((struct rt_msghdr *)(void *)msg, n);
|
1994-05-26 06:35:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-17 14:05:20 +00:00
|
|
|
static struct {
|
1994-05-26 06:35:07 +00:00
|
|
|
struct rt_msghdr m_rtm;
|
|
|
|
char m_space[512];
|
|
|
|
} m_rtmsg;
|
|
|
|
|
2010-02-27 10:18:33 +00:00
|
|
|
static int
|
2012-11-17 19:54:23 +00:00
|
|
|
rtmsg(int cmd, int flags, int fib)
|
1994-05-26 06:35:07 +00:00
|
|
|
{
|
|
|
|
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
|
|
|
|
2013-07-17 14:05:20 +00:00
|
|
|
#define NEXTADDR(w, u) \
|
|
|
|
if (rtm_addrs & (w)) { \
|
2013-07-18 02:34:25 +00:00
|
|
|
l = (((struct sockaddr *)&(u))->sa_len == 0) ? \
|
|
|
|
sizeof(long) : \
|
|
|
|
1 + ((((struct sockaddr *)&(u))->sa_len - 1) \
|
|
|
|
| (sizeof(long) - 1)); \
|
2013-07-17 14:05:20 +00:00
|
|
|
memmove(cp, (char *)&(u), l); \
|
|
|
|
cp += l; \
|
|
|
|
if (verbose) \
|
|
|
|
sodump((struct sockaddr *)&(u), #w); \
|
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;
|
2009-04-14 23:05:36 +00:00
|
|
|
else if (cmd == 'g' || cmd == 's') {
|
1994-05-26 06:35:07 +00:00
|
|
|
cmd = RTM_GET;
|
2013-07-17 14:05:20 +00:00
|
|
|
if (so[RTAX_IFP].ss_family == 0) {
|
|
|
|
so[RTAX_IFP].ss_family = AF_LINK;
|
|
|
|
so[RTAX_IFP].ss_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;
|
|
|
|
|
2013-07-17 14:05:20 +00:00
|
|
|
NEXTADDR(RTA_DST, so[RTAX_DST]);
|
|
|
|
NEXTADDR(RTA_GATEWAY, so[RTAX_GATEWAY]);
|
|
|
|
NEXTADDR(RTA_NETMASK, so[RTAX_NETMASK]);
|
|
|
|
NEXTADDR(RTA_GENMASK, so[RTAX_GENMASK]);
|
|
|
|
NEXTADDR(RTA_IFP, so[RTAX_IFP]);
|
|
|
|
NEXTADDR(RTA_IFA, so[RTAX_IFA]);
|
1994-05-26 06:35:07 +00:00
|
|
|
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
|
2012-11-17 19:54:23 +00:00
|
|
|
print_getmsg(&rtm, l, fib);
|
1994-05-26 06:35:07 +00:00
|
|
|
}
|
|
|
|
#undef rtm
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2013-07-17 14:05:20 +00:00
|
|
|
static const char *msgtypes[] = {
|
1994-05-26 06:35:07 +00:00
|
|
|
"",
|
|
|
|
"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",
|
2010-12-08 15:10:27 +00:00
|
|
|
"RTM_IEEE80211: IEEE 802.11 wireless event",
|
1994-05-26 06:35:07 +00:00
|
|
|
};
|
|
|
|
|
2013-07-17 14:05:20 +00:00
|
|
|
static const char metricnames[] =
|
|
|
|
"\011weight\010rttvar\7rtt\6ssthresh\5sendpipe\4recvpipe\3expire"
|
|
|
|
"\1mtu";
|
|
|
|
static const char routeflags[] =
|
|
|
|
"\1UP\2GATEWAY\3HOST\4REJECT\5DYNAMIC\6MODIFIED\7DONE"
|
|
|
|
"\012XRESOLVE\013LLINFO\014STATIC\015BLACKHOLE"
|
|
|
|
"\017PROTO2\020PROTO1\021PRCLONING\022WASCLONED\023PROTO3"
|
|
|
|
"\025PINNED\026LOCAL\027BROADCAST\030MULTICAST\035STICKY";
|
|
|
|
static const char ifnetflags[] =
|
|
|
|
"\1UP\2BROADCAST\3DEBUG\4LOOPBACK\5PTP\6b6\7RUNNING\010NOARP"
|
|
|
|
"\011PPROMISC\012ALLMULTI\013OACTIVE\014SIMPLEX\015LINK0\016LINK1"
|
|
|
|
"\017LINK2\020MULTICAST";
|
|
|
|
static const char addrnames[] =
|
|
|
|
"\1DST\2GATEWAY\3NETMASK\4GENMASK\5IFP\6IFA\7AUTHOR\010BRD";
|
1994-05-26 06:35:07 +00:00
|
|
|
|
2010-12-08 15:12:37 +00:00
|
|
|
static const char errfmt[] =
|
2013-07-17 14:05:20 +00:00
|
|
|
"\n%s: truncated route message, only %zu bytes left\n";
|
2010-12-08 15:12:37 +00:00
|
|
|
|
2010-02-27 10:18:33 +00:00
|
|
|
static void
|
2010-12-08 15:12:37 +00:00
|
|
|
print_rtmsg(struct rt_msghdr *rtm, size_t msglen)
|
1994-05-26 06:35:07 +00:00
|
|
|
{
|
|
|
|
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;
|
2010-02-27 10:18:33 +00:00
|
|
|
const char *state;
|
1994-05-26 06:35:07 +00:00
|
|
|
|
|
|
|
if (verbose == 0)
|
|
|
|
return;
|
|
|
|
if (rtm->rtm_version != RTM_VERSION) {
|
2013-07-17 14:05:20 +00:00
|
|
|
(void)printf("routing message version %d not understood\n",
|
1994-05-26 06:35:07 +00:00
|
|
|
rtm->rtm_version);
|
|
|
|
return;
|
|
|
|
}
|
2013-07-21 11:59:41 +00:00
|
|
|
if (rtm->rtm_type < nitems(msgtypes))
|
2002-01-18 14:33:04 +00:00
|
|
|
(void)printf("%s: ", msgtypes[rtm->rtm_type]);
|
|
|
|
else
|
2010-12-08 15:12:37 +00:00
|
|
|
(void)printf("unknown type %d: ", rtm->rtm_type);
|
2002-01-18 14:33:04 +00:00
|
|
|
(void)printf("len %d, ", rtm->rtm_msglen);
|
2010-12-08 15:12:37 +00:00
|
|
|
|
|
|
|
#define REQUIRE(x) do { \
|
|
|
|
if (msglen < sizeof(x)) \
|
|
|
|
goto badlen; \
|
|
|
|
else \
|
|
|
|
msglen -= sizeof(x); \
|
|
|
|
} while (0)
|
|
|
|
|
1994-05-26 06:35:07 +00:00
|
|
|
switch (rtm->rtm_type) {
|
|
|
|
case RTM_IFINFO:
|
2010-12-08 15:12:37 +00:00
|
|
|
REQUIRE(struct if_msghdr);
|
1994-05-26 06:35:07 +00:00
|
|
|
ifm = (struct if_msghdr *)rtm;
|
2013-07-17 14:05:20 +00:00
|
|
|
(void)printf("if# %d, ", ifm->ifm_index);
|
2004-05-03 16:30:32 +00:00
|
|
|
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;
|
|
|
|
}
|
2013-07-17 14:05:20 +00:00
|
|
|
(void)printf("link: %s, flags:", state);
|
|
|
|
printb(ifm->ifm_flags, ifnetflags);
|
2010-12-08 15:12:37 +00:00
|
|
|
pmsg_addrs((char *)(ifm + 1), ifm->ifm_addrs, msglen);
|
1994-05-26 06:35:07 +00:00
|
|
|
break;
|
|
|
|
case RTM_NEWADDR:
|
|
|
|
case RTM_DELADDR:
|
2010-12-08 15:12:37 +00:00
|
|
|
REQUIRE(struct ifa_msghdr);
|
1994-05-26 06:35:07 +00:00
|
|
|
ifam = (struct ifa_msghdr *)rtm;
|
2013-07-17 14:05:20 +00:00
|
|
|
(void)printf("metric %d, flags:", ifam->ifam_metric);
|
|
|
|
printb(ifam->ifam_flags, routeflags);
|
2010-12-08 15:12:37 +00:00
|
|
|
pmsg_addrs((char *)(ifam + 1), ifam->ifam_addrs, msglen);
|
1994-05-26 06:35:07 +00:00
|
|
|
break;
|
1997-01-09 21:34:03 +00:00
|
|
|
#ifdef RTM_NEWMADDR
|
|
|
|
case RTM_NEWMADDR:
|
|
|
|
case RTM_DELMADDR:
|
2010-12-08 15:12:37 +00:00
|
|
|
REQUIRE(struct ifma_msghdr);
|
1997-01-09 21:34:03 +00:00
|
|
|
ifmam = (struct ifma_msghdr *)rtm;
|
2010-12-08 15:12:37 +00:00
|
|
|
pmsg_addrs((char *)(ifmam + 1), ifmam->ifmam_addrs, msglen);
|
1997-01-09 21:34:03 +00:00
|
|
|
break;
|
|
|
|
#endif
|
2002-01-18 14:33:04 +00:00
|
|
|
case RTM_IFANNOUNCE:
|
2010-12-08 15:12:37 +00:00
|
|
|
REQUIRE(struct if_announcemsghdr);
|
2002-01-18 14:33:04 +00:00
|
|
|
ifan = (struct if_announcemsghdr *)rtm;
|
2013-07-17 14:05:20 +00:00
|
|
|
(void)printf("if# %d, what: ", ifan->ifan_index);
|
2002-01-18 14:33:04 +00:00
|
|
|
switch (ifan->ifan_what) {
|
|
|
|
case IFAN_ARRIVAL:
|
2013-07-17 14:05:20 +00:00
|
|
|
(void)printf("arrival");
|
2002-01-18 14:33:04 +00:00
|
|
|
break;
|
|
|
|
case IFAN_DEPARTURE:
|
|
|
|
printf("departure");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
printf("#%d", ifan->ifan_what);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
printf("\n");
|
2012-12-04 11:10:01 +00:00
|
|
|
fflush(stdout);
|
2002-01-18 14:33:04 +00:00
|
|
|
break;
|
|
|
|
|
1994-05-26 06:35:07 +00:00
|
|
|
default:
|
2013-07-17 14:05:20 +00:00
|
|
|
printf("pid: %ld, seq %d, errno %d, flags:",
|
1996-01-02 20:09:22 +00:00
|
|
|
(long)rtm->rtm_pid, rtm->rtm_seq, rtm->rtm_errno);
|
2013-07-17 14:05:20 +00:00
|
|
|
printb(rtm->rtm_flags, routeflags);
|
2010-12-08 15:12:37 +00:00
|
|
|
pmsg_common(rtm, msglen);
|
1994-05-26 06:35:07 +00:00
|
|
|
}
|
2010-12-08 15:12:37 +00:00
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
badlen:
|
|
|
|
(void)printf(errfmt, __func__, msglen);
|
|
|
|
#undef REQUIRE
|
1994-05-26 06:35:07 +00:00
|
|
|
}
|
|
|
|
|
2010-02-27 10:18:33 +00:00
|
|
|
static void
|
2012-11-17 19:54:23 +00:00
|
|
|
print_getmsg(struct rt_msghdr *rtm, int msglen, int fib)
|
1994-05-26 06:35:07 +00:00
|
|
|
{
|
2013-07-20 16:46:51 +00:00
|
|
|
struct sockaddr *sp[RTAX_MAX];
|
2002-03-20 17:55:10 +00:00
|
|
|
char *cp;
|
|
|
|
int i;
|
1994-05-26 06:35:07 +00:00
|
|
|
|
2013-07-20 16:46:51 +00:00
|
|
|
memset(sp, 0, sizeof(sp));
|
2013-07-17 14:05:20 +00:00
|
|
|
(void)printf(" route to: %s\n",
|
|
|
|
routename((struct sockaddr *)&so[RTAX_DST]));
|
1994-05-26 06:35:07 +00:00
|
|
|
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);
|
2013-07-21 11:59:41 +00:00
|
|
|
return;
|
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));
|
2013-07-24 04:05:48 +00:00
|
|
|
for (i = 0; i < RTAX_MAX; i++)
|
|
|
|
if (rtm->rtm_addrs & (1 << i)) {
|
2013-07-20 16:46:51 +00:00
|
|
|
sp[i] = (struct sockaddr *)cp;
|
2013-07-24 04:05:48 +00:00
|
|
|
cp += SA_SIZE((struct sockaddr *)cp);
|
|
|
|
}
|
|
|
|
if ((rtm->rtm_addrs & RTA_IFP) &&
|
|
|
|
(sp[RTAX_IFP]->sa_family != AF_LINK ||
|
|
|
|
((struct sockaddr_dl *)(void *)sp[RTAX_IFP])->sdl_nlen == 0))
|
2013-07-20 16:46:51 +00:00
|
|
|
sp[RTAX_IFP] = NULL;
|
|
|
|
if (sp[RTAX_DST] && sp[RTAX_NETMASK])
|
|
|
|
sp[RTAX_NETMASK]->sa_family = sp[RTAX_DST]->sa_family; /* XXX */
|
|
|
|
if (sp[RTAX_DST])
|
|
|
|
(void)printf("destination: %s\n", routename(sp[RTAX_DST]));
|
|
|
|
if (sp[RTAX_NETMASK])
|
|
|
|
(void)printf(" mask: %s\n", routename(sp[RTAX_NETMASK]));
|
|
|
|
if (sp[RTAX_GATEWAY] && (rtm->rtm_flags & RTF_GATEWAY))
|
|
|
|
(void)printf(" gateway: %s\n", routename(sp[RTAX_GATEWAY]));
|
2012-11-17 19:54:23 +00:00
|
|
|
if (fib >= 0)
|
|
|
|
(void)printf(" fib: %u\n", (unsigned int)fib);
|
2013-07-20 16:46:51 +00:00
|
|
|
if (sp[RTAX_IFP])
|
1994-05-26 06:35:07 +00:00
|
|
|
(void)printf(" interface: %.*s\n",
|
2013-07-20 16:46:51 +00:00
|
|
|
((struct sockaddr_dl *)(void *)sp[RTAX_IFP])->sdl_nlen,
|
|
|
|
((struct sockaddr_dl *)(void *)sp[RTAX_IFP])->sdl_data);
|
1994-05-26 06:35:07 +00:00
|
|
|
(void)printf(" flags: ");
|
2013-07-17 14:05:20 +00:00
|
|
|
printb(rtm->rtm_flags, routeflags);
|
1994-05-26 06:35:07 +00:00
|
|
|
|
|
|
|
#define lock(f) ((rtm->rtm_rmx.rmx_locks & __CONCAT(RTV_,f)) ? 'L' : ' ')
|
|
|
|
#define msec(u) (((u) + 500) / 1000) /* usec to msec */
|
2013-07-21 11:59:41 +00:00
|
|
|
printf("\n%9s %9s %9s %9s %9s %10s %9s\n", "recvpipe",
|
|
|
|
"sendpipe", "ssthresh", "rtt,msec", "mtu ", "weight", "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 ", rtm->rtm_rmx.rmx_mtu, lock(MTU));
|
2009-04-14 23:05:36 +00:00
|
|
|
printf("%8ld%c ", rtm->rtm_rmx.rmx_weight, lock(WEIGHT));
|
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)
|
2010-12-08 15:12:37 +00:00
|
|
|
pmsg_common(rtm, msglen);
|
1994-05-26 06:35:07 +00:00
|
|
|
else if (rtm->rtm_addrs &~ RTA_IGN) {
|
2013-07-17 14:05:20 +00:00
|
|
|
(void)printf("sockaddrs: ");
|
|
|
|
printb(rtm->rtm_addrs, addrnames);
|
1994-05-26 06:35:07 +00:00
|
|
|
putchar('\n');
|
|
|
|
}
|
|
|
|
#undef RTA_IGN
|
|
|
|
}
|
|
|
|
|
2010-02-27 10:18:33 +00:00
|
|
|
static void
|
2010-12-08 15:12:37 +00:00
|
|
|
pmsg_common(struct rt_msghdr *rtm, size_t msglen)
|
1994-05-26 06:35:07 +00:00
|
|
|
{
|
2013-07-17 14:05:20 +00:00
|
|
|
|
|
|
|
(void)printf("\nlocks: ");
|
|
|
|
printb(rtm->rtm_rmx.rmx_locks, metricnames);
|
|
|
|
(void)printf(" inits: ");
|
|
|
|
printb(rtm->rtm_inits, metricnames);
|
2010-12-08 15:12:37 +00:00
|
|
|
if (msglen > sizeof(struct rt_msghdr))
|
|
|
|
pmsg_addrs(((char *)(rtm + 1)), rtm->rtm_addrs,
|
|
|
|
msglen - sizeof(struct rt_msghdr));
|
|
|
|
else
|
2013-07-17 14:05:20 +00:00
|
|
|
(void)fflush(stdout);
|
1994-05-26 06:35:07 +00:00
|
|
|
}
|
|
|
|
|
2010-02-27 10:18:33 +00:00
|
|
|
static void
|
2010-12-08 15:12:37 +00:00
|
|
|
pmsg_addrs(char *cp, int addrs, size_t len)
|
1994-05-26 06:35:07 +00:00
|
|
|
{
|
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) {
|
2013-07-17 14:05:20 +00:00
|
|
|
(void)putchar('\n');
|
1994-05-26 06:35:07 +00:00
|
|
|
return;
|
2001-01-15 12:28:48 +00:00
|
|
|
}
|
2013-07-17 14:05:20 +00:00
|
|
|
(void)printf("\nsockaddrs: ");
|
|
|
|
printb(addrs, addrnames);
|
|
|
|
putchar('\n');
|
2013-07-21 11:59:41 +00:00
|
|
|
for (i = 0; i < RTAX_MAX; i++)
|
|
|
|
if (addrs & (1 << i)) {
|
1994-05-26 06:35:07 +00:00
|
|
|
sa = (struct sockaddr *)cp;
|
2010-12-08 15:12:37 +00:00
|
|
|
if (len == 0 || len < SA_SIZE(sa)) {
|
2013-07-17 14:05:20 +00:00
|
|
|
(void)printf(errfmt, __func__, len);
|
2010-12-08 15:12:37 +00:00
|
|
|
break;
|
|
|
|
}
|
2013-07-17 14:05:20 +00:00
|
|
|
(void)printf(" %s", routename(sa));
|
2010-12-08 15:12:37 +00:00
|
|
|
len -= SA_SIZE(sa);
|
2004-04-13 11:24:43 +00:00
|
|
|
cp += SA_SIZE(sa);
|
1994-05-26 06:35:07 +00:00
|
|
|
}
|
2013-07-17 14:05:20 +00:00
|
|
|
(void)putchar('\n');
|
|
|
|
(void)fflush(stdout);
|
1994-05-26 06:35:07 +00:00
|
|
|
}
|
|
|
|
|
2010-02-27 10:18:33 +00:00
|
|
|
static void
|
2013-07-17 14:05:20 +00:00
|
|
|
printb(int b, const char *str)
|
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;
|
2010-02-27 10:18:33 +00:00
|
|
|
while ((i = *str++) != 0) {
|
1994-05-26 06:35:07 +00:00
|
|
|
if (b & (1 << (i-1))) {
|
|
|
|
if (gotsome == 0)
|
|
|
|
i = '<';
|
|
|
|
else
|
|
|
|
i = ',';
|
2013-07-17 14:05:20 +00:00
|
|
|
putchar(i);
|
1994-05-26 06:35:07 +00:00
|
|
|
gotsome = 1;
|
2010-02-27 10:18:33 +00:00
|
|
|
for (; (i = *str) > 32; str++)
|
2013-07-17 14:05:20 +00:00
|
|
|
putchar(i);
|
1994-05-26 06:35:07 +00:00
|
|
|
} else
|
2010-02-27 10:18:33 +00:00
|
|
|
while (*str > 32)
|
|
|
|
str++;
|
1994-05-26 06:35:07 +00:00
|
|
|
}
|
|
|
|
if (gotsome)
|
2013-07-17 14:05:20 +00:00
|
|
|
putchar('>');
|
1994-05-26 06:35:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2010-02-27 10:18:33 +00:00
|
|
|
keyword(const char *cp)
|
1994-05-26 06:35:07 +00:00
|
|
|
{
|
2002-03-20 17:55:10 +00:00
|
|
|
struct keytab *kt = keywords;
|
1994-05-26 06:35:07 +00:00
|
|
|
|
2010-02-27 10:18:33 +00:00
|
|
|
while (kt->kt_cp != NULL && strcmp(kt->kt_cp, cp) != 0)
|
1994-05-26 06:35:07 +00:00
|
|
|
kt++;
|
2010-02-27 10:18:33 +00:00
|
|
|
return (kt->kt_i);
|
1994-05-26 06:35:07 +00:00
|
|
|
}
|
|
|
|
|
2010-02-27 10:18:33 +00:00
|
|
|
static void
|
2013-07-17 14:05:20 +00:00
|
|
|
sodump(struct sockaddr *sa, const char *which)
|
1994-05-26 06:35:07 +00:00
|
|
|
{
|
2013-07-17 14:05:20 +00:00
|
|
|
#ifdef INET6
|
|
|
|
char nbuf[INET6_ADDRSTRLEN];
|
|
|
|
#endif
|
|
|
|
|
|
|
|
switch (sa->sa_family) {
|
1994-05-26 06:35:07 +00:00
|
|
|
case AF_LINK:
|
2013-07-17 14:05:20 +00:00
|
|
|
(void)printf("%s: link %s; ", which,
|
2013-07-20 15:23:42 +00:00
|
|
|
link_ntoa((struct sockaddr_dl *)(void *)sa));
|
1994-05-26 06:35:07 +00:00
|
|
|
break;
|
2013-07-17 14:05:20 +00:00
|
|
|
#ifdef INET
|
1994-05-26 06:35:07 +00:00
|
|
|
case AF_INET:
|
2013-07-17 14:05:20 +00:00
|
|
|
(void)printf("%s: inet %s; ", which,
|
2013-07-20 15:23:42 +00:00
|
|
|
inet_ntoa(((struct sockaddr_in *)(void *)sa)->sin_addr));
|
1994-05-26 06:35:07 +00:00
|
|
|
break;
|
2013-07-17 14:05:20 +00:00
|
|
|
#endif
|
|
|
|
#ifdef INET6
|
|
|
|
case AF_INET6:
|
|
|
|
(void)printf("%s: inet6 %s; ", which, inet_ntop(sa->sa_family,
|
2013-07-20 15:23:42 +00:00
|
|
|
&((struct sockaddr_in6 *)(void *)sa)->sin6_addr, nbuf,
|
2013-07-17 14:05:20 +00:00
|
|
|
sizeof(nbuf)));
|
|
|
|
break;
|
|
|
|
#endif
|
1996-07-09 19:02:28 +00:00
|
|
|
case AF_APPLETALK:
|
2013-07-17 14:05:20 +00:00
|
|
|
(void)printf("%s: atalk %s; ", which,
|
2013-07-20 15:23:42 +00:00
|
|
|
atalk_ntoa(((struct sockaddr_at *)(void *)sa)->sat_addr));
|
1996-07-09 19:02:28 +00:00
|
|
|
break;
|
1994-05-26 06:35:07 +00:00
|
|
|
}
|
2013-07-17 14:05:20 +00:00
|
|
|
(void)fflush(stdout);
|
1994-05-26 06:35:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* States*/
|
|
|
|
#define VIRGIN 0
|
|
|
|
#define GOTONE 1
|
|
|
|
#define GOTTWO 2
|
|
|
|
/* Inputs */
|
|
|
|
#define DIGIT (4*0)
|
|
|
|
#define END (4*1)
|
|
|
|
#define DELIM (4*2)
|
|
|
|
|
2010-02-27 10:18:33 +00:00
|
|
|
static void
|
2013-07-17 14:05:20 +00:00
|
|
|
sockaddr(char *addr, struct sockaddr *sa, size_t size)
|
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
|
|
|
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;
|
2010-02-27 10:18:33 +00:00
|
|
|
} else if (*addr == '\0')
|
1994-05-26 06:35:07 +00:00
|
|
|
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
|
|
|
|
2010-02-27 10:18:33 +00:00
|
|
|
static int
|
1996-07-09 19:02:28 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2010-02-27 10:18:33 +00:00
|
|
|
static char *
|
1996-07-09 19:02:28 +00:00
|
|
|
atalk_ntoa(struct at_addr at)
|
|
|
|
{
|
|
|
|
static char buf[20];
|
|
|
|
|
2013-07-17 14:05:20 +00:00
|
|
|
(void)snprintf(buf, sizeof(buf), "%u.%u", ntohs(at.s_net), at.s_node);
|
|
|
|
buf[sizeof(buf) - 1] = '\0';
|
1996-07-09 19:02:28 +00:00
|
|
|
return(buf);
|
|
|
|
}
|