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