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.5 2005/03/16 07:30:33 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 } 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 (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0) { 146 perror("route-sysctl-estimate"); 147 exit(1); 148 } 149 if (needed > 0) { 150 if ((buf = malloc(needed)) == NULL) { 151 printf("out of space\n"); 152 exit(1); 153 } 154 if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0) { 155 perror("sysctl of routing table"); 156 exit(1); 157 } 158 lim = buf + needed; 159 } 160 161 printf("Routing tables\n"); 162 163 if (buf != NULL) { 164 for (next = buf; next < lim; next += rtm->rtm_msglen) { 165 rtm = (struct rt_msghdr *)next; 166 sa = (struct sockaddr *)(rtm + 1); 167 if (af != 0 && sa->sa_family != af) 168 continue; 169 p_rtentry(rtm); 170 } 171 free(buf); 172 } 173 } 174 175 /* column widths; each followed by one space */ 176 #define WID_DST (nflag ? 20 : 32) /* destination column width */ 177 #define WID_GW (nflag ? 20 : 32) /* gateway column width */ 178 179 /* 180 * Print header for routing table columns. 181 */ 182 static void 183 pr_rthdr(void) 184 { 185 printf("%-*.*s %-*.*s %-6.6s\n", 186 WID_DST, WID_DST, "Destination", 187 WID_GW, WID_GW, "Gateway", 188 "Flags"); 189 } 190 191 /* 192 * Print a routing table entry. 193 */ 194 static void 195 p_rtentry(struct rt_msghdr *rtm) 196 { 197 struct sockaddr *sa = (struct sockaddr *)(rtm + 1); 198 #ifdef notdef 199 static int masks_done, banner_printed; 200 #endif 201 static int old_af; 202 int af = 0, interesting = RTF_UP | RTF_GATEWAY | RTF_HOST; 203 int width; 204 205 #ifdef notdef 206 /* for the moment, netmasks are skipped over */ 207 if (!banner_printed) { 208 printf("Netmasks:\n"); 209 banner_printed = 1; 210 } 211 if (masks_done == 0) { 212 if (rtm->rtm_addrs != RTA_DST) { 213 masks_done = 1; 214 af = sa->sa_family; 215 } 216 } else 217 #endif 218 af = sa->sa_family; 219 if (old_af != af) { 220 old_af = af; 221 pr_family(af); 222 pr_rthdr(); 223 } 224 225 /* 226 * Print the information. If wflag is set p_sockaddr() can return 227 * a wider width then specified and we try to fit the second 228 * address in any remaining space so the flags still lines up. 229 */ 230 if (rtm->rtm_addrs == RTA_DST) { 231 p_sockaddr(sa, 0, WID_DST + WID_GW + 2); 232 } else { 233 width = p_sockaddr(sa, rtm->rtm_flags, WID_DST); 234 sa = (struct sockaddr *)(ROUNDUP(sa->sa_len) + (char *)sa); 235 p_sockaddr(sa, 0, WID_GW + WID_DST - width); 236 } 237 p_flags(rtm->rtm_flags & interesting, "%-6.6s "); 238 putchar('\n'); 239 } 240 241 /* 242 * Print address family header before a section of the routing table. 243 */ 244 static void 245 pr_family(int af) 246 { 247 const char *afname; 248 249 switch (af) { 250 case AF_INET: 251 afname = "Internet"; 252 break; 253 #ifdef INET6 254 case AF_INET6: 255 afname = "Internet6"; 256 break; 257 #endif /* INET6 */ 258 case AF_NS: 259 afname = "XNS"; 260 break; 261 case AF_IPX: 262 afname = "IPX"; 263 break; 264 case AF_ISO: 265 afname = "ISO"; 266 break; 267 case AF_CCITT: 268 afname = "X.25"; 269 break; 270 case AF_APPLETALK: 271 afname = "AppleTalk"; 272 break; 273 default: 274 afname = NULL; 275 break; 276 } 277 if (afname != NULL) 278 printf("\n%s:\n", afname); 279 else 280 printf("\nProtocol Family %d:\n", af); 281 } 282 283 static int 284 p_sockaddr(struct sockaddr *sa, int flags, int width) 285 { 286 char workbuf[128]; 287 char *cp = workbuf; 288 const char *cplim; 289 int len = sizeof(workbuf); 290 int count; 291 292 switch(sa->sa_family) { 293 294 case AF_LINK: 295 { 296 struct sockaddr_dl *sdl = (struct sockaddr_dl *)sa; 297 298 if (sdl->sdl_nlen == 0 && sdl->sdl_alen == 0 && 299 sdl->sdl_slen == 0) { 300 snprintf(workbuf, sizeof(workbuf), 301 "link#%d", sdl->sdl_index); 302 } else { 303 switch (sdl->sdl_type) { 304 case IFT_ETHER: 305 { 306 int i; 307 u_char *lla = (u_char *)sdl->sdl_data + 308 sdl->sdl_nlen; 309 310 cplim = ""; 311 for (i = 0; i < sdl->sdl_alen; i++, lla++) { 312 snprintf(cp, len, "%s%x", cplim, *lla); 313 len -= strlen(cp); 314 cp += strlen(cp); 315 if (len <= 0) 316 break; /* overflow */ 317 cplim = ":"; 318 } 319 cp = workbuf; 320 break; 321 } 322 default: 323 cp = link_ntoa(sdl); 324 break; 325 } 326 } 327 break; 328 } 329 330 case AF_INET: 331 { 332 struct sockaddr_in *in = (struct sockaddr_in *)sa; 333 334 if (in->sin_addr.s_addr == 0) { 335 /* cp points to workbuf */ 336 strncpy(cp, "default", len); 337 } else 338 cp = (flags & RTF_HOST) ? routename(sa) : netname(sa); 339 break; 340 } 341 342 #ifdef INET6 343 case AF_INET6: 344 { 345 struct sockaddr_in6 *in6 = (struct sockaddr_in6 *)sa; 346 347 if (IN6_IS_ADDR_UNSPECIFIED(&in6->sin6_addr)) { 348 /* cp points to workbuf */ 349 strncpy(cp, "default", len); 350 } else { 351 cp = ((flags & RTF_HOST) ? routename(sa) 352 : netname(sa)); 353 } 354 /* make sure numeric address is not truncated */ 355 if (strchr(cp, ':') != NULL && 356 (width < 0 || strlen(cp) > (size_t)width)) 357 width = strlen(cp); 358 break; 359 } 360 #endif /* INET6 */ 361 362 case AF_NS: 363 cp = ns_print((struct sockaddr_ns *)sa); 364 break; 365 366 default: 367 { 368 u_char *s = (u_char *)sa->sa_data, *slim; 369 370 slim = sa->sa_len + (u_char *) sa; 371 cplim = cp + sizeof(workbuf) - 6; 372 snprintf(cp, len, "(%d)", sa->sa_family); 373 len -= strlen(cp); 374 cp += strlen(cp); 375 if (len <= 0) { 376 cp = workbuf; 377 break; /* overflow */ 378 } 379 while (s < slim && cp < cplim) { 380 snprintf(cp, len, " %02x", *s++); 381 len -= strlen(cp); 382 cp += strlen(cp); 383 if (len <= 0) 384 break; /* overflow */ 385 if (s < slim) { 386 snprintf(cp, len, "%02x", *s++); 387 len -= strlen(cp); 388 cp += strlen(cp); 389 if (len <= 0) 390 break; /* overflow */ 391 } 392 } 393 cp = workbuf; 394 } 395 } 396 if (width < 0) { 397 count = printf("%s ", cp); 398 } else { 399 if (nflag || wflag) 400 count = printf("%-*s ", width, cp); 401 else 402 count = printf("%-*.*s ", width, width, cp); 403 } 404 return(count); 405 } 406 407 static void 408 p_flags(int f, const char *format) 409 { 410 char name[33], *flags; 411 const struct bits *p = bits; 412 413 for (flags = name; p->b_mask && flags < &name[sizeof(name-2)]; p++) { 414 if (p->b_mask & f) 415 *flags++ = p->b_val; 416 } 417 *flags = '\0'; 418 printf(format, name); 419 } 420