1 /* $OpenBSD: route.c,v 1.101 2016/09/15 01:01:07 dlg 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/select.h> 36 #include <sys/socket.h> 37 38 #include <net/if.h> 39 #include <net/if_dl.h> 40 #include <net/if_types.h> 41 #include <net/route.h> 42 #include <netinet/ip_ipsp.h> 43 #include <netinet/in.h> 44 #include <arpa/inet.h> 45 46 #include <sys/sysctl.h> 47 48 #include <err.h> 49 #include <limits.h> 50 #include <netdb.h> 51 #include <stdio.h> 52 #include <stdlib.h> 53 #include <string.h> 54 #include <unistd.h> 55 56 #include "netstat.h" 57 58 /* alignment constraint for routing socket */ 59 #define ROUNDUP(a) \ 60 ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long)) 61 #define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len)) 62 63 struct radix_node_head ***rt_head; 64 struct radix_node_head ***rnt; 65 struct radix_node_head *rt_tables[AF_MAX+1]; /* provides enough space */ 66 u_int8_t af2rtafidx[AF_MAX+1]; 67 68 static union { 69 struct sockaddr u_sa; 70 u_int32_t u_data[64]; 71 int u_dummy; /* force word-alignment */ 72 } pt_u; 73 74 int do_rtent = 0; 75 struct rtentry rtentry; 76 struct radix_node rnode; 77 struct radix_mask rmask; 78 79 static struct sockaddr *kgetsa(struct sockaddr *); 80 static void p_tree(struct radix_node *); 81 static void p_rtnode(void); 82 static void p_rtflags(u_char); 83 static void p_krtentry(struct rtentry *); 84 85 /* 86 * Print routing tables. 87 */ 88 void 89 routepr(u_long rtree, u_long mtree, u_long af2idx, u_long rtbl_id_max, 90 u_int tableid) 91 { 92 struct radix_node_head *rnh, head; 93 int i, idxmax = 0; 94 u_int rtidxmax; 95 96 printf("Routing tables\n"); 97 98 if (rtree == 0 || af2idx == 0) { 99 printf("rt_tables: symbol not in namelist\n"); 100 return; 101 } 102 103 kread((u_long)rtree, &rt_head, sizeof(rt_head)); 104 kread((u_long)rtbl_id_max, &rtidxmax, sizeof(rtidxmax)); 105 kread((long)af2idx, &af2rtafidx, sizeof(af2rtafidx)); 106 107 for (i = 0; i <= AF_MAX; i++) { 108 if (af2rtafidx[i] > idxmax) 109 idxmax = af2rtafidx[i]; 110 } 111 112 if ((rnt = calloc(rtidxmax + 1, sizeof(struct radix_node_head **))) == 113 NULL) 114 err(1, NULL); 115 116 kread((u_long)rt_head, rnt, (rtidxmax + 1) * 117 sizeof(struct radix_node_head **)); 118 if (tableid > rtidxmax || rnt[tableid] == NULL) { 119 printf("Bad table %u\n", tableid); 120 return; 121 } 122 kread((u_long)rnt[tableid], rt_tables, (idxmax + 1) * sizeof(rnh)); 123 124 for (i = 0; i <= AF_MAX; i++) { 125 if (i == AF_UNSPEC) { 126 if (Aflag && (af == AF_UNSPEC || af == 0xff)) { 127 kread(mtree, &rnh, sizeof(rnh)); 128 kread((u_long)rnh, &head, sizeof(head)); 129 printf("Netmasks:\n"); 130 p_tree(head.rnh_treetop); 131 } 132 continue; 133 } 134 if (af2rtafidx[i] == 0) 135 /* no table for this AF */ 136 continue; 137 if ((rnh = rt_tables[af2rtafidx[i]]) == NULL) 138 continue; 139 kread((u_long)rnh, &head, sizeof(head)); 140 if (af == AF_UNSPEC || af == i) { 141 pr_family(i); 142 do_rtent = 1; 143 pr_rthdr(i, Aflag); 144 p_tree(head.rnh_treetop); 145 } 146 } 147 } 148 149 static struct sockaddr * 150 kgetsa(struct sockaddr *dst) 151 { 152 153 kread((u_long)dst, &pt_u.u_sa, sizeof(pt_u.u_sa)); 154 if (pt_u.u_sa.sa_len > sizeof (pt_u.u_sa)) 155 kread((u_long)dst, pt_u.u_data, pt_u.u_sa.sa_len); 156 return (&pt_u.u_sa); 157 } 158 159 static void 160 p_tree(struct radix_node *rn) 161 { 162 163 again: 164 kread((u_long)rn, &rnode, sizeof(rnode)); 165 if (rnode.rn_b < 0) { 166 if (Aflag) 167 printf("%-16p ", rn); 168 if (rnode.rn_flags & RNF_ROOT) { 169 if (Aflag) 170 printf("(root node)%s", 171 rnode.rn_dupedkey ? " =>\n" : "\n"); 172 } else if (do_rtent) { 173 kread((u_long)rn, &rtentry, sizeof(rtentry)); 174 p_krtentry(&rtentry); 175 if (Aflag) 176 p_rtnode(); 177 } else { 178 p_sockaddr(kgetsa((struct sockaddr *)rnode.rn_key), 179 0, 0, 44); 180 putchar('\n'); 181 } 182 if ((rn = rnode.rn_dupedkey)) 183 goto again; 184 } else { 185 if (Aflag && do_rtent) { 186 printf("%-16p ", rn); 187 p_rtnode(); 188 } 189 rn = rnode.rn_r; 190 p_tree(rnode.rn_l); 191 p_tree(rn); 192 } 193 } 194 195 static void 196 p_rtflags(u_char flags) 197 { 198 putchar('<'); 199 if (flags & RNF_NORMAL) 200 putchar('N'); 201 if (flags & RNF_ROOT) 202 putchar('R'); 203 if (flags & RNF_ACTIVE) 204 putchar('A'); 205 if (flags & ~(RNF_NORMAL | RNF_ROOT | RNF_ACTIVE)) 206 printf("/0x%02x", flags); 207 putchar('>'); 208 } 209 210 char nbuf[25]; 211 212 static void 213 p_rtnode(void) 214 { 215 struct radix_mask *rm = rnode.rn_mklist; 216 217 if (rnode.rn_b < 0) { 218 snprintf(nbuf, sizeof nbuf, " => %p", rnode.rn_dupedkey); 219 printf("\t (%p)%s", rnode.rn_p, rnode.rn_dupedkey ? nbuf : ""); 220 if (rnode.rn_mask) { 221 printf(" mask "); 222 p_sockaddr(kgetsa((struct sockaddr *)rnode.rn_mask), 223 0, 0, -1); 224 } else if (rm == NULL) { 225 putchar('\n'); 226 return; 227 } 228 } else { 229 snprintf(nbuf, sizeof nbuf, "(%d)", rnode.rn_b); 230 printf("%6.6s (%p) %16p : %16p", nbuf, 231 rnode.rn_p, rnode.rn_l, rnode.rn_r); 232 } 233 234 putchar(' '); 235 p_rtflags(rnode.rn_flags); 236 237 while (rm) { 238 kread((u_long)rm, &rmask, sizeof(rmask)); 239 snprintf(nbuf, sizeof nbuf, " %d refs, ", rmask.rm_refs); 240 printf("\n\tmk = %p {(%d),%s", rm, -1 - rmask.rm_b, 241 rmask.rm_refs ? nbuf : " "); 242 p_rtflags(rmask.rm_flags); 243 printf(", "); 244 if (rmask.rm_flags & RNF_NORMAL) { 245 struct radix_node rnode_aux; 246 247 printf("leaf = %p ", rmask.rm_leaf); 248 kread((u_long)rmask.rm_leaf, &rnode_aux, sizeof(rnode_aux)); 249 p_sockaddr(kgetsa((struct sockaddr *)rnode_aux.rn_mask), 250 0, 0, -1); 251 } else 252 p_sockaddr(kgetsa((struct sockaddr *)rmask.rm_mask), 253 0, 0, -1); 254 putchar('}'); 255 if ((rm = rmask.rm_mklist)) 256 printf(" ->"); 257 } 258 putchar('\n'); 259 } 260 261 static void 262 p_krtentry(struct rtentry *rt) 263 { 264 struct sockaddr_storage sock1, sock2; 265 struct sockaddr *sa = (struct sockaddr *)&sock1; 266 struct sockaddr *mask = (struct sockaddr *)&sock2; 267 268 bcopy(kgetsa(rt_key(rt)), sa, sizeof(struct sockaddr)); 269 if (sa->sa_len > sizeof(struct sockaddr)) 270 bcopy(kgetsa(rt_key(rt)), sa, sa->sa_len); 271 272 if (sa->sa_family == PF_KEY) { 273 /* Ignore PF_KEY entries */ 274 return; 275 } 276 277 if (rt_mask(rt)) { 278 bcopy(kgetsa(rt_mask(rt)), mask, sizeof(struct sockaddr)); 279 if (sa->sa_len > sizeof(struct sockaddr)) 280 bcopy(kgetsa(rt_mask(rt)), mask, sa->sa_len); 281 } else 282 mask = 0; 283 284 p_addr(sa, mask, rt->rt_flags); 285 p_gwaddr(kgetsa(rt->rt_gateway), sa->sa_family); 286 p_flags(rt->rt_flags, "%-6.6s "); 287 printf("%5u %8lld ", rt->rt_refcnt, rt->rt_use); 288 if (rt->rt_rmx.rmx_mtu) 289 printf("%5u ", rt->rt_rmx.rmx_mtu); 290 else 291 printf("%5s ", "-"); 292 putchar((rt->rt_rmx.rmx_locks & RTV_MTU) ? 'L' : ' '); 293 printf(" %2d", rt->rt_priority); 294 295 if (rt->rt_ifidx != 0) { 296 printf(" if%d%s", rt->rt_ifidx, 297 rt->rt_nodes[0].rn_dupedkey ? " =>" : ""); 298 } 299 putchar('\n'); 300 if (vflag) 301 printf("\texpire %10lld%c\n", 302 (long long)rt->rt_rmx.rmx_expire, 303 (rt->rt_rmx.rmx_locks & RTV_EXPIRE) ? 'L' : ' '); 304 } 305 306 /* 307 * Print routing statistics 308 */ 309 void 310 rt_stats(void) 311 { 312 struct rtstat rtstat; 313 int mib[6]; 314 size_t size; 315 316 mib[0] = CTL_NET; 317 mib[1] = PF_ROUTE; 318 mib[2] = 0; 319 mib[3] = 0; 320 mib[4] = NET_RT_STATS; 321 mib[5] = 0; 322 size = sizeof (rtstat); 323 324 if (sysctl(mib, 6, &rtstat, &size, NULL, 0) < 0) { 325 perror("sysctl of routing table statistics"); 326 exit(1); 327 } 328 329 printf("routing:\n"); 330 printf("\t%u bad routing redirect%s\n", 331 rtstat.rts_badredirect, plural(rtstat.rts_badredirect)); 332 printf("\t%u dynamically created route%s\n", 333 rtstat.rts_dynamic, plural(rtstat.rts_dynamic)); 334 printf("\t%u new gateway%s due to redirects\n", 335 rtstat.rts_newgateway, plural(rtstat.rts_newgateway)); 336 printf("\t%u destination%s found unreachable\n", 337 rtstat.rts_unreach, plural(rtstat.rts_unreach)); 338 printf("\t%u use%s of a wildcard route\n", 339 rtstat.rts_wildcard, plural(rtstat.rts_wildcard)); 340 } 341