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.
6 * Subject to the GPL, version 2.
16 #include <linux/if_packet.h>
21 #define TCPDUMP_MAGIC 0xa1b2c3d4
22 #define PCAP_VERSION_MAJOR 2
23 #define PCAP_VERSION_MINOR 4
24 #define PCAP_DEFAULT_SNAPSHOT_LEN 65535
26 #define LINKTYPE_NULL 0 /* BSD loopback encapsulation */
27 #define LINKTYPE_EN10MB 1 /* Ethernet (10Mb) */
28 #define LINKTYPE_EN3MB 2 /* Experimental Ethernet (3Mb) */
29 #define LINKTYPE_AX25 3 /* Amateur Radio AX.25 */
30 #define LINKTYPE_PRONET 4 /* Proteon ProNET Token Ring */
31 #define LINKTYPE_CHAOS 5 /* Chaos */
32 #define LINKTYPE_IEEE802 6 /* 802.5 Token Ring */
33 #define LINKTYPE_ARCNET 7 /* ARCNET, with BSD-style header */
34 #define LINKTYPE_SLIP 8 /* Serial Line IP */
35 #define LINKTYPE_PPP 9 /* Point-to-point Protocol */
36 #define LINKTYPE_FDDI 10 /* FDDI */
37 #define LINKTYPE_IEEE802_11 105 /* IEEE 802.11 wireless */
41 uint16_t version_major
;
42 uint16_t version_minor
;
54 struct pcap_nsf_pkthdr
{
61 struct pcap_timeval ts
;
66 static inline void tpacket_hdr_to_pcap_pkthdr(struct tpacket2_hdr
*thdr
,
67 struct pcap_pkthdr
*phdr
)
69 phdr
->ts
.tv_sec
= thdr
->tp_sec
;
70 phdr
->ts
.tv_usec
= (thdr
->tp_nsec
/ 1000);
71 phdr
->caplen
= thdr
->tp_snaplen
;
73 /* phdr->len = thdr->tp_len; */
74 phdr
->len
= thdr
->tp_snaplen
;
77 static inline void pcap_pkthdr_to_tpacket_hdr(struct pcap_pkthdr
*phdr
,
78 struct tpacket2_hdr
*thdr
)
80 thdr
->tp_sec
= phdr
->ts
.tv_sec
;
81 thdr
->tp_nsec
= phdr
->ts
.tv_usec
* 1000;
82 thdr
->tp_snaplen
= phdr
->caplen
;
83 thdr
->tp_len
= phdr
->len
;
86 enum pcap_ops_groups
{
88 #define PCAP_OPS_RW PCAP_OPS_RW
90 #define PCAP_OPS_SG PCAP_OPS_SG
92 #define PCAP_OPS_MMAP PCAP_OPS_MMAP
95 #define PCAP_OPS_MAX (__PCAP_OPS_MAX - 1)
96 #define PCAP_OPS_SIZ (__PCAP_OPS_MAX)
103 struct pcap_file_ops
{
105 int (*pull_file_header
)(int fd
, uint32_t *linktype
);
106 int (*push_file_header
)(int fd
, uint32_t linktype
);
107 int (*prepare_writing_pcap
)(int fd
);
108 ssize_t (*write_pcap_pkt
)(int fd
, struct pcap_pkthdr
*hdr
,
109 uint8_t *packet
, size_t len
);
110 void (*fsync_pcap
)(int fd
);
111 int (*prepare_reading_pcap
)(int fd
);
112 ssize_t (*read_pcap_pkt
)(int fd
, struct pcap_pkthdr
*hdr
,
113 uint8_t *packet
, size_t len
);
114 void (*prepare_close_pcap
)(int fd
, enum pcap_mode mode
);
117 extern const struct pcap_file_ops
*pcap_ops
[PCAP_OPS_SIZ
];
119 extern int pcap_ops_group_register(const struct pcap_file_ops
*ops
,
120 enum pcap_ops_groups group
);
121 extern void pcap_ops_group_unregister(enum pcap_ops_groups group
);
123 static inline const struct pcap_file_ops
*
124 pcap_ops_group_get(enum pcap_ops_groups group
)
126 return pcap_ops
[group
];
129 static inline void pcap_prepare_header(struct pcap_filehdr
*hdr
,
131 int32_t thiszone
, uint32_t snaplen
)
133 hdr
->magic
= TCPDUMP_MAGIC
;
134 hdr
->version_major
= PCAP_VERSION_MAJOR
;
135 hdr
->version_minor
= PCAP_VERSION_MINOR
;
136 hdr
->thiszone
= thiszone
;
138 hdr
->snaplen
= snaplen
;
139 hdr
->linktype
= linktype
;
142 static inline void pcap_validate_header(struct pcap_filehdr
*hdr
)
144 if (unlikely(hdr
->magic
!= TCPDUMP_MAGIC
||
145 hdr
->version_major
!= PCAP_VERSION_MAJOR
||
146 hdr
->version_minor
!= PCAP_VERSION_MINOR
||
147 (hdr
->linktype
!= LINKTYPE_EN10MB
&&
148 hdr
->linktype
!= LINKTYPE_IEEE802_11
)))
149 panic("This file has not a valid pcap header\n");
152 extern int init_pcap_mmap(int jumbo_support
);
153 extern int init_pcap_rw(int jumbo_support
);
154 extern int init_pcap_sg(int jumbo_support
);
156 extern void cleanup_pcap_mmap(void);
157 extern void cleanup_pcap_rw(void);
158 extern void cleanup_pcap_sg(void);
160 static inline int init_pcap(int jumbo_support
)
162 init_pcap_rw(jumbo_support
);
163 init_pcap_sg(jumbo_support
);
164 init_pcap_mmap(jumbo_support
);
169 static inline void cleanup_pcap(void)