libpcap  1.10.1
About: libpcap is a packet filter library used by tools like tcpdump.
  Fossies Dox: libpcap-1.10.1.tar.gz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

pcap-linux.c File Reference
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <limits.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <sys/utsname.h>
#include <sys/mman.h>
#include <linux/if.h>
#include <linux/if_packet.h>
#include <linux/sockios.h>
#include <linux/ethtool.h>
#include <netinet/in.h>
#include <linux/if_ether.h>
#include <linux/if_arp.h>
#include <poll.h>
#include <dirent.h>
#include <sys/eventfd.h>
#include "pcap-int.h"
#include "pcap/sll.h"
#include "pcap/vlan.h"
#include "diag-control.h"
#include <linux/types.h>
#include <linux/filter.h>
#include <linux/if_bonding.h>
Include dependency graph for pcap-linux.c:

Go to the source code of this file.

Data Structures

struct  pcap_linux
 
union  thdr
 
struct  dsa_proto
 

Macros

#define _GNU_SOURCE
 
#define __atomic_load_n(ptr, memory_model)   (*(ptr))
 
#define __atomic_store_n(ptr, val, memory_model)   *(ptr) = (val)
 
#define packet_mmap_acquire(pkt)    (__atomic_load_n(&pkt->tp_status, __ATOMIC_ACQUIRE) != TP_STATUS_KERNEL)
 
#define packet_mmap_release(pkt)    (__atomic_store_n(&pkt->tp_status, TP_STATUS_KERNEL, __ATOMIC_RELEASE))
 
#define packet_mmap_v3_acquire(pkt)    (__atomic_load_n(&pkt->hdr.bh1.block_status, __ATOMIC_ACQUIRE) != TP_STATUS_KERNEL)
 
#define packet_mmap_v3_release(pkt)    (__atomic_store_n(&pkt->hdr.bh1.block_status, TP_STATUS_KERNEL, __ATOMIC_RELEASE))
 
#define MAX_LINKHEADER_SIZE   256
 
#define BIGGER_THAN_ALL_MTUS   (64*1024)
 
#define MUST_CLEAR_RFMON   0x00000001 /* clear rfmon (monitor) mode */
 
#define MUST_DELETE_MONIF   0x00000002 /* delete monitor-mode interface */
 
#define RING_GET_FRAME_AT(h, offset)   (((u_char **)h->buffer)[(offset)])
 
#define RING_GET_CURRENT_FRAME(h)   RING_GET_FRAME_AT(h, h->offset)
 
#define VLAN_VALID(hdr, hv)   ((hv)->tp_vlan_tci != 0 || ((hdr)->tp_status & 0x10))
 
#define VLAN_TPID(hdr, hv)   ETH_P_8021Q
 
#define ARPHRD_CAN   280
 
#define ARPHRD_IEEE802_TR   800 /* From Linux 2.4 */
 
#define ARPHRD_FDDI   774
 
#define ARPHRD_ATM   19
 
#define ARPHRD_IEEE80211   801
 
#define ARPHRD_IEEE80211_PRISM   802
 
#define ARPHRD_IEEE80211_RADIOTAP   803
 
#define ARPHRD_CISCO   513 /* previously ARPHRD_HDLC */
 
#define ARPHRD_SIT   776 /* From Linux 2.2.13 */
 
#define ARPHRD_RAWHDLC   518
 
#define ARPHRD_DLCI   15
 
#define ARPHRD_FRAD   770
 
#define ARPHRD_FCPP   784
 
#define ARPHRD_FCAL   785
 
#define ARPHRD_FCPL   786
 
#define ARPHRD_FCFABRIC   787
 
#define ARPHRD_IRDA   783
 
#define ARPHRD_LAPD   8445
 
#define ARPHRD_NONE   0xFFFE
 
#define ARPHRD_IEEE802154   804
 
#define ARPHRD_NETLINK   824
 
#define ARPHRD_VSOCKMON   826
 
#define MAX(a, b)   ((a)>(b)?(a):(b))
 

Typedefs

typedef int socklen_t
 

Functions

static int get_if_flags (const char *, bpf_u_int32 *, char *)
 
static int is_wifi (const char *)
 
static void map_arphrd_to_dlt (pcap_t *, int, const char *, int)
 
static int pcap_activate_linux (pcap_t *)
 
static int activate_pf_packet (pcap_t *, int)
 
static int setup_mmapped (pcap_t *, int *)
 
static int pcap_can_set_rfmon_linux (pcap_t *)
 
