rt_metrics: Add rmx_iw* to record user-set IW parameters
[dragonfly.git] / usr.bin / netstat / route.c
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.
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 $
35  */
36
37 #include <sys/kinfo.h>
38 #include <sys/param.h>
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>
51 #include <netgraph/socket/ng_socket.h>
52
53 #include <netproto/mpls/mpls.h>
54
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>
66 #include <kinfo.h>
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  */
80 struct 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' },
102         { RTF_MPLSOPS,  'm' },
103         { 0, 0 }
104 };
105
106 typedef union {
107         long    dummy;          /* Helps align structure. */
108         struct  sockaddr u_sa;
109         u_short u_data[128];
110 } sa_u;
111
112 static sa_u pt_u;
113
114 int     do_rtent = 0;
115 struct  rtentry rtentry;
116 struct  radix_node rnode;
117 struct  radix_mask rmask;
118 struct  radix_node_head *rt_tables[AF_MAX+1];
119
120 int     NewTree = 0;
121
122 static struct sockaddr *kgetsa (struct sockaddr *);
123 static void size_cols (int ef, struct radix_node *rn);
124 static void size_cols_tree (struct radix_node *rn);
125 static void size_cols_rtentry (struct rtentry *rt);
126 static void p_tree (struct radix_node *);
127 static void p_rtnode (void);
128 static void ntreestuff (void);
129 static void np_rtentry (struct rt_msghdr *);
130 static void p_sockaddr (struct sockaddr *, struct sockaddr *, int, int);
131 static const char *fmt_sockaddr (struct sockaddr *sa, struct sockaddr *mask,
132                                  int flags);
133 static void p_flags (int, const char *);
134 static const char *fmt_flags(int f);
135 static void p_rtentry (struct rtentry *);
136 static u_long forgemask (u_long);
137 static void domask (char *, u_long, u_long);
138 static const char *labelops(struct rtentry *);
139
140 /*
141  * Print routing tables.
142  */
143 void
144 routepr(u_long rtree)
145 {
146         struct radix_node_head *rnh, head;
147         int i;
148
149         printf("Routing tables\n");
150
151         if (Aflag == 0 && NewTree) {
152                 ntreestuff();
153         } else {
154                 if (rtree == 0) {
155                         printf("rt_tables: symbol not in namelist\n");
156                         return;
157                 }
158                 if (cpuflag >= 0) {
159                         /*
160                          * Severe hack.
161                          */
162                         rtree += cpuflag * (AF_MAX + 1) * sizeof(void *);
163                 }
164                 kget(rtree, rt_tables);
165                 for (i = 0; i <= AF_MAX; i++) {
166                         if ((rnh = rt_tables[i]) == NULL)
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  */
188 void
189 pr_family(int af1)
190 {
191         const char *afname;
192
193         switch (af1) {
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;
205         case AF_ISO:
206                 afname = "ISO";
207                 break;
208         case AF_CCITT:
209                 afname = "X.25";
210                 break;
211         case AF_NETGRAPH:
212                 afname = "Netgraph";
213                 break;
214         case AF_MPLS:
215                 afname = "MPLS";
216                 break;
217         default:
218                 afname = NULL;
219                 break;
220         }
221         if (afname)
222                 printf("\n%s:\n", afname);
223         else
224                 printf("\nProtocol Family %d:\n", af1);
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 */
231 #define WID_IF_DEFAULT(af)      (Wflag ? 8 : 6) /* width of netif column */
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)
237 #define WID_IF_DEFAULT(af)      ((af) == AF_INET6 ? 8 : (Wflag ? 8 :6))
238 #endif /*INET6*/
239
240 static int wid_dst;
241 static int wid_gw;
242 static int wid_flags;
243 static int wid_refs;
244 static int wid_use;
245 static int wid_mtu;
246 static int wid_if;
247 static int wid_expire;
248 static int wid_mplslops;
249 static int wid_msl;
250 static int wid_iwmax;
251 static int wid_iw;
252
253 static void
254 size_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;
264         wid_mplslops = 7;
265         wid_msl = 7;
266         wid_iwmax = 5;
267         wid_iw = 2;
268
269         if (Wflag)
270                 size_cols_tree(rn);
271 }
272
273 static void
274 size_cols_tree(struct radix_node *rn)
275 {
276 again:
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
292 static void
293 size_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;
301         int len;
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) {
343                         kget(rt->rt_ifp, ifnet);
344                         lastif = rt->rt_ifp;
345                         len = strlen(ifnet.if_xname);
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         }
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                 }
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                 }
380         }
381 }
382
383
384 /*
385  * Print header for routing table columns.
386  */
387 void
388 pr_rthdr(int af1)
389 {
390
391         if (Aflag)
392                 printf("%-8.8s ","Address");
393         if (af1 == AF_INET || Wflag) {
394                 if (Wflag) {
395                         printf("%-*.*s %-*.*s %-*.*s %*.*s %*.*s %*.*s %*.*s "
396                                "%*s %-*s%*s %*s %*s\n",
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",
404                                 wid_expire,                     "Expire",
405                                 wid_mplslops,                   "Labelops",
406                                 wid_msl,                        "Msl",
407                                 wid_iwmax,                      "IWmax",
408                                 wid_iw,                         "IW");
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
429 static struct sockaddr *
430 kgetsa(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
439 static void
440 p_tree(struct radix_node *rn)
441 {
442
443 again:
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
475 char    nbuf[20];
476
477 static void
478 p_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);
487                 } else if (rm == NULL)
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('}');
508                 if ((rm = rmask.rm_next))
509                         printf(" ->");
510         }
511         putchar('\n');
512 }
513
514 static void
515 ntreestuff(void)
516 {
517         size_t needed;
518         int mib[7];
519         int miblen;
520         char *buf, *next, *lim;
521         struct rt_msghdr *rtm;
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;
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) {
536                 err(1, "sysctl: net.route.0.0.dump estimate");
537         }
538
539         if ((buf = malloc(needed)) == NULL) {
540                 err(2, "malloc(%lu)", (unsigned long)needed);
541         }
542         if (sysctl(mib, miblen, buf, &needed, NULL, 0) < 0) {
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
552 static void
553 np_rtentry(struct rt_msghdr *rtm)
554 {
555         struct sockaddr *sa = (struct sockaddr *)(rtm + 1);
556 #ifdef notdef
557         static int masks_done, banner_printed;
558 #endif
559         static int old_af;
560         int af1 = 0, interesting = RTF_UP | RTF_GATEWAY | RTF_HOST;
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;
571                         af1 = sa->sa_family;
572                 }
573         } else
574 #endif
575                 af1 = sa->sa_family;
576         if (af1 != old_af) {
577                 pr_family(af1);
578                 old_af = af1;
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
591 static void
592 p_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
608 static const char *
609 fmt_sockaddr(struct sockaddr *sa, struct sockaddr *mask, int flags)
610 {
611         static char workbuf[128];
612         const char *cp = workbuf;
613
614         switch(sa->sa_family) {
615         case AF_INET:
616             {
617                 struct sockaddr_in *sin = (struct sockaddr_in *)sa;
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             }
673         case AF_NETGRAPH:
674             {
675                 printf("%s", ((struct sockaddr_ng *)sa)->sg_data);
676                 break;
677             }
678         case AF_LINK:
679             {
680                 struct sockaddr_dl *sdl = (struct sockaddr_dl *)sa;
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:
689                         case IFT_L2VLAN:
690                         case IFT_CARP:
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
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                 
712         default:
713             {
714                 u_char *s = (u_char *)sa->sa_data, *slim;
715                 char *cq, *cqlim;
716
717                 cq = workbuf;
718                 slim =  sa->sa_len + (u_char *) sa;
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++);
723                         if (s < slim)
724                             cq += sprintf(cq, "%02x", *s++);
725                 }
726                 cp = workbuf;
727             }
728         }
729
730         return (cp);
731 }
732
733 static void
734 p_flags(int f, const char *format)
735 {
736         printf(format, fmt_flags(f));
737 }
738
739 static const char *
740 fmt_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
753 static void
754 p_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);
795                         lastif = rt->rt_ifp;
796                         strlcpy(prettyname, ifnet.if_xname, sizeof(prettyname));
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 =
803                             rt->rt_rmx.rmx_expire - time(NULL)) > 0)
804                                 printf(" %*d", wid_expire, (int)expire_time);
805                         else
806                                 printf("%*s ", wid_expire, "");
807                 } else {
808                         printf("%*s ", wid_expire, "");
809                 }
810                 if (rt->rt_nodes[0].rn_dupedkey)
811                         printf(" =>");
812         }
813         if (Wflag) {
814                 if (rt->rt_shim[0] != NULL)
815                         printf(" %-*s", wid_mplslops, labelops(rt));
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
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, "");
830         }
831         putchar('\n');
832 }
833
834 char *
835 routename(u_long in)
836 {
837         char *cp;
838         static char line[MAXHOSTNAMELEN];
839         struct hostent *hp;
840
841         cp = NULL;
842         if (!numeric_addr) {
843                 hp = gethostbyaddr(&in, sizeof (struct in_addr), AF_INET);
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
861 static u_long
862 forgemask(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
875 static void
876 domask(char *dst, u_long addr, u_long mask)
877 {
878         int b, i;
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)) {
887                         int bb;
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  */
907 char *
908 netname(u_long in, u_long mask)
909 {
910         char *cp = NULL;
911         static char line[MAXHOSTNAMELEN];
912         struct netent *np = NULL;
913         u_long dmask;
914         u_long i;
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 {
938                 if (mask <= IN_CLASSA_NET &&
939                         (i & IN_CLASSA_HOST) == 0) {
940                                 sprintf(line, "%lu", C(i >> 24));
941                 } else if (mask <= IN_CLASSB_NET &&
942                         (i & IN_CLASSB_HOST) == 0) {
943                                 sprintf(line, "%lu.%lu",
944                                         C(i >> 24), C(i >> 16));
945                 } else if (mask <= IN_CLASSC_NET &&
946                         (i & IN_CLASSC_HOST) == 0) {
947                                 sprintf(line, "%lu.%lu.%lu",
948                                         C(i >> 24), C(i >> 16), C(i >> 8));
949                 } else {
950                         sprintf(line, "%lu.%lu.%lu.%lu",
951                                 C(i >> 24), C(i >> 16), C(i >> 8), C(i));
952                 }
953         }
954         domask(line + strlen(line), i, mask);
955         return (line);
956 }
957
958 #ifdef INET6
959 const char *
960 netname6(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
1021 char *
1022 routename6(struct sockaddr_in6 *sa6)
1023 {
1024         static char line[MAXHOSTNAMELEN];
1025         int flag = NI_WITHSCOPEID;
1026         /* use local variable for safety */
1027         struct sockaddr_in6 sa6_local;
1028
1029         sa6_local.sin6_family = AF_INET6;
1030         sa6_local.sin6_len = sizeof(sa6_local);
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  */
1047 void
1048 rt_stats(void)
1049 {
1050         struct rtstatistics rts;
1051         int error = 0;
1052
1053         error = kinfo_get_net_rtstatistics(&rts);
1054         if (error) {
1055                 printf("routing: could not retrieve statistics\n");
1056                 return;
1057         }
1058         printf("routing:\n");
1059
1060 #define p(f, m) if (rts.f || sflag <= 1) \
1061         printf(m, rts.f, plural(rts.f))
1062
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");
1068 #undef p
1069 }
1070
1071 char *
1072 ipx_print(struct sockaddr *sa)
1073 {
1074         u_short port;
1075         struct servent *sp = NULL;
1076         const char *net = "", *host = "";
1077         char *p;
1078         u_char *q;
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
1140 char *
1141 ipx_phost(struct sockaddr *sa)
1142 {
1143         struct sockaddr_ipx *sipx = (struct sockaddr_ipx *)sa;
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
1160 void
1161 upHex(char *p0)
1162 {
1163         char *p = p0;
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 }
1178
1179 static const char *
1180 labelops(struct rtentry *rt)
1181 {
1182         const char *lops[] = { "push", "pop", "swap", "pop all" };
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 }