Deleted Added
sdiff udiff text old ( 57851 ) new ( 62627 )
full compact
1/* $KAME: ping6.c,v 1.54 2000/06/12 16:16:44 itojun Exp $ */
2
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.
68 */
69
70#ifndef lint
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
80static const char rcsid[] =
81 "$FreeBSD: head/sbin/ping6/ping6.c 62627 2000-07-05 09:34:10Z kris $";
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
102 * as IPV6_PKTINFO. Some people object it (sin6_scope_id specifies *link* while
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
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
146#define IP6LEN 40
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 */
151#define EXTRA 256 /* for AH and various other headers. weird. */
152#define DEFDATALEN ICMP6ECHOTMLEN
153#define MAXDATALEN MAXPACKETLEN - IP6LEN - ICMP6ECHOLEN
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
172#define F_POLICY 0x0400
173#else
174#define F_AUTHHDR 0x0200
175#define F_ENCRYPT 0x0400
176#endif /*IPSEC_POLICY_IPSEC*/
177#endif /*IPSEC*/
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
188u_int options;
189
190#define IN6LEN sizeof(struct in6_addr)
191#define SA6LEN sizeof(struct sockaddr_in6)
192#define DUMMY_PORT 10101
193
194#define SIN6(s) ((struct sockaddr_in6 *)(s))
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
205struct addrinfo *res;
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;
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
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 *));
243void onalrm __P((int));
244void oninfo __P((int));
245void onint __P((int));
246void pinger __P((void));
247const char *pr_addr __P((struct sockaddr_in6 *));
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 *));
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 *));
260void usage __P((void));
261
262int
263main(argc, argv)
264 int argc;
265 char *argv[];
266{
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
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
293 preload = 0;
294 datap = &outpack[ICMP6ECHOLEN + ICMP6ECHOTMLEN];
295#ifndef IPSEC
296 while ((ch = getopt(argc, argv, "a:b:c:dfHh:I:i:l:nNp:qRS:s:vwW")) != EOF)
297#else
298#ifdef IPSEC_POLICY_IPSEC
299 while ((ch = getopt(argc, argv, "a:b:c:dfHh:I:i:l:nNp:qRS:s:vwWP:")) != EOF)
300#else
301 while ((ch = getopt(argc, argv, "a:b:c:dfHh:I:i:l:nNp:qRS:s:vwWAE")) != EOF)
302#endif /*IPSEC_POLICY_IPSEC*/
303#endif
304 {
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*/
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;
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
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;
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':
414#ifdef IPV6_REACHCONF
415 options |= F_REACHCONF;
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;
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':
440 options |= F_FQDN;
441 break;
442 case 'W':
443 options |= F_FQDNOLD;
444 break;
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*/
468 }
469 }
470 argc -= optind;
471 argv += optind;
472
473 if (argc < 1) {
474 usage();
475 /*NOTREACHED*/
476 }
477
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 */
482 ip6optlen += inet6_rthdr_space(IPV6_RTHDR_TYPE_0, argc - 1);
483#endif
484 }
485
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];
494
495 /* getaddrinfo */
496 bzero(&hints, sizeof(struct addrinfo));
497 if ((options & F_NUMERIC) != 0)
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));
506 exit(1);
507 }
508 if (res->ai_canonname)
509 hostname = res->ai_canonname;
510 else
511 hostname = target;
512
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
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);
581 if ((options & F_FQDN) || (options & F_FQDNOLD) ||
582 (options & F_NODEADDR))
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
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*/
641
642 /* Specify the outgoing interface and/or the source address */
643 if (usepktinfo)
644 ip6optlen += CMSG_SPACE(sizeof(struct in6_pktinfo));
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
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 }
662 if (usepktinfo) {
663 pktinfo = (struct in6_pktinfo *)(CMSG_DATA(scmsgp));
664 memset(pktinfo, 0, sizeof(*pktinfo));
665 scmsgp->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo));
666 scmsgp->cmsg_level = IPPROTO_IPV6;
667 scmsgp->cmsg_type = IPV6_PKTINFO;
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);
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
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
704 if (argc > 1) { /* some intermediate addrs are specified */
705 int hops, error;
706#ifdef USE_RFC2292BIS
707 int rthdrlen;
708#endif
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 */
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 */
725
726 for (hops = 0; hops < argc - 1; hops++) {
727 struct addrinfo *iaip;
728
729 if ((error = getaddrinfo(argv[hops], NULL, &hints, &iaip)))
730 errx(1, gai_strerror(error));
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 */
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 */
745 }
746
747#ifndef USE_RFC2292BIS
748 if (inet6_rthdr_lasthop(scmsgp, IPV6_RTHDR_LOOSE))
749 errx(1, "can't set the last flag");
750#endif
751
752 scmsgp = CMSG_NXTHDR(&smsghdr, scmsgp);
753 }
754
755 {
756 /*
757 * source selection
758 */
759 int dummy, len = sizeof(src);
760
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
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,
792 (void *)smsghdr.msg_control,
793 smsghdr.msg_controllen)) {
794 err(1, "UDP setsockopt(IPV6_PKTOPTIONS)");
795 }
796#endif
797
798 if (connect(dummy, (struct sockaddr *)&src, len) < 0)
799 err(1, "UDP connect");
800
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)
810 warnx("you need -b to increase socket buffer size");
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
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? */
842#endif
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
853
854 printf("PING6(%d=40+8+%d bytes) ", datalen + 48, datalen);
855 printf("%s --> ", pr_addr(&src));
856 printf("%s\n", pr_addr(&dst));
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;
879 u_char buf[1024];
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 */
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));
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;
993 /* code field is always 0 */
994 /* XXX: overwrite icmp6_id */
995 ((struct icmp6_nodeinfo *)icp)->ni_qtype = htons(NI_QTYPE_FQDN);
996 ((struct icmp6_nodeinfo *)icp)->ni_flags = htons(0);
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;
1004 icp->icmp6_code = ICMP6_NI_SUBJ_IPV6;
1005 /* XXX: overwrite icmp6_id */
1006 ((struct icmp6_nodeinfo *)icp)->ni_qtype =
1007 htons(NI_QTYPE_NODEADDR);
1008 ((struct icmp6_nodeinfo *)icp)->ni_flags = htons(0);
1009 if (timing)
1010 (void)gettimeofday((struct timeval *)
1011 &outpack[ICMP6ECHOLEN], NULL);
1012 memcpy(&outpack[ICMP6_NIQLEN], &dst.sin6_addr,
1013 sizeof(dst.sin6_addr));
1014 cc = ICMP6_NIQLEN + sizeof(dst.sin6_addr);
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)
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;
1065 struct in6_pktinfo *pktinfo = NULL;
1066 struct timeval tv, *tp;
1067 double triptime = 0;
1068 int dupflag;
1069 size_t off;
1070 int oldfqdn;
1071
1072 (void)gettimeofday(&tv, NULL);
1073
1074 if (cc < sizeof(struct icmp6_hdr)) {
1075 if (options & F_VERBOSE)
1076 warnx("packet too short (%d bytes) from %s\n", cc,
1077 pr_addr(from));
1078 return;
1079 }
1080 icp = (struct icmp6_hdr *)buf;
1081 off = 0;
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 }
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,
1126 pr_addr(from),
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 }
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 */
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;
1215
1216 (void)printf(" ("); /*)*/
1217
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 }
1228
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++;
1247
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 ;
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);
1291 (void)fflush(stdout);
1292 }
1293#undef safeputc
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
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];
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
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 */
1515void
1516oninfo(notused)
1517 int notused;
1518{
1519 summary();
1520}
1521
1522/*
1523 * onint --
1524 * SIGINT handler.
1525 */
1526/* ARGSUSED */
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
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);
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:
1667 (void)printf("Echo Request");
1668 /* XXX ID + Seq + Data */
1669 break;
1670 case ICMP6_ECHO_REPLY:
1671 (void)printf("Echo Reply");
1672 /* XXX ID + Seq + Data */
1673 break;
1674 case ICMP6_MEMBERSHIP_QUERY:
1675 (void)printf("Listener Query");
1676 break;
1677 case ICMP6_MEMBERSHIP_REPORT:
1678 (void)printf("Listener Report");
1679 break;
1680 case ICMP6_MEMBERSHIP_REDUCTION:
1681 (void)printf("Listener Done");
1682 break;
1683 case ND_ROUTER_SOLICIT:
1684 (void)printf("Router Solicitation");
1685 break;
1686 case ND_ROUTER_ADVERT:
1687 (void)printf("Router Advertisement");
1688 break;
1689 case ND_NEIGHBOR_SOLICIT:
1690 (void)printf("Neighbor Solicitation");
1691 break;
1692 case ND_NEIGHBOR_ADVERT:
1693 (void)printf("Neighbor Advertisement");
1694 break;
1695 case ND_REDIRECT:
1696 {
1697 struct nd_redirect *red = (struct nd_redirect *)icp;
1698
1699 (void)printf("Redirect\n");
1700 (void)printf("Destination: %s",
1701 inet_ntop(AF_INET6, &red->nd_rd_dst,
1702 ntop_buf, sizeof(ntop_buf)));
1703 (void)printf("New Target: %s",
1704 inet_ntop(AF_INET6, &red->nd_rd_target,
1705 ntop_buf, sizeof(ntop_buf)));
1706 break;
1707 }
1708 case ICMP6_NI_QUERY:
1709 (void)printf("Node Information Query");
1710 /* XXX ID + Seq + Data */
1711 break;
1712 case ICMP6_NI_REPLY:
1713 (void)printf("Node Information Reply");
1714 /* XXX ID + Seq + Data */
1715 break;
1716 default:
1717 (void)printf("Bad ICMP type: %d", icp->icmp6_type);
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];
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,
1743 ntop_buf, sizeof(ntop_buf)));
1744 printf("%s\n", inet_ntop(AF_INET6, &ip6->ip6_dst,
1745 ntop_buf, sizeof(ntop_buf)));
1746}
1747
1748/*
1749 * pr_addr --
1750 * Return an ascii host address as a dotted quad and optionally with
1751 * a hostname.
1752 */
1753const char *
1754pr_addr(addr)
1755 struct sockaddr_in6 *addr;
1756{
1757 static char buf[MAXHOSTNAMELEN];
1758 int flag = 0;
1759
1760 if ((options & F_HOSTNAME) == 0)
1761 flag |= NI_NUMERICHOST;
1762#ifdef KAME_SCOPEID
1763 flag |= NI_WITHSCOPEID;
1764#endif
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
1875/* xxx */
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
1952void
1953usage()
1954{
1955 (void)fprintf(stderr,
1956"usage: ping6 [-dfHnNqvwW"
1957#ifdef IPV6_REACHCONF
1958 "R"
1959#endif
1960#ifdef IPSEC
1961#ifdef IPSEC_POLICY_IPSEC
1962 "] [-P policy"
1963#else
1964 "AE"
1965#endif
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");
1970 exit(1);
1971}