]> git.saurik.com Git - apple/network_cmds.git/blame_incremental - ping6.tproj/ping6.c
network_cmds-329.2.2.tar.gz
[apple/network_cmds.git] / ping6.tproj / ping6.c
... / ...
CommitLineData
1/* $KAME: ping6.c,v 1.169 2003/07/25 06:01:47 itojun Exp $ */
2
3/*
4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5 * 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.
15 * 3. Neither the name of the project nor the names of its contributors
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 PROJECT 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 PROJECT 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/* BSDI ping.c,v 2.3 1996/01/21 17:56:50 jch Exp */
33
34/*
35 * Copyright (c) 1989, 1993
36 * The Regents of the University of California. All rights reserved.
37 *
38 * This code is derived from software contributed to Berkeley by
39 * Mike Muuss.
40 *
41 * Redistribution and use in source and binary forms, with or without
42 * modification, are permitted provided that the following conditions
43 * are met:
44 * 1. Redistributions of source code must retain the above copyright
45 * notice, this list of conditions and the following disclaimer.
46 * 2. Redistributions in binary form must reproduce the above copyright
47 * notice, this list of conditions and the following disclaimer in the
48 * documentation and/or other materials provided with the distribution.
49 * 3. All advertising materials mentioning features or use of this software
50 * must display the following acknowledgement:
51 * This product includes software developed by the University of
52 * California, Berkeley and its contributors.
53 * 4. Neither the name of the University nor the names of its contributors
54 * may be used to endorse or promote products derived from this software
55 * without specific prior written permission.
56 *
57 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
58 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
59 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
60 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
61 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
62 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
63 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
64 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
65 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
66 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
67 * SUCH DAMAGE.
68 */
69
70#ifndef lint
71static const char copyright[] =
72"@(#) Copyright (c) 1989, 1993\n\
73 The Regents of the University of California. All rights reserved.\n";
74#endif /* not lint */
75
76#ifndef lint
77#if 0
78static char sccsid[] = "@(#)ping.c 8.1 (Berkeley) 6/5/93";
79#endif
80static const char rcsid[] =
81 "$FreeBSD: src/sbin/ping6/ping6.c,v 1.29.2.1 2007/05/22 22:01:44 mtm Exp $";
82#endif /* not lint */
83
84/*
85 * Using the InterNet Control Message Protocol (ICMP) "ECHO" facility,
86 * measure round-trip-delays and packet loss across network paths.
87 *
88 * Author -
89 * Mike Muuss
90 * U. S. Army Ballistic Research Laboratory
91 * December, 1983
92 *
93 * Status -
94 * Public Domain. Distribution Unlimited.
95 * Bugs -
96 * More statistics could always be gathered.
97 * This program has to run SUID to ROOT to access the ICMP socket.
98 */
99/*
100 * NOTE:
101 * USE_SIN6_SCOPE_ID assumes that sin6_scope_id has the same semantics
102 * as IPV6_PKTINFO. Some people object it (sin6_scope_id specifies *link*
103 * while IPV6_PKTINFO specifies *interface*. Link is defined as collection of
104 * network attached to 1 or more interfaces)
105 */
106
107#include <sys/param.h>
108#include <sys/uio.h>
109#include <sys/socket.h>
110#include <sys/time.h>
111
112#include <net/if.h>
113#include <net/route.h>
114
115#include <netinet/in.h>
116#include <netinet/ip6.h>
117#include <netinet/icmp6.h>
118#include <arpa/inet.h>
119#include <arpa/nameser.h>
120#include <netdb.h>
121
122#include <ctype.h>
123#include <err.h>
124#include <errno.h>
125#include <fcntl.h>
126#include <math.h>
127#include <signal.h>
128#include <stdio.h>
129#include <stdlib.h>
130#include <string.h>
131#include <unistd.h>
132#ifdef HAVE_POLL_H
133#include <poll.h>
134#endif
135
136#ifdef IPSEC
137#include <netinet6/ah.h>
138#include <netinet6/ipsec.h>
139#endif
140
141#if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__OpenBSD__)
142#include <md5.h>
143#else
144#include "md5.h"
145#endif
146
147struct tv32 {
148 u_int32_t tv32_sec;
149 u_int32_t tv32_usec;
150};
151
152#define MAXPACKETLEN 131072
153#define IP6LEN 40
154#define ICMP6ECHOLEN 8 /* icmp echo header len excluding time */
155#define ICMP6ECHOTMLEN sizeof(struct tv32)
156#define ICMP6_NIQLEN (ICMP6ECHOLEN + 8)
157# define CONTROLLEN 10240 /* ancillary data buffer size RFC3542 20.1 */
158/* FQDN case, 64 bits of nonce + 32 bits ttl */
159#define ICMP6_NIRLEN (ICMP6ECHOLEN + 12)
160#define EXTRA 256 /* for AH and various other headers. weird. */
161#define DEFDATALEN ICMP6ECHOTMLEN
162#define MAXDATALEN MAXPACKETLEN - IP6LEN - ICMP6ECHOLEN
163#define NROUTES 9 /* number of record route slots */
164
165#define A(bit) rcvd_tbl[(bit)>>3] /* identify byte in array */
166#define B(bit) (1 << ((bit) & 0x07)) /* identify bit in byte */
167#define SET(bit) (A(bit) |= B(bit))
168#define CLR(bit) (A(bit) &= (~B(bit)))
169#define TST(bit) (A(bit) & B(bit))
170
171#define F_FLOOD 0x0001
172#define F_INTERVAL 0x0002
173#define F_PINGFILLED 0x0008
174#define F_QUIET 0x0010
175#define F_RROUTE 0x0020
176#define F_SO_DEBUG 0x0040
177#define F_VERBOSE 0x0100
178#ifdef IPSEC
179#ifdef IPSEC_POLICY_IPSEC
180#define F_POLICY 0x0400
181#else
182#define F_AUTHHDR 0x0200
183#define F_ENCRYPT 0x0400
184#endif /*IPSEC_POLICY_IPSEC*/
185#endif /*IPSEC*/
186#define F_NODEADDR 0x0800
187#define F_FQDN 0x1000
188#define F_INTERFACE 0x2000
189#define F_SRCADDR 0x4000
190#define F_HOSTNAME 0x10000
191#define F_FQDNOLD 0x20000
192#define F_NIGROUP 0x40000
193#define F_SUPTYPES 0x80000
194#define F_NOMINMTU 0x100000
195#define F_ONCE 0x200000
196#define F_NOUSERDATA (F_NODEADDR | F_FQDN | F_FQDNOLD | F_SUPTYPES)
197u_int options;
198
199#define IN6LEN sizeof(struct in6_addr)
200#define SA6LEN sizeof(struct sockaddr_in6)
201#define DUMMY_PORT 10101
202
203#define SIN6(s) ((struct sockaddr_in6 *)(s))
204
205#define MAXTOS 255
206
207/*
208 * MAX_DUP_CHK is the number of bits in received table, i.e. the maximum
209 * number of received sequence numbers we can keep track of. Change 128
210 * to 8192 for complete accuracy...
211 */
212#define MAX_DUP_CHK (8 * 8192)
213int mx_dup_ck = MAX_DUP_CHK;
214char rcvd_tbl[MAX_DUP_CHK / 8];
215
216struct addrinfo *res;
217struct sockaddr_in6 dst; /* who to ping6 */
218struct sockaddr_in6 src; /* src addr of this packet */
219socklen_t srclen;
220int datalen = DEFDATALEN;
221int s; /* socket file descriptor */
222u_char outpack[MAXPACKETLEN];
223char BSPACE = '\b'; /* characters written for flood */
224char DOT = '.';
225char *hostname;
226int ident; /* process id to identify our packets */
227u_int8_t nonce[8]; /* nonce field for node information */
228int hoplimit = -1; /* hoplimit */
229int pathmtu = 0; /* path MTU for the destination. 0 = unspec. */
230
231/* counters */
232long npackets; /* max packets to transmit */
233long nreceived; /* # of packets we got back */
234long nrepeats; /* number of duplicates */
235long ntransmitted; /* sequence # for outbound packets = #sent */
236long nmissed; /* # of packet missed */
237struct timeval interval = {1, 0}; /* interval between packets */
238
239/* timing */
240int timing; /* flag to do timing */
241double tmin = 999999999.0; /* minimum round trip time */
242double tmax = 0.0; /* maximum round trip time */
243double tsum = 0.0; /* sum of all times, for doing average */
244double tsumsq = 0.0; /* sum of all times squared, for std. dev. */
245
246/* for node addresses */
247u_short naflags;
248
249/* for ancillary data(advanced API) */
250struct msghdr smsghdr;
251struct iovec smsgiov;
252char *scmsg = 0;
253
254volatile sig_atomic_t seenalrm;
255volatile sig_atomic_t seenint;
256#ifdef SIGINFO
257volatile sig_atomic_t seeninfo;
258#endif
259
260int rcvtclass = 0;
261
262int main(int, char *[]);
263void fill(char *, char *);
264int get_hoplim(struct msghdr *);
265int get_pathmtu(struct msghdr *);
266int get_tclass(struct msghdr *);
267struct in6_pktinfo *get_rcvpktinfo(struct msghdr *);
268void onsignal(int);
269void retransmit(void);
270void onint(int);
271size_t pingerlen(void);
272int pinger(void);
273const char *pr_addr(struct sockaddr *, int);
274void pr_icmph(struct icmp6_hdr *, u_char *);
275void pr_iph(struct ip6_hdr *);
276void pr_suptypes(struct icmp6_nodeinfo *, size_t);
277void pr_nodeaddr(struct icmp6_nodeinfo *, int);
278int myechoreply(const struct icmp6_hdr *);
279int mynireply(const struct icmp6_nodeinfo *);
280char *dnsdecode(const u_char **, const u_char *, const u_char *,
281 char *, size_t);
282void pr_pack(u_char *, int, struct msghdr *);
283void pr_exthdrs(struct msghdr *);
284void pr_ip6opt(void *, size_t);
285void pr_rthdr(void *, size_t);
286int pr_bitrange(u_int32_t, int, int);
287void pr_retip(struct ip6_hdr *, u_char *);
288void summary(void);
289void tvsub(struct timeval *, struct timeval *);
290int setpolicy(int, char *);
291char *nigroup(char *);
292void usage(void);
293
294int
295main(argc, argv)
296 int argc;
297 char *argv[];
298{
299 struct itimerval itimer;
300 struct sockaddr_in6 from;
301#ifndef HAVE_ARC4RANDOM
302 struct timeval seed;
303#endif
304#ifdef HAVE_POLL_H
305 int timeout;
306#else
307 struct timeval timeout, *tv;
308#endif
309 struct addrinfo hints;
310#ifdef HAVE_POLL_H
311 struct pollfd fdmaskp[1];
312#else
313 fd_set *fdmaskp;
314 int fdmasks;
315#endif
316 int cc, i;
317 int ch, hold, packlen, preload, optval, ret_ga;
318 u_char *datap, *packet;
319 char *e, *target, *ifname = NULL, *gateway = NULL;
320 int ip6optlen = 0;
321 struct cmsghdr *scmsgp = NULL;
322 struct cmsghdr *cm;
323#if defined(SO_SNDBUF) && defined(SO_RCVBUF)
324 u_long lsockbufsize;
325 int sockbufsize = 0;
326#endif
327 int usepktinfo = 0;
328 struct in6_pktinfo *pktinfo = NULL;
329#ifdef USE_RFC2292BIS
330 struct ip6_rthdr *rthdr = NULL;
331#endif
332#ifdef IPSEC_POLICY_IPSEC
333 char *policy_in = NULL;
334 char *policy_out = NULL;
335#endif
336 double intval;
337 size_t rthlen;
338#ifdef IPV6_USE_MIN_MTU
339 int mflag = 0;
340#endif
341 int tclass = -2; /* T_CLASS value -1 means default, so -2 means do not bother */
342
343 /* just to be sure */
344 memset(&smsghdr, 0, sizeof(smsghdr));
345 memset(&smsgiov, 0, sizeof(smsgiov));
346
347 preload = 0;
348 datap = &outpack[ICMP6ECHOLEN + ICMP6ECHOTMLEN];
349#ifndef IPSEC
350#define ADDOPTS
351#else
352#ifdef IPSEC_POLICY_IPSEC
353#define ADDOPTS "P:"
354#else
355#define ADDOPTS "AE"
356#endif /*IPSEC_POLICY_IPSEC*/
357#endif
358 while ((ch = getopt(argc, argv,
359 "a:b:c:dfHg:h:I:i:l:mnNop:qS:s:tvwWz:" ADDOPTS)) != -1) {
360#undef ADDOPTS
361 switch (ch) {
362 case 'a':
363 {
364 char *cp;
365
366 options &= ~F_NOUSERDATA;
367 options |= F_NODEADDR;
368 for (cp = optarg; *cp != '\0'; cp++) {
369 switch (*cp) {
370 case 'a':
371 naflags |= NI_NODEADDR_FLAG_ALL;
372 break;
373 case 'c':
374 case 'C':
375 naflags |= NI_NODEADDR_FLAG_COMPAT;
376 break;
377 case 'l':
378 case 'L':
379 naflags |= NI_NODEADDR_FLAG_LINKLOCAL;
380 break;
381 case 's':
382 case 'S':
383 naflags |= NI_NODEADDR_FLAG_SITELOCAL;
384 break;
385 case 'g':
386 case 'G':
387 naflags |= NI_NODEADDR_FLAG_GLOBAL;
388 break;
389 case 'A': /* experimental. not in the spec */
390#ifdef NI_NODEADDR_FLAG_ANYCAST
391 naflags |= NI_NODEADDR_FLAG_ANYCAST;
392 break;
393#else
394 errx(1,
395"-a A is not supported on the platform");
396 /*NOTREACHED*/
397#endif
398 default:
399 usage();
400 /*NOTREACHED*/
401 }
402 }
403 break;
404 }
405 case 'b':
406#if defined(SO_SNDBUF) && defined(SO_RCVBUF)
407 errno = 0;
408 e = NULL;
409 lsockbufsize = strtoul(optarg, &e, 10);
410 sockbufsize = lsockbufsize;
411 if (errno || !*optarg || *e ||
412 sockbufsize != lsockbufsize)
413 errx(1, "invalid socket buffer size");
414#else
415 errx(1,
416"-b option ignored: SO_SNDBUF/SO_RCVBUF socket options not supported");
417#endif
418 break;
419 case 'c':
420 npackets = strtol(optarg, &e, 10);
421 if (npackets <= 0 || *optarg == '\0' || *e != '\0')
422 errx(1,
423 "illegal number of packets -- %s", optarg);
424 break;
425 case 'd':
426 options |= F_SO_DEBUG;
427 break;
428 case 'f':
429 if (getuid()) {
430 errno = EPERM;
431 errx(1, "Must be superuser to flood ping");
432 }
433 options |= F_FLOOD;
434 setbuf(stdout, (char *)NULL);
435 break;
436 case 'g':
437 gateway = optarg;
438 break;
439 case 'H':
440 options |= F_HOSTNAME;
441 break;
442 case 'h': /* hoplimit */
443 hoplimit = strtol(optarg, &e, 10);
444 if (*optarg == '\0' || *e != '\0')
445 errx(1, "illegal hoplimit %s", optarg);
446 if (255 < hoplimit || hoplimit < -1)
447 errx(1,
448 "illegal hoplimit -- %s", optarg);
449 break;
450 case 'I':
451 ifname = optarg;
452 options |= F_INTERFACE;
453#ifndef USE_SIN6_SCOPE_ID
454 usepktinfo++;
455#endif
456 break;
457 case 'i': /* wait between sending packets */
458 intval = strtod(optarg, &e);
459 if (*optarg == '\0' || *e != '\0')
460 errx(1, "illegal timing interval %s", optarg);
461 if (intval < 1 && getuid()) {
462 errx(1, "%s: only root may use interval < 1s",
463 strerror(EPERM));
464 }
465 interval.tv_sec = (long)intval;
466 interval.tv_usec =
467 (long)((intval - interval.tv_sec) * 1000000);
468 if (interval.tv_sec < 0)
469 errx(1, "illegal timing interval %s", optarg);
470 /* less than 1/hz does not make sense */
471 if (interval.tv_sec == 0 && interval.tv_usec < 1) {
472 warnx("too small interval, raised to .000001");
473 interval.tv_usec = 1;
474 }
475 options |= F_INTERVAL;
476 break;
477 case 'l':
478 if (getuid()) {
479 errno = EPERM;
480 errx(1, "Must be superuser to preload");
481 }
482 preload = strtol(optarg, &e, 10);
483 if (preload < 0 || *optarg == '\0' || *e != '\0')
484 errx(1, "illegal preload value -- %s", optarg);
485 break;
486 case 'm':
487#ifdef IPV6_USE_MIN_MTU
488 mflag++;
489 break;
490#else
491 errx(1, "-%c is not supported on this platform", ch);
492 /*NOTREACHED*/
493#endif
494 case 'n':
495 options &= ~F_HOSTNAME;
496 break;
497 case 'N':
498 options |= F_NIGROUP;
499 break;
500 case 'o':
501 options |= F_ONCE;
502 break;
503 case 'p': /* fill buffer with user pattern */
504 options |= F_PINGFILLED;
505 fill((char *)datap, optarg);
506 break;
507 case 'q':
508 options |= F_QUIET;
509 break;
510 case 'S':
511 memset(&hints, 0, sizeof(struct addrinfo));
512 hints.ai_flags = AI_NUMERICHOST; /* allow hostname? */
513 hints.ai_family = AF_INET6;
514 hints.ai_socktype = SOCK_RAW;
515 hints.ai_protocol = IPPROTO_ICMPV6;
516
517 ret_ga = getaddrinfo(optarg, NULL, &hints, &res);
518 if (ret_ga) {
519 errx(1, "invalid source address: %s",
520 gai_strerror(ret_ga));
521 }
522 /*
523 * res->ai_family must be AF_INET6 and res->ai_addrlen
524 * must be sizeof(src).
525 */
526 memcpy(&src, res->ai_addr, res->ai_addrlen);
527 srclen = res->ai_addrlen;
528 freeaddrinfo(res);
529 options |= F_SRCADDR;
530 break;
531 case 's': /* size of packet to send */
532 datalen = strtol(optarg, &e, 10);
533 if (datalen <= 0 || *optarg == '\0' || *e != '\0')
534 errx(1, "illegal datalen value -- %s", optarg);
535 if (datalen > MAXDATALEN) {
536 errx(1,
537 "datalen value too large, maximum is %d",
538 MAXDATALEN);
539 }
540 break;
541 case 't':
542 options &= ~F_NOUSERDATA;
543 options |= F_SUPTYPES;
544 break;
545 case 'v':
546 options |= F_VERBOSE;
547 break;
548 case 'w':
549 options &= ~F_NOUSERDATA;
550 options |= F_FQDN;
551 break;
552 case 'W':
553 options &= ~F_NOUSERDATA;
554 options |= F_FQDNOLD;
555 break;
556 case 'z':
557 tclass = (int)strtol(optarg, &e, 10);
558 if (tclass < -1 || *optarg == '\0' || *e != '\0')
559 errx(1, "illegal TOS value -- %s", optarg);
560 if (tclass > MAXTOS)
561 errx(1,
562 "TOS value too large, maximum is %d",
563 MAXTOS);
564 rcvtclass = 1;
565 break;
566#ifdef IPSEC
567#ifdef IPSEC_POLICY_IPSEC
568 case 'P':
569 options |= F_POLICY;
570 if (!strncmp("in", optarg, 2)) {
571 if ((policy_in = strdup(optarg)) == NULL)
572 errx(1, "strdup");
573 } else if (!strncmp("out", optarg, 3)) {
574 if ((policy_out = strdup(optarg)) == NULL)
575 errx(1, "strdup");
576 } else
577 errx(1, "invalid security policy");
578 break;
579#else
580 case 'A':
581 options |= F_AUTHHDR;
582 break;
583 case 'E':
584 options |= F_ENCRYPT;
585 break;
586#endif /*IPSEC_POLICY_IPSEC*/
587#endif /*IPSEC*/
588 default:
589 usage();
590 /*NOTREACHED*/
591 }
592 }
593
594 argc -= optind;
595 argv += optind;
596
597 if (argc < 1) {
598 usage();
599 /*NOTREACHED*/
600 }
601
602 if (argc > 1) {
603#ifdef IPV6_RECVRTHDR /* 2292bis */
604 rthlen = CMSG_SPACE(inet6_rth_space(IPV6_RTHDR_TYPE_0,
605 argc - 1));
606#else /* RFC2292 */
607 rthlen = inet6_rthdr_space(IPV6_RTHDR_TYPE_0, argc - 1);
608#endif
609 if (rthlen == 0) {
610 errx(1, "too many intermediate hops");
611 /*NOTREACHED*/
612 }
613 ip6optlen += rthlen;
614 }
615
616 if (options & F_NIGROUP) {
617 target = nigroup(argv[argc - 1]);
618 if (target == NULL) {
619 usage();
620 /*NOTREACHED*/
621 }
622 } else
623 target = argv[argc - 1];
624
625 /* getaddrinfo */
626 memset(&hints, 0, sizeof(struct addrinfo));
627 hints.ai_flags = AI_CANONNAME;
628 hints.ai_family = AF_INET6;
629 /* XXX getaddrinfo does like SOCK_DGRAM for IPPROTO_ICMPV6 */
630 hints.ai_socktype = SOCK_RAW;
631 hints.ai_protocol = IPPROTO_ICMPV6;
632
633 ret_ga = getaddrinfo(target, NULL, &hints, &res);
634 if (ret_ga)
635 errx(1, "getaddrinfo -- %s", gai_strerror(ret_ga));
636 if (res->ai_canonname)
637 hostname = res->ai_canonname;
638 else
639 hostname = target;
640
641 if (!res->ai_addr)
642 errx(1, "getaddrinfo failed");
643
644 (void)memcpy(&dst, res->ai_addr, res->ai_addrlen);
645
646 res->ai_socktype = getuid() ? SOCK_DGRAM : SOCK_RAW;
647 res->ai_protocol = IPPROTO_ICMPV6;
648
649 if ((s = socket(res->ai_family, res->ai_socktype,
650 res->ai_protocol)) < 0)
651 err(1, "socket");
652
653 /* set the source address if specified. */
654 if ((options & F_SRCADDR) &&
655 bind(s, (struct sockaddr *)&src, srclen) != 0) {
656 err(1, "bind");
657 }
658
659 /* set the gateway (next hop) if specified */
660 if (gateway) {
661 struct addrinfo ghints, *gres;
662 int error;
663
664 memset(&ghints, 0, sizeof(ghints));
665 ghints.ai_family = AF_INET6;
666 ghints.ai_socktype = SOCK_RAW;
667 ghints.ai_protocol = IPPROTO_ICMPV6;
668
669 error = getaddrinfo(gateway, NULL, &hints, &gres);
670 if (error) {
671 errx(1, "getaddrinfo for the gateway %s: %s",
672 gateway, gai_strerror(error));
673 }
674 if (gres->ai_next && (options & F_VERBOSE))
675 warnx("gateway resolves to multiple addresses");
676
677 if (setsockopt(s, IPPROTO_IPV6, IPV6_NEXTHOP,
678 gres->ai_addr, gres->ai_addrlen)) {
679 err(1, "setsockopt(IPV6_NEXTHOP)");
680 }
681
682 freeaddrinfo(gres);
683 }
684
685 /*
686 * let the kerel pass extension headers of incoming packets,
687 * for privileged socket options
688 */
689 if ((options & F_VERBOSE) != 0) {
690 int opton = 1;
691
692#ifdef IPV6_RECVHOPOPTS
693 if (setsockopt(s, IPPROTO_IPV6, IPV6_RECVHOPOPTS, &opton,
694 sizeof(opton)))
695 err(1, "setsockopt(IPV6_RECVHOPOPTS)");
696#else /* old adv. API */
697 if (setsockopt(s, IPPROTO_IPV6, IPV6_HOPOPTS, &opton,
698 sizeof(opton)))
699 err(1, "setsockopt(IPV6_HOPOPTS)");
700#endif
701#ifdef IPV6_RECVDSTOPTS
702 if (setsockopt(s, IPPROTO_IPV6, IPV6_RECVDSTOPTS, &opton,
703 sizeof(opton)))
704 err(1, "setsockopt(IPV6_RECVDSTOPTS)");
705#else /* old adv. API */
706 if (setsockopt(s, IPPROTO_IPV6, IPV6_DSTOPTS, &opton,
707 sizeof(opton)))
708 err(1, "setsockopt(IPV6_DSTOPTS)");
709#endif
710#ifdef IPV6_RECVRTHDRDSTOPTS
711 if (setsockopt(s, IPPROTO_IPV6, IPV6_RECVRTHDRDSTOPTS, &opton,
712 sizeof(opton)))
713 err(1, "setsockopt(IPV6_RECVRTHDRDSTOPTS)");
714#endif
715 }
716
717 /* revoke root privilege */
718 seteuid(getuid());
719 setuid(getuid());
720
721 if ((options & F_FLOOD) && (options & F_INTERVAL))
722 errx(1, "-f and -i incompatible options");
723
724 if ((options & F_NOUSERDATA) == 0) {
725 if (datalen >= sizeof(struct tv32)) {
726 /* we can time transfer */
727 timing = 1;
728 } else
729 timing = 0;
730 /* in F_VERBOSE case, we may get non-echoreply packets*/
731 if (options & F_VERBOSE)
732 packlen = 2048 + IP6LEN + ICMP6ECHOLEN + EXTRA;
733 else
734 packlen = datalen + IP6LEN + ICMP6ECHOLEN + EXTRA;
735 } else {
736 /* suppress timing for node information query */
737 timing = 0;
738 datalen = 2048;
739 packlen = 2048 + IP6LEN + ICMP6ECHOLEN + EXTRA;
740 }
741
742 if (!(packet = (u_char *)malloc((u_int)packlen)))
743 err(1, "Unable to allocate packet");
744 if (!(options & F_PINGFILLED))
745 for (i = ICMP6ECHOLEN; i < packlen; ++i)
746 *datap++ = i;
747
748 ident = getpid() & 0xFFFF;
749#ifndef HAVE_ARC4RANDOM
750 gettimeofday(&seed, NULL);
751 srand((unsigned int)(seed.tv_sec ^ seed.tv_usec ^ (long)ident));
752 memset(nonce, 0, sizeof(nonce));
753 for (i = 0; i < sizeof(nonce); i += sizeof(int))
754 *((int *)&nonce[i]) = rand();
755#else
756 memset(nonce, 0, sizeof(nonce));
757 for (i = 0; i < sizeof(nonce); i += sizeof(u_int32_t))
758 *((u_int32_t *)&nonce[i]) = arc4random();
759#endif
760
761 hold = 1;
762
763 if (options & F_SO_DEBUG)
764 (void)setsockopt(s, SOL_SOCKET, SO_DEBUG, (char *)&hold,
765 sizeof(hold));
766 optval = IPV6_DEFHLIM;
767 if (IN6_IS_ADDR_MULTICAST(&dst.sin6_addr))
768 if (setsockopt(s, IPPROTO_IPV6, IPV6_MULTICAST_HOPS,
769 &optval, sizeof(optval)) == -1)
770 err(1, "IPV6_MULTICAST_HOPS");
771#ifdef IPV6_USE_MIN_MTU
772 if (mflag != 1) {
773 optval = mflag > 1 ? 0 : 1;
774
775 if (setsockopt(s, IPPROTO_IPV6, IPV6_USE_MIN_MTU,
776 &optval, sizeof(optval)) == -1)
777 err(1, "setsockopt(IPV6_USE_MIN_MTU)");
778 }
779#ifdef IPV6_RECVPATHMTU
780 else {
781 optval = 1;
782 if (setsockopt(s, IPPROTO_IPV6, IPV6_RECVPATHMTU,
783 &optval, sizeof(optval)) == -1)
784 err(1, "setsockopt(IPV6_RECVPATHMTU)");
785 }
786#endif /* IPV6_RECVPATHMTU */
787#endif /* IPV6_USE_MIN_MTU */
788
789#ifdef IPSEC
790#ifdef IPSEC_POLICY_IPSEC
791 if (options & F_POLICY) {
792 if (setpolicy(s, policy_in) < 0)
793 errx(1, "%s", ipsec_strerror());
794 if (setpolicy(s, policy_out) < 0)
795 errx(1, "%s", ipsec_strerror());
796 }
797#else
798 if (options & F_AUTHHDR) {
799 optval = IPSEC_LEVEL_REQUIRE;
800#ifdef IPV6_AUTH_TRANS_LEVEL
801 if (setsockopt(s, IPPROTO_IPV6, IPV6_AUTH_TRANS_LEVEL,
802 &optval, sizeof(optval)) == -1)
803 err(1, "setsockopt(IPV6_AUTH_TRANS_LEVEL)");
804#else /* old def */
805 if (setsockopt(s, IPPROTO_IPV6, IPV6_AUTH_LEVEL,
806 &optval, sizeof(optval)) == -1)
807 err(1, "setsockopt(IPV6_AUTH_LEVEL)");
808#endif
809 }
810 if (options & F_ENCRYPT) {
811 optval = IPSEC_LEVEL_REQUIRE;
812 if (setsockopt(s, IPPROTO_IPV6, IPV6_ESP_TRANS_LEVEL,
813 &optval, sizeof(optval)) == -1)
814 err(1, "setsockopt(IPV6_ESP_TRANS_LEVEL)");
815 }
816#endif /*IPSEC_POLICY_IPSEC*/
817#endif
818
819#ifdef ICMP6_FILTER
820 {
821 struct icmp6_filter filt;
822 if (!(options & F_VERBOSE)) {
823 ICMP6_FILTER_SETBLOCKALL(&filt);
824 if ((options & F_FQDN) || (options & F_FQDNOLD) ||
825 (options & F_NODEADDR) || (options & F_SUPTYPES))
826 ICMP6_FILTER_SETPASS(ICMP6_NI_REPLY, &filt);
827 else
828 ICMP6_FILTER_SETPASS(ICMP6_ECHO_REPLY, &filt);
829 } else {
830 ICMP6_FILTER_SETPASSALL(&filt);
831 }
832 if (setsockopt(s, IPPROTO_ICMPV6, ICMP6_FILTER, &filt,
833 sizeof(filt)) < 0)
834 err(1, "setsockopt(ICMP6_FILTER)");
835 }
836#endif /*ICMP6_FILTER*/
837
838 /* let the kerel pass extension headers of incoming packets */
839 if ((options & F_VERBOSE) != 0) {
840 int opton = 1;
841
842#ifdef IPV6_RECVRTHDR
843 if (setsockopt(s, IPPROTO_IPV6, IPV6_RECVRTHDR, &opton,
844 sizeof(opton)))
845 err(1, "setsockopt(IPV6_RECVRTHDR)");
846#else /* old adv. API */
847 if (setsockopt(s, IPPROTO_IPV6, IPV6_RTHDR, &opton,
848 sizeof(opton)))
849 err(1, "setsockopt(IPV6_RTHDR)");
850#endif
851 }
852
853 if (tclass != -2) {
854 int on = 1;
855
856 if (setsockopt(s, IPPROTO_IPV6, IPV6_RECVTCLASS, &on, sizeof(on)))
857 err(1, "setsockopt(IPV6_RECVTCLASS)");
858
859 if (setsockopt(s, IPPROTO_IPV6, IPV6_TCLASS, &tclass, sizeof(tclass)))
860 err(1, "setsockopt(IPV6_TCLASS)");
861 }
862
863/*
864 optval = 1;
865 if (IN6_IS_ADDR_MULTICAST(&dst.sin6_addr))
866 if (setsockopt(s, IPPROTO_IPV6, IPV6_MULTICAST_LOOP,
867 &optval, sizeof(optval)) == -1)
868 err(1, "IPV6_MULTICAST_LOOP");
869*/
870
871 /* Specify the outgoing interface and/or the source address */
872 if (usepktinfo)
873 ip6optlen += CMSG_SPACE(sizeof(struct in6_pktinfo));
874
875 if (hoplimit != -1)
876 ip6optlen += CMSG_SPACE(sizeof(int));
877
878 if (tclass != -2)
879 ip6optlen += CMSG_SPACE(sizeof(int));
880
881 /* set IP6 packet options */
882 if (ip6optlen) {
883 if ((scmsg = (char *)malloc(ip6optlen)) == 0)
884 errx(1, "can't allocate enough memory");
885 smsghdr.msg_control = (caddr_t)scmsg;
886 smsghdr.msg_controllen = ip6optlen;
887 scmsgp = (struct cmsghdr *)scmsg;
888 }
889 if (usepktinfo) {
890 pktinfo = (struct in6_pktinfo *)(CMSG_DATA(scmsgp));
891 memset(pktinfo, 0, sizeof(*pktinfo));
892 scmsgp->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo));
893 scmsgp->cmsg_level = IPPROTO_IPV6;
894 scmsgp->cmsg_type = IPV6_PKTINFO;
895 scmsgp = CMSG_NXTHDR(&smsghdr, scmsgp);
896 }
897
898 /* set the outgoing interface */
899 if (ifname) {
900#ifndef USE_SIN6_SCOPE_ID
901 /* pktinfo must have already been allocated */
902 if ((pktinfo->ipi6_ifindex = if_nametoindex(ifname)) == 0)
903 errx(1, "%s: invalid interface name", ifname);
904#else
905 if ((dst.sin6_scope_id = if_nametoindex(ifname)) == 0)
906 errx(1, "%s: invalid interface name", ifname);
907#endif
908 }
909 if (hoplimit != -1) {
910 scmsgp->cmsg_len = CMSG_LEN(sizeof(int));
911 scmsgp->cmsg_level = IPPROTO_IPV6;
912 scmsgp->cmsg_type = IPV6_HOPLIMIT;
913 *(int *)(CMSG_DATA(scmsgp)) = hoplimit;
914
915 scmsgp = CMSG_NXTHDR(&smsghdr, scmsgp);
916 }
917
918 if (argc > 1) { /* some intermediate addrs are specified */
919 int hops, error;
920#ifdef USE_RFC2292BIS
921 int rthdrlen;
922#endif
923
924#ifdef USE_RFC2292BIS
925 rthdrlen = inet6_rth_space(IPV6_RTHDR_TYPE_0, argc - 1);
926 scmsgp->cmsg_len = CMSG_LEN(rthdrlen);
927 scmsgp->cmsg_level = IPPROTO_IPV6;
928 scmsgp->cmsg_type = IPV6_RTHDR;
929 rthdr = (struct ip6_rthdr *)CMSG_DATA(scmsgp);
930 rthdr = inet6_rth_init((void *)rthdr, rthdrlen,
931 IPV6_RTHDR_TYPE_0, argc - 1);
932 if (rthdr == NULL)
933 errx(1, "can't initialize rthdr");
934#else /* old advanced API */
935 if ((scmsgp = (struct cmsghdr *)inet6_rthdr_init(scmsgp,
936 IPV6_RTHDR_TYPE_0)) == 0)
937 errx(1, "can't initialize rthdr");
938#endif /* USE_RFC2292BIS */
939
940 for (hops = 0; hops < argc - 1; hops++) {
941 struct addrinfo *iaip;
942
943 if ((error = getaddrinfo(argv[hops], NULL, &hints,
944 &iaip)))
945 errx(1, "%s", gai_strerror(error));
946 if (SIN6(iaip->ai_addr)->sin6_family != AF_INET6)
947 errx(1,
948 "bad addr family of an intermediate addr");
949
950#ifdef USE_RFC2292BIS
951 if (inet6_rth_add(rthdr,
952 &(SIN6(iaip->ai_addr))->sin6_addr))
953 errx(1, "can't add an intermediate node");
954#else /* old advanced API */
955 if (inet6_rthdr_add(scmsgp,
956 &(SIN6(iaip->ai_addr))->sin6_addr,
957 IPV6_RTHDR_LOOSE))
958 errx(1, "can't add an intermediate node");
959#endif /* USE_RFC2292BIS */
960 freeaddrinfo(iaip);
961 }
962
963#ifndef USE_RFC2292BIS
964 if (inet6_rthdr_lasthop(scmsgp, IPV6_RTHDR_LOOSE))
965 errx(1, "can't set the last flag");
966#endif
967
968 scmsgp = CMSG_NXTHDR(&smsghdr, scmsgp);
969 }
970
971 if (tclass != -2) {
972 scmsgp->cmsg_len = CMSG_LEN(sizeof(int));
973 scmsgp->cmsg_level = IPPROTO_IPV6;
974 scmsgp->cmsg_type = IPV6_TCLASS;
975 *(int *)(CMSG_DATA(scmsgp)) = tclass;
976
977 scmsgp = CMSG_NXTHDR(&smsghdr, scmsgp);
978 }
979
980 if (!(options & F_SRCADDR)) {
981 /*
982 * get the source address. XXX since we revoked the root
983 * privilege, we cannot use a raw socket for this.
984 */
985 int dummy;
986 socklen_t len = sizeof(src);
987
988 if ((dummy = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
989 err(1, "UDP socket");
990
991 src.sin6_family = AF_INET6;
992 src.sin6_addr = dst.sin6_addr;
993 src.sin6_port = ntohs(DUMMY_PORT);
994 src.sin6_scope_id = dst.sin6_scope_id;
995
996#ifdef USE_RFC2292BIS
997 if (pktinfo &&
998 setsockopt(dummy, IPPROTO_IPV6, IPV6_PKTINFO,
999 (void *)pktinfo, sizeof(*pktinfo)))
1000 err(1, "UDP setsockopt(IPV6_PKTINFO)");
1001
1002 if (hoplimit != -1 &&
1003 setsockopt(dummy, IPPROTO_IPV6, IPV6_UNICAST_HOPS,
1004 (void *)&hoplimit, sizeof(hoplimit)))
1005 err(1, "UDP setsockopt(IPV6_UNICAST_HOPS)");
1006
1007 if (hoplimit != -1 &&
1008 setsockopt(dummy, IPPROTO_IPV6, IPV6_MULTICAST_HOPS,
1009 (void *)&hoplimit, sizeof(hoplimit)))
1010 err(1, "UDP setsockopt(IPV6_MULTICAST_HOPS)");
1011
1012 if (rthdr &&
1013 setsockopt(dummy, IPPROTO_IPV6, IPV6_RTHDR,
1014 (void *)rthdr, (rthdr->ip6r_len + 1) << 3))
1015 err(1, "UDP setsockopt(IPV6_RTHDR)");
1016#else /* old advanced API */
1017 if (smsghdr.msg_control &&
1018 setsockopt(dummy, IPPROTO_IPV6, IPV6_PKTOPTIONS,
1019 (void *)smsghdr.msg_control, smsghdr.msg_controllen))
1020 err(1, "UDP setsockopt(IPV6_PKTOPTIONS)");
1021#endif
1022
1023 if (connect(dummy, (struct sockaddr *)&src, len) < 0)
1024 err(1, "UDP connect");
1025
1026 if (getsockname(dummy, (struct sockaddr *)&src, &len) < 0)
1027 err(1, "getsockname");
1028
1029 close(dummy);
1030 }
1031
1032#if defined(SO_SNDBUF) && defined(SO_RCVBUF)
1033 if (sockbufsize) {
1034 if (datalen > sockbufsize)
1035 warnx("you need -b to increase socket buffer size");
1036 if (setsockopt(s, SOL_SOCKET, SO_SNDBUF, &sockbufsize,
1037 sizeof(sockbufsize)) < 0)
1038 err(1, "setsockopt(SO_SNDBUF)");
1039 if (setsockopt(s, SOL_SOCKET, SO_RCVBUF, &sockbufsize,
1040 sizeof(sockbufsize)) < 0)
1041 err(1, "setsockopt(SO_RCVBUF)");
1042 }
1043 else {
1044 if (datalen > 8 * 1024) /*XXX*/
1045 warnx("you need -b to increase socket buffer size");
1046 /*
1047 * When pinging the broadcast address, you can get a lot of
1048 * answers. Doing something so evil is useful if you are trying
1049 * to stress the ethernet, or just want to fill the arp cache
1050 * to get some stuff for /etc/ethers.
1051 */
1052 hold = 48 * 1024;
1053 setsockopt(s, SOL_SOCKET, SO_RCVBUF, (char *)&hold,
1054 sizeof(hold));
1055 }
1056#endif
1057
1058 optval = 1;
1059#ifndef USE_SIN6_SCOPE_ID
1060#ifdef IPV6_RECVPKTINFO
1061 if (setsockopt(s, IPPROTO_IPV6, IPV6_RECVPKTINFO, &optval,
1062 sizeof(optval)) < 0)
1063 warn("setsockopt(IPV6_RECVPKTINFO)"); /* XXX err? */
1064#else /* old adv. API */
1065 if (setsockopt(s, IPPROTO_IPV6, IPV6_PKTINFO, &optval,
1066 sizeof(optval)) < 0)
1067 warn("setsockopt(IPV6_PKTINFO)"); /* XXX err? */
1068#endif
1069#endif /* USE_SIN6_SCOPE_ID */
1070#ifdef IPV6_RECVHOPLIMIT
1071 if (setsockopt(s, IPPROTO_IPV6, IPV6_RECVHOPLIMIT, &optval,
1072 sizeof(optval)) < 0)
1073 warn("setsockopt(IPV6_RECVHOPLIMIT)"); /* XXX err? */
1074#else /* old adv. API */
1075 if (setsockopt(s, IPPROTO_IPV6, IPV6_HOPLIMIT, &optval,
1076 sizeof(optval)) < 0)
1077 warn("setsockopt(IPV6_HOPLIMIT, %d, %u)", optval, sizeof(optval)); /* XXX err? */
1078#endif
1079
1080 printf("PING6(%lu=40+8+%lu bytes) ", (unsigned long)(40 + pingerlen()),
1081 (unsigned long)(pingerlen() - 8));
1082 printf("%s --> ", pr_addr((struct sockaddr *)&src, sizeof(src)));
1083 printf("%s\n", pr_addr((struct sockaddr *)&dst, sizeof(dst)));
1084
1085 while (preload--) /* Fire off them quickies. */
1086 (void)pinger();
1087
1088 (void)signal(SIGINT, onsignal);
1089#ifdef SIGINFO
1090 (void)signal(SIGINFO, onsignal);
1091#endif
1092
1093 if ((options & F_FLOOD) == 0) {
1094 (void)signal(SIGALRM, onsignal);
1095 itimer.it_interval = interval;
1096 itimer.it_value = interval;
1097 (void)setitimer(ITIMER_REAL, &itimer, NULL);
1098 if (ntransmitted == 0)
1099 retransmit();
1100 }
1101
1102#ifndef HAVE_POLL_H
1103 fdmasks = howmany(s + 1, NFDBITS) * sizeof(fd_mask);
1104 if ((fdmaskp = malloc(fdmasks)) == NULL)
1105 err(1, "malloc");
1106#endif
1107
1108 seenalrm = seenint = 0;
1109#ifdef SIGINFO
1110 seeninfo = 0;
1111#endif
1112
1113 /* For control (ancillary) data received from recvmsg() */
1114 cm = (struct cmsghdr *)malloc(CONTROLLEN);
1115 if (cm == NULL)
1116 err(1, "malloc");
1117
1118 for (;;) {
1119 struct msghdr m;
1120 struct iovec iov[2];
1121
1122 /* signal handling */
1123 if (seenalrm) {
1124 if (ntransmitted - nreceived > nmissed) {
1125 nmissed++;
1126 if (!(options & F_QUIET))
1127 printf("Request timeout for icmp_seq=%ld\n", ntransmitted - 1);
1128 }
1129 retransmit();
1130 seenalrm = 0;
1131 continue;
1132 }
1133 if (seenint) {
1134 onint(SIGINT);
1135 seenint = 0;
1136 continue;
1137 }
1138#ifdef SIGINFO
1139 if (seeninfo) {
1140 summary();
1141 seeninfo = 0;
1142 continue;
1143 }
1144#endif
1145
1146 if (options & F_FLOOD) {
1147 (void)pinger();
1148#ifdef HAVE_POLL_H
1149 timeout = 10;
1150#else
1151 timeout.tv_sec = 0;
1152 timeout.tv_usec = 10000;
1153 tv = &timeout;
1154#endif
1155 } else {
1156#ifdef HAVE_POLL_H
1157 timeout = INFTIM;
1158#else
1159 tv = NULL;
1160#endif
1161 }
1162#ifdef HAVE_POLL_H
1163 fdmaskp[0].fd = s;
1164 fdmaskp[0].events = POLLIN;
1165 cc = poll(fdmaskp, 1, timeout);
1166#else
1167 memset(fdmaskp, 0, fdmasks);
1168 FD_SET(s, fdmaskp);
1169 cc = select(s + 1, fdmaskp, NULL, NULL, tv);
1170#endif
1171 if (cc < 0) {
1172 if (errno != EINTR) {
1173#ifdef HAVE_POLL_H
1174 warn("poll");
1175#else
1176 warn("select");
1177#endif
1178 sleep(1);
1179 }
1180 continue;
1181 } else if (cc == 0)
1182 continue;
1183
1184 m.msg_name = (caddr_t)&from;
1185 m.msg_namelen = sizeof(from);
1186 memset(&iov, 0, sizeof(iov));
1187 iov[0].iov_base = (caddr_t)packet;
1188 iov[0].iov_len = packlen;
1189 m.msg_iov = iov;
1190 m.msg_iovlen = 1;
1191 memset(cm, 0, CONTROLLEN);
1192 m.msg_control = (void *)cm;
1193 m.msg_controllen = CONTROLLEN;
1194
1195 cc = recvmsg(s, &m, 0);
1196 if (cc < 0) {
1197 if (errno != EINTR) {
1198 warn("recvmsg");
1199 sleep(1);
1200 }
1201 continue;
1202 } else if (cc == 0) {
1203 int mtu;
1204
1205 /*
1206 * receive control messages only. Process the
1207 * exceptions (currently the only possiblity is
1208 * a path MTU notification.)
1209 */
1210 if ((mtu = get_pathmtu(&m)) > 0) {
1211 if ((options & F_VERBOSE) != 0) {
1212 printf("new path MTU (%d) is "
1213 "notified\n", mtu);
1214 }
1215 }
1216 continue;
1217 } else {
1218 /*
1219 * an ICMPv6 message (probably an echoreply) arrived.
1220 */
1221 pr_pack(packet, cc, &m);
1222 }
1223 if (((options & F_ONCE) != 0 && nreceived > 0) ||
1224 (npackets > 0 && nreceived >= npackets))
1225 break;
1226 }
1227 summary();
1228 exit(nreceived == 0);
1229}
1230
1231void
1232onsignal(sig)
1233 int sig;
1234{
1235
1236 switch (sig) {
1237 case SIGALRM:
1238 seenalrm++;
1239 break;
1240 case SIGINT:
1241 seenint++;
1242 break;
1243#ifdef SIGINFO
1244 case SIGINFO:
1245 seeninfo++;
1246 break;
1247#endif
1248 }
1249}
1250
1251/*
1252 * retransmit --
1253 * This routine transmits another ping6.
1254 */
1255void
1256retransmit()
1257{
1258 struct itimerval itimer;
1259
1260 if (pinger() == 0)
1261 return;
1262
1263 /*
1264 * If we're not transmitting any more packets, change the timer
1265 * to wait two round-trip times if we've received any packets or
1266 * ten seconds if we haven't.
1267 */
1268#define MAXWAIT 10
1269 if (nreceived) {
1270 itimer.it_value.tv_sec = 2 * tmax / 1000;
1271 if (itimer.it_value.tv_sec == 0)
1272 itimer.it_value.tv_sec = 1;
1273 } else
1274 itimer.it_value.tv_sec = MAXWAIT;
1275 itimer.it_interval.tv_sec = 0;
1276 itimer.it_interval.tv_usec = 0;
1277 itimer.it_value.tv_usec = 0;
1278
1279 (void)signal(SIGALRM, onint);
1280 (void)setitimer(ITIMER_REAL, &itimer, NULL);
1281}
1282
1283/*
1284 * pinger --
1285 * Compose and transmit an ICMP ECHO REQUEST packet. The IP packet
1286 * will be added on by the kernel. The ID field is our UNIX process ID,
1287 * and the sequence number is an ascending integer. The first 8 bytes
1288 * of the data portion are used to hold a UNIX "timeval" struct in VAX
1289 * byte-order, to compute the round-trip time.
1290 */
1291size_t
1292pingerlen()
1293{
1294 size_t l;
1295
1296 if (options & F_FQDN)
1297 l = ICMP6_NIQLEN + sizeof(dst.sin6_addr);
1298 else if (options & F_FQDNOLD)
1299 l = ICMP6_NIQLEN;
1300 else if (options & F_NODEADDR)
1301 l = ICMP6_NIQLEN + sizeof(dst.sin6_addr);
1302 else if (options & F_SUPTYPES)
1303 l = ICMP6_NIQLEN;
1304 else
1305 l = ICMP6ECHOLEN + datalen;
1306
1307 return l;
1308}
1309
1310int
1311pinger()
1312{
1313 struct icmp6_hdr *icp;
1314 struct iovec iov[2];
1315 int i, cc;
1316 struct icmp6_nodeinfo *nip;
1317 int seq;
1318
1319 if (npackets && ntransmitted >= npackets)
1320 return(-1); /* no more transmission */
1321
1322 icp = (struct icmp6_hdr *)outpack;
1323 nip = (struct icmp6_nodeinfo *)outpack;
1324 memset(icp, 0, sizeof(*icp));
1325 icp->icmp6_cksum = 0;
1326 seq = ntransmitted++;
1327 CLR(seq % mx_dup_ck);
1328
1329 if (options & F_FQDN) {
1330 icp->icmp6_type = ICMP6_NI_QUERY;
1331 icp->icmp6_code = ICMP6_NI_SUBJ_IPV6;
1332 nip->ni_qtype = htons(NI_QTYPE_FQDN);
1333 nip->ni_flags = htons(0);
1334
1335 memcpy(nip->icmp6_ni_nonce, nonce,
1336 sizeof(nip->icmp6_ni_nonce));
1337 *(u_int16_t *)nip->icmp6_ni_nonce = ntohs(seq);
1338
1339 memcpy(&outpack[ICMP6_NIQLEN], &dst.sin6_addr,
1340 sizeof(dst.sin6_addr));
1341 cc = ICMP6_NIQLEN + sizeof(dst.sin6_addr);
1342 datalen = 0;
1343 } else if (options & F_FQDNOLD) {
1344 /* packet format in 03 draft - no Subject data on queries */
1345 icp->icmp6_type = ICMP6_NI_QUERY;
1346 icp->icmp6_code = 0; /* code field is always 0 */
1347 nip->ni_qtype = htons(NI_QTYPE_FQDN);
1348 nip->ni_flags = htons(0);
1349
1350 memcpy(nip->icmp6_ni_nonce, nonce,
1351 sizeof(nip->icmp6_ni_nonce));
1352 *(u_int16_t *)nip->icmp6_ni_nonce = ntohs(seq);
1353
1354 cc = ICMP6_NIQLEN;
1355 datalen = 0;
1356 } else if (options & F_NODEADDR) {
1357 icp->icmp6_type = ICMP6_NI_QUERY;
1358 icp->icmp6_code = ICMP6_NI_SUBJ_IPV6;
1359 nip->ni_qtype = htons(NI_QTYPE_NODEADDR);
1360 nip->ni_flags = naflags;
1361
1362 memcpy(nip->icmp6_ni_nonce, nonce,
1363 sizeof(nip->icmp6_ni_nonce));
1364 *(u_int16_t *)nip->icmp6_ni_nonce = ntohs(seq);
1365
1366 memcpy(&outpack[ICMP6_NIQLEN], &dst.sin6_addr,
1367 sizeof(dst.sin6_addr));
1368 cc = ICMP6_NIQLEN + sizeof(dst.sin6_addr);
1369 datalen = 0;
1370 } else if (options & F_SUPTYPES) {
1371 icp->icmp6_type = ICMP6_NI_QUERY;
1372 icp->icmp6_code = ICMP6_NI_SUBJ_FQDN; /*empty*/
1373 nip->ni_qtype = htons(NI_QTYPE_SUPTYPES);
1374 /* we support compressed bitmap */
1375 nip->ni_flags = NI_SUPTYPE_FLAG_COMPRESS;
1376
1377 memcpy(nip->icmp6_ni_nonce, nonce,
1378 sizeof(nip->icmp6_ni_nonce));
1379 *(u_int16_t *)nip->icmp6_ni_nonce = ntohs(seq);
1380 cc = ICMP6_NIQLEN;
1381 datalen = 0;
1382 } else {
1383 icp->icmp6_type = ICMP6_ECHO_REQUEST;
1384 icp->icmp6_code = 0;
1385 icp->icmp6_id = htons(ident);
1386 icp->icmp6_seq = ntohs(seq);
1387 if (timing) {
1388 struct timeval tv;
1389 struct tv32 *tv32;
1390 (void)gettimeofday(&tv, NULL);
1391 tv32 = (struct tv32 *)&outpack[ICMP6ECHOLEN];
1392 tv32->tv32_sec = htonl(tv.tv_sec);
1393 tv32->tv32_usec = htonl(tv.tv_usec);
1394 }
1395 cc = ICMP6ECHOLEN + datalen;
1396 }
1397
1398#ifdef DIAGNOSTIC
1399 if (pingerlen() != cc)
1400 errx(1, "internal error; length mismatch");
1401#endif
1402
1403 smsghdr.msg_name = (caddr_t)&dst;
1404 smsghdr.msg_namelen = sizeof(dst);
1405 memset(&iov, 0, sizeof(iov));
1406 iov[0].iov_base = (caddr_t)outpack;
1407 iov[0].iov_len = cc;
1408 smsghdr.msg_iov = iov;
1409 smsghdr.msg_iovlen = 1;
1410
1411 i = sendmsg(s, &smsghdr, 0);
1412
1413 if (i < 0 || i != cc) {
1414 if (i < 0)
1415 warn("sendmsg");
1416 (void)printf("ping6: wrote %s %d chars, ret=%d\n",
1417 hostname, cc, i);
1418 }
1419 if (!(options & F_QUIET) && options & F_FLOOD)
1420 (void)write(STDOUT_FILENO, &DOT, 1);
1421
1422 return(0);
1423}
1424
1425int
1426myechoreply(icp)
1427 const struct icmp6_hdr *icp;
1428{
1429 if (ntohs(icp->icmp6_id) == ident)
1430 return 1;
1431 else
1432 return 0;
1433}
1434
1435int
1436mynireply(nip)
1437 const struct icmp6_nodeinfo *nip;
1438{
1439 if (memcmp(nip->icmp6_ni_nonce + sizeof(u_int16_t),
1440 nonce + sizeof(u_int16_t),
1441 sizeof(nonce) - sizeof(u_int16_t)) == 0)
1442 return 1;
1443 else
1444 return 0;
1445}
1446
1447char *
1448dnsdecode(sp, ep, base, buf, bufsiz)
1449 const u_char **sp;
1450 const u_char *ep;
1451 const u_char *base; /*base for compressed name*/
1452 char *buf;
1453 size_t bufsiz;
1454{
1455 int i;
1456 const u_char *cp;
1457 char cresult[NS_MAXDNAME + 1];
1458 const u_char *comp;
1459 int l;
1460
1461 cp = *sp;
1462 *buf = '\0';
1463
1464 if (cp >= ep)
1465 return NULL;
1466 while (cp < ep) {
1467 i = *cp;
1468 if (i == 0 || cp != *sp) {
1469 if (strlcat((char *)buf, ".", bufsiz) >= bufsiz)
1470 return NULL; /*result overrun*/
1471 }
1472 if (i == 0)
1473 break;
1474 cp++;
1475
1476 if ((i & 0xc0) == 0xc0 && cp - base > (i & 0x3f)) {
1477 /* DNS compression */
1478 if (!base)
1479 return NULL;
1480
1481 comp = base + (i & 0x3f);
1482 if (dnsdecode(&comp, cp, base, cresult,
1483 sizeof(cresult)) == NULL)
1484 return NULL;
1485 if (strlcat(buf, cresult, bufsiz) >= bufsiz)
1486 return NULL; /*result overrun*/
1487 break;
1488 } else if ((i & 0x3f) == i) {
1489 if (i > ep - cp)
1490 return NULL; /*source overrun*/
1491 while (i-- > 0 && cp < ep) {
1492 l = snprintf(cresult, sizeof(cresult),
1493 isprint(*cp) ? "%c" : "\\%03o", *cp & 0xff);
1494 if (l >= sizeof(cresult) || l < 0)
1495 return NULL;
1496 if (strlcat(buf, cresult, bufsiz) >= bufsiz)
1497 return NULL; /*result overrun*/
1498 cp++;
1499 }
1500 } else
1501 return NULL; /*invalid label*/
1502 }
1503 if (i != 0)
1504 return NULL; /*not terminated*/
1505 cp++;
1506 *sp = cp;
1507 return buf;
1508}
1509
1510/*
1511 * pr_pack --
1512 * Print out the packet, if it came from us. This logic is necessary
1513 * because ALL readers of the ICMP socket get a copy of ALL ICMP packets
1514 * which arrive ('tis only fair). This permits multiple copies of this
1515 * program to be run without having intermingled output (or statistics!).
1516 */
1517void
1518pr_pack(buf, cc, mhdr)
1519 u_char *buf;
1520 int cc;
1521 struct msghdr *mhdr;
1522{
1523#define safeputc(c) printf((isprint((c)) ? "%c" : "\\%03o"), c)
1524 struct icmp6_hdr *icp;
1525 struct icmp6_nodeinfo *ni;
1526 int i;
1527 int hoplim;
1528 struct sockaddr *from;
1529 int fromlen;
1530 u_char *cp = NULL, *dp, *end = buf + cc;
1531 struct in6_pktinfo *pktinfo = NULL;
1532 struct timeval tv, tp;
1533 struct tv32 *tpp;
1534 double triptime = 0;
1535 int dupflag;
1536 size_t off;
1537 int oldfqdn;
1538 u_int16_t seq;
1539 char dnsname[NS_MAXDNAME + 1];
1540 int tclass;
1541
1542 (void)gettimeofday(&tv, NULL);
1543
1544 if (!mhdr || !mhdr->msg_name ||
1545 mhdr->msg_namelen != sizeof(struct sockaddr_in6) ||
1546 ((struct sockaddr *)mhdr->msg_name)->sa_family != AF_INET6) {
1547 if (options & F_VERBOSE)
1548 warnx("invalid peername");
1549 return;
1550 }
1551 from = (struct sockaddr *)mhdr->msg_name;
1552 fromlen = mhdr->msg_namelen;
1553 if (cc < sizeof(struct icmp6_hdr)) {
1554 if (options & F_VERBOSE)
1555 warnx("packet too short (%d bytes) from %s", cc,
1556 pr_addr(from, fromlen));
1557 return;
1558 }
1559 if (((mhdr->msg_flags & MSG_CTRUNC) != 0) &&
1560 (options & F_VERBOSE) != 0)
1561 warnx("some control data discarded, insufficient buffer size");
1562 icp = (struct icmp6_hdr *)buf;
1563 ni = (struct icmp6_nodeinfo *)buf;
1564 off = 0;
1565
1566 if ((hoplim = get_hoplim(mhdr)) == -1) {
1567 warnx("failed to get receiving hop limit");
1568 return;
1569 }
1570 if ((pktinfo = get_rcvpktinfo(mhdr)) == NULL) {
1571 warnx("failed to get receiving packet information");
1572 return;
1573 }
1574 if (rcvtclass && (tclass = get_tclass(mhdr)) == -1) {
1575 warnx("failed to get receiving traffic class");
1576 return;
1577 }
1578
1579 if (icp->icmp6_type == ICMP6_ECHO_REPLY && myechoreply(icp)) {
1580 seq = ntohs(icp->icmp6_seq);
1581 ++nreceived;
1582 if (timing) {
1583 tpp = (struct tv32 *)(icp + 1);
1584 tp.tv_sec = ntohl(tpp->tv32_sec);
1585 tp.tv_usec = ntohl(tpp->tv32_usec);
1586 tvsub(&tv, &tp);
1587 triptime = ((double)tv.tv_sec) * 1000.0 +
1588 ((double)tv.tv_usec) / 1000.0;
1589 tsum += triptime;
1590 tsumsq += triptime * triptime;
1591 if (triptime < tmin)
1592 tmin = triptime;
1593 if (triptime > tmax)
1594 tmax = triptime;
1595 }
1596
1597 if (TST(seq % mx_dup_ck)) {
1598 ++nrepeats;
1599 --nreceived;
1600 dupflag = 1;
1601 } else {
1602 SET(seq % mx_dup_ck);
1603 dupflag = 0;
1604 }
1605
1606 if (options & F_QUIET)
1607 return;
1608
1609 if (options & F_FLOOD)
1610 (void)write(STDOUT_FILENO, &BSPACE, 1);
1611 else {
1612 (void)printf("%d bytes from %s, icmp_seq=%u", cc,
1613 pr_addr(from, fromlen), seq);
1614 (void)printf(" hlim=%d", hoplim);
1615 if ((options & F_VERBOSE) != 0) {
1616 struct sockaddr_in6 dstsa;
1617
1618 memset(&dstsa, 0, sizeof(dstsa));
1619 dstsa.sin6_family = AF_INET6;
1620 dstsa.sin6_len = sizeof(dstsa);
1621 dstsa.sin6_scope_id = pktinfo->ipi6_ifindex;
1622 dstsa.sin6_addr = pktinfo->ipi6_addr;
1623 (void)printf(" dst=%s",
1624 pr_addr((struct sockaddr *)&dstsa,
1625 sizeof(dstsa)));
1626 }
1627 if (timing)
1628 (void)printf(" time=%.3f ms", triptime);
1629 if (dupflag) {
1630 if (!IN6_IS_ADDR_MULTICAST(&dst.sin6_addr))
1631 (void)printf("(DUP!)");
1632 }
1633 /* check the data */
1634 cp = buf + off + ICMP6ECHOLEN + ICMP6ECHOTMLEN;
1635 dp = outpack + ICMP6ECHOLEN + ICMP6ECHOTMLEN;
1636 for (i = 8; cp < end; ++i, ++cp, ++dp) {
1637 if (*cp != *dp) {
1638 (void)printf("\nwrong data byte #%d should be 0x%x but was 0x%x", i, *dp, *cp);
1639 break;
1640 }
1641 }
1642 }
1643 } else if (icp->icmp6_type == ICMP6_NI_REPLY && mynireply(ni)) {
1644 seq = ntohs(*(u_int16_t *)ni->icmp6_ni_nonce);
1645 ++nreceived;
1646 if (TST(seq % mx_dup_ck)) {
1647 ++nrepeats;
1648 --nreceived;
1649 dupflag = 1;
1650 } else {
1651 SET(seq % mx_dup_ck);
1652 dupflag = 0;
1653 }
1654
1655 if (options & F_QUIET)
1656 return;
1657
1658 (void)printf("%d bytes from %s: ", cc, pr_addr(from, fromlen));
1659
1660 switch (ntohs(ni->ni_code)) {
1661 case ICMP6_NI_SUCCESS:
1662 break;
1663 case ICMP6_NI_REFUSED:
1664 printf("refused, type 0x%x", ntohs(ni->ni_type));
1665 goto fqdnend;
1666 case ICMP6_NI_UNKNOWN:
1667 printf("unknown, type 0x%x", ntohs(ni->ni_type));
1668 goto fqdnend;
1669 default:
1670 printf("unknown code 0x%x, type 0x%x",
1671 ntohs(ni->ni_code), ntohs(ni->ni_type));
1672 goto fqdnend;
1673 }
1674
1675 switch (ntohs(ni->ni_qtype)) {
1676 case NI_QTYPE_NOOP:
1677 printf("NodeInfo NOOP");
1678 break;
1679 case NI_QTYPE_SUPTYPES:
1680 pr_suptypes(ni, end - (u_char *)ni);
1681 break;
1682 case NI_QTYPE_NODEADDR:
1683 pr_nodeaddr(ni, end - (u_char *)ni);
1684 break;
1685 case NI_QTYPE_FQDN:
1686 default: /* XXX: for backward compatibility */
1687 cp = (u_char *)ni + ICMP6_NIRLEN;
1688 if (buf[off + ICMP6_NIRLEN] ==
1689 cc - off - ICMP6_NIRLEN - 1)
1690 oldfqdn = 1;
1691 else
1692 oldfqdn = 0;
1693 if (oldfqdn) {
1694 cp++; /* skip length */
1695 while (cp < end) {
1696 safeputc(*cp & 0xff);
1697 cp++;
1698 }
1699 } else {
1700 i = 0;
1701 while (cp < end) {
1702 if (dnsdecode((const u_char **)&cp, end,
1703 (const u_char *)(ni + 1), dnsname,
1704 sizeof(dnsname)) == NULL) {
1705 printf("???");
1706 break;
1707 }
1708 /*
1709 * name-lookup special handling for
1710 * truncated name
1711 */
1712 if (cp + 1 <= end && !*cp &&
1713 strlen(dnsname) > 0) {
1714 dnsname[strlen(dnsname) - 1] = '\0';
1715 cp++;
1716 }
1717 printf("%s%s", i > 0 ? "," : "",
1718 dnsname);
1719 }
1720 }
1721 if (options & F_VERBOSE) {
1722 int32_t ttl;
1723 int comma = 0;
1724
1725 (void)printf(" ("); /*)*/
1726
1727 switch (ni->ni_code) {
1728 case ICMP6_NI_REFUSED:
1729 (void)printf("refused");
1730 comma++;
1731 break;
1732 case ICMP6_NI_UNKNOWN:
1733 (void)printf("unknown qtype");
1734 comma++;
1735 break;
1736 }
1737
1738 if ((end - (u_char *)ni) < ICMP6_NIRLEN) {
1739 /* case of refusion, unknown */
1740 /*(*/
1741 putchar(')');
1742 goto fqdnend;
1743 }
1744 ttl = (int32_t)ntohl(*(u_int32_t *)&buf[off+ICMP6ECHOLEN+8]);
1745 if (comma)
1746 printf(",");
1747 if (!(ni->ni_flags & NI_FQDN_FLAG_VALIDTTL)) {
1748 (void)printf("TTL=%d:meaningless",
1749 (int)ttl);
1750 } else {
1751 if (ttl < 0) {
1752 (void)printf("TTL=%d:invalid",
1753 ttl);
1754 } else
1755 (void)printf("TTL=%d", ttl);
1756 }
1757 comma++;
1758
1759 if (oldfqdn) {
1760 if (comma)
1761 printf(",");
1762 printf("03 draft");
1763 comma++;
1764 } else {
1765 cp = (u_char *)ni + ICMP6_NIRLEN;
1766 if (cp == end) {
1767 if (comma)
1768 printf(",");
1769 printf("no name");
1770 comma++;
1771 }
1772 }
1773
1774 if (buf[off + ICMP6_NIRLEN] !=
1775 cc - off - ICMP6_NIRLEN - 1 && oldfqdn) {
1776 if (comma)
1777 printf(",");
1778 (void)printf("invalid namelen:%d/%lu",
1779 buf[off + ICMP6_NIRLEN],
1780 (u_long)cc - off - ICMP6_NIRLEN - 1);
1781 comma++;
1782 }
1783 /*(*/
1784 putchar(')');
1785 }
1786 fqdnend:
1787 ;
1788 }
1789 } else {
1790 /* We've got something other than an ECHOREPLY */
1791 if (!(options & F_VERBOSE))
1792 return;
1793 (void)printf("%d bytes from %s: ", cc, pr_addr(from, fromlen));
1794 pr_icmph(icp, end);
1795 }
1796
1797 if (!(options & F_FLOOD)) {
1798 (void)putchar('\n');
1799 if (options & F_VERBOSE)
1800 pr_exthdrs(mhdr);
1801 (void)fflush(stdout);
1802 }
1803#undef safeputc
1804}
1805
1806void
1807pr_exthdrs(mhdr)
1808 struct msghdr *mhdr;
1809{
1810 ssize_t bufsize;
1811 void *bufp;
1812 struct cmsghdr *cm;
1813
1814 bufsize = 0;
1815 bufp = mhdr->msg_control;
1816 for (cm = (struct cmsghdr *)CMSG_FIRSTHDR(mhdr); cm;
1817 cm = (struct cmsghdr *)CMSG_NXTHDR(mhdr, cm)) {
1818 if (cm->cmsg_level != IPPROTO_IPV6)
1819 continue;
1820
1821 bufsize = CONTROLLEN - ((caddr_t)CMSG_DATA(cm) - (caddr_t)bufp);
1822 if (bufsize <= 0)
1823 continue;
1824 switch (cm->cmsg_type) {
1825 case IPV6_HOPOPTS:
1826 printf(" HbH Options: ");
1827 pr_ip6opt(CMSG_DATA(cm), (size_t)bufsize);
1828 break;
1829 case IPV6_DSTOPTS:
1830#ifdef IPV6_RTHDRDSTOPTS
1831 case IPV6_RTHDRDSTOPTS:
1832#endif
1833 printf(" Dst Options: ");
1834 pr_ip6opt(CMSG_DATA(cm), (size_t)bufsize);
1835 break;
1836 case IPV6_RTHDR:
1837 printf(" Routing: ");
1838 pr_rthdr(CMSG_DATA(cm), (size_t)bufsize);
1839 break;
1840 }
1841 }
1842}
1843
1844#ifdef USE_RFC2292BIS
1845void
1846pr_ip6opt(void *extbuf, size_t bufsize)
1847{
1848 struct ip6_hbh *ext;
1849 int currentlen;
1850 u_int8_t type;
1851 socklen_t extlen, len, origextlen;
1852 void *databuf;
1853 size_t offset;
1854 u_int16_t value2;
1855 u_int32_t value4;
1856
1857 ext = (struct ip6_hbh *)extbuf;
1858 extlen = (ext->ip6h_len + 1) * 8;
1859 printf("nxt %u, len %u (%lu bytes)\n", ext->ip6h_nxt,
1860 (unsigned int)ext->ip6h_len, (unsigned long)extlen);
1861
1862 /*
1863 * Bounds checking on the ancillary data buffer:
1864 * subtract the size of a cmsg structure from the buffer size.
1865 */
1866 if (bufsize < (extlen + CMSG_SPACE(0))) {
1867 origextlen = extlen;
1868 extlen = bufsize - CMSG_SPACE(0);
1869 warnx("options truncated, showing only %u (total=%u)",
1870 (unsigned int)(extlen / 8 - 1),
1871 (unsigned int)(ext->ip6h_len));
1872 }
1873
1874 currentlen = 0;
1875 while (1) {
1876 currentlen = inet6_opt_next(extbuf, extlen, currentlen,
1877 &type, &len, &databuf);
1878 if (currentlen == -1)
1879 break;
1880 switch (type) {
1881 /*
1882 * Note that inet6_opt_next automatically skips any padding
1883 * optins.
1884 */
1885 case IP6OPT_JUMBO:
1886 offset = 0;
1887 offset = inet6_opt_get_val(databuf, offset,
1888 &value4, sizeof(value4));
1889 printf(" Jumbo Payload Opt: Length %u\n",
1890 (u_int32_t)ntohl(value4));
1891 break;
1892 case IP6OPT_ROUTER_ALERT:
1893 offset = 0;
1894 offset = inet6_opt_get_val(databuf, offset,
1895 &value2, sizeof(value2));
1896 printf(" Router Alert Opt: Type %u\n",
1897 ntohs(value2));
1898 break;
1899 default:
1900 printf(" Received Opt %u len %lu\n",
1901 type, (unsigned long)len);
1902 break;
1903 }
1904 }
1905 return;
1906}
1907#else /* !USE_RFC2292BIS */
1908/* ARGSUSED */
1909void
1910pr_ip6opt(void *extbuf, size_t bufsize __unused)
1911{
1912 putchar('\n');
1913 return;
1914}
1915#endif /* USE_RFC2292BIS */
1916
1917#ifdef USE_RFC2292BIS
1918void
1919pr_rthdr(void *extbuf, size_t bufsize)
1920{
1921 struct in6_addr *in6;
1922 char ntopbuf[INET6_ADDRSTRLEN];
1923 struct ip6_rthdr *rh = (struct ip6_rthdr *)extbuf;
1924 int i, segments, origsegs, rthsize, size0, size1;
1925
1926 /* print fixed part of the header */
1927 printf("nxt %u, len %u (%d bytes), type %u, ", rh->ip6r_nxt,
1928 rh->ip6r_len, (rh->ip6r_len + 1) << 3, rh->ip6r_type);
1929 if ((segments = inet6_rth_segments(extbuf)) >= 0) {
1930 printf("%d segments, ", segments);
1931 printf("%d left\n", rh->ip6r_segleft);
1932 } else {
1933 printf("segments unknown, ");
1934 printf("%d left\n", rh->ip6r_segleft);
1935 return;
1936 }
1937
1938 /*
1939 * Bounds checking on the ancillary data buffer. When calculating
1940 * the number of items to show keep in mind:
1941 * - The size of the cmsg structure
1942 * - The size of one segment (the size of a Type 0 routing header)
1943 * - When dividing add a fudge factor of one in case the
1944 * dividend is not evenly divisible by the divisor
1945 */
1946 rthsize = (rh->ip6r_len + 1) * 8;
1947 if (bufsize < (rthsize + CMSG_SPACE(0))) {
1948 origsegs = segments;
1949 size0 = inet6_rth_space(IPV6_RTHDR_TYPE_0, 0);
1950 size1 = inet6_rth_space(IPV6_RTHDR_TYPE_0, 1);
1951 segments -= (rthsize - (bufsize - CMSG_SPACE(0))) /
1952 (size1 - size0) + 1;
1953 warnx("segments truncated, showing only %d (total=%d)",
1954 segments, origsegs);
1955 }
1956
1957 for (i = 0; i < segments; i++) {
1958 in6 = inet6_rth_getaddr(extbuf, i);
1959 if (in6 == NULL)
1960 printf(" [%d]<NULL>\n", i);
1961 else {
1962 if (!inet_ntop(AF_INET6, in6, ntopbuf,
1963 sizeof(ntopbuf)))
1964 strlcpy(ntopbuf, "?", sizeof(ntopbuf));
1965 printf(" [%d]%s\n", i, ntopbuf);
1966 }
1967 }
1968
1969 return;
1970
1971}
1972
1973#else /* !USE_RFC2292BIS */
1974/* ARGSUSED */
1975void
1976pr_rthdr(void *extbuf, size_t bufsize __unused)
1977{
1978 putchar('\n');
1979 return;
1980}
1981#endif /* USE_RFC2292BIS */
1982
1983int
1984pr_bitrange(v, soff, ii)
1985 u_int32_t v;
1986 int soff;
1987 int ii;
1988{
1989 int off;
1990 int i;
1991
1992 off = 0;
1993 while (off < 32) {
1994 /* shift till we have 0x01 */
1995 if ((v & 0x01) == 0) {
1996 if (ii > 1)
1997 printf("-%u", soff + off - 1);
1998 ii = 0;
1999 switch (v & 0x0f) {
2000 case 0x00:
2001 v >>= 4;
2002 off += 4;
2003 continue;
2004 case 0x08:
2005 v >>= 3;
2006 off += 3;
2007 continue;
2008 case 0x04: case 0x0c:
2009 v >>= 2;
2010 off += 2;
2011 continue;
2012 default:
2013 v >>= 1;
2014 off += 1;
2015 continue;
2016 }
2017 }
2018
2019 /* we have 0x01 with us */
2020 for (i = 0; i < 32 - off; i++) {
2021 if ((v & (0x01 << i)) == 0)
2022 break;
2023 }
2024 if (!ii)
2025 printf(" %u", soff + off);
2026 ii += i;
2027 v >>= i; off += i;
2028 }
2029 return ii;
2030}
2031
2032void
2033pr_suptypes(ni, nilen)
2034 struct icmp6_nodeinfo *ni; /* ni->qtype must be SUPTYPES */
2035 size_t nilen;
2036{
2037 size_t clen;
2038 u_int32_t v;
2039 const u_char *cp, *end;
2040 u_int16_t cur;
2041 struct cbit {
2042 u_int16_t words; /*32bit count*/
2043 u_int16_t skip;
2044 } cbit;
2045#define MAXQTYPES (1 << 16)
2046 size_t off;
2047 int b;
2048
2049 cp = (u_char *)(ni + 1);
2050 end = ((u_char *)ni) + nilen;
2051 cur = 0;
2052 b = 0;
2053
2054 printf("NodeInfo Supported Qtypes");
2055 if (options & F_VERBOSE) {
2056 if (ni->ni_flags & NI_SUPTYPE_FLAG_COMPRESS)
2057 printf(", compressed bitmap");
2058 else
2059 printf(", raw bitmap");
2060 }
2061
2062 while (cp < end) {
2063 clen = (size_t)(end - cp);
2064 if ((ni->ni_flags & NI_SUPTYPE_FLAG_COMPRESS) == 0) {
2065 if (clen == 0 || clen > MAXQTYPES / 8 ||
2066 clen % sizeof(v)) {
2067 printf("???");
2068 return;
2069 }
2070 } else {
2071 if (clen < sizeof(cbit) || clen % sizeof(v))
2072 return;
2073 memcpy(&cbit, cp, sizeof(cbit));
2074 if (sizeof(cbit) + ntohs(cbit.words) * sizeof(v) >
2075 clen)
2076 return;
2077 cp += sizeof(cbit);
2078 clen = ntohs(cbit.words) * sizeof(v);
2079 if (cur + clen * 8 + (u_long)ntohs(cbit.skip) * 32 >
2080 MAXQTYPES)
2081 return;
2082 }
2083
2084 for (off = 0; off < clen; off += sizeof(v)) {
2085 memcpy(&v, cp + off, sizeof(v));
2086 v = (u_int32_t)ntohl(v);
2087 b = pr_bitrange(v, (int)(cur + off * 8), b);
2088 }
2089 /* flush the remaining bits */
2090 b = pr_bitrange(0, (int)(cur + off * 8), b);
2091
2092 cp += clen;
2093 cur += clen * 8;
2094 if ((ni->ni_flags & NI_SUPTYPE_FLAG_COMPRESS) != 0)
2095 cur += ntohs(cbit.skip) * 32;
2096 }
2097}
2098
2099void
2100pr_nodeaddr(ni, nilen)
2101 struct icmp6_nodeinfo *ni; /* ni->qtype must be NODEADDR */
2102 int nilen;
2103{
2104 u_char *cp = (u_char *)(ni + 1);
2105 char ntop_buf[INET6_ADDRSTRLEN];
2106 int withttl = 0;
2107
2108 nilen -= sizeof(struct icmp6_nodeinfo);
2109
2110 if (options & F_VERBOSE) {
2111 switch (ni->ni_code) {
2112 case ICMP6_NI_REFUSED:
2113 (void)printf("refused");
2114 break;
2115 case ICMP6_NI_UNKNOWN:
2116 (void)printf("unknown qtype");
2117 break;
2118 }
2119 if (ni->ni_flags & NI_NODEADDR_FLAG_TRUNCATE)
2120 (void)printf(" truncated");
2121 }
2122 putchar('\n');
2123 if (nilen <= 0)
2124 printf(" no address\n");
2125
2126 /*
2127 * In icmp-name-lookups 05 and later, TTL of each returned address
2128 * is contained in the resposne. We try to detect the version
2129 * by the length of the data, but note that the detection algorithm
2130 * is incomplete. We assume the latest draft by default.
2131 */
2132 if (nilen % (sizeof(u_int32_t) + sizeof(struct in6_addr)) == 0)
2133 withttl = 1;
2134 while (nilen > 0) {
2135 u_int32_t ttl = 0;
2136
2137 if (withttl) {
2138 /* XXX: alignment? */
2139 ttl = (u_int32_t)ntohl(*(u_int32_t *)cp);
2140 cp += sizeof(u_int32_t);
2141 nilen -= sizeof(u_int32_t);
2142 }
2143
2144 if (inet_ntop(AF_INET6, cp, ntop_buf, sizeof(ntop_buf)) ==
2145 NULL)
2146 strlcpy(ntop_buf, "?", sizeof(ntop_buf));
2147 printf(" %s", ntop_buf);
2148 if (withttl) {
2149 if (ttl == 0xffffffff) {
2150 /*
2151 * XXX: can this convention be applied to all
2152 * type of TTL (i.e. non-ND TTL)?
2153 */
2154 printf("(TTL=infty)");
2155 }
2156 else
2157 printf("(TTL=%u)", ttl);
2158 }
2159 putchar('\n');
2160
2161 nilen -= sizeof(struct in6_addr);
2162 cp += sizeof(struct in6_addr);
2163 }
2164}
2165
2166int
2167get_hoplim(mhdr)
2168 struct msghdr *mhdr;
2169{
2170 struct cmsghdr *cm;
2171
2172 for (cm = (struct cmsghdr *)CMSG_FIRSTHDR(mhdr); cm;
2173 cm = (struct cmsghdr *)CMSG_NXTHDR(mhdr, cm)) {
2174 if (cm->cmsg_len == 0)
2175 return(-1);
2176
2177 if (cm->cmsg_level == IPPROTO_IPV6 &&
2178 cm->cmsg_type == IPV6_HOPLIMIT &&
2179 cm->cmsg_len == CMSG_LEN(sizeof(int)))
2180 return(*(int *)CMSG_DATA(cm));
2181 }
2182
2183 return(-1);
2184}
2185
2186struct in6_pktinfo *
2187get_rcvpktinfo(mhdr)
2188 struct msghdr *mhdr;
2189{
2190 struct cmsghdr *cm;
2191
2192 for (cm = (struct cmsghdr *)CMSG_FIRSTHDR(mhdr); cm;
2193 cm = (struct cmsghdr *)CMSG_NXTHDR(mhdr, cm)) {
2194 if (cm->cmsg_len == 0)
2195 return(NULL);
2196
2197 if (cm->cmsg_level == IPPROTO_IPV6 &&
2198 cm->cmsg_type == IPV6_PKTINFO &&
2199 cm->cmsg_len == CMSG_LEN(sizeof(struct in6_pktinfo)))
2200 return((struct in6_pktinfo *)CMSG_DATA(cm));
2201 }
2202
2203 return(NULL);
2204}
2205
2206int
2207get_pathmtu(mhdr)
2208 struct msghdr *mhdr;
2209{
2210#ifdef IPV6_RECVPATHMTU
2211 struct cmsghdr *cm;
2212 struct ip6_mtuinfo *mtuctl = NULL;
2213
2214 for (cm = (struct cmsghdr *)CMSG_FIRSTHDR(mhdr); cm;
2215 cm = (struct cmsghdr *)CMSG_NXTHDR(mhdr, cm)) {
2216 if (cm->cmsg_len == 0)
2217 return(0);
2218
2219 if (cm->cmsg_level == IPPROTO_IPV6 &&
2220 cm->cmsg_type == IPV6_PATHMTU &&
2221 cm->cmsg_len == CMSG_LEN(sizeof(struct ip6_mtuinfo))) {
2222 mtuctl = (struct ip6_mtuinfo *)CMSG_DATA(cm);
2223
2224 /*
2225 * If the notified destination is different from
2226 * the one we are pinging, just ignore the info.
2227 * We check the scope ID only when both notified value
2228 * and our own value have non-0 values, because we may
2229 * have used the default scope zone ID for sending,
2230 * in which case the scope ID value is 0.
2231 */
2232 if (!IN6_ARE_ADDR_EQUAL(&mtuctl->ip6m_addr.sin6_addr,
2233 &dst.sin6_addr) ||
2234 (mtuctl->ip6m_addr.sin6_scope_id &&
2235 dst.sin6_scope_id &&
2236 mtuctl->ip6m_addr.sin6_scope_id !=
2237 dst.sin6_scope_id)) {
2238 if ((options & F_VERBOSE) != 0) {
2239 printf("path MTU for %s is notified. "
2240 "(ignored)\n",
2241 pr_addr((struct sockaddr *)&mtuctl->ip6m_addr,
2242 sizeof(mtuctl->ip6m_addr)));
2243 }
2244 return(0);
2245 }
2246
2247 /*
2248 * Ignore an invalid MTU. XXX: can we just believe
2249 * the kernel check?
2250 */
2251 if (mtuctl->ip6m_mtu < IPV6_MMTU)
2252 return(0);
2253
2254 /* notification for our destination. return the MTU. */
2255 return((int)mtuctl->ip6m_mtu);
2256 }
2257 }
2258#endif
2259 return(0);
2260}
2261
2262int
2263get_tclass(mhdr)
2264 struct msghdr *mhdr;
2265{
2266 struct cmsghdr *cm;
2267
2268 for (cm = (struct cmsghdr *)CMSG_FIRSTHDR(mhdr); cm;
2269 cm = (struct cmsghdr *)CMSG_NXTHDR(mhdr, cm)) {
2270 if (cm->cmsg_len == 0)
2271 return(-1);
2272
2273 if (cm->cmsg_level == IPPROTO_IPV6 &&
2274 cm->cmsg_type == IPV6_TCLASS &&
2275 cm->cmsg_len == CMSG_LEN(sizeof(int)))
2276 return(*(int *)CMSG_DATA(cm));
2277 }
2278
2279 return(-1);
2280}
2281
2282
2283
2284/*
2285 * tvsub --
2286 * Subtract 2 timeval structs: out = out - in. Out is assumed to
2287 * be >= in.
2288 */
2289void
2290tvsub(out, in)
2291 struct timeval *out, *in;
2292{
2293 if ((out->tv_usec -= in->tv_usec) < 0) {
2294 --out->tv_sec;
2295 out->tv_usec += 1000000;
2296 }
2297 out->tv_sec -= in->tv_sec;
2298}
2299
2300/*
2301 * onint --
2302 * SIGINT handler.
2303 */
2304/* ARGSUSED */
2305void
2306onint(notused)
2307 int notused;
2308{
2309 summary();
2310
2311 (void)signal(SIGINT, SIG_DFL);
2312 (void)kill(getpid(), SIGINT);
2313
2314 /* NOTREACHED */
2315 exit(1);
2316}
2317
2318/*
2319 * summary --
2320 * Print out statistics.
2321 */
2322void
2323summary()
2324{
2325
2326 (void)printf("\n--- %s ping6 statistics ---\n", hostname);
2327 (void)printf("%ld packets transmitted, ", ntransmitted);
2328 (void)printf("%ld packets received, ", nreceived);
2329 if (nrepeats)
2330 (void)printf("+%ld duplicates, ", nrepeats);
2331 if (ntransmitted) {
2332 if (nreceived > ntransmitted)
2333 (void)printf("-- somebody's duplicating packets!");
2334 else
2335 (void)printf("%.1f%% packet loss",
2336 ((((double)ntransmitted - nreceived) * 100.0) /
2337 ntransmitted));
2338 }
2339 (void)putchar('\n');
2340 if (nreceived && timing) {
2341 /* Only display average to microseconds */
2342 double num = nreceived + nrepeats;
2343 double avg = tsum / num;
2344 double dev = sqrt(tsumsq / num - avg * avg);
2345 (void)printf(
2346 "round-trip min/avg/max/std-dev = %.3f/%.3f/%.3f/%.3f ms\n",
2347 tmin, avg, tmax, dev);
2348 (void)fflush(stdout);
2349 }
2350 (void)fflush(stdout);
2351}
2352
2353/*subject type*/
2354static const char *niqcode[] = {
2355 "IPv6 address",
2356 "DNS label", /*or empty*/
2357 "IPv4 address",
2358};
2359
2360/*result code*/
2361static const char *nircode[] = {
2362 "Success", "Refused", "Unknown",
2363};
2364
2365
2366/*
2367 * pr_icmph --
2368 * Print a descriptive string about an ICMP header.
2369 */
2370void
2371pr_icmph(icp, end)
2372 struct icmp6_hdr *icp;
2373 u_char *end;
2374{
2375 char ntop_buf[INET6_ADDRSTRLEN];
2376 struct nd_redirect *red;
2377 struct icmp6_nodeinfo *ni;
2378 char dnsname[NS_MAXDNAME + 1];
2379 const u_char *cp;
2380 size_t l;
2381
2382 switch (icp->icmp6_type) {
2383 case ICMP6_DST_UNREACH:
2384 switch (icp->icmp6_code) {
2385 case ICMP6_DST_UNREACH_NOROUTE:
2386 (void)printf("No Route to Destination\n");
2387 break;
2388 case ICMP6_DST_UNREACH_ADMIN:
2389 (void)printf("Destination Administratively "
2390 "Unreachable\n");
2391 break;
2392 case ICMP6_DST_UNREACH_BEYONDSCOPE:
2393 (void)printf("Destination Unreachable Beyond Scope\n");
2394 break;
2395 case ICMP6_DST_UNREACH_ADDR:
2396 (void)printf("Destination Host Unreachable\n");
2397 break;
2398 case ICMP6_DST_UNREACH_NOPORT:
2399 (void)printf("Destination Port Unreachable\n");
2400 break;
2401 default:
2402 (void)printf("Destination Unreachable, Bad Code: %d\n",
2403 icp->icmp6_code);
2404 break;
2405 }
2406 /* Print returned IP header information */
2407 pr_retip((struct ip6_hdr *)(icp + 1), end);
2408 break;
2409 case ICMP6_PACKET_TOO_BIG:
2410 (void)printf("Packet too big mtu = %d\n",
2411 (int)ntohl(icp->icmp6_mtu));
2412 pr_retip((struct ip6_hdr *)(icp + 1), end);
2413 break;
2414 case ICMP6_TIME_EXCEEDED:
2415 switch (icp->icmp6_code) {
2416 case ICMP6_TIME_EXCEED_TRANSIT:
2417 (void)printf("Time to live exceeded\n");
2418 break;
2419 case ICMP6_TIME_EXCEED_REASSEMBLY:
2420 (void)printf("Frag reassembly time exceeded\n");
2421 break;
2422 default:
2423 (void)printf("Time exceeded, Bad Code: %d\n",
2424 icp->icmp6_code);
2425 break;
2426 }
2427 pr_retip((struct ip6_hdr *)(icp + 1), end);
2428 break;
2429 case ICMP6_PARAM_PROB:
2430 (void)printf("Parameter problem: ");
2431 switch (icp->icmp6_code) {
2432 case ICMP6_PARAMPROB_HEADER:
2433 (void)printf("Erroneous Header ");
2434 break;
2435 case ICMP6_PARAMPROB_NEXTHEADER:
2436 (void)printf("Unknown Nextheader ");
2437 break;
2438 case ICMP6_PARAMPROB_OPTION:
2439 (void)printf("Unrecognized Option ");
2440 break;
2441 default:
2442 (void)printf("Bad code(%d) ", icp->icmp6_code);
2443 break;
2444 }
2445 (void)printf("pointer = 0x%02x\n",
2446 (u_int32_t)ntohl(icp->icmp6_pptr));
2447 pr_retip((struct ip6_hdr *)(icp + 1), end);
2448 break;
2449 case ICMP6_ECHO_REQUEST:
2450 (void)printf("Echo Request");
2451 /* XXX ID + Seq + Data */
2452 break;
2453 case ICMP6_ECHO_REPLY:
2454 (void)printf("Echo Reply");
2455 /* XXX ID + Seq + Data */
2456 break;
2457 case ICMP6_MEMBERSHIP_QUERY:
2458 (void)printf("Listener Query");
2459 break;
2460 case ICMP6_MEMBERSHIP_REPORT:
2461 (void)printf("Listener Report");
2462 break;
2463 case ICMP6_MEMBERSHIP_REDUCTION:
2464 (void)printf("Listener Done");
2465 break;
2466 case ND_ROUTER_SOLICIT:
2467 (void)printf("Router Solicitation");
2468 break;
2469 case ND_ROUTER_ADVERT:
2470 (void)printf("Router Advertisement");
2471 break;
2472 case ND_NEIGHBOR_SOLICIT:
2473 (void)printf("Neighbor Solicitation");
2474 break;
2475 case ND_NEIGHBOR_ADVERT:
2476 (void)printf("Neighbor Advertisement");
2477 break;
2478 case ND_REDIRECT:
2479 red = (struct nd_redirect *)icp;
2480 (void)printf("Redirect\n");
2481 if (!inet_ntop(AF_INET6, &red->nd_rd_dst, ntop_buf,
2482 sizeof(ntop_buf)))
2483 strlcpy(ntop_buf, "?", sizeof(ntop_buf));
2484 (void)printf("Destination: %s", ntop_buf);
2485 if (!inet_ntop(AF_INET6, &red->nd_rd_target, ntop_buf,
2486 sizeof(ntop_buf)))
2487 strlcpy(ntop_buf, "?", sizeof(ntop_buf));
2488 (void)printf(" New Target: %s", ntop_buf);
2489 break;
2490 case ICMP6_NI_QUERY:
2491 (void)printf("Node Information Query");
2492 /* XXX ID + Seq + Data */
2493 ni = (struct icmp6_nodeinfo *)icp;
2494 l = end - (u_char *)(ni + 1);
2495 printf(", ");
2496 switch (ntohs(ni->ni_qtype)) {
2497 case NI_QTYPE_NOOP:
2498 (void)printf("NOOP");
2499 break;
2500 case NI_QTYPE_SUPTYPES:
2501 (void)printf("Supported qtypes");
2502 break;
2503 case NI_QTYPE_FQDN:
2504 (void)printf("DNS name");
2505 break;
2506 case NI_QTYPE_NODEADDR:
2507 (void)printf("nodeaddr");
2508 break;
2509 case NI_QTYPE_IPV4ADDR:
2510 (void)printf("IPv4 nodeaddr");
2511 break;
2512 default:
2513 (void)printf("unknown qtype");
2514 break;
2515 }
2516 if (options & F_VERBOSE) {
2517 switch (ni->ni_code) {
2518 case ICMP6_NI_SUBJ_IPV6:
2519 if (l == sizeof(struct in6_addr) &&
2520 inet_ntop(AF_INET6, ni + 1, ntop_buf,
2521 sizeof(ntop_buf)) != NULL) {
2522 (void)printf(", subject=%s(%s)",
2523 niqcode[ni->ni_code], ntop_buf);
2524 } else {
2525#if 1
2526 /* backward compat to -W */
2527 (void)printf(", oldfqdn");
2528#else
2529 (void)printf(", invalid");
2530#endif
2531 }
2532 break;
2533 case ICMP6_NI_SUBJ_FQDN:
2534 if (end == (u_char *)(ni + 1)) {
2535 (void)printf(", no subject");
2536 break;
2537 }
2538 printf(", subject=%s", niqcode[ni->ni_code]);
2539 cp = (const u_char *)(ni + 1);
2540 if (dnsdecode(&cp, end, NULL, dnsname,
2541 sizeof(dnsname)) != NULL)
2542 printf("(%s)", dnsname);
2543 else
2544 printf("(invalid)");
2545 break;
2546 case ICMP6_NI_SUBJ_IPV4:
2547 if (l == sizeof(struct in_addr) &&
2548 inet_ntop(AF_INET, ni + 1, ntop_buf,
2549 sizeof(ntop_buf)) != NULL) {
2550 (void)printf(", subject=%s(%s)",
2551 niqcode[ni->ni_code], ntop_buf);
2552 } else
2553 (void)printf(", invalid");
2554 break;
2555 default:
2556 (void)printf(", invalid");
2557 break;
2558 }
2559 }
2560 break;
2561 case ICMP6_NI_REPLY:
2562 (void)printf("Node Information Reply");
2563 /* XXX ID + Seq + Data */
2564 ni = (struct icmp6_nodeinfo *)icp;
2565 printf(", ");
2566 switch (ntohs(ni->ni_qtype)) {
2567 case NI_QTYPE_NOOP:
2568 (void)printf("NOOP");
2569 break;
2570 case NI_QTYPE_SUPTYPES:
2571 (void)printf("Supported qtypes");
2572 break;
2573 case NI_QTYPE_FQDN:
2574 (void)printf("DNS name");
2575 break;
2576 case NI_QTYPE_NODEADDR:
2577 (void)printf("nodeaddr");
2578 break;
2579 case NI_QTYPE_IPV4ADDR:
2580 (void)printf("IPv4 nodeaddr");
2581 break;
2582 default:
2583 (void)printf("unknown qtype");
2584 break;
2585 }
2586 if (options & F_VERBOSE) {
2587 if (ni->ni_code > sizeof(nircode) / sizeof(nircode[0]))
2588 printf(", invalid");
2589 else
2590 printf(", %s", nircode[ni->ni_code]);
2591 }
2592 break;
2593 default:
2594 (void)printf("Bad ICMP type: %d", icp->icmp6_type);
2595 }
2596}
2597
2598/*
2599 * pr_iph --
2600 * Print an IP6 header.
2601 */
2602void
2603pr_iph(ip6)
2604 struct ip6_hdr *ip6;
2605{
2606 u_int32_t flow = ip6->ip6_flow & IPV6_FLOWLABEL_MASK;
2607 u_int8_t tc;
2608 char ntop_buf[INET6_ADDRSTRLEN];
2609
2610 tc = *(&ip6->ip6_vfc + 1); /* XXX */
2611 tc = (tc >> 4) & 0x0f;
2612 tc |= (ip6->ip6_vfc << 4);
2613
2614 printf("Vr TC Flow Plen Nxt Hlim\n");
2615 printf(" %1x %02x %05x %04x %02x %02x\n",
2616 (ip6->ip6_vfc & IPV6_VERSION_MASK) >> 4, tc, (u_int32_t)ntohl(flow),
2617 ntohs(ip6->ip6_plen), ip6->ip6_nxt, ip6->ip6_hlim);
2618 if (!inet_ntop(AF_INET6, &ip6->ip6_src, ntop_buf, sizeof(ntop_buf)))
2619 strlcpy(ntop_buf, "?", sizeof(ntop_buf));
2620 printf("%s->", ntop_buf);
2621 if (!inet_ntop(AF_INET6, &ip6->ip6_dst, ntop_buf, sizeof(ntop_buf)))
2622 strlcpy(ntop_buf, "?", sizeof(ntop_buf));
2623 printf("%s\n", ntop_buf);
2624}
2625
2626/*
2627 * pr_addr --
2628 * Return an ascii host address as a dotted quad and optionally with
2629 * a hostname.
2630 */
2631const char *
2632pr_addr(addr, addrlen)
2633 struct sockaddr *addr;
2634 int addrlen;
2635{
2636 static char buf[NI_MAXHOST];
2637 int flag = 0;
2638
2639 if ((options & F_HOSTNAME) == 0)
2640 flag |= NI_NUMERICHOST;
2641
2642 if (getnameinfo(addr, addrlen, buf, sizeof(buf), NULL, 0, flag) == 0)
2643 return (buf);
2644 else
2645 return "?";
2646}
2647
2648/*
2649 * pr_retip --
2650 * Dump some info on a returned (via ICMPv6) IPv6 packet.
2651 */
2652void
2653pr_retip(ip6, end)
2654 struct ip6_hdr *ip6;
2655 u_char *end;
2656{
2657 u_char *cp = (u_char *)ip6, nh;
2658 int hlen;
2659
2660 if (end - (u_char *)ip6 < sizeof(*ip6)) {
2661 printf("IP6");
2662 goto trunc;
2663 }
2664 pr_iph(ip6);
2665 hlen = sizeof(*ip6);
2666
2667 nh = ip6->ip6_nxt;
2668 cp += hlen;
2669 while (end - cp >= 8) {
2670 switch (nh) {
2671 case IPPROTO_HOPOPTS:
2672 printf("HBH ");
2673 hlen = (((struct ip6_hbh *)cp)->ip6h_len+1) << 3;
2674 nh = ((struct ip6_hbh *)cp)->ip6h_nxt;
2675 break;
2676 case IPPROTO_DSTOPTS:
2677 printf("DSTOPT ");
2678 hlen = (((struct ip6_dest *)cp)->ip6d_len+1) << 3;
2679 nh = ((struct ip6_dest *)cp)->ip6d_nxt;
2680 break;
2681 case IPPROTO_FRAGMENT:
2682 printf("FRAG ");
2683 hlen = sizeof(struct ip6_frag);
2684 nh = ((struct ip6_frag *)cp)->ip6f_nxt;
2685 break;
2686 case IPPROTO_ROUTING:
2687 printf("RTHDR ");
2688 hlen = (((struct ip6_rthdr *)cp)->ip6r_len+1) << 3;
2689 nh = ((struct ip6_rthdr *)cp)->ip6r_nxt;
2690 break;
2691#ifdef IPSEC
2692 case IPPROTO_AH:
2693 printf("AH ");
2694 hlen = (((struct ah *)cp)->ah_len+2) << 2;
2695 nh = ((struct ah *)cp)->ah_nxt;
2696 break;
2697#endif
2698 case IPPROTO_ICMPV6:
2699 printf("ICMP6: type = %d, code = %d\n",
2700 *cp, *(cp + 1));
2701 return;
2702 case IPPROTO_ESP:
2703 printf("ESP\n");
2704 return;
2705 case IPPROTO_TCP:
2706 printf("TCP: from port %u, to port %u (decimal)\n",
2707 (*cp * 256 + *(cp + 1)),
2708 (*(cp + 2) * 256 + *(cp + 3)));
2709 return;
2710 case IPPROTO_UDP:
2711 printf("UDP: from port %u, to port %u (decimal)\n",
2712 (*cp * 256 + *(cp + 1)),
2713 (*(cp + 2) * 256 + *(cp + 3)));
2714 return;
2715 default:
2716 printf("Unknown Header(%d)\n", nh);
2717 return;
2718 }
2719
2720 if ((cp += hlen) >= end)
2721 goto trunc;
2722 }
2723 if (end - cp < 8)
2724 goto trunc;
2725
2726 putchar('\n');
2727 return;
2728
2729 trunc:
2730 printf("...\n");
2731 return;
2732}
2733
2734void
2735fill(bp, patp)
2736 char *bp, *patp;
2737{
2738 int ii, jj, kk;
2739 int pat[16];
2740 char *cp;
2741
2742 for (cp = patp; *cp; cp++)
2743 if (!isxdigit(*cp))
2744 errx(1, "patterns must be specified as hex digits");
2745 ii = sscanf(patp,
2746 "%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x",
2747 &pat[0], &pat[1], &pat[2], &pat[3], &pat[4], &pat[5], &pat[6],
2748 &pat[7], &pat[8], &pat[9], &pat[10], &pat[11], &pat[12],
2749 &pat[13], &pat[14], &pat[15]);
2750
2751/* xxx */
2752 if (ii > 0)
2753 for (kk = 0;
2754 kk <= MAXDATALEN - (8 + sizeof(struct tv32) + ii);
2755 kk += ii)
2756 for (jj = 0; jj < ii; ++jj)
2757 bp[jj + kk] = pat[jj];
2758 if (!(options & F_QUIET)) {
2759 (void)printf("PATTERN: 0x");
2760 for (jj = 0; jj < ii; ++jj)
2761 (void)printf("%02x", bp[jj] & 0xFF);
2762 (void)printf("\n");
2763 }
2764}
2765
2766#ifdef IPSEC
2767#ifdef IPSEC_POLICY_IPSEC
2768int
2769setpolicy(so, policy)
2770 int so;
2771 char *policy;
2772{
2773 char *buf;
2774
2775 if (policy == NULL)
2776 return 0; /* ignore */
2777
2778 buf = ipsec_set_policy(policy, strlen(policy));
2779 if (buf == NULL)
2780 errx(1, "%s", ipsec_strerror());
2781 if (setsockopt(s, IPPROTO_IPV6, IPV6_IPSEC_POLICY, buf,
2782 ipsec_get_policylen(buf)) < 0)
2783 warnx("Unable to set IPsec policy");
2784 free(buf);
2785
2786 return 0;
2787}
2788#endif
2789#endif
2790
2791char *
2792nigroup(name)
2793 char *name;
2794{
2795 char *p;
2796 char *q;
2797 MD5_CTX ctxt;
2798 u_int8_t digest[16];
2799 u_int8_t c;
2800 size_t l;
2801 char hbuf[NI_MAXHOST];
2802 struct in6_addr in6;
2803
2804 p = strchr(name, '.');
2805 if (!p)
2806 p = name + strlen(name);
2807 l = p - name;
2808 if (l > 63 || l > sizeof(hbuf) - 1)
2809 return NULL; /*label too long*/
2810 strncpy(hbuf, name, l);
2811 hbuf[(int)l] = '\0';
2812
2813 for (q = name; *q; q++) {
2814 if (isupper(*(unsigned char *)q))
2815 *q = tolower(*(unsigned char *)q);
2816 }
2817
2818 /* generate 8 bytes of pseudo-random value. */
2819 memset(&ctxt, 0, sizeof(ctxt));
2820 MD5Init(&ctxt);
2821 c = l & 0xff;
2822 MD5Update(&ctxt, &c, sizeof(c));
2823 MD5Update(&ctxt, (unsigned char *)name, l);
2824 MD5Final(digest, &ctxt);
2825
2826 if (inet_pton(AF_INET6, "ff02::2:0000:0000", &in6) != 1)
2827 return NULL; /*XXX*/
2828 bcopy(digest, &in6.s6_addr[12], 4);
2829
2830 if (inet_ntop(AF_INET6, &in6, hbuf, sizeof(hbuf)) == NULL)
2831 return NULL;
2832
2833 return strdup(hbuf);
2834}
2835
2836void
2837usage()
2838{
2839 (void)fprintf(stderr,
2840#if defined(IPSEC) && !defined(IPSEC_POLICY_IPSEC)
2841 "A"
2842#endif
2843 "usage: ping6 [-"
2844 "d"
2845#if defined(IPSEC) && !defined(IPSEC_POLICY_IPSEC)
2846 "E"
2847#endif
2848 "fH"
2849#ifdef IPV6_USE_MIN_MTU
2850 "m"
2851#endif
2852 "nNoqtvwW] "
2853 "[-a addrtype] [-b bufsiz] [-c count] [-g gateway]\n"
2854 " [-h hoplimit] [-I interface] [-i wait] [-l preload]"
2855#if defined(IPSEC) && defined(IPSEC_POLICY_IPSEC)
2856 " [-P policy]"
2857#endif
2858 "\n"
2859 " [-p pattern] [-S sourceaddr] [-s packetsize] [-z tclass] "
2860 "[hops ...] host\n");
2861 exit(1);
2862}