route.c revision 258907
11558Srgrimes/* 21558Srgrimes * Copyright (c) 1983, 1989, 1991, 1993 31558Srgrimes * The Regents of the University of California. All rights reserved. 41558Srgrimes * 51558Srgrimes * Redistribution and use in source and binary forms, with or without 61558Srgrimes * modification, are permitted provided that the following conditions 71558Srgrimes * are met: 81558Srgrimes * 1. Redistributions of source code must retain the above copyright 91558Srgrimes * notice, this list of conditions and the following disclaimer. 101558Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 111558Srgrimes * notice, this list of conditions and the following disclaimer in the 121558Srgrimes * documentation and/or other materials provided with the distribution. 131558Srgrimes * 4. Neither the name of the University nor the names of its contributors 141558Srgrimes * may be used to endorse or promote products derived from this software 151558Srgrimes * without specific prior written permission. 161558Srgrimes * 171558Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 181558Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 191558Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 201558Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 211558Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 221558Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 231558Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 241558Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 251558Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 261558Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 271558Srgrimes * SUCH DAMAGE. 281558Srgrimes */ 291558Srgrimes 301558Srgrimes#ifndef lint 3113171Swollmanstatic const char copyright[] = 321558Srgrimes"@(#) Copyright (c) 1983, 1989, 1991, 1993\n\ 331558Srgrimes The Regents of the University of California. All rights reserved.\n"; 341558Srgrimes#endif /* not lint */ 351558Srgrimes 361558Srgrimes#ifndef lint 3737907Scharnier#if 0 3885048Srustatic char sccsid[] = "@(#)route.c 8.6 (Berkeley) 4/28/95"; 3937907Scharnier#endif 401558Srgrimes#endif /* not lint */ 411558Srgrimes 42196527Scharnier#include <sys/cdefs.h> 43196527Scharnier__FBSDID("$FreeBSD: head/sbin/route/route.c 258907 2013-12-04 04:29:52Z eadler $"); 44196527Scharnier 451558Srgrimes#include <sys/param.h> 461558Srgrimes#include <sys/file.h> 471558Srgrimes#include <sys/socket.h> 481558Srgrimes#include <sys/ioctl.h> 491558Srgrimes#include <sys/sysctl.h> 5051639Sbillf#include <sys/types.h> 51243185Shrs#include <sys/queue.h> 521558Srgrimes 531558Srgrimes#include <net/if.h> 541558Srgrimes#include <net/route.h> 551558Srgrimes#include <net/if_dl.h> 561558Srgrimes#include <netinet/in.h> 5778140Sru#include <netinet/if_ether.h> 5817046Sjulian#include <netatalk/at.h> 591558Srgrimes#include <arpa/inet.h> 601558Srgrimes#include <netdb.h> 611558Srgrimes 6220287Swollman#include <ctype.h> 6320287Swollman#include <err.h> 641558Srgrimes#include <errno.h> 6569793Sobrien#include <paths.h> 661558Srgrimes#include <stdio.h> 671558Srgrimes#include <stdlib.h> 681558Srgrimes#include <string.h> 6913171Swollman#include <sysexits.h> 70256695Shrs#include <time.h> 7120287Swollman#include <unistd.h> 7278064Sume#include <ifaddrs.h> 731558Srgrimes 74253427Shrsstatic struct keytab { 75204406Suqs const char *kt_cp; 761558Srgrimes int kt_i; 77258907Seadler} const keywords[] = { 781558Srgrimes#include "keywords.h" 791558Srgrimes {0, 0} 801558Srgrimes}; 811558Srgrimes 82253427Shrsstatic struct sockaddr_storage so[RTAX_MAX]; 83253427Shrsstatic int pid, rtm_addrs; 84253427Shrsstatic int s; 85253427Shrsstatic int forcehost, forcenet, nflag, af, qflag, tflag; 86253427Shrsstatic int verbose, aflen; 87253427Shrsstatic int locking, lockrest, debugonly; 88253427Shrsstatic struct rt_metrics rt_metrics; 89253427Shrsstatic u_long rtm_inits; 90253427Shrsstatic uid_t uid; 91243185Shrsstatic int defaultfib; 92243185Shrsstatic int numfibs; 93196527Scharnier 94204406Suqsstatic int atalk_aton(const char *, struct at_addr *); 95204406Suqsstatic char *atalk_ntoa(struct at_addr); 96253427Shrsstatic void printb(int, const char *); 97204406Suqsstatic void flushroutes(int argc, char *argv[]); 98243185Shrsstatic int flushroutes_fib(int); 99245168Shrsstatic int getaddr(int, char *, struct hostent **, int); 100204406Suqsstatic int keyword(const char *); 101253427Shrs#ifdef INET 102253427Shrsstatic void inet_makenetandmask(u_long, struct sockaddr_in *, 103253427Shrs struct sockaddr_in *, u_long); 104253427Shrs#endif 10597062Sume#ifdef INET6 106253427Shrsstatic int inet6_makenetandmask(struct sockaddr_in6 *, const char *); 10797062Sume#endif 108204406Suqsstatic void interfaces(void); 109243185Shrsstatic void monitor(int, char*[]); 110204406Suqsstatic const char *netname(struct sockaddr *); 111204406Suqsstatic void newroute(int, char **); 112243185Shrsstatic int newroute_fib(int, char *, int); 113216297Sglebiusstatic void pmsg_addrs(char *, int, size_t); 114216297Sglebiusstatic void pmsg_common(struct rt_msghdr *, size_t); 115204406Suqsstatic int prefixlen(const char *); 116243185Shrsstatic void print_getmsg(struct rt_msghdr *, int, int); 117216297Sglebiusstatic void print_rtmsg(struct rt_msghdr *, size_t); 118204406Suqsstatic const char *routename(struct sockaddr *); 119243185Shrsstatic int rtmsg(int, int, int); 120204406Suqsstatic void set_metric(char *, int); 121243185Shrsstatic int set_sofib(int); 122253427Shrsstatic void sockaddr(char *, struct sockaddr *, size_t); 123253427Shrsstatic void sodump(struct sockaddr *, const char *); 1241558Srgrimes 125243185Shrsstruct fibl { 126243185Shrs TAILQ_ENTRY(fibl) fl_next; 127243185Shrs 128243185Shrs int fl_num; 129243185Shrs int fl_error; 130243185Shrs int fl_errno; 131243185Shrs}; 132253427Shrsstatic TAILQ_HEAD(fibl_head_t, fibl) fibl_head; 133243185Shrs 134243185Shrsstatic int fiboptlist_csv(const char *, struct fibl_head_t *); 135243185Shrsstatic int fiboptlist_range(const char *, struct fibl_head_t *); 136243185Shrs 137204406Suqsstatic void usage(const char *) __dead2; 13813171Swollman 139253519Shrsstatic void 140196527Scharnierusage(const char *cp) 1411558Srgrimes{ 142204406Suqs if (cp != NULL) 14313171Swollman warnx("bad keyword: %s", cp); 144253427Shrs errx(EX_USAGE, "usage: route [-dnqtv] command [[modifiers] args]"); 1451558Srgrimes /* NOTREACHED */ 1461558Srgrimes} 1471558Srgrimes 1481558Srgrimesint 149196527Scharniermain(int argc, char **argv) 1501558Srgrimes{ 1511558Srgrimes int ch; 152243185Shrs size_t len; 1531558Srgrimes 1541558Srgrimes if (argc < 2) 155204406Suqs usage(NULL); 1561558Srgrimes 15737907Scharnier while ((ch = getopt(argc, argv, "nqdtv")) != -1) 1581558Srgrimes switch(ch) { 1591558Srgrimes case 'n': 1601558Srgrimes nflag = 1; 1611558Srgrimes break; 1621558Srgrimes case 'q': 1631558Srgrimes qflag = 1; 1641558Srgrimes break; 1651558Srgrimes case 'v': 1661558Srgrimes verbose = 1; 1671558Srgrimes break; 1681558Srgrimes case 't': 1691558Srgrimes tflag = 1; 1701558Srgrimes break; 1711558Srgrimes case 'd': 1721558Srgrimes debugonly = 1; 1731558Srgrimes break; 1741558Srgrimes case '?': 1751558Srgrimes default: 176204406Suqs usage(NULL); 1771558Srgrimes } 1781558Srgrimes argc -= optind; 1791558Srgrimes argv += optind; 1801558Srgrimes 1811558Srgrimes pid = getpid(); 182109811Skbyanc uid = geteuid(); 1831558Srgrimes if (tflag) 18469793Sobrien s = open(_PATH_DEVNULL, O_WRONLY, 0); 1851558Srgrimes else 1861558Srgrimes s = socket(PF_ROUTE, SOCK_RAW, 0); 1871558Srgrimes if (s < 0) 18813171Swollman err(EX_OSERR, "socket"); 189243185Shrs 190243185Shrs len = sizeof(numfibs); 191243185Shrs if (sysctlbyname("net.fibs", (void *)&numfibs, &len, NULL, 0) == -1) 192243185Shrs numfibs = -1; 193243185Shrs 194243185Shrs len = sizeof(defaultfib); 195243185Shrs if (numfibs != -1 && 196243185Shrs sysctlbyname("net.my_fibnum", (void *)&defaultfib, &len, NULL, 197243185Shrs 0) == -1) 198243185Shrs defaultfib = -1; 199243185Shrs 200204406Suqs if (*argv != NULL) 2011558Srgrimes switch (keyword(*argv)) { 2021558Srgrimes case K_GET: 203191080Skmacy case K_SHOW: 2041558Srgrimes uid = 0; 2051558Srgrimes /* FALLTHROUGH */ 2061558Srgrimes 2071558Srgrimes case K_CHANGE: 2081558Srgrimes case K_ADD: 209150679Stobez case K_DEL: 2101558Srgrimes case K_DELETE: 2111558Srgrimes newroute(argc, argv); 2121558Srgrimes /* NOTREACHED */ 2131558Srgrimes 2141558Srgrimes case K_MONITOR: 215243185Shrs monitor(argc, argv); 2161558Srgrimes /* NOTREACHED */ 2171558Srgrimes 2181558Srgrimes case K_FLUSH: 2191558Srgrimes flushroutes(argc, argv); 2201558Srgrimes exit(0); 2211558Srgrimes /* NOTREACHED */ 2221558Srgrimes } 2231558Srgrimes usage(*argv); 2241558Srgrimes /* NOTREACHED */ 2251558Srgrimes} 2261558Srgrimes 227243185Shrsstatic int 228243185Shrsset_sofib(int fib) 229243185Shrs{ 230243185Shrs 231243185Shrs if (fib < 0) 232243185Shrs return (0); 233243185Shrs return (setsockopt(s, SOL_SOCKET, SO_SETFIB, (void *)&fib, 234243185Shrs sizeof(fib))); 235243185Shrs} 236243185Shrs 237243185Shrsstatic int 238243185Shrsfiboptlist_range(const char *arg, struct fibl_head_t *flh) 239243185Shrs{ 240243185Shrs struct fibl *fl; 241244325Shrs char *str0, *str, *token, *endptr; 242243185Shrs int fib[2], i, error; 243243185Shrs 244244325Shrs str0 = str = strdup(arg); 245243185Shrs error = 0; 246243185Shrs i = 0; 247243185Shrs while ((token = strsep(&str, "-")) != NULL) { 248243185Shrs switch (i) { 249243185Shrs case 0: 250243185Shrs case 1: 251244325Shrs errno = 0; 252243185Shrs fib[i] = strtol(token, &endptr, 0); 253244325Shrs if (errno == 0) { 254244325Shrs if (*endptr != '\0' || 255244325Shrs fib[i] < 0 || 256244325Shrs (numfibs != -1 && fib[i] > numfibs - 1)) 257244325Shrs errno = EINVAL; 258244325Shrs } 259244325Shrs if (errno) 260243185Shrs error = 1; 261243185Shrs break; 262243185Shrs default: 263243185Shrs error = 1; 264243185Shrs } 265243185Shrs if (error) 266243185Shrs goto fiboptlist_range_ret; 267243185Shrs i++; 268243185Shrs } 269243185Shrs if (fib[0] >= fib[1]) { 270243185Shrs error = 1; 271243185Shrs goto fiboptlist_range_ret; 272243185Shrs } 273243185Shrs for (i = fib[0]; i <= fib[1]; i++) { 274243185Shrs fl = calloc(1, sizeof(*fl)); 275243185Shrs if (fl == NULL) { 276243185Shrs error = 1; 277243185Shrs goto fiboptlist_range_ret; 278243185Shrs } 279243185Shrs fl->fl_num = i; 280243185Shrs TAILQ_INSERT_TAIL(flh, fl, fl_next); 281243185Shrs } 282243185Shrsfiboptlist_range_ret: 283244325Shrs free(str0); 284243185Shrs return (error); 285243185Shrs} 286243185Shrs 287243185Shrs#define ALLSTRLEN 64 288243185Shrsstatic int 289243185Shrsfiboptlist_csv(const char *arg, struct fibl_head_t *flh) 290243185Shrs{ 291243185Shrs struct fibl *fl; 292244325Shrs char *str0, *str, *token, *endptr; 293243185Shrs int fib, error; 294243185Shrs 295253427Shrs str0 = str = NULL; 296243185Shrs if (strcmp("all", arg) == 0) { 297243185Shrs str = calloc(1, ALLSTRLEN); 298243185Shrs if (str == NULL) { 299243185Shrs error = 1; 300243185Shrs goto fiboptlist_csv_ret; 301243185Shrs } 302243185Shrs if (numfibs > 1) 303243185Shrs snprintf(str, ALLSTRLEN - 1, "%d-%d", 0, numfibs - 1); 304243185Shrs else 305243185Shrs snprintf(str, ALLSTRLEN - 1, "%d", 0); 306243185Shrs } else if (strcmp("default", arg) == 0) { 307244325Shrs str0 = str = calloc(1, ALLSTRLEN); 308243185Shrs if (str == NULL) { 309243185Shrs error = 1; 310243185Shrs goto fiboptlist_csv_ret; 311243185Shrs } 312243185Shrs snprintf(str, ALLSTRLEN - 1, "%d", defaultfib); 313243185Shrs } else 314244325Shrs str0 = str = strdup(arg); 315243185Shrs 316243185Shrs error = 0; 317243185Shrs while ((token = strsep(&str, ",")) != NULL) { 318243185Shrs if (*token != '-' && strchr(token, '-') != NULL) { 319243185Shrs error = fiboptlist_range(token, flh); 320243185Shrs if (error) 321243185Shrs goto fiboptlist_csv_ret; 322243185Shrs } else { 323244325Shrs errno = 0; 324243185Shrs fib = strtol(token, &endptr, 0); 325244325Shrs if (errno == 0) { 326244325Shrs if (*endptr != '\0' || 327244325Shrs fib < 0 || 328244325Shrs (numfibs != -1 && fib > numfibs - 1)) 329244325Shrs errno = EINVAL; 330244325Shrs } 331244325Shrs if (errno) { 332243185Shrs error = 1; 333243185Shrs goto fiboptlist_csv_ret; 334243185Shrs } 335243185Shrs fl = calloc(1, sizeof(*fl)); 336243185Shrs if (fl == NULL) { 337243185Shrs error = 1; 338243185Shrs goto fiboptlist_csv_ret; 339243185Shrs } 340243185Shrs fl->fl_num = fib; 341243185Shrs TAILQ_INSERT_TAIL(flh, fl, fl_next); 342243185Shrs } 343243185Shrs } 344243185Shrsfiboptlist_csv_ret: 345253427Shrs if (str0 != NULL) 346253427Shrs free(str0); 347243185Shrs return (error); 348243185Shrs} 349243185Shrs 3501558Srgrimes/* 3511558Srgrimes * Purge all entries in the routing tables not 3521558Srgrimes * associated with network interfaces. 3531558Srgrimes */ 354204406Suqsstatic void 355196527Scharnierflushroutes(int argc, char *argv[]) 3561558Srgrimes{ 357243185Shrs struct fibl *fl; 358243185Shrs int error; 3591558Srgrimes 360253427Shrs if (uid != 0 && !debugonly && !tflag) 36113171Swollman errx(EX_NOPERM, "must be root to alter routing table"); 362146079Sjmallett shutdown(s, SHUT_RD); /* Don't want to read back our messages */ 363243185Shrs 364243185Shrs TAILQ_INIT(&fibl_head); 365243185Shrs while (argc > 1) { 366243185Shrs argc--; 3671558Srgrimes argv++; 368243185Shrs if (**argv != '-') 369243185Shrs usage(*argv); 370243185Shrs switch (keyword(*argv + 1)) { 371253427Shrs#ifdef INET 372243185Shrs case K_INET: 373243185Shrs af = AF_INET; 374243185Shrs break; 375253427Shrs#endif 37654263Sshin#ifdef INET6 377243185Shrs case K_INET6: 378243185Shrs af = AF_INET6; 379243185Shrs break; 38054263Sshin#endif 381243185Shrs case K_ATALK: 382243185Shrs af = AF_APPLETALK; 383243185Shrs break; 384243185Shrs case K_LINK: 385243185Shrs af = AF_LINK; 386243185Shrs break; 387243185Shrs case K_FIB: 388243185Shrs if (!--argc) 389243185Shrs usage(*argv); 390243185Shrs error = fiboptlist_csv(*++argv, &fibl_head); 391243185Shrs if (error) 392244325Shrs errx(EX_USAGE, "invalid fib number: %s", *argv); 393243185Shrs break; 394243185Shrs default: 395243185Shrs usage(*argv); 396243185Shrs } 3971558Srgrimes } 398243185Shrs if (TAILQ_EMPTY(&fibl_head)) { 399243185Shrs error = fiboptlist_csv("default", &fibl_head); 400243185Shrs if (error) 401243185Shrs errx(EX_OSERR, "fiboptlist_csv failed."); 402243185Shrs } 403243185Shrs TAILQ_FOREACH(fl, &fibl_head, fl_next) 404243185Shrs flushroutes_fib(fl->fl_num); 405243185Shrs} 406243185Shrs 407243185Shrsstatic int 408243185Shrsflushroutes_fib(int fib) 409243185Shrs{ 410243185Shrs struct rt_msghdr *rtm; 411243185Shrs size_t needed; 412243185Shrs char *buf, *next, *lim; 413253429Shrs int mib[7], rlen, seqno, count = 0; 414243185Shrs int error; 415243185Shrs 416243185Shrs error = set_sofib(fib); 417243185Shrs if (error) { 418243185Shrs warn("fib number %d is ignored", fib); 419243185Shrs return (error); 420243185Shrs } 421243185Shrs 422128782Sambriskoretry: 4231558Srgrimes mib[0] = CTL_NET; 4241558Srgrimes mib[1] = PF_ROUTE; 4251558Srgrimes mib[2] = 0; /* protocol */ 426253427Shrs mib[3] = AF_UNSPEC; 4271558Srgrimes mib[4] = NET_RT_DUMP; 4281558Srgrimes mib[5] = 0; /* no flags */ 429253429Shrs mib[6] = fib; 430253427Shrs if (sysctl(mib, nitems(mib), NULL, &needed, NULL, 0) < 0) 43113171Swollman err(EX_OSERR, "route-sysctl-estimate"); 4321558Srgrimes if ((buf = malloc(needed)) == NULL) 43337907Scharnier errx(EX_OSERR, "malloc failed"); 434253427Shrs if (sysctl(mib, nitems(mib), buf, &needed, NULL, 0) < 0) { 435128782Sambrisko if (errno == ENOMEM && count++ < 10) { 436204406Suqs warnx("Routing table grew, retrying"); 437128782Sambrisko sleep(1); 438128782Sambrisko free(buf); 439128782Sambrisko goto retry; 440128782Sambrisko } 44113171Swollman err(EX_OSERR, "route-sysctl-get"); 442128782Sambrisko } 4431558Srgrimes lim = buf + needed; 4441558Srgrimes if (verbose) 445253427Shrs (void)printf("Examining routing table from sysctl\n"); 4461558Srgrimes seqno = 0; /* ??? */ 4471558Srgrimes for (next = buf; next < lim; next += rtm->rtm_msglen) { 448253502Shrs rtm = (struct rt_msghdr *)(void *)next; 4491558Srgrimes if (verbose) 4501558Srgrimes print_rtmsg(rtm, rtm->rtm_msglen); 4511558Srgrimes if ((rtm->rtm_flags & RTF_GATEWAY) == 0) 4521558Srgrimes continue; 453204406Suqs if (af != 0) { 4541558Srgrimes struct sockaddr *sa = (struct sockaddr *)(rtm + 1); 4551558Srgrimes 4561558Srgrimes if (sa->sa_family != af) 4571558Srgrimes continue; 4581558Srgrimes } 4591558Srgrimes if (debugonly) 4601558Srgrimes continue; 4611558Srgrimes rtm->rtm_type = RTM_DELETE; 4621558Srgrimes rtm->rtm_seq = seqno; 4631558Srgrimes rlen = write(s, next, rtm->rtm_msglen); 464129034Scsjp if (rlen < 0 && errno == EPERM) 465129034Scsjp err(1, "write to routing socket"); 4661558Srgrimes if (rlen < (int)rtm->rtm_msglen) { 46713171Swollman warn("write to routing socket"); 468253427Shrs (void)printf("got only %d for rlen\n", rlen); 469128782Sambrisko free(buf); 470128782Sambrisko goto retry; 4711558Srgrimes break; 4721558Srgrimes } 4731558Srgrimes seqno++; 4741558Srgrimes if (qflag) 4751558Srgrimes continue; 4761558Srgrimes if (verbose) 4771558Srgrimes print_rtmsg(rtm, rlen); 4781558Srgrimes else { 4791558Srgrimes struct sockaddr *sa = (struct sockaddr *)(rtm + 1); 480243185Shrs 481243185Shrs printf("%-20.20s ", rtm->rtm_flags & RTF_HOST ? 4821558Srgrimes routename(sa) : netname(sa)); 483128186Sluigi sa = (struct sockaddr *)(SA_SIZE(sa) + (char *)sa); 484243185Shrs printf("%-20.20s ", routename(sa)); 485243185Shrs if (fib >= 0) 486243185Shrs printf("-fib %-3d ", fib); 487243185Shrs printf("done\n"); 4881558Srgrimes } 4891558Srgrimes } 490243185Shrs return (error); 4911558Srgrimes} 4921558Srgrimes 493253519Shrsstatic const char * 494196527Scharnierroutename(struct sockaddr *sa) 4951558Srgrimes{ 496253517Shrs struct sockaddr_dl *sdl; 497204406Suqs const char *cp; 498253519Shrs static char line[NI_MAXHOST]; 4991558Srgrimes static char domain[MAXHOSTNAMELEN + 1]; 500258905Seadler static int first = 1; 501258905Seadler int n; 5021558Srgrimes 5031558Srgrimes if (first) { 5041558Srgrimes first = 0; 5051558Srgrimes if (gethostname(domain, MAXHOSTNAMELEN) == 0 && 50685048Sru (cp = strchr(domain, '.'))) { 50719209Sfenner domain[MAXHOSTNAMELEN] = '\0'; 508253427Shrs (void)strcpy(domain, cp + 1); 50919209Sfenner } else 510253427Shrs domain[0] = '\0'; 5111558Srgrimes } 5121558Srgrimes 513253519Shrs /* If the address is zero-filled, use "default". */ 514253503Shrs if (sa->sa_len == 0 && nflag == 0) 515253503Shrs return ("default"); 516253519Shrs#if defined(INET) || defined(INET6) 517253427Shrs switch (sa->sa_family) { 518253427Shrs#ifdef INET 519253427Shrs case AF_INET: 520253519Shrs /* If the address is zero-filled, use "default". */ 521253519Shrs if (nflag == 0 && 522253519Shrs ((struct sockaddr_in *)(void *)sa)->sin_addr.s_addr == 523253519Shrs INADDR_ANY) 524253519Shrs return("default"); 5251558Srgrimes break; 526253519Shrs#endif 527253519Shrs#ifdef INET6 528253519Shrs case AF_INET6: 529253519Shrs /* If the address is zero-filled, use "default". */ 530253519Shrs if (nflag == 0 && 531253519Shrs IN6_IS_ADDR_UNSPECIFIED(&((struct sockaddr_in6 *)(void *)sa)->sin6_addr)) 532253519Shrs return("default"); 533253519Shrs break; 534253519Shrs#endif 535253427Shrs } 536253519Shrs#endif 5371558Srgrimes 538253519Shrs switch (sa->sa_family) { 539253519Shrs#if defined(INET) || defined(INET6) 540253519Shrs#ifdef INET 541253519Shrs case AF_INET: 542253427Shrs#endif 54354263Sshin#ifdef INET6 54454263Sshin case AF_INET6: 545253519Shrs#endif 54678064Sume { 547253519Shrs struct sockaddr_storage ss; 548253519Shrs int error; 549253519Shrs char *p; 55054263Sshin 551253519Shrs memset(&ss, 0, sizeof(ss)); 552253519Shrs if (sa->sa_len == 0) 553253519Shrs ss.ss_family = sa->sa_family; 554253519Shrs else 555253519Shrs memcpy(&ss, sa, sa->sa_len); 556253519Shrs /* Expand sa->sa_len because it could be shortened. */ 557253519Shrs if (sa->sa_family == AF_INET) 558253519Shrs ss.ss_len = sizeof(struct sockaddr_in); 559253519Shrs else if (sa->sa_family == AF_INET6) 560253519Shrs ss.ss_len = sizeof(struct sockaddr_in6); 561253519Shrs error = getnameinfo((struct sockaddr *)&ss, ss.ss_len, 562253519Shrs line, sizeof(line), NULL, 0, 563253519Shrs (nflag == 0) ? 0 : NI_NUMERICHOST); 564253519Shrs if (error) { 565253519Shrs warnx("getnameinfo(): %s", gai_strerror(error)); 56678064Sume strncpy(line, "invalid", sizeof(line)); 567253519Shrs } 56878064Sume 569253519Shrs /* Remove the domain part if any. */ 570253519Shrs p = strchr(line, '.'); 571253519Shrs if (p != NULL && strcmp(p + 1, domain) == 0) 572253519Shrs *p = '\0'; 573253519Shrs 574253427Shrs return (line); 575253519Shrs break; 57678064Sume } 57778064Sume#endif 57817046Sjulian case AF_APPLETALK: 579253427Shrs (void)snprintf(line, sizeof(line), "atalk %s", 580253502Shrs atalk_ntoa(((struct sockaddr_at *)(void *)sa)->sat_addr)); 58117046Sjulian break; 58217046Sjulian 5831558Srgrimes case AF_LINK: 584253517Shrs sdl = (struct sockaddr_dl *)(void *)sa; 585253517Shrs 586253517Shrs if (sdl->sdl_nlen == 0 && 587253517Shrs sdl->sdl_alen == 0 && 588253517Shrs sdl->sdl_slen == 0) { 589253517Shrs n = snprintf(line, sizeof(line), "link#%d", 590253517Shrs sdl->sdl_index); 591253517Shrs if (n > (int)sizeof(line)) 592253517Shrs line[0] = '\0'; 593253517Shrs return (line); 594253517Shrs } else 595253517Shrs return (link_ntoa(sdl)); 596253427Shrs break; 5971558Srgrimes 5981558Srgrimes default: 599204406Suqs { 600253502Shrs u_short *sp = (u_short *)(void *)sa; 601204406Suqs u_short *splim = sp + ((sa->sa_len + 1) >> 1); 602204406Suqs char *cps = line + sprintf(line, "(%d)", sa->sa_family); 60319209Sfenner char *cpe = line + sizeof(line); 6041558Srgrimes 605204406Suqs while (++sp < splim && cps < cpe) /* start with sa->sa_data */ 606204406Suqs if ((n = snprintf(cps, cpe - cps, " %x", *sp)) > 0) 607204406Suqs cps += n; 60881980Sbrian else 609204406Suqs *cps = '\0'; 6101558Srgrimes break; 6111558Srgrimes } 6121558Srgrimes } 6131558Srgrimes return (line); 6141558Srgrimes} 6151558Srgrimes 6161558Srgrimes/* 6171558Srgrimes * Return the name of the network whose address is given. 618243019Sglebius * The address is assumed to be that of a net, not a host. 6191558Srgrimes */ 620253519Shrsstatic const char * 621196527Scharniernetname(struct sockaddr *sa) 6221558Srgrimes{ 623253517Shrs struct sockaddr_dl *sdl; 62419209Sfenner static char line[MAXHOSTNAMELEN + 1]; 625253427Shrs int n; 626253427Shrs#ifdef INET 627204406Suqs struct netent *np = NULL; 628253427Shrs const char *cp = NULL; 62992806Sobrien u_long i; 630253427Shrs#endif 6311558Srgrimes 6321558Srgrimes switch (sa->sa_family) { 633253427Shrs#ifdef INET 634253427Shrs case AF_INET: 635253427Shrs { 636253427Shrs struct in_addr in; 6371558Srgrimes 638253502Shrs in = ((struct sockaddr_in *)(void *)sa)->sin_addr; 6391558Srgrimes i = in.s_addr = ntohl(in.s_addr); 6401558Srgrimes if (in.s_addr == 0) 6411558Srgrimes cp = "default"; 6421558Srgrimes else if (!nflag) { 643243019Sglebius np = getnetbyaddr(i, AF_INET); 644204406Suqs if (np != NULL) 6451558Srgrimes cp = np->n_name; 6461558Srgrimes } 64777873Sru#define C(x) (unsigned)((x) & 0xff) 648204406Suqs if (cp != NULL) 64919209Sfenner strncpy(line, cp, sizeof(line)); 6501558Srgrimes else if ((in.s_addr & 0xffffff) == 0) 651253427Shrs (void)sprintf(line, "%u", C(in.s_addr >> 24)); 6521558Srgrimes else if ((in.s_addr & 0xffff) == 0) 653253427Shrs (void)sprintf(line, "%u.%u", C(in.s_addr >> 24), 6541558Srgrimes C(in.s_addr >> 16)); 6551558Srgrimes else if ((in.s_addr & 0xff) == 0) 656253427Shrs (void)sprintf(line, "%u.%u.%u", C(in.s_addr >> 24), 6571558Srgrimes C(in.s_addr >> 16), C(in.s_addr >> 8)); 6581558Srgrimes else 659253427Shrs (void)sprintf(line, "%u.%u.%u.%u", C(in.s_addr >> 24), 6601558Srgrimes C(in.s_addr >> 16), C(in.s_addr >> 8), 6611558Srgrimes C(in.s_addr)); 66277873Sru#undef C 6631558Srgrimes break; 664253427Shrs } 665253427Shrs#endif 66654263Sshin#ifdef INET6 66754263Sshin case AF_INET6: 66878064Sume { 669253427Shrs struct sockaddr_in6 sin6; 67078064Sume int niflags = 0; 67154263Sshin 67278064Sume memset(&sin6, 0, sizeof(sin6)); 67378064Sume memcpy(&sin6, sa, sa->sa_len); 674253427Shrs sin6.sin6_len = sizeof(sin6); 67578064Sume sin6.sin6_family = AF_INET6; 67678064Sume if (nflag) 67778064Sume niflags |= NI_NUMERICHOST; 67878064Sume if (getnameinfo((struct sockaddr *)&sin6, sin6.sin6_len, 67978064Sume line, sizeof(line), NULL, 0, niflags) != 0) 68078064Sume strncpy(line, "invalid", sizeof(line)); 68178064Sume 68278064Sume return(line); 68378064Sume } 68478064Sume#endif 68578064Sume 68617046Sjulian case AF_APPLETALK: 687253427Shrs (void)snprintf(line, sizeof(line), "atalk %s", 688253502Shrs atalk_ntoa(((struct sockaddr_at *)(void *)sa)->sat_addr)); 68917046Sjulian break; 69017046Sjulian 6911558Srgrimes case AF_LINK: 692253517Shrs sdl = (struct sockaddr_dl *)(void *)sa; 693253517Shrs 694253517Shrs if (sdl->sdl_nlen == 0 && 695253517Shrs sdl->sdl_alen == 0 && 696253517Shrs sdl->sdl_slen == 0) { 697253517Shrs n = snprintf(line, sizeof(line), "link#%d", 698253517Shrs sdl->sdl_index); 699253517Shrs if (n > (int)sizeof(line)) 700253517Shrs line[0] = '\0'; 701253517Shrs return (line); 702253517Shrs } else 703253517Shrs return (link_ntoa(sdl)); 704253427Shrs break; 7051558Srgrimes 7061558Srgrimes default: 707204406Suqs { 708253502Shrs u_short *sp = (u_short *)(void *)sa->sa_data; 709204406Suqs u_short *splim = sp + ((sa->sa_len + 1)>>1); 710204406Suqs char *cps = line + sprintf(line, "af %d:", sa->sa_family); 71119209Sfenner char *cpe = line + sizeof(line); 7121558Srgrimes 713204406Suqs while (sp < splim && cps < cpe) 714204406Suqs if ((n = snprintf(cps, cpe - cps, " %x", *sp++)) > 0) 715204406Suqs cps += n; 71681980Sbrian else 717204406Suqs *cps = '\0'; 7181558Srgrimes break; 7191558Srgrimes } 7201558Srgrimes } 7211558Srgrimes return (line); 7221558Srgrimes} 7231558Srgrimes 724204406Suqsstatic void 725196527Scharnierset_metric(char *value, int key) 7261558Srgrimes{ 7271558Srgrimes int flag = 0; 728256695Shrs char *endptr; 7291558Srgrimes u_long noval, *valp = &noval; 7301558Srgrimes 7311558Srgrimes switch (key) { 7321558Srgrimes#define caseof(x, y, z) case x: valp = &rt_metrics.z; flag = y; break 7331558Srgrimes caseof(K_MTU, RTV_MTU, rmx_mtu); 7341558Srgrimes caseof(K_HOPCOUNT, RTV_HOPCOUNT, rmx_hopcount); 7351558Srgrimes caseof(K_EXPIRE, RTV_EXPIRE, rmx_expire); 7361558Srgrimes caseof(K_RECVPIPE, RTV_RPIPE, rmx_recvpipe); 7371558Srgrimes caseof(K_SENDPIPE, RTV_SPIPE, rmx_sendpipe); 7381558Srgrimes caseof(K_SSTHRESH, RTV_SSTHRESH, rmx_ssthresh); 7391558Srgrimes caseof(K_RTT, RTV_RTT, rmx_rtt); 7401558Srgrimes caseof(K_RTTVAR, RTV_RTTVAR, rmx_rttvar); 741191080Skmacy caseof(K_WEIGHT, RTV_WEIGHT, rmx_weight); 7421558Srgrimes } 7431558Srgrimes rtm_inits |= flag; 7441558Srgrimes if (lockrest || locking) 7451558Srgrimes rt_metrics.rmx_locks |= flag; 7461558Srgrimes if (locking) 7471558Srgrimes locking = 0; 748256695Shrs errno = 0; 749256695Shrs *valp = strtol(value, &endptr, 0); 750256695Shrs if (errno == 0 && *endptr != '\0') 751256695Shrs errno = EINVAL; 752256695Shrs if (errno) 753256695Shrs err(EX_USAGE, "%s", value); 754256695Shrs if (flag & RTV_EXPIRE && (value[0] == '+' || value[0] == '-')) { 755256695Shrs struct timespec ts; 756256695Shrs 757256695Shrs clock_gettime(CLOCK_REALTIME_FAST, &ts); 758256695Shrs *valp += ts.tv_sec; 759256695Shrs } 7601558Srgrimes} 7611558Srgrimes 762243185Shrs#define F_ISHOST 0x01 763243185Shrs#define F_FORCENET 0x02 764243185Shrs#define F_FORCEHOST 0x04 765243185Shrs#define F_PROXY 0x08 766243185Shrs#define F_INTERFACE 0x10 767243185Shrs 768204406Suqsstatic void 769196527Scharniernewroute(int argc, char **argv) 7701558Srgrimes{ 771243185Shrs struct hostent *hp; 772243185Shrs struct fibl *fl; 773204406Suqs char *cmd; 774243185Shrs const char *dest, *gateway, *errmsg; 775243185Shrs int key, error, flags, nrflags, fibnum; 7761558Srgrimes 777253427Shrs if (uid != 0 && !debugonly && !tflag) 77813171Swollman errx(EX_NOPERM, "must be root to alter routing table"); 779243185Shrs dest = NULL; 780243185Shrs gateway = NULL; 781243185Shrs flags = RTF_STATIC; 782243185Shrs nrflags = 0; 783243185Shrs hp = NULL; 784243185Shrs TAILQ_INIT(&fibl_head); 785243185Shrs 7861558Srgrimes cmd = argv[0]; 787191080Skmacy if (*cmd != 'g' && *cmd != 's') 788146079Sjmallett shutdown(s, SHUT_RD); /* Don't want to read back our messages */ 7891558Srgrimes while (--argc > 0) { 7901558Srgrimes if (**(++argv)== '-') { 7911558Srgrimes switch (key = keyword(1 + *argv)) { 7921558Srgrimes case K_LINK: 7931558Srgrimes af = AF_LINK; 7941558Srgrimes aflen = sizeof(struct sockaddr_dl); 7951558Srgrimes break; 796253427Shrs#ifdef INET 7971558Srgrimes case K_INET: 7981558Srgrimes af = AF_INET; 7991558Srgrimes aflen = sizeof(struct sockaddr_in); 8001558Srgrimes break; 801253427Shrs#endif 80254263Sshin#ifdef INET6 80354263Sshin case K_INET6: 80454263Sshin af = AF_INET6; 80554263Sshin aflen = sizeof(struct sockaddr_in6); 80654263Sshin break; 80754263Sshin#endif 80817046Sjulian case K_ATALK: 80917046Sjulian af = AF_APPLETALK; 81017046Sjulian aflen = sizeof(struct sockaddr_at); 81117046Sjulian break; 8121558Srgrimes case K_SA: 8131558Srgrimes af = PF_ROUTE; 814253427Shrs aflen = sizeof(struct sockaddr_storage); 8151558Srgrimes break; 8161558Srgrimes case K_IFACE: 8171558Srgrimes case K_INTERFACE: 818243185Shrs nrflags |= F_INTERFACE; 8192787Spst break; 8201558Srgrimes case K_NOSTATIC: 8211558Srgrimes flags &= ~RTF_STATIC; 8221558Srgrimes break; 8231558Srgrimes case K_LOCK: 8241558Srgrimes locking = 1; 8251558Srgrimes break; 8261558Srgrimes case K_LOCKREST: 8271558Srgrimes lockrest = 1; 8281558Srgrimes break; 8291558Srgrimes case K_HOST: 830243185Shrs nrflags |= F_FORCEHOST; 8311558Srgrimes break; 8321558Srgrimes case K_REJECT: 8331558Srgrimes flags |= RTF_REJECT; 8341558Srgrimes break; 8351558Srgrimes case K_BLACKHOLE: 8361558Srgrimes flags |= RTF_BLACKHOLE; 8371558Srgrimes break; 8381558Srgrimes case K_PROTO1: 8391558Srgrimes flags |= RTF_PROTO1; 8401558Srgrimes break; 8411558Srgrimes case K_PROTO2: 8421558Srgrimes flags |= RTF_PROTO2; 8431558Srgrimes break; 844256695Shrs case K_PROTO3: 845256695Shrs flags |= RTF_PROTO3; 846256695Shrs break; 84778140Sru case K_PROXY: 848243185Shrs nrflags |= F_PROXY; 84978140Sru break; 8501558Srgrimes case K_XRESOLVE: 8511558Srgrimes flags |= RTF_XRESOLVE; 8521558Srgrimes break; 8531558Srgrimes case K_STATIC: 8541558Srgrimes flags |= RTF_STATIC; 8551558Srgrimes break; 856191080Skmacy case K_STICKY: 857191080Skmacy flags |= RTF_STICKY; 858191080Skmacy break; 859191080Skmacy case K_NOSTICK: 860191080Skmacy flags &= ~RTF_STICKY; 861191080Skmacy break; 862243185Shrs case K_FIB: 863243185Shrs if (!--argc) 864243185Shrs usage(NULL); 865243185Shrs error = fiboptlist_csv(*++argv, &fibl_head); 866243185Shrs if (error) 867244325Shrs errx(EX_USAGE, 868244325Shrs "invalid fib number: %s", *argv); 869243185Shrs break; 8701558Srgrimes case K_IFA: 87147668Sru if (!--argc) 872204406Suqs usage(NULL); 873253504Shrs getaddr(RTAX_IFA, *++argv, 0, nrflags); 8741558Srgrimes break; 8751558Srgrimes case K_IFP: 87647668Sru if (!--argc) 877204406Suqs usage(NULL); 878253504Shrs getaddr(RTAX_IFP, *++argv, 0, nrflags); 8791558Srgrimes break; 8801558Srgrimes case K_GENMASK: 88147668Sru if (!--argc) 882204406Suqs usage(NULL); 883253504Shrs getaddr(RTAX_GENMASK, *++argv, 0, nrflags); 8841558Srgrimes break; 8851558Srgrimes case K_GATEWAY: 88647668Sru if (!--argc) 887204406Suqs usage(NULL); 888253504Shrs getaddr(RTAX_GATEWAY, *++argv, 0, nrflags); 889251581Shrs gateway = *argv; 8901558Srgrimes break; 8911558Srgrimes case K_DST: 89247668Sru if (!--argc) 893204406Suqs usage(NULL); 894253504Shrs if (getaddr(RTAX_DST, *++argv, &hp, nrflags)) 895243185Shrs nrflags |= F_ISHOST; 8961558Srgrimes dest = *argv; 8971558Srgrimes break; 8981558Srgrimes case K_NETMASK: 89947668Sru if (!--argc) 900204406Suqs usage(NULL); 901253504Shrs getaddr(RTAX_NETMASK, *++argv, 0, nrflags); 9021558Srgrimes /* FALLTHROUGH */ 9031558Srgrimes case K_NET: 904243185Shrs nrflags |= F_FORCENET; 9051558Srgrimes break; 90654263Sshin case K_PREFIXLEN: 90754263Sshin if (!--argc) 908204406Suqs usage(NULL); 90954263Sshin if (prefixlen(*++argv) == -1) { 910243185Shrs nrflags &= ~F_FORCENET; 911243185Shrs nrflags |= F_ISHOST; 91254263Sshin } else { 913243185Shrs nrflags |= F_FORCENET; 914243185Shrs nrflags &= ~F_ISHOST; 91554263Sshin } 91654263Sshin break; 9171558Srgrimes case K_MTU: 9181558Srgrimes case K_HOPCOUNT: 9191558Srgrimes case K_EXPIRE: 9201558Srgrimes case K_RECVPIPE: 9211558Srgrimes case K_SENDPIPE: 9221558Srgrimes case K_SSTHRESH: 9231558Srgrimes case K_RTT: 9241558Srgrimes case K_RTTVAR: 925191080Skmacy case K_WEIGHT: 92647668Sru if (!--argc) 927204406Suqs usage(NULL); 9281558Srgrimes set_metric(*++argv, key); 9291558Srgrimes break; 9301558Srgrimes default: 9311558Srgrimes usage(1+*argv); 9321558Srgrimes } 9331558Srgrimes } else { 9341558Srgrimes if ((rtm_addrs & RTA_DST) == 0) { 9351558Srgrimes dest = *argv; 936253504Shrs if (getaddr(RTAX_DST, *argv, &hp, nrflags)) 937243185Shrs nrflags |= F_ISHOST; 9381558Srgrimes } else if ((rtm_addrs & RTA_GATEWAY) == 0) { 9391558Srgrimes gateway = *argv; 940253504Shrs getaddr(RTAX_GATEWAY, *argv, &hp, nrflags); 9411558Srgrimes } else { 942253504Shrs getaddr(RTAX_NETMASK, *argv, 0, nrflags); 943243185Shrs nrflags |= F_FORCENET; 9441558Srgrimes } 9451558Srgrimes } 9461558Srgrimes } 947243185Shrs 948256137Sglebius if (so[RTAX_DST].ss_len == 0) { 949256137Sglebius warnx("destination parameter required"); 950256137Sglebius usage(NULL); 951256137Sglebius } 952256137Sglebius 953243185Shrs if (nrflags & F_FORCEHOST) { 954243185Shrs nrflags |= F_ISHOST; 95554263Sshin#ifdef INET6 95654263Sshin if (af == AF_INET6) { 95754263Sshin rtm_addrs &= ~RTA_NETMASK; 958253427Shrs memset(&so[RTAX_NETMASK], 0, sizeof(so[RTAX_NETMASK])); 95954263Sshin } 960204406Suqs#endif 96154263Sshin } 962243185Shrs if (nrflags & F_FORCENET) 963243185Shrs nrflags &= ~F_ISHOST; 9641558Srgrimes flags |= RTF_UP; 965243185Shrs if (nrflags & F_ISHOST) 9661558Srgrimes flags |= RTF_HOST; 967243185Shrs if ((nrflags & F_INTERFACE) == 0) 9681558Srgrimes flags |= RTF_GATEWAY; 969246143Sglebius if (nrflags & F_PROXY) 97078140Sru flags |= RTF_ANNOUNCE; 971243185Shrs if (dest == NULL) 972243185Shrs dest = ""; 973243185Shrs if (gateway == NULL) 974243185Shrs gateway = ""; 975243185Shrs 976243185Shrs if (TAILQ_EMPTY(&fibl_head)) { 977243185Shrs error = fiboptlist_csv("default", &fibl_head); 978243185Shrs if (error) 979243185Shrs errx(EX_OSERR, "fiboptlist_csv failed."); 9801558Srgrimes } 981243185Shrs error = 0; 982243185Shrs TAILQ_FOREACH(fl, &fibl_head, fl_next) { 983243185Shrs fl->fl_error = newroute_fib(fl->fl_num, cmd, flags); 984243185Shrs if (fl->fl_error) 985243185Shrs fl->fl_errno = errno; 986243185Shrs error += fl->fl_error; 987243185Shrs } 988191080Skmacy if (*cmd == 'g' || *cmd == 's') 989243185Shrs exit(error); 990243185Shrs 991243185Shrs error = 0; 99297278Sru if (!qflag) { 993243185Shrs fibnum = 0; 994243185Shrs TAILQ_FOREACH(fl, &fibl_head, fl_next) { 995243185Shrs if (fl->fl_error == 0) 996243185Shrs fibnum++; 9971558Srgrimes } 998243185Shrs if (fibnum > 0) { 999243185Shrs int firstfib = 1; 1000243185Shrs 1001243185Shrs printf("%s %s %s", cmd, 1002243185Shrs (nrflags & F_ISHOST) ? "host" : "net", dest); 1003243185Shrs if (*gateway) 1004243185Shrs printf(": gateway %s", gateway); 1005243185Shrs 1006243185Shrs if (numfibs > 1) { 1007243185Shrs TAILQ_FOREACH(fl, &fibl_head, fl_next) { 1008243185Shrs if (fl->fl_error == 0 1009243185Shrs && fl->fl_num >= 0) { 1010243185Shrs if (firstfib) { 1011243185Shrs printf(" fib "); 1012243185Shrs firstfib = 0; 1013243185Shrs } 1014243185Shrs printf("%d", fl->fl_num); 1015243185Shrs if (fibnum-- > 1) 1016243185Shrs printf(","); 1017243185Shrs } 1018243185Shrs } 101997278Sru } 1020243185Shrs printf("\n"); 102197278Sru } 1022243185Shrs 1023243185Shrs fibnum = 0; 1024243185Shrs TAILQ_FOREACH(fl, &fibl_head, fl_next) { 1025243185Shrs if (fl->fl_error != 0) { 1026243185Shrs printf("%s %s %s", cmd, (nrflags & F_ISHOST) 1027243185Shrs ? "host" : "net", dest); 1028243185Shrs if (*gateway) 1029243185Shrs printf(": gateway %s", gateway); 1030243185Shrs 1031243185Shrs if (fl->fl_num >= 0) 1032243185Shrs printf(" fib %d", fl->fl_num); 1033243185Shrs 1034243185Shrs switch (fl->fl_errno) { 1035243185Shrs case ESRCH: 1036243185Shrs errmsg = "not in table"; 1037243185Shrs break; 1038243185Shrs case EBUSY: 1039243185Shrs errmsg = "entry in use"; 1040243185Shrs break; 1041243185Shrs case ENOBUFS: 1042243185Shrs errmsg = "not enough memory"; 1043243185Shrs break; 1044243185Shrs case EADDRINUSE: 1045243185Shrs /* 1046243185Shrs * handle recursion avoidance 1047243185Shrs * in rt_setgate() 1048243185Shrs */ 1049243185Shrs errmsg = "gateway uses the same route"; 1050243185Shrs break; 1051243185Shrs case EEXIST: 1052243185Shrs errmsg = "route already in table"; 1053243185Shrs break; 1054243185Shrs default: 1055243185Shrs errmsg = strerror(fl->fl_errno); 1056243185Shrs break; 1057243185Shrs } 1058243185Shrs printf(": %s\n", errmsg); 1059243185Shrs error = 1; 1060243185Shrs } 1061243185Shrs } 10621558Srgrimes } 1063243185Shrs exit(error); 10641558Srgrimes} 10651558Srgrimes 1066243185Shrsstatic int 1067243185Shrsnewroute_fib(int fib, char *cmd, int flags) 1068243185Shrs{ 1069243185Shrs int error; 1070243185Shrs 1071243185Shrs error = set_sofib(fib); 1072243185Shrs if (error) { 1073243185Shrs warn("fib number %d is ignored", fib); 1074243185Shrs return (error); 1075243185Shrs } 1076243185Shrs 1077243185Shrs error = rtmsg(*cmd, flags, fib); 1078243185Shrs return (error); 1079243185Shrs} 1080243185Shrs 1081253427Shrs#ifdef INET 1082204406Suqsstatic void 1083253427Shrsinet_makenetandmask(u_long net, struct sockaddr_in *sin, 1084253427Shrs struct sockaddr_in *sin_mask, u_long bits) 10851558Srgrimes{ 1086243019Sglebius u_long mask = 0; 10871558Srgrimes 10881558Srgrimes rtm_addrs |= RTA_NETMASK; 1089243019Sglebius 1090204406Suqs /* 1091243867Sglebius * MSB of net should be meaningful. 0/0 is exception. 1092243867Sglebius */ 1093243867Sglebius if (net > 0) 1094243867Sglebius while ((net & 0xff000000) == 0) 1095243867Sglebius net <<= 8; 1096243867Sglebius 1097243867Sglebius /* 1098204406Suqs * If no /xx was specified we must calculate the 1099190758Srrs * CIDR address. 1100190758Srrs */ 1101243019Sglebius if ((bits == 0) && (net != 0)) { 1102190775Srrs u_long i, j; 1103253427Shrs 1104253427Shrs for(i = 0, j = 0xff; i < 4; i++) { 1105243019Sglebius if (net & j) { 1106190758Srrs break; 1107190758Srrs } 1108190775Srrs j <<= 8; 1109190758Srrs } 1110190758Srrs /* i holds the first non zero bit */ 1111190775Srrs bits = 32 - (i*8); 1112190758Srrs } 1113190913Srrs if (bits != 0) 1114190913Srrs mask = 0xffffffff << (32 - bits); 1115173124Smtm 1116243019Sglebius sin->sin_addr.s_addr = htonl(net); 1117253427Shrs sin_mask->sin_addr.s_addr = htonl(mask); 1118253427Shrs sin_mask->sin_len = sizeof(struct sockaddr_in); 1119253427Shrs sin_mask->sin_family = AF_INET; 11201558Srgrimes} 1121253427Shrs#endif 11221558Srgrimes 112396997Sume#ifdef INET6 11241558Srgrimes/* 112596997Sume * XXX the function may need more improvement... 112696997Sume */ 112797062Sumestatic int 1128204406Suqsinet6_makenetandmask(struct sockaddr_in6 *sin6, const char *plen) 112996997Sume{ 113096997Sume struct in6_addr in6; 113196997Sume 1132204406Suqs if (plen == NULL) { 113397073Sume if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr) && 113497073Sume sin6->sin6_scope_id == 0) { 113597073Sume plen = "0"; 113697073Sume } else if ((sin6->sin6_addr.s6_addr[0] & 0xe0) == 0x20) { 113797073Sume /* aggregatable global unicast - RFC2374 */ 113897073Sume memset(&in6, 0, sizeof(in6)); 113997073Sume if (!memcmp(&sin6->sin6_addr.s6_addr[8], 114097073Sume &in6.s6_addr[8], 8)) 114197073Sume plen = "64"; 114297073Sume } 114396997Sume } 114496997Sume 1145204406Suqs if (plen == NULL || strcmp(plen, "128") == 0) 1146204406Suqs return (1); 114798053Sume rtm_addrs |= RTA_NETMASK; 1148204406Suqs prefixlen(plen); 1149204406Suqs return (0); 115096997Sume} 115196997Sume#endif 115296997Sume 115396997Sume/* 11541558Srgrimes * Interpret an argument as a network address of some kind, 11551558Srgrimes * returning 1 if a host address, 0 if a network address. 11561558Srgrimes */ 1157204406Suqsstatic int 1158253504Shrsgetaddr(int idx, char *str, struct hostent **hpp, int nrflags) 11591558Srgrimes{ 1160253427Shrs struct sockaddr *sa; 1161253427Shrs#if defined(INET) 1162253427Shrs struct sockaddr_in *sin; 11631558Srgrimes struct hostent *hp; 11641558Srgrimes struct netent *np; 11651558Srgrimes u_long val; 116666449Sru char *q; 1167253427Shrs#elif defined(INET6) 1168253427Shrs char *q; 1169253427Shrs#endif 11701558Srgrimes 1171253852Shrs if (idx < 0 || idx >= RTAX_MAX) 1172253852Shrs usage("internal error"); 11731558Srgrimes if (af == 0) { 1174253427Shrs#if defined(INET) 11751558Srgrimes af = AF_INET; 11761558Srgrimes aflen = sizeof(struct sockaddr_in); 1177253427Shrs#elif defined(INET6) 1178253427Shrs af = AF_INET6; 1179253427Shrs aflen = sizeof(struct sockaddr_in6); 1180253427Shrs#else 1181253427Shrs af = AF_LINK; 1182253427Shrs aflen = sizeof(struct sockaddr_dl); 1183253427Shrs#endif 11841558Srgrimes } 1185253519Shrs#ifndef INET 1186253519Shrs hpp = NULL; 1187253519Shrs#endif 1188253504Shrs rtm_addrs |= (1 << idx); 1189253504Shrs sa = (struct sockaddr *)&so[idx]; 1190253427Shrs sa->sa_family = af; 1191253427Shrs sa->sa_len = aflen; 1192253427Shrs 1193253504Shrs switch (idx) { 1194253504Shrs case RTAX_GATEWAY: 1195245168Shrs if (nrflags & F_INTERFACE) { 119678064Sume struct ifaddrs *ifap, *ifa; 1197253502Shrs struct sockaddr_dl *sdl0 = (struct sockaddr_dl *)(void *)sa; 119878064Sume struct sockaddr_dl *sdl = NULL; 119917486Sjulian 120078064Sume if (getifaddrs(&ifap)) 1201253427Shrs err(EX_OSERR, "getifaddrs"); 120217486Sjulian 1203204406Suqs for (ifa = ifap; ifa != NULL; ifa = ifa->ifa_next) { 120478064Sume if (ifa->ifa_addr->sa_family != AF_LINK) 120578064Sume continue; 120617486Sjulian 1207204406Suqs if (strcmp(str, ifa->ifa_name) != 0) 120878064Sume continue; 120978064Sume 1210253502Shrs sdl = (struct sockaddr_dl *)(void *)ifa->ifa_addr; 121117486Sjulian } 121217486Sjulian /* If we found it, then use it */ 1213204406Suqs if (sdl != NULL) { 121478064Sume /* 121578064Sume * Note that we need to copy before calling 121678064Sume * freeifaddrs(). 121778064Sume */ 1218253427Shrs memcpy(sdl0, sdl, sdl->sdl_len); 121978064Sume } 122078064Sume freeifaddrs(ifap); 1221204406Suqs if (sdl != NULL) 122217486Sjulian return(1); 122317486Sjulian } 12241558Srgrimes break; 1225253504Shrs case RTAX_IFP: 1226253427Shrs sa->sa_family = AF_LINK; 12271558Srgrimes break; 12281558Srgrimes } 1229204406Suqs if (strcmp(str, "default") == 0) { 123027500Sjulian /* 1231204406Suqs * Default is net 0.0.0.0/0 123227500Sjulian */ 1233253504Shrs switch (idx) { 1234253504Shrs case RTAX_DST: 12351558Srgrimes forcenet++; 1236253504Shrs getaddr(RTAX_NETMASK, str, 0, nrflags); 12371558Srgrimes break; 12381558Srgrimes } 12391558Srgrimes return (0); 12401558Srgrimes } 1241253427Shrs switch (sa->sa_family) { 124254263Sshin#ifdef INET6 124354263Sshin case AF_INET6: 124478064Sume { 124557108Sshin struct addrinfo hints, *res; 1246146546Sume int ecode; 124757108Sshin 124897073Sume q = NULL; 1249253504Shrs if (idx == RTAX_DST && (q = strchr(str, '/')) != NULL) 125097073Sume *q = '\0'; 125178064Sume memset(&hints, 0, sizeof(hints)); 1252253427Shrs hints.ai_family = sa->sa_family; 1253253427Shrs hints.ai_socktype = SOCK_DGRAM; 1254204406Suqs ecode = getaddrinfo(str, NULL, &hints, &res); 1255146546Sume if (ecode != 0 || res->ai_family != AF_INET6 || 1256253427Shrs res->ai_addrlen != sizeof(struct sockaddr_in6)) 1257253427Shrs errx(EX_OSERR, "%s: %s", str, gai_strerror(ecode)); 1258253427Shrs memcpy(sa, res->ai_addr, res->ai_addrlen); 125997062Sume freeaddrinfo(res); 126097073Sume if (q != NULL) 126197073Sume *q++ = '/'; 1262253504Shrs if (idx == RTAX_DST) 1263253502Shrs return (inet6_makenetandmask((struct sockaddr_in6 *)(void *)sa, q)); 126478064Sume return (0); 126578064Sume } 126678064Sume#endif /* INET6 */ 126754263Sshin 126817046Sjulian case AF_APPLETALK: 1269253427Shrs { 1270253502Shrs struct sockaddr_at *sat = (struct sockaddr_at *)(void *)sa; 1271253427Shrs 1272253427Shrs if (!atalk_aton(str, &sat->sat_addr)) 1273204406Suqs errx(EX_NOHOST, "bad address: %s", str); 127417265Sjulian rtm_addrs |= RTA_NETMASK; 1275253427Shrs return(forcehost || sat->sat_addr.s_node != 0); 1276253427Shrs } 12771558Srgrimes case AF_LINK: 1278253502Shrs link_addr(str, (struct sockaddr_dl *)(void *)sa); 12791558Srgrimes return (1); 12801558Srgrimes 12811558Srgrimes case PF_ROUTE: 1282253427Shrs sockaddr(str, sa, sizeof(struct sockaddr_storage)); 12831558Srgrimes return (1); 1284253427Shrs#ifdef INET 12851558Srgrimes case AF_INET: 1286253427Shrs#endif 12871558Srgrimes default: 12881558Srgrimes break; 12891558Srgrimes } 12901558Srgrimes 1291253427Shrs#ifdef INET 1292253502Shrs sin = (struct sockaddr_in *)(void *)sa; 12931558Srgrimes if (hpp == NULL) 12941558Srgrimes hpp = &hp; 12951558Srgrimes *hpp = NULL; 129624558Sphk 1297204406Suqs q = strchr(str,'/'); 1298253504Shrs if (q != NULL && idx == RTAX_DST) { 129924558Sphk *q = '\0'; 1300204406Suqs if ((val = inet_network(str)) != INADDR_NONE) { 1301253427Shrs inet_makenetandmask(val, sin, 1302253427Shrs (struct sockaddr_in *)&so[RTAX_NETMASK], 1303253427Shrs strtoul(q+1, 0, 0)); 130424558Sphk return (0); 130524558Sphk } 130666449Sru *q = '/'; 130724558Sphk } 1308253504Shrs if ((idx != RTAX_DST || forcenet == 0) && 1309253427Shrs inet_aton(str, &sin->sin_addr)) { 1310253427Shrs val = sin->sin_addr.s_addr; 1311253504Shrs if (idx != RTAX_DST || forcehost || 1312253427Shrs inet_lnaof(sin->sin_addr) != INADDR_ANY) 13131558Srgrimes return (1); 13141558Srgrimes else { 13151558Srgrimes val = ntohl(val); 13161558Srgrimes goto netdone; 13171558Srgrimes } 13181558Srgrimes } 1319253504Shrs if (idx == RTAX_DST && forcehost == 0 && 1320204406Suqs ((val = inet_network(str)) != INADDR_NONE || 1321204406Suqs ((np = getnetbyname(str)) != NULL && (val = np->n_net) != 0))) { 13221558Srgrimesnetdone: 1323253427Shrs inet_makenetandmask(val, sin, 1324253427Shrs (struct sockaddr_in *)&so[RTAX_NETMASK], 0); 13251558Srgrimes return (0); 13261558Srgrimes } 1327204406Suqs hp = gethostbyname(str); 1328204406Suqs if (hp != NULL) { 13291558Srgrimes *hpp = hp; 1330253427Shrs sin->sin_family = hp->h_addrtype; 1331253427Shrs memmove((char *)&sin->sin_addr, hp->h_addr, 1332253427Shrs MIN((size_t)hp->h_length, sizeof(sin->sin_addr))); 13331558Srgrimes return (1); 13341558Srgrimes } 1335253427Shrs#endif 1336204406Suqs errx(EX_NOHOST, "bad address: %s", str); 13371558Srgrimes} 13381558Srgrimes 1339204406Suqsstatic int 1340204406Suqsprefixlen(const char *str) 134154263Sshin{ 1342204406Suqs int len = atoi(str), q, r; 134354263Sshin int max; 134454263Sshin char *p; 13451558Srgrimes 134654263Sshin rtm_addrs |= RTA_NETMASK; 134754263Sshin switch (af) { 134854263Sshin#ifdef INET6 134954263Sshin case AF_INET6: 1350253427Shrs { 1351253427Shrs struct sockaddr_in6 *sin6 = 1352253427Shrs (struct sockaddr_in6 *)&so[RTAX_NETMASK]; 1353253427Shrs 135454263Sshin max = 128; 1355253427Shrs p = (char *)&sin6->sin6_addr; 1356253427Shrs sin6->sin6_family = AF_INET6; 1357253427Shrs sin6->sin6_len = sizeof(*sin6); 135854263Sshin break; 1359253427Shrs } 136054263Sshin#endif 1361253427Shrs#ifdef INET 136254263Sshin case AF_INET: 1363253427Shrs { 1364253427Shrs struct sockaddr_in *sin = 1365253427Shrs (struct sockaddr_in *)&so[RTAX_NETMASK]; 1366253427Shrs 136754263Sshin max = 32; 1368253427Shrs p = (char *)&sin->sin_addr; 1369253427Shrs sin->sin_family = AF_INET; 1370253427Shrs sin->sin_len = sizeof(*sin); 137154263Sshin break; 1372253427Shrs } 1373253427Shrs#endif 137454263Sshin default: 1375253427Shrs errx(EX_OSERR, "prefixlen not supported in this af"); 137654263Sshin } 137754263Sshin 1378253427Shrs if (len < 0 || max < len) 1379253427Shrs errx(EX_USAGE, "%s: invalid prefixlen", str); 138054263Sshin 138154263Sshin q = len >> 3; 138254263Sshin r = len & 7; 138354263Sshin memset((void *)p, 0, max / 8); 138454263Sshin if (q > 0) 138554263Sshin memset((void *)p, 0xff, q); 138654263Sshin if (r > 0) 138754263Sshin *((u_char *)p + q) = (0xff00 >> r) & 0xff; 138854263Sshin if (len == max) 1389204406Suqs return (-1); 139054263Sshin else 1391204406Suqs return (len); 139254263Sshin} 139354263Sshin 1394204406Suqsstatic void 1395196527Scharnierinterfaces(void) 13961558Srgrimes{ 13971558Srgrimes size_t needed; 13981558Srgrimes int mib[6]; 1399128782Sambrisko char *buf, *lim, *next, count = 0; 140092806Sobrien struct rt_msghdr *rtm; 14011558Srgrimes 1402128782Sambriskoretry2: 14031558Srgrimes mib[0] = CTL_NET; 14041558Srgrimes mib[1] = PF_ROUTE; 14051558Srgrimes mib[2] = 0; /* protocol */ 1406253427Shrs mib[3] = AF_UNSPEC; 14071558Srgrimes mib[4] = NET_RT_IFLIST; 14081558Srgrimes mib[5] = 0; /* no flags */ 1409253427Shrs if (sysctl(mib, nitems(mib), NULL, &needed, NULL, 0) < 0) 141013171Swollman err(EX_OSERR, "route-sysctl-estimate"); 14111558Srgrimes if ((buf = malloc(needed)) == NULL) 141237907Scharnier errx(EX_OSERR, "malloc failed"); 1413253427Shrs if (sysctl(mib, nitems(mib), buf, &needed, NULL, 0) < 0) { 1414128782Sambrisko if (errno == ENOMEM && count++ < 10) { 1415128782Sambrisko warnx("Routing table grew, retrying"); 1416128782Sambrisko sleep(1); 1417128782Sambrisko free(buf); 1418128782Sambrisko goto retry2; 1419128782Sambrisko } 142013171Swollman err(EX_OSERR, "actual retrieval of interface table"); 1421128782Sambrisko } 14221558Srgrimes lim = buf + needed; 14231558Srgrimes for (next = buf; next < lim; next += rtm->rtm_msglen) { 1424253502Shrs rtm = (struct rt_msghdr *)(void *)next; 14251558Srgrimes print_rtmsg(rtm, rtm->rtm_msglen); 14261558Srgrimes } 14271558Srgrimes} 14281558Srgrimes 1429204406Suqsstatic void 1430243185Shrsmonitor(int argc, char *argv[]) 14311558Srgrimes{ 1432243185Shrs int n, fib, error; 1433243185Shrs char msg[2048], *endptr; 14341558Srgrimes 1435243185Shrs fib = defaultfib; 1436243185Shrs while (argc > 1) { 1437243185Shrs argc--; 1438243185Shrs argv++; 1439243185Shrs if (**argv != '-') 1440243185Shrs usage(*argv); 1441243185Shrs switch (keyword(*argv + 1)) { 1442243185Shrs case K_FIB: 1443243185Shrs if (!--argc) 1444243185Shrs usage(*argv); 1445244325Shrs errno = 0; 1446243185Shrs fib = strtol(*++argv, &endptr, 0); 1447244325Shrs if (errno == 0) { 1448244325Shrs if (*endptr != '\0' || 1449244325Shrs fib < 0 || 1450244325Shrs (numfibs != -1 && fib > numfibs - 1)) 1451244325Shrs errno = EINVAL; 1452244325Shrs } 1453244325Shrs if (errno) 1454244325Shrs errx(EX_USAGE, "invalid fib number: %s", *argv); 1455243185Shrs break; 1456243185Shrs default: 1457243185Shrs usage(*argv); 1458243185Shrs } 1459243185Shrs } 1460243185Shrs error = set_sofib(fib); 1461243185Shrs if (error) 1462243185Shrs errx(EX_USAGE, "invalid fib number: %d", fib); 1463243185Shrs 14641558Srgrimes verbose = 1; 14651558Srgrimes if (debugonly) { 14661558Srgrimes interfaces(); 14671558Srgrimes exit(0); 14681558Srgrimes } 1469204406Suqs for (;;) { 147054263Sshin time_t now; 14711558Srgrimes n = read(s, msg, 2048); 147254263Sshin now = time(NULL); 1473253427Shrs (void)printf("\ngot message of size %d on %s", n, ctime(&now)); 1474253502Shrs print_rtmsg((struct rt_msghdr *)(void *)msg, n); 14751558Srgrimes } 14761558Srgrimes} 14771558Srgrimes 1478253427Shrsstatic struct { 14791558Srgrimes struct rt_msghdr m_rtm; 14801558Srgrimes char m_space[512]; 14811558Srgrimes} m_rtmsg; 14821558Srgrimes 1483204406Suqsstatic int 1484243185Shrsrtmsg(int cmd, int flags, int fib) 14851558Srgrimes{ 14861558Srgrimes static int seq; 14871558Srgrimes int rlen; 148892806Sobrien char *cp = m_rtmsg.m_space; 148992806Sobrien int l; 14901558Srgrimes 1491253427Shrs#define NEXTADDR(w, u) \ 1492253427Shrs if (rtm_addrs & (w)) { \ 1493253443Shrs l = (((struct sockaddr *)&(u))->sa_len == 0) ? \ 1494253443Shrs sizeof(long) : \ 1495253443Shrs 1 + ((((struct sockaddr *)&(u))->sa_len - 1) \ 1496253443Shrs | (sizeof(long) - 1)); \ 1497253427Shrs memmove(cp, (char *)&(u), l); \ 1498253427Shrs cp += l; \ 1499253427Shrs if (verbose) \ 1500253427Shrs sodump((struct sockaddr *)&(u), #w); \ 15011558Srgrimes } 15021558Srgrimes 15031558Srgrimes errno = 0; 150485048Sru memset(&m_rtmsg, 0, sizeof(m_rtmsg)); 15051558Srgrimes if (cmd == 'a') 15061558Srgrimes cmd = RTM_ADD; 15071558Srgrimes else if (cmd == 'c') 15081558Srgrimes cmd = RTM_CHANGE; 1509191080Skmacy else if (cmd == 'g' || cmd == 's') { 15101558Srgrimes cmd = RTM_GET; 1511253427Shrs if (so[RTAX_IFP].ss_family == 0) { 1512253427Shrs so[RTAX_IFP].ss_family = AF_LINK; 1513253427Shrs so[RTAX_IFP].ss_len = sizeof(struct sockaddr_dl); 15141558Srgrimes rtm_addrs |= RTA_IFP; 15151558Srgrimes } 15161558Srgrimes } else 15171558Srgrimes cmd = RTM_DELETE; 15181558Srgrimes#define rtm m_rtmsg.m_rtm 15191558Srgrimes rtm.rtm_type = cmd; 15201558Srgrimes rtm.rtm_flags = flags; 15211558Srgrimes rtm.rtm_version = RTM_VERSION; 15221558Srgrimes rtm.rtm_seq = ++seq; 15231558Srgrimes rtm.rtm_addrs = rtm_addrs; 15241558Srgrimes rtm.rtm_rmx = rt_metrics; 15251558Srgrimes rtm.rtm_inits = rtm_inits; 15261558Srgrimes 1527253427Shrs NEXTADDR(RTA_DST, so[RTAX_DST]); 1528253427Shrs NEXTADDR(RTA_GATEWAY, so[RTAX_GATEWAY]); 1529253427Shrs NEXTADDR(RTA_NETMASK, so[RTAX_NETMASK]); 1530253427Shrs NEXTADDR(RTA_GENMASK, so[RTAX_GENMASK]); 1531253427Shrs NEXTADDR(RTA_IFP, so[RTAX_IFP]); 1532253427Shrs NEXTADDR(RTA_IFA, so[RTAX_IFA]); 15331558Srgrimes rtm.rtm_msglen = l = cp - (char *)&m_rtmsg; 15341558Srgrimes if (verbose) 15351558Srgrimes print_rtmsg(&rtm, l); 15361558Srgrimes if (debugonly) 15371558Srgrimes return (0); 15381558Srgrimes if ((rlen = write(s, (char *)&m_rtmsg, l)) < 0) { 1539129034Scsjp if (errno == EPERM) 1540129034Scsjp err(1, "writing to routing socket"); 154137907Scharnier warn("writing to routing socket"); 15421558Srgrimes return (-1); 15431558Srgrimes } 15441558Srgrimes if (cmd == RTM_GET) { 15451558Srgrimes do { 15461558Srgrimes l = read(s, (char *)&m_rtmsg, sizeof(m_rtmsg)); 15471558Srgrimes } while (l > 0 && (rtm.rtm_seq != seq || rtm.rtm_pid != pid)); 15481558Srgrimes if (l < 0) 154913171Swollman warn("read from routing socket"); 15501558Srgrimes else 1551243185Shrs print_getmsg(&rtm, l, fib); 15521558Srgrimes } 15531558Srgrimes#undef rtm 15541558Srgrimes return (0); 15551558Srgrimes} 15561558Srgrimes 1557258906Seadlerstatic const char *const msgtypes[] = { 15581558Srgrimes "", 15591558Srgrimes "RTM_ADD: Add Route", 15601558Srgrimes "RTM_DELETE: Delete Route", 15611558Srgrimes "RTM_CHANGE: Change Metrics or flags", 15621558Srgrimes "RTM_GET: Report Metrics", 15631558Srgrimes "RTM_LOSING: Kernel Suspects Partitioning", 15641558Srgrimes "RTM_REDIRECT: Told to use different route", 15651558Srgrimes "RTM_MISS: Lookup failed on this address", 15661558Srgrimes "RTM_LOCK: fix specified metrics", 15671558Srgrimes "RTM_OLDADD: caused by SIOCADDRT", 15681558Srgrimes "RTM_OLDDEL: caused by SIOCDELRT", 15691558Srgrimes "RTM_RESOLVE: Route created by cloning", 15701558Srgrimes "RTM_NEWADDR: address being added to iface", 15711558Srgrimes "RTM_DELADDR: address being removed from iface", 15721558Srgrimes "RTM_IFINFO: iface status change", 157321465Swollman "RTM_NEWMADDR: new multicast group membership on iface", 157421465Swollman "RTM_DELMADDR: multicast group membership removed from iface", 157589498Sru "RTM_IFANNOUNCE: interface arrival/departure", 1576216296Sglebius "RTM_IEEE80211: IEEE 802.11 wireless event", 15771558Srgrimes}; 15781558Srgrimes 1579253427Shrsstatic const char metricnames[] = 1580253427Shrs "\011weight\010rttvar\7rtt\6ssthresh\5sendpipe\4recvpipe\3expire" 1581253427Shrs "\1mtu"; 1582253427Shrsstatic const char routeflags[] = 1583253427Shrs "\1UP\2GATEWAY\3HOST\4REJECT\5DYNAMIC\6MODIFIED\7DONE" 1584253427Shrs "\012XRESOLVE\013LLINFO\014STATIC\015BLACKHOLE" 1585253427Shrs "\017PROTO2\020PROTO1\021PRCLONING\022WASCLONED\023PROTO3" 1586253427Shrs "\025PINNED\026LOCAL\027BROADCAST\030MULTICAST\035STICKY"; 1587253427Shrsstatic const char ifnetflags[] = 1588253427Shrs "\1UP\2BROADCAST\3DEBUG\4LOOPBACK\5PTP\6b6\7RUNNING\010NOARP" 1589253427Shrs "\011PPROMISC\012ALLMULTI\013OACTIVE\014SIMPLEX\015LINK0\016LINK1" 1590253427Shrs "\017LINK2\020MULTICAST"; 1591253427Shrsstatic const char addrnames[] = 1592253427Shrs "\1DST\2GATEWAY\3NETMASK\4GENMASK\5IFP\6IFA\7AUTHOR\010BRD"; 15931558Srgrimes 1594216297Sglebiusstatic const char errfmt[] = 1595253427Shrs "\n%s: truncated route message, only %zu bytes left\n"; 1596216297Sglebius 1597204406Suqsstatic void 1598216297Sglebiusprint_rtmsg(struct rt_msghdr *rtm, size_t msglen) 15991558Srgrimes{ 16001558Srgrimes struct if_msghdr *ifm; 16011558Srgrimes struct ifa_msghdr *ifam; 160221465Swollman#ifdef RTM_NEWMADDR 160321465Swollman struct ifma_msghdr *ifmam; 160421465Swollman#endif 160589498Sru struct if_announcemsghdr *ifan; 1606204406Suqs const char *state; 16071558Srgrimes 16081558Srgrimes if (verbose == 0) 16091558Srgrimes return; 16101558Srgrimes if (rtm->rtm_version != RTM_VERSION) { 1611253427Shrs (void)printf("routing message version %d not understood\n", 16121558Srgrimes rtm->rtm_version); 16131558Srgrimes return; 16141558Srgrimes } 1615253517Shrs if (rtm->rtm_type < nitems(msgtypes)) 161689498Sru (void)printf("%s: ", msgtypes[rtm->rtm_type]); 161789498Sru else 1618216297Sglebius (void)printf("unknown type %d: ", rtm->rtm_type); 161989498Sru (void)printf("len %d, ", rtm->rtm_msglen); 1620216297Sglebius 1621216297Sglebius#define REQUIRE(x) do { \ 1622216297Sglebius if (msglen < sizeof(x)) \ 1623216297Sglebius goto badlen; \ 1624216297Sglebius else \ 1625216297Sglebius msglen -= sizeof(x); \ 1626216297Sglebius } while (0) 1627216297Sglebius 16281558Srgrimes switch (rtm->rtm_type) { 16291558Srgrimes case RTM_IFINFO: 1630216297Sglebius REQUIRE(struct if_msghdr); 16311558Srgrimes ifm = (struct if_msghdr *)rtm; 1632253427Shrs (void)printf("if# %d, ", ifm->ifm_index); 1633128878Sandre switch (ifm->ifm_data.ifi_link_state) { 1634128878Sandre case LINK_STATE_DOWN: 1635128878Sandre state = "down"; 1636128878Sandre break; 1637128878Sandre case LINK_STATE_UP: 1638128878Sandre state = "up"; 1639128878Sandre break; 1640128878Sandre default: 1641128878Sandre state = "unknown"; 1642128878Sandre break; 1643128878Sandre } 1644253427Shrs (void)printf("link: %s, flags:", state); 1645253427Shrs printb(ifm->ifm_flags, ifnetflags); 1646216297Sglebius pmsg_addrs((char *)(ifm + 1), ifm->ifm_addrs, msglen); 16471558Srgrimes break; 16481558Srgrimes case RTM_NEWADDR: 16491558Srgrimes case RTM_DELADDR: 1650216297Sglebius REQUIRE(struct ifa_msghdr); 16511558Srgrimes ifam = (struct ifa_msghdr *)rtm; 1652253427Shrs (void)printf("metric %d, flags:", ifam->ifam_metric); 1653253427Shrs printb(ifam->ifam_flags, routeflags); 1654216297Sglebius pmsg_addrs((char *)(ifam + 1), ifam->ifam_addrs, msglen); 16551558Srgrimes break; 165621465Swollman#ifdef RTM_NEWMADDR 165721465Swollman case RTM_NEWMADDR: 165821465Swollman case RTM_DELMADDR: 1659216297Sglebius REQUIRE(struct ifma_msghdr); 166021465Swollman ifmam = (struct ifma_msghdr *)rtm; 1661216297Sglebius pmsg_addrs((char *)(ifmam + 1), ifmam->ifmam_addrs, msglen); 166221465Swollman break; 166321465Swollman#endif 166489498Sru case RTM_IFANNOUNCE: 1665216297Sglebius REQUIRE(struct if_announcemsghdr); 166689498Sru ifan = (struct if_announcemsghdr *)rtm; 1667253427Shrs (void)printf("if# %d, what: ", ifan->ifan_index); 166889498Sru switch (ifan->ifan_what) { 166989498Sru case IFAN_ARRIVAL: 1670253427Shrs (void)printf("arrival"); 167189498Sru break; 167289498Sru case IFAN_DEPARTURE: 167389498Sru printf("departure"); 167489498Sru break; 167589498Sru default: 167689498Sru printf("#%d", ifan->ifan_what); 167789498Sru break; 167889498Sru } 167989498Sru printf("\n"); 1680243860Sglebius fflush(stdout); 168189498Sru break; 168289498Sru 16831558Srgrimes default: 1684253427Shrs printf("pid: %ld, seq %d, errno %d, flags:", 168513171Swollman (long)rtm->rtm_pid, rtm->rtm_seq, rtm->rtm_errno); 1686253427Shrs printb(rtm->rtm_flags, routeflags); 1687216297Sglebius pmsg_common(rtm, msglen); 16881558Srgrimes } 1689216297Sglebius 1690216297Sglebius return; 1691216297Sglebius 1692216297Sglebiusbadlen: 1693216297Sglebius (void)printf(errfmt, __func__, msglen); 1694216297Sglebius#undef REQUIRE 16951558Srgrimes} 16961558Srgrimes 1697204406Suqsstatic void 1698243185Shrsprint_getmsg(struct rt_msghdr *rtm, int msglen, int fib) 16991558Srgrimes{ 1700253504Shrs struct sockaddr *sp[RTAX_MAX]; 1701256695Shrs struct timespec ts; 170292806Sobrien char *cp; 170392806Sobrien int i; 17041558Srgrimes 1705253504Shrs memset(sp, 0, sizeof(sp)); 1706253427Shrs (void)printf(" route to: %s\n", 1707253427Shrs routename((struct sockaddr *)&so[RTAX_DST])); 17081558Srgrimes if (rtm->rtm_version != RTM_VERSION) { 170913171Swollman warnx("routing message version %d not understood", 171013171Swollman rtm->rtm_version); 17111558Srgrimes return; 17121558Srgrimes } 17131558Srgrimes if (rtm->rtm_msglen > msglen) { 171437907Scharnier warnx("message length mismatch, in packet %d, returned %d", 171513171Swollman rtm->rtm_msglen, msglen); 1716253517Shrs return; 17171558Srgrimes } 17181558Srgrimes if (rtm->rtm_errno) { 171913171Swollman errno = rtm->rtm_errno; 172013171Swollman warn("message indicates error %d", errno); 17211558Srgrimes return; 17221558Srgrimes } 17231558Srgrimes cp = ((char *)(rtm + 1)); 1724253589Shrs for (i = 0; i < RTAX_MAX; i++) 1725253589Shrs if (rtm->rtm_addrs & (1 << i)) { 1726253504Shrs sp[i] = (struct sockaddr *)cp; 1727253589Shrs cp += SA_SIZE((struct sockaddr *)cp); 1728253589Shrs } 1729253589Shrs if ((rtm->rtm_addrs & RTA_IFP) && 1730253589Shrs (sp[RTAX_IFP]->sa_family != AF_LINK || 1731253589Shrs ((struct sockaddr_dl *)(void *)sp[RTAX_IFP])->sdl_nlen == 0)) 1732253504Shrs sp[RTAX_IFP] = NULL; 1733253504Shrs if (sp[RTAX_DST] && sp[RTAX_NETMASK]) 1734253504Shrs sp[RTAX_NETMASK]->sa_family = sp[RTAX_DST]->sa_family; /* XXX */ 1735253504Shrs if (sp[RTAX_DST]) 1736253504Shrs (void)printf("destination: %s\n", routename(sp[RTAX_DST])); 1737253504Shrs if (sp[RTAX_NETMASK]) 1738253504Shrs (void)printf(" mask: %s\n", routename(sp[RTAX_NETMASK])); 1739253504Shrs if (sp[RTAX_GATEWAY] && (rtm->rtm_flags & RTF_GATEWAY)) 1740253504Shrs (void)printf(" gateway: %s\n", routename(sp[RTAX_GATEWAY])); 1741243185Shrs if (fib >= 0) 1742243185Shrs (void)printf(" fib: %u\n", (unsigned int)fib); 1743253504Shrs if (sp[RTAX_IFP]) 17441558Srgrimes (void)printf(" interface: %.*s\n", 1745253504Shrs ((struct sockaddr_dl *)(void *)sp[RTAX_IFP])->sdl_nlen, 1746253504Shrs ((struct sockaddr_dl *)(void *)sp[RTAX_IFP])->sdl_data); 17471558Srgrimes (void)printf(" flags: "); 1748253427Shrs printb(rtm->rtm_flags, routeflags); 17491558Srgrimes 17501558Srgrimes#define lock(f) ((rtm->rtm_rmx.rmx_locks & __CONCAT(RTV_,f)) ? 'L' : ' ') 17511558Srgrimes#define msec(u) (((u) + 500) / 1000) /* usec to msec */ 1752253517Shrs printf("\n%9s %9s %9s %9s %9s %10s %9s\n", "recvpipe", 1753253517Shrs "sendpipe", "ssthresh", "rtt,msec", "mtu ", "weight", "expire"); 1754256695Shrs printf("%8lu%c ", rtm->rtm_rmx.rmx_recvpipe, lock(RPIPE)); 1755256695Shrs printf("%8lu%c ", rtm->rtm_rmx.rmx_sendpipe, lock(SPIPE)); 1756256695Shrs printf("%8lu%c ", rtm->rtm_rmx.rmx_ssthresh, lock(SSTHRESH)); 1757256695Shrs printf("%8lu%c ", msec(rtm->rtm_rmx.rmx_rtt), lock(RTT)); 1758256695Shrs printf("%8lu%c ", rtm->rtm_rmx.rmx_mtu, lock(MTU)); 1759256695Shrs printf("%8lu%c ", rtm->rtm_rmx.rmx_weight, lock(WEIGHT)); 1760256695Shrs if (rtm->rtm_rmx.rmx_expire > 0) 1761256695Shrs clock_gettime(CLOCK_REALTIME_FAST, &ts); 1762256695Shrs else 1763256695Shrs ts.tv_sec = 0; 1764256715Shrs printf("%8ld%c\n", (long)(rtm->rtm_rmx.rmx_expire - ts.tv_sec), 1765256715Shrs lock(EXPIRE)); 17661558Srgrimes#undef lock 17671558Srgrimes#undef msec 17681558Srgrimes#define RTA_IGN (RTA_DST|RTA_GATEWAY|RTA_NETMASK|RTA_IFP|RTA_IFA|RTA_BRD) 17691558Srgrimes if (verbose) 1770216297Sglebius pmsg_common(rtm, msglen); 17711558Srgrimes else if (rtm->rtm_addrs &~ RTA_IGN) { 1772253427Shrs (void)printf("sockaddrs: "); 1773253427Shrs printb(rtm->rtm_addrs, addrnames); 17741558Srgrimes putchar('\n'); 17751558Srgrimes } 17761558Srgrimes#undef RTA_IGN 17771558Srgrimes} 17781558Srgrimes 1779204406Suqsstatic void 1780216297Sglebiuspmsg_common(struct rt_msghdr *rtm, size_t msglen) 17811558Srgrimes{ 1782253427Shrs 1783253427Shrs (void)printf("\nlocks: "); 1784253427Shrs printb(rtm->rtm_rmx.rmx_locks, metricnames); 1785253427Shrs (void)printf(" inits: "); 1786253427Shrs printb(rtm->rtm_inits, metricnames); 1787216297Sglebius if (msglen > sizeof(struct rt_msghdr)) 1788216297Sglebius pmsg_addrs(((char *)(rtm + 1)), rtm->rtm_addrs, 1789216297Sglebius msglen - sizeof(struct rt_msghdr)); 1790216297Sglebius else 1791253427Shrs (void)fflush(stdout); 17921558Srgrimes} 17931558Srgrimes 1794204406Suqsstatic void 1795216297Sglebiuspmsg_addrs(char *cp, int addrs, size_t len) 17961558Srgrimes{ 179792806Sobrien struct sockaddr *sa; 17981558Srgrimes int i; 17991558Srgrimes 180071061Sphk if (addrs == 0) { 1801253427Shrs (void)putchar('\n'); 18021558Srgrimes return; 180371061Sphk } 1804253427Shrs (void)printf("\nsockaddrs: "); 1805253427Shrs printb(addrs, addrnames); 1806253427Shrs putchar('\n'); 1807253517Shrs for (i = 0; i < RTAX_MAX; i++) 1808253517Shrs if (addrs & (1 << i)) { 18091558Srgrimes sa = (struct sockaddr *)cp; 1810216297Sglebius if (len == 0 || len < SA_SIZE(sa)) { 1811253427Shrs (void)printf(errfmt, __func__, len); 1812216297Sglebius break; 1813216297Sglebius } 1814253427Shrs (void)printf(" %s", routename(sa)); 1815216297Sglebius len -= SA_SIZE(sa); 1816128186Sluigi cp += SA_SIZE(sa); 18171558Srgrimes } 1818253427Shrs (void)putchar('\n'); 1819253427Shrs (void)fflush(stdout); 18201558Srgrimes} 18211558Srgrimes 1822204406Suqsstatic void 1823253427Shrsprintb(int b, const char *str) 18241558Srgrimes{ 182592806Sobrien int i; 18261558Srgrimes int gotsome = 0; 18271558Srgrimes 18281558Srgrimes if (b == 0) 18291558Srgrimes return; 1830204406Suqs while ((i = *str++) != 0) { 18311558Srgrimes if (b & (1 << (i-1))) { 18321558Srgrimes if (gotsome == 0) 18331558Srgrimes i = '<'; 18341558Srgrimes else 18351558Srgrimes i = ','; 1836253427Shrs putchar(i); 18371558Srgrimes gotsome = 1; 1838204406Suqs for (; (i = *str) > 32; str++) 1839253427Shrs putchar(i); 18401558Srgrimes } else 1841204406Suqs while (*str > 32) 1842204406Suqs str++; 18431558Srgrimes } 18441558Srgrimes if (gotsome) 1845253427Shrs putchar('>'); 18461558Srgrimes} 18471558Srgrimes 18481558Srgrimesint 1849204406Suqskeyword(const char *cp) 18501558Srgrimes{ 1851258907Seadler const struct keytab *kt = keywords; 18521558Srgrimes 1853204406Suqs while (kt->kt_cp != NULL && strcmp(kt->kt_cp, cp) != 0) 18541558Srgrimes kt++; 1855204406Suqs return (kt->kt_i); 18561558Srgrimes} 18571558Srgrimes 1858204406Suqsstatic void 1859253427Shrssodump(struct sockaddr *sa, const char *which) 18601558Srgrimes{ 1861253427Shrs#ifdef INET6 1862253427Shrs char nbuf[INET6_ADDRSTRLEN]; 1863253427Shrs#endif 1864253427Shrs 1865253427Shrs switch (sa->sa_family) { 18661558Srgrimes case AF_LINK: 1867253427Shrs (void)printf("%s: link %s; ", which, 1868253502Shrs link_ntoa((struct sockaddr_dl *)(void *)sa)); 18691558Srgrimes break; 1870253427Shrs#ifdef INET 18711558Srgrimes case AF_INET: 1872253427Shrs (void)printf("%s: inet %s; ", which, 1873253502Shrs inet_ntoa(((struct sockaddr_in *)(void *)sa)->sin_addr)); 18741558Srgrimes break; 1875253427Shrs#endif 1876253427Shrs#ifdef INET6 1877253427Shrs case AF_INET6: 1878253427Shrs (void)printf("%s: inet6 %s; ", which, inet_ntop(sa->sa_family, 1879253502Shrs &((struct sockaddr_in6 *)(void *)sa)->sin6_addr, nbuf, 1880253427Shrs sizeof(nbuf))); 1881253427Shrs break; 1882253427Shrs#endif 188317046Sjulian case AF_APPLETALK: 1884253427Shrs (void)printf("%s: atalk %s; ", which, 1885253502Shrs atalk_ntoa(((struct sockaddr_at *)(void *)sa)->sat_addr)); 188617046Sjulian break; 18871558Srgrimes } 1888253427Shrs (void)fflush(stdout); 18891558Srgrimes} 18901558Srgrimes 18911558Srgrimes/* States*/ 18921558Srgrimes#define VIRGIN 0 18931558Srgrimes#define GOTONE 1 18941558Srgrimes#define GOTTWO 2 18951558Srgrimes/* Inputs */ 18961558Srgrimes#define DIGIT (4*0) 18971558Srgrimes#define END (4*1) 18981558Srgrimes#define DELIM (4*2) 18991558Srgrimes 1900204406Suqsstatic void 1901253427Shrssockaddr(char *addr, struct sockaddr *sa, size_t size) 19021558Srgrimes{ 190392806Sobrien char *cp = (char *)sa; 19041558Srgrimes char *cplim = cp + size; 190592806Sobrien int byte = 0, state = VIRGIN, new = 0 /* foil gcc */; 19061558Srgrimes 190785048Sru memset(cp, 0, size); 19081558Srgrimes cp++; 19091558Srgrimes do { 19101558Srgrimes if ((*addr >= '0') && (*addr <= '9')) { 19111558Srgrimes new = *addr - '0'; 19121558Srgrimes } else if ((*addr >= 'a') && (*addr <= 'f')) { 19131558Srgrimes new = *addr - 'a' + 10; 19141558Srgrimes } else if ((*addr >= 'A') && (*addr <= 'F')) { 19151558Srgrimes new = *addr - 'A' + 10; 1916204406Suqs } else if (*addr == '\0') 19171558Srgrimes state |= END; 19181558Srgrimes else 19191558Srgrimes state |= DELIM; 19201558Srgrimes addr++; 19211558Srgrimes switch (state /* | INPUT */) { 19221558Srgrimes case GOTTWO | DIGIT: 19231558Srgrimes *cp++ = byte; /*FALLTHROUGH*/ 19241558Srgrimes case VIRGIN | DIGIT: 19251558Srgrimes state = GOTONE; byte = new; continue; 19261558Srgrimes case GOTONE | DIGIT: 19271558Srgrimes state = GOTTWO; byte = new + (byte << 4); continue; 19281558Srgrimes default: /* | DELIM */ 19291558Srgrimes state = VIRGIN; *cp++ = byte; byte = 0; continue; 19301558Srgrimes case GOTONE | END: 19311558Srgrimes case GOTTWO | END: 19321558Srgrimes *cp++ = byte; /* FALLTHROUGH */ 19331558Srgrimes case VIRGIN | END: 19341558Srgrimes break; 19351558Srgrimes } 19361558Srgrimes break; 19371558Srgrimes } while (cp < cplim); 19381558Srgrimes sa->sa_len = cp - (char *)sa; 19391558Srgrimes} 194017046Sjulian 1941204406Suqsstatic int 194217046Sjulianatalk_aton(const char *text, struct at_addr *addr) 194317046Sjulian{ 194417046Sjulian u_int net, node; 194517046Sjulian 194617046Sjulian if (sscanf(text, "%u.%u", &net, &node) != 2 194717046Sjulian || net > 0xffff || node > 0xff) 194817046Sjulian return(0); 194917254Sjulian addr->s_net = htons(net); 195017046Sjulian addr->s_node = node; 195117046Sjulian return(1); 195217046Sjulian} 195317046Sjulian 1954204406Suqsstatic char * 195517046Sjulianatalk_ntoa(struct at_addr at) 195617046Sjulian{ 195717046Sjulian static char buf[20]; 195817046Sjulian 1959253427Shrs (void)snprintf(buf, sizeof(buf), "%u.%u", ntohs(at.s_net), at.s_node); 1960253427Shrs buf[sizeof(buf) - 1] = '\0'; 196117046Sjulian return(buf); 196217046Sjulian} 1963