forked from the-tcpdump-group/libpcap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pcap-linux.c
7729 lines (7110 loc) · 213 KB
/
pcap-linux.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
/*
* pcap-linux.c: Packet capture interface to the Linux kernel
*
* Copyright (c) 2000 Torsten Landschoff <[email protected]>
* Sebastian Krahmer <[email protected]>
*
* License: BSD
*
* 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. The names of the authors may not be used to endorse or promote
* products derived from this software without specific prior
* written permission.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
*
* Modifications: Added PACKET_MMAP support
* Paolo Abeni <[email protected]>
* Added TPACKET_V3 support
* Gabor Tatarka <[email protected]>
*
* based on previous works of:
* Simon Patarin <[email protected]>
* Phil Wood <[email protected]>
*
* Monitor-mode support for mac80211 includes code taken from the iw
* command; the copyright notice for that code is
*
* Copyright (c) 2007, 2008 Johannes Berg
* Copyright (c) 2007 Andy Lutomirski
* Copyright (c) 2007 Mike Kershaw
* Copyright (c) 2008 Gábor Stefanik
*
* 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. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.
*/
/*
* Known problems with 2.0[.x] kernels:
*
* - The loopback device gives every packet twice; on 2.2[.x] kernels,
* if we use PF_PACKET, we can filter out the transmitted version
* of the packet by using data in the "sockaddr_ll" returned by
* "recvfrom()", but, on 2.0[.x] kernels, we have to use
* PF_INET/SOCK_PACKET, which means "recvfrom()" supplies a
* "sockaddr_pkt" which doesn't give us enough information to let
* us do that.
*
* - We have to set the interface's IFF_PROMISC flag ourselves, if
* we're to run in promiscuous mode, which means we have to turn
* it off ourselves when we're done; the kernel doesn't keep track
* of how many sockets are listening promiscuously, which means
* it won't get turned off automatically when no sockets are
* listening promiscuously. We catch "pcap_close()" and, for
* interfaces we put into promiscuous mode, take them out of
* promiscuous mode - which isn't necessarily the right thing to
* do, if another socket also requested promiscuous mode between
* the time when we opened the socket and the time when we close
* the socket.
*
* - MSG_TRUNC isn't supported, so you can't specify that "recvfrom()"
* return the amount of data that you could have read, rather than
* the amount that was returned, so we can't just allocate a buffer
* whose size is the snapshot length and pass the snapshot length
* as the byte count, and also pass MSG_TRUNC, so that the return
* value tells us how long the packet was on the wire.
*
* This means that, if we want to get the actual size of the packet,
* so we can return it in the "len" field of the packet header,
* we have to read the entire packet, not just the part that fits
* within the snapshot length, and thus waste CPU time copying data
* from the kernel that our caller won't see.
*
* We have to get the actual size, and supply it in "len", because
* otherwise, the IP dissector in tcpdump, for example, will complain
* about "truncated-ip", as the packet will appear to have been
* shorter, on the wire, than the IP header said it should have been.
*/
#define _GNU_SOURCE
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <limits.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <sys/utsname.h>
#include <sys/mman.h>
#include <linux/if.h>
#include <linux/if_packet.h>
#include <linux/sockios.h>
#include <netinet/in.h>
#include <linux/if_ether.h>
#include <net/if_arp.h>
#include <poll.h>
#include <dirent.h>
#ifdef HAVE_SYS_EVENTFD_H
#include <sys/eventfd.h>
#endif
#include "pcap-int.h"
#include "pcap/sll.h"
#include "pcap/vlan.h"
#include "diag-control.h"
/*
* If PF_PACKET is defined, we can use {SOCK_RAW,SOCK_DGRAM}/PF_PACKET
* sockets rather than SOCK_PACKET sockets.
*
* To use them, we include <linux/if_packet.h> rather than
* <netpacket/packet.h>; we do so because
*
* some Linux distributions (e.g., Slackware 4.0) have 2.2 or
* later kernels and libc5, and don't provide a <netpacket/packet.h>
* file;
*
* not all versions of glibc2 have a <netpacket/packet.h> file
* that defines stuff needed for some of the 2.4-or-later-kernel
* features, so if the system has a 2.4 or later kernel, we
* still can't use those features.
*
* We're already including a number of other <linux/XXX.h> headers, and
* this code is Linux-specific (no other OS has PF_PACKET sockets as
* a raw packet capture mechanism), so it's not as if you gain any
* useful portability by using <netpacket/packet.h>
*
* XXX - should we just include <linux/if_packet.h> even if PF_PACKET
* isn't defined? It only defines one data structure in 2.0.x, so
* it shouldn't cause any problems.
*/
#ifdef PF_PACKET
# include <linux/if_packet.h>
/*
* On at least some Linux distributions (for example, Red Hat 5.2),
* there's no <netpacket/packet.h> file, but PF_PACKET is defined if
* you include <sys/socket.h>, but <linux/if_packet.h> doesn't define
* any of the PF_PACKET stuff such as "struct sockaddr_ll" or any of
* the PACKET_xxx stuff.
*
* So we check whether PACKET_HOST is defined, and assume that we have
* PF_PACKET sockets only if it is defined.
*/
# ifdef PACKET_HOST
# define HAVE_PF_PACKET_SOCKETS
# ifdef PACKET_AUXDATA
# define HAVE_PACKET_AUXDATA
# endif /* PACKET_AUXDATA */
# endif /* PACKET_HOST */
/* check for memory mapped access avaibility. We assume every needed
* struct is defined if the macro TPACKET_HDRLEN is defined, because it
* uses many ring related structs and macros */
# ifdef PCAP_SUPPORT_PACKET_RING
# ifdef TPACKET_HDRLEN
# define HAVE_PACKET_RING
# ifdef TPACKET3_HDRLEN
# define HAVE_TPACKET3
# endif /* TPACKET3_HDRLEN */
# ifdef TPACKET2_HDRLEN
# define HAVE_TPACKET2
# else /* TPACKET2_HDRLEN */
# define TPACKET_V1 0 /* Old kernel with only V1, so no TPACKET_Vn defined */
# endif /* TPACKET2_HDRLEN */
# endif /* TPACKET_HDRLEN */
# endif /* PCAP_SUPPORT_PACKET_RING */
#endif /* PF_PACKET */
#ifdef SO_ATTACH_FILTER
#include <linux/types.h>
#include <linux/filter.h>
#endif
#ifdef HAVE_LINUX_NET_TSTAMP_H
#include <linux/net_tstamp.h>
#endif
#ifdef HAVE_LINUX_SOCKIOS_H
#include <linux/sockios.h>
#endif
#ifdef HAVE_LINUX_IF_BONDING_H
#include <linux/if_bonding.h>
/*
* The ioctl code to use to check whether a device is a bonding device.
*/
#if defined(SIOCBONDINFOQUERY)
#define BOND_INFO_QUERY_IOCTL SIOCBONDINFOQUERY
#elif defined(BOND_INFO_QUERY_OLD)
#define BOND_INFO_QUERY_IOCTL BOND_INFO_QUERY_OLD
#endif
#endif /* HAVE_LINUX_IF_BONDING_H */
/*
* Got Wireless Extensions?
*/
#ifdef HAVE_LINUX_WIRELESS_H
#include <linux/wireless.h>
#endif /* HAVE_LINUX_WIRELESS_H */
/*
* Got libnl?
*/
#ifdef HAVE_LIBNL
#include <linux/nl80211.h>
#include <netlink/genl/genl.h>
#include <netlink/genl/family.h>
#include <netlink/genl/ctrl.h>
#include <netlink/msg.h>
#include <netlink/attr.h>
#endif /* HAVE_LIBNL */
/*
* Got ethtool support?
*/
#ifdef HAVE_LINUX_ETHTOOL_H
#include <linux/ethtool.h>
#endif
#ifndef HAVE_SOCKLEN_T
typedef int socklen_t;
#endif
#ifndef MSG_TRUNC
/*
* This is being compiled on a system that lacks MSG_TRUNC; define it
* with the value it has in the 2.2 and later kernels, so that, on
* those kernels, when we pass it in the flags argument to "recvfrom()"
* we're passing the right value and thus get the MSG_TRUNC behavior
* we want. (We don't get that behavior on 2.0[.x] kernels, because
* they didn't support MSG_TRUNC.)
*/
#define MSG_TRUNC 0x20
#endif
#ifndef SOL_PACKET
/*
* This is being compiled on a system that lacks SOL_PACKET; define it
* with the value it has in the 2.2 and later kernels, so that we can
* set promiscuous mode in the good modern way rather than the old
* 2.0-kernel crappy way.
*/
#define SOL_PACKET 263
#endif
#define MAX_LINKHEADER_SIZE 256
/*
* When capturing on all interfaces we use this as the buffer size.
* Should be bigger then all MTUs that occur in real life.
* 64kB should be enough for now.
*/
#define BIGGER_THAN_ALL_MTUS (64*1024)
/*
* Private data for capturing on Linux SOCK_PACKET or PF_PACKET sockets.
*/
struct pcap_linux {
u_int packets_read; /* count of packets read with recvfrom() */
long proc_dropped; /* packets reported dropped by /proc/net/dev */
struct pcap_stat stat;
char *device; /* device name */
int filter_in_userland; /* must filter in userland */
int blocks_to_filter_in_userland;
int must_do_on_close; /* stuff we must do when we close */
int timeout; /* timeout for buffering */
int sock_packet; /* using Linux 2.0 compatible interface */
int cooked; /* using SOCK_DGRAM rather than SOCK_RAW */
int ifindex; /* interface index of device we're bound to */
int lo_ifindex; /* interface index of the loopback device */
bpf_u_int32 oldmode; /* mode to restore when turning monitor mode off */
char *mondevice; /* mac80211 monitor device we created */
u_char *mmapbuf; /* memory-mapped region pointer */
size_t mmapbuflen; /* size of region */
int vlan_offset; /* offset at which to insert vlan tags; if -1, don't insert */
u_int tp_version; /* version of tpacket_hdr for mmaped ring */
u_int tp_hdrlen; /* hdrlen of tpacket_hdr for mmaped ring */
u_char *oneshot_buffer; /* buffer for copy of packet */
int poll_timeout; /* timeout to use in poll() */
#ifdef HAVE_TPACKET3
unsigned char *current_packet; /* Current packet within the TPACKET_V3 block. Move to next block if NULL. */
int packets_left; /* Unhandled packets left within the block from previous call to pcap_read_linux_mmap_v3 in case of TPACKET_V3. */
#endif
#ifdef HAVE_SYS_EVENTFD_H
int poll_breakloop_fd; /* fd to an eventfd to break from blocking operations */
#endif
};
/*
* Stuff to do when we close.
*/
#define MUST_CLEAR_PROMISC 0x00000001 /* clear promiscuous mode */
#define MUST_CLEAR_RFMON 0x00000002 /* clear rfmon (monitor) mode */
#define MUST_DELETE_MONIF 0x00000004 /* delete monitor-mode interface */
/*
* Prototypes for internal functions and methods.
*/
static int get_if_flags(const char *, bpf_u_int32 *, char *);
static int is_wifi(int, const char *);
static void map_arphrd_to_dlt(pcap_t *, int, int, const char *, int);
static int pcap_activate_linux(pcap_t *);
static int activate_old(pcap_t *, int);
#ifdef HAVE_PF_PACKET_SOCKETS
static int activate_new(pcap_t *, int);
#ifdef HAVE_PACKET_RING
static int activate_mmap(pcap_t *, int *);
#endif
#endif /* HAVE_PF_PACKET_SOCKETS */
static int pcap_can_set_rfmon_linux(pcap_t *);
static int pcap_read_linux(pcap_t *, int, pcap_handler, u_char *);
static int pcap_read_packet(pcap_t *, pcap_handler, u_char *);
static int pcap_inject_linux(pcap_t *, const void *, int);
static int pcap_stats_linux(pcap_t *, struct pcap_stat *);
static int pcap_setfilter_linux(pcap_t *, struct bpf_program *);
static int pcap_setdirection_linux(pcap_t *, pcap_direction_t);
static int pcap_set_datalink_linux(pcap_t *, int);
static void pcap_cleanup_linux(pcap_t *);
/*
* This is what the header structure looks like in a 64-bit kernel;
* we use this, rather than struct tpacket_hdr, if we're using
* TPACKET_V1 in 32-bit code running on a 64-bit kernel.
*/
struct tpacket_hdr_64 {
uint64_t tp_status;
unsigned int tp_len;
unsigned int tp_snaplen;
unsigned short tp_mac;
unsigned short tp_net;
unsigned int tp_sec;
unsigned int tp_usec;
};
/*
* We use this internally as the tpacket version for TPACKET_V1 in
* 32-bit code on a 64-bit kernel.
*/
#define TPACKET_V1_64 99
union thdr {
struct tpacket_hdr *h1;
struct tpacket_hdr_64 *h1_64;
#ifdef HAVE_TPACKET2
struct tpacket2_hdr *h2;
#endif
#ifdef HAVE_TPACKET3
struct tpacket_block_desc *h3;
#endif
void *raw;
};
#ifdef HAVE_PACKET_RING
#define RING_GET_FRAME_AT(h, offset) (((union thdr **)h->buffer)[(offset)])
#define RING_GET_CURRENT_FRAME(h) RING_GET_FRAME_AT(h, h->offset)
static void destroy_ring(pcap_t *handle);
static int create_ring(pcap_t *handle, int *status);
static int prepare_tpacket_socket(pcap_t *handle);
static void pcap_cleanup_linux_mmap(pcap_t *);
static int pcap_read_linux_mmap_v1(pcap_t *, int, pcap_handler , u_char *);
static int pcap_read_linux_mmap_v1_64(pcap_t *, int, pcap_handler , u_char *);
#ifdef HAVE_TPACKET2
static int pcap_read_linux_mmap_v2(pcap_t *, int, pcap_handler , u_char *);
#endif
#ifdef HAVE_TPACKET3
static int pcap_read_linux_mmap_v3(pcap_t *, int, pcap_handler , u_char *);
#endif
static int pcap_setfilter_linux_mmap(pcap_t *, struct bpf_program *);
static int pcap_setnonblock_mmap(pcap_t *p, int nonblock);
static int pcap_getnonblock_mmap(pcap_t *p);
static void pcap_oneshot_mmap(u_char *user, const struct pcap_pkthdr *h,
const u_char *bytes);
#endif
/*
* In pre-3.0 kernels, the tp_vlan_tci field is set to whatever the
* vlan_tci field in the skbuff is. 0 can either mean "not on a VLAN"
* or "on VLAN 0". There is no flag set in the tp_status field to
* distinguish between them.
*
* In 3.0 and later kernels, if there's a VLAN tag present, the tp_vlan_tci
* field is set to the VLAN tag, and the TP_STATUS_VLAN_VALID flag is set
* in the tp_status field, otherwise the tp_vlan_tci field is set to 0 and
* the TP_STATUS_VLAN_VALID flag isn't set in the tp_status field.
*
* With a pre-3.0 kernel, we cannot distinguish between packets with no
* VLAN tag and packets on VLAN 0, so we will mishandle some packets, and
* there's nothing we can do about that.
*
* So, on those systems, which never set the TP_STATUS_VLAN_VALID flag, we
* continue the behavior of earlier libpcaps, wherein we treated packets
* with a VLAN tag of 0 as being packets without a VLAN tag rather than packets
* on VLAN 0. We do this by treating packets with a tp_vlan_tci of 0 and
* with the TP_STATUS_VLAN_VALID flag not set in tp_status as not having
* VLAN tags. This does the right thing on 3.0 and later kernels, and
* continues the old unfixably-imperfect behavior on pre-3.0 kernels.
*
* If TP_STATUS_VLAN_VALID isn't defined, we test it as the 0x10 bit; it
* has that value in 3.0 and later kernels.
*/
#ifdef TP_STATUS_VLAN_VALID
#define VLAN_VALID(hdr, hv) ((hv)->tp_vlan_tci != 0 || ((hdr)->tp_status & TP_STATUS_VLAN_VALID))
#else
/*
* This is being compiled on a system that lacks TP_STATUS_VLAN_VALID,
* so we testwith the value it has in the 3.0 and later kernels, so
* we can test it if we're running on a system that has it. (If we're
* running on a system that doesn't have it, it won't be set in the
* tp_status field, so the tests of it will always fail; that means
* we behave the way we did before we introduced this macro.)
*/
#define VLAN_VALID(hdr, hv) ((hv)->tp_vlan_tci != 0 || ((hdr)->tp_status & 0x10))
#endif
#ifdef TP_STATUS_VLAN_TPID_VALID
# define VLAN_TPID(hdr, hv) (((hv)->tp_vlan_tpid || ((hdr)->tp_status & TP_STATUS_VLAN_TPID_VALID)) ? (hv)->tp_vlan_tpid : ETH_P_8021Q)
#else
# define VLAN_TPID(hdr, hv) ETH_P_8021Q
#endif
/*
* Wrap some ioctl calls
*/
#ifdef HAVE_PF_PACKET_SOCKETS
static int iface_get_id(int fd, const char *device, char *ebuf);
#endif /* HAVE_PF_PACKET_SOCKETS */
static int iface_get_mtu(int fd, const char *device, char *ebuf);
static int iface_get_arptype(int fd, const char *device, char *ebuf);
#ifdef HAVE_PF_PACKET_SOCKETS
static int iface_bind(int fd, int ifindex, char *ebuf, int protocol);
#ifdef IW_MODE_MONITOR
static int has_wext(int sock_fd, const char *device, char *ebuf);
#endif /* IW_MODE_MONITOR */
static int enter_rfmon_mode(pcap_t *handle, int sock_fd,
const char *device);
#endif /* HAVE_PF_PACKET_SOCKETS */
#if defined(HAVE_LINUX_NET_TSTAMP_H) && defined(PACKET_TIMESTAMP)
static int iface_ethtool_get_ts_info(const char *device, pcap_t *handle,
char *ebuf);
#endif
#ifdef HAVE_PACKET_RING
static int iface_get_offload(pcap_t *handle);
#endif
static int iface_bind_old(int fd, const char *device, char *ebuf);
#ifdef SO_ATTACH_FILTER
static int fix_program(pcap_t *handle, struct sock_fprog *fcode,
int is_mapped);
static int fix_offset(pcap_t *handle, struct bpf_insn *p);
static int set_kernel_filter(pcap_t *handle, struct sock_fprog *fcode);
static int reset_kernel_filter(pcap_t *handle);
static struct sock_filter total_insn
= BPF_STMT(BPF_RET | BPF_K, 0);
static struct sock_fprog total_fcode
= { 1, &total_insn };
#endif /* SO_ATTACH_FILTER */
static int iface_dsa_get_proto_info(const char *device, pcap_t *handle);
pcap_t *
pcap_create_interface(const char *device, char *ebuf)
{
pcap_t *handle;
handle = pcap_create_common(ebuf, sizeof (struct pcap_linux));
if (handle == NULL)
return NULL;
handle->activate_op = pcap_activate_linux;
handle->can_set_rfmon_op = pcap_can_set_rfmon_linux;
#if defined(HAVE_LINUX_NET_TSTAMP_H) && defined(PACKET_TIMESTAMP)
/*
* See what time stamp types we support.
*/
if (iface_ethtool_get_ts_info(device, handle, ebuf) == -1) {
pcap_close(handle);
return NULL;
}
#endif
#if defined(SIOCGSTAMPNS) && defined(SO_TIMESTAMPNS)
/*
* We claim that we support microsecond and nanosecond time
* stamps.
*
* XXX - with adapter-supplied time stamps, can we choose
* microsecond or nanosecond time stamps on arbitrary
* adapters?
*/
handle->tstamp_precision_count = 2;
handle->tstamp_precision_list = malloc(2 * sizeof(u_int));
if (handle->tstamp_precision_list == NULL) {
pcap_fmt_errmsg_for_errno(ebuf, PCAP_ERRBUF_SIZE,
errno, "malloc");
pcap_close(handle);
return NULL;
}
handle->tstamp_precision_list[0] = PCAP_TSTAMP_PRECISION_MICRO;
handle->tstamp_precision_list[1] = PCAP_TSTAMP_PRECISION_NANO;
#endif /* defined(SIOCGSTAMPNS) && defined(SO_TIMESTAMPNS) */
#ifdef HAVE_SYS_EVENTFD_H
struct pcap_linux *handlep = handle->priv;
handlep->poll_breakloop_fd = eventfd(0, EFD_NONBLOCK);
#endif
return handle;
}
#ifdef HAVE_LIBNL
/*
* If interface {if} is a mac80211 driver, the file
* /sys/class/net/{if}/phy80211 is a symlink to
* /sys/class/ieee80211/{phydev}, for some {phydev}.
*
* On Fedora 9, with a 2.6.26.3-29 kernel, my Zydas stick, at
* least, has a "wmaster0" device and a "wlan0" device; the
* latter is the one with the IP address. Both show up in
* "tcpdump -D" output. Capturing on the wmaster0 device
* captures with 802.11 headers.
*
* airmon-ng searches through /sys/class/net for devices named
* monN, starting with mon0; as soon as one *doesn't* exist,
* it chooses that as the monitor device name. If the "iw"
* command exists, it does "iw dev {if} interface add {monif}
* type monitor", where {monif} is the monitor device. It
* then (sigh) sleeps .1 second, and then configures the
* device up. Otherwise, if /sys/class/ieee80211/{phydev}/add_iface
* is a file, it writes {mondev}, without a newline, to that file,
* and again (sigh) sleeps .1 second, and then iwconfig's that
* device into monitor mode and configures it up. Otherwise,
* you can't do monitor mode.
*
* All these devices are "glued" together by having the
* /sys/class/net/{device}/phy80211 links pointing to the same
* place, so, given a wmaster, wlan, or mon device, you can
* find the other devices by looking for devices with
* the same phy80211 link.
*
* To turn monitor mode off, delete the monitor interface,
* either with "iw dev {monif} interface del" or by sending
* {monif}, with no NL, down /sys/class/ieee80211/{phydev}/remove_iface
*
* Note: if you try to create a monitor device named "monN", and
* there's already a "monN" device, it fails, as least with
* the netlink interface (which is what iw uses), with a return
* value of -ENFILE. (Return values are negative errnos.) We
* could probably use that to find an unused device.
*
* Yes, you can have multiple monitor devices for a given
* physical device.
*/
/*
* Is this a mac80211 device? If so, fill in the physical device path and
* return 1; if not, return 0. On an error, fill in handle->errbuf and
* return PCAP_ERROR.
*/
static int
get_mac80211_phydev(pcap_t *handle, const char *device, char *phydev_path,
size_t phydev_max_pathlen)
{
char *pathstr;
ssize_t bytes_read;
/*
* Generate the path string for the symlink to the physical device.
*/
if (asprintf(&pathstr, "/sys/class/net/%s/phy80211", device) == -1) {
snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
"%s: Can't generate path name string for /sys/class/net device",
device);
return PCAP_ERROR;
}
bytes_read = readlink(pathstr, phydev_path, phydev_max_pathlen);
if (bytes_read == -1) {
if (errno == ENOENT || errno == EINVAL) {
/*
* Doesn't exist, or not a symlink; assume that
* means it's not a mac80211 device.
*/
free(pathstr);
return 0;
}
pcap_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE,
errno, "%s: Can't readlink %s", device, pathstr);
free(pathstr);
return PCAP_ERROR;
}
free(pathstr);
phydev_path[bytes_read] = '\0';
return 1;
}
#ifdef HAVE_LIBNL_SOCKETS
#define get_nl_errmsg nl_geterror
#else
/* libnl 2.x compatibility code */
#define nl_sock nl_handle
static inline struct nl_handle *
nl_socket_alloc(void)
{
return nl_handle_alloc();
}
static inline void
nl_socket_free(struct nl_handle *h)
{
nl_handle_destroy(h);
}
#define get_nl_errmsg strerror
static inline int
__genl_ctrl_alloc_cache(struct nl_handle *h, struct nl_cache **cache)
{
struct nl_cache *tmp = genl_ctrl_alloc_cache(h);
if (!tmp)
return -ENOMEM;
*cache = tmp;
return 0;
}
#define genl_ctrl_alloc_cache __genl_ctrl_alloc_cache
#endif /* !HAVE_LIBNL_SOCKETS */
struct nl80211_state {
struct nl_sock *nl_sock;
struct nl_cache *nl_cache;
struct genl_family *nl80211;
};
static int
nl80211_init(pcap_t *handle, struct nl80211_state *state, const char *device)
{
int err;
state->nl_sock = nl_socket_alloc();
if (!state->nl_sock) {
snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
"%s: failed to allocate netlink handle", device);
return PCAP_ERROR;
}
if (genl_connect(state->nl_sock)) {
snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
"%s: failed to connect to generic netlink", device);
goto out_handle_destroy;
}
err = genl_ctrl_alloc_cache(state->nl_sock, &state->nl_cache);
if (err < 0) {
snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
"%s: failed to allocate generic netlink cache: %s",
device, get_nl_errmsg(-err));
goto out_handle_destroy;
}
state->nl80211 = genl_ctrl_search_by_name(state->nl_cache, "nl80211");
if (!state->nl80211) {
snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
"%s: nl80211 not found", device);
goto out_cache_free;
}
return 0;
out_cache_free:
nl_cache_free(state->nl_cache);
out_handle_destroy:
nl_socket_free(state->nl_sock);
return PCAP_ERROR;
}
static void
nl80211_cleanup(struct nl80211_state *state)
{
genl_family_put(state->nl80211);
nl_cache_free(state->nl_cache);
nl_socket_free(state->nl_sock);
}
static int
del_mon_if(pcap_t *handle, int sock_fd, struct nl80211_state *state,
const char *device, const char *mondevice);
static int
add_mon_if(pcap_t *handle, int sock_fd, struct nl80211_state *state,
const char *device, const char *mondevice)
{
struct pcap_linux *handlep = handle->priv;
int ifindex;
struct nl_msg *msg;
int err;
ifindex = iface_get_id(sock_fd, device, handle->errbuf);
if (ifindex == -1)
return PCAP_ERROR;
msg = nlmsg_alloc();
if (!msg) {
snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
"%s: failed to allocate netlink msg", device);
return PCAP_ERROR;
}
genlmsg_put(msg, 0, 0, genl_family_get_id(state->nl80211), 0,
0, NL80211_CMD_NEW_INTERFACE, 0);
NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
DIAG_OFF_NARROWING
NLA_PUT_STRING(msg, NL80211_ATTR_IFNAME, mondevice);
DIAG_ON_NARROWING
NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, NL80211_IFTYPE_MONITOR);
err = nl_send_auto_complete(state->nl_sock, msg);
if (err < 0) {
#if defined HAVE_LIBNL_NLE
if (err == -NLE_FAILURE) {
#else
if (err == -ENFILE) {
#endif
/*
* Device not available; our caller should just
* keep trying. (libnl 2.x maps ENFILE to
* NLE_FAILURE; it can also map other errors
* to that, but there's not much we can do
* about that.)
*/
nlmsg_free(msg);
return 0;
} else {
/*
* Real failure, not just "that device is not
* available.
*/
snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
"%s: nl_send_auto_complete failed adding %s interface: %s",
device, mondevice, get_nl_errmsg(-err));
nlmsg_free(msg);
return PCAP_ERROR;
}
}
err = nl_wait_for_ack(state->nl_sock);
if (err < 0) {
#if defined HAVE_LIBNL_NLE
if (err == -NLE_FAILURE) {
#else
if (err == -ENFILE) {
#endif
/*
* Device not available; our caller should just
* keep trying. (libnl 2.x maps ENFILE to
* NLE_FAILURE; it can also map other errors
* to that, but there's not much we can do
* about that.)
*/
nlmsg_free(msg);
return 0;
} else {
/*
* Real failure, not just "that device is not
* available.
*/
snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
"%s: nl_wait_for_ack failed adding %s interface: %s",
device, mondevice, get_nl_errmsg(-err));
nlmsg_free(msg);
return PCAP_ERROR;
}
}
/*
* Success.
*/
nlmsg_free(msg);
/*
* Try to remember the monitor device.
*/
handlep->mondevice = strdup(mondevice);
if (handlep->mondevice == NULL) {
pcap_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE,
errno, "strdup");
/*
* Get rid of the monitor device.
*/
del_mon_if(handle, sock_fd, state, device, mondevice);
return PCAP_ERROR;
}
return 1;
nla_put_failure:
snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
"%s: nl_put failed adding %s interface",
device, mondevice);
nlmsg_free(msg);
return PCAP_ERROR;
}
static int
del_mon_if(pcap_t *handle, int sock_fd, struct nl80211_state *state,
const char *device, const char *mondevice)
{
int ifindex;
struct nl_msg *msg;
int err;
ifindex = iface_get_id(sock_fd, mondevice, handle->errbuf);
if (ifindex == -1)
return PCAP_ERROR;
msg = nlmsg_alloc();
if (!msg) {
snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
"%s: failed to allocate netlink msg", device);
return PCAP_ERROR;
}
genlmsg_put(msg, 0, 0, genl_family_get_id(state->nl80211), 0,
0, NL80211_CMD_DEL_INTERFACE, 0);
NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
err = nl_send_auto_complete(state->nl_sock, msg);
if (err < 0) {
snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
"%s: nl_send_auto_complete failed deleting %s interface: %s",
device, mondevice, get_nl_errmsg(-err));
nlmsg_free(msg);
return PCAP_ERROR;
}
err = nl_wait_for_ack(state->nl_sock);
if (err < 0) {
snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
"%s: nl_wait_for_ack failed adding %s interface: %s",
device, mondevice, get_nl_errmsg(-err));
nlmsg_free(msg);
return PCAP_ERROR;
}
/*
* Success.
*/
nlmsg_free(msg);
return 1;
nla_put_failure:
snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
"%s: nl_put failed deleting %s interface",
device, mondevice);
nlmsg_free(msg);
return PCAP_ERROR;
}
static int
enter_rfmon_mode_mac80211(pcap_t *handle, int sock_fd, const char *device)
{
struct pcap_linux *handlep = handle->priv;
int ret;
char phydev_path[PATH_MAX+1];
struct nl80211_state nlstate;
struct ifreq ifr;
u_int n;
/*
* Is this a mac80211 device?
*/
ret = get_mac80211_phydev(handle, device, phydev_path, PATH_MAX);
if (ret < 0)
return ret; /* error */
if (ret == 0)
return 0; /* no error, but not mac80211 device */
/*
* XXX - is this already a monN device?
* If so, we're done.
* Is that determined by old Wireless Extensions ioctls?
*/
/*
* OK, it's apparently a mac80211 device.
* Try to find an unused monN device for it.
*/
ret = nl80211_init(handle, &nlstate, device);
if (ret != 0)
return ret;
for (n = 0; n < UINT_MAX; n++) {
/*
* Try mon{n}.
*/
char mondevice[3+10+1]; /* mon{UINT_MAX}\0 */
snprintf(mondevice, sizeof mondevice, "mon%u", n);
ret = add_mon_if(handle, sock_fd, &nlstate, device, mondevice);
if (ret == 1) {
/*
* Success. We don't clean up the libnl state
* yet, as we'll be using it later.
*/
goto added;
}
if (ret < 0) {
/*
* Hard failure. Just return ret; handle->errbuf
* has already been set.
*/
nl80211_cleanup(&nlstate);
return ret;
}
}
snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
"%s: No free monN interfaces", device);
nl80211_cleanup(&nlstate);
return PCAP_ERROR;
added:
#if 0
/*
* Sleep for .1 seconds.
*/
delay.tv_sec = 0;
delay.tv_nsec = 500000000;
nanosleep(&delay, NULL);
#endif
/*
* If we haven't already done so, arrange to have
* "pcap_close_all()" called when we exit.
*/
if (!pcap_do_addexit(handle)) {
/*
* "atexit()" failed; don't put the interface
* in rfmon mode, just give up.
*/
del_mon_if(handle, sock_fd, &nlstate, device,
handlep->mondevice);
nl80211_cleanup(&nlstate);
return PCAP_ERROR;
}
/*
* Now configure the monitor interface up.
*/
memset(&ifr, 0, sizeof(ifr));
pcap_strlcpy(ifr.ifr_name, handlep->mondevice, sizeof(ifr.ifr_name));
if (ioctl(sock_fd, SIOCGIFFLAGS, &ifr) == -1) {
pcap_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE,