Bring in an errno.9 manual page (based on NetBSD's).
[dragonfly.git] / usr.bin / netstat / route.c
blob128a8f1c9932bc5dff9d41d30132024b0bb178b5
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_CCITT:
194 afname = "X.25";
195 break;
196 case AF_NETGRAPH:
197 afname = "Netgraph";
198 break;
199 case AF_MPLS:
200 afname = "MPLS";
201 break;
202 default:
203 afname = NULL;
204 break;
206 if (afname)
207 printf("\n%s:\n", afname);
208 else
209 printf("\nProtocol Family %d:\n", af1);
212 /* column widths; each followed by one space */
213 #ifndef INET6
214 #define WID_DST_DEFAULT(af) 18 /* width of destination column */
215 #define WID_GW_DEFAULT(af) 18 /* width of gateway column */
216 #define WID_IF_DEFAULT(af) (Wflag ? 8 : 6) /* width of netif column */
217 #else
218 #define WID_DST_DEFAULT(af) \
219 ((af) == AF_INET6 ? (numeric_addr ? 33: 18) : 18)
220 #define WID_GW_DEFAULT(af) \
221 ((af) == AF_INET6 ? (numeric_addr ? 29 : 18) : 18)
222 #define WID_IF_DEFAULT(af) ((af) == AF_INET6 ? 8 : (Wflag ? 8 :6))
223 #endif /*INET6*/
225 static int wid_dst;
226 static int wid_gw;
227 static int wid_flags;
228 static int wid_refs;
229 static int wid_use;
230 static int wid_mtu;
231 static int wid_if;
232 static int wid_expire;
233 static int wid_mplslops;
234 static int wid_msl;
235 static int wid_iwmax;
236 static int wid_iw;
238 static void
239 size_cols(int ef, struct radix_node *rn)
241 wid_dst = WID_DST_DEFAULT(ef);
242 wid_gw = WID_GW_DEFAULT(ef);
243 wid_flags = 6;
244 wid_refs = 6;
245 wid_use = 8;
246 wid_mtu = 6;
247 wid_if = WID_IF_DEFAULT(ef);
248 wid_expire = 6;
249 wid_mplslops = 7;
250 wid_msl = 7;
251 wid_iwmax = 5;
252 wid_iw = 2;
254 if (Wflag)
255 size_cols_tree(rn);
258 static void
259 size_cols_tree(struct radix_node *rn)
261 again:
262 if (kget(rn, rnode) != 0)
263 return;
264 if (!(rnode.rn_flags & RNF_ACTIVE))
265 return;
266 if (rnode.rn_bit < 0) {
267 if ((rnode.rn_flags & RNF_ROOT) == 0) {
268 if (kget(rn, rtentry) != 0)
269 return;
270 size_cols_rtentry(&rtentry);
272 if ((rn = rnode.rn_dupedkey))
273 goto again;
274 } else {
275 rn = rnode.rn_right;
276 size_cols_tree(rnode.rn_left);
277 size_cols_tree(rn);
281 static void
282 size_cols_rtentry(struct rtentry *rt)
284 static struct ifnet ifnet, *lastif;
285 struct rtentry parent;
286 static char buffer[100];
287 const char *bp;
288 struct sockaddr *sa;
289 sa_u addr, mask;
290 int len;
293 * Don't print protocol-cloned routes unless -a.
295 if (rt->rt_flags & RTF_WASCLONED && !aflag) {
296 if (kget(rt->rt_parent, parent) != 0)
297 return;
298 if (parent.rt_flags & RTF_PRCLONING)
299 return;
302 bzero(&addr, sizeof(addr));
303 if ((sa = kgetsa(rt_key(rt))))
304 bcopy(sa, &addr, sa->sa_len);
305 bzero(&mask, sizeof(mask));
306 if (rt_mask(rt) && (sa = kgetsa(rt_mask(rt))))
307 bcopy(sa, &mask, sa->sa_len);
308 bp = fmt_sockaddr(&addr.u_sa, &mask.u_sa, rt->rt_flags);
309 len = strlen(bp);
310 wid_dst = MAX(len, wid_dst);
312 bp = fmt_sockaddr(kgetsa(rt->rt_gateway), NULL, RTF_HOST);
313 len = strlen(bp);
314 wid_gw = MAX(len, wid_gw);
316 bp = fmt_flags(rt->rt_flags);
317 len = strlen(bp);
318 wid_flags = MAX(len, wid_flags);
320 if (addr.u_sa.sa_family == AF_INET || Wflag) {
321 len = snprintf(buffer, sizeof(buffer), "%ld", rt->rt_refcnt);
322 wid_refs = MAX(len, wid_refs);
323 len = snprintf(buffer, sizeof(buffer), "%lu", rt->rt_use);
324 wid_use = MAX(len, wid_use);
325 if (Wflag && rt->rt_rmx.rmx_mtu != 0) {
326 len = snprintf(buffer, sizeof(buffer),
327 "%lu", rt->rt_rmx.rmx_mtu);
328 wid_mtu = MAX(len, wid_mtu);
331 if (rt->rt_ifp) {
332 if (rt->rt_ifp != lastif) {
333 if (kget(rt->rt_ifp, ifnet) == 0)
334 len = strlen(ifnet.if_xname);
335 else
336 len = strlen("---");
337 lastif = rt->rt_ifp;
338 wid_if = MAX(len, wid_if);
340 if (rt->rt_rmx.rmx_expire) {
341 struct timespec sp;
342 int expire_time;
344 clock_gettime(CLOCK_MONOTONIC, &sp);
346 expire_time = (int)(rt->rt_rmx.rmx_expire - sp.tv_sec);
348 if (expire_time > 0) {
349 snprintf(buffer, sizeof(buffer), "%d",
350 (int)expire_time);
351 wid_expire = MAX(len, wid_expire);
355 if (Wflag) {
356 if (rt->rt_shim[0] != NULL) {
357 len = strlen(labelops(rt));
358 wid_mplslops = MAX(len, wid_mplslops);
361 if (rt->rt_rmx.rmx_msl) {
362 len = snprintf(buffer, sizeof(buffer),
363 "%lu", rt->rt_rmx.rmx_msl);
364 wid_msl = MAX(len, wid_msl);
366 if (rt->rt_rmx.rmx_iwmaxsegs) {
367 len = snprintf(buffer, sizeof(buffer),
368 "%lu", rt->rt_rmx.rmx_iwmaxsegs);
369 wid_iwmax = MAX(len, wid_iwmax);
371 if (rt->rt_rmx.rmx_iwcapsegs) {
372 len = snprintf(buffer, sizeof(buffer),
373 "%lu", rt->rt_rmx.rmx_iwcapsegs);
374 wid_iw = MAX(len, wid_iw);
381 * Print header for routing table columns.
383 void
384 pr_rthdr(int af1)
387 if (Aflag)
388 printf("%-8.8s ","Address");
389 if (af1 == AF_INET || Wflag) {
390 if (Wflag) {
391 printf("%-*.*s %-*.*s %-*.*s %*.*s %*.*s %*.*s %*.*s "
392 "%*s %-*s%*s %*s %*s\n",
393 wid_dst, wid_dst, "Destination",
394 wid_gw, wid_gw, "Gateway",
395 wid_flags, wid_flags, "Flags",
396 wid_refs, wid_refs, "Refs",
397 wid_use, wid_use, "Use",
398 wid_mtu, wid_mtu, "Mtu",
399 wid_if, wid_if, "Netif",
400 wid_expire, "Expire",
401 wid_mplslops, "Labelops",
402 wid_msl, "Msl",
403 wid_iwmax, "IWmax",
404 wid_iw, "IW");
405 } else {
406 printf("%-*.*s %-*.*s %-*.*s %*.*s %*.*s %*.*s %*s\n",
407 wid_dst, wid_dst, "Destination",
408 wid_gw, wid_gw, "Gateway",
409 wid_flags, wid_flags, "Flags",
410 wid_refs, wid_refs, "Refs",
411 wid_use, wid_use, "Use",
412 wid_if, wid_if, "Netif",
413 wid_expire, "Expire");
415 } else {
416 printf("%-*.*s %-*.*s %-*.*s %*.*s %*s\n",
417 wid_dst, wid_dst, "Destination",
418 wid_gw, wid_gw, "Gateway",
419 wid_flags, wid_flags, "Flags",
420 wid_if, wid_if, "Netif",
421 wid_expire, "Expire");
425 static struct sockaddr *
426 kgetsa(struct sockaddr *dst)
429 if (kget(dst, pt_u.u_sa) != 0)
430 return (NULL);
431 if (pt_u.u_sa.sa_len > sizeof (pt_u.u_sa))
432 kread((u_long)dst, (char *)pt_u.u_data, pt_u.u_sa.sa_len);
433 return (&pt_u.u_sa);
436 static void
437 p_tree(struct radix_node *rn)
440 again:
441 if (kget(rn, rnode) != 0)
442 return;
443 if (!(rnode.rn_flags & RNF_ACTIVE))
444 return;
445 if (rnode.rn_bit < 0) {
446 if (Aflag)
447 printf("%-8.8lx ", (u_long)rn);
448 if (rnode.rn_flags & RNF_ROOT) {
449 if (Aflag)
450 printf("(root node)%s",
451 rnode.rn_dupedkey ? " =>\n" : "\n");
452 } else if (do_rtent) {
453 if (kget(rn, rtentry) == 0) {
454 p_rtentry(&rtentry);
455 if (Aflag)
456 p_rtnode();
458 } else {
459 p_sockaddr(kgetsa((struct sockaddr *)rnode.rn_key),
460 NULL, 0, 44);
461 putchar('\n');
463 if ((rn = rnode.rn_dupedkey))
464 goto again;
465 } else {
466 if (Aflag && do_rtent) {
467 printf("%-8.8lx ", (u_long)rn);
468 p_rtnode();
470 rn = rnode.rn_right;
471 p_tree(rnode.rn_left);
472 p_tree(rn);
476 char nbuf[20];
478 static void
479 p_rtnode(void)
481 struct radix_mask *rm = rnode.rn_mklist;
483 if (rnode.rn_bit < 0) {
484 if (rnode.rn_mask) {
485 printf("\t mask ");
486 p_sockaddr(kgetsa((struct sockaddr *)rnode.rn_mask),
487 NULL, 0, -1);
488 } else if (rm == NULL)
489 return;
490 } else {
491 sprintf(nbuf, "(%d)", rnode.rn_bit);
492 printf("%6.6s %8.8lx : %8.8lx", nbuf, (u_long)rnode.rn_left, (u_long)rnode.rn_right);
494 while (rm) {
495 if (kget(rm, rmask) != 0)
496 break;
497 sprintf(nbuf, " %d refs, ", rmask.rm_refs);
498 printf(" mk = %8.8lx {(%d),%s",
499 (u_long)rm, -1 - rmask.rm_bit, rmask.rm_refs ? nbuf : " ");
500 if (rmask.rm_flags & RNF_NORMAL) {
501 struct radix_node rnode_aux;
502 printf(" <normal>, ");
503 if (kget(rmask.rm_leaf, rnode_aux) == 0)
504 p_sockaddr(kgetsa((struct sockaddr *)rnode_aux.rn_mask),
505 NULL, 0, -1);
506 else
507 p_sockaddr(NULL, NULL, 0, -1);
508 } else
509 p_sockaddr(kgetsa((struct sockaddr *)rmask.rm_mask),
510 NULL, 0, -1);
511 putchar('}');
512 if ((rm = rmask.rm_next))
513 printf(" ->");
515 putchar('\n');
518 static void
519 ntreestuff(void)
521 size_t needed;
522 int mib[7];
523 int miblen;
524 char *buf, *next, *lim;
525 struct rt_msghdr *rtm;
527 mib[0] = CTL_NET;
528 mib[1] = PF_ROUTE;
529 mib[2] = 0;
530 mib[3] = 0;
531 mib[4] = NET_RT_DUMP;
532 mib[5] = 0;
533 if (cpuflag >= 0) {
534 mib[6] = cpuflag;
535 miblen = 7;
536 } else {
537 miblen = 6;
539 if (sysctl(mib, miblen, NULL, &needed, NULL, 0) < 0) {
540 err(1, "sysctl: net.route.0.0.dump estimate");
543 if ((buf = malloc(needed)) == NULL) {
544 err(2, "malloc(%lu)", (unsigned long)needed);
546 if (sysctl(mib, miblen, buf, &needed, NULL, 0) < 0) {
547 err(1, "sysctl: net.route.0.0.dump");
549 lim = buf + needed;
550 for (next = buf; next < lim; next += rtm->rtm_msglen) {
551 rtm = (struct rt_msghdr *)next;
552 np_rtentry(rtm);
556 static void
557 np_rtentry(struct rt_msghdr *rtm)
559 struct sockaddr *sa = (struct sockaddr *)(rtm + 1);
560 #ifdef notdef
561 static int masks_done, banner_printed;
562 #endif
563 static int old_af;
564 int af1 = 0, interesting = RTF_UP | RTF_GATEWAY | RTF_HOST;
566 #ifdef notdef
567 /* for the moment, netmasks are skipped over */
568 if (!banner_printed) {
569 printf("Netmasks:\n");
570 banner_printed = 1;
572 if (masks_done == 0) {
573 if (rtm->rtm_addrs != RTA_DST ) {
574 masks_done = 1;
575 af1 = sa->sa_family;
577 } else
578 #endif
579 af1 = sa->sa_family;
580 if (af1 != old_af) {
581 pr_family(af1);
582 old_af = af1;
584 if (rtm->rtm_addrs == RTA_DST)
585 p_sockaddr(sa, NULL, 0, 36);
586 else {
587 p_sockaddr(sa, NULL, rtm->rtm_flags, 16);
588 sa = (struct sockaddr *)(RT_ROUNDUP(sa->sa_len) + (char *)sa);
589 p_sockaddr(sa, NULL, 0, 18);
591 p_flags(rtm->rtm_flags & interesting, "%-6.6s ");
592 putchar('\n');
595 static void
596 p_sockaddr(struct sockaddr *sa, struct sockaddr *mask, int flags, int width)
598 const char *cp;
600 cp = fmt_sockaddr(sa, mask, flags);
602 if (width < 0 )
603 printf("%s ", cp);
604 else {
605 if (numeric_addr)
606 printf("%-*s ", width, cp);
607 else
608 printf("%-*.*s ", width, width, cp);
612 static const char *
613 fmt_sockaddr(struct sockaddr *sa, struct sockaddr *mask, int flags)
615 static char workbuf[128];
616 const char *cp = workbuf;
618 if (sa == NULL)
619 return ("null");
621 switch(sa->sa_family) {
622 case AF_INET:
624 struct sockaddr_in *sin = (struct sockaddr_in *)sa;
626 if ((sin->sin_addr.s_addr == INADDR_ANY) &&
627 mask &&
628 ntohl(((struct sockaddr_in *)mask)->sin_addr.s_addr)
629 ==0L)
630 cp = "default" ;
631 else if (flags & RTF_HOST)
632 cp = routename(sin->sin_addr.s_addr);
633 else if (mask)
634 cp = netname(sin->sin_addr.s_addr,
635 ntohl(((struct sockaddr_in *)mask)
636 ->sin_addr.s_addr));
637 else
638 cp = netname(sin->sin_addr.s_addr, 0L);
639 break;
642 #ifdef INET6
643 case AF_INET6:
645 struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)sa;
646 struct in6_addr *in6 = &sa6->sin6_addr;
649 * XXX: This is a special workaround for KAME kernels.
650 * sin6_scope_id field of SA should be set in the future.
652 if (IN6_IS_ADDR_LINKLOCAL(in6) ||
653 IN6_IS_ADDR_MC_LINKLOCAL(in6)) {
654 /* XXX: override is ok? */
655 sa6->sin6_scope_id = (u_int32_t)ntohs(*(u_short *)&in6->s6_addr[2]);
656 *(u_short *)&in6->s6_addr[2] = 0;
659 if (flags & RTF_HOST)
660 cp = routename6(sa6);
661 else if (mask)
662 cp = netname6(sa6,
663 &((struct sockaddr_in6 *)mask)->sin6_addr);
664 else {
665 cp = netname6(sa6, NULL);
667 break;
669 #endif /*INET6*/
671 case AF_NETGRAPH:
673 printf("%s", ((struct sockaddr_ng *)sa)->sg_data);
674 break;
676 case AF_LINK:
678 struct sockaddr_dl *sdl = (struct sockaddr_dl *)sa;
680 if (sdl->sdl_nlen == 0 && sdl->sdl_alen == 0 &&
681 sdl->sdl_slen == 0)
682 (void) sprintf(workbuf, "link#%d", sdl->sdl_index);
683 else
684 switch (sdl->sdl_type) {
686 case IFT_ETHER:
687 case IFT_L2VLAN:
688 case IFT_CARP:
689 if (sdl->sdl_alen == ETHER_ADDR_LEN) {
690 cp = ether_ntoa((struct ether_addr *)
691 (sdl->sdl_data + sdl->sdl_nlen));
692 break;
694 /* FALLTHROUGH */
695 default:
696 cp = link_ntoa(sdl);
697 break;
699 break;
702 case AF_MPLS:
704 struct sockaddr_mpls *smpls = (struct sockaddr_mpls *)sa;
706 (void) sprintf(workbuf, "%d", ntohl(smpls->smpls_label));
707 break;
710 default:
712 u_char *s = (u_char *)sa->sa_data, *slim;
713 char *cq, *cqlim;
715 cq = workbuf;
716 slim = sa->sa_len + (u_char *) sa;
717 cqlim = cq + sizeof(workbuf) - 6;
718 cq += sprintf(cq, "(%d)", sa->sa_family);
719 while (s < slim && cq < cqlim) {
720 cq += sprintf(cq, " %02x", *s++);
721 if (s < slim)
722 cq += sprintf(cq, "%02x", *s++);
724 cp = workbuf;
728 return (cp);
731 static void
732 p_flags(int f, const char *format)
734 printf(format, fmt_flags(f));
737 static const char *
738 fmt_flags(int f)
740 static char name[33];
741 char *flags;
742 struct bits *p = bits;
744 for (flags = name; p->b_mask; p++)
745 if (p->b_mask & f)
746 *flags++ = p->b_val;
747 *flags = '\0';
748 return (name);
751 static void
752 p_rtentry(struct rtentry *rt)
754 static struct ifnet ifnet, *lastif;
755 struct rtentry parent;
756 static char buffer[128];
757 static char prettyname[128];
758 struct sockaddr *sa;
759 sa_u addr, mask;
762 * Don't print protocol-cloned routes unless -a or -A.
764 if (rt->rt_flags & RTF_WASCLONED && !(aflag || Aflag)) {
765 if (kget(rt->rt_parent, parent) != 0)
766 return;
767 if (parent.rt_flags & RTF_PRCLONING)
768 return;
771 bzero(&addr, sizeof(addr));
772 if ((sa = kgetsa(rt_key(rt))))
773 bcopy(sa, &addr, sa->sa_len);
774 bzero(&mask, sizeof(mask));
775 if (rt_mask(rt) && (sa = kgetsa(rt_mask(rt))))
776 bcopy(sa, &mask, sa->sa_len);
777 p_sockaddr(&addr.u_sa, &mask.u_sa, rt->rt_flags, wid_dst);
778 p_sockaddr(kgetsa(rt->rt_gateway), NULL, RTF_HOST, wid_gw);
779 snprintf(buffer, sizeof(buffer), "%%-%d.%ds ", wid_flags, wid_flags);
780 p_flags(rt->rt_flags, buffer);
781 if (addr.u_sa.sa_family == AF_INET || Wflag) {
782 printf("%*ld %*lu ", wid_refs, rt->rt_refcnt,
783 wid_use, rt->rt_use);
784 if (Wflag) {
785 if (rt->rt_rmx.rmx_mtu != 0)
786 printf("%*lu ", wid_mtu, rt->rt_rmx.rmx_mtu);
787 else
788 printf("%*s ", wid_mtu, "");
791 if (rt->rt_ifp) {
792 if (rt->rt_ifp != lastif) {
793 if (kget(rt->rt_ifp, ifnet) == 0)
794 strlcpy(prettyname, ifnet.if_xname,
795 sizeof(prettyname));
796 else
797 strlcpy(prettyname, "---", sizeof(prettyname));
798 lastif = rt->rt_ifp;
800 printf("%*.*s", wid_if, wid_if, prettyname);
801 if (rt->rt_rmx.rmx_expire) {
802 struct timespec sp;
803 int expire_time;
805 clock_gettime(CLOCK_MONOTONIC, &sp);
807 expire_time = (int)(rt->rt_rmx.rmx_expire - sp.tv_sec);
808 if (expire_time > 0)
809 printf(" %*d", wid_expire, (int)expire_time);
810 else
811 printf("%*s ", wid_expire, "");
812 } else {
813 printf("%*s ", wid_expire, "");
815 if (rt->rt_nodes[0].rn_dupedkey)
816 printf(" =>");
818 if (Wflag) {
819 if (rt->rt_shim[0] != NULL)
820 printf(" %-*s", wid_mplslops, labelops(rt));
821 else
822 printf(" %-*s", wid_mplslops, "");
823 if (rt->rt_rmx.rmx_msl != 0)
824 printf(" %*lu", wid_msl, rt->rt_rmx.rmx_msl);
825 else
826 printf("%*s ", wid_msl, "");
827 if (rt->rt_rmx.rmx_iwmaxsegs != 0)
828 printf(" %*lu", wid_iwmax, rt->rt_rmx.rmx_iwmaxsegs);
829 else
830 printf("%*s ", wid_iwmax, "");
831 if (rt->rt_rmx.rmx_iwcapsegs != 0)
832 printf(" %*lu", wid_iw, rt->rt_rmx.rmx_iwcapsegs);
833 else
834 printf("%*s ", wid_iw, "");
836 putchar('\n');
839 char *
840 routename(u_long in)
842 char *cp;
843 static char line[MAXHOSTNAMELEN];
844 struct hostent *hp;
846 cp = NULL;
847 if (!numeric_addr) {
848 hp = gethostbyaddr(&in, sizeof (struct in_addr), AF_INET);
849 if (hp) {
850 cp = hp->h_name;
851 trimdomain(cp, strlen(cp));
854 if (cp) {
855 strncpy(line, cp, sizeof(line) - 1);
856 line[sizeof(line) - 1] = '\0';
857 } else {
858 #define C(x) ((x) & 0xff)
859 in = ntohl(in);
860 sprintf(line, "%lu.%lu.%lu.%lu",
861 C(in >> 24), C(in >> 16), C(in >> 8), C(in));
863 return (line);
866 static u_long
867 forgemask(u_long a)
869 u_long m;
871 if (IN_CLASSA(a))
872 m = IN_CLASSA_NET;
873 else if (IN_CLASSB(a))
874 m = IN_CLASSB_NET;
875 else
876 m = IN_CLASSC_NET;
877 return (m);
880 static void
881 domask(char *dst, u_long addr, u_long mask)
883 int b, i;
885 if (!mask || (forgemask(addr) == mask)) {
886 *dst = '\0';
887 return;
889 i = 0;
890 for (b = 0; b < 32; b++)
891 if (mask & (1 << b)) {
892 int bb;
894 i = b;
895 for (bb = b+1; bb < 32; bb++)
896 if (!(mask & (1 << bb))) {
897 i = -1; /* noncontig */
898 break;
900 break;
902 if (i == -1)
903 sprintf(dst, "&0x%lx", mask);
904 else
905 sprintf(dst, "/%d", 32-i);
909 * Return the name of the network whose address is given.
910 * The address is assumed to be that of a net or subnet, not a host.
912 char *
913 netname(u_long in, u_long mask)
915 char *cp = NULL;
916 static char line[MAXHOSTNAMELEN];
917 struct netent *np = NULL;
918 u_long dmask;
919 u_long i;
921 #define NSHIFT(m) ( \
922 (m) == IN_CLASSA_NET ? IN_CLASSA_NSHIFT : \
923 (m) == IN_CLASSB_NET ? IN_CLASSB_NSHIFT : \
924 (m) == IN_CLASSC_NET ? IN_CLASSC_NSHIFT : \
927 i = ntohl(in);
928 dmask = forgemask(i);
929 if (!numeric_addr && i) {
930 np = getnetbyaddr(i >> NSHIFT(mask), AF_INET);
931 if (np == NULL && mask == 0)
932 np = getnetbyaddr(i >> NSHIFT(dmask), AF_INET);
933 if (np != NULL) {
934 cp = np->n_name;
935 trimdomain(cp, strlen(cp));
938 #undef NSHIFT
939 if (cp != NULL) {
940 strncpy(line, cp, sizeof(line) - 1);
941 line[sizeof(line) - 1] = '\0';
942 } else {
943 if (mask <= IN_CLASSA_NET &&
944 (i & IN_CLASSA_HOST) == 0) {
945 sprintf(line, "%lu", C(i >> 24));
946 } else if (mask <= IN_CLASSB_NET &&
947 (i & IN_CLASSB_HOST) == 0) {
948 sprintf(line, "%lu.%lu",
949 C(i >> 24), C(i >> 16));
950 } else if (mask <= IN_CLASSC_NET &&
951 (i & IN_CLASSC_HOST) == 0) {
952 sprintf(line, "%lu.%lu.%lu",
953 C(i >> 24), C(i >> 16), C(i >> 8));
954 } else {
955 sprintf(line, "%lu.%lu.%lu.%lu",
956 C(i >> 24), C(i >> 16), C(i >> 8), C(i));
959 domask(line + strlen(line), i, mask);
960 return (line);
963 #ifdef INET6
964 const char *
965 netname6(struct sockaddr_in6 *sa6, struct in6_addr *mask)
967 static char line[MAXHOSTNAMELEN];
968 u_char *p = (u_char *)mask;
969 u_char *lim;
970 int masklen, illegal = 0, flag = NI_WITHSCOPEID;
972 if (mask) {
973 for (masklen = 0, lim = p + 16; p < lim; p++) {
974 switch (*p) {
975 case 0xff:
976 masklen += 8;
977 break;
978 case 0xfe:
979 masklen += 7;
980 break;
981 case 0xfc:
982 masklen += 6;
983 break;
984 case 0xf8:
985 masklen += 5;
986 break;
987 case 0xf0:
988 masklen += 4;
989 break;
990 case 0xe0:
991 masklen += 3;
992 break;
993 case 0xc0:
994 masklen += 2;
995 break;
996 case 0x80:
997 masklen += 1;
998 break;
999 case 0x00:
1000 break;
1001 default:
1002 illegal ++;
1003 break;
1006 if (illegal)
1007 fprintf(stderr, "illegal prefixlen\n");
1009 else
1010 masklen = 128;
1012 if (masklen == 0 && IN6_IS_ADDR_UNSPECIFIED(&sa6->sin6_addr))
1013 return("default");
1015 if (numeric_addr)
1016 flag |= NI_NUMERICHOST;
1017 getnameinfo((struct sockaddr *)sa6, sa6->sin6_len, line, sizeof(line),
1018 NULL, 0, flag);
1020 if (numeric_addr)
1021 sprintf(&line[strlen(line)], "/%d", masklen);
1023 return line;
1026 char *
1027 routename6(struct sockaddr_in6 *sa6)
1029 static char line[MAXHOSTNAMELEN];
1030 int flag = NI_WITHSCOPEID;
1031 /* use local variable for safety */
1032 struct sockaddr_in6 sa6_local;
1034 sa6_local.sin6_family = AF_INET6;
1035 sa6_local.sin6_len = sizeof(sa6_local);
1036 sa6_local.sin6_addr = sa6->sin6_addr;
1037 sa6_local.sin6_scope_id = sa6->sin6_scope_id;
1039 if (numeric_addr)
1040 flag |= NI_NUMERICHOST;
1042 getnameinfo((struct sockaddr *)&sa6_local, sa6_local.sin6_len,
1043 line, sizeof(line), NULL, 0, flag);
1045 return line;
1047 #endif /*INET6*/
1050 * Print routing statistics
1052 void
1053 rt_stats(void)
1055 struct rtstatistics rts;
1056 int error = 0;
1058 error = kinfo_get_net_rtstatistics(&rts);
1059 if (error) {
1060 printf("routing: could not retrieve statistics\n");
1061 return;
1063 printf("routing:\n");
1065 #define p(f, m) if (rts.f || sflag <= 1) \
1066 printf(m, rts.f, plural(rts.f))
1068 p(rts_badredirect, "\t%lu bad routing redirect%s\n");
1069 p(rts_dynamic, "\t%lu dynamically created route%s\n");
1070 p(rts_newgateway, "\t%lu new gateway%s due to redirects\n");
1071 p(rts_unreach, "\t%lu destination%s found unreachable\n");
1072 p(rts_wildcard, "\t%lu use%s of a wildcard route\n");
1073 #undef p
1076 static const char *
1077 labelops(struct rtentry *rt)
1079 const char *lops[] = { "push", "pop", "swap", "pop all" };
1080 static char buffer[100];
1081 char *cp = buffer;
1082 struct sockaddr_mpls *smpls;
1083 int i;
1085 for (i=0; i<MPLS_MAXLOPS; ++i) {
1087 if (rt->rt_shim[i] == NULL)
1088 break;
1089 if (i>0) {
1090 cp += snprintf(cp,
1091 sizeof(buffer) - (cp - buffer),
1092 ", ");
1094 smpls = (struct sockaddr_mpls *)kgetsa(rt->rt_shim[i]);
1095 if (smpls->smpls_op != MPLSLOP_POP &&
1096 smpls->smpls_op != MPLSLOP_POPALL){
1097 cp += snprintf(cp,
1098 sizeof(buffer) - (cp - buffer),
1099 "%s %d",
1100 lops[smpls->smpls_op - 1],
1101 ntohl(smpls->smpls_label));
1102 } else {
1103 cp += snprintf(cp,
1104 sizeof(buffer) - (cp - buffer),
1105 "%s",
1106 lops[smpls->smpls_op - 1]);
1110 return (buffer);