show.c revision 1.31 1 1.31 dogcow /* $NetBSD: show.c,v 1.31 2006/09/07 02:40:31 dogcow Exp $ */
2 1.1 gwr
3 1.1 gwr /*
4 1.1 gwr * Copyright (c) 1983, 1988, 1993
5 1.1 gwr * The Regents of the University of California. All rights reserved.
6 1.1 gwr *
7 1.1 gwr * Redistribution and use in source and binary forms, with or without
8 1.1 gwr * modification, are permitted provided that the following conditions
9 1.1 gwr * are met:
10 1.1 gwr * 1. Redistributions of source code must retain the above copyright
11 1.1 gwr * notice, this list of conditions and the following disclaimer.
12 1.1 gwr * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 gwr * notice, this list of conditions and the following disclaimer in the
14 1.1 gwr * documentation and/or other materials provided with the distribution.
15 1.21 agc * 3. Neither the name of the University nor the names of its contributors
16 1.1 gwr * may be used to endorse or promote products derived from this software
17 1.1 gwr * without specific prior written permission.
18 1.1 gwr *
19 1.1 gwr * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 1.1 gwr * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 1.1 gwr * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 1.1 gwr * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 1.1 gwr * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 1.1 gwr * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 1.1 gwr * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 1.1 gwr * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 1.1 gwr * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 1.1 gwr * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 1.1 gwr * SUCH DAMAGE.
30 1.1 gwr */
31 1.1 gwr
32 1.4 lukem #include <sys/cdefs.h>
33 1.1 gwr #ifndef lint
34 1.1 gwr #if 0
35 1.1 gwr static char sccsid[] = "from: @(#)route.c 8.3 (Berkeley) 3/9/94";
36 1.1 gwr #else
37 1.31 dogcow __RCSID("$NetBSD: show.c,v 1.31 2006/09/07 02:40:31 dogcow Exp $");
38 1.1 gwr #endif
39 1.1 gwr #endif /* not lint */
40 1.1 gwr
41 1.1 gwr #include <sys/param.h>
42 1.1 gwr #include <sys/protosw.h>
43 1.1 gwr #include <sys/socket.h>
44 1.1 gwr #include <sys/mbuf.h>
45 1.1 gwr
46 1.1 gwr #include <net/if.h>
47 1.1 gwr #include <net/if_dl.h>
48 1.1 gwr #include <net/if_types.h>
49 1.1 gwr #include <net/route.h>
50 1.1 gwr #include <netinet/in.h>
51 1.1 gwr
52 1.1 gwr #include <sys/sysctl.h>
53 1.1 gwr
54 1.1 gwr #include <netdb.h>
55 1.1 gwr #include <stdio.h>
56 1.1 gwr #include <stdlib.h>
57 1.1 gwr #include <string.h>
58 1.1 gwr #include <unistd.h>
59 1.5 christos #include <err.h>
60 1.1 gwr
61 1.26 ginsbach #include "keywords.h"
62 1.2 christos #include "extern.h"
63 1.1 gwr
64 1.12 itojun #define ROUNDUP(a) \
65 1.12 itojun ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
66 1.12 itojun #define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len))
67 1.12 itojun
68 1.1 gwr /*
69 1.1 gwr * Definitions for showing gateway flags.
70 1.1 gwr */
71 1.1 gwr struct bits {
72 1.1 gwr short b_mask;
73 1.1 gwr char b_val;
74 1.1 gwr };
75 1.1 gwr static const struct bits bits[] = {
76 1.1 gwr { RTF_UP, 'U' },
77 1.1 gwr { RTF_GATEWAY, 'G' },
78 1.1 gwr { RTF_HOST, 'H' },
79 1.1 gwr { RTF_REJECT, 'R' },
80 1.1 gwr { RTF_DYNAMIC, 'D' },
81 1.1 gwr { RTF_MODIFIED, 'M' },
82 1.1 gwr { RTF_DONE, 'd' }, /* Completed -- for routing messages only */
83 1.1 gwr { RTF_MASK, 'm' }, /* Mask Present -- for routing messages only */
84 1.1 gwr { RTF_CLONING, 'C' },
85 1.1 gwr { RTF_XRESOLVE, 'X' },
86 1.1 gwr { RTF_LLINFO, 'L' },
87 1.1 gwr { RTF_STATIC, 'S' },
88 1.16 itojun { RTF_BLACKHOLE, 'B' },
89 1.17 itojun { RTF_CLONED, 'c' },
90 1.1 gwr { RTF_PROTO1, '1' },
91 1.1 gwr { RTF_PROTO2, '2' },
92 1.1 gwr { 0 }
93 1.1 gwr };
94 1.1 gwr
95 1.23 ginsbach static void pr_rthdr(int);
96 1.22 xtraeme static void p_rtentry(struct rt_msghdr *);
97 1.22 xtraeme static void pr_family(int);
98 1.22 xtraeme static void p_sockaddr(struct sockaddr *, struct sockaddr *, int, int );
99 1.22 xtraeme static void p_flags(int);
100 1.1 gwr
101 1.1 gwr /*
102 1.1 gwr * Print routing tables.
103 1.1 gwr */
104 1.1 gwr void
105 1.22 xtraeme show(int argc, char **argv)
106 1.1 gwr {
107 1.1 gwr size_t needed;
108 1.26 ginsbach int af, mib[6];
109 1.1 gwr char *buf, *next, *lim;
110 1.4 lukem struct rt_msghdr *rtm;
111 1.26 ginsbach struct sockaddr *sa;
112 1.1 gwr
113 1.26 ginsbach af = AF_UNSPEC;
114 1.26 ginsbach if (argc > 1) {
115 1.26 ginsbach argv++;
116 1.26 ginsbach if (argc == 2 && **argv == '-')
117 1.26 ginsbach switch (keyword(*argv + 1)) {
118 1.26 ginsbach case K_INET:
119 1.26 ginsbach af = AF_INET;
120 1.26 ginsbach break;
121 1.26 ginsbach #ifdef INET6
122 1.26 ginsbach case K_INET6:
123 1.26 ginsbach af = AF_INET6;
124 1.26 ginsbach break;
125 1.26 ginsbach #endif
126 1.26 ginsbach #ifndef SMALL
127 1.26 ginsbach case K_ATALK:
128 1.26 ginsbach af = AF_APPLETALK;
129 1.26 ginsbach break;
130 1.26 ginsbach #endif /* SMALL */
131 1.26 ginsbach #if 0
132 1.26 ginsbach /* XXX Links are never destinations */
133 1.26 ginsbach case K_LINK:
134 1.26 ginsbach af = AF_LINK;
135 1.26 ginsbach break;
136 1.26 ginsbach #endif
137 1.26 ginsbach #ifndef SMALL
138 1.26 ginsbach case K_ISO:
139 1.26 ginsbach case K_OSI:
140 1.26 ginsbach af = AF_ISO;
141 1.26 ginsbach break;
142 1.26 ginsbach #endif /* SMALL */
143 1.26 ginsbach default:
144 1.26 ginsbach goto bad;
145 1.26 ginsbach } else
146 1.26 ginsbach bad: usage(*argv);
147 1.26 ginsbach }
148 1.1 gwr mib[0] = CTL_NET;
149 1.1 gwr mib[1] = PF_ROUTE;
150 1.1 gwr mib[2] = 0;
151 1.1 gwr mib[3] = 0;
152 1.1 gwr mib[4] = NET_RT_DUMP;
153 1.1 gwr mib[5] = 0;
154 1.25 ginsbach if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
155 1.25 ginsbach err(1, "route-sysctl-estimate");
156 1.27 ginsbach buf = lim = NULL;
157 1.27 ginsbach if (needed) {
158 1.27 ginsbach if ((buf = malloc(needed)) == 0)
159 1.27 ginsbach err(1, "malloc");
160 1.27 ginsbach if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0)
161 1.27 ginsbach err(1, "sysctl of routing table");
162 1.27 ginsbach lim = buf + needed;
163 1.27 ginsbach }
164 1.1 gwr
165 1.26 ginsbach printf("Routing table%s\n", (af == AF_UNSPEC)? "s" : "");
166 1.1 gwr
167 1.27 ginsbach if (needed) {
168 1.1 gwr for (next = buf; next < lim; next += rtm->rtm_msglen) {
169 1.1 gwr rtm = (struct rt_msghdr *)next;
170 1.26 ginsbach sa = (struct sockaddr *)(rtm + 1);
171 1.26 ginsbach if (af == AF_UNSPEC || af == sa->sa_family)
172 1.26 ginsbach p_rtentry(rtm);
173 1.1 gwr }
174 1.27 ginsbach free(buf);
175 1.1 gwr }
176 1.1 gwr }
177 1.1 gwr
178 1.1 gwr
179 1.1 gwr /* column widths; each followed by one space */
180 1.23 ginsbach #ifndef INET6
181 1.23 ginsbach #define WID_DST(af) 18 /* width of destination column */
182 1.23 ginsbach #define WID_GW(af) 18 /* width of gateway column */
183 1.23 ginsbach #else
184 1.23 ginsbach /* width of destination/gateway column */
185 1.23 ginsbach #if 1
186 1.23 ginsbach /* strlen("fe80::aaaa:bbbb:cccc:dddd@gif0") == 30, strlen("/128") == 4 */
187 1.23 ginsbach #define WID_DST(af) ((af) == AF_INET6 ? (nflag ? 34 : 18) : 18)
188 1.23 ginsbach #define WID_GW(af) ((af) == AF_INET6 ? (nflag ? 30 : 18) : 18)
189 1.23 ginsbach #else
190 1.23 ginsbach /* strlen("fe80::aaaa:bbbb:cccc:dddd") == 25, strlen("/128") == 4 */
191 1.23 ginsbach #define WID_DST(af) ((af) == AF_INET6 ? (nflag ? 29 : 18) : 18)
192 1.23 ginsbach #define WID_GW(af) ((af) == AF_INET6 ? (nflag ? 25 : 18) : 18)
193 1.23 ginsbach #endif
194 1.23 ginsbach #endif /* INET6 */
195 1.1 gwr
196 1.1 gwr /*
197 1.1 gwr * Print header for routing table columns.
198 1.1 gwr */
199 1.1 gwr static void
200 1.23 ginsbach pr_rthdr(int af)
201 1.1 gwr {
202 1.1 gwr
203 1.1 gwr printf("%-*.*s %-*.*s %-6.6s\n",
204 1.23 ginsbach WID_DST(af), WID_DST(af), "Destination",
205 1.23 ginsbach WID_GW(af), WID_GW(af), "Gateway",
206 1.1 gwr "Flags");
207 1.1 gwr }
208 1.1 gwr
209 1.1 gwr
210 1.1 gwr /*
211 1.1 gwr * Print a routing table entry.
212 1.1 gwr */
213 1.1 gwr static void
214 1.22 xtraeme p_rtentry(struct rt_msghdr *rtm)
215 1.1 gwr {
216 1.4 lukem struct sockaddr *sa = (struct sockaddr *)(rtm + 1);
217 1.1 gwr #ifdef notdef
218 1.1 gwr static int masks_done, banner_printed;
219 1.1 gwr #endif
220 1.1 gwr static int old_af;
221 1.19 atatat int af = 0, interesting = RTF_UP | RTF_GATEWAY | RTF_HOST | RTF_REJECT;
222 1.1 gwr
223 1.1 gwr #ifdef notdef
224 1.1 gwr /* for the moment, netmasks are skipped over */
225 1.1 gwr if (!banner_printed) {
226 1.1 gwr printf("Netmasks:\n");
227 1.1 gwr banner_printed = 1;
228 1.1 gwr }
229 1.1 gwr if (masks_done == 0) {
230 1.1 gwr if (rtm->rtm_addrs != RTA_DST ) {
231 1.1 gwr masks_done = 1;
232 1.1 gwr af = sa->sa_family;
233 1.1 gwr }
234 1.1 gwr } else
235 1.1 gwr #endif
236 1.1 gwr af = sa->sa_family;
237 1.1 gwr if (old_af != af) {
238 1.1 gwr old_af = af;
239 1.1 gwr pr_family(af);
240 1.23 ginsbach pr_rthdr(af);
241 1.1 gwr }
242 1.1 gwr if (rtm->rtm_addrs == RTA_DST)
243 1.23 ginsbach p_sockaddr(sa, NULL, 0, WID_DST(af) + 1 + WID_GW(af) + 1);
244 1.1 gwr else {
245 1.19 atatat struct sockaddr *nm;
246 1.19 atatat
247 1.19 atatat if ((rtm->rtm_addrs & RTA_NETMASK) == 0)
248 1.19 atatat nm = NULL;
249 1.19 atatat else {
250 1.19 atatat /* skip to gateway */
251 1.19 atatat nm = (struct sockaddr *)
252 1.19 atatat (ROUNDUP(sa->sa_len) + (char *)sa);
253 1.19 atatat /* skip over gateway to netmask */
254 1.19 atatat nm = (struct sockaddr *)
255 1.19 atatat (ROUNDUP(nm->sa_len) + (char *)nm);
256 1.19 atatat }
257 1.19 atatat
258 1.23 ginsbach p_sockaddr(sa, nm, rtm->rtm_flags, WID_DST(af));
259 1.12 itojun sa = (struct sockaddr *)(ROUNDUP(sa->sa_len) + (char *)sa);
260 1.23 ginsbach p_sockaddr(sa, NULL, 0, WID_GW(af));
261 1.1 gwr }
262 1.15 is p_flags(rtm->rtm_flags & interesting);
263 1.1 gwr putchar('\n');
264 1.1 gwr }
265 1.1 gwr
266 1.1 gwr
267 1.1 gwr /*
268 1.1 gwr * Print address family header before a section of the routing table.
269 1.1 gwr */
270 1.1 gwr static void
271 1.22 xtraeme pr_family(int af)
272 1.1 gwr {
273 1.24 christos const char *afname;
274 1.1 gwr
275 1.1 gwr switch (af) {
276 1.1 gwr case AF_INET:
277 1.1 gwr afname = "Internet";
278 1.1 gwr break;
279 1.9 itojun #ifdef INET6
280 1.9 itojun case AF_INET6:
281 1.9 itojun afname = "Internet6";
282 1.9 itojun break;
283 1.9 itojun #endif /* INET6 */
284 1.28 ginsbach #ifndef SMALL
285 1.1 gwr case AF_ISO:
286 1.1 gwr afname = "ISO";
287 1.1 gwr break;
288 1.8 lukem #endif /* SMALL */
289 1.6 kleink case AF_APPLETALK:
290 1.6 kleink afname = "AppleTalk";
291 1.1 gwr break;
292 1.1 gwr default:
293 1.1 gwr afname = NULL;
294 1.1 gwr break;
295 1.1 gwr }
296 1.1 gwr if (afname)
297 1.1 gwr printf("\n%s:\n", afname);
298 1.1 gwr else
299 1.1 gwr printf("\nProtocol Family %d:\n", af);
300 1.1 gwr }
301 1.1 gwr
302 1.1 gwr
303 1.1 gwr static void
304 1.22 xtraeme p_sockaddr(struct sockaddr *sa, struct sockaddr *nm, int flags, int width)
305 1.1 gwr {
306 1.24 christos char workbuf[128];
307 1.24 christos const char *cp;
308 1.1 gwr
309 1.1 gwr switch(sa->sa_family) {
310 1.1 gwr
311 1.1 gwr case AF_LINK:
312 1.18 bjh21 if (getnameinfo(sa, sa->sa_len, workbuf, sizeof(workbuf),
313 1.18 bjh21 NULL, 0, NI_NUMERICHOST) != 0)
314 1.20 itojun strlcpy(workbuf, "invalid", sizeof(workbuf));
315 1.18 bjh21 cp = workbuf;
316 1.1 gwr break;
317 1.1 gwr
318 1.1 gwr case AF_INET:
319 1.19 atatat cp = routename(sa, nm, flags);
320 1.1 gwr break;
321 1.1 gwr
322 1.9 itojun #ifdef INET6
323 1.9 itojun case AF_INET6:
324 1.19 atatat cp = routename(sa, nm, flags);
325 1.11 itojun /* make sure numeric address is not truncated */
326 1.11 itojun if (strchr(cp, ':') != NULL && strlen(cp) > width)
327 1.11 itojun width = strlen(cp);
328 1.9 itojun break;
329 1.9 itojun #endif /* INET6 */
330 1.9 itojun
331 1.28 ginsbach #ifndef SMALL
332 1.1 gwr #endif /* SMALL */
333 1.1 gwr
334 1.1 gwr default:
335 1.1 gwr {
336 1.4 lukem u_char *s = (u_char *)sa->sa_data, *slim;
337 1.24 christos char *wp = workbuf, *wplim;
338 1.1 gwr
339 1.25 ginsbach slim = sa->sa_len + (u_char *)sa;
340 1.24 christos wplim = wp + sizeof(workbuf) - 6;
341 1.24 christos wp += snprintf(wp, wplim - wp, "(%d)", sa->sa_family);
342 1.24 christos while (s < slim && wp < wplim) {
343 1.24 christos wp += snprintf(wp, wplim - wp, " %02x", *s++);
344 1.1 gwr if (s < slim)
345 1.24 christos wp += snprintf(wp, wplim - wp, "%02x", *s++);
346 1.1 gwr }
347 1.1 gwr cp = workbuf;
348 1.1 gwr }
349 1.1 gwr }
350 1.1 gwr if (width < 0 )
351 1.1 gwr printf("%s ", cp);
352 1.1 gwr else {
353 1.1 gwr if (nflag)
354 1.1 gwr printf("%-*s ", width, cp);
355 1.1 gwr else
356 1.1 gwr printf("%-*.*s ", width, width, cp);
357 1.1 gwr }
358 1.1 gwr }
359 1.1 gwr
360 1.1 gwr static void
361 1.22 xtraeme p_flags(int f)
362 1.1 gwr {
363 1.1 gwr char name[33], *flags;
364 1.4 lukem const struct bits *p = bits;
365 1.1 gwr
366 1.1 gwr for (flags = name; p->b_mask; p++)
367 1.1 gwr if (p->b_mask & f)
368 1.1 gwr *flags++ = p->b_val;
369 1.29 christos else if (Sflag)
370 1.29 christos *flags++ = ' ';
371 1.1 gwr *flags = '\0';
372 1.15 is printf("%-6.6s ", name);
373 1.1 gwr }
374 1.1 gwr
375