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