xref: /openbsd-src/usr.bin/netstat/route.c (revision 50b7afb2c2c0993b0894d4e34bf857cb13ed9c80)
1 /*	$OpenBSD: route.c,v 1.93 2013/11/25 12:48:39 bluhm Exp $	*/
2 /*	$NetBSD: route.c,v 1.15 1996/05/07 02:55:06 thorpej Exp $	*/
3 
4 /*
5  * Copyright (c) 1983, 1988, 1993
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the University nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32 
33 #include <sys/param.h>
34 #include <sys/protosw.h>
35 #include <sys/socket.h>
36 
37 #include <net/if.h>
38 #include <net/if_var.h>
39 #include <net/if_dl.h>
40 #include <net/if_types.h>
41 #define _KERNEL
42 #include <net/route.h>
43 #undef _KERNEL
44 #include <netinet/in.h>
45 #include <arpa/inet.h>
46 
47 #include <sys/sysctl.h>
48 
49 #include <err.h>
50 #include <limits.h>
51 #include <netdb.h>
52 #include <stdio.h>
53 #include <stdlib.h>
54 #include <string.h>
55 #include <unistd.h>
56 
57 #ifndef INET
58 #define INET
59 #endif
60 
61 #include <netinet/ip_ipsp.h>
62 #include "netstat.h"
63 
64 /* alignment constraint for routing socket */
65 #define ROUNDUP(a) \
66 	((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
67 #define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len))
68 
69 struct radix_node_head ***rt_head;
70 struct radix_node_head ***rnt;
71 struct radix_node_head *rt_tables[AF_MAX+1];	/* provides enough space */
72 u_int8_t		  af2rtafidx[AF_MAX+1];
73 
74 static union {
75 	struct		sockaddr u_sa;
76 	u_int32_t	u_data[64];
77 	int		u_dummy;	/* force word-alignment */
78 } pt_u;
79 
80 int	do_rtent = 0;
81 struct	rtentry rtentry;
82 struct	radix_node rnode;
83 struct	radix_mask rmask;
84 
85 static struct sockaddr *kgetsa(struct sockaddr *);
86 static void p_tree(struct radix_node *);
87 static void p_rtnode(void);
88 static void p_rtflags(u_char);
89 static void p_krtentry(struct rtentry *);
90 static void encap_print(struct rtentry *);
91 
92 /*
93  * Print routing tables.
94  */
95 void
96 routepr(u_long rtree, u_long mtree, u_long af2idx, u_long rtbl_id_max,
97     u_int tableid)
98 {
99 	struct radix_node_head *rnh, head;
100 	int i, idxmax = 0;
101 	u_int rtidxmax;
102 
103 	printf("Routing tables\n");
104 
105 	if (rtree == 0 || af2idx == 0) {
106 		printf("rt_tables: symbol not in namelist\n");
107 		return;
108 	}
109 
110 	kread((u_long)rtree, &rt_head, sizeof(rt_head));
111 	kread((u_long)rtbl_id_max, &rtidxmax, sizeof(rtidxmax));
112 	kread((long)af2idx, &af2rtafidx, sizeof(af2rtafidx));
113 
114 	for (i = 0; i <= AF_MAX; i++) {
115 		if (af2rtafidx[i] > idxmax)
116 			idxmax = af2rtafidx[i];
117 	}
118 
119 	if ((rnt = calloc(rtidxmax + 1, sizeof(struct radix_node_head **))) ==
120 	    NULL)
121 		err(1, NULL);
122 
123 	kread((u_long)rt_head, rnt, (rtidxmax + 1) *
124 	    sizeof(struct radix_node_head **));
125 	if (tableid > rtidxmax || rnt[tableid] == NULL) {
126 		printf("Bad table %u\n", tableid);
127 		return;
128 	}
129 	kread((u_long)rnt[tableid], rt_tables, (idxmax + 1) * sizeof(rnh));
130 
131 	for (i = 0; i <= AF_MAX; i++) {
132 		if (i == AF_UNSPEC) {
133 			if (Aflag && (af == AF_UNSPEC || af == 0xff)) {
134 				kread(mtree, &rnh, sizeof(rnh));
135 				kread((u_long)rnh, &head, sizeof(head));
136 				printf("Netmasks:\n");
137 				p_tree(head.rnh_treetop);
138 			}
139 			continue;
140 		}
141 		if (af2rtafidx[i] == 0)
142 			/* no table for this AF */
143 			continue;
144 		if ((rnh = rt_tables[af2rtafidx[i]]) == NULL)
145 			continue;
146 		kread((u_long)rnh, &head, sizeof(head));
147 		if (af == AF_UNSPEC || af == i) {
148 			pr_family(i);
149 			do_rtent = 1;
150 			pr_rthdr(i, Aflag);
151 			p_tree(head.rnh_treetop);
152 		}
153 	}
154 }
155 
156 static struct sockaddr *
157 kgetsa(struct sockaddr *dst)
158 {
159 
160 	kread((u_long)dst, &pt_u.u_sa, sizeof(pt_u.u_sa));
161 	if (pt_u.u_sa.sa_len > sizeof (pt_u.u_sa))
162 		kread((u_long)dst, pt_u.u_data, pt_u.u_sa.sa_len);
163 	return (&pt_u.u_sa);
164 }
165 
166 static void
167 p_tree(struct radix_node *rn)
168 {
169 
170 again:
171 	kread((u_long)rn, &rnode, sizeof(rnode));
172 	if (rnode.rn_b < 0) {
173 		if (Aflag)
174 			printf("%-16p ", hideroot ? 0 : rn);
175 		if (rnode.rn_flags & RNF_ROOT) {
176 			if (Aflag)
177 				printf("(root node)%s",
178 				    rnode.rn_dupedkey ? " =>\n" : "\n");
179 		} else if (do_rtent) {
180 			kread((u_long)rn, &rtentry, sizeof(rtentry));
181 			p_krtentry(&rtentry);
182 			if (Aflag)
183 				p_rtnode();
184 		} else {
185 			p_sockaddr(kgetsa((struct sockaddr *)rnode.rn_key),
186 			    0, 0, 44);
187 			putchar('\n');
188 		}
189 		if ((rn = rnode.rn_dupedkey))
190 			goto again;
191 	} else {
192 		if (Aflag && do_rtent) {
193 			printf("%-16p ", hideroot ? 0 : rn);
194 			p_rtnode();
195 		}
196 		rn = rnode.rn_r;
197 		p_tree(rnode.rn_l);
198 		p_tree(rn);
199 	}
200 }
201 
202 static void
203 p_rtflags(u_char flags)
204 {
205 	putchar('<');
206 	if (flags & RNF_NORMAL)
207 		putchar('N');
208 	if (flags & RNF_ROOT)
209 		putchar('R');
210 	if (flags & RNF_ACTIVE)
211 		putchar('A');
212 	if (flags & ~(RNF_NORMAL | RNF_ROOT | RNF_ACTIVE))
213 		printf("/0x%02x", flags);
214 	putchar('>');
215 }
216 
217 char	nbuf[25];
218 
219 static void
220 p_rtnode(void)
221 {
222 	struct radix_mask *rm = rnode.rn_mklist;
223 
224 	if (rnode.rn_b < 0) {
225 		snprintf(nbuf, sizeof nbuf, " => %p",
226 		    hideroot ? 0 : rnode.rn_dupedkey);
227 		printf("\t  (%p)%s", hideroot ? 0 : rnode.rn_p,
228 		    rnode.rn_dupedkey ? nbuf : "");
229 		if (rnode.rn_mask) {
230 			printf(" mask ");
231 			p_sockaddr(kgetsa((struct sockaddr *)rnode.rn_mask),
232 			    0, 0, -1);
233 		} else if (rm == NULL) {
234 			putchar('\n');
235 			return;
236 		}
237 	} else {
238 		snprintf(nbuf, sizeof nbuf, "(%d)", rnode.rn_b);
239 		printf("%6.6s (%p) %16p : %16p", nbuf,
240 		    hideroot ? 0 : rnode.rn_p,
241 		    hideroot ? 0 : rnode.rn_l,
242 		    hideroot ? 0 : rnode.rn_r);
243 	}
244 
245 	putchar(' ');
246 	p_rtflags(rnode.rn_flags);
247 
248 	while (rm) {
249 		kread((u_long)rm, &rmask, sizeof(rmask));
250 		snprintf(nbuf, sizeof nbuf, " %d refs, ", rmask.rm_refs);
251 		printf("\n\tmk = %p {(%d),%s",
252 		    hideroot ? 0 : rm,
253 		    -1 - rmask.rm_b, rmask.rm_refs ? nbuf : " ");
254 		p_rtflags(rmask.rm_flags);
255 		printf(", ");
256 		if (rmask.rm_flags & RNF_NORMAL) {
257 			struct radix_node rnode_aux;
258 
259 			printf("leaf = %p ", hideroot ? 0 : rmask.rm_leaf);
260 			kread((u_long)rmask.rm_leaf, &rnode_aux, sizeof(rnode_aux));
261 			p_sockaddr(kgetsa((struct sockaddr *)rnode_aux.rn_mask),
262 			    0, 0, -1);
263 		} else
264 			p_sockaddr(kgetsa((struct sockaddr *)rmask.rm_mask),
265 			    0, 0, -1);
266 		putchar('}');
267 		if ((rm = rmask.rm_mklist))
268 			printf(" ->");
269 	}
270 	putchar('\n');
271 }
272 
273 static void
274 p_krtentry(struct rtentry *rt)
275 {
276 	static struct ifnet ifnet, *lastif;
277 	struct sockaddr_storage sock1, sock2;
278 	struct sockaddr *sa = (struct sockaddr *)&sock1;
279 	struct sockaddr *mask = (struct sockaddr *)&sock2;
280 
281 	bcopy(kgetsa(rt_key(rt)), sa, sizeof(struct sockaddr));
282 	if (sa->sa_len > sizeof(struct sockaddr))
283 		bcopy(kgetsa(rt_key(rt)), sa, sa->sa_len);
284 
285 	if (sa->sa_family == PF_KEY) {
286 		encap_print(rt);
287 		return;
288 	}
289 
290 	if (rt_mask(rt)) {
291 		bcopy(kgetsa(rt_mask(rt)), mask, sizeof(struct sockaddr));
292 		if (sa->sa_len > sizeof(struct sockaddr))
293 			bcopy(kgetsa(rt_mask(rt)), mask, sa->sa_len);
294 	} else
295 		mask = 0;
296 
297 	p_addr(sa, mask, rt->rt_flags);
298 	p_gwaddr(kgetsa(rt->rt_gateway), sa->sa_family);
299 	p_flags(rt->rt_flags, "%-6.6s ");
300 	printf("%5u %8lld ", rt->rt_refcnt, rt->rt_use);
301 	if (rt->rt_rmx.rmx_mtu)
302 		printf("%5u ", rt->rt_rmx.rmx_mtu);
303 	else
304 		printf("%5s ", "-");
305 	putchar((rt->rt_rmx.rmx_locks & RTV_MTU) ? 'L' : ' ');
306 	printf("  %2d", rt->rt_priority);
307 
308 	if (rt->rt_ifp) {
309 		if (rt->rt_ifp != lastif) {
310 			kread((u_long)rt->rt_ifp, &ifnet, sizeof(ifnet));
311 			lastif = rt->rt_ifp;
312 		}
313 		printf(" %.16s%s", ifnet.if_xname,
314 		    rt->rt_nodes[0].rn_dupedkey ? " =>" : "");
315 	}
316 	putchar('\n');
317 	if (vflag)
318 		printf("\texpire   %10lld%c\n",
319 		    (long long)rt->rt_rmx.rmx_expire,
320 		    (rt->rt_rmx.rmx_locks & RTV_EXPIRE) ? 'L' : ' ');
321 }
322 
323 /*
324  * Print routing statistics
325  */
326 void
327 rt_stats(void)
328 {
329 	struct rtstat rtstat;
330 	int mib[6];
331 	size_t size;
332 
333 	mib[0] = CTL_NET;
334 	mib[1] = PF_ROUTE;
335 	mib[2] = 0;
336 	mib[3] = 0;
337 	mib[4] = NET_RT_STATS;
338 	mib[5] = 0;
339 	size = sizeof (rtstat);
340 
341 	if (sysctl(mib, 6, &rtstat, &size, NULL, 0) < 0) {
342 		perror("sysctl of routing table statistics");
343 		exit(1);
344 	}
345 
346 	printf("routing:\n");
347 	printf("\t%u bad routing redirect%s\n",
348 	    rtstat.rts_badredirect, plural(rtstat.rts_badredirect));
349 	printf("\t%u dynamically created route%s\n",
350 	    rtstat.rts_dynamic, plural(rtstat.rts_dynamic));
351 	printf("\t%u new gateway%s due to redirects\n",
352 	    rtstat.rts_newgateway, plural(rtstat.rts_newgateway));
353 	printf("\t%u destination%s found unreachable\n",
354 	    rtstat.rts_unreach, plural(rtstat.rts_unreach));
355 	printf("\t%u use%s of a wildcard route\n",
356 	    rtstat.rts_wildcard, plural(rtstat.rts_wildcard));
357 }
358 
359 static void
360 encap_print(struct rtentry *rt)
361 {
362 	struct sockaddr_encap sen1, sen2, sen3;
363 	struct ipsec_policy ipo;
364 	struct sockaddr_in6 s61, s62;
365 
366 	bcopy(kgetsa(rt_key(rt)), &sen1, sizeof(sen1));
367 	bcopy(kgetsa(rt_mask(rt)), &sen2, sizeof(sen2));
368 	bcopy(kgetsa(rt->rt_gateway), &sen3, sizeof(sen3));
369 
370 	if (sen1.sen_type == SENT_IP4) {
371 		printf("%-18s %-5u ", netname4(sen1.sen_ip_src.s_addr,
372 		    sen2.sen_ip_src.s_addr), ntohs(sen1.sen_sport));
373 		printf("%-18s %-5u %-5u ", netname4(sen1.sen_ip_dst.s_addr,
374 		    sen2.sen_ip_dst.s_addr),
375 		    ntohs(sen1.sen_dport), sen1.sen_proto);
376 	}
377 
378 	if (sen1.sen_type == SENT_IP6) {
379 		bzero(&s61, sizeof(s61));
380 		bzero(&s62, sizeof(s62));
381 		s61.sin6_family = s62.sin6_family = AF_INET6;
382 		s61.sin6_len = s62.sin6_len = sizeof(s61);
383 		bcopy(&sen1.sen_ip6_src, &s61.sin6_addr, sizeof(struct in6_addr));
384 #ifdef __KAME__
385 		if (IN6_IS_ADDR_LINKLOCAL(&s61.sin6_addr) ||
386 		    IN6_IS_ADDR_MC_LINKLOCAL(&s61.sin6_addr) ||
387 		    IN6_IS_ADDR_MC_INTFACELOCAL(&s61.sin6_addr)) {
388 			s61.sin6_scope_id =
389 			    ((u_int16_t)s61.sin6_addr.s6_addr[2] << 8) |
390 			    s61.sin6_addr.s6_addr[3];
391 			s61.sin6_addr.s6_addr[2] = s61.sin6_addr.s6_addr[3] = 0;
392 		}
393 #endif
394 		bcopy(&sen2.sen_ip6_src, &s62.sin6_addr, sizeof(struct in6_addr));
395 #ifdef __KAME__
396 		if (IN6_IS_ADDR_LINKLOCAL(&s62.sin6_addr) ||
397 		    IN6_IS_ADDR_MC_LINKLOCAL(&s62.sin6_addr) ||
398 		    IN6_IS_ADDR_MC_INTFACELOCAL(&s62.sin6_addr)) {
399 			s62.sin6_scope_id =
400 			    ((u_int16_t)s62.sin6_addr.s6_addr[2] << 8) |
401 			    s62.sin6_addr.s6_addr[3];
402 			s62.sin6_addr.s6_addr[2] = s62.sin6_addr.s6_addr[3] = 0;
403 		}
404 #endif
405 
406 		printf("%-42s %-5u ", netname6(&s61, &s62),
407 		    ntohs(sen1.sen_ip6_sport));
408 
409 		bzero(&s61, sizeof(s61));
410 		bzero(&s62, sizeof(s62));
411 		s61.sin6_family = s62.sin6_family = AF_INET6;
412 		s61.sin6_len = s62.sin6_len = sizeof(s61);
413 		bcopy(&sen1.sen_ip6_dst, &s61.sin6_addr, sizeof(struct in6_addr));
414 #ifdef __KAME__
415 		if (IN6_IS_ADDR_LINKLOCAL(&s61.sin6_addr) ||
416 		    IN6_IS_ADDR_MC_LINKLOCAL(&s61.sin6_addr) ||
417 		    IN6_IS_ADDR_MC_INTFACELOCAL(&s61.sin6_addr)) {
418 			s61.sin6_scope_id =
419 			    ((u_int16_t)s61.sin6_addr.s6_addr[2] << 8) |
420 			    s61.sin6_addr.s6_addr[3];
421 			s61.sin6_addr.s6_addr[2] = s61.sin6_addr.s6_addr[3] = 0;
422 		}
423 #endif
424 		bcopy(&sen2.sen_ip6_dst, &s62.sin6_addr, sizeof(struct in6_addr));
425 #ifdef __KAME__
426 		if (IN6_IS_ADDR_LINKLOCAL(&s62.sin6_addr) ||
427 		    IN6_IS_ADDR_MC_LINKLOCAL(&s62.sin6_addr) ||
428 		    IN6_IS_ADDR_MC_INTFACELOCAL(&s62.sin6_addr)) {
429 			s62.sin6_scope_id =
430 			    ((u_int16_t)s62.sin6_addr.s6_addr[2] << 8) |
431 			    s62.sin6_addr.s6_addr[3];
432 			s62.sin6_addr.s6_addr[2] = s62.sin6_addr.s6_addr[3] = 0;
433 		}
434 #endif
435 
436 		printf("%-42s %-5u %-5u ", netname6(&s61, &s62),
437 		    ntohs(sen1.sen_ip6_dport), sen1.sen_ip6_proto);
438 	}
439 
440 	if (sen3.sen_type == SENT_IPSP) {
441 		char hostn[NI_MAXHOST];
442 
443 		kread((u_long)sen3.sen_ipsp, &ipo, sizeof(ipo));
444 
445 		if (getnameinfo(&ipo.ipo_dst.sa, ipo.ipo_dst.sa.sa_len,
446 		    hostn, NI_MAXHOST, NULL, 0, NI_NUMERICHOST) != 0)
447 			strlcpy (hostn, "none", NI_MAXHOST);
448 
449 		printf("%s", hostn);
450 		printf("/%-u", ipo.ipo_sproto);
451 
452 		switch (ipo.ipo_type) {
453 		case IPSP_IPSEC_REQUIRE:
454 			printf("/require");
455 			break;
456 		case IPSP_IPSEC_ACQUIRE:
457 			printf("/acquire");
458 			break;
459 		case IPSP_IPSEC_USE:
460 			printf("/use");
461 			break;
462 		case IPSP_IPSEC_DONTACQ:
463 			printf("/dontacq");
464 			break;
465 		case IPSP_PERMIT:
466 			printf("/bypass");
467 			break;
468 		case IPSP_DENY:
469 			printf("/deny");
470 			break;
471 		default:
472 			printf("/<unknown type!>");
473 			break;
474 		}
475 
476 		if ((ipo.ipo_addr.sen_type == SENT_IP4 &&
477 		    ipo.ipo_addr.sen_direction == IPSP_DIRECTION_IN) ||
478 		    (ipo.ipo_addr.sen_type == SENT_IP6 &&
479 		    ipo.ipo_addr.sen_ip6_direction == IPSP_DIRECTION_IN))
480 			printf("/in\n");
481 		else if ((ipo.ipo_addr.sen_type == SENT_IP4 &&
482 		    ipo.ipo_addr.sen_direction == IPSP_DIRECTION_OUT) ||
483 		    (ipo.ipo_addr.sen_type == SENT_IP6 &&
484 		    ipo.ipo_addr.sen_ip6_direction == IPSP_DIRECTION_OUT))
485 			printf("/out\n");
486 		else
487 			printf("/<unknown>\n");
488 	}
489 }
490