route: Print protocol-cloned routes if -A is specified
[dragonfly.git] / usr.bin / netstat / route.c
blob61e5dcfd87a9e91a1c8add289c17f45d2c1f34a1
1 /*
2 * Copyright (c) 1983, 1988, 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 * @(#)route.c 8.6 (Berkeley) 4/28/95
30 * $FreeBSD: src/usr.bin/netstat/route.c,v 1.41.2.14 2002/07/17 02:22:22 kbyanc Exp $
33 #include <sys/kinfo.h>
34 #include <sys/param.h>
35 #include <sys/socket.h>
36 #include <sys/time.h>
38 #include <net/ethernet.h>
39 #include <net/if.h>
40 #include <net/if_var.h>
41 #include <net/if_dl.h>
42 #include <net/if_types.h>
43 #include <net/route.h>
45 #include <netinet/in.h>
46 #include <netgraph/socket/ng_socket.h>
48 #include <netproto/mpls/mpls.h>
50 #include <sys/sysctl.h>
52 #include <arpa/inet.h>
53 #include <libutil.h>
54 #include <netdb.h>
55 #include <stdio.h>
56 #include <stdlib.h>
57 #include <string.h>
58 #include <unistd.h>
59 #include <err.h>
60 #include <time.h>
61 #include <kinfo.h>
62 #include "netstat.h"
64 #define kget(p, d) (kread((u_long)(p), (char *)&(d), sizeof (d)))
67 * Definitions for showing gateway flags.
69 struct bits {
70 u_long b_mask;
71 char b_val;
72 } bits[] = {
73 { RTF_UP, 'U' },
74 { RTF_GATEWAY, 'G' },
75 { RTF_HOST, 'H' },
76 { RTF_REJECT, 'R' },
77 { RTF_DYNAMIC, 'D' },
78 { RTF_MODIFIED, 'M' },
79 { RTF_DONE, 'd' }, /* Completed -- for routing messages only */
80 { RTF_CLONING, 'C' },
81 { RTF_XRESOLVE, 'X' },
82 { RTF_LLINFO, 'L' },
83 { RTF_STATIC, 'S' },
84 { RTF_PROTO1, '1' },
85 { RTF_PROTO2, '2' },
86 { RTF_WASCLONED,'W' },
87 { RTF_PRCLONING,'c' },
88 { RTF_PROTO3, '3' },
89 { RTF_BLACKHOLE,'B' },
90 { RTF_BROADCAST,'b' },
91 { RTF_MPLSOPS, 'm' },
92 { 0, 0 }
95 typedef union {
96 long dummy; /* Helps align structure. */
97 struct sockaddr u_sa;
98 u_short u_data[128];
99 } sa_u;
101 static sa_u pt_u;
103 int do_rtent = 0;
104 struct rtentry rtentry;
105 struct radix_node rnode;
106 struct radix_mask rmask;
107 struct radix_node_head *rt_tables[AF_MAX+1];
109 int NewTree = 0;
111 static struct sockaddr *kgetsa (struct sockaddr *);
112 static void size_cols (int ef, struct radix_node *rn);
113 static void size_cols_tree (struct radix_node *rn);
114 static void size_cols_rtentry (struct rtentry *rt);
115 static void p_tree (struct radix_node *);
116 static void p_rtnode (void);
117 static void ntreestuff (void);
118 static void np_rtentry (struct rt_msghdr *);
119 static void p_sockaddr (struct sockaddr *, struct sockaddr *, int, int);
120 static const char *fmt_sockaddr (struct sockaddr *sa, struct sockaddr *mask,
121 int flags);
122 static void p_flags (int, const char *);
123 static const char *fmt_flags(int f);
124 static void p_rtentry (struct rtentry *);
125 static u_long forgemask (u_long);
126 static void domask (char *, u_long, u_long);
127 static const char *labelops(struct rtentry *);
130 * Print routing tables.
132 void
133 routepr(u_long rtree)
135 struct radix_node_head *rnh, head;
136 int i;
138 printf("Routing tables\n");
140 if (Aflag == 0 && NewTree) {
141 ntreestuff();
142 } else {
143 if (rtree == 0) {
144 printf("rt_tables: symbol not in namelist\n");
145 return;
147 if (cpuflag >= 0) {
149 * Severe hack.
151 rtree += cpuflag * (AF_MAX + 1) * sizeof(void *);
153 if (kget(rtree, rt_tables) != 0)
154 return;
155 for (i = 0; i <= AF_MAX; i++) {
156 if ((rnh = rt_tables[i]) == NULL)
157 continue;
158 if (kget(rnh, head) != 0)
159 continue;
160 if (i == AF_UNSPEC) {
161 if (Aflag && af == 0) {
162 printf("Netmasks:\n");
163 p_tree(head.rnh_treetop);
165 } else if (af == AF_UNSPEC || af == i) {
166 size_cols(i, head.rnh_treetop);
167 pr_family(i);
168 do_rtent = 1;
169 pr_rthdr(i);
170 p_tree(head.rnh_treetop);
177 * Print address family header before a section of the routing table.
179 void
180 pr_family(int af1)
182 const char *afname;
184 switch (af1) {
185 case AF_INET:
186 afname = "Internet";
187 break;
188 #ifdef INET6
189 case AF_INET6:
190 afname = "Internet6";
191 break;
192 #endif /*INET6*/
193 case AF_ISO:
194 afname = "ISO";
195 break;
196 case AF_CCITT:
197 afname = "X.25";
198 break;
199 case AF_NETGRAPH:
200 afname = "Netgraph";
201 break;
202 case AF_MPLS:
203 afname = "MPLS";
204 break;
205 default:
206 afname = NULL;
207 break;
209 if (afname)
210 printf("\n%s:\n", afname);
211 else
212 printf("\nProtocol Family %d:\n", af1);
215 /* column widths; each followed by one space */
216 #ifndef INET6
217 #define WID_DST_DEFAULT(af) 18 /* width of destination column */
218 #define WID_GW_DEFAULT(af) 18 /* width of gateway column */
219 #define WID_IF_DEFAULT(af) (Wflag ? 8 : 6) /* width of netif column */
220 #else
221 #define WID_DST_DEFAULT(af) \
222 ((af) == AF_INET6 ? (numeric_addr ? 33: 18) : 18)
223 #define WID_GW_DEFAULT(af) \
224 ((af) == AF_INET6 ? (numeric_addr ? 29 : 18) : 18)
225 #define WID_IF_DEFAULT(af) ((af) == AF_INET6 ? 8 : (Wflag ? 8 :6))
226 #endif /*INET6*/
228 static int wid_dst;
229 static int wid_gw;
230 static int wid_flags;
231 static int wid_refs;
232 static int wid_use;
233 static int wid_mtu;
234 static int wid_if;
235 static int wid_expire;
236 static int wid_mplslops;
237 static int wid_msl;
238 static int wid_iwmax;
239 static int wid_iw;
241 static void
242 size_cols(int ef, struct radix_node *rn)
244 wid_dst = WID_DST_DEFAULT(ef);
245 wid_gw = WID_GW_DEFAULT(ef);
246 wid_flags = 6;
247 wid_refs = 6;
248 wid_use = 8;
249 wid_mtu = 6;
250 wid_if = WID_IF_DEFAULT(ef);
251 wid_expire = 6;
252 wid_mplslops = 7;
253 wid_msl = 7;
254 wid_iwmax = 5;
255 wid_iw = 2;
257 if (Wflag)
258 size_cols_tree(rn);
261 static void
262 size_cols_tree(struct radix_node *rn)
264 again:
265 if (kget(rn, rnode) != 0)
266 return;
267 if (!(rnode.rn_flags & RNF_ACTIVE))
268 return;
269 if (rnode.rn_bit < 0) {
270 if ((rnode.rn_flags & RNF_ROOT) == 0) {
271 if (kget(rn, rtentry) != 0)
272 return;
273 size_cols_rtentry(&rtentry);
275 if ((rn = rnode.rn_dupedkey))
276 goto again;
277 } else {
278 rn = rnode.rn_right;
279 size_cols_tree(rnode.rn_left);
280 size_cols_tree(rn);
284 static void
285 size_cols_rtentry(struct rtentry *rt)
287 static struct ifnet ifnet, *lastif;
288 struct rtentry parent;
289 static char buffer[100];
290 const char *bp;
291 struct sockaddr *sa;
292 sa_u addr, mask;
293 int len;
296 * Don't print protocol-cloned routes unless -a.
298 if (rt->rt_flags & RTF_WASCLONED && !aflag) {
299 if (kget(rt->rt_parent, parent) != 0)
300 return;
301 if (parent.rt_flags & RTF_PRCLONING)
302 return;
305 bzero(&addr, sizeof(addr));
306 if ((sa = kgetsa(rt_key(rt))))
307 bcopy(sa, &addr, sa->sa_len);
308 bzero(&mask, sizeof(mask));
309 if (rt_mask(rt) && (sa = kgetsa(rt_mask(rt))))
310 bcopy(sa, &mask, sa->sa_len);
311 bp = fmt_sockaddr(&addr.u_sa, &mask.u_sa, rt->rt_flags);
312 len = strlen(bp);
313 wid_dst = MAX(len, wid_dst);
315 bp = fmt_sockaddr(kgetsa(rt->rt_gateway), NULL, RTF_HOST);
316 len = strlen(bp);
317 wid_gw = MAX(len, wid_gw);
319 bp = fmt_flags(rt->rt_flags);
320 len = strlen(bp);
321 wid_flags = MAX(len, wid_flags);
323 if (addr.u_sa.sa_family == AF_INET || Wflag) {
324 len = snprintf(buffer, sizeof(buffer), "%ld", rt->rt_refcnt);
325 wid_refs = MAX(len, wid_refs);
326 len = snprintf(buffer, sizeof(buffer), "%lu", rt->rt_use);
327 wid_use = MAX(len, wid_use);
328 if (Wflag && rt->rt_rmx.rmx_mtu != 0) {
329 len = snprintf(buffer, sizeof(buffer),
330 "%lu", rt->rt_rmx.rmx_mtu);
331 wid_mtu = MAX(len, wid_mtu);
334 if (rt->rt_ifp) {
335 if (rt->rt_ifp != lastif) {
336 if (kget(rt->rt_ifp, ifnet) == 0)
337 len = strlen(ifnet.if_xname);
338 else
339 len = strlen("---");
340 lastif = rt->rt_ifp;
341 wid_if = MAX(len, wid_if);
343 if (rt->rt_rmx.rmx_expire) {
344 struct timespec sp;
345 int expire_time;
347 clock_gettime(CLOCK_MONOTONIC, &sp);
349 expire_time = (int)(rt->rt_rmx.rmx_expire - sp.tv_sec);
351 if (expire_time > 0) {
352 snprintf(buffer, sizeof(buffer), "%d",
353 (int)expire_time);
354 wid_expire = MAX(len, wid_expire);
358 if (Wflag) {
359 if (rt->rt_shim[0] != NULL) {
360 len = strlen(labelops(rt));
361 wid_mplslops = MAX(len, wid_mplslops);
364 if (rt->rt_rmx.rmx_msl) {
365 len = snprintf(buffer, sizeof(buffer),
366 "%lu", rt->rt_rmx.rmx_msl);
367 wid_msl = MAX(len, wid_msl);
369 if (rt->rt_rmx.rmx_iwmaxsegs) {
370 len = snprintf(buffer, sizeof(buffer),
371 "%lu", rt->rt_rmx.rmx_iwmaxsegs);
372 wid_iwmax = MAX(len, wid_iwmax);
374 if (rt->rt_rmx.rmx_iwcapsegs) {
375 len = snprintf(buffer, sizeof(buffer),
376 "%lu", rt->rt_rmx.rmx_iwcapsegs);
377 wid_iw = MAX(len, wid_iw);
384 * Print header for routing table columns.
386 void
387 pr_rthdr(int af1)
390 if (Aflag)
391 printf("%-8.8s ","Address");
392 if (af1 == AF_INET || Wflag) {
393 if (Wflag) {
394 printf("%-*.*s %-*.*s %-*.*s %*.*s %*.*s %*.*s %*.*s "
395 "%*s %-*s%*s %*s %*s\n",
396 wid_dst, wid_dst, "Destination",
397 wid_gw, wid_gw, "Gateway",
398 wid_flags, wid_flags, "Flags",
399 wid_refs, wid_refs, "Refs",
400 wid_use, wid_use, "Use",
401 wid_mtu, wid_mtu, "Mtu",
402 wid_if, wid_if, "Netif",
403 wid_expire, "Expire",
404 wid_mplslops, "Labelops",
405 wid_msl, "Msl",
406 wid_iwmax, "IWmax",
407 wid_iw, "IW");
408 } else {
409 printf("%-*.*s %-*.*s %-*.*s %*.*s %*.*s %*.*s %*s\n",
410 wid_dst, wid_dst, "Destination",
411 wid_gw, wid_gw, "Gateway",
412 wid_flags, wid_flags, "Flags",
413 wid_refs, wid_refs, "Refs",
414 wid_use, wid_use, "Use",
415 wid_if, wid_if, "Netif",
416 wid_expire, "Expire");
418 } else {
419 printf("%-*.*s %-*.*s %-*.*s %*.*s %*s\n",
420 wid_dst, wid_dst, "Destination",
421 wid_gw, wid_gw, "Gateway",
422 wid_flags, wid_flags, "Flags",
423 wid_if, wid_if, "Netif",
424 wid_expire, "Expire");
428 static struct sockaddr *
429 kgetsa(struct sockaddr *dst)
432 if (kget(dst, pt_u.u_sa) != 0)
433 return (NULL);
434 if (pt_u.u_sa.sa_len > sizeof (pt_u.u_sa))
435 kread((u_long)dst, (char *)pt_u.u_data, pt_u.u_sa.sa_len);
436 return (&pt_u.u_sa);
439 static void
440 p_tree(struct radix_node *rn)
443 again:
444 if (kget(rn, rnode) != 0)
445 return;
446 if (!(rnode.rn_flags & RNF_ACTIVE))
447 return;
448 if (rnode.rn_bit < 0) {
449 if (Aflag)
450 printf("%-8.8lx ", (u_long)rn);
451 if (rnode.rn_flags & RNF_ROOT) {
452 if (Aflag)
453 printf("(root node)%s",
454 rnode.rn_dupedkey ? " =>\n" : "\n");
455 } else if (do_rtent) {
456 if (kget(rn, rtentry) == 0) {
457 p_rtentry(&rtentry);
458 if (Aflag)
459 p_rtnode();
461 } else {
462 p_sockaddr(kgetsa((struct sockaddr *)rnode.rn_key),
463 NULL, 0, 44);
464 putchar('\n');
466 if ((rn = rnode.rn_dupedkey))
467 goto again;
468 } else {
469 if (Aflag && do_rtent) {
470 printf("%-8.8lx ", (u_long)rn);
471 p_rtnode();
473 rn = rnode.rn_right;
474 p_tree(rnode.rn_left);
475 p_tree(rn);
479 char nbuf[20];
481 static void
482 p_rtnode(void)
484 struct radix_mask *rm = rnode.rn_mklist;
486 if (rnode.rn_bit < 0) {
487 if (rnode.rn_mask) {
488 printf("\t mask ");
489 p_sockaddr(kgetsa((struct sockaddr *)rnode.rn_mask),
490 NULL, 0, -1);
491 } else if (rm == NULL)
492 return;
493 } else {
494 sprintf(nbuf, "(%d)", rnode.rn_bit);
495 printf("%6.6s %8.8lx : %8.8lx", nbuf, (u_long)rnode.rn_left, (u_long)rnode.rn_right);
497 while (rm) {
498 if (kget(rm, rmask) != 0)
499 break;
500 sprintf(nbuf, " %d refs, ", rmask.rm_refs);
501 printf(" mk = %8.8lx {(%d),%s",
502 (u_long)rm, -1 - rmask.rm_bit, rmask.rm_refs ? nbuf : " ");
503 if (rmask.rm_flags & RNF_NORMAL) {
504 struct radix_node rnode_aux;
505 printf(" <normal>, ");
506 if (kget(rmask.rm_leaf, rnode_aux) == 0)
507 p_sockaddr(kgetsa((struct sockaddr *)rnode_aux.rn_mask),
508 NULL, 0, -1);
509 else
510 p_sockaddr(NULL, NULL, 0, -1);
511 } else
512 p_sockaddr(kgetsa((struct sockaddr *)rmask.rm_mask),
513 NULL, 0, -1);
514 putchar('}');
515 if ((rm = rmask.rm_next))
516 printf(" ->");
518 putchar('\n');
521 static void
522 ntreestuff(void)
524 size_t needed;
525 int mib[7];
526 int miblen;
527 char *buf, *next, *lim;
528 struct rt_msghdr *rtm;
530 mib[0] = CTL_NET;
531 mib[1] = PF_ROUTE;
532 mib[2] = 0;
533 mib[3] = 0;
534 mib[4] = NET_RT_DUMP;
535 mib[5] = 0;
536 if (cpuflag >= 0) {
537 mib[6] = cpuflag;
538 miblen = 7;
539 } else {
540 miblen = 6;
542 if (sysctl(mib, miblen, NULL, &needed, NULL, 0) < 0) {
543 err(1, "sysctl: net.route.0.0.dump estimate");
546 if ((buf = malloc(needed)) == NULL) {
547 err(2, "malloc(%lu)", (unsigned long)needed);
549 if (sysctl(mib, miblen, buf, &needed, NULL, 0) < 0) {
550 err(1, "sysctl: net.route.0.0.dump");
552 lim = buf + needed;
553 for (next = buf; next < lim; next += rtm->rtm_msglen) {
554 rtm = (struct rt_msghdr *)next;
555 np_rtentry(rtm);
559 static void
560 np_rtentry(struct rt_msghdr *rtm)
562 struct sockaddr *sa = (struct sockaddr *)(rtm + 1);
563 #ifdef notdef
564 static int masks_done, banner_printed;
565 #endif
566 static int old_af;
567 int af1 = 0, interesting = RTF_UP | RTF_GATEWAY | RTF_HOST;
569 #ifdef notdef
570 /* for the moment, netmasks are skipped over */
571 if (!banner_printed) {
572 printf("Netmasks:\n");
573 banner_printed = 1;
575 if (masks_done == 0) {
576 if (rtm->rtm_addrs != RTA_DST ) {
577 masks_done = 1;
578 af1 = sa->sa_family;
580 } else
581 #endif
582 af1 = sa->sa_family;
583 if (af1 != old_af) {
584 pr_family(af1);
585 old_af = af1;
587 if (rtm->rtm_addrs == RTA_DST)
588 p_sockaddr(sa, NULL, 0, 36);
589 else {
590 p_sockaddr(sa, NULL, rtm->rtm_flags, 16);
591 sa = (struct sockaddr *)(RT_ROUNDUP(sa->sa_len) + (char *)sa);
592 p_sockaddr(sa, NULL, 0, 18);
594 p_flags(rtm->rtm_flags & interesting, "%-6.6s ");
595 putchar('\n');
598 static void
599 p_sockaddr(struct sockaddr *sa, struct sockaddr *mask, int flags, int width)
601 const char *cp;
603 cp = fmt_sockaddr(sa, mask, flags);
605 if (width < 0 )
606 printf("%s ", cp);
607 else {
608 if (numeric_addr)
609 printf("%-*s ", width, cp);
610 else
611 printf("%-*.*s ", width, width, cp);
615 static const char *
616 fmt_sockaddr(struct sockaddr *sa, struct sockaddr *mask, int flags)
618 static char workbuf[128];
619 const char *cp = workbuf;
621 if (sa == NULL)
622 return ("null");
624 switch(sa->sa_family) {
625 case AF_INET:
627 struct sockaddr_in *sin = (struct sockaddr_in *)sa;
629 if ((sin->sin_addr.s_addr == INADDR_ANY) &&
630 mask &&
631 ntohl(((struct sockaddr_in *)mask)->sin_addr.s_addr)
632 ==0L)
633 cp = "default" ;
634 else if (flags & RTF_HOST)
635 cp = routename(sin->sin_addr.s_addr);
636 else if (mask)
637 cp = netname(sin->sin_addr.s_addr,
638 ntohl(((struct sockaddr_in *)mask)
639 ->sin_addr.s_addr));
640 else
641 cp = netname(sin->sin_addr.s_addr, 0L);
642 break;
645 #ifdef INET6
646 case AF_INET6:
648 struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)sa;
649 struct in6_addr *in6 = &sa6->sin6_addr;
652 * XXX: This is a special workaround for KAME kernels.
653 * sin6_scope_id field of SA should be set in the future.
655 if (IN6_IS_ADDR_LINKLOCAL(in6) ||
656 IN6_IS_ADDR_MC_LINKLOCAL(in6)) {
657 /* XXX: override is ok? */
658 sa6->sin6_scope_id = (u_int32_t)ntohs(*(u_short *)&in6->s6_addr[2]);
659 *(u_short *)&in6->s6_addr[2] = 0;
662 if (flags & RTF_HOST)
663 cp = routename6(sa6);
664 else if (mask)
665 cp = netname6(sa6,
666 &((struct sockaddr_in6 *)mask)->sin6_addr);
667 else {
668 cp = netname6(sa6, NULL);
670 break;
672 #endif /*INET6*/
674 case AF_NETGRAPH:
676 printf("%s", ((struct sockaddr_ng *)sa)->sg_data);
677 break;
679 case AF_LINK:
681 struct sockaddr_dl *sdl = (struct sockaddr_dl *)sa;
683 if (sdl->sdl_nlen == 0 && sdl->sdl_alen == 0 &&
684 sdl->sdl_slen == 0)
685 (void) sprintf(workbuf, "link#%d", sdl->sdl_index);
686 else
687 switch (sdl->sdl_type) {
689 case IFT_ETHER:
690 case IFT_L2VLAN:
691 case IFT_CARP:
692 if (sdl->sdl_alen == ETHER_ADDR_LEN) {
693 cp = ether_ntoa((struct ether_addr *)
694 (sdl->sdl_data + sdl->sdl_nlen));
695 break;
697 /* FALLTHROUGH */
698 default:
699 cp = link_ntoa(sdl);
700 break;
702 break;
705 case AF_MPLS:
707 struct sockaddr_mpls *smpls = (struct sockaddr_mpls *)sa;
709 (void) sprintf(workbuf, "%d", ntohl(smpls->smpls_label));
710 break;
713 default:
715 u_char *s = (u_char *)sa->sa_data, *slim;
716 char *cq, *cqlim;
718 cq = workbuf;
719 slim = sa->sa_len + (u_char *) sa;
720 cqlim = cq + sizeof(workbuf) - 6;
721 cq += sprintf(cq, "(%d)", sa->sa_family);
722 while (s < slim && cq < cqlim) {
723 cq += sprintf(cq, " %02x", *s++);
724 if (s < slim)
725 cq += sprintf(cq, "%02x", *s++);
727 cp = workbuf;
731 return (cp);
734 static void
735 p_flags(int f, const char *format)
737 printf(format, fmt_flags(f));
740 static const char *
741 fmt_flags(int f)
743 static char name[33];
744 char *flags;
745 struct bits *p = bits;
747 for (flags = name; p->b_mask; p++)
748 if (p->b_mask & f)
749 *flags++ = p->b_val;
750 *flags = '\0';
751 return (name);
754 static void
755 p_rtentry(struct rtentry *rt)
757 static struct ifnet ifnet, *lastif;
758 struct rtentry parent;
759 static char buffer[128];
760 static char prettyname[128];
761 struct sockaddr *sa;
762 sa_u addr, mask;
765 * Don't print protocol-cloned routes unless -a or -A.
767 if (rt->rt_flags & RTF_WASCLONED && !(aflag || Aflag)) {
768 if (kget(rt->rt_parent, parent) != 0)
769 return;
770 if (parent.rt_flags & RTF_PRCLONING)
771 return;
774 bzero(&addr, sizeof(addr));
775 if ((sa = kgetsa(rt_key(rt))))
776 bcopy(sa, &addr, sa->sa_len);
777 bzero(&mask, sizeof(mask));
778 if (rt_mask(rt) && (sa = kgetsa(rt_mask(rt))))
779 bcopy(sa, &mask, sa->sa_len);
780 p_sockaddr(&addr.u_sa, &mask.u_sa, rt->rt_flags, wid_dst);
781 p_sockaddr(kgetsa(rt->rt_gateway), NULL, RTF_HOST, wid_gw);
782 snprintf(buffer, sizeof(buffer), "%%-%d.%ds ", wid_flags, wid_flags);
783 p_flags(rt->rt_flags, buffer);
784 if (addr.u_sa.sa_family == AF_INET || Wflag) {
785 printf("%*ld %*lu ", wid_refs, rt->rt_refcnt,
786 wid_use, rt->rt_use);
787 if (Wflag) {
788 if (rt->rt_rmx.rmx_mtu != 0)
789 printf("%*lu ", wid_mtu, rt->rt_rmx.rmx_mtu);
790 else
791 printf("%*s ", wid_mtu, "");
794 if (rt->rt_ifp) {
795 if (rt->rt_ifp != lastif) {
796 if (kget(rt->rt_ifp, ifnet) == 0)
797 strlcpy(prettyname, ifnet.if_xname,
798 sizeof(prettyname));
799 else
800 strlcpy(prettyname, "---", sizeof(prettyname));
801 lastif = rt->rt_ifp;
803 printf("%*.*s", wid_if, wid_if, prettyname);
804 if (rt->rt_rmx.rmx_expire) {
805 struct timespec sp;
806 int expire_time;
808 clock_gettime(CLOCK_MONOTONIC, &sp);
810 expire_time = (int)(rt->rt_rmx.rmx_expire - sp.tv_sec);
811 if (expire_time > 0)
812 printf(" %*d", wid_expire, (int)expire_time);
813 else
814 printf("%*s ", wid_expire, "");
815 } else {
816 printf("%*s ", wid_expire, "");
818 if (rt->rt_nodes[0].rn_dupedkey)
819 printf(" =>");
821 if (Wflag) {
822 if (rt->rt_shim[0] != NULL)
823 printf(" %-*s", wid_mplslops, labelops(rt));
824 else
825 printf(" %-*s", wid_mplslops, "");
826 if (rt->rt_rmx.rmx_msl != 0)
827 printf(" %*lu", wid_msl, rt->rt_rmx.rmx_msl);
828 else
829 printf("%*s ", wid_msl, "");
830 if (rt->rt_rmx.rmx_iwmaxsegs != 0)
831 printf(" %*lu", wid_iwmax, rt->rt_rmx.rmx_iwmaxsegs);
832 else
833 printf("%*s ", wid_iwmax, "");
834 if (rt->rt_rmx.rmx_iwcapsegs != 0)
835 printf(" %*lu", wid_iw, rt->rt_rmx.rmx_iwcapsegs);
836 else
837 printf("%*s ", wid_iw, "");
839 putchar('\n');
842 char *
843 routename(u_long in)
845 char *cp;
846 static char line[MAXHOSTNAMELEN];
847 struct hostent *hp;
849 cp = NULL;
850 if (!numeric_addr) {
851 hp = gethostbyaddr(&in, sizeof (struct in_addr), AF_INET);
852 if (hp) {
853 cp = hp->h_name;
854 trimdomain(cp, strlen(cp));
857 if (cp) {
858 strncpy(line, cp, sizeof(line) - 1);
859 line[sizeof(line) - 1] = '\0';
860 } else {
861 #define C(x) ((x) & 0xff)
862 in = ntohl(in);
863 sprintf(line, "%lu.%lu.%lu.%lu",
864 C(in >> 24), C(in >> 16), C(in >> 8), C(in));
866 return (line);
869 static u_long
870 forgemask(u_long a)
872 u_long m;
874 if (IN_CLASSA(a))
875 m = IN_CLASSA_NET;
876 else if (IN_CLASSB(a))
877 m = IN_CLASSB_NET;
878 else
879 m = IN_CLASSC_NET;
880 return (m);
883 static void
884 domask(char *dst, u_long addr, u_long mask)
886 int b, i;
888 if (!mask || (forgemask(addr) == mask)) {
889 *dst = '\0';
890 return;
892 i = 0;
893 for (b = 0; b < 32; b++)
894 if (mask & (1 << b)) {
895 int bb;
897 i = b;
898 for (bb = b+1; bb < 32; bb++)
899 if (!(mask & (1 << bb))) {
900 i = -1; /* noncontig */
901 break;
903 break;
905 if (i == -1)
906 sprintf(dst, "&0x%lx", mask);
907 else
908 sprintf(dst, "/%d", 32-i);
912 * Return the name of the network whose address is given.
913 * The address is assumed to be that of a net or subnet, not a host.
915 char *
916 netname(u_long in, u_long mask)
918 char *cp = NULL;
919 static char line[MAXHOSTNAMELEN];
920 struct netent *np = NULL;
921 u_long dmask;
922 u_long i;
924 #define NSHIFT(m) ( \
925 (m) == IN_CLASSA_NET ? IN_CLASSA_NSHIFT : \
926 (m) == IN_CLASSB_NET ? IN_CLASSB_NSHIFT : \
927 (m) == IN_CLASSC_NET ? IN_CLASSC_NSHIFT : \
930 i = ntohl(in);
931 dmask = forgemask(i);
932 if (!numeric_addr && i) {
933 np = getnetbyaddr(i >> NSHIFT(mask), AF_INET);
934 if (np == NULL && mask == 0)
935 np = getnetbyaddr(i >> NSHIFT(dmask), AF_INET);
936 if (np != NULL) {
937 cp = np->n_name;
938 trimdomain(cp, strlen(cp));
941 #undef NSHIFT
942 if (cp != NULL) {
943 strncpy(line, cp, sizeof(line) - 1);
944 line[sizeof(line) - 1] = '\0';
945 } else {
946 if (mask <= IN_CLASSA_NET &&
947 (i & IN_CLASSA_HOST) == 0) {
948 sprintf(line, "%lu", C(i >> 24));
949 } else if (mask <= IN_CLASSB_NET &&
950 (i & IN_CLASSB_HOST) == 0) {
951 sprintf(line, "%lu.%lu",
952 C(i >> 24), C(i >> 16));
953 } else if (mask <= IN_CLASSC_NET &&
954 (i & IN_CLASSC_HOST) == 0) {
955 sprintf(line, "%lu.%lu.%lu",
956 C(i >> 24), C(i >> 16), C(i >> 8));
957 } else {
958 sprintf(line, "%lu.%lu.%lu.%lu",
959 C(i >> 24), C(i >> 16), C(i >> 8), C(i));
962 domask(line + strlen(line), i, mask);
963 return (line);
966 #ifdef INET6
967 const char *
968 netname6(struct sockaddr_in6 *sa6, struct in6_addr *mask)
970 static char line[MAXHOSTNAMELEN];
971 u_char *p = (u_char *)mask;
972 u_char *lim;
973 int masklen, illegal = 0, flag = NI_WITHSCOPEID;
975 if (mask) {
976 for (masklen = 0, lim = p + 16; p < lim; p++) {
977 switch (*p) {
978 case 0xff:
979 masklen += 8;
980 break;
981 case 0xfe:
982 masklen += 7;
983 break;
984 case 0xfc:
985 masklen += 6;
986 break;
987 case 0xf8:
988 masklen += 5;
989 break;
990 case 0xf0:
991 masklen += 4;
992 break;
993 case 0xe0:
994 masklen += 3;
995 break;
996 case 0xc0:
997 masklen += 2;
998 break;
999 case 0x80:
1000 masklen += 1;
1001 break;
1002 case 0x00:
1003 break;
1004 default:
1005 illegal ++;
1006 break;
1009 if (illegal)
1010 fprintf(stderr, "illegal prefixlen\n");
1012 else
1013 masklen = 128;
1015 if (masklen == 0 && IN6_IS_ADDR_UNSPECIFIED(&sa6->sin6_addr))
1016 return("default");
1018 if (numeric_addr)
1019 flag |= NI_NUMERICHOST;
1020 getnameinfo((struct sockaddr *)sa6, sa6->sin6_len, line, sizeof(line),
1021 NULL, 0, flag);
1023 if (numeric_addr)
1024 sprintf(&line[strlen(line)], "/%d", masklen);
1026 return line;
1029 char *
1030 routename6(struct sockaddr_in6 *sa6)
1032 static char line[MAXHOSTNAMELEN];
1033 int flag = NI_WITHSCOPEID;
1034 /* use local variable for safety */
1035 struct sockaddr_in6 sa6_local;
1037 sa6_local.sin6_family = AF_INET6;
1038 sa6_local.sin6_len = sizeof(sa6_local);
1039 sa6_local.sin6_addr = sa6->sin6_addr;
1040 sa6_local.sin6_scope_id = sa6->sin6_scope_id;
1042 if (numeric_addr)
1043 flag |= NI_NUMERICHOST;
1045 getnameinfo((struct sockaddr *)&sa6_local, sa6_local.sin6_len,
1046 line, sizeof(line), NULL, 0, flag);
1048 return line;
1050 #endif /*INET6*/
1053 * Print routing statistics
1055 void
1056 rt_stats(void)
1058 struct rtstatistics rts;
1059 int error = 0;
1061 error = kinfo_get_net_rtstatistics(&rts);
1062 if (error) {
1063 printf("routing: could not retrieve statistics\n");
1064 return;
1066 printf("routing:\n");
1068 #define p(f, m) if (rts.f || sflag <= 1) \
1069 printf(m, rts.f, plural(rts.f))
1071 p(rts_badredirect, "\t%lu bad routing redirect%s\n");
1072 p(rts_dynamic, "\t%lu dynamically created route%s\n");
1073 p(rts_newgateway, "\t%lu new gateway%s due to redirects\n");
1074 p(rts_unreach, "\t%lu destination%s found unreachable\n");
1075 p(rts_wildcard, "\t%lu use%s of a wildcard route\n");
1076 #undef p
1079 static const char *
1080 labelops(struct rtentry *rt)
1082 const char *lops[] = { "push", "pop", "swap", "pop all" };
1083 static char buffer[100];
1084 char *cp = buffer;
1085 struct sockaddr_mpls *smpls;
1086 int i;
1088 for (i=0; i<MPLS_MAXLOPS; ++i) {
1090 if (rt->rt_shim[i] == NULL)
1091 break;
1092 if (i>0) {
1093 cp += snprintf(cp,
1094 sizeof(buffer) - (cp - buffer),
1095 ", ");
1097 smpls = (struct sockaddr_mpls *)kgetsa(rt->rt_shim[i]);
1098 if (smpls->smpls_op != MPLSLOP_POP &&
1099 smpls->smpls_op != MPLSLOP_POPALL){
1100 cp += snprintf(cp,
1101 sizeof(buffer) - (cp - buffer),
1102 "%s %d",
1103 lops[smpls->smpls_op - 1],
1104 ntohl(smpls->smpls_label));
1105 } else {
1106 cp += snprintf(cp,
1107 sizeof(buffer) - (cp - buffer),
1108 "%s",
1109 lops[smpls->smpls_op - 1]);
1113 return (buffer);