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