rt_metrics: Add rmx_iw* to record user-set IW parameters
[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 $
984263bc
MD
35 */
36
e0ecab34 37#include <sys/kinfo.h>
984263bc 38#include <sys/param.h>
984263bc
MD
39#include <sys/socket.h>
40#include <sys/time.h>
41
42#include <net/ethernet.h>
43#include <net/if.h>
44#include <net/if_var.h>
45#include <net/if_dl.h>
46#include <net/if_types.h>
47#include <net/route.h>
48
49#include <netinet/in.h>
50#include <netipx/ipx.h>
38a690d7 51#include <netgraph/socket/ng_socket.h>
984263bc 52
9b42cabe
NA
53#include <netproto/mpls/mpls.h>
54
984263bc
MD
55#include <sys/sysctl.h>
56
57#include <arpa/inet.h>
58#include <libutil.h>
59#include <netdb.h>
60#include <stdio.h>
61#include <stdlib.h>
62#include <string.h>
63#include <unistd.h>
64#include <err.h>
65#include <time.h>
72ce0f6d 66#include <kinfo.h>
984263bc
MD
67#include "netstat.h"
68
69#define kget(p, d) (kread((u_long)(p), (char *)&(d), sizeof (d)))
70
71
72/* alignment constraint for routing socket */
73#define ROUNDUP(a) \
74 ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
75#define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len))
76
77/*
78 * Definitions for showing gateway flags.
79 */
80struct bits {
81 u_long b_mask;
82 char b_val;
83} bits[] = {
84 { RTF_UP, 'U' },
85 { RTF_GATEWAY, 'G' },
86 { RTF_HOST, 'H' },
87 { RTF_REJECT, 'R' },
88 { RTF_DYNAMIC, 'D' },
89 { RTF_MODIFIED, 'M' },
90 { RTF_DONE, 'd' }, /* Completed -- for routing messages only */
91 { RTF_CLONING, 'C' },
92 { RTF_XRESOLVE, 'X' },
93 { RTF_LLINFO, 'L' },
94 { RTF_STATIC, 'S' },
95 { RTF_PROTO1, '1' },
96 { RTF_PROTO2, '2' },
97 { RTF_WASCLONED,'W' },
98 { RTF_PRCLONING,'c' },
99 { RTF_PROTO3, '3' },
100 { RTF_BLACKHOLE,'B' },
101 { RTF_BROADCAST,'b' },
9b42cabe 102 { RTF_MPLSOPS, 'm' },
b82e1528 103 { 0, 0 }
984263bc
MD
104};
105
106typedef union {
107 long dummy; /* Helps align structure. */
108 struct sockaddr u_sa;
109 u_short u_data[128];
110} sa_u;
111
112static sa_u pt_u;
113
114int do_rtent = 0;
115struct rtentry rtentry;
116struct radix_node rnode;
117struct radix_mask rmask;
118struct radix_node_head *rt_tables[AF_MAX+1];
119
120int NewTree = 0;
121
122static struct sockaddr *kgetsa (struct sockaddr *);
123static void size_cols (int ef, struct radix_node *rn);
124static void size_cols_tree (struct radix_node *rn);
125static void size_cols_rtentry (struct rtentry *rt);
126static void p_tree (struct radix_node *);
127static void p_rtnode (void);
128static void ntreestuff (void);
129static void np_rtentry (struct rt_msghdr *);
130static void p_sockaddr (struct sockaddr *, struct sockaddr *, int, int);
131static const char *fmt_sockaddr (struct sockaddr *sa, struct sockaddr *mask,
132 int flags);
b82e1528 133static void p_flags (int, const char *);
984263bc
MD
134static const char *fmt_flags(int f);
135static void p_rtentry (struct rtentry *);
136static u_long forgemask (u_long);
137static void domask (char *, u_long, u_long);
9b42cabe 138static const char *labelops(struct rtentry *);
984263bc
MD
139
140/*
141 * Print routing tables.
142 */
143void
144routepr(u_long rtree)
145{
146 struct radix_node_head *rnh, head;
147 int i;
148
149 printf("Routing tables\n");
150
b06a5e2a 151 if (Aflag == 0 && NewTree) {
984263bc 152 ntreestuff();
b06a5e2a 153 } else {
984263bc
MD
154 if (rtree == 0) {
155 printf("rt_tables: symbol not in namelist\n");
156 return;
157 }
b06a5e2a
MD
158 if (cpuflag >= 0) {
159 /*
160 * Severe hack.
161 */
162 rtree += cpuflag * (AF_MAX + 1) * sizeof(void *);
163 }
984263bc
MD
164 kget(rtree, rt_tables);
165 for (i = 0; i <= AF_MAX; i++) {
678e8cc6 166 if ((rnh = rt_tables[i]) == NULL)
984263bc
MD
167 continue;
168 kget(rnh, head);
169 if (i == AF_UNSPEC) {
170 if (Aflag && af == 0) {
171 printf("Netmasks:\n");
172 p_tree(head.rnh_treetop);
173 }
174 } else if (af == AF_UNSPEC || af == i) {
175 size_cols(i, head.rnh_treetop);
176 pr_family(i);
177 do_rtent = 1;
178 pr_rthdr(i);
179 p_tree(head.rnh_treetop);
180 }
181 }
182 }
183}
184
185/*
186 * Print address family header before a section of the routing table.
187 */
188void
b82e1528 189pr_family(int af1)
984263bc 190{
b82e1528 191 const char *afname;
984263bc 192
b82e1528 193 switch (af1) {
984263bc
MD
194 case AF_INET:
195 afname = "Internet";
196 break;
197#ifdef INET6
198 case AF_INET6:
199 afname = "Internet6";
200 break;
201#endif /*INET6*/
202 case AF_IPX:
203 afname = "IPX";
204 break;
984263bc
MD
205 case AF_ISO:
206 afname = "ISO";
207 break;
984263bc
MD
208 case AF_CCITT:
209 afname = "X.25";
210 break;
211 case AF_NETGRAPH:
212 afname = "Netgraph";
213 break;
9b42cabe
NA
214 case AF_MPLS:
215 afname = "MPLS";
216 break;
984263bc
MD
217 default:
218 afname = NULL;
219 break;
220 }
221 if (afname)
222 printf("\n%s:\n", afname);
223 else
b82e1528 224 printf("\nProtocol Family %d:\n", af1);
984263bc
MD
225}
226
227/* column widths; each followed by one space */
228#ifndef INET6
229#define WID_DST_DEFAULT(af) 18 /* width of destination column */
230#define WID_GW_DEFAULT(af) 18 /* width of gateway column */
35d7d0f9 231#define WID_IF_DEFAULT(af) (Wflag ? 8 : 6) /* width of netif column */
984263bc
MD
232#else
233#define WID_DST_DEFAULT(af) \
234 ((af) == AF_INET6 ? (numeric_addr ? 33: 18) : 18)
235#define WID_GW_DEFAULT(af) \
236 ((af) == AF_INET6 ? (numeric_addr ? 29 : 18) : 18)
35d7d0f9 237#define WID_IF_DEFAULT(af) ((af) == AF_INET6 ? 8 : (Wflag ? 8 :6))
984263bc
MD
238#endif /*INET6*/
239
240static int wid_dst;
241static int wid_gw;
242static int wid_flags;
243static int wid_refs;
244static int wid_use;
245static int wid_mtu;
246static int wid_if;
247static int wid_expire;
9b42cabe 248static int wid_mplslops;
8ec89e60 249static int wid_msl;
bf522a91
SZ
250static int wid_iwmax;
251static int wid_iw;
984263bc
MD
252
253static void
254size_cols(int ef, struct radix_node *rn)
255{
256 wid_dst = WID_DST_DEFAULT(ef);
257 wid_gw = WID_GW_DEFAULT(ef);
258 wid_flags = 6;
259 wid_refs = 6;
260 wid_use = 8;
261 wid_mtu = 6;
262 wid_if = WID_IF_DEFAULT(ef);
263 wid_expire = 6;
9b42cabe 264 wid_mplslops = 7;
8ec89e60 265 wid_msl = 7;
bf522a91
SZ
266 wid_iwmax = 5;
267 wid_iw = 2;
984263bc
MD
268
269 if (Wflag)
270 size_cols_tree(rn);
271}
272
273static void
274size_cols_tree(struct radix_node *rn)
275{
276again:
277 kget(rn, rnode);
278 if (rnode.rn_bit < 0) {
279 if ((rnode.rn_flags & RNF_ROOT) == 0) {
280 kget(rn, rtentry);
281 size_cols_rtentry(&rtentry);
282 }
283 if ((rn = rnode.rn_dupedkey))
284 goto again;
285 } else {
286 rn = rnode.rn_right;
287 size_cols_tree(rnode.rn_left);
288 size_cols_tree(rn);
289 }
290}
291
292static void
293size_cols_rtentry(struct rtentry *rt)
294{
295 static struct ifnet ifnet, *lastif;
296 struct rtentry parent;
297 static char buffer[100];
298 const char *bp;
299 struct sockaddr *sa;
300 sa_u addr, mask;
b82e1528 301 int len;
984263bc
MD
302
303 /*
304 * Don't print protocol-cloned routes unless -a.
305 */
306 if (rt->rt_flags & RTF_WASCLONED && !aflag) {
307 kget(rt->rt_parent, parent);
308 if (parent.rt_flags & RTF_PRCLONING)
309 return;
310 }
311
312 bzero(&addr, sizeof(addr));
313 if ((sa = kgetsa(rt_key(rt))))
314 bcopy(sa, &addr, sa->sa_len);
315 bzero(&mask, sizeof(mask));
316 if (rt_mask(rt) && (sa = kgetsa(rt_mask(rt))))
317 bcopy(sa, &mask, sa->sa_len);
318 bp = fmt_sockaddr(&addr.u_sa, &mask.u_sa, rt->rt_flags);
319 len = strlen(bp);
320 wid_dst = MAX(len, wid_dst);
321
322 bp = fmt_sockaddr(kgetsa(rt->rt_gateway), NULL, RTF_HOST);
323 len = strlen(bp);
324 wid_gw = MAX(len, wid_gw);
325
326 bp = fmt_flags(rt->rt_flags);
327 len = strlen(bp);
328 wid_flags = MAX(len, wid_flags);
329
330 if (addr.u_sa.sa_family == AF_INET || Wflag) {
331 len = snprintf(buffer, sizeof(buffer), "%ld", rt->rt_refcnt);
332 wid_refs = MAX(len, wid_refs);
333 len = snprintf(buffer, sizeof(buffer), "%lu", rt->rt_use);
334 wid_use = MAX(len, wid_use);
335 if (Wflag && rt->rt_rmx.rmx_mtu != 0) {
336 len = snprintf(buffer, sizeof(buffer),
337 "%lu", rt->rt_rmx.rmx_mtu);
338 wid_mtu = MAX(len, wid_mtu);
339 }
340 }
341 if (rt->rt_ifp) {
342 if (rt->rt_ifp != lastif) {
984263bc 343 kget(rt->rt_ifp, ifnet);
984263bc 344 lastif = rt->rt_ifp;
3e4a09e7 345 len = strlen(ifnet.if_xname);
984263bc
MD
346 wid_if = MAX(len, wid_if);
347 }
348 if (rt->rt_rmx.rmx_expire) {
349 time_t expire_time;
350
351 if ((expire_time =
352 rt->rt_rmx.rmx_expire - time(NULL)) > 0) {
353 snprintf(buffer, sizeof(buffer), "%d",
354 (int)expire_time);
355 wid_expire = MAX(len, wid_expire);
356 }
357 }
358 }
8ec89e60
SZ
359 if (Wflag) {
360 if (rt->rt_shim[0] != NULL) {
361 len = strlen(labelops(rt));
362 wid_mplslops = MAX(len, wid_mplslops);
363 }
364
365 if (rt->rt_rmx.rmx_msl) {
366 len = snprintf(buffer, sizeof(buffer),
367 "%lu", rt->rt_rmx.rmx_msl);
368 wid_msl = MAX(len, wid_msl);
369 }
bf522a91
SZ
370 if (rt->rt_rmx.rmx_iwmaxsegs) {
371 len = snprintf(buffer, sizeof(buffer),
372 "%lu", rt->rt_rmx.rmx_iwmaxsegs);
373 wid_iwmax = MAX(len, wid_iwmax);
374 }
375 if (rt->rt_rmx.rmx_iwcapsegs) {
376 len = snprintf(buffer, sizeof(buffer),
377 "%lu", rt->rt_rmx.rmx_iwcapsegs);
378 wid_iw = MAX(len, wid_iw);
379 }
8ec89e60 380 }
984263bc
MD
381}
382
383
384/*
385 * Print header for routing table columns.
386 */
387void
b82e1528 388pr_rthdr(int af1)
984263bc
MD
389{
390
391 if (Aflag)
392 printf("%-8.8s ","Address");
b82e1528 393 if (af1 == AF_INET || Wflag) {
984263bc 394 if (Wflag) {
bf522a91
SZ
395 printf("%-*.*s %-*.*s %-*.*s %*.*s %*.*s %*.*s %*.*s "
396 "%*s %-*s%*s %*s %*s\n",
984263bc
MD
397 wid_dst, wid_dst, "Destination",
398 wid_gw, wid_gw, "Gateway",
399 wid_flags, wid_flags, "Flags",
400 wid_refs, wid_refs, "Refs",
401 wid_use, wid_use, "Use",
402 wid_mtu, wid_mtu, "Mtu",
403 wid_if, wid_if, "Netif",
9b42cabe 404 wid_expire, "Expire",
8ec89e60 405 wid_mplslops, "Labelops",
bf522a91
SZ
406 wid_msl, "Msl",
407 wid_iwmax, "IWmax",
408 wid_iw, "IW");
984263bc
MD
409 } else {
410 printf("%-*.*s %-*.*s %-*.*s %*.*s %*.*s %*.*s %*s\n",
411 wid_dst, wid_dst, "Destination",
412 wid_gw, wid_gw, "Gateway",
413 wid_flags, wid_flags, "Flags",
414 wid_refs, wid_refs, "Refs",
415 wid_use, wid_use, "Use",
416 wid_if, wid_if, "Netif",
417 wid_expire, "Expire");
418 }
419 } else {
420 printf("%-*.*s %-*.*s %-*.*s %*.*s %*s\n",
421 wid_dst, wid_dst, "Destination",
422 wid_gw, wid_gw, "Gateway",
423 wid_flags, wid_flags, "Flags",
424 wid_if, wid_if, "Netif",
425 wid_expire, "Expire");
426 }
427}
428
429static struct sockaddr *
430kgetsa(struct sockaddr *dst)
431{
432
433 kget(dst, pt_u.u_sa);
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);
437}
438
439static void
440p_tree(struct radix_node *rn)
441{
442
443again:
444 kget(rn, rnode);
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 kget(rn, rtentry);
454 p_rtentry(&rtentry);
455 if (Aflag)
456 p_rtnode();
457 } else {
458 p_sockaddr(kgetsa((struct sockaddr *)rnode.rn_key),
459 NULL, 0, 44);
460 putchar('\n');
461 }
462 if ((rn = rnode.rn_dupedkey))
463 goto again;
464 } else {
465 if (Aflag && do_rtent) {
466 printf("%-8.8lx ", (u_long)rn);
467 p_rtnode();
468 }
469 rn = rnode.rn_right;
470 p_tree(rnode.rn_left);
471 p_tree(rn);
472 }
473}
474
475char nbuf[20];
476
477static void
478p_rtnode(void)
479{
480 struct radix_mask *rm = rnode.rn_mklist;
481
482 if (rnode.rn_bit < 0) {
483 if (rnode.rn_mask) {
484 printf("\t mask ");
485 p_sockaddr(kgetsa((struct sockaddr *)rnode.rn_mask),
486 NULL, 0, -1);
678e8cc6 487 } else if (rm == NULL)
984263bc
MD
488 return;
489 } else {
490 sprintf(nbuf, "(%d)", rnode.rn_bit);
491 printf("%6.6s %8.8lx : %8.8lx", nbuf, (u_long)rnode.rn_left, (u_long)rnode.rn_right);
492 }
493 while (rm) {
494 kget(rm, rmask);
495 sprintf(nbuf, " %d refs, ", rmask.rm_refs);
496 printf(" mk = %8.8lx {(%d),%s",
497 (u_long)rm, -1 - rmask.rm_bit, rmask.rm_refs ? nbuf : " ");
498 if (rmask.rm_flags & RNF_NORMAL) {
499 struct radix_node rnode_aux;
500 printf(" <normal>, ");
501 kget(rmask.rm_leaf, rnode_aux);
502 p_sockaddr(kgetsa((struct sockaddr *)rnode_aux.rn_mask),
503 NULL, 0, -1);
504 } else
505 p_sockaddr(kgetsa((struct sockaddr *)rmask.rm_mask),
506 NULL, 0, -1);
507 putchar('}');
6bf4ea91 508 if ((rm = rmask.rm_next))
984263bc
MD
509 printf(" ->");
510 }
511 putchar('\n');
512}
513
514static void
515ntreestuff(void)
516{
517 size_t needed;
b06a5e2a
MD
518 int mib[7];
519 int miblen;
984263bc 520 char *buf, *next, *lim;
6bd457ed 521 struct rt_msghdr *rtm;
984263bc
MD
522
523 mib[0] = CTL_NET;
524 mib[1] = PF_ROUTE;
525 mib[2] = 0;
526 mib[3] = 0;
527 mib[4] = NET_RT_DUMP;
528 mib[5] = 0;
b06a5e2a
MD
529 if (cpuflag >= 0) {
530 mib[6] = cpuflag;
531 miblen = 7;
532 } else {
533 miblen = 6;
534 }
535 if (sysctl(mib, miblen, NULL, &needed, NULL, 0) < 0) {
984263bc
MD
536 err(1, "sysctl: net.route.0.0.dump estimate");
537 }
538
678e8cc6 539 if ((buf = malloc(needed)) == NULL) {
984263bc
MD
540 err(2, "malloc(%lu)", (unsigned long)needed);
541 }
b06a5e2a 542 if (sysctl(mib, miblen, buf, &needed, NULL, 0) < 0) {
984263bc
MD
543 err(1, "sysctl: net.route.0.0.dump");
544 }
545 lim = buf + needed;
546 for (next = buf; next < lim; next += rtm->rtm_msglen) {
547 rtm = (struct rt_msghdr *)next;
548 np_rtentry(rtm);
549 }
550}
551
552static void
553np_rtentry(struct rt_msghdr *rtm)
554{
6bd457ed 555 struct sockaddr *sa = (struct sockaddr *)(rtm + 1);
984263bc
MD
556#ifdef notdef
557 static int masks_done, banner_printed;
558#endif
559 static int old_af;
b82e1528 560 int af1 = 0, interesting = RTF_UP | RTF_GATEWAY | RTF_HOST;
984263bc
MD
561
562#ifdef notdef
563 /* for the moment, netmasks are skipped over */
564 if (!banner_printed) {
565 printf("Netmasks:\n");
566 banner_printed = 1;
567 }
568 if (masks_done == 0) {
569 if (rtm->rtm_addrs != RTA_DST ) {
570 masks_done = 1;
b82e1528 571 af1 = sa->sa_family;
984263bc
MD
572 }
573 } else
574#endif
b82e1528
SW
575 af1 = sa->sa_family;
576 if (af1 != old_af) {
577 pr_family(af1);
578 old_af = af1;
984263bc
MD
579 }
580 if (rtm->rtm_addrs == RTA_DST)
581 p_sockaddr(sa, NULL, 0, 36);
582 else {
583 p_sockaddr(sa, NULL, rtm->rtm_flags, 16);
584 sa = (struct sockaddr *)(ROUNDUP(sa->sa_len) + (char *)sa);
585 p_sockaddr(sa, NULL, 0, 18);
586 }
587 p_flags(rtm->rtm_flags & interesting, "%-6.6s ");
588 putchar('\n');
589}
590
591static void
592p_sockaddr(struct sockaddr *sa, struct sockaddr *mask, int flags, int width)
593{
594 const char *cp;
595
596 cp = fmt_sockaddr(sa, mask, flags);
597
598 if (width < 0 )
599 printf("%s ", cp);
600 else {
601 if (numeric_addr)
602 printf("%-*s ", width, cp);
603 else
604 printf("%-*.*s ", width, width, cp);
605 }
606}
607
608static const char *
609fmt_sockaddr(struct sockaddr *sa, struct sockaddr *mask, int flags)
610{
611 static char workbuf[128];
b82e1528 612 const char *cp = workbuf;
984263bc
MD
613
614 switch(sa->sa_family) {
615 case AF_INET:
616 {
6bd457ed 617 struct sockaddr_in *sin = (struct sockaddr_in *)sa;
984263bc
MD
618
619 if ((sin->sin_addr.s_addr == INADDR_ANY) &&
620 mask &&
621 ntohl(((struct sockaddr_in *)mask)->sin_addr.s_addr)
622 ==0L)
623 cp = "default" ;
624 else if (flags & RTF_HOST)
625 cp = routename(sin->sin_addr.s_addr);
626 else if (mask)
627 cp = netname(sin->sin_addr.s_addr,
628 ntohl(((struct sockaddr_in *)mask)
629 ->sin_addr.s_addr));
630 else
631 cp = netname(sin->sin_addr.s_addr, 0L);
632 break;
633 }
634
635#ifdef INET6
636 case AF_INET6:
637 {
638 struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)sa;
639 struct in6_addr *in6 = &sa6->sin6_addr;
640
641 /*
642 * XXX: This is a special workaround for KAME kernels.
643 * sin6_scope_id field of SA should be set in the future.
644 */
645 if (IN6_IS_ADDR_LINKLOCAL(in6) ||
646 IN6_IS_ADDR_MC_LINKLOCAL(in6)) {
647 /* XXX: override is ok? */
648 sa6->sin6_scope_id = (u_int32_t)ntohs(*(u_short *)&in6->s6_addr[2]);
649 *(u_short *)&in6->s6_addr[2] = 0;
650 }
651
652 if (flags & RTF_HOST)
653 cp = routename6(sa6);
654 else if (mask)
655 cp = netname6(sa6,
656 &((struct sockaddr_in6 *)mask)->sin6_addr);
657 else {
658 cp = netname6(sa6, NULL);
659 }
660 break;
661 }
662#endif /*INET6*/
663
664 case AF_IPX:
665 {
666 struct ipx_addr work = ((struct sockaddr_ipx *)sa)->sipx_addr;
667 if (ipx_nullnet(satoipx_addr(work)))
668 cp = "default";
669 else
670 cp = ipx_print(sa);
671 break;
672 }
984263bc
MD
673 case AF_NETGRAPH:
674 {
675 printf("%s", ((struct sockaddr_ng *)sa)->sg_data);
676 break;
677 }
984263bc
MD
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:
dc655811 690 case IFT_CARP:
984263bc
MD
691 if (sdl->sdl_alen == ETHER_ADDR_LEN) {
692 cp = ether_ntoa((struct ether_addr *)
693 (sdl->sdl_data + sdl->sdl_nlen));
694 break;
695 }
696 /* FALLTHROUGH */
697 default:
698 cp = link_ntoa(sdl);
699 break;
700 }
701 break;
702 }
703
9b42cabe
NA
704 case AF_MPLS:
705 {
706 struct sockaddr_mpls *smpls = (struct sockaddr_mpls *)sa;
707
708 (void) sprintf(workbuf, "%d", ntohl(smpls->smpls_label));
709 break;
710 }
711
984263bc
MD
712 default:
713 {
6bd457ed 714 u_char *s = (u_char *)sa->sa_data, *slim;
b82e1528 715 char *cq, *cqlim;
984263bc 716
b82e1528 717 cq = workbuf;
984263bc 718 slim = sa->sa_len + (u_char *) sa;
b82e1528
SW
719 cqlim = cq + sizeof(workbuf) - 6;
720 cq += sprintf(cq, "(%d)", sa->sa_family);
721 while (s < slim && cq < cqlim) {
722 cq += sprintf(cq, " %02x", *s++);
984263bc 723 if (s < slim)
b82e1528 724 cq += sprintf(cq, "%02x", *s++);
984263bc
MD
725 }
726 cp = workbuf;
727 }
728 }
729
730 return (cp);
731}
732
733static void
b82e1528 734p_flags(int f, const char *format)
984263bc
MD
735{
736 printf(format, fmt_flags(f));
737}
738
739static const char *
740fmt_flags(int f)
741{
742 static char name[33];
743 char *flags;
744 struct bits *p = bits;
745
746 for (flags = name; p->b_mask; p++)
747 if (p->b_mask & f)
748 *flags++ = p->b_val;
749 *flags = '\0';
750 return (name);
751}
752
753static void
754p_rtentry(struct rtentry *rt)
755{
756 static struct ifnet ifnet, *lastif;
757 struct rtentry parent;
758 static char buffer[128];
759 static char prettyname[128];
760 struct sockaddr *sa;
761 sa_u addr, mask;
762
763 /*
764 * Don't print protocol-cloned routes unless -a.
765 */
766 if (rt->rt_flags & RTF_WASCLONED && !aflag) {
767 kget(rt->rt_parent, parent);
768 if (parent.rt_flags & RTF_PRCLONING)
769 return;
770 }
771
772 bzero(&addr, sizeof(addr));
773 if ((sa = kgetsa(rt_key(rt))))
774 bcopy(sa, &addr, sa->sa_len);
775 bzero(&mask, sizeof(mask));
776 if (rt_mask(rt) && (sa = kgetsa(rt_mask(rt))))
777 bcopy(sa, &mask, sa->sa_len);
778 p_sockaddr(&addr.u_sa, &mask.u_sa, rt->rt_flags, wid_dst);
779 p_sockaddr(kgetsa(rt->rt_gateway), NULL, RTF_HOST, wid_gw);
780 snprintf(buffer, sizeof(buffer), "%%-%d.%ds ", wid_flags, wid_flags);
781 p_flags(rt->rt_flags, buffer);
782 if (addr.u_sa.sa_family == AF_INET || Wflag) {
783 printf("%*ld %*lu ", wid_refs, rt->rt_refcnt,
784 wid_use, rt->rt_use);
785 if (Wflag) {
786 if (rt->rt_rmx.rmx_mtu != 0)
787 printf("%*lu ", wid_mtu, rt->rt_rmx.rmx_mtu);
788 else
789 printf("%*s ", wid_mtu, "");
790 }
791 }
792 if (rt->rt_ifp) {
793 if (rt->rt_ifp != lastif) {
794 kget(rt->rt_ifp, ifnet);
984263bc 795 lastif = rt->rt_ifp;
3e4a09e7 796 strlcpy(prettyname, ifnet.if_xname, sizeof(prettyname));
984263bc
MD
797 }
798 printf("%*.*s", wid_if, wid_if, prettyname);
799 if (rt->rt_rmx.rmx_expire) {
800 time_t expire_time;
801
802 if ((expire_time =
60233e58 803 rt->rt_rmx.rmx_expire - time(NULL)) > 0)
984263bc 804 printf(" %*d", wid_expire, (int)expire_time);
8ec89e60
SZ
805 else
806 printf("%*s ", wid_expire, "");
9b42cabe
NA
807 } else {
808 printf("%*s ", wid_expire, "");
984263bc
MD
809 }
810 if (rt->rt_nodes[0].rn_dupedkey)
811 printf(" =>");
812 }
9b42cabe
NA
813 if (Wflag) {
814 if (rt->rt_shim[0] != NULL)
815 printf(" %-*s", wid_mplslops, labelops(rt));
8ec89e60
SZ
816 else
817 printf(" %-*s", wid_mplslops, "");
818 if (rt->rt_rmx.rmx_msl != 0)
819 printf(" %*lu", wid_msl, rt->rt_rmx.rmx_msl);
820 else
bf522a91
SZ
821 printf("%*s ", wid_msl, "");
822 if (rt->rt_rmx.rmx_iwmaxsegs != 0)
823 printf(" %*lu", wid_iwmax, rt->rt_rmx.rmx_iwmaxsegs);
824 else
825 printf("%*s ", wid_iwmax, "");
826 if (rt->rt_rmx.rmx_iwcapsegs != 0)
827 printf(" %*lu", wid_iw, rt->rt_rmx.rmx_iwcapsegs);
828 else
829 printf("%*s ", wid_iw, "");
9b42cabe 830 }
984263bc
MD
831 putchar('\n');
832}
833
834char *
835routename(u_long in)
836{
6bd457ed 837 char *cp;
984263bc
MD
838 static char line[MAXHOSTNAMELEN];
839 struct hostent *hp;
840
678e8cc6 841 cp = NULL;
984263bc 842 if (!numeric_addr) {
15b85273 843 hp = gethostbyaddr(&in, sizeof (struct in_addr), AF_INET);
984263bc
MD
844 if (hp) {
845 cp = hp->h_name;
846 trimdomain(cp, strlen(cp));
847 }
848 }
849 if (cp) {
850 strncpy(line, cp, sizeof(line) - 1);
851 line[sizeof(line) - 1] = '\0';
852 } else {
853#define C(x) ((x) & 0xff)
854 in = ntohl(in);
855 sprintf(line, "%lu.%lu.%lu.%lu",
856 C(in >> 24), C(in >> 16), C(in >> 8), C(in));
857 }
858 return (line);
859}
860
861static u_long
862forgemask(u_long a)
863{
864 u_long m;
865
866 if (IN_CLASSA(a))
867 m = IN_CLASSA_NET;
868 else if (IN_CLASSB(a))
869 m = IN_CLASSB_NET;
870 else
871 m = IN_CLASSC_NET;
872 return (m);
873}
874
875static void
876domask(char *dst, u_long addr, u_long mask)
877{
6bd457ed 878 int b, i;
984263bc
MD
879
880 if (!mask || (forgemask(addr) == mask)) {
881 *dst = '\0';
882 return;
883 }
884 i = 0;
885 for (b = 0; b < 32; b++)
886 if (mask & (1 << b)) {
6bd457ed 887 int bb;
984263bc
MD
888
889 i = b;
890 for (bb = b+1; bb < 32; bb++)
891 if (!(mask & (1 << bb))) {
892 i = -1; /* noncontig */
893 break;
894 }
895 break;
896 }
897 if (i == -1)
898 sprintf(dst, "&0x%lx", mask);
899 else
900 sprintf(dst, "/%d", 32-i);
901}
902
903/*
904 * Return the name of the network whose address is given.
905 * The address is assumed to be that of a net or subnet, not a host.
906 */
907char *
908netname(u_long in, u_long mask)
909{
678e8cc6 910 char *cp = NULL;
984263bc 911 static char line[MAXHOSTNAMELEN];
678e8cc6 912 struct netent *np = NULL;
984263bc 913 u_long dmask;
6bd457ed 914 u_long i;
984263bc
MD
915
916#define NSHIFT(m) ( \
917 (m) == IN_CLASSA_NET ? IN_CLASSA_NSHIFT : \
918 (m) == IN_CLASSB_NET ? IN_CLASSB_NSHIFT : \
919 (m) == IN_CLASSC_NET ? IN_CLASSC_NSHIFT : \
920 0)
921
922 i = ntohl(in);
923 dmask = forgemask(i);
924 if (!numeric_addr && i) {
925 np = getnetbyaddr(i >> NSHIFT(mask), AF_INET);
926 if (np == NULL && mask == 0)
927 np = getnetbyaddr(i >> NSHIFT(dmask), AF_INET);
928 if (np != NULL) {
929 cp = np->n_name;
930 trimdomain(cp, strlen(cp));
931 }
932 }
933#undef NSHIFT
934 if (cp != NULL) {
935 strncpy(line, cp, sizeof(line) - 1);
936 line[sizeof(line) - 1] = '\0';
937 } else {
3d970e2c
SZ
938 if (mask <= IN_CLASSA_NET &&
939 (i & IN_CLASSA_HOST) == 0) {
984263bc 940 sprintf(line, "%lu", C(i >> 24));
3d970e2c
SZ
941 } else if (mask <= IN_CLASSB_NET &&
942 (i & IN_CLASSB_HOST) == 0) {
984263bc
MD
943 sprintf(line, "%lu.%lu",
944 C(i >> 24), C(i >> 16));
3d970e2c
SZ
945 } else if (mask <= IN_CLASSC_NET &&
946 (i & IN_CLASSC_HOST) == 0) {
984263bc
MD
947 sprintf(line, "%lu.%lu.%lu",
948 C(i >> 24), C(i >> 16), C(i >> 8));
3d970e2c 949 } else {
984263bc
MD
950 sprintf(line, "%lu.%lu.%lu.%lu",
951 C(i >> 24), C(i >> 16), C(i >> 8), C(i));
984263bc
MD
952 }
953 }
954 domask(line + strlen(line), i, mask);
955 return (line);
956}
957
958#ifdef INET6
b82e1528 959const char *
984263bc
MD
960netname6(struct sockaddr_in6 *sa6, struct in6_addr *mask)
961{
962 static char line[MAXHOSTNAMELEN];
963 u_char *p = (u_char *)mask;
964 u_char *lim;
965 int masklen, illegal = 0, flag = NI_WITHSCOPEID;
966
967 if (mask) {
968 for (masklen = 0, lim = p + 16; p < lim; p++) {
969 switch (*p) {
970 case 0xff:
971 masklen += 8;
972 break;
973 case 0xfe:
974 masklen += 7;
975 break;
976 case 0xfc:
977 masklen += 6;
978 break;
979 case 0xf8:
980 masklen += 5;
981 break;
982 case 0xf0:
983 masklen += 4;
984 break;
985 case 0xe0:
986 masklen += 3;
987 break;
988 case 0xc0:
989 masklen += 2;
990 break;
991 case 0x80:
992 masklen += 1;
993 break;
994 case 0x00:
995 break;
996 default:
997 illegal ++;
998 break;
999 }
1000 }
1001 if (illegal)
1002 fprintf(stderr, "illegal prefixlen\n");
1003 }
1004 else
1005 masklen = 128;
1006
1007 if (masklen == 0 && IN6_IS_ADDR_UNSPECIFIED(&sa6->sin6_addr))
1008 return("default");
1009
1010 if (numeric_addr)
1011 flag |= NI_NUMERICHOST;
1012 getnameinfo((struct sockaddr *)sa6, sa6->sin6_len, line, sizeof(line),
1013 NULL, 0, flag);
1014
1015 if (numeric_addr)
1016 sprintf(&line[strlen(line)], "/%d", masklen);
1017
1018 return line;
1019}
1020
1021char *
1022routename6(struct sockaddr_in6 *sa6)
1023{
1024 static char line[MAXHOSTNAMELEN];
1025 int flag = NI_WITHSCOPEID;
1026 /* use local variable for safety */
b82e1528 1027 struct sockaddr_in6 sa6_local;
984263bc 1028
b82e1528
SW
1029 sa6_local.sin6_family = AF_INET6;
1030 sa6_local.sin6_len = sizeof(sa6_local);
984263bc
MD
1031 sa6_local.sin6_addr = sa6->sin6_addr;
1032 sa6_local.sin6_scope_id = sa6->sin6_scope_id;
1033
1034 if (numeric_addr)
1035 flag |= NI_NUMERICHOST;
1036
1037 getnameinfo((struct sockaddr *)&sa6_local, sa6_local.sin6_len,
1038 line, sizeof(line), NULL, 0, flag);
1039
1040 return line;
1041}
1042#endif /*INET6*/
1043
1044/*
1045 * Print routing statistics
1046 */
1047void
72ce0f6d 1048rt_stats(void)
984263bc 1049{
72ce0f6d
HP
1050 struct rtstatistics rts;
1051 int error = 0;
984263bc 1052
72ce0f6d
HP
1053 error = kinfo_get_net_rtstatistics(&rts);
1054 if (error) {
1055 printf("routing: could not retrieve statistics\n");
984263bc
MD
1056 return;
1057 }
984263bc
MD
1058 printf("routing:\n");
1059
72ce0f6d
HP
1060#define p(f, m) if (rts.f || sflag <= 1) \
1061 printf(m, rts.f, plural(rts.f))
984263bc 1062
72ce0f6d
HP
1063 p(rts_badredirect, "\t%lu bad routing redirect%s\n");
1064 p(rts_dynamic, "\t%lu dynamically created route%s\n");
1065 p(rts_newgateway, "\t%lu new gateway%s due to redirects\n");
1066 p(rts_unreach, "\t%lu destination%s found unreachable\n");
1067 p(rts_wildcard, "\t%lu use%s of a wildcard route\n");
984263bc 1068#undef p
984263bc
MD
1069}
1070
1071char *
1072ipx_print(struct sockaddr *sa)
1073{
1074 u_short port;
678e8cc6 1075 struct servent *sp = NULL;
b82e1528 1076 const char *net = "", *host = "";
6bd457ed
DR
1077 char *p;
1078 u_char *q;
984263bc
MD
1079 struct ipx_addr work = ((struct sockaddr_ipx *)sa)->sipx_addr;
1080 static char mybuf[50];
1081 char cport[10], chost[15], cnet[15];
1082
1083 port = ntohs(work.x_port);
1084
1085 if (ipx_nullnet(work) && ipx_nullhost(work)) {
1086
1087 if (port) {
1088 if (sp)
1089 sprintf(mybuf, "*.%s", sp->s_name);
1090 else
1091 sprintf(mybuf, "*.%x", port);
1092 } else
1093 sprintf(mybuf, "*.*");
1094
1095 return (mybuf);
1096 }
1097
1098 if (ipx_wildnet(work))
1099 net = "any";
1100 else if (ipx_nullnet(work))
1101 net = "*";
1102 else {
1103 q = work.x_net.c_net;
1104 sprintf(cnet, "%02x%02x%02x%02x",
1105 q[0], q[1], q[2], q[3]);
1106 for (p = cnet; *p == '0' && p < cnet + 8; p++)
1107 continue;
1108 net = p;
1109 }
1110
1111 if (ipx_wildhost(work))
1112 host = "any";
1113 else if (ipx_nullhost(work))
1114 host = "*";
1115 else {
1116 q = work.x_host.c_host;
1117 sprintf(chost, "%02x%02x%02x%02x%02x%02x",
1118 q[0], q[1], q[2], q[3], q[4], q[5]);
1119 for (p = chost; *p == '0' && p < chost + 12; p++)
1120 continue;
1121 host = p;
1122 }
1123
1124 if (port) {
1125 if (strcmp(host, "*") == 0)
1126 host = "";
1127 if (sp)
1128 snprintf(cport, sizeof(cport),
1129 "%s%s", *host ? "." : "", sp->s_name);
1130 else
1131 snprintf(cport, sizeof(cport),
1132 "%s%x", *host ? "." : "", port);
1133 } else
1134 *cport = 0;
1135
1136 snprintf(mybuf, sizeof(mybuf), "%s.%s%s", net, host, cport);
1137 return(mybuf);
1138}
1139
1140char *
1141ipx_phost(struct sockaddr *sa)
1142{
6bd457ed 1143 struct sockaddr_ipx *sipx = (struct sockaddr_ipx *)sa;
984263bc
MD
1144 struct sockaddr_ipx work;
1145 static union ipx_net ipx_zeronet;
1146 char *p;
1147 struct ipx_addr in;
1148
1149 work = *sipx;
1150 in = work.sipx_addr;
1151
1152 work.sipx_addr.x_port = 0;
1153 work.sipx_addr.x_net = ipx_zeronet;
1154 p = ipx_print((struct sockaddr *)&work);
1155 if (strncmp("*.", p, 2) == 0) p += 2;
1156
1157 return(p);
1158}
1159
984263bc
MD
1160void
1161upHex(char *p0)
1162{
6bd457ed 1163 char *p = p0;
984263bc
MD
1164
1165 for (; *p; p++)
1166 switch (*p) {
1167
1168 case 'a':
1169 case 'b':
1170 case 'c':
1171 case 'd':
1172 case 'e':
1173 case 'f':
1174 *p += ('A' - 'a');
1175 break;
1176 }
1177}
9b42cabe
NA
1178
1179static const char *
1180labelops(struct rtentry *rt)
1181{
b82e1528 1182 const char *lops[] = { "push", "pop", "swap", "pop all" };
9b42cabe
NA
1183 static char buffer[100];
1184 char *cp = buffer;
1185 struct sockaddr_mpls *smpls;
1186 int i;
1187
1188 for (i=0; i<MPLS_MAXLOPS; ++i) {
1189
1190 if (rt->rt_shim[i] == NULL)
1191 break;
1192 if (i>0) {
1193 cp += snprintf(cp,
1194 sizeof(buffer) - (cp - buffer),
1195 ", ");
1196 }
1197 smpls = (struct sockaddr_mpls *)kgetsa(rt->rt_shim[i]);
1198 if (smpls->smpls_op != MPLSLOP_POP &&
1199 smpls->smpls_op != MPLSLOP_POPALL){
1200 cp += snprintf(cp,
1201 sizeof(buffer) - (cp - buffer),
1202 "%s %d",
1203 lops[smpls->smpls_op - 1],
1204 ntohl(smpls->smpls_label));
1205 } else {
1206 cp += snprintf(cp,
1207 sizeof(buffer) - (cp - buffer),
1208 "%s",
1209 lops[smpls->smpls_op - 1]);
1210 }
1211 }
1212
1213 return (buffer);
1214}