Remove some unnecessary casts.
[dragonfly.git] / usr.bin / netstat / route.c
CommitLineData
984263bc
MD
1/*
2 * Copyright (c) 1983, 1988, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
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.
20 *
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.
1de703da
MD
32 *
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 $
9b42cabe 35 * $DragonFly: src/usr.bin/netstat/route.c,v 1.13 2008/07/07 22:02:10 nant Exp $
984263bc
MD
36 */
37
984263bc
MD
38#include <sys/param.h>
39#include <sys/protosw.h>
40#include <sys/socket.h>
41#include <sys/time.h>
42
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>
49
50#include <netinet/in.h>
51#include <netipx/ipx.h>
52#include <netatalk/at.h>
38a690d7 53#include <netgraph/socket/ng_socket.h>
984263bc
MD
54
55#ifdef NS
56#include <netns/ns.h>
57#endif
58
9b42cabe
NA
59#include <netproto/mpls/mpls.h>
60
984263bc
MD
61#include <sys/sysctl.h>
62
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>
72ce0f6d 72#include <kinfo.h>
984263bc
MD
73#include "netstat.h"
74
75#define kget(p, d) (kread((u_long)(p), (char *)&(d), sizeof (d)))
76
77
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))
82
83/*
84 * Definitions for showing gateway flags.
85 */
86struct 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' },
9b42cabe 108 { RTF_MPLSOPS, 'm' },
984263bc
MD
109 { 0 }
110};
111
112typedef union {
113 long dummy; /* Helps align structure. */
114 struct sockaddr u_sa;
115 u_short u_data[128];
116} sa_u;
117
118static sa_u pt_u;
119
120int do_rtent = 0;
121struct rtentry rtentry;
122struct radix_node rnode;
123struct radix_mask rmask;
124struct radix_node_head *rt_tables[AF_MAX+1];
125
126int NewTree = 0;
127
128static struct sockaddr *kgetsa (struct sockaddr *);
129static void size_cols (int ef, struct radix_node *rn);
130static void size_cols_tree (struct radix_node *rn);
131static void size_cols_rtentry (struct rtentry *rt);
132static void p_tree (struct radix_node *);
133static void p_rtnode (void);
134static void ntreestuff (void);
135static void np_rtentry (struct rt_msghdr *);
136static void p_sockaddr (struct sockaddr *, struct sockaddr *, int, int);
137static const char *fmt_sockaddr (struct sockaddr *sa, struct sockaddr *mask,
138 int flags);
139static void p_flags (int, char *);
140static const char *fmt_flags(int f);
141static void p_rtentry (struct rtentry *);
142static u_long forgemask (u_long);
143static void domask (char *, u_long, u_long);
9b42cabe 144static const char *labelops(struct rtentry *);
984263bc
MD
145
146/*
147 * Print routing tables.
148 */
149void
150routepr(u_long rtree)
151{
152 struct radix_node_head *rnh, head;
153 int i;
154
155 printf("Routing tables\n");
156
b06a5e2a 157 if (Aflag == 0 && NewTree) {
984263bc 158 ntreestuff();
b06a5e2a 159 } else {
984263bc
MD
160 if (rtree == 0) {
161 printf("rt_tables: symbol not in namelist\n");
162 return;
163 }
b06a5e2a
MD
164 if (cpuflag >= 0) {
165 /*
166 * Severe hack.
167 */
168 rtree += cpuflag * (AF_MAX + 1) * sizeof(void *);
169 }
984263bc
MD
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);
179 }
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);
186 }
187 }
188 }
189}
190
191/*
192 * Print address family header before a section of the routing table.
193 */
194void
195pr_family(int af)
196{
197 char *afname;
198
199 switch (af) {
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;
9b42cabe
NA
228 case AF_MPLS:
229 afname = "MPLS";
230 break;
984263bc
MD
231 default:
232 afname = NULL;
233 break;
234 }
235 if (afname)
236 printf("\n%s:\n", afname);
237 else
238 printf("\nProtocol Family %d:\n", af);
239}
240
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 */
35d7d0f9 245#define WID_IF_DEFAULT(af) (Wflag ? 8 : 6) /* width of netif column */
984263bc
MD
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)
35d7d0f9 251#define WID_IF_DEFAULT(af) ((af) == AF_INET6 ? 8 : (Wflag ? 8 :6))
984263bc
MD
252#endif /*INET6*/
253
254static int wid_dst;
255static int wid_gw;
256static int wid_flags;
257static int wid_refs;
258static int wid_use;
259static int wid_mtu;
260static int wid_if;
261static int wid_expire;
9b42cabe 262static int wid_mplslops;
984263bc
MD
263
264static void
265size_cols(int ef, struct radix_node *rn)
266{
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;
9b42cabe 275 wid_mplslops = 7;
984263bc
MD
276
277 if (Wflag)
278 size_cols_tree(rn);
279}
280
281static void
282size_cols_tree(struct radix_node *rn)
283{
284again:
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);
290 }
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);
297 }
298}
299
300static void
301size_cols_rtentry(struct rtentry *rt)
302{
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;
9b42cabe 309 int len, i;
984263bc
MD
310
311 /*
312 * Don't print protocol-cloned routes unless -a.
313 */
314 if (rt->rt_flags & RTF_WASCLONED && !aflag) {
315 kget(rt->rt_parent, parent);
316 if (parent.rt_flags & RTF_PRCLONING)
317 return;
318 }
319
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);
329
330 bp = fmt_sockaddr(kgetsa(rt->rt_gateway), NULL, RTF_HOST);
331 len = strlen(bp);
332 wid_gw = MAX(len, wid_gw);
333
334 bp = fmt_flags(rt->rt_flags);
335 len = strlen(bp);
336 wid_flags = MAX(len, wid_flags);
337
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);
347 }
348 }
349 if (rt->rt_ifp) {
350 if (rt->rt_ifp != lastif) {
984263bc 351 kget(rt->rt_ifp, ifnet);
984263bc 352 lastif = rt->rt_ifp;
3e4a09e7 353 len = strlen(ifnet.if_xname);
984263bc
MD
354 wid_if = MAX(len, wid_if);
355 }
356 if (rt->rt_rmx.rmx_expire) {
357 time_t expire_time;
358
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);
364 }
365 }
366 }
9b42cabe
NA
367 if (rt->rt_shim[0] != NULL)
368 len = strlen(labelops(rt));
369 wid_mplslops = MAX(len, wid_mplslops);
984263bc
MD
370}
371
372
373/*
374 * Print header for routing table columns.
375 */
376void
377pr_rthdr(int af)
378{
379
380 if (Aflag)
381 printf("%-8.8s ","Address");
382 if (af == AF_INET || Wflag) {
383 if (Wflag) {
9b42cabe 384 printf("%-*.*s %-*.*s %-*.*s %*.*s %*.*s %*.*s %*.*s %*s %-*s\n",
984263bc
MD
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",
9b42cabe
NA
392 wid_expire, "Expire",
393 wid_mplslops, "Labelops");
984263bc
MD
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");
403 }
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");
411 }
412}
413
414static struct sockaddr *
415kgetsa(struct sockaddr *dst)
416{
417
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);
422}
423
424static void
425p_tree(struct radix_node *rn)
426{
427
428again:
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');
446 }
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();
453 }
454 rn = rnode.rn_right;
455 p_tree(rnode.rn_left);
456 p_tree(rn);
457 }
458}
459
460char nbuf[20];
461
462static void
463p_rtnode(void)
464{
465 struct radix_mask *rm = rnode.rn_mklist;
466
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);
477 }
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('}');
6bf4ea91 493 if ((rm = rmask.rm_next))
984263bc
MD
494 printf(" ->");
495 }
496 putchar('\n');
497}
498
499static void
500ntreestuff(void)
501{
502 size_t needed;
b06a5e2a
MD
503 int mib[7];
504 int miblen;
984263bc 505 char *buf, *next, *lim;
6bd457ed 506 struct rt_msghdr *rtm;
984263bc
MD
507
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;
b06a5e2a
MD
514 if (cpuflag >= 0) {
515 mib[6] = cpuflag;
516 miblen = 7;
517 } else {
518 miblen = 6;
519 }
520 if (sysctl(mib, miblen, NULL, &needed, NULL, 0) < 0) {
984263bc
MD
521 err(1, "sysctl: net.route.0.0.dump estimate");
522 }
523
524 if ((buf = malloc(needed)) == 0) {
525 err(2, "malloc(%lu)", (unsigned long)needed);
526 }
b06a5e2a 527 if (sysctl(mib, miblen, buf, &needed, NULL, 0) < 0) {
984263bc
MD
528 err(1, "sysctl: net.route.0.0.dump");
529 }
530 lim = buf + needed;
531 for (next = buf; next < lim; next += rtm->rtm_msglen) {
532 rtm = (struct rt_msghdr *)next;
533 np_rtentry(rtm);
534 }
535}
536
537static void
538np_rtentry(struct rt_msghdr *rtm)
539{
6bd457ed 540 struct sockaddr *sa = (struct sockaddr *)(rtm + 1);
984263bc
MD
541#ifdef notdef
542 static int masks_done, banner_printed;
543#endif
544 static int old_af;
545 int af = 0, interesting = RTF_UP | RTF_GATEWAY | RTF_HOST;
546
547#ifdef notdef
548 /* for the moment, netmasks are skipped over */
549 if (!banner_printed) {
550 printf("Netmasks:\n");
551 banner_printed = 1;
552 }
553 if (masks_done == 0) {
554 if (rtm->rtm_addrs != RTA_DST ) {
555 masks_done = 1;
556 af = sa->sa_family;
557 }
558 } else
559#endif
560 af = sa->sa_family;
561 if (af != old_af) {
562 pr_family(af);
563 old_af = af;
564 }
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);
571 }
572 p_flags(rtm->rtm_flags & interesting, "%-6.6s ");
573 putchar('\n');
574}
575
576static void
577p_sockaddr(struct sockaddr *sa, struct sockaddr *mask, int flags, int width)
578{
579 const char *cp;
580
581 cp = fmt_sockaddr(sa, mask, flags);
582
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);
590 }
591}
592
593static const char *
594fmt_sockaddr(struct sockaddr *sa, struct sockaddr *mask, int flags)
595{
596 static char workbuf[128];
597 char *cplim;
598 char *cp = workbuf;
599
600 switch(sa->sa_family) {
601 case AF_INET:
602 {
6bd457ed 603 struct sockaddr_in *sin = (struct sockaddr_in *)sa;
984263bc
MD
604
605 if ((sin->sin_addr.s_addr == INADDR_ANY) &&
606 mask &&
607 ntohl(((struct sockaddr_in *)mask)->sin_addr.s_addr)
608 ==0L)
609 cp = "default" ;
610 else if (flags & RTF_HOST)
611 cp = routename(sin->sin_addr.s_addr);
612 else if (mask)
613 cp = netname(sin->sin_addr.s_addr,
614 ntohl(((struct sockaddr_in *)mask)
615 ->sin_addr.s_addr));
616 else
617 cp = netname(sin->sin_addr.s_addr, 0L);
618 break;
619 }
620
621#ifdef INET6
622 case AF_INET6:
623 {
624 struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)sa;
625 struct in6_addr *in6 = &sa6->sin6_addr;
626
627 /*
628 * XXX: This is a special workaround for KAME kernels.
629 * sin6_scope_id field of SA should be set in the future.
630 */
631 if (IN6_IS_ADDR_LINKLOCAL(in6) ||
632 IN6_IS_ADDR_MC_LINKLOCAL(in6)) {
633 /* XXX: override is ok? */
634 sa6->sin6_scope_id = (u_int32_t)ntohs(*(u_short *)&in6->s6_addr[2]);
635 *(u_short *)&in6->s6_addr[2] = 0;
636 }
637
638 if (flags & RTF_HOST)
639 cp = routename6(sa6);
640 else if (mask)
641 cp = netname6(sa6,
642 &((struct sockaddr_in6 *)mask)->sin6_addr);
643 else {
644 cp = netname6(sa6, NULL);
645 }
646 break;
647 }
648#endif /*INET6*/
649
650 case AF_IPX:
651 {
652 struct ipx_addr work = ((struct sockaddr_ipx *)sa)->sipx_addr;
653 if (ipx_nullnet(satoipx_addr(work)))
654 cp = "default";
655 else
656 cp = ipx_print(sa);
657 break;
658 }
659 case AF_APPLETALK:
660 {
661 if (!(flags & RTF_HOST) && mask)
662 cp = atalk_print2(sa,mask,9);
663 else
664 cp = atalk_print(sa,11);
665 break;
666 }
667 case AF_NETGRAPH:
668 {
669 printf("%s", ((struct sockaddr_ng *)sa)->sg_data);
670 break;
671 }
672#ifdef NS
673 case AF_NS:
674 cp = ns_print(sa);
675 break;
676#endif
677
678 case AF_LINK:
679 {
6bd457ed 680 struct sockaddr_dl *sdl = (struct sockaddr_dl *)sa;
984263bc
MD
681
682 if (sdl->sdl_nlen == 0 && sdl->sdl_alen == 0 &&
683 sdl->sdl_slen == 0)
684 (void) sprintf(workbuf, "link#%d", sdl->sdl_index);
685 else
686 switch (sdl->sdl_type) {
687
688 case IFT_ETHER:
44f8b4d2 689 case IFT_L2VLAN:
984263bc
MD
690 if (sdl->sdl_alen == ETHER_ADDR_LEN) {
691 cp = ether_ntoa((struct ether_addr *)
692 (sdl->sdl_data + sdl->sdl_nlen));
693 break;
694 }
695 /* FALLTHROUGH */
696 default:
697 cp = link_ntoa(sdl);
698 break;
699 }
700 break;
701 }
702
9b42cabe
NA
703 case AF_MPLS:
704 {
705 struct sockaddr_mpls *smpls = (struct sockaddr_mpls *)sa;
706
707 (void) sprintf(workbuf, "%d", ntohl(smpls->smpls_label));
708 break;
709 }
710
984263bc
MD
711 default:
712 {
6bd457ed 713 u_char *s = (u_char *)sa->sa_data, *slim;
984263bc
MD
714
715 slim = sa->sa_len + (u_char *) sa;
716 cplim = cp + sizeof(workbuf) - 6;
717 cp += sprintf(cp, "(%d)", sa->sa_family);
718 while (s < slim && cp < cplim) {
719 cp += sprintf(cp, " %02x", *s++);
720 if (s < slim)
721 cp += sprintf(cp, "%02x", *s++);
722 }
723 cp = workbuf;
724 }
725 }
726
727 return (cp);
728}
729
730static void
731p_flags(int f, char *format)
732{
733 printf(format, fmt_flags(f));
734}
735
736static const char *
737fmt_flags(int f)
738{
739 static char name[33];
740 char *flags;
741 struct bits *p = bits;
742
743 for (flags = name; p->b_mask; p++)
744 if (p->b_mask & f)
745 *flags++ = p->b_val;
746 *flags = '\0';
747 return (name);
748}
749
750static void
751p_rtentry(struct rtentry *rt)
752{
753 static struct ifnet ifnet, *lastif;
754 struct rtentry parent;
755 static char buffer[128];
756 static char prettyname[128];
757 struct sockaddr *sa;
758 sa_u addr, mask;
759
760 /*
761 * Don't print protocol-cloned routes unless -a.
762 */
763 if (rt->rt_flags & RTF_WASCLONED && !aflag) {
764 kget(rt->rt_parent, parent);
765 if (parent.rt_flags & RTF_PRCLONING)
766 return;
767 }
768
769 bzero(&addr, sizeof(addr));
770 if ((sa = kgetsa(rt_key(rt))))
771 bcopy(sa, &addr, sa->sa_len);
772 bzero(&mask, sizeof(mask));
773 if (rt_mask(rt) && (sa = kgetsa(rt_mask(rt))))
774 bcopy(sa, &mask, sa->sa_len);
775 p_sockaddr(&addr.u_sa, &mask.u_sa, rt->rt_flags, wid_dst);
776 p_sockaddr(kgetsa(rt->rt_gateway), NULL, RTF_HOST, wid_gw);
777 snprintf(buffer, sizeof(buffer), "%%-%d.%ds ", wid_flags, wid_flags);
778 p_flags(rt->rt_flags, buffer);
779 if (addr.u_sa.sa_family == AF_INET || Wflag) {
780 printf("%*ld %*lu ", wid_refs, rt->rt_refcnt,
781 wid_use, rt->rt_use);
782 if (Wflag) {
783 if (rt->rt_rmx.rmx_mtu != 0)
784 printf("%*lu ", wid_mtu, rt->rt_rmx.rmx_mtu);
785 else
786 printf("%*s ", wid_mtu, "");
787 }
788 }
789 if (rt->rt_ifp) {
790 if (rt->rt_ifp != lastif) {
791 kget(rt->rt_ifp, ifnet);
984263bc 792 lastif = rt->rt_ifp;
3e4a09e7 793 strlcpy(prettyname, ifnet.if_xname, sizeof(prettyname));
984263bc
MD
794 }
795 printf("%*.*s", wid_if, wid_if, prettyname);
796 if (rt->rt_rmx.rmx_expire) {
797 time_t expire_time;
798
799 if ((expire_time =
800 rt->rt_rmx.rmx_expire - time((time_t *)0)) > 0)
801 printf(" %*d", wid_expire, (int)expire_time);
9b42cabe
NA
802 } else {
803 printf("%*s ", wid_expire, "");
984263bc
MD
804 }
805 if (rt->rt_nodes[0].rn_dupedkey)
806 printf(" =>");
807 }
9b42cabe
NA
808 if (Wflag) {
809 if (rt->rt_shim[0] != NULL)
810 printf(" %-*s", wid_mplslops, labelops(rt));
811 }
984263bc
MD
812 putchar('\n');
813}
814
815char *
816routename(u_long in)
817{
6bd457ed 818 char *cp;
984263bc
MD
819 static char line[MAXHOSTNAMELEN];
820 struct hostent *hp;
821
822 cp = 0;
823 if (!numeric_addr) {
15b85273 824 hp = gethostbyaddr(&in, sizeof (struct in_addr), AF_INET);
984263bc
MD
825 if (hp) {
826 cp = hp->h_name;
827 trimdomain(cp, strlen(cp));
828 }
829 }
830 if (cp) {
831 strncpy(line, cp, sizeof(line) - 1);
832 line[sizeof(line) - 1] = '\0';
833 } else {
834#define C(x) ((x) & 0xff)
835 in = ntohl(in);
836 sprintf(line, "%lu.%lu.%lu.%lu",
837 C(in >> 24), C(in >> 16), C(in >> 8), C(in));
838 }
839 return (line);
840}
841
842static u_long
843forgemask(u_long a)
844{
845 u_long m;
846
847 if (IN_CLASSA(a))
848 m = IN_CLASSA_NET;
849 else if (IN_CLASSB(a))
850 m = IN_CLASSB_NET;
851 else
852 m = IN_CLASSC_NET;
853 return (m);
854}
855
856static void
857domask(char *dst, u_long addr, u_long mask)
858{
6bd457ed 859 int b, i;
984263bc
MD
860
861 if (!mask || (forgemask(addr) == mask)) {
862 *dst = '\0';
863 return;
864 }
865 i = 0;
866 for (b = 0; b < 32; b++)
867 if (mask & (1 << b)) {
6bd457ed 868 int bb;
984263bc
MD
869
870 i = b;
871 for (bb = b+1; bb < 32; bb++)
872 if (!(mask & (1 << bb))) {
873 i = -1; /* noncontig */
874 break;
875 }
876 break;
877 }
878 if (i == -1)
879 sprintf(dst, "&0x%lx", mask);
880 else
881 sprintf(dst, "/%d", 32-i);
882}
883
884/*
885 * Return the name of the network whose address is given.
886 * The address is assumed to be that of a net or subnet, not a host.
887 */
888char *
889netname(u_long in, u_long mask)
890{
891 char *cp = 0;
892 static char line[MAXHOSTNAMELEN];
893 struct netent *np = 0;
894 u_long dmask;
6bd457ed 895 u_long i;
984263bc
MD
896
897#define NSHIFT(m) ( \
898 (m) == IN_CLASSA_NET ? IN_CLASSA_NSHIFT : \
899 (m) == IN_CLASSB_NET ? IN_CLASSB_NSHIFT : \
900 (m) == IN_CLASSC_NET ? IN_CLASSC_NSHIFT : \
901 0)
902
903 i = ntohl(in);
904 dmask = forgemask(i);
905 if (!numeric_addr && i) {
906 np = getnetbyaddr(i >> NSHIFT(mask), AF_INET);
907 if (np == NULL && mask == 0)
908 np = getnetbyaddr(i >> NSHIFT(dmask), AF_INET);
909 if (np != NULL) {
910 cp = np->n_name;
911 trimdomain(cp, strlen(cp));
912 }
913 }
914#undef NSHIFT
915 if (cp != NULL) {
916 strncpy(line, cp, sizeof(line) - 1);
917 line[sizeof(line) - 1] = '\0';
918 } else {
3d970e2c
SZ
919 if (mask <= IN_CLASSA_NET &&
920 (i & IN_CLASSA_HOST) == 0) {
984263bc 921 sprintf(line, "%lu", C(i >> 24));
3d970e2c
SZ
922 } else if (mask <= IN_CLASSB_NET &&
923 (i & IN_CLASSB_HOST) == 0) {
984263bc
MD
924 sprintf(line, "%lu.%lu",
925 C(i >> 24), C(i >> 16));
3d970e2c
SZ
926 } else if (mask <= IN_CLASSC_NET &&
927 (i & IN_CLASSC_HOST) == 0) {
984263bc
MD
928 sprintf(line, "%lu.%lu.%lu",
929 C(i >> 24), C(i >> 16), C(i >> 8));
3d970e2c 930 } else {
984263bc
MD
931 sprintf(line, "%lu.%lu.%lu.%lu",
932 C(i >> 24), C(i >> 16), C(i >> 8), C(i));
984263bc
MD
933 }
934 }
935 domask(line + strlen(line), i, mask);
936 return (line);
937}
938
939#ifdef INET6
940char *
941netname6(struct sockaddr_in6 *sa6, struct in6_addr *mask)
942{
943 static char line[MAXHOSTNAMELEN];
944 u_char *p = (u_char *)mask;
945 u_char *lim;
946 int masklen, illegal = 0, flag = NI_WITHSCOPEID;
947
948 if (mask) {
949 for (masklen = 0, lim = p + 16; p < lim; p++) {
950 switch (*p) {
951 case 0xff:
952 masklen += 8;
953 break;
954 case 0xfe:
955 masklen += 7;
956 break;
957 case 0xfc:
958 masklen += 6;
959 break;
960 case 0xf8:
961 masklen += 5;
962 break;
963 case 0xf0:
964 masklen += 4;
965 break;
966 case 0xe0:
967 masklen += 3;
968 break;
969 case 0xc0:
970 masklen += 2;
971 break;
972 case 0x80:
973 masklen += 1;
974 break;
975 case 0x00:
976 break;
977 default:
978 illegal ++;
979 break;
980 }
981 }
982 if (illegal)
983 fprintf(stderr, "illegal prefixlen\n");
984 }
985 else
986 masklen = 128;
987
988 if (masklen == 0 && IN6_IS_ADDR_UNSPECIFIED(&sa6->sin6_addr))
989 return("default");
990
991 if (numeric_addr)
992 flag |= NI_NUMERICHOST;
993 getnameinfo((struct sockaddr *)sa6, sa6->sin6_len, line, sizeof(line),
994 NULL, 0, flag);
995
996 if (numeric_addr)
997 sprintf(&line[strlen(line)], "/%d", masklen);
998
999 return line;
1000}
1001
1002char *
1003routename6(struct sockaddr_in6 *sa6)
1004{
1005 static char line[MAXHOSTNAMELEN];
1006 int flag = NI_WITHSCOPEID;
1007 /* use local variable for safety */
1008 struct sockaddr_in6 sa6_local = {AF_INET6, sizeof(sa6_local),};
1009
1010 sa6_local.sin6_addr = sa6->sin6_addr;
1011 sa6_local.sin6_scope_id = sa6->sin6_scope_id;
1012
1013 if (numeric_addr)
1014 flag |= NI_NUMERICHOST;
1015
1016 getnameinfo((struct sockaddr *)&sa6_local, sa6_local.sin6_len,
1017 line, sizeof(line), NULL, 0, flag);
1018
1019 return line;
1020}
1021#endif /*INET6*/
1022
1023/*
1024 * Print routing statistics
1025 */
1026void
72ce0f6d 1027rt_stats(void)
984263bc 1028{
72ce0f6d
HP
1029 struct rtstatistics rts;
1030 int error = 0;
984263bc 1031
72ce0f6d
HP
1032 error = kinfo_get_net_rtstatistics(&rts);
1033 if (error) {
1034 printf("routing: could not retrieve statistics\n");
984263bc
MD
1035 return;
1036 }
984263bc
MD
1037 printf("routing:\n");
1038
72ce0f6d
HP
1039#define p(f, m) if (rts.f || sflag <= 1) \
1040 printf(m, rts.f, plural(rts.f))
984263bc 1041
72ce0f6d
HP
1042 p(rts_badredirect, "\t%lu bad routing redirect%s\n");
1043 p(rts_dynamic, "\t%lu dynamically created route%s\n");
1044 p(rts_newgateway, "\t%lu new gateway%s due to redirects\n");
1045 p(rts_unreach, "\t%lu destination%s found unreachable\n");
1046 p(rts_wildcard, "\t%lu use%s of a wildcard route\n");
984263bc 1047#undef p
984263bc
MD
1048}
1049
1050char *
1051ipx_print(struct sockaddr *sa)
1052{
1053 u_short port;
1054 struct servent *sp = 0;
1055 char *net = "", *host = "";
6bd457ed
DR
1056 char *p;
1057 u_char *q;
984263bc
MD
1058 struct ipx_addr work = ((struct sockaddr_ipx *)sa)->sipx_addr;
1059 static char mybuf[50];
1060 char cport[10], chost[15], cnet[15];
1061
1062 port = ntohs(work.x_port);
1063
1064 if (ipx_nullnet(work) && ipx_nullhost(work)) {
1065
1066 if (port) {
1067 if (sp)
1068 sprintf(mybuf, "*.%s", sp->s_name);
1069 else
1070 sprintf(mybuf, "*.%x", port);
1071 } else
1072 sprintf(mybuf, "*.*");
1073
1074 return (mybuf);
1075 }
1076
1077 if (ipx_wildnet(work))
1078 net = "any";
1079 else if (ipx_nullnet(work))
1080 net = "*";
1081 else {
1082 q = work.x_net.c_net;
1083 sprintf(cnet, "%02x%02x%02x%02x",
1084 q[0], q[1], q[2], q[3]);
1085 for (p = cnet; *p == '0' && p < cnet + 8; p++)
1086 continue;
1087 net = p;
1088 }
1089
1090 if (ipx_wildhost(work))
1091 host = "any";
1092 else if (ipx_nullhost(work))
1093 host = "*";
1094 else {
1095 q = work.x_host.c_host;
1096 sprintf(chost, "%02x%02x%02x%02x%02x%02x",
1097 q[0], q[1], q[2], q[3], q[4], q[5]);
1098 for (p = chost; *p == '0' && p < chost + 12; p++)
1099 continue;
1100 host = p;
1101 }
1102
1103 if (port) {
1104 if (strcmp(host, "*") == 0)
1105 host = "";
1106 if (sp)
1107 snprintf(cport, sizeof(cport),
1108 "%s%s", *host ? "." : "", sp->s_name);
1109 else
1110 snprintf(cport, sizeof(cport),
1111 "%s%x", *host ? "." : "", port);
1112 } else
1113 *cport = 0;
1114
1115 snprintf(mybuf, sizeof(mybuf), "%s.%s%s", net, host, cport);
1116 return(mybuf);
1117}
1118
1119char *
1120ipx_phost(struct sockaddr *sa)
1121{
6bd457ed 1122 struct sockaddr_ipx *sipx = (struct sockaddr_ipx *)sa;
984263bc
MD
1123 struct sockaddr_ipx work;
1124 static union ipx_net ipx_zeronet;
1125 char *p;
1126 struct ipx_addr in;
1127
1128 work = *sipx;
1129 in = work.sipx_addr;
1130
1131 work.sipx_addr.x_port = 0;
1132 work.sipx_addr.x_net = ipx_zeronet;
1133 p = ipx_print((struct sockaddr *)&work);
1134 if (strncmp("*.", p, 2) == 0) p += 2;
1135
1136 return(p);
1137}
1138
1139#ifdef NS
1140short ns_nullh[] = {0,0,0};
1141short ns_bh[] = {-1,-1,-1};
1142
1143char *
1144ns_print(struct sockaddr *sa)
1145{
6bd457ed 1146 struct sockaddr_ns *sns = (struct sockaddr_ns*)sa;
984263bc
MD
1147 struct ns_addr work;
1148 union { union ns_net net_e; u_long long_e; } net;
1149 u_short port;
1150 static char mybuf[50], cport[10], chost[25];
1151 char *host = "";
6bd457ed 1152 char *p; u_char *q;
984263bc
MD
1153
1154 work = sns->sns_addr;
1155 port = ntohs(work.x_port);
1156 work.x_port = 0;
1157 net.net_e = work.x_net;
1158 if (ns_nullhost(work) && net.long_e == 0) {
1159 if (port ) {
1160 sprintf(mybuf, "*.%xH", port);
1161 upHex(mybuf);
1162 } else
1163 sprintf(mybuf, "*.*");
1164 return (mybuf);
1165 }
1166
1167 if (bcmp(ns_bh, work.x_host.c_host, 6) == 0) {
1168 host = "any";
1169 } else if (bcmp(ns_nullh, work.x_host.c_host, 6) == 0) {
1170 host = "*";
1171 } else {
1172 q = work.x_host.c_host;
1173 sprintf(chost, "%02x%02x%02x%02x%02x%02xH",
1174 q[0], q[1], q[2], q[3], q[4], q[5]);
1175 for (p = chost; *p == '0' && p < chost + 12; p++)
1176 continue;
1177 host = p;
1178 }
1179 if (port)
1180 sprintf(cport, ".%xH", htons(port));
1181 else
1182 *cport = 0;
1183
1184 sprintf(mybuf,"%xH.%s%s", ntohl(net.long_e), host, cport);
1185 upHex(mybuf);
1186 return(mybuf);
1187}
1188
1189char *
1190ns_phost(struct sockaddr *sa)
1191{
6bd457ed 1192 struct sockaddr_ns *sns = (struct sockaddr_ns *)sa;
984263bc
MD
1193 struct sockaddr_ns work;
1194 static union ns_net ns_zeronet;
1195 char *p;
1196
1197 work = *sns;
1198 work.sns_addr.x_port = 0;
1199 work.sns_addr.x_net = ns_zeronet;
1200
1201 p = ns_print((struct sockaddr *)&work);
1202 if (strncmp("0H.", p, 3) == 0)
1203 p += 3;
1204 return(p);
1205}
1206#endif
1207
1208void
1209upHex(char *p0)
1210{
6bd457ed 1211 char *p = p0;
984263bc
MD
1212
1213 for (; *p; p++)
1214 switch (*p) {
1215
1216 case 'a':
1217 case 'b':
1218 case 'c':
1219 case 'd':
1220 case 'e':
1221 case 'f':
1222 *p += ('A' - 'a');
1223 break;
1224 }
1225}
9b42cabe
NA
1226
1227static const char *
1228labelops(struct rtentry *rt)
1229{
1230 char *lops[] = { "push", "pop", "swap", "pop all" };
1231 static char buffer[100];
1232 char *cp = buffer;
1233 struct sockaddr_mpls *smpls;
1234 int i;
1235
1236 for (i=0; i<MPLS_MAXLOPS; ++i) {
1237
1238 if (rt->rt_shim[i] == NULL)
1239 break;
1240 if (i>0) {
1241 cp += snprintf(cp,
1242 sizeof(buffer) - (cp - buffer),
1243 ", ");
1244 }
1245 smpls = (struct sockaddr_mpls *)kgetsa(rt->rt_shim[i]);
1246 if (smpls->smpls_op != MPLSLOP_POP &&
1247 smpls->smpls_op != MPLSLOP_POPALL){
1248 cp += snprintf(cp,
1249 sizeof(buffer) - (cp - buffer),
1250 "%s %d",
1251 lops[smpls->smpls_op - 1],
1252 ntohl(smpls->smpls_label));
1253 } else {
1254 cp += snprintf(cp,
1255 sizeof(buffer) - (cp - buffer),
1256 "%s",
1257 lops[smpls->smpls_op - 1]);
1258 }
1259 }
1260
1261 return (buffer);
1262}