The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1 | /* |
| 2 | * pcap-linux.c: Packet capture interface to the Linux kernel |
| 3 | * |
| 4 | * Copyright (c) 2000 Torsten Landschoff <torsten@debian.org> |
| 5 | * Sebastian Krahmer <krahmer@cs.uni-potsdam.de> |
| 6 | * |
| 7 | * License: BSD |
| 8 | * |
| 9 | * Redistribution and use in source and binary forms, with or without |
| 10 | * modification, are permitted provided that the following conditions |
| 11 | * are met: |
| 12 | * |
| 13 | * 1. Redistributions of source code must retain the above copyright |
| 14 | * notice, this list of conditions and the following disclaimer. |
| 15 | * 2. Redistributions in binary form must reproduce the above copyright |
| 16 | * notice, this list of conditions and the following disclaimer in |
| 17 | * the documentation and/or other materials provided with the |
| 18 | * distribution. |
| 19 | * 3. The names of the authors may not be used to endorse or promote |
| 20 | * products derived from this software without specific prior |
| 21 | * written permission. |
| 22 | * |
| 23 | * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR |
| 24 | * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED |
| 25 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 26 | * |
| 27 | * Modifications: Added PACKET_MMAP support |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 28 | * Paolo Abeni <paolo.abeni@email.it> |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 29 | * Added TPACKET_V3 support |
| 30 | * Gabor Tatarka <gabor.tatarka@ericsson.com> |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 31 | * |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 32 | * based on previous works of: |
| 33 | * Simon Patarin <patarin@cs.unibo.it> |
| 34 | * Phil Wood <cpw@lanl.gov> |
| 35 | * |
| 36 | * Monitor-mode support for mac80211 includes code taken from the iw |
| 37 | * command; the copyright notice for that code is |
| 38 | * |
| 39 | * Copyright (c) 2007, 2008 Johannes Berg |
| 40 | * Copyright (c) 2007 Andy Lutomirski |
| 41 | * Copyright (c) 2007 Mike Kershaw |
| 42 | * Copyright (c) 2008 Gábor Stefanik |
| 43 | * |
| 44 | * All rights reserved. |
| 45 | * |
| 46 | * Redistribution and use in source and binary forms, with or without |
| 47 | * modification, are permitted provided that the following conditions |
| 48 | * are met: |
| 49 | * 1. Redistributions of source code must retain the above copyright |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 50 | * notice, this list of conditions and the following disclaimer. |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 51 | * 2. Redistributions in binary form must reproduce the above copyright |
| 52 | * notice, this list of conditions and the following disclaimer in the |
| 53 | * documentation and/or other materials provided with the distribution. |
| 54 | * 3. The name of the author may not be used to endorse or promote products |
| 55 | * derived from this software without specific prior written permission. |
| 56 | * |
| 57 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 58 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 59 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 60 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 61 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
| 62 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 63 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 64 | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 65 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 66 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 67 | * SUCH DAMAGE. |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 68 | */ |
| 69 | |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 70 | |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 71 | #define _GNU_SOURCE |
| 72 | |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 73 | #ifdef HAVE_CONFIG_H |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 74 | #include <config.h> |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 75 | #endif |
| 76 | |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 77 | #include <errno.h> |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 78 | #include <stdio.h> |
| 79 | #include <stdlib.h> |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 80 | #include <unistd.h> |
| 81 | #include <fcntl.h> |
| 82 | #include <string.h> |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 83 | #include <limits.h> |
| 84 | #include <sys/stat.h> |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 85 | #include <sys/socket.h> |
| 86 | #include <sys/ioctl.h> |
| 87 | #include <sys/utsname.h> |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 88 | #include <sys/mman.h> |
| 89 | #include <linux/if.h> |
| 90 | #include <linux/if_packet.h> |
| 91 | #include <linux/sockios.h> |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 92 | #include <linux/ethtool.h> |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 93 | #include <netinet/in.h> |
| 94 | #include <linux/if_ether.h> |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 95 | #include <linux/if_arp.h> |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 96 | #include <poll.h> |
| 97 | #include <dirent.h> |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 98 | #include <sys/eventfd.h> |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 99 | |
| 100 | #include "pcap-int.h" |
| 101 | #include "pcap/sll.h" |
| 102 | #include "pcap/vlan.h" |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 103 | |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 104 | #include "diag-control.h" |
| 105 | |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 106 | /* |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 107 | * We require TPACKET_V2 support. |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 108 | */ |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 109 | #ifndef TPACKET2_HDRLEN |
| 110 | #error "Libpcap will only work if TPACKET_V2 is supported; you must build for a 2.6.27 or later kernel" |
| 111 | #endif |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 112 | |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 113 | /* check for memory mapped access avaibility. We assume every needed |
| 114 | * struct is defined if the macro TPACKET_HDRLEN is defined, because it |
| 115 | * uses many ring related structs and macros */ |
| 116 | #ifdef TPACKET3_HDRLEN |
| 117 | # define HAVE_TPACKET3 |
| 118 | #endif /* TPACKET3_HDRLEN */ |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 119 | |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 120 | #define packet_mmap_acquire(pkt) \ |
| 121 | (__atomic_load_n(&pkt->tp_status, __ATOMIC_ACQUIRE) != TP_STATUS_KERNEL) |
| 122 | #define packet_mmap_release(pkt) \ |
| 123 | (__atomic_store_n(&pkt->tp_status, TP_STATUS_KERNEL, __ATOMIC_RELEASE)) |
| 124 | #define packet_mmap_v3_acquire(pkt) \ |
| 125 | (__atomic_load_n(&pkt->hdr.bh1.block_status, __ATOMIC_ACQUIRE) != TP_STATUS_KERNEL) |
| 126 | #define packet_mmap_v3_release(pkt) \ |
| 127 | (__atomic_store_n(&pkt->hdr.bh1.block_status, TP_STATUS_KERNEL, __ATOMIC_RELEASE)) |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 128 | |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 129 | #include <linux/types.h> |
| 130 | #include <linux/filter.h> |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 131 | |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 132 | #ifdef HAVE_LINUX_NET_TSTAMP_H |
| 133 | #include <linux/net_tstamp.h> |
| 134 | #endif |
| 135 | |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 136 | /* |
| 137 | * For checking whether a device is a bonding device. |
| 138 | */ |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 139 | #include <linux/if_bonding.h> |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 140 | |
| 141 | /* |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 142 | * Got libnl? |
| 143 | */ |
| 144 | #ifdef HAVE_LIBNL |
| 145 | #include <linux/nl80211.h> |
| 146 | |
| 147 | #include <netlink/genl/genl.h> |
| 148 | #include <netlink/genl/family.h> |
| 149 | #include <netlink/genl/ctrl.h> |
| 150 | #include <netlink/msg.h> |
| 151 | #include <netlink/attr.h> |
| 152 | #endif /* HAVE_LIBNL */ |
| 153 | |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 154 | #ifndef HAVE_SOCKLEN_T |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 155 | typedef int socklen_t; |
| 156 | #endif |
| 157 | |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 158 | #define MAX_LINKHEADER_SIZE 256 |
| 159 | |
| 160 | /* |
| 161 | * When capturing on all interfaces we use this as the buffer size. |
| 162 | * Should be bigger then all MTUs that occur in real life. |
| 163 | * 64kB should be enough for now. |
| 164 | */ |
| 165 | #define BIGGER_THAN_ALL_MTUS (64*1024) |
| 166 | |
| 167 | /* |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 168 | * Private data for capturing on Linux PF_PACKET sockets. |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 169 | */ |
| 170 | struct pcap_linux { |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 171 | long long sysfs_dropped; /* packets reported dropped by /sys/class/net/{if_name}/statistics/rx_{missed,fifo}_errors */ |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 172 | struct pcap_stat stat; |
| 173 | |
| 174 | char *device; /* device name */ |
| 175 | int filter_in_userland; /* must filter in userland */ |
| 176 | int blocks_to_filter_in_userland; |
| 177 | int must_do_on_close; /* stuff we must do when we close */ |
| 178 | int timeout; /* timeout for buffering */ |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 179 | int cooked; /* using SOCK_DGRAM rather than SOCK_RAW */ |
| 180 | int ifindex; /* interface index of device we're bound to */ |
| 181 | int lo_ifindex; /* interface index of the loopback device */ |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 182 | int netdown; /* we got an ENETDOWN and haven't resolved it */ |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 183 | bpf_u_int32 oldmode; /* mode to restore when turning monitor mode off */ |
| 184 | char *mondevice; /* mac80211 monitor device we created */ |
| 185 | u_char *mmapbuf; /* memory-mapped region pointer */ |
| 186 | size_t mmapbuflen; /* size of region */ |
| 187 | int vlan_offset; /* offset at which to insert vlan tags; if -1, don't insert */ |
| 188 | u_int tp_version; /* version of tpacket_hdr for mmaped ring */ |
| 189 | u_int tp_hdrlen; /* hdrlen of tpacket_hdr for mmaped ring */ |
| 190 | u_char *oneshot_buffer; /* buffer for copy of packet */ |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 191 | int poll_timeout; /* timeout to use in poll() */ |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 192 | #ifdef HAVE_TPACKET3 |
| 193 | unsigned char *current_packet; /* Current packet within the TPACKET_V3 block. Move to next block if NULL. */ |
| 194 | int packets_left; /* Unhandled packets left within the block from previous call to pcap_read_linux_mmap_v3 in case of TPACKET_V3. */ |
| 195 | #endif |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 196 | int poll_breakloop_fd; /* fd to an eventfd to break from blocking operations */ |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 197 | }; |
| 198 | |
| 199 | /* |
| 200 | * Stuff to do when we close. |
| 201 | */ |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 202 | #define MUST_CLEAR_RFMON 0x00000001 /* clear rfmon (monitor) mode */ |
| 203 | #define MUST_DELETE_MONIF 0x00000002 /* delete monitor-mode interface */ |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 204 | |
| 205 | /* |
| 206 | * Prototypes for internal functions and methods. |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 207 | */ |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 208 | static int get_if_flags(const char *, bpf_u_int32 *, char *); |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 209 | static int is_wifi(const char *); |
| 210 | static void map_arphrd_to_dlt(pcap_t *, int, const char *, int); |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 211 | static int pcap_activate_linux(pcap_t *); |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 212 | static int activate_pf_packet(pcap_t *, int); |
| 213 | static int setup_mmapped(pcap_t *, int *); |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 214 | static int pcap_can_set_rfmon_linux(pcap_t *); |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 215 | static int pcap_inject_linux(pcap_t *, const void *, int); |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 216 | static int pcap_stats_linux(pcap_t *, struct pcap_stat *); |
| 217 | static int pcap_setfilter_linux(pcap_t *, struct bpf_program *); |
| 218 | static int pcap_setdirection_linux(pcap_t *, pcap_direction_t); |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 219 | static int pcap_set_datalink_linux(pcap_t *, int); |
| 220 | static void pcap_cleanup_linux(pcap_t *); |
| 221 | |
| 222 | union thdr { |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 223 | struct tpacket2_hdr *h2; |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 224 | #ifdef HAVE_TPACKET3 |
| 225 | struct tpacket_block_desc *h3; |
| 226 | #endif |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 227 | u_char *raw; |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 228 | }; |
| 229 | |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 230 | #define RING_GET_FRAME_AT(h, offset) (((u_char **)h->buffer)[(offset)]) |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 231 | #define RING_GET_CURRENT_FRAME(h) RING_GET_FRAME_AT(h, h->offset) |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 232 | |
| 233 | static void destroy_ring(pcap_t *handle); |
| 234 | static int create_ring(pcap_t *handle, int *status); |
| 235 | static int prepare_tpacket_socket(pcap_t *handle); |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 236 | static int pcap_read_linux_mmap_v2(pcap_t *, int, pcap_handler , u_char *); |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 237 | #ifdef HAVE_TPACKET3 |
| 238 | static int pcap_read_linux_mmap_v3(pcap_t *, int, pcap_handler , u_char *); |
| 239 | #endif |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 240 | static int pcap_setnonblock_linux(pcap_t *p, int nonblock); |
| 241 | static int pcap_getnonblock_linux(pcap_t *p); |
| 242 | static void pcap_oneshot_linux(u_char *user, const struct pcap_pkthdr *h, |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 243 | const u_char *bytes); |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 244 | |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 245 | /* |
| 246 | * In pre-3.0 kernels, the tp_vlan_tci field is set to whatever the |
| 247 | * vlan_tci field in the skbuff is. 0 can either mean "not on a VLAN" |
| 248 | * or "on VLAN 0". There is no flag set in the tp_status field to |
| 249 | * distinguish between them. |
| 250 | * |
| 251 | * In 3.0 and later kernels, if there's a VLAN tag present, the tp_vlan_tci |
| 252 | * field is set to the VLAN tag, and the TP_STATUS_VLAN_VALID flag is set |
| 253 | * in the tp_status field, otherwise the tp_vlan_tci field is set to 0 and |
| 254 | * the TP_STATUS_VLAN_VALID flag isn't set in the tp_status field. |
| 255 | * |
| 256 | * With a pre-3.0 kernel, we cannot distinguish between packets with no |
| 257 | * VLAN tag and packets on VLAN 0, so we will mishandle some packets, and |
| 258 | * there's nothing we can do about that. |
| 259 | * |
| 260 | * So, on those systems, which never set the TP_STATUS_VLAN_VALID flag, we |
| 261 | * continue the behavior of earlier libpcaps, wherein we treated packets |
| 262 | * with a VLAN tag of 0 as being packets without a VLAN tag rather than packets |
| 263 | * on VLAN 0. We do this by treating packets with a tp_vlan_tci of 0 and |
| 264 | * with the TP_STATUS_VLAN_VALID flag not set in tp_status as not having |
| 265 | * VLAN tags. This does the right thing on 3.0 and later kernels, and |
| 266 | * continues the old unfixably-imperfect behavior on pre-3.0 kernels. |
| 267 | * |
| 268 | * If TP_STATUS_VLAN_VALID isn't defined, we test it as the 0x10 bit; it |
| 269 | * has that value in 3.0 and later kernels. |
| 270 | */ |
| 271 | #ifdef TP_STATUS_VLAN_VALID |
| 272 | #define VLAN_VALID(hdr, hv) ((hv)->tp_vlan_tci != 0 || ((hdr)->tp_status & TP_STATUS_VLAN_VALID)) |
| 273 | #else |
| 274 | /* |
| 275 | * This is being compiled on a system that lacks TP_STATUS_VLAN_VALID, |
| 276 | * so we testwith the value it has in the 3.0 and later kernels, so |
| 277 | * we can test it if we're running on a system that has it. (If we're |
| 278 | * running on a system that doesn't have it, it won't be set in the |
| 279 | * tp_status field, so the tests of it will always fail; that means |
| 280 | * we behave the way we did before we introduced this macro.) |
| 281 | */ |
| 282 | #define VLAN_VALID(hdr, hv) ((hv)->tp_vlan_tci != 0 || ((hdr)->tp_status & 0x10)) |
| 283 | #endif |
| 284 | |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 285 | #ifdef TP_STATUS_VLAN_TPID_VALID |
| 286 | # define VLAN_TPID(hdr, hv) (((hv)->tp_vlan_tpid || ((hdr)->tp_status & TP_STATUS_VLAN_TPID_VALID)) ? (hv)->tp_vlan_tpid : ETH_P_8021Q) |
| 287 | #else |
| 288 | # define VLAN_TPID(hdr, hv) ETH_P_8021Q |
| 289 | #endif |
| 290 | |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 291 | /* |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 292 | * Required select timeout if we're polling for an "interface disappeared" |
| 293 | * indication - 1 millisecond. |
| 294 | */ |
| 295 | static const struct timeval netdown_timeout = { |
| 296 | 0, 1000 /* 1000 microseconds = 1 millisecond */ |
| 297 | }; |
| 298 | |
| 299 | /* |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 300 | * Wrap some ioctl calls |
| 301 | */ |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 302 | static int iface_get_id(int fd, const char *device, char *ebuf); |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 303 | static int iface_get_mtu(int fd, const char *device, char *ebuf); |
| 304 | static int iface_get_arptype(int fd, const char *device, char *ebuf); |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 305 | static int iface_bind(int fd, int ifindex, char *ebuf, int protocol); |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 306 | static int enter_rfmon_mode(pcap_t *handle, int sock_fd, |
| 307 | const char *device); |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 308 | #if defined(HAVE_LINUX_NET_TSTAMP_H) && defined(PACKET_TIMESTAMP) |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 309 | static int iface_ethtool_get_ts_info(const char *device, pcap_t *handle, |
| 310 | char *ebuf); |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 311 | #endif |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 312 | static int iface_get_offload(pcap_t *handle); |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 313 | |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 314 | static int fix_program(pcap_t *handle, struct sock_fprog *fcode); |
Haibo Huang | 4ccd683 | 2020-04-23 18:03:48 -0700 | [diff] [blame] | 315 | static int fix_offset(pcap_t *handle, struct bpf_insn *p); |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 316 | static int set_kernel_filter(pcap_t *handle, struct sock_fprog *fcode); |
| 317 | static int reset_kernel_filter(pcap_t *handle); |
| 318 | |
| 319 | static struct sock_filter total_insn |
| 320 | = BPF_STMT(BPF_RET | BPF_K, 0); |
| 321 | static struct sock_fprog total_fcode |
| 322 | = { 1, &total_insn }; |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 323 | |
| 324 | static int iface_dsa_get_proto_info(const char *device, pcap_t *handle); |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 325 | |
| 326 | pcap_t * |
| 327 | pcap_create_interface(const char *device, char *ebuf) |
| 328 | { |
| 329 | pcap_t *handle; |
| 330 | |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 331 | handle = PCAP_CREATE_COMMON(ebuf, struct pcap_linux); |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 332 | if (handle == NULL) |
| 333 | return NULL; |
| 334 | |
| 335 | handle->activate_op = pcap_activate_linux; |
| 336 | handle->can_set_rfmon_op = pcap_can_set_rfmon_linux; |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 337 | |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 338 | #if defined(HAVE_LINUX_NET_TSTAMP_H) && defined(PACKET_TIMESTAMP) |
| 339 | /* |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 340 | * See what time stamp types we support. |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 341 | */ |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 342 | if (iface_ethtool_get_ts_info(device, handle, ebuf) == -1) { |
| 343 | pcap_close(handle); |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 344 | return NULL; |
| 345 | } |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 346 | #endif |
| 347 | |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 348 | /* |
| 349 | * We claim that we support microsecond and nanosecond time |
| 350 | * stamps. |
| 351 | * |
| 352 | * XXX - with adapter-supplied time stamps, can we choose |
| 353 | * microsecond or nanosecond time stamps on arbitrary |
| 354 | * adapters? |
| 355 | */ |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 356 | handle->tstamp_precision_list = malloc(2 * sizeof(u_int)); |
| 357 | if (handle->tstamp_precision_list == NULL) { |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 358 | pcap_fmt_errmsg_for_errno(ebuf, PCAP_ERRBUF_SIZE, |
| 359 | errno, "malloc"); |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 360 | pcap_close(handle); |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 361 | return NULL; |
| 362 | } |
| 363 | handle->tstamp_precision_list[0] = PCAP_TSTAMP_PRECISION_MICRO; |
| 364 | handle->tstamp_precision_list[1] = PCAP_TSTAMP_PRECISION_NANO; |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 365 | handle->tstamp_precision_count = 2; |
| 366 | |
| 367 | struct pcap_linux *handlep = handle->priv; |
| 368 | handlep->poll_breakloop_fd = eventfd(0, EFD_NONBLOCK); |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 369 | |
| 370 | return handle; |
| 371 | } |
| 372 | |
| 373 | #ifdef HAVE_LIBNL |
| 374 | /* |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 375 | * If interface {if_name} is a mac80211 driver, the file |
| 376 | * /sys/class/net/{if_name}/phy80211 is a symlink to |
| 377 | * /sys/class/ieee80211/{phydev_name}, for some {phydev_name}. |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 378 | * |
| 379 | * On Fedora 9, with a 2.6.26.3-29 kernel, my Zydas stick, at |
| 380 | * least, has a "wmaster0" device and a "wlan0" device; the |
| 381 | * latter is the one with the IP address. Both show up in |
| 382 | * "tcpdump -D" output. Capturing on the wmaster0 device |
| 383 | * captures with 802.11 headers. |
| 384 | * |
| 385 | * airmon-ng searches through /sys/class/net for devices named |
| 386 | * monN, starting with mon0; as soon as one *doesn't* exist, |
| 387 | * it chooses that as the monitor device name. If the "iw" |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 388 | * command exists, it does |
| 389 | * |
| 390 | * iw dev {if_name} interface add {monif_name} type monitor |
| 391 | * |
| 392 | * where {monif_name} is the monitor device. It then (sigh) sleeps |
| 393 | * .1 second, and then configures the device up. Otherwise, if |
| 394 | * /sys/class/ieee80211/{phydev_name}/add_iface is a file, it writes |
| 395 | * {mondev_name}, without a newline, to that file, and again (sigh) |
| 396 | * sleeps .1 second, and then iwconfig's that device into monitor |
| 397 | * mode and configures it up. Otherwise, you can't do monitor mode. |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 398 | * |
| 399 | * All these devices are "glued" together by having the |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 400 | * /sys/class/net/{if_name}/phy80211 links pointing to the same |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 401 | * place, so, given a wmaster, wlan, or mon device, you can |
| 402 | * find the other devices by looking for devices with |
| 403 | * the same phy80211 link. |
| 404 | * |
| 405 | * To turn monitor mode off, delete the monitor interface, |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 406 | * either with |
| 407 | * |
| 408 | * iw dev {monif_name} interface del |
| 409 | * |
| 410 | * or by sending {monif_name}, with no NL, down |
| 411 | * /sys/class/ieee80211/{phydev_name}/remove_iface |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 412 | * |
| 413 | * Note: if you try to create a monitor device named "monN", and |
| 414 | * there's already a "monN" device, it fails, as least with |
| 415 | * the netlink interface (which is what iw uses), with a return |
| 416 | * value of -ENFILE. (Return values are negative errnos.) We |
| 417 | * could probably use that to find an unused device. |
| 418 | * |
| 419 | * Yes, you can have multiple monitor devices for a given |
| 420 | * physical device. |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 421 | */ |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 422 | |
| 423 | /* |
| 424 | * Is this a mac80211 device? If so, fill in the physical device path and |
| 425 | * return 1; if not, return 0. On an error, fill in handle->errbuf and |
| 426 | * return PCAP_ERROR. |
| 427 | */ |
| 428 | static int |
| 429 | get_mac80211_phydev(pcap_t *handle, const char *device, char *phydev_path, |
| 430 | size_t phydev_max_pathlen) |
| 431 | { |
| 432 | char *pathstr; |
| 433 | ssize_t bytes_read; |
| 434 | |
| 435 | /* |
| 436 | * Generate the path string for the symlink to the physical device. |
| 437 | */ |
| 438 | if (asprintf(&pathstr, "/sys/class/net/%s/phy80211", device) == -1) { |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 439 | snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 440 | "%s: Can't generate path name string for /sys/class/net device", |
| 441 | device); |
| 442 | return PCAP_ERROR; |
| 443 | } |
| 444 | bytes_read = readlink(pathstr, phydev_path, phydev_max_pathlen); |
| 445 | if (bytes_read == -1) { |
| 446 | if (errno == ENOENT || errno == EINVAL) { |
| 447 | /* |
| 448 | * Doesn't exist, or not a symlink; assume that |
| 449 | * means it's not a mac80211 device. |
| 450 | */ |
| 451 | free(pathstr); |
| 452 | return 0; |
| 453 | } |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 454 | pcap_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE, |
| 455 | errno, "%s: Can't readlink %s", device, pathstr); |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 456 | free(pathstr); |
| 457 | return PCAP_ERROR; |
| 458 | } |
| 459 | free(pathstr); |
| 460 | phydev_path[bytes_read] = '\0'; |
| 461 | return 1; |
| 462 | } |
| 463 | |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 464 | struct nl80211_state { |
| 465 | struct nl_sock *nl_sock; |
| 466 | struct nl_cache *nl_cache; |
| 467 | struct genl_family *nl80211; |
| 468 | }; |
| 469 | |
| 470 | static int |
| 471 | nl80211_init(pcap_t *handle, struct nl80211_state *state, const char *device) |
| 472 | { |
| 473 | int err; |
| 474 | |
| 475 | state->nl_sock = nl_socket_alloc(); |
| 476 | if (!state->nl_sock) { |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 477 | snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 478 | "%s: failed to allocate netlink handle", device); |
| 479 | return PCAP_ERROR; |
| 480 | } |
| 481 | |
| 482 | if (genl_connect(state->nl_sock)) { |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 483 | snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 484 | "%s: failed to connect to generic netlink", device); |
| 485 | goto out_handle_destroy; |
| 486 | } |
| 487 | |
| 488 | err = genl_ctrl_alloc_cache(state->nl_sock, &state->nl_cache); |
| 489 | if (err < 0) { |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 490 | snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 491 | "%s: failed to allocate generic netlink cache: %s", |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 492 | device, nl_geterror(-err)); |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 493 | goto out_handle_destroy; |
| 494 | } |
| 495 | |
| 496 | state->nl80211 = genl_ctrl_search_by_name(state->nl_cache, "nl80211"); |
| 497 | if (!state->nl80211) { |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 498 | snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 499 | "%s: nl80211 not found", device); |
| 500 | goto out_cache_free; |
| 501 | } |
| 502 | |
| 503 | return 0; |
| 504 | |
| 505 | out_cache_free: |
| 506 | nl_cache_free(state->nl_cache); |
| 507 | out_handle_destroy: |
| 508 | nl_socket_free(state->nl_sock); |
| 509 | return PCAP_ERROR; |
| 510 | } |
| 511 | |
| 512 | static void |
| 513 | nl80211_cleanup(struct nl80211_state *state) |
| 514 | { |
| 515 | genl_family_put(state->nl80211); |
| 516 | nl_cache_free(state->nl_cache); |
| 517 | nl_socket_free(state->nl_sock); |
| 518 | } |
| 519 | |
| 520 | static int |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 521 | del_mon_if(pcap_t *handle, int sock_fd, struct nl80211_state *state, |
| 522 | const char *device, const char *mondevice); |
| 523 | |
| 524 | static int |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 525 | add_mon_if(pcap_t *handle, int sock_fd, struct nl80211_state *state, |
| 526 | const char *device, const char *mondevice) |
| 527 | { |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 528 | struct pcap_linux *handlep = handle->priv; |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 529 | int ifindex; |
| 530 | struct nl_msg *msg; |
| 531 | int err; |
| 532 | |
| 533 | ifindex = iface_get_id(sock_fd, device, handle->errbuf); |
| 534 | if (ifindex == -1) |
| 535 | return PCAP_ERROR; |
| 536 | |
| 537 | msg = nlmsg_alloc(); |
| 538 | if (!msg) { |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 539 | snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 540 | "%s: failed to allocate netlink msg", device); |
| 541 | return PCAP_ERROR; |
| 542 | } |
| 543 | |
| 544 | genlmsg_put(msg, 0, 0, genl_family_get_id(state->nl80211), 0, |
| 545 | 0, NL80211_CMD_NEW_INTERFACE, 0); |
| 546 | NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex); |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 547 | DIAG_OFF_NARROWING |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 548 | NLA_PUT_STRING(msg, NL80211_ATTR_IFNAME, mondevice); |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 549 | DIAG_ON_NARROWING |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 550 | NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, NL80211_IFTYPE_MONITOR); |
| 551 | |
| 552 | err = nl_send_auto_complete(state->nl_sock, msg); |
| 553 | if (err < 0) { |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 554 | if (err == -NLE_FAILURE) { |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 555 | /* |
| 556 | * Device not available; our caller should just |
| 557 | * keep trying. (libnl 2.x maps ENFILE to |
| 558 | * NLE_FAILURE; it can also map other errors |
| 559 | * to that, but there's not much we can do |
| 560 | * about that.) |
| 561 | */ |
| 562 | nlmsg_free(msg); |
| 563 | return 0; |
| 564 | } else { |
| 565 | /* |
| 566 | * Real failure, not just "that device is not |
| 567 | * available. |
| 568 | */ |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 569 | snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 570 | "%s: nl_send_auto_complete failed adding %s interface: %s", |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 571 | device, mondevice, nl_geterror(-err)); |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 572 | nlmsg_free(msg); |
| 573 | return PCAP_ERROR; |
| 574 | } |
| 575 | } |
| 576 | err = nl_wait_for_ack(state->nl_sock); |
| 577 | if (err < 0) { |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 578 | if (err == -NLE_FAILURE) { |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 579 | /* |
| 580 | * Device not available; our caller should just |
| 581 | * keep trying. (libnl 2.x maps ENFILE to |
| 582 | * NLE_FAILURE; it can also map other errors |
| 583 | * to that, but there's not much we can do |
| 584 | * about that.) |
| 585 | */ |
| 586 | nlmsg_free(msg); |
| 587 | return 0; |
| 588 | } else { |
| 589 | /* |
| 590 | * Real failure, not just "that device is not |
| 591 | * available. |
| 592 | */ |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 593 | snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 594 | "%s: nl_wait_for_ack failed adding %s interface: %s", |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 595 | device, mondevice, nl_geterror(-err)); |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 596 | nlmsg_free(msg); |
| 597 | return PCAP_ERROR; |
| 598 | } |
| 599 | } |
| 600 | |
| 601 | /* |
| 602 | * Success. |
| 603 | */ |
| 604 | nlmsg_free(msg); |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 605 | |
| 606 | /* |
| 607 | * Try to remember the monitor device. |
| 608 | */ |
| 609 | handlep->mondevice = strdup(mondevice); |
| 610 | if (handlep->mondevice == NULL) { |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 611 | pcap_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE, |
| 612 | errno, "strdup"); |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 613 | /* |
| 614 | * Get rid of the monitor device. |
| 615 | */ |
| 616 | del_mon_if(handle, sock_fd, state, device, mondevice); |
| 617 | return PCAP_ERROR; |
| 618 | } |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 619 | return 1; |
| 620 | |
| 621 | nla_put_failure: |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 622 | snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 623 | "%s: nl_put failed adding %s interface", |
| 624 | device, mondevice); |
| 625 | nlmsg_free(msg); |
| 626 | return PCAP_ERROR; |
| 627 | } |
| 628 | |
| 629 | static int |
| 630 | del_mon_if(pcap_t *handle, int sock_fd, struct nl80211_state *state, |
| 631 | const char *device, const char *mondevice) |
| 632 | { |
| 633 | int ifindex; |
| 634 | struct nl_msg *msg; |
| 635 | int err; |
| 636 | |
| 637 | ifindex = iface_get_id(sock_fd, mondevice, handle->errbuf); |
| 638 | if (ifindex == -1) |
| 639 | return PCAP_ERROR; |
| 640 | |
| 641 | msg = nlmsg_alloc(); |
| 642 | if (!msg) { |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 643 | snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 644 | "%s: failed to allocate netlink msg", device); |
| 645 | return PCAP_ERROR; |
| 646 | } |
| 647 | |
| 648 | genlmsg_put(msg, 0, 0, genl_family_get_id(state->nl80211), 0, |
| 649 | 0, NL80211_CMD_DEL_INTERFACE, 0); |
| 650 | NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex); |
| 651 | |
| 652 | err = nl_send_auto_complete(state->nl_sock, msg); |
| 653 | if (err < 0) { |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 654 | snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 655 | "%s: nl_send_auto_complete failed deleting %s interface: %s", |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 656 | device, mondevice, nl_geterror(-err)); |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 657 | nlmsg_free(msg); |
| 658 | return PCAP_ERROR; |
| 659 | } |
| 660 | err = nl_wait_for_ack(state->nl_sock); |
| 661 | if (err < 0) { |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 662 | snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 663 | "%s: nl_wait_for_ack failed adding %s interface: %s", |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 664 | device, mondevice, nl_geterror(-err)); |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 665 | nlmsg_free(msg); |
| 666 | return PCAP_ERROR; |
| 667 | } |
| 668 | |
| 669 | /* |
| 670 | * Success. |
| 671 | */ |
| 672 | nlmsg_free(msg); |
| 673 | return 1; |
| 674 | |
| 675 | nla_put_failure: |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 676 | snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 677 | "%s: nl_put failed deleting %s interface", |
| 678 | device, mondevice); |
| 679 | nlmsg_free(msg); |
| 680 | return PCAP_ERROR; |
| 681 | } |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 682 | #endif /* HAVE_LIBNL */ |
| 683 | |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 684 | static int pcap_protocol(pcap_t *handle) |
| 685 | { |
| 686 | int protocol; |
| 687 | |
| 688 | protocol = handle->opt.protocol; |
| 689 | if (protocol == 0) |
| 690 | protocol = ETH_P_ALL; |
| 691 | |
| 692 | return htons(protocol); |
| 693 | } |
| 694 | |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 695 | static int |
| 696 | pcap_can_set_rfmon_linux(pcap_t *handle) |
| 697 | { |
| 698 | #ifdef HAVE_LIBNL |
| 699 | char phydev_path[PATH_MAX+1]; |
| 700 | int ret; |
| 701 | #endif |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 702 | |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 703 | if (strcmp(handle->opt.device, "any") == 0) { |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 704 | /* |
| 705 | * Monitor mode makes no sense on the "any" device. |
| 706 | */ |
| 707 | return 0; |
| 708 | } |
| 709 | |
| 710 | #ifdef HAVE_LIBNL |
| 711 | /* |
| 712 | * Bleah. There doesn't seem to be a way to ask a mac80211 |
| 713 | * device, through libnl, whether it supports monitor mode; |
| 714 | * we'll just check whether the device appears to be a |
| 715 | * mac80211 device and, if so, assume the device supports |
| 716 | * monitor mode. |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 717 | */ |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 718 | ret = get_mac80211_phydev(handle, handle->opt.device, phydev_path, |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 719 | PATH_MAX); |
| 720 | if (ret < 0) |
| 721 | return ret; /* error */ |
| 722 | if (ret == 1) |
| 723 | return 1; /* mac80211 device */ |
| 724 | #endif |
| 725 | |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 726 | return 0; |
| 727 | } |
| 728 | |
| 729 | /* |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 730 | * Grabs the number of missed packets by the interface from |
| 731 | * /sys/class/net/{if_name}/statistics/rx_{missed,fifo}_errors. |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 732 | * |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 733 | * Compared to /proc/net/dev this avoids counting software drops, |
| 734 | * but may be unimplemented and just return 0. |
| 735 | * The author has found no straigthforward way to check for support. |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 736 | */ |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 737 | static long long int |
| 738 | linux_get_stat(const char * if_name, const char * stat) { |
| 739 | ssize_t bytes_read; |
| 740 | int fd; |
| 741 | char buffer[PATH_MAX]; |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 742 | |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 743 | snprintf(buffer, sizeof(buffer), "/sys/class/net/%s/statistics/%s", if_name, stat); |
| 744 | fd = open(buffer, O_RDONLY); |
| 745 | if (fd == -1) |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 746 | return 0; |
| 747 | |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 748 | bytes_read = read(fd, buffer, sizeof(buffer) - 1); |
| 749 | close(fd); |
| 750 | if (bytes_read == -1) |
| 751 | return 0; |
| 752 | buffer[bytes_read] = '\0'; |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 753 | |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 754 | return strtoll(buffer, NULL, 10); |
| 755 | } |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 756 | |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 757 | static long long int |
| 758 | linux_if_drops(const char * if_name) |
| 759 | { |
| 760 | long long int missed = linux_get_stat(if_name, "rx_missed_errors"); |
| 761 | long long int fifo = linux_get_stat(if_name, "rx_fifo_errors"); |
| 762 | return missed + fifo; |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 763 | } |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 764 | |
| 765 | |
| 766 | /* |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 767 | * Monitor mode is kind of interesting because we have to reset the |
| 768 | * interface before exiting. The problem can't really be solved without |
| 769 | * some daemon taking care of managing usage counts. If we put the |
| 770 | * interface into monitor mode, we set a flag indicating that we must |
| 771 | * take it out of that mode when the interface is closed, and, when |
| 772 | * closing the interface, if that flag is set we take it out of monitor |
| 773 | * mode. |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 774 | */ |
| 775 | |
| 776 | static void pcap_cleanup_linux( pcap_t *handle ) |
| 777 | { |
| 778 | struct pcap_linux *handlep = handle->priv; |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 779 | #ifdef HAVE_LIBNL |
| 780 | struct nl80211_state nlstate; |
| 781 | int ret; |
| 782 | #endif /* HAVE_LIBNL */ |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 783 | |
| 784 | if (handlep->must_do_on_close != 0) { |
| 785 | /* |
| 786 | * There's something we have to do when closing this |
| 787 | * pcap_t. |
| 788 | */ |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 789 | #ifdef HAVE_LIBNL |
| 790 | if (handlep->must_do_on_close & MUST_DELETE_MONIF) { |
| 791 | ret = nl80211_init(handle, &nlstate, handlep->device); |
| 792 | if (ret >= 0) { |
| 793 | ret = del_mon_if(handle, handle->fd, &nlstate, |
| 794 | handlep->device, handlep->mondevice); |
| 795 | nl80211_cleanup(&nlstate); |
| 796 | } |
| 797 | if (ret < 0) { |
| 798 | fprintf(stderr, |
| 799 | "Can't delete monitor interface %s (%s).\n" |
| 800 | "Please delete manually.\n", |
| 801 | handlep->mondevice, handle->errbuf); |
| 802 | } |
| 803 | } |
| 804 | #endif /* HAVE_LIBNL */ |
| 805 | |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 806 | /* |
| 807 | * Take this pcap out of the list of pcaps for which we |
| 808 | * have to take the interface out of some mode. |
| 809 | */ |
| 810 | pcap_remove_from_pcaps_to_close(handle); |
| 811 | } |
| 812 | |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 813 | if (handle->fd != -1) { |
| 814 | /* |
| 815 | * Destroy the ring buffer (assuming we've set it up), |
| 816 | * and unmap it if it's mapped. |
| 817 | */ |
| 818 | destroy_ring(handle); |
| 819 | } |
| 820 | |
| 821 | if (handlep->oneshot_buffer != NULL) { |
| 822 | free(handlep->oneshot_buffer); |
| 823 | handlep->oneshot_buffer = NULL; |
| 824 | } |
| 825 | |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 826 | if (handlep->mondevice != NULL) { |
| 827 | free(handlep->mondevice); |
| 828 | handlep->mondevice = NULL; |
| 829 | } |
| 830 | if (handlep->device != NULL) { |
| 831 | free(handlep->device); |
| 832 | handlep->device = NULL; |
| 833 | } |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 834 | |
| 835 | close(handlep->poll_breakloop_fd); |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 836 | pcap_cleanup_live_common(handle); |
| 837 | } |
| 838 | |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 839 | #ifdef HAVE_TPACKET3 |
| 840 | /* |
| 841 | * Some versions of TPACKET_V3 have annoying bugs/misfeatures |
| 842 | * around which we have to work. Determine if we have those |
| 843 | * problems or not. |
| 844 | * 3.19 is the first release with a fixed version of |
| 845 | * TPACKET_V3. We treat anything before that as |
| 846 | * not having a fixed version; that may really mean |
| 847 | * it has *no* version. |
| 848 | */ |
| 849 | static int has_broken_tpacket_v3(void) |
| 850 | { |
| 851 | struct utsname utsname; |
| 852 | const char *release; |
| 853 | long major, minor; |
| 854 | int matches, verlen; |
| 855 | |
| 856 | /* No version information, assume broken. */ |
| 857 | if (uname(&utsname) == -1) |
| 858 | return 1; |
| 859 | release = utsname.release; |
| 860 | |
| 861 | /* A malformed version, ditto. */ |
| 862 | matches = sscanf(release, "%ld.%ld%n", &major, &minor, &verlen); |
| 863 | if (matches != 2) |
| 864 | return 1; |
| 865 | if (release[verlen] != '.' && release[verlen] != '\0') |
| 866 | return 1; |
| 867 | |
| 868 | /* OK, a fixed version. */ |
| 869 | if (major > 3 || (major == 3 && minor >= 19)) |
| 870 | return 0; |
| 871 | |
| 872 | /* Too old :( */ |
| 873 | return 1; |
| 874 | } |
| 875 | #endif |
| 876 | |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 877 | /* |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 878 | * Set the timeout to be used in poll() with memory-mapped packet capture. |
| 879 | */ |
| 880 | static void |
| 881 | set_poll_timeout(struct pcap_linux *handlep) |
| 882 | { |
| 883 | #ifdef HAVE_TPACKET3 |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 884 | int broken_tpacket_v3 = has_broken_tpacket_v3(); |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 885 | #endif |
| 886 | if (handlep->timeout == 0) { |
| 887 | #ifdef HAVE_TPACKET3 |
| 888 | /* |
| 889 | * XXX - due to a set of (mis)features in the TPACKET_V3 |
| 890 | * kernel code prior to the 3.19 kernel, blocking forever |
| 891 | * with a TPACKET_V3 socket can, if few packets are |
| 892 | * arriving and passing the socket filter, cause most |
| 893 | * packets to be dropped. See libpcap issue #335 for the |
| 894 | * full painful story. |
| 895 | * |
| 896 | * The workaround is to have poll() time out very quickly, |
| 897 | * so we grab the frames handed to us, and return them to |
| 898 | * the kernel, ASAP. |
| 899 | */ |
| 900 | if (handlep->tp_version == TPACKET_V3 && broken_tpacket_v3) |
| 901 | handlep->poll_timeout = 1; /* don't block for very long */ |
| 902 | else |
| 903 | #endif |
| 904 | handlep->poll_timeout = -1; /* block forever */ |
| 905 | } else if (handlep->timeout > 0) { |
| 906 | #ifdef HAVE_TPACKET3 |
| 907 | /* |
| 908 | * For TPACKET_V3, the timeout is handled by the kernel, |
| 909 | * so block forever; that way, we don't get extra timeouts. |
| 910 | * Don't do that if we have a broken TPACKET_V3, though. |
| 911 | */ |
| 912 | if (handlep->tp_version == TPACKET_V3 && !broken_tpacket_v3) |
| 913 | handlep->poll_timeout = -1; /* block forever, let TPACKET_V3 wake us up */ |
| 914 | else |
| 915 | #endif |
| 916 | handlep->poll_timeout = handlep->timeout; /* block for that amount of time */ |
| 917 | } else { |
| 918 | /* |
| 919 | * Non-blocking mode; we call poll() to pick up error |
| 920 | * indications, but we don't want it to wait for |
| 921 | * anything. |
| 922 | */ |
| 923 | handlep->poll_timeout = 0; |
| 924 | } |
| 925 | } |
| 926 | |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 927 | static void pcap_breakloop_linux(pcap_t *handle) |
| 928 | { |
| 929 | pcap_breakloop_common(handle); |
| 930 | struct pcap_linux *handlep = handle->priv; |
| 931 | |
| 932 | uint64_t value = 1; |
| 933 | /* XXX - what if this fails? */ |
| 934 | (void)write(handlep->poll_breakloop_fd, &value, sizeof(value)); |
| 935 | } |
| 936 | |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 937 | /* |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 938 | * Get a handle for a live capture from the given device. You can |
| 939 | * pass NULL as device to get all packages (without link level |
| 940 | * information of course). If you pass 1 as promisc the interface |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 941 | * will be set to promiscuous mode (XXX: I think this usage should |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 942 | * be deprecated and functions be added to select that later allow |
| 943 | * modification of that values -- Torsten). |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 944 | */ |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 945 | static int |
| 946 | pcap_activate_linux(pcap_t *handle) |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 947 | { |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 948 | struct pcap_linux *handlep = handle->priv; |
| 949 | const char *device; |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 950 | int is_any_device; |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 951 | struct ifreq ifr; |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 952 | int status = 0; |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 953 | int status2 = 0; |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 954 | int ret; |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 955 | |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 956 | device = handle->opt.device; |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 957 | |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 958 | /* |
| 959 | * Make sure the name we were handed will fit into the ioctls we |
| 960 | * might perform on the device; if not, return a "No such device" |
| 961 | * indication, as the Linux kernel shouldn't support creating |
| 962 | * a device whose name won't fit into those ioctls. |
| 963 | * |
| 964 | * "Will fit" means "will fit, complete with a null terminator", |
| 965 | * so if the length, which does *not* include the null terminator, |
| 966 | * is greater than *or equal to* the size of the field into which |
| 967 | * we'll be copying it, that won't fit. |
| 968 | */ |
| 969 | if (strlen(device) >= sizeof(ifr.ifr_name)) { |
| 970 | status = PCAP_ERROR_NO_SUCH_DEVICE; |
| 971 | goto fail; |
| 972 | } |
| 973 | |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 974 | /* |
| 975 | * Turn a negative snapshot value (invalid), a snapshot value of |
| 976 | * 0 (unspecified), or a value bigger than the normal maximum |
| 977 | * value, into the maximum allowed value. |
| 978 | * |
| 979 | * If some application really *needs* a bigger snapshot |
| 980 | * length, we should just increase MAXIMUM_SNAPLEN. |
| 981 | */ |
| 982 | if (handle->snapshot <= 0 || handle->snapshot > MAXIMUM_SNAPLEN) |
| 983 | handle->snapshot = MAXIMUM_SNAPLEN; |
| 984 | |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 985 | handlep->device = strdup(device); |
| 986 | if (handlep->device == NULL) { |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 987 | pcap_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE, |
| 988 | errno, "strdup"); |
Haibo Huang | 4ccd683 | 2020-04-23 18:03:48 -0700 | [diff] [blame] | 989 | status = PCAP_ERROR; |
| 990 | goto fail; |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 991 | } |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 992 | |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 993 | /* |
| 994 | * The "any" device is a special device which causes us not |
| 995 | * to bind to a particular device and thus to look at all |
| 996 | * devices. |
| 997 | */ |
| 998 | is_any_device = (strcmp(device, "any") == 0); |
| 999 | if (is_any_device) { |
| 1000 | if (handle->opt.promisc) { |
| 1001 | handle->opt.promisc = 0; |
| 1002 | /* Just a warning. */ |
| 1003 | snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, |
| 1004 | "Promiscuous mode not supported on the \"any\" device"); |
| 1005 | status = PCAP_WARNING_PROMISC_NOTSUP; |
| 1006 | } |
| 1007 | } |
| 1008 | |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 1009 | /* copy timeout value */ |
| 1010 | handlep->timeout = handle->opt.timeout; |
| 1011 | |
| 1012 | /* |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 1013 | * If we're in promiscuous mode, then we probably want |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 1014 | * to see when the interface drops packets too, so get an |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 1015 | * initial count from |
| 1016 | * /sys/class/net/{if_name}/statistics/rx_{missed,fifo}_errors |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 1017 | */ |
| 1018 | if (handle->opt.promisc) |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 1019 | handlep->sysfs_dropped = linux_if_drops(handlep->device); |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 1020 | |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1021 | /* |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 1022 | * If the "any" device is specified, try to open a SOCK_DGRAM. |
| 1023 | * Otherwise, open a SOCK_RAW. |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1024 | */ |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 1025 | ret = activate_pf_packet(handle, is_any_device); |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 1026 | if (ret < 0) { |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1027 | /* |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 1028 | * Fatal error; the return value is the error code, |
| 1029 | * and handle->errbuf has been set to an appropriate |
| 1030 | * error message. |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1031 | */ |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 1032 | status = ret; |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 1033 | goto fail; |
| 1034 | } |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1035 | /* |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 1036 | * Success. |
| 1037 | * Try to set up memory-mapped access. |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1038 | */ |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 1039 | ret = setup_mmapped(handle, &status); |
| 1040 | if (ret == -1) { |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1041 | /* |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 1042 | * We failed to set up to use it, or the |
| 1043 | * kernel supports it, but we failed to |
| 1044 | * enable it. status has been set to the |
| 1045 | * error status to return and, if it's |
| 1046 | * PCAP_ERROR, handle->errbuf contains |
| 1047 | * the error message. |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1048 | */ |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 1049 | goto fail; |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1050 | } |
| 1051 | |
| 1052 | /* |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 1053 | * We succeeded. status has been set to the status to return, |
| 1054 | * which might be 0, or might be a PCAP_WARNING_ value. |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1055 | */ |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 1056 | /* |
| 1057 | * Now that we have activated the mmap ring, we can |
| 1058 | * set the correct protocol. |
| 1059 | */ |
| 1060 | if ((status2 = iface_bind(handle->fd, handlep->ifindex, |
| 1061 | handle->errbuf, pcap_protocol(handle))) != 0) { |
| 1062 | status = status2; |
| 1063 | goto fail; |
| 1064 | } |
| 1065 | |
| 1066 | handle->inject_op = pcap_inject_linux; |
| 1067 | handle->setfilter_op = pcap_setfilter_linux; |
| 1068 | handle->setdirection_op = pcap_setdirection_linux; |
| 1069 | handle->set_datalink_op = pcap_set_datalink_linux; |
| 1070 | handle->setnonblock_op = pcap_setnonblock_linux; |
| 1071 | handle->getnonblock_op = pcap_getnonblock_linux; |
| 1072 | handle->cleanup_op = pcap_cleanup_linux; |
| 1073 | handle->stats_op = pcap_stats_linux; |
| 1074 | handle->breakloop_op = pcap_breakloop_linux; |
| 1075 | |
| 1076 | switch (handlep->tp_version) { |
| 1077 | |
| 1078 | case TPACKET_V2: |
| 1079 | handle->read_op = pcap_read_linux_mmap_v2; |
| 1080 | break; |
| 1081 | #ifdef HAVE_TPACKET3 |
| 1082 | case TPACKET_V3: |
| 1083 | handle->read_op = pcap_read_linux_mmap_v3; |
| 1084 | break; |
| 1085 | #endif |
| 1086 | } |
| 1087 | handle->oneshot_callback = pcap_oneshot_linux; |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1088 | handle->selectable_fd = handle->fd; |
| 1089 | |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 1090 | return status; |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1091 | |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 1092 | fail: |
| 1093 | pcap_cleanup_linux(handle); |
| 1094 | return status; |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1095 | } |
| 1096 | |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 1097 | static int |
| 1098 | pcap_set_datalink_linux(pcap_t *handle, int dlt) |
| 1099 | { |
| 1100 | handle->linktype = dlt; |
| 1101 | return 0; |
| 1102 | } |
| 1103 | |
| 1104 | /* |
| 1105 | * linux_check_direction() |
| 1106 | * |
| 1107 | * Do checks based on packet direction. |
| 1108 | */ |
| 1109 | static inline int |
| 1110 | linux_check_direction(const pcap_t *handle, const struct sockaddr_ll *sll) |
| 1111 | { |
| 1112 | struct pcap_linux *handlep = handle->priv; |
| 1113 | |
| 1114 | if (sll->sll_pkttype == PACKET_OUTGOING) { |
| 1115 | /* |
| 1116 | * Outgoing packet. |
| 1117 | * If this is from the loopback device, reject it; |
| 1118 | * we'll see the packet as an incoming packet as well, |
| 1119 | * and we don't want to see it twice. |
| 1120 | */ |
| 1121 | if (sll->sll_ifindex == handlep->lo_ifindex) |
| 1122 | return 0; |
| 1123 | |
| 1124 | /* |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 1125 | * If this is an outgoing CAN or CAN FD frame, and |
| 1126 | * the user doesn't only want outgoing packets, |
| 1127 | * reject it; CAN devices and drivers, and the CAN |
| 1128 | * stack, always arrange to loop back transmitted |
| 1129 | * packets, so they also appear as incoming packets. |
| 1130 | * We don't want duplicate packets, and we can't |
| 1131 | * easily distinguish packets looped back by the CAN |
| 1132 | * layer than those received by the CAN layer, so we |
| 1133 | * eliminate this packet instead. |
| 1134 | */ |
| 1135 | if ((sll->sll_protocol == LINUX_SLL_P_CAN || |
| 1136 | sll->sll_protocol == LINUX_SLL_P_CANFD) && |
| 1137 | handle->direction != PCAP_D_OUT) |
| 1138 | return 0; |
| 1139 | |
| 1140 | /* |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 1141 | * If the user only wants incoming packets, reject it. |
| 1142 | */ |
| 1143 | if (handle->direction == PCAP_D_IN) |
| 1144 | return 0; |
| 1145 | } else { |
| 1146 | /* |
| 1147 | * Incoming packet. |
| 1148 | * If the user only wants outgoing packets, reject it. |
| 1149 | */ |
| 1150 | if (handle->direction == PCAP_D_OUT) |
| 1151 | return 0; |
| 1152 | } |
| 1153 | return 1; |
| 1154 | } |
| 1155 | |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1156 | /* |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 1157 | * Check whether the device to which the pcap_t is bound still exists. |
| 1158 | * We do so by asking what address the socket is bound to, and checking |
| 1159 | * whether the ifindex in the address is -1, meaning "that device is gone", |
| 1160 | * or some other value, meaning "that device still exists". |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1161 | */ |
| 1162 | static int |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 1163 | device_still_exists(pcap_t *handle) |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1164 | { |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 1165 | struct pcap_linux *handlep = handle->priv; |
| 1166 | struct sockaddr_ll addr; |
| 1167 | socklen_t addr_len; |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1168 | |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 1169 | /* |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 1170 | * If handlep->ifindex is -1, the socket isn't bound, meaning |
| 1171 | * we're capturing on the "any" device; that device never |
| 1172 | * disappears. (It should also never be configured down, so |
| 1173 | * we shouldn't even get here, but let's make sure.) |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 1174 | */ |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 1175 | if (handlep->ifindex == -1) |
| 1176 | return (1); /* it's still here */ |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 1177 | |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 1178 | /* |
| 1179 | * OK, now try to get the address for the socket. |
| 1180 | */ |
| 1181 | addr_len = sizeof (addr); |
| 1182 | if (getsockname(handle->fd, (struct sockaddr *) &addr, &addr_len) == -1) { |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1183 | /* |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 1184 | * Error - report an error and return -1. |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1185 | */ |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 1186 | pcap_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE, |
| 1187 | errno, "getsockname failed"); |
| 1188 | return (-1); |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1189 | } |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 1190 | if (addr.sll_ifindex == -1) { |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1191 | /* |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 1192 | * This means the device went away. |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1193 | */ |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 1194 | return (0); |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1195 | } |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 1196 | |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 1197 | /* |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 1198 | * The device presumably just went down. |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 1199 | */ |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 1200 | return (1); |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1201 | } |
| 1202 | |
| 1203 | static int |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 1204 | pcap_inject_linux(pcap_t *handle, const void *buf, int size) |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1205 | { |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 1206 | struct pcap_linux *handlep = handle->priv; |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1207 | int ret; |
| 1208 | |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 1209 | if (handlep->ifindex == -1) { |
| 1210 | /* |
| 1211 | * We don't support sending on the "any" device. |
| 1212 | */ |
| 1213 | pcap_strlcpy(handle->errbuf, |
| 1214 | "Sending packets isn't supported on the \"any\" device", |
| 1215 | PCAP_ERRBUF_SIZE); |
| 1216 | return (-1); |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1217 | } |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1218 | |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 1219 | if (handlep->cooked) { |
| 1220 | /* |
| 1221 | * We don't support sending on cooked-mode sockets. |
| 1222 | * |
| 1223 | * XXX - how do you send on a bound cooked-mode |
| 1224 | * socket? |
| 1225 | * Is a "sendto()" required there? |
| 1226 | */ |
| 1227 | pcap_strlcpy(handle->errbuf, |
| 1228 | "Sending packets isn't supported in cooked mode", |
| 1229 | PCAP_ERRBUF_SIZE); |
| 1230 | return (-1); |
| 1231 | } |
| 1232 | |
| 1233 | ret = (int)send(handle->fd, buf, size, 0); |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1234 | if (ret == -1) { |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 1235 | pcap_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE, |
| 1236 | errno, "send"); |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1237 | return (-1); |
| 1238 | } |
| 1239 | return (ret); |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 1240 | } |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1241 | |
| 1242 | /* |
| 1243 | * Get the statistics for the given packet capture handle. |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1244 | */ |
| 1245 | static int |
| 1246 | pcap_stats_linux(pcap_t *handle, struct pcap_stat *stats) |
| 1247 | { |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 1248 | struct pcap_linux *handlep = handle->priv; |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 1249 | #ifdef HAVE_TPACKET3 |
| 1250 | /* |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 1251 | * For sockets using TPACKET_V2, the extra stuff at the end |
| 1252 | * of a struct tpacket_stats_v3 will not be filled in, and |
| 1253 | * we don't look at it so this is OK even for those sockets. |
| 1254 | * In addition, the PF_PACKET socket code in the kernel only |
| 1255 | * uses the length parameter to compute how much data to |
| 1256 | * copy out and to indicate how much data was copied out, so |
| 1257 | * it's OK to base it on the size of a struct tpacket_stats. |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 1258 | * |
| 1259 | * XXX - it's probably OK, in fact, to just use a |
| 1260 | * struct tpacket_stats for V3 sockets, as we don't |
| 1261 | * care about the tp_freeze_q_cnt stat. |
| 1262 | */ |
| 1263 | struct tpacket_stats_v3 kstats; |
| 1264 | #else /* HAVE_TPACKET3 */ |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1265 | struct tpacket_stats kstats; |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 1266 | #endif /* HAVE_TPACKET3 */ |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1267 | socklen_t len = sizeof (struct tpacket_stats); |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 1268 | |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 1269 | long long if_dropped = 0; |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 1270 | |
| 1271 | /* |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 1272 | * To fill in ps_ifdrop, we parse |
| 1273 | * /sys/class/net/{if_name}/statistics/rx_{missed,fifo}_errors |
| 1274 | * for the numbers |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 1275 | */ |
| 1276 | if (handle->opt.promisc) |
| 1277 | { |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 1278 | /* |
| 1279 | * XXX - is there any reason to do this by remembering |
| 1280 | * the last counts value, subtracting it from the |
| 1281 | * current counts value, and adding that to stat.ps_ifdrop, |
| 1282 | * maintaining stat.ps_ifdrop as a count, rather than just |
| 1283 | * saving the *initial* counts value and setting |
| 1284 | * stat.ps_ifdrop to the difference between the current |
| 1285 | * value and the initial value? |
| 1286 | * |
| 1287 | * One reason might be to handle the count wrapping |
| 1288 | * around, on platforms where the count is 32 bits |
| 1289 | * and where you might get more than 2^32 dropped |
| 1290 | * packets; is there any other reason? |
| 1291 | * |
| 1292 | * (We maintain the count as a long long int so that, |
| 1293 | * if the kernel maintains the counts as 64-bit even |
| 1294 | * on 32-bit platforms, we can handle the real count. |
| 1295 | * |
| 1296 | * Unfortunately, we can't report 64-bit counts; we |
| 1297 | * need a better API for reporting statistics, such as |
| 1298 | * one that reports them in a style similar to the |
| 1299 | * pcapng Interface Statistics Block, so that 1) the |
| 1300 | * counts are 64-bit, 2) it's easier to add new statistics |
| 1301 | * without breaking the ABI, and 3) it's easier to |
| 1302 | * indicate to a caller that wants one particular |
| 1303 | * statistic that it's not available by just not supplying |
| 1304 | * it.) |
| 1305 | */ |
| 1306 | if_dropped = handlep->sysfs_dropped; |
| 1307 | handlep->sysfs_dropped = linux_if_drops(handlep->device); |
| 1308 | handlep->stat.ps_ifdrop += (u_int)(handlep->sysfs_dropped - if_dropped); |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 1309 | } |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1310 | |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1311 | /* |
| 1312 | * Try to get the packet counts from the kernel. |
| 1313 | */ |
| 1314 | if (getsockopt(handle->fd, SOL_PACKET, PACKET_STATISTICS, |
| 1315 | &kstats, &len) > -1) { |
| 1316 | /* |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 1317 | * "ps_recv" counts only packets that *passed* the |
| 1318 | * filter, not packets that didn't pass the filter. |
| 1319 | * This includes packets later dropped because we |
| 1320 | * ran out of buffer space. |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1321 | * |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 1322 | * "ps_drop" counts packets dropped because we ran |
| 1323 | * out of buffer space. It doesn't count packets |
| 1324 | * dropped by the interface driver. It counts only |
| 1325 | * packets that passed the filter. |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1326 | * |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 1327 | * See above for ps_ifdrop. |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1328 | * |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 1329 | * Both statistics include packets not yet read from |
| 1330 | * the kernel by libpcap, and thus not yet seen by |
| 1331 | * the application. |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 1332 | * |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 1333 | * In "linux/net/packet/af_packet.c", at least in 2.6.27 |
| 1334 | * through 5.6 kernels, "tp_packets" is incremented for |
| 1335 | * every packet that passes the packet filter *and* is |
| 1336 | * successfully copied to the ring buffer; "tp_drops" is |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1337 | * incremented for every packet dropped because there's |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 1338 | * not enough free space in the ring buffer. |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1339 | * |
| 1340 | * When the statistics are returned for a PACKET_STATISTICS |
| 1341 | * "getsockopt()" call, "tp_drops" is added to "tp_packets", |
| 1342 | * so that "tp_packets" counts all packets handed to |
| 1343 | * the PF_PACKET socket, including packets dropped because |
| 1344 | * there wasn't room on the socket buffer - but not |
| 1345 | * including packets that didn't pass the filter. |
| 1346 | * |
| 1347 | * In the BSD BPF, the count of received packets is |
| 1348 | * incremented for every packet handed to BPF, regardless |
| 1349 | * of whether it passed the filter. |
| 1350 | * |
| 1351 | * We can't make "pcap_stats()" work the same on both |
| 1352 | * platforms, but the best approximation is to return |
| 1353 | * "tp_packets" as the count of packets and "tp_drops" |
| 1354 | * as the count of drops. |
| 1355 | * |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 1356 | * Keep a running total because each call to |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1357 | * getsockopt(handle->fd, SOL_PACKET, PACKET_STATISTICS, .... |
| 1358 | * resets the counters to zero. |
| 1359 | */ |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 1360 | handlep->stat.ps_recv += kstats.tp_packets; |
| 1361 | handlep->stat.ps_drop += kstats.tp_drops; |
| 1362 | *stats = handlep->stat; |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1363 | return 0; |
| 1364 | } |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 1365 | |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 1366 | pcap_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE, errno, |
| 1367 | "failed to get statistics from socket"); |
| 1368 | return -1; |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 1369 | } |
| 1370 | |
| 1371 | /* |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1372 | * Description string for the "any" device. |
| 1373 | */ |
| 1374 | static const char any_descr[] = "Pseudo-device that captures on all interfaces"; |
| 1375 | |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 1376 | /* |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 1377 | * A PF_PACKET socket can be bound to any network interface. |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 1378 | */ |
| 1379 | static int |
| 1380 | can_be_bound(const char *name _U_) |
| 1381 | { |
| 1382 | return (1); |
| 1383 | } |
| 1384 | |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 1385 | /* |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 1386 | * Get a socket to use with various interface ioctls. |
| 1387 | */ |
| 1388 | static int |
| 1389 | get_if_ioctl_socket(void) |
| 1390 | { |
| 1391 | int fd; |
| 1392 | |
| 1393 | /* |
| 1394 | * This is a bit ugly. |
| 1395 | * |
| 1396 | * There isn't a socket type that's guaranteed to work. |
| 1397 | * |
| 1398 | * AF_NETLINK will work *if* you have Netlink configured into the |
| 1399 | * kernel (can it be configured out if you have any networking |
| 1400 | * support at all?) *and* if you're running a sufficiently recent |
| 1401 | * kernel, but not all the kernels we support are sufficiently |
| 1402 | * recent - that feature was introduced in Linux 4.6. |
| 1403 | * |
| 1404 | * AF_UNIX will work *if* you have UNIX-domain sockets configured |
| 1405 | * into the kernel and *if* you're not on a system that doesn't |
| 1406 | * allow them - some SELinux systems don't allow you create them. |
| 1407 | * Most systems probably have them configured in, but not all systems |
| 1408 | * have them configured in and allow them to be created. |
| 1409 | * |
| 1410 | * AF_INET will work *if* you have IPv4 configured into the kernel, |
| 1411 | * but, apparently, some systems have network adapters but have |
| 1412 | * kernels without IPv4 support. |
| 1413 | * |
| 1414 | * AF_INET6 will work *if* you have IPv6 configured into the |
| 1415 | * kernel, but if you don't have AF_INET, you might not have |
| 1416 | * AF_INET6, either (that is, independently on its own grounds). |
| 1417 | * |
| 1418 | * AF_PACKET would work, except that some of these calls should |
| 1419 | * work even if you *don't* have capture permission (you should be |
| 1420 | * able to enumerate interfaces and get information about them |
| 1421 | * without capture permission; you shouldn't get a failure until |
| 1422 | * you try pcap_activate()). (If you don't allow programs to |
| 1423 | * get as much information as possible about interfaces if you |
| 1424 | * don't have permission to capture, you run the risk of users |
| 1425 | * asking "why isn't it showing XXX" - or, worse, if you don't |
| 1426 | * show interfaces *at all* if you don't have permission to |
| 1427 | * capture on them, "why do no interfaces show up?" - when the |
| 1428 | * real problem is a permissions problem. Error reports of that |
| 1429 | * type require a lot more back-and-forth to debug, as evidenced |
| 1430 | * by many Wireshark bugs/mailing list questions/Q&A questoins.) |
| 1431 | * |
| 1432 | * So: |
| 1433 | * |
| 1434 | * we first try an AF_NETLINK socket, where "try" includes |
| 1435 | * "try to do a device ioctl on it", as, in the future, once |
| 1436 | * pre-4.6 kernels are sufficiently rare, that will probably |
| 1437 | * be the mechanism most likely to work; |
| 1438 | * |
| 1439 | * if that fails, we try an AF_UNIX socket, as that's less |
| 1440 | * likely to be configured out on a networking-capable system |
| 1441 | * than is IP; |
| 1442 | * |
| 1443 | * if that fails, we try an AF_INET6 socket; |
| 1444 | * |
| 1445 | * if that fails, we try an AF_INET socket. |
| 1446 | */ |
| 1447 | fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_GENERIC); |
| 1448 | if (fd != -1) { |
| 1449 | /* |
| 1450 | * OK, let's make sure we can do an SIOCGIFNAME |
| 1451 | * ioctl. |
| 1452 | */ |
| 1453 | struct ifreq ifr; |
| 1454 | |
| 1455 | memset(&ifr, 0, sizeof(ifr)); |
| 1456 | if (ioctl(fd, SIOCGIFNAME, &ifr) == 0 || |
| 1457 | errno != EOPNOTSUPP) { |
| 1458 | /* |
| 1459 | * It succeeded, or failed for some reason |
| 1460 | * other than "netlink sockets don't support |
| 1461 | * device ioctls". Go with the AF_NETLINK |
| 1462 | * socket. |
| 1463 | */ |
| 1464 | return (fd); |
| 1465 | } |
| 1466 | |
| 1467 | /* |
| 1468 | * OK, that didn't work, so it's as bad as "netlink |
| 1469 | * sockets aren't available". Close the socket and |
| 1470 | * drive on. |
| 1471 | */ |
| 1472 | close(fd); |
| 1473 | } |
| 1474 | |
| 1475 | /* |
| 1476 | * Now try an AF_UNIX socket. |
| 1477 | */ |
| 1478 | fd = socket(AF_UNIX, SOCK_RAW, 0); |
| 1479 | if (fd != -1) { |
| 1480 | /* |
| 1481 | * OK, we got it! |
| 1482 | */ |
| 1483 | return (fd); |
| 1484 | } |
| 1485 | |
| 1486 | /* |
| 1487 | * Now try an AF_INET6 socket. |
| 1488 | */ |
| 1489 | fd = socket(AF_INET6, SOCK_DGRAM, 0); |
| 1490 | if (fd != -1) { |
| 1491 | return (fd); |
| 1492 | } |
| 1493 | |
| 1494 | /* |
| 1495 | * Now try an AF_INET socket. |
| 1496 | * |
| 1497 | * XXX - if that fails, is there anything else we should try? |
| 1498 | * AF_CAN, for embedded systems in vehicles, in case they're |
| 1499 | * built without Internet protocol support? Any other socket |
| 1500 | * types popular in non-Internet embedded systems? |
| 1501 | */ |
| 1502 | return (socket(AF_INET, SOCK_DGRAM, 0)); |
| 1503 | } |
| 1504 | |
| 1505 | /* |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 1506 | * Get additional flags for a device, using SIOCGIFMEDIA. |
| 1507 | */ |
| 1508 | static int |
| 1509 | get_if_flags(const char *name, bpf_u_int32 *flags, char *errbuf) |
| 1510 | { |
| 1511 | int sock; |
| 1512 | FILE *fh; |
| 1513 | unsigned int arptype; |
| 1514 | struct ifreq ifr; |
| 1515 | struct ethtool_value info; |
| 1516 | |
| 1517 | if (*flags & PCAP_IF_LOOPBACK) { |
| 1518 | /* |
| 1519 | * Loopback devices aren't wireless, and "connected"/ |
| 1520 | * "disconnected" doesn't apply to them. |
| 1521 | */ |
| 1522 | *flags |= PCAP_IF_CONNECTION_STATUS_NOT_APPLICABLE; |
| 1523 | return 0; |
| 1524 | } |
| 1525 | |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 1526 | sock = get_if_ioctl_socket(); |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 1527 | if (sock == -1) { |
| 1528 | pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE, errno, |
| 1529 | "Can't create socket to get ethtool information for %s", |
| 1530 | name); |
| 1531 | return -1; |
| 1532 | } |
| 1533 | |
| 1534 | /* |
| 1535 | * OK, what type of network is this? |
| 1536 | * In particular, is it wired or wireless? |
| 1537 | */ |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 1538 | if (is_wifi(name)) { |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 1539 | /* |
| 1540 | * Wi-Fi, hence wireless. |
| 1541 | */ |
| 1542 | *flags |= PCAP_IF_WIRELESS; |
| 1543 | } else { |
| 1544 | /* |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 1545 | * OK, what does /sys/class/net/{if_name}/type contain? |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 1546 | * (We don't use that for Wi-Fi, as it'll report |
| 1547 | * "Ethernet", i.e. ARPHRD_ETHER, for non-monitor- |
| 1548 | * mode devices.) |
| 1549 | */ |
| 1550 | char *pathstr; |
| 1551 | |
| 1552 | if (asprintf(&pathstr, "/sys/class/net/%s/type", name) == -1) { |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 1553 | snprintf(errbuf, PCAP_ERRBUF_SIZE, |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 1554 | "%s: Can't generate path name string for /sys/class/net device", |
| 1555 | name); |
| 1556 | close(sock); |
| 1557 | return -1; |
| 1558 | } |
| 1559 | fh = fopen(pathstr, "r"); |
| 1560 | if (fh != NULL) { |
| 1561 | if (fscanf(fh, "%u", &arptype) == 1) { |
| 1562 | /* |
| 1563 | * OK, we got an ARPHRD_ type; what is it? |
| 1564 | */ |
| 1565 | switch (arptype) { |
| 1566 | |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 1567 | case ARPHRD_LOOPBACK: |
| 1568 | /* |
| 1569 | * These are types to which |
| 1570 | * "connected" and "disconnected" |
| 1571 | * don't apply, so don't bother |
| 1572 | * asking about it. |
| 1573 | * |
| 1574 | * XXX - add other types? |
| 1575 | */ |
| 1576 | close(sock); |
| 1577 | fclose(fh); |
| 1578 | free(pathstr); |
| 1579 | return 0; |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 1580 | |
| 1581 | case ARPHRD_IRDA: |
| 1582 | case ARPHRD_IEEE80211: |
| 1583 | case ARPHRD_IEEE80211_PRISM: |
| 1584 | case ARPHRD_IEEE80211_RADIOTAP: |
| 1585 | #ifdef ARPHRD_IEEE802154 |
| 1586 | case ARPHRD_IEEE802154: |
| 1587 | #endif |
| 1588 | #ifdef ARPHRD_IEEE802154_MONITOR |
| 1589 | case ARPHRD_IEEE802154_MONITOR: |
| 1590 | #endif |
| 1591 | #ifdef ARPHRD_6LOWPAN |
| 1592 | case ARPHRD_6LOWPAN: |
| 1593 | #endif |
| 1594 | /* |
| 1595 | * Various wireless types. |
| 1596 | */ |
| 1597 | *flags |= PCAP_IF_WIRELESS; |
| 1598 | break; |
| 1599 | } |
| 1600 | } |
| 1601 | fclose(fh); |
| 1602 | free(pathstr); |
| 1603 | } |
| 1604 | } |
| 1605 | |
| 1606 | #ifdef ETHTOOL_GLINK |
| 1607 | memset(&ifr, 0, sizeof(ifr)); |
Haibo Huang | 4ccd683 | 2020-04-23 18:03:48 -0700 | [diff] [blame] | 1608 | pcap_strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name)); |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 1609 | info.cmd = ETHTOOL_GLINK; |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 1610 | /* |
| 1611 | * XXX - while Valgrind handles SIOCETHTOOL and knows that |
| 1612 | * the ETHTOOL_GLINK command sets the .data member of the |
| 1613 | * structure, Memory Sanitizer doesn't yet do so: |
| 1614 | * |
| 1615 | * https://bugs.llvm.org/show_bug.cgi?id=45814 |
| 1616 | * |
| 1617 | * For now, we zero it out to squelch warnings; if the bug |
| 1618 | * in question is fixed, we can remove this. |
| 1619 | */ |
| 1620 | info.data = 0; |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 1621 | ifr.ifr_data = (caddr_t)&info; |
| 1622 | if (ioctl(sock, SIOCETHTOOL, &ifr) == -1) { |
| 1623 | int save_errno = errno; |
| 1624 | |
| 1625 | switch (save_errno) { |
| 1626 | |
| 1627 | case EOPNOTSUPP: |
| 1628 | case EINVAL: |
| 1629 | /* |
| 1630 | * OK, this OS version or driver doesn't support |
| 1631 | * asking for this information. |
| 1632 | * XXX - distinguish between "this doesn't |
| 1633 | * support ethtool at all because it's not |
| 1634 | * that type of device" vs. "this doesn't |
| 1635 | * support ethtool even though it's that |
| 1636 | * type of device", and return "unknown". |
| 1637 | */ |
| 1638 | *flags |= PCAP_IF_CONNECTION_STATUS_NOT_APPLICABLE; |
| 1639 | close(sock); |
| 1640 | return 0; |
| 1641 | |
| 1642 | case ENODEV: |
| 1643 | /* |
| 1644 | * OK, no such device. |
| 1645 | * The user will find that out when they try to |
| 1646 | * activate the device; just say "OK" and |
| 1647 | * don't set anything. |
| 1648 | */ |
| 1649 | close(sock); |
| 1650 | return 0; |
| 1651 | |
| 1652 | default: |
| 1653 | /* |
| 1654 | * Other error. |
| 1655 | */ |
| 1656 | pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE, |
| 1657 | save_errno, |
| 1658 | "%s: SIOCETHTOOL(ETHTOOL_GLINK) ioctl failed", |
| 1659 | name); |
| 1660 | close(sock); |
| 1661 | return -1; |
| 1662 | } |
| 1663 | } |
| 1664 | |
| 1665 | /* |
| 1666 | * Is it connected? |
| 1667 | */ |
| 1668 | if (info.data) { |
| 1669 | /* |
| 1670 | * It's connected. |
| 1671 | */ |
| 1672 | *flags |= PCAP_IF_CONNECTION_STATUS_CONNECTED; |
| 1673 | } else { |
| 1674 | /* |
| 1675 | * It's disconnected. |
| 1676 | */ |
| 1677 | *flags |= PCAP_IF_CONNECTION_STATUS_DISCONNECTED; |
| 1678 | } |
| 1679 | #endif |
| 1680 | |
| 1681 | close(sock); |
| 1682 | return 0; |
| 1683 | } |
| 1684 | |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1685 | int |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 1686 | pcap_platform_finddevs(pcap_if_list_t *devlistp, char *errbuf) |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1687 | { |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 1688 | /* |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 1689 | * Get the list of regular interfaces first. |
| 1690 | */ |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 1691 | if (pcap_findalldevs_interfaces(devlistp, errbuf, can_be_bound, |
| 1692 | get_if_flags) == -1) |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 1693 | return (-1); /* failure */ |
| 1694 | |
| 1695 | /* |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 1696 | * Add the "any" device. |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 1697 | * As it refers to all network devices, not to any particular |
| 1698 | * network device, the notion of "connected" vs. "disconnected" |
| 1699 | * doesn't apply. |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 1700 | */ |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 1701 | if (add_dev(devlistp, "any", |
| 1702 | PCAP_IF_UP|PCAP_IF_RUNNING|PCAP_IF_CONNECTION_STATUS_NOT_APPLICABLE, |
| 1703 | any_descr, errbuf) == NULL) |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1704 | return (-1); |
| 1705 | |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1706 | return (0); |
| 1707 | } |
| 1708 | |
| 1709 | /* |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1710 | * Set direction flag: Which packets do we accept on a forwarding |
| 1711 | * single device? IN, OUT or both? |
| 1712 | */ |
| 1713 | static int |
| 1714 | pcap_setdirection_linux(pcap_t *handle, pcap_direction_t d) |
| 1715 | { |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1716 | /* |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 1717 | * It's guaranteed, at this point, that d is a valid |
| 1718 | * direction value. |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1719 | */ |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 1720 | handle->direction = d; |
| 1721 | return 0; |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1722 | } |
| 1723 | |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 1724 | static int |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 1725 | is_wifi(const char *device) |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 1726 | { |
| 1727 | char *pathstr; |
| 1728 | struct stat statb; |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 1729 | |
| 1730 | /* |
| 1731 | * See if there's a sysfs wireless directory for it. |
| 1732 | * If so, it's a wireless interface. |
| 1733 | */ |
| 1734 | if (asprintf(&pathstr, "/sys/class/net/%s/wireless", device) == -1) { |
| 1735 | /* |
| 1736 | * Just give up here. |
| 1737 | */ |
| 1738 | return 0; |
| 1739 | } |
| 1740 | if (stat(pathstr, &statb) == 0) { |
| 1741 | free(pathstr); |
| 1742 | return 1; |
| 1743 | } |
| 1744 | free(pathstr); |
| 1745 | |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 1746 | return 0; |
| 1747 | } |
| 1748 | |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1749 | /* |
| 1750 | * Linux uses the ARP hardware type to identify the type of an |
| 1751 | * interface. pcap uses the DLT_xxx constants for this. This |
| 1752 | * function takes a pointer to a "pcap_t", and an ARPHRD_xxx |
| 1753 | * constant, as arguments, and sets "handle->linktype" to the |
| 1754 | * appropriate DLT_XXX constant and sets "handle->offset" to |
| 1755 | * the appropriate value (to make "handle->offset" plus link-layer |
| 1756 | * header length be a multiple of 4, so that the link-layer payload |
| 1757 | * will be aligned on a 4-byte boundary when capturing packets). |
| 1758 | * (If the offset isn't set here, it'll be 0; add code as appropriate |
| 1759 | * for cases where it shouldn't be 0.) |
| 1760 | * |
| 1761 | * If "cooked_ok" is non-zero, we can use DLT_LINUX_SLL and capture |
| 1762 | * in cooked mode; otherwise, we can't use cooked mode, so we have |
| 1763 | * to pick some type that works in raw mode, or fail. |
| 1764 | * |
| 1765 | * Sets the link type to -1 if unable to map the type. |
| 1766 | */ |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 1767 | static void map_arphrd_to_dlt(pcap_t *handle, int arptype, |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 1768 | const char *device, int cooked_ok) |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1769 | { |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 1770 | static const char cdma_rmnet[] = "cdma_rmnet"; |
| 1771 | |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1772 | switch (arptype) { |
| 1773 | |
| 1774 | case ARPHRD_ETHER: |
| 1775 | /* |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 1776 | * For various annoying reasons having to do with DHCP |
| 1777 | * software, some versions of Android give the mobile- |
| 1778 | * phone-network interface an ARPHRD_ value of |
| 1779 | * ARPHRD_ETHER, even though the packets supplied by |
| 1780 | * that interface have no link-layer header, and begin |
| 1781 | * with an IP header, so that the ARPHRD_ value should |
| 1782 | * be ARPHRD_NONE. |
| 1783 | * |
| 1784 | * Detect those devices by checking the device name, and |
| 1785 | * use DLT_RAW for them. |
| 1786 | */ |
| 1787 | if (strncmp(device, cdma_rmnet, sizeof cdma_rmnet - 1) == 0) { |
| 1788 | handle->linktype = DLT_RAW; |
| 1789 | return; |
| 1790 | } |
| 1791 | |
| 1792 | /* |
| 1793 | * Is this a real Ethernet device? If so, give it a |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1794 | * link-layer-type list with DLT_EN10MB and DLT_DOCSIS, so |
| 1795 | * that an application can let you choose it, in case you're |
| 1796 | * capturing DOCSIS traffic that a Cisco Cable Modem |
| 1797 | * Termination System is putting out onto an Ethernet (it |
| 1798 | * doesn't put an Ethernet header onto the wire, it puts raw |
| 1799 | * DOCSIS frames out on the wire inside the low-level |
| 1800 | * Ethernet framing). |
| 1801 | * |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 1802 | * XXX - are there any other sorts of "fake Ethernet" that |
| 1803 | * have ARPHRD_ETHER but that shouldn't offer DLT_DOCSIS as |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1804 | * a Cisco CMTS won't put traffic onto it or get traffic |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 1805 | * bridged onto it? ISDN is handled in "activate_pf_packet()", |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 1806 | * as we fall back on cooked mode there, and we use |
| 1807 | * is_wifi() to check for 802.11 devices; are there any |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1808 | * others? |
| 1809 | */ |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 1810 | if (!is_wifi(device)) { |
| 1811 | int ret; |
| 1812 | |
| 1813 | /* |
| 1814 | * This is not a Wi-Fi device but it could be |
| 1815 | * a DSA master/management network device. |
| 1816 | */ |
| 1817 | ret = iface_dsa_get_proto_info(device, handle); |
| 1818 | if (ret < 0) |
| 1819 | return; |
| 1820 | |
| 1821 | if (ret == 1) { |
| 1822 | /* |
| 1823 | * This is a DSA master/management network |
| 1824 | * device linktype is already set by |
| 1825 | * iface_dsa_get_proto_info() set an |
| 1826 | * appropriate offset here. |
| 1827 | */ |
| 1828 | handle->offset = 2; |
| 1829 | break; |
| 1830 | } |
| 1831 | |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 1832 | /* |
| 1833 | * It's not a Wi-Fi device; offer DOCSIS. |
| 1834 | */ |
| 1835 | handle->dlt_list = (u_int *) malloc(sizeof(u_int) * 2); |
| 1836 | /* |
| 1837 | * If that fails, just leave the list empty. |
| 1838 | */ |
| 1839 | if (handle->dlt_list != NULL) { |
| 1840 | handle->dlt_list[0] = DLT_EN10MB; |
| 1841 | handle->dlt_list[1] = DLT_DOCSIS; |
| 1842 | handle->dlt_count = 2; |
| 1843 | } |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1844 | } |
| 1845 | /* FALLTHROUGH */ |
| 1846 | |
| 1847 | case ARPHRD_METRICOM: |
| 1848 | case ARPHRD_LOOPBACK: |
| 1849 | handle->linktype = DLT_EN10MB; |
| 1850 | handle->offset = 2; |
| 1851 | break; |
| 1852 | |
| 1853 | case ARPHRD_EETHER: |
| 1854 | handle->linktype = DLT_EN3MB; |
| 1855 | break; |
| 1856 | |
| 1857 | case ARPHRD_AX25: |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 1858 | handle->linktype = DLT_AX25_KISS; |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1859 | break; |
| 1860 | |
| 1861 | case ARPHRD_PRONET: |
| 1862 | handle->linktype = DLT_PRONET; |
| 1863 | break; |
| 1864 | |
| 1865 | case ARPHRD_CHAOS: |
| 1866 | handle->linktype = DLT_CHAOS; |
| 1867 | break; |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 1868 | #ifndef ARPHRD_CAN |
| 1869 | #define ARPHRD_CAN 280 |
| 1870 | #endif |
| 1871 | case ARPHRD_CAN: |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 1872 | /* |
| 1873 | * Map this to DLT_LINUX_SLL; that way, CAN frames will |
| 1874 | * have ETH_P_CAN/LINUX_SLL_P_CAN as the protocol and |
| 1875 | * CAN FD frames will have ETH_P_CANFD/LINUX_SLL_P_CANFD |
| 1876 | * as the protocol, so they can be distinguished by the |
| 1877 | * protocol in the SLL header. |
| 1878 | */ |
| 1879 | handle->linktype = DLT_LINUX_SLL; |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 1880 | break; |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1881 | |
| 1882 | #ifndef ARPHRD_IEEE802_TR |
| 1883 | #define ARPHRD_IEEE802_TR 800 /* From Linux 2.4 */ |
| 1884 | #endif |
| 1885 | case ARPHRD_IEEE802_TR: |
| 1886 | case ARPHRD_IEEE802: |
| 1887 | handle->linktype = DLT_IEEE802; |
| 1888 | handle->offset = 2; |
| 1889 | break; |
| 1890 | |
| 1891 | case ARPHRD_ARCNET: |
| 1892 | handle->linktype = DLT_ARCNET_LINUX; |
| 1893 | break; |
| 1894 | |
| 1895 | #ifndef ARPHRD_FDDI /* From Linux 2.2.13 */ |
| 1896 | #define ARPHRD_FDDI 774 |
| 1897 | #endif |
| 1898 | case ARPHRD_FDDI: |
| 1899 | handle->linktype = DLT_FDDI; |
| 1900 | handle->offset = 3; |
| 1901 | break; |
| 1902 | |
| 1903 | #ifndef ARPHRD_ATM /* FIXME: How to #include this? */ |
| 1904 | #define ARPHRD_ATM 19 |
| 1905 | #endif |
| 1906 | case ARPHRD_ATM: |
| 1907 | /* |
| 1908 | * The Classical IP implementation in ATM for Linux |
| 1909 | * supports both what RFC 1483 calls "LLC Encapsulation", |
| 1910 | * in which each packet has an LLC header, possibly |
| 1911 | * with a SNAP header as well, prepended to it, and |
| 1912 | * what RFC 1483 calls "VC Based Multiplexing", in which |
| 1913 | * different virtual circuits carry different network |
| 1914 | * layer protocols, and no header is prepended to packets. |
| 1915 | * |
| 1916 | * They both have an ARPHRD_ type of ARPHRD_ATM, so |
| 1917 | * you can't use the ARPHRD_ type to find out whether |
| 1918 | * captured packets will have an LLC header, and, |
| 1919 | * while there's a socket ioctl to *set* the encapsulation |
| 1920 | * type, there's no ioctl to *get* the encapsulation type. |
| 1921 | * |
| 1922 | * This means that |
| 1923 | * |
| 1924 | * programs that dissect Linux Classical IP frames |
| 1925 | * would have to check for an LLC header and, |
| 1926 | * depending on whether they see one or not, dissect |
| 1927 | * the frame as LLC-encapsulated or as raw IP (I |
| 1928 | * don't know whether there's any traffic other than |
| 1929 | * IP that would show up on the socket, or whether |
| 1930 | * there's any support for IPv6 in the Linux |
| 1931 | * Classical IP code); |
| 1932 | * |
| 1933 | * filter expressions would have to compile into |
| 1934 | * code that checks for an LLC header and does |
| 1935 | * the right thing. |
| 1936 | * |
| 1937 | * Both of those are a nuisance - and, at least on systems |
| 1938 | * that support PF_PACKET sockets, we don't have to put |
| 1939 | * up with those nuisances; instead, we can just capture |
| 1940 | * in cooked mode. That's what we'll do, if we can. |
| 1941 | * Otherwise, we'll just fail. |
| 1942 | */ |
| 1943 | if (cooked_ok) |
| 1944 | handle->linktype = DLT_LINUX_SLL; |
| 1945 | else |
| 1946 | handle->linktype = -1; |
| 1947 | break; |
| 1948 | |
| 1949 | #ifndef ARPHRD_IEEE80211 /* From Linux 2.4.6 */ |
| 1950 | #define ARPHRD_IEEE80211 801 |
| 1951 | #endif |
| 1952 | case ARPHRD_IEEE80211: |
| 1953 | handle->linktype = DLT_IEEE802_11; |
| 1954 | break; |
| 1955 | |
| 1956 | #ifndef ARPHRD_IEEE80211_PRISM /* From Linux 2.4.18 */ |
| 1957 | #define ARPHRD_IEEE80211_PRISM 802 |
| 1958 | #endif |
| 1959 | case ARPHRD_IEEE80211_PRISM: |
| 1960 | handle->linktype = DLT_PRISM_HEADER; |
| 1961 | break; |
| 1962 | |
| 1963 | #ifndef ARPHRD_IEEE80211_RADIOTAP /* new */ |
| 1964 | #define ARPHRD_IEEE80211_RADIOTAP 803 |
| 1965 | #endif |
| 1966 | case ARPHRD_IEEE80211_RADIOTAP: |
| 1967 | handle->linktype = DLT_IEEE802_11_RADIO; |
| 1968 | break; |
| 1969 | |
| 1970 | case ARPHRD_PPP: |
| 1971 | /* |
| 1972 | * Some PPP code in the kernel supplies no link-layer |
| 1973 | * header whatsoever to PF_PACKET sockets; other PPP |
| 1974 | * code supplies PPP link-layer headers ("syncppp.c"); |
| 1975 | * some PPP code might supply random link-layer |
| 1976 | * headers (PPP over ISDN - there's code in Ethereal, |
| 1977 | * for example, to cope with PPP-over-ISDN captures |
| 1978 | * with which the Ethereal developers have had to cope, |
| 1979 | * heuristically trying to determine which of the |
| 1980 | * oddball link-layer headers particular packets have). |
| 1981 | * |
| 1982 | * As such, we just punt, and run all PPP interfaces |
| 1983 | * in cooked mode, if we can; otherwise, we just treat |
| 1984 | * it as DLT_RAW, for now - if somebody needs to capture, |
| 1985 | * on a 2.0[.x] kernel, on PPP devices that supply a |
| 1986 | * link-layer header, they'll have to add code here to |
| 1987 | * map to the appropriate DLT_ type (possibly adding a |
| 1988 | * new DLT_ type, if necessary). |
| 1989 | */ |
| 1990 | if (cooked_ok) |
| 1991 | handle->linktype = DLT_LINUX_SLL; |
| 1992 | else { |
| 1993 | /* |
| 1994 | * XXX - handle ISDN types here? We can't fall |
| 1995 | * back on cooked sockets, so we'd have to |
| 1996 | * figure out from the device name what type of |
| 1997 | * link-layer encapsulation it's using, and map |
| 1998 | * that to an appropriate DLT_ value, meaning |
| 1999 | * we'd map "isdnN" devices to DLT_RAW (they |
| 2000 | * supply raw IP packets with no link-layer |
| 2001 | * header) and "isdY" devices to a new DLT_I4L_IP |
| 2002 | * type that has only an Ethernet packet type as |
| 2003 | * a link-layer header. |
| 2004 | * |
| 2005 | * But sometimes we seem to get random crap |
| 2006 | * in the link-layer header when capturing on |
| 2007 | * ISDN devices.... |
| 2008 | */ |
| 2009 | handle->linktype = DLT_RAW; |
| 2010 | } |
| 2011 | break; |
| 2012 | |
| 2013 | #ifndef ARPHRD_CISCO |
| 2014 | #define ARPHRD_CISCO 513 /* previously ARPHRD_HDLC */ |
| 2015 | #endif |
| 2016 | case ARPHRD_CISCO: |
| 2017 | handle->linktype = DLT_C_HDLC; |
| 2018 | break; |
| 2019 | |
| 2020 | /* Not sure if this is correct for all tunnels, but it |
| 2021 | * works for CIPE */ |
| 2022 | case ARPHRD_TUNNEL: |
| 2023 | #ifndef ARPHRD_SIT |
| 2024 | #define ARPHRD_SIT 776 /* From Linux 2.2.13 */ |
| 2025 | #endif |
| 2026 | case ARPHRD_SIT: |
| 2027 | case ARPHRD_CSLIP: |
| 2028 | case ARPHRD_SLIP6: |
| 2029 | case ARPHRD_CSLIP6: |
| 2030 | case ARPHRD_ADAPT: |
| 2031 | case ARPHRD_SLIP: |
| 2032 | #ifndef ARPHRD_RAWHDLC |
| 2033 | #define ARPHRD_RAWHDLC 518 |
| 2034 | #endif |
| 2035 | case ARPHRD_RAWHDLC: |
| 2036 | #ifndef ARPHRD_DLCI |
| 2037 | #define ARPHRD_DLCI 15 |
| 2038 | #endif |
| 2039 | case ARPHRD_DLCI: |
| 2040 | /* |
| 2041 | * XXX - should some of those be mapped to DLT_LINUX_SLL |
| 2042 | * instead? Should we just map all of them to DLT_LINUX_SLL? |
| 2043 | */ |
| 2044 | handle->linktype = DLT_RAW; |
| 2045 | break; |
| 2046 | |
| 2047 | #ifndef ARPHRD_FRAD |
| 2048 | #define ARPHRD_FRAD 770 |
| 2049 | #endif |
| 2050 | case ARPHRD_FRAD: |
| 2051 | handle->linktype = DLT_FRELAY; |
| 2052 | break; |
| 2053 | |
| 2054 | case ARPHRD_LOCALTLK: |
| 2055 | handle->linktype = DLT_LTALK; |
| 2056 | break; |
| 2057 | |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 2058 | case 18: |
| 2059 | /* |
| 2060 | * RFC 4338 defines an encapsulation for IP and ARP |
| 2061 | * packets that's compatible with the RFC 2625 |
| 2062 | * encapsulation, but that uses a different ARP |
| 2063 | * hardware type and hardware addresses. That |
| 2064 | * ARP hardware type is 18; Linux doesn't define |
| 2065 | * any ARPHRD_ value as 18, but if it ever officially |
| 2066 | * supports RFC 4338-style IP-over-FC, it should define |
| 2067 | * one. |
| 2068 | * |
| 2069 | * For now, we map it to DLT_IP_OVER_FC, in the hopes |
| 2070 | * that this will encourage its use in the future, |
| 2071 | * should Linux ever officially support RFC 4338-style |
| 2072 | * IP-over-FC. |
| 2073 | */ |
| 2074 | handle->linktype = DLT_IP_OVER_FC; |
| 2075 | break; |
| 2076 | |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2077 | #ifndef ARPHRD_FCPP |
| 2078 | #define ARPHRD_FCPP 784 |
| 2079 | #endif |
| 2080 | case ARPHRD_FCPP: |
| 2081 | #ifndef ARPHRD_FCAL |
| 2082 | #define ARPHRD_FCAL 785 |
| 2083 | #endif |
| 2084 | case ARPHRD_FCAL: |
| 2085 | #ifndef ARPHRD_FCPL |
| 2086 | #define ARPHRD_FCPL 786 |
| 2087 | #endif |
| 2088 | case ARPHRD_FCPL: |
| 2089 | #ifndef ARPHRD_FCFABRIC |
| 2090 | #define ARPHRD_FCFABRIC 787 |
| 2091 | #endif |
| 2092 | case ARPHRD_FCFABRIC: |
| 2093 | /* |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 2094 | * Back in 2002, Donald Lee at Cray wanted a DLT_ for |
| 2095 | * IP-over-FC: |
| 2096 | * |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 2097 | * https://www.mail-archive.com/tcpdump-workers@sandelman.ottawa.on.ca/msg01043.html |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 2098 | * |
| 2099 | * and one was assigned. |
| 2100 | * |
| 2101 | * In a later private discussion (spun off from a message |
| 2102 | * on the ethereal-users list) on how to get that DLT_ |
| 2103 | * value in libpcap on Linux, I ended up deciding that |
| 2104 | * the best thing to do would be to have him tweak the |
| 2105 | * driver to set the ARPHRD_ value to some ARPHRD_FCxx |
| 2106 | * type, and map all those types to DLT_IP_OVER_FC: |
| 2107 | * |
| 2108 | * I've checked into the libpcap and tcpdump CVS tree |
| 2109 | * support for DLT_IP_OVER_FC. In order to use that, |
| 2110 | * you'd have to modify your modified driver to return |
| 2111 | * one of the ARPHRD_FCxxx types, in "fcLINUXfcp.c" - |
| 2112 | * change it to set "dev->type" to ARPHRD_FCFABRIC, for |
| 2113 | * example (the exact value doesn't matter, it can be |
| 2114 | * any of ARPHRD_FCPP, ARPHRD_FCAL, ARPHRD_FCPL, or |
| 2115 | * ARPHRD_FCFABRIC). |
| 2116 | * |
| 2117 | * 11 years later, Christian Svensson wanted to map |
| 2118 | * various ARPHRD_ values to DLT_FC_2 and |
| 2119 | * DLT_FC_2_WITH_FRAME_DELIMS for raw Fibre Channel |
| 2120 | * frames: |
| 2121 | * |
| 2122 | * https://github.com/mcr/libpcap/pull/29 |
| 2123 | * |
| 2124 | * There doesn't seem to be any network drivers that uses |
| 2125 | * any of the ARPHRD_FC* values for IP-over-FC, and |
| 2126 | * it's not exactly clear what the "Dummy types for non |
| 2127 | * ARP hardware" are supposed to mean (link-layer |
| 2128 | * header type? Physical network type?), so it's |
| 2129 | * not exactly clear why the ARPHRD_FC* types exist |
| 2130 | * in the first place. |
| 2131 | * |
| 2132 | * For now, we map them to DLT_FC_2, and provide an |
| 2133 | * option of DLT_FC_2_WITH_FRAME_DELIMS, as well as |
| 2134 | * DLT_IP_OVER_FC just in case there's some old |
| 2135 | * driver out there that uses one of those types for |
| 2136 | * IP-over-FC on which somebody wants to capture |
| 2137 | * packets. |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2138 | */ |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 2139 | handle->dlt_list = (u_int *) malloc(sizeof(u_int) * 3); |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 2140 | /* |
| 2141 | * If that fails, just leave the list empty. |
| 2142 | */ |
| 2143 | if (handle->dlt_list != NULL) { |
| 2144 | handle->dlt_list[0] = DLT_FC_2; |
| 2145 | handle->dlt_list[1] = DLT_FC_2_WITH_FRAME_DELIMS; |
| 2146 | handle->dlt_list[2] = DLT_IP_OVER_FC; |
| 2147 | handle->dlt_count = 3; |
| 2148 | } |
| 2149 | handle->linktype = DLT_FC_2; |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2150 | break; |
| 2151 | |
| 2152 | #ifndef ARPHRD_IRDA |
| 2153 | #define ARPHRD_IRDA 783 |
| 2154 | #endif |
| 2155 | case ARPHRD_IRDA: |
| 2156 | /* Don't expect IP packet out of this interfaces... */ |
| 2157 | handle->linktype = DLT_LINUX_IRDA; |
| 2158 | /* We need to save packet direction for IrDA decoding, |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 2159 | * so let's use "Linux-cooked" mode. Jean II |
| 2160 | * |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 2161 | * XXX - this is handled in activate_pf_packet(). */ |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 2162 | /* handlep->cooked = 1; */ |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2163 | break; |
| 2164 | |
| 2165 | /* ARPHRD_LAPD is unofficial and randomly allocated, if reallocation |
| 2166 | * is needed, please report it to <daniele@orlandi.com> */ |
| 2167 | #ifndef ARPHRD_LAPD |
| 2168 | #define ARPHRD_LAPD 8445 |
| 2169 | #endif |
| 2170 | case ARPHRD_LAPD: |
| 2171 | /* Don't expect IP packet out of this interfaces... */ |
| 2172 | handle->linktype = DLT_LINUX_LAPD; |
| 2173 | break; |
| 2174 | |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 2175 | #ifndef ARPHRD_NONE |
| 2176 | #define ARPHRD_NONE 0xFFFE |
| 2177 | #endif |
| 2178 | case ARPHRD_NONE: |
| 2179 | /* |
| 2180 | * No link-layer header; packets are just IP |
| 2181 | * packets, so use DLT_RAW. |
| 2182 | */ |
| 2183 | handle->linktype = DLT_RAW; |
| 2184 | break; |
| 2185 | |
| 2186 | #ifndef ARPHRD_IEEE802154 |
| 2187 | #define ARPHRD_IEEE802154 804 |
| 2188 | #endif |
| 2189 | case ARPHRD_IEEE802154: |
| 2190 | handle->linktype = DLT_IEEE802_15_4_NOFCS; |
| 2191 | break; |
| 2192 | |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 2193 | #ifndef ARPHRD_NETLINK |
| 2194 | #define ARPHRD_NETLINK 824 |
| 2195 | #endif |
| 2196 | case ARPHRD_NETLINK: |
| 2197 | handle->linktype = DLT_NETLINK; |
| 2198 | /* |
| 2199 | * We need to use cooked mode, so that in sll_protocol we |
| 2200 | * pick up the netlink protocol type such as NETLINK_ROUTE, |
| 2201 | * NETLINK_GENERIC, NETLINK_FIB_LOOKUP, etc. |
| 2202 | * |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 2203 | * XXX - this is handled in activate_pf_packet(). |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 2204 | */ |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 2205 | /* handlep->cooked = 1; */ |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 2206 | break; |
| 2207 | |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 2208 | #ifndef ARPHRD_VSOCKMON |
| 2209 | #define ARPHRD_VSOCKMON 826 |
| 2210 | #endif |
| 2211 | case ARPHRD_VSOCKMON: |
| 2212 | handle->linktype = DLT_VSOCK; |
| 2213 | break; |
| 2214 | |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2215 | default: |
| 2216 | handle->linktype = -1; |
| 2217 | break; |
| 2218 | } |
| 2219 | } |
| 2220 | |
Haibo Huang | 4ccd683 | 2020-04-23 18:03:48 -0700 | [diff] [blame] | 2221 | #ifdef PACKET_RESERVE |
| 2222 | static void |
| 2223 | set_dlt_list_cooked(pcap_t *handle, int sock_fd) |
| 2224 | { |
| 2225 | socklen_t len; |
| 2226 | unsigned int tp_reserve; |
| 2227 | |
| 2228 | /* |
| 2229 | * If we can't do PACKET_RESERVE, we can't reserve extra space |
| 2230 | * for a DLL_LINUX_SLL2 header, so we can't support DLT_LINUX_SLL2. |
| 2231 | */ |
| 2232 | len = sizeof(tp_reserve); |
| 2233 | if (getsockopt(sock_fd, SOL_PACKET, PACKET_RESERVE, &tp_reserve, |
| 2234 | &len) == 0) { |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 2235 | /* |
| 2236 | * Yes, we can do DLL_LINUX_SLL2. |
| 2237 | */ |
Haibo Huang | 4ccd683 | 2020-04-23 18:03:48 -0700 | [diff] [blame] | 2238 | handle->dlt_list = (u_int *) malloc(sizeof(u_int) * 2); |
| 2239 | /* |
| 2240 | * If that fails, just leave the list empty. |
| 2241 | */ |
| 2242 | if (handle->dlt_list != NULL) { |
| 2243 | handle->dlt_list[0] = DLT_LINUX_SLL; |
| 2244 | handle->dlt_list[1] = DLT_LINUX_SLL2; |
| 2245 | handle->dlt_count = 2; |
| 2246 | } |
| 2247 | } |
| 2248 | } |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 2249 | #else/* PACKET_RESERVE */ |
Haibo Huang | 4ccd683 | 2020-04-23 18:03:48 -0700 | [diff] [blame] | 2250 | /* |
| 2251 | * The build environment doesn't define PACKET_RESERVE, so we can't reserve |
| 2252 | * extra space for a DLL_LINUX_SLL2 header, so we can't support DLT_LINUX_SLL2. |
| 2253 | */ |
| 2254 | static void |
| 2255 | set_dlt_list_cooked(pcap_t *handle _U_, int sock_fd _U_) |
| 2256 | { |
| 2257 | } |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 2258 | #endif /* PACKET_RESERVE */ |
Haibo Huang | 4ccd683 | 2020-04-23 18:03:48 -0700 | [diff] [blame] | 2259 | |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2260 | /* |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 2261 | * Try to set up a PF_PACKET socket. |
| 2262 | * Returns 0 on success and a PCAP_ERROR_ value on failure. |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2263 | */ |
| 2264 | static int |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 2265 | activate_pf_packet(pcap_t *handle, int is_any_device) |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2266 | { |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 2267 | struct pcap_linux *handlep = handle->priv; |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 2268 | const char *device = handle->opt.device; |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 2269 | int status = 0; |
| 2270 | int sock_fd, arptype; |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 2271 | #ifdef HAVE_PACKET_AUXDATA |
| 2272 | int val; |
| 2273 | #endif |
| 2274 | int err = 0; |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2275 | struct packet_mreq mr; |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 2276 | #if defined(SO_BPF_EXTENSIONS) && defined(SKF_AD_VLAN_TAG_PRESENT) |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 2277 | int bpf_extensions; |
| 2278 | socklen_t len = sizeof(bpf_extensions); |
| 2279 | #endif |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2280 | |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 2281 | /* |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 2282 | * Open a socket with protocol family packet. If cooked is true, |
| 2283 | * we open a SOCK_DGRAM socket for the cooked interface, otherwise |
| 2284 | * we open a SOCK_RAW socket for the raw interface. |
| 2285 | * |
| 2286 | * The protocol is set to 0. This means we will receive no |
| 2287 | * packets until we "bind" the socket with a non-zero |
| 2288 | * protocol. This allows us to setup the ring buffers without |
| 2289 | * dropping any packets. |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 2290 | */ |
| 2291 | sock_fd = is_any_device ? |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 2292 | socket(PF_PACKET, SOCK_DGRAM, 0) : |
| 2293 | socket(PF_PACKET, SOCK_RAW, 0); |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2294 | |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 2295 | if (sock_fd == -1) { |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 2296 | if (errno == EPERM || errno == EACCES) { |
| 2297 | /* |
| 2298 | * You don't have permission to open the |
| 2299 | * socket. |
| 2300 | */ |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 2301 | status = PCAP_ERROR_PERM_DENIED; |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2302 | } else { |
| 2303 | /* |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 2304 | * Other error. |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2305 | */ |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 2306 | status = PCAP_ERROR; |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 2307 | } |
Haibo Huang | 4ccd683 | 2020-04-23 18:03:48 -0700 | [diff] [blame] | 2308 | pcap_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE, |
| 2309 | errno, "socket"); |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 2310 | return status; |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 2311 | } |
| 2312 | |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 2313 | /* |
| 2314 | * Get the interface index of the loopback device. |
| 2315 | * If the attempt fails, don't fail, just set the |
| 2316 | * "handlep->lo_ifindex" to -1. |
| 2317 | * |
| 2318 | * XXX - can there be more than one device that loops |
| 2319 | * packets back, i.e. devices other than "lo"? If so, |
| 2320 | * we'd need to find them all, and have an array of |
| 2321 | * indices for them, and check all of them in |
| 2322 | * "pcap_read_packet()". |
| 2323 | */ |
| 2324 | handlep->lo_ifindex = iface_get_id(sock_fd, "lo", handle->errbuf); |
| 2325 | |
| 2326 | /* |
| 2327 | * Default value for offset to align link-layer payload |
| 2328 | * on a 4-byte boundary. |
| 2329 | */ |
| 2330 | handle->offset = 0; |
| 2331 | |
| 2332 | /* |
| 2333 | * What kind of frames do we have to deal with? Fall back |
| 2334 | * to cooked mode if we have an unknown interface type |
| 2335 | * or a type we know doesn't work well in raw mode. |
| 2336 | */ |
| 2337 | if (!is_any_device) { |
| 2338 | /* Assume for now we don't need cooked mode. */ |
| 2339 | handlep->cooked = 0; |
| 2340 | |
| 2341 | if (handle->opt.rfmon) { |
| 2342 | /* |
| 2343 | * We were asked to turn on monitor mode. |
| 2344 | * Do so before we get the link-layer type, |
| 2345 | * because entering monitor mode could change |
| 2346 | * the link-layer type. |
| 2347 | */ |
| 2348 | err = enter_rfmon_mode(handle, sock_fd, device); |
| 2349 | if (err < 0) { |
| 2350 | /* Hard failure */ |
| 2351 | close(sock_fd); |
| 2352 | return err; |
| 2353 | } |
| 2354 | if (err == 0) { |
| 2355 | /* |
| 2356 | * Nothing worked for turning monitor mode |
| 2357 | * on. |
| 2358 | */ |
| 2359 | close(sock_fd); |
| 2360 | return PCAP_ERROR_RFMON_NOTSUP; |
| 2361 | } |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2362 | |
| 2363 | /* |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 2364 | * Either monitor mode has been turned on for |
| 2365 | * the device, or we've been given a different |
| 2366 | * device to open for monitor mode. If we've |
| 2367 | * been given a different device, use it. |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2368 | */ |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 2369 | if (handlep->mondevice != NULL) |
| 2370 | device = handlep->mondevice; |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2371 | } |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 2372 | arptype = iface_get_arptype(sock_fd, device, handle->errbuf); |
| 2373 | if (arptype < 0) { |
| 2374 | close(sock_fd); |
| 2375 | return arptype; |
| 2376 | } |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 2377 | map_arphrd_to_dlt(handle, arptype, device, 1); |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 2378 | if (handle->linktype == -1 || |
| 2379 | handle->linktype == DLT_LINUX_SLL || |
| 2380 | handle->linktype == DLT_LINUX_IRDA || |
| 2381 | handle->linktype == DLT_LINUX_LAPD || |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 2382 | handle->linktype == DLT_NETLINK || |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 2383 | (handle->linktype == DLT_EN10MB && |
| 2384 | (strncmp("isdn", device, 4) == 0 || |
| 2385 | strncmp("isdY", device, 4) == 0))) { |
| 2386 | /* |
| 2387 | * Unknown interface type (-1), or a |
| 2388 | * device we explicitly chose to run |
| 2389 | * in cooked mode (e.g., PPP devices), |
| 2390 | * or an ISDN device (whose link-layer |
| 2391 | * type we can only determine by using |
| 2392 | * APIs that may be different on different |
| 2393 | * kernels) - reopen in cooked mode. |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 2394 | * |
| 2395 | * If the type is unknown, return a warning; |
| 2396 | * map_arphrd_to_dlt() has already set the |
| 2397 | * warning message. |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 2398 | */ |
| 2399 | if (close(sock_fd) == -1) { |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 2400 | pcap_fmt_errmsg_for_errno(handle->errbuf, |
| 2401 | PCAP_ERRBUF_SIZE, errno, "close"); |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 2402 | return PCAP_ERROR; |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2403 | } |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 2404 | sock_fd = socket(PF_PACKET, SOCK_DGRAM, 0); |
| 2405 | if (sock_fd < 0) { |
| 2406 | /* |
| 2407 | * Fatal error. We treat this as |
| 2408 | * a generic error; we already know |
| 2409 | * that we were able to open a |
| 2410 | * PF_PACKET/SOCK_RAW socket, so |
| 2411 | * any failure is a "this shouldn't |
| 2412 | * happen" case. |
| 2413 | */ |
Haibo Huang | 4ccd683 | 2020-04-23 18:03:48 -0700 | [diff] [blame] | 2414 | pcap_fmt_errmsg_for_errno(handle->errbuf, |
| 2415 | PCAP_ERRBUF_SIZE, errno, "socket"); |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 2416 | return PCAP_ERROR; |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 2417 | } |
| 2418 | handlep->cooked = 1; |
| 2419 | |
| 2420 | /* |
| 2421 | * Get rid of any link-layer type list |
| 2422 | * we allocated - this only supports cooked |
| 2423 | * capture. |
| 2424 | */ |
| 2425 | if (handle->dlt_list != NULL) { |
| 2426 | free(handle->dlt_list); |
| 2427 | handle->dlt_list = NULL; |
| 2428 | handle->dlt_count = 0; |
Haibo Huang | 4ccd683 | 2020-04-23 18:03:48 -0700 | [diff] [blame] | 2429 | set_dlt_list_cooked(handle, sock_fd); |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 2430 | } |
| 2431 | |
| 2432 | if (handle->linktype == -1) { |
| 2433 | /* |
| 2434 | * Warn that we're falling back on |
| 2435 | * cooked mode; we may want to |
| 2436 | * update "map_arphrd_to_dlt()" |
| 2437 | * to handle the new type. |
| 2438 | */ |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 2439 | snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 2440 | "arptype %d not " |
| 2441 | "supported by libpcap - " |
| 2442 | "falling back to cooked " |
| 2443 | "socket", |
| 2444 | arptype); |
| 2445 | } |
| 2446 | |
| 2447 | /* |
| 2448 | * IrDA capture is not a real "cooked" capture, |
| 2449 | * it's IrLAP frames, not IP packets. The |
| 2450 | * same applies to LAPD capture. |
| 2451 | */ |
| 2452 | if (handle->linktype != DLT_LINUX_IRDA && |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 2453 | handle->linktype != DLT_LINUX_LAPD && |
| 2454 | handle->linktype != DLT_NETLINK) |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 2455 | handle->linktype = DLT_LINUX_SLL; |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 2456 | if (handle->linktype == -1) { |
| 2457 | snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, |
| 2458 | "unknown arptype %d, defaulting to cooked mode", |
| 2459 | arptype); |
| 2460 | status = PCAP_WARNING; |
| 2461 | } |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2462 | } |
| 2463 | |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 2464 | handlep->ifindex = iface_get_id(sock_fd, device, |
| 2465 | handle->errbuf); |
| 2466 | if (handlep->ifindex == -1) { |
| 2467 | close(sock_fd); |
| 2468 | return PCAP_ERROR; |
| 2469 | } |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2470 | |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 2471 | if ((err = iface_bind(sock_fd, handlep->ifindex, |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 2472 | handle->errbuf, 0)) != 0) { |
| 2473 | close(sock_fd); |
| 2474 | return err; |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 2475 | } |
| 2476 | } else { |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2477 | /* |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 2478 | * The "any" device. |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2479 | */ |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 2480 | if (handle->opt.rfmon) { |
| 2481 | /* |
| 2482 | * It doesn't support monitor mode. |
| 2483 | */ |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 2484 | close(sock_fd); |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 2485 | return PCAP_ERROR_RFMON_NOTSUP; |
| 2486 | } |
| 2487 | |
| 2488 | /* |
| 2489 | * It uses cooked mode. |
| 2490 | */ |
| 2491 | handlep->cooked = 1; |
| 2492 | handle->linktype = DLT_LINUX_SLL; |
Haibo Huang | 4ccd683 | 2020-04-23 18:03:48 -0700 | [diff] [blame] | 2493 | handle->dlt_list = NULL; |
| 2494 | handle->dlt_count = 0; |
| 2495 | set_dlt_list_cooked(handle, sock_fd); |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 2496 | |
| 2497 | /* |
| 2498 | * We're not bound to a device. |
| 2499 | * For now, we're using this as an indication |
| 2500 | * that we can't transmit; stop doing that only |
| 2501 | * if we figure out how to transmit in cooked |
| 2502 | * mode. |
| 2503 | */ |
| 2504 | handlep->ifindex = -1; |
| 2505 | } |
| 2506 | |
| 2507 | /* |
| 2508 | * Select promiscuous mode on if "promisc" is set. |
| 2509 | * |
| 2510 | * Do not turn allmulti mode on if we don't select |
| 2511 | * promiscuous mode - on some devices (e.g., Orinoco |
| 2512 | * wireless interfaces), allmulti mode isn't supported |
| 2513 | * and the driver implements it by turning promiscuous |
| 2514 | * mode on, and that screws up the operation of the |
| 2515 | * card as a normal networking interface, and on no |
| 2516 | * other platform I know of does starting a non- |
| 2517 | * promiscuous capture affect which multicast packets |
| 2518 | * are received by the interface. |
| 2519 | */ |
| 2520 | |
| 2521 | /* |
| 2522 | * Hmm, how can we set promiscuous mode on all interfaces? |
| 2523 | * I am not sure if that is possible at all. For now, we |
| 2524 | * silently ignore attempts to turn promiscuous mode on |
| 2525 | * for the "any" device (so you don't have to explicitly |
| 2526 | * disable it in programs such as tcpdump). |
| 2527 | */ |
| 2528 | |
| 2529 | if (!is_any_device && handle->opt.promisc) { |
| 2530 | memset(&mr, 0, sizeof(mr)); |
| 2531 | mr.mr_ifindex = handlep->ifindex; |
| 2532 | mr.mr_type = PACKET_MR_PROMISC; |
| 2533 | if (setsockopt(sock_fd, SOL_PACKET, PACKET_ADD_MEMBERSHIP, |
| 2534 | &mr, sizeof(mr)) == -1) { |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 2535 | pcap_fmt_errmsg_for_errno(handle->errbuf, |
Haibo Huang | 4ccd683 | 2020-04-23 18:03:48 -0700 | [diff] [blame] | 2536 | PCAP_ERRBUF_SIZE, errno, "setsockopt (PACKET_ADD_MEMBERSHIP)"); |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 2537 | close(sock_fd); |
| 2538 | return PCAP_ERROR; |
| 2539 | } |
| 2540 | } |
| 2541 | |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 2542 | /* Enable auxiliary data if supported and reserve room for |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 2543 | * reconstructing VLAN headers. */ |
| 2544 | #ifdef HAVE_PACKET_AUXDATA |
| 2545 | val = 1; |
| 2546 | if (setsockopt(sock_fd, SOL_PACKET, PACKET_AUXDATA, &val, |
| 2547 | sizeof(val)) == -1 && errno != ENOPROTOOPT) { |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 2548 | pcap_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE, |
Haibo Huang | 4ccd683 | 2020-04-23 18:03:48 -0700 | [diff] [blame] | 2549 | errno, "setsockopt (PACKET_AUXDATA)"); |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 2550 | close(sock_fd); |
| 2551 | return PCAP_ERROR; |
| 2552 | } |
| 2553 | handle->offset += VLAN_TAG_LEN; |
| 2554 | #endif /* HAVE_PACKET_AUXDATA */ |
| 2555 | |
| 2556 | /* |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 2557 | * If we're in cooked mode, make the snapshot length |
| 2558 | * large enough to hold a "cooked mode" header plus |
| 2559 | * 1 byte of packet data (so we don't pass a byte |
| 2560 | * count of 0 to "recvfrom()"). |
Haibo Huang | 4ccd683 | 2020-04-23 18:03:48 -0700 | [diff] [blame] | 2561 | * XXX - we don't know whether this will be DLT_LINUX_SLL |
| 2562 | * or DLT_LINUX_SLL2, so make sure it's big enough for |
| 2563 | * a DLT_LINUX_SLL2 "cooked mode" header; a snapshot length |
| 2564 | * that small is silly anyway. |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 2565 | */ |
| 2566 | if (handlep->cooked) { |
Haibo Huang | 4ccd683 | 2020-04-23 18:03:48 -0700 | [diff] [blame] | 2567 | if (handle->snapshot < SLL2_HDR_LEN + 1) |
| 2568 | handle->snapshot = SLL2_HDR_LEN + 1; |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 2569 | } |
| 2570 | handle->bufsize = handle->snapshot; |
| 2571 | |
| 2572 | /* |
| 2573 | * Set the offset at which to insert VLAN tags. |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 2574 | * That should be the offset of the type field. |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 2575 | */ |
| 2576 | switch (handle->linktype) { |
| 2577 | |
| 2578 | case DLT_EN10MB: |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 2579 | /* |
| 2580 | * The type field is after the destination and source |
| 2581 | * MAC address. |
| 2582 | */ |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 2583 | handlep->vlan_offset = 2 * ETH_ALEN; |
| 2584 | break; |
| 2585 | |
| 2586 | case DLT_LINUX_SLL: |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 2587 | /* |
| 2588 | * The type field is in the last 2 bytes of the |
| 2589 | * DLT_LINUX_SLL header. |
| 2590 | */ |
| 2591 | handlep->vlan_offset = SLL_HDR_LEN - 2; |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 2592 | break; |
| 2593 | |
| 2594 | default: |
| 2595 | handlep->vlan_offset = -1; /* unknown */ |
| 2596 | break; |
| 2597 | } |
| 2598 | |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 2599 | if (handle->opt.tstamp_precision == PCAP_TSTAMP_PRECISION_NANO) { |
| 2600 | int nsec_tstamps = 1; |
| 2601 | |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 2602 | if (setsockopt(sock_fd, SOL_SOCKET, SO_TIMESTAMPNS, &nsec_tstamps, sizeof(nsec_tstamps)) < 0) { |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 2603 | snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "setsockopt: unable to set SO_TIMESTAMPNS"); |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 2604 | close(sock_fd); |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 2605 | return PCAP_ERROR; |
| 2606 | } |
| 2607 | } |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 2608 | |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 2609 | /* |
| 2610 | * We've succeeded. Save the socket FD in the pcap structure. |
| 2611 | */ |
| 2612 | handle->fd = sock_fd; |
| 2613 | |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 2614 | #if defined(SO_BPF_EXTENSIONS) && defined(SKF_AD_VLAN_TAG_PRESENT) |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 2615 | /* |
| 2616 | * Can we generate special code for VLAN checks? |
| 2617 | * (XXX - what if we need the special code but it's not supported |
| 2618 | * by the OS? Is that possible?) |
| 2619 | */ |
| 2620 | if (getsockopt(sock_fd, SOL_SOCKET, SO_BPF_EXTENSIONS, |
| 2621 | &bpf_extensions, &len) == 0) { |
| 2622 | if (bpf_extensions >= SKF_AD_VLAN_TAG_PRESENT) { |
| 2623 | /* |
| 2624 | * Yes, we can. Request that we do so. |
| 2625 | */ |
| 2626 | handle->bpf_codegen_flags |= BPF_SPECIAL_VLAN_HANDLING; |
| 2627 | } |
| 2628 | } |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 2629 | #endif /* defined(SO_BPF_EXTENSIONS) && defined(SKF_AD_VLAN_TAG_PRESENT) */ |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 2630 | |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 2631 | return status; |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2632 | } |
| 2633 | |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 2634 | /* |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 2635 | * Attempt to setup memory-mapped access. |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 2636 | * |
| 2637 | * On success, returns 1, and sets *status to 0 if there are no warnings |
| 2638 | * or to a PCAP_WARNING_ code if there is a warning. |
| 2639 | * |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 2640 | * On error, returns -1, and sets *status to the appropriate error code; |
| 2641 | * if that is PCAP_ERROR, sets handle->errbuf to the appropriate message. |
| 2642 | */ |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 2643 | static int |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 2644 | setup_mmapped(pcap_t *handle, int *status) |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 2645 | { |
| 2646 | struct pcap_linux *handlep = handle->priv; |
| 2647 | int ret; |
| 2648 | |
| 2649 | /* |
| 2650 | * Attempt to allocate a buffer to hold the contents of one |
| 2651 | * packet, for use by the oneshot callback. |
| 2652 | */ |
| 2653 | handlep->oneshot_buffer = malloc(handle->snapshot); |
| 2654 | if (handlep->oneshot_buffer == NULL) { |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 2655 | pcap_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE, |
| 2656 | errno, "can't allocate oneshot buffer"); |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 2657 | *status = PCAP_ERROR; |
| 2658 | return -1; |
| 2659 | } |
| 2660 | |
| 2661 | if (handle->opt.buffer_size == 0) { |
| 2662 | /* by default request 2M for the ring buffer */ |
| 2663 | handle->opt.buffer_size = 2*1024*1024; |
| 2664 | } |
| 2665 | ret = prepare_tpacket_socket(handle); |
| 2666 | if (ret == -1) { |
| 2667 | free(handlep->oneshot_buffer); |
| 2668 | *status = PCAP_ERROR; |
| 2669 | return ret; |
| 2670 | } |
| 2671 | ret = create_ring(handle, status); |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 2672 | if (ret == -1) { |
| 2673 | /* |
| 2674 | * Error attempting to enable memory-mapped capture; |
| 2675 | * fail. create_ring() has set *status. |
| 2676 | */ |
| 2677 | free(handlep->oneshot_buffer); |
| 2678 | return -1; |
| 2679 | } |
| 2680 | |
| 2681 | /* |
| 2682 | * Success. *status has been set either to 0 if there are no |
| 2683 | * warnings or to a PCAP_WARNING_ value if there is a warning. |
| 2684 | * |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 2685 | * handle->offset is used to get the current position into the rx ring. |
| 2686 | * handle->cc is used to store the ring size. |
| 2687 | */ |
| 2688 | |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 2689 | /* |
| 2690 | * Set the timeout to use in poll() before returning. |
| 2691 | */ |
| 2692 | set_poll_timeout(handlep); |
| 2693 | |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 2694 | return 1; |
| 2695 | } |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 2696 | |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 2697 | /* |
| 2698 | * Attempt to set the socket to the specified version of the memory-mapped |
| 2699 | * header. |
| 2700 | * |
| 2701 | * Return 0 if we succeed; return 1 if we fail because that version isn't |
| 2702 | * supported; return -1 on any other error, and set handle->errbuf. |
| 2703 | */ |
| 2704 | static int |
| 2705 | init_tpacket(pcap_t *handle, int version, const char *version_str) |
| 2706 | { |
| 2707 | struct pcap_linux *handlep = handle->priv; |
| 2708 | int val = version; |
| 2709 | socklen_t len = sizeof(val); |
| 2710 | |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 2711 | /* |
| 2712 | * Probe whether kernel supports the specified TPACKET version; |
| 2713 | * this also gets the length of the header for that version. |
Haibo Huang | 4ccd683 | 2020-04-23 18:03:48 -0700 | [diff] [blame] | 2714 | * |
| 2715 | * This socket option was introduced in 2.6.27, which was |
| 2716 | * also the first release with TPACKET_V2 support. |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 2717 | */ |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 2718 | if (getsockopt(handle->fd, SOL_PACKET, PACKET_HDRLEN, &val, &len) < 0) { |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 2719 | if (errno == EINVAL) { |
Haibo Huang | 4ccd683 | 2020-04-23 18:03:48 -0700 | [diff] [blame] | 2720 | /* |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 2721 | * EINVAL means this specific version of TPACKET |
| 2722 | * is not supported. Tell the caller they can try |
| 2723 | * with a different one; if they've run out of |
| 2724 | * others to try, let them set the error message |
| 2725 | * appropriately. |
Haibo Huang | 4ccd683 | 2020-04-23 18:03:48 -0700 | [diff] [blame] | 2726 | */ |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 2727 | return 1; |
Haibo Huang | 4ccd683 | 2020-04-23 18:03:48 -0700 | [diff] [blame] | 2728 | } |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 2729 | |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 2730 | /* |
| 2731 | * All other errors are fatal. |
| 2732 | */ |
| 2733 | if (errno == ENOPROTOOPT) { |
| 2734 | /* |
| 2735 | * PACKET_HDRLEN isn't supported, which means |
| 2736 | * that memory-mapped capture isn't supported. |
| 2737 | * Indicate that in the message. |
| 2738 | */ |
| 2739 | snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, |
| 2740 | "Kernel doesn't support memory-mapped capture; a 2.6.27 or later 2.x kernel is required, with CONFIG_PACKET_MMAP specified for 2.x kernels"); |
| 2741 | } else { |
| 2742 | /* |
| 2743 | * Some unexpected error. |
| 2744 | */ |
| 2745 | pcap_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE, |
| 2746 | errno, "can't get %s header len on packet socket", |
| 2747 | version_str); |
| 2748 | } |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 2749 | return -1; |
| 2750 | } |
| 2751 | handlep->tp_hdrlen = val; |
| 2752 | |
| 2753 | val = version; |
| 2754 | if (setsockopt(handle->fd, SOL_PACKET, PACKET_VERSION, &val, |
| 2755 | sizeof(val)) < 0) { |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 2756 | pcap_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE, |
| 2757 | errno, "can't activate %s on packet socket", version_str); |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 2758 | return -1; |
| 2759 | } |
| 2760 | handlep->tp_version = version; |
| 2761 | |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 2762 | return 0; |
| 2763 | } |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 2764 | |
| 2765 | /* |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 2766 | * Attempt to set the socket to version 3 of the memory-mapped header and, |
| 2767 | * if that fails because version 3 isn't supported, attempt to fall |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 2768 | * back to version 2. If version 2 isn't supported, just fail. |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 2769 | * |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 2770 | * Return 0 if we succeed and -1 on any other error, and set handle->errbuf. |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 2771 | */ |
| 2772 | static int |
| 2773 | prepare_tpacket_socket(pcap_t *handle) |
| 2774 | { |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 2775 | int ret; |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 2776 | |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 2777 | #ifdef HAVE_TPACKET3 |
| 2778 | /* |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 2779 | * Try setting the version to TPACKET_V3. |
| 2780 | * |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 2781 | * The only mode in which buffering is done on PF_PACKET |
| 2782 | * sockets, so that packets might not be delivered |
| 2783 | * immediately, is TPACKET_V3 mode. |
| 2784 | * |
| 2785 | * The buffering cannot be disabled in that mode, so |
| 2786 | * if the user has requested immediate mode, we don't |
| 2787 | * use TPACKET_V3. |
| 2788 | */ |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 2789 | if (!handle->opt.immediate) { |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 2790 | ret = init_tpacket(handle, TPACKET_V3, "TPACKET_V3"); |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 2791 | if (ret == 0) { |
| 2792 | /* |
| 2793 | * Success. |
| 2794 | */ |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 2795 | return 0; |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 2796 | } |
| 2797 | if (ret == -1) { |
| 2798 | /* |
| 2799 | * We failed for some reason other than "the |
| 2800 | * kernel doesn't support TPACKET_V3". |
| 2801 | */ |
| 2802 | return -1; |
| 2803 | } |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 2804 | |
| 2805 | /* |
| 2806 | * This means it returned 1, which means "the kernel |
| 2807 | * doesn't support TPACKET_V3"; try TPACKET_V2. |
| 2808 | */ |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 2809 | } |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 2810 | #endif /* HAVE_TPACKET3 */ |
| 2811 | |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 2812 | /* |
| 2813 | * Try setting the version to TPACKET_V2. |
| 2814 | */ |
| 2815 | ret = init_tpacket(handle, TPACKET_V2, "TPACKET_V2"); |
| 2816 | if (ret == 0) { |
| 2817 | /* |
| 2818 | * Success. |
| 2819 | */ |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 2820 | return 0; |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 2821 | } |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 2822 | |
| 2823 | if (ret == 1) { |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 2824 | /* |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 2825 | * OK, the kernel supports memory-mapped capture, but |
| 2826 | * not TPACKET_V2. Set the error message appropriately. |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 2827 | */ |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 2828 | snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, |
| 2829 | "Kernel doesn't support TPACKET_V2; a 2.6.27 or later kernel is required"); |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 2830 | } |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 2831 | |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 2832 | /* |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 2833 | * We failed. |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 2834 | */ |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 2835 | return -1; |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 2836 | } |
| 2837 | |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 2838 | #define MAX(a,b) ((a)>(b)?(a):(b)) |
| 2839 | |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 2840 | /* |
| 2841 | * Attempt to set up memory-mapped access. |
| 2842 | * |
| 2843 | * On success, returns 1, and sets *status to 0 if there are no warnings |
| 2844 | * or to a PCAP_WARNING_ code if there is a warning. |
| 2845 | * |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 2846 | * On error, returns -1, and sets *status to the appropriate error code; |
| 2847 | * if that is PCAP_ERROR, sets handle->errbuf to the appropriate message. |
| 2848 | */ |
| 2849 | static int |
| 2850 | create_ring(pcap_t *handle, int *status) |
| 2851 | { |
| 2852 | struct pcap_linux *handlep = handle->priv; |
| 2853 | unsigned i, j, frames_per_block; |
| 2854 | #ifdef HAVE_TPACKET3 |
| 2855 | /* |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 2856 | * For sockets using TPACKET_V2, the extra stuff at the end of a |
| 2857 | * struct tpacket_req3 will be ignored, so this is OK even for |
| 2858 | * those sockets. |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 2859 | */ |
| 2860 | struct tpacket_req3 req; |
| 2861 | #else |
| 2862 | struct tpacket_req req; |
| 2863 | #endif |
| 2864 | socklen_t len; |
| 2865 | unsigned int sk_type, tp_reserve, maclen, tp_hdrlen, netoff, macoff; |
| 2866 | unsigned int frame_size; |
| 2867 | |
| 2868 | /* |
| 2869 | * Start out assuming no warnings or errors. |
| 2870 | */ |
| 2871 | *status = 0; |
| 2872 | |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 2873 | /* |
| 2874 | * Reserve space for VLAN tag reconstruction. |
| 2875 | */ |
| 2876 | tp_reserve = VLAN_TAG_LEN; |
| 2877 | |
| 2878 | /* |
| 2879 | * If we're using DLT_LINUX_SLL2, reserve space for a |
| 2880 | * DLT_LINUX_SLL2 header. |
| 2881 | * |
| 2882 | * XXX - we assume that the kernel is still adding |
| 2883 | * 16 bytes of extra space; that happens to |
| 2884 | * correspond to SLL_HDR_LEN (whether intentionally |
| 2885 | * or not - the kernel code has a raw "16" in |
| 2886 | * the expression), so we subtract SLL_HDR_LEN |
| 2887 | * from SLL2_HDR_LEN to get the additional space |
| 2888 | * needed. That also means we don't bother reserving |
| 2889 | * any additional space if we're using DLT_LINUX_SLL. |
| 2890 | * |
| 2891 | * XXX - should we use TPACKET_ALIGN(SLL2_HDR_LEN - SLL_HDR_LEN)? |
| 2892 | */ |
| 2893 | if (handle->linktype == DLT_LINUX_SLL2) |
| 2894 | tp_reserve += SLL2_HDR_LEN - SLL_HDR_LEN; |
| 2895 | |
| 2896 | /* |
| 2897 | * Try to request that amount of reserve space. |
| 2898 | * This must be done before creating the ring buffer. |
| 2899 | * If PACKET_RESERVE is supported, creating the ring |
| 2900 | * buffer should be, although if creating the ring |
| 2901 | * buffer fails, the PACKET_RESERVE call has no effect, |
| 2902 | * so falling back on read-from-the-socket capturing |
| 2903 | * won't be affected. |
| 2904 | */ |
| 2905 | len = sizeof(tp_reserve); |
| 2906 | if (setsockopt(handle->fd, SOL_PACKET, PACKET_RESERVE, |
| 2907 | &tp_reserve, len) < 0) { |
| 2908 | /* |
| 2909 | * We treat ENOPROTOOPT as an error, as we |
| 2910 | * already determined that we support |
| 2911 | * TPACKET_V2 and later; see above. |
| 2912 | */ |
| 2913 | pcap_fmt_errmsg_for_errno(handle->errbuf, |
| 2914 | PCAP_ERRBUF_SIZE, errno, |
| 2915 | "setsockopt (PACKET_RESERVE)"); |
| 2916 | *status = PCAP_ERROR; |
| 2917 | return -1; |
| 2918 | } |
| 2919 | |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 2920 | switch (handlep->tp_version) { |
| 2921 | |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 2922 | case TPACKET_V2: |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 2923 | /* Note that with large snapshot length (say 256K, which is |
| 2924 | * the default for recent versions of tcpdump, Wireshark, |
| 2925 | * TShark, dumpcap or 64K, the value that "-s 0" has given for |
| 2926 | * a long time with tcpdump), if we use the snapshot |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 2927 | * length to calculate the frame length, only a few frames |
| 2928 | * will be available in the ring even with pretty |
| 2929 | * large ring size (and a lot of memory will be unused). |
| 2930 | * |
| 2931 | * Ideally, we should choose a frame length based on the |
| 2932 | * minimum of the specified snapshot length and the maximum |
| 2933 | * packet size. That's not as easy as it sounds; consider, |
| 2934 | * for example, an 802.11 interface in monitor mode, where |
| 2935 | * the frame would include a radiotap header, where the |
| 2936 | * maximum radiotap header length is device-dependent. |
| 2937 | * |
| 2938 | * So, for now, we just do this for Ethernet devices, where |
| 2939 | * there's no metadata header, and the link-layer header is |
| 2940 | * fixed length. We can get the maximum packet size by |
| 2941 | * adding 18, the Ethernet header length plus the CRC length |
| 2942 | * (just in case we happen to get the CRC in the packet), to |
| 2943 | * the MTU of the interface; we fetch the MTU in the hopes |
| 2944 | * that it reflects support for jumbo frames. (Even if the |
| 2945 | * interface is just being used for passive snooping, the |
| 2946 | * driver might set the size of buffers in the receive ring |
| 2947 | * based on the MTU, so that the MTU limits the maximum size |
| 2948 | * of packets that we can receive.) |
| 2949 | * |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 2950 | * If segmentation/fragmentation or receive offload are |
| 2951 | * enabled, we can get reassembled/aggregated packets larger |
| 2952 | * than MTU, but bounded to 65535 plus the Ethernet overhead, |
| 2953 | * due to kernel and protocol constraints */ |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 2954 | frame_size = handle->snapshot; |
| 2955 | if (handle->linktype == DLT_EN10MB) { |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 2956 | unsigned int max_frame_len; |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 2957 | int mtu; |
| 2958 | int offload; |
| 2959 | |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 2960 | mtu = iface_get_mtu(handle->fd, handle->opt.device, |
| 2961 | handle->errbuf); |
| 2962 | if (mtu == -1) { |
| 2963 | *status = PCAP_ERROR; |
| 2964 | return -1; |
| 2965 | } |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 2966 | offload = iface_get_offload(handle); |
| 2967 | if (offload == -1) { |
| 2968 | *status = PCAP_ERROR; |
| 2969 | return -1; |
| 2970 | } |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 2971 | if (offload) |
| 2972 | max_frame_len = MAX(mtu, 65535); |
| 2973 | else |
| 2974 | max_frame_len = mtu; |
| 2975 | max_frame_len += 18; |
| 2976 | |
| 2977 | if (frame_size > max_frame_len) |
| 2978 | frame_size = max_frame_len; |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 2979 | } |
| 2980 | |
| 2981 | /* NOTE: calculus matching those in tpacket_rcv() |
| 2982 | * in linux-2.6/net/packet/af_packet.c |
| 2983 | */ |
| 2984 | len = sizeof(sk_type); |
| 2985 | if (getsockopt(handle->fd, SOL_SOCKET, SO_TYPE, &sk_type, |
| 2986 | &len) < 0) { |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 2987 | pcap_fmt_errmsg_for_errno(handle->errbuf, |
Haibo Huang | 4ccd683 | 2020-04-23 18:03:48 -0700 | [diff] [blame] | 2988 | PCAP_ERRBUF_SIZE, errno, "getsockopt (SO_TYPE)"); |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 2989 | *status = PCAP_ERROR; |
| 2990 | return -1; |
| 2991 | } |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 2992 | maclen = (sk_type == SOCK_DGRAM) ? 0 : MAX_LINKHEADER_SIZE; |
| 2993 | /* XXX: in the kernel maclen is calculated from |
| 2994 | * LL_ALLOCATED_SPACE(dev) and vnet_hdr.hdr_len |
| 2995 | * in: packet_snd() in linux-2.6/net/packet/af_packet.c |
| 2996 | * then packet_alloc_skb() in linux-2.6/net/packet/af_packet.c |
| 2997 | * then sock_alloc_send_pskb() in linux-2.6/net/core/sock.c |
| 2998 | * but I see no way to get those sizes in userspace, |
| 2999 | * like for instance with an ifreq ioctl(); |
| 3000 | * the best thing I've found so far is MAX_HEADER in |
| 3001 | * the kernel part of linux-2.6/include/linux/netdevice.h |
| 3002 | * which goes up to 128+48=176; since pcap-linux.c |
| 3003 | * defines a MAX_LINKHEADER_SIZE of 256 which is |
| 3004 | * greater than that, let's use it.. maybe is it even |
| 3005 | * large enough to directly replace macoff.. |
| 3006 | */ |
| 3007 | tp_hdrlen = TPACKET_ALIGN(handlep->tp_hdrlen) + sizeof(struct sockaddr_ll) ; |
| 3008 | netoff = TPACKET_ALIGN(tp_hdrlen + (maclen < 16 ? 16 : maclen)) + tp_reserve; |
| 3009 | /* NOTE: AFAICS tp_reserve may break the TPACKET_ALIGN |
| 3010 | * of netoff, which contradicts |
| 3011 | * linux-2.6/Documentation/networking/packet_mmap.txt |
| 3012 | * documenting that: |
| 3013 | * "- Gap, chosen so that packet data (Start+tp_net) |
| 3014 | * aligns to TPACKET_ALIGNMENT=16" |
| 3015 | */ |
| 3016 | /* NOTE: in linux-2.6/include/linux/skbuff.h: |
| 3017 | * "CPUs often take a performance hit |
| 3018 | * when accessing unaligned memory locations" |
| 3019 | */ |
| 3020 | macoff = netoff - maclen; |
| 3021 | req.tp_frame_size = TPACKET_ALIGN(macoff + frame_size); |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 3022 | /* |
| 3023 | * Round the buffer size up to a multiple of the |
| 3024 | * frame size (rather than rounding down, which |
| 3025 | * would give a buffer smaller than our caller asked |
| 3026 | * for, and possibly give zero frames if the requested |
| 3027 | * buffer size is too small for one frame). |
| 3028 | */ |
| 3029 | req.tp_frame_nr = (handle->opt.buffer_size + req.tp_frame_size - 1)/req.tp_frame_size; |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 3030 | break; |
| 3031 | |
| 3032 | #ifdef HAVE_TPACKET3 |
| 3033 | case TPACKET_V3: |
| 3034 | /* The "frames" for this are actually buffers that |
| 3035 | * contain multiple variable-sized frames. |
| 3036 | * |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 3037 | * We pick a "frame" size of MAXIMUM_SNAPLEN to leave |
| 3038 | * enough room for at least one reasonably-sized packet |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 3039 | * in the "frame". */ |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 3040 | req.tp_frame_size = MAXIMUM_SNAPLEN; |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 3041 | /* |
| 3042 | * Round the buffer size up to a multiple of the |
| 3043 | * "frame" size (rather than rounding down, which |
| 3044 | * would give a buffer smaller than our caller asked |
| 3045 | * for, and possibly give zero "frames" if the requested |
| 3046 | * buffer size is too small for one "frame"). |
| 3047 | */ |
| 3048 | req.tp_frame_nr = (handle->opt.buffer_size + req.tp_frame_size - 1)/req.tp_frame_size; |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 3049 | break; |
| 3050 | #endif |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 3051 | default: |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 3052 | snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 3053 | "Internal error: unknown TPACKET_ value %u", |
| 3054 | handlep->tp_version); |
| 3055 | *status = PCAP_ERROR; |
| 3056 | return -1; |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 3057 | } |
| 3058 | |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 3059 | /* compute the minimum block size that will handle this frame. |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 3060 | * The block has to be page size aligned. |
| 3061 | * The max block size allowed by the kernel is arch-dependent and |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 3062 | * it's not explicitly checked here. */ |
| 3063 | req.tp_block_size = getpagesize(); |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 3064 | while (req.tp_block_size < req.tp_frame_size) |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 3065 | req.tp_block_size <<= 1; |
| 3066 | |
| 3067 | frames_per_block = req.tp_block_size/req.tp_frame_size; |
| 3068 | |
| 3069 | /* |
| 3070 | * PACKET_TIMESTAMP was added after linux/net_tstamp.h was, |
| 3071 | * so we check for PACKET_TIMESTAMP. We check for |
| 3072 | * linux/net_tstamp.h just in case a system somehow has |
| 3073 | * PACKET_TIMESTAMP but not linux/net_tstamp.h; that might |
| 3074 | * be unnecessary. |
| 3075 | * |
| 3076 | * SIOCSHWTSTAMP was introduced in the patch that introduced |
| 3077 | * linux/net_tstamp.h, so we don't bother checking whether |
| 3078 | * SIOCSHWTSTAMP is defined (if your Linux system has |
| 3079 | * linux/net_tstamp.h but doesn't define SIOCSHWTSTAMP, your |
| 3080 | * Linux system is badly broken). |
| 3081 | */ |
| 3082 | #if defined(HAVE_LINUX_NET_TSTAMP_H) && defined(PACKET_TIMESTAMP) |
| 3083 | /* |
| 3084 | * If we were told to do so, ask the kernel and the driver |
| 3085 | * to use hardware timestamps. |
| 3086 | * |
| 3087 | * Hardware timestamps are only supported with mmapped |
| 3088 | * captures. |
| 3089 | */ |
| 3090 | if (handle->opt.tstamp_type == PCAP_TSTAMP_ADAPTER || |
| 3091 | handle->opt.tstamp_type == PCAP_TSTAMP_ADAPTER_UNSYNCED) { |
| 3092 | struct hwtstamp_config hwconfig; |
| 3093 | struct ifreq ifr; |
| 3094 | int timesource; |
| 3095 | |
| 3096 | /* |
| 3097 | * Ask for hardware time stamps on all packets, |
| 3098 | * including transmitted packets. |
| 3099 | */ |
| 3100 | memset(&hwconfig, 0, sizeof(hwconfig)); |
| 3101 | hwconfig.tx_type = HWTSTAMP_TX_ON; |
| 3102 | hwconfig.rx_filter = HWTSTAMP_FILTER_ALL; |
| 3103 | |
| 3104 | memset(&ifr, 0, sizeof(ifr)); |
Haibo Huang | 4ccd683 | 2020-04-23 18:03:48 -0700 | [diff] [blame] | 3105 | pcap_strlcpy(ifr.ifr_name, handle->opt.device, sizeof(ifr.ifr_name)); |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 3106 | ifr.ifr_data = (void *)&hwconfig; |
| 3107 | |
| 3108 | if (ioctl(handle->fd, SIOCSHWTSTAMP, &ifr) < 0) { |
| 3109 | switch (errno) { |
| 3110 | |
| 3111 | case EPERM: |
| 3112 | /* |
| 3113 | * Treat this as an error, as the |
| 3114 | * user should try to run this |
| 3115 | * with the appropriate privileges - |
| 3116 | * and, if they can't, shouldn't |
| 3117 | * try requesting hardware time stamps. |
| 3118 | */ |
| 3119 | *status = PCAP_ERROR_PERM_DENIED; |
| 3120 | return -1; |
| 3121 | |
| 3122 | case EOPNOTSUPP: |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 3123 | case ERANGE: |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 3124 | /* |
| 3125 | * Treat this as a warning, as the |
| 3126 | * only way to fix the warning is to |
| 3127 | * get an adapter that supports hardware |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 3128 | * time stamps for *all* packets. |
| 3129 | * (ERANGE means "we support hardware |
| 3130 | * time stamps, but for packets matching |
| 3131 | * that particular filter", so it means |
| 3132 | * "we don't support hardware time stamps |
| 3133 | * for all incoming packets" here.) |
| 3134 | * |
| 3135 | * We'll just fall back on the standard |
| 3136 | * host time stamps. |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 3137 | */ |
| 3138 | *status = PCAP_WARNING_TSTAMP_TYPE_NOTSUP; |
| 3139 | break; |
| 3140 | |
| 3141 | default: |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 3142 | pcap_fmt_errmsg_for_errno(handle->errbuf, |
| 3143 | PCAP_ERRBUF_SIZE, errno, |
| 3144 | "SIOCSHWTSTAMP failed"); |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 3145 | *status = PCAP_ERROR; |
| 3146 | return -1; |
| 3147 | } |
| 3148 | } else { |
| 3149 | /* |
| 3150 | * Well, that worked. Now specify the type of |
| 3151 | * hardware time stamp we want for this |
| 3152 | * socket. |
| 3153 | */ |
| 3154 | if (handle->opt.tstamp_type == PCAP_TSTAMP_ADAPTER) { |
| 3155 | /* |
| 3156 | * Hardware timestamp, synchronized |
| 3157 | * with the system clock. |
| 3158 | */ |
| 3159 | timesource = SOF_TIMESTAMPING_SYS_HARDWARE; |
| 3160 | } else { |
| 3161 | /* |
| 3162 | * PCAP_TSTAMP_ADAPTER_UNSYNCED - hardware |
| 3163 | * timestamp, not synchronized with the |
| 3164 | * system clock. |
| 3165 | */ |
| 3166 | timesource = SOF_TIMESTAMPING_RAW_HARDWARE; |
| 3167 | } |
| 3168 | if (setsockopt(handle->fd, SOL_PACKET, PACKET_TIMESTAMP, |
| 3169 | (void *)×ource, sizeof(timesource))) { |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 3170 | pcap_fmt_errmsg_for_errno(handle->errbuf, |
| 3171 | PCAP_ERRBUF_SIZE, errno, |
| 3172 | "can't set PACKET_TIMESTAMP"); |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 3173 | *status = PCAP_ERROR; |
| 3174 | return -1; |
| 3175 | } |
| 3176 | } |
| 3177 | } |
| 3178 | #endif /* HAVE_LINUX_NET_TSTAMP_H && PACKET_TIMESTAMP */ |
| 3179 | |
| 3180 | /* ask the kernel to create the ring */ |
| 3181 | retry: |
| 3182 | req.tp_block_nr = req.tp_frame_nr / frames_per_block; |
| 3183 | |
| 3184 | /* req.tp_frame_nr is requested to match frames_per_block*req.tp_block_nr */ |
| 3185 | req.tp_frame_nr = req.tp_block_nr * frames_per_block; |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 3186 | |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 3187 | #ifdef HAVE_TPACKET3 |
| 3188 | /* timeout value to retire block - use the configured buffering timeout, or default if <0. */ |
Haibo Huang | 4ccd683 | 2020-04-23 18:03:48 -0700 | [diff] [blame] | 3189 | if (handlep->timeout > 0) { |
| 3190 | /* Use the user specified timeout as the block timeout */ |
| 3191 | req.tp_retire_blk_tov = handlep->timeout; |
| 3192 | } else if (handlep->timeout == 0) { |
| 3193 | /* |
| 3194 | * In pcap, this means "infinite timeout"; TPACKET_V3 |
| 3195 | * doesn't support that, so just set it to UINT_MAX |
| 3196 | * milliseconds. In the TPACKET_V3 loop, if the |
| 3197 | * timeout is 0, and we haven't yet seen any packets, |
| 3198 | * and we block and still don't have any packets, we |
| 3199 | * keep blocking until we do. |
| 3200 | */ |
| 3201 | req.tp_retire_blk_tov = UINT_MAX; |
| 3202 | } else { |
| 3203 | /* |
| 3204 | * XXX - this is not valid; use 0, meaning "have the |
| 3205 | * kernel pick a default", for now. |
| 3206 | */ |
| 3207 | req.tp_retire_blk_tov = 0; |
| 3208 | } |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 3209 | /* private data not used */ |
| 3210 | req.tp_sizeof_priv = 0; |
| 3211 | /* Rx ring - feature request bits - none (rxhash will not be filled) */ |
| 3212 | req.tp_feature_req_word = 0; |
| 3213 | #endif |
| 3214 | |
| 3215 | if (setsockopt(handle->fd, SOL_PACKET, PACKET_RX_RING, |
| 3216 | (void *) &req, sizeof(req))) { |
| 3217 | if ((errno == ENOMEM) && (req.tp_block_nr > 1)) { |
| 3218 | /* |
| 3219 | * Memory failure; try to reduce the requested ring |
| 3220 | * size. |
| 3221 | * |
| 3222 | * We used to reduce this by half -- do 5% instead. |
| 3223 | * That may result in more iterations and a longer |
| 3224 | * startup, but the user will be much happier with |
| 3225 | * the resulting buffer size. |
| 3226 | */ |
| 3227 | if (req.tp_frame_nr < 20) |
| 3228 | req.tp_frame_nr -= 1; |
| 3229 | else |
| 3230 | req.tp_frame_nr -= req.tp_frame_nr/20; |
| 3231 | goto retry; |
| 3232 | } |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 3233 | pcap_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE, |
| 3234 | errno, "can't create rx ring on packet socket"); |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 3235 | *status = PCAP_ERROR; |
| 3236 | return -1; |
| 3237 | } |
| 3238 | |
| 3239 | /* memory map the rx ring */ |
| 3240 | handlep->mmapbuflen = req.tp_block_nr * req.tp_block_size; |
| 3241 | handlep->mmapbuf = mmap(0, handlep->mmapbuflen, |
| 3242 | PROT_READ|PROT_WRITE, MAP_SHARED, handle->fd, 0); |
| 3243 | if (handlep->mmapbuf == MAP_FAILED) { |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 3244 | pcap_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE, |
| 3245 | errno, "can't mmap rx ring"); |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 3246 | |
| 3247 | /* clear the allocated ring on error*/ |
| 3248 | destroy_ring(handle); |
| 3249 | *status = PCAP_ERROR; |
| 3250 | return -1; |
| 3251 | } |
| 3252 | |
| 3253 | /* allocate a ring for each frame header pointer*/ |
| 3254 | handle->cc = req.tp_frame_nr; |
| 3255 | handle->buffer = malloc(handle->cc * sizeof(union thdr *)); |
| 3256 | if (!handle->buffer) { |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 3257 | pcap_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE, |
| 3258 | errno, "can't allocate ring of frame headers"); |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 3259 | |
| 3260 | destroy_ring(handle); |
| 3261 | *status = PCAP_ERROR; |
| 3262 | return -1; |
| 3263 | } |
| 3264 | |
| 3265 | /* fill the header ring with proper frame ptr*/ |
| 3266 | handle->offset = 0; |
| 3267 | for (i=0; i<req.tp_block_nr; ++i) { |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 3268 | u_char *base = &handlep->mmapbuf[i*req.tp_block_size]; |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 3269 | for (j=0; j<frames_per_block; ++j, ++handle->offset) { |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 3270 | RING_GET_CURRENT_FRAME(handle) = base; |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 3271 | base += req.tp_frame_size; |
| 3272 | } |
| 3273 | } |
| 3274 | |
| 3275 | handle->bufsize = req.tp_frame_size; |
| 3276 | handle->offset = 0; |
| 3277 | return 1; |
| 3278 | } |
| 3279 | |
| 3280 | /* free all ring related resources*/ |
| 3281 | static void |
| 3282 | destroy_ring(pcap_t *handle) |
| 3283 | { |
| 3284 | struct pcap_linux *handlep = handle->priv; |
| 3285 | |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 3286 | /* |
| 3287 | * Tell the kernel to destroy the ring. |
| 3288 | * We don't check for setsockopt failure, as 1) we can't recover |
| 3289 | * from an error and 2) we might not yet have set it up in the |
| 3290 | * first place. |
| 3291 | */ |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 3292 | struct tpacket_req req; |
| 3293 | memset(&req, 0, sizeof(req)); |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 3294 | (void)setsockopt(handle->fd, SOL_PACKET, PACKET_RX_RING, |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 3295 | (void *) &req, sizeof(req)); |
| 3296 | |
| 3297 | /* if ring is mapped, unmap it*/ |
| 3298 | if (handlep->mmapbuf) { |
| 3299 | /* do not test for mmap failure, as we can't recover from any error */ |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 3300 | (void)munmap(handlep->mmapbuf, handlep->mmapbuflen); |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 3301 | handlep->mmapbuf = NULL; |
| 3302 | } |
| 3303 | } |
| 3304 | |
| 3305 | /* |
| 3306 | * Special one-shot callback, used for pcap_next() and pcap_next_ex(), |
| 3307 | * for Linux mmapped capture. |
| 3308 | * |
| 3309 | * The problem is that pcap_next() and pcap_next_ex() expect the packet |
| 3310 | * data handed to the callback to be valid after the callback returns, |
| 3311 | * but pcap_read_linux_mmap() has to release that packet as soon as |
| 3312 | * the callback returns (otherwise, the kernel thinks there's still |
| 3313 | * at least one unprocessed packet available in the ring, so a select() |
| 3314 | * will immediately return indicating that there's data to process), so, |
| 3315 | * in the callback, we have to make a copy of the packet. |
| 3316 | * |
| 3317 | * Yes, this means that, if the capture is using the ring buffer, using |
| 3318 | * pcap_next() or pcap_next_ex() requires more copies than using |
| 3319 | * pcap_loop() or pcap_dispatch(). If that bothers you, don't use |
| 3320 | * pcap_next() or pcap_next_ex(). |
| 3321 | */ |
| 3322 | static void |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 3323 | pcap_oneshot_linux(u_char *user, const struct pcap_pkthdr *h, |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 3324 | const u_char *bytes) |
| 3325 | { |
| 3326 | struct oneshot_userdata *sp = (struct oneshot_userdata *)user; |
| 3327 | pcap_t *handle = sp->pd; |
| 3328 | struct pcap_linux *handlep = handle->priv; |
| 3329 | |
| 3330 | *sp->hdr = *h; |
| 3331 | memcpy(handlep->oneshot_buffer, bytes, h->caplen); |
| 3332 | *sp->pkt = handlep->oneshot_buffer; |
| 3333 | } |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 3334 | |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 3335 | static int |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 3336 | pcap_getnonblock_linux(pcap_t *handle) |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 3337 | { |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 3338 | struct pcap_linux *handlep = handle->priv; |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 3339 | |
| 3340 | /* use negative value of timeout to indicate non blocking ops */ |
| 3341 | return (handlep->timeout<0); |
| 3342 | } |
| 3343 | |
| 3344 | static int |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 3345 | pcap_setnonblock_linux(pcap_t *handle, int nonblock) |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 3346 | { |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 3347 | struct pcap_linux *handlep = handle->priv; |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 3348 | |
| 3349 | /* |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 3350 | * Set the file descriptor to non-blocking mode, as we use |
| 3351 | * it for sending packets. |
| 3352 | */ |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 3353 | if (pcap_setnonblock_fd(handle, nonblock) == -1) |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 3354 | return -1; |
| 3355 | |
| 3356 | /* |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 3357 | * Map each value to their corresponding negation to |
| 3358 | * preserve the timeout value provided with pcap_set_timeout. |
| 3359 | */ |
| 3360 | if (nonblock) { |
| 3361 | if (handlep->timeout >= 0) { |
| 3362 | /* |
| 3363 | * Indicate that we're switching to |
| 3364 | * non-blocking mode. |
| 3365 | */ |
| 3366 | handlep->timeout = ~handlep->timeout; |
| 3367 | } |
| 3368 | } else { |
| 3369 | if (handlep->timeout < 0) { |
| 3370 | handlep->timeout = ~handlep->timeout; |
| 3371 | } |
| 3372 | } |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 3373 | /* Update the timeout to use in poll(). */ |
| 3374 | set_poll_timeout(handlep); |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 3375 | return 0; |
| 3376 | } |
| 3377 | |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 3378 | /* |
| 3379 | * Get the status field of the ring buffer frame at a specified offset. |
| 3380 | */ |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 3381 | static inline u_int |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 3382 | pcap_get_ring_frame_status(pcap_t *handle, int offset) |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 3383 | { |
| 3384 | struct pcap_linux *handlep = handle->priv; |
| 3385 | union thdr h; |
| 3386 | |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 3387 | h.raw = RING_GET_FRAME_AT(handle, offset); |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 3388 | switch (handlep->tp_version) { |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 3389 | case TPACKET_V2: |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 3390 | return __atomic_load_n(&h.h2->tp_status, __ATOMIC_ACQUIRE); |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 3391 | break; |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 3392 | #ifdef HAVE_TPACKET3 |
| 3393 | case TPACKET_V3: |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 3394 | return __atomic_load_n(&h.h3->hdr.bh1.block_status, __ATOMIC_ACQUIRE); |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 3395 | break; |
| 3396 | #endif |
| 3397 | } |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 3398 | /* This should not happen. */ |
| 3399 | return 0; |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 3400 | } |
| 3401 | |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 3402 | /* |
| 3403 | * Block waiting for frames to be available. |
| 3404 | */ |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 3405 | static int pcap_wait_for_frames_mmap(pcap_t *handle) |
| 3406 | { |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 3407 | struct pcap_linux *handlep = handle->priv; |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 3408 | int timeout; |
| 3409 | struct ifreq ifr; |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 3410 | int ret; |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 3411 | struct pollfd pollinfo[2]; |
| 3412 | pollinfo[0].fd = handle->fd; |
| 3413 | pollinfo[0].events = POLLIN; |
| 3414 | pollinfo[1].fd = handlep->poll_breakloop_fd; |
| 3415 | pollinfo[1].events = POLLIN; |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 3416 | |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 3417 | /* |
| 3418 | * Keep polling until we either get some packets to read, see |
| 3419 | * that we got told to break out of the loop, get a fatal error, |
| 3420 | * or discover that the device went away. |
| 3421 | * |
| 3422 | * In non-blocking mode, we must still do one poll() to catch |
| 3423 | * any pending error indications, but the poll() has a timeout |
| 3424 | * of 0, so that it doesn't block, and we quit after that one |
| 3425 | * poll(). |
| 3426 | * |
| 3427 | * If we've seen an ENETDOWN, it might be the first indication |
| 3428 | * that the device went away, or it might just be that it was |
| 3429 | * configured down. Unfortunately, there's no guarantee that |
| 3430 | * the device has actually been removed as an interface, because: |
| 3431 | * |
| 3432 | * 1) if, as appears to be the case at least some of the time, |
| 3433 | * the PF_PACKET socket code first gets a NETDEV_DOWN indication |
| 3434 | * for the device and then gets a NETDEV_UNREGISTER indication |
| 3435 | * for it, the first indication will cause a wakeup with ENETDOWN |
| 3436 | * but won't set the packet socket's field for the interface index |
| 3437 | * to -1, and the second indication won't cause a wakeup (because |
| 3438 | * the first indication also caused the protocol hook to be |
| 3439 | * unregistered) but will set the packet socket's field for the |
| 3440 | * interface index to -1; |
| 3441 | * |
| 3442 | * 2) even if just a NETDEV_UNREGISTER indication is registered, |
| 3443 | * the packet socket's field for the interface index only gets |
| 3444 | * set to -1 after the wakeup, so there's a small but non-zero |
| 3445 | * risk that a thread blocked waiting for the wakeup will get |
| 3446 | * to the "fetch the socket name" code before the interface index |
| 3447 | * gets set to -1, so it'll get the old interface index. |
| 3448 | * |
| 3449 | * Therefore, if we got an ENETDOWN and haven't seen a packet |
| 3450 | * since then, we assume that we might be waiting for the interface |
| 3451 | * to disappear, and poll with a timeout to try again in a short |
| 3452 | * period of time. If we *do* see a packet, the interface has |
| 3453 | * come back up again, and is *definitely* still there, so we |
| 3454 | * don't need to poll. |
| 3455 | */ |
| 3456 | for (;;) { |
| 3457 | /* |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 3458 | * Yes, we do this even in non-blocking mode, as it's |
| 3459 | * the only way to get error indications from a |
| 3460 | * tpacket socket. |
| 3461 | * |
| 3462 | * The timeout is 0 in non-blocking mode, so poll() |
| 3463 | * returns immediately. |
| 3464 | */ |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 3465 | timeout = handlep->poll_timeout; |
| 3466 | |
| 3467 | /* |
| 3468 | * If we got an ENETDOWN and haven't gotten an indication |
| 3469 | * that the device has gone away or that the device is up, |
| 3470 | * we don't yet know for certain whether the device has |
| 3471 | * gone away or not, do a poll() with a 1-millisecond timeout, |
| 3472 | * as we have to poll indefinitely for "device went away" |
| 3473 | * indications until we either get one or see that the |
| 3474 | * device is up. |
| 3475 | */ |
| 3476 | if (handlep->netdown) { |
| 3477 | if (timeout != 0) |
| 3478 | timeout = 1; |
| 3479 | } |
| 3480 | ret = poll(pollinfo, 2, timeout); |
| 3481 | if (ret < 0) { |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 3482 | /* |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 3483 | * Error. If it's not EINTR, report it. |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 3484 | */ |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 3485 | if (errno != EINTR) { |
| 3486 | pcap_fmt_errmsg_for_errno(handle->errbuf, |
| 3487 | PCAP_ERRBUF_SIZE, errno, |
| 3488 | "can't poll on packet socket"); |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 3489 | return PCAP_ERROR; |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 3490 | } |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 3491 | |
| 3492 | /* |
| 3493 | * It's EINTR; if we were told to break out of |
| 3494 | * the loop, do so. |
| 3495 | */ |
| 3496 | if (handle->break_loop) { |
| 3497 | handle->break_loop = 0; |
| 3498 | return PCAP_ERROR_BREAK; |
| 3499 | } |
| 3500 | } else if (ret > 0) { |
| 3501 | /* |
| 3502 | * OK, some descriptor is ready. |
| 3503 | * Check the socket descriptor first. |
| 3504 | * |
| 3505 | * As I read the Linux man page, pollinfo[0].revents |
| 3506 | * will either be POLLIN, POLLERR, POLLHUP, or POLLNVAL. |
| 3507 | */ |
| 3508 | if (pollinfo[0].revents == POLLIN) { |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 3509 | /* |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 3510 | * OK, we may have packets to |
| 3511 | * read. |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 3512 | */ |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 3513 | break; |
| 3514 | } |
| 3515 | if (pollinfo[0].revents != 0) { |
| 3516 | /* |
| 3517 | * There's some indication other than |
| 3518 | * "you can read on this descriptor" on |
| 3519 | * the descriptor. |
| 3520 | */ |
| 3521 | if (pollinfo[0].revents & POLLNVAL) { |
| 3522 | snprintf(handle->errbuf, |
| 3523 | PCAP_ERRBUF_SIZE, |
| 3524 | "Invalid polling request on packet socket"); |
| 3525 | return PCAP_ERROR; |
| 3526 | } |
| 3527 | if (pollinfo[0].revents & (POLLHUP | POLLRDHUP)) { |
| 3528 | snprintf(handle->errbuf, |
| 3529 | PCAP_ERRBUF_SIZE, |
| 3530 | "Hangup on packet socket"); |
| 3531 | return PCAP_ERROR; |
| 3532 | } |
| 3533 | if (pollinfo[0].revents & POLLERR) { |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 3534 | /* |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 3535 | * Get the error. |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 3536 | */ |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 3537 | int err; |
| 3538 | socklen_t errlen; |
| 3539 | |
| 3540 | errlen = sizeof(err); |
| 3541 | if (getsockopt(handle->fd, SOL_SOCKET, |
| 3542 | SO_ERROR, &err, &errlen) == -1) { |
| 3543 | /* |
| 3544 | * The call *itself* returned |
| 3545 | * an error; make *that* |
| 3546 | * the error. |
| 3547 | */ |
| 3548 | err = errno; |
| 3549 | } |
| 3550 | |
| 3551 | /* |
| 3552 | * OK, we have the error. |
| 3553 | */ |
| 3554 | if (err == ENETDOWN) { |
| 3555 | /* |
| 3556 | * The device on which we're |
| 3557 | * capturing went away or the |
| 3558 | * interface was taken down. |
| 3559 | * |
| 3560 | * We don't know for certain |
| 3561 | * which happened, and the |
| 3562 | * next poll() may indicate |
| 3563 | * that there are packets |
| 3564 | * to be read, so just set |
| 3565 | * a flag to get us to do |
| 3566 | * checks later, and set |
| 3567 | * the required select |
| 3568 | * timeout to 1 millisecond |
| 3569 | * so that event loops that |
| 3570 | * check our socket descriptor |
| 3571 | * also time out so that |
| 3572 | * they can call us and we |
| 3573 | * can do the checks. |
| 3574 | */ |
| 3575 | handlep->netdown = 1; |
| 3576 | handle->required_select_timeout = &netdown_timeout; |
| 3577 | } else if (err == 0) { |
| 3578 | /* |
| 3579 | * This shouldn't happen, so |
| 3580 | * report a special indication |
| 3581 | * that it did. |
| 3582 | */ |
| 3583 | snprintf(handle->errbuf, |
| 3584 | PCAP_ERRBUF_SIZE, |
| 3585 | "Error condition on packet socket: Reported error was 0"); |
| 3586 | return PCAP_ERROR; |
| 3587 | } else { |
| 3588 | pcap_fmt_errmsg_for_errno(handle->errbuf, |
| 3589 | PCAP_ERRBUF_SIZE, |
| 3590 | err, |
| 3591 | "Error condition on packet socket"); |
| 3592 | return PCAP_ERROR; |
| 3593 | } |
| 3594 | } |
| 3595 | } |
| 3596 | /* |
| 3597 | * Now check the event device. |
| 3598 | */ |
| 3599 | if (pollinfo[1].revents & POLLIN) { |
| 3600 | ssize_t nread; |
| 3601 | uint64_t value; |
| 3602 | |
| 3603 | /* |
| 3604 | * This should never fail, but, just |
| 3605 | * in case.... |
| 3606 | */ |
| 3607 | nread = read(handlep->poll_breakloop_fd, &value, |
| 3608 | sizeof(value)); |
| 3609 | if (nread == -1) { |
| 3610 | pcap_fmt_errmsg_for_errno(handle->errbuf, |
| 3611 | PCAP_ERRBUF_SIZE, |
| 3612 | errno, |
| 3613 | "Error reading from event FD"); |
| 3614 | return PCAP_ERROR; |
| 3615 | } |
| 3616 | |
| 3617 | /* |
| 3618 | * According to the Linux read(2) man |
| 3619 | * page, read() will transfer at most |
| 3620 | * 2^31-1 bytes, so the return value is |
| 3621 | * either -1 or a value between 0 |
| 3622 | * and 2^31-1, so it's non-negative. |
| 3623 | * |
| 3624 | * Cast it to size_t to squelch |
| 3625 | * warnings from the compiler; add this |
| 3626 | * comment to squelch warnings from |
| 3627 | * humans reading the code. :-) |
| 3628 | * |
| 3629 | * Don't treat an EOF as an error, but |
| 3630 | * *do* treat a short read as an error; |
| 3631 | * that "shouldn't happen", but.... |
| 3632 | */ |
| 3633 | if (nread != 0 && |
| 3634 | (size_t)nread < sizeof(value)) { |
| 3635 | snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, |
| 3636 | "Short read from event FD: expected %zu, got %zd", |
| 3637 | sizeof(value), nread); |
| 3638 | return PCAP_ERROR; |
| 3639 | } |
| 3640 | |
| 3641 | /* |
| 3642 | * This event gets signaled by a |
| 3643 | * pcap_breakloop() call; if we were told |
| 3644 | * to break out of the loop, do so. |
| 3645 | */ |
| 3646 | if (handle->break_loop) { |
| 3647 | handle->break_loop = 0; |
| 3648 | return PCAP_ERROR_BREAK; |
| 3649 | } |
| 3650 | } |
| 3651 | } |
| 3652 | |
| 3653 | /* |
| 3654 | * Either: |
| 3655 | * |
| 3656 | * 1) we got neither an error from poll() nor any |
| 3657 | * readable descriptors, in which case there |
| 3658 | * are no packets waiting to read |
| 3659 | * |
| 3660 | * or |
| 3661 | * |
| 3662 | * 2) We got readable descriptors but the PF_PACKET |
| 3663 | * socket wasn't one of them, in which case there |
| 3664 | * are no packets waiting to read |
| 3665 | * |
| 3666 | * so, if we got an ENETDOWN, we've drained whatever |
| 3667 | * packets were available to read at the point of the |
| 3668 | * ENETDOWN. |
| 3669 | * |
| 3670 | * So, if we got an ENETDOWN and haven't gotten an indication |
| 3671 | * that the device has gone away or that the device is up, |
| 3672 | * we don't yet know for certain whether the device has |
| 3673 | * gone away or not, check whether the device exists and is |
| 3674 | * up. |
| 3675 | */ |
| 3676 | if (handlep->netdown) { |
| 3677 | if (!device_still_exists(handle)) { |
| 3678 | /* |
| 3679 | * The device doesn't exist any more; |
| 3680 | * report that. |
| 3681 | * |
| 3682 | * XXX - we should really return an |
| 3683 | * appropriate error for that, but |
| 3684 | * pcap_dispatch() etc. aren't documented |
| 3685 | * as having error returns other than |
| 3686 | * PCAP_ERROR or PCAP_ERROR_BREAK. |
| 3687 | */ |
| 3688 | snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, |
| 3689 | "The interface disappeared"); |
| 3690 | return PCAP_ERROR; |
| 3691 | } |
| 3692 | |
| 3693 | /* |
| 3694 | * The device still exists; try to see if it's up. |
| 3695 | */ |
| 3696 | memset(&ifr, 0, sizeof(ifr)); |
| 3697 | pcap_strlcpy(ifr.ifr_name, handlep->device, |
| 3698 | sizeof(ifr.ifr_name)); |
| 3699 | if (ioctl(handle->fd, SIOCGIFFLAGS, &ifr) == -1) { |
| 3700 | if (errno == ENXIO || errno == ENODEV) { |
| 3701 | /* |
| 3702 | * OK, *now* it's gone. |
| 3703 | * |
| 3704 | * XXX - see above comment. |
| 3705 | */ |
| 3706 | snprintf(handle->errbuf, |
| 3707 | PCAP_ERRBUF_SIZE, |
| 3708 | "The interface disappeared"); |
| 3709 | return PCAP_ERROR; |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 3710 | } else { |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 3711 | pcap_fmt_errmsg_for_errno(handle->errbuf, |
| 3712 | PCAP_ERRBUF_SIZE, errno, |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 3713 | "%s: Can't get flags", |
| 3714 | handlep->device); |
| 3715 | return PCAP_ERROR; |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 3716 | } |
| 3717 | } |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 3718 | if (ifr.ifr_flags & IFF_UP) { |
| 3719 | /* |
| 3720 | * It's up, so it definitely still exists. |
| 3721 | * Cancel the ENETDOWN indication - we |
| 3722 | * presumably got it due to the interface |
| 3723 | * going down rather than the device going |
| 3724 | * away - and revert to "no required select |
| 3725 | * timeout. |
| 3726 | */ |
| 3727 | handlep->netdown = 0; |
| 3728 | handle->required_select_timeout = NULL; |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 3729 | } |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 3730 | } |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 3731 | |
| 3732 | /* |
| 3733 | * If we're in non-blocking mode, just quit now, rather |
| 3734 | * than spinning in a loop doing poll()s that immediately |
| 3735 | * time out if there's no indication on any descriptor. |
| 3736 | */ |
| 3737 | if (handlep->poll_timeout == 0) |
| 3738 | break; |
| 3739 | } |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 3740 | return 0; |
| 3741 | } |
| 3742 | |
| 3743 | /* handle a single memory mapped packet */ |
| 3744 | static int pcap_handle_packet_mmap( |
| 3745 | pcap_t *handle, |
| 3746 | pcap_handler callback, |
| 3747 | u_char *user, |
| 3748 | unsigned char *frame, |
| 3749 | unsigned int tp_len, |
| 3750 | unsigned int tp_mac, |
| 3751 | unsigned int tp_snaplen, |
| 3752 | unsigned int tp_sec, |
| 3753 | unsigned int tp_usec, |
| 3754 | int tp_vlan_tci_valid, |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 3755 | __u16 tp_vlan_tci, |
| 3756 | __u16 tp_vlan_tpid) |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 3757 | { |
| 3758 | struct pcap_linux *handlep = handle->priv; |
| 3759 | unsigned char *bp; |
| 3760 | struct sockaddr_ll *sll; |
| 3761 | struct pcap_pkthdr pcaphdr; |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 3762 | unsigned int snaplen = tp_snaplen; |
Haibo Huang | 4ccd683 | 2020-04-23 18:03:48 -0700 | [diff] [blame] | 3763 | struct utsname utsname; |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 3764 | |
| 3765 | /* perform sanity check on internal offset. */ |
| 3766 | if (tp_mac + tp_snaplen > handle->bufsize) { |
Haibo Huang | 4ccd683 | 2020-04-23 18:03:48 -0700 | [diff] [blame] | 3767 | /* |
| 3768 | * Report some system information as a debugging aid. |
| 3769 | */ |
| 3770 | if (uname(&utsname) != -1) { |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 3771 | snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, |
Haibo Huang | 4ccd683 | 2020-04-23 18:03:48 -0700 | [diff] [blame] | 3772 | "corrupted frame on kernel ring mac " |
| 3773 | "offset %u + caplen %u > frame len %d " |
| 3774 | "(kernel %.32s version %s, machine %.16s)", |
| 3775 | tp_mac, tp_snaplen, handle->bufsize, |
| 3776 | utsname.release, utsname.version, |
| 3777 | utsname.machine); |
| 3778 | } else { |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 3779 | snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, |
Haibo Huang | 4ccd683 | 2020-04-23 18:03:48 -0700 | [diff] [blame] | 3780 | "corrupted frame on kernel ring mac " |
| 3781 | "offset %u + caplen %u > frame len %d", |
| 3782 | tp_mac, tp_snaplen, handle->bufsize); |
| 3783 | } |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 3784 | return -1; |
| 3785 | } |
| 3786 | |
| 3787 | /* run filter on received packet |
| 3788 | * If the kernel filtering is enabled we need to run the |
| 3789 | * filter until all the frames present into the ring |
| 3790 | * at filter creation time are processed. |
| 3791 | * In this case, blocks_to_filter_in_userland is used |
| 3792 | * as a counter for the packet we need to filter. |
| 3793 | * Note: alternatively it could be possible to stop applying |
| 3794 | * the filter when the ring became empty, but it can possibly |
| 3795 | * happen a lot later... */ |
| 3796 | bp = frame + tp_mac; |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 3797 | |
| 3798 | /* if required build in place the sll header*/ |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 3799 | sll = (void *)(frame + TPACKET_ALIGN(handlep->tp_hdrlen)); |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 3800 | if (handlep->cooked) { |
Haibo Huang | 4ccd683 | 2020-04-23 18:03:48 -0700 | [diff] [blame] | 3801 | if (handle->linktype == DLT_LINUX_SLL2) { |
| 3802 | struct sll2_header *hdrp; |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 3803 | |
Haibo Huang | 4ccd683 | 2020-04-23 18:03:48 -0700 | [diff] [blame] | 3804 | /* |
| 3805 | * The kernel should have left us with enough |
| 3806 | * space for an sll header; back up the packet |
| 3807 | * data pointer into that space, as that'll be |
| 3808 | * the beginning of the packet we pass to the |
| 3809 | * callback. |
| 3810 | */ |
| 3811 | bp -= SLL2_HDR_LEN; |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 3812 | |
Haibo Huang | 4ccd683 | 2020-04-23 18:03:48 -0700 | [diff] [blame] | 3813 | /* |
| 3814 | * Let's make sure that's past the end of |
| 3815 | * the tpacket header, i.e. >= |
| 3816 | * ((u_char *)thdr + TPACKET_HDRLEN), so we |
| 3817 | * don't step on the header when we construct |
| 3818 | * the sll header. |
| 3819 | */ |
| 3820 | if (bp < (u_char *)frame + |
| 3821 | TPACKET_ALIGN(handlep->tp_hdrlen) + |
| 3822 | sizeof(struct sockaddr_ll)) { |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 3823 | snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, |
Haibo Huang | 4ccd683 | 2020-04-23 18:03:48 -0700 | [diff] [blame] | 3824 | "cooked-mode frame doesn't have room for sll header"); |
| 3825 | return -1; |
| 3826 | } |
| 3827 | |
| 3828 | /* |
| 3829 | * OK, that worked; construct the sll header. |
| 3830 | */ |
| 3831 | hdrp = (struct sll2_header *)bp; |
| 3832 | hdrp->sll2_protocol = sll->sll_protocol; |
| 3833 | hdrp->sll2_reserved_mbz = 0; |
| 3834 | hdrp->sll2_if_index = htonl(sll->sll_ifindex); |
| 3835 | hdrp->sll2_hatype = htons(sll->sll_hatype); |
| 3836 | hdrp->sll2_pkttype = sll->sll_pkttype; |
| 3837 | hdrp->sll2_halen = sll->sll_halen; |
| 3838 | memcpy(hdrp->sll2_addr, sll->sll_addr, SLL_ADDRLEN); |
| 3839 | |
| 3840 | snaplen += sizeof(struct sll2_header); |
| 3841 | } else { |
| 3842 | struct sll_header *hdrp; |
| 3843 | |
| 3844 | /* |
| 3845 | * The kernel should have left us with enough |
| 3846 | * space for an sll header; back up the packet |
| 3847 | * data pointer into that space, as that'll be |
| 3848 | * the beginning of the packet we pass to the |
| 3849 | * callback. |
| 3850 | */ |
| 3851 | bp -= SLL_HDR_LEN; |
| 3852 | |
| 3853 | /* |
| 3854 | * Let's make sure that's past the end of |
| 3855 | * the tpacket header, i.e. >= |
| 3856 | * ((u_char *)thdr + TPACKET_HDRLEN), so we |
| 3857 | * don't step on the header when we construct |
| 3858 | * the sll header. |
| 3859 | */ |
| 3860 | if (bp < (u_char *)frame + |
| 3861 | TPACKET_ALIGN(handlep->tp_hdrlen) + |
| 3862 | sizeof(struct sockaddr_ll)) { |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 3863 | snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, |
Haibo Huang | 4ccd683 | 2020-04-23 18:03:48 -0700 | [diff] [blame] | 3864 | "cooked-mode frame doesn't have room for sll header"); |
| 3865 | return -1; |
| 3866 | } |
| 3867 | |
| 3868 | /* |
| 3869 | * OK, that worked; construct the sll header. |
| 3870 | */ |
| 3871 | hdrp = (struct sll_header *)bp; |
| 3872 | hdrp->sll_pkttype = htons(sll->sll_pkttype); |
| 3873 | hdrp->sll_hatype = htons(sll->sll_hatype); |
| 3874 | hdrp->sll_halen = htons(sll->sll_halen); |
| 3875 | memcpy(hdrp->sll_addr, sll->sll_addr, SLL_ADDRLEN); |
| 3876 | hdrp->sll_protocol = sll->sll_protocol; |
| 3877 | |
| 3878 | snaplen += sizeof(struct sll_header); |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 3879 | } |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 3880 | } |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 3881 | |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 3882 | if (handlep->filter_in_userland && handle->fcode.bf_insns) { |
| 3883 | struct bpf_aux_data aux_data; |
| 3884 | |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 3885 | aux_data.vlan_tag_present = tp_vlan_tci_valid; |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 3886 | aux_data.vlan_tag = tp_vlan_tci & 0x0fff; |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 3887 | |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 3888 | if (pcap_filter_with_aux_data(handle->fcode.bf_insns, |
| 3889 | bp, |
| 3890 | tp_len, |
| 3891 | snaplen, |
| 3892 | &aux_data) == 0) |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 3893 | return 0; |
| 3894 | } |
| 3895 | |
| 3896 | if (!linux_check_direction(handle, sll)) |
| 3897 | return 0; |
| 3898 | |
| 3899 | /* get required packet info from ring header */ |
| 3900 | pcaphdr.ts.tv_sec = tp_sec; |
| 3901 | pcaphdr.ts.tv_usec = tp_usec; |
| 3902 | pcaphdr.caplen = tp_snaplen; |
| 3903 | pcaphdr.len = tp_len; |
| 3904 | |
| 3905 | /* if required build in place the sll header*/ |
| 3906 | if (handlep->cooked) { |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 3907 | /* update packet len */ |
Haibo Huang | 4ccd683 | 2020-04-23 18:03:48 -0700 | [diff] [blame] | 3908 | if (handle->linktype == DLT_LINUX_SLL2) { |
| 3909 | pcaphdr.caplen += SLL2_HDR_LEN; |
| 3910 | pcaphdr.len += SLL2_HDR_LEN; |
| 3911 | } else { |
| 3912 | pcaphdr.caplen += SLL_HDR_LEN; |
| 3913 | pcaphdr.len += SLL_HDR_LEN; |
| 3914 | } |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 3915 | } |
| 3916 | |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 3917 | if (tp_vlan_tci_valid && |
| 3918 | handlep->vlan_offset != -1 && |
| 3919 | tp_snaplen >= (unsigned int) handlep->vlan_offset) |
| 3920 | { |
| 3921 | struct vlan_tag *tag; |
| 3922 | |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 3923 | /* |
| 3924 | * Move everything in the header, except the type field, |
| 3925 | * down VLAN_TAG_LEN bytes, to allow us to insert the |
| 3926 | * VLAN tag between that stuff and the type field. |
| 3927 | */ |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 3928 | bp -= VLAN_TAG_LEN; |
| 3929 | memmove(bp, bp + VLAN_TAG_LEN, handlep->vlan_offset); |
| 3930 | |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 3931 | /* |
| 3932 | * Now insert the tag. |
| 3933 | */ |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 3934 | tag = (struct vlan_tag *)(bp + handlep->vlan_offset); |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 3935 | tag->vlan_tpid = htons(tp_vlan_tpid); |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 3936 | tag->vlan_tci = htons(tp_vlan_tci); |
| 3937 | |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 3938 | /* |
| 3939 | * Add the tag to the packet lengths. |
| 3940 | */ |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 3941 | pcaphdr.caplen += VLAN_TAG_LEN; |
| 3942 | pcaphdr.len += VLAN_TAG_LEN; |
| 3943 | } |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 3944 | |
| 3945 | /* |
| 3946 | * The only way to tell the kernel to cut off the |
| 3947 | * packet at a snapshot length is with a filter program; |
| 3948 | * if there's no filter program, the kernel won't cut |
| 3949 | * the packet off. |
| 3950 | * |
| 3951 | * Trim the snapshot length to be no longer than the |
| 3952 | * specified snapshot length. |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 3953 | * |
| 3954 | * XXX - an alternative is to put a filter, consisting |
| 3955 | * of a "ret <snaplen>" instruction, on the socket |
| 3956 | * in the activate routine, so that the truncation is |
| 3957 | * done in the kernel even if nobody specified a filter; |
| 3958 | * that means that less buffer space is consumed in |
| 3959 | * the memory-mapped buffer. |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 3960 | */ |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 3961 | if (pcaphdr.caplen > (bpf_u_int32)handle->snapshot) |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 3962 | pcaphdr.caplen = handle->snapshot; |
| 3963 | |
| 3964 | /* pass the packet to the user */ |
| 3965 | callback(user, &pcaphdr, bp); |
| 3966 | |
| 3967 | return 1; |
| 3968 | } |
| 3969 | |
| 3970 | static int |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 3971 | pcap_read_linux_mmap_v2(pcap_t *handle, int max_packets, pcap_handler callback, |
| 3972 | u_char *user) |
| 3973 | { |
| 3974 | struct pcap_linux *handlep = handle->priv; |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 3975 | union thdr h; |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 3976 | int pkts = 0; |
| 3977 | int ret; |
| 3978 | |
| 3979 | /* wait for frames availability.*/ |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 3980 | h.raw = RING_GET_CURRENT_FRAME(handle); |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 3981 | if (!packet_mmap_acquire(h.h2)) { |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 3982 | /* |
| 3983 | * The current frame is owned by the kernel; wait for |
| 3984 | * a frame to be handed to us. |
| 3985 | */ |
| 3986 | ret = pcap_wait_for_frames_mmap(handle); |
| 3987 | if (ret) { |
| 3988 | return ret; |
| 3989 | } |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 3990 | } |
| 3991 | |
| 3992 | /* non-positive values of max_packets are used to require all |
| 3993 | * packets currently available in the ring */ |
| 3994 | while ((pkts < max_packets) || PACKET_COUNT_IS_UNLIMITED(max_packets)) { |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 3995 | /* |
| 3996 | * Get the current ring buffer frame, and break if |
| 3997 | * it's still owned by the kernel. |
| 3998 | */ |
| 3999 | h.raw = RING_GET_CURRENT_FRAME(handle); |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 4000 | if (!packet_mmap_acquire(h.h2)) |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 4001 | break; |
| 4002 | |
| 4003 | ret = pcap_handle_packet_mmap( |
| 4004 | handle, |
| 4005 | callback, |
| 4006 | user, |
| 4007 | h.raw, |
| 4008 | h.h2->tp_len, |
| 4009 | h.h2->tp_mac, |
| 4010 | h.h2->tp_snaplen, |
| 4011 | h.h2->tp_sec, |
| 4012 | handle->opt.tstamp_precision == PCAP_TSTAMP_PRECISION_NANO ? h.h2->tp_nsec : h.h2->tp_nsec / 1000, |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 4013 | VLAN_VALID(h.h2, h.h2), |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 4014 | h.h2->tp_vlan_tci, |
| 4015 | VLAN_TPID(h.h2, h.h2)); |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 4016 | if (ret == 1) { |
| 4017 | pkts++; |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 4018 | } else if (ret < 0) { |
| 4019 | return ret; |
| 4020 | } |
| 4021 | |
| 4022 | /* |
| 4023 | * Hand this block back to the kernel, and, if we're |
| 4024 | * counting blocks that need to be filtered in userland |
| 4025 | * after having been filtered by the kernel, count |
| 4026 | * the one we've just processed. |
| 4027 | */ |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 4028 | packet_mmap_release(h.h2); |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 4029 | if (handlep->blocks_to_filter_in_userland > 0) { |
| 4030 | handlep->blocks_to_filter_in_userland--; |
| 4031 | if (handlep->blocks_to_filter_in_userland == 0) { |
| 4032 | /* |
| 4033 | * No more blocks need to be filtered |
| 4034 | * in userland. |
| 4035 | */ |
| 4036 | handlep->filter_in_userland = 0; |
| 4037 | } |
| 4038 | } |
| 4039 | |
| 4040 | /* next block */ |
| 4041 | if (++handle->offset >= handle->cc) |
| 4042 | handle->offset = 0; |
| 4043 | |
| 4044 | /* check for break loop condition*/ |
| 4045 | if (handle->break_loop) { |
| 4046 | handle->break_loop = 0; |
| 4047 | return PCAP_ERROR_BREAK; |
| 4048 | } |
| 4049 | } |
| 4050 | return pkts; |
| 4051 | } |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 4052 | |
| 4053 | #ifdef HAVE_TPACKET3 |
| 4054 | static int |
| 4055 | pcap_read_linux_mmap_v3(pcap_t *handle, int max_packets, pcap_handler callback, |
| 4056 | u_char *user) |
| 4057 | { |
| 4058 | struct pcap_linux *handlep = handle->priv; |
| 4059 | union thdr h; |
| 4060 | int pkts = 0; |
| 4061 | int ret; |
| 4062 | |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 4063 | again: |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 4064 | if (handlep->current_packet == NULL) { |
| 4065 | /* wait for frames availability.*/ |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 4066 | h.raw = RING_GET_CURRENT_FRAME(handle); |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 4067 | if (!packet_mmap_v3_acquire(h.h3)) { |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 4068 | /* |
| 4069 | * The current frame is owned by the kernel; wait |
| 4070 | * for a frame to be handed to us. |
| 4071 | */ |
| 4072 | ret = pcap_wait_for_frames_mmap(handle); |
| 4073 | if (ret) { |
| 4074 | return ret; |
| 4075 | } |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 4076 | } |
| 4077 | } |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 4078 | h.raw = RING_GET_CURRENT_FRAME(handle); |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 4079 | if (!packet_mmap_v3_acquire(h.h3)) { |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 4080 | if (pkts == 0 && handlep->timeout == 0) { |
| 4081 | /* Block until we see a packet. */ |
| 4082 | goto again; |
| 4083 | } |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 4084 | return pkts; |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 4085 | } |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 4086 | |
| 4087 | /* non-positive values of max_packets are used to require all |
| 4088 | * packets currently available in the ring */ |
| 4089 | while ((pkts < max_packets) || PACKET_COUNT_IS_UNLIMITED(max_packets)) { |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 4090 | int packets_to_read; |
| 4091 | |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 4092 | if (handlep->current_packet == NULL) { |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 4093 | h.raw = RING_GET_CURRENT_FRAME(handle); |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 4094 | if (!packet_mmap_v3_acquire(h.h3)) |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 4095 | break; |
| 4096 | |
| 4097 | handlep->current_packet = h.raw + h.h3->hdr.bh1.offset_to_first_pkt; |
| 4098 | handlep->packets_left = h.h3->hdr.bh1.num_pkts; |
| 4099 | } |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 4100 | packets_to_read = handlep->packets_left; |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 4101 | |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 4102 | if (!PACKET_COUNT_IS_UNLIMITED(max_packets) && |
| 4103 | packets_to_read > (max_packets - pkts)) { |
| 4104 | /* |
| 4105 | * We've been given a maximum number of packets |
| 4106 | * to process, and there are more packets in |
| 4107 | * this buffer than that. Only process enough |
| 4108 | * of them to get us up to that maximum. |
| 4109 | */ |
| 4110 | packets_to_read = max_packets - pkts; |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 4111 | } |
| 4112 | |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 4113 | while (packets_to_read-- && !handle->break_loop) { |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 4114 | struct tpacket3_hdr* tp3_hdr = (struct tpacket3_hdr*) handlep->current_packet; |
| 4115 | ret = pcap_handle_packet_mmap( |
| 4116 | handle, |
| 4117 | callback, |
| 4118 | user, |
| 4119 | handlep->current_packet, |
| 4120 | tp3_hdr->tp_len, |
| 4121 | tp3_hdr->tp_mac, |
| 4122 | tp3_hdr->tp_snaplen, |
| 4123 | tp3_hdr->tp_sec, |
| 4124 | handle->opt.tstamp_precision == PCAP_TSTAMP_PRECISION_NANO ? tp3_hdr->tp_nsec : tp3_hdr->tp_nsec / 1000, |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 4125 | VLAN_VALID(tp3_hdr, &tp3_hdr->hv1), |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 4126 | tp3_hdr->hv1.tp_vlan_tci, |
| 4127 | VLAN_TPID(tp3_hdr, &tp3_hdr->hv1)); |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 4128 | if (ret == 1) { |
| 4129 | pkts++; |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 4130 | } else if (ret < 0) { |
| 4131 | handlep->current_packet = NULL; |
| 4132 | return ret; |
| 4133 | } |
| 4134 | handlep->current_packet += tp3_hdr->tp_next_offset; |
| 4135 | handlep->packets_left--; |
| 4136 | } |
| 4137 | |
| 4138 | if (handlep->packets_left <= 0) { |
| 4139 | /* |
| 4140 | * Hand this block back to the kernel, and, if |
| 4141 | * we're counting blocks that need to be |
| 4142 | * filtered in userland after having been |
| 4143 | * filtered by the kernel, count the one we've |
| 4144 | * just processed. |
| 4145 | */ |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 4146 | packet_mmap_v3_release(h.h3); |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 4147 | if (handlep->blocks_to_filter_in_userland > 0) { |
| 4148 | handlep->blocks_to_filter_in_userland--; |
| 4149 | if (handlep->blocks_to_filter_in_userland == 0) { |
| 4150 | /* |
| 4151 | * No more blocks need to be filtered |
| 4152 | * in userland. |
| 4153 | */ |
| 4154 | handlep->filter_in_userland = 0; |
| 4155 | } |
| 4156 | } |
| 4157 | |
| 4158 | /* next block */ |
| 4159 | if (++handle->offset >= handle->cc) |
| 4160 | handle->offset = 0; |
| 4161 | |
| 4162 | handlep->current_packet = NULL; |
| 4163 | } |
| 4164 | |
| 4165 | /* check for break loop condition*/ |
| 4166 | if (handle->break_loop) { |
| 4167 | handle->break_loop = 0; |
| 4168 | return PCAP_ERROR_BREAK; |
| 4169 | } |
| 4170 | } |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 4171 | if (pkts == 0 && handlep->timeout == 0) { |
| 4172 | /* Block until we see a packet. */ |
| 4173 | goto again; |
| 4174 | } |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 4175 | return pkts; |
| 4176 | } |
| 4177 | #endif /* HAVE_TPACKET3 */ |
| 4178 | |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 4179 | /* |
| 4180 | * Attach the given BPF code to the packet capture device. |
| 4181 | */ |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 4182 | static int |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 4183 | pcap_setfilter_linux(pcap_t *handle, struct bpf_program *filter) |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 4184 | { |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 4185 | struct pcap_linux *handlep; |
| 4186 | struct sock_fprog fcode; |
| 4187 | int can_filter_in_kernel; |
| 4188 | int err = 0; |
| 4189 | int n, offset; |
| 4190 | |
| 4191 | if (!handle) |
| 4192 | return -1; |
| 4193 | if (!filter) { |
| 4194 | pcap_strlcpy(handle->errbuf, "setfilter: No filter specified", |
| 4195 | PCAP_ERRBUF_SIZE); |
| 4196 | return -1; |
| 4197 | } |
| 4198 | |
| 4199 | handlep = handle->priv; |
| 4200 | |
| 4201 | /* Make our private copy of the filter */ |
| 4202 | |
| 4203 | if (install_bpf_program(handle, filter) < 0) |
| 4204 | /* install_bpf_program() filled in errbuf */ |
| 4205 | return -1; |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 4206 | |
| 4207 | /* |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 4208 | * Run user level packet filter by default. Will be overridden if |
| 4209 | * installing a kernel filter succeeds. |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 4210 | */ |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 4211 | handlep->filter_in_userland = 1; |
| 4212 | |
| 4213 | /* Install kernel level filter if possible */ |
| 4214 | |
| 4215 | #ifdef USHRT_MAX |
| 4216 | if (handle->fcode.bf_len > USHRT_MAX) { |
| 4217 | /* |
| 4218 | * fcode.len is an unsigned short for current kernel. |
| 4219 | * I have yet to see BPF-Code with that much |
| 4220 | * instructions but still it is possible. So for the |
| 4221 | * sake of correctness I added this check. |
| 4222 | */ |
| 4223 | fprintf(stderr, "Warning: Filter too complex for kernel\n"); |
| 4224 | fcode.len = 0; |
| 4225 | fcode.filter = NULL; |
| 4226 | can_filter_in_kernel = 0; |
| 4227 | } else |
| 4228 | #endif /* USHRT_MAX */ |
| 4229 | { |
| 4230 | /* |
| 4231 | * Oh joy, the Linux kernel uses struct sock_fprog instead |
| 4232 | * of struct bpf_program and of course the length field is |
| 4233 | * of different size. Pointed out by Sebastian |
| 4234 | * |
| 4235 | * Oh, and we also need to fix it up so that all "ret" |
| 4236 | * instructions with non-zero operands have MAXIMUM_SNAPLEN |
| 4237 | * as the operand if we're not capturing in memory-mapped |
| 4238 | * mode, and so that, if we're in cooked mode, all memory- |
| 4239 | * reference instructions use special magic offsets in |
| 4240 | * references to the link-layer header and assume that the |
| 4241 | * link-layer payload begins at 0; "fix_program()" will do |
| 4242 | * that. |
| 4243 | */ |
| 4244 | switch (fix_program(handle, &fcode)) { |
| 4245 | |
| 4246 | case -1: |
| 4247 | default: |
| 4248 | /* |
| 4249 | * Fatal error; just quit. |
| 4250 | * (The "default" case shouldn't happen; we |
| 4251 | * return -1 for that reason.) |
| 4252 | */ |
| 4253 | return -1; |
| 4254 | |
| 4255 | case 0: |
| 4256 | /* |
| 4257 | * The program performed checks that we can't make |
| 4258 | * work in the kernel. |
| 4259 | */ |
| 4260 | can_filter_in_kernel = 0; |
| 4261 | break; |
| 4262 | |
| 4263 | case 1: |
| 4264 | /* |
| 4265 | * We have a filter that'll work in the kernel. |
| 4266 | */ |
| 4267 | can_filter_in_kernel = 1; |
| 4268 | break; |
| 4269 | } |
| 4270 | } |
| 4271 | |
| 4272 | /* |
| 4273 | * NOTE: at this point, we've set both the "len" and "filter" |
| 4274 | * fields of "fcode". As of the 2.6.32.4 kernel, at least, |
| 4275 | * those are the only members of the "sock_fprog" structure, |
| 4276 | * so we initialize every member of that structure. |
| 4277 | * |
| 4278 | * If there is anything in "fcode" that is not initialized, |
| 4279 | * it is either a field added in a later kernel, or it's |
| 4280 | * padding. |
| 4281 | * |
| 4282 | * If a new field is added, this code needs to be updated |
| 4283 | * to set it correctly. |
| 4284 | * |
| 4285 | * If there are no other fields, then: |
| 4286 | * |
| 4287 | * if the Linux kernel looks at the padding, it's |
| 4288 | * buggy; |
| 4289 | * |
| 4290 | * if the Linux kernel doesn't look at the padding, |
| 4291 | * then if some tool complains that we're passing |
| 4292 | * uninitialized data to the kernel, then the tool |
| 4293 | * is buggy and needs to understand that it's just |
| 4294 | * padding. |
| 4295 | */ |
| 4296 | if (can_filter_in_kernel) { |
| 4297 | if ((err = set_kernel_filter(handle, &fcode)) == 0) |
| 4298 | { |
| 4299 | /* |
| 4300 | * Installation succeeded - using kernel filter, |
| 4301 | * so userland filtering not needed. |
| 4302 | */ |
| 4303 | handlep->filter_in_userland = 0; |
| 4304 | } |
| 4305 | else if (err == -1) /* Non-fatal error */ |
| 4306 | { |
| 4307 | /* |
| 4308 | * Print a warning if we weren't able to install |
| 4309 | * the filter for a reason other than "this kernel |
| 4310 | * isn't configured to support socket filters. |
| 4311 | */ |
| 4312 | if (errno != ENOPROTOOPT && errno != EOPNOTSUPP) { |
| 4313 | fprintf(stderr, |
| 4314 | "Warning: Kernel filter failed: %s\n", |
| 4315 | pcap_strerror(errno)); |
| 4316 | } |
| 4317 | } |
| 4318 | } |
| 4319 | |
| 4320 | /* |
| 4321 | * If we're not using the kernel filter, get rid of any kernel |
| 4322 | * filter that might've been there before, e.g. because the |
| 4323 | * previous filter could work in the kernel, or because some other |
| 4324 | * code attached a filter to the socket by some means other than |
| 4325 | * calling "pcap_setfilter()". Otherwise, the kernel filter may |
| 4326 | * filter out packets that would pass the new userland filter. |
| 4327 | */ |
| 4328 | if (handlep->filter_in_userland) { |
| 4329 | if (reset_kernel_filter(handle) == -1) { |
| 4330 | pcap_fmt_errmsg_for_errno(handle->errbuf, |
| 4331 | PCAP_ERRBUF_SIZE, errno, |
| 4332 | "can't remove kernel filter"); |
| 4333 | err = -2; /* fatal error */ |
| 4334 | } |
| 4335 | } |
| 4336 | |
| 4337 | /* |
| 4338 | * Free up the copy of the filter that was made by "fix_program()". |
| 4339 | */ |
| 4340 | if (fcode.filter != NULL) |
| 4341 | free(fcode.filter); |
| 4342 | |
| 4343 | if (err == -2) |
| 4344 | /* Fatal error */ |
| 4345 | return -1; |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 4346 | |
| 4347 | /* |
| 4348 | * If we're filtering in userland, there's nothing to do; |
| 4349 | * the new filter will be used for the next packet. |
| 4350 | */ |
| 4351 | if (handlep->filter_in_userland) |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 4352 | return 0; |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 4353 | |
| 4354 | /* |
| 4355 | * We're filtering in the kernel; the packets present in |
| 4356 | * all blocks currently in the ring were already filtered |
| 4357 | * by the old filter, and so will need to be filtered in |
| 4358 | * userland by the new filter. |
| 4359 | * |
| 4360 | * Get an upper bound for the number of such blocks; first, |
| 4361 | * walk the ring backward and count the free blocks. |
| 4362 | */ |
| 4363 | offset = handle->offset; |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 4364 | if (--offset < 0) |
| 4365 | offset = handle->cc - 1; |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 4366 | for (n=0; n < handle->cc; ++n) { |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 4367 | if (--offset < 0) |
| 4368 | offset = handle->cc - 1; |
| 4369 | if (pcap_get_ring_frame_status(handle, offset) != TP_STATUS_KERNEL) |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 4370 | break; |
| 4371 | } |
| 4372 | |
| 4373 | /* |
| 4374 | * If we found free blocks, decrement the count of free |
| 4375 | * blocks by 1, just in case we lost a race with another |
| 4376 | * thread of control that was adding a packet while |
| 4377 | * we were counting and that had run the filter before |
| 4378 | * we changed it. |
| 4379 | * |
| 4380 | * XXX - could there be more than one block added in |
| 4381 | * this fashion? |
| 4382 | * |
| 4383 | * XXX - is there a way to avoid that race, e.g. somehow |
| 4384 | * wait for all packets that passed the old filter to |
| 4385 | * be added to the ring? |
| 4386 | */ |
| 4387 | if (n != 0) |
| 4388 | n--; |
| 4389 | |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 4390 | /* |
| 4391 | * Set the count of blocks worth of packets to filter |
| 4392 | * in userland to the total number of blocks in the |
| 4393 | * ring minus the number of free blocks we found, and |
| 4394 | * turn on userland filtering. (The count of blocks |
| 4395 | * worth of packets to filter in userland is guaranteed |
| 4396 | * not to be zero - n, above, couldn't be set to a |
| 4397 | * value > handle->cc, and if it were equal to |
| 4398 | * handle->cc, it wouldn't be zero, and thus would |
| 4399 | * be decremented to handle->cc - 1.) |
| 4400 | */ |
| 4401 | handlep->blocks_to_filter_in_userland = handle->cc - n; |
| 4402 | handlep->filter_in_userland = 1; |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 4403 | |
| 4404 | return 0; |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 4405 | } |
| 4406 | |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 4407 | /* |
| 4408 | * Return the index of the given device name. Fill ebuf and return |
| 4409 | * -1 on failure. |
| 4410 | */ |
| 4411 | static int |
| 4412 | iface_get_id(int fd, const char *device, char *ebuf) |
| 4413 | { |
| 4414 | struct ifreq ifr; |
| 4415 | |
| 4416 | memset(&ifr, 0, sizeof(ifr)); |
Haibo Huang | 4ccd683 | 2020-04-23 18:03:48 -0700 | [diff] [blame] | 4417 | pcap_strlcpy(ifr.ifr_name, device, sizeof(ifr.ifr_name)); |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 4418 | |
| 4419 | if (ioctl(fd, SIOCGIFINDEX, &ifr) == -1) { |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 4420 | pcap_fmt_errmsg_for_errno(ebuf, PCAP_ERRBUF_SIZE, |
| 4421 | errno, "SIOCGIFINDEX"); |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 4422 | return -1; |
| 4423 | } |
| 4424 | |
| 4425 | return ifr.ifr_ifindex; |
| 4426 | } |
| 4427 | |
| 4428 | /* |
| 4429 | * Bind the socket associated with FD to the given device. |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 4430 | * Return 0 on success or a PCAP_ERROR_ value on a hard error. |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 4431 | */ |
| 4432 | static int |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 4433 | iface_bind(int fd, int ifindex, char *ebuf, int protocol) |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 4434 | { |
| 4435 | struct sockaddr_ll sll; |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 4436 | int ret, err; |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 4437 | socklen_t errlen = sizeof(err); |
| 4438 | |
| 4439 | memset(&sll, 0, sizeof(sll)); |
| 4440 | sll.sll_family = AF_PACKET; |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 4441 | sll.sll_ifindex = ifindex < 0 ? 0 : ifindex; |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 4442 | sll.sll_protocol = protocol; |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 4443 | |
| 4444 | if (bind(fd, (struct sockaddr *) &sll, sizeof(sll)) == -1) { |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 4445 | if (errno == ENETDOWN) { |
| 4446 | /* |
| 4447 | * Return a "network down" indication, so that |
| 4448 | * the application can report that rather than |
| 4449 | * saying we had a mysterious failure and |
| 4450 | * suggest that they report a problem to the |
| 4451 | * libpcap developers. |
| 4452 | */ |
| 4453 | return PCAP_ERROR_IFACE_NOT_UP; |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 4454 | } |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 4455 | if (errno == ENODEV) |
| 4456 | ret = PCAP_ERROR_NO_SUCH_DEVICE; |
| 4457 | else |
| 4458 | ret = PCAP_ERROR; |
| 4459 | pcap_fmt_errmsg_for_errno(ebuf, PCAP_ERRBUF_SIZE, |
| 4460 | errno, "bind"); |
| 4461 | return ret; |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 4462 | } |
| 4463 | |
| 4464 | /* Any pending errors, e.g., network is down? */ |
| 4465 | |
| 4466 | if (getsockopt(fd, SOL_SOCKET, SO_ERROR, &err, &errlen) == -1) { |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 4467 | pcap_fmt_errmsg_for_errno(ebuf, PCAP_ERRBUF_SIZE, |
Haibo Huang | 4ccd683 | 2020-04-23 18:03:48 -0700 | [diff] [blame] | 4468 | errno, "getsockopt (SO_ERROR)"); |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 4469 | return PCAP_ERROR; |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 4470 | } |
| 4471 | |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 4472 | if (err == ENETDOWN) { |
| 4473 | /* |
| 4474 | * Return a "network down" indication, so that |
| 4475 | * the application can report that rather than |
| 4476 | * saying we had a mysterious failure and |
| 4477 | * suggest that they report a problem to the |
| 4478 | * libpcap developers. |
| 4479 | */ |
| 4480 | return PCAP_ERROR_IFACE_NOT_UP; |
| 4481 | } else if (err > 0) { |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 4482 | pcap_fmt_errmsg_for_errno(ebuf, PCAP_ERRBUF_SIZE, |
| 4483 | err, "bind"); |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 4484 | return PCAP_ERROR; |
| 4485 | } |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 4486 | |
| 4487 | return 0; |
| 4488 | } |
| 4489 | |
| 4490 | /* |
| 4491 | * Try to enter monitor mode. |
| 4492 | * If we have libnl, try to create a new monitor-mode device and |
| 4493 | * capture on that; otherwise, just say "not supported". |
| 4494 | */ |
| 4495 | #ifdef HAVE_LIBNL |
| 4496 | static int |
| 4497 | enter_rfmon_mode(pcap_t *handle, int sock_fd, const char *device) |
| 4498 | { |
| 4499 | struct pcap_linux *handlep = handle->priv; |
| 4500 | int ret; |
| 4501 | char phydev_path[PATH_MAX+1]; |
| 4502 | struct nl80211_state nlstate; |
| 4503 | struct ifreq ifr; |
| 4504 | u_int n; |
| 4505 | |
| 4506 | /* |
| 4507 | * Is this a mac80211 device? |
| 4508 | */ |
| 4509 | ret = get_mac80211_phydev(handle, device, phydev_path, PATH_MAX); |
| 4510 | if (ret < 0) |
| 4511 | return ret; /* error */ |
| 4512 | if (ret == 0) |
| 4513 | return 0; /* no error, but not mac80211 device */ |
| 4514 | |
| 4515 | /* |
| 4516 | * XXX - is this already a monN device? |
| 4517 | * If so, we're done. |
| 4518 | */ |
| 4519 | |
| 4520 | /* |
| 4521 | * OK, it's apparently a mac80211 device. |
| 4522 | * Try to find an unused monN device for it. |
| 4523 | */ |
| 4524 | ret = nl80211_init(handle, &nlstate, device); |
| 4525 | if (ret != 0) |
| 4526 | return ret; |
| 4527 | for (n = 0; n < UINT_MAX; n++) { |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 4528 | /* |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 4529 | * Try mon{n}. |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 4530 | */ |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 4531 | char mondevice[3+10+1]; /* mon{UINT_MAX}\0 */ |
| 4532 | |
| 4533 | snprintf(mondevice, sizeof mondevice, "mon%u", n); |
| 4534 | ret = add_mon_if(handle, sock_fd, &nlstate, device, mondevice); |
| 4535 | if (ret == 1) { |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 4536 | /* |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 4537 | * Success. We don't clean up the libnl state |
| 4538 | * yet, as we'll be using it later. |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 4539 | */ |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 4540 | goto added; |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 4541 | } |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 4542 | if (ret < 0) { |
| 4543 | /* |
| 4544 | * Hard failure. Just return ret; handle->errbuf |
| 4545 | * has already been set. |
| 4546 | */ |
| 4547 | nl80211_cleanup(&nlstate); |
| 4548 | return ret; |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 4549 | } |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 4550 | } |
| 4551 | |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 4552 | snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, |
| 4553 | "%s: No free monN interfaces", device); |
| 4554 | nl80211_cleanup(&nlstate); |
| 4555 | return PCAP_ERROR; |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 4556 | |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 4557 | added: |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 4558 | |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 4559 | #if 0 |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 4560 | /* |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 4561 | * Sleep for .1 seconds. |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 4562 | */ |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 4563 | delay.tv_sec = 0; |
| 4564 | delay.tv_nsec = 500000000; |
| 4565 | nanosleep(&delay, NULL); |
| 4566 | #endif |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 4567 | |
| 4568 | /* |
| 4569 | * If we haven't already done so, arrange to have |
| 4570 | * "pcap_close_all()" called when we exit. |
| 4571 | */ |
| 4572 | if (!pcap_do_addexit(handle)) { |
| 4573 | /* |
| 4574 | * "atexit()" failed; don't put the interface |
| 4575 | * in rfmon mode, just give up. |
| 4576 | */ |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 4577 | del_mon_if(handle, sock_fd, &nlstate, device, |
| 4578 | handlep->mondevice); |
| 4579 | nl80211_cleanup(&nlstate); |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 4580 | return PCAP_ERROR; |
| 4581 | } |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 4582 | |
| 4583 | /* |
| 4584 | * Now configure the monitor interface up. |
| 4585 | */ |
| 4586 | memset(&ifr, 0, sizeof(ifr)); |
| 4587 | pcap_strlcpy(ifr.ifr_name, handlep->mondevice, sizeof(ifr.ifr_name)); |
| 4588 | if (ioctl(sock_fd, SIOCGIFFLAGS, &ifr) == -1) { |
| 4589 | pcap_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE, |
| 4590 | errno, "%s: Can't get flags for %s", device, |
| 4591 | handlep->mondevice); |
| 4592 | del_mon_if(handle, sock_fd, &nlstate, device, |
| 4593 | handlep->mondevice); |
| 4594 | nl80211_cleanup(&nlstate); |
| 4595 | return PCAP_ERROR; |
| 4596 | } |
| 4597 | ifr.ifr_flags |= IFF_UP|IFF_RUNNING; |
| 4598 | if (ioctl(sock_fd, SIOCSIFFLAGS, &ifr) == -1) { |
| 4599 | pcap_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE, |
| 4600 | errno, "%s: Can't set flags for %s", device, |
| 4601 | handlep->mondevice); |
| 4602 | del_mon_if(handle, sock_fd, &nlstate, device, |
| 4603 | handlep->mondevice); |
| 4604 | nl80211_cleanup(&nlstate); |
| 4605 | return PCAP_ERROR; |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 4606 | } |
| 4607 | |
| 4608 | /* |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 4609 | * Success. Clean up the libnl state. |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 4610 | */ |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 4611 | nl80211_cleanup(&nlstate); |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 4612 | |
| 4613 | /* |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 4614 | * Note that we have to delete the monitor device when we close |
| 4615 | * the handle. |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 4616 | */ |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 4617 | handlep->must_do_on_close |= MUST_DELETE_MONIF; |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 4618 | |
| 4619 | /* |
| 4620 | * Add this to the list of pcaps to close when we exit. |
| 4621 | */ |
| 4622 | pcap_add_to_pcaps_to_close(handle); |
| 4623 | |
| 4624 | return 1; |
| 4625 | } |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 4626 | #else /* HAVE_LIBNL */ |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 4627 | static int |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 4628 | enter_rfmon_mode(pcap_t *handle _U_, int sock_fd _U_, const char *device _U_) |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 4629 | { |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 4630 | /* |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 4631 | * We don't have libnl, so we can't do monitor mode. |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 4632 | */ |
| 4633 | return 0; |
| 4634 | } |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 4635 | #endif /* HAVE_LIBNL */ |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 4636 | |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 4637 | #if defined(HAVE_LINUX_NET_TSTAMP_H) && defined(PACKET_TIMESTAMP) |
| 4638 | /* |
| 4639 | * Map SOF_TIMESTAMPING_ values to PCAP_TSTAMP_ values. |
| 4640 | */ |
| 4641 | static const struct { |
| 4642 | int soft_timestamping_val; |
| 4643 | int pcap_tstamp_val; |
| 4644 | } sof_ts_type_map[3] = { |
| 4645 | { SOF_TIMESTAMPING_SOFTWARE, PCAP_TSTAMP_HOST }, |
| 4646 | { SOF_TIMESTAMPING_SYS_HARDWARE, PCAP_TSTAMP_ADAPTER }, |
| 4647 | { SOF_TIMESTAMPING_RAW_HARDWARE, PCAP_TSTAMP_ADAPTER_UNSYNCED } |
| 4648 | }; |
| 4649 | #define NUM_SOF_TIMESTAMPING_TYPES (sizeof sof_ts_type_map / sizeof sof_ts_type_map[0]) |
| 4650 | |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 4651 | /* |
| 4652 | * Set the list of time stamping types to include all types. |
| 4653 | */ |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 4654 | static int |
| 4655 | iface_set_all_ts_types(pcap_t *handle, char *ebuf) |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 4656 | { |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 4657 | u_int i; |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 4658 | |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 4659 | handle->tstamp_type_list = malloc(NUM_SOF_TIMESTAMPING_TYPES * sizeof(u_int)); |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 4660 | if (handle->tstamp_type_list == NULL) { |
| 4661 | pcap_fmt_errmsg_for_errno(ebuf, PCAP_ERRBUF_SIZE, |
| 4662 | errno, "malloc"); |
| 4663 | return -1; |
| 4664 | } |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 4665 | for (i = 0; i < NUM_SOF_TIMESTAMPING_TYPES; i++) |
| 4666 | handle->tstamp_type_list[i] = sof_ts_type_map[i].pcap_tstamp_val; |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 4667 | handle->tstamp_type_count = NUM_SOF_TIMESTAMPING_TYPES; |
| 4668 | return 0; |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 4669 | } |
| 4670 | |
| 4671 | #ifdef ETHTOOL_GET_TS_INFO |
| 4672 | /* |
| 4673 | * Get a list of time stamping capabilities. |
| 4674 | */ |
| 4675 | static int |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 4676 | iface_ethtool_get_ts_info(const char *device, pcap_t *handle, char *ebuf) |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 4677 | { |
| 4678 | int fd; |
| 4679 | struct ifreq ifr; |
| 4680 | struct ethtool_ts_info info; |
| 4681 | int num_ts_types; |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 4682 | u_int i, j; |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 4683 | |
| 4684 | /* |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 4685 | * This doesn't apply to the "any" device; you can't say "turn on |
| 4686 | * hardware time stamping for all devices that exist now and arrange |
| 4687 | * that it be turned on for any device that appears in the future", |
| 4688 | * and not all devices even necessarily *support* hardware time |
| 4689 | * stamping, so don't report any time stamp types. |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 4690 | */ |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 4691 | if (strcmp(device, "any") == 0) { |
| 4692 | handle->tstamp_type_list = NULL; |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 4693 | return 0; |
| 4694 | } |
| 4695 | |
| 4696 | /* |
| 4697 | * Create a socket from which to fetch time stamping capabilities. |
| 4698 | */ |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 4699 | fd = get_if_ioctl_socket(); |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 4700 | if (fd < 0) { |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 4701 | pcap_fmt_errmsg_for_errno(ebuf, PCAP_ERRBUF_SIZE, |
| 4702 | errno, "socket for SIOCETHTOOL(ETHTOOL_GET_TS_INFO)"); |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 4703 | return -1; |
| 4704 | } |
| 4705 | |
| 4706 | memset(&ifr, 0, sizeof(ifr)); |
Haibo Huang | 4ccd683 | 2020-04-23 18:03:48 -0700 | [diff] [blame] | 4707 | pcap_strlcpy(ifr.ifr_name, device, sizeof(ifr.ifr_name)); |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 4708 | memset(&info, 0, sizeof(info)); |
| 4709 | info.cmd = ETHTOOL_GET_TS_INFO; |
| 4710 | ifr.ifr_data = (caddr_t)&info; |
| 4711 | if (ioctl(fd, SIOCETHTOOL, &ifr) == -1) { |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 4712 | int save_errno = errno; |
| 4713 | |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 4714 | close(fd); |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 4715 | switch (save_errno) { |
| 4716 | |
| 4717 | case EOPNOTSUPP: |
| 4718 | case EINVAL: |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 4719 | /* |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 4720 | * OK, this OS version or driver doesn't support |
| 4721 | * asking for the time stamping types, so let's |
| 4722 | * just return all the possible types. |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 4723 | */ |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 4724 | if (iface_set_all_ts_types(handle, ebuf) == -1) |
| 4725 | return -1; |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 4726 | return 0; |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 4727 | |
| 4728 | case ENODEV: |
| 4729 | /* |
| 4730 | * OK, no such device. |
| 4731 | * The user will find that out when they try to |
| 4732 | * activate the device; just return an empty |
| 4733 | * list of time stamp types. |
| 4734 | */ |
| 4735 | handle->tstamp_type_list = NULL; |
| 4736 | return 0; |
| 4737 | |
| 4738 | default: |
| 4739 | /* |
| 4740 | * Other error. |
| 4741 | */ |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 4742 | pcap_fmt_errmsg_for_errno(ebuf, PCAP_ERRBUF_SIZE, |
| 4743 | save_errno, |
| 4744 | "%s: SIOCETHTOOL(ETHTOOL_GET_TS_INFO) ioctl failed", |
| 4745 | device); |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 4746 | return -1; |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 4747 | } |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 4748 | } |
| 4749 | close(fd); |
| 4750 | |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 4751 | /* |
| 4752 | * Do we support hardware time stamping of *all* packets? |
| 4753 | */ |
| 4754 | if (!(info.rx_filters & (1 << HWTSTAMP_FILTER_ALL))) { |
| 4755 | /* |
| 4756 | * No, so don't report any time stamp types. |
| 4757 | * |
| 4758 | * XXX - some devices either don't report |
| 4759 | * HWTSTAMP_FILTER_ALL when they do support it, or |
| 4760 | * report HWTSTAMP_FILTER_ALL but map it to only |
| 4761 | * time stamping a few PTP packets. See |
| 4762 | * http://marc.info/?l=linux-netdev&m=146318183529571&w=2 |
| 4763 | */ |
| 4764 | handle->tstamp_type_list = NULL; |
| 4765 | return 0; |
| 4766 | } |
| 4767 | |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 4768 | num_ts_types = 0; |
| 4769 | for (i = 0; i < NUM_SOF_TIMESTAMPING_TYPES; i++) { |
| 4770 | if (info.so_timestamping & sof_ts_type_map[i].soft_timestamping_val) |
| 4771 | num_ts_types++; |
| 4772 | } |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 4773 | if (num_ts_types != 0) { |
| 4774 | handle->tstamp_type_list = malloc(num_ts_types * sizeof(u_int)); |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 4775 | if (handle->tstamp_type_list == NULL) { |
| 4776 | pcap_fmt_errmsg_for_errno(ebuf, PCAP_ERRBUF_SIZE, |
| 4777 | errno, "malloc"); |
| 4778 | return -1; |
| 4779 | } |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 4780 | for (i = 0, j = 0; i < NUM_SOF_TIMESTAMPING_TYPES; i++) { |
| 4781 | if (info.so_timestamping & sof_ts_type_map[i].soft_timestamping_val) { |
| 4782 | handle->tstamp_type_list[j] = sof_ts_type_map[i].pcap_tstamp_val; |
| 4783 | j++; |
| 4784 | } |
| 4785 | } |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 4786 | handle->tstamp_type_count = num_ts_types; |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 4787 | } else |
| 4788 | handle->tstamp_type_list = NULL; |
| 4789 | |
| 4790 | return 0; |
| 4791 | } |
| 4792 | #else /* ETHTOOL_GET_TS_INFO */ |
| 4793 | static int |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 4794 | iface_ethtool_get_ts_info(const char *device, pcap_t *handle, char *ebuf) |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 4795 | { |
| 4796 | /* |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 4797 | * This doesn't apply to the "any" device; you can't say "turn on |
| 4798 | * hardware time stamping for all devices that exist now and arrange |
| 4799 | * that it be turned on for any device that appears in the future", |
| 4800 | * and not all devices even necessarily *support* hardware time |
| 4801 | * stamping, so don't report any time stamp types. |
| 4802 | */ |
| 4803 | if (strcmp(device, "any") == 0) { |
| 4804 | handle->tstamp_type_list = NULL; |
| 4805 | return 0; |
| 4806 | } |
| 4807 | |
| 4808 | /* |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 4809 | * We don't have an ioctl to use to ask what's supported, |
| 4810 | * so say we support everything. |
| 4811 | */ |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 4812 | if (iface_set_all_ts_types(handle, ebuf) == -1) |
| 4813 | return -1; |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 4814 | return 0; |
| 4815 | } |
| 4816 | #endif /* ETHTOOL_GET_TS_INFO */ |
| 4817 | |
| 4818 | #endif /* defined(HAVE_LINUX_NET_TSTAMP_H) && defined(PACKET_TIMESTAMP) */ |
| 4819 | |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 4820 | /* |
| 4821 | * Find out if we have any form of fragmentation/reassembly offloading. |
| 4822 | * |
| 4823 | * We do so using SIOCETHTOOL checking for various types of offloading; |
| 4824 | * if SIOCETHTOOL isn't defined, or we don't have any #defines for any |
| 4825 | * of the types of offloading, there's nothing we can do to check, so |
| 4826 | * we just say "no, we don't". |
Haibo Huang | 4ccd683 | 2020-04-23 18:03:48 -0700 | [diff] [blame] | 4827 | * |
| 4828 | * We treat EOPNOTSUPP, EINVAL and, if eperm_ok is true, EPERM as |
| 4829 | * indications that the operation isn't supported. We do EPERM |
| 4830 | * weirdly because the SIOCETHTOOL code in later kernels 1) doesn't |
| 4831 | * support ETHTOOL_GUFO, 2) also doesn't include it in the list |
| 4832 | * of ethtool operations that don't require CAP_NET_ADMIN privileges, |
| 4833 | * and 3) does the "is this permitted" check before doing the "is |
| 4834 | * this even supported" check, so it fails with "this is not permitted" |
| 4835 | * rather than "this is not even supported". To work around this |
| 4836 | * annoyance, we only treat EPERM as an error for the first feature, |
| 4837 | * and assume that they all do the same permission checks, so if the |
| 4838 | * first one is allowed all the others are allowed if supported. |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 4839 | */ |
| 4840 | #if defined(SIOCETHTOOL) && (defined(ETHTOOL_GTSO) || defined(ETHTOOL_GUFO) || defined(ETHTOOL_GGSO) || defined(ETHTOOL_GFLAGS) || defined(ETHTOOL_GGRO)) |
| 4841 | static int |
Haibo Huang | 4ccd683 | 2020-04-23 18:03:48 -0700 | [diff] [blame] | 4842 | iface_ethtool_flag_ioctl(pcap_t *handle, int cmd, const char *cmdname, |
| 4843 | int eperm_ok) |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 4844 | { |
| 4845 | struct ifreq ifr; |
| 4846 | struct ethtool_value eval; |
| 4847 | |
| 4848 | memset(&ifr, 0, sizeof(ifr)); |
Haibo Huang | 4ccd683 | 2020-04-23 18:03:48 -0700 | [diff] [blame] | 4849 | pcap_strlcpy(ifr.ifr_name, handle->opt.device, sizeof(ifr.ifr_name)); |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 4850 | eval.cmd = cmd; |
| 4851 | eval.data = 0; |
| 4852 | ifr.ifr_data = (caddr_t)&eval; |
| 4853 | if (ioctl(handle->fd, SIOCETHTOOL, &ifr) == -1) { |
Haibo Huang | 4ccd683 | 2020-04-23 18:03:48 -0700 | [diff] [blame] | 4854 | if (errno == EOPNOTSUPP || errno == EINVAL || |
| 4855 | (errno == EPERM && eperm_ok)) { |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 4856 | /* |
| 4857 | * OK, let's just return 0, which, in our |
| 4858 | * case, either means "no, what we're asking |
| 4859 | * about is not enabled" or "all the flags |
| 4860 | * are clear (i.e., nothing is enabled)". |
| 4861 | */ |
| 4862 | return 0; |
| 4863 | } |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 4864 | pcap_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE, |
| 4865 | errno, "%s: SIOCETHTOOL(%s) ioctl failed", |
| 4866 | handle->opt.device, cmdname); |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 4867 | return -1; |
| 4868 | } |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 4869 | return eval.data; |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 4870 | } |
| 4871 | |
Haibo Huang | 4ccd683 | 2020-04-23 18:03:48 -0700 | [diff] [blame] | 4872 | /* |
| 4873 | * XXX - it's annoying that we have to check for offloading at all, but, |
| 4874 | * given that we have to, it's still annoying that we have to check for |
| 4875 | * particular types of offloading, especially that shiny new types of |
| 4876 | * offloading may be added - and, worse, may not be checkable with |
| 4877 | * a particular ETHTOOL_ operation; ETHTOOL_GFEATURES would, in |
| 4878 | * theory, give those to you, but the actual flags being used are |
| 4879 | * opaque (defined in a non-uapi header), and there doesn't seem to |
| 4880 | * be any obvious way to ask the kernel what all the offloading flags |
| 4881 | * are - at best, you can ask for a set of strings(!) to get *names* |
| 4882 | * for various flags. (That whole mechanism appears to have been |
| 4883 | * designed for the sole purpose of letting ethtool report flags |
| 4884 | * by name and set flags by name, with the names having no semantics |
| 4885 | * ethtool understands.) |
| 4886 | */ |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 4887 | static int |
| 4888 | iface_get_offload(pcap_t *handle) |
| 4889 | { |
| 4890 | int ret; |
| 4891 | |
| 4892 | #ifdef ETHTOOL_GTSO |
Haibo Huang | 4ccd683 | 2020-04-23 18:03:48 -0700 | [diff] [blame] | 4893 | ret = iface_ethtool_flag_ioctl(handle, ETHTOOL_GTSO, "ETHTOOL_GTSO", 0); |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 4894 | if (ret == -1) |
| 4895 | return -1; |
| 4896 | if (ret) |
| 4897 | return 1; /* TCP segmentation offloading on */ |
| 4898 | #endif |
| 4899 | |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 4900 | #ifdef ETHTOOL_GGSO |
| 4901 | /* |
| 4902 | * XXX - will this cause large unsegmented packets to be |
| 4903 | * handed to PF_PACKET sockets on transmission? If not, |
| 4904 | * this need not be checked. |
| 4905 | */ |
Haibo Huang | 4ccd683 | 2020-04-23 18:03:48 -0700 | [diff] [blame] | 4906 | ret = iface_ethtool_flag_ioctl(handle, ETHTOOL_GGSO, "ETHTOOL_GGSO", 0); |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 4907 | if (ret == -1) |
| 4908 | return -1; |
| 4909 | if (ret) |
| 4910 | return 1; /* generic segmentation offloading on */ |
| 4911 | #endif |
| 4912 | |
| 4913 | #ifdef ETHTOOL_GFLAGS |
Haibo Huang | 4ccd683 | 2020-04-23 18:03:48 -0700 | [diff] [blame] | 4914 | ret = iface_ethtool_flag_ioctl(handle, ETHTOOL_GFLAGS, "ETHTOOL_GFLAGS", 0); |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 4915 | if (ret == -1) |
| 4916 | return -1; |
| 4917 | if (ret & ETH_FLAG_LRO) |
| 4918 | return 1; /* large receive offloading on */ |
| 4919 | #endif |
| 4920 | |
| 4921 | #ifdef ETHTOOL_GGRO |
| 4922 | /* |
| 4923 | * XXX - will this cause large reassembled packets to be |
| 4924 | * handed to PF_PACKET sockets on receipt? If not, |
| 4925 | * this need not be checked. |
| 4926 | */ |
Haibo Huang | 4ccd683 | 2020-04-23 18:03:48 -0700 | [diff] [blame] | 4927 | ret = iface_ethtool_flag_ioctl(handle, ETHTOOL_GGRO, "ETHTOOL_GGRO", 0); |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 4928 | if (ret == -1) |
| 4929 | return -1; |
| 4930 | if (ret) |
| 4931 | return 1; /* generic (large) receive offloading on */ |
| 4932 | #endif |
| 4933 | |
Haibo Huang | 4ccd683 | 2020-04-23 18:03:48 -0700 | [diff] [blame] | 4934 | #ifdef ETHTOOL_GUFO |
| 4935 | /* |
| 4936 | * Do this one last, as support for it was removed in later |
| 4937 | * kernels, and it fails with EPERM on those kernels rather |
| 4938 | * than with EOPNOTSUPP (see explanation in comment for |
| 4939 | * iface_ethtool_flag_ioctl()). |
| 4940 | */ |
| 4941 | ret = iface_ethtool_flag_ioctl(handle, ETHTOOL_GUFO, "ETHTOOL_GUFO", 1); |
| 4942 | if (ret == -1) |
| 4943 | return -1; |
| 4944 | if (ret) |
| 4945 | return 1; /* UDP fragmentation offloading on */ |
| 4946 | #endif |
| 4947 | |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 4948 | return 0; |
| 4949 | } |
| 4950 | #else /* SIOCETHTOOL */ |
| 4951 | static int |
| 4952 | iface_get_offload(pcap_t *handle _U_) |
| 4953 | { |
| 4954 | /* |
| 4955 | * XXX - do we need to get this information if we don't |
| 4956 | * have the ethtool ioctls? If so, how do we do that? |
| 4957 | */ |
| 4958 | return 0; |
| 4959 | } |
| 4960 | #endif /* SIOCETHTOOL */ |
| 4961 | |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 4962 | static struct dsa_proto { |
| 4963 | const char *name; |
| 4964 | bpf_u_int32 linktype; |
| 4965 | } dsa_protos[] = { |
Haibo Huang | 4ccd683 | 2020-04-23 18:03:48 -0700 | [diff] [blame] | 4966 | /* |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 4967 | * None is special and indicates that the interface does not have |
| 4968 | * any tagging protocol configured, and is therefore a standard |
| 4969 | * Ethernet interface. |
Haibo Huang | 4ccd683 | 2020-04-23 18:03:48 -0700 | [diff] [blame] | 4970 | */ |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 4971 | { "none", DLT_EN10MB }, |
| 4972 | { "brcm", DLT_DSA_TAG_BRCM }, |
| 4973 | { "brcm-prepend", DLT_DSA_TAG_BRCM_PREPEND }, |
| 4974 | { "dsa", DLT_DSA_TAG_DSA }, |
| 4975 | { "edsa", DLT_DSA_TAG_EDSA }, |
| 4976 | }; |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 4977 | |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 4978 | static int |
| 4979 | iface_dsa_get_proto_info(const char *device, pcap_t *handle) |
| 4980 | { |
| 4981 | char *pathstr; |
| 4982 | unsigned int i; |
| 4983 | /* |
| 4984 | * Make this significantly smaller than PCAP_ERRBUF_SIZE; |
| 4985 | * the tag *shouldn't* have some huge long name, and making |
| 4986 | * it smaller keeps newer versions of GCC from whining that |
| 4987 | * the error message if we don't support the tag could |
| 4988 | * overflow the error message buffer. |
| 4989 | */ |
| 4990 | char buf[128]; |
| 4991 | ssize_t r; |
| 4992 | int fd; |
| 4993 | |
| 4994 | fd = asprintf(&pathstr, "/sys/class/net/%s/dsa/tagging", device); |
| 4995 | if (fd < 0) { |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 4996 | pcap_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE, |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 4997 | fd, "asprintf"); |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 4998 | return PCAP_ERROR; |
| 4999 | } |
| 5000 | |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 5001 | fd = open(pathstr, O_RDONLY); |
| 5002 | free(pathstr); |
| 5003 | /* |
| 5004 | * This is not fatal, kernel >= 4.20 *might* expose this attribute |
| 5005 | */ |
| 5006 | if (fd < 0) |
| 5007 | return 0; |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 5008 | |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 5009 | r = read(fd, buf, sizeof(buf) - 1); |
| 5010 | if (r <= 0) { |
| 5011 | pcap_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE, |
| 5012 | errno, "read"); |
| 5013 | close(fd); |
| 5014 | return PCAP_ERROR; |
| 5015 | } |
| 5016 | close(fd); |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 5017 | |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 5018 | /* |
| 5019 | * Buffer should be LF terminated. |
| 5020 | */ |
| 5021 | if (buf[r - 1] == '\n') |
| 5022 | r--; |
| 5023 | buf[r] = '\0'; |
| 5024 | |
| 5025 | for (i = 0; i < sizeof(dsa_protos) / sizeof(dsa_protos[0]); i++) { |
| 5026 | if (strlen(dsa_protos[i].name) == (size_t)r && |
| 5027 | strcmp(buf, dsa_protos[i].name) == 0) { |
| 5028 | handle->linktype = dsa_protos[i].linktype; |
| 5029 | switch (dsa_protos[i].linktype) { |
| 5030 | case DLT_EN10MB: |
| 5031 | return 0; |
| 5032 | default: |
| 5033 | return 1; |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 5034 | } |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 5035 | } |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 5036 | } |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 5037 | |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 5038 | snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, |
| 5039 | "unsupported DSA tag: %s", buf); |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 5040 | |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 5041 | return PCAP_ERROR; |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 5042 | } |
| 5043 | |
| 5044 | /* |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 5045 | * Query the kernel for the MTU of the given interface. |
| 5046 | */ |
| 5047 | static int |
| 5048 | iface_get_mtu(int fd, const char *device, char *ebuf) |
| 5049 | { |
| 5050 | struct ifreq ifr; |
| 5051 | |
| 5052 | if (!device) |
| 5053 | return BIGGER_THAN_ALL_MTUS; |
| 5054 | |
| 5055 | memset(&ifr, 0, sizeof(ifr)); |
Haibo Huang | 4ccd683 | 2020-04-23 18:03:48 -0700 | [diff] [blame] | 5056 | pcap_strlcpy(ifr.ifr_name, device, sizeof(ifr.ifr_name)); |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 5057 | |
| 5058 | if (ioctl(fd, SIOCGIFMTU, &ifr) == -1) { |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 5059 | pcap_fmt_errmsg_for_errno(ebuf, PCAP_ERRBUF_SIZE, |
| 5060 | errno, "SIOCGIFMTU"); |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 5061 | return -1; |
| 5062 | } |
| 5063 | |
| 5064 | return ifr.ifr_mtu; |
| 5065 | } |
| 5066 | |
| 5067 | /* |
| 5068 | * Get the hardware type of the given interface as ARPHRD_xxx constant. |
| 5069 | */ |
| 5070 | static int |
| 5071 | iface_get_arptype(int fd, const char *device, char *ebuf) |
| 5072 | { |
| 5073 | struct ifreq ifr; |
Haibo Huang | 4ccd683 | 2020-04-23 18:03:48 -0700 | [diff] [blame] | 5074 | int ret; |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 5075 | |
| 5076 | memset(&ifr, 0, sizeof(ifr)); |
Haibo Huang | 4ccd683 | 2020-04-23 18:03:48 -0700 | [diff] [blame] | 5077 | pcap_strlcpy(ifr.ifr_name, device, sizeof(ifr.ifr_name)); |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 5078 | |
| 5079 | if (ioctl(fd, SIOCGIFHWADDR, &ifr) == -1) { |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 5080 | if (errno == ENODEV) { |
| 5081 | /* |
| 5082 | * No such device. |
| 5083 | */ |
Haibo Huang | 4ccd683 | 2020-04-23 18:03:48 -0700 | [diff] [blame] | 5084 | ret = PCAP_ERROR_NO_SUCH_DEVICE; |
| 5085 | } else |
| 5086 | ret = PCAP_ERROR; |
| 5087 | pcap_fmt_errmsg_for_errno(ebuf, PCAP_ERRBUF_SIZE, |
| 5088 | errno, "SIOCGIFHWADDR"); |
| 5089 | return ret; |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 5090 | } |
| 5091 | |
| 5092 | return ifr.ifr_hwaddr.sa_family; |
| 5093 | } |
| 5094 | |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 5095 | static int |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 5096 | fix_program(pcap_t *handle, struct sock_fprog *fcode) |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 5097 | { |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 5098 | struct pcap_linux *handlep = handle->priv; |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 5099 | size_t prog_size; |
| 5100 | register int i; |
| 5101 | register struct bpf_insn *p; |
| 5102 | struct bpf_insn *f; |
| 5103 | int len; |
| 5104 | |
| 5105 | /* |
| 5106 | * Make a copy of the filter, and modify that copy if |
| 5107 | * necessary. |
| 5108 | */ |
| 5109 | prog_size = sizeof(*handle->fcode.bf_insns) * handle->fcode.bf_len; |
| 5110 | len = handle->fcode.bf_len; |
| 5111 | f = (struct bpf_insn *)malloc(prog_size); |
| 5112 | if (f == NULL) { |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 5113 | pcap_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE, |
| 5114 | errno, "malloc"); |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 5115 | return -1; |
| 5116 | } |
| 5117 | memcpy(f, handle->fcode.bf_insns, prog_size); |
| 5118 | fcode->len = len; |
| 5119 | fcode->filter = (struct sock_filter *) f; |
| 5120 | |
| 5121 | for (i = 0; i < len; ++i) { |
| 5122 | p = &f[i]; |
| 5123 | /* |
| 5124 | * What type of instruction is this? |
| 5125 | */ |
| 5126 | switch (BPF_CLASS(p->code)) { |
| 5127 | |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 5128 | case BPF_LD: |
| 5129 | case BPF_LDX: |
| 5130 | /* |
| 5131 | * It's a load instruction; is it loading |
| 5132 | * from the packet? |
| 5133 | */ |
| 5134 | switch (BPF_MODE(p->code)) { |
| 5135 | |
| 5136 | case BPF_ABS: |
| 5137 | case BPF_IND: |
| 5138 | case BPF_MSH: |
| 5139 | /* |
| 5140 | * Yes; are we in cooked mode? |
| 5141 | */ |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 5142 | if (handlep->cooked) { |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 5143 | /* |
| 5144 | * Yes, so we need to fix this |
| 5145 | * instruction. |
| 5146 | */ |
Haibo Huang | 4ccd683 | 2020-04-23 18:03:48 -0700 | [diff] [blame] | 5147 | if (fix_offset(handle, p) < 0) { |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 5148 | /* |
| 5149 | * We failed to do so. |
| 5150 | * Return 0, so our caller |
| 5151 | * knows to punt to userland. |
| 5152 | */ |
| 5153 | return 0; |
| 5154 | } |
| 5155 | } |
| 5156 | break; |
| 5157 | } |
| 5158 | break; |
| 5159 | } |
| 5160 | } |
| 5161 | return 1; /* we succeeded */ |
| 5162 | } |
| 5163 | |
| 5164 | static int |
Haibo Huang | 4ccd683 | 2020-04-23 18:03:48 -0700 | [diff] [blame] | 5165 | fix_offset(pcap_t *handle, struct bpf_insn *p) |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 5166 | { |
| 5167 | /* |
Haibo Huang | 4ccd683 | 2020-04-23 18:03:48 -0700 | [diff] [blame] | 5168 | * Existing references to auxiliary data shouldn't be adjusted. |
| 5169 | * |
| 5170 | * Note that SKF_AD_OFF is negative, but p->k is unsigned, so |
| 5171 | * we use >= and cast SKF_AD_OFF to unsigned. |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 5172 | */ |
Haibo Huang | 4ccd683 | 2020-04-23 18:03:48 -0700 | [diff] [blame] | 5173 | if (p->k >= (bpf_u_int32)SKF_AD_OFF) |
| 5174 | return 0; |
| 5175 | if (handle->linktype == DLT_LINUX_SLL2) { |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 5176 | /* |
Haibo Huang | 4ccd683 | 2020-04-23 18:03:48 -0700 | [diff] [blame] | 5177 | * What's the offset? |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 5178 | */ |
Haibo Huang | 4ccd683 | 2020-04-23 18:03:48 -0700 | [diff] [blame] | 5179 | if (p->k >= SLL2_HDR_LEN) { |
| 5180 | /* |
| 5181 | * It's within the link-layer payload; that starts |
| 5182 | * at an offset of 0, as far as the kernel packet |
| 5183 | * filter is concerned, so subtract the length of |
| 5184 | * the link-layer header. |
| 5185 | */ |
| 5186 | p->k -= SLL2_HDR_LEN; |
| 5187 | } else if (p->k == 0) { |
| 5188 | /* |
| 5189 | * It's the protocol field; map it to the |
| 5190 | * special magic kernel offset for that field. |
| 5191 | */ |
| 5192 | p->k = SKF_AD_OFF + SKF_AD_PROTOCOL; |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 5193 | } else if (p->k == 4) { |
| 5194 | /* |
| 5195 | * It's the ifindex field; map it to the |
| 5196 | * special magic kernel offset for that field. |
| 5197 | */ |
| 5198 | p->k = SKF_AD_OFF + SKF_AD_IFINDEX; |
Haibo Huang | 4ccd683 | 2020-04-23 18:03:48 -0700 | [diff] [blame] | 5199 | } else if (p->k == 10) { |
| 5200 | /* |
| 5201 | * It's the packet type field; map it to the |
| 5202 | * special magic kernel offset for that field. |
| 5203 | */ |
| 5204 | p->k = SKF_AD_OFF + SKF_AD_PKTTYPE; |
| 5205 | } else if ((bpf_int32)(p->k) > 0) { |
| 5206 | /* |
| 5207 | * It's within the header, but it's not one of |
| 5208 | * those fields; we can't do that in the kernel, |
| 5209 | * so punt to userland. |
| 5210 | */ |
| 5211 | return -1; |
| 5212 | } |
| 5213 | } else { |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 5214 | /* |
Haibo Huang | 4ccd683 | 2020-04-23 18:03:48 -0700 | [diff] [blame] | 5215 | * What's the offset? |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 5216 | */ |
Haibo Huang | 4ccd683 | 2020-04-23 18:03:48 -0700 | [diff] [blame] | 5217 | if (p->k >= SLL_HDR_LEN) { |
| 5218 | /* |
| 5219 | * It's within the link-layer payload; that starts |
| 5220 | * at an offset of 0, as far as the kernel packet |
| 5221 | * filter is concerned, so subtract the length of |
| 5222 | * the link-layer header. |
| 5223 | */ |
| 5224 | p->k -= SLL_HDR_LEN; |
| 5225 | } else if (p->k == 0) { |
| 5226 | /* |
| 5227 | * It's the packet type field; map it to the |
| 5228 | * special magic kernel offset for that field. |
| 5229 | */ |
| 5230 | p->k = SKF_AD_OFF + SKF_AD_PKTTYPE; |
| 5231 | } else if (p->k == 14) { |
| 5232 | /* |
| 5233 | * It's the protocol field; map it to the |
| 5234 | * special magic kernel offset for that field. |
| 5235 | */ |
| 5236 | p->k = SKF_AD_OFF + SKF_AD_PROTOCOL; |
| 5237 | } else if ((bpf_int32)(p->k) > 0) { |
| 5238 | /* |
| 5239 | * It's within the header, but it's not one of |
| 5240 | * those fields; we can't do that in the kernel, |
| 5241 | * so punt to userland. |
| 5242 | */ |
| 5243 | return -1; |
| 5244 | } |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 5245 | } |
| 5246 | return 0; |
| 5247 | } |
| 5248 | |
| 5249 | static int |
| 5250 | set_kernel_filter(pcap_t *handle, struct sock_fprog *fcode) |
| 5251 | { |
| 5252 | int total_filter_on = 0; |
| 5253 | int save_mode; |
| 5254 | int ret; |
| 5255 | int save_errno; |
| 5256 | |
| 5257 | /* |
| 5258 | * The socket filter code doesn't discard all packets queued |
| 5259 | * up on the socket when the filter is changed; this means |
| 5260 | * that packets that don't match the new filter may show up |
| 5261 | * after the new filter is put onto the socket, if those |
| 5262 | * packets haven't yet been read. |
| 5263 | * |
| 5264 | * This means, for example, that if you do a tcpdump capture |
| 5265 | * with a filter, the first few packets in the capture might |
| 5266 | * be packets that wouldn't have passed the filter. |
| 5267 | * |
| 5268 | * We therefore discard all packets queued up on the socket |
| 5269 | * when setting a kernel filter. (This isn't an issue for |
| 5270 | * userland filters, as the userland filtering is done after |
| 5271 | * packets are queued up.) |
| 5272 | * |
| 5273 | * To flush those packets, we put the socket in read-only mode, |
| 5274 | * and read packets from the socket until there are no more to |
| 5275 | * read. |
| 5276 | * |
| 5277 | * In order to keep that from being an infinite loop - i.e., |
| 5278 | * to keep more packets from arriving while we're draining |
| 5279 | * the queue - we put the "total filter", which is a filter |
| 5280 | * that rejects all packets, onto the socket before draining |
| 5281 | * the queue. |
| 5282 | * |
| 5283 | * This code deliberately ignores any errors, so that you may |
| 5284 | * get bogus packets if an error occurs, rather than having |
| 5285 | * the filtering done in userland even if it could have been |
| 5286 | * done in the kernel. |
| 5287 | */ |
| 5288 | if (setsockopt(handle->fd, SOL_SOCKET, SO_ATTACH_FILTER, |
| 5289 | &total_fcode, sizeof(total_fcode)) == 0) { |
| 5290 | char drain[1]; |
| 5291 | |
| 5292 | /* |
| 5293 | * Note that we've put the total filter onto the socket. |
| 5294 | */ |
| 5295 | total_filter_on = 1; |
| 5296 | |
| 5297 | /* |
| 5298 | * Save the socket's current mode, and put it in |
| 5299 | * non-blocking mode; we drain it by reading packets |
| 5300 | * until we get an error (which is normally a |
| 5301 | * "nothing more to be read" error). |
| 5302 | */ |
| 5303 | save_mode = fcntl(handle->fd, F_GETFL, 0); |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 5304 | if (save_mode == -1) { |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 5305 | pcap_fmt_errmsg_for_errno(handle->errbuf, |
| 5306 | PCAP_ERRBUF_SIZE, errno, |
| 5307 | "can't get FD flags when changing filter"); |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 5308 | return -2; |
| 5309 | } |
| 5310 | if (fcntl(handle->fd, F_SETFL, save_mode | O_NONBLOCK) < 0) { |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 5311 | pcap_fmt_errmsg_for_errno(handle->errbuf, |
| 5312 | PCAP_ERRBUF_SIZE, errno, |
| 5313 | "can't set nonblocking mode when changing filter"); |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 5314 | return -2; |
| 5315 | } |
| 5316 | while (recv(handle->fd, &drain, sizeof drain, MSG_TRUNC) >= 0) |
| 5317 | ; |
| 5318 | save_errno = errno; |
| 5319 | if (save_errno != EAGAIN) { |
| 5320 | /* |
| 5321 | * Fatal error. |
| 5322 | * |
| 5323 | * If we can't restore the mode or reset the |
| 5324 | * kernel filter, there's nothing we can do. |
| 5325 | */ |
| 5326 | (void)fcntl(handle->fd, F_SETFL, save_mode); |
| 5327 | (void)reset_kernel_filter(handle); |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 5328 | pcap_fmt_errmsg_for_errno(handle->errbuf, |
| 5329 | PCAP_ERRBUF_SIZE, save_errno, |
| 5330 | "recv failed when changing filter"); |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 5331 | return -2; |
| 5332 | } |
| 5333 | if (fcntl(handle->fd, F_SETFL, save_mode) == -1) { |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 5334 | pcap_fmt_errmsg_for_errno(handle->errbuf, |
| 5335 | PCAP_ERRBUF_SIZE, errno, |
| 5336 | "can't restore FD flags when changing filter"); |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 5337 | return -2; |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 5338 | } |
| 5339 | } |
| 5340 | |
| 5341 | /* |
| 5342 | * Now attach the new filter. |
| 5343 | */ |
| 5344 | ret = setsockopt(handle->fd, SOL_SOCKET, SO_ATTACH_FILTER, |
| 5345 | fcode, sizeof(*fcode)); |
| 5346 | if (ret == -1 && total_filter_on) { |
| 5347 | /* |
| 5348 | * Well, we couldn't set that filter on the socket, |
| 5349 | * but we could set the total filter on the socket. |
| 5350 | * |
| 5351 | * This could, for example, mean that the filter was |
| 5352 | * too big to put into the kernel, so we'll have to |
| 5353 | * filter in userland; in any case, we'll be doing |
| 5354 | * filtering in userland, so we need to remove the |
| 5355 | * total filter so we see packets. |
| 5356 | */ |
| 5357 | save_errno = errno; |
| 5358 | |
| 5359 | /* |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 5360 | * If this fails, we're really screwed; we have the |
| 5361 | * total filter on the socket, and it won't come off. |
| 5362 | * Report it as a fatal error. |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 5363 | */ |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 5364 | if (reset_kernel_filter(handle) == -1) { |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 5365 | pcap_fmt_errmsg_for_errno(handle->errbuf, |
| 5366 | PCAP_ERRBUF_SIZE, errno, |
| 5367 | "can't remove kernel total filter"); |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 5368 | return -2; /* fatal error */ |
| 5369 | } |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 5370 | |
| 5371 | errno = save_errno; |
| 5372 | } |
| 5373 | return ret; |
| 5374 | } |
| 5375 | |
| 5376 | static int |
| 5377 | reset_kernel_filter(pcap_t *handle) |
| 5378 | { |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 5379 | int ret; |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 5380 | /* |
| 5381 | * setsockopt() barfs unless it get a dummy parameter. |
| 5382 | * valgrind whines unless the value is initialized, |
| 5383 | * as it has no idea that setsockopt() ignores its |
| 5384 | * parameter. |
| 5385 | */ |
| 5386 | int dummy = 0; |
| 5387 | |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 5388 | ret = setsockopt(handle->fd, SOL_SOCKET, SO_DETACH_FILTER, |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 5389 | &dummy, sizeof(dummy)); |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 5390 | /* |
| 5391 | * Ignore ENOENT - it means "we don't have a filter", so there |
| 5392 | * was no filter to remove, and there's still no filter. |
| 5393 | * |
| 5394 | * Also ignore ENONET, as a lot of kernel versions had a |
| 5395 | * typo where ENONET, rather than ENOENT, was returned. |
| 5396 | */ |
| 5397 | if (ret == -1 && errno != ENOENT && errno != ENONET) |
| 5398 | return -1; |
| 5399 | return 0; |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 5400 | } |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 5401 | |
| 5402 | int |
| 5403 | pcap_set_protocol_linux(pcap_t *p, int protocol) |
| 5404 | { |
| 5405 | if (pcap_check_activated(p)) |
| 5406 | return (PCAP_ERROR_ACTIVATED); |
| 5407 | p->opt.protocol = protocol; |
| 5408 | return (0); |
| 5409 | } |
| 5410 | |
| 5411 | /* |
| 5412 | * Libpcap version string. |
| 5413 | */ |
| 5414 | const char * |
| 5415 | pcap_lib_version(void) |
| 5416 | { |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 5417 | #if defined(HAVE_TPACKET3) |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 5418 | return (PCAP_VERSION_STRING " (with TPACKET_V3)"); |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 5419 | #else |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 5420 | return (PCAP_VERSION_STRING " (with TPACKET_V2)"); |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 5421 | #endif |
| 5422 | } |