]> git.saurik.com Git - apple/network_cmds.git/blob - ping6.tproj/ping6.c
e1a225e363770a7fbe69c59276edfad1a7844028
[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 #define BIND_8_COMPAT
108
109 #include <sys/param.h>
110 #include <sys/uio.h>
111 #include <sys/socket.h>
112 #include <sys/time.h>
113
114 #include <net/if.h>
115 #include <net/route.h>
116
117 #include <netinet/in.h>
118 #include <netinet/ip6.h>
119 #include <netinet/icmp6.h>
120 #include <arpa/inet.h>
121 #include <arpa/nameser.h>
122 #include <netdb.h>
123
124 #include <ctype.h>
125 #include <err.h>
126 #include <errno.h>
127 #include <fcntl.h>
128 #if defined(__OpenBSD__) || defined(__NetBSD__)
129 #include <math.h>
130 #endif
131 #include <signal.h>
132 #include <stdio.h>
133 #include <stdlib.h>
134 #include <string.h>
135 #include <unistd.h>
136
137 #ifdef IPSEC
138 #include <netinet6/ah.h>
139 #include <netinet6/ipsec.h>
140 #endif
141
142 #if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__OpenBSD__)
143 #include <md5.h>
144 #else
145 #include "md5.h"
146 #endif
147
148 #define MAXPACKETLEN 131072
149 #define IP6LEN 40
150 #define ICMP6ECHOLEN 8 /* icmp echo header len excluding time */
151 #define ICMP6ECHOTMLEN sizeof(struct timeval)
152 #define ICMP6_NIQLEN (ICMP6ECHOLEN + 8)
153 /* FQDN case, 64 bits of nonce + 32 bits ttl */
154 #define ICMP6_NIRLEN (ICMP6ECHOLEN + 12)
155 #define EXTRA 256 /* for AH and various other headers. weird. */
156 #define DEFDATALEN ICMP6ECHOTMLEN
157 #define MAXDATALEN MAXPACKETLEN - IP6LEN - ICMP6ECHOLEN
158 #define NROUTES 9 /* number of record route slots */
159
160 #define A(bit) rcvd_tbl[(bit)>>3] /* identify byte in array */
161 #define B(bit) (1 << ((bit) & 0x07)) /* identify bit in byte */
162 #define SET(bit) (A(bit) |= B(bit))
163 #define CLR(bit) (A(bit) &= (~B(bit)))
164 #define TST(bit) (A(bit) & B(bit))
165
166 #define F_FLOOD 0x0001
167 #define F_INTERVAL 0x0002
168 #define F_PINGFILLED 0x0008
169 #define F_QUIET 0x0010
170 #define F_RROUTE 0x0020
171 #define F_SO_DEBUG 0x0040
172 #define F_VERBOSE 0x0100
173 #ifdef IPSEC
174 #ifdef IPSEC_POLICY_IPSEC
175 #define F_POLICY 0x0400
176 #else
177 #define F_AUTHHDR 0x0200
178 #define F_ENCRYPT 0x0400
179 #endif /*IPSEC_POLICY_IPSEC*/
180 #endif /*IPSEC*/
181 #define F_NODEADDR 0x0800
182 #define F_FQDN 0x1000
183 #define F_INTERFACE 0x2000
184 #define F_SRCADDR 0x4000
185 #ifdef IPV6_REACHCONF
186 #define F_REACHCONF 0x8000
187 #endif
188 #define F_HOSTNAME 0x10000
189 #define F_FQDNOLD 0x20000
190 #define F_NIGROUP 0x40000
191 #define F_SUPTYPES 0x80000
192 #define F_NOMINMTU 0x100000
193 #define F_NOUSERDATA (F_NODEADDR | F_FQDN | F_FQDNOLD | F_SUPTYPES)
194 u_int options;
195
196 #define IN6LEN sizeof(struct in6_addr)
197 #define SA6LEN sizeof(struct sockaddr_in6)
198 #define DUMMY_PORT 10101
199
200 #define SIN6(s) ((struct sockaddr_in6 *)(s))
201
202 /*
203 * MAX_DUP_CHK is the number of bits in received table, i.e. the maximum
204 * number of received sequence numbers we can keep track of. Change 128
205 * to 8192 for complete accuracy...
206 */
207 #define MAX_DUP_CHK (8 * 8192)
208 int mx_dup_ck = MAX_DUP_CHK;
209 char rcvd_tbl[MAX_DUP_CHK / 8];
210
211 struct addrinfo *res;
212 struct sockaddr_in6 dst; /* who to ping6 */
213 struct sockaddr_in6 src; /* src addr of this packet */
214 int datalen = DEFDATALEN;
215 int s; /* socket file descriptor */
216 u_char outpack[MAXPACKETLEN];
217 char BSPACE = '\b'; /* characters written for flood */
218 char DOT = '.';
219 char *hostname;
220 int ident; /* process id to identify our packets */
221 u_int8_t nonce[8]; /* nonce field for node information */
222 struct in6_addr srcaddr;
223 int hoplimit = -1; /* hoplimit */
224 int pathmtu = 0; /* path MTU for the destination. 0 = unspec. */
225
226 /* counters */
227 long npackets; /* max packets to transmit */
228 long nreceived; /* # of packets we got back */
229 long nrepeats; /* number of duplicates */
230 long ntransmitted; /* sequence # for outbound packets = #sent */
231 struct timeval interval = {1, 0}; /* interval between packets */
232
233 /* timing */
234 int timing; /* flag to do timing */
235 double tmin = 999999999.0; /* minimum round trip time */
236 double tmax = 0.0; /* maximum round trip time */
237 double tsum = 0.0; /* sum of all times, for doing average */
238 #if defined(__OpenBSD__) || defined(__NetBSD__)
239 double tsumsq = 0.0; /* sum of all times squared, for std. dev. */
240 #endif
241
242 /* for node addresses */
243 u_short naflags;
244
245 /* for ancillary data(advanced API) */
246 struct msghdr smsghdr;
247 struct iovec smsgiov;
248 char *scmsg = 0;
249
250 volatile int signo;
251 volatile sig_atomic_t seenalrm;
252 volatile sig_atomic_t seenint;
253 #ifdef SIGINFO
254 volatile sig_atomic_t seeninfo;
255 #endif
256
257 int main __P((int, char *[]));
258 void fill __P((char *, char *));
259 int get_hoplim __P((struct msghdr *));
260 int get_pathmtu __P((struct msghdr *));
261 void set_pathmtu __P((int));
262 struct in6_pktinfo *get_rcvpktinfo __P((struct msghdr *));
263 void onsignal __P((int));
264 void retransmit __P((void));
265 void onint __P((int));
266 size_t pingerlen __P((void));
267 int pinger __P((void));
268 const char *pr_addr __P((struct sockaddr *, int));
269 void pr_icmph __P((struct icmp6_hdr *, u_char *));
270 void pr_iph __P((struct ip6_hdr *));
271 void pr_suptypes __P((struct icmp6_nodeinfo *, size_t));
272 void pr_nodeaddr __P((struct icmp6_nodeinfo *, int));
273 int myechoreply __P((const struct icmp6_hdr *));
274 int mynireply __P((const struct icmp6_nodeinfo *));
275 char *dnsdecode __P((const u_char **, const u_char *, const u_char *,
276 u_char *, size_t));
277 void pr_pack __P((u_char *, int, struct msghdr *));
278 void pr_exthdrs __P((struct msghdr *));
279 void pr_ip6opt __P((void *));
280 void pr_rthdr __P((void *));
281 int pr_bitrange __P((u_int32_t, int, int));
282 void pr_retip __P((struct ip6_hdr *, u_char *));
283 void summary __P((void));
284 void tvsub __P((struct timeval *, struct timeval *));
285 int setpolicy __P((int, char *));
286 char *nigroup __P((char *));
287 void usage __P((void));
288
289 int
290 main(argc, argv)
291 int argc;
292 char *argv[];
293 {
294 struct itimerval itimer;
295 struct sockaddr_in6 from;
296 struct timeval timeout, *tv;
297 struct addrinfo hints;
298 fd_set *fdmaskp;
299 int fdmasks;
300 register int cc, i;
301 int ch, fromlen, hold, packlen, preload, optval, ret_ga;
302 u_char *datap, *packet;
303 char *e, *target, *ifname = NULL;
304 int ip6optlen = 0;
305 struct cmsghdr *scmsgp = NULL;
306 int sockbufsize = 0;
307 int usepktinfo = 0;
308 struct in6_pktinfo *pktinfo = NULL;
309 #ifdef USE_RFC2292BIS
310 struct ip6_rthdr *rthdr = NULL;
311 #endif
312 #ifdef IPSEC_POLICY_IPSEC
313 char *policy_in = NULL;
314 char *policy_out = NULL;
315 #endif
316 double intval;
317 size_t rthlen;
318
319 /* just to be sure */
320 memset(&smsghdr, 0, sizeof(&smsghdr));
321 memset(&smsgiov, 0, sizeof(&smsgiov));
322
323 preload = 0;
324 datap = &outpack[ICMP6ECHOLEN + ICMP6ECHOTMLEN];
325 #ifndef IPSEC
326 #define ADDOPTS
327 #else
328 #ifdef IPSEC_POLICY_IPSEC
329 #define ADDOPTS "P:"
330 #else
331 #define ADDOPTS "AE"
332 #endif /*IPSEC_POLICY_IPSEC*/
333 #endif
334 while ((ch = getopt(argc, argv,
335 "a:b:c:dfHh:I:i:l:mnNp:qRS:s:tvwW" ADDOPTS)) != -1) {
336 #undef ADDOPTS
337 switch (ch) {
338 case 'a':
339 {
340 char *cp;
341
342 options &= ~F_NOUSERDATA;
343 options |= F_NODEADDR;
344 for (cp = optarg; *cp != '\0'; cp++) {
345 switch (*cp) {
346 case 'a':
347 naflags |= NI_NODEADDR_FLAG_ALL;
348 break;
349 case 'c':
350 case 'C':
351 naflags |= NI_NODEADDR_FLAG_COMPAT;
352 break;
353 case 'l':
354 case 'L':
355 naflags |= NI_NODEADDR_FLAG_LINKLOCAL;
356 break;
357 case 's':
358 case 'S':
359 naflags |= NI_NODEADDR_FLAG_SITELOCAL;
360 break;
361 case 'g':
362 case 'G':
363 naflags |= NI_NODEADDR_FLAG_GLOBAL;
364 break;
365 case 'A': /* experimental. not in the spec */
366 #ifdef NI_NODEADDR_FLAG_ANYCAST
367 naflags |= NI_NODEADDR_FLAG_ANYCAST;
368 break;
369 #else
370 errx(1,
371 "-a A is not supported on the platform");
372 /*NOTREACHED*/
373 #endif
374 default:
375 usage();
376 /*NOTREACHED*/
377 }
378 }
379 break;
380 }
381 case 'b':
382 #if defined(SO_SNDBUF) && defined(SO_RCVBUF)
383 sockbufsize = atoi(optarg);
384 #else
385 err(1,
386 "-b option ignored: SO_SNDBUF/SO_RCVBUF socket options not supported");
387 #endif
388 break;
389 case 'c':
390 npackets = strtol(optarg, &e, 10);
391 if (npackets <= 0 || *optarg == '\0' || *e != '\0')
392 errx(1,
393 "illegal number of packets -- %s", optarg);
394 break;
395 case 'd':
396 options |= F_SO_DEBUG;
397 break;
398 case 'f':
399 if (getuid()) {
400 errno = EPERM;
401 errx(1, "Must be superuser to flood ping");
402 }
403 options |= F_FLOOD;
404 setbuf(stdout, (char *)NULL);
405 break;
406 case 'H':
407 options |= F_HOSTNAME;
408 break;
409 case 'h': /* hoplimit */
410 hoplimit = strtol(optarg, &e, 10);
411 if (255 < hoplimit || hoplimit < -1)
412 errx(1,
413 "illegal hoplimit -- %s", optarg);
414 break;
415 case 'I':
416 ifname = optarg;
417 options |= F_INTERFACE;
418 #ifndef USE_SIN6_SCOPE_ID
419 usepktinfo++;
420 #endif
421 break;
422 case 'i': /* wait between sending packets */
423 intval = strtod(optarg, &e);
424 if (*optarg == '\0' || *e != '\0')
425 errx(1, "illegal timing interval %s", optarg);
426 if (intval < 1 && getuid()) {
427 errx(1, "%s: only root may use interval < 1s",
428 strerror(EPERM));
429 }
430 interval.tv_sec = (long)intval;
431 interval.tv_usec =
432 (long)((intval - interval.tv_sec) * 1000000);
433 if (interval.tv_sec < 0)
434 errx(1, "illegal timing interval %s", optarg);
435 /* less than 1/hz does not make sense */
436 if (interval.tv_sec == 0 && interval.tv_usec < 10000) {
437 warnx("too small interval, raised to 0.01");
438 interval.tv_usec = 10000;
439 }
440 options |= F_INTERVAL;
441 break;
442 case 'l':
443 if (getuid()) {
444 errno = EPERM;
445 errx(1, "Must be superuser to preload");
446 }
447 preload = strtol(optarg, &e, 10);
448 if (preload < 0 || *optarg == '\0' || *e != '\0')
449 errx(1, "illegal preload value -- %s", optarg);
450 break;
451 case 'm':
452 #ifdef IPV6_USE_MIN_MTU
453 options |= F_NOMINMTU;
454 break;
455 #else
456 errx(1, "-%c is not supported on this platform", ch);
457 /*NOTREACHED*/
458 #endif
459 case 'n':
460 options &= ~F_HOSTNAME;
461 break;
462 case 'N':
463 options |= F_NIGROUP;
464 break;
465 case 'p': /* fill buffer with user pattern */
466 options |= F_PINGFILLED;
467 fill((char *)datap, optarg);
468 break;
469 case 'q':
470 options |= F_QUIET;
471 break;
472 case 'R':
473 #ifdef IPV6_REACHCONF
474 options |= F_REACHCONF;
475 break;
476 #else
477 errx(1, "-R is not supported in this configuration");
478 #endif
479 case 'S':
480 /* XXX: use getaddrinfo? */
481 if (inet_pton(AF_INET6, optarg, (void *)&srcaddr) != 1)
482 errx(1, "invalid IPv6 address: %s", optarg);
483 options |= F_SRCADDR;
484 usepktinfo++;
485 break;
486 case 's': /* size of packet to send */
487 datalen = strtol(optarg, &e, 10);
488 if (datalen <= 0 || *optarg == '\0' || *e != '\0')
489 errx(1, "illegal datalen value -- %s", optarg);
490 if (datalen > MAXDATALEN) {
491 errx(1,
492 "datalen value too large, maximum is %d",
493 MAXDATALEN);
494 }
495 break;
496 case 't':
497 options &= ~F_NOUSERDATA;
498 options |= F_SUPTYPES;
499 break;
500 case 'v':
501 options |= F_VERBOSE;
502 break;
503 case 'w':
504 options &= ~F_NOUSERDATA;
505 options |= F_FQDN;
506 break;
507 case 'W':
508 options &= ~F_NOUSERDATA;
509 options |= F_FQDNOLD;
510 break;
511 #ifdef IPSEC
512 #ifdef IPSEC_POLICY_IPSEC
513 case 'P':
514 options |= F_POLICY;
515 if (!strncmp("in", optarg, 2)) {
516 if ((policy_in = strdup(optarg)) == NULL)
517 errx(1, "strdup");
518 } else if (!strncmp("out", optarg, 3)) {
519 if ((policy_out = strdup(optarg)) == NULL)
520 errx(1, "strdup");
521 } else
522 errx(1, "invalid security policy");
523 break;
524 #else
525 case 'A':
526 options |= F_AUTHHDR;
527 break;
528 case 'E':
529 options |= F_ENCRYPT;
530 break;
531 #endif /*IPSEC_POLICY_IPSEC*/
532 #endif /*IPSEC*/
533 default:
534 usage();
535 /*NOTREACHED*/
536 }
537 }
538 argc -= optind;
539 argv += optind;
540
541 if (argc < 1) {
542 usage();
543 /*NOTREACHED*/
544 }
545
546 if (argc > 1) {
547 #ifdef IPV6_RECVRTHDR /* 2292bis */
548 rthlen = CMSG_SPACE(inet6_rth_space(IPV6_RTHDR_TYPE_0,
549 argc - 1));
550 #else /* RFC2292 */
551 rthlen = inet6_rthdr_space(IPV6_RTHDR_TYPE_0, argc - 1);
552 #endif
553 if (rthlen == 0) {
554 errx(1, "too many intermediate hops");
555 /*NOTREACHED*/
556 }
557 ip6optlen += rthlen;
558 }
559
560 if (options & F_NIGROUP) {
561 target = nigroup(argv[argc - 1]);
562 if (target == NULL) {
563 usage();
564 /*NOTREACHED*/
565 }
566 } else
567 target = argv[argc - 1];
568
569 /* getaddrinfo */
570 bzero(&hints, sizeof(struct addrinfo));
571 hints.ai_flags = AI_CANONNAME;
572 hints.ai_family = AF_INET6;
573 hints.ai_socktype = SOCK_RAW;
574 hints.ai_protocol = IPPROTO_ICMPV6;
575
576 ret_ga = getaddrinfo(target, NULL, &hints, &res);
577 if (ret_ga) {
578 fprintf(stderr, "ping6: %s\n", gai_strerror(ret_ga));
579 exit(1);
580 }
581 if (res->ai_canonname)
582 hostname = res->ai_canonname;
583 else
584 hostname = target;
585
586 if (!res->ai_addr)
587 errx(1, "getaddrinfo failed");
588
589 (void)memcpy(&dst, res->ai_addr, res->ai_addrlen);
590
591 res->ai_socktype = SOCK_RAW;
592 res->ai_protocol = IPPROTO_ICMPV6;
593
594 if ((s = socket(res->ai_family, res->ai_socktype,
595 res->ai_protocol)) < 0)
596 err(1, "socket");
597
598 /*
599 * let the kerel pass extension headers of incoming packets,
600 * for privileged socket options
601 */
602 if ((options & F_VERBOSE) != 0) {
603 int opton = 1;
604
605 #ifdef IPV6_RECVHOPOPTS
606 if (setsockopt(s, IPPROTO_IPV6, IPV6_RECVHOPOPTS, &opton,
607 sizeof(opton)))
608 err(1, "setsockopt(IPV6_RECVHOPOPTS)");
609 #else /* old adv. API */
610 if (setsockopt(s, IPPROTO_IPV6, IPV6_HOPOPTS, &opton,
611 sizeof(opton)))
612 err(1, "setsockopt(IPV6_HOPOPTS)");
613 #endif
614 #ifdef IPV6_RECVDSTOPTS
615 if (setsockopt(s, IPPROTO_IPV6, IPV6_RECVDSTOPTS, &opton,
616 sizeof(opton)))
617 err(1, "setsockopt(IPV6_RECVDSTOPTS)");
618 #else /* old adv. API */
619 if (setsockopt(s, IPPROTO_IPV6, IPV6_DSTOPTS, &opton,
620 sizeof(opton)))
621 err(1, "setsockopt(IPV6_DSTOPTS)");
622 #endif
623 #ifdef IPV6_RECVRTHDRDSTOPTS
624 if (setsockopt(s, IPPROTO_IPV6, IPV6_RECVRTHDRDSTOPTS, &opton,
625 sizeof(opton)))
626 err(1, "setsockopt(IPV6_RECVRTHDRDSTOPTS)");
627 #endif
628 }
629
630 /* revoke root privilege */
631 seteuid(getuid());
632 setuid(getuid());
633
634 if (options & F_FLOOD && options & F_INTERVAL)
635 errx(1, "-f and -i incompatible options");
636
637 if ((options & F_NOUSERDATA) == 0) {
638 if (datalen >= sizeof(struct timeval)) {
639 /* we can time transfer */
640 timing = 1;
641 } else
642 timing = 0;
643 /* in F_VERBOSE case, we may get non-echoreply packets*/
644 if (options & F_VERBOSE)
645 packlen = 2048 + IP6LEN + ICMP6ECHOLEN + EXTRA;
646 else
647 packlen = datalen + IP6LEN + ICMP6ECHOLEN + EXTRA;
648 } else {
649 /* suppress timing for node information query */
650 timing = 0;
651 datalen = 2048;
652 packlen = 2048 + IP6LEN + ICMP6ECHOLEN + EXTRA;
653 }
654
655 if (!(packet = (u_char *)malloc((u_int)packlen)))
656 err(1, "Unable to allocate packet");
657 if (!(options & F_PINGFILLED))
658 for (i = ICMP6ECHOLEN; i < packlen; ++i)
659 *datap++ = i;
660
661 ident = getpid() & 0xFFFF;
662 #ifndef __OpenBSD__
663 gettimeofday(&timeout, NULL);
664 srand((unsigned int)(timeout.tv_sec ^ timeout.tv_usec ^ (long)ident));
665 memset(nonce, 0, sizeof(nonce));
666 for (i = 0; i < sizeof(nonce); i += sizeof(int))
667 *((int *)&nonce[i]) = rand();
668 #else
669 memset(nonce, 0, sizeof(nonce));
670 for (i = 0; i < sizeof(nonce); i += sizeof(u_int32_t))
671 *((u_int32_t *)&nonce[i]) = arc4random();
672 #endif
673
674 hold = 1;
675
676 if (options & F_SO_DEBUG)
677 (void)setsockopt(s, SOL_SOCKET, SO_DEBUG, (char *)&hold,
678 sizeof(hold));
679 optval = IPV6_DEFHLIM;
680 if (IN6_IS_ADDR_MULTICAST(&dst.sin6_addr))
681 if (setsockopt(s, IPPROTO_IPV6, IPV6_MULTICAST_HOPS,
682 &optval, sizeof(optval)) == -1)
683 err(1, "IPV6_MULTICAST_HOPS");
684 #ifdef IPV6_USE_MIN_MTU
685 if ((options & F_NOMINMTU) == 0) {
686 optval = 1;
687 if (setsockopt(s, IPPROTO_IPV6, IPV6_USE_MIN_MTU,
688 &optval, sizeof(optval)) == -1)
689 err(1, "setsockopt(IPV6_USE_MIN_MTU)");
690 }
691 #ifdef IPV6_RECVPATHMTU
692 else {
693 optval = 1;
694 if (setsockopt(s, IPPROTO_IPV6, IPV6_RECVPATHMTU,
695 &optval, sizeof(optval)) == -1)
696 err(1, "setsockopt(IPV6_RECVPATHMTU)");
697 }
698 #endif /* IPV6_RECVPATHMTU */
699 #endif /* IPV6_USE_MIN_MTU */
700
701 #ifdef IPSEC
702 #ifdef IPSEC_POLICY_IPSEC
703 if (options & F_POLICY) {
704 if (setpolicy(s, policy_in) < 0)
705 errx(1, "%s", ipsec_strerror());
706 if (setpolicy(s, policy_out) < 0)
707 errx(1, "%s", ipsec_strerror());
708 }
709 #else
710 if (options & F_AUTHHDR) {
711 optval = IPSEC_LEVEL_REQUIRE;
712 #ifdef IPV6_AUTH_TRANS_LEVEL
713 if (setsockopt(s, IPPROTO_IPV6, IPV6_AUTH_TRANS_LEVEL,
714 &optval, sizeof(optval)) == -1)
715 err(1, "setsockopt(IPV6_AUTH_TRANS_LEVEL)");
716 #else /* old def */
717 if (setsockopt(s, IPPROTO_IPV6, IPV6_AUTH_LEVEL,
718 &optval, sizeof(optval)) == -1)
719 err(1, "setsockopt(IPV6_AUTH_LEVEL)");
720 #endif
721 }
722 if (options & F_ENCRYPT) {
723 optval = IPSEC_LEVEL_REQUIRE;
724 if (setsockopt(s, IPPROTO_IPV6, IPV6_ESP_TRANS_LEVEL,
725 &optval, sizeof(optval)) == -1)
726 err(1, "setsockopt(IPV6_ESP_TRANS_LEVEL)");
727 }
728 #endif /*IPSEC_POLICY_IPSEC*/
729 #endif
730
731 #ifdef ICMP6_FILTER
732 {
733 struct icmp6_filter filt;
734 if (!(options & F_VERBOSE)) {
735 ICMP6_FILTER_SETBLOCKALL(&filt);
736 if ((options & F_FQDN) || (options & F_FQDNOLD) ||
737 (options & F_NODEADDR) || (options & F_SUPTYPES))
738 ICMP6_FILTER_SETPASS(ICMP6_NI_REPLY, &filt);
739 else
740 ICMP6_FILTER_SETPASS(ICMP6_ECHO_REPLY, &filt);
741 } else {
742 ICMP6_FILTER_SETPASSALL(&filt);
743 }
744 if (setsockopt(s, IPPROTO_ICMPV6, ICMP6_FILTER, &filt,
745 sizeof(filt)) < 0)
746 err(1, "setsockopt(ICMP6_FILTER)");
747 }
748 #endif /*ICMP6_FILTER*/
749
750 /* let the kerel pass extension headers of incoming packets */
751 if ((options & F_VERBOSE) != 0) {
752 int opton = 1;
753
754 #ifdef IPV6_RECVRTHDR
755 if (setsockopt(s, IPPROTO_IPV6, IPV6_RECVRTHDR, &opton,
756 sizeof(opton)))
757 err(1, "setsockopt(IPV6_RECVRTHDR)");
758 #else /* old adv. API */
759 if (setsockopt(s, IPPROTO_IPV6, IPV6_RTHDR, &opton,
760 sizeof(opton)))
761 err(1, "setsockopt(IPV6_RTHDR)");
762 #endif
763 }
764
765 /*
766 optval = 1;
767 if (IN6_IS_ADDR_MULTICAST(&dst.sin6_addr))
768 if (setsockopt(s, IPPROTO_IPV6, IPV6_MULTICAST_LOOP,
769 &optval, sizeof(optval)) == -1)
770 err(1, "IPV6_MULTICAST_LOOP");
771 */
772
773 /* Specify the outgoing interface and/or the source address */
774 if (usepktinfo)
775 ip6optlen += CMSG_SPACE(sizeof(struct in6_pktinfo));
776
777 if (hoplimit != -1)
778 ip6optlen += CMSG_SPACE(sizeof(int));
779
780 #ifdef IPV6_REACHCONF
781 if (options & F_REACHCONF)
782 ip6optlen += CMSG_SPACE(0);
783 #endif
784
785 /* set IP6 packet options */
786 if (ip6optlen) {
787 if ((scmsg = (char *)malloc(ip6optlen)) == 0)
788 errx(1, "can't allocate enough memory");
789 smsghdr.msg_control = (caddr_t)scmsg;
790 smsghdr.msg_controllen = ip6optlen;
791 scmsgp = (struct cmsghdr *)scmsg;
792 }
793 if (usepktinfo) {
794 pktinfo = (struct in6_pktinfo *)(CMSG_DATA(scmsgp));
795 memset(pktinfo, 0, sizeof(*pktinfo));
796 scmsgp->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo));
797 scmsgp->cmsg_level = IPPROTO_IPV6;
798 scmsgp->cmsg_type = IPV6_PKTINFO;
799 scmsgp = CMSG_NXTHDR(&smsghdr, scmsgp);
800 }
801
802 /* set the outgoing interface */
803 if (ifname) {
804 #ifndef USE_SIN6_SCOPE_ID
805 /* pktinfo must have already been allocated */
806 if ((pktinfo->ipi6_ifindex = if_nametoindex(ifname)) == 0)
807 errx(1, "%s: invalid interface name", ifname);
808 #else
809 if ((dst.sin6_scope_id = if_nametoindex(ifname)) == 0)
810 errx(1, "%s: invalid interface name", ifname);
811 #endif
812 }
813 /* set the source address */
814 if (options & F_SRCADDR)/* pktinfo must be valid */
815 pktinfo->ipi6_addr = srcaddr;
816
817 if (hoplimit != -1) {
818 scmsgp->cmsg_len = CMSG_LEN(sizeof(int));
819 scmsgp->cmsg_level = IPPROTO_IPV6;
820 scmsgp->cmsg_type = IPV6_HOPLIMIT;
821 *(int *)(CMSG_DATA(scmsgp)) = hoplimit;
822
823 scmsgp = CMSG_NXTHDR(&smsghdr, scmsgp);
824 }
825 #ifdef IPV6_REACHCONF
826 if (options & F_REACHCONF) {
827 scmsgp->cmsg_len = CMSG_LEN(0);
828 scmsgp->cmsg_level = IPPROTO_IPV6;
829 scmsgp->cmsg_type = IPV6_REACHCONF;
830
831 scmsgp = CMSG_NXTHDR(&smsghdr, scmsgp);
832 }
833 #endif
834
835 if (argc > 1) { /* some intermediate addrs are specified */
836 int hops, error;
837 #ifdef USE_RFC2292BIS
838 int rthdrlen;
839 #endif
840
841 #ifdef USE_RFC2292BIS
842 rthdrlen = inet6_rth_space(IPV6_RTHDR_TYPE_0, argc - 1);
843 scmsgp->cmsg_len = CMSG_LEN(rthdrlen);
844 scmsgp->cmsg_level = IPPROTO_IPV6;
845 scmsgp->cmsg_type = IPV6_RTHDR;
846 rthdr = (struct ip6_rthdr *)CMSG_DATA(scmsgp);
847 rthdr = inet6_rth_init((void *)rthdr, rthdrlen,
848 IPV6_RTHDR_TYPE_0, argc - 1);
849 if (rthdr == NULL)
850 errx(1, "can't initialize rthdr");
851 #else /* old advanced API */
852 if ((scmsgp = (struct cmsghdr *)inet6_rthdr_init(scmsgp,
853 IPV6_RTHDR_TYPE_0)) == 0)
854 errx(1, "can't initialize rthdr");
855 #endif /* USE_RFC2292BIS */
856
857 for (hops = 0; hops < argc - 1; hops++) {
858 struct addrinfo *iaip;
859
860 if ((error = getaddrinfo(argv[hops], NULL, &hints,
861 &iaip)))
862 errx(1, "%s", gai_strerror(error));
863 if (SIN6(iaip->ai_addr)->sin6_family != AF_INET6)
864 errx(1,
865 "bad addr family of an intermediate addr");
866
867 #ifdef USE_RFC2292BIS
868 if (inet6_rth_add(rthdr,
869 &(SIN6(iaip->ai_addr))->sin6_addr))
870 errx(1, "can't add an intermediate node");
871 #else /* old advanced API */
872 if (inet6_rthdr_add(scmsgp,
873 &(SIN6(iaip->ai_addr))->sin6_addr,
874 IPV6_RTHDR_LOOSE))
875 errx(1, "can't add an intermediate node");
876 #endif /* USE_RFC2292BIS */
877 freeaddrinfo(iaip);
878 }
879
880 #ifndef USE_RFC2292BIS
881 if (inet6_rthdr_lasthop(scmsgp, IPV6_RTHDR_LOOSE))
882 errx(1, "can't set the last flag");
883 #endif
884
885 scmsgp = CMSG_NXTHDR(&smsghdr, scmsgp);
886 }
887
888 {
889 /*
890 * source selection
891 */
892 int dummy, 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[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(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, cresult,
1350 sizeof(cresult)) == NULL)
1351 return NULL;
1352 if (strlcat(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(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 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[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), 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;
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[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, 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 = 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, 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 }