]>
git.saurik.com Git - apple/network_cmds.git/blob - netstat.tproj/route.c
fceabf0a98fc9451cacc4853d127635a9586a454
2 * Copyright (c) 2008-2017 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 * Copyright (c) 1983, 1988, 1993
30 * The Regents of the University of California. All rights reserved.
32 * Redistribution and use in source and binary forms, with or without
33 * modification, are permitted provided that the following conditions
35 * 1. Redistributions of source code must retain the above copyright
36 * notice, this list of conditions and the following disclaimer.
37 * 2. Redistributions in binary form must reproduce the above copyright
38 * notice, this list of conditions and the following disclaimer in the
39 * documentation and/or other materials provided with the distribution.
40 * 3. All advertising materials mentioning features or use of this software
41 * must display the following acknowledgement:
42 * This product includes software developed by the University of
43 * California, Berkeley and its contributors.
44 * 4. Neither the name of the University nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
62 #include <sys/param.h>
63 #include <sys/socket.h>
67 #include <net/if_var.h>
68 #include <net/if_dl.h>
69 #include <net/if_types.h>
70 #include <net/route.h>
71 #include <net/radix.h>
73 #include <netinet/in.h>
75 #include <sys/sysctl.h>
77 #include <arpa/inet.h>
87 /* alignment constraint for routing socket */
89 ((a) > 0 ? (1 + (((a) - 1) | (sizeof(uint32_t) - 1))) : sizeof(uint32_t))
90 #define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len))
93 * Definitions for showing gateway flags.
100 { RTF_GATEWAY
, 'G' },
103 { RTF_DYNAMIC
, 'D' },
104 { RTF_MODIFIED
, 'M' },
105 { RTF_MULTICAST
,'m' },
106 { RTF_DONE
, 'd' }, /* Completed -- for routing messages only */
107 { RTF_CLONING
, 'C' },
108 { RTF_XRESOLVE
, 'X' },
113 { RTF_WASCLONED
,'W' },
114 { RTF_PRCLONING
,'c' },
116 { RTF_BLACKHOLE
,'B' },
117 { RTF_BROADCAST
,'b' },
118 { RTF_IFSCOPE
, 'I' },
126 uint32_t dummy
; /* Helps align structure. */
127 struct sockaddr u_sa
;
131 static void np_rtentry
__P((struct rt_msghdr2
*));
132 static void p_sockaddr
__P((struct sockaddr
*, struct sockaddr
*, int, int));
133 static void p_flags
__P((int, char *));
134 static uint32_t forgemask
__P((uint32_t));
135 static void domask
__P((char *, uint32_t, uint32_t));
138 * Print address family header before a section of the routing table.
151 afname
= "Internet6";
162 printf("\n%s:\n", afname
);
164 printf("\nProtocol Family %d:\n", af
);
167 /* column widths; each followed by one space */
169 #define WID_DST(af) 18 /* width of destination column */
170 #define WID_GW(af) 18 /* width of gateway column */
171 #define WID_IF(af) 7 /* width of netif column */
173 #define WID_DST(af) \
174 ((af) == AF_INET6 ? (lflag ? 39 : (nflag ? 39: 18)) : 18)
176 ((af) == AF_INET6 ? (lflag ? 31 : (nflag ? 31 : 18)) : 18)
177 #define WID_IF(af) ((af) == AF_INET6 ? 8 : 7)
181 * Print header for routing table columns.
188 printf("%-8.8s ","Address");
189 if (af
== AF_INET
|| lflag
) {
191 printf("%-*.*s %-*.*s %-10.10s %6.6s %8.8s %6.6s %*.*s %6s "
192 "%10s %10s %8s %8s %8s\n",
193 WID_DST(af
), WID_DST(af
), "Destination",
194 WID_GW(af
), WID_GW(af
), "Gateway",
195 "Flags", "Refs", "Use", "Mtu",
196 WID_IF(af
), WID_IF(af
), "Netif", "Expire",
197 "rtt(ns)", "rttvar(ns)", "recvpipe", "sendpipe", "ssthresh");
199 printf("%-*.*s %-*.*s %-10.10s %6.6s %8.8s %6.6s %*.*s %6s "
201 WID_DST(af
), WID_DST(af
), "Destination",
202 WID_GW(af
), WID_GW(af
), "Gateway",
203 "Flags", "Refs", "Use", "Mtu",
204 WID_IF(af
), WID_IF(af
), "Netif", "Expire",
205 "rtt(ns)", "rttvar(ns)");
207 printf("%-*.*s %-*.*s %-10.10s %6.6s %8.8s %6.6s %*.*s %6s\n",
208 WID_DST(af
), WID_DST(af
), "Destination",
209 WID_GW(af
), WID_GW(af
), "Gateway",
210 "Flags", "Refs", "Use", "Mtu",
211 WID_IF(af
), WID_IF(af
), "Netif", "Expire");
213 printf("%-*.*s %-*.*s %-10.10s %6.6s %8.8s %*.*s %6s\n",
214 WID_DST(af
), WID_DST(af
), "Destination",
215 WID_GW(af
), WID_GW(af
), "Gateway",
216 "Flags", "Refs", "Use",
217 WID_IF(af
), WID_IF(af
), "Netif", "Expire");
219 printf("%-*.*s %-*.*s %-10.10s %8.8s %6s\n",
220 WID_DST(af
), WID_DST(af
), "Destination",
221 WID_GW(af
), WID_GW(af
), "Gateway",
222 "Flags", "Netif", "Expire");
227 * Print routing tables.
234 char *buf
, *next
, *lim
;
235 struct rt_msghdr2
*rtm
;
237 printf("Routing tables\n");
243 mib
[4] = NET_RT_DUMP2
;
245 if (sysctl(mib
, 6, NULL
, &needed
, NULL
, 0) < 0) {
246 err(1, "sysctl: net.route.0.0.dump estimate");
249 if ((buf
= malloc(needed
)) == 0) {
250 err(2, "malloc(%lu)", (unsigned long)needed
);
252 if (sysctl(mib
, 6, buf
, &needed
, NULL
, 0) < 0) {
253 err(1, "sysctl: net.route.0.0.dump");
256 for (next
= buf
; next
< lim
; next
+= rtm
->rtm_msglen
) {
257 rtm
= (struct rt_msghdr2
*)next
;
263 get_rtaddrs(int addrs
, struct sockaddr
*sa
, struct sockaddr
**rti_info
)
267 for (i
= 0; i
< RTAX_MAX
; i
++) {
268 if (addrs
& (1 << i
)) {
270 sa
= (struct sockaddr
*)(ROUNDUP(sa
->sa_len
) + (char *)sa
);
278 np_rtentry(struct rt_msghdr2
*rtm
)
280 struct sockaddr
*sa
= (struct sockaddr
*)(rtm
+ 1);
281 struct sockaddr
*rti_info
[RTAX_MAX
];
284 u_short lastindex
= 0xffff;
285 static char ifname
[IFNAMSIZ
+ 1];
289 * Don't print protocol-cloned routes unless -a.
291 if ((rtm
->rtm_flags
& RTF_WASCLONED
) &&
292 (rtm
->rtm_parentflags
& RTF_PRCLONING
) &&
297 if (lflag
> 1 && zflag
!= 0 && rtm
->rtm_rmx
.rmx_rtt
== 0 && rtm
->rtm_rmx
.rmx_rttvar
== 0)
300 if (af
!= AF_UNSPEC
&& af
!= fam
)
302 if (fam
!= old_fam
) {
307 get_rtaddrs(rtm
->rtm_addrs
, sa
, rti_info
);
308 bzero(&addr
, sizeof(addr
));
309 if ((rtm
->rtm_addrs
& RTA_DST
))
310 bcopy(rti_info
[RTAX_DST
], &addr
, rti_info
[RTAX_DST
]->sa_len
);
311 bzero(&mask
, sizeof(mask
));
312 if ((rtm
->rtm_addrs
& RTA_NETMASK
))
313 bcopy(rti_info
[RTAX_NETMASK
], &mask
, rti_info
[RTAX_NETMASK
]->sa_len
);
314 p_sockaddr(&addr
.u_sa
, &mask
.u_sa
, rtm
->rtm_flags
,
315 WID_DST(addr
.u_sa
.sa_family
));
317 p_sockaddr(rti_info
[RTAX_GATEWAY
], NULL
, RTF_HOST
,
318 WID_GW(addr
.u_sa
.sa_family
));
320 p_flags(rtm
->rtm_flags
, "%-10.10s ");
322 if (addr
.u_sa
.sa_family
== AF_INET
|| lflag
) {
323 printf("%6u %8u ", rtm
->rtm_refcnt
, (unsigned int)rtm
->rtm_use
);
325 if (rtm
->rtm_rmx
.rmx_mtu
!= 0)
326 printf("%6u ", rtm
->rtm_rmx
.rmx_mtu
);
331 if (rtm
->rtm_index
!= lastindex
) {
332 if_indextoname(rtm
->rtm_index
, ifname
);
333 lastindex
= rtm
->rtm_index
;
335 printf("%*.*s", WID_IF(addr
.u_sa
.sa_family
),
336 WID_IF(addr
.u_sa
.sa_family
), ifname
);
338 if (rtm
->rtm_rmx
.rmx_expire
) {
342 rtm
->rtm_rmx
.rmx_expire
- time((time_t *)0)) > 0)
343 printf(" %6d", (int)expire_time
);
350 if (rtm
->rtm_rmx
.rmx_rtt
!= 0)
351 printf(" %6u.%03u", rtm
->rtm_rmx
.rmx_rtt
/ 1000,
352 rtm
->rtm_rmx
.rmx_rtt
% 1000);
355 if (rtm
->rtm_rmx
.rmx_rttvar
!= 0)
356 printf(" %6u.%03u", rtm
->rtm_rmx
.rmx_rttvar
/ 1000,
357 rtm
->rtm_rmx
.rmx_rttvar
% 1000);
361 if (rtm
->rtm_rmx
.rmx_recvpipe
!= 0)
362 printf(" %8u", rtm
->rtm_rmx
.rmx_recvpipe
);
365 if (rtm
->rtm_rmx
.rmx_sendpipe
!= 0)
366 printf(" %8u", rtm
->rtm_rmx
.rmx_sendpipe
);
369 if (rtm
->rtm_rmx
.rmx_ssthresh
!= 0)
370 printf(" %8u", rtm
->rtm_rmx
.rmx_ssthresh
);
379 p_sockaddr(struct sockaddr
*sa
, struct sockaddr
*mask
, int flags
, int width
)
381 char workbuf
[128], *cplim
;
384 switch(sa
->sa_family
) {
386 struct sockaddr_in
*sin
= (struct sockaddr_in
*)sa
;
388 if ((sin
->sin_addr
.s_addr
== INADDR_ANY
) &&
390 (ntohl(((struct sockaddr_in
*)mask
)->sin_addr
.s_addr
) == 0L || mask
->sa_len
== 0))
392 else if (flags
& RTF_HOST
)
393 cp
= routename(sin
->sin_addr
.s_addr
);
395 cp
= netname(sin
->sin_addr
.s_addr
,
396 ntohl(((struct sockaddr_in
*)mask
)->
399 cp
= netname(sin
->sin_addr
.s_addr
, 0L);
405 struct sockaddr_in6
*sa6
= (struct sockaddr_in6
*)sa
;
406 struct in6_addr
*in6
= &sa6
->sin6_addr
;
409 * XXX: This is a special workaround for KAME kernels.
410 * sin6_scope_id field of SA should be set in the future.
412 if (IN6_IS_ADDR_LINKLOCAL(in6
) ||
413 IN6_IS_ADDR_MC_NODELOCAL(in6
) ||
414 IN6_IS_ADDR_MC_LINKLOCAL(in6
)) {
415 /* XXX: override is ok? */
416 sa6
->sin6_scope_id
= (u_int32_t
)ntohs(*(u_short
*)&in6
->s6_addr
[2]);
417 *(u_short
*)&in6
->s6_addr
[2] = 0;
420 if (flags
& RTF_HOST
)
421 cp
= routename6(sa6
);
423 cp
= netname6(sa6
, mask
);
425 cp
= netname6(sa6
, NULL
);
431 struct sockaddr_dl
*sdl
= (struct sockaddr_dl
*)sa
;
433 if (sdl
->sdl_nlen
== 0 && sdl
->sdl_alen
== 0 &&
434 sdl
->sdl_slen
== 0) {
435 (void) snprintf(workbuf
, sizeof(workbuf
), "link#%d", sdl
->sdl_index
);
437 switch (sdl
->sdl_type
) {
441 u_char
*lla
= (u_char
*)sdl
->sdl_data
+
445 for (i
= 0; i
< sdl
->sdl_alen
; i
++, lla
++) {
446 cp
+= snprintf(cp
, sizeof(workbuf
) - (cp
- workbuf
), "%s%x", cplim
, *lla
);
462 u_char
*s
= (u_char
*)sa
->sa_data
, *slim
;
464 slim
= sa
->sa_len
+ (u_char
*) sa
;
465 cplim
= cp
+ sizeof(workbuf
) - 6;
466 cp
+= snprintf(cp
, sizeof(workbuf
) - (cp
- workbuf
), "(%d)", sa
->sa_family
);
467 while (s
< slim
&& cp
< cplim
) {
468 cp
+= snprintf(cp
, sizeof(workbuf
) - (cp
- workbuf
), " %02x", *s
++);
470 cp
+= snprintf(cp
, sizeof(workbuf
) - (cp
- workbuf
), "%02x", *s
++);
479 printf("%-*s ", width
, cp
);
481 printf("%-*.*s ", width
, width
, cp
);
486 p_flags(int f
, char *format
)
488 char name
[33], *flags
;
489 struct bits
*p
= bits
;
491 for (flags
= name
; p
->b_mask
; p
++)
495 printf(format
, name
);
499 routename(uint32_t in
)
502 static char line
[MAXHOSTNAMELEN
];
507 hp
= gethostbyaddr((char *)&in
, sizeof (struct in_addr
),
511 //### trimdomain(cp, strlen(cp));
515 strncpy(line
, cp
, sizeof(line
) - 1);
516 line
[sizeof(line
) - 1] = '\0';
518 #define C(x) ((x) & 0xff)
520 snprintf(line
, sizeof(line
), "%u.%u.%u.%u",
521 C(in
>> 24), C(in
>> 16), C(in
>> 8), C(in
));
527 forgemask(uint32_t a
)
533 else if (IN_CLASSB(a
))
541 domask(char *dst
, uint32_t addr
, uint32_t mask
)
545 if (!mask
|| (forgemask(addr
) == mask
)) {
550 for (b
= 0; b
< 32; b
++)
551 if (mask
& (1 << b
)) {
555 for (bb
= b
+1; bb
< 32; bb
++)
556 if (!(mask
& (1 << bb
))) {
557 i
= -1; /* noncontig */
563 snprintf(dst
, sizeof(dst
), "&0x%x", mask
);
565 snprintf(dst
, sizeof(dst
), "/%d", 32-i
);
569 * Return the name of the network whose address is given.
570 * The address is assumed to be that of a net or subnet, not a host.
573 netname(uint32_t in
, uint32_t mask
)
576 static char line
[MAXHOSTNAMELEN
];
577 struct netent
*np
= 0;
578 uint32_t net
, omask
, dmask
;
582 dmask
= forgemask(i
);
586 if (!(np
= getnetbyaddr(i
, AF_INET
)) && net
!= i
)
587 np
= getnetbyaddr(net
, AF_INET
);
590 //### trimdomain(cp, strlen(cp));
594 strncpy(line
, cp
, sizeof(line
) - 1);
598 if ((i
& IN_CLASSA_HOST
) == 0) {
599 snprintf(line
, sizeof(line
), "%u", C(i
>> 24));
604 if ((i
& IN_CLASSB_HOST
) == 0) {
605 snprintf(line
, sizeof(line
), "%u.%u",
606 C(i
>> 24), C(i
>> 16));
611 if ((i
& IN_CLASSC_HOST
) == 0) {
612 snprintf(line
, sizeof(line
), "%u.%u.%u",
613 C(i
>> 24), C(i
>> 16), C(i
>> 8));
618 snprintf(line
, sizeof(line
), "%u.%u.%u.%u",
619 C(i
>> 24), C(i
>> 16), C(i
>> 8), C(i
));
623 domask(line
+strlen(line
), i
, omask
);
629 netname6(struct sockaddr_in6
*sa6
, struct sockaddr
*sam
)
631 static char line
[MAXHOSTNAMELEN
];
633 int masklen
, illegal
= 0, flag
= NI_WITHSCOPEID
;
634 struct in6_addr
*mask
= sam
? &((struct sockaddr_in6
*)sam
)->sin6_addr
: 0;
636 if (sam
&& sam
->sa_len
== 0) {
639 u_char
*p
= (u_char
*)mask
;
640 for (masklen
= 0, lim
= p
+ 16; p
< lim
; p
++) {
674 fprintf(stderr
, "illegal prefixlen\n");
678 if (masklen
== 0 && IN6_IS_ADDR_UNSPECIFIED(&sa6
->sin6_addr
))
682 flag
|= NI_NUMERICHOST
;
683 getnameinfo((struct sockaddr
*)sa6
, sa6
->sin6_len
, line
, sizeof(line
),
687 snprintf(&line
[strlen(line
)], sizeof(line
) - strlen(line
), "/%d", masklen
);
693 routename6(struct sockaddr_in6
*sa6
)
695 static char line
[MAXHOSTNAMELEN
];
696 int flag
= NI_WITHSCOPEID
;
697 /* use local variable for safety */
698 struct sockaddr_in6 sa6_local
= {sizeof(sa6_local
), AF_INET6
, };
700 sa6_local
.sin6_addr
= sa6
->sin6_addr
;
701 sa6_local
.sin6_scope_id
= sa6
->sin6_scope_id
;
704 flag
|= NI_NUMERICHOST
;
706 getnameinfo((struct sockaddr
*)&sa6_local
, sa6_local
.sin6_len
,
707 line
, sizeof(line
), NULL
, 0, flag
);
714 * Print routing statistics
719 struct rtstat rtstat
;
728 mib
[4] = NET_RT_STAT
;
730 len
= sizeof(struct rtstat
);
731 if (sysctl(mib
, 6, &rtstat
, &len
, 0, 0) == -1)
738 mib
[4] = NET_RT_TRASH
;
740 len
= sizeof(rttrash
);
741 if (sysctl(mib
, 6, &rttrash
, &len
, 0, 0) == -1)
744 printf("routing:\n");
746 #define p(f, m) if (rtstat.f || sflag <= 1) \
747 printf(m, rtstat.f, plural(rtstat.f))
749 p(rts_badredirect
, "\t%u bad routing redirect%s\n");
750 p(rts_dynamic
, "\t%u dynamically created route%s\n");
751 p(rts_newgateway
, "\t%u new gateway%s due to redirects\n");
752 p(rts_unreach
, "\t%u destination%s found unreachable\n");
753 p(rts_wildcard
, "\t%u use%s of a wildcard route\n");
754 p(rts_badrtgwroute
, "\t%u lookup%s returned indirect "
755 "routes pointing to indirect gateway route\n");
758 if (rttrash
|| sflag
<= 1)
759 printf("\t%u route%s not in table but not freed\n",
760 rttrash
, plural(rttrash
));