static int pcap_inject_linux (pcap_t *, const void *, int)
 
static int pcap_stats_linux (pcap_t *, struct pcap_stat *)
 
static int pcap_setfilter_linux (pcap_t *, struct bpf_program *)
 
static int pcap_setdirection_linux (pcap_t *, pcap_direction_t)
 
static int pcap_set_datalink_linux (pcap_t *, int)
 
static void pcap_cleanup_linux (pcap_t *)
 
static void destroy_ring (pcap_t *handle)
 
static int create_ring (pcap_t *handle, int *status)
 
static int prepare_tpacket_socket (pcap_t *handle)
 
static int pcap_read_linux_mmap_v2 (pcap_t *, int, pcap_handler, u_char *)
 
static int pcap_setnonblock_linux (pcap_t *p, int nonblock)
 
static int pcap_getnonblock_linux (pcap_t *p)
 
static void pcap_oneshot_linux (u_char *user, const struct pcap_pkthdr *h, const u_char *bytes)
 
static int iface_get_id (int fd, const char *device, char *ebuf)
 
static int iface_get_mtu (int fd, const char *device, char *ebuf)
 
static int iface_get_arptype (int fd, const char *device, char *ebuf)
 
static int iface_bind (int fd, int ifindex, char *ebuf, int protocol)
 
static int enter_rfmon_mode (pcap_t *handle, int sock_fd, const char *device)
 
static int iface_get_offload (pcap_t *handle)
 
static int fix_program (pcap_t *handle, struct sock_fprog *fcode)
 
static int fix_offset (pcap_t *handle, struct bpf_insn *p)
 
static int set_kernel_filter (pcap_t *handle, struct sock_fprog *fcode)
 
static int reset_kernel_filter (pcap_t *handle)
 
static int iface_dsa_get_proto_info (const char *device, pcap_t *handle)
 
pcap_tpcap_create_interface (const char *device, char *ebuf)
 
static int pcap_protocol (pcap_t *handle)
 
static long long int linux_get_stat (const char *if_name, const char *stat)
 
static long long int linux_if_drops (const char *if_name)
 
static void set_poll_timeout (struct pcap_linux *handlep)
 
static void pcap_breakloop_linux (pcap_t *handle)
 
static int linux_check_direction (const pcap_t *handle, const struct sockaddr_ll *sll)
 
static int device_still_exists (pcap_t *handle)
 
static int can_be_bound (const char *name)
 
static int get_if_ioctl_socket (void)
 
int pcap_platform_finddevs (pcap_if_list_t *devlistp, char *errbuf)
 
static void set_dlt_list_cooked (pcap_t *handle, int sock_fd)
 
static int init_tpacket (pcap_t *handle, int version, const char *version_str)
 
static u_int pcap_get_ring_frame_status (pcap_t *handle, int offset)
 
static int pcap_wait_for_frames_mmap (pcap_t *handle)
 
static int pcap_handle_packet_mmap (pcap_t *handle, pcap_handler callback, u_char *user, unsigned char *frame, unsigned int tp_len, unsigned int tp_mac, unsigned int tp_snaplen, unsigned int tp_sec, unsigned int tp_usec, int tp_vlan_tci_valid, __u16 tp_vlan_tci, __u16 tp_vlan_tpid)
 
int pcap_set_protocol_linux (pcap_t *p, int protocol)
 
const char * pcap_lib_version (void)
 

Variables

static const struct timeval netdown_timeout
 
static struct sock_filter total_insn = { (u_short)( 0x06 | 0x00 ), 0, 0, 0 }
 
static struct sock_fprog total_fcode = { 1, &total_insn }
 
static const char any_descr [] = "Pseudo-device that captures on all interfaces"
 
static struct dsa_proto dsa_protos []
 

Macro Definition Documentation

◆ __atomic_load_n

#define __atomic_load_n (   ptr,
  memory_model 
)    (*(ptr))

Definition at line 127 of file pcap-linux.c.

◆ __atomic_store_n

#define __atomic_store_n (   ptr,
  val,
  memory_model 
)    *(ptr) = (val)

Definition at line 130 of file pcap-linux.c.

◆ _GNU_SOURCE

#define _GNU_SOURCE

Definition at line 71 of file pcap-linux.c.

◆ ARPHRD_ATM

#define ARPHRD_ATM   19

◆ ARPHRD_CAN

#define ARPHRD_CAN   280

◆ ARPHRD_CISCO

#define ARPHRD_CISCO   513 /* previously ARPHRD_HDLC */

