]> git.saurik.com Git - apple/network_cmds.git/blame - netstat.tproj/inet6.c
network_cmds-396.6.tar.gz
[apple/network_cmds.git] / netstat.tproj / inet6.c
CommitLineData
9c859447 1/*
fdfd5971 2 * Copyright (c) 2008-2010 Apple Inc. All rights reserved.
9c859447
A
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
b8dff150 28
7ba0088d
A
29/* BSDI inet.c,v 2.3 1995/10/24 02:19:29 prb Exp */
30/*
31 * Copyright (c) 1983, 1988, 1993
32 * The Regents of the University of California. All rights reserved.
33 *
34 * Redistribution and use in source and binary forms, with or without
35 * modification, are permitted provided that the following conditions
36 * are met:
37 * 1. Redistributions of source code must retain the above copyright
38 * notice, this list of conditions and the following disclaimer.
39 * 2. Redistributions in binary form must reproduce the above copyright
40 * notice, this list of conditions and the following disclaimer in the
41 * documentation and/or other materials provided with the distribution.
42 * 3. All advertising materials mentioning features or use of this software
43 * must display the following acknowledgement:
44 * This product includes software developed by the University of
45 * California, Berkeley and its contributors.
46 * 4. Neither the name of the University nor the names of its contributors
47 * may be used to endorse or promote products derived from this software
48 * without specific prior written permission.
49 *
50 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
51 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
52 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
53 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
54 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
55 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
56 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
57 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
58 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
59 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
60 * SUCH DAMAGE.
61 *
62 * $FreeBSD: src/usr.bin/netstat/inet6.c,v 1.3.2.9 2001/08/10 09:07:09 ru Exp $
63 */
64
65#ifndef lint
66/*
67static char sccsid[] = "@(#)inet6.c 8.4 (Berkeley) 4/20/94";
68*/
69#endif /* not lint */
70
71#ifdef INET6
72#include <sys/param.h>
73#include <sys/socket.h>
74#include <sys/socketvar.h>
75#include <sys/ioctl.h>
7ba0088d
A
76#include <sys/sysctl.h>
77
78#include <net/route.h>
79#include <net/if.h>
80#include <net/if_var.h>
81#include <netinet/in.h>
82#include <netinet/ip6.h>
83#include <netinet/icmp6.h>
84#include <netinet/in_systm.h>
85#include <netinet6/in6_pcb.h>
86#include <netinet6/in6_var.h>
87#include <netinet6/ip6_var.h>
88#include <netinet6/pim6_var.h>
89#include <netinet6/raw_ip6.h>
90
91#include <arpa/inet.h>
92#include <netdb.h>
93
94#include <stdio.h>
95#include <string.h>
96#include <unistd.h>
97#include "netstat.h"
98
b8dff150 99#if defined(__APPLE__) && !defined(__unused)
7ba0088d
A
100#define __unused
101#endif
7ba0088d
A
102
103char *inet6name (struct in6_addr *);
104void inet6print (struct in6_addr *, int, char *, int);
105
7ba0088d
A
106static char *ip6nh[] = {
107 "hop by hop",
108 "ICMP",
109 "IGMP",
110 "#3",
111 "IP",
112 "#5",
113 "TCP",
114 "#7",
115 "#8",
116 "#9",
117 "#10",
118 "#11",
119 "#12",
120 "#13",
121 "#14",
122 "#15",
123 "#16",
124 "UDP",
125 "#18",
126 "#19",
127 "#20",
128 "#21",
129 "IDP",
130 "#23",
131 "#24",
132 "#25",
133 "#26",
134 "#27",
135 "#28",
136 "TP",
137 "#30",
138 "#31",
139 "#32",
140 "#33",
141 "#34",
142 "#35",
143 "#36",
144 "#37",
145 "#38",
146 "#39",
147 "#40",
148 "IP6",
149 "#42",
150 "routing",
151 "fragment",
152 "#45",
153 "#46",
154 "#47",
155 "#48",
156 "#49",
157 "ESP",
158 "AH",
159 "#52",
160 "#53",
161 "#54",
162 "#55",
163 "#56",
164 "#57",
165 "ICMP6",
166 "no next header",
167 "destination option",
168 "#61",
fdfd5971 169 "mobility",
7ba0088d
A
170 "#63",
171 "#64",
172 "#65",
173 "#66",
174 "#67",
175 "#68",
176 "#69",
177 "#70",
178 "#71",
179 "#72",
180 "#73",
181 "#74",
182 "#75",
183 "#76",
184 "#77",
185 "#78",
186 "#79",
187 "ISOIP",
188 "#81",
189 "#82",
190 "#83",
191 "#84",
192 "#85",
193 "#86",
194 "#87",
195 "#88",
196 "OSPF",
197 "#80",
198 "#91",
199 "#92",
200 "#93",
201 "#94",
202 "#95",
203 "#96",
204 "Ethernet",
205 "#98",
206 "#99",
207 "#100",
208 "#101",
209 "#102",
210 "PIM",
211 "#104",
212 "#105",
213 "#106",
214 "#107",
215 "#108",
216 "#109",
217 "#110",
218 "#111",
219 "#112",
220 "#113",
221 "#114",
222 "#115",
223 "#116",
224 "#117",
225 "#118",
226 "#119",
227 "#120",
228 "#121",
229 "#122",
230 "#123",
231 "#124",
232 "#125",
233 "#126",
234 "#127",
235 "#128",
236 "#129",
237 "#130",
238 "#131",
239 "#132",
240 "#133",
241 "#134",
242 "#135",
243 "#136",
244 "#137",
245 "#138",
246 "#139",
247 "#140",
248 "#141",
249 "#142",
250 "#143",
251 "#144",
252 "#145",
253 "#146",
254 "#147",
255 "#148",
256 "#149",
257 "#150",
258 "#151",
259 "#152",
260 "#153",
261 "#154",
262 "#155",
263 "#156",
264 "#157",
265 "#158",
266 "#159",
267 "#160",
268 "#161",
269 "#162",
270 "#163",
271 "#164",
272 "#165",
273 "#166",
274 "#167",
275 "#168",
276 "#169",
277 "#170",
278 "#171",
279 "#172",
280 "#173",
281 "#174",
282 "#175",
283 "#176",
284 "#177",
285 "#178",
286 "#179",
287 "#180",
288 "#181",
289 "#182",
290 "#183",
291 "#184",
292 "#185",
293 "#186",
294 "#187",
295 "#188",
296 "#189",
297 "#180",
298 "#191",
299 "#192",
300 "#193",
301 "#194",
302 "#195",
303 "#196",
304 "#197",
305 "#198",
306 "#199",
307 "#200",
308 "#201",
309 "#202",
310 "#203",
311 "#204",
312 "#205",
313 "#206",
314 "#207",
315 "#208",
316 "#209",
317 "#210",
318 "#211",
319 "#212",
320 "#213",
321 "#214",
322 "#215",
323 "#216",
324 "#217",
325 "#218",
326 "#219",
327 "#220",
328 "#221",
329 "#222",
330 "#223",
331 "#224",
332 "#225",
333 "#226",
334 "#227",
335 "#228",
336 "#229",
337 "#230",
338 "#231",
339 "#232",
340 "#233",
341 "#234",
342 "#235",
343 "#236",
344 "#237",
345 "#238",
346 "#239",
347 "#240",
348 "#241",
349 "#242",
350 "#243",
351 "#244",
352 "#245",
353 "#246",
354 "#247",
355 "#248",
356 "#249",
357 "#250",
358 "#251",
359 "#252",
360 "#253",
361 "#254",
362 "#255",
363};
364
365/*
366 * Dump IP6 statistics structure.
367 */
368void
9c859447 369ip6_stats(uint32_t off __unused, char *name, int af __unused)
7ba0088d 370{
b8dff150 371 static struct ip6stat pip6stat;
7ba0088d
A
372 struct ip6stat ip6stat;
373 int first, i;
374 int mib[4];
375 size_t len;
376
377 mib[0] = CTL_NET;
378 mib[1] = PF_INET6;
379 mib[2] = IPPROTO_IPV6;
380 mib[3] = IPV6CTL_STATS;
381
382 len = sizeof ip6stat;
383 memset(&ip6stat, 0, len);
384 if (sysctl(mib, 4, &ip6stat, &len, (void *)0, 0) < 0)
385 return;
386 printf("%s:\n", name);
387
b8dff150
A
388#define IP6DIFF(f) (ip6stat.f - pip6stat.f)
389#define p(f, m) if (IP6DIFF(f) || sflag <= 1) \
390 printf(m, (unsigned long long)IP6DIFF(f), plural(IP6DIFF(f)))
391#define p1a(f, m) if (IP6DIFF(f) || sflag <= 1) \
392 printf(m, (unsigned long long)IP6DIFF(f))
7ba0088d
A
393
394 p(ip6s_total, "\t%llu total packet%s received\n");
395 p1a(ip6s_toosmall, "\t%llu with size smaller than minimum\n");
396 p1a(ip6s_tooshort, "\t%llu with data size < data length\n");
397 p1a(ip6s_badoptions, "\t%llu with bad options\n");
398 p1a(ip6s_badvers, "\t%llu with incorrect version number\n");
399 p(ip6s_fragments, "\t%llu fragment%s received\n");
400 p(ip6s_fragdropped, "\t%llu fragment%s dropped (dup or out of space)\n");
401 p(ip6s_fragtimeout, "\t%llu fragment%s dropped after timeout\n");
402 p(ip6s_fragoverflow, "\t%llu fragment%s that exceeded limit\n");
403 p(ip6s_reassembled, "\t%llu packet%s reassembled ok\n");
404 p(ip6s_delivered, "\t%llu packet%s for this host\n");
405 p(ip6s_forward, "\t%llu packet%s forwarded\n");
406 p(ip6s_cantforward, "\t%llu packet%s not forwardable\n");
407 p(ip6s_redirectsent, "\t%llu redirect%s sent\n");
408 p(ip6s_localout, "\t%llu packet%s sent from this host\n");
409 p(ip6s_rawout, "\t%llu packet%s sent with fabricated ip header\n");
410 p(ip6s_odropped, "\t%llu output packet%s dropped due to no bufs, etc.\n");
411 p(ip6s_noroute, "\t%llu output packet%s discarded due to no route\n");
412 p(ip6s_fragmented, "\t%llu output datagram%s fragmented\n");
413 p(ip6s_ofragments, "\t%llu fragment%s created\n");
414 p(ip6s_cantfrag, "\t%llu datagram%s that can't be fragmented\n");
415 p(ip6s_badscope, "\t%llu packet%s that violated scope rules\n");
416 p(ip6s_notmember, "\t%llu multicast packet%s which we don't join\n");
417 for (first = 1, i = 0; i < 256; i++)
b8dff150 418 if (IP6DIFF(ip6s_nxthist[i]) != 0) {
7ba0088d
A
419 if (first) {
420 printf("\tInput histogram:\n");
421 first = 0;
422 }
423 printf("\t\t%s: %llu\n", ip6nh[i],
b8dff150 424 (unsigned long long)IP6DIFF(ip6s_nxthist[i]));
7ba0088d
A
425 }
426 printf("\tMbuf statistics:\n");
b8dff150 427 printf("\t\t%llu one mbuf\n", (unsigned long long)IP6DIFF(ip6s_m1));
7ba0088d
A
428 for (first = 1, i = 0; i < 32; i++) {
429 char ifbuf[IFNAMSIZ];
b8dff150 430 if (IP6DIFF(ip6s_m2m[i]) != 0) {
7ba0088d
A
431 if (first) {
432 printf("\t\ttwo or more mbuf:\n");
433 first = 0;
434 }
435 printf("\t\t\t%s= %llu\n",
436 if_indextoname(i, ifbuf),
b8dff150 437 (unsigned long long)IP6DIFF(ip6s_m2m[i]));
7ba0088d
A
438 }
439 }
440 printf("\t\t%llu one ext mbuf\n",
b8dff150 441 (unsigned long long)IP6DIFF(ip6s_mext1));
7ba0088d 442 printf("\t\t%llu two or more ext mbuf\n",
b8dff150 443 (unsigned long long)IP6DIFF(ip6s_mext2m));
7ba0088d
A
444 p(ip6s_exthdrtoolong,
445 "\t%llu packet%s whose headers are not continuous\n");
446 p(ip6s_nogif, "\t%llu tunneling packet%s that can't find gif\n");
447 p(ip6s_toomanyhdr,
448 "\t%llu packet%s discarded due to too may headers\n");
449
450 /* for debugging source address selection */
451#define PRINT_SCOPESTAT(s,i) do {\
452 switch(i) { /* XXX hardcoding in each case */\
453 case 1:\
454 p(s, "\t\t%llu node-local%s\n");\
455 break;\
456 case 2:\
457 p(s,"\t\t%llu link-local%s\n");\
458 break;\
459 case 5:\
460 p(s,"\t\t%llu site-local%s\n");\
461 break;\
462 case 14:\
463 p(s,"\t\t%llu global%s\n");\
464 break;\
465 default:\
466 printf("\t\t%llu addresses scope=%x\n",\
b8dff150 467 (unsigned long long)IP6DIFF(s), i);\
7ba0088d
A
468 }\
469 } while (0);
470
471 p(ip6s_sources_none,
472 "\t%llu failure%s of source address selection\n");
473 for (first = 1, i = 0; i < 16; i++) {
b8dff150 474 if (IP6DIFF(ip6s_sources_sameif[i])) {
7ba0088d
A
475 if (first) {
476 printf("\tsource addresses on an outgoing I/F\n");
477 first = 0;
478 }
479 PRINT_SCOPESTAT(ip6s_sources_sameif[i], i);
480 }
481 }
482 for (first = 1, i = 0; i < 16; i++) {
b8dff150 483 if (IP6DIFF(ip6s_sources_otherif[i])) {
7ba0088d
A
484 if (first) {
485 printf("\tsource addresses on a non-outgoing I/F\n");
486 first = 0;
487 }
488 PRINT_SCOPESTAT(ip6s_sources_otherif[i], i);
489 }
490 }
491 for (first = 1, i = 0; i < 16; i++) {
b8dff150 492 if (IP6DIFF(ip6s_sources_samescope[i])) {
7ba0088d
A
493 if (first) {
494 printf("\tsource addresses of same scope\n");
495 first = 0;
496 }
497 PRINT_SCOPESTAT(ip6s_sources_samescope[i], i);
498 }
499 }
500 for (first = 1, i = 0; i < 16; i++) {
b8dff150 501 if (IP6DIFF(ip6s_sources_otherscope[i])) {
7ba0088d
A
502 if (first) {
503 printf("\tsource addresses of a different scope\n");
504 first = 0;
505 }
506 PRINT_SCOPESTAT(ip6s_sources_otherscope[i], i);
507 }
508 }
509 for (first = 1, i = 0; i < 16; i++) {
b8dff150 510 if (IP6DIFF(ip6s_sources_deprecated[i])) {
7ba0088d
A
511 if (first) {
512 printf("\tdeprecated source addresses\n");
513 first = 0;
514 }
515 PRINT_SCOPESTAT(ip6s_sources_deprecated[i], i);
516 }
517 }
518
519 p1a(ip6s_forward_cachehit, "\t%llu forward cache hit\n");
520 p1a(ip6s_forward_cachemiss, "\t%llu forward cache miss\n");
fdfd5971
A
521 p(ip6s_pktdropcntrl, "\t%llu packet%s dropped due to no bufs for control data\n");
522
b8dff150
A
523 if (interval > 0)
524 bcopy(&ip6stat, &pip6stat, len);
525
526#undef IP6DIFF
7ba0088d
A
527#undef p
528#undef p1a
529}
530
531/*
532 * Dump IPv6 per-interface statistics based on RFC 2465.
533 */
534void
535ip6_ifstats(char *ifname)
536{
537 struct in6_ifreq ifr;
538 int s;
539#define p(f, m) if (ifr.ifr_ifru.ifru_stat.f || sflag <= 1) \
540 printf(m, (unsigned long long)ifr.ifr_ifru.ifru_stat.f, plural(ifr.ifr_ifru.ifru_stat.f))
541#define p_5(f, m) if (ifr.ifr_ifru.ifru_stat.f || sflag <= 1) \
542 printf(m, (unsigned long long)ip6stat.f)
543
544 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
545 perror("Warning: socket(AF_INET6)");
546 return;
547 }
548
fdfd5971 549 strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
7ba0088d
A
550 printf("ip6 on %s:\n", ifr.ifr_name);
551
552 if (ioctl(s, SIOCGIFSTAT_IN6, (char *)&ifr) < 0) {
553 perror("Warning: ioctl(SIOCGIFSTAT_IN6)");
554 goto end;
555 }
556
557 p(ifs6_in_receive, "\t%llu total input datagram%s\n");
558 p(ifs6_in_hdrerr, "\t%llu datagram%s with invalid header received\n");
559 p(ifs6_in_toobig, "\t%llu datagram%s exceeded MTU received\n");
560 p(ifs6_in_noroute, "\t%llu datagram%s with no route received\n");
561 p(ifs6_in_addrerr, "\t%llu datagram%s with invalid dst received\n");
562 p(ifs6_in_protounknown, "\t%llu datagram%s with unknown proto received\n");
563 p(ifs6_in_truncated, "\t%llu truncated datagram%s received\n");
564 p(ifs6_in_discard, "\t%llu input datagram%s discarded\n");
565 p(ifs6_in_deliver,
566 "\t%llu datagram%s delivered to an upper layer protocol\n");
567 p(ifs6_out_forward, "\t%llu datagram%s forwarded to this interface\n");
568 p(ifs6_out_request,
569 "\t%llu datagram%s sent from an upper layer protocol\n");
570 p(ifs6_out_discard, "\t%llu total discarded output datagram%s\n");
571 p(ifs6_out_fragok, "\t%llu output datagram%s fragmented\n");
572 p(ifs6_out_fragfail, "\t%llu output datagram%s failed on fragment\n");
573 p(ifs6_out_fragcreat, "\t%llu output datagram%s succeeded on fragment\n");
574 p(ifs6_reass_reqd, "\t%llu incoming datagram%s fragmented\n");
575 p(ifs6_reass_ok, "\t%llu datagram%s reassembled\n");
576 p(ifs6_reass_fail, "\t%llu datagram%s failed on reassembling\n");
577 p(ifs6_in_mcast, "\t%llu multicast datagram%s received\n");
578 p(ifs6_out_mcast, "\t%llu multicast datagram%s sent\n");
579
580 end:
581 close(s);
582
583#undef p
584#undef p_5
585}
586
587static char *icmp6names[] = {
588 "#0",
589 "unreach",
590 "packet too big",
591 "time exceed",
592 "parameter problem",
593 "#5",
594 "#6",
595 "#7",
596 "#8",
597 "#9",
598 "#10",
599 "#11",
600 "#12",
601 "#13",
602 "#14",
603 "#15",
604 "#16",
605 "#17",
606 "#18",
607 "#19",
608 "#20",
609 "#21",
610 "#22",
611 "#23",
612 "#24",
613 "#25",
614 "#26",
615 "#27",
616 "#28",
617 "#29",
618 "#30",
619 "#31",
620 "#32",
621 "#33",
622 "#34",
623 "#35",
624 "#36",
625 "#37",
626 "#38",
627 "#39",
628 "#40",
629 "#41",
630 "#42",
631 "#43",
632 "#44",
633 "#45",
634 "#46",
635 "#47",
636 "#48",
637 "#49",
638 "#50",
639 "#51",
640 "#52",
641 "#53",
642 "#54",
643 "#55",
644 "#56",
645 "#57",
646 "#58",
647 "#59",
648 "#60",
649 "#61",
650 "#62",
651 "#63",
652 "#64",
653 "#65",
654 "#66",
655 "#67",
656 "#68",
657 "#69",
658 "#70",
659 "#71",
660 "#72",
661 "#73",
662 "#74",
663 "#75",
664 "#76",
665 "#77",
666 "#78",
667 "#79",
668 "#80",
669 "#81",
670 "#82",
671 "#83",
672 "#84",
673 "#85",
674 "#86",
675 "#87",
676 "#88",
677 "#89",
678 "#80",
679 "#91",
680 "#92",
681 "#93",
682 "#94",
683 "#95",
684 "#96",
685 "#97",
686 "#98",
687 "#99",
688 "#100",
689 "#101",
690 "#102",
691 "#103",
692 "#104",
693 "#105",
694 "#106",
695 "#107",
696 "#108",
697 "#109",
698 "#110",
699 "#111",
700 "#112",
701 "#113",
702 "#114",
703 "#115",
704 "#116",
705 "#117",
706 "#118",
707 "#119",
708 "#120",
709 "#121",
710 "#122",
711 "#123",
712 "#124",
713 "#125",
714 "#126",
715 "#127",
716 "echo",
717 "echo reply",
718 "multicast listener query",
fdfd5971
A
719 "MLDv1 listener report",
720 "MLDv1 listener done",
7ba0088d
A
721 "router solicitation",
722 "router advertisement",
723 "neighbor solicitation",
724 "neighbor advertisement",
725 "redirect",
726 "router renumbering",
727 "node information request",
728 "node information reply",
729 "inverse neighbor solicitation",
730 "inverse neighbor advertisement",
fdfd5971 731 "MLDv2 listener report",
7ba0088d
A
732 "#144",
733 "#145",
734 "#146",
735 "#147",
736 "#148",
737 "#149",
738 "#150",
739 "#151",
740 "#152",
741 "#153",
742 "#154",
743 "#155",
744 "#156",
745 "#157",
746 "#158",
747 "#159",
748 "#160",
749 "#161",
750 "#162",
751 "#163",
752 "#164",
753 "#165",
754 "#166",
755 "#167",
756 "#168",
757 "#169",
758 "#170",
759 "#171",
760 "#172",
761 "#173",
762 "#174",
763 "#175",
764 "#176",
765 "#177",
766 "#178",
767 "#179",
768 "#180",
769 "#181",
770 "#182",
771 "#183",
772 "#184",
773 "#185",
774 "#186",
775 "#187",
776 "#188",
777 "#189",
778 "#180",
779 "#191",
780 "#192",
781 "#193",
782 "#194",
783 "#195",
784 "#196",
785 "#197",
786 "#198",
787 "#199",
788 "#200",
789 "#201",
790 "#202",
791 "#203",
792 "#204",
793 "#205",
794 "#206",
795 "#207",
796 "#208",
797 "#209",
798 "#210",
799 "#211",
800 "#212",
801 "#213",
802 "#214",
803 "#215",
804 "#216",
805 "#217",
806 "#218",
807 "#219",
808 "#220",
809 "#221",
810 "#222",
811 "#223",
812 "#224",
813 "#225",
814 "#226",
815 "#227",
816 "#228",
817 "#229",
818 "#230",
819 "#231",
820 "#232",
821 "#233",
822 "#234",
823 "#235",
824 "#236",
825 "#237",
826 "#238",
827 "#239",
828 "#240",
829 "#241",
830 "#242",
831 "#243",
832 "#244",
833 "#245",
834 "#246",
835 "#247",
836 "#248",
837 "#249",
838 "#250",
839 "#251",
840 "#252",
841 "#253",
842 "#254",
843 "#255",
844};
845
846/*
847 * Dump ICMP6 statistics.
848 */
849void
9c859447 850icmp6_stats(uint32_t off __unused, char *name, int af __unused)
7ba0088d 851{
b8dff150 852 static struct icmp6stat picmp6stat;
7ba0088d
A
853 struct icmp6stat icmp6stat;
854 register int i, first;
855 int mib[4];
856 size_t len;
857
858 mib[0] = CTL_NET;
859 mib[1] = PF_INET6;
860 mib[2] = IPPROTO_ICMPV6;
861 mib[3] = ICMPV6CTL_STATS;
862
863 len = sizeof icmp6stat;
864 memset(&icmp6stat, 0, len);
865 if (sysctl(mib, 4, &icmp6stat, &len, (void *)0, 0) < 0)
866 return;
867 printf("%s:\n", name);
868
b8dff150
A
869#define ICMP6DIFF(f) (icmp6stat.f - picmp6stat.f)
870#define p(f, m) if (ICMP6DIFF(f) || sflag <= 1) \
871 printf(m, (unsigned long long)ICMP6DIFF(f), plural(ICMP6DIFF(f)))
872#define p_5(f, m) printf(m, (unsigned long long)ICMP6DIFF(f))
7ba0088d
A
873
874 p(icp6s_error, "\t%llu call%s to icmp_error\n");
875 p(icp6s_canterror,
876 "\t%llu error%s not generated because old message was icmp error or so\n");
877 p(icp6s_toofreq,
878 "\t%llu error%s not generated because rate limitation\n");
879#define NELEM (sizeof(icmp6stat.icp6s_outhist)/sizeof(icmp6stat.icp6s_outhist[0]))
880 for (first = 1, i = 0; i < NELEM; i++)
b8dff150 881 if (ICMP6DIFF(icp6s_outhist[i]) != 0) {
7ba0088d
A
882 if (first) {
883 printf("\tOutput histogram:\n");
884 first = 0;
885 }
886 printf("\t\t%s: %llu\n", icmp6names[i],
b8dff150 887 (unsigned long long)ICMP6DIFF(icp6s_outhist[i]));
7ba0088d
A
888 }
889#undef NELEM
890 p(icp6s_badcode, "\t%llu message%s with bad code fields\n");
891 p(icp6s_tooshort, "\t%llu message%s < minimum length\n");
892 p(icp6s_checksum, "\t%llu bad checksum%s\n");
893 p(icp6s_badlen, "\t%llu message%s with bad length\n");
894#define NELEM (sizeof(icmp6stat.icp6s_inhist)/sizeof(icmp6stat.icp6s_inhist[0]))
895 for (first = 1, i = 0; i < NELEM; i++)
b8dff150 896 if (ICMP6DIFF(icp6s_inhist[i]) != 0) {
7ba0088d
A
897 if (first) {
898 printf("\tInput histogram:\n");
899 first = 0;
900 }
901 printf("\t\t%s: %llu\n", icmp6names[i],
b8dff150 902 (unsigned long long)ICMP6DIFF(icp6s_inhist[i]));
7ba0088d
A
903 }
904#undef NELEM
905 printf("\tHistogram of error messages to be generated:\n");
906 p_5(icp6s_odst_unreach_noroute, "\t\t%llu no route\n");
907 p_5(icp6s_odst_unreach_admin, "\t\t%llu administratively prohibited\n");
908 p_5(icp6s_odst_unreach_beyondscope, "\t\t%llu beyond scope\n");
909 p_5(icp6s_odst_unreach_addr, "\t\t%llu address unreachable\n");
910 p_5(icp6s_odst_unreach_noport, "\t\t%llu port unreachable\n");
911 p_5(icp6s_opacket_too_big, "\t\t%llu packet too big\n");
912 p_5(icp6s_otime_exceed_transit, "\t\t%llu time exceed transit\n");
913 p_5(icp6s_otime_exceed_reassembly, "\t\t%llu time exceed reassembly\n");
914 p_5(icp6s_oparamprob_header, "\t\t%llu erroneous header field\n");
915 p_5(icp6s_oparamprob_nextheader, "\t\t%llu unrecognized next header\n");
916 p_5(icp6s_oparamprob_option, "\t\t%llu unrecognized option\n");
917 p_5(icp6s_oredirect, "\t\t%llu redirect\n");
918 p_5(icp6s_ounknown, "\t\t%llu unknown\n");
919
920 p(icp6s_reflect, "\t%llu message response%s generated\n");
921 p(icp6s_nd_toomanyopt, "\t%llu message%s with too many ND options\n");
922 p(icp6s_nd_badopt, "\t%qu message%s with bad ND options\n");
923 p(icp6s_badns, "\t%qu bad neighbor solicitation message%s\n");
924 p(icp6s_badna, "\t%qu bad neighbor advertisement message%s\n");
925 p(icp6s_badrs, "\t%qu bad router solicitation message%s\n");
926 p(icp6s_badra, "\t%qu bad router advertisement message%s\n");
927 p(icp6s_badredirect, "\t%qu bad redirect message%s\n");
928 p(icp6s_pmtuchg, "\t%llu path MTU change%s\n");
b8dff150
A
929
930 if (interval > 0)
931 bcopy(&icmp6stat, &picmp6stat, len);
932
933#undef ICMP6DIFF
7ba0088d
A
934#undef p
935#undef p_5
936}
937
938/*
939 * Dump ICMPv6 per-interface statistics based on RFC 2466.
940 */
941void
942icmp6_ifstats(char *ifname)
943{
944 struct in6_ifreq ifr;
945 int s;
946#define p(f, m) if (ifr.ifr_ifru.ifru_icmp6stat.f || sflag <= 1) \
947 printf(m, (unsigned long long)ifr.ifr_ifru.ifru_icmp6stat.f, plural(ifr.ifr_ifru.ifru_icmp6stat.f))
948
949 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
950 perror("Warning: socket(AF_INET6)");
951 return;
952 }
953
fdfd5971 954 strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
7ba0088d
A
955 printf("icmp6 on %s:\n", ifr.ifr_name);
956
957 if (ioctl(s, SIOCGIFSTAT_ICMP6, (char *)&ifr) < 0) {
958 perror("Warning: ioctl(SIOCGIFSTAT_ICMP6)");
959 goto end;
960 }
961
962 p(ifs6_in_msg, "\t%llu total input message%s\n");
963 p(ifs6_in_error, "\t%llu total input error message%s\n");
964 p(ifs6_in_dstunreach, "\t%llu input destination unreachable error%s\n");
965 p(ifs6_in_adminprohib, "\t%llu input administratively prohibited error%s\n");
966 p(ifs6_in_timeexceed, "\t%llu input time exceeded error%s\n");
967 p(ifs6_in_paramprob, "\t%llu input parameter problem error%s\n");
968 p(ifs6_in_pkttoobig, "\t%llu input packet too big error%s\n");
969 p(ifs6_in_echo, "\t%llu input echo request%s\n");
970 p(ifs6_in_echoreply, "\t%llu input echo reply%s\n");
971 p(ifs6_in_routersolicit, "\t%llu input router solicitation%s\n");
972 p(ifs6_in_routeradvert, "\t%llu input router advertisement%s\n");
973 p(ifs6_in_neighborsolicit, "\t%llu input neighbor solicitation%s\n");
974 p(ifs6_in_neighboradvert, "\t%llu input neighbor advertisement%s\n");
975 p(ifs6_in_redirect, "\t%llu input redirect%s\n");
976 p(ifs6_in_mldquery, "\t%llu input MLD query%s\n");
977 p(ifs6_in_mldreport, "\t%llu input MLD report%s\n");
978 p(ifs6_in_mlddone, "\t%llu input MLD done%s\n");
979
980 p(ifs6_out_msg, "\t%llu total output message%s\n");
981 p(ifs6_out_error, "\t%llu total output error message%s\n");
982 p(ifs6_out_dstunreach, "\t%llu output destination unreachable error%s\n");
983 p(ifs6_out_adminprohib, "\t%llu output administratively prohibited error%s\n");
984 p(ifs6_out_timeexceed, "\t%llu output time exceeded error%s\n");
985 p(ifs6_out_paramprob, "\t%llu output parameter problem error%s\n");
986 p(ifs6_out_pkttoobig, "\t%llu output packet too big error%s\n");
987 p(ifs6_out_echo, "\t%llu output echo request%s\n");
988 p(ifs6_out_echoreply, "\t%llu output echo reply%s\n");
989 p(ifs6_out_routersolicit, "\t%llu output router solicitation%s\n");
990 p(ifs6_out_routeradvert, "\t%llu output router advertisement%s\n");
991 p(ifs6_out_neighborsolicit, "\t%llu output neighbor solicitation%s\n");
992 p(ifs6_out_neighboradvert, "\t%llu output neighbor advertisement%s\n");
993 p(ifs6_out_redirect, "\t%llu output redirect%s\n");
994 p(ifs6_out_mldquery, "\t%llu output MLD query%s\n");
995 p(ifs6_out_mldreport, "\t%llu output MLD report%s\n");
996 p(ifs6_out_mlddone, "\t%llu output MLD done%s\n");
997
998 end:
999 close(s);
1000#undef p
1001}
1002
1003/*
1004 * Dump PIM statistics structure.
1005 */
2b484d24 1006#ifdef notyet
7ba0088d 1007void
2b484d24 1008pim6_stats(void)
7ba0088d 1009{
b8dff150 1010 static struct pim6stat ppim6stat;
7ba0088d 1011 struct pim6stat pim6stat;
2b484d24 1012 size_t len = sizeof(struct pim6stat);
7ba0088d 1013
2b484d24 1014 if (sysctlbyname("net.inet6.ip6.pim6stat", &pim6stat, &len, 0, 0) == -1)
7ba0088d 1015 return;
7ba0088d
A
1016 printf("%s:\n", name);
1017
b8dff150
A
1018#define PIM6DIFF(f) (pim6stat.f - ppim6stat.f)
1019#define p(f, m) if (PIM6DIFF(f) || sflag <= 1) \
1020 printf(m, (unsigned long long)PIM6DIFF(f), plural(PIM6DIFF(f)))
7ba0088d
A
1021 p(pim6s_rcv_total, "\t%llu message%s received\n");
1022 p(pim6s_rcv_tooshort, "\t%llu message%s received with too few bytes\n");
1023 p(pim6s_rcv_badsum, "\t%llu message%s received with bad checksum\n");
1024 p(pim6s_rcv_badversion, "\t%llu message%s received with bad version\n");
1025 p(pim6s_rcv_registers, "\t%llu register%s received\n");
1026 p(pim6s_rcv_badregisters, "\t%llu bad register%s received\n");
1027 p(pim6s_snd_registers, "\t%llu register%s sent\n");
b8dff150
A
1028
1029 if (interval > 0)
1030 bcopy(&pim6stat, &ppim6stat, len);
1031
1032#undef PIM6DIFF
7ba0088d
A
1033#undef p
1034}
2b484d24 1035#endif
7ba0088d
A
1036
1037/*
1038 * Dump raw ip6 statistics structure.
1039 */
1040void
9c859447 1041rip6_stats(uint32_t off __unused, char *name, int af __unused)
7ba0088d 1042{
b8dff150 1043 static struct rip6stat prip6stat;
7ba0088d
A
1044 struct rip6stat rip6stat;
1045 u_quad_t delivered;
1046 int mib[4];
1047 size_t l;
1048
1049 mib[0] = CTL_NET;
1050 mib[1] = PF_INET6;
1051 mib[2] = IPPROTO_IPV6;
1052 mib[3] = IPV6CTL_RIP6STATS;
1053 l = sizeof(rip6stat);
1054 if (sysctl(mib, 4, &rip6stat, &l, NULL, 0) < 0) {
1055 perror("Warning: sysctl(net.inet6.ip6.rip6stats)");
1056 return;
1057 }
1058
1059 printf("%s:\n", name);
1060
b8dff150
A
1061#define RIP6DIFF(f) (rip6stat.f - prip6stat.f)
1062#define p(f, m) if (RIP6DIFF(f) || sflag <= 1) \
1063 printf(m, (unsigned long long)RIP6DIFF(f), plural(RIP6DIFF(f)))
7ba0088d
A
1064 p(rip6s_ipackets, "\t%llu message%s received\n");
1065 p(rip6s_isum, "\t%llu checksum calcuration%s on inbound\n");
1066 p(rip6s_badsum, "\t%llu message%s with bad checksum\n");
1067 p(rip6s_nosock, "\t%llu message%s dropped due to no socket\n");
1068 p(rip6s_nosockmcast,
1069 "\t%llu multicast message%s dropped due to no socket\n");
1070 p(rip6s_fullsock,
1071 "\t%llu message%s dropped due to full socket buffers\n");
b8dff150
A
1072 delivered = RIP6DIFF(rip6s_ipackets) -
1073 RIP6DIFF(rip6s_badsum) -
1074 RIP6DIFF(rip6s_nosock) -
1075 RIP6DIFF(rip6s_nosockmcast) -
1076 RIP6DIFF(rip6s_fullsock);
7ba0088d
A
1077 if (delivered || sflag <= 1)
1078 printf("\t%llu delivered\n", (unsigned long long)delivered);
1079 p(rip6s_opackets, "\t%llu datagram%s output\n");
b8dff150
A
1080
1081 if (interval > 0)
1082 bcopy(&rip6stat, &prip6stat, l);
1083
1084#undef RIP6DIFF
7ba0088d
A
1085#undef p
1086}
1087
1088/*
1089 * Pretty print an Internet address (net address + port).
1090 * If the nflag was specified, use numbers instead of names.
1091 */
ac2f15b3
A
1092#ifdef SRVCACHE
1093extern struct servent * _serv_cache_getservbyport(int port, char *proto);
1094
1095#define GETSERVBYPORT6(port, proto, ret)\
1096{\
1097 if (strcmp((proto), "tcp6") == 0)\
1098 (ret) = _serv_cache_getservbyport((int)(port), "tcp");\
1099 else if (strcmp((proto), "udp6") == 0)\
1100 (ret) = _serv_cache_getservbyport((int)(port), "udp");\
1101 else\
1102 (ret) = _serv_cache_getservbyport((int)(port), (proto));\
1103};
1104#else
7ba0088d
A
1105#define GETSERVBYPORT6(port, proto, ret)\
1106{\
1107 if (strcmp((proto), "tcp6") == 0)\
1108 (ret) = getservbyport((int)(port), "tcp");\
1109 else if (strcmp((proto), "udp6") == 0)\
1110 (ret) = getservbyport((int)(port), "udp");\
1111 else\
1112 (ret) = getservbyport((int)(port), (proto));\
1113};
ac2f15b3 1114#endif
7ba0088d
A
1115void
1116inet6print(struct in6_addr *in6, int port, char *proto, int numeric)
1117{
1118 struct servent *sp = 0;
1119 char line[80], *cp;
1120 int width;
1121
fdfd5971 1122 snprintf(line, sizeof(line), "%.*s.", lflag ? 39 :
7ba0088d
A
1123 (Aflag && !numeric) ? 12 : 16, inet6name(in6));
1124 cp = index(line, '\0');
1125 if (!numeric && port)
1126 GETSERVBYPORT6(port, proto, sp);
1127 if (sp || port == 0)
fdfd5971 1128 snprintf(cp, sizeof(line) - (cp - line), "%.8s", sp ? sp->s_name : "*");
7ba0088d 1129 else
fdfd5971 1130 snprintf(cp, sizeof(line) - (cp - line), "%d", ntohs((u_short)port));
7ba0088d
A
1131 width = lflag ? 45 : Aflag ? 18 : 22;
1132 printf("%-*.*s ", width, width, line);
1133}
1134
1135/*
1136 * Construct an Internet address representation.
1137 * If the nflag has been supplied, give
1138 * numeric value, otherwise try for symbolic name.
1139 */
1140
1141char *
1142inet6name(struct in6_addr *in6p)
1143{
1144 register char *cp;
1145 static char line[50];
1146 struct hostent *hp;
1147 static char domain[MAXHOSTNAMELEN];
1148 static int first = 1;
b8dff150
A
1149 char hbuf[NI_MAXHOST];
1150 struct sockaddr_in6 sin6;
1151 const int niflag = NI_NUMERICHOST;
7ba0088d
A
1152
1153 if (first && !nflag) {
1154 first = 0;
1155 if (gethostname(domain, MAXHOSTNAMELEN) == 0 &&
1156 (cp = index(domain, '.')))
fdfd5971 1157 (void) strlcpy(domain, cp + 1, sizeof(domain));
7ba0088d
A
1158 else
1159 domain[0] = 0;
1160 }
1161 cp = 0;
1162 if (!nflag && !IN6_IS_ADDR_UNSPECIFIED(in6p)) {
1163 hp = gethostbyaddr((char *)in6p, sizeof(*in6p), AF_INET6);
1164 if (hp) {
1165 if ((cp = index(hp->h_name, '.')) &&
1166 !strcmp(cp + 1, domain))
1167 *cp = 0;
1168 cp = hp->h_name;
1169 }
1170 }
1171 if (IN6_IS_ADDR_UNSPECIFIED(in6p))
fdfd5971 1172 strlcpy(line, "*", sizeof(line));
7ba0088d 1173 else if (cp)
b8dff150
A
1174 strlcpy(line, cp, sizeof(line));
1175 else {
1176 memset(&sin6, 0, sizeof(sin6));
1177 sin6.sin6_len = sizeof(sin6);
1178 sin6.sin6_family = AF_INET6;
1179 sin6.sin6_addr = *in6p;
1180
1181 if (IN6_IS_ADDR_LINKLOCAL(in6p) ||
fdfd5971 1182 IN6_IS_ADDR_MC_NODELOCAL(in6p) ||
b8dff150
A
1183 IN6_IS_ADDR_MC_LINKLOCAL(in6p)) {
1184 sin6.sin6_scope_id =
1185 ntohs(*(u_int16_t *)&in6p->s6_addr[2]);
1186 sin6.sin6_addr.s6_addr[2] = 0;
1187 sin6.sin6_addr.s6_addr[3] = 0;
1188 }
1189
1190 if (getnameinfo((struct sockaddr *)&sin6, sin6.sin6_len,
1191 hbuf, sizeof(hbuf), NULL, 0, niflag) != 0)
1192 strlcpy(hbuf, "?", sizeof(hbuf));
1193 strlcpy(line, hbuf, sizeof(line));
1194 }
7ba0088d
A
1195 return (line);
1196}
1197#endif /*INET6*/