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