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