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