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