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