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