route.c revision 1.78.2.2 1 /* $NetBSD: route.c,v 1.78.2.2 2014/05/22 11:42:46 yamt Exp $ */
2
3 /*
4 * Copyright (c) 1983, 1988, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
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.
18 *
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
29 * SUCH DAMAGE.
30 */
31
32 #include <sys/cdefs.h>
33 #ifndef lint
34 #if 0
35 static char sccsid[] = "from: @(#)route.c 8.3 (Berkeley) 3/9/94";
36 #else
37 __RCSID("$NetBSD: route.c,v 1.78.2.2 2014/05/22 11:42:46 yamt Exp $");
38 #endif
39 #endif /* not lint */
40
41 #include <stdbool.h>
42 #include <sys/param.h>
43 #include <sys/protosw.h>
44 #include <sys/socket.h>
45 #include <sys/mbuf.h>
46 #include <sys/un.h>
47
48 #include <net/if.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>
55
56 #include <sys/sysctl.h>
57
58 #include <arpa/inet.h>
59
60 #include <err.h>
61 #include <kvm.h>
62 #include <netdb.h>
63 #include <stdio.h>
64 #include <stdlib.h>
65 #include <string.h>
66 #include <unistd.h>
67
68 #include "netstat.h"
69
70 #define kget(p, d) (kread((u_long)(p), (char *)&(d), sizeof (d)))
71
72 /*
73 * XXX we put all of the sockaddr types in here to force the alignment
74 * to be correct.
75 */
76 static union sockaddr_union {
77 struct sockaddr u_sa;
78 struct sockaddr_in u_in;
79 struct sockaddr_un u_un;
80 struct sockaddr_at u_at;
81 struct sockaddr_dl u_dl;
82 u_short u_data[128];
83 int u_dummy; /* force word-alignment */
84 } pt_u;
85
86 int do_rtent = 0;
87 struct rtentry rtentry;
88 struct radix_node rnode;
89 struct radix_mask rmask;
90
91 static struct sockaddr *kgetsa(const struct sockaddr *);
92 static void p_tree(struct radix_node *);
93 static void p_rtnode(void);
94 static void p_krtentry(struct rtentry *);
95
96 /*
97 * Print routing tables.
98 */
99 void
100 routepr(u_long rtree)
101 {
102 struct radix_node_head *rnh, head;
103 struct radix_node_head *rt_nodes[AF_MAX+1];
104 int i;
105
106 printf("Routing tables\n");
107
108 if (rtree == 0) {
109 printf("rt_tables: symbol not in namelist\n");
110 return;
111 }
112
113 kget(rtree, rt_nodes);
114 for (i = 0; i <= AF_MAX; i++) {
115 if ((rnh = rt_nodes[i]) == 0)
116 continue;
117 kget(rnh, head);
118 if (i == AF_UNSPEC) {
119 if (Aflag && (af == 0 || af == 0xff)) {
120 printf("Netmasks:\n");
121 p_tree(head.rnh_treetop);
122 }
123 } else if (af == AF_UNSPEC || af == i) {
124 pr_family(i);
125 do_rtent = 1;
126 pr_rthdr(i, Aflag);
127 p_tree(head.rnh_treetop);
128 }
129 }
130 }
131
132 static struct sockaddr *
133 kgetsa(const struct sockaddr *dst)
134 {
135
136 kget(dst, pt_u.u_sa);
137 if (pt_u.u_sa.sa_len > sizeof (pt_u.u_sa))
138 kread((u_long)dst, (char *)pt_u.u_data, pt_u.u_sa.sa_len);
139 return (&pt_u.u_sa);
140 }
141
142 static void
143 p_tree(struct radix_node *rn)
144 {
145
146 again:
147 kget(rn, rnode);
148 if (rnode.rn_b < 0) {
149 if (Aflag)
150 printf("%-8.8lx ", (u_long) rn);
151 if (rnode.rn_flags & RNF_ROOT) {
152 if (Aflag)
153 printf("(root node)%s",
154 rnode.rn_dupedkey ? " =>\n" : "\n");
155 } else if (do_rtent) {
156 kget(rn, rtentry);
157 p_krtentry(&rtentry);
158 if (Aflag)
159 p_rtnode();
160 } else {
161 p_sockaddr(kgetsa((const struct sockaddr *)rnode.rn_key),
162 NULL, 0, 44);
163 putchar('\n');
164 }
165 if ((rn = rnode.rn_dupedkey) != NULL)
166 goto again;
167 } else {
168 if (Aflag && do_rtent) {
169 printf("%-8.8lx ", (u_long) rn);
170 p_rtnode();
171 }
172 rn = rnode.rn_r;
173 p_tree(rnode.rn_l);
174 p_tree(rn);
175 }
176 }
177
178 static void
179 p_rtnode(void)
180 {
181 struct radix_mask *rm = rnode.rn_mklist;
182 char nbuf[20];
183
184 if (rnode.rn_b < 0) {
185 if (rnode.rn_mask) {
186 printf("\t mask ");
187 p_sockaddr(kgetsa((const struct sockaddr *)rnode.rn_mask),
188 NULL, 0, -1);
189 } else if (rm == 0)
190 return;
191 } else {
192 (void)snprintf(nbuf, sizeof nbuf, "(%d)", rnode.rn_b);
193 printf("%6.6s %8.8lx : %8.8lx", nbuf, (u_long) rnode.rn_l,
194 (u_long) rnode.rn_r);
195 }
196 while (rm) {
197 kget(rm, rmask);
198 (void)snprintf(nbuf, sizeof nbuf, " %d refs, ", rmask.rm_refs);
199 printf(" mk = %8.8lx {(%d),%s", (u_long) rm,
200 -1 - rmask.rm_b, rmask.rm_refs ? nbuf : " ");
201 if (rmask.rm_flags & RNF_NORMAL) {
202 struct radix_node rnode_aux;
203 printf(" <normal>, ");
204 kget(rmask.rm_leaf, rnode_aux);
205 p_sockaddr(kgetsa((const struct sockaddr *)rnode_aux.rn_mask),
206 NULL, 0, -1);
207 } else
208 p_sockaddr(kgetsa((const struct sockaddr *)rmask.rm_mask),
209 NULL, 0, -1);
210 putchar('}');
211 if ((rm = rmask.rm_mklist) != NULL)
212 printf(" ->");
213 }
214 putchar('\n');
215 }
216
217 static struct sockaddr *sockcopy(struct sockaddr *, union sockaddr_union *);
218
219 /*
220 * copy a sockaddr into an allocated region, allocate at least sockaddr
221 * bytes and zero unused
222 */
223 static struct sockaddr *
224 sockcopy(struct sockaddr *sp, union sockaddr_union *dp)
225 {
226 int len;
227
228 if (sp == 0 || sp->sa_len == 0)
229 (void)memset(dp, 0, sizeof (*sp));
230 else {
231 len = (sp->sa_len >= sizeof (*sp)) ? sp->sa_len : sizeof (*sp);
232 (void)memcpy(dp, sp, len);
233 }
234 return ((struct sockaddr *)dp);
235 }
236
237 static void
238 p_krtentry(struct rtentry *rt)
239 {
240 static struct ifnet ifnet, *lastif;
241 union sockaddr_union addr_un, mask_un;
242 struct sockaddr *addr, *mask;
243
244 if (Lflag && (rt->rt_flags & RTF_LLINFO))
245 return;
246
247 memset(&addr_un, 0, sizeof(addr_un));
248 memset(&mask_un, 0, sizeof(mask_un));
249 addr = sockcopy(kgetsa(rt_getkey(rt)), &addr_un);
250 if (rt_mask(rt))
251 mask = sockcopy(kgetsa(rt_mask(rt)), &mask_un);
252 else
253 mask = sockcopy(NULL, &mask_un);
254 p_addr(addr, mask, rt->rt_flags);
255 p_gwaddr(kgetsa(rt->rt_gateway), kgetsa(rt->rt_gateway)->sa_family);
256 p_flags(rt->rt_flags, "%-6.6s ");
257 printf("%6d %8"PRIu64" ", rt->rt_refcnt, rt->rt_use);
258 if (rt->rt_rmx.rmx_mtu)
259 printf("%6"PRIu64, rt->rt_rmx.rmx_mtu);
260 else
261 printf("%6s", "-");
262 putchar((rt->rt_rmx.rmx_locks & RTV_MTU) ? 'L' : ' ');
263 if (tagflag == 1) {
264 if (rt->rt_tag != NULL) {
265 const struct sockaddr *tagsa = kgetsa(rt->rt_tag);
266 char *tagstr;
267
268 if (tagsa->sa_family == AF_MPLS) {
269 tagstr = mpls_ntoa(tagsa);
270 if (strlen(tagstr) < 7)
271 printf("%7s", tagstr);
272 else
273 printf("%s", tagstr);
274 }
275 else
276 printf("%7s", "-");
277 } else
278 printf("%7s", "-");
279 }
280 if (rt->rt_ifp) {
281 if (rt->rt_ifp != lastif) {
282 kget(rt->rt_ifp, ifnet);
283 lastif = rt->rt_ifp;
284 }
285 printf(" %.16s%s", ifnet.if_xname,
286 rt->rt_nodes[0].rn_dupedkey ? " =>" : "");
287 }
288 putchar('\n');
289 if (vflag)
290 pr_rtrmx(&rt->rt_rmx);
291 }
292
293 void
294 pr_rtrmx(struct rt_metrics *rmx)
295 {
296 printf("\texpire %10"PRId64"%c recvpipe %10"PRIu64"%c "
297 "sendpipe %10"PRIu64"%c\n",
298 (int64_t)rmx->rmx_expire,
299 (rmx->rmx_locks & RTV_EXPIRE) ? 'L' : ' ', rmx->rmx_recvpipe,
300 (rmx->rmx_locks & RTV_RPIPE) ? 'L' : ' ', rmx->rmx_sendpipe,
301 (rmx->rmx_locks & RTV_SPIPE) ? 'L' : ' ');
302 printf("\tssthresh %10"PRIu64"%c rtt %10"PRIu64"%c "
303 "rttvar %10"PRIu64"%c\n", rmx->rmx_ssthresh,
304 (rmx->rmx_locks & RTV_SSTHRESH) ? 'L' : ' ',
305 rmx->rmx_rtt, (rmx->rmx_locks & RTV_RTT) ? 'L' : ' ',
306 rmx->rmx_rttvar, (rmx->rmx_locks & RTV_RTTVAR) ? 'L' : ' ');
307 printf("\thopcount %10"PRIu64"%c\n",
308 rmx->rmx_hopcount, (rmx->rmx_locks & RTV_HOPCOUNT) ? 'L' : ' ');
309 }
310
311 /*
312 * Print routing statistics
313 */
314 void
315 rt_stats(u_long off)
316 {
317 struct rtstat rtstats;
318
319 if (use_sysctl) {
320 size_t rtsize = sizeof(rtstats);
321
322 if (sysctlbyname("net.route.stats", &rtstats, &rtsize,
323 NULL, 0) == -1)
324 err(1, "rt_stats: sysctl");
325 } else if (off == 0) {
326 printf("rtstat: symbol not in namelist\n");
327 return;
328 } else
329 kread(off, (char *)&rtstats, sizeof(rtstats));
330
331 printf("routing:\n");
332 printf("\t%llu bad routing redirect%s\n",
333 (unsigned long long)rtstats.rts_badredirect,
334 plural(rtstats.rts_badredirect));
335 printf("\t%llu dynamically created route%s\n",
336 (unsigned long long)rtstats.rts_dynamic,
337 plural(rtstats.rts_dynamic));
338 printf("\t%llu new gateway%s due to redirects\n",
339 (unsigned long long)rtstats.rts_newgateway,
340 plural(rtstats.rts_newgateway));
341 printf("\t%llu destination%s found unreachable\n",
342 (unsigned long long)rtstats.rts_unreach,
343 plural(rtstats.rts_unreach));
344 printf("\t%llu use%s of a wildcard route\n",
345 (unsigned long long)rtstats.rts_wildcard,
346 plural(rtstats.rts_wildcard));
347 }
348