◆ ARPHRD_DLCI

#define ARPHRD_DLCI   15

◆ ARPHRD_FCAL

#define ARPHRD_FCAL   785

◆ ARPHRD_FCFABRIC

#define ARPHRD_FCFABRIC   787

◆ ARPHRD_FCPL

#define ARPHRD_FCPL   786

◆ ARPHRD_FCPP

#define ARPHRD_FCPP   784

◆ ARPHRD_FDDI

#define ARPHRD_FDDI   774

◆ ARPHRD_FRAD

#define ARPHRD_FRAD   770

◆ ARPHRD_IEEE80211

#define ARPHRD_IEEE80211   801

◆ ARPHRD_IEEE80211_PRISM

#define ARPHRD_IEEE80211_PRISM   802

◆ ARPHRD_IEEE80211_RADIOTAP

#define ARPHRD_IEEE80211_RADIOTAP   803

◆ ARPHRD_IEEE802154

#define ARPHRD_IEEE802154   804

◆ ARPHRD_IEEE802_TR

#define ARPHRD_IEEE802_TR   800 /* From Linux 2.4 */

◆ ARPHRD_IRDA

#define ARPHRD_IRDA   783

◆ ARPHRD_LAPD

#define ARPHRD_LAPD   8445

◆ ARPHRD_NETLINK

#define ARPHRD_NETLINK   824

◆ ARPHRD_NONE

#define ARPHRD_NONE   0xFFFE

◆ ARPHRD_RAWHDLC

#define ARPHRD_RAWHDLC   518

◆ ARPHRD_SIT

#define ARPHRD_SIT   776 /* From Linux 2.2.13 */

◆ ARPHRD_VSOCKMON

#define ARPHRD_VSOCKMON   826

◆ BIGGER_THAN_ALL_MTUS

#define BIGGER_THAN_ALL_MTUS   (64*1024)

Definition at line 178 of file pcap-linux.c.

◆ MAX

#define MAX (   a,
 
)    ((a)>(b)?(a):(b))

Definition at line 2851 of file pcap-linux.c.

◆ MAX_LINKHEADER_SIZE

#define MAX_LINKHEADER_SIZE   256

Definition at line 171 of file pcap-linux.c.

◆ MUST_CLEAR_RFMON

#define MUST_CLEAR_RFMON   0x00000001 /* clear rfmon (monitor) mode */

Definition at line 215 of file pcap-linux.c.

◆ MUST_DELETE_MONIF

#define MUST_DELETE_MONIF   0x00000002 /* delete monitor-mode interface */

Definition at line 216 of file pcap-linux.c.

◆ packet_mmap_acquire

#define packet_mmap_acquire (   pkt)     (__atomic_load_n(&pkt->tp_status, __ATOMIC_ACQUIRE) != TP_STATUS_KERNEL)

Definition at line 133 of file pcap-linux.c.

◆ packet_mmap_release

#define packet_mmap_release (   pkt)     (__atomic_store_n(&pkt->tp_status, TP_STATUS_KERNEL, __ATOMIC_RELEASE))

Definition at line 135 of file pcap-linux.c.

◆ packet_mmap_v3_acquire

#define packet_mmap_v3_acquire (   pkt)     (__atomic_load_n(&pkt->hdr.bh1.block_status, __ATOMIC_ACQUIRE) != TP_STATUS_KERNEL)

Definition at line 137 of file pcap-linux.c.

◆ packet_mmap_v3_release

#define packet_mmap_v3_release (   pkt)     (__atomic_store_n(&pkt->hdr.bh1.block_status, TP_STATUS_KERNEL, __ATOMIC_RELEASE))

Definition at line 139 of file pcap-linux.c.

◆ RING_GET_CURRENT_FRAME

#define RING_GET_CURRENT_FRAME (   h)    RING_GET_FRAME_AT(h, h->offset)

Definition at line 244 of file pcap-linux.c.

◆ RING_GET_FRAME_AT

#define RING_GET_FRAME_AT (   h,
  offset 
)    (((u_char **)h->buffer)[(offset)])

Definition at line 243 of file pcap-linux.c.

◆ VLAN_TPID

#define VLAN_TPID (   hdr,
  hv 
)    ETH_P_8021Q

Definition at line 301 of file pcap-linux.c.

◆ VLAN_VALID

#define VLAN_VALID (   hdr,
  hv 
)    ((hv)->tp_vlan_tci != 0 || ((hdr)->tp_status & 0x10))

Definition at line 295 of file pcap-linux.c.

Typedef Documentation

