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