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: stable/10/sbin/route/route.c 330498 2018-03-05 12:06:41Z eugen $"); 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> 70272852Shrs#include <time.h> 7120287Swollman#include <unistd.h> 7278064Sume#include <ifaddrs.h> 731558Srgrimes 74253427Shrsstatic struct keytab { 75204406Suqs const char *kt_cp; 761558Srgrimes int kt_i; 771558Srgrimes} 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; 85270050Sbzstatic int 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); 144265701Smelifaro errx(EX_USAGE, "usage: route [-46dnqtv] 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 157265701Smelifaro while ((ch = getopt(argc, argv, "46nqdtv")) != -1) 1581558Srgrimes switch(ch) { 159265701Smelifaro case '4': 160265701Smelifaro#ifdef INET 161265701Smelifaro af = AF_INET; 162265701Smelifaro aflen = sizeof(struct sockaddr_in); 163265701Smelifaro#else 164265701Smelifaro errx(1, "IPv4 support is not compiled in"); 165265701Smelifaro#endif 166265701Smelifaro break; 167265701Smelifaro case '6': 168265701Smelifaro#ifdef INET6 169265701Smelifaro af = AF_INET6; 170265701Smelifaro aflen = sizeof(struct sockaddr_in6); 171265701Smelifaro#else 172265701Smelifaro errx(1, "IPv6 support is not compiled in"); 173265701Smelifaro#endif 174265701Smelifaro break; 1751558Srgrimes case 'n': 1761558Srgrimes nflag = 1; 1771558Srgrimes break; 1781558Srgrimes case 'q': 1791558Srgrimes qflag = 1; 1801558Srgrimes break; 1811558Srgrimes case 'v': 1821558Srgrimes verbose = 1; 1831558Srgrimes break; 1841558Srgrimes case 't': 1851558Srgrimes tflag = 1; 1861558Srgrimes break; 1871558Srgrimes case 'd': 1881558Srgrimes debugonly = 1; 1891558Srgrimes break; 1901558Srgrimes case '?': 1911558Srgrimes default: 192204406Suqs usage(NULL); 1931558Srgrimes } 1941558Srgrimes argc -= optind; 1951558Srgrimes argv += optind; 1961558Srgrimes 1971558Srgrimes pid = getpid(); 198109811Skbyanc uid = geteuid(); 1991558Srgrimes if (tflag) 20069793Sobrien s = open(_PATH_DEVNULL, O_WRONLY, 0); 2011558Srgrimes else 2021558Srgrimes s = socket(PF_ROUTE, SOCK_RAW, 0); 2031558Srgrimes if (s < 0) 20413171Swollman err(EX_OSERR, "socket"); 205243185Shrs 206243185Shrs len = sizeof(numfibs); 207243185Shrs if (sysctlbyname("net.fibs", (void *)&numfibs, &len, NULL, 0) == -1) 208243185Shrs numfibs = -1; 209243185Shrs 210243185Shrs len = sizeof(defaultfib); 211243185Shrs if (numfibs != -1 && 212243185Shrs sysctlbyname("net.my_fibnum", (void *)&defaultfib, &len, NULL, 213243185Shrs 0) == -1) 214243185Shrs defaultfib = -1; 215243185Shrs 216204406Suqs if (*argv != NULL) 2171558Srgrimes switch (keyword(*argv)) { 2181558Srgrimes case K_GET: 219191080Skmacy case K_SHOW: 2201558Srgrimes uid = 0; 2211558Srgrimes /* FALLTHROUGH */ 2221558Srgrimes 2231558Srgrimes case K_CHANGE: 2241558Srgrimes case K_ADD: 225150679Stobez case K_DEL: 2261558Srgrimes case K_DELETE: 2271558Srgrimes newroute(argc, argv); 2281558Srgrimes /* NOTREACHED */ 2291558Srgrimes 2301558Srgrimes case K_MONITOR: 231243185Shrs monitor(argc, argv); 2321558Srgrimes /* NOTREACHED */ 2331558Srgrimes 2341558Srgrimes case K_FLUSH: 2351558Srgrimes flushroutes(argc, argv); 2361558Srgrimes exit(0); 2371558Srgrimes /* NOTREACHED */ 2381558Srgrimes } 2391558Srgrimes usage(*argv); 2401558Srgrimes /* NOTREACHED */ 2411558Srgrimes} 2421558Srgrimes 243243185Shrsstatic int 244243185Shrsset_sofib(int fib) 245243185Shrs{ 246243185Shrs 247243185Shrs if (fib < 0) 248243185Shrs return (0); 249243185Shrs return (setsockopt(s, SOL_SOCKET, SO_SETFIB, (void *)&fib, 250243185Shrs sizeof(fib))); 251243185Shrs} 252243185Shrs 253243185Shrsstatic int 254243185Shrsfiboptlist_range(const char *arg, struct fibl_head_t *flh) 255243185Shrs{ 256243185Shrs struct fibl *fl; 257244325Shrs char *str0, *str, *token, *endptr; 258243185Shrs int fib[2], i, error; 259243185Shrs 260244325Shrs str0 = str = strdup(arg); 261243185Shrs error = 0; 262243185Shrs i = 0; 263243185Shrs while ((token = strsep(&str, "-")) != NULL) { 264243185Shrs switch (i) { 265243185Shrs case 0: 266243185Shrs case 1: 267244325Shrs errno = 0; 268243185Shrs fib[i] = strtol(token, &endptr, 0); 269244325Shrs if (errno == 0) { 270244325Shrs if (*endptr != '\0' || 271244325Shrs fib[i] < 0 || 272316042Sngie (numfibs != -1 && fib[i] > numfibs - 1)) 273244325Shrs errno = EINVAL; 274244325Shrs } 275244325Shrs if (errno) 276243185Shrs error = 1; 277243185Shrs break; 278243185Shrs default: 279243185Shrs error = 1; 280243185Shrs } 281243185Shrs if (error) 282243185Shrs goto fiboptlist_range_ret; 283243185Shrs i++; 284243185Shrs } 285243185Shrs if (fib[0] >= fib[1]) { 286243185Shrs error = 1; 287243185Shrs goto fiboptlist_range_ret; 288243185Shrs } 289243185Shrs for (i = fib[0]; i <= fib[1]; i++) { 290243185Shrs fl = calloc(1, sizeof(*fl)); 291243185Shrs if (fl == NULL) { 292243185Shrs error = 1; 293243185Shrs goto fiboptlist_range_ret; 294243185Shrs } 295243185Shrs fl->fl_num = i; 296243185Shrs TAILQ_INSERT_TAIL(flh, fl, fl_next); 297243185Shrs } 298243185Shrsfiboptlist_range_ret: 299244325Shrs free(str0); 300243185Shrs return (error); 301243185Shrs} 302243185Shrs 303243185Shrs#define ALLSTRLEN 64 304243185Shrsstatic int 305243185Shrsfiboptlist_csv(const char *arg, struct fibl_head_t *flh) 306243185Shrs{ 307243185Shrs struct fibl *fl; 308244325Shrs char *str0, *str, *token, *endptr; 309243185Shrs int fib, error; 310243185Shrs 311253427Shrs str0 = str = NULL; 312243185Shrs if (strcmp("all", arg) == 0) { 313243185Shrs str = calloc(1, ALLSTRLEN); 314243185Shrs if (str == NULL) { 315243185Shrs error = 1; 316243185Shrs goto fiboptlist_csv_ret; 317243185Shrs } 318243185Shrs if (numfibs > 1) 319243185Shrs snprintf(str, ALLSTRLEN - 1, "%d-%d", 0, numfibs - 1); 320243185Shrs else 321243185Shrs snprintf(str, ALLSTRLEN - 1, "%d", 0); 322243185Shrs } else if (strcmp("default", arg) == 0) { 323244325Shrs str0 = str = calloc(1, ALLSTRLEN); 324243185Shrs if (str == NULL) { 325243185Shrs error = 1; 326243185Shrs goto fiboptlist_csv_ret; 327243185Shrs } 328243185Shrs snprintf(str, ALLSTRLEN - 1, "%d", defaultfib); 329243185Shrs } else 330244325Shrs str0 = str = strdup(arg); 331243185Shrs 332243185Shrs error = 0; 333243185Shrs while ((token = strsep(&str, ",")) != NULL) { 334243185Shrs if (*token != '-' && strchr(token, '-') != NULL) { 335243185Shrs error = fiboptlist_range(token, flh); 336243185Shrs if (error) 337243185Shrs goto fiboptlist_csv_ret; 338243185Shrs } else { 339244325Shrs errno = 0; 340243185Shrs fib = strtol(token, &endptr, 0); 341244325Shrs if (errno == 0) { 342244325Shrs if (*endptr != '\0' || 343244325Shrs fib < 0 || 344244325Shrs (numfibs != -1 && fib > numfibs - 1)) 345244325Shrs errno = EINVAL; 346244325Shrs } 347244325Shrs if (errno) { 348243185Shrs error = 1; 349243185Shrs goto fiboptlist_csv_ret; 350243185Shrs } 351243185Shrs fl = calloc(1, sizeof(*fl)); 352243185Shrs if (fl == NULL) { 353243185Shrs error = 1; 354243185Shrs goto fiboptlist_csv_ret; 355243185Shrs } 356243185Shrs fl->fl_num = fib; 357243185Shrs TAILQ_INSERT_TAIL(flh, fl, fl_next); 358243185Shrs } 359243185Shrs } 360243185Shrsfiboptlist_csv_ret: 361253427Shrs if (str0 != NULL) 362253427Shrs free(str0); 363243185Shrs return (error); 364243185Shrs} 365243185Shrs 3661558Srgrimes/* 3671558Srgrimes * Purge all entries in the routing tables not 3681558Srgrimes * associated with network interfaces. 3691558Srgrimes */ 370204406Suqsstatic void 371196527Scharnierflushroutes(int argc, char *argv[]) 3721558Srgrimes{ 373243185Shrs struct fibl *fl; 374243185Shrs int error; 3751558Srgrimes 376253427Shrs if (uid != 0 && !debugonly && !tflag) 37713171Swollman errx(EX_NOPERM, "must be root to alter routing table"); 378146079Sjmallett shutdown(s, SHUT_RD); /* Don't want to read back our messages */ 379243185Shrs 380243185Shrs TAILQ_INIT(&fibl_head); 381243185Shrs while (argc > 1) { 382243185Shrs argc--; 3831558Srgrimes argv++; 384243185Shrs if (**argv != '-') 385243185Shrs usage(*argv); 386243185Shrs switch (keyword(*argv + 1)) { 387253427Shrs#ifdef INET 388265701Smelifaro case K_4: 389243185Shrs case K_INET: 390243185Shrs af = AF_INET; 391243185Shrs break; 392253427Shrs#endif 39354263Sshin#ifdef INET6 394265701Smelifaro case K_6: 395243185Shrs case K_INET6: 396243185Shrs af = AF_INET6; 397243185Shrs break; 39854263Sshin#endif 399243185Shrs case K_ATALK: 400243185Shrs af = AF_APPLETALK; 401243185Shrs break; 402243185Shrs case K_LINK: 403243185Shrs af = AF_LINK; 404243185Shrs break; 405243185Shrs case K_FIB: 406243185Shrs if (!--argc) 407243185Shrs usage(*argv); 408243185Shrs error = fiboptlist_csv(*++argv, &fibl_head); 409243185Shrs if (error) 410244325Shrs errx(EX_USAGE, "invalid fib number: %s", *argv); 411243185Shrs break; 412243185Shrs default: 413243185Shrs usage(*argv); 414243185Shrs } 4151558Srgrimes } 416243185Shrs if (TAILQ_EMPTY(&fibl_head)) { 417243185Shrs error = fiboptlist_csv("default", &fibl_head); 418243185Shrs if (error) 419243185Shrs errx(EX_OSERR, "fiboptlist_csv failed."); 420243185Shrs } 421243185Shrs TAILQ_FOREACH(fl, &fibl_head, fl_next) 422243185Shrs flushroutes_fib(fl->fl_num); 423243185Shrs} 424243185Shrs 425243185Shrsstatic int 426243185Shrsflushroutes_fib(int fib) 427243185Shrs{ 428243185Shrs struct rt_msghdr *rtm; 429243185Shrs size_t needed; 430243185Shrs char *buf, *next, *lim; 431253429Shrs int mib[7], rlen, seqno, count = 0; 432243185Shrs int error; 433243185Shrs 434243185Shrs error = set_sofib(fib); 435243185Shrs if (error) { 436243185Shrs warn("fib number %d is ignored", fib); 437243185Shrs return (error); 438243185Shrs } 439243185Shrs 440128782Sambriskoretry: 4411558Srgrimes mib[0] = CTL_NET; 4421558Srgrimes mib[1] = PF_ROUTE; 4431558Srgrimes mib[2] = 0; /* protocol */ 444253427Shrs mib[3] = AF_UNSPEC; 4451558Srgrimes mib[4] = NET_RT_DUMP; 4461558Srgrimes mib[5] = 0; /* no flags */ 447253429Shrs mib[6] = fib; 448253427Shrs if (sysctl(mib, nitems(mib), NULL, &needed, NULL, 0) < 0) 44913171Swollman err(EX_OSERR, "route-sysctl-estimate"); 4501558Srgrimes if ((buf = malloc(needed)) == NULL) 45137907Scharnier errx(EX_OSERR, "malloc failed"); 452253427Shrs if (sysctl(mib, nitems(mib), buf, &needed, NULL, 0) < 0) { 453128782Sambrisko if (errno == ENOMEM && count++ < 10) { 454204406Suqs warnx("Routing table grew, retrying"); 455128782Sambrisko sleep(1); 456128782Sambrisko free(buf); 457128782Sambrisko goto retry; 458128782Sambrisko } 45913171Swollman err(EX_OSERR, "route-sysctl-get"); 460128782Sambrisko } 4611558Srgrimes lim = buf + needed; 4621558Srgrimes if (verbose) 463253427Shrs (void)printf("Examining routing table from sysctl\n"); 4641558Srgrimes seqno = 0; /* ??? */ 4651558Srgrimes for (next = buf; next < lim; next += rtm->rtm_msglen) { 466253502Shrs rtm = (struct rt_msghdr *)(void *)next; 4671558Srgrimes if (verbose) 4681558Srgrimes print_rtmsg(rtm, rtm->rtm_msglen); 4691558Srgrimes if ((rtm->rtm_flags & RTF_GATEWAY) == 0) 4701558Srgrimes continue; 471204406Suqs if (af != 0) { 4721558Srgrimes struct sockaddr *sa = (struct sockaddr *)(rtm + 1); 4731558Srgrimes 4741558Srgrimes if (sa->sa_family != af) 4751558Srgrimes continue; 4761558Srgrimes } 4771558Srgrimes if (debugonly) 4781558Srgrimes continue; 4791558Srgrimes rtm->rtm_type = RTM_DELETE; 4801558Srgrimes rtm->rtm_seq = seqno; 4811558Srgrimes rlen = write(s, next, rtm->rtm_msglen); 482129034Scsjp if (rlen < 0 && errno == EPERM) 483129034Scsjp err(1, "write to routing socket"); 4841558Srgrimes if (rlen < (int)rtm->rtm_msglen) { 48513171Swollman warn("write to routing socket"); 486253427Shrs (void)printf("got only %d for rlen\n", rlen); 487128782Sambrisko free(buf); 488128782Sambrisko goto retry; 4891558Srgrimes break; 4901558Srgrimes } 4911558Srgrimes seqno++; 4921558Srgrimes if (qflag) 4931558Srgrimes continue; 4941558Srgrimes if (verbose) 4951558Srgrimes print_rtmsg(rtm, rlen); 4961558Srgrimes else { 4971558Srgrimes struct sockaddr *sa = (struct sockaddr *)(rtm + 1); 498243185Shrs 499243185Shrs printf("%-20.20s ", rtm->rtm_flags & RTF_HOST ? 5001558Srgrimes routename(sa) : netname(sa)); 501128186Sluigi sa = (struct sockaddr *)(SA_SIZE(sa) + (char *)sa); 502243185Shrs printf("%-20.20s ", routename(sa)); 503243185Shrs if (fib >= 0) 504243185Shrs printf("-fib %-3d ", fib); 505243185Shrs printf("done\n"); 5061558Srgrimes } 5071558Srgrimes } 508243185Shrs return (error); 5091558Srgrimes} 5101558Srgrimes 511253519Shrsstatic const char * 512196527Scharnierroutename(struct sockaddr *sa) 5131558Srgrimes{ 514253517Shrs struct sockaddr_dl *sdl; 515204406Suqs const char *cp; 516253519Shrs static char line[NI_MAXHOST]; 5171558Srgrimes static char domain[MAXHOSTNAMELEN + 1]; 51881976Sbrian static int first = 1, n; 5191558Srgrimes 5201558Srgrimes if (first) { 5211558Srgrimes first = 0; 5221558Srgrimes if (gethostname(domain, MAXHOSTNAMELEN) == 0 && 52385048Sru (cp = strchr(domain, '.'))) { 52419209Sfenner domain[MAXHOSTNAMELEN] = '\0'; 525253427Shrs (void)strcpy(domain, cp + 1); 52619209Sfenner } else 527253427Shrs domain[0] = '\0'; 5281558Srgrimes } 5291558Srgrimes 530253519Shrs /* If the address is zero-filled, use "default". */ 531253503Shrs if (sa->sa_len == 0 && nflag == 0) 532253503Shrs return ("default"); 533253519Shrs#if defined(INET) || defined(INET6) 534253427Shrs switch (sa->sa_family) { 535253427Shrs#ifdef INET 536253427Shrs case AF_INET: 537253519Shrs /* If the address is zero-filled, use "default". */ 538253519Shrs if (nflag == 0 && 539253519Shrs ((struct sockaddr_in *)(void *)sa)->sin_addr.s_addr == 540253519Shrs INADDR_ANY) 541253519Shrs return("default"); 5421558Srgrimes break; 543253519Shrs#endif 544253519Shrs#ifdef INET6 545253519Shrs case AF_INET6: 546253519Shrs /* If the address is zero-filled, use "default". */ 547253519Shrs if (nflag == 0 && 548253519Shrs IN6_IS_ADDR_UNSPECIFIED(&((struct sockaddr_in6 *)(void *)sa)->sin6_addr)) 549253519Shrs return("default"); 550253519Shrs break; 551253519Shrs#endif 552253427Shrs } 553253519Shrs#endif 5541558Srgrimes 555253519Shrs switch (sa->sa_family) { 556253519Shrs#if defined(INET) || defined(INET6) 557253519Shrs#ifdef INET 558253519Shrs case AF_INET: 559253427Shrs#endif 56054263Sshin#ifdef INET6 56154263Sshin case AF_INET6: 562253519Shrs#endif 56378064Sume { 564253519Shrs struct sockaddr_storage ss; 565253519Shrs int error; 566253519Shrs char *p; 56754263Sshin 568253519Shrs memset(&ss, 0, sizeof(ss)); 569253519Shrs if (sa->sa_len == 0) 570253519Shrs ss.ss_family = sa->sa_family; 571253519Shrs else 572253519Shrs memcpy(&ss, sa, sa->sa_len); 573253519Shrs /* Expand sa->sa_len because it could be shortened. */ 574253519Shrs if (sa->sa_family == AF_INET) 575253519Shrs ss.ss_len = sizeof(struct sockaddr_in); 576253519Shrs else if (sa->sa_family == AF_INET6) 577253519Shrs ss.ss_len = sizeof(struct sockaddr_in6); 578253519Shrs error = getnameinfo((struct sockaddr *)&ss, ss.ss_len, 579253519Shrs line, sizeof(line), NULL, 0, 580253519Shrs (nflag == 0) ? 0 : NI_NUMERICHOST); 581253519Shrs if (error) { 582253519Shrs warnx("getnameinfo(): %s", gai_strerror(error)); 58378064Sume strncpy(line, "invalid", sizeof(line)); 584253519Shrs } 58578064Sume 586253519Shrs /* Remove the domain part if any. */ 587253519Shrs p = strchr(line, '.'); 588253519Shrs if (p != NULL && strcmp(p + 1, domain) == 0) 589253519Shrs *p = '\0'; 590253519Shrs 591253427Shrs return (line); 592253519Shrs break; 59378064Sume } 59478064Sume#endif 59517046Sjulian case AF_APPLETALK: 596253427Shrs (void)snprintf(line, sizeof(line), "atalk %s", 597253502Shrs atalk_ntoa(((struct sockaddr_at *)(void *)sa)->sat_addr)); 59817046Sjulian break; 59917046Sjulian 6001558Srgrimes case AF_LINK: 601253517Shrs sdl = (struct sockaddr_dl *)(void *)sa; 602253517Shrs 603253517Shrs if (sdl->sdl_nlen == 0 && 604253517Shrs sdl->sdl_alen == 0 && 605253517Shrs sdl->sdl_slen == 0) { 606253517Shrs n = snprintf(line, sizeof(line), "link#%d", 607253517Shrs sdl->sdl_index); 608253517Shrs if (n > (int)sizeof(line)) 609253517Shrs line[0] = '\0'; 610253517Shrs return (line); 611253517Shrs } else 612253517Shrs return (link_ntoa(sdl)); 613253427Shrs break; 6141558Srgrimes 6151558Srgrimes default: 616204406Suqs { 617253502Shrs u_short *sp = (u_short *)(void *)sa; 618204406Suqs u_short *splim = sp + ((sa->sa_len + 1) >> 1); 619204406Suqs char *cps = line + sprintf(line, "(%d)", sa->sa_family); 62019209Sfenner char *cpe = line + sizeof(line); 6211558Srgrimes 622204406Suqs while (++sp < splim && cps < cpe) /* start with sa->sa_data */ 623204406Suqs if ((n = snprintf(cps, cpe - cps, " %x", *sp)) > 0) 624204406Suqs cps += n; 62581980Sbrian else 626204406Suqs *cps = '\0'; 6271558Srgrimes break; 6281558Srgrimes } 6291558Srgrimes } 6301558Srgrimes return (line); 6311558Srgrimes} 6321558Srgrimes 6331558Srgrimes/* 6341558Srgrimes * Return the name of the network whose address is given. 635243019Sglebius * The address is assumed to be that of a net, not a host. 6361558Srgrimes */ 637253519Shrsstatic const char * 638196527Scharniernetname(struct sockaddr *sa) 6391558Srgrimes{ 640253517Shrs struct sockaddr_dl *sdl; 64119209Sfenner static char line[MAXHOSTNAMELEN + 1]; 642253427Shrs int n; 643253427Shrs#ifdef INET 644204406Suqs struct netent *np = NULL; 645253427Shrs const char *cp = NULL; 64692806Sobrien u_long i; 647253427Shrs#endif 6481558Srgrimes 6491558Srgrimes switch (sa->sa_family) { 650253427Shrs#ifdef INET 651253427Shrs case AF_INET: 652253427Shrs { 653253427Shrs struct in_addr in; 6541558Srgrimes 655253502Shrs in = ((struct sockaddr_in *)(void *)sa)->sin_addr; 6561558Srgrimes i = in.s_addr = ntohl(in.s_addr); 6571558Srgrimes if (in.s_addr == 0) 6581558Srgrimes cp = "default"; 6591558Srgrimes else if (!nflag) { 660243019Sglebius np = getnetbyaddr(i, AF_INET); 661204406Suqs if (np != NULL) 6621558Srgrimes cp = np->n_name; 6631558Srgrimes } 66477873Sru#define C(x) (unsigned)((x) & 0xff) 665204406Suqs if (cp != NULL) 66619209Sfenner strncpy(line, cp, sizeof(line)); 6671558Srgrimes else if ((in.s_addr & 0xffffff) == 0) 668253427Shrs (void)sprintf(line, "%u", C(in.s_addr >> 24)); 6691558Srgrimes else if ((in.s_addr & 0xffff) == 0) 670253427Shrs (void)sprintf(line, "%u.%u", C(in.s_addr >> 24), 6711558Srgrimes C(in.s_addr >> 16)); 6721558Srgrimes else if ((in.s_addr & 0xff) == 0) 673253427Shrs (void)sprintf(line, "%u.%u.%u", C(in.s_addr >> 24), 6741558Srgrimes C(in.s_addr >> 16), C(in.s_addr >> 8)); 6751558Srgrimes else 676253427Shrs (void)sprintf(line, "%u.%u.%u.%u", C(in.s_addr >> 24), 6771558Srgrimes C(in.s_addr >> 16), C(in.s_addr >> 8), 6781558Srgrimes C(in.s_addr)); 67977873Sru#undef C 6801558Srgrimes break; 681253427Shrs } 682253427Shrs#endif 68354263Sshin#ifdef INET6 68454263Sshin case AF_INET6: 68578064Sume { 686253427Shrs struct sockaddr_in6 sin6; 68778064Sume int niflags = 0; 68854263Sshin 68978064Sume memset(&sin6, 0, sizeof(sin6)); 69078064Sume memcpy(&sin6, sa, sa->sa_len); 691253427Shrs sin6.sin6_len = sizeof(sin6); 69278064Sume sin6.sin6_family = AF_INET6; 69378064Sume if (nflag) 69478064Sume niflags |= NI_NUMERICHOST; 69578064Sume if (getnameinfo((struct sockaddr *)&sin6, sin6.sin6_len, 69678064Sume line, sizeof(line), NULL, 0, niflags) != 0) 69778064Sume strncpy(line, "invalid", sizeof(line)); 69878064Sume 69978064Sume return(line); 70078064Sume } 70178064Sume#endif 70278064Sume 70317046Sjulian case AF_APPLETALK: 704253427Shrs (void)snprintf(line, sizeof(line), "atalk %s", 705253502Shrs atalk_ntoa(((struct sockaddr_at *)(void *)sa)->sat_addr)); 70617046Sjulian break; 70717046Sjulian 7081558Srgrimes case AF_LINK: 709253517Shrs sdl = (struct sockaddr_dl *)(void *)sa; 710253517Shrs 711253517Shrs if (sdl->sdl_nlen == 0 && 712253517Shrs sdl->sdl_alen == 0 && 713253517Shrs sdl->sdl_slen == 0) { 714253517Shrs n = snprintf(line, sizeof(line), "link#%d", 715253517Shrs sdl->sdl_index); 716253517Shrs if (n > (int)sizeof(line)) 717253517Shrs line[0] = '\0'; 718253517Shrs return (line); 719253517Shrs } else 720253517Shrs return (link_ntoa(sdl)); 721253427Shrs break; 7221558Srgrimes 7231558Srgrimes default: 724204406Suqs { 725253502Shrs u_short *sp = (u_short *)(void *)sa->sa_data; 726204406Suqs u_short *splim = sp + ((sa->sa_len + 1)>>1); 727204406Suqs char *cps = line + sprintf(line, "af %d:", sa->sa_family); 72819209Sfenner char *cpe = line + sizeof(line); 7291558Srgrimes 730204406Suqs while (sp < splim && cps < cpe) 731204406Suqs if ((n = snprintf(cps, cpe - cps, " %x", *sp++)) > 0) 732204406Suqs cps += n; 73381980Sbrian else 734204406Suqs *cps = '\0'; 7351558Srgrimes break; 7361558Srgrimes } 7371558Srgrimes } 7381558Srgrimes return (line); 7391558Srgrimes} 7401558Srgrimes 741204406Suqsstatic void 742196527Scharnierset_metric(char *value, int key) 7431558Srgrimes{ 7441558Srgrimes int flag = 0; 745272852Shrs char *endptr; 7461558Srgrimes u_long noval, *valp = &noval; 7471558Srgrimes 7481558Srgrimes switch (key) { 7491558Srgrimes#define caseof(x, y, z) case x: valp = &rt_metrics.z; flag = y; break 7501558Srgrimes caseof(K_MTU, RTV_MTU, rmx_mtu); 7511558Srgrimes caseof(K_HOPCOUNT, RTV_HOPCOUNT, rmx_hopcount); 7521558Srgrimes caseof(K_EXPIRE, RTV_EXPIRE, rmx_expire); 7531558Srgrimes caseof(K_RECVPIPE, RTV_RPIPE, rmx_recvpipe); 7541558Srgrimes caseof(K_SENDPIPE, RTV_SPIPE, rmx_sendpipe); 7551558Srgrimes caseof(K_SSTHRESH, RTV_SSTHRESH, rmx_ssthresh); 7561558Srgrimes caseof(K_RTT, RTV_RTT, rmx_rtt); 7571558Srgrimes caseof(K_RTTVAR, RTV_RTTVAR, rmx_rttvar); 758191080Skmacy caseof(K_WEIGHT, RTV_WEIGHT, rmx_weight); 7591558Srgrimes } 7601558Srgrimes rtm_inits |= flag; 7611558Srgrimes if (lockrest || locking) 7621558Srgrimes rt_metrics.rmx_locks |= flag; 7631558Srgrimes if (locking) 7641558Srgrimes locking = 0; 765272852Shrs errno = 0; 766272852Shrs *valp = strtol(value, &endptr, 0); 767272852Shrs if (errno == 0 && *endptr != '\0') 768272852Shrs errno = EINVAL; 769272852Shrs if (errno) 770272852Shrs err(EX_USAGE, "%s", value); 771272852Shrs if (flag & RTV_EXPIRE && (value[0] == '+' || value[0] == '-')) { 772272852Shrs struct timespec ts; 773272852Shrs 774272852Shrs clock_gettime(CLOCK_REALTIME_FAST, &ts); 775272852Shrs *valp += ts.tv_sec; 776272852Shrs } 7771558Srgrimes} 7781558Srgrimes 779243185Shrs#define F_ISHOST 0x01 780243185Shrs#define F_FORCENET 0x02 781243185Shrs#define F_FORCEHOST 0x04 782243185Shrs#define F_PROXY 0x08 783243185Shrs#define F_INTERFACE 0x10 784243185Shrs 785204406Suqsstatic void 786196527Scharniernewroute(int argc, char **argv) 7871558Srgrimes{ 788243185Shrs struct hostent *hp; 789243185Shrs struct fibl *fl; 790204406Suqs char *cmd; 791243185Shrs const char *dest, *gateway, *errmsg; 792243185Shrs int key, error, flags, nrflags, fibnum; 7931558Srgrimes 794253427Shrs if (uid != 0 && !debugonly && !tflag) 79513171Swollman errx(EX_NOPERM, "must be root to alter routing table"); 796243185Shrs dest = NULL; 797243185Shrs gateway = NULL; 798243185Shrs flags = RTF_STATIC; 799243185Shrs nrflags = 0; 800243185Shrs hp = NULL; 801243185Shrs TAILQ_INIT(&fibl_head); 802243185Shrs 8031558Srgrimes cmd = argv[0]; 804191080Skmacy if (*cmd != 'g' && *cmd != 's') 805146079Sjmallett shutdown(s, SHUT_RD); /* Don't want to read back our messages */ 8061558Srgrimes while (--argc > 0) { 8071558Srgrimes if (**(++argv)== '-') { 8081558Srgrimes switch (key = keyword(1 + *argv)) { 8091558Srgrimes case K_LINK: 8101558Srgrimes af = AF_LINK; 8111558Srgrimes aflen = sizeof(struct sockaddr_dl); 8121558Srgrimes break; 813253427Shrs#ifdef INET 814265701Smelifaro case K_4: 8151558Srgrimes case K_INET: 8161558Srgrimes af = AF_INET; 8171558Srgrimes aflen = sizeof(struct sockaddr_in); 8181558Srgrimes break; 819253427Shrs#endif 82054263Sshin#ifdef INET6 821265701Smelifaro case K_6: 82254263Sshin case K_INET6: 82354263Sshin af = AF_INET6; 82454263Sshin aflen = sizeof(struct sockaddr_in6); 82554263Sshin break; 82654263Sshin#endif 82717046Sjulian case K_ATALK: 82817046Sjulian af = AF_APPLETALK; 82917046Sjulian aflen = sizeof(struct sockaddr_at); 83017046Sjulian break; 8311558Srgrimes case K_SA: 8321558Srgrimes af = PF_ROUTE; 833253427Shrs aflen = sizeof(struct sockaddr_storage); 8341558Srgrimes break; 8351558Srgrimes case K_IFACE: 8361558Srgrimes case K_INTERFACE: 837243185Shrs nrflags |= F_INTERFACE; 8382787Spst break; 8391558Srgrimes case K_NOSTATIC: 8401558Srgrimes flags &= ~RTF_STATIC; 8411558Srgrimes break; 8421558Srgrimes case K_LOCK: 8431558Srgrimes locking = 1; 8441558Srgrimes break; 8451558Srgrimes case K_LOCKREST: 8461558Srgrimes lockrest = 1; 8471558Srgrimes break; 8481558Srgrimes case K_HOST: 849243185Shrs nrflags |= F_FORCEHOST; 8501558Srgrimes break; 8511558Srgrimes case K_REJECT: 8521558Srgrimes flags |= RTF_REJECT; 8531558Srgrimes break; 8541558Srgrimes case K_BLACKHOLE: 8551558Srgrimes flags |= RTF_BLACKHOLE; 8561558Srgrimes break; 8571558Srgrimes case K_PROTO1: 8581558Srgrimes flags |= RTF_PROTO1; 8591558Srgrimes break; 8601558Srgrimes case K_PROTO2: 8611558Srgrimes flags |= RTF_PROTO2; 8621558Srgrimes break; 863272852Shrs case K_PROTO3: 864272852Shrs flags |= RTF_PROTO3; 865272852Shrs break; 86678140Sru case K_PROXY: 867243185Shrs nrflags |= F_PROXY; 86878140Sru break; 8691558Srgrimes case K_XRESOLVE: 8701558Srgrimes flags |= RTF_XRESOLVE; 8711558Srgrimes break; 8721558Srgrimes case K_STATIC: 8731558Srgrimes flags |= RTF_STATIC; 8741558Srgrimes break; 875191080Skmacy case K_STICKY: 876191080Skmacy flags |= RTF_STICKY; 877191080Skmacy break; 878191080Skmacy case K_NOSTICK: 879191080Skmacy flags &= ~RTF_STICKY; 880191080Skmacy break; 881243185Shrs case K_FIB: 882243185Shrs if (!--argc) 883243185Shrs usage(NULL); 884243185Shrs error = fiboptlist_csv(*++argv, &fibl_head); 885243185Shrs if (error) 886244325Shrs errx(EX_USAGE, 887244325Shrs "invalid fib number: %s", *argv); 888243185Shrs break; 8891558Srgrimes case K_IFA: 89047668Sru if (!--argc) 891204406Suqs usage(NULL); 892253504Shrs getaddr(RTAX_IFA, *++argv, 0, nrflags); 8931558Srgrimes break; 8941558Srgrimes case K_IFP: 89547668Sru if (!--argc) 896204406Suqs usage(NULL); 897253504Shrs getaddr(RTAX_IFP, *++argv, 0, nrflags); 8981558Srgrimes break; 8991558Srgrimes case K_GENMASK: 90047668Sru if (!--argc) 901204406Suqs usage(NULL); 902253504Shrs getaddr(RTAX_GENMASK, *++argv, 0, nrflags); 9031558Srgrimes break; 9041558Srgrimes case K_GATEWAY: 90547668Sru if (!--argc) 906204406Suqs usage(NULL); 907253504Shrs getaddr(RTAX_GATEWAY, *++argv, 0, nrflags); 908251581Shrs gateway = *argv; 9091558Srgrimes break; 9101558Srgrimes case K_DST: 91147668Sru if (!--argc) 912204406Suqs usage(NULL); 913253504Shrs if (getaddr(RTAX_DST, *++argv, &hp, nrflags)) 914243185Shrs nrflags |= F_ISHOST; 9151558Srgrimes dest = *argv; 9161558Srgrimes break; 9171558Srgrimes case K_NETMASK: 91847668Sru if (!--argc) 919204406Suqs usage(NULL); 920253504Shrs getaddr(RTAX_NETMASK, *++argv, 0, nrflags); 9211558Srgrimes /* FALLTHROUGH */ 9221558Srgrimes case K_NET: 923243185Shrs nrflags |= F_FORCENET; 9241558Srgrimes break; 92554263Sshin case K_PREFIXLEN: 92654263Sshin if (!--argc) 927204406Suqs usage(NULL); 92854263Sshin if (prefixlen(*++argv) == -1) { 929243185Shrs nrflags &= ~F_FORCENET; 930243185Shrs nrflags |= F_ISHOST; 93154263Sshin } else { 932243185Shrs nrflags |= F_FORCENET; 933243185Shrs nrflags &= ~F_ISHOST; 93454263Sshin } 93554263Sshin break; 9361558Srgrimes case K_MTU: 9371558Srgrimes case K_HOPCOUNT: 9381558Srgrimes case K_EXPIRE: 9391558Srgrimes case K_RECVPIPE: 9401558Srgrimes case K_SENDPIPE: 9411558Srgrimes case K_SSTHRESH: 9421558Srgrimes case K_RTT: 9431558Srgrimes case K_RTTVAR: 944191080Skmacy case K_WEIGHT: 94547668Sru if (!--argc) 946204406Suqs usage(NULL); 9471558Srgrimes set_metric(*++argv, key); 9481558Srgrimes break; 9491558Srgrimes default: 9501558Srgrimes usage(1+*argv); 9511558Srgrimes } 9521558Srgrimes } else { 9531558Srgrimes if ((rtm_addrs & RTA_DST) == 0) { 9541558Srgrimes dest = *argv; 955253504Shrs if (getaddr(RTAX_DST, *argv, &hp, nrflags)) 956243185Shrs nrflags |= F_ISHOST; 9571558Srgrimes } else if ((rtm_addrs & RTA_GATEWAY) == 0) { 9581558Srgrimes gateway = *argv; 959253504Shrs getaddr(RTAX_GATEWAY, *argv, &hp, nrflags); 9601558Srgrimes } else { 961253504Shrs getaddr(RTAX_NETMASK, *argv, 0, nrflags); 962243185Shrs nrflags |= F_FORCENET; 9631558Srgrimes } 9641558Srgrimes } 9651558Srgrimes } 966243185Shrs 967256137Sglebius if (so[RTAX_DST].ss_len == 0) { 968256137Sglebius warnx("destination parameter required"); 969256137Sglebius usage(NULL); 970256137Sglebius } 971256137Sglebius 972243185Shrs if (nrflags & F_FORCEHOST) { 973243185Shrs nrflags |= F_ISHOST; 97454263Sshin#ifdef INET6 97554263Sshin if (af == AF_INET6) { 97654263Sshin rtm_addrs &= ~RTA_NETMASK; 977253427Shrs memset(&so[RTAX_NETMASK], 0, sizeof(so[RTAX_NETMASK])); 97854263Sshin } 979204406Suqs#endif 98054263Sshin } 981243185Shrs if (nrflags & F_FORCENET) 982243185Shrs nrflags &= ~F_ISHOST; 9831558Srgrimes flags |= RTF_UP; 984243185Shrs if (nrflags & F_ISHOST) 9851558Srgrimes flags |= RTF_HOST; 986243185Shrs if ((nrflags & F_INTERFACE) == 0) 9871558Srgrimes flags |= RTF_GATEWAY; 988246143Sglebius if (nrflags & F_PROXY) 98978140Sru flags |= RTF_ANNOUNCE; 990243185Shrs if (dest == NULL) 991243185Shrs dest = ""; 992243185Shrs if (gateway == NULL) 993243185Shrs gateway = ""; 994243185Shrs 995243185Shrs if (TAILQ_EMPTY(&fibl_head)) { 996243185Shrs error = fiboptlist_csv("default", &fibl_head); 997243185Shrs if (error) 998243185Shrs errx(EX_OSERR, "fiboptlist_csv failed."); 9991558Srgrimes } 1000243185Shrs error = 0; 1001243185Shrs TAILQ_FOREACH(fl, &fibl_head, fl_next) { 1002243185Shrs fl->fl_error = newroute_fib(fl->fl_num, cmd, flags); 1003243185Shrs if (fl->fl_error) 1004243185Shrs fl->fl_errno = errno; 1005243185Shrs error += fl->fl_error; 1006243185Shrs } 1007191080Skmacy if (*cmd == 'g' || *cmd == 's') 1008243185Shrs exit(error); 1009243185Shrs 1010243185Shrs error = 0; 101197278Sru if (!qflag) { 1012243185Shrs fibnum = 0; 1013243185Shrs TAILQ_FOREACH(fl, &fibl_head, fl_next) { 1014243185Shrs if (fl->fl_error == 0) 1015243185Shrs fibnum++; 10161558Srgrimes } 1017243185Shrs if (fibnum > 0) { 1018243185Shrs int firstfib = 1; 1019243185Shrs 1020243185Shrs printf("%s %s %s", cmd, 1021243185Shrs (nrflags & F_ISHOST) ? "host" : "net", dest); 1022243185Shrs if (*gateway) 1023243185Shrs printf(": gateway %s", gateway); 1024243185Shrs 1025243185Shrs if (numfibs > 1) { 1026243185Shrs TAILQ_FOREACH(fl, &fibl_head, fl_next) { 1027243185Shrs if (fl->fl_error == 0 1028243185Shrs && fl->fl_num >= 0) { 1029243185Shrs if (firstfib) { 1030243185Shrs printf(" fib "); 1031243185Shrs firstfib = 0; 1032243185Shrs } 1033243185Shrs printf("%d", fl->fl_num); 1034243185Shrs if (fibnum-- > 1) 1035243185Shrs printf(","); 1036243185Shrs } 1037243185Shrs } 103897278Sru } 1039243185Shrs printf("\n"); 104097278Sru } 1041243185Shrs 1042243185Shrs fibnum = 0; 1043243185Shrs TAILQ_FOREACH(fl, &fibl_head, fl_next) { 1044243185Shrs if (fl->fl_error != 0) { 1045243185Shrs printf("%s %s %s", cmd, (nrflags & F_ISHOST) 1046243185Shrs ? "host" : "net", dest); 1047243185Shrs if (*gateway) 1048243185Shrs printf(": gateway %s", gateway); 1049243185Shrs 1050243185Shrs if (fl->fl_num >= 0) 1051243185Shrs printf(" fib %d", fl->fl_num); 1052243185Shrs 1053243185Shrs switch (fl->fl_errno) { 1054243185Shrs case ESRCH: 1055243185Shrs errmsg = "not in table"; 1056243185Shrs break; 1057243185Shrs case EBUSY: 1058243185Shrs errmsg = "entry in use"; 1059243185Shrs break; 1060243185Shrs case ENOBUFS: 1061243185Shrs errmsg = "not enough memory"; 1062243185Shrs break; 1063243185Shrs case EADDRINUSE: 1064243185Shrs /* 1065243185Shrs * handle recursion avoidance 1066243185Shrs * in rt_setgate() 1067243185Shrs */ 1068243185Shrs errmsg = "gateway uses the same route"; 1069243185Shrs break; 1070243185Shrs case EEXIST: 1071243185Shrs errmsg = "route already in table"; 1072243185Shrs break; 1073243185Shrs default: 1074243185Shrs errmsg = strerror(fl->fl_errno); 1075243185Shrs break; 1076243185Shrs } 1077243185Shrs printf(": %s\n", errmsg); 1078243185Shrs error = 1; 1079243185Shrs } 1080243185Shrs } 10811558Srgrimes } 1082243185Shrs exit(error); 10831558Srgrimes} 10841558Srgrimes 1085243185Shrsstatic int 1086243185Shrsnewroute_fib(int fib, char *cmd, int flags) 1087243185Shrs{ 1088243185Shrs int error; 1089243185Shrs 1090243185Shrs error = set_sofib(fib); 1091243185Shrs if (error) { 1092243185Shrs warn("fib number %d is ignored", fib); 1093243185Shrs return (error); 1094243185Shrs } 1095243185Shrs 1096243185Shrs error = rtmsg(*cmd, flags, fib); 1097243185Shrs return (error); 1098243185Shrs} 1099243185Shrs 1100253427Shrs#ifdef INET 1101204406Suqsstatic void 1102253427Shrsinet_makenetandmask(u_long net, struct sockaddr_in *sin, 1103253427Shrs struct sockaddr_in *sin_mask, u_long bits) 11041558Srgrimes{ 1105243019Sglebius u_long mask = 0; 11061558Srgrimes 11071558Srgrimes rtm_addrs |= RTA_NETMASK; 1108243019Sglebius 1109204406Suqs /* 1110243867Sglebius * MSB of net should be meaningful. 0/0 is exception. 1111243867Sglebius */ 1112243867Sglebius if (net > 0) 1113243867Sglebius while ((net & 0xff000000) == 0) 1114243867Sglebius net <<= 8; 1115243867Sglebius 1116243867Sglebius /* 1117204406Suqs * If no /xx was specified we must calculate the 1118190758Srrs * CIDR address. 1119190758Srrs */ 1120243019Sglebius if ((bits == 0) && (net != 0)) { 1121190775Srrs u_long i, j; 1122253427Shrs 1123253427Shrs for(i = 0, j = 0xff; i < 4; i++) { 1124243019Sglebius if (net & j) { 1125190758Srrs break; 1126190758Srrs } 1127190775Srrs j <<= 8; 1128190758Srrs } 1129190758Srrs /* i holds the first non zero bit */ 1130316042Sngie bits = 32 - (i*8); 1131190758Srrs } 1132190913Srrs if (bits != 0) 1133190913Srrs mask = 0xffffffff << (32 - bits); 1134173124Smtm 1135243019Sglebius sin->sin_addr.s_addr = htonl(net); 1136253427Shrs sin_mask->sin_addr.s_addr = htonl(mask); 1137253427Shrs sin_mask->sin_len = sizeof(struct sockaddr_in); 1138253427Shrs sin_mask->sin_family = AF_INET; 11391558Srgrimes} 1140253427Shrs#endif 11411558Srgrimes 114296997Sume#ifdef INET6 11431558Srgrimes/* 114496997Sume * XXX the function may need more improvement... 114596997Sume */ 114697062Sumestatic int 1147204406Suqsinet6_makenetandmask(struct sockaddr_in6 *sin6, const char *plen) 114896997Sume{ 114996997Sume struct in6_addr in6; 115096997Sume 1151204406Suqs if (plen == NULL) { 115297073Sume if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr) && 115397073Sume sin6->sin6_scope_id == 0) { 115497073Sume plen = "0"; 115597073Sume } else if ((sin6->sin6_addr.s6_addr[0] & 0xe0) == 0x20) { 115697073Sume /* aggregatable global unicast - RFC2374 */ 115797073Sume memset(&in6, 0, sizeof(in6)); 115897073Sume if (!memcmp(&sin6->sin6_addr.s6_addr[8], 115997073Sume &in6.s6_addr[8], 8)) 116097073Sume plen = "64"; 116197073Sume } 116296997Sume } 116396997Sume 1164204406Suqs if (plen == NULL || strcmp(plen, "128") == 0) 1165204406Suqs return (1); 116698053Sume rtm_addrs |= RTA_NETMASK; 1167204406Suqs prefixlen(plen); 1168204406Suqs return (0); 116996997Sume} 117096997Sume#endif 117196997Sume 117296997Sume/* 11731558Srgrimes * Interpret an argument as a network address of some kind, 11741558Srgrimes * returning 1 if a host address, 0 if a network address. 11751558Srgrimes */ 1176204406Suqsstatic int 1177253504Shrsgetaddr(int idx, char *str, struct hostent **hpp, int nrflags) 11781558Srgrimes{ 1179253427Shrs struct sockaddr *sa; 1180253427Shrs#if defined(INET) 1181253427Shrs struct sockaddr_in *sin; 11821558Srgrimes struct hostent *hp; 11831558Srgrimes struct netent *np; 11841558Srgrimes u_long val; 118566449Sru char *q; 1186253427Shrs#elif defined(INET6) 1187253427Shrs char *q; 1188253427Shrs#endif 11891558Srgrimes 1190253852Shrs if (idx < 0 || idx >= RTAX_MAX) 1191253852Shrs usage("internal error"); 11921558Srgrimes if (af == 0) { 1193253427Shrs#if defined(INET) 11941558Srgrimes af = AF_INET; 11951558Srgrimes aflen = sizeof(struct sockaddr_in); 1196253427Shrs#elif defined(INET6) 1197253427Shrs af = AF_INET6; 1198253427Shrs aflen = sizeof(struct sockaddr_in6); 1199253427Shrs#else 1200253427Shrs af = AF_LINK; 1201253427Shrs aflen = sizeof(struct sockaddr_dl); 1202253427Shrs#endif 12031558Srgrimes } 1204253519Shrs#ifndef INET 1205253519Shrs hpp = NULL; 1206253519Shrs#endif 1207253504Shrs rtm_addrs |= (1 << idx); 1208253504Shrs sa = (struct sockaddr *)&so[idx]; 1209253427Shrs sa->sa_family = af; 1210253427Shrs sa->sa_len = aflen; 1211253427Shrs 1212253504Shrs switch (idx) { 1213253504Shrs case RTAX_GATEWAY: 1214245168Shrs if (nrflags & F_INTERFACE) { 121578064Sume struct ifaddrs *ifap, *ifa; 1216253502Shrs struct sockaddr_dl *sdl0 = (struct sockaddr_dl *)(void *)sa; 121778064Sume struct sockaddr_dl *sdl = NULL; 121817486Sjulian 121978064Sume if (getifaddrs(&ifap)) 1220253427Shrs err(EX_OSERR, "getifaddrs"); 122117486Sjulian 1222204406Suqs for (ifa = ifap; ifa != NULL; ifa = ifa->ifa_next) { 122378064Sume if (ifa->ifa_addr->sa_family != AF_LINK) 122478064Sume continue; 122517486Sjulian 1226204406Suqs if (strcmp(str, ifa->ifa_name) != 0) 122778064Sume continue; 122878064Sume 1229253502Shrs sdl = (struct sockaddr_dl *)(void *)ifa->ifa_addr; 123017486Sjulian } 123117486Sjulian /* If we found it, then use it */ 1232204406Suqs if (sdl != NULL) { 123378064Sume /* 123478064Sume * Note that we need to copy before calling 123578064Sume * freeifaddrs(). 123678064Sume */ 1237253427Shrs memcpy(sdl0, sdl, sdl->sdl_len); 123878064Sume } 123978064Sume freeifaddrs(ifap); 1240204406Suqs if (sdl != NULL) 124117486Sjulian return(1); 1242293208Srstone else 1243293208Srstone errx(EX_DATAERR, 1244293208Srstone "interface '%s' does not exist", str); 124517486Sjulian } 12461558Srgrimes break; 1247253504Shrs case RTAX_IFP: 1248253427Shrs sa->sa_family = AF_LINK; 12491558Srgrimes break; 12501558Srgrimes } 1251204406Suqs if (strcmp(str, "default") == 0) { 125227500Sjulian /* 1253204406Suqs * Default is net 0.0.0.0/0 125427500Sjulian */ 1255253504Shrs switch (idx) { 1256253504Shrs case RTAX_DST: 1257270050Sbz nrflags |= F_FORCENET; 1258253504Shrs getaddr(RTAX_NETMASK, str, 0, nrflags); 12591558Srgrimes break; 12601558Srgrimes } 12611558Srgrimes return (0); 12621558Srgrimes } 1263253427Shrs switch (sa->sa_family) { 126454263Sshin#ifdef INET6 126554263Sshin case AF_INET6: 126678064Sume { 126757108Sshin struct addrinfo hints, *res; 1268146546Sume int ecode; 126957108Sshin 127097073Sume q = NULL; 1271253504Shrs if (idx == RTAX_DST && (q = strchr(str, '/')) != NULL) 127297073Sume *q = '\0'; 127378064Sume memset(&hints, 0, sizeof(hints)); 1274253427Shrs hints.ai_family = sa->sa_family; 1275253427Shrs hints.ai_socktype = SOCK_DGRAM; 1276204406Suqs ecode = getaddrinfo(str, NULL, &hints, &res); 1277146546Sume if (ecode != 0 || res->ai_family != AF_INET6 || 1278253427Shrs res->ai_addrlen != sizeof(struct sockaddr_in6)) 1279253427Shrs errx(EX_OSERR, "%s: %s", str, gai_strerror(ecode)); 1280253427Shrs memcpy(sa, res->ai_addr, res->ai_addrlen); 128197062Sume freeaddrinfo(res); 128297073Sume if (q != NULL) 128397073Sume *q++ = '/'; 1284253504Shrs if (idx == RTAX_DST) 1285253502Shrs return (inet6_makenetandmask((struct sockaddr_in6 *)(void *)sa, q)); 128678064Sume return (0); 128778064Sume } 128878064Sume#endif /* INET6 */ 128954263Sshin 129017046Sjulian case AF_APPLETALK: 1291253427Shrs { 1292253502Shrs struct sockaddr_at *sat = (struct sockaddr_at *)(void *)sa; 1293253427Shrs 1294253427Shrs if (!atalk_aton(str, &sat->sat_addr)) 1295204406Suqs errx(EX_NOHOST, "bad address: %s", str); 129617265Sjulian rtm_addrs |= RTA_NETMASK; 1297270050Sbz return(nrflags & F_FORCEHOST || sat->sat_addr.s_node != 0); 1298253427Shrs } 12991558Srgrimes case AF_LINK: 1300253502Shrs link_addr(str, (struct sockaddr_dl *)(void *)sa); 13011558Srgrimes return (1); 13021558Srgrimes 13031558Srgrimes case PF_ROUTE: 1304253427Shrs sockaddr(str, sa, sizeof(struct sockaddr_storage)); 13051558Srgrimes return (1); 1306253427Shrs#ifdef INET 13071558Srgrimes case AF_INET: 1308253427Shrs#endif 13091558Srgrimes default: 13101558Srgrimes break; 13111558Srgrimes } 13121558Srgrimes 1313253427Shrs#ifdef INET 1314253502Shrs sin = (struct sockaddr_in *)(void *)sa; 13151558Srgrimes if (hpp == NULL) 13161558Srgrimes hpp = &hp; 13171558Srgrimes *hpp = NULL; 131824558Sphk 1319204406Suqs q = strchr(str,'/'); 1320253504Shrs if (q != NULL && idx == RTAX_DST) { 132124558Sphk *q = '\0'; 1322204406Suqs if ((val = inet_network(str)) != INADDR_NONE) { 1323253427Shrs inet_makenetandmask(val, sin, 1324253427Shrs (struct sockaddr_in *)&so[RTAX_NETMASK], 1325253427Shrs strtoul(q+1, 0, 0)); 132624558Sphk return (0); 132724558Sphk } 132866449Sru *q = '/'; 132924558Sphk } 1330270050Sbz if ((idx != RTAX_DST || (nrflags & F_FORCENET) == 0) && 1331253427Shrs inet_aton(str, &sin->sin_addr)) { 1332253427Shrs val = sin->sin_addr.s_addr; 1333270050Sbz if (idx != RTAX_DST || nrflags & F_FORCEHOST || 1334253427Shrs inet_lnaof(sin->sin_addr) != INADDR_ANY) 13351558Srgrimes return (1); 13361558Srgrimes else { 13371558Srgrimes val = ntohl(val); 13381558Srgrimes goto netdone; 13391558Srgrimes } 13401558Srgrimes } 1341270050Sbz if (idx == RTAX_DST && (nrflags & F_FORCEHOST) == 0 && 1342204406Suqs ((val = inet_network(str)) != INADDR_NONE || 1343204406Suqs ((np = getnetbyname(str)) != NULL && (val = np->n_net) != 0))) { 13441558Srgrimesnetdone: 1345253427Shrs inet_makenetandmask(val, sin, 1346253427Shrs (struct sockaddr_in *)&so[RTAX_NETMASK], 0); 13471558Srgrimes return (0); 13481558Srgrimes } 1349204406Suqs hp = gethostbyname(str); 1350204406Suqs if (hp != NULL) { 13511558Srgrimes *hpp = hp; 1352253427Shrs sin->sin_family = hp->h_addrtype; 1353253427Shrs memmove((char *)&sin->sin_addr, hp->h_addr, 1354253427Shrs MIN((size_t)hp->h_length, sizeof(sin->sin_addr))); 13551558Srgrimes return (1); 13561558Srgrimes } 1357253427Shrs#endif 1358204406Suqs errx(EX_NOHOST, "bad address: %s", str); 13591558Srgrimes} 13601558Srgrimes 1361204406Suqsstatic int 1362204406Suqsprefixlen(const char *str) 136354263Sshin{ 1364204406Suqs int len = atoi(str), q, r; 136554263Sshin int max; 136654263Sshin char *p; 13671558Srgrimes 1368316042Sngie rtm_addrs |= RTA_NETMASK; 136954263Sshin switch (af) { 137054263Sshin#ifdef INET6 137154263Sshin case AF_INET6: 1372253427Shrs { 1373253427Shrs struct sockaddr_in6 *sin6 = 1374253427Shrs (struct sockaddr_in6 *)&so[RTAX_NETMASK]; 1375253427Shrs 137654263Sshin max = 128; 1377253427Shrs p = (char *)&sin6->sin6_addr; 1378253427Shrs sin6->sin6_family = AF_INET6; 1379253427Shrs sin6->sin6_len = sizeof(*sin6); 138054263Sshin break; 1381253427Shrs } 138254263Sshin#endif 1383253427Shrs#ifdef INET 138454263Sshin case AF_INET: 1385253427Shrs { 1386253427Shrs struct sockaddr_in *sin = 1387253427Shrs (struct sockaddr_in *)&so[RTAX_NETMASK]; 1388253427Shrs 138954263Sshin max = 32; 1390253427Shrs p = (char *)&sin->sin_addr; 1391253427Shrs sin->sin_family = AF_INET; 1392253427Shrs sin->sin_len = sizeof(*sin); 139354263Sshin break; 1394253427Shrs } 1395253427Shrs#endif 139654263Sshin default: 1397253427Shrs errx(EX_OSERR, "prefixlen not supported in this af"); 139854263Sshin } 139954263Sshin 1400253427Shrs if (len < 0 || max < len) 1401253427Shrs errx(EX_USAGE, "%s: invalid prefixlen", str); 1402316042Sngie 140354263Sshin q = len >> 3; 140454263Sshin r = len & 7; 140554263Sshin memset((void *)p, 0, max / 8); 140654263Sshin if (q > 0) 140754263Sshin memset((void *)p, 0xff, q); 140854263Sshin if (r > 0) 140954263Sshin *((u_char *)p + q) = (0xff00 >> r) & 0xff; 141054263Sshin if (len == max) 1411204406Suqs return (-1); 141254263Sshin else 1413204406Suqs return (len); 141454263Sshin} 141554263Sshin 1416204406Suqsstatic void 1417196527Scharnierinterfaces(void) 14181558Srgrimes{ 14191558Srgrimes size_t needed; 14201558Srgrimes int mib[6]; 1421128782Sambrisko char *buf, *lim, *next, count = 0; 142292806Sobrien struct rt_msghdr *rtm; 14231558Srgrimes 1424128782Sambriskoretry2: 14251558Srgrimes mib[0] = CTL_NET; 14261558Srgrimes mib[1] = PF_ROUTE; 14271558Srgrimes mib[2] = 0; /* protocol */ 1428253427Shrs mib[3] = AF_UNSPEC; 14291558Srgrimes mib[4] = NET_RT_IFLIST; 14301558Srgrimes mib[5] = 0; /* no flags */ 1431253427Shrs if (sysctl(mib, nitems(mib), NULL, &needed, NULL, 0) < 0) 143213171Swollman err(EX_OSERR, "route-sysctl-estimate"); 14331558Srgrimes if ((buf = malloc(needed)) == NULL) 143437907Scharnier errx(EX_OSERR, "malloc failed"); 1435253427Shrs if (sysctl(mib, nitems(mib), buf, &needed, NULL, 0) < 0) { 1436128782Sambrisko if (errno == ENOMEM && count++ < 10) { 1437128782Sambrisko warnx("Routing table grew, retrying"); 1438128782Sambrisko sleep(1); 1439128782Sambrisko free(buf); 1440128782Sambrisko goto retry2; 1441128782Sambrisko } 144213171Swollman err(EX_OSERR, "actual retrieval of interface table"); 1443128782Sambrisko } 14441558Srgrimes lim = buf + needed; 14451558Srgrimes for (next = buf; next < lim; next += rtm->rtm_msglen) { 1446253502Shrs rtm = (struct rt_msghdr *)(void *)next; 14471558Srgrimes print_rtmsg(rtm, rtm->rtm_msglen); 14481558Srgrimes } 14491558Srgrimes} 14501558Srgrimes 1451204406Suqsstatic void 1452243185Shrsmonitor(int argc, char *argv[]) 14531558Srgrimes{ 1454243185Shrs int n, fib, error; 1455243185Shrs char msg[2048], *endptr; 14561558Srgrimes 1457243185Shrs fib = defaultfib; 1458243185Shrs while (argc > 1) { 1459243185Shrs argc--; 1460243185Shrs argv++; 1461243185Shrs if (**argv != '-') 1462243185Shrs usage(*argv); 1463243185Shrs switch (keyword(*argv + 1)) { 1464243185Shrs case K_FIB: 1465243185Shrs if (!--argc) 1466243185Shrs usage(*argv); 1467244325Shrs errno = 0; 1468243185Shrs fib = strtol(*++argv, &endptr, 0); 1469244325Shrs if (errno == 0) { 1470244325Shrs if (*endptr != '\0' || 1471244325Shrs fib < 0 || 1472244325Shrs (numfibs != -1 && fib > numfibs - 1)) 1473244325Shrs errno = EINVAL; 1474244325Shrs } 1475244325Shrs if (errno) 1476244325Shrs errx(EX_USAGE, "invalid fib number: %s", *argv); 1477243185Shrs break; 1478243185Shrs default: 1479243185Shrs usage(*argv); 1480243185Shrs } 1481243185Shrs } 1482243185Shrs error = set_sofib(fib); 1483243185Shrs if (error) 1484243185Shrs errx(EX_USAGE, "invalid fib number: %d", fib); 1485243185Shrs 14861558Srgrimes verbose = 1; 14871558Srgrimes if (debugonly) { 14881558Srgrimes interfaces(); 14891558Srgrimes exit(0); 14901558Srgrimes } 1491204406Suqs for (;;) { 149254263Sshin time_t now; 14931558Srgrimes n = read(s, msg, 2048); 149454263Sshin now = time(NULL); 1495253427Shrs (void)printf("\ngot message of size %d on %s", n, ctime(&now)); 1496253502Shrs print_rtmsg((struct rt_msghdr *)(void *)msg, n); 14971558Srgrimes } 14981558Srgrimes} 14991558Srgrimes 1500253427Shrsstatic struct { 15011558Srgrimes struct rt_msghdr m_rtm; 15021558Srgrimes char m_space[512]; 15031558Srgrimes} m_rtmsg; 15041558Srgrimes 1505204406Suqsstatic int 1506243185Shrsrtmsg(int cmd, int flags, int fib) 15071558Srgrimes{ 15081558Srgrimes static int seq; 15091558Srgrimes int rlen; 151092806Sobrien char *cp = m_rtmsg.m_space; 151192806Sobrien int l; 15121558Srgrimes 1513253427Shrs#define NEXTADDR(w, u) \ 1514253427Shrs if (rtm_addrs & (w)) { \ 1515253443Shrs l = (((struct sockaddr *)&(u))->sa_len == 0) ? \ 1516253443Shrs sizeof(long) : \ 1517253443Shrs 1 + ((((struct sockaddr *)&(u))->sa_len - 1) \ 1518253443Shrs | (sizeof(long) - 1)); \ 1519253427Shrs memmove(cp, (char *)&(u), l); \ 1520253427Shrs cp += l; \ 1521253427Shrs if (verbose) \ 1522253427Shrs sodump((struct sockaddr *)&(u), #w); \ 15231558Srgrimes } 15241558Srgrimes 15251558Srgrimes errno = 0; 152685048Sru memset(&m_rtmsg, 0, sizeof(m_rtmsg)); 15271558Srgrimes if (cmd == 'a') 15281558Srgrimes cmd = RTM_ADD; 15291558Srgrimes else if (cmd == 'c') 15301558Srgrimes cmd = RTM_CHANGE; 1531191080Skmacy else if (cmd == 'g' || cmd == 's') { 15321558Srgrimes cmd = RTM_GET; 1533253427Shrs if (so[RTAX_IFP].ss_family == 0) { 1534253427Shrs so[RTAX_IFP].ss_family = AF_LINK; 1535253427Shrs so[RTAX_IFP].ss_len = sizeof(struct sockaddr_dl); 15361558Srgrimes rtm_addrs |= RTA_IFP; 15371558Srgrimes } 1538330498Seugen } else { 15391558Srgrimes cmd = RTM_DELETE; 1540330498Seugen flags |= RTF_PINNED; 1541330498Seugen } 15421558Srgrimes#define rtm m_rtmsg.m_rtm 15431558Srgrimes rtm.rtm_type = cmd; 15441558Srgrimes rtm.rtm_flags = flags; 15451558Srgrimes rtm.rtm_version = RTM_VERSION; 15461558Srgrimes rtm.rtm_seq = ++seq; 15471558Srgrimes rtm.rtm_addrs = rtm_addrs; 15481558Srgrimes rtm.rtm_rmx = rt_metrics; 15491558Srgrimes rtm.rtm_inits = rtm_inits; 15501558Srgrimes 1551253427Shrs NEXTADDR(RTA_DST, so[RTAX_DST]); 1552253427Shrs NEXTADDR(RTA_GATEWAY, so[RTAX_GATEWAY]); 1553253427Shrs NEXTADDR(RTA_NETMASK, so[RTAX_NETMASK]); 1554253427Shrs NEXTADDR(RTA_GENMASK, so[RTAX_GENMASK]); 1555253427Shrs NEXTADDR(RTA_IFP, so[RTAX_IFP]); 1556253427Shrs NEXTADDR(RTA_IFA, so[RTAX_IFA]); 15571558Srgrimes rtm.rtm_msglen = l = cp - (char *)&m_rtmsg; 15581558Srgrimes if (verbose) 15591558Srgrimes print_rtmsg(&rtm, l); 15601558Srgrimes if (debugonly) 15611558Srgrimes return (0); 15621558Srgrimes if ((rlen = write(s, (char *)&m_rtmsg, l)) < 0) { 1563129034Scsjp if (errno == EPERM) 1564129034Scsjp err(1, "writing to routing socket"); 156537907Scharnier warn("writing to routing socket"); 15661558Srgrimes return (-1); 15671558Srgrimes } 15681558Srgrimes if (cmd == RTM_GET) { 15691558Srgrimes do { 15701558Srgrimes l = read(s, (char *)&m_rtmsg, sizeof(m_rtmsg)); 15711558Srgrimes } while (l > 0 && (rtm.rtm_seq != seq || rtm.rtm_pid != pid)); 15721558Srgrimes if (l < 0) 157313171Swollman warn("read from routing socket"); 15741558Srgrimes else 1575243185Shrs print_getmsg(&rtm, l, fib); 15761558Srgrimes } 15771558Srgrimes#undef rtm 15781558Srgrimes return (0); 15791558Srgrimes} 15801558Srgrimes 1581253427Shrsstatic const char *msgtypes[] = { 15821558Srgrimes "", 15831558Srgrimes "RTM_ADD: Add Route", 15841558Srgrimes "RTM_DELETE: Delete Route", 15851558Srgrimes "RTM_CHANGE: Change Metrics or flags", 15861558Srgrimes "RTM_GET: Report Metrics", 15871558Srgrimes "RTM_LOSING: Kernel Suspects Partitioning", 15881558Srgrimes "RTM_REDIRECT: Told to use different route", 15891558Srgrimes "RTM_MISS: Lookup failed on this address", 15901558Srgrimes "RTM_LOCK: fix specified metrics", 15911558Srgrimes "RTM_OLDADD: caused by SIOCADDRT", 15921558Srgrimes "RTM_OLDDEL: caused by SIOCDELRT", 15931558Srgrimes "RTM_RESOLVE: Route created by cloning", 15941558Srgrimes "RTM_NEWADDR: address being added to iface", 15951558Srgrimes "RTM_DELADDR: address being removed from iface", 15961558Srgrimes "RTM_IFINFO: iface status change", 159721465Swollman "RTM_NEWMADDR: new multicast group membership on iface", 159821465Swollman "RTM_DELMADDR: multicast group membership removed from iface", 159989498Sru "RTM_IFANNOUNCE: interface arrival/departure", 1600216296Sglebius "RTM_IEEE80211: IEEE 802.11 wireless event", 16011558Srgrimes}; 16021558Srgrimes 1603253427Shrsstatic const char metricnames[] = 1604253427Shrs "\011weight\010rttvar\7rtt\6ssthresh\5sendpipe\4recvpipe\3expire" 1605253427Shrs "\1mtu"; 1606253427Shrsstatic const char routeflags[] = 1607253427Shrs "\1UP\2GATEWAY\3HOST\4REJECT\5DYNAMIC\6MODIFIED\7DONE" 1608253427Shrs "\012XRESOLVE\013LLINFO\014STATIC\015BLACKHOLE" 1609253427Shrs "\017PROTO2\020PROTO1\021PRCLONING\022WASCLONED\023PROTO3" 1610253427Shrs "\025PINNED\026LOCAL\027BROADCAST\030MULTICAST\035STICKY"; 1611253427Shrsstatic const char ifnetflags[] = 1612253427Shrs "\1UP\2BROADCAST\3DEBUG\4LOOPBACK\5PTP\6b6\7RUNNING\010NOARP" 1613253427Shrs "\011PPROMISC\012ALLMULTI\013OACTIVE\014SIMPLEX\015LINK0\016LINK1" 1614253427Shrs "\017LINK2\020MULTICAST"; 1615253427Shrsstatic const char addrnames[] = 1616253427Shrs "\1DST\2GATEWAY\3NETMASK\4GENMASK\5IFP\6IFA\7AUTHOR\010BRD"; 16171558Srgrimes 1618216297Sglebiusstatic const char errfmt[] = 1619253427Shrs "\n%s: truncated route message, only %zu bytes left\n"; 1620216297Sglebius 1621204406Suqsstatic void 1622216297Sglebiusprint_rtmsg(struct rt_msghdr *rtm, size_t msglen) 16231558Srgrimes{ 16241558Srgrimes struct if_msghdr *ifm; 16251558Srgrimes struct ifa_msghdr *ifam; 162621465Swollman#ifdef RTM_NEWMADDR 162721465Swollman struct ifma_msghdr *ifmam; 162821465Swollman#endif 162989498Sru struct if_announcemsghdr *ifan; 1630204406Suqs const char *state; 16311558Srgrimes 16321558Srgrimes if (verbose == 0) 16331558Srgrimes return; 16341558Srgrimes if (rtm->rtm_version != RTM_VERSION) { 1635253427Shrs (void)printf("routing message version %d not understood\n", 16361558Srgrimes rtm->rtm_version); 16371558Srgrimes return; 16381558Srgrimes } 1639253517Shrs if (rtm->rtm_type < nitems(msgtypes)) 164089498Sru (void)printf("%s: ", msgtypes[rtm->rtm_type]); 164189498Sru else 1642216297Sglebius (void)printf("unknown type %d: ", rtm->rtm_type); 164389498Sru (void)printf("len %d, ", rtm->rtm_msglen); 1644216297Sglebius 1645216297Sglebius#define REQUIRE(x) do { \ 1646216297Sglebius if (msglen < sizeof(x)) \ 1647216297Sglebius goto badlen; \ 1648216297Sglebius else \ 1649216297Sglebius msglen -= sizeof(x); \ 1650216297Sglebius } while (0) 1651216297Sglebius 16521558Srgrimes switch (rtm->rtm_type) { 16531558Srgrimes case RTM_IFINFO: 1654216297Sglebius REQUIRE(struct if_msghdr); 16551558Srgrimes ifm = (struct if_msghdr *)rtm; 1656253427Shrs (void)printf("if# %d, ", ifm->ifm_index); 1657128878Sandre switch (ifm->ifm_data.ifi_link_state) { 1658128878Sandre case LINK_STATE_DOWN: 1659128878Sandre state = "down"; 1660128878Sandre break; 1661128878Sandre case LINK_STATE_UP: 1662128878Sandre state = "up"; 1663128878Sandre break; 1664128878Sandre default: 1665128878Sandre state = "unknown"; 1666128878Sandre break; 1667128878Sandre } 1668253427Shrs (void)printf("link: %s, flags:", state); 1669253427Shrs printb(ifm->ifm_flags, ifnetflags); 1670216297Sglebius pmsg_addrs((char *)(ifm + 1), ifm->ifm_addrs, msglen); 16711558Srgrimes break; 16721558Srgrimes case RTM_NEWADDR: 16731558Srgrimes case RTM_DELADDR: 1674216297Sglebius REQUIRE(struct ifa_msghdr); 16751558Srgrimes ifam = (struct ifa_msghdr *)rtm; 1676253427Shrs (void)printf("metric %d, flags:", ifam->ifam_metric); 1677253427Shrs printb(ifam->ifam_flags, routeflags); 1678216297Sglebius pmsg_addrs((char *)(ifam + 1), ifam->ifam_addrs, msglen); 16791558Srgrimes break; 168021465Swollman#ifdef RTM_NEWMADDR 168121465Swollman case RTM_NEWMADDR: 168221465Swollman case RTM_DELMADDR: 1683216297Sglebius REQUIRE(struct ifma_msghdr); 168421465Swollman ifmam = (struct ifma_msghdr *)rtm; 1685216297Sglebius pmsg_addrs((char *)(ifmam + 1), ifmam->ifmam_addrs, msglen); 168621465Swollman break; 168721465Swollman#endif 168889498Sru case RTM_IFANNOUNCE: 1689216297Sglebius REQUIRE(struct if_announcemsghdr); 169089498Sru ifan = (struct if_announcemsghdr *)rtm; 1691253427Shrs (void)printf("if# %d, what: ", ifan->ifan_index); 169289498Sru switch (ifan->ifan_what) { 169389498Sru case IFAN_ARRIVAL: 1694253427Shrs (void)printf("arrival"); 169589498Sru break; 169689498Sru case IFAN_DEPARTURE: 169789498Sru printf("departure"); 169889498Sru break; 169989498Sru default: 170089498Sru printf("#%d", ifan->ifan_what); 170189498Sru break; 170289498Sru } 170389498Sru printf("\n"); 1704243860Sglebius fflush(stdout); 170589498Sru break; 170689498Sru 17071558Srgrimes default: 1708253427Shrs printf("pid: %ld, seq %d, errno %d, flags:", 170913171Swollman (long)rtm->rtm_pid, rtm->rtm_seq, rtm->rtm_errno); 1710253427Shrs printb(rtm->rtm_flags, routeflags); 1711216297Sglebius pmsg_common(rtm, msglen); 17121558Srgrimes } 1713216297Sglebius 1714216297Sglebius return; 1715216297Sglebius 1716216297Sglebiusbadlen: 1717216297Sglebius (void)printf(errfmt, __func__, msglen); 1718216297Sglebius#undef REQUIRE 17191558Srgrimes} 17201558Srgrimes 1721204406Suqsstatic void 1722243185Shrsprint_getmsg(struct rt_msghdr *rtm, int msglen, int fib) 17231558Srgrimes{ 1724253504Shrs struct sockaddr *sp[RTAX_MAX]; 1725272852Shrs struct timespec ts; 172692806Sobrien char *cp; 172792806Sobrien int i; 17281558Srgrimes 1729253504Shrs memset(sp, 0, sizeof(sp)); 1730253427Shrs (void)printf(" route to: %s\n", 1731253427Shrs routename((struct sockaddr *)&so[RTAX_DST])); 17321558Srgrimes if (rtm->rtm_version != RTM_VERSION) { 173313171Swollman warnx("routing message version %d not understood", 173413171Swollman rtm->rtm_version); 17351558Srgrimes return; 17361558Srgrimes } 17371558Srgrimes if (rtm->rtm_msglen > msglen) { 173837907Scharnier warnx("message length mismatch, in packet %d, returned %d", 173913171Swollman rtm->rtm_msglen, msglen); 1740253517Shrs return; 17411558Srgrimes } 17421558Srgrimes if (rtm->rtm_errno) { 174313171Swollman errno = rtm->rtm_errno; 174413171Swollman warn("message indicates error %d", errno); 17451558Srgrimes return; 17461558Srgrimes } 17471558Srgrimes cp = ((char *)(rtm + 1)); 1748253589Shrs for (i = 0; i < RTAX_MAX; i++) 1749253589Shrs if (rtm->rtm_addrs & (1 << i)) { 1750253504Shrs sp[i] = (struct sockaddr *)cp; 1751253589Shrs cp += SA_SIZE((struct sockaddr *)cp); 1752253589Shrs } 1753253589Shrs if ((rtm->rtm_addrs & RTA_IFP) && 1754253589Shrs (sp[RTAX_IFP]->sa_family != AF_LINK || 1755253589Shrs ((struct sockaddr_dl *)(void *)sp[RTAX_IFP])->sdl_nlen == 0)) 1756253504Shrs sp[RTAX_IFP] = NULL; 1757253504Shrs if (sp[RTAX_DST] && sp[RTAX_NETMASK]) 1758253504Shrs sp[RTAX_NETMASK]->sa_family = sp[RTAX_DST]->sa_family; /* XXX */ 1759253504Shrs if (sp[RTAX_DST]) 1760253504Shrs (void)printf("destination: %s\n", routename(sp[RTAX_DST])); 1761253504Shrs if (sp[RTAX_NETMASK]) 1762253504Shrs (void)printf(" mask: %s\n", routename(sp[RTAX_NETMASK])); 1763253504Shrs if (sp[RTAX_GATEWAY] && (rtm->rtm_flags & RTF_GATEWAY)) 1764253504Shrs (void)printf(" gateway: %s\n", routename(sp[RTAX_GATEWAY])); 1765243185Shrs if (fib >= 0) 1766243185Shrs (void)printf(" fib: %u\n", (unsigned int)fib); 1767253504Shrs if (sp[RTAX_IFP]) 17681558Srgrimes (void)printf(" interface: %.*s\n", 1769253504Shrs ((struct sockaddr_dl *)(void *)sp[RTAX_IFP])->sdl_nlen, 1770253504Shrs ((struct sockaddr_dl *)(void *)sp[RTAX_IFP])->sdl_data); 17711558Srgrimes (void)printf(" flags: "); 1772253427Shrs printb(rtm->rtm_flags, routeflags); 17731558Srgrimes 17741558Srgrimes#define lock(f) ((rtm->rtm_rmx.rmx_locks & __CONCAT(RTV_,f)) ? 'L' : ' ') 17751558Srgrimes#define msec(u) (((u) + 500) / 1000) /* usec to msec */ 1776253517Shrs printf("\n%9s %9s %9s %9s %9s %10s %9s\n", "recvpipe", 1777253517Shrs "sendpipe", "ssthresh", "rtt,msec", "mtu ", "weight", "expire"); 1778272852Shrs printf("%8lu%c ", rtm->rtm_rmx.rmx_recvpipe, lock(RPIPE)); 1779272852Shrs printf("%8lu%c ", rtm->rtm_rmx.rmx_sendpipe, lock(SPIPE)); 1780272852Shrs printf("%8lu%c ", rtm->rtm_rmx.rmx_ssthresh, lock(SSTHRESH)); 1781272852Shrs printf("%8lu%c ", msec(rtm->rtm_rmx.rmx_rtt), lock(RTT)); 1782272852Shrs printf("%8lu%c ", rtm->rtm_rmx.rmx_mtu, lock(MTU)); 1783272852Shrs printf("%8lu%c ", rtm->rtm_rmx.rmx_weight, lock(WEIGHT)); 1784272852Shrs if (rtm->rtm_rmx.rmx_expire > 0) 1785272852Shrs clock_gettime(CLOCK_REALTIME_FAST, &ts); 1786272852Shrs else 1787272852Shrs ts.tv_sec = 0; 1788272853Shrs printf("%8ld%c\n", (long)(rtm->rtm_rmx.rmx_expire - ts.tv_sec), 1789272853Shrs lock(EXPIRE)); 17901558Srgrimes#undef lock 17911558Srgrimes#undef msec 17921558Srgrimes#define RTA_IGN (RTA_DST|RTA_GATEWAY|RTA_NETMASK|RTA_IFP|RTA_IFA|RTA_BRD) 17931558Srgrimes if (verbose) 1794216297Sglebius pmsg_common(rtm, msglen); 17951558Srgrimes else if (rtm->rtm_addrs &~ RTA_IGN) { 1796253427Shrs (void)printf("sockaddrs: "); 1797253427Shrs printb(rtm->rtm_addrs, addrnames); 17981558Srgrimes putchar('\n'); 17991558Srgrimes } 18001558Srgrimes#undef RTA_IGN 18011558Srgrimes} 18021558Srgrimes 1803204406Suqsstatic void 1804216297Sglebiuspmsg_common(struct rt_msghdr *rtm, size_t msglen) 18051558Srgrimes{ 1806253427Shrs 1807253427Shrs (void)printf("\nlocks: "); 1808253427Shrs printb(rtm->rtm_rmx.rmx_locks, metricnames); 1809253427Shrs (void)printf(" inits: "); 1810253427Shrs printb(rtm->rtm_inits, metricnames); 1811216297Sglebius if (msglen > sizeof(struct rt_msghdr)) 1812216297Sglebius pmsg_addrs(((char *)(rtm + 1)), rtm->rtm_addrs, 1813216297Sglebius msglen - sizeof(struct rt_msghdr)); 1814216297Sglebius else 1815253427Shrs (void)fflush(stdout); 18161558Srgrimes} 18171558Srgrimes 1818204406Suqsstatic void 1819216297Sglebiuspmsg_addrs(char *cp, int addrs, size_t len) 18201558Srgrimes{ 182192806Sobrien struct sockaddr *sa; 18221558Srgrimes int i; 18231558Srgrimes 182471061Sphk if (addrs == 0) { 1825253427Shrs (void)putchar('\n'); 18261558Srgrimes return; 182771061Sphk } 1828253427Shrs (void)printf("\nsockaddrs: "); 1829253427Shrs printb(addrs, addrnames); 1830253427Shrs putchar('\n'); 1831253517Shrs for (i = 0; i < RTAX_MAX; i++) 1832253517Shrs if (addrs & (1 << i)) { 18331558Srgrimes sa = (struct sockaddr *)cp; 1834216297Sglebius if (len == 0 || len < SA_SIZE(sa)) { 1835253427Shrs (void)printf(errfmt, __func__, len); 1836216297Sglebius break; 1837216297Sglebius } 1838253427Shrs (void)printf(" %s", routename(sa)); 1839216297Sglebius len -= SA_SIZE(sa); 1840128186Sluigi cp += SA_SIZE(sa); 18411558Srgrimes } 1842253427Shrs (void)putchar('\n'); 1843253427Shrs (void)fflush(stdout); 18441558Srgrimes} 18451558Srgrimes 1846204406Suqsstatic void 1847253427Shrsprintb(int b, const char *str) 18481558Srgrimes{ 184992806Sobrien int i; 18501558Srgrimes int gotsome = 0; 18511558Srgrimes 18521558Srgrimes if (b == 0) 18531558Srgrimes return; 1854204406Suqs while ((i = *str++) != 0) { 18551558Srgrimes if (b & (1 << (i-1))) { 18561558Srgrimes if (gotsome == 0) 18571558Srgrimes i = '<'; 18581558Srgrimes else 18591558Srgrimes i = ','; 1860253427Shrs putchar(i); 18611558Srgrimes gotsome = 1; 1862204406Suqs for (; (i = *str) > 32; str++) 1863253427Shrs putchar(i); 18641558Srgrimes } else 1865204406Suqs while (*str > 32) 1866204406Suqs str++; 18671558Srgrimes } 18681558Srgrimes if (gotsome) 1869253427Shrs putchar('>'); 18701558Srgrimes} 18711558Srgrimes 18721558Srgrimesint 1873204406Suqskeyword(const char *cp) 18741558Srgrimes{ 187592806Sobrien struct keytab *kt = keywords; 18761558Srgrimes 1877204406Suqs while (kt->kt_cp != NULL && strcmp(kt->kt_cp, cp) != 0) 18781558Srgrimes kt++; 1879204406Suqs return (kt->kt_i); 18801558Srgrimes} 18811558Srgrimes 1882204406Suqsstatic void 1883253427Shrssodump(struct sockaddr *sa, const char *which) 18841558Srgrimes{ 1885253427Shrs#ifdef INET6 1886253427Shrs char nbuf[INET6_ADDRSTRLEN]; 1887253427Shrs#endif 1888253427Shrs 1889253427Shrs switch (sa->sa_family) { 18901558Srgrimes case AF_LINK: 1891253427Shrs (void)printf("%s: link %s; ", which, 1892253502Shrs link_ntoa((struct sockaddr_dl *)(void *)sa)); 18931558Srgrimes break; 1894253427Shrs#ifdef INET 18951558Srgrimes case AF_INET: 1896253427Shrs (void)printf("%s: inet %s; ", which, 1897253502Shrs inet_ntoa(((struct sockaddr_in *)(void *)sa)->sin_addr)); 18981558Srgrimes break; 1899253427Shrs#endif 1900253427Shrs#ifdef INET6 1901253427Shrs case AF_INET6: 1902253427Shrs (void)printf("%s: inet6 %s; ", which, inet_ntop(sa->sa_family, 1903253502Shrs &((struct sockaddr_in6 *)(void *)sa)->sin6_addr, nbuf, 1904253427Shrs sizeof(nbuf))); 1905253427Shrs break; 1906253427Shrs#endif 190717046Sjulian case AF_APPLETALK: 1908253427Shrs (void)printf("%s: atalk %s; ", which, 1909253502Shrs atalk_ntoa(((struct sockaddr_at *)(void *)sa)->sat_addr)); 191017046Sjulian break; 19111558Srgrimes } 1912253427Shrs (void)fflush(stdout); 19131558Srgrimes} 19141558Srgrimes 19151558Srgrimes/* States*/ 19161558Srgrimes#define VIRGIN 0 19171558Srgrimes#define GOTONE 1 19181558Srgrimes#define GOTTWO 2 19191558Srgrimes/* Inputs */ 19201558Srgrimes#define DIGIT (4*0) 19211558Srgrimes#define END (4*1) 19221558Srgrimes#define DELIM (4*2) 19231558Srgrimes 1924204406Suqsstatic void 1925253427Shrssockaddr(char *addr, struct sockaddr *sa, size_t size) 19261558Srgrimes{ 192792806Sobrien char *cp = (char *)sa; 19281558Srgrimes char *cplim = cp + size; 192992806Sobrien int byte = 0, state = VIRGIN, new = 0 /* foil gcc */; 19301558Srgrimes 193185048Sru memset(cp, 0, size); 19321558Srgrimes cp++; 19331558Srgrimes do { 19341558Srgrimes if ((*addr >= '0') && (*addr <= '9')) { 19351558Srgrimes new = *addr - '0'; 19361558Srgrimes } else if ((*addr >= 'a') && (*addr <= 'f')) { 19371558Srgrimes new = *addr - 'a' + 10; 19381558Srgrimes } else if ((*addr >= 'A') && (*addr <= 'F')) { 19391558Srgrimes new = *addr - 'A' + 10; 1940204406Suqs } else if (*addr == '\0') 19411558Srgrimes state |= END; 19421558Srgrimes else 19431558Srgrimes state |= DELIM; 19441558Srgrimes addr++; 19451558Srgrimes switch (state /* | INPUT */) { 19461558Srgrimes case GOTTWO | DIGIT: 19471558Srgrimes *cp++ = byte; /*FALLTHROUGH*/ 19481558Srgrimes case VIRGIN | DIGIT: 19491558Srgrimes state = GOTONE; byte = new; continue; 19501558Srgrimes case GOTONE | DIGIT: 19511558Srgrimes state = GOTTWO; byte = new + (byte << 4); continue; 19521558Srgrimes default: /* | DELIM */ 19531558Srgrimes state = VIRGIN; *cp++ = byte; byte = 0; continue; 19541558Srgrimes case GOTONE | END: 19551558Srgrimes case GOTTWO | END: 19561558Srgrimes *cp++ = byte; /* FALLTHROUGH */ 19571558Srgrimes case VIRGIN | END: 19581558Srgrimes break; 19591558Srgrimes } 19601558Srgrimes break; 19611558Srgrimes } while (cp < cplim); 19621558Srgrimes sa->sa_len = cp - (char *)sa; 19631558Srgrimes} 196417046Sjulian 1965204406Suqsstatic int 196617046Sjulianatalk_aton(const char *text, struct at_addr *addr) 196717046Sjulian{ 196817046Sjulian u_int net, node; 196917046Sjulian 197017046Sjulian if (sscanf(text, "%u.%u", &net, &node) != 2 197117046Sjulian || net > 0xffff || node > 0xff) 197217046Sjulian return(0); 197317254Sjulian addr->s_net = htons(net); 197417046Sjulian addr->s_node = node; 197517046Sjulian return(1); 197617046Sjulian} 197717046Sjulian 1978204406Suqsstatic char * 197917046Sjulianatalk_ntoa(struct at_addr at) 198017046Sjulian{ 198117046Sjulian static char buf[20]; 198217046Sjulian 1983253427Shrs (void)snprintf(buf, sizeof(buf), "%u.%u", ntohs(at.s_net), at.s_node); 1984253427Shrs buf[sizeof(buf) - 1] = '\0'; 198517046Sjulian return(buf); 198617046Sjulian} 1987