xref: /netbsd-src/usr.bin/netstat/route.c (revision 8b0f9554ff8762542c4defc4f70e1eb76fb508fa)
1 /*	$NetBSD: route.c,v 1.69 2007/07/19 20:51:04 dyoung 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.69 2007/07/19 20:51:04 dyoung Exp $");
38 #endif
39 #endif /* not lint */
40 
41 #include <sys/param.h>
42 #include <sys/protosw.h>
43 #include <sys/socket.h>
44 #include <sys/mbuf.h>
45 #include <sys/un.h>
46 
47 #include <net/if.h>
48 #include <net/if_dl.h>
49 #include <net/if_types.h>
50 #define _KERNEL
51 #include <net/route.h>
52 #undef _KERNEL
53 #include <netinet/in.h>
54 #include <netatalk/at.h>
55 #include <netiso/iso.h>
56 
57 #ifdef NS
58 #include <netns/ns.h>
59 #endif
60 
61 #include <sys/sysctl.h>
62 
63 #include <arpa/inet.h>
64 
65 #include <err.h>
66 #include <kvm.h>
67 #include <netdb.h>
68 #include <stdio.h>
69 #include <stdlib.h>
70 #include <string.h>
71 #include <unistd.h>
72 
73 #include "netstat.h"
74 
75 #define kget(p, d) (kread((u_long)(p), (char *)&(d), sizeof (d)))
76 
77 /* alignment constraint for routing socket */
78 #define ROUNDUP(a) \
79 	((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
80 #define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len))
81 
82 /*
83  * XXX we put all of the sockaddr types in here to force the alignment
84  * to be correct.
85  */
86 static union sockaddr_union {
87 	struct	sockaddr u_sa;
88 	struct	sockaddr_in u_in;
89 	struct	sockaddr_un u_un;
90 	struct	sockaddr_iso u_iso;
91 	struct	sockaddr_at u_at;
92 	struct	sockaddr_dl u_dl;
93 #ifdef NS
94 	struct	sockaddr_ns u_ns;
95 #endif
96 	u_short	u_data[128];
97 	int u_dummy;		/* force word-alignment */
98 } pt_u;
99 
100 int	do_rtent = 0;
101 struct	rtentry rtentry;
102 struct	radix_node rnode;
103 struct	radix_mask rmask;
104 
105 static struct sockaddr *kgetsa(const struct sockaddr *);
106 static void p_tree(struct radix_node *);
107 static void p_rtnode(void);
108 static void p_krtentry(struct rtentry *);
109 
110 /*
111  * Print routing tables.
112  */
113 void
114 routepr(rtree)
115 	u_long rtree;
116 {
117 	struct radix_node_head *rnh, head;
118 	struct radix_node_head *rt_tables[AF_MAX+1];
119 	int i;
120 
121 	printf("Routing tables\n");
122 
123 	if (rtree == 0) {
124 		printf("rt_tables: symbol not in namelist\n");
125 		return;
126 	}
127 
128 	kget(rtree, rt_tables);
129 	for (i = 0; i <= AF_MAX; i++) {
130 		if ((rnh = rt_tables[i]) == 0)
131 			continue;
132 		kget(rnh, head);
133 		if (i == AF_UNSPEC) {
134 			if (Aflag && (af == 0 || af == 0xff)) {
135 				printf("Netmasks:\n");
136 				p_tree(head.rnh_treetop);
137 			}
138 		} else if (af == AF_UNSPEC || af == i) {
139 			pr_family(i);
140 			do_rtent = 1;
141 			pr_rthdr(i, Aflag);
142 			p_tree(head.rnh_treetop);
143 		}
144 	}
145 }
146 
147 static struct sockaddr *
148 kgetsa(const struct sockaddr *dst)
149 {
150 
151 	kget(dst, pt_u.u_sa);
152 	if (pt_u.u_sa.sa_len > sizeof (pt_u.u_sa))
153 		kread((u_long)dst, (char *)pt_u.u_data, pt_u.u_sa.sa_len);
154 	return (&pt_u.u_sa);
155 }
156 
157 static void
158 p_tree(rn)
159 	struct radix_node *rn;
160 {
161 
162 again:
163 	kget(rn, rnode);
164 	if (rnode.rn_b < 0) {
165 		if (Aflag)
166 			printf("%-8.8lx ", (u_long) rn);
167 		if (rnode.rn_flags & RNF_ROOT) {
168 			if (Aflag)
169 				printf("(root node)%s",
170 				    rnode.rn_dupedkey ? " =>\n" : "\n");
171 		} else if (do_rtent) {
172 			kget(rn, rtentry);
173 			p_krtentry(&rtentry);
174 			if (Aflag)
175 				p_rtnode();
176 		} else {
177 			p_sockaddr(kgetsa((struct sockaddr *)rnode.rn_key),
178 			    NULL, 0, 44);
179 			putchar('\n');
180 		}
181 		if ((rn = rnode.rn_dupedkey) != NULL)
182 			goto again;
183 	} else {
184 		if (Aflag && do_rtent) {
185 			printf("%-8.8lx ", (u_long) rn);
186 			p_rtnode();
187 		}
188 		rn = rnode.rn_r;
189 		p_tree(rnode.rn_l);
190 		p_tree(rn);
191 	}
192 }
193 
194 static void
195 p_rtnode()
196 {
197 	struct radix_mask *rm = rnode.rn_mklist;
198 	char	nbuf[20];
199 
200 	if (rnode.rn_b < 0) {
201 		if (rnode.rn_mask) {
202 			printf("\t  mask ");
203 			p_sockaddr(kgetsa((struct sockaddr *)rnode.rn_mask),
204 				    NULL, 0, -1);
205 		} else if (rm == 0)
206 			return;
207 	} else {
208 		(void)snprintf(nbuf, sizeof nbuf, "(%d)", rnode.rn_b);
209 		printf("%6.6s %8.8lx : %8.8lx", nbuf, (u_long) rnode.rn_l,
210 		    (u_long) rnode.rn_r);
211 	}
212 	while (rm) {
213 		kget(rm, rmask);
214 		(void)snprintf(nbuf, sizeof nbuf, " %d refs, ", rmask.rm_refs);
215 		printf(" mk = %8.8lx {(%d),%s", (u_long) rm,
216 		    -1 - rmask.rm_b, rmask.rm_refs ? nbuf : " ");
217 		if (rmask.rm_flags & RNF_NORMAL) {
218 			struct radix_node rnode_aux;
219 			printf(" <normal>, ");
220 			kget(rmask.rm_leaf, rnode_aux);
221 			p_sockaddr(kgetsa((struct sockaddr *)rnode_aux.rn_mask),
222 				    NULL, 0, -1);
223 		} else
224 			p_sockaddr(kgetsa((struct sockaddr *)rmask.rm_mask),
225 			    NULL, 0, -1);
226 		putchar('}');
227 		if ((rm = rmask.rm_mklist) != NULL)
228 			printf(" ->");
229 	}
230 	putchar('\n');
231 }
232 
233 static struct sockaddr *sockcopy __P((struct sockaddr *,
234     union sockaddr_union *));
235 
236 /*
237  * copy a sockaddr into an allocated region, allocate at least sockaddr
238  * bytes and zero unused
239  */
240 static struct sockaddr *
241 sockcopy(sp, dp)
242 	struct sockaddr *sp;
243 	union sockaddr_union *dp;
244 {
245 	int len;
246 
247 	if (sp == 0 || sp->sa_len == 0)
248 		(void)memset(dp, 0, sizeof (*sp));
249 	else {
250 		len = (sp->sa_len >= sizeof (*sp)) ? sp->sa_len : sizeof (*sp);
251 		(void)memcpy(dp, sp, len);
252 	}
253 	return ((struct sockaddr *)dp);
254 }
255 
256 static void
257 p_krtentry(rt)
258 	struct rtentry *rt;
259 {
260 	static struct ifnet ifnet, *lastif;
261 	union sockaddr_union addr_un, mask_un;
262 	struct sockaddr *addr, *mask;
263 	int af;
264 
265 	if (Lflag && (rt->rt_flags & RTF_LLINFO))
266 		return;
267 
268 	memset(&addr_un, 0, sizeof(addr_un));
269 	memset(&mask_un, 0, sizeof(mask_un));
270 	addr = sockcopy(kgetsa(rt_getkey(rt)), &addr_un);
271 	af = addr->sa_family;
272 	if (rt_mask(rt))
273 		mask = sockcopy(kgetsa(rt_mask(rt)), &mask_un);
274 	else
275 		mask = sockcopy(NULL, &mask_un);
276 	p_addr(addr, mask, rt->rt_flags);
277 	p_gwaddr(kgetsa(rt->rt_gateway), kgetsa(rt->rt_gateway)->sa_family);
278 	p_flags(rt->rt_flags, "%-6.6s ");
279 	printf("%6d %8lu ", rt->rt_refcnt, rt->rt_use);
280 	if (rt->rt_rmx.rmx_mtu)
281 		printf("%6lu", rt->rt_rmx.rmx_mtu);
282 	else
283 		printf("%6s", "-");
284 	putchar((rt->rt_rmx.rmx_locks & RTV_MTU) ? 'L' : ' ');
285 	if (rt->rt_ifp) {
286 		if (rt->rt_ifp != lastif) {
287 			kget(rt->rt_ifp, ifnet);
288 			lastif = rt->rt_ifp;
289 		}
290 		printf(" %.16s%s", ifnet.if_xname,
291 			rt->rt_nodes[0].rn_dupedkey ? " =>" : "");
292 	}
293 	putchar('\n');
294  	if (vflag) {
295  		printf("\texpire   %10lu%c  recvpipe %10ld%c  "
296 		       "sendpipe %10ld%c\n",
297  			rt->rt_rmx.rmx_expire,
298  			(rt->rt_rmx.rmx_locks & RTV_EXPIRE) ? 'L' : ' ',
299  			rt->rt_rmx.rmx_recvpipe,
300  			(rt->rt_rmx.rmx_locks & RTV_RPIPE) ? 'L' : ' ',
301  			rt->rt_rmx.rmx_sendpipe,
302  			(rt->rt_rmx.rmx_locks & RTV_SPIPE) ? 'L' : ' ');
303  		printf("\tssthresh %10lu%c  rtt      %10ld%c  "
304 		       "rttvar   %10ld%c\n",
305  			rt->rt_rmx.rmx_ssthresh,
306  			(rt->rt_rmx.rmx_locks & RTV_SSTHRESH) ? 'L' : ' ',
307  			rt->rt_rmx.rmx_rtt,
308  			(rt->rt_rmx.rmx_locks & RTV_RTT) ? 'L' : ' ',
309  			rt->rt_rmx.rmx_rttvar,
310 			(rt->rt_rmx.rmx_locks & RTV_RTTVAR) ? 'L' : ' ');
311  		printf("\thopcount %10lu%c\n",
312  			rt->rt_rmx.rmx_hopcount,
313 			(rt->rt_rmx.rmx_locks & RTV_HOPCOUNT) ? 'L' : ' ');
314  	}
315 }
316 
317 /*
318  * Print routing statistics
319  */
320 void
321 rt_stats(off)
322 	u_long off;
323 {
324 	struct rtstat rtstat;
325 
326 	if (use_sysctl) {
327 		size_t rtsize = sizeof(rtstat);
328 
329 		if (sysctlbyname("net.route.stats", &rtstat, &rtsize,
330 		    NULL, 0) == -1)
331 			err(1, "rt_stats: sysctl");
332 	} else 	if (off == 0) {
333 		printf("rtstat: symbol not in namelist\n");
334 		return;
335 	} else
336 		kread(off, (char *)&rtstat, sizeof (rtstat));
337 
338 	printf("routing:\n");
339 	printf("\t%llu bad routing redirect%s\n",
340 		(unsigned long long)rtstat.rts_badredirect,
341 		plural(rtstat.rts_badredirect));
342 	printf("\t%llu dynamically created route%s\n",
343 		(unsigned long long)rtstat.rts_dynamic,
344 		plural(rtstat.rts_dynamic));
345 	printf("\t%llu new gateway%s due to redirects\n",
346 		(unsigned long long)rtstat.rts_newgateway,
347 		plural(rtstat.rts_newgateway));
348 	printf("\t%llu destination%s found unreachable\n",
349 		(unsigned long long)rtstat.rts_unreach,
350 		plural(rtstat.rts_unreach));
351 	printf("\t%llu use%s of a wildcard route\n",
352 		(unsigned long long)rtstat.rts_wildcard,
353 		plural(rtstat.rts_wildcard));
354 }
355 
356 #ifdef NS
357 short ns_nullh[] = {0,0,0};
358 short ns_bh[] = {-1,-1,-1};
359 
360 char *
361 ns_print(sa)
362 	struct sockaddr *sa;
363 {
364 	struct sockaddr_ns *sns = (struct sockaddr_ns*)sa;
365 	struct ns_addr work;
366 	union {
367 		union	ns_net net_e;
368 		u_long	long_e;
369 	} net;
370 	u_short port;
371 	static char mybuf[50], cport[10], chost[25];
372 	char *host = "";
373 	char *p;
374 	u_char *q;
375 
376 	work = sns->sns_addr;
377 	port = ntohs(work.x_port);
378 	work.x_port = 0;
379 	net.net_e  = work.x_net;
380 	if (ns_nullhost(work) && net.long_e == 0) {
381 		if (port ) {
382 			(void)snprintf(mybuf, sizeof mybuf, "*.%xH", port);
383 			upHex(mybuf);
384 		} else
385 			(void)snprintf(mybuf, sizeof mybuf, "*.*");
386 		return (mybuf);
387 	}
388 
389 	if (memcmp(ns_bh, work.x_host.c_host, 6) == 0) {
390 		host = "any";
391 	} else if (memcmp(ns_nullh, work.x_host.c_host, 6) == 0) {
392 		host = "*";
393 	} else {
394 		q = work.x_host.c_host;
395 		(void)snprintf(chost, sizeof chost, "%02x%02x%02x%02x%02x%02xH",
396 			q[0], q[1], q[2], q[3], q[4], q[5]);
397 		for (p = chost; *p == '0' && p < chost + 12; p++)
398 			continue;
399 		host = p;
400 	}
401 	if (port)
402 		(void)snprintf(cport, sizeof cport, ".%xH", htons(port));
403 	else
404 		*cport = 0;
405 
406 	(void)snprintf(mybuf, sizeof mybuf, "%xH.%s%s", (int)ntohl(net.long_e),
407 	    host, cport);
408 	upHex(mybuf);
409 	return (mybuf);
410 }
411 
412 char *
413 ns_phost(sa)
414 	struct sockaddr *sa;
415 {
416 	struct sockaddr_ns *sns = (struct sockaddr_ns *)sa;
417 	struct sockaddr_ns work;
418 	static union ns_net ns_zeronet;
419 	char *p;
420 
421 	work = *sns;
422 	work.sns_addr.x_port = 0;
423 	work.sns_addr.x_net = ns_zeronet;
424 
425 	p = ns_print((struct sockaddr *)&work);
426 	if (strncmp("0H.", p, 3) == 0)
427 		p += 3;
428 	return (p);
429 }
430 #endif
431 
432 void
433 upHex(p0)
434 	char *p0;
435 {
436 	char *p = p0;
437 
438 	for (; *p; p++)
439 		switch (*p) {
440 		case 'a':
441 		case 'b':
442 		case 'c':
443 		case 'd':
444 		case 'e':
445 		case 'f':
446 			*p += ('A' - 'a');
447 		}
448 }
449 
450 
451