◆ socklen_t

typedef int socklen_t

Definition at line 168 of file pcap-linux.c.

Function Documentation

◆ activate_pf_packet()

◆ can_be_bound()

static int can_be_bound ( const char *  name)
static

Definition at line 1393 of file pcap-linux.c.

Referenced by pcap_platform_finddevs().

◆ create_ring()

◆ destroy_ring()

static void destroy_ring ( pcap_t handle)
static

◆ device_still_exists()

static int device_still_exists ( pcap_t handle)
static

◆ enter_rfmon_mode()

static int enter_rfmon_mode ( pcap_t handle,
int  sock_fd,
const char *  device 
)
static

Definition at line 4641 of file pcap-linux.c.

Referenced by activate_pf_packet().

◆ fix_offset()

static int fix_offset ( pcap_t handle,
struct bpf_insn p 
)
static

Definition at line 5178 of file pcap-linux.c.

References DLT_LINUX_SLL2, bpf_insn::k, pcap::linktype, SLL2_HDR_LEN, and SLL_HDR_LEN.

Referenced by fix_program().

◆ fix_program()

static int fix_program ( pcap_t handle,
struct sock_fprog *  fcode 
)
static

◆ get_if_flags()

◆ get_if_ioctl_socket()

static int get_if_ioctl_socket ( void  )
static

Definition at line 1402 of file pcap-linux.c.

References close(), errno, ioctl(), and socket().

Referenced by get_if_flags().

◆ iface_bind()

static int iface_bind ( int  fd,
int  ifindex,
char *  ebuf,
int  protocol 
)
static

◆ iface_dsa_get_proto_info()

static int iface_dsa_get_proto_info ( const char *  device,
pcap_t handle 
)
static

◆ iface_get_arptype()

static int iface_get_arptype ( int  fd,
const char *  device,
char *  ebuf 
)
static

◆ iface_get_id()

static int iface_get_id ( int  fd,
const char *  device,
char *  ebuf 
)
static

Definition at line 4425 of file pcap-linux.c.

References errno, ioctl(), PCAP_ERRBUF_SIZE, pcap_fmt_errmsg_for_errno(), and pcap_strlcpy().

Referenced by activate_pf_packet().

◆ iface_get_mtu()

static int iface_get_mtu ( int  fd,
const char *  device,
char *  ebuf 
)
static

◆ iface_get_offload()

static int iface_get_offload ( pcap_t handle)
static

Definition at line 4965 of file pcap-linux.c.

Referenced by create_ring().

◆ init_tpacket()

static int init_tpacket ( pcap_t handle,
int  version,
const char *  version_str 
)
static

◆ is_wifi()

static int is_wifi ( const char *  device)
static

Definition at line 1738 of file pcap-linux.c.

References stat.

Referenced by get_if_flags(), and map_arphrd_to_dlt().

◆ linux_check_direction()

static int linux_check_direction ( const pcap_t handle,
const struct sockaddr_ll *  sll 
)
inlinestatic

◆ linux_get_stat()

static long long int linux_get_stat ( const char *  if_name,
const char *  stat 
)
static

Definition at line 751 of file pcap-linux.c.

References close(), read(), snprintf(), and stat.

Referenced by linux_if_drops().

◆ linux_if_drops()

static long long int linux_if_drops ( const char *  if_name)
static

Definition at line 771 of file pcap-linux.c.

References linux_get_stat().

Referenced by pcap_activate_linux(), and pcap_stats_linux().

◆ map_arphrd_to_dlt()

◆ pcap_activate_linux()

◆ pcap_breakloop_linux()

static void pcap_breakloop_linux ( pcap_t handle)
static

Definition at line 940 of file pcap-linux.c.

References pcap_breakloop_common(), pcap_linux::poll_breakloop_fd, pcap::priv, and write().

Referenced by pcap_activate_linux().

◆ pcap_can_set_rfmon_linux()

static int pcap_can_set_rfmon_linux ( pcap_t handle)
static

Definition at line 709 of file pcap-linux.c.

References pcap_opt::device, and pcap::opt.

Referenced by pcap_create_interface().

◆ pcap_cleanup_linux()

◆ pcap_create_interface()

◆ pcap_get_ring_frame_status()

static u_int pcap_get_ring_frame_status ( pcap_t handle,
int  offset 
)
inlinestatic

◆ pcap_getnonblock_linux()

static int pcap_getnonblock_linux ( pcap_t p)
static

Definition at line 3349 of file pcap-linux.c.

