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