Always do PCI_REROUTE_INTERRUPT.
[dragonfly.git] / usr.bin / netstat / route.c
blob0536935204b7e9bde55b289b19dd3d88119a9ad5
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. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
33 * @(#)route.c 8.6 (Berkeley) 4/28/95
34 * $FreeBSD: src/usr.bin/netstat/route.c,v 1.41.2.14 2002/07/17 02:22:22 kbyanc Exp $
35 * $DragonFly: src/usr.bin/netstat/route.c,v 1.13 2008/07/07 22:02:10 nant Exp $
38 #include <sys/param.h>
39 #include <sys/protosw.h>
40 #include <sys/socket.h>
41 #include <sys/time.h>
43 #include <net/ethernet.h>
44 #include <net/if.h>
45 #include <net/if_var.h>
46 #include <net/if_dl.h>
47 #include <net/if_types.h>
48 #include <net/route.h>
50 #include <netinet/in.h>
51 #include <netipx/ipx.h>
52 #include <netatalk/at.h>
53 #include <netgraph/socket/ng_socket.h>
55 #ifdef NS
56 #include <netns/ns.h>
57 #endif
59 #include <netproto/mpls/mpls.h>
61 #include <sys/sysctl.h>
63 #include <arpa/inet.h>
64 #include <libutil.h>
65 #include <netdb.h>
66 #include <stdio.h>
67 #include <stdlib.h>
68 #include <string.h>
69 #include <unistd.h>
70 #include <err.h>
71 #include <time.h>
72 #include <kinfo.h>
73 #include "netstat.h"
75 #define kget(p, d) (kread((u_long)(p), (char *)&(d), sizeof (d)))
78 /* alignment constraint for routing socket */
79 #define ROUNDUP(a) \
80 ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
81 #define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len))
84 * Definitions for showing gateway flags.
86 struct bits {
87 u_long b_mask;
88 char b_val;
89 } bits[] = {
90 { RTF_UP, 'U' },
91 { RTF_GATEWAY, 'G' },
92 { RTF_HOST, 'H' },
93 { RTF_REJECT, 'R' },
94 { RTF_DYNAMIC, 'D' },
95 { RTF_MODIFIED, 'M' },
96 { RTF_DONE, 'd' }, /* Completed -- for routing messages only */
97 { RTF_CLONING, 'C' },
98 { RTF_XRESOLVE, 'X' },
99 { RTF_LLINFO, 'L' },
100 { RTF_STATIC, 'S' },
101 { RTF_PROTO1, '1' },
102 { RTF_PROTO2, '2' },
103 { RTF_WASCLONED,'W' },
104 { RTF_PRCLONING,'c' },
105 { RTF_PROTO3, '3' },
106 { RTF_BLACKHOLE,'B' },
107 { RTF_BROADCAST,'b' },
108 { RTF_MPLSOPS, 'm' },
109 { 0, 0 }
112 typedef union {
113 long dummy; /* Helps align structure. */
114 struct sockaddr u_sa;
115 u_short u_data[128];
116 } sa_u;
118 static sa_u pt_u;
120 int do_rtent = 0;
121 struct rtentry rtentry;
122 struct radix_node rnode;
123 struct radix_mask rmask;
124 struct radix_node_head *rt_tables[AF_MAX+1];
126 int NewTree = 0;
128 static struct sockaddr *kgetsa (struct sockaddr *);
129 static void size_cols (int ef, struct radix_node *rn);
130 static void size_cols_tree (struct radix_node *rn);
131 static void size_cols_rtentry (struct rtentry *rt);
132 static void p_tree (struct radix_node *);
133 static void p_rtnode (void);
134 static void ntreestuff (void);
135 static void np_rtentry (struct rt_msghdr *);
136 static void p_sockaddr (struct sockaddr *, struct sockaddr *, int, int);
137 static const char *fmt_sockaddr (struct sockaddr *sa, struct sockaddr *mask,
138 int flags);
139 static void p_flags (int, const char *);
140 static const char *fmt_flags(int f);
141 static void p_rtentry (struct rtentry *);
142 static u_long forgemask (u_long);
143 static void domask (char *, u_long, u_long);
144 static const char *labelops(struct rtentry *);
147 * Print routing tables.
149 void
150 routepr(u_long rtree)
152 struct radix_node_head *rnh, head;
153 int i;
155 printf("Routing tables\n");
157 if (Aflag == 0 && NewTree) {
158 ntreestuff();
159 } else {
160 if (rtree == 0) {
161 printf("rt_tables: symbol not in namelist\n");
162 return;
164 if (cpuflag >= 0) {
166 * Severe hack.
168 rtree += cpuflag * (AF_MAX + 1) * sizeof(void *);
170 kget(rtree, rt_tables);
171 for (i = 0; i <= AF_MAX; i++) {
172 if ((rnh = rt_tables[i]) == 0)
173 continue;
174 kget(rnh, head);
175 if (i == AF_UNSPEC) {
176 if (Aflag && af == 0) {
177 printf("Netmasks:\n");
178 p_tree(head.rnh_treetop);
180 } else if (af == AF_UNSPEC || af == i) {
181 size_cols(i, head.rnh_treetop);
182 pr_family(i);
183 do_rtent = 1;
184 pr_rthdr(i);
185 p_tree(head.rnh_treetop);
192 * Print address family header before a section of the routing table.
194 void
195 pr_family(int af1)
197 const char *afname;
199 switch (af1) {
200 case AF_INET:
201 afname = "Internet";
202 break;
203 #ifdef INET6
204 case AF_INET6:
205 afname = "Internet6";
206 break;
207 #endif /*INET6*/
208 case AF_IPX:
209 afname = "IPX";
210 break;
211 #ifdef NS
212 case AF_NS:
213 afname = "XNS";
214 break;
215 #endif
216 case AF_ISO:
217 afname = "ISO";
218 break;
219 case AF_APPLETALK:
220 afname = "AppleTalk";
221 break;
222 case AF_CCITT:
223 afname = "X.25";
224 break;
225 case AF_NETGRAPH:
226 afname = "Netgraph";
227 break;
228 case AF_MPLS:
229 afname = "MPLS";
230 break;
231 default:
232 afname = NULL;
233 break;
235 if (afname)
236 printf("\n%s:\n", afname);
237 else
238 printf("\nProtocol Family %d:\n", af1);
241 /* column widths; each followed by one space */
242 #ifndef INET6
243 #define WID_DST_DEFAULT(af) 18 /* width of destination column */
244 #define WID_GW_DEFAULT(af) 18 /* width of gateway column */
245 #define WID_IF_DEFAULT(af) (Wflag ? 8 : 6) /* width of netif column */
246 #else
247 #define WID_DST_DEFAULT(af) \
248 ((af) == AF_INET6 ? (numeric_addr ? 33: 18) : 18)
249 #define WID_GW_DEFAULT(af) \
250 ((af) == AF_INET6 ? (numeric_addr ? 29 : 18) : 18)
251 #define WID_IF_DEFAULT(af) ((af) == AF_INET6 ? 8 : (Wflag ? 8 :6))
252 #endif /*INET6*/
254 static int wid_dst;
255 static int wid_gw;
256 static int wid_flags;
257 static int wid_refs;
258 static int wid_use;
259 static int wid_mtu;
260 static int wid_if;
261 static int wid_expire;
262 static int wid_mplslops;
264 static void
265 size_cols(int ef, struct radix_node *rn)
267 wid_dst = WID_DST_DEFAULT(ef);
268 wid_gw = WID_GW_DEFAULT(ef);
269 wid_flags = 6;
270 wid_refs = 6;
271 wid_use = 8;
272 wid_mtu = 6;
273 wid_if = WID_IF_DEFAULT(ef);
274 wid_expire = 6;
275 wid_mplslops = 7;
277 if (Wflag)
278 size_cols_tree(rn);
281 static void
282 size_cols_tree(struct radix_node *rn)
284 again:
285 kget(rn, rnode);
286 if (rnode.rn_bit < 0) {
287 if ((rnode.rn_flags & RNF_ROOT) == 0) {
288 kget(rn, rtentry);
289 size_cols_rtentry(&rtentry);
291 if ((rn = rnode.rn_dupedkey))
292 goto again;
293 } else {
294 rn = rnode.rn_right;
295 size_cols_tree(rnode.rn_left);
296 size_cols_tree(rn);
300 static void
301 size_cols_rtentry(struct rtentry *rt)
303 static struct ifnet ifnet, *lastif;
304 struct rtentry parent;
305 static char buffer[100];
306 const char *bp;
307 struct sockaddr *sa;
308 sa_u addr, mask;
309 int len;
312 * Don't print protocol-cloned routes unless -a.
314 if (rt->rt_flags & RTF_WASCLONED && !aflag) {
315 kget(rt->rt_parent, parent);
316 if (parent.rt_flags & RTF_PRCLONING)
317 return;
320 bzero(&addr, sizeof(addr));
321 if ((sa = kgetsa(rt_key(rt))))
322 bcopy(sa, &addr, sa->sa_len);
323 bzero(&mask, sizeof(mask));
324 if (rt_mask(rt) && (sa = kgetsa(rt_mask(rt))))
325 bcopy(sa, &mask, sa->sa_len);
326 bp = fmt_sockaddr(&addr.u_sa, &mask.u_sa, rt->rt_flags);
327 len = strlen(bp);
328 wid_dst = MAX(len, wid_dst);
330 bp = fmt_sockaddr(kgetsa(rt->rt_gateway), NULL, RTF_HOST);
331 len = strlen(bp);
332 wid_gw = MAX(len, wid_gw);
334 bp = fmt_flags(rt->rt_flags);
335 len = strlen(bp);
336 wid_flags = MAX(len, wid_flags);
338 if (addr.u_sa.sa_family == AF_INET || Wflag) {
339 len = snprintf(buffer, sizeof(buffer), "%ld", rt->rt_refcnt);
340 wid_refs = MAX(len, wid_refs);
341 len = snprintf(buffer, sizeof(buffer), "%lu", rt->rt_use);
342 wid_use = MAX(len, wid_use);
343 if (Wflag && rt->rt_rmx.rmx_mtu != 0) {
344 len = snprintf(buffer, sizeof(buffer),
345 "%lu", rt->rt_rmx.rmx_mtu);
346 wid_mtu = MAX(len, wid_mtu);
349 if (rt->rt_ifp) {
350 if (rt->rt_ifp != lastif) {
351 kget(rt->rt_ifp, ifnet);
352 lastif = rt->rt_ifp;
353 len = strlen(ifnet.if_xname);
354 wid_if = MAX(len, wid_if);
356 if (rt->rt_rmx.rmx_expire) {
357 time_t expire_time;
359 if ((expire_time =
360 rt->rt_rmx.rmx_expire - time(NULL)) > 0) {
361 snprintf(buffer, sizeof(buffer), "%d",
362 (int)expire_time);
363 wid_expire = MAX(len, wid_expire);
367 if (rt->rt_shim[0] != NULL)
368 len = strlen(labelops(rt));
369 wid_mplslops = MAX(len, wid_mplslops);
374 * Print header for routing table columns.
376 void
377 pr_rthdr(int af1)
380 if (Aflag)
381 printf("%-8.8s ","Address");
382 if (af1 == AF_INET || Wflag) {
383 if (Wflag) {
384 printf("%-*.*s %-*.*s %-*.*s %*.*s %*.*s %*.*s %*.*s %*s %-*s\n",
385 wid_dst, wid_dst, "Destination",
386 wid_gw, wid_gw, "Gateway",
387 wid_flags, wid_flags, "Flags",
388 wid_refs, wid_refs, "Refs",
389 wid_use, wid_use, "Use",
390 wid_mtu, wid_mtu, "Mtu",
391 wid_if, wid_if, "Netif",
392 wid_expire, "Expire",
393 wid_mplslops, "Labelops");
394 } else {
395 printf("%-*.*s %-*.*s %-*.*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_if, wid_if, "Netif",
402 wid_expire, "Expire");
404 } else {
405 printf("%-*.*s %-*.*s %-*.*s %*.*s %*s\n",
406 wid_dst, wid_dst, "Destination",
407 wid_gw, wid_gw, "Gateway",
408 wid_flags, wid_flags, "Flags",
409 wid_if, wid_if, "Netif",
410 wid_expire, "Expire");
414 static struct sockaddr *
415 kgetsa(struct sockaddr *dst)
418 kget(dst, pt_u.u_sa);
419 if (pt_u.u_sa.sa_len > sizeof (pt_u.u_sa))
420 kread((u_long)dst, (char *)pt_u.u_data, pt_u.u_sa.sa_len);
421 return (&pt_u.u_sa);
424 static void
425 p_tree(struct radix_node *rn)
428 again:
429 kget(rn, rnode);
430 if (rnode.rn_bit < 0) {
431 if (Aflag)
432 printf("%-8.8lx ", (u_long)rn);
433 if (rnode.rn_flags & RNF_ROOT) {
434 if (Aflag)
435 printf("(root node)%s",
436 rnode.rn_dupedkey ? " =>\n" : "\n");
437 } else if (do_rtent) {
438 kget(rn, rtentry);
439 p_rtentry(&rtentry);
440 if (Aflag)
441 p_rtnode();
442 } else {
443 p_sockaddr(kgetsa((struct sockaddr *)rnode.rn_key),
444 NULL, 0, 44);
445 putchar('\n');
447 if ((rn = rnode.rn_dupedkey))
448 goto again;
449 } else {
450 if (Aflag && do_rtent) {
451 printf("%-8.8lx ", (u_long)rn);
452 p_rtnode();
454 rn = rnode.rn_right;
455 p_tree(rnode.rn_left);
456 p_tree(rn);
460 char nbuf[20];
462 static void
463 p_rtnode(void)
465 struct radix_mask *rm = rnode.rn_mklist;
467 if (rnode.rn_bit < 0) {
468 if (rnode.rn_mask) {
469 printf("\t mask ");
470 p_sockaddr(kgetsa((struct sockaddr *)rnode.rn_mask),
471 NULL, 0, -1);
472 } else if (rm == 0)
473 return;
474 } else {
475 sprintf(nbuf, "(%d)", rnode.rn_bit);
476 printf("%6.6s %8.8lx : %8.8lx", nbuf, (u_long)rnode.rn_left, (u_long)rnode.rn_right);
478 while (rm) {
479 kget(rm, rmask);
480 sprintf(nbuf, " %d refs, ", rmask.rm_refs);
481 printf(" mk = %8.8lx {(%d),%s",
482 (u_long)rm, -1 - rmask.rm_bit, rmask.rm_refs ? nbuf : " ");
483 if (rmask.rm_flags & RNF_NORMAL) {
484 struct radix_node rnode_aux;
485 printf(" <normal>, ");
486 kget(rmask.rm_leaf, rnode_aux);
487 p_sockaddr(kgetsa((struct sockaddr *)rnode_aux.rn_mask),
488 NULL, 0, -1);
489 } else
490 p_sockaddr(kgetsa((struct sockaddr *)rmask.rm_mask),
491 NULL, 0, -1);
492 putchar('}');
493 if ((rm = rmask.rm_next))
494 printf(" ->");
496 putchar('\n');
499 static void
500 ntreestuff(void)
502 size_t needed;
503 int mib[7];
504 int miblen;
505 char *buf, *next, *lim;
506 struct rt_msghdr *rtm;
508 mib[0] = CTL_NET;
509 mib[1] = PF_ROUTE;
510 mib[2] = 0;
511 mib[3] = 0;
512 mib[4] = NET_RT_DUMP;
513 mib[5] = 0;
514 if (cpuflag >= 0) {
515 mib[6] = cpuflag;
516 miblen = 7;
517 } else {
518 miblen = 6;
520 if (sysctl(mib, miblen, NULL, &needed, NULL, 0) < 0) {
521 err(1, "sysctl: net.route.0.0.dump estimate");
524 if ((buf = malloc(needed)) == 0) {
525 err(2, "malloc(%lu)", (unsigned long)needed);
527 if (sysctl(mib, miblen, buf, &needed, NULL, 0) < 0) {
528 err(1, "sysctl: net.route.0.0.dump");
530 lim = buf + needed;
531 for (next = buf; next < lim; next += rtm->rtm_msglen) {
532 rtm = (struct rt_msghdr *)next;
533 np_rtentry(rtm);
537 static void
538 np_rtentry(struct rt_msghdr *rtm)
540 struct sockaddr *sa = (struct sockaddr *)(rtm + 1);
541 #ifdef notdef
542 static int masks_done, banner_printed;
543 #endif
544 static int old_af;
545 int af1 = 0, interesting = RTF_UP | RTF_GATEWAY | RTF_HOST;
547 #ifdef notdef
548 /* for the moment, netmasks are skipped over */
549 if (!banner_printed) {
550 printf("Netmasks:\n");
551 banner_printed = 1;
553 if (masks_done == 0) {
554 if (rtm->rtm_addrs != RTA_DST ) {
555 masks_done = 1;
556 af1 = sa->sa_family;
558 } else
559 #endif
560 af1 = sa->sa_family;
561 if (af1 != old_af) {
562 pr_family(af1);
563 old_af = af1;
565 if (rtm->rtm_addrs == RTA_DST)
566 p_sockaddr(sa, NULL, 0, 36);
567 else {
568 p_sockaddr(sa, NULL, rtm->rtm_flags, 16);
569 sa = (struct sockaddr *)(ROUNDUP(sa->sa_len) + (char *)sa);
570 p_sockaddr(sa, NULL, 0, 18);
572 p_flags(rtm->rtm_flags & interesting, "%-6.6s ");
573 putchar('\n');
576 static void
577 p_sockaddr(struct sockaddr *sa, struct sockaddr *mask, int flags, int width)
579 const char *cp;
581 cp = fmt_sockaddr(sa, mask, flags);
583 if (width < 0 )
584 printf("%s ", cp);
585 else {
586 if (numeric_addr)
587 printf("%-*s ", width, cp);
588 else
589 printf("%-*.*s ", width, width, cp);
593 static const char *
594 fmt_sockaddr(struct sockaddr *sa, struct sockaddr *mask, int flags)
596 static char workbuf[128];
597 const char *cp = workbuf;
599 switch(sa->sa_family) {
600 case AF_INET:
602 struct sockaddr_in *sin = (struct sockaddr_in *)sa;
604 if ((sin->sin_addr.s_addr == INADDR_ANY) &&
605 mask &&
606 ntohl(((struct sockaddr_in *)mask)->sin_addr.s_addr)
607 ==0L)
608 cp = "default" ;
609 else if (flags & RTF_HOST)
610 cp = routename(sin->sin_addr.s_addr);
611 else if (mask)
612 cp = netname(sin->sin_addr.s_addr,
613 ntohl(((struct sockaddr_in *)mask)
614 ->sin_addr.s_addr));
615 else
616 cp = netname(sin->sin_addr.s_addr, 0L);
617 break;
620 #ifdef INET6
621 case AF_INET6:
623 struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)sa;
624 struct in6_addr *in6 = &sa6->sin6_addr;
627 * XXX: This is a special workaround for KAME kernels.
628 * sin6_scope_id field of SA should be set in the future.
630 if (IN6_IS_ADDR_LINKLOCAL(in6) ||
631 IN6_IS_ADDR_MC_LINKLOCAL(in6)) {
632 /* XXX: override is ok? */
633 sa6->sin6_scope_id = (u_int32_t)ntohs(*(u_short *)&in6->s6_addr[2]);
634 *(u_short *)&in6->s6_addr[2] = 0;
637 if (flags & RTF_HOST)
638 cp = routename6(sa6);
639 else if (mask)
640 cp = netname6(sa6,
641 &((struct sockaddr_in6 *)mask)->sin6_addr);
642 else {
643 cp = netname6(sa6, NULL);
645 break;
647 #endif /*INET6*/
649 case AF_IPX:
651 struct ipx_addr work = ((struct sockaddr_ipx *)sa)->sipx_addr;
652 if (ipx_nullnet(satoipx_addr(work)))
653 cp = "default";
654 else
655 cp = ipx_print(sa);
656 break;
658 case AF_APPLETALK:
660 if (!(flags & RTF_HOST) && mask)
661 cp = atalk_print2(sa,mask,9);
662 else
663 cp = atalk_print(sa,11);
664 break;
666 case AF_NETGRAPH:
668 printf("%s", ((struct sockaddr_ng *)sa)->sg_data);
669 break;
671 #ifdef NS
672 case AF_NS:
673 cp = ns_print(sa);
674 break;
675 #endif
677 case AF_LINK:
679 struct sockaddr_dl *sdl = (struct sockaddr_dl *)sa;
681 if (sdl->sdl_nlen == 0 && sdl->sdl_alen == 0 &&
682 sdl->sdl_slen == 0)
683 (void) sprintf(workbuf, "link#%d", sdl->sdl_index);
684 else
685 switch (sdl->sdl_type) {
687 case IFT_ETHER:
688 case IFT_L2VLAN:
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.
764 if (rt->rt_flags & RTF_WASCLONED && !aflag) {
765 kget(rt->rt_parent, parent);
766 if (parent.rt_flags & RTF_PRCLONING)
767 return;
770 bzero(&addr, sizeof(addr));
771 if ((sa = kgetsa(rt_key(rt))))
772 bcopy(sa, &addr, sa->sa_len);
773 bzero(&mask, sizeof(mask));
774 if (rt_mask(rt) && (sa = kgetsa(rt_mask(rt))))
775 bcopy(sa, &mask, sa->sa_len);
776 p_sockaddr(&addr.u_sa, &mask.u_sa, rt->rt_flags, wid_dst);
777 p_sockaddr(kgetsa(rt->rt_gateway), NULL, RTF_HOST, wid_gw);
778 snprintf(buffer, sizeof(buffer), "%%-%d.%ds ", wid_flags, wid_flags);
779 p_flags(rt->rt_flags, buffer);
780 if (addr.u_sa.sa_family == AF_INET || Wflag) {
781 printf("%*ld %*lu ", wid_refs, rt->rt_refcnt,
782 wid_use, rt->rt_use);
783 if (Wflag) {
784 if (rt->rt_rmx.rmx_mtu != 0)
785 printf("%*lu ", wid_mtu, rt->rt_rmx.rmx_mtu);
786 else
787 printf("%*s ", wid_mtu, "");
790 if (rt->rt_ifp) {
791 if (rt->rt_ifp != lastif) {
792 kget(rt->rt_ifp, ifnet);
793 lastif = rt->rt_ifp;
794 strlcpy(prettyname, ifnet.if_xname, sizeof(prettyname));
796 printf("%*.*s", wid_if, wid_if, prettyname);
797 if (rt->rt_rmx.rmx_expire) {
798 time_t expire_time;
800 if ((expire_time =
801 rt->rt_rmx.rmx_expire - time(NULL)) > 0)
802 printf(" %*d", wid_expire, (int)expire_time);
803 } else {
804 printf("%*s ", wid_expire, "");
806 if (rt->rt_nodes[0].rn_dupedkey)
807 printf(" =>");
809 if (Wflag) {
810 if (rt->rt_shim[0] != NULL)
811 printf(" %-*s", wid_mplslops, labelops(rt));
813 putchar('\n');
816 char *
817 routename(u_long in)
819 char *cp;
820 static char line[MAXHOSTNAMELEN];
821 struct hostent *hp;
823 cp = 0;
824 if (!numeric_addr) {
825 hp = gethostbyaddr(&in, sizeof (struct in_addr), AF_INET);
826 if (hp) {
827 cp = hp->h_name;
828 trimdomain(cp, strlen(cp));
831 if (cp) {
832 strncpy(line, cp, sizeof(line) - 1);
833 line[sizeof(line) - 1] = '\0';
834 } else {
835 #define C(x) ((x) & 0xff)
836 in = ntohl(in);
837 sprintf(line, "%lu.%lu.%lu.%lu",
838 C(in >> 24), C(in >> 16), C(in >> 8), C(in));
840 return (line);
843 static u_long
844 forgemask(u_long a)
846 u_long m;
848 if (IN_CLASSA(a))
849 m = IN_CLASSA_NET;
850 else if (IN_CLASSB(a))
851 m = IN_CLASSB_NET;
852 else
853 m = IN_CLASSC_NET;
854 return (m);
857 static void
858 domask(char *dst, u_long addr, u_long mask)
860 int b, i;
862 if (!mask || (forgemask(addr) == mask)) {
863 *dst = '\0';
864 return;
866 i = 0;
867 for (b = 0; b < 32; b++)
868 if (mask & (1 << b)) {
869 int bb;
871 i = b;
872 for (bb = b+1; bb < 32; bb++)
873 if (!(mask & (1 << bb))) {
874 i = -1; /* noncontig */
875 break;
877 break;
879 if (i == -1)
880 sprintf(dst, "&0x%lx", mask);
881 else
882 sprintf(dst, "/%d", 32-i);
886 * Return the name of the network whose address is given.
887 * The address is assumed to be that of a net or subnet, not a host.
889 char *
890 netname(u_long in, u_long mask)
892 char *cp = 0;
893 static char line[MAXHOSTNAMELEN];
894 struct netent *np = 0;
895 u_long dmask;
896 u_long i;
898 #define NSHIFT(m) ( \
899 (m) == IN_CLASSA_NET ? IN_CLASSA_NSHIFT : \
900 (m) == IN_CLASSB_NET ? IN_CLASSB_NSHIFT : \
901 (m) == IN_CLASSC_NET ? IN_CLASSC_NSHIFT : \
904 i = ntohl(in);
905 dmask = forgemask(i);
906 if (!numeric_addr && i) {
907 np = getnetbyaddr(i >> NSHIFT(mask), AF_INET);
908 if (np == NULL && mask == 0)
909 np = getnetbyaddr(i >> NSHIFT(dmask), AF_INET);
910 if (np != NULL) {
911 cp = np->n_name;
912 trimdomain(cp, strlen(cp));
915 #undef NSHIFT
916 if (cp != NULL) {
917 strncpy(line, cp, sizeof(line) - 1);
918 line[sizeof(line) - 1] = '\0';
919 } else {
920 if (mask <= IN_CLASSA_NET &&
921 (i & IN_CLASSA_HOST) == 0) {
922 sprintf(line, "%lu", C(i >> 24));
923 } else if (mask <= IN_CLASSB_NET &&
924 (i & IN_CLASSB_HOST) == 0) {
925 sprintf(line, "%lu.%lu",
926 C(i >> 24), C(i >> 16));
927 } else if (mask <= IN_CLASSC_NET &&
928 (i & IN_CLASSC_HOST) == 0) {
929 sprintf(line, "%lu.%lu.%lu",
930 C(i >> 24), C(i >> 16), C(i >> 8));
931 } else {
932 sprintf(line, "%lu.%lu.%lu.%lu",
933 C(i >> 24), C(i >> 16), C(i >> 8), C(i));
936 domask(line + strlen(line), i, mask);
937 return (line);
940 #ifdef INET6
941 const char *
942 netname6(struct sockaddr_in6 *sa6, struct in6_addr *mask)
944 static char line[MAXHOSTNAMELEN];
945 u_char *p = (u_char *)mask;
946 u_char *lim;
947 int masklen, illegal = 0, flag = NI_WITHSCOPEID;
949 if (mask) {
950 for (masklen = 0, lim = p + 16; p < lim; p++) {
951 switch (*p) {
952 case 0xff:
953 masklen += 8;
954 break;
955 case 0xfe:
956 masklen += 7;
957 break;
958 case 0xfc:
959 masklen += 6;
960 break;
961 case 0xf8:
962 masklen += 5;
963 break;
964 case 0xf0:
965 masklen += 4;
966 break;
967 case 0xe0:
968 masklen += 3;
969 break;
970 case 0xc0:
971 masklen += 2;
972 break;
973 case 0x80:
974 masklen += 1;
975 break;
976 case 0x00:
977 break;
978 default:
979 illegal ++;
980 break;
983 if (illegal)
984 fprintf(stderr, "illegal prefixlen\n");
986 else
987 masklen = 128;
989 if (masklen == 0 && IN6_IS_ADDR_UNSPECIFIED(&sa6->sin6_addr))
990 return("default");
992 if (numeric_addr)
993 flag |= NI_NUMERICHOST;
994 getnameinfo((struct sockaddr *)sa6, sa6->sin6_len, line, sizeof(line),
995 NULL, 0, flag);
997 if (numeric_addr)
998 sprintf(&line[strlen(line)], "/%d", masklen);
1000 return line;
1003 char *
1004 routename6(struct sockaddr_in6 *sa6)
1006 static char line[MAXHOSTNAMELEN];
1007 int flag = NI_WITHSCOPEID;
1008 /* use local variable for safety */
1009 struct sockaddr_in6 sa6_local;
1011 sa6_local.sin6_family = AF_INET6;
1012 sa6_local.sin6_len = sizeof(sa6_local);
1013 sa6_local.sin6_addr = sa6->sin6_addr;
1014 sa6_local.sin6_scope_id = sa6->sin6_scope_id;
1016 if (numeric_addr)
1017 flag |= NI_NUMERICHOST;
1019 getnameinfo((struct sockaddr *)&sa6_local, sa6_local.sin6_len,
1020 line, sizeof(line), NULL, 0, flag);
1022 return line;
1024 #endif /*INET6*/
1027 * Print routing statistics
1029 void
1030 rt_stats(void)
1032 struct rtstatistics rts;
1033 int error = 0;
1035 error = kinfo_get_net_rtstatistics(&rts);
1036 if (error) {
1037 printf("routing: could not retrieve statistics\n");
1038 return;
1040 printf("routing:\n");
1042 #define p(f, m) if (rts.f || sflag <= 1) \
1043 printf(m, rts.f, plural(rts.f))
1045 p(rts_badredirect, "\t%lu bad routing redirect%s\n");
1046 p(rts_dynamic, "\t%lu dynamically created route%s\n");
1047 p(rts_newgateway, "\t%lu new gateway%s due to redirects\n");
1048 p(rts_unreach, "\t%lu destination%s found unreachable\n");
1049 p(rts_wildcard, "\t%lu use%s of a wildcard route\n");
1050 #undef p
1053 char *
1054 ipx_print(struct sockaddr *sa)
1056 u_short port;
1057 struct servent *sp = 0;
1058 const char *net = "", *host = "";
1059 char *p;
1060 u_char *q;
1061 struct ipx_addr work = ((struct sockaddr_ipx *)sa)->sipx_addr;
1062 static char mybuf[50];
1063 char cport[10], chost[15], cnet[15];
1065 port = ntohs(work.x_port);
1067 if (ipx_nullnet(work) && ipx_nullhost(work)) {
1069 if (port) {
1070 if (sp)
1071 sprintf(mybuf, "*.%s", sp->s_name);
1072 else
1073 sprintf(mybuf, "*.%x", port);
1074 } else
1075 sprintf(mybuf, "*.*");
1077 return (mybuf);
1080 if (ipx_wildnet(work))
1081 net = "any";
1082 else if (ipx_nullnet(work))
1083 net = "*";
1084 else {
1085 q = work.x_net.c_net;
1086 sprintf(cnet, "%02x%02x%02x%02x",
1087 q[0], q[1], q[2], q[3]);
1088 for (p = cnet; *p == '0' && p < cnet + 8; p++)
1089 continue;
1090 net = p;
1093 if (ipx_wildhost(work))
1094 host = "any";
1095 else if (ipx_nullhost(work))
1096 host = "*";
1097 else {
1098 q = work.x_host.c_host;
1099 sprintf(chost, "%02x%02x%02x%02x%02x%02x",
1100 q[0], q[1], q[2], q[3], q[4], q[5]);
1101 for (p = chost; *p == '0' && p < chost + 12; p++)
1102 continue;
1103 host = p;
1106 if (port) {
1107 if (strcmp(host, "*") == 0)
1108 host = "";
1109 if (sp)
1110 snprintf(cport, sizeof(cport),
1111 "%s%s", *host ? "." : "", sp->s_name);
1112 else
1113 snprintf(cport, sizeof(cport),
1114 "%s%x", *host ? "." : "", port);
1115 } else
1116 *cport = 0;
1118 snprintf(mybuf, sizeof(mybuf), "%s.%s%s", net, host, cport);
1119 return(mybuf);
1122 char *
1123 ipx_phost(struct sockaddr *sa)
1125 struct sockaddr_ipx *sipx = (struct sockaddr_ipx *)sa;
1126 struct sockaddr_ipx work;
1127 static union ipx_net ipx_zeronet;
1128 char *p;
1129 struct ipx_addr in;
1131 work = *sipx;
1132 in = work.sipx_addr;
1134 work.sipx_addr.x_port = 0;
1135 work.sipx_addr.x_net = ipx_zeronet;
1136 p = ipx_print((struct sockaddr *)&work);
1137 if (strncmp("*.", p, 2) == 0) p += 2;
1139 return(p);
1142 #ifdef NS
1143 short ns_nullh[] = {0,0,0};
1144 short ns_bh[] = {-1,-1,-1};
1146 char *
1147 ns_print(struct sockaddr *sa)
1149 struct sockaddr_ns *sns = (struct sockaddr_ns*)sa;
1150 struct ns_addr work;
1151 union { union ns_net net_e; u_long long_e; } net;
1152 u_short port;
1153 static char mybuf[50], cport[10], chost[25];
1154 char *host = "";
1155 char *p; u_char *q;
1157 work = sns->sns_addr;
1158 port = ntohs(work.x_port);
1159 work.x_port = 0;
1160 net.net_e = work.x_net;
1161 if (ns_nullhost(work) && net.long_e == 0) {
1162 if (port ) {
1163 sprintf(mybuf, "*.%xH", port);
1164 upHex(mybuf);
1165 } else
1166 sprintf(mybuf, "*.*");
1167 return (mybuf);
1170 if (bcmp(ns_bh, work.x_host.c_host, 6) == 0) {
1171 host = "any";
1172 } else if (bcmp(ns_nullh, work.x_host.c_host, 6) == 0) {
1173 host = "*";
1174 } else {
1175 q = work.x_host.c_host;
1176 sprintf(chost, "%02x%02x%02x%02x%02x%02xH",
1177 q[0], q[1], q[2], q[3], q[4], q[5]);
1178 for (p = chost; *p == '0' && p < chost + 12; p++)
1179 continue;
1180 host = p;
1182 if (port)
1183 sprintf(cport, ".%xH", htons(port));
1184 else
1185 *cport = 0;
1187 sprintf(mybuf,"%xH.%s%s", ntohl(net.long_e), host, cport);
1188 upHex(mybuf);
1189 return(mybuf);
1192 char *
1193 ns_phost(struct sockaddr *sa)
1195 struct sockaddr_ns *sns = (struct sockaddr_ns *)sa;
1196 struct sockaddr_ns work;
1197 static union ns_net ns_zeronet;
1198 char *p;
1200 work = *sns;
1201 work.sns_addr.x_port = 0;
1202 work.sns_addr.x_net = ns_zeronet;
1204 p = ns_print((struct sockaddr *)&work);
1205 if (strncmp("0H.", p, 3) == 0)
1206 p += 3;
1207 return(p);
1209 #endif
1211 void
1212 upHex(char *p0)
1214 char *p = p0;
1216 for (; *p; p++)
1217 switch (*p) {
1219 case 'a':
1220 case 'b':
1221 case 'c':
1222 case 'd':
1223 case 'e':
1224 case 'f':
1225 *p += ('A' - 'a');
1226 break;
1230 static const char *
1231 labelops(struct rtentry *rt)
1233 const char *lops[] = { "push", "pop", "swap", "pop all" };
1234 static char buffer[100];
1235 char *cp = buffer;
1236 struct sockaddr_mpls *smpls;
1237 int i;
1239 for (i=0; i<MPLS_MAXLOPS; ++i) {
1241 if (rt->rt_shim[i] == NULL)
1242 break;
1243 if (i>0) {
1244 cp += snprintf(cp,
1245 sizeof(buffer) - (cp - buffer),
1246 ", ");
1248 smpls = (struct sockaddr_mpls *)kgetsa(rt->rt_shim[i]);
1249 if (smpls->smpls_op != MPLSLOP_POP &&
1250 smpls->smpls_op != MPLSLOP_POPALL){
1251 cp += snprintf(cp,
1252 sizeof(buffer) - (cp - buffer),
1253 "%s %d",
1254 lops[smpls->smpls_op - 1],
1255 ntohl(smpls->smpls_label));
1256 } else {
1257 cp += snprintf(cp,
1258 sizeof(buffer) - (cp - buffer),
1259 "%s",
1260 lops[smpls->smpls_op - 1]);
1264 return (buffer);