1 /* $NetBSD: show.c,v 1.45 2013/03/01 18:25:17 joerg 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.45 2013/03/01 18:25:17 joerg 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 int 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 { RTF_ANNOUNCE, 'p' }, 94 { 0, '\0' } 95 }; 96 97 static void pr_rthdr(int); 98 static void p_rtentry(struct rt_msghdr *); 99 static void pr_family(int); 100 static void p_sockaddr(struct sockaddr *, struct sockaddr *, int, int ); 101 static void p_flags(int); 102 103 void 104 parse_show_opts(int argc, char * const *argv, int *afp, int *flagsp, 105 const char **afnamep, bool nolink) 106 { 107 const char *afname = "unspec"; 108 int af, flags; 109 110 flags = 0; 111 af = AF_UNSPEC; 112 for (; argc >= 2; argc--) { 113 if (*argv[argc - 1] != '-') 114 goto bad; 115 switch (keyword(argv[argc - 1] + 1)) { 116 case K_HOST: 117 flags |= RTF_HOST; 118 break; 119 case K_LLINFO: 120 flags |= RTF_LLINFO; 121 break; 122 case K_INET: 123 af = AF_INET; 124 afname = argv[argc - 1] + 1; 125 break; 126 #ifdef INET6 127 case K_INET6: 128 af = AF_INET6; 129 afname = argv[argc - 1] + 1; 130 break; 131 #endif 132 #ifndef SMALL 133 case K_ATALK: 134 af = AF_APPLETALK; 135 afname = argv[argc - 1] + 1; 136 break; 137 case K_MPLS: 138 af = AF_MPLS; 139 afname = argv[argc - 1] + 1; 140 break; 141 #endif /* SMALL */ 142 case K_LINK: 143 if (nolink) 144 goto bad; 145 af = AF_LINK; 146 afname = argv[argc - 1] + 1; 147 break; 148 default: 149 goto bad; 150 } 151 } 152 switch (argc) { 153 case 1: 154 case 0: 155 break; 156 default: 157 bad: 158 usage(argv[argc - 1]); 159 } 160 if (afnamep != NULL) 161 *afnamep = afname; 162 *afp = af; 163 *flagsp = flags; 164 } 165 166 /* 167 * Print routing tables. 168 */ 169 void 170 show(int argc, char *const *argv) 171 { 172 size_t needed; 173 int af, flags, mib[6]; 174 char *buf, *next, *lim; 175 struct rt_msghdr *rtm; 176 struct sockaddr *sa; 177 178 parse_show_opts(argc, argv, &af, &flags, NULL, true); 179 mib[0] = CTL_NET; 180 mib[1] = PF_ROUTE; 181 mib[2] = 0; 182 mib[3] = 0; 183 mib[4] = NET_RT_DUMP; 184 mib[5] = 0; 185 if (prog_sysctl(mib, 6, NULL, &needed, NULL, 0) < 0) 186 err(EXIT_FAILURE, "route-sysctl-estimate"); 187 buf = lim = NULL; 188 if (needed) { 189 if ((buf = malloc(needed)) == 0) 190 err(EXIT_FAILURE, "malloc"); 191 if (prog_sysctl(mib, 6, buf, &needed, NULL, 0) < 0) 192 err(EXIT_FAILURE, "sysctl of routing table"); 193 lim = buf + needed; 194 } 195 196 printf("Routing table%s\n", (af == AF_UNSPEC)? "s" : ""); 197 198 if (needed) { 199 for (next = buf; next < lim; next += rtm->rtm_msglen) { 200 rtm = (struct rt_msghdr *)next; 201 sa = (struct sockaddr *)(rtm + 1); 202 if ((rtm->rtm_flags & flags) != flags) 203 continue; 204 if (af == AF_UNSPEC || af == sa->sa_family) 205 p_rtentry(rtm); 206 } 207 free(buf); 208 } 209 } 210 211 212 /* column widths; each followed by one space */ 213 #ifndef INET6 214 #define WID_DST(af) 18 /* width of destination column */ 215 #define WID_GW(af) 18 /* width of gateway column */ 216 #else 217 /* width of destination/gateway column */ 218 #if 1 219 /* strlen("fe80::aaaa:bbbb:cccc:dddd@gif0") == 30, strlen("/128") == 4 */ 220 #define WID_DST(af) ((af) == AF_INET6 ? (nflag ? 34 : 18) : 18) 221 #define WID_GW(af) ((af) == AF_INET6 ? (nflag ? 30 : 18) : 18) 222 #else 223 /* strlen("fe80::aaaa:bbbb:cccc:dddd") == 25, strlen("/128") == 4 */ 224 #define WID_DST(af) ((af) == AF_INET6 ? (nflag ? 29 : 18) : 18) 225 #define WID_GW(af) ((af) == AF_INET6 ? (nflag ? 25 : 18) : 18) 226 #endif 227 #endif /* INET6 */ 228 229 /* 230 * Print header for routing table columns. 231 */ 232 static void 233 pr_rthdr(int af) 234 { 235 236 printf("%-*.*s %-*.*s %-6.6s\n", 237 WID_DST(af), WID_DST(af), "Destination", 238 WID_GW(af), WID_GW(af), "Gateway", 239 "Flags"); 240 } 241 242 243 /* 244 * Print a routing table entry. 245 */ 246 static void 247 p_rtentry(struct rt_msghdr *rtm) 248 { 249 struct sockaddr *sa = (struct sockaddr *)(rtm + 1); 250 #ifdef notdef 251 static int masks_done, banner_printed; 252 #endif 253 static int old_af; 254 int af = 0, interesting = RTF_UP | RTF_GATEWAY | RTF_HOST | 255 RTF_REJECT | RTF_LLINFO; 256 257 #ifdef notdef 258 /* for the moment, netmasks are skipped over */ 259 if (!banner_printed) { 260 printf("Netmasks:\n"); 261 banner_printed = 1; 262 } 263 if (masks_done == 0) { 264 if (rtm->rtm_addrs != RTA_DST ) { 265 masks_done = 1; 266 af = sa->sa_family; 267 } 268 } else 269 #endif 270 af = sa->sa_family; 271 if (old_af != af) { 272 old_af = af; 273 pr_family(af); 274 pr_rthdr(af); 275 } 276 if (rtm->rtm_addrs == RTA_DST) 277 p_sockaddr(sa, NULL, 0, WID_DST(af) + 1 + WID_GW(af) + 1); 278 else { 279 struct sockaddr *nm; 280 281 if ((rtm->rtm_addrs & RTA_NETMASK) == 0) 282 nm = NULL; 283 else { 284 /* skip to gateway */ 285 nm = (struct sockaddr *) 286 (RT_ROUNDUP(sa->sa_len) + (char *)sa); 287 /* skip over gateway to netmask */ 288 nm = (struct sockaddr *) 289 (RT_ROUNDUP(nm->sa_len) + (char *)nm); 290 } 291 292 p_sockaddr(sa, nm, rtm->rtm_flags, WID_DST(af)); 293 sa = (struct sockaddr *)(RT_ROUNDUP(sa->sa_len) + (char *)sa); 294 p_sockaddr(sa, NULL, 0, WID_GW(af)); 295 } 296 p_flags(rtm->rtm_flags & interesting); 297 putchar('\n'); 298 } 299 300 301 /* 302 * Print address family header before a section of the routing table. 303 */ 304 static void 305 pr_family(int af) 306 { 307 const char *afname; 308 309 switch (af) { 310 case AF_INET: 311 afname = "Internet"; 312 break; 313 #ifdef INET6 314 case AF_INET6: 315 afname = "Internet6"; 316 break; 317 #endif /* INET6 */ 318 #ifndef SMALL 319 case AF_MPLS: 320 afname = "MPLS"; 321 break; 322 #endif /* SMALL */ 323 case AF_APPLETALK: 324 afname = "AppleTalk"; 325 break; 326 default: 327 afname = NULL; 328 break; 329 } 330 if (afname) 331 printf("\n%s:\n", afname); 332 else 333 printf("\nProtocol Family %d:\n", af); 334 } 335 336 337 static void 338 p_sockaddr(struct sockaddr *sa, struct sockaddr *nm, int flags, int width) 339 { 340 char workbuf[128]; 341 const char *cp; 342 343 switch(sa->sa_family) { 344 345 case AF_LINK: 346 if (getnameinfo(sa, sa->sa_len, workbuf, sizeof(workbuf), 347 NULL, 0, NI_NUMERICHOST) != 0) 348 strlcpy(workbuf, "invalid", sizeof(workbuf)); 349 cp = workbuf; 350 break; 351 352 case AF_INET: 353 cp = routename(sa, nm, flags); 354 break; 355 356 #ifdef INET6 357 case AF_INET6: 358 cp = routename(sa, nm, flags); 359 /* make sure numeric address is not truncated */ 360 if (strchr(cp, ':') != NULL && (int)strlen(cp) > width) 361 width = strlen(cp); 362 break; 363 #endif /* INET6 */ 364 365 #ifndef SMALL 366 case AF_MPLS: 367 { 368 struct sockaddr_mpls *smpls = (struct sockaddr_mpls *)sa; 369 union mpls_shim ms; 370 371 ms.s_addr = ntohl(smpls->smpls_addr.s_addr); 372 373 snprintf(workbuf, sizeof(workbuf), "%u", 374 ms.shim.label); 375 cp = workbuf; 376 } 377 break; 378 case AF_APPLETALK: 379 if (getnameinfo(sa, sa->sa_len, workbuf, sizeof(workbuf), 380 NULL, 0, NI_NUMERICHOST) != 0) 381 strlcpy(workbuf, "invalid", sizeof(workbuf)); 382 cp = workbuf; 383 break; 384 385 #endif /* SMALL */ 386 387 default: 388 { 389 u_char *s = (u_char *)sa->sa_data, *slim; 390 char *wp = workbuf, *wplim; 391 392 slim = sa->sa_len + (u_char *)sa; 393 wplim = wp + sizeof(workbuf) - 6; 394 wp += snprintf(wp, wplim - wp, "(%d)", sa->sa_family); 395 while (s < slim && wp < wplim) { 396 wp += snprintf(wp, wplim - wp, " %02x", *s++); 397 if (s < slim) 398 wp += snprintf(wp, wplim - wp, "%02x", *s++); 399 } 400 cp = workbuf; 401 } 402 } 403 if (width < 0 ) 404 printf("%s ", cp); 405 else { 406 if (nflag) 407 printf("%-*s ", width, cp); 408 else 409 printf("%-*.*s ", width, width, cp); 410 } 411 } 412 413 static void 414 p_flags(int f) 415 { 416 char name[33], *flags; 417 const struct bits *p = bits; 418 419 for (flags = name; p->b_mask; p++) 420 if (p->b_mask & f) 421 *flags++ = p->b_val; 422 else if (Sflag) 423 *flags++ = ' '; 424 *flags = '\0'; 425 printf("%-6.6s ", name); 426 } 427 428