1 /* $NetBSD: route.c,v 1.72 2008/12/29 01:33:03 christos Exp $ */
4 * Copyright (c) 1983, 1988, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/cdefs.h>
35 static char sccsid
[] = "from: @(#)route.c 8.3 (Berkeley) 3/9/94";
37 __RCSID("$NetBSD: route.c,v 1.72 2008/12/29 01:33:03 christos Exp $");
42 #include <sys/param.h>
43 #include <sys/protosw.h>
44 #include <sys/socket.h>
49 #include <net/if_dl.h>
50 #include <net/if_types.h>
52 #include <net/route.h>
54 #include <netinet/in.h>
55 #include <netatalk/at.h>
56 #include <netiso/iso.h>
58 #include <sys/sysctl.h>
60 #include <arpa/inet.h>
72 #define kget(p, d) (kread((u_long)(p), (char *)&(d), sizeof (d)))
74 /* alignment constraint for routing socket */
76 ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
77 #define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len))
80 * XXX we put all of the sockaddr types in here to force the alignment
83 static union sockaddr_union
{
85 struct sockaddr_in u_in
;
86 struct sockaddr_un u_un
;
87 struct sockaddr_iso u_iso
;
88 struct sockaddr_at u_at
;
89 struct sockaddr_dl u_dl
;
91 int u_dummy
; /* force word-alignment */
95 struct rtentry rtentry
;
96 struct radix_node rnode
;
97 struct radix_mask rmask
;
99 static struct sockaddr
*kgetsa(const struct sockaddr
*);
100 static void p_tree(struct radix_node
*);
101 static void p_rtnode(void);
102 static void p_krtentry(struct rtentry
*);
105 * Print routing tables.
111 struct radix_node_head
*rnh
, head
;
112 struct radix_node_head
*rt_nodes
[AF_MAX
+1];
115 printf("Routing tables\n");
118 printf("rt_tables: symbol not in namelist\n");
122 kget(rtree
, rt_nodes
);
123 for (i
= 0; i
<= AF_MAX
; i
++) {
124 if ((rnh
= rt_nodes
[i
]) == 0)
127 if (i
== AF_UNSPEC
) {
128 if (Aflag
&& (af
== 0 || af
== 0xff)) {
129 printf("Netmasks:\n");
130 p_tree(head
.rnh_treetop
);
132 } else if (af
== AF_UNSPEC
|| af
== i
) {
136 p_tree(head
.rnh_treetop
);
141 static struct sockaddr
*
142 kgetsa(const struct sockaddr
*dst
)
145 kget(dst
, pt_u
.u_sa
);
146 if (pt_u
.u_sa
.sa_len
> sizeof (pt_u
.u_sa
))
147 kread((u_long
)dst
, (char *)pt_u
.u_data
, pt_u
.u_sa
.sa_len
);
153 struct radix_node
*rn
;
158 if (rnode
.rn_b
< 0) {
160 printf("%-8.8lx ", (u_long
) rn
);
161 if (rnode
.rn_flags
& RNF_ROOT
) {
163 printf("(root node)%s",
164 rnode
.rn_dupedkey
? " =>\n" : "\n");
165 } else if (do_rtent
) {
167 p_krtentry(&rtentry
);
171 p_sockaddr(kgetsa((const struct sockaddr
*)rnode
.rn_key
),
175 if ((rn
= rnode
.rn_dupedkey
) != NULL
)
178 if (Aflag
&& do_rtent
) {
179 printf("%-8.8lx ", (u_long
) rn
);
191 struct radix_mask
*rm
= rnode
.rn_mklist
;
194 if (rnode
.rn_b
< 0) {
197 p_sockaddr(kgetsa((const struct sockaddr
*)rnode
.rn_mask
),
202 (void)snprintf(nbuf
, sizeof nbuf
, "(%d)", rnode
.rn_b
);
203 printf("%6.6s %8.8lx : %8.8lx", nbuf
, (u_long
) rnode
.rn_l
,
204 (u_long
) rnode
.rn_r
);
208 (void)snprintf(nbuf
, sizeof nbuf
, " %d refs, ", rmask
.rm_refs
);
209 printf(" mk = %8.8lx {(%d),%s", (u_long
) rm
,
210 -1 - rmask
.rm_b
, rmask
.rm_refs
? nbuf
: " ");
211 if (rmask
.rm_flags
& RNF_NORMAL
) {
212 struct radix_node rnode_aux
;
213 printf(" <normal>, ");
214 kget(rmask
.rm_leaf
, rnode_aux
);
215 p_sockaddr(kgetsa((const struct sockaddr
*)rnode_aux
.rn_mask
),
218 p_sockaddr(kgetsa((const struct sockaddr
*)rmask
.rm_mask
),
221 if ((rm
= rmask
.rm_mklist
) != NULL
)
227 static struct sockaddr
*sockcopy
__P((struct sockaddr
*,
228 union sockaddr_union
*));
231 * copy a sockaddr into an allocated region, allocate at least sockaddr
232 * bytes and zero unused
234 static struct sockaddr
*
237 union sockaddr_union
*dp
;
241 if (sp
== 0 || sp
->sa_len
== 0)
242 (void)memset(dp
, 0, sizeof (*sp
));
244 len
= (sp
->sa_len
>= sizeof (*sp
)) ? sp
->sa_len
: sizeof (*sp
);
245 (void)memcpy(dp
, sp
, len
);
247 return ((struct sockaddr
*)dp
);
254 static struct ifnet ifnet
, *lastif
;
255 union sockaddr_union addr_un
, mask_un
;
256 struct sockaddr
*addr
, *mask
;
258 if (Lflag
&& (rt
->rt_flags
& RTF_LLINFO
))
261 memset(&addr_un
, 0, sizeof(addr_un
));
262 memset(&mask_un
, 0, sizeof(mask_un
));
263 addr
= sockcopy(kgetsa(rt_getkey(rt
)), &addr_un
);
265 mask
= sockcopy(kgetsa(rt_mask(rt
)), &mask_un
);
267 mask
= sockcopy(NULL
, &mask_un
);
268 p_addr(addr
, mask
, rt
->rt_flags
);
269 p_gwaddr(kgetsa(rt
->rt_gateway
), kgetsa(rt
->rt_gateway
)->sa_family
);
270 p_flags(rt
->rt_flags
, "%-6.6s ");
271 printf("%6d %8lu ", rt
->rt_refcnt
, rt
->rt_use
);
272 if (rt
->rt_rmx
.rmx_mtu
)
273 printf("%6lu", rt
->rt_rmx
.rmx_mtu
);
276 putchar((rt
->rt_rmx
.rmx_locks
& RTV_MTU
) ? 'L' : ' ');
278 if (rt
->rt_ifp
!= lastif
) {
279 kget(rt
->rt_ifp
, ifnet
);
282 printf(" %.16s%s", ifnet
.if_xname
,
283 rt
->rt_nodes
[0].rn_dupedkey
? " =>" : "");
287 printf("\texpire %10lld%c recvpipe %10ld%c "
288 "sendpipe %10ld%c\n",
289 (long long)rt
->rt_rmx
.rmx_expire
,
290 (rt
->rt_rmx
.rmx_locks
& RTV_EXPIRE
) ? 'L' : ' ',
291 rt
->rt_rmx
.rmx_recvpipe
,
292 (rt
->rt_rmx
.rmx_locks
& RTV_RPIPE
) ? 'L' : ' ',
293 rt
->rt_rmx
.rmx_sendpipe
,
294 (rt
->rt_rmx
.rmx_locks
& RTV_SPIPE
) ? 'L' : ' ');
295 printf("\tssthresh %10lu%c rtt %10ld%c "
297 rt
->rt_rmx
.rmx_ssthresh
,
298 (rt
->rt_rmx
.rmx_locks
& RTV_SSTHRESH
) ? 'L' : ' ',
300 (rt
->rt_rmx
.rmx_locks
& RTV_RTT
) ? 'L' : ' ',
301 rt
->rt_rmx
.rmx_rttvar
,
302 (rt
->rt_rmx
.rmx_locks
& RTV_RTTVAR
) ? 'L' : ' ');
303 printf("\thopcount %10lu%c\n",
304 rt
->rt_rmx
.rmx_hopcount
,
305 (rt
->rt_rmx
.rmx_locks
& RTV_HOPCOUNT
) ? 'L' : ' ');
310 * Print routing statistics
316 struct rtstat rtstats
;
319 size_t rtsize
= sizeof(rtstats
);
321 if (sysctlbyname("net.route.stats", &rtstats
, &rtsize
,
323 err(1, "rt_stats: sysctl");
324 } else if (off
== 0) {
325 printf("rtstat: symbol not in namelist\n");
328 kread(off
, (char *)&rtstats
, sizeof(rtstats
));
330 printf("routing:\n");
331 printf("\t%llu bad routing redirect%s\n",
332 (unsigned long long)rtstats
.rts_badredirect
,
333 plural(rtstats
.rts_badredirect
));
334 printf("\t%llu dynamically created route%s\n",
335 (unsigned long long)rtstats
.rts_dynamic
,
336 plural(rtstats
.rts_dynamic
));
337 printf("\t%llu new gateway%s due to redirects\n",
338 (unsigned long long)rtstats
.rts_newgateway
,
339 plural(rtstats
.rts_newgateway
));
340 printf("\t%llu destination%s found unreachable\n",
341 (unsigned long long)rtstats
.rts_unreach
,
342 plural(rtstats
.rts_unreach
));
343 printf("\t%llu use%s of a wildcard route\n",
344 (unsigned long long)rtstats
.rts_wildcard
,
345 plural(rtstats
.rts_wildcard
));