forked from dgibson/iputils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathping6.c
1861 lines (1615 loc) · 43.4 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
/*
*
* Modified for AF_INET6 by Pedro Roque
*
*
* Original copyright notice included bellow
*/
/*
* Copyright (c) 1989 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. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. 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.
*/
#ifndef lint
char copyright[] =
"@(#) Copyright (c) 1989 The Regents of the University of California.\n\
All rights reserved.\n";
#endif /* not lint */
/*
* P I N G . C
*
* 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.
*/
#include "ping_common.h"
#include <linux/filter.h>
#include <netinet/ip6.h>
#include <netinet/icmp6.h>
#include <resolv.h>
#ifndef WITHOUT_IFADDRS
#include <ifaddrs.h>
#endif
#ifdef USE_IDN
#include <stringprep.h>
#endif
#include "ping6_niquery.h"
#include "in6_flowlabel.h"
#ifndef SOL_IPV6
#define SOL_IPV6 IPPROTO_IPV6
#endif
#ifndef SOL_ICMPV6
#define SOL_ICMPV6 IPPROTO_ICMPV6
#endif
/* RFC3542 */
#ifndef ICMP6_DST_UNREACH_BEYONDSCOPE
#define ICMP6_DST_UNREACH_BEYONDSCOPE ICMP6_DST_UNREACH_NOTNEIGHBOR
#endif
#if defined(ENABLE_PING6_RTHDR) && !defined(ENABLE_PING6_RTHDR_RFC3542)
#ifndef IPV6_SRCRT_TYPE_0
#define IPV6_SRCRT_TYPE_0 0
#endif
#endif
#ifndef MLD_LISTENER_QUERY
#define MLD_LISTENER_QUERY 130
#define MLD_LISTENER_REPORT 131
#define MLD_LISTENER_REDUCTION 132
#endif
#define BIT_CLEAR(nr, addr) do { ((__u32 *)(addr))[(nr) >> 5] &= ~(1U << ((nr) & 31)); } while(0)
#define BIT_SET(nr, addr) do { ((__u32 *)(addr))[(nr) >> 5] |= (1U << ((nr) & 31)); } while(0)
#define BIT_TEST(nr, addr) do { (__u32 *)(addr))[(nr) >> 5] & (1U << ((nr) & 31)); } while(0)
#ifndef ICMP6_FILTER_WILLPASS
#define ICMP6_FILTER_WILLPASS(type, filterp) \
(BIT_TEST((type), filterp) == 0)
#define ICMP6_FILTER_WILLBLOCK(type, filterp) \
BIT_TEST((type), filterp)
#define ICMP6_FILTER_SETPASS(type, filterp) \
BIT_CLEAR((type), filterp)
#define ICMP6_FILTER_SETBLOCK(type, filterp) \
BIT_SET((type), filterp)
#define ICMP6_FILTER_SETPASSALL(filterp) \
memset(filterp, 0, sizeof(struct icmp6_filter));
#define ICMP6_FILTER_SETBLOCKALL(filterp) \
memset(filterp, 0xFF, sizeof(struct icmp6_filter));
#endif
#define MAXPACKET 128000 /* max packet size */
#ifdef SO_TIMESTAMP
#define HAVE_SIN6_SCOPEID 1
#endif
#ifndef SCOPE_DELIMITER
# define SCOPE_DELIMITER '%'
#endif
__u32 flowlabel;
__u32 tclass;
#ifdef ENABLE_PING6_RTHDR
struct cmsghdr *srcrt;
#endif
struct sockaddr_in6 whereto; /* who to ping */
u_char outpack[MAXPACKET];
int maxpacket = sizeof(outpack);
static unsigned char cmsgbuf[4096];
static int cmsglen = 0;
static char * pr_addr(struct in6_addr *addr);
static char * pr_addr_n(struct in6_addr *addr);
static int pr_icmph(__u8 type, __u8 code, __u32 info);
static void usage(void) __attribute((noreturn));
struct sockaddr_in6 source;
char *device;
int pmtudisc=-1;
static int icmp_sock;
#ifdef USE_GNUTLS
# include <gnutls/openssl.h>
#else
# include <openssl/md5.h>
#endif
/* Node Information query */
int ni_query = -1;
int ni_flag = 0;
void *ni_subject = NULL;
int ni_subject_len = 0;
int ni_subject_type = -1;
char *ni_group;
static inline int ntohsp(__u16 *p)
{
__u16 v;
memcpy(&v, p, sizeof(v));
return ntohs(v);
}
#if defined(ENABLE_PING6_RTHDR) && !defined(ENABLE_PING6_RTHDR_RFC3542)
size_t inet6_srcrt_space(int type, int segments)
{
if (type != 0 || segments > 24)
return 0;
return (sizeof(struct cmsghdr) + sizeof(struct ip6_rthdr0) +
segments * sizeof(struct in6_addr));
}
extern struct cmsghdr * inet6_srcrt_init(void *bp, int type)
{
struct cmsghdr *cmsg;
if (type)
return NULL;
memset(bp, 0, sizeof(struct cmsghdr) + sizeof(struct ip6_rthdr0));
cmsg = (struct cmsghdr *) bp;
cmsg->cmsg_len = sizeof(struct cmsghdr) + sizeof(struct ip6_rthdr0);
cmsg->cmsg_level = SOL_IPV6;
cmsg->cmsg_type = IPV6_RTHDR;
return cmsg;
}
int inet6_srcrt_add(struct cmsghdr *cmsg, const struct in6_addr *addr)
{
struct ip6_rthdr0 *hdr;
hdr = (struct ip6_rthdr0 *) CMSG_DATA(cmsg);
cmsg->cmsg_len += sizeof(struct in6_addr);
hdr->ip6r0_len += sizeof(struct in6_addr) / 8;
memcpy(&hdr->ip6r0_addr[hdr->ip6r0_segleft++], addr,
sizeof(struct in6_addr));
return 0;
}
#endif
unsigned int if_name2index(const char *ifname)
{
unsigned int i = if_nametoindex(ifname);
if (!i) {
fprintf(stderr, "ping: unknown iface %s\n", ifname);
exit(2);
}
return i;
}
struct niquery_option {
char *name;
int namelen;
int has_arg;
int data;
int (*handler)(int index, const char *arg);
};
#define NIQUERY_OPTION(_name, _has_arg, _data, _handler) \
{ \
.name = _name, \
.namelen = sizeof(_name) - 1, \
.has_arg = _has_arg, \
.data = _data, \
.handler = _handler \
}
static int niquery_option_name_handler(int index, const char *arg);
static int niquery_option_ipv6_handler(int index, const char *arg);
static int niquery_option_ipv6_flag_handler(int index, const char *arg);
static int niquery_option_ipv4_handler(int index, const char *arg);
static int niquery_option_ipv4_flag_handler(int index, const char *arg);
static int niquery_option_subject_addr_handler(int index, const char *arg);
static int niquery_option_subject_name_handler(int index, const char *arg);
static int niquery_option_help_handler(int index, const char *arg);
struct niquery_option niquery_options[] = {
NIQUERY_OPTION("name", 0, 0, niquery_option_name_handler),
NIQUERY_OPTION("fqdn", 0, 0, niquery_option_name_handler),
NIQUERY_OPTION("ipv6", 0, 0, niquery_option_ipv6_handler),
NIQUERY_OPTION("ipv6-all", 0, NI_IPV6ADDR_F_ALL, niquery_option_ipv6_flag_handler),
NIQUERY_OPTION("ipv6-compatible", 0, NI_IPV6ADDR_F_COMPAT, niquery_option_ipv6_flag_handler),
NIQUERY_OPTION("ipv6-linklocal", 0, NI_IPV6ADDR_F_LINKLOCAL, niquery_option_ipv6_flag_handler),
NIQUERY_OPTION("ipv6-sitelocal", 0, NI_IPV6ADDR_F_SITELOCAL, niquery_option_ipv6_flag_handler),
NIQUERY_OPTION("ipv6-global", 0, NI_IPV6ADDR_F_GLOBAL, niquery_option_ipv6_flag_handler),
NIQUERY_OPTION("ipv4", 0, 0, niquery_option_ipv4_handler),
NIQUERY_OPTION("ipv4-all", 0, NI_IPV4ADDR_F_ALL, niquery_option_ipv4_flag_handler),
NIQUERY_OPTION("subject-ipv6", 1, NI_SUBJ_IPV6, niquery_option_subject_addr_handler),
NIQUERY_OPTION("subject-ipv4", 1, NI_SUBJ_IPV4, niquery_option_subject_addr_handler),
NIQUERY_OPTION("subject-name", 1, 0, niquery_option_subject_name_handler),
NIQUERY_OPTION("subject-fqdn", 1, -1, niquery_option_subject_name_handler),
NIQUERY_OPTION("help", 0, 0, niquery_option_help_handler),
{},
};
static inline int niquery_is_enabled(void)
{
return ni_query >= 0;
}
#if PING6_NONCE_MEMORY
__u8 *ni_nonce_ptr;
#else
struct {
struct timeval tv;
pid_t pid;
} ni_nonce_secret;
#endif
static void niquery_init_nonce(void)
{
#if PING6_NONCE_MEMORY
struct timeval tv;
unsigned long seed;
seed = (unsigned long)getpid();
if (!gettimeofday(&tv, NULL))
seed ^= tv.tv_usec;
srand(seed);
ni_nonce_ptr = calloc(NI_NONCE_SIZE, MAX_DUP_CHK);
if (!ni_nonce_ptr) {
perror("ping6: calloc");
exit(2);
}
ni_nonce_ptr[0] = ~0;
#else
gettimeofday(&ni_nonce_secret.tv, NULL);
ni_nonce_secret.pid = getpid();
#endif
}
#if !PING6_NONCE_MEMORY
static int niquery_nonce(__u8 *nonce, int fill)
{
static __u8 digest[MD5_DIGEST_LENGTH];
static int seq = -1;
if (fill || seq != *(__u16 *)nonce || seq < 0) {
MD5_CTX ctxt;
MD5_Init(&ctxt);
MD5_Update(&ctxt, &ni_nonce_secret, sizeof(ni_nonce_secret));
MD5_Update(&ctxt, nonce, sizeof(__u16));
MD5_Final(digest, &ctxt);
seq = *(__u16 *)nonce;
}
if (fill) {
memcpy(nonce + sizeof(__u16), digest, NI_NONCE_SIZE - sizeof(__u16));
return 0;
} else {
if (memcmp(nonce + sizeof(__u16), digest, NI_NONCE_SIZE - sizeof(__u16)))
return -1;
return ntohsp((__u16 *)nonce);
}
}
#endif
static inline void niquery_fill_nonce(__u16 seq, __u8 *nonce)
{
__u16 v = htons(seq);
#if PING6_NONCE_MEMORY
int i;
memcpy(&ni_nonce_ptr[NI_NONCE_SIZE * (seq % MAX_DUP_CHK)], &v, sizeof(v));
for (i = sizeof(v); i < NI_NONCE_SIZE; i++)
ni_nonce_ptr[NI_NONCE_SIZE * (seq % MAX_DUP_CHK) + i] = 0x100 * (rand() / (RAND_MAX + 1.0));
memcpy(nonce, &ni_nonce_ptr[NI_NONCE_SIZE * (seq % MAX_DUP_CHK)], NI_NONCE_SIZE);
#else
memcpy(nonce, &v, sizeof(v));
niquery_nonce(nonce, 1);
#endif
}
static inline int niquery_check_nonce(__u8 *nonce)
{
#if PING6_NONCE_MEMORY
__u16 seq = ntohsp((__u16 *)nonce);
if (memcmp(nonce, &ni_nonce_ptr[NI_NONCE_SIZE * (seq % MAX_DUP_CHK)], NI_NONCE_SIZE))
return -1;
return seq;
#else
return niquery_nonce(nonce, 0);
#endif
}
static int niquery_set_qtype(int type)
{
if (niquery_is_enabled() && ni_query != type) {
printf("Qtype conflict\n");
return -1;
}
ni_query = type;
return 0;
}
static int niquery_option_name_handler(int index, const char *arg)
{
if (niquery_set_qtype(NI_QTYPE_NAME) < 0)
return -1;
return 0;
}
static int niquery_option_ipv6_handler(int index, const char *arg)
{
if (niquery_set_qtype(NI_QTYPE_IPV6ADDR) < 0)
return -1;
return 0;
}
static int niquery_option_ipv6_flag_handler(int index, const char *arg)
{
if (niquery_set_qtype(NI_QTYPE_IPV6ADDR) < 0)
return -1;
ni_flag |= niquery_options[index].data;
return 0;
}
static int niquery_option_ipv4_handler(int index, const char *arg)
{
if (niquery_set_qtype(NI_QTYPE_IPV4ADDR) < 0)
return -1;
return 0;
}
static int niquery_option_ipv4_flag_handler(int index, const char *arg)
{
if (niquery_set_qtype(NI_QTYPE_IPV4ADDR) < 0)
return -1;
ni_flag |= niquery_options[index].data;
return 0;
}
static inline int niquery_is_subject_valid(void)
{
return ni_subject_type >= 0 && ni_subject;
}
static int niquery_set_subject_type(int type)
{
if (niquery_is_subject_valid() && ni_subject_type != type) {
printf("Subject type conflict\n");
return -1;
}
ni_subject_type = type;
return 0;
}
#define ARRAY_SIZE(array) (sizeof(array) / sizeof(array[0]))
#define OFFSET_OF(type,elem) ((size_t)&((type *)0)->elem)
static int niquery_option_subject_addr_handler(int index, const char *arg)
{
struct addrinfo hints, *ai0, *ai;
int offset;
int gai;
if (niquery_set_subject_type(niquery_options[index].data) < 0)
return -1;
ni_subject_type = niquery_options[index].data;
memset(&hints, 0, sizeof(hints));
switch (niquery_options[index].data) {
case NI_SUBJ_IPV6:
ni_subject_len = sizeof(struct in6_addr);
offset = OFFSET_OF(struct sockaddr_in6, sin6_addr);
hints.ai_family = AF_INET6;
break;
case NI_SUBJ_IPV4:
ni_subject_len = sizeof(struct in_addr);
offset = OFFSET_OF(struct sockaddr_in, sin_addr);
hints.ai_family = AF_INET;
break;
default:
/* should not happen. */
offset = -1;
}
hints.ai_socktype = SOCK_DGRAM;
#ifdef USE_IDN
hints.ai_flags = AI_IDN;
#endif
gai = getaddrinfo(arg, 0, &hints, &ai0);
if (gai) {
fprintf(stderr, "Unknown host: %s\n", arg);
return -1;
}
for (ai = ai0; ai; ai = ai->ai_next) {
void *p = malloc(ni_subject_len);
if (!p)
continue;
memcpy(p, (__u8 *)ai->ai_addr + offset, ni_subject_len);
free(ni_subject);
ni_subject = p;
break;
}
freeaddrinfo(ai0);
return 0;
}
static int niquery_option_subject_name_handler(int index, const char *arg)
{
static char nigroup_buf[INET6_ADDRSTRLEN + 1 + IFNAMSIZ];
unsigned char *dnptrs[2], **dpp, **lastdnptr;
int n;
int i;
char *name, *p;
char *canonname = NULL, *idn = NULL;
unsigned char *buf = NULL;
size_t namelen;
size_t buflen;
int dots, fqdn = niquery_options[index].data;
MD5_CTX ctxt;
__u8 digest[MD5_DIGEST_LENGTH];
#ifdef USE_IDN
int rc;
#endif
if (niquery_set_subject_type(NI_SUBJ_NAME) < 0)
return -1;
#ifdef USE_IDN
name = stringprep_locale_to_utf8(arg);
if (!name) {
fprintf(stderr, "ping6: IDN support failed.\n");
exit(2);
}
#else
name = strdup(arg);
if (!name)
goto oomexit;
#endif
p = strchr(name, SCOPE_DELIMITER);
if (p) {
*p = '\0';
if (strlen(p + 1) >= IFNAMSIZ) {
fprintf(stderr, "ping6: too long scope name.\n");
exit(1);
}
}
#ifdef USE_IDN
rc = idna_to_ascii_8z(name, &idn, 0);
if (rc) {
fprintf(stderr, "ping6: IDN encoding error: %s\n",
idna_strerror(rc));
exit(2);
}
#else
idn = strdup(name);
if (!idn)
goto oomexit;
#endif
namelen = strlen(idn);
canonname = malloc(namelen + 1);
if (!canonname)
goto oomexit;
dots = 0;
for (i = 0; i < namelen + 1; i++) {
canonname[i] = isupper(idn[i]) ? tolower(idn[i]) : idn[i];
if (idn[i] == '.')
dots++;
}
if (fqdn == 0) {
/* guess if hostname is FQDN */
fqdn = dots ? 1 : -1;
}
buflen = namelen + 3 + 1; /* dn_comp() requrires strlen() + 3,
plus non-fqdn indicator. */
buf = malloc(buflen);
if (!buf) {
fprintf(stderr, "ping6: out of memory.\n");
goto errexit;
}
dpp = dnptrs;
lastdnptr = &dnptrs[ARRAY_SIZE(dnptrs)];
*dpp++ = (unsigned char *)buf;
*dpp++ = NULL;
n = dn_comp(canonname, (unsigned char *)buf, buflen, dnptrs, lastdnptr);
if (n < 0) {
fprintf(stderr, "ping6: Inappropriate subject name: %s\n", canonname);
goto errexit;
} else if (n >= buflen) {
fprintf(stderr, "ping6: dn_comp() returned too long result.\n");
goto errexit;
}
MD5_Init(&ctxt);
MD5_Update(&ctxt, buf, buf[0]);
MD5_Final(digest, &ctxt);
sprintf(nigroup_buf, "ff02::2:%02x%02x:%02x%02x%s%s",
digest[0], digest[1], digest[2], digest[3],
p ? "%" : "",
p ? p + 1 : "");
if (fqdn < 0)
buf[n] = 0;
free(ni_subject);
ni_group = nigroup_buf;
ni_subject = buf;
ni_subject_len = n + (fqdn < 0);
ni_group = nigroup_buf;
free(canonname);
free(idn);
free(name);
return 0;
oomexit:
fprintf(stderr, "ping6: out of memory.\n");
errexit:
free(buf);
free(canonname);
free(idn);
free(name);
exit(1);
}
int niquery_option_help_handler(int index, const char *arg)
{
fprintf(stderr, "ping6 -N suboptions\n"
"\tHelp:\n"
"\t\thelp\n"
"\tQuery:\n"
"\t\tname,\n"
"\t\tipv6,ipv6-all,ipv6-compatible,ipv6-linklocal,ipv6-sitelocal,ipv6-global,\n"
"\t\tipv4,ipv4-all,\n"
"\tSubject:\n"
"\t\tsubject-ipv6=addr,subject-ipv4=addr,subject-name=name,subject-fqdn=name,\n"
);
exit(2);
}
int niquery_option_handler(const char *opt_arg)
{
struct niquery_option *p;
int i;
int ret = -1;
for (i = 0, p = niquery_options; p->name; i++, p++) {
if (strncmp(p->name, opt_arg, p->namelen))
continue;
if (!p->has_arg) {
if (opt_arg[p->namelen] == '\0') {
ret = p->handler(i, NULL);
if (ret >= 0)
break;
}
} else {
if (opt_arg[p->namelen] == '=') {
ret = p->handler(i, &opt_arg[p->namelen] + 1);
if (ret >= 0)
break;
}
}
}
if (!p->name)
ret = niquery_option_help_handler(0, NULL);
return ret;
}
static int hextoui(const char *str)
{
unsigned long val;
char *ep;
errno = 0;
val = strtoul(str, &ep, 16);
if (*ep) {
if (!errno)
errno = EINVAL;
return -1;
}
if (val > UINT_MAX) {
errno = ERANGE;
return UINT_MAX;
}
return val;
}
int main(int argc, char *argv[])
{
int ch, hold, packlen;
u_char *packet;
char *target;
struct addrinfo hints, *ai;
int gai;
struct sockaddr_in6 firsthop;
int socket_errno;
struct icmp6_filter filter;
int err;
#ifdef __linux__
int csum_offset, sz_opt;
#endif
static uint32_t scope_id = 0;
limit_capabilities();
#ifdef USE_IDN
setlocale(LC_ALL, "");
#endif
enable_capability_raw();
icmp_sock = socket(AF_INET6, SOCK_RAW, IPPROTO_ICMPV6);
socket_errno = errno;
disable_capability_raw();
source.sin6_family = AF_INET6;
memset(&firsthop, 0, sizeof(firsthop));
firsthop.sin6_family = AF_INET6;
preload = 1;
while ((ch = getopt(argc, argv, COMMON_OPTSTR "F:N:")) != EOF) {
switch(ch) {
case 'F':
flowlabel = hextoui(optarg);
if (errno || (flowlabel & ~IPV6_FLOWINFO_FLOWLABEL)) {
fprintf(stderr, "ping: Invalid flowinfo %s\n", optarg);
exit(2);
}
options |= F_FLOWINFO;
break;
case 'Q':
tclass = hextoui(optarg);
if (errno || (tclass & ~0xff)) {
fprintf(stderr, "ping: Invalid tclass %s\n", optarg);
exit(2);
}
options |= F_TCLASS;
break;
case 'I':
if (strchr(optarg, ':')) {
char *p, *addr = strdup(optarg);
if (!addr) {
fprintf(stderr, "ping: out of memory\n");
exit(2);
}
p = strchr(addr, SCOPE_DELIMITER);
if (p) {
*p = '\0';
device = optarg + (p - addr) + 1;
}
if (inet_pton(AF_INET6, addr, (char*)&source.sin6_addr) <= 0) {
fprintf(stderr, "ping: invalid source address %s\n", optarg);
exit(2);
}
options |= F_STRICTSOURCE;
free(addr);
} else {
device = optarg;
}
break;
case 'M':
if (strcmp(optarg, "do") == 0)
pmtudisc = IPV6_PMTUDISC_DO;
else if (strcmp(optarg, "dont") == 0)
pmtudisc = IPV6_PMTUDISC_DONT;
else if (strcmp(optarg, "want") == 0)
pmtudisc = IPV6_PMTUDISC_WANT;
else {
fprintf(stderr, "ping: wrong value for -M: do, dont, want are valid ones.\n");
exit(2);
}
break;
case 'V':
printf("ping6 utility, iputils-%s\n", SNAPSHOT);
exit(0);
case 'N':
if (niquery_option_handler(optarg) < 0) {
usage();
break;
}
break;
COMMON_OPTIONS
common_options(ch);
break;
default:
usage();
}
}
argc -= optind;
argv += optind;
#ifdef ENABLE_PING6_RTHDR
while (argc > 1) {
struct in6_addr *addr;
if (srcrt == NULL) {
int space;
fprintf(stderr, "ping6: Warning: "
"Source routing is deprecated by RFC5095.\n");
#ifdef ENABLE_PING6_RTHDR_RFC3542
space = inet6_rth_space(IPV6_RTHDR_TYPE_0, argc - 1);
#else
space = inet6_srcrt_space(IPV6_SRCRT_TYPE_0, argc - 1);
#endif
if (space == 0) {
fprintf(stderr, "srcrt_space failed\n");
exit(2);
}
#ifdef ENABLE_PING6_RTHDR_RFC3542
if (cmsglen + CMSG_SPACE(space) > sizeof(cmsgbuf)) {
fprintf(stderr, "no room for options\n");
exit(2);
}
#else
if (space + cmsglen > sizeof(cmsgbuf)) {
fprintf(stderr, "no room for options\n");
exit(2);
}
#endif
srcrt = (struct cmsghdr*)(cmsgbuf+cmsglen);
#ifdef ENABLE_PING6_RTHDR_RFC3542
memset(srcrt, 0, CMSG_SPACE(0));
srcrt->cmsg_len = CMSG_LEN(space);
srcrt->cmsg_level = IPPROTO_IPV6;
srcrt->cmsg_type = IPV6_RTHDR;
inet6_rth_init(CMSG_DATA(srcrt), space, IPV6_RTHDR_TYPE_0, argc - 1);
cmsglen += CMSG_SPACE(space);
#else
cmsglen += CMSG_ALIGN(space);
inet6_srcrt_init(srcrt, IPV6_SRCRT_TYPE_0);
#endif
}
target = *argv;
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_INET6;
#ifdef USE_IDN
hints.ai_flags = AI_IDN;
#endif
gai = getaddrinfo(target, NULL, &hints, &ai);
if (gai) {
fprintf(stderr, "unknown host\n");
exit(2);
}
addr = &((struct sockaddr_in6 *)(ai->ai_addr))->sin6_addr;
#ifdef ENABLE_PING6_RTHDR_RFC3542
inet6_rth_add(CMSG_DATA(srcrt), addr);
#else
inet6_srcrt_add(srcrt, addr);
#endif
if (IN6_IS_ADDR_UNSPECIFIED(&firsthop.sin6_addr)) {
memcpy(&firsthop.sin6_addr, addr, 16);
#ifdef HAVE_SIN6_SCOPEID
firsthop.sin6_scope_id = ((struct sockaddr_in6 *)(ai->ai_addr))->sin6_scope_id;
/* Verify scope_id is the same as previous nodes */
if (firsthop.sin6_scope_id && scope_id && firsthop.sin6_scope_id != scope_id) {
fprintf(stderr, "scope discrepancy among the nodes\n");
exit(2);
} else if (!scope_id) {
scope_id = firsthop.sin6_scope_id;
}
#endif
}
freeaddrinfo(ai);
argv++;
argc--;
}
#endif
if (niquery_is_enabled()) {
niquery_init_nonce();
if (!niquery_is_subject_valid()) {
ni_subject = &whereto.sin6_addr;
ni_subject_len = sizeof(whereto.sin6_addr);
ni_subject_type = NI_SUBJ_IPV6;
}
}
if (argc > 1) {
#ifndef ENABLE_PING6_RTHDR
fprintf(stderr, "ping6: Source routing is deprecated by RFC5095.\n");
#endif
usage();
} else if (argc == 1) {
target = *argv;
} else {
if (ni_query < 0 && ni_subject_type != NI_SUBJ_NAME)
usage();
target = ni_group;
}
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_INET6;
#ifdef USE_IDN
hints.ai_flags = AI_IDN;
#endif
gai = getaddrinfo(target, NULL, &hints, &ai);
if (gai) {
fprintf(stderr, "unknown host\n");
exit(2);
}
memcpy(&whereto, ai->ai_addr, sizeof(whereto));
whereto.sin6_port = htons(IPPROTO_ICMPV6);
if (memchr(target, ':', strlen(target)))
options |= F_NUMERIC;
freeaddrinfo(ai);
if (IN6_IS_ADDR_UNSPECIFIED(&firsthop.sin6_addr)) {
memcpy(&firsthop.sin6_addr, &whereto.sin6_addr, 16);
#ifdef HAVE_SIN6_SCOPEID
firsthop.sin6_scope_id = whereto.sin6_scope_id;
/* Verify scope_id is the same as intermediate nodes */
if (firsthop.sin6_scope_id && scope_id && firsthop.sin6_scope_id != scope_id) {
fprintf(stderr, "scope discrepancy among the nodes\n");
exit(2);
} else if (!scope_id) {
scope_id = firsthop.sin6_scope_id;
}
#endif
}
hostname = target;
if (IN6_IS_ADDR_UNSPECIFIED(&source.sin6_addr)) {
socklen_t alen;
int probe_fd = socket(AF_INET6, SOCK_DGRAM, 0);
if (probe_fd < 0) {
perror("socket");
exit(2);
}
if (device) {
#if defined(IPV6_RECVPKTINFO) || defined(HAVE_SIN6_SCOPEID)
unsigned int iface = if_name2index(device);
#endif
#ifdef IPV6_RECVPKTINFO
struct in6_pktinfo ipi;
memset(&ipi, 0, sizeof(ipi));
ipi.ipi6_ifindex = iface;
#endif
#ifdef HAVE_SIN6_SCOPEID
if (IN6_IS_ADDR_LINKLOCAL(&firsthop.sin6_addr) ||
IN6_IS_ADDR_MC_LINKLOCAL(&firsthop.sin6_addr))
firsthop.sin6_scope_id = iface;
#endif
enable_capability_raw();
if (
#ifdef IPV6_RECVPKTINFO
setsockopt(probe_fd, IPPROTO_IPV6, IPV6_PKTINFO, &ipi, sizeof(ipi)) == -1 &&
#endif
setsockopt(probe_fd, SOL_SOCKET, SO_BINDTODEVICE, device, strlen(device)+1) == -1) {
perror("setsockopt(SO_BINDTODEVICE)");
exit(2);
}
disable_capability_raw();
}
firsthop.sin6_port = htons(1025);
if (connect(probe_fd, (struct sockaddr*)&firsthop, sizeof(firsthop)) == -1) {
perror("connect");
exit(2);
}
alen = sizeof(source);
if (getsockname(probe_fd, (struct sockaddr*)&source, &alen) == -1) {
perror("getsockname");
exit(2);
}
source.sin6_port = 0;
close(probe_fd);
#ifndef WITHOUT_IFADDRS
if (device) {
struct ifaddrs *ifa0, *ifa;
if (getifaddrs(&ifa0)) {
perror("getifaddrs");
exit(2);
}