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