show.c revision 1.13 1 /* $NetBSD: show.c,v 1.13 1999/09/03 04:17:19 itojun 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. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36 #include <sys/cdefs.h>
37 #ifndef lint
38 #if 0
39 static char sccsid[] = "from: @(#)route.c 8.3 (Berkeley) 3/9/94";
40 #else
41 __RCSID("$NetBSD: show.c,v 1.13 1999/09/03 04:17:19 itojun Exp $");
42 #endif
43 #endif /* not lint */
44
45 #include <sys/param.h>
46 #include <sys/protosw.h>
47 #include <sys/socket.h>
48 #include <sys/mbuf.h>
49
50 #include <net/if.h>
51 #include <net/if_dl.h>
52 #include <net/if_types.h>
53 #include <net/route.h>
54 #include <netinet/in.h>
55 #include <netns/ns.h>
56
57 #include <sys/sysctl.h>
58
59 #include <netdb.h>
60 #include <stdio.h>
61 #include <stdlib.h>
62 #include <string.h>
63 #include <unistd.h>
64 #include <err.h>
65
66 #include "extern.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_PROTO1, '1' },
93 { RTF_PROTO2, '2' },
94 { 0 }
95 };
96
97 static void pr_rthdr __P((void));
98 static void p_rtentry __P((struct rt_msghdr *));
99 static void pr_family __P((int));
100 static void p_sockaddr __P((struct sockaddr *, int, int ));
101 static void p_flags __P((int, char *));
102
103 /*
104 * Print routing tables.
105 */
106 void
107 show(argc, argv)
108 int argc;
109 char **argv;
110 {
111 size_t needed;
112 int mib[6];
113 char *buf, *next, *lim;
114 struct rt_msghdr *rtm;
115
116 mib[0] = CTL_NET;
117 mib[1] = PF_ROUTE;
118 mib[2] = 0;
119 mib[3] = 0;
120 mib[4] = NET_RT_DUMP;
121 mib[5] = 0;
122 if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0) {
123 perror("route-sysctl-estimate");
124 exit(1);
125 }
126 if ((buf = malloc(needed)) == 0)
127 err(1, "%s", "");
128 if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0)
129 err(1, "sysctl of routing table");
130 lim = buf + needed;
131
132 printf("Routing tables\n");
133
134 /* for (i = 0; i <= AF_MAX; i++) ??? */
135 {
136 for (next = buf; next < lim; next += rtm->rtm_msglen) {
137 rtm = (struct rt_msghdr *)next;
138 p_rtentry(rtm);
139 }
140 }
141 }
142
143
144 /* column widths; each followed by one space */
145 #define WID_DST 16 /* width of destination column */
146 #define WID_GW 18 /* width of gateway column */
147
148 /*
149 * Print header for routing table columns.
150 */
151 static void
152 pr_rthdr()
153 {
154
155 printf("%-*.*s %-*.*s %-6.6s\n",
156 WID_DST, WID_DST, "Destination",
157 WID_GW, WID_GW, "Gateway",
158 "Flags");
159 }
160
161
162 /*
163 * Print a routing table entry.
164 */
165 static void
166 p_rtentry(rtm)
167 struct rt_msghdr *rtm;
168 {
169 struct sockaddr *sa = (struct sockaddr *)(rtm + 1);
170 #ifdef notdef
171 static int masks_done, banner_printed;
172 #endif
173 static int old_af;
174 int af = 0, interesting = RTF_UP | RTF_GATEWAY | RTF_HOST;
175
176 #ifdef notdef
177 /* for the moment, netmasks are skipped over */
178 if (!banner_printed) {
179 printf("Netmasks:\n");
180 banner_printed = 1;
181 }
182 if (masks_done == 0) {
183 if (rtm->rtm_addrs != RTA_DST ) {
184 masks_done = 1;
185 af = sa->sa_family;
186 }
187 } else
188 #endif
189 af = sa->sa_family;
190 if (old_af != af) {
191 old_af = af;
192 pr_family(af);
193 pr_rthdr();
194 }
195 if (rtm->rtm_addrs == RTA_DST)
196 p_sockaddr(sa, 0, WID_DST + 1 + WID_GW + 1);
197 else {
198 p_sockaddr(sa, rtm->rtm_flags, WID_DST);
199 sa = (struct sockaddr *)(ROUNDUP(sa->sa_len) + (char *)sa);
200 p_sockaddr(sa, 0, WID_GW);
201 }
202 p_flags(rtm->rtm_flags & interesting, "%-6.6s ");
203 putchar('\n');
204 }
205
206
207 /*
208 * Print address family header before a section of the routing table.
209 */
210 static void
211 pr_family(af)
212 int af;
213 {
214 char *afname;
215
216 switch (af) {
217 case AF_INET:
218 afname = "Internet";
219 break;
220 #ifndef SMALL
221 #ifdef INET6
222 case AF_INET6:
223 afname = "Internet6";
224 break;
225 #endif /* INET6 */
226 case AF_NS:
227 afname = "XNS";
228 break;
229 case AF_ISO:
230 afname = "ISO";
231 break;
232 case AF_CCITT:
233 afname = "X.25";
234 break;
235 #endif /* SMALL */
236 case AF_APPLETALK:
237 afname = "AppleTalk";
238 break;
239 default:
240 afname = NULL;
241 break;
242 }
243 if (afname)
244 printf("\n%s:\n", afname);
245 else
246 printf("\nProtocol Family %d:\n", af);
247 }
248
249
250 static void
251 p_sockaddr(sa, flags, width)
252 struct sockaddr *sa;
253 int flags, width;
254 {
255 char workbuf[128], *cplim;
256 char *cp = workbuf;
257 int cplen = 0, len;
258
259 switch(sa->sa_family) {
260
261 case AF_LINK:
262 {
263 struct sockaddr_dl *sdl = (struct sockaddr_dl *)sa;
264
265 if (sdl->sdl_nlen == 0 && sdl->sdl_alen == 0 &&
266 sdl->sdl_slen == 0)
267 (void)snprintf(workbuf, sizeof workbuf, "link#%d",
268 sdl->sdl_index);
269 else switch (sdl->sdl_type) {
270 case IFT_ETHER:
271 {
272 int i;
273 u_char *lla = (u_char *)sdl->sdl_data +
274 sdl->sdl_nlen;
275
276 cplim = "";
277 for (i = 0; i < sdl->sdl_alen; i++, lla++) {
278 len = snprintf(cp, sizeof(workbuf) - cplen,
279 "%s%x", cplim, *lla);
280 cp += len;
281 cplen += len;
282 cplim = ":";
283 }
284 cp = workbuf;
285 break;
286 }
287 default:
288 cp = link_ntoa(sdl);
289 break;
290 }
291 break;
292 }
293
294 case AF_INET:
295 {
296 struct sockaddr_in *sin = (struct sockaddr_in *)sa;
297
298 cp = (sin->sin_addr.s_addr == 0) ? "default" :
299 ((flags & RTF_HOST) ?
300 routename(sa) : netname(sa));
301 break;
302 }
303
304 #ifndef SMALL
305 #ifdef INET6
306 case AF_INET6:
307 {
308 struct sockaddr_in6 *sin = (struct sockaddr_in6 *)sa;
309
310 cp = IN6_IS_ADDR_UNSPECIFIED(&sin->sin6_addr) ? "default" :
311 ((flags & RTF_HOST) ?
312 routename(sa) : netname(sa));
313 /* make sure numeric address is not truncated */
314 if (strchr(cp, ':') != NULL && strlen(cp) > width)
315 width = strlen(cp);
316 break;
317 }
318 #endif /* INET6 */
319
320 case AF_NS:
321 cp = ns_print((struct sockaddr_ns *)sa);
322 break;
323 #endif /* SMALL */
324
325 default:
326 {
327 u_char *s = (u_char *)sa->sa_data, *slim;
328
329 slim = sa->sa_len + (u_char *) sa;
330 cplim = cp + sizeof(workbuf) - 6;
331 cp += snprintf(cp, cplim - cp, "(%d)", sa->sa_family);
332 while (s < slim && cp < cplim) {
333 cp += snprintf(cp, cplim - cp, " %02x", *s++);
334 if (s < slim)
335 cp += snprintf(cp, cplim - cp, "%02x", *s++);
336 }
337 cp = workbuf;
338 }
339 }
340 if (width < 0 )
341 printf("%s ", cp);
342 else {
343 if (nflag)
344 printf("%-*s ", width, cp);
345 else
346 printf("%-*.*s ", width, width, cp);
347 }
348 }
349
350 static void
351 p_flags(f, format)
352 int f;
353 char *format;
354 {
355 char name[33], *flags;
356 const struct bits *p = bits;
357
358 for (flags = name; p->b_mask; p++)
359 if (p->b_mask & f)
360 *flags++ = p->b_val;
361 *flags = '\0';
362 printf(format, name);
363 }
364
365