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