1 /* $NetBSD: route.c,v 1.79 2012/03/20 20:34:58 matt 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: route.c,v 1.79 2012/03/20 20:34:58 matt Exp $"); 38 #endif 39 #endif /* not lint */ 40 41 #include <stdbool.h> 42 #include <sys/param.h> 43 #include <sys/protosw.h> 44 #include <sys/socket.h> 45 #include <sys/mbuf.h> 46 #include <sys/un.h> 47 48 #include <net/if.h> 49 #include <net/if_dl.h> 50 #include <net/if_types.h> 51 #include <net/route.h> 52 #include <netinet/in.h> 53 #include <netatalk/at.h> 54 #include <netiso/iso.h> 55 #include <netmpls/mpls.h> 56 57 #include <sys/sysctl.h> 58 59 #include <arpa/inet.h> 60 61 #include <err.h> 62 #include <kvm.h> 63 #include <netdb.h> 64 #include <stdio.h> 65 #include <stdlib.h> 66 #include <string.h> 67 #include <unistd.h> 68 69 #include "netstat.h" 70 71 #define kget(p, d) (kread((u_long)(p), (char *)&(d), sizeof (d))) 72 73 /* 74 * XXX we put all of the sockaddr types in here to force the alignment 75 * to be correct. 76 */ 77 static union sockaddr_union { 78 struct sockaddr u_sa; 79 struct sockaddr_in u_in; 80 struct sockaddr_un u_un; 81 struct sockaddr_iso u_iso; 82 struct sockaddr_at u_at; 83 struct sockaddr_dl u_dl; 84 u_short u_data[128]; 85 int u_dummy; /* force word-alignment */ 86 } pt_u; 87 88 int do_rtent = 0; 89 struct rtentry rtentry; 90 struct radix_node rnode; 91 struct radix_mask rmask; 92 93 static struct sockaddr *kgetsa(const struct sockaddr *); 94 static void p_tree(struct radix_node *); 95 static void p_rtnode(void); 96 static void p_krtentry(struct rtentry *); 97 98 /* 99 * Print routing tables. 100 */ 101 void 102 routepr(u_long rtree) 103 { 104 struct radix_node_head *rnh, head; 105 struct radix_node_head *rt_nodes[AF_MAX+1]; 106 int i; 107 108 printf("Routing tables\n"); 109 110 if (rtree == 0) { 111 printf("rt_tables: symbol not in namelist\n"); 112 return; 113 } 114 115 kget(rtree, rt_nodes); 116 for (i = 0; i <= AF_MAX; i++) { 117 if ((rnh = rt_nodes[i]) == 0) 118 continue; 119 kget(rnh, head); 120 if (i == AF_UNSPEC) { 121 if (Aflag && (af == 0 || af == 0xff)) { 122 printf("Netmasks:\n"); 123 p_tree(head.rnh_treetop); 124 } 125 } else if (af == AF_UNSPEC || af == i) { 126 pr_family(i); 127 do_rtent = 1; 128 pr_rthdr(i, Aflag); 129 p_tree(head.rnh_treetop); 130 } 131 } 132 } 133 134 static struct sockaddr * 135 kgetsa(const struct sockaddr *dst) 136 { 137 138 kget(dst, pt_u.u_sa); 139 if (pt_u.u_sa.sa_len > sizeof (pt_u.u_sa)) 140 kread((u_long)dst, (char *)pt_u.u_data, pt_u.u_sa.sa_len); 141 return (&pt_u.u_sa); 142 } 143 144 static void 145 p_tree(struct radix_node *rn) 146 { 147 148 again: 149 kget(rn, rnode); 150 if (rnode.rn_b < 0) { 151 if (Aflag) 152 printf("%-8.8lx ", (u_long) rn); 153 if (rnode.rn_flags & RNF_ROOT) { 154 if (Aflag) 155 printf("(root node)%s", 156 rnode.rn_dupedkey ? " =>\n" : "\n"); 157 } else if (do_rtent) { 158 kget(rn, rtentry); 159 p_krtentry(&rtentry); 160 if (Aflag) 161 p_rtnode(); 162 } else { 163 p_sockaddr(kgetsa((const struct sockaddr *)rnode.rn_key), 164 NULL, 0, 44); 165 putchar('\n'); 166 } 167 if ((rn = rnode.rn_dupedkey) != NULL) 168 goto again; 169 } else { 170 if (Aflag && do_rtent) { 171 printf("%-8.8lx ", (u_long) rn); 172 p_rtnode(); 173 } 174 rn = rnode.rn_r; 175 p_tree(rnode.rn_l); 176 p_tree(rn); 177 } 178 } 179 180 static void 181 p_rtnode(void) 182 { 183 struct radix_mask *rm = rnode.rn_mklist; 184 char nbuf[20]; 185 186 if (rnode.rn_b < 0) { 187 if (rnode.rn_mask) { 188 printf("\t mask "); 189 p_sockaddr(kgetsa((const struct sockaddr *)rnode.rn_mask), 190 NULL, 0, -1); 191 } else if (rm == 0) 192 return; 193 } else { 194 (void)snprintf(nbuf, sizeof nbuf, "(%d)", rnode.rn_b); 195 printf("%6.6s %8.8lx : %8.8lx", nbuf, (u_long) rnode.rn_l, 196 (u_long) rnode.rn_r); 197 } 198 while (rm) { 199 kget(rm, rmask); 200 (void)snprintf(nbuf, sizeof nbuf, " %d refs, ", rmask.rm_refs); 201 printf(" mk = %8.8lx {(%d),%s", (u_long) rm, 202 -1 - rmask.rm_b, rmask.rm_refs ? nbuf : " "); 203 if (rmask.rm_flags & RNF_NORMAL) { 204 struct radix_node rnode_aux; 205 printf(" <normal>, "); 206 kget(rmask.rm_leaf, rnode_aux); 207 p_sockaddr(kgetsa((const struct sockaddr *)rnode_aux.rn_mask), 208 NULL, 0, -1); 209 } else 210 p_sockaddr(kgetsa((const struct sockaddr *)rmask.rm_mask), 211 NULL, 0, -1); 212 putchar('}'); 213 if ((rm = rmask.rm_mklist) != NULL) 214 printf(" ->"); 215 } 216 putchar('\n'); 217 } 218 219 static struct sockaddr *sockcopy(struct sockaddr *, union sockaddr_union *); 220 221 /* 222 * copy a sockaddr into an allocated region, allocate at least sockaddr 223 * bytes and zero unused 224 */ 225 static struct sockaddr * 226 sockcopy(struct sockaddr *sp, union sockaddr_union *dp) 227 { 228 int len; 229 230 if (sp == 0 || sp->sa_len == 0) 231 (void)memset(dp, 0, sizeof (*sp)); 232 else { 233 len = (sp->sa_len >= sizeof (*sp)) ? sp->sa_len : sizeof (*sp); 234 (void)memcpy(dp, sp, len); 235 } 236 return ((struct sockaddr *)dp); 237 } 238 239 static void 240 p_krtentry(struct rtentry *rt) 241 { 242 static struct ifnet ifnet, *lastif; 243 union sockaddr_union addr_un, mask_un; 244 struct sockaddr *addr, *mask; 245 246 if (Lflag && (rt->rt_flags & RTF_LLINFO)) 247 return; 248 249 memset(&addr_un, 0, sizeof(addr_un)); 250 memset(&mask_un, 0, sizeof(mask_un)); 251 addr = sockcopy(kgetsa(rt_getkey(rt)), &addr_un); 252 if (rt_mask(rt)) 253 mask = sockcopy(kgetsa(rt_mask(rt)), &mask_un); 254 else 255 mask = sockcopy(NULL, &mask_un); 256 p_addr(addr, mask, rt->rt_flags); 257 p_gwaddr(kgetsa(rt->rt_gateway), kgetsa(rt->rt_gateway)->sa_family); 258 p_flags(rt->rt_flags, "%-6.6s "); 259 printf("%6d %8"PRIu64" ", rt->rt_refcnt, rt->rt_use); 260 if (rt->rt_rmx.rmx_mtu) 261 printf("%6"PRIu64, rt->rt_rmx.rmx_mtu); 262 else 263 printf("%6s", "-"); 264 putchar((rt->rt_rmx.rmx_locks & RTV_MTU) ? 'L' : ' '); 265 if (tagflag == 1) { 266 if (rt->rt_tag != NULL) { 267 const struct sockaddr *tagsa = kgetsa(rt->rt_tag); 268 char *tagstr; 269 270 if (tagsa->sa_family == AF_MPLS) { 271 tagstr = mpls_ntoa(tagsa); 272 if (strlen(tagstr) < 7) 273 printf("%7s", tagstr); 274 else 275 printf("%s", tagstr); 276 } 277 else 278 printf("%7s", "-"); 279 } else 280 printf("%7s", "-"); 281 } 282 if (rt->rt_ifp) { 283 if (rt->rt_ifp != lastif) { 284 kget(rt->rt_ifp, ifnet); 285 lastif = rt->rt_ifp; 286 } 287 printf(" %.16s%s", ifnet.if_xname, 288 rt->rt_nodes[0].rn_dupedkey ? " =>" : ""); 289 } 290 putchar('\n'); 291 if (vflag) { 292 printf("\texpire %10"PRId64"%c recvpipe %10"PRIu64"%c " 293 "sendpipe %10"PRIu64"%c\n", 294 (int64_t)rt->rt_rmx.rmx_expire, 295 (rt->rt_rmx.rmx_locks & RTV_EXPIRE) ? 'L' : ' ', 296 rt->rt_rmx.rmx_recvpipe, 297 (rt->rt_rmx.rmx_locks & RTV_RPIPE) ? 'L' : ' ', 298 rt->rt_rmx.rmx_sendpipe, 299 (rt->rt_rmx.rmx_locks & RTV_SPIPE) ? 'L' : ' '); 300 printf("\tssthresh %10"PRIu64"%c rtt %10"PRIu64"%c " 301 "rttvar %10"PRIu64"%c\n", 302 rt->rt_rmx.rmx_ssthresh, 303 (rt->rt_rmx.rmx_locks & RTV_SSTHRESH) ? 'L' : ' ', 304 rt->rt_rmx.rmx_rtt, 305 (rt->rt_rmx.rmx_locks & RTV_RTT) ? 'L' : ' ', 306 rt->rt_rmx.rmx_rttvar, 307 (rt->rt_rmx.rmx_locks & RTV_RTTVAR) ? 'L' : ' '); 308 printf("\thopcount %10"PRIu64"%c\n", 309 rt->rt_rmx.rmx_hopcount, 310 (rt->rt_rmx.rmx_locks & RTV_HOPCOUNT) ? 'L' : ' '); 311 } 312 } 313 314 /* 315 * Print routing statistics 316 */ 317 void 318 rt_stats(u_long off) 319 { 320 struct rtstat rtstats; 321 322 if (use_sysctl) { 323 size_t rtsize = sizeof(rtstats); 324 325 if (sysctlbyname("net.route.stats", &rtstats, &rtsize, 326 NULL, 0) == -1) 327 err(1, "rt_stats: sysctl"); 328 } else if (off == 0) { 329 printf("rtstat: symbol not in namelist\n"); 330 return; 331 } else 332 kread(off, (char *)&rtstats, sizeof(rtstats)); 333 334 printf("routing:\n"); 335 printf("\t%llu bad routing redirect%s\n", 336 (unsigned long long)rtstats.rts_badredirect, 337 plural(rtstats.rts_badredirect)); 338 printf("\t%llu dynamically created route%s\n", 339 (unsigned long long)rtstats.rts_dynamic, 340 plural(rtstats.rts_dynamic)); 341 printf("\t%llu new gateway%s due to redirects\n", 342 (unsigned long long)rtstats.rts_newgateway, 343 plural(rtstats.rts_newgateway)); 344 printf("\t%llu destination%s found unreachable\n", 345 (unsigned long long)rtstats.rts_unreach, 346 plural(rtstats.rts_unreach)); 347 printf("\t%llu use%s of a wildcard route\n", 348 (unsigned long long)rtstats.rts_wildcard, 349 plural(rtstats.rts_wildcard)); 350 } 351 352 void 353 upHex(char *p0) 354 { 355 char *p = p0; 356 357 for (; *p; p++) 358 switch (*p) { 359 case 'a': 360 case 'b': 361 case 'c': 362 case 'd': 363 case 'e': 364 case 'f': 365 *p += ('A' - 'a'); 366 } 367 } 368