ping6.c revision 1.9 1 1.9 itojun /* $NetBSD: ping6.c,v 1.9 1999/12/13 15:26:00 itojun Exp $ */
2 1.6 itojun
3 1.1 itojun /*
4 1.1 itojun * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5 1.1 itojun * All rights reserved.
6 1.1 itojun *
7 1.1 itojun * Redistribution and use in source and binary forms, with or without
8 1.1 itojun * modification, are permitted provided that the following conditions
9 1.1 itojun * are met:
10 1.1 itojun * 1. Redistributions of source code must retain the above copyright
11 1.1 itojun * notice, this list of conditions and the following disclaimer.
12 1.1 itojun * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 itojun * notice, this list of conditions and the following disclaimer in the
14 1.1 itojun * documentation and/or other materials provided with the distribution.
15 1.1 itojun * 3. Neither the name of the project nor the names of its contributors
16 1.1 itojun * may be used to endorse or promote products derived from this software
17 1.1 itojun * without specific prior written permission.
18 1.1 itojun *
19 1.1 itojun * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20 1.1 itojun * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 1.1 itojun * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 1.1 itojun * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23 1.1 itojun * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 1.1 itojun * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 1.1 itojun * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 1.1 itojun * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 1.1 itojun * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 1.1 itojun * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 1.1 itojun * SUCH DAMAGE.
30 1.1 itojun */
31 1.1 itojun
32 1.1 itojun /* BSDI ping.c,v 2.3 1996/01/21 17:56:50 jch Exp */
33 1.1 itojun
34 1.1 itojun /*
35 1.1 itojun * Copyright (c) 1989, 1993
36 1.1 itojun * The Regents of the University of California. All rights reserved.
37 1.1 itojun *
38 1.1 itojun * This code is derived from software contributed to Berkeley by
39 1.1 itojun * Mike Muuss.
40 1.1 itojun *
41 1.1 itojun * Redistribution and use in source and binary forms, with or without
42 1.1 itojun * modification, are permitted provided that the following conditions
43 1.1 itojun * are met:
44 1.1 itojun * 1. Redistributions of source code must retain the above copyright
45 1.1 itojun * notice, this list of conditions and the following disclaimer.
46 1.1 itojun * 2. Redistributions in binary form must reproduce the above copyright
47 1.1 itojun * notice, this list of conditions and the following disclaimer in the
48 1.1 itojun * documentation and/or other materials provided with the distribution.
49 1.1 itojun * 3. All advertising materials mentioning features or use of this software
50 1.1 itojun * must display the following acknowledgement:
51 1.1 itojun * This product includes software developed by the University of
52 1.1 itojun * California, Berkeley and its contributors.
53 1.1 itojun * 4. Neither the name of the University nor the names of its contributors
54 1.1 itojun * may be used to endorse or promote products derived from this software
55 1.1 itojun * without specific prior written permission.
56 1.1 itojun *
57 1.1 itojun * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
58 1.1 itojun * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
59 1.1 itojun * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
60 1.1 itojun * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
61 1.1 itojun * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
62 1.1 itojun * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
63 1.1 itojun * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
64 1.1 itojun * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
65 1.1 itojun * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
66 1.1 itojun * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
67 1.1 itojun * SUCH DAMAGE.
68 1.1 itojun */
69 1.1 itojun
70 1.4 itojun #if 0
71 1.1 itojun #ifndef lint
72 1.6 itojun static char copyright[] =
73 1.1 itojun "@(#) Copyright (c) 1989, 1993\n\
74 1.6 itojun The Regents of the University of California. All rights reserved.\n";
75 1.6 itojun #endif /* not lint */
76 1.6 itojun
77 1.6 itojun #ifndef lint
78 1.6 itojun static char sccsid[] = "@(#)ping.c 8.1 (Berkeley) 6/5/93";
79 1.1 itojun #endif /* not lint */
80 1.6 itojun #else
81 1.6 itojun #include <sys/cdefs.h>
82 1.6 itojun #ifndef lint
83 1.9 itojun __RCSID("$NetBSD: ping6.c,v 1.9 1999/12/13 15:26:00 itojun Exp $");
84 1.4 itojun #endif
85 1.1 itojun #endif
86 1.1 itojun
87 1.1 itojun /*
88 1.1 itojun * Using the InterNet Control Message Protocol (ICMP) "ECHO" facility,
89 1.1 itojun * measure round-trip-delays and packet loss across network paths.
90 1.1 itojun *
91 1.1 itojun * Author -
92 1.1 itojun * Mike Muuss
93 1.1 itojun * U. S. Army Ballistic Research Laboratory
94 1.1 itojun * December, 1983
95 1.1 itojun *
96 1.1 itojun * Status -
97 1.1 itojun * Public Domain. Distribution Unlimited.
98 1.1 itojun * Bugs -
99 1.1 itojun * More statistics could always be gathered.
100 1.1 itojun * This program has to run SUID to ROOT to access the ICMP socket.
101 1.1 itojun */
102 1.9 itojun /*
103 1.9 itojun * NOTE:
104 1.9 itojun * USE_SIN6_SCOPE_ID assumes that sin6_scope_id has the same semantics
105 1.9 itojun * as IPV6_PKTINFO. Some objects it (sin6_scope_id specifies *link* while
106 1.9 itojun * IPV6_PKTINFO specifies *interface*. Link is defined as collection of
107 1.9 itojun * network attached to 1 or more interfaces)
108 1.9 itojun */
109 1.1 itojun
110 1.1 itojun #include <sys/param.h>
111 1.1 itojun #include <sys/uio.h>
112 1.1 itojun #include <sys/socket.h>
113 1.1 itojun #include <sys/time.h>
114 1.1 itojun
115 1.1 itojun #include <net/if.h>
116 1.1 itojun #include <net/route.h>
117 1.1 itojun
118 1.1 itojun #include <netinet/in_systm.h>
119 1.1 itojun #include <netinet/in.h>
120 1.1 itojun #include <netinet/ip.h>
121 1.1 itojun #include <netinet/ip_var.h>
122 1.1 itojun #include <netinet/ip6.h>
123 1.1 itojun #include <netinet/icmp6.h>
124 1.1 itojun #include <arpa/inet.h>
125 1.1 itojun #include <netdb.h>
126 1.1 itojun
127 1.1 itojun #include <ctype.h>
128 1.1 itojun #include <err.h>
129 1.1 itojun #include <errno.h>
130 1.1 itojun #include <fcntl.h>
131 1.1 itojun #include <signal.h>
132 1.1 itojun #include <stdio.h>
133 1.1 itojun #include <stdlib.h>
134 1.1 itojun #include <string.h>
135 1.1 itojun #include <unistd.h>
136 1.1 itojun
137 1.1 itojun #ifdef IPSEC
138 1.9 itojun #include <netinet6/ah.h>
139 1.1 itojun #include <netinet6/ipsec.h>
140 1.1 itojun #endif
141 1.1 itojun
142 1.1 itojun #define MAXPACKETLEN 131072
143 1.1 itojun #define IP6LEN 40
144 1.1 itojun #define ICMP6ECHOLEN 8 /* icmp echo header len excluding time */
145 1.1 itojun #define ICMP6ECHOTMLEN sizeof(struct timeval)
146 1.1 itojun #define ICMP6_NIQLEN (ICMP6ECHOLEN + 8)
147 1.1 itojun #define ICMP6_NIRLEN (ICMP6ECHOLEN + 12) /* 64 bits of nonce + 32 bits ttl */
148 1.1 itojun #define EXTRA 256 /* for AH and various other headers. weird. */
149 1.1 itojun #define DEFDATALEN ICMP6ECHOTMLEN
150 1.1 itojun #define MAXDATALEN MAXPACKETLEN - IP6LEN - ICMP6ECHOLEN
151 1.1 itojun #define NROUTES 9 /* number of record route slots */
152 1.1 itojun
153 1.1 itojun #define A(bit) rcvd_tbl[(bit)>>3] /* identify byte in array */
154 1.1 itojun #define B(bit) (1 << ((bit) & 0x07)) /* identify bit in byte */
155 1.1 itojun #define SET(bit) (A(bit) |= B(bit))
156 1.1 itojun #define CLR(bit) (A(bit) &= (~B(bit)))
157 1.1 itojun #define TST(bit) (A(bit) & B(bit))
158 1.1 itojun
159 1.1 itojun #define F_FLOOD 0x0001
160 1.1 itojun #define F_INTERVAL 0x0002
161 1.1 itojun #define F_NUMERIC 0x0004
162 1.1 itojun #define F_PINGFILLED 0x0008
163 1.1 itojun #define F_QUIET 0x0010
164 1.1 itojun #define F_RROUTE 0x0020
165 1.1 itojun #define F_SO_DEBUG 0x0040
166 1.1 itojun #define F_VERBOSE 0x0100
167 1.1 itojun #ifdef IPSEC
168 1.1 itojun #ifdef IPSEC_POLICY_IPSEC
169 1.9 itojun #define F_POLICY 0x0400
170 1.1 itojun #else
171 1.1 itojun #define F_AUTHHDR 0x0200
172 1.1 itojun #define F_ENCRYPT 0x0400
173 1.1 itojun #endif /*IPSEC_POLICY_IPSEC*/
174 1.1 itojun #endif /*IPSEC*/
175 1.1 itojun #define F_NODEADDR 0x0800
176 1.1 itojun #define F_FQDN 0x1000
177 1.1 itojun #define F_INTERFACE 0x2000
178 1.9 itojun #define F_SRCADDR 0x4000
179 1.1 itojun u_int options;
180 1.1 itojun
181 1.1 itojun #define IN6LEN sizeof(struct in6_addr)
182 1.1 itojun #define SA6LEN sizeof(struct sockaddr_in6)
183 1.1 itojun #define DUMMY_PORT 10101
184 1.1 itojun
185 1.1 itojun #define SIN6(s) ((struct sockaddr_in6 *)(s))
186 1.1 itojun
187 1.1 itojun /*
188 1.1 itojun * MAX_DUP_CHK is the number of bits in received table, i.e. the maximum
189 1.1 itojun * number of received sequence numbers we can keep track of. Change 128
190 1.1 itojun * to 8192 for complete accuracy...
191 1.1 itojun */
192 1.1 itojun #define MAX_DUP_CHK (8 * 8192)
193 1.1 itojun int mx_dup_ck = MAX_DUP_CHK;
194 1.1 itojun char rcvd_tbl[MAX_DUP_CHK / 8];
195 1.1 itojun
196 1.1 itojun struct addrinfo *res;
197 1.1 itojun struct sockaddr_in6 dst; /* who to ping6 */
198 1.1 itojun struct sockaddr_in6 src; /* src addr of this packet */
199 1.1 itojun int datalen = DEFDATALEN;
200 1.1 itojun int s; /* socket file descriptor */
201 1.1 itojun u_char outpack[MAXPACKETLEN];
202 1.1 itojun char BSPACE = '\b'; /* characters written for flood */
203 1.1 itojun char DOT = '.';
204 1.1 itojun char *hostname;
205 1.1 itojun int ident; /* process id to identify our packets */
206 1.9 itojun struct in6_addr srcaddr;
207 1.1 itojun
208 1.1 itojun /* counters */
209 1.1 itojun long npackets; /* max packets to transmit */
210 1.1 itojun long nreceived; /* # of packets we got back */
211 1.1 itojun long nrepeats; /* number of duplicates */
212 1.1 itojun long ntransmitted; /* sequence # for outbound packets = #sent */
213 1.1 itojun int interval = 1; /* interval between packets */
214 1.1 itojun int hoplimit = -1; /* hoplimit */
215 1.1 itojun
216 1.1 itojun /* timing */
217 1.1 itojun int timing; /* flag to do timing */
218 1.1 itojun double tmin = 999999999.0; /* minimum round trip time */
219 1.1 itojun double tmax = 0.0; /* maximum round trip time */
220 1.1 itojun double tsum = 0.0; /* sum of all times, for doing average */
221 1.1 itojun
222 1.1 itojun /* for inet_ntop() */
223 1.1 itojun char ntop_buf[INET6_ADDRSTRLEN];
224 1.1 itojun
225 1.1 itojun /* for node addresses */
226 1.1 itojun u_short naflags;
227 1.1 itojun
228 1.1 itojun /* for ancillary data(advanced API) */
229 1.1 itojun struct msghdr smsghdr;
230 1.1 itojun struct iovec smsgiov;
231 1.1 itojun char *scmsg = 0;
232 1.1 itojun
233 1.1 itojun int main __P((int, char *[]));
234 1.1 itojun void fill __P((char *, char *));
235 1.1 itojun int get_hoplim __P((struct msghdr *));
236 1.1 itojun void onalrm __P((int));
237 1.1 itojun void oninfo __P((int));
238 1.1 itojun void onint __P((int));
239 1.1 itojun void pinger __P((void));
240 1.9 itojun char *pr_addr __P((struct sockaddr_in6 *));
241 1.1 itojun void pr_icmph __P((struct icmp6_hdr *, u_char *));
242 1.1 itojun void pr_iph __P((struct ip6_hdr *));
243 1.1 itojun void pr_nodeaddr __P((struct icmp6_nodeinfo *, int));
244 1.1 itojun void pr_pack __P((u_char *, int, struct msghdr *));
245 1.1 itojun void pr_retip __P((struct ip6_hdr *, u_char *));
246 1.1 itojun void summary __P((void));
247 1.1 itojun void tvsub __P((struct timeval *, struct timeval *));
248 1.1 itojun void usage __P((void));
249 1.1 itojun
250 1.1 itojun int
251 1.1 itojun main(argc, argv)
252 1.1 itojun int argc;
253 1.1 itojun char *argv[];
254 1.1 itojun {
255 1.1 itojun extern int errno, optind;
256 1.1 itojun extern char *optarg;
257 1.1 itojun struct itimerval itimer;
258 1.1 itojun struct sockaddr_in6 from;
259 1.1 itojun struct timeval timeout;
260 1.1 itojun struct addrinfo hints;
261 1.1 itojun fd_set fdset;
262 1.1 itojun register int cc, i;
263 1.1 itojun int ch, fromlen, hold, packlen, preload, optval, ret_ga;
264 1.1 itojun u_char *datap, *packet;
265 1.1 itojun char *e, *target, *ifname = NULL;
266 1.1 itojun int ip6optlen = 0;
267 1.1 itojun struct cmsghdr *scmsgp = NULL;
268 1.1 itojun int sockbufsize = 0;
269 1.9 itojun int usepktinfo = 0;
270 1.9 itojun struct in6_pktinfo *pktinfo = NULL;
271 1.1 itojun #ifdef IPSEC_POLICY_IPSEC
272 1.1 itojun char *policy = NULL;
273 1.1 itojun #endif
274 1.1 itojun
275 1.1 itojun /* just to be sure */
276 1.1 itojun memset(&smsghdr, 0, sizeof(&smsghdr));
277 1.1 itojun memset(&smsgiov, 0, sizeof(&smsgiov));
278 1.1 itojun
279 1.1 itojun preload = 0;
280 1.1 itojun datap = &outpack[ICMP6ECHOLEN + ICMP6ECHOTMLEN];
281 1.1 itojun #ifndef IPSEC
282 1.9 itojun while ((ch = getopt(argc, argv, "a:b:c:dfh:I:i:l:np:qRrS:s:vwW")) != EOF)
283 1.1 itojun #else
284 1.1 itojun #ifdef IPSEC_POLICY_IPSEC
285 1.9 itojun while ((ch = getopt(argc, argv, "a:b:c:dfh:I:i:l:np:qRrS:s:vwWP:")) != EOF)
286 1.1 itojun #else
287 1.9 itojun while ((ch = getopt(argc, argv, "a:b:c:dfh:I:i:l:np:qRrS:s:vwWAE")) != EOF)
288 1.1 itojun #endif /*IPSEC_POLICY_IPSEC*/
289 1.1 itojun #endif
290 1.1 itojun switch(ch) {
291 1.1 itojun case 'a':
292 1.1 itojun {
293 1.9 itojun char *cp;
294 1.1 itojun
295 1.1 itojun options |= F_NODEADDR;
296 1.1 itojun datalen = 2048; /* XXX: enough? */
297 1.1 itojun for (cp = optarg; *cp != '\0'; cp++) {
298 1.1 itojun switch(*cp) {
299 1.9 itojun case 'a':
300 1.9 itojun naflags |= NI_NODEADDR_FLAG_ALL;
301 1.9 itojun break;
302 1.9 itojun case 'l':
303 1.9 itojun case 'L':
304 1.9 itojun naflags |= NI_NODEADDR_FLAG_LINKLOCAL;
305 1.9 itojun break;
306 1.9 itojun case 's':
307 1.9 itojun case 'S':
308 1.9 itojun naflags |= NI_NODEADDR_FLAG_SITELOCAL;
309 1.9 itojun break;
310 1.9 itojun case 'g':
311 1.9 itojun case 'G':
312 1.9 itojun naflags |= NI_NODEADDR_FLAG_GLOBAL;
313 1.9 itojun break;
314 1.9 itojun case 'A': /* experimental. not in the spec */
315 1.9 itojun naflags |= NI_NODEADDR_FLAG_ANYCAST;
316 1.9 itojun break;
317 1.9 itojun default:
318 1.9 itojun usage();
319 1.1 itojun }
320 1.1 itojun }
321 1.1 itojun break;
322 1.1 itojun }
323 1.1 itojun case 'b':
324 1.1 itojun #if defined(SO_SNDBUF) && defined(SO_RCVBUF)
325 1.1 itojun sockbufsize = atoi(optarg);
326 1.1 itojun #else
327 1.1 itojun err(1,
328 1.1 itojun "-b option ignored: SO_SNDBUF/SO_RCVBUF socket options not supported");
329 1.1 itojun #endif
330 1.1 itojun break;
331 1.1 itojun case 'c':
332 1.1 itojun npackets = strtol(optarg, &e, 10);
333 1.1 itojun if (npackets <= 0 || *optarg == '\0' || *e != '\0')
334 1.1 itojun errx(1,
335 1.1 itojun "illegal number of packets -- %s", optarg);
336 1.1 itojun break;
337 1.1 itojun case 'd':
338 1.1 itojun options |= F_SO_DEBUG;
339 1.1 itojun break;
340 1.1 itojun case 'f':
341 1.1 itojun if (getuid()) {
342 1.1 itojun errno = EPERM;
343 1.3 thorpej errx(1, "Must be superuser to flood ping");
344 1.1 itojun }
345 1.1 itojun options |= F_FLOOD;
346 1.1 itojun setbuf(stdout, (char *)NULL);
347 1.1 itojun break;
348 1.1 itojun case 'h': /* hoplimit */
349 1.1 itojun hoplimit = strtol(optarg, &e, 10);
350 1.1 itojun if (255 < hoplimit || hoplimit < -1)
351 1.1 itojun errx(1,
352 1.1 itojun "illegal hoplimit -- %s", optarg);
353 1.1 itojun break;
354 1.1 itojun case 'I':
355 1.1 itojun ifname = optarg;
356 1.1 itojun options |= F_INTERFACE;
357 1.9 itojun #ifndef USE_SIN6_SCOPE_ID
358 1.9 itojun usepktinfo++;
359 1.9 itojun #endif
360 1.1 itojun break;
361 1.1 itojun case 'i': /* wait between sending packets */
362 1.1 itojun interval = strtol(optarg, &e, 10);
363 1.1 itojun if (interval <= 0 || *optarg == '\0' || *e != '\0')
364 1.1 itojun errx(1,
365 1.1 itojun "illegal timing interval -- %s", optarg);
366 1.1 itojun options |= F_INTERVAL;
367 1.1 itojun break;
368 1.1 itojun case 'l':
369 1.1 itojun preload = strtol(optarg, &e, 10);
370 1.1 itojun if (preload < 0 || *optarg == '\0' || *e != '\0')
371 1.1 itojun errx(1, "illegal preload value -- %s", optarg);
372 1.1 itojun break;
373 1.1 itojun case 'n':
374 1.1 itojun options |= F_NUMERIC;
375 1.1 itojun break;
376 1.1 itojun case 'p': /* fill buffer with user pattern */
377 1.1 itojun options |= F_PINGFILLED;
378 1.1 itojun fill((char *)datap, optarg);
379 1.1 itojun break;
380 1.1 itojun case 'q':
381 1.1 itojun options |= F_QUIET;
382 1.1 itojun break;
383 1.1 itojun case 'R':
384 1.1 itojun options |= F_RROUTE;
385 1.1 itojun break;
386 1.9 itojun case 'S':
387 1.9 itojun /* XXX: use getaddrinfo? */
388 1.9 itojun if (inet_pton(AF_INET6, optarg, (void *)&srcaddr) != 1)
389 1.9 itojun errx(1, "invalid IPv6 address: %s", optarg);
390 1.9 itojun options |= F_SRCADDR;
391 1.9 itojun usepktinfo++;
392 1.9 itojun break;
393 1.1 itojun case 's': /* size of packet to send */
394 1.1 itojun datalen = strtol(optarg, &e, 10);
395 1.1 itojun if (datalen <= 0 || *optarg == '\0' || *e != '\0')
396 1.1 itojun errx(1, "illegal datalen value -- %s", optarg);
397 1.1 itojun if (datalen > MAXDATALEN)
398 1.1 itojun errx(1,
399 1.1 itojun "datalen value too large, maximum is %d",
400 1.1 itojun MAXDATALEN);
401 1.1 itojun break;
402 1.1 itojun case 'v':
403 1.1 itojun options |= F_VERBOSE;
404 1.1 itojun break;
405 1.1 itojun case 'w':
406 1.1 itojun case 'W':
407 1.1 itojun options |= F_FQDN;
408 1.1 itojun break;
409 1.1 itojun #ifdef IPSEC
410 1.1 itojun #ifdef IPSEC_POLICY_IPSEC
411 1.1 itojun case 'P':
412 1.1 itojun options |= F_POLICY;
413 1.1 itojun policy = strdup(optarg);
414 1.1 itojun break;
415 1.1 itojun #else
416 1.1 itojun case 'A':
417 1.1 itojun options |= F_AUTHHDR;
418 1.1 itojun break;
419 1.1 itojun case 'E':
420 1.1 itojun options |= F_ENCRYPT;
421 1.1 itojun break;
422 1.1 itojun #endif /*IPSEC_POLICY_IPSEC*/
423 1.1 itojun #endif /*IPSEC*/
424 1.1 itojun default:
425 1.1 itojun usage();
426 1.1 itojun }
427 1.1 itojun argc -= optind;
428 1.1 itojun argv += optind;
429 1.1 itojun
430 1.1 itojun if (argc < 1)
431 1.1 itojun usage();
432 1.1 itojun
433 1.1 itojun if (argc > 1)
434 1.1 itojun ip6optlen += inet6_rthdr_space(IPV6_RTHDR_TYPE_0, argc - 1);
435 1.1 itojun
436 1.1 itojun target = argv[argc - 1];
437 1.1 itojun
438 1.1 itojun /* getaddrinfo */
439 1.1 itojun bzero(&hints, sizeof(struct addrinfo));
440 1.1 itojun if ((options & F_NUMERIC) == 0)
441 1.1 itojun hints.ai_flags = AI_CANONNAME;
442 1.1 itojun hints.ai_family = AF_INET6;
443 1.1 itojun hints.ai_socktype = SOCK_RAW;
444 1.1 itojun hints.ai_protocol = IPPROTO_ICMPV6;
445 1.1 itojun
446 1.1 itojun ret_ga = getaddrinfo(target, NULL, &hints, &res);
447 1.1 itojun if (ret_ga) {
448 1.1 itojun fprintf(stderr, "ping6: %s\n", gai_strerror(ret_ga));
449 1.1 itojun exit(1);
450 1.1 itojun }
451 1.1 itojun if (res->ai_canonname)
452 1.1 itojun hostname = res->ai_canonname;
453 1.1 itojun else
454 1.1 itojun hostname = target;
455 1.1 itojun
456 1.1 itojun if (!res->ai_addr)
457 1.1 itojun errx(1, "getaddrinfo failed");
458 1.1 itojun
459 1.1 itojun (void)memcpy(&dst, res->ai_addr, res->ai_addrlen);
460 1.1 itojun
461 1.1 itojun if (options & F_FLOOD && options & F_INTERVAL)
462 1.1 itojun errx(1, "-f and -i incompatible options");
463 1.1 itojun
464 1.1 itojun if (datalen >= sizeof(struct timeval)) /* can we time transfer */
465 1.1 itojun timing = 1;
466 1.1 itojun packlen = datalen + IP6LEN + ICMP6ECHOLEN + EXTRA;
467 1.1 itojun if (!(packet = (u_char *)malloc((u_int)packlen)))
468 1.3 thorpej err(1, "Unable to allocate packet");
469 1.1 itojun if (!(options & F_PINGFILLED))
470 1.1 itojun for (i = 8; i < datalen; ++i)
471 1.1 itojun *datap++ = i;
472 1.1 itojun
473 1.1 itojun ident = getpid() & 0xFFFF;
474 1.1 itojun
475 1.9 itojun if ((s = socket(res->ai_family, res->ai_socktype,
476 1.9 itojun res->ai_protocol)) < 0)
477 1.9 itojun err(1, "socket");
478 1.1 itojun
479 1.1 itojun hold = 1;
480 1.1 itojun
481 1.1 itojun if (options & F_SO_DEBUG)
482 1.1 itojun (void)setsockopt(s, SOL_SOCKET, SO_DEBUG, (char *)&hold,
483 1.1 itojun sizeof(hold));
484 1.1 itojun optval = IPV6_DEFHLIM;
485 1.1 itojun if (IN6_IS_ADDR_MULTICAST(&dst.sin6_addr))
486 1.1 itojun if (setsockopt(s, IPPROTO_IPV6, IPV6_MULTICAST_HOPS,
487 1.1 itojun &optval, sizeof(optval)) == -1)
488 1.1 itojun err(1, "IPV6_MULTICAST_HOPS");
489 1.1 itojun
490 1.1 itojun #ifdef IPSEC
491 1.1 itojun #ifdef IPSEC_POLICY_IPSEC
492 1.1 itojun if (options & F_POLICY) {
493 1.1 itojun int len;
494 1.1 itojun char *buf;
495 1.1 itojun if ((len = ipsec_get_policylen(policy)) < 0)
496 1.1 itojun errx(1, ipsec_strerror());
497 1.1 itojun if ((buf = malloc(len)) == NULL)
498 1.1 itojun err(1, "malloc");
499 1.1 itojun if ((len = ipsec_set_policy(buf, len, policy)) < 0)
500 1.1 itojun errx(1, ipsec_strerror());
501 1.1 itojun if (setsockopt(s, IPPROTO_IPV6, IPV6_IPSEC_POLICY, buf, len) < 0)
502 1.5 itojun warnx("Unable to set IPSec policy");
503 1.1 itojun free(buf);
504 1.1 itojun }
505 1.1 itojun #else
506 1.1 itojun if (options & F_AUTHHDR) {
507 1.1 itojun optval = IPSEC_LEVEL_REQUIRE;
508 1.1 itojun #ifdef IPV6_AUTH_TRANS_LEVEL
509 1.1 itojun if (setsockopt(s, IPPROTO_IPV6, IPV6_AUTH_TRANS_LEVEL,
510 1.1 itojun &optval, sizeof(optval)) == -1)
511 1.1 itojun err(1, "setsockopt(IPV6_AUTH_TRANS_LEVEL)");
512 1.1 itojun #else /* old def */
513 1.1 itojun if (setsockopt(s, IPPROTO_IPV6, IPV6_AUTH_LEVEL,
514 1.1 itojun &optval, sizeof(optval)) == -1)
515 1.1 itojun err(1, "setsockopt(IPV6_AUTH_LEVEL)");
516 1.1 itojun #endif
517 1.1 itojun }
518 1.1 itojun if (options & F_ENCRYPT) {
519 1.1 itojun optval = IPSEC_LEVEL_REQUIRE;
520 1.1 itojun if (setsockopt(s, IPPROTO_IPV6, IPV6_ESP_TRANS_LEVEL,
521 1.1 itojun &optval, sizeof(optval)) == -1)
522 1.1 itojun err(1, "setsockopt(IPV6_ESP_TRANS_LEVEL)");
523 1.1 itojun }
524 1.1 itojun #endif /*IPSEC_POLICY_IPSEC*/
525 1.1 itojun #endif
526 1.1 itojun
527 1.1 itojun #ifdef ICMP6_FILTER
528 1.1 itojun {
529 1.1 itojun struct icmp6_filter filt;
530 1.1 itojun if (!(options & F_VERBOSE)) {
531 1.1 itojun ICMP6_FILTER_SETBLOCKALL(&filt);
532 1.1 itojun if ((options & F_FQDN) || (options & F_NODEADDR))
533 1.1 itojun ICMP6_FILTER_SETPASS(ICMP6_NI_REPLY, &filt);
534 1.1 itojun else
535 1.1 itojun ICMP6_FILTER_SETPASS(ICMP6_ECHO_REPLY, &filt);
536 1.1 itojun } else {
537 1.1 itojun ICMP6_FILTER_SETPASSALL(&filt);
538 1.1 itojun }
539 1.1 itojun if (setsockopt(s, IPPROTO_ICMPV6, ICMP6_FILTER, &filt,
540 1.1 itojun sizeof(filt)) < 0)
541 1.1 itojun err(1, "setsockopt(ICMP6_FILTER)");
542 1.1 itojun }
543 1.1 itojun #endif /*ICMP6_FILTER*/
544 1.1 itojun
545 1.1 itojun /*
546 1.1 itojun optval = 1;
547 1.1 itojun if (IN6_IS_ADDR_MULTICAST(&dst.sin6_addr))
548 1.1 itojun if (setsockopt(s, IPPROTO_IPV6, IPV6_MULTICAST_LOOP,
549 1.1 itojun &optval, sizeof(optval)) == -1)
550 1.1 itojun err(1, "IPV6_MULTICAST_LOOP");
551 1.1 itojun */
552 1.1 itojun /* record route option */
553 1.1 itojun if (options & F_RROUTE)
554 1.1 itojun errx(1, "record route not available in this implementation");
555 1.1 itojun
556 1.9 itojun /* Specify the outgoing interface and/or the source address */
557 1.9 itojun if (usepktinfo)
558 1.1 itojun ip6optlen += CMSG_SPACE(sizeof(struct in6_pktinfo));
559 1.1 itojun
560 1.1 itojun if (hoplimit != -1)
561 1.1 itojun ip6optlen += CMSG_SPACE(sizeof(int));
562 1.1 itojun
563 1.1 itojun /* set IP6 packet options */
564 1.1 itojun if (ip6optlen) {
565 1.1 itojun if ((scmsg = (char *)malloc(ip6optlen)) == 0)
566 1.1 itojun errx(1, "can't allocate enough memory");
567 1.1 itojun smsghdr.msg_control = (caddr_t)scmsg;
568 1.1 itojun smsghdr.msg_controllen = ip6optlen;
569 1.1 itojun scmsgp = (struct cmsghdr *)scmsg;
570 1.1 itojun }
571 1.9 itojun if (usepktinfo) {
572 1.9 itojun pktinfo = (struct in6_pktinfo *)(CMSG_DATA(scmsgp));
573 1.9 itojun memset(pktinfo, 0, sizeof(*pktinfo));
574 1.1 itojun scmsgp->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo));
575 1.1 itojun scmsgp->cmsg_level = IPPROTO_IPV6;
576 1.1 itojun scmsgp->cmsg_type = IPV6_PKTINFO;
577 1.9 itojun scmsgp = CMSG_NXTHDR(&smsghdr, scmsgp);
578 1.9 itojun }
579 1.1 itojun
580 1.9 itojun /* set the outgoing interface */
581 1.9 itojun if (ifname) {
582 1.9 itojun #ifndef USE_SIN6_SCOPE_ID
583 1.9 itojun /* pktinfo must have already been allocated */
584 1.9 itojun if ((pktinfo->ipi6_ifindex = if_nametoindex(ifname)) == 0)
585 1.9 itojun errx(1, "%s: invalid interface name", ifname);
586 1.1 itojun #else
587 1.9 itojun if ((dst.sin6_scope_id = if_nametoindex(ifname)) == 0)
588 1.1 itojun errx(1, "%s: invalid interface name", ifname);
589 1.1 itojun #endif
590 1.1 itojun }
591 1.9 itojun /* set the source address */
592 1.9 itojun if (options & F_SRCADDR)/* pktinfo must be valid */
593 1.9 itojun pktinfo->ipi6_addr = srcaddr;
594 1.9 itojun
595 1.1 itojun if (hoplimit != -1) {
596 1.1 itojun scmsgp->cmsg_len = CMSG_LEN(sizeof(int));
597 1.1 itojun scmsgp->cmsg_level = IPPROTO_IPV6;
598 1.1 itojun scmsgp->cmsg_type = IPV6_HOPLIMIT;
599 1.1 itojun *(int *)(CMSG_DATA(scmsgp)) = hoplimit;
600 1.1 itojun
601 1.1 itojun scmsgp = CMSG_NXTHDR(&smsghdr, scmsgp);
602 1.1 itojun }
603 1.1 itojun if (argc > 1) { /* some intermediate addrs are specified */
604 1.1 itojun int hops, error;
605 1.1 itojun
606 1.1 itojun if ((scmsgp = (struct cmsghdr *)inet6_rthdr_init(scmsgp,
607 1.1 itojun IPV6_RTHDR_TYPE_0)) == 0)
608 1.1 itojun errx(1, "can't initialize rthdr");
609 1.1 itojun
610 1.1 itojun for (hops = 0; hops < argc - 1; hops++) {
611 1.1 itojun struct addrinfo *iaip;
612 1.1 itojun
613 1.1 itojun if ((error = getaddrinfo(argv[hops], NULL, &hints, &iaip)))
614 1.1 itojun errx(1, gai_strerror(error));
615 1.1 itojun if (SIN6(res->ai_addr)->sin6_family != AF_INET6)
616 1.1 itojun errx(1,
617 1.1 itojun "bad addr family of an intermediate addr");
618 1.1 itojun
619 1.1 itojun if (inet6_rthdr_add(scmsgp,
620 1.1 itojun &(SIN6(iaip->ai_addr))->sin6_addr,
621 1.1 itojun IPV6_RTHDR_LOOSE))
622 1.1 itojun errx(1, "can't add an intermediate node");
623 1.1 itojun }
624 1.1 itojun
625 1.1 itojun if (inet6_rthdr_lasthop(scmsgp, IPV6_RTHDR_LOOSE))
626 1.1 itojun errx(1, "can't set the last flag");
627 1.1 itojun
628 1.1 itojun scmsgp = CMSG_NXTHDR(&smsghdr, scmsgp);
629 1.1 itojun }
630 1.1 itojun
631 1.1 itojun {
632 1.1 itojun /*
633 1.1 itojun * source selection
634 1.1 itojun */
635 1.1 itojun int dummy, len = sizeof(src);
636 1.1 itojun
637 1.1 itojun if ((dummy = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
638 1.1 itojun err(1, "UDP socket");
639 1.1 itojun
640 1.1 itojun src.sin6_family = AF_INET6;
641 1.1 itojun src.sin6_addr = dst.sin6_addr;
642 1.1 itojun src.sin6_port = ntohs(DUMMY_PORT);
643 1.9 itojun src.sin6_scope_id = dst.sin6_scope_id;
644 1.1 itojun
645 1.9 itojun #ifndef USE_SIN6_SCOPE_ID
646 1.1 itojun if (setsockopt(dummy, IPPROTO_IPV6, IPV6_PKTOPTIONS,
647 1.1 itojun (void *)smsghdr.msg_control,
648 1.1 itojun smsghdr.msg_controllen)) {
649 1.9 itojun err(1, "UDP setsockopt(IPV6_PKTOPTIONS)");
650 1.1 itojun }
651 1.1 itojun #else
652 1.9 itojun src.sin6_scope_id = dst.sin6_scope_id;
653 1.1 itojun #endif
654 1.1 itojun
655 1.1 itojun if (connect(dummy, (struct sockaddr *)&src, len) < 0)
656 1.1 itojun err(1, "UDP connect");
657 1.1 itojun
658 1.1 itojun if (getsockname(dummy, (struct sockaddr *)&src, &len) < 0)
659 1.1 itojun err(1, "getsockname");
660 1.1 itojun
661 1.1 itojun close(dummy);
662 1.1 itojun }
663 1.1 itojun
664 1.1 itojun #if defined(SO_SNDBUF) && defined(SO_RCVBUF)
665 1.1 itojun if (sockbufsize) {
666 1.1 itojun if (datalen > sockbufsize)
667 1.1 itojun warnx("you need -b to increae socket buffer size");
668 1.1 itojun if (setsockopt(s, SOL_SOCKET, SO_SNDBUF, &sockbufsize,
669 1.1 itojun sizeof(sockbufsize)) < 0)
670 1.1 itojun err(1, "setsockopt(SO_SNDBUF)");
671 1.1 itojun if (setsockopt(s, SOL_SOCKET, SO_RCVBUF, &sockbufsize,
672 1.1 itojun sizeof(sockbufsize)) < 0)
673 1.1 itojun err(1, "setsockopt(SO_RCVBUF)");
674 1.1 itojun }
675 1.1 itojun else {
676 1.1 itojun if (datalen > 8 * 1024) /*XXX*/
677 1.1 itojun warnx("you need -b to increase socket buffer size");
678 1.1 itojun /*
679 1.1 itojun * When pinging the broadcast address, you can get a lot of
680 1.1 itojun * answers. Doing something so evil is useful if you are trying
681 1.1 itojun * to stress the ethernet, or just want to fill the arp cache
682 1.1 itojun * to get some stuff for /etc/ethers.
683 1.1 itojun */
684 1.1 itojun hold = 48 * 1024;
685 1.1 itojun setsockopt(s, SOL_SOCKET, SO_RCVBUF, (char *)&hold, sizeof(hold));
686 1.1 itojun }
687 1.1 itojun #endif
688 1.1 itojun
689 1.1 itojun optval = 1;
690 1.9 itojun #ifndef USE_SIN6_SCOPE_ID
691 1.1 itojun setsockopt(s, IPPROTO_IPV6, IPV6_PKTINFO, &optval, sizeof(optval));
692 1.1 itojun #endif
693 1.1 itojun setsockopt(s, IPPROTO_IPV6, IPV6_HOPLIMIT, &optval, sizeof(optval));
694 1.1 itojun
695 1.1 itojun printf("PING6(%d=40+8+%d bytes) ", datalen + 48, datalen);
696 1.1 itojun printf("%s --> ", inet_ntop(AF_INET6, &src.sin6_addr, ntop_buf, sizeof(ntop_buf)));
697 1.1 itojun printf("%s\n", inet_ntop(AF_INET6, &dst.sin6_addr, ntop_buf, sizeof(ntop_buf)));
698 1.1 itojun
699 1.1 itojun while (preload--) /* Fire off them quickies. */
700 1.1 itojun pinger();
701 1.1 itojun
702 1.1 itojun (void)signal(SIGINT, onint);
703 1.1 itojun (void)signal(SIGINFO, oninfo);
704 1.1 itojun
705 1.1 itojun if ((options & F_FLOOD) == 0) {
706 1.1 itojun (void)signal(SIGALRM, onalrm);
707 1.1 itojun itimer.it_interval.tv_sec = interval;
708 1.1 itojun itimer.it_interval.tv_usec = 0;
709 1.1 itojun itimer.it_value.tv_sec = 0;
710 1.1 itojun itimer.it_value.tv_usec = 1;
711 1.1 itojun (void)setitimer(ITIMER_REAL, &itimer, NULL);
712 1.1 itojun }
713 1.1 itojun
714 1.1 itojun FD_ZERO(&fdset);
715 1.1 itojun timeout.tv_sec = 0;
716 1.1 itojun timeout.tv_usec = 10000;
717 1.1 itojun for (;;) {
718 1.1 itojun struct msghdr m;
719 1.1 itojun struct cmsghdr *cm;
720 1.1 itojun u_char buf[256];
721 1.1 itojun struct iovec iov[2];
722 1.1 itojun
723 1.1 itojun if (options & F_FLOOD) {
724 1.1 itojun pinger();
725 1.1 itojun FD_SET(s, &fdset);
726 1.1 itojun if (select(s + 1, &fdset, NULL, NULL, &timeout) < 1)
727 1.1 itojun continue;
728 1.1 itojun }
729 1.1 itojun fromlen = sizeof(from);
730 1.1 itojun
731 1.1 itojun m.msg_name = (caddr_t)&from;
732 1.1 itojun m.msg_namelen = sizeof(from);
733 1.1 itojun memset(&iov, 0, sizeof(iov));
734 1.1 itojun iov[0].iov_base = (caddr_t)packet;
735 1.1 itojun iov[0].iov_len = packlen;
736 1.1 itojun m.msg_iov = iov;
737 1.1 itojun m.msg_iovlen = 1;
738 1.1 itojun cm = (struct cmsghdr *)buf;
739 1.1 itojun m.msg_control = (caddr_t)buf;
740 1.1 itojun m.msg_controllen = sizeof(buf);
741 1.1 itojun
742 1.1 itojun if ((cc = recvmsg(s, &m, 0)) < 0) {
743 1.1 itojun if (errno == EINTR)
744 1.1 itojun continue;
745 1.1 itojun warn("recvfrom");
746 1.1 itojun continue;
747 1.1 itojun }
748 1.1 itojun
749 1.1 itojun pr_pack(packet, cc, &m);
750 1.1 itojun if (npackets && nreceived >= npackets)
751 1.1 itojun break;
752 1.1 itojun }
753 1.1 itojun summary();
754 1.1 itojun exit(nreceived == 0);
755 1.1 itojun }
756 1.1 itojun
757 1.1 itojun /*
758 1.1 itojun * onalrm --
759 1.1 itojun * This routine transmits another ping6.
760 1.1 itojun */
761 1.1 itojun void
762 1.1 itojun onalrm(signo)
763 1.1 itojun int signo;
764 1.1 itojun {
765 1.1 itojun struct itimerval itimer;
766 1.1 itojun
767 1.1 itojun if (!npackets || ntransmitted < npackets) {
768 1.1 itojun pinger();
769 1.1 itojun return;
770 1.1 itojun }
771 1.1 itojun
772 1.1 itojun /*
773 1.1 itojun * If we're not transmitting any more packets, change the timer
774 1.1 itojun * to wait two round-trip times if we've received any packets or
775 1.1 itojun * ten seconds if we haven't.
776 1.1 itojun */
777 1.1 itojun #define MAXWAIT 10
778 1.1 itojun if (nreceived) {
779 1.1 itojun itimer.it_value.tv_sec = 2 * tmax / 1000;
780 1.1 itojun if (itimer.it_value.tv_sec == 0)
781 1.1 itojun itimer.it_value.tv_sec = 1;
782 1.1 itojun } else
783 1.1 itojun itimer.it_value.tv_sec = MAXWAIT;
784 1.1 itojun itimer.it_interval.tv_sec = 0;
785 1.1 itojun itimer.it_interval.tv_usec = 0;
786 1.1 itojun itimer.it_value.tv_usec = 0;
787 1.1 itojun
788 1.1 itojun (void)signal(SIGALRM, onint);
789 1.1 itojun (void)setitimer(ITIMER_REAL, &itimer, NULL);
790 1.1 itojun }
791 1.1 itojun
792 1.1 itojun /*
793 1.1 itojun * pinger --
794 1.1 itojun * Compose and transmit an ICMP ECHO REQUEST packet. The IP packet
795 1.1 itojun * will be added on by the kernel. The ID field is our UNIX process ID,
796 1.1 itojun * and the sequence number is an ascending integer. The first 8 bytes
797 1.1 itojun * of the data portion are used to hold a UNIX "timeval" struct in VAX
798 1.1 itojun * byte-order, to compute the round-trip time.
799 1.1 itojun */
800 1.1 itojun void
801 1.1 itojun pinger()
802 1.1 itojun {
803 1.1 itojun struct icmp6_hdr *icp;
804 1.1 itojun struct iovec iov[2];
805 1.1 itojun int i, cc;
806 1.1 itojun
807 1.1 itojun icp = (struct icmp6_hdr *)outpack;
808 1.1 itojun icp->icmp6_code = 0;
809 1.1 itojun icp->icmp6_cksum = 0;
810 1.9 itojun icp->icmp6_seq = ntransmitted++; /* htons later */
811 1.9 itojun icp->icmp6_id = htons(ident); /* ID */
812 1.1 itojun
813 1.1 itojun CLR(icp->icmp6_seq % mx_dup_ck);
814 1.9 itojun icp->icmp6_seq = htons(icp->icmp6_seq);
815 1.1 itojun
816 1.1 itojun if (options & F_FQDN) {
817 1.1 itojun icp->icmp6_type = ICMP6_NI_QUERY;
818 1.1 itojun /* code field is always 0 */
819 1.1 itojun /* XXX: overwrite icmp6_id */
820 1.1 itojun icp->icmp6_data16[0] = htons(NI_QTYPE_FQDN);
821 1.1 itojun if (timing)
822 1.1 itojun (void)gettimeofday((struct timeval *)
823 1.1 itojun &outpack[ICMP6ECHOLEN], NULL);
824 1.1 itojun cc = ICMP6_NIQLEN;
825 1.1 itojun datalen = 0;
826 1.1 itojun } else if (options & F_NODEADDR) {
827 1.1 itojun icp->icmp6_type = ICMP6_NI_QUERY;
828 1.1 itojun /* code field is always 0 */
829 1.1 itojun /* XXX: overwrite icmp6_id */
830 1.1 itojun icp->icmp6_data16[0] = htons(NI_QTYPE_NODEADDR);
831 1.1 itojun if (timing)
832 1.1 itojun (void)gettimeofday((struct timeval *)
833 1.1 itojun &outpack[ICMP6ECHOLEN], NULL);
834 1.1 itojun cc = ICMP6_NIQLEN;
835 1.1 itojun datalen = 0;
836 1.1 itojun ((struct icmp6_nodeinfo *)icp)->ni_flags = naflags;
837 1.1 itojun }
838 1.1 itojun else {
839 1.1 itojun icp->icmp6_type = ICMP6_ECHO_REQUEST;
840 1.1 itojun if (timing)
841 1.1 itojun (void)gettimeofday((struct timeval *)
842 1.1 itojun &outpack[ICMP6ECHOLEN], NULL);
843 1.1 itojun cc = ICMP6ECHOLEN + datalen;
844 1.1 itojun }
845 1.1 itojun
846 1.1 itojun smsghdr.msg_name = (caddr_t)&dst;
847 1.1 itojun smsghdr.msg_namelen = sizeof(dst);
848 1.1 itojun memset(&iov, 0, sizeof(iov));
849 1.1 itojun iov[0].iov_base = (caddr_t)outpack;
850 1.1 itojun iov[0].iov_len = cc;
851 1.1 itojun smsghdr.msg_iov = iov;
852 1.1 itojun smsghdr.msg_iovlen = 1;
853 1.1 itojun
854 1.1 itojun i = sendmsg(s, &smsghdr, 0);
855 1.1 itojun
856 1.1 itojun if (i < 0 || i != cc) {
857 1.1 itojun if (i < 0)
858 1.1 itojun warn("sendmsg");
859 1.1 itojun (void)printf("ping6: wrote %s %d chars, ret=%d\n",
860 1.1 itojun hostname, cc, i);
861 1.1 itojun }
862 1.1 itojun if (!(options & F_QUIET) && options & F_FLOOD)
863 1.1 itojun (void)write(STDOUT_FILENO, &DOT, 1);
864 1.1 itojun }
865 1.1 itojun
866 1.1 itojun /*
867 1.1 itojun * pr_pack --
868 1.1 itojun * Print out the packet, if it came from us. This logic is necessary
869 1.1 itojun * because ALL readers of the ICMP socket get a copy of ALL ICMP packets
870 1.1 itojun * which arrive ('tis only fair). This permits multiple copies of this
871 1.1 itojun * program to be run without having intermingled output (or statistics!).
872 1.1 itojun */
873 1.1 itojun void
874 1.1 itojun pr_pack(buf, cc, mhdr)
875 1.1 itojun u_char *buf;
876 1.1 itojun int cc;
877 1.1 itojun struct msghdr *mhdr;
878 1.1 itojun {
879 1.1 itojun struct icmp6_hdr *icp;
880 1.1 itojun int i;
881 1.1 itojun int hoplim;
882 1.1 itojun struct sockaddr_in6 *from = (struct sockaddr_in6 *)mhdr->msg_name;
883 1.1 itojun u_char *cp = NULL, *dp, *end = buf + cc;
884 1.1 itojun #ifdef OLD_RAWSOCKET
885 1.1 itojun struct ip6_hdr *ip;
886 1.1 itojun #endif
887 1.1 itojun struct timeval tv, *tp;
888 1.1 itojun double triptime = 0;
889 1.1 itojun int dupflag;
890 1.1 itojun size_t off;
891 1.1 itojun
892 1.1 itojun (void)gettimeofday(&tv, NULL);
893 1.1 itojun
894 1.1 itojun #ifdef OLD_RAWSOCKET
895 1.1 itojun /* Check the IP header */
896 1.1 itojun ip = (struct ip6_hdr *)buf;
897 1.1 itojun if (cc < sizeof(struct icmp6_hdr) + sizeof(struct ip6_hdr)) {
898 1.1 itojun if (options & F_VERBOSE)
899 1.1 itojun warnx("packet too short (%d bytes) from %s\n", cc,
900 1.1 itojun inet_ntop(AF_INET6, (void *)&from->sin6_addr,
901 1.1 itojun ntop_buf, sizeof(ntop_buf)));
902 1.1 itojun return;
903 1.1 itojun }
904 1.1 itojun
905 1.1 itojun /* chase nexthdr link */
906 1.1 itojun {
907 1.1 itojun u_int8_t nh;
908 1.1 itojun struct ah *ah;
909 1.1 itojun struct ip6_ext *ip6e;
910 1.1 itojun
911 1.1 itojun off = IP6LEN;
912 1.1 itojun nh = ip->ip6_nxt;
913 1.1 itojun while (nh != IPPROTO_ICMPV6) {
914 1.1 itojun if (options & F_VERBOSE)
915 1.1 itojun fprintf(stderr, "header chain: type=0x%x\n", nh);
916 1.1 itojun
917 1.1 itojun switch (nh) {
918 1.9 itojun #ifdef IPSEC
919 1.1 itojun case IPPROTO_AH:
920 1.1 itojun ah = (struct ah *)(buf + off);
921 1.1 itojun off += sizeof(struct ah);
922 1.1 itojun off += (ah->ah_len << 2);
923 1.1 itojun nh = ah->ah_nxt;
924 1.1 itojun break;
925 1.9 itojun #endif
926 1.1 itojun
927 1.1 itojun case IPPROTO_HOPOPTS:
928 1.1 itojun ip6e = (struct ip6_ext *)(buf + off);
929 1.1 itojun off += (ip6e->ip6e_len + 1) << 3;
930 1.1 itojun nh = ip6e->ip6e_nxt;
931 1.1 itojun break;
932 1.1 itojun default:
933 1.1 itojun if (options & F_VERBOSE) {
934 1.1 itojun fprintf(stderr,
935 1.1 itojun "unknown header type=0x%x: drop it\n",
936 1.1 itojun nh);
937 1.1 itojun }
938 1.1 itojun return;
939 1.1 itojun }
940 1.1 itojun }
941 1.1 itojun }
942 1.1 itojun /* Now the ICMP part */
943 1.1 itojun icp = (struct icmp6_hdr *)(buf + off);
944 1.1 itojun #else
945 1.1 itojun if (cc < sizeof(struct icmp6_hdr)) {
946 1.1 itojun if (options & F_VERBOSE)
947 1.1 itojun warnx("packet too short (%d bytes) from %s\n", cc,
948 1.1 itojun inet_ntop(AF_INET6, (void *)&from->sin6_addr,
949 1.1 itojun ntop_buf, sizeof(ntop_buf)));
950 1.1 itojun return;
951 1.1 itojun }
952 1.1 itojun icp = (struct icmp6_hdr *)buf;
953 1.1 itojun off = 0;
954 1.1 itojun #endif
955 1.1 itojun
956 1.1 itojun if ((hoplim = get_hoplim(mhdr)) == -1) {
957 1.1 itojun warnx("failed to get receiving hop limit");
958 1.1 itojun return;
959 1.1 itojun }
960 1.1 itojun
961 1.1 itojun if (icp->icmp6_type == ICMP6_ECHO_REPLY) {
962 1.9 itojun /* XXX the following line overwrites the original packet */
963 1.9 itojun icp->icmp6_seq = ntohs(icp->icmp6_seq);
964 1.9 itojun if (ntohs(icp->icmp6_id) != ident)
965 1.1 itojun return; /* It was not our ECHO */
966 1.1 itojun ++nreceived;
967 1.1 itojun if (timing) {
968 1.1 itojun tp = (struct timeval *)(icp + 1);
969 1.1 itojun tvsub(&tv, tp);
970 1.1 itojun triptime = ((double)tv.tv_sec) * 1000.0 +
971 1.1 itojun ((double)tv.tv_usec) / 1000.0;
972 1.1 itojun tsum += triptime;
973 1.1 itojun if (triptime < tmin)
974 1.1 itojun tmin = triptime;
975 1.1 itojun if (triptime > tmax)
976 1.1 itojun tmax = triptime;
977 1.1 itojun }
978 1.1 itojun
979 1.1 itojun if (TST(icp->icmp6_seq % mx_dup_ck)) {
980 1.1 itojun ++nrepeats;
981 1.1 itojun --nreceived;
982 1.1 itojun dupflag = 1;
983 1.1 itojun } else {
984 1.1 itojun SET(icp->icmp6_seq % mx_dup_ck);
985 1.1 itojun dupflag = 0;
986 1.1 itojun }
987 1.1 itojun
988 1.1 itojun if (options & F_QUIET)
989 1.1 itojun return;
990 1.1 itojun
991 1.1 itojun if (options & F_FLOOD)
992 1.1 itojun (void)write(STDOUT_FILENO, &BSPACE, 1);
993 1.1 itojun else {
994 1.1 itojun (void)printf("%d bytes from %s, icmp_seq=%u", cc,
995 1.9 itojun pr_addr(from),
996 1.1 itojun icp->icmp6_seq);
997 1.1 itojun (void)printf(" hlim=%d", hoplim);
998 1.1 itojun if (timing)
999 1.1 itojun (void)printf(" time=%g ms", triptime);
1000 1.1 itojun if (dupflag)
1001 1.1 itojun (void)printf("(DUP!)");
1002 1.1 itojun /* check the data */
1003 1.1 itojun cp = buf + off + ICMP6ECHOLEN + ICMP6ECHOTMLEN;
1004 1.1 itojun dp = outpack + ICMP6ECHOLEN + ICMP6ECHOTMLEN;
1005 1.1 itojun for (i = 8; cp < end; ++i, ++cp, ++dp) {
1006 1.1 itojun if (*cp != *dp) {
1007 1.1 itojun (void)printf("\nwrong data byte #%d should be 0x%x but was 0x%x", i, *dp, *cp);
1008 1.1 itojun break;
1009 1.1 itojun }
1010 1.1 itojun }
1011 1.1 itojun }
1012 1.1 itojun } else if (icp->icmp6_type == ICMP6_NI_REPLY) { /* ICMP6_NI_REPLY */
1013 1.1 itojun struct icmp6_nodeinfo *ni = (struct icmp6_nodeinfo *)(buf + off);
1014 1.1 itojun
1015 1.1 itojun (void)printf("%d bytes from %s: ", cc,
1016 1.9 itojun pr_addr(from));
1017 1.1 itojun
1018 1.1 itojun switch(ntohs(ni->ni_qtype)) {
1019 1.1 itojun case NI_QTYPE_NOOP:
1020 1.1 itojun printf("NodeInfo NOOP");
1021 1.1 itojun break;
1022 1.1 itojun case NI_QTYPE_SUPTYPES:
1023 1.1 itojun printf("NodeInfo Supported Qtypes");
1024 1.1 itojun break;
1025 1.1 itojun case NI_QTYPE_NODEADDR:
1026 1.1 itojun pr_nodeaddr(ni, end - (u_char *)ni);
1027 1.1 itojun break;
1028 1.1 itojun case NI_QTYPE_FQDN:
1029 1.1 itojun default: /* XXX: for backward compatibility */
1030 1.1 itojun cp = (u_char *)ni + ICMP6_NIRLEN + 1;
1031 1.1 itojun while (cp < end) {
1032 1.1 itojun if (isprint(*cp))
1033 1.1 itojun putchar(*cp);
1034 1.1 itojun else
1035 1.1 itojun printf("\\%03o", *cp & 0xff);
1036 1.1 itojun cp++;
1037 1.1 itojun }
1038 1.1 itojun if (options & F_VERBOSE) {
1039 1.1 itojun long ttl;
1040 1.1 itojun
1041 1.1 itojun (void)printf(" (");
1042 1.1 itojun
1043 1.1 itojun switch(ni->ni_code) {
1044 1.1 itojun case ICMP6_NI_REFUSED:
1045 1.1 itojun (void)printf("refused,");
1046 1.1 itojun break;
1047 1.1 itojun case ICMP6_NI_UNKNOWN:
1048 1.1 itojun (void)printf("unknwon qtype,");
1049 1.1 itojun break;
1050 1.1 itojun }
1051 1.1 itojun
1052 1.1 itojun if ((end - (u_char *)ni) < ICMP6_NIRLEN) {
1053 1.1 itojun /* case of refusion, unkown */
1054 1.1 itojun goto fqdnend;
1055 1.1 itojun }
1056 1.1 itojun ttl = ntohl(*(u_long *)&buf[off+ICMP6ECHOLEN+8]);
1057 1.1 itojun if (!(ni->ni_flags & NI_FQDN_FLAG_VALIDTTL))
1058 1.1 itojun (void)printf("TTL=%d:meaningless",
1059 1.1 itojun (int)ttl);
1060 1.1 itojun else {
1061 1.1 itojun if (ttl < 0)
1062 1.1 itojun (void)printf("TTL=%d:invalid",
1063 1.1 itojun (int)ttl);
1064 1.1 itojun else
1065 1.1 itojun (void)printf("TTL=%d",
1066 1.1 itojun (int)ttl);
1067 1.1 itojun }
1068 1.1 itojun
1069 1.1 itojun if (buf[off + ICMP6_NIRLEN] !=
1070 1.1 itojun cc - off - ICMP6_NIRLEN - 1) {
1071 1.2 thorpej (void)printf(",invalid namelen:%d/%lu",
1072 1.1 itojun buf[off + ICMP6_NIRLEN],
1073 1.2 thorpej (u_long)cc - off - ICMP6_NIRLEN - 1);
1074 1.1 itojun }
1075 1.1 itojun putchar(')');
1076 1.1 itojun }
1077 1.1 itojun fqdnend:
1078 1.1 itojun ;
1079 1.1 itojun }
1080 1.1 itojun } else {
1081 1.1 itojun /* We've got something other than an ECHOREPLY */
1082 1.1 itojun if (!(options & F_VERBOSE))
1083 1.1 itojun return;
1084 1.1 itojun (void)printf("%d bytes from %s: ", cc,
1085 1.9 itojun pr_addr(from));
1086 1.1 itojun pr_icmph(icp, end);
1087 1.1 itojun }
1088 1.1 itojun
1089 1.1 itojun if (!(options & F_FLOOD)) {
1090 1.1 itojun (void)putchar('\n');
1091 1.1 itojun (void)fflush(stdout);
1092 1.1 itojun }
1093 1.1 itojun }
1094 1.1 itojun
1095 1.1 itojun void
1096 1.1 itojun pr_nodeaddr(ni, nilen)
1097 1.1 itojun struct icmp6_nodeinfo *ni; /* ni->qtype must be NODEADDR */
1098 1.1 itojun int nilen;
1099 1.1 itojun {
1100 1.1 itojun struct in6_addr *ia6 = (struct in6_addr *)(ni + 1);
1101 1.1 itojun
1102 1.1 itojun nilen -= sizeof(struct icmp6_nodeinfo);
1103 1.1 itojun
1104 1.1 itojun if (options & F_VERBOSE) {
1105 1.1 itojun switch(ni->ni_code) {
1106 1.1 itojun case ICMP6_NI_REFUSED:
1107 1.1 itojun (void)printf("refused");
1108 1.1 itojun break;
1109 1.1 itojun case ICMP6_NI_UNKNOWN:
1110 1.1 itojun (void)printf("unknown qtype");
1111 1.1 itojun break;
1112 1.1 itojun }
1113 1.1 itojun if (ni->ni_flags & NI_NODEADDR_FLAG_ALL)
1114 1.1 itojun (void)printf(" truncated");
1115 1.1 itojun }
1116 1.1 itojun putchar('\n');
1117 1.1 itojun if (nilen <= 0)
1118 1.1 itojun printf(" no address\n");
1119 1.1 itojun for (; nilen > 0; nilen -= sizeof(*ia6), ia6 += 1) {
1120 1.1 itojun printf(" %s\n",
1121 1.1 itojun inet_ntop(AF_INET6, ia6, ntop_buf, sizeof(ntop_buf)));
1122 1.1 itojun }
1123 1.1 itojun }
1124 1.1 itojun
1125 1.1 itojun int
1126 1.1 itojun get_hoplim(mhdr)
1127 1.1 itojun struct msghdr *mhdr;
1128 1.1 itojun {
1129 1.1 itojun struct cmsghdr *cm;
1130 1.1 itojun
1131 1.1 itojun for (cm = (struct cmsghdr *)CMSG_FIRSTHDR(mhdr); cm;
1132 1.1 itojun cm = (struct cmsghdr *)CMSG_NXTHDR(mhdr, cm)) {
1133 1.1 itojun if (cm->cmsg_level == IPPROTO_IPV6 &&
1134 1.1 itojun cm->cmsg_type == IPV6_HOPLIMIT &&
1135 1.1 itojun cm->cmsg_len == CMSG_LEN(sizeof(int)))
1136 1.1 itojun return(*(int *)CMSG_DATA(cm));
1137 1.1 itojun }
1138 1.1 itojun
1139 1.1 itojun return(-1);
1140 1.1 itojun }
1141 1.1 itojun
1142 1.1 itojun /*
1143 1.1 itojun * tvsub --
1144 1.1 itojun * Subtract 2 timeval structs: out = out - in. Out is assumed to
1145 1.1 itojun * be >= in.
1146 1.1 itojun */
1147 1.1 itojun void
1148 1.1 itojun tvsub(out, in)
1149 1.1 itojun register struct timeval *out, *in;
1150 1.1 itojun {
1151 1.1 itojun if ((out->tv_usec -= in->tv_usec) < 0) {
1152 1.1 itojun --out->tv_sec;
1153 1.1 itojun out->tv_usec += 1000000;
1154 1.1 itojun }
1155 1.1 itojun out->tv_sec -= in->tv_sec;
1156 1.1 itojun }
1157 1.1 itojun
1158 1.1 itojun /*
1159 1.1 itojun * oninfo --
1160 1.1 itojun * SIGINFO handler.
1161 1.1 itojun */
1162 1.1 itojun void
1163 1.1 itojun oninfo(notused)
1164 1.1 itojun int notused;
1165 1.1 itojun {
1166 1.1 itojun summary();
1167 1.1 itojun }
1168 1.1 itojun
1169 1.1 itojun /*
1170 1.1 itojun * onint --
1171 1.1 itojun * SIGINT handler.
1172 1.1 itojun */
1173 1.1 itojun void
1174 1.1 itojun onint(notused)
1175 1.1 itojun int notused;
1176 1.1 itojun {
1177 1.1 itojun summary();
1178 1.1 itojun
1179 1.1 itojun (void)signal(SIGINT, SIG_DFL);
1180 1.1 itojun (void)kill(getpid(), SIGINT);
1181 1.1 itojun
1182 1.1 itojun /* NOTREACHED */
1183 1.1 itojun exit(1);
1184 1.1 itojun }
1185 1.1 itojun
1186 1.1 itojun /*
1187 1.1 itojun * summary --
1188 1.1 itojun * Print out statistics.
1189 1.1 itojun */
1190 1.1 itojun void
1191 1.1 itojun summary()
1192 1.1 itojun {
1193 1.1 itojun register int i;
1194 1.1 itojun
1195 1.1 itojun (void)printf("\n--- %s ping6 statistics ---\n", hostname);
1196 1.1 itojun (void)printf("%ld packets transmitted, ", ntransmitted);
1197 1.1 itojun (void)printf("%ld packets received, ", nreceived);
1198 1.1 itojun if (nrepeats)
1199 1.1 itojun (void)printf("+%ld duplicates, ", nrepeats);
1200 1.1 itojun if (ntransmitted) {
1201 1.1 itojun if (nreceived > ntransmitted)
1202 1.1 itojun (void)printf("-- somebody's printing up packets!");
1203 1.1 itojun else
1204 1.1 itojun (void)printf("%d%% packet loss",
1205 1.1 itojun (int) (((ntransmitted - nreceived) * 100) /
1206 1.1 itojun ntransmitted));
1207 1.1 itojun }
1208 1.1 itojun (void)putchar('\n');
1209 1.1 itojun if (nreceived && timing) {
1210 1.1 itojun /* Only display average to microseconds */
1211 1.1 itojun i = 1000.0 * tsum / (nreceived + nrepeats);
1212 1.1 itojun (void)printf("round-trip min/avg/max = %g/%g/%g ms\n",
1213 1.1 itojun tmin, ((double)i) / 1000.0, tmax);
1214 1.1 itojun (void)fflush(stdout);
1215 1.1 itojun }
1216 1.1 itojun }
1217 1.1 itojun
1218 1.1 itojun #ifdef notdef
1219 1.1 itojun static char *ttab[] = {
1220 1.1 itojun "Echo Reply", /* ip + seq + udata */
1221 1.1 itojun "Dest Unreachable", /* net, host, proto, port, frag, sr + IP */
1222 1.1 itojun "Source Quench", /* IP */
1223 1.1 itojun "Redirect", /* redirect type, gateway, + IP */
1224 1.1 itojun "Echo",
1225 1.1 itojun "Time Exceeded", /* transit, frag reassem + IP */
1226 1.1 itojun "Parameter Problem", /* pointer + IP */
1227 1.1 itojun "Timestamp", /* id + seq + three timestamps */
1228 1.1 itojun "Timestamp Reply", /* " */
1229 1.1 itojun "Info Request", /* id + sq */
1230 1.1 itojun "Info Reply" /* " */
1231 1.1 itojun };
1232 1.1 itojun #endif
1233 1.1 itojun
1234 1.1 itojun /*
1235 1.1 itojun * pr_icmph --
1236 1.1 itojun * Print a descriptive string about an ICMP header.
1237 1.1 itojun */
1238 1.1 itojun void
1239 1.1 itojun pr_icmph(icp, end)
1240 1.1 itojun struct icmp6_hdr *icp;
1241 1.1 itojun u_char *end;
1242 1.1 itojun {
1243 1.1 itojun switch(icp->icmp6_type) {
1244 1.1 itojun case ICMP6_DST_UNREACH:
1245 1.1 itojun switch(icp->icmp6_code) {
1246 1.1 itojun case ICMP6_DST_UNREACH_NOROUTE:
1247 1.1 itojun (void)printf("No Route to Destination\n");
1248 1.1 itojun break;
1249 1.1 itojun case ICMP6_DST_UNREACH_ADMIN:
1250 1.1 itojun (void)printf("Destination Administratively "
1251 1.1 itojun "Unreachable\n");
1252 1.1 itojun break;
1253 1.9 itojun case ICMP6_DST_UNREACH_BEYONDSCOPE:
1254 1.9 itojun (void)printf("Destination Unreachable Beyond Scope\n");
1255 1.1 itojun break;
1256 1.1 itojun case ICMP6_DST_UNREACH_ADDR:
1257 1.1 itojun (void)printf("Destination Host Unreachable\n");
1258 1.1 itojun break;
1259 1.1 itojun case ICMP6_DST_UNREACH_NOPORT:
1260 1.1 itojun (void)printf("Destination Port Unreachable\n");
1261 1.1 itojun break;
1262 1.1 itojun default:
1263 1.1 itojun (void)printf("Destination Unreachable, Bad Code: %d\n",
1264 1.1 itojun icp->icmp6_code);
1265 1.1 itojun break;
1266 1.1 itojun }
1267 1.1 itojun /* Print returned IP header information */
1268 1.1 itojun pr_retip((struct ip6_hdr *)(icp + 1), end);
1269 1.1 itojun break;
1270 1.1 itojun case ICMP6_PACKET_TOO_BIG:
1271 1.1 itojun (void)printf("Packet too big mtu = %d\n",
1272 1.1 itojun (int)ntohl(icp->icmp6_mtu));
1273 1.1 itojun break;
1274 1.1 itojun case ICMP6_TIME_EXCEEDED:
1275 1.1 itojun switch(icp->icmp6_code) {
1276 1.1 itojun case ICMP6_TIME_EXCEED_TRANSIT:
1277 1.1 itojun (void)printf("Time to live exceeded\n");
1278 1.1 itojun break;
1279 1.1 itojun case ICMP6_TIME_EXCEED_REASSEMBLY:
1280 1.1 itojun (void)printf("Frag reassembly time exceeded\n");
1281 1.1 itojun break;
1282 1.1 itojun default:
1283 1.1 itojun (void)printf("Time exceeded, Bad Code: %d\n",
1284 1.1 itojun icp->icmp6_code);
1285 1.1 itojun break;
1286 1.1 itojun }
1287 1.1 itojun pr_retip((struct ip6_hdr *)(icp + 1), end);
1288 1.1 itojun break;
1289 1.1 itojun case ICMP6_PARAM_PROB:
1290 1.1 itojun (void)printf("Parameter problem: ");
1291 1.1 itojun switch(icp->icmp6_code) {
1292 1.1 itojun case ICMP6_PARAMPROB_HEADER:
1293 1.1 itojun (void)printf("Erroneous Header ");
1294 1.1 itojun break;
1295 1.1 itojun case ICMP6_PARAMPROB_NEXTHEADER:
1296 1.1 itojun (void)printf("Unknown Nextheader ");
1297 1.1 itojun break;
1298 1.1 itojun case ICMP6_PARAMPROB_OPTION:
1299 1.1 itojun (void)printf("Unrecognized Option ");
1300 1.1 itojun break;
1301 1.1 itojun default:
1302 1.1 itojun (void)printf("Bad code(%d) ", icp->icmp6_code);
1303 1.1 itojun break;
1304 1.1 itojun }
1305 1.1 itojun (void)printf("pointer = 0x%02x\n",
1306 1.1 itojun (int)ntohl(icp->icmp6_pptr));
1307 1.1 itojun pr_retip((struct ip6_hdr *)(icp + 1), end);
1308 1.1 itojun break;
1309 1.1 itojun case ICMP6_ECHO_REQUEST:
1310 1.1 itojun (void)printf("Echo Request\n");
1311 1.1 itojun /* XXX ID + Seq + Data */
1312 1.1 itojun break;
1313 1.1 itojun case ICMP6_ECHO_REPLY:
1314 1.1 itojun (void)printf("Echo Reply\n");
1315 1.1 itojun /* XXX ID + Seq + Data */
1316 1.1 itojun break;
1317 1.1 itojun case ICMP6_MEMBERSHIP_QUERY:
1318 1.1 itojun (void)printf("Membership Query\n");
1319 1.1 itojun break;
1320 1.1 itojun case ICMP6_MEMBERSHIP_REPORT:
1321 1.1 itojun (void)printf("Membership Report\n");
1322 1.1 itojun break;
1323 1.1 itojun case ICMP6_MEMBERSHIP_REDUCTION:
1324 1.1 itojun (void)printf("Membership Reduction\n");
1325 1.1 itojun break;
1326 1.1 itojun case ND_ROUTER_SOLICIT:
1327 1.1 itojun (void)printf("Router Solicitation\n");
1328 1.1 itojun break;
1329 1.1 itojun case ND_ROUTER_ADVERT:
1330 1.1 itojun (void)printf("Router Advertisement\n");
1331 1.1 itojun break;
1332 1.1 itojun case ND_NEIGHBOR_SOLICIT:
1333 1.1 itojun (void)printf("Neighbor Solicitation\n");
1334 1.1 itojun break;
1335 1.1 itojun case ND_NEIGHBOR_ADVERT:
1336 1.1 itojun (void)printf("Neighbor Advertisement\n");
1337 1.1 itojun break;
1338 1.1 itojun case ND_REDIRECT:
1339 1.1 itojun {
1340 1.1 itojun struct nd_redirect *red = (struct nd_redirect *)icp;
1341 1.1 itojun
1342 1.1 itojun (void)printf("Redirect\n");
1343 1.1 itojun (void)printf("Destination: %s\n",
1344 1.1 itojun inet_ntop(AF_INET6, &red->nd_rd_dst,
1345 1.1 itojun ntop_buf, sizeof(ntop_buf)));
1346 1.1 itojun (void)printf("New Target: %s\n",
1347 1.1 itojun inet_ntop(AF_INET6, &red->nd_rd_target,
1348 1.1 itojun ntop_buf, sizeof(ntop_buf)));
1349 1.1 itojun break;
1350 1.1 itojun }
1351 1.1 itojun case ICMP6_NI_QUERY:
1352 1.1 itojun (void)printf("Node Information Query\n");
1353 1.1 itojun /* XXX ID + Seq + Data */
1354 1.1 itojun break;
1355 1.1 itojun case ICMP6_NI_REPLY:
1356 1.1 itojun (void)printf("Node Information Reply\n");
1357 1.1 itojun /* XXX ID + Seq + Data */
1358 1.1 itojun break;
1359 1.1 itojun default:
1360 1.1 itojun (void)printf("Bad ICMP type: %d\n", icp->icmp6_type);
1361 1.1 itojun }
1362 1.1 itojun }
1363 1.1 itojun
1364 1.1 itojun /*
1365 1.1 itojun * pr_iph --
1366 1.1 itojun * Print an IP6 header.
1367 1.1 itojun */
1368 1.1 itojun void
1369 1.1 itojun pr_iph(ip6)
1370 1.1 itojun struct ip6_hdr *ip6;
1371 1.1 itojun {
1372 1.1 itojun u_int32_t flow = ip6->ip6_flow & IPV6_FLOWLABEL_MASK;
1373 1.1 itojun u_int8_t tc;
1374 1.1 itojun
1375 1.1 itojun tc = *(&ip6->ip6_vfc + 1); /* XXX */
1376 1.1 itojun tc = (tc >> 4) & 0x0f;
1377 1.1 itojun tc |= (ip6->ip6_vfc << 4);
1378 1.1 itojun
1379 1.1 itojun printf("Vr TC Flow Plen Nxt Hlim\n");
1380 1.1 itojun printf(" %1x %02x %05x %04x %02x %02x\n",
1381 1.1 itojun (ip6->ip6_vfc & IPV6_VERSION_MASK) >> 4, tc, (int)ntohl(flow),
1382 1.1 itojun ntohs(ip6->ip6_plen),
1383 1.1 itojun ip6->ip6_nxt, ip6->ip6_hlim);
1384 1.1 itojun printf("%s->", inet_ntop(AF_INET6, &ip6->ip6_src,
1385 1.1 itojun ntop_buf, INET6_ADDRSTRLEN));
1386 1.1 itojun printf("%s\n", inet_ntop(AF_INET6, &ip6->ip6_dst,
1387 1.1 itojun ntop_buf, INET6_ADDRSTRLEN));
1388 1.1 itojun }
1389 1.1 itojun
1390 1.1 itojun /*
1391 1.1 itojun * pr_addr --
1392 1.1 itojun * Return an ascii host address as a dotted quad and optionally with
1393 1.1 itojun * a hostname.
1394 1.1 itojun */
1395 1.1 itojun char *
1396 1.1 itojun pr_addr(addr)
1397 1.9 itojun struct sockaddr_in6 *addr;
1398 1.1 itojun {
1399 1.1 itojun static char buf[MAXHOSTNAMELEN];
1400 1.1 itojun int flag = 0;
1401 1.1 itojun
1402 1.1 itojun if (options & F_NUMERIC)
1403 1.1 itojun flag |= NI_NUMERICHOST;
1404 1.9 itojun #ifdef KAME_SCOPEID
1405 1.9 itojun flag |= NI_WITHSCOPEID;
1406 1.9 itojun #endif
1407 1.1 itojun
1408 1.9 itojun getnameinfo((struct sockaddr *)addr, addr->sin6_len, buf, sizeof(buf),
1409 1.9 itojun NULL, 0, flag);
1410 1.1 itojun
1411 1.1 itojun return (buf);
1412 1.1 itojun }
1413 1.1 itojun
1414 1.1 itojun /*
1415 1.1 itojun * pr_retip --
1416 1.1 itojun * Dump some info on a returned (via ICMPv6) IPv6 packet.
1417 1.1 itojun */
1418 1.1 itojun void
1419 1.1 itojun pr_retip(ip6, end)
1420 1.1 itojun struct ip6_hdr *ip6;
1421 1.1 itojun u_char *end;
1422 1.1 itojun {
1423 1.1 itojun u_char *cp = (u_char *)ip6, nh;
1424 1.1 itojun int hlen;
1425 1.1 itojun
1426 1.1 itojun if (end - (u_char *)ip6 < sizeof(*ip6)) {
1427 1.1 itojun printf("IP6");
1428 1.1 itojun goto trunc;
1429 1.1 itojun }
1430 1.1 itojun pr_iph(ip6);
1431 1.1 itojun hlen = sizeof(*ip6);
1432 1.1 itojun
1433 1.1 itojun nh = ip6->ip6_nxt;
1434 1.1 itojun cp += hlen;
1435 1.9 itojun while (end - cp >= 8) {
1436 1.9 itojun switch (nh) {
1437 1.1 itojun case IPPROTO_HOPOPTS:
1438 1.1 itojun printf("HBH ");
1439 1.1 itojun hlen = (((struct ip6_hbh *)cp)->ip6h_len+1) << 3;
1440 1.1 itojun nh = ((struct ip6_hbh *)cp)->ip6h_nxt;
1441 1.1 itojun break;
1442 1.1 itojun case IPPROTO_DSTOPTS:
1443 1.1 itojun printf("DSTOPT ");
1444 1.1 itojun hlen = (((struct ip6_dest *)cp)->ip6d_len+1) << 3;
1445 1.1 itojun nh = ((struct ip6_dest *)cp)->ip6d_nxt;
1446 1.1 itojun break;
1447 1.1 itojun case IPPROTO_FRAGMENT:
1448 1.1 itojun printf("FRAG ");
1449 1.1 itojun hlen = sizeof(struct ip6_frag);
1450 1.1 itojun nh = ((struct ip6_frag *)cp)->ip6f_nxt;
1451 1.1 itojun break;
1452 1.1 itojun case IPPROTO_ROUTING:
1453 1.1 itojun printf("RTHDR ");
1454 1.1 itojun hlen = (((struct ip6_rthdr *)cp)->ip6r_len+1) << 3;
1455 1.1 itojun nh = ((struct ip6_rthdr *)cp)->ip6r_nxt;
1456 1.1 itojun break;
1457 1.9 itojun #ifdef IPSEC
1458 1.1 itojun case IPPROTO_AH:
1459 1.1 itojun printf("AH ");
1460 1.1 itojun hlen = (((struct ah *)cp)->ah_len+2) << 2;
1461 1.1 itojun nh = ((struct ah *)cp)->ah_nxt;
1462 1.1 itojun break;
1463 1.9 itojun #endif
1464 1.1 itojun case IPPROTO_ICMPV6:
1465 1.1 itojun printf("ICMP6: type = %d, code = %d\n",
1466 1.1 itojun *cp, *(cp + 1));
1467 1.1 itojun return;
1468 1.1 itojun case IPPROTO_ESP:
1469 1.1 itojun printf("ESP\n");
1470 1.1 itojun return;
1471 1.1 itojun case IPPROTO_TCP:
1472 1.1 itojun printf("TCP: from port %u, to port %u (decimal)\n",
1473 1.1 itojun (*cp * 256 + *(cp + 1)),
1474 1.1 itojun (*(cp + 2) * 256 + *(cp + 3)));
1475 1.1 itojun return;
1476 1.1 itojun case IPPROTO_UDP:
1477 1.1 itojun printf("UDP: from port %u, to port %u (decimal)\n",
1478 1.1 itojun (*cp * 256 + *(cp + 1)),
1479 1.1 itojun (*(cp + 2) * 256 + *(cp + 3)));
1480 1.1 itojun return;
1481 1.1 itojun default:
1482 1.1 itojun printf("Unknown Header(%d)\n", nh);
1483 1.1 itojun return;
1484 1.1 itojun }
1485 1.1 itojun
1486 1.1 itojun if ((cp += hlen) >= end)
1487 1.1 itojun goto trunc;
1488 1.1 itojun }
1489 1.1 itojun if (end - cp < 8)
1490 1.1 itojun goto trunc;
1491 1.1 itojun
1492 1.1 itojun putchar('\n');
1493 1.1 itojun return;
1494 1.1 itojun
1495 1.1 itojun trunc:
1496 1.1 itojun printf("...\n");
1497 1.1 itojun return;
1498 1.1 itojun }
1499 1.1 itojun
1500 1.1 itojun void
1501 1.1 itojun fill(bp, patp)
1502 1.1 itojun char *bp, *patp;
1503 1.1 itojun {
1504 1.1 itojun register int ii, jj, kk;
1505 1.1 itojun int pat[16];
1506 1.1 itojun char *cp;
1507 1.1 itojun
1508 1.1 itojun for (cp = patp; *cp; cp++)
1509 1.1 itojun if (!isxdigit(*cp))
1510 1.1 itojun errx(1, "patterns must be specified as hex digits");
1511 1.1 itojun ii = sscanf(patp,
1512 1.1 itojun "%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x",
1513 1.1 itojun &pat[0], &pat[1], &pat[2], &pat[3], &pat[4], &pat[5], &pat[6],
1514 1.1 itojun &pat[7], &pat[8], &pat[9], &pat[10], &pat[11], &pat[12],
1515 1.1 itojun &pat[13], &pat[14], &pat[15]);
1516 1.1 itojun
1517 1.1 itojun /* xxx */
1518 1.1 itojun if (ii > 0)
1519 1.1 itojun for (kk = 0;
1520 1.1 itojun kk <= MAXDATALEN - (8 + sizeof(struct timeval) + ii);
1521 1.1 itojun kk += ii)
1522 1.1 itojun for (jj = 0; jj < ii; ++jj)
1523 1.1 itojun bp[jj + kk] = pat[jj];
1524 1.1 itojun if (!(options & F_QUIET)) {
1525 1.1 itojun (void)printf("PATTERN: 0x");
1526 1.1 itojun for (jj = 0; jj < ii; ++jj)
1527 1.1 itojun (void)printf("%02x", bp[jj] & 0xFF);
1528 1.1 itojun (void)printf("\n");
1529 1.1 itojun }
1530 1.1 itojun }
1531 1.1 itojun
1532 1.1 itojun void
1533 1.1 itojun usage()
1534 1.1 itojun {
1535 1.1 itojun (void)fprintf(stderr,
1536 1.1 itojun "usage: ping6 [-dfnqRrvwW"
1537 1.1 itojun #ifdef IPSEC
1538 1.1 itojun #ifdef IPSEC_POLICY_IPSEC
1539 1.1 itojun "] [-P policy"
1540 1.1 itojun #else
1541 1.1 itojun "AE"
1542 1.1 itojun #endif
1543 1.1 itojun #endif
1544 1.9 itojun "] [-a [alsg]] [-b sockbufsiz] [-c count] [-I interface]\n\
1545 1.9 itojun [-i wait] [-l preload] [-p pattern] [-S sourceaddr]\n\
1546 1.9 itojun [-s packetsize] [-h hoplimit] host [hosts...]\n");
1547 1.1 itojun exit(1);
1548 1.1 itojun }
1549