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