1 /*        $NetBSD: route.c,v 1.88 2022/09/02 06:25:43 msaitoh Exp $   */
2 
3 /*
4  * Copyright (c) 1983, 1988, 1993
5  *        The Regents of the University of California.  All rights reserved.
6  *
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.
18  *
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.
30  */
31 
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.88 2022/09/02 06:25:43 msaitoh Exp $");
38 #endif
39 #endif /* not lint */
40 
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>
47 
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>
55 
56 #include <sys/sysctl.h>
57 
58 #include <arpa/inet.h>
59 
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>
67 
68 #include "netstat.h"
69 #include "rtutil.h"
70 
71 #define kget(p, d) (kread((u_long)(p), (char *)&(d), sizeof (d)))
72 
73 /*
74  * XXX we put all of the sockaddr types in here to force the alignment
75  * to be correct.
76  */
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;
86 
87 int       do_rtent = 0;
88 struct    rtentry rtentry;
89 struct    radix_node rnode;
90 struct    radix_mask rmask;
91 
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 *);
96 
97 /*
98  * Print routing tables.
99  */
100 void
routepr(u_long rtree)101 routepr(u_long rtree)
102 {
103           struct radix_node_head *rnh, head;
104           struct radix_node_head *rt_nodes[AF_MAX + 1];
105           int i;
106 
107           printf("Routing tables\n");
108 
109           if (rtree == 0) {
110                     printf("rt_tables: symbol not in namelist\n");
111                     return;
112           }
113 
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);
123                               }
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);
129                     }
130           }
131 }
132 
133 static struct sockaddr *
kgetsa(const struct sockaddr * dst)134 kgetsa(const struct sockaddr *dst)
135 {
136 
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);
141 }
142 
143 static void
p_tree(struct radix_node * rn)144 p_tree(struct radix_node *rn)
145 {
146 
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(
163                                         kgetsa((const struct sockaddr *)rnode.rn_key),
164                                         NULL, 0, 44, nflag);
165                               putchar('\n');
166                     }
167                     if ((rn = rnode.rn_dupedkey) != NULL)
168                               goto again;
169           } else {
170                     if (Aflag && do_rtent) {
171                               printf("%-8.8lx ", (u_long) rn);
172                               p_rtnode();
173                     }
174                     rn = rnode.rn_r;
175                     p_tree(rnode.rn_l);
176                     p_tree(rn);
177           }
178 }
179 
180 static void
p_rtnode(void)181 p_rtnode(void)
182 {
183           struct radix_mask *rm = rnode.rn_mklist;
184           char      nbuf[20];
185 
186           if (rnode.rn_b < 0) {
187                     if (rnode.rn_mask) {
188                               printf("\t  mask ");
189                               p_sockaddr(
190                                         kgetsa((const struct sockaddr *)rnode.rn_mask),
191                                         NULL, 0, -1, nflag);
192                     } else if (rm == 0)
193                               return;
194           } else {
195                     (void)snprintf(nbuf, sizeof nbuf, "(%d)", rnode.rn_b);
196                     printf("%6.6s %8.8lx : %8.8lx", nbuf, (u_long) rnode.rn_l,
197                         (u_long) rnode.rn_r);
198           }
199           while (rm) {
200                     kget(rm, rmask);
201                     (void)snprintf(nbuf, sizeof nbuf, " %d refs, ", rmask.rm_refs);
202                     printf(" mk = %8.8lx {(%d),%s", (u_long) rm,
203                         -1 - rmask.rm_b, rmask.rm_refs ? nbuf : " ");
204                     if (rmask.rm_flags & RNF_NORMAL) {
205                               struct radix_node rnode_aux;
206                               printf(" <normal>, ");
207                               kget(rmask.rm_leaf, rnode_aux);
208                               p_sockaddr(kgetsa((const struct sockaddr *)rnode_aux.rn_mask),
209                                   NULL, 0, -1, nflag);
210                     } else
211                               p_sockaddr(
212                                         kgetsa((const struct sockaddr *)rmask.rm_mask),
213                                         NULL, 0, -1, nflag);
214                     putchar('}');
215                     if ((rm = rmask.rm_mklist) != NULL)
216                               printf(" ->");
217           }
218           putchar('\n');
219 }
220 
221 static struct sockaddr *sockcopy(struct sockaddr *, union sockaddr_union *);
222 
223 /*
224  * copy a sockaddr into an allocated region, allocate at least sockaddr
225  * bytes and zero unused
226  */
227 static struct sockaddr *
sockcopy(struct sockaddr * sp,union sockaddr_union * dp)228 sockcopy(struct sockaddr *sp, union sockaddr_union *dp)
229 {
230           int len;
231 
232           if (sp == 0 || sp->sa_len == 0)
233                     (void)memset(dp, 0, sizeof (*sp));
234           else {
235                     len = (sp->sa_len >= sizeof (*sp)) ? sp->sa_len : sizeof (*sp);
236                     (void)memcpy(dp, sp, len);
237           }
238           return ((struct sockaddr *)dp);
239 }
240 
241 static void
p_krtentry(struct rtentry * rt)242 p_krtentry(struct rtentry *rt)
243 {
244           static struct ifnet ifnet, *lastif;
245           union sockaddr_union addr_un, mask_un;
246           struct sockaddr *addr, *mask;
247 
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,
257               nflag);
258           p_flags(rt->rt_flags);
259           printf("%6d %8"PRIu64" ", rt->rt_refcnt, rt->rt_use);
260           if (rt->rt_rmx.rmx_mtu)
261                     printf("%6"PRIu64, rt->rt_rmx.rmx_mtu);
262           else
263                     printf("%6s", "-");
264           putchar((rt->rt_rmx.rmx_locks & RTV_MTU) ? 'L' : ' ');
265           if (tagflag == 1) {
266 #ifndef SMALL
267                     if (rt->rt_tag != NULL) {
268                               const struct sockaddr *tagsa = kgetsa(rt->rt_tag);
269                               char *tagstr;
270 
271                               if (tagsa->sa_family == AF_MPLS) {
272                                         tagstr = mpls_ntoa(tagsa);
273                                         if (strlen(tagstr) < 7)
274                                                   printf("%7s", tagstr);
275                                         else
276                                                   printf("%s", tagstr);
277                               } else
278                                         printf("%7s", "-");
279                     } else
280 #endif
281                               printf("%7s", "-");
282           }
283           if (rt->rt_ifp) {
284                     if (rt->rt_ifp != lastif) {
285                               kget(rt->rt_ifp, ifnet);
286                               lastif = rt->rt_ifp;
287                     }
288                     printf(" %.16s%s", ifnet.if_xname,
289                               rt->rt_nodes[0].rn_dupedkey ? " =>" : "");
290           }
291           putchar('\n');
292 #ifndef SMALL
293           if (vflag)
294                     p_rtrmx(&rt->rt_rmx);
295 #endif
296 }
297 
298 /*
299  * Print routing statistics
300  */
301 void
rt_stats(u_long off)302 rt_stats(u_long off)
303 {
304           struct rtstat rtstats;
305 
306           if (use_sysctl) {
307                     size_t rtsize = sizeof(rtstats);
308 
309                     if (sysctlbyname("net.rtable.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));
317 
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));
334 }
335