xref: /netbsd-src/sbin/route/show.c (revision 7f21db1c0118155e0dd40b75182e30c589d9f63e)
1 /*	$NetBSD: show.c,v 1.40 2010/01/26 21:27:54 is 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: show.c,v 1.40 2010/01/26 21:27:54 is 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 
46 #include <net/if.h>
47 #include <net/if_dl.h>
48 #include <net/if_types.h>
49 #include <net/route.h>
50 #include <netinet/in.h>
51 
52 #include <sys/sysctl.h>
53 
54 #include <netdb.h>
55 #include <stdbool.h>
56 #include <stdio.h>
57 #include <stdlib.h>
58 #include <string.h>
59 #include <unistd.h>
60 #include <err.h>
61 
62 #include "keywords.h"
63 #include "extern.h"
64 
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 /*
70  * Definitions for showing gateway flags.
71  */
72 struct bits {
73 	short	b_mask;
74 	char	b_val;
75 };
76 static const struct bits bits[] = {
77 	{ RTF_UP,	'U' },
78 	{ RTF_GATEWAY,	'G' },
79 	{ RTF_HOST,	'H' },
80 	{ RTF_REJECT,	'R' },
81 	{ RTF_DYNAMIC,	'D' },
82 	{ RTF_MODIFIED,	'M' },
83 	{ RTF_DONE,	'd' }, /* Completed -- for routing messages only */
84 	{ RTF_MASK,	'm' }, /* Mask Present -- for routing messages only */
85 	{ RTF_CLONING,	'C' },
86 	{ RTF_XRESOLVE,	'X' },
87 	{ RTF_LLINFO,	'L' },
88 	{ RTF_STATIC,	'S' },
89 	{ RTF_BLACKHOLE, 'B' },
90 	{ RTF_CLONED,	'c' },
91 	{ RTF_PROTO1,	'1' },
92 	{ RTF_PROTO2,	'2' },
93 	{ 0, '\0' }
94 };
95 
96 static void pr_rthdr(int);
97 static void p_rtentry(struct rt_msghdr *);
98 static void pr_family(int);
99 static void p_sockaddr(struct sockaddr *, struct sockaddr *, int, int );
100 static void p_flags(int);
101 
102 void
103 parse_show_opts(int argc, char * const *argv, int *afp, int *flagsp,
104     const char **afnamep, bool nolink)
105 {
106 	const char *afname = "unspec";
107 	int af, flags;
108 
109 	flags = 0;
110 	af = AF_UNSPEC;
111 	for (; argc >= 2; argc--) {
112 		if (*argv[argc - 1] != '-')
113 			goto bad;
114 		switch (keyword(argv[argc - 1] + 1)) {
115 		case K_HOST:
116 			flags |= RTF_HOST;
117 			break;
118 		case K_LLINFO:
119 			flags |= RTF_LLINFO;
120 			break;
121 		case K_INET:
122 			af = AF_INET;
123 			afname = argv[argc - 1] + 1;
124 			break;
125 #ifdef INET6
126 		case K_INET6:
127 			af = AF_INET6;
128 			afname = argv[argc - 1] + 1;
129 			break;
130 #endif
131 #ifndef SMALL
132 		case K_ATALK:
133 			af = AF_APPLETALK;
134 			afname = argv[argc - 1] + 1;
135 			break;
136 		case K_ISO:
137 		case K_OSI:
138 			af = AF_ISO;
139 			afname = argv[argc - 1] + 1;
140 			break;
141 #endif /* SMALL */
142 		case K_LINK:
143 			if (nolink)
144 				goto bad;
145 			af = AF_LINK;
146 			afname = argv[argc - 1] + 1;
147 			break;
148 		default:
149 			goto bad;
150 		}
151 	}
152 	switch (argc) {
153 	case 1:
154 	case 0:
155 		break;
156 	default:
157 	bad:
158 		usage(argv[argc - 1]);
159 	}
160 	if (afnamep != NULL)
161 		*afnamep = afname;
162 	*afp = af;
163 	*flagsp = flags;
164 }
165 
166 /*
167  * Print routing tables.
168  */
169 void
170 show(int argc, char *const *argv)
171 {
172 	size_t needed;
173 	int af, flags, mib[6];
174 	char *buf, *next, *lim;
175 	struct rt_msghdr *rtm;
176 	struct sockaddr *sa;
177 
178 	parse_show_opts(argc, argv, &af, &flags, NULL, true);
179 	mib[0] = CTL_NET;
180 	mib[1] = PF_ROUTE;
181 	mib[2] = 0;
182 	mib[3] = 0;
183 	mib[4] = NET_RT_DUMP;
184 	mib[5] = 0;
185 	if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
186 		err(EXIT_FAILURE, "route-sysctl-estimate");
187 	buf = lim = NULL;
188 	if (needed) {
189 		if ((buf = malloc(needed)) == 0)
190 			err(EXIT_FAILURE, "malloc");
191 		if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0)
192 			err(EXIT_FAILURE, "sysctl of routing table");
193 		lim  = buf + needed;
194 	}
195 
196 	printf("Routing table%s\n", (af == AF_UNSPEC)? "s" : "");
197 
198 	if (needed) {
199 		for (next = buf; next < lim; next += rtm->rtm_msglen) {
200 			rtm = (struct rt_msghdr *)next;
201 			sa = (struct sockaddr *)(rtm + 1);
202 			if ((rtm->rtm_flags & flags) != flags)
203 				continue;
204 			if (af == AF_UNSPEC || af == sa->sa_family)
205 				p_rtentry(rtm);
206 		}
207 		free(buf);
208 	}
209 }
210 
211 
212 /* column widths; each followed by one space */
213 #ifndef INET6
214 #define	WID_DST(af)	18	/* width of destination column */
215 #define	WID_GW(af)	18	/* width of gateway column */
216 #else
217 /* width of destination/gateway column */
218 #if 1
219 /* strlen("fe80::aaaa:bbbb:cccc:dddd@gif0") == 30, strlen("/128") == 4 */
220 #define	WID_DST(af)	((af) == AF_INET6 ? (nflag ? 34 : 18) : 18)
221 #define	WID_GW(af)	((af) == AF_INET6 ? (nflag ? 30 : 18) : 18)
222 #else
223 /* strlen("fe80::aaaa:bbbb:cccc:dddd") == 25, strlen("/128") == 4 */
224 #define	WID_DST(af)	((af) == AF_INET6 ? (nflag ? 29 : 18) : 18)
225 #define	WID_GW(af)	((af) == AF_INET6 ? (nflag ? 25 : 18) : 18)
226 #endif
227 #endif /* INET6 */
228 
229 /*
230  * Print header for routing table columns.
231  */
232 static void
233 pr_rthdr(int af)
234 {
235 
236 	printf("%-*.*s %-*.*s %-6.6s\n",
237 		WID_DST(af), WID_DST(af), "Destination",
238 		WID_GW(af), WID_GW(af), "Gateway",
239 		"Flags");
240 }
241 
242 
243 /*
244  * Print a routing table entry.
245  */
246 static void
247 p_rtentry(struct rt_msghdr *rtm)
248 {
249 	struct sockaddr *sa = (struct sockaddr *)(rtm + 1);
250 #ifdef notdef
251 	static int masks_done, banner_printed;
252 #endif
253 	static int old_af;
254 	int af = 0, interesting = RTF_UP | RTF_GATEWAY | RTF_HOST |
255 	    RTF_REJECT | RTF_LLINFO;
256 
257 #ifdef notdef
258 	/* for the moment, netmasks are skipped over */
259 	if (!banner_printed) {
260 		printf("Netmasks:\n");
261 		banner_printed = 1;
262 	}
263 	if (masks_done == 0) {
264 		if (rtm->rtm_addrs != RTA_DST ) {
265 			masks_done = 1;
266 			af = sa->sa_family;
267 		}
268 	} else
269 #endif
270 		af = sa->sa_family;
271 	if (old_af != af) {
272 		old_af = af;
273 		pr_family(af);
274 		pr_rthdr(af);
275 	}
276 	if (rtm->rtm_addrs == RTA_DST)
277 		p_sockaddr(sa, NULL, 0, WID_DST(af) + 1 + WID_GW(af) + 1);
278 	else {
279 		struct sockaddr *nm;
280 
281 		if ((rtm->rtm_addrs & RTA_NETMASK) == 0)
282 			nm = NULL;
283 		else {
284 			/* skip to gateway */
285 			nm = (struct sockaddr *)
286 			    (ROUNDUP(sa->sa_len) + (char *)sa);
287 			/* skip over gateway to netmask */
288 			nm = (struct sockaddr *)
289 			    (ROUNDUP(nm->sa_len) + (char *)nm);
290 		}
291 
292 		p_sockaddr(sa, nm, rtm->rtm_flags, WID_DST(af));
293 		sa = (struct sockaddr *)(ROUNDUP(sa->sa_len) + (char *)sa);
294 		p_sockaddr(sa, NULL, 0, WID_GW(af));
295 	}
296 	p_flags(rtm->rtm_flags & interesting);
297 	putchar('\n');
298 }
299 
300 
301 /*
302  * Print address family header before a section of the routing table.
303  */
304 static void
305 pr_family(int af)
306 {
307 	const char *afname;
308 
309 	switch (af) {
310 	case AF_INET:
311 		afname = "Internet";
312 		break;
313 #ifdef INET6
314 	case AF_INET6:
315 		afname = "Internet6";
316 		break;
317 #endif /* INET6 */
318 #ifndef SMALL
319 	case AF_ISO:
320 		afname = "ISO";
321 		break;
322 #endif /* SMALL */
323 	case AF_APPLETALK:
324 		afname = "AppleTalk";
325 		break;
326 	default:
327 		afname = NULL;
328 		break;
329 	}
330 	if (afname)
331 		printf("\n%s:\n", afname);
332 	else
333 		printf("\nProtocol Family %d:\n", af);
334 }
335 
336 
337 static void
338 p_sockaddr(struct sockaddr *sa, struct sockaddr *nm, int flags, int width)
339 {
340 	char workbuf[128];
341 	const char *cp;
342 
343 	switch(sa->sa_family) {
344 
345 	case AF_LINK:
346 		if (getnameinfo(sa, sa->sa_len, workbuf, sizeof(workbuf),
347 		    NULL, 0, NI_NUMERICHOST) != 0)
348 			strlcpy(workbuf, "invalid", sizeof(workbuf));
349 		cp = workbuf;
350 		break;
351 
352 	case AF_INET:
353 		cp = routename(sa, nm, flags);
354 		break;
355 
356 #ifdef INET6
357 	case AF_INET6:
358 		cp = routename(sa, nm, flags);
359 		/* make sure numeric address is not truncated */
360 		if (strchr(cp, ':') != NULL && (int)strlen(cp) > width)
361 			width = strlen(cp);
362 		break;
363 #endif /* INET6 */
364 
365 #ifndef SMALL
366 	case AF_APPLETALK:
367 		if (getnameinfo(sa, sa->sa_len, workbuf, sizeof(workbuf),
368 		    NULL, 0, NI_NUMERICHOST) != 0)
369 			strlcpy(workbuf, "invalid", sizeof(workbuf));
370 		cp = workbuf;
371 		break;
372 
373 #endif /* SMALL */
374 
375 	default:
376 	    {
377 		u_char *s = (u_char *)sa->sa_data, *slim;
378 		char *wp = workbuf, *wplim;
379 
380 		slim = sa->sa_len + (u_char *)sa;
381 		wplim = wp + sizeof(workbuf) - 6;
382 		wp += snprintf(wp, wplim - wp, "(%d)", sa->sa_family);
383 		while (s < slim && wp < wplim) {
384 			wp += snprintf(wp, wplim - wp, " %02x", *s++);
385 			if (s < slim)
386 			    wp += snprintf(wp, wplim - wp, "%02x", *s++);
387 		}
388 		cp = workbuf;
389 	    }
390 	}
391 	if (width < 0 )
392 		printf("%s ", cp);
393 	else {
394 		if (nflag)
395 			printf("%-*s ", width, cp);
396 		else
397 			printf("%-*.*s ", width, width, cp);
398 	}
399 }
400 
401 static void
402 p_flags(int f)
403 {
404 	char name[33], *flags;
405 	const struct bits *p = bits;
406 
407 	for (flags = name; p->b_mask; p++)
408 		if (p->b_mask & f)
409 			*flags++ = p->b_val;
410 		else if (Sflag)
411 			*flags++ = ' ';
412 	*flags = '\0';
413 	printf("%-6.6s ", name);
414 }
415 
416