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