show.c revision 1.10 1 /* $NetBSD: show.c,v 1.10 1999/07/12 20:46:15 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.10 1999/07/12 20:46:15 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 /*
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_PROTO1, '1' },
89 { RTF_PROTO2, '2' },
90 { 0 }
91 };
92
93 static void pr_rthdr __P((void));
94 static void p_rtentry __P((struct rt_msghdr *));
95 static void pr_family __P((int));
96 static void p_sockaddr __P((struct sockaddr *, int, int ));
97 static void p_flags __P((int, char *));
98
99 /*
100 * Print routing tables.
101 */
102 void
103 show(argc, argv)
104 int argc;
105 char **argv;
106 {
107 size_t needed;
108 int mib[6];
109 char *buf, *next, *lim;
110 struct rt_msghdr *rtm;
111
112 mib[0] = CTL_NET;
113 mib[1] = PF_ROUTE;
114 mib[2] = 0;
115 mib[3] = 0;
116 mib[4] = NET_RT_DUMP;
117 mib[5] = 0;
118 if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0) {
119 perror("route-sysctl-estimate");
120 exit(1);
121 }
122 if ((buf = malloc(needed)) == 0)
123 err(1, "%s", "");
124 if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0)
125 err(1, "sysctl of routing table");
126 lim = buf + needed;
127
128 printf("Routing tables\n");
129
130 /* for (i = 0; i <= AF_MAX; i++) ??? */
131 {
132 for (next = buf; next < lim; next += rtm->rtm_msglen) {
133 rtm = (struct rt_msghdr *)next;
134 p_rtentry(rtm);
135 }
136 }
137 }
138
139
140 /* column widths; each followed by one space */
141 #define WID_DST 16 /* width of destination column */
142 #define WID_GW 18 /* width of gateway column */
143
144 /*
145 * Print header for routing table columns.
146 */
147 static void
148 pr_rthdr()
149 {
150
151 printf("%-*.*s %-*.*s %-6.6s\n",
152 WID_DST, WID_DST, "Destination",
153 WID_GW, WID_GW, "Gateway",
154 "Flags");
155 }
156
157
158 /*
159 * Print a routing table entry.
160 */
161 static void
162 p_rtentry(rtm)
163 struct rt_msghdr *rtm;
164 {
165 struct sockaddr *sa = (struct sockaddr *)(rtm + 1);
166 #ifdef notdef
167 static int masks_done, banner_printed;
168 #endif
169 static int old_af;
170 int af = 0, interesting = RTF_UP | RTF_GATEWAY | RTF_HOST;
171
172 #ifdef notdef
173 /* for the moment, netmasks are skipped over */
174 if (!banner_printed) {
175 printf("Netmasks:\n");
176 banner_printed = 1;
177 }
178 if (masks_done == 0) {
179 if (rtm->rtm_addrs != RTA_DST ) {
180 masks_done = 1;
181 af = sa->sa_family;
182 }
183 } else
184 #endif
185 af = sa->sa_family;
186 if (old_af != af) {
187 old_af = af;
188 pr_family(af);
189 pr_rthdr();
190 }
191 if (rtm->rtm_addrs == RTA_DST)
192 p_sockaddr(sa, 0, WID_DST + 1 + WID_GW + 1);
193 else {
194 p_sockaddr(sa, rtm->rtm_flags, WID_DST);
195 if (sa->sa_len == 0)
196 sa->sa_len = sizeof(long);
197 sa = (struct sockaddr *)(sa->sa_len + (char *)sa);
198 p_sockaddr(sa, 0, WID_GW);
199 }
200 p_flags(rtm->rtm_flags & interesting, "%-6.6s ");
201 putchar('\n');
202 }
203
204
205 /*
206 * Print address family header before a section of the routing table.
207 */
208 static void
209 pr_family(af)
210 int af;
211 {
212 char *afname;
213
214 switch (af) {
215 case AF_INET:
216 afname = "Internet";
217 break;
218 #ifndef SMALL
219 #ifdef INET6
220 case AF_INET6:
221 afname = "Internet6";
222 break;
223 #endif /* INET6 */
224 case AF_NS:
225 afname = "XNS";
226 break;
227 case AF_ISO:
228 afname = "ISO";
229 break;
230 case AF_CCITT:
231 afname = "X.25";
232 break;
233 #endif /* SMALL */
234 case AF_APPLETALK:
235 afname = "AppleTalk";
236 break;
237 default:
238 afname = NULL;
239 break;
240 }
241 if (afname)
242 printf("\n%s:\n", afname);
243 else
244 printf("\nProtocol Family %d:\n", af);
245 }
246
247
248 static void
249 p_sockaddr(sa, flags, width)
250 struct sockaddr *sa;
251 int flags, width;
252 {
253 char workbuf[128], *cplim;
254 char *cp = workbuf;
255 int cplen = 0, len;
256
257 switch(sa->sa_family) {
258
259 case AF_LINK:
260 {
261 struct sockaddr_dl *sdl = (struct sockaddr_dl *)sa;
262
263 if (sdl->sdl_nlen == 0 && sdl->sdl_alen == 0 &&
264 sdl->sdl_slen == 0)
265 (void)snprintf(workbuf, sizeof workbuf, "link#%d",
266 sdl->sdl_index);
267 else switch (sdl->sdl_type) {
268 case IFT_ETHER:
269 {
270 int i;
271 u_char *lla = (u_char *)sdl->sdl_data +
272 sdl->sdl_nlen;
273
274 cplim = "";
275 for (i = 0; i < sdl->sdl_alen; i++, lla++) {
276 len = snprintf(cp, sizeof(workbuf) - cplen,
277 "%s%x", cplim, *lla);
278 cp += len;
279 cplen += len;
280 cplim = ":";
281 }
282 cp = workbuf;
283 break;
284 }
285 default:
286 cp = link_ntoa(sdl);
287 break;
288 }
289 break;
290 }
291
292 case AF_INET:
293 {
294 struct sockaddr_in *sin = (struct sockaddr_in *)sa;
295
296 cp = (sin->sin_addr.s_addr == 0) ? "default" :
297 ((flags & RTF_HOST) ?
298 routename(sa) : netname(sa));
299 break;
300 }
301
302 #ifndef SMALL
303 #ifdef INET6
304 case AF_INET6:
305 {
306 struct sockaddr_in6 *sin = (struct sockaddr_in6 *)sa;
307
308 cp = (sin->sin6_addr.s6_addr32[0] == 0 &&
309 sin->sin6_addr.s6_addr32[1] == 0 &&
310 sin->sin6_addr.s6_addr32[2] == 0 &&
311 sin->sin6_addr.s6_addr32[3] == 0) ? "default" :
312 ((flags & RTF_HOST) ?
313 routename(sa) : netname(sa));
314 break;
315 }
316 #endif /* INET6 */
317
318 case AF_NS:
319 cp = ns_print((struct sockaddr_ns *)sa);
320 break;
321 #endif /* SMALL */
322
323 default:
324 {
325 u_char *s = (u_char *)sa->sa_data, *slim;
326
327 slim = sa->sa_len + (u_char *) sa;
328 cplim = cp + sizeof(workbuf) - 6;
329 cp += snprintf(cp, cplim - cp, "(%d)", sa->sa_family);
330 while (s < slim && cp < cplim) {
331 cp += snprintf(cp, cplim - cp, " %02x", *s++);
332 if (s < slim)
333 cp += snprintf(cp, cplim - cp, "%02x", *s++);
334 }
335 cp = workbuf;
336 }
337 }
338 if (width < 0 )
339 printf("%s ", cp);
340 else {
341 if (nflag)
342 printf("%-*s ", width, cp);
343 else
344 printf("%-*.*s ", width, width, cp);
345 }
346 }
347
348 static void
349 p_flags(f, format)
350 int f;
351 char *format;
352 {
353 char name[33], *flags;
354 const struct bits *p = bits;
355
356 for (flags = name; p->b_mask; p++)
357 if (p->b_mask & f)
358 *flags++ = p->b_val;
359 *flags = '\0';
360 printf(format, name);
361 }
362
363