-
Notifications
You must be signed in to change notification settings - Fork 64
/
Copy patharping.c
2691 lines (2491 loc) · 82.5 KB
/
arping.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
/** arping/src/arping.c
*
* arping
*
* By Thomas Habets <thomas@habets.se>
*
* ARP 'ping' utility
*
* Broadcasts a who-has ARP packet on the network and prints answers.
* *VERY* useful when you are trying to pick an unused IP for a net that
* you don't yet have routing to. Then again, if you have no idea what I'm
* talking about then you prolly don't need it.
*
* Also finds out IP of specified MAC.
*
*/
/*
* Copyright (C) 2000-2024 Thomas Habets <thomas@habets.se>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#define _GNU_SOURCE
#if HAVE_CONFIG_H
#include "config.h"
#endif
#include <assert.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <math.h>
#include <poll.h>
#if HAVE_UNISTD_H
#include <unistd.h>
#endif
#if HAVE_GETOPT_H
#include <getopt.h>
#endif
#if HAVE_STDINT_H
#include <stdint.h>
#endif
#if HAVE_INTTYPES_H
#include <inttypes.h>
#endif
#if HAVE_TIME_H
#include <time.h>
#endif
#if HAVE_SYS_TIME_H
#include <sys/time.h>
#endif
#if HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
#if HAVE_SYS_PARAM_H
#include <sys/param.h>
#endif
#if HAVE_SYS_RANDOM_H
#include <sys/random.h>
#endif
#if HAVE_GRP_H
#include <grp.h>
#endif
#if HAVE_SYS_SOCKET_H
#include <sys/socket.h>
#endif
#if HAVE_NETINET_IN_H
#include <netinet/in.h>
#endif
#if HAVE_ARPA_INET_H
#include <arpa/inet.h>
#endif
#if HAVE_LIBNET_H
#include <libnet.h>
#endif
#if HAVE_WIN32_LIBNET_H
#include <win32/libnet.h>
#endif
#if HAVE_PWD_H
#include <pwd.h>
#endif
#if HAVE_SYS_CAPABILITY_H
#include <sys/capability.h>
#else
// It seems that some environments have a libc with cap_init, but do not have
// the header files. Without the header files we won't have the cap_t struct, so
// it won't actually work.
//
// TODO: Probably this logic should be in configure.ac instead.
#undef HAVE_CAP_INIT
#endif
#if HAVE_NET_BPF_H
#include <net/bpf.h>
#endif
#include <pcap.h>
#include "arping.h"
#include "cast.h"
#ifndef ETH_ALEN
#define ETH_ALEN 6
#endif
#ifndef IP_ALEN
#define IP_ALEN 4
#endif
#ifndef WIN32
#define WIN32 0
#endif
#ifndef CLOCK_MONOTONIC
#define CLOCK_MONOTONIC CLOCK_REALTIME
#endif
#define UNUSED(x) (void)(x)
#ifndef PCAP_NETMASK_UNKNOWN
#define PCAP_NETMASK_UNKNOWN 0xffffffff
#endif
/**
* OS-specific interface finding using routing table. See findif_*.c
* ebuf must be called with a size of at least
* max(LIBNET_ERRBUF_SIZE, PCAP_ERRBUF_SIZE).
*/
const char *
arping_lookupdev(uint32_t srcip, uint32_t dstip, char *ebuf);
const char *
arping_lookupdev_default(uint32_t srcip, uint32_t dstip, char *ebuf);
static const char *version = VERSION; /* from autoconf */
libnet_t *libnet = 0;
/* Timestamp of last packet sent.
* Used for timing, and assumes that reply is due to most recent sent query.
*/
static struct timespec lastpacketsent;
/* target string */
static const char *target = "huh? bug in arping?";
/*
* Ping IP mode: cmdline target
* Ping MAC mode: 255.255.255.255, override with -T
*/
uint32_t dstip;
/*
* Ping IP mode: ethxmas, override with -t
* Ping MAC mode: cmdline target
*/
static uint8_t dstmac[ETH_ALEN];
static char* payload_suffix = NULL;
static size_t payload_suffix_size = 4;
/* If there were any libnet write failures, we return error. */
static size_t libnet_write_failures = 0;
uint32_t srcip; /* autodetected, override with -S/-b/-0 */
uint8_t srcmac[ETH_ALEN]; /* autodetected, override with -s */
static int16_t vlan_tag = -1; /* 802.1Q tag to add to packets. -V */
static int16_t vlan_prio = -1; /* 802.1p prio to use with 802.1Q. -Q */
static int beep = 0; /* beep when reply is received. -a */
static int reverse_beep = 0; /* beep when expected reply absent. -e */
static int alsototal = 0; /* print sent as well as received. -u */
static int addr_must_be_same = 0; /* -A */
static int unsolicited = 0; /* -U */
static int send_reply = 0; /* Send reply instead of request. -P */
static int promisc = 0; /* Use promisc mode. -p */
static int finddup = 0; /* finddup mode. -d */
static int dupfound = 0; /* set to 1 if dup found */
static char lastreplymac[ETH_ALEN]; /* if last different from this then dup */
/* -z to turn on, -Z to turn off. Default is compile time option */
static int use_seccomp = DEFAULT_SECCOMP;
unsigned int numsent = 0; /* packets sent */
unsigned int numrecvd = 0; /* packets received */
static unsigned int max_replies = UINT_MAX; /* exit after -C replies */
static const char* timestamp_type = NULL; /* Incoming packet measurement ts type (-m) */
static double stats_min_time = -1;
static double stats_max_time = -1;
static double stats_total_time = 0;
static double stats_total_sq_time = 0;
/* RAWRAW is RAW|RRAW */
static enum { NORMAL, /* normal output */
QUIET, /* No output. -q */
RAW, /* Print MAC when pinging IP. -r */
RRAW, /* Print IP when pinging IP. -R */
RAWRAW, /* Print both. -r and -R */
DOT /* Print '.' and '!', Cisco-style. -D */
} display = NORMAL;
static const uint8_t ethnull[ETH_ALEN] = {0, 0, 0, 0, 0, 0};
static const uint8_t ethxmas[ETH_ALEN] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
static const char* ip_broadcast = "255.255.255.255";
static const uint32_t ip_broadcast_num = (uint32_t)-1;
int verbose = 0; /* Increase with -v */
/* Doesn't really need to be volatile, but doesn't hurt. */
static volatile sig_atomic_t time_to_die = 0;
static float
must_parse_float(const char* in, const char* what)
{
if (!*in) {
fprintf(stderr, "arping: %s: value was empty\n", what);
exit(1);
}
char *endp = NULL;
errno = 0;
// Maybe need to use strtod() instead? It's in C89, whereas
// looks like strtof() is in POSIX.1-2001, POSIX.1-2008, C99.
const float ret = strtof(in, &endp);
if (errno) {
fprintf(stderr, "arping: %s: parsing <%s> as float: %s\n",
what, in, strerror(errno));
exit(1);
}
if (*endp) {
fprintf(stderr,
"arping: %s: failed parsing <%s> as float\n",
what, in);
exit(1);
}
return ret;
}
static int
must_parse_int(const char* in, const char* what)
{
if (!*in) {
fprintf(stderr, "arping: %s: value was empty\n", what);
exit(1);
}
char *endp = NULL;
errno = 0;
const long ret = strtol(in, &endp, 0);
if (errno) {
fprintf(stderr, "arping: %s: parsing <%s> as integer: %s\n",
what, in, strerror(errno));
exit(1);
}
if (*endp) {
fprintf(stderr,
"arping: %s: failed parsing <%s> as integer\n",
what, in);
exit(1);
}
return cast_long_int(ret, "%s %s", what, in);
}
static int16_t
must_parse_int16(const char* in, const char* what)
{
const int out = must_parse_int(in, what);
return cast_int_int16(out, "%s %s", what, in);
}
/*
* It's not actually possible to parse unsigned numbers in C.
* The best this function can do is parse as a long, then reject
* negative numbers. If sizeof(int) == sizeof(long) then that cuts
* off half of all possible values.
*
* More ranting here:
* https://blog.habets.se/2022/10/No-way-to-parse-integers-in-C.html
*/
static unsigned int
must_parse_uint(const char* in, const char* what)
{
if (!*in) {
fprintf(stderr, "arping: %s: value was empty\n", what);
exit(1);
}
char *endp = NULL;
errno = 0;
#if HAVE_STRTOLL
#define CASTER cast_longlong_uint
const long long ret = strtoll(in, &endp, 0);
#else
#define CASTER cast_long_uint
const long ret = strtol(in, &endp, 0);
#endif
if (errno) {
fprintf(stderr, "arping: %s: parsing <%s> as integer: %s\n",
what, in, strerror(errno));
exit(1);
}
if (*endp) {
fprintf(stderr,
"arping: %s: failed parsing <%s> as integer\n",
what, in);
exit(1);
}
if (ret < 0) {
fprintf(stderr,
"arping: %s: <%s> is negative\n", what, in);
exit(1);
}
return CASTER(ret, "%s %s", what, in);
#undef CASTER
}
static unsigned long
parse_ulong(const char* in, const char* what, char* ebuf, size_t ebuflen)
{
if (!*in) {
snprintf(ebuf, ebuflen, "arping: %s: value was empty\n", what);
exit(1);
}
char *endp = NULL;
errno = 0;
#if HAVE_STRTOLL
#define CASTER cast_longlong_ulonglong
const long long ret = strtoll(in, &endp, 0);
#else
#define CASTER cast_long_ulong
const long ret = strtol(in, &endp, 0);
#endif
if (errno) {
snprintf(ebuf, ebuflen, "arping: %s: parsing <%s> as integer: %s\n",
what, in, strerror(errno));
exit(1);
}
if (*endp) {
snprintf(ebuf, ebuflen,
"arping: %s: failed parsing <%s> as integer\n",
what, in);
exit(1);
}
if (ret < 0) {
snprintf(ebuf, ebuflen,
"arping: %s: <%s> is negative\n", what, in);
exit(1);
}
return CASTER(ret, "%s %s", what, in);
#undef CASTER
}
static ssize_t
xgetrandom(void *buf, const size_t buflen, const unsigned int flags)
{
#ifdef ACTUALLY_HAVE_GETRANDOM
return getrandom(buf, buflen, flags);
#else
UNUSED(flags);
uint8_t* p = buf;
for (size_t n = 0; n < buflen; n++) {
p[n] = cast_long_uint8(random() & 0xff, NULL);
}
return cast_size_ssize(buflen, NULL);
#endif
}
static long int
xrandom() {
const int maxtry = 10;
for (int c = 0; c < maxtry; c++) {
long int ret;
const ssize_t rc = xgetrandom(&ret, sizeof(ret), 0);
if (rc == -1) {
fprintf(stderr, "arping: failed to get random bytes: %s\n", strerror(errno));
continue;
}
if (sizeof(ret) != rc) {
fprintf(stderr, "arping: got too few random bytes %zd, want %zd\n", rc, sizeof(ret));
continue;
}
return ret;
}
fprintf(stderr, "arping: failed to get random bytes after %d tries\n", maxtry);
exit(1);
}
/**
* If possible, chroot.
*
* The sshd user is used for privilege separation in OpenSSH.
* Let's assume it's installed and chroot() to there.
*/
static void
drop_fs_root()
{
const char* chroot_user = "sshd";
struct passwd *pw;
errno = 0;
if (!(pw = getpwnam(chroot_user))) {
if (verbose > 1) {
printf("arping: getpwnam(%s): %s\n",
chroot_user, strerror(errno));
}
return;
}
if (chdir(pw->pw_dir)) {
if (verbose) {
printf("arping: chdir(%s): %s\n",
pw->pw_dir, strerror(errno));
}
return;
}
if (chroot(pw->pw_dir)) {
if (verbose) {
printf("arping: chroot(%s): %s\n",
pw->pw_dir, strerror(errno));
}
return;
}
if (chdir("/")) {
if (verbose) {
printf("arping: chdir(/): %s\n", strerror(errno));
}
return;
}
if (verbose > 1) {
printf("arping: Successfully chrooted to %s\n", pw->pw_dir);
}
}
/**
* If possible, drop uid to nobody.
*
* This code only successfully sets all [ug]ids if running as
* root. ARPing is most likely running as root unless using
* capabilities, and those are dropped elsewhere.
*/
static void
drop_uid(uid_t uid, gid_t gid)
{
int fail = 0;
if (setgroups(0, NULL)) {
if (verbose) {
printf("arping: setgroups(0, NULL): %s\n", strerror(errno));
}
fail++;
}
if (gid && setgid(gid)) {
if (verbose) {
printf("arping: setgid(): %s\n", strerror(errno));
}
fail++;
}
if (uid && setuid(uid)) {
if (verbose) {
printf("arping: setuid(): %s\n", strerror(errno));
}
fail++;
}
if (!fail && verbose > 1) {
printf("arping: Successfully dropped uid/gid to %d/%d.\n",
uid, gid);
}
}
/**
* Drop any and all capabilities.
*/
static void
drop_capabilities()
{
#ifdef ACTUALLY_HAVE_CAP_INIT
cap_t no_cap;
if (!(no_cap = cap_init())) {
if (verbose) {
printf("arping: cap_init(): %s\n", strerror(errno));
}
return;
}
if (cap_set_proc(no_cap)) {
if (verbose) {
printf("arping: cap_set_proc(): %s\n", strerror(errno));
}
}
if (verbose > 1) {
printf("arping: Successfully dropped all capabilities.\n");
}
cap_free(no_cap);
#endif
}
/**
* Get GID of input (handle both name and number) or die.
*/
static gid_t
must_get_group(const char* ident)
{
// Special case empty string, because strtol.
int saved_errno = 0;
if (*ident) {
// First try it as a name.
{
struct group* gr;
errno = 0;
if ((gr = getgrnam(ident))) {
return gr->gr_gid;
}
saved_errno = errno;
}
// Not a name. Try it as an integer.
{
char ebuf[1024] = {0};
const unsigned long v = parse_ulong(ident, "gid", ebuf, sizeof(ebuf));
if (!ebuf[0]) {
return cast_ulong_gid(v, NULL);
}
}
}
if (saved_errno != 0) {
fprintf(stderr,
"arping: %s not a number and getgrnam(%s): %s\n",
ident, ident, strerror(saved_errno));
} else {
// If group was supplied, then not
// existing is fatal error too.
fprintf(stderr,
"arping: %s is not a number or group\n",
ident);
}
exit(1);
}
/**
* drop all privileges.
*
* To be called as early as possible. IOW: immediately after opening
* raw socket.
*/
static void
drop_privileges(const char* drop_group)
{
// Need to get uid/gid of 'nobody' before chroot().
const char* drop_user = "nobody";
struct passwd *pw;
errno = 0;
uid_t uid = 0;
gid_t gid = 0;
if (!(pw = getpwnam(drop_user))) {
if (verbose) {
if (errno != 0) {
printf("arping: getpwnam(%s): %s\n",
drop_user, strerror(errno));
} else {
printf("arping: getpwnam(%s): unknown user\n",
drop_user);
}
}
} else {
uid = pw->pw_uid;
gid = pw->pw_gid;
}
// If group is supplied, use that gid instead.
if (drop_group != NULL) {
gid = must_get_group(drop_group);
}
drop_fs_root();
drop_uid(uid, gid);
drop_capabilities();
#ifdef HAVE_UNVEIL
if (unveil("/", "")) {
fprintf(stderr,
"arping: failed to unveil(/, <>): %s\n",
strerror(errno));
exit(1);
}
if (unveil(NULL, NULL)) {
fprintf(stderr, "arping: failed to unveil(NULL, NULL): %s\n",
strerror(errno));
exit(1);
}
if (verbose > 1) {
printf("arping: Successfully unveiled\n");
}
#endif
}
/**
* drop even more privileges, where possible.
*
* After all setup is done and main loop is about to start.
*/
static void
drop_more_privileges(int libnet_fd)
{
UNUSED(libnet_fd);
#ifdef HAVE_PLEDGE
if (pledge("stdio tty", "")) {
fprintf(stderr, "arping: failed to pledge(stdio, <empty>): %s\n",
strerror(errno));
exit(1);
} else if (verbose > 1) {
printf("arping: Successfully pledged\n");
}
#endif
if (use_seccomp) {
drop_seccomp(libnet_fd);
}
}
/**
* Check for buggy libpcap version
* https://github.com/the-tcpdump-group/libpcap/issues/461
*
* This list is not authoritative.
*
* If it's over matching wrong the only impact *should* be that too many packets
* come through the BPF filter.
*
* If it's under-matching then replies with dot1p nonzero will not be seen.
*/
static int
bug_pcap_vlan()
{
const char* bad[] = {
// Broken in 1.7.0.
"libpcap version 1.7",
"libpcap version 1.8",
"libpcap version 1.9.0",
// Fixed in 1.9.1.
// Fixed in 1.10 and up.
NULL,
};
const char* v = pcap_lib_version();
for (const char** t = bad; *t; t++) {
if (!strncmp(v, *t, strlen(*t))) {
return 1;
}
}
return 0;
}
/**
* Do pcap_open_live(), except by using the pcap_create() interface
* introduced in 2008 (libpcap 0.4) where available.
* This is so that we can set some options, which can't be set with
* pcap_open_live:
* 1) Immediate mode -- this prevents pcap from buffering.
* 2) Set timestamp type -- specify what type of timer to use.
*
* FIXME: Use pcap_set_buffer_size()?
*/
static pcap_t*
try_pcap_open_live(const char *device, int snaplen, int to_ms, char *errbuf)
{
#ifdef HAVE_PCAP_CREATE
pcap_t* pcap;
int rc;
if (!(pcap = pcap_create(device, errbuf))) {
goto err;
}
if ((rc = pcap_set_snaplen(pcap, snaplen))) {
snprintf(errbuf, PCAP_ERRBUF_SIZE, "pcap_set_snaplen(): %s", pcap_statustostr(rc));
goto err;
}
if ((rc = pcap_set_promisc(pcap, promisc))) {
snprintf(errbuf, PCAP_ERRBUF_SIZE, "pcap_set_promisc(): %s", pcap_statustostr(rc));
goto err;
}
if ((rc = pcap_set_timeout(pcap, to_ms))) {
snprintf(errbuf, PCAP_ERRBUF_SIZE, "pcap_set_timeout(): %s", pcap_statustostr(rc));
goto err;
}
#ifdef HAVE_PCAP_SET_IMMEDIATE_MODE
// Without immediate mode some architectures (e.g. Linux with
// TPACKET_V3) will buffer replies and incorrectly report upwards of
// hundreds of milliseconds of delay.
if ((rc = pcap_set_immediate_mode(pcap, 1))) {
if (verbose) {
fprintf(stderr, "arping: pcap_set_immediate_mode() failed: %s\n", pcap_statustostr(rc));
}
}
#endif
#ifdef HAVE_PCAP_LIST_TSTAMP_TYPES
if (timestamp_type) {
int err;
int v = pcap_tstamp_type_name_to_val(timestamp_type);
if (v == PCAP_ERROR) {
fprintf(stderr, "arping: Unknown timestamp type \"%s\"\n", timestamp_type);
exit(1);
}
if ((err = pcap_set_tstamp_type(pcap, v))) {
fprintf(stderr,
"arping: Failed to set timestamp type \"%s\" (%d): %s\n",
timestamp_type, v, pcap_statustostr(err));
}
}
#endif
if ((rc = pcap_activate(pcap))) {
if (timestamp_type) {
snprintf(errbuf, PCAP_ERRBUF_SIZE, "pcap_activate(tstype=\"%s\"): %s. Try without setting timestamp type.", timestamp_type, pcap_statustostr(rc));
} else {
snprintf(errbuf, PCAP_ERRBUF_SIZE, "pcap_activate(): %s", pcap_statustostr(rc));
}
goto err;
}
#ifdef HAVE_PCAP_LIST_TSTAMP_TYPES
// List timestamp types after activating, since we don't want to list
// them if activating failed.
if (verbose > 1) {
int *ts;
int count;
count = pcap_list_tstamp_types(pcap, &ts);
if (count == PCAP_ERROR) {
fprintf(stderr, "arping: pcap_list_tstamp_types() failed\n");
} else {
int c;
const char* fmt = " %-18s %s\n";
fprintf(stderr, "Timestamp types:\n");
fprintf(stderr, fmt, "Name", "Description");
for (c = 0; c < count; c++) {
fprintf(stderr, fmt, pcap_tstamp_type_val_to_name(ts[c]),
pcap_tstamp_type_val_to_description(ts[c]));
}
pcap_free_tstamp_types(ts);
}
}
#endif
return pcap;
err:
if (pcap) {
pcap_close(pcap);
}
return NULL;
#else
return pcap_open_live(device, snaplen, to_ms, errbuf);
#endif
}
/**
* Some stupid OSs (Solaris) think it's a good idea to put network
* devices in /dev and then play musical chairs with them.
*
* Since libpcap doesn't seem to have a workaround for that, here's arpings
* workaround.
*
* E.g. if the network interface is called net0, pcap will fail because it
* fails to open /dev/net, because it's a directory.
*/
static pcap_t*
do_pcap_open_live(const char *device, int snaplen, int to_ms, char *errbuf)
{
pcap_t* ret;
char buf[PATH_MAX];
if ((ret = try_pcap_open_live(device, snaplen, to_ms, errbuf))) {
return ret;
}
snprintf(buf, sizeof(buf), "/dev/%s", device);
if ((ret = try_pcap_open_live(buf, snaplen, to_ms, errbuf))) {
return ret;
}
snprintf(buf, sizeof(buf), "/dev/net/%s", device);
if ((ret = try_pcap_open_live(buf, snaplen, to_ms, errbuf))) {
return ret;
}
/* Call original again to reset the error message. */
return try_pcap_open_live(device, snaplen, to_ms, errbuf);
}
/**
* Some Libnet error messages end with a newline. Strip that in place.
*/
void
strip_newline(char* s) {
size_t n;
for (n = strlen(s); n && (s[n - 1] == '\n'); --n) {
s[n - 1] = 0;
}
}
/**
* Init libnet with specified ifname. Destroy if already inited.
*
* Libnet usually needs init before we have searched for the real
* interface. In that case, first we just give a NULL pointer as the
* interface. But libnet sometimes fails to find an interface (no idea
* why), so then we try to use "lo" and "lo0" explicitly.
*
* If even loopback fails, then it'll preserve the original error
* message.
*
* Call with recursive=0.
*/
void
do_libnet_init(const char *inifname, int recursive)
{
const char* ifname = inifname;
int last = 0;
switch (recursive) {
case 0:
break;
case 1:
ifname = "lo"; // E.g. Linux.
break;
case 2:
ifname = "lo0"; // E.g. OpenBSD.
break;
default:
last = 1;
break;
}
// If we're given an interface name then always use that.
// No need to be recursive about it.
if (inifname != NULL && recursive == 0) {
ifname = inifname;
last = 1;
}
char ebuf[LIBNET_ERRBUF_SIZE];
ebuf[0] = 0;
if (verbose > 1) {
printf("arping: trying libnet_init(LIBNET_LINK, %s)\n",
ifname ? ifname : "<null>");
}
if (libnet) {
/* Probably going to switch interface from temp to real. */
libnet_destroy(libnet);
libnet = 0;
}
/* Try libnet_init() even though we maybe aren't root. We may have
* a capability or something. */
if (!(libnet = libnet_init(LIBNET_LINK,
(char*)ifname,
ebuf))) {
strip_newline(ebuf);
if (verbose) {
fprintf(stderr,
"arping: libnet_init(LIBNET_LINK, %s): %s\n",
ifname ? ifname : "<null>",
*ebuf ? ebuf : "<no error message>");
}
if (!last) {
do_libnet_init(ifname, recursive+1);
if (libnet != NULL) {
return;
}
}
if (!verbose) {
// Prevent double-print when verbose.
fprintf(stderr,
"arping: libnet_init(LIBNET_LINK, %s): %s\n",
ifname ? ifname : "<null>",
*ebuf ? ebuf : "<no error message>");
}
if (getuid() && geteuid()) {
fprintf(stderr,
"arping: you may need to run as root\n");
}
exit(1);
}
}
/**
*
*/
void
sigint(int i)
{
UNUSED(i);
time_to_die = 1;
}
/**
* idiot-proof clock_gettime() wrapper
*/
static void
getclock(struct timespec *ts)
{
#if HAVE_CLOCK_MONOTONIC
static int clock_gettime_failed = 0;
if (!clock_gettime_failed) {
if (0 == clock_gettime(CLOCK_MONOTONIC, ts)) {
return;
}
fprintf(stderr, "arping: clock_gettime(): %s\n",
strerror(errno));
clock_gettime_failed = 1; // Prevent duplicate error messages.
}
#endif
struct timeval tv;
if (-1 == gettimeofday(&tv, NULL)) {
fprintf(stderr, "arping: gettimeofday(): %s\n",
strerror(errno));
sigint(0);
}
ts->tv_sec = tv.tv_sec;
ts->tv_nsec = tv.tv_usec * 1000;
}
/**
*
*/
static char*
format_mac(const unsigned char* mac, char* buf, size_t bufsize) {
snprintf(buf, bufsize, "%.2x:%.2x:%.2x:%.2x:%.2x:%.2x",
mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
return buf;
}
/**
*
*/
static void
extended_usage()
{
printf("\nOptions:\n");
printf("\n"
" -0 Use this option to ping with source IP address 0.0.0.0. Use this\n"
" when you haven't configured your interface yet. Note that this\n"
" may get the MAC-ping unanswered. This is an alias for -S\n"
" 0.0.0.0.\n"
" -a Audiable ping.\n"
" -A Only count addresses matching requested address (This *WILL*\n"
" break most things you do. Only useful if you are arpinging many\n"
" hosts at once. See arping-scan-net.sh for an example).\n"
" -b Like -0 but source broadcast source address (255.255.255.255).\n"
" Note that this may get the arping unanswered since it's not nor-\n"
" mal behavior for a host.\n"
" -B Use instead of host if you want to address 255.255.255.255.\n"
" -c count\n"
" Only send count requests.\n"
" -C count\n"
" Only wait for this many replies, regardless of -c and -w.\n"
" -d Find duplicate replies. Exit with 1 if there are "
"answers from\n"
" two different MAC addresses.\n"
" -D Display answers as exclamation points and missing packets as dots.\n"
" -e Like -a but beep when there is no reply.\n"
" -F Don't try to be smart about the interface name. (even if this\n"
" switch is not given, -i overrides smartness)\n"
" -g group\n"
" setgid() to this group instead of the nobody group.\n"
" -h Displays a help message and exits.\n"
" -i interface\n"
" Use the specified interface.\n"
" -m type"
#ifndef HAVE_PCAP_LIST_TSTAMP_TYPES
" (Disabled on this system. Option ignored)"
#endif
"\n Type of timestamp to use for incoming packets. Use -vv when\n"
" pinging to list available ones.\n"
" -q Does not display messages, except error messages.\n"
" -Q pri 802.1p priority to set. Should be used with 802.1Q (-V).\n"
" Defaults to 0.\n"
" -r Raw output: only the MAC/IP address is displayed for each reply.\n"
" -R Raw output: Like -r but shows \"the other one\", can be combined\n"
" with -r.\n"
" -s MAC Set source MAC address. You may need to use -p with this.\n"
" -S IP Like -b and -0 but with set source address. Note that this may\n"
" get the arping unanswered if the target does not have routing to\n"