kernel: Remove support for the Xerox Network Systems (NS) protocol.
[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.6 2006/01/19 22:19:30 dillon 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 <arpa/inet.h>
46
47 #include <sys/sysctl.h>
48
49 #include <stdio.h>
50 #include <stdlib.h>
51 #include <string.h>
52 #include <unistd.h>
53
54 #include <netdb.h>
55
56 #include "extern.h"
57 #include "keywords.h"
58
59 #define ROUNDUP(a) \
60         ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
61 #define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len))
62
63 /*
64  * Definitions for showing gateway flags.
65  */
66 struct bits {
67         int     b_mask;
68         char    b_val;
69 };
70 static const struct bits bits[] = {
71         { RTF_UP,       'U' },
72         { RTF_GATEWAY,  'G' },
73         { RTF_HOST,     'H' },
74         { RTF_REJECT,   'R' },
75         { RTF_BLACKHOLE, 'B' },
76         { RTF_DYNAMIC,  'D' },
77         { RTF_MODIFIED, 'M' },
78         { RTF_DONE,     'd' }, /* Completed -- for routing messages only */
79         { RTF_CLONING,  'C' },
80         { RTF_XRESOLVE, 'X' },
81         { RTF_LLINFO,   'L' },
82         { RTF_STATIC,   'S' },
83         { RTF_PROTO1,   '1' },
84         { RTF_PROTO2,   '2' },
85         { RTF_PROTO3,   '3' },
86         { 0, 0 }
87 };
88
89 static void     p_rtentry(struct rt_msghdr *);
90 static int      p_sockaddr(struct sockaddr *, int, int);
91 static void     p_flags(int, const char *);
92 static void     pr_rthdr(void);
93 static void     pr_family(int);
94
95 /*
96  * Print routing tables.
97  */
98 void
99 show(int argc, char *argv[])
100 {
101         struct rt_msghdr *rtm;
102         char *buf = NULL, *next, *lim = NULL;
103         size_t needed;
104         int mib[7], af = 0;
105         int miblen;
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                     }
136                 } else
137 bad:                    usage(*argv);
138         }
139         mib[0] = CTL_NET;
140         mib[1] = PF_ROUTE;
141         mib[2] = 0;
142         mib[3] = 0;
143         mib[4] = NET_RT_DUMP;
144         mib[5] = 0;
145         if (cpuflag >= 0) {
146                 mib[6] = cpuflag;
147                 miblen = 7;
148         } else {
149                 miblen = 6;
150         }
151         if (sysctl(mib, miblen, NULL, &needed, NULL, 0) < 0)    {
152                 perror("route-sysctl-estimate");
153                 exit(1);
154         }
155         if (needed > 0) {
156                 if ((buf = malloc(needed)) == NULL) {
157                         printf("out of space\n");
158                         exit(1);
159                 }
160                 if (sysctl(mib, miblen, buf, &needed, NULL, 0) < 0) {
161                         perror("sysctl of routing table");
162                         exit(1);
163                 }
164                 lim  = buf + needed;
165         }
166
167         printf("Routing tables\n");
168
169         if (buf != NULL) {
170                 for (next = buf; next < lim; next += rtm->rtm_msglen) {
171                         rtm = (struct rt_msghdr *)next;
172                         sa = (struct sockaddr *)(rtm + 1);
173                         if (af != 0 && sa->sa_family != af)
174                                 continue;
175                         p_rtentry(rtm);
176                 }
177                 free(buf);
178         }
179 }
180
181 /* column widths; each followed by one space */
182 #define WID_DST         (nflag ? 20 : 32)       /* destination column width */
183 #define WID_GW          (nflag ? 20 : 32)       /* gateway column width */
184
185 /*
186  * Print header for routing table columns.
187  */
188 static void
189 pr_rthdr(void)
190 {
191         printf("%-*.*s %-*.*s %-6.6s\n",
192             WID_DST, WID_DST, "Destination",
193             WID_GW, WID_GW, "Gateway",
194             "Flags");
195 }
196
197 /*
198  * Print a routing table entry.
199  */
200 static void
201 p_rtentry(struct rt_msghdr *rtm)
202 {
203         struct sockaddr *sa = (struct sockaddr *)(rtm + 1);
204 #ifdef notdef
205         static int masks_done, banner_printed;
206 #endif
207         static int old_af;
208         int af = 0, interesting = RTF_UP | RTF_GATEWAY | RTF_HOST;
209         int width;
210
211 #ifdef notdef
212         /* for the moment, netmasks are skipped over */
213         if (!banner_printed) {
214                 printf("Netmasks:\n");
215                 banner_printed = 1;
216         }
217         if (masks_done == 0) {
218                 if (rtm->rtm_addrs != RTA_DST) {
219                         masks_done = 1;
220                         af = sa->sa_family;
221                 }
222         } else
223 #endif
224                 af = sa->sa_family;
225         if (old_af != af) {
226                 old_af = af;
227                 pr_family(af);
228                 pr_rthdr();
229         }
230
231         /*
232          * Print the information.  If wflag is set p_sockaddr() can return
233          * a wider width then specified and we try to fit the second
234          * address in any remaining space so the flags still lines up.
235          */
236         if (rtm->rtm_addrs == RTA_DST) {
237                 p_sockaddr(sa, 0, WID_DST + WID_GW + 2);
238         } else {
239                 width = p_sockaddr(sa, rtm->rtm_flags, WID_DST);
240                 sa = (struct sockaddr *)(ROUNDUP(sa->sa_len) + (char *)sa);
241                 p_sockaddr(sa, 0, WID_GW + WID_DST - width);
242         }
243         p_flags(rtm->rtm_flags & interesting, "%-6.6s ");
244         putchar('\n');
245 }
246
247 /*
248  * Print address family header before a section of the routing table.
249  */
250 static void
251 pr_family(int af)
252 {
253         const char *afname;
254
255         switch (af) {
256         case AF_INET:
257                 afname = "Internet";
258                 break;
259 #ifdef INET6
260         case AF_INET6:
261                 afname = "Internet6";
262                 break;
263 #endif /* INET6 */
264         case AF_NS:
265                 afname = "XNS";
266                 break;
267         case AF_IPX:
268                 afname = "IPX";
269                 break;
270         case AF_ISO:
271                 afname = "ISO";
272                 break;
273         case AF_CCITT:
274                 afname = "X.25";
275                 break;
276         case AF_APPLETALK:
277                 afname = "AppleTalk";
278                 break;
279         default:
280                 afname = NULL;
281                 break;
282         }
283         if (afname != NULL)
284                 printf("\n%s:\n", afname);
285         else
286                 printf("\nProtocol Family %d:\n", af);
287 }
288
289 static int
290 p_sockaddr(struct sockaddr *sa, int flags, int width)
291 {
292         char workbuf[128];
293         char *cp = workbuf;
294         const char *cplim;
295         int len = sizeof(workbuf);
296         int count;
297
298         switch(sa->sa_family) {
299
300         case AF_LINK:
301             {
302                 struct sockaddr_dl *sdl = (struct sockaddr_dl *)sa;
303
304                 if (sdl->sdl_nlen == 0 && sdl->sdl_alen == 0 &&
305                     sdl->sdl_slen == 0) {
306                         snprintf(workbuf, sizeof(workbuf),
307                             "link#%d", sdl->sdl_index);
308                 } else {
309                         switch (sdl->sdl_type) {
310                         case IFT_ETHER:
311                             {
312                                 int i;
313                                 u_char *lla = (u_char *)sdl->sdl_data +
314                                     sdl->sdl_nlen;
315
316                                 cplim = "";
317                                 for (i = 0; i < sdl->sdl_alen; i++, lla++) {
318                                         snprintf(cp, len, "%s%x", cplim, *lla);
319                                         len -= strlen(cp);
320                                         cp += strlen(cp);
321                                         if (len <= 0)
322                                                 break;  /* overflow */
323                                         cplim = ":";
324                                 }
325                                 cp = workbuf;
326                                 break;
327                             }
328                         default:
329                                 cp = link_ntoa(sdl);
330                                 break;
331                         }
332                 }
333                 break;
334             }
335
336         case AF_INET:
337             {
338                 struct sockaddr_in *in = (struct sockaddr_in *)sa;
339
340                 if (in->sin_addr.s_addr == 0) {
341                         /* cp points to workbuf */
342                         strncpy(cp, "default", len);
343                 } else
344                         cp = (flags & RTF_HOST) ? routename(sa) : netname(sa);
345                 break;
346             }
347
348 #ifdef INET6
349         case AF_INET6:
350             {
351                 struct sockaddr_in6 *in6 = (struct sockaddr_in6 *)sa;
352
353                 if (IN6_IS_ADDR_UNSPECIFIED(&in6->sin6_addr)) {
354                         /* cp points to workbuf */
355                         strncpy(cp, "default", len);
356                 } else {
357                         cp = ((flags & RTF_HOST) ? routename(sa)
358                                                  : netname(sa));
359                 }
360                 /* make sure numeric address is not truncated */
361                 if (strchr(cp, ':') != NULL &&
362                     (width < 0 || strlen(cp) > (size_t)width))
363                         width = strlen(cp);
364                 break;
365             }
366 #endif /* INET6 */
367
368         default:
369             {
370                 u_char *s = (u_char *)sa->sa_data, *slim;
371
372                 slim =  sa->sa_len + (u_char *) sa;
373                 cplim = cp + sizeof(workbuf) - 6;
374                 snprintf(cp, len, "(%d)", sa->sa_family);
375                 len -= strlen(cp);
376                 cp += strlen(cp);
377                 if (len <= 0) {
378                         cp = workbuf;
379                         break;          /* overflow */
380                 }
381                 while (s < slim && cp < cplim) {
382                         snprintf(cp, len, " %02x", *s++);
383                         len -= strlen(cp);
384                         cp += strlen(cp);
385                         if (len <= 0)
386                                 break;          /* overflow */
387                         if (s < slim) {
388                                 snprintf(cp, len, "%02x", *s++);
389                                 len -= strlen(cp);
390                                 cp += strlen(cp);
391                                 if (len <= 0)
392                                         break;          /* overflow */
393                         }
394                 }
395                 cp = workbuf;
396             }
397         }
398         if (width < 0) {
399                 count = printf("%s ", cp);
400         } else {
401                 if (nflag || wflag)
402                         count = printf("%-*s ", width, cp);
403                 else
404                         count = printf("%-*.*s ", width, width, cp);
405         }
406         return(count);
407 }
408
409 static void
410 p_flags(int f, const char *format)
411 {
412         char name[33], *flags;
413         const struct bits *p = bits;
414
415         for (flags = name; p->b_mask && flags < &name[sizeof(name-2)]; p++) {
416                 if (p->b_mask & f)
417                         *flags++ = p->b_val;
418         }
419         *flags = '\0';
420         printf(format, name);
421 }