]> git.saurik.com Git - apple/network_cmds.git/blob - netstat.tproj/inet6.c
b90b1411a1ce362702de2f7f1309e245edaf1c85
[apple/network_cmds.git] / netstat.tproj / inet6.c
1 /*
2 * Copyright (c) 2008-2018 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28
29 /* BSDI inet.c,v 2.3 1995/10/24 02:19:29 prb Exp */
30 /*
31 * Copyright (c) 1983, 1988, 1993
32 * The Regents of the University of California. All rights reserved.
33 *
34 * Redistribution and use in source and binary forms, with or without
35 * modification, are permitted provided that the following conditions
36 * are met:
37 * 1. Redistributions of source code must retain the above copyright
38 * notice, this list of conditions and the following disclaimer.
39 * 2. Redistributions in binary form must reproduce the above copyright
40 * notice, this list of conditions and the following disclaimer in the
41 * documentation and/or other materials provided with the distribution.
42 * 3. All advertising materials mentioning features or use of this software
43 * must display the following acknowledgement:
44 * This product includes software developed by the University of
45 * California, Berkeley and its contributors.
46 * 4. Neither the name of the University nor the names of its contributors
47 * may be used to endorse or promote products derived from this software
48 * without specific prior written permission.
49 *
50 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
51 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
52 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
53 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
54 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
55 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
56 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
57 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
58 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
59 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
60 * SUCH DAMAGE.
61 *
62 * $FreeBSD: src/usr.bin/netstat/inet6.c,v 1.3.2.9 2001/08/10 09:07:09 ru Exp $
63 */
64
65 #ifdef INET6
66 #include <sys/param.h>
67 #include <sys/socket.h>
68 #include <sys/socketvar.h>
69 #include <sys/ioctl.h>
70 #include <sys/sysctl.h>
71
72 #include <net/route.h>
73 #include <net/if.h>
74 #include <net/if_var.h>
75 #include <net/net_perf.h>
76 #include <netinet/in.h>
77 #include <netinet/ip6.h>
78 #include <netinet/icmp6.h>
79 #include <netinet/in_systm.h>
80 #include <netinet6/in6_pcb.h>
81 #include <netinet6/in6_var.h>
82 #include <netinet6/ip6_var.h>
83 #include <netinet6/raw_ip6.h>
84
85 #include <arpa/inet.h>
86 #include <netdb.h>
87
88 #include <stdio.h>
89 #include <string.h>
90 #include <unistd.h>
91 #include "netstat.h"
92
93 #if defined(__APPLE__) && !defined(__unused)
94 #define __unused
95 #endif
96
97 char *inet6name (struct in6_addr *);
98 void inet6print (struct in6_addr *, int, char *, int);
99
100 static char *ip6nh[] = {
101 "hop by hop",
102 "ICMP",
103 "IGMP",
104 "#3",
105 "IP",
106 "#5",
107 "TCP",
108 "#7",
109 "#8",
110 "#9",
111 "#10",
112 "#11",
113 "#12",
114 "#13",
115 "#14",
116 "#15",
117 "#16",
118 "UDP",
119 "#18",
120 "#19",
121 "#20",
122 "#21",
123 "IDP",
124 "#23",
125 "#24",
126 "#25",
127 "#26",
128 "#27",
129 "#28",
130 "TP",
131 "#30",
132 "#31",
133 "#32",
134 "#33",
135 "#34",
136 "#35",
137 "#36",
138 "#37",
139 "#38",
140 "#39",
141 "#40",
142 "IP6",
143 "#42",
144 "routing",
145 "fragment",
146 "#45",
147 "#46",
148 "#47",
149 "#48",
150 "#49",
151 "ESP",
152 "AH",
153 "#52",
154 "#53",
155 "#54",
156 "#55",
157 "#56",
158 "#57",
159 "ICMP6",
160 "no next header",
161 "destination option",
162 "#61",
163 "mobility",
164 "#63",
165 "#64",
166 "#65",
167 "#66",
168 "#67",
169 "#68",
170 "#69",
171 "#70",
172 "#71",
173 "#72",
174 "#73",
175 "#74",
176 "#75",
177 "#76",
178 "#77",
179 "#78",
180 "#79",
181 "ISOIP",
182 "#81",
183 "#82",
184 "#83",
185 "#84",
186 "#85",
187 "#86",
188 "#87",
189 "#88",
190 "OSPF",
191 "#80",
192 "#91",
193 "#92",
194 "#93",
195 "#94",
196 "#95",
197 "#96",
198 "Ethernet",
199 "#98",
200 "#99",
201 "#100",
202 "#101",
203 "#102",
204 "PIM",
205 "#104",
206 "#105",
207 "#106",
208 "#107",
209 "#108",
210 "#109",
211 "#110",
212 "#111",
213 "#112",
214 "#113",
215 "#114",
216 "#115",
217 "#116",
218 "#117",
219 "#118",
220 "#119",
221 "#120",
222 "#121",
223 "#122",
224 "#123",
225 "#124",
226 "#125",
227 "#126",
228 "#127",
229 "#128",
230 "#129",
231 "#130",
232 "#131",
233 "#132",
234 "#133",
235 "#134",
236 "#135",
237 "#136",
238 "#137",
239 "#138",
240 "#139",
241 "#140",
242 "#141",
243 "#142",
244 "#143",
245 "#144",
246 "#145",
247 "#146",
248 "#147",
249 "#148",
250 "#149",
251 "#150",
252 "#151",
253 "#152",
254 "#153",
255 "#154",
256 "#155",
257 "#156",
258 "#157",
259 "#158",
260 "#159",
261 "#160",
262 "#161",
263 "#162",
264 "#163",
265 "#164",
266 "#165",
267 "#166",
268 "#167",
269 "#168",
270 "#169",
271 "#170",
272 "#171",
273 "#172",
274 "#173",
275 "#174",
276 "#175",
277 "#176",
278 "#177",
279 "#178",
280 "#179",
281 "#180",
282 "#181",
283 "#182",
284 "#183",
285 "#184",
286 "#185",
287 "#186",
288 "#187",
289 "#188",
290 "#189",
291 "#180",
292 "#191",
293 "#192",
294 "#193",
295 "#194",
296 "#195",
297 "#196",
298 "#197",
299 "#198",
300 "#199",
301 "#200",
302 "#201",
303 "#202",
304 "#203",
305 "#204",
306 "#205",
307 "#206",
308 "#207",
309 "#208",
310 "#209",
311 "#210",
312 "#211",
313 "#212",
314 "#213",
315 "#214",
316 "#215",
317 "#216",
318 "#217",
319 "#218",
320 "#219",
321 "#220",
322 "#221",
323 "#222",
324 "#223",
325 "#224",
326 "#225",
327 "#226",
328 "#227",
329 "#228",
330 "#229",
331 "#230",
332 "#231",
333 "#232",
334 "#233",
335 "#234",
336 "#235",
337 "#236",
338 "#237",
339 "#238",
340 "#239",
341 "#240",
342 "#241",
343 "#242",
344 "#243",
345 "#244",
346 "#245",
347 "#246",
348 "#247",
349 "#248",
350 "#249",
351 "#250",
352 "#251",
353 "#252",
354 "#253",
355 "#254",
356 "#255",
357 };
358
359
360 static const char *srcrulenames[IP6S_SRCRULE_COUNT] = {
361 "default", // IP6S_SRCRULE_0
362 "prefer same address", // IP6S_SRCRULE_1
363 "prefer appropriate scope", // IP6S_SRCRULE_2
364 "avoid deprecated addresses", // IP6S_SRCRULE_3
365 "prefer home addresses", // IP6S_SRCRULE_4
366 "prefer outgoing interface", // IP6S_SRCRULE_5
367 "prefer matching label", // IP6S_SRCRULE_6
368 "prefer temporary addresses", // IP6S_SRCRULE_7
369 "prefer addresses on alive interfaces", // IP6S_SRCRULE_7x
370 "use longest matching prefix", // IP6S_SRCRULE_8
371 NULL,
372 NULL,
373 NULL,
374 NULL,
375 NULL,
376 NULL
377 };
378
379 /*
380 * Dump IP6 statistics structure.
381 */
382 void
383 ip6_stats(uint32_t off __unused, char *name, int af __unused)
384 {
385 static struct ip6stat pip6stat;
386 struct ip6stat ip6stat;
387 int first, i;
388 int mib[4];
389 size_t len;
390 static net_perf_t pout_net_perf, pin_net_perf;
391 net_perf_t out_net_perf, in_net_perf;
392 size_t out_net_perf_len = sizeof (out_net_perf);
393 size_t in_net_perf_len = sizeof (in_net_perf);
394
395 if (sysctlbyname("net.inet6.ip6.output_perf_data", &out_net_perf, &out_net_perf_len, 0, 0) < 0) {
396 perror("sysctl: net.inet6.ip6.output_perf_data");
397 return;
398 }
399
400 if (sysctlbyname("net.inet6.ip6.input_perf_data", &in_net_perf, &in_net_perf_len, 0, 0) < 0) {
401 perror("sysctl: net.inet6.ip6.input_perf_data");
402 return;
403 }
404
405 mib[0] = CTL_NET;
406 mib[1] = PF_INET6;
407 mib[2] = IPPROTO_IPV6;
408 mib[3] = IPV6CTL_STATS;
409
410 len = sizeof ip6stat;
411 memset(&ip6stat, 0, len);
412 if (sysctl(mib, 4, &ip6stat, &len, (void *)0, 0) < 0)
413 return;
414 if (interval && vflag > 0)
415 print_time();
416 printf("%s:\n", name);
417
418 #define IP6DIFF(f) (ip6stat.f - pip6stat.f)
419 #define p(f, m) if (IP6DIFF(f) || sflag <= 1) \
420 printf(m, (unsigned long long)IP6DIFF(f), plural(IP6DIFF(f)))
421 #define p1a(f, m) if (IP6DIFF(f) || sflag <= 1) \
422 printf(m, (unsigned long long)IP6DIFF(f))
423
424 p(ip6s_total, "\t%llu total packet%s received\n");
425 p1a(ip6s_toosmall, "\t\t%llu with size smaller than minimum\n");
426 p1a(ip6s_tooshort, "\t\t%llu with data size < data length\n");
427 p1a(ip6s_adj, "\t\t%llu with data size > data length\n");
428 p(ip6s_adj_hwcsum_clr,
429 "\t\t\t%llu packet%s forced to software checksum\n");
430 p1a(ip6s_badoptions, "\t\t%llu with bad options\n");
431 p1a(ip6s_badvers, "\t\t%llu with incorrect version number\n");
432 p(ip6s_fragments, "\t\t%llu fragment%s received\n");
433 p1a(ip6s_fragdropped,
434 "\t\t\t%llu dropped (dup or out of space)\n");
435 p1a(ip6s_fragtimeout, "\t\t\t%llu dropped after timeout\n");
436 p1a(ip6s_fragoverflow, "\t\t\t%llu exceeded limit\n");
437 p1a(ip6s_reassembled, "\t\t\t%llu reassembled ok\n");
438 p1a(ip6s_atmfrag_rcvd, "\t\t\t%llu atomic fragments received\n");
439 p(ip6s_delivered, "\t\t%llu packet%s for this host\n");
440 p(ip6s_forward, "\t\t%llu packet%s forwarded\n");
441 p(ip6s_cantforward, "\t\t%llu packet%s not forwardable\n");
442 p(ip6s_redirectsent, "\t\t%llu redirect%s sent\n");
443 p(ip6s_notmember, "\t\t%llu multicast packet%s which we don't join\n");
444 p(ip6s_exthdrtoolong,
445 "\t\t%llu packet%s whose headers are not continuous\n");
446 p(ip6s_nogif, "\t\t%llu tunneling packet%s that can't find gif\n");
447 p(ip6s_toomanyhdr,
448 "\t\t%llu packet%s discarded due to too may headers\n");
449 p1a(ip6s_forward_cachehit, "\t\t%llu forward cache hit\n");
450 p1a(ip6s_forward_cachemiss, "\t\t%llu forward cache miss\n");
451 p(ip6s_pktdropcntrl,
452 "\t\t%llu packet%s dropped due to no bufs for control data\n");
453 /* CLAT46 input stats */
454 p(ip6s_clat464_in_tooshort_drop,
455 "\t\t%llu input packet%s dropped due to too short length \n");
456 p(ip6s_clat464_in_nov6addr_drop,
457 "\t\t%llu input packet%s dropped due to missing CLAT46 IPv6 address\n");
458 p(ip6s_clat464_in_nov4addr_drop,
459 "\t\t%llu input packet%s dropped due to missing CLAT46 IPv4 address\n");
460 p(ip6s_clat464_in_v4synthfail_drop,
461 "\t\t%llu input packet%s dropped due to CLAT46 IPv4 address derivation failure\n");
462 p(ip6s_clat464_in_64transfail_drop,
463 "\t\t%llu input packet%s dropped due to CLAT46 IP header translation failure\n");
464 p(ip6s_clat464_in_64proto_transfail_drop,
465 "\t\t%llu input packet%s dropped due to CLAT46 protocol translation failure\n");
466 p(ip6s_clat464_in_64frag_transfail_drop,
467 "\t\t%llu input packet%s dropped due to CLAT46 fragment translation failure\n");
468 p(ip6s_clat464_in_invalpbuf_drop,
469 "\t\t%llu input packet%s dropped due to invalid pbuf\n");
470 p(ip6s_clat464_in_v4_drop,
471 "\t\t%llu input IPv4 packet%s dropped on CLAT46 enabled interface\n");
472 p(ip6s_clat464_in_drop,
473 "\t\t%llu input packet%s dropped due to CLAT46 failures\n");
474 p(ip6s_clat464_in_success,
475 "\t\t%llu input packet%s successfully translated from IPv6 to IPv4\n");
476
477 #define INPERFDIFF(f) (in_net_perf.f - pin_net_perf.f)
478 if (INPERFDIFF(np_total_pkts) > 0 && in_net_perf.np_total_usecs > 0) {
479 printf("\tInput Performance Stats:\n");
480 printf("\t\t%llu total packets measured\n", INPERFDIFF(np_total_pkts));
481 printf("\t\t%llu total usec elapsed\n", INPERFDIFF(np_total_usecs));
482 printf("\t\t%f usec per packet\n",
483 (double)in_net_perf.np_total_usecs/(double)in_net_perf.np_total_pkts);
484 printf("\t\tPerformance Histogram:\n");
485 printf("\t\t\t x <= %u: %llu\n", in_net_perf.np_hist_bars[0],
486 INPERFDIFF(np_hist1));
487 printf("\t\t\t %u < x <= %u: %llu\n",
488 in_net_perf.np_hist_bars[0], in_net_perf.np_hist_bars[1],
489 INPERFDIFF(np_hist2));
490 printf("\t\t\t %u < x <= %u: %llu\n",
491 in_net_perf.np_hist_bars[1], in_net_perf.np_hist_bars[2],
492 INPERFDIFF(np_hist3));
493 printf("\t\t\t %u < x <= %u: %llu\n",
494 in_net_perf.np_hist_bars[2], in_net_perf.np_hist_bars[3],
495 INPERFDIFF(np_hist4));
496 printf("\t\t\t %u < x: %llu\n",
497 in_net_perf.np_hist_bars[3], INPERFDIFF(np_hist5));
498 }
499 #undef INPERFDIFF
500
501 p(ip6s_localout, "\t%llu packet%s sent from this host\n");
502 p(ip6s_rawout, "\t\t%llu packet%s sent with fabricated ip header\n");
503 p(ip6s_odropped,
504 "\t\t%llu output packet%s dropped due to no bufs, etc.\n");
505 p(ip6s_noroute, "\t\t%llu output packet%s discarded due to no route\n");
506 p(ip6s_fragmented, "\t\t%llu output datagram%s fragmented\n");
507 p(ip6s_ofragments, "\t\t%llu fragment%s created\n");
508 p(ip6s_cantfrag, "\t\t%llu datagram%s that can't be fragmented\n");
509 p(ip6s_badscope, "\t\t%llu packet%s that violated scope rules\n");
510 p(ip6s_necp_policy_drop, "\t\t%llu packet%s dropped due to NECP policy\n");
511 /* CLAT46 output stats */
512 p(ip6s_clat464_out_nov6addr_drop,
513 "\t\t%llu output packet%s dropped due to missing CLAT46 IPv6 address\n");
514 p(ip6s_clat464_out_v6synthfail_drop,
515 "\t\t%llu output packet%s dropped due to CLAT46 IPv6 address synthesis failure\n");
516 p(ip6s_clat464_out_46transfail_drop,
517 "\t\t%llu output packet%s dropped due to CLAT46 IP header translation failure\n");
518 p(ip6s_clat464_out_46proto_transfail_drop,
519 "\t\t%llu output packet%s dropped due to CLAT46 protocol translation failure\n");
520 p(ip6s_clat464_out_46frag_transfail_drop,
521 "\t\t%llu output packet%s dropped due to CLAT46 fragment translation failure\n");
522 p(ip6s_clat464_out_invalpbuf_drop,
523 "\t\t%llu output packet%s dropped due to invalid pbuf\n");
524 p(ip6s_clat464_out_drop,
525 "\t\t%llu output packet%s dropped due to CLAT46 failures\n");
526 p(ip6s_clat464_out_success,
527 "\t\t%llu output packet%s successfully translated from IPv4 to IPv6\n");
528
529 #define OUTPERFDIFF(f) (out_net_perf.f - pout_net_perf.f)
530 if (OUTPERFDIFF(np_total_pkts) > 0 && out_net_perf.np_total_usecs > 0) {
531 printf("\tOutput Performance Stats:\n");
532 printf("\t\t%llu total packets measured\n", OUTPERFDIFF(np_total_pkts));
533 printf("\t\t%llu total usec elapsed\n", OUTPERFDIFF(np_total_usecs));
534 printf("\t\t%f usec per packet\n",
535 (double)out_net_perf.np_total_usecs/(double)out_net_perf.np_total_pkts);
536 printf("\t\tHistogram:\n");
537 printf("\t\t\t x <= %u: %llu\n", out_net_perf.np_hist_bars[0],
538 OUTPERFDIFF(np_hist1));
539 printf("\t\t\t %u < x <= %u: %llu\n",
540 out_net_perf.np_hist_bars[0], out_net_perf.np_hist_bars[1],
541 OUTPERFDIFF(np_hist2));
542 printf("\t\t\t %u < x <= %u: %llu\n",
543 out_net_perf.np_hist_bars[1], out_net_perf.np_hist_bars[2],
544 OUTPERFDIFF(np_hist3));
545 printf("\t\t\t %u < x <= %u: %llu\n",
546 out_net_perf.np_hist_bars[2], out_net_perf.np_hist_bars[3],
547 OUTPERFDIFF(np_hist4));
548 printf("\t\t\t %u < x: %llu\n",
549 out_net_perf.np_hist_bars[3], OUTPERFDIFF(np_hist5));
550 }
551 #undef OUTPERFDIFF
552
553 for (first = 1, i = 0; i < 256; i++)
554 if (IP6DIFF(ip6s_nxthist[i]) != 0) {
555 if (first) {
556 printf("\tInput histogram:\n");
557 first = 0;
558 }
559 printf("\t\t%s: %llu\n", ip6nh[i],
560 (unsigned long long)IP6DIFF(ip6s_nxthist[i]));
561 }
562 printf("\tMbuf statistics:\n");
563 printf("\t\t%llu one mbuf\n", (unsigned long long)IP6DIFF(ip6s_m1));
564 for (first = 1, i = 0; i < 32; i++) {
565 char ifbuf[IFNAMSIZ];
566 if (IP6DIFF(ip6s_m2m[i]) != 0) {
567 if (first) {
568 printf("\t\ttwo or more mbuf:\n");
569 first = 0;
570 }
571 printf("\t\t\t%s= %llu\n",
572 if_indextoname(i, ifbuf),
573 (unsigned long long)IP6DIFF(ip6s_m2m[i]));
574 }
575 }
576 printf("\t\t%llu one ext mbuf\n",
577 (unsigned long long)IP6DIFF(ip6s_mext1));
578 printf("\t\t%llu two or more ext mbuf\n",
579 (unsigned long long)IP6DIFF(ip6s_mext2m));
580
581 /* for debugging source address selection */
582 #define PRINT_SCOPESTAT(s,i) do {\
583 switch(i) { /* XXX hardcoding in each case */\
584 case 1:\
585 p(s, "\t\t\t%llu node-local%s\n");\
586 break;\
587 case 2:\
588 p(s,"\t\t\t%llu link-local%s\n");\
589 break;\
590 case 5:\
591 p(s,"\t\t\t%llu site-local%s\n");\
592 break;\
593 case 14:\
594 p(s,"\t\t\t%llu global%s\n");\
595 break;\
596 default:\
597 printf("\t\t\t%llu addresses scope=%x\n",\
598 (unsigned long long)IP6DIFF(s), i);\
599 }\
600 } while (0);
601
602 p(ip6s_sources_none,
603 "\t\t%llu failure%s of source address selection\n");
604 for (first = 1, i = 0; i < SCOPE6_ID_MAX; i++) {
605 if (IP6DIFF(ip6s_sources_sameif[i]) || 1) {
606 if (first) {
607 printf("\t\tsource addresses on an outgoing I/F\n");
608 first = 0;
609 }
610 PRINT_SCOPESTAT(ip6s_sources_sameif[i], i);
611 }
612 }
613 for (first = 1, i = 0; i < SCOPE6_ID_MAX; i++) {
614 if (IP6DIFF(ip6s_sources_otherif[i]) || 1) {
615 if (first) {
616 printf("\t\tsource addresses on a non-outgoing I/F\n");
617 first = 0;
618 }
619 PRINT_SCOPESTAT(ip6s_sources_otherif[i], i);
620 }
621 }
622 for (first = 1, i = 0; i < SCOPE6_ID_MAX; i++) {
623 if (IP6DIFF(ip6s_sources_samescope[i]) || 1) {
624 if (first) {
625 printf("\t\tsource addresses of same scope\n");
626 first = 0;
627 }
628 PRINT_SCOPESTAT(ip6s_sources_samescope[i], i);
629 }
630 }
631 for (first = 1, i = 0; i < SCOPE6_ID_MAX; i++) {
632 if (IP6DIFF(ip6s_sources_otherscope[i]) || 1) {
633 if (first) {
634 printf("\t\tsource addresses of a different scope\n");
635 first = 0;
636 }
637 PRINT_SCOPESTAT(ip6s_sources_otherscope[i], i);
638 }
639 }
640 for (first = 1, i = 0; i < SCOPE6_ID_MAX; i++) {
641 if (IP6DIFF(ip6s_sources_deprecated[i]) || 1) {
642 if (first) {
643 printf("\t\tdeprecated source addresses\n");
644 first = 0;
645 }
646 PRINT_SCOPESTAT(ip6s_sources_deprecated[i], i);
647 }
648 }
649 #define PRINT_SRCRULESTAT(s,i) do {\
650 if (srcrulenames[i] != NULL) \
651 printf("\t\t\t%llu rule%s %s\n", \
652 (unsigned long long)IP6DIFF(s), \
653 plural(IP6DIFF(s)), \
654 srcrulenames[i]); \
655 } while (0);
656
657 for (first = 1, i = 0; i < IP6S_SRCRULE_COUNT; i++) {
658 if (IP6DIFF(ip6s_sources_rule[i]) || 1) {
659 if (first) {
660 printf("\t\tsource address selection\n");
661 first = 0;
662 }
663 PRINT_SRCRULESTAT(ip6s_sources_rule[i], i);
664 }
665 }
666
667 p(ip6s_dad_collide, "\t\t%llu duplicate address detection collision%s\n");
668
669 p(ip6s_dad_loopcount, "\t\t%llu duplicate address detection NS loop%s\n");
670
671 p(ip6s_sources_skip_expensive_secondary_if, "\t\t%llu time%s ignored source on secondary expensive I/F\n");
672
673 if (interval > 0) {
674 bcopy(&ip6stat, &pip6stat, len);
675 bcopy(&in_net_perf, &pin_net_perf, in_net_perf_len);
676 bcopy(&out_net_perf, &pout_net_perf, out_net_perf_len);
677 }
678 #undef IP6DIFF
679 #undef p
680 #undef p1a
681 }
682
683 /*
684 * Dump IPv6 per-interface statistics based on RFC 2465.
685 */
686 void
687 ip6_ifstats(char *ifname)
688 {
689 struct in6_ifreq ifr;
690 int s;
691 #define p(f, m) if (ifr.ifr_ifru.ifru_stat.f || sflag <= 1) \
692 printf(m, (unsigned long long)ifr.ifr_ifru.ifru_stat.f, plural(ifr.ifr_ifru.ifru_stat.f))
693 #define p_5(f, m) if (ifr.ifr_ifru.ifru_stat.f || sflag <= 1) \
694 printf(m, (unsigned long long)ip6stat.f)
695
696 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
697 perror("Warning: socket(AF_INET6)");
698 return;
699 }
700
701 if (interval && vflag > 0)
702 print_time();
703 strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
704 printf("ip6 on %s:\n", ifr.ifr_name);
705
706 if (ioctl(s, SIOCGIFSTAT_IN6, (char *)&ifr) < 0) {
707 perror("Warning: ioctl(SIOCGIFSTAT_IN6)");
708 goto end;
709 }
710
711 p(ifs6_in_receive, "\t%llu total input datagram%s\n");
712 p(ifs6_in_hdrerr, "\t%llu datagram%s with invalid header received\n");
713 p(ifs6_in_toobig, "\t%llu datagram%s exceeded MTU received\n");
714 p(ifs6_in_noroute, "\t%llu datagram%s with no route received\n");
715 p(ifs6_in_addrerr, "\t%llu datagram%s with invalid dst received\n");
716 p(ifs6_in_protounknown, "\t%llu datagram%s with unknown proto received\n");
717 p(ifs6_in_truncated, "\t%llu truncated datagram%s received\n");
718 p(ifs6_in_discard, "\t%llu input datagram%s discarded\n");
719 p(ifs6_in_deliver,
720 "\t%llu datagram%s delivered to an upper layer protocol\n");
721 p(ifs6_out_forward, "\t%llu datagram%s forwarded to this interface\n");
722 p(ifs6_out_request,
723 "\t%llu datagram%s sent from an upper layer protocol\n");
724 p(ifs6_out_discard, "\t%llu total discarded output datagram%s\n");
725 p(ifs6_out_fragok, "\t%llu output datagram%s fragmented\n");
726 p(ifs6_out_fragfail, "\t%llu output datagram%s failed on fragment\n");
727 p(ifs6_out_fragcreat, "\t%llu output datagram%s succeeded on fragment\n");
728 p(ifs6_reass_reqd, "\t%llu incoming datagram%s fragmented\n");
729 p(ifs6_reass_ok, "\t%llu datagram%s reassembled\n");
730 p(ifs6_atmfrag_rcvd, "\t%llu atomic fragments%s received\n");
731 p(ifs6_reass_fail, "\t%llu datagram%s failed on reassembling\n");
732 p(ifs6_in_mcast, "\t%llu multicast datagram%s received\n");
733 p(ifs6_out_mcast, "\t%llu multicast datagram%s sent\n");
734
735 p(ifs6_cantfoward_icmp6, "\t%llu ICMPv6 packet%s received for unreachable destination\n");
736 p(ifs6_addr_expiry_cnt, "\t%llu address expiry event%s reported\n");
737 p(ifs6_pfx_expiry_cnt, "\t%llu prefix expiry event%s reported\n");
738 p(ifs6_defrtr_expiry_cnt, "\t%llu default router expiry event%s reported\n");
739 end:
740 close(s);
741
742 #undef p
743 #undef p_5
744 }
745
746 static char *icmp6names[] = {
747 "#0",
748 "unreach",
749 "packet too big",
750 "time exceed",
751 "parameter problem",
752 "#5",
753 "#6",
754 "#7",
755 "#8",
756 "#9",
757 "#10",
758 "#11",
759 "#12",
760 "#13",
761 "#14",
762 "#15",
763 "#16",
764 "#17",
765 "#18",
766 "#19",
767 "#20",
768 "#21",
769 "#22",
770 "#23",
771 "#24",
772 "#25",
773 "#26",
774 "#27",
775 "#28",
776 "#29",
777 "#30",
778 "#31",
779 "#32",
780 "#33",
781 "#34",
782 "#35",
783 "#36",
784 "#37",
785 "#38",
786 "#39",
787 "#40",
788 "#41",
789 "#42",
790 "#43",
791 "#44",
792 "#45",
793 "#46",
794 "#47",
795 "#48",
796 "#49",
797 "#50",
798 "#51",
799 "#52",
800 "#53",
801 "#54",
802 "#55",
803 "#56",
804 "#57",
805 "#58",
806 "#59",
807 "#60",
808 "#61",
809 "#62",
810 "#63",
811 "#64",
812 "#65",
813 "#66",
814 "#67",
815 "#68",
816 "#69",
817 "#70",
818 "#71",
819 "#72",
820 "#73",
821 "#74",
822 "#75",
823 "#76",
824 "#77",
825 "#78",
826 "#79",
827 "#80",
828 "#81",
829 "#82",
830 "#83",
831 "#84",
832 "#85",
833 "#86",
834 "#87",
835 "#88",
836 "#89",
837 "#80",
838 "#91",
839 "#92",
840 "#93",
841 "#94",
842 "#95",
843 "#96",
844 "#97",
845 "#98",
846 "#99",
847 "#100",
848 "#101",
849 "#102",
850 "#103",
851 "#104",
852 "#105",
853 "#106",
854 "#107",
855 "#108",
856 "#109",
857 "#110",
858 "#111",
859 "#112",
860 "#113",
861 "#114",
862 "#115",
863 "#116",
864 "#117",
865 "#118",
866 "#119",
867 "#120",
868 "#121",
869 "#122",
870 "#123",
871 "#124",
872 "#125",
873 "#126",
874 "#127",
875 "echo",
876 "echo reply",
877 "multicast listener query",
878 "MLDv1 listener report",
879 "MLDv1 listener done",
880 "router solicitation",
881 "router advertisement",
882 "neighbor solicitation",
883 "neighbor advertisement",
884 "redirect",
885 "router renumbering",
886 "node information request",
887 "node information reply",
888 "inverse neighbor solicitation",
889 "inverse neighbor advertisement",
890 "MLDv2 listener report",
891 "#144",
892 "#145",
893 "#146",
894 "#147",
895 "#148",
896 "#149",
897 "#150",
898 "#151",
899 "#152",
900 "#153",
901 "#154",
902 "#155",
903 "#156",
904 "#157",
905 "#158",
906 "#159",
907 "#160",
908 "#161",
909 "#162",
910 "#163",
911 "#164",
912 "#165",
913 "#166",
914 "#167",
915 "#168",
916 "#169",
917 "#170",
918 "#171",
919 "#172",
920 "#173",
921 "#174",
922 "#175",
923 "#176",
924 "#177",
925 "#178",
926 "#179",
927 "#180",
928 "#181",
929 "#182",
930 "#183",
931 "#184",
932 "#185",
933 "#186",
934 "#187",
935 "#188",
936 "#189",
937 "#180",
938 "#191",
939 "#192",
940 "#193",
941 "#194",
942 "#195",
943 "#196",
944 "#197",
945 "#198",
946 "#199",
947 "#200",
948 "#201",
949 "#202",
950 "#203",
951 "#204",
952 "#205",
953 "#206",
954 "#207",
955 "#208",
956 "#209",
957 "#210",
958 "#211",
959 "#212",
960 "#213",
961 "#214",
962 "#215",
963 "#216",
964 "#217",
965 "#218",
966 "#219",
967 "#220",
968 "#221",
969 "#222",
970 "#223",
971 "#224",
972 "#225",
973 "#226",
974 "#227",
975 "#228",
976 "#229",
977 "#230",
978 "#231",
979 "#232",
980 "#233",
981 "#234",
982 "#235",
983 "#236",
984 "#237",
985 "#238",
986 "#239",
987 "#240",
988 "#241",
989 "#242",
990 "#243",
991 "#244",
992 "#245",
993 "#246",
994 "#247",
995 "#248",
996 "#249",
997 "#250",
998 "#251",
999 "#252",
1000 "#253",
1001 "#254",
1002 "#255",
1003 };
1004
1005 /*
1006 * Dump ICMP6 statistics.
1007 */
1008 void
1009 icmp6_stats(uint32_t off __unused, char *name, int af __unused)
1010 {
1011 static struct icmp6stat picmp6stat;
1012 struct icmp6stat icmp6stat;
1013 register int i, first;
1014 int mib[4];
1015 size_t len;
1016
1017 mib[0] = CTL_NET;
1018 mib[1] = PF_INET6;
1019 mib[2] = IPPROTO_ICMPV6;
1020 mib[3] = ICMPV6CTL_STATS;
1021
1022 len = sizeof icmp6stat;
1023 memset(&icmp6stat, 0, len);
1024 if (sysctl(mib, 4, &icmp6stat, &len, (void *)0, 0) < 0)
1025 return;
1026 if (interval && vflag > 0)
1027 print_time();
1028 printf("%s:\n", name);
1029
1030 #define ICMP6DIFF(f) (icmp6stat.f - picmp6stat.f)
1031 #define p(f, m) if (ICMP6DIFF(f) || sflag <= 1) \
1032 printf(m, (unsigned long long)ICMP6DIFF(f), plural(ICMP6DIFF(f)))
1033 #define p_5(f, m) printf(m, (unsigned long long)ICMP6DIFF(f))
1034
1035 p(icp6s_error, "\t%llu call%s to icmp_error\n");
1036 p(icp6s_canterror,
1037 "\t%llu error%s not generated because old message was icmp error or so\n");
1038 p(icp6s_toofreq,
1039 "\t%llu error%s not generated because rate limitation\n");
1040 #define NELEM (sizeof(icmp6stat.icp6s_outhist)/sizeof(icmp6stat.icp6s_outhist[0]))
1041 for (first = 1, i = 0; i < NELEM; i++)
1042 if (ICMP6DIFF(icp6s_outhist[i]) != 0) {
1043 if (first) {
1044 printf("\tOutput histogram:\n");
1045 first = 0;
1046 }
1047 printf("\t\t%s: %llu\n", icmp6names[i],
1048 (unsigned long long)ICMP6DIFF(icp6s_outhist[i]));
1049 }
1050 #undef NELEM
1051 p(icp6s_badcode, "\t%llu message%s with bad code fields\n");
1052 p(icp6s_tooshort, "\t%llu message%s < minimum length\n");
1053 p(icp6s_checksum, "\t%llu bad checksum%s\n");
1054 p(icp6s_badlen, "\t%llu message%s with bad length\n");
1055 #define NELEM (sizeof(icmp6stat.icp6s_inhist)/sizeof(icmp6stat.icp6s_inhist[0]))
1056 for (first = 1, i = 0; i < NELEM; i++)
1057 if (ICMP6DIFF(icp6s_inhist[i]) != 0) {
1058 if (first) {
1059 printf("\tInput histogram:\n");
1060 first = 0;
1061 }
1062 printf("\t\t%s: %llu\n", icmp6names[i],
1063 (unsigned long long)ICMP6DIFF(icp6s_inhist[i]));
1064 }
1065 #undef NELEM
1066 printf("\tHistogram of error messages to be generated:\n");
1067 p_5(icp6s_odst_unreach_noroute, "\t\t%llu no route\n");
1068 p_5(icp6s_odst_unreach_admin, "\t\t%llu administratively prohibited\n");
1069 p_5(icp6s_odst_unreach_beyondscope, "\t\t%llu beyond scope\n");
1070 p_5(icp6s_odst_unreach_addr, "\t\t%llu address unreachable\n");
1071 p_5(icp6s_odst_unreach_noport, "\t\t%llu port unreachable\n");
1072 p_5(icp6s_opacket_too_big, "\t\t%llu packet too big\n");
1073 p_5(icp6s_otime_exceed_transit, "\t\t%llu time exceed transit\n");
1074 p_5(icp6s_otime_exceed_reassembly, "\t\t%llu time exceed reassembly\n");
1075 p_5(icp6s_oparamprob_header, "\t\t%llu erroneous header field\n");
1076 p_5(icp6s_oparamprob_nextheader, "\t\t%llu unrecognized next header\n");
1077 p_5(icp6s_oparamprob_option, "\t\t%llu unrecognized option\n");
1078 p_5(icp6s_oredirect, "\t\t%llu redirect\n");
1079 p_5(icp6s_ounknown, "\t\t%llu unknown\n");
1080
1081 p(icp6s_reflect, "\t%llu message response%s generated\n");
1082 p(icp6s_nd_toomanyopt, "\t%llu message%s with too many ND options\n");
1083 p(icp6s_nd_badopt, "\t%qu message%s with bad ND options\n");
1084 p(icp6s_badns, "\t%qu bad neighbor solicitation message%s\n");
1085 p(icp6s_badna, "\t%qu bad neighbor advertisement message%s\n");
1086 p(icp6s_badrs, "\t%qu bad router solicitation message%s\n");
1087 p(icp6s_badra, "\t%qu bad router advertisement message%s\n");
1088 p(icp6s_badredirect, "\t%qu bad redirect message%s\n");
1089 p(icp6s_pmtuchg, "\t%llu path MTU change%s\n");
1090 p(icp6s_rfc6980_drop, "\t%qu dropped fragmented NDP message%s\n");
1091
1092 if (interval > 0)
1093 bcopy(&icmp6stat, &picmp6stat, len);
1094
1095 #undef ICMP6DIFF
1096 #undef p
1097 #undef p_5
1098 }
1099
1100 /*
1101 * Dump ICMPv6 per-interface statistics based on RFC 2466.
1102 */
1103 void
1104 icmp6_ifstats(char *ifname)
1105 {
1106 struct in6_ifreq ifr;
1107 int s;
1108 #define p(f, m) if (ifr.ifr_ifru.ifru_icmp6stat.f || sflag <= 1) \
1109 printf(m, (unsigned long long)ifr.ifr_ifru.ifru_icmp6stat.f, plural(ifr.ifr_ifru.ifru_icmp6stat.f))
1110
1111 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
1112 perror("Warning: socket(AF_INET6)");
1113 return;
1114 }
1115
1116 if (interval && vflag > 0)
1117 print_time();
1118 strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
1119 printf("icmp6 on %s:\n", ifr.ifr_name);
1120
1121 if (ioctl(s, SIOCGIFSTAT_ICMP6, (char *)&ifr) < 0) {
1122 perror("Warning: ioctl(SIOCGIFSTAT_ICMP6)");
1123 goto end;
1124 }
1125
1126 p(ifs6_in_msg, "\t%llu total input message%s\n");
1127 p(ifs6_in_error, "\t%llu total input error message%s\n");
1128 p(ifs6_in_dstunreach, "\t%llu input destination unreachable error%s\n");
1129 p(ifs6_in_adminprohib, "\t%llu input administratively prohibited error%s\n");
1130 p(ifs6_in_timeexceed, "\t%llu input time exceeded error%s\n");
1131 p(ifs6_in_paramprob, "\t%llu input parameter problem error%s\n");
1132 p(ifs6_in_pkttoobig, "\t%llu input packet too big error%s\n");
1133 p(ifs6_in_echo, "\t%llu input echo request%s\n");
1134 p(ifs6_in_echoreply, "\t%llu input echo reply%s\n");
1135 p(ifs6_in_routersolicit, "\t%llu input router solicitation%s\n");
1136 p(ifs6_in_routeradvert, "\t%llu input router advertisement%s\n");
1137 p(ifs6_in_neighborsolicit, "\t%llu input neighbor solicitation%s\n");
1138 p(ifs6_in_neighboradvert, "\t%llu input neighbor advertisement%s\n");
1139 p(ifs6_in_redirect, "\t%llu input redirect%s\n");
1140 p(ifs6_in_mldquery, "\t%llu input MLD query%s\n");
1141 p(ifs6_in_mldreport, "\t%llu input MLD report%s\n");
1142 p(ifs6_in_mlddone, "\t%llu input MLD done%s\n");
1143
1144 p(ifs6_out_msg, "\t%llu total output message%s\n");
1145 p(ifs6_out_error, "\t%llu total output error message%s\n");
1146 p(ifs6_out_dstunreach, "\t%llu output destination unreachable error%s\n");
1147 p(ifs6_out_adminprohib, "\t%llu output administratively prohibited error%s\n");
1148 p(ifs6_out_timeexceed, "\t%llu output time exceeded error%s\n");
1149 p(ifs6_out_paramprob, "\t%llu output parameter problem error%s\n");
1150 p(ifs6_out_pkttoobig, "\t%llu output packet too big error%s\n");
1151 p(ifs6_out_echo, "\t%llu output echo request%s\n");
1152 p(ifs6_out_echoreply, "\t%llu output echo reply%s\n");
1153 p(ifs6_out_routersolicit, "\t%llu output router solicitation%s\n");
1154 p(ifs6_out_routeradvert, "\t%llu output router advertisement%s\n");
1155 p(ifs6_out_neighborsolicit, "\t%llu output neighbor solicitation%s\n");
1156 p(ifs6_out_neighboradvert, "\t%llu output neighbor advertisement%s\n");
1157 p(ifs6_out_redirect, "\t%llu output redirect%s\n");
1158 p(ifs6_out_mldquery, "\t%llu output MLD query%s\n");
1159 p(ifs6_out_mldreport, "\t%llu output MLD report%s\n");
1160 p(ifs6_out_mlddone, "\t%llu output MLD done%s\n");
1161
1162 end:
1163 close(s);
1164 #undef p
1165 }
1166
1167 /*
1168 * Dump raw ip6 statistics structure.
1169 */
1170 void
1171 rip6_stats(uint32_t off __unused, char *name, int af __unused)
1172 {
1173 static struct rip6stat prip6stat;
1174 struct rip6stat rip6stat;
1175 u_quad_t delivered;
1176 int mib[4];
1177 size_t l;
1178
1179 mib[0] = CTL_NET;
1180 mib[1] = PF_INET6;
1181 mib[2] = IPPROTO_IPV6;
1182 mib[3] = IPV6CTL_RIP6STATS;
1183 l = sizeof(rip6stat);
1184 if (sysctl(mib, 4, &rip6stat, &l, NULL, 0) < 0) {
1185 perror("Warning: sysctl(net.inet6.ip6.rip6stats)");
1186 return;
1187 }
1188
1189 if (interval && vflag > 0)
1190 print_time();
1191 printf("%s:\n", name);
1192
1193 #define RIP6DIFF(f) (rip6stat.f - prip6stat.f)
1194 #define p(f, m) if (RIP6DIFF(f) || sflag <= 1) \
1195 printf(m, (unsigned long long)RIP6DIFF(f), plural(RIP6DIFF(f)))
1196 p(rip6s_ipackets, "\t%llu message%s received\n");
1197 p(rip6s_isum, "\t%llu checksum calculation%s on inbound\n");
1198 p(rip6s_badsum, "\t%llu message%s with bad checksum\n");
1199 p(rip6s_nosock, "\t%llu message%s dropped due to no socket\n");
1200 p(rip6s_nosockmcast,
1201 "\t%llu multicast message%s dropped due to no socket\n");
1202 p(rip6s_fullsock,
1203 "\t%llu message%s dropped due to full socket buffers\n");
1204 delivered = RIP6DIFF(rip6s_ipackets) -
1205 RIP6DIFF(rip6s_badsum) -
1206 RIP6DIFF(rip6s_nosock) -
1207 RIP6DIFF(rip6s_nosockmcast) -
1208 RIP6DIFF(rip6s_fullsock);
1209 if (delivered || sflag <= 1)
1210 printf("\t%llu delivered\n", (unsigned long long)delivered);
1211 p(rip6s_opackets, "\t%llu datagram%s output\n");
1212
1213 if (interval > 0)
1214 bcopy(&rip6stat, &prip6stat, l);
1215
1216 #undef RIP6DIFF
1217 #undef p
1218 }
1219
1220 /*
1221 * Pretty print an Internet address (net address + port).
1222 * If the nflag was specified, use numbers instead of names.
1223 */
1224 #ifdef SRVCACHE
1225 extern struct servent * _serv_cache_getservbyport(int port, char *proto);
1226
1227 #define GETSERVBYPORT6(port, proto, ret)\
1228 {\
1229 if (strcmp((proto), "tcp6") == 0)\
1230 (ret) = _serv_cache_getservbyport((int)(port), "tcp");\
1231 else if (strcmp((proto), "udp6") == 0)\
1232 (ret) = _serv_cache_getservbyport((int)(port), "udp");\
1233 else\
1234 (ret) = _serv_cache_getservbyport((int)(port), (proto));\
1235 };
1236 #else
1237 #define GETSERVBYPORT6(port, proto, ret)\
1238 {\
1239 if (strcmp((proto), "tcp6") == 0)\
1240 (ret) = getservbyport((int)(port), "tcp");\
1241 else if (strcmp((proto), "udp6") == 0)\
1242 (ret) = getservbyport((int)(port), "udp");\
1243 else\
1244 (ret) = getservbyport((int)(port), (proto));\
1245 };
1246 #endif
1247 void
1248 inet6print(struct in6_addr *in6, int port, char *proto, int numeric)
1249 {
1250 struct servent *sp = 0;
1251 char line[80], *cp;
1252 int width;
1253
1254 snprintf(line, sizeof(line), "%.*s.", lflag ? 39 :
1255 (Aflag && !numeric) ? 12 : 16, inet6name(in6));
1256 cp = index(line, '\0');
1257 if (!numeric && port)
1258 GETSERVBYPORT6(port, proto, sp);
1259 if (sp || port == 0)
1260 snprintf(cp, sizeof(line) - (cp - line), "%.15s", sp ? sp->s_name : "*");
1261 else
1262 snprintf(cp, sizeof(line) - (cp - line), "%d", ntohs((u_short)port));
1263 width = lflag ? 45 : Aflag ? 18 : 22;
1264 printf("%-*.*s ", width, width, line);
1265 }
1266
1267 /*
1268 * Construct an Internet address representation.
1269 * If the nflag has been supplied, give
1270 * numeric value, otherwise try for symbolic name.
1271 */
1272
1273 char *
1274 inet6name(struct in6_addr *in6p)
1275 {
1276 register char *cp;
1277 static char line[50];
1278 struct hostent *hp;
1279 static char domain[MAXHOSTNAMELEN];
1280 static int first = 1;
1281 char hbuf[NI_MAXHOST];
1282 struct sockaddr_in6 sin6;
1283 const int niflag = NI_NUMERICHOST;
1284
1285 if (first && !nflag) {
1286 first = 0;
1287 if (gethostname(domain, MAXHOSTNAMELEN) == 0 &&
1288 (cp = index(domain, '.')))
1289 (void) memmove(domain, cp + 1, strlen(cp + 1) + 1);
1290 else
1291 domain[0] = 0;
1292 }
1293 cp = 0;
1294 if (!nflag && !IN6_IS_ADDR_UNSPECIFIED(in6p)) {
1295 hp = gethostbyaddr((char *)in6p, sizeof(*in6p), AF_INET6);
1296 if (hp) {
1297 if ((cp = index(hp->h_name, '.')) &&
1298 !strcmp(cp + 1, domain))
1299 *cp = 0;
1300 cp = hp->h_name;
1301 }
1302 }
1303 if (IN6_IS_ADDR_UNSPECIFIED(in6p))
1304 strlcpy(line, "*", sizeof(line));
1305 else if (cp)
1306 strlcpy(line, cp, sizeof(line));
1307 else {
1308 memset(&sin6, 0, sizeof(sin6));
1309 sin6.sin6_len = sizeof(sin6);
1310 sin6.sin6_family = AF_INET6;
1311 sin6.sin6_addr = *in6p;
1312
1313 if (IN6_IS_ADDR_LINKLOCAL(in6p) ||
1314 IN6_IS_ADDR_MC_NODELOCAL(in6p) ||
1315 IN6_IS_ADDR_MC_LINKLOCAL(in6p)) {
1316 sin6.sin6_scope_id =
1317 ntohs(*(u_int16_t *)&in6p->s6_addr[2]);
1318 sin6.sin6_addr.s6_addr[2] = 0;
1319 sin6.sin6_addr.s6_addr[3] = 0;
1320 }
1321
1322 if (getnameinfo((struct sockaddr *)&sin6, sin6.sin6_len,
1323 hbuf, sizeof(hbuf), NULL, 0, niflag) != 0)
1324 strlcpy(hbuf, "?", sizeof(hbuf));
1325 strlcpy(line, hbuf, sizeof(line));
1326 }
1327 return (line);
1328 }
1329 #endif /*INET6*/