2 * netsniff-ng - the packet sniffing beast
3 * By Daniel Borkmann <daniel@netsniff-ng.org>
4 * Copyright 2009, 2010 Daniel Borkmann.
5 * Copyright 2010 Emmanuel Roullit.
15 #include <linux/if_packet.h>
20 #define TCPDUMP_MAGIC 0xa1b2c3d4
21 #define PCAP_VERSION_MAJOR 2
22 #define PCAP_VERSION_MINOR 4
23 #define PCAP_DEFAULT_SNAPSHOT_LEN 65535
25 #define LINKTYPE_NULL 0 /* BSD loopback encapsulation */
26 #define LINKTYPE_EN10MB 1 /* Ethernet (10Mb) */
27 #define LINKTYPE_EN3MB 2 /* Experimental Ethernet (3Mb) */
28 #define LINKTYPE_AX25 3 /* Amateur Radio AX.25 */
29 #define LINKTYPE_PRONET 4 /* Proteon ProNET Token Ring */
30 #define LINKTYPE_CHAOS 5 /* Chaos */
31 #define LINKTYPE_IEEE802 6 /* 802.5 Token Ring */
32 #define LINKTYPE_ARCNET 7 /* ARCNET, with BSD-style header */
33 #define LINKTYPE_SLIP 8 /* Serial Line IP */
34 #define LINKTYPE_PPP 9 /* Point-to-point Protocol */
35 #define LINKTYPE_FDDI 10 /* FDDI */
39 uint16_t version_major
;
40 uint16_t version_minor
;
52 struct pcap_nsf_pkthdr
{
59 struct pcap_timeval ts
;
64 static inline void tpacket_hdr_to_pcap_pkthdr(struct tpacket_hdr
*thdr
,
65 struct pcap_pkthdr
*phdr
)
67 phdr
->ts
.tv_sec
= thdr
->tp_sec
;
68 phdr
->ts
.tv_usec
= thdr
->tp_usec
;
69 phdr
->caplen
= thdr
->tp_snaplen
;
70 phdr
->len
= thdr
->tp_len
;
73 static inline void pcap_pkthdr_to_tpacket_hdr(struct pcap_pkthdr
*phdr
,
74 struct tpacket_hdr
*thdr
)
76 thdr
->tp_sec
= phdr
->ts
.tv_sec
;
77 thdr
->tp_usec
= phdr
->ts
.tv_usec
;
78 thdr
->tp_snaplen
= phdr
->caplen
;
79 thdr
->tp_len
= phdr
->len
;
82 struct pcap_file_ops
{
83 int (*pull_file_header
)(int fd
);
84 int (*push_file_header
)(int fd
);
85 ssize_t (*write_pcap_pkt
)(int fd
, struct pcap_pkthdr
*hdr
,
87 ssize_t (*read_pcap_pkt
)(int fd
, struct pcap_pkthdr
*hdr
,
91 static inline void pcap_prepare_header(struct pcap_filehdr
*hdr
,
93 int32_t thiszone
, uint32_t snaplen
)
95 hdr
->magic
= TCPDUMP_MAGIC
;
96 hdr
->version_major
= PCAP_VERSION_MAJOR
;
97 hdr
->version_minor
= PCAP_VERSION_MINOR
;
98 hdr
->thiszone
= thiszone
;
100 hdr
->snaplen
= snaplen
;
101 hdr
->linktype
= linktype
;
104 static inline void pcap_validate_header_maybe_die(struct pcap_filehdr
*hdr
)
106 if (unlikely(hdr
->magic
!= TCPDUMP_MAGIC
||
107 hdr
->version_major
!= PCAP_VERSION_MAJOR
||
108 hdr
->version_minor
!= PCAP_VERSION_MINOR
||
109 hdr
->linktype
!= LINKTYPE_EN10MB
))
110 panic("This file has not a valid pcap header!\n");