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