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