Nuke SIMPLEQ_* and logprintf.
[dragonfly.git] / sbin / route / route.c
blobce2d347e035702ca20491aad6982ce47ce4aade1
1 /*
2 * Copyright (c) 1983, 1989, 1991, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
33 * @(#) Copyright (c) 1983, 1989, 1991, 1993 The Regents of the University of California. All rights reserved.
34 * @(#)route.c 8.6 (Berkeley) 4/28/95
35 * $FreeBSD: src/sbin/route/route.c,v 1.40.2.11 2003/02/27 23:10:10 ru Exp $
36 * $DragonFly: src/sbin/route/route.c,v 1.15 2007/04/11 18:51:33 dillon Exp $
39 #include <sys/param.h>
40 #include <sys/file.h>
41 #include <sys/socket.h>
42 #include <sys/ioctl.h>
43 #include <sys/sysctl.h>
44 #include <sys/types.h>
46 #include <net/if.h>
47 #include <net/route.h>
48 #include <net/if_dl.h>
49 #include <netinet/in.h>
50 #include <netinet/if_ether.h>
51 #include <netatalk/at.h>
52 #ifdef NS
53 #include <netns/ns.h>
54 #endif
55 #include <arpa/inet.h>
56 #include <netdb.h>
58 #include <ctype.h>
59 #include <err.h>
60 #include <errno.h>
61 #include <paths.h>
62 #include <stdio.h>
63 #include <stdlib.h>
64 #include <string.h>
65 #include <sysexits.h>
66 #include <unistd.h>
67 #include <ifaddrs.h>
69 #include "extern.h"
70 #include "keywords.h"
72 union sockunion {
73 struct sockaddr sa;
74 struct sockaddr_in sin;
75 #ifdef INET6
76 struct sockaddr_in6 sin6;
77 #endif
78 struct sockaddr_at sat;
79 #ifdef NS
80 struct sockaddr_ns sns;
81 #endif
82 struct sockaddr_dl sdl;
83 struct sockaddr_inarp sinarp;
84 struct sockaddr_storage ss; /* added to avoid memory overrun */
85 } so_dst, so_gate, so_mask, so_genmask, so_ifa, so_ifp;
87 typedef union sockunion *sup;
89 int nflag, wflag;
90 int cpuflag = -1;
92 static struct ortentry route;
93 static struct rt_metrics rt_metrics;
94 static int pid, rtm_addrs;
95 static int s;
96 static int forcehost, forcenet, af, qflag, tflag;
97 static int iflag, verbose, aflen = sizeof(struct sockaddr_in);
98 static int locking, lockrest, debugonly;
99 static u_long rtm_inits;
100 static uid_t uid;
101 #ifdef NS
102 static short ns_bh[] = {-1,-1,-1};
103 #endif
105 static int atalk_aton(const char *, struct at_addr *);
106 static char *atalk_ntoa(struct at_addr);
107 static void flushroutes(int, char **);
108 static void set_metric(char *, int);
109 static void newroute(int, char **);
110 static void inet_makenetandmask(u_long, struct sockaddr_in *, u_long);
111 static void interfaces(void);
112 static void monitor(void);
113 static void sockaddr(char *, struct sockaddr *);
114 static void sodump(sup, const char *);
115 static void bprintf(FILE *, int, u_char *);
116 static void print_getmsg(struct rt_msghdr *, int);
117 static void print_rtmsg(struct rt_msghdr *, int);
118 static void pmsg_common(struct rt_msghdr *);
119 static void pmsg_addrs(char *, int);
120 static void mask_addr(void);
121 static int getaddr(int, char *, struct hostent **);
122 static int rtmsg(int, int);
123 static int prefixlen(const char *);
124 #ifdef INET6
125 static int inet6_makenetandmask(struct sockaddr_in6 *, const char *);
126 #endif
128 void
129 usage(const char *cp)
131 if (cp != NULL)
132 warnx("bad keyword: %s", cp);
133 fprintf(stderr, "usage: route [-dnqtv] command [[modifiers] args]\n");
134 exit(EX_USAGE);
135 /* NOTREACHED */
138 #define ROUNDUP(a) \
139 ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
140 #define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len))
143 main(int argc, char **argv)
145 int ch;
147 if (argc < 2)
148 usage((char *)NULL);
150 while ((ch = getopt(argc, argv, "c:wnqdtv")) != -1)
151 switch(ch) {
152 case 'c':
153 cpuflag = strtol(optarg, NULL, 0);
154 break;
155 case 'w':
156 wflag = 1;
157 break;
158 case 'n':
159 nflag = 1;
160 break;
161 case 'q':
162 qflag = 1;
163 break;
164 case 'v':
165 verbose = 1;
166 break;
167 case 't':
168 tflag = 1;
169 break;
170 case 'd':
171 debugonly = 1;
172 break;
173 case '?':
174 default:
175 usage((char *)NULL);
177 argc -= optind;
178 argv += optind;
180 pid = getpid();
181 uid = geteuid();
182 if (tflag)
183 s = open(_PATH_DEVNULL, O_WRONLY, 0);
184 else
185 s = socket(PF_ROUTE, SOCK_RAW, 0);
186 if (s < 0)
187 err(EX_OSERR, "socket");
188 if (*argv != NULL)
189 switch (keyword(*argv)) {
190 case K_GET:
191 uid = 0;
192 /* FALLTHROUGH */
194 case K_CHANGE:
195 case K_ADD:
196 case K_DELETE:
197 newroute(argc, argv);
198 /* NOTREACHED */
200 case K_SHOW:
201 show(argc, argv);
202 return(0);
204 case K_MONITOR:
205 monitor();
206 /* NOTREACHED */
208 case K_FLUSH:
209 flushroutes(argc, argv);
210 exit(0);
211 /* NOTREACHED */
213 usage(*argv);
214 /* NOTREACHED */
218 * Purge all entries in the routing tables not
219 * associated with network interfaces.
221 static void
222 flushroutes(int argc, char **argv)
224 size_t needed;
225 int mib[7], rlen, seqno;
226 int miblen;
227 char *buf, *next, *lim;
228 struct rt_msghdr *rtm;
230 if (uid != 0) {
231 errx(EX_NOPERM, "must be root to alter routing table");
233 shutdown(s, SHUT_RD); /* Don't want to read back our messages */
234 if (argc > 1) {
235 argv++;
236 if (argc == 2 && **argv == '-')
237 switch (keyword(*argv + 1)) {
238 case K_INET:
239 af = AF_INET;
240 break;
241 #ifdef INET6
242 case K_INET6:
243 af = AF_INET6;
244 break;
245 #endif
246 case K_ATALK:
247 af = AF_APPLETALK;
248 break;
249 #ifdef NS
250 case K_XNS:
251 af = AF_NS;
252 break;
253 #endif
254 case K_LINK:
255 af = AF_LINK;
256 break;
257 default:
258 goto bad;
259 } else
260 bad: usage(*argv);
262 mib[0] = CTL_NET;
263 mib[1] = PF_ROUTE;
264 mib[2] = 0; /* protocol */
265 mib[3] = 0; /* wildcard address family */
266 mib[4] = NET_RT_DUMP;
267 mib[5] = 0; /* no flags */
268 if (cpuflag >= 0) {
269 mib[6] = cpuflag;
270 miblen = 7;
271 } else {
272 miblen = 6;
274 if (sysctl(mib, miblen, NULL, &needed, NULL, 0) < 0)
275 err(EX_OSERR, "route-sysctl-estimate");
276 if ((buf = malloc(needed)) == NULL)
277 errx(EX_OSERR, "malloc failed");
278 if (sysctl(mib, miblen, buf, &needed, NULL, 0) < 0)
279 err(EX_OSERR, "route-sysctl-get");
280 lim = buf + needed;
281 if (verbose)
282 printf("Examining routing table from sysctl\n");
283 seqno = 0; /* ??? */
284 for (next = buf; next < lim; next += rtm->rtm_msglen) {
285 rtm = (struct rt_msghdr *)next;
286 if (verbose)
287 print_rtmsg(rtm, rtm->rtm_msglen);
288 if ((rtm->rtm_flags & RTF_GATEWAY) == 0)
289 continue;
290 if (af != 0) {
291 struct sockaddr *sa = (struct sockaddr *)(rtm + 1);
293 if (sa->sa_family != af)
294 continue;
296 if (debugonly)
297 continue;
298 rtm->rtm_type = RTM_DELETE;
299 rtm->rtm_seq = seqno;
300 rlen = write(s, next, rtm->rtm_msglen);
301 if (rlen < (int)rtm->rtm_msglen) {
302 warn("write to routing socket");
303 printf("got only %d for rlen\n", rlen);
304 break;
306 seqno++;
307 if (qflag)
308 continue;
309 if (verbose) {
310 print_rtmsg(rtm, rlen);
311 } else {
312 struct sockaddr *sa = (struct sockaddr *)(rtm + 1);
313 if (wflag) {
314 printf("%-20s ", rtm->rtm_flags & RTF_HOST ?
315 routename(sa) : netname(sa));
316 } else {
317 printf("%-20.20s ", rtm->rtm_flags & RTF_HOST ?
318 routename(sa) : netname(sa));
320 sa = (struct sockaddr *)(ROUNDUP(sa->sa_len) +
321 (char *)sa);
322 if (wflag) {
323 printf("%-20s ", routename(sa));
324 } else {
325 printf("%-20.20s ", routename(sa));
327 printf("done\n");
332 char *
333 routename(struct sockaddr *sa)
335 const char *cp;
336 static char line[MAXHOSTNAMELEN + 1];
337 struct hostent *hp;
338 static char domain[MAXHOSTNAMELEN + 1];
339 static int first = 1;
341 if (first) {
342 first = 0;
343 if (gethostname(domain, MAXHOSTNAMELEN) == 0 &&
344 (cp = strchr(domain, '.'))) {
345 domain[MAXHOSTNAMELEN] = '\0';
346 strcpy(domain, cp + 1);
347 } else
348 domain[0] = 0;
351 if (sa->sa_len == 0)
352 strcpy(line, "default");
353 else switch (sa->sa_family) {
355 case AF_INET:
356 { struct in_addr in;
357 in = ((struct sockaddr_in *)sa)->sin_addr;
359 cp = NULL;
360 if (in.s_addr == INADDR_ANY || sa->sa_len < 4)
361 cp = "default";
362 if (cp == NULL && !nflag) {
363 hp = gethostbyaddr((char *)&in, sizeof(struct in_addr),
364 AF_INET);
365 if (hp != NULL) {
366 char *cptr;
367 cptr = strchr(hp->h_name, '.');
368 if (cptr != NULL && !wflag &&
369 strcmp(cptr + 1, domain) == 0)
370 *cptr = '\0';
371 cp = hp->h_name;
374 if (cp != NULL) {
375 strncpy(line, cp, sizeof(line) - 1);
376 line[sizeof(line) - 1] = '\0';
377 } else
378 sprintf(line, "%s", inet_ntoa(in));
379 break;
382 #ifdef INET6
383 case AF_INET6:
385 struct sockaddr_in6 sin6; /* use static var for safety */
386 int niflags = 0;
387 #ifdef NI_WITHSCOPEID
388 niflags = NI_WITHSCOPEID;
389 #endif
391 memset(&sin6, 0, sizeof(sin6));
392 memcpy(&sin6, sa, sa->sa_len);
393 sin6.sin6_len = sizeof(struct sockaddr_in6);
394 sin6.sin6_family = AF_INET6;
395 #ifdef __KAME__
396 if (sa->sa_len == sizeof(struct sockaddr_in6) &&
397 (IN6_IS_ADDR_LINKLOCAL(&sin6.sin6_addr) ||
398 IN6_IS_ADDR_MC_LINKLOCAL(&sin6.sin6_addr)) &&
399 sin6.sin6_scope_id == 0) {
400 sin6.sin6_scope_id =
401 ntohs(*(u_int16_t *)&sin6.sin6_addr.s6_addr[2]);
402 sin6.sin6_addr.s6_addr[2] = 0;
403 sin6.sin6_addr.s6_addr[3] = 0;
405 #endif
406 if (nflag)
407 niflags |= NI_NUMERICHOST;
408 if (getnameinfo((struct sockaddr *)&sin6, sin6.sin6_len,
409 line, sizeof(line), NULL, 0, niflags) != 0)
410 strncpy(line, "invalid", sizeof(line));
412 return(line);
414 #endif
416 case AF_APPLETALK:
417 snprintf(line, sizeof(line), "atalk %s",
418 atalk_ntoa(((struct sockaddr_at *)sa)->sat_addr));
419 break;
421 #ifdef NS
422 case AF_NS:
423 return(ns_print((struct sockaddr_ns *)sa));
424 #endif
426 case AF_LINK:
427 return(link_ntoa((struct sockaddr_dl *)sa));
429 default:
432 * Unknown address family; just render the raw
433 * data in sa->sa_data as hex values.
435 uint8_t *sp = (uint8_t *)sa->sa_data;
436 uint8_t *splim = (uint8_t *)sa + sa->sa_len;
437 char *cps = line + sprintf(line, "(%d)", sa->sa_family);
438 char *cpe = line + sizeof(line);
440 while (sp < splim && cps < cpe)
441 cps += snprintf(cps, cpe - cps, " %02x", *sp++);
442 break;
445 return(line);
449 * Return the name of the network whose address is given.
450 * The address is assumed to be that of a net or subnet, not a host.
452 char *
453 netname(struct sockaddr *sa)
455 const char *cp = 0;
456 static char line[MAXHOSTNAMELEN + 1];
457 struct netent *np = 0;
458 u_long net, mask;
459 u_long i;
460 int subnetshift;
462 switch (sa->sa_family) {
464 case AF_INET:
465 { struct in_addr in;
466 in = ((struct sockaddr_in *)sa)->sin_addr;
468 i = in.s_addr = ntohl(in.s_addr);
469 if (in.s_addr == 0)
470 cp = "default";
471 else if (!nflag) {
472 if (IN_CLASSA(i)) {
473 mask = IN_CLASSA_NET;
474 subnetshift = 8;
475 } else if (IN_CLASSB(i)) {
476 mask = IN_CLASSB_NET;
477 subnetshift = 8;
478 } else {
479 mask = IN_CLASSC_NET;
480 subnetshift = 4;
483 * If there are more bits than the standard mask
484 * would suggest, subnets must be in use.
485 * Guess at the subnet mask, assuming reasonable
486 * width subnet fields.
488 while (in.s_addr &~ mask)
489 mask = (long)mask >> subnetshift;
490 net = in.s_addr & mask;
491 while ((mask & 1) == 0)
492 mask >>= 1, net >>= 1;
493 np = getnetbyaddr(net, AF_INET);
494 if (np != NULL)
495 cp = np->n_name;
497 #define C(x) (unsigned)((x) & 0xff)
498 if (cp != NULL)
499 strncpy(line, cp, sizeof(line));
500 else if ((in.s_addr & 0xffffff) == 0)
501 sprintf(line, "%u", C(in.s_addr >> 24));
502 else if ((in.s_addr & 0xffff) == 0)
503 sprintf(line, "%u.%u", C(in.s_addr >> 24),
504 C(in.s_addr >> 16));
505 else if ((in.s_addr & 0xff) == 0)
506 sprintf(line, "%u.%u.%u", C(in.s_addr >> 24),
507 C(in.s_addr >> 16), C(in.s_addr >> 8));
508 else
509 sprintf(line, "%u.%u.%u.%u", C(in.s_addr >> 24),
510 C(in.s_addr >> 16), C(in.s_addr >> 8),
511 C(in.s_addr));
512 #undef C
513 break;
516 #ifdef INET6
517 case AF_INET6:
519 struct sockaddr_in6 sin6; /* use static var for safety */
520 int niflags = 0;
521 #ifdef NI_WITHSCOPEID
522 niflags = NI_WITHSCOPEID;
523 #endif
525 memset(&sin6, 0, sizeof(sin6));
526 memcpy(&sin6, sa, sa->sa_len);
527 sin6.sin6_len = sizeof(struct sockaddr_in6);
528 sin6.sin6_family = AF_INET6;
529 #ifdef __KAME__
530 if (sa->sa_len == sizeof(struct sockaddr_in6) &&
531 (IN6_IS_ADDR_LINKLOCAL(&sin6.sin6_addr) ||
532 IN6_IS_ADDR_MC_LINKLOCAL(&sin6.sin6_addr)) &&
533 sin6.sin6_scope_id == 0) {
534 sin6.sin6_scope_id =
535 ntohs(*(u_int16_t *)&sin6.sin6_addr.s6_addr[2]);
536 sin6.sin6_addr.s6_addr[2] = 0;
537 sin6.sin6_addr.s6_addr[3] = 0;
539 #endif
540 if (nflag)
541 niflags |= NI_NUMERICHOST;
542 if (getnameinfo((struct sockaddr *)&sin6, sin6.sin6_len,
543 line, sizeof(line), NULL, 0, niflags) != 0)
544 strncpy(line, "invalid", sizeof(line));
546 return(line);
548 #endif
550 case AF_APPLETALK:
551 snprintf(line, sizeof(line), "atalk %s",
552 atalk_ntoa(((struct sockaddr_at *)sa)->sat_addr));
553 break;
555 #ifdef NS
556 case AF_NS:
557 return(ns_print((struct sockaddr_ns *)sa));
558 break;
559 #endif
561 case AF_LINK:
562 return(link_ntoa((struct sockaddr_dl *)sa));
565 default:
568 * Unknown address family; just render the raw
569 * data in sa->sa_data as hex values.
571 uint8_t *sp = (uint8_t *)sa->sa_data;
572 uint8_t *splim = (uint8_t *)sa + sa->sa_len;
573 char *cps = line + sprintf(line, "af %d:", sa->sa_family);
574 char *cpe = line + sizeof(line);
576 while (sp < splim && cps < cpe)
577 cps += snprintf(cps, cpe - cps, " %02x", *sp++);
578 break;
581 return(line);
584 static void
585 set_metric(char *value, int key)
587 int flag = 0;
588 u_long noval, *valp = &noval;
590 switch (key) {
591 #define caseof(x, y, z) case x: valp = &rt_metrics.z; flag = y; break
592 caseof(K_MTU, RTV_MTU, rmx_mtu);
593 caseof(K_HOPCOUNT, RTV_HOPCOUNT, rmx_hopcount);
594 caseof(K_EXPIRE, RTV_EXPIRE, rmx_expire);
595 caseof(K_RECVPIPE, RTV_RPIPE, rmx_recvpipe);
596 caseof(K_SENDPIPE, RTV_SPIPE, rmx_sendpipe);
597 caseof(K_SSTHRESH, RTV_SSTHRESH, rmx_ssthresh);
598 caseof(K_RTT, RTV_RTT, rmx_rtt);
599 caseof(K_RTTVAR, RTV_RTTVAR, rmx_rttvar);
601 rtm_inits |= flag;
602 if (lockrest || locking)
603 rt_metrics.rmx_locks |= flag;
604 if (locking)
605 locking = 0;
606 *valp = atoi(value);
609 static void
610 newroute(int argc, char **argv)
612 char *cmd;
613 const char *err_str, *dest = "", *gateway = "";
614 int ishost = 0, proxy = 0, ret, attempts, oerrno, flags = RTF_STATIC;
615 int key;
616 struct hostent *hp = 0;
618 if (uid != 0) {
619 errx(EX_NOPERM, "must be root to alter routing table");
621 cmd = argv[0];
622 if (*cmd != 'g')
623 shutdown(s, SHUT_RD); /* Don't want to read back our messages */
624 while (--argc > 0) {
625 if (**(++argv)== '-') {
626 switch (key = keyword(1 + *argv)) {
627 case K_LINK:
628 af = AF_LINK;
629 aflen = sizeof(struct sockaddr_dl);
630 break;
631 case K_INET:
632 af = AF_INET;
633 aflen = sizeof(struct sockaddr_in);
634 break;
635 #ifdef INET6
636 case K_INET6:
637 af = AF_INET6;
638 aflen = sizeof(struct sockaddr_in6);
639 break;
640 #endif
641 case K_ATALK:
642 af = AF_APPLETALK;
643 aflen = sizeof(struct sockaddr_at);
644 break;
645 case K_SA:
646 af = PF_ROUTE;
647 aflen = sizeof(union sockunion);
648 break;
649 #ifdef NS
650 case K_XNS:
651 af = AF_NS;
652 aflen = sizeof(struct sockaddr_ns);
653 break;
654 #endif
655 case K_IFACE:
656 case K_INTERFACE:
657 iflag++;
658 break;
659 case K_NOSTATIC:
660 flags &= ~RTF_STATIC;
661 break;
662 case K_LLINFO:
663 flags |= RTF_LLINFO;
664 break;
665 case K_LOCK:
666 locking = 1;
667 break;
668 case K_LOCKREST:
669 lockrest = 1;
670 break;
671 case K_HOST:
672 forcehost++;
673 break;
674 case K_REJECT:
675 flags |= RTF_REJECT;
676 break;
677 case K_BLACKHOLE:
678 flags |= RTF_BLACKHOLE;
679 break;
680 case K_PROTO1:
681 flags |= RTF_PROTO1;
682 break;
683 case K_PROTO2:
684 flags |= RTF_PROTO2;
685 break;
686 case K_PROXY:
687 proxy = 1;
688 break;
689 case K_CLONING:
690 flags |= RTF_CLONING;
691 break;
692 case K_XRESOLVE:
693 flags |= RTF_XRESOLVE;
694 break;
695 case K_STATIC:
696 flags |= RTF_STATIC;
697 break;
698 case K_IFA:
699 if (--argc == 0)
700 usage((char *)NULL);
701 getaddr(RTA_IFA, *++argv, 0);
702 break;
703 case K_IFP:
704 if (--argc == 0)
705 usage((char *)NULL);
706 getaddr(RTA_IFP, *++argv, 0);
707 break;
708 case K_GENMASK:
709 if (--argc == 0)
710 usage((char *)NULL);
711 getaddr(RTA_GENMASK, *++argv, 0);
712 break;
713 case K_GATEWAY:
714 if (--argc == 0)
715 usage((char *)NULL);
716 getaddr(RTA_GATEWAY, *++argv, 0);
717 break;
718 case K_DST:
719 if (--argc == 0)
720 usage((char *)NULL);
721 ishost = getaddr(RTA_DST, *++argv, &hp);
722 dest = *argv;
723 break;
724 case K_NETMASK:
725 if (--argc == 0)
726 usage((char *)NULL);
727 getaddr(RTA_NETMASK, *++argv, 0);
728 /* FALLTHROUGH */
729 case K_NET:
730 forcenet++;
731 break;
732 case K_PREFIXLEN:
733 if (--argc == 0)
734 usage((char *)NULL);
735 if (prefixlen(*++argv) == -1) {
736 forcenet = 0;
737 ishost = 1;
738 } else {
739 forcenet = 1;
740 ishost = 0;
742 break;
743 case K_MTU:
744 case K_HOPCOUNT:
745 case K_EXPIRE:
746 case K_RECVPIPE:
747 case K_SENDPIPE:
748 case K_SSTHRESH:
749 case K_RTT:
750 case K_RTTVAR:
751 if (--argc == 0)
752 usage((char *)NULL);
753 set_metric(*++argv, key);
754 break;
755 default:
756 usage(1+*argv);
758 } else {
759 if ((rtm_addrs & RTA_DST) == 0) {
760 dest = *argv;
761 ishost = getaddr(RTA_DST, *argv, &hp);
762 } else if ((rtm_addrs & RTA_GATEWAY) == 0) {
763 gateway = *argv;
764 getaddr(RTA_GATEWAY, *argv, &hp);
765 } else {
766 getaddr(RTA_NETMASK, *argv, 0);
767 forcenet = 1;
771 if (forcehost) {
772 ishost = 1;
773 #ifdef INET6
774 if (af == AF_INET6) {
775 rtm_addrs &= ~RTA_NETMASK;
776 memset((void *)&so_mask, 0, sizeof(so_mask));
778 #endif
780 if (forcenet)
781 ishost = 0;
782 flags |= RTF_UP;
783 if (ishost)
784 flags |= RTF_HOST;
785 if (iflag == 0)
786 flags |= RTF_GATEWAY;
787 if (proxy) {
788 so_dst.sinarp.sin_other = SIN_PROXY;
789 flags |= RTF_ANNOUNCE;
791 for (attempts = 1; ; attempts++) {
792 errno = 0;
793 if ((ret = rtmsg(*cmd, flags)) == 0)
794 break;
795 if (errno != ENETUNREACH && errno != ESRCH)
796 break;
797 if (af == AF_INET && *gateway != '\0' &&
798 hp != NULL && hp->h_addr_list[1] != NULL) {
799 hp->h_addr_list++;
800 memmove(&so_gate.sin.sin_addr,
801 hp->h_addr_list[0],
802 MIN((size_t)hp->h_length,
803 sizeof(so_gate.sin.sin_addr)));
804 } else
805 break;
807 if (*cmd == 'g')
808 exit(0);
809 if (!qflag) {
810 oerrno = errno;
811 printf("%s %s %s", cmd, ishost? "host" : "net", dest);
812 if (*gateway != '\0') {
813 printf(": gateway %s", gateway);
814 if (attempts > 1 && ret == 0 && af == AF_INET)
815 printf(" (%s)",
816 inet_ntoa(((struct sockaddr_in *)&route.rt_gateway)->sin_addr));
818 if (ret == 0) {
819 printf("\n");
820 } else {
821 switch (oerrno) {
822 case ESRCH:
823 err_str = "not in table";
824 break;
825 case EBUSY:
826 err_str = "entry in use";
827 break;
828 case ENOBUFS:
829 err_str = "routing table overflow";
830 break;
831 case EDQUOT: /* handle recursion avoidance in
832 rt_setgate() */
833 err_str = "gateway uses the same route";
834 break;
835 default:
836 err_str = strerror(oerrno);
837 break;
839 printf(": %s\n", err_str);
842 exit(ret != 0);
845 static void
846 inet_makenetandmask(u_long net, struct sockaddr_in *in, u_long bits)
848 u_long addr, mask = 0;
849 char *cp;
851 rtm_addrs |= RTA_NETMASK;
852 if (net == 0)
853 mask = addr = 0;
854 else if (net < 128) {
855 addr = net << IN_CLASSA_NSHIFT;
856 mask = IN_CLASSA_NET;
857 } else if (net < 65536) {
858 addr = net << IN_CLASSB_NSHIFT;
859 mask = IN_CLASSB_NET;
860 } else if (net < 16777216L) {
861 addr = net << IN_CLASSC_NSHIFT;
862 mask = IN_CLASSC_NET;
863 } else {
864 addr = net;
865 if ((addr & IN_CLASSA_HOST) == 0)
866 mask = IN_CLASSA_NET;
867 else if ((addr & IN_CLASSB_HOST) == 0)
868 mask = IN_CLASSB_NET;
869 else if ((addr & IN_CLASSC_HOST) == 0)
870 mask = IN_CLASSC_NET;
871 else
872 mask = -1;
874 if (bits != 0)
875 mask = 0xffffffff << (32 - bits);
876 in->sin_addr.s_addr = htonl(addr);
877 in = &so_mask.sin;
878 in->sin_addr.s_addr = htonl(mask);
879 in->sin_len = 0;
880 in->sin_family = 0;
881 cp = (char *)(&in->sin_addr + 1);
882 while (*--cp == 0 && cp > (char *)in)
884 in->sin_len = 1 + cp - (char *)in;
887 #ifdef INET6
889 * XXX the function may need more improvement...
891 static int
892 inet6_makenetandmask(struct sockaddr_in6 *sin6, const char *plen)
894 struct in6_addr in6;
896 if (plen == NULL) {
897 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr) &&
898 sin6->sin6_scope_id == 0) {
899 plen = "0";
900 } else if ((sin6->sin6_addr.s6_addr[0] & 0xe0) == 0x20) {
901 /* aggregatable global unicast - RFC2374 */
902 memset(&in6, 0, sizeof(in6));
903 if (memcmp(&sin6->sin6_addr.s6_addr[8],
904 &in6.s6_addr[8], 8) == 0)
905 plen = "64";
909 if (plen == NULL || strcmp(plen, "128") == 0)
910 return(1);
911 rtm_addrs |= RTA_NETMASK;
912 prefixlen(plen);
913 return(0);
915 #endif
918 * Interpret an argument as a network address of some kind,
919 * returning 1 if a host address, 0 if a network address.
921 static int
922 getaddr(int which, char *str, struct hostent **hpp)
924 sup su;
925 struct hostent *hp;
926 struct netent *np;
927 u_long val;
928 char *q;
929 int afamily; /* local copy of af so we can change it */
931 if (af == 0) {
932 af = AF_INET;
933 aflen = sizeof(struct sockaddr_in);
935 afamily = af;
936 rtm_addrs |= which;
937 switch (which) {
938 case RTA_DST:
939 su = &so_dst;
940 break;
941 case RTA_GATEWAY:
942 su = &so_gate;
943 if (iflag) {
944 struct ifaddrs *ifap, *ifa;
945 struct sockaddr_dl *sdl = NULL;
947 if (getifaddrs(&ifap))
948 err(1, "getifaddrs");
950 for (ifa = ifap; ifa != NULL; ifa = ifa->ifa_next) {
951 if (ifa->ifa_addr->sa_family != AF_LINK)
952 continue;
954 if (strcmp(str, ifa->ifa_name) != 0)
955 continue;
957 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
959 /* If we found it, then use it */
960 if (sdl != NULL) {
962 * Copy is safe since we have a
963 * sockaddr_storage member in sockunion{}.
964 * Note that we need to copy before calling
965 * freeifaddrs().
967 * Interface routes are routed only by their
968 * sdl_index or interface name (we provide
969 * both here), not by the ARP address of the
970 * local interface (alen, slen = 0).
972 memcpy(&su->sdl, sdl, sdl->sdl_len);
973 su->sdl.sdl_alen = 0;
974 su->sdl.sdl_slen = 0;
976 freeifaddrs(ifap);
977 if (sdl != NULL)
978 return(1);
980 break;
981 case RTA_NETMASK:
982 su = &so_mask;
983 break;
984 case RTA_GENMASK:
985 su = &so_genmask;
986 break;
987 case RTA_IFP:
988 su = &so_ifp;
989 afamily = AF_LINK;
990 break;
991 case RTA_IFA:
992 su = &so_ifa;
993 break;
994 default:
995 usage("internal error");
996 /*NOTREACHED*/
998 su->sa.sa_len = aflen;
999 su->sa.sa_family = afamily; /* cases that don't want it have left
1000 already */
1001 if (strcmp(str, "default") == 0) {
1003 * Default is net 0.0.0.0/0
1005 switch (which) {
1006 case RTA_DST:
1007 forcenet++;
1008 /* bzero(su, sizeof(*su)); *//* for readability */
1009 getaddr(RTA_NETMASK, str, 0);
1010 break;
1011 case RTA_NETMASK:
1012 case RTA_GENMASK:
1013 /* bzero(su, sizeof(*su)); *//* for readability */
1014 break;
1016 return(0);
1018 switch (afamily) {
1019 #ifdef INET6
1020 case AF_INET6:
1022 struct addrinfo hints, *res;
1024 q = NULL;
1025 if (which == RTA_DST && (q = strchr(str, '/')) != NULL)
1026 *q = '\0';
1027 memset(&hints, 0, sizeof(hints));
1028 hints.ai_family = afamily; /*AF_INET6*/
1029 hints.ai_flags = AI_NUMERICHOST;
1030 hints.ai_socktype = SOCK_DGRAM; /*dummy*/
1031 if (getaddrinfo(str, "0", &hints, &res) != 0 ||
1032 res->ai_family != AF_INET6 ||
1033 res->ai_addrlen != sizeof(su->sin6)) {
1034 fprintf(stderr, "%s: bad value\n", str);
1035 exit(1);
1037 memcpy(&su->sin6, res->ai_addr, sizeof(su->sin6));
1038 #ifdef __KAME__
1039 if ((IN6_IS_ADDR_LINKLOCAL(&su->sin6.sin6_addr) ||
1040 IN6_IS_ADDR_LINKLOCAL(&su->sin6.sin6_addr)) &&
1041 su->sin6.sin6_scope_id) {
1042 *(u_int16_t *)&su->sin6.sin6_addr.s6_addr[2] =
1043 htons(su->sin6.sin6_scope_id);
1044 su->sin6.sin6_scope_id = 0;
1046 #endif
1047 freeaddrinfo(res);
1048 if (q != NULL)
1049 *q++ = '/';
1050 if (which == RTA_DST)
1051 return(inet6_makenetandmask(&su->sin6, q));
1052 return(0);
1054 #endif /* INET6 */
1056 #ifdef NS
1057 case AF_NS:
1058 if (which == RTA_DST) {
1059 struct sockaddr_ns *sms = &(so_mask.sns);
1060 memset(sms, 0, sizeof(*sms));
1061 sms->sns_family = 0;
1062 sms->sns_len = 6;
1063 sms->sns_addr.x_net = *(union ns_net *)ns_bh;
1064 rtm_addrs |= RTA_NETMASK;
1066 su->sns.sns_addr = ns_addr(str);
1067 return(!ns_nullhost(su->sns.sns_addr));
1068 #endif
1071 case AF_APPLETALK:
1072 if (!atalk_aton(str, &su->sat.sat_addr))
1073 errx(EX_NOHOST, "bad address: %s", str);
1074 rtm_addrs |= RTA_NETMASK;
1075 return(forcehost || su->sat.sat_addr.s_node != 0);
1077 case AF_LINK:
1078 link_addr(str, &su->sdl);
1079 return(1);
1082 case PF_ROUTE:
1083 su->sa.sa_len = sizeof(*su);
1084 sockaddr(str, &su->sa);
1085 return(1);
1087 case AF_INET:
1088 default:
1089 break;
1092 if (hpp == NULL)
1093 hpp = &hp;
1094 *hpp = NULL;
1096 q = strchr(str,'/');
1097 if (q != NULL && which == RTA_DST) {
1098 *q = '\0';
1099 if ((val = inet_network(str)) != INADDR_NONE) {
1100 inet_makenetandmask(
1101 val, &su->sin, strtoul(q+1, 0, 0));
1102 return(0);
1104 *q = '/';
1106 if ((which != RTA_DST || forcenet == 0) &&
1107 inet_aton(str, &su->sin.sin_addr)) {
1108 val = su->sin.sin_addr.s_addr;
1109 if (which != RTA_DST ||
1110 inet_lnaof(su->sin.sin_addr) != INADDR_ANY)
1111 return(1);
1112 else {
1113 val = ntohl(val);
1114 goto netdone;
1117 if (which == RTA_DST && forcehost == 0 &&
1118 ((val = inet_network(str)) != INADDR_NONE ||
1119 ((np = getnetbyname(str)) != NULL && (val = np->n_net) != 0))) {
1120 netdone:
1121 inet_makenetandmask(val, &su->sin, 0);
1122 return(0);
1124 hp = gethostbyname(str);
1125 if (hp != NULL) {
1126 *hpp = hp;
1127 su->sin.sin_family = hp->h_addrtype;
1128 memmove((char *)&su->sin.sin_addr, hp->h_addr,
1129 MIN((size_t)hp->h_length, sizeof(su->sin.sin_addr)));
1130 return(1);
1132 errx(EX_NOHOST, "bad address: %s", str);
1135 static int
1136 prefixlen(const char *len_str)
1138 int len = atoi(len_str), q, r;
1139 int max;
1140 char *p;
1142 rtm_addrs |= RTA_NETMASK;
1143 switch (af) {
1144 #ifdef INET6
1145 case AF_INET6:
1146 max = 128;
1147 p = (char *)&so_mask.sin6.sin6_addr;
1148 break;
1149 #endif
1150 case AF_INET:
1151 max = 32;
1152 p = (char *)&so_mask.sin.sin_addr;
1153 break;
1154 default:
1155 fprintf(stderr, "prefixlen not supported in this af\n");
1156 exit(1);
1157 /*NOTREACHED*/
1160 if (len < 0 || max < len) {
1161 fprintf(stderr, "%s: bad value\n", len_str);
1162 exit(1);
1165 q = len >> 3;
1166 r = len & 7;
1167 so_mask.sa.sa_family = af;
1168 so_mask.sa.sa_len = aflen;
1169 memset((void *)p, 0, max / 8);
1170 if (q > 0)
1171 memset((void *)p, 0xff, q);
1172 if (r > 0)
1173 *((u_char *)p + q) = (0xff00 >> r) & 0xff;
1174 if (len == max)
1175 return(-1);
1176 else
1177 return(len);
1180 #ifdef NS
1181 short ns_nullh[] = {0,0,0};
1183 char *
1184 ns_print(struct sockaddr_ns *sns)
1186 struct ns_addr work;
1187 union { union ns_net net_e; u_long long_e; } net;
1188 u_short port;
1189 static char mybuf[50+MAXHOSTNAMELEN], cport[10], chost[25];
1190 const char *host = "";
1191 char *p;
1192 u_char *q;
1194 work = sns->sns_addr;
1195 port = ntohs(work.x_port);
1196 work.x_port = 0;
1197 net.net_e = work.x_net;
1198 if (ns_nullhost(work) && net.long_e == 0) {
1199 if (port == 0)
1200 strncpy(mybuf, "*.*", sizeof(mybuf));
1201 else
1202 sprintf(mybuf, "*.%XH", port);
1203 return(mybuf);
1206 if (memcmp(ns_bh, work.x_host.c_host, 6) == 0)
1207 host = "any";
1208 else if (memcmp(ns_nullh, work.x_host.c_host, 6) == 0)
1209 host = "*";
1210 else {
1211 q = work.x_host.c_host;
1212 sprintf(chost, "%02X%02X%02X%02X%02X%02XH",
1213 q[0], q[1], q[2], q[3], q[4], q[5]);
1214 for (p = chost; *p == '0' && p < chost + 12; p++)
1215 /* void */;
1216 host = p;
1218 if (port != 0)
1219 sprintf(cport, ".%XH", htons(port));
1220 else
1221 *cport = 0;
1223 snprintf(mybuf, sizeof(mybuf), "%lxH.%s%s",
1224 (unsigned long)ntohl(net.long_e),
1225 host, cport);
1226 return(mybuf);
1228 #endif
1230 static void
1231 interfaces(void)
1233 size_t needed;
1234 int mib[6];
1235 char *buf, *lim, *next;
1236 struct rt_msghdr *rtm;
1238 mib[0] = CTL_NET;
1239 mib[1] = PF_ROUTE;
1240 mib[2] = 0; /* protocol */
1241 mib[3] = 0; /* wildcard address family */
1242 mib[4] = NET_RT_IFLIST;
1243 mib[5] = 0; /* no flags */
1244 if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
1245 err(EX_OSERR, "route-sysctl-estimate");
1246 if ((buf = malloc(needed)) == NULL)
1247 errx(EX_OSERR, "malloc failed");
1248 if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0)
1249 err(EX_OSERR, "actual retrieval of interface table");
1250 lim = buf + needed;
1251 for (next = buf; next < lim; next += rtm->rtm_msglen) {
1252 rtm = (struct rt_msghdr *)next;
1253 print_rtmsg(rtm, rtm->rtm_msglen);
1257 static void
1258 monitor(void)
1260 int n;
1261 char msg[2048];
1263 verbose = 1;
1264 if (debugonly) {
1265 interfaces();
1266 exit(0);
1268 for (;;) {
1269 time_t now;
1270 n = read(s, msg, 2048);
1271 now = time(NULL);
1272 printf("\ngot message of size %d on %s", n, ctime(&now));
1273 print_rtmsg((struct rt_msghdr *)msg, n);
1277 struct {
1278 struct rt_msghdr m_rtm;
1279 char m_space[512];
1280 } m_rtmsg;
1282 static int
1283 rtmsg(int cmd, int flags)
1285 static int seq;
1286 int rlen;
1287 char *cp = m_rtmsg.m_space;
1288 int l;
1290 #define NEXTADDR(w, u) \
1291 if (rtm_addrs & (w)) {\
1292 l = ROUNDUP(u.sa.sa_len); memmove(cp, &(u), l); cp += l;\
1293 if (verbose) sodump(&(u),"u");\
1296 errno = 0;
1297 memset(&m_rtmsg, 0, sizeof(m_rtmsg));
1298 if (cmd == 'a')
1299 cmd = RTM_ADD;
1300 else if (cmd == 'c')
1301 cmd = RTM_CHANGE;
1302 else if (cmd == 'g') {
1303 cmd = RTM_GET;
1304 if (so_ifp.sa.sa_family == 0) {
1305 so_ifp.sa.sa_family = AF_LINK;
1306 so_ifp.sa.sa_len = sizeof(struct sockaddr_dl);
1307 rtm_addrs |= RTA_IFP;
1309 } else
1310 cmd = RTM_DELETE;
1311 #define rtm m_rtmsg.m_rtm
1312 rtm.rtm_type = cmd;
1313 rtm.rtm_flags = flags;
1314 rtm.rtm_version = RTM_VERSION;
1315 rtm.rtm_seq = ++seq;
1316 rtm.rtm_addrs = rtm_addrs;
1317 rtm.rtm_rmx = rt_metrics;
1318 rtm.rtm_inits = rtm_inits;
1320 if (rtm_addrs & RTA_NETMASK)
1321 mask_addr();
1322 NEXTADDR(RTA_DST, so_dst);
1323 NEXTADDR(RTA_GATEWAY, so_gate);
1324 NEXTADDR(RTA_NETMASK, so_mask);
1325 NEXTADDR(RTA_GENMASK, so_genmask);
1326 NEXTADDR(RTA_IFP, so_ifp);
1327 NEXTADDR(RTA_IFA, so_ifa);
1328 rtm.rtm_msglen = l = cp - (char *)&m_rtmsg;
1329 if (verbose)
1330 print_rtmsg(&rtm, l);
1331 if (debugonly)
1332 return(0);
1333 if ((rlen = write(s, (char *)&m_rtmsg, l)) < 0) {
1334 warn("writing to routing socket");
1335 return(-1);
1337 if (cmd == RTM_GET) {
1338 do {
1339 l = read(s, (char *)&m_rtmsg, sizeof(m_rtmsg));
1340 } while (l > 0 && (rtm.rtm_seq != seq || rtm.rtm_pid != pid));
1341 if (l < 0)
1342 warn("read from routing socket");
1343 else
1344 print_getmsg(&rtm, l);
1346 #undef rtm
1347 return(0);
1350 static void
1351 mask_addr(void)
1353 int olen = so_mask.sa.sa_len;
1354 char *cp1 = olen + (char *)&so_mask, *cp2;
1356 for (so_mask.sa.sa_len = 0; cp1 > (char *)&so_mask; )
1357 if (*--cp1 != 0) {
1358 so_mask.sa.sa_len = 1 + cp1 - (char *)&so_mask;
1359 break;
1361 if ((rtm_addrs & RTA_DST) == 0)
1362 return;
1363 switch (so_dst.sa.sa_family) {
1364 #ifdef NS
1365 case AF_NS:
1366 #endif
1367 case AF_INET:
1368 #ifdef INET6
1369 case AF_INET6:
1370 #endif
1371 case AF_APPLETALK:
1372 case 0:
1373 return;
1375 cp1 = so_mask.sa.sa_len + 1 + (char *)&so_dst;
1376 cp2 = so_dst.sa.sa_len + 1 + (char *)&so_dst;
1377 while (cp2 > cp1)
1378 *--cp2 = 0;
1379 cp2 = so_mask.sa.sa_len + 1 + (char *)&so_mask;
1380 while (cp1 > so_dst.sa.sa_data)
1381 *--cp1 &= *--cp2;
1384 const char *msgtypes[] = {
1386 "RTM_ADD: Add Route",
1387 "RTM_DELETE: Delete Route",
1388 "RTM_CHANGE: Change Metrics or flags",
1389 "RTM_GET: Report Metrics",
1390 "RTM_LOSING: Kernel Suspects Partitioning",
1391 "RTM_REDIRECT: Told to use different route",
1392 "RTM_MISS: Lookup failed on this address",
1393 "RTM_LOCK: fix specified metrics",
1394 "RTM_OLDADD: caused by SIOCADDRT",
1395 "RTM_OLDDEL: caused by SIOCDELRT",
1396 "RTM_RESOLVE: Route created by cloning",
1397 "RTM_NEWADDR: address being added to iface",
1398 "RTM_DELADDR: address being removed from iface",
1399 "RTM_IFINFO: iface status change",
1400 "RTM_NEWMADDR: new multicast group membership on iface",
1401 "RTM_DELMADDR: multicast group membership removed from iface",
1402 "RTM_IFANNOUNCE: interface arrival/departure",
1406 char metricnames[] =
1407 "\011pksent\010rttvar\7rtt\6ssthresh\5sendpipe\4recvpipe\3expire\2hopcount"
1408 "\1mtu";
1409 char routeflags[] =
1410 "\1UP\2GATEWAY\3HOST\4REJECT\5DYNAMIC\6MODIFIED\7DONE\010MASK_PRESENT"
1411 "\011CLONING\012XRESOLVE\013LLINFO\014STATIC\015BLACKHOLE\016b016"
1412 "\017PROTO2\020PROTO1\021PRCLONING\022WASCLONED\023PROTO3\024CHAINDELETE"
1413 "\025PINNED\026LOCAL\027BROADCAST\030MULTICAST";
1414 char ifnetflags[] =
1415 "\1UP\2BROADCAST\3DEBUG\4LOOPBACK\5PTP\6b6\7RUNNING\010NOARP"
1416 "\011PPROMISC\012ALLMULTI\013OACTIVE\014SIMPLEX\015LINK0\016LINK1"
1417 "\017LINK2\020MULTICAST";
1418 char addrnames[] =
1419 "\1DST\2GATEWAY\3NETMASK\4GENMASK\5IFP\6IFA\7AUTHOR\010BRD";
1421 static void
1422 print_rtmsg(struct rt_msghdr *rtm, int msglen __unused)
1424 struct if_msghdr *ifm;
1425 struct ifa_msghdr *ifam;
1426 #ifdef RTM_NEWMADDR
1427 struct ifma_msghdr *ifmam;
1428 #endif
1429 struct if_announcemsghdr *ifan;
1431 if (verbose == 0)
1432 return;
1433 if (rtm->rtm_version != RTM_VERSION) {
1434 printf("routing message version %d not understood\n",
1435 rtm->rtm_version);
1436 return;
1438 if (msgtypes[rtm->rtm_type] != NULL)
1439 printf("%s: ", msgtypes[rtm->rtm_type]);
1440 else
1441 printf("#%d: ", rtm->rtm_type);
1442 printf("len %d, ", rtm->rtm_msglen);
1443 switch (rtm->rtm_type) {
1444 case RTM_IFINFO:
1445 ifm = (struct if_msghdr *)rtm;
1446 printf("if# %d, flags:", ifm->ifm_index);
1447 bprintf(stdout, ifm->ifm_flags, ifnetflags);
1448 pmsg_addrs((char *)(ifm + 1), ifm->ifm_addrs);
1449 break;
1450 case RTM_NEWADDR:
1451 case RTM_DELADDR:
1452 ifam = (struct ifa_msghdr *)rtm;
1453 printf("metric %d, flags:", ifam->ifam_metric);
1454 bprintf(stdout, ifam->ifam_flags, routeflags);
1455 pmsg_addrs((char *)(ifam + 1), ifam->ifam_addrs);
1456 break;
1457 #ifdef RTM_NEWMADDR
1458 case RTM_NEWMADDR:
1459 case RTM_DELMADDR:
1460 ifmam = (struct ifma_msghdr *)rtm;
1461 pmsg_addrs((char *)(ifmam + 1), ifmam->ifmam_addrs);
1462 break;
1463 #endif
1464 case RTM_IFANNOUNCE:
1465 ifan = (struct if_announcemsghdr *)rtm;
1466 printf("if# %d, what: ", ifan->ifan_index);
1467 switch (ifan->ifan_what) {
1468 case IFAN_ARRIVAL:
1469 printf("arrival");
1470 break;
1471 case IFAN_DEPARTURE:
1472 printf("departure");
1473 break;
1474 default:
1475 printf("#%d", ifan->ifan_what);
1476 break;
1478 printf("\n");
1479 break;
1481 default:
1482 printf("pid: %ld, seq %d, errno %d, flags:",
1483 (long)rtm->rtm_pid, rtm->rtm_seq, rtm->rtm_errno);
1484 bprintf(stdout, rtm->rtm_flags, routeflags);
1485 pmsg_common(rtm);
1489 static void
1490 print_getmsg(struct rt_msghdr *rtm, int msglen)
1492 struct sockaddr *dst = NULL, *gate = NULL, *mask = NULL;
1493 struct sockaddr_dl *ifp = NULL;
1494 struct sockaddr *sa;
1495 char *cp;
1496 int i;
1498 printf(" route to: %s\n", routename(&so_dst.sa));
1499 if (rtm->rtm_version != RTM_VERSION) {
1500 warnx("routing message version %d not understood",
1501 rtm->rtm_version);
1502 return;
1504 if (rtm->rtm_msglen > msglen) {
1505 warnx("message length mismatch, in packet %d, returned %d",
1506 rtm->rtm_msglen, msglen);
1508 if (rtm->rtm_errno != 0) {
1509 errno = rtm->rtm_errno;
1510 warn("message indicates error %d", errno);
1511 return;
1513 cp = ((char *)(rtm + 1));
1514 if (rtm->rtm_addrs)
1515 for (i = 1; i != 0; i <<= 1)
1516 if (i & rtm->rtm_addrs) {
1517 sa = (struct sockaddr *)cp;
1518 switch (i) {
1519 case RTA_DST:
1520 dst = sa;
1521 break;
1522 case RTA_GATEWAY:
1523 gate = sa;
1524 break;
1525 case RTA_NETMASK:
1526 mask = sa;
1527 break;
1528 case RTA_IFP:
1529 if (sa->sa_family == AF_LINK &&
1530 ((struct sockaddr_dl *)sa)->sdl_nlen)
1531 ifp = (struct sockaddr_dl *)sa;
1532 break;
1534 ADVANCE(cp, sa);
1536 if (dst != NULL && mask != NULL)
1537 mask->sa_family = dst->sa_family; /* XXX */
1538 if (dst != NULL)
1539 printf("destination: %s\n", routename(dst));
1540 if (mask != NULL) {
1541 int savenflag = nflag;
1543 nflag = 1;
1544 printf(" mask: %s\n", routename(mask));
1545 nflag = savenflag;
1547 if (gate != NULL && rtm->rtm_flags & RTF_GATEWAY)
1548 printf(" gateway: %s\n", routename(gate));
1549 if (ifp != NULL)
1550 printf(" interface: %.*s\n",
1551 ifp->sdl_nlen, ifp->sdl_data);
1552 printf(" flags: ");
1553 bprintf(stdout, rtm->rtm_flags, routeflags);
1555 #define lock(f) ((rtm->rtm_rmx.rmx_locks & __CONCAT(RTV_,f)) ? 'L' : ' ')
1556 #define msec(u) (((u) + 500) / 1000) /* usec to msec */
1558 printf("\n%s\n", "\
1559 recvpipe sendpipe ssthresh rtt,msec rttvar hopcount mtu expire");
1560 printf("%8ld%c ", rtm->rtm_rmx.rmx_recvpipe, lock(RPIPE));
1561 printf("%8ld%c ", rtm->rtm_rmx.rmx_sendpipe, lock(SPIPE));
1562 printf("%8ld%c ", rtm->rtm_rmx.rmx_ssthresh, lock(SSTHRESH));
1563 printf("%8ld%c ", msec(rtm->rtm_rmx.rmx_rtt), lock(RTT));
1564 printf("%8ld%c ", msec(rtm->rtm_rmx.rmx_rttvar), lock(RTTVAR));
1565 printf("%8ld%c ", rtm->rtm_rmx.rmx_hopcount, lock(HOPCOUNT));
1566 printf("%8ld%c ", rtm->rtm_rmx.rmx_mtu, lock(MTU));
1567 if (rtm->rtm_rmx.rmx_expire != 0)
1568 rtm->rtm_rmx.rmx_expire -= time(0);
1569 printf("%8ld%c\n", rtm->rtm_rmx.rmx_expire, lock(EXPIRE));
1570 #undef lock
1571 #undef msec
1572 #define RTA_IGN (RTA_DST|RTA_GATEWAY|RTA_NETMASK|RTA_IFP|RTA_IFA|RTA_BRD)
1573 if (verbose)
1574 pmsg_common(rtm);
1575 else if (rtm->rtm_addrs &~ RTA_IGN) {
1576 printf("sockaddrs: ");
1577 bprintf(stdout, rtm->rtm_addrs, addrnames);
1578 putchar('\n');
1580 #undef RTA_IGN
1583 static void
1584 pmsg_common(struct rt_msghdr *rtm)
1586 printf("\nlocks: ");
1587 bprintf(stdout, rtm->rtm_rmx.rmx_locks, metricnames);
1588 printf(" inits: ");
1589 bprintf(stdout, rtm->rtm_inits, metricnames);
1590 pmsg_addrs(((char *)(rtm + 1)), rtm->rtm_addrs);
1593 static void
1594 pmsg_addrs(char *cp, int addrs)
1596 struct sockaddr *sa;
1597 int i;
1599 if (addrs == 0) {
1600 putchar('\n');
1601 return;
1603 printf("\nsockaddrs: ");
1604 bprintf(stdout, addrs, addrnames);
1605 putchar('\n');
1606 for (i = 1; i != 0; i <<= 1)
1607 if (i & addrs) {
1608 sa = (struct sockaddr *)cp;
1609 printf(" %s", routename(sa));
1610 ADVANCE(cp, sa);
1612 putchar('\n');
1613 fflush(stdout);
1616 static void
1617 bprintf(FILE *fp, int b, u_char *str)
1619 int i;
1620 int gotsome = 0;
1622 if (b == 0)
1623 return;
1624 while ((i = *str++) != 0) {
1625 if (b & (1 << (i-1))) {
1626 if (!gotsome)
1627 i = '<';
1628 else
1629 i = ',';
1630 putc(i, fp);
1631 gotsome = 1;
1632 for (; (i = *str) > 32; str++)
1633 putc(i, fp);
1634 } else
1635 while (*str > 32)
1636 str++;
1638 if (gotsome)
1639 putc('>', fp);
1643 keyword(const char *cp)
1645 struct keytab *kt = keywords;
1647 while (kt->kt_cp != NULL && strcmp(kt->kt_cp, cp) != 0)
1648 kt++;
1649 return(kt->kt_i);
1652 static void
1653 sodump(sup su, const char *which)
1655 switch (su->sa.sa_family) {
1656 case AF_LINK:
1657 printf("%s: link %s; ",
1658 which, link_ntoa(&su->sdl));
1659 break;
1660 case AF_INET:
1661 printf("%s: inet %s; ",
1662 which, inet_ntoa(su->sin.sin_addr));
1663 break;
1664 case AF_APPLETALK:
1665 printf("%s: atalk %s; ",
1666 which, atalk_ntoa(su->sat.sat_addr));
1667 break;
1668 #ifdef NS
1669 case AF_NS:
1670 printf("%s: xns %s; ",
1671 which, ns_ntoa(su->sns.sns_addr));
1672 break;
1673 #endif
1675 fflush(stdout);
1678 /* States*/
1679 #define VIRGIN 0
1680 #define GOTONE 1
1681 #define GOTTWO 2
1682 /* Inputs */
1683 #define DIGIT (4*0)
1684 #define END (4*1)
1685 #define DELIM (4*2)
1687 static void
1688 sockaddr(char *addr, struct sockaddr *sa)
1690 char *cp = (char *)sa;
1691 int size = sa->sa_len;
1692 char *cplim = cp + size;
1693 int byte = 0, state = VIRGIN, new = 0 /* foil gcc */;
1695 memset(cp, 0, size);
1696 cp++;
1697 do {
1698 if ((*addr >= '0') && (*addr <= '9')) {
1699 new = *addr - '0';
1700 } else if ((*addr >= 'a') && (*addr <= 'f')) {
1701 new = *addr - 'a' + 10;
1702 } else if ((*addr >= 'A') && (*addr <= 'F')) {
1703 new = *addr - 'A' + 10;
1704 } else if (*addr == '\0')
1705 state |= END;
1706 else
1707 state |= DELIM;
1708 addr++;
1709 switch (state /* | INPUT */) {
1710 case GOTTWO | DIGIT:
1711 *cp++ = byte; /*FALLTHROUGH*/
1712 case VIRGIN | DIGIT:
1713 state = GOTONE; byte = new; continue;
1714 case GOTONE | DIGIT:
1715 state = GOTTWO; byte = new + (byte << 4); continue;
1716 default: /* | DELIM */
1717 state = VIRGIN; *cp++ = byte; byte = 0; continue;
1718 case GOTONE | END:
1719 case GOTTWO | END:
1720 *cp++ = byte; /* FALLTHROUGH */
1721 case VIRGIN | END:
1722 break;
1724 break;
1725 } while (cp < cplim);
1726 sa->sa_len = cp - (char *)sa;
1729 static int
1730 atalk_aton(const char *text, struct at_addr *addr)
1732 u_int net, node;
1734 if (sscanf(text, "%u.%u", &net, &node) != 2
1735 || net > 0xffff || node > 0xff)
1736 return(0);
1737 addr->s_net = htons(net);
1738 addr->s_node = node;
1739 return(1);
1742 static char *
1743 atalk_ntoa(struct at_addr at)
1745 static char buf[20];
1747 snprintf(buf, sizeof(buf), "%u.%u", ntohs(at.s_net), at.s_node);
1748 return(buf);