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