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