show.c revision 1.21 1 /* $NetBSD: show.c,v 1.21 2003/08/07 10:04:39 agc 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.21 2003/08/07 10:04:39 agc 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 #include <netns/ns.h>
52
53 #include <sys/sysctl.h>
54
55 #include <netdb.h>
56 #include <stdio.h>
57 #include <stdlib.h>
58 #include <string.h>
59 #include <unistd.h>
60 #include <err.h>
61
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 __P((void));
96 static void p_rtentry __P((struct rt_msghdr *));
97 static void pr_family __P((int));
98 static void p_sockaddr __P((struct sockaddr *, struct sockaddr *, int, int ));
99 static void p_flags __P((int));
100
101 /*
102 * Print routing tables.
103 */
104 void
105 show(argc, argv)
106 int argc;
107 char **argv;
108 {
109 size_t needed;
110 int mib[6];
111 char *buf, *next, *lim;
112 struct rt_msghdr *rtm;
113
114 mib[0] = CTL_NET;
115 mib[1] = PF_ROUTE;
116 mib[2] = 0;
117 mib[3] = 0;
118 mib[4] = NET_RT_DUMP;
119 mib[5] = 0;
120 if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0) {
121 perror("route-sysctl-estimate");
122 exit(1);
123 }
124 if ((buf = malloc(needed)) == 0)
125 err(1, NULL);
126 if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0)
127 err(1, "sysctl of routing table");
128 lim = buf + needed;
129
130 printf("Routing tables\n");
131
132 /* for (i = 0; i <= AF_MAX; i++) ??? */
133 {
134 for (next = buf; next < lim; next += rtm->rtm_msglen) {
135 rtm = (struct rt_msghdr *)next;
136 p_rtentry(rtm);
137 }
138 }
139 }
140
141
142 /* column widths; each followed by one space */
143 #define WID_DST 17 /* width of destination column */
144 #define WID_GW 18 /* width of gateway column */
145
146 /*
147 * Print header for routing table columns.
148 */
149 static void
150 pr_rthdr()
151 {
152
153 printf("%-*.*s %-*.*s %-6.6s\n",
154 WID_DST, WID_DST, "Destination",
155 WID_GW, WID_GW, "Gateway",
156 "Flags");
157 }
158
159
160 /*
161 * Print a routing table entry.
162 */
163 static void
164 p_rtentry(rtm)
165 struct rt_msghdr *rtm;
166 {
167 struct sockaddr *sa = (struct sockaddr *)(rtm + 1);
168 #ifdef notdef
169 static int masks_done, banner_printed;
170 #endif
171 static int old_af;
172 int af = 0, interesting = RTF_UP | RTF_GATEWAY | RTF_HOST | RTF_REJECT;
173
174 #ifdef notdef
175 /* for the moment, netmasks are skipped over */
176 if (!banner_printed) {
177 printf("Netmasks:\n");
178 banner_printed = 1;
179 }
180 if (masks_done == 0) {
181 if (rtm->rtm_addrs != RTA_DST ) {
182 masks_done = 1;
183 af = sa->sa_family;
184 }
185 } else
186 #endif
187 af = sa->sa_family;
188 if (old_af != af) {
189 old_af = af;
190 pr_family(af);
191 pr_rthdr();
192 }
193 if (rtm->rtm_addrs == RTA_DST)
194 p_sockaddr(sa, NULL, 0, WID_DST + 1 + WID_GW + 1);
195 else {
196 struct sockaddr *nm;
197
198 if ((rtm->rtm_addrs & RTA_NETMASK) == 0)
199 nm = NULL;
200 else {
201 /* skip to gateway */
202 nm = (struct sockaddr *)
203 (ROUNDUP(sa->sa_len) + (char *)sa);
204 /* skip over gateway to netmask */
205 nm = (struct sockaddr *)
206 (ROUNDUP(nm->sa_len) + (char *)nm);
207 }
208
209 p_sockaddr(sa, nm, rtm->rtm_flags, WID_DST);
210 sa = (struct sockaddr *)(ROUNDUP(sa->sa_len) + (char *)sa);
211 p_sockaddr(sa, NULL, 0, WID_GW);
212 }
213 p_flags(rtm->rtm_flags & interesting);
214 putchar('\n');
215 }
216
217
218 /*
219 * Print address family header before a section of the routing table.
220 */
221 static void
222 pr_family(af)
223 int af;
224 {
225 char *afname;
226
227 switch (af) {
228 case AF_INET:
229 afname = "Internet";
230 break;
231 #ifndef SMALL
232 #ifdef INET6
233 case AF_INET6:
234 afname = "Internet6";
235 break;
236 #endif /* INET6 */
237 case AF_NS:
238 afname = "XNS";
239 break;
240 case AF_ISO:
241 afname = "ISO";
242 break;
243 case AF_CCITT:
244 afname = "X.25";
245 break;
246 #endif /* SMALL */
247 case AF_APPLETALK:
248 afname = "AppleTalk";
249 break;
250 default:
251 afname = NULL;
252 break;
253 }
254 if (afname)
255 printf("\n%s:\n", afname);
256 else
257 printf("\nProtocol Family %d:\n", af);
258 }
259
260
261 static void
262 p_sockaddr(sa, nm, flags, width)
263 struct sockaddr *sa, *nm;
264 int flags, width;
265 {
266 char workbuf[128], *cplim;
267 char *cp = workbuf;
268
269 switch(sa->sa_family) {
270
271 case AF_LINK:
272 if (getnameinfo(sa, sa->sa_len, workbuf, sizeof(workbuf),
273 NULL, 0, NI_NUMERICHOST) != 0)
274 strlcpy(workbuf, "invalid", sizeof(workbuf));
275 cp = workbuf;
276 break;
277
278 case AF_INET:
279 cp = routename(sa, nm, flags);
280 break;
281
282 #ifndef SMALL
283 #ifdef INET6
284 case AF_INET6:
285 cp = routename(sa, nm, flags);
286 /* make sure numeric address is not truncated */
287 if (strchr(cp, ':') != NULL && strlen(cp) > width)
288 width = strlen(cp);
289 break;
290 #endif /* INET6 */
291
292 case AF_NS:
293 cp = ns_print((struct sockaddr_ns *)sa);
294 break;
295 #endif /* SMALL */
296
297 default:
298 {
299 u_char *s = (u_char *)sa->sa_data, *slim;
300
301 slim = sa->sa_len + (u_char *) sa;
302 cplim = cp + sizeof(workbuf) - 6;
303 cp += snprintf(cp, cplim - cp, "(%d)", sa->sa_family);
304 while (s < slim && cp < cplim) {
305 cp += snprintf(cp, cplim - cp, " %02x", *s++);
306 if (s < slim)
307 cp += snprintf(cp, cplim - cp, "%02x", *s++);
308 }
309 cp = workbuf;
310 }
311 }
312 if (width < 0 )
313 printf("%s ", cp);
314 else {
315 if (nflag)
316 printf("%-*s ", width, cp);
317 else
318 printf("%-*.*s ", width, width, cp);
319 }
320 }
321
322 static void
323 p_flags(f)
324 int f;
325 {
326 char name[33], *flags;
327 const struct bits *p = bits;
328
329 for (flags = name; p->b_mask; p++)
330 if (p->b_mask & f)
331 *flags++ = p->b_val;
332 *flags = '\0';
333 printf("%-6.6s ", name);
334 }
335
336