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. 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.
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
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 * $DragonFly: src/usr.bin/netstat/route.c,v 1.13 2008/07/07 22:02:10 nant Exp $
38 #include <sys/param.h>
39 #include <sys/protosw.h>
40 #include <sys/socket.h>
43 #include <net/ethernet.h>
45 #include <net/if_var.h>
46 #include <net/if_dl.h>
47 #include <net/if_types.h>
48 #include <net/route.h>
50 #include <netinet/in.h>
51 #include <netipx/ipx.h>
52 #include <netatalk/at.h>
53 #include <netgraph/socket/ng_socket.h>
59 #include <netproto/mpls/mpls.h>
61 #include <sys/sysctl.h>
63 #include <arpa/inet.h>
75 #define kget(p, d) (kread((u_long)(p), (char *)&(d), sizeof (d)))
78 /* alignment constraint for routing socket */
80 ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
81 #define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len))
84 * Definitions for showing gateway flags.
95 { RTF_MODIFIED
, 'M' },
96 { RTF_DONE
, 'd' }, /* Completed -- for routing messages only */
98 { RTF_XRESOLVE
, 'X' },
103 { RTF_WASCLONED
,'W' },
104 { RTF_PRCLONING
,'c' },
106 { RTF_BLACKHOLE
,'B' },
107 { RTF_BROADCAST
,'b' },
108 { RTF_MPLSOPS
, 'm' },
113 long dummy
; /* Helps align structure. */
114 struct sockaddr u_sa
;
121 struct rtentry rtentry
;
122 struct radix_node rnode
;
123 struct radix_mask rmask
;
124 struct radix_node_head
*rt_tables
[AF_MAX
+1];
128 static struct sockaddr
*kgetsa (struct sockaddr
*);
129 static void size_cols (int ef
, struct radix_node
*rn
);
130 static void size_cols_tree (struct radix_node
*rn
);
131 static void size_cols_rtentry (struct rtentry
*rt
);
132 static void p_tree (struct radix_node
*);
133 static void p_rtnode (void);
134 static void ntreestuff (void);
135 static void np_rtentry (struct rt_msghdr
*);
136 static void p_sockaddr (struct sockaddr
*, struct sockaddr
*, int, int);
137 static const char *fmt_sockaddr (struct sockaddr
*sa
, struct sockaddr
*mask
,
139 static void p_flags (int, const char *);
140 static const char *fmt_flags(int f
);
141 static void p_rtentry (struct rtentry
*);
142 static u_long
forgemask (u_long
);
143 static void domask (char *, u_long
, u_long
);
144 static const char *labelops(struct rtentry
*);
147 * Print routing tables.
150 routepr(u_long rtree
)
152 struct radix_node_head
*rnh
, head
;
155 printf("Routing tables\n");
157 if (Aflag
== 0 && NewTree
) {
161 printf("rt_tables: symbol not in namelist\n");
168 rtree
+= cpuflag
* (AF_MAX
+ 1) * sizeof(void *);
170 kget(rtree
, rt_tables
);
171 for (i
= 0; i
<= AF_MAX
; i
++) {
172 if ((rnh
= rt_tables
[i
]) == 0)
175 if (i
== AF_UNSPEC
) {
176 if (Aflag
&& af
== 0) {
177 printf("Netmasks:\n");
178 p_tree(head
.rnh_treetop
);
180 } else if (af
== AF_UNSPEC
|| af
== i
) {
181 size_cols(i
, head
.rnh_treetop
);
185 p_tree(head
.rnh_treetop
);
192 * Print address family header before a section of the routing table.
205 afname
= "Internet6";
220 afname
= "AppleTalk";
236 printf("\n%s:\n", afname
);
238 printf("\nProtocol Family %d:\n", af1
);
241 /* column widths; each followed by one space */
243 #define WID_DST_DEFAULT(af) 18 /* width of destination column */
244 #define WID_GW_DEFAULT(af) 18 /* width of gateway column */
245 #define WID_IF_DEFAULT(af) (Wflag ? 8 : 6) /* width of netif column */
247 #define WID_DST_DEFAULT(af) \
248 ((af) == AF_INET6 ? (numeric_addr ? 33: 18) : 18)
249 #define WID_GW_DEFAULT(af) \
250 ((af) == AF_INET6 ? (numeric_addr ? 29 : 18) : 18)
251 #define WID_IF_DEFAULT(af) ((af) == AF_INET6 ? 8 : (Wflag ? 8 :6))
256 static int wid_flags
;
261 static int wid_expire
;
262 static int wid_mplslops
;
265 size_cols(int ef
, struct radix_node
*rn
)
267 wid_dst
= WID_DST_DEFAULT(ef
);
268 wid_gw
= WID_GW_DEFAULT(ef
);
273 wid_if
= WID_IF_DEFAULT(ef
);
282 size_cols_tree(struct radix_node
*rn
)
286 if (rnode
.rn_bit
< 0) {
287 if ((rnode
.rn_flags
& RNF_ROOT
) == 0) {
289 size_cols_rtentry(&rtentry
);
291 if ((rn
= rnode
.rn_dupedkey
))
295 size_cols_tree(rnode
.rn_left
);
301 size_cols_rtentry(struct rtentry
*rt
)
303 static struct ifnet ifnet
, *lastif
;
304 struct rtentry parent
;
305 static char buffer
[100];
312 * Don't print protocol-cloned routes unless -a.
314 if (rt
->rt_flags
& RTF_WASCLONED
&& !aflag
) {
315 kget(rt
->rt_parent
, parent
);
316 if (parent
.rt_flags
& RTF_PRCLONING
)
320 bzero(&addr
, sizeof(addr
));
321 if ((sa
= kgetsa(rt_key(rt
))))
322 bcopy(sa
, &addr
, sa
->sa_len
);
323 bzero(&mask
, sizeof(mask
));
324 if (rt_mask(rt
) && (sa
= kgetsa(rt_mask(rt
))))
325 bcopy(sa
, &mask
, sa
->sa_len
);
326 bp
= fmt_sockaddr(&addr
.u_sa
, &mask
.u_sa
, rt
->rt_flags
);
328 wid_dst
= MAX(len
, wid_dst
);
330 bp
= fmt_sockaddr(kgetsa(rt
->rt_gateway
), NULL
, RTF_HOST
);
332 wid_gw
= MAX(len
, wid_gw
);
334 bp
= fmt_flags(rt
->rt_flags
);
336 wid_flags
= MAX(len
, wid_flags
);
338 if (addr
.u_sa
.sa_family
== AF_INET
|| Wflag
) {
339 len
= snprintf(buffer
, sizeof(buffer
), "%ld", rt
->rt_refcnt
);
340 wid_refs
= MAX(len
, wid_refs
);
341 len
= snprintf(buffer
, sizeof(buffer
), "%lu", rt
->rt_use
);
342 wid_use
= MAX(len
, wid_use
);
343 if (Wflag
&& rt
->rt_rmx
.rmx_mtu
!= 0) {
344 len
= snprintf(buffer
, sizeof(buffer
),
345 "%lu", rt
->rt_rmx
.rmx_mtu
);
346 wid_mtu
= MAX(len
, wid_mtu
);
350 if (rt
->rt_ifp
!= lastif
) {
351 kget(rt
->rt_ifp
, ifnet
);
353 len
= strlen(ifnet
.if_xname
);
354 wid_if
= MAX(len
, wid_if
);
356 if (rt
->rt_rmx
.rmx_expire
) {
360 rt
->rt_rmx
.rmx_expire
- time(NULL
)) > 0) {
361 snprintf(buffer
, sizeof(buffer
), "%d",
363 wid_expire
= MAX(len
, wid_expire
);
367 if (rt
->rt_shim
[0] != NULL
)
368 len
= strlen(labelops(rt
));
369 wid_mplslops
= MAX(len
, wid_mplslops
);
374 * Print header for routing table columns.
381 printf("%-8.8s ","Address");
382 if (af1
== AF_INET
|| Wflag
) {
384 printf("%-*.*s %-*.*s %-*.*s %*.*s %*.*s %*.*s %*.*s %*s %-*s\n",
385 wid_dst
, wid_dst
, "Destination",
386 wid_gw
, wid_gw
, "Gateway",
387 wid_flags
, wid_flags
, "Flags",
388 wid_refs
, wid_refs
, "Refs",
389 wid_use
, wid_use
, "Use",
390 wid_mtu
, wid_mtu
, "Mtu",
391 wid_if
, wid_if
, "Netif",
392 wid_expire
, "Expire",
393 wid_mplslops
, "Labelops");
395 printf("%-*.*s %-*.*s %-*.*s %*.*s %*.*s %*.*s %*s\n",
396 wid_dst
, wid_dst
, "Destination",
397 wid_gw
, wid_gw
, "Gateway",
398 wid_flags
, wid_flags
, "Flags",
399 wid_refs
, wid_refs
, "Refs",
400 wid_use
, wid_use
, "Use",
401 wid_if
, wid_if
, "Netif",
402 wid_expire
, "Expire");
405 printf("%-*.*s %-*.*s %-*.*s %*.*s %*s\n",
406 wid_dst
, wid_dst
, "Destination",
407 wid_gw
, wid_gw
, "Gateway",
408 wid_flags
, wid_flags
, "Flags",
409 wid_if
, wid_if
, "Netif",
410 wid_expire
, "Expire");
414 static struct sockaddr
*
415 kgetsa(struct sockaddr
*dst
)
418 kget(dst
, pt_u
.u_sa
);
419 if (pt_u
.u_sa
.sa_len
> sizeof (pt_u
.u_sa
))
420 kread((u_long
)dst
, (char *)pt_u
.u_data
, pt_u
.u_sa
.sa_len
);
425 p_tree(struct radix_node
*rn
)
430 if (rnode
.rn_bit
< 0) {
432 printf("%-8.8lx ", (u_long
)rn
);
433 if (rnode
.rn_flags
& RNF_ROOT
) {
435 printf("(root node)%s",
436 rnode
.rn_dupedkey
? " =>\n" : "\n");
437 } else if (do_rtent
) {
443 p_sockaddr(kgetsa((struct sockaddr
*)rnode
.rn_key
),
447 if ((rn
= rnode
.rn_dupedkey
))
450 if (Aflag
&& do_rtent
) {
451 printf("%-8.8lx ", (u_long
)rn
);
455 p_tree(rnode
.rn_left
);
465 struct radix_mask
*rm
= rnode
.rn_mklist
;
467 if (rnode
.rn_bit
< 0) {
470 p_sockaddr(kgetsa((struct sockaddr
*)rnode
.rn_mask
),
475 sprintf(nbuf
, "(%d)", rnode
.rn_bit
);
476 printf("%6.6s %8.8lx : %8.8lx", nbuf
, (u_long
)rnode
.rn_left
, (u_long
)rnode
.rn_right
);
480 sprintf(nbuf
, " %d refs, ", rmask
.rm_refs
);
481 printf(" mk = %8.8lx {(%d),%s",
482 (u_long
)rm
, -1 - rmask
.rm_bit
, rmask
.rm_refs
? nbuf
: " ");
483 if (rmask
.rm_flags
& RNF_NORMAL
) {
484 struct radix_node rnode_aux
;
485 printf(" <normal>, ");
486 kget(rmask
.rm_leaf
, rnode_aux
);
487 p_sockaddr(kgetsa((struct sockaddr
*)rnode_aux
.rn_mask
),
490 p_sockaddr(kgetsa((struct sockaddr
*)rmask
.rm_mask
),
493 if ((rm
= rmask
.rm_next
))
505 char *buf
, *next
, *lim
;
506 struct rt_msghdr
*rtm
;
512 mib
[4] = NET_RT_DUMP
;
520 if (sysctl(mib
, miblen
, NULL
, &needed
, NULL
, 0) < 0) {
521 err(1, "sysctl: net.route.0.0.dump estimate");
524 if ((buf
= malloc(needed
)) == 0) {
525 err(2, "malloc(%lu)", (unsigned long)needed
);
527 if (sysctl(mib
, miblen
, buf
, &needed
, NULL
, 0) < 0) {
528 err(1, "sysctl: net.route.0.0.dump");
531 for (next
= buf
; next
< lim
; next
+= rtm
->rtm_msglen
) {
532 rtm
= (struct rt_msghdr
*)next
;
538 np_rtentry(struct rt_msghdr
*rtm
)
540 struct sockaddr
*sa
= (struct sockaddr
*)(rtm
+ 1);
542 static int masks_done
, banner_printed
;
545 int af1
= 0, interesting
= RTF_UP
| RTF_GATEWAY
| RTF_HOST
;
548 /* for the moment, netmasks are skipped over */
549 if (!banner_printed
) {
550 printf("Netmasks:\n");
553 if (masks_done
== 0) {
554 if (rtm
->rtm_addrs
!= RTA_DST
) {
565 if (rtm
->rtm_addrs
== RTA_DST
)
566 p_sockaddr(sa
, NULL
, 0, 36);
568 p_sockaddr(sa
, NULL
, rtm
->rtm_flags
, 16);
569 sa
= (struct sockaddr
*)(ROUNDUP(sa
->sa_len
) + (char *)sa
);
570 p_sockaddr(sa
, NULL
, 0, 18);
572 p_flags(rtm
->rtm_flags
& interesting
, "%-6.6s ");
577 p_sockaddr(struct sockaddr
*sa
, struct sockaddr
*mask
, int flags
, int width
)
581 cp
= fmt_sockaddr(sa
, mask
, flags
);
587 printf("%-*s ", width
, cp
);
589 printf("%-*.*s ", width
, width
, cp
);
594 fmt_sockaddr(struct sockaddr
*sa
, struct sockaddr
*mask
, int flags
)
596 static char workbuf
[128];
597 const char *cp
= workbuf
;
599 switch(sa
->sa_family
) {
602 struct sockaddr_in
*sin
= (struct sockaddr_in
*)sa
;
604 if ((sin
->sin_addr
.s_addr
== INADDR_ANY
) &&
606 ntohl(((struct sockaddr_in
*)mask
)->sin_addr
.s_addr
)
609 else if (flags
& RTF_HOST
)
610 cp
= routename(sin
->sin_addr
.s_addr
);
612 cp
= netname(sin
->sin_addr
.s_addr
,
613 ntohl(((struct sockaddr_in
*)mask
)
616 cp
= netname(sin
->sin_addr
.s_addr
, 0L);
623 struct sockaddr_in6
*sa6
= (struct sockaddr_in6
*)sa
;
624 struct in6_addr
*in6
= &sa6
->sin6_addr
;
627 * XXX: This is a special workaround for KAME kernels.
628 * sin6_scope_id field of SA should be set in the future.
630 if (IN6_IS_ADDR_LINKLOCAL(in6
) ||
631 IN6_IS_ADDR_MC_LINKLOCAL(in6
)) {
632 /* XXX: override is ok? */
633 sa6
->sin6_scope_id
= (u_int32_t
)ntohs(*(u_short
*)&in6
->s6_addr
[2]);
634 *(u_short
*)&in6
->s6_addr
[2] = 0;
637 if (flags
& RTF_HOST
)
638 cp
= routename6(sa6
);
641 &((struct sockaddr_in6
*)mask
)->sin6_addr
);
643 cp
= netname6(sa6
, NULL
);
651 struct ipx_addr work
= ((struct sockaddr_ipx
*)sa
)->sipx_addr
;
652 if (ipx_nullnet(satoipx_addr(work
)))
660 if (!(flags
& RTF_HOST
) && mask
)
661 cp
= atalk_print2(sa
,mask
,9);
663 cp
= atalk_print(sa
,11);
668 printf("%s", ((struct sockaddr_ng
*)sa
)->sg_data
);
679 struct sockaddr_dl
*sdl
= (struct sockaddr_dl
*)sa
;
681 if (sdl
->sdl_nlen
== 0 && sdl
->sdl_alen
== 0 &&
683 (void) sprintf(workbuf
, "link#%d", sdl
->sdl_index
);
685 switch (sdl
->sdl_type
) {
689 if (sdl
->sdl_alen
== ETHER_ADDR_LEN
) {
690 cp
= ether_ntoa((struct ether_addr
*)
691 (sdl
->sdl_data
+ sdl
->sdl_nlen
));
704 struct sockaddr_mpls
*smpls
= (struct sockaddr_mpls
*)sa
;
706 (void) sprintf(workbuf
, "%d", ntohl(smpls
->smpls_label
));
712 u_char
*s
= (u_char
*)sa
->sa_data
, *slim
;
716 slim
= sa
->sa_len
+ (u_char
*) sa
;
717 cqlim
= cq
+ sizeof(workbuf
) - 6;
718 cq
+= sprintf(cq
, "(%d)", sa
->sa_family
);
719 while (s
< slim
&& cq
< cqlim
) {
720 cq
+= sprintf(cq
, " %02x", *s
++);
722 cq
+= sprintf(cq
, "%02x", *s
++);
732 p_flags(int f
, const char *format
)
734 printf(format
, fmt_flags(f
));
740 static char name
[33];
742 struct bits
*p
= bits
;
744 for (flags
= name
; p
->b_mask
; p
++)
752 p_rtentry(struct rtentry
*rt
)
754 static struct ifnet ifnet
, *lastif
;
755 struct rtentry parent
;
756 static char buffer
[128];
757 static char prettyname
[128];
762 * Don't print protocol-cloned routes unless -a.
764 if (rt
->rt_flags
& RTF_WASCLONED
&& !aflag
) {
765 kget(rt
->rt_parent
, parent
);
766 if (parent
.rt_flags
& RTF_PRCLONING
)
770 bzero(&addr
, sizeof(addr
));
771 if ((sa
= kgetsa(rt_key(rt
))))
772 bcopy(sa
, &addr
, sa
->sa_len
);
773 bzero(&mask
, sizeof(mask
));
774 if (rt_mask(rt
) && (sa
= kgetsa(rt_mask(rt
))))
775 bcopy(sa
, &mask
, sa
->sa_len
);
776 p_sockaddr(&addr
.u_sa
, &mask
.u_sa
, rt
->rt_flags
, wid_dst
);
777 p_sockaddr(kgetsa(rt
->rt_gateway
), NULL
, RTF_HOST
, wid_gw
);
778 snprintf(buffer
, sizeof(buffer
), "%%-%d.%ds ", wid_flags
, wid_flags
);
779 p_flags(rt
->rt_flags
, buffer
);
780 if (addr
.u_sa
.sa_family
== AF_INET
|| Wflag
) {
781 printf("%*ld %*lu ", wid_refs
, rt
->rt_refcnt
,
782 wid_use
, rt
->rt_use
);
784 if (rt
->rt_rmx
.rmx_mtu
!= 0)
785 printf("%*lu ", wid_mtu
, rt
->rt_rmx
.rmx_mtu
);
787 printf("%*s ", wid_mtu
, "");
791 if (rt
->rt_ifp
!= lastif
) {
792 kget(rt
->rt_ifp
, ifnet
);
794 strlcpy(prettyname
, ifnet
.if_xname
, sizeof(prettyname
));
796 printf("%*.*s", wid_if
, wid_if
, prettyname
);
797 if (rt
->rt_rmx
.rmx_expire
) {
801 rt
->rt_rmx
.rmx_expire
- time(NULL
)) > 0)
802 printf(" %*d", wid_expire
, (int)expire_time
);
804 printf("%*s ", wid_expire
, "");
806 if (rt
->rt_nodes
[0].rn_dupedkey
)
810 if (rt
->rt_shim
[0] != NULL
)
811 printf(" %-*s", wid_mplslops
, labelops(rt
));
820 static char line
[MAXHOSTNAMELEN
];
825 hp
= gethostbyaddr(&in
, sizeof (struct in_addr
), AF_INET
);
828 trimdomain(cp
, strlen(cp
));
832 strncpy(line
, cp
, sizeof(line
) - 1);
833 line
[sizeof(line
) - 1] = '\0';
835 #define C(x) ((x) & 0xff)
837 sprintf(line
, "%lu.%lu.%lu.%lu",
838 C(in
>> 24), C(in
>> 16), C(in
>> 8), C(in
));
850 else if (IN_CLASSB(a
))
858 domask(char *dst
, u_long addr
, u_long mask
)
862 if (!mask
|| (forgemask(addr
) == mask
)) {
867 for (b
= 0; b
< 32; b
++)
868 if (mask
& (1 << b
)) {
872 for (bb
= b
+1; bb
< 32; bb
++)
873 if (!(mask
& (1 << bb
))) {
874 i
= -1; /* noncontig */
880 sprintf(dst
, "&0x%lx", mask
);
882 sprintf(dst
, "/%d", 32-i
);
886 * Return the name of the network whose address is given.
887 * The address is assumed to be that of a net or subnet, not a host.
890 netname(u_long in
, u_long mask
)
893 static char line
[MAXHOSTNAMELEN
];
894 struct netent
*np
= 0;
898 #define NSHIFT(m) ( \
899 (m) == IN_CLASSA_NET ? IN_CLASSA_NSHIFT : \
900 (m) == IN_CLASSB_NET ? IN_CLASSB_NSHIFT : \
901 (m) == IN_CLASSC_NET ? IN_CLASSC_NSHIFT : \
905 dmask
= forgemask(i
);
906 if (!numeric_addr
&& i
) {
907 np
= getnetbyaddr(i
>> NSHIFT(mask
), AF_INET
);
908 if (np
== NULL
&& mask
== 0)
909 np
= getnetbyaddr(i
>> NSHIFT(dmask
), AF_INET
);
912 trimdomain(cp
, strlen(cp
));
917 strncpy(line
, cp
, sizeof(line
) - 1);
918 line
[sizeof(line
) - 1] = '\0';
920 if (mask
<= IN_CLASSA_NET
&&
921 (i
& IN_CLASSA_HOST
) == 0) {
922 sprintf(line
, "%lu", C(i
>> 24));
923 } else if (mask
<= IN_CLASSB_NET
&&
924 (i
& IN_CLASSB_HOST
) == 0) {
925 sprintf(line
, "%lu.%lu",
926 C(i
>> 24), C(i
>> 16));
927 } else if (mask
<= IN_CLASSC_NET
&&
928 (i
& IN_CLASSC_HOST
) == 0) {
929 sprintf(line
, "%lu.%lu.%lu",
930 C(i
>> 24), C(i
>> 16), C(i
>> 8));
932 sprintf(line
, "%lu.%lu.%lu.%lu",
933 C(i
>> 24), C(i
>> 16), C(i
>> 8), C(i
));
936 domask(line
+ strlen(line
), i
, mask
);
942 netname6(struct sockaddr_in6
*sa6
, struct in6_addr
*mask
)
944 static char line
[MAXHOSTNAMELEN
];
945 u_char
*p
= (u_char
*)mask
;
947 int masklen
, illegal
= 0, flag
= NI_WITHSCOPEID
;
950 for (masklen
= 0, lim
= p
+ 16; p
< lim
; p
++) {
984 fprintf(stderr
, "illegal prefixlen\n");
989 if (masklen
== 0 && IN6_IS_ADDR_UNSPECIFIED(&sa6
->sin6_addr
))
993 flag
|= NI_NUMERICHOST
;
994 getnameinfo((struct sockaddr
*)sa6
, sa6
->sin6_len
, line
, sizeof(line
),
998 sprintf(&line
[strlen(line
)], "/%d", masklen
);
1004 routename6(struct sockaddr_in6
*sa6
)
1006 static char line
[MAXHOSTNAMELEN
];
1007 int flag
= NI_WITHSCOPEID
;
1008 /* use local variable for safety */
1009 struct sockaddr_in6 sa6_local
;
1011 sa6_local
.sin6_family
= AF_INET6
;
1012 sa6_local
.sin6_len
= sizeof(sa6_local
);
1013 sa6_local
.sin6_addr
= sa6
->sin6_addr
;
1014 sa6_local
.sin6_scope_id
= sa6
->sin6_scope_id
;
1017 flag
|= NI_NUMERICHOST
;
1019 getnameinfo((struct sockaddr
*)&sa6_local
, sa6_local
.sin6_len
,
1020 line
, sizeof(line
), NULL
, 0, flag
);
1027 * Print routing statistics
1032 struct rtstatistics rts
;
1035 error
= kinfo_get_net_rtstatistics(&rts
);
1037 printf("routing: could not retrieve statistics\n");
1040 printf("routing:\n");
1042 #define p(f, m) if (rts.f || sflag <= 1) \
1043 printf(m, rts.f, plural(rts.f))
1045 p(rts_badredirect
, "\t%lu bad routing redirect%s\n");
1046 p(rts_dynamic
, "\t%lu dynamically created route%s\n");
1047 p(rts_newgateway
, "\t%lu new gateway%s due to redirects\n");
1048 p(rts_unreach
, "\t%lu destination%s found unreachable\n");
1049 p(rts_wildcard
, "\t%lu use%s of a wildcard route\n");
1054 ipx_print(struct sockaddr
*sa
)
1057 struct servent
*sp
= 0;
1058 const char *net
= "", *host
= "";
1061 struct ipx_addr work
= ((struct sockaddr_ipx
*)sa
)->sipx_addr
;
1062 static char mybuf
[50];
1063 char cport
[10], chost
[15], cnet
[15];
1065 port
= ntohs(work
.x_port
);
1067 if (ipx_nullnet(work
) && ipx_nullhost(work
)) {
1071 sprintf(mybuf
, "*.%s", sp
->s_name
);
1073 sprintf(mybuf
, "*.%x", port
);
1075 sprintf(mybuf
, "*.*");
1080 if (ipx_wildnet(work
))
1082 else if (ipx_nullnet(work
))
1085 q
= work
.x_net
.c_net
;
1086 sprintf(cnet
, "%02x%02x%02x%02x",
1087 q
[0], q
[1], q
[2], q
[3]);
1088 for (p
= cnet
; *p
== '0' && p
< cnet
+ 8; p
++)
1093 if (ipx_wildhost(work
))
1095 else if (ipx_nullhost(work
))
1098 q
= work
.x_host
.c_host
;
1099 sprintf(chost
, "%02x%02x%02x%02x%02x%02x",
1100 q
[0], q
[1], q
[2], q
[3], q
[4], q
[5]);
1101 for (p
= chost
; *p
== '0' && p
< chost
+ 12; p
++)
1107 if (strcmp(host
, "*") == 0)
1110 snprintf(cport
, sizeof(cport
),
1111 "%s%s", *host
? "." : "", sp
->s_name
);
1113 snprintf(cport
, sizeof(cport
),
1114 "%s%x", *host
? "." : "", port
);
1118 snprintf(mybuf
, sizeof(mybuf
), "%s.%s%s", net
, host
, cport
);
1123 ipx_phost(struct sockaddr
*sa
)
1125 struct sockaddr_ipx
*sipx
= (struct sockaddr_ipx
*)sa
;
1126 struct sockaddr_ipx work
;
1127 static union ipx_net ipx_zeronet
;
1132 in
= work
.sipx_addr
;
1134 work
.sipx_addr
.x_port
= 0;
1135 work
.sipx_addr
.x_net
= ipx_zeronet
;
1136 p
= ipx_print((struct sockaddr
*)&work
);
1137 if (strncmp("*.", p
, 2) == 0) p
+= 2;
1143 short ns_nullh
[] = {0,0,0};
1144 short ns_bh
[] = {-1,-1,-1};
1147 ns_print(struct sockaddr
*sa
)
1149 struct sockaddr_ns
*sns
= (struct sockaddr_ns
*)sa
;
1150 struct ns_addr work
;
1151 union { union ns_net net_e
; u_long long_e
; } net
;
1153 static char mybuf
[50], cport
[10], chost
[25];
1157 work
= sns
->sns_addr
;
1158 port
= ntohs(work
.x_port
);
1160 net
.net_e
= work
.x_net
;
1161 if (ns_nullhost(work
) && net
.long_e
== 0) {
1163 sprintf(mybuf
, "*.%xH", port
);
1166 sprintf(mybuf
, "*.*");
1170 if (bcmp(ns_bh
, work
.x_host
.c_host
, 6) == 0) {
1172 } else if (bcmp(ns_nullh
, work
.x_host
.c_host
, 6) == 0) {
1175 q
= work
.x_host
.c_host
;
1176 sprintf(chost
, "%02x%02x%02x%02x%02x%02xH",
1177 q
[0], q
[1], q
[2], q
[3], q
[4], q
[5]);
1178 for (p
= chost
; *p
== '0' && p
< chost
+ 12; p
++)
1183 sprintf(cport
, ".%xH", htons(port
));
1187 sprintf(mybuf
,"%xH.%s%s", ntohl(net
.long_e
), host
, cport
);
1193 ns_phost(struct sockaddr
*sa
)
1195 struct sockaddr_ns
*sns
= (struct sockaddr_ns
*)sa
;
1196 struct sockaddr_ns work
;
1197 static union ns_net ns_zeronet
;
1201 work
.sns_addr
.x_port
= 0;
1202 work
.sns_addr
.x_net
= ns_zeronet
;
1204 p
= ns_print((struct sockaddr
*)&work
);
1205 if (strncmp("0H.", p
, 3) == 0)
1231 labelops(struct rtentry
*rt
)
1233 const char *lops
[] = { "push", "pop", "swap", "pop all" };
1234 static char buffer
[100];
1236 struct sockaddr_mpls
*smpls
;
1239 for (i
=0; i
<MPLS_MAXLOPS
; ++i
) {
1241 if (rt
->rt_shim
[i
] == NULL
)
1245 sizeof(buffer
) - (cp
- buffer
),
1248 smpls
= (struct sockaddr_mpls
*)kgetsa(rt
->rt_shim
[i
]);
1249 if (smpls
->smpls_op
!= MPLSLOP_POP
&&
1250 smpls
->smpls_op
!= MPLSLOP_POPALL
){
1252 sizeof(buffer
) - (cp
- buffer
),
1254 lops
[smpls
->smpls_op
- 1],
1255 ntohl(smpls
->smpls_label
));
1258 sizeof(buffer
) - (cp
- buffer
),
1260 lops
[smpls
->smpls_op
- 1]);