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