pc64: Turn CLK_USE_* flags into tunable, skip code when results are unused.
[dragonfly.git] / sbin / route / route.c
blobe48f2a8711d0462e119f02297c32e1a52ee6e2b0
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. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
29 * @(#) Copyright (c) 1983, 1989, 1991, 1993 The Regents of the University of California. All rights reserved.
30 * @(#)route.c 8.6 (Berkeley) 4/28/95
31 * $FreeBSD: src/sbin/route/route.c,v 1.40.2.11 2003/02/27 23:10:10 ru Exp $
34 #include <sys/param.h>
35 #include <sys/file.h>
36 #include <sys/socket.h>
37 #include <sys/ioctl.h>
38 #include <sys/sysctl.h>
39 #include <sys/types.h>
41 #include <net/if.h>
42 #include <net/route.h>
43 #include <net/if_dl.h>
44 #include <netinet/in.h>
45 #include <netinet/if_ether.h>
46 #include <arpa/inet.h>
47 #include <netdb.h>
49 #include <netproto/mpls/mpls.h>
51 #include <ctype.h>
52 #include <err.h>
53 #include <errno.h>
54 #include <paths.h>
55 #include <stdio.h>
56 #include <stdlib.h>
57 #include <string.h>
58 #include <sysexits.h>
59 #include <unistd.h>
60 #include <ifaddrs.h>
62 #include "extern.h"
63 #include "keywords.h"
65 union sockunion {
66 struct sockaddr sa;
67 struct sockaddr_in sin;
68 #ifdef INET6
69 struct sockaddr_in6 sin6;
70 #endif
71 struct sockaddr_mpls smpls;
72 struct sockaddr_dl sdl;
73 struct sockaddr_inarp sinarp;
74 struct sockaddr_storage ss; /* added to avoid memory overrun */
75 } so_dst, so_gate, so_mask, so_genmask, so_ifa, so_ifp, so_mpls1,
76 so_mpls2, so_mpls3;
78 typedef union sockunion *sup;
80 int nflag, wflag;
81 int cpuflag = -1;
83 static struct sockaddr_in so_gate_tried;
84 static struct rt_metrics rt_metrics;
85 static int pid, rtm_addrs;
86 static int s;
87 static int forcehost, forcenet, af, qflag, tflag;
88 static int iflag, verbose, aflen = sizeof(struct sockaddr_in);
89 static int locking, lockrest, debugonly;
90 static int mplsop, popcount, pushcount, swapcount;
91 static u_long rtm_inits;
92 static uid_t uid;
94 static void flushroutes(int, char **);
95 static void set_metric(char *, int);
96 static void newroute(int, char **);
97 static void inet_makenetandmask(u_long, struct sockaddr_in *, u_long);
98 static void interfaces(void);
99 static void monitor(void);
100 static void sockaddr(const char *, struct sockaddr *);
101 static void sodump(sup, const char *);
102 static void bprintf(FILE *, int, u_char *);
103 static void print_getmsg(struct rt_msghdr *, int);
104 static void print_rtmsg(struct rt_msghdr *, int);
105 static void pmsg_common(struct rt_msghdr *);
106 static void pmsg_addrs(char *, int);
107 static void mask_addr(void);
108 static int getaddr(int, const char *, struct hostent **);
109 static int rtmsg(int, int);
110 static int prefixlen(const char *);
111 #ifdef INET6
112 static int inet6_makenetandmask(struct sockaddr_in6 *, const char *);
113 #endif
115 void
116 usage(const char *cp)
118 if (cp != NULL)
119 warnx("bad keyword: %s", cp);
120 fprintf(stderr, "usage: route [-dnqtvw] [-c cpu] command [[modifiers] args]\n");
121 exit(EX_USAGE);
122 /* NOTREACHED */
126 main(int argc, char **argv)
128 int ch;
130 if (argc < 2)
131 usage(NULL);
133 while ((ch = getopt(argc, argv, "c:wnqdtv")) != -1)
134 switch(ch) {
135 case 'c':
136 cpuflag = strtol(optarg, NULL, 0);
137 break;
138 case 'w':
139 wflag = 1;
140 break;
141 case 'n':
142 nflag = 1;
143 break;
144 case 'q':
145 qflag = 1;
146 break;
147 case 'v':
148 verbose = 1;
149 break;
150 case 't':
151 tflag = 1;
152 break;
153 case 'd':
154 debugonly = 1;
155 break;
156 case '?':
157 default:
158 usage(NULL);
160 argc -= optind;
161 argv += optind;
163 pid = getpid();
164 uid = geteuid();
165 if (tflag)
166 s = open(_PATH_DEVNULL, O_WRONLY, 0);
167 else
168 s = socket(PF_ROUTE, SOCK_RAW, 0);
169 if (s < 0)
170 err(EX_OSERR, "socket");
171 if (*argv != NULL)
172 switch (keyword(*argv)) {
173 case K_GET:
174 uid = 0;
175 /* FALLTHROUGH */
177 case K_CHANGE:
178 case K_ADD:
179 case K_DELETE:
180 newroute(argc, argv);
181 /* NOTREACHED */
183 case K_SHOW:
184 show(argc, argv);
185 return(0);
187 case K_MONITOR:
188 monitor();
189 /* NOTREACHED */
191 case K_FLUSH:
192 flushroutes(argc, argv);
193 exit(0);
194 /* NOTREACHED */
196 usage(*argv);
197 /* NOTREACHED */
201 * Purge all entries in the routing tables not
202 * associated with network interfaces.
204 static void
205 flushroutes(int argc, char **argv)
207 size_t needed;
208 int mib[7], rlen, seqno;
209 int miblen;
210 char *buf, *next, *lim;
211 struct rt_msghdr *rtm;
213 if (uid != 0) {
214 errx(EX_NOPERM, "must be root to alter routing table");
216 shutdown(s, SHUT_RD); /* Don't want to read back our messages */
217 if (argc > 1) {
218 argv++;
219 if (argc == 2 && **argv == '-')
220 switch (keyword(*argv + 1)) {
221 case K_INET:
222 af = AF_INET;
223 break;
224 #ifdef INET6
225 case K_INET6:
226 af = AF_INET6;
227 break;
228 #endif
229 case K_LINK:
230 af = AF_LINK;
231 break;
232 case K_MPLS:
233 af = AF_MPLS;
234 break;
235 default:
236 goto bad;
237 } else
238 bad: usage(*argv);
240 mib[0] = CTL_NET;
241 mib[1] = PF_ROUTE;
242 mib[2] = 0; /* protocol */
243 mib[3] = 0; /* wildcard address family */
244 mib[4] = NET_RT_DUMP;
245 mib[5] = 0; /* no flags */
246 if (cpuflag >= 0) {
247 mib[6] = cpuflag;
248 miblen = 7;
249 } else {
250 miblen = 6;
252 if (sysctl(mib, miblen, NULL, &needed, NULL, 0) < 0)
253 err(EX_OSERR, "route-sysctl-estimate");
254 if ((buf = malloc(needed)) == NULL)
255 errx(EX_OSERR, "malloc failed");
256 if (sysctl(mib, miblen, buf, &needed, NULL, 0) < 0)
257 err(EX_OSERR, "route-sysctl-get");
258 lim = buf + needed;
259 if (verbose)
260 printf("Examining routing table from sysctl\n");
261 seqno = 0; /* ??? */
262 for (next = buf; next < lim; next += rtm->rtm_msglen) {
263 rtm = (struct rt_msghdr *)next;
264 if (verbose)
265 print_rtmsg(rtm, rtm->rtm_msglen);
266 if ((rtm->rtm_flags & RTF_GATEWAY) == 0)
267 continue;
268 if (af != 0) {
269 struct sockaddr *sa = (struct sockaddr *)(rtm + 1);
271 if (sa->sa_family != af)
272 continue;
274 if (debugonly)
275 continue;
276 rtm->rtm_type = RTM_DELETE;
277 rtm->rtm_seq = seqno;
278 rlen = write(s, next, rtm->rtm_msglen);
279 if (rlen < (int)rtm->rtm_msglen) {
280 warn("write to routing socket");
281 printf("got only %d for rlen\n", rlen);
282 break;
284 seqno++;
285 if (qflag)
286 continue;
287 if (verbose) {
288 print_rtmsg(rtm, rlen);
289 } else {
290 struct sockaddr *sa = (struct sockaddr *)(rtm + 1);
291 if (wflag) {
292 printf("%-20s ", rtm->rtm_flags & RTF_HOST ?
293 routename(sa) : netname(sa));
294 } else {
295 printf("%-20.20s ", rtm->rtm_flags & RTF_HOST ?
296 routename(sa) : netname(sa));
298 sa = (struct sockaddr *)(RT_ROUNDUP(sa->sa_len) +
299 (char *)sa);
300 if (wflag) {
301 printf("%-20s ", routename(sa));
302 } else {
303 printf("%-20.20s ", routename(sa));
305 printf("done\n");
310 char *
311 routename(struct sockaddr *sa)
313 const char *cp;
314 static char line[MAXHOSTNAMELEN + 1];
315 struct hostent *hp;
316 static char domain[MAXHOSTNAMELEN + 1];
317 static int first = 1;
319 if (first) {
320 first = 0;
321 if (gethostname(domain, MAXHOSTNAMELEN) == 0 &&
322 (cp = strchr(domain, '.'))) {
323 domain[MAXHOSTNAMELEN] = '\0';
324 strcpy(domain, cp + 1);
325 } else
326 domain[0] = 0;
329 if (sa->sa_len == 0)
330 strcpy(line, "default");
331 else switch (sa->sa_family) {
333 case AF_INET:
334 { struct in_addr in;
335 in = ((struct sockaddr_in *)sa)->sin_addr;
337 cp = NULL;
338 if (in.s_addr == INADDR_ANY || sa->sa_len < 4)
339 cp = "default";
340 if (cp == NULL && !nflag) {
341 hp = gethostbyaddr(&in, sizeof(struct in_addr),
342 AF_INET);
343 if (hp != NULL) {
344 char *cptr;
345 cptr = strchr(hp->h_name, '.');
346 if (cptr != NULL && !wflag &&
347 strcmp(cptr + 1, domain) == 0)
348 *cptr = '\0';
349 cp = hp->h_name;
352 if (cp != NULL) {
353 strncpy(line, cp, sizeof(line) - 1);
354 line[sizeof(line) - 1] = '\0';
355 } else
356 sprintf(line, "%s", inet_ntoa(in));
357 break;
360 #ifdef INET6
361 case AF_INET6:
363 struct sockaddr_in6 sin6; /* use static var for safety */
364 int niflags = 0;
365 #ifdef NI_WITHSCOPEID
366 niflags = NI_WITHSCOPEID;
367 #endif
369 memset(&sin6, 0, sizeof(sin6));
370 memcpy(&sin6, sa, sa->sa_len);
371 sin6.sin6_len = sizeof(struct sockaddr_in6);
372 sin6.sin6_family = AF_INET6;
373 #ifdef __KAME__
374 if (sa->sa_len == sizeof(struct sockaddr_in6) &&
375 (IN6_IS_ADDR_LINKLOCAL(&sin6.sin6_addr) ||
376 IN6_IS_ADDR_MC_LINKLOCAL(&sin6.sin6_addr)) &&
377 sin6.sin6_scope_id == 0) {
378 sin6.sin6_scope_id =
379 ntohs(*(u_int16_t *)&sin6.sin6_addr.s6_addr[2]);
380 sin6.sin6_addr.s6_addr[2] = 0;
381 sin6.sin6_addr.s6_addr[3] = 0;
383 #endif
384 if (nflag)
385 niflags |= NI_NUMERICHOST;
386 if (getnameinfo((struct sockaddr *)&sin6, sin6.sin6_len,
387 line, sizeof(line), NULL, 0, niflags) != 0)
388 strncpy(line, "invalid", sizeof(line));
390 return(line);
392 #endif
394 case AF_LINK:
395 return(link_ntoa((struct sockaddr_dl *)sa));
397 default:
400 * Unknown address family; just render the raw
401 * data in sa->sa_data as hex values.
403 uint8_t *sp = (uint8_t *)sa->sa_data;
404 uint8_t *splim = (uint8_t *)sa + sa->sa_len;
405 char *cps = line + sprintf(line, "(%d)", sa->sa_family);
406 char *cpe = line + sizeof(line);
408 while (sp < splim && cps < cpe)
409 cps += snprintf(cps, cpe - cps, " %02x", *sp++);
410 break;
413 return(line);
417 * Return the name of the network whose address is given.
418 * The address is assumed to be that of a net or subnet, not a host.
420 char *
421 netname(struct sockaddr *sa)
423 const char *cp = NULL;
424 static char line[MAXHOSTNAMELEN + 1];
425 struct netent *np = NULL;
426 u_long net, mask;
427 u_long i;
428 int subnetshift;
430 switch (sa->sa_family) {
432 case AF_INET:
433 { struct in_addr in;
434 in = ((struct sockaddr_in *)sa)->sin_addr;
436 i = in.s_addr = ntohl(in.s_addr);
437 if (in.s_addr == 0)
438 cp = "default";
439 else if (!nflag) {
440 if (IN_CLASSA(i)) {
441 mask = IN_CLASSA_NET;
442 subnetshift = 8;
443 } else if (IN_CLASSB(i)) {
444 mask = IN_CLASSB_NET;
445 subnetshift = 8;
446 } else {
447 mask = IN_CLASSC_NET;
448 subnetshift = 4;
451 * If there are more bits than the standard mask
452 * would suggest, subnets must be in use.
453 * Guess at the subnet mask, assuming reasonable
454 * width subnet fields.
456 while (in.s_addr &~ mask)
457 mask |= mask >> subnetshift;
458 net = in.s_addr & mask;
459 while ((mask & 1) == 0)
460 mask >>= 1, net >>= 1;
461 np = getnetbyaddr(net, AF_INET);
462 if (np != NULL)
463 cp = np->n_name;
465 #define C(x) (unsigned)((x) & 0xff)
466 if (cp != NULL)
467 strncpy(line, cp, sizeof(line));
468 else if ((in.s_addr & 0xffffff) == 0)
469 sprintf(line, "%u", C(in.s_addr >> 24));
470 else if ((in.s_addr & 0xffff) == 0)
471 sprintf(line, "%u.%u", C(in.s_addr >> 24),
472 C(in.s_addr >> 16));
473 else if ((in.s_addr & 0xff) == 0)
474 sprintf(line, "%u.%u.%u", C(in.s_addr >> 24),
475 C(in.s_addr >> 16), C(in.s_addr >> 8));
476 else
477 sprintf(line, "%u.%u.%u.%u", C(in.s_addr >> 24),
478 C(in.s_addr >> 16), C(in.s_addr >> 8),
479 C(in.s_addr));
480 #undef C
481 break;
484 #ifdef INET6
485 case AF_INET6:
487 struct sockaddr_in6 sin6; /* use static var for safety */
488 int niflags = 0;
489 #ifdef NI_WITHSCOPEID
490 niflags = NI_WITHSCOPEID;
491 #endif
493 memset(&sin6, 0, sizeof(sin6));
494 memcpy(&sin6, sa, sa->sa_len);
495 sin6.sin6_len = sizeof(struct sockaddr_in6);
496 sin6.sin6_family = AF_INET6;
497 #ifdef __KAME__
498 if (sa->sa_len == sizeof(struct sockaddr_in6) &&
499 (IN6_IS_ADDR_LINKLOCAL(&sin6.sin6_addr) ||
500 IN6_IS_ADDR_MC_LINKLOCAL(&sin6.sin6_addr)) &&
501 sin6.sin6_scope_id == 0) {
502 sin6.sin6_scope_id =
503 ntohs(*(u_int16_t *)&sin6.sin6_addr.s6_addr[2]);
504 sin6.sin6_addr.s6_addr[2] = 0;
505 sin6.sin6_addr.s6_addr[3] = 0;
507 #endif
508 if (nflag)
509 niflags |= NI_NUMERICHOST;
510 if (getnameinfo((struct sockaddr *)&sin6, sin6.sin6_len,
511 line, sizeof(line), NULL, 0, niflags) != 0)
512 strncpy(line, "invalid", sizeof(line));
514 return(line);
516 #endif
518 case AF_LINK:
519 return(link_ntoa((struct sockaddr_dl *)sa));
522 default:
525 * Unknown address family; just render the raw
526 * data in sa->sa_data as hex values.
528 uint8_t *sp = (uint8_t *)sa->sa_data;
529 uint8_t *splim = (uint8_t *)sa + sa->sa_len;
530 char *cps = line + sprintf(line, "af %d:", sa->sa_family);
531 char *cpe = line + sizeof(line);
533 while (sp < splim && cps < cpe)
534 cps += snprintf(cps, cpe - cps, " %02x", *sp++);
535 break;
538 return(line);
541 static void
542 set_metric(char *value, int key)
544 int flag = 0;
545 u_long noval, *valp = &noval;
547 switch (key) {
548 #define caseof(x, y, z) case x: valp = &rt_metrics.z; flag = y; break
549 caseof(K_MTU, RTV_MTU, rmx_mtu);
550 caseof(K_HOPCOUNT, RTV_HOPCOUNT, rmx_hopcount);
551 caseof(K_EXPIRE, RTV_EXPIRE, rmx_expire);
552 caseof(K_RECVPIPE, RTV_RPIPE, rmx_recvpipe);
553 caseof(K_SENDPIPE, RTV_SPIPE, rmx_sendpipe);
554 caseof(K_SSTHRESH, RTV_SSTHRESH, rmx_ssthresh);
555 caseof(K_RTT, RTV_RTT, rmx_rtt);
556 caseof(K_RTTVAR, RTV_RTTVAR, rmx_rttvar);
557 caseof(K_MSL, RTV_MSL, rmx_msl);
558 caseof(K_IWMAX, RTV_IWMAXSEGS, rmx_iwmaxsegs);
559 caseof(K_IW, RTV_IWCAPSEGS, rmx_iwcapsegs);
561 rtm_inits |= flag;
562 if (lockrest || locking)
563 rt_metrics.rmx_locks |= flag;
564 if (locking)
565 locking = 0;
566 *valp = atoi(value);
569 static void
570 newroute(int argc, char **argv)
572 char *cmd;
573 const char *err_str, *dest = "", *gateway = "";
574 int ishost = 0, proxy = 0, ret, attempts, oerrno, flags = RTF_STATIC;
575 int key;
576 struct hostent *hp = NULL;
578 if (uid != 0) {
579 errx(EX_NOPERM, "must be root to alter routing table");
581 cmd = argv[0];
582 if (*cmd != 'g')
583 shutdown(s, SHUT_RD); /* Don't want to read back our messages */
584 while (--argc > 0) {
585 if (**(++argv)== '-') {
586 switch (key = keyword(1 + *argv)) {
587 case K_LINK:
588 af = AF_LINK;
589 aflen = sizeof(struct sockaddr_dl);
590 break;
591 case K_INET:
592 af = AF_INET;
593 aflen = sizeof(struct sockaddr_in);
594 break;
595 #ifdef INET6
596 case K_INET6:
597 af = AF_INET6;
598 aflen = sizeof(struct sockaddr_in6);
599 break;
600 #endif
601 case K_SA:
602 af = PF_ROUTE;
603 aflen = sizeof(union sockunion);
604 break;
605 case K_IFACE:
606 case K_INTERFACE:
607 iflag++;
608 break;
609 case K_NOSTATIC:
610 flags &= ~RTF_STATIC;
611 break;
612 case K_LLINFO:
613 flags |= RTF_LLINFO;
614 break;
615 case K_LOCK:
616 locking = 1;
617 break;
618 case K_LOCKREST:
619 lockrest = 1;
620 break;
621 case K_HOST:
622 forcehost++;
623 break;
624 case K_REJECT:
625 flags |= RTF_REJECT;
626 break;
627 case K_BLACKHOLE:
628 flags |= RTF_BLACKHOLE;
629 break;
630 case K_PROTO1:
631 flags |= RTF_PROTO1;
632 break;
633 case K_PROTO2:
634 flags |= RTF_PROTO2;
635 break;
636 case K_PROXY:
637 proxy = 1;
638 break;
639 case K_CLONING:
640 flags |= RTF_CLONING;
641 break;
642 case K_XRESOLVE:
643 flags |= RTF_XRESOLVE;
644 break;
645 case K_STATIC:
646 flags |= RTF_STATIC;
647 break;
648 case K_IFA:
649 if (--argc == 0)
650 usage(NULL);
651 getaddr(RTA_IFA, *++argv, 0);
652 break;
653 case K_IFP:
654 if (--argc == 0)
655 usage(NULL);
656 getaddr(RTA_IFP, *++argv, 0);
657 break;
658 case K_GENMASK:
659 if (--argc == 0)
660 usage(NULL);
661 getaddr(RTA_GENMASK, *++argv, 0);
662 break;
663 case K_GATEWAY:
664 if (--argc == 0)
665 usage(NULL);
666 getaddr(RTA_GATEWAY, *++argv, 0);
667 break;
668 case K_DST:
669 if (--argc == 0)
670 usage(NULL);
671 ishost = getaddr(RTA_DST, *++argv, &hp);
672 dest = *argv;
673 break;
674 case K_NETMASK:
675 if (--argc == 0)
676 usage(NULL);
677 getaddr(RTA_NETMASK, *++argv, 0);
678 /* FALLTHROUGH */
679 case K_NET:
680 forcenet++;
681 break;
682 case K_PREFIXLEN:
683 if (--argc == 0)
684 usage(NULL);
685 if (prefixlen(*++argv) == -1) {
686 forcenet = 0;
687 ishost = 1;
688 } else {
689 forcenet = 1;
690 ishost = 0;
692 break;
693 case K_MPLS:
694 af = AF_MPLS;
695 aflen = sizeof(struct sockaddr_mpls);
696 break;
697 case K_POP:
698 flags |= RTF_MPLSOPS;
699 af = AF_MPLS;
700 mplsop = MPLSLOP_POP;
701 switch(++popcount){
702 case 1:
703 getaddr(RTA_MPLS1, "", 0);
704 break;
705 case 2:
706 getaddr(RTA_MPLS2, "", 0);
707 break;
708 case 3:
709 getaddr(RTA_MPLS3, "", 0);
710 break;
712 break;
713 case K_PUSH:
714 case K_SWAP:
715 if (popcount > 0) {
716 warnx("Push or swap after pop. Ignoring.");
717 break;
719 if (key == K_PUSH) {
720 mplsop = MPLSLOP_PUSH;
721 ++pushcount;
722 } else {
723 if (pushcount > 0) {
724 warnx("Swap after push. Ignoring.");
725 break;
727 if (swapcount > 0) {
728 warnx("Too many swaps. Ignoring.");
729 break;
731 mplsop = MPLSLOP_SWAP;
732 ++swapcount;
734 flags |= RTF_MPLSOPS;
735 af = AF_MPLS;
736 aflen = sizeof(struct sockaddr_mpls);
737 if (--argc == 0)
738 usage(NULL);
739 switch(pushcount + swapcount){
740 case 1:
741 getaddr(RTA_MPLS1, *++argv, 0);
742 break;
743 case 2:
744 getaddr(RTA_MPLS2, *++argv, 0);
745 break;
746 case 3:
747 getaddr(RTA_MPLS3, *++argv, 0);
748 break;
750 break;
751 case K_MTU:
752 case K_HOPCOUNT:
753 case K_EXPIRE:
754 case K_RECVPIPE:
755 case K_SENDPIPE:
756 case K_SSTHRESH:
757 case K_RTT:
758 case K_RTTVAR:
759 case K_MSL:
760 case K_IWMAX:
761 case K_IW:
762 if (--argc == 0)
763 usage(NULL);
764 set_metric(*++argv, key);
765 break;
766 default:
767 usage(1+*argv);
769 } else {
770 if ((rtm_addrs & RTA_DST) == 0) {
771 dest = *argv;
772 ishost = getaddr(RTA_DST, *argv, &hp);
773 } else if ((rtm_addrs & RTA_GATEWAY) == 0) {
774 gateway = *argv;
775 getaddr(RTA_GATEWAY, *argv, &hp);
776 } else {
777 getaddr(RTA_NETMASK, *argv, 0);
778 forcenet = 1;
782 if (forcehost) {
783 ishost = 1;
784 #ifdef INET6
785 if (af == AF_INET6) {
786 rtm_addrs &= ~RTA_NETMASK;
787 memset((void *)&so_mask, 0, sizeof(so_mask));
789 #endif
791 if (forcenet)
792 ishost = 0;
793 flags |= RTF_UP;
794 if (ishost)
795 flags |= RTF_HOST;
796 if (iflag == 0)
797 flags |= RTF_GATEWAY;
798 if (proxy) {
799 so_dst.sinarp.sin_other = SIN_PROXY;
800 flags |= RTF_ANNOUNCE;
802 for (attempts = 1; ; attempts++) {
803 errno = 0;
804 if ((ret = rtmsg(*cmd, flags)) == 0)
805 break;
806 if (errno != ENETUNREACH && errno != ESRCH)
807 break;
808 if (af == AF_INET && *gateway != '\0' &&
809 hp != NULL && hp->h_addr_list[1] != NULL) {
810 so_gate_tried = so_gate.sin;
811 hp->h_addr_list++;
812 memmove(&so_gate.sin.sin_addr,
813 hp->h_addr_list[0],
814 MIN((size_t)hp->h_length,
815 sizeof(so_gate.sin.sin_addr)));
816 } else
817 break;
819 if (*cmd == 'g')
820 exit(0);
821 if (!qflag) {
822 oerrno = errno;
823 printf("%s %s %s", cmd, ishost? "host" : "net", dest);
824 if (*gateway != '\0') {
825 printf(": gateway %s", gateway);
826 if (attempts > 1 && ret == 0 && af == AF_INET) {
827 printf(" (%s)",
828 inet_ntoa(so_gate_tried.sin_addr));
831 if (ret == 0) {
832 printf("\n");
833 } else {
834 switch (oerrno) {
835 case ESRCH:
836 err_str = "not in table";
837 break;
838 case EBUSY:
839 err_str = "entry in use";
840 break;
841 case ENOBUFS:
842 err_str = "routing table overflow";
843 break;
844 case EDQUOT: /* handle recursion avoidance in
845 rt_setgate() */
846 err_str = "gateway uses the same route";
847 break;
848 default:
849 err_str = strerror(oerrno);
850 break;
852 printf(": %s\n", err_str);
855 exit(ret != 0);
858 static void
859 inet_makenetandmask(u_long net, struct sockaddr_in *in, u_long bits)
861 u_long addr, mask = 0;
862 char *cp;
864 rtm_addrs |= RTA_NETMASK;
866 * XXX: This approach unable to handle 0.0.0.1/32 correctly
867 * as inet_network() converts 0.0.0.1 and 1 equally.
869 if (net <= 0xff)
870 addr = net << IN_CLASSA_NSHIFT;
871 else if (net <= 0xffff)
872 addr = net << IN_CLASSB_NSHIFT;
873 else if (net <= 0xffffff)
874 addr = net << IN_CLASSC_NSHIFT;
875 else
876 addr = net;
878 if (bits != 0)
879 mask = 0xffffffff << (32 - bits);
880 else if (net == 0)
881 mask = 0;
882 else if (IN_CLASSA(addr))
883 mask = IN_CLASSA_NET;
884 else if (IN_CLASSB(addr))
885 mask = IN_CLASSB_NET;
886 else if (IN_CLASSC(addr))
887 mask = IN_CLASSC_NET;
888 else if (IN_MULTICAST(addr))
889 mask = IN_CLASSD_NET;
890 else
891 mask = 0xffffffff;
893 in->sin_addr.s_addr = htonl(addr);
894 in = &so_mask.sin;
895 in->sin_addr.s_addr = htonl(mask);
896 in->sin_len = 0;
897 in->sin_family = 0;
898 cp = (char *)(&in->sin_addr + 1);
899 while (*--cp == 0 && cp > (char *)in)
901 in->sin_len = 1 + cp - (char *)in;
904 #ifdef INET6
906 * XXX the function may need more improvement...
908 static int
909 inet6_makenetandmask(struct sockaddr_in6 *sin6, const char *plen)
911 struct in6_addr in6;
913 if (plen == NULL) {
914 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr) &&
915 sin6->sin6_scope_id == 0) {
916 plen = "0";
917 } else if ((sin6->sin6_addr.s6_addr[0] & 0xe0) == 0x20) {
918 /* aggregatable global unicast - RFC2374 */
919 memset(&in6, 0, sizeof(in6));
920 if (memcmp(&sin6->sin6_addr.s6_addr[8],
921 &in6.s6_addr[8], 8) == 0)
922 plen = "64";
926 if (plen == NULL || strcmp(plen, "128") == 0)
927 return(1);
928 rtm_addrs |= RTA_NETMASK;
929 prefixlen(plen);
930 return(0);
932 #endif
935 * Interpret an argument as a network address of some kind,
936 * returning 1 if a host address, 0 if a network address.
938 static int
939 getaddr(int which, const char *str, struct hostent **hpp)
941 sup su;
942 struct hostent *hp;
943 struct netent *np;
944 u_long val;
945 char *q;
946 int afamily; /* local copy of af so we can change it */
948 if (af == 0) {
949 af = AF_INET;
950 aflen = sizeof(struct sockaddr_in);
952 afamily = af;
953 rtm_addrs |= which;
954 switch (which) {
955 case RTA_DST:
956 su = &so_dst;
957 break;
958 case RTA_GATEWAY:
959 su = &so_gate;
960 if (iflag) {
961 struct ifaddrs *ifap, *ifa;
962 struct sockaddr_dl *sdl = NULL;
964 if (getifaddrs(&ifap))
965 err(1, "getifaddrs");
967 for (ifa = ifap; ifa != NULL; ifa = ifa->ifa_next) {
968 if (ifa->ifa_addr->sa_family != AF_LINK)
969 continue;
971 if (strcmp(str, ifa->ifa_name) != 0)
972 continue;
974 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
976 /* If we found it, then use it */
977 if (sdl != NULL) {
979 * Copy is safe since we have a
980 * sockaddr_storage member in sockunion{}.
981 * Note that we need to copy before calling
982 * freeifaddrs().
984 * Interface routes are routed only by their
985 * sdl_index or interface name (we provide
986 * both here), not by the ARP address of the
987 * local interface (alen, slen = 0).
989 memcpy(&su->sdl, sdl, sdl->sdl_len);
990 su->sdl.sdl_alen = 0;
991 su->sdl.sdl_slen = 0;
993 freeifaddrs(ifap);
994 if (sdl != NULL)
995 return(1);
997 break;
998 case RTA_NETMASK:
999 su = &so_mask;
1000 break;
1001 case RTA_GENMASK:
1002 su = &so_genmask;
1003 break;
1004 case RTA_IFP:
1005 su = &so_ifp;
1006 afamily = AF_LINK;
1007 break;
1008 case RTA_IFA:
1009 su = &so_ifa;
1010 break;
1011 case RTA_MPLS1:
1012 su = &so_mpls1;
1013 break;
1014 case RTA_MPLS2:
1015 su = &so_mpls2;
1016 break;
1017 case RTA_MPLS3:
1018 su = &so_mpls3;
1019 break;
1020 default:
1021 usage("internal error");
1022 /*NOTREACHED*/
1024 su->sa.sa_len = aflen;
1025 su->sa.sa_family = afamily; /* cases that don't want it have left
1026 already */
1027 if (strcmp(str, "default") == 0) {
1029 * Default is net 0.0.0.0/0
1031 switch (which) {
1032 case RTA_DST:
1033 forcenet++;
1034 /* bzero(su, sizeof(*su)); *//* for readability */
1035 getaddr(RTA_NETMASK, str, 0);
1036 break;
1037 case RTA_NETMASK:
1038 case RTA_GENMASK:
1039 /* bzero(su, sizeof(*su)); *//* for readability */
1040 break;
1042 return(0);
1044 switch (afamily) {
1045 #ifdef INET6
1046 case AF_INET6:
1048 struct addrinfo hints, *res;
1050 q = NULL;
1051 if (which == RTA_DST && (q = strchr(str, '/')) != NULL)
1052 *q = '\0';
1053 memset(&hints, 0, sizeof(hints));
1054 hints.ai_family = afamily; /*AF_INET6*/
1055 hints.ai_flags = AI_NUMERICHOST;
1056 hints.ai_socktype = SOCK_DGRAM; /*dummy*/
1057 if (getaddrinfo(str, "0", &hints, &res) != 0 ||
1058 res->ai_family != AF_INET6 ||
1059 res->ai_addrlen != sizeof(su->sin6)) {
1060 fprintf(stderr, "%s: bad value\n", str);
1061 exit(1);
1063 memcpy(&su->sin6, res->ai_addr, sizeof(su->sin6));
1064 #ifdef __KAME__
1065 if ((IN6_IS_ADDR_LINKLOCAL(&su->sin6.sin6_addr) ||
1066 IN6_IS_ADDR_LINKLOCAL(&su->sin6.sin6_addr)) &&
1067 su->sin6.sin6_scope_id) {
1068 *(u_int16_t *)&su->sin6.sin6_addr.s6_addr[2] =
1069 htons(su->sin6.sin6_scope_id);
1070 su->sin6.sin6_scope_id = 0;
1072 #endif
1073 freeaddrinfo(res);
1074 if (q != NULL)
1075 *q++ = '/';
1076 if (which == RTA_DST)
1077 return(inet6_makenetandmask(&su->sin6, q));
1078 return(0);
1080 #endif /* INET6 */
1082 case AF_LINK:
1083 link_addr(str, &su->sdl);
1084 return(1);
1086 case PF_ROUTE:
1087 su->sa.sa_len = sizeof(*su);
1088 sockaddr(str, &su->sa);
1089 return(1);
1091 case AF_MPLS:
1093 mpls_label_t label;
1095 bzero(su, sizeof(*su));
1096 su->sa.sa_len = sizeof(*su);
1097 su->sa.sa_family = AF_MPLS;
1098 su->smpls.smpls_op = mplsop;
1099 if (mplsop != MPLSLOP_POP && mplsop != MPLSLOP_POPALL &&
1100 *str != '\0') {
1101 if (sscanf(str, "%u", &label) != 1)
1102 errx(EX_NOHOST, "bad address: %s", str);
1103 su->smpls.smpls_label = htonl(label);
1105 return(1);
1108 case AF_INET:
1109 default:
1110 break;
1113 if (hpp == NULL)
1114 hpp = &hp;
1115 *hpp = NULL;
1117 q = strchr(str,'/');
1118 if (q != NULL && which == RTA_DST) {
1119 *q = '\0';
1120 if ((val = inet_network(str)) != INADDR_NONE) {
1121 inet_makenetandmask(
1122 val, &su->sin, strtoul(q+1, 0, 0));
1123 return(0);
1125 *q = '/';
1127 if ((which != RTA_DST || forcenet == 0) &&
1128 inet_aton(str, &su->sin.sin_addr)) {
1129 val = su->sin.sin_addr.s_addr;
1130 if (which != RTA_DST ||
1131 inet_lnaof(su->sin.sin_addr) != INADDR_ANY)
1132 return(1);
1133 else {
1134 val = ntohl(val);
1135 goto netdone;
1138 if (which == RTA_DST && forcehost == 0 &&
1139 ((val = inet_network(str)) != INADDR_NONE ||
1140 ((np = getnetbyname(str)) != NULL && (val = np->n_net) != 0))) {
1141 netdone:
1142 inet_makenetandmask(val, &su->sin, 0);
1143 return(0);
1145 hp = gethostbyname(str);
1146 if (hp != NULL) {
1147 *hpp = hp;
1148 su->sin.sin_family = hp->h_addrtype;
1149 memmove((char *)&su->sin.sin_addr, hp->h_addr,
1150 MIN((size_t)hp->h_length, sizeof(su->sin.sin_addr)));
1151 return(1);
1153 errx(EX_NOHOST, "bad address: %s", str);
1156 static int
1157 prefixlen(const char *len_str)
1159 int len = atoi(len_str), q, r;
1160 int max;
1161 char *p;
1163 rtm_addrs |= RTA_NETMASK;
1164 switch (af) {
1165 #ifdef INET6
1166 case AF_INET6:
1167 max = 128;
1168 p = (char *)&so_mask.sin6.sin6_addr;
1169 break;
1170 #endif
1171 case AF_INET:
1172 max = 32;
1173 p = (char *)&so_mask.sin.sin_addr;
1174 break;
1175 default:
1176 fprintf(stderr, "prefixlen not supported in this af\n");
1177 exit(1);
1178 /*NOTREACHED*/
1181 if (len < 0 || max < len) {
1182 fprintf(stderr, "%s: bad value\n", len_str);
1183 exit(1);
1186 q = len >> 3;
1187 r = len & 7;
1188 so_mask.sa.sa_family = af;
1189 so_mask.sa.sa_len = aflen;
1190 memset((void *)p, 0, max / 8);
1191 if (q > 0)
1192 memset((void *)p, 0xff, q);
1193 if (r > 0)
1194 *((u_char *)p + q) = (0xff00 >> r) & 0xff;
1195 if (len == max)
1196 return(-1);
1197 else
1198 return(len);
1201 static void
1202 interfaces(void)
1204 size_t needed;
1205 int mib[6];
1206 char *buf, *lim, *next;
1207 struct rt_msghdr *rtm;
1209 mib[0] = CTL_NET;
1210 mib[1] = PF_ROUTE;
1211 mib[2] = 0; /* protocol */
1212 mib[3] = 0; /* wildcard address family */
1213 mib[4] = NET_RT_IFLIST;
1214 mib[5] = 0; /* no flags */
1215 if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
1216 err(EX_OSERR, "route-sysctl-estimate");
1217 if ((buf = malloc(needed)) == NULL)
1218 errx(EX_OSERR, "malloc failed");
1219 if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0)
1220 err(EX_OSERR, "actual retrieval of interface table");
1221 lim = buf + needed;
1222 for (next = buf; next < lim; next += rtm->rtm_msglen) {
1223 rtm = (struct rt_msghdr *)next;
1224 print_rtmsg(rtm, rtm->rtm_msglen);
1228 static void
1229 monitor(void)
1231 int n;
1232 char msg[2048];
1234 verbose = 1;
1235 if (debugonly) {
1236 interfaces();
1237 exit(0);
1239 for (;;) {
1240 time_t now;
1241 n = read(s, msg, 2048);
1242 now = time(NULL);
1243 printf("\ngot message of size %d on %s", n, ctime(&now));
1244 print_rtmsg((struct rt_msghdr *)msg, n);
1248 struct {
1249 struct rt_msghdr m_rtm;
1250 char m_space[512];
1251 } m_rtmsg;
1253 static int
1254 rtmsg(int cmd, int flags)
1256 static int seq;
1257 int rlen;
1258 char *cp = m_rtmsg.m_space;
1259 int l;
1261 #define NEXTADDR(w, u) \
1262 if (rtm_addrs & (w)) {\
1263 l = RT_ROUNDUP(u.sa.sa_len); memmove(cp, &(u), l); cp += l;\
1264 if (verbose) sodump(&(u),"u");\
1267 errno = 0;
1268 memset(&m_rtmsg, 0, sizeof(m_rtmsg));
1269 if (cmd == 'a')
1270 cmd = RTM_ADD;
1271 else if (cmd == 'c')
1272 cmd = RTM_CHANGE;
1273 else if (cmd == 'g') {
1274 cmd = RTM_GET;
1275 if (so_ifp.sa.sa_family == 0) {
1276 so_ifp.sa.sa_family = AF_LINK;
1277 so_ifp.sa.sa_len = sizeof(struct sockaddr_dl);
1278 rtm_addrs |= RTA_IFP;
1280 } else
1281 cmd = RTM_DELETE;
1282 #define rtm m_rtmsg.m_rtm
1283 rtm.rtm_type = cmd;
1284 rtm.rtm_flags = flags;
1285 rtm.rtm_version = RTM_VERSION;
1286 rtm.rtm_seq = ++seq;
1287 rtm.rtm_addrs = rtm_addrs;
1288 rtm.rtm_rmx = rt_metrics;
1289 rtm.rtm_inits = rtm_inits;
1291 if (rtm_addrs & RTA_NETMASK)
1292 mask_addr();
1293 NEXTADDR(RTA_DST, so_dst);
1294 NEXTADDR(RTA_GATEWAY, so_gate);
1295 NEXTADDR(RTA_NETMASK, so_mask);
1296 NEXTADDR(RTA_GENMASK, so_genmask);
1297 NEXTADDR(RTA_IFP, so_ifp);
1298 NEXTADDR(RTA_IFA, so_ifa);
1299 NEXTADDR(RTA_MPLS1, so_mpls1);
1300 NEXTADDR(RTA_MPLS2, so_mpls2);
1301 NEXTADDR(RTA_MPLS3, so_mpls3);
1302 rtm.rtm_msglen = l = cp - (char *)&m_rtmsg;
1303 if (verbose)
1304 print_rtmsg(&rtm, l);
1305 if (debugonly)
1306 return(0);
1307 if ((rlen = write(s, (char *)&m_rtmsg, l)) < 0) {
1308 warn("writing to routing socket");
1309 return(-1);
1311 if (cmd == RTM_GET) {
1312 do {
1313 l = read(s, (char *)&m_rtmsg, sizeof(m_rtmsg));
1314 } while (l > 0 && (rtm.rtm_seq != seq || rtm.rtm_pid != pid));
1315 if (l < 0)
1316 warn("read from routing socket");
1317 else
1318 print_getmsg(&rtm, l);
1320 #undef rtm
1321 return(0);
1324 static void
1325 mask_addr(void)
1327 int olen = so_mask.sa.sa_len;
1328 char *cp1 = olen + (char *)&so_mask, *cp2;
1330 for (so_mask.sa.sa_len = 0; cp1 > (char *)&so_mask; )
1331 if (*--cp1 != 0) {
1332 so_mask.sa.sa_len = 1 + cp1 - (char *)&so_mask;
1333 break;
1335 if ((rtm_addrs & RTA_DST) == 0)
1336 return;
1337 switch (so_dst.sa.sa_family) {
1338 case AF_INET:
1339 #ifdef INET6
1340 case AF_INET6:
1341 #endif
1342 case 0:
1343 return;
1345 cp1 = so_mask.sa.sa_len + 1 + (char *)&so_dst;
1346 cp2 = so_dst.sa.sa_len + 1 + (char *)&so_dst;
1347 while (cp2 > cp1)
1348 *--cp2 = 0;
1349 cp2 = so_mask.sa.sa_len + 1 + (char *)&so_mask;
1350 while (cp1 > so_dst.sa.sa_data)
1351 *--cp1 &= *--cp2;
1354 const char *msgtypes[] = {
1356 "RTM_ADD: Add Route",
1357 "RTM_DELETE: Delete Route",
1358 "RTM_CHANGE: Change Metrics or flags",
1359 "RTM_GET: Report Metrics",
1360 "RTM_LOSING: Kernel Suspects Partitioning",
1361 "RTM_REDIRECT: Told to use different route",
1362 "RTM_MISS: Lookup failed on this address",
1363 "RTM_LOCK: fix specified metrics",
1364 "0x9: unused",
1365 "0xa: unused",
1366 "RTM_RESOLVE: Route created by cloning",
1367 "RTM_NEWADDR: address being added to iface",
1368 "RTM_DELADDR: address being removed from iface",
1369 "RTM_IFINFO: iface status change",
1370 "RTM_NEWMADDR: new multicast group membership on iface",
1371 "RTM_DELMADDR: multicast group membership removed from iface",
1372 "RTM_IFANNOUNCE: interface arrival/departure",
1376 char metricnames[] =
1377 "\013iw\012iwmax\011msl\010rttvar\7rtt\6ssthresh\5sendpipe\4recvpipe\3expire"
1378 "\2hopcount\1mtu";
1379 char routeflags[] =
1380 "\1UP\2GATEWAY\3HOST\4REJECT\5DYNAMIC\6MODIFIED\7DONE\010MASK_PRESENT"
1381 "\011CLONING\012XRESOLVE\013LLINFO\014STATIC\015BLACKHOLE\016b016"
1382 "\017PROTO2\020PROTO1\021PRCLONING\022WASCLONED\023PROTO3\024CHAINDELETE"
1383 "\025PINNED\026LOCAL\027BROADCAST\030MULTICAST";
1384 char ifnetflags[] =
1385 "\1UP\2BROADCAST\3DEBUG\4LOOPBACK\5PTP\6b6\7RUNNING\010NOARP"
1386 "\011PPROMISC\012ALLMULTI\013OACTIVE\014SIMPLEX\015LINK0\016LINK1"
1387 "\017LINK2\020MULTICAST";
1388 char addrnames[] =
1389 "\1DST\2GATEWAY\3NETMASK\4GENMASK\5IFP\6IFA\7AUTHOR\010BRD"
1390 "\011MPLS1\012MPLS2\013MPLS3";
1392 static void
1393 print_rtmsg(struct rt_msghdr *rtm, int msglen __unused)
1395 struct if_msghdr *ifm;
1396 struct ifa_msghdr *ifam;
1397 #ifdef RTM_NEWMADDR
1398 struct ifma_msghdr *ifmam;
1399 #endif
1400 struct if_announcemsghdr *ifan;
1402 if (verbose == 0)
1403 return;
1404 if (rtm->rtm_version != RTM_VERSION) {
1405 printf("routing message version %d not understood\n",
1406 rtm->rtm_version);
1407 return;
1409 if (msgtypes[rtm->rtm_type] != NULL)
1410 printf("%s: ", msgtypes[rtm->rtm_type]);
1411 else
1412 printf("#%d: ", rtm->rtm_type);
1413 printf("len %d, ", rtm->rtm_msglen);
1414 switch (rtm->rtm_type) {
1415 case RTM_IFINFO:
1416 ifm = (struct if_msghdr *)rtm;
1417 printf("if# %d, flags:", ifm->ifm_index);
1418 bprintf(stdout, ifm->ifm_flags, ifnetflags);
1419 pmsg_addrs((char *)(ifm + 1), ifm->ifm_addrs);
1420 break;
1421 case RTM_NEWADDR:
1422 case RTM_DELADDR:
1423 ifam = (struct ifa_msghdr *)rtm;
1424 printf("metric %d, flags:", ifam->ifam_metric);
1425 bprintf(stdout, ifam->ifam_flags, routeflags);
1426 pmsg_addrs((char *)(ifam + 1), ifam->ifam_addrs);
1427 break;
1428 #ifdef RTM_NEWMADDR
1429 case RTM_NEWMADDR:
1430 case RTM_DELMADDR:
1431 ifmam = (struct ifma_msghdr *)rtm;
1432 pmsg_addrs((char *)(ifmam + 1), ifmam->ifmam_addrs);
1433 break;
1434 #endif
1435 case RTM_IFANNOUNCE:
1436 ifan = (struct if_announcemsghdr *)rtm;
1437 printf("if# %d, what: ", ifan->ifan_index);
1438 switch (ifan->ifan_what) {
1439 case IFAN_ARRIVAL:
1440 printf("arrival");
1441 break;
1442 case IFAN_DEPARTURE:
1443 printf("departure");
1444 break;
1445 default:
1446 printf("#%d", ifan->ifan_what);
1447 break;
1449 printf("\n");
1450 break;
1452 default:
1453 printf("pid: %ld, seq %d, errno %d, flags:",
1454 (long)rtm->rtm_pid, rtm->rtm_seq, rtm->rtm_errno);
1455 bprintf(stdout, rtm->rtm_flags, routeflags);
1456 pmsg_common(rtm);
1460 static void
1461 print_getmsg(struct rt_msghdr *rtm, int msglen)
1463 struct sockaddr *dst = NULL, *gate = NULL, *mask = NULL;
1464 struct sockaddr_dl *ifp = NULL;
1465 struct sockaddr *sa;
1466 char *cp;
1467 int i;
1469 printf(" route to: %s\n", routename(&so_dst.sa));
1470 if (rtm->rtm_version != RTM_VERSION) {
1471 warnx("routing message version %d not understood",
1472 rtm->rtm_version);
1473 return;
1475 if (rtm->rtm_msglen > msglen) {
1476 warnx("message length mismatch, in packet %d, returned %d",
1477 rtm->rtm_msglen, msglen);
1479 if (rtm->rtm_errno != 0) {
1480 errno = rtm->rtm_errno;
1481 warn("message indicates error %d", errno);
1482 return;
1484 cp = ((char *)(rtm + 1));
1485 if (rtm->rtm_addrs)
1486 for (i = 1; i != 0; i <<= 1)
1487 if (i & rtm->rtm_addrs) {
1488 sa = (struct sockaddr *)cp;
1489 switch (i) {
1490 case RTA_DST:
1491 dst = sa;
1492 break;
1493 case RTA_GATEWAY:
1494 gate = sa;
1495 break;
1496 case RTA_NETMASK:
1497 mask = sa;
1498 break;
1499 case RTA_IFP:
1500 if (sa->sa_family == AF_LINK &&
1501 ((struct sockaddr_dl *)sa)->sdl_nlen)
1502 ifp = (struct sockaddr_dl *)sa;
1503 break;
1505 RT_ADVANCE(cp, sa);
1507 if (dst != NULL && mask != NULL)
1508 mask->sa_family = dst->sa_family; /* XXX */
1509 if (dst != NULL)
1510 printf("destination: %s\n", routename(dst));
1511 if (mask != NULL) {
1512 int savenflag = nflag;
1514 nflag = 1;
1515 printf(" mask: %s\n", routename(mask));
1516 nflag = savenflag;
1518 if (gate != NULL && rtm->rtm_flags & RTF_GATEWAY)
1519 printf(" gateway: %s\n", routename(gate));
1520 if (ifp != NULL)
1521 printf(" interface: %.*s\n",
1522 ifp->sdl_nlen, ifp->sdl_data);
1523 printf(" flags: ");
1524 bprintf(stdout, rtm->rtm_flags, routeflags);
1526 #define lock(f) ((rtm->rtm_rmx.rmx_locks & __CONCAT(RTV_,f)) ? 'L' : ' ')
1527 #define msec(u) (((u) + 500) / 1000) /* usec to msec */
1529 printf("\n%s\n", "\
1530 recvpipe sendpipe ssthresh rtt,msec rttvar hopcount mtu expire msl,msec iwmax iw");
1531 printf("%8ld%c ", rtm->rtm_rmx.rmx_recvpipe, lock(RPIPE));
1532 printf("%8ld%c ", rtm->rtm_rmx.rmx_sendpipe, lock(SPIPE));
1533 printf("%8ld%c ", rtm->rtm_rmx.rmx_ssthresh, lock(SSTHRESH));
1534 printf("%8ld%c ", msec(rtm->rtm_rmx.rmx_rtt), lock(RTT));
1535 printf("%8ld%c ", msec(rtm->rtm_rmx.rmx_rttvar), lock(RTTVAR));
1536 printf("%8ld%c ", rtm->rtm_rmx.rmx_hopcount, lock(HOPCOUNT));
1537 printf("%8ld%c ", rtm->rtm_rmx.rmx_mtu, lock(MTU));
1538 if (rtm->rtm_rmx.rmx_expire != 0) {
1539 struct timespec sp;
1540 clock_gettime(CLOCK_MONOTONIC, &sp);
1541 rtm->rtm_rmx.rmx_expire -= sp.tv_sec;
1543 printf("%8ld%c ", rtm->rtm_rmx.rmx_expire, lock(EXPIRE));
1544 printf("%8ld ", rtm->rtm_rmx.rmx_msl);
1545 printf("%5ld ", rtm->rtm_rmx.rmx_iwmaxsegs);
1546 printf("%3ld\n", rtm->rtm_rmx.rmx_iwcapsegs);
1547 #undef lock
1548 #undef msec
1549 #define RTA_IGN (RTA_DST|RTA_GATEWAY|RTA_NETMASK|RTA_IFP|RTA_IFA|RTA_BRD)
1550 if (verbose)
1551 pmsg_common(rtm);
1552 else if (rtm->rtm_addrs &~ RTA_IGN) {
1553 printf("sockaddrs: ");
1554 bprintf(stdout, rtm->rtm_addrs, addrnames);
1555 putchar('\n');
1557 #undef RTA_IGN
1560 static void
1561 pmsg_common(struct rt_msghdr *rtm)
1563 printf("\nlocks: ");
1564 bprintf(stdout, rtm->rtm_rmx.rmx_locks, metricnames);
1565 printf(" inits: ");
1566 bprintf(stdout, rtm->rtm_inits, metricnames);
1567 pmsg_addrs(((char *)(rtm + 1)), rtm->rtm_addrs);
1570 static void
1571 pmsg_addrs(char *cp, int addrs)
1573 struct sockaddr *sa;
1574 int i;
1576 if (addrs == 0) {
1577 putchar('\n');
1578 return;
1580 printf("\nsockaddrs: ");
1581 bprintf(stdout, addrs, addrnames);
1582 putchar('\n');
1583 for (i = 1; i != 0; i <<= 1)
1584 if (i & addrs) {
1585 sa = (struct sockaddr *)cp;
1586 printf(" %s", routename(sa));
1587 RT_ADVANCE(cp, sa);
1589 putchar('\n');
1590 fflush(stdout);
1593 static void
1594 bprintf(FILE *fp, int b, u_char *str)
1596 int i;
1597 int gotsome = 0;
1599 if (b == 0)
1600 return;
1601 while ((i = *str++) != 0) {
1602 if (b & (1 << (i-1))) {
1603 if (!gotsome)
1604 i = '<';
1605 else
1606 i = ',';
1607 putc(i, fp);
1608 gotsome = 1;
1609 for (; (i = *str) > 32; str++)
1610 putc(i, fp);
1611 } else
1612 while (*str > 32)
1613 str++;
1615 if (gotsome)
1616 putc('>', fp);
1620 keyword(const char *cp)
1622 struct keytab *kt = keywords;
1624 while (kt->kt_cp != NULL && strcmp(kt->kt_cp, cp) != 0)
1625 kt++;
1626 return(kt->kt_i);
1629 static void
1630 sodump(sup su, const char *which)
1632 switch (su->sa.sa_family) {
1633 case AF_LINK:
1634 printf("%s: link %s; ",
1635 which, link_ntoa(&su->sdl));
1636 break;
1637 case AF_INET:
1638 printf("%s: inet %s; ",
1639 which, inet_ntoa(su->sin.sin_addr));
1640 break;
1642 fflush(stdout);
1645 /* States*/
1646 #define VIRGIN 0
1647 #define GOTONE 1
1648 #define GOTTWO 2
1649 /* Inputs */
1650 #define DIGIT (4*0)
1651 #define END (4*1)
1652 #define DELIM (4*2)
1654 static void
1655 sockaddr(const char *addr, struct sockaddr *sa)
1657 char *cp = (char *)sa;
1658 int size = sa->sa_len;
1659 char *cplim = cp + size;
1660 int byte = 0, state = VIRGIN, new = 0 /* foil gcc */;
1662 memset(cp, 0, size);
1663 cp++;
1664 do {
1665 if ((*addr >= '0') && (*addr <= '9')) {
1666 new = *addr - '0';
1667 } else if ((*addr >= 'a') && (*addr <= 'f')) {
1668 new = *addr - 'a' + 10;
1669 } else if ((*addr >= 'A') && (*addr <= 'F')) {
1670 new = *addr - 'A' + 10;
1671 } else if (*addr == '\0')
1672 state |= END;
1673 else
1674 state |= DELIM;
1675 addr++;
1676 switch (state /* | INPUT */) {
1677 case GOTTWO | DIGIT:
1678 *cp++ = byte; /*FALLTHROUGH*/
1679 case VIRGIN | DIGIT:
1680 state = GOTONE; byte = new; continue;
1681 case GOTONE | DIGIT:
1682 state = GOTTWO; byte = new + (byte << 4); continue;
1683 default: /* | DELIM */
1684 state = VIRGIN; *cp++ = byte; byte = 0; continue;
1685 case GOTONE | END:
1686 case GOTTWO | END:
1687 *cp++ = byte; /* FALLTHROUGH */
1688 case VIRGIN | END:
1689 break;
1691 break;
1692 } while (cp < cplim);
1693 sa->sa_len = cp - (char *)sa;