route.c revision 1.73 1 1.73 lukem /* $NetBSD: route.c,v 1.73 2009/04/12 16:08:37 lukem Exp $ */
2 1.14 thorpej
3 1.1 cgd /*
4 1.10 mycroft * Copyright (c) 1983, 1988, 1993
5 1.10 mycroft * The Regents of the University of California. All rights reserved.
6 1.1 cgd *
7 1.1 cgd * Redistribution and use in source and binary forms, with or without
8 1.1 cgd * modification, are permitted provided that the following conditions
9 1.1 cgd * are met:
10 1.1 cgd * 1. Redistributions of source code must retain the above copyright
11 1.1 cgd * notice, this list of conditions and the following disclaimer.
12 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 cgd * notice, this list of conditions and the following disclaimer in the
14 1.1 cgd * documentation and/or other materials provided with the distribution.
15 1.63 agc * 3. Neither the name of the University nor the names of its contributors
16 1.1 cgd * may be used to endorse or promote products derived from this software
17 1.1 cgd * without specific prior written permission.
18 1.1 cgd *
19 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 1.1 cgd * SUCH DAMAGE.
30 1.1 cgd */
31 1.1 cgd
32 1.23 lukem #include <sys/cdefs.h>
33 1.1 cgd #ifndef lint
34 1.14 thorpej #if 0
35 1.14 thorpej static char sccsid[] = "from: @(#)route.c 8.3 (Berkeley) 3/9/94";
36 1.14 thorpej #else
37 1.73 lukem __RCSID("$NetBSD: route.c,v 1.73 2009/04/12 16:08:37 lukem Exp $");
38 1.14 thorpej #endif
39 1.1 cgd #endif /* not lint */
40 1.1 cgd
41 1.70 dyoung #include <stdbool.h>
42 1.1 cgd #include <sys/param.h>
43 1.10 mycroft #include <sys/protosw.h>
44 1.1 cgd #include <sys/socket.h>
45 1.1 cgd #include <sys/mbuf.h>
46 1.30 mrg #include <sys/un.h>
47 1.1 cgd
48 1.1 cgd #include <net/if.h>
49 1.10 mycroft #include <net/if_dl.h>
50 1.10 mycroft #include <net/if_types.h>
51 1.11 jtc #define _KERNEL
52 1.1 cgd #include <net/route.h>
53 1.11 jtc #undef _KERNEL
54 1.1 cgd #include <netinet/in.h>
55 1.21 christos #include <netatalk/at.h>
56 1.30 mrg #include <netiso/iso.h>
57 1.1 cgd
58 1.10 mycroft #include <sys/sysctl.h>
59 1.4 paul
60 1.38 itojun #include <arpa/inet.h>
61 1.38 itojun
62 1.29 mrg #include <err.h>
63 1.66 rpaulo #include <kvm.h>
64 1.1 cgd #include <netdb.h>
65 1.1 cgd #include <stdio.h>
66 1.10 mycroft #include <stdlib.h>
67 1.1 cgd #include <string.h>
68 1.10 mycroft #include <unistd.h>
69 1.29 mrg
70 1.10 mycroft #include "netstat.h"
71 1.1 cgd
72 1.10 mycroft #define kget(p, d) (kread((u_long)(p), (char *)&(d), sizeof (d)))
73 1.1 cgd
74 1.39 itojun /* alignment constraint for routing socket */
75 1.39 itojun #define ROUNDUP(a) \
76 1.39 itojun ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
77 1.39 itojun #define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len))
78 1.39 itojun
79 1.1 cgd /*
80 1.30 mrg * XXX we put all of the sockaddr types in here to force the alignment
81 1.30 mrg * to be correct.
82 1.30 mrg */
83 1.37 chopps static union sockaddr_union {
84 1.10 mycroft struct sockaddr u_sa;
85 1.30 mrg struct sockaddr_in u_in;
86 1.30 mrg struct sockaddr_un u_un;
87 1.30 mrg struct sockaddr_iso u_iso;
88 1.30 mrg struct sockaddr_at u_at;
89 1.30 mrg struct sockaddr_dl u_dl;
90 1.10 mycroft u_short u_data[128];
91 1.38 itojun int u_dummy; /* force word-alignment */
92 1.10 mycroft } pt_u;
93 1.10 mycroft
94 1.10 mycroft int do_rtent = 0;
95 1.10 mycroft struct rtentry rtentry;
96 1.10 mycroft struct radix_node rnode;
97 1.10 mycroft struct radix_mask rmask;
98 1.10 mycroft
99 1.69 dyoung static struct sockaddr *kgetsa(const struct sockaddr *);
100 1.67 elad static void p_tree(struct radix_node *);
101 1.67 elad static void p_rtnode(void);
102 1.67 elad static void p_krtentry(struct rtentry *);
103 1.3 cgd
104 1.3 cgd /*
105 1.1 cgd * Print routing tables.
106 1.1 cgd */
107 1.10 mycroft void
108 1.10 mycroft routepr(rtree)
109 1.10 mycroft u_long rtree;
110 1.1 cgd {
111 1.10 mycroft struct radix_node_head *rnh, head;
112 1.73 lukem struct radix_node_head *rt_nodes[AF_MAX+1];
113 1.10 mycroft int i;
114 1.1 cgd
115 1.1 cgd printf("Routing tables\n");
116 1.10 mycroft
117 1.67 elad if (rtree == 0) {
118 1.67 elad printf("rt_tables: symbol not in namelist\n");
119 1.67 elad return;
120 1.67 elad }
121 1.10 mycroft
122 1.73 lukem kget(rtree, rt_nodes);
123 1.67 elad for (i = 0; i <= AF_MAX; i++) {
124 1.73 lukem if ((rnh = rt_nodes[i]) == 0)
125 1.67 elad continue;
126 1.67 elad kget(rnh, head);
127 1.67 elad if (i == AF_UNSPEC) {
128 1.67 elad if (Aflag && (af == 0 || af == 0xff)) {
129 1.67 elad printf("Netmasks:\n");
130 1.10 mycroft p_tree(head.rnh_treetop);
131 1.10 mycroft }
132 1.67 elad } else if (af == AF_UNSPEC || af == i) {
133 1.67 elad pr_family(i);
134 1.67 elad do_rtent = 1;
135 1.67 elad pr_rthdr(i, Aflag);
136 1.67 elad p_tree(head.rnh_treetop);
137 1.1 cgd }
138 1.1 cgd }
139 1.1 cgd }
140 1.1 cgd
141 1.10 mycroft static struct sockaddr *
142 1.69 dyoung kgetsa(const struct sockaddr *dst)
143 1.1 cgd {
144 1.10 mycroft
145 1.1 cgd kget(dst, pt_u.u_sa);
146 1.10 mycroft if (pt_u.u_sa.sa_len > sizeof (pt_u.u_sa))
147 1.10 mycroft kread((u_long)dst, (char *)pt_u.u_data, pt_u.u_sa.sa_len);
148 1.1 cgd return (&pt_u.u_sa);
149 1.1 cgd }
150 1.1 cgd
151 1.10 mycroft static void
152 1.1 cgd p_tree(rn)
153 1.10 mycroft struct radix_node *rn;
154 1.1 cgd {
155 1.1 cgd
156 1.1 cgd again:
157 1.1 cgd kget(rn, rnode);
158 1.1 cgd if (rnode.rn_b < 0) {
159 1.1 cgd if (Aflag)
160 1.21 christos printf("%-8.8lx ", (u_long) rn);
161 1.10 mycroft if (rnode.rn_flags & RNF_ROOT) {
162 1.10 mycroft if (Aflag)
163 1.10 mycroft printf("(root node)%s",
164 1.1 cgd rnode.rn_dupedkey ? " =>\n" : "\n");
165 1.10 mycroft } else if (do_rtent) {
166 1.1 cgd kget(rn, rtentry);
167 1.67 elad p_krtentry(&rtentry);
168 1.1 cgd if (Aflag)
169 1.1 cgd p_rtnode();
170 1.1 cgd } else {
171 1.73 lukem p_sockaddr(kgetsa((const struct sockaddr *)rnode.rn_key),
172 1.16 mellon NULL, 0, 44);
173 1.1 cgd putchar('\n');
174 1.1 cgd }
175 1.21 christos if ((rn = rnode.rn_dupedkey) != NULL)
176 1.1 cgd goto again;
177 1.1 cgd } else {
178 1.1 cgd if (Aflag && do_rtent) {
179 1.21 christos printf("%-8.8lx ", (u_long) rn);
180 1.1 cgd p_rtnode();
181 1.1 cgd }
182 1.1 cgd rn = rnode.rn_r;
183 1.1 cgd p_tree(rnode.rn_l);
184 1.1 cgd p_tree(rn);
185 1.1 cgd }
186 1.1 cgd }
187 1.1 cgd
188 1.10 mycroft static void
189 1.1 cgd p_rtnode()
190 1.1 cgd {
191 1.10 mycroft struct radix_mask *rm = rnode.rn_mklist;
192 1.29 mrg char nbuf[20];
193 1.1 cgd
194 1.1 cgd if (rnode.rn_b < 0) {
195 1.1 cgd if (rnode.rn_mask) {
196 1.1 cgd printf("\t mask ");
197 1.73 lukem p_sockaddr(kgetsa((const struct sockaddr *)rnode.rn_mask),
198 1.16 mellon NULL, 0, -1);
199 1.1 cgd } else if (rm == 0)
200 1.1 cgd return;
201 1.1 cgd } else {
202 1.29 mrg (void)snprintf(nbuf, sizeof nbuf, "(%d)", rnode.rn_b);
203 1.21 christos printf("%6.6s %8.8lx : %8.8lx", nbuf, (u_long) rnode.rn_l,
204 1.21 christos (u_long) rnode.rn_r);
205 1.1 cgd }
206 1.1 cgd while (rm) {
207 1.1 cgd kget(rm, rmask);
208 1.29 mrg (void)snprintf(nbuf, sizeof nbuf, " %d refs, ", rmask.rm_refs);
209 1.21 christos printf(" mk = %8.8lx {(%d),%s", (u_long) rm,
210 1.21 christos -1 - rmask.rm_b, rmask.rm_refs ? nbuf : " ");
211 1.16 mellon if (rmask.rm_flags & RNF_NORMAL) {
212 1.16 mellon struct radix_node rnode_aux;
213 1.16 mellon printf(" <normal>, ");
214 1.16 mellon kget(rmask.rm_leaf, rnode_aux);
215 1.73 lukem p_sockaddr(kgetsa((const struct sockaddr *)rnode_aux.rn_mask),
216 1.16 mellon NULL, 0, -1);
217 1.16 mellon } else
218 1.73 lukem p_sockaddr(kgetsa((const struct sockaddr *)rmask.rm_mask),
219 1.29 mrg NULL, 0, -1);
220 1.1 cgd putchar('}');
221 1.21 christos if ((rm = rmask.rm_mklist) != NULL)
222 1.1 cgd printf(" ->");
223 1.1 cgd }
224 1.1 cgd putchar('\n');
225 1.1 cgd }
226 1.1 cgd
227 1.37 chopps static struct sockaddr *sockcopy __P((struct sockaddr *,
228 1.37 chopps union sockaddr_union *));
229 1.35 chopps
230 1.35 chopps /*
231 1.35 chopps * copy a sockaddr into an allocated region, allocate at least sockaddr
232 1.35 chopps * bytes and zero unused
233 1.35 chopps */
234 1.35 chopps static struct sockaddr *
235 1.37 chopps sockcopy(sp, dp)
236 1.35 chopps struct sockaddr *sp;
237 1.37 chopps union sockaddr_union *dp;
238 1.35 chopps {
239 1.37 chopps int len;
240 1.35 chopps
241 1.37 chopps if (sp == 0 || sp->sa_len == 0)
242 1.37 chopps (void)memset(dp, 0, sizeof (*sp));
243 1.37 chopps else {
244 1.37 chopps len = (sp->sa_len >= sizeof (*sp)) ? sp->sa_len : sizeof (*sp);
245 1.37 chopps (void)memcpy(dp, sp, len);
246 1.37 chopps }
247 1.37 chopps return ((struct sockaddr *)dp);
248 1.35 chopps }
249 1.35 chopps
250 1.10 mycroft static void
251 1.67 elad p_krtentry(rt)
252 1.23 lukem struct rtentry *rt;
253 1.4 paul {
254 1.10 mycroft static struct ifnet ifnet, *lastif;
255 1.37 chopps union sockaddr_union addr_un, mask_un;
256 1.35 chopps struct sockaddr *addr, *mask;
257 1.40 is
258 1.40 is if (Lflag && (rt->rt_flags & RTF_LLINFO))
259 1.40 is return;
260 1.4 paul
261 1.38 itojun memset(&addr_un, 0, sizeof(addr_un));
262 1.38 itojun memset(&mask_un, 0, sizeof(mask_un));
263 1.69 dyoung addr = sockcopy(kgetsa(rt_getkey(rt)), &addr_un);
264 1.35 chopps if (rt_mask(rt))
265 1.37 chopps mask = sockcopy(kgetsa(rt_mask(rt)), &mask_un);
266 1.16 mellon else
267 1.38 itojun mask = sockcopy(NULL, &mask_un);
268 1.67 elad p_addr(addr, mask, rt->rt_flags);
269 1.67 elad p_gwaddr(kgetsa(rt->rt_gateway), kgetsa(rt->rt_gateway)->sa_family);
270 1.67 elad p_flags(rt->rt_flags, "%-6.6s ");
271 1.27 kml printf("%6d %8lu ", rt->rt_refcnt, rt->rt_use);
272 1.13 thorpej if (rt->rt_rmx.rmx_mtu)
273 1.27 kml printf("%6lu", rt->rt_rmx.rmx_mtu);
274 1.13 thorpej else
275 1.26 kml printf("%6s", "-");
276 1.26 kml putchar((rt->rt_rmx.rmx_locks & RTV_MTU) ? 'L' : ' ');
277 1.10 mycroft if (rt->rt_ifp) {
278 1.10 mycroft if (rt->rt_ifp != lastif) {
279 1.10 mycroft kget(rt->rt_ifp, ifnet);
280 1.10 mycroft lastif = rt->rt_ifp;
281 1.10 mycroft }
282 1.15 thorpej printf(" %.16s%s", ifnet.if_xname,
283 1.10 mycroft rt->rt_nodes[0].rn_dupedkey ? " =>" : "");
284 1.1 cgd }
285 1.10 mycroft putchar('\n');
286 1.34 kml if (vflag) {
287 1.72 christos printf("\texpire %10lld%c recvpipe %10ld%c "
288 1.34 kml "sendpipe %10ld%c\n",
289 1.72 christos (long long)rt->rt_rmx.rmx_expire,
290 1.34 kml (rt->rt_rmx.rmx_locks & RTV_EXPIRE) ? 'L' : ' ',
291 1.34 kml rt->rt_rmx.rmx_recvpipe,
292 1.34 kml (rt->rt_rmx.rmx_locks & RTV_RPIPE) ? 'L' : ' ',
293 1.34 kml rt->rt_rmx.rmx_sendpipe,
294 1.34 kml (rt->rt_rmx.rmx_locks & RTV_SPIPE) ? 'L' : ' ');
295 1.34 kml printf("\tssthresh %10lu%c rtt %10ld%c "
296 1.34 kml "rttvar %10ld%c\n",
297 1.34 kml rt->rt_rmx.rmx_ssthresh,
298 1.34 kml (rt->rt_rmx.rmx_locks & RTV_SSTHRESH) ? 'L' : ' ',
299 1.34 kml rt->rt_rmx.rmx_rtt,
300 1.34 kml (rt->rt_rmx.rmx_locks & RTV_RTT) ? 'L' : ' ',
301 1.34 kml rt->rt_rmx.rmx_rttvar,
302 1.34 kml (rt->rt_rmx.rmx_locks & RTV_RTTVAR) ? 'L' : ' ');
303 1.54 enami printf("\thopcount %10lu%c\n",
304 1.54 enami rt->rt_rmx.rmx_hopcount,
305 1.54 enami (rt->rt_rmx.rmx_locks & RTV_HOPCOUNT) ? 'L' : ' ');
306 1.54 enami }
307 1.1 cgd }
308 1.1 cgd
309 1.1 cgd /*
310 1.1 cgd * Print routing statistics
311 1.1 cgd */
312 1.10 mycroft void
313 1.1 cgd rt_stats(off)
314 1.9 cgd u_long off;
315 1.1 cgd {
316 1.73 lukem struct rtstat rtstats;
317 1.1 cgd
318 1.67 elad if (use_sysctl) {
319 1.73 lukem size_t rtsize = sizeof(rtstats);
320 1.67 elad
321 1.73 lukem if (sysctlbyname("net.route.stats", &rtstats, &rtsize,
322 1.67 elad NULL, 0) == -1)
323 1.67 elad err(1, "rt_stats: sysctl");
324 1.67 elad } else if (off == 0) {
325 1.1 cgd printf("rtstat: symbol not in namelist\n");
326 1.1 cgd return;
327 1.67 elad } else
328 1.73 lukem kread(off, (char *)&rtstats, sizeof(rtstats));
329 1.67 elad
330 1.1 cgd printf("routing:\n");
331 1.53 itojun printf("\t%llu bad routing redirect%s\n",
332 1.73 lukem (unsigned long long)rtstats.rts_badredirect,
333 1.73 lukem plural(rtstats.rts_badredirect));
334 1.53 itojun printf("\t%llu dynamically created route%s\n",
335 1.73 lukem (unsigned long long)rtstats.rts_dynamic,
336 1.73 lukem plural(rtstats.rts_dynamic));
337 1.53 itojun printf("\t%llu new gateway%s due to redirects\n",
338 1.73 lukem (unsigned long long)rtstats.rts_newgateway,
339 1.73 lukem plural(rtstats.rts_newgateway));
340 1.53 itojun printf("\t%llu destination%s found unreachable\n",
341 1.73 lukem (unsigned long long)rtstats.rts_unreach,
342 1.73 lukem plural(rtstats.rts_unreach));
343 1.53 itojun printf("\t%llu use%s of a wildcard route\n",
344 1.73 lukem (unsigned long long)rtstats.rts_wildcard,
345 1.73 lukem plural(rtstats.rts_wildcard));
346 1.1 cgd }
347 1.67 elad
348 1.10 mycroft void
349 1.1 cgd upHex(p0)
350 1.10 mycroft char *p0;
351 1.1 cgd {
352 1.23 lukem char *p = p0;
353 1.1 cgd
354 1.29 mrg for (; *p; p++)
355 1.29 mrg switch (*p) {
356 1.29 mrg case 'a':
357 1.29 mrg case 'b':
358 1.29 mrg case 'c':
359 1.29 mrg case 'd':
360 1.29 mrg case 'e':
361 1.29 mrg case 'f':
362 1.29 mrg *p += ('A' - 'a');
363 1.29 mrg }
364 1.1 cgd }
365 1.67 elad
366 1.67 elad
367