forked from IAmTheOneTheyCallNeo/android_external_ping6
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ping6.c
2696 lines (2462 loc) · 65 KB
/
ping6.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* $NetBSD: ping6.c,v 1.73 2010/09/20 11:49:48 ahoka Exp $ */
/* $KAME: ping6.c,v 1.164 2002/11/16 14:05:37 itojun Exp $ */
/*
* Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the project nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/* BSDI ping.c,v 2.3 1996/01/21 17:56:50 jch Exp */
/*
* Copyright (c) 1989, 1993
* The Regents of the University of California. All rights reserved.
*
* This code is derived from software contributed to Berkeley by
* Mike Muuss.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#if 0
#ifndef lint
static char copyright[] =
"@(#) Copyright (c) 1989, 1993\n\
The Regents of the University of California. All rights reserved.\n";
#endif /* not lint */
#ifndef lint
static char sccsid[] = "@(#)ping.c 8.1 (Berkeley) 6/5/93";
#endif /* not lint */
#else
#include <sys/cdefs.h>
#ifndef lint
__RCSID("$NetBSD: ping6.c,v 1.73 2010/09/20 11:49:48 ahoka Exp $");
#endif
#endif
/*
* Using the InterNet Control Message Protocol (ICMP) "ECHO" facility,
* measure round-trip-delays and packet loss across network paths.
*
* Author -
* Mike Muuss
* U. S. Army Ballistic Research Laboratory
* December, 1983
*
* Status -
* Public Domain. Distribution Unlimited.
* Bugs -
* More statistics could always be gathered.
* This program has to run SUID to ROOT to access the ICMP socket.
*/
/*
* NOTE:
* USE_SIN6_SCOPE_ID assumes that sin6_scope_id has the same semantics
* as IPV6_PKTINFO. Some people object it (sin6_scope_id specifies *link*
* while IPV6_PKTINFO specifies *interface*. Link is defined as collection of
* network attached to 1 or more interfaces)
*/
#include <sys/param.h>
#include <sys/uio.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <net/if.h>
#include <net/route.h>
#include <netinet/in.h>
#include <netinet/ip6.h>
#include <netinet/icmp6.h>
#include <arpa/inet.h>
#include <arpa/nameser.h>
#include <netdb.h>
#include <ctype.h>
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <math.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <poll.h>
#include <private/android_filesystem_config.h>
#ifdef IPSEC
#include <netinet6/ah.h>
#include <netinet6/ipsec.h>
#endif
/*
* We currently don't have the libc support required for these two features in
* Android. Should we get enough support later, feel free to remove the #ifdefs
* altogether.
*/
#undef ANDROID_INCLUDE_MD5_SUPPORT
#undef ANDROID_INCLUDE_RTHDR_SUPPORT
#ifdef ANDROID_INCLUDE_MD5_SUPPORT
#include <md5.h>
#endif
struct tv32 {
u_int32_t tv32_sec;
u_int32_t tv32_usec;
};
#define MAXPACKETLEN 131072
#define IP6LEN 40
#define ICMP6ECHOLEN 8 /* icmp echo header len excluding time */
#define ICMP6ECHOTMLEN sizeof(struct tv32)
#define ICMP6_NIQLEN (ICMP6ECHOLEN + 8)
/* FQDN case, 64 bits of nonce + 32 bits ttl */
#define ICMP6_NIRLEN (ICMP6ECHOLEN + 12)
#define EXTRA 256 /* for AH and various other headers. weird. */
#define DEFDATALEN ICMP6ECHOTMLEN
#define MAXDATALEN MAXPACKETLEN - IP6LEN - ICMP6ECHOLEN
#define NROUTES 9 /* number of record route slots */
#define A(bit) rcvd_tbl[(bit)>>3] /* identify byte in array */
#define B(bit) (1 << ((bit) & 0x07)) /* identify bit in byte */
#define SET(bit) (A(bit) |= B(bit))
#define CLR(bit) (A(bit) &= (~B(bit)))
#define TST(bit) (A(bit) & B(bit))
#define F_FLOOD 0x0001
#define F_INTERVAL 0x0002
#define F_PINGFILLED 0x0008
#define F_QUIET 0x0010
#define F_RROUTE 0x0020
#define F_SO_DEBUG 0x0040
#define F_VERBOSE 0x0100
#ifdef IPSEC
#ifdef IPSEC_POLICY_IPSEC
#define F_POLICY 0x0400
#else
#define F_AUTHHDR 0x0200
#define F_ENCRYPT 0x0400
#endif /*IPSEC_POLICY_IPSEC*/
#endif /*IPSEC*/
#define F_NODEADDR 0x0800
#define F_FQDN 0x1000
#define F_INTERFACE 0x2000
#define F_SRCADDR 0x4000
#ifdef IPV6_REACHCONF
#define F_REACHCONF 0x8000
#endif
#define F_HOSTNAME 0x10000
#define F_FQDNOLD 0x20000
#define F_NIGROUP 0x40000
#define F_SUPTYPES 0x80000
#define F_NOMINMTU 0x100000
#define F_NOUSERDATA (F_NODEADDR | F_FQDN | F_FQDNOLD | F_SUPTYPES)
u_int options;
#define IN6LEN sizeof(struct in6_addr)
#define SA6LEN sizeof(struct sockaddr_in6)
#define DUMMY_PORT 10101
#define SIN6(s) ((struct sockaddr_in6 *)(s))
/* Android-specific hacks to get this to compile.*/
#define INFTIM -1
#define MAXDNAME 1025
/*
* MAX_DUP_CHK is the number of bits in received table, i.e. the maximum
* number of received sequence numbers we can keep track of. Change 128
* to 8192 for complete accuracy...
*/
#define MAX_DUP_CHK (8 * 8192)
int mx_dup_ck = MAX_DUP_CHK;
char rcvd_tbl[MAX_DUP_CHK / 8];
struct addrinfo *res;
struct sockaddr_in6 dst; /* who to ping6 */
struct sockaddr_in6 src; /* src addr of this packet */
socklen_t srclen;
int datalen = DEFDATALEN;
int s; /* socket file descriptor */
u_char outpack[MAXPACKETLEN];
char BSPACE = '\b'; /* characters written for flood */
char DOT = '.';
char *hostname;
int ident; /* process id to identify our packets */
u_int8_t nonce[8]; /* nonce field for node information */
int hoplimit = -1; /* hoplimit */
int pathmtu = 0; /* path MTU for the destination. 0 = unspec. */
/* counters */
long npackets; /* max packets to transmit */
long nreceived; /* # of packets we got back */
long nrepeats; /* number of duplicates */
long ntransmitted; /* sequence # for outbound packets = #sent */
struct timeval interval = {1, 0}; /* interval between packets */
/* timing */
int timing; /* flag to do timing */
double tmin = 999999999.0; /* minimum round trip time */
double tmax = 0.0; /* maximum round trip time */
double tsum = 0.0; /* sum of all times, for doing average */
double tsumsq = 0.0; /* sum of all times squared, for std. dev. */
/* for node addresses */
u_short naflags;
/* for ancillary data(advanced API) */
struct msghdr smsghdr;
struct iovec smsgiov;
char *scmsg = 0;
volatile sig_atomic_t seenalrm;
volatile sig_atomic_t seenint;
#ifdef SIGINFO
volatile sig_atomic_t seeninfo;
#endif
void fill(char *, char *);
int get_hoplim(struct msghdr *);
int get_pathmtu(struct msghdr *);
struct in6_pktinfo *get_rcvpktinfo(struct msghdr *);
void onsignal(int);
void retransmit(void);
void onint(int);
size_t pingerlen(void);
int pinger(void);
const char *pr_addr(struct sockaddr *, int);
void pr_icmph(struct icmp6_hdr *, u_char *);
void pr_iph(struct ip6_hdr *);
void pr_suptypes(struct icmp6_nodeinfo *, size_t);
void pr_nodeaddr(struct icmp6_nodeinfo *, int);
int myechoreply(const struct icmp6_hdr *);
int mynireply(const struct icmp6_nodeinfo *);
char *dnsdecode(const u_char **, const u_char *, const u_char *,
char *, size_t);
void pr_pack(u_char *, int, struct msghdr *);
void pr_exthdrs(struct msghdr *);
#ifdef ANDROID_INCLUDE_RTHDR_SUPPORT
void pr_ip6opt(void *);
void pr_rthdr(void *);
#endif
int pr_bitrange(u_int32_t, int, int);
void pr_retip(struct ip6_hdr *, u_char *);
void summary(void);
void tvsub(struct timeval *, struct timeval *);
int setpolicy(int, char *);
#ifdef ANDROID_INCLUDE_MD5_SUPPORT
char *nigroup(char *);
#endif
void usage(void);
int
isInSupplementaryGroup(gid_t group)
{
int numgroups, i;
gid_t groups[sysconf(_SC_NGROUPS_MAX) + 1];
numgroups = getgroups(sizeof(groups) / sizeof(groups[0]), groups);
if (numgroups < 0) {
perror("getgroups");
return 0;
}
for (i = 0; i < numgroups; i++) {
if (group == groups[i])
return 1;
}
return 0;
}
int
main(int argc, char *argv[])
{
struct itimerval itimer;
struct sockaddr_in6 from;
int timeout;
struct addrinfo hints;
struct pollfd fdmaskp[1];
int cc;
u_int i, packlen;
int ch, hold, preload, optval, ret_ga;
u_char *datap, *packet;
char *e, *target, *ifname = NULL, *gateway = NULL;
int ip6optlen = 0;
struct cmsghdr *scmsgp = NULL;
#if defined(SO_SNDBUF) && defined(SO_RCVBUF)
u_long lsockbufsize;
int sockbufsize = 0;
#endif
int usepktinfo = 0;
struct in6_pktinfo *pktinfo = NULL;
#ifdef ANDROID_INCLUDE_RTHDR_SUPPORT
struct ip6_rthdr *rthdr = NULL;
#endif
#ifdef IPSEC_POLICY_IPSEC
char *policy_in = NULL;
char *policy_out = NULL;
#endif
double intval;
size_t rthlen;
#ifdef IPV6_USE_MIN_MTU
int mflag = 0;
#endif
if (getuid() != 0 && !isInSupplementaryGroup(AID_INET)) {
fprintf(stderr, "%s: not root or in group AID_INET\n", argv[0]);
exit(3);
}
/* just to be sure */
memset(&smsghdr, 0, sizeof(smsghdr));
memset(&smsgiov, 0, sizeof(smsgiov));
preload = 0;
datap = &outpack[ICMP6ECHOLEN + ICMP6ECHOTMLEN];
#ifndef IPSEC
#define ADDOPTS
#else
#ifdef IPSEC_POLICY_IPSEC
#define ADDOPTS "P:"
#else
#define ADDOPTS "AE"
#endif /*IPSEC_POLICY_IPSEC*/
#endif
#ifdef ANDROID_INCLUDE_MD5_SUPPORT
#define ANDROID_MD5_OPTS "N"
#else
#define ANDROID_MD5_OPTS ""
#endif
while ((ch = getopt(argc, argv,
"a:b:c:dfHg:h:I:i:l:mnp:qRS:s:tvwW" ADDOPTS ANDROID_MD5_OPTS)) != -1) {
#undef ADDOPTS
switch (ch) {
case 'a':
{
char *cp;
options &= ~F_NOUSERDATA;
options |= F_NODEADDR;
for (cp = optarg; *cp != '\0'; cp++) {
switch (*cp) {
case 'a':
naflags |= NI_NODEADDR_FLAG_ALL;
break;
case 'c':
case 'C':
naflags |= NI_NODEADDR_FLAG_COMPAT;
break;
case 'l':
case 'L':
naflags |= NI_NODEADDR_FLAG_LINKLOCAL;
break;
case 's':
case 'S':
naflags |= NI_NODEADDR_FLAG_SITELOCAL;
break;
case 'g':
case 'G':
naflags |= NI_NODEADDR_FLAG_GLOBAL;
break;
case 'A': /* experimental. not in the spec */
#ifdef NI_NODEADDR_FLAG_ANYCAST
naflags |= NI_NODEADDR_FLAG_ANYCAST;
break;
#else
errx(1,
"-a A is not supported on the platform");
/*NOTREACHED*/
#endif
default:
usage();
/*NOTREACHED*/
}
}
break;
}
case 'b':
#if defined(SO_SNDBUF) && defined(SO_RCVBUF)
errno = 0;
e = NULL;
lsockbufsize = strtoul(optarg, &e, 10);
sockbufsize = lsockbufsize;
if (errno || !*optarg || *e ||
(u_long)sockbufsize != lsockbufsize)
errx(1, "invalid socket buffer size");
#else
errx(1,
"-b option ignored: SO_SNDBUF/SO_RCVBUF socket options not supported");
#endif
break;
case 'c':
npackets = strtol(optarg, &e, 10);
if (npackets <= 0 || *optarg == '\0' || *e != '\0')
errx(1,
"illegal number of packets -- %s", optarg);
break;
case 'd':
options |= F_SO_DEBUG;
break;
case 'f':
if (getuid()) {
errno = EPERM;
errx(1, "Must be superuser to flood ping");
}
options |= F_FLOOD;
setbuf(stdout, (char *)NULL);
break;
case 'g':
gateway = optarg;
break;
case 'H':
options |= F_HOSTNAME;
break;
case 'h': /* hoplimit */
hoplimit = strtol(optarg, &e, 10);
if (*optarg == '\0' || *e != '\0')
errx(1, "illegal hoplimit %s", optarg);
if (255 < hoplimit || hoplimit < -1)
errx(1,
"illegal hoplimit -- %s", optarg);
break;
case 'I':
ifname = optarg;
options |= F_INTERFACE;
#ifndef USE_SIN6_SCOPE_ID
usepktinfo++;
#endif
break;
case 'i': /* wait between sending packets */
intval = strtod(optarg, &e);
if (*optarg == '\0' || *e != '\0')
errx(1, "illegal timing interval %s", optarg);
if (intval < 1 && getuid()) {
errx(1, "%s: only root may use interval < 1s",
strerror(EPERM));
}
interval.tv_sec = (long)intval;
interval.tv_usec =
(long)((intval - interval.tv_sec) * 1000000);
if (interval.tv_sec < 0)
errx(1, "illegal timing interval %s", optarg);
/* less than 1/hz does not make sense */
if (interval.tv_sec == 0 && interval.tv_usec < 10000) {
warnx("too small interval, raised to 0.01");
interval.tv_usec = 10000;
}
options |= F_INTERVAL;
break;
case 'l':
if (getuid()) {
errno = EPERM;
errx(1, "Must be superuser to preload");
}
preload = strtol(optarg, &e, 10);
if (preload < 0 || *optarg == '\0' || *e != '\0')
errx(1, "illegal preload value -- %s", optarg);
break;
case 'm':
#ifdef IPV6_USE_MIN_MTU
mflag++;
break;
#else
errx(1, "-%c is not supported on this platform", ch);
/*NOTREACHED*/
#endif
case 'n':
options &= ~F_HOSTNAME;
break;
#ifdef ANDROID_INCLUDE_MD5_SUPPORT
case 'N':
options |= F_NIGROUP;
break;
#endif
case 'p': /* fill buffer with user pattern */
options |= F_PINGFILLED;
fill((char *)datap, optarg);
break;
case 'q':
options |= F_QUIET;
break;
case 'R':
#ifdef IPV6_REACHCONF
options |= F_REACHCONF;
break;
#else
errx(1, "-R is not supported in this configuration");
#endif
case 'S':
memset(&hints, 0, sizeof(struct addrinfo));
hints.ai_flags = AI_NUMERICHOST; /* allow hostname? */
hints.ai_family = AF_INET6;
hints.ai_socktype = SOCK_RAW;
hints.ai_protocol = IPPROTO_ICMPV6;
ret_ga = getaddrinfo(optarg, NULL, &hints, &res);
if (ret_ga) {
errx(1, "invalid source address: %s",
gai_strerror(ret_ga));
}
/*
* res->ai_family must be AF_INET6 and res->ai_addrlen
* must be sizeof(src).
*/
memcpy(&src, res->ai_addr, res->ai_addrlen);
srclen = res->ai_addrlen;
freeaddrinfo(res);
options |= F_SRCADDR;
break;
case 's': /* size of packet to send */
datalen = strtol(optarg, &e, 10);
if (datalen <= 0 || *optarg == '\0' || *e != '\0')
errx(1, "illegal datalen value -- %s", optarg);
if (datalen > MAXDATALEN) {
errx(1,
"datalen value too large, maximum is %d",
MAXDATALEN);
}
break;
case 't':
options &= ~F_NOUSERDATA;
options |= F_SUPTYPES;
break;
case 'v':
options |= F_VERBOSE;
break;
case 'w':
options &= ~F_NOUSERDATA;
options |= F_FQDN;
break;
case 'W':
options &= ~F_NOUSERDATA;
options |= F_FQDNOLD;
break;
#ifdef IPSEC
#ifdef IPSEC_POLICY_IPSEC
case 'P':
options |= F_POLICY;
if (!strncmp("in", optarg, 2)) {
if ((policy_in = strdup(optarg)) == NULL)
errx(1, "strdup");
} else if (!strncmp("out", optarg, 3)) {
if ((policy_out = strdup(optarg)) == NULL)
errx(1, "strdup");
} else
errx(1, "invalid security policy");
break;
#else
case 'A':
options |= F_AUTHHDR;
break;
case 'E':
options |= F_ENCRYPT;
break;
#endif /*IPSEC_POLICY_IPSEC*/
#endif /*IPSEC*/
default:
usage();
/*NOTREACHED*/
}
}
argc -= optind;
argv += optind;
if (argc < 1) {
usage();
/*NOTREACHED*/
}
if (argc > 1) {
#ifdef ANDROID_INCLUDE_RTHDR_SUPPORT
rthlen = CMSG_SPACE(inet6_rth_space(IPV6_RTHDR_TYPE_0,
argc - 1));
if (rthlen == 0) {
errx(1, "too many intermediate hops");
/*NOTREACHED*/
}
ip6optlen += rthlen;
#else
errx(1, "compiled without support for routing headers");
#endif
}
#ifdef ANDROID_INCLUDE_MD5_SUPPORT
if (options & F_NIGROUP) {
target = nigroup(argv[argc - 1]);
if (target == NULL) {
usage();
/*NOTREACHED*/
}
} else
#endif
target = argv[argc - 1];
/* getaddrinfo */
memset(&hints, 0, sizeof(struct addrinfo));
hints.ai_flags = AI_CANONNAME;
hints.ai_family = AF_INET6;
hints.ai_socktype = SOCK_RAW;
hints.ai_protocol = IPPROTO_ICMPV6;
ret_ga = getaddrinfo(target, NULL, &hints, &res);
if (ret_ga)
errx(1, "%s", gai_strerror(ret_ga));
if (res->ai_canonname)
hostname = res->ai_canonname;
else
hostname = target;
if (!res->ai_addr)
errx(1, "getaddrinfo failed");
(void)memcpy(&dst, res->ai_addr, res->ai_addrlen);
if ((s = socket(res->ai_family, res->ai_socktype,
res->ai_protocol)) < 0)
err(1, "socket");
/* set the source address if specified. */
if ((options & F_SRCADDR) &&
bind(s, (struct sockaddr *)&src, srclen) != 0) {
err(1, "bind");
}
/* set the gateway (next hop) if specified */
if (gateway) {
struct addrinfo ghints, *gres;
int error;
memset(&ghints, 0, sizeof(ghints));
ghints.ai_family = AF_INET6;
ghints.ai_socktype = SOCK_RAW;
ghints.ai_protocol = IPPROTO_ICMPV6;
error = getaddrinfo(gateway, NULL, &hints, &gres);
if (error) {
errx(1, "getaddrinfo for the gateway %s: %s",
gateway, gai_strerror(error));
}
if (gres->ai_next && (options & F_VERBOSE))
warnx("gateway resolves to multiple addresses");
if (setsockopt(s, IPPROTO_IPV6, IPV6_NEXTHOP,
gres->ai_addr, gres->ai_addrlen)) {
err(1, "setsockopt(IPV6_NEXTHOP)");
}
freeaddrinfo(gres);
}
/*
* let the kerel pass extension headers of incoming packets,
* for privileged socket options
*/
if ((options & F_VERBOSE) != 0) {
int opton = 1;
#ifdef IPV6_RECVHOPOPTS
if (setsockopt(s, IPPROTO_IPV6, IPV6_RECVHOPOPTS, &opton,
sizeof(opton)))
err(1, "setsockopt(IPV6_RECVHOPOPTS)");
#else /* old adv. API */
if (setsockopt(s, IPPROTO_IPV6, IPV6_HOPOPTS, &opton,
sizeof(opton)))
err(1, "setsockopt(IPV6_HOPOPTS)");
#endif
#ifdef IPV6_RECVDSTOPTS
if (setsockopt(s, IPPROTO_IPV6, IPV6_RECVDSTOPTS, &opton,
sizeof(opton)))
err(1, "setsockopt(IPV6_RECVDSTOPTS)");
#else /* old adv. API */
if (setsockopt(s, IPPROTO_IPV6, IPV6_DSTOPTS, &opton,
sizeof(opton)))
err(1, "setsockopt(IPV6_DSTOPTS)");
#endif
#ifdef IPV6_RECVRTHDRDSTOPTS
if (setsockopt(s, IPPROTO_IPV6, IPV6_RECVRTHDRDSTOPTS, &opton,
sizeof(opton)))
err(1, "setsockopt(IPV6_RECVRTHDRDSTOPTS)");
#endif
}
/* revoke root privilege */
seteuid(getuid());
setuid(getuid());
if ((options & F_FLOOD) && (options & F_INTERVAL))
errx(1, "-f and -i incompatible options");
if ((options & F_NOUSERDATA) == 0) {
if (datalen >= (int)sizeof(struct tv32)) {
/* we can time transfer */
timing = 1;
} else
timing = 0;
/* in F_VERBOSE case, we may get non-echoreply packets*/
if (options & F_VERBOSE)
packlen = 2048 + IP6LEN + ICMP6ECHOLEN + EXTRA;
else
packlen = datalen + IP6LEN + ICMP6ECHOLEN + EXTRA;
} else {
/* suppress timing for node information query */
timing = 0;
datalen = 2048;
packlen = 2048 + IP6LEN + ICMP6ECHOLEN + EXTRA;
}
if (!(packet = (u_char *)malloc(packlen)))
err(1, "Unable to allocate packet");
if (!(options & F_PINGFILLED))
for (i = ICMP6ECHOLEN; i < packlen; ++i)
*datap++ = i;
ident = arc4random() & 0xFFFF;
memset(nonce, 0, sizeof(nonce));
for (i = 0; i < sizeof(nonce); i += sizeof(u_int32_t))
*((u_int32_t *)&nonce[i]) = arc4random();
hold = 1;
if (options & F_SO_DEBUG)
(void)setsockopt(s, SOL_SOCKET, SO_DEBUG, (char *)&hold,
sizeof(hold));
optval = IPV6_DEFHLIM;
if (IN6_IS_ADDR_MULTICAST(&dst.sin6_addr))
if (setsockopt(s, IPPROTO_IPV6, IPV6_MULTICAST_HOPS,
&optval, sizeof(optval)) == -1)
err(1, "IPV6_MULTICAST_HOPS");
#ifdef IPV6_USE_MIN_MTU
if (mflag != 1) {
optval = mflag > 1 ? 0 : 1;
if (setsockopt(s, IPPROTO_IPV6, IPV6_USE_MIN_MTU,
&optval, sizeof(optval)) == -1)
err(1, "setsockopt(IPV6_USE_MIN_MTU)");
}
#ifdef IPV6_RECVPATHMTU
else {
optval = 1;
if (setsockopt(s, IPPROTO_IPV6, IPV6_RECVPATHMTU,
&optval, sizeof(optval)) == -1)
err(1, "setsockopt(IPV6_RECVPATHMTU)");
}
#endif /* IPV6_RECVPATHMTU */
#endif /* IPV6_USE_MIN_MTU */
#ifdef IPSEC
#ifdef IPSEC_POLICY_IPSEC
if (options & F_POLICY) {
if (setpolicy(s, policy_in) < 0)
errx(1, "%s", ipsec_strerror());
if (setpolicy(s, policy_out) < 0)
errx(1, "%s", ipsec_strerror());
}
#else
if (options & F_AUTHHDR) {
optval = IPSEC_LEVEL_REQUIRE;
#ifdef IPV6_AUTH_TRANS_LEVEL
if (setsockopt(s, IPPROTO_IPV6, IPV6_AUTH_TRANS_LEVEL,
&optval, sizeof(optval)) == -1)
err(1, "setsockopt(IPV6_AUTH_TRANS_LEVEL)");
#else /* old def */
if (setsockopt(s, IPPROTO_IPV6, IPV6_AUTH_LEVEL,
&optval, sizeof(optval)) == -1)
err(1, "setsockopt(IPV6_AUTH_LEVEL)");
#endif
}
if (options & F_ENCRYPT) {
optval = IPSEC_LEVEL_REQUIRE;
if (setsockopt(s, IPPROTO_IPV6, IPV6_ESP_TRANS_LEVEL,
&optval, sizeof(optval)) == -1)
err(1, "setsockopt(IPV6_ESP_TRANS_LEVEL)");
}
#endif /*IPSEC_POLICY_IPSEC*/
#endif
#ifdef ICMP6_FILTER
{
struct icmp6_filter filt;
if (!(options & F_VERBOSE)) {
ICMP6_FILTER_SETBLOCKALL(&filt);
if ((options & F_FQDN) || (options & F_FQDNOLD) ||
(options & F_NODEADDR) || (options & F_SUPTYPES))
ICMP6_FILTER_SETPASS(ICMP6_NI_REPLY, &filt);
else
ICMP6_FILTER_SETPASS(ICMP6_ECHO_REPLY, &filt);
} else {
ICMP6_FILTER_SETPASSALL(&filt);
}
if (setsockopt(s, IPPROTO_ICMPV6, ICMP6_FILTER, &filt,
sizeof(filt)) < 0)
err(1, "setsockopt(ICMP6_FILTER)");
}
#endif /*ICMP6_FILTER*/
/* let the kerel pass extension headers of incoming packets */
if ((options & F_VERBOSE) != 0) {
int opton = 1;
#ifdef IPV6_RECVRTHDR
if (setsockopt(s, IPPROTO_IPV6, IPV6_RECVRTHDR, &opton,
sizeof(opton)))
err(1, "setsockopt(IPV6_RECVRTHDR)");
#else /* old adv. API */
if (setsockopt(s, IPPROTO_IPV6, IPV6_RTHDR, &opton,
sizeof(opton)))
err(1, "setsockopt(IPV6_RTHDR)");
#endif
}
/*
optval = 1;
if (IN6_IS_ADDR_MULTICAST(&dst.sin6_addr))
if (setsockopt(s, IPPROTO_IPV6, IPV6_MULTICAST_LOOP,
&optval, sizeof(optval)) == -1)
err(1, "IPV6_MULTICAST_LOOP");
*/
/* Specify the outgoing interface and/or the source address */
if (usepktinfo)
ip6optlen += CMSG_SPACE(sizeof(struct in6_pktinfo));
if (hoplimit != -1)
ip6optlen += CMSG_SPACE(sizeof(int));
#ifdef IPV6_REACHCONF
if (options & F_REACHCONF)
ip6optlen += CMSG_SPACE(0);
#endif
/* set IP6 packet options */
if (ip6optlen) {
if ((scmsg = (char *)malloc(ip6optlen)) == 0)
errx(1, "can't allocate enough memory");
smsghdr.msg_control = (caddr_t)scmsg;
smsghdr.msg_controllen = ip6optlen;
scmsgp = (struct cmsghdr *)scmsg;
}
if (usepktinfo) {
pktinfo = (struct in6_pktinfo *)(CMSG_DATA(scmsgp));
memset(pktinfo, 0, sizeof(*pktinfo));
scmsgp->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo));
scmsgp->cmsg_level = IPPROTO_IPV6;
scmsgp->cmsg_type = IPV6_PKTINFO;
scmsgp = CMSG_NXTHDR(&smsghdr, scmsgp);
}
/* set the outgoing interface */
if (ifname) {
#ifndef USE_SIN6_SCOPE_ID
/* pktinfo must have already been allocated */
if ((pktinfo->ipi6_ifindex = if_nametoindex(ifname)) == 0)
errx(1, "%s: invalid interface name", ifname);
#else
if ((dst.sin6_scope_id = if_nametoindex(ifname)) == 0)
errx(1, "%s: invalid interface name", ifname);
#endif
}
if (hoplimit != -1) {
scmsgp->cmsg_len = CMSG_LEN(sizeof(int));
scmsgp->cmsg_level = IPPROTO_IPV6;
scmsgp->cmsg_type = IPV6_HOPLIMIT;
*(int *)(CMSG_DATA(scmsgp)) = hoplimit;
scmsgp = CMSG_NXTHDR(&smsghdr, scmsgp);
}
#ifdef IPV6_REACHCONF
if (options & F_REACHCONF) {
scmsgp->cmsg_len = CMSG_LEN(0);
scmsgp->cmsg_level = IPPROTO_IPV6;
scmsgp->cmsg_type = IPV6_REACHCONF;
scmsgp = CMSG_NXTHDR(&smsghdr, scmsgp);
}
#endif
if (argc > 1) { /* some intermediate addrs are specified */
#ifdef ANDROID_INCLUDE_RTHDR_SUPPORT
int hops, error;
int rthdrlen;
rthdrlen = inet6_rth_space(IPV6_RTHDR_TYPE_0, argc - 1);
scmsgp->cmsg_len = CMSG_LEN(rthdrlen);
scmsgp->cmsg_level = IPPROTO_IPV6;
scmsgp->cmsg_type = IPV6_RTHDR;
rthdr = (struct ip6_rthdr *)CMSG_DATA(scmsgp);
rthdr = inet6_rth_init((void *)rthdr, rthdrlen,
IPV6_RTHDR_TYPE_0, argc - 1);
if (rthdr == NULL)
errx(1, "can't initialize rthdr");
for (hops = 0; hops < argc - 1; hops++) {
struct addrinfo *iaip;
if ((error = getaddrinfo(argv[hops], NULL, &hints,
&iaip)))
errx(1, "%s", gai_strerror(error));
if (SIN6(iaip->ai_addr)->sin6_family != AF_INET6)
errx(1,
"bad addr family of an intermediate addr");
if (inet6_rth_add(rthdr,
&(SIN6(iaip->ai_addr))->sin6_addr))
errx(1, "can't add an intermediate node");
freeaddrinfo(iaip);
}
scmsgp = CMSG_NXTHDR(&smsghdr, scmsgp);
#else
errx(1, "compiled without support for routing headers");
#endif
}
if (!(options & F_SRCADDR)) {
/*
* get the source address. XXX since we revoked the root
* privilege, we cannot use a raw socket for this.
*/
int dummy;
socklen_t len = sizeof(src);
if ((dummy = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
err(1, "UDP socket");
src.sin6_family = AF_INET6;
src.sin6_addr = dst.sin6_addr;
src.sin6_port = ntohs(DUMMY_PORT);
src.sin6_scope_id = dst.sin6_scope_id;
if (pktinfo &&
setsockopt(dummy, IPPROTO_IPV6, IPV6_PKTINFO,
(void *)pktinfo, sizeof(*pktinfo)))
err(1, "UDP setsockopt(IPV6_PKTINFO)");
if (hoplimit != -1 &&
setsockopt(dummy, IPPROTO_IPV6, IPV6_UNICAST_HOPS,
(void *)&hoplimit, sizeof(hoplimit)))
err(1, "UDP setsockopt(IPV6_UNICAST_HOPS)");
if (hoplimit != -1 &&
setsockopt(dummy, IPPROTO_IPV6, IPV6_MULTICAST_HOPS,
(void *)&hoplimit, sizeof(hoplimit)))
err(1, "UDP setsockopt(IPV6_MULTICAST_HOPS)");
#ifdef ANDROID_INCLUDE_RTHDR_SUPPORT
if (rthdr &&
setsockopt(dummy, IPPROTO_IPV6, IPV6_RTHDR,
(void *)rthdr, (rthdr->ip6r_len + 1) << 3))
err(1, "UDP setsockopt(IPV6_RTHDR)");
#endif