Deleted Added
full compact
ping6.c (57851) ping6.c (62627)
1/* $KAME: ping6.c,v 1.54 2000/06/12 16:16:44 itojun Exp $ */
2
1/*
2 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright

--- 49 unchanged lines hidden (view full) ---

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.
3/*
4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright

--- 49 unchanged lines hidden (view full) ---

60 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
61 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
62 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
63 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
64 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
65 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
66 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
67 * SUCH DAMAGE.
66 *
67 * $FreeBSD: head/sbin/ping6/ping6.c 57851 2000-03-09 14:47:21Z shin $
68 */
69
70#ifndef lint
68 */
69
70#ifndef lint
71static const char copyright[] =
71static char copyright[] =
72"@(#) Copyright (c) 1989, 1993\n\
73 The Regents of the University of California. All rights reserved.\n";
74#endif /* not lint */
75
76#ifndef lint
77#if 0
78static char sccsid[] = "@(#)ping.c 8.1 (Berkeley) 6/5/93";
79#endif
72"@(#) Copyright (c) 1989, 1993\n\
73 The Regents of the University of California. All rights reserved.\n";
74#endif /* not lint */
75
76#ifndef lint
77#if 0
78static char sccsid[] = "@(#)ping.c 8.1 (Berkeley) 6/5/93";
79#endif
80static const char rcsid[] =
81 "$FreeBSD: head/sbin/ping6/ping6.c 62627 2000-07-05 09:34:10Z kris $";
80#endif /* not lint */
81
82/*
83 * Using the InterNet Control Message Protocol (ICMP) "ECHO" facility,
84 * measure round-trip-delays and packet loss across network paths.
85 *
86 * Author -
87 * Mike Muuss

--- 4 unchanged lines hidden (view full) ---

92 * Public Domain. Distribution Unlimited.
93 * Bugs -
94 * More statistics could always be gathered.
95 * This program has to run SUID to ROOT to access the ICMP socket.
96 */
97/*
98 * NOTE:
99 * USE_SIN6_SCOPE_ID assumes that sin6_scope_id has the same semantics
82#endif /* not lint */
83
84/*
85 * Using the InterNet Control Message Protocol (ICMP) "ECHO" facility,
86 * measure round-trip-delays and packet loss across network paths.
87 *
88 * Author -
89 * Mike Muuss

--- 4 unchanged lines hidden (view full) ---

94 * Public Domain. Distribution Unlimited.
95 * Bugs -
96 * More statistics could always be gathered.
97 * This program has to run SUID to ROOT to access the ICMP socket.
98 */
99/*
100 * NOTE:
101 * USE_SIN6_SCOPE_ID assumes that sin6_scope_id has the same semantics
100 * as IPV6_PKTINFO. Some objects it (sin6_scope_id specifies *link* while
102 * as IPV6_PKTINFO. Some people object it (sin6_scope_id specifies *link* while
101 * IPV6_PKTINFO specifies *interface*. Link is defined as collection of
102 * network attached to 1 or more interfaces)
103 */
104
105#include <sys/param.h>
106#include <sys/uio.h>
107#include <sys/socket.h>
108#include <sys/time.h>

--- 20 unchanged lines hidden (view full) ---

129#include <string.h>
130#include <unistd.h>
131
132#ifdef IPSEC
133#include <netinet6/ah.h>
134#include <netinet6/ipsec.h>
135#endif
136
103 * IPV6_PKTINFO specifies *interface*. Link is defined as collection of
104 * network attached to 1 or more interfaces)
105 */
106
107#include <sys/param.h>
108#include <sys/uio.h>
109#include <sys/socket.h>
110#include <sys/time.h>

--- 20 unchanged lines hidden (view full) ---

