No empty .Rs/.Re
[netbsd-mini2440.git] / usr.bin / netstat / route.c
blobbc9f8e39070754c359be8e569e913a84c351de9f
1 /* $NetBSD: route.c,v 1.72 2008/12/29 01:33:03 christos Exp $ */
3 /*
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
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.
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.
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.72 2008/12/29 01:33:03 christos Exp $");
38 #endif
39 #endif /* not lint */
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>
48 #include <net/if.h>
49 #include <net/if_dl.h>
50 #include <net/if_types.h>
51 #define _KERNEL
52 #include <net/route.h>
53 #undef _KERNEL
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>
62 #include <err.h>
63 #include <kvm.h>
64 #include <netdb.h>
65 #include <stdio.h>
66 #include <stdlib.h>
67 #include <string.h>
68 #include <unistd.h>
70 #include "netstat.h"
72 #define kget(p, d) (kread((u_long)(p), (char *)&(d), sizeof (d)))
74 /* alignment constraint for routing socket */
75 #define ROUNDUP(a) \
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
81 * to be correct.
83 static union sockaddr_union {
84 struct sockaddr u_sa;
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;
90 u_short u_data[128];
91 int u_dummy; /* force word-alignment */
92 } pt_u;
94 int do_rtent = 0;
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.
107 void
108 routepr(rtree)
109 u_long rtree;
111 struct radix_node_head *rnh, head;
112 struct radix_node_head *rt_nodes[AF_MAX+1];
113 int i;
115 printf("Routing tables\n");
117 if (rtree == 0) {
118 printf("rt_tables: symbol not in namelist\n");
119 return;
122 kget(rtree, rt_nodes);
123 for (i = 0; i <= AF_MAX; i++) {
124 if ((rnh = rt_nodes[i]) == 0)
125 continue;
126 kget(rnh, head);
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) {
133 pr_family(i);
134 do_rtent = 1;
135 pr_rthdr(i, Aflag);
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);
148 return (&pt_u.u_sa);
151 static void
152 p_tree(rn)
153 struct radix_node *rn;
156 again:
157 kget(rn, rnode);
158 if (rnode.rn_b < 0) {
159 if (Aflag)
160 printf("%-8.8lx ", (u_long) rn);
161 if (rnode.rn_flags & RNF_ROOT) {
162 if (Aflag)
163 printf("(root node)%s",
164 rnode.rn_dupedkey ? " =>\n" : "\n");
165 } else if (do_rtent) {
166 kget(rn, rtentry);
167 p_krtentry(&rtentry);
168 if (Aflag)
169 p_rtnode();
170 } else {
171 p_sockaddr(kgetsa((const struct sockaddr *)rnode.rn_key),
172 NULL, 0, 44);
173 putchar('\n');
175 if ((rn = rnode.rn_dupedkey) != NULL)
176 goto again;
177 } else {
178 if (Aflag && do_rtent) {
179 printf("%-8.8lx ", (u_long) rn);
180 p_rtnode();
182 rn = rnode.rn_r;
183 p_tree(rnode.rn_l);
184 p_tree(rn);
188 static void
189 p_rtnode()
191 struct radix_mask *rm = rnode.rn_mklist;
192 char nbuf[20];
194 if (rnode.rn_b < 0) {
195 if (rnode.rn_mask) {
196 printf("\t mask ");
197 p_sockaddr(kgetsa((const struct sockaddr *)rnode.rn_mask),
198 NULL, 0, -1);
199 } else if (rm == 0)
200 return;
201 } else {
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);
206 while (rm) {
207 kget(rm, rmask);
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),
216 NULL, 0, -1);
217 } else
218 p_sockaddr(kgetsa((const struct sockaddr *)rmask.rm_mask),
219 NULL, 0, -1);
220 putchar('}');
221 if ((rm = rmask.rm_mklist) != NULL)
222 printf(" ->");
224 putchar('\n');
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 *
235 sockcopy(sp, dp)
236 struct sockaddr *sp;
237 union sockaddr_union *dp;
239 int len;
241 if (sp == 0 || sp->sa_len == 0)
242 (void)memset(dp, 0, sizeof (*sp));
243 else {
244 len = (sp->sa_len >= sizeof (*sp)) ? sp->sa_len : sizeof (*sp);
245 (void)memcpy(dp, sp, len);
247 return ((struct sockaddr *)dp);
250 static void
251 p_krtentry(rt)
252 struct rtentry *rt;
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))
259 return;
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);
264 if (rt_mask(rt))
265 mask = sockcopy(kgetsa(rt_mask(rt)), &mask_un);
266 else
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);
274 else
275 printf("%6s", "-");
276 putchar((rt->rt_rmx.rmx_locks & RTV_MTU) ? 'L' : ' ');
277 if (rt->rt_ifp) {
278 if (rt->rt_ifp != lastif) {
279 kget(rt->rt_ifp, ifnet);
280 lastif = rt->rt_ifp;
282 printf(" %.16s%s", ifnet.if_xname,
283 rt->rt_nodes[0].rn_dupedkey ? " =>" : "");
285 putchar('\n');
286 if (vflag) {
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 "
296 "rttvar %10ld%c\n",
297 rt->rt_rmx.rmx_ssthresh,
298 (rt->rt_rmx.rmx_locks & RTV_SSTHRESH) ? 'L' : ' ',
299 rt->rt_rmx.rmx_rtt,
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
312 void
313 rt_stats(off)
314 u_long off;
316 struct rtstat rtstats;
318 if (use_sysctl) {
319 size_t rtsize = sizeof(rtstats);
321 if (sysctlbyname("net.route.stats", &rtstats, &rtsize,
322 NULL, 0) == -1)
323 err(1, "rt_stats: sysctl");
324 } else if (off == 0) {
325 printf("rtstat: symbol not in namelist\n");
326 return;
327 } else
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));
348 void
349 upHex(p0)
350 char *p0;
352 char *p = p0;
354 for (; *p; p++)
355 switch (*p) {
356 case 'a':
357 case 'b':
358 case 'c':
359 case 'd':
360 case 'e':
361 case 'f':
362 *p += ('A' - 'a');