Commit | Line | Data |
---|---|---|
984263bc MD |
1 | /* |
2 | * Copyright (c) 1983, 1988, 1993 | |
3 | * The Regents of the University of California. All rights reserved. | |
4 | * | |
5 | * Redistribution and use in source and binary forms, with or without | |
6 | * modification, are permitted provided that the following conditions | |
7 | * are met: | |
8 | * 1. Redistributions of source code must retain the above copyright | |
9 | * notice, this list of conditions and the following disclaimer. | |
10 | * 2. Redistributions in binary form must reproduce the above copyright | |
11 | * notice, this list of conditions and the following disclaimer in the | |
12 | * documentation and/or other materials provided with the distribution. | |
dc71b7ab | 13 | * 3. Neither the name of the University nor the names of its contributors |
984263bc MD |
14 | * may be used to endorse or promote products derived from this software |
15 | * without specific prior written permission. | |
16 | * | |
17 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
18 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
21 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
22 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
23 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
24 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
25 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
26 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
27 | * SUCH DAMAGE. | |
1de703da MD |
28 | * |
29 | * @(#)route.c 8.6 (Berkeley) 4/28/95 | |
30 | * $FreeBSD: src/usr.bin/netstat/route.c,v 1.41.2.14 2002/07/17 02:22:22 kbyanc Exp $ | |
984263bc MD |
31 | */ |
32 | ||
5569e488 | 33 | #define _KERNEL_STRUCTURES |
e0ecab34 | 34 | #include <sys/kinfo.h> |
984263bc | 35 | #include <sys/param.h> |
984263bc | 36 | #include <sys/socket.h> |
e43c2985 | 37 | #include <sys/sysctl.h> |
984263bc MD |
38 | #include <sys/time.h> |
39 | ||
40 | #include <net/ethernet.h> | |
41 | #include <net/if.h> | |
5569e488 | 42 | #include <net/if_var.h> /* for struct ifnet */ |
984263bc MD |
43 | #include <net/if_dl.h> |
44 | #include <net/if_types.h> | |
45 | #include <net/route.h> | |
46 | ||
47 | #include <netinet/in.h> | |
38a690d7 | 48 | #include <netgraph/socket/ng_socket.h> |
984263bc | 49 | |
9b42cabe NA |
50 | #include <netproto/mpls/mpls.h> |
51 | ||
984263bc | 52 | #include <arpa/inet.h> |
e43c2985 AL |
53 | #include <err.h> |
54 | #include <kinfo.h> | |
984263bc MD |
55 | #include <libutil.h> |
56 | #include <netdb.h> | |
57 | #include <stdio.h> | |
58 | #include <stdlib.h> | |
59 | #include <string.h> | |
984263bc | 60 | #include <time.h> |
e43c2985 AL |
61 | #include <unistd.h> |
62 | ||
984263bc MD |
63 | #include "netstat.h" |
64 | ||
65 | #define kget(p, d) (kread((u_long)(p), (char *)&(d), sizeof (d))) | |
7d605468 | 66 | #define constSA(x) ((const struct sockaddr *)(x)) |
984263bc | 67 | |
984263bc MD |
68 | /* |
69 | * Definitions for showing gateway flags. | |
70 | */ | |
71 | struct bits { | |
72 | u_long b_mask; | |
73 | char b_val; | |
74 | } bits[] = { | |
75 | { RTF_UP, 'U' }, | |
76 | { RTF_GATEWAY, 'G' }, | |
77 | { RTF_HOST, 'H' }, | |
78 | { RTF_REJECT, 'R' }, | |
79 | { RTF_DYNAMIC, 'D' }, | |
80 | { RTF_MODIFIED, 'M' }, | |
81 | { RTF_DONE, 'd' }, /* Completed -- for routing messages only */ | |
82 | { RTF_CLONING, 'C' }, | |
83 | { RTF_XRESOLVE, 'X' }, | |
84 | { RTF_LLINFO, 'L' }, | |
85 | { RTF_STATIC, 'S' }, | |
86 | { RTF_PROTO1, '1' }, | |
87 | { RTF_PROTO2, '2' }, | |
88 | { RTF_WASCLONED,'W' }, | |
89 | { RTF_PRCLONING,'c' }, | |
90 | { RTF_PROTO3, '3' }, | |
91 | { RTF_BLACKHOLE,'B' }, | |
92 | { RTF_BROADCAST,'b' }, | |
9b42cabe | 93 | { RTF_MPLSOPS, 'm' }, |
b82e1528 | 94 | { 0, 0 } |
984263bc MD |
95 | }; |
96 | ||
97 | typedef union { | |
98 | long dummy; /* Helps align structure. */ | |
99 | struct sockaddr u_sa; | |
100 | u_short u_data[128]; | |
101 | } sa_u; | |
102 | ||
e43c2985 AL |
103 | static sa_u pt_u; |
104 | static int NewTree = 0; | |
105 | static struct radix_node_head *rt_tables[AF_MAX+1]; | |
984263bc | 106 | |
7d605468 | 107 | static struct sockaddr *kgetsa (const struct sockaddr *); |
984263bc MD |
108 | static void size_cols (int ef, struct radix_node *rn); |
109 | static void size_cols_tree (struct radix_node *rn); | |
110 | static void size_cols_rtentry (struct rtentry *rt); | |
e43c2985 AL |
111 | static void p_tree (struct radix_node *, int); |
112 | static void p_rtnode (struct radix_node *); | |
984263bc MD |
113 | static void ntreestuff (void); |
114 | static void np_rtentry (struct rt_msghdr *); | |
115 | static void p_sockaddr (struct sockaddr *, struct sockaddr *, int, int); | |
116 | static const char *fmt_sockaddr (struct sockaddr *sa, struct sockaddr *mask, | |
117 | int flags); | |
b82e1528 | 118 | static void p_flags (int, const char *); |
984263bc MD |
119 | static const char *fmt_flags(int f); |
120 | static void p_rtentry (struct rtentry *); | |
121 | static u_long forgemask (u_long); | |
122 | static void domask (char *, u_long, u_long); | |
9b42cabe | 123 | static const char *labelops(struct rtentry *); |
984263bc MD |
124 | |
125 | /* | |
126 | * Print routing tables. | |
127 | */ | |
128 | void | |
129 | routepr(u_long rtree) | |
130 | { | |
131 | struct radix_node_head *rnh, head; | |
132 | int i; | |
133 | ||
134 | printf("Routing tables\n"); | |
135 | ||
b06a5e2a | 136 | if (Aflag == 0 && NewTree) { |
984263bc | 137 | ntreestuff(); |
b06a5e2a | 138 | } else { |
984263bc MD |
139 | if (rtree == 0) { |
140 | printf("rt_tables: symbol not in namelist\n"); | |
141 | return; | |
142 | } | |
b06a5e2a MD |
143 | if (cpuflag >= 0) { |
144 | /* | |
145 | * Severe hack. | |
146 | */ | |
147 | rtree += cpuflag * (AF_MAX + 1) * sizeof(void *); | |
148 | } | |
0e23ab10 SZ |
149 | if (kget(rtree, rt_tables) != 0) |
150 | return; | |
984263bc | 151 | for (i = 0; i <= AF_MAX; i++) { |
678e8cc6 | 152 | if ((rnh = rt_tables[i]) == NULL) |
984263bc | 153 | continue; |
0e23ab10 SZ |
154 | if (kget(rnh, head) != 0) |
155 | continue; | |
984263bc MD |
156 | if (i == AF_UNSPEC) { |
157 | if (Aflag && af == 0) { | |
158 | printf("Netmasks:\n"); | |
e43c2985 | 159 | p_tree(head.rnh_treetop, 0); |
984263bc MD |
160 | } |
161 | } else if (af == AF_UNSPEC || af == i) { | |
162 | size_cols(i, head.rnh_treetop); | |
163 | pr_family(i); | |
984263bc | 164 | pr_rthdr(i); |
e43c2985 | 165 | p_tree(head.rnh_treetop, 1); |
984263bc MD |
166 | } |
167 | } | |
168 | } | |
169 | } | |
170 | ||
171 | /* | |
172 | * Print address family header before a section of the routing table. | |
173 | */ | |
174 | void | |
b82e1528 | 175 | pr_family(int af1) |
984263bc | 176 | { |
b82e1528 | 177 | const char *afname; |
984263bc | 178 | |
b82e1528 | 179 | switch (af1) { |
984263bc MD |
180 | case AF_INET: |
181 | afname = "Internet"; | |
182 | break; | |
183 | #ifdef INET6 | |
184 | case AF_INET6: | |
185 | afname = "Internet6"; | |
186 | break; | |
187 | #endif /*INET6*/ | |
984263bc MD |
188 | case AF_CCITT: |
189 | afname = "X.25"; | |
190 | break; | |
191 | case AF_NETGRAPH: | |
192 | afname = "Netgraph"; | |
193 | break; | |
9b42cabe NA |
194 | case AF_MPLS: |
195 | afname = "MPLS"; | |
196 | break; | |
984263bc MD |
197 | default: |
198 | afname = NULL; | |
199 | break; | |
200 | } | |
201 | if (afname) | |
202 | printf("\n%s:\n", afname); | |
203 | else | |
b82e1528 | 204 | printf("\nProtocol Family %d:\n", af1); |
984263bc MD |
205 | } |
206 | ||
207 | /* column widths; each followed by one space */ | |
208 | #ifndef INET6 | |
209 | #define WID_DST_DEFAULT(af) 18 /* width of destination column */ | |
210 | #define WID_GW_DEFAULT(af) 18 /* width of gateway column */ | |
ad668869 | 211 | #define WID_IF_DEFAULT(af) (Wflag ? 10 : 8) /* width of netif column */ |
984263bc MD |
212 | #else |
213 | #define WID_DST_DEFAULT(af) \ | |
214 | ((af) == AF_INET6 ? (numeric_addr ? 33: 18) : 18) | |
215 | #define WID_GW_DEFAULT(af) \ | |
216 | ((af) == AF_INET6 ? (numeric_addr ? 29 : 18) : 18) | |
35d7d0f9 | 217 | #define WID_IF_DEFAULT(af) ((af) == AF_INET6 ? 8 : (Wflag ? 8 :6)) |
984263bc MD |
218 | #endif /*INET6*/ |
219 | ||
220 | static int wid_dst; | |
221 | static int wid_gw; | |
222 | static int wid_flags; | |
223 | static int wid_refs; | |
224 | static int wid_use; | |
225 | static int wid_mtu; | |
226 | static int wid_if; | |
227 | static int wid_expire; | |
9b42cabe | 228 | static int wid_mplslops; |
8ec89e60 | 229 | static int wid_msl; |
bf522a91 SZ |
230 | static int wid_iwmax; |
231 | static int wid_iw; | |
984263bc MD |
232 | |
233 | static void | |
234 | size_cols(int ef, struct radix_node *rn) | |
235 | { | |
236 | wid_dst = WID_DST_DEFAULT(ef); | |
237 | wid_gw = WID_GW_DEFAULT(ef); | |
238 | wid_flags = 6; | |
239 | wid_refs = 6; | |
240 | wid_use = 8; | |
241 | wid_mtu = 6; | |
242 | wid_if = WID_IF_DEFAULT(ef); | |
243 | wid_expire = 6; | |
9b42cabe | 244 | wid_mplslops = 7; |
8ec89e60 | 245 | wid_msl = 7; |
bf522a91 SZ |
246 | wid_iwmax = 5; |
247 | wid_iw = 2; | |
984263bc MD |
248 | |
249 | if (Wflag) | |
250 | size_cols_tree(rn); | |
251 | } | |
252 | ||
253 | static void | |
254 | size_cols_tree(struct radix_node *rn) | |
255 | { | |
e43c2985 AL |
256 | struct radix_node rnode; |
257 | struct rtentry rtentry; | |
258 | ||
984263bc | 259 | again: |
0e23ab10 SZ |
260 | if (kget(rn, rnode) != 0) |
261 | return; | |
262 | if (!(rnode.rn_flags & RNF_ACTIVE)) | |
263 | return; | |
984263bc MD |
264 | if (rnode.rn_bit < 0) { |
265 | if ((rnode.rn_flags & RNF_ROOT) == 0) { | |
0e23ab10 SZ |
266 | if (kget(rn, rtentry) != 0) |
267 | return; | |
984263bc MD |
268 | size_cols_rtentry(&rtentry); |
269 | } | |
e43c2985 | 270 | if ((rn = rnode.rn_dupedkey) != NULL) |
984263bc MD |
271 | goto again; |
272 | } else { | |
273 | rn = rnode.rn_right; | |
274 | size_cols_tree(rnode.rn_left); | |
275 | size_cols_tree(rn); | |
276 | } | |
277 | } | |
278 | ||
279 | static void | |
280 | size_cols_rtentry(struct rtentry *rt) | |
281 | { | |
282 | static struct ifnet ifnet, *lastif; | |
283 | struct rtentry parent; | |
284 | static char buffer[100]; | |
285 | const char *bp; | |
286 | struct sockaddr *sa; | |
287 | sa_u addr, mask; | |
b82e1528 | 288 | int len; |
984263bc MD |
289 | |
290 | /* | |
291 | * Don't print protocol-cloned routes unless -a. | |
292 | */ | |
293 | if (rt->rt_flags & RTF_WASCLONED && !aflag) { | |
0e23ab10 SZ |
294 | if (kget(rt->rt_parent, parent) != 0) |
295 | return; | |
984263bc MD |
296 | if (parent.rt_flags & RTF_PRCLONING) |
297 | return; | |
298 | } | |
299 | ||
300 | bzero(&addr, sizeof(addr)); | |
301 | if ((sa = kgetsa(rt_key(rt)))) | |
302 | bcopy(sa, &addr, sa->sa_len); | |
303 | bzero(&mask, sizeof(mask)); | |
304 | if (rt_mask(rt) && (sa = kgetsa(rt_mask(rt)))) | |
305 | bcopy(sa, &mask, sa->sa_len); | |
306 | bp = fmt_sockaddr(&addr.u_sa, &mask.u_sa, rt->rt_flags); | |
307 | len = strlen(bp); | |
308 | wid_dst = MAX(len, wid_dst); | |
309 | ||
310 | bp = fmt_sockaddr(kgetsa(rt->rt_gateway), NULL, RTF_HOST); | |
311 | len = strlen(bp); | |
312 | wid_gw = MAX(len, wid_gw); | |
313 | ||
314 | bp = fmt_flags(rt->rt_flags); | |
315 | len = strlen(bp); | |
316 | wid_flags = MAX(len, wid_flags); | |
317 | ||
318 | if (addr.u_sa.sa_family == AF_INET || Wflag) { | |
319 | len = snprintf(buffer, sizeof(buffer), "%ld", rt->rt_refcnt); | |
320 | wid_refs = MAX(len, wid_refs); | |
321 | len = snprintf(buffer, sizeof(buffer), "%lu", rt->rt_use); | |
322 | wid_use = MAX(len, wid_use); | |
323 | if (Wflag && rt->rt_rmx.rmx_mtu != 0) { | |
324 | len = snprintf(buffer, sizeof(buffer), | |
325 | "%lu", rt->rt_rmx.rmx_mtu); | |
326 | wid_mtu = MAX(len, wid_mtu); | |
327 | } | |
328 | } | |
329 | if (rt->rt_ifp) { | |
330 | if (rt->rt_ifp != lastif) { | |
7d605468 | 331 | if (kget(rt->rt_ifp, ifnet) == 0) |
0e23ab10 SZ |
332 | len = strlen(ifnet.if_xname); |
333 | else | |
334 | len = strlen("---"); | |
984263bc | 335 | lastif = rt->rt_ifp; |
984263bc MD |
336 | wid_if = MAX(len, wid_if); |
337 | } | |
338 | if (rt->rt_rmx.rmx_expire) { | |
748810cc MD |
339 | struct timespec sp; |
340 | int expire_time; | |
984263bc | 341 | |
748810cc | 342 | clock_gettime(CLOCK_MONOTONIC, &sp); |
748810cc | 343 | expire_time = (int)(rt->rt_rmx.rmx_expire - sp.tv_sec); |
748810cc | 344 | if (expire_time > 0) { |
984263bc MD |
345 | snprintf(buffer, sizeof(buffer), "%d", |
346 | (int)expire_time); | |
347 | wid_expire = MAX(len, wid_expire); | |
348 | } | |
349 | } | |
350 | } | |
8ec89e60 SZ |
351 | if (Wflag) { |
352 | if (rt->rt_shim[0] != NULL) { | |
353 | len = strlen(labelops(rt)); | |
354 | wid_mplslops = MAX(len, wid_mplslops); | |
355 | } | |
356 | ||
357 | if (rt->rt_rmx.rmx_msl) { | |
358 | len = snprintf(buffer, sizeof(buffer), | |
359 | "%lu", rt->rt_rmx.rmx_msl); | |
360 | wid_msl = MAX(len, wid_msl); | |
361 | } | |
bf522a91 SZ |
362 | if (rt->rt_rmx.rmx_iwmaxsegs) { |
363 | len = snprintf(buffer, sizeof(buffer), | |
364 | "%lu", rt->rt_rmx.rmx_iwmaxsegs); | |
365 | wid_iwmax = MAX(len, wid_iwmax); | |
366 | } | |
367 | if (rt->rt_rmx.rmx_iwcapsegs) { | |
368 | len = snprintf(buffer, sizeof(buffer), | |
369 | "%lu", rt->rt_rmx.rmx_iwcapsegs); | |
370 | wid_iw = MAX(len, wid_iw); | |
371 | } | |
8ec89e60 | 372 | } |
984263bc MD |
373 | } |
374 | ||
375 | ||
376 | /* | |
377 | * Print header for routing table columns. | |
378 | */ | |
379 | void | |
b82e1528 | 380 | pr_rthdr(int af1) |
984263bc MD |
381 | { |
382 | ||
383 | if (Aflag) | |
384 | printf("%-8.8s ","Address"); | |
b82e1528 | 385 | if (af1 == AF_INET || Wflag) { |
984263bc | 386 | if (Wflag) { |
bf522a91 SZ |
387 | printf("%-*.*s %-*.*s %-*.*s %*.*s %*.*s %*.*s %*.*s " |
388 | "%*s %-*s%*s %*s %*s\n", | |
984263bc MD |
389 | wid_dst, wid_dst, "Destination", |
390 | wid_gw, wid_gw, "Gateway", | |
391 | wid_flags, wid_flags, "Flags", | |
392 | wid_refs, wid_refs, "Refs", | |
393 | wid_use, wid_use, "Use", | |
394 | wid_mtu, wid_mtu, "Mtu", | |
395 | wid_if, wid_if, "Netif", | |
9b42cabe | 396 | wid_expire, "Expire", |
8ec89e60 | 397 | wid_mplslops, "Labelops", |
bf522a91 SZ |
398 | wid_msl, "Msl", |
399 | wid_iwmax, "IWmax", | |
400 | wid_iw, "IW"); | |
984263bc MD |
401 | } else { |
402 | printf("%-*.*s %-*.*s %-*.*s %*.*s %*.*s %*.*s %*s\n", | |
403 | wid_dst, wid_dst, "Destination", | |
404 | wid_gw, wid_gw, "Gateway", | |
405 | wid_flags, wid_flags, "Flags", | |
406 | wid_refs, wid_refs, "Refs", | |
407 | wid_use, wid_use, "Use", | |
408 | wid_if, wid_if, "Netif", | |
409 | wid_expire, "Expire"); | |
410 | } | |
411 | } else { | |
412 | printf("%-*.*s %-*.*s %-*.*s %*.*s %*s\n", | |
413 | wid_dst, wid_dst, "Destination", | |
414 | wid_gw, wid_gw, "Gateway", | |
415 | wid_flags, wid_flags, "Flags", | |
416 | wid_if, wid_if, "Netif", | |
417 | wid_expire, "Expire"); | |
418 | } | |
419 | } | |
420 | ||
421 | static struct sockaddr * | |
7d605468 | 422 | kgetsa(const struct sockaddr *dst) |
984263bc MD |
423 | { |
424 | ||
0e23ab10 SZ |
425 | if (kget(dst, pt_u.u_sa) != 0) |
426 | return (NULL); | |
984263bc MD |
427 | if (pt_u.u_sa.sa_len > sizeof (pt_u.u_sa)) |
428 | kread((u_long)dst, (char *)pt_u.u_data, pt_u.u_sa.sa_len); | |
429 | return (&pt_u.u_sa); | |
430 | } | |
431 | ||
432 | static void | |
e43c2985 | 433 | p_tree(struct radix_node *rn, int is_rtentry) |
984263bc | 434 | { |
e43c2985 AL |
435 | struct radix_node rnode; |
436 | struct rtentry rtentry; | |
984263bc MD |
437 | |
438 | again: | |
0e23ab10 SZ |
439 | if (kget(rn, rnode) != 0) |
440 | return; | |
441 | if (!(rnode.rn_flags & RNF_ACTIVE)) | |
442 | return; | |
984263bc MD |
443 | if (rnode.rn_bit < 0) { |
444 | if (Aflag) | |
445 | printf("%-8.8lx ", (u_long)rn); | |
446 | if (rnode.rn_flags & RNF_ROOT) { | |
447 | if (Aflag) | |
448 | printf("(root node)%s", | |
04a3b05a | 449 | rnode.rn_dupedkey ? " =>\n" : "\n"); |
e43c2985 | 450 | } else if (is_rtentry) { |
0e23ab10 SZ |
451 | if (kget(rn, rtentry) == 0) { |
452 | p_rtentry(&rtentry); | |
453 | if (Aflag) | |
e43c2985 | 454 | p_rtnode(&rnode); |
0e23ab10 | 455 | } |
984263bc | 456 | } else { |
7d605468 | 457 | p_sockaddr(kgetsa(constSA(rnode.rn_key)), NULL, 0, 44); |
984263bc MD |
458 | putchar('\n'); |
459 | } | |
e43c2985 | 460 | if ((rn = rnode.rn_dupedkey) != NULL) |
984263bc MD |
461 | goto again; |
462 | } else { | |
e43c2985 | 463 | if (Aflag && is_rtentry) { |
984263bc | 464 | printf("%-8.8lx ", (u_long)rn); |
e43c2985 | 465 | p_rtnode(&rnode); |
984263bc MD |
466 | } |
467 | rn = rnode.rn_right; | |
e43c2985 AL |
468 | p_tree(rnode.rn_left, is_rtentry); |
469 | p_tree(rn, is_rtentry); | |
984263bc MD |
470 | } |
471 | } | |
472 | ||
984263bc | 473 | static void |
e43c2985 | 474 | p_rtnode(struct radix_node *rnode) |
984263bc | 475 | { |
e43c2985 AL |
476 | struct radix_node rnode_aux; |
477 | struct radix_mask rmask, *rm = rnode->rn_mklist; | |
478 | char nbuf[20]; | |
984263bc | 479 | |
e43c2985 AL |
480 | if (rnode->rn_bit < 0) { |
481 | if (rnode->rn_mask) { | |
984263bc | 482 | printf("\t mask "); |
e43c2985 | 483 | p_sockaddr(kgetsa(constSA(rnode->rn_mask)), |
984263bc | 484 | NULL, 0, -1); |
678e8cc6 | 485 | } else if (rm == NULL) |
984263bc MD |
486 | return; |
487 | } else { | |
e43c2985 | 488 | sprintf(nbuf, "(%d)", rnode->rn_bit); |
04a3b05a | 489 | printf("%6.6s %8.8lx : %8.8lx", nbuf, |
e43c2985 | 490 | (u_long)rnode->rn_left, (u_long)rnode->rn_right); |
984263bc | 491 | } |
e43c2985 | 492 | while (rm != NULL) { |
0e23ab10 SZ |
493 | if (kget(rm, rmask) != 0) |
494 | break; | |
984263bc | 495 | sprintf(nbuf, " %d refs, ", rmask.rm_refs); |
04a3b05a AL |
496 | printf(" mk = %8.8lx {(%d),%s", (u_long)rm, |
497 | -1 - rmask.rm_bit, rmask.rm_refs ? nbuf : " "); | |
984263bc | 498 | if (rmask.rm_flags & RNF_NORMAL) { |
984263bc | 499 | printf(" <normal>, "); |
0e23ab10 | 500 | if (kget(rmask.rm_leaf, rnode_aux) == 0) |
7d605468 AL |
501 | p_sockaddr(kgetsa(constSA(rnode_aux.rn_mask)), |
502 | NULL, 0, -1); | |
0e23ab10 SZ |
503 | else |
504 | p_sockaddr(NULL, NULL, 0, -1); | |
7d605468 AL |
505 | } else { |
506 | p_sockaddr(kgetsa(constSA(rmask.rm_mask)), | |
507 | NULL, 0, -1); | |
508 | } | |
984263bc | 509 | putchar('}'); |
e43c2985 | 510 | if ((rm = rmask.rm_next) != NULL) |
984263bc MD |
511 | printf(" ->"); |
512 | } | |
513 | putchar('\n'); | |
514 | } | |
515 | ||
516 | static void | |
517 | ntreestuff(void) | |
518 | { | |
519 | size_t needed; | |
b06a5e2a MD |
520 | int mib[7]; |
521 | int miblen; | |
984263bc | 522 | char *buf, *next, *lim; |
6bd457ed | 523 | struct rt_msghdr *rtm; |
984263bc MD |
524 | |
525 | mib[0] = CTL_NET; | |
526 | mib[1] = PF_ROUTE; | |
527 | mib[2] = 0; | |
528 | mib[3] = 0; | |
529 | mib[4] = NET_RT_DUMP; | |
530 | mib[5] = 0; | |
b06a5e2a MD |
531 | if (cpuflag >= 0) { |
532 | mib[6] = cpuflag; | |
533 | miblen = 7; | |
534 | } else { | |
535 | miblen = 6; | |
536 | } | |
537 | if (sysctl(mib, miblen, NULL, &needed, NULL, 0) < 0) { | |
984263bc MD |
538 | err(1, "sysctl: net.route.0.0.dump estimate"); |
539 | } | |
540 | ||
678e8cc6 | 541 | if ((buf = malloc(needed)) == NULL) { |
984263bc MD |
542 | err(2, "malloc(%lu)", (unsigned long)needed); |
543 | } | |
b06a5e2a | 544 | if (sysctl(mib, miblen, buf, &needed, NULL, 0) < 0) { |
984263bc MD |
545 | err(1, "sysctl: net.route.0.0.dump"); |
546 | } | |
547 | lim = buf + needed; | |
548 | for (next = buf; next < lim; next += rtm->rtm_msglen) { | |
549 | rtm = (struct rt_msghdr *)next; | |
550 | np_rtentry(rtm); | |
551 | } | |
552 | } | |
553 | ||
554 | static void | |
555 | np_rtentry(struct rt_msghdr *rtm) | |
556 | { | |
6bd457ed | 557 | struct sockaddr *sa = (struct sockaddr *)(rtm + 1); |
984263bc MD |
558 | #ifdef notdef |
559 | static int masks_done, banner_printed; | |
560 | #endif | |
561 | static int old_af; | |
b82e1528 | 562 | int af1 = 0, interesting = RTF_UP | RTF_GATEWAY | RTF_HOST; |
984263bc MD |
563 | |
564 | #ifdef notdef | |
565 | /* for the moment, netmasks are skipped over */ | |
566 | if (!banner_printed) { | |
567 | printf("Netmasks:\n"); | |
568 | banner_printed = 1; | |
569 | } | |
570 | if (masks_done == 0) { | |
571 | if (rtm->rtm_addrs != RTA_DST ) { | |
572 | masks_done = 1; | |
b82e1528 | 573 | af1 = sa->sa_family; |
984263bc MD |
574 | } |
575 | } else | |
576 | #endif | |
b82e1528 SW |
577 | af1 = sa->sa_family; |
578 | if (af1 != old_af) { | |
579 | pr_family(af1); | |
580 | old_af = af1; | |
984263bc MD |
581 | } |
582 | if (rtm->rtm_addrs == RTA_DST) | |
583 | p_sockaddr(sa, NULL, 0, 36); | |
584 | else { | |
585 | p_sockaddr(sa, NULL, rtm->rtm_flags, 16); | |
4ff4d99f | 586 | sa = (struct sockaddr *)(RT_ROUNDUP(sa->sa_len) + (char *)sa); |
984263bc MD |
587 | p_sockaddr(sa, NULL, 0, 18); |
588 | } | |
589 | p_flags(rtm->rtm_flags & interesting, "%-6.6s "); | |
590 | putchar('\n'); | |
591 | } | |
592 | ||
593 | static void | |
594 | p_sockaddr(struct sockaddr *sa, struct sockaddr *mask, int flags, int width) | |
595 | { | |
596 | const char *cp; | |
597 | ||
598 | cp = fmt_sockaddr(sa, mask, flags); | |
599 | ||
600 | if (width < 0 ) | |
601 | printf("%s ", cp); | |
602 | else { | |
603 | if (numeric_addr) | |
604 | printf("%-*s ", width, cp); | |
605 | else | |
606 | printf("%-*.*s ", width, width, cp); | |
607 | } | |
608 | } | |
609 | ||
610 | static const char * | |
611 | fmt_sockaddr(struct sockaddr *sa, struct sockaddr *mask, int flags) | |
612 | { | |
613 | static char workbuf[128]; | |
b82e1528 | 614 | const char *cp = workbuf; |
984263bc | 615 | |
0e23ab10 SZ |
616 | if (sa == NULL) |
617 | return ("null"); | |
618 | ||
984263bc MD |
619 | switch(sa->sa_family) { |
620 | case AF_INET: | |
621 | { | |
6bd457ed | 622 | struct sockaddr_in *sin = (struct sockaddr_in *)sa; |
984263bc | 623 | |
04a3b05a AL |
624 | if ((sin->sin_addr.s_addr == INADDR_ANY) && mask && |
625 | ntohl(((struct sockaddr_in *)mask)->sin_addr.s_addr) ==0L) | |
626 | cp = "default" ; | |
984263bc MD |
627 | else if (flags & RTF_HOST) |
628 | cp = routename(sin->sin_addr.s_addr); | |
629 | else if (mask) | |
630 | cp = netname(sin->sin_addr.s_addr, | |
631 | ntohl(((struct sockaddr_in *)mask) | |
632 | ->sin_addr.s_addr)); | |
633 | else | |
634 | cp = netname(sin->sin_addr.s_addr, 0L); | |
635 | break; | |
636 | } | |
637 | ||
638 | #ifdef INET6 | |
639 | case AF_INET6: | |
640 | { | |
641 | struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)sa; | |
642 | struct in6_addr *in6 = &sa6->sin6_addr; | |
643 | ||
644 | /* | |
645 | * XXX: This is a special workaround for KAME kernels. | |
646 | * sin6_scope_id field of SA should be set in the future. | |
647 | */ | |
648 | if (IN6_IS_ADDR_LINKLOCAL(in6) || | |
649 | IN6_IS_ADDR_MC_LINKLOCAL(in6)) { | |
04a3b05a AL |
650 | /* XXX: override is ok? */ |
651 | sa6->sin6_scope_id = | |
652 | (u_int32_t)ntohs(*(u_short *)&in6->s6_addr[2]); | |
653 | *(u_short *)&in6->s6_addr[2] = 0; | |
984263bc MD |
654 | } |
655 | ||
656 | if (flags & RTF_HOST) | |
04a3b05a | 657 | cp = routename6(sa6); |
984263bc | 658 | else if (mask) |
04a3b05a AL |
659 | cp = netname6(sa6, |
660 | &((struct sockaddr_in6 *)mask)->sin6_addr); | |
661 | else | |
662 | cp = netname6(sa6, NULL); | |
984263bc MD |
663 | break; |
664 | } | |
665 | #endif /*INET6*/ | |
666 | ||
984263bc MD |
667 | case AF_NETGRAPH: |
668 | { | |
669 | printf("%s", ((struct sockaddr_ng *)sa)->sg_data); | |
670 | break; | |
671 | } | |
984263bc MD |
672 | case AF_LINK: |
673 | { | |
6bd457ed | 674 | struct sockaddr_dl *sdl = (struct sockaddr_dl *)sa; |
984263bc MD |
675 | |
676 | if (sdl->sdl_nlen == 0 && sdl->sdl_alen == 0 && | |
677 | sdl->sdl_slen == 0) | |
04a3b05a | 678 | sprintf(workbuf, "link#%d", sdl->sdl_index); |
984263bc MD |
679 | else |
680 | switch (sdl->sdl_type) { | |
681 | ||
682 | case IFT_ETHER: | |
44f8b4d2 | 683 | case IFT_L2VLAN: |
dc655811 | 684 | case IFT_CARP: |
984263bc MD |
685 | if (sdl->sdl_alen == ETHER_ADDR_LEN) { |
686 | cp = ether_ntoa((struct ether_addr *) | |
687 | (sdl->sdl_data + sdl->sdl_nlen)); | |
688 | break; | |
689 | } | |
690 | /* FALLTHROUGH */ | |
691 | default: | |
692 | cp = link_ntoa(sdl); | |
693 | break; | |
694 | } | |
695 | break; | |
696 | } | |
697 | ||
9b42cabe NA |
698 | case AF_MPLS: |
699 | { | |
700 | struct sockaddr_mpls *smpls = (struct sockaddr_mpls *)sa; | |
701 | ||
702 | (void) sprintf(workbuf, "%d", ntohl(smpls->smpls_label)); | |
703 | break; | |
704 | } | |
7d605468 | 705 | |
984263bc MD |
706 | default: |
707 | { | |
6bd457ed | 708 | u_char *s = (u_char *)sa->sa_data, *slim; |
b82e1528 | 709 | char *cq, *cqlim; |
984263bc | 710 | |
b82e1528 | 711 | cq = workbuf; |
984263bc | 712 | slim = sa->sa_len + (u_char *) sa; |
b82e1528 SW |
713 | cqlim = cq + sizeof(workbuf) - 6; |
714 | cq += sprintf(cq, "(%d)", sa->sa_family); | |
715 | while (s < slim && cq < cqlim) { | |
716 | cq += sprintf(cq, " %02x", *s++); | |
984263bc | 717 | if (s < slim) |
04a3b05a | 718 | cq += sprintf(cq, "%02x", *s++); |
984263bc MD |
719 | } |
720 | cp = workbuf; | |
721 | } | |
722 | } | |
723 | ||
724 | return (cp); | |
725 | } | |
726 | ||
727 | static void | |
b82e1528 | 728 | p_flags(int f, const char *format) |
984263bc MD |
729 | { |
730 | printf(format, fmt_flags(f)); | |
731 | } | |
732 | ||
733 | static const char * | |
734 | fmt_flags(int f) | |
735 | { | |
736 | static char name[33]; | |
737 | char *flags; | |
738 | struct bits *p = bits; | |
739 | ||
740 | for (flags = name; p->b_mask; p++) | |
741 | if (p->b_mask & f) | |
742 | *flags++ = p->b_val; | |
743 | *flags = '\0'; | |
744 | return (name); | |
745 | } | |
746 | ||
747 | static void | |
748 | p_rtentry(struct rtentry *rt) | |
749 | { | |
750 | static struct ifnet ifnet, *lastif; | |
751 | struct rtentry parent; | |
752 | static char buffer[128]; | |
753 | static char prettyname[128]; | |
754 | struct sockaddr *sa; | |
755 | sa_u addr, mask; | |
756 | ||
757 | /* | |
d5c02967 | 758 | * Don't print protocol-cloned routes unless -a or -A. |
984263bc | 759 | */ |
d5c02967 | 760 | if (rt->rt_flags & RTF_WASCLONED && !(aflag || Aflag)) { |
0e23ab10 SZ |
761 | if (kget(rt->rt_parent, parent) != 0) |
762 | return; | |
984263bc MD |
763 | if (parent.rt_flags & RTF_PRCLONING) |
764 | return; | |
765 | } | |
766 | ||
767 | bzero(&addr, sizeof(addr)); | |
768 | if ((sa = kgetsa(rt_key(rt)))) | |
769 | bcopy(sa, &addr, sa->sa_len); | |
770 | bzero(&mask, sizeof(mask)); | |
771 | if (rt_mask(rt) && (sa = kgetsa(rt_mask(rt)))) | |
772 | bcopy(sa, &mask, sa->sa_len); | |
773 | p_sockaddr(&addr.u_sa, &mask.u_sa, rt->rt_flags, wid_dst); | |
774 | p_sockaddr(kgetsa(rt->rt_gateway), NULL, RTF_HOST, wid_gw); | |
775 | snprintf(buffer, sizeof(buffer), "%%-%d.%ds ", wid_flags, wid_flags); | |
776 | p_flags(rt->rt_flags, buffer); | |
777 | if (addr.u_sa.sa_family == AF_INET || Wflag) { | |
778 | printf("%*ld %*lu ", wid_refs, rt->rt_refcnt, | |
779 | wid_use, rt->rt_use); | |
780 | if (Wflag) { | |
781 | if (rt->rt_rmx.rmx_mtu != 0) | |
782 | printf("%*lu ", wid_mtu, rt->rt_rmx.rmx_mtu); | |
783 | else | |
784 | printf("%*s ", wid_mtu, ""); | |
785 | } | |
786 | } | |
787 | if (rt->rt_ifp) { | |
788 | if (rt->rt_ifp != lastif) { | |
0e23ab10 SZ |
789 | if (kget(rt->rt_ifp, ifnet) == 0) |
790 | strlcpy(prettyname, ifnet.if_xname, | |
04a3b05a | 791 | sizeof(prettyname)); |
0e23ab10 SZ |
792 | else |
793 | strlcpy(prettyname, "---", sizeof(prettyname)); | |
984263bc | 794 | lastif = rt->rt_ifp; |
984263bc MD |
795 | } |
796 | printf("%*.*s", wid_if, wid_if, prettyname); | |
797 | if (rt->rt_rmx.rmx_expire) { | |
748810cc MD |
798 | struct timespec sp; |
799 | int expire_time; | |
800 | ||
801 | clock_gettime(CLOCK_MONOTONIC, &sp); | |
984263bc | 802 | |
748810cc MD |
803 | expire_time = (int)(rt->rt_rmx.rmx_expire - sp.tv_sec); |
804 | if (expire_time > 0) | |
984263bc | 805 | printf(" %*d", wid_expire, (int)expire_time); |
8ec89e60 SZ |
806 | else |
807 | printf("%*s ", wid_expire, ""); | |
9b42cabe NA |
808 | } else { |
809 | printf("%*s ", wid_expire, ""); | |
984263bc MD |
810 | } |
811 | if (rt->rt_nodes[0].rn_dupedkey) | |
812 | printf(" =>"); | |
813 | } | |
9b42cabe NA |
814 | if (Wflag) { |
815 | if (rt->rt_shim[0] != NULL) | |
816 | printf(" %-*s", wid_mplslops, labelops(rt)); | |
8ec89e60 SZ |
817 | else |
818 | printf(" %-*s", wid_mplslops, ""); | |
819 | if (rt->rt_rmx.rmx_msl != 0) | |
820 | printf(" %*lu", wid_msl, rt->rt_rmx.rmx_msl); | |
821 | else | |
bf522a91 SZ |
822 | printf("%*s ", wid_msl, ""); |
823 | if (rt->rt_rmx.rmx_iwmaxsegs != 0) | |
824 | printf(" %*lu", wid_iwmax, rt->rt_rmx.rmx_iwmaxsegs); | |
825 | else | |
826 | printf("%*s ", wid_iwmax, ""); | |
827 | if (rt->rt_rmx.rmx_iwcapsegs != 0) | |
828 | printf(" %*lu", wid_iw, rt->rt_rmx.rmx_iwcapsegs); | |
829 | else | |
830 | printf("%*s ", wid_iw, ""); | |
9b42cabe | 831 | } |
984263bc MD |
832 | putchar('\n'); |
833 | } | |
834 | ||
835 | char * | |
836 | routename(u_long in) | |
837 | { | |
6bd457ed | 838 | char *cp; |
984263bc MD |
839 | static char line[MAXHOSTNAMELEN]; |
840 | struct hostent *hp; | |
841 | ||
678e8cc6 | 842 | cp = NULL; |
984263bc | 843 | if (!numeric_addr) { |
15b85273 | 844 | hp = gethostbyaddr(&in, sizeof (struct in_addr), AF_INET); |
984263bc MD |
845 | if (hp) { |
846 | cp = hp->h_name; | |
847 | trimdomain(cp, strlen(cp)); | |
848 | } | |
849 | } | |
850 | if (cp) { | |
851 | strncpy(line, cp, sizeof(line) - 1); | |
852 | line[sizeof(line) - 1] = '\0'; | |
853 | } else { | |
854 | #define C(x) ((x) & 0xff) | |
855 | in = ntohl(in); | |
856 | sprintf(line, "%lu.%lu.%lu.%lu", | |
04a3b05a | 857 | C(in >> 24), C(in >> 16), C(in >> 8), C(in)); |
984263bc MD |
858 | } |
859 | return (line); | |
860 | } | |
861 | ||
862 | static u_long | |
863 | forgemask(u_long a) | |
864 | { | |
865 | u_long m; | |
866 | ||
867 | if (IN_CLASSA(a)) | |
868 | m = IN_CLASSA_NET; | |
869 | else if (IN_CLASSB(a)) | |
870 | m = IN_CLASSB_NET; | |
871 | else | |
872 | m = IN_CLASSC_NET; | |
873 | return (m); | |
874 | } | |
875 | ||
876 | static void | |
877 | domask(char *dst, u_long addr, u_long mask) | |
878 | { | |
04a3b05a | 879 | int b, bb, i; |
984263bc MD |
880 | |
881 | if (!mask || (forgemask(addr) == mask)) { | |
882 | *dst = '\0'; | |
883 | return; | |
884 | } | |
885 | i = 0; | |
886 | for (b = 0; b < 32; b++) | |
887 | if (mask & (1 << b)) { | |
984263bc MD |
888 | i = b; |
889 | for (bb = b+1; bb < 32; bb++) | |
890 | if (!(mask & (1 << bb))) { | |
891 | i = -1; /* noncontig */ | |
892 | break; | |
893 | } | |
894 | break; | |
895 | } | |
896 | if (i == -1) | |
897 | sprintf(dst, "&0x%lx", mask); | |
898 | else | |
899 | sprintf(dst, "/%d", 32-i); | |
900 | } | |
901 | ||
902 | /* | |
903 | * Return the name of the network whose address is given. | |
904 | * The address is assumed to be that of a net or subnet, not a host. | |
905 | */ | |
906 | char * | |
907 | netname(u_long in, u_long mask) | |
908 | { | |
678e8cc6 | 909 | char *cp = NULL; |
984263bc | 910 | static char line[MAXHOSTNAMELEN]; |
678e8cc6 | 911 | struct netent *np = NULL; |
984263bc | 912 | u_long dmask; |
6bd457ed | 913 | u_long i; |
984263bc MD |
914 | |
915 | #define NSHIFT(m) ( \ | |
916 | (m) == IN_CLASSA_NET ? IN_CLASSA_NSHIFT : \ | |
917 | (m) == IN_CLASSB_NET ? IN_CLASSB_NSHIFT : \ | |
918 | (m) == IN_CLASSC_NET ? IN_CLASSC_NSHIFT : \ | |
919 | 0) | |
920 | ||
921 | i = ntohl(in); | |
922 | dmask = forgemask(i); | |
923 | if (!numeric_addr && i) { | |
924 | np = getnetbyaddr(i >> NSHIFT(mask), AF_INET); | |
925 | if (np == NULL && mask == 0) | |
926 | np = getnetbyaddr(i >> NSHIFT(dmask), AF_INET); | |
927 | if (np != NULL) { | |
928 | cp = np->n_name; | |
929 | trimdomain(cp, strlen(cp)); | |
930 | } | |
931 | } | |
932 | #undef NSHIFT | |
933 | if (cp != NULL) { | |
934 | strncpy(line, cp, sizeof(line) - 1); | |
935 | line[sizeof(line) - 1] = '\0'; | |
936 | } else { | |
3d970e2c SZ |
937 | if (mask <= IN_CLASSA_NET && |
938 | (i & IN_CLASSA_HOST) == 0) { | |
984263bc | 939 | sprintf(line, "%lu", C(i >> 24)); |
3d970e2c SZ |
940 | } else if (mask <= IN_CLASSB_NET && |
941 | (i & IN_CLASSB_HOST) == 0) { | |
984263bc MD |
942 | sprintf(line, "%lu.%lu", |
943 | C(i >> 24), C(i >> 16)); | |
3d970e2c SZ |
944 | } else if (mask <= IN_CLASSC_NET && |
945 | (i & IN_CLASSC_HOST) == 0) { | |
984263bc MD |
946 | sprintf(line, "%lu.%lu.%lu", |
947 | C(i >> 24), C(i >> 16), C(i >> 8)); | |
3d970e2c | 948 | } else { |
984263bc MD |
949 | sprintf(line, "%lu.%lu.%lu.%lu", |
950 | C(i >> 24), C(i >> 16), C(i >> 8), C(i)); | |
984263bc MD |
951 | } |
952 | } | |
953 | domask(line + strlen(line), i, mask); | |
954 | return (line); | |
955 | } | |
956 | ||
957 | #ifdef INET6 | |
b82e1528 | 958 | const char * |
984263bc MD |
959 | netname6(struct sockaddr_in6 *sa6, struct in6_addr *mask) |
960 | { | |
961 | static char line[MAXHOSTNAMELEN]; | |
962 | u_char *p = (u_char *)mask; | |
963 | u_char *lim; | |
964 | int masklen, illegal = 0, flag = NI_WITHSCOPEID; | |
965 | ||
966 | if (mask) { | |
967 | for (masklen = 0, lim = p + 16; p < lim; p++) { | |
968 | switch (*p) { | |
04a3b05a AL |
969 | case 0xff: |
970 | masklen += 8; | |
971 | break; | |
972 | case 0xfe: | |
973 | masklen += 7; | |
974 | break; | |
975 | case 0xfc: | |
976 | masklen += 6; | |
977 | break; | |
978 | case 0xf8: | |
979 | masklen += 5; | |
980 | break; | |
981 | case 0xf0: | |
982 | masklen += 4; | |
983 | break; | |
984 | case 0xe0: | |
985 | masklen += 3; | |
986 | break; | |
987 | case 0xc0: | |
988 | masklen += 2; | |
989 | break; | |
990 | case 0x80: | |
991 | masklen += 1; | |
992 | break; | |
993 | case 0x00: | |
994 | break; | |
995 | default: | |
996 | illegal ++; | |
997 | break; | |
984263bc MD |
998 | } |
999 | } | |
1000 | if (illegal) | |
1001 | fprintf(stderr, "illegal prefixlen\n"); | |
1002 | } | |
1003 | else | |
1004 | masklen = 128; | |
1005 | ||
1006 | if (masklen == 0 && IN6_IS_ADDR_UNSPECIFIED(&sa6->sin6_addr)) | |
1007 | return("default"); | |
1008 | ||
1009 | if (numeric_addr) | |
1010 | flag |= NI_NUMERICHOST; | |
1011 | getnameinfo((struct sockaddr *)sa6, sa6->sin6_len, line, sizeof(line), | |
1012 | NULL, 0, flag); | |
1013 | ||
1014 | if (numeric_addr) | |
1015 | sprintf(&line[strlen(line)], "/%d", masklen); | |
1016 | ||
1017 | return line; | |
1018 | } | |
1019 | ||
1020 | char * | |
1021 | routename6(struct sockaddr_in6 *sa6) | |
1022 | { | |
1023 | static char line[MAXHOSTNAMELEN]; | |
1024 | int flag = NI_WITHSCOPEID; | |
1025 | /* use local variable for safety */ | |
b82e1528 | 1026 | struct sockaddr_in6 sa6_local; |
984263bc | 1027 | |
b82e1528 SW |
1028 | sa6_local.sin6_family = AF_INET6; |
1029 | sa6_local.sin6_len = sizeof(sa6_local); | |
984263bc MD |
1030 | sa6_local.sin6_addr = sa6->sin6_addr; |
1031 | sa6_local.sin6_scope_id = sa6->sin6_scope_id; | |
1032 | ||
1033 | if (numeric_addr) | |
1034 | flag |= NI_NUMERICHOST; | |
1035 | ||
1036 | getnameinfo((struct sockaddr *)&sa6_local, sa6_local.sin6_len, | |
1037 | line, sizeof(line), NULL, 0, flag); | |
1038 | ||
1039 | return line; | |
1040 | } | |
1041 | #endif /*INET6*/ | |
1042 | ||
1043 | /* | |
1044 | * Print routing statistics | |
1045 | */ | |
1046 | void | |
72ce0f6d | 1047 | rt_stats(void) |
984263bc | 1048 | { |
72ce0f6d HP |
1049 | struct rtstatistics rts; |
1050 | int error = 0; | |
984263bc | 1051 | |
72ce0f6d HP |
1052 | error = kinfo_get_net_rtstatistics(&rts); |
1053 | if (error) { | |
1054 | printf("routing: could not retrieve statistics\n"); | |
984263bc MD |
1055 | return; |
1056 | } | |
984263bc MD |
1057 | printf("routing:\n"); |
1058 | ||
72ce0f6d HP |
1059 | #define p(f, m) if (rts.f || sflag <= 1) \ |
1060 | printf(m, rts.f, plural(rts.f)) | |
984263bc | 1061 | |
72ce0f6d HP |
1062 | p(rts_badredirect, "\t%lu bad routing redirect%s\n"); |
1063 | p(rts_dynamic, "\t%lu dynamically created route%s\n"); | |
1064 | p(rts_newgateway, "\t%lu new gateway%s due to redirects\n"); | |
1065 | p(rts_unreach, "\t%lu destination%s found unreachable\n"); | |
1066 | p(rts_wildcard, "\t%lu use%s of a wildcard route\n"); | |
984263bc | 1067 | #undef p |
984263bc MD |
1068 | } |
1069 | ||
9b42cabe NA |
1070 | static const char * |
1071 | labelops(struct rtentry *rt) | |
1072 | { | |
b82e1528 | 1073 | const char *lops[] = { "push", "pop", "swap", "pop all" }; |
9b42cabe NA |
1074 | static char buffer[100]; |
1075 | char *cp = buffer; | |
1076 | struct sockaddr_mpls *smpls; | |
1077 | int i; | |
1078 | ||
1079 | for (i=0; i<MPLS_MAXLOPS; ++i) { | |
1080 | ||
1081 | if (rt->rt_shim[i] == NULL) | |
1082 | break; | |
1083 | if (i>0) { | |
1084 | cp += snprintf(cp, | |
1085 | sizeof(buffer) - (cp - buffer), | |
1086 | ", "); | |
1087 | } | |
1088 | smpls = (struct sockaddr_mpls *)kgetsa(rt->rt_shim[i]); | |
1089 | if (smpls->smpls_op != MPLSLOP_POP && | |
1090 | smpls->smpls_op != MPLSLOP_POPALL){ | |
1091 | cp += snprintf(cp, | |
1092 | sizeof(buffer) - (cp - buffer), | |
1093 | "%s %d", | |
1094 | lops[smpls->smpls_op - 1], | |
1095 | ntohl(smpls->smpls_label)); | |
1096 | } else { | |
1097 | cp += snprintf(cp, | |
1098 | sizeof(buffer) - (cp - buffer), | |
1099 | "%s", | |
1100 | lops[smpls->smpls_op - 1]); | |
1101 | } | |
1102 | } | |
1103 | ||
1104 | return (buffer); | |
1105 | } |