1 /* $NetBSD: route.c,v 1.84 2015/05/25 03:56:20 manu 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.84 2015/05/25 03:56:20 manu 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>
51 #include <net/route.h>
52 #include <netinet/in.h>
53 #include <netatalk/at.h>
54 #include <netmpls/mpls.h>
56 #include <sys/sysctl.h>
58 #include <arpa/inet.h>
71 #define kget(p, d) (kread((u_long)(p), (char *)&(d), sizeof (d)))
74 * XXX we put all of the sockaddr types in here to force the alignment
77 static union sockaddr_union
{
79 struct sockaddr_in u_in
;
80 struct sockaddr_un u_un
;
81 struct sockaddr_at u_at
;
82 struct sockaddr_dl u_dl
;
84 int u_dummy
; /* force word-alignment */
88 struct rtentry rtentry
;
89 struct radix_node rnode
;
90 struct radix_mask rmask
;
92 static struct sockaddr
*kgetsa(const struct sockaddr
*);
93 static void p_tree(struct radix_node
*);
94 static void p_rtnode(void);
95 static void p_krtentry(struct rtentry
*);
98 * Print routing tables.
101 routepr(u_long rtree
)
103 struct radix_node_head
*rnh
, head
;
104 struct radix_node_head
*rt_nodes
[AF_MAX
+1];
107 printf("Routing tables\n");
110 printf("rt_tables: symbol not in namelist\n");
114 kget(rtree
, rt_nodes
);
115 for (i
= 0; i
<= AF_MAX
; i
++) {
116 if ((rnh
= rt_nodes
[i
]) == 0)
119 if (i
== AF_UNSPEC
) {
120 if (Aflag
&& (af
== 0 || af
== 0xff)) {
121 printf("Netmasks:\n");
122 p_tree(head
.rnh_treetop
);
124 } else if (af
== AF_UNSPEC
|| af
== i
) {
128 p_tree(head
.rnh_treetop
);
133 static struct sockaddr
*
134 kgetsa(const struct sockaddr
*dst
)
137 kget(dst
, pt_u
.u_sa
);
138 if (pt_u
.u_sa
.sa_len
> sizeof (pt_u
.u_sa
))
139 kread((u_long
)dst
, (char *)pt_u
.u_data
, pt_u
.u_sa
.sa_len
);
144 p_tree(struct radix_node
*rn
)
149 if (rnode
.rn_b
< 0) {
151 printf("%-8.8lx ", (u_long
) rn
);
152 if (rnode
.rn_flags
& RNF_ROOT
) {
154 printf("(root node)%s",
155 rnode
.rn_dupedkey
? " =>\n" : "\n");
156 } else if (do_rtent
) {
158 p_krtentry(&rtentry
);
162 p_sockaddr(kgetsa((const struct sockaddr
*)rnode
.rn_key
),
166 if ((rn
= rnode
.rn_dupedkey
) != NULL
)
169 if (Aflag
&& do_rtent
) {
170 printf("%-8.8lx ", (u_long
) rn
);
182 struct radix_mask
*rm
= rnode
.rn_mklist
;
185 if (rnode
.rn_b
< 0) {
188 p_sockaddr(kgetsa((const struct sockaddr
*)rnode
.rn_mask
),
193 (void)snprintf(nbuf
, sizeof nbuf
, "(%d)", rnode
.rn_b
);
194 printf("%6.6s %8.8lx : %8.8lx", nbuf
, (u_long
) rnode
.rn_l
,
195 (u_long
) rnode
.rn_r
);
199 (void)snprintf(nbuf
, sizeof nbuf
, " %d refs, ", rmask
.rm_refs
);
200 printf(" mk = %8.8lx {(%d),%s", (u_long
) rm
,
201 -1 - rmask
.rm_b
, rmask
.rm_refs
? nbuf
: " ");
202 if (rmask
.rm_flags
& RNF_NORMAL
) {
203 struct radix_node rnode_aux
;
204 printf(" <normal>, ");
205 kget(rmask
.rm_leaf
, rnode_aux
);
206 p_sockaddr(kgetsa((const struct sockaddr
*)rnode_aux
.rn_mask
),
209 p_sockaddr(kgetsa((const struct sockaddr
*)rmask
.rm_mask
),
212 if ((rm
= rmask
.rm_mklist
) != NULL
)
218 static struct sockaddr
*sockcopy(struct sockaddr
*, union sockaddr_union
*);
221 * copy a sockaddr into an allocated region, allocate at least sockaddr
222 * bytes and zero unused
224 static struct sockaddr
*
225 sockcopy(struct sockaddr
*sp
, union sockaddr_union
*dp
)
229 if (sp
== 0 || sp
->sa_len
== 0)
230 (void)memset(dp
, 0, sizeof (*sp
));
232 len
= (sp
->sa_len
>= sizeof (*sp
)) ? sp
->sa_len
: sizeof (*sp
);
233 (void)memcpy(dp
, sp
, len
);
235 return ((struct sockaddr
*)dp
);
239 p_krtentry(struct rtentry
*rt
)
241 static struct ifnet ifnet
, *lastif
;
242 union sockaddr_union addr_un
, mask_un
;
243 struct sockaddr
*addr
, *mask
;
245 if (Lflag
&& (rt
->rt_flags
& RTF_LLINFO
))
248 memset(&addr_un
, 0, sizeof(addr_un
));
249 memset(&mask_un
, 0, sizeof(mask_un
));
250 addr
= sockcopy(kgetsa(rt_getkey(rt
)), &addr_un
);
252 mask
= sockcopy(kgetsa(rt_mask(rt
)), &mask_un
);
254 mask
= sockcopy(NULL
, &mask_un
);
255 p_addr(addr
, mask
, rt
->rt_flags
, nflag
);
256 p_gwaddr(kgetsa(rt
->rt_gateway
), kgetsa(rt
->rt_gateway
)->sa_family
, nflag
);
257 p_flags(rt
->rt_flags
);
258 printf("%6d %8"PRIu64
" ", rt
->rt_refcnt
, rt
->rt_use
);
259 if (rt
->rt_rmx
.rmx_mtu
)
260 printf("%6"PRIu64
, rt
->rt_rmx
.rmx_mtu
);
263 putchar((rt
->rt_rmx
.rmx_locks
& RTV_MTU
) ? 'L' : ' ');
266 if (rt
->rt_tag
!= NULL
) {
267 const struct sockaddr
*tagsa
= kgetsa(rt
->rt_tag
);
270 if (tagsa
->sa_family
== AF_MPLS
) {
271 tagstr
= mpls_ntoa(tagsa
);
272 if (strlen(tagstr
) < 7)
273 printf("%7s", tagstr
);
275 printf("%s", tagstr
);
284 if (rt
->rt_ifp
!= lastif
) {
285 kget(rt
->rt_ifp
, ifnet
);
288 printf(" %.16s%s", ifnet
.if_xname
,
289 rt
->rt_nodes
[0].rn_dupedkey
? " =>" : "");
294 p_rtrmx(&rt
->rt_rmx
);
299 * Print routing statistics
304 struct rtstat rtstats
;
307 size_t rtsize
= sizeof(rtstats
);
309 if (sysctlbyname("net.route.stats", &rtstats
, &rtsize
,
311 err(1, "rt_stats: sysctl");
312 } else if (off
== 0) {
313 printf("rtstat: symbol not in namelist\n");
316 kread(off
, (char *)&rtstats
, sizeof(rtstats
));
318 printf("routing:\n");
319 printf("\t%llu bad routing redirect%s\n",
320 (unsigned long long)rtstats
.rts_badredirect
,
321 plural(rtstats
.rts_badredirect
));
322 printf("\t%llu dynamically created route%s\n",
323 (unsigned long long)rtstats
.rts_dynamic
,
324 plural(rtstats
.rts_dynamic
));
325 printf("\t%llu new gateway%s due to redirects\n",
326 (unsigned long long)rtstats
.rts_newgateway
,
327 plural(rtstats
.rts_newgateway
));
328 printf("\t%llu destination%s found unreachable\n",
329 (unsigned long long)rtstats
.rts_unreach
,
330 plural(rtstats
.rts_unreach
));
331 printf("\t%llu use%s of a wildcard route\n",
332 (unsigned long long)rtstats
.rts_wildcard
,
333 plural(rtstats
.rts_wildcard
));