show.c revision 1.31 1 /* $NetBSD: show.c,v 1.31 2006/09/07 02:40:31 dogcow 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.31 2006/09/07 02:40:31 dogcow 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 /*
102 * Print routing tables.
103 */
104 void
105 show(int argc, char **argv)
106 {
107 size_t needed;
108 int af, mib[6];
109 char *buf, *next, *lim;
110 struct rt_msghdr *rtm;
111 struct sockaddr *sa;
112
113 af = AF_UNSPEC;
114 if (argc > 1) {
115 argv++;
116 if (argc == 2 && **argv == '-')
117 switch (keyword(*argv + 1)) {
118 case K_INET:
119 af = AF_INET;
120 break;
121 #ifdef INET6
122 case K_INET6:
123 af = AF_INET6;
124 break;
125 #endif
126 #ifndef SMALL
127 case K_ATALK:
128 af = AF_APPLETALK;
129 break;
130 #endif /* SMALL */
131 #if 0
132 /* XXX Links are never destinations */
133 case K_LINK:
134 af = AF_LINK;
135 break;
136 #endif
137 #ifndef SMALL
138 case K_ISO:
139 case K_OSI:
140 af = AF_ISO;
141 break;
142 #endif /* SMALL */
143 default:
144 goto bad;
145 } else
146 bad: usage(*argv);
147 }
148 mib[0] = CTL_NET;
149 mib[1] = PF_ROUTE;
150 mib[2] = 0;
151 mib[3] = 0;
152 mib[4] = NET_RT_DUMP;
153 mib[5] = 0;
154 if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
155 err(1, "route-sysctl-estimate");
156 buf = lim = NULL;
157 if (needed) {
158 if ((buf = malloc(needed)) == 0)
159 err(1, "malloc");
160 if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0)
161 err(1, "sysctl of routing table");
162 lim = buf + needed;
163 }
164
165 printf("Routing table%s\n", (af == AF_UNSPEC)? "s" : "");
166
167 if (needed) {
168 for (next = buf; next < lim; next += rtm->rtm_msglen) {
169 rtm = (struct rt_msghdr *)next;
170 sa = (struct sockaddr *)(rtm + 1);
171 if (af == AF_UNSPEC || af == sa->sa_family)
172 p_rtentry(rtm);
173 }
174 free(buf);
175 }
176 }
177
178
179 /* column widths; each followed by one space */
180 #ifndef INET6
181 #define WID_DST(af) 18 /* width of destination column */
182 #define WID_GW(af) 18 /* width of gateway column */
183 #else
184 /* width of destination/gateway column */
185 #if 1
186 /* strlen("fe80::aaaa:bbbb:cccc:dddd@gif0") == 30, strlen("/128") == 4 */
187 #define WID_DST(af) ((af) == AF_INET6 ? (nflag ? 34 : 18) : 18)
188 #define WID_GW(af) ((af) == AF_INET6 ? (nflag ? 30 : 18) : 18)
189 #else
190 /* strlen("fe80::aaaa:bbbb:cccc:dddd") == 25, strlen("/128") == 4 */
191 #define WID_DST(af) ((af) == AF_INET6 ? (nflag ? 29 : 18) : 18)
192 #define WID_GW(af) ((af) == AF_INET6 ? (nflag ? 25 : 18) : 18)
193 #endif
194 #endif /* INET6 */
195
196 /*
197 * Print header for routing table columns.
198 */
199 static void
200 pr_rthdr(int af)
201 {
202
203 printf("%-*.*s %-*.*s %-6.6s\n",
204 WID_DST(af), WID_DST(af), "Destination",
205 WID_GW(af), WID_GW(af), "Gateway",
206 "Flags");
207 }
208
209
210 /*
211 * Print a routing table entry.
212 */
213 static void
214 p_rtentry(struct rt_msghdr *rtm)
215 {
216 struct sockaddr *sa = (struct sockaddr *)(rtm + 1);
217 #ifdef notdef
218 static int masks_done, banner_printed;
219 #endif
220 static int old_af;
221 int af = 0, interesting = RTF_UP | RTF_GATEWAY | RTF_HOST | RTF_REJECT;
222
223 #ifdef notdef
224 /* for the moment, netmasks are skipped over */
225 if (!banner_printed) {
226 printf("Netmasks:\n");
227 banner_printed = 1;
228 }
229 if (masks_done == 0) {
230 if (rtm->rtm_addrs != RTA_DST ) {
231 masks_done = 1;
232 af = sa->sa_family;
233 }
234 } else
235 #endif
236 af = sa->sa_family;
237 if (old_af != af) {
238 old_af = af;
239 pr_family(af);
240 pr_rthdr(af);
241 }
242 if (rtm->rtm_addrs == RTA_DST)
243 p_sockaddr(sa, NULL, 0, WID_DST(af) + 1 + WID_GW(af) + 1);
244 else {
245 struct sockaddr *nm;
246
247 if ((rtm->rtm_addrs & RTA_NETMASK) == 0)
248 nm = NULL;
249 else {
250 /* skip to gateway */
251 nm = (struct sockaddr *)
252 (ROUNDUP(sa->sa_len) + (char *)sa);
253 /* skip over gateway to netmask */
254 nm = (struct sockaddr *)
255 (ROUNDUP(nm->sa_len) + (char *)nm);
256 }
257
258 p_sockaddr(sa, nm, rtm->rtm_flags, WID_DST(af));
259 sa = (struct sockaddr *)(ROUNDUP(sa->sa_len) + (char *)sa);
260 p_sockaddr(sa, NULL, 0, WID_GW(af));
261 }
262 p_flags(rtm->rtm_flags & interesting);
263 putchar('\n');
264 }
265
266
267 /*
268 * Print address family header before a section of the routing table.
269 */
270 static void
271 pr_family(int af)
272 {
273 const char *afname;
274
275 switch (af) {
276 case AF_INET:
277 afname = "Internet";
278 break;
279 #ifdef INET6
280 case AF_INET6:
281 afname = "Internet6";
282 break;
283 #endif /* INET6 */
284 #ifndef SMALL
285 case AF_ISO:
286 afname = "ISO";
287 break;
288 #endif /* SMALL */
289 case AF_APPLETALK:
290 afname = "AppleTalk";
291 break;
292 default:
293 afname = NULL;
294 break;
295 }
296 if (afname)
297 printf("\n%s:\n", afname);
298 else
299 printf("\nProtocol Family %d:\n", af);
300 }
301
302
303 static void
304 p_sockaddr(struct sockaddr *sa, struct sockaddr *nm, int flags, int width)
305 {
306 char workbuf[128];
307 const char *cp;
308
309 switch(sa->sa_family) {
310
311 case AF_LINK:
312 if (getnameinfo(sa, sa->sa_len, workbuf, sizeof(workbuf),
313 NULL, 0, NI_NUMERICHOST) != 0)
314 strlcpy(workbuf, "invalid", sizeof(workbuf));
315 cp = workbuf;
316 break;
317
318 case AF_INET:
319 cp = routename(sa, nm, flags);
320 break;
321
322 #ifdef INET6
323 case AF_INET6:
324 cp = routename(sa, nm, flags);
325 /* make sure numeric address is not truncated */
326 if (strchr(cp, ':') != NULL && strlen(cp) > width)
327 width = strlen(cp);
328 break;
329 #endif /* INET6 */
330
331 #ifndef SMALL
332 #endif /* SMALL */
333
334 default:
335 {
336 u_char *s = (u_char *)sa->sa_data, *slim;
337 char *wp = workbuf, *wplim;
338
339 slim = sa->sa_len + (u_char *)sa;
340 wplim = wp + sizeof(workbuf) - 6;
341 wp += snprintf(wp, wplim - wp, "(%d)", sa->sa_family);
342 while (s < slim && wp < wplim) {
343 wp += snprintf(wp, wplim - wp, " %02x", *s++);
344 if (s < slim)
345 wp += snprintf(wp, wplim - wp, "%02x", *s++);
346 }
347 cp = workbuf;
348 }
349 }
350 if (width < 0 )
351 printf("%s ", cp);
352 else {
353 if (nflag)
354 printf("%-*s ", width, cp);
355 else
356 printf("%-*.*s ", width, width, cp);
357 }
358 }
359
360 static void
361 p_flags(int f)
362 {
363 char name[33], *flags;
364 const struct bits *p = bits;
365
366 for (flags = name; p->b_mask; p++)
367 if (p->b_mask & f)
368 *flags++ = p->b_val;
369 else if (Sflag)
370 *flags++ = ' ';
371 *flags = '\0';
372 printf("%-6.6s ", name);
373 }
374
375