Raise WARNS to 6:
[dragonfly.git] / sbin / route / show.c
1 /*
2  * $OpenBSD: show.c,v 1.26 2003/08/26 08:33:12 itojun Exp $
3  * $NetBSD: show.c,v 1.1 1996/11/15 18:01:41 gwr Exp $
4  * $DragonFly: src/sbin/route/show.c,v 1.4 2005/03/16 04:18:35 cpressey Exp $
5  */
6 /*
7  * Copyright (c) 1983, 1988, 1993
8  *      The Regents of the University of California.  All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. Neither the name of the University nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  */
34
35 #include <sys/param.h>
36 #include <sys/protosw.h>
37 #include <sys/socket.h>
38 #include <sys/mbuf.h>
39
40 #include <net/if.h>
41 #include <net/if_dl.h>
42 #include <net/if_types.h>
43 #include <net/route.h>
44 #include <netinet/in.h>
45 #include <netns/ns.h>
46 #include <arpa/inet.h>
47
48 #include <sys/sysctl.h>
49
50 #include <stdio.h>
51 #include <stdlib.h>
52 #include <string.h>
53 #include <unistd.h>
54
55 #include <netdb.h>
56
57 #include "extern.h"
58 #include "keywords.h"
59
60 #define ROUNDUP(a) \
61         ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
62 #define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len))
63
64 /*
65  * Definitions for showing gateway flags.
66  */
67 struct bits {
68         int     b_mask;
69         char    b_val;
70 };
71 static const struct bits bits[] = {
72         { RTF_UP,       'U' },
73         { RTF_GATEWAY,  'G' },
74         { RTF_HOST,     'H' },
75         { RTF_REJECT,   'R' },
76         { RTF_BLACKHOLE, 'B' },
77         { RTF_DYNAMIC,  'D' },
78         { RTF_MODIFIED, 'M' },
79         { RTF_DONE,     'd' }, /* Completed -- for routing messages only */
80         { RTF_CLONING,  'C' },
81         { RTF_XRESOLVE, 'X' },
82         { RTF_LLINFO,   'L' },
83         { RTF_STATIC,   'S' },
84         { RTF_PROTO1,   '1' },
85         { RTF_PROTO2,   '2' },
86         { RTF_PROTO3,   '3' },
87         { 0, 0 }
88 };
89
90 static void     p_rtentry(struct rt_msghdr *);
91 static int      p_sockaddr(struct sockaddr *, int, int);
92 static void     p_flags(int, const char *);
93 static void     pr_rthdr(void);
94 static void     pr_family(int);
95
96 /*
97  * Print routing tables.
98  */
99 void
100 show(int argc, char *argv[])
101 {
102         struct rt_msghdr *rtm;
103         char *buf = NULL, *next, *lim = NULL;
104         size_t needed;
105         int mib[6], af = 0;
106         struct sockaddr *sa;
107
108         if (argc > 1) {
109                 argv++;
110                 if (argc == 2 && **argv == '-')
111                     switch (keyword(*argv + 1)) {
112                         case K_INET:
113                                 af = AF_INET;
114                                 break;
115 #ifdef INET6
116                         case K_INET6:
117                                 af = AF_INET6;
118                                 break;
119 #endif
120                         case K_XNS:
121                                 af = AF_NS;
122                                 break;
123                         case K_LINK:
124                                 af = AF_LINK;
125                                 break;
126                         case K_ISO:
127                         case K_OSI:
128                                 af = AF_ISO;
129                                 break;
130                         case K_X25:
131                                 af = AF_CCITT;
132                                 break;
133                         default:
134                                 goto bad;
135                 } else
136 bad:                    usage(*argv);
137         }
138         mib[0] = CTL_NET;
139         mib[1] = PF_ROUTE;
140         mib[2] = 0;
141         mib[3] = 0;
142         mib[4] = NET_RT_DUMP;
143         mib[5] = 0;
144         if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0) {
145                 perror("route-sysctl-estimate");
146                 exit(1);
147         }
148         if (needed > 0) {
149                 if ((buf = malloc(needed)) == 0) {
150                         printf("out of space\n");
151                         exit(1);
152                 }
153                 if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0) {
154                         perror("sysctl of routing table");
155                         exit(1);
156                 }
157                 lim  = buf + needed;
158         }
159
160         printf("Routing tables\n");
161
162         if (buf) {
163                 for (next = buf; next < lim; next += rtm->rtm_msglen) {
164                         rtm = (struct rt_msghdr *)next;
165                         sa = (struct sockaddr *)(rtm + 1);
166                         if (af && sa->sa_family != af)
167                                 continue;
168                         p_rtentry(rtm);
169                 }
170                 free(buf);
171         }
172 }
173
174 /* column widths; each followed by one space */
175 #define WID_DST         (nflag ? 20 : 32)       /* destination column width */
176 #define WID_GW          (nflag ? 20 : 32)       /* gateway column width */
177
178 /*
179  * Print header for routing table columns.
180  */
181 static void
182 pr_rthdr(void)
183 {
184         printf("%-*.*s %-*.*s %-6.6s\n",
185             WID_DST, WID_DST, "Destination",
186             WID_GW, WID_GW, "Gateway",
187             "Flags");
188 }
189
190 /*
191  * Print a routing table entry.
192  */
193 static void
194 p_rtentry(struct rt_msghdr *rtm)
195 {
196         struct sockaddr *sa = (struct sockaddr *)(rtm + 1);
197 #ifdef notdef
198         static int masks_done, banner_printed;
199 #endif
200         static int old_af;
201         int af = 0, interesting = RTF_UP | RTF_GATEWAY | RTF_HOST;
202         int width;
203
204 #ifdef notdef
205         /* for the moment, netmasks are skipped over */
206         if (!banner_printed) {
207                 printf("Netmasks:\n");
208                 banner_printed = 1;
209         }
210         if (masks_done == 0) {
211                 if (rtm->rtm_addrs != RTA_DST ) {
212                         masks_done = 1;
213                         af = sa->sa_family;
214                 }
215         } else
216 #endif
217                 af = sa->sa_family;
218         if (old_af != af) {
219                 old_af = af;
220                 pr_family(af);
221                 pr_rthdr();
222         }
223
224         /*
225          * Print the information.  If wflag is set p_sockaddr() can return
226          * a wider width then specified and we try to fit the second
227          * address in any remaining space so the flags still lines up.
228          */
229         if (rtm->rtm_addrs == RTA_DST) {
230                 p_sockaddr(sa, 0, WID_DST + WID_GW + 2);
231         } else {
232                 width = p_sockaddr(sa, rtm->rtm_flags, WID_DST);
233                 sa = (struct sockaddr *)(ROUNDUP(sa->sa_len) + (char *)sa);
234                 p_sockaddr(sa, 0, WID_GW + WID_DST - width);
235         }
236         p_flags(rtm->rtm_flags & interesting, "%-6.6s ");
237         putchar('\n');
238 }
239
240 /*
241  * Print address family header before a section of the routing table.
242  */
243 static void
244 pr_family(int af)
245 {
246         const char *afname;
247
248         switch (af) {
249         case AF_INET:
250                 afname = "Internet";
251                 break;
252 #ifdef INET6
253         case AF_INET6:
254                 afname = "Internet6";
255                 break;
256 #endif /* INET6 */
257         case AF_NS:
258                 afname = "XNS";
259                 break;
260         case AF_IPX:
261                 afname = "IPX";
262                 break;
263         case AF_ISO:
264                 afname = "ISO";
265                 break;
266         case AF_CCITT:
267                 afname = "X.25";
268                 break;
269         case AF_APPLETALK:
270                 afname = "AppleTalk";
271                 break;
272         default:
273                 afname = NULL;
274                 break;
275         }
276         if (afname)
277                 printf("\n%s:\n", afname);
278         else
279                 printf("\nProtocol Family %d:\n", af);
280 }
281
282 static int
283 p_sockaddr(struct sockaddr *sa, int flags, int width)
284 {
285         char workbuf[128];
286         char *cp = workbuf;
287         const char *cplim;
288         int len = sizeof(workbuf);
289         int count;
290
291         switch(sa->sa_family) {
292
293         case AF_LINK:
294             {
295                 struct sockaddr_dl *sdl = (struct sockaddr_dl *)sa;
296
297                 if (sdl->sdl_nlen == 0 && sdl->sdl_alen == 0 &&
298                     sdl->sdl_slen == 0)
299                         snprintf(workbuf, sizeof(workbuf),
300                             "link#%d", sdl->sdl_index);
301                 else {
302                         switch (sdl->sdl_type) {
303                         case IFT_ETHER:
304                             {
305                                 int i;
306                                 u_char *lla = (u_char *)sdl->sdl_data +
307                                     sdl->sdl_nlen;
308
309                                 cplim = "";
310                                 for (i = 0; i < sdl->sdl_alen; i++, lla++) {
311                                         snprintf(cp, len, "%s%x", cplim, *lla);
312                                         len -= strlen(cp);
313                                         cp += strlen(cp);
314                                         if (len <= 0)
315                                                 break;  /* overflow */
316                                         cplim = ":";
317                                 }
318                                 cp = workbuf;
319                                 break;
320                             }
321                         default:
322                                 cp = link_ntoa(sdl);
323                                 break;
324                         }
325                 }
326                 break;
327             }
328
329         case AF_INET:
330             {
331                 struct sockaddr_in *in = (struct sockaddr_in *)sa;
332
333                 if (in->sin_addr.s_addr == 0) {
334                         /* cp points to workbuf */
335                         strncpy(cp, "default", len);
336                 } else
337                         cp = (flags & RTF_HOST) ? routename(sa) : netname(sa);
338                 break;
339             }
340
341 #ifdef INET6
342         case AF_INET6:
343             {
344                 struct sockaddr_in6 *in6 = (struct sockaddr_in6 *)sa;
345
346                 if (IN6_IS_ADDR_UNSPECIFIED(&in6->sin6_addr)) {
347                         /* cp points to workbuf */
348                         strncpy(cp, "default", len);
349                 } else {
350                         cp = ((flags & RTF_HOST) ? routename(sa)
351                                                  : netname(sa));
352                 }
353                 /* make sure numeric address is not truncated */
354                 if (strchr(cp, ':') != NULL &&
355                     (width < 0 || strlen(cp) > (size_t)width))
356                         width = strlen(cp);
357                 break;
358             }
359 #endif /* INET6 */
360
361         case AF_NS:
362                 cp = ns_print((struct sockaddr_ns *)sa);
363                 break;
364
365         default:
366             {
367                 u_char *s = (u_char *)sa->sa_data, *slim;
368
369                 slim =  sa->sa_len + (u_char *) sa;
370                 cplim = cp + sizeof(workbuf) - 6;
371                 snprintf(cp, len, "(%d)", sa->sa_family);
372                 len -= strlen(cp);
373                 cp += strlen(cp);
374                 if (len <= 0) {
375                         cp = workbuf;
376                         break;          /* overflow */
377                 }
378                 while (s < slim && cp < cplim) {
379                         snprintf(cp, len, " %02x", *s++);
380                         len -= strlen(cp);
381                         cp += strlen(cp);
382                         if (len <= 0)
383                                 break;          /* overflow */
384                         if (s < slim) {
385                                 snprintf(cp, len, "%02x", *s++);
386                                 len -= strlen(cp);
387                                 cp += strlen(cp);
388                                 if (len <= 0)
389                                         break;          /* overflow */
390                         }
391                 }
392                 cp = workbuf;
393             }
394         }
395         if (width < 0 ) {
396                 count = printf("%s ", cp);
397         } else {
398                 if (nflag || wflag)
399                         count = printf("%-*s ", width, cp);
400                 else
401                         count = printf("%-*.*s ", width, width, cp);
402         }
403         return(count);
404 }
405
406 static void
407 p_flags(int f, const char *format)
408 {
409         char name[33], *flags;
410         const struct bits *p = bits;
411
412         for (flags = name; p->b_mask && flags < &name[sizeof(name-2)]; p++) {
413                 if (p->b_mask & f)
414                         *flags++ = p->b_val;
415         }
416         *flags = '\0';
417         printf(format, name);
418 }