1 /* $OpenBSD: route.c,v 1.98 2015/02/12 01:49:02 claudio Exp $ */ 2 /* $NetBSD: route.c,v 1.15 1996/05/07 02:55:06 thorpej Exp $ */ 3 4 /* 5 * Copyright (c) 1983, 1988, 1993 6 * The Regents of the University of California. All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. Neither the name of the University nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 */ 32 33 #include <sys/types.h> 34 #include <sys/protosw.h> 35 #include <sys/socket.h> 36 37 #include <net/if.h> 38 #include <net/if_var.h> 39 #include <net/if_dl.h> 40 #include <net/if_types.h> 41 #define _KERNEL 42 #include <net/route.h> 43 #include <netinet/ip_ipsp.h> 44 #undef _KERNEL 45 #include <netinet/in.h> 46 #include <arpa/inet.h> 47 48 #include <sys/sysctl.h> 49 50 #include <err.h> 51 #include <limits.h> 52 #include <netdb.h> 53 #include <stdio.h> 54 #include <stdlib.h> 55 #include <string.h> 56 #include <unistd.h> 57 58 #include "netstat.h" 59 60 /* alignment constraint for routing socket */ 61 #define ROUNDUP(a) \ 62 ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long)) 63 #define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len)) 64 65 struct radix_node_head ***rt_head; 66 struct radix_node_head ***rnt; 67 struct radix_node_head *rt_tables[AF_MAX+1]; /* provides enough space */ 68 u_int8_t af2rtafidx[AF_MAX+1]; 69 70 static union { 71 struct sockaddr u_sa; 72 u_int32_t u_data[64]; 73 int u_dummy; /* force word-alignment */ 74 } pt_u; 75 76 int do_rtent = 0; 77 struct rtentry rtentry; 78 struct radix_node rnode; 79 struct radix_mask rmask; 80 81 static struct sockaddr *kgetsa(struct sockaddr *); 82 static void p_tree(struct radix_node *); 83 static void p_rtnode(void); 84 static void p_rtflags(u_char); 85 static void p_krtentry(struct rtentry *); 86 87 /* 88 * Print routing tables. 89 */ 90 void 91 routepr(u_long rtree, u_long mtree, u_long af2idx, u_long rtbl_id_max, 92 u_int tableid) 93 { 94 struct radix_node_head *rnh, head; 95 int i, idxmax = 0; 96 u_int rtidxmax; 97 98 printf("Routing tables\n"); 99 100 if (rtree == 0 || af2idx == 0) { 101 printf("rt_tables: symbol not in namelist\n"); 102 return; 103 } 104 105 kread((u_long)rtree, &rt_head, sizeof(rt_head)); 106 kread((u_long)rtbl_id_max, &rtidxmax, sizeof(rtidxmax)); 107 kread((long)af2idx, &af2rtafidx, sizeof(af2rtafidx)); 108 109 for (i = 0; i <= AF_MAX; i++) { 110 if (af2rtafidx[i] > idxmax) 111 idxmax = af2rtafidx[i]; 112 } 113 114 if ((rnt = calloc(rtidxmax + 1, sizeof(struct radix_node_head **))) == 115 NULL) 116 err(1, NULL); 117 118 kread((u_long)rt_head, rnt, (rtidxmax + 1) * 119 sizeof(struct radix_node_head **)); 120 if (tableid > rtidxmax || rnt[tableid] == NULL) { 121 printf("Bad table %u\n", tableid); 122 return; 123 } 124 kread((u_long)rnt[tableid], rt_tables, (idxmax + 1) * sizeof(rnh)); 125 126 for (i = 0; i <= AF_MAX; i++) { 127 if (i == AF_UNSPEC) { 128 if (Aflag && (af == AF_UNSPEC || af == 0xff)) { 129 kread(mtree, &rnh, sizeof(rnh)); 130 kread((u_long)rnh, &head, sizeof(head)); 131 printf("Netmasks:\n"); 132 p_tree(head.rnh_treetop); 133 } 134 continue; 135 } 136 if (af2rtafidx[i] == 0) 137 /* no table for this AF */ 138 continue; 139 if ((rnh = rt_tables[af2rtafidx[i]]) == NULL) 140 continue; 141 kread((u_long)rnh, &head, sizeof(head)); 142 if (af == AF_UNSPEC || af == i) { 143 pr_family(i); 144 do_rtent = 1; 145 pr_rthdr(i, Aflag); 146 p_tree(head.rnh_treetop); 147 } 148 } 149 } 150 151 static struct sockaddr * 152 kgetsa(struct sockaddr *dst) 153 { 154 155 kread((u_long)dst, &pt_u.u_sa, sizeof(pt_u.u_sa)); 156 if (pt_u.u_sa.sa_len > sizeof (pt_u.u_sa)) 157 kread((u_long)dst, pt_u.u_data, pt_u.u_sa.sa_len); 158 return (&pt_u.u_sa); 159 } 160 161 static void 162 p_tree(struct radix_node *rn) 163 { 164 165 again: 166 kread((u_long)rn, &rnode, sizeof(rnode)); 167 if (rnode.rn_b < 0) { 168 if (Aflag) 169 printf("%-16p ", rn); 170 if (rnode.rn_flags & RNF_ROOT) { 171 if (Aflag) 172 printf("(root node)%s", 173 rnode.rn_dupedkey ? " =>\n" : "\n"); 174 } else if (do_rtent) { 175 kread((u_long)rn, &rtentry, sizeof(rtentry)); 176 p_krtentry(&rtentry); 177 if (Aflag) 178 p_rtnode(); 179 } else { 180 p_sockaddr(kgetsa((struct sockaddr *)rnode.rn_key), 181 0, 0, 44); 182 putchar('\n'); 183 } 184 if ((rn = rnode.rn_dupedkey)) 185 goto again; 186 } else { 187 if (Aflag && do_rtent) { 188 printf("%-16p ", rn); 189 p_rtnode(); 190 } 191 rn = rnode.rn_r; 192 p_tree(rnode.rn_l); 193 p_tree(rn); 194 } 195 } 196 197 static void 198 p_rtflags(u_char flags) 199 { 200 putchar('<'); 201 if (flags & RNF_NORMAL) 202 putchar('N'); 203 if (flags & RNF_ROOT) 204 putchar('R'); 205 if (flags & RNF_ACTIVE) 206 putchar('A'); 207 if (flags & ~(RNF_NORMAL | RNF_ROOT | RNF_ACTIVE)) 208 printf("/0x%02x", flags); 209 putchar('>'); 210 } 211 212 char nbuf[25]; 213 214 static void 215 p_rtnode(void) 216 { 217 struct radix_mask *rm = rnode.rn_mklist; 218 219 if (rnode.rn_b < 0) { 220 snprintf(nbuf, sizeof nbuf, " => %p", rnode.rn_dupedkey); 221 printf("\t (%p)%s", rnode.rn_p, rnode.rn_dupedkey ? nbuf : ""); 222 if (rnode.rn_mask) { 223 printf(" mask "); 224 p_sockaddr(kgetsa((struct sockaddr *)rnode.rn_mask), 225 0, 0, -1); 226 } else if (rm == NULL) { 227 putchar('\n'); 228 return; 229 } 230 } else { 231 snprintf(nbuf, sizeof nbuf, "(%d)", rnode.rn_b); 232 printf("%6.6s (%p) %16p : %16p", nbuf, 233 rnode.rn_p, rnode.rn_l, rnode.rn_r); 234 } 235 236 putchar(' '); 237 p_rtflags(rnode.rn_flags); 238 239 while (rm) { 240 kread((u_long)rm, &rmask, sizeof(rmask)); 241 snprintf(nbuf, sizeof nbuf, " %d refs, ", rmask.rm_refs); 242 printf("\n\tmk = %p {(%d),%s", rm, -1 - rmask.rm_b, 243 rmask.rm_refs ? nbuf : " "); 244 p_rtflags(rmask.rm_flags); 245 printf(", "); 246 if (rmask.rm_flags & RNF_NORMAL) { 247 struct radix_node rnode_aux; 248 249 printf("leaf = %p ", rmask.rm_leaf); 250 kread((u_long)rmask.rm_leaf, &rnode_aux, sizeof(rnode_aux)); 251 p_sockaddr(kgetsa((struct sockaddr *)rnode_aux.rn_mask), 252 0, 0, -1); 253 } else 254 p_sockaddr(kgetsa((struct sockaddr *)rmask.rm_mask), 255 0, 0, -1); 256 putchar('}'); 257 if ((rm = rmask.rm_mklist)) 258 printf(" ->"); 259 } 260 putchar('\n'); 261 } 262 263 static void 264 p_krtentry(struct rtentry *rt) 265 { 266 static struct ifnet ifnet, *lastif; 267 struct sockaddr_storage sock1, sock2; 268 struct sockaddr *sa = (struct sockaddr *)&sock1; 269 struct sockaddr *mask = (struct sockaddr *)&sock2; 270 271 bcopy(kgetsa(rt_key(rt)), sa, sizeof(struct sockaddr)); 272 if (sa->sa_len > sizeof(struct sockaddr)) 273 bcopy(kgetsa(rt_key(rt)), sa, sa->sa_len); 274 275 if (sa->sa_family == PF_KEY) { 276 /* Ignore PF_KEY entries */ 277 return; 278 } 279 280 if (rt_mask(rt)) { 281 bcopy(kgetsa(rt_mask(rt)), mask, sizeof(struct sockaddr)); 282 if (sa->sa_len > sizeof(struct sockaddr)) 283 bcopy(kgetsa(rt_mask(rt)), mask, sa->sa_len); 284 } else 285 mask = 0; 286 287 p_addr(sa, mask, rt->rt_flags); 288 p_gwaddr(kgetsa(rt->rt_gateway), sa->sa_family); 289 p_flags(rt->rt_flags, "%-6.6s "); 290 printf("%5u %8lld ", rt->rt_refcnt, rt->rt_use); 291 if (rt->rt_rmx.rmx_mtu) 292 printf("%5u ", rt->rt_rmx.rmx_mtu); 293 else 294 printf("%5s ", "-"); 295 putchar((rt->rt_rmx.rmx_locks & RTV_MTU) ? 'L' : ' '); 296 printf(" %2d", rt->rt_priority); 297 298 if (rt->rt_ifp) { 299 if (rt->rt_ifp != lastif) { 300 kread((u_long)rt->rt_ifp, &ifnet, sizeof(ifnet)); 301 lastif = rt->rt_ifp; 302 } 303 printf(" %.16s%s", ifnet.if_xname, 304 rt->rt_nodes[0].rn_dupedkey ? " =>" : ""); 305 } 306 putchar('\n'); 307 if (vflag) 308 printf("\texpire %10lld%c\n", 309 (long long)rt->rt_rmx.rmx_expire, 310 (rt->rt_rmx.rmx_locks & RTV_EXPIRE) ? 'L' : ' '); 311 } 312 313 /* 314 * Print routing statistics 315 */ 316 void 317 rt_stats(void) 318 { 319 struct rtstat rtstat; 320 int mib[6]; 321 size_t size; 322 323 mib[0] = CTL_NET; 324 mib[1] = PF_ROUTE; 325 mib[2] = 0; 326 mib[3] = 0; 327 mib[4] = NET_RT_STATS; 328 mib[5] = 0; 329 size = sizeof (rtstat); 330 331 if (sysctl(mib, 6, &rtstat, &size, NULL, 0) < 0) { 332 perror("sysctl of routing table statistics"); 333 exit(1); 334 } 335 336 printf("routing:\n"); 337 printf("\t%u bad routing redirect%s\n", 338 rtstat.rts_badredirect, plural(rtstat.rts_badredirect)); 339 printf("\t%u dynamically created route%s\n", 340 rtstat.rts_dynamic, plural(rtstat.rts_dynamic)); 341 printf("\t%u new gateway%s due to redirects\n", 342 rtstat.rts_newgateway, plural(rtstat.rts_newgateway)); 343 printf("\t%u destination%s found unreachable\n", 344 rtstat.rts_unreach, plural(rtstat.rts_unreach)); 345 printf("\t%u use%s of a wildcard route\n", 346 rtstat.rts_wildcard, plural(rtstat.rts_wildcard)); 347 } 348