References pcap::priv, and pcap_linux::timeout.

Referenced by pcap_activate_linux().

◆ pcap_handle_packet_mmap()

◆ pcap_inject_linux()

static int pcap_inject_linux ( pcap_t handle,
const void *  buf,
int  size 
)
static

◆ pcap_lib_version()

const char* pcap_lib_version ( void  )

Definition at line 5428 of file pcap-linux.c.

References PCAP_VERSION_STRING.

◆ pcap_oneshot_linux()

static void pcap_oneshot_linux ( u_char *  user,
const struct pcap_pkthdr h,
const u_char *  bytes 
)
static

◆ pcap_platform_finddevs()

int pcap_platform_finddevs ( pcap_if_list_t devlistp,
char *  errbuf 
)

◆ pcap_protocol()

static int pcap_protocol ( pcap_t handle)
static

Definition at line 697 of file pcap-linux.c.

References pcap::opt.

Referenced by pcap_activate_linux().

◆ pcap_read_linux_mmap_v2()

◆ pcap_set_datalink_linux()

static int pcap_set_datalink_linux ( pcap_t handle,
int  dlt 
)
static

Definition at line 1111 of file pcap-linux.c.

References pcap::linktype.

Referenced by pcap_activate_linux().

◆ pcap_set_protocol_linux()

int pcap_set_protocol_linux ( pcap_t p,
int  protocol 
)

Definition at line 5416 of file pcap-linux.c.

References pcap::opt, pcap_check_activated(), and PCAP_ERROR_ACTIVATED.

◆ pcap_setdirection_linux()

static int pcap_setdirection_linux ( pcap_t handle,
pcap_direction_t  d 
)
static

Definition at line 1727 of file pcap-linux.c.

References pcap::direction.

Referenced by pcap_activate_linux().

◆ pcap_setfilter_linux()

◆ pcap_setnonblock_linux()

static int pcap_setnonblock_linux ( pcap_t p,
int  nonblock 
)
static

Definition at line 3358 of file pcap-linux.c.

References pcap_setnonblock_fd(), pcap::priv, set_poll_timeout(), and pcap_linux::timeout.

Referenced by pcap_activate_linux().

◆ pcap_stats_linux()

◆ pcap_wait_for_frames_mmap()

◆ prepare_tpacket_socket()

static int prepare_tpacket_socket ( pcap_t handle)
static

Definition at line 2786 of file pcap-linux.c.

References pcap::errbuf, pcap_opt::immediate, init_tpacket(), pcap::opt, PCAP_ERRBUF_SIZE, and snprintf().

Referenced by setup_mmapped().

◆ reset_kernel_filter()

static int reset_kernel_filter ( pcap_t handle)
static

Definition at line 5390 of file pcap-linux.c.

References errno, pcap::fd, and setsockopt().

Referenced by pcap_setfilter_linux(), and set_kernel_filter().

◆ set_dlt_list_cooked()

static void set_dlt_list_cooked ( pcap_t handle,
int  sock_fd 
)
static

Definition at line 2268 of file pcap-linux.c.

Referenced by activate_pf_packet().

◆ set_kernel_filter()

static int set_kernel_filter ( pcap_t handle,
struct sock_fprog *  fcode 
)
static

◆ set_poll_timeout()

static void set_poll_timeout ( struct pcap_linux handlep)
static

◆ setup_mmapped()

Variable Documentation

◆ any_descr

const char any_descr[] = "Pseudo-device that captures on all interfaces"
static

Definition at line 1387 of file pcap-linux.c.

Referenced by pcap_platform_finddevs().

◆ dsa_protos

struct dsa_proto dsa_protos[]
static
Initial value:
= {
{ "none", 1 },
{ "brcm", 281 },
{ "brcm-prepend", 282 },
{ "dsa", 284 },
{ "edsa", 285 },
}

Referenced by iface_dsa_get_proto_info().

◆ netdown_timeout

const struct timeval netdown_timeout
static
Initial value:
= {
0, 1000
}

Definition at line 255 of file pcap-linux.c.

Referenced by pcap_wait_for_frames_mmap().

◆ total_fcode

struct sock_fprog total_fcode = { 1, &total_insn }
static

Definition at line 330 of file pcap-linux.c.

Referenced by set_kernel_filter().

◆ total_insn

struct sock_filter total_insn = { (u_short)( 0x06 | 0x00 ), 0, 0, 0 }
static

Definition at line 330 of file pcap-linux.c.

Referenced by pcap_activate_bpf(), and pcap_activate_npf().