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