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