show.c revision 1.22 1 /* $NetBSD: show.c,v 1.22 2005/02/05 14:05:23 xtraeme 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.22 2005/02/05 14:05:23 xtraeme 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(void);
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 perror("route-sysctl-estimate");
120 exit(1);
121 }
122 if ((buf = malloc(needed)) == 0)
123 err(1, NULL);
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 17 /* 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(void)
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(struct rt_msghdr *rtm)
163 {
164 struct sockaddr *sa = (struct sockaddr *)(rtm + 1);
165 #ifdef notdef
166 static int masks_done, banner_printed;
167 #endif
168 static int old_af;
169 int af = 0, interesting = RTF_UP | RTF_GATEWAY | RTF_HOST | RTF_REJECT;
170
171 #ifdef notdef
172 /* for the moment, netmasks are skipped over */
173 if (!banner_printed) {
174 printf("Netmasks:\n");
175 banner_printed = 1;
176 }
177 if (masks_done == 0) {
178 if (rtm->rtm_addrs != RTA_DST ) {
179 masks_done = 1;
180 af = sa->sa_family;
181 }
182 } else
183 #endif
184 af = sa->sa_family;
185 if (old_af != af) {
186 old_af = af;
187 pr_family(af);
188 pr_rthdr();
189 }
190 if (rtm->rtm_addrs == RTA_DST)
191 p_sockaddr(sa, NULL, 0, WID_DST + 1 + WID_GW + 1);
192 else {
193 struct sockaddr *nm;
194
195 if ((rtm->rtm_addrs & RTA_NETMASK) == 0)
196 nm = NULL;
197 else {
198 /* skip to gateway */
199 nm = (struct sockaddr *)
200 (ROUNDUP(sa->sa_len) + (char *)sa);
201 /* skip over gateway to netmask */
202 nm = (struct sockaddr *)
203 (ROUNDUP(nm->sa_len) + (char *)nm);
204 }
205
206 p_sockaddr(sa, nm, rtm->rtm_flags, WID_DST);
207 sa = (struct sockaddr *)(ROUNDUP(sa->sa_len) + (char *)sa);
208 p_sockaddr(sa, NULL, 0, WID_GW);
209 }
210 p_flags(rtm->rtm_flags & interesting);
211 putchar('\n');
212 }
213
214
215 /*
216 * Print address family header before a section of the routing table.
217 */
218 static void
219 pr_family(int af)
220 {
221 char *afname;
222
223 switch (af) {
224 case AF_INET:
225 afname = "Internet";
226 break;
227 #ifndef SMALL
228 #ifdef INET6
229 case AF_INET6:
230 afname = "Internet6";
231 break;
232 #endif /* INET6 */
233 case AF_NS:
234 afname = "XNS";
235 break;
236 case AF_ISO:
237 afname = "ISO";
238 break;
239 case AF_CCITT:
240 afname = "X.25";
241 break;
242 #endif /* SMALL */
243 case AF_APPLETALK:
244 afname = "AppleTalk";
245 break;
246 default:
247 afname = NULL;
248 break;
249 }
250 if (afname)
251 printf("\n%s:\n", afname);
252 else
253 printf("\nProtocol Family %d:\n", af);
254 }
255
256
257 static void
258 p_sockaddr(struct sockaddr *sa, struct sockaddr *nm, int flags, int width)
259 {
260 char workbuf[128], *cplim;
261 char *cp = workbuf;
262
263 switch(sa->sa_family) {
264
265 case AF_LINK:
266 if (getnameinfo(sa, sa->sa_len, workbuf, sizeof(workbuf),
267 NULL, 0, NI_NUMERICHOST) != 0)
268 strlcpy(workbuf, "invalid", sizeof(workbuf));
269 cp = workbuf;
270 break;
271
272 case AF_INET:
273 cp = routename(sa, nm, flags);
274 break;
275
276 #ifndef SMALL
277 #ifdef INET6
278 case AF_INET6:
279 cp = routename(sa, nm, flags);
280 /* make sure numeric address is not truncated */
281 if (strchr(cp, ':') != NULL && strlen(cp) > width)
282 width = strlen(cp);
283 break;
284 #endif /* INET6 */
285
286 case AF_NS:
287 cp = ns_print((struct sockaddr_ns *)sa);
288 break;
289 #endif /* SMALL */
290
291 default:
292 {
293 u_char *s = (u_char *)sa->sa_data, *slim;
294
295 slim = sa->sa_len + (u_char *) sa;
296 cplim = cp + sizeof(workbuf) - 6;
297 cp += snprintf(cp, cplim - cp, "(%d)", sa->sa_family);
298 while (s < slim && cp < cplim) {
299 cp += snprintf(cp, cplim - cp, " %02x", *s++);
300 if (s < slim)
301 cp += snprintf(cp, cplim - cp, "%02x", *s++);
302 }
303 cp = workbuf;
304 }
305 }
306 if (width < 0 )
307 printf("%s ", cp);
308 else {
309 if (nflag)
310 printf("%-*s ", width, cp);
311 else
312 printf("%-*.*s ", width, width, cp);
313 }
314 }
315
316 static void
317 p_flags(int f)
318 {
319 char name[33], *flags;
320 const struct bits *p = bits;
321
322 for (flags = name; p->b_mask; p++)
323 if (p->b_mask & f)
324 *flags++ = p->b_val;
325 *flags = '\0';
326 printf("%-6.6s ", name);
327 }
328
329