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