2 * Copyright (c) 1983, 1988, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
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 $
33 #include <sys/kinfo.h>
34 #include <sys/param.h>
35 #include <sys/socket.h>
38 #include <net/ethernet.h>
40 #include <net/if_var.h>
41 #include <net/if_dl.h>
42 #include <net/if_types.h>
43 #include <net/route.h>
45 #include <netinet/in.h>
46 #include <netgraph/socket/ng_socket.h>
48 #include <netproto/mpls/mpls.h>
50 #include <sys/sysctl.h>
52 #include <arpa/inet.h>
64 #define kget(p, d) (kread((u_long)(p), (char *)&(d), sizeof (d)))
67 * Definitions for showing gateway flags.
78 { RTF_MODIFIED
, 'M' },
79 { RTF_DONE
, 'd' }, /* Completed -- for routing messages only */
81 { RTF_XRESOLVE
, 'X' },
86 { RTF_WASCLONED
,'W' },
87 { RTF_PRCLONING
,'c' },
89 { RTF_BLACKHOLE
,'B' },
90 { RTF_BROADCAST
,'b' },
96 long dummy
; /* Helps align structure. */
104 struct rtentry rtentry
;
105 struct radix_node rnode
;
106 struct radix_mask rmask
;
107 struct radix_node_head
*rt_tables
[AF_MAX
+1];
111 static struct sockaddr
*kgetsa (struct sockaddr
*);
112 static void size_cols (int ef
, struct radix_node
*rn
);
113 static void size_cols_tree (struct radix_node
*rn
);
114 static void size_cols_rtentry (struct rtentry
*rt
);
115 static void p_tree (struct radix_node
*);
116 static void p_rtnode (void);
117 static void ntreestuff (void);
118 static void np_rtentry (struct rt_msghdr
*);
119 static void p_sockaddr (struct sockaddr
*, struct sockaddr
*, int, int);
120 static const char *fmt_sockaddr (struct sockaddr
*sa
, struct sockaddr
*mask
,
122 static void p_flags (int, const char *);
123 static const char *fmt_flags(int f
);
124 static void p_rtentry (struct rtentry
*);
125 static u_long
forgemask (u_long
);
126 static void domask (char *, u_long
, u_long
);
127 static const char *labelops(struct rtentry
*);
130 * Print routing tables.
133 routepr(u_long rtree
)
135 struct radix_node_head
*rnh
, head
;
138 printf("Routing tables\n");
140 if (Aflag
== 0 && NewTree
) {
144 printf("rt_tables: symbol not in namelist\n");
151 rtree
+= cpuflag
* (AF_MAX
+ 1) * sizeof(void *);
153 if (kget(rtree
, rt_tables
) != 0)
155 for (i
= 0; i
<= AF_MAX
; i
++) {
156 if ((rnh
= rt_tables
[i
]) == NULL
)
158 if (kget(rnh
, head
) != 0)
160 if (i
== AF_UNSPEC
) {
161 if (Aflag
&& af
== 0) {
162 printf("Netmasks:\n");
163 p_tree(head
.rnh_treetop
);
165 } else if (af
== AF_UNSPEC
|| af
== i
) {
166 size_cols(i
, head
.rnh_treetop
);
170 p_tree(head
.rnh_treetop
);
177 * Print address family header before a section of the routing table.
190 afname
= "Internet6";
210 printf("\n%s:\n", afname
);
212 printf("\nProtocol Family %d:\n", af1
);
215 /* column widths; each followed by one space */
217 #define WID_DST_DEFAULT(af) 18 /* width of destination column */
218 #define WID_GW_DEFAULT(af) 18 /* width of gateway column */
219 #define WID_IF_DEFAULT(af) (Wflag ? 8 : 6) /* width of netif column */
221 #define WID_DST_DEFAULT(af) \
222 ((af) == AF_INET6 ? (numeric_addr ? 33: 18) : 18)
223 #define WID_GW_DEFAULT(af) \
224 ((af) == AF_INET6 ? (numeric_addr ? 29 : 18) : 18)
225 #define WID_IF_DEFAULT(af) ((af) == AF_INET6 ? 8 : (Wflag ? 8 :6))
230 static int wid_flags
;
235 static int wid_expire
;
236 static int wid_mplslops
;
238 static int wid_iwmax
;
242 size_cols(int ef
, struct radix_node
*rn
)
244 wid_dst
= WID_DST_DEFAULT(ef
);
245 wid_gw
= WID_GW_DEFAULT(ef
);
250 wid_if
= WID_IF_DEFAULT(ef
);
262 size_cols_tree(struct radix_node
*rn
)
265 if (kget(rn
, rnode
) != 0)
267 if (!(rnode
.rn_flags
& RNF_ACTIVE
))
269 if (rnode
.rn_bit
< 0) {
270 if ((rnode
.rn_flags
& RNF_ROOT
) == 0) {
271 if (kget(rn
, rtentry
) != 0)
273 size_cols_rtentry(&rtentry
);
275 if ((rn
= rnode
.rn_dupedkey
))
279 size_cols_tree(rnode
.rn_left
);
285 size_cols_rtentry(struct rtentry
*rt
)
287 static struct ifnet ifnet
, *lastif
;
288 struct rtentry parent
;
289 static char buffer
[100];
296 * Don't print protocol-cloned routes unless -a.
298 if (rt
->rt_flags
& RTF_WASCLONED
&& !aflag
) {
299 if (kget(rt
->rt_parent
, parent
) != 0)
301 if (parent
.rt_flags
& RTF_PRCLONING
)
305 bzero(&addr
, sizeof(addr
));
306 if ((sa
= kgetsa(rt_key(rt
))))
307 bcopy(sa
, &addr
, sa
->sa_len
);
308 bzero(&mask
, sizeof(mask
));
309 if (rt_mask(rt
) && (sa
= kgetsa(rt_mask(rt
))))
310 bcopy(sa
, &mask
, sa
->sa_len
);
311 bp
= fmt_sockaddr(&addr
.u_sa
, &mask
.u_sa
, rt
->rt_flags
);
313 wid_dst
= MAX(len
, wid_dst
);
315 bp
= fmt_sockaddr(kgetsa(rt
->rt_gateway
), NULL
, RTF_HOST
);
317 wid_gw
= MAX(len
, wid_gw
);
319 bp
= fmt_flags(rt
->rt_flags
);
321 wid_flags
= MAX(len
, wid_flags
);
323 if (addr
.u_sa
.sa_family
== AF_INET
|| Wflag
) {
324 len
= snprintf(buffer
, sizeof(buffer
), "%ld", rt
->rt_refcnt
);
325 wid_refs
= MAX(len
, wid_refs
);
326 len
= snprintf(buffer
, sizeof(buffer
), "%lu", rt
->rt_use
);
327 wid_use
= MAX(len
, wid_use
);
328 if (Wflag
&& rt
->rt_rmx
.rmx_mtu
!= 0) {
329 len
= snprintf(buffer
, sizeof(buffer
),
330 "%lu", rt
->rt_rmx
.rmx_mtu
);
331 wid_mtu
= MAX(len
, wid_mtu
);
335 if (rt
->rt_ifp
!= lastif
) {
336 if (kget(rt
->rt_ifp
, ifnet
) == 0)
337 len
= strlen(ifnet
.if_xname
);
341 wid_if
= MAX(len
, wid_if
);
343 if (rt
->rt_rmx
.rmx_expire
) {
347 clock_gettime(CLOCK_MONOTONIC
, &sp
);
349 expire_time
= (int)(rt
->rt_rmx
.rmx_expire
- sp
.tv_sec
);
351 if (expire_time
> 0) {
352 snprintf(buffer
, sizeof(buffer
), "%d",
354 wid_expire
= MAX(len
, wid_expire
);
359 if (rt
->rt_shim
[0] != NULL
) {
360 len
= strlen(labelops(rt
));
361 wid_mplslops
= MAX(len
, wid_mplslops
);
364 if (rt
->rt_rmx
.rmx_msl
) {
365 len
= snprintf(buffer
, sizeof(buffer
),
366 "%lu", rt
->rt_rmx
.rmx_msl
);
367 wid_msl
= MAX(len
, wid_msl
);
369 if (rt
->rt_rmx
.rmx_iwmaxsegs
) {
370 len
= snprintf(buffer
, sizeof(buffer
),
371 "%lu", rt
->rt_rmx
.rmx_iwmaxsegs
);
372 wid_iwmax
= MAX(len
, wid_iwmax
);
374 if (rt
->rt_rmx
.rmx_iwcapsegs
) {
375 len
= snprintf(buffer
, sizeof(buffer
),
376 "%lu", rt
->rt_rmx
.rmx_iwcapsegs
);
377 wid_iw
= MAX(len
, wid_iw
);
384 * Print header for routing table columns.
391 printf("%-8.8s ","Address");
392 if (af1
== AF_INET
|| Wflag
) {
394 printf("%-*.*s %-*.*s %-*.*s %*.*s %*.*s %*.*s %*.*s "
395 "%*s %-*s%*s %*s %*s\n",
396 wid_dst
, wid_dst
, "Destination",
397 wid_gw
, wid_gw
, "Gateway",
398 wid_flags
, wid_flags
, "Flags",
399 wid_refs
, wid_refs
, "Refs",
400 wid_use
, wid_use
, "Use",
401 wid_mtu
, wid_mtu
, "Mtu",
402 wid_if
, wid_if
, "Netif",
403 wid_expire
, "Expire",
404 wid_mplslops
, "Labelops",
409 printf("%-*.*s %-*.*s %-*.*s %*.*s %*.*s %*.*s %*s\n",
410 wid_dst
, wid_dst
, "Destination",
411 wid_gw
, wid_gw
, "Gateway",
412 wid_flags
, wid_flags
, "Flags",
413 wid_refs
, wid_refs
, "Refs",
414 wid_use
, wid_use
, "Use",
415 wid_if
, wid_if
, "Netif",
416 wid_expire
, "Expire");
419 printf("%-*.*s %-*.*s %-*.*s %*.*s %*s\n",
420 wid_dst
, wid_dst
, "Destination",
421 wid_gw
, wid_gw
, "Gateway",
422 wid_flags
, wid_flags
, "Flags",
423 wid_if
, wid_if
, "Netif",
424 wid_expire
, "Expire");
428 static struct sockaddr
*
429 kgetsa(struct sockaddr
*dst
)
432 if (kget(dst
, pt_u
.u_sa
) != 0)
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
);
440 p_tree(struct radix_node
*rn
)
444 if (kget(rn
, rnode
) != 0)
446 if (!(rnode
.rn_flags
& RNF_ACTIVE
))
448 if (rnode
.rn_bit
< 0) {
450 printf("%-8.8lx ", (u_long
)rn
);
451 if (rnode
.rn_flags
& RNF_ROOT
) {
453 printf("(root node)%s",
454 rnode
.rn_dupedkey
? " =>\n" : "\n");
455 } else if (do_rtent
) {
456 if (kget(rn
, rtentry
) == 0) {
462 p_sockaddr(kgetsa((struct sockaddr
*)rnode
.rn_key
),
466 if ((rn
= rnode
.rn_dupedkey
))
469 if (Aflag
&& do_rtent
) {
470 printf("%-8.8lx ", (u_long
)rn
);
474 p_tree(rnode
.rn_left
);
484 struct radix_mask
*rm
= rnode
.rn_mklist
;
486 if (rnode
.rn_bit
< 0) {
489 p_sockaddr(kgetsa((struct sockaddr
*)rnode
.rn_mask
),
491 } else if (rm
== NULL
)
494 sprintf(nbuf
, "(%d)", rnode
.rn_bit
);
495 printf("%6.6s %8.8lx : %8.8lx", nbuf
, (u_long
)rnode
.rn_left
, (u_long
)rnode
.rn_right
);
498 if (kget(rm
, rmask
) != 0)
500 sprintf(nbuf
, " %d refs, ", rmask
.rm_refs
);
501 printf(" mk = %8.8lx {(%d),%s",
502 (u_long
)rm
, -1 - rmask
.rm_bit
, rmask
.rm_refs
? nbuf
: " ");
503 if (rmask
.rm_flags
& RNF_NORMAL
) {
504 struct radix_node rnode_aux
;
505 printf(" <normal>, ");
506 if (kget(rmask
.rm_leaf
, rnode_aux
) == 0)
507 p_sockaddr(kgetsa((struct sockaddr
*)rnode_aux
.rn_mask
),
510 p_sockaddr(NULL
, NULL
, 0, -1);
512 p_sockaddr(kgetsa((struct sockaddr
*)rmask
.rm_mask
),
515 if ((rm
= rmask
.rm_next
))
527 char *buf
, *next
, *lim
;
528 struct rt_msghdr
*rtm
;
534 mib
[4] = NET_RT_DUMP
;
542 if (sysctl(mib
, miblen
, NULL
, &needed
, NULL
, 0) < 0) {
543 err(1, "sysctl: net.route.0.0.dump estimate");
546 if ((buf
= malloc(needed
)) == NULL
) {
547 err(2, "malloc(%lu)", (unsigned long)needed
);
549 if (sysctl(mib
, miblen
, buf
, &needed
, NULL
, 0) < 0) {
550 err(1, "sysctl: net.route.0.0.dump");
553 for (next
= buf
; next
< lim
; next
+= rtm
->rtm_msglen
) {
554 rtm
= (struct rt_msghdr
*)next
;
560 np_rtentry(struct rt_msghdr
*rtm
)
562 struct sockaddr
*sa
= (struct sockaddr
*)(rtm
+ 1);
564 static int masks_done
, banner_printed
;
567 int af1
= 0, interesting
= RTF_UP
| RTF_GATEWAY
| RTF_HOST
;
570 /* for the moment, netmasks are skipped over */
571 if (!banner_printed
) {
572 printf("Netmasks:\n");
575 if (masks_done
== 0) {
576 if (rtm
->rtm_addrs
!= RTA_DST
) {
587 if (rtm
->rtm_addrs
== RTA_DST
)
588 p_sockaddr(sa
, NULL
, 0, 36);
590 p_sockaddr(sa
, NULL
, rtm
->rtm_flags
, 16);
591 sa
= (struct sockaddr
*)(RT_ROUNDUP(sa
->sa_len
) + (char *)sa
);
592 p_sockaddr(sa
, NULL
, 0, 18);
594 p_flags(rtm
->rtm_flags
& interesting
, "%-6.6s ");
599 p_sockaddr(struct sockaddr
*sa
, struct sockaddr
*mask
, int flags
, int width
)
603 cp
= fmt_sockaddr(sa
, mask
, flags
);
609 printf("%-*s ", width
, cp
);
611 printf("%-*.*s ", width
, width
, cp
);
616 fmt_sockaddr(struct sockaddr
*sa
, struct sockaddr
*mask
, int flags
)
618 static char workbuf
[128];
619 const char *cp
= workbuf
;
624 switch(sa
->sa_family
) {
627 struct sockaddr_in
*sin
= (struct sockaddr_in
*)sa
;
629 if ((sin
->sin_addr
.s_addr
== INADDR_ANY
) &&
631 ntohl(((struct sockaddr_in
*)mask
)->sin_addr
.s_addr
)
634 else if (flags
& RTF_HOST
)
635 cp
= routename(sin
->sin_addr
.s_addr
);
637 cp
= netname(sin
->sin_addr
.s_addr
,
638 ntohl(((struct sockaddr_in
*)mask
)
641 cp
= netname(sin
->sin_addr
.s_addr
, 0L);
648 struct sockaddr_in6
*sa6
= (struct sockaddr_in6
*)sa
;
649 struct in6_addr
*in6
= &sa6
->sin6_addr
;
652 * XXX: This is a special workaround for KAME kernels.
653 * sin6_scope_id field of SA should be set in the future.
655 if (IN6_IS_ADDR_LINKLOCAL(in6
) ||
656 IN6_IS_ADDR_MC_LINKLOCAL(in6
)) {
657 /* XXX: override is ok? */
658 sa6
->sin6_scope_id
= (u_int32_t
)ntohs(*(u_short
*)&in6
->s6_addr
[2]);
659 *(u_short
*)&in6
->s6_addr
[2] = 0;
662 if (flags
& RTF_HOST
)
663 cp
= routename6(sa6
);
666 &((struct sockaddr_in6
*)mask
)->sin6_addr
);
668 cp
= netname6(sa6
, NULL
);
676 printf("%s", ((struct sockaddr_ng
*)sa
)->sg_data
);
681 struct sockaddr_dl
*sdl
= (struct sockaddr_dl
*)sa
;
683 if (sdl
->sdl_nlen
== 0 && sdl
->sdl_alen
== 0 &&
685 (void) sprintf(workbuf
, "link#%d", sdl
->sdl_index
);
687 switch (sdl
->sdl_type
) {
692 if (sdl
->sdl_alen
== ETHER_ADDR_LEN
) {
693 cp
= ether_ntoa((struct ether_addr
*)
694 (sdl
->sdl_data
+ sdl
->sdl_nlen
));
707 struct sockaddr_mpls
*smpls
= (struct sockaddr_mpls
*)sa
;
709 (void) sprintf(workbuf
, "%d", ntohl(smpls
->smpls_label
));
715 u_char
*s
= (u_char
*)sa
->sa_data
, *slim
;
719 slim
= sa
->sa_len
+ (u_char
*) sa
;
720 cqlim
= cq
+ sizeof(workbuf
) - 6;
721 cq
+= sprintf(cq
, "(%d)", sa
->sa_family
);
722 while (s
< slim
&& cq
< cqlim
) {
723 cq
+= sprintf(cq
, " %02x", *s
++);
725 cq
+= sprintf(cq
, "%02x", *s
++);
735 p_flags(int f
, const char *format
)
737 printf(format
, fmt_flags(f
));
743 static char name
[33];
745 struct bits
*p
= bits
;
747 for (flags
= name
; p
->b_mask
; p
++)
755 p_rtentry(struct rtentry
*rt
)
757 static struct ifnet ifnet
, *lastif
;
758 struct rtentry parent
;
759 static char buffer
[128];
760 static char prettyname
[128];
765 * Don't print protocol-cloned routes unless -a or -A.
767 if (rt
->rt_flags
& RTF_WASCLONED
&& !(aflag
|| Aflag
)) {
768 if (kget(rt
->rt_parent
, parent
) != 0)
770 if (parent
.rt_flags
& RTF_PRCLONING
)
774 bzero(&addr
, sizeof(addr
));
775 if ((sa
= kgetsa(rt_key(rt
))))
776 bcopy(sa
, &addr
, sa
->sa_len
);
777 bzero(&mask
, sizeof(mask
));
778 if (rt_mask(rt
) && (sa
= kgetsa(rt_mask(rt
))))
779 bcopy(sa
, &mask
, sa
->sa_len
);
780 p_sockaddr(&addr
.u_sa
, &mask
.u_sa
, rt
->rt_flags
, wid_dst
);
781 p_sockaddr(kgetsa(rt
->rt_gateway
), NULL
, RTF_HOST
, wid_gw
);
782 snprintf(buffer
, sizeof(buffer
), "%%-%d.%ds ", wid_flags
, wid_flags
);
783 p_flags(rt
->rt_flags
, buffer
);
784 if (addr
.u_sa
.sa_family
== AF_INET
|| Wflag
) {
785 printf("%*ld %*lu ", wid_refs
, rt
->rt_refcnt
,
786 wid_use
, rt
->rt_use
);
788 if (rt
->rt_rmx
.rmx_mtu
!= 0)
789 printf("%*lu ", wid_mtu
, rt
->rt_rmx
.rmx_mtu
);
791 printf("%*s ", wid_mtu
, "");
795 if (rt
->rt_ifp
!= lastif
) {
796 if (kget(rt
->rt_ifp
, ifnet
) == 0)
797 strlcpy(prettyname
, ifnet
.if_xname
,
800 strlcpy(prettyname
, "---", sizeof(prettyname
));
803 printf("%*.*s", wid_if
, wid_if
, prettyname
);
804 if (rt
->rt_rmx
.rmx_expire
) {
808 clock_gettime(CLOCK_MONOTONIC
, &sp
);
810 expire_time
= (int)(rt
->rt_rmx
.rmx_expire
- sp
.tv_sec
);
812 printf(" %*d", wid_expire
, (int)expire_time
);
814 printf("%*s ", wid_expire
, "");
816 printf("%*s ", wid_expire
, "");
818 if (rt
->rt_nodes
[0].rn_dupedkey
)
822 if (rt
->rt_shim
[0] != NULL
)
823 printf(" %-*s", wid_mplslops
, labelops(rt
));
825 printf(" %-*s", wid_mplslops
, "");
826 if (rt
->rt_rmx
.rmx_msl
!= 0)
827 printf(" %*lu", wid_msl
, rt
->rt_rmx
.rmx_msl
);
829 printf("%*s ", wid_msl
, "");
830 if (rt
->rt_rmx
.rmx_iwmaxsegs
!= 0)
831 printf(" %*lu", wid_iwmax
, rt
->rt_rmx
.rmx_iwmaxsegs
);
833 printf("%*s ", wid_iwmax
, "");
834 if (rt
->rt_rmx
.rmx_iwcapsegs
!= 0)
835 printf(" %*lu", wid_iw
, rt
->rt_rmx
.rmx_iwcapsegs
);
837 printf("%*s ", wid_iw
, "");
846 static char line
[MAXHOSTNAMELEN
];
851 hp
= gethostbyaddr(&in
, sizeof (struct in_addr
), AF_INET
);
854 trimdomain(cp
, strlen(cp
));
858 strncpy(line
, cp
, sizeof(line
) - 1);
859 line
[sizeof(line
) - 1] = '\0';
861 #define C(x) ((x) & 0xff)
863 sprintf(line
, "%lu.%lu.%lu.%lu",
864 C(in
>> 24), C(in
>> 16), C(in
>> 8), C(in
));
876 else if (IN_CLASSB(a
))
884 domask(char *dst
, u_long addr
, u_long mask
)
888 if (!mask
|| (forgemask(addr
) == mask
)) {
893 for (b
= 0; b
< 32; b
++)
894 if (mask
& (1 << b
)) {
898 for (bb
= b
+1; bb
< 32; bb
++)
899 if (!(mask
& (1 << bb
))) {
900 i
= -1; /* noncontig */
906 sprintf(dst
, "&0x%lx", mask
);
908 sprintf(dst
, "/%d", 32-i
);
912 * Return the name of the network whose address is given.
913 * The address is assumed to be that of a net or subnet, not a host.
916 netname(u_long in
, u_long mask
)
919 static char line
[MAXHOSTNAMELEN
];
920 struct netent
*np
= NULL
;
924 #define NSHIFT(m) ( \
925 (m) == IN_CLASSA_NET ? IN_CLASSA_NSHIFT : \
926 (m) == IN_CLASSB_NET ? IN_CLASSB_NSHIFT : \
927 (m) == IN_CLASSC_NET ? IN_CLASSC_NSHIFT : \
931 dmask
= forgemask(i
);
932 if (!numeric_addr
&& i
) {
933 np
= getnetbyaddr(i
>> NSHIFT(mask
), AF_INET
);
934 if (np
== NULL
&& mask
== 0)
935 np
= getnetbyaddr(i
>> NSHIFT(dmask
), AF_INET
);
938 trimdomain(cp
, strlen(cp
));
943 strncpy(line
, cp
, sizeof(line
) - 1);
944 line
[sizeof(line
) - 1] = '\0';
946 if (mask
<= IN_CLASSA_NET
&&
947 (i
& IN_CLASSA_HOST
) == 0) {
948 sprintf(line
, "%lu", C(i
>> 24));
949 } else if (mask
<= IN_CLASSB_NET
&&
950 (i
& IN_CLASSB_HOST
) == 0) {
951 sprintf(line
, "%lu.%lu",
952 C(i
>> 24), C(i
>> 16));
953 } else if (mask
<= IN_CLASSC_NET
&&
954 (i
& IN_CLASSC_HOST
) == 0) {
955 sprintf(line
, "%lu.%lu.%lu",
956 C(i
>> 24), C(i
>> 16), C(i
>> 8));
958 sprintf(line
, "%lu.%lu.%lu.%lu",
959 C(i
>> 24), C(i
>> 16), C(i
>> 8), C(i
));
962 domask(line
+ strlen(line
), i
, mask
);
968 netname6(struct sockaddr_in6
*sa6
, struct in6_addr
*mask
)
970 static char line
[MAXHOSTNAMELEN
];
971 u_char
*p
= (u_char
*)mask
;
973 int masklen
, illegal
= 0, flag
= NI_WITHSCOPEID
;
976 for (masklen
= 0, lim
= p
+ 16; p
< lim
; p
++) {
1010 fprintf(stderr
, "illegal prefixlen\n");
1015 if (masklen
== 0 && IN6_IS_ADDR_UNSPECIFIED(&sa6
->sin6_addr
))
1019 flag
|= NI_NUMERICHOST
;
1020 getnameinfo((struct sockaddr
*)sa6
, sa6
->sin6_len
, line
, sizeof(line
),
1024 sprintf(&line
[strlen(line
)], "/%d", masklen
);
1030 routename6(struct sockaddr_in6
*sa6
)
1032 static char line
[MAXHOSTNAMELEN
];
1033 int flag
= NI_WITHSCOPEID
;
1034 /* use local variable for safety */
1035 struct sockaddr_in6 sa6_local
;
1037 sa6_local
.sin6_family
= AF_INET6
;
1038 sa6_local
.sin6_len
= sizeof(sa6_local
);
1039 sa6_local
.sin6_addr
= sa6
->sin6_addr
;
1040 sa6_local
.sin6_scope_id
= sa6
->sin6_scope_id
;
1043 flag
|= NI_NUMERICHOST
;
1045 getnameinfo((struct sockaddr
*)&sa6_local
, sa6_local
.sin6_len
,
1046 line
, sizeof(line
), NULL
, 0, flag
);
1053 * Print routing statistics
1058 struct rtstatistics rts
;
1061 error
= kinfo_get_net_rtstatistics(&rts
);
1063 printf("routing: could not retrieve statistics\n");
1066 printf("routing:\n");
1068 #define p(f, m) if (rts.f || sflag <= 1) \
1069 printf(m, rts.f, plural(rts.f))
1071 p(rts_badredirect
, "\t%lu bad routing redirect%s\n");
1072 p(rts_dynamic
, "\t%lu dynamically created route%s\n");
1073 p(rts_newgateway
, "\t%lu new gateway%s due to redirects\n");
1074 p(rts_unreach
, "\t%lu destination%s found unreachable\n");
1075 p(rts_wildcard
, "\t%lu use%s of a wildcard route\n");
1080 labelops(struct rtentry
*rt
)
1082 const char *lops
[] = { "push", "pop", "swap", "pop all" };
1083 static char buffer
[100];
1085 struct sockaddr_mpls
*smpls
;
1088 for (i
=0; i
<MPLS_MAXLOPS
; ++i
) {
1090 if (rt
->rt_shim
[i
] == NULL
)
1094 sizeof(buffer
) - (cp
- buffer
),
1097 smpls
= (struct sockaddr_mpls
*)kgetsa(rt
->rt_shim
[i
]);
1098 if (smpls
->smpls_op
!= MPLSLOP_POP
&&
1099 smpls
->smpls_op
!= MPLSLOP_POPALL
){
1101 sizeof(buffer
) - (cp
- buffer
),
1103 lops
[smpls
->smpls_op
- 1],
1104 ntohl(smpls
->smpls_label
));
1107 sizeof(buffer
) - (cp
- buffer
),
1109 lops
[smpls
->smpls_op
- 1]);