131#include <string.h>
132#include <unistd.h>
133
134#ifdef IPSEC
135#include <netinet6/ah.h>
136#include <netinet6/ipsec.h>
137#endif
138
137#define MAXPACKETLEN 131072
139#if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__OpenBSD__)
140#include <md5.h>
141#else
142#include "md5.h"
143#endif
144
145#define MAXPACKETLEN 131072
138#define IP6LEN 40
146#define IP6LEN 40
139#define ICMP6ECHOLEN 8 /* icmp echo header len excluding time */
140#define ICMP6ECHOTMLEN sizeof(struct timeval)
141#define ICMP6_NIQLEN (ICMP6ECHOLEN + 8)
142#define ICMP6_NIRLEN (ICMP6ECHOLEN + 12) /* 64 bits of nonce + 32 bits ttl */
147#define ICMP6ECHOLEN 8 /* icmp echo header len excluding time */
148#define ICMP6ECHOTMLEN sizeof(struct timeval)
149#define ICMP6_NIQLEN (ICMP6ECHOLEN + 8)
150#define ICMP6_NIRLEN (ICMP6ECHOLEN + 12) /* 64 bits of nonce + 32 bits ttl */
143#define EXTRA 256 /* for AH and various other headers. weird. */
144#define DEFDATALEN ICMP6ECHOTMLEN
151#define EXTRA 256 /* for AH and various other headers. weird. */
152#define DEFDATALEN ICMP6ECHOTMLEN
145#define MAXDATALEN MAXPACKETLEN - IP6LEN - ICMP6ECHOLEN
153#define MAXDATALEN MAXPACKETLEN - IP6LEN - ICMP6ECHOLEN
146#define NROUTES 9 /* number of record route slots */
147
148#define A(bit) rcvd_tbl[(bit)>>3] /* identify byte in array */
149#define B(bit) (1 << ((bit) & 0x07)) /* identify bit in byte */
150#define SET(bit) (A(bit) |= B(bit))
151#define CLR(bit) (A(bit) &= (~B(bit)))
152#define TST(bit) (A(bit) & B(bit))
153
154#define F_FLOOD 0x0001
155#define F_INTERVAL 0x0002
156#define F_NUMERIC 0x0004
157#define F_PINGFILLED 0x0008
158#define F_QUIET 0x0010
159#define F_RROUTE 0x0020
160#define F_SO_DEBUG 0x0040
161#define F_VERBOSE 0x0100
162#ifdef IPSEC
163#ifdef IPSEC_POLICY_IPSEC
154#define NROUTES 9 /* number of record route slots */
155
156#define A(bit) rcvd_tbl[(bit)>>3] /* identify byte in array */
157#define B(bit) (1 << ((bit) & 0x07)) /* identify bit in byte */
158#define SET(bit) (A(bit) |= B(bit))
159#define CLR(bit) (A(bit) &= (~B(bit)))
160#define TST(bit) (A(bit) & B(bit))
161
162#define F_FLOOD 0x0001
163#define F_INTERVAL 0x0002
164#define F_NUMERIC 0x0004
165#define F_PINGFILLED 0x0008
166#define F_QUIET 0x0010
167#define F_RROUTE 0x0020
168#define F_SO_DEBUG 0x0040
169#define F_VERBOSE 0x0100
170#ifdef IPSEC
171#ifdef IPSEC_POLICY_IPSEC
164#define F_POLICY 0x4000
172#define F_POLICY 0x0400
165#else
173#else
166#define F_AUTHHDR 0x0200
167#define F_ENCRYPT 0x0400
174#define F_AUTHHDR 0x0200
175#define F_ENCRYPT 0x0400
168#endif /*IPSEC_POLICY_IPSEC*/
169#endif /*IPSEC*/
176#endif /*IPSEC_POLICY_IPSEC*/
177#endif /*IPSEC*/
170#define F_NODEADDR 0x0800
171#define F_FQDN 0x1000
172#define F_INTERFACE 0x2000
178#define F_NODEADDR 0x0800
179#define F_FQDN 0x1000
180#define F_INTERFACE 0x2000
181#define F_SRCADDR 0x4000
182#ifdef IPV6_REACHCONF
183#define F_REACHCONF 0x8000
184#endif
185#define F_HOSTNAME 0x10000
186#define F_FQDNOLD 0x20000
187#define F_NIGROUP 0x40000
173u_int options;
174
188u_int options;
189
175#define IN6LEN sizeof(struct in6_addr)
176#define SA6LEN sizeof(struct sockaddr_in6)
177#define DUMMY_PORT 10101
190#define IN6LEN sizeof(struct in6_addr)
191#define SA6LEN sizeof(struct sockaddr_in6)
192#define DUMMY_PORT 10101
178
193
179#define SIN6(s) ((struct sockaddr_in6 *)(s))
194#define SIN6(s) ((struct sockaddr_in6 *)(s))
180
181/*
182 * MAX_DUP_CHK is the number of bits in received table, i.e. the maximum
183 * number of received sequence numbers we can keep track of. Change 128
184 * to 8192 for complete accuracy...
185 */
186#define MAX_DUP_CHK (8 * 8192)
187int mx_dup_ck = MAX_DUP_CHK;
188char rcvd_tbl[MAX_DUP_CHK / 8];
189
195
196/*
197 * MAX_DUP_CHK is the number of bits in received table, i.e. the maximum
198 * number of received sequence numbers we can keep track of. Change 128
199 * to 8192 for complete accuracy...
200 */
201#define MAX_DUP_CHK (8 * 8192)
202int mx_dup_ck = MAX_DUP_CHK;
203char rcvd_tbl[MAX_DUP_CHK / 8];
204
190struct addrinfo *res;
205struct addrinfo *res;
191struct sockaddr_in6 dst; /* who to ping6 */
192struct sockaddr_in6 src; /* src addr of this packet */
193int datalen = DEFDATALEN;
194int s; /* socket file descriptor */
195u_char outpack[MAXPACKETLEN];
196char BSPACE = '\b'; /* characters written for flood */
197char DOT = '.';
198char *hostname;
199int ident; /* process id to identify our packets */
206struct sockaddr_in6 dst; /* who to ping6 */
207struct sockaddr_in6 src; /* src addr of this packet */
208int datalen = DEFDATALEN;
209int s; /* socket file descriptor */
210u_char outpack[MAXPACKETLEN];
211char BSPACE = '\b'; /* characters written for flood */
212char DOT = '.';
213char *hostname;
214int ident; /* process id to identify our packets */
215struct in6_addr srcaddr;
200
201/* counters */
202long npackets; /* max packets to transmit */
203long nreceived; /* # of packets we got back */
204long nrepeats; /* number of duplicates */
205long ntransmitted; /* sequence # for outbound packets = #sent */
206int interval = 1; /* interval between packets */
207int hoplimit = -1; /* hoplimit */
208
209/* timing */
210int timing; /* flag to do timing */
211double tmin = 999999999.0; /* minimum round trip time */
212double tmax = 0.0; /* maximum round trip time */
213double tsum = 0.0; /* sum of all times, for doing average */
214
216
217/* counters */
218long npackets; /* max packets to transmit */
219long nreceived; /* # of packets we got back */
220long nrepeats; /* number of duplicates */
221long ntransmitted; /* sequence # for outbound packets = #sent */
222int interval = 1; /* interval between packets */
223int hoplimit = -1; /* hoplimit */
224
225/* timing */
226int timing; /* flag to do timing */
227double tmin = 999999999.0; /* minimum round trip time */
228double tmax = 0.0; /* maximum round trip time */
229double tsum = 0.0; /* sum of all times, for doing average */
230
215/* for inet_ntop() */
216char ntop_buf[INET6_ADDRSTRLEN];
217
218/* for node addresses */
219u_short naflags;
220
221/* for ancillary data(advanced API) */
222struct msghdr smsghdr;
223struct iovec smsgiov;
224char *scmsg = 0;
225
226int main __P((int, char *[]));
227void fill __P((char *, char *));
228int get_hoplim __P((struct msghdr *));
231/* for node addresses */
232u_short naflags;
233
234/* for ancillary data(advanced API) */
235struct msghdr smsghdr;
236struct iovec smsgiov;
237char *scmsg = 0;
238
239int main __P((int, char *[]));
240void fill __P((char *, char *));
241int get_hoplim __P((struct msghdr *));
242struct in6_pktinfo *get_rcvpktinfo __P((struct msghdr *));
229void onalrm __P((int));
230void oninfo __P((int));
231void onint __P((int));
232void pinger __P((void));
243void onalrm __P((int));
244void oninfo __P((int));
245void onint __P((int));
246void pinger __P((void));
233char *pr_addr __P((struct sockaddr_in6 *));
247const char *pr_addr __P((struct sockaddr_in6 *));
234void pr_icmph __P((struct icmp6_hdr *, u_char *));
235void pr_iph __P((struct ip6_hdr *));
236void pr_nodeaddr __P((struct icmp6_nodeinfo *, int));
237void pr_pack __P((u_char *, int, struct msghdr *));
248void pr_icmph __P((struct icmp6_hdr *, u_char *));
249void pr_iph __P((struct ip6_hdr *));
250void pr_nodeaddr __P((struct icmp6_nodeinfo *, int));
251void pr_pack __P((u_char *, int, struct msghdr *));
252void pr_exthdrs __P((struct msghdr *));
253void pr_ip6opt __P((void *));
254void pr_rthdr __P((void *));
238void pr_retip __P((struct ip6_hdr *, u_char *));
239void summary __P((void));
240void tvsub __P((struct timeval *, struct timeval *));
241int setpolicy __P((int, char *));
255void pr_retip __P((struct ip6_hdr *, u_char *));
256void summary __P((void));
257void tvsub __P((struct timeval *, struct timeval *));
258int setpolicy __P((int, char *));
259char *nigroup __P((char *));
242void usage __P((void));
243
244int
245main(argc, argv)
246 int argc;
247 char *argv[];
248{
260void usage __P((void));
261
262int
263main(argc, argv)
264 int argc;
265 char *argv[];
266{
249 extern int errno, optind;
250 extern char *optarg;
251 struct itimerval itimer;
252 struct sockaddr_in6 from;
253 struct timeval timeout;
254 struct addrinfo hints;
255 fd_set fdset;
256 register int cc, i;
257 int ch, fromlen, hold, packlen, preload, optval, ret_ga;
258 u_char *datap, *packet;
259 char *e, *target, *ifname = NULL;
260 int ip6optlen = 0;
261 struct cmsghdr *scmsgp = NULL;
262 int sockbufsize = 0;
267 struct itimerval itimer;
268 struct sockaddr_in6 from;
269 struct timeval timeout;
270 struct addrinfo hints;
271 fd_set fdset;
272 register int cc, i;
273 int ch, fromlen, hold, packlen, preload, optval, ret_ga;
274 u_char *datap, *packet;
275 char *e, *target, *ifname = NULL;
276 int ip6optlen = 0;
277 struct cmsghdr *scmsgp = NULL;
278 int sockbufsize = 0;
279 int usepktinfo = 0;
280 struct in6_pktinfo *pktinfo = NULL;
281#ifdef USE_RFC2292BIS
282 struct ip6_rthdr *rthdr = NULL;
283#endif
263#ifdef IPSEC_POLICY_IPSEC
264 char *policy_in = NULL;
265 char *policy_out = NULL;
266#endif
267
268 /* just to be sure */
269 memset(&smsghdr, 0, sizeof(&smsghdr));
270 memset(&smsgiov, 0, sizeof(&smsgiov));
271
284#ifdef IPSEC_POLICY_IPSEC
285 char *policy_in = NULL;
286 char *policy_out = NULL;
287#endif
288
289 /* just to be sure */
290 memset(&smsghdr, 0, sizeof(&smsghdr));
291 memset(&smsgiov, 0, sizeof(&smsgiov));
292
272 if ((s = socket(AF_INET6, SOCK_RAW, IPPROTO_ICMPV6)) < 0)
273 err(1, "socket");
274 setuid(getuid());
275
276 preload = 0;
277 datap = &outpack[ICMP6ECHOLEN + ICMP6ECHOTMLEN];
278#ifndef IPSEC
293 preload = 0;
294 datap = &outpack[ICMP6ECHOLEN + ICMP6ECHOTMLEN];
295#ifndef IPSEC
279 while ((ch = getopt(argc, argv, "a:b:c:dfh:I:i:l:np:qRrs:vwW")) != EOF)
296 while ((ch = getopt(argc, argv, "a:b:c:dfHh:I:i:l:nNp:qRS:s:vwW")) != EOF)
280#else
281#ifdef IPSEC_POLICY_IPSEC
297#else
298#ifdef IPSEC_POLICY_IPSEC
282 while ((ch = getopt(argc, argv, "a:b:c:dfh:I:i:l:np:qRrs:vwWP:")) != EOF)
299 while ((ch = getopt(argc, argv, "a:b:c:dfHh:I:i:l:nNp:qRS:s:vwWP:")) != EOF)
283#else
300#else
284 while ((ch = getopt(argc, argv, "a:b:c:dfh:I:i:l:np:qRrs:vwWAE")) != EOF)
301 while ((ch = getopt(argc, argv, "a:b:c:dfHh:I:i:l:nNp:qRS:s:vwWAE")) != EOF)
285#endif /*IPSEC_POLICY_IPSEC*/
286#endif
302#endif /*IPSEC_POLICY_IPSEC*/
303#endif
304 {
287 switch(ch) {
288 case 'a':
289 {
290 char *cp;
291
292 options |= F_NODEADDR;
293 datalen = 2048; /* XXX: enough? */
294 for (cp = optarg; *cp != '\0'; cp++) {

--- 17 unchanged lines hidden (view full) ---

312 case 'G':
313 naflags |= NI_NODEADDR_FLAG_GLOBAL;
314 break;
315 case 'A': /* experimental. not in the spec */
316 naflags |= NI_NODEADDR_FLAG_ANYCAST;
317 break;
318 default:
319 usage();
305 switch(ch) {
306 case 'a':
307 {
308 char *cp;
309
310 options |= F_NODEADDR;
311 datalen = 2048; /* XXX: enough? */
312 for (cp = optarg; *cp != '\0'; cp++) {

--- 17 unchanged lines hidden (view full) ---

330 case 'G':
331 naflags |= NI_NODEADDR_FLAG_GLOBAL;
332 break;
333 case 'A': /* experimental. not in the spec */
334 naflags |= NI_NODEADDR_FLAG_ANYCAST;
335 break;
336 default:
337 usage();
338 /*NOTREACHED*/
320 }
321 }
322 break;
323 }
324 case 'b':
325#if defined(SO_SNDBUF) && defined(SO_RCVBUF)
326 sockbufsize = atoi(optarg);
327#else

--- 13 unchanged lines hidden (view full) ---

341 case 'f':
342 if (getuid()) {
343 errno = EPERM;
344 errx(1, "Must be superuser to flood ping");
345 }
346 options |= F_FLOOD;
347 setbuf(stdout, (char *)NULL);
348 break;
339 }
340 }
341 break;
342 }
343 case 'b':
344#if defined(SO_SNDBUF) && defined(SO_RCVBUF)
345 sockbufsize = atoi(optarg);
346#else

--- 13 unchanged lines hidden (view full) ---

360 case 'f':
361 if (getuid()) {
362 errno = EPERM;
363 errx(1, "Must be superuser to flood ping");
364 }
365 options |= F_FLOOD;
366 setbuf(stdout, (char *)NULL);
367 break;
368 case 'H':
369 options |= F_HOSTNAME;
370 break;
349 case 'h': /* hoplimit */
350 hoplimit = strtol(optarg, &e, 10);
351 if (255 < hoplimit || hoplimit < -1)
352 errx(1,
353 "illegal hoplimit -- %s", optarg);
354 break;
355 case 'I':
356 ifname = optarg;
357 options |= F_INTERFACE;
371 case 'h': /* hoplimit */
372 hoplimit = strtol(optarg, &e, 10);
373 if (255 < hoplimit || hoplimit < -1)
374 errx(1,
375 "illegal hoplimit -- %s", optarg);
376 break;
377 case 'I':
378 ifname = optarg;
379 options |= F_INTERFACE;
380#ifndef USE_SIN6_SCOPE_ID
381 usepktinfo++;
382#endif
358 break;
359 case 'i': /* wait between sending packets */
360 interval = strtol(optarg, &e, 10);
361 if (interval <= 0 || *optarg == '\0' || *e != '\0')
362 errx(1,
363 "illegal timing interval -- %s", optarg);
364 options |= F_INTERVAL;
365 break;

--- 4 unchanged lines hidden (view full) ---

370 }
371 preload = strtol(optarg, &e, 10);
372 if (preload < 0 || *optarg == '\0' || *e != '\0')
373 errx(1, "illegal preload value -- %s", optarg);
374 break;
375 case 'n':
376 options |= F_NUMERIC;
377 break;
383 break;
384 case 'i': /* wait between sending packets */
385 interval = strtol(optarg, &e, 10);
386 if (interval <= 0 || *optarg == '\0' || *e != '\0')
387 errx(1,
388 "illegal timing interval -- %s", optarg);
389 options |= F_INTERVAL;
390 break;

--- 4 unchanged lines hidden (view full) ---

395 }
396 preload = strtol(optarg, &e, 10);
397 if (preload < 0 || *optarg == '\0' || *e != '\0')
398 errx(1, "illegal preload value -- %s", optarg);
399 break;
400 case 'n':
401 options |= F_NUMERIC;
402 break;
403 case 'N':
404 options |= F_NIGROUP;
405 break;
378 case 'p': /* fill buffer with user pattern */
379 options |= F_PINGFILLED;
380 fill((char *)datap, optarg);
381 break;
382 case 'q':
383 options |= F_QUIET;
384 break;
385 case 'R':
406 case 'p': /* fill buffer with user pattern */
407 options |= F_PINGFILLED;
408 fill((char *)datap, optarg);
409 break;
410 case 'q':
411 options |= F_QUIET;
412 break;
413 case 'R':
386 options |= F_RROUTE;
414#ifdef IPV6_REACHCONF
415 options |= F_REACHCONF;
387 break;
416 break;
417#else
418 errx(1, "-R is not supported in this configuration");
419#endif
420 case 'S':
421 /* XXX: use getaddrinfo? */
422 if (inet_pton(AF_INET6, optarg, (void *)&srcaddr) != 1)
423 errx(1, "invalid IPv6 address: %s", optarg);
424 options |= F_SRCADDR;
425 usepktinfo++;
426 break;
388 case 's': /* size of packet to send */
389 datalen = strtol(optarg, &e, 10);
390 if (datalen <= 0 || *optarg == '\0' || *e != '\0')
391 errx(1, "illegal datalen value -- %s", optarg);
392 if (datalen > MAXDATALEN)
393 errx(1,
394 "datalen value too large, maximum is %d",
395 MAXDATALEN);
396 break;
397 case 'v':
398 options |= F_VERBOSE;
399 break;
400 case 'w':
427 case 's': /* size of packet to send */
428 datalen = strtol(optarg, &e, 10);
429 if (datalen <= 0 || *optarg == '\0' || *e != '\0')
430 errx(1, "illegal datalen value -- %s", optarg);
431 if (datalen > MAXDATALEN)
432 errx(1,
433 "datalen value too large, maximum is %d",
434 MAXDATALEN);
435 break;
436 case 'v':
437 options |= F_VERBOSE;
438 break;
439 case 'w':
401 case 'W':
402 options |= F_FQDN;
403 break;
440 options |= F_FQDN;
441 break;
442 case 'W':
443 options |= F_FQDNOLD;
444 break;
404#ifdef IPSEC
405#ifdef IPSEC_POLICY_IPSEC
406 case 'P':
407 options |= F_POLICY;
408 if (!strncmp("in", optarg, 2))
409 policy_in = strdup(optarg);
410 else if (!strncmp("out", optarg, 3))
411 policy_out = strdup(optarg);

--- 6 unchanged lines hidden (view full) ---

418 break;
419 case 'E':
420 options |= F_ENCRYPT;
421 break;
422#endif /*IPSEC_POLICY_IPSEC*/
423#endif /*IPSEC*/
424 default:
425 usage();
445#ifdef IPSEC
446#ifdef IPSEC_POLICY_IPSEC
447 case 'P':
448 options |= F_POLICY;
449 if (!strncmp("in", optarg, 2))
450 policy_in = strdup(optarg);
451 else if (!strncmp("out", optarg, 3))
452 policy_out = strdup(optarg);

--- 6 unchanged lines hidden (view full) ---

459 break;
460 case 'E':
461 options |= F_ENCRYPT;
462 break;
463#endif /*IPSEC_POLICY_IPSEC*/
464#endif /*IPSEC*/
465 default:
466 usage();
467 /*NOTREACHED*/
426 }
468 }
469 }
427 argc -= optind;
428 argv += optind;
429
470 argc -= optind;
471 argv += optind;
472
430 if (argc < 1)
473 if (argc < 1) {
431 usage();
474 usage();
475 /*NOTREACHED*/
476 }
432
477
433 if (argc > 1)
478 if (argc > 1) {
479#ifdef USE_SIN6_SCOPE_ID
480 ip6optlen += CMSG_SPACE(inet6_rth_space(IPV6_RTHDR_TYPE_0, argc - 1));
481#else /* old advanced API */
434 ip6optlen += inet6_rthdr_space(IPV6_RTHDR_TYPE_0, argc - 1);
482 ip6optlen += inet6_rthdr_space(IPV6_RTHDR_TYPE_0, argc - 1);
483#endif
484 }
435
485
436 target = argv[argc - 1];
486 if (options & F_NIGROUP) {
487 target = nigroup(argv[argc - 1]);
488 if (target == NULL) {
489 usage();
490 /*NOTREACHED*/
491 }
492 } else
493 target = argv[argc - 1];
437
438 /* getaddrinfo */
439 bzero(&hints, sizeof(struct addrinfo));
494
495 /* getaddrinfo */
496 bzero(&hints, sizeof(struct addrinfo));
440 if ((options & F_NUMERIC) == 0)
497 if ((options & F_NUMERIC) != 0)
441 hints.ai_flags = AI_CANONNAME;
442 hints.ai_family = AF_INET6;
443 hints.ai_socktype = SOCK_RAW;
444 hints.ai_protocol = IPPROTO_ICMPV6;
445
446 ret_ga = getaddrinfo(target, NULL, &hints, &res);
447 if (ret_ga) {
448 fprintf(stderr, "ping6: %s\n", gai_strerror(ret_ga));
498 hints.ai_flags = AI_CANONNAME;
499 hints.ai_family = AF_INET6;
500 hints.ai_socktype = SOCK_RAW;
501 hints.ai_protocol = IPPROTO_ICMPV6;
502
503 ret_ga = getaddrinfo(target, NULL, &hints, &res);
504 if (ret_ga) {
505 fprintf(stderr, "ping6: %s\n", gai_strerror(ret_ga));
449 if (ret_ga == EAI_SYSTEM)
450 errx(1, "%s", strerror(errno));
451 exit(1);
452 }
453 if (res->ai_canonname)
454 hostname = res->ai_canonname;
455 else
456 hostname = target;
506 exit(1);
507 }
508 if (res->ai_canonname)
509 hostname = res->ai_canonname;
510 else
511 hostname = target;
457
512
458 if (!res->ai_addr)
459 errx(1, "getaddrinfo failed");
460
461 (void)memcpy(&dst, res->ai_addr, res->ai_addrlen);
462
463 if (options & F_FLOOD && options & F_INTERVAL)
464 errx(1, "-f and -i incompatible options");
465
466 if (datalen >= sizeof(struct timeval)) /* can we time transfer */
467 timing = 1;
468 packlen = datalen + IP6LEN + ICMP6ECHOLEN + EXTRA;
469 if (!(packet = (u_char *)malloc((u_int)packlen)))
470 err(1, "Unable to allocate packet");
471 if (!(options & F_PINGFILLED))
472 for (i = 8; i < datalen; ++i)
473 *datap++ = i;
474
475 ident = getpid() & 0xFFFF;
476
513 if (!res->ai_addr)
514 errx(1, "getaddrinfo failed");
515
516 (void)memcpy(&dst, res->ai_addr, res->ai_addrlen);
517
518 if (options & F_FLOOD && options & F_INTERVAL)
519 errx(1, "-f and -i incompatible options");
520
521 if (datalen >= sizeof(struct timeval)) /* can we time transfer */
522 timing = 1;
523 packlen = datalen + IP6LEN + ICMP6ECHOLEN + EXTRA;
524 if (!(packet = (u_char *)malloc((u_int)packlen)))
525 err(1, "Unable to allocate packet");
526 if (!(options & F_PINGFILLED))
527 for (i = 8; i < datalen; ++i)
528 *datap++ = i;
529
530 ident = getpid() & 0xFFFF;
531
532 if ((s = socket(res->ai_family, res->ai_socktype, res->ai_protocol)) < 0)
533 err(1, "socket");
534
477 hold = 1;
478
479 if (options & F_SO_DEBUG)
480 (void)setsockopt(s, SOL_SOCKET, SO_DEBUG, (char *)&hold,
481 sizeof(hold));
482 optval = IPV6_DEFHLIM;
483 if (IN6_IS_ADDR_MULTICAST(&dst.sin6_addr))
484 if (setsockopt(s, IPPROTO_IPV6, IPV6_MULTICAST_HOPS,

--- 30 unchanged lines hidden (view full) ---

515#endif /*IPSEC_POLICY_IPSEC*/
516#endif
517
518#ifdef ICMP6_FILTER
519 {
520 struct icmp6_filter filt;
521 if (!(options & F_VERBOSE)) {
522 ICMP6_FILTER_SETBLOCKALL(&filt);
535 hold = 1;
536
537 if (options & F_SO_DEBUG)
538 (void)setsockopt(s, SOL_SOCKET, SO_DEBUG, (char *)&hold,
539 sizeof(hold));
540 optval = IPV6_DEFHLIM;
541 if (IN6_IS_ADDR_MULTICAST(&dst.sin6_addr))
542 if (setsockopt(s, IPPROTO_IPV6, IPV6_MULTICAST_HOPS,

--- 30 unchanged lines hidden (view full) ---

573#endif /*IPSEC_POLICY_IPSEC*/
574#endif
575
576#ifdef ICMP6_FILTER
577 {
578 struct icmp6_filter filt;
579 if (!(options & F_VERBOSE)) {
580 ICMP6_FILTER_SETBLOCKALL(&filt);
523 if ((options & F_FQDN) || (options & F_NODEADDR))
581 if ((options & F_FQDN) || (options & F_FQDNOLD) ||
582 (options & F_NODEADDR))
524 ICMP6_FILTER_SETPASS(ICMP6_NI_REPLY, &filt);
525 else
526 ICMP6_FILTER_SETPASS(ICMP6_ECHO_REPLY, &filt);
527 } else {
528 ICMP6_FILTER_SETPASSALL(&filt);
529 }
530 if (setsockopt(s, IPPROTO_ICMPV6, ICMP6_FILTER, &filt,
531 sizeof(filt)) < 0)
532 err(1, "setsockopt(ICMP6_FILTER)");
533 }
534#endif /*ICMP6_FILTER*/
535
583 ICMP6_FILTER_SETPASS(ICMP6_NI_REPLY, &filt);
584 else
585 ICMP6_FILTER_SETPASS(ICMP6_ECHO_REPLY, &filt);
586 } else {
587 ICMP6_FILTER_SETPASSALL(&filt);
588 }
589 if (setsockopt(s, IPPROTO_ICMPV6, ICMP6_FILTER, &filt,
590 sizeof(filt)) < 0)
591 err(1, "setsockopt(ICMP6_FILTER)");
592 }
593#endif /*ICMP6_FILTER*/
594
595 /* let the kerel pass extension headers of incoming packets */
596 /* TODO: implement parsing routine */
597 if ((options & F_VERBOSE) != 0) {
598 int opton = 1;
599
600#ifdef IPV6_RECVRTHDR
601 if (setsockopt(s, IPPROTO_IPV6, IPV6_RECVRTHDR, &opton,
602 sizeof(opton)))
603 err(1, "setsockopt(IPV6_RECVRTHDR)");
604#else /* old adv. API */
605 if (setsockopt(s, IPPROTO_IPV6, IPV6_RTHDR, &opton,
606 sizeof(opton)))
607 err(1, "setsockopt(IPV6_RTHDR)");
608#endif
609#ifdef IPV6_RECVHOPOPTS
610 if (setsockopt(s, IPPROTO_IPV6, IPV6_RECVHOPOPTS, &opton,
611 sizeof(opton)))
612 err(1, "setsockopt(IPV6_RECVHOPOPTS)");
613#else /* old adv. API */
614 if (setsockopt(s, IPPROTO_IPV6, IPV6_HOPOPTS, &opton,
615 sizeof(opton)))
616 err(1, "setsockopt(IPV6_HOPOPTS)");
617#endif
618#ifdef IPV6_RECVDSTOPTS
619 if (setsockopt(s, IPPROTO_IPV6, IPV6_RECVDSTOPTS, &opton,
620 sizeof(opton)))
621 err(1, "setsockopt(IPV6_RECVDSTOPTS)");
622#else /* olad adv. API */
623 if (setsockopt(s, IPPROTO_IPV6, IPV6_DSTOPTS, &opton,
624 sizeof(opton)))
625 err(1, "setsockopt(IPV6_DSTOPTS)");
626#endif
627#ifdef IPV6_RECVRTHDRDSTOPTS
628 if (setsockopt(s, IPPROTO_IPV6, IPV6_RECVRTHDRDSTOPTS, &opton,
629 sizeof(opton)))
630 err(1, "setsockopt(IPV6_RECVRTHDRDSTOPTS)");
631#endif
632 }
633
536/*
537 optval = 1;
538 if (IN6_IS_ADDR_MULTICAST(&dst.sin6_addr))
539 if (setsockopt(s, IPPROTO_IPV6, IPV6_MULTICAST_LOOP,
540 &optval, sizeof(optval)) == -1)
541 err(1, "IPV6_MULTICAST_LOOP");
542*/
634/*
635 optval = 1;
636 if (IN6_IS_ADDR_MULTICAST(&dst.sin6_addr))
637 if (setsockopt(s, IPPROTO_IPV6, IPV6_MULTICAST_LOOP,
638 &optval, sizeof(optval)) == -1)
639 err(1, "IPV6_MULTICAST_LOOP");
640*/
543 /* record route option */
544 if (options & F_RROUTE)
545 errx(1, "record route not available in this implementation");
546
641
547 /* Outgoing interface */
548#ifndef USE_SIN6_SCOPE_ID
549 if (options & F_INTERFACE)
642 /* Specify the outgoing interface and/or the source address */
643 if (usepktinfo)
550 ip6optlen += CMSG_SPACE(sizeof(struct in6_pktinfo));
644 ip6optlen += CMSG_SPACE(sizeof(struct in6_pktinfo));
551#endif
552
553 if (hoplimit != -1)
554 ip6optlen += CMSG_SPACE(sizeof(int));
555
645
646 if (hoplimit != -1)
647 ip6optlen += CMSG_SPACE(sizeof(int));
648
649#ifdef IPV6_REACHCONF
650 if (options & F_REACHCONF)
651 ip6optlen += CMSG_SPACE(0);
652#endif
653
556 /* set IP6 packet options */
557 if (ip6optlen) {
558 if ((scmsg = (char *)malloc(ip6optlen)) == 0)
559 errx(1, "can't allocate enough memory");
560 smsghdr.msg_control = (caddr_t)scmsg;
561 smsghdr.msg_controllen = ip6optlen;
562 scmsgp = (struct cmsghdr *)scmsg;
563 }
654 /* set IP6 packet options */
655 if (ip6optlen) {
656 if ((scmsg = (char *)malloc(ip6optlen)) == 0)
657 errx(1, "can't allocate enough memory");
658 smsghdr.msg_control = (caddr_t)scmsg;
659 smsghdr.msg_controllen = ip6optlen;
660 scmsgp = (struct cmsghdr *)scmsg;
661 }
564 if (options & F_INTERFACE) {
565#ifndef USE_SIN6_SCOPE_ID
566 struct in6_pktinfo *pktinfo =
567 (struct in6_pktinfo *)(CMSG_DATA(scmsgp));
568
569 if ((pktinfo->ipi6_ifindex = if_nametoindex(ifname)) == 0)
570 errx(1, "%s: invalid interface name", ifname);
571 bzero(&pktinfo->ipi6_addr, sizeof(struct in6_addr));
662 if (usepktinfo) {
663 pktinfo = (struct in6_pktinfo *)(CMSG_DATA(scmsgp));
664 memset(pktinfo, 0, sizeof(*pktinfo));
572 scmsgp->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo));
573 scmsgp->cmsg_level = IPPROTO_IPV6;
574 scmsgp->cmsg_type = IPV6_PKTINFO;
665 scmsgp->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo));
666 scmsgp->cmsg_level = IPPROTO_IPV6;
667 scmsgp->cmsg_type = IPV6_PKTINFO;
575
576 scmsgp = CMSG_NXTHDR(&smsghdr, scmsgp);
668 scmsgp = CMSG_NXTHDR(&smsghdr, scmsgp);
669 }
670
671 /* set the outgoing interface */
672 if (ifname) {
673#ifndef USE_SIN6_SCOPE_ID
674 /* pktinfo must have already been allocated */
675 if ((pktinfo->ipi6_ifindex = if_nametoindex(ifname)) == 0)
676 errx(1, "%s: invalid interface name", ifname);
577#else
578 if ((dst.sin6_scope_id = if_nametoindex(ifname)) == 0)
579 errx(1, "%s: invalid interface name", ifname);
580#endif
581 }
677#else
678 if ((dst.sin6_scope_id = if_nametoindex(ifname)) == 0)
679 errx(1, "%s: invalid interface name", ifname);
680#endif
681 }
682 /* set the source address */
683 if (options & F_SRCADDR)/* pktinfo must be valid */
684 pktinfo->ipi6_addr = srcaddr;
685
582 if (hoplimit != -1) {
583 scmsgp->cmsg_len = CMSG_LEN(sizeof(int));
584 scmsgp->cmsg_level = IPPROTO_IPV6;
585 scmsgp->cmsg_type = IPV6_HOPLIMIT;
586 *(int *)(CMSG_DATA(scmsgp)) = hoplimit;
587
588 scmsgp = CMSG_NXTHDR(&smsghdr, scmsgp);
589 }
686 if (hoplimit != -1) {
687 scmsgp->cmsg_len = CMSG_LEN(sizeof(int));
688 scmsgp->cmsg_level = IPPROTO_IPV6;
689 scmsgp->cmsg_type = IPV6_HOPLIMIT;
690 *(int *)(CMSG_DATA(scmsgp)) = hoplimit;
691
692 scmsgp = CMSG_NXTHDR(&smsghdr, scmsgp);
693 }
694#ifdef IPV6_REACHCONF
695 if (options & F_REACHCONF) {
696 scmsgp->cmsg_len = CMSG_LEN(0);
697 scmsgp->cmsg_level = IPPROTO_IPV6;
698 scmsgp->cmsg_type = IPV6_REACHCONF;
699
700 scmsgp = CMSG_NXTHDR(&smsghdr, scmsgp);
701 }
702#endif
703
590 if (argc > 1) { /* some intermediate addrs are specified */
591 int hops, error;
704 if (argc > 1) { /* some intermediate addrs are specified */
705 int hops, error;
706#ifdef USE_RFC2292BIS
707 int rthdrlen;
708#endif
592
709
710#ifdef USE_RFC2292BIS
711 rthdrlen = inet6_rth_space(IPV6_RTHDR_TYPE_0, argc - 1);
712 scmsgp->cmsg_len = CMSG_LEN(rthdrlen);
713 scmsgp->cmsg_level = IPPROTO_IPV6;
714 scmsgp->cmsg_type = IPV6_RTHDR;
715 rthdr = (struct ip6_rthdr *)CMSG_DATA(scmsgp);
716 rthdr = inet6_rth_init((void *)rthdr, rthdrlen,
717 IPV6_RTHDR_TYPE_0, argc - 1);
718 if (rthdr == NULL)
719 errx(1, "can't initialize rthdr");
720#else /* old advanced API */
593 if ((scmsgp = (struct cmsghdr *)inet6_rthdr_init(scmsgp,
594 IPV6_RTHDR_TYPE_0)) == 0)
595 errx(1, "can't initialize rthdr");
721 if ((scmsgp = (struct cmsghdr *)inet6_rthdr_init(scmsgp,
722 IPV6_RTHDR_TYPE_0)) == 0)
723 errx(1, "can't initialize rthdr");
724#endif /* USE_RFC2292BIS */
596
597 for (hops = 0; hops < argc - 1; hops++) {
598 struct addrinfo *iaip;
599
725
726 for (hops = 0; hops < argc - 1; hops++) {
727 struct addrinfo *iaip;
728
600 if ((error = getaddrinfo(argv[hops], NULL, &hints, &iaip))) {
601 fprintf(stderr, "ping6: %s\n", gai_strerror(error));
602 if (error == EAI_SYSTEM)
603 errx(1, strerror(errno));
604 exit(1);
605 }
729 if ((error = getaddrinfo(argv[hops], NULL, &hints, &iaip)))
730 errx(1, gai_strerror(error));
606 if (SIN6(res->ai_addr)->sin6_family != AF_INET6)
607 errx(1,
608 "bad addr family of an intermediate addr");
609
731 if (SIN6(res->ai_addr)->sin6_family != AF_INET6)
732 errx(1,
733 "bad addr family of an intermediate addr");
734
735#ifdef USE_RFC2292BIS
736 if (inet6_rth_add(rthdr,
737 &(SIN6(iaip->ai_addr))->sin6_addr))
738 errx(1, "can't add an intermediate node");
739#else /* old advanced API */
610 if (inet6_rthdr_add(scmsgp,
611 &(SIN6(iaip->ai_addr))->sin6_addr,
612 IPV6_RTHDR_LOOSE))
613 errx(1, "can't add an intermediate node");
740 if (inet6_rthdr_add(scmsgp,
741 &(SIN6(iaip->ai_addr))->sin6_addr,
742 IPV6_RTHDR_LOOSE))
743 errx(1, "can't add an intermediate node");
744#endif /* USE_RFC2292BIS */
614 }
615
745 }
746
747#ifndef USE_RFC2292BIS
616 if (inet6_rthdr_lasthop(scmsgp, IPV6_RTHDR_LOOSE))
617 errx(1, "can't set the last flag");
748 if (inet6_rthdr_lasthop(scmsgp, IPV6_RTHDR_LOOSE))
749 errx(1, "can't set the last flag");
750#endif
618
619 scmsgp = CMSG_NXTHDR(&smsghdr, scmsgp);
620 }
621
622 {
623 /*
624 * source selection
625 */
626 int dummy, len = sizeof(src);
751
752 scmsgp = CMSG_NXTHDR(&smsghdr, scmsgp);
753 }
754
755 {
756 /*
757 * source selection
758 */
759 int dummy, len = sizeof(src);
627
760
628 if ((dummy = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
629 err(1, "UDP socket");
630
631 src.sin6_family = AF_INET6;
632 src.sin6_addr = dst.sin6_addr;
633 src.sin6_port = ntohs(DUMMY_PORT);
634 src.sin6_scope_id = dst.sin6_scope_id;
635
761 if ((dummy = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
762 err(1, "UDP socket");
763
764 src.sin6_family = AF_INET6;
765 src.sin6_addr = dst.sin6_addr;
766 src.sin6_port = ntohs(DUMMY_PORT);
767 src.sin6_scope_id = dst.sin6_scope_id;
768
636#ifndef USE_SIN6_SCOPE_ID
637 if (setsockopt(dummy, IPPROTO_IPV6, IPV6_PKTOPTIONS,
769
770#ifdef USE_SIN6_SCOPE_ID
771 src.sin6_scope_id = dst.sin6_scope_id;
772#endif
773
774#ifdef USE_RFC2292BIS
775 if (pktinfo &&
776 setsockopt(dummy, IPPROTO_IPV6, IPV6_PKTINFO,
777 (void *)pktinfo, sizeof(*pktinfo)))
778 err(1, "UDP setsockopt(IPV6_PKTINFO)");
779
780 if (hoplimit != -1 &&
781 setsockopt(dummy, IPPROTO_IPV6, IPV6_HOPLIMIT,
782 (void *)&hoplimit, sizeof(hoplimit)))
783 err(1, "UDP setsockopt(IPV6_HOPLIMIT)");
784
785 if (rthdr &&
786 setsockopt(dummy, IPPROTO_IPV6, IPV6_RTHDR,
787 (void *)rthdr, (rthdr->ip6r_len + 1) << 3))
788 err(1, "UDP setsockopt(IPV6_RTHDR)");
789#else /* old advanced API */
790 if (smsghdr.msg_control &&
791 setsockopt(dummy, IPPROTO_IPV6, IPV6_PKTOPTIONS,
638 (void *)smsghdr.msg_control,
639 smsghdr.msg_controllen)) {
792 (void *)smsghdr.msg_control,
793 smsghdr.msg_controllen)) {
640 err(1, "UDP setsockopt");
794 err(1, "UDP setsockopt(IPV6_PKTOPTIONS)");
641 }
795 }
642#else
643 src.sin6_scope_id = dst.sin6_scope_id;
644#endif
796#endif
645
797
646 if (connect(dummy, (struct sockaddr *)&src, len) < 0)
647 err(1, "UDP connect");
798 if (connect(dummy, (struct sockaddr *)&src, len) < 0)
799 err(1, "UDP connect");
648
800
649 if (getsockname(dummy, (struct sockaddr *)&src, &len) < 0)
650 err(1, "getsockname");
651
652 close(dummy);
653 }
654
655#if defined(SO_SNDBUF) && defined(SO_RCVBUF)
656 if (sockbufsize) {
657 if (datalen > sockbufsize)
801 if (getsockname(dummy, (struct sockaddr *)&src, &len) < 0)
802 err(1, "getsockname");
803
804 close(dummy);
805 }
806
807#if defined(SO_SNDBUF) && defined(SO_RCVBUF)
808 if (sockbufsize) {
809 if (datalen > sockbufsize)
658 warnx("you need -b to increae socket buffer size");
810 warnx("you need -b to increase socket buffer size");
659 if (setsockopt(s, SOL_SOCKET, SO_SNDBUF, &sockbufsize,
660 sizeof(sockbufsize)) < 0)
661 err(1, "setsockopt(SO_SNDBUF)");
662 if (setsockopt(s, SOL_SOCKET, SO_RCVBUF, &sockbufsize,
663 sizeof(sockbufsize)) < 0)
664 err(1, "setsockopt(SO_RCVBUF)");
665 }
666 else {

--- 7 unchanged lines hidden (view full) ---

674 */
675 hold = 48 * 1024;
676 setsockopt(s, SOL_SOCKET, SO_RCVBUF, (char *)&hold, sizeof(hold));
677 }
678#endif
679
680 optval = 1;
681#ifndef USE_SIN6_SCOPE_ID
811 if (setsockopt(s, SOL_SOCKET, SO_SNDBUF, &sockbufsize,
812 sizeof(sockbufsize)) < 0)
813 err(1, "setsockopt(SO_SNDBUF)");
814 if (setsockopt(s, SOL_SOCKET, SO_RCVBUF, &sockbufsize,
815 sizeof(sockbufsize)) < 0)
816 err(1, "setsockopt(SO_RCVBUF)");
817 }
818 else {

--- 7 unchanged lines hidden (view full) ---

826 */
827 hold = 48 * 1024;
828 setsockopt(s, SOL_SOCKET, SO_RCVBUF, (char *)&hold, sizeof(hold));
829 }
830#endif
831
832 optval = 1;
833#ifndef USE_SIN6_SCOPE_ID
682 setsockopt(s, IPPROTO_IPV6, IPV6_PKTINFO, &optval, sizeof(optval));
834#ifdef IPV6_RECVPKTINFO
835 if (setsockopt(s, IPPROTO_IPV6, IPV6_RECVPKTINFO, &optval,
836 sizeof(optval)) < 0)
837 warn("setsockopt(IPV6_RECVPKTINFO)"); /* XXX err? */
838#else /* old adv. API */
839 if (setsockopt(s, IPPROTO_IPV6, IPV6_PKTINFO, &optval,
840 sizeof(optval)) < 0)
841 warn("setsockopt(IPV6_PKTINFO)"); /* XXX err? */
683#endif
842#endif
684 setsockopt(s, IPPROTO_IPV6, IPV6_HOPLIMIT, &optval, sizeof(optval));
843#endif /* USE_SIN6_SCOPE_ID */
844#ifdef IPV6_RECVHOPLIMIT
845 if (setsockopt(s, IPPROTO_IPV6, IPV6_RECVHOPLIMIT, &optval,
846 sizeof(optval)) < 0)
847 warn("setsockopt(IPV6_RECVHOPLIMIT)"); /* XXX err? */
848#else /* old adv. API */
849 if (setsockopt(s, IPPROTO_IPV6, IPV6_HOPLIMIT, &optval,
850 sizeof(optval)) < 0)
851 warn("setsockopt(IPV6_HOPLIMIT)"); /* XXX err? */
852#endif
685
686 printf("PING6(%d=40+8+%d bytes) ", datalen + 48, datalen);
853
854 printf("PING6(%d=40+8+%d bytes) ", datalen + 48, datalen);
687 printf("%s --> ", inet_ntop(AF_INET6, &src.sin6_addr, ntop_buf, sizeof(ntop_buf)));
688 printf("%s\n", inet_ntop(AF_INET6, &dst.sin6_addr, ntop_buf, sizeof(ntop_buf)));
855 printf("%s --> ", pr_addr(&src));
856 printf("%s\n", pr_addr(&dst));
689
690 while (preload--) /* Fire off them quickies. */
691 pinger();
692
693 (void)signal(SIGINT, onint);
694 (void)signal(SIGINFO, oninfo);
695
696 if ((options & F_FLOOD) == 0) {

--- 6 unchanged lines hidden (view full) ---

703 }
704
705 FD_ZERO(&fdset);
706 timeout.tv_sec = 0;
707 timeout.tv_usec = 10000;
708 for (;;) {
709 struct msghdr m;
710 struct cmsghdr *cm;
857
858 while (preload--) /* Fire off them quickies. */
859 pinger();
860
861 (void)signal(SIGINT, onint);
862 (void)signal(SIGINFO, oninfo);
863
864 if ((options & F_FLOOD) == 0) {

--- 6 unchanged lines hidden (view full) ---

871 }
872
873 FD_ZERO(&fdset);
874 timeout.tv_sec = 0;
875 timeout.tv_usec = 10000;
876 for (;;) {
877 struct msghdr m;
878 struct cmsghdr *cm;
711 u_char buf[256];
879 u_char buf[1024];
712 struct iovec iov[2];
713
714 if (options & F_FLOOD) {
715 pinger();
716 FD_SET(s, &fdset);
717 if (select(s + 1, &fdset, NULL, NULL, &timeout) < 1)
718 continue;
719 }

--- 24 unchanged lines hidden (view full) ---

744 summary();
745 exit(nreceived == 0);
746}
747
748/*
749 * onalrm --
750 * This routine transmits another ping6.
751 */
880 struct iovec iov[2];
881
882 if (options & F_FLOOD) {
883 pinger();
884 FD_SET(s, &fdset);
885 if (select(s + 1, &fdset, NULL, NULL, &timeout) < 1)
886 continue;
887 }

--- 24 unchanged lines hidden (view full) ---

912 summary();
913 exit(nreceived == 0);
914}
915
916/*
917 * onalrm --
918 * This routine transmits another ping6.
919 */
920/* ARGSUSED */
752void
753onalrm(signo)
754 int signo;
755{
756 struct itimerval itimer;
757
758 if (!npackets || ntransmitted < npackets) {
759 pinger();

--- 31 unchanged lines hidden (view full) ---

791void
792pinger()
793{
794 struct icmp6_hdr *icp;
795 struct iovec iov[2];
796 int i, cc;
797
798 icp = (struct icmp6_hdr *)outpack;
921void
922onalrm(signo)
923 int signo;
924{
925 struct itimerval itimer;
926
927 if (!npackets || ntransmitted < npackets) {
928 pinger();

--- 31 unchanged lines hidden (view full) ---

960void
961pinger()
962{
963 struct icmp6_hdr *icp;
964 struct iovec iov[2];
965 int i, cc;
966
967 icp = (struct icmp6_hdr *)outpack;
968 memset(icp, 0, sizeof(*icp));
799 icp->icmp6_code = 0;
800 icp->icmp6_cksum = 0;
801 icp->icmp6_seq = ntransmitted++; /* htons later */
802 icp->icmp6_id = htons(ident); /* ID */
803
804 CLR(icp->icmp6_seq % mx_dup_ck);
805 icp->icmp6_seq = htons(icp->icmp6_seq);
806
807 if (options & F_FQDN) {
808 icp->icmp6_type = ICMP6_NI_QUERY;
969 icp->icmp6_code = 0;
970 icp->icmp6_cksum = 0;
971 icp->icmp6_seq = ntransmitted++; /* htons later */
972 icp->icmp6_id = htons(ident); /* ID */
973
974 CLR(icp->icmp6_seq % mx_dup_ck);
975 icp->icmp6_seq = htons(icp->icmp6_seq);
976
977 if (options & F_FQDN) {
978 icp->icmp6_type = ICMP6_NI_QUERY;
979 icp->icmp6_code = ICMP6_NI_SUBJ_IPV6;
980 /* XXX: overwrite icmp6_id */
981 ((struct icmp6_nodeinfo *)icp)->ni_qtype = htons(NI_QTYPE_FQDN);
982 ((struct icmp6_nodeinfo *)icp)->ni_flags = htons(0);
983 if (timing)
984 (void)gettimeofday((struct timeval *)
985 &outpack[ICMP6ECHOLEN], NULL);
986 memcpy(&outpack[ICMP6_NIQLEN], &dst.sin6_addr,
987 sizeof(dst.sin6_addr));
988 cc = ICMP6_NIQLEN + sizeof(dst.sin6_addr);
989 datalen = 0;
990 } else if (options & F_FQDNOLD) {
991 /* packet format in 03 draft - no Subject data on queries */
992 icp->icmp6_type = ICMP6_NI_QUERY;
809 /* code field is always 0 */
810 /* XXX: overwrite icmp6_id */
993 /* code field is always 0 */
994 /* XXX: overwrite icmp6_id */
811 icp->icmp6_data16[0] = htons(NI_QTYPE_FQDN);
995 ((struct icmp6_nodeinfo *)icp)->ni_qtype = htons(NI_QTYPE_FQDN);
996 ((struct icmp6_nodeinfo *)icp)->ni_flags = htons(0);
812 if (timing)
813 (void)gettimeofday((struct timeval *)
814 &outpack[ICMP6ECHOLEN], NULL);
815 cc = ICMP6_NIQLEN;
816 datalen = 0;
817 } else if (options & F_NODEADDR) {
818 icp->icmp6_type = ICMP6_NI_QUERY;
997 if (timing)
998 (void)gettimeofday((struct timeval *)
999 &outpack[ICMP6ECHOLEN], NULL);
1000 cc = ICMP6_NIQLEN;
1001 datalen = 0;
1002 } else if (options & F_NODEADDR) {
1003 icp->icmp6_type = ICMP6_NI_QUERY;
819 /* code field is always 0 */
1004 icp->icmp6_code = ICMP6_NI_SUBJ_IPV6;
820 /* XXX: overwrite icmp6_id */
1005 /* XXX: overwrite icmp6_id */
821 icp->icmp6_data16[0] = htons(NI_QTYPE_NODEADDR);
1006 ((struct icmp6_nodeinfo *)icp)->ni_qtype =
1007 htons(NI_QTYPE_NODEADDR);
1008 ((struct icmp6_nodeinfo *)icp)->ni_flags = htons(0);
822 if (timing)
823 (void)gettimeofday((struct timeval *)
824 &outpack[ICMP6ECHOLEN], NULL);
1009 if (timing)
1010 (void)gettimeofday((struct timeval *)
1011 &outpack[ICMP6ECHOLEN], NULL);
825 cc = ICMP6_NIQLEN;
1012 memcpy(&outpack[ICMP6_NIQLEN], &dst.sin6_addr,
1013 sizeof(dst.sin6_addr));
1014 cc = ICMP6_NIQLEN + sizeof(dst.sin6_addr);
826 datalen = 0;
827 ((struct icmp6_nodeinfo *)icp)->ni_flags = naflags;
828 }
829 else {
830 icp->icmp6_type = ICMP6_ECHO_REQUEST;
831 if (timing)
832 (void)gettimeofday((struct timeval *)
833 &outpack[ICMP6ECHOLEN], NULL);

--- 28 unchanged lines hidden (view full) ---

862 * program to be run without having intermingled output (or statistics!).
863 */
864void
865pr_pack(buf, cc, mhdr)
866 u_char *buf;
867 int cc;
868 struct msghdr *mhdr;
869{
1015 datalen = 0;
1016 ((struct icmp6_nodeinfo *)icp)->ni_flags = naflags;
1017 }
1018 else {
1019 icp->icmp6_type = ICMP6_ECHO_REQUEST;
1020 if (timing)
1021 (void)gettimeofday((struct timeval *)
1022 &outpack[ICMP6ECHOLEN], NULL);

--- 28 unchanged lines hidden (view full) ---

1051 * program to be run without having intermingled output (or statistics!).
1052 */
1053void
1054pr_pack(buf, cc, mhdr)
1055 u_char *buf;
1056 int cc;
1057 struct msghdr *mhdr;
1058{
1059#define safeputc(c) printf((isprint((c)) ? "%c" : "\\%03o"), c)
870 struct icmp6_hdr *icp;
871 int i;
872 int hoplim;
873 struct sockaddr_in6 *from = (struct sockaddr_in6 *)mhdr->msg_name;
874 u_char *cp = NULL, *dp, *end = buf + cc;
1060 struct icmp6_hdr *icp;
1061 int i;
1062 int hoplim;
1063 struct sockaddr_in6 *from = (struct sockaddr_in6 *)mhdr->msg_name;
1064 u_char *cp = NULL, *dp, *end = buf + cc;
875#ifdef OLD_RAWSOCKET
876 struct ip6_hdr *ip;
877#endif
1065 struct in6_pktinfo *pktinfo = NULL;
878 struct timeval tv, *tp;
879 double triptime = 0;
880 int dupflag;
881 size_t off;
1066 struct timeval tv, *tp;
1067 double triptime = 0;
1068 int dupflag;
1069 size_t off;
1070 int oldfqdn;
882
883 (void)gettimeofday(&tv, NULL);
884
1071
1072 (void)gettimeofday(&tv, NULL);
1073
885#ifdef OLD_RAWSOCKET
886 /* Check the IP header */
887 ip = (struct ip6_hdr *)buf;
888 if (cc < sizeof(struct icmp6_hdr) + sizeof(struct ip6_hdr)) {
889 if (options & F_VERBOSE)
890 warnx("packet too short (%d bytes) from %s\n", cc,
891 inet_ntop(AF_INET6, (void *)&from->sin6_addr,
892 ntop_buf, sizeof(ntop_buf)));
893 return;
894 }
895
896 /* chase nexthdr link */
897 {
898 u_int8_t nh;
899 struct ah *ah;
900 struct ip6_ext *ip6e;
901
902 off = IP6LEN;
903 nh = ip->ip6_nxt;
904 while (nh != IPPROTO_ICMPV6) {
905 if (options & F_VERBOSE)
906 fprintf(stderr, "header chain: type=0x%x\n", nh);
907
908 switch (nh) {
909#ifdef IPSEC
910 case IPPROTO_AH:
911 ah = (struct ah *)(buf + off);
912 off += sizeof(struct ah);
913 off += (ah->ah_len << 2);
914 nh = ah->ah_nxt;
915 break;
916#endif
917
918 case IPPROTO_HOPOPTS:
919 ip6e = (struct ip6_ext *)(buf + off);
920 off += (ip6e->ip6e_len + 1) << 3;
921 nh = ip6e->ip6e_nxt;
922 break;
923 default:
924 if (options & F_VERBOSE) {
925 fprintf(stderr,
926 "unknown header type=0x%x: drop it\n",
927 nh);
928 }
929 return;
930 }
931 }
932 }
933 /* Now the ICMP part */
934 icp = (struct icmp6_hdr *)(buf + off);
935#else
936 if (cc < sizeof(struct icmp6_hdr)) {
937 if (options & F_VERBOSE)
938 warnx("packet too short (%d bytes) from %s\n", cc,
1074 if (cc < sizeof(struct icmp6_hdr)) {
1075 if (options & F_VERBOSE)
1076 warnx("packet too short (%d bytes) from %s\n", cc,
939 inet_ntop(AF_INET6, (void *)&from->sin6_addr,
940 ntop_buf, sizeof(ntop_buf)));
1077 pr_addr(from));
941 return;
942 }
943 icp = (struct icmp6_hdr *)buf;
944 off = 0;
1078 return;
1079 }
1080 icp = (struct icmp6_hdr *)buf;
1081 off = 0;
945#endif
946
947 if ((hoplim = get_hoplim(mhdr)) == -1) {
948 warnx("failed to get receiving hop limit");
949 return;
950 }
1082
1083 if ((hoplim = get_hoplim(mhdr)) == -1) {
1084 warnx("failed to get receiving hop limit");
1085 return;
1086 }
1087 if ((pktinfo = get_rcvpktinfo(mhdr)) == NULL) {
1088 warnx("failed to get receiving pakcet information");
1089 return;
1090 }
951
952 if (icp->icmp6_type == ICMP6_ECHO_REPLY) {
953 /* XXX the following line overwrites the original packet */
954 icp->icmp6_seq = ntohs(icp->icmp6_seq);
955 if (ntohs(icp->icmp6_id) != ident)
956 return; /* It was not our ECHO */
957 ++nreceived;
958 if (timing) {

--- 19 unchanged lines hidden (view full) ---

978
979 if (options & F_QUIET)
980 return;
981
982 if (options & F_FLOOD)
983 (void)write(STDOUT_FILENO, &BSPACE, 1);
984 else {
985 (void)printf("%d bytes from %s, icmp_seq=%u", cc,
1091
1092 if (icp->icmp6_type == ICMP6_ECHO_REPLY) {
1093 /* XXX the following line overwrites the original packet */
1094 icp->icmp6_seq = ntohs(icp->icmp6_seq);
1095 if (ntohs(icp->icmp6_id) != ident)
1096 return; /* It was not our ECHO */
1097 ++nreceived;
1098 if (timing) {

--- 19 unchanged lines hidden (view full) ---

1118
1119 if (options & F_QUIET)
1120 return;
1121
1122 if (options & F_FLOOD)
1123 (void)write(STDOUT_FILENO, &BSPACE, 1);
1124 else {
1125 (void)printf("%d bytes from %s, icmp_seq=%u", cc,
986 inet_ntop(AF_INET6, &from->sin6_addr,
987 ntop_buf, sizeof(ntop_buf)),
1126 pr_addr(from),
988 icp->icmp6_seq);
989 (void)printf(" hlim=%d", hoplim);
1127 icp->icmp6_seq);
1128 (void)printf(" hlim=%d", hoplim);
1129 if ((options & F_VERBOSE) != 0) {
1130 struct sockaddr_in6 dstsa;
1131
1132 memset(&dstsa, 0, sizeof(dstsa));
1133 dstsa.sin6_family = AF_INET6;
1134 dstsa.sin6_len = sizeof(dstsa);
1135 dstsa.sin6_scope_id = pktinfo->ipi6_ifindex;
1136 dstsa.sin6_addr = pktinfo->ipi6_addr;
1137 (void)printf(" dst=%s", pr_addr(&dstsa));
1138 }
990 if (timing)
991 (void)printf(" time=%g ms", triptime);
992 if (dupflag)
993 (void)printf("(DUP!)");
994 /* check the data */
995 cp = buf + off + ICMP6ECHOLEN + ICMP6ECHOTMLEN;
996 dp = outpack + ICMP6ECHOLEN + ICMP6ECHOTMLEN;
997 for (i = 8; cp < end; ++i, ++cp, ++dp) {

--- 16 unchanged lines hidden (view full) ---

1014 case NI_QTYPE_SUPTYPES:
1015 printf("NodeInfo Supported Qtypes");
1016 break;
1017 case NI_QTYPE_NODEADDR:
1018 pr_nodeaddr(ni, end - (u_char *)ni);
1019 break;
1020 case NI_QTYPE_FQDN:
1021 default: /* XXX: for backward compatibility */
1139 if (timing)
1140 (void)printf(" time=%g ms", triptime);
1141 if (dupflag)
1142 (void)printf("(DUP!)");
1143 /* check the data */
1144 cp = buf + off + ICMP6ECHOLEN + ICMP6ECHOTMLEN;
1145 dp = outpack + ICMP6ECHOLEN + ICMP6ECHOTMLEN;
1146 for (i = 8; cp < end; ++i, ++cp, ++dp) {

--- 16 unchanged lines hidden (view full) ---

1163 case NI_QTYPE_SUPTYPES:
1164 printf("NodeInfo Supported Qtypes");
1165 break;
1166 case NI_QTYPE_NODEADDR:
1167 pr_nodeaddr(ni, end - (u_char *)ni);
1168 break;
1169 case NI_QTYPE_FQDN:
1170 default: /* XXX: for backward compatibility */
1022 cp = (u_char *)ni + ICMP6_NIRLEN + 1;
1023 while (cp < end) {
1024 if (isprint(*cp))
1025 putchar(*cp);
1026 else
1027 printf("\\%03o", *cp & 0xff);
1028 cp++;
1029 }
1030 if (options & F_VERBOSE) {
1031 long ttl;
1171 cp = (u_char *)ni + ICMP6_NIRLEN;
1172 if (buf[off + ICMP6_NIRLEN] ==
1173 cc - off - ICMP6_NIRLEN - 1)
1174 oldfqdn = 1;
1175 else
1176 oldfqdn = 0;
1177 if (oldfqdn) {
1178 cp++;
1179 while (cp < end) {
1180 safeputc(*cp & 0xff);
1181 cp++;
1182 }
1183 } else {
1184 while (cp < end) {
1185 i = *cp++;
1186 if (i) {
1187 if (i > end - cp) {
1188 printf("???");
1189 break;
1190 }
1191 while (i-- && cp < end) {
1192 safeputc(*cp & 0xff);
1193 cp++;
1194 }
1195 if (cp + 1 < end && *cp)
1196 printf(".");
1197 } else {
1198 if (cp == end) {
1199 /* FQDN */
1200 printf(".");
1201 } else if (cp + 1 == end &&
1202 *cp == '\0') {
1203 /* truncated */
1204 } else {
1205 /* invalid */
1206 printf("???");
1207 }
1208 break;
1209 }
1210 }
1211 }
1212 if (options & F_VERBOSE) {
1213 int32_t ttl;
1214 int comma = 0;
1032
1215
1033 (void)printf(" (");
1216 (void)printf(" ("); /*)*/
1034
1217
1035 switch(ni->ni_code) {
1036 case ICMP6_NI_REFUSED:
1037 (void)printf("refused,");
1038 break;
1039 case ICMP6_NI_UNKNOWN:
1040 (void)printf("unknwon qtype,");
1041 break;
1042 }
1218 switch(ni->ni_code) {
1219 case ICMP6_NI_REFUSED:
1220 (void)printf("refused");
1221 comma++;
1222 break;
1223 case ICMP6_NI_UNKNOWN:
1224 (void)printf("unknwon qtype");
1225 comma++;
1226 break;
1227 }
1043
1228
1044 if ((end - (u_char *)ni) < ICMP6_NIRLEN) {
1045 /* case of refusion, unkown */
1046 goto fqdnend;
1047 }
1048 ttl = ntohl(*(u_long *)&buf[off+ICMP6ECHOLEN+8]);
1049 if (!(ni->ni_flags & NI_FQDN_FLAG_VALIDTTL))
1050 (void)printf("TTL=%d:meaningless",
1051 (int)ttl);
1052 else {
1053 if (ttl < 0)
1054 (void)printf("TTL=%d:invalid",
1055 (int)ttl);
1056 else
1057 (void)printf("TTL=%d",
1058 (int)ttl);
1059 }
1229 if ((end - (u_char *)ni) < ICMP6_NIRLEN) {
1230 /* case of refusion, unkown */
1231 goto fqdnend;
1232 }
1233 ttl = (int32_t)ntohl(*(u_long *)&buf[off+ICMP6ECHOLEN+8]);
1234 if (comma)
1235 printf(",");
1236 if (!(ni->ni_flags & NI_FQDN_FLAG_VALIDTTL))
1237 (void)printf("TTL=%d:meaningless",
1238 (int)ttl);
1239 else {
1240 if (ttl < 0) {
1241 (void)printf("TTL=%d:invalid",
1242 ttl);
1243 } else
1244 (void)printf("TTL=%d", ttl);
1245 }
1246 comma++;
1060
1247
1061 if (buf[off + ICMP6_NIRLEN] !=
1062 cc - off - ICMP6_NIRLEN - 1) {
1063 (void)printf(",invalid namelen:%d/%lu",
1064 buf[off + ICMP6_NIRLEN],
1065 (u_long)cc - off - ICMP6_NIRLEN - 1);
1066 }
1067 putchar(')');
1068 }
1069 fqdnend:
1070 ;
1248 if (oldfqdn) {
1249 if (comma)
1250 printf(",");
1251 printf("03 draft");
1252 comma++;
1253 } else {
1254 cp = (u_char *)ni + ICMP6_NIRLEN;
1255 if (cp == end) {
1256 if (comma)
1257 printf(",");
1258 printf("no name");
1259 comma++;
1260 }
1261 }
1262
1263 if (buf[off + ICMP6_NIRLEN] !=
1264 cc - off - ICMP6_NIRLEN - 1 && oldfqdn) {
1265 if (comma)
1266 printf(",");
1267 (void)printf("invalid namelen:%d/%lu",
1268 buf[off + ICMP6_NIRLEN],
1269 (u_long)cc - off - ICMP6_NIRLEN - 1);
1270 comma++;
1271 }
1272 /*(*/
1273 putchar(')');
1274 }
1275 fqdnend:
1276 ;
1071 }
1072 } else {
1073 /* We've got something other than an ECHOREPLY */
1074 if (!(options & F_VERBOSE))
1075 return;
1076 (void)printf("%d bytes from %s: ", cc,
1077 pr_addr(from));
1078 pr_icmph(icp, end);
1079 }
1080
1081 if (!(options & F_FLOOD)) {
1082 (void)putchar('\n');
1277 }
1278 } else {
1279 /* We've got something other than an ECHOREPLY */
1280 if (!(options & F_VERBOSE))
1281 return;
1282 (void)printf("%d bytes from %s: ", cc,
1283 pr_addr(from));
1284 pr_icmph(icp, end);
1285 }
1286
1287 if (!(options & F_FLOOD)) {
1288 (void)putchar('\n');
1289 if (options & F_VERBOSE)
1290 pr_exthdrs(mhdr);
1083 (void)fflush(stdout);
1084 }
1291 (void)fflush(stdout);
1292 }
1293#undef safeputc
1085}
1086
1087void
1294}
1295
1296void
1297pr_exthdrs(mhdr)
1298 struct msghdr *mhdr;
1299{
1300 struct cmsghdr *cm;
1301
1302 for (cm = (struct cmsghdr *)CMSG_FIRSTHDR(mhdr); cm;
1303 cm = (struct cmsghdr *)CMSG_NXTHDR(mhdr, cm)) {
1304 if (cm->cmsg_level != IPPROTO_IPV6)
1305 continue;
1306
1307 switch(cm->cmsg_type) {
1308 case IPV6_HOPOPTS:
1309 printf(" HbH Options: ");
1310 pr_ip6opt(CMSG_DATA(cm));
1311 break;
1312 case IPV6_DSTOPTS:
1313#ifdef IPV6_RTHDRDSTOPTS
1314 case IPV6_RTHDRDSTOPTS:
1315#endif
1316 printf(" Dst Options: ");
1317 pr_ip6opt(CMSG_DATA(cm));
1318 break;
1319 case IPV6_RTHDR:
1320 printf(" Routing: ");
1321 pr_rthdr(CMSG_DATA(cm));
1322 break;
1323 }
1324 }
1325}
1326
1327#ifdef USE_RFC2292BIS
1328void
1329pr_ip6opt(void *extbuf)
1330{
1331 struct ip6_hbh *ext;
1332 int currentlen;
1333 u_int8_t type;
1334 size_t extlen, len;
1335 void *databuf;
1336 size_t offset;
1337 u_int16_t value2;
1338 u_int32_t value4;
1339
1340 ext = (struct ip6_hbh *)extbuf;
1341 extlen = (ext->ip6h_len + 1) * 8;
1342 printf("nxt %u, len %u (%d bytes)\n", ext->ip6h_nxt,
1343 ext->ip6h_len, extlen);
1344
1345 currentlen = 0;
1346 while (1) {
1347 currentlen = inet6_opt_next(extbuf, extlen, currentlen,
1348 &type, &len, &databuf);
1349 if (currentlen == -1)
1350 break;
1351 switch (type) {
1352 /*
1353 * Note that inet6_opt_next automatically skips any padding
1354 * optins.
1355 */
1356 case IP6OPT_JUMBO:
1357 offset = 0;
1358 offset = inet6_opt_get_val(databuf, offset,
1359 &value4, sizeof(value4));
1360 printf(" Jumbo Payload Opt: Length %u\n",
1361 (unsigned int)ntohl(value4));
1362 break;
1363 case IP6OPT_ROUTER_ALERT:
1364 offset = 0;
1365 offset = inet6_opt_get_val(databuf, offset,
1366 &value2, sizeof(value2));
1367 printf(" Router Alert Opt: Type %u\n",
1368 ntohs(value2));
1369 break;
1370 default:
1371 printf(" Received Opt %u len %u\n", type, len);
1372 break;
1373 }
1374 }
1375 return;
1376}
1377#else /* !USE_RFC2292BIS */
1378/* ARGSUSED */
1379void
1380pr_ip6opt(void *extbuf)
1381{
1382 putchar('\n');
1383 return;
1384}
1385#endif /* USE_RFC2292BIS */
1386
1387#ifdef USE_RFC2292BIS
1388void
1389pr_rthdr(void *extbuf)
1390{
1391 struct in6_addr *in6;
1392 char ntopbuf[INET6_ADDRSTRLEN];
1393 struct ip6_rthdr *rh = (struct ip6_rthdr *)extbuf;
1394 int i, segments;
1395
1396 /* print fixed part of the header */
1397 printf("nxt %u, len %u (%d bytes), type %u, ", rh->ip6r_nxt,
1398 rh->ip6r_len, (rh->ip6r_len + 1) << 3, rh->ip6r_type);
1399 if ((segments = inet6_rth_segments(extbuf)) >= 0)
1400 printf("%d segments, ", segments);
1401 else
1402 printf("segments unknown, ");
1403 printf("%d left\n", rh->ip6r_segleft);
1404
1405 for (i = 0; i < segments; i++) {
1406 in6 = inet6_rth_getaddr(extbuf, i);
1407 if (in6 == NULL)
1408 printf(" [%d]<NULL>\n", i);
1409 else
1410 printf(" [%d]%s\n", i,
1411 inet_ntop(AF_INET6, in6,
1412 ntopbuf, sizeof(ntopbuf)));
1413 }
1414
1415 return;
1416
1417}
1418#else /* !USE_RFC2292BIS */
1419/* ARGSUSED */
1420void
1421pr_rthdr(void *extbuf)
1422{
1423 putchar('\n');
1424 return;
1425}
1426#endif /* USE_RFC2292BIS */
1427
1428
1429void
1088pr_nodeaddr(ni, nilen)
1089 struct icmp6_nodeinfo *ni; /* ni->qtype must be NODEADDR */
1090 int nilen;
1091{
1092 struct in6_addr *ia6 = (struct in6_addr *)(ni + 1);
1430pr_nodeaddr(ni, nilen)
1431 struct icmp6_nodeinfo *ni; /* ni->qtype must be NODEADDR */
1432 int nilen;
1433{
1434 struct in6_addr *ia6 = (struct in6_addr *)(ni + 1);
1435 char ntop_buf[INET6_ADDRSTRLEN];
1093
1094 nilen -= sizeof(struct icmp6_nodeinfo);
1095
1096 if (options & F_VERBOSE) {
1097 switch(ni->ni_code) {
1098 case ICMP6_NI_REFUSED:
1099 (void)printf("refused");
1100 break;

--- 25 unchanged lines hidden (view full) ---

1126 cm->cmsg_type == IPV6_HOPLIMIT &&
1127 cm->cmsg_len == CMSG_LEN(sizeof(int)))
1128 return(*(int *)CMSG_DATA(cm));
1129 }
1130
1131 return(-1);
1132}
1133
1436
1437 nilen -= sizeof(struct icmp6_nodeinfo);
1438
1439 if (options & F_VERBOSE) {
1440 switch(ni->ni_code) {
1441 case ICMP6_NI_REFUSED:
1442 (void)printf("refused");
1443 break;

--- 25 unchanged lines hidden (view full) ---

1469 cm->cmsg_type == IPV6_HOPLIMIT &&
1470 cm->cmsg_len == CMSG_LEN(sizeof(int)))
1471 return(*(int *)CMSG_DATA(cm));
1472 }
1473
1474 return(-1);
1475}
1476
1477struct in6_pktinfo *
1478get_rcvpktinfo(mhdr)
1479 struct msghdr *mhdr;
1480{
1481 struct cmsghdr *cm;
1482
1483 for (cm = (struct cmsghdr *)CMSG_FIRSTHDR(mhdr); cm;
1484 cm = (struct cmsghdr *)CMSG_NXTHDR(mhdr, cm)) {
1485 if (cm->cmsg_level == IPPROTO_IPV6 &&
1486 cm->cmsg_type == IPV6_PKTINFO &&
1487 cm->cmsg_len == CMSG_LEN(sizeof(struct in6_pktinfo)))
1488 return((struct in6_pktinfo *)CMSG_DATA(cm));
1489 }
1490
1491 return(NULL);
1492}
1493
1134/*
1135 * tvsub --
1136 * Subtract 2 timeval structs: out = out - in. Out is assumed to
1137 * be >= in.
1138 */
1139void
1140tvsub(out, in)
1141 register struct timeval *out, *in;

--- 4 unchanged lines hidden (view full) ---

1146 }
1147 out->tv_sec -= in->tv_sec;
1148}
1149
1150/*
1151 * oninfo --
1152 * SIGINFO handler.
1153 */
1494/*
1495 * tvsub --
1496 * Subtract 2 timeval structs: out = out - in. Out is assumed to
1497 * be >= in.
1498 */
1499void
1500tvsub(out, in)
1501 register struct timeval *out, *in;

--- 4 unchanged lines hidden (view full) ---

1506 }
1507 out->tv_sec -= in->tv_sec;
1508}
1509
1510/*
1511 * oninfo --
1512 * SIGINFO handler.
1513 */
1514/* ARGSUSED */
1154void
1155oninfo(notused)
1156 int notused;
1157{
1158 summary();
1159}
1160
1161/*
1162 * onint --
1163 * SIGINT handler.
1164 */
1515void
1516oninfo(notused)
1517 int notused;
1518{
1519 summary();
1520}
1521
1522/*
1523 * onint --
1524 * SIGINT handler.
1525 */
1526/* ARGSUSED */
1165void
1166onint(notused)
1167 int notused;
1168{
1169 summary();
1170
1171 (void)signal(SIGINT, SIG_DFL);
1172 (void)kill(getpid(), SIGINT);

--- 54 unchanged lines hidden (view full) ---

1227 * pr_icmph --
1228 * Print a descriptive string about an ICMP header.
1229 */
1230void
1231pr_icmph(icp, end)
1232 struct icmp6_hdr *icp;
1233 u_char *end;
1234{
1527void
1528onint(notused)
1529 int notused;
1530{
1531 summary();
1532
1533 (void)signal(SIGINT, SIG_DFL);
1534 (void)kill(getpid(), SIGINT);

--- 54 unchanged lines hidden (view full) ---

1589 * pr_icmph --
1590 * Print a descriptive string about an ICMP header.
1591 */
1592void
1593pr_icmph(icp, end)
1594 struct icmp6_hdr *icp;
1595 u_char *end;
1596{
1597 char ntop_buf[INET6_ADDRSTRLEN];
1598
1235 switch(icp->icmp6_type) {
1236 case ICMP6_DST_UNREACH:
1237 switch(icp->icmp6_code) {
1238 case ICMP6_DST_UNREACH_NOROUTE:
1239 (void)printf("No Route to Destination\n");
1240 break;
1241 case ICMP6_DST_UNREACH_ADMIN:
1242 (void)printf("Destination Administratively "

--- 14 unchanged lines hidden (view full) ---

1257 break;
1258 }
1259 /* Print returned IP header information */
1260 pr_retip((struct ip6_hdr *)(icp + 1), end);
1261 break;
1262 case ICMP6_PACKET_TOO_BIG:
1263 (void)printf("Packet too big mtu = %d\n",
1264 (int)ntohl(icp->icmp6_mtu));
1599 switch(icp->icmp6_type) {
1600 case ICMP6_DST_UNREACH:
1601 switch(icp->icmp6_code) {
1602 case ICMP6_DST_UNREACH_NOROUTE:
1603 (void)printf("No Route to Destination\n");
1604 break;
1605 case ICMP6_DST_UNREACH_ADMIN:
1606 (void)printf("Destination Administratively "

--- 14 unchanged lines hidden (view full) ---

1621 break;
1622 }
1623 /* Print returned IP header information */
1624 pr_retip((struct ip6_hdr *)(icp + 1), end);
1625 break;
1626 case ICMP6_PACKET_TOO_BIG:
1627 (void)printf("Packet too big mtu = %d\n",
1628 (int)ntohl(icp->icmp6_mtu));
1629 pr_retip((struct ip6_hdr *)(icp + 1), end);
1265 break;
1266 case ICMP6_TIME_EXCEEDED:
1267 switch(icp->icmp6_code) {
1268 case ICMP6_TIME_EXCEED_TRANSIT:
1269 (void)printf("Time to live exceeded\n");
1270 break;
1271 case ICMP6_TIME_EXCEED_REASSEMBLY:
1272 (void)printf("Frag reassembly time exceeded\n");

--- 21 unchanged lines hidden (view full) ---

1294 (void)printf("Bad code(%d) ", icp->icmp6_code);
1295 break;
1296 }
1297 (void)printf("pointer = 0x%02x\n",
1298 (int)ntohl(icp->icmp6_pptr));
1299 pr_retip((struct ip6_hdr *)(icp + 1), end);
1300 break;
1301 case ICMP6_ECHO_REQUEST:
1630 break;
1631 case ICMP6_TIME_EXCEEDED:
1632 switch(icp->icmp6_code) {
1633 case ICMP6_TIME_EXCEED_TRANSIT:
1634 (void)printf("Time to live exceeded\n");
1635 break;
1636 case ICMP6_TIME_EXCEED_REASSEMBLY:
1637 (void)printf("Frag reassembly time exceeded\n");

--- 21 unchanged lines hidden (view full) ---

1659 (void)printf("Bad code(%d) ", icp->icmp6_code);
1660 break;
1661 }
1662 (void)printf("pointer = 0x%02x\n",
1663 (int)ntohl(icp->icmp6_pptr));
1664 pr_retip((struct ip6_hdr *)(icp + 1), end);
1665 break;
1666 case ICMP6_ECHO_REQUEST:
1302 (void)printf("Echo Request\n");
1667 (void)printf("Echo Request");
1303 /* XXX ID + Seq + Data */
1304 break;
1305 case ICMP6_ECHO_REPLY:
1668 /* XXX ID + Seq + Data */
1669 break;
1670 case ICMP6_ECHO_REPLY:
1306 (void)printf("Echo Reply\n");
1671 (void)printf("Echo Reply");
1307 /* XXX ID + Seq + Data */
1308 break;
1309 case ICMP6_MEMBERSHIP_QUERY:
1672 /* XXX ID + Seq + Data */
1673 break;
1674 case ICMP6_MEMBERSHIP_QUERY:
1310 (void)printf("Membership Query\n");
1675 (void)printf("Listener Query");
1311 break;
1312 case ICMP6_MEMBERSHIP_REPORT:
1676 break;
1677 case ICMP6_MEMBERSHIP_REPORT:
1313 (void)printf("Membership Report\n");
1678 (void)printf("Listener Report");
1314 break;
1315 case ICMP6_MEMBERSHIP_REDUCTION:
1679 break;
1680 case ICMP6_MEMBERSHIP_REDUCTION:
1316 (void)printf("Membership Reduction\n");
1681 (void)printf("Listener Done");
1317 break;
1318 case ND_ROUTER_SOLICIT:
1682 break;
1683 case ND_ROUTER_SOLICIT:
1319 (void)printf("Router Solicitation\n");
1684 (void)printf("Router Solicitation");
1320 break;
1321 case ND_ROUTER_ADVERT:
1685 break;
1686 case ND_ROUTER_ADVERT:
1322 (void)printf("Router Advertisement\n");
1687 (void)printf("Router Advertisement");
1323 break;
1324 case ND_NEIGHBOR_SOLICIT:
1688 break;
1689 case ND_NEIGHBOR_SOLICIT:
1325 (void)printf("Neighbor Solicitation\n");
1690 (void)printf("Neighbor Solicitation");
1326 break;
1327 case ND_NEIGHBOR_ADVERT:
1691 break;
1692 case ND_NEIGHBOR_ADVERT:
1328 (void)printf("Neighbor Advertisement\n");
1693 (void)printf("Neighbor Advertisement");
1329 break;
1330 case ND_REDIRECT:
1331 {
1332 struct nd_redirect *red = (struct nd_redirect *)icp;
1333
1334 (void)printf("Redirect\n");
1694 break;
1695 case ND_REDIRECT:
1696 {
1697 struct nd_redirect *red = (struct nd_redirect *)icp;
1698
1699 (void)printf("Redirect\n");
1335 (void)printf("Destination: %s\n",
1700 (void)printf("Destination: %s",
1336 inet_ntop(AF_INET6, &red->nd_rd_dst,
1337 ntop_buf, sizeof(ntop_buf)));
1701 inet_ntop(AF_INET6, &red->nd_rd_dst,
1702 ntop_buf, sizeof(ntop_buf)));
1338 (void)printf("New Target: %s\n",
1703 (void)printf("New Target: %s",
1339 inet_ntop(AF_INET6, &red->nd_rd_target,
1340 ntop_buf, sizeof(ntop_buf)));
1341 break;
1342 }
1343 case ICMP6_NI_QUERY:
1704 inet_ntop(AF_INET6, &red->nd_rd_target,
1705 ntop_buf, sizeof(ntop_buf)));
1706 break;
1707 }
1708 case ICMP6_NI_QUERY:
1344 (void)printf("Node Information Query\n");
1709 (void)printf("Node Information Query");
1345 /* XXX ID + Seq + Data */
1346 break;
1347 case ICMP6_NI_REPLY:
1710 /* XXX ID + Seq + Data */
1711 break;
1712 case ICMP6_NI_REPLY:
1348 (void)printf("Node Information Reply\n");
1713 (void)printf("Node Information Reply");
1349 /* XXX ID + Seq + Data */
1350 break;
1351 default:
1714 /* XXX ID + Seq + Data */
1715 break;
1716 default:
1352 (void)printf("Bad ICMP type: %d\n", icp->icmp6_type);
1717 (void)printf("Bad ICMP type: %d", icp->icmp6_type);
1353 }
1354}
1355
1356/*
1357 * pr_iph --
1358 * Print an IP6 header.
1359 */
1360void
1361pr_iph(ip6)
1362 struct ip6_hdr *ip6;
1363{
1364 u_int32_t flow = ip6->ip6_flow & IPV6_FLOWLABEL_MASK;
1365 u_int8_t tc;
1718 }
1719}
1720
1721/*
1722 * pr_iph --
1723 * Print an IP6 header.
1724 */
1725void
1726pr_iph(ip6)
1727 struct ip6_hdr *ip6;
1728{
1729 u_int32_t flow = ip6->ip6_flow & IPV6_FLOWLABEL_MASK;
1730 u_int8_t tc;
1731 char ntop_buf[INET6_ADDRSTRLEN];
1366
1367 tc = *(&ip6->ip6_vfc + 1); /* XXX */
1368 tc = (tc >> 4) & 0x0f;
1369 tc |= (ip6->ip6_vfc << 4);
1370
1371 printf("Vr TC Flow Plen Nxt Hlim\n");
1372 printf(" %1x %02x %05x %04x %02x %02x\n",
1373 (ip6->ip6_vfc & IPV6_VERSION_MASK) >> 4, tc, (int)ntohl(flow),
1374 ntohs(ip6->ip6_plen),
1375 ip6->ip6_nxt, ip6->ip6_hlim);
1376 printf("%s->", inet_ntop(AF_INET6, &ip6->ip6_src,
1732
1733 tc = *(&ip6->ip6_vfc + 1); /* XXX */
1734 tc = (tc >> 4) & 0x0f;
1735 tc |= (ip6->ip6_vfc << 4);
1736
1737 printf("Vr TC Flow Plen Nxt Hlim\n");
1738 printf(" %1x %02x %05x %04x %02x %02x\n",
1739 (ip6->ip6_vfc & IPV6_VERSION_MASK) >> 4, tc, (int)ntohl(flow),
1740 ntohs(ip6->ip6_plen),
1741 ip6->ip6_nxt, ip6->ip6_hlim);
1742 printf("%s->", inet_ntop(AF_INET6, &ip6->ip6_src,
1377 ntop_buf, INET6_ADDRSTRLEN));
1743 ntop_buf, sizeof(ntop_buf)));
1378 printf("%s\n", inet_ntop(AF_INET6, &ip6->ip6_dst,
1744 printf("%s\n", inet_ntop(AF_INET6, &ip6->ip6_dst,
1379 ntop_buf, INET6_ADDRSTRLEN));
1745 ntop_buf, sizeof(ntop_buf)));
1380}
1381
1382/*
1383 * pr_addr --
1384 * Return an ascii host address as a dotted quad and optionally with
1385 * a hostname.
1386 */
1746}
1747
1748/*
1749 * pr_addr --
1750 * Return an ascii host address as a dotted quad and optionally with
1751 * a hostname.
1752 */
1387char *
1753const char *
1388pr_addr(addr)
1389 struct sockaddr_in6 *addr;
1390{
1391 static char buf[MAXHOSTNAMELEN];
1392 int flag = 0;
1393
1754pr_addr(addr)
1755 struct sockaddr_in6 *addr;
1756{
1757 static char buf[MAXHOSTNAMELEN];
1758 int flag = 0;
1759
1394 if (options & F_NUMERIC)
1760 if ((options & F_HOSTNAME) == 0)
1395 flag |= NI_NUMERICHOST;
1761 flag |= NI_NUMERICHOST;
1396
1762#ifdef KAME_SCOPEID
1397 flag |= NI_WITHSCOPEID;
1763 flag |= NI_WITHSCOPEID;
1764#endif
1398
1399 getnameinfo((struct sockaddr *)addr, addr->sin6_len, buf, sizeof(buf),
1400 NULL, 0, flag);
1401
1402 return (buf);
1403}
1404
1405/*

--- 94 unchanged lines hidden (view full) ---

1500 if (!isxdigit(*cp))
1501 errx(1, "patterns must be specified as hex digits");
1502 ii = sscanf(patp,
1503 "%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x",
1504 &pat[0], &pat[1], &pat[2], &pat[3], &pat[4], &pat[5], &pat[6],
1505 &pat[7], &pat[8], &pat[9], &pat[10], &pat[11], &pat[12],
1506 &pat[13], &pat[14], &pat[15]);
1507
1765
1766 getnameinfo((struct sockaddr *)addr, addr->sin6_len, buf, sizeof(buf),
1767 NULL, 0, flag);
1768
1769 return (buf);
1770}
1771
1772/*

--- 94 unchanged lines hidden (view full) ---

1867 if (!isxdigit(*cp))
1868 errx(1, "patterns must be specified as hex digits");
1869 ii = sscanf(patp,
1870 "%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x",
1871 &pat[0], &pat[1], &pat[2], &pat[3], &pat[4], &pat[5], &pat[6],
1872 &pat[7], &pat[8], &pat[9], &pat[10], &pat[11], &pat[12],
1873 &pat[13], &pat[14], &pat[15]);
1874
1508/* xxx */
1875/* xxx */
1509 if (ii > 0)
1510 for (kk = 0;
1511 kk <= MAXDATALEN - (8 + sizeof(struct timeval) + ii);
1512 kk += ii)
1513 for (jj = 0; jj < ii; ++jj)
1514 bp[jj + kk] = pat[jj];
1515 if (!(options & F_QUIET)) {
1516 (void)printf("PATTERN: 0x");

--- 23 unchanged lines hidden (view full) ---

1540 warnx("Unable to set IPSec policy");
1541 free(buf);
1542
1543 return 0;
1544}
1545#endif
1546#endif
1547
1876 if (ii > 0)
1877 for (kk = 0;
1878 kk <= MAXDATALEN - (8 + sizeof(struct timeval) + ii);
1879 kk += ii)
1880 for (jj = 0; jj < ii; ++jj)
1881 bp[jj + kk] = pat[jj];
1882 if (!(options & F_QUIET)) {
1883 (void)printf("PATTERN: 0x");

--- 23 unchanged lines hidden (view full) ---

1907 warnx("Unable to set IPSec policy");
1908 free(buf);
1909
1910 return 0;
1911}
1912#endif
1913#endif
1914
1915char *
1916nigroup(name)
1917 char *name;
1918{
1919 char *p;
1920 MD5_CTX ctxt;
1921 u_int8_t digest[16];
1922 char l;
1923 char hbuf[NI_MAXHOST];
1924 struct in6_addr in6;
1925
1926 p = name;
1927 while (p && *p && *p != '.')
1928 p++;
1929 if (p - name > 63)
1930 return NULL; /*label too long*/
1931 l = p - name;
1932
1933 /* generate 8 bytes of pseudo-random value. */
1934 bzero(&ctxt, sizeof(ctxt));
1935 MD5Init(&ctxt);
1936 MD5Update(&ctxt, &l, sizeof(l));
1937 MD5Update(&ctxt, name, p - name);
1938 MD5Final(digest, &ctxt);
1939
1940 bzero(&in6, sizeof(in6));
1941 in6.s6_addr[0] = 0xff;
1942 in6.s6_addr[1] = 0x02;
1943 in6.s6_addr[11] = 0x02;
1944 bcopy(digest, &in6.s6_addr[12], 4);
1945
1946 if (inet_ntop(AF_INET6, &in6, hbuf, sizeof(hbuf)) == NULL)
1947 return NULL;
1948
1949 return strdup(hbuf);
1950}
1951
1548void
1549usage()
1550{
1551 (void)fprintf(stderr,
1952void
1953usage()
1954{
1955 (void)fprintf(stderr,
1552"usage: ping6 [-dfnqRrvwW"
1956"usage: ping6 [-dfHnNqvwW"
1957#ifdef IPV6_REACHCONF
1958 "R"
1959#endif
1553#ifdef IPSEC
1554#ifdef IPSEC_POLICY_IPSEC
1555 "] [-P policy"
1556#else
1557 "AE"
1558#endif
1960#ifdef IPSEC
1961#ifdef IPSEC_POLICY_IPSEC
1962 "] [-P policy"
1963#else
1964 "AE"
1965#endif
1559#endif
1560 "] [-a [aclsg]] [-b sockbufsiz] [-c count] [-I interface]\n\
1561 [-i wait] [-l preload] [-p pattern] [-s packetsize]\n\
1562 [-h hoplimit] host [hosts...]\n");
1966#endif
1967 "] [-a [aAclsg]] [-b sockbufsiz] [-c count] \n\
1968 [-I interface] [-i wait] [-l preload] [-p pattern] [-S sourceaddr]\n\
1969 [-s packetsize] [-h hoplimit] [hops...] host\n");
1563 exit(1);
1564}
1970 exit(1);
1971}