Deleted Added
sdiff udiff text old ( 57851 ) new ( 62627 )
full compact
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.
66 *
67 * $FreeBSD: head/sbin/ping6/ping6.c 57851 2000-03-09 14:47:21Z shin $
68 */
69
70#ifndef lint
71static const 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
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
100 * as IPV6_PKTINFO. Some objects 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
137#define MAXPACKETLEN 131072
138#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 */
143#define EXTRA 256 /* for AH and various other headers. weird. */
144#define DEFDATALEN ICMP6ECHOTMLEN
145#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
164#define F_POLICY 0x4000
165#else
166#define F_AUTHHDR 0x0200
167#define F_ENCRYPT 0x0400
168#endif /*IPSEC_POLICY_IPSEC*/
169#endif /*IPSEC*/
170#define F_NODEADDR 0x0800
171#define F_FQDN 0x1000
172#define F_INTERFACE 0x2000
173u_int options;
174
175#define IN6LEN sizeof(struct in6_addr)
176#define SA6LEN sizeof(struct sockaddr_in6)
177#define DUMMY_PORT 10101
178
179#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
190struct 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 */
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
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 *));
229void onalrm __P((int));
230void oninfo __P((int));
231void onint __P((int));
232void pinger __P((void));
233char *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 *));
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 *));
242void usage __P((void));
243
244int
245main(argc, argv)
246 int argc;
247 char *argv[];
248{
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;
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
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
279 while ((ch = getopt(argc, argv, "a:b:c:dfh:I:i:l:np:qRrs:vwW")) != EOF)
280#else
281#ifdef IPSEC_POLICY_IPSEC
282 while ((ch = getopt(argc, argv, "a:b:c:dfh:I:i:l:np:qRrs:vwWP:")) != EOF)
283#else
284 while ((ch = getopt(argc, argv, "a:b:c:dfh:I:i:l:np:qRrs:vwWAE")) != EOF)
285#endif /*IPSEC_POLICY_IPSEC*/
286#endif
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();
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;
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;
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;
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':
386 options |= F_RROUTE;
387 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':
401 case 'W':
402 options |= F_FQDN;
403 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();
426 }
427 argc -= optind;
428 argv += optind;
429
430 if (argc < 1)
431 usage();
432
433 if (argc > 1)
434 ip6optlen += inet6_rthdr_space(IPV6_RTHDR_TYPE_0, argc - 1);
435
436 target = argv[argc - 1];
437
438 /* getaddrinfo */
439 bzero(&hints, sizeof(struct addrinfo));
440 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));
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;
457
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
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);
523 if ((options & F_FQDN) || (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
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*/
543 /* record route option */
544 if (options & F_RROUTE)
545 errx(1, "record route not available in this implementation");
546
547 /* Outgoing interface */
548#ifndef USE_SIN6_SCOPE_ID
549 if (options & F_INTERFACE)
550 ip6optlen += CMSG_SPACE(sizeof(struct in6_pktinfo));
551#endif
552
553 if (hoplimit != -1)
554 ip6optlen += CMSG_SPACE(sizeof(int));
555
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 }
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));
572 scmsgp->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo));
573 scmsgp->cmsg_level = IPPROTO_IPV6;
574 scmsgp->cmsg_type = IPV6_PKTINFO;
575
576 scmsgp = CMSG_NXTHDR(&smsghdr, scmsgp);
577#else
578 if ((dst.sin6_scope_id = if_nametoindex(ifname)) == 0)
579 errx(1, "%s: invalid interface name", ifname);
580#endif
581 }
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 }
590 if (argc > 1) { /* some intermediate addrs are specified */
591 int hops, error;
592
593 if ((scmsgp = (struct cmsghdr *)inet6_rthdr_init(scmsgp,
594 IPV6_RTHDR_TYPE_0)) == 0)
595 errx(1, "can't initialize rthdr");
596
597 for (hops = 0; hops < argc - 1; hops++) {
598 struct addrinfo *iaip;
599
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 }
606 if (SIN6(res->ai_addr)->sin6_family != AF_INET6)
607 errx(1,
608 "bad addr family of an intermediate addr");
609
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");
614 }
615
616 if (inet6_rthdr_lasthop(scmsgp, IPV6_RTHDR_LOOSE))
617 errx(1, "can't set the last flag");
618
619 scmsgp = CMSG_NXTHDR(&smsghdr, scmsgp);
620 }
621
622 {
623 /*
624 * source selection
625 */
626 int dummy, len = sizeof(src);
627
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
636#ifndef USE_SIN6_SCOPE_ID
637 if (setsockopt(dummy, IPPROTO_IPV6, IPV6_PKTOPTIONS,
638 (void *)smsghdr.msg_control,
639 smsghdr.msg_controllen)) {
640 err(1, "UDP setsockopt");
641 }
642#else
643 src.sin6_scope_id = dst.sin6_scope_id;
644#endif
645
646 if (connect(dummy, (struct sockaddr *)&src, len) < 0)
647 err(1, "UDP connect");
648
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)
658 warnx("you need -b to increae 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
682 setsockopt(s, IPPROTO_IPV6, IPV6_PKTINFO, &optval, sizeof(optval));
683#endif
684 setsockopt(s, IPPROTO_IPV6, IPV6_HOPLIMIT, &optval, sizeof(optval));
685
686 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)));
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;
711 u_char buf[256];
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 */
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;
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;
809 /* code field is always 0 */
810 /* XXX: overwrite icmp6_id */
811 icp->icmp6_data16[0] = htons(NI_QTYPE_FQDN);
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;
819 /* code field is always 0 */
820 /* XXX: overwrite icmp6_id */
821 icp->icmp6_data16[0] = htons(NI_QTYPE_NODEADDR);
822 if (timing)
823 (void)gettimeofday((struct timeval *)
824 &outpack[ICMP6ECHOLEN], NULL);
825 cc = ICMP6_NIQLEN;
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{
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;
875#ifdef OLD_RAWSOCKET
876 struct ip6_hdr *ip;
877#endif
878 struct timeval tv, *tp;
879 double triptime = 0;
880 int dupflag;
881 size_t off;
882
883 (void)gettimeofday(&tv, NULL);
884
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,
939 inet_ntop(AF_INET6, (void *)&from->sin6_addr,
940 ntop_buf, sizeof(ntop_buf)));
941 return;
942 }
943 icp = (struct icmp6_hdr *)buf;
944 off = 0;
945#endif
946
947 if ((hoplim = get_hoplim(mhdr)) == -1) {
948 warnx("failed to get receiving hop limit");
949 return;
950 }
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,
986 inet_ntop(AF_INET6, &from->sin6_addr,
987 ntop_buf, sizeof(ntop_buf)),
988 icp->icmp6_seq);
989 (void)printf(" hlim=%d", hoplim);
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 */
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;
1032
1033 (void)printf(" (");
1034
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 }
1043
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 }
1060
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 ;
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');
1083 (void)fflush(stdout);
1084 }
1085}
1086
1087void
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);
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
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 */
1154void
1155oninfo(notused)
1156 int notused;
1157{
1158 summary();
1159}
1160
1161/*
1162 * onint --
1163 * SIGINT handler.
1164 */
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{
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));
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:
1302 (void)printf("Echo Request\n");
1303 /* XXX ID + Seq + Data */
1304 break;
1305 case ICMP6_ECHO_REPLY:
1306 (void)printf("Echo Reply\n");
1307 /* XXX ID + Seq + Data */
1308 break;
1309 case ICMP6_MEMBERSHIP_QUERY:
1310 (void)printf("Membership Query\n");
1311 break;
1312 case ICMP6_MEMBERSHIP_REPORT:
1313 (void)printf("Membership Report\n");
1314 break;
1315 case ICMP6_MEMBERSHIP_REDUCTION:
1316 (void)printf("Membership Reduction\n");
1317 break;
1318 case ND_ROUTER_SOLICIT:
1319 (void)printf("Router Solicitation\n");
1320 break;
1321 case ND_ROUTER_ADVERT:
1322 (void)printf("Router Advertisement\n");
1323 break;
1324 case ND_NEIGHBOR_SOLICIT:
1325 (void)printf("Neighbor Solicitation\n");
1326 break;
1327 case ND_NEIGHBOR_ADVERT:
1328 (void)printf("Neighbor Advertisement\n");
1329 break;
1330 case ND_REDIRECT:
1331 {
1332 struct nd_redirect *red = (struct nd_redirect *)icp;
1333
1334 (void)printf("Redirect\n");
1335 (void)printf("Destination: %s\n",
1336 inet_ntop(AF_INET6, &red->nd_rd_dst,
1337 ntop_buf, sizeof(ntop_buf)));
1338 (void)printf("New Target: %s\n",
1339 inet_ntop(AF_INET6, &red->nd_rd_target,
1340 ntop_buf, sizeof(ntop_buf)));
1341 break;
1342 }
1343 case ICMP6_NI_QUERY:
1344 (void)printf("Node Information Query\n");
1345 /* XXX ID + Seq + Data */
1346 break;
1347 case ICMP6_NI_REPLY:
1348 (void)printf("Node Information Reply\n");
1349 /* XXX ID + Seq + Data */
1350 break;
1351 default:
1352 (void)printf("Bad ICMP type: %d\n", 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;
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,
1377 ntop_buf, INET6_ADDRSTRLEN));
1378 printf("%s\n", inet_ntop(AF_INET6, &ip6->ip6_dst,
1379 ntop_buf, INET6_ADDRSTRLEN));
1380}
1381
1382/*
1383 * pr_addr --
1384 * Return an ascii host address as a dotted quad and optionally with
1385 * a hostname.
1386 */
1387char *
1388pr_addr(addr)
1389 struct sockaddr_in6 *addr;
1390{
1391 static char buf[MAXHOSTNAMELEN];
1392 int flag = 0;
1393
1394 if (options & F_NUMERIC)
1395 flag |= NI_NUMERICHOST;
1396
1397 flag |= NI_WITHSCOPEID;
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
1508/* 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
1548void
1549usage()
1550{
1551 (void)fprintf(stderr,
1552"usage: ping6 [-dfnqRrvwW"
1553#ifdef IPSEC
1554#ifdef IPSEC_POLICY_IPSEC
1555 "] [-P policy"
1556#else
1557 "AE"
1558#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");
1563 exit(1);
1564}