Remove building with NOCRYPTO option
[minix.git] / usr.bin / netstat / route.c
blobffa99ee18071e832f65fef9a48d183a77d2bc1e6
1 /* $NetBSD: route.c,v 1.84 2015/05/25 03:56:20 manu Exp $ */
3 /*
4 * Copyright (c) 1983, 1988, 1993
5 * The Regents of the University of California. All rights reserved.
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.
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.
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.84 2015/05/25 03:56:20 manu Exp $");
38 #endif
39 #endif /* not lint */
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>
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 <netmpls/mpls.h>
56 #include <sys/sysctl.h>
58 #include <arpa/inet.h>
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>
68 #include "netstat.h"
69 #include "rtutil.h"
71 #define kget(p, d) (kread((u_long)(p), (char *)&(d), sizeof (d)))
74 * XXX we put all of the sockaddr types in here to force the alignment
75 * to be correct.
77 static union sockaddr_union {
78 struct sockaddr u_sa;
79 struct sockaddr_in u_in;
80 struct sockaddr_un u_un;
81 struct sockaddr_at u_at;
82 struct sockaddr_dl u_dl;
83 u_short u_data[128];
84 int u_dummy; /* force word-alignment */
85 } pt_u;
87 int do_rtent = 0;
88 struct rtentry rtentry;
89 struct radix_node rnode;
90 struct radix_mask rmask;
92 static struct sockaddr *kgetsa(const struct sockaddr *);
93 static void p_tree(struct radix_node *);
94 static void p_rtnode(void);
95 static void p_krtentry(struct rtentry *);
98 * Print routing tables.
100 void
101 routepr(u_long rtree)
103 struct radix_node_head *rnh, head;
104 struct radix_node_head *rt_nodes[AF_MAX+1];
105 int i;
107 printf("Routing tables\n");
109 if (rtree == 0) {
110 printf("rt_tables: symbol not in namelist\n");
111 return;
114 kget(rtree, rt_nodes);
115 for (i = 0; i <= AF_MAX; i++) {
116 if ((rnh = rt_nodes[i]) == 0)
117 continue;
118 kget(rnh, head);
119 if (i == AF_UNSPEC) {
120 if (Aflag && (af == 0 || af == 0xff)) {
121 printf("Netmasks:\n");
122 p_tree(head.rnh_treetop);
124 } else if (af == AF_UNSPEC || af == i) {
125 p_family(i);
126 do_rtent = 1;
127 p_rthdr(i, Aflag);
128 p_tree(head.rnh_treetop);
133 static struct sockaddr *
134 kgetsa(const struct sockaddr *dst)
137 kget(dst, pt_u.u_sa);
138 if (pt_u.u_sa.sa_len > sizeof (pt_u.u_sa))
139 kread((u_long)dst, (char *)pt_u.u_data, pt_u.u_sa.sa_len);
140 return (&pt_u.u_sa);
143 static void
144 p_tree(struct radix_node *rn)
147 again:
148 kget(rn, rnode);
149 if (rnode.rn_b < 0) {
150 if (Aflag)
151 printf("%-8.8lx ", (u_long) rn);
152 if (rnode.rn_flags & RNF_ROOT) {
153 if (Aflag)
154 printf("(root node)%s",
155 rnode.rn_dupedkey ? " =>\n" : "\n");
156 } else if (do_rtent) {
157 kget(rn, rtentry);
158 p_krtentry(&rtentry);
159 if (Aflag)
160 p_rtnode();
161 } else {
162 p_sockaddr(kgetsa((const struct sockaddr *)rnode.rn_key),
163 NULL, 0, 44, nflag);
164 putchar('\n');
166 if ((rn = rnode.rn_dupedkey) != NULL)
167 goto again;
168 } else {
169 if (Aflag && do_rtent) {
170 printf("%-8.8lx ", (u_long) rn);
171 p_rtnode();
173 rn = rnode.rn_r;
174 p_tree(rnode.rn_l);
175 p_tree(rn);
179 static void
180 p_rtnode(void)
182 struct radix_mask *rm = rnode.rn_mklist;
183 char nbuf[20];
185 if (rnode.rn_b < 0) {
186 if (rnode.rn_mask) {
187 printf("\t mask ");
188 p_sockaddr(kgetsa((const struct sockaddr *)rnode.rn_mask),
189 NULL, 0, -1, nflag);
190 } else if (rm == 0)
191 return;
192 } else {
193 (void)snprintf(nbuf, sizeof nbuf, "(%d)", rnode.rn_b);
194 printf("%6.6s %8.8lx : %8.8lx", nbuf, (u_long) rnode.rn_l,
195 (u_long) rnode.rn_r);
197 while (rm) {
198 kget(rm, rmask);
199 (void)snprintf(nbuf, sizeof nbuf, " %d refs, ", rmask.rm_refs);
200 printf(" mk = %8.8lx {(%d),%s", (u_long) rm,
201 -1 - rmask.rm_b, rmask.rm_refs ? nbuf : " ");
202 if (rmask.rm_flags & RNF_NORMAL) {
203 struct radix_node rnode_aux;
204 printf(" <normal>, ");
205 kget(rmask.rm_leaf, rnode_aux);
206 p_sockaddr(kgetsa((const struct sockaddr *)rnode_aux.rn_mask),
207 NULL, 0, -1, nflag);
208 } else
209 p_sockaddr(kgetsa((const struct sockaddr *)rmask.rm_mask),
210 NULL, 0, -1, nflag);
211 putchar('}');
212 if ((rm = rmask.rm_mklist) != NULL)
213 printf(" ->");
215 putchar('\n');
218 static struct sockaddr *sockcopy(struct sockaddr *, union sockaddr_union *);
221 * copy a sockaddr into an allocated region, allocate at least sockaddr
222 * bytes and zero unused
224 static struct sockaddr *
225 sockcopy(struct sockaddr *sp, union sockaddr_union *dp)
227 int len;
229 if (sp == 0 || sp->sa_len == 0)
230 (void)memset(dp, 0, sizeof (*sp));
231 else {
232 len = (sp->sa_len >= sizeof (*sp)) ? sp->sa_len : sizeof (*sp);
233 (void)memcpy(dp, sp, len);
235 return ((struct sockaddr *)dp);
238 static void
239 p_krtentry(struct rtentry *rt)
241 static struct ifnet ifnet, *lastif;
242 union sockaddr_union addr_un, mask_un;
243 struct sockaddr *addr, *mask;
245 if (Lflag && (rt->rt_flags & RTF_LLINFO))
246 return;
248 memset(&addr_un, 0, sizeof(addr_un));
249 memset(&mask_un, 0, sizeof(mask_un));
250 addr = sockcopy(kgetsa(rt_getkey(rt)), &addr_un);
251 if (rt_mask(rt))
252 mask = sockcopy(kgetsa(rt_mask(rt)), &mask_un);
253 else
254 mask = sockcopy(NULL, &mask_un);
255 p_addr(addr, mask, rt->rt_flags, nflag);
256 p_gwaddr(kgetsa(rt->rt_gateway), kgetsa(rt->rt_gateway)->sa_family, nflag);
257 p_flags(rt->rt_flags);
258 printf("%6d %8"PRIu64" ", rt->rt_refcnt, rt->rt_use);
259 if (rt->rt_rmx.rmx_mtu)
260 printf("%6"PRIu64, rt->rt_rmx.rmx_mtu);
261 else
262 printf("%6s", "-");
263 putchar((rt->rt_rmx.rmx_locks & RTV_MTU) ? 'L' : ' ');
264 if (tagflag == 1) {
265 #ifndef SMALL
266 if (rt->rt_tag != NULL) {
267 const struct sockaddr *tagsa = kgetsa(rt->rt_tag);
268 char *tagstr;
270 if (tagsa->sa_family == AF_MPLS) {
271 tagstr = mpls_ntoa(tagsa);
272 if (strlen(tagstr) < 7)
273 printf("%7s", tagstr);
274 else
275 printf("%s", tagstr);
277 else
278 printf("%7s", "-");
279 } else
280 #endif
281 printf("%7s", "-");
283 if (rt->rt_ifp) {
284 if (rt->rt_ifp != lastif) {
285 kget(rt->rt_ifp, ifnet);
286 lastif = rt->rt_ifp;
288 printf(" %.16s%s", ifnet.if_xname,
289 rt->rt_nodes[0].rn_dupedkey ? " =>" : "");
291 putchar('\n');
292 #ifndef SMALL
293 if (vflag)
294 p_rtrmx(&rt->rt_rmx);
295 #endif
299 * Print routing statistics
301 void
302 rt_stats(u_long off)
304 struct rtstat rtstats;
306 if (use_sysctl) {
307 size_t rtsize = sizeof(rtstats);
309 if (sysctlbyname("net.route.stats", &rtstats, &rtsize,
310 NULL, 0) == -1)
311 err(1, "rt_stats: sysctl");
312 } else if (off == 0) {
313 printf("rtstat: symbol not in namelist\n");
314 return;
315 } else
316 kread(off, (char *)&rtstats, sizeof(rtstats));
318 printf("routing:\n");
319 printf("\t%llu bad routing redirect%s\n",
320 (unsigned long long)rtstats.rts_badredirect,
321 plural(rtstats.rts_badredirect));
322 printf("\t%llu dynamically created route%s\n",
323 (unsigned long long)rtstats.rts_dynamic,
324 plural(rtstats.rts_dynamic));
325 printf("\t%llu new gateway%s due to redirects\n",
326 (unsigned long long)rtstats.rts_newgateway,
327 plural(rtstats.rts_newgateway));
328 printf("\t%llu destination%s found unreachable\n",
329 (unsigned long long)rtstats.rts_unreach,
330 plural(rtstats.rts_unreach));
331 printf("\t%llu use%s of a wildcard route\n",
332 (unsigned long long)rtstats.rts_wildcard,
333 plural(rtstats.rts_